* c-cppbuiltin.c (c_cpp_builtins): Define __pic__ and __PIC__ when
[official-gcc.git] / gcc / ada / decl.c
blobbbbb471a3ae9a6592a82601da0728c25198c9c49
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;
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 after forcing the evaluation of any
774 SAVE_EXPR. At the global level, we can only do this if we
775 know no SAVE_EXPRs will be made. */
776 if (!Materialize_Entity (gnat_entity)
777 && (!global_bindings_p ()
778 || (staticp (gnu_expr)
779 && !TREE_SIDE_EFFECTS (gnu_expr))))
781 maybe_stable_expr
782 = maybe_stabilize_reference (gnu_expr, true, false,
783 &stabilized);
785 if (stabilized)
787 gnu_decl = maybe_stable_expr;
788 save_gnu_tree (gnat_entity, gnu_decl, true);
789 saved = true;
790 break;
793 /* The stabilization failed. Keep maybe_stable_expr
794 untouched here to let the pointer case below know
795 about that failure. */
798 /* Case 3: Make this into a constant pointer to the object we
799 are to rename and attach the object to the pointer if it is
800 an lvalue that can be stabilized.
802 From the proper scope, attached objects will be referenced
803 directly instead of indirectly via the pointer to avoid
804 subtle aliasing problems with non addressable entities.
805 They have to be stable because we must not evaluate the
806 variables in the expression every time the renaming is used.
807 They also have to be lvalues because the context in which
808 they are reused sometimes requires so. We call pointers
809 with an attached object "renaming" pointers.
811 In the rare cases where we cannot stabilize the renamed
812 object, we just make a "bare" pointer, and the renamed
813 entity is always accessed indirectly through it. */
815 bool has_side_effects = TREE_SIDE_EFFECTS (gnu_expr);
816 inner_const_flag = TREE_READONLY (gnu_expr);
817 const_flag = true;
818 gnu_type = build_reference_type (gnu_type);
820 /* If a previous attempt at unrestricted
821 stabilization failed, there is no point trying
822 again and we can reuse the result without
823 attaching it to the pointer. */
824 if (maybe_stable_expr)
827 /* Otherwise, try to stabilize now, restricting to
828 lvalues only, and attach the expression to the pointer
829 if the stabilization succeeds. */
830 else
832 maybe_stable_expr
833 = maybe_stabilize_reference (gnu_expr, true, true,
834 &stabilized);
836 if (stabilized)
837 renamed_obj = maybe_stable_expr;
838 /* Attaching is actually performed downstream, as soon
839 as we have a DECL for the pointer we make. */
842 gnu_expr
843 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
845 if (!global_bindings_p ())
847 /* If the original expression had side effects, put a
848 SAVE_EXPR around this whole thing. */
849 if (has_side_effects)
850 gnu_expr = save_expr (gnu_expr);
852 add_stmt (gnu_expr);
855 gnu_size = NULL_TREE;
856 used_by_ref = true;
861 /* If this is an aliased object whose nominal subtype is unconstrained,
862 the object is a record that contains both the template and
863 the object. If there is an initializer, it will have already
864 been converted to the right type, but we need to create the
865 template if there is no initializer. */
866 else if (definition && TREE_CODE (gnu_type) == RECORD_TYPE
867 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
868 /* Beware that padding might have been introduced
869 via maybe_pad_type above. */
870 || (TYPE_IS_PADDING_P (gnu_type)
871 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
872 == RECORD_TYPE
873 && TYPE_CONTAINS_TEMPLATE_P
874 (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
875 && !gnu_expr)
877 tree template_field
878 = TYPE_IS_PADDING_P (gnu_type)
879 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
880 : TYPE_FIELDS (gnu_type);
882 gnu_expr
883 = gnat_build_constructor
884 (gnu_type,
885 tree_cons
886 (template_field,
887 build_template (TREE_TYPE (template_field),
888 TREE_TYPE (TREE_CHAIN (template_field)),
889 NULL_TREE),
890 NULL_TREE));
893 /* If this is a pointer and it does not have an initializing
894 expression, initialize it to NULL, unless the object is
895 imported. */
896 if (definition
897 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
898 && !Is_Imported (gnat_entity) && !gnu_expr)
899 gnu_expr = integer_zero_node;
901 /* If we are defining the object and it has an Address clause we must
902 get the address expression from the saved GCC tree for the
903 object if the object has a Freeze_Node. Otherwise, we elaborate
904 the address expression here since the front-end has guaranteed
905 in that case that the elaboration has no effects. Note that
906 only the latter mechanism is currently in use. */
907 if (definition && Present (Address_Clause (gnat_entity)))
909 tree gnu_address
910 = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
911 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
913 save_gnu_tree (gnat_entity, NULL_TREE, false);
915 /* Ignore the size. It's either meaningless or was handled
916 above. */
917 gnu_size = NULL_TREE;
918 gnu_type = build_reference_type (gnu_type);
919 gnu_address = convert (gnu_type, gnu_address);
920 used_by_ref = true;
921 const_flag = !Is_Public (gnat_entity);
923 /* If we don't have an initializing expression for the underlying
924 variable, the initializing expression for the pointer is the
925 specified address. Otherwise, we have to make a COMPOUND_EXPR
926 to assign both the address and the initial value. */
927 if (!gnu_expr)
928 gnu_expr = gnu_address;
929 else
930 gnu_expr
931 = build2 (COMPOUND_EXPR, gnu_type,
932 build_binary_op
933 (MODIFY_EXPR, NULL_TREE,
934 build_unary_op (INDIRECT_REF, NULL_TREE,
935 gnu_address),
936 gnu_expr),
937 gnu_address);
940 /* If it has an address clause and we are not defining it, mark it
941 as an indirect object. Likewise for Stdcall objects that are
942 imported. */
943 if ((!definition && Present (Address_Clause (gnat_entity)))
944 || (Is_Imported (gnat_entity)
945 && Has_Stdcall_Convention (gnat_entity)))
947 gnu_type = build_reference_type (gnu_type);
948 gnu_size = NULL_TREE;
950 gnu_expr = NULL_TREE;
951 /* No point in taking the address of an initializing expression
952 that isn't going to be used. */
954 used_by_ref = true;
957 /* If we are at top level and this object is of variable size,
958 make the actual type a hidden pointer to the real type and
959 make the initializer be a memory allocation and initialization.
960 Likewise for objects we aren't defining (presumed to be
961 external references from other packages), but there we do
962 not set up an initialization.
964 If the object's size overflows, make an allocator too, so that
965 Storage_Error gets raised. Note that we will never free
966 such memory, so we presume it never will get allocated. */
968 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
969 global_bindings_p () || !definition
970 || static_p)
971 || (gnu_size
972 && ! allocatable_size_p (gnu_size,
973 global_bindings_p () || !definition
974 || static_p)))
976 gnu_type = build_reference_type (gnu_type);
977 gnu_size = NULL_TREE;
978 used_by_ref = true;
979 const_flag = true;
981 /* In case this was a aliased object whose nominal subtype is
982 unconstrained, the pointer above will be a thin pointer and
983 build_allocator will automatically make the template.
985 If we have a template initializer only (that we made above),
986 pretend there is none and rely on what build_allocator creates
987 again anyway. Otherwise (if we have a full initializer), get
988 the data part and feed that to build_allocator.
990 If we are elaborating a mutable object, tell build_allocator to
991 ignore a possibly simpler size from the initializer, if any, as
992 we must allocate the maximum possible size in this case. */
994 if (definition)
996 tree gnu_alloc_type = TREE_TYPE (gnu_type);
998 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
999 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1001 gnu_alloc_type
1002 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1004 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1005 && VEC_length (constructor_elt,
1006 CONSTRUCTOR_ELTS (gnu_expr)) == 1)
1007 gnu_expr = 0;
1008 else
1009 gnu_expr
1010 = build_component_ref
1011 (gnu_expr, NULL_TREE,
1012 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1013 false);
1016 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1017 && TREE_CONSTANT_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1018 && !Is_Imported (gnat_entity))
1019 post_error ("Storage_Error will be raised at run-time?",
1020 gnat_entity);
1022 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1023 0, 0, gnat_entity, mutable_p);
1025 else
1027 gnu_expr = NULL_TREE;
1028 const_flag = false;
1032 /* If this object would go into the stack and has an alignment
1033 larger than the default largest alignment, make a variable
1034 to hold the "aligning type" with a modified initial value,
1035 if any, then point to it and make that the value of this
1036 variable, which is now indirect. */
1037 if (!global_bindings_p () && !static_p && definition
1038 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1040 tree gnu_new_type
1041 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1042 TYPE_SIZE_UNIT (gnu_type));
1043 tree gnu_new_var;
1045 gnu_new_var
1046 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1047 NULL_TREE, gnu_new_type, gnu_expr, false,
1048 false, false, false, NULL, gnat_entity);
1050 if (gnu_expr)
1051 add_stmt_with_node
1052 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1053 build_component_ref
1054 (gnu_new_var, NULL_TREE,
1055 TYPE_FIELDS (gnu_new_type), false),
1056 gnu_expr),
1057 gnat_entity);
1059 gnu_type = build_reference_type (gnu_type);
1060 gnu_expr
1061 = build_unary_op
1062 (ADDR_EXPR, gnu_type,
1063 build_component_ref (gnu_new_var, NULL_TREE,
1064 TYPE_FIELDS (gnu_new_type), false));
1066 gnu_size = NULL_TREE;
1067 used_by_ref = true;
1068 const_flag = true;
1071 if (const_flag)
1072 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1073 | TYPE_QUAL_CONST));
1075 /* Convert the expression to the type of the object except in the
1076 case where the object's type is unconstrained or the object's type
1077 is a padded record whose field is of self-referential size. In
1078 the former case, converting will generate unnecessary evaluations
1079 of the CONSTRUCTOR to compute the size and in the latter case, we
1080 want to only copy the actual data. */
1081 if (gnu_expr
1082 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1083 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1084 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1085 && TYPE_IS_PADDING_P (gnu_type)
1086 && (CONTAINS_PLACEHOLDER_P
1087 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1088 gnu_expr = convert (gnu_type, gnu_expr);
1090 /* If this name is external or there was a name specified, use it,
1091 unless this is a VMS exception object since this would conflict
1092 with the symbol we need to export in addition. Don't use the
1093 Interface_Name if there is an address clause (see CD30005). */
1094 if (!Is_VMS_Exception (gnat_entity)
1095 && ((Present (Interface_Name (gnat_entity))
1096 && No (Address_Clause (gnat_entity)))
1097 || (Is_Public (gnat_entity)
1098 && (!Is_Imported (gnat_entity)
1099 || Is_Exported (gnat_entity)))))
1100 gnu_ext_name = create_concat_name (gnat_entity, 0);
1102 /* If this is constant initialized to a static constant and the
1103 object has an aggregate type, force it to be statically
1104 allocated. */
1105 if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1106 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1107 && (AGGREGATE_TYPE_P (gnu_type)
1108 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1109 && TYPE_IS_PADDING_P (gnu_type))))
1110 static_p = true;
1112 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1113 gnu_expr, const_flag,
1114 Is_Public (gnat_entity),
1115 imported_p || !definition,
1116 static_p, attr_list, gnat_entity);
1117 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1118 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1119 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1121 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1122 DECL_RENAMING_GLOBAL_P (gnu_decl) = global_bindings_p ();
1125 /* If we have an address clause and we've made this indirect, it's
1126 not enough to merely mark the type as volatile since volatile
1127 references only conflict with other volatile references while this
1128 reference must conflict with all other references. So ensure that
1129 the dereferenced value has alias set 0. */
1130 if (Present (Address_Clause (gnat_entity)) && used_by_ref)
1131 DECL_POINTER_ALIAS_SET (gnu_decl) = 0;
1133 if (definition && DECL_SIZE (gnu_decl)
1134 && get_block_jmpbuf_decl ()
1135 && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1136 || (flag_stack_check && !STACK_CHECK_BUILTIN
1137 && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1138 STACK_CHECK_MAX_VAR_SIZE))))
1139 add_stmt_with_node (build_call_1_expr
1140 (update_setjmp_buf_decl,
1141 build_unary_op (ADDR_EXPR, NULL_TREE,
1142 get_block_jmpbuf_decl ())),
1143 gnat_entity);
1145 /* If this is a public constant or we're not optimizing and we're not
1146 making a VAR_DECL for it, make one just for export or debugger
1147 use. Likewise if the address is taken or if the object or type is
1148 aliased. */
1149 if (definition && TREE_CODE (gnu_decl) == CONST_DECL
1150 && (Is_Public (gnat_entity)
1151 || optimize == 0
1152 || Address_Taken (gnat_entity)
1153 || Is_Aliased (gnat_entity)
1154 || Is_Aliased (Etype (gnat_entity))))
1156 tree gnu_corr_var
1157 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1158 gnu_expr, false, Is_Public (gnat_entity),
1159 false, static_p, NULL, gnat_entity);
1161 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1164 /* If this is declared in a block that contains a block with an
1165 exception handler, we must force this variable in memory to
1166 suppress an invalid optimization. */
1167 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1168 && Exception_Mechanism != Back_End_Exceptions)
1169 TREE_ADDRESSABLE (gnu_decl) = 1;
1171 /* Back-annotate the Alignment of the object if not already in the
1172 tree. Likewise for Esize if the object is of a constant size.
1173 But if the "object" is actually a pointer to an object, the
1174 alignment and size are the same as the type, so don't back-annotate
1175 the values for the pointer. */
1176 if (!used_by_ref && Unknown_Alignment (gnat_entity))
1177 Set_Alignment (gnat_entity,
1178 UI_From_Int (DECL_ALIGN (gnu_decl) / BITS_PER_UNIT));
1180 if (!used_by_ref && Unknown_Esize (gnat_entity)
1181 && DECL_SIZE (gnu_decl))
1183 tree gnu_back_size = DECL_SIZE (gnu_decl);
1185 if (TREE_CODE (TREE_TYPE (gnu_decl)) == RECORD_TYPE
1186 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_decl)))
1187 gnu_back_size
1188 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN
1189 (TYPE_FIELDS (TREE_TYPE (gnu_decl)))));
1191 Set_Esize (gnat_entity, annotate_value (gnu_back_size));
1194 break;
1196 case E_Void:
1197 /* Return a TYPE_DECL for "void" that we previously made. */
1198 gnu_decl = void_type_decl_node;
1199 break;
1201 case E_Enumeration_Type:
1202 /* A special case, for the types Character and Wide_Character in
1203 Standard, we do not list all the literals. So if the literals
1204 are not specified, make this an unsigned type. */
1205 if (No (First_Literal (gnat_entity)))
1207 gnu_type = make_unsigned_type (esize);
1208 break;
1211 /* Normal case of non-character type, or non-Standard character type */
1213 /* Here we have a list of enumeral constants in First_Literal.
1214 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1215 the list to be places into TYPE_FIELDS. Each node in the list
1216 is a TREE_LIST node whose TREE_VALUE is the literal name
1217 and whose TREE_PURPOSE is the value of the literal.
1219 Esize contains the number of bits needed to represent the enumeral
1220 type, Type_Low_Bound also points to the first literal and
1221 Type_High_Bound points to the last literal. */
1223 Entity_Id gnat_literal;
1224 tree gnu_literal_list = NULL_TREE;
1226 if (Is_Unsigned_Type (gnat_entity))
1227 gnu_type = make_unsigned_type (esize);
1228 else
1229 gnu_type = make_signed_type (esize);
1231 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1233 for (gnat_literal = First_Literal (gnat_entity);
1234 Present (gnat_literal);
1235 gnat_literal = Next_Literal (gnat_literal))
1237 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1238 gnu_type);
1239 tree gnu_literal
1240 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1241 gnu_type, gnu_value, true, false, false,
1242 false, NULL, gnat_literal);
1244 save_gnu_tree (gnat_literal, gnu_literal, false);
1245 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1246 gnu_value, gnu_literal_list);
1249 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1251 /* Note that the bounds are updated at the end of this function
1252 because to avoid an infinite recursion when we get the bounds of
1253 this type, since those bounds are objects of this type. */
1255 break;
1257 case E_Signed_Integer_Type:
1258 case E_Ordinary_Fixed_Point_Type:
1259 case E_Decimal_Fixed_Point_Type:
1260 /* For integer types, just make a signed type the appropriate number
1261 of bits. */
1262 gnu_type = make_signed_type (esize);
1263 break;
1265 case E_Modular_Integer_Type:
1266 /* For modular types, make the unsigned type of the proper number of
1267 bits and then set up the modulus, if required. */
1269 enum machine_mode mode;
1270 tree gnu_modulus;
1271 tree gnu_high = 0;
1273 if (Is_Packed_Array_Type (gnat_entity))
1274 esize = UI_To_Int (RM_Size (gnat_entity));
1276 /* Find the smallest mode at least ESIZE bits wide and make a class
1277 using that mode. */
1279 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1280 GET_MODE_BITSIZE (mode) < esize;
1281 mode = GET_MODE_WIDER_MODE (mode))
1284 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1285 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1286 = Is_Packed_Array_Type (gnat_entity);
1288 /* Get the modulus in this type. If it overflows, assume it is because
1289 it is equal to 2**Esize. Note that there is no overflow checking
1290 done on unsigned type, so we detect the overflow by looking for
1291 a modulus of zero, which is otherwise invalid. */
1292 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1294 if (!integer_zerop (gnu_modulus))
1296 TYPE_MODULAR_P (gnu_type) = 1;
1297 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1298 gnu_high = fold (build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1299 convert (gnu_type, integer_one_node)));
1302 /* If we have to set TYPE_PRECISION different from its natural value,
1303 make a subtype to do do. Likewise if there is a modulus and
1304 it is not one greater than TYPE_MAX_VALUE. */
1305 if (TYPE_PRECISION (gnu_type) != esize
1306 || (TYPE_MODULAR_P (gnu_type)
1307 && !tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1309 tree gnu_subtype = make_node (INTEGER_TYPE);
1311 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1312 TREE_TYPE (gnu_subtype) = gnu_type;
1313 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1314 TYPE_MAX_VALUE (gnu_subtype)
1315 = TYPE_MODULAR_P (gnu_type)
1316 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1317 TYPE_PRECISION (gnu_subtype) = esize;
1318 TYPE_UNSIGNED (gnu_subtype) = 1;
1319 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1320 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1321 = Is_Packed_Array_Type (gnat_entity);
1322 layout_type (gnu_subtype);
1324 gnu_type = gnu_subtype;
1327 break;
1329 case E_Signed_Integer_Subtype:
1330 case E_Enumeration_Subtype:
1331 case E_Modular_Integer_Subtype:
1332 case E_Ordinary_Fixed_Point_Subtype:
1333 case E_Decimal_Fixed_Point_Subtype:
1335 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1336 that we do not want to call build_range_type since we would
1337 like each subtype node to be distinct. This will be important
1338 when memory aliasing is implemented.
1340 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1341 parent type; this fact is used by the arithmetic conversion
1342 functions.
1344 We elaborate the Ancestor_Subtype if it is not in the current
1345 unit and one of our bounds is non-static. We do this to ensure
1346 consistent naming in the case where several subtypes share the same
1347 bounds by always elaborating the first such subtype first, thus
1348 using its name. */
1350 if (definition == 0
1351 && Present (Ancestor_Subtype (gnat_entity))
1352 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1353 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1354 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1355 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1356 gnu_expr, definition);
1358 gnu_type = make_node (INTEGER_TYPE);
1359 if (Is_Packed_Array_Type (gnat_entity))
1361 esize = UI_To_Int (RM_Size (gnat_entity));
1362 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1365 TYPE_PRECISION (gnu_type) = esize;
1366 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1368 TYPE_MIN_VALUE (gnu_type)
1369 = convert (TREE_TYPE (gnu_type),
1370 elaborate_expression (Type_Low_Bound (gnat_entity),
1371 gnat_entity,
1372 get_identifier ("L"), definition, 1,
1373 Needs_Debug_Info (gnat_entity)));
1375 TYPE_MAX_VALUE (gnu_type)
1376 = convert (TREE_TYPE (gnu_type),
1377 elaborate_expression (Type_High_Bound (gnat_entity),
1378 gnat_entity,
1379 get_identifier ("U"), definition, 1,
1380 Needs_Debug_Info (gnat_entity)));
1382 /* One of the above calls might have caused us to be elaborated,
1383 so don't blow up if so. */
1384 if (present_gnu_tree (gnat_entity))
1386 maybe_present = true;
1387 break;
1390 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1391 = Has_Biased_Representation (gnat_entity);
1393 /* This should be an unsigned type if the lower bound is constant
1394 and non-negative or if the base type is unsigned; a signed type
1395 otherwise. */
1396 TYPE_UNSIGNED (gnu_type)
1397 = (TYPE_UNSIGNED (TREE_TYPE (gnu_type))
1398 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1399 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1400 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1401 || Is_Unsigned_Type (gnat_entity));
1403 layout_type (gnu_type);
1405 /* If the type we are dealing with is to represent a packed array,
1406 we need to have the bits left justified on big-endian targets
1407 and right justified on little-endian targets. We also need to
1408 ensure that when the value is read (e.g. for comparison of two
1409 such values), we only get the good bits, since the unused bits
1410 are uninitialized. Both goals are accomplished by wrapping the
1411 modular value in an enclosing struct. */
1412 if (Is_Packed_Array_Type (gnat_entity))
1414 tree gnu_field_type = gnu_type;
1415 tree gnu_field;
1417 TYPE_RM_SIZE_NUM (gnu_field_type)
1418 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1419 gnu_type = make_node (RECORD_TYPE);
1420 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1421 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1422 TYPE_PACKED (gnu_type) = 1;
1424 /* Create a stripped-down declaration of the original type, mainly
1425 for debugging. */
1426 create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1427 NULL, true, debug_info_p, gnat_entity);
1429 /* Don't notify the field as "addressable", since we won't be taking
1430 it's address and it would prevent create_field_decl from making a
1431 bitfield. */
1432 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1433 gnu_field_type, gnu_type, 1, 0, 0, 0);
1435 finish_record_type (gnu_type, gnu_field, false, false);
1436 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1437 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1440 break;
1442 case E_Floating_Point_Type:
1443 /* If this is a VAX floating-point type, use an integer of the proper
1444 size. All the operations will be handled with ASM statements. */
1445 if (Vax_Float (gnat_entity))
1447 gnu_type = make_signed_type (esize);
1448 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1449 SET_TYPE_DIGITS_VALUE (gnu_type,
1450 UI_To_gnu (Digits_Value (gnat_entity),
1451 sizetype));
1452 break;
1455 /* The type of the Low and High bounds can be our type if this is
1456 a type from Standard, so set them at the end of the function. */
1457 gnu_type = make_node (REAL_TYPE);
1458 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1459 layout_type (gnu_type);
1460 break;
1462 case E_Floating_Point_Subtype:
1463 if (Vax_Float (gnat_entity))
1465 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1466 break;
1470 if (definition == 0
1471 && Present (Ancestor_Subtype (gnat_entity))
1472 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1473 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1474 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1475 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1476 gnu_expr, definition);
1478 gnu_type = make_node (REAL_TYPE);
1479 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1480 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1482 TYPE_MIN_VALUE (gnu_type)
1483 = convert (TREE_TYPE (gnu_type),
1484 elaborate_expression (Type_Low_Bound (gnat_entity),
1485 gnat_entity, get_identifier ("L"),
1486 definition, 1,
1487 Needs_Debug_Info (gnat_entity)));
1489 TYPE_MAX_VALUE (gnu_type)
1490 = convert (TREE_TYPE (gnu_type),
1491 elaborate_expression (Type_High_Bound (gnat_entity),
1492 gnat_entity, get_identifier ("U"),
1493 definition, 1,
1494 Needs_Debug_Info (gnat_entity)));
1496 /* One of the above calls might have caused us to be elaborated,
1497 so don't blow up if so. */
1498 if (present_gnu_tree (gnat_entity))
1500 maybe_present = true;
1501 break;
1504 layout_type (gnu_type);
1506 break;
1508 /* Array and String Types and Subtypes
1510 Unconstrained array types are represented by E_Array_Type and
1511 constrained array types are represented by E_Array_Subtype. There
1512 are no actual objects of an unconstrained array type; all we have
1513 are pointers to that type.
1515 The following fields are defined on array types and subtypes:
1517 Component_Type Component type of the array.
1518 Number_Dimensions Number of dimensions (an int).
1519 First_Index Type of first index. */
1521 case E_String_Type:
1522 case E_Array_Type:
1524 tree gnu_template_fields = NULL_TREE;
1525 tree gnu_template_type = make_node (RECORD_TYPE);
1526 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1527 tree gnu_fat_type = make_node (RECORD_TYPE);
1528 int ndim = Number_Dimensions (gnat_entity);
1529 int firstdim
1530 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1531 int nextdim
1532 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1533 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1534 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1535 tree gnu_comp_size = 0;
1536 tree gnu_max_size = size_one_node;
1537 tree gnu_max_size_unit;
1538 int index;
1539 Entity_Id gnat_ind_subtype;
1540 Entity_Id gnat_ind_base_subtype;
1541 tree gnu_template_reference;
1542 tree tem;
1544 TYPE_NAME (gnu_template_type)
1545 = create_concat_name (gnat_entity, "XUB");
1546 TYPE_NAME (gnu_fat_type) = create_concat_name (gnat_entity, "XUP");
1547 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1548 TYPE_READONLY (gnu_template_type) = 1;
1550 /* Make a node for the array. If we are not defining the array
1551 suppress expanding incomplete types. */
1552 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1554 if (!definition)
1555 defer_incomplete_level++, this_deferred = true;
1557 /* Build the fat pointer type. Use a "void *" object instead of
1558 a pointer to the array type since we don't have the array type
1559 yet (it will reference the fat pointer via the bounds). */
1560 tem = chainon (chainon (NULL_TREE,
1561 create_field_decl (get_identifier ("P_ARRAY"),
1562 ptr_void_type_node,
1563 gnu_fat_type, 0, 0, 0, 0)),
1564 create_field_decl (get_identifier ("P_BOUNDS"),
1565 gnu_ptr_template,
1566 gnu_fat_type, 0, 0, 0, 0));
1568 /* Make sure we can put this into a register. */
1569 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1570 finish_record_type (gnu_fat_type, tem, false, true);
1572 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1573 is the fat pointer. This will be used to access the individual
1574 fields once we build them. */
1575 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1576 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1577 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1578 gnu_template_reference
1579 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1580 TREE_READONLY (gnu_template_reference) = 1;
1582 /* Now create the GCC type for each index and add the fields for
1583 that index to the template. */
1584 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1585 gnat_ind_base_subtype
1586 = First_Index (Implementation_Base_Type (gnat_entity));
1587 index < ndim && index >= 0;
1588 index += nextdim,
1589 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1590 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1592 char field_name[10];
1593 tree gnu_ind_subtype
1594 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1595 tree gnu_base_subtype
1596 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1597 tree gnu_base_min
1598 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1599 tree gnu_base_max
1600 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1601 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1603 /* Make the FIELD_DECLs for the minimum and maximum of this
1604 type and then make extractions of that field from the
1605 template. */
1606 sprintf (field_name, "LB%d", index);
1607 gnu_min_field = create_field_decl (get_identifier (field_name),
1608 gnu_ind_subtype,
1609 gnu_template_type, 0, 0, 0, 0);
1610 field_name[0] = 'U';
1611 gnu_max_field = create_field_decl (get_identifier (field_name),
1612 gnu_ind_subtype,
1613 gnu_template_type, 0, 0, 0, 0);
1615 Sloc_to_locus (Sloc (gnat_entity),
1616 &DECL_SOURCE_LOCATION (gnu_min_field));
1617 Sloc_to_locus (Sloc (gnat_entity),
1618 &DECL_SOURCE_LOCATION (gnu_max_field));
1619 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1621 /* We can't use build_component_ref here since the template
1622 type isn't complete yet. */
1623 gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1624 gnu_template_reference, gnu_min_field,
1625 NULL_TREE);
1626 gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1627 gnu_template_reference, gnu_max_field,
1628 NULL_TREE);
1629 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1631 /* Make a range type with the new ranges, but using
1632 the Ada subtype. Then we convert to sizetype. */
1633 gnu_index_types[index]
1634 = create_index_type (convert (sizetype, gnu_min),
1635 convert (sizetype, gnu_max),
1636 build_range_type (gnu_ind_subtype,
1637 gnu_min, gnu_max));
1638 /* Update the maximum size of the array, in elements. */
1639 gnu_max_size
1640 = size_binop (MULT_EXPR, gnu_max_size,
1641 size_binop (PLUS_EXPR, size_one_node,
1642 size_binop (MINUS_EXPR, gnu_base_max,
1643 gnu_base_min)));
1645 TYPE_NAME (gnu_index_types[index])
1646 = create_concat_name (gnat_entity, field_name);
1649 for (index = 0; index < ndim; index++)
1650 gnu_template_fields
1651 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1653 /* Install all the fields into the template. */
1654 finish_record_type (gnu_template_type, gnu_template_fields,
1655 false, false);
1656 TYPE_READONLY (gnu_template_type) = 1;
1658 /* Now make the array of arrays and update the pointer to the array
1659 in the fat pointer. Note that it is the first field. */
1661 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1663 /* Get and validate any specified Component_Size, but if Packed,
1664 ignore it since the front end will have taken care of it. */
1665 gnu_comp_size
1666 = validate_size (Component_Size (gnat_entity), tem,
1667 gnat_entity,
1668 (Is_Bit_Packed_Array (gnat_entity)
1669 ? TYPE_DECL : VAR_DECL),
1670 true, Has_Component_Size_Clause (gnat_entity));
1672 if (Has_Atomic_Components (gnat_entity))
1673 check_ok_for_atomic (tem, gnat_entity, true);
1675 /* If the component type is a RECORD_TYPE that has a self-referential
1676 size, use the maxium size. */
1677 if (!gnu_comp_size && TREE_CODE (tem) == RECORD_TYPE
1678 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1679 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1681 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
1683 tem = make_type_from_size (tem, gnu_comp_size, false);
1684 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1685 "C_PAD", false, definition, true);
1688 if (Has_Volatile_Components (gnat_entity))
1689 tem = build_qualified_type (tem,
1690 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1692 /* If Component_Size is not already specified, annotate it with the
1693 size of the component. */
1694 if (Unknown_Component_Size (gnat_entity))
1695 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1697 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1698 size_binop (MULT_EXPR, gnu_max_size,
1699 TYPE_SIZE_UNIT (tem)));
1700 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1701 size_binop (MULT_EXPR,
1702 convert (bitsizetype,
1703 gnu_max_size),
1704 TYPE_SIZE (tem)));
1706 for (index = ndim - 1; index >= 0; index--)
1708 tem = build_array_type (tem, gnu_index_types[index]);
1709 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1711 /* If the type below this an multi-array type, then this
1712 does not not have aliased components.
1714 ??? Otherwise, for now, we say that any component of aggregate
1715 type is addressable because the front end may take 'Reference
1716 of it. But we have to make it addressable if it must be passed
1717 by reference or it that is the default. */
1718 TYPE_NONALIASED_COMPONENT (tem)
1719 = ((TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
1720 && TYPE_MULTI_ARRAY_P (TREE_TYPE (tem))) ? 1
1721 : (!Has_Aliased_Components (gnat_entity)
1722 && !AGGREGATE_TYPE_P (TREE_TYPE (tem))));
1725 /* If an alignment is specified, use it if valid. But ignore it for
1726 types that represent the unpacked base type for packed arrays. */
1727 if (No (Packed_Array_Type (gnat_entity))
1728 && Known_Alignment (gnat_entity))
1730 gcc_assert (Present (Alignment (gnat_entity)));
1731 TYPE_ALIGN (tem)
1732 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1733 TYPE_ALIGN (tem));
1736 TYPE_CONVENTION_FORTRAN_P (tem)
1737 = (Convention (gnat_entity) == Convention_Fortran);
1738 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1740 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1741 corresponding fat pointer. */
1742 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1743 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1744 TYPE_MODE (gnu_type) = BLKmode;
1745 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1746 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
1748 /* If the maximum size doesn't overflow, use it. */
1749 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1750 && !TREE_OVERFLOW (gnu_max_size))
1751 TYPE_SIZE (tem)
1752 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1753 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
1754 && !TREE_OVERFLOW (gnu_max_size_unit))
1755 TYPE_SIZE_UNIT (tem)
1756 = size_binop (MIN_EXPR, gnu_max_size_unit,
1757 TYPE_SIZE_UNIT (tem));
1759 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1760 tem, NULL, !Comes_From_Source (gnat_entity),
1761 debug_info_p, gnat_entity);
1763 /* Create a record type for the object and its template and
1764 set the template at a negative offset. */
1765 tem = build_unc_object_type (gnu_template_type, tem,
1766 create_concat_name (gnat_entity, "XUT"));
1767 DECL_FIELD_OFFSET (TYPE_FIELDS (tem))
1768 = size_binop (MINUS_EXPR, size_zero_node,
1769 byte_position (TREE_CHAIN (TYPE_FIELDS (tem))));
1770 DECL_FIELD_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem))) = size_zero_node;
1771 DECL_FIELD_BIT_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem)))
1772 = bitsize_zero_node;
1773 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
1774 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1776 /* Give the thin pointer type a name. */
1777 create_type_decl (create_concat_name (gnat_entity, "XUX"),
1778 build_pointer_type (tem), NULL,
1779 !Comes_From_Source (gnat_entity), debug_info_p,
1780 gnat_entity);
1782 break;
1784 case E_String_Subtype:
1785 case E_Array_Subtype:
1787 /* This is the actual data type for array variables. Multidimensional
1788 arrays are implemented in the gnu tree as arrays of arrays. Note
1789 that for the moment arrays which have sparse enumeration subtypes as
1790 index components create sparse arrays, which is obviously space
1791 inefficient but so much easier to code for now.
1793 Also note that the subtype never refers to the unconstrained
1794 array type, which is somewhat at variance with Ada semantics.
1796 First check to see if this is simply a renaming of the array
1797 type. If so, the result is the array type. */
1799 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1800 if (!Is_Constrained (gnat_entity))
1801 break;
1802 else
1804 int index;
1805 int array_dim = Number_Dimensions (gnat_entity);
1806 int first_dim
1807 = ((Convention (gnat_entity) == Convention_Fortran)
1808 ? array_dim - 1 : 0);
1809 int next_dim
1810 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
1811 Entity_Id gnat_ind_subtype;
1812 Entity_Id gnat_ind_base_subtype;
1813 tree gnu_base_type = gnu_type;
1814 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
1815 tree gnu_comp_size = NULL_TREE;
1816 tree gnu_max_size = size_one_node;
1817 tree gnu_max_size_unit;
1818 bool need_index_type_struct = false;
1819 bool max_overflow = false;
1821 /* First create the gnu types for each index. Create types for
1822 debugging information to point to the index types if the
1823 are not integer types, have variable bounds, or are
1824 wider than sizetype. */
1826 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1827 gnat_ind_base_subtype
1828 = First_Index (Implementation_Base_Type (gnat_entity));
1829 index < array_dim && index >= 0;
1830 index += next_dim,
1831 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1832 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1834 tree gnu_index_subtype
1835 = get_unpadded_type (Etype (gnat_ind_subtype));
1836 tree gnu_min
1837 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
1838 tree gnu_max
1839 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
1840 tree gnu_base_subtype
1841 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1842 tree gnu_base_min
1843 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1844 tree gnu_base_max
1845 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1846 tree gnu_base_type = get_base_type (gnu_base_subtype);
1847 tree gnu_base_base_min
1848 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
1849 tree gnu_base_base_max
1850 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
1851 tree gnu_high;
1852 tree gnu_this_max;
1854 /* If the minimum and maximum values both overflow in
1855 SIZETYPE, but the difference in the original type
1856 does not overflow in SIZETYPE, ignore the overflow
1857 indications. */
1858 if ((TYPE_PRECISION (gnu_index_subtype)
1859 > TYPE_PRECISION (sizetype)
1860 || TYPE_UNSIGNED (gnu_index_subtype)
1861 != TYPE_UNSIGNED (sizetype))
1862 && TREE_CODE (gnu_min) == INTEGER_CST
1863 && TREE_CODE (gnu_max) == INTEGER_CST
1864 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
1865 && (!TREE_OVERFLOW
1866 (fold (build2 (MINUS_EXPR, gnu_index_subtype,
1867 TYPE_MAX_VALUE (gnu_index_subtype),
1868 TYPE_MIN_VALUE (gnu_index_subtype))))))
1869 TREE_OVERFLOW (gnu_min) = TREE_OVERFLOW (gnu_max)
1870 = TREE_CONSTANT_OVERFLOW (gnu_min)
1871 = TREE_CONSTANT_OVERFLOW (gnu_max) = 0;
1873 /* Similarly, if the range is null, use bounds of 1..0 for
1874 the sizetype bounds. */
1875 else if ((TYPE_PRECISION (gnu_index_subtype)
1876 > TYPE_PRECISION (sizetype)
1877 || TYPE_UNSIGNED (gnu_index_subtype)
1878 != TYPE_UNSIGNED (sizetype))
1879 && TREE_CODE (gnu_min) == INTEGER_CST
1880 && TREE_CODE (gnu_max) == INTEGER_CST
1881 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
1882 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
1883 TYPE_MIN_VALUE (gnu_index_subtype)))
1884 gnu_min = size_one_node, gnu_max = size_zero_node;
1886 /* Now compute the size of this bound. We need to provide
1887 GCC with an upper bound to use but have to deal with the
1888 "superflat" case. There are three ways to do this. If we
1889 can prove that the array can never be superflat, we can
1890 just use the high bound of the index subtype. If we can
1891 prove that the low bound minus one can't overflow, we
1892 can do this as MAX (hb, lb - 1). Otherwise, we have to use
1893 the expression hb >= lb ? hb : lb - 1. */
1894 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
1896 /* See if the base array type is already flat. If it is, we
1897 are probably compiling an ACVC test, but it will cause the
1898 code below to malfunction if we don't handle it specially. */
1899 if (TREE_CODE (gnu_base_min) == INTEGER_CST
1900 && TREE_CODE (gnu_base_max) == INTEGER_CST
1901 && !TREE_CONSTANT_OVERFLOW (gnu_base_min)
1902 && !TREE_CONSTANT_OVERFLOW (gnu_base_max)
1903 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
1904 gnu_high = size_zero_node, gnu_min = size_one_node;
1906 /* If gnu_high is now an integer which overflowed, the array
1907 cannot be superflat. */
1908 else if (TREE_CODE (gnu_high) == INTEGER_CST
1909 && TREE_OVERFLOW (gnu_high))
1910 gnu_high = gnu_max;
1911 else if (TYPE_UNSIGNED (gnu_base_subtype)
1912 || TREE_CODE (gnu_high) == INTEGER_CST)
1913 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
1914 else
1915 gnu_high
1916 = build_cond_expr
1917 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
1918 gnu_max, gnu_min),
1919 gnu_max, gnu_high);
1921 gnu_index_type[index]
1922 = create_index_type (gnu_min, gnu_high, gnu_index_subtype);
1924 /* Also compute the maximum size of the array. Here we
1925 see if any constraint on the index type of the base type
1926 can be used in the case of self-referential bound on
1927 the index type of the subtype. We look for a non-"infinite"
1928 and non-self-referential bound from any type involved and
1929 handle each bound separately. */
1931 if ((TREE_CODE (gnu_min) == INTEGER_CST
1932 && !TREE_OVERFLOW (gnu_min)
1933 && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
1934 || !CONTAINS_PLACEHOLDER_P (gnu_min))
1935 gnu_base_min = gnu_min;
1937 if ((TREE_CODE (gnu_max) == INTEGER_CST
1938 && !TREE_OVERFLOW (gnu_max)
1939 && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
1940 || !CONTAINS_PLACEHOLDER_P (gnu_max))
1941 gnu_base_max = gnu_max;
1943 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
1944 && TREE_CONSTANT_OVERFLOW (gnu_base_min))
1945 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
1946 || (TREE_CODE (gnu_base_max) == INTEGER_CST
1947 && TREE_CONSTANT_OVERFLOW (gnu_base_max))
1948 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
1949 max_overflow = true;
1951 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
1952 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
1954 gnu_this_max
1955 = size_binop (MAX_EXPR,
1956 size_binop (PLUS_EXPR, size_one_node,
1957 size_binop (MINUS_EXPR, gnu_base_max,
1958 gnu_base_min)),
1959 size_zero_node);
1961 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1962 && TREE_CONSTANT_OVERFLOW (gnu_this_max))
1963 max_overflow = true;
1965 gnu_max_size
1966 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1968 if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
1969 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
1970 != INTEGER_CST)
1971 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
1972 || (TREE_TYPE (gnu_index_subtype)
1973 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
1974 != INTEGER_TYPE))
1975 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
1976 || (TYPE_PRECISION (gnu_index_subtype)
1977 > TYPE_PRECISION (sizetype)))
1978 need_index_type_struct = true;
1981 /* Then flatten: create the array of arrays. */
1983 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
1985 /* One of the above calls might have caused us to be elaborated,
1986 so don't blow up if so. */
1987 if (present_gnu_tree (gnat_entity))
1989 maybe_present = true;
1990 break;
1993 /* Get and validate any specified Component_Size, but if Packed,
1994 ignore it since the front end will have taken care of it. */
1995 gnu_comp_size
1996 = validate_size (Component_Size (gnat_entity), gnu_type,
1997 gnat_entity,
1998 (Is_Bit_Packed_Array (gnat_entity)
1999 ? TYPE_DECL : VAR_DECL),
2000 true, Has_Component_Size_Clause (gnat_entity));
2002 /* If the component type is a RECORD_TYPE that has a self-referential
2003 size, use the maxium size. */
2004 if (!gnu_comp_size && TREE_CODE (gnu_type) == RECORD_TYPE
2005 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2006 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
2008 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
2010 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
2011 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
2012 gnat_entity, "C_PAD", false,
2013 definition, true);
2016 if (Has_Volatile_Components (Base_Type (gnat_entity)))
2017 gnu_type = build_qualified_type (gnu_type,
2018 (TYPE_QUALS (gnu_type)
2019 | TYPE_QUAL_VOLATILE));
2021 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2022 TYPE_SIZE_UNIT (gnu_type));
2023 gnu_max_size = size_binop (MULT_EXPR,
2024 convert (bitsizetype, gnu_max_size),
2025 TYPE_SIZE (gnu_type));
2027 for (index = array_dim - 1; index >= 0; index --)
2029 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
2030 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2031 /* If the type below this an multi-array type, then this
2032 does not not have aliased components.
2034 ??? Otherwise, for now, we say that any component of aggregate
2035 type is addressable because the front end may take 'Reference
2036 of it. But we have to make it addressable if it must be passed
2037 by reference or it that is the default. */
2038 TYPE_NONALIASED_COMPONENT (gnu_type)
2039 = ((TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
2040 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type))) ? 1
2041 : (!Has_Aliased_Components (gnat_entity)
2042 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_type))));
2045 /* If we are at file level and this is a multi-dimensional array, we
2046 need to make a variable corresponding to the stride of the
2047 inner dimensions. */
2048 if (global_bindings_p () && array_dim > 1)
2050 tree gnu_str_name = get_identifier ("ST");
2051 tree gnu_arr_type;
2053 for (gnu_arr_type = TREE_TYPE (gnu_type);
2054 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2055 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2056 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
2058 tree eltype = TREE_TYPE (gnu_arr_type);
2060 TYPE_SIZE (gnu_arr_type)
2061 = elaborate_expression_1 (gnat_entity, gnat_entity,
2062 TYPE_SIZE (gnu_arr_type),
2063 gnu_str_name, definition, 0);
2065 /* ??? For now, store the size as a multiple of the
2066 alignment of the element type in bytes so that we
2067 can see the alignment from the tree. */
2068 TYPE_SIZE_UNIT (gnu_arr_type)
2069 = build_binary_op
2070 (MULT_EXPR, sizetype,
2071 elaborate_expression_1
2072 (gnat_entity, gnat_entity,
2073 build_binary_op (EXACT_DIV_EXPR, sizetype,
2074 TYPE_SIZE_UNIT (gnu_arr_type),
2075 size_int (TYPE_ALIGN (eltype)
2076 / BITS_PER_UNIT)),
2077 concat_id_with_name (gnu_str_name, "A_U"),
2078 definition, 0),
2079 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2083 /* If we need to write out a record type giving the names of
2084 the bounds, do it now. */
2085 if (need_index_type_struct && debug_info_p)
2087 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
2088 tree gnu_field_list = NULL_TREE;
2089 tree gnu_field;
2091 TYPE_NAME (gnu_bound_rec_type)
2092 = create_concat_name (gnat_entity, "XA");
2094 for (index = array_dim - 1; index >= 0; index--)
2096 tree gnu_type_name
2097 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
2099 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
2100 gnu_type_name = DECL_NAME (gnu_type_name);
2102 gnu_field = create_field_decl (gnu_type_name,
2103 integer_type_node,
2104 gnu_bound_rec_type,
2105 0, NULL_TREE, NULL_TREE, 0);
2106 TREE_CHAIN (gnu_field) = gnu_field_list;
2107 gnu_field_list = gnu_field;
2110 finish_record_type (gnu_bound_rec_type, gnu_field_list,
2111 false, false);
2114 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2115 = (Convention (gnat_entity) == Convention_Fortran);
2116 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2117 = Is_Packed_Array_Type (gnat_entity);
2119 /* If our size depends on a placeholder and the maximum size doesn't
2120 overflow, use it. */
2121 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2122 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2123 && TREE_OVERFLOW (gnu_max_size))
2124 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2125 && TREE_OVERFLOW (gnu_max_size_unit))
2126 && !max_overflow)
2128 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2129 TYPE_SIZE (gnu_type));
2130 TYPE_SIZE_UNIT (gnu_type)
2131 = size_binop (MIN_EXPR, gnu_max_size_unit,
2132 TYPE_SIZE_UNIT (gnu_type));
2135 /* Set our alias set to that of our base type. This gives all
2136 array subtypes the same alias set. */
2137 copy_alias_set (gnu_type, gnu_base_type);
2140 /* If this is a packed type, make this type the same as the packed
2141 array type, but do some adjusting in the type first. */
2143 if (Present (Packed_Array_Type (gnat_entity)))
2145 Entity_Id gnat_index;
2146 tree gnu_inner_type;
2148 /* First finish the type we had been making so that we output
2149 debugging information for it */
2150 gnu_type
2151 = build_qualified_type (gnu_type,
2152 (TYPE_QUALS (gnu_type)
2153 | (TYPE_QUAL_VOLATILE
2154 * Treat_As_Volatile (gnat_entity))));
2155 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2156 !Comes_From_Source (gnat_entity),
2157 debug_info_p, gnat_entity);
2158 if (!Comes_From_Source (gnat_entity))
2159 DECL_ARTIFICIAL (gnu_decl) = 1;
2161 /* Save it as our equivalent in case the call below elaborates
2162 this type again. */
2163 save_gnu_tree (gnat_entity, gnu_decl, false);
2165 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2166 NULL_TREE, 0);
2167 this_made_decl = true;
2168 gnu_inner_type = gnu_type = TREE_TYPE (gnu_decl);
2169 save_gnu_tree (gnat_entity, NULL_TREE, false);
2171 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2172 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2173 || TYPE_IS_PADDING_P (gnu_inner_type)))
2174 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2176 /* We need to point the type we just made to our index type so
2177 the actual bounds can be put into a template. */
2179 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2180 && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2181 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2182 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2184 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2186 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2187 If it is, we need to make another type. */
2188 if (TYPE_MODULAR_P (gnu_inner_type))
2190 tree gnu_subtype;
2192 gnu_subtype = make_node (INTEGER_TYPE);
2194 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2195 TYPE_MIN_VALUE (gnu_subtype)
2196 = TYPE_MIN_VALUE (gnu_inner_type);
2197 TYPE_MAX_VALUE (gnu_subtype)
2198 = TYPE_MAX_VALUE (gnu_inner_type);
2199 TYPE_PRECISION (gnu_subtype)
2200 = TYPE_PRECISION (gnu_inner_type);
2201 TYPE_UNSIGNED (gnu_subtype)
2202 = TYPE_UNSIGNED (gnu_inner_type);
2203 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2204 layout_type (gnu_subtype);
2206 gnu_inner_type = gnu_subtype;
2209 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2212 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2214 for (gnat_index = First_Index (gnat_entity);
2215 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2216 SET_TYPE_ACTUAL_BOUNDS
2217 (gnu_inner_type,
2218 tree_cons (NULL_TREE,
2219 get_unpadded_type (Etype (gnat_index)),
2220 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2222 if (Convention (gnat_entity) != Convention_Fortran)
2223 SET_TYPE_ACTUAL_BOUNDS
2224 (gnu_inner_type,
2225 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2227 if (TREE_CODE (gnu_type) == RECORD_TYPE
2228 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2229 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2233 /* Abort if packed array with no packed array type field set. */
2234 else
2235 gcc_assert (!Is_Packed (gnat_entity));
2237 break;
2239 case E_String_Literal_Subtype:
2240 /* Create the type for a string literal. */
2242 Entity_Id gnat_full_type
2243 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2244 && Present (Full_View (Etype (gnat_entity)))
2245 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2246 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2247 tree gnu_string_array_type
2248 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2249 tree gnu_string_index_type
2250 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2251 (TYPE_DOMAIN (gnu_string_array_type))));
2252 tree gnu_lower_bound
2253 = convert (gnu_string_index_type,
2254 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2255 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2256 tree gnu_length = ssize_int (length - 1);
2257 tree gnu_upper_bound
2258 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2259 gnu_lower_bound,
2260 convert (gnu_string_index_type, gnu_length));
2261 tree gnu_range_type
2262 = build_range_type (gnu_string_index_type,
2263 gnu_lower_bound, gnu_upper_bound);
2264 tree gnu_index_type
2265 = create_index_type (convert (sizetype,
2266 TYPE_MIN_VALUE (gnu_range_type)),
2267 convert (sizetype,
2268 TYPE_MAX_VALUE (gnu_range_type)),
2269 gnu_range_type);
2271 gnu_type
2272 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2273 gnu_index_type);
2274 copy_alias_set (gnu_type, gnu_string_type);
2276 break;
2278 /* Record Types and Subtypes
2280 The following fields are defined on record types:
2282 Has_Discriminants True if the record has discriminants
2283 First_Discriminant Points to head of list of discriminants
2284 First_Entity Points to head of list of fields
2285 Is_Tagged_Type True if the record is tagged
2287 Implementation of Ada records and discriminated records:
2289 A record type definition is transformed into the equivalent of a C
2290 struct definition. The fields that are the discriminants which are
2291 found in the Full_Type_Declaration node and the elements of the
2292 Component_List found in the Record_Type_Definition node. The
2293 Component_List can be a recursive structure since each Variant of
2294 the Variant_Part of the Component_List has a Component_List.
2296 Processing of a record type definition comprises starting the list of
2297 field declarations here from the discriminants and the calling the
2298 function components_to_record to add the rest of the fields from the
2299 component list and return the gnu type node. The function
2300 components_to_record will call itself recursively as it traverses
2301 the tree. */
2303 case E_Record_Type:
2304 if (Has_Complex_Representation (gnat_entity))
2306 gnu_type
2307 = build_complex_type
2308 (get_unpadded_type
2309 (Etype (Defining_Entity
2310 (First (Component_Items
2311 (Component_List
2312 (Type_Definition
2313 (Declaration_Node (gnat_entity)))))))));
2315 break;
2319 Node_Id full_definition = Declaration_Node (gnat_entity);
2320 Node_Id record_definition = Type_Definition (full_definition);
2321 Entity_Id gnat_field;
2322 tree gnu_field;
2323 tree gnu_field_list = NULL_TREE;
2324 tree gnu_get_parent;
2325 int packed = (Is_Packed (gnat_entity) ? 1
2326 : (Component_Alignment (gnat_entity)
2327 == Calign_Storage_Unit) ? -1
2328 : 0);
2329 bool has_rep = Has_Specified_Layout (gnat_entity);
2330 bool all_rep = has_rep;
2331 bool is_extension
2332 = (Is_Tagged_Type (gnat_entity)
2333 && Nkind (record_definition) == N_Derived_Type_Definition);
2335 /* See if all fields have a rep clause. Stop when we find one
2336 that doesn't. */
2337 for (gnat_field = First_Entity (gnat_entity);
2338 Present (gnat_field) && all_rep;
2339 gnat_field = Next_Entity (gnat_field))
2340 if ((Ekind (gnat_field) == E_Component
2341 || Ekind (gnat_field) == E_Discriminant)
2342 && No (Component_Clause (gnat_field)))
2343 all_rep = false;
2345 /* If this is a record extension, go a level further to find the
2346 record definition. Also, verify we have a Parent_Subtype. */
2347 if (is_extension)
2349 if (!type_annotate_only
2350 || Present (Record_Extension_Part (record_definition)))
2351 record_definition = Record_Extension_Part (record_definition);
2353 gcc_assert (type_annotate_only
2354 || Present (Parent_Subtype (gnat_entity)));
2357 /* Make a node for the record. If we are not defining the record,
2358 suppress expanding incomplete types. We use the same RECORD_TYPE
2359 as for a dummy type and reset TYPE_DUMMY_P to show it's no longer
2360 a dummy.
2362 It is very tempting to delay resetting this bit until we are done
2363 with completing the type, e.g. to let possible intermediate
2364 elaboration of access types designating the record know it is not
2365 complete and arrange for update_pointer_to to fix things up later.
2367 It would be wrong, however, because dummy types are expected only
2368 to be created for Ada incomplete or private types, which is not
2369 what we have here. Doing so would make other parts of gigi think
2370 we are dealing with a really incomplete or private type, and have
2371 nasty side effects, typically on the generation of the associated
2372 debugging information. */
2373 gnu_type = make_dummy_type (gnat_entity);
2374 TYPE_DUMMY_P (gnu_type) = 0;
2376 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL && debug_info_p)
2377 DECL_IGNORED_P (TYPE_NAME (gnu_type)) = 0;
2379 TYPE_ALIGN (gnu_type) = 0;
2380 TYPE_PACKED (gnu_type) = packed || has_rep;
2382 if (!definition)
2383 defer_incomplete_level++, this_deferred = true;
2385 /* If both a size and rep clause was specified, put the size in
2386 the record type now so that it can get the proper mode. */
2387 if (has_rep && Known_Esize (gnat_entity))
2388 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2390 /* Always set the alignment here so that it can be used to
2391 set the mode, if it is making the alignment stricter. If
2392 it is invalid, it will be checked again below. If this is to
2393 be Atomic, choose a default alignment of a word unless we know
2394 the size and it's smaller. */
2395 if (Known_Alignment (gnat_entity))
2396 TYPE_ALIGN (gnu_type)
2397 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2398 else if (Is_Atomic (gnat_entity))
2399 TYPE_ALIGN (gnu_type)
2400 = (esize >= BITS_PER_WORD ? BITS_PER_WORD
2401 : 1 << (floor_log2 (esize - 1) + 1));
2403 /* If we have a Parent_Subtype, make a field for the parent. If
2404 this record has rep clauses, force the position to zero. */
2405 if (Present (Parent_Subtype (gnat_entity)))
2407 tree gnu_parent;
2409 /* A major complexity here is that the parent subtype will
2410 reference our discriminants. But those must reference
2411 the parent component of this record. So here we will
2412 initialize each of those components to a COMPONENT_REF.
2413 The first operand of that COMPONENT_REF is another
2414 COMPONENT_REF which will be filled in below, once
2415 the parent type can be safely built. */
2417 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2418 build0 (PLACEHOLDER_EXPR, gnu_type),
2419 build_decl (FIELD_DECL, NULL_TREE,
2420 NULL_TREE),
2421 NULL_TREE);
2423 if (Has_Discriminants (gnat_entity))
2424 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2425 Present (gnat_field);
2426 gnat_field = Next_Stored_Discriminant (gnat_field))
2427 if (Present (Corresponding_Discriminant (gnat_field)))
2428 save_gnu_tree
2429 (gnat_field,
2430 build3 (COMPONENT_REF,
2431 get_unpadded_type (Etype (gnat_field)),
2432 gnu_get_parent,
2433 gnat_to_gnu_field_decl (Corresponding_Discriminant
2434 (gnat_field)),
2435 NULL_TREE),
2436 true);
2438 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_entity));
2440 gnu_field_list
2441 = create_field_decl (get_identifier
2442 (Get_Name_String (Name_uParent)),
2443 gnu_parent, gnu_type, 0,
2444 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2445 has_rep ? bitsize_zero_node : 0, 1);
2446 DECL_INTERNAL_P (gnu_field_list) = 1;
2448 TREE_TYPE (gnu_get_parent) = gnu_parent;
2449 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2452 /* Add the fields for the discriminants into the record. */
2453 if (!Is_Unchecked_Union (gnat_entity)
2454 && Has_Discriminants (gnat_entity))
2455 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2456 Present (gnat_field);
2457 gnat_field = Next_Stored_Discriminant (gnat_field))
2459 /* If this is a record extension and this discriminant
2460 is the renaming of another discriminant, we've already
2461 handled the discriminant above. */
2462 if (Present (Parent_Subtype (gnat_entity))
2463 && Present (Corresponding_Discriminant (gnat_field)))
2464 continue;
2466 gnu_field
2467 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2469 /* Make an expression using a PLACEHOLDER_EXPR from the
2470 FIELD_DECL node just created and link that with the
2471 corresponding GNAT defining identifier. Then add to the
2472 list of fields. */
2473 save_gnu_tree (gnat_field,
2474 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2475 build0 (PLACEHOLDER_EXPR,
2476 DECL_CONTEXT (gnu_field)),
2477 gnu_field, NULL_TREE),
2478 true);
2480 TREE_CHAIN (gnu_field) = gnu_field_list;
2481 gnu_field_list = gnu_field;
2484 /* Put the discriminants into the record (backwards), so we can
2485 know the appropriate discriminant to use for the names of the
2486 variants. */
2487 TYPE_FIELDS (gnu_type) = gnu_field_list;
2489 /* Add the listed fields into the record and finish up. */
2490 components_to_record (gnu_type, Component_List (record_definition),
2491 gnu_field_list, packed, definition, NULL,
2492 false, all_rep, this_deferred);
2494 if (this_deferred)
2496 debug_deferred = true;
2497 defer_debug_level++;
2499 defer_debug_incomplete_list
2500 = tree_cons (NULL_TREE, gnu_type,
2501 defer_debug_incomplete_list);
2504 /* We used to remove the associations of the discriminants and
2505 _Parent for validity checking, but we may need them if there's
2506 Freeze_Node for a subtype used in this record. */
2508 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2509 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2511 /* If it is a tagged record force the type to BLKmode to insure
2512 that these objects will always be placed in memory. Do the
2513 same thing for limited record types. */
2514 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2515 TYPE_MODE (gnu_type) = BLKmode;
2517 /* If this is a derived type, we must make the alias set of this type
2518 the same as that of the type we are derived from. We assume here
2519 that the other type is already frozen. */
2520 if (Etype (gnat_entity) != gnat_entity
2521 && !(Is_Private_Type (Etype (gnat_entity))
2522 && Full_View (Etype (gnat_entity)) == gnat_entity))
2523 copy_alias_set (gnu_type, gnat_to_gnu_type (Etype (gnat_entity)));
2525 /* Fill in locations of fields. */
2526 annotate_rep (gnat_entity, gnu_type);
2528 /* If there are any entities in the chain corresponding to
2529 components that we did not elaborate, ensure we elaborate their
2530 types if they are Itypes. */
2531 for (gnat_temp = First_Entity (gnat_entity);
2532 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2533 if ((Ekind (gnat_temp) == E_Component
2534 || Ekind (gnat_temp) == E_Discriminant)
2535 && Is_Itype (Etype (gnat_temp))
2536 && !present_gnu_tree (gnat_temp))
2537 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2539 break;
2541 case E_Class_Wide_Subtype:
2542 /* If an equivalent type is present, that is what we should use.
2543 Otherwise, fall through to handle this like a record subtype
2544 since it may have constraints. */
2546 if (Present (Equivalent_Type (gnat_entity)))
2548 gnu_decl = gnat_to_gnu_entity (Equivalent_Type (gnat_entity),
2549 NULL_TREE, 0);
2550 maybe_present = true;
2551 break;
2554 /* ... fall through ... */
2556 case E_Record_Subtype:
2558 /* If Cloned_Subtype is Present it means this record subtype has
2559 identical layout to that type or subtype and we should use
2560 that GCC type for this one. The front end guarantees that
2561 the component list is shared. */
2562 if (Present (Cloned_Subtype (gnat_entity)))
2564 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2565 NULL_TREE, 0);
2566 maybe_present = true;
2569 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2570 changing the type, make a new type with each field having the
2571 type of the field in the new subtype but having the position
2572 computed by transforming every discriminant reference according
2573 to the constraints. We don't see any difference between
2574 private and nonprivate type here since derivations from types should
2575 have been deferred until the completion of the private type. */
2576 else
2578 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2579 tree gnu_base_type;
2580 tree gnu_orig_type;
2582 if (!definition)
2583 defer_incomplete_level++, this_deferred = true;
2585 /* Get the base type initially for its alignment and sizes. But
2586 if it is a padded type, we do all the other work with the
2587 unpadded type. */
2588 gnu_type = gnu_orig_type = gnu_base_type
2589 = gnat_to_gnu_type (gnat_base_type);
2591 if (TREE_CODE (gnu_type) == RECORD_TYPE
2592 && TYPE_IS_PADDING_P (gnu_type))
2593 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2595 if (present_gnu_tree (gnat_entity))
2597 maybe_present = true;
2598 break;
2601 /* When the type has discriminants, and these discriminants
2602 affect the shape of what it built, factor them in.
2604 If we are making a subtype of an Unchecked_Union (must be an
2605 Itype), just return the type.
2607 We can't just use Is_Constrained because private subtypes without
2608 discriminants of full types with discriminants with default
2609 expressions are Is_Constrained but aren't constrained! */
2611 if (IN (Ekind (gnat_base_type), Record_Kind)
2612 && !Is_For_Access_Subtype (gnat_entity)
2613 && !Is_Unchecked_Union (gnat_base_type)
2614 && Is_Constrained (gnat_entity)
2615 && Stored_Constraint (gnat_entity) != No_Elist
2616 && Present (Discriminant_Constraint (gnat_entity)))
2618 Entity_Id gnat_field;
2619 tree gnu_field_list = 0;
2620 tree gnu_pos_list
2621 = compute_field_positions (gnu_orig_type, NULL_TREE,
2622 size_zero_node, bitsize_zero_node,
2623 BIGGEST_ALIGNMENT);
2624 tree gnu_subst_list
2625 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2626 definition);
2627 tree gnu_temp;
2629 gnu_type = make_node (RECORD_TYPE);
2630 TYPE_NAME (gnu_type) = gnu_entity_id;
2631 TYPE_STUB_DECL (gnu_type)
2632 = create_type_decl (NULL_TREE, gnu_type, NULL, false, false,
2633 gnat_entity);
2634 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2636 for (gnat_field = First_Entity (gnat_entity);
2637 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2638 if ((Ekind (gnat_field) == E_Component
2639 || Ekind (gnat_field) == E_Discriminant)
2640 && (Underlying_Type (Scope (Original_Record_Component
2641 (gnat_field)))
2642 == gnat_base_type)
2643 && (No (Corresponding_Discriminant (gnat_field))
2644 || !Is_Tagged_Type (gnat_base_type)))
2646 tree gnu_old_field
2647 = gnat_to_gnu_field_decl (Original_Record_Component
2648 (gnat_field));
2649 tree gnu_offset
2650 = TREE_VALUE (purpose_member (gnu_old_field,
2651 gnu_pos_list));
2652 tree gnu_pos = TREE_PURPOSE (gnu_offset);
2653 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
2654 tree gnu_field_type
2655 = gnat_to_gnu_type (Etype (gnat_field));
2656 tree gnu_size = TYPE_SIZE (gnu_field_type);
2657 tree gnu_new_pos = 0;
2658 unsigned int offset_align
2659 = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
2661 tree gnu_field;
2663 /* If there was a component clause, the field types must be
2664 the same for the type and subtype, so copy the data from
2665 the old field to avoid recomputation here. Also if the
2666 field is justified modular and the optimization in
2667 gnat_to_gnu_field was applied. */
2668 if (Present (Component_Clause
2669 (Original_Record_Component (gnat_field)))
2670 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
2671 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
2672 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
2673 == TREE_TYPE (gnu_old_field)))
2675 gnu_size = DECL_SIZE (gnu_old_field);
2676 gnu_field_type = TREE_TYPE (gnu_old_field);
2679 /* If this was a bitfield, get the size from the old field.
2680 Also ensure the type can be placed into a bitfield. */
2681 else if (DECL_BIT_FIELD (gnu_old_field))
2683 gnu_size = DECL_SIZE (gnu_old_field);
2684 if (TYPE_MODE (gnu_field_type) == BLKmode
2685 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2686 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2687 gnu_field_type = make_packable_type (gnu_field_type);
2690 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
2691 for (gnu_temp = gnu_subst_list;
2692 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2693 gnu_pos = substitute_in_expr (gnu_pos,
2694 TREE_PURPOSE (gnu_temp),
2695 TREE_VALUE (gnu_temp));
2697 /* If the size is now a constant, we can set it as the
2698 size of the field when we make it. Otherwise, we need
2699 to deal with it specially. */
2700 if (TREE_CONSTANT (gnu_pos))
2701 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2703 gnu_field
2704 = create_field_decl
2705 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2706 0, gnu_size, gnu_new_pos,
2707 !DECL_NONADDRESSABLE_P (gnu_old_field));
2709 if (!TREE_CONSTANT (gnu_pos))
2711 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
2712 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2713 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
2714 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
2715 DECL_SIZE (gnu_field) = gnu_size;
2716 DECL_SIZE_UNIT (gnu_field)
2717 = convert (sizetype,
2718 size_binop (CEIL_DIV_EXPR, gnu_size,
2719 bitsize_unit_node));
2720 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2723 DECL_INTERNAL_P (gnu_field)
2724 = DECL_INTERNAL_P (gnu_old_field);
2725 SET_DECL_ORIGINAL_FIELD
2726 (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
2727 ? DECL_ORIGINAL_FIELD (gnu_old_field)
2728 : gnu_old_field));
2729 DECL_DISCRIMINANT_NUMBER (gnu_field)
2730 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2731 TREE_THIS_VOLATILE (gnu_field)
2732 = TREE_THIS_VOLATILE (gnu_old_field);
2733 TREE_CHAIN (gnu_field) = gnu_field_list;
2734 gnu_field_list = gnu_field;
2735 save_gnu_tree (gnat_field, gnu_field, false);
2738 /* Now go through the entities again looking for Itypes that
2739 we have not elaborated but should (e.g., Etypes of fields
2740 that have Original_Components). */
2741 for (gnat_field = First_Entity (gnat_entity);
2742 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2743 if ((Ekind (gnat_field) == E_Discriminant
2744 || Ekind (gnat_field) == E_Component)
2745 && !present_gnu_tree (Etype (gnat_field)))
2746 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
2748 finish_record_type (gnu_type, nreverse (gnu_field_list),
2749 true, false);
2751 /* Now set the size, alignment and alias set of the new type to
2752 match that of the old one, doing any substitutions, as
2753 above. */
2754 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2755 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2756 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
2757 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
2758 copy_alias_set (gnu_type, gnu_base_type);
2760 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2761 for (gnu_temp = gnu_subst_list;
2762 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2763 TYPE_SIZE (gnu_type)
2764 = substitute_in_expr (TYPE_SIZE (gnu_type),
2765 TREE_PURPOSE (gnu_temp),
2766 TREE_VALUE (gnu_temp));
2768 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
2769 for (gnu_temp = gnu_subst_list;
2770 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2771 TYPE_SIZE_UNIT (gnu_type)
2772 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
2773 TREE_PURPOSE (gnu_temp),
2774 TREE_VALUE (gnu_temp));
2776 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
2777 for (gnu_temp = gnu_subst_list;
2778 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2779 SET_TYPE_ADA_SIZE
2780 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
2781 TREE_PURPOSE (gnu_temp),
2782 TREE_VALUE (gnu_temp)));
2784 /* Recompute the mode of this record type now that we know its
2785 actual size. */
2786 compute_record_mode (gnu_type);
2788 /* Fill in locations of fields. */
2789 annotate_rep (gnat_entity, gnu_type);
2792 /* If we've made a new type, record it and make an XVS type to show
2793 what this is a subtype of. Some debuggers require the XVS
2794 type to be output first, so do it in that order. */
2795 if (gnu_type != gnu_orig_type)
2797 if (debug_info_p)
2799 tree gnu_subtype_marker = make_node (RECORD_TYPE);
2800 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
2802 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
2803 gnu_orig_name = DECL_NAME (gnu_orig_name);
2805 TYPE_NAME (gnu_subtype_marker)
2806 = create_concat_name (gnat_entity, "XVS");
2807 finish_record_type (gnu_subtype_marker,
2808 create_field_decl (gnu_orig_name,
2809 integer_type_node,
2810 gnu_subtype_marker,
2811 0, NULL_TREE,
2812 NULL_TREE, 0),
2813 false, false);
2816 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2817 TYPE_NAME (gnu_type) = gnu_entity_id;
2818 TYPE_STUB_DECL (gnu_type)
2819 = create_type_decl (TYPE_NAME (gnu_type), gnu_type,
2820 NULL, true, debug_info_p, gnat_entity);
2823 /* Otherwise, go down all the components in the new type and
2824 make them equivalent to those in the base type. */
2825 else
2826 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
2827 gnat_temp = Next_Entity (gnat_temp))
2828 if ((Ekind (gnat_temp) == E_Discriminant
2829 && !Is_Unchecked_Union (gnat_base_type))
2830 || Ekind (gnat_temp) == E_Component)
2831 save_gnu_tree (gnat_temp,
2832 gnat_to_gnu_field_decl
2833 (Original_Record_Component (gnat_temp)), false);
2835 break;
2837 case E_Access_Subprogram_Type:
2838 case E_Anonymous_Access_Subprogram_Type:
2839 /* If we are not defining this entity, and we have incomplete
2840 entities being processed above us, make a dummy type and
2841 fill it in later. */
2842 if (!definition && defer_incomplete_level != 0)
2844 struct incomplete *p
2845 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2847 gnu_type
2848 = build_pointer_type
2849 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2850 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2851 !Comes_From_Source (gnat_entity),
2852 debug_info_p, gnat_entity);
2853 save_gnu_tree (gnat_entity, gnu_decl, false);
2854 this_made_decl = saved = true;
2856 p->old_type = TREE_TYPE (gnu_type);
2857 p->full_type = Directly_Designated_Type (gnat_entity);
2858 p->next = defer_incomplete_list;
2859 defer_incomplete_list = p;
2860 break;
2863 /* ... fall through ... */
2865 case E_Allocator_Type:
2866 case E_Access_Type:
2867 case E_Access_Attribute_Type:
2868 case E_Anonymous_Access_Type:
2869 case E_General_Access_Type:
2871 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
2872 Entity_Id gnat_desig_full
2873 = ((IN (Ekind (Etype (gnat_desig_type)),
2874 Incomplete_Or_Private_Kind))
2875 ? Full_View (gnat_desig_type) : 0);
2876 /* We want to know if we'll be seeing the freeze node for any
2877 incomplete type we may be pointing to. */
2878 bool in_main_unit
2879 = (Present (gnat_desig_full)
2880 ? In_Extended_Main_Code_Unit (gnat_desig_full)
2881 : In_Extended_Main_Code_Unit (gnat_desig_type));
2882 bool got_fat_p = false;
2883 bool made_dummy = false;
2884 tree gnu_desig_type = NULL_TREE;
2885 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
2887 if (!targetm.valid_pointer_mode (p_mode))
2888 p_mode = ptr_mode;
2890 if (No (gnat_desig_full)
2891 && (Ekind (gnat_desig_type) == E_Class_Wide_Type
2892 || (Ekind (gnat_desig_type) == E_Class_Wide_Subtype
2893 && Present (Equivalent_Type (gnat_desig_type)))))
2895 if (Present (Equivalent_Type (gnat_desig_type)))
2897 gnat_desig_full = Equivalent_Type (gnat_desig_type);
2898 if (IN (Ekind (gnat_desig_full), Incomplete_Or_Private_Kind))
2899 gnat_desig_full = Full_View (gnat_desig_full);
2901 else if (IN (Ekind (Root_Type (gnat_desig_type)),
2902 Incomplete_Or_Private_Kind))
2903 gnat_desig_full = Full_View (Root_Type (gnat_desig_type));
2906 if (Present (gnat_desig_full) && Is_Concurrent_Type (gnat_desig_full))
2907 gnat_desig_full = Corresponding_Record_Type (gnat_desig_full);
2909 /* If either the designated type or its full view is an
2910 unconstrained array subtype, replace it with the type it's a
2911 subtype of. This avoids problems with multiple copies of
2912 unconstrained array types. */
2913 if (Ekind (gnat_desig_type) == E_Array_Subtype
2914 && !Is_Constrained (gnat_desig_type))
2915 gnat_desig_type = Etype (gnat_desig_type);
2916 if (Present (gnat_desig_full)
2917 && Ekind (gnat_desig_full) == E_Array_Subtype
2918 && !Is_Constrained (gnat_desig_full))
2919 gnat_desig_full = Etype (gnat_desig_full);
2921 /* If the designated type is a subtype of an incomplete record type,
2922 use the parent type to avoid order of elaboration issues. This
2923 can lose some code efficiency, but there is no alternative. */
2924 if (Present (gnat_desig_full)
2925 && Ekind (gnat_desig_full) == E_Record_Subtype
2926 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)
2927 gnat_desig_full = Etype (gnat_desig_full);
2929 /* If we are pointing to an incomplete type whose completion is an
2930 unconstrained array, make a fat pointer type instead of a pointer
2931 to VOID. The two types in our fields will be pointers to VOID and
2932 will be replaced in update_pointer_to. Similarly, if the type
2933 itself is a dummy type or an unconstrained array. Also make
2934 a dummy TYPE_OBJECT_RECORD_TYPE in case we have any thin
2935 pointers to it. */
2937 if ((Present (gnat_desig_full)
2938 && Is_Array_Type (gnat_desig_full)
2939 && !Is_Constrained (gnat_desig_full))
2940 || (present_gnu_tree (gnat_desig_type)
2941 && TYPE_IS_DUMMY_P (TREE_TYPE
2942 (get_gnu_tree (gnat_desig_type)))
2943 && Is_Array_Type (gnat_desig_type)
2944 && !Is_Constrained (gnat_desig_type))
2945 || (present_gnu_tree (gnat_desig_type)
2946 && (TREE_CODE (TREE_TYPE (get_gnu_tree (gnat_desig_type)))
2947 == UNCONSTRAINED_ARRAY_TYPE)
2948 && !(TYPE_POINTER_TO (TREE_TYPE
2949 (get_gnu_tree (gnat_desig_type)))))
2950 || (No (gnat_desig_full) && !in_main_unit
2951 && defer_incomplete_level
2952 && !present_gnu_tree (gnat_desig_type)
2953 && Is_Array_Type (gnat_desig_type)
2954 && !Is_Constrained (gnat_desig_type)))
2956 tree gnu_old
2957 = (present_gnu_tree (gnat_desig_type)
2958 ? gnat_to_gnu_type (gnat_desig_type)
2959 : make_dummy_type (gnat_desig_type));
2960 tree fields;
2962 /* Show the dummy we get will be a fat pointer. */
2963 got_fat_p = made_dummy = true;
2965 /* If the call above got something that has a pointer, that
2966 pointer is our type. This could have happened either
2967 because the type was elaborated or because somebody
2968 else executed the code below. */
2969 gnu_type = TYPE_POINTER_TO (gnu_old);
2970 if (!gnu_type)
2972 gnu_type = make_node (RECORD_TYPE);
2973 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
2974 TYPE_POINTER_TO (gnu_old) = gnu_type;
2976 Sloc_to_locus (Sloc (gnat_entity), &input_location);
2977 fields
2978 = chainon (chainon (NULL_TREE,
2979 create_field_decl
2980 (get_identifier ("P_ARRAY"),
2981 ptr_void_type_node, gnu_type,
2982 0, 0, 0, 0)),
2983 create_field_decl (get_identifier ("P_BOUNDS"),
2984 ptr_void_type_node,
2985 gnu_type, 0, 0, 0, 0));
2987 /* Make sure we can place this into a register. */
2988 TYPE_ALIGN (gnu_type)
2989 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
2990 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
2991 finish_record_type (gnu_type, fields, false, true);
2993 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
2994 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
2995 = concat_id_with_name (get_entity_name (gnat_desig_type),
2996 "XUT");
2997 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
3001 /* If we already know what the full type is, use it. */
3002 else if (Present (gnat_desig_full)
3003 && present_gnu_tree (gnat_desig_full))
3004 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3006 /* Get the type of the thing we are to point to and build a pointer
3007 to it. If it is a reference to an incomplete or private type with a
3008 full view that is a record, make a dummy type node and get the
3009 actual type later when we have verified it is safe. */
3010 else if (!in_main_unit
3011 && !present_gnu_tree (gnat_desig_type)
3012 && Present (gnat_desig_full)
3013 && !present_gnu_tree (gnat_desig_full)
3014 && Is_Record_Type (gnat_desig_full))
3016 gnu_desig_type = make_dummy_type (gnat_desig_type);
3017 made_dummy = true;
3020 /* Likewise if we are pointing to a record or array and we are to defer
3021 elaborating incomplete types. We do this since this access type
3022 may be the full view of some private type. Note that the
3023 unconstrained array case is handled above. */
3024 else if ((!in_main_unit || imported_p) && defer_incomplete_level != 0
3025 && !present_gnu_tree (gnat_desig_type)
3026 && ((Is_Record_Type (gnat_desig_type)
3027 || Is_Array_Type (gnat_desig_type))
3028 || (Present (gnat_desig_full)
3029 && (Is_Record_Type (gnat_desig_full)
3030 || Is_Array_Type (gnat_desig_full)))))
3032 gnu_desig_type = make_dummy_type (gnat_desig_type);
3033 made_dummy = true;
3035 else if (gnat_desig_type == gnat_entity)
3037 gnu_type
3038 = build_pointer_type_for_mode (make_node (VOID_TYPE),
3039 p_mode,
3040 No_Strict_Aliasing (gnat_entity));
3041 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3043 else
3044 gnu_desig_type = gnat_to_gnu_type (gnat_desig_type);
3046 /* It is possible that the above call to gnat_to_gnu_type resolved our
3047 type. If so, just return it. */
3048 if (present_gnu_tree (gnat_entity))
3050 maybe_present = true;
3051 break;
3054 /* If we have a GCC type for the designated type, possibly modify it
3055 if we are pointing only to constant objects and then make a pointer
3056 to it. Don't do this for unconstrained arrays. */
3057 if (!gnu_type && gnu_desig_type)
3059 if (Is_Access_Constant (gnat_entity)
3060 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3062 gnu_desig_type
3063 = build_qualified_type
3064 (gnu_desig_type,
3065 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3067 /* Some extra processing is required if we are building a
3068 pointer to an incomplete type (in the GCC sense). We might
3069 have such a type if we just made a dummy, or directly out
3070 of the call to gnat_to_gnu_type above if we are processing
3071 an access type for a record component designating the
3072 record type itself. */
3073 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3075 /* We must ensure that the pointer to variant we make will
3076 be processed by update_pointer_to when the initial type
3077 is completed. Pretend we made a dummy and let further
3078 processing act as usual. */
3079 made_dummy = true;
3081 /* We must ensure that update_pointer_to will not retrieve
3082 the dummy variant when building a properly qualified
3083 version of the complete type. We take advantage of the
3084 fact that get_qualified_type is requiring TYPE_NAMEs to
3085 match to influence build_qualified_type and then also
3086 update_pointer_to here. */
3087 TYPE_NAME (gnu_desig_type)
3088 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3092 gnu_type
3093 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3094 No_Strict_Aliasing (gnat_entity));
3097 /* If we are not defining this object and we made a dummy pointer,
3098 save our current definition, evaluate the actual type, and replace
3099 the tentative type we made with the actual one. If we are to defer
3100 actually looking up the actual type, make an entry in the
3101 deferred list. */
3103 if (!in_main_unit && made_dummy)
3105 tree gnu_old_type
3106 = TYPE_FAT_POINTER_P (gnu_type)
3107 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3109 if (esize == POINTER_SIZE
3110 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
3111 gnu_type
3112 = build_pointer_type
3113 (TYPE_OBJECT_RECORD_TYPE
3114 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3116 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3117 !Comes_From_Source (gnat_entity),
3118 debug_info_p, gnat_entity);
3119 save_gnu_tree (gnat_entity, gnu_decl, false);
3120 this_made_decl = saved = true;
3122 if (defer_incomplete_level == 0)
3123 /* Note that the call to gnat_to_gnu_type here might have
3124 updated gnu_old_type directly, in which case it is not a
3125 dummy type any more when we get into update_pointer_to.
3127 This may happen for instance when the designated type is a
3128 record type, because their elaboration starts with an
3129 initial node from make_dummy_type, which may yield the same
3130 node as the one we got.
3132 Besides, variants of this non-dummy type might have been
3133 created along the way. update_pointer_to is expected to
3134 properly take care of those situations. */
3135 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
3136 gnat_to_gnu_type (gnat_desig_type));
3137 else
3139 struct incomplete *p
3140 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3142 p->old_type = gnu_old_type;
3143 p->full_type = gnat_desig_type;
3144 p->next = defer_incomplete_list;
3145 defer_incomplete_list = p;
3149 break;
3151 case E_Access_Protected_Subprogram_Type:
3152 case E_Anonymous_Access_Protected_Subprogram_Type:
3153 if (type_annotate_only && No (Equivalent_Type (gnat_entity)))
3154 gnu_type = build_pointer_type (void_type_node);
3155 else
3156 /* The runtime representation is the equivalent type. */
3157 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3159 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3160 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3161 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3162 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3163 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3164 NULL_TREE, 0);
3166 break;
3168 case E_Access_Subtype:
3170 /* We treat this as identical to its base type; any constraint is
3171 meaningful only to the front end.
3173 The designated type must be elaborated as well, if it does
3174 not have its own freeze node. Designated (sub)types created
3175 for constrained components of records with discriminants are
3176 not frozen by the front end and thus not elaborated by gigi,
3177 because their use may appear before the base type is frozen,
3178 and because it is not clear that they are needed anywhere in
3179 Gigi. With the current model, there is no correct place where
3180 they could be elaborated. */
3182 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3183 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3184 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3185 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3186 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3188 /* If we are not defining this entity, and we have incomplete
3189 entities being processed above us, make a dummy type and
3190 elaborate it later. */
3191 if (!definition && defer_incomplete_level != 0)
3193 struct incomplete *p
3194 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3195 tree gnu_ptr_type
3196 = build_pointer_type
3197 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3199 p->old_type = TREE_TYPE (gnu_ptr_type);
3200 p->full_type = Directly_Designated_Type (gnat_entity);
3201 p->next = defer_incomplete_list;
3202 defer_incomplete_list = p;
3204 else if (IN (Ekind (Base_Type
3205 (Directly_Designated_Type (gnat_entity))),
3206 Incomplete_Or_Private_Kind))
3208 else
3209 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3210 NULL_TREE, 0);
3213 maybe_present = true;
3214 break;
3216 /* Subprogram Entities
3218 The following access functions are defined for subprograms (functions
3219 or procedures):
3221 First_Formal The first formal parameter.
3222 Is_Imported Indicates that the subprogram has appeared in
3223 an INTERFACE or IMPORT pragma. For now we
3224 assume that the external language is C.
3225 Is_Inlined True if the subprogram is to be inlined.
3227 In addition for function subprograms we have:
3229 Etype Return type of the function.
3231 Each parameter is first checked by calling must_pass_by_ref on its
3232 type to determine if it is passed by reference. For parameters which
3233 are copied in, if they are Ada IN OUT or OUT parameters, their return
3234 value becomes part of a record which becomes the return type of the
3235 function (C function - note that this applies only to Ada procedures
3236 so there is no Ada return type). Additional code to store back the
3237 parameters will be generated on the caller side. This transformation
3238 is done here, not in the front-end.
3240 The intended result of the transformation can be seen from the
3241 equivalent source rewritings that follow:
3243 struct temp {int a,b};
3244 procedure P (A,B: IN OUT ...) is temp P (int A,B) {
3245 .. ..
3246 end P; return {A,B};
3248 procedure call
3251 temp t;
3252 P(X,Y); t = P(X,Y);
3253 X = t.a , Y = t.b;
3256 For subprogram types we need to perform mainly the same conversions to
3257 GCC form that are needed for procedures and function declarations. The
3258 only difference is that at the end, we make a type declaration instead
3259 of a function declaration. */
3261 case E_Subprogram_Type:
3262 case E_Function:
3263 case E_Procedure:
3265 /* The first GCC parameter declaration (a PARM_DECL node). The
3266 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3267 actually is the head of this parameter list. */
3268 tree gnu_param_list = NULL_TREE;
3269 /* The type returned by a function. If the subprogram is a procedure
3270 this type should be void_type_node. */
3271 tree gnu_return_type = void_type_node;
3272 /* List of fields in return type of procedure with copy in copy out
3273 parameters. */
3274 tree gnu_field_list = NULL_TREE;
3275 /* Non-null for subprograms containing parameters passed by copy in
3276 copy out (Ada IN OUT or OUT parameters not passed by reference),
3277 in which case it is the list of nodes used to specify the values of
3278 the in out/out parameters that are returned as a record upon
3279 procedure return. The TREE_PURPOSE of an element of this list is
3280 a field of the record and the TREE_VALUE is the PARM_DECL
3281 corresponding to that field. This list will be saved in the
3282 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3283 tree gnu_return_list = NULL_TREE;
3284 /* If an import pragma asks to map this subprogram to a GCC builtin,
3285 this is the builtin DECL node. */
3286 tree gnu_builtin_decl = NULL_TREE;
3287 Entity_Id gnat_param;
3288 bool inline_flag = Is_Inlined (gnat_entity);
3289 bool public_flag = Is_Public (gnat_entity);
3290 bool extern_flag
3291 = (Is_Public (gnat_entity) && !definition) || imported_p;
3292 bool pure_flag = Is_Pure (gnat_entity);
3293 bool volatile_flag = No_Return (gnat_entity);
3294 bool returns_by_ref = false;
3295 bool returns_unconstrained = false;
3296 bool returns_by_target_ptr = false;
3297 tree gnu_ext_name = create_concat_name (gnat_entity, 0);
3298 bool has_copy_in_out = false;
3299 int parmnum;
3301 if (kind == E_Subprogram_Type && !definition)
3302 /* A parameter may refer to this type, so defer completion
3303 of any incomplete types. */
3304 defer_incomplete_level++, this_deferred = true;
3306 /* If the subprogram has an alias, it is probably inherited, so
3307 we can use the original one. If the original "subprogram"
3308 is actually an enumeration literal, it may be the first use
3309 of its type, so we must elaborate that type now. */
3310 if (Present (Alias (gnat_entity)))
3312 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3313 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3315 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3316 gnu_expr, 0);
3318 /* Elaborate any Itypes in the parameters of this entity. */
3319 for (gnat_temp = First_Formal (gnat_entity);
3320 Present (gnat_temp);
3321 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3322 if (Is_Itype (Etype (gnat_temp)))
3323 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3325 break;
3328 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3329 corresponding DECL node.
3331 We still want the parameter associations to take place because the
3332 proper generation of calls depends on it (a GNAT parameter without
3333 a corresponding GCC tree has a very specific meaning), so we don't
3334 just break here. */
3335 if (Convention (gnat_entity) == Convention_Intrinsic)
3336 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3338 /* ??? What if we don't find the builtin node above ? warn ? err ?
3339 In the current state we neither warn nor err, and calls will just
3340 be handled as for regular subprograms. */
3342 if (kind == E_Function || kind == E_Subprogram_Type)
3343 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3345 /* If this function returns by reference, make the actual
3346 return type of this function the pointer and mark the decl. */
3347 if (Returns_By_Ref (gnat_entity))
3349 returns_by_ref = true;
3350 gnu_return_type = build_pointer_type (gnu_return_type);
3353 /* If the Mechanism is By_Reference, ensure the return type uses
3354 the machine's by-reference mechanism, which may not the same
3355 as above (e.g., it might be by passing a fake parameter). */
3356 else if (kind == E_Function
3357 && Mechanism (gnat_entity) == By_Reference)
3359 gnu_return_type = copy_type (gnu_return_type);
3360 TREE_ADDRESSABLE (gnu_return_type) = 1;
3363 /* If we are supposed to return an unconstrained array,
3364 actually return a fat pointer and make a note of that. Return
3365 a pointer to an unconstrained record of variable size. */
3366 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3368 gnu_return_type = TREE_TYPE (gnu_return_type);
3369 returns_unconstrained = true;
3372 /* If the type requires a transient scope, the result is allocated
3373 on the secondary stack, so the result type of the function is
3374 just a pointer. */
3375 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3377 gnu_return_type = build_pointer_type (gnu_return_type);
3378 returns_unconstrained = true;
3381 /* If the type is a padded type and the underlying type would not
3382 be passed by reference or this function has a foreign convention,
3383 return the underlying type. */
3384 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3385 && TYPE_IS_PADDING_P (gnu_return_type)
3386 && (!default_pass_by_ref (TREE_TYPE
3387 (TYPE_FIELDS (gnu_return_type)))
3388 || Has_Foreign_Convention (gnat_entity)))
3389 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3391 /* If the return type is unconstrained, that means it must have a
3392 maximum size. We convert the function into a procedure and its
3393 caller will pass a pointer to an object of that maximum size as the
3394 first parameter when we call the function. */
3395 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
3397 returns_by_target_ptr = true;
3398 gnu_param_list
3399 = create_param_decl (get_identifier ("TARGET"),
3400 build_reference_type (gnu_return_type),
3401 true);
3402 gnu_return_type = void_type_node;
3405 /* If the return type has a size that overflows, we cannot have
3406 a function that returns that type. This usage doesn't make
3407 sense anyway, so give an error here. */
3408 if (TYPE_SIZE_UNIT (gnu_return_type)
3409 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
3411 post_error ("cannot return type whose size overflows",
3412 gnat_entity);
3413 gnu_return_type = copy_node (gnu_return_type);
3414 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
3415 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
3416 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
3417 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
3420 /* Look at all our parameters and get the type of
3421 each. While doing this, build a copy-out structure if
3422 we need one. */
3424 for (gnat_param = First_Formal (gnat_entity), parmnum = 0;
3425 Present (gnat_param);
3426 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3428 tree gnu_param_name = get_entity_name (gnat_param);
3429 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3430 tree gnu_param, gnu_field;
3431 bool by_ref_p = false;
3432 bool by_descr_p = false;
3433 bool by_component_ptr_p = false;
3434 bool copy_in_copy_out_flag = false;
3435 bool req_by_copy = false, req_by_ref = false;
3437 /* Builtins are expanded inline and there is no real call sequence
3438 involved. so the type expected by the underlying expander is
3439 always the type of each argument "as is". */
3440 if (gnu_builtin_decl)
3441 req_by_copy = 1;
3443 /* Otherwise, see if a Mechanism was supplied that forced this
3444 parameter to be passed one way or another. */
3445 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3446 req_by_copy = true;
3447 else if (Mechanism (gnat_param) == Default)
3449 else if (Mechanism (gnat_param) == By_Copy)
3450 req_by_copy = true;
3451 else if (Mechanism (gnat_param) == By_Reference)
3452 req_by_ref = true;
3453 else if (Mechanism (gnat_param) <= By_Descriptor)
3454 by_descr_p = true;
3455 else if (Mechanism (gnat_param) > 0)
3457 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3458 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3459 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3460 Mechanism (gnat_param)))
3461 req_by_ref = true;
3462 else
3463 req_by_copy = true;
3465 else
3466 post_error ("unsupported mechanism for&", gnat_param);
3468 /* If this is either a foreign function or if the
3469 underlying type won't be passed by reference, strip off
3470 possible padding type. */
3471 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
3472 && TYPE_IS_PADDING_P (gnu_param_type)
3473 && (req_by_ref || Has_Foreign_Convention (gnat_entity)
3474 || (!must_pass_by_ref (TREE_TYPE (TYPE_FIELDS
3475 (gnu_param_type)))
3476 && (req_by_copy
3477 || !default_pass_by_ref (TREE_TYPE
3478 (TYPE_FIELDS
3479 (gnu_param_type)))))))
3480 gnu_param_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
3482 /* If this is an IN parameter it is read-only, so make a variant
3483 of the type that is read-only.
3485 ??? However, if this is an unconstrained array, that type can
3486 be very complex. So skip it for now. Likewise for any other
3487 self-referential type. */
3488 if (Ekind (gnat_param) == E_In_Parameter
3489 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
3490 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
3491 gnu_param_type
3492 = build_qualified_type (gnu_param_type,
3493 (TYPE_QUALS (gnu_param_type)
3494 | TYPE_QUAL_CONST));
3496 /* For foreign conventions, pass arrays as a pointer to the
3497 underlying type. First check for unconstrained array and get
3498 the underlying array. Then get the component type and build
3499 a pointer to it. */
3500 if (Has_Foreign_Convention (gnat_entity)
3501 && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
3502 gnu_param_type
3503 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3504 (TREE_TYPE (gnu_param_type))));
3506 if (by_descr_p)
3507 gnu_param_type
3508 = build_pointer_type
3509 (build_vms_descriptor (gnu_param_type,
3510 Mechanism (gnat_param), gnat_entity));
3512 else if (Has_Foreign_Convention (gnat_entity)
3513 && !req_by_copy
3514 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
3516 /* Strip off any multi-dimensional entries, then strip
3517 off the last array to get the component type. */
3518 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
3519 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
3520 gnu_param_type = TREE_TYPE (gnu_param_type);
3522 by_component_ptr_p = true;
3523 gnu_param_type = TREE_TYPE (gnu_param_type);
3525 if (Ekind (gnat_param) == E_In_Parameter)
3526 gnu_param_type
3527 = build_qualified_type (gnu_param_type,
3528 (TYPE_QUALS (gnu_param_type)
3529 | TYPE_QUAL_CONST));
3531 gnu_param_type = build_pointer_type (gnu_param_type);
3534 /* Fat pointers are passed as thin pointers for foreign
3535 conventions. */
3536 else if (Has_Foreign_Convention (gnat_entity)
3537 && TYPE_FAT_POINTER_P (gnu_param_type))
3538 gnu_param_type
3539 = make_type_from_size (gnu_param_type,
3540 size_int (POINTER_SIZE), false);
3542 /* If we must pass or were requested to pass by reference, do so.
3543 If we were requested to pass by copy, do so.
3544 Otherwise, for foreign conventions, pass all in out parameters
3545 or aggregates by reference. For COBOL and Fortran, pass
3546 all integer and FP types that way too. For Convention Ada,
3547 use the standard Ada default. */
3548 else if (must_pass_by_ref (gnu_param_type) || req_by_ref
3549 || (!req_by_copy
3550 && ((Has_Foreign_Convention (gnat_entity)
3551 && (Ekind (gnat_param) != E_In_Parameter
3552 || AGGREGATE_TYPE_P (gnu_param_type)))
3553 || (((Convention (gnat_entity)
3554 == Convention_Fortran)
3555 || (Convention (gnat_entity)
3556 == Convention_COBOL))
3557 && (INTEGRAL_TYPE_P (gnu_param_type)
3558 || FLOAT_TYPE_P (gnu_param_type)))
3559 /* For convention Ada, see if we pass by reference
3560 by default. */
3561 || (!Has_Foreign_Convention (gnat_entity)
3562 && default_pass_by_ref (gnu_param_type)))))
3564 gnu_param_type = build_reference_type (gnu_param_type);
3565 by_ref_p = true;
3568 else if (Ekind (gnat_param) != E_In_Parameter)
3569 copy_in_copy_out_flag = true;
3571 if (req_by_copy && (by_ref_p || by_component_ptr_p))
3572 post_error ("?cannot pass & by copy", gnat_param);
3574 /* If this is an OUT parameter that isn't passed by reference
3575 and isn't a pointer or aggregate, we don't make a PARM_DECL
3576 for it. Instead, it will be a VAR_DECL created when we process
3577 the procedure. For the special parameter of Valued_Procedure,
3578 never pass it in.
3580 An exception is made to cover the RM-6.4.1 rule requiring "by
3581 copy" out parameters with discriminants or implicit initial
3582 values to be handled like in out parameters. These type are
3583 normally built as aggregates, and hence passed by reference,
3584 except for some packed arrays which end up encoded in special
3585 integer types.
3587 The exception we need to make is then for packed arrays of
3588 records with discriminants or implicit initial values. We have
3589 no light/easy way to check for the latter case, so we merely
3590 check for packed arrays of records. This may lead to useless
3591 copy-in operations, but in very rare cases only, as these would
3592 be exceptions in a set of already exceptional situations. */
3593 if (Ekind (gnat_param) == E_Out_Parameter && !by_ref_p
3594 && ((Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3595 || (!by_descr_p
3596 && !POINTER_TYPE_P (gnu_param_type)
3597 && !AGGREGATE_TYPE_P (gnu_param_type)))
3598 && !(Is_Array_Type (Etype (gnat_param))
3599 && Is_Packed (Etype (gnat_param))
3600 && Is_Composite_Type (Component_Type
3601 (Etype (gnat_param)))))
3602 gnu_param = NULL_TREE;
3603 else
3605 gnu_param
3606 = create_param_decl
3607 (gnu_param_name, gnu_param_type,
3608 by_ref_p || by_component_ptr_p
3609 || Ekind (gnat_param) == E_In_Parameter);
3611 DECL_BY_REF_P (gnu_param) = by_ref_p;
3612 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr_p;
3613 DECL_BY_DESCRIPTOR_P (gnu_param) = by_descr_p;
3614 DECL_POINTS_TO_READONLY_P (gnu_param)
3615 = (Ekind (gnat_param) == E_In_Parameter
3616 && (by_ref_p || by_component_ptr_p));
3617 Sloc_to_locus (Sloc (gnat_param),
3618 &DECL_SOURCE_LOCATION (gnu_param));
3619 save_gnu_tree (gnat_param, gnu_param, false);
3620 gnu_param_list = chainon (gnu_param, gnu_param_list);
3622 /* If a parameter is a pointer, this function may modify
3623 memory through it and thus shouldn't be considered
3624 a pure function. Also, the memory may be modified
3625 between two calls, so they can't be CSE'ed. The latter
3626 case also handles by-ref parameters. */
3627 if (POINTER_TYPE_P (gnu_param_type)
3628 || TYPE_FAT_POINTER_P (gnu_param_type))
3629 pure_flag = false;
3632 if (copy_in_copy_out_flag)
3634 if (!has_copy_in_out)
3636 gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
3637 gnu_return_type = make_node (RECORD_TYPE);
3638 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3639 has_copy_in_out = true;
3642 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3643 gnu_return_type, 0, 0, 0, 0);
3644 Sloc_to_locus (Sloc (gnat_param),
3645 &DECL_SOURCE_LOCATION (gnu_field));
3646 TREE_CHAIN (gnu_field) = gnu_field_list;
3647 gnu_field_list = gnu_field;
3648 gnu_return_list = tree_cons (gnu_field, gnu_param,
3649 gnu_return_list);
3653 /* Do not compute record for out parameters if subprogram is
3654 stubbed since structures are incomplete for the back-end. */
3655 if (gnu_field_list
3656 && Convention (gnat_entity) != Convention_Stubbed)
3658 /* If all types are not complete, defer emission of debug
3659 information for this record types. Otherwise, we risk emitting
3660 debug information for a dummy type contained in the fields
3661 for that record. */
3662 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3663 false, defer_incomplete_level);
3665 if (defer_incomplete_level)
3667 debug_deferred = true;
3668 defer_debug_level++;
3670 defer_debug_incomplete_list
3671 = tree_cons (NULL_TREE, gnu_return_type,
3672 defer_debug_incomplete_list);
3676 /* If we have a CICO list but it has only one entry, we convert
3677 this function into a function that simply returns that one
3678 object. */
3679 if (list_length (gnu_return_list) == 1)
3680 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3682 if (Has_Stdcall_Convention (gnat_entity))
3684 struct attrib *attr
3685 = (struct attrib *) xmalloc (sizeof (struct attrib));
3687 attr->next = attr_list;
3688 attr->type = ATTR_MACHINE_ATTRIBUTE;
3689 attr->name = get_identifier ("stdcall");
3690 attr->args = NULL_TREE;
3691 attr->error_point = gnat_entity;
3692 attr_list = attr;
3695 /* Both lists ware built in reverse. */
3696 gnu_param_list = nreverse (gnu_param_list);
3697 gnu_return_list = nreverse (gnu_return_list);
3699 gnu_type
3700 = create_subprog_type (gnu_return_type, gnu_param_list,
3701 gnu_return_list, returns_unconstrained,
3702 returns_by_ref,
3703 Function_Returns_With_DSP (gnat_entity),
3704 returns_by_target_ptr);
3706 /* A subprogram (something that doesn't return anything) shouldn't
3707 be considered Pure since there would be no reason for such a
3708 subprogram. Note that procedures with Out (or In Out) parameters
3709 have already been converted into a function with a return type. */
3710 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
3711 pure_flag = false;
3713 gnu_type
3714 = build_qualified_type (gnu_type,
3715 (TYPE_QUALS (gnu_type)
3716 | (TYPE_QUAL_CONST * pure_flag)
3717 | (TYPE_QUAL_VOLATILE * volatile_flag)));
3719 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3721 /* If we have a builtin decl for that function, check the signatures
3722 compatibilities. If the signatures are compatible, use the builtin
3723 decl. If they are not, we expect the checker predicate to have
3724 posted the appropriate errors, and just continue with what we have
3725 so far. */
3726 if (gnu_builtin_decl)
3728 tree gnu_builtin_type = TREE_TYPE (gnu_builtin_decl);
3730 if (compatible_signatures_p (gnu_type, gnu_builtin_type))
3732 gnu_decl = gnu_builtin_decl;
3733 gnu_type = gnu_builtin_type;
3734 break;
3738 /* If there was no specified Interface_Name and the external and
3739 internal names of the subprogram are the same, only use the
3740 internal name to allow disambiguation of nested subprograms. */
3741 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_id)
3742 gnu_ext_name = NULL_TREE;
3744 /* If we are defining the subprogram and it has an Address clause
3745 we must get the address expression from the saved GCC tree for the
3746 subprogram if it has a Freeze_Node. Otherwise, we elaborate
3747 the address expression here since the front-end has guaranteed
3748 in that case that the elaboration has no effects. If there is
3749 an Address clause and we are not defining the object, just
3750 make it a constant. */
3751 if (Present (Address_Clause (gnat_entity)))
3753 tree gnu_address = NULL_TREE;
3755 if (definition)
3756 gnu_address
3757 = (present_gnu_tree (gnat_entity)
3758 ? get_gnu_tree (gnat_entity)
3759 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
3761 save_gnu_tree (gnat_entity, NULL_TREE, false);
3763 gnu_type = build_reference_type (gnu_type);
3764 if (gnu_address)
3765 gnu_address = convert (gnu_type, gnu_address);
3767 gnu_decl
3768 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
3769 gnu_address, false, Is_Public (gnat_entity),
3770 extern_flag, false, NULL, gnat_entity);
3771 DECL_BY_REF_P (gnu_decl) = 1;
3774 else if (kind == E_Subprogram_Type)
3775 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3776 !Comes_From_Source (gnat_entity),
3777 debug_info_p && !defer_incomplete_level,
3778 gnat_entity);
3779 else
3781 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
3782 gnu_type, gnu_param_list,
3783 inline_flag, public_flag,
3784 extern_flag, attr_list,
3785 gnat_entity);
3786 DECL_STUBBED_P (gnu_decl)
3787 = Convention (gnat_entity) == Convention_Stubbed;
3790 break;
3792 case E_Incomplete_Type:
3793 case E_Private_Type:
3794 case E_Limited_Private_Type:
3795 case E_Record_Type_With_Private:
3796 case E_Private_Subtype:
3797 case E_Limited_Private_Subtype:
3798 case E_Record_Subtype_With_Private:
3800 /* If this type does not have a full view in the unit we are
3801 compiling, then just get the type from its Etype. */
3802 if (No (Full_View (gnat_entity)))
3804 /* If this is an incomplete type with no full view, it must be
3805 either a limited view brought in by a limited_with clause, in
3806 which case we use the non-limited view, or a Taft Amendement
3807 type, in which case we just return a dummy type. */
3808 if (kind == E_Incomplete_Type)
3810 if (From_With_Type (gnat_entity)
3811 && Present (Non_Limited_View (gnat_entity)))
3812 gnu_decl = gnat_to_gnu_entity (Non_Limited_View (gnat_entity),
3813 NULL_TREE, 0);
3814 else
3815 gnu_type = make_dummy_type (gnat_entity);
3818 else if (Present (Underlying_Full_View (gnat_entity)))
3819 gnu_decl = gnat_to_gnu_entity (Underlying_Full_View (gnat_entity),
3820 NULL_TREE, 0);
3821 else
3823 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
3824 NULL_TREE, 0);
3825 maybe_present = true;
3828 break;
3831 /* Otherwise, if we are not defining the type now, get the
3832 type from the full view. But always get the type from the full
3833 view for define on use types, since otherwise we won't see them! */
3835 else if (!definition
3836 || (Is_Itype (Full_View (gnat_entity))
3837 && No (Freeze_Node (gnat_entity)))
3838 || (Is_Itype (gnat_entity)
3839 && No (Freeze_Node (Full_View (gnat_entity)))))
3841 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
3842 NULL_TREE, 0);
3843 maybe_present = true;
3844 break;
3847 /* For incomplete types, make a dummy type entry which will be
3848 replaced later. */
3849 gnu_type = make_dummy_type (gnat_entity);
3851 /* Save this type as the full declaration's type so we can do any needed
3852 updates when we see it. */
3853 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3854 !Comes_From_Source (gnat_entity),
3855 debug_info_p, gnat_entity);
3856 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
3857 break;
3859 /* Simple class_wide types are always viewed as their root_type
3860 by Gigi unless an Equivalent_Type is specified. */
3861 case E_Class_Wide_Type:
3862 if (Present (Equivalent_Type (gnat_entity)))
3863 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3864 else
3865 gnu_type = gnat_to_gnu_type (Root_Type (gnat_entity));
3867 maybe_present = true;
3868 break;
3870 case E_Task_Type:
3871 case E_Task_Subtype:
3872 case E_Protected_Type:
3873 case E_Protected_Subtype:
3874 if (type_annotate_only && No (Corresponding_Record_Type (gnat_entity)))
3875 gnu_type = void_type_node;
3876 else
3877 gnu_type = gnat_to_gnu_type (Corresponding_Record_Type (gnat_entity));
3879 maybe_present = true;
3880 break;
3882 case E_Label:
3883 gnu_decl = create_label_decl (gnu_entity_id);
3884 break;
3886 case E_Block:
3887 case E_Loop:
3888 /* Nothing at all to do here, so just return an ERROR_MARK and claim
3889 we've already saved it, so we don't try to. */
3890 gnu_decl = error_mark_node;
3891 saved = true;
3892 break;
3894 default:
3895 gcc_unreachable ();
3898 /* If we had a case where we evaluated another type and it might have
3899 defined this one, handle it here. */
3900 if (maybe_present && present_gnu_tree (gnat_entity))
3902 gnu_decl = get_gnu_tree (gnat_entity);
3903 saved = true;
3906 /* If we are processing a type and there is either no decl for it or
3907 we just made one, do some common processing for the type, such as
3908 handling alignment and possible padding. */
3910 if ((!gnu_decl || this_made_decl) && IN (kind, Type_Kind))
3912 if (Is_Tagged_Type (gnat_entity)
3913 || Is_Class_Wide_Equivalent_Type (gnat_entity))
3914 TYPE_ALIGN_OK (gnu_type) = 1;
3916 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
3917 TYPE_BY_REFERENCE_P (gnu_type) = 1;
3919 /* ??? Don't set the size for a String_Literal since it is either
3920 confirming or we don't handle it properly (if the low bound is
3921 non-constant). */
3922 if (!gnu_size && kind != E_String_Literal_Subtype)
3923 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
3924 TYPE_DECL, false,
3925 Has_Size_Clause (gnat_entity));
3927 /* If a size was specified, see if we can make a new type of that size
3928 by rearranging the type, for example from a fat to a thin pointer. */
3929 if (gnu_size)
3931 gnu_type
3932 = make_type_from_size (gnu_type, gnu_size,
3933 Has_Biased_Representation (gnat_entity));
3935 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
3936 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
3937 gnu_size = 0;
3940 /* If the alignment hasn't already been processed and this is
3941 not an unconstrained array, see if an alignment is specified.
3942 If not, we pick a default alignment for atomic objects. */
3943 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
3945 else if (Known_Alignment (gnat_entity))
3946 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
3947 TYPE_ALIGN (gnu_type));
3948 else if (Is_Atomic (gnat_entity) && !gnu_size
3949 && host_integerp (TYPE_SIZE (gnu_type), 1)
3950 && integer_pow2p (TYPE_SIZE (gnu_type)))
3951 align = MIN (BIGGEST_ALIGNMENT,
3952 tree_low_cst (TYPE_SIZE (gnu_type), 1));
3953 else if (Is_Atomic (gnat_entity) && gnu_size
3954 && host_integerp (gnu_size, 1)
3955 && integer_pow2p (gnu_size))
3956 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
3958 /* See if we need to pad the type. If we did, and made a record,
3959 the name of the new type may be changed. So get it back for
3960 us when we make the new TYPE_DECL below. */
3961 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity, "PAD",
3962 true, definition, false);
3963 if (TREE_CODE (gnu_type) == RECORD_TYPE
3964 && TYPE_IS_PADDING_P (gnu_type))
3966 gnu_entity_id = TYPE_NAME (gnu_type);
3967 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
3968 gnu_entity_id = DECL_NAME (gnu_entity_id);
3971 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
3973 /* If we are at global level, GCC will have applied variable_size to
3974 the type, but that won't have done anything. So, if it's not
3975 a constant or self-referential, call elaborate_expression_1 to
3976 make a variable for the size rather than calculating it each time.
3977 Handle both the RM size and the actual size. */
3978 if (global_bindings_p ()
3979 && TYPE_SIZE (gnu_type)
3980 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
3981 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3983 if (TREE_CODE (gnu_type) == RECORD_TYPE
3984 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
3985 TYPE_SIZE (gnu_type), 0))
3987 TYPE_SIZE (gnu_type)
3988 = elaborate_expression_1 (gnat_entity, gnat_entity,
3989 TYPE_SIZE (gnu_type),
3990 get_identifier ("SIZE"),
3991 definition, 0);
3992 SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
3994 else
3996 TYPE_SIZE (gnu_type)
3997 = elaborate_expression_1 (gnat_entity, gnat_entity,
3998 TYPE_SIZE (gnu_type),
3999 get_identifier ("SIZE"),
4000 definition, 0);
4002 /* ??? For now, store the size as a multiple of the alignment
4003 in bytes so that we can see the alignment from the tree. */
4004 TYPE_SIZE_UNIT (gnu_type)
4005 = build_binary_op
4006 (MULT_EXPR, sizetype,
4007 elaborate_expression_1
4008 (gnat_entity, gnat_entity,
4009 build_binary_op (EXACT_DIV_EXPR, sizetype,
4010 TYPE_SIZE_UNIT (gnu_type),
4011 size_int (TYPE_ALIGN (gnu_type)
4012 / BITS_PER_UNIT)),
4013 get_identifier ("SIZE_A_UNIT"),
4014 definition, 0),
4015 size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4017 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4018 SET_TYPE_ADA_SIZE
4019 (gnu_type,
4020 elaborate_expression_1 (gnat_entity,
4021 gnat_entity,
4022 TYPE_ADA_SIZE (gnu_type),
4023 get_identifier ("RM_SIZE"),
4024 definition, 0));
4028 /* If this is a record type or subtype, call elaborate_expression_1 on
4029 any field position. Do this for both global and local types.
4030 Skip any fields that we haven't made trees for to avoid problems with
4031 class wide types. */
4032 if (IN (kind, Record_Kind))
4033 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4034 gnat_temp = Next_Entity (gnat_temp))
4035 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4037 tree gnu_field = get_gnu_tree (gnat_temp);
4039 /* ??? Unfortunately, GCC needs to be able to prove the
4040 alignment of this offset and if it's a variable, it can't.
4041 In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
4042 right now, we have to put in an explicit multiply and
4043 divide by that value. */
4044 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4045 DECL_FIELD_OFFSET (gnu_field)
4046 = build_binary_op
4047 (MULT_EXPR, sizetype,
4048 elaborate_expression_1
4049 (gnat_temp, gnat_temp,
4050 build_binary_op (EXACT_DIV_EXPR, sizetype,
4051 DECL_FIELD_OFFSET (gnu_field),
4052 size_int (DECL_OFFSET_ALIGN (gnu_field)
4053 / BITS_PER_UNIT)),
4054 get_identifier ("OFFSET"),
4055 definition, 0),
4056 size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
4059 gnu_type = build_qualified_type (gnu_type,
4060 (TYPE_QUALS (gnu_type)
4061 | (TYPE_QUAL_VOLATILE
4062 * Treat_As_Volatile (gnat_entity))));
4064 if (Is_Atomic (gnat_entity))
4065 check_ok_for_atomic (gnu_type, gnat_entity, false);
4067 if (Known_Alignment (gnat_entity))
4068 TYPE_USER_ALIGN (gnu_type) = 1;
4070 if (!gnu_decl)
4071 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4072 !Comes_From_Source (gnat_entity),
4073 debug_info_p, gnat_entity);
4074 else
4075 TREE_TYPE (gnu_decl) = gnu_type;
4078 if (IN (kind, Type_Kind) && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4080 gnu_type = TREE_TYPE (gnu_decl);
4082 /* Back-annotate the Alignment of the type if not already in the
4083 tree. Likewise for sizes. */
4084 if (Unknown_Alignment (gnat_entity))
4085 Set_Alignment (gnat_entity,
4086 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4088 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4090 /* If the size is self-referential, we annotate the maximum
4091 value of that size. */
4092 tree gnu_size = TYPE_SIZE (gnu_type);
4094 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4095 gnu_size = max_size (gnu_size, true);
4097 Set_Esize (gnat_entity, annotate_value (gnu_size));
4099 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4101 /* In this mode the tag and the parent components are not
4102 generated by the front-end, so the sizes must be adjusted
4103 explicitly now. */
4105 int size_offset;
4106 int new_size;
4108 if (Is_Derived_Type (gnat_entity))
4110 size_offset
4111 = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
4112 Set_Alignment (gnat_entity,
4113 Alignment (Etype (Base_Type (gnat_entity))));
4115 else
4116 size_offset = POINTER_SIZE;
4118 new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
4119 Set_Esize (gnat_entity,
4120 UI_From_Int (((new_size + (POINTER_SIZE - 1))
4121 / POINTER_SIZE) * POINTER_SIZE));
4122 Set_RM_Size (gnat_entity, Esize (gnat_entity));
4126 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4127 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4130 if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4131 DECL_ARTIFICIAL (gnu_decl) = 1;
4133 if (!debug_info_p && DECL_P (gnu_decl)
4134 && TREE_CODE (gnu_decl) != FUNCTION_DECL
4135 && No (Renamed_Object (gnat_entity)))
4136 DECL_IGNORED_P (gnu_decl) = 1;
4138 /* If we haven't already, associate the ..._DECL node that we just made with
4139 the input GNAT entity node. */
4140 if (!saved)
4141 save_gnu_tree (gnat_entity, gnu_decl, false);
4143 /* If this is an enumeral or floating-point type, we were not able to set
4144 the bounds since they refer to the type. These bounds are always static.
4146 For enumeration types, also write debugging information and declare the
4147 enumeration literal table, if needed. */
4149 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4150 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4152 tree gnu_scalar_type = gnu_type;
4154 /* If this is a padded type, we need to use the underlying type. */
4155 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
4156 && TYPE_IS_PADDING_P (gnu_scalar_type))
4157 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4159 /* If this is a floating point type and we haven't set a floating
4160 point type yet, use this in the evaluation of the bounds. */
4161 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4162 longest_float_type_node = gnu_type;
4164 TYPE_MIN_VALUE (gnu_scalar_type)
4165 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4166 TYPE_MAX_VALUE (gnu_scalar_type)
4167 = gnat_to_gnu (Type_High_Bound (gnat_entity));
4169 if (TREE_CODE (gnu_scalar_type) == ENUMERAL_TYPE)
4171 TYPE_STUB_DECL (gnu_scalar_type) = gnu_decl;
4173 /* Since this has both a typedef and a tag, avoid outputting
4174 the name twice. */
4175 DECL_ARTIFICIAL (gnu_decl) = 1;
4176 rest_of_type_compilation (gnu_scalar_type, global_bindings_p ());
4180 /* If we deferred processing of incomplete types, re-enable it. If there
4181 were no other disables and we have some to process, do so. */
4182 if (this_deferred && --defer_incomplete_level == 0 && defer_incomplete_list)
4184 struct incomplete *incp = defer_incomplete_list;
4185 struct incomplete *next;
4187 defer_incomplete_list = NULL;
4188 for (; incp; incp = next)
4190 next = incp->next;
4192 if (incp->old_type)
4193 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4194 gnat_to_gnu_type (incp->full_type));
4195 free (incp);
4199 /* If we are not defining this type, see if it's in the incomplete list.
4200 If so, handle that list entry now. */
4201 else if (!definition)
4203 struct incomplete *incp;
4205 for (incp = defer_incomplete_list; incp; incp = incp->next)
4206 if (incp->old_type && incp->full_type == gnat_entity)
4208 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4209 TREE_TYPE (gnu_decl));
4210 incp->old_type = NULL_TREE;
4214 /* If there are no incomplete types and we have deferred emission
4215 of debug information, check whether we have finished defining
4216 all nested records.
4217 If so, handle the list now. */
4219 if (debug_deferred)
4220 defer_debug_level--;
4222 if (defer_debug_incomplete_list
4223 && !defer_incomplete_level
4224 && !defer_debug_level)
4226 tree c, n;
4228 defer_debug_incomplete_list = nreverse (defer_debug_incomplete_list);
4230 for (c = defer_debug_incomplete_list; c; c = n)
4232 n = TREE_CHAIN (c);
4233 write_record_type_debug_info (TREE_VALUE (c));
4236 defer_debug_incomplete_list = 0;
4239 if (this_global)
4240 force_global--;
4242 if (Is_Packed_Array_Type (gnat_entity)
4243 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
4244 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
4245 && !present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
4246 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
4248 return gnu_decl;
4251 /* Similar, but if the returned value is a COMPONENT_REF, return the
4252 FIELD_DECL. */
4254 tree
4255 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4257 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4259 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4260 gnu_field = TREE_OPERAND (gnu_field, 1);
4262 return gnu_field;
4265 /* Given GNAT_ENTITY, elaborate all expressions that are required to
4266 be elaborated at the point of its definition, but do nothing else. */
4268 void
4269 elaborate_entity (Entity_Id gnat_entity)
4271 switch (Ekind (gnat_entity))
4273 case E_Signed_Integer_Subtype:
4274 case E_Modular_Integer_Subtype:
4275 case E_Enumeration_Subtype:
4276 case E_Ordinary_Fixed_Point_Subtype:
4277 case E_Decimal_Fixed_Point_Subtype:
4278 case E_Floating_Point_Subtype:
4280 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
4281 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
4283 /* ??? Tests for avoiding static constraint error expression
4284 is needed until the front stops generating bogus conversions
4285 on bounds of real types. */
4287 if (!Raises_Constraint_Error (gnat_lb))
4288 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
4289 1, 0, Needs_Debug_Info (gnat_entity));
4290 if (!Raises_Constraint_Error (gnat_hb))
4291 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
4292 1, 0, Needs_Debug_Info (gnat_entity));
4293 break;
4296 case E_Record_Type:
4298 Node_Id full_definition = Declaration_Node (gnat_entity);
4299 Node_Id record_definition = Type_Definition (full_definition);
4301 /* If this is a record extension, go a level further to find the
4302 record definition. */
4303 if (Nkind (record_definition) == N_Derived_Type_Definition)
4304 record_definition = Record_Extension_Part (record_definition);
4306 break;
4308 case E_Record_Subtype:
4309 case E_Private_Subtype:
4310 case E_Limited_Private_Subtype:
4311 case E_Record_Subtype_With_Private:
4312 if (Is_Constrained (gnat_entity)
4313 && Has_Discriminants (Base_Type (gnat_entity))
4314 && Present (Discriminant_Constraint (gnat_entity)))
4316 Node_Id gnat_discriminant_expr;
4317 Entity_Id gnat_field;
4319 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
4320 gnat_discriminant_expr
4321 = First_Elmt (Discriminant_Constraint (gnat_entity));
4322 Present (gnat_field);
4323 gnat_field = Next_Discriminant (gnat_field),
4324 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
4325 /* ??? For now, ignore access discriminants. */
4326 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
4327 elaborate_expression (Node (gnat_discriminant_expr),
4328 gnat_entity,
4329 get_entity_name (gnat_field), 1, 0, 0);
4331 break;
4336 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
4337 any entities on its entity chain similarly. */
4339 void
4340 mark_out_of_scope (Entity_Id gnat_entity)
4342 Entity_Id gnat_sub_entity;
4343 unsigned int kind = Ekind (gnat_entity);
4345 /* If this has an entity list, process all in the list. */
4346 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
4347 || IN (kind, Private_Kind)
4348 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
4349 || kind == E_Function || kind == E_Generic_Function
4350 || kind == E_Generic_Package || kind == E_Generic_Procedure
4351 || kind == E_Loop || kind == E_Operator || kind == E_Package
4352 || kind == E_Package_Body || kind == E_Procedure
4353 || kind == E_Record_Type || kind == E_Record_Subtype
4354 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
4355 for (gnat_sub_entity = First_Entity (gnat_entity);
4356 Present (gnat_sub_entity);
4357 gnat_sub_entity = Next_Entity (gnat_sub_entity))
4358 if (Scope (gnat_sub_entity) == gnat_entity
4359 && gnat_sub_entity != gnat_entity)
4360 mark_out_of_scope (gnat_sub_entity);
4362 /* Now clear this if it has been defined, but only do so if it isn't
4363 a subprogram or parameter. We could refine this, but it isn't
4364 worth it. If this is statically allocated, it is supposed to
4365 hang around out of cope. */
4366 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
4367 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
4369 save_gnu_tree (gnat_entity, NULL_TREE, true);
4370 save_gnu_tree (gnat_entity, error_mark_node, true);
4374 /* Set the alias set of GNU_NEW_TYPE to be that of GNU_OLD_TYPE. If this
4375 is a multi-dimensional array type, do this recursively. */
4377 static void
4378 copy_alias_set (tree gnu_new_type, tree gnu_old_type)
4380 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
4381 of a one-dimensional array, since the padding has the same alias set
4382 as the field type, but if it's a multi-dimensional array, we need to
4383 see the inner types. */
4384 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
4385 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
4386 || TYPE_IS_PADDING_P (gnu_old_type)))
4387 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
4389 /* We need to be careful here in case GNU_OLD_TYPE is an unconstrained
4390 array. In that case, it doesn't have the same shape as GNU_NEW_TYPE,
4391 so we need to go down to what does. */
4392 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
4393 gnu_old_type
4394 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
4396 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
4397 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
4398 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
4399 copy_alias_set (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type));
4401 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
4402 record_component_aliases (gnu_new_type);
4405 /* Return a TREE_LIST describing the substitutions needed to reflect
4406 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
4407 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
4408 of GNAT_SUBTYPE. The substitutions can be in any order. TREE_PURPOSE
4409 gives the tree for the discriminant and TREE_VALUES is the replacement
4410 value. They are in the form of operands to substitute_in_expr.
4411 DEFINITION is as in gnat_to_gnu_entity. */
4413 static tree
4414 substitution_list (Entity_Id gnat_subtype, Entity_Id gnat_type,
4415 tree gnu_list, bool definition)
4417 Entity_Id gnat_discrim;
4418 Node_Id gnat_value;
4420 if (No (gnat_type))
4421 gnat_type = Implementation_Base_Type (gnat_subtype);
4423 if (Has_Discriminants (gnat_type))
4424 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
4425 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
4426 Present (gnat_discrim);
4427 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
4428 gnat_value = Next_Elmt (gnat_value))
4429 /* Ignore access discriminants. */
4430 if (!Is_Access_Type (Etype (Node (gnat_value))))
4431 gnu_list = tree_cons (gnat_to_gnu_field_decl (gnat_discrim),
4432 elaborate_expression
4433 (Node (gnat_value), gnat_subtype,
4434 get_entity_name (gnat_discrim), definition,
4435 1, 0),
4436 gnu_list);
4438 return gnu_list;
4441 /* For the following two functions: for each GNAT entity, the GCC
4442 tree node used as a dummy for that entity, if any. */
4444 static GTY((length ("max_gnat_nodes"))) tree * dummy_node_table;
4446 /* Initialize the above table. */
4448 void
4449 init_dummy_type (void)
4451 Node_Id gnat_node;
4453 dummy_node_table = (tree *) ggc_alloc (max_gnat_nodes * sizeof (tree));
4455 for (gnat_node = 0; gnat_node < max_gnat_nodes; gnat_node++)
4456 dummy_node_table[gnat_node] = NULL_TREE;
4458 dummy_node_table -= First_Node_Id;
4461 /* Make a dummy type corresponding to GNAT_TYPE. */
4463 tree
4464 make_dummy_type (Entity_Id gnat_type)
4466 Entity_Id gnat_underlying;
4467 tree gnu_type;
4469 /* Find a full type for GNAT_TYPE, taking into account any class wide
4470 types. */
4471 if (Is_Class_Wide_Type (gnat_type) && Present (Equivalent_Type (gnat_type)))
4472 gnat_type = Equivalent_Type (gnat_type);
4473 else if (Ekind (gnat_type) == E_Class_Wide_Type)
4474 gnat_type = Root_Type (gnat_type);
4476 for (gnat_underlying = gnat_type;
4477 (IN (Ekind (gnat_underlying), Incomplete_Or_Private_Kind)
4478 && Present (Full_View (gnat_underlying)));
4479 gnat_underlying = Full_View (gnat_underlying))
4482 /* If it there already a dummy type, use that one. Else make one. */
4483 if (dummy_node_table[gnat_underlying])
4484 return dummy_node_table[gnat_underlying];
4486 /* If this is a record, make this a RECORD_TYPE or UNION_TYPE; else make
4487 it a VOID_TYPE. */
4488 if (Is_Unchecked_Union (gnat_underlying))
4490 gnu_type = make_node (UNION_TYPE);
4491 TYPE_UNCHECKED_UNION_P (gnu_type) = 1;
4493 else if (Is_Record_Type (gnat_underlying))
4494 gnu_type = make_node (RECORD_TYPE);
4495 else
4496 gnu_type = make_node (ENUMERAL_TYPE);
4498 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
4499 TYPE_DUMMY_P (gnu_type) = 1;
4500 if (AGGREGATE_TYPE_P (gnu_type))
4501 TYPE_STUB_DECL (gnu_type) = build_decl (TYPE_DECL, NULL_TREE, gnu_type);
4503 dummy_node_table[gnat_underlying] = gnu_type;
4505 return gnu_type;
4508 /* Return true if the size represented by GNU_SIZE can be handled by an
4509 allocation. If STATIC_P is true, consider only what can be done with a
4510 static allocation. */
4512 static bool
4513 allocatable_size_p (tree gnu_size, bool static_p)
4515 HOST_WIDE_INT our_size;
4517 /* If this is not a static allocation, the only case we want to forbid
4518 is an overflowing size. That will be converted into a raise a
4519 Storage_Error. */
4520 if (!static_p)
4521 return !(TREE_CODE (gnu_size) == INTEGER_CST
4522 && TREE_CONSTANT_OVERFLOW (gnu_size));
4524 /* Otherwise, we need to deal with both variable sizes and constant
4525 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
4526 since assemblers may not like very large sizes. */
4527 if (!host_integerp (gnu_size, 1))
4528 return false;
4530 our_size = tree_low_cst (gnu_size, 1);
4531 return (int) our_size == our_size;
4534 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
4536 static void
4537 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
4539 Node_Id gnat_temp;
4541 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
4542 gnat_temp = Next_Rep_Item (gnat_temp))
4543 if (Nkind (gnat_temp) == N_Pragma)
4545 struct attrib *attr;
4546 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
4547 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
4548 enum attr_type etype;
4550 if (Present (gnat_assoc) && Present (First (gnat_assoc))
4551 && Present (Next (First (gnat_assoc)))
4552 && (Nkind (Expression (Next (First (gnat_assoc))))
4553 == N_String_Literal))
4555 gnu_arg0 = get_identifier (TREE_STRING_POINTER
4556 (gnat_to_gnu
4557 (Expression (Next
4558 (First (gnat_assoc))))));
4559 if (Present (Next (Next (First (gnat_assoc))))
4560 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
4561 == N_String_Literal))
4562 gnu_arg1 = get_identifier (TREE_STRING_POINTER
4563 (gnat_to_gnu
4564 (Expression
4565 (Next (Next
4566 (First (gnat_assoc)))))));
4569 switch (Get_Pragma_Id (Chars (gnat_temp)))
4571 case Pragma_Machine_Attribute:
4572 etype = ATTR_MACHINE_ATTRIBUTE;
4573 break;
4575 case Pragma_Linker_Alias:
4576 etype = ATTR_LINK_ALIAS;
4577 break;
4579 case Pragma_Linker_Section:
4580 etype = ATTR_LINK_SECTION;
4581 break;
4583 case Pragma_Linker_Constructor:
4584 etype = ATTR_LINK_CONSTRUCTOR;
4585 break;
4587 case Pragma_Linker_Destructor:
4588 etype = ATTR_LINK_DESTRUCTOR;
4589 break;
4591 case Pragma_Weak_External:
4592 etype = ATTR_WEAK_EXTERNAL;
4593 break;
4595 default:
4596 continue;
4599 attr = (struct attrib *) xmalloc (sizeof (struct attrib));
4600 attr->next = *attr_list;
4601 attr->type = etype;
4602 attr->name = gnu_arg0;
4604 /* If we have an argument specified together with an attribute name,
4605 make it a single TREE_VALUE entry in a list of arguments, as GCC
4606 expects it. */
4607 if (gnu_arg1 != NULL_TREE)
4608 attr->args = build_tree_list (NULL_TREE, gnu_arg1);
4609 else
4610 attr->args = NULL_TREE;
4612 attr->error_point
4613 = Present (Next (First (gnat_assoc)))
4614 ? Expression (Next (First (gnat_assoc))) : gnat_temp;
4615 *attr_list = attr;
4619 /* Get the unpadded version of a GNAT type. */
4621 tree
4622 get_unpadded_type (Entity_Id gnat_entity)
4624 tree type = gnat_to_gnu_type (gnat_entity);
4626 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4627 type = TREE_TYPE (TYPE_FIELDS (type));
4629 return type;
4632 /* Called when we need to protect a variable object using a save_expr. */
4634 tree
4635 maybe_variable (tree gnu_operand)
4637 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
4638 || TREE_CODE (gnu_operand) == SAVE_EXPR
4639 || TREE_CODE (gnu_operand) == NULL_EXPR)
4640 return gnu_operand;
4642 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
4644 tree gnu_result = build1 (UNCONSTRAINED_ARRAY_REF,
4645 TREE_TYPE (gnu_operand),
4646 variable_size (TREE_OPERAND (gnu_operand, 0)));
4648 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result)
4649 = TYPE_READONLY (TREE_TYPE (TREE_TYPE (gnu_operand)));
4650 return gnu_result;
4652 else
4653 return variable_size (gnu_operand);
4656 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
4657 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
4658 return the GCC tree to use for that expression. GNU_NAME is the
4659 qualification to use if an external name is appropriate and DEFINITION is
4660 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
4661 we need a result. Otherwise, we are just elaborating this for
4662 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
4663 purposes even if it isn't needed for code generation. */
4665 static tree
4666 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity,
4667 tree gnu_name, bool definition, bool need_value,
4668 bool need_debug)
4670 tree gnu_expr;
4672 /* If we already elaborated this expression (e.g., it was involved
4673 in the definition of a private type), use the old value. */
4674 if (present_gnu_tree (gnat_expr))
4675 return get_gnu_tree (gnat_expr);
4677 /* If we don't need a value and this is static or a discriment, we
4678 don't need to do anything. */
4679 else if (!need_value
4680 && (Is_OK_Static_Expression (gnat_expr)
4681 || (Nkind (gnat_expr) == N_Identifier
4682 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
4683 return 0;
4685 /* Otherwise, convert this tree to its GCC equivalent. */
4686 gnu_expr
4687 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
4688 gnu_name, definition, need_debug);
4690 /* Save the expression in case we try to elaborate this entity again. Since
4691 this is not a DECL, don't check it. Don't save if it's a discriminant. */
4692 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
4693 save_gnu_tree (gnat_expr, gnu_expr, true);
4695 return need_value ? gnu_expr : error_mark_node;
4698 /* Similar, but take a GNU expression. */
4700 static tree
4701 elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
4702 tree gnu_expr, tree gnu_name, bool definition,
4703 bool need_debug)
4705 tree gnu_decl = NULL_TREE;
4706 /* Strip any conversions to see if the expression is a readonly variable.
4707 ??? This really should remain readonly, but we have to think about
4708 the typing of the tree here. */
4709 tree gnu_inner_expr = remove_conversions (gnu_expr, true);
4710 bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
4711 bool expr_variable;
4713 /* In most cases, we won't see a naked FIELD_DECL here because a
4714 discriminant reference will have been replaced with a COMPONENT_REF
4715 when the type is being elaborated. However, there are some cases
4716 involving child types where we will. So convert it to a COMPONENT_REF
4717 here. We have to hope it will be at the highest level of the
4718 expression in these cases. */
4719 if (TREE_CODE (gnu_expr) == FIELD_DECL)
4720 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
4721 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
4722 gnu_expr, NULL_TREE);
4724 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
4725 that is a constant, make a variable that is initialized to contain the
4726 bound when the package containing the definition is elaborated. If
4727 this entity is defined at top level and a bound or discriminant value
4728 isn't a constant or a reference to a discriminant, replace the bound
4729 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
4730 rely here on the fact that an expression cannot contain both the
4731 discriminant and some other variable. */
4733 expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
4734 && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
4735 && TREE_READONLY (gnu_inner_expr))
4736 && !CONTAINS_PLACEHOLDER_P (gnu_expr));
4738 /* If this is a static expression or contains a discriminant, we don't
4739 need the variable for debugging (and can't elaborate anyway if a
4740 discriminant). */
4741 if (need_debug
4742 && (Is_OK_Static_Expression (gnat_expr)
4743 || CONTAINS_PLACEHOLDER_P (gnu_expr)))
4744 need_debug = false;
4746 /* Now create the variable if we need it. */
4747 if (need_debug || (expr_variable && expr_global))
4748 gnu_decl
4749 = create_var_decl (create_concat_name (gnat_entity,
4750 IDENTIFIER_POINTER (gnu_name)),
4751 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
4752 !need_debug, Is_Public (gnat_entity),
4753 !definition, false, NULL, gnat_entity);
4755 /* We only need to use this variable if we are in global context since GCC
4756 can do the right thing in the local case. */
4757 if (expr_global && expr_variable)
4758 return gnu_decl;
4759 else if (!expr_variable)
4760 return gnu_expr;
4761 else
4762 return maybe_variable (gnu_expr);
4765 /* Create a record type that contains a field of TYPE with a starting bit
4766 position so that it is aligned to ALIGN bits and is SIZE bytes long. */
4768 tree
4769 make_aligning_type (tree type, int align, tree size)
4771 tree record_type = make_node (RECORD_TYPE);
4772 tree place = build0 (PLACEHOLDER_EXPR, record_type);
4773 tree size_addr_place = convert (sizetype,
4774 build_unary_op (ADDR_EXPR, NULL_TREE,
4775 place));
4776 tree name = TYPE_NAME (type);
4777 tree pos, field;
4779 if (TREE_CODE (name) == TYPE_DECL)
4780 name = DECL_NAME (name);
4782 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
4784 /* The bit position is obtained by "and"ing the alignment minus 1
4785 with the two's complement of the address and multiplying
4786 by the number of bits per unit. Do all this in sizetype. */
4787 pos = size_binop (MULT_EXPR,
4788 convert (bitsizetype,
4789 size_binop (BIT_AND_EXPR,
4790 size_diffop (size_zero_node,
4791 size_addr_place),
4792 ssize_int ((align / BITS_PER_UNIT)
4793 - 1))),
4794 bitsize_unit_node);
4796 /* Create the field, with -1 as the 'addressable' indication to avoid the
4797 creation of a bitfield. We don't need one, it would have damaging
4798 consequences on the alignment computation, and create_field_decl would
4799 make one without this special argument, for instance because of the
4800 complex position expression. */
4801 field = create_field_decl (get_identifier ("F"), type, record_type, 1, size,
4802 pos, -1);
4804 finish_record_type (record_type, field, true, false);
4805 TYPE_ALIGN (record_type) = BIGGEST_ALIGNMENT;
4806 TYPE_SIZE (record_type)
4807 = size_binop (PLUS_EXPR,
4808 size_binop (MULT_EXPR, convert (bitsizetype, size),
4809 bitsize_unit_node),
4810 bitsize_int (align));
4811 TYPE_SIZE_UNIT (record_type)
4812 = size_binop (PLUS_EXPR, size, size_int (align / BITS_PER_UNIT));
4813 copy_alias_set (record_type, type);
4814 return record_type;
4817 /* TYPE is a RECORD_TYPE, UNION_TYPE, or QUAL_UNION_TYPE, with BLKmode that's
4818 being used as the field type of a packed record. See if we can rewrite it
4819 as a record that has a non-BLKmode type, which we can pack tighter. If so,
4820 return the new type. If not, return the original type. */
4822 static tree
4823 make_packable_type (tree type)
4825 tree new_type = make_node (TREE_CODE (type));
4826 tree field_list = NULL_TREE;
4827 tree old_field;
4829 /* Copy the name and flags from the old type to that of the new and set
4830 the alignment to try for an integral type. For QUAL_UNION_TYPE,
4831 also copy the size. */
4832 TYPE_NAME (new_type) = TYPE_NAME (type);
4833 TYPE_JUSTIFIED_MODULAR_P (new_type)
4834 = TYPE_JUSTIFIED_MODULAR_P (type);
4835 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
4837 if (TREE_CODE (type) == RECORD_TYPE)
4838 TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
4839 else if (TREE_CODE (type) == QUAL_UNION_TYPE)
4841 TYPE_SIZE (new_type) = TYPE_SIZE (type);
4842 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
4845 TYPE_ALIGN (new_type)
4846 = ((HOST_WIDE_INT) 1
4847 << (floor_log2 (tree_low_cst (TYPE_SIZE (type), 1) - 1) + 1));
4849 /* Now copy the fields, keeping the position and size. */
4850 for (old_field = TYPE_FIELDS (type); old_field;
4851 old_field = TREE_CHAIN (old_field))
4853 tree new_field_type = TREE_TYPE (old_field);
4854 tree new_field;
4856 if (TYPE_MODE (new_field_type) == BLKmode
4857 && (TREE_CODE (new_field_type) == RECORD_TYPE
4858 || TREE_CODE (new_field_type) == UNION_TYPE
4859 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
4860 && host_integerp (TYPE_SIZE (new_field_type), 1))
4861 new_field_type = make_packable_type (new_field_type);
4863 new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
4864 new_type, TYPE_PACKED (type),
4865 DECL_SIZE (old_field),
4866 bit_position (old_field),
4867 !DECL_NONADDRESSABLE_P (old_field));
4869 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
4870 SET_DECL_ORIGINAL_FIELD
4871 (new_field, (DECL_ORIGINAL_FIELD (old_field)
4872 ? DECL_ORIGINAL_FIELD (old_field) : old_field));
4874 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
4875 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
4877 TREE_CHAIN (new_field) = field_list;
4878 field_list = new_field;
4881 finish_record_type (new_type, nreverse (field_list), true, true);
4882 copy_alias_set (new_type, type);
4883 return TYPE_MODE (new_type) == BLKmode ? type : new_type;
4886 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
4887 if needed. We have already verified that SIZE and TYPE are large enough.
4889 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
4890 to issue a warning.
4892 IS_USER_TYPE is true if we must be sure we complete the original type.
4894 DEFINITION is true if this type is being defined.
4896 SAME_RM_SIZE is true if the RM_Size of the resulting type is to be
4897 set to its TYPE_SIZE; otherwise, it's set to the RM_Size of the original
4898 type. */
4900 tree
4901 maybe_pad_type (tree type, tree size, unsigned int align,
4902 Entity_Id gnat_entity, const char *name_trailer,
4903 bool is_user_type, bool definition, bool same_rm_size)
4905 tree orig_size = TYPE_SIZE (type);
4906 tree record;
4907 tree field;
4909 /* If TYPE is a padded type, see if it agrees with any size and alignment
4910 we were given. If so, return the original type. Otherwise, strip
4911 off the padding, since we will either be returning the inner type
4912 or repadding it. If no size or alignment is specified, use that of
4913 the original padded type. */
4915 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4917 if ((!size
4918 || operand_equal_p (round_up (size,
4919 MAX (align, TYPE_ALIGN (type))),
4920 round_up (TYPE_SIZE (type),
4921 MAX (align, TYPE_ALIGN (type))),
4923 && (align == 0 || align == TYPE_ALIGN (type)))
4924 return type;
4926 if (!size)
4927 size = TYPE_SIZE (type);
4928 if (align == 0)
4929 align = TYPE_ALIGN (type);
4931 type = TREE_TYPE (TYPE_FIELDS (type));
4932 orig_size = TYPE_SIZE (type);
4935 /* If the size is either not being changed or is being made smaller (which
4936 is not done here (and is only valid for bitfields anyway), show the size
4937 isn't changing. Likewise, clear the alignment if it isn't being
4938 changed. Then return if we aren't doing anything. */
4940 if (size
4941 && (operand_equal_p (size, orig_size, 0)
4942 || (TREE_CODE (orig_size) == INTEGER_CST
4943 && tree_int_cst_lt (size, orig_size))))
4944 size = NULL_TREE;
4946 if (align == TYPE_ALIGN (type))
4947 align = 0;
4949 if (align == 0 && !size)
4950 return type;
4952 /* We used to modify the record in place in some cases, but that could
4953 generate incorrect debugging information. So make a new record
4954 type and name. */
4955 record = make_node (RECORD_TYPE);
4957 if (Present (gnat_entity))
4958 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
4960 /* If we were making a type, complete the original type and give it a
4961 name. */
4962 if (is_user_type)
4963 create_type_decl (get_entity_name (gnat_entity), type,
4964 NULL, !Comes_From_Source (gnat_entity),
4965 !(TYPE_NAME (type)
4966 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4967 && DECL_IGNORED_P (TYPE_NAME (type))),
4968 gnat_entity);
4970 /* If we are changing the alignment and the input type is a record with
4971 BLKmode and a small constant size, try to make a form that has an
4972 integral mode. That might allow this record to have an integral mode,
4973 which will be much more efficient. There is no point in doing this if a
4974 size is specified unless it is also smaller than the biggest alignment
4975 and it is incorrect to do this if the size of the original type is not a
4976 multiple of the alignment. */
4977 if (align != 0
4978 && TREE_CODE (type) == RECORD_TYPE
4979 && TYPE_MODE (type) == BLKmode
4980 && host_integerp (orig_size, 1)
4981 && compare_tree_int (orig_size, BIGGEST_ALIGNMENT) <= 0
4982 && (!size
4983 || (TREE_CODE (size) == INTEGER_CST
4984 && compare_tree_int (size, BIGGEST_ALIGNMENT) <= 0))
4985 && tree_low_cst (orig_size, 1) % align == 0)
4986 type = make_packable_type (type);
4988 field = create_field_decl (get_identifier ("F"), type, record, 0,
4989 NULL_TREE, bitsize_zero_node, 1);
4991 DECL_INTERNAL_P (field) = 1;
4992 TYPE_SIZE (record) = size ? size : orig_size;
4993 TYPE_SIZE_UNIT (record)
4994 = (size ? convert (sizetype,
4995 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node))
4996 : TYPE_SIZE_UNIT (type));
4998 TYPE_ALIGN (record) = align;
4999 TYPE_IS_PADDING_P (record) = 1;
5000 TYPE_VOLATILE (record)
5001 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
5002 finish_record_type (record, field, true, false);
5004 /* Keep the RM_Size of the padded record as that of the old record
5005 if requested. */
5006 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : rm_size (type));
5008 /* Unless debugging information isn't being written for the input type,
5009 write a record that shows what we are a subtype of and also make a
5010 variable that indicates our size, if variable. */
5011 if (TYPE_NAME (record) && AGGREGATE_TYPE_P (type)
5012 && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
5013 || !DECL_IGNORED_P (TYPE_NAME (type))))
5015 tree marker = make_node (RECORD_TYPE);
5016 tree name = (TREE_CODE (TYPE_NAME (record)) == TYPE_DECL
5017 ? DECL_NAME (TYPE_NAME (record))
5018 : TYPE_NAME (record));
5019 tree orig_name = TYPE_NAME (type);
5021 if (TREE_CODE (orig_name) == TYPE_DECL)
5022 orig_name = DECL_NAME (orig_name);
5024 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
5025 finish_record_type (marker,
5026 create_field_decl (orig_name, integer_type_node,
5027 marker, 0, NULL_TREE, NULL_TREE,
5029 false, false);
5031 if (size && TREE_CODE (size) != INTEGER_CST && definition)
5032 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
5033 bitsizetype, TYPE_SIZE (record), false, false, false,
5034 false, NULL, gnat_entity);
5037 type = record;
5039 if (CONTAINS_PLACEHOLDER_P (orig_size))
5040 orig_size = max_size (orig_size, true);
5042 /* If the size was widened explicitly, maybe give a warning. */
5043 if (size && Present (gnat_entity)
5044 && !operand_equal_p (size, orig_size, 0)
5045 && !(TREE_CODE (size) == INTEGER_CST
5046 && TREE_CODE (orig_size) == INTEGER_CST
5047 && tree_int_cst_lt (size, orig_size)))
5049 Node_Id gnat_error_node = Empty;
5051 if (Is_Packed_Array_Type (gnat_entity))
5052 gnat_entity = Associated_Node_For_Itype (gnat_entity);
5054 if ((Ekind (gnat_entity) == E_Component
5055 || Ekind (gnat_entity) == E_Discriminant)
5056 && Present (Component_Clause (gnat_entity)))
5057 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
5058 else if (Present (Size_Clause (gnat_entity)))
5059 gnat_error_node = Expression (Size_Clause (gnat_entity));
5061 /* Generate message only for entities that come from source, since
5062 if we have an entity created by expansion, the message will be
5063 generated for some other corresponding source entity. */
5064 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
5065 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
5066 gnat_entity,
5067 size_diffop (size, orig_size));
5069 else if (*name_trailer == 'C' && !Is_Internal (gnat_entity))
5070 post_error_ne_tree ("component of& padded{ by ^ bits}?",
5071 gnat_entity, gnat_entity,
5072 size_diffop (size, orig_size));
5075 return type;
5078 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
5079 the value passed against the list of choices. */
5081 tree
5082 choices_to_gnu (tree operand, Node_Id choices)
5084 Node_Id choice;
5085 Node_Id gnat_temp;
5086 tree result = integer_zero_node;
5087 tree this_test, low = 0, high = 0, single = 0;
5089 for (choice = First (choices); Present (choice); choice = Next (choice))
5091 switch (Nkind (choice))
5093 case N_Range:
5094 low = gnat_to_gnu (Low_Bound (choice));
5095 high = gnat_to_gnu (High_Bound (choice));
5097 /* There's no good type to use here, so we might as well use
5098 integer_type_node. */
5099 this_test
5100 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5101 build_binary_op (GE_EXPR, integer_type_node,
5102 operand, low),
5103 build_binary_op (LE_EXPR, integer_type_node,
5104 operand, high));
5106 break;
5108 case N_Subtype_Indication:
5109 gnat_temp = Range_Expression (Constraint (choice));
5110 low = gnat_to_gnu (Low_Bound (gnat_temp));
5111 high = gnat_to_gnu (High_Bound (gnat_temp));
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));
5119 break;
5121 case N_Identifier:
5122 case N_Expanded_Name:
5123 /* This represents either a subtype range, an enumeration
5124 literal, or a constant Ekind says which. If an enumeration
5125 literal or constant, fall through to the next case. */
5126 if (Ekind (Entity (choice)) != E_Enumeration_Literal
5127 && Ekind (Entity (choice)) != E_Constant)
5129 tree type = gnat_to_gnu_type (Entity (choice));
5131 low = TYPE_MIN_VALUE (type);
5132 high = TYPE_MAX_VALUE (type);
5134 this_test
5135 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5136 build_binary_op (GE_EXPR, integer_type_node,
5137 operand, low),
5138 build_binary_op (LE_EXPR, integer_type_node,
5139 operand, high));
5140 break;
5142 /* ... fall through ... */
5143 case N_Character_Literal:
5144 case N_Integer_Literal:
5145 single = gnat_to_gnu (choice);
5146 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
5147 single);
5148 break;
5150 case N_Others_Choice:
5151 this_test = integer_one_node;
5152 break;
5154 default:
5155 gcc_unreachable ();
5158 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
5159 result, this_test);
5162 return result;
5165 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
5166 placed in GNU_RECORD_TYPE.
5168 PACKED is 1 if the enclosing record is packed and -1 if the enclosing
5169 record has a Component_Alignment of Storage_Unit.
5171 DEFINITION is true if this field is for a record being defined. */
5173 static tree
5174 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
5175 bool definition)
5177 tree gnu_field_id = get_entity_name (gnat_field);
5178 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
5179 tree gnu_pos = 0;
5180 tree gnu_size = 0;
5181 tree gnu_field;
5182 bool needs_strict_alignment
5183 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
5184 || Treat_As_Volatile (gnat_field));
5186 /* If this field requires strict alignment or contains an item of
5187 variable sized, pretend it isn't packed. */
5188 if (needs_strict_alignment || is_variable_size (gnu_field_type))
5189 packed = 0;
5191 /* For packed records, this is one of the few occasions on which we use
5192 the official RM size for discrete or fixed-point components, instead
5193 of the normal GNAT size stored in Esize. See description in Einfo:
5194 "Handling of Type'Size Values" for further details. */
5196 if (packed == 1)
5197 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
5198 gnat_field, FIELD_DECL, false, true);
5200 if (Known_Static_Esize (gnat_field))
5201 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5202 gnat_field, FIELD_DECL, false, true);
5204 /* If we are packing this record, have a specified size that's smaller than
5205 that of the field type, or a position is specified, and the field type is
5206 also a record that's BLKmode and with a small constant size, see if we
5207 can get a better form of the type that allows more packing. If we can,
5208 show a size was specified for it if there wasn't one so we know to make
5209 this a bitfield and avoid making things wider. */
5210 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5211 && TYPE_MODE (gnu_field_type) == BLKmode
5212 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
5213 && compare_tree_int (TYPE_SIZE (gnu_field_type), BIGGEST_ALIGNMENT) <= 0
5214 && (packed == 1
5215 || (gnu_size
5216 && tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type)))
5217 || Present (Component_Clause (gnat_field))))
5219 /* See what the alternate type and size would be. */
5220 tree gnu_packable_type = make_packable_type (gnu_field_type);
5222 /* Compute whether we should avoid the substitution. */
5223 int reject =
5224 /* There is no point substituting if there is no change. */
5225 (gnu_packable_type == gnu_field_type
5227 /* The size of an aliased field must be an exact multiple of the
5228 type's alignment, which the substitution might increase. Reject
5229 substitutions that would so invalidate a component clause when the
5230 specified position is byte aligned, as the change would have no
5231 real benefit from the packing standpoint anyway. */
5232 (Is_Aliased (gnat_field)
5233 && Present (Component_Clause (gnat_field))
5234 && UI_To_Int (Component_Bit_Offset (gnat_field)) % BITS_PER_UNIT == 0
5235 && tree_low_cst (gnu_size, 1) % TYPE_ALIGN (gnu_packable_type) != 0)
5238 /* Substitute unless told otherwise. */
5239 if (!reject)
5241 gnu_field_type = gnu_packable_type;
5243 if (gnu_size == 0)
5244 gnu_size = rm_size (gnu_field_type);
5248 /* If we are packing the record and the field is BLKmode, round the
5249 size up to a byte boundary. */
5250 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
5251 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
5253 if (Present (Component_Clause (gnat_field)))
5255 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
5256 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5257 gnat_field, FIELD_DECL, false, true);
5259 /* Ensure the position does not overlap with the parent subtype,
5260 if there is one. */
5261 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
5263 tree gnu_parent
5264 = gnat_to_gnu_type (Parent_Subtype
5265 (Underlying_Type (Scope (gnat_field))));
5267 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
5268 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
5270 post_error_ne_tree
5271 ("offset of& must be beyond parent{, minimum allowed is ^}",
5272 First_Bit (Component_Clause (gnat_field)), gnat_field,
5273 TYPE_SIZE_UNIT (gnu_parent));
5277 /* If this field needs strict alignment, ensure the record is
5278 sufficiently aligned and that that position and size are
5279 consistent with the alignment. */
5280 if (needs_strict_alignment)
5282 tree gnu_rounded_size = round_up (rm_size (gnu_field_type),
5283 TYPE_ALIGN (gnu_field_type));
5285 TYPE_ALIGN (gnu_record_type)
5286 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
5288 /* If Atomic, the size must match exactly that of the field. */
5289 if ((Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
5290 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
5292 post_error_ne_tree
5293 ("atomic field& must be natural size of type{ (^)}",
5294 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5295 TYPE_SIZE (gnu_field_type));
5297 gnu_size = NULL_TREE;
5300 /* If Aliased, the size must match exactly the rounded size. We
5301 used to be more accommodating here and accept greater sizes, but
5302 fully supporting this case on big-endian platforms would require
5303 switching to a more involved layout for the field. */
5304 else if (Is_Aliased (gnat_field)
5305 && gnu_size
5306 && ! operand_equal_p (gnu_size, gnu_rounded_size, 0))
5308 post_error_ne_tree
5309 ("size of aliased field& must be ^ bits",
5310 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5311 gnu_rounded_size);
5312 gnu_size = NULL_TREE;
5315 if (!integer_zerop (size_binop
5316 (TRUNC_MOD_EXPR, gnu_pos,
5317 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
5319 if (Is_Aliased (gnat_field))
5320 post_error_ne_num
5321 ("position of aliased field& must be multiple of ^ bits",
5322 First_Bit (Component_Clause (gnat_field)), gnat_field,
5323 TYPE_ALIGN (gnu_field_type));
5325 else if (Treat_As_Volatile (gnat_field))
5326 post_error_ne_num
5327 ("position of volatile field& must be multiple of ^ bits",
5328 First_Bit (Component_Clause (gnat_field)), gnat_field,
5329 TYPE_ALIGN (gnu_field_type));
5331 else if (Strict_Alignment (Etype (gnat_field)))
5332 post_error_ne_num
5333 ("position of & with aliased or tagged components not multiple of ^ bits",
5334 First_Bit (Component_Clause (gnat_field)), gnat_field,
5335 TYPE_ALIGN (gnu_field_type));
5336 else
5337 gcc_unreachable ();
5339 gnu_pos = NULL_TREE;
5343 if (Is_Atomic (gnat_field))
5344 check_ok_for_atomic (gnu_field_type, gnat_field, false);
5347 /* If the record has rep clauses and this is the tag field, make a rep
5348 clause for it as well. */
5349 else if (Has_Specified_Layout (Scope (gnat_field))
5350 && Chars (gnat_field) == Name_uTag)
5352 gnu_pos = bitsize_zero_node;
5353 gnu_size = TYPE_SIZE (gnu_field_type);
5356 /* We need to make the size the maximum for the type if it is
5357 self-referential and an unconstrained type. In that case, we can't
5358 pack the field since we can't make a copy to align it. */
5359 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5360 && !gnu_size
5361 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
5362 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
5364 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
5365 packed = 0;
5368 /* If no size is specified (or if there was an error), don't specify a
5369 position. */
5370 if (!gnu_size)
5371 gnu_pos = NULL_TREE;
5372 else
5374 /* If the field's type is justified modular, we would need to remove
5375 the wrapper to (better) meet the layout requirements. However we
5376 can do so only if the field is not aliased to preserve the unique
5377 layout and if the prescribed size is not greater than that of the
5378 packed array to preserve the justification. */
5379 if (!needs_strict_alignment
5380 && TREE_CODE (gnu_field_type) == RECORD_TYPE
5381 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
5382 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
5383 <= 0)
5384 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
5386 gnu_field_type
5387 = make_type_from_size (gnu_field_type, gnu_size,
5388 Has_Biased_Representation (gnat_field));
5389 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
5390 "PAD", false, definition, true);
5393 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
5394 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
5396 /* Now create the decl for the field. */
5397 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
5398 packed, gnu_size, gnu_pos,
5399 Is_Aliased (gnat_field));
5400 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
5401 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
5403 if (Ekind (gnat_field) == E_Discriminant)
5404 DECL_DISCRIMINANT_NUMBER (gnu_field)
5405 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
5407 return gnu_field;
5410 /* Return true if TYPE is a type with variable size, a padding type with a
5411 field of variable size or is a record that has a field such a field. */
5413 static bool
5414 is_variable_size (tree type)
5416 tree field;
5418 /* We need not be concerned about this at all if we don't have
5419 strict alignment. */
5420 if (!STRICT_ALIGNMENT)
5421 return false;
5422 else if (!TREE_CONSTANT (TYPE_SIZE (type)))
5423 return true;
5424 else if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type)
5425 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
5426 return true;
5427 else if (TREE_CODE (type) != RECORD_TYPE
5428 && TREE_CODE (type) != UNION_TYPE
5429 && TREE_CODE (type) != QUAL_UNION_TYPE)
5430 return false;
5432 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5433 if (is_variable_size (TREE_TYPE (field)))
5434 return true;
5436 return false;
5439 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
5440 of GCC trees for fields that are in the record and have already been
5441 processed. When called from gnat_to_gnu_entity during the processing of a
5442 record type definition, the GCC nodes for the discriminants will be on
5443 the chain. The other calls to this function are recursive calls from
5444 itself for the Component_List of a variant and the chain is empty.
5446 PACKED is 1 if this is for a record with "pragma pack" and -1 is this is
5447 for a record type with "pragma component_alignment (storage_unit)".
5449 DEFINITION is true if we are defining this record.
5451 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
5452 with a rep clause is to be added. If it is nonzero, that is all that
5453 should be done with such fields.
5455 CANCEL_ALIGNMENT, if true, means the alignment should be zeroed before
5456 laying out the record. This means the alignment only serves to force fields
5457 to be bitfields, but not require the record to be that aligned. This is
5458 used for variants.
5460 ALL_REP, if true, means a rep clause was found for all the fields. This
5461 simplifies the logic since we know we're not in the mixed case.
5463 DEFER_DEBUG, if true, means that the debugging routines should not be
5464 called when finishing constructing the record type.
5466 The processing of the component list fills in the chain with all of the
5467 fields of the record and then the record type is finished. */
5469 static void
5470 components_to_record (tree gnu_record_type, Node_Id component_list,
5471 tree gnu_field_list, int packed, bool definition,
5472 tree *p_gnu_rep_list, bool cancel_alignment,
5473 bool all_rep, bool defer_debug)
5475 Node_Id component_decl;
5476 Entity_Id gnat_field;
5477 Node_Id variant_part;
5478 Node_Id variant;
5479 tree gnu_our_rep_list = NULL_TREE;
5480 tree gnu_field, gnu_last;
5481 bool layout_with_rep = false;
5482 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
5484 /* For each variable within each component declaration create a GCC field
5485 and add it to the list, skipping any pragmas in the list. */
5487 if (Present (Component_Items (component_list)))
5488 for (component_decl = First_Non_Pragma (Component_Items (component_list));
5489 Present (component_decl);
5490 component_decl = Next_Non_Pragma (component_decl))
5492 gnat_field = Defining_Entity (component_decl);
5494 if (Chars (gnat_field) == Name_uParent)
5495 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
5496 else
5498 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
5499 packed, definition);
5501 /* If this is the _Tag field, put it before any discriminants,
5502 instead of after them as is the case for all other fields.
5503 Ignore field of void type if only annotating. */
5504 if (Chars (gnat_field) == Name_uTag)
5505 gnu_field_list = chainon (gnu_field_list, gnu_field);
5506 else
5508 TREE_CHAIN (gnu_field) = gnu_field_list;
5509 gnu_field_list = gnu_field;
5513 save_gnu_tree (gnat_field, gnu_field, false);
5516 /* At the end of the component list there may be a variant part. */
5517 variant_part = Variant_Part (component_list);
5519 /* If this is an unchecked union, each variant must have exactly one
5520 component, each of which becomes one component of this union. */
5521 if (TREE_CODE (gnu_record_type) == UNION_TYPE
5522 && TYPE_UNCHECKED_UNION_P (gnu_record_type)
5523 && Present (variant_part))
5524 for (variant = First_Non_Pragma (Variants (variant_part));
5525 Present (variant);
5526 variant = Next_Non_Pragma (variant))
5528 component_decl
5529 = First_Non_Pragma (Component_Items (Component_List (variant)));
5530 gnat_field = Defining_Entity (component_decl);
5531 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
5532 definition);
5533 TREE_CHAIN (gnu_field) = gnu_field_list;
5534 gnu_field_list = gnu_field;
5535 save_gnu_tree (gnat_field, gnu_field, false);
5538 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
5539 mutually exclusive and should go in the same memory. To do this we need
5540 to treat each variant as a record whose elements are created from the
5541 component list for the variant. So here we create the records from the
5542 lists for the variants and put them all into the QUAL_UNION_TYPE. */
5543 else if (Present (variant_part))
5545 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
5546 Node_Id variant;
5547 tree gnu_union_type = make_node (QUAL_UNION_TYPE);
5548 tree gnu_union_field;
5549 tree gnu_variant_list = NULL_TREE;
5550 tree gnu_name = TYPE_NAME (gnu_record_type);
5551 tree gnu_var_name
5552 = concat_id_with_name
5553 (get_identifier (Get_Name_String (Chars (Name (variant_part)))),
5554 "XVN");
5556 if (TREE_CODE (gnu_name) == TYPE_DECL)
5557 gnu_name = DECL_NAME (gnu_name);
5559 TYPE_NAME (gnu_union_type)
5560 = concat_id_with_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
5561 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
5563 for (variant = First_Non_Pragma (Variants (variant_part));
5564 Present (variant);
5565 variant = Next_Non_Pragma (variant))
5567 tree gnu_variant_type = make_node (RECORD_TYPE);
5568 tree gnu_inner_name;
5569 tree gnu_qual;
5571 Get_Variant_Encoding (variant);
5572 gnu_inner_name = get_identifier (Name_Buffer);
5573 TYPE_NAME (gnu_variant_type)
5574 = concat_id_with_name (TYPE_NAME (gnu_union_type),
5575 IDENTIFIER_POINTER (gnu_inner_name));
5577 /* Set the alignment of the inner type in case we need to make
5578 inner objects into bitfields, but then clear it out
5579 so the record actually gets only the alignment required. */
5580 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
5581 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
5583 /* Similarly, if the outer record has a size specified and all fields
5584 have record rep clauses, we can propagate the size into the
5585 variant part. */
5586 if (all_rep_and_size)
5588 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
5589 TYPE_SIZE_UNIT (gnu_variant_type)
5590 = TYPE_SIZE_UNIT (gnu_record_type);
5593 components_to_record (gnu_variant_type, Component_List (variant),
5594 NULL_TREE, packed, definition,
5595 &gnu_our_rep_list, !all_rep_and_size, all_rep,
5596 false);
5598 gnu_qual = choices_to_gnu (gnu_discriminant,
5599 Discrete_Choices (variant));
5601 Set_Present_Expr (variant, annotate_value (gnu_qual));
5602 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
5603 gnu_union_type, 0,
5604 (all_rep_and_size
5605 ? TYPE_SIZE (gnu_record_type) : 0),
5606 (all_rep_and_size
5607 ? bitsize_zero_node : 0),
5610 DECL_INTERNAL_P (gnu_field) = 1;
5611 DECL_QUALIFIER (gnu_field) = gnu_qual;
5612 TREE_CHAIN (gnu_field) = gnu_variant_list;
5613 gnu_variant_list = gnu_field;
5616 /* We use to delete the empty variants from the end. However,
5617 we no longer do that because we need them to generate complete
5618 debugging information for the variant record. Otherwise,
5619 the union type definition will be missing the fields associated
5620 to these empty variants. */
5622 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
5623 if (gnu_variant_list)
5625 if (all_rep_and_size)
5627 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
5628 TYPE_SIZE_UNIT (gnu_union_type)
5629 = TYPE_SIZE_UNIT (gnu_record_type);
5632 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
5633 all_rep_and_size, false);
5635 gnu_union_field
5636 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
5637 packed,
5638 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
5639 all_rep ? bitsize_zero_node : 0, 0);
5641 DECL_INTERNAL_P (gnu_union_field) = 1;
5642 TREE_CHAIN (gnu_union_field) = gnu_field_list;
5643 gnu_field_list = gnu_union_field;
5647 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
5648 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
5649 in a separate pass since we want to handle the discriminants but can't
5650 play with them until we've used them in debugging data above.
5652 ??? Note: if we then reorder them, debugging information will be wrong,
5653 but there's nothing that can be done about this at the moment. */
5655 for (gnu_field = gnu_field_list, gnu_last = NULL_TREE; gnu_field; )
5657 if (DECL_FIELD_OFFSET (gnu_field))
5659 tree gnu_next = TREE_CHAIN (gnu_field);
5661 if (!gnu_last)
5662 gnu_field_list = gnu_next;
5663 else
5664 TREE_CHAIN (gnu_last) = gnu_next;
5666 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
5667 gnu_our_rep_list = gnu_field;
5668 gnu_field = gnu_next;
5670 else
5672 gnu_last = gnu_field;
5673 gnu_field = TREE_CHAIN (gnu_field);
5677 /* If we have any items in our rep'ed field list, it is not the case that all
5678 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
5679 set it and ignore the items. Otherwise, sort the fields by bit position
5680 and put them into their own record if we have any fields without
5681 rep clauses. */
5682 if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
5683 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
5684 else if (gnu_our_rep_list)
5686 tree gnu_rep_type
5687 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
5688 int len = list_length (gnu_our_rep_list);
5689 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
5690 int i;
5692 /* Set/abuse DECL_FCONTEXT to increasing integers so we have a
5693 stable sort. */
5694 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
5695 gnu_field = TREE_CHAIN (gnu_field), i++)
5697 gnu_arr[i] = gnu_field;
5698 DECL_FCONTEXT (gnu_field) = size_int (i);
5701 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
5703 /* Put the fields in the list in order of increasing position, which
5704 means we start from the end. */
5705 gnu_our_rep_list = NULL_TREE;
5706 for (i = len - 1; i >= 0; i--)
5708 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
5709 gnu_our_rep_list = gnu_arr[i];
5710 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
5711 DECL_FCONTEXT (gnu_arr[i]) = NULL_TREE;
5714 if (gnu_field_list)
5716 finish_record_type (gnu_rep_type, gnu_our_rep_list, true, false);
5717 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
5718 gnu_record_type, 0, 0, 0, 1);
5719 DECL_INTERNAL_P (gnu_field) = 1;
5720 gnu_field_list = chainon (gnu_field_list, gnu_field);
5722 else
5724 layout_with_rep = true;
5725 gnu_field_list = nreverse (gnu_our_rep_list);
5729 if (cancel_alignment)
5730 TYPE_ALIGN (gnu_record_type) = 0;
5732 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
5733 layout_with_rep, defer_debug);
5736 /* Called via qsort from the above. Returns -1, 1, depending on the
5737 bit positions and ordinals of the two fields. */
5739 static int
5740 compare_field_bitpos (const PTR rt1, const PTR rt2)
5742 tree *t1 = (tree *) rt1;
5743 tree *t2 = (tree *) rt2;
5745 if (tree_int_cst_equal (bit_position (*t1), bit_position (*t2)))
5746 return
5747 (tree_int_cst_lt (DECL_FCONTEXT (*t1), DECL_FCONTEXT (*t2))
5748 ? -1 : 1);
5749 else if (tree_int_cst_lt (bit_position (*t1), bit_position (*t2)))
5750 return -1;
5751 else
5752 return 1;
5755 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
5756 placed into an Esize, Component_Bit_Offset, or Component_Size value
5757 in the GNAT tree. */
5759 static Uint
5760 annotate_value (tree gnu_size)
5762 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
5763 TCode tcode;
5764 Node_Ref_Or_Val ops[3], ret;
5765 int i;
5766 int size;
5768 /* See if we've already saved the value for this node. */
5769 if (EXPR_P (gnu_size) && TREE_COMPLEXITY (gnu_size))
5770 return (Node_Ref_Or_Val) TREE_COMPLEXITY (gnu_size);
5772 /* If we do not return inside this switch, TCODE will be set to the
5773 code to use for a Create_Node operand and LEN (set above) will be
5774 the number of recursive calls for us to make. */
5776 switch (TREE_CODE (gnu_size))
5778 case INTEGER_CST:
5779 if (TREE_OVERFLOW (gnu_size))
5780 return No_Uint;
5782 /* This may have come from a conversion from some smaller type,
5783 so ensure this is in bitsizetype. */
5784 gnu_size = convert (bitsizetype, gnu_size);
5786 /* For negative values, use NEGATE_EXPR of the supplied value. */
5787 if (tree_int_cst_sgn (gnu_size) < 0)
5789 /* The ridiculous code below is to handle the case of the largest
5790 negative integer. */
5791 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
5792 bool adjust = false;
5793 tree temp;
5795 if (TREE_CONSTANT_OVERFLOW (negative_size))
5797 negative_size
5798 = size_binop (MINUS_EXPR, bitsize_zero_node,
5799 size_binop (PLUS_EXPR, gnu_size,
5800 bitsize_one_node));
5801 adjust = true;
5804 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
5805 if (adjust)
5806 temp = build2 (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
5808 return annotate_value (temp);
5811 if (!host_integerp (gnu_size, 1))
5812 return No_Uint;
5814 size = tree_low_cst (gnu_size, 1);
5816 /* This peculiar test is to make sure that the size fits in an int
5817 on machines where HOST_WIDE_INT is not "int". */
5818 if (tree_low_cst (gnu_size, 1) == size)
5819 return UI_From_Int (size);
5820 else
5821 return No_Uint;
5823 case COMPONENT_REF:
5824 /* The only case we handle here is a simple discriminant reference. */
5825 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
5826 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
5827 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
5828 return Create_Node (Discrim_Val,
5829 annotate_value (DECL_DISCRIMINANT_NUMBER
5830 (TREE_OPERAND (gnu_size, 1))),
5831 No_Uint, No_Uint);
5832 else
5833 return No_Uint;
5835 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
5836 return annotate_value (TREE_OPERAND (gnu_size, 0));
5838 /* Now just list the operations we handle. */
5839 case COND_EXPR: tcode = Cond_Expr; break;
5840 case PLUS_EXPR: tcode = Plus_Expr; break;
5841 case MINUS_EXPR: tcode = Minus_Expr; break;
5842 case MULT_EXPR: tcode = Mult_Expr; break;
5843 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
5844 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
5845 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
5846 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
5847 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
5848 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
5849 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
5850 case NEGATE_EXPR: tcode = Negate_Expr; break;
5851 case MIN_EXPR: tcode = Min_Expr; break;
5852 case MAX_EXPR: tcode = Max_Expr; break;
5853 case ABS_EXPR: tcode = Abs_Expr; break;
5854 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
5855 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
5856 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
5857 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
5858 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
5859 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
5860 case BIT_AND_EXPR: tcode = Bit_And_Expr; break;
5861 case LT_EXPR: tcode = Lt_Expr; break;
5862 case LE_EXPR: tcode = Le_Expr; break;
5863 case GT_EXPR: tcode = Gt_Expr; break;
5864 case GE_EXPR: tcode = Ge_Expr; break;
5865 case EQ_EXPR: tcode = Eq_Expr; break;
5866 case NE_EXPR: tcode = Ne_Expr; break;
5868 default:
5869 return No_Uint;
5872 /* Now get each of the operands that's relevant for this code. If any
5873 cannot be expressed as a repinfo node, say we can't. */
5874 for (i = 0; i < 3; i++)
5875 ops[i] = No_Uint;
5877 for (i = 0; i < len; i++)
5879 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
5880 if (ops[i] == No_Uint)
5881 return No_Uint;
5884 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
5885 TREE_COMPLEXITY (gnu_size) = ret;
5886 return ret;
5889 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
5890 GCC type, set Component_Bit_Offset and Esize to the position and size
5891 used by Gigi. */
5893 static void
5894 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
5896 tree gnu_list;
5897 tree gnu_entry;
5898 Entity_Id gnat_field;
5900 /* We operate by first making a list of all fields and their positions
5901 (we can get the sizes easily at any time) by a recursive call
5902 and then update all the sizes into the tree. */
5903 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
5904 size_zero_node, bitsize_zero_node,
5905 BIGGEST_ALIGNMENT);
5907 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
5908 gnat_field = Next_Entity (gnat_field))
5909 if ((Ekind (gnat_field) == E_Component
5910 || (Ekind (gnat_field) == E_Discriminant
5911 && !Is_Unchecked_Union (Scope (gnat_field)))))
5913 tree parent_offset = bitsize_zero_node;
5915 gnu_entry = purpose_member (gnat_to_gnu_field_decl (gnat_field),
5916 gnu_list);
5918 if (gnu_entry)
5920 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
5922 /* In this mode the tag and parent components have not been
5923 generated, so we add the appropriate offset to each
5924 component. For a component appearing in the current
5925 extension, the offset is the size of the parent. */
5926 if (Is_Derived_Type (gnat_entity)
5927 && Original_Record_Component (gnat_field) == gnat_field)
5928 parent_offset
5929 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
5930 bitsizetype);
5931 else
5932 parent_offset = bitsize_int (POINTER_SIZE);
5935 Set_Component_Bit_Offset
5936 (gnat_field,
5937 annotate_value
5938 (size_binop (PLUS_EXPR,
5939 bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
5940 TREE_VALUE (TREE_VALUE
5941 (TREE_VALUE (gnu_entry)))),
5942 parent_offset)));
5944 Set_Esize (gnat_field,
5945 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
5947 else if (Is_Tagged_Type (gnat_entity)
5948 && Is_Derived_Type (gnat_entity))
5950 /* If there is no gnu_entry, this is an inherited component whose
5951 position is the same as in the parent type. */
5952 Set_Component_Bit_Offset
5953 (gnat_field,
5954 Component_Bit_Offset (Original_Record_Component (gnat_field)));
5955 Set_Esize (gnat_field,
5956 Esize (Original_Record_Component (gnat_field)));
5961 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
5962 FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
5963 position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
5964 placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position. GNU_POS is
5965 to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
5966 the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
5967 so far. */
5969 static tree
5970 compute_field_positions (tree gnu_type, tree gnu_list, tree gnu_pos,
5971 tree gnu_bitpos, unsigned int offset_align)
5973 tree gnu_field;
5974 tree gnu_result = gnu_list;
5976 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
5977 gnu_field = TREE_CHAIN (gnu_field))
5979 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
5980 DECL_FIELD_BIT_OFFSET (gnu_field));
5981 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
5982 DECL_FIELD_OFFSET (gnu_field));
5983 unsigned int our_offset_align
5984 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
5986 gnu_result
5987 = tree_cons (gnu_field,
5988 tree_cons (gnu_our_offset,
5989 tree_cons (size_int (our_offset_align),
5990 gnu_our_bitpos, NULL_TREE),
5991 NULL_TREE),
5992 gnu_result);
5994 if (DECL_INTERNAL_P (gnu_field))
5995 gnu_result
5996 = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
5997 gnu_our_offset, gnu_our_bitpos,
5998 our_offset_align);
6001 return gnu_result;
6004 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
6005 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
6006 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
6007 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
6008 for the size of a field. COMPONENT_P is true if we are being called
6009 to process the Component_Size of GNAT_OBJECT. This is used for error
6010 message handling and to indicate to use the object size of GNU_TYPE.
6011 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
6012 it means that a size of zero should be treated as an unspecified size. */
6014 static tree
6015 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
6016 enum tree_code kind, bool component_p, bool zero_ok)
6018 Node_Id gnat_error_node;
6019 tree type_size
6020 = kind == VAR_DECL ? TYPE_SIZE (gnu_type) : rm_size (gnu_type);
6021 tree size;
6023 /* Find the node to use for errors. */
6024 if ((Ekind (gnat_object) == E_Component
6025 || Ekind (gnat_object) == E_Discriminant)
6026 && Present (Component_Clause (gnat_object)))
6027 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
6028 else if (Present (Size_Clause (gnat_object)))
6029 gnat_error_node = Expression (Size_Clause (gnat_object));
6030 else
6031 gnat_error_node = gnat_object;
6033 /* Return 0 if no size was specified, either because Esize was not Present or
6034 the specified size was zero. */
6035 if (No (uint_size) || uint_size == No_Uint)
6036 return NULL_TREE;
6038 /* Get the size as a tree. Give an error if a size was specified, but cannot
6039 be represented as in sizetype. */
6040 size = UI_To_gnu (uint_size, bitsizetype);
6041 if (TREE_OVERFLOW (size))
6043 post_error_ne (component_p ? "component size of & is too large"
6044 : "size of & is too large",
6045 gnat_error_node, gnat_object);
6046 return NULL_TREE;
6049 /* Ignore a negative size since that corresponds to our back-annotation.
6050 Also ignore a zero size unless a size clause exists. */
6051 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
6052 return NULL_TREE;
6054 /* The size of objects is always a multiple of a byte. */
6055 if (kind == VAR_DECL
6056 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
6058 if (component_p)
6059 post_error_ne ("component size for& is not a multiple of Storage_Unit",
6060 gnat_error_node, gnat_object);
6061 else
6062 post_error_ne ("size for& is not a multiple of Storage_Unit",
6063 gnat_error_node, gnat_object);
6064 return NULL_TREE;
6067 /* If this is an integral type or a packed array type, the front-end has
6068 verified the size, so we need not do it here (which would entail
6069 checking against the bounds). However, if this is an aliased object, it
6070 may not be smaller than the type of the object. */
6071 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
6072 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
6073 return size;
6075 /* If the object is a record that contains a template, add the size of
6076 the template to the specified size. */
6077 if (TREE_CODE (gnu_type) == RECORD_TYPE
6078 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6079 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
6081 /* Modify the size of the type to be that of the maximum size if it has a
6082 discriminant or the size of a thin pointer if this is a fat pointer. */
6083 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
6084 type_size = max_size (type_size, true);
6085 else if (TYPE_FAT_POINTER_P (gnu_type))
6086 type_size = bitsize_int (POINTER_SIZE);
6088 /* If this is an access type, the minimum size is that given by the smallest
6089 integral mode that's valid for pointers. */
6090 if (TREE_CODE (gnu_type) == POINTER_TYPE)
6092 enum machine_mode p_mode;
6094 for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
6095 !targetm.valid_pointer_mode (p_mode);
6096 p_mode = GET_MODE_WIDER_MODE (p_mode))
6099 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
6102 /* If the size of the object is a constant, the new size must not be
6103 smaller. */
6104 if (TREE_CODE (type_size) != INTEGER_CST
6105 || TREE_OVERFLOW (type_size)
6106 || tree_int_cst_lt (size, type_size))
6108 if (component_p)
6109 post_error_ne_tree
6110 ("component size for& too small{, minimum allowed is ^}",
6111 gnat_error_node, gnat_object, type_size);
6112 else
6113 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
6114 gnat_error_node, gnat_object, type_size);
6116 if (kind == VAR_DECL && !component_p
6117 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
6118 && !tree_int_cst_lt (size, rm_size (gnu_type)))
6119 post_error_ne_tree_2
6120 ("\\size of ^ is not a multiple of alignment (^ bits)",
6121 gnat_error_node, gnat_object, rm_size (gnu_type),
6122 TYPE_ALIGN (gnu_type));
6124 else if (INTEGRAL_TYPE_P (gnu_type))
6125 post_error_ne ("\\size would be legal if & were not aliased!",
6126 gnat_error_node, gnat_object);
6128 return NULL_TREE;
6131 return size;
6134 /* Similarly, but both validate and process a value of RM_Size. This
6135 routine is only called for types. */
6137 static void
6138 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
6140 /* Only give an error if a Value_Size clause was explicitly given.
6141 Otherwise, we'd be duplicating an error on the Size clause. */
6142 Node_Id gnat_attr_node
6143 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
6144 tree old_size = rm_size (gnu_type);
6145 tree size;
6147 /* Get the size as a tree. Do nothing if none was specified, either
6148 because RM_Size was not Present or if the specified size was zero.
6149 Give an error if a size was specified, but cannot be represented as
6150 in sizetype. */
6151 if (No (uint_size) || uint_size == No_Uint)
6152 return;
6154 size = UI_To_gnu (uint_size, bitsizetype);
6155 if (TREE_OVERFLOW (size))
6157 if (Present (gnat_attr_node))
6158 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
6159 gnat_entity);
6161 return;
6164 /* Ignore a negative size since that corresponds to our back-annotation.
6165 Also ignore a zero size unless a size clause exists, a Value_Size
6166 clause exists, or this is an integer type, in which case the
6167 front end will have always set it. */
6168 else if (tree_int_cst_sgn (size) < 0
6169 || (integer_zerop (size) && No (gnat_attr_node)
6170 && !Has_Size_Clause (gnat_entity)
6171 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
6172 return;
6174 /* If the old size is self-referential, get the maximum size. */
6175 if (CONTAINS_PLACEHOLDER_P (old_size))
6176 old_size = max_size (old_size, true);
6178 /* If the size of the object is a constant, the new size must not be
6179 smaller (the front end checks this for scalar types). */
6180 if (TREE_CODE (old_size) != INTEGER_CST
6181 || TREE_OVERFLOW (old_size)
6182 || (AGGREGATE_TYPE_P (gnu_type)
6183 && tree_int_cst_lt (size, old_size)))
6185 if (Present (gnat_attr_node))
6186 post_error_ne_tree
6187 ("Value_Size for& too small{, minimum allowed is ^}",
6188 gnat_attr_node, gnat_entity, old_size);
6190 return;
6193 /* Otherwise, set the RM_Size. */
6194 if (TREE_CODE (gnu_type) == INTEGER_TYPE
6195 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
6196 TYPE_RM_SIZE_NUM (gnu_type) = size;
6197 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
6198 TYPE_RM_SIZE_NUM (gnu_type) = size;
6199 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6200 || TREE_CODE (gnu_type) == UNION_TYPE
6201 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6202 && !TYPE_IS_FAT_POINTER_P (gnu_type))
6203 SET_TYPE_ADA_SIZE (gnu_type, size);
6206 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
6207 If TYPE is the best type, return it. Otherwise, make a new type. We
6208 only support new integral and pointer types. BIASED_P is nonzero if
6209 we are making a biased type. */
6211 static tree
6212 make_type_from_size (tree type, tree size_tree, bool biased_p)
6214 tree new_type;
6215 unsigned HOST_WIDE_INT size;
6216 bool unsigned_p;
6218 /* If size indicates an error, just return TYPE to avoid propagating the
6219 error. Likewise if it's too large to represent. */
6220 if (!size_tree || !host_integerp (size_tree, 1))
6221 return type;
6223 size = tree_low_cst (size_tree, 1);
6224 switch (TREE_CODE (type))
6226 case INTEGER_TYPE:
6227 case ENUMERAL_TYPE:
6228 /* Only do something if the type is not already the proper size and is
6229 not a packed array type. */
6230 if (TYPE_PACKED_ARRAY_TYPE_P (type)
6231 || (TYPE_PRECISION (type) == size
6232 && biased_p == (TREE_CODE (type) == INTEGER_CST
6233 && TYPE_BIASED_REPRESENTATION_P (type))))
6234 break;
6236 biased_p |= (TREE_CODE (type) == INTEGER_TYPE
6237 && TYPE_BIASED_REPRESENTATION_P (type));
6238 unsigned_p = TYPE_UNSIGNED (type) || biased_p;
6240 size = MIN (size, LONG_LONG_TYPE_SIZE);
6241 new_type
6242 = unsigned_p ? make_unsigned_type (size) : make_signed_type (size);
6243 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
6244 TYPE_MIN_VALUE (new_type)
6245 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
6246 TYPE_MAX_VALUE (new_type)
6247 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
6248 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
6249 TYPE_RM_SIZE_NUM (new_type) = bitsize_int (size);
6250 return new_type;
6252 case RECORD_TYPE:
6253 /* Do something if this is a fat pointer, in which case we
6254 may need to return the thin pointer. */
6255 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
6256 return
6257 build_pointer_type
6258 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
6259 break;
6261 case POINTER_TYPE:
6262 /* Only do something if this is a thin pointer, in which case we
6263 may need to return the fat pointer. */
6264 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
6265 return
6266 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
6268 break;
6270 default:
6271 break;
6274 return type;
6277 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
6278 a type or object whose present alignment is ALIGN. If this alignment is
6279 valid, return it. Otherwise, give an error and return ALIGN. */
6281 static unsigned int
6282 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
6284 Node_Id gnat_error_node = gnat_entity;
6285 unsigned int new_align;
6287 #ifndef MAX_OFILE_ALIGNMENT
6288 #define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
6289 #endif
6291 if (Present (Alignment_Clause (gnat_entity)))
6292 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
6294 /* Don't worry about checking alignment if alignment was not specified
6295 by the source program and we already posted an error for this entity. */
6297 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
6298 return align;
6300 /* Within GCC, an alignment is an integer, so we must make sure a
6301 value is specified that fits in that range. Also, alignments of
6302 more than MAX_OFILE_ALIGNMENT can't be supported. */
6304 if (! UI_Is_In_Int_Range (alignment)
6305 || ((new_align = UI_To_Int (alignment))
6306 > MAX_OFILE_ALIGNMENT / BITS_PER_UNIT))
6307 post_error_ne_num ("largest supported alignment for& is ^",
6308 gnat_error_node, gnat_entity,
6309 MAX_OFILE_ALIGNMENT / BITS_PER_UNIT);
6310 else if (!(Present (Alignment_Clause (gnat_entity))
6311 && From_At_Mod (Alignment_Clause (gnat_entity)))
6312 && new_align * BITS_PER_UNIT < align)
6313 post_error_ne_num ("alignment for& must be at least ^",
6314 gnat_error_node, gnat_entity,
6315 align / BITS_PER_UNIT);
6316 else
6317 align = MAX (align, new_align == 0 ? 1 : new_align * BITS_PER_UNIT);
6319 return align;
6322 /* Verify that OBJECT, a type or decl, is something we can implement
6323 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
6324 if we require atomic components. */
6326 static void
6327 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
6329 Node_Id gnat_error_point = gnat_entity;
6330 Node_Id gnat_node;
6331 enum machine_mode mode;
6332 unsigned int align;
6333 tree size;
6335 /* There are three case of what OBJECT can be. It can be a type, in which
6336 case we take the size, alignment and mode from the type. It can be a
6337 declaration that was indirect, in which case the relevant values are
6338 that of the type being pointed to, or it can be a normal declaration,
6339 in which case the values are of the decl. The code below assumes that
6340 OBJECT is either a type or a decl. */
6341 if (TYPE_P (object))
6343 mode = TYPE_MODE (object);
6344 align = TYPE_ALIGN (object);
6345 size = TYPE_SIZE (object);
6347 else if (DECL_BY_REF_P (object))
6349 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
6350 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
6351 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
6353 else
6355 mode = DECL_MODE (object);
6356 align = DECL_ALIGN (object);
6357 size = DECL_SIZE (object);
6360 /* Consider all floating-point types atomic and any types that that are
6361 represented by integers no wider than a machine word. */
6362 if (GET_MODE_CLASS (mode) == MODE_FLOAT
6363 || ((GET_MODE_CLASS (mode) == MODE_INT
6364 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
6365 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
6366 return;
6368 /* For the moment, also allow anything that has an alignment equal
6369 to its size and which is smaller than a word. */
6370 if (size && TREE_CODE (size) == INTEGER_CST
6371 && compare_tree_int (size, align) == 0
6372 && align <= BITS_PER_WORD)
6373 return;
6375 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
6376 gnat_node = Next_Rep_Item (gnat_node))
6378 if (!comp_p && Nkind (gnat_node) == N_Pragma
6379 && Get_Pragma_Id (Chars (gnat_node)) == Pragma_Atomic)
6380 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6381 else if (comp_p && Nkind (gnat_node) == N_Pragma
6382 && (Get_Pragma_Id (Chars (gnat_node))
6383 == Pragma_Atomic_Components))
6384 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6387 if (comp_p)
6388 post_error_ne ("atomic access to component of & cannot be guaranteed",
6389 gnat_error_point, gnat_entity);
6390 else
6391 post_error_ne ("atomic access to & cannot be guaranteed",
6392 gnat_error_point, gnat_entity);
6395 /* Check if FTYPE1 and FTYPE2, two potentially different function type nodes,
6396 have compatible signatures so that a call using one type may be safely
6397 issued if the actual target function type is the other. Return 1 if it is
6398 the case, 0 otherwise, and post errors on the incompatibilities.
6400 This is used when an Ada subprogram is mapped onto a GCC builtin, to ensure
6401 that calls to the subprogram will have arguments suitable for the later
6402 underlying builtin expansion. */
6404 static int
6405 compatible_signatures_p (tree ftype1, tree ftype2)
6407 /* As of now, we only perform very trivial tests and consider it's the
6408 programmer's responsibility to ensure the type correctness in the Ada
6409 declaration, as in the regular Import cases.
6411 Mismatches typically result in either error messages from the builtin
6412 expander, internal compiler errors, or in a real call sequence. This
6413 should be refined to issue diagnostics helping error detection and
6414 correction. */
6416 /* Almost fake test, ensuring a use of each argument. */
6417 if (ftype1 == ftype2)
6418 return 1;
6420 return 1;
6423 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a new type
6424 with all size expressions that contain F updated by replacing F with R.
6425 This is identical to GCC's substitute_in_type except that it knows about
6426 TYPE_INDEX_TYPE. If F is NULL_TREE, always make a new RECORD_TYPE, even if
6427 nothing has changed. */
6429 tree
6430 gnat_substitute_in_type (tree t, tree f, tree r)
6432 tree new = t;
6433 tree tem;
6435 switch (TREE_CODE (t))
6437 case INTEGER_TYPE:
6438 case ENUMERAL_TYPE:
6439 case BOOLEAN_TYPE:
6440 case CHAR_TYPE:
6441 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6442 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
6444 tree low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
6445 tree high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
6447 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6448 return t;
6450 new = build_range_type (TREE_TYPE (t), low, high);
6451 if (TYPE_INDEX_TYPE (t))
6452 SET_TYPE_INDEX_TYPE
6453 (new, gnat_substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
6454 return new;
6457 return t;
6459 case REAL_TYPE:
6460 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6461 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
6463 tree low = NULL_TREE, high = NULL_TREE;
6465 if (TYPE_MIN_VALUE (t))
6466 low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
6467 if (TYPE_MAX_VALUE (t))
6468 high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
6470 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6471 return t;
6473 t = copy_type (t);
6474 TYPE_MIN_VALUE (t) = low;
6475 TYPE_MAX_VALUE (t) = high;
6477 return t;
6479 case COMPLEX_TYPE:
6480 tem = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6481 if (tem == TREE_TYPE (t))
6482 return t;
6484 return build_complex_type (tem);
6486 case OFFSET_TYPE:
6487 case METHOD_TYPE:
6488 case FUNCTION_TYPE:
6489 case LANG_TYPE:
6490 /* Don't know how to do these yet. */
6491 gcc_unreachable ();
6493 case ARRAY_TYPE:
6495 tree component = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6496 tree domain = gnat_substitute_in_type (TYPE_DOMAIN (t), f, r);
6498 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6499 return t;
6501 new = build_array_type (component, domain);
6502 TYPE_SIZE (new) = 0;
6503 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
6504 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
6505 layout_type (new);
6506 TYPE_ALIGN (new) = TYPE_ALIGN (t);
6508 /* If we had bounded the sizes of T by a constant, bound the sizes of
6509 NEW by the same constant. */
6510 if (TREE_CODE (TYPE_SIZE (t)) == MIN_EXPR)
6511 TYPE_SIZE (new)
6512 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE (t), 1),
6513 TYPE_SIZE (new));
6514 if (TREE_CODE (TYPE_SIZE_UNIT (t)) == MIN_EXPR)
6515 TYPE_SIZE_UNIT (new)
6516 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE_UNIT (t), 1),
6517 TYPE_SIZE_UNIT (new));
6518 return new;
6521 case RECORD_TYPE:
6522 case UNION_TYPE:
6523 case QUAL_UNION_TYPE:
6525 tree field;
6526 bool changed_field
6527 = (f == NULL_TREE && !TREE_CONSTANT (TYPE_SIZE (t)));
6528 bool field_has_rep = false;
6529 tree last_field = NULL_TREE;
6531 tree new = copy_type (t);
6533 /* Start out with no fields, make new fields, and chain them
6534 in. If we haven't actually changed the type of any field,
6535 discard everything we've done and return the old type. */
6537 TYPE_FIELDS (new) = NULL_TREE;
6538 TYPE_SIZE (new) = NULL_TREE;
6540 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6542 tree new_field = copy_node (field);
6544 TREE_TYPE (new_field)
6545 = gnat_substitute_in_type (TREE_TYPE (new_field), f, r);
6547 if (DECL_HAS_REP_P (field) && !DECL_INTERNAL_P (field))
6548 field_has_rep = true;
6549 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
6550 changed_field = true;
6552 /* If this is an internal field and the type of this field is
6553 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
6554 the type just has one element, treat that as the field.
6555 But don't do this if we are processing a QUAL_UNION_TYPE. */
6556 if (TREE_CODE (t) != QUAL_UNION_TYPE
6557 && DECL_INTERNAL_P (new_field)
6558 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
6559 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
6561 if (!TYPE_FIELDS (TREE_TYPE (new_field)))
6562 continue;
6564 if (!TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))))
6566 tree next_new_field
6567 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
6569 /* Make sure omitting the union doesn't change
6570 the layout. */
6571 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
6572 new_field = next_new_field;
6576 DECL_CONTEXT (new_field) = new;
6577 SET_DECL_ORIGINAL_FIELD (new_field,
6578 (DECL_ORIGINAL_FIELD (field)
6579 ? DECL_ORIGINAL_FIELD (field) : field));
6581 /* If the size of the old field was set at a constant,
6582 propagate the size in case the type's size was variable.
6583 (This occurs in the case of a variant or discriminated
6584 record with a default size used as a field of another
6585 record.) */
6586 DECL_SIZE (new_field)
6587 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
6588 ? DECL_SIZE (field) : NULL_TREE;
6589 DECL_SIZE_UNIT (new_field)
6590 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
6591 ? DECL_SIZE_UNIT (field) : NULL_TREE;
6593 if (TREE_CODE (t) == QUAL_UNION_TYPE)
6595 tree new_q = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
6597 if (new_q != DECL_QUALIFIER (new_field))
6598 changed_field = true;
6600 /* Do the substitution inside the qualifier and if we find
6601 that this field will not be present, omit it. */
6602 DECL_QUALIFIER (new_field) = new_q;
6604 if (integer_zerop (DECL_QUALIFIER (new_field)))
6605 continue;
6608 if (!last_field)
6609 TYPE_FIELDS (new) = new_field;
6610 else
6611 TREE_CHAIN (last_field) = new_field;
6613 last_field = new_field;
6615 /* If this is a qualified type and this field will always be
6616 present, we are done. */
6617 if (TREE_CODE (t) == QUAL_UNION_TYPE
6618 && integer_onep (DECL_QUALIFIER (new_field)))
6619 break;
6622 /* If this used to be a qualified union type, but we now know what
6623 field will be present, make this a normal union. */
6624 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
6625 && (!TYPE_FIELDS (new)
6626 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
6627 TREE_SET_CODE (new, UNION_TYPE);
6628 else if (!changed_field)
6629 return t;
6631 gcc_assert (!field_has_rep);
6632 layout_type (new);
6634 /* If the size was originally a constant use it. */
6635 if (TYPE_SIZE (t) && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
6636 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
6638 TYPE_SIZE (new) = TYPE_SIZE (t);
6639 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
6640 SET_TYPE_ADA_SIZE (new, TYPE_ADA_SIZE (t));
6643 return new;
6646 default:
6647 return t;
6651 /* Return the "RM size" of GNU_TYPE. This is the actual number of bits
6652 needed to represent the object. */
6654 tree
6655 rm_size (tree gnu_type)
6657 /* For integer types, this is the precision. For record types, we store
6658 the size explicitly. For other types, this is just the size. */
6660 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
6661 return TYPE_RM_SIZE (gnu_type);
6662 else if (TREE_CODE (gnu_type) == RECORD_TYPE
6663 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6664 /* Return the rm_size of the actual data plus the size of the template. */
6665 return
6666 size_binop (PLUS_EXPR,
6667 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
6668 DECL_SIZE (TYPE_FIELDS (gnu_type)));
6669 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6670 || TREE_CODE (gnu_type) == UNION_TYPE
6671 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6672 && !TYPE_IS_FAT_POINTER_P (gnu_type)
6673 && TYPE_ADA_SIZE (gnu_type))
6674 return TYPE_ADA_SIZE (gnu_type);
6675 else
6676 return TYPE_SIZE (gnu_type);
6679 /* Return an identifier representing the external name to be used for
6680 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
6681 and the specified suffix. */
6683 tree
6684 create_concat_name (Entity_Id gnat_entity, const char *suffix)
6686 Entity_Kind kind = Ekind (gnat_entity);
6688 const char *str = (!suffix ? "" : suffix);
6689 String_Template temp = {1, strlen (str)};
6690 Fat_Pointer fp = {str, &temp};
6692 Get_External_Name_With_Suffix (gnat_entity, fp);
6694 /* A variable using the Stdcall convention (meaning we are running
6695 on a Windows box) live in a DLL. Here we adjust its name to use
6696 the jump-table, the _imp__NAME contains the address for the NAME
6697 variable. */
6698 if ((kind == E_Variable || kind == E_Constant)
6699 && Has_Stdcall_Convention (gnat_entity))
6701 const char *prefix = "_imp__";
6702 int k, plen = strlen (prefix);
6704 for (k = 0; k <= Name_Len; k++)
6705 Name_Buffer [Name_Len - k + plen] = Name_Buffer [Name_Len - k];
6706 strncpy (Name_Buffer, prefix, plen);
6709 return get_identifier (Name_Buffer);
6712 /* Return the name to be used for GNAT_ENTITY. If a type, create a
6713 fully-qualified name, possibly with type information encoding.
6714 Otherwise, return the name. */
6716 tree
6717 get_entity_name (Entity_Id gnat_entity)
6719 Get_Encoded_Name (gnat_entity);
6720 return get_identifier (Name_Buffer);
6723 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
6724 string, return a new IDENTIFIER_NODE that is the concatenation of
6725 the name in GNU_ID and SUFFIX. */
6727 tree
6728 concat_id_with_name (tree gnu_id, const char *suffix)
6730 int len = IDENTIFIER_LENGTH (gnu_id);
6732 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id),
6733 IDENTIFIER_LENGTH (gnu_id));
6734 strncpy (Name_Buffer + len, "___", 3);
6735 len += 3;
6736 strcpy (Name_Buffer + len, suffix);
6737 return get_identifier (Name_Buffer);
6740 #include "gt-ada-decl.h"