* tree-cfg.c (tree_find_edge_insert_loc): Handle naked RETURN_EXPR.
[official-gcc.git] / gcc / ada / decl.c
blob701501e88896d8a8f6e4695eb8d2d5ce653d46f8
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"
39 #include "ada.h"
40 #include "types.h"
41 #include "atree.h"
42 #include "elists.h"
43 #include "namet.h"
44 #include "nlists.h"
45 #include "repinfo.h"
46 #include "snames.h"
47 #include "stringt.h"
48 #include "uintp.h"
49 #include "fe.h"
50 #include "sinfo.h"
51 #include "einfo.h"
52 #include "ada-tree.h"
53 #include "gigi.h"
55 /* Provide default values for the macros controlling stack checking.
56 This is copied from GCC's expr.h. */
58 #ifndef STACK_CHECK_BUILTIN
59 #define STACK_CHECK_BUILTIN 0
60 #endif
61 #ifndef STACK_CHECK_PROBE_INTERVAL
62 #define STACK_CHECK_PROBE_INTERVAL 4096
63 #endif
64 #ifndef STACK_CHECK_MAX_FRAME_SIZE
65 #define STACK_CHECK_MAX_FRAME_SIZE \
66 (STACK_CHECK_PROBE_INTERVAL - UNITS_PER_WORD)
67 #endif
68 #ifndef STACK_CHECK_MAX_VAR_SIZE
69 #define STACK_CHECK_MAX_VAR_SIZE (STACK_CHECK_MAX_FRAME_SIZE / 100)
70 #endif
72 /* These two variables are used to defer recursively expanding incomplete
73 types while we are processing a record or subprogram type. */
75 static int defer_incomplete_level = 0;
76 static struct incomplete
78 struct incomplete *next;
79 tree old_type;
80 Entity_Id full_type;
81 } *defer_incomplete_list = 0;
83 /* These two variables are used to defer emission of debug information for
84 nested incomplete record types */
86 static int defer_debug_level = 0;
87 static tree defer_debug_incomplete_list;
89 static void copy_alias_set (tree, tree);
90 static tree substitution_list (Entity_Id, Entity_Id, tree, bool);
91 static bool allocatable_size_p (tree, bool);
92 static void prepend_attributes (Entity_Id, struct attrib **);
93 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
94 static bool is_variable_size (tree);
95 static tree elaborate_expression_1 (Node_Id, Entity_Id, tree, tree,
96 bool, bool);
97 static tree make_packable_type (tree);
98 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
99 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
100 bool, bool, bool);
101 static int compare_field_bitpos (const PTR, const PTR);
102 static Uint annotate_value (tree);
103 static void annotate_rep (Entity_Id, tree);
104 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
105 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
106 static void set_rm_size (Uint, tree, Entity_Id);
107 static tree make_type_from_size (tree, tree, bool);
108 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
109 static void check_ok_for_atomic (tree, Entity_Id, bool);
110 static int compatible_signatures_p (tree ftype1, tree ftype2);
112 /* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
113 GCC type corresponding to that entity. GNAT_ENTITY is assumed to
114 refer to an Ada type. */
116 tree
117 gnat_to_gnu_type (Entity_Id gnat_entity)
119 tree gnu_decl;
121 /* The back end never attempts to annotate generic types */
122 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
123 return void_type_node;
125 /* Convert the ada entity type into a GCC TYPE_DECL node. */
126 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
127 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
128 return TREE_TYPE (gnu_decl);
131 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
132 entity, this routine returns the equivalent GCC tree for that entity
133 (an ..._DECL node) and associates the ..._DECL node with the input GNAT
134 defining identifier.
136 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
137 initial value (in GCC tree form). This is optional for variables.
138 For renamed entities, GNU_EXPR gives the object being renamed.
140 DEFINITION is nonzero if this call is intended for a definition. This is
141 used for separate compilation where it necessary to know whether an
142 external declaration or a definition should be created if the GCC equivalent
143 was not created previously. The value of 1 is normally used for a non-zero
144 DEFINITION, but a value of 2 is used in special circumstances, defined in
145 the code. */
147 tree
148 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
150 tree gnu_entity_id;
151 tree gnu_type = NULL_TREE;
152 /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
153 GNAT tree. This node will be associated with the GNAT node by calling
154 the save_gnu_tree routine at the end of the `switch' statement. */
155 tree gnu_decl = NULL_TREE;
156 /* true if we have already saved gnu_decl as a gnat association. */
157 bool saved = false;
158 /* Nonzero if we incremented defer_incomplete_level. */
159 bool this_deferred = false;
160 /* Nonzero if we incremented defer_debug_level. */
161 bool debug_deferred = false;
162 /* Nonzero if we incremented force_global. */
163 bool this_global = false;
164 /* Nonzero if we should check to see if elaborated during processing. */
165 bool maybe_present = false;
166 /* Nonzero if we made GNU_DECL and its type here. */
167 bool this_made_decl = false;
168 struct attrib *attr_list = NULL;
169 bool debug_info_p = (Needs_Debug_Info (gnat_entity)
170 || debug_info_level == DINFO_LEVEL_VERBOSE);
171 Entity_Kind kind = Ekind (gnat_entity);
172 Entity_Id gnat_temp;
173 unsigned int esize
174 = ((Known_Esize (gnat_entity)
175 && UI_Is_In_Int_Range (Esize (gnat_entity)))
176 ? MIN (UI_To_Int (Esize (gnat_entity)),
177 IN (kind, Float_Kind)
178 ? fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE)
179 : IN (kind, Access_Kind) ? POINTER_SIZE * 2
180 : LONG_LONG_TYPE_SIZE)
181 : LONG_LONG_TYPE_SIZE);
182 tree gnu_size = 0;
183 bool imported_p
184 = ((Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)))
185 || From_With_Type (gnat_entity));
186 unsigned int align = 0;
188 /* Since a use of an Itype is a definition, process it as such if it
189 is not in a with'ed unit. */
191 if (!definition && Is_Itype (gnat_entity)
192 && !present_gnu_tree (gnat_entity)
193 && In_Extended_Main_Code_Unit (gnat_entity))
195 /* Ensure that we are in a subprogram mentioned in the Scope
196 chain of this entity, our current scope is global,
197 or that we encountered a task or entry (where we can't currently
198 accurately check scoping). */
199 if (!current_function_decl
200 || DECL_ELABORATION_PROC_P (current_function_decl))
202 process_type (gnat_entity);
203 return get_gnu_tree (gnat_entity);
206 for (gnat_temp = Scope (gnat_entity);
207 Present (gnat_temp); gnat_temp = Scope (gnat_temp))
209 if (Is_Type (gnat_temp))
210 gnat_temp = Underlying_Type (gnat_temp);
212 if (Ekind (gnat_temp) == E_Subprogram_Body)
213 gnat_temp
214 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
216 if (IN (Ekind (gnat_temp), Subprogram_Kind)
217 && Present (Protected_Body_Subprogram (gnat_temp)))
218 gnat_temp = Protected_Body_Subprogram (gnat_temp);
220 if (Ekind (gnat_temp) == E_Entry
221 || Ekind (gnat_temp) == E_Entry_Family
222 || Ekind (gnat_temp) == E_Task_Type
223 || (IN (Ekind (gnat_temp), Subprogram_Kind)
224 && present_gnu_tree (gnat_temp)
225 && (current_function_decl
226 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
228 process_type (gnat_entity);
229 return get_gnu_tree (gnat_entity);
233 /* This abort means the entity "gnat_entity" has an incorrect scope,
234 i.e. that its scope does not correspond to the subprogram in which
235 it is declared */
236 gcc_unreachable ();
239 /* If this is entity 0, something went badly wrong. */
240 gcc_assert (Present (gnat_entity));
242 /* If we've already processed this entity, return what we got last time.
243 If we are defining the node, we should not have already processed it.
244 In that case, we will abort below when we try to save a new GCC tree for
245 this object. We also need to handle the case of getting a dummy type
246 when a Full_View exists. */
248 if (present_gnu_tree (gnat_entity)
249 && (! definition
250 || (Is_Type (gnat_entity) && imported_p)))
252 gnu_decl = get_gnu_tree (gnat_entity);
254 if (TREE_CODE (gnu_decl) == TYPE_DECL
255 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
256 && IN (kind, Incomplete_Or_Private_Kind)
257 && Present (Full_View (gnat_entity)))
259 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
260 NULL_TREE, 0);
262 save_gnu_tree (gnat_entity, NULL_TREE, false);
263 save_gnu_tree (gnat_entity, gnu_decl, false);
266 return gnu_decl;
269 /* If this is a numeric or enumeral type, or an access type, a nonzero
270 Esize must be specified unless it was specified by the programmer. */
271 gcc_assert (!Unknown_Esize (gnat_entity)
272 || Has_Size_Clause (gnat_entity)
273 || (!IN (kind, Numeric_Kind) && !IN (kind, Enumeration_Kind)
274 && (!IN (kind, Access_Kind)
275 || kind == E_Access_Protected_Subprogram_Type
276 || kind == E_Access_Subtype)));
278 /* Likewise, RM_Size must be specified for all discrete and fixed-point
279 types. */
280 gcc_assert (!IN (kind, Discrete_Or_Fixed_Point_Kind)
281 || !Unknown_RM_Size (gnat_entity));
283 /* Get the name of the entity and set up the line number and filename of
284 the original definition for use in any decl we make. */
285 gnu_entity_id = get_entity_name (gnat_entity);
286 Sloc_to_locus (Sloc (gnat_entity), &input_location);
288 /* If we get here, it means we have not yet done anything with this
289 entity. If we are not defining it here, it must be external,
290 otherwise we should have defined it already. */
291 gcc_assert (definition || Is_Public (gnat_entity) || type_annotate_only
292 || kind == E_Discriminant || kind == E_Component
293 || kind == E_Label
294 || (kind == E_Constant && Present (Full_View (gnat_entity)))
295 || IN (kind, Type_Kind));
297 /* For cases when we are not defining (i.e., we are referencing from
298 another compilation unit) Public entities, show we are at global level
299 for the purpose of computing scopes. Don't do this for components or
300 discriminants since the relevant test is whether or not the record is
301 being defined. But do this for Imported functions or procedures in
302 all cases. */
303 if ((!definition && Is_Public (gnat_entity)
304 && !Is_Statically_Allocated (gnat_entity)
305 && kind != E_Discriminant && kind != E_Component)
306 || (Is_Imported (gnat_entity)
307 && (kind == E_Function || kind == E_Procedure)))
308 force_global++, this_global = true;
310 /* Handle any attributes directly attached to the entity. */
311 if (Has_Gigi_Rep_Item (gnat_entity))
312 prepend_attributes (gnat_entity, &attr_list);
314 /* Machine_Attributes on types are expected to be propagated to subtypes.
315 The corresponding Gigi_Rep_Items are only attached to the first subtype
316 though, so we handle the propagation here. */
317 if (Is_Type (gnat_entity) && Base_Type (gnat_entity) != gnat_entity
318 && !Is_First_Subtype (gnat_entity)
319 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
320 prepend_attributes (First_Subtype (Base_Type (gnat_entity)), &attr_list);
322 switch (kind)
324 case E_Constant:
325 /* If this is a use of a deferred constant, get its full
326 declaration. */
327 if (!definition && Present (Full_View (gnat_entity)))
329 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
330 gnu_expr, definition);
331 saved = true;
332 break;
335 /* If we have an external constant that we are not defining,
336 get the expression that is was defined to represent. We
337 may throw that expression away later if it is not a
338 constant.
339 Do not retrieve the expression if it is an aggregate, because
340 in complex instantiation contexts it may not be expanded */
342 if (!definition
343 && Present (Expression (Declaration_Node (gnat_entity)))
344 && !No_Initialization (Declaration_Node (gnat_entity))
345 && (Nkind (Expression (Declaration_Node (gnat_entity)))
346 != N_Aggregate))
347 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
349 /* Ignore deferred constant definitions; they are processed fully in the
350 front-end. For deferred constant references, get the full
351 definition. On the other hand, constants that are renamings are
352 handled like variable renamings. If No_Initialization is set, this is
353 not a deferred constant but a constant whose value is built
354 manually. */
356 if (definition && !gnu_expr
357 && !No_Initialization (Declaration_Node (gnat_entity))
358 && No (Renamed_Object (gnat_entity)))
360 gnu_decl = error_mark_node;
361 saved = true;
362 break;
364 else if (!definition && IN (kind, Incomplete_Or_Private_Kind)
365 && Present (Full_View (gnat_entity)))
367 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
368 NULL_TREE, 0);
369 saved = true;
370 break;
373 goto object;
375 case E_Exception:
376 /* We used to special case VMS exceptions here to directly map them to
377 their associated condition code. Since this code had to be masked
378 dynamically to strip off the severity bits, this caused trouble in
379 the GCC/ZCX case because the "type" pointers we store in the tables
380 have to be static. We now don't special case here anymore, and let
381 the regular processing take place, which leaves us with a regular
382 exception data object for VMS exceptions too. The condition code
383 mapping is taken care of by the front end and the bitmasking by the
384 runtime library. */
385 goto object;
387 case E_Discriminant:
388 case E_Component:
390 /* The GNAT record where the component was defined. */
391 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
393 /* If the variable is an inherited record component (in the case of
394 extended record types), just return the inherited entity, which
395 must be a FIELD_DECL. Likewise for discriminants.
396 For discriminants of untagged records which have explicit
397 stored discriminants, return the entity for the corresponding
398 stored discriminant. Also use Original_Record_Component
399 if the record has a private extension. */
401 if (Present (Original_Record_Component (gnat_entity))
402 && Original_Record_Component (gnat_entity) != gnat_entity)
404 gnu_decl
405 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
406 gnu_expr, definition);
407 saved = true;
408 break;
411 /* If the enclosing record has explicit stored discriminants,
412 then it is an untagged record. If the Corresponding_Discriminant
413 is not empty then this must be a renamed discriminant and its
414 Original_Record_Component must point to the corresponding explicit
415 stored discriminant (i.e., we should have taken the previous
416 branch). */
418 else if (Present (Corresponding_Discriminant (gnat_entity))
419 && Is_Tagged_Type (gnat_record))
421 /* A tagged record has no explicit stored discriminants. */
423 gcc_assert (First_Discriminant (gnat_record)
424 == First_Stored_Discriminant (gnat_record));
425 gnu_decl
426 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
427 gnu_expr, definition);
428 saved = true;
429 break;
432 /* If the enclosing record has explicit stored discriminants,
433 then it is an untagged record. If the Corresponding_Discriminant
434 is not empty then this must be a renamed discriminant and its
435 Original_Record_Component must point to the corresponding explicit
436 stored discriminant (i.e., we should have taken the first
437 branch). */
439 else if (Present (Corresponding_Discriminant (gnat_entity))
440 && (First_Discriminant (gnat_record)
441 != First_Stored_Discriminant (gnat_record)))
442 gcc_unreachable ();
444 /* Otherwise, if we are not defining this and we have no GCC type
445 for the containing record, make one for it. Then we should
446 have made our own equivalent. */
447 else if (!definition && !present_gnu_tree (gnat_record))
449 /* ??? If this is in a record whose scope is a protected
450 type and we have an Original_Record_Component, use it.
451 This is a workaround for major problems in protected type
452 handling. */
454 Entity_Id Scop = Scope (Scope (gnat_entity));
455 if ((Is_Protected_Type (Scop)
456 || (Is_Private_Type (Scop)
457 && Present (Full_View (Scop))
458 && Is_Protected_Type (Full_View (Scop))))
459 && Present (Original_Record_Component (gnat_entity)))
461 gnu_decl
462 = gnat_to_gnu_entity (Original_Record_Component
463 (gnat_entity),
464 gnu_expr, definition);
465 saved = true;
466 break;
469 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
470 gnu_decl = get_gnu_tree (gnat_entity);
471 saved = true;
472 break;
475 else
476 /* Here we have no GCC type and this is a reference rather than a
477 definition. This should never happen. Most likely the cause is a
478 reference before declaration in the gnat tree for gnat_entity. */
479 gcc_unreachable ();
482 case E_Loop_Parameter:
483 case E_Out_Parameter:
484 case E_Variable:
486 /* Simple variables, loop variables, OUT parameters, and exceptions. */
487 object:
489 bool used_by_ref = false;
490 bool const_flag
491 = ((kind == E_Constant || kind == E_Variable)
492 && !Is_Statically_Allocated (gnat_entity)
493 && Is_True_Constant (gnat_entity)
494 && (((Nkind (Declaration_Node (gnat_entity))
495 == N_Object_Declaration)
496 && Present (Expression (Declaration_Node (gnat_entity))))
497 || Present (Renamed_Object (gnat_entity))));
498 bool inner_const_flag = const_flag;
499 bool static_p = Is_Statically_Allocated (gnat_entity);
500 bool mutable_p = false;
501 tree gnu_ext_name = NULL_TREE;
502 tree renamed_obj = NULL_TREE;
504 if (Present (Renamed_Object (gnat_entity)) && !definition)
506 if (kind == E_Exception)
507 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
508 NULL_TREE, 0);
509 else
510 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
513 /* Get the type after elaborating the renamed object. */
514 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
516 /* If this is a loop variable, its type should be the base type.
517 This is because the code for processing a loop determines whether
518 a normal loop end test can be done by comparing the bounds of the
519 loop against those of the base type, which is presumed to be the
520 size used for computation. But this is not correct when the size
521 of the subtype is smaller than the type. */
522 if (kind == E_Loop_Parameter)
523 gnu_type = get_base_type (gnu_type);
525 /* Reject non-renamed objects whose types are unconstrained arrays or
526 any object whose type is a dummy type or VOID_TYPE. */
528 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
529 && No (Renamed_Object (gnat_entity)))
530 || TYPE_IS_DUMMY_P (gnu_type)
531 || TREE_CODE (gnu_type) == VOID_TYPE)
533 gcc_assert (type_annotate_only);
534 return error_mark_node;
537 /* If an alignment is specified, use it if valid. Note that
538 exceptions are objects but don't have alignments. We must do this
539 before we validate the size, since the alignment can affect the
540 size. */
541 if (kind != E_Exception && Known_Alignment (gnat_entity))
543 gcc_assert (Present (Alignment (gnat_entity)));
544 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
545 TYPE_ALIGN (gnu_type));
546 gnu_type = maybe_pad_type (gnu_type, NULL_TREE, align,
547 gnat_entity, "PAD", 0, definition, 1);
550 /* If we are defining the object, see if it has a Size value and
551 validate it if so. If we are not defining the object and a Size
552 clause applies, simply retrieve the value. We don't want to ignore
553 the clause and it is expected to have been validated already. Then
554 get the new type, if any. */
555 if (definition)
556 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
557 gnat_entity, VAR_DECL, false,
558 Has_Size_Clause (gnat_entity));
559 else if (Has_Size_Clause (gnat_entity))
560 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
562 if (gnu_size)
564 gnu_type
565 = make_type_from_size (gnu_type, gnu_size,
566 Has_Biased_Representation (gnat_entity));
568 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
569 gnu_size = NULL_TREE;
572 /* If this object has self-referential size, it must be a record with
573 a default value. We are supposed to allocate an object of the
574 maximum size in this case unless it is a constant with an
575 initializing expression, in which case we can get the size from
576 that. Note that the resulting size may still be a variable, so
577 this may end up with an indirect allocation. */
579 if (No (Renamed_Object (gnat_entity))
580 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
582 if (gnu_expr && kind == E_Constant)
583 gnu_size
584 = SUBSTITUTE_PLACEHOLDER_IN_EXPR
585 (TYPE_SIZE (TREE_TYPE (gnu_expr)), gnu_expr);
587 /* We may have no GNU_EXPR because No_Initialization is
588 set even though there's an Expression. */
589 else if (kind == E_Constant
590 && (Nkind (Declaration_Node (gnat_entity))
591 == N_Object_Declaration)
592 && Present (Expression (Declaration_Node (gnat_entity))))
593 gnu_size
594 = TYPE_SIZE (gnat_to_gnu_type
595 (Etype
596 (Expression (Declaration_Node (gnat_entity)))));
597 else
599 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
600 mutable_p = true;
604 /* If the size is zero bytes, make it one byte since some linkers have
605 trouble with zero-sized objects. If the object will have a
606 template, that will make it nonzero so don't bother. Also avoid
607 doing that for an object renaming or an object with an address
608 clause, as we would lose useful information on the view size
609 (e.g. for null array slices) and we are not allocating the object
610 here anyway. */
611 if (((gnu_size && integer_zerop (gnu_size))
612 || (TYPE_SIZE (gnu_type) && integer_zerop (TYPE_SIZE (gnu_type))))
613 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
614 || !Is_Array_Type (Etype (gnat_entity)))
615 && !Present (Renamed_Object (gnat_entity))
616 && !Present (Address_Clause (gnat_entity)))
617 gnu_size = bitsize_unit_node;
619 /* If this is an atomic object with no specified size and alignment,
620 but where the size of the type is a constant, set the alignment to
621 the lowest power of two greater than the size, or to the
622 biggest meaningful alignment, whichever is smaller. */
624 if (Is_Atomic (gnat_entity) && !gnu_size && align == 0
625 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
627 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
628 || 0 <= compare_tree_int (TYPE_SIZE (gnu_type),
629 BIGGEST_ALIGNMENT))
630 align = BIGGEST_ALIGNMENT;
631 else
632 align = ((unsigned int) 1
633 << (floor_log2 (tree_low_cst
634 (TYPE_SIZE (gnu_type), 1) - 1)
635 + 1));
638 /* If the object is set to have atomic components, find the component
639 type and validate it.
641 ??? Note that we ignore Has_Volatile_Components on objects; it's
642 not at all clear what to do in that case. */
644 if (Has_Atomic_Components (gnat_entity))
646 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
647 ? TREE_TYPE (gnu_type) : gnu_type);
649 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
650 && TYPE_MULTI_ARRAY_P (gnu_inner))
651 gnu_inner = TREE_TYPE (gnu_inner);
653 check_ok_for_atomic (gnu_inner, gnat_entity, true);
656 /* Now check if the type of the object allows atomic access. Note
657 that we must test the type, even if this object has size and
658 alignment to allow such access, because we will be going
659 inside the padded record to assign to the object. We could fix
660 this by always copying via an intermediate value, but it's not
661 clear it's worth the effort. */
662 if (Is_Atomic (gnat_entity))
663 check_ok_for_atomic (gnu_type, gnat_entity, false);
665 /* If this is an aliased object with an unconstrained nominal subtype,
666 make a type that includes the template. */
667 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
668 && Is_Array_Type (Etype (gnat_entity))
669 && !type_annotate_only)
671 tree gnu_fat
672 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
673 tree gnu_temp_type
674 = TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat))));
676 gnu_type
677 = build_unc_object_type (gnu_temp_type, gnu_type,
678 concat_id_with_name (gnu_entity_id,
679 "UNC"));
682 #ifdef MINIMUM_ATOMIC_ALIGNMENT
683 /* If the size is a constant and no alignment is specified, force
684 the alignment to be the minimum valid atomic alignment. The
685 restriction on constant size avoids problems with variable-size
686 temporaries; if the size is variable, there's no issue with
687 atomic access. Also don't do this for a constant, since it isn't
688 necessary and can interfere with constant replacement. Finally,
689 do not do it for Out parameters since that creates an
690 size inconsistency with In parameters. */
691 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
692 && !FLOAT_TYPE_P (gnu_type)
693 && !const_flag && No (Renamed_Object (gnat_entity))
694 && !imported_p && No (Address_Clause (gnat_entity))
695 && kind != E_Out_Parameter
696 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
697 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
698 align = MINIMUM_ATOMIC_ALIGNMENT;
699 #endif
701 /* Make a new type with the desired size and alignment, if needed. */
702 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
703 "PAD", false, definition, true);
705 /* Make a volatile version of this object's type if we are to
706 make the object volatile. Note that 13.3(19) says that we
707 should treat other types of objects as volatile as well. */
708 if ((Treat_As_Volatile (gnat_entity)
709 || Is_Exported (gnat_entity)
710 || Is_Imported (gnat_entity)
711 || Present (Address_Clause (gnat_entity)))
712 && !TYPE_VOLATILE (gnu_type))
713 gnu_type = build_qualified_type (gnu_type,
714 (TYPE_QUALS (gnu_type)
715 | TYPE_QUAL_VOLATILE));
717 /* Convert the expression to the type of the object except in the
718 case where the object's type is unconstrained or the object's type
719 is a padded record whose field is of self-referential size. In
720 the former case, converting will generate unnecessary evaluations
721 of the CONSTRUCTOR to compute the size and in the latter case, we
722 want to only copy the actual data. */
723 if (gnu_expr
724 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
725 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
726 && !(TREE_CODE (gnu_type) == RECORD_TYPE
727 && TYPE_IS_PADDING_P (gnu_type)
728 && (CONTAINS_PLACEHOLDER_P
729 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
730 gnu_expr = convert (gnu_type, gnu_expr);
732 /* See if this is a renaming. If this is a constant renaming, treat
733 it as a normal variable whose initial value is what is being
734 renamed. We cannot do this if the type is unconstrained or
735 class-wide.
737 Otherwise, if what we are renaming is a reference, we can simply
738 return a stabilized version of that reference, after forcing any
739 SAVE_EXPRs to be evaluated. But, if this is at global level, we
740 can only do this if we know no SAVE_EXPRs will be made.
742 Otherwise, make this into a constant pointer to the object we are
743 to rename. */
745 if (Present (Renamed_Object (gnat_entity)))
747 /* If the renamed object had padding, strip off the reference
748 to the inner object and reset our type. */
749 if (TREE_CODE (gnu_expr) == COMPONENT_REF
750 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
751 == RECORD_TYPE)
752 && (TYPE_IS_PADDING_P
753 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
755 gnu_expr = TREE_OPERAND (gnu_expr, 0);
756 gnu_type = TREE_TYPE (gnu_expr);
759 if (const_flag
760 && !TREE_SIDE_EFFECTS (gnu_expr)
761 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
762 && TYPE_MODE (gnu_type) != BLKmode
763 && Ekind (Etype (gnat_entity)) != E_Class_Wide_Type
764 && !Is_Array_Type (Etype (gnat_entity)))
767 /* If this is a declaration or reference that we can stabilize,
768 just use that declaration or reference as this entity unless
769 the latter has to be materialized. */
770 else if ((DECL_P (gnu_expr) || REFERENCE_CLASS_P (gnu_expr))
771 && !Materialize_Entity (gnat_entity)
772 && (!global_bindings_p ()
773 || (staticp (gnu_expr)
774 && !TREE_SIDE_EFFECTS (gnu_expr))))
776 gnu_decl = gnat_stabilize_reference (gnu_expr, true);
777 save_gnu_tree (gnat_entity, gnu_decl, true);
778 saved = true;
779 break;
782 /* Otherwise, make this into a constant pointer to the object we
783 are to rename and attach the object to the pointer. We need
784 to stabilize too since the renaming evaluation may directly
785 reference the renamed object instead of the pointer we will
786 attach it to. We don't want variables in the expression to
787 be evaluated every time the renaming is used, since their
788 value may change in between. */
789 else
791 bool has_side_effects = TREE_SIDE_EFFECTS (gnu_expr);
792 inner_const_flag = TREE_READONLY (gnu_expr);
793 const_flag = true;
794 gnu_type = build_reference_type (gnu_type);
795 renamed_obj = gnat_stabilize_reference (gnu_expr, true);
796 gnu_expr = build_unary_op (ADDR_EXPR, gnu_type, renamed_obj);
798 if (!global_bindings_p ())
800 /* If the original expression had side effects, put a
801 SAVE_EXPR around this whole thing. */
802 if (has_side_effects)
803 gnu_expr = save_expr (gnu_expr);
805 add_stmt (gnu_expr);
808 gnu_size = NULL_TREE;
809 used_by_ref = true;
813 /* If this is an aliased object whose nominal subtype is unconstrained,
814 the object is a record that contains both the template and
815 the object. If there is an initializer, it will have already
816 been converted to the right type, but we need to create the
817 template if there is no initializer. */
818 else if (definition && TREE_CODE (gnu_type) == RECORD_TYPE
819 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
820 /* Beware that padding might have been introduced
821 via maybe_pad_type above. */
822 || (TYPE_IS_PADDING_P (gnu_type)
823 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
824 == RECORD_TYPE
825 && TYPE_CONTAINS_TEMPLATE_P
826 (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
827 && !gnu_expr)
829 tree template_field
830 = TYPE_IS_PADDING_P (gnu_type)
831 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
832 : TYPE_FIELDS (gnu_type);
834 gnu_expr
835 = gnat_build_constructor
836 (gnu_type,
837 tree_cons
838 (template_field,
839 build_template (TREE_TYPE (template_field),
840 TREE_TYPE (TREE_CHAIN (template_field)),
841 NULL_TREE),
842 NULL_TREE));
845 /* If this is a pointer and it does not have an initializing
846 expression, initialize it to NULL, unless the object is
847 imported. */
848 if (definition
849 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
850 && !Is_Imported (gnat_entity) && !gnu_expr)
851 gnu_expr = integer_zero_node;
853 /* If we are defining the object and it has an Address clause we must
854 get the address expression from the saved GCC tree for the
855 object if the object has a Freeze_Node. Otherwise, we elaborate
856 the address expression here since the front-end has guaranteed
857 in that case that the elaboration has no effects. Note that
858 only the latter mechanism is currently in use. */
859 if (definition && Present (Address_Clause (gnat_entity)))
861 tree gnu_address
862 = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
863 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
865 save_gnu_tree (gnat_entity, NULL_TREE, false);
867 /* Ignore the size. It's either meaningless or was handled
868 above. */
869 gnu_size = NULL_TREE;
870 gnu_type = build_reference_type (gnu_type);
871 gnu_address = convert (gnu_type, gnu_address);
872 used_by_ref = true;
873 const_flag = !Is_Public (gnat_entity);
875 /* If we don't have an initializing expression for the underlying
876 variable, the initializing expression for the pointer is the
877 specified address. Otherwise, we have to make a COMPOUND_EXPR
878 to assign both the address and the initial value. */
879 if (!gnu_expr)
880 gnu_expr = gnu_address;
881 else
882 gnu_expr
883 = build2 (COMPOUND_EXPR, gnu_type,
884 build_binary_op
885 (MODIFY_EXPR, NULL_TREE,
886 build_unary_op (INDIRECT_REF, NULL_TREE,
887 gnu_address),
888 gnu_expr),
889 gnu_address);
892 /* If it has an address clause and we are not defining it, mark it
893 as an indirect object. Likewise for Stdcall objects that are
894 imported. */
895 if ((!definition && Present (Address_Clause (gnat_entity)))
896 || (Is_Imported (gnat_entity)
897 && Convention (gnat_entity) == Convention_Stdcall))
899 gnu_type = build_reference_type (gnu_type);
900 gnu_size = NULL_TREE;
901 used_by_ref = true;
904 /* If we are at top level and this object is of variable size,
905 make the actual type a hidden pointer to the real type and
906 make the initializer be a memory allocation and initialization.
907 Likewise for objects we aren't defining (presumed to be
908 external references from other packages), but there we do
909 not set up an initialization.
911 If the object's size overflows, make an allocator too, so that
912 Storage_Error gets raised. Note that we will never free
913 such memory, so we presume it never will get allocated. */
915 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
916 global_bindings_p () || !definition
917 || static_p)
918 || (gnu_size
919 && ! allocatable_size_p (gnu_size,
920 global_bindings_p () || !definition
921 || static_p)))
923 gnu_type = build_reference_type (gnu_type);
924 gnu_size = NULL_TREE;
925 used_by_ref = true;
926 const_flag = true;
928 /* In case this was a aliased object whose nominal subtype is
929 unconstrained, the pointer above will be a thin pointer and
930 build_allocator will automatically make the template.
932 If we have a template initializer only (that we made above),
933 pretend there is none and rely on what build_allocator creates
934 again anyway. Otherwise (if we have a full initializer), get
935 the data part and feed that to build_allocator.
937 If we are elaborating a mutable object, tell build_allocator to
938 ignore a possibly simpler size from the initializer, if any, as
939 we must allocate the maximum possible size in this case. */
941 if (definition)
943 tree gnu_alloc_type = TREE_TYPE (gnu_type);
945 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
946 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
948 gnu_alloc_type
949 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
951 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
952 && VEC_length (constructor_elt,
953 CONSTRUCTOR_ELTS (gnu_expr)) == 1)
954 gnu_expr = 0;
955 else
956 gnu_expr
957 = build_component_ref
958 (gnu_expr, NULL_TREE,
959 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
960 false);
963 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
964 && TREE_CONSTANT_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
965 && !Is_Imported (gnat_entity))
966 post_error ("Storage_Error will be raised at run-time?",
967 gnat_entity);
969 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
970 0, 0, gnat_entity, mutable_p);
972 else
974 gnu_expr = NULL_TREE;
975 const_flag = false;
979 /* If this object would go into the stack and has an alignment
980 larger than the default largest alignment, make a variable
981 to hold the "aligning type" with a modified initial value,
982 if any, then point to it and make that the value of this
983 variable, which is now indirect. */
984 if (!global_bindings_p () && !static_p && definition
985 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
987 tree gnu_new_type
988 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
989 TYPE_SIZE_UNIT (gnu_type));
990 tree gnu_new_var;
992 gnu_new_var
993 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
994 NULL_TREE, gnu_new_type, gnu_expr, false,
995 false, false, false, NULL, gnat_entity);
997 if (gnu_expr)
998 add_stmt_with_node
999 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1000 build_component_ref
1001 (gnu_new_var, NULL_TREE,
1002 TYPE_FIELDS (gnu_new_type), false),
1003 gnu_expr),
1004 gnat_entity);
1006 gnu_type = build_reference_type (gnu_type);
1007 gnu_expr
1008 = build_unary_op
1009 (ADDR_EXPR, gnu_type,
1010 build_component_ref (gnu_new_var, NULL_TREE,
1011 TYPE_FIELDS (gnu_new_type), false));
1013 gnu_size = NULL_TREE;
1014 used_by_ref = true;
1015 const_flag = true;
1018 if (const_flag)
1019 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1020 | TYPE_QUAL_CONST));
1022 /* Convert the expression to the type of the object except in the
1023 case where the object's type is unconstrained or the object's type
1024 is a padded record whose field is of self-referential size. In
1025 the former case, converting will generate unnecessary evaluations
1026 of the CONSTRUCTOR to compute the size and in the latter case, we
1027 want to only copy the actual data. */
1028 if (gnu_expr
1029 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1030 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1031 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1032 && TYPE_IS_PADDING_P (gnu_type)
1033 && (CONTAINS_PLACEHOLDER_P
1034 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1035 gnu_expr = convert (gnu_type, gnu_expr);
1037 /* If this name is external or there was a name specified, use it,
1038 unless this is a VMS exception object since this would conflict
1039 with the symbol we need to export in addition. Don't use the
1040 Interface_Name if there is an address clause (see CD30005). */
1041 if (!Is_VMS_Exception (gnat_entity)
1042 && ((Present (Interface_Name (gnat_entity))
1043 && No (Address_Clause (gnat_entity)))
1044 || (Is_Public (gnat_entity)
1045 && (!Is_Imported (gnat_entity)
1046 || Is_Exported (gnat_entity)))))
1047 gnu_ext_name = create_concat_name (gnat_entity, 0);
1049 /* If this is constant initialized to a static constant and the
1050 object has an aggregate type, force it to be statically
1051 allocated. */
1052 if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1053 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1054 && (AGGREGATE_TYPE_P (gnu_type)
1055 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1056 && TYPE_IS_PADDING_P (gnu_type))))
1057 static_p = true;
1059 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1060 gnu_expr, const_flag,
1061 Is_Public (gnat_entity),
1062 imported_p || !definition,
1063 static_p, attr_list, gnat_entity);
1064 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1065 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1066 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1068 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1069 DECL_RENAMING_GLOBAL_P (gnu_decl) = global_bindings_p ();
1072 /* If we have an address clause and we've made this indirect, it's
1073 not enough to merely mark the type as volatile since volatile
1074 references only conflict with other volatile references while this
1075 reference must conflict with all other references. So ensure that
1076 the dereferenced value has alias set 0. */
1077 if (Present (Address_Clause (gnat_entity)) && used_by_ref)
1078 DECL_POINTER_ALIAS_SET (gnu_decl) = 0;
1080 if (definition && DECL_SIZE (gnu_decl)
1081 && get_block_jmpbuf_decl ()
1082 && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1083 || (flag_stack_check && !STACK_CHECK_BUILTIN
1084 && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1085 STACK_CHECK_MAX_VAR_SIZE))))
1086 add_stmt_with_node (build_call_1_expr
1087 (update_setjmp_buf_decl,
1088 build_unary_op (ADDR_EXPR, NULL_TREE,
1089 get_block_jmpbuf_decl ())),
1090 gnat_entity);
1092 /* If this is a public constant or we're not optimizing and we're not
1093 making a VAR_DECL for it, make one just for export or debugger
1094 use. Likewise if the address is taken or if the object or type is
1095 aliased. */
1096 if (definition && TREE_CODE (gnu_decl) == CONST_DECL
1097 && (Is_Public (gnat_entity)
1098 || optimize == 0
1099 || Address_Taken (gnat_entity)
1100 || Is_Aliased (gnat_entity)
1101 || Is_Aliased (Etype (gnat_entity))))
1103 tree gnu_corr_var
1104 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1105 gnu_expr, false, Is_Public (gnat_entity),
1106 false, static_p, NULL, gnat_entity);
1108 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1111 /* If this is declared in a block that contains a block with an
1112 exception handler, we must force this variable in memory to
1113 suppress an invalid optimization. */
1114 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1115 && Exception_Mechanism != Back_End_Exceptions)
1116 TREE_ADDRESSABLE (gnu_decl) = 1;
1118 /* Back-annotate the Alignment of the object if not already in the
1119 tree. Likewise for Esize if the object is of a constant size.
1120 But if the "object" is actually a pointer to an object, the
1121 alignment and size are the same as the type, so don't back-annotate
1122 the values for the pointer. */
1123 if (!used_by_ref && Unknown_Alignment (gnat_entity))
1124 Set_Alignment (gnat_entity,
1125 UI_From_Int (DECL_ALIGN (gnu_decl) / BITS_PER_UNIT));
1127 if (!used_by_ref && Unknown_Esize (gnat_entity)
1128 && DECL_SIZE (gnu_decl))
1130 tree gnu_back_size = DECL_SIZE (gnu_decl);
1132 if (TREE_CODE (TREE_TYPE (gnu_decl)) == RECORD_TYPE
1133 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_decl)))
1134 gnu_back_size
1135 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN
1136 (TYPE_FIELDS (TREE_TYPE (gnu_decl)))));
1138 Set_Esize (gnat_entity, annotate_value (gnu_back_size));
1141 break;
1143 case E_Void:
1144 /* Return a TYPE_DECL for "void" that we previously made. */
1145 gnu_decl = void_type_decl_node;
1146 break;
1148 case E_Enumeration_Type:
1149 /* A special case, for the types Character and Wide_Character in
1150 Standard, we do not list all the literals. So if the literals
1151 are not specified, make this an unsigned type. */
1152 if (No (First_Literal (gnat_entity)))
1154 gnu_type = make_unsigned_type (esize);
1155 break;
1158 /* Normal case of non-character type, or non-Standard character type */
1160 /* Here we have a list of enumeral constants in First_Literal.
1161 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1162 the list to be places into TYPE_FIELDS. Each node in the list
1163 is a TREE_LIST node whose TREE_VALUE is the literal name
1164 and whose TREE_PURPOSE is the value of the literal.
1166 Esize contains the number of bits needed to represent the enumeral
1167 type, Type_Low_Bound also points to the first literal and
1168 Type_High_Bound points to the last literal. */
1170 Entity_Id gnat_literal;
1171 tree gnu_literal_list = NULL_TREE;
1173 if (Is_Unsigned_Type (gnat_entity))
1174 gnu_type = make_unsigned_type (esize);
1175 else
1176 gnu_type = make_signed_type (esize);
1178 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1180 for (gnat_literal = First_Literal (gnat_entity);
1181 Present (gnat_literal);
1182 gnat_literal = Next_Literal (gnat_literal))
1184 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1185 gnu_type);
1186 tree gnu_literal
1187 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1188 gnu_type, gnu_value, true, false, false,
1189 false, NULL, gnat_literal);
1191 save_gnu_tree (gnat_literal, gnu_literal, false);
1192 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1193 gnu_value, gnu_literal_list);
1196 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1198 /* Note that the bounds are updated at the end of this function
1199 because to avoid an infinite recursion when we get the bounds of
1200 this type, since those bounds are objects of this type. */
1202 break;
1204 case E_Signed_Integer_Type:
1205 case E_Ordinary_Fixed_Point_Type:
1206 case E_Decimal_Fixed_Point_Type:
1207 /* For integer types, just make a signed type the appropriate number
1208 of bits. */
1209 gnu_type = make_signed_type (esize);
1210 break;
1212 case E_Modular_Integer_Type:
1213 /* For modular types, make the unsigned type of the proper number of
1214 bits and then set up the modulus, if required. */
1216 enum machine_mode mode;
1217 tree gnu_modulus;
1218 tree gnu_high = 0;
1220 if (Is_Packed_Array_Type (gnat_entity))
1221 esize = UI_To_Int (RM_Size (gnat_entity));
1223 /* Find the smallest mode at least ESIZE bits wide and make a class
1224 using that mode. */
1226 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1227 GET_MODE_BITSIZE (mode) < esize;
1228 mode = GET_MODE_WIDER_MODE (mode))
1231 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1232 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1233 = Is_Packed_Array_Type (gnat_entity);
1235 /* Get the modulus in this type. If it overflows, assume it is because
1236 it is equal to 2**Esize. Note that there is no overflow checking
1237 done on unsigned type, so we detect the overflow by looking for
1238 a modulus of zero, which is otherwise invalid. */
1239 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1241 if (!integer_zerop (gnu_modulus))
1243 TYPE_MODULAR_P (gnu_type) = 1;
1244 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1245 gnu_high = fold (build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1246 convert (gnu_type, integer_one_node)));
1249 /* If we have to set TYPE_PRECISION different from its natural value,
1250 make a subtype to do do. Likewise if there is a modulus and
1251 it is not one greater than TYPE_MAX_VALUE. */
1252 if (TYPE_PRECISION (gnu_type) != esize
1253 || (TYPE_MODULAR_P (gnu_type)
1254 && !tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1256 tree gnu_subtype = make_node (INTEGER_TYPE);
1258 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1259 TREE_TYPE (gnu_subtype) = gnu_type;
1260 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1261 TYPE_MAX_VALUE (gnu_subtype)
1262 = TYPE_MODULAR_P (gnu_type)
1263 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1264 TYPE_PRECISION (gnu_subtype) = esize;
1265 TYPE_UNSIGNED (gnu_subtype) = 1;
1266 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1267 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1268 = Is_Packed_Array_Type (gnat_entity);
1269 layout_type (gnu_subtype);
1271 gnu_type = gnu_subtype;
1274 break;
1276 case E_Signed_Integer_Subtype:
1277 case E_Enumeration_Subtype:
1278 case E_Modular_Integer_Subtype:
1279 case E_Ordinary_Fixed_Point_Subtype:
1280 case E_Decimal_Fixed_Point_Subtype:
1282 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1283 that we do not want to call build_range_type since we would
1284 like each subtype node to be distinct. This will be important
1285 when memory aliasing is implemented.
1287 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1288 parent type; this fact is used by the arithmetic conversion
1289 functions.
1291 We elaborate the Ancestor_Subtype if it is not in the current
1292 unit and one of our bounds is non-static. We do this to ensure
1293 consistent naming in the case where several subtypes share the same
1294 bounds by always elaborating the first such subtype first, thus
1295 using its name. */
1297 if (definition == 0
1298 && Present (Ancestor_Subtype (gnat_entity))
1299 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1300 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1301 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1302 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1303 gnu_expr, definition);
1305 gnu_type = make_node (INTEGER_TYPE);
1306 if (Is_Packed_Array_Type (gnat_entity))
1308 esize = UI_To_Int (RM_Size (gnat_entity));
1309 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1312 TYPE_PRECISION (gnu_type) = esize;
1313 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1315 TYPE_MIN_VALUE (gnu_type)
1316 = convert (TREE_TYPE (gnu_type),
1317 elaborate_expression (Type_Low_Bound (gnat_entity),
1318 gnat_entity,
1319 get_identifier ("L"), definition, 1,
1320 Needs_Debug_Info (gnat_entity)));
1322 TYPE_MAX_VALUE (gnu_type)
1323 = convert (TREE_TYPE (gnu_type),
1324 elaborate_expression (Type_High_Bound (gnat_entity),
1325 gnat_entity,
1326 get_identifier ("U"), definition, 1,
1327 Needs_Debug_Info (gnat_entity)));
1329 /* One of the above calls might have caused us to be elaborated,
1330 so don't blow up if so. */
1331 if (present_gnu_tree (gnat_entity))
1333 maybe_present = true;
1334 break;
1337 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1338 = Has_Biased_Representation (gnat_entity);
1340 /* This should be an unsigned type if the lower bound is constant
1341 and non-negative or if the base type is unsigned; a signed type
1342 otherwise. */
1343 TYPE_UNSIGNED (gnu_type)
1344 = (TYPE_UNSIGNED (TREE_TYPE (gnu_type))
1345 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1346 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1347 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1348 || Is_Unsigned_Type (gnat_entity));
1350 layout_type (gnu_type);
1352 /* If the type we are dealing with is to represent a packed array,
1353 we need to have the bits left justified on big-endian targets
1354 and right justified on little-endian targets. We also need to
1355 ensure that when the value is read (e.g. for comparison of two
1356 such values), we only get the good bits, since the unused bits
1357 are uninitialized. Both goals are accomplished by wrapping the
1358 modular value in an enclosing struct. */
1359 if (Is_Packed_Array_Type (gnat_entity))
1361 tree gnu_field_type = gnu_type;
1362 tree gnu_field;
1364 TYPE_RM_SIZE_NUM (gnu_field_type)
1365 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1366 gnu_type = make_node (RECORD_TYPE);
1367 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1368 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1369 TYPE_PACKED (gnu_type) = 1;
1371 /* Create a stripped-down declaration of the original type, mainly
1372 for debugging. */
1373 create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1374 NULL, true, debug_info_p, gnat_entity);
1376 /* Don't notify the field as "addressable", since we won't be taking
1377 it's address and it would prevent create_field_decl from making a
1378 bitfield. */
1379 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1380 gnu_field_type, gnu_type, 1, 0, 0, 0);
1382 finish_record_type (gnu_type, gnu_field, false, false);
1383 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1384 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1387 break;
1389 case E_Floating_Point_Type:
1390 /* If this is a VAX floating-point type, use an integer of the proper
1391 size. All the operations will be handled with ASM statements. */
1392 if (Vax_Float (gnat_entity))
1394 gnu_type = make_signed_type (esize);
1395 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1396 SET_TYPE_DIGITS_VALUE (gnu_type,
1397 UI_To_gnu (Digits_Value (gnat_entity),
1398 sizetype));
1399 break;
1402 /* The type of the Low and High bounds can be our type if this is
1403 a type from Standard, so set them at the end of the function. */
1404 gnu_type = make_node (REAL_TYPE);
1405 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1406 layout_type (gnu_type);
1407 break;
1409 case E_Floating_Point_Subtype:
1410 if (Vax_Float (gnat_entity))
1412 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1413 break;
1417 if (definition == 0
1418 && Present (Ancestor_Subtype (gnat_entity))
1419 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1420 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1421 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1422 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1423 gnu_expr, definition);
1425 gnu_type = make_node (REAL_TYPE);
1426 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1427 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1429 TYPE_MIN_VALUE (gnu_type)
1430 = convert (TREE_TYPE (gnu_type),
1431 elaborate_expression (Type_Low_Bound (gnat_entity),
1432 gnat_entity, get_identifier ("L"),
1433 definition, 1,
1434 Needs_Debug_Info (gnat_entity)));
1436 TYPE_MAX_VALUE (gnu_type)
1437 = convert (TREE_TYPE (gnu_type),
1438 elaborate_expression (Type_High_Bound (gnat_entity),
1439 gnat_entity, get_identifier ("U"),
1440 definition, 1,
1441 Needs_Debug_Info (gnat_entity)));
1443 /* One of the above calls might have caused us to be elaborated,
1444 so don't blow up if so. */
1445 if (present_gnu_tree (gnat_entity))
1447 maybe_present = true;
1448 break;
1451 layout_type (gnu_type);
1453 break;
1455 /* Array and String Types and Subtypes
1457 Unconstrained array types are represented by E_Array_Type and
1458 constrained array types are represented by E_Array_Subtype. There
1459 are no actual objects of an unconstrained array type; all we have
1460 are pointers to that type.
1462 The following fields are defined on array types and subtypes:
1464 Component_Type Component type of the array.
1465 Number_Dimensions Number of dimensions (an int).
1466 First_Index Type of first index. */
1468 case E_String_Type:
1469 case E_Array_Type:
1471 tree gnu_template_fields = NULL_TREE;
1472 tree gnu_template_type = make_node (RECORD_TYPE);
1473 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1474 tree gnu_fat_type = make_node (RECORD_TYPE);
1475 int ndim = Number_Dimensions (gnat_entity);
1476 int firstdim
1477 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1478 int nextdim
1479 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1480 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1481 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1482 tree gnu_comp_size = 0;
1483 tree gnu_max_size = size_one_node;
1484 tree gnu_max_size_unit;
1485 int index;
1486 Entity_Id gnat_ind_subtype;
1487 Entity_Id gnat_ind_base_subtype;
1488 tree gnu_template_reference;
1489 tree tem;
1491 TYPE_NAME (gnu_template_type)
1492 = create_concat_name (gnat_entity, "XUB");
1493 TYPE_NAME (gnu_fat_type) = create_concat_name (gnat_entity, "XUP");
1494 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1495 TYPE_READONLY (gnu_template_type) = 1;
1497 /* Make a node for the array. If we are not defining the array
1498 suppress expanding incomplete types and save the node as the type
1499 for GNAT_ENTITY. */
1500 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1501 if (!definition)
1503 defer_incomplete_level++;
1504 this_deferred = this_made_decl = true;
1505 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
1506 !Comes_From_Source (gnat_entity),
1507 debug_info_p, gnat_entity);
1508 save_gnu_tree (gnat_entity, gnu_decl, false);
1509 saved = true;
1512 /* Build the fat pointer type. Use a "void *" object instead of
1513 a pointer to the array type since we don't have the array type
1514 yet (it will reference the fat pointer via the bounds). */
1515 tem = chainon (chainon (NULL_TREE,
1516 create_field_decl (get_identifier ("P_ARRAY"),
1517 ptr_void_type_node,
1518 gnu_fat_type, 0, 0, 0, 0)),
1519 create_field_decl (get_identifier ("P_BOUNDS"),
1520 gnu_ptr_template,
1521 gnu_fat_type, 0, 0, 0, 0));
1523 /* Make sure we can put this into a register. */
1524 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1525 finish_record_type (gnu_fat_type, tem, false, true);
1527 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1528 is the fat pointer. This will be used to access the individual
1529 fields once we build them. */
1530 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1531 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1532 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1533 gnu_template_reference
1534 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1535 TREE_READONLY (gnu_template_reference) = 1;
1537 /* Now create the GCC type for each index and add the fields for
1538 that index to the template. */
1539 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1540 gnat_ind_base_subtype
1541 = First_Index (Implementation_Base_Type (gnat_entity));
1542 index < ndim && index >= 0;
1543 index += nextdim,
1544 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1545 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1547 char field_name[10];
1548 tree gnu_ind_subtype
1549 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1550 tree gnu_base_subtype
1551 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1552 tree gnu_base_min
1553 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1554 tree gnu_base_max
1555 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1556 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1558 /* Make the FIELD_DECLs for the minimum and maximum of this
1559 type and then make extractions of that field from the
1560 template. */
1561 sprintf (field_name, "LB%d", index);
1562 gnu_min_field = create_field_decl (get_identifier (field_name),
1563 gnu_ind_subtype,
1564 gnu_template_type, 0, 0, 0, 0);
1565 field_name[0] = 'U';
1566 gnu_max_field = create_field_decl (get_identifier (field_name),
1567 gnu_ind_subtype,
1568 gnu_template_type, 0, 0, 0, 0);
1570 Sloc_to_locus (Sloc (gnat_entity),
1571 &DECL_SOURCE_LOCATION (gnu_min_field));
1572 Sloc_to_locus (Sloc (gnat_entity),
1573 &DECL_SOURCE_LOCATION (gnu_max_field));
1574 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1576 /* We can't use build_component_ref here since the template
1577 type isn't complete yet. */
1578 gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1579 gnu_template_reference, gnu_min_field,
1580 NULL_TREE);
1581 gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1582 gnu_template_reference, gnu_max_field,
1583 NULL_TREE);
1584 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1586 /* Make a range type with the new ranges, but using
1587 the Ada subtype. Then we convert to sizetype. */
1588 gnu_index_types[index]
1589 = create_index_type (convert (sizetype, gnu_min),
1590 convert (sizetype, gnu_max),
1591 build_range_type (gnu_ind_subtype,
1592 gnu_min, gnu_max));
1593 /* Update the maximum size of the array, in elements. */
1594 gnu_max_size
1595 = size_binop (MULT_EXPR, gnu_max_size,
1596 size_binop (PLUS_EXPR, size_one_node,
1597 size_binop (MINUS_EXPR, gnu_base_max,
1598 gnu_base_min)));
1600 TYPE_NAME (gnu_index_types[index])
1601 = create_concat_name (gnat_entity, field_name);
1604 for (index = 0; index < ndim; index++)
1605 gnu_template_fields
1606 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1608 /* Install all the fields into the template. */
1609 finish_record_type (gnu_template_type, gnu_template_fields,
1610 false, false);
1611 TYPE_READONLY (gnu_template_type) = 1;
1613 /* Now make the array of arrays and update the pointer to the array
1614 in the fat pointer. Note that it is the first field. */
1616 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1618 /* Get and validate any specified Component_Size, but if Packed,
1619 ignore it since the front end will have taken care of it. */
1620 gnu_comp_size
1621 = validate_size (Component_Size (gnat_entity), tem,
1622 gnat_entity,
1623 (Is_Bit_Packed_Array (gnat_entity)
1624 ? TYPE_DECL : VAR_DECL),
1625 true, Has_Component_Size_Clause (gnat_entity));
1627 if (Has_Atomic_Components (gnat_entity))
1628 check_ok_for_atomic (tem, gnat_entity, true);
1630 /* If the component type is a RECORD_TYPE that has a self-referential
1631 size, use the maxium size. */
1632 if (!gnu_comp_size && TREE_CODE (tem) == RECORD_TYPE
1633 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1634 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1636 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
1638 tem = make_type_from_size (tem, gnu_comp_size, false);
1639 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1640 "C_PAD", false, definition, true);
1643 if (Has_Volatile_Components (gnat_entity))
1644 tem = build_qualified_type (tem,
1645 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1647 /* If Component_Size is not already specified, annotate it with the
1648 size of the component. */
1649 if (Unknown_Component_Size (gnat_entity))
1650 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1652 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1653 size_binop (MULT_EXPR, gnu_max_size,
1654 TYPE_SIZE_UNIT (tem)));
1655 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1656 size_binop (MULT_EXPR,
1657 convert (bitsizetype,
1658 gnu_max_size),
1659 TYPE_SIZE (tem)));
1661 for (index = ndim - 1; index >= 0; index--)
1663 tem = build_array_type (tem, gnu_index_types[index]);
1664 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1666 /* If the type below this an multi-array type, then this
1667 does not not have aliased components.
1669 ??? Otherwise, for now, we say that any component of aggregate
1670 type is addressable because the front end may take 'Reference
1671 of it. But we have to make it addressable if it must be passed
1672 by reference or it that is the default. */
1673 TYPE_NONALIASED_COMPONENT (tem)
1674 = ((TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
1675 && TYPE_MULTI_ARRAY_P (TREE_TYPE (tem))) ? 1
1676 : (!Has_Aliased_Components (gnat_entity)
1677 && !AGGREGATE_TYPE_P (TREE_TYPE (tem))));
1680 /* If an alignment is specified, use it if valid. But ignore it for
1681 types that represent the unpacked base type for packed arrays. */
1682 if (No (Packed_Array_Type (gnat_entity))
1683 && Known_Alignment (gnat_entity))
1685 gcc_assert (Present (Alignment (gnat_entity)));
1686 TYPE_ALIGN (tem)
1687 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1688 TYPE_ALIGN (tem));
1691 TYPE_CONVENTION_FORTRAN_P (tem)
1692 = (Convention (gnat_entity) == Convention_Fortran);
1693 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1695 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1696 corresponding fat pointer. */
1697 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1698 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1699 TYPE_MODE (gnu_type) = BLKmode;
1700 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1701 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
1703 /* If the maximum size doesn't overflow, use it. */
1704 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1705 && !TREE_OVERFLOW (gnu_max_size))
1706 TYPE_SIZE (tem)
1707 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1708 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
1709 && !TREE_OVERFLOW (gnu_max_size_unit))
1710 TYPE_SIZE_UNIT (tem)
1711 = size_binop (MIN_EXPR, gnu_max_size_unit,
1712 TYPE_SIZE_UNIT (tem));
1714 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1715 tem, NULL, !Comes_From_Source (gnat_entity),
1716 debug_info_p, gnat_entity);
1718 /* Create a record type for the object and its template and
1719 set the template at a negative offset. */
1720 tem = build_unc_object_type (gnu_template_type, tem,
1721 create_concat_name (gnat_entity, "XUT"));
1722 DECL_FIELD_OFFSET (TYPE_FIELDS (tem))
1723 = size_binop (MINUS_EXPR, size_zero_node,
1724 byte_position (TREE_CHAIN (TYPE_FIELDS (tem))));
1725 DECL_FIELD_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem))) = size_zero_node;
1726 DECL_FIELD_BIT_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem)))
1727 = bitsize_zero_node;
1728 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
1729 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1731 /* Give the thin pointer type a name. */
1732 create_type_decl (create_concat_name (gnat_entity, "XUX"),
1733 build_pointer_type (tem), NULL,
1734 !Comes_From_Source (gnat_entity), debug_info_p,
1735 gnat_entity);
1737 break;
1739 case E_String_Subtype:
1740 case E_Array_Subtype:
1742 /* This is the actual data type for array variables. Multidimensional
1743 arrays are implemented in the gnu tree as arrays of arrays. Note
1744 that for the moment arrays which have sparse enumeration subtypes as
1745 index components create sparse arrays, which is obviously space
1746 inefficient but so much easier to code for now.
1748 Also note that the subtype never refers to the unconstrained
1749 array type, which is somewhat at variance with Ada semantics.
1751 First check to see if this is simply a renaming of the array
1752 type. If so, the result is the array type. */
1754 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1755 if (!Is_Constrained (gnat_entity))
1756 break;
1757 else
1759 int index;
1760 int array_dim = Number_Dimensions (gnat_entity);
1761 int first_dim
1762 = ((Convention (gnat_entity) == Convention_Fortran)
1763 ? array_dim - 1 : 0);
1764 int next_dim
1765 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
1766 Entity_Id gnat_ind_subtype;
1767 Entity_Id gnat_ind_base_subtype;
1768 tree gnu_base_type = gnu_type;
1769 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
1770 tree gnu_comp_size = NULL_TREE;
1771 tree gnu_max_size = size_one_node;
1772 tree gnu_max_size_unit;
1773 bool need_index_type_struct = false;
1774 bool max_overflow = false;
1776 /* First create the gnu types for each index. Create types for
1777 debugging information to point to the index types if the
1778 are not integer types, have variable bounds, or are
1779 wider than sizetype. */
1781 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1782 gnat_ind_base_subtype
1783 = First_Index (Implementation_Base_Type (gnat_entity));
1784 index < array_dim && index >= 0;
1785 index += next_dim,
1786 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1787 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1789 tree gnu_index_subtype
1790 = get_unpadded_type (Etype (gnat_ind_subtype));
1791 tree gnu_min
1792 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
1793 tree gnu_max
1794 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
1795 tree gnu_base_subtype
1796 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1797 tree gnu_base_min
1798 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1799 tree gnu_base_max
1800 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1801 tree gnu_base_type = get_base_type (gnu_base_subtype);
1802 tree gnu_base_base_min
1803 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
1804 tree gnu_base_base_max
1805 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
1806 tree gnu_high;
1807 tree gnu_this_max;
1809 /* If the minimum and maximum values both overflow in
1810 SIZETYPE, but the difference in the original type
1811 does not overflow in SIZETYPE, ignore the overflow
1812 indications. */
1813 if ((TYPE_PRECISION (gnu_index_subtype)
1814 > TYPE_PRECISION (sizetype)
1815 || TYPE_UNSIGNED (gnu_index_subtype)
1816 != TYPE_UNSIGNED (sizetype))
1817 && TREE_CODE (gnu_min) == INTEGER_CST
1818 && TREE_CODE (gnu_max) == INTEGER_CST
1819 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
1820 && (!TREE_OVERFLOW
1821 (fold (build2 (MINUS_EXPR, gnu_index_subtype,
1822 TYPE_MAX_VALUE (gnu_index_subtype),
1823 TYPE_MIN_VALUE (gnu_index_subtype))))))
1824 TREE_OVERFLOW (gnu_min) = TREE_OVERFLOW (gnu_max)
1825 = TREE_CONSTANT_OVERFLOW (gnu_min)
1826 = TREE_CONSTANT_OVERFLOW (gnu_max) = 0;
1828 /* Similarly, if the range is null, use bounds of 1..0 for
1829 the sizetype bounds. */
1830 else if ((TYPE_PRECISION (gnu_index_subtype)
1831 > TYPE_PRECISION (sizetype)
1832 || TYPE_UNSIGNED (gnu_index_subtype)
1833 != TYPE_UNSIGNED (sizetype))
1834 && TREE_CODE (gnu_min) == INTEGER_CST
1835 && TREE_CODE (gnu_max) == INTEGER_CST
1836 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
1837 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
1838 TYPE_MIN_VALUE (gnu_index_subtype)))
1839 gnu_min = size_one_node, gnu_max = size_zero_node;
1841 /* Now compute the size of this bound. We need to provide
1842 GCC with an upper bound to use but have to deal with the
1843 "superflat" case. There are three ways to do this. If we
1844 can prove that the array can never be superflat, we can
1845 just use the high bound of the index subtype. If we can
1846 prove that the low bound minus one can't overflow, we
1847 can do this as MAX (hb, lb - 1). Otherwise, we have to use
1848 the expression hb >= lb ? hb : lb - 1. */
1849 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
1851 /* See if the base array type is already flat. If it is, we
1852 are probably compiling an ACVC test, but it will cause the
1853 code below to malfunction if we don't handle it specially. */
1854 if (TREE_CODE (gnu_base_min) == INTEGER_CST
1855 && TREE_CODE (gnu_base_max) == INTEGER_CST
1856 && !TREE_CONSTANT_OVERFLOW (gnu_base_min)
1857 && !TREE_CONSTANT_OVERFLOW (gnu_base_max)
1858 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
1859 gnu_high = size_zero_node, gnu_min = size_one_node;
1861 /* If gnu_high is now an integer which overflowed, the array
1862 cannot be superflat. */
1863 else if (TREE_CODE (gnu_high) == INTEGER_CST
1864 && TREE_OVERFLOW (gnu_high))
1865 gnu_high = gnu_max;
1866 else if (TYPE_UNSIGNED (gnu_base_subtype)
1867 || TREE_CODE (gnu_high) == INTEGER_CST)
1868 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
1869 else
1870 gnu_high
1871 = build_cond_expr
1872 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
1873 gnu_max, gnu_min),
1874 gnu_max, gnu_high);
1876 gnu_index_type[index]
1877 = create_index_type (gnu_min, gnu_high, gnu_index_subtype);
1879 /* Also compute the maximum size of the array. Here we
1880 see if any constraint on the index type of the base type
1881 can be used in the case of self-referential bound on
1882 the index type of the subtype. We look for a non-"infinite"
1883 and non-self-referential bound from any type involved and
1884 handle each bound separately. */
1886 if ((TREE_CODE (gnu_min) == INTEGER_CST
1887 && !TREE_OVERFLOW (gnu_min)
1888 && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
1889 || !CONTAINS_PLACEHOLDER_P (gnu_min))
1890 gnu_base_min = gnu_min;
1892 if ((TREE_CODE (gnu_max) == INTEGER_CST
1893 && !TREE_OVERFLOW (gnu_max)
1894 && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
1895 || !CONTAINS_PLACEHOLDER_P (gnu_max))
1896 gnu_base_max = gnu_max;
1898 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
1899 && TREE_CONSTANT_OVERFLOW (gnu_base_min))
1900 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
1901 || (TREE_CODE (gnu_base_max) == INTEGER_CST
1902 && TREE_CONSTANT_OVERFLOW (gnu_base_max))
1903 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
1904 max_overflow = true;
1906 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
1907 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
1909 gnu_this_max
1910 = size_binop (MAX_EXPR,
1911 size_binop (PLUS_EXPR, size_one_node,
1912 size_binop (MINUS_EXPR, gnu_base_max,
1913 gnu_base_min)),
1914 size_zero_node);
1916 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1917 && TREE_CONSTANT_OVERFLOW (gnu_this_max))
1918 max_overflow = true;
1920 gnu_max_size
1921 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1923 if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
1924 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
1925 != INTEGER_CST)
1926 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
1927 || (TREE_TYPE (gnu_index_subtype)
1928 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
1929 != INTEGER_TYPE))
1930 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
1931 || (TYPE_PRECISION (gnu_index_subtype)
1932 > TYPE_PRECISION (sizetype)))
1933 need_index_type_struct = true;
1936 /* Then flatten: create the array of arrays. */
1938 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
1940 /* One of the above calls might have caused us to be elaborated,
1941 so don't blow up if so. */
1942 if (present_gnu_tree (gnat_entity))
1944 maybe_present = true;
1945 break;
1948 /* Get and validate any specified Component_Size, but if Packed,
1949 ignore it since the front end will have taken care of it. */
1950 gnu_comp_size
1951 = validate_size (Component_Size (gnat_entity), gnu_type,
1952 gnat_entity,
1953 (Is_Bit_Packed_Array (gnat_entity)
1954 ? TYPE_DECL : VAR_DECL),
1955 true, Has_Component_Size_Clause (gnat_entity));
1957 /* If the component type is a RECORD_TYPE that has a self-referential
1958 size, use the maxium size. */
1959 if (!gnu_comp_size && TREE_CODE (gnu_type) == RECORD_TYPE
1960 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
1961 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
1963 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
1965 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
1966 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
1967 gnat_entity, "C_PAD", false,
1968 definition, true);
1971 if (Has_Volatile_Components (Base_Type (gnat_entity)))
1972 gnu_type = build_qualified_type (gnu_type,
1973 (TYPE_QUALS (gnu_type)
1974 | TYPE_QUAL_VOLATILE));
1976 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
1977 TYPE_SIZE_UNIT (gnu_type));
1978 gnu_max_size = size_binop (MULT_EXPR,
1979 convert (bitsizetype, gnu_max_size),
1980 TYPE_SIZE (gnu_type));
1982 for (index = array_dim - 1; index >= 0; index --)
1984 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
1985 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
1986 /* If the type below this an multi-array type, then this
1987 does not not have aliased components.
1989 ??? Otherwise, for now, we say that any component of aggregate
1990 type is addressable because the front end may take 'Reference
1991 of it. But we have to make it addressable if it must be passed
1992 by reference or it that is the default. */
1993 TYPE_NONALIASED_COMPONENT (gnu_type)
1994 = ((TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
1995 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type))) ? 1
1996 : (!Has_Aliased_Components (gnat_entity)
1997 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_type))));
2000 /* If we are at file level and this is a multi-dimensional array, we
2001 need to make a variable corresponding to the stride of the
2002 inner dimensions. */
2003 if (global_bindings_p () && array_dim > 1)
2005 tree gnu_str_name = get_identifier ("ST");
2006 tree gnu_arr_type;
2008 for (gnu_arr_type = TREE_TYPE (gnu_type);
2009 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2010 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2011 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
2013 tree eltype = TREE_TYPE (gnu_arr_type);
2015 TYPE_SIZE (gnu_arr_type)
2016 = elaborate_expression_1 (gnat_entity, gnat_entity,
2017 TYPE_SIZE (gnu_arr_type),
2018 gnu_str_name, definition, 0);
2020 /* ??? For now, store the size as a multiple of the
2021 alignment of the element type in bytes so that we
2022 can see the alignment from the tree. */
2023 TYPE_SIZE_UNIT (gnu_arr_type)
2024 = build_binary_op
2025 (MULT_EXPR, sizetype,
2026 elaborate_expression_1
2027 (gnat_entity, gnat_entity,
2028 build_binary_op (EXACT_DIV_EXPR, sizetype,
2029 TYPE_SIZE_UNIT (gnu_arr_type),
2030 size_int (TYPE_ALIGN (eltype)
2031 / BITS_PER_UNIT)),
2032 concat_id_with_name (gnu_str_name, "A_U"),
2033 definition, 0),
2034 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2038 /* If we need to write out a record type giving the names of
2039 the bounds, do it now. */
2040 if (need_index_type_struct && debug_info_p)
2042 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
2043 tree gnu_field_list = NULL_TREE;
2044 tree gnu_field;
2046 TYPE_NAME (gnu_bound_rec_type)
2047 = create_concat_name (gnat_entity, "XA");
2049 for (index = array_dim - 1; index >= 0; index--)
2051 tree gnu_type_name
2052 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
2054 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
2055 gnu_type_name = DECL_NAME (gnu_type_name);
2057 gnu_field = create_field_decl (gnu_type_name,
2058 integer_type_node,
2059 gnu_bound_rec_type,
2060 0, NULL_TREE, NULL_TREE, 0);
2061 TREE_CHAIN (gnu_field) = gnu_field_list;
2062 gnu_field_list = gnu_field;
2065 finish_record_type (gnu_bound_rec_type, gnu_field_list,
2066 false, false);
2069 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2070 = (Convention (gnat_entity) == Convention_Fortran);
2071 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2072 = Is_Packed_Array_Type (gnat_entity);
2074 /* If our size depends on a placeholder and the maximum size doesn't
2075 overflow, use it. */
2076 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2077 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2078 && TREE_OVERFLOW (gnu_max_size))
2079 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2080 && TREE_OVERFLOW (gnu_max_size_unit))
2081 && !max_overflow)
2083 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2084 TYPE_SIZE (gnu_type));
2085 TYPE_SIZE_UNIT (gnu_type)
2086 = size_binop (MIN_EXPR, gnu_max_size_unit,
2087 TYPE_SIZE_UNIT (gnu_type));
2090 /* Set our alias set to that of our base type. This gives all
2091 array subtypes the same alias set. */
2092 copy_alias_set (gnu_type, gnu_base_type);
2095 /* If this is a packed type, make this type the same as the packed
2096 array type, but do some adjusting in the type first. */
2098 if (Present (Packed_Array_Type (gnat_entity)))
2100 Entity_Id gnat_index;
2101 tree gnu_inner_type;
2103 /* First finish the type we had been making so that we output
2104 debugging information for it */
2105 gnu_type
2106 = build_qualified_type (gnu_type,
2107 (TYPE_QUALS (gnu_type)
2108 | (TYPE_QUAL_VOLATILE
2109 * Treat_As_Volatile (gnat_entity))));
2110 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2111 !Comes_From_Source (gnat_entity),
2112 debug_info_p, gnat_entity);
2113 if (!Comes_From_Source (gnat_entity))
2114 DECL_ARTIFICIAL (gnu_decl) = 1;
2116 /* Save it as our equivalent in case the call below elaborates
2117 this type again. */
2118 save_gnu_tree (gnat_entity, gnu_decl, false);
2120 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2121 NULL_TREE, 0);
2122 this_made_decl = true;
2123 gnu_inner_type = gnu_type = TREE_TYPE (gnu_decl);
2124 save_gnu_tree (gnat_entity, NULL_TREE, false);
2126 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2127 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2128 || TYPE_IS_PADDING_P (gnu_inner_type)))
2129 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2131 /* We need to point the type we just made to our index type so
2132 the actual bounds can be put into a template. */
2134 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2135 && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2136 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2137 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2139 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2141 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2142 If it is, we need to make another type. */
2143 if (TYPE_MODULAR_P (gnu_inner_type))
2145 tree gnu_subtype;
2147 gnu_subtype = make_node (INTEGER_TYPE);
2149 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2150 TYPE_MIN_VALUE (gnu_subtype)
2151 = TYPE_MIN_VALUE (gnu_inner_type);
2152 TYPE_MAX_VALUE (gnu_subtype)
2153 = TYPE_MAX_VALUE (gnu_inner_type);
2154 TYPE_PRECISION (gnu_subtype)
2155 = TYPE_PRECISION (gnu_inner_type);
2156 TYPE_UNSIGNED (gnu_subtype)
2157 = TYPE_UNSIGNED (gnu_inner_type);
2158 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2159 layout_type (gnu_subtype);
2161 gnu_inner_type = gnu_subtype;
2164 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2167 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2169 for (gnat_index = First_Index (gnat_entity);
2170 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2171 SET_TYPE_ACTUAL_BOUNDS
2172 (gnu_inner_type,
2173 tree_cons (NULL_TREE,
2174 get_unpadded_type (Etype (gnat_index)),
2175 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2177 if (Convention (gnat_entity) != Convention_Fortran)
2178 SET_TYPE_ACTUAL_BOUNDS
2179 (gnu_inner_type,
2180 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2182 if (TREE_CODE (gnu_type) == RECORD_TYPE
2183 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2184 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2188 /* Abort if packed array with no packed array type field set. */
2189 else
2190 gcc_assert (!Is_Packed (gnat_entity));
2192 break;
2194 case E_String_Literal_Subtype:
2195 /* Create the type for a string literal. */
2197 Entity_Id gnat_full_type
2198 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2199 && Present (Full_View (Etype (gnat_entity)))
2200 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2201 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2202 tree gnu_string_array_type
2203 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2204 tree gnu_string_index_type
2205 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2206 (TYPE_DOMAIN (gnu_string_array_type))));
2207 tree gnu_lower_bound
2208 = convert (gnu_string_index_type,
2209 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2210 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2211 tree gnu_length = ssize_int (length - 1);
2212 tree gnu_upper_bound
2213 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2214 gnu_lower_bound,
2215 convert (gnu_string_index_type, gnu_length));
2216 tree gnu_range_type
2217 = build_range_type (gnu_string_index_type,
2218 gnu_lower_bound, gnu_upper_bound);
2219 tree gnu_index_type
2220 = create_index_type (convert (sizetype,
2221 TYPE_MIN_VALUE (gnu_range_type)),
2222 convert (sizetype,
2223 TYPE_MAX_VALUE (gnu_range_type)),
2224 gnu_range_type);
2226 gnu_type
2227 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2228 gnu_index_type);
2229 copy_alias_set (gnu_type, gnu_string_type);
2231 break;
2233 /* Record Types and Subtypes
2235 The following fields are defined on record types:
2237 Has_Discriminants True if the record has discriminants
2238 First_Discriminant Points to head of list of discriminants
2239 First_Entity Points to head of list of fields
2240 Is_Tagged_Type True if the record is tagged
2242 Implementation of Ada records and discriminated records:
2244 A record type definition is transformed into the equivalent of a C
2245 struct definition. The fields that are the discriminants which are
2246 found in the Full_Type_Declaration node and the elements of the
2247 Component_List found in the Record_Type_Definition node. The
2248 Component_List can be a recursive structure since each Variant of
2249 the Variant_Part of the Component_List has a Component_List.
2251 Processing of a record type definition comprises starting the list of
2252 field declarations here from the discriminants and the calling the
2253 function components_to_record to add the rest of the fields from the
2254 component list and return the gnu type node. The function
2255 components_to_record will call itself recursively as it traverses
2256 the tree. */
2258 case E_Record_Type:
2259 if (Has_Complex_Representation (gnat_entity))
2261 gnu_type
2262 = build_complex_type
2263 (get_unpadded_type
2264 (Etype (Defining_Entity
2265 (First (Component_Items
2266 (Component_List
2267 (Type_Definition
2268 (Declaration_Node (gnat_entity)))))))));
2270 break;
2274 Node_Id full_definition = Declaration_Node (gnat_entity);
2275 Node_Id record_definition = Type_Definition (full_definition);
2276 Entity_Id gnat_field;
2277 tree gnu_field;
2278 tree gnu_field_list = NULL_TREE;
2279 tree gnu_get_parent;
2280 int packed = (Is_Packed (gnat_entity) ? 1
2281 : (Component_Alignment (gnat_entity)
2282 == Calign_Storage_Unit) ? -1
2283 : 0);
2284 bool has_rep = Has_Specified_Layout (gnat_entity);
2285 bool all_rep = has_rep;
2286 bool is_extension
2287 = (Is_Tagged_Type (gnat_entity)
2288 && Nkind (record_definition) == N_Derived_Type_Definition);
2290 /* See if all fields have a rep clause. Stop when we find one
2291 that doesn't. */
2292 for (gnat_field = First_Entity (gnat_entity);
2293 Present (gnat_field) && all_rep;
2294 gnat_field = Next_Entity (gnat_field))
2295 if ((Ekind (gnat_field) == E_Component
2296 || Ekind (gnat_field) == E_Discriminant)
2297 && No (Component_Clause (gnat_field)))
2298 all_rep = false;
2300 /* If this is a record extension, go a level further to find the
2301 record definition. Also, verify we have a Parent_Subtype. */
2302 if (is_extension)
2304 if (!type_annotate_only
2305 || Present (Record_Extension_Part (record_definition)))
2306 record_definition = Record_Extension_Part (record_definition);
2308 gcc_assert (type_annotate_only
2309 || Present (Parent_Subtype (gnat_entity)));
2312 /* Make a node for the record. If we are not defining the record,
2313 suppress expanding incomplete types and save the node as the type
2314 for GNAT_ENTITY. We use the same RECORD_TYPE as for a dummy type
2315 and reset TYPE_DUMMY_P to show it's no longer a dummy.
2317 It is very tempting to delay resetting this bit until we are done
2318 with completing the type, e.g. to let possible intermediate
2319 elaboration of access types designating the record know it is not
2320 complete and arrange for update_pointer_to to fix things up later.
2322 It would be wrong, however, because dummy types are expected only
2323 to be created for Ada incomplete or private types, which is not
2324 what we have here. Doing so would make other parts of gigi think
2325 we are dealing with a really incomplete or private type, and have
2326 nasty side effects, typically on the generation of the associated
2327 debugging information. */
2328 gnu_type = make_dummy_type (gnat_entity);
2329 TYPE_DUMMY_P (gnu_type) = 0;
2331 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL && debug_info_p)
2332 DECL_IGNORED_P (TYPE_NAME (gnu_type)) = 0;
2334 TYPE_ALIGN (gnu_type) = 0;
2335 TYPE_PACKED (gnu_type) = packed || has_rep;
2337 if (!definition)
2339 defer_incomplete_level++;
2340 this_deferred = true;
2341 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2342 !Comes_From_Source (gnat_entity),
2343 debug_info_p, gnat_entity);
2344 save_gnu_tree (gnat_entity, gnu_decl, false);
2345 this_made_decl = saved = true;
2348 /* If both a size and rep clause was specified, put the size in
2349 the record type now so that it can get the proper mode. */
2350 if (has_rep && Known_Esize (gnat_entity))
2351 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2353 /* Always set the alignment here so that it can be used to
2354 set the mode, if it is making the alignment stricter. If
2355 it is invalid, it will be checked again below. If this is to
2356 be Atomic, choose a default alignment of a word unless we know
2357 the size and it's smaller. */
2358 if (Known_Alignment (gnat_entity))
2359 TYPE_ALIGN (gnu_type)
2360 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2361 else if (Is_Atomic (gnat_entity))
2362 TYPE_ALIGN (gnu_type)
2363 = (esize >= BITS_PER_WORD ? BITS_PER_WORD
2364 : 1 << (floor_log2 (esize - 1) + 1));
2366 /* If we have a Parent_Subtype, make a field for the parent. If
2367 this record has rep clauses, force the position to zero. */
2368 if (Present (Parent_Subtype (gnat_entity)))
2370 tree gnu_parent;
2372 /* A major complexity here is that the parent subtype will
2373 reference our discriminants. But those must reference
2374 the parent component of this record. So here we will
2375 initialize each of those components to a COMPONENT_REF.
2376 The first operand of that COMPONENT_REF is another
2377 COMPONENT_REF which will be filled in below, once
2378 the parent type can be safely built. */
2380 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2381 build0 (PLACEHOLDER_EXPR, gnu_type),
2382 build_decl (FIELD_DECL, NULL_TREE,
2383 NULL_TREE),
2384 NULL_TREE);
2386 if (Has_Discriminants (gnat_entity))
2387 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2388 Present (gnat_field);
2389 gnat_field = Next_Stored_Discriminant (gnat_field))
2390 if (Present (Corresponding_Discriminant (gnat_field)))
2391 save_gnu_tree
2392 (gnat_field,
2393 build3 (COMPONENT_REF,
2394 get_unpadded_type (Etype (gnat_field)),
2395 gnu_get_parent,
2396 gnat_to_gnu_field_decl (Corresponding_Discriminant
2397 (gnat_field)),
2398 NULL_TREE),
2399 true);
2401 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_entity));
2403 gnu_field_list
2404 = create_field_decl (get_identifier
2405 (Get_Name_String (Name_uParent)),
2406 gnu_parent, gnu_type, 0,
2407 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2408 has_rep ? bitsize_zero_node : 0, 1);
2409 DECL_INTERNAL_P (gnu_field_list) = 1;
2411 TREE_TYPE (gnu_get_parent) = gnu_parent;
2412 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2415 /* Add the fields for the discriminants into the record. */
2416 if (!Is_Unchecked_Union (gnat_entity)
2417 && Has_Discriminants (gnat_entity))
2418 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2419 Present (gnat_field);
2420 gnat_field = Next_Stored_Discriminant (gnat_field))
2422 /* If this is a record extension and this discriminant
2423 is the renaming of another discriminant, we've already
2424 handled the discriminant above. */
2425 if (Present (Parent_Subtype (gnat_entity))
2426 && Present (Corresponding_Discriminant (gnat_field)))
2427 continue;
2429 gnu_field
2430 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2432 /* Make an expression using a PLACEHOLDER_EXPR from the
2433 FIELD_DECL node just created and link that with the
2434 corresponding GNAT defining identifier. Then add to the
2435 list of fields. */
2436 save_gnu_tree (gnat_field,
2437 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2438 build0 (PLACEHOLDER_EXPR,
2439 DECL_CONTEXT (gnu_field)),
2440 gnu_field, NULL_TREE),
2441 true);
2443 TREE_CHAIN (gnu_field) = gnu_field_list;
2444 gnu_field_list = gnu_field;
2447 /* Put the discriminants into the record (backwards), so we can
2448 know the appropriate discriminant to use for the names of the
2449 variants. */
2450 TYPE_FIELDS (gnu_type) = gnu_field_list;
2452 /* Add the listed fields into the record and finish up. */
2453 components_to_record (gnu_type, Component_List (record_definition),
2454 gnu_field_list, packed, definition, NULL,
2455 false, all_rep, this_deferred);
2457 if (this_deferred)
2459 debug_deferred = true;
2460 defer_debug_level++;
2462 defer_debug_incomplete_list
2463 = tree_cons (NULL_TREE, gnu_type,
2464 defer_debug_incomplete_list);
2467 /* We used to remove the associations of the discriminants and
2468 _Parent for validity checking, but we may need them if there's
2469 Freeze_Node for a subtype used in this record. */
2471 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2472 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2474 /* If it is a tagged record force the type to BLKmode to insure
2475 that these objects will always be placed in memory. Do the
2476 same thing for limited record types. */
2477 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2478 TYPE_MODE (gnu_type) = BLKmode;
2480 /* If this is a derived type, we must make the alias set of this type
2481 the same as that of the type we are derived from. We assume here
2482 that the other type is already frozen. */
2483 if (Etype (gnat_entity) != gnat_entity
2484 && !(Is_Private_Type (Etype (gnat_entity))
2485 && Full_View (Etype (gnat_entity)) == gnat_entity))
2486 copy_alias_set (gnu_type, gnat_to_gnu_type (Etype (gnat_entity)));
2488 /* Fill in locations of fields. */
2489 annotate_rep (gnat_entity, gnu_type);
2491 /* If there are any entities in the chain corresponding to
2492 components that we did not elaborate, ensure we elaborate their
2493 types if they are Itypes. */
2494 for (gnat_temp = First_Entity (gnat_entity);
2495 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2496 if ((Ekind (gnat_temp) == E_Component
2497 || Ekind (gnat_temp) == E_Discriminant)
2498 && Is_Itype (Etype (gnat_temp))
2499 && !present_gnu_tree (gnat_temp))
2500 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2502 break;
2504 case E_Class_Wide_Subtype:
2505 /* If an equivalent type is present, that is what we should use.
2506 Otherwise, fall through to handle this like a record subtype
2507 since it may have constraints. */
2509 if (Present (Equivalent_Type (gnat_entity)))
2511 gnu_decl = gnat_to_gnu_entity (Equivalent_Type (gnat_entity),
2512 NULL_TREE, 0);
2513 maybe_present = true;
2514 break;
2517 /* ... fall through ... */
2519 case E_Record_Subtype:
2521 /* If Cloned_Subtype is Present it means this record subtype has
2522 identical layout to that type or subtype and we should use
2523 that GCC type for this one. The front end guarantees that
2524 the component list is shared. */
2525 if (Present (Cloned_Subtype (gnat_entity)))
2527 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2528 NULL_TREE, 0);
2529 maybe_present = true;
2532 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2533 changing the type, make a new type with each field having the
2534 type of the field in the new subtype but having the position
2535 computed by transforming every discriminant reference according
2536 to the constraints. We don't see any difference between
2537 private and nonprivate type here since derivations from types should
2538 have been deferred until the completion of the private type. */
2539 else
2541 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2542 tree gnu_base_type;
2543 tree gnu_orig_type;
2545 if (!definition)
2546 defer_incomplete_level++, this_deferred = true;
2548 /* Get the base type initially for its alignment and sizes. But
2549 if it is a padded type, we do all the other work with the
2550 unpadded type. */
2551 gnu_type = gnu_orig_type = gnu_base_type
2552 = gnat_to_gnu_type (gnat_base_type);
2554 if (TREE_CODE (gnu_type) == RECORD_TYPE
2555 && TYPE_IS_PADDING_P (gnu_type))
2556 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2558 if (present_gnu_tree (gnat_entity))
2560 maybe_present = true;
2561 break;
2564 /* When the type has discriminants, and these discriminants
2565 affect the shape of what it built, factor them in.
2567 If we are making a subtype of an Unchecked_Union (must be an
2568 Itype), just return the type.
2570 We can't just use Is_Constrained because private subtypes without
2571 discriminants of full types with discriminants with default
2572 expressions are Is_Constrained but aren't constrained! */
2574 if (IN (Ekind (gnat_base_type), Record_Kind)
2575 && !Is_For_Access_Subtype (gnat_entity)
2576 && !Is_Unchecked_Union (gnat_base_type)
2577 && Is_Constrained (gnat_entity)
2578 && Stored_Constraint (gnat_entity) != No_Elist
2579 && Present (Discriminant_Constraint (gnat_entity)))
2581 Entity_Id gnat_field;
2582 tree gnu_field_list = 0;
2583 tree gnu_pos_list
2584 = compute_field_positions (gnu_orig_type, NULL_TREE,
2585 size_zero_node, bitsize_zero_node,
2586 BIGGEST_ALIGNMENT);
2587 tree gnu_subst_list
2588 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2589 definition);
2590 tree gnu_temp;
2592 gnu_type = make_node (RECORD_TYPE);
2593 TYPE_NAME (gnu_type) = gnu_entity_id;
2594 TYPE_STUB_DECL (gnu_type)
2595 = create_type_decl (NULL_TREE, gnu_type, NULL, false, false,
2596 gnat_entity);
2597 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2599 for (gnat_field = First_Entity (gnat_entity);
2600 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2601 if ((Ekind (gnat_field) == E_Component
2602 || Ekind (gnat_field) == E_Discriminant)
2603 && (Underlying_Type (Scope (Original_Record_Component
2604 (gnat_field)))
2605 == gnat_base_type)
2606 && (No (Corresponding_Discriminant (gnat_field))
2607 || !Is_Tagged_Type (gnat_base_type)))
2609 tree gnu_old_field
2610 = gnat_to_gnu_field_decl (Original_Record_Component
2611 (gnat_field));
2612 tree gnu_offset
2613 = TREE_VALUE (purpose_member (gnu_old_field,
2614 gnu_pos_list));
2615 tree gnu_pos = TREE_PURPOSE (gnu_offset);
2616 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
2617 tree gnu_field_type
2618 = gnat_to_gnu_type (Etype (gnat_field));
2619 tree gnu_size = TYPE_SIZE (gnu_field_type);
2620 tree gnu_new_pos = 0;
2621 unsigned int offset_align
2622 = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
2624 tree gnu_field;
2626 /* If there was a component clause, the field types must be
2627 the same for the type and subtype, so copy the data from
2628 the old field to avoid recomputation here. Also if the
2629 field is justified modular and the optimization in
2630 gnat_to_gnu_field was applied. */
2631 if (Present (Component_Clause
2632 (Original_Record_Component (gnat_field)))
2633 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
2634 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
2635 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
2636 == TREE_TYPE (gnu_old_field)))
2638 gnu_size = DECL_SIZE (gnu_old_field);
2639 gnu_field_type = TREE_TYPE (gnu_old_field);
2642 /* If this was a bitfield, get the size from the old field.
2643 Also ensure the type can be placed into a bitfield. */
2644 else if (DECL_BIT_FIELD (gnu_old_field))
2646 gnu_size = DECL_SIZE (gnu_old_field);
2647 if (TYPE_MODE (gnu_field_type) == BLKmode
2648 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2649 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2650 gnu_field_type = make_packable_type (gnu_field_type);
2653 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
2654 for (gnu_temp = gnu_subst_list;
2655 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2656 gnu_pos = substitute_in_expr (gnu_pos,
2657 TREE_PURPOSE (gnu_temp),
2658 TREE_VALUE (gnu_temp));
2660 /* If the size is now a constant, we can set it as the
2661 size of the field when we make it. Otherwise, we need
2662 to deal with it specially. */
2663 if (TREE_CONSTANT (gnu_pos))
2664 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2666 gnu_field
2667 = create_field_decl
2668 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2669 0, gnu_size, gnu_new_pos,
2670 !DECL_NONADDRESSABLE_P (gnu_old_field));
2672 if (!TREE_CONSTANT (gnu_pos))
2674 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
2675 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2676 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
2677 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
2678 DECL_SIZE (gnu_field) = gnu_size;
2679 DECL_SIZE_UNIT (gnu_field)
2680 = convert (sizetype,
2681 size_binop (CEIL_DIV_EXPR, gnu_size,
2682 bitsize_unit_node));
2683 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2686 DECL_INTERNAL_P (gnu_field)
2687 = DECL_INTERNAL_P (gnu_old_field);
2688 SET_DECL_ORIGINAL_FIELD
2689 (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
2690 ? DECL_ORIGINAL_FIELD (gnu_old_field)
2691 : gnu_old_field));
2692 DECL_DISCRIMINANT_NUMBER (gnu_field)
2693 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2694 TREE_THIS_VOLATILE (gnu_field)
2695 = TREE_THIS_VOLATILE (gnu_old_field);
2696 TREE_CHAIN (gnu_field) = gnu_field_list;
2697 gnu_field_list = gnu_field;
2698 save_gnu_tree (gnat_field, gnu_field, false);
2701 /* Now go through the entities again looking for Itypes that
2702 we have not elaborated but should (e.g., Etypes of fields
2703 that have Original_Components). */
2704 for (gnat_field = First_Entity (gnat_entity);
2705 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2706 if ((Ekind (gnat_field) == E_Discriminant
2707 || Ekind (gnat_field) == E_Component)
2708 && !present_gnu_tree (Etype (gnat_field)))
2709 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
2711 finish_record_type (gnu_type, nreverse (gnu_field_list),
2712 true, false);
2714 /* Now set the size, alignment and alias set of the new type to
2715 match that of the old one, doing any substitutions, as
2716 above. */
2717 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2718 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2719 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
2720 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
2721 copy_alias_set (gnu_type, gnu_base_type);
2723 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2724 for (gnu_temp = gnu_subst_list;
2725 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2726 TYPE_SIZE (gnu_type)
2727 = substitute_in_expr (TYPE_SIZE (gnu_type),
2728 TREE_PURPOSE (gnu_temp),
2729 TREE_VALUE (gnu_temp));
2731 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
2732 for (gnu_temp = gnu_subst_list;
2733 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2734 TYPE_SIZE_UNIT (gnu_type)
2735 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
2736 TREE_PURPOSE (gnu_temp),
2737 TREE_VALUE (gnu_temp));
2739 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
2740 for (gnu_temp = gnu_subst_list;
2741 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2742 SET_TYPE_ADA_SIZE
2743 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
2744 TREE_PURPOSE (gnu_temp),
2745 TREE_VALUE (gnu_temp)));
2747 /* Recompute the mode of this record type now that we know its
2748 actual size. */
2749 compute_record_mode (gnu_type);
2751 /* Fill in locations of fields. */
2752 annotate_rep (gnat_entity, gnu_type);
2755 /* If we've made a new type, record it and make an XVS type to show
2756 what this is a subtype of. Some debuggers require the XVS
2757 type to be output first, so do it in that order. */
2758 if (gnu_type != gnu_orig_type)
2760 if (debug_info_p)
2762 tree gnu_subtype_marker = make_node (RECORD_TYPE);
2763 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
2765 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
2766 gnu_orig_name = DECL_NAME (gnu_orig_name);
2768 TYPE_NAME (gnu_subtype_marker)
2769 = create_concat_name (gnat_entity, "XVS");
2770 finish_record_type (gnu_subtype_marker,
2771 create_field_decl (gnu_orig_name,
2772 integer_type_node,
2773 gnu_subtype_marker,
2774 0, NULL_TREE,
2775 NULL_TREE, 0),
2776 false, false);
2779 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2780 TYPE_NAME (gnu_type) = gnu_entity_id;
2781 TYPE_STUB_DECL (gnu_type)
2782 = create_type_decl (TYPE_NAME (gnu_type), gnu_type,
2783 NULL, true, debug_info_p, gnat_entity);
2786 /* Otherwise, go down all the components in the new type and
2787 make them equivalent to those in the base type. */
2788 else
2789 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
2790 gnat_temp = Next_Entity (gnat_temp))
2791 if ((Ekind (gnat_temp) == E_Discriminant
2792 && !Is_Unchecked_Union (gnat_base_type))
2793 || Ekind (gnat_temp) == E_Component)
2794 save_gnu_tree (gnat_temp,
2795 gnat_to_gnu_field_decl
2796 (Original_Record_Component (gnat_temp)), false);
2798 break;
2800 case E_Access_Subprogram_Type:
2801 case E_Anonymous_Access_Subprogram_Type:
2802 /* If we are not defining this entity, and we have incomplete
2803 entities being processed above us, make a dummy type and
2804 fill it in later. */
2805 if (!definition && defer_incomplete_level != 0)
2807 struct incomplete *p
2808 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2810 gnu_type
2811 = build_pointer_type
2812 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2813 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2814 !Comes_From_Source (gnat_entity),
2815 debug_info_p, gnat_entity);
2816 save_gnu_tree (gnat_entity, gnu_decl, false);
2817 this_made_decl = saved = true;
2819 p->old_type = TREE_TYPE (gnu_type);
2820 p->full_type = Directly_Designated_Type (gnat_entity);
2821 p->next = defer_incomplete_list;
2822 defer_incomplete_list = p;
2823 break;
2826 /* ... fall through ... */
2828 case E_Allocator_Type:
2829 case E_Access_Type:
2830 case E_Access_Attribute_Type:
2831 case E_Anonymous_Access_Type:
2832 case E_General_Access_Type:
2834 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
2835 Entity_Id gnat_desig_full
2836 = ((IN (Ekind (Etype (gnat_desig_type)),
2837 Incomplete_Or_Private_Kind))
2838 ? Full_View (gnat_desig_type) : 0);
2839 /* We want to know if we'll be seeing the freeze node for any
2840 incomplete type we may be pointing to. */
2841 bool in_main_unit
2842 = (Present (gnat_desig_full)
2843 ? In_Extended_Main_Code_Unit (gnat_desig_full)
2844 : In_Extended_Main_Code_Unit (gnat_desig_type));
2845 bool got_fat_p = false;
2846 bool made_dummy = false;
2847 tree gnu_desig_type = NULL_TREE;
2848 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
2850 if (!targetm.valid_pointer_mode (p_mode))
2851 p_mode = ptr_mode;
2853 if (No (gnat_desig_full)
2854 && (Ekind (gnat_desig_type) == E_Class_Wide_Type
2855 || (Ekind (gnat_desig_type) == E_Class_Wide_Subtype
2856 && Present (Equivalent_Type (gnat_desig_type)))))
2858 if (Present (Equivalent_Type (gnat_desig_type)))
2860 gnat_desig_full = Equivalent_Type (gnat_desig_type);
2861 if (IN (Ekind (gnat_desig_full), Incomplete_Or_Private_Kind))
2862 gnat_desig_full = Full_View (gnat_desig_full);
2864 else if (IN (Ekind (Root_Type (gnat_desig_type)),
2865 Incomplete_Or_Private_Kind))
2866 gnat_desig_full = Full_View (Root_Type (gnat_desig_type));
2869 if (Present (gnat_desig_full) && Is_Concurrent_Type (gnat_desig_full))
2870 gnat_desig_full = Corresponding_Record_Type (gnat_desig_full);
2872 /* If either the designated type or its full view is an
2873 unconstrained array subtype, replace it with the type it's a
2874 subtype of. This avoids problems with multiple copies of
2875 unconstrained array types. */
2876 if (Ekind (gnat_desig_type) == E_Array_Subtype
2877 && !Is_Constrained (gnat_desig_type))
2878 gnat_desig_type = Etype (gnat_desig_type);
2879 if (Present (gnat_desig_full)
2880 && Ekind (gnat_desig_full) == E_Array_Subtype
2881 && !Is_Constrained (gnat_desig_full))
2882 gnat_desig_full = Etype (gnat_desig_full);
2884 /* If the designated type is a subtype of an incomplete record type,
2885 use the parent type to avoid order of elaboration issues. This
2886 can lose some code efficiency, but there is no alternative. */
2887 if (Present (gnat_desig_full)
2888 && Ekind (gnat_desig_full) == E_Record_Subtype
2889 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)
2890 gnat_desig_full = Etype (gnat_desig_full);
2892 /* If we are pointing to an incomplete type whose completion is an
2893 unconstrained array, make a fat pointer type instead of a pointer
2894 to VOID. The two types in our fields will be pointers to VOID and
2895 will be replaced in update_pointer_to. Similarly, if the type
2896 itself is a dummy type or an unconstrained array. Also make
2897 a dummy TYPE_OBJECT_RECORD_TYPE in case we have any thin
2898 pointers to it. */
2900 if ((Present (gnat_desig_full)
2901 && Is_Array_Type (gnat_desig_full)
2902 && !Is_Constrained (gnat_desig_full))
2903 || (present_gnu_tree (gnat_desig_type)
2904 && TYPE_IS_DUMMY_P (TREE_TYPE
2905 (get_gnu_tree (gnat_desig_type)))
2906 && Is_Array_Type (gnat_desig_type)
2907 && !Is_Constrained (gnat_desig_type))
2908 || (present_gnu_tree (gnat_desig_type)
2909 && (TREE_CODE (TREE_TYPE (get_gnu_tree (gnat_desig_type)))
2910 == UNCONSTRAINED_ARRAY_TYPE)
2911 && !(TYPE_POINTER_TO (TREE_TYPE
2912 (get_gnu_tree (gnat_desig_type)))))
2913 || (No (gnat_desig_full) && !in_main_unit
2914 && defer_incomplete_level
2915 && !present_gnu_tree (gnat_desig_type)
2916 && Is_Array_Type (gnat_desig_type)
2917 && !Is_Constrained (gnat_desig_type)))
2919 tree gnu_old
2920 = (present_gnu_tree (gnat_desig_type)
2921 ? gnat_to_gnu_type (gnat_desig_type)
2922 : make_dummy_type (gnat_desig_type));
2923 tree fields;
2925 /* Show the dummy we get will be a fat pointer. */
2926 got_fat_p = made_dummy = true;
2928 /* If the call above got something that has a pointer, that
2929 pointer is our type. This could have happened either
2930 because the type was elaborated or because somebody
2931 else executed the code below. */
2932 gnu_type = TYPE_POINTER_TO (gnu_old);
2933 if (!gnu_type)
2935 gnu_type = make_node (RECORD_TYPE);
2936 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
2937 TYPE_POINTER_TO (gnu_old) = gnu_type;
2939 Sloc_to_locus (Sloc (gnat_entity), &input_location);
2940 fields
2941 = chainon (chainon (NULL_TREE,
2942 create_field_decl
2943 (get_identifier ("P_ARRAY"),
2944 ptr_void_type_node, gnu_type,
2945 0, 0, 0, 0)),
2946 create_field_decl (get_identifier ("P_BOUNDS"),
2947 ptr_void_type_node,
2948 gnu_type, 0, 0, 0, 0));
2950 /* Make sure we can place this into a register. */
2951 TYPE_ALIGN (gnu_type)
2952 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
2953 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
2954 finish_record_type (gnu_type, fields, false, true);
2956 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
2957 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
2958 = concat_id_with_name (get_entity_name (gnat_desig_type),
2959 "XUT");
2960 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
2964 /* If we already know what the full type is, use it. */
2965 else if (Present (gnat_desig_full)
2966 && present_gnu_tree (gnat_desig_full))
2967 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
2969 /* Get the type of the thing we are to point to and build a pointer
2970 to it. If it is a reference to an incomplete or private type with a
2971 full view that is a record, make a dummy type node and get the
2972 actual type later when we have verified it is safe. */
2973 else if (!in_main_unit
2974 && !present_gnu_tree (gnat_desig_type)
2975 && Present (gnat_desig_full)
2976 && !present_gnu_tree (gnat_desig_full)
2977 && Is_Record_Type (gnat_desig_full))
2979 gnu_desig_type = make_dummy_type (gnat_desig_type);
2980 made_dummy = true;
2983 /* Likewise if we are pointing to a record or array and we are to defer
2984 elaborating incomplete types. We do this since this access type
2985 may be the full view of some private type. Note that the
2986 unconstrained array case is handled above. */
2987 else if ((!in_main_unit || imported_p) && defer_incomplete_level != 0
2988 && !present_gnu_tree (gnat_desig_type)
2989 && ((Is_Record_Type (gnat_desig_type)
2990 || Is_Array_Type (gnat_desig_type))
2991 || (Present (gnat_desig_full)
2992 && (Is_Record_Type (gnat_desig_full)
2993 || Is_Array_Type (gnat_desig_full)))))
2995 gnu_desig_type = make_dummy_type (gnat_desig_type);
2996 made_dummy = true;
2998 else if (gnat_desig_type == gnat_entity)
3000 gnu_type
3001 = build_pointer_type_for_mode (make_node (VOID_TYPE),
3002 p_mode,
3003 No_Strict_Aliasing (gnat_entity));
3004 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3006 else
3007 gnu_desig_type = gnat_to_gnu_type (gnat_desig_type);
3009 /* It is possible that the above call to gnat_to_gnu_type resolved our
3010 type. If so, just return it. */
3011 if (present_gnu_tree (gnat_entity))
3013 maybe_present = true;
3014 break;
3017 /* If we have a GCC type for the designated type, possibly modify it
3018 if we are pointing only to constant objects and then make a pointer
3019 to it. Don't do this for unconstrained arrays. */
3020 if (!gnu_type && gnu_desig_type)
3022 if (Is_Access_Constant (gnat_entity)
3023 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3025 gnu_desig_type
3026 = build_qualified_type
3027 (gnu_desig_type,
3028 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3030 /* Some extra processing is required if we are building a
3031 pointer to an incomplete type (in the GCC sense). We might
3032 have such a type if we just made a dummy, or directly out
3033 of the call to gnat_to_gnu_type above if we are processing
3034 an access type for a record component designating the
3035 record type itself. */
3036 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3038 /* We must ensure that the pointer to variant we make will
3039 be processed by update_pointer_to when the initial type
3040 is completed. Pretend we made a dummy and let further
3041 processing act as usual. */
3042 made_dummy = true;
3044 /* We must ensure that update_pointer_to will not retrieve
3045 the dummy variant when building a properly qualified
3046 version of the complete type. We take advantage of the
3047 fact that get_qualified_type is requiring TYPE_NAMEs to
3048 match to influence build_qualified_type and then also
3049 update_pointer_to here. */
3050 TYPE_NAME (gnu_desig_type)
3051 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3055 gnu_type
3056 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3057 No_Strict_Aliasing (gnat_entity));
3060 /* If we are not defining this object and we made a dummy pointer,
3061 save our current definition, evaluate the actual type, and replace
3062 the tentative type we made with the actual one. If we are to defer
3063 actually looking up the actual type, make an entry in the
3064 deferred list. */
3066 if (!in_main_unit && made_dummy)
3068 tree gnu_old_type
3069 = TYPE_FAT_POINTER_P (gnu_type)
3070 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3072 if (esize == POINTER_SIZE
3073 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
3074 gnu_type
3075 = build_pointer_type
3076 (TYPE_OBJECT_RECORD_TYPE
3077 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3079 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3080 !Comes_From_Source (gnat_entity),
3081 debug_info_p, gnat_entity);
3082 save_gnu_tree (gnat_entity, gnu_decl, false);
3083 this_made_decl = saved = true;
3085 if (defer_incomplete_level == 0)
3086 /* Note that the call to gnat_to_gnu_type here might have
3087 updated gnu_old_type directly, in which case it is not a
3088 dummy type any more when we get into update_pointer_to.
3090 This may happen for instance when the designated type is a
3091 record type, because their elaboration starts with an
3092 initial node from make_dummy_type, which may yield the same
3093 node as the one we got.
3095 Besides, variants of this non-dummy type might have been
3096 created along the way. update_pointer_to is expected to
3097 properly take care of those situations. */
3098 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
3099 gnat_to_gnu_type (gnat_desig_type));
3100 else
3102 struct incomplete *p
3103 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3105 p->old_type = gnu_old_type;
3106 p->full_type = gnat_desig_type;
3107 p->next = defer_incomplete_list;
3108 defer_incomplete_list = p;
3112 break;
3114 case E_Access_Protected_Subprogram_Type:
3115 case E_Anonymous_Access_Protected_Subprogram_Type:
3116 if (type_annotate_only && No (Equivalent_Type (gnat_entity)))
3117 gnu_type = build_pointer_type (void_type_node);
3118 else
3119 /* The runtime representation is the equivalent type. */
3120 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3122 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3123 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3124 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3125 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3126 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3127 NULL_TREE, 0);
3129 break;
3131 case E_Access_Subtype:
3133 /* We treat this as identical to its base type; any constraint is
3134 meaningful only to the front end.
3136 The designated type must be elaborated as well, if it does
3137 not have its own freeze node. Designated (sub)types created
3138 for constrained components of records with discriminants are
3139 not frozen by the front end and thus not elaborated by gigi,
3140 because their use may appear before the base type is frozen,
3141 and because it is not clear that they are needed anywhere in
3142 Gigi. With the current model, there is no correct place where
3143 they could be elaborated. */
3145 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3146 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3147 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3148 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3149 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3151 /* If we are not defining this entity, and we have incomplete
3152 entities being processed above us, make a dummy type and
3153 elaborate it later. */
3154 if (!definition && defer_incomplete_level != 0)
3156 struct incomplete *p
3157 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3158 tree gnu_ptr_type
3159 = build_pointer_type
3160 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3162 p->old_type = TREE_TYPE (gnu_ptr_type);
3163 p->full_type = Directly_Designated_Type (gnat_entity);
3164 p->next = defer_incomplete_list;
3165 defer_incomplete_list = p;
3167 else if (IN (Ekind (Base_Type
3168 (Directly_Designated_Type (gnat_entity))),
3169 Incomplete_Or_Private_Kind))
3171 else
3172 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3173 NULL_TREE, 0);
3176 maybe_present = true;
3177 break;
3179 /* Subprogram Entities
3181 The following access functions are defined for subprograms (functions
3182 or procedures):
3184 First_Formal The first formal parameter.
3185 Is_Imported Indicates that the subprogram has appeared in
3186 an INTERFACE or IMPORT pragma. For now we
3187 assume that the external language is C.
3188 Is_Inlined True if the subprogram is to be inlined.
3190 In addition for function subprograms we have:
3192 Etype Return type of the function.
3194 Each parameter is first checked by calling must_pass_by_ref on its
3195 type to determine if it is passed by reference. For parameters which
3196 are copied in, if they are Ada IN OUT or OUT parameters, their return
3197 value becomes part of a record which becomes the return type of the
3198 function (C function - note that this applies only to Ada procedures
3199 so there is no Ada return type). Additional code to store back the
3200 parameters will be generated on the caller side. This transformation
3201 is done here, not in the front-end.
3203 The intended result of the transformation can be seen from the
3204 equivalent source rewritings that follow:
3206 struct temp {int a,b};
3207 procedure P (A,B: IN OUT ...) is temp P (int A,B) {
3208 .. ..
3209 end P; return {A,B};
3211 procedure call
3214 temp t;
3215 P(X,Y); t = P(X,Y);
3216 X = t.a , Y = t.b;
3219 For subprogram types we need to perform mainly the same conversions to
3220 GCC form that are needed for procedures and function declarations. The
3221 only difference is that at the end, we make a type declaration instead
3222 of a function declaration. */
3224 case E_Subprogram_Type:
3225 case E_Function:
3226 case E_Procedure:
3228 /* The first GCC parameter declaration (a PARM_DECL node). The
3229 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3230 actually is the head of this parameter list. */
3231 tree gnu_param_list = NULL_TREE;
3232 /* The type returned by a function. If the subprogram is a procedure
3233 this type should be void_type_node. */
3234 tree gnu_return_type = void_type_node;
3235 /* List of fields in return type of procedure with copy in copy out
3236 parameters. */
3237 tree gnu_field_list = NULL_TREE;
3238 /* Non-null for subprograms containing parameters passed by copy in
3239 copy out (Ada IN OUT or OUT parameters not passed by reference),
3240 in which case it is the list of nodes used to specify the values of
3241 the in out/out parameters that are returned as a record upon
3242 procedure return. The TREE_PURPOSE of an element of this list is
3243 a field of the record and the TREE_VALUE is the PARM_DECL
3244 corresponding to that field. This list will be saved in the
3245 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3246 tree gnu_return_list = NULL_TREE;
3247 /* If an import pragma asks to map this subprogram to a GCC builtin,
3248 this is the builtin DECL node. */
3249 tree gnu_builtin_decl = NULL_TREE;
3250 Entity_Id gnat_param;
3251 bool inline_flag = Is_Inlined (gnat_entity);
3252 bool public_flag = Is_Public (gnat_entity);
3253 bool extern_flag
3254 = (Is_Public (gnat_entity) && !definition) || imported_p;
3255 bool pure_flag = Is_Pure (gnat_entity);
3256 bool volatile_flag = No_Return (gnat_entity);
3257 bool returns_by_ref = false;
3258 bool returns_unconstrained = false;
3259 bool returns_by_target_ptr = false;
3260 tree gnu_ext_name = create_concat_name (gnat_entity, 0);
3261 bool has_copy_in_out = false;
3262 int parmnum;
3264 if (kind == E_Subprogram_Type && !definition)
3265 /* A parameter may refer to this type, so defer completion
3266 of any incomplete types. */
3267 defer_incomplete_level++, this_deferred = true;
3269 /* If the subprogram has an alias, it is probably inherited, so
3270 we can use the original one. If the original "subprogram"
3271 is actually an enumeration literal, it may be the first use
3272 of its type, so we must elaborate that type now. */
3273 if (Present (Alias (gnat_entity)))
3275 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3276 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3278 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3279 gnu_expr, 0);
3281 /* Elaborate any Itypes in the parameters of this entity. */
3282 for (gnat_temp = First_Formal (gnat_entity);
3283 Present (gnat_temp);
3284 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3285 if (Is_Itype (Etype (gnat_temp)))
3286 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3288 break;
3291 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3292 corresponding DECL node.
3294 We still want the parameter associations to take place because the
3295 proper generation of calls depends on it (a GNAT parameter without
3296 a corresponding GCC tree has a very specific meaning), so we don't
3297 just break here. */
3298 if (Convention (gnat_entity) == Convention_Intrinsic)
3299 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3301 /* ??? What if we don't find the builtin node above ? warn ? err ?
3302 In the current state we neither warn nor err, and calls will just
3303 be handled as for regular subprograms. */
3305 if (kind == E_Function || kind == E_Subprogram_Type)
3306 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3308 /* If this function returns by reference, make the actual
3309 return type of this function the pointer and mark the decl. */
3310 if (Returns_By_Ref (gnat_entity))
3312 returns_by_ref = true;
3313 gnu_return_type = build_pointer_type (gnu_return_type);
3316 /* If the Mechanism is By_Reference, ensure the return type uses
3317 the machine's by-reference mechanism, which may not the same
3318 as above (e.g., it might be by passing a fake parameter). */
3319 else if (kind == E_Function
3320 && Mechanism (gnat_entity) == By_Reference)
3322 gnu_return_type = copy_type (gnu_return_type);
3323 TREE_ADDRESSABLE (gnu_return_type) = 1;
3326 /* If we are supposed to return an unconstrained array,
3327 actually return a fat pointer and make a note of that. Return
3328 a pointer to an unconstrained record of variable size. */
3329 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3331 gnu_return_type = TREE_TYPE (gnu_return_type);
3332 returns_unconstrained = true;
3335 /* If the type requires a transient scope, the result is allocated
3336 on the secondary stack, so the result type of the function is
3337 just a pointer. */
3338 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3340 gnu_return_type = build_pointer_type (gnu_return_type);
3341 returns_unconstrained = true;
3344 /* If the type is a padded type and the underlying type would not
3345 be passed by reference or this function has a foreign convention,
3346 return the underlying type. */
3347 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3348 && TYPE_IS_PADDING_P (gnu_return_type)
3349 && (!default_pass_by_ref (TREE_TYPE
3350 (TYPE_FIELDS (gnu_return_type)))
3351 || Has_Foreign_Convention (gnat_entity)))
3352 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3354 /* If the return type is unconstrained, that means it must have a
3355 maximum size. We convert the function into a procedure and its
3356 caller will pass a pointer to an object of that maximum size as the
3357 first parameter when we call the function. */
3358 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
3360 returns_by_target_ptr = true;
3361 gnu_param_list
3362 = create_param_decl (get_identifier ("TARGET"),
3363 build_reference_type (gnu_return_type),
3364 true);
3365 gnu_return_type = void_type_node;
3368 /* If the return type has a size that overflows, we cannot have
3369 a function that returns that type. This usage doesn't make
3370 sense anyway, so give an error here. */
3371 if (TYPE_SIZE_UNIT (gnu_return_type)
3372 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
3374 post_error ("cannot return type whose size overflows",
3375 gnat_entity);
3376 gnu_return_type = copy_node (gnu_return_type);
3377 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
3378 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
3379 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
3380 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
3383 /* Look at all our parameters and get the type of
3384 each. While doing this, build a copy-out structure if
3385 we need one. */
3387 for (gnat_param = First_Formal (gnat_entity), parmnum = 0;
3388 Present (gnat_param);
3389 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3391 tree gnu_param_name = get_entity_name (gnat_param);
3392 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3393 tree gnu_param, gnu_field;
3394 bool by_ref_p = false;
3395 bool by_descr_p = false;
3396 bool by_component_ptr_p = false;
3397 bool copy_in_copy_out_flag = false;
3398 bool req_by_copy = false, req_by_ref = false;
3400 /* Builtins are expanded inline and there is no real call sequence
3401 involved. so the type expected by the underlying expander is
3402 always the type of each argument "as is". */
3403 if (gnu_builtin_decl)
3404 req_by_copy = 1;
3406 /* Otherwise, see if a Mechanism was supplied that forced this
3407 parameter to be passed one way or another. */
3408 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3409 req_by_copy = true;
3410 else if (Mechanism (gnat_param) == Default)
3412 else if (Mechanism (gnat_param) == By_Copy)
3413 req_by_copy = true;
3414 else if (Mechanism (gnat_param) == By_Reference)
3415 req_by_ref = true;
3416 else if (Mechanism (gnat_param) <= By_Descriptor)
3417 by_descr_p = true;
3418 else if (Mechanism (gnat_param) > 0)
3420 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3421 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3422 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3423 Mechanism (gnat_param)))
3424 req_by_ref = true;
3425 else
3426 req_by_copy = true;
3428 else
3429 post_error ("unsupported mechanism for&", gnat_param);
3431 /* If this is either a foreign function or if the
3432 underlying type won't be passed by reference, strip off
3433 possible padding type. */
3434 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
3435 && TYPE_IS_PADDING_P (gnu_param_type)
3436 && (req_by_ref || Has_Foreign_Convention (gnat_entity)
3437 || (!must_pass_by_ref (TREE_TYPE (TYPE_FIELDS
3438 (gnu_param_type)))
3439 && (req_by_copy
3440 || !default_pass_by_ref (TREE_TYPE
3441 (TYPE_FIELDS
3442 (gnu_param_type)))))))
3443 gnu_param_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
3445 /* If this is an IN parameter it is read-only, so make a variant
3446 of the type that is read-only.
3448 ??? However, if this is an unconstrained array, that type can
3449 be very complex. So skip it for now. Likewise for any other
3450 self-referential type. */
3451 if (Ekind (gnat_param) == E_In_Parameter
3452 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
3453 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
3454 gnu_param_type
3455 = build_qualified_type (gnu_param_type,
3456 (TYPE_QUALS (gnu_param_type)
3457 | TYPE_QUAL_CONST));
3459 /* For foreign conventions, pass arrays as a pointer to the
3460 underlying type. First check for unconstrained array and get
3461 the underlying array. Then get the component type and build
3462 a pointer to it. */
3463 if (Has_Foreign_Convention (gnat_entity)
3464 && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
3465 gnu_param_type
3466 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3467 (TREE_TYPE (gnu_param_type))));
3469 if (by_descr_p)
3470 gnu_param_type
3471 = build_pointer_type
3472 (build_vms_descriptor (gnu_param_type,
3473 Mechanism (gnat_param), gnat_entity));
3475 else if (Has_Foreign_Convention (gnat_entity)
3476 && !req_by_copy
3477 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
3479 /* Strip off any multi-dimensional entries, then strip
3480 off the last array to get the component type. */
3481 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
3482 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
3483 gnu_param_type = TREE_TYPE (gnu_param_type);
3485 by_component_ptr_p = true;
3486 gnu_param_type = TREE_TYPE (gnu_param_type);
3488 if (Ekind (gnat_param) == E_In_Parameter)
3489 gnu_param_type
3490 = build_qualified_type (gnu_param_type,
3491 (TYPE_QUALS (gnu_param_type)
3492 | TYPE_QUAL_CONST));
3494 gnu_param_type = build_pointer_type (gnu_param_type);
3497 /* Fat pointers are passed as thin pointers for foreign
3498 conventions. */
3499 else if (Has_Foreign_Convention (gnat_entity)
3500 && TYPE_FAT_POINTER_P (gnu_param_type))
3501 gnu_param_type
3502 = make_type_from_size (gnu_param_type,
3503 size_int (POINTER_SIZE), false);
3505 /* If we must pass or were requested to pass by reference, do so.
3506 If we were requested to pass by copy, do so.
3507 Otherwise, for foreign conventions, pass all in out parameters
3508 or aggregates by reference. For COBOL and Fortran, pass
3509 all integer and FP types that way too. For Convention Ada,
3510 use the standard Ada default. */
3511 else if (must_pass_by_ref (gnu_param_type) || req_by_ref
3512 || (!req_by_copy
3513 && ((Has_Foreign_Convention (gnat_entity)
3514 && (Ekind (gnat_param) != E_In_Parameter
3515 || AGGREGATE_TYPE_P (gnu_param_type)))
3516 || (((Convention (gnat_entity)
3517 == Convention_Fortran)
3518 || (Convention (gnat_entity)
3519 == Convention_COBOL))
3520 && (INTEGRAL_TYPE_P (gnu_param_type)
3521 || FLOAT_TYPE_P (gnu_param_type)))
3522 /* For convention Ada, see if we pass by reference
3523 by default. */
3524 || (!Has_Foreign_Convention (gnat_entity)
3525 && default_pass_by_ref (gnu_param_type)))))
3527 gnu_param_type = build_reference_type (gnu_param_type);
3528 by_ref_p = true;
3531 else if (Ekind (gnat_param) != E_In_Parameter)
3532 copy_in_copy_out_flag = true;
3534 if (req_by_copy && (by_ref_p || by_component_ptr_p))
3535 post_error ("?cannot pass & by copy", gnat_param);
3537 /* If this is an OUT parameter that isn't passed by reference
3538 and isn't a pointer or aggregate, we don't make a PARM_DECL
3539 for it. Instead, it will be a VAR_DECL created when we process
3540 the procedure. For the special parameter of Valued_Procedure,
3541 never pass it in.
3543 An exception is made to cover the RM-6.4.1 rule requiring "by
3544 copy" out parameters with discriminants or implicit initial
3545 values to be handled like in out parameters. These type are
3546 normally built as aggregates, and hence passed by reference,
3547 except for some packed arrays which end up encoded in special
3548 integer types.
3550 The exception we need to make is then for packed arrays of
3551 records with discriminants or implicit initial values. We have
3552 no light/easy way to check for the latter case, so we merely
3553 check for packed arrays of records. This may lead to useless
3554 copy-in operations, but in very rare cases only, as these would
3555 be exceptions in a set of already exceptional situations. */
3556 if (Ekind (gnat_param) == E_Out_Parameter && !by_ref_p
3557 && ((Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3558 || (!by_descr_p
3559 && !POINTER_TYPE_P (gnu_param_type)
3560 && !AGGREGATE_TYPE_P (gnu_param_type)))
3561 && !(Is_Array_Type (Etype (gnat_param))
3562 && Is_Packed (Etype (gnat_param))
3563 && Is_Composite_Type (Component_Type
3564 (Etype (gnat_param)))))
3565 gnu_param = NULL_TREE;
3566 else
3568 gnu_param
3569 = create_param_decl
3570 (gnu_param_name, gnu_param_type,
3571 by_ref_p || by_component_ptr_p
3572 || Ekind (gnat_param) == E_In_Parameter);
3574 DECL_BY_REF_P (gnu_param) = by_ref_p;
3575 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr_p;
3576 DECL_BY_DESCRIPTOR_P (gnu_param) = by_descr_p;
3577 DECL_POINTS_TO_READONLY_P (gnu_param)
3578 = (Ekind (gnat_param) == E_In_Parameter
3579 && (by_ref_p || by_component_ptr_p));
3580 Sloc_to_locus (Sloc (gnat_param),
3581 &DECL_SOURCE_LOCATION (gnu_param));
3582 save_gnu_tree (gnat_param, gnu_param, false);
3583 gnu_param_list = chainon (gnu_param, gnu_param_list);
3585 /* If a parameter is a pointer, this function may modify
3586 memory through it and thus shouldn't be considered
3587 a pure function. Also, the memory may be modified
3588 between two calls, so they can't be CSE'ed. The latter
3589 case also handles by-ref parameters. */
3590 if (POINTER_TYPE_P (gnu_param_type)
3591 || TYPE_FAT_POINTER_P (gnu_param_type))
3592 pure_flag = false;
3595 if (copy_in_copy_out_flag)
3597 if (!has_copy_in_out)
3599 gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
3600 gnu_return_type = make_node (RECORD_TYPE);
3601 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3602 has_copy_in_out = true;
3605 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3606 gnu_return_type, 0, 0, 0, 0);
3607 Sloc_to_locus (Sloc (gnat_param),
3608 &DECL_SOURCE_LOCATION (gnu_field));
3609 TREE_CHAIN (gnu_field) = gnu_field_list;
3610 gnu_field_list = gnu_field;
3611 gnu_return_list = tree_cons (gnu_field, gnu_param,
3612 gnu_return_list);
3616 /* Do not compute record for out parameters if subprogram is
3617 stubbed since structures are incomplete for the back-end. */
3618 if (gnu_field_list
3619 && Convention (gnat_entity) != Convention_Stubbed)
3621 /* If all types are not complete, defer emission of debug
3622 information for this record types. Otherwise, we risk emitting
3623 debug information for a dummy type contained in the fields
3624 for that record. */
3625 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3626 false, defer_incomplete_level);
3628 if (defer_incomplete_level)
3630 debug_deferred = true;
3631 defer_debug_level++;
3633 defer_debug_incomplete_list
3634 = tree_cons (NULL_TREE, gnu_return_type,
3635 defer_debug_incomplete_list);
3639 /* If we have a CICO list but it has only one entry, we convert
3640 this function into a function that simply returns that one
3641 object. */
3642 if (list_length (gnu_return_list) == 1)
3643 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3645 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
3646 if (Convention (gnat_entity) == Convention_Stdcall)
3648 struct attrib *attr
3649 = (struct attrib *) xmalloc (sizeof (struct attrib));
3651 attr->next = attr_list;
3652 attr->type = ATTR_MACHINE_ATTRIBUTE;
3653 attr->name = get_identifier ("stdcall");
3654 attr->args = NULL_TREE;
3655 attr->error_point = gnat_entity;
3656 attr_list = attr;
3658 #endif
3660 /* Both lists ware built in reverse. */
3661 gnu_param_list = nreverse (gnu_param_list);
3662 gnu_return_list = nreverse (gnu_return_list);
3664 gnu_type
3665 = create_subprog_type (gnu_return_type, gnu_param_list,
3666 gnu_return_list, returns_unconstrained,
3667 returns_by_ref,
3668 Function_Returns_With_DSP (gnat_entity),
3669 returns_by_target_ptr);
3671 /* A subprogram (something that doesn't return anything) shouldn't
3672 be considered Pure since there would be no reason for such a
3673 subprogram. Note that procedures with Out (or In Out) parameters
3674 have already been converted into a function with a return type. */
3675 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
3676 pure_flag = false;
3678 gnu_type
3679 = build_qualified_type (gnu_type,
3680 (TYPE_QUALS (gnu_type)
3681 | (TYPE_QUAL_CONST * pure_flag)
3682 | (TYPE_QUAL_VOLATILE * volatile_flag)));
3684 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3686 /* If we have a builtin decl for that function, check the signatures
3687 compatibilities. If the signatures are compatible, use the builtin
3688 decl. If they are not, we expect the checker predicate to have
3689 posted the appropriate errors, and just continue with what we have
3690 so far. */
3691 if (gnu_builtin_decl)
3693 tree gnu_builtin_type = TREE_TYPE (gnu_builtin_decl);
3695 if (compatible_signatures_p (gnu_type, gnu_builtin_type))
3697 gnu_decl = gnu_builtin_decl;
3698 gnu_type = gnu_builtin_type;
3699 break;
3703 /* If there was no specified Interface_Name and the external and
3704 internal names of the subprogram are the same, only use the
3705 internal name to allow disambiguation of nested subprograms. */
3706 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_id)
3707 gnu_ext_name = NULL_TREE;
3709 /* If we are defining the subprogram and it has an Address clause
3710 we must get the address expression from the saved GCC tree for the
3711 subprogram if it has a Freeze_Node. Otherwise, we elaborate
3712 the address expression here since the front-end has guaranteed
3713 in that case that the elaboration has no effects. If there is
3714 an Address clause and we are not defining the object, just
3715 make it a constant. */
3716 if (Present (Address_Clause (gnat_entity)))
3718 tree gnu_address = NULL_TREE;
3720 if (definition)
3721 gnu_address
3722 = (present_gnu_tree (gnat_entity)
3723 ? get_gnu_tree (gnat_entity)
3724 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
3726 save_gnu_tree (gnat_entity, NULL_TREE, false);
3728 gnu_type = build_reference_type (gnu_type);
3729 if (gnu_address)
3730 gnu_address = convert (gnu_type, gnu_address);
3732 gnu_decl
3733 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
3734 gnu_address, false, Is_Public (gnat_entity),
3735 extern_flag, false, NULL, gnat_entity);
3736 DECL_BY_REF_P (gnu_decl) = 1;
3739 else if (kind == E_Subprogram_Type)
3740 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3741 !Comes_From_Source (gnat_entity),
3742 debug_info_p && !defer_incomplete_level,
3743 gnat_entity);
3744 else
3746 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
3747 gnu_type, gnu_param_list,
3748 inline_flag, public_flag,
3749 extern_flag, attr_list,
3750 gnat_entity);
3751 DECL_STUBBED_P (gnu_decl)
3752 = Convention (gnat_entity) == Convention_Stubbed;
3755 break;
3757 case E_Incomplete_Type:
3758 case E_Private_Type:
3759 case E_Limited_Private_Type:
3760 case E_Record_Type_With_Private:
3761 case E_Private_Subtype:
3762 case E_Limited_Private_Subtype:
3763 case E_Record_Subtype_With_Private:
3765 /* If this type does not have a full view in the unit we are
3766 compiling, then just get the type from its Etype. */
3767 if (No (Full_View (gnat_entity)))
3769 /* If this is an incomplete type with no full view, it must
3770 be a Taft Amendement type, so just return a dummy type. */
3771 if (kind == E_Incomplete_Type)
3772 gnu_type = make_dummy_type (gnat_entity);
3774 else if (Present (Underlying_Full_View (gnat_entity)))
3775 gnu_decl = gnat_to_gnu_entity (Underlying_Full_View (gnat_entity),
3776 NULL_TREE, 0);
3777 else
3779 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
3780 NULL_TREE, 0);
3781 maybe_present = true;
3784 break;
3787 /* Otherwise, if we are not defining the type now, get the
3788 type from the full view. But always get the type from the full
3789 view for define on use types, since otherwise we won't see them! */
3791 else if (!definition
3792 || (Is_Itype (Full_View (gnat_entity))
3793 && No (Freeze_Node (gnat_entity)))
3794 || (Is_Itype (gnat_entity)
3795 && No (Freeze_Node (Full_View (gnat_entity)))))
3797 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
3798 NULL_TREE, 0);
3799 maybe_present = true;
3800 break;
3803 /* For incomplete types, make a dummy type entry which will be
3804 replaced later. */
3805 gnu_type = make_dummy_type (gnat_entity);
3807 /* Save this type as the full declaration's type so we can do any needed
3808 updates when we see it. */
3809 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3810 !Comes_From_Source (gnat_entity),
3811 debug_info_p, gnat_entity);
3812 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
3813 break;
3815 /* Simple class_wide types are always viewed as their root_type
3816 by Gigi unless an Equivalent_Type is specified. */
3817 case E_Class_Wide_Type:
3818 if (Present (Equivalent_Type (gnat_entity)))
3819 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3820 else
3821 gnu_type = gnat_to_gnu_type (Root_Type (gnat_entity));
3823 maybe_present = true;
3824 break;
3826 case E_Task_Type:
3827 case E_Task_Subtype:
3828 case E_Protected_Type:
3829 case E_Protected_Subtype:
3830 if (type_annotate_only && No (Corresponding_Record_Type (gnat_entity)))
3831 gnu_type = void_type_node;
3832 else
3833 gnu_type = gnat_to_gnu_type (Corresponding_Record_Type (gnat_entity));
3835 maybe_present = true;
3836 break;
3838 case E_Label:
3839 gnu_decl = create_label_decl (gnu_entity_id);
3840 break;
3842 case E_Block:
3843 case E_Loop:
3844 /* Nothing at all to do here, so just return an ERROR_MARK and claim
3845 we've already saved it, so we don't try to. */
3846 gnu_decl = error_mark_node;
3847 saved = true;
3848 break;
3850 default:
3851 gcc_unreachable ();
3854 /* If we had a case where we evaluated another type and it might have
3855 defined this one, handle it here. */
3856 if (maybe_present && present_gnu_tree (gnat_entity))
3858 gnu_decl = get_gnu_tree (gnat_entity);
3859 saved = true;
3862 /* If we are processing a type and there is either no decl for it or
3863 we just made one, do some common processing for the type, such as
3864 handling alignment and possible padding. */
3866 if ((!gnu_decl || this_made_decl) && IN (kind, Type_Kind))
3868 if (Is_Tagged_Type (gnat_entity)
3869 || Is_Class_Wide_Equivalent_Type (gnat_entity))
3870 TYPE_ALIGN_OK (gnu_type) = 1;
3872 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
3873 TYPE_BY_REFERENCE_P (gnu_type) = 1;
3875 /* ??? Don't set the size for a String_Literal since it is either
3876 confirming or we don't handle it properly (if the low bound is
3877 non-constant). */
3878 if (!gnu_size && kind != E_String_Literal_Subtype)
3879 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
3880 TYPE_DECL, false,
3881 Has_Size_Clause (gnat_entity));
3883 /* If a size was specified, see if we can make a new type of that size
3884 by rearranging the type, for example from a fat to a thin pointer. */
3885 if (gnu_size)
3887 gnu_type
3888 = make_type_from_size (gnu_type, gnu_size,
3889 Has_Biased_Representation (gnat_entity));
3891 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
3892 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
3893 gnu_size = 0;
3896 /* If the alignment hasn't already been processed and this is
3897 not an unconstrained array, see if an alignment is specified.
3898 If not, we pick a default alignment for atomic objects. */
3899 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
3901 else if (Known_Alignment (gnat_entity))
3902 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
3903 TYPE_ALIGN (gnu_type));
3904 else if (Is_Atomic (gnat_entity) && !gnu_size
3905 && host_integerp (TYPE_SIZE (gnu_type), 1)
3906 && integer_pow2p (TYPE_SIZE (gnu_type)))
3907 align = MIN (BIGGEST_ALIGNMENT,
3908 tree_low_cst (TYPE_SIZE (gnu_type), 1));
3909 else if (Is_Atomic (gnat_entity) && gnu_size
3910 && host_integerp (gnu_size, 1)
3911 && integer_pow2p (gnu_size))
3912 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
3914 /* See if we need to pad the type. If we did, and made a record,
3915 the name of the new type may be changed. So get it back for
3916 us when we make the new TYPE_DECL below. */
3917 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity, "PAD",
3918 true, definition, false);
3919 if (TREE_CODE (gnu_type) == RECORD_TYPE
3920 && TYPE_IS_PADDING_P (gnu_type))
3922 gnu_entity_id = TYPE_NAME (gnu_type);
3923 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
3924 gnu_entity_id = DECL_NAME (gnu_entity_id);
3927 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
3929 /* If we are at global level, GCC will have applied variable_size to
3930 the type, but that won't have done anything. So, if it's not
3931 a constant or self-referential, call elaborate_expression_1 to
3932 make a variable for the size rather than calculating it each time.
3933 Handle both the RM size and the actual size. */
3934 if (global_bindings_p ()
3935 && TYPE_SIZE (gnu_type)
3936 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
3937 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3939 if (TREE_CODE (gnu_type) == RECORD_TYPE
3940 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
3941 TYPE_SIZE (gnu_type), 0))
3943 TYPE_SIZE (gnu_type)
3944 = elaborate_expression_1 (gnat_entity, gnat_entity,
3945 TYPE_SIZE (gnu_type),
3946 get_identifier ("SIZE"),
3947 definition, 0);
3948 SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
3950 else
3952 TYPE_SIZE (gnu_type)
3953 = elaborate_expression_1 (gnat_entity, gnat_entity,
3954 TYPE_SIZE (gnu_type),
3955 get_identifier ("SIZE"),
3956 definition, 0);
3958 /* ??? For now, store the size as a multiple of the alignment
3959 in bytes so that we can see the alignment from the tree. */
3960 TYPE_SIZE_UNIT (gnu_type)
3961 = build_binary_op
3962 (MULT_EXPR, sizetype,
3963 elaborate_expression_1
3964 (gnat_entity, gnat_entity,
3965 build_binary_op (EXACT_DIV_EXPR, sizetype,
3966 TYPE_SIZE_UNIT (gnu_type),
3967 size_int (TYPE_ALIGN (gnu_type)
3968 / BITS_PER_UNIT)),
3969 get_identifier ("SIZE_A_UNIT"),
3970 definition, 0),
3971 size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
3973 if (TREE_CODE (gnu_type) == RECORD_TYPE)
3974 SET_TYPE_ADA_SIZE
3975 (gnu_type,
3976 elaborate_expression_1 (gnat_entity,
3977 gnat_entity,
3978 TYPE_ADA_SIZE (gnu_type),
3979 get_identifier ("RM_SIZE"),
3980 definition, 0));
3984 /* If this is a record type or subtype, call elaborate_expression_1 on
3985 any field position. Do this for both global and local types.
3986 Skip any fields that we haven't made trees for to avoid problems with
3987 class wide types. */
3988 if (IN (kind, Record_Kind))
3989 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3990 gnat_temp = Next_Entity (gnat_temp))
3991 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
3993 tree gnu_field = get_gnu_tree (gnat_temp);
3995 /* ??? Unfortunately, GCC needs to be able to prove the
3996 alignment of this offset and if it's a variable, it can't.
3997 In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
3998 right now, we have to put in an explicit multiply and
3999 divide by that value. */
4000 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4001 DECL_FIELD_OFFSET (gnu_field)
4002 = build_binary_op
4003 (MULT_EXPR, sizetype,
4004 elaborate_expression_1
4005 (gnat_temp, gnat_temp,
4006 build_binary_op (EXACT_DIV_EXPR, sizetype,
4007 DECL_FIELD_OFFSET (gnu_field),
4008 size_int (DECL_OFFSET_ALIGN (gnu_field)
4009 / BITS_PER_UNIT)),
4010 get_identifier ("OFFSET"),
4011 definition, 0),
4012 size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
4015 gnu_type = build_qualified_type (gnu_type,
4016 (TYPE_QUALS (gnu_type)
4017 | (TYPE_QUAL_VOLATILE
4018 * Treat_As_Volatile (gnat_entity))));
4020 if (Is_Atomic (gnat_entity))
4021 check_ok_for_atomic (gnu_type, gnat_entity, false);
4023 if (Known_Alignment (gnat_entity))
4024 TYPE_USER_ALIGN (gnu_type) = 1;
4026 if (!gnu_decl)
4027 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4028 !Comes_From_Source (gnat_entity),
4029 debug_info_p, gnat_entity);
4030 else
4031 TREE_TYPE (gnu_decl) = gnu_type;
4034 if (IN (kind, Type_Kind) && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4036 gnu_type = TREE_TYPE (gnu_decl);
4038 /* Back-annotate the Alignment of the type if not already in the
4039 tree. Likewise for sizes. */
4040 if (Unknown_Alignment (gnat_entity))
4041 Set_Alignment (gnat_entity,
4042 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4044 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4046 /* If the size is self-referential, we annotate the maximum
4047 value of that size. */
4048 tree gnu_size = TYPE_SIZE (gnu_type);
4050 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4051 gnu_size = max_size (gnu_size, true);
4053 Set_Esize (gnat_entity, annotate_value (gnu_size));
4055 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4057 /* In this mode the tag and the parent components are not
4058 generated by the front-end, so the sizes must be adjusted
4059 explicitly now. */
4061 int size_offset;
4062 int new_size;
4064 if (Is_Derived_Type (gnat_entity))
4066 size_offset
4067 = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
4068 Set_Alignment (gnat_entity,
4069 Alignment (Etype (Base_Type (gnat_entity))));
4071 else
4072 size_offset = POINTER_SIZE;
4074 new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
4075 Set_Esize (gnat_entity,
4076 UI_From_Int (((new_size + (POINTER_SIZE - 1))
4077 / POINTER_SIZE) * POINTER_SIZE));
4078 Set_RM_Size (gnat_entity, Esize (gnat_entity));
4082 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4083 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4086 if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4087 DECL_ARTIFICIAL (gnu_decl) = 1;
4089 if (!debug_info_p && DECL_P (gnu_decl)
4090 && TREE_CODE (gnu_decl) != FUNCTION_DECL)
4091 DECL_IGNORED_P (gnu_decl) = 1;
4093 /* If we haven't already, associate the ..._DECL node that we just made with
4094 the input GNAT entity node. */
4095 if (!saved)
4096 save_gnu_tree (gnat_entity, gnu_decl, false);
4098 /* If this is an enumeral or floating-point type, we were not able to set
4099 the bounds since they refer to the type. These bounds are always static.
4101 For enumeration types, also write debugging information and declare the
4102 enumeration literal table, if needed. */
4104 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4105 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4107 tree gnu_scalar_type = gnu_type;
4109 /* If this is a padded type, we need to use the underlying type. */
4110 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
4111 && TYPE_IS_PADDING_P (gnu_scalar_type))
4112 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4114 /* If this is a floating point type and we haven't set a floating
4115 point type yet, use this in the evaluation of the bounds. */
4116 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4117 longest_float_type_node = gnu_type;
4119 TYPE_MIN_VALUE (gnu_scalar_type)
4120 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4121 TYPE_MAX_VALUE (gnu_scalar_type)
4122 = gnat_to_gnu (Type_High_Bound (gnat_entity));
4124 if (TREE_CODE (gnu_scalar_type) == ENUMERAL_TYPE)
4126 TYPE_STUB_DECL (gnu_scalar_type) = gnu_decl;
4128 /* Since this has both a typedef and a tag, avoid outputting
4129 the name twice. */
4130 DECL_ARTIFICIAL (gnu_decl) = 1;
4131 rest_of_type_compilation (gnu_scalar_type, global_bindings_p ());
4135 /* If we deferred processing of incomplete types, re-enable it. If there
4136 were no other disables and we have some to process, do so. */
4137 if (this_deferred && --defer_incomplete_level == 0 && defer_incomplete_list)
4139 struct incomplete *incp = defer_incomplete_list;
4140 struct incomplete *next;
4142 defer_incomplete_list = NULL;
4143 for (; incp; incp = next)
4145 next = incp->next;
4147 if (incp->old_type)
4148 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4149 gnat_to_gnu_type (incp->full_type));
4150 free (incp);
4154 /* If we are not defining this type, see if it's in the incomplete list.
4155 If so, handle that list entry now. */
4156 else if (!definition)
4158 struct incomplete *incp;
4160 for (incp = defer_incomplete_list; incp; incp = incp->next)
4161 if (incp->old_type && incp->full_type == gnat_entity)
4163 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4164 TREE_TYPE (gnu_decl));
4165 incp->old_type = NULL_TREE;
4169 /* If there are no incomplete types and we have deferred emission
4170 of debug information, check whether we have finished defining
4171 all nested records.
4172 If so, handle the list now. */
4174 if (debug_deferred)
4175 defer_debug_level--;
4177 if (defer_debug_incomplete_list
4178 && !defer_incomplete_level
4179 && !defer_debug_level)
4181 tree c, n;
4183 defer_debug_incomplete_list = nreverse (defer_debug_incomplete_list);
4185 for (c = defer_debug_incomplete_list; c; c = n)
4187 n = TREE_CHAIN (c);
4188 write_record_type_debug_info (TREE_VALUE (c));
4191 defer_debug_incomplete_list = 0;
4194 if (this_global)
4195 force_global--;
4197 if (Is_Packed_Array_Type (gnat_entity)
4198 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
4199 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
4200 && !present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
4201 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
4203 return gnu_decl;
4206 /* Similar, but if the returned value is a COMPONENT_REF, return the
4207 FIELD_DECL. */
4209 tree
4210 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4212 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4214 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4215 gnu_field = TREE_OPERAND (gnu_field, 1);
4217 return gnu_field;
4220 /* Given GNAT_ENTITY, elaborate all expressions that are required to
4221 be elaborated at the point of its definition, but do nothing else. */
4223 void
4224 elaborate_entity (Entity_Id gnat_entity)
4226 switch (Ekind (gnat_entity))
4228 case E_Signed_Integer_Subtype:
4229 case E_Modular_Integer_Subtype:
4230 case E_Enumeration_Subtype:
4231 case E_Ordinary_Fixed_Point_Subtype:
4232 case E_Decimal_Fixed_Point_Subtype:
4233 case E_Floating_Point_Subtype:
4235 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
4236 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
4238 /* ??? Tests for avoiding static constraint error expression
4239 is needed until the front stops generating bogus conversions
4240 on bounds of real types. */
4242 if (!Raises_Constraint_Error (gnat_lb))
4243 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
4244 1, 0, Needs_Debug_Info (gnat_entity));
4245 if (!Raises_Constraint_Error (gnat_hb))
4246 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
4247 1, 0, Needs_Debug_Info (gnat_entity));
4248 break;
4251 case E_Record_Type:
4253 Node_Id full_definition = Declaration_Node (gnat_entity);
4254 Node_Id record_definition = Type_Definition (full_definition);
4256 /* If this is a record extension, go a level further to find the
4257 record definition. */
4258 if (Nkind (record_definition) == N_Derived_Type_Definition)
4259 record_definition = Record_Extension_Part (record_definition);
4261 break;
4263 case E_Record_Subtype:
4264 case E_Private_Subtype:
4265 case E_Limited_Private_Subtype:
4266 case E_Record_Subtype_With_Private:
4267 if (Is_Constrained (gnat_entity)
4268 && Has_Discriminants (Base_Type (gnat_entity))
4269 && Present (Discriminant_Constraint (gnat_entity)))
4271 Node_Id gnat_discriminant_expr;
4272 Entity_Id gnat_field;
4274 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
4275 gnat_discriminant_expr
4276 = First_Elmt (Discriminant_Constraint (gnat_entity));
4277 Present (gnat_field);
4278 gnat_field = Next_Discriminant (gnat_field),
4279 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
4280 /* ??? For now, ignore access discriminants. */
4281 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
4282 elaborate_expression (Node (gnat_discriminant_expr),
4283 gnat_entity,
4284 get_entity_name (gnat_field), 1, 0, 0);
4286 break;
4291 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
4292 any entities on its entity chain similarly. */
4294 void
4295 mark_out_of_scope (Entity_Id gnat_entity)
4297 Entity_Id gnat_sub_entity;
4298 unsigned int kind = Ekind (gnat_entity);
4300 /* If this has an entity list, process all in the list. */
4301 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
4302 || IN (kind, Private_Kind)
4303 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
4304 || kind == E_Function || kind == E_Generic_Function
4305 || kind == E_Generic_Package || kind == E_Generic_Procedure
4306 || kind == E_Loop || kind == E_Operator || kind == E_Package
4307 || kind == E_Package_Body || kind == E_Procedure
4308 || kind == E_Record_Type || kind == E_Record_Subtype
4309 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
4310 for (gnat_sub_entity = First_Entity (gnat_entity);
4311 Present (gnat_sub_entity);
4312 gnat_sub_entity = Next_Entity (gnat_sub_entity))
4313 if (Scope (gnat_sub_entity) == gnat_entity
4314 && gnat_sub_entity != gnat_entity)
4315 mark_out_of_scope (gnat_sub_entity);
4317 /* Now clear this if it has been defined, but only do so if it isn't
4318 a subprogram or parameter. We could refine this, but it isn't
4319 worth it. If this is statically allocated, it is supposed to
4320 hang around out of cope. */
4321 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
4322 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
4324 save_gnu_tree (gnat_entity, NULL_TREE, true);
4325 save_gnu_tree (gnat_entity, error_mark_node, true);
4329 /* Set the alias set of GNU_NEW_TYPE to be that of GNU_OLD_TYPE. If this
4330 is a multi-dimensional array type, do this recursively. */
4332 static void
4333 copy_alias_set (tree gnu_new_type, tree gnu_old_type)
4335 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
4336 of a one-dimensional array, since the padding has the same alias set
4337 as the field type, but if it's a multi-dimensional array, we need to
4338 see the inner types. */
4339 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
4340 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
4341 || TYPE_IS_PADDING_P (gnu_old_type)))
4342 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
4344 /* We need to be careful here in case GNU_OLD_TYPE is an unconstrained
4345 array. In that case, it doesn't have the same shape as GNU_NEW_TYPE,
4346 so we need to go down to what does. */
4347 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
4348 gnu_old_type
4349 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
4351 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
4352 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
4353 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
4354 copy_alias_set (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type));
4356 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
4357 record_component_aliases (gnu_new_type);
4360 /* Return a TREE_LIST describing the substitutions needed to reflect
4361 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
4362 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
4363 of GNAT_SUBTYPE. The substitutions can be in any order. TREE_PURPOSE
4364 gives the tree for the discriminant and TREE_VALUES is the replacement
4365 value. They are in the form of operands to substitute_in_expr.
4366 DEFINITION is as in gnat_to_gnu_entity. */
4368 static tree
4369 substitution_list (Entity_Id gnat_subtype, Entity_Id gnat_type,
4370 tree gnu_list, bool definition)
4372 Entity_Id gnat_discrim;
4373 Node_Id gnat_value;
4375 if (No (gnat_type))
4376 gnat_type = Implementation_Base_Type (gnat_subtype);
4378 if (Has_Discriminants (gnat_type))
4379 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
4380 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
4381 Present (gnat_discrim);
4382 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
4383 gnat_value = Next_Elmt (gnat_value))
4384 /* Ignore access discriminants. */
4385 if (!Is_Access_Type (Etype (Node (gnat_value))))
4386 gnu_list = tree_cons (gnat_to_gnu_field_decl (gnat_discrim),
4387 elaborate_expression
4388 (Node (gnat_value), gnat_subtype,
4389 get_entity_name (gnat_discrim), definition,
4390 1, 0),
4391 gnu_list);
4393 return gnu_list;
4396 /* For the following two functions: for each GNAT entity, the GCC
4397 tree node used as a dummy for that entity, if any. */
4399 static GTY((length ("max_gnat_nodes"))) tree * dummy_node_table;
4401 /* Initialize the above table. */
4403 void
4404 init_dummy_type (void)
4406 Node_Id gnat_node;
4408 dummy_node_table = (tree *) ggc_alloc (max_gnat_nodes * sizeof (tree));
4410 for (gnat_node = 0; gnat_node < max_gnat_nodes; gnat_node++)
4411 dummy_node_table[gnat_node] = NULL_TREE;
4413 dummy_node_table -= First_Node_Id;
4416 /* Make a dummy type corresponding to GNAT_TYPE. */
4418 tree
4419 make_dummy_type (Entity_Id gnat_type)
4421 Entity_Id gnat_underlying;
4422 tree gnu_type;
4424 /* Find a full type for GNAT_TYPE, taking into account any class wide
4425 types. */
4426 if (Is_Class_Wide_Type (gnat_type) && Present (Equivalent_Type (gnat_type)))
4427 gnat_type = Equivalent_Type (gnat_type);
4428 else if (Ekind (gnat_type) == E_Class_Wide_Type)
4429 gnat_type = Root_Type (gnat_type);
4431 for (gnat_underlying = gnat_type;
4432 (IN (Ekind (gnat_underlying), Incomplete_Or_Private_Kind)
4433 && Present (Full_View (gnat_underlying)));
4434 gnat_underlying = Full_View (gnat_underlying))
4437 /* If it there already a dummy type, use that one. Else make one. */
4438 if (dummy_node_table[gnat_underlying])
4439 return dummy_node_table[gnat_underlying];
4441 /* If this is a record, make this a RECORD_TYPE or UNION_TYPE; else make
4442 it a VOID_TYPE. */
4443 if (Is_Unchecked_Union (gnat_underlying))
4445 gnu_type = make_node (UNION_TYPE);
4446 TYPE_UNCHECKED_UNION_P (gnu_type) = 1;
4448 else if (Is_Record_Type (gnat_underlying))
4449 gnu_type = make_node (RECORD_TYPE);
4450 else
4451 gnu_type = make_node (ENUMERAL_TYPE);
4453 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
4454 TYPE_DUMMY_P (gnu_type) = 1;
4455 if (AGGREGATE_TYPE_P (gnu_type))
4456 TYPE_STUB_DECL (gnu_type) = build_decl (TYPE_DECL, NULL_TREE, gnu_type);
4458 dummy_node_table[gnat_underlying] = gnu_type;
4460 return gnu_type;
4463 /* Return true if the size represented by GNU_SIZE can be handled by an
4464 allocation. If STATIC_P is true, consider only what can be done with a
4465 static allocation. */
4467 static bool
4468 allocatable_size_p (tree gnu_size, bool static_p)
4470 HOST_WIDE_INT our_size;
4472 /* If this is not a static allocation, the only case we want to forbid
4473 is an overflowing size. That will be converted into a raise a
4474 Storage_Error. */
4475 if (!static_p)
4476 return !(TREE_CODE (gnu_size) == INTEGER_CST
4477 && TREE_CONSTANT_OVERFLOW (gnu_size));
4479 /* Otherwise, we need to deal with both variable sizes and constant
4480 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
4481 since assemblers may not like very large sizes. */
4482 if (!host_integerp (gnu_size, 1))
4483 return false;
4485 our_size = tree_low_cst (gnu_size, 1);
4486 return (int) our_size == our_size;
4489 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
4491 static void
4492 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
4494 Node_Id gnat_temp;
4496 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
4497 gnat_temp = Next_Rep_Item (gnat_temp))
4498 if (Nkind (gnat_temp) == N_Pragma)
4500 struct attrib *attr;
4501 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
4502 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
4503 enum attr_type etype;
4505 if (Present (gnat_assoc) && Present (First (gnat_assoc))
4506 && Present (Next (First (gnat_assoc)))
4507 && (Nkind (Expression (Next (First (gnat_assoc))))
4508 == N_String_Literal))
4510 gnu_arg0 = get_identifier (TREE_STRING_POINTER
4511 (gnat_to_gnu
4512 (Expression (Next
4513 (First (gnat_assoc))))));
4514 if (Present (Next (Next (First (gnat_assoc))))
4515 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
4516 == N_String_Literal))
4517 gnu_arg1 = get_identifier (TREE_STRING_POINTER
4518 (gnat_to_gnu
4519 (Expression
4520 (Next (Next
4521 (First (gnat_assoc)))))));
4524 switch (Get_Pragma_Id (Chars (gnat_temp)))
4526 case Pragma_Machine_Attribute:
4527 etype = ATTR_MACHINE_ATTRIBUTE;
4528 break;
4530 case Pragma_Linker_Alias:
4531 etype = ATTR_LINK_ALIAS;
4532 break;
4534 case Pragma_Linker_Section:
4535 etype = ATTR_LINK_SECTION;
4536 break;
4538 case Pragma_Linker_Constructor:
4539 etype = ATTR_LINK_CONSTRUCTOR;
4540 break;
4542 case Pragma_Linker_Destructor:
4543 etype = ATTR_LINK_DESTRUCTOR;
4544 break;
4546 case Pragma_Weak_External:
4547 etype = ATTR_WEAK_EXTERNAL;
4548 break;
4550 default:
4551 continue;
4554 attr = (struct attrib *) xmalloc (sizeof (struct attrib));
4555 attr->next = *attr_list;
4556 attr->type = etype;
4557 attr->name = gnu_arg0;
4559 /* If we have an argument specified together with an attribute name,
4560 make it a single TREE_VALUE entry in a list of arguments, as GCC
4561 expects it. */
4562 if (gnu_arg1 != NULL_TREE)
4563 attr->args = build_tree_list (NULL_TREE, gnu_arg1);
4564 else
4565 attr->args = NULL_TREE;
4567 attr->error_point
4568 = Present (Next (First (gnat_assoc)))
4569 ? Expression (Next (First (gnat_assoc))) : gnat_temp;
4570 *attr_list = attr;
4574 /* Get the unpadded version of a GNAT type. */
4576 tree
4577 get_unpadded_type (Entity_Id gnat_entity)
4579 tree type = gnat_to_gnu_type (gnat_entity);
4581 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4582 type = TREE_TYPE (TYPE_FIELDS (type));
4584 return type;
4587 /* Called when we need to protect a variable object using a save_expr. */
4589 tree
4590 maybe_variable (tree gnu_operand)
4592 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
4593 || TREE_CODE (gnu_operand) == SAVE_EXPR
4594 || TREE_CODE (gnu_operand) == NULL_EXPR)
4595 return gnu_operand;
4597 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
4599 tree gnu_result = build1 (UNCONSTRAINED_ARRAY_REF,
4600 TREE_TYPE (gnu_operand),
4601 variable_size (TREE_OPERAND (gnu_operand, 0)));
4603 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result)
4604 = TYPE_READONLY (TREE_TYPE (TREE_TYPE (gnu_operand)));
4605 return gnu_result;
4607 else
4608 return variable_size (gnu_operand);
4611 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
4612 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
4613 return the GCC tree to use for that expression. GNU_NAME is the
4614 qualification to use if an external name is appropriate and DEFINITION is
4615 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
4616 we need a result. Otherwise, we are just elaborating this for
4617 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
4618 purposes even if it isn't needed for code generation. */
4620 static tree
4621 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity,
4622 tree gnu_name, bool definition, bool need_value,
4623 bool need_debug)
4625 tree gnu_expr;
4627 /* If we already elaborated this expression (e.g., it was involved
4628 in the definition of a private type), use the old value. */
4629 if (present_gnu_tree (gnat_expr))
4630 return get_gnu_tree (gnat_expr);
4632 /* If we don't need a value and this is static or a discriment, we
4633 don't need to do anything. */
4634 else if (!need_value
4635 && (Is_OK_Static_Expression (gnat_expr)
4636 || (Nkind (gnat_expr) == N_Identifier
4637 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
4638 return 0;
4640 /* Otherwise, convert this tree to its GCC equivalent. */
4641 gnu_expr
4642 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
4643 gnu_name, definition, need_debug);
4645 /* Save the expression in case we try to elaborate this entity again. Since
4646 this is not a DECL, don't check it. Don't save if it's a discriminant. */
4647 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
4648 save_gnu_tree (gnat_expr, gnu_expr, true);
4650 return need_value ? gnu_expr : error_mark_node;
4653 /* Similar, but take a GNU expression. */
4655 static tree
4656 elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
4657 tree gnu_expr, tree gnu_name, bool definition,
4658 bool need_debug)
4660 tree gnu_decl = NULL_TREE;
4661 /* Strip any conversions to see if the expression is a readonly variable.
4662 ??? This really should remain readonly, but we have to think about
4663 the typing of the tree here. */
4664 tree gnu_inner_expr = remove_conversions (gnu_expr, true);
4665 bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
4666 bool expr_variable;
4668 /* In most cases, we won't see a naked FIELD_DECL here because a
4669 discriminant reference will have been replaced with a COMPONENT_REF
4670 when the type is being elaborated. However, there are some cases
4671 involving child types where we will. So convert it to a COMPONENT_REF
4672 here. We have to hope it will be at the highest level of the
4673 expression in these cases. */
4674 if (TREE_CODE (gnu_expr) == FIELD_DECL)
4675 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
4676 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
4677 gnu_expr, NULL_TREE);
4679 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
4680 that is a constant, make a variable that is initialized to contain the
4681 bound when the package containing the definition is elaborated. If
4682 this entity is defined at top level and a bound or discriminant value
4683 isn't a constant or a reference to a discriminant, replace the bound
4684 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
4685 rely here on the fact that an expression cannot contain both the
4686 discriminant and some other variable. */
4688 expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
4689 && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
4690 && TREE_READONLY (gnu_inner_expr))
4691 && !CONTAINS_PLACEHOLDER_P (gnu_expr));
4693 /* If this is a static expression or contains a discriminant, we don't
4694 need the variable for debugging (and can't elaborate anyway if a
4695 discriminant). */
4696 if (need_debug
4697 && (Is_OK_Static_Expression (gnat_expr)
4698 || CONTAINS_PLACEHOLDER_P (gnu_expr)))
4699 need_debug = false;
4701 /* Now create the variable if we need it. */
4702 if (need_debug || (expr_variable && expr_global))
4703 gnu_decl
4704 = create_var_decl (create_concat_name (gnat_entity,
4705 IDENTIFIER_POINTER (gnu_name)),
4706 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true,
4707 Is_Public (gnat_entity), !definition, false, NULL,
4708 gnat_entity);
4710 /* We only need to use this variable if we are in global context since GCC
4711 can do the right thing in the local case. */
4712 if (expr_global && expr_variable)
4713 return gnu_decl;
4714 else if (!expr_variable)
4715 return gnu_expr;
4716 else
4717 return maybe_variable (gnu_expr);
4720 /* Create a record type that contains a field of TYPE with a starting bit
4721 position so that it is aligned to ALIGN bits and is SIZE bytes long. */
4723 tree
4724 make_aligning_type (tree type, int align, tree size)
4726 tree record_type = make_node (RECORD_TYPE);
4727 tree place = build0 (PLACEHOLDER_EXPR, record_type);
4728 tree size_addr_place = convert (sizetype,
4729 build_unary_op (ADDR_EXPR, NULL_TREE,
4730 place));
4731 tree name = TYPE_NAME (type);
4732 tree pos, field;
4734 if (TREE_CODE (name) == TYPE_DECL)
4735 name = DECL_NAME (name);
4737 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
4739 /* The bit position is obtained by "and"ing the alignment minus 1
4740 with the two's complement of the address and multiplying
4741 by the number of bits per unit. Do all this in sizetype. */
4742 pos = size_binop (MULT_EXPR,
4743 convert (bitsizetype,
4744 size_binop (BIT_AND_EXPR,
4745 size_diffop (size_zero_node,
4746 size_addr_place),
4747 ssize_int ((align / BITS_PER_UNIT)
4748 - 1))),
4749 bitsize_unit_node);
4751 /* Create the field, with -1 as the 'addressable' indication to avoid the
4752 creation of a bitfield. We don't need one, it would have damaging
4753 consequences on the alignment computation, and create_field_decl would
4754 make one without this special argument, for instance because of the
4755 complex position expression. */
4756 field = create_field_decl (get_identifier ("F"), type, record_type, 1, size,
4757 pos, -1);
4759 finish_record_type (record_type, field, true, false);
4760 TYPE_ALIGN (record_type) = BIGGEST_ALIGNMENT;
4761 TYPE_SIZE (record_type)
4762 = size_binop (PLUS_EXPR,
4763 size_binop (MULT_EXPR, convert (bitsizetype, size),
4764 bitsize_unit_node),
4765 bitsize_int (align));
4766 TYPE_SIZE_UNIT (record_type)
4767 = size_binop (PLUS_EXPR, size, size_int (align / BITS_PER_UNIT));
4768 copy_alias_set (record_type, type);
4769 return record_type;
4772 /* TYPE is a RECORD_TYPE, UNION_TYPE, or QUAL_UNION_TYPE, with BLKmode that's
4773 being used as the field type of a packed record. See if we can rewrite it
4774 as a record that has a non-BLKmode type, which we can pack tighter. If so,
4775 return the new type. If not, return the original type. */
4777 static tree
4778 make_packable_type (tree type)
4780 tree new_type = make_node (TREE_CODE (type));
4781 tree field_list = NULL_TREE;
4782 tree old_field;
4784 /* Copy the name and flags from the old type to that of the new and set
4785 the alignment to try for an integral type. For QUAL_UNION_TYPE,
4786 also copy the size. */
4787 TYPE_NAME (new_type) = TYPE_NAME (type);
4788 TYPE_JUSTIFIED_MODULAR_P (new_type)
4789 = TYPE_JUSTIFIED_MODULAR_P (type);
4790 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
4792 if (TREE_CODE (type) == RECORD_TYPE)
4793 TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
4794 else if (TREE_CODE (type) == QUAL_UNION_TYPE)
4796 TYPE_SIZE (new_type) = TYPE_SIZE (type);
4797 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
4800 TYPE_ALIGN (new_type)
4801 = ((HOST_WIDE_INT) 1
4802 << (floor_log2 (tree_low_cst (TYPE_SIZE (type), 1) - 1) + 1));
4804 /* Now copy the fields, keeping the position and size. */
4805 for (old_field = TYPE_FIELDS (type); old_field;
4806 old_field = TREE_CHAIN (old_field))
4808 tree new_field_type = TREE_TYPE (old_field);
4809 tree new_field;
4811 if (TYPE_MODE (new_field_type) == BLKmode
4812 && (TREE_CODE (new_field_type) == RECORD_TYPE
4813 || TREE_CODE (new_field_type) == UNION_TYPE
4814 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
4815 && host_integerp (TYPE_SIZE (new_field_type), 1))
4816 new_field_type = make_packable_type (new_field_type);
4818 new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
4819 new_type, TYPE_PACKED (type),
4820 DECL_SIZE (old_field),
4821 bit_position (old_field),
4822 !DECL_NONADDRESSABLE_P (old_field));
4824 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
4825 SET_DECL_ORIGINAL_FIELD
4826 (new_field, (DECL_ORIGINAL_FIELD (old_field)
4827 ? DECL_ORIGINAL_FIELD (old_field) : old_field));
4829 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
4830 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
4832 TREE_CHAIN (new_field) = field_list;
4833 field_list = new_field;
4836 finish_record_type (new_type, nreverse (field_list), true, true);
4837 copy_alias_set (new_type, type);
4838 return TYPE_MODE (new_type) == BLKmode ? type : new_type;
4841 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
4842 if needed. We have already verified that SIZE and TYPE are large enough.
4844 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
4845 to issue a warning.
4847 IS_USER_TYPE is true if we must be sure we complete the original type.
4849 DEFINITION is true if this type is being defined.
4851 SAME_RM_SIZE is true if the RM_Size of the resulting type is to be
4852 set to its TYPE_SIZE; otherwise, it's set to the RM_Size of the original
4853 type. */
4855 tree
4856 maybe_pad_type (tree type, tree size, unsigned int align,
4857 Entity_Id gnat_entity, const char *name_trailer,
4858 bool is_user_type, bool definition, bool same_rm_size)
4860 tree orig_size = TYPE_SIZE (type);
4861 tree record;
4862 tree field;
4864 /* If TYPE is a padded type, see if it agrees with any size and alignment
4865 we were given. If so, return the original type. Otherwise, strip
4866 off the padding, since we will either be returning the inner type
4867 or repadding it. If no size or alignment is specified, use that of
4868 the original padded type. */
4870 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4872 if ((!size
4873 || operand_equal_p (round_up (size,
4874 MAX (align, TYPE_ALIGN (type))),
4875 round_up (TYPE_SIZE (type),
4876 MAX (align, TYPE_ALIGN (type))),
4878 && (align == 0 || align == TYPE_ALIGN (type)))
4879 return type;
4881 if (!size)
4882 size = TYPE_SIZE (type);
4883 if (align == 0)
4884 align = TYPE_ALIGN (type);
4886 type = TREE_TYPE (TYPE_FIELDS (type));
4887 orig_size = TYPE_SIZE (type);
4890 /* If the size is either not being changed or is being made smaller (which
4891 is not done here (and is only valid for bitfields anyway), show the size
4892 isn't changing. Likewise, clear the alignment if it isn't being
4893 changed. Then return if we aren't doing anything. */
4895 if (size
4896 && (operand_equal_p (size, orig_size, 0)
4897 || (TREE_CODE (orig_size) == INTEGER_CST
4898 && tree_int_cst_lt (size, orig_size))))
4899 size = NULL_TREE;
4901 if (align == TYPE_ALIGN (type))
4902 align = 0;
4904 if (align == 0 && !size)
4905 return type;
4907 /* We used to modify the record in place in some cases, but that could
4908 generate incorrect debugging information. So make a new record
4909 type and name. */
4910 record = make_node (RECORD_TYPE);
4912 if (Present (gnat_entity))
4913 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
4915 /* If we were making a type, complete the original type and give it a
4916 name. */
4917 if (is_user_type)
4918 create_type_decl (get_entity_name (gnat_entity), type,
4919 NULL, !Comes_From_Source (gnat_entity),
4920 !(TYPE_NAME (type)
4921 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4922 && DECL_IGNORED_P (TYPE_NAME (type))),
4923 gnat_entity);
4925 /* If we are changing the alignment and the input type is a record with
4926 BLKmode and a small constant size, try to make a form that has an
4927 integral mode. That might allow this record to have an integral mode,
4928 which will be much more efficient. There is no point in doing this if a
4929 size is specified unless it is also smaller than the biggest alignment
4930 and it is incorrect to do this if the size of the original type is not a
4931 multiple of the alignment. */
4932 if (align != 0
4933 && TREE_CODE (type) == RECORD_TYPE
4934 && TYPE_MODE (type) == BLKmode
4935 && host_integerp (orig_size, 1)
4936 && compare_tree_int (orig_size, BIGGEST_ALIGNMENT) <= 0
4937 && (!size
4938 || (TREE_CODE (size) == INTEGER_CST
4939 && compare_tree_int (size, BIGGEST_ALIGNMENT) <= 0))
4940 && tree_low_cst (orig_size, 1) % align == 0)
4941 type = make_packable_type (type);
4943 field = create_field_decl (get_identifier ("F"), type, record, 0,
4944 NULL_TREE, bitsize_zero_node, 1);
4946 DECL_INTERNAL_P (field) = 1;
4947 TYPE_SIZE (record) = size ? size : orig_size;
4948 TYPE_SIZE_UNIT (record)
4949 = (size ? convert (sizetype,
4950 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node))
4951 : TYPE_SIZE_UNIT (type));
4953 TYPE_ALIGN (record) = align;
4954 TYPE_IS_PADDING_P (record) = 1;
4955 TYPE_VOLATILE (record)
4956 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
4957 finish_record_type (record, field, true, false);
4959 /* Keep the RM_Size of the padded record as that of the old record
4960 if requested. */
4961 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : rm_size (type));
4963 /* Unless debugging information isn't being written for the input type,
4964 write a record that shows what we are a subtype of and also make a
4965 variable that indicates our size, if variable. */
4966 if (TYPE_NAME (record) && AGGREGATE_TYPE_P (type)
4967 && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
4968 || !DECL_IGNORED_P (TYPE_NAME (type))))
4970 tree marker = make_node (RECORD_TYPE);
4971 tree name = (TREE_CODE (TYPE_NAME (record)) == TYPE_DECL
4972 ? DECL_NAME (TYPE_NAME (record))
4973 : TYPE_NAME (record));
4974 tree orig_name = TYPE_NAME (type);
4976 if (TREE_CODE (orig_name) == TYPE_DECL)
4977 orig_name = DECL_NAME (orig_name);
4979 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
4980 finish_record_type (marker,
4981 create_field_decl (orig_name, integer_type_node,
4982 marker, 0, NULL_TREE, NULL_TREE,
4984 false, false);
4986 if (size && TREE_CODE (size) != INTEGER_CST && definition)
4987 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
4988 sizetype, TYPE_SIZE (record), false, false, false,
4989 false, NULL, gnat_entity);
4992 type = record;
4994 if (CONTAINS_PLACEHOLDER_P (orig_size))
4995 orig_size = max_size (orig_size, true);
4997 /* If the size was widened explicitly, maybe give a warning. */
4998 if (size && Present (gnat_entity)
4999 && !operand_equal_p (size, orig_size, 0)
5000 && !(TREE_CODE (size) == INTEGER_CST
5001 && TREE_CODE (orig_size) == INTEGER_CST
5002 && tree_int_cst_lt (size, orig_size)))
5004 Node_Id gnat_error_node = Empty;
5006 if (Is_Packed_Array_Type (gnat_entity))
5007 gnat_entity = Associated_Node_For_Itype (gnat_entity);
5009 if ((Ekind (gnat_entity) == E_Component
5010 || Ekind (gnat_entity) == E_Discriminant)
5011 && Present (Component_Clause (gnat_entity)))
5012 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
5013 else if (Present (Size_Clause (gnat_entity)))
5014 gnat_error_node = Expression (Size_Clause (gnat_entity));
5016 /* Generate message only for entities that come from source, since
5017 if we have an entity created by expansion, the message will be
5018 generated for some other corresponding source entity. */
5019 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
5020 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
5021 gnat_entity,
5022 size_diffop (size, orig_size));
5024 else if (*name_trailer == 'C' && !Is_Internal (gnat_entity))
5025 post_error_ne_tree ("component of& padded{ by ^ bits}?",
5026 gnat_entity, gnat_entity,
5027 size_diffop (size, orig_size));
5030 return type;
5033 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
5034 the value passed against the list of choices. */
5036 tree
5037 choices_to_gnu (tree operand, Node_Id choices)
5039 Node_Id choice;
5040 Node_Id gnat_temp;
5041 tree result = integer_zero_node;
5042 tree this_test, low = 0, high = 0, single = 0;
5044 for (choice = First (choices); Present (choice); choice = Next (choice))
5046 switch (Nkind (choice))
5048 case N_Range:
5049 low = gnat_to_gnu (Low_Bound (choice));
5050 high = gnat_to_gnu (High_Bound (choice));
5052 /* There's no good type to use here, so we might as well use
5053 integer_type_node. */
5054 this_test
5055 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5056 build_binary_op (GE_EXPR, integer_type_node,
5057 operand, low),
5058 build_binary_op (LE_EXPR, integer_type_node,
5059 operand, high));
5061 break;
5063 case N_Subtype_Indication:
5064 gnat_temp = Range_Expression (Constraint (choice));
5065 low = gnat_to_gnu (Low_Bound (gnat_temp));
5066 high = gnat_to_gnu (High_Bound (gnat_temp));
5068 this_test
5069 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5070 build_binary_op (GE_EXPR, integer_type_node,
5071 operand, low),
5072 build_binary_op (LE_EXPR, integer_type_node,
5073 operand, high));
5074 break;
5076 case N_Identifier:
5077 case N_Expanded_Name:
5078 /* This represents either a subtype range, an enumeration
5079 literal, or a constant Ekind says which. If an enumeration
5080 literal or constant, fall through to the next case. */
5081 if (Ekind (Entity (choice)) != E_Enumeration_Literal
5082 && Ekind (Entity (choice)) != E_Constant)
5084 tree type = gnat_to_gnu_type (Entity (choice));
5086 low = TYPE_MIN_VALUE (type);
5087 high = TYPE_MAX_VALUE (type);
5089 this_test
5090 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5091 build_binary_op (GE_EXPR, integer_type_node,
5092 operand, low),
5093 build_binary_op (LE_EXPR, integer_type_node,
5094 operand, high));
5095 break;
5097 /* ... fall through ... */
5098 case N_Character_Literal:
5099 case N_Integer_Literal:
5100 single = gnat_to_gnu (choice);
5101 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
5102 single);
5103 break;
5105 case N_Others_Choice:
5106 this_test = integer_one_node;
5107 break;
5109 default:
5110 gcc_unreachable ();
5113 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
5114 result, this_test);
5117 return result;
5120 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
5121 placed in GNU_RECORD_TYPE.
5123 PACKED is 1 if the enclosing record is packed and -1 if the enclosing
5124 record has a Component_Alignment of Storage_Unit.
5126 DEFINITION is true if this field is for a record being defined. */
5128 static tree
5129 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
5130 bool definition)
5132 tree gnu_field_id = get_entity_name (gnat_field);
5133 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
5134 tree gnu_pos = 0;
5135 tree gnu_size = 0;
5136 tree gnu_field;
5137 bool needs_strict_alignment
5138 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
5139 || Treat_As_Volatile (gnat_field));
5141 /* If this field requires strict alignment or contains an item of
5142 variable sized, pretend it isn't packed. */
5143 if (needs_strict_alignment || is_variable_size (gnu_field_type))
5144 packed = 0;
5146 /* For packed records, this is one of the few occasions on which we use
5147 the official RM size for discrete or fixed-point components, instead
5148 of the normal GNAT size stored in Esize. See description in Einfo:
5149 "Handling of Type'Size Values" for further details. */
5151 if (packed == 1)
5152 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
5153 gnat_field, FIELD_DECL, false, true);
5155 if (Known_Static_Esize (gnat_field))
5156 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5157 gnat_field, FIELD_DECL, false, true);
5159 /* If we are packing this record, have a specified size that's smaller than
5160 that of the field type, or a position is specified, and the field type is
5161 also a record that's BLKmode and with a small constant size, see if we
5162 can get a better form of the type that allows more packing. If we can,
5163 show a size was specified for it if there wasn't one so we know to make
5164 this a bitfield and avoid making things wider. */
5165 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5166 && TYPE_MODE (gnu_field_type) == BLKmode
5167 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
5168 && compare_tree_int (TYPE_SIZE (gnu_field_type), BIGGEST_ALIGNMENT) <= 0
5169 && (packed == 1
5170 || (gnu_size
5171 && tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type)))
5172 || Present (Component_Clause (gnat_field))))
5174 /* See what the alternate type and size would be. */
5175 tree gnu_packable_type = make_packable_type (gnu_field_type);
5177 /* Compute whether we should avoid the substitution. */
5178 int reject =
5179 /* There is no point substituting if there is no change. */
5180 (gnu_packable_type == gnu_field_type
5182 /* The size of an aliased field must be an exact multiple of the
5183 type's alignment, which the substitution might increase. Reject
5184 substitutions that would so invalidate a component clause when the
5185 specified position is byte aligned, as the change would have no
5186 real benefit from the packing standpoint anyway. */
5187 (Is_Aliased (gnat_field)
5188 && Present (Component_Clause (gnat_field))
5189 && UI_To_Int (Component_Bit_Offset (gnat_field)) % BITS_PER_UNIT == 0
5190 && tree_low_cst (gnu_size, 1) % TYPE_ALIGN (gnu_packable_type) != 0)
5193 /* Substitute unless told otherwise. */
5194 if (!reject)
5196 gnu_field_type = gnu_packable_type;
5198 if (gnu_size == 0)
5199 gnu_size = rm_size (gnu_field_type);
5203 /* If we are packing the record and the field is BLKmode, round the
5204 size up to a byte boundary. */
5205 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
5206 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
5208 if (Present (Component_Clause (gnat_field)))
5210 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
5211 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5212 gnat_field, FIELD_DECL, false, true);
5214 /* Ensure the position does not overlap with the parent subtype,
5215 if there is one. */
5216 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
5218 tree gnu_parent
5219 = gnat_to_gnu_type (Parent_Subtype
5220 (Underlying_Type (Scope (gnat_field))));
5222 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
5223 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
5225 post_error_ne_tree
5226 ("offset of& must be beyond parent{, minimum allowed is ^}",
5227 First_Bit (Component_Clause (gnat_field)), gnat_field,
5228 TYPE_SIZE_UNIT (gnu_parent));
5232 /* If this field needs strict alignment, ensure the record is
5233 sufficiently aligned and that that position and size are
5234 consistent with the alignment. */
5235 if (needs_strict_alignment)
5237 tree gnu_rounded_size = round_up (rm_size (gnu_field_type),
5238 TYPE_ALIGN (gnu_field_type));
5240 TYPE_ALIGN (gnu_record_type)
5241 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
5243 /* If Atomic, the size must match exactly that of the field. */
5244 if ((Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
5245 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
5247 post_error_ne_tree
5248 ("atomic field& must be natural size of type{ (^)}",
5249 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5250 TYPE_SIZE (gnu_field_type));
5252 gnu_size = NULL_TREE;
5255 /* If Aliased, the size must match exactly the rounded size. We
5256 used to be more accommodating here and accept greater sizes, but
5257 fully supporting this case on big-endian platforms would require
5258 switching to a more involved layout for the field. */
5259 else if (Is_Aliased (gnat_field)
5260 && gnu_size
5261 && ! operand_equal_p (gnu_size, gnu_rounded_size, 0))
5263 post_error_ne_tree
5264 ("size of aliased field& must be ^ bits",
5265 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5266 gnu_rounded_size);
5267 gnu_size = NULL_TREE;
5270 if (!integer_zerop (size_binop
5271 (TRUNC_MOD_EXPR, gnu_pos,
5272 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
5274 if (Is_Aliased (gnat_field))
5275 post_error_ne_num
5276 ("position of aliased field& must be multiple of ^ bits",
5277 First_Bit (Component_Clause (gnat_field)), gnat_field,
5278 TYPE_ALIGN (gnu_field_type));
5280 else if (Treat_As_Volatile (gnat_field))
5281 post_error_ne_num
5282 ("position of volatile field& must be multiple of ^ bits",
5283 First_Bit (Component_Clause (gnat_field)), gnat_field,
5284 TYPE_ALIGN (gnu_field_type));
5286 else if (Strict_Alignment (Etype (gnat_field)))
5287 post_error_ne_num
5288 ("position of & with aliased or tagged components not multiple of ^ bits",
5289 First_Bit (Component_Clause (gnat_field)), gnat_field,
5290 TYPE_ALIGN (gnu_field_type));
5291 else
5292 gcc_unreachable ();
5294 gnu_pos = NULL_TREE;
5298 if (Is_Atomic (gnat_field))
5299 check_ok_for_atomic (gnu_field_type, gnat_field, false);
5302 /* If the record has rep clauses and this is the tag field, make a rep
5303 clause for it as well. */
5304 else if (Has_Specified_Layout (Scope (gnat_field))
5305 && Chars (gnat_field) == Name_uTag)
5307 gnu_pos = bitsize_zero_node;
5308 gnu_size = TYPE_SIZE (gnu_field_type);
5311 /* We need to make the size the maximum for the type if it is
5312 self-referential and an unconstrained type. In that case, we can't
5313 pack the field since we can't make a copy to align it. */
5314 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5315 && !gnu_size
5316 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
5317 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
5319 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
5320 packed = 0;
5323 /* If no size is specified (or if there was an error), don't specify a
5324 position. */
5325 if (!gnu_size)
5326 gnu_pos = NULL_TREE;
5327 else
5329 /* If the field's type is justified modular, we would need to remove
5330 the wrapper to (better) meet the layout requirements. However we
5331 can do so only if the field is not aliased to preserve the unique
5332 layout and if the prescribed size is not greater than that of the
5333 packed array to preserve the justification. */
5334 if (!needs_strict_alignment
5335 && TREE_CODE (gnu_field_type) == RECORD_TYPE
5336 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
5337 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
5338 <= 0)
5339 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
5341 gnu_field_type
5342 = make_type_from_size (gnu_field_type, gnu_size,
5343 Has_Biased_Representation (gnat_field));
5344 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
5345 "PAD", false, definition, true);
5348 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
5349 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
5351 /* Now create the decl for the field. */
5352 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
5353 packed, gnu_size, gnu_pos,
5354 Is_Aliased (gnat_field));
5355 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
5356 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
5358 if (Ekind (gnat_field) == E_Discriminant)
5359 DECL_DISCRIMINANT_NUMBER (gnu_field)
5360 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
5362 return gnu_field;
5365 /* Return true if TYPE is a type with variable size, a padding type with a
5366 field of variable size or is a record that has a field such a field. */
5368 static bool
5369 is_variable_size (tree type)
5371 tree field;
5373 /* We need not be concerned about this at all if we don't have
5374 strict alignment. */
5375 if (!STRICT_ALIGNMENT)
5376 return false;
5377 else if (!TREE_CONSTANT (TYPE_SIZE (type)))
5378 return true;
5379 else if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type)
5380 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
5381 return true;
5382 else if (TREE_CODE (type) != RECORD_TYPE
5383 && TREE_CODE (type) != UNION_TYPE
5384 && TREE_CODE (type) != QUAL_UNION_TYPE)
5385 return false;
5387 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5388 if (is_variable_size (TREE_TYPE (field)))
5389 return true;
5391 return false;
5394 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
5395 of GCC trees for fields that are in the record and have already been
5396 processed. When called from gnat_to_gnu_entity during the processing of a
5397 record type definition, the GCC nodes for the discriminants will be on
5398 the chain. The other calls to this function are recursive calls from
5399 itself for the Component_List of a variant and the chain is empty.
5401 PACKED is 1 if this is for a record with "pragma pack" and -1 is this is
5402 for a record type with "pragma component_alignment (storage_unit)".
5404 DEFINITION is true if we are defining this record.
5406 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
5407 with a rep clause is to be added. If it is nonzero, that is all that
5408 should be done with such fields.
5410 CANCEL_ALIGNMENT, if true, means the alignment should be zeroed before
5411 laying out the record. This means the alignment only serves to force fields
5412 to be bitfields, but not require the record to be that aligned. This is
5413 used for variants.
5415 ALL_REP, if true, means a rep clause was found for all the fields. This
5416 simplifies the logic since we know we're not in the mixed case.
5418 DEFER_DEBUG, if true, means that the debugging routines should not be
5419 called when finishing constructing the record type.
5421 The processing of the component list fills in the chain with all of the
5422 fields of the record and then the record type is finished. */
5424 static void
5425 components_to_record (tree gnu_record_type, Node_Id component_list,
5426 tree gnu_field_list, int packed, bool definition,
5427 tree *p_gnu_rep_list, bool cancel_alignment,
5428 bool all_rep, bool defer_debug)
5430 Node_Id component_decl;
5431 Entity_Id gnat_field;
5432 Node_Id variant_part;
5433 Node_Id variant;
5434 tree gnu_our_rep_list = NULL_TREE;
5435 tree gnu_field, gnu_last;
5436 bool layout_with_rep = false;
5437 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
5439 /* For each variable within each component declaration create a GCC field
5440 and add it to the list, skipping any pragmas in the list. */
5442 if (Present (Component_Items (component_list)))
5443 for (component_decl = First_Non_Pragma (Component_Items (component_list));
5444 Present (component_decl);
5445 component_decl = Next_Non_Pragma (component_decl))
5447 gnat_field = Defining_Entity (component_decl);
5449 if (Chars (gnat_field) == Name_uParent)
5450 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
5451 else
5453 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
5454 packed, definition);
5456 /* If this is the _Tag field, put it before any discriminants,
5457 instead of after them as is the case for all other fields.
5458 Ignore field of void type if only annotating. */
5459 if (Chars (gnat_field) == Name_uTag)
5460 gnu_field_list = chainon (gnu_field_list, gnu_field);
5461 else
5463 TREE_CHAIN (gnu_field) = gnu_field_list;
5464 gnu_field_list = gnu_field;
5468 save_gnu_tree (gnat_field, gnu_field, false);
5471 /* At the end of the component list there may be a variant part. */
5472 variant_part = Variant_Part (component_list);
5474 /* If this is an unchecked union, each variant must have exactly one
5475 component, each of which becomes one component of this union. */
5476 if (TREE_CODE (gnu_record_type) == UNION_TYPE
5477 && TYPE_UNCHECKED_UNION_P (gnu_record_type)
5478 && Present (variant_part))
5479 for (variant = First_Non_Pragma (Variants (variant_part));
5480 Present (variant);
5481 variant = Next_Non_Pragma (variant))
5483 component_decl
5484 = First_Non_Pragma (Component_Items (Component_List (variant)));
5485 gnat_field = Defining_Entity (component_decl);
5486 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
5487 definition);
5488 TREE_CHAIN (gnu_field) = gnu_field_list;
5489 gnu_field_list = gnu_field;
5490 save_gnu_tree (gnat_field, gnu_field, false);
5493 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
5494 mutually exclusive and should go in the same memory. To do this we need
5495 to treat each variant as a record whose elements are created from the
5496 component list for the variant. So here we create the records from the
5497 lists for the variants and put them all into the QUAL_UNION_TYPE. */
5498 else if (Present (variant_part))
5500 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
5501 Node_Id variant;
5502 tree gnu_union_type = make_node (QUAL_UNION_TYPE);
5503 tree gnu_union_field;
5504 tree gnu_variant_list = NULL_TREE;
5505 tree gnu_name = TYPE_NAME (gnu_record_type);
5506 tree gnu_var_name
5507 = concat_id_with_name
5508 (get_identifier (Get_Name_String (Chars (Name (variant_part)))),
5509 "XVN");
5511 if (TREE_CODE (gnu_name) == TYPE_DECL)
5512 gnu_name = DECL_NAME (gnu_name);
5514 TYPE_NAME (gnu_union_type)
5515 = concat_id_with_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
5516 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
5518 for (variant = First_Non_Pragma (Variants (variant_part));
5519 Present (variant);
5520 variant = Next_Non_Pragma (variant))
5522 tree gnu_variant_type = make_node (RECORD_TYPE);
5523 tree gnu_inner_name;
5524 tree gnu_qual;
5526 Get_Variant_Encoding (variant);
5527 gnu_inner_name = get_identifier (Name_Buffer);
5528 TYPE_NAME (gnu_variant_type)
5529 = concat_id_with_name (TYPE_NAME (gnu_union_type),
5530 IDENTIFIER_POINTER (gnu_inner_name));
5532 /* Set the alignment of the inner type in case we need to make
5533 inner objects into bitfields, but then clear it out
5534 so the record actually gets only the alignment required. */
5535 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
5536 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
5538 /* Similarly, if the outer record has a size specified and all fields
5539 have record rep clauses, we can propagate the size into the
5540 variant part. */
5541 if (all_rep_and_size)
5543 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
5544 TYPE_SIZE_UNIT (gnu_variant_type)
5545 = TYPE_SIZE_UNIT (gnu_record_type);
5548 components_to_record (gnu_variant_type, Component_List (variant),
5549 NULL_TREE, packed, definition,
5550 &gnu_our_rep_list, !all_rep_and_size, all_rep,
5551 false);
5553 gnu_qual = choices_to_gnu (gnu_discriminant,
5554 Discrete_Choices (variant));
5556 Set_Present_Expr (variant, annotate_value (gnu_qual));
5557 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
5558 gnu_union_type, 0,
5559 (all_rep_and_size
5560 ? TYPE_SIZE (gnu_record_type) : 0),
5561 (all_rep_and_size
5562 ? bitsize_zero_node : 0),
5565 DECL_INTERNAL_P (gnu_field) = 1;
5566 DECL_QUALIFIER (gnu_field) = gnu_qual;
5567 TREE_CHAIN (gnu_field) = gnu_variant_list;
5568 gnu_variant_list = gnu_field;
5571 /* We use to delete the empty variants from the end. However,
5572 we no longer do that because we need them to generate complete
5573 debugging information for the variant record. Otherwise,
5574 the union type definition will be missing the fields associated
5575 to these empty variants. */
5577 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
5578 if (gnu_variant_list)
5580 if (all_rep_and_size)
5582 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
5583 TYPE_SIZE_UNIT (gnu_union_type)
5584 = TYPE_SIZE_UNIT (gnu_record_type);
5587 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
5588 all_rep_and_size, false);
5590 gnu_union_field
5591 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
5592 packed,
5593 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
5594 all_rep ? bitsize_zero_node : 0, 0);
5596 DECL_INTERNAL_P (gnu_union_field) = 1;
5597 TREE_CHAIN (gnu_union_field) = gnu_field_list;
5598 gnu_field_list = gnu_union_field;
5602 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
5603 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
5604 in a separate pass since we want to handle the discriminants but can't
5605 play with them until we've used them in debugging data above.
5607 ??? Note: if we then reorder them, debugging information will be wrong,
5608 but there's nothing that can be done about this at the moment. */
5610 for (gnu_field = gnu_field_list, gnu_last = NULL_TREE; gnu_field; )
5612 if (DECL_FIELD_OFFSET (gnu_field))
5614 tree gnu_next = TREE_CHAIN (gnu_field);
5616 if (!gnu_last)
5617 gnu_field_list = gnu_next;
5618 else
5619 TREE_CHAIN (gnu_last) = gnu_next;
5621 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
5622 gnu_our_rep_list = gnu_field;
5623 gnu_field = gnu_next;
5625 else
5627 gnu_last = gnu_field;
5628 gnu_field = TREE_CHAIN (gnu_field);
5632 /* If we have any items in our rep'ed field list, it is not the case that all
5633 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
5634 set it and ignore the items. Otherwise, sort the fields by bit position
5635 and put them into their own record if we have any fields without
5636 rep clauses. */
5637 if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
5638 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
5639 else if (gnu_our_rep_list)
5641 tree gnu_rep_type
5642 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
5643 int len = list_length (gnu_our_rep_list);
5644 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
5645 int i;
5647 /* Set/abuse DECL_FCONTEXT to increasing integers so we have a
5648 stable sort. */
5649 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
5650 gnu_field = TREE_CHAIN (gnu_field), i++)
5652 gnu_arr[i] = gnu_field;
5653 DECL_FCONTEXT (gnu_field) = size_int (i);
5656 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
5658 /* Put the fields in the list in order of increasing position, which
5659 means we start from the end. */
5660 gnu_our_rep_list = NULL_TREE;
5661 for (i = len - 1; i >= 0; i--)
5663 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
5664 gnu_our_rep_list = gnu_arr[i];
5665 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
5666 DECL_FCONTEXT (gnu_arr[i]) = NULL_TREE;
5669 if (gnu_field_list)
5671 finish_record_type (gnu_rep_type, gnu_our_rep_list, true, false);
5672 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
5673 gnu_record_type, 0, 0, 0, 1);
5674 DECL_INTERNAL_P (gnu_field) = 1;
5675 gnu_field_list = chainon (gnu_field_list, gnu_field);
5677 else
5679 layout_with_rep = true;
5680 gnu_field_list = nreverse (gnu_our_rep_list);
5684 if (cancel_alignment)
5685 TYPE_ALIGN (gnu_record_type) = 0;
5687 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
5688 layout_with_rep, defer_debug);
5691 /* Called via qsort from the above. Returns -1, 1, depending on the
5692 bit positions and ordinals of the two fields. */
5694 static int
5695 compare_field_bitpos (const PTR rt1, const PTR rt2)
5697 tree *t1 = (tree *) rt1;
5698 tree *t2 = (tree *) rt2;
5700 if (tree_int_cst_equal (bit_position (*t1), bit_position (*t2)))
5701 return
5702 (tree_int_cst_lt (DECL_FCONTEXT (*t1), DECL_FCONTEXT (*t2))
5703 ? -1 : 1);
5704 else if (tree_int_cst_lt (bit_position (*t1), bit_position (*t2)))
5705 return -1;
5706 else
5707 return 1;
5710 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
5711 placed into an Esize, Component_Bit_Offset, or Component_Size value
5712 in the GNAT tree. */
5714 static Uint
5715 annotate_value (tree gnu_size)
5717 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
5718 TCode tcode;
5719 Node_Ref_Or_Val ops[3], ret;
5720 int i;
5721 int size;
5723 /* See if we've already saved the value for this node. */
5724 if (EXPR_P (gnu_size) && TREE_COMPLEXITY (gnu_size))
5725 return (Node_Ref_Or_Val) TREE_COMPLEXITY (gnu_size);
5727 /* If we do not return inside this switch, TCODE will be set to the
5728 code to use for a Create_Node operand and LEN (set above) will be
5729 the number of recursive calls for us to make. */
5731 switch (TREE_CODE (gnu_size))
5733 case INTEGER_CST:
5734 if (TREE_OVERFLOW (gnu_size))
5735 return No_Uint;
5737 /* This may have come from a conversion from some smaller type,
5738 so ensure this is in bitsizetype. */
5739 gnu_size = convert (bitsizetype, gnu_size);
5741 /* For negative values, use NEGATE_EXPR of the supplied value. */
5742 if (tree_int_cst_sgn (gnu_size) < 0)
5744 /* The ridiculous code below is to handle the case of the largest
5745 negative integer. */
5746 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
5747 bool adjust = false;
5748 tree temp;
5750 if (TREE_CONSTANT_OVERFLOW (negative_size))
5752 negative_size
5753 = size_binop (MINUS_EXPR, bitsize_zero_node,
5754 size_binop (PLUS_EXPR, gnu_size,
5755 bitsize_one_node));
5756 adjust = true;
5759 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
5760 if (adjust)
5761 temp = build2 (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
5763 return annotate_value (temp);
5766 if (!host_integerp (gnu_size, 1))
5767 return No_Uint;
5769 size = tree_low_cst (gnu_size, 1);
5771 /* This peculiar test is to make sure that the size fits in an int
5772 on machines where HOST_WIDE_INT is not "int". */
5773 if (tree_low_cst (gnu_size, 1) == size)
5774 return UI_From_Int (size);
5775 else
5776 return No_Uint;
5778 case COMPONENT_REF:
5779 /* The only case we handle here is a simple discriminant reference. */
5780 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
5781 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
5782 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
5783 return Create_Node (Discrim_Val,
5784 annotate_value (DECL_DISCRIMINANT_NUMBER
5785 (TREE_OPERAND (gnu_size, 1))),
5786 No_Uint, No_Uint);
5787 else
5788 return No_Uint;
5790 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
5791 return annotate_value (TREE_OPERAND (gnu_size, 0));
5793 /* Now just list the operations we handle. */
5794 case COND_EXPR: tcode = Cond_Expr; break;
5795 case PLUS_EXPR: tcode = Plus_Expr; break;
5796 case MINUS_EXPR: tcode = Minus_Expr; break;
5797 case MULT_EXPR: tcode = Mult_Expr; break;
5798 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
5799 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
5800 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
5801 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
5802 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
5803 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
5804 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
5805 case NEGATE_EXPR: tcode = Negate_Expr; break;
5806 case MIN_EXPR: tcode = Min_Expr; break;
5807 case MAX_EXPR: tcode = Max_Expr; break;
5808 case ABS_EXPR: tcode = Abs_Expr; break;
5809 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
5810 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
5811 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
5812 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
5813 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
5814 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
5815 case LT_EXPR: tcode = Lt_Expr; break;
5816 case LE_EXPR: tcode = Le_Expr; break;
5817 case GT_EXPR: tcode = Gt_Expr; break;
5818 case GE_EXPR: tcode = Ge_Expr; break;
5819 case EQ_EXPR: tcode = Eq_Expr; break;
5820 case NE_EXPR: tcode = Ne_Expr; break;
5822 default:
5823 return No_Uint;
5826 /* Now get each of the operands that's relevant for this code. If any
5827 cannot be expressed as a repinfo node, say we can't. */
5828 for (i = 0; i < 3; i++)
5829 ops[i] = No_Uint;
5831 for (i = 0; i < len; i++)
5833 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
5834 if (ops[i] == No_Uint)
5835 return No_Uint;
5838 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
5839 TREE_COMPLEXITY (gnu_size) = ret;
5840 return ret;
5843 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
5844 GCC type, set Component_Bit_Offset and Esize to the position and size
5845 used by Gigi. */
5847 static void
5848 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
5850 tree gnu_list;
5851 tree gnu_entry;
5852 Entity_Id gnat_field;
5854 /* We operate by first making a list of all fields and their positions
5855 (we can get the sizes easily at any time) by a recursive call
5856 and then update all the sizes into the tree. */
5857 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
5858 size_zero_node, bitsize_zero_node,
5859 BIGGEST_ALIGNMENT);
5861 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
5862 gnat_field = Next_Entity (gnat_field))
5863 if ((Ekind (gnat_field) == E_Component
5864 || (Ekind (gnat_field) == E_Discriminant
5865 && !Is_Unchecked_Union (Scope (gnat_field)))))
5867 tree parent_offset = bitsize_zero_node;
5869 gnu_entry = purpose_member (gnat_to_gnu_field_decl (gnat_field),
5870 gnu_list);
5872 if (gnu_entry)
5874 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
5876 /* In this mode the tag and parent components have not been
5877 generated, so we add the appropriate offset to each
5878 component. For a component appearing in the current
5879 extension, the offset is the size of the parent. */
5880 if (Is_Derived_Type (gnat_entity)
5881 && Original_Record_Component (gnat_field) == gnat_field)
5882 parent_offset
5883 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
5884 bitsizetype);
5885 else
5886 parent_offset = bitsize_int (POINTER_SIZE);
5889 Set_Component_Bit_Offset
5890 (gnat_field,
5891 annotate_value
5892 (size_binop (PLUS_EXPR,
5893 bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
5894 TREE_VALUE (TREE_VALUE
5895 (TREE_VALUE (gnu_entry)))),
5896 parent_offset)));
5898 Set_Esize (gnat_field,
5899 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
5901 else if (type_annotate_only
5902 && Is_Tagged_Type (gnat_entity)
5903 && Is_Derived_Type (gnat_entity))
5905 /* If there is no gnu_entry, this is an inherited component whose
5906 position is the same as in the parent type. */
5907 Set_Component_Bit_Offset
5908 (gnat_field,
5909 Component_Bit_Offset (Original_Record_Component (gnat_field)));
5910 Set_Esize (gnat_field,
5911 Esize (Original_Record_Component (gnat_field)));
5916 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
5917 FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
5918 position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
5919 placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position. GNU_POS is
5920 to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
5921 the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
5922 so far. */
5924 static tree
5925 compute_field_positions (tree gnu_type, tree gnu_list, tree gnu_pos,
5926 tree gnu_bitpos, unsigned int offset_align)
5928 tree gnu_field;
5929 tree gnu_result = gnu_list;
5931 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
5932 gnu_field = TREE_CHAIN (gnu_field))
5934 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
5935 DECL_FIELD_BIT_OFFSET (gnu_field));
5936 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
5937 DECL_FIELD_OFFSET (gnu_field));
5938 unsigned int our_offset_align
5939 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
5941 gnu_result
5942 = tree_cons (gnu_field,
5943 tree_cons (gnu_our_offset,
5944 tree_cons (size_int (our_offset_align),
5945 gnu_our_bitpos, NULL_TREE),
5946 NULL_TREE),
5947 gnu_result);
5949 if (DECL_INTERNAL_P (gnu_field))
5950 gnu_result
5951 = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
5952 gnu_our_offset, gnu_our_bitpos,
5953 our_offset_align);
5956 return gnu_result;
5959 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
5960 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
5961 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
5962 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
5963 for the size of a field. COMPONENT_P is true if we are being called
5964 to process the Component_Size of GNAT_OBJECT. This is used for error
5965 message handling and to indicate to use the object size of GNU_TYPE.
5966 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
5967 it means that a size of zero should be treated as an unspecified size. */
5969 static tree
5970 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
5971 enum tree_code kind, bool component_p, bool zero_ok)
5973 Node_Id gnat_error_node;
5974 tree type_size
5975 = kind == VAR_DECL ? TYPE_SIZE (gnu_type) : rm_size (gnu_type);
5976 tree size;
5978 /* Find the node to use for errors. */
5979 if ((Ekind (gnat_object) == E_Component
5980 || Ekind (gnat_object) == E_Discriminant)
5981 && Present (Component_Clause (gnat_object)))
5982 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
5983 else if (Present (Size_Clause (gnat_object)))
5984 gnat_error_node = Expression (Size_Clause (gnat_object));
5985 else
5986 gnat_error_node = gnat_object;
5988 /* Return 0 if no size was specified, either because Esize was not Present or
5989 the specified size was zero. */
5990 if (No (uint_size) || uint_size == No_Uint)
5991 return NULL_TREE;
5993 /* Get the size as a tree. Give an error if a size was specified, but cannot
5994 be represented as in sizetype. */
5995 size = UI_To_gnu (uint_size, bitsizetype);
5996 if (TREE_OVERFLOW (size))
5998 post_error_ne (component_p ? "component size of & is too large"
5999 : "size of & is too large",
6000 gnat_error_node, gnat_object);
6001 return NULL_TREE;
6004 /* Ignore a negative size since that corresponds to our back-annotation.
6005 Also ignore a zero size unless a size clause exists. */
6006 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
6007 return NULL_TREE;
6009 /* The size of objects is always a multiple of a byte. */
6010 if (kind == VAR_DECL
6011 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
6013 if (component_p)
6014 post_error_ne ("component size for& is not a multiple of Storage_Unit",
6015 gnat_error_node, gnat_object);
6016 else
6017 post_error_ne ("size for& is not a multiple of Storage_Unit",
6018 gnat_error_node, gnat_object);
6019 return NULL_TREE;
6022 /* If this is an integral type or a packed array type, the front-end has
6023 verified the size, so we need not do it here (which would entail
6024 checking against the bounds). However, if this is an aliased object, it
6025 may not be smaller than the type of the object. */
6026 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
6027 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
6028 return size;
6030 /* If the object is a record that contains a template, add the size of
6031 the template to the specified size. */
6032 if (TREE_CODE (gnu_type) == RECORD_TYPE
6033 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6034 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
6036 /* Modify the size of the type to be that of the maximum size if it has a
6037 discriminant or the size of a thin pointer if this is a fat pointer. */
6038 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
6039 type_size = max_size (type_size, true);
6040 else if (TYPE_FAT_POINTER_P (gnu_type))
6041 type_size = bitsize_int (POINTER_SIZE);
6043 /* If this is an access type, the minimum size is that given by the smallest
6044 integral mode that's valid for pointers. */
6045 if (TREE_CODE (gnu_type) == POINTER_TYPE)
6047 enum machine_mode p_mode;
6049 for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
6050 !targetm.valid_pointer_mode (p_mode);
6051 p_mode = GET_MODE_WIDER_MODE (p_mode))
6054 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
6057 /* If the size of the object is a constant, the new size must not be
6058 smaller. */
6059 if (TREE_CODE (type_size) != INTEGER_CST
6060 || TREE_OVERFLOW (type_size)
6061 || tree_int_cst_lt (size, type_size))
6063 if (component_p)
6064 post_error_ne_tree
6065 ("component size for& too small{, minimum allowed is ^}",
6066 gnat_error_node, gnat_object, type_size);
6067 else
6068 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
6069 gnat_error_node, gnat_object, type_size);
6071 if (kind == VAR_DECL && !component_p
6072 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
6073 && !tree_int_cst_lt (size, rm_size (gnu_type)))
6074 post_error_ne_tree_2
6075 ("\\size of ^ is not a multiple of alignment (^ bits)",
6076 gnat_error_node, gnat_object, rm_size (gnu_type),
6077 TYPE_ALIGN (gnu_type));
6079 else if (INTEGRAL_TYPE_P (gnu_type))
6080 post_error_ne ("\\size would be legal if & were not aliased!",
6081 gnat_error_node, gnat_object);
6083 return NULL_TREE;
6086 return size;
6089 /* Similarly, but both validate and process a value of RM_Size. This
6090 routine is only called for types. */
6092 static void
6093 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
6095 /* Only give an error if a Value_Size clause was explicitly given.
6096 Otherwise, we'd be duplicating an error on the Size clause. */
6097 Node_Id gnat_attr_node
6098 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
6099 tree old_size = rm_size (gnu_type);
6100 tree size;
6102 /* Get the size as a tree. Do nothing if none was specified, either
6103 because RM_Size was not Present or if the specified size was zero.
6104 Give an error if a size was specified, but cannot be represented as
6105 in sizetype. */
6106 if (No (uint_size) || uint_size == No_Uint)
6107 return;
6109 size = UI_To_gnu (uint_size, bitsizetype);
6110 if (TREE_OVERFLOW (size))
6112 if (Present (gnat_attr_node))
6113 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
6114 gnat_entity);
6116 return;
6119 /* Ignore a negative size since that corresponds to our back-annotation.
6120 Also ignore a zero size unless a size clause exists, a Value_Size
6121 clause exists, or this is an integer type, in which case the
6122 front end will have always set it. */
6123 else if (tree_int_cst_sgn (size) < 0
6124 || (integer_zerop (size) && No (gnat_attr_node)
6125 && !Has_Size_Clause (gnat_entity)
6126 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
6127 return;
6129 /* If the old size is self-referential, get the maximum size. */
6130 if (CONTAINS_PLACEHOLDER_P (old_size))
6131 old_size = max_size (old_size, true);
6133 /* If the size of the object is a constant, the new size must not be
6134 smaller (the front end checks this for scalar types). */
6135 if (TREE_CODE (old_size) != INTEGER_CST
6136 || TREE_OVERFLOW (old_size)
6137 || (AGGREGATE_TYPE_P (gnu_type)
6138 && tree_int_cst_lt (size, old_size)))
6140 if (Present (gnat_attr_node))
6141 post_error_ne_tree
6142 ("Value_Size for& too small{, minimum allowed is ^}",
6143 gnat_attr_node, gnat_entity, old_size);
6145 return;
6148 /* Otherwise, set the RM_Size. */
6149 if (TREE_CODE (gnu_type) == INTEGER_TYPE
6150 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
6151 TYPE_RM_SIZE_NUM (gnu_type) = size;
6152 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
6153 TYPE_RM_SIZE_NUM (gnu_type) = size;
6154 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6155 || TREE_CODE (gnu_type) == UNION_TYPE
6156 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6157 && !TYPE_IS_FAT_POINTER_P (gnu_type))
6158 SET_TYPE_ADA_SIZE (gnu_type, size);
6161 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
6162 If TYPE is the best type, return it. Otherwise, make a new type. We
6163 only support new integral and pointer types. BIASED_P is nonzero if
6164 we are making a biased type. */
6166 static tree
6167 make_type_from_size (tree type, tree size_tree, bool biased_p)
6169 tree new_type;
6170 unsigned HOST_WIDE_INT size;
6171 bool unsigned_p;
6173 /* If size indicates an error, just return TYPE to avoid propagating the
6174 error. Likewise if it's too large to represent. */
6175 if (!size_tree || !host_integerp (size_tree, 1))
6176 return type;
6178 size = tree_low_cst (size_tree, 1);
6179 switch (TREE_CODE (type))
6181 case INTEGER_TYPE:
6182 case ENUMERAL_TYPE:
6183 /* Only do something if the type is not already the proper size and is
6184 not a packed array type. */
6185 if (TYPE_PACKED_ARRAY_TYPE_P (type)
6186 || (TYPE_PRECISION (type) == size
6187 && biased_p == (TREE_CODE (type) == INTEGER_CST
6188 && TYPE_BIASED_REPRESENTATION_P (type))))
6189 break;
6191 biased_p |= (TREE_CODE (type) == INTEGER_TYPE
6192 && TYPE_BIASED_REPRESENTATION_P (type));
6193 unsigned_p = TYPE_UNSIGNED (type) || biased_p;
6195 size = MIN (size, LONG_LONG_TYPE_SIZE);
6196 new_type
6197 = unsigned_p ? make_unsigned_type (size) : make_signed_type (size);
6198 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
6199 TYPE_MIN_VALUE (new_type)
6200 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
6201 TYPE_MAX_VALUE (new_type)
6202 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
6203 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
6204 TYPE_RM_SIZE_NUM (new_type) = bitsize_int (size);
6205 return new_type;
6207 case RECORD_TYPE:
6208 /* Do something if this is a fat pointer, in which case we
6209 may need to return the thin pointer. */
6210 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
6211 return
6212 build_pointer_type
6213 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
6214 break;
6216 case POINTER_TYPE:
6217 /* Only do something if this is a thin pointer, in which case we
6218 may need to return the fat pointer. */
6219 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
6220 return
6221 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
6223 break;
6225 default:
6226 break;
6229 return type;
6232 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
6233 a type or object whose present alignment is ALIGN. If this alignment is
6234 valid, return it. Otherwise, give an error and return ALIGN. */
6236 static unsigned int
6237 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
6239 Node_Id gnat_error_node = gnat_entity;
6240 unsigned int new_align;
6242 #ifndef MAX_OFILE_ALIGNMENT
6243 #define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
6244 #endif
6246 if (Present (Alignment_Clause (gnat_entity)))
6247 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
6249 /* Don't worry about checking alignment if alignment was not specified
6250 by the source program and we already posted an error for this entity. */
6252 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
6253 return align;
6255 /* Within GCC, an alignment is an integer, so we must make sure a
6256 value is specified that fits in that range. Also, alignments of
6257 more than MAX_OFILE_ALIGNMENT can't be supported. */
6259 if (! UI_Is_In_Int_Range (alignment)
6260 || ((new_align = UI_To_Int (alignment))
6261 > MAX_OFILE_ALIGNMENT / BITS_PER_UNIT))
6262 post_error_ne_num ("largest supported alignment for& is ^",
6263 gnat_error_node, gnat_entity,
6264 MAX_OFILE_ALIGNMENT / BITS_PER_UNIT);
6265 else if (!(Present (Alignment_Clause (gnat_entity))
6266 && From_At_Mod (Alignment_Clause (gnat_entity)))
6267 && new_align * BITS_PER_UNIT < align)
6268 post_error_ne_num ("alignment for& must be at least ^",
6269 gnat_error_node, gnat_entity,
6270 align / BITS_PER_UNIT);
6271 else
6272 align = MAX (align, new_align == 0 ? 1 : new_align * BITS_PER_UNIT);
6274 return align;
6277 /* Verify that OBJECT, a type or decl, is something we can implement
6278 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
6279 if we require atomic components. */
6281 static void
6282 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
6284 Node_Id gnat_error_point = gnat_entity;
6285 Node_Id gnat_node;
6286 enum machine_mode mode;
6287 unsigned int align;
6288 tree size;
6290 /* There are three case of what OBJECT can be. It can be a type, in which
6291 case we take the size, alignment and mode from the type. It can be a
6292 declaration that was indirect, in which case the relevant values are
6293 that of the type being pointed to, or it can be a normal declaration,
6294 in which case the values are of the decl. The code below assumes that
6295 OBJECT is either a type or a decl. */
6296 if (TYPE_P (object))
6298 mode = TYPE_MODE (object);
6299 align = TYPE_ALIGN (object);
6300 size = TYPE_SIZE (object);
6302 else if (DECL_BY_REF_P (object))
6304 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
6305 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
6306 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
6308 else
6310 mode = DECL_MODE (object);
6311 align = DECL_ALIGN (object);
6312 size = DECL_SIZE (object);
6315 /* Consider all floating-point types atomic and any types that that are
6316 represented by integers no wider than a machine word. */
6317 if (GET_MODE_CLASS (mode) == MODE_FLOAT
6318 || ((GET_MODE_CLASS (mode) == MODE_INT
6319 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
6320 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
6321 return;
6323 /* For the moment, also allow anything that has an alignment equal
6324 to its size and which is smaller than a word. */
6325 if (size && TREE_CODE (size) == INTEGER_CST
6326 && compare_tree_int (size, align) == 0
6327 && align <= BITS_PER_WORD)
6328 return;
6330 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
6331 gnat_node = Next_Rep_Item (gnat_node))
6333 if (!comp_p && Nkind (gnat_node) == N_Pragma
6334 && Get_Pragma_Id (Chars (gnat_node)) == Pragma_Atomic)
6335 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6336 else if (comp_p && Nkind (gnat_node) == N_Pragma
6337 && (Get_Pragma_Id (Chars (gnat_node))
6338 == Pragma_Atomic_Components))
6339 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6342 if (comp_p)
6343 post_error_ne ("atomic access to component of & cannot be guaranteed",
6344 gnat_error_point, gnat_entity);
6345 else
6346 post_error_ne ("atomic access to & cannot be guaranteed",
6347 gnat_error_point, gnat_entity);
6350 /* Check if FTYPE1 and FTYPE2, two potentially different function type nodes,
6351 have compatible signatures so that a call using one type may be safely
6352 issued if the actual target function type is the other. Return 1 if it is
6353 the case, 0 otherwise, and post errors on the incompatibilities.
6355 This is used when an Ada subprogram is mapped onto a GCC builtin, to ensure
6356 that calls to the subprogram will have arguments suitable for the later
6357 underlying builtin expansion. */
6359 static int
6360 compatible_signatures_p (tree ftype1, tree ftype2)
6362 /* As of now, we only perform very trivial tests and consider it's the
6363 programmer's responsibility to ensure the type correctness in the Ada
6364 declaration, as in the regular Import cases.
6366 Mismatches typically result in either error messages from the builtin
6367 expander, internal compiler errors, or in a real call sequence. This
6368 should be refined to issue diagnostics helping error detection and
6369 correction. */
6371 /* Almost fake test, ensuring a use of each argument. */
6372 if (ftype1 == ftype2)
6373 return 1;
6375 return 1;
6378 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a new type
6379 with all size expressions that contain F updated by replacing F with R.
6380 This is identical to GCC's substitute_in_type except that it knows about
6381 TYPE_INDEX_TYPE. If F is NULL_TREE, always make a new RECORD_TYPE, even if
6382 nothing has changed. */
6384 tree
6385 gnat_substitute_in_type (tree t, tree f, tree r)
6387 tree new = t;
6388 tree tem;
6390 switch (TREE_CODE (t))
6392 case INTEGER_TYPE:
6393 case ENUMERAL_TYPE:
6394 case BOOLEAN_TYPE:
6395 case CHAR_TYPE:
6396 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6397 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
6399 tree low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
6400 tree high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
6402 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6403 return t;
6405 new = build_range_type (TREE_TYPE (t), low, high);
6406 if (TYPE_INDEX_TYPE (t))
6407 SET_TYPE_INDEX_TYPE
6408 (new, gnat_substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
6409 return new;
6412 return t;
6414 case REAL_TYPE:
6415 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6416 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
6418 tree low = NULL_TREE, high = NULL_TREE;
6420 if (TYPE_MIN_VALUE (t))
6421 low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
6422 if (TYPE_MAX_VALUE (t))
6423 high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
6425 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6426 return t;
6428 t = copy_type (t);
6429 TYPE_MIN_VALUE (t) = low;
6430 TYPE_MAX_VALUE (t) = high;
6432 return t;
6434 case COMPLEX_TYPE:
6435 tem = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6436 if (tem == TREE_TYPE (t))
6437 return t;
6439 return build_complex_type (tem);
6441 case OFFSET_TYPE:
6442 case METHOD_TYPE:
6443 case FUNCTION_TYPE:
6444 case LANG_TYPE:
6445 /* Don't know how to do these yet. */
6446 gcc_unreachable ();
6448 case ARRAY_TYPE:
6450 tree component = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6451 tree domain = gnat_substitute_in_type (TYPE_DOMAIN (t), f, r);
6453 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6454 return t;
6456 new = build_array_type (component, domain);
6457 TYPE_SIZE (new) = 0;
6458 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
6459 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
6460 layout_type (new);
6461 TYPE_ALIGN (new) = TYPE_ALIGN (t);
6463 /* If we had bounded the sizes of T by a constant, bound the sizes of
6464 NEW by the same constant. */
6465 if (TREE_CODE (TYPE_SIZE (t)) == MIN_EXPR)
6466 TYPE_SIZE (new)
6467 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE (t), 1),
6468 TYPE_SIZE (new));
6469 if (TREE_CODE (TYPE_SIZE_UNIT (t)) == MIN_EXPR)
6470 TYPE_SIZE_UNIT (new)
6471 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE_UNIT (t), 1),
6472 TYPE_SIZE_UNIT (new));
6473 return new;
6476 case RECORD_TYPE:
6477 case UNION_TYPE:
6478 case QUAL_UNION_TYPE:
6480 tree field;
6481 bool changed_field
6482 = (f == NULL_TREE && !TREE_CONSTANT (TYPE_SIZE (t)));
6483 bool field_has_rep = false;
6484 tree last_field = NULL_TREE;
6486 tree new = copy_type (t);
6488 /* Start out with no fields, make new fields, and chain them
6489 in. If we haven't actually changed the type of any field,
6490 discard everything we've done and return the old type. */
6492 TYPE_FIELDS (new) = NULL_TREE;
6493 TYPE_SIZE (new) = NULL_TREE;
6495 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6497 tree new_field = copy_node (field);
6499 TREE_TYPE (new_field)
6500 = gnat_substitute_in_type (TREE_TYPE (new_field), f, r);
6502 if (DECL_HAS_REP_P (field) && !DECL_INTERNAL_P (field))
6503 field_has_rep = true;
6504 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
6505 changed_field = true;
6507 /* If this is an internal field and the type of this field is
6508 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
6509 the type just has one element, treat that as the field.
6510 But don't do this if we are processing a QUAL_UNION_TYPE. */
6511 if (TREE_CODE (t) != QUAL_UNION_TYPE
6512 && DECL_INTERNAL_P (new_field)
6513 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
6514 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
6516 if (!TYPE_FIELDS (TREE_TYPE (new_field)))
6517 continue;
6519 if (!TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))))
6521 tree next_new_field
6522 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
6524 /* Make sure omitting the union doesn't change
6525 the layout. */
6526 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
6527 new_field = next_new_field;
6531 DECL_CONTEXT (new_field) = new;
6532 SET_DECL_ORIGINAL_FIELD (new_field,
6533 (DECL_ORIGINAL_FIELD (field)
6534 ? DECL_ORIGINAL_FIELD (field) : field));
6536 /* If the size of the old field was set at a constant,
6537 propagate the size in case the type's size was variable.
6538 (This occurs in the case of a variant or discriminated
6539 record with a default size used as a field of another
6540 record.) */
6541 DECL_SIZE (new_field)
6542 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
6543 ? DECL_SIZE (field) : NULL_TREE;
6544 DECL_SIZE_UNIT (new_field)
6545 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
6546 ? DECL_SIZE_UNIT (field) : NULL_TREE;
6548 if (TREE_CODE (t) == QUAL_UNION_TYPE)
6550 tree new_q = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
6552 if (new_q != DECL_QUALIFIER (new_field))
6553 changed_field = true;
6555 /* Do the substitution inside the qualifier and if we find
6556 that this field will not be present, omit it. */
6557 DECL_QUALIFIER (new_field) = new_q;
6559 if (integer_zerop (DECL_QUALIFIER (new_field)))
6560 continue;
6563 if (!last_field)
6564 TYPE_FIELDS (new) = new_field;
6565 else
6566 TREE_CHAIN (last_field) = new_field;
6568 last_field = new_field;
6570 /* If this is a qualified type and this field will always be
6571 present, we are done. */
6572 if (TREE_CODE (t) == QUAL_UNION_TYPE
6573 && integer_onep (DECL_QUALIFIER (new_field)))
6574 break;
6577 /* If this used to be a qualified union type, but we now know what
6578 field will be present, make this a normal union. */
6579 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
6580 && (!TYPE_FIELDS (new)
6581 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
6582 TREE_SET_CODE (new, UNION_TYPE);
6583 else if (!changed_field)
6584 return t;
6586 gcc_assert (!field_has_rep);
6587 layout_type (new);
6589 /* If the size was originally a constant use it. */
6590 if (TYPE_SIZE (t) && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
6591 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
6593 TYPE_SIZE (new) = TYPE_SIZE (t);
6594 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
6595 SET_TYPE_ADA_SIZE (new, TYPE_ADA_SIZE (t));
6598 return new;
6601 default:
6602 return t;
6606 /* Return the "RM size" of GNU_TYPE. This is the actual number of bits
6607 needed to represent the object. */
6609 tree
6610 rm_size (tree gnu_type)
6612 /* For integer types, this is the precision. For record types, we store
6613 the size explicitly. For other types, this is just the size. */
6615 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
6616 return TYPE_RM_SIZE (gnu_type);
6617 else if (TREE_CODE (gnu_type) == RECORD_TYPE
6618 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6619 /* Return the rm_size of the actual data plus the size of the template. */
6620 return
6621 size_binop (PLUS_EXPR,
6622 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
6623 DECL_SIZE (TYPE_FIELDS (gnu_type)));
6624 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6625 || TREE_CODE (gnu_type) == UNION_TYPE
6626 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6627 && !TYPE_IS_FAT_POINTER_P (gnu_type)
6628 && TYPE_ADA_SIZE (gnu_type))
6629 return TYPE_ADA_SIZE (gnu_type);
6630 else
6631 return TYPE_SIZE (gnu_type);
6634 /* Return an identifier representing the external name to be used for
6635 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
6636 and the specified suffix. */
6638 tree
6639 create_concat_name (Entity_Id gnat_entity, const char *suffix)
6641 const char *str = (!suffix ? "" : suffix);
6642 String_Template temp = {1, strlen (str)};
6643 Fat_Pointer fp = {str, &temp};
6645 Get_External_Name_With_Suffix (gnat_entity, fp);
6647 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
6648 /* A variable using the Stdcall convention (meaning we are running
6649 on a Windows box) live in a DLL. Here we adjust its name to use
6650 the jump-table, the _imp__NAME contains the address for the NAME
6651 variable. */
6653 Entity_Kind kind = Ekind (gnat_entity);
6654 const char *prefix = "_imp__";
6655 int plen = strlen (prefix);
6657 if ((kind == E_Variable || kind == E_Constant)
6658 && Convention (gnat_entity) == Convention_Stdcall)
6660 int k;
6661 for (k = 0; k <= Name_Len; k++)
6662 Name_Buffer [Name_Len - k + plen] = Name_Buffer [Name_Len - k];
6663 strncpy (Name_Buffer, prefix, plen);
6666 #endif
6668 return get_identifier (Name_Buffer);
6671 /* Return the name to be used for GNAT_ENTITY. If a type, create a
6672 fully-qualified name, possibly with type information encoding.
6673 Otherwise, return the name. */
6675 tree
6676 get_entity_name (Entity_Id gnat_entity)
6678 Get_Encoded_Name (gnat_entity);
6679 return get_identifier (Name_Buffer);
6682 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
6683 string, return a new IDENTIFIER_NODE that is the concatenation of
6684 the name in GNU_ID and SUFFIX. */
6686 tree
6687 concat_id_with_name (tree gnu_id, const char *suffix)
6689 int len = IDENTIFIER_LENGTH (gnu_id);
6691 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id),
6692 IDENTIFIER_LENGTH (gnu_id));
6693 strncpy (Name_Buffer + len, "___", 3);
6694 len += 3;
6695 strcpy (Name_Buffer + len, suffix);
6696 return get_identifier (Name_Buffer);
6699 #include "gt-ada-decl.h"