2006-01-10 Jan Beulich <jbeulich@novell.com>
[official-gcc.git] / gcc / ada / decl.c
blobb64463a53b619fcaa28b1887ea1549ad6ce7ae11
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2005, 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 void components_to_record (tree, Node_Id, tree, int, bool, tree *,
94 bool, bool, bool);
95 static int compare_field_bitpos (const PTR, const PTR);
96 static Uint annotate_value (tree);
97 static void annotate_rep (Entity_Id, tree);
98 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
99 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
100 static void set_rm_size (Uint, tree, Entity_Id);
101 static tree make_type_from_size (tree, tree, bool);
102 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
103 static void check_ok_for_atomic (tree, Entity_Id, bool);
104 static int compatible_signatures_p (tree ftype1, tree ftype2);
106 /* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
107 GCC type corresponding to that entity. GNAT_ENTITY is assumed to
108 refer to an Ada type. */
110 tree
111 gnat_to_gnu_type (Entity_Id gnat_entity)
113 tree gnu_decl;
115 /* The back end never attempts to annotate generic types */
116 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
117 return void_type_node;
119 /* Convert the ada entity type into a GCC TYPE_DECL node. */
120 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
121 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
122 return TREE_TYPE (gnu_decl);
125 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
126 entity, this routine returns the equivalent GCC tree for that entity
127 (an ..._DECL node) and associates the ..._DECL node with the input GNAT
128 defining identifier.
130 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
131 initial value (in GCC tree form). This is optional for variables.
132 For renamed entities, GNU_EXPR gives the object being renamed.
134 DEFINITION is nonzero if this call is intended for a definition. This is
135 used for separate compilation where it necessary to know whether an
136 external declaration or a definition should be created if the GCC equivalent
137 was not created previously. The value of 1 is normally used for a non-zero
138 DEFINITION, but a value of 2 is used in special circumstances, defined in
139 the code. */
141 tree
142 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
144 tree gnu_entity_id;
145 tree gnu_type = NULL_TREE;
146 /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
147 GNAT tree. This node will be associated with the GNAT node by calling
148 the save_gnu_tree routine at the end of the `switch' statement. */
149 tree gnu_decl = NULL_TREE;
150 /* true if we have already saved gnu_decl as a gnat association. */
151 bool saved = false;
152 /* Nonzero if we incremented defer_incomplete_level. */
153 bool this_deferred = false;
154 /* Nonzero if we incremented defer_debug_level. */
155 bool debug_deferred = false;
156 /* Nonzero if we incremented force_global. */
157 bool this_global = false;
158 /* Nonzero if we should check to see if elaborated during processing. */
159 bool maybe_present = false;
160 /* Nonzero if we made GNU_DECL and its type here. */
161 bool this_made_decl = false;
162 struct attrib *attr_list = NULL;
163 bool debug_info_p = (Needs_Debug_Info (gnat_entity)
164 || debug_info_level == DINFO_LEVEL_VERBOSE);
165 Entity_Kind kind = Ekind (gnat_entity);
166 Entity_Id gnat_temp;
167 unsigned int esize
168 = ((Known_Esize (gnat_entity)
169 && UI_Is_In_Int_Range (Esize (gnat_entity)))
170 ? MIN (UI_To_Int (Esize (gnat_entity)),
171 IN (kind, Float_Kind)
172 ? fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE)
173 : IN (kind, Access_Kind) ? POINTER_SIZE * 2
174 : LONG_LONG_TYPE_SIZE)
175 : LONG_LONG_TYPE_SIZE);
176 tree gnu_size = 0;
177 bool imported_p
178 = ((Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)))
179 || From_With_Type (gnat_entity));
180 unsigned int align = 0;
182 /* Since a use of an Itype is a definition, process it as such if it
183 is not in a with'ed unit. */
185 if (!definition && Is_Itype (gnat_entity)
186 && !present_gnu_tree (gnat_entity)
187 && In_Extended_Main_Code_Unit (gnat_entity))
189 /* Ensure that we are in a subprogram mentioned in the Scope
190 chain of this entity, our current scope is global,
191 or that we encountered a task or entry (where we can't currently
192 accurately check scoping). */
193 if (!current_function_decl
194 || DECL_ELABORATION_PROC_P (current_function_decl))
196 process_type (gnat_entity);
197 return get_gnu_tree (gnat_entity);
200 for (gnat_temp = Scope (gnat_entity);
201 Present (gnat_temp); gnat_temp = Scope (gnat_temp))
203 if (Is_Type (gnat_temp))
204 gnat_temp = Underlying_Type (gnat_temp);
206 if (Ekind (gnat_temp) == E_Subprogram_Body)
207 gnat_temp
208 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
210 if (IN (Ekind (gnat_temp), Subprogram_Kind)
211 && Present (Protected_Body_Subprogram (gnat_temp)))
212 gnat_temp = Protected_Body_Subprogram (gnat_temp);
214 if (Ekind (gnat_temp) == E_Entry
215 || Ekind (gnat_temp) == E_Entry_Family
216 || Ekind (gnat_temp) == E_Task_Type
217 || (IN (Ekind (gnat_temp), Subprogram_Kind)
218 && present_gnu_tree (gnat_temp)
219 && (current_function_decl
220 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
222 process_type (gnat_entity);
223 return get_gnu_tree (gnat_entity);
227 /* This abort means the entity "gnat_entity" has an incorrect scope,
228 i.e. that its scope does not correspond to the subprogram in which
229 it is declared */
230 gcc_unreachable ();
233 /* If this is entity 0, something went badly wrong. */
234 gcc_assert (Present (gnat_entity));
236 /* If we've already processed this entity, return what we got last time.
237 If we are defining the node, we should not have already processed it.
238 In that case, we will abort below when we try to save a new GCC tree for
239 this object. We also need to handle the case of getting a dummy type
240 when a Full_View exists. */
242 if (present_gnu_tree (gnat_entity)
243 && (! definition
244 || (Is_Type (gnat_entity) && imported_p)))
246 gnu_decl = get_gnu_tree (gnat_entity);
248 if (TREE_CODE (gnu_decl) == TYPE_DECL
249 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
250 && IN (kind, Incomplete_Or_Private_Kind)
251 && Present (Full_View (gnat_entity)))
253 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
254 NULL_TREE, 0);
256 save_gnu_tree (gnat_entity, NULL_TREE, false);
257 save_gnu_tree (gnat_entity, gnu_decl, false);
260 return gnu_decl;
263 /* If this is a numeric or enumeral type, or an access type, a nonzero
264 Esize must be specified unless it was specified by the programmer. */
265 gcc_assert (!Unknown_Esize (gnat_entity)
266 || Has_Size_Clause (gnat_entity)
267 || (!IN (kind, Numeric_Kind) && !IN (kind, Enumeration_Kind)
268 && (!IN (kind, Access_Kind)
269 || kind == E_Access_Protected_Subprogram_Type
270 || kind == E_Access_Subtype)));
272 /* Likewise, RM_Size must be specified for all discrete and fixed-point
273 types. */
274 gcc_assert (!IN (kind, Discrete_Or_Fixed_Point_Kind)
275 || !Unknown_RM_Size (gnat_entity));
277 /* Get the name of the entity and set up the line number and filename of
278 the original definition for use in any decl we make. */
279 gnu_entity_id = get_entity_name (gnat_entity);
280 Sloc_to_locus (Sloc (gnat_entity), &input_location);
282 /* If we get here, it means we have not yet done anything with this
283 entity. If we are not defining it here, it must be external,
284 otherwise we should have defined it already. */
285 gcc_assert (definition || Is_Public (gnat_entity) || type_annotate_only
286 || kind == E_Discriminant || kind == E_Component
287 || kind == E_Label
288 || (kind == E_Constant && Present (Full_View (gnat_entity)))
289 || IN (kind, Type_Kind));
291 /* For cases when we are not defining (i.e., we are referencing from
292 another compilation unit) Public entities, show we are at global level
293 for the purpose of computing scopes. Don't do this for components or
294 discriminants since the relevant test is whether or not the record is
295 being defined. But do this for Imported functions or procedures in
296 all cases. */
297 if ((!definition && Is_Public (gnat_entity)
298 && !Is_Statically_Allocated (gnat_entity)
299 && kind != E_Discriminant && kind != E_Component)
300 || (Is_Imported (gnat_entity)
301 && (kind == E_Function || kind == E_Procedure)))
302 force_global++, this_global = true;
304 /* Handle any attributes directly attached to the entity. */
305 if (Has_Gigi_Rep_Item (gnat_entity))
306 prepend_attributes (gnat_entity, &attr_list);
308 /* Machine_Attributes on types are expected to be propagated to subtypes.
309 The corresponding Gigi_Rep_Items are only attached to the first subtype
310 though, so we handle the propagation here. */
311 if (Is_Type (gnat_entity) && Base_Type (gnat_entity) != gnat_entity
312 && !Is_First_Subtype (gnat_entity)
313 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
314 prepend_attributes (First_Subtype (Base_Type (gnat_entity)), &attr_list);
316 switch (kind)
318 case E_Constant:
319 /* If this is a use of a deferred constant, get its full
320 declaration. */
321 if (!definition && Present (Full_View (gnat_entity)))
323 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
324 gnu_expr, definition);
325 saved = true;
326 break;
329 /* If we have an external constant that we are not defining,
330 get the expression that is was defined to represent. We
331 may throw that expression away later if it is not a
332 constant.
333 Do not retrieve the expression if it is an aggregate, because
334 in complex instantiation contexts it may not be expanded */
336 if (!definition
337 && Present (Expression (Declaration_Node (gnat_entity)))
338 && !No_Initialization (Declaration_Node (gnat_entity))
339 && (Nkind (Expression (Declaration_Node (gnat_entity)))
340 != N_Aggregate))
341 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
343 /* Ignore deferred constant definitions; they are processed fully in the
344 front-end. For deferred constant references, get the full
345 definition. On the other hand, constants that are renamings are
346 handled like variable renamings. If No_Initialization is set, this is
347 not a deferred constant but a constant whose value is built
348 manually. */
350 if (definition && !gnu_expr
351 && !No_Initialization (Declaration_Node (gnat_entity))
352 && No (Renamed_Object (gnat_entity)))
354 gnu_decl = error_mark_node;
355 saved = true;
356 break;
358 else if (!definition && IN (kind, Incomplete_Or_Private_Kind)
359 && Present (Full_View (gnat_entity)))
361 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
362 NULL_TREE, 0);
363 saved = true;
364 break;
367 goto object;
369 case E_Exception:
370 /* We used to special case VMS exceptions here to directly map them to
371 their associated condition code. Since this code had to be masked
372 dynamically to strip off the severity bits, this caused trouble in
373 the GCC/ZCX case because the "type" pointers we store in the tables
374 have to be static. We now don't special case here anymore, and let
375 the regular processing take place, which leaves us with a regular
376 exception data object for VMS exceptions too. The condition code
377 mapping is taken care of by the front end and the bitmasking by the
378 runtime library. */
379 goto object;
381 case E_Discriminant:
382 case E_Component:
384 /* The GNAT record where the component was defined. */
385 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
387 /* If the variable is an inherited record component (in the case of
388 extended record types), just return the inherited entity, which
389 must be a FIELD_DECL. Likewise for discriminants.
390 For discriminants of untagged records which have explicit
391 stored discriminants, return the entity for the corresponding
392 stored discriminant. Also use Original_Record_Component
393 if the record has a private extension. */
395 if (Present (Original_Record_Component (gnat_entity))
396 && Original_Record_Component (gnat_entity) != gnat_entity)
398 gnu_decl
399 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
400 gnu_expr, definition);
401 saved = true;
402 break;
405 /* If the enclosing record has explicit stored discriminants,
406 then it is an untagged record. If the Corresponding_Discriminant
407 is not empty then this must be a renamed discriminant and its
408 Original_Record_Component must point to the corresponding explicit
409 stored discriminant (i.e., we should have taken the previous
410 branch). */
412 else if (Present (Corresponding_Discriminant (gnat_entity))
413 && Is_Tagged_Type (gnat_record))
415 /* A tagged record has no explicit stored discriminants. */
417 gcc_assert (First_Discriminant (gnat_record)
418 == First_Stored_Discriminant (gnat_record));
419 gnu_decl
420 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
421 gnu_expr, definition);
422 saved = true;
423 break;
426 /* If the enclosing record has explicit stored discriminants,
427 then it is an untagged record. If the Corresponding_Discriminant
428 is not empty then this must be a renamed discriminant and its
429 Original_Record_Component must point to the corresponding explicit
430 stored discriminant (i.e., we should have taken the first
431 branch). */
433 else if (Present (Corresponding_Discriminant (gnat_entity))
434 && (First_Discriminant (gnat_record)
435 != First_Stored_Discriminant (gnat_record)))
436 gcc_unreachable ();
438 /* Otherwise, if we are not defining this and we have no GCC type
439 for the containing record, make one for it. Then we should
440 have made our own equivalent. */
441 else if (!definition && !present_gnu_tree (gnat_record))
443 /* ??? If this is in a record whose scope is a protected
444 type and we have an Original_Record_Component, use it.
445 This is a workaround for major problems in protected type
446 handling. */
448 Entity_Id Scop = Scope (Scope (gnat_entity));
449 if ((Is_Protected_Type (Scop)
450 || (Is_Private_Type (Scop)
451 && Present (Full_View (Scop))
452 && Is_Protected_Type (Full_View (Scop))))
453 && Present (Original_Record_Component (gnat_entity)))
455 gnu_decl
456 = gnat_to_gnu_entity (Original_Record_Component
457 (gnat_entity),
458 gnu_expr, definition);
459 saved = true;
460 break;
463 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
464 gnu_decl = get_gnu_tree (gnat_entity);
465 saved = true;
466 break;
469 else
470 /* Here we have no GCC type and this is a reference rather than a
471 definition. This should never happen. Most likely the cause is a
472 reference before declaration in the gnat tree for gnat_entity. */
473 gcc_unreachable ();
476 case E_Loop_Parameter:
477 case E_Out_Parameter:
478 case E_Variable:
480 /* Simple variables, loop variables, OUT parameters, and exceptions. */
481 object:
483 bool used_by_ref = false;
484 bool const_flag
485 = ((kind == E_Constant || kind == E_Variable)
486 && !Is_Statically_Allocated (gnat_entity)
487 && Is_True_Constant (gnat_entity)
488 && (((Nkind (Declaration_Node (gnat_entity))
489 == N_Object_Declaration)
490 && Present (Expression (Declaration_Node (gnat_entity))))
491 || Present (Renamed_Object (gnat_entity))));
492 bool inner_const_flag = const_flag;
493 bool static_p = Is_Statically_Allocated (gnat_entity);
494 bool mutable_p = false;
495 tree gnu_ext_name = NULL_TREE;
496 tree renamed_obj = NULL_TREE;
498 if (Present (Renamed_Object (gnat_entity)) && !definition)
500 if (kind == E_Exception)
501 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
502 NULL_TREE, 0);
503 else
504 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
507 /* Get the type after elaborating the renamed object. */
508 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
510 /* If this is a loop variable, its type should be the base type.
511 This is because the code for processing a loop determines whether
512 a normal loop end test can be done by comparing the bounds of the
513 loop against those of the base type, which is presumed to be the
514 size used for computation. But this is not correct when the size
515 of the subtype is smaller than the type. */
516 if (kind == E_Loop_Parameter)
517 gnu_type = get_base_type (gnu_type);
519 /* Reject non-renamed objects whose types are unconstrained arrays or
520 any object whose type is a dummy type or VOID_TYPE. */
522 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
523 && No (Renamed_Object (gnat_entity)))
524 || TYPE_IS_DUMMY_P (gnu_type)
525 || TREE_CODE (gnu_type) == VOID_TYPE)
527 gcc_assert (type_annotate_only);
528 if (this_global)
529 force_global--;
530 return error_mark_node;
533 /* If an alignment is specified, use it if valid. Note that
534 exceptions are objects but don't have alignments. We must do this
535 before we validate the size, since the alignment can affect the
536 size. */
537 if (kind != E_Exception && Known_Alignment (gnat_entity))
539 gcc_assert (Present (Alignment (gnat_entity)));
540 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
541 TYPE_ALIGN (gnu_type));
542 gnu_type = maybe_pad_type (gnu_type, NULL_TREE, align,
543 gnat_entity, "PAD", 0, definition, 1);
546 /* If we are defining the object, see if it has a Size value and
547 validate it if so. If we are not defining the object and a Size
548 clause applies, simply retrieve the value. We don't want to ignore
549 the clause and it is expected to have been validated already. Then
550 get the new type, if any. */
551 if (definition)
552 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
553 gnat_entity, VAR_DECL, false,
554 Has_Size_Clause (gnat_entity));
555 else if (Has_Size_Clause (gnat_entity))
556 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
558 if (gnu_size)
560 gnu_type
561 = make_type_from_size (gnu_type, gnu_size,
562 Has_Biased_Representation (gnat_entity));
564 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
565 gnu_size = NULL_TREE;
568 /* If this object has self-referential size, it must be a record with
569 a default value. We are supposed to allocate an object of the
570 maximum size in this case unless it is a constant with an
571 initializing expression, in which case we can get the size from
572 that. Note that the resulting size may still be a variable, so
573 this may end up with an indirect allocation. */
575 if (No (Renamed_Object (gnat_entity))
576 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
578 if (gnu_expr && kind == E_Constant)
579 gnu_size
580 = SUBSTITUTE_PLACEHOLDER_IN_EXPR
581 (TYPE_SIZE (TREE_TYPE (gnu_expr)), gnu_expr);
583 /* We may have no GNU_EXPR because No_Initialization is
584 set even though there's an Expression. */
585 else if (kind == E_Constant
586 && (Nkind (Declaration_Node (gnat_entity))
587 == N_Object_Declaration)
588 && Present (Expression (Declaration_Node (gnat_entity))))
589 gnu_size
590 = TYPE_SIZE (gnat_to_gnu_type
591 (Etype
592 (Expression (Declaration_Node (gnat_entity)))));
593 else
595 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
596 mutable_p = true;
600 /* If the size is zero bytes, make it one byte since some linkers have
601 trouble with zero-sized objects. If the object will have a
602 template, that will make it nonzero so don't bother. Also avoid
603 doing that for an object renaming or an object with an address
604 clause, as we would lose useful information on the view size
605 (e.g. for null array slices) and we are not allocating the object
606 here anyway. */
607 if (((gnu_size && integer_zerop (gnu_size))
608 || (TYPE_SIZE (gnu_type) && integer_zerop (TYPE_SIZE (gnu_type))))
609 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
610 || !Is_Array_Type (Etype (gnat_entity)))
611 && !Present (Renamed_Object (gnat_entity))
612 && !Present (Address_Clause (gnat_entity)))
613 gnu_size = bitsize_unit_node;
615 /* If this is an atomic object with no specified size and alignment,
616 but where the size of the type is a constant, set the alignment to
617 the lowest power of two greater than the size, or to the
618 biggest meaningful alignment, whichever is smaller. */
620 if (Is_Atomic (gnat_entity) && !gnu_size && align == 0
621 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
623 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
624 || 0 <= compare_tree_int (TYPE_SIZE (gnu_type),
625 BIGGEST_ALIGNMENT))
626 align = BIGGEST_ALIGNMENT;
627 else
628 align = ((unsigned int) 1
629 << (floor_log2 (tree_low_cst
630 (TYPE_SIZE (gnu_type), 1) - 1)
631 + 1));
634 /* If the object is set to have atomic components, find the component
635 type and validate it.
637 ??? Note that we ignore Has_Volatile_Components on objects; it's
638 not at all clear what to do in that case. */
640 if (Has_Atomic_Components (gnat_entity))
642 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
643 ? TREE_TYPE (gnu_type) : gnu_type);
645 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
646 && TYPE_MULTI_ARRAY_P (gnu_inner))
647 gnu_inner = TREE_TYPE (gnu_inner);
649 check_ok_for_atomic (gnu_inner, gnat_entity, true);
652 /* Now check if the type of the object allows atomic access. Note
653 that we must test the type, even if this object has size and
654 alignment to allow such access, because we will be going
655 inside the padded record to assign to the object. We could fix
656 this by always copying via an intermediate value, but it's not
657 clear it's worth the effort. */
658 if (Is_Atomic (gnat_entity))
659 check_ok_for_atomic (gnu_type, gnat_entity, false);
661 /* If this is an aliased object with an unconstrained nominal subtype,
662 make a type that includes the template. */
663 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
664 && Is_Array_Type (Etype (gnat_entity))
665 && !type_annotate_only)
667 tree gnu_fat
668 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
670 gnu_type
671 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
672 concat_id_with_name (gnu_entity_id,
673 "UNC"));
676 #ifdef MINIMUM_ATOMIC_ALIGNMENT
677 /* If the size is a constant and no alignment is specified, force
678 the alignment to be the minimum valid atomic alignment. The
679 restriction on constant size avoids problems with variable-size
680 temporaries; if the size is variable, there's no issue with
681 atomic access. Also don't do this for a constant, since it isn't
682 necessary and can interfere with constant replacement. Finally,
683 do not do it for Out parameters since that creates an
684 size inconsistency with In parameters. */
685 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
686 && !FLOAT_TYPE_P (gnu_type)
687 && !const_flag && No (Renamed_Object (gnat_entity))
688 && !imported_p && No (Address_Clause (gnat_entity))
689 && kind != E_Out_Parameter
690 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
691 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
692 align = MINIMUM_ATOMIC_ALIGNMENT;
693 #endif
695 /* Make a new type with the desired size and alignment, if needed. */
696 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
697 "PAD", false, definition, true);
699 /* Make a volatile version of this object's type if we are to
700 make the object volatile. Note that 13.3(19) says that we
701 should treat other types of objects as volatile as well. */
702 if ((Treat_As_Volatile (gnat_entity)
703 || Is_Exported (gnat_entity)
704 || Is_Imported (gnat_entity)
705 || Present (Address_Clause (gnat_entity)))
706 && !TYPE_VOLATILE (gnu_type))
707 gnu_type = build_qualified_type (gnu_type,
708 (TYPE_QUALS (gnu_type)
709 | TYPE_QUAL_VOLATILE));
711 /* Convert the expression to the type of the object except in the
712 case where the object's type is unconstrained or the object's type
713 is a padded record whose field is of self-referential size. In
714 the former case, converting will generate unnecessary evaluations
715 of the CONSTRUCTOR to compute the size and in the latter case, we
716 want to only copy the actual data. */
717 if (gnu_expr
718 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
719 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
720 && !(TREE_CODE (gnu_type) == RECORD_TYPE
721 && TYPE_IS_PADDING_P (gnu_type)
722 && (CONTAINS_PLACEHOLDER_P
723 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
724 gnu_expr = convert (gnu_type, gnu_expr);
726 /* See if this is a renaming, and handle appropriately depending on
727 what is renamed and in which context. There are three major
728 cases:
730 1/ This is a constant renaming and we can just make an object
731 with what is renamed as its initial value,
733 2/ We can reuse a stabilized version of what is renamed in place
734 of the renaming,
736 3/ If neither 1 or 2 applies, we make the renaming entity a constant
737 pointer to what is being renamed. */
739 if (Present (Renamed_Object (gnat_entity)))
741 /* If the renamed object had padding, strip off the reference
742 to the inner object and reset our type. */
743 if (TREE_CODE (gnu_expr) == COMPONENT_REF
744 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
745 == RECORD_TYPE)
746 && (TYPE_IS_PADDING_P
747 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
749 gnu_expr = TREE_OPERAND (gnu_expr, 0);
750 gnu_type = TREE_TYPE (gnu_expr);
753 /* Case 1: If this is a constant renaming, treat it as a normal
754 object whose initial value is what is being renamed. We cannot
755 do this if the type is unconstrained or class-wide. */
756 if (const_flag
757 && !TREE_SIDE_EFFECTS (gnu_expr)
758 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
759 && TYPE_MODE (gnu_type) != BLKmode
760 && Ekind (Etype (gnat_entity)) != E_Class_Wide_Type
761 && !Is_Array_Type (Etype (gnat_entity)))
764 /* Otherwise, see if we can proceed with a stabilized version of
765 the renamed entity or if we need to make a pointer. */
766 else
768 bool stabilized = false;
769 tree maybe_stable_expr = NULL_TREE;
771 /* Case 2: If the renaming entity need not be materialized and
772 the renamed expression is something we can stabilize, use
773 that for the renaming. At the global level, we can only do
774 this if we know no SAVE_EXPRs need be made, because the
775 expression we return might be used in arbitrary conditional
776 branches so we must force the SAVE_EXPRs evaluation
777 immediately and this requires a function context. */
778 if (!Materialize_Entity (gnat_entity)
779 && (!global_bindings_p ()
780 || (staticp (gnu_expr)
781 && !TREE_SIDE_EFFECTS (gnu_expr))))
783 maybe_stable_expr
784 = maybe_stabilize_reference (gnu_expr, true, false,
785 &stabilized);
787 if (stabilized)
789 gnu_decl = maybe_stable_expr;
790 save_gnu_tree (gnat_entity, gnu_decl, true);
791 saved = true;
792 break;
795 /* The stabilization failed. Keep maybe_stable_expr
796 untouched here to let the pointer case below know
797 about that failure. */
800 /* Case 3: Make this into a constant pointer to the object we
801 are to rename and attach the object to the pointer if it is
802 an lvalue that can be stabilized.
804 From the proper scope, attached objects will be referenced
805 directly instead of indirectly via the pointer to avoid
806 subtle aliasing problems with non addressable entities.
807 They have to be stable because we must not evaluate the
808 variables in the expression every time the renaming is used.
809 They also have to be lvalues because the context in which
810 they are reused sometimes requires so. We call pointers
811 with an attached object "renaming" pointers.
813 In the rare cases where we cannot stabilize the renamed
814 object, we just make a "bare" pointer, and the renamed
815 entity is always accessed indirectly through it. */
817 bool expr_has_side_effects = TREE_SIDE_EFFECTS (gnu_expr);
819 inner_const_flag = TREE_READONLY (gnu_expr);
820 const_flag = true;
821 gnu_type = build_reference_type (gnu_type);
823 /* If a previous attempt at unrestricted stabilization
824 failed, there is no point trying again and we can reuse
825 the result without attaching it to the pointer. */
826 if (maybe_stable_expr)
829 /* Otherwise, try to stabilize now, restricting to
830 lvalues only, and attach the expression to the pointer
831 if the stabilization succeeds.
833 Note that this might introduce SAVE_EXPRs and we don't
834 check whether we're at the global level or not. This is
835 fine since we are building a pointer initializer and
836 neither the pointer nor the initializing expression can
837 be accessed before the pointer elaboration has taken
838 place in a correct program.
840 SAVE_EXPRs will be evaluated at the right spots by either
841 create_var_decl->expand_decl_init for the non-global case
842 or build_unit_elab for the global case, and will be
843 attached to the elaboration procedure by the RTL expander
844 in the latter case. We have no need to force an early
845 evaluation here. */
846 else
848 maybe_stable_expr
849 = maybe_stabilize_reference (gnu_expr, true, true,
850 &stabilized);
852 if (stabilized)
853 renamed_obj = maybe_stable_expr;
854 /* Attaching is actually performed downstream, as soon
855 as we have a DECL for the pointer we make. */
858 gnu_expr
859 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
861 /* If the initial expression has side effects, we might
862 still have an unstabilized version at this point (for
863 instance if it involves a function call). Wrap the
864 result into a SAVE_EXPR now, in case it happens to be
865 referenced several times. */
866 if (expr_has_side_effects && ! stabilized)
867 gnu_expr = save_expr (gnu_expr);
869 gnu_size = NULL_TREE;
870 used_by_ref = true;
875 /* If this is an aliased object whose nominal subtype is unconstrained,
876 the object is a record that contains both the template and
877 the object. If there is an initializer, it will have already
878 been converted to the right type, but we need to create the
879 template if there is no initializer. */
880 else if (definition && TREE_CODE (gnu_type) == RECORD_TYPE
881 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
882 /* Beware that padding might have been introduced
883 via maybe_pad_type above. */
884 || (TYPE_IS_PADDING_P (gnu_type)
885 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
886 == RECORD_TYPE
887 && TYPE_CONTAINS_TEMPLATE_P
888 (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
889 && !gnu_expr)
891 tree template_field
892 = TYPE_IS_PADDING_P (gnu_type)
893 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
894 : TYPE_FIELDS (gnu_type);
896 gnu_expr
897 = gnat_build_constructor
898 (gnu_type,
899 tree_cons
900 (template_field,
901 build_template (TREE_TYPE (template_field),
902 TREE_TYPE (TREE_CHAIN (template_field)),
903 NULL_TREE),
904 NULL_TREE));
907 /* If this is a pointer and it does not have an initializing
908 expression, initialize it to NULL, unless the object is
909 imported. */
910 if (definition
911 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
912 && !Is_Imported (gnat_entity) && !gnu_expr)
913 gnu_expr = integer_zero_node;
915 /* If we are defining the object and it has an Address clause we must
916 get the address expression from the saved GCC tree for the
917 object if the object has a Freeze_Node. Otherwise, we elaborate
918 the address expression here since the front-end has guaranteed
919 in that case that the elaboration has no effects. Note that
920 only the latter mechanism is currently in use. */
921 if (definition && Present (Address_Clause (gnat_entity)))
923 tree gnu_address
924 = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
925 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
927 save_gnu_tree (gnat_entity, NULL_TREE, false);
929 /* Ignore the size. It's either meaningless or was handled
930 above. */
931 gnu_size = NULL_TREE;
932 gnu_type = build_reference_type (gnu_type);
933 gnu_address = convert (gnu_type, gnu_address);
934 used_by_ref = true;
935 const_flag = !Is_Public (gnat_entity);
937 /* If we don't have an initializing expression for the underlying
938 variable, the initializing expression for the pointer is the
939 specified address. Otherwise, we have to make a COMPOUND_EXPR
940 to assign both the address and the initial value. */
941 if (!gnu_expr)
942 gnu_expr = gnu_address;
943 else
944 gnu_expr
945 = build2 (COMPOUND_EXPR, gnu_type,
946 build_binary_op
947 (MODIFY_EXPR, NULL_TREE,
948 build_unary_op (INDIRECT_REF, NULL_TREE,
949 gnu_address),
950 gnu_expr),
951 gnu_address);
954 /* If it has an address clause and we are not defining it, mark it
955 as an indirect object. Likewise for Stdcall objects that are
956 imported. */
957 if ((!definition && Present (Address_Clause (gnat_entity)))
958 || (Is_Imported (gnat_entity)
959 && Has_Stdcall_Convention (gnat_entity)))
961 gnu_type = build_reference_type (gnu_type);
962 gnu_size = NULL_TREE;
964 gnu_expr = NULL_TREE;
965 /* No point in taking the address of an initializing expression
966 that isn't going to be used. */
968 used_by_ref = true;
971 /* If we are at top level and this object is of variable size,
972 make the actual type a hidden pointer to the real type and
973 make the initializer be a memory allocation and initialization.
974 Likewise for objects we aren't defining (presumed to be
975 external references from other packages), but there we do
976 not set up an initialization.
978 If the object's size overflows, make an allocator too, so that
979 Storage_Error gets raised. Note that we will never free
980 such memory, so we presume it never will get allocated. */
982 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
983 global_bindings_p () || !definition
984 || static_p)
985 || (gnu_size
986 && ! allocatable_size_p (gnu_size,
987 global_bindings_p () || !definition
988 || static_p)))
990 gnu_type = build_reference_type (gnu_type);
991 gnu_size = NULL_TREE;
992 used_by_ref = true;
993 const_flag = true;
995 /* In case this was a aliased object whose nominal subtype is
996 unconstrained, the pointer above will be a thin pointer and
997 build_allocator will automatically make the template.
999 If we have a template initializer only (that we made above),
1000 pretend there is none and rely on what build_allocator creates
1001 again anyway. Otherwise (if we have a full initializer), get
1002 the data part and feed that to build_allocator.
1004 If we are elaborating a mutable object, tell build_allocator to
1005 ignore a possibly simpler size from the initializer, if any, as
1006 we must allocate the maximum possible size in this case. */
1008 if (definition)
1010 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1012 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1013 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1015 gnu_alloc_type
1016 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1018 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1019 && 1 == VEC_length (constructor_elt,
1020 CONSTRUCTOR_ELTS (gnu_expr)))
1021 gnu_expr = 0;
1022 else
1023 gnu_expr
1024 = build_component_ref
1025 (gnu_expr, NULL_TREE,
1026 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1027 false);
1030 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1031 && TREE_CONSTANT_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1032 && !Is_Imported (gnat_entity))
1033 post_error ("Storage_Error will be raised at run-time?",
1034 gnat_entity);
1036 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1037 0, 0, gnat_entity, mutable_p);
1039 else
1041 gnu_expr = NULL_TREE;
1042 const_flag = false;
1046 /* If this object would go into the stack and has an alignment
1047 larger than the default largest alignment, make a variable
1048 to hold the "aligning type" with a modified initial value,
1049 if any, then point to it and make that the value of this
1050 variable, which is now indirect. */
1051 if (!global_bindings_p () && !static_p && definition
1052 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1054 tree gnu_new_type
1055 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1056 TYPE_SIZE_UNIT (gnu_type));
1057 tree gnu_new_var;
1059 gnu_new_var
1060 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1061 NULL_TREE, gnu_new_type, gnu_expr, false,
1062 false, false, false, NULL, gnat_entity);
1064 if (gnu_expr)
1065 add_stmt_with_node
1066 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1067 build_component_ref
1068 (gnu_new_var, NULL_TREE,
1069 TYPE_FIELDS (gnu_new_type), false),
1070 gnu_expr),
1071 gnat_entity);
1073 gnu_type = build_reference_type (gnu_type);
1074 gnu_expr
1075 = build_unary_op
1076 (ADDR_EXPR, gnu_type,
1077 build_component_ref (gnu_new_var, NULL_TREE,
1078 TYPE_FIELDS (gnu_new_type), false));
1080 gnu_size = NULL_TREE;
1081 used_by_ref = true;
1082 const_flag = true;
1085 if (const_flag)
1086 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1087 | TYPE_QUAL_CONST));
1089 /* Convert the expression to the type of the object except in the
1090 case where the object's type is unconstrained or the object's type
1091 is a padded record whose field is of self-referential size. In
1092 the former case, converting will generate unnecessary evaluations
1093 of the CONSTRUCTOR to compute the size and in the latter case, we
1094 want to only copy the actual data. */
1095 if (gnu_expr
1096 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1097 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1098 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1099 && TYPE_IS_PADDING_P (gnu_type)
1100 && (CONTAINS_PLACEHOLDER_P
1101 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1102 gnu_expr = convert (gnu_type, gnu_expr);
1104 /* If this name is external or there was a name specified, use it,
1105 unless this is a VMS exception object since this would conflict
1106 with the symbol we need to export in addition. Don't use the
1107 Interface_Name if there is an address clause (see CD30005). */
1108 if (!Is_VMS_Exception (gnat_entity)
1109 && ((Present (Interface_Name (gnat_entity))
1110 && No (Address_Clause (gnat_entity)))
1111 || (Is_Public (gnat_entity)
1112 && (!Is_Imported (gnat_entity)
1113 || Is_Exported (gnat_entity)))))
1114 gnu_ext_name = create_concat_name (gnat_entity, 0);
1116 /* If this is constant initialized to a static constant and the
1117 object has an aggregate type, force it to be statically
1118 allocated. */
1119 if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1120 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1121 && (AGGREGATE_TYPE_P (gnu_type)
1122 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1123 && TYPE_IS_PADDING_P (gnu_type))))
1124 static_p = true;
1126 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1127 gnu_expr, const_flag,
1128 Is_Public (gnat_entity),
1129 imported_p || !definition,
1130 static_p, attr_list, gnat_entity);
1131 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1132 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1133 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1135 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1136 DECL_RENAMING_GLOBAL_P (gnu_decl) = global_bindings_p ();
1139 /* If we have an address clause and we've made this indirect, it's
1140 not enough to merely mark the type as volatile since volatile
1141 references only conflict with other volatile references while this
1142 reference must conflict with all other references. So ensure that
1143 the dereferenced value has alias set 0. */
1144 if (Present (Address_Clause (gnat_entity)) && used_by_ref)
1145 DECL_POINTER_ALIAS_SET (gnu_decl) = 0;
1147 if (definition && DECL_SIZE (gnu_decl)
1148 && get_block_jmpbuf_decl ()
1149 && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1150 || (flag_stack_check && !STACK_CHECK_BUILTIN
1151 && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1152 STACK_CHECK_MAX_VAR_SIZE))))
1153 add_stmt_with_node (build_call_1_expr
1154 (update_setjmp_buf_decl,
1155 build_unary_op (ADDR_EXPR, NULL_TREE,
1156 get_block_jmpbuf_decl ())),
1157 gnat_entity);
1159 /* If this is a public constant or we're not optimizing and we're not
1160 making a VAR_DECL for it, make one just for export or debugger
1161 use. Likewise if the address is taken or if the object or type is
1162 aliased. */
1163 if (definition && TREE_CODE (gnu_decl) == CONST_DECL
1164 && (Is_Public (gnat_entity)
1165 || optimize == 0
1166 || Address_Taken (gnat_entity)
1167 || Is_Aliased (gnat_entity)
1168 || Is_Aliased (Etype (gnat_entity))))
1170 tree gnu_corr_var
1171 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1172 gnu_expr, false, Is_Public (gnat_entity),
1173 false, static_p, NULL, gnat_entity);
1175 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1178 /* If this is declared in a block that contains a block with an
1179 exception handler, we must force this variable in memory to
1180 suppress an invalid optimization. */
1181 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1182 && Exception_Mechanism != Back_End_Exceptions)
1183 TREE_ADDRESSABLE (gnu_decl) = 1;
1185 /* Back-annotate the Alignment of the object if not already in the
1186 tree. Likewise for Esize if the object is of a constant size.
1187 But if the "object" is actually a pointer to an object, the
1188 alignment and size are the same as the type, so don't back-annotate
1189 the values for the pointer. */
1190 if (!used_by_ref && Unknown_Alignment (gnat_entity))
1191 Set_Alignment (gnat_entity,
1192 UI_From_Int (DECL_ALIGN (gnu_decl) / BITS_PER_UNIT));
1194 if (!used_by_ref && Unknown_Esize (gnat_entity)
1195 && DECL_SIZE (gnu_decl))
1197 tree gnu_back_size = DECL_SIZE (gnu_decl);
1199 if (TREE_CODE (TREE_TYPE (gnu_decl)) == RECORD_TYPE
1200 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_decl)))
1201 gnu_back_size
1202 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN
1203 (TYPE_FIELDS (TREE_TYPE (gnu_decl)))));
1205 Set_Esize (gnat_entity, annotate_value (gnu_back_size));
1208 break;
1210 case E_Void:
1211 /* Return a TYPE_DECL for "void" that we previously made. */
1212 gnu_decl = void_type_decl_node;
1213 break;
1215 case E_Enumeration_Type:
1216 /* A special case, for the types Character and Wide_Character in
1217 Standard, we do not list all the literals. So if the literals
1218 are not specified, make this an unsigned type. */
1219 if (No (First_Literal (gnat_entity)))
1221 gnu_type = make_unsigned_type (esize);
1222 break;
1225 /* Normal case of non-character type, or non-Standard character type */
1227 /* Here we have a list of enumeral constants in First_Literal.
1228 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1229 the list to be places into TYPE_FIELDS. Each node in the list
1230 is a TREE_LIST node whose TREE_VALUE is the literal name
1231 and whose TREE_PURPOSE is the value of the literal.
1233 Esize contains the number of bits needed to represent the enumeral
1234 type, Type_Low_Bound also points to the first literal and
1235 Type_High_Bound points to the last literal. */
1237 Entity_Id gnat_literal;
1238 tree gnu_literal_list = NULL_TREE;
1240 if (Is_Unsigned_Type (gnat_entity))
1241 gnu_type = make_unsigned_type (esize);
1242 else
1243 gnu_type = make_signed_type (esize);
1245 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1247 for (gnat_literal = First_Literal (gnat_entity);
1248 Present (gnat_literal);
1249 gnat_literal = Next_Literal (gnat_literal))
1251 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1252 gnu_type);
1253 tree gnu_literal
1254 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1255 gnu_type, gnu_value, true, false, false,
1256 false, NULL, gnat_literal);
1258 save_gnu_tree (gnat_literal, gnu_literal, false);
1259 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1260 gnu_value, gnu_literal_list);
1263 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1265 /* Note that the bounds are updated at the end of this function
1266 because to avoid an infinite recursion when we get the bounds of
1267 this type, since those bounds are objects of this type. */
1269 break;
1271 case E_Signed_Integer_Type:
1272 case E_Ordinary_Fixed_Point_Type:
1273 case E_Decimal_Fixed_Point_Type:
1274 /* For integer types, just make a signed type the appropriate number
1275 of bits. */
1276 gnu_type = make_signed_type (esize);
1277 break;
1279 case E_Modular_Integer_Type:
1280 /* For modular types, make the unsigned type of the proper number of
1281 bits and then set up the modulus, if required. */
1283 enum machine_mode mode;
1284 tree gnu_modulus;
1285 tree gnu_high = 0;
1287 if (Is_Packed_Array_Type (gnat_entity))
1288 esize = UI_To_Int (RM_Size (gnat_entity));
1290 /* Find the smallest mode at least ESIZE bits wide and make a class
1291 using that mode. */
1293 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1294 GET_MODE_BITSIZE (mode) < esize;
1295 mode = GET_MODE_WIDER_MODE (mode))
1298 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1299 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1300 = Is_Packed_Array_Type (gnat_entity);
1302 /* Get the modulus in this type. If it overflows, assume it is because
1303 it is equal to 2**Esize. Note that there is no overflow checking
1304 done on unsigned type, so we detect the overflow by looking for
1305 a modulus of zero, which is otherwise invalid. */
1306 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1308 if (!integer_zerop (gnu_modulus))
1310 TYPE_MODULAR_P (gnu_type) = 1;
1311 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1312 gnu_high = fold (build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1313 convert (gnu_type, integer_one_node)));
1316 /* If we have to set TYPE_PRECISION different from its natural value,
1317 make a subtype to do do. Likewise if there is a modulus and
1318 it is not one greater than TYPE_MAX_VALUE. */
1319 if (TYPE_PRECISION (gnu_type) != esize
1320 || (TYPE_MODULAR_P (gnu_type)
1321 && !tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1323 tree gnu_subtype = make_node (INTEGER_TYPE);
1325 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1326 TREE_TYPE (gnu_subtype) = gnu_type;
1327 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1328 TYPE_MAX_VALUE (gnu_subtype)
1329 = TYPE_MODULAR_P (gnu_type)
1330 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1331 TYPE_PRECISION (gnu_subtype) = esize;
1332 TYPE_UNSIGNED (gnu_subtype) = 1;
1333 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1334 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1335 = Is_Packed_Array_Type (gnat_entity);
1336 layout_type (gnu_subtype);
1338 gnu_type = gnu_subtype;
1341 break;
1343 case E_Signed_Integer_Subtype:
1344 case E_Enumeration_Subtype:
1345 case E_Modular_Integer_Subtype:
1346 case E_Ordinary_Fixed_Point_Subtype:
1347 case E_Decimal_Fixed_Point_Subtype:
1349 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1350 that we do not want to call build_range_type since we would
1351 like each subtype node to be distinct. This will be important
1352 when memory aliasing is implemented.
1354 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1355 parent type; this fact is used by the arithmetic conversion
1356 functions.
1358 We elaborate the Ancestor_Subtype if it is not in the current
1359 unit and one of our bounds is non-static. We do this to ensure
1360 consistent naming in the case where several subtypes share the same
1361 bounds by always elaborating the first such subtype first, thus
1362 using its name. */
1364 if (definition == 0
1365 && Present (Ancestor_Subtype (gnat_entity))
1366 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1367 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1368 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1369 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1370 gnu_expr, definition);
1372 gnu_type = make_node (INTEGER_TYPE);
1373 if (Is_Packed_Array_Type (gnat_entity))
1375 esize = UI_To_Int (RM_Size (gnat_entity));
1376 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1379 TYPE_PRECISION (gnu_type) = esize;
1380 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1382 TYPE_MIN_VALUE (gnu_type)
1383 = convert (TREE_TYPE (gnu_type),
1384 elaborate_expression (Type_Low_Bound (gnat_entity),
1385 gnat_entity,
1386 get_identifier ("L"), definition, 1,
1387 Needs_Debug_Info (gnat_entity)));
1389 TYPE_MAX_VALUE (gnu_type)
1390 = convert (TREE_TYPE (gnu_type),
1391 elaborate_expression (Type_High_Bound (gnat_entity),
1392 gnat_entity,
1393 get_identifier ("U"), definition, 1,
1394 Needs_Debug_Info (gnat_entity)));
1396 /* One of the above calls might have caused us to be elaborated,
1397 so don't blow up if so. */
1398 if (present_gnu_tree (gnat_entity))
1400 maybe_present = true;
1401 break;
1404 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1405 = Has_Biased_Representation (gnat_entity);
1407 /* This should be an unsigned type if the lower bound is constant
1408 and non-negative or if the base type is unsigned; a signed type
1409 otherwise. */
1410 TYPE_UNSIGNED (gnu_type)
1411 = (TYPE_UNSIGNED (TREE_TYPE (gnu_type))
1412 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1413 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1414 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1415 || Is_Unsigned_Type (gnat_entity));
1417 layout_type (gnu_type);
1419 /* If the type we are dealing with is to represent a packed array,
1420 we need to have the bits left justified on big-endian targets
1421 and right justified on little-endian targets. We also need to
1422 ensure that when the value is read (e.g. for comparison of two
1423 such values), we only get the good bits, since the unused bits
1424 are uninitialized. Both goals are accomplished by wrapping the
1425 modular value in an enclosing struct. */
1426 if (Is_Packed_Array_Type (gnat_entity))
1428 tree gnu_field_type = gnu_type;
1429 tree gnu_field;
1431 TYPE_RM_SIZE_NUM (gnu_field_type)
1432 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1433 gnu_type = make_node (RECORD_TYPE);
1434 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1435 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1436 TYPE_PACKED (gnu_type) = 1;
1438 /* Create a stripped-down declaration of the original type, mainly
1439 for debugging. */
1440 create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1441 NULL, true, debug_info_p, gnat_entity);
1443 /* Don't notify the field as "addressable", since we won't be taking
1444 it's address and it would prevent create_field_decl from making a
1445 bitfield. */
1446 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1447 gnu_field_type, gnu_type, 1, 0, 0, 0);
1449 finish_record_type (gnu_type, gnu_field, false, false);
1450 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1451 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1454 break;
1456 case E_Floating_Point_Type:
1457 /* If this is a VAX floating-point type, use an integer of the proper
1458 size. All the operations will be handled with ASM statements. */
1459 if (Vax_Float (gnat_entity))
1461 gnu_type = make_signed_type (esize);
1462 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1463 SET_TYPE_DIGITS_VALUE (gnu_type,
1464 UI_To_gnu (Digits_Value (gnat_entity),
1465 sizetype));
1466 break;
1469 /* The type of the Low and High bounds can be our type if this is
1470 a type from Standard, so set them at the end of the function. */
1471 gnu_type = make_node (REAL_TYPE);
1472 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1473 layout_type (gnu_type);
1474 break;
1476 case E_Floating_Point_Subtype:
1477 if (Vax_Float (gnat_entity))
1479 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1480 break;
1484 if (definition == 0
1485 && Present (Ancestor_Subtype (gnat_entity))
1486 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1487 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1488 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1489 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1490 gnu_expr, definition);
1492 gnu_type = make_node (REAL_TYPE);
1493 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1494 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1496 TYPE_MIN_VALUE (gnu_type)
1497 = convert (TREE_TYPE (gnu_type),
1498 elaborate_expression (Type_Low_Bound (gnat_entity),
1499 gnat_entity, get_identifier ("L"),
1500 definition, 1,
1501 Needs_Debug_Info (gnat_entity)));
1503 TYPE_MAX_VALUE (gnu_type)
1504 = convert (TREE_TYPE (gnu_type),
1505 elaborate_expression (Type_High_Bound (gnat_entity),
1506 gnat_entity, get_identifier ("U"),
1507 definition, 1,
1508 Needs_Debug_Info (gnat_entity)));
1510 /* One of the above calls might have caused us to be elaborated,
1511 so don't blow up if so. */
1512 if (present_gnu_tree (gnat_entity))
1514 maybe_present = true;
1515 break;
1518 layout_type (gnu_type);
1520 break;
1522 /* Array and String Types and Subtypes
1524 Unconstrained array types are represented by E_Array_Type and
1525 constrained array types are represented by E_Array_Subtype. There
1526 are no actual objects of an unconstrained array type; all we have
1527 are pointers to that type.
1529 The following fields are defined on array types and subtypes:
1531 Component_Type Component type of the array.
1532 Number_Dimensions Number of dimensions (an int).
1533 First_Index Type of first index. */
1535 case E_String_Type:
1536 case E_Array_Type:
1538 tree gnu_template_fields = NULL_TREE;
1539 tree gnu_template_type = make_node (RECORD_TYPE);
1540 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1541 tree gnu_fat_type = make_node (RECORD_TYPE);
1542 int ndim = Number_Dimensions (gnat_entity);
1543 int firstdim
1544 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1545 int nextdim
1546 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1547 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1548 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1549 tree gnu_comp_size = 0;
1550 tree gnu_max_size = size_one_node;
1551 tree gnu_max_size_unit;
1552 int index;
1553 Entity_Id gnat_ind_subtype;
1554 Entity_Id gnat_ind_base_subtype;
1555 tree gnu_template_reference;
1556 tree tem;
1558 TYPE_NAME (gnu_template_type)
1559 = create_concat_name (gnat_entity, "XUB");
1560 TYPE_NAME (gnu_fat_type) = create_concat_name (gnat_entity, "XUP");
1561 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1562 TYPE_READONLY (gnu_template_type) = 1;
1564 /* Make a node for the array. If we are not defining the array
1565 suppress expanding incomplete types. */
1566 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1568 if (!definition)
1569 defer_incomplete_level++, this_deferred = true;
1571 /* Build the fat pointer type. Use a "void *" object instead of
1572 a pointer to the array type since we don't have the array type
1573 yet (it will reference the fat pointer via the bounds). */
1574 tem = chainon (chainon (NULL_TREE,
1575 create_field_decl (get_identifier ("P_ARRAY"),
1576 ptr_void_type_node,
1577 gnu_fat_type, 0, 0, 0, 0)),
1578 create_field_decl (get_identifier ("P_BOUNDS"),
1579 gnu_ptr_template,
1580 gnu_fat_type, 0, 0, 0, 0));
1582 /* Make sure we can put this into a register. */
1583 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1584 finish_record_type (gnu_fat_type, tem, false, true);
1586 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1587 is the fat pointer. This will be used to access the individual
1588 fields once we build them. */
1589 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1590 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1591 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1592 gnu_template_reference
1593 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1594 TREE_READONLY (gnu_template_reference) = 1;
1596 /* Now create the GCC type for each index and add the fields for
1597 that index to the template. */
1598 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1599 gnat_ind_base_subtype
1600 = First_Index (Implementation_Base_Type (gnat_entity));
1601 index < ndim && index >= 0;
1602 index += nextdim,
1603 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1604 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1606 char field_name[10];
1607 tree gnu_ind_subtype
1608 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1609 tree gnu_base_subtype
1610 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1611 tree gnu_base_min
1612 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1613 tree gnu_base_max
1614 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1615 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1617 /* Make the FIELD_DECLs for the minimum and maximum of this
1618 type and then make extractions of that field from the
1619 template. */
1620 sprintf (field_name, "LB%d", index);
1621 gnu_min_field = create_field_decl (get_identifier (field_name),
1622 gnu_ind_subtype,
1623 gnu_template_type, 0, 0, 0, 0);
1624 field_name[0] = 'U';
1625 gnu_max_field = create_field_decl (get_identifier (field_name),
1626 gnu_ind_subtype,
1627 gnu_template_type, 0, 0, 0, 0);
1629 Sloc_to_locus (Sloc (gnat_entity),
1630 &DECL_SOURCE_LOCATION (gnu_min_field));
1631 Sloc_to_locus (Sloc (gnat_entity),
1632 &DECL_SOURCE_LOCATION (gnu_max_field));
1633 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1635 /* We can't use build_component_ref here since the template
1636 type isn't complete yet. */
1637 gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1638 gnu_template_reference, gnu_min_field,
1639 NULL_TREE);
1640 gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1641 gnu_template_reference, gnu_max_field,
1642 NULL_TREE);
1643 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1645 /* Make a range type with the new ranges, but using
1646 the Ada subtype. Then we convert to sizetype. */
1647 gnu_index_types[index]
1648 = create_index_type (convert (sizetype, gnu_min),
1649 convert (sizetype, gnu_max),
1650 build_range_type (gnu_ind_subtype,
1651 gnu_min, gnu_max));
1652 /* Update the maximum size of the array, in elements. */
1653 gnu_max_size
1654 = size_binop (MULT_EXPR, gnu_max_size,
1655 size_binop (PLUS_EXPR, size_one_node,
1656 size_binop (MINUS_EXPR, gnu_base_max,
1657 gnu_base_min)));
1659 TYPE_NAME (gnu_index_types[index])
1660 = create_concat_name (gnat_entity, field_name);
1663 for (index = 0; index < ndim; index++)
1664 gnu_template_fields
1665 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1667 /* Install all the fields into the template. */
1668 finish_record_type (gnu_template_type, gnu_template_fields,
1669 false, false);
1670 TYPE_READONLY (gnu_template_type) = 1;
1672 /* Now make the array of arrays and update the pointer to the array
1673 in the fat pointer. Note that it is the first field. */
1675 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1677 /* Get and validate any specified Component_Size, but if Packed,
1678 ignore it since the front end will have taken care of it. */
1679 gnu_comp_size
1680 = validate_size (Component_Size (gnat_entity), tem,
1681 gnat_entity,
1682 (Is_Bit_Packed_Array (gnat_entity)
1683 ? TYPE_DECL : VAR_DECL),
1684 true, Has_Component_Size_Clause (gnat_entity));
1686 if (Has_Atomic_Components (gnat_entity))
1687 check_ok_for_atomic (tem, gnat_entity, true);
1689 /* If the component type is a RECORD_TYPE that has a self-referential
1690 size, use the maxium size. */
1691 if (!gnu_comp_size && TREE_CODE (tem) == RECORD_TYPE
1692 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1693 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1695 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
1697 tem = make_type_from_size (tem, gnu_comp_size, false);
1698 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1699 "C_PAD", false, definition, true);
1702 if (Has_Volatile_Components (gnat_entity))
1703 tem = build_qualified_type (tem,
1704 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1706 /* If Component_Size is not already specified, annotate it with the
1707 size of the component. */
1708 if (Unknown_Component_Size (gnat_entity))
1709 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1711 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1712 size_binop (MULT_EXPR, gnu_max_size,
1713 TYPE_SIZE_UNIT (tem)));
1714 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1715 size_binop (MULT_EXPR,
1716 convert (bitsizetype,
1717 gnu_max_size),
1718 TYPE_SIZE (tem)));
1720 for (index = ndim - 1; index >= 0; index--)
1722 tem = build_array_type (tem, gnu_index_types[index]);
1723 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1725 /* If the type below this an multi-array type, then this
1726 does not not have aliased components.
1728 ??? Otherwise, for now, we say that any component of aggregate
1729 type is addressable because the front end may take 'Reference
1730 of it. But we have to make it addressable if it must be passed
1731 by reference or it that is the default. */
1732 TYPE_NONALIASED_COMPONENT (tem)
1733 = ((TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
1734 && TYPE_MULTI_ARRAY_P (TREE_TYPE (tem))) ? 1
1735 : (!Has_Aliased_Components (gnat_entity)
1736 && !AGGREGATE_TYPE_P (TREE_TYPE (tem))));
1739 /* If an alignment is specified, use it if valid. But ignore it for
1740 types that represent the unpacked base type for packed arrays. */
1741 if (No (Packed_Array_Type (gnat_entity))
1742 && Known_Alignment (gnat_entity))
1744 gcc_assert (Present (Alignment (gnat_entity)));
1745 TYPE_ALIGN (tem)
1746 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1747 TYPE_ALIGN (tem));
1750 TYPE_CONVENTION_FORTRAN_P (tem)
1751 = (Convention (gnat_entity) == Convention_Fortran);
1752 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1754 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1755 corresponding fat pointer. */
1756 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1757 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1758 TYPE_MODE (gnu_type) = BLKmode;
1759 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1760 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
1762 /* If the maximum size doesn't overflow, use it. */
1763 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1764 && !TREE_OVERFLOW (gnu_max_size))
1765 TYPE_SIZE (tem)
1766 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1767 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
1768 && !TREE_OVERFLOW (gnu_max_size_unit))
1769 TYPE_SIZE_UNIT (tem)
1770 = size_binop (MIN_EXPR, gnu_max_size_unit,
1771 TYPE_SIZE_UNIT (tem));
1773 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1774 tem, NULL, !Comes_From_Source (gnat_entity),
1775 debug_info_p, gnat_entity);
1777 /* Create a record type for the object and its template and
1778 set the template at a negative offset. */
1779 tem = build_unc_object_type (gnu_template_type, tem,
1780 create_concat_name (gnat_entity, "XUT"));
1781 DECL_FIELD_OFFSET (TYPE_FIELDS (tem))
1782 = size_binop (MINUS_EXPR, size_zero_node,
1783 byte_position (TREE_CHAIN (TYPE_FIELDS (tem))));
1784 DECL_FIELD_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem))) = size_zero_node;
1785 DECL_FIELD_BIT_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem)))
1786 = bitsize_zero_node;
1787 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
1788 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1790 /* Give the thin pointer type a name. */
1791 create_type_decl (create_concat_name (gnat_entity, "XUX"),
1792 build_pointer_type (tem), NULL,
1793 !Comes_From_Source (gnat_entity), debug_info_p,
1794 gnat_entity);
1796 break;
1798 case E_String_Subtype:
1799 case E_Array_Subtype:
1801 /* This is the actual data type for array variables. Multidimensional
1802 arrays are implemented in the gnu tree as arrays of arrays. Note
1803 that for the moment arrays which have sparse enumeration subtypes as
1804 index components create sparse arrays, which is obviously space
1805 inefficient but so much easier to code for now.
1807 Also note that the subtype never refers to the unconstrained
1808 array type, which is somewhat at variance with Ada semantics.
1810 First check to see if this is simply a renaming of the array
1811 type. If so, the result is the array type. */
1813 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1814 if (!Is_Constrained (gnat_entity))
1815 break;
1816 else
1818 int index;
1819 int array_dim = Number_Dimensions (gnat_entity);
1820 int first_dim
1821 = ((Convention (gnat_entity) == Convention_Fortran)
1822 ? array_dim - 1 : 0);
1823 int next_dim
1824 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
1825 Entity_Id gnat_ind_subtype;
1826 Entity_Id gnat_ind_base_subtype;
1827 tree gnu_base_type = gnu_type;
1828 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
1829 tree gnu_comp_size = NULL_TREE;
1830 tree gnu_max_size = size_one_node;
1831 tree gnu_max_size_unit;
1832 bool need_index_type_struct = false;
1833 bool max_overflow = false;
1835 /* First create the gnu types for each index. Create types for
1836 debugging information to point to the index types if the
1837 are not integer types, have variable bounds, or are
1838 wider than sizetype. */
1840 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1841 gnat_ind_base_subtype
1842 = First_Index (Implementation_Base_Type (gnat_entity));
1843 index < array_dim && index >= 0;
1844 index += next_dim,
1845 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1846 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1848 tree gnu_index_subtype
1849 = get_unpadded_type (Etype (gnat_ind_subtype));
1850 tree gnu_min
1851 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
1852 tree gnu_max
1853 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
1854 tree gnu_base_subtype
1855 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1856 tree gnu_base_min
1857 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1858 tree gnu_base_max
1859 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1860 tree gnu_base_type = get_base_type (gnu_base_subtype);
1861 tree gnu_base_base_min
1862 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
1863 tree gnu_base_base_max
1864 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
1865 tree gnu_high;
1866 tree gnu_this_max;
1868 /* If the minimum and maximum values both overflow in
1869 SIZETYPE, but the difference in the original type
1870 does not overflow in SIZETYPE, ignore the overflow
1871 indications. */
1872 if ((TYPE_PRECISION (gnu_index_subtype)
1873 > TYPE_PRECISION (sizetype)
1874 || TYPE_UNSIGNED (gnu_index_subtype)
1875 != TYPE_UNSIGNED (sizetype))
1876 && TREE_CODE (gnu_min) == INTEGER_CST
1877 && TREE_CODE (gnu_max) == INTEGER_CST
1878 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
1879 && (!TREE_OVERFLOW
1880 (fold (build2 (MINUS_EXPR, gnu_index_subtype,
1881 TYPE_MAX_VALUE (gnu_index_subtype),
1882 TYPE_MIN_VALUE (gnu_index_subtype))))))
1883 TREE_OVERFLOW (gnu_min) = TREE_OVERFLOW (gnu_max)
1884 = TREE_CONSTANT_OVERFLOW (gnu_min)
1885 = TREE_CONSTANT_OVERFLOW (gnu_max) = 0;
1887 /* Similarly, if the range is null, use bounds of 1..0 for
1888 the sizetype bounds. */
1889 else if ((TYPE_PRECISION (gnu_index_subtype)
1890 > TYPE_PRECISION (sizetype)
1891 || TYPE_UNSIGNED (gnu_index_subtype)
1892 != TYPE_UNSIGNED (sizetype))
1893 && TREE_CODE (gnu_min) == INTEGER_CST
1894 && TREE_CODE (gnu_max) == INTEGER_CST
1895 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
1896 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
1897 TYPE_MIN_VALUE (gnu_index_subtype)))
1898 gnu_min = size_one_node, gnu_max = size_zero_node;
1900 /* Now compute the size of this bound. We need to provide
1901 GCC with an upper bound to use but have to deal with the
1902 "superflat" case. There are three ways to do this. If we
1903 can prove that the array can never be superflat, we can
1904 just use the high bound of the index subtype. If we can
1905 prove that the low bound minus one can't overflow, we
1906 can do this as MAX (hb, lb - 1). Otherwise, we have to use
1907 the expression hb >= lb ? hb : lb - 1. */
1908 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
1910 /* See if the base array type is already flat. If it is, we
1911 are probably compiling an ACVC test, but it will cause the
1912 code below to malfunction if we don't handle it specially. */
1913 if (TREE_CODE (gnu_base_min) == INTEGER_CST
1914 && TREE_CODE (gnu_base_max) == INTEGER_CST
1915 && !TREE_CONSTANT_OVERFLOW (gnu_base_min)
1916 && !TREE_CONSTANT_OVERFLOW (gnu_base_max)
1917 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
1918 gnu_high = size_zero_node, gnu_min = size_one_node;
1920 /* If gnu_high is now an integer which overflowed, the array
1921 cannot be superflat. */
1922 else if (TREE_CODE (gnu_high) == INTEGER_CST
1923 && TREE_OVERFLOW (gnu_high))
1924 gnu_high = gnu_max;
1925 else if (TYPE_UNSIGNED (gnu_base_subtype)
1926 || TREE_CODE (gnu_high) == INTEGER_CST)
1927 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
1928 else
1929 gnu_high
1930 = build_cond_expr
1931 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
1932 gnu_max, gnu_min),
1933 gnu_max, gnu_high);
1935 gnu_index_type[index]
1936 = create_index_type (gnu_min, gnu_high, gnu_index_subtype);
1938 /* Also compute the maximum size of the array. Here we
1939 see if any constraint on the index type of the base type
1940 can be used in the case of self-referential bound on
1941 the index type of the subtype. We look for a non-"infinite"
1942 and non-self-referential bound from any type involved and
1943 handle each bound separately. */
1945 if ((TREE_CODE (gnu_min) == INTEGER_CST
1946 && !TREE_OVERFLOW (gnu_min)
1947 && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
1948 || !CONTAINS_PLACEHOLDER_P (gnu_min))
1949 gnu_base_min = gnu_min;
1951 if ((TREE_CODE (gnu_max) == INTEGER_CST
1952 && !TREE_OVERFLOW (gnu_max)
1953 && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
1954 || !CONTAINS_PLACEHOLDER_P (gnu_max))
1955 gnu_base_max = gnu_max;
1957 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
1958 && TREE_CONSTANT_OVERFLOW (gnu_base_min))
1959 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
1960 || (TREE_CODE (gnu_base_max) == INTEGER_CST
1961 && TREE_CONSTANT_OVERFLOW (gnu_base_max))
1962 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
1963 max_overflow = true;
1965 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
1966 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
1968 gnu_this_max
1969 = size_binop (MAX_EXPR,
1970 size_binop (PLUS_EXPR, size_one_node,
1971 size_binop (MINUS_EXPR, gnu_base_max,
1972 gnu_base_min)),
1973 size_zero_node);
1975 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1976 && TREE_CONSTANT_OVERFLOW (gnu_this_max))
1977 max_overflow = true;
1979 gnu_max_size
1980 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1982 if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
1983 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
1984 != INTEGER_CST)
1985 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
1986 || (TREE_TYPE (gnu_index_subtype)
1987 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
1988 != INTEGER_TYPE))
1989 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
1990 || (TYPE_PRECISION (gnu_index_subtype)
1991 > TYPE_PRECISION (sizetype)))
1992 need_index_type_struct = true;
1995 /* Then flatten: create the array of arrays. */
1997 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
1999 /* One of the above calls might have caused us to be elaborated,
2000 so don't blow up if so. */
2001 if (present_gnu_tree (gnat_entity))
2003 maybe_present = true;
2004 break;
2007 /* Get and validate any specified Component_Size, but if Packed,
2008 ignore it since the front end will have taken care of it. */
2009 gnu_comp_size
2010 = validate_size (Component_Size (gnat_entity), gnu_type,
2011 gnat_entity,
2012 (Is_Bit_Packed_Array (gnat_entity)
2013 ? TYPE_DECL : VAR_DECL),
2014 true, Has_Component_Size_Clause (gnat_entity));
2016 /* If the component type is a RECORD_TYPE that has a self-referential
2017 size, use the maxium size. */
2018 if (!gnu_comp_size && TREE_CODE (gnu_type) == RECORD_TYPE
2019 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2020 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
2022 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
2024 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
2025 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
2026 gnat_entity, "C_PAD", false,
2027 definition, true);
2030 if (Has_Volatile_Components (Base_Type (gnat_entity)))
2031 gnu_type = build_qualified_type (gnu_type,
2032 (TYPE_QUALS (gnu_type)
2033 | TYPE_QUAL_VOLATILE));
2035 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2036 TYPE_SIZE_UNIT (gnu_type));
2037 gnu_max_size = size_binop (MULT_EXPR,
2038 convert (bitsizetype, gnu_max_size),
2039 TYPE_SIZE (gnu_type));
2041 for (index = array_dim - 1; index >= 0; index --)
2043 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
2044 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2045 /* If the type below this an multi-array type, then this
2046 does not not have aliased components.
2048 ??? Otherwise, for now, we say that any component of aggregate
2049 type is addressable because the front end may take 'Reference
2050 of it. But we have to make it addressable if it must be passed
2051 by reference or it that is the default. */
2052 TYPE_NONALIASED_COMPONENT (gnu_type)
2053 = ((TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
2054 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type))) ? 1
2055 : (!Has_Aliased_Components (gnat_entity)
2056 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_type))));
2059 /* If we are at file level and this is a multi-dimensional array, we
2060 need to make a variable corresponding to the stride of the
2061 inner dimensions. */
2062 if (global_bindings_p () && array_dim > 1)
2064 tree gnu_str_name = get_identifier ("ST");
2065 tree gnu_arr_type;
2067 for (gnu_arr_type = TREE_TYPE (gnu_type);
2068 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2069 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2070 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
2072 tree eltype = TREE_TYPE (gnu_arr_type);
2074 TYPE_SIZE (gnu_arr_type)
2075 = elaborate_expression_1 (gnat_entity, gnat_entity,
2076 TYPE_SIZE (gnu_arr_type),
2077 gnu_str_name, definition, 0);
2079 /* ??? For now, store the size as a multiple of the
2080 alignment of the element type in bytes so that we
2081 can see the alignment from the tree. */
2082 TYPE_SIZE_UNIT (gnu_arr_type)
2083 = build_binary_op
2084 (MULT_EXPR, sizetype,
2085 elaborate_expression_1
2086 (gnat_entity, gnat_entity,
2087 build_binary_op (EXACT_DIV_EXPR, sizetype,
2088 TYPE_SIZE_UNIT (gnu_arr_type),
2089 size_int (TYPE_ALIGN (eltype)
2090 / BITS_PER_UNIT)),
2091 concat_id_with_name (gnu_str_name, "A_U"),
2092 definition, 0),
2093 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2097 /* If we need to write out a record type giving the names of
2098 the bounds, do it now. */
2099 if (need_index_type_struct && debug_info_p)
2101 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
2102 tree gnu_field_list = NULL_TREE;
2103 tree gnu_field;
2105 TYPE_NAME (gnu_bound_rec_type)
2106 = create_concat_name (gnat_entity, "XA");
2108 for (index = array_dim - 1; index >= 0; index--)
2110 tree gnu_type_name
2111 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
2113 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
2114 gnu_type_name = DECL_NAME (gnu_type_name);
2116 gnu_field = create_field_decl (gnu_type_name,
2117 integer_type_node,
2118 gnu_bound_rec_type,
2119 0, NULL_TREE, NULL_TREE, 0);
2120 TREE_CHAIN (gnu_field) = gnu_field_list;
2121 gnu_field_list = gnu_field;
2124 finish_record_type (gnu_bound_rec_type, gnu_field_list,
2125 false, false);
2128 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2129 = (Convention (gnat_entity) == Convention_Fortran);
2130 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2131 = Is_Packed_Array_Type (gnat_entity);
2133 /* If our size depends on a placeholder and the maximum size doesn't
2134 overflow, use it. */
2135 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2136 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2137 && TREE_OVERFLOW (gnu_max_size))
2138 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2139 && TREE_OVERFLOW (gnu_max_size_unit))
2140 && !max_overflow)
2142 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2143 TYPE_SIZE (gnu_type));
2144 TYPE_SIZE_UNIT (gnu_type)
2145 = size_binop (MIN_EXPR, gnu_max_size_unit,
2146 TYPE_SIZE_UNIT (gnu_type));
2149 /* Set our alias set to that of our base type. This gives all
2150 array subtypes the same alias set. */
2151 copy_alias_set (gnu_type, gnu_base_type);
2154 /* If this is a packed type, make this type the same as the packed
2155 array type, but do some adjusting in the type first. */
2157 if (Present (Packed_Array_Type (gnat_entity)))
2159 Entity_Id gnat_index;
2160 tree gnu_inner_type;
2162 /* First finish the type we had been making so that we output
2163 debugging information for it */
2164 gnu_type
2165 = build_qualified_type (gnu_type,
2166 (TYPE_QUALS (gnu_type)
2167 | (TYPE_QUAL_VOLATILE
2168 * Treat_As_Volatile (gnat_entity))));
2169 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2170 !Comes_From_Source (gnat_entity),
2171 debug_info_p, gnat_entity);
2172 if (!Comes_From_Source (gnat_entity))
2173 DECL_ARTIFICIAL (gnu_decl) = 1;
2175 /* Save it as our equivalent in case the call below elaborates
2176 this type again. */
2177 save_gnu_tree (gnat_entity, gnu_decl, false);
2179 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2180 NULL_TREE, 0);
2181 this_made_decl = true;
2182 gnu_inner_type = gnu_type = TREE_TYPE (gnu_decl);
2183 save_gnu_tree (gnat_entity, NULL_TREE, false);
2185 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2186 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2187 || TYPE_IS_PADDING_P (gnu_inner_type)))
2188 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2190 /* We need to point the type we just made to our index type so
2191 the actual bounds can be put into a template. */
2193 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2194 && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2195 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2196 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2198 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2200 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2201 If it is, we need to make another type. */
2202 if (TYPE_MODULAR_P (gnu_inner_type))
2204 tree gnu_subtype;
2206 gnu_subtype = make_node (INTEGER_TYPE);
2208 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2209 TYPE_MIN_VALUE (gnu_subtype)
2210 = TYPE_MIN_VALUE (gnu_inner_type);
2211 TYPE_MAX_VALUE (gnu_subtype)
2212 = TYPE_MAX_VALUE (gnu_inner_type);
2213 TYPE_PRECISION (gnu_subtype)
2214 = TYPE_PRECISION (gnu_inner_type);
2215 TYPE_UNSIGNED (gnu_subtype)
2216 = TYPE_UNSIGNED (gnu_inner_type);
2217 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2218 layout_type (gnu_subtype);
2220 gnu_inner_type = gnu_subtype;
2223 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2226 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2228 for (gnat_index = First_Index (gnat_entity);
2229 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2230 SET_TYPE_ACTUAL_BOUNDS
2231 (gnu_inner_type,
2232 tree_cons (NULL_TREE,
2233 get_unpadded_type (Etype (gnat_index)),
2234 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2236 if (Convention (gnat_entity) != Convention_Fortran)
2237 SET_TYPE_ACTUAL_BOUNDS
2238 (gnu_inner_type,
2239 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2241 if (TREE_CODE (gnu_type) == RECORD_TYPE
2242 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2243 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2247 /* Abort if packed array with no packed array type field set. */
2248 else
2249 gcc_assert (!Is_Packed (gnat_entity));
2251 break;
2253 case E_String_Literal_Subtype:
2254 /* Create the type for a string literal. */
2256 Entity_Id gnat_full_type
2257 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2258 && Present (Full_View (Etype (gnat_entity)))
2259 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2260 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2261 tree gnu_string_array_type
2262 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2263 tree gnu_string_index_type
2264 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2265 (TYPE_DOMAIN (gnu_string_array_type))));
2266 tree gnu_lower_bound
2267 = convert (gnu_string_index_type,
2268 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2269 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2270 tree gnu_length = ssize_int (length - 1);
2271 tree gnu_upper_bound
2272 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2273 gnu_lower_bound,
2274 convert (gnu_string_index_type, gnu_length));
2275 tree gnu_range_type
2276 = build_range_type (gnu_string_index_type,
2277 gnu_lower_bound, gnu_upper_bound);
2278 tree gnu_index_type
2279 = create_index_type (convert (sizetype,
2280 TYPE_MIN_VALUE (gnu_range_type)),
2281 convert (sizetype,
2282 TYPE_MAX_VALUE (gnu_range_type)),
2283 gnu_range_type);
2285 gnu_type
2286 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2287 gnu_index_type);
2288 copy_alias_set (gnu_type, gnu_string_type);
2290 break;
2292 /* Record Types and Subtypes
2294 The following fields are defined on record types:
2296 Has_Discriminants True if the record has discriminants
2297 First_Discriminant Points to head of list of discriminants
2298 First_Entity Points to head of list of fields
2299 Is_Tagged_Type True if the record is tagged
2301 Implementation of Ada records and discriminated records:
2303 A record type definition is transformed into the equivalent of a C
2304 struct definition. The fields that are the discriminants which are
2305 found in the Full_Type_Declaration node and the elements of the
2306 Component_List found in the Record_Type_Definition node. The
2307 Component_List can be a recursive structure since each Variant of
2308 the Variant_Part of the Component_List has a Component_List.
2310 Processing of a record type definition comprises starting the list of
2311 field declarations here from the discriminants and the calling the
2312 function components_to_record to add the rest of the fields from the
2313 component list and return the gnu type node. The function
2314 components_to_record will call itself recursively as it traverses
2315 the tree. */
2317 case E_Record_Type:
2318 if (Has_Complex_Representation (gnat_entity))
2320 gnu_type
2321 = build_complex_type
2322 (get_unpadded_type
2323 (Etype (Defining_Entity
2324 (First (Component_Items
2325 (Component_List
2326 (Type_Definition
2327 (Declaration_Node (gnat_entity)))))))));
2329 break;
2333 Node_Id full_definition = Declaration_Node (gnat_entity);
2334 Node_Id record_definition = Type_Definition (full_definition);
2335 Entity_Id gnat_field;
2336 tree gnu_field;
2337 tree gnu_field_list = NULL_TREE;
2338 tree gnu_get_parent;
2339 int packed = (Is_Packed (gnat_entity) ? 1
2340 : (Component_Alignment (gnat_entity)
2341 == Calign_Storage_Unit) ? -1
2342 : 0);
2343 bool has_rep = Has_Specified_Layout (gnat_entity);
2344 bool all_rep = has_rep;
2345 bool is_extension
2346 = (Is_Tagged_Type (gnat_entity)
2347 && Nkind (record_definition) == N_Derived_Type_Definition);
2349 /* See if all fields have a rep clause. Stop when we find one
2350 that doesn't. */
2351 for (gnat_field = First_Entity (gnat_entity);
2352 Present (gnat_field) && all_rep;
2353 gnat_field = Next_Entity (gnat_field))
2354 if ((Ekind (gnat_field) == E_Component
2355 || Ekind (gnat_field) == E_Discriminant)
2356 && No (Component_Clause (gnat_field)))
2357 all_rep = false;
2359 /* If this is a record extension, go a level further to find the
2360 record definition. Also, verify we have a Parent_Subtype. */
2361 if (is_extension)
2363 if (!type_annotate_only
2364 || Present (Record_Extension_Part (record_definition)))
2365 record_definition = Record_Extension_Part (record_definition);
2367 gcc_assert (type_annotate_only
2368 || Present (Parent_Subtype (gnat_entity)));
2371 /* Make a node for the record. If we are not defining the record,
2372 suppress expanding incomplete types. We use the same RECORD_TYPE
2373 as for a dummy type and reset TYPE_DUMMY_P to show it's no longer
2374 a dummy.
2376 It is very tempting to delay resetting this bit until we are done
2377 with completing the type, e.g. to let possible intermediate
2378 elaboration of access types designating the record know it is not
2379 complete and arrange for update_pointer_to to fix things up later.
2381 It would be wrong, however, because dummy types are expected only
2382 to be created for Ada incomplete or private types, which is not
2383 what we have here. Doing so would make other parts of gigi think
2384 we are dealing with a really incomplete or private type, and have
2385 nasty side effects, typically on the generation of the associated
2386 debugging information. */
2387 gnu_type = make_dummy_type (gnat_entity);
2388 TYPE_DUMMY_P (gnu_type) = 0;
2390 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL && debug_info_p)
2391 DECL_IGNORED_P (TYPE_NAME (gnu_type)) = 0;
2393 TYPE_ALIGN (gnu_type) = 0;
2394 TYPE_PACKED (gnu_type) = packed || has_rep;
2396 if (!definition)
2397 defer_incomplete_level++, this_deferred = true;
2399 /* If both a size and rep clause was specified, put the size in
2400 the record type now so that it can get the proper mode. */
2401 if (has_rep && Known_Esize (gnat_entity))
2402 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2404 /* Always set the alignment here so that it can be used to
2405 set the mode, if it is making the alignment stricter. If
2406 it is invalid, it will be checked again below. If this is to
2407 be Atomic, choose a default alignment of a word unless we know
2408 the size and it's smaller. */
2409 if (Known_Alignment (gnat_entity))
2410 TYPE_ALIGN (gnu_type)
2411 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2412 else if (Is_Atomic (gnat_entity))
2413 TYPE_ALIGN (gnu_type)
2414 = (esize >= BITS_PER_WORD ? BITS_PER_WORD
2415 : 1 << (floor_log2 (esize - 1) + 1));
2417 /* If we have a Parent_Subtype, make a field for the parent. If
2418 this record has rep clauses, force the position to zero. */
2419 if (Present (Parent_Subtype (gnat_entity)))
2421 tree gnu_parent;
2423 /* A major complexity here is that the parent subtype will
2424 reference our discriminants. But those must reference
2425 the parent component of this record. So here we will
2426 initialize each of those components to a COMPONENT_REF.
2427 The first operand of that COMPONENT_REF is another
2428 COMPONENT_REF which will be filled in below, once
2429 the parent type can be safely built. */
2431 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2432 build0 (PLACEHOLDER_EXPR, gnu_type),
2433 build_decl (FIELD_DECL, NULL_TREE,
2434 NULL_TREE),
2435 NULL_TREE);
2437 if (Has_Discriminants (gnat_entity))
2438 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2439 Present (gnat_field);
2440 gnat_field = Next_Stored_Discriminant (gnat_field))
2441 if (Present (Corresponding_Discriminant (gnat_field)))
2442 save_gnu_tree
2443 (gnat_field,
2444 build3 (COMPONENT_REF,
2445 get_unpadded_type (Etype (gnat_field)),
2446 gnu_get_parent,
2447 gnat_to_gnu_field_decl (Corresponding_Discriminant
2448 (gnat_field)),
2449 NULL_TREE),
2450 true);
2452 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_entity));
2454 gnu_field_list
2455 = create_field_decl (get_identifier
2456 (Get_Name_String (Name_uParent)),
2457 gnu_parent, gnu_type, 0,
2458 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2459 has_rep ? bitsize_zero_node : 0, 1);
2460 DECL_INTERNAL_P (gnu_field_list) = 1;
2462 TREE_TYPE (gnu_get_parent) = gnu_parent;
2463 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2466 /* Add the fields for the discriminants into the record. */
2467 if (!Is_Unchecked_Union (gnat_entity)
2468 && Has_Discriminants (gnat_entity))
2469 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2470 Present (gnat_field);
2471 gnat_field = Next_Stored_Discriminant (gnat_field))
2473 /* If this is a record extension and this discriminant
2474 is the renaming of another discriminant, we've already
2475 handled the discriminant above. */
2476 if (Present (Parent_Subtype (gnat_entity))
2477 && Present (Corresponding_Discriminant (gnat_field)))
2478 continue;
2480 gnu_field
2481 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2483 /* Make an expression using a PLACEHOLDER_EXPR from the
2484 FIELD_DECL node just created and link that with the
2485 corresponding GNAT defining identifier. Then add to the
2486 list of fields. */
2487 save_gnu_tree (gnat_field,
2488 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2489 build0 (PLACEHOLDER_EXPR,
2490 DECL_CONTEXT (gnu_field)),
2491 gnu_field, NULL_TREE),
2492 true);
2494 TREE_CHAIN (gnu_field) = gnu_field_list;
2495 gnu_field_list = gnu_field;
2498 /* Put the discriminants into the record (backwards), so we can
2499 know the appropriate discriminant to use for the names of the
2500 variants. */
2501 TYPE_FIELDS (gnu_type) = gnu_field_list;
2503 /* Add the listed fields into the record and finish up. */
2504 components_to_record (gnu_type, Component_List (record_definition),
2505 gnu_field_list, packed, definition, NULL,
2506 false, all_rep, this_deferred);
2508 if (this_deferred)
2510 debug_deferred = true;
2511 defer_debug_level++;
2513 defer_debug_incomplete_list
2514 = tree_cons (NULL_TREE, gnu_type,
2515 defer_debug_incomplete_list);
2518 /* We used to remove the associations of the discriminants and
2519 _Parent for validity checking, but we may need them if there's
2520 Freeze_Node for a subtype used in this record. */
2522 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2523 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2525 /* If it is a tagged record force the type to BLKmode to insure
2526 that these objects will always be placed in memory. Do the
2527 same thing for limited record types. */
2528 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2529 TYPE_MODE (gnu_type) = BLKmode;
2531 /* If this is a derived type, we must make the alias set of this type
2532 the same as that of the type we are derived from. We assume here
2533 that the other type is already frozen. */
2534 if (Etype (gnat_entity) != gnat_entity
2535 && !(Is_Private_Type (Etype (gnat_entity))
2536 && Full_View (Etype (gnat_entity)) == gnat_entity))
2537 copy_alias_set (gnu_type, gnat_to_gnu_type (Etype (gnat_entity)));
2539 /* Fill in locations of fields. */
2540 annotate_rep (gnat_entity, gnu_type);
2542 /* If there are any entities in the chain corresponding to
2543 components that we did not elaborate, ensure we elaborate their
2544 types if they are Itypes. */
2545 for (gnat_temp = First_Entity (gnat_entity);
2546 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2547 if ((Ekind (gnat_temp) == E_Component
2548 || Ekind (gnat_temp) == E_Discriminant)
2549 && Is_Itype (Etype (gnat_temp))
2550 && !present_gnu_tree (gnat_temp))
2551 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2553 break;
2555 case E_Class_Wide_Subtype:
2556 /* If an equivalent type is present, that is what we should use.
2557 Otherwise, fall through to handle this like a record subtype
2558 since it may have constraints. */
2560 if (Present (Equivalent_Type (gnat_entity)))
2562 gnu_decl = gnat_to_gnu_entity (Equivalent_Type (gnat_entity),
2563 NULL_TREE, 0);
2564 maybe_present = true;
2565 break;
2568 /* ... fall through ... */
2570 case E_Record_Subtype:
2572 /* If Cloned_Subtype is Present it means this record subtype has
2573 identical layout to that type or subtype and we should use
2574 that GCC type for this one. The front end guarantees that
2575 the component list is shared. */
2576 if (Present (Cloned_Subtype (gnat_entity)))
2578 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2579 NULL_TREE, 0);
2580 maybe_present = true;
2583 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2584 changing the type, make a new type with each field having the
2585 type of the field in the new subtype but having the position
2586 computed by transforming every discriminant reference according
2587 to the constraints. We don't see any difference between
2588 private and nonprivate type here since derivations from types should
2589 have been deferred until the completion of the private type. */
2590 else
2592 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2593 tree gnu_base_type;
2594 tree gnu_orig_type;
2596 if (!definition)
2597 defer_incomplete_level++, this_deferred = true;
2599 /* Get the base type initially for its alignment and sizes. But
2600 if it is a padded type, we do all the other work with the
2601 unpadded type. */
2602 gnu_type = gnu_orig_type = gnu_base_type
2603 = gnat_to_gnu_type (gnat_base_type);
2605 if (TREE_CODE (gnu_type) == RECORD_TYPE
2606 && TYPE_IS_PADDING_P (gnu_type))
2607 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2609 if (present_gnu_tree (gnat_entity))
2611 maybe_present = true;
2612 break;
2615 /* When the type has discriminants, and these discriminants
2616 affect the shape of what it built, factor them in.
2618 If we are making a subtype of an Unchecked_Union (must be an
2619 Itype), just return the type.
2621 We can't just use Is_Constrained because private subtypes without
2622 discriminants of full types with discriminants with default
2623 expressions are Is_Constrained but aren't constrained! */
2625 if (IN (Ekind (gnat_base_type), Record_Kind)
2626 && !Is_For_Access_Subtype (gnat_entity)
2627 && !Is_Unchecked_Union (gnat_base_type)
2628 && Is_Constrained (gnat_entity)
2629 && Stored_Constraint (gnat_entity) != No_Elist
2630 && Present (Discriminant_Constraint (gnat_entity)))
2632 Entity_Id gnat_field;
2633 tree gnu_field_list = 0;
2634 tree gnu_pos_list
2635 = compute_field_positions (gnu_orig_type, NULL_TREE,
2636 size_zero_node, bitsize_zero_node,
2637 BIGGEST_ALIGNMENT);
2638 tree gnu_subst_list
2639 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2640 definition);
2641 tree gnu_temp;
2643 gnu_type = make_node (RECORD_TYPE);
2644 TYPE_NAME (gnu_type) = gnu_entity_id;
2645 TYPE_STUB_DECL (gnu_type)
2646 = create_type_decl (NULL_TREE, gnu_type, NULL, false, false,
2647 gnat_entity);
2648 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2650 for (gnat_field = First_Entity (gnat_entity);
2651 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2652 if ((Ekind (gnat_field) == E_Component
2653 || Ekind (gnat_field) == E_Discriminant)
2654 && (Underlying_Type (Scope (Original_Record_Component
2655 (gnat_field)))
2656 == gnat_base_type)
2657 && (No (Corresponding_Discriminant (gnat_field))
2658 || !Is_Tagged_Type (gnat_base_type)))
2660 tree gnu_old_field
2661 = gnat_to_gnu_field_decl (Original_Record_Component
2662 (gnat_field));
2663 tree gnu_offset
2664 = TREE_VALUE (purpose_member (gnu_old_field,
2665 gnu_pos_list));
2666 tree gnu_pos = TREE_PURPOSE (gnu_offset);
2667 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
2668 tree gnu_field_type
2669 = gnat_to_gnu_type (Etype (gnat_field));
2670 tree gnu_size = TYPE_SIZE (gnu_field_type);
2671 tree gnu_new_pos = 0;
2672 unsigned int offset_align
2673 = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
2675 tree gnu_field;
2677 /* If there was a component clause, the field types must be
2678 the same for the type and subtype, so copy the data from
2679 the old field to avoid recomputation here. Also if the
2680 field is justified modular and the optimization in
2681 gnat_to_gnu_field was applied. */
2682 if (Present (Component_Clause
2683 (Original_Record_Component (gnat_field)))
2684 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
2685 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
2686 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
2687 == TREE_TYPE (gnu_old_field)))
2689 gnu_size = DECL_SIZE (gnu_old_field);
2690 gnu_field_type = TREE_TYPE (gnu_old_field);
2693 /* If this was a bitfield, get the size from the old field.
2694 Also ensure the type can be placed into a bitfield. */
2695 else if (DECL_BIT_FIELD (gnu_old_field))
2697 gnu_size = DECL_SIZE (gnu_old_field);
2698 if (TYPE_MODE (gnu_field_type) == BLKmode
2699 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2700 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2701 gnu_field_type = make_packable_type (gnu_field_type);
2704 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
2705 for (gnu_temp = gnu_subst_list;
2706 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2707 gnu_pos = substitute_in_expr (gnu_pos,
2708 TREE_PURPOSE (gnu_temp),
2709 TREE_VALUE (gnu_temp));
2711 /* If the size is now a constant, we can set it as the
2712 size of the field when we make it. Otherwise, we need
2713 to deal with it specially. */
2714 if (TREE_CONSTANT (gnu_pos))
2715 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2717 gnu_field
2718 = create_field_decl
2719 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2720 0, gnu_size, gnu_new_pos,
2721 !DECL_NONADDRESSABLE_P (gnu_old_field));
2723 if (!TREE_CONSTANT (gnu_pos))
2725 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
2726 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2727 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
2728 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
2729 DECL_SIZE (gnu_field) = gnu_size;
2730 DECL_SIZE_UNIT (gnu_field)
2731 = convert (sizetype,
2732 size_binop (CEIL_DIV_EXPR, gnu_size,
2733 bitsize_unit_node));
2734 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2737 DECL_INTERNAL_P (gnu_field)
2738 = DECL_INTERNAL_P (gnu_old_field);
2739 SET_DECL_ORIGINAL_FIELD
2740 (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
2741 ? DECL_ORIGINAL_FIELD (gnu_old_field)
2742 : gnu_old_field));
2743 DECL_DISCRIMINANT_NUMBER (gnu_field)
2744 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2745 TREE_THIS_VOLATILE (gnu_field)
2746 = TREE_THIS_VOLATILE (gnu_old_field);
2747 TREE_CHAIN (gnu_field) = gnu_field_list;
2748 gnu_field_list = gnu_field;
2749 save_gnu_tree (gnat_field, gnu_field, false);
2752 /* Now go through the entities again looking for Itypes that
2753 we have not elaborated but should (e.g., Etypes of fields
2754 that have Original_Components). */
2755 for (gnat_field = First_Entity (gnat_entity);
2756 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2757 if ((Ekind (gnat_field) == E_Discriminant
2758 || Ekind (gnat_field) == E_Component)
2759 && !present_gnu_tree (Etype (gnat_field)))
2760 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
2762 finish_record_type (gnu_type, nreverse (gnu_field_list),
2763 true, false);
2765 /* Now set the size, alignment and alias set of the new type to
2766 match that of the old one, doing any substitutions, as
2767 above. */
2768 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2769 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2770 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
2771 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
2772 copy_alias_set (gnu_type, gnu_base_type);
2774 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2775 for (gnu_temp = gnu_subst_list;
2776 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2777 TYPE_SIZE (gnu_type)
2778 = substitute_in_expr (TYPE_SIZE (gnu_type),
2779 TREE_PURPOSE (gnu_temp),
2780 TREE_VALUE (gnu_temp));
2782 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
2783 for (gnu_temp = gnu_subst_list;
2784 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2785 TYPE_SIZE_UNIT (gnu_type)
2786 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
2787 TREE_PURPOSE (gnu_temp),
2788 TREE_VALUE (gnu_temp));
2790 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
2791 for (gnu_temp = gnu_subst_list;
2792 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2793 SET_TYPE_ADA_SIZE
2794 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
2795 TREE_PURPOSE (gnu_temp),
2796 TREE_VALUE (gnu_temp)));
2798 /* Recompute the mode of this record type now that we know its
2799 actual size. */
2800 compute_record_mode (gnu_type);
2802 /* Fill in locations of fields. */
2803 annotate_rep (gnat_entity, gnu_type);
2806 /* If we've made a new type, record it and make an XVS type to show
2807 what this is a subtype of. Some debuggers require the XVS
2808 type to be output first, so do it in that order. */
2809 if (gnu_type != gnu_orig_type)
2811 if (debug_info_p)
2813 tree gnu_subtype_marker = make_node (RECORD_TYPE);
2814 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
2816 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
2817 gnu_orig_name = DECL_NAME (gnu_orig_name);
2819 TYPE_NAME (gnu_subtype_marker)
2820 = create_concat_name (gnat_entity, "XVS");
2821 finish_record_type (gnu_subtype_marker,
2822 create_field_decl (gnu_orig_name,
2823 integer_type_node,
2824 gnu_subtype_marker,
2825 0, NULL_TREE,
2826 NULL_TREE, 0),
2827 false, false);
2830 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2831 TYPE_NAME (gnu_type) = gnu_entity_id;
2832 TYPE_STUB_DECL (gnu_type)
2833 = create_type_decl (TYPE_NAME (gnu_type), gnu_type,
2834 NULL, true, debug_info_p, gnat_entity);
2837 /* Otherwise, go down all the components in the new type and
2838 make them equivalent to those in the base type. */
2839 else
2840 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
2841 gnat_temp = Next_Entity (gnat_temp))
2842 if ((Ekind (gnat_temp) == E_Discriminant
2843 && !Is_Unchecked_Union (gnat_base_type))
2844 || Ekind (gnat_temp) == E_Component)
2845 save_gnu_tree (gnat_temp,
2846 gnat_to_gnu_field_decl
2847 (Original_Record_Component (gnat_temp)), false);
2849 break;
2851 case E_Access_Subprogram_Type:
2852 case E_Anonymous_Access_Subprogram_Type:
2853 /* If we are not defining this entity, and we have incomplete
2854 entities being processed above us, make a dummy type and
2855 fill it in later. */
2856 if (!definition && defer_incomplete_level != 0)
2858 struct incomplete *p
2859 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2861 gnu_type
2862 = build_pointer_type
2863 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2864 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2865 !Comes_From_Source (gnat_entity),
2866 debug_info_p, gnat_entity);
2867 save_gnu_tree (gnat_entity, gnu_decl, false);
2868 this_made_decl = saved = true;
2870 p->old_type = TREE_TYPE (gnu_type);
2871 p->full_type = Directly_Designated_Type (gnat_entity);
2872 p->next = defer_incomplete_list;
2873 defer_incomplete_list = p;
2874 break;
2877 /* ... fall through ... */
2879 case E_Allocator_Type:
2880 case E_Access_Type:
2881 case E_Access_Attribute_Type:
2882 case E_Anonymous_Access_Type:
2883 case E_General_Access_Type:
2885 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
2886 Entity_Id gnat_desig_full
2887 = ((IN (Ekind (Etype (gnat_desig_type)),
2888 Incomplete_Or_Private_Kind))
2889 ? Full_View (gnat_desig_type) : 0);
2890 /* We want to know if we'll be seeing the freeze node for any
2891 incomplete type we may be pointing to. */
2892 bool in_main_unit
2893 = (Present (gnat_desig_full)
2894 ? In_Extended_Main_Code_Unit (gnat_desig_full)
2895 : In_Extended_Main_Code_Unit (gnat_desig_type));
2896 bool got_fat_p = false;
2897 bool made_dummy = false;
2898 tree gnu_desig_type = NULL_TREE;
2899 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
2901 if (!targetm.valid_pointer_mode (p_mode))
2902 p_mode = ptr_mode;
2904 if (No (gnat_desig_full)
2905 && (Ekind (gnat_desig_type) == E_Class_Wide_Type
2906 || (Ekind (gnat_desig_type) == E_Class_Wide_Subtype
2907 && Present (Equivalent_Type (gnat_desig_type)))))
2909 if (Present (Equivalent_Type (gnat_desig_type)))
2911 gnat_desig_full = Equivalent_Type (gnat_desig_type);
2912 if (IN (Ekind (gnat_desig_full), Incomplete_Or_Private_Kind))
2913 gnat_desig_full = Full_View (gnat_desig_full);
2915 else if (IN (Ekind (Root_Type (gnat_desig_type)),
2916 Incomplete_Or_Private_Kind))
2917 gnat_desig_full = Full_View (Root_Type (gnat_desig_type));
2920 if (Present (gnat_desig_full) && Is_Concurrent_Type (gnat_desig_full))
2921 gnat_desig_full = Corresponding_Record_Type (gnat_desig_full);
2923 /* If either the designated type or its full view is an
2924 unconstrained array subtype, replace it with the type it's a
2925 subtype of. This avoids problems with multiple copies of
2926 unconstrained array types. */
2927 if (Ekind (gnat_desig_type) == E_Array_Subtype
2928 && !Is_Constrained (gnat_desig_type))
2929 gnat_desig_type = Etype (gnat_desig_type);
2930 if (Present (gnat_desig_full)
2931 && Ekind (gnat_desig_full) == E_Array_Subtype
2932 && !Is_Constrained (gnat_desig_full))
2933 gnat_desig_full = Etype (gnat_desig_full);
2935 /* If the designated type is a subtype of an incomplete record type,
2936 use the parent type to avoid order of elaboration issues. This
2937 can lose some code efficiency, but there is no alternative. */
2938 if (Present (gnat_desig_full)
2939 && Ekind (gnat_desig_full) == E_Record_Subtype
2940 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)
2941 gnat_desig_full = Etype (gnat_desig_full);
2943 /* If we are pointing to an incomplete type whose completion is an
2944 unconstrained array, make a fat pointer type instead of a pointer
2945 to VOID. The two types in our fields will be pointers to VOID and
2946 will be replaced in update_pointer_to. Similarly, if the type
2947 itself is a dummy type or an unconstrained array. Also make
2948 a dummy TYPE_OBJECT_RECORD_TYPE in case we have any thin
2949 pointers to it. */
2951 if ((Present (gnat_desig_full)
2952 && Is_Array_Type (gnat_desig_full)
2953 && !Is_Constrained (gnat_desig_full))
2954 || (present_gnu_tree (gnat_desig_type)
2955 && TYPE_IS_DUMMY_P (TREE_TYPE
2956 (get_gnu_tree (gnat_desig_type)))
2957 && Is_Array_Type (gnat_desig_type)
2958 && !Is_Constrained (gnat_desig_type))
2959 || (present_gnu_tree (gnat_desig_type)
2960 && (TREE_CODE (TREE_TYPE (get_gnu_tree (gnat_desig_type)))
2961 == UNCONSTRAINED_ARRAY_TYPE)
2962 && !(TYPE_POINTER_TO (TREE_TYPE
2963 (get_gnu_tree (gnat_desig_type)))))
2964 || (No (gnat_desig_full) && !in_main_unit
2965 && defer_incomplete_level
2966 && !present_gnu_tree (gnat_desig_type)
2967 && Is_Array_Type (gnat_desig_type)
2968 && !Is_Constrained (gnat_desig_type)))
2970 tree gnu_old
2971 = (present_gnu_tree (gnat_desig_type)
2972 ? gnat_to_gnu_type (gnat_desig_type)
2973 : make_dummy_type (gnat_desig_type));
2974 tree fields;
2976 /* Show the dummy we get will be a fat pointer. */
2977 got_fat_p = made_dummy = true;
2979 /* If the call above got something that has a pointer, that
2980 pointer is our type. This could have happened either
2981 because the type was elaborated or because somebody
2982 else executed the code below. */
2983 gnu_type = TYPE_POINTER_TO (gnu_old);
2984 if (!gnu_type)
2986 gnu_type = make_node (RECORD_TYPE);
2987 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
2988 TYPE_POINTER_TO (gnu_old) = gnu_type;
2990 Sloc_to_locus (Sloc (gnat_entity), &input_location);
2991 fields
2992 = chainon (chainon (NULL_TREE,
2993 create_field_decl
2994 (get_identifier ("P_ARRAY"),
2995 ptr_void_type_node, gnu_type,
2996 0, 0, 0, 0)),
2997 create_field_decl (get_identifier ("P_BOUNDS"),
2998 ptr_void_type_node,
2999 gnu_type, 0, 0, 0, 0));
3001 /* Make sure we can place this into a register. */
3002 TYPE_ALIGN (gnu_type)
3003 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
3004 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
3005 finish_record_type (gnu_type, fields, false, true);
3007 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
3008 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
3009 = concat_id_with_name (get_entity_name (gnat_desig_type),
3010 "XUT");
3011 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
3015 /* If we already know what the full type is, use it. */
3016 else if (Present (gnat_desig_full)
3017 && present_gnu_tree (gnat_desig_full))
3018 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3020 /* Get the type of the thing we are to point to and build a pointer
3021 to it. If it is a reference to an incomplete or private type with a
3022 full view that is a record, make a dummy type node and get the
3023 actual type later when we have verified it is safe. */
3024 else if (!in_main_unit
3025 && !present_gnu_tree (gnat_desig_type)
3026 && Present (gnat_desig_full)
3027 && !present_gnu_tree (gnat_desig_full)
3028 && Is_Record_Type (gnat_desig_full))
3030 gnu_desig_type = make_dummy_type (gnat_desig_type);
3031 made_dummy = true;
3034 /* Likewise if we are pointing to a record or array and we are to defer
3035 elaborating incomplete types. We do this since this access type
3036 may be the full view of some private type. Note that the
3037 unconstrained array case is handled above. */
3038 else if ((!in_main_unit || imported_p) && defer_incomplete_level != 0
3039 && !present_gnu_tree (gnat_desig_type)
3040 && ((Is_Record_Type (gnat_desig_type)
3041 || Is_Array_Type (gnat_desig_type))
3042 || (Present (gnat_desig_full)
3043 && (Is_Record_Type (gnat_desig_full)
3044 || Is_Array_Type (gnat_desig_full)))))
3046 gnu_desig_type = make_dummy_type (gnat_desig_type);
3047 made_dummy = true;
3049 else if (gnat_desig_type == gnat_entity)
3051 gnu_type
3052 = build_pointer_type_for_mode (make_node (VOID_TYPE),
3053 p_mode,
3054 No_Strict_Aliasing (gnat_entity));
3055 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3057 else
3058 gnu_desig_type = gnat_to_gnu_type (gnat_desig_type);
3060 /* It is possible that the above call to gnat_to_gnu_type resolved our
3061 type. If so, just return it. */
3062 if (present_gnu_tree (gnat_entity))
3064 maybe_present = true;
3065 break;
3068 /* If we have a GCC type for the designated type, possibly modify it
3069 if we are pointing only to constant objects and then make a pointer
3070 to it. Don't do this for unconstrained arrays. */
3071 if (!gnu_type && gnu_desig_type)
3073 if (Is_Access_Constant (gnat_entity)
3074 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3076 gnu_desig_type
3077 = build_qualified_type
3078 (gnu_desig_type,
3079 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3081 /* Some extra processing is required if we are building a
3082 pointer to an incomplete type (in the GCC sense). We might
3083 have such a type if we just made a dummy, or directly out
3084 of the call to gnat_to_gnu_type above if we are processing
3085 an access type for a record component designating the
3086 record type itself. */
3087 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3089 /* We must ensure that the pointer to variant we make will
3090 be processed by update_pointer_to when the initial type
3091 is completed. Pretend we made a dummy and let further
3092 processing act as usual. */
3093 made_dummy = true;
3095 /* We must ensure that update_pointer_to will not retrieve
3096 the dummy variant when building a properly qualified
3097 version of the complete type. We take advantage of the
3098 fact that get_qualified_type is requiring TYPE_NAMEs to
3099 match to influence build_qualified_type and then also
3100 update_pointer_to here. */
3101 TYPE_NAME (gnu_desig_type)
3102 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3106 gnu_type
3107 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3108 No_Strict_Aliasing (gnat_entity));
3111 /* If we are not defining this object and we made a dummy pointer,
3112 save our current definition, evaluate the actual type, and replace
3113 the tentative type we made with the actual one. If we are to defer
3114 actually looking up the actual type, make an entry in the
3115 deferred list. */
3117 if (!in_main_unit && made_dummy)
3119 tree gnu_old_type
3120 = TYPE_FAT_POINTER_P (gnu_type)
3121 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3123 if (esize == POINTER_SIZE
3124 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
3125 gnu_type
3126 = build_pointer_type
3127 (TYPE_OBJECT_RECORD_TYPE
3128 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3130 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3131 !Comes_From_Source (gnat_entity),
3132 debug_info_p, gnat_entity);
3133 save_gnu_tree (gnat_entity, gnu_decl, false);
3134 this_made_decl = saved = true;
3136 if (defer_incomplete_level == 0)
3137 /* Note that the call to gnat_to_gnu_type here might have
3138 updated gnu_old_type directly, in which case it is not a
3139 dummy type any more when we get into update_pointer_to.
3141 This may happen for instance when the designated type is a
3142 record type, because their elaboration starts with an
3143 initial node from make_dummy_type, which may yield the same
3144 node as the one we got.
3146 Besides, variants of this non-dummy type might have been
3147 created along the way. update_pointer_to is expected to
3148 properly take care of those situations. */
3149 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
3150 gnat_to_gnu_type (gnat_desig_type));
3151 else
3153 struct incomplete *p
3154 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3156 p->old_type = gnu_old_type;
3157 p->full_type = gnat_desig_type;
3158 p->next = defer_incomplete_list;
3159 defer_incomplete_list = p;
3163 break;
3165 case E_Access_Protected_Subprogram_Type:
3166 case E_Anonymous_Access_Protected_Subprogram_Type:
3167 if (type_annotate_only && No (Equivalent_Type (gnat_entity)))
3168 gnu_type = build_pointer_type (void_type_node);
3169 else
3170 /* The runtime representation is the equivalent type. */
3171 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3173 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3174 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3175 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3176 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3177 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3178 NULL_TREE, 0);
3180 break;
3182 case E_Access_Subtype:
3184 /* We treat this as identical to its base type; any constraint is
3185 meaningful only to the front end.
3187 The designated type must be elaborated as well, if it does
3188 not have its own freeze node. Designated (sub)types created
3189 for constrained components of records with discriminants are
3190 not frozen by the front end and thus not elaborated by gigi,
3191 because their use may appear before the base type is frozen,
3192 and because it is not clear that they are needed anywhere in
3193 Gigi. With the current model, there is no correct place where
3194 they could be elaborated. */
3196 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3197 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3198 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3199 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3200 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3202 /* If we are not defining this entity, and we have incomplete
3203 entities being processed above us, make a dummy type and
3204 elaborate it later. */
3205 if (!definition && defer_incomplete_level != 0)
3207 struct incomplete *p
3208 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3209 tree gnu_ptr_type
3210 = build_pointer_type
3211 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3213 p->old_type = TREE_TYPE (gnu_ptr_type);
3214 p->full_type = Directly_Designated_Type (gnat_entity);
3215 p->next = defer_incomplete_list;
3216 defer_incomplete_list = p;
3218 else if (IN (Ekind (Base_Type
3219 (Directly_Designated_Type (gnat_entity))),
3220 Incomplete_Or_Private_Kind))
3222 else
3223 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3224 NULL_TREE, 0);
3227 maybe_present = true;
3228 break;
3230 /* Subprogram Entities
3232 The following access functions are defined for subprograms (functions
3233 or procedures):
3235 First_Formal The first formal parameter.
3236 Is_Imported Indicates that the subprogram has appeared in
3237 an INTERFACE or IMPORT pragma. For now we
3238 assume that the external language is C.
3239 Is_Inlined True if the subprogram is to be inlined.
3241 In addition for function subprograms we have:
3243 Etype Return type of the function.
3245 Each parameter is first checked by calling must_pass_by_ref on its
3246 type to determine if it is passed by reference. For parameters which
3247 are copied in, if they are Ada IN OUT or OUT parameters, their return
3248 value becomes part of a record which becomes the return type of the
3249 function (C function - note that this applies only to Ada procedures
3250 so there is no Ada return type). Additional code to store back the
3251 parameters will be generated on the caller side. This transformation
3252 is done here, not in the front-end.
3254 The intended result of the transformation can be seen from the
3255 equivalent source rewritings that follow:
3257 struct temp {int a,b};
3258 procedure P (A,B: IN OUT ...) is temp P (int A,B) {
3259 .. ..
3260 end P; return {A,B};
3262 procedure call
3265 temp t;
3266 P(X,Y); t = P(X,Y);
3267 X = t.a , Y = t.b;
3270 For subprogram types we need to perform mainly the same conversions to
3271 GCC form that are needed for procedures and function declarations. The
3272 only difference is that at the end, we make a type declaration instead
3273 of a function declaration. */
3275 case E_Subprogram_Type:
3276 case E_Function:
3277 case E_Procedure:
3279 /* The first GCC parameter declaration (a PARM_DECL node). The
3280 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3281 actually is the head of this parameter list. */
3282 tree gnu_param_list = NULL_TREE;
3283 /* The type returned by a function. If the subprogram is a procedure
3284 this type should be void_type_node. */
3285 tree gnu_return_type = void_type_node;
3286 /* List of fields in return type of procedure with copy in copy out
3287 parameters. */
3288 tree gnu_field_list = NULL_TREE;
3289 /* Non-null for subprograms containing parameters passed by copy in
3290 copy out (Ada IN OUT or OUT parameters not passed by reference),
3291 in which case it is the list of nodes used to specify the values of
3292 the in out/out parameters that are returned as a record upon
3293 procedure return. The TREE_PURPOSE of an element of this list is
3294 a field of the record and the TREE_VALUE is the PARM_DECL
3295 corresponding to that field. This list will be saved in the
3296 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3297 tree gnu_return_list = NULL_TREE;
3298 /* If an import pragma asks to map this subprogram to a GCC builtin,
3299 this is the builtin DECL node. */
3300 tree gnu_builtin_decl = NULL_TREE;
3301 Entity_Id gnat_param;
3302 bool inline_flag = Is_Inlined (gnat_entity);
3303 bool public_flag = Is_Public (gnat_entity);
3304 bool extern_flag
3305 = (Is_Public (gnat_entity) && !definition) || imported_p;
3306 bool pure_flag = Is_Pure (gnat_entity);
3307 bool volatile_flag = No_Return (gnat_entity);
3308 bool returns_by_ref = false;
3309 bool returns_unconstrained = false;
3310 bool returns_by_target_ptr = false;
3311 tree gnu_ext_name = create_concat_name (gnat_entity, 0);
3312 bool has_copy_in_out = false;
3313 int parmnum;
3315 if (kind == E_Subprogram_Type && !definition)
3316 /* A parameter may refer to this type, so defer completion
3317 of any incomplete types. */
3318 defer_incomplete_level++, this_deferred = true;
3320 /* If the subprogram has an alias, it is probably inherited, so
3321 we can use the original one. If the original "subprogram"
3322 is actually an enumeration literal, it may be the first use
3323 of its type, so we must elaborate that type now. */
3324 if (Present (Alias (gnat_entity)))
3326 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3327 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3329 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3330 gnu_expr, 0);
3332 /* Elaborate any Itypes in the parameters of this entity. */
3333 for (gnat_temp = First_Formal (gnat_entity);
3334 Present (gnat_temp);
3335 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3336 if (Is_Itype (Etype (gnat_temp)))
3337 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3339 break;
3342 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3343 corresponding DECL node.
3345 We still want the parameter associations to take place because the
3346 proper generation of calls depends on it (a GNAT parameter without
3347 a corresponding GCC tree has a very specific meaning), so we don't
3348 just break here. */
3349 if (Convention (gnat_entity) == Convention_Intrinsic)
3350 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3352 /* ??? What if we don't find the builtin node above ? warn ? err ?
3353 In the current state we neither warn nor err, and calls will just
3354 be handled as for regular subprograms. */
3356 if (kind == E_Function || kind == E_Subprogram_Type)
3357 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3359 /* If this function returns by reference, make the actual
3360 return type of this function the pointer and mark the decl. */
3361 if (Returns_By_Ref (gnat_entity))
3363 returns_by_ref = true;
3364 gnu_return_type = build_pointer_type (gnu_return_type);
3367 /* If the Mechanism is By_Reference, ensure the return type uses
3368 the machine's by-reference mechanism, which may not the same
3369 as above (e.g., it might be by passing a fake parameter). */
3370 else if (kind == E_Function
3371 && Mechanism (gnat_entity) == By_Reference)
3373 gnu_return_type = copy_type (gnu_return_type);
3374 TREE_ADDRESSABLE (gnu_return_type) = 1;
3377 /* If we are supposed to return an unconstrained array,
3378 actually return a fat pointer and make a note of that. Return
3379 a pointer to an unconstrained record of variable size. */
3380 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3382 gnu_return_type = TREE_TYPE (gnu_return_type);
3383 returns_unconstrained = true;
3386 /* If the type requires a transient scope, the result is allocated
3387 on the secondary stack, so the result type of the function is
3388 just a pointer. */
3389 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3391 gnu_return_type = build_pointer_type (gnu_return_type);
3392 returns_unconstrained = true;
3395 /* If the type is a padded type and the underlying type would not
3396 be passed by reference or this function has a foreign convention,
3397 return the underlying type. */
3398 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3399 && TYPE_IS_PADDING_P (gnu_return_type)
3400 && (!default_pass_by_ref (TREE_TYPE
3401 (TYPE_FIELDS (gnu_return_type)))
3402 || Has_Foreign_Convention (gnat_entity)))
3403 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3405 /* If the return type is unconstrained, that means it must have a
3406 maximum size. We convert the function into a procedure and its
3407 caller will pass a pointer to an object of that maximum size as the
3408 first parameter when we call the function. */
3409 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
3411 returns_by_target_ptr = true;
3412 gnu_param_list
3413 = create_param_decl (get_identifier ("TARGET"),
3414 build_reference_type (gnu_return_type),
3415 true);
3416 gnu_return_type = void_type_node;
3419 /* If the return type has a size that overflows, we cannot have
3420 a function that returns that type. This usage doesn't make
3421 sense anyway, so give an error here. */
3422 if (TYPE_SIZE_UNIT (gnu_return_type)
3423 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
3425 post_error ("cannot return type whose size overflows",
3426 gnat_entity);
3427 gnu_return_type = copy_node (gnu_return_type);
3428 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
3429 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
3430 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
3431 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
3434 /* Look at all our parameters and get the type of
3435 each. While doing this, build a copy-out structure if
3436 we need one. */
3438 for (gnat_param = First_Formal (gnat_entity), parmnum = 0;
3439 Present (gnat_param);
3440 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3442 tree gnu_param_name = get_entity_name (gnat_param);
3443 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3444 tree gnu_param, gnu_field;
3445 bool by_ref_p = false;
3446 bool by_descr_p = false;
3447 bool by_component_ptr_p = false;
3448 bool copy_in_copy_out_flag = false;
3449 bool req_by_copy = false, req_by_ref = false;
3451 /* Builtins are expanded inline and there is no real call sequence
3452 involved. so the type expected by the underlying expander is
3453 always the type of each argument "as is". */
3454 if (gnu_builtin_decl)
3455 req_by_copy = 1;
3457 /* Otherwise, see if a Mechanism was supplied that forced this
3458 parameter to be passed one way or another. */
3459 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3460 req_by_copy = true;
3461 else if (Mechanism (gnat_param) == Default)
3463 else if (Mechanism (gnat_param) == By_Copy)
3464 req_by_copy = true;
3465 else if (Mechanism (gnat_param) == By_Reference)
3466 req_by_ref = true;
3467 else if (Mechanism (gnat_param) <= By_Descriptor)
3468 by_descr_p = true;
3469 else if (Mechanism (gnat_param) > 0)
3471 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3472 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3473 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3474 Mechanism (gnat_param)))
3475 req_by_ref = true;
3476 else
3477 req_by_copy = true;
3479 else
3480 post_error ("unsupported mechanism for&", gnat_param);
3482 /* If this is either a foreign function or if the
3483 underlying type won't be passed by reference, strip off
3484 possible padding type. */
3485 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
3486 && TYPE_IS_PADDING_P (gnu_param_type)
3487 && (req_by_ref || Has_Foreign_Convention (gnat_entity)
3488 || (!must_pass_by_ref (TREE_TYPE (TYPE_FIELDS
3489 (gnu_param_type)))
3490 && (req_by_copy
3491 || !default_pass_by_ref (TREE_TYPE
3492 (TYPE_FIELDS
3493 (gnu_param_type)))))))
3494 gnu_param_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
3496 /* If this is an IN parameter it is read-only, so make a variant
3497 of the type that is read-only.
3499 ??? However, if this is an unconstrained array, that type can
3500 be very complex. So skip it for now. Likewise for any other
3501 self-referential type. */
3502 if (Ekind (gnat_param) == E_In_Parameter
3503 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
3504 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
3505 gnu_param_type
3506 = build_qualified_type (gnu_param_type,
3507 (TYPE_QUALS (gnu_param_type)
3508 | TYPE_QUAL_CONST));
3510 /* For foreign conventions, pass arrays as a pointer to the
3511 underlying type. First check for unconstrained array and get
3512 the underlying array. Then get the component type and build
3513 a pointer to it. */
3514 if (Has_Foreign_Convention (gnat_entity)
3515 && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
3516 gnu_param_type
3517 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3518 (TREE_TYPE (gnu_param_type))));
3520 if (by_descr_p)
3521 gnu_param_type
3522 = build_pointer_type
3523 (build_vms_descriptor (gnu_param_type,
3524 Mechanism (gnat_param), gnat_entity));
3526 else if (Has_Foreign_Convention (gnat_entity)
3527 && !req_by_copy
3528 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
3530 /* Strip off any multi-dimensional entries, then strip
3531 off the last array to get the component type. */
3532 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
3533 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
3534 gnu_param_type = TREE_TYPE (gnu_param_type);
3536 by_component_ptr_p = true;
3537 gnu_param_type = TREE_TYPE (gnu_param_type);
3539 if (Ekind (gnat_param) == E_In_Parameter)
3540 gnu_param_type
3541 = build_qualified_type (gnu_param_type,
3542 (TYPE_QUALS (gnu_param_type)
3543 | TYPE_QUAL_CONST));
3545 gnu_param_type = build_pointer_type (gnu_param_type);
3548 /* Fat pointers are passed as thin pointers for foreign
3549 conventions. */
3550 else if (Has_Foreign_Convention (gnat_entity)
3551 && TYPE_FAT_POINTER_P (gnu_param_type))
3552 gnu_param_type
3553 = make_type_from_size (gnu_param_type,
3554 size_int (POINTER_SIZE), false);
3556 /* If we must pass or were requested to pass by reference, do so.
3557 If we were requested to pass by copy, do so.
3558 Otherwise, for foreign conventions, pass all in out parameters
3559 or aggregates by reference. For COBOL and Fortran, pass
3560 all integer and FP types that way too. For Convention Ada,
3561 use the standard Ada default. */
3562 else if (must_pass_by_ref (gnu_param_type) || req_by_ref
3563 || (!req_by_copy
3564 && ((Has_Foreign_Convention (gnat_entity)
3565 && (Ekind (gnat_param) != E_In_Parameter
3566 || AGGREGATE_TYPE_P (gnu_param_type)))
3567 || (((Convention (gnat_entity)
3568 == Convention_Fortran)
3569 || (Convention (gnat_entity)
3570 == Convention_COBOL))
3571 && (INTEGRAL_TYPE_P (gnu_param_type)
3572 || FLOAT_TYPE_P (gnu_param_type)))
3573 /* For convention Ada, see if we pass by reference
3574 by default. */
3575 || (!Has_Foreign_Convention (gnat_entity)
3576 && default_pass_by_ref (gnu_param_type)))))
3578 gnu_param_type = build_reference_type (gnu_param_type);
3579 by_ref_p = true;
3582 else if (Ekind (gnat_param) != E_In_Parameter)
3583 copy_in_copy_out_flag = true;
3585 if (req_by_copy && (by_ref_p || by_component_ptr_p))
3586 post_error ("?cannot pass & by copy", gnat_param);
3588 /* If this is an OUT parameter that isn't passed by reference
3589 and isn't a pointer or aggregate, we don't make a PARM_DECL
3590 for it. Instead, it will be a VAR_DECL created when we process
3591 the procedure. For the special parameter of Valued_Procedure,
3592 never pass it in.
3594 An exception is made to cover the RM-6.4.1 rule requiring "by
3595 copy" out parameters with discriminants or implicit initial
3596 values to be handled like in out parameters. These type are
3597 normally built as aggregates, and hence passed by reference,
3598 except for some packed arrays which end up encoded in special
3599 integer types.
3601 The exception we need to make is then for packed arrays of
3602 records with discriminants or implicit initial values. We have
3603 no light/easy way to check for the latter case, so we merely
3604 check for packed arrays of records. This may lead to useless
3605 copy-in operations, but in very rare cases only, as these would
3606 be exceptions in a set of already exceptional situations. */
3607 if (Ekind (gnat_param) == E_Out_Parameter && !by_ref_p
3608 && ((Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3609 || (!by_descr_p
3610 && !POINTER_TYPE_P (gnu_param_type)
3611 && !AGGREGATE_TYPE_P (gnu_param_type)))
3612 && !(Is_Array_Type (Etype (gnat_param))
3613 && Is_Packed (Etype (gnat_param))
3614 && Is_Composite_Type (Component_Type
3615 (Etype (gnat_param)))))
3616 gnu_param = NULL_TREE;
3617 else
3619 gnu_param
3620 = create_param_decl
3621 (gnu_param_name, gnu_param_type,
3622 by_ref_p || by_component_ptr_p
3623 || Ekind (gnat_param) == E_In_Parameter);
3625 DECL_BY_REF_P (gnu_param) = by_ref_p;
3626 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr_p;
3627 DECL_BY_DESCRIPTOR_P (gnu_param) = by_descr_p;
3628 DECL_POINTS_TO_READONLY_P (gnu_param)
3629 = (Ekind (gnat_param) == E_In_Parameter
3630 && (by_ref_p || by_component_ptr_p));
3631 Sloc_to_locus (Sloc (gnat_param),
3632 &DECL_SOURCE_LOCATION (gnu_param));
3633 save_gnu_tree (gnat_param, gnu_param, false);
3634 gnu_param_list = chainon (gnu_param, gnu_param_list);
3636 /* If a parameter is a pointer, this function may modify
3637 memory through it and thus shouldn't be considered
3638 a pure function. Also, the memory may be modified
3639 between two calls, so they can't be CSE'ed. The latter
3640 case also handles by-ref parameters. */
3641 if (POINTER_TYPE_P (gnu_param_type)
3642 || TYPE_FAT_POINTER_P (gnu_param_type))
3643 pure_flag = false;
3646 if (copy_in_copy_out_flag)
3648 if (!has_copy_in_out)
3650 gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
3651 gnu_return_type = make_node (RECORD_TYPE);
3652 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3653 has_copy_in_out = true;
3656 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3657 gnu_return_type, 0, 0, 0, 0);
3658 Sloc_to_locus (Sloc (gnat_param),
3659 &DECL_SOURCE_LOCATION (gnu_field));
3660 TREE_CHAIN (gnu_field) = gnu_field_list;
3661 gnu_field_list = gnu_field;
3662 gnu_return_list = tree_cons (gnu_field, gnu_param,
3663 gnu_return_list);
3667 /* Do not compute record for out parameters if subprogram is
3668 stubbed since structures are incomplete for the back-end. */
3669 if (gnu_field_list
3670 && Convention (gnat_entity) != Convention_Stubbed)
3672 /* If all types are not complete, defer emission of debug
3673 information for this record types. Otherwise, we risk emitting
3674 debug information for a dummy type contained in the fields
3675 for that record. */
3676 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3677 false, defer_incomplete_level);
3679 if (defer_incomplete_level)
3681 debug_deferred = true;
3682 defer_debug_level++;
3684 defer_debug_incomplete_list
3685 = tree_cons (NULL_TREE, gnu_return_type,
3686 defer_debug_incomplete_list);
3690 /* If we have a CICO list but it has only one entry, we convert
3691 this function into a function that simply returns that one
3692 object. */
3693 if (list_length (gnu_return_list) == 1)
3694 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3696 if (Has_Stdcall_Convention (gnat_entity))
3698 struct attrib *attr
3699 = (struct attrib *) xmalloc (sizeof (struct attrib));
3701 attr->next = attr_list;
3702 attr->type = ATTR_MACHINE_ATTRIBUTE;
3703 attr->name = get_identifier ("stdcall");
3704 attr->args = NULL_TREE;
3705 attr->error_point = gnat_entity;
3706 attr_list = attr;
3709 /* Both lists ware built in reverse. */
3710 gnu_param_list = nreverse (gnu_param_list);
3711 gnu_return_list = nreverse (gnu_return_list);
3713 gnu_type
3714 = create_subprog_type (gnu_return_type, gnu_param_list,
3715 gnu_return_list, returns_unconstrained,
3716 returns_by_ref,
3717 Function_Returns_With_DSP (gnat_entity),
3718 returns_by_target_ptr);
3720 /* A subprogram (something that doesn't return anything) shouldn't
3721 be considered Pure since there would be no reason for such a
3722 subprogram. Note that procedures with Out (or In Out) parameters
3723 have already been converted into a function with a return type. */
3724 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
3725 pure_flag = false;
3727 gnu_type
3728 = build_qualified_type (gnu_type,
3729 (TYPE_QUALS (gnu_type)
3730 | (TYPE_QUAL_CONST * pure_flag)
3731 | (TYPE_QUAL_VOLATILE * volatile_flag)));
3733 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3735 /* If we have a builtin decl for that function, check the signatures
3736 compatibilities. If the signatures are compatible, use the builtin
3737 decl. If they are not, we expect the checker predicate to have
3738 posted the appropriate errors, and just continue with what we have
3739 so far. */
3740 if (gnu_builtin_decl)
3742 tree gnu_builtin_type = TREE_TYPE (gnu_builtin_decl);
3744 if (compatible_signatures_p (gnu_type, gnu_builtin_type))
3746 gnu_decl = gnu_builtin_decl;
3747 gnu_type = gnu_builtin_type;
3748 break;
3752 /* If there was no specified Interface_Name and the external and
3753 internal names of the subprogram are the same, only use the
3754 internal name to allow disambiguation of nested subprograms. */
3755 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_id)
3756 gnu_ext_name = NULL_TREE;
3758 /* If we are defining the subprogram and it has an Address clause
3759 we must get the address expression from the saved GCC tree for the
3760 subprogram if it has a Freeze_Node. Otherwise, we elaborate
3761 the address expression here since the front-end has guaranteed
3762 in that case that the elaboration has no effects. If there is
3763 an Address clause and we are not defining the object, just
3764 make it a constant. */
3765 if (Present (Address_Clause (gnat_entity)))
3767 tree gnu_address = NULL_TREE;
3769 if (definition)
3770 gnu_address
3771 = (present_gnu_tree (gnat_entity)
3772 ? get_gnu_tree (gnat_entity)
3773 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
3775 save_gnu_tree (gnat_entity, NULL_TREE, false);
3777 gnu_type = build_reference_type (gnu_type);
3778 if (gnu_address)
3779 gnu_address = convert (gnu_type, gnu_address);
3781 gnu_decl
3782 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
3783 gnu_address, false, Is_Public (gnat_entity),
3784 extern_flag, false, NULL, gnat_entity);
3785 DECL_BY_REF_P (gnu_decl) = 1;
3788 else if (kind == E_Subprogram_Type)
3789 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3790 !Comes_From_Source (gnat_entity),
3791 debug_info_p && !defer_incomplete_level,
3792 gnat_entity);
3793 else
3795 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
3796 gnu_type, gnu_param_list,
3797 inline_flag, public_flag,
3798 extern_flag, attr_list,
3799 gnat_entity);
3800 DECL_STUBBED_P (gnu_decl)
3801 = Convention (gnat_entity) == Convention_Stubbed;
3804 break;
3806 case E_Incomplete_Type:
3807 case E_Private_Type:
3808 case E_Limited_Private_Type:
3809 case E_Record_Type_With_Private:
3810 case E_Private_Subtype:
3811 case E_Limited_Private_Subtype:
3812 case E_Record_Subtype_With_Private:
3814 /* If this type does not have a full view in the unit we are
3815 compiling, then just get the type from its Etype. */
3816 if (No (Full_View (gnat_entity)))
3818 /* If this is an incomplete type with no full view, it must be
3819 either a limited view brought in by a limited_with clause, in
3820 which case we use the non-limited view, or a Taft Amendement
3821 type, in which case we just return a dummy type. */
3822 if (kind == E_Incomplete_Type)
3824 if (From_With_Type (gnat_entity)
3825 && Present (Non_Limited_View (gnat_entity)))
3826 gnu_decl = gnat_to_gnu_entity (Non_Limited_View (gnat_entity),
3827 NULL_TREE, 0);
3828 else
3829 gnu_type = make_dummy_type (gnat_entity);
3832 else if (Present (Underlying_Full_View (gnat_entity)))
3833 gnu_decl = gnat_to_gnu_entity (Underlying_Full_View (gnat_entity),
3834 NULL_TREE, 0);
3835 else
3837 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
3838 NULL_TREE, 0);
3839 maybe_present = true;
3842 break;
3845 /* Otherwise, if we are not defining the type now, get the
3846 type from the full view. But always get the type from the full
3847 view for define on use types, since otherwise we won't see them! */
3849 else if (!definition
3850 || (Is_Itype (Full_View (gnat_entity))
3851 && No (Freeze_Node (gnat_entity)))
3852 || (Is_Itype (gnat_entity)
3853 && No (Freeze_Node (Full_View (gnat_entity)))))
3855 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
3856 NULL_TREE, 0);
3857 maybe_present = true;
3858 break;
3861 /* For incomplete types, make a dummy type entry which will be
3862 replaced later. */
3863 gnu_type = make_dummy_type (gnat_entity);
3865 /* Save this type as the full declaration's type so we can do any needed
3866 updates when we see it. */
3867 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3868 !Comes_From_Source (gnat_entity),
3869 debug_info_p, gnat_entity);
3870 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
3871 break;
3873 /* Simple class_wide types are always viewed as their root_type
3874 by Gigi unless an Equivalent_Type is specified. */
3875 case E_Class_Wide_Type:
3876 if (Present (Equivalent_Type (gnat_entity)))
3877 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3878 else
3879 gnu_type = gnat_to_gnu_type (Root_Type (gnat_entity));
3881 maybe_present = true;
3882 break;
3884 case E_Task_Type:
3885 case E_Task_Subtype:
3886 case E_Protected_Type:
3887 case E_Protected_Subtype:
3888 if (type_annotate_only && No (Corresponding_Record_Type (gnat_entity)))
3889 gnu_type = void_type_node;
3890 else
3891 gnu_type = gnat_to_gnu_type (Corresponding_Record_Type (gnat_entity));
3893 maybe_present = true;
3894 break;
3896 case E_Label:
3897 gnu_decl = create_label_decl (gnu_entity_id);
3898 break;
3900 case E_Block:
3901 case E_Loop:
3902 /* Nothing at all to do here, so just return an ERROR_MARK and claim
3903 we've already saved it, so we don't try to. */
3904 gnu_decl = error_mark_node;
3905 saved = true;
3906 break;
3908 default:
3909 gcc_unreachable ();
3912 /* If we had a case where we evaluated another type and it might have
3913 defined this one, handle it here. */
3914 if (maybe_present && present_gnu_tree (gnat_entity))
3916 gnu_decl = get_gnu_tree (gnat_entity);
3917 saved = true;
3920 /* If we are processing a type and there is either no decl for it or
3921 we just made one, do some common processing for the type, such as
3922 handling alignment and possible padding. */
3924 if ((!gnu_decl || this_made_decl) && IN (kind, Type_Kind))
3926 if (Is_Tagged_Type (gnat_entity)
3927 || Is_Class_Wide_Equivalent_Type (gnat_entity))
3928 TYPE_ALIGN_OK (gnu_type) = 1;
3930 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
3931 TYPE_BY_REFERENCE_P (gnu_type) = 1;
3933 /* ??? Don't set the size for a String_Literal since it is either
3934 confirming or we don't handle it properly (if the low bound is
3935 non-constant). */
3936 if (!gnu_size && kind != E_String_Literal_Subtype)
3937 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
3938 TYPE_DECL, false,
3939 Has_Size_Clause (gnat_entity));
3941 /* If a size was specified, see if we can make a new type of that size
3942 by rearranging the type, for example from a fat to a thin pointer. */
3943 if (gnu_size)
3945 gnu_type
3946 = make_type_from_size (gnu_type, gnu_size,
3947 Has_Biased_Representation (gnat_entity));
3949 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
3950 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
3951 gnu_size = 0;
3954 /* If the alignment hasn't already been processed and this is
3955 not an unconstrained array, see if an alignment is specified.
3956 If not, we pick a default alignment for atomic objects. */
3957 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
3959 else if (Known_Alignment (gnat_entity))
3960 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
3961 TYPE_ALIGN (gnu_type));
3962 else if (Is_Atomic (gnat_entity) && !gnu_size
3963 && host_integerp (TYPE_SIZE (gnu_type), 1)
3964 && integer_pow2p (TYPE_SIZE (gnu_type)))
3965 align = MIN (BIGGEST_ALIGNMENT,
3966 tree_low_cst (TYPE_SIZE (gnu_type), 1));
3967 else if (Is_Atomic (gnat_entity) && gnu_size
3968 && host_integerp (gnu_size, 1)
3969 && integer_pow2p (gnu_size))
3970 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
3972 /* See if we need to pad the type. If we did, and made a record,
3973 the name of the new type may be changed. So get it back for
3974 us when we make the new TYPE_DECL below. */
3975 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity, "PAD",
3976 true, definition, false);
3977 if (TREE_CODE (gnu_type) == RECORD_TYPE
3978 && TYPE_IS_PADDING_P (gnu_type))
3980 gnu_entity_id = TYPE_NAME (gnu_type);
3981 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
3982 gnu_entity_id = DECL_NAME (gnu_entity_id);
3985 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
3987 /* If we are at global level, GCC will have applied variable_size to
3988 the type, but that won't have done anything. So, if it's not
3989 a constant or self-referential, call elaborate_expression_1 to
3990 make a variable for the size rather than calculating it each time.
3991 Handle both the RM size and the actual size. */
3992 if (global_bindings_p ()
3993 && TYPE_SIZE (gnu_type)
3994 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
3995 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3997 if (TREE_CODE (gnu_type) == RECORD_TYPE
3998 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
3999 TYPE_SIZE (gnu_type), 0))
4001 TYPE_SIZE (gnu_type)
4002 = elaborate_expression_1 (gnat_entity, gnat_entity,
4003 TYPE_SIZE (gnu_type),
4004 get_identifier ("SIZE"),
4005 definition, 0);
4006 SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
4008 else
4010 TYPE_SIZE (gnu_type)
4011 = elaborate_expression_1 (gnat_entity, gnat_entity,
4012 TYPE_SIZE (gnu_type),
4013 get_identifier ("SIZE"),
4014 definition, 0);
4016 /* ??? For now, store the size as a multiple of the alignment
4017 in bytes so that we can see the alignment from the tree. */
4018 TYPE_SIZE_UNIT (gnu_type)
4019 = build_binary_op
4020 (MULT_EXPR, sizetype,
4021 elaborate_expression_1
4022 (gnat_entity, gnat_entity,
4023 build_binary_op (EXACT_DIV_EXPR, sizetype,
4024 TYPE_SIZE_UNIT (gnu_type),
4025 size_int (TYPE_ALIGN (gnu_type)
4026 / BITS_PER_UNIT)),
4027 get_identifier ("SIZE_A_UNIT"),
4028 definition, 0),
4029 size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4031 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4032 SET_TYPE_ADA_SIZE
4033 (gnu_type,
4034 elaborate_expression_1 (gnat_entity,
4035 gnat_entity,
4036 TYPE_ADA_SIZE (gnu_type),
4037 get_identifier ("RM_SIZE"),
4038 definition, 0));
4042 /* If this is a record type or subtype, call elaborate_expression_1 on
4043 any field position. Do this for both global and local types.
4044 Skip any fields that we haven't made trees for to avoid problems with
4045 class wide types. */
4046 if (IN (kind, Record_Kind))
4047 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4048 gnat_temp = Next_Entity (gnat_temp))
4049 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4051 tree gnu_field = get_gnu_tree (gnat_temp);
4053 /* ??? Unfortunately, GCC needs to be able to prove the
4054 alignment of this offset and if it's a variable, it can't.
4055 In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
4056 right now, we have to put in an explicit multiply and
4057 divide by that value. */
4058 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4059 DECL_FIELD_OFFSET (gnu_field)
4060 = build_binary_op
4061 (MULT_EXPR, sizetype,
4062 elaborate_expression_1
4063 (gnat_temp, gnat_temp,
4064 build_binary_op (EXACT_DIV_EXPR, sizetype,
4065 DECL_FIELD_OFFSET (gnu_field),
4066 size_int (DECL_OFFSET_ALIGN (gnu_field)
4067 / BITS_PER_UNIT)),
4068 get_identifier ("OFFSET"),
4069 definition, 0),
4070 size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
4073 gnu_type = build_qualified_type (gnu_type,
4074 (TYPE_QUALS (gnu_type)
4075 | (TYPE_QUAL_VOLATILE
4076 * Treat_As_Volatile (gnat_entity))));
4078 if (Is_Atomic (gnat_entity))
4079 check_ok_for_atomic (gnu_type, gnat_entity, false);
4081 if (Known_Alignment (gnat_entity))
4082 TYPE_USER_ALIGN (gnu_type) = 1;
4084 if (!gnu_decl)
4085 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4086 !Comes_From_Source (gnat_entity),
4087 debug_info_p, gnat_entity);
4088 else
4089 TREE_TYPE (gnu_decl) = gnu_type;
4092 if (IN (kind, Type_Kind) && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4094 gnu_type = TREE_TYPE (gnu_decl);
4096 /* Back-annotate the Alignment of the type if not already in the
4097 tree. Likewise for sizes. */
4098 if (Unknown_Alignment (gnat_entity))
4099 Set_Alignment (gnat_entity,
4100 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4102 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4104 /* If the size is self-referential, we annotate the maximum
4105 value of that size. */
4106 tree gnu_size = TYPE_SIZE (gnu_type);
4108 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4109 gnu_size = max_size (gnu_size, true);
4111 Set_Esize (gnat_entity, annotate_value (gnu_size));
4113 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4115 /* In this mode the tag and the parent components are not
4116 generated by the front-end, so the sizes must be adjusted
4117 explicitly now. */
4119 int size_offset;
4120 int new_size;
4122 if (Is_Derived_Type (gnat_entity))
4124 size_offset
4125 = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
4126 Set_Alignment (gnat_entity,
4127 Alignment (Etype (Base_Type (gnat_entity))));
4129 else
4130 size_offset = POINTER_SIZE;
4132 new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
4133 Set_Esize (gnat_entity,
4134 UI_From_Int (((new_size + (POINTER_SIZE - 1))
4135 / POINTER_SIZE) * POINTER_SIZE));
4136 Set_RM_Size (gnat_entity, Esize (gnat_entity));
4140 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4141 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4144 if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4145 DECL_ARTIFICIAL (gnu_decl) = 1;
4147 if (!debug_info_p && DECL_P (gnu_decl)
4148 && TREE_CODE (gnu_decl) != FUNCTION_DECL
4149 && No (Renamed_Object (gnat_entity)))
4150 DECL_IGNORED_P (gnu_decl) = 1;
4152 /* If we haven't already, associate the ..._DECL node that we just made with
4153 the input GNAT entity node. */
4154 if (!saved)
4155 save_gnu_tree (gnat_entity, gnu_decl, false);
4157 /* If this is an enumeral or floating-point type, we were not able to set
4158 the bounds since they refer to the type. These bounds are always static.
4160 For enumeration types, also write debugging information and declare the
4161 enumeration literal table, if needed. */
4163 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4164 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4166 tree gnu_scalar_type = gnu_type;
4168 /* If this is a padded type, we need to use the underlying type. */
4169 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
4170 && TYPE_IS_PADDING_P (gnu_scalar_type))
4171 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4173 /* If this is a floating point type and we haven't set a floating
4174 point type yet, use this in the evaluation of the bounds. */
4175 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4176 longest_float_type_node = gnu_type;
4178 TYPE_MIN_VALUE (gnu_scalar_type)
4179 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4180 TYPE_MAX_VALUE (gnu_scalar_type)
4181 = gnat_to_gnu (Type_High_Bound (gnat_entity));
4183 if (TREE_CODE (gnu_scalar_type) == ENUMERAL_TYPE)
4185 TYPE_STUB_DECL (gnu_scalar_type) = gnu_decl;
4187 /* Since this has both a typedef and a tag, avoid outputting
4188 the name twice. */
4189 DECL_ARTIFICIAL (gnu_decl) = 1;
4190 rest_of_type_compilation (gnu_scalar_type, global_bindings_p ());
4194 /* If we deferred processing of incomplete types, re-enable it. If there
4195 were no other disables and we have some to process, do so. */
4196 if (this_deferred && --defer_incomplete_level == 0 && defer_incomplete_list)
4198 struct incomplete *incp = defer_incomplete_list;
4199 struct incomplete *next;
4201 defer_incomplete_list = NULL;
4202 for (; incp; incp = next)
4204 next = incp->next;
4206 if (incp->old_type)
4207 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4208 gnat_to_gnu_type (incp->full_type));
4209 free (incp);
4213 /* If we are not defining this type, see if it's in the incomplete list.
4214 If so, handle that list entry now. */
4215 else if (!definition)
4217 struct incomplete *incp;
4219 for (incp = defer_incomplete_list; incp; incp = incp->next)
4220 if (incp->old_type && incp->full_type == gnat_entity)
4222 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4223 TREE_TYPE (gnu_decl));
4224 incp->old_type = NULL_TREE;
4228 /* If there are no incomplete types and we have deferred emission
4229 of debug information, check whether we have finished defining
4230 all nested records.
4231 If so, handle the list now. */
4233 if (debug_deferred)
4234 defer_debug_level--;
4236 if (defer_debug_incomplete_list
4237 && !defer_incomplete_level
4238 && !defer_debug_level)
4240 tree c, n;
4242 defer_debug_incomplete_list = nreverse (defer_debug_incomplete_list);
4244 for (c = defer_debug_incomplete_list; c; c = n)
4246 n = TREE_CHAIN (c);
4247 write_record_type_debug_info (TREE_VALUE (c));
4250 defer_debug_incomplete_list = 0;
4253 if (this_global)
4254 force_global--;
4256 if (Is_Packed_Array_Type (gnat_entity)
4257 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
4258 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
4259 && !present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
4260 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
4262 return gnu_decl;
4265 /* Similar, but if the returned value is a COMPONENT_REF, return the
4266 FIELD_DECL. */
4268 tree
4269 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4271 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4273 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4274 gnu_field = TREE_OPERAND (gnu_field, 1);
4276 return gnu_field;
4279 /* Given GNAT_ENTITY, elaborate all expressions that are required to
4280 be elaborated at the point of its definition, but do nothing else. */
4282 void
4283 elaborate_entity (Entity_Id gnat_entity)
4285 switch (Ekind (gnat_entity))
4287 case E_Signed_Integer_Subtype:
4288 case E_Modular_Integer_Subtype:
4289 case E_Enumeration_Subtype:
4290 case E_Ordinary_Fixed_Point_Subtype:
4291 case E_Decimal_Fixed_Point_Subtype:
4292 case E_Floating_Point_Subtype:
4294 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
4295 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
4297 /* ??? Tests for avoiding static constraint error expression
4298 is needed until the front stops generating bogus conversions
4299 on bounds of real types. */
4301 if (!Raises_Constraint_Error (gnat_lb))
4302 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
4303 1, 0, Needs_Debug_Info (gnat_entity));
4304 if (!Raises_Constraint_Error (gnat_hb))
4305 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
4306 1, 0, Needs_Debug_Info (gnat_entity));
4307 break;
4310 case E_Record_Type:
4312 Node_Id full_definition = Declaration_Node (gnat_entity);
4313 Node_Id record_definition = Type_Definition (full_definition);
4315 /* If this is a record extension, go a level further to find the
4316 record definition. */
4317 if (Nkind (record_definition) == N_Derived_Type_Definition)
4318 record_definition = Record_Extension_Part (record_definition);
4320 break;
4322 case E_Record_Subtype:
4323 case E_Private_Subtype:
4324 case E_Limited_Private_Subtype:
4325 case E_Record_Subtype_With_Private:
4326 if (Is_Constrained (gnat_entity)
4327 && Has_Discriminants (Base_Type (gnat_entity))
4328 && Present (Discriminant_Constraint (gnat_entity)))
4330 Node_Id gnat_discriminant_expr;
4331 Entity_Id gnat_field;
4333 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
4334 gnat_discriminant_expr
4335 = First_Elmt (Discriminant_Constraint (gnat_entity));
4336 Present (gnat_field);
4337 gnat_field = Next_Discriminant (gnat_field),
4338 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
4339 /* ??? For now, ignore access discriminants. */
4340 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
4341 elaborate_expression (Node (gnat_discriminant_expr),
4342 gnat_entity,
4343 get_entity_name (gnat_field), 1, 0, 0);
4345 break;
4350 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
4351 any entities on its entity chain similarly. */
4353 void
4354 mark_out_of_scope (Entity_Id gnat_entity)
4356 Entity_Id gnat_sub_entity;
4357 unsigned int kind = Ekind (gnat_entity);
4359 /* If this has an entity list, process all in the list. */
4360 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
4361 || IN (kind, Private_Kind)
4362 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
4363 || kind == E_Function || kind == E_Generic_Function
4364 || kind == E_Generic_Package || kind == E_Generic_Procedure
4365 || kind == E_Loop || kind == E_Operator || kind == E_Package
4366 || kind == E_Package_Body || kind == E_Procedure
4367 || kind == E_Record_Type || kind == E_Record_Subtype
4368 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
4369 for (gnat_sub_entity = First_Entity (gnat_entity);
4370 Present (gnat_sub_entity);
4371 gnat_sub_entity = Next_Entity (gnat_sub_entity))
4372 if (Scope (gnat_sub_entity) == gnat_entity
4373 && gnat_sub_entity != gnat_entity)
4374 mark_out_of_scope (gnat_sub_entity);
4376 /* Now clear this if it has been defined, but only do so if it isn't
4377 a subprogram or parameter. We could refine this, but it isn't
4378 worth it. If this is statically allocated, it is supposed to
4379 hang around out of cope. */
4380 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
4381 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
4383 save_gnu_tree (gnat_entity, NULL_TREE, true);
4384 save_gnu_tree (gnat_entity, error_mark_node, true);
4388 /* Set the alias set of GNU_NEW_TYPE to be that of GNU_OLD_TYPE. If this
4389 is a multi-dimensional array type, do this recursively. */
4391 static void
4392 copy_alias_set (tree gnu_new_type, tree gnu_old_type)
4394 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
4395 of a one-dimensional array, since the padding has the same alias set
4396 as the field type, but if it's a multi-dimensional array, we need to
4397 see the inner types. */
4398 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
4399 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
4400 || TYPE_IS_PADDING_P (gnu_old_type)))
4401 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
4403 /* We need to be careful here in case GNU_OLD_TYPE is an unconstrained
4404 array. In that case, it doesn't have the same shape as GNU_NEW_TYPE,
4405 so we need to go down to what does. */
4406 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
4407 gnu_old_type
4408 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
4410 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
4411 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
4412 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
4413 copy_alias_set (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type));
4415 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
4416 record_component_aliases (gnu_new_type);
4419 /* Return a TREE_LIST describing the substitutions needed to reflect
4420 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
4421 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
4422 of GNAT_SUBTYPE. The substitutions can be in any order. TREE_PURPOSE
4423 gives the tree for the discriminant and TREE_VALUES is the replacement
4424 value. They are in the form of operands to substitute_in_expr.
4425 DEFINITION is as in gnat_to_gnu_entity. */
4427 static tree
4428 substitution_list (Entity_Id gnat_subtype, Entity_Id gnat_type,
4429 tree gnu_list, bool definition)
4431 Entity_Id gnat_discrim;
4432 Node_Id gnat_value;
4434 if (No (gnat_type))
4435 gnat_type = Implementation_Base_Type (gnat_subtype);
4437 if (Has_Discriminants (gnat_type))
4438 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
4439 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
4440 Present (gnat_discrim);
4441 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
4442 gnat_value = Next_Elmt (gnat_value))
4443 /* Ignore access discriminants. */
4444 if (!Is_Access_Type (Etype (Node (gnat_value))))
4445 gnu_list = tree_cons (gnat_to_gnu_field_decl (gnat_discrim),
4446 elaborate_expression
4447 (Node (gnat_value), gnat_subtype,
4448 get_entity_name (gnat_discrim), definition,
4449 1, 0),
4450 gnu_list);
4452 return gnu_list;
4455 /* For the following two functions: for each GNAT entity, the GCC
4456 tree node used as a dummy for that entity, if any. */
4458 static GTY((length ("max_gnat_nodes"))) tree * dummy_node_table;
4460 /* Initialize the above table. */
4462 void
4463 init_dummy_type (void)
4465 Node_Id gnat_node;
4467 dummy_node_table = (tree *) ggc_alloc (max_gnat_nodes * sizeof (tree));
4469 for (gnat_node = 0; gnat_node < max_gnat_nodes; gnat_node++)
4470 dummy_node_table[gnat_node] = NULL_TREE;
4472 dummy_node_table -= First_Node_Id;
4475 /* Make a dummy type corresponding to GNAT_TYPE. */
4477 tree
4478 make_dummy_type (Entity_Id gnat_type)
4480 Entity_Id gnat_underlying;
4481 tree gnu_type;
4483 /* Find a full type for GNAT_TYPE, taking into account any class wide
4484 types. */
4485 if (Is_Class_Wide_Type (gnat_type) && Present (Equivalent_Type (gnat_type)))
4486 gnat_type = Equivalent_Type (gnat_type);
4487 else if (Ekind (gnat_type) == E_Class_Wide_Type)
4488 gnat_type = Root_Type (gnat_type);
4490 for (gnat_underlying = gnat_type;
4491 (IN (Ekind (gnat_underlying), Incomplete_Or_Private_Kind)
4492 && Present (Full_View (gnat_underlying)));
4493 gnat_underlying = Full_View (gnat_underlying))
4496 /* If it there already a dummy type, use that one. Else make one. */
4497 if (dummy_node_table[gnat_underlying])
4498 return dummy_node_table[gnat_underlying];
4500 /* If this is a record, make this a RECORD_TYPE or UNION_TYPE; else make
4501 it a VOID_TYPE. */
4502 if (Is_Unchecked_Union (gnat_underlying))
4504 gnu_type = make_node (UNION_TYPE);
4505 TYPE_UNCHECKED_UNION_P (gnu_type) = 1;
4507 else if (Is_Record_Type (gnat_underlying))
4508 gnu_type = make_node (RECORD_TYPE);
4509 else
4510 gnu_type = make_node (ENUMERAL_TYPE);
4512 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
4513 TYPE_DUMMY_P (gnu_type) = 1;
4514 if (AGGREGATE_TYPE_P (gnu_type))
4515 TYPE_STUB_DECL (gnu_type) = build_decl (TYPE_DECL, NULL_TREE, gnu_type);
4517 dummy_node_table[gnat_underlying] = gnu_type;
4519 return gnu_type;
4522 /* Return true if the size represented by GNU_SIZE can be handled by an
4523 allocation. If STATIC_P is true, consider only what can be done with a
4524 static allocation. */
4526 static bool
4527 allocatable_size_p (tree gnu_size, bool static_p)
4529 HOST_WIDE_INT our_size;
4531 /* If this is not a static allocation, the only case we want to forbid
4532 is an overflowing size. That will be converted into a raise a
4533 Storage_Error. */
4534 if (!static_p)
4535 return !(TREE_CODE (gnu_size) == INTEGER_CST
4536 && TREE_CONSTANT_OVERFLOW (gnu_size));
4538 /* Otherwise, we need to deal with both variable sizes and constant
4539 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
4540 since assemblers may not like very large sizes. */
4541 if (!host_integerp (gnu_size, 1))
4542 return false;
4544 our_size = tree_low_cst (gnu_size, 1);
4545 return (int) our_size == our_size;
4548 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
4550 static void
4551 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
4553 Node_Id gnat_temp;
4555 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
4556 gnat_temp = Next_Rep_Item (gnat_temp))
4557 if (Nkind (gnat_temp) == N_Pragma)
4559 struct attrib *attr;
4560 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
4561 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
4562 enum attr_type etype;
4564 if (Present (gnat_assoc) && Present (First (gnat_assoc))
4565 && Present (Next (First (gnat_assoc)))
4566 && (Nkind (Expression (Next (First (gnat_assoc))))
4567 == N_String_Literal))
4569 gnu_arg0 = get_identifier (TREE_STRING_POINTER
4570 (gnat_to_gnu
4571 (Expression (Next
4572 (First (gnat_assoc))))));
4573 if (Present (Next (Next (First (gnat_assoc))))
4574 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
4575 == N_String_Literal))
4576 gnu_arg1 = get_identifier (TREE_STRING_POINTER
4577 (gnat_to_gnu
4578 (Expression
4579 (Next (Next
4580 (First (gnat_assoc)))))));
4583 switch (Get_Pragma_Id (Chars (gnat_temp)))
4585 case Pragma_Machine_Attribute:
4586 etype = ATTR_MACHINE_ATTRIBUTE;
4587 break;
4589 case Pragma_Linker_Alias:
4590 etype = ATTR_LINK_ALIAS;
4591 break;
4593 case Pragma_Linker_Section:
4594 etype = ATTR_LINK_SECTION;
4595 break;
4597 case Pragma_Linker_Constructor:
4598 etype = ATTR_LINK_CONSTRUCTOR;
4599 break;
4601 case Pragma_Linker_Destructor:
4602 etype = ATTR_LINK_DESTRUCTOR;
4603 break;
4605 case Pragma_Weak_External:
4606 etype = ATTR_WEAK_EXTERNAL;
4607 break;
4609 default:
4610 continue;
4613 attr = (struct attrib *) xmalloc (sizeof (struct attrib));
4614 attr->next = *attr_list;
4615 attr->type = etype;
4616 attr->name = gnu_arg0;
4618 /* If we have an argument specified together with an attribute name,
4619 make it a single TREE_VALUE entry in a list of arguments, as GCC
4620 expects it. */
4621 if (gnu_arg1 != NULL_TREE)
4622 attr->args = build_tree_list (NULL_TREE, gnu_arg1);
4623 else
4624 attr->args = NULL_TREE;
4626 attr->error_point
4627 = Present (Next (First (gnat_assoc)))
4628 ? Expression (Next (First (gnat_assoc))) : gnat_temp;
4629 *attr_list = attr;
4633 /* Get the unpadded version of a GNAT type. */
4635 tree
4636 get_unpadded_type (Entity_Id gnat_entity)
4638 tree type = gnat_to_gnu_type (gnat_entity);
4640 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4641 type = TREE_TYPE (TYPE_FIELDS (type));
4643 return type;
4646 /* Called when we need to protect a variable object using a save_expr. */
4648 tree
4649 maybe_variable (tree gnu_operand)
4651 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
4652 || TREE_CODE (gnu_operand) == SAVE_EXPR
4653 || TREE_CODE (gnu_operand) == NULL_EXPR)
4654 return gnu_operand;
4656 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
4658 tree gnu_result = build1 (UNCONSTRAINED_ARRAY_REF,
4659 TREE_TYPE (gnu_operand),
4660 variable_size (TREE_OPERAND (gnu_operand, 0)));
4662 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result)
4663 = TYPE_READONLY (TREE_TYPE (TREE_TYPE (gnu_operand)));
4664 return gnu_result;
4666 else
4667 return variable_size (gnu_operand);
4670 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
4671 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
4672 return the GCC tree to use for that expression. GNU_NAME is the
4673 qualification to use if an external name is appropriate and DEFINITION is
4674 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
4675 we need a result. Otherwise, we are just elaborating this for
4676 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
4677 purposes even if it isn't needed for code generation. */
4679 static tree
4680 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity,
4681 tree gnu_name, bool definition, bool need_value,
4682 bool need_debug)
4684 tree gnu_expr;
4686 /* If we already elaborated this expression (e.g., it was involved
4687 in the definition of a private type), use the old value. */
4688 if (present_gnu_tree (gnat_expr))
4689 return get_gnu_tree (gnat_expr);
4691 /* If we don't need a value and this is static or a discriment, we
4692 don't need to do anything. */
4693 else if (!need_value
4694 && (Is_OK_Static_Expression (gnat_expr)
4695 || (Nkind (gnat_expr) == N_Identifier
4696 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
4697 return 0;
4699 /* Otherwise, convert this tree to its GCC equivalent. */
4700 gnu_expr
4701 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
4702 gnu_name, definition, need_debug);
4704 /* Save the expression in case we try to elaborate this entity again. Since
4705 this is not a DECL, don't check it. Don't save if it's a discriminant. */
4706 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
4707 save_gnu_tree (gnat_expr, gnu_expr, true);
4709 return need_value ? gnu_expr : error_mark_node;
4712 /* Similar, but take a GNU expression. */
4714 static tree
4715 elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
4716 tree gnu_expr, tree gnu_name, bool definition,
4717 bool need_debug)
4719 tree gnu_decl = NULL_TREE;
4720 /* Strip any conversions to see if the expression is a readonly variable.
4721 ??? This really should remain readonly, but we have to think about
4722 the typing of the tree here. */
4723 tree gnu_inner_expr = remove_conversions (gnu_expr, true);
4724 bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
4725 bool expr_variable;
4727 /* In most cases, we won't see a naked FIELD_DECL here because a
4728 discriminant reference will have been replaced with a COMPONENT_REF
4729 when the type is being elaborated. However, there are some cases
4730 involving child types where we will. So convert it to a COMPONENT_REF
4731 here. We have to hope it will be at the highest level of the
4732 expression in these cases. */
4733 if (TREE_CODE (gnu_expr) == FIELD_DECL)
4734 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
4735 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
4736 gnu_expr, NULL_TREE);
4738 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
4739 that is a constant, make a variable that is initialized to contain the
4740 bound when the package containing the definition is elaborated. If
4741 this entity is defined at top level and a bound or discriminant value
4742 isn't a constant or a reference to a discriminant, replace the bound
4743 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
4744 rely here on the fact that an expression cannot contain both the
4745 discriminant and some other variable. */
4747 expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
4748 && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
4749 && TREE_READONLY (gnu_inner_expr))
4750 && !CONTAINS_PLACEHOLDER_P (gnu_expr));
4752 /* If this is a static expression or contains a discriminant, we don't
4753 need the variable for debugging (and can't elaborate anyway if a
4754 discriminant). */
4755 if (need_debug
4756 && (Is_OK_Static_Expression (gnat_expr)
4757 || CONTAINS_PLACEHOLDER_P (gnu_expr)))
4758 need_debug = false;
4760 /* Now create the variable if we need it. */
4761 if (need_debug || (expr_variable && expr_global))
4762 gnu_decl
4763 = create_var_decl (create_concat_name (gnat_entity,
4764 IDENTIFIER_POINTER (gnu_name)),
4765 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
4766 !need_debug, Is_Public (gnat_entity),
4767 !definition, false, NULL, gnat_entity);
4769 /* We only need to use this variable if we are in global context since GCC
4770 can do the right thing in the local case. */
4771 if (expr_global && expr_variable)
4772 return gnu_decl;
4773 else if (!expr_variable)
4774 return gnu_expr;
4775 else
4776 return maybe_variable (gnu_expr);
4779 /* Create a record type that contains a field of TYPE with a starting bit
4780 position so that it is aligned to ALIGN bits and is SIZE bytes long. */
4782 tree
4783 make_aligning_type (tree type, int align, tree size)
4785 tree record_type = make_node (RECORD_TYPE);
4786 tree place = build0 (PLACEHOLDER_EXPR, record_type);
4787 tree size_addr_place = convert (sizetype,
4788 build_unary_op (ADDR_EXPR, NULL_TREE,
4789 place));
4790 tree name = TYPE_NAME (type);
4791 tree pos, field;
4793 if (TREE_CODE (name) == TYPE_DECL)
4794 name = DECL_NAME (name);
4796 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
4798 /* The bit position is obtained by "and"ing the alignment minus 1
4799 with the two's complement of the address and multiplying
4800 by the number of bits per unit. Do all this in sizetype. */
4801 pos = size_binop (MULT_EXPR,
4802 convert (bitsizetype,
4803 size_binop (BIT_AND_EXPR,
4804 size_diffop (size_zero_node,
4805 size_addr_place),
4806 ssize_int ((align / BITS_PER_UNIT)
4807 - 1))),
4808 bitsize_unit_node);
4810 /* Create the field, with -1 as the 'addressable' indication to avoid the
4811 creation of a bitfield. We don't need one, it would have damaging
4812 consequences on the alignment computation, and create_field_decl would
4813 make one without this special argument, for instance because of the
4814 complex position expression. */
4815 field = create_field_decl (get_identifier ("F"), type, record_type, 1, size,
4816 pos, -1);
4818 finish_record_type (record_type, field, true, false);
4819 TYPE_ALIGN (record_type) = BIGGEST_ALIGNMENT;
4820 TYPE_SIZE (record_type)
4821 = size_binop (PLUS_EXPR,
4822 size_binop (MULT_EXPR, convert (bitsizetype, size),
4823 bitsize_unit_node),
4824 bitsize_int (align));
4825 TYPE_SIZE_UNIT (record_type)
4826 = size_binop (PLUS_EXPR, size, size_int (align / BITS_PER_UNIT));
4827 copy_alias_set (record_type, type);
4828 return record_type;
4831 /* TYPE is a RECORD_TYPE, UNION_TYPE, or QUAL_UNION_TYPE, with BLKmode that's
4832 being used as the field type of a packed record. See if we can rewrite it
4833 as a record that has a non-BLKmode type, which we can pack tighter. If so,
4834 return the new type. If not, return the original type. */
4836 static tree
4837 make_packable_type (tree type)
4839 tree new_type = make_node (TREE_CODE (type));
4840 tree field_list = NULL_TREE;
4841 tree old_field;
4843 /* Copy the name and flags from the old type to that of the new and set
4844 the alignment to try for an integral type. For QUAL_UNION_TYPE,
4845 also copy the size. */
4846 TYPE_NAME (new_type) = TYPE_NAME (type);
4847 TYPE_JUSTIFIED_MODULAR_P (new_type)
4848 = TYPE_JUSTIFIED_MODULAR_P (type);
4849 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
4851 if (TREE_CODE (type) == RECORD_TYPE)
4852 TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
4853 else if (TREE_CODE (type) == QUAL_UNION_TYPE)
4855 TYPE_SIZE (new_type) = TYPE_SIZE (type);
4856 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
4859 TYPE_ALIGN (new_type)
4860 = ((HOST_WIDE_INT) 1
4861 << (floor_log2 (tree_low_cst (TYPE_SIZE (type), 1) - 1) + 1));
4863 /* Now copy the fields, keeping the position and size. */
4864 for (old_field = TYPE_FIELDS (type); old_field;
4865 old_field = TREE_CHAIN (old_field))
4867 tree new_field_type = TREE_TYPE (old_field);
4868 tree new_field;
4870 if (TYPE_MODE (new_field_type) == BLKmode
4871 && (TREE_CODE (new_field_type) == RECORD_TYPE
4872 || TREE_CODE (new_field_type) == UNION_TYPE
4873 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
4874 && host_integerp (TYPE_SIZE (new_field_type), 1))
4875 new_field_type = make_packable_type (new_field_type);
4877 new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
4878 new_type, TYPE_PACKED (type),
4879 DECL_SIZE (old_field),
4880 bit_position (old_field),
4881 !DECL_NONADDRESSABLE_P (old_field));
4883 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
4884 SET_DECL_ORIGINAL_FIELD
4885 (new_field, (DECL_ORIGINAL_FIELD (old_field)
4886 ? DECL_ORIGINAL_FIELD (old_field) : old_field));
4888 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
4889 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
4891 TREE_CHAIN (new_field) = field_list;
4892 field_list = new_field;
4895 finish_record_type (new_type, nreverse (field_list), true, true);
4896 copy_alias_set (new_type, type);
4897 return TYPE_MODE (new_type) == BLKmode ? type : new_type;
4900 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
4901 if needed. We have already verified that SIZE and TYPE are large enough.
4903 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
4904 to issue a warning.
4906 IS_USER_TYPE is true if we must be sure we complete the original type.
4908 DEFINITION is true if this type is being defined.
4910 SAME_RM_SIZE is true if the RM_Size of the resulting type is to be
4911 set to its TYPE_SIZE; otherwise, it's set to the RM_Size of the original
4912 type. */
4914 tree
4915 maybe_pad_type (tree type, tree size, unsigned int align,
4916 Entity_Id gnat_entity, const char *name_trailer,
4917 bool is_user_type, bool definition, bool same_rm_size)
4919 tree orig_size = TYPE_SIZE (type);
4920 tree record;
4921 tree field;
4923 /* If TYPE is a padded type, see if it agrees with any size and alignment
4924 we were given. If so, return the original type. Otherwise, strip
4925 off the padding, since we will either be returning the inner type
4926 or repadding it. If no size or alignment is specified, use that of
4927 the original padded type. */
4929 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4931 if ((!size
4932 || operand_equal_p (round_up (size,
4933 MAX (align, TYPE_ALIGN (type))),
4934 round_up (TYPE_SIZE (type),
4935 MAX (align, TYPE_ALIGN (type))),
4937 && (align == 0 || align == TYPE_ALIGN (type)))
4938 return type;
4940 if (!size)
4941 size = TYPE_SIZE (type);
4942 if (align == 0)
4943 align = TYPE_ALIGN (type);
4945 type = TREE_TYPE (TYPE_FIELDS (type));
4946 orig_size = TYPE_SIZE (type);
4949 /* If the size is either not being changed or is being made smaller (which
4950 is not done here (and is only valid for bitfields anyway), show the size
4951 isn't changing. Likewise, clear the alignment if it isn't being
4952 changed. Then return if we aren't doing anything. */
4954 if (size
4955 && (operand_equal_p (size, orig_size, 0)
4956 || (TREE_CODE (orig_size) == INTEGER_CST
4957 && tree_int_cst_lt (size, orig_size))))
4958 size = NULL_TREE;
4960 if (align == TYPE_ALIGN (type))
4961 align = 0;
4963 if (align == 0 && !size)
4964 return type;
4966 /* We used to modify the record in place in some cases, but that could
4967 generate incorrect debugging information. So make a new record
4968 type and name. */
4969 record = make_node (RECORD_TYPE);
4971 if (Present (gnat_entity))
4972 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
4974 /* If we were making a type, complete the original type and give it a
4975 name. */
4976 if (is_user_type)
4977 create_type_decl (get_entity_name (gnat_entity), type,
4978 NULL, !Comes_From_Source (gnat_entity),
4979 !(TYPE_NAME (type)
4980 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4981 && DECL_IGNORED_P (TYPE_NAME (type))),
4982 gnat_entity);
4984 /* If we are changing the alignment and the input type is a record with
4985 BLKmode and a small constant size, try to make a form that has an
4986 integral mode. That might allow this record to have an integral mode,
4987 which will be much more efficient. There is no point in doing this if a
4988 size is specified unless it is also smaller than the biggest alignment
4989 and it is incorrect to do this if the size of the original type is not a
4990 multiple of the alignment. */
4991 if (align != 0
4992 && TREE_CODE (type) == RECORD_TYPE
4993 && TYPE_MODE (type) == BLKmode
4994 && host_integerp (orig_size, 1)
4995 && compare_tree_int (orig_size, BIGGEST_ALIGNMENT) <= 0
4996 && (!size
4997 || (TREE_CODE (size) == INTEGER_CST
4998 && compare_tree_int (size, BIGGEST_ALIGNMENT) <= 0))
4999 && tree_low_cst (orig_size, 1) % align == 0)
5000 type = make_packable_type (type);
5002 field = create_field_decl (get_identifier ("F"), type, record, 0,
5003 NULL_TREE, bitsize_zero_node, 1);
5005 DECL_INTERNAL_P (field) = 1;
5006 TYPE_SIZE (record) = size ? size : orig_size;
5007 TYPE_SIZE_UNIT (record)
5008 = (size ? convert (sizetype,
5009 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node))
5010 : TYPE_SIZE_UNIT (type));
5012 TYPE_ALIGN (record) = align;
5013 TYPE_IS_PADDING_P (record) = 1;
5014 TYPE_VOLATILE (record)
5015 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
5016 finish_record_type (record, field, true, false);
5018 /* Keep the RM_Size of the padded record as that of the old record
5019 if requested. */
5020 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : rm_size (type));
5022 /* Unless debugging information isn't being written for the input type,
5023 write a record that shows what we are a subtype of and also make a
5024 variable that indicates our size, if variable. */
5025 if (TYPE_NAME (record) && AGGREGATE_TYPE_P (type)
5026 && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
5027 || !DECL_IGNORED_P (TYPE_NAME (type))))
5029 tree marker = make_node (RECORD_TYPE);
5030 tree name = (TREE_CODE (TYPE_NAME (record)) == TYPE_DECL
5031 ? DECL_NAME (TYPE_NAME (record))
5032 : TYPE_NAME (record));
5033 tree orig_name = TYPE_NAME (type);
5035 if (TREE_CODE (orig_name) == TYPE_DECL)
5036 orig_name = DECL_NAME (orig_name);
5038 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
5039 finish_record_type (marker,
5040 create_field_decl (orig_name, integer_type_node,
5041 marker, 0, NULL_TREE, NULL_TREE,
5043 false, false);
5045 if (size && TREE_CODE (size) != INTEGER_CST && definition)
5046 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
5047 bitsizetype, TYPE_SIZE (record), false, false, false,
5048 false, NULL, gnat_entity);
5051 type = record;
5053 if (CONTAINS_PLACEHOLDER_P (orig_size))
5054 orig_size = max_size (orig_size, true);
5056 /* If the size was widened explicitly, maybe give a warning. */
5057 if (size && Present (gnat_entity)
5058 && !operand_equal_p (size, orig_size, 0)
5059 && !(TREE_CODE (size) == INTEGER_CST
5060 && TREE_CODE (orig_size) == INTEGER_CST
5061 && tree_int_cst_lt (size, orig_size)))
5063 Node_Id gnat_error_node = Empty;
5065 if (Is_Packed_Array_Type (gnat_entity))
5066 gnat_entity = Associated_Node_For_Itype (gnat_entity);
5068 if ((Ekind (gnat_entity) == E_Component
5069 || Ekind (gnat_entity) == E_Discriminant)
5070 && Present (Component_Clause (gnat_entity)))
5071 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
5072 else if (Present (Size_Clause (gnat_entity)))
5073 gnat_error_node = Expression (Size_Clause (gnat_entity));
5075 /* Generate message only for entities that come from source, since
5076 if we have an entity created by expansion, the message will be
5077 generated for some other corresponding source entity. */
5078 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
5079 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
5080 gnat_entity,
5081 size_diffop (size, orig_size));
5083 else if (*name_trailer == 'C' && !Is_Internal (gnat_entity))
5084 post_error_ne_tree ("component of& padded{ by ^ bits}?",
5085 gnat_entity, gnat_entity,
5086 size_diffop (size, orig_size));
5089 return type;
5092 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
5093 the value passed against the list of choices. */
5095 tree
5096 choices_to_gnu (tree operand, Node_Id choices)
5098 Node_Id choice;
5099 Node_Id gnat_temp;
5100 tree result = integer_zero_node;
5101 tree this_test, low = 0, high = 0, single = 0;
5103 for (choice = First (choices); Present (choice); choice = Next (choice))
5105 switch (Nkind (choice))
5107 case N_Range:
5108 low = gnat_to_gnu (Low_Bound (choice));
5109 high = gnat_to_gnu (High_Bound (choice));
5111 /* There's no good type to use here, so we might as well use
5112 integer_type_node. */
5113 this_test
5114 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5115 build_binary_op (GE_EXPR, integer_type_node,
5116 operand, low),
5117 build_binary_op (LE_EXPR, integer_type_node,
5118 operand, high));
5120 break;
5122 case N_Subtype_Indication:
5123 gnat_temp = Range_Expression (Constraint (choice));
5124 low = gnat_to_gnu (Low_Bound (gnat_temp));
5125 high = gnat_to_gnu (High_Bound (gnat_temp));
5127 this_test
5128 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5129 build_binary_op (GE_EXPR, integer_type_node,
5130 operand, low),
5131 build_binary_op (LE_EXPR, integer_type_node,
5132 operand, high));
5133 break;
5135 case N_Identifier:
5136 case N_Expanded_Name:
5137 /* This represents either a subtype range, an enumeration
5138 literal, or a constant Ekind says which. If an enumeration
5139 literal or constant, fall through to the next case. */
5140 if (Ekind (Entity (choice)) != E_Enumeration_Literal
5141 && Ekind (Entity (choice)) != E_Constant)
5143 tree type = gnat_to_gnu_type (Entity (choice));
5145 low = TYPE_MIN_VALUE (type);
5146 high = TYPE_MAX_VALUE (type);
5148 this_test
5149 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5150 build_binary_op (GE_EXPR, integer_type_node,
5151 operand, low),
5152 build_binary_op (LE_EXPR, integer_type_node,
5153 operand, high));
5154 break;
5156 /* ... fall through ... */
5157 case N_Character_Literal:
5158 case N_Integer_Literal:
5159 single = gnat_to_gnu (choice);
5160 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
5161 single);
5162 break;
5164 case N_Others_Choice:
5165 this_test = integer_one_node;
5166 break;
5168 default:
5169 gcc_unreachable ();
5172 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
5173 result, this_test);
5176 return result;
5179 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
5180 placed in GNU_RECORD_TYPE.
5182 PACKED is 1 if the enclosing record is packed and -1 if the enclosing
5183 record has a Component_Alignment of Storage_Unit.
5185 DEFINITION is true if this field is for a record being defined. */
5187 static tree
5188 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
5189 bool definition)
5191 tree gnu_field_id = get_entity_name (gnat_field);
5192 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
5193 tree gnu_pos = 0;
5194 tree gnu_size = 0;
5195 tree gnu_field;
5196 bool needs_strict_alignment
5197 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
5198 || Treat_As_Volatile (gnat_field));
5200 /* If this field requires strict alignment or contains an item of
5201 variable sized, pretend it isn't packed. */
5202 if (needs_strict_alignment || is_variable_size (gnu_field_type))
5203 packed = 0;
5205 /* For packed records, this is one of the few occasions on which we use
5206 the official RM size for discrete or fixed-point components, instead
5207 of the normal GNAT size stored in Esize. See description in Einfo:
5208 "Handling of Type'Size Values" for further details. */
5210 if (packed == 1)
5211 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
5212 gnat_field, FIELD_DECL, false, true);
5214 if (Known_Static_Esize (gnat_field))
5215 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5216 gnat_field, FIELD_DECL, false, true);
5218 /* If we are packing this record, have a specified size that's smaller than
5219 that of the field type, or a position is specified, and the field type is
5220 also a record that's BLKmode and with a small constant size, see if we
5221 can get a better form of the type that allows more packing. If we can,
5222 show a size was specified for it if there wasn't one so we know to make
5223 this a bitfield and avoid making things wider. */
5224 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5225 && TYPE_MODE (gnu_field_type) == BLKmode
5226 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
5227 && compare_tree_int (TYPE_SIZE (gnu_field_type), BIGGEST_ALIGNMENT) <= 0
5228 && (packed == 1
5229 || (gnu_size
5230 && tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type)))
5231 || Present (Component_Clause (gnat_field))))
5233 /* See what the alternate type and size would be. */
5234 tree gnu_packable_type = make_packable_type (gnu_field_type);
5236 /* Compute whether we should avoid the substitution. */
5237 int reject =
5238 /* There is no point substituting if there is no change. */
5239 (gnu_packable_type == gnu_field_type
5241 /* The size of an aliased field must be an exact multiple of the
5242 type's alignment, which the substitution might increase. Reject
5243 substitutions that would so invalidate a component clause when the
5244 specified position is byte aligned, as the change would have no
5245 real benefit from the packing standpoint anyway. */
5246 (Is_Aliased (gnat_field)
5247 && Present (Component_Clause (gnat_field))
5248 && UI_To_Int (Component_Bit_Offset (gnat_field)) % BITS_PER_UNIT == 0
5249 && tree_low_cst (gnu_size, 1) % TYPE_ALIGN (gnu_packable_type) != 0)
5252 /* Substitute unless told otherwise. */
5253 if (!reject)
5255 gnu_field_type = gnu_packable_type;
5257 if (gnu_size == 0)
5258 gnu_size = rm_size (gnu_field_type);
5262 /* If we are packing the record and the field is BLKmode, round the
5263 size up to a byte boundary. */
5264 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
5265 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
5267 if (Present (Component_Clause (gnat_field)))
5269 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
5270 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5271 gnat_field, FIELD_DECL, false, true);
5273 /* Ensure the position does not overlap with the parent subtype,
5274 if there is one. */
5275 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
5277 tree gnu_parent
5278 = gnat_to_gnu_type (Parent_Subtype
5279 (Underlying_Type (Scope (gnat_field))));
5281 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
5282 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
5284 post_error_ne_tree
5285 ("offset of& must be beyond parent{, minimum allowed is ^}",
5286 First_Bit (Component_Clause (gnat_field)), gnat_field,
5287 TYPE_SIZE_UNIT (gnu_parent));
5291 /* If this field needs strict alignment, ensure the record is
5292 sufficiently aligned and that that position and size are
5293 consistent with the alignment. */
5294 if (needs_strict_alignment)
5296 tree gnu_rounded_size = round_up (rm_size (gnu_field_type),
5297 TYPE_ALIGN (gnu_field_type));
5299 TYPE_ALIGN (gnu_record_type)
5300 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
5302 /* If Atomic, the size must match exactly that of the field. */
5303 if ((Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
5304 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
5306 post_error_ne_tree
5307 ("atomic field& must be natural size of type{ (^)}",
5308 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5309 TYPE_SIZE (gnu_field_type));
5311 gnu_size = NULL_TREE;
5314 /* If Aliased, the size must match exactly the rounded size. We
5315 used to be more accommodating here and accept greater sizes, but
5316 fully supporting this case on big-endian platforms would require
5317 switching to a more involved layout for the field. */
5318 else if (Is_Aliased (gnat_field)
5319 && gnu_size
5320 && ! operand_equal_p (gnu_size, gnu_rounded_size, 0))
5322 post_error_ne_tree
5323 ("size of aliased field& must be ^ bits",
5324 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5325 gnu_rounded_size);
5326 gnu_size = NULL_TREE;
5329 if (!integer_zerop (size_binop
5330 (TRUNC_MOD_EXPR, gnu_pos,
5331 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
5333 if (Is_Aliased (gnat_field))
5334 post_error_ne_num
5335 ("position of aliased field& must be multiple of ^ bits",
5336 First_Bit (Component_Clause (gnat_field)), gnat_field,
5337 TYPE_ALIGN (gnu_field_type));
5339 else if (Treat_As_Volatile (gnat_field))
5340 post_error_ne_num
5341 ("position of volatile field& must be multiple of ^ bits",
5342 First_Bit (Component_Clause (gnat_field)), gnat_field,
5343 TYPE_ALIGN (gnu_field_type));
5345 else if (Strict_Alignment (Etype (gnat_field)))
5346 post_error_ne_num
5347 ("position of & with aliased or tagged components not multiple of ^ bits",
5348 First_Bit (Component_Clause (gnat_field)), gnat_field,
5349 TYPE_ALIGN (gnu_field_type));
5350 else
5351 gcc_unreachable ();
5353 gnu_pos = NULL_TREE;
5357 if (Is_Atomic (gnat_field))
5358 check_ok_for_atomic (gnu_field_type, gnat_field, false);
5361 /* If the record has rep clauses and this is the tag field, make a rep
5362 clause for it as well. */
5363 else if (Has_Specified_Layout (Scope (gnat_field))
5364 && Chars (gnat_field) == Name_uTag)
5366 gnu_pos = bitsize_zero_node;
5367 gnu_size = TYPE_SIZE (gnu_field_type);
5370 /* We need to make the size the maximum for the type if it is
5371 self-referential and an unconstrained type. In that case, we can't
5372 pack the field since we can't make a copy to align it. */
5373 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5374 && !gnu_size
5375 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
5376 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
5378 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
5379 packed = 0;
5382 /* If no size is specified (or if there was an error), don't specify a
5383 position. */
5384 if (!gnu_size)
5385 gnu_pos = NULL_TREE;
5386 else
5388 /* If the field's type is justified modular, we would need to remove
5389 the wrapper to (better) meet the layout requirements. However we
5390 can do so only if the field is not aliased to preserve the unique
5391 layout and if the prescribed size is not greater than that of the
5392 packed array to preserve the justification. */
5393 if (!needs_strict_alignment
5394 && TREE_CODE (gnu_field_type) == RECORD_TYPE
5395 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
5396 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
5397 <= 0)
5398 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
5400 gnu_field_type
5401 = make_type_from_size (gnu_field_type, gnu_size,
5402 Has_Biased_Representation (gnat_field));
5403 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
5404 "PAD", false, definition, true);
5407 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
5408 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
5410 /* Now create the decl for the field. */
5411 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
5412 packed, gnu_size, gnu_pos,
5413 Is_Aliased (gnat_field));
5414 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
5415 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
5417 if (Ekind (gnat_field) == E_Discriminant)
5418 DECL_DISCRIMINANT_NUMBER (gnu_field)
5419 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
5421 return gnu_field;
5424 /* Return true if TYPE is a type with variable size, a padding type with a
5425 field of variable size or is a record that has a field such a field. */
5427 static bool
5428 is_variable_size (tree type)
5430 tree field;
5432 /* We need not be concerned about this at all if we don't have
5433 strict alignment. */
5434 if (!STRICT_ALIGNMENT)
5435 return false;
5436 else if (!TREE_CONSTANT (TYPE_SIZE (type)))
5437 return true;
5438 else if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type)
5439 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
5440 return true;
5441 else if (TREE_CODE (type) != RECORD_TYPE
5442 && TREE_CODE (type) != UNION_TYPE
5443 && TREE_CODE (type) != QUAL_UNION_TYPE)
5444 return false;
5446 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5447 if (is_variable_size (TREE_TYPE (field)))
5448 return true;
5450 return false;
5453 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
5454 of GCC trees for fields that are in the record and have already been
5455 processed. When called from gnat_to_gnu_entity during the processing of a
5456 record type definition, the GCC nodes for the discriminants will be on
5457 the chain. The other calls to this function are recursive calls from
5458 itself for the Component_List of a variant and the chain is empty.
5460 PACKED is 1 if this is for a record with "pragma pack" and -1 is this is
5461 for a record type with "pragma component_alignment (storage_unit)".
5463 DEFINITION is true if we are defining this record.
5465 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
5466 with a rep clause is to be added. If it is nonzero, that is all that
5467 should be done with such fields.
5469 CANCEL_ALIGNMENT, if true, means the alignment should be zeroed before
5470 laying out the record. This means the alignment only serves to force fields
5471 to be bitfields, but not require the record to be that aligned. This is
5472 used for variants.
5474 ALL_REP, if true, means a rep clause was found for all the fields. This
5475 simplifies the logic since we know we're not in the mixed case.
5477 DEFER_DEBUG, if true, means that the debugging routines should not be
5478 called when finishing constructing the record type.
5480 The processing of the component list fills in the chain with all of the
5481 fields of the record and then the record type is finished. */
5483 static void
5484 components_to_record (tree gnu_record_type, Node_Id component_list,
5485 tree gnu_field_list, int packed, bool definition,
5486 tree *p_gnu_rep_list, bool cancel_alignment,
5487 bool all_rep, bool defer_debug)
5489 Node_Id component_decl;
5490 Entity_Id gnat_field;
5491 Node_Id variant_part;
5492 Node_Id variant;
5493 tree gnu_our_rep_list = NULL_TREE;
5494 tree gnu_field, gnu_last;
5495 bool layout_with_rep = false;
5496 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
5498 /* For each variable within each component declaration create a GCC field
5499 and add it to the list, skipping any pragmas in the list. */
5501 if (Present (Component_Items (component_list)))
5502 for (component_decl = First_Non_Pragma (Component_Items (component_list));
5503 Present (component_decl);
5504 component_decl = Next_Non_Pragma (component_decl))
5506 gnat_field = Defining_Entity (component_decl);
5508 if (Chars (gnat_field) == Name_uParent)
5509 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
5510 else
5512 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
5513 packed, definition);
5515 /* If this is the _Tag field, put it before any discriminants,
5516 instead of after them as is the case for all other fields.
5517 Ignore field of void type if only annotating. */
5518 if (Chars (gnat_field) == Name_uTag)
5519 gnu_field_list = chainon (gnu_field_list, gnu_field);
5520 else
5522 TREE_CHAIN (gnu_field) = gnu_field_list;
5523 gnu_field_list = gnu_field;
5527 save_gnu_tree (gnat_field, gnu_field, false);
5530 /* At the end of the component list there may be a variant part. */
5531 variant_part = Variant_Part (component_list);
5533 /* If this is an unchecked union, each variant must have exactly one
5534 component, each of which becomes one component of this union. */
5535 if (TREE_CODE (gnu_record_type) == UNION_TYPE
5536 && TYPE_UNCHECKED_UNION_P (gnu_record_type)
5537 && Present (variant_part))
5538 for (variant = First_Non_Pragma (Variants (variant_part));
5539 Present (variant);
5540 variant = Next_Non_Pragma (variant))
5542 component_decl
5543 = First_Non_Pragma (Component_Items (Component_List (variant)));
5544 gnat_field = Defining_Entity (component_decl);
5545 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
5546 definition);
5547 TREE_CHAIN (gnu_field) = gnu_field_list;
5548 gnu_field_list = gnu_field;
5549 save_gnu_tree (gnat_field, gnu_field, false);
5552 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
5553 mutually exclusive and should go in the same memory. To do this we need
5554 to treat each variant as a record whose elements are created from the
5555 component list for the variant. So here we create the records from the
5556 lists for the variants and put them all into the QUAL_UNION_TYPE. */
5557 else if (Present (variant_part))
5559 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
5560 Node_Id variant;
5561 tree gnu_union_type = make_node (QUAL_UNION_TYPE);
5562 tree gnu_union_field;
5563 tree gnu_variant_list = NULL_TREE;
5564 tree gnu_name = TYPE_NAME (gnu_record_type);
5565 tree gnu_var_name
5566 = concat_id_with_name
5567 (get_identifier (Get_Name_String (Chars (Name (variant_part)))),
5568 "XVN");
5570 if (TREE_CODE (gnu_name) == TYPE_DECL)
5571 gnu_name = DECL_NAME (gnu_name);
5573 TYPE_NAME (gnu_union_type)
5574 = concat_id_with_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
5575 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
5577 for (variant = First_Non_Pragma (Variants (variant_part));
5578 Present (variant);
5579 variant = Next_Non_Pragma (variant))
5581 tree gnu_variant_type = make_node (RECORD_TYPE);
5582 tree gnu_inner_name;
5583 tree gnu_qual;
5585 Get_Variant_Encoding (variant);
5586 gnu_inner_name = get_identifier (Name_Buffer);
5587 TYPE_NAME (gnu_variant_type)
5588 = concat_id_with_name (TYPE_NAME (gnu_union_type),
5589 IDENTIFIER_POINTER (gnu_inner_name));
5591 /* Set the alignment of the inner type in case we need to make
5592 inner objects into bitfields, but then clear it out
5593 so the record actually gets only the alignment required. */
5594 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
5595 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
5597 /* Similarly, if the outer record has a size specified and all fields
5598 have record rep clauses, we can propagate the size into the
5599 variant part. */
5600 if (all_rep_and_size)
5602 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
5603 TYPE_SIZE_UNIT (gnu_variant_type)
5604 = TYPE_SIZE_UNIT (gnu_record_type);
5607 components_to_record (gnu_variant_type, Component_List (variant),
5608 NULL_TREE, packed, definition,
5609 &gnu_our_rep_list, !all_rep_and_size, all_rep,
5610 false);
5612 gnu_qual = choices_to_gnu (gnu_discriminant,
5613 Discrete_Choices (variant));
5615 Set_Present_Expr (variant, annotate_value (gnu_qual));
5616 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
5617 gnu_union_type, 0,
5618 (all_rep_and_size
5619 ? TYPE_SIZE (gnu_record_type) : 0),
5620 (all_rep_and_size
5621 ? bitsize_zero_node : 0),
5624 DECL_INTERNAL_P (gnu_field) = 1;
5625 DECL_QUALIFIER (gnu_field) = gnu_qual;
5626 TREE_CHAIN (gnu_field) = gnu_variant_list;
5627 gnu_variant_list = gnu_field;
5630 /* We use to delete the empty variants from the end. However,
5631 we no longer do that because we need them to generate complete
5632 debugging information for the variant record. Otherwise,
5633 the union type definition will be missing the fields associated
5634 to these empty variants. */
5636 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
5637 if (gnu_variant_list)
5639 if (all_rep_and_size)
5641 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
5642 TYPE_SIZE_UNIT (gnu_union_type)
5643 = TYPE_SIZE_UNIT (gnu_record_type);
5646 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
5647 all_rep_and_size, false);
5649 gnu_union_field
5650 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
5651 packed,
5652 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
5653 all_rep ? bitsize_zero_node : 0, 0);
5655 DECL_INTERNAL_P (gnu_union_field) = 1;
5656 TREE_CHAIN (gnu_union_field) = gnu_field_list;
5657 gnu_field_list = gnu_union_field;
5661 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
5662 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
5663 in a separate pass since we want to handle the discriminants but can't
5664 play with them until we've used them in debugging data above.
5666 ??? Note: if we then reorder them, debugging information will be wrong,
5667 but there's nothing that can be done about this at the moment. */
5669 for (gnu_field = gnu_field_list, gnu_last = NULL_TREE; gnu_field; )
5671 if (DECL_FIELD_OFFSET (gnu_field))
5673 tree gnu_next = TREE_CHAIN (gnu_field);
5675 if (!gnu_last)
5676 gnu_field_list = gnu_next;
5677 else
5678 TREE_CHAIN (gnu_last) = gnu_next;
5680 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
5681 gnu_our_rep_list = gnu_field;
5682 gnu_field = gnu_next;
5684 else
5686 gnu_last = gnu_field;
5687 gnu_field = TREE_CHAIN (gnu_field);
5691 /* If we have any items in our rep'ed field list, it is not the case that all
5692 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
5693 set it and ignore the items. */
5694 if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
5695 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
5696 else if (gnu_our_rep_list)
5698 /* Otherwise, sort the fields by bit position and put them into their
5699 own record if we have any fields without rep clauses. */
5700 tree gnu_rep_type
5701 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
5702 int len = list_length (gnu_our_rep_list);
5703 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
5704 int i;
5706 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
5707 gnu_field = TREE_CHAIN (gnu_field), i++)
5708 gnu_arr[i] = gnu_field;
5710 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
5712 /* Put the fields in the list in order of increasing position, which
5713 means we start from the end. */
5714 gnu_our_rep_list = NULL_TREE;
5715 for (i = len - 1; i >= 0; i--)
5717 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
5718 gnu_our_rep_list = gnu_arr[i];
5719 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
5722 if (gnu_field_list)
5724 finish_record_type (gnu_rep_type, gnu_our_rep_list, true, false);
5725 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
5726 gnu_record_type, 0, 0, 0, 1);
5727 DECL_INTERNAL_P (gnu_field) = 1;
5728 gnu_field_list = chainon (gnu_field_list, gnu_field);
5730 else
5732 layout_with_rep = true;
5733 gnu_field_list = nreverse (gnu_our_rep_list);
5737 if (cancel_alignment)
5738 TYPE_ALIGN (gnu_record_type) = 0;
5740 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
5741 layout_with_rep, defer_debug);
5744 /* Called via qsort from the above. Returns -1, 1, depending on the
5745 bit positions and ordinals of the two fields. Use DECL_UID to ensure
5746 a stable sort. */
5748 static int
5749 compare_field_bitpos (const PTR rt1, const PTR rt2)
5751 tree *t1 = (tree *) rt1;
5752 tree *t2 = (tree *) rt2;
5754 if (tree_int_cst_equal (bit_position (*t1), bit_position (*t2)))
5755 return DECL_UID (*t1) < DECL_UID (*t2) ? -1 : 1;
5756 else if (tree_int_cst_lt (bit_position (*t1), bit_position (*t2)))
5757 return -1;
5758 else
5759 return 1;
5762 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
5763 placed into an Esize, Component_Bit_Offset, or Component_Size value
5764 in the GNAT tree. */
5766 static Uint
5767 annotate_value (tree gnu_size)
5769 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
5770 TCode tcode;
5771 Node_Ref_Or_Val ops[3], ret;
5772 int i;
5773 int size;
5775 /* See if we've already saved the value for this node. */
5776 if (EXPR_P (gnu_size) && TREE_COMPLEXITY (gnu_size))
5777 return (Node_Ref_Or_Val) TREE_COMPLEXITY (gnu_size);
5779 /* If we do not return inside this switch, TCODE will be set to the
5780 code to use for a Create_Node operand and LEN (set above) will be
5781 the number of recursive calls for us to make. */
5783 switch (TREE_CODE (gnu_size))
5785 case INTEGER_CST:
5786 if (TREE_OVERFLOW (gnu_size))
5787 return No_Uint;
5789 /* This may have come from a conversion from some smaller type,
5790 so ensure this is in bitsizetype. */
5791 gnu_size = convert (bitsizetype, gnu_size);
5793 /* For negative values, use NEGATE_EXPR of the supplied value. */
5794 if (tree_int_cst_sgn (gnu_size) < 0)
5796 /* The ridiculous code below is to handle the case of the largest
5797 negative integer. */
5798 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
5799 bool adjust = false;
5800 tree temp;
5802 if (TREE_CONSTANT_OVERFLOW (negative_size))
5804 negative_size
5805 = size_binop (MINUS_EXPR, bitsize_zero_node,
5806 size_binop (PLUS_EXPR, gnu_size,
5807 bitsize_one_node));
5808 adjust = true;
5811 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
5812 if (adjust)
5813 temp = build2 (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
5815 return annotate_value (temp);
5818 if (!host_integerp (gnu_size, 1))
5819 return No_Uint;
5821 size = tree_low_cst (gnu_size, 1);
5823 /* This peculiar test is to make sure that the size fits in an int
5824 on machines where HOST_WIDE_INT is not "int". */
5825 if (tree_low_cst (gnu_size, 1) == size)
5826 return UI_From_Int (size);
5827 else
5828 return No_Uint;
5830 case COMPONENT_REF:
5831 /* The only case we handle here is a simple discriminant reference. */
5832 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
5833 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
5834 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
5835 return Create_Node (Discrim_Val,
5836 annotate_value (DECL_DISCRIMINANT_NUMBER
5837 (TREE_OPERAND (gnu_size, 1))),
5838 No_Uint, No_Uint);
5839 else
5840 return No_Uint;
5842 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
5843 return annotate_value (TREE_OPERAND (gnu_size, 0));
5845 /* Now just list the operations we handle. */
5846 case COND_EXPR: tcode = Cond_Expr; break;
5847 case PLUS_EXPR: tcode = Plus_Expr; break;
5848 case MINUS_EXPR: tcode = Minus_Expr; break;
5849 case MULT_EXPR: tcode = Mult_Expr; break;
5850 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
5851 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
5852 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
5853 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
5854 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
5855 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
5856 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
5857 case NEGATE_EXPR: tcode = Negate_Expr; break;
5858 case MIN_EXPR: tcode = Min_Expr; break;
5859 case MAX_EXPR: tcode = Max_Expr; break;
5860 case ABS_EXPR: tcode = Abs_Expr; break;
5861 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
5862 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
5863 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
5864 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
5865 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
5866 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
5867 case BIT_AND_EXPR: tcode = Bit_And_Expr; break;
5868 case LT_EXPR: tcode = Lt_Expr; break;
5869 case LE_EXPR: tcode = Le_Expr; break;
5870 case GT_EXPR: tcode = Gt_Expr; break;
5871 case GE_EXPR: tcode = Ge_Expr; break;
5872 case EQ_EXPR: tcode = Eq_Expr; break;
5873 case NE_EXPR: tcode = Ne_Expr; break;
5875 default:
5876 return No_Uint;
5879 /* Now get each of the operands that's relevant for this code. If any
5880 cannot be expressed as a repinfo node, say we can't. */
5881 for (i = 0; i < 3; i++)
5882 ops[i] = No_Uint;
5884 for (i = 0; i < len; i++)
5886 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
5887 if (ops[i] == No_Uint)
5888 return No_Uint;
5891 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
5892 TREE_COMPLEXITY (gnu_size) = ret;
5893 return ret;
5896 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
5897 GCC type, set Component_Bit_Offset and Esize to the position and size
5898 used by Gigi. */
5900 static void
5901 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
5903 tree gnu_list;
5904 tree gnu_entry;
5905 Entity_Id gnat_field;
5907 /* We operate by first making a list of all fields and their positions
5908 (we can get the sizes easily at any time) by a recursive call
5909 and then update all the sizes into the tree. */
5910 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
5911 size_zero_node, bitsize_zero_node,
5912 BIGGEST_ALIGNMENT);
5914 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
5915 gnat_field = Next_Entity (gnat_field))
5916 if ((Ekind (gnat_field) == E_Component
5917 || (Ekind (gnat_field) == E_Discriminant
5918 && !Is_Unchecked_Union (Scope (gnat_field)))))
5920 tree parent_offset = bitsize_zero_node;
5922 gnu_entry = purpose_member (gnat_to_gnu_field_decl (gnat_field),
5923 gnu_list);
5925 if (gnu_entry)
5927 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
5929 /* In this mode the tag and parent components have not been
5930 generated, so we add the appropriate offset to each
5931 component. For a component appearing in the current
5932 extension, the offset is the size of the parent. */
5933 if (Is_Derived_Type (gnat_entity)
5934 && Original_Record_Component (gnat_field) == gnat_field)
5935 parent_offset
5936 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
5937 bitsizetype);
5938 else
5939 parent_offset = bitsize_int (POINTER_SIZE);
5942 Set_Component_Bit_Offset
5943 (gnat_field,
5944 annotate_value
5945 (size_binop (PLUS_EXPR,
5946 bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
5947 TREE_VALUE (TREE_VALUE
5948 (TREE_VALUE (gnu_entry)))),
5949 parent_offset)));
5951 Set_Esize (gnat_field,
5952 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
5954 else if (Is_Tagged_Type (gnat_entity)
5955 && Is_Derived_Type (gnat_entity))
5957 /* If there is no gnu_entry, this is an inherited component whose
5958 position is the same as in the parent type. */
5959 Set_Component_Bit_Offset
5960 (gnat_field,
5961 Component_Bit_Offset (Original_Record_Component (gnat_field)));
5962 Set_Esize (gnat_field,
5963 Esize (Original_Record_Component (gnat_field)));
5968 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
5969 FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
5970 position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
5971 placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position. GNU_POS is
5972 to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
5973 the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
5974 so far. */
5976 static tree
5977 compute_field_positions (tree gnu_type, tree gnu_list, tree gnu_pos,
5978 tree gnu_bitpos, unsigned int offset_align)
5980 tree gnu_field;
5981 tree gnu_result = gnu_list;
5983 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
5984 gnu_field = TREE_CHAIN (gnu_field))
5986 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
5987 DECL_FIELD_BIT_OFFSET (gnu_field));
5988 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
5989 DECL_FIELD_OFFSET (gnu_field));
5990 unsigned int our_offset_align
5991 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
5993 gnu_result
5994 = tree_cons (gnu_field,
5995 tree_cons (gnu_our_offset,
5996 tree_cons (size_int (our_offset_align),
5997 gnu_our_bitpos, NULL_TREE),
5998 NULL_TREE),
5999 gnu_result);
6001 if (DECL_INTERNAL_P (gnu_field))
6002 gnu_result
6003 = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
6004 gnu_our_offset, gnu_our_bitpos,
6005 our_offset_align);
6008 return gnu_result;
6011 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
6012 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
6013 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
6014 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
6015 for the size of a field. COMPONENT_P is true if we are being called
6016 to process the Component_Size of GNAT_OBJECT. This is used for error
6017 message handling and to indicate to use the object size of GNU_TYPE.
6018 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
6019 it means that a size of zero should be treated as an unspecified size. */
6021 static tree
6022 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
6023 enum tree_code kind, bool component_p, bool zero_ok)
6025 Node_Id gnat_error_node;
6026 tree type_size
6027 = kind == VAR_DECL ? TYPE_SIZE (gnu_type) : rm_size (gnu_type);
6028 tree size;
6030 /* Find the node to use for errors. */
6031 if ((Ekind (gnat_object) == E_Component
6032 || Ekind (gnat_object) == E_Discriminant)
6033 && Present (Component_Clause (gnat_object)))
6034 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
6035 else if (Present (Size_Clause (gnat_object)))
6036 gnat_error_node = Expression (Size_Clause (gnat_object));
6037 else
6038 gnat_error_node = gnat_object;
6040 /* Return 0 if no size was specified, either because Esize was not Present or
6041 the specified size was zero. */
6042 if (No (uint_size) || uint_size == No_Uint)
6043 return NULL_TREE;
6045 /* Get the size as a tree. Give an error if a size was specified, but cannot
6046 be represented as in sizetype. */
6047 size = UI_To_gnu (uint_size, bitsizetype);
6048 if (TREE_OVERFLOW (size))
6050 post_error_ne (component_p ? "component size of & is too large"
6051 : "size of & is too large",
6052 gnat_error_node, gnat_object);
6053 return NULL_TREE;
6056 /* Ignore a negative size since that corresponds to our back-annotation.
6057 Also ignore a zero size unless a size clause exists. */
6058 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
6059 return NULL_TREE;
6061 /* The size of objects is always a multiple of a byte. */
6062 if (kind == VAR_DECL
6063 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
6065 if (component_p)
6066 post_error_ne ("component size for& is not a multiple of Storage_Unit",
6067 gnat_error_node, gnat_object);
6068 else
6069 post_error_ne ("size for& is not a multiple of Storage_Unit",
6070 gnat_error_node, gnat_object);
6071 return NULL_TREE;
6074 /* If this is an integral type or a packed array type, the front-end has
6075 verified the size, so we need not do it here (which would entail
6076 checking against the bounds). However, if this is an aliased object, it
6077 may not be smaller than the type of the object. */
6078 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
6079 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
6080 return size;
6082 /* If the object is a record that contains a template, add the size of
6083 the template to the specified size. */
6084 if (TREE_CODE (gnu_type) == RECORD_TYPE
6085 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6086 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
6088 /* Modify the size of the type to be that of the maximum size if it has a
6089 discriminant or the size of a thin pointer if this is a fat pointer. */
6090 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
6091 type_size = max_size (type_size, true);
6092 else if (TYPE_FAT_POINTER_P (gnu_type))
6093 type_size = bitsize_int (POINTER_SIZE);
6095 /* If this is an access type, the minimum size is that given by the smallest
6096 integral mode that's valid for pointers. */
6097 if (TREE_CODE (gnu_type) == POINTER_TYPE)
6099 enum machine_mode p_mode;
6101 for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
6102 !targetm.valid_pointer_mode (p_mode);
6103 p_mode = GET_MODE_WIDER_MODE (p_mode))
6106 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
6109 /* If the size of the object is a constant, the new size must not be
6110 smaller. */
6111 if (TREE_CODE (type_size) != INTEGER_CST
6112 || TREE_OVERFLOW (type_size)
6113 || tree_int_cst_lt (size, type_size))
6115 if (component_p)
6116 post_error_ne_tree
6117 ("component size for& too small{, minimum allowed is ^}",
6118 gnat_error_node, gnat_object, type_size);
6119 else
6120 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
6121 gnat_error_node, gnat_object, type_size);
6123 if (kind == VAR_DECL && !component_p
6124 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
6125 && !tree_int_cst_lt (size, rm_size (gnu_type)))
6126 post_error_ne_tree_2
6127 ("\\size of ^ is not a multiple of alignment (^ bits)",
6128 gnat_error_node, gnat_object, rm_size (gnu_type),
6129 TYPE_ALIGN (gnu_type));
6131 else if (INTEGRAL_TYPE_P (gnu_type))
6132 post_error_ne ("\\size would be legal if & were not aliased!",
6133 gnat_error_node, gnat_object);
6135 return NULL_TREE;
6138 return size;
6141 /* Similarly, but both validate and process a value of RM_Size. This
6142 routine is only called for types. */
6144 static void
6145 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
6147 /* Only give an error if a Value_Size clause was explicitly given.
6148 Otherwise, we'd be duplicating an error on the Size clause. */
6149 Node_Id gnat_attr_node
6150 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
6151 tree old_size = rm_size (gnu_type);
6152 tree size;
6154 /* Get the size as a tree. Do nothing if none was specified, either
6155 because RM_Size was not Present or if the specified size was zero.
6156 Give an error if a size was specified, but cannot be represented as
6157 in sizetype. */
6158 if (No (uint_size) || uint_size == No_Uint)
6159 return;
6161 size = UI_To_gnu (uint_size, bitsizetype);
6162 if (TREE_OVERFLOW (size))
6164 if (Present (gnat_attr_node))
6165 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
6166 gnat_entity);
6168 return;
6171 /* Ignore a negative size since that corresponds to our back-annotation.
6172 Also ignore a zero size unless a size clause exists, a Value_Size
6173 clause exists, or this is an integer type, in which case the
6174 front end will have always set it. */
6175 else if (tree_int_cst_sgn (size) < 0
6176 || (integer_zerop (size) && No (gnat_attr_node)
6177 && !Has_Size_Clause (gnat_entity)
6178 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
6179 return;
6181 /* If the old size is self-referential, get the maximum size. */
6182 if (CONTAINS_PLACEHOLDER_P (old_size))
6183 old_size = max_size (old_size, true);
6185 /* If the size of the object is a constant, the new size must not be
6186 smaller (the front end checks this for scalar types). */
6187 if (TREE_CODE (old_size) != INTEGER_CST
6188 || TREE_OVERFLOW (old_size)
6189 || (AGGREGATE_TYPE_P (gnu_type)
6190 && tree_int_cst_lt (size, old_size)))
6192 if (Present (gnat_attr_node))
6193 post_error_ne_tree
6194 ("Value_Size for& too small{, minimum allowed is ^}",
6195 gnat_attr_node, gnat_entity, old_size);
6197 return;
6200 /* Otherwise, set the RM_Size. */
6201 if (TREE_CODE (gnu_type) == INTEGER_TYPE
6202 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
6203 TYPE_RM_SIZE_NUM (gnu_type) = size;
6204 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
6205 TYPE_RM_SIZE_NUM (gnu_type) = size;
6206 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6207 || TREE_CODE (gnu_type) == UNION_TYPE
6208 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6209 && !TYPE_IS_FAT_POINTER_P (gnu_type))
6210 SET_TYPE_ADA_SIZE (gnu_type, size);
6213 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
6214 If TYPE is the best type, return it. Otherwise, make a new type. We
6215 only support new integral and pointer types. BIASED_P is nonzero if
6216 we are making a biased type. */
6218 static tree
6219 make_type_from_size (tree type, tree size_tree, bool biased_p)
6221 tree new_type;
6222 unsigned HOST_WIDE_INT size;
6223 bool unsigned_p;
6225 /* If size indicates an error, just return TYPE to avoid propagating the
6226 error. Likewise if it's too large to represent. */
6227 if (!size_tree || !host_integerp (size_tree, 1))
6228 return type;
6230 size = tree_low_cst (size_tree, 1);
6231 switch (TREE_CODE (type))
6233 case INTEGER_TYPE:
6234 case ENUMERAL_TYPE:
6235 /* Only do something if the type is not already the proper size and is
6236 not a packed array type. */
6237 if (TYPE_PACKED_ARRAY_TYPE_P (type)
6238 || (TYPE_PRECISION (type) == size
6239 && biased_p == (TREE_CODE (type) == INTEGER_CST
6240 && TYPE_BIASED_REPRESENTATION_P (type))))
6241 break;
6243 biased_p |= (TREE_CODE (type) == INTEGER_TYPE
6244 && TYPE_BIASED_REPRESENTATION_P (type));
6245 unsigned_p = TYPE_UNSIGNED (type) || biased_p;
6247 size = MIN (size, LONG_LONG_TYPE_SIZE);
6248 new_type
6249 = unsigned_p ? make_unsigned_type (size) : make_signed_type (size);
6250 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
6251 TYPE_MIN_VALUE (new_type)
6252 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
6253 TYPE_MAX_VALUE (new_type)
6254 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
6255 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
6256 TYPE_RM_SIZE_NUM (new_type) = bitsize_int (size);
6257 return new_type;
6259 case RECORD_TYPE:
6260 /* Do something if this is a fat pointer, in which case we
6261 may need to return the thin pointer. */
6262 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
6263 return
6264 build_pointer_type
6265 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
6266 break;
6268 case POINTER_TYPE:
6269 /* Only do something if this is a thin pointer, in which case we
6270 may need to return the fat pointer. */
6271 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
6272 return
6273 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
6275 break;
6277 default:
6278 break;
6281 return type;
6284 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
6285 a type or object whose present alignment is ALIGN. If this alignment is
6286 valid, return it. Otherwise, give an error and return ALIGN. */
6288 static unsigned int
6289 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
6291 Node_Id gnat_error_node = gnat_entity;
6292 unsigned int new_align;
6294 #ifndef MAX_OFILE_ALIGNMENT
6295 #define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
6296 #endif
6298 if (Present (Alignment_Clause (gnat_entity)))
6299 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
6301 /* Don't worry about checking alignment if alignment was not specified
6302 by the source program and we already posted an error for this entity. */
6304 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
6305 return align;
6307 /* Within GCC, an alignment is an integer, so we must make sure a
6308 value is specified that fits in that range. Also, alignments of
6309 more than MAX_OFILE_ALIGNMENT can't be supported. */
6311 if (! UI_Is_In_Int_Range (alignment)
6312 || ((new_align = UI_To_Int (alignment))
6313 > MAX_OFILE_ALIGNMENT / BITS_PER_UNIT))
6314 post_error_ne_num ("largest supported alignment for& is ^",
6315 gnat_error_node, gnat_entity,
6316 MAX_OFILE_ALIGNMENT / BITS_PER_UNIT);
6317 else if (!(Present (Alignment_Clause (gnat_entity))
6318 && From_At_Mod (Alignment_Clause (gnat_entity)))
6319 && new_align * BITS_PER_UNIT < align)
6320 post_error_ne_num ("alignment for& must be at least ^",
6321 gnat_error_node, gnat_entity,
6322 align / BITS_PER_UNIT);
6323 else
6324 align = MAX (align, new_align == 0 ? 1 : new_align * BITS_PER_UNIT);
6326 return align;
6329 /* Verify that OBJECT, a type or decl, is something we can implement
6330 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
6331 if we require atomic components. */
6333 static void
6334 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
6336 Node_Id gnat_error_point = gnat_entity;
6337 Node_Id gnat_node;
6338 enum machine_mode mode;
6339 unsigned int align;
6340 tree size;
6342 /* There are three case of what OBJECT can be. It can be a type, in which
6343 case we take the size, alignment and mode from the type. It can be a
6344 declaration that was indirect, in which case the relevant values are
6345 that of the type being pointed to, or it can be a normal declaration,
6346 in which case the values are of the decl. The code below assumes that
6347 OBJECT is either a type or a decl. */
6348 if (TYPE_P (object))
6350 mode = TYPE_MODE (object);
6351 align = TYPE_ALIGN (object);
6352 size = TYPE_SIZE (object);
6354 else if (DECL_BY_REF_P (object))
6356 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
6357 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
6358 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
6360 else
6362 mode = DECL_MODE (object);
6363 align = DECL_ALIGN (object);
6364 size = DECL_SIZE (object);
6367 /* Consider all floating-point types atomic and any types that that are
6368 represented by integers no wider than a machine word. */
6369 if (GET_MODE_CLASS (mode) == MODE_FLOAT
6370 || ((GET_MODE_CLASS (mode) == MODE_INT
6371 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
6372 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
6373 return;
6375 /* For the moment, also allow anything that has an alignment equal
6376 to its size and which is smaller than a word. */
6377 if (size && TREE_CODE (size) == INTEGER_CST
6378 && compare_tree_int (size, align) == 0
6379 && align <= BITS_PER_WORD)
6380 return;
6382 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
6383 gnat_node = Next_Rep_Item (gnat_node))
6385 if (!comp_p && Nkind (gnat_node) == N_Pragma
6386 && Get_Pragma_Id (Chars (gnat_node)) == Pragma_Atomic)
6387 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6388 else if (comp_p && Nkind (gnat_node) == N_Pragma
6389 && (Get_Pragma_Id (Chars (gnat_node))
6390 == Pragma_Atomic_Components))
6391 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6394 if (comp_p)
6395 post_error_ne ("atomic access to component of & cannot be guaranteed",
6396 gnat_error_point, gnat_entity);
6397 else
6398 post_error_ne ("atomic access to & cannot be guaranteed",
6399 gnat_error_point, gnat_entity);
6402 /* Check if FTYPE1 and FTYPE2, two potentially different function type nodes,
6403 have compatible signatures so that a call using one type may be safely
6404 issued if the actual target function type is the other. Return 1 if it is
6405 the case, 0 otherwise, and post errors on the incompatibilities.
6407 This is used when an Ada subprogram is mapped onto a GCC builtin, to ensure
6408 that calls to the subprogram will have arguments suitable for the later
6409 underlying builtin expansion. */
6411 static int
6412 compatible_signatures_p (tree ftype1, tree ftype2)
6414 /* As of now, we only perform very trivial tests and consider it's the
6415 programmer's responsibility to ensure the type correctness in the Ada
6416 declaration, as in the regular Import cases.
6418 Mismatches typically result in either error messages from the builtin
6419 expander, internal compiler errors, or in a real call sequence. This
6420 should be refined to issue diagnostics helping error detection and
6421 correction. */
6423 /* Almost fake test, ensuring a use of each argument. */
6424 if (ftype1 == ftype2)
6425 return 1;
6427 return 1;
6430 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a new type
6431 with all size expressions that contain F updated by replacing F with R.
6432 This is identical to GCC's substitute_in_type except that it knows about
6433 TYPE_INDEX_TYPE. If F is NULL_TREE, always make a new RECORD_TYPE, even if
6434 nothing has changed. */
6436 tree
6437 gnat_substitute_in_type (tree t, tree f, tree r)
6439 tree new = t;
6440 tree tem;
6442 switch (TREE_CODE (t))
6444 case INTEGER_TYPE:
6445 case ENUMERAL_TYPE:
6446 case BOOLEAN_TYPE:
6447 case CHAR_TYPE:
6448 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6449 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
6451 tree low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
6452 tree high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
6454 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6455 return t;
6457 new = build_range_type (TREE_TYPE (t), low, high);
6458 if (TYPE_INDEX_TYPE (t))
6459 SET_TYPE_INDEX_TYPE
6460 (new, gnat_substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
6461 return new;
6464 return t;
6466 case REAL_TYPE:
6467 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6468 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
6470 tree low = NULL_TREE, high = NULL_TREE;
6472 if (TYPE_MIN_VALUE (t))
6473 low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
6474 if (TYPE_MAX_VALUE (t))
6475 high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
6477 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6478 return t;
6480 t = copy_type (t);
6481 TYPE_MIN_VALUE (t) = low;
6482 TYPE_MAX_VALUE (t) = high;
6484 return t;
6486 case COMPLEX_TYPE:
6487 tem = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6488 if (tem == TREE_TYPE (t))
6489 return t;
6491 return build_complex_type (tem);
6493 case OFFSET_TYPE:
6494 case METHOD_TYPE:
6495 case FUNCTION_TYPE:
6496 case LANG_TYPE:
6497 /* Don't know how to do these yet. */
6498 gcc_unreachable ();
6500 case ARRAY_TYPE:
6502 tree component = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6503 tree domain = gnat_substitute_in_type (TYPE_DOMAIN (t), f, r);
6505 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6506 return t;
6508 new = build_array_type (component, domain);
6509 TYPE_SIZE (new) = 0;
6510 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
6511 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
6512 layout_type (new);
6513 TYPE_ALIGN (new) = TYPE_ALIGN (t);
6515 /* If we had bounded the sizes of T by a constant, bound the sizes of
6516 NEW by the same constant. */
6517 if (TREE_CODE (TYPE_SIZE (t)) == MIN_EXPR)
6518 TYPE_SIZE (new)
6519 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE (t), 1),
6520 TYPE_SIZE (new));
6521 if (TREE_CODE (TYPE_SIZE_UNIT (t)) == MIN_EXPR)
6522 TYPE_SIZE_UNIT (new)
6523 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE_UNIT (t), 1),
6524 TYPE_SIZE_UNIT (new));
6525 return new;
6528 case RECORD_TYPE:
6529 case UNION_TYPE:
6530 case QUAL_UNION_TYPE:
6532 tree field;
6533 bool changed_field
6534 = (f == NULL_TREE && !TREE_CONSTANT (TYPE_SIZE (t)));
6535 bool field_has_rep = false;
6536 tree last_field = NULL_TREE;
6538 tree new = copy_type (t);
6540 /* Start out with no fields, make new fields, and chain them
6541 in. If we haven't actually changed the type of any field,
6542 discard everything we've done and return the old type. */
6544 TYPE_FIELDS (new) = NULL_TREE;
6545 TYPE_SIZE (new) = NULL_TREE;
6547 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6549 tree new_field = copy_node (field);
6551 TREE_TYPE (new_field)
6552 = gnat_substitute_in_type (TREE_TYPE (new_field), f, r);
6554 if (DECL_HAS_REP_P (field) && !DECL_INTERNAL_P (field))
6555 field_has_rep = true;
6556 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
6557 changed_field = true;
6559 /* If this is an internal field and the type of this field is
6560 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
6561 the type just has one element, treat that as the field.
6562 But don't do this if we are processing a QUAL_UNION_TYPE. */
6563 if (TREE_CODE (t) != QUAL_UNION_TYPE
6564 && DECL_INTERNAL_P (new_field)
6565 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
6566 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
6568 if (!TYPE_FIELDS (TREE_TYPE (new_field)))
6569 continue;
6571 if (!TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))))
6573 tree next_new_field
6574 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
6576 /* Make sure omitting the union doesn't change
6577 the layout. */
6578 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
6579 new_field = next_new_field;
6583 DECL_CONTEXT (new_field) = new;
6584 SET_DECL_ORIGINAL_FIELD (new_field,
6585 (DECL_ORIGINAL_FIELD (field)
6586 ? DECL_ORIGINAL_FIELD (field) : field));
6588 /* If the size of the old field was set at a constant,
6589 propagate the size in case the type's size was variable.
6590 (This occurs in the case of a variant or discriminated
6591 record with a default size used as a field of another
6592 record.) */
6593 DECL_SIZE (new_field)
6594 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
6595 ? DECL_SIZE (field) : NULL_TREE;
6596 DECL_SIZE_UNIT (new_field)
6597 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
6598 ? DECL_SIZE_UNIT (field) : NULL_TREE;
6600 if (TREE_CODE (t) == QUAL_UNION_TYPE)
6602 tree new_q = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
6604 if (new_q != DECL_QUALIFIER (new_field))
6605 changed_field = true;
6607 /* Do the substitution inside the qualifier and if we find
6608 that this field will not be present, omit it. */
6609 DECL_QUALIFIER (new_field) = new_q;
6611 if (integer_zerop (DECL_QUALIFIER (new_field)))
6612 continue;
6615 if (!last_field)
6616 TYPE_FIELDS (new) = new_field;
6617 else
6618 TREE_CHAIN (last_field) = new_field;
6620 last_field = new_field;
6622 /* If this is a qualified type and this field will always be
6623 present, we are done. */
6624 if (TREE_CODE (t) == QUAL_UNION_TYPE
6625 && integer_onep (DECL_QUALIFIER (new_field)))
6626 break;
6629 /* If this used to be a qualified union type, but we now know what
6630 field will be present, make this a normal union. */
6631 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
6632 && (!TYPE_FIELDS (new)
6633 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
6634 TREE_SET_CODE (new, UNION_TYPE);
6635 else if (!changed_field)
6636 return t;
6638 gcc_assert (!field_has_rep);
6639 layout_type (new);
6641 /* If the size was originally a constant use it. */
6642 if (TYPE_SIZE (t) && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
6643 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
6645 TYPE_SIZE (new) = TYPE_SIZE (t);
6646 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
6647 SET_TYPE_ADA_SIZE (new, TYPE_ADA_SIZE (t));
6650 return new;
6653 default:
6654 return t;
6658 /* Return the "RM size" of GNU_TYPE. This is the actual number of bits
6659 needed to represent the object. */
6661 tree
6662 rm_size (tree gnu_type)
6664 /* For integer types, this is the precision. For record types, we store
6665 the size explicitly. For other types, this is just the size. */
6667 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
6668 return TYPE_RM_SIZE (gnu_type);
6669 else if (TREE_CODE (gnu_type) == RECORD_TYPE
6670 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6671 /* Return the rm_size of the actual data plus the size of the template. */
6672 return
6673 size_binop (PLUS_EXPR,
6674 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
6675 DECL_SIZE (TYPE_FIELDS (gnu_type)));
6676 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6677 || TREE_CODE (gnu_type) == UNION_TYPE
6678 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6679 && !TYPE_IS_FAT_POINTER_P (gnu_type)
6680 && TYPE_ADA_SIZE (gnu_type))
6681 return TYPE_ADA_SIZE (gnu_type);
6682 else
6683 return TYPE_SIZE (gnu_type);
6686 /* Return an identifier representing the external name to be used for
6687 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
6688 and the specified suffix. */
6690 tree
6691 create_concat_name (Entity_Id gnat_entity, const char *suffix)
6693 Entity_Kind kind = Ekind (gnat_entity);
6695 const char *str = (!suffix ? "" : suffix);
6696 String_Template temp = {1, strlen (str)};
6697 Fat_Pointer fp = {str, &temp};
6699 Get_External_Name_With_Suffix (gnat_entity, fp);
6701 /* A variable using the Stdcall convention (meaning we are running
6702 on a Windows box) live in a DLL. Here we adjust its name to use
6703 the jump-table, the _imp__NAME contains the address for the NAME
6704 variable. */
6705 if ((kind == E_Variable || kind == E_Constant)
6706 && Has_Stdcall_Convention (gnat_entity))
6708 const char *prefix = "_imp__";
6709 int k, plen = strlen (prefix);
6711 for (k = 0; k <= Name_Len; k++)
6712 Name_Buffer [Name_Len - k + plen] = Name_Buffer [Name_Len - k];
6713 strncpy (Name_Buffer, prefix, plen);
6716 return get_identifier (Name_Buffer);
6719 /* Return the name to be used for GNAT_ENTITY. If a type, create a
6720 fully-qualified name, possibly with type information encoding.
6721 Otherwise, return the name. */
6723 tree
6724 get_entity_name (Entity_Id gnat_entity)
6726 Get_Encoded_Name (gnat_entity);
6727 return get_identifier (Name_Buffer);
6730 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
6731 string, return a new IDENTIFIER_NODE that is the concatenation of
6732 the name in GNU_ID and SUFFIX. */
6734 tree
6735 concat_id_with_name (tree gnu_id, const char *suffix)
6737 int len = IDENTIFIER_LENGTH (gnu_id);
6739 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id),
6740 IDENTIFIER_LENGTH (gnu_id));
6741 strncpy (Name_Buffer + len, "___", 3);
6742 len += 3;
6743 strcpy (Name_Buffer + len, suffix);
6744 return get_identifier (Name_Buffer);
6747 #include "gt-ada-decl.h"