2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / decl.c
bloba61c2f0f28e059f0349d817bed9b7a56d9ce5ffa
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2008, 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 3, 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 along with GCC; see the file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
20 * *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
23 * *
24 ****************************************************************************/
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "flags.h"
32 #include "toplev.h"
33 #include "convert.h"
34 #include "ggc.h"
35 #include "obstack.h"
36 #include "target.h"
37 #include "expr.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 "hashtab.h"
53 #include "ada-tree.h"
54 #include "gigi.h"
56 #ifndef MAX_FIXED_MODE_SIZE
57 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
58 #endif
60 /* Convention_Stdcall should be processed in a specific way on Windows targets
61 only. The macro below is a helper to avoid having to check for a Windows
62 specific attribute throughout this unit. */
64 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
65 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
66 #else
67 #define Has_Stdcall_Convention(E) (0)
68 #endif
70 struct incomplete
72 struct incomplete *next;
73 tree old_type;
74 Entity_Id full_type;
77 /* These variables are used to defer recursively expanding incomplete types
78 while we are processing an array, a record or a subprogram type. */
79 static int defer_incomplete_level = 0;
80 static struct incomplete *defer_incomplete_list;
82 /* This variable is used to delay expanding From_With_Type types until the
83 end of the spec. */
84 static struct incomplete *defer_limited_with;
86 /* These variables are used to defer finalizing types. The element of the
87 list is the TYPE_DECL associated with the type. */
88 static int defer_finalize_level = 0;
89 static VEC (tree,heap) *defer_finalize_list;
91 /* A hash table used to cache the result of annotate_value. */
92 static GTY ((if_marked ("tree_int_map_marked_p"),
93 param_is (struct tree_int_map))) htab_t annotate_value_cache;
95 static void copy_alias_set (tree, tree);
96 static tree substitution_list (Entity_Id, Entity_Id, tree, bool);
97 static bool allocatable_size_p (tree, bool);
98 static void prepend_one_attribute_to (struct attrib **,
99 enum attr_type, tree, tree, Node_Id);
100 static void prepend_attributes (Entity_Id, struct attrib **);
101 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
102 static bool is_variable_size (tree);
103 static tree elaborate_expression_1 (Node_Id, Entity_Id, tree, tree,
104 bool, bool);
105 static tree make_packable_type (tree, bool);
106 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
107 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
108 bool *);
109 static bool same_discriminant_p (Entity_Id, Entity_Id);
110 static bool array_type_has_nonaliased_component (Entity_Id, tree);
111 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
112 bool, bool, bool, bool);
113 static Uint annotate_value (tree);
114 static void annotate_rep (Entity_Id, tree);
115 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
116 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
117 static void set_rm_size (Uint, tree, Entity_Id);
118 static tree make_type_from_size (tree, tree, bool);
119 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
120 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
121 static void check_ok_for_atomic (tree, Entity_Id, bool);
122 static int compatible_signatures_p (tree ftype1, tree ftype2);
123 static void rest_of_type_decl_compilation_no_defer (tree);
125 /* Return true if GNAT_ADDRESS is a compile time known value.
126 In particular catch System'To_Address. */
128 static bool
129 compile_time_known_address_p (Node_Id gnat_address)
131 return ((Nkind (gnat_address) == N_Unchecked_Type_Conversion
132 && Compile_Time_Known_Value (Expression (gnat_address)))
133 || Compile_Time_Known_Value (gnat_address));
136 /* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
137 GCC type corresponding to that entity. GNAT_ENTITY is assumed to
138 refer to an Ada type. */
140 tree
141 gnat_to_gnu_type (Entity_Id gnat_entity)
143 tree gnu_decl;
145 /* The back end never attempts to annotate generic types */
146 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
147 return void_type_node;
149 /* Convert the ada entity type into a GCC TYPE_DECL node. */
150 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
151 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
152 return TREE_TYPE (gnu_decl);
155 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
156 entity, this routine returns the equivalent GCC tree for that entity
157 (an ..._DECL node) and associates the ..._DECL node with the input GNAT
158 defining identifier.
160 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
161 initial value (in GCC tree form). This is optional for variables.
162 For renamed entities, GNU_EXPR gives the object being renamed.
164 DEFINITION is nonzero if this call is intended for a definition. This is
165 used for separate compilation where it necessary to know whether an
166 external declaration or a definition should be created if the GCC equivalent
167 was not created previously. The value of 1 is normally used for a nonzero
168 DEFINITION, but a value of 2 is used in special circumstances, defined in
169 the code. */
171 tree
172 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
174 Entity_Id gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
175 tree gnu_entity_id;
176 tree gnu_type = NULL_TREE;
177 /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
178 GNAT tree. This node will be associated with the GNAT node by calling
179 the save_gnu_tree routine at the end of the `switch' statement. */
180 tree gnu_decl = NULL_TREE;
181 /* true if we have already saved gnu_decl as a gnat association. */
182 bool saved = false;
183 /* Nonzero if we incremented defer_incomplete_level. */
184 bool this_deferred = false;
185 /* Nonzero if we incremented force_global. */
186 bool this_global = false;
187 /* Nonzero if we should check to see if elaborated during processing. */
188 bool maybe_present = false;
189 /* Nonzero if we made GNU_DECL and its type here. */
190 bool this_made_decl = false;
191 struct attrib *attr_list = NULL;
192 bool debug_info_p = (Needs_Debug_Info (gnat_entity)
193 || debug_info_level == DINFO_LEVEL_VERBOSE);
194 Entity_Kind kind = Ekind (gnat_entity);
195 Entity_Id gnat_temp;
196 unsigned int esize
197 = ((Known_Esize (gnat_entity)
198 && UI_Is_In_Int_Range (Esize (gnat_entity)))
199 ? MIN (UI_To_Int (Esize (gnat_entity)),
200 IN (kind, Float_Kind)
201 ? fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE)
202 : IN (kind, Access_Kind) ? POINTER_SIZE * 2
203 : LONG_LONG_TYPE_SIZE)
204 : LONG_LONG_TYPE_SIZE);
205 tree gnu_size = 0;
206 bool imported_p
207 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
208 unsigned int align = 0;
210 /* Since a use of an Itype is a definition, process it as such if it
211 is not in a with'ed unit. */
213 if (!definition && Is_Itype (gnat_entity)
214 && !present_gnu_tree (gnat_entity)
215 && In_Extended_Main_Code_Unit (gnat_entity))
217 /* Ensure that we are in a subprogram mentioned in the Scope
218 chain of this entity, our current scope is global,
219 or that we encountered a task or entry (where we can't currently
220 accurately check scoping). */
221 if (!current_function_decl
222 || DECL_ELABORATION_PROC_P (current_function_decl))
224 process_type (gnat_entity);
225 return get_gnu_tree (gnat_entity);
228 for (gnat_temp = Scope (gnat_entity);
229 Present (gnat_temp); gnat_temp = Scope (gnat_temp))
231 if (Is_Type (gnat_temp))
232 gnat_temp = Underlying_Type (gnat_temp);
234 if (Ekind (gnat_temp) == E_Subprogram_Body)
235 gnat_temp
236 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
238 if (IN (Ekind (gnat_temp), Subprogram_Kind)
239 && Present (Protected_Body_Subprogram (gnat_temp)))
240 gnat_temp = Protected_Body_Subprogram (gnat_temp);
242 if (Ekind (gnat_temp) == E_Entry
243 || Ekind (gnat_temp) == E_Entry_Family
244 || Ekind (gnat_temp) == E_Task_Type
245 || (IN (Ekind (gnat_temp), Subprogram_Kind)
246 && present_gnu_tree (gnat_temp)
247 && (current_function_decl
248 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
250 process_type (gnat_entity);
251 return get_gnu_tree (gnat_entity);
255 /* This abort means the entity "gnat_entity" has an incorrect scope,
256 i.e. that its scope does not correspond to the subprogram in which
257 it is declared */
258 gcc_unreachable ();
261 /* If this is entity 0, something went badly wrong. */
262 gcc_assert (Present (gnat_entity));
264 /* If we've already processed this entity, return what we got last time.
265 If we are defining the node, we should not have already processed it.
266 In that case, we will abort below when we try to save a new GCC tree for
267 this object. We also need to handle the case of getting a dummy type
268 when a Full_View exists. */
270 if (present_gnu_tree (gnat_entity)
271 && (!definition || (Is_Type (gnat_entity) && imported_p)))
273 gnu_decl = get_gnu_tree (gnat_entity);
275 if (TREE_CODE (gnu_decl) == TYPE_DECL
276 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
277 && IN (kind, Incomplete_Or_Private_Kind)
278 && Present (Full_View (gnat_entity)))
280 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
281 NULL_TREE, 0);
283 save_gnu_tree (gnat_entity, NULL_TREE, false);
284 save_gnu_tree (gnat_entity, gnu_decl, false);
287 return gnu_decl;
290 /* If this is a numeric or enumeral type, or an access type, a nonzero
291 Esize must be specified unless it was specified by the programmer. */
292 gcc_assert (!Unknown_Esize (gnat_entity)
293 || Has_Size_Clause (gnat_entity)
294 || (!IN (kind, Numeric_Kind) && !IN (kind, Enumeration_Kind)
295 && (!IN (kind, Access_Kind)
296 || kind == E_Access_Protected_Subprogram_Type
297 || kind == E_Anonymous_Access_Protected_Subprogram_Type
298 || kind == E_Access_Subtype)));
300 /* Likewise, RM_Size must be specified for all discrete and fixed-point
301 types. */
302 gcc_assert (!IN (kind, Discrete_Or_Fixed_Point_Kind)
303 || !Unknown_RM_Size (gnat_entity));
305 /* Get the name of the entity and set up the line number and filename of
306 the original definition for use in any decl we make. */
307 gnu_entity_id = get_entity_name (gnat_entity);
308 Sloc_to_locus (Sloc (gnat_entity), &input_location);
310 /* If we get here, it means we have not yet done anything with this
311 entity. If we are not defining it here, it must be external,
312 otherwise we should have defined it already. */
313 gcc_assert (definition || Is_Public (gnat_entity) || type_annotate_only
314 || kind == E_Discriminant || kind == E_Component
315 || kind == E_Label
316 || (kind == E_Constant && Present (Full_View (gnat_entity)))
317 || IN (kind, Type_Kind));
319 /* For cases when we are not defining (i.e., we are referencing from
320 another compilation unit) Public entities, show we are at global level
321 for the purpose of computing scopes. Don't do this for components or
322 discriminants since the relevant test is whether or not the record is
323 being defined. But do this for Imported functions or procedures in
324 all cases. */
325 if ((!definition && Is_Public (gnat_entity)
326 && !Is_Statically_Allocated (gnat_entity)
327 && kind != E_Discriminant && kind != E_Component)
328 || (Is_Imported (gnat_entity)
329 && (kind == E_Function || kind == E_Procedure)))
330 force_global++, this_global = true;
332 /* Handle any attributes directly attached to the entity. */
333 if (Has_Gigi_Rep_Item (gnat_entity))
334 prepend_attributes (gnat_entity, &attr_list);
336 /* Machine_Attributes on types are expected to be propagated to subtypes.
337 The corresponding Gigi_Rep_Items are only attached to the first subtype
338 though, so we handle the propagation here. */
339 if (Is_Type (gnat_entity) && Base_Type (gnat_entity) != gnat_entity
340 && !Is_First_Subtype (gnat_entity)
341 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
342 prepend_attributes (First_Subtype (Base_Type (gnat_entity)), &attr_list);
344 switch (kind)
346 case E_Constant:
347 /* If this is a use of a deferred constant, get its full
348 declaration. */
349 if (!definition && Present (Full_View (gnat_entity)))
351 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
352 gnu_expr, 0);
353 saved = true;
354 break;
357 /* If we have an external constant that we are not defining, get the
358 expression that is was defined to represent. We may throw that
359 expression away later if it is not a constant. Do not retrieve the
360 expression if it is an aggregate or allocator, because in complex
361 instantiation contexts it may not be expanded */
362 if (!definition
363 && Present (Expression (Declaration_Node (gnat_entity)))
364 && !No_Initialization (Declaration_Node (gnat_entity))
365 && (Nkind (Expression (Declaration_Node (gnat_entity)))
366 != N_Aggregate)
367 && (Nkind (Expression (Declaration_Node (gnat_entity)))
368 != N_Allocator))
369 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
371 /* Ignore deferred constant definitions; they are processed fully in the
372 front-end. For deferred constant references get the full definition.
373 On the other hand, constants that are renamings are handled like
374 variable renamings. If No_Initialization is set, this is not a
375 deferred constant but a constant whose value is built manually. */
376 if (definition && !gnu_expr
377 && !No_Initialization (Declaration_Node (gnat_entity))
378 && No (Renamed_Object (gnat_entity)))
380 gnu_decl = error_mark_node;
381 saved = true;
382 break;
384 else if (!definition && IN (kind, Incomplete_Or_Private_Kind)
385 && Present (Full_View (gnat_entity)))
387 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
388 NULL_TREE, 0);
389 saved = true;
390 break;
393 goto object;
395 case E_Exception:
396 /* We used to special case VMS exceptions here to directly map them to
397 their associated condition code. Since this code had to be masked
398 dynamically to strip off the severity bits, this caused trouble in
399 the GCC/ZCX case because the "type" pointers we store in the tables
400 have to be static. We now don't special case here anymore, and let
401 the regular processing take place, which leaves us with a regular
402 exception data object for VMS exceptions too. The condition code
403 mapping is taken care of by the front end and the bitmasking by the
404 runtime library. */
405 goto object;
407 case E_Discriminant:
408 case E_Component:
410 /* The GNAT record where the component was defined. */
411 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
413 /* If the variable is an inherited record component (in the case of
414 extended record types), just return the inherited entity, which
415 must be a FIELD_DECL. Likewise for discriminants.
416 For discriminants of untagged records which have explicit
417 stored discriminants, return the entity for the corresponding
418 stored discriminant. Also use Original_Record_Component
419 if the record has a private extension. */
421 if (Present (Original_Record_Component (gnat_entity))
422 && Original_Record_Component (gnat_entity) != gnat_entity)
424 gnu_decl
425 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
426 gnu_expr, definition);
427 saved = true;
428 break;
431 /* If the enclosing record has explicit stored discriminants,
432 then it is an untagged record. If the Corresponding_Discriminant
433 is not empty then this must be a renamed discriminant and its
434 Original_Record_Component must point to the corresponding explicit
435 stored discriminant (i.e., we should have taken the previous
436 branch). */
438 else if (Present (Corresponding_Discriminant (gnat_entity))
439 && Is_Tagged_Type (gnat_record))
441 /* A tagged record has no explicit stored discriminants. */
443 gcc_assert (First_Discriminant (gnat_record)
444 == First_Stored_Discriminant (gnat_record));
445 gnu_decl
446 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
447 gnu_expr, definition);
448 saved = true;
449 break;
452 else if (Present (CR_Discriminant (gnat_entity))
453 && type_annotate_only)
455 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
456 gnu_expr, definition);
457 saved = true;
458 break;
461 /* If the enclosing record has explicit stored discriminants,
462 then it is an untagged record. If the Corresponding_Discriminant
463 is not empty then this must be a renamed discriminant and its
464 Original_Record_Component must point to the corresponding explicit
465 stored discriminant (i.e., we should have taken the first
466 branch). */
468 else if (Present (Corresponding_Discriminant (gnat_entity))
469 && (First_Discriminant (gnat_record)
470 != First_Stored_Discriminant (gnat_record)))
471 gcc_unreachable ();
473 /* Otherwise, if we are not defining this and we have no GCC type
474 for the containing record, make one for it. Then we should
475 have made our own equivalent. */
476 else if (!definition && !present_gnu_tree (gnat_record))
478 /* ??? If this is in a record whose scope is a protected
479 type and we have an Original_Record_Component, use it.
480 This is a workaround for major problems in protected type
481 handling. */
482 Entity_Id Scop = Scope (Scope (gnat_entity));
483 if ((Is_Protected_Type (Scop)
484 || (Is_Private_Type (Scop)
485 && Present (Full_View (Scop))
486 && Is_Protected_Type (Full_View (Scop))))
487 && Present (Original_Record_Component (gnat_entity)))
489 gnu_decl
490 = gnat_to_gnu_entity (Original_Record_Component
491 (gnat_entity),
492 gnu_expr, 0);
493 saved = true;
494 break;
497 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
498 gnu_decl = get_gnu_tree (gnat_entity);
499 saved = true;
500 break;
503 else
504 /* Here we have no GCC type and this is a reference rather than a
505 definition. This should never happen. Most likely the cause is a
506 reference before declaration in the gnat tree for gnat_entity. */
507 gcc_unreachable ();
510 case E_Loop_Parameter:
511 case E_Out_Parameter:
512 case E_Variable:
514 /* Simple variables, loop variables, Out parameters, and exceptions. */
515 object:
517 bool used_by_ref = false;
518 bool const_flag
519 = ((kind == E_Constant || kind == E_Variable)
520 && Is_True_Constant (gnat_entity)
521 && (((Nkind (Declaration_Node (gnat_entity))
522 == N_Object_Declaration)
523 && Present (Expression (Declaration_Node (gnat_entity))))
524 || Present (Renamed_Object (gnat_entity))));
525 bool inner_const_flag = const_flag;
526 bool static_p = Is_Statically_Allocated (gnat_entity);
527 bool mutable_p = false;
528 tree gnu_ext_name = NULL_TREE;
529 tree renamed_obj = NULL_TREE;
530 tree gnu_object_size;
532 if (Present (Renamed_Object (gnat_entity)) && !definition)
534 if (kind == E_Exception)
535 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
536 NULL_TREE, 0);
537 else
538 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
541 /* Get the type after elaborating the renamed object. */
542 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
544 /* For a debug renaming declaration, build a pure debug entity. */
545 if (Present (Debug_Renaming_Link (gnat_entity)))
547 rtx addr;
548 gnu_decl = build_decl (VAR_DECL, gnu_entity_id, gnu_type);
549 /* The (MEM (CONST (0))) pattern is prescribed by STABS. */
550 if (global_bindings_p ())
551 addr = gen_rtx_CONST (VOIDmode, const0_rtx);
552 else
553 addr = stack_pointer_rtx;
554 SET_DECL_RTL (gnu_decl, gen_rtx_MEM (Pmode, addr));
555 gnat_pushdecl (gnu_decl, gnat_entity);
556 break;
559 /* If this is a loop variable, its type should be the base type.
560 This is because the code for processing a loop determines whether
561 a normal loop end test can be done by comparing the bounds of the
562 loop against those of the base type, which is presumed to be the
563 size used for computation. But this is not correct when the size
564 of the subtype is smaller than the type. */
565 if (kind == E_Loop_Parameter)
566 gnu_type = get_base_type (gnu_type);
568 /* Reject non-renamed objects whose types are unconstrained arrays or
569 any object whose type is a dummy type or VOID_TYPE. */
571 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
572 && No (Renamed_Object (gnat_entity)))
573 || TYPE_IS_DUMMY_P (gnu_type)
574 || TREE_CODE (gnu_type) == VOID_TYPE)
576 gcc_assert (type_annotate_only);
577 if (this_global)
578 force_global--;
579 return error_mark_node;
582 /* If an alignment is specified, use it if valid. Note that
583 exceptions are objects but don't have alignments. We must do this
584 before we validate the size, since the alignment can affect the
585 size. */
586 if (kind != E_Exception && Known_Alignment (gnat_entity))
588 gcc_assert (Present (Alignment (gnat_entity)));
589 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
590 TYPE_ALIGN (gnu_type));
591 gnu_type = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
592 "PAD", false, definition, true);
595 /* If we are defining the object, see if it has a Size value and
596 validate it if so. If we are not defining the object and a Size
597 clause applies, simply retrieve the value. We don't want to ignore
598 the clause and it is expected to have been validated already. Then
599 get the new type, if any. */
600 if (definition)
601 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
602 gnat_entity, VAR_DECL, false,
603 Has_Size_Clause (gnat_entity));
604 else if (Has_Size_Clause (gnat_entity))
605 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
607 if (gnu_size)
609 gnu_type
610 = make_type_from_size (gnu_type, gnu_size,
611 Has_Biased_Representation (gnat_entity));
613 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
614 gnu_size = NULL_TREE;
617 /* If this object has self-referential size, it must be a record with
618 a default value. We are supposed to allocate an object of the
619 maximum size in this case unless it is a constant with an
620 initializing expression, in which case we can get the size from
621 that. Note that the resulting size may still be a variable, so
622 this may end up with an indirect allocation. */
623 if (No (Renamed_Object (gnat_entity))
624 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
626 if (gnu_expr && kind == E_Constant)
628 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
629 if (CONTAINS_PLACEHOLDER_P (size))
631 /* If the initializing expression is itself a constant,
632 despite having a nominal type with self-referential
633 size, we can get the size directly from it. */
634 if (TREE_CODE (gnu_expr) == COMPONENT_REF
635 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
636 == RECORD_TYPE
637 && TYPE_IS_PADDING_P
638 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
639 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
640 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
641 || DECL_READONLY_ONCE_ELAB
642 (TREE_OPERAND (gnu_expr, 0))))
643 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
644 else
645 gnu_size
646 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
648 else
649 gnu_size = size;
651 /* We may have no GNU_EXPR because No_Initialization is
652 set even though there's an Expression. */
653 else if (kind == E_Constant
654 && (Nkind (Declaration_Node (gnat_entity))
655 == N_Object_Declaration)
656 && Present (Expression (Declaration_Node (gnat_entity))))
657 gnu_size
658 = TYPE_SIZE (gnat_to_gnu_type
659 (Etype
660 (Expression (Declaration_Node (gnat_entity)))));
661 else
663 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
664 mutable_p = true;
668 /* If the size is zero bytes, make it one byte since some linkers have
669 trouble with zero-sized objects. If the object will have a
670 template, that will make it nonzero so don't bother. Also avoid
671 doing that for an object renaming or an object with an address
672 clause, as we would lose useful information on the view size
673 (e.g. for null array slices) and we are not allocating the object
674 here anyway. */
675 if (((gnu_size
676 && integer_zerop (gnu_size)
677 && !TREE_OVERFLOW (gnu_size))
678 || (TYPE_SIZE (gnu_type)
679 && integer_zerop (TYPE_SIZE (gnu_type))
680 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
681 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
682 || !Is_Array_Type (Etype (gnat_entity)))
683 && !Present (Renamed_Object (gnat_entity))
684 && !Present (Address_Clause (gnat_entity)))
685 gnu_size = bitsize_unit_node;
687 /* If this is an object with no specified size and alignment, and
688 if either it is atomic or we are not optimizing alignment for
689 space and it is composite and not an exception, an Out parameter
690 or a reference to another object, and the size of its type is a
691 constant, set the alignment to the smallest one which is not
692 smaller than the size, with an appropriate cap. */
693 if (!gnu_size && align == 0
694 && (Is_Atomic (gnat_entity)
695 || (!Optimize_Alignment_Space (gnat_entity)
696 && kind != E_Exception
697 && kind != E_Out_Parameter
698 && Is_Composite_Type (Etype (gnat_entity))
699 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
700 && !imported_p
701 && No (Renamed_Object (gnat_entity))
702 && No (Address_Clause (gnat_entity))))
703 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
705 /* No point in jumping through all the hoops needed in order
706 to support BIGGEST_ALIGNMENT if we don't really have to. */
707 unsigned int align_cap = Is_Atomic (gnat_entity)
708 ? BIGGEST_ALIGNMENT
709 : get_mode_alignment (word_mode);
711 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
712 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
713 align = align_cap;
714 else
715 align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
717 /* But make sure not to under-align the object. */
718 if (align <= TYPE_ALIGN (gnu_type))
719 align = 0;
721 /* And honor the minimum valid atomic alignment, if any. */
722 #ifdef MINIMUM_ATOMIC_ALIGNMENT
723 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
724 align = MINIMUM_ATOMIC_ALIGNMENT;
725 #endif
728 /* If the object is set to have atomic components, find the component
729 type and validate it.
731 ??? Note that we ignore Has_Volatile_Components on objects; it's
732 not at all clear what to do in that case. */
734 if (Has_Atomic_Components (gnat_entity))
736 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
737 ? TREE_TYPE (gnu_type) : gnu_type);
739 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
740 && TYPE_MULTI_ARRAY_P (gnu_inner))
741 gnu_inner = TREE_TYPE (gnu_inner);
743 check_ok_for_atomic (gnu_inner, gnat_entity, true);
746 /* Now check if the type of the object allows atomic access. Note
747 that we must test the type, even if this object has size and
748 alignment to allow such access, because we will be going
749 inside the padded record to assign to the object. We could fix
750 this by always copying via an intermediate value, but it's not
751 clear it's worth the effort. */
752 if (Is_Atomic (gnat_entity))
753 check_ok_for_atomic (gnu_type, gnat_entity, false);
755 /* If this is an aliased object with an unconstrained nominal subtype,
756 make a type that includes the template. */
757 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
758 && Is_Array_Type (Etype (gnat_entity))
759 && !type_annotate_only)
761 tree gnu_fat
762 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
764 gnu_type
765 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
766 concat_id_with_name (gnu_entity_id,
767 "UNC"));
770 #ifdef MINIMUM_ATOMIC_ALIGNMENT
771 /* If the size is a constant and no alignment is specified, force
772 the alignment to be the minimum valid atomic alignment. The
773 restriction on constant size avoids problems with variable-size
774 temporaries; if the size is variable, there's no issue with
775 atomic access. Also don't do this for a constant, since it isn't
776 necessary and can interfere with constant replacement. Finally,
777 do not do it for Out parameters since that creates an
778 size inconsistency with In parameters. */
779 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
780 && !FLOAT_TYPE_P (gnu_type)
781 && !const_flag && No (Renamed_Object (gnat_entity))
782 && !imported_p && No (Address_Clause (gnat_entity))
783 && kind != E_Out_Parameter
784 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
785 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
786 align = MINIMUM_ATOMIC_ALIGNMENT;
787 #endif
789 /* Make a new type with the desired size and alignment, if needed.
790 But do not take into account alignment promotions to compute the
791 size of the object. */
792 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
793 if (gnu_size || align > 0)
794 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
795 "PAD", false, definition,
796 gnu_size ? true : false);
798 /* Make a volatile version of this object's type if we are to make
799 the object volatile. We also interpret 13.3(19) conservatively
800 and disallow any optimizations for an object covered by it. */
801 if ((Treat_As_Volatile (gnat_entity)
802 || (Is_Exported (gnat_entity)
803 /* Exclude exported constants created by the compiler,
804 which should boil down to static dispatch tables and
805 make it possible to put them in read-only memory. */
806 && (Comes_From_Source (gnat_entity) || !const_flag))
807 || Is_Imported (gnat_entity)
808 || Present (Address_Clause (gnat_entity)))
809 && !TYPE_VOLATILE (gnu_type))
810 gnu_type = build_qualified_type (gnu_type,
811 (TYPE_QUALS (gnu_type)
812 | TYPE_QUAL_VOLATILE));
814 /* If this is a renaming, avoid as much as possible to create a new
815 object. However, in several cases, creating it is required.
816 This processing needs to be applied to the raw expression so
817 as to make it more likely to rename the underlying object. */
818 if (Present (Renamed_Object (gnat_entity)))
820 bool create_normal_object = false;
822 /* If the renamed object had padding, strip off the reference
823 to the inner object and reset our type. */
824 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
825 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
826 == RECORD_TYPE
827 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
828 /* Strip useless conversions around the object. */
829 || TREE_CODE (gnu_expr) == NOP_EXPR)
831 gnu_expr = TREE_OPERAND (gnu_expr, 0);
832 gnu_type = TREE_TYPE (gnu_expr);
835 /* Case 1: If this is a constant renaming stemming from a function
836 call, treat it as a normal object whose initial value is what
837 is being renamed. RM 3.3 says that the result of evaluating a
838 function call is a constant object. As a consequence, it can
839 be the inner object of a constant renaming. In this case, the
840 renaming must be fully instantiated, i.e. it cannot be a mere
841 reference to (part of) an existing object. */
842 if (const_flag)
844 tree inner_object = gnu_expr;
845 while (handled_component_p (inner_object))
846 inner_object = TREE_OPERAND (inner_object, 0);
847 if (TREE_CODE (inner_object) == CALL_EXPR)
848 create_normal_object = true;
851 /* Otherwise, see if we can proceed with a stabilized version of
852 the renamed entity or if we need to make a new object. */
853 if (!create_normal_object)
855 tree maybe_stable_expr = NULL_TREE;
856 bool stable = false;
858 /* Case 2: If the renaming entity need not be materialized and
859 the renamed expression is something we can stabilize, use
860 that for the renaming. At the global level, we can only do
861 this if we know no SAVE_EXPRs need be made, because the
862 expression we return might be used in arbitrary conditional
863 branches so we must force the SAVE_EXPRs evaluation
864 immediately and this requires a function context. */
865 if (!Materialize_Entity (gnat_entity)
866 && (!global_bindings_p ()
867 || (staticp (gnu_expr)
868 && !TREE_SIDE_EFFECTS (gnu_expr))))
870 maybe_stable_expr
871 = maybe_stabilize_reference (gnu_expr, true, &stable);
873 if (stable)
875 gnu_decl = maybe_stable_expr;
876 /* ??? No DECL_EXPR is created so we need to mark
877 the expression manually lest it is shared. */
878 if (global_bindings_p ())
879 mark_visited (&gnu_decl);
880 save_gnu_tree (gnat_entity, gnu_decl, true);
881 saved = true;
882 break;
885 /* The stabilization failed. Keep maybe_stable_expr
886 untouched here to let the pointer case below know
887 about that failure. */
890 /* Case 3: If this is a constant renaming and creating a
891 new object is allowed and cheap, treat it as a normal
892 object whose initial value is what is being renamed. */
893 if (const_flag && Is_Elementary_Type (Etype (gnat_entity)))
896 /* Case 4: Make this into a constant pointer to the object we
897 are to rename and attach the object to the pointer if it is
898 something we can stabilize.
900 From the proper scope, attached objects will be referenced
901 directly instead of indirectly via the pointer to avoid
902 subtle aliasing problems with non-addressable entities.
903 They have to be stable because we must not evaluate the
904 variables in the expression every time the renaming is used.
905 The pointer is called a "renaming" pointer in this case.
907 In the rare cases where we cannot stabilize the renamed
908 object, we just make a "bare" pointer, and the renamed
909 entity is always accessed indirectly through it. */
910 else
912 gnu_type = build_reference_type (gnu_type);
913 inner_const_flag = TREE_READONLY (gnu_expr);
914 const_flag = true;
916 /* If the previous attempt at stabilizing failed, there
917 is no point in trying again and we reuse the result
918 without attaching it to the pointer. In this case it
919 will only be used as the initializing expression of
920 the pointer and thus needs no special treatment with
921 regard to multiple evaluations. */
922 if (maybe_stable_expr)
925 /* Otherwise, try to stabilize and attach the expression
926 to the pointer if the stabilization succeeds.
928 Note that this might introduce SAVE_EXPRs and we don't
929 check whether we're at the global level or not. This
930 is fine since we are building a pointer initializer and
931 neither the pointer nor the initializing expression can
932 be accessed before the pointer elaboration has taken
933 place in a correct program.
935 These SAVE_EXPRs will be evaluated at the right place
936 by either the evaluation of the initializer for the
937 non-global case or the elaboration code for the global
938 case, and will be attached to the elaboration procedure
939 in the latter case. */
940 else
942 maybe_stable_expr
943 = maybe_stabilize_reference (gnu_expr, true, &stable);
945 if (stable)
946 renamed_obj = maybe_stable_expr;
948 /* Attaching is actually performed downstream, as soon
949 as we have a VAR_DECL for the pointer we make. */
952 gnu_expr
953 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
955 gnu_size = NULL_TREE;
956 used_by_ref = true;
961 /* If this is an aliased object whose nominal subtype is unconstrained,
962 the object is a record that contains both the template and
963 the object. If there is an initializer, it will have already
964 been converted to the right type, but we need to create the
965 template if there is no initializer. */
966 else if (definition
967 && TREE_CODE (gnu_type) == RECORD_TYPE
968 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
969 /* Beware that padding might have been introduced
970 via maybe_pad_type above. */
971 || (TYPE_IS_PADDING_P (gnu_type)
972 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
973 == RECORD_TYPE
974 && TYPE_CONTAINS_TEMPLATE_P
975 (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
976 && !gnu_expr)
978 tree template_field
979 = TYPE_IS_PADDING_P (gnu_type)
980 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
981 : TYPE_FIELDS (gnu_type);
983 gnu_expr
984 = gnat_build_constructor
985 (gnu_type,
986 tree_cons
987 (template_field,
988 build_template (TREE_TYPE (template_field),
989 TREE_TYPE (TREE_CHAIN (template_field)),
990 NULL_TREE),
991 NULL_TREE));
994 /* Convert the expression to the type of the object except in the
995 case where the object's type is unconstrained or the object's type
996 is a padded record whose field is of self-referential size. In
997 the former case, converting will generate unnecessary evaluations
998 of the CONSTRUCTOR to compute the size and in the latter case, we
999 want to only copy the actual data. */
1000 if (gnu_expr
1001 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1002 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1003 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1004 && TYPE_IS_PADDING_P (gnu_type)
1005 && (CONTAINS_PLACEHOLDER_P
1006 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1007 gnu_expr = convert (gnu_type, gnu_expr);
1009 /* If this is a pointer and it does not have an initializing
1010 expression, initialize it to NULL, unless the object is
1011 imported. */
1012 if (definition
1013 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
1014 && !Is_Imported (gnat_entity) && !gnu_expr)
1015 gnu_expr = integer_zero_node;
1017 /* If we are defining the object and it has an Address clause we must
1018 get the address expression from the saved GCC tree for the
1019 object if the object has a Freeze_Node. Otherwise, we elaborate
1020 the address expression here since the front-end has guaranteed
1021 in that case that the elaboration has no effects. Note that
1022 only the latter mechanism is currently in use. */
1023 if (definition && Present (Address_Clause (gnat_entity)))
1025 tree gnu_address
1026 = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
1027 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
1029 save_gnu_tree (gnat_entity, NULL_TREE, false);
1031 /* Ignore the size. It's either meaningless or was handled
1032 above. */
1033 gnu_size = NULL_TREE;
1034 /* Convert the type of the object to a reference type that can
1035 alias everything as per 13.3(19). */
1036 gnu_type
1037 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1038 gnu_address = convert (gnu_type, gnu_address);
1039 used_by_ref = true;
1040 const_flag = !Is_Public (gnat_entity)
1041 || compile_time_known_address_p (Expression (Address_Clause
1042 (gnat_entity)));
1044 /* If we don't have an initializing expression for the underlying
1045 variable, the initializing expression for the pointer is the
1046 specified address. Otherwise, we have to make a COMPOUND_EXPR
1047 to assign both the address and the initial value. */
1048 if (!gnu_expr)
1049 gnu_expr = gnu_address;
1050 else
1051 gnu_expr
1052 = build2 (COMPOUND_EXPR, gnu_type,
1053 build_binary_op
1054 (MODIFY_EXPR, NULL_TREE,
1055 build_unary_op (INDIRECT_REF, NULL_TREE,
1056 gnu_address),
1057 gnu_expr),
1058 gnu_address);
1061 /* If it has an address clause and we are not defining it, mark it
1062 as an indirect object. Likewise for Stdcall objects that are
1063 imported. */
1064 if ((!definition && Present (Address_Clause (gnat_entity)))
1065 || (Is_Imported (gnat_entity)
1066 && Has_Stdcall_Convention (gnat_entity)))
1068 /* Convert the type of the object to a reference type that can
1069 alias everything as per 13.3(19). */
1070 gnu_type
1071 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1072 gnu_size = NULL_TREE;
1074 /* No point in taking the address of an initializing expression
1075 that isn't going to be used. */
1076 gnu_expr = NULL_TREE;
1078 /* If it has an address clause whose value is known at compile
1079 time, make the object a CONST_DECL. This will avoid a
1080 useless dereference. */
1081 if (Present (Address_Clause (gnat_entity)))
1083 Node_Id gnat_address
1084 = Expression (Address_Clause (gnat_entity));
1086 if (compile_time_known_address_p (gnat_address))
1088 gnu_expr = gnat_to_gnu (gnat_address);
1089 const_flag = true;
1093 used_by_ref = true;
1096 /* If we are at top level and this object is of variable size,
1097 make the actual type a hidden pointer to the real type and
1098 make the initializer be a memory allocation and initialization.
1099 Likewise for objects we aren't defining (presumed to be
1100 external references from other packages), but there we do
1101 not set up an initialization.
1103 If the object's size overflows, make an allocator too, so that
1104 Storage_Error gets raised. Note that we will never free
1105 such memory, so we presume it never will get allocated. */
1107 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1108 global_bindings_p () || !definition
1109 || static_p)
1110 || (gnu_size
1111 && ! allocatable_size_p (gnu_size,
1112 global_bindings_p () || !definition
1113 || static_p)))
1115 gnu_type = build_reference_type (gnu_type);
1116 gnu_size = NULL_TREE;
1117 used_by_ref = true;
1118 const_flag = true;
1120 /* In case this was a aliased object whose nominal subtype is
1121 unconstrained, the pointer above will be a thin pointer and
1122 build_allocator will automatically make the template.
1124 If we have a template initializer only (that we made above),
1125 pretend there is none and rely on what build_allocator creates
1126 again anyway. Otherwise (if we have a full initializer), get
1127 the data part and feed that to build_allocator.
1129 If we are elaborating a mutable object, tell build_allocator to
1130 ignore a possibly simpler size from the initializer, if any, as
1131 we must allocate the maximum possible size in this case. */
1133 if (definition)
1135 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1137 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1138 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1140 gnu_alloc_type
1141 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1143 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1144 && 1 == VEC_length (constructor_elt,
1145 CONSTRUCTOR_ELTS (gnu_expr)))
1146 gnu_expr = 0;
1147 else
1148 gnu_expr
1149 = build_component_ref
1150 (gnu_expr, NULL_TREE,
1151 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1152 false);
1155 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1156 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1157 && !Is_Imported (gnat_entity))
1158 post_error ("?Storage_Error will be raised at run-time!",
1159 gnat_entity);
1161 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1162 0, 0, gnat_entity, mutable_p);
1164 else
1166 gnu_expr = NULL_TREE;
1167 const_flag = false;
1171 /* If this object would go into the stack and has an alignment larger
1172 than the largest stack alignment the back-end can honor, resort to
1173 a variable of "aligning type". */
1174 if (!global_bindings_p () && !static_p && definition
1175 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1177 /* Create the new variable. No need for extra room before the
1178 aligned field as this is in automatic storage. */
1179 tree gnu_new_type
1180 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1181 TYPE_SIZE_UNIT (gnu_type),
1182 BIGGEST_ALIGNMENT, 0);
1183 tree gnu_new_var
1184 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1185 NULL_TREE, gnu_new_type, NULL_TREE, false,
1186 false, false, false, NULL, gnat_entity);
1188 /* Initialize the aligned field if we have an initializer. */
1189 if (gnu_expr)
1190 add_stmt_with_node
1191 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1192 build_component_ref
1193 (gnu_new_var, NULL_TREE,
1194 TYPE_FIELDS (gnu_new_type), false),
1195 gnu_expr),
1196 gnat_entity);
1198 /* And setup this entity as a reference to the aligned field. */
1199 gnu_type = build_reference_type (gnu_type);
1200 gnu_expr
1201 = build_unary_op
1202 (ADDR_EXPR, gnu_type,
1203 build_component_ref (gnu_new_var, NULL_TREE,
1204 TYPE_FIELDS (gnu_new_type), false));
1206 gnu_size = NULL_TREE;
1207 used_by_ref = true;
1208 const_flag = true;
1211 if (const_flag)
1212 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1213 | TYPE_QUAL_CONST));
1215 /* Convert the expression to the type of the object except in the
1216 case where the object's type is unconstrained or the object's type
1217 is a padded record whose field is of self-referential size. In
1218 the former case, converting will generate unnecessary evaluations
1219 of the CONSTRUCTOR to compute the size and in the latter case, we
1220 want to only copy the actual data. */
1221 if (gnu_expr
1222 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1223 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1224 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1225 && TYPE_IS_PADDING_P (gnu_type)
1226 && (CONTAINS_PLACEHOLDER_P
1227 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1228 gnu_expr = convert (gnu_type, gnu_expr);
1230 /* If this name is external or there was a name specified, use it,
1231 unless this is a VMS exception object since this would conflict
1232 with the symbol we need to export in addition. Don't use the
1233 Interface_Name if there is an address clause (see CD30005). */
1234 if (!Is_VMS_Exception (gnat_entity)
1235 && ((Present (Interface_Name (gnat_entity))
1236 && No (Address_Clause (gnat_entity)))
1237 || (Is_Public (gnat_entity)
1238 && (!Is_Imported (gnat_entity)
1239 || Is_Exported (gnat_entity)))))
1240 gnu_ext_name = create_concat_name (gnat_entity, 0);
1242 /* If this is constant initialized to a static constant and the
1243 object has an aggregate type, force it to be statically
1244 allocated. */
1245 if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1246 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1247 && (AGGREGATE_TYPE_P (gnu_type)
1248 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1249 && TYPE_IS_PADDING_P (gnu_type))))
1250 static_p = true;
1252 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1253 gnu_expr, const_flag,
1254 Is_Public (gnat_entity),
1255 imported_p || !definition,
1256 static_p, attr_list, gnat_entity);
1257 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1258 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1259 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1261 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1262 if (global_bindings_p ())
1264 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1265 record_global_renaming_pointer (gnu_decl);
1269 if (definition && DECL_SIZE (gnu_decl)
1270 && get_block_jmpbuf_decl ()
1271 && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1272 || (flag_stack_check && !STACK_CHECK_BUILTIN
1273 && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1274 STACK_CHECK_MAX_VAR_SIZE))))
1275 add_stmt_with_node (build_call_1_expr
1276 (update_setjmp_buf_decl,
1277 build_unary_op (ADDR_EXPR, NULL_TREE,
1278 get_block_jmpbuf_decl ())),
1279 gnat_entity);
1281 /* If this is a public constant or we're not optimizing and we're not
1282 making a VAR_DECL for it, make one just for export or debugger use.
1283 Likewise if the address is taken or if either the object or type is
1284 aliased. Make an external declaration for a reference, unless this
1285 is a Standard entity since there no real symbol at the object level
1286 for these. */
1287 if (TREE_CODE (gnu_decl) == CONST_DECL
1288 && (definition || Sloc (gnat_entity) > Standard_Location)
1289 && ((Is_Public (gnat_entity)
1290 && !Present (Address_Clause (gnat_entity)))
1291 || optimize == 0
1292 || Address_Taken (gnat_entity)
1293 || Is_Aliased (gnat_entity)
1294 || Is_Aliased (Etype (gnat_entity))))
1296 tree gnu_corr_var
1297 = create_true_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1298 gnu_expr, true, Is_Public (gnat_entity),
1299 !definition, static_p, NULL,
1300 gnat_entity);
1302 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1304 /* As debugging information will be generated for the variable,
1305 do not generate information for the constant. */
1306 DECL_IGNORED_P (gnu_decl) = true;
1309 /* If this is declared in a block that contains a block with an
1310 exception handler, we must force this variable in memory to
1311 suppress an invalid optimization. */
1312 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1313 && Exception_Mechanism != Back_End_Exceptions)
1314 TREE_ADDRESSABLE (gnu_decl) = 1;
1316 gnu_type = TREE_TYPE (gnu_decl);
1318 /* Back-annotate Alignment and Esize of the object if not already
1319 known, except for when the object is actually a pointer to the
1320 real object, since alignment and size of a pointer don't have
1321 anything to do with those of the designated object. Note that
1322 we pick the values of the type, not those of the object, to
1323 shield ourselves from low-level platform-dependent adjustments
1324 like alignment promotion. This is both consistent with all the
1325 treatment above, where alignment and size are set on the type of
1326 the object and not on the object directly, and makes it possible
1327 to support confirming representation clauses in all cases. */
1329 if (!used_by_ref && Unknown_Alignment (gnat_entity))
1330 Set_Alignment (gnat_entity,
1331 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
1333 if (!used_by_ref && Unknown_Esize (gnat_entity))
1335 if (TREE_CODE (gnu_type) == RECORD_TYPE
1336 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1337 gnu_object_size
1338 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type))));
1340 Set_Esize (gnat_entity, annotate_value (gnu_object_size));
1343 break;
1345 case E_Void:
1346 /* Return a TYPE_DECL for "void" that we previously made. */
1347 gnu_decl = void_type_decl_node;
1348 break;
1350 case E_Enumeration_Type:
1351 /* A special case, for the types Character and Wide_Character in
1352 Standard, we do not list all the literals. So if the literals
1353 are not specified, make this an unsigned type. */
1354 if (No (First_Literal (gnat_entity)))
1356 gnu_type = make_unsigned_type (esize);
1357 TYPE_NAME (gnu_type) = gnu_entity_id;
1359 /* Set the TYPE_STRING_FLAG for Ada Character and
1360 Wide_Character types. This is needed by the dwarf-2 debug writer to
1361 distinguish between unsigned integer types and character types. */
1362 TYPE_STRING_FLAG (gnu_type) = 1;
1363 break;
1366 /* Normal case of non-character type, or non-Standard character type */
1368 /* Here we have a list of enumeral constants in First_Literal.
1369 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1370 the list to be places into TYPE_FIELDS. Each node in the list
1371 is a TREE_LIST node whose TREE_VALUE is the literal name
1372 and whose TREE_PURPOSE is the value of the literal.
1374 Esize contains the number of bits needed to represent the enumeral
1375 type, Type_Low_Bound also points to the first literal and
1376 Type_High_Bound points to the last literal. */
1378 Entity_Id gnat_literal;
1379 tree gnu_literal_list = NULL_TREE;
1381 if (Is_Unsigned_Type (gnat_entity))
1382 gnu_type = make_unsigned_type (esize);
1383 else
1384 gnu_type = make_signed_type (esize);
1386 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1388 for (gnat_literal = First_Literal (gnat_entity);
1389 Present (gnat_literal);
1390 gnat_literal = Next_Literal (gnat_literal))
1392 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1393 gnu_type);
1394 tree gnu_literal
1395 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1396 gnu_type, gnu_value, true, false, false,
1397 false, NULL, gnat_literal);
1399 save_gnu_tree (gnat_literal, gnu_literal, false);
1400 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1401 gnu_value, gnu_literal_list);
1404 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1406 /* Note that the bounds are updated at the end of this function
1407 because to avoid an infinite recursion when we get the bounds of
1408 this type, since those bounds are objects of this type. */
1410 break;
1412 case E_Signed_Integer_Type:
1413 case E_Ordinary_Fixed_Point_Type:
1414 case E_Decimal_Fixed_Point_Type:
1415 /* For integer types, just make a signed type the appropriate number
1416 of bits. */
1417 gnu_type = make_signed_type (esize);
1418 break;
1420 case E_Modular_Integer_Type:
1421 /* For modular types, make the unsigned type of the proper number of
1422 bits and then set up the modulus, if required. */
1424 enum machine_mode mode;
1425 tree gnu_modulus;
1426 tree gnu_high = 0;
1428 if (Is_Packed_Array_Type (gnat_entity))
1429 esize = UI_To_Int (RM_Size (gnat_entity));
1431 /* Find the smallest mode at least ESIZE bits wide and make a class
1432 using that mode. */
1434 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1435 GET_MODE_BITSIZE (mode) < esize;
1436 mode = GET_MODE_WIDER_MODE (mode))
1439 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1440 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1441 = (Is_Packed_Array_Type (gnat_entity)
1442 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
1444 /* Get the modulus in this type. If it overflows, assume it is because
1445 it is equal to 2**Esize. Note that there is no overflow checking
1446 done on unsigned type, so we detect the overflow by looking for
1447 a modulus of zero, which is otherwise invalid. */
1448 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1450 if (!integer_zerop (gnu_modulus))
1452 TYPE_MODULAR_P (gnu_type) = 1;
1453 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1454 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1455 convert (gnu_type, integer_one_node));
1458 /* If we have to set TYPE_PRECISION different from its natural value,
1459 make a subtype to do do. Likewise if there is a modulus and
1460 it is not one greater than TYPE_MAX_VALUE. */
1461 if (TYPE_PRECISION (gnu_type) != esize
1462 || (TYPE_MODULAR_P (gnu_type)
1463 && !tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1465 tree gnu_subtype = make_node (INTEGER_TYPE);
1467 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1468 TREE_TYPE (gnu_subtype) = gnu_type;
1469 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1470 TYPE_MAX_VALUE (gnu_subtype)
1471 = TYPE_MODULAR_P (gnu_type)
1472 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1473 TYPE_PRECISION (gnu_subtype) = esize;
1474 TYPE_UNSIGNED (gnu_subtype) = 1;
1475 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1476 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1477 = (Is_Packed_Array_Type (gnat_entity)
1478 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
1479 layout_type (gnu_subtype);
1481 gnu_type = gnu_subtype;
1484 break;
1486 case E_Signed_Integer_Subtype:
1487 case E_Enumeration_Subtype:
1488 case E_Modular_Integer_Subtype:
1489 case E_Ordinary_Fixed_Point_Subtype:
1490 case E_Decimal_Fixed_Point_Subtype:
1492 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1493 that we do not want to call build_range_type since we would
1494 like each subtype node to be distinct. This will be important
1495 when memory aliasing is implemented.
1497 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1498 parent type; this fact is used by the arithmetic conversion
1499 functions.
1501 We elaborate the Ancestor_Subtype if it is not in the current
1502 unit and one of our bounds is non-static. We do this to ensure
1503 consistent naming in the case where several subtypes share the same
1504 bounds by always elaborating the first such subtype first, thus
1505 using its name. */
1507 if (!definition
1508 && Present (Ancestor_Subtype (gnat_entity))
1509 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1510 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1511 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1512 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1513 gnu_expr, 0);
1515 gnu_type = make_node (INTEGER_TYPE);
1516 if (Is_Packed_Array_Type (gnat_entity)
1517 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1519 esize = UI_To_Int (RM_Size (gnat_entity));
1520 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1523 TYPE_PRECISION (gnu_type) = esize;
1524 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1526 TYPE_MIN_VALUE (gnu_type)
1527 = convert (TREE_TYPE (gnu_type),
1528 elaborate_expression (Type_Low_Bound (gnat_entity),
1529 gnat_entity,
1530 get_identifier ("L"), definition, 1,
1531 Needs_Debug_Info (gnat_entity)));
1533 TYPE_MAX_VALUE (gnu_type)
1534 = convert (TREE_TYPE (gnu_type),
1535 elaborate_expression (Type_High_Bound (gnat_entity),
1536 gnat_entity,
1537 get_identifier ("U"), definition, 1,
1538 Needs_Debug_Info (gnat_entity)));
1540 /* One of the above calls might have caused us to be elaborated,
1541 so don't blow up if so. */
1542 if (present_gnu_tree (gnat_entity))
1544 maybe_present = true;
1545 break;
1548 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1549 = Has_Biased_Representation (gnat_entity);
1551 /* This should be an unsigned type if the lower bound is constant
1552 and non-negative or if the base type is unsigned; a signed type
1553 otherwise. */
1554 TYPE_UNSIGNED (gnu_type)
1555 = (TYPE_UNSIGNED (TREE_TYPE (gnu_type))
1556 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1557 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1558 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1559 || Is_Unsigned_Type (gnat_entity));
1561 layout_type (gnu_type);
1563 /* Inherit our alias set from what we're a subtype of. Subtypes
1564 are not different types and a pointer can designate any instance
1565 within a subtype hierarchy. */
1566 copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1568 /* If the type we are dealing with is to represent a packed array,
1569 we need to have the bits left justified on big-endian targets
1570 and right justified on little-endian targets. We also need to
1571 ensure that when the value is read (e.g. for comparison of two
1572 such values), we only get the good bits, since the unused bits
1573 are uninitialized. Both goals are accomplished by wrapping the
1574 modular value in an enclosing struct. */
1575 if (Is_Packed_Array_Type (gnat_entity)
1576 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1578 tree gnu_field_type = gnu_type;
1579 tree gnu_field;
1581 TYPE_RM_SIZE_NUM (gnu_field_type)
1582 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1583 gnu_type = make_node (RECORD_TYPE);
1584 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1586 /* Propagate the alignment of the modular type to the record.
1587 This means that bitpacked arrays have "ceil" alignment for
1588 their size, which may seem counter-intuitive but makes it
1589 possible to easily overlay them on modular types. */
1590 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1591 TYPE_PACKED (gnu_type) = 1;
1593 /* Create a stripped-down declaration of the original type, mainly
1594 for debugging. */
1595 create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1596 NULL, true, debug_info_p, gnat_entity);
1598 /* Don't notify the field as "addressable", since we won't be taking
1599 it's address and it would prevent create_field_decl from making a
1600 bitfield. */
1601 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1602 gnu_field_type, gnu_type, 1, 0, 0, 0);
1604 finish_record_type (gnu_type, gnu_field, 0, false);
1605 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1606 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1608 copy_alias_set (gnu_type, gnu_field_type);
1611 /* If the type we are dealing with has got a smaller alignment than the
1612 natural one, we need to wrap it up in a record type and under-align
1613 the latter. We reuse the padding machinery for this purpose. */
1614 else if (Known_Alignment (gnat_entity)
1615 && UI_Is_In_Int_Range (Alignment (gnat_entity))
1616 && (align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT)
1617 && align < TYPE_ALIGN (gnu_type))
1619 tree gnu_field_type = gnu_type;
1620 tree gnu_field;
1622 gnu_type = make_node (RECORD_TYPE);
1623 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1625 TYPE_ALIGN (gnu_type) = align;
1626 TYPE_PACKED (gnu_type) = 1;
1628 /* Create a stripped-down declaration of the original type, mainly
1629 for debugging. */
1630 create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1631 NULL, true, debug_info_p, gnat_entity);
1633 /* Don't notify the field as "addressable", since we won't be taking
1634 it's address and it would prevent create_field_decl from making a
1635 bitfield. */
1636 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1637 gnu_field_type, gnu_type, 1, 0, 0, 0);
1639 finish_record_type (gnu_type, gnu_field, 0, false);
1640 TYPE_IS_PADDING_P (gnu_type) = 1;
1641 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1643 copy_alias_set (gnu_type, gnu_field_type);
1646 /* Otherwise reset the alignment lest we computed it above. */
1647 else
1648 align = 0;
1650 break;
1652 case E_Floating_Point_Type:
1653 /* If this is a VAX floating-point type, use an integer of the proper
1654 size. All the operations will be handled with ASM statements. */
1655 if (Vax_Float (gnat_entity))
1657 gnu_type = make_signed_type (esize);
1658 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1659 SET_TYPE_DIGITS_VALUE (gnu_type,
1660 UI_To_gnu (Digits_Value (gnat_entity),
1661 sizetype));
1662 break;
1665 /* The type of the Low and High bounds can be our type if this is
1666 a type from Standard, so set them at the end of the function. */
1667 gnu_type = make_node (REAL_TYPE);
1668 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1669 layout_type (gnu_type);
1670 break;
1672 case E_Floating_Point_Subtype:
1673 if (Vax_Float (gnat_entity))
1675 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1676 break;
1680 if (!definition
1681 && Present (Ancestor_Subtype (gnat_entity))
1682 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1683 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1684 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1685 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1686 gnu_expr, 0);
1688 gnu_type = make_node (REAL_TYPE);
1689 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1690 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1692 TYPE_MIN_VALUE (gnu_type)
1693 = convert (TREE_TYPE (gnu_type),
1694 elaborate_expression (Type_Low_Bound (gnat_entity),
1695 gnat_entity, get_identifier ("L"),
1696 definition, 1,
1697 Needs_Debug_Info (gnat_entity)));
1699 TYPE_MAX_VALUE (gnu_type)
1700 = convert (TREE_TYPE (gnu_type),
1701 elaborate_expression (Type_High_Bound (gnat_entity),
1702 gnat_entity, get_identifier ("U"),
1703 definition, 1,
1704 Needs_Debug_Info (gnat_entity)));
1706 /* One of the above calls might have caused us to be elaborated,
1707 so don't blow up if so. */
1708 if (present_gnu_tree (gnat_entity))
1710 maybe_present = true;
1711 break;
1714 layout_type (gnu_type);
1716 /* Inherit our alias set from what we're a subtype of, as for
1717 integer subtypes. */
1718 copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1720 break;
1722 /* Array and String Types and Subtypes
1724 Unconstrained array types are represented by E_Array_Type and
1725 constrained array types are represented by E_Array_Subtype. There
1726 are no actual objects of an unconstrained array type; all we have
1727 are pointers to that type.
1729 The following fields are defined on array types and subtypes:
1731 Component_Type Component type of the array.
1732 Number_Dimensions Number of dimensions (an int).
1733 First_Index Type of first index. */
1735 case E_String_Type:
1736 case E_Array_Type:
1738 tree gnu_template_fields = NULL_TREE;
1739 tree gnu_template_type = make_node (RECORD_TYPE);
1740 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1741 tree gnu_fat_type = make_node (RECORD_TYPE);
1742 int ndim = Number_Dimensions (gnat_entity);
1743 int firstdim
1744 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1745 int nextdim
1746 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1747 int index;
1748 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1749 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1750 tree gnu_comp_size = 0;
1751 tree gnu_max_size = size_one_node;
1752 tree gnu_max_size_unit;
1753 Entity_Id gnat_ind_subtype;
1754 Entity_Id gnat_ind_base_subtype;
1755 tree gnu_template_reference;
1756 tree tem;
1758 TYPE_NAME (gnu_template_type)
1759 = create_concat_name (gnat_entity, "XUB");
1761 /* Make a node for the array. If we are not defining the array
1762 suppress expanding incomplete types. */
1763 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1765 if (!definition)
1766 defer_incomplete_level++, this_deferred = true;
1768 /* Build the fat pointer type. Use a "void *" object instead of
1769 a pointer to the array type since we don't have the array type
1770 yet (it will reference the fat pointer via the bounds). */
1771 tem = chainon (chainon (NULL_TREE,
1772 create_field_decl (get_identifier ("P_ARRAY"),
1773 ptr_void_type_node,
1774 gnu_fat_type, 0, 0, 0, 0)),
1775 create_field_decl (get_identifier ("P_BOUNDS"),
1776 gnu_ptr_template,
1777 gnu_fat_type, 0, 0, 0, 0));
1779 /* Make sure we can put this into a register. */
1780 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1782 /* Do not finalize this record type since the types of its fields
1783 are still incomplete at this point. */
1784 finish_record_type (gnu_fat_type, tem, 0, true);
1785 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1787 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1788 is the fat pointer. This will be used to access the individual
1789 fields once we build them. */
1790 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1791 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1792 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1793 gnu_template_reference
1794 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1795 TREE_READONLY (gnu_template_reference) = 1;
1797 /* Now create the GCC type for each index and add the fields for
1798 that index to the template. */
1799 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1800 gnat_ind_base_subtype
1801 = First_Index (Implementation_Base_Type (gnat_entity));
1802 index < ndim && index >= 0;
1803 index += nextdim,
1804 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1805 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1807 char field_name[10];
1808 tree gnu_ind_subtype
1809 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1810 tree gnu_base_subtype
1811 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1812 tree gnu_base_min
1813 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1814 tree gnu_base_max
1815 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1816 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1818 /* Make the FIELD_DECLs for the minimum and maximum of this
1819 type and then make extractions of that field from the
1820 template. */
1821 sprintf (field_name, "LB%d", index);
1822 gnu_min_field = create_field_decl (get_identifier (field_name),
1823 gnu_ind_subtype,
1824 gnu_template_type, 0, 0, 0, 0);
1825 field_name[0] = 'U';
1826 gnu_max_field = create_field_decl (get_identifier (field_name),
1827 gnu_ind_subtype,
1828 gnu_template_type, 0, 0, 0, 0);
1830 Sloc_to_locus (Sloc (gnat_entity),
1831 &DECL_SOURCE_LOCATION (gnu_min_field));
1832 Sloc_to_locus (Sloc (gnat_entity),
1833 &DECL_SOURCE_LOCATION (gnu_max_field));
1834 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1836 /* We can't use build_component_ref here since the template
1837 type isn't complete yet. */
1838 gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1839 gnu_template_reference, gnu_min_field,
1840 NULL_TREE);
1841 gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1842 gnu_template_reference, gnu_max_field,
1843 NULL_TREE);
1844 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1846 /* Make a range type with the new ranges, but using
1847 the Ada subtype. Then we convert to sizetype. */
1848 gnu_index_types[index]
1849 = create_index_type (convert (sizetype, gnu_min),
1850 convert (sizetype, gnu_max),
1851 build_range_type (gnu_ind_subtype,
1852 gnu_min, gnu_max),
1853 gnat_entity);
1854 /* Update the maximum size of the array, in elements. */
1855 gnu_max_size
1856 = size_binop (MULT_EXPR, gnu_max_size,
1857 size_binop (PLUS_EXPR, size_one_node,
1858 size_binop (MINUS_EXPR, gnu_base_max,
1859 gnu_base_min)));
1861 TYPE_NAME (gnu_index_types[index])
1862 = create_concat_name (gnat_entity, field_name);
1865 for (index = 0; index < ndim; index++)
1866 gnu_template_fields
1867 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1869 /* Install all the fields into the template. */
1870 finish_record_type (gnu_template_type, gnu_template_fields, 0, false);
1871 TYPE_READONLY (gnu_template_type) = 1;
1873 /* Now make the array of arrays and update the pointer to the array
1874 in the fat pointer. Note that it is the first field. */
1875 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1877 /* Try to get a smaller form of the component if needed. */
1878 if ((Is_Packed (gnat_entity)
1879 || Has_Component_Size_Clause (gnat_entity))
1880 && !Is_Bit_Packed_Array (gnat_entity)
1881 && !Has_Aliased_Components (gnat_entity)
1882 && !Strict_Alignment (Component_Type (gnat_entity))
1883 && TREE_CODE (tem) == RECORD_TYPE
1884 && host_integerp (TYPE_SIZE (tem), 1))
1885 tem = make_packable_type (tem, false);
1887 if (Has_Atomic_Components (gnat_entity))
1888 check_ok_for_atomic (tem, gnat_entity, true);
1890 /* Get and validate any specified Component_Size, but if Packed,
1891 ignore it since the front end will have taken care of it. */
1892 gnu_comp_size
1893 = validate_size (Component_Size (gnat_entity), tem,
1894 gnat_entity,
1895 (Is_Bit_Packed_Array (gnat_entity)
1896 ? TYPE_DECL : VAR_DECL),
1897 true, Has_Component_Size_Clause (gnat_entity));
1899 /* If the component type is a RECORD_TYPE that has a self-referential
1900 size, use the maxium size. */
1901 if (!gnu_comp_size && TREE_CODE (tem) == RECORD_TYPE
1902 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1903 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1905 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
1907 tree orig_tem;
1908 tem = make_type_from_size (tem, gnu_comp_size, false);
1909 orig_tem = tem;
1910 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1911 "C_PAD", false, definition, true);
1912 /* If a padding record was made, declare it now since it will
1913 never be declared otherwise. This is necessary to ensure
1914 that its subtrees are properly marked. */
1915 if (tem != orig_tem)
1916 create_type_decl (TYPE_NAME (tem), tem, NULL, true, false,
1917 gnat_entity);
1920 if (Has_Volatile_Components (gnat_entity))
1921 tem = build_qualified_type (tem,
1922 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1924 /* If Component_Size is not already specified, annotate it with the
1925 size of the component. */
1926 if (Unknown_Component_Size (gnat_entity))
1927 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1929 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1930 size_binop (MULT_EXPR, gnu_max_size,
1931 TYPE_SIZE_UNIT (tem)));
1932 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1933 size_binop (MULT_EXPR,
1934 convert (bitsizetype,
1935 gnu_max_size),
1936 TYPE_SIZE (tem)));
1938 for (index = ndim - 1; index >= 0; index--)
1940 tem = build_array_type (tem, gnu_index_types[index]);
1941 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1942 if (array_type_has_nonaliased_component (gnat_entity, tem))
1943 TYPE_NONALIASED_COMPONENT (tem) = 1;
1946 /* If an alignment is specified, use it if valid. But ignore it for
1947 types that represent the unpacked base type for packed arrays. If
1948 the alignment was requested with an explicit user alignment clause,
1949 state so. */
1950 if (No (Packed_Array_Type (gnat_entity))
1951 && Known_Alignment (gnat_entity))
1953 gcc_assert (Present (Alignment (gnat_entity)));
1954 TYPE_ALIGN (tem)
1955 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1956 TYPE_ALIGN (tem));
1957 if (Present (Alignment_Clause (gnat_entity)))
1958 TYPE_USER_ALIGN (tem) = 1;
1961 TYPE_CONVENTION_FORTRAN_P (tem)
1962 = (Convention (gnat_entity) == Convention_Fortran);
1963 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1965 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1966 corresponding fat pointer. */
1967 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1968 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1969 TYPE_MODE (gnu_type) = BLKmode;
1970 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1971 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
1973 /* If the maximum size doesn't overflow, use it. */
1974 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1975 && !TREE_OVERFLOW (gnu_max_size))
1976 TYPE_SIZE (tem)
1977 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1978 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
1979 && !TREE_OVERFLOW (gnu_max_size_unit))
1980 TYPE_SIZE_UNIT (tem)
1981 = size_binop (MIN_EXPR, gnu_max_size_unit,
1982 TYPE_SIZE_UNIT (tem));
1984 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1985 tem, NULL, !Comes_From_Source (gnat_entity),
1986 debug_info_p, gnat_entity);
1988 /* Give the fat pointer type a name. */
1989 create_type_decl (create_concat_name (gnat_entity, "XUP"),
1990 gnu_fat_type, NULL, !Comes_From_Source (gnat_entity),
1991 debug_info_p, gnat_entity);
1993 /* Create the type to be used as what a thin pointer designates: an
1994 record type for the object and its template with the field offsets
1995 shifted to have the template at a negative offset. */
1996 tem = build_unc_object_type (gnu_template_type, tem,
1997 create_concat_name (gnat_entity, "XUT"));
1998 shift_unc_components_for_thin_pointers (tem);
2000 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2001 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2003 /* Give the thin pointer type a name. */
2004 create_type_decl (create_concat_name (gnat_entity, "XUX"),
2005 build_pointer_type (tem), NULL,
2006 !Comes_From_Source (gnat_entity), debug_info_p,
2007 gnat_entity);
2009 break;
2011 case E_String_Subtype:
2012 case E_Array_Subtype:
2014 /* This is the actual data type for array variables. Multidimensional
2015 arrays are implemented in the gnu tree as arrays of arrays. Note
2016 that for the moment arrays which have sparse enumeration subtypes as
2017 index components create sparse arrays, which is obviously space
2018 inefficient but so much easier to code for now.
2020 Also note that the subtype never refers to the unconstrained
2021 array type, which is somewhat at variance with Ada semantics.
2023 First check to see if this is simply a renaming of the array
2024 type. If so, the result is the array type. */
2026 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2027 if (!Is_Constrained (gnat_entity))
2028 break;
2029 else
2031 int index;
2032 int array_dim = Number_Dimensions (gnat_entity);
2033 int first_dim
2034 = ((Convention (gnat_entity) == Convention_Fortran)
2035 ? array_dim - 1 : 0);
2036 int next_dim
2037 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
2038 Entity_Id gnat_ind_subtype;
2039 Entity_Id gnat_ind_base_subtype;
2040 tree gnu_base_type = gnu_type;
2041 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
2042 tree gnu_comp_size = NULL_TREE;
2043 tree gnu_max_size = size_one_node;
2044 tree gnu_max_size_unit;
2045 bool need_index_type_struct = false;
2046 bool max_overflow = false;
2048 /* First create the gnu types for each index. Create types for
2049 debugging information to point to the index types if the
2050 are not integer types, have variable bounds, or are
2051 wider than sizetype. */
2053 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
2054 gnat_ind_base_subtype
2055 = First_Index (Implementation_Base_Type (gnat_entity));
2056 index < array_dim && index >= 0;
2057 index += next_dim,
2058 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
2059 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
2061 tree gnu_index_subtype
2062 = get_unpadded_type (Etype (gnat_ind_subtype));
2063 tree gnu_min
2064 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
2065 tree gnu_max
2066 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
2067 tree gnu_base_subtype
2068 = get_unpadded_type (Etype (gnat_ind_base_subtype));
2069 tree gnu_base_min
2070 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
2071 tree gnu_base_max
2072 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
2073 tree gnu_base_type = get_base_type (gnu_base_subtype);
2074 tree gnu_base_base_min
2075 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
2076 tree gnu_base_base_max
2077 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
2078 tree gnu_high;
2079 tree gnu_this_max;
2081 /* If the minimum and maximum values both overflow in
2082 SIZETYPE, but the difference in the original type
2083 does not overflow in SIZETYPE, ignore the overflow
2084 indications. */
2085 if ((TYPE_PRECISION (gnu_index_subtype)
2086 > TYPE_PRECISION (sizetype)
2087 || TYPE_UNSIGNED (gnu_index_subtype)
2088 != TYPE_UNSIGNED (sizetype))
2089 && TREE_CODE (gnu_min) == INTEGER_CST
2090 && TREE_CODE (gnu_max) == INTEGER_CST
2091 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2092 && (!TREE_OVERFLOW
2093 (fold_build2 (MINUS_EXPR, gnu_index_subtype,
2094 TYPE_MAX_VALUE (gnu_index_subtype),
2095 TYPE_MIN_VALUE (gnu_index_subtype)))))
2097 TREE_OVERFLOW (gnu_min) = 0;
2098 TREE_OVERFLOW (gnu_max) = 0;
2101 /* Similarly, if the range is null, use bounds of 1..0 for
2102 the sizetype bounds. */
2103 else if ((TYPE_PRECISION (gnu_index_subtype)
2104 > TYPE_PRECISION (sizetype)
2105 || TYPE_UNSIGNED (gnu_index_subtype)
2106 != TYPE_UNSIGNED (sizetype))
2107 && TREE_CODE (gnu_min) == INTEGER_CST
2108 && TREE_CODE (gnu_max) == INTEGER_CST
2109 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2110 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
2111 TYPE_MIN_VALUE (gnu_index_subtype)))
2112 gnu_min = size_one_node, gnu_max = size_zero_node;
2114 /* Now compute the size of this bound. We need to provide
2115 GCC with an upper bound to use but have to deal with the
2116 "superflat" case. There are three ways to do this. If we
2117 can prove that the array can never be superflat, we can
2118 just use the high bound of the index subtype. If we can
2119 prove that the low bound minus one can't overflow, we
2120 can do this as MAX (hb, lb - 1). Otherwise, we have to use
2121 the expression hb >= lb ? hb : lb - 1. */
2122 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
2124 /* See if the base array type is already flat. If it is, we
2125 are probably compiling an ACVC test, but it will cause the
2126 code below to malfunction if we don't handle it specially. */
2127 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2128 && TREE_CODE (gnu_base_max) == INTEGER_CST
2129 && !TREE_OVERFLOW (gnu_base_min)
2130 && !TREE_OVERFLOW (gnu_base_max)
2131 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
2132 gnu_high = size_zero_node, gnu_min = size_one_node;
2134 /* If gnu_high is now an integer which overflowed, the array
2135 cannot be superflat. */
2136 else if (TREE_CODE (gnu_high) == INTEGER_CST
2137 && TREE_OVERFLOW (gnu_high))
2138 gnu_high = gnu_max;
2139 else if (TYPE_UNSIGNED (gnu_base_subtype)
2140 || TREE_CODE (gnu_high) == INTEGER_CST)
2141 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
2142 else
2143 gnu_high
2144 = build_cond_expr
2145 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
2146 gnu_max, gnu_min),
2147 gnu_max, gnu_high);
2149 gnu_index_type[index]
2150 = create_index_type (gnu_min, gnu_high, gnu_index_subtype,
2151 gnat_entity);
2153 /* Also compute the maximum size of the array. Here we
2154 see if any constraint on the index type of the base type
2155 can be used in the case of self-referential bound on
2156 the index type of the subtype. We look for a non-"infinite"
2157 and non-self-referential bound from any type involved and
2158 handle each bound separately. */
2160 if ((TREE_CODE (gnu_min) == INTEGER_CST
2161 && !TREE_OVERFLOW (gnu_min)
2162 && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
2163 || !CONTAINS_PLACEHOLDER_P (gnu_min)
2164 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2165 && !TREE_OVERFLOW (gnu_base_min)))
2166 gnu_base_min = gnu_min;
2168 if ((TREE_CODE (gnu_max) == INTEGER_CST
2169 && !TREE_OVERFLOW (gnu_max)
2170 && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
2171 || !CONTAINS_PLACEHOLDER_P (gnu_max)
2172 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2173 && !TREE_OVERFLOW (gnu_base_max)))
2174 gnu_base_max = gnu_max;
2176 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2177 && TREE_OVERFLOW (gnu_base_min))
2178 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2179 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2180 && TREE_OVERFLOW (gnu_base_max))
2181 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2182 max_overflow = true;
2184 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
2185 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
2187 gnu_this_max
2188 = size_binop (MAX_EXPR,
2189 size_binop (PLUS_EXPR, size_one_node,
2190 size_binop (MINUS_EXPR, gnu_base_max,
2191 gnu_base_min)),
2192 size_zero_node);
2194 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2195 && TREE_OVERFLOW (gnu_this_max))
2196 max_overflow = true;
2198 gnu_max_size
2199 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2201 if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
2202 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
2203 != INTEGER_CST)
2204 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
2205 || (TREE_TYPE (gnu_index_subtype)
2206 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
2207 != INTEGER_TYPE))
2208 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
2209 || (TYPE_PRECISION (gnu_index_subtype)
2210 > TYPE_PRECISION (sizetype)))
2211 need_index_type_struct = true;
2214 /* Then flatten: create the array of arrays. For an array type
2215 used to implement a packed array, get the component type from
2216 the original array type since the representation clauses that
2217 can affect it are on the latter. */
2218 if (Is_Packed_Array_Type (gnat_entity)
2219 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2221 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2222 for (index = array_dim - 1; index >= 0; index--)
2223 gnu_type = TREE_TYPE (gnu_type);
2225 /* One of the above calls might have caused us to be elaborated,
2226 so don't blow up if so. */
2227 if (present_gnu_tree (gnat_entity))
2229 maybe_present = true;
2230 break;
2233 else
2235 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
2237 /* One of the above calls might have caused us to be elaborated,
2238 so don't blow up if so. */
2239 if (present_gnu_tree (gnat_entity))
2241 maybe_present = true;
2242 break;
2245 /* Try to get a smaller form of the component if needed. */
2246 if ((Is_Packed (gnat_entity)
2247 || Has_Component_Size_Clause (gnat_entity))
2248 && !Is_Bit_Packed_Array (gnat_entity)
2249 && !Has_Aliased_Components (gnat_entity)
2250 && !Strict_Alignment (Component_Type (gnat_entity))
2251 && TREE_CODE (gnu_type) == RECORD_TYPE
2252 && host_integerp (TYPE_SIZE (gnu_type), 1))
2253 gnu_type = make_packable_type (gnu_type, false);
2255 /* Get and validate any specified Component_Size, but if Packed,
2256 ignore it since the front end will have taken care of it. */
2257 gnu_comp_size
2258 = validate_size (Component_Size (gnat_entity), gnu_type,
2259 gnat_entity,
2260 (Is_Bit_Packed_Array (gnat_entity)
2261 ? TYPE_DECL : VAR_DECL), true,
2262 Has_Component_Size_Clause (gnat_entity));
2264 /* If the component type is a RECORD_TYPE that has a
2265 self-referential size, use the maxium size. */
2266 if (!gnu_comp_size
2267 && TREE_CODE (gnu_type) == RECORD_TYPE
2268 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2269 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
2271 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
2273 tree orig_gnu_type;
2274 gnu_type
2275 = make_type_from_size (gnu_type, gnu_comp_size, false);
2276 orig_gnu_type = gnu_type;
2277 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
2278 gnat_entity, "C_PAD", false,
2279 definition, true);
2280 /* If a padding record was made, declare it now since it
2281 will never be declared otherwise. This is necessary
2282 to ensure that its subtrees are properly marked. */
2283 if (gnu_type != orig_gnu_type)
2284 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL,
2285 true, false, gnat_entity);
2288 if (Has_Volatile_Components (Base_Type (gnat_entity)))
2289 gnu_type = build_qualified_type (gnu_type,
2290 (TYPE_QUALS (gnu_type)
2291 | TYPE_QUAL_VOLATILE));
2294 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2295 TYPE_SIZE_UNIT (gnu_type));
2296 gnu_max_size = size_binop (MULT_EXPR,
2297 convert (bitsizetype, gnu_max_size),
2298 TYPE_SIZE (gnu_type));
2300 for (index = array_dim - 1; index >= 0; index --)
2302 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
2303 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2304 if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2305 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2308 /* If we are at file level and this is a multi-dimensional array, we
2309 need to make a variable corresponding to the stride of the
2310 inner dimensions. */
2311 if (global_bindings_p () && array_dim > 1)
2313 tree gnu_str_name = get_identifier ("ST");
2314 tree gnu_arr_type;
2316 for (gnu_arr_type = TREE_TYPE (gnu_type);
2317 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2318 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2319 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
2321 tree eltype = TREE_TYPE (gnu_arr_type);
2323 TYPE_SIZE (gnu_arr_type)
2324 = elaborate_expression_1 (gnat_entity, gnat_entity,
2325 TYPE_SIZE (gnu_arr_type),
2326 gnu_str_name, definition, 0);
2328 /* ??? For now, store the size as a multiple of the
2329 alignment of the element type in bytes so that we
2330 can see the alignment from the tree. */
2331 TYPE_SIZE_UNIT (gnu_arr_type)
2332 = build_binary_op
2333 (MULT_EXPR, sizetype,
2334 elaborate_expression_1
2335 (gnat_entity, gnat_entity,
2336 build_binary_op (EXACT_DIV_EXPR, sizetype,
2337 TYPE_SIZE_UNIT (gnu_arr_type),
2338 size_int (TYPE_ALIGN (eltype)
2339 / BITS_PER_UNIT)),
2340 concat_id_with_name (gnu_str_name, "A_U"),
2341 definition, 0),
2342 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2344 /* ??? create_type_decl is not invoked on the inner types so
2345 the MULT_EXPR node built above will never be marked. */
2346 mark_visited (&TYPE_SIZE_UNIT (gnu_arr_type));
2350 /* If we need to write out a record type giving the names of
2351 the bounds, do it now. */
2352 if (need_index_type_struct && debug_info_p)
2354 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
2355 tree gnu_field_list = NULL_TREE;
2356 tree gnu_field;
2358 TYPE_NAME (gnu_bound_rec_type)
2359 = create_concat_name (gnat_entity, "XA");
2361 for (index = array_dim - 1; index >= 0; index--)
2363 tree gnu_type_name
2364 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
2366 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
2367 gnu_type_name = DECL_NAME (gnu_type_name);
2369 gnu_field = create_field_decl (gnu_type_name,
2370 integer_type_node,
2371 gnu_bound_rec_type,
2372 0, NULL_TREE, NULL_TREE, 0);
2373 TREE_CHAIN (gnu_field) = gnu_field_list;
2374 gnu_field_list = gnu_field;
2377 finish_record_type (gnu_bound_rec_type, gnu_field_list,
2378 0, false);
2381 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2382 = (Convention (gnat_entity) == Convention_Fortran);
2383 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2384 = (Is_Packed_Array_Type (gnat_entity)
2385 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2387 /* If our size depends on a placeholder and the maximum size doesn't
2388 overflow, use it. */
2389 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2390 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2391 && TREE_OVERFLOW (gnu_max_size))
2392 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2393 && TREE_OVERFLOW (gnu_max_size_unit))
2394 && !max_overflow)
2396 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2397 TYPE_SIZE (gnu_type));
2398 TYPE_SIZE_UNIT (gnu_type)
2399 = size_binop (MIN_EXPR, gnu_max_size_unit,
2400 TYPE_SIZE_UNIT (gnu_type));
2403 /* Set our alias set to that of our base type. This gives all
2404 array subtypes the same alias set. */
2405 copy_alias_set (gnu_type, gnu_base_type);
2408 /* If this is a packed type, make this type the same as the packed
2409 array type, but do some adjusting in the type first. */
2411 if (Present (Packed_Array_Type (gnat_entity)))
2413 Entity_Id gnat_index;
2414 tree gnu_inner_type;
2416 /* First finish the type we had been making so that we output
2417 debugging information for it */
2418 gnu_type
2419 = build_qualified_type (gnu_type,
2420 (TYPE_QUALS (gnu_type)
2421 | (TYPE_QUAL_VOLATILE
2422 * Treat_As_Volatile (gnat_entity))));
2423 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2424 !Comes_From_Source (gnat_entity),
2425 debug_info_p, gnat_entity);
2426 if (!Comes_From_Source (gnat_entity))
2427 DECL_ARTIFICIAL (gnu_decl) = 1;
2429 /* Save it as our equivalent in case the call below elaborates
2430 this type again. */
2431 save_gnu_tree (gnat_entity, gnu_decl, false);
2433 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2434 NULL_TREE, 0);
2435 this_made_decl = true;
2436 gnu_type = TREE_TYPE (gnu_decl);
2437 save_gnu_tree (gnat_entity, NULL_TREE, false);
2439 gnu_inner_type = gnu_type;
2440 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2441 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2442 || TYPE_IS_PADDING_P (gnu_inner_type)))
2443 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2445 /* We need to point the type we just made to our index type so
2446 the actual bounds can be put into a template. */
2448 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2449 && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2450 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2451 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2453 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2455 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2456 If it is, we need to make another type. */
2457 if (TYPE_MODULAR_P (gnu_inner_type))
2459 tree gnu_subtype;
2461 gnu_subtype = make_node (INTEGER_TYPE);
2463 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2464 TYPE_MIN_VALUE (gnu_subtype)
2465 = TYPE_MIN_VALUE (gnu_inner_type);
2466 TYPE_MAX_VALUE (gnu_subtype)
2467 = TYPE_MAX_VALUE (gnu_inner_type);
2468 TYPE_PRECISION (gnu_subtype)
2469 = TYPE_PRECISION (gnu_inner_type);
2470 TYPE_UNSIGNED (gnu_subtype)
2471 = TYPE_UNSIGNED (gnu_inner_type);
2472 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2473 layout_type (gnu_subtype);
2475 gnu_inner_type = gnu_subtype;
2478 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2481 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2483 for (gnat_index = First_Index (gnat_entity);
2484 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2485 SET_TYPE_ACTUAL_BOUNDS
2486 (gnu_inner_type,
2487 tree_cons (NULL_TREE,
2488 get_unpadded_type (Etype (gnat_index)),
2489 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2491 if (Convention (gnat_entity) != Convention_Fortran)
2492 SET_TYPE_ACTUAL_BOUNDS
2493 (gnu_inner_type,
2494 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2496 if (TREE_CODE (gnu_type) == RECORD_TYPE
2497 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2498 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2502 /* Abort if packed array with no packed array type field set. */
2503 else
2504 gcc_assert (!Is_Packed (gnat_entity));
2506 break;
2508 case E_String_Literal_Subtype:
2509 /* Create the type for a string literal. */
2511 Entity_Id gnat_full_type
2512 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2513 && Present (Full_View (Etype (gnat_entity)))
2514 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2515 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2516 tree gnu_string_array_type
2517 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2518 tree gnu_string_index_type
2519 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2520 (TYPE_DOMAIN (gnu_string_array_type))));
2521 tree gnu_lower_bound
2522 = convert (gnu_string_index_type,
2523 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2524 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2525 tree gnu_length = ssize_int (length - 1);
2526 tree gnu_upper_bound
2527 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2528 gnu_lower_bound,
2529 convert (gnu_string_index_type, gnu_length));
2530 tree gnu_range_type
2531 = build_range_type (gnu_string_index_type,
2532 gnu_lower_bound, gnu_upper_bound);
2533 tree gnu_index_type
2534 = create_index_type (convert (sizetype,
2535 TYPE_MIN_VALUE (gnu_range_type)),
2536 convert (sizetype,
2537 TYPE_MAX_VALUE (gnu_range_type)),
2538 gnu_range_type, gnat_entity);
2540 gnu_type
2541 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2542 gnu_index_type);
2543 copy_alias_set (gnu_type, gnu_string_type);
2545 break;
2547 /* Record Types and Subtypes
2549 The following fields are defined on record types:
2551 Has_Discriminants True if the record has discriminants
2552 First_Discriminant Points to head of list of discriminants
2553 First_Entity Points to head of list of fields
2554 Is_Tagged_Type True if the record is tagged
2556 Implementation of Ada records and discriminated records:
2558 A record type definition is transformed into the equivalent of a C
2559 struct definition. The fields that are the discriminants which are
2560 found in the Full_Type_Declaration node and the elements of the
2561 Component_List found in the Record_Type_Definition node. The
2562 Component_List can be a recursive structure since each Variant of
2563 the Variant_Part of the Component_List has a Component_List.
2565 Processing of a record type definition comprises starting the list of
2566 field declarations here from the discriminants and the calling the
2567 function components_to_record to add the rest of the fields from the
2568 component list and return the gnu type node. The function
2569 components_to_record will call itself recursively as it traverses
2570 the tree. */
2572 case E_Record_Type:
2573 if (Has_Complex_Representation (gnat_entity))
2575 gnu_type
2576 = build_complex_type
2577 (get_unpadded_type
2578 (Etype (Defining_Entity
2579 (First (Component_Items
2580 (Component_List
2581 (Type_Definition
2582 (Declaration_Node (gnat_entity)))))))));
2584 break;
2588 Node_Id full_definition = Declaration_Node (gnat_entity);
2589 Node_Id record_definition = Type_Definition (full_definition);
2590 Entity_Id gnat_field;
2591 tree gnu_field;
2592 tree gnu_field_list = NULL_TREE;
2593 tree gnu_get_parent;
2594 /* Set PACKED in keeping with gnat_to_gnu_field. */
2595 int packed
2596 = Is_Packed (gnat_entity)
2598 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2599 ? -1
2600 : (Known_Alignment (gnat_entity)
2601 || (Strict_Alignment (gnat_entity)
2602 && Known_Static_Esize (gnat_entity)))
2603 ? -2
2604 : 0;
2605 bool has_rep = Has_Specified_Layout (gnat_entity);
2606 bool all_rep = has_rep;
2607 bool is_extension
2608 = (Is_Tagged_Type (gnat_entity)
2609 && Nkind (record_definition) == N_Derived_Type_Definition);
2611 /* See if all fields have a rep clause. Stop when we find one
2612 that doesn't. */
2613 for (gnat_field = First_Entity (gnat_entity);
2614 Present (gnat_field) && all_rep;
2615 gnat_field = Next_Entity (gnat_field))
2616 if ((Ekind (gnat_field) == E_Component
2617 || Ekind (gnat_field) == E_Discriminant)
2618 && No (Component_Clause (gnat_field)))
2619 all_rep = false;
2621 /* If this is a record extension, go a level further to find the
2622 record definition. Also, verify we have a Parent_Subtype. */
2623 if (is_extension)
2625 if (!type_annotate_only
2626 || Present (Record_Extension_Part (record_definition)))
2627 record_definition = Record_Extension_Part (record_definition);
2629 gcc_assert (type_annotate_only
2630 || Present (Parent_Subtype (gnat_entity)));
2633 /* Make a node for the record. If we are not defining the record,
2634 suppress expanding incomplete types. */
2635 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2636 TYPE_NAME (gnu_type) = gnu_entity_id;
2637 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2639 if (!definition)
2640 defer_incomplete_level++, this_deferred = true;
2642 /* If both a size and rep clause was specified, put the size in
2643 the record type now so that it can get the proper mode. */
2644 if (has_rep && Known_Esize (gnat_entity))
2645 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2647 /* Always set the alignment here so that it can be used to
2648 set the mode, if it is making the alignment stricter. If
2649 it is invalid, it will be checked again below. If this is to
2650 be Atomic, choose a default alignment of a word unless we know
2651 the size and it's smaller. */
2652 if (Known_Alignment (gnat_entity))
2653 TYPE_ALIGN (gnu_type)
2654 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2655 else if (Is_Atomic (gnat_entity))
2656 TYPE_ALIGN (gnu_type)
2657 = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2658 /* If a type needs strict alignment, the minimum size will be the
2659 type size instead of the RM size (see validate_size). Cap the
2660 alignment, lest it causes this type size to become too large. */
2661 else if (Strict_Alignment (gnat_entity)
2662 && Known_Static_Esize (gnat_entity))
2664 unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2665 unsigned int raw_align = raw_size & -raw_size;
2666 if (raw_align < BIGGEST_ALIGNMENT)
2667 TYPE_ALIGN (gnu_type) = raw_align;
2669 else
2670 TYPE_ALIGN (gnu_type) = 0;
2672 /* If we have a Parent_Subtype, make a field for the parent. If
2673 this record has rep clauses, force the position to zero. */
2674 if (Present (Parent_Subtype (gnat_entity)))
2676 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2677 tree gnu_parent;
2679 /* A major complexity here is that the parent subtype will
2680 reference our discriminants in its Discriminant_Constraint
2681 list. But those must reference the parent component of this
2682 record which is of the parent subtype we have not built yet!
2683 To break the circle we first build a dummy COMPONENT_REF which
2684 represents the "get to the parent" operation and initialize
2685 each of those discriminants to a COMPONENT_REF of the above
2686 dummy parent referencing the corresponding discriminant of the
2687 base type of the parent subtype. */
2688 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2689 build0 (PLACEHOLDER_EXPR, gnu_type),
2690 build_decl (FIELD_DECL, NULL_TREE,
2691 void_type_node),
2692 NULL_TREE);
2694 if (Has_Discriminants (gnat_entity))
2695 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2696 Present (gnat_field);
2697 gnat_field = Next_Stored_Discriminant (gnat_field))
2698 if (Present (Corresponding_Discriminant (gnat_field)))
2699 save_gnu_tree
2700 (gnat_field,
2701 build3 (COMPONENT_REF,
2702 get_unpadded_type (Etype (gnat_field)),
2703 gnu_get_parent,
2704 gnat_to_gnu_field_decl (Corresponding_Discriminant
2705 (gnat_field)),
2706 NULL_TREE),
2707 true);
2709 /* Then we build the parent subtype. */
2710 gnu_parent = gnat_to_gnu_type (gnat_parent);
2712 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2713 initially built. The discriminants must reference the fields
2714 of the parent subtype and not those of its base type for the
2715 placeholder machinery to properly work. */
2716 if (Has_Discriminants (gnat_entity))
2717 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2718 Present (gnat_field);
2719 gnat_field = Next_Stored_Discriminant (gnat_field))
2720 if (Present (Corresponding_Discriminant (gnat_field)))
2722 Entity_Id field = Empty;
2723 for (field = First_Stored_Discriminant (gnat_parent);
2724 Present (field);
2725 field = Next_Stored_Discriminant (field))
2726 if (same_discriminant_p (gnat_field, field))
2727 break;
2728 gcc_assert (Present (field));
2729 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2730 = gnat_to_gnu_field_decl (field);
2733 /* The "get to the parent" COMPONENT_REF must be given its
2734 proper type... */
2735 TREE_TYPE (gnu_get_parent) = gnu_parent;
2737 /* ...and reference the _parent field of this record. */
2738 gnu_field_list
2739 = create_field_decl (get_identifier
2740 (Get_Name_String (Name_uParent)),
2741 gnu_parent, gnu_type, 0,
2742 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2743 has_rep ? bitsize_zero_node : 0, 1);
2744 DECL_INTERNAL_P (gnu_field_list) = 1;
2745 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2748 /* Make the fields for the discriminants and put them into the record
2749 unless it's an Unchecked_Union. */
2750 if (Has_Discriminants (gnat_entity))
2751 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2752 Present (gnat_field);
2753 gnat_field = Next_Stored_Discriminant (gnat_field))
2755 /* If this is a record extension and this discriminant
2756 is the renaming of another discriminant, we've already
2757 handled the discriminant above. */
2758 if (Present (Parent_Subtype (gnat_entity))
2759 && Present (Corresponding_Discriminant (gnat_field)))
2760 continue;
2762 gnu_field
2763 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2765 /* Make an expression using a PLACEHOLDER_EXPR from the
2766 FIELD_DECL node just created and link that with the
2767 corresponding GNAT defining identifier. Then add to the
2768 list of fields. */
2769 save_gnu_tree (gnat_field,
2770 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2771 build0 (PLACEHOLDER_EXPR,
2772 DECL_CONTEXT (gnu_field)),
2773 gnu_field, NULL_TREE),
2774 true);
2776 if (!Is_Unchecked_Union (gnat_entity))
2778 TREE_CHAIN (gnu_field) = gnu_field_list;
2779 gnu_field_list = gnu_field;
2783 /* Put the discriminants into the record (backwards), so we can
2784 know the appropriate discriminant to use for the names of the
2785 variants. */
2786 TYPE_FIELDS (gnu_type) = gnu_field_list;
2788 /* Add the listed fields into the record and finish it up. */
2789 components_to_record (gnu_type, Component_List (record_definition),
2790 gnu_field_list, packed, definition, NULL,
2791 false, all_rep, false,
2792 Is_Unchecked_Union (gnat_entity));
2794 /* We used to remove the associations of the discriminants and
2795 _Parent for validity checking, but we may need them if there's
2796 Freeze_Node for a subtype used in this record. */
2797 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2798 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2800 /* If it is a tagged record force the type to BLKmode to insure
2801 that these objects will always be placed in memory. Do the
2802 same thing for limited record types. */
2803 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2804 TYPE_MODE (gnu_type) = BLKmode;
2806 /* If this is a derived type, we must make the alias set of this type
2807 the same as that of the type we are derived from. We assume here
2808 that the other type is already frozen. */
2809 if (Etype (gnat_entity) != gnat_entity
2810 && !(Is_Private_Type (Etype (gnat_entity))
2811 && Full_View (Etype (gnat_entity)) == gnat_entity))
2812 copy_alias_set (gnu_type, gnat_to_gnu_type (Etype (gnat_entity)));
2814 /* Fill in locations of fields. */
2815 annotate_rep (gnat_entity, gnu_type);
2817 /* If there are any entities in the chain corresponding to
2818 components that we did not elaborate, ensure we elaborate their
2819 types if they are Itypes. */
2820 for (gnat_temp = First_Entity (gnat_entity);
2821 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2822 if ((Ekind (gnat_temp) == E_Component
2823 || Ekind (gnat_temp) == E_Discriminant)
2824 && Is_Itype (Etype (gnat_temp))
2825 && !present_gnu_tree (gnat_temp))
2826 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2828 break;
2830 case E_Class_Wide_Subtype:
2831 /* If an equivalent type is present, that is what we should use.
2832 Otherwise, fall through to handle this like a record subtype
2833 since it may have constraints. */
2834 if (gnat_equiv_type != gnat_entity)
2836 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
2837 maybe_present = true;
2838 break;
2841 /* ... fall through ... */
2843 case E_Record_Subtype:
2845 /* If Cloned_Subtype is Present it means this record subtype has
2846 identical layout to that type or subtype and we should use
2847 that GCC type for this one. The front end guarantees that
2848 the component list is shared. */
2849 if (Present (Cloned_Subtype (gnat_entity)))
2851 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2852 NULL_TREE, 0);
2853 maybe_present = true;
2856 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2857 changing the type, make a new type with each field having the
2858 type of the field in the new subtype but having the position
2859 computed by transforming every discriminant reference according
2860 to the constraints. We don't see any difference between
2861 private and nonprivate type here since derivations from types should
2862 have been deferred until the completion of the private type. */
2863 else
2865 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2866 tree gnu_base_type;
2867 tree gnu_orig_type;
2869 if (!definition)
2870 defer_incomplete_level++, this_deferred = true;
2872 /* Get the base type initially for its alignment and sizes. But
2873 if it is a padded type, we do all the other work with the
2874 unpadded type. */
2875 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
2877 if (TREE_CODE (gnu_base_type) == RECORD_TYPE
2878 && TYPE_IS_PADDING_P (gnu_base_type))
2879 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
2880 else
2881 gnu_type = gnu_orig_type = gnu_base_type;
2883 if (present_gnu_tree (gnat_entity))
2885 maybe_present = true;
2886 break;
2889 /* When the type has discriminants, and these discriminants
2890 affect the shape of what it built, factor them in.
2892 If we are making a subtype of an Unchecked_Union (must be an
2893 Itype), just return the type.
2895 We can't just use Is_Constrained because private subtypes without
2896 discriminants of full types with discriminants with default
2897 expressions are Is_Constrained but aren't constrained! */
2899 if (IN (Ekind (gnat_base_type), Record_Kind)
2900 && !Is_For_Access_Subtype (gnat_entity)
2901 && !Is_Unchecked_Union (gnat_base_type)
2902 && Is_Constrained (gnat_entity)
2903 && Stored_Constraint (gnat_entity) != No_Elist
2904 && Present (Discriminant_Constraint (gnat_entity)))
2906 Entity_Id gnat_field;
2907 tree gnu_field_list = 0;
2908 tree gnu_pos_list
2909 = compute_field_positions (gnu_orig_type, NULL_TREE,
2910 size_zero_node, bitsize_zero_node,
2911 BIGGEST_ALIGNMENT);
2912 tree gnu_subst_list
2913 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2914 definition);
2915 tree gnu_temp;
2917 gnu_type = make_node (RECORD_TYPE);
2918 TYPE_NAME (gnu_type) = gnu_entity_id;
2919 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2920 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2922 for (gnat_field = First_Entity (gnat_entity);
2923 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2924 if ((Ekind (gnat_field) == E_Component
2925 || Ekind (gnat_field) == E_Discriminant)
2926 && (Underlying_Type (Scope (Original_Record_Component
2927 (gnat_field)))
2928 == gnat_base_type)
2929 && (No (Corresponding_Discriminant (gnat_field))
2930 || !Is_Tagged_Type (gnat_base_type)))
2932 tree gnu_old_field
2933 = gnat_to_gnu_field_decl (Original_Record_Component
2934 (gnat_field));
2935 tree gnu_offset
2936 = TREE_VALUE (purpose_member (gnu_old_field,
2937 gnu_pos_list));
2938 tree gnu_pos = TREE_PURPOSE (gnu_offset);
2939 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
2940 tree gnu_field_type
2941 = gnat_to_gnu_type (Etype (gnat_field));
2942 tree gnu_size = TYPE_SIZE (gnu_field_type);
2943 tree gnu_new_pos = 0;
2944 unsigned int offset_align
2945 = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
2947 tree gnu_field;
2949 /* If there was a component clause, the field types must be
2950 the same for the type and subtype, so copy the data from
2951 the old field to avoid recomputation here. Also if the
2952 field is justified modular and the optimization in
2953 gnat_to_gnu_field was applied. */
2954 if (Present (Component_Clause
2955 (Original_Record_Component (gnat_field)))
2956 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
2957 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
2958 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
2959 == TREE_TYPE (gnu_old_field)))
2961 gnu_size = DECL_SIZE (gnu_old_field);
2962 gnu_field_type = TREE_TYPE (gnu_old_field);
2965 /* If the old field was packed and of constant size, we
2966 have to get the old size here, as it might differ from
2967 what the Etype conveys and the latter might overlap
2968 onto the following field. Try to arrange the type for
2969 possible better packing along the way. */
2970 else if (DECL_PACKED (gnu_old_field)
2971 && TREE_CODE (DECL_SIZE (gnu_old_field))
2972 == INTEGER_CST)
2974 gnu_size = DECL_SIZE (gnu_old_field);
2975 if (TYPE_MODE (gnu_field_type) == BLKmode
2976 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2977 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2978 gnu_field_type
2979 = make_packable_type (gnu_field_type, true);
2982 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
2983 for (gnu_temp = gnu_subst_list;
2984 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2985 gnu_pos = substitute_in_expr (gnu_pos,
2986 TREE_PURPOSE (gnu_temp),
2987 TREE_VALUE (gnu_temp));
2989 /* If the size is now a constant, we can set it as the
2990 size of the field when we make it. Otherwise, we need
2991 to deal with it specially. */
2992 if (TREE_CONSTANT (gnu_pos))
2993 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2995 gnu_field
2996 = create_field_decl
2997 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2998 DECL_PACKED (gnu_old_field), gnu_size, gnu_new_pos,
2999 !DECL_NONADDRESSABLE_P (gnu_old_field));
3001 if (!TREE_CONSTANT (gnu_pos))
3003 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
3004 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
3005 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
3006 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
3007 DECL_SIZE (gnu_field) = gnu_size;
3008 DECL_SIZE_UNIT (gnu_field)
3009 = convert (sizetype,
3010 size_binop (CEIL_DIV_EXPR, gnu_size,
3011 bitsize_unit_node));
3012 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
3015 DECL_INTERNAL_P (gnu_field)
3016 = DECL_INTERNAL_P (gnu_old_field);
3017 SET_DECL_ORIGINAL_FIELD
3018 (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
3019 ? DECL_ORIGINAL_FIELD (gnu_old_field)
3020 : gnu_old_field));
3021 DECL_DISCRIMINANT_NUMBER (gnu_field)
3022 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
3023 TREE_THIS_VOLATILE (gnu_field)
3024 = TREE_THIS_VOLATILE (gnu_old_field);
3025 TREE_CHAIN (gnu_field) = gnu_field_list;
3026 gnu_field_list = gnu_field;
3027 save_gnu_tree (gnat_field, gnu_field, false);
3030 /* Now go through the entities again looking for Itypes that
3031 we have not elaborated but should (e.g., Etypes of fields
3032 that have Original_Components). */
3033 for (gnat_field = First_Entity (gnat_entity);
3034 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3035 if ((Ekind (gnat_field) == E_Discriminant
3036 || Ekind (gnat_field) == E_Component)
3037 && !present_gnu_tree (Etype (gnat_field)))
3038 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3040 /* Do not finalize it since we're going to modify it below. */
3041 finish_record_type (gnu_type, nreverse (gnu_field_list),
3042 2, true);
3044 /* Now set the size, alignment and alias set of the new type to
3045 match that of the old one, doing any substitutions, as
3046 above. */
3047 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
3048 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
3049 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
3050 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
3051 copy_alias_set (gnu_type, gnu_base_type);
3053 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3054 for (gnu_temp = gnu_subst_list;
3055 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3056 TYPE_SIZE (gnu_type)
3057 = substitute_in_expr (TYPE_SIZE (gnu_type),
3058 TREE_PURPOSE (gnu_temp),
3059 TREE_VALUE (gnu_temp));
3061 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
3062 for (gnu_temp = gnu_subst_list;
3063 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3064 TYPE_SIZE_UNIT (gnu_type)
3065 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
3066 TREE_PURPOSE (gnu_temp),
3067 TREE_VALUE (gnu_temp));
3069 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
3070 for (gnu_temp = gnu_subst_list;
3071 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3072 SET_TYPE_ADA_SIZE
3073 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
3074 TREE_PURPOSE (gnu_temp),
3075 TREE_VALUE (gnu_temp)));
3077 /* Reapply variable_size since we have changed the sizes. */
3078 TYPE_SIZE (gnu_type) = variable_size (TYPE_SIZE (gnu_type));
3079 TYPE_SIZE_UNIT (gnu_type)
3080 = variable_size (TYPE_SIZE_UNIT (gnu_type));
3082 /* Recompute the mode of this record type now that we know its
3083 actual size. */
3084 compute_record_mode (gnu_type);
3086 /* Fill in locations of fields. */
3087 annotate_rep (gnat_entity, gnu_type);
3089 /* We've built a new type, make an XVS type to show what this
3090 is a subtype of. Some debuggers require the XVS type to be
3091 output first, so do it in that order. */
3092 if (debug_info_p)
3094 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3095 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
3097 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
3098 gnu_orig_name = DECL_NAME (gnu_orig_name);
3100 TYPE_NAME (gnu_subtype_marker)
3101 = create_concat_name (gnat_entity, "XVS");
3102 finish_record_type (gnu_subtype_marker,
3103 create_field_decl (gnu_orig_name,
3104 integer_type_node,
3105 gnu_subtype_marker,
3106 0, NULL_TREE,
3107 NULL_TREE, 0),
3108 0, false);
3111 /* Now we can finalize it. */
3112 rest_of_record_type_compilation (gnu_type);
3115 /* Otherwise, go down all the components in the new type and
3116 make them equivalent to those in the base type. */
3117 else
3118 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3119 gnat_temp = Next_Entity (gnat_temp))
3120 if ((Ekind (gnat_temp) == E_Discriminant
3121 && !Is_Unchecked_Union (gnat_base_type))
3122 || Ekind (gnat_temp) == E_Component)
3123 save_gnu_tree (gnat_temp,
3124 gnat_to_gnu_field_decl
3125 (Original_Record_Component (gnat_temp)), false);
3127 break;
3129 case E_Access_Subprogram_Type:
3130 /* Use the special descriptor type for dispatch tables if needed,
3131 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3132 Note that we are only required to do so for static tables in
3133 order to be compatible with the C++ ABI, but Ada 2005 allows
3134 to extend library level tagged types at the local level so
3135 we do it in the non-static case as well. */
3136 if (TARGET_VTABLE_USES_DESCRIPTORS
3137 && Is_Dispatch_Table_Entity (gnat_entity))
3139 gnu_type = fdesc_type_node;
3140 gnu_size = TYPE_SIZE (gnu_type);
3141 break;
3144 /* ... fall through ... */
3146 case E_Anonymous_Access_Subprogram_Type:
3147 /* If we are not defining this entity, and we have incomplete
3148 entities being processed above us, make a dummy type and
3149 fill it in later. */
3150 if (!definition && defer_incomplete_level != 0)
3152 struct incomplete *p
3153 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3155 gnu_type
3156 = build_pointer_type
3157 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3158 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3159 !Comes_From_Source (gnat_entity),
3160 debug_info_p, gnat_entity);
3161 this_made_decl = true;
3162 gnu_type = TREE_TYPE (gnu_decl);
3163 save_gnu_tree (gnat_entity, gnu_decl, false);
3164 saved = true;
3166 p->old_type = TREE_TYPE (gnu_type);
3167 p->full_type = Directly_Designated_Type (gnat_entity);
3168 p->next = defer_incomplete_list;
3169 defer_incomplete_list = p;
3170 break;
3173 /* ... fall through ... */
3175 case E_Allocator_Type:
3176 case E_Access_Type:
3177 case E_Access_Attribute_Type:
3178 case E_Anonymous_Access_Type:
3179 case E_General_Access_Type:
3181 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3182 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3183 bool is_from_limited_with
3184 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3185 && From_With_Type (gnat_desig_equiv));
3187 /* Get the "full view" of this entity. If this is an incomplete
3188 entity from a limited with, treat its non-limited view as the full
3189 view. Otherwise, if this is an incomplete or private type, use the
3190 full view. In the former case, we might point to a private type,
3191 in which case, we need its full view. Also, we want to look at the
3192 actual type used for the representation, so this takes a total of
3193 three steps. */
3194 Entity_Id gnat_desig_full_direct_first
3195 = (is_from_limited_with ? Non_Limited_View (gnat_desig_equiv)
3196 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3197 ? Full_View (gnat_desig_equiv) : Empty));
3198 Entity_Id gnat_desig_full_direct
3199 = ((is_from_limited_with
3200 && Present (gnat_desig_full_direct_first)
3201 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3202 ? Full_View (gnat_desig_full_direct_first)
3203 : gnat_desig_full_direct_first);
3204 Entity_Id gnat_desig_full
3205 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3207 /* This the type actually used to represent the designated type,
3208 either gnat_desig_full or gnat_desig_equiv. */
3209 Entity_Id gnat_desig_rep;
3211 /* Nonzero if this is a pointer to an unconstrained array. */
3212 bool is_unconstrained_array;
3214 /* We want to know if we'll be seeing the freeze node for any
3215 incomplete type we may be pointing to. */
3216 bool in_main_unit
3217 = (Present (gnat_desig_full)
3218 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3219 : In_Extended_Main_Code_Unit (gnat_desig_type));
3221 /* Nonzero if we make a dummy type here. */
3222 bool got_fat_p = false;
3223 /* Nonzero if the dummy is a fat pointer. */
3224 bool made_dummy = false;
3225 tree gnu_desig_type = NULL_TREE;
3226 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3228 if (!targetm.valid_pointer_mode (p_mode))
3229 p_mode = ptr_mode;
3231 /* If either the designated type or its full view is an unconstrained
3232 array subtype, replace it with the type it's a subtype of. This
3233 avoids problems with multiple copies of unconstrained array types.
3234 Likewise, if the designated type is a subtype of an incomplete
3235 record type, use the parent type to avoid order of elaboration
3236 issues. This can lose some code efficiency, but there is no
3237 alternative. */
3238 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3239 && ! Is_Constrained (gnat_desig_equiv))
3240 gnat_desig_equiv = Etype (gnat_desig_equiv);
3241 if (Present (gnat_desig_full)
3242 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3243 && ! Is_Constrained (gnat_desig_full))
3244 || (Ekind (gnat_desig_full) == E_Record_Subtype
3245 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3246 gnat_desig_full = Etype (gnat_desig_full);
3248 /* Now set the type that actually marks the representation of
3249 the designated type and also flag whether we have a unconstrained
3250 array. */
3251 gnat_desig_rep = gnat_desig_full ? gnat_desig_full : gnat_desig_equiv;
3252 is_unconstrained_array
3253 = (Is_Array_Type (gnat_desig_rep)
3254 && ! Is_Constrained (gnat_desig_rep));
3256 /* If we are pointing to an incomplete type whose completion is an
3257 unconstrained array, make a fat pointer type. The two types in our
3258 fields will be pointers to dummy nodes and will be replaced in
3259 update_pointer_to. Similarly, if the type itself is a dummy type or
3260 an unconstrained array. Also make a dummy TYPE_OBJECT_RECORD_TYPE
3261 in case we have any thin pointers to it. */
3262 if (is_unconstrained_array
3263 && (Present (gnat_desig_full)
3264 || (present_gnu_tree (gnat_desig_equiv)
3265 && TYPE_IS_DUMMY_P (TREE_TYPE
3266 (get_gnu_tree (gnat_desig_equiv))))
3267 || (No (gnat_desig_full) && ! in_main_unit
3268 && defer_incomplete_level != 0
3269 && ! present_gnu_tree (gnat_desig_equiv))
3270 || (in_main_unit && is_from_limited_with
3271 && Present (Freeze_Node (gnat_desig_rep)))))
3273 tree gnu_old
3274 = (present_gnu_tree (gnat_desig_rep)
3275 ? TREE_TYPE (get_gnu_tree (gnat_desig_rep))
3276 : make_dummy_type (gnat_desig_rep));
3277 tree fields;
3279 /* Show the dummy we get will be a fat pointer. */
3280 got_fat_p = made_dummy = true;
3282 /* If the call above got something that has a pointer, that
3283 pointer is our type. This could have happened either
3284 because the type was elaborated or because somebody
3285 else executed the code below. */
3286 gnu_type = TYPE_POINTER_TO (gnu_old);
3287 if (!gnu_type)
3289 tree gnu_template_type = make_node (ENUMERAL_TYPE);
3290 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
3291 tree gnu_array_type = make_node (ENUMERAL_TYPE);
3292 tree gnu_ptr_array = build_pointer_type (gnu_array_type);
3294 TYPE_NAME (gnu_template_type)
3295 = concat_id_with_name (get_entity_name (gnat_desig_equiv),
3296 "XUB");
3297 TYPE_DUMMY_P (gnu_template_type) = 1;
3299 TYPE_NAME (gnu_array_type)
3300 = concat_id_with_name (get_entity_name (gnat_desig_equiv),
3301 "XUA");
3302 TYPE_DUMMY_P (gnu_array_type) = 1;
3304 gnu_type = make_node (RECORD_TYPE);
3305 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
3306 TYPE_POINTER_TO (gnu_old) = gnu_type;
3308 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3309 fields
3310 = chainon (chainon (NULL_TREE,
3311 create_field_decl
3312 (get_identifier ("P_ARRAY"),
3313 gnu_ptr_array,
3314 gnu_type, 0, 0, 0, 0)),
3315 create_field_decl (get_identifier ("P_BOUNDS"),
3316 gnu_ptr_template,
3317 gnu_type, 0, 0, 0, 0));
3319 /* Make sure we can place this into a register. */
3320 TYPE_ALIGN (gnu_type)
3321 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
3322 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
3324 /* Do not finalize this record type since the types of
3325 its fields are incomplete. */
3326 finish_record_type (gnu_type, fields, 0, true);
3328 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
3329 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
3330 = concat_id_with_name (get_entity_name (gnat_desig_equiv),
3331 "XUT");
3332 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
3336 /* If we already know what the full type is, use it. */
3337 else if (Present (gnat_desig_full)
3338 && present_gnu_tree (gnat_desig_full))
3339 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3341 /* Get the type of the thing we are to point to and build a pointer
3342 to it. If it is a reference to an incomplete or private type with a
3343 full view that is a record, make a dummy type node and get the
3344 actual type later when we have verified it is safe. */
3345 else if ((! in_main_unit
3346 && ! present_gnu_tree (gnat_desig_equiv)
3347 && Present (gnat_desig_full)
3348 && ! present_gnu_tree (gnat_desig_full)
3349 && Is_Record_Type (gnat_desig_full))
3350 /* Likewise if we are pointing to a record or array and we
3351 are to defer elaborating incomplete types. We do this
3352 since this access type may be the full view of some
3353 private type. Note that the unconstrained array case is
3354 handled above. */
3355 || ((! in_main_unit || imported_p)
3356 && defer_incomplete_level != 0
3357 && ! present_gnu_tree (gnat_desig_equiv)
3358 && ((Is_Record_Type (gnat_desig_rep)
3359 || Is_Array_Type (gnat_desig_rep))))
3360 /* If this is a reference from a limited_with type back to our
3361 main unit and there's a Freeze_Node for it, either we have
3362 already processed the declaration and made the dummy type,
3363 in which case we just reuse the latter, or we have not yet,
3364 in which case we make the dummy type and it will be reused
3365 when the declaration is processed. In both cases, the
3366 pointer eventually created below will be automatically
3367 adjusted when the Freeze_Node is processed. Note that the
3368 unconstrained array case is handled above. */
3369 || (in_main_unit && is_from_limited_with
3370 && Present (Freeze_Node (gnat_desig_rep))))
3372 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3373 made_dummy = true;
3376 /* Otherwise handle the case of a pointer to itself. */
3377 else if (gnat_desig_equiv == gnat_entity)
3379 gnu_type
3380 = build_pointer_type_for_mode (void_type_node, p_mode,
3381 No_Strict_Aliasing (gnat_entity));
3382 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3385 /* If expansion is disabled, the equivalent type of a concurrent
3386 type is absent, so build a dummy pointer type. */
3387 else if (type_annotate_only && No (gnat_desig_equiv))
3388 gnu_type = ptr_void_type_node;
3390 /* Finally, handle the straightforward case where we can just
3391 elaborate our designated type and point to it. */
3392 else
3393 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3395 /* It is possible that a call to gnat_to_gnu_type above resolved our
3396 type. If so, just return it. */
3397 if (present_gnu_tree (gnat_entity))
3399 maybe_present = true;
3400 break;
3403 /* If we have a GCC type for the designated type, possibly modify it
3404 if we are pointing only to constant objects and then make a pointer
3405 to it. Don't do this for unconstrained arrays. */
3406 if (!gnu_type && gnu_desig_type)
3408 if (Is_Access_Constant (gnat_entity)
3409 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3411 gnu_desig_type
3412 = build_qualified_type
3413 (gnu_desig_type,
3414 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3416 /* Some extra processing is required if we are building a
3417 pointer to an incomplete type (in the GCC sense). We might
3418 have such a type if we just made a dummy, or directly out
3419 of the call to gnat_to_gnu_type above if we are processing
3420 an access type for a record component designating the
3421 record type itself. */
3422 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3424 /* We must ensure that the pointer to variant we make will
3425 be processed by update_pointer_to when the initial type
3426 is completed. Pretend we made a dummy and let further
3427 processing act as usual. */
3428 made_dummy = true;
3430 /* We must ensure that update_pointer_to will not retrieve
3431 the dummy variant when building a properly qualified
3432 version of the complete type. We take advantage of the
3433 fact that get_qualified_type is requiring TYPE_NAMEs to
3434 match to influence build_qualified_type and then also
3435 update_pointer_to here. */
3436 TYPE_NAME (gnu_desig_type)
3437 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3441 gnu_type
3442 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3443 No_Strict_Aliasing (gnat_entity));
3446 /* If we are not defining this object and we made a dummy pointer,
3447 save our current definition, evaluate the actual type, and replace
3448 the tentative type we made with the actual one. If we are to defer
3449 actually looking up the actual type, make an entry in the
3450 deferred list. If this is from a limited with, we have to defer
3451 to the end of the current spec in two cases: first if the
3452 designated type is in the current unit and second if the access
3453 type is. */
3454 if ((! in_main_unit || is_from_limited_with) && made_dummy)
3456 tree gnu_old_type
3457 = TYPE_FAT_POINTER_P (gnu_type)
3458 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3460 if (esize == POINTER_SIZE
3461 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
3462 gnu_type
3463 = build_pointer_type
3464 (TYPE_OBJECT_RECORD_TYPE
3465 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3467 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3468 !Comes_From_Source (gnat_entity),
3469 debug_info_p, gnat_entity);
3470 this_made_decl = true;
3471 gnu_type = TREE_TYPE (gnu_decl);
3472 save_gnu_tree (gnat_entity, gnu_decl, false);
3473 saved = true;
3475 if (defer_incomplete_level == 0
3476 && ! (is_from_limited_with
3477 && (in_main_unit
3478 || In_Extended_Main_Code_Unit (gnat_entity))))
3479 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
3480 gnat_to_gnu_type (gnat_desig_equiv));
3482 /* Note that the call to gnat_to_gnu_type here might have
3483 updated gnu_old_type directly, in which case it is not a
3484 dummy type any more when we get into update_pointer_to.
3486 This may happen for instance when the designated type is a
3487 record type, because their elaboration starts with an
3488 initial node from make_dummy_type, which may yield the same
3489 node as the one we got.
3491 Besides, variants of this non-dummy type might have been
3492 created along the way. update_pointer_to is expected to
3493 properly take care of those situations. */
3494 else
3496 struct incomplete *p
3497 = (struct incomplete *) xmalloc (sizeof
3498 (struct incomplete));
3499 struct incomplete **head
3500 = (is_from_limited_with
3501 && (in_main_unit
3502 || In_Extended_Main_Code_Unit (gnat_entity))
3503 ? &defer_limited_with : &defer_incomplete_list);
3505 p->old_type = gnu_old_type;
3506 p->full_type = gnat_desig_equiv;
3507 p->next = *head;
3508 *head = p;
3512 break;
3514 case E_Access_Protected_Subprogram_Type:
3515 case E_Anonymous_Access_Protected_Subprogram_Type:
3516 if (type_annotate_only && No (gnat_equiv_type))
3517 gnu_type = ptr_void_type_node;
3518 else
3520 /* The runtime representation is the equivalent type. */
3521 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3522 maybe_present = 1;
3525 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3526 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3527 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3528 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3529 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3530 NULL_TREE, 0);
3532 break;
3534 case E_Access_Subtype:
3536 /* We treat this as identical to its base type; any constraint is
3537 meaningful only to the front end.
3539 The designated type must be elaborated as well, if it does
3540 not have its own freeze node. Designated (sub)types created
3541 for constrained components of records with discriminants are
3542 not frozen by the front end and thus not elaborated by gigi,
3543 because their use may appear before the base type is frozen,
3544 and because it is not clear that they are needed anywhere in
3545 Gigi. With the current model, there is no correct place where
3546 they could be elaborated. */
3548 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3549 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3550 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3551 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3552 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3554 /* If we are not defining this entity, and we have incomplete
3555 entities being processed above us, make a dummy type and
3556 elaborate it later. */
3557 if (!definition && defer_incomplete_level != 0)
3559 struct incomplete *p
3560 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3561 tree gnu_ptr_type
3562 = build_pointer_type
3563 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3565 p->old_type = TREE_TYPE (gnu_ptr_type);
3566 p->full_type = Directly_Designated_Type (gnat_entity);
3567 p->next = defer_incomplete_list;
3568 defer_incomplete_list = p;
3570 else if (!IN (Ekind (Base_Type
3571 (Directly_Designated_Type (gnat_entity))),
3572 Incomplete_Or_Private_Kind))
3573 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3574 NULL_TREE, 0);
3577 maybe_present = true;
3578 break;
3580 /* Subprogram Entities
3582 The following access functions are defined for subprograms (functions
3583 or procedures):
3585 First_Formal The first formal parameter.
3586 Is_Imported Indicates that the subprogram has appeared in
3587 an INTERFACE or IMPORT pragma. For now we
3588 assume that the external language is C.
3589 Is_Exported Likewise but for an EXPORT pragma.
3590 Is_Inlined True if the subprogram is to be inlined.
3592 In addition for function subprograms we have:
3594 Etype Return type of the function.
3596 Each parameter is first checked by calling must_pass_by_ref on its
3597 type to determine if it is passed by reference. For parameters which
3598 are copied in, if they are Ada In Out or Out parameters, their return
3599 value becomes part of a record which becomes the return type of the
3600 function (C function - note that this applies only to Ada procedures
3601 so there is no Ada return type). Additional code to store back the
3602 parameters will be generated on the caller side. This transformation
3603 is done here, not in the front-end.
3605 The intended result of the transformation can be seen from the
3606 equivalent source rewritings that follow:
3608 struct temp {int a,b};
3609 procedure P (A,B: In Out ...) is temp P (int A,B)
3610 begin {
3611 .. ..
3612 end P; return {A,B};
3615 temp t;
3616 P(X,Y); t = P(X,Y);
3617 X = t.a , Y = t.b;
3619 For subprogram types we need to perform mainly the same conversions to
3620 GCC form that are needed for procedures and function declarations. The
3621 only difference is that at the end, we make a type declaration instead
3622 of a function declaration. */
3624 case E_Subprogram_Type:
3625 case E_Function:
3626 case E_Procedure:
3628 /* The first GCC parameter declaration (a PARM_DECL node). The
3629 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3630 actually is the head of this parameter list. */
3631 tree gnu_param_list = NULL_TREE;
3632 /* Likewise for the stub associated with an exported procedure. */
3633 tree gnu_stub_param_list = NULL_TREE;
3634 /* The type returned by a function. If the subprogram is a procedure
3635 this type should be void_type_node. */
3636 tree gnu_return_type = void_type_node;
3637 /* List of fields in return type of procedure with copy-in copy-out
3638 parameters. */
3639 tree gnu_field_list = NULL_TREE;
3640 /* Non-null for subprograms containing parameters passed by copy-in
3641 copy-out (Ada In Out or Out parameters not passed by reference),
3642 in which case it is the list of nodes used to specify the values of
3643 the in out/out parameters that are returned as a record upon
3644 procedure return. The TREE_PURPOSE of an element of this list is
3645 a field of the record and the TREE_VALUE is the PARM_DECL
3646 corresponding to that field. This list will be saved in the
3647 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3648 tree gnu_return_list = NULL_TREE;
3649 /* If an import pragma asks to map this subprogram to a GCC builtin,
3650 this is the builtin DECL node. */
3651 tree gnu_builtin_decl = NULL_TREE;
3652 /* For the stub associated with an exported procedure. */
3653 tree gnu_stub_type = NULL_TREE, gnu_stub_name = NULL_TREE;
3654 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
3655 Entity_Id gnat_param;
3656 bool inline_flag = Is_Inlined (gnat_entity);
3657 bool public_flag = Is_Public (gnat_entity) || imported_p;
3658 bool extern_flag
3659 = (Is_Public (gnat_entity) && !definition) || imported_p;
3660 bool pure_flag = Is_Pure (gnat_entity);
3661 bool volatile_flag = No_Return (gnat_entity);
3662 bool returns_by_ref = false;
3663 bool returns_unconstrained = false;
3664 bool returns_by_target_ptr = false;
3665 bool has_copy_in_out = false;
3666 bool has_stub = false;
3667 int parmnum;
3669 if (kind == E_Subprogram_Type && !definition)
3670 /* A parameter may refer to this type, so defer completion
3671 of any incomplete types. */
3672 defer_incomplete_level++, this_deferred = true;
3674 /* If the subprogram has an alias, it is probably inherited, so
3675 we can use the original one. If the original "subprogram"
3676 is actually an enumeration literal, it may be the first use
3677 of its type, so we must elaborate that type now. */
3678 if (Present (Alias (gnat_entity)))
3680 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3681 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3683 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3684 gnu_expr, 0);
3686 /* Elaborate any Itypes in the parameters of this entity. */
3687 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3688 Present (gnat_temp);
3689 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3690 if (Is_Itype (Etype (gnat_temp)))
3691 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3693 break;
3696 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3697 corresponding DECL node.
3699 We still want the parameter associations to take place because the
3700 proper generation of calls depends on it (a GNAT parameter without
3701 a corresponding GCC tree has a very specific meaning), so we don't
3702 just break here. */
3703 if (Convention (gnat_entity) == Convention_Intrinsic)
3704 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3706 /* ??? What if we don't find the builtin node above ? warn ? err ?
3707 In the current state we neither warn nor err, and calls will just
3708 be handled as for regular subprograms. */
3710 if (kind == E_Function || kind == E_Subprogram_Type)
3711 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3713 /* If this function returns by reference, make the actual
3714 return type of this function the pointer and mark the decl. */
3715 if (Returns_By_Ref (gnat_entity))
3717 returns_by_ref = true;
3718 gnu_return_type = build_pointer_type (gnu_return_type);
3721 /* If the Mechanism is By_Reference, ensure the return type uses
3722 the machine's by-reference mechanism, which may not the same
3723 as above (e.g., it might be by passing a fake parameter). */
3724 else if (kind == E_Function
3725 && Mechanism (gnat_entity) == By_Reference)
3727 TREE_ADDRESSABLE (gnu_return_type) = 1;
3729 /* We expect this bit to be reset by gigi shortly, so can avoid a
3730 type node copy here. This actually also prevents troubles with
3731 the generation of debug information for the function, because
3732 we might have issued such info for this type already, and would
3733 be attaching a distinct type node to the function if we made a
3734 copy here. */
3737 /* If we are supposed to return an unconstrained array,
3738 actually return a fat pointer and make a note of that. Return
3739 a pointer to an unconstrained record of variable size. */
3740 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3742 gnu_return_type = TREE_TYPE (gnu_return_type);
3743 returns_unconstrained = true;
3746 /* If the type requires a transient scope, the result is allocated
3747 on the secondary stack, so the result type of the function is
3748 just a pointer. */
3749 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3751 gnu_return_type = build_pointer_type (gnu_return_type);
3752 returns_unconstrained = true;
3755 /* If the type is a padded type and the underlying type would not
3756 be passed by reference or this function has a foreign convention,
3757 return the underlying type. */
3758 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3759 && TYPE_IS_PADDING_P (gnu_return_type)
3760 && (!default_pass_by_ref (TREE_TYPE
3761 (TYPE_FIELDS (gnu_return_type)))
3762 || Has_Foreign_Convention (gnat_entity)))
3763 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3765 /* If the return type has a non-constant size, we convert the function
3766 into a procedure and its caller will pass a pointer to an object as
3767 the first parameter when we call the function. This can happen for
3768 an unconstrained type with a maximum size or a constrained type with
3769 a size not known at compile time. */
3770 if (TYPE_SIZE_UNIT (gnu_return_type)
3771 && !TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type)))
3773 returns_by_target_ptr = true;
3774 gnu_param_list
3775 = create_param_decl (get_identifier ("TARGET"),
3776 build_reference_type (gnu_return_type),
3777 true);
3778 gnu_return_type = void_type_node;
3781 /* If the return type has a size that overflows, we cannot have
3782 a function that returns that type. This usage doesn't make
3783 sense anyway, so give an error here. */
3784 if (TYPE_SIZE_UNIT (gnu_return_type)
3785 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type))
3786 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
3788 post_error ("cannot return type whose size overflows",
3789 gnat_entity);
3790 gnu_return_type = copy_node (gnu_return_type);
3791 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
3792 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
3793 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
3794 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
3797 /* Look at all our parameters and get the type of
3798 each. While doing this, build a copy-out structure if
3799 we need one. */
3801 /* Loop over the parameters and get their associated GCC tree.
3802 While doing this, build a copy-out structure if we need one. */
3803 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
3804 Present (gnat_param);
3805 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3807 tree gnu_param_name = get_entity_name (gnat_param);
3808 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3809 tree gnu_param, gnu_field;
3810 bool copy_in_copy_out = false;
3811 Mechanism_Type mech = Mechanism (gnat_param);
3813 /* Builtins are expanded inline and there is no real call sequence
3814 involved. So the type expected by the underlying expander is
3815 always the type of each argument "as is". */
3816 if (gnu_builtin_decl)
3817 mech = By_Copy;
3818 /* Handle the first parameter of a valued procedure specially. */
3819 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3820 mech = By_Copy_Return;
3821 /* Otherwise, see if a Mechanism was supplied that forced this
3822 parameter to be passed one way or another. */
3823 else if (mech == Default
3824 || mech == By_Copy || mech == By_Reference)
3826 else if (By_Descriptor_Last <= mech && mech <= By_Descriptor)
3827 mech = By_Descriptor;
3828 else if (mech > 0)
3830 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3831 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3832 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3833 mech))
3834 mech = By_Reference;
3835 else
3836 mech = By_Copy;
3838 else
3840 post_error ("unsupported mechanism for&", gnat_param);
3841 mech = Default;
3844 gnu_param
3845 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
3846 Has_Foreign_Convention (gnat_entity),
3847 &copy_in_copy_out);
3849 /* We are returned either a PARM_DECL or a type if no parameter
3850 needs to be passed; in either case, adjust the type. */
3851 if (DECL_P (gnu_param))
3852 gnu_param_type = TREE_TYPE (gnu_param);
3853 else
3855 gnu_param_type = gnu_param;
3856 gnu_param = NULL_TREE;
3859 if (gnu_param)
3861 /* If it's an exported subprogram, we build a parameter list
3862 in parallel, in case we need to emit a stub for it. */
3863 if (Is_Exported (gnat_entity))
3865 gnu_stub_param_list
3866 = chainon (gnu_param, gnu_stub_param_list);
3867 /* Change By_Descriptor parameter to By_Reference for
3868 the internal version of an exported subprogram. */
3869 if (mech == By_Descriptor)
3871 gnu_param
3872 = gnat_to_gnu_param (gnat_param, By_Reference,
3873 gnat_entity, false,
3874 &copy_in_copy_out);
3875 has_stub = true;
3877 else
3878 gnu_param = copy_node (gnu_param);
3881 gnu_param_list = chainon (gnu_param, gnu_param_list);
3882 Sloc_to_locus (Sloc (gnat_param),
3883 &DECL_SOURCE_LOCATION (gnu_param));
3884 save_gnu_tree (gnat_param, gnu_param, false);
3886 /* If a parameter is a pointer, this function may modify
3887 memory through it and thus shouldn't be considered
3888 a pure function. Also, the memory may be modified
3889 between two calls, so they can't be CSE'ed. The latter
3890 case also handles by-ref parameters. */
3891 if (POINTER_TYPE_P (gnu_param_type)
3892 || TYPE_FAT_POINTER_P (gnu_param_type))
3893 pure_flag = false;
3896 if (copy_in_copy_out)
3898 if (!has_copy_in_out)
3900 gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
3901 gnu_return_type = make_node (RECORD_TYPE);
3902 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3903 has_copy_in_out = true;
3906 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3907 gnu_return_type, 0, 0, 0, 0);
3908 Sloc_to_locus (Sloc (gnat_param),
3909 &DECL_SOURCE_LOCATION (gnu_field));
3910 TREE_CHAIN (gnu_field) = gnu_field_list;
3911 gnu_field_list = gnu_field;
3912 gnu_return_list = tree_cons (gnu_field, gnu_param,
3913 gnu_return_list);
3917 /* Do not compute record for out parameters if subprogram is
3918 stubbed since structures are incomplete for the back-end. */
3919 if (gnu_field_list && Convention (gnat_entity) != Convention_Stubbed)
3920 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3921 0, false);
3923 /* If we have a CICO list but it has only one entry, we convert
3924 this function into a function that simply returns that one
3925 object. */
3926 if (list_length (gnu_return_list) == 1)
3927 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3929 if (Has_Stdcall_Convention (gnat_entity))
3930 prepend_one_attribute_to
3931 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
3932 get_identifier ("stdcall"), NULL_TREE,
3933 gnat_entity);
3935 /* The lists have been built in reverse. */
3936 gnu_param_list = nreverse (gnu_param_list);
3937 if (has_stub)
3938 gnu_stub_param_list = nreverse (gnu_stub_param_list);
3939 gnu_return_list = nreverse (gnu_return_list);
3941 if (Ekind (gnat_entity) == E_Function)
3942 Set_Mechanism (gnat_entity,
3943 (returns_by_ref || returns_unconstrained
3944 ? By_Reference : By_Copy));
3945 gnu_type
3946 = create_subprog_type (gnu_return_type, gnu_param_list,
3947 gnu_return_list, returns_unconstrained,
3948 returns_by_ref, returns_by_target_ptr);
3950 if (has_stub)
3951 gnu_stub_type
3952 = create_subprog_type (gnu_return_type, gnu_stub_param_list,
3953 gnu_return_list, returns_unconstrained,
3954 returns_by_ref, returns_by_target_ptr);
3956 /* A subprogram (something that doesn't return anything) shouldn't
3957 be considered Pure since there would be no reason for such a
3958 subprogram. Note that procedures with Out (or In Out) parameters
3959 have already been converted into a function with a return type. */
3960 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
3961 pure_flag = false;
3963 /* The semantics of "pure" in Ada essentially matches that of "const"
3964 in the back-end. In particular, both properties are orthogonal to
3965 the "nothrow" property. But this is true only if the EH circuitry
3966 is explicit in the internal representation of the back-end. If we
3967 are to completely hide the EH circuitry from it, we need to declare
3968 that calls to pure Ada subprograms that can throw have side effects
3969 since they can trigger an "abnormal" transfer of control flow; thus
3970 they can be neither "const" nor "pure" in the back-end sense. */
3971 gnu_type
3972 = build_qualified_type (gnu_type,
3973 TYPE_QUALS (gnu_type)
3974 | (Exception_Mechanism == Back_End_Exceptions
3975 ? TYPE_QUAL_CONST * pure_flag : 0)
3976 | (TYPE_QUAL_VOLATILE * volatile_flag));
3978 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3980 if (has_stub)
3981 gnu_stub_type
3982 = build_qualified_type (gnu_stub_type,
3983 TYPE_QUALS (gnu_stub_type)
3984 | (Exception_Mechanism == Back_End_Exceptions
3985 ? TYPE_QUAL_CONST * pure_flag : 0)
3986 | (TYPE_QUAL_VOLATILE * volatile_flag));
3988 /* If we have a builtin decl for that function, check the signatures
3989 compatibilities. If the signatures are compatible, use the builtin
3990 decl. If they are not, we expect the checker predicate to have
3991 posted the appropriate errors, and just continue with what we have
3992 so far. */
3993 if (gnu_builtin_decl)
3995 tree gnu_builtin_type = TREE_TYPE (gnu_builtin_decl);
3997 if (compatible_signatures_p (gnu_type, gnu_builtin_type))
3999 gnu_decl = gnu_builtin_decl;
4000 gnu_type = gnu_builtin_type;
4001 break;
4005 /* If there was no specified Interface_Name and the external and
4006 internal names of the subprogram are the same, only use the
4007 internal name to allow disambiguation of nested subprograms. */
4008 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_id)
4009 gnu_ext_name = NULL_TREE;
4011 /* If we are defining the subprogram and it has an Address clause
4012 we must get the address expression from the saved GCC tree for the
4013 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4014 the address expression here since the front-end has guaranteed
4015 in that case that the elaboration has no effects. If there is
4016 an Address clause and we are not defining the object, just
4017 make it a constant. */
4018 if (Present (Address_Clause (gnat_entity)))
4020 tree gnu_address = NULL_TREE;
4022 if (definition)
4023 gnu_address
4024 = (present_gnu_tree (gnat_entity)
4025 ? get_gnu_tree (gnat_entity)
4026 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4028 save_gnu_tree (gnat_entity, NULL_TREE, false);
4030 /* Convert the type of the object to a reference type that can
4031 alias everything as per 13.3(19). */
4032 gnu_type
4033 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4034 if (gnu_address)
4035 gnu_address = convert (gnu_type, gnu_address);
4037 gnu_decl
4038 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
4039 gnu_address, false, Is_Public (gnat_entity),
4040 extern_flag, false, NULL, gnat_entity);
4041 DECL_BY_REF_P (gnu_decl) = 1;
4044 else if (kind == E_Subprogram_Type)
4045 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4046 !Comes_From_Source (gnat_entity),
4047 debug_info_p, gnat_entity);
4048 else
4050 if (has_stub)
4052 gnu_stub_name = gnu_ext_name;
4053 gnu_ext_name = create_concat_name (gnat_entity, "internal");
4054 public_flag = false;
4057 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
4058 gnu_type, gnu_param_list,
4059 inline_flag, public_flag,
4060 extern_flag, attr_list,
4061 gnat_entity);
4062 if (has_stub)
4064 tree gnu_stub_decl
4065 = create_subprog_decl (gnu_entity_id, gnu_stub_name,
4066 gnu_stub_type, gnu_stub_param_list,
4067 inline_flag, true,
4068 extern_flag, attr_list,
4069 gnat_entity);
4070 SET_DECL_FUNCTION_STUB (gnu_decl, gnu_stub_decl);
4073 /* This is unrelated to the stub built right above. */
4074 DECL_STUBBED_P (gnu_decl)
4075 = Convention (gnat_entity) == Convention_Stubbed;
4078 break;
4080 case E_Incomplete_Type:
4081 case E_Incomplete_Subtype:
4082 case E_Private_Type:
4083 case E_Private_Subtype:
4084 case E_Limited_Private_Type:
4085 case E_Limited_Private_Subtype:
4086 case E_Record_Type_With_Private:
4087 case E_Record_Subtype_With_Private:
4089 /* Get the "full view" of this entity. If this is an incomplete
4090 entity from a limited with, treat its non-limited view as the
4091 full view. Otherwise, use either the full view or the underlying
4092 full view, whichever is present. This is used in all the tests
4093 below. */
4094 Entity_Id full_view
4095 = (IN (Ekind (gnat_entity), Incomplete_Kind)
4096 && From_With_Type (gnat_entity))
4097 ? Non_Limited_View (gnat_entity)
4098 : Present (Full_View (gnat_entity))
4099 ? Full_View (gnat_entity)
4100 : Underlying_Full_View (gnat_entity);
4102 /* If this is an incomplete type with no full view, it must be a Taft
4103 Amendment type, in which case we return a dummy type. Otherwise,
4104 just get the type from its Etype. */
4105 if (No (full_view))
4107 if (kind == E_Incomplete_Type)
4108 gnu_type = make_dummy_type (gnat_entity);
4109 else
4111 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4112 NULL_TREE, 0);
4113 maybe_present = true;
4115 break;
4118 /* If we already made a type for the full view, reuse it. */
4119 else if (present_gnu_tree (full_view))
4121 gnu_decl = get_gnu_tree (full_view);
4122 break;
4125 /* Otherwise, if we are not defining the type now, get the type
4126 from the full view. But always get the type from the full view
4127 for define on use types, since otherwise we won't see them! */
4128 else if (!definition
4129 || (Is_Itype (full_view)
4130 && No (Freeze_Node (gnat_entity)))
4131 || (Is_Itype (gnat_entity)
4132 && No (Freeze_Node (full_view))))
4134 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4135 maybe_present = true;
4136 break;
4139 /* For incomplete types, make a dummy type entry which will be
4140 replaced later. */
4141 gnu_type = make_dummy_type (gnat_entity);
4143 /* Save this type as the full declaration's type so we can do any
4144 needed updates when we see it. */
4145 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4146 !Comes_From_Source (gnat_entity),
4147 debug_info_p, gnat_entity);
4148 save_gnu_tree (full_view, gnu_decl, 0);
4149 break;
4152 /* Simple class_wide types are always viewed as their root_type
4153 by Gigi unless an Equivalent_Type is specified. */
4154 case E_Class_Wide_Type:
4155 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4156 maybe_present = true;
4157 break;
4159 case E_Task_Type:
4160 case E_Task_Subtype:
4161 case E_Protected_Type:
4162 case E_Protected_Subtype:
4163 if (type_annotate_only && No (gnat_equiv_type))
4164 gnu_type = void_type_node;
4165 else
4166 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4168 maybe_present = true;
4169 break;
4171 case E_Label:
4172 gnu_decl = create_label_decl (gnu_entity_id);
4173 break;
4175 case E_Block:
4176 case E_Loop:
4177 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4178 we've already saved it, so we don't try to. */
4179 gnu_decl = error_mark_node;
4180 saved = true;
4181 break;
4183 default:
4184 gcc_unreachable ();
4187 /* If we had a case where we evaluated another type and it might have
4188 defined this one, handle it here. */
4189 if (maybe_present && present_gnu_tree (gnat_entity))
4191 gnu_decl = get_gnu_tree (gnat_entity);
4192 saved = true;
4195 /* If we are processing a type and there is either no decl for it or
4196 we just made one, do some common processing for the type, such as
4197 handling alignment and possible padding. */
4199 if ((!gnu_decl || this_made_decl) && IN (kind, Type_Kind))
4201 if (Is_Tagged_Type (gnat_entity)
4202 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4203 TYPE_ALIGN_OK (gnu_type) = 1;
4205 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4206 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4208 /* ??? Don't set the size for a String_Literal since it is either
4209 confirming or we don't handle it properly (if the low bound is
4210 non-constant). */
4211 if (!gnu_size && kind != E_String_Literal_Subtype)
4212 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
4213 TYPE_DECL, false,
4214 Has_Size_Clause (gnat_entity));
4216 /* If a size was specified, see if we can make a new type of that size
4217 by rearranging the type, for example from a fat to a thin pointer. */
4218 if (gnu_size)
4220 gnu_type
4221 = make_type_from_size (gnu_type, gnu_size,
4222 Has_Biased_Representation (gnat_entity));
4224 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4225 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4226 gnu_size = 0;
4229 /* If the alignment hasn't already been processed and this is
4230 not an unconstrained array, see if an alignment is specified.
4231 If not, we pick a default alignment for atomic objects. */
4232 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4234 else if (Known_Alignment (gnat_entity))
4236 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4237 TYPE_ALIGN (gnu_type));
4239 /* Warn on suspiciously large alignments. This should catch
4240 errors about the (alignment,byte)/(size,bit) discrepancy. */
4241 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4243 tree size;
4245 /* If a size was specified, take it into account. Otherwise
4246 use the RM size for records as the type size has already
4247 been adjusted to the alignment. */
4248 if (gnu_size)
4249 size = gnu_size;
4250 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
4251 || TREE_CODE (gnu_type) == UNION_TYPE
4252 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
4253 && !TYPE_IS_FAT_POINTER_P (gnu_type))
4254 size = rm_size (gnu_type);
4255 else
4256 size = TYPE_SIZE (gnu_type);
4258 /* Consider an alignment as suspicious if the alignment/size
4259 ratio is greater or equal to the byte/bit ratio. */
4260 if (host_integerp (size, 1)
4261 && align >= TREE_INT_CST_LOW (size) * BITS_PER_UNIT)
4262 post_error_ne ("?suspiciously large alignment specified for&",
4263 Expression (Alignment_Clause (gnat_entity)),
4264 gnat_entity);
4267 else if (Is_Atomic (gnat_entity) && !gnu_size
4268 && host_integerp (TYPE_SIZE (gnu_type), 1)
4269 && integer_pow2p (TYPE_SIZE (gnu_type)))
4270 align = MIN (BIGGEST_ALIGNMENT,
4271 tree_low_cst (TYPE_SIZE (gnu_type), 1));
4272 else if (Is_Atomic (gnat_entity) && gnu_size
4273 && host_integerp (gnu_size, 1)
4274 && integer_pow2p (gnu_size))
4275 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
4277 /* See if we need to pad the type. If we did, and made a record,
4278 the name of the new type may be changed. So get it back for
4279 us when we make the new TYPE_DECL below. */
4280 if (gnu_size || align > 0)
4281 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4282 "PAD", true, definition, false);
4284 if (TREE_CODE (gnu_type) == RECORD_TYPE
4285 && TYPE_IS_PADDING_P (gnu_type))
4287 gnu_entity_id = TYPE_NAME (gnu_type);
4288 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
4289 gnu_entity_id = DECL_NAME (gnu_entity_id);
4292 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4294 /* If we are at global level, GCC will have applied variable_size to
4295 the type, but that won't have done anything. So, if it's not
4296 a constant or self-referential, call elaborate_expression_1 to
4297 make a variable for the size rather than calculating it each time.
4298 Handle both the RM size and the actual size. */
4299 if (global_bindings_p ()
4300 && TYPE_SIZE (gnu_type)
4301 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4302 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4304 if (TREE_CODE (gnu_type) == RECORD_TYPE
4305 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
4306 TYPE_SIZE (gnu_type), 0))
4308 TYPE_SIZE (gnu_type)
4309 = elaborate_expression_1 (gnat_entity, gnat_entity,
4310 TYPE_SIZE (gnu_type),
4311 get_identifier ("SIZE"),
4312 definition, 0);
4313 SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
4315 else
4317 TYPE_SIZE (gnu_type)
4318 = elaborate_expression_1 (gnat_entity, gnat_entity,
4319 TYPE_SIZE (gnu_type),
4320 get_identifier ("SIZE"),
4321 definition, 0);
4323 /* ??? For now, store the size as a multiple of the alignment
4324 in bytes so that we can see the alignment from the tree. */
4325 TYPE_SIZE_UNIT (gnu_type)
4326 = build_binary_op
4327 (MULT_EXPR, sizetype,
4328 elaborate_expression_1
4329 (gnat_entity, gnat_entity,
4330 build_binary_op (EXACT_DIV_EXPR, sizetype,
4331 TYPE_SIZE_UNIT (gnu_type),
4332 size_int (TYPE_ALIGN (gnu_type)
4333 / BITS_PER_UNIT)),
4334 get_identifier ("SIZE_A_UNIT"),
4335 definition, 0),
4336 size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4338 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4339 SET_TYPE_ADA_SIZE
4340 (gnu_type,
4341 elaborate_expression_1 (gnat_entity,
4342 gnat_entity,
4343 TYPE_ADA_SIZE (gnu_type),
4344 get_identifier ("RM_SIZE"),
4345 definition, 0));
4349 /* If this is a record type or subtype, call elaborate_expression_1 on
4350 any field position. Do this for both global and local types.
4351 Skip any fields that we haven't made trees for to avoid problems with
4352 class wide types. */
4353 if (IN (kind, Record_Kind))
4354 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4355 gnat_temp = Next_Entity (gnat_temp))
4356 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4358 tree gnu_field = get_gnu_tree (gnat_temp);
4360 /* ??? Unfortunately, GCC needs to be able to prove the
4361 alignment of this offset and if it's a variable, it can't.
4362 In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
4363 right now, we have to put in an explicit multiply and
4364 divide by that value. */
4365 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4367 DECL_FIELD_OFFSET (gnu_field)
4368 = build_binary_op
4369 (MULT_EXPR, sizetype,
4370 elaborate_expression_1
4371 (gnat_temp, gnat_temp,
4372 build_binary_op (EXACT_DIV_EXPR, sizetype,
4373 DECL_FIELD_OFFSET (gnu_field),
4374 size_int (DECL_OFFSET_ALIGN (gnu_field)
4375 / BITS_PER_UNIT)),
4376 get_identifier ("OFFSET"),
4377 definition, 0),
4378 size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
4380 /* ??? The context of gnu_field is not necessarily gnu_type so
4381 the MULT_EXPR node built above may not be marked by the call
4382 to create_type_decl below. */
4383 if (global_bindings_p ())
4384 mark_visited (&DECL_FIELD_OFFSET (gnu_field));
4388 gnu_type = build_qualified_type (gnu_type,
4389 (TYPE_QUALS (gnu_type)
4390 | (TYPE_QUAL_VOLATILE
4391 * Treat_As_Volatile (gnat_entity))));
4393 if (Is_Atomic (gnat_entity))
4394 check_ok_for_atomic (gnu_type, gnat_entity, false);
4396 if (Present (Alignment_Clause (gnat_entity)))
4397 TYPE_USER_ALIGN (gnu_type) = 1;
4399 if (Universal_Aliasing (gnat_entity))
4400 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
4402 if (!gnu_decl)
4403 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4404 !Comes_From_Source (gnat_entity),
4405 debug_info_p, gnat_entity);
4406 else
4407 TREE_TYPE (gnu_decl) = gnu_type;
4410 if (IN (kind, Type_Kind) && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4412 gnu_type = TREE_TYPE (gnu_decl);
4414 /* Back-annotate the Alignment of the type if not already in the
4415 tree. Likewise for sizes. */
4416 if (Unknown_Alignment (gnat_entity))
4417 Set_Alignment (gnat_entity,
4418 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4420 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4422 /* If the size is self-referential, we annotate the maximum
4423 value of that size. */
4424 tree gnu_size = TYPE_SIZE (gnu_type);
4426 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4427 gnu_size = max_size (gnu_size, true);
4429 Set_Esize (gnat_entity, annotate_value (gnu_size));
4431 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4433 /* In this mode the tag and the parent components are not
4434 generated by the front-end, so the sizes must be adjusted
4435 explicitly now. */
4436 int size_offset, new_size;
4438 if (Is_Derived_Type (gnat_entity))
4440 size_offset
4441 = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
4442 Set_Alignment (gnat_entity,
4443 Alignment (Etype (Base_Type (gnat_entity))));
4445 else
4446 size_offset = POINTER_SIZE;
4448 new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
4449 Set_Esize (gnat_entity,
4450 UI_From_Int (((new_size + (POINTER_SIZE - 1))
4451 / POINTER_SIZE) * POINTER_SIZE));
4452 Set_RM_Size (gnat_entity, Esize (gnat_entity));
4456 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4457 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4460 if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4461 DECL_ARTIFICIAL (gnu_decl) = 1;
4463 if (!debug_info_p && DECL_P (gnu_decl)
4464 && TREE_CODE (gnu_decl) != FUNCTION_DECL
4465 && No (Renamed_Object (gnat_entity)))
4466 DECL_IGNORED_P (gnu_decl) = 1;
4468 /* If we haven't already, associate the ..._DECL node that we just made with
4469 the input GNAT entity node. */
4470 if (!saved)
4471 save_gnu_tree (gnat_entity, gnu_decl, false);
4473 /* If this is an enumeral or floating-point type, we were not able to set
4474 the bounds since they refer to the type. These bounds are always static.
4476 For enumeration types, also write debugging information and declare the
4477 enumeration literal table, if needed. */
4479 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4480 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4482 tree gnu_scalar_type = gnu_type;
4484 /* If this is a padded type, we need to use the underlying type. */
4485 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
4486 && TYPE_IS_PADDING_P (gnu_scalar_type))
4487 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4489 /* If this is a floating point type and we haven't set a floating
4490 point type yet, use this in the evaluation of the bounds. */
4491 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4492 longest_float_type_node = gnu_type;
4494 TYPE_MIN_VALUE (gnu_scalar_type)
4495 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4496 TYPE_MAX_VALUE (gnu_scalar_type)
4497 = gnat_to_gnu (Type_High_Bound (gnat_entity));
4499 if (TREE_CODE (gnu_scalar_type) == ENUMERAL_TYPE)
4501 /* Since this has both a typedef and a tag, avoid outputting
4502 the name twice. */
4503 DECL_ARTIFICIAL (gnu_decl) = 1;
4504 rest_of_type_decl_compilation (gnu_decl);
4508 /* If we deferred processing of incomplete types, re-enable it. If there
4509 were no other disables and we have some to process, do so. */
4510 if (this_deferred && --defer_incomplete_level == 0)
4512 if (defer_incomplete_list)
4514 struct incomplete *incp, *next;
4516 /* We are back to level 0 for the deferring of incomplete types.
4517 But processing these incomplete types below may itself require
4518 deferring, so preserve what we have and restart from scratch. */
4519 incp = defer_incomplete_list;
4520 defer_incomplete_list = NULL;
4522 /* For finalization, however, all types must be complete so we
4523 cannot do the same because deferred incomplete types may end up
4524 referencing each other. Process them all recursively first. */
4525 defer_finalize_level++;
4527 for (; incp; incp = next)
4529 next = incp->next;
4531 if (incp->old_type)
4532 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4533 gnat_to_gnu_type (incp->full_type));
4534 free (incp);
4537 defer_finalize_level--;
4540 /* All the deferred incomplete types have been processed so we can
4541 now proceed with the finalization of the deferred types. */
4542 if (defer_finalize_level == 0 && defer_finalize_list)
4544 unsigned int i;
4545 tree t;
4547 for (i = 0; VEC_iterate (tree, defer_finalize_list, i, t); i++)
4548 rest_of_type_decl_compilation_no_defer (t);
4550 VEC_free (tree, heap, defer_finalize_list);
4554 /* If we are not defining this type, see if it's in the incomplete list.
4555 If so, handle that list entry now. */
4556 else if (!definition)
4558 struct incomplete *incp;
4560 for (incp = defer_incomplete_list; incp; incp = incp->next)
4561 if (incp->old_type && incp->full_type == gnat_entity)
4563 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4564 TREE_TYPE (gnu_decl));
4565 incp->old_type = NULL_TREE;
4569 if (this_global)
4570 force_global--;
4572 if (Is_Packed_Array_Type (gnat_entity)
4573 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
4574 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
4575 && !present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
4576 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
4578 return gnu_decl;
4581 /* Similar, but if the returned value is a COMPONENT_REF, return the
4582 FIELD_DECL. */
4584 tree
4585 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4587 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4589 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4590 gnu_field = TREE_OPERAND (gnu_field, 1);
4592 return gnu_field;
4595 /* Wrap up compilation of DECL, a TYPE_DECL, possibly deferring it.
4596 Every TYPE_DECL generated for a type definition must be passed
4597 to this function once everything else has been done for it. */
4599 void
4600 rest_of_type_decl_compilation (tree decl)
4602 /* We need to defer finalizing the type if incomplete types
4603 are being deferred or if they are being processed. */
4604 if (defer_incomplete_level || defer_finalize_level)
4605 VEC_safe_push (tree, heap, defer_finalize_list, decl);
4606 else
4607 rest_of_type_decl_compilation_no_defer (decl);
4610 /* Same as above but without deferring the compilation. This
4611 function should not be invoked directly on a TYPE_DECL. */
4613 static void
4614 rest_of_type_decl_compilation_no_defer (tree decl)
4616 const int toplev = global_bindings_p ();
4617 tree t = TREE_TYPE (decl);
4619 rest_of_decl_compilation (decl, toplev, 0);
4621 /* Now process all the variants. This is needed for STABS. */
4622 for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
4624 if (t == TREE_TYPE (decl))
4625 continue;
4627 if (!TYPE_STUB_DECL (t))
4629 TYPE_STUB_DECL (t) = build_decl (TYPE_DECL, DECL_NAME (decl), t);
4630 DECL_ARTIFICIAL (TYPE_STUB_DECL (t)) = 1;
4633 rest_of_type_compilation (t, toplev);
4637 /* Finalize any From_With_Type incomplete types. We do this after processing
4638 our compilation unit and after processing its spec, if this is a body. */
4640 void
4641 finalize_from_with_types (void)
4643 struct incomplete *incp = defer_limited_with;
4644 struct incomplete *next;
4646 defer_limited_with = 0;
4647 for (; incp; incp = next)
4649 next = incp->next;
4651 if (incp->old_type != 0)
4652 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4653 gnat_to_gnu_type (incp->full_type));
4654 free (incp);
4658 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
4659 kind of type (such E_Task_Type) that has a different type which Gigi
4660 uses for its representation. If the type does not have a special type
4661 for its representation, return GNAT_ENTITY. If a type is supposed to
4662 exist, but does not, abort unless annotating types, in which case
4663 return Empty. If GNAT_ENTITY is Empty, return Empty. */
4665 Entity_Id
4666 Gigi_Equivalent_Type (Entity_Id gnat_entity)
4668 Entity_Id gnat_equiv = gnat_entity;
4670 if (No (gnat_entity))
4671 return gnat_entity;
4673 switch (Ekind (gnat_entity))
4675 case E_Class_Wide_Subtype:
4676 if (Present (Equivalent_Type (gnat_entity)))
4677 gnat_equiv = Equivalent_Type (gnat_entity);
4678 break;
4680 case E_Access_Protected_Subprogram_Type:
4681 case E_Anonymous_Access_Protected_Subprogram_Type:
4682 gnat_equiv = Equivalent_Type (gnat_entity);
4683 break;
4685 case E_Class_Wide_Type:
4686 gnat_equiv = ((Present (Equivalent_Type (gnat_entity)))
4687 ? Equivalent_Type (gnat_entity)
4688 : Root_Type (gnat_entity));
4689 break;
4691 case E_Task_Type:
4692 case E_Task_Subtype:
4693 case E_Protected_Type:
4694 case E_Protected_Subtype:
4695 gnat_equiv = Corresponding_Record_Type (gnat_entity);
4696 break;
4698 default:
4699 break;
4702 gcc_assert (Present (gnat_equiv) || type_annotate_only);
4703 return gnat_equiv;
4706 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
4707 using MECH as its passing mechanism, to be placed in the parameter
4708 list built for GNAT_SUBPROG. Assume a foreign convention for the
4709 latter if FOREIGN is true. Also set CICO to true if the parameter
4710 must use the copy-in copy-out implementation mechanism.
4712 The returned tree is a PARM_DECL, except for those cases where no
4713 parameter needs to be actually passed to the subprogram; the type
4714 of this "shadow" parameter is then returned instead. */
4716 static tree
4717 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
4718 Entity_Id gnat_subprog, bool foreign, bool *cico)
4720 tree gnu_param_name = get_entity_name (gnat_param);
4721 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
4722 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
4723 /* The parameter can be indirectly modified if its address is taken. */
4724 bool ro_param = in_param && !Address_Taken (gnat_param);
4725 bool by_return = false, by_component_ptr = false, by_ref = false;
4726 tree gnu_param;
4728 /* Copy-return is used only for the first parameter of a valued procedure.
4729 It's a copy mechanism for which a parameter is never allocated. */
4730 if (mech == By_Copy_Return)
4732 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
4733 mech = By_Copy;
4734 by_return = true;
4737 /* If this is either a foreign function or if the underlying type won't
4738 be passed by reference, strip off possible padding type. */
4739 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
4740 && TYPE_IS_PADDING_P (gnu_param_type))
4742 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
4744 if (mech == By_Reference
4745 || foreign
4746 || (!must_pass_by_ref (unpadded_type)
4747 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
4748 gnu_param_type = unpadded_type;
4751 /* If this is a read-only parameter, make a variant of the type that is
4752 read-only. ??? However, if this is an unconstrained array, that type
4753 can be very complex, so skip it for now. Likewise for any other
4754 self-referential type. */
4755 if (ro_param
4756 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
4757 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
4758 gnu_param_type = build_qualified_type (gnu_param_type,
4759 (TYPE_QUALS (gnu_param_type)
4760 | TYPE_QUAL_CONST));
4762 /* For foreign conventions, pass arrays as pointers to the element type.
4763 First check for unconstrained array and get the underlying array. */
4764 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
4765 gnu_param_type
4766 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
4768 /* VMS descriptors are themselves passed by reference. */
4769 if (mech == By_Descriptor)
4770 gnu_param_type
4771 = build_pointer_type (build_vms_descriptor (gnu_param_type,
4772 Mechanism (gnat_param),
4773 gnat_subprog));
4775 /* Arrays are passed as pointers to element type for foreign conventions. */
4776 else if (foreign
4777 && mech != By_Copy
4778 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
4780 /* Strip off any multi-dimensional entries, then strip
4781 off the last array to get the component type. */
4782 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
4783 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
4784 gnu_param_type = TREE_TYPE (gnu_param_type);
4786 by_component_ptr = true;
4787 gnu_param_type = TREE_TYPE (gnu_param_type);
4789 if (ro_param)
4790 gnu_param_type = build_qualified_type (gnu_param_type,
4791 (TYPE_QUALS (gnu_param_type)
4792 | TYPE_QUAL_CONST));
4794 gnu_param_type = build_pointer_type (gnu_param_type);
4797 /* Fat pointers are passed as thin pointers for foreign conventions. */
4798 else if (foreign && TYPE_FAT_POINTER_P (gnu_param_type))
4799 gnu_param_type
4800 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
4802 /* If we must pass or were requested to pass by reference, do so.
4803 If we were requested to pass by copy, do so.
4804 Otherwise, for foreign conventions, pass In Out or Out parameters
4805 or aggregates by reference. For COBOL and Fortran, pass all
4806 integer and FP types that way too. For Convention Ada, use
4807 the standard Ada default. */
4808 else if (must_pass_by_ref (gnu_param_type)
4809 || mech == By_Reference
4810 || (mech != By_Copy
4811 && ((foreign
4812 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
4813 || (foreign
4814 && (Convention (gnat_subprog) == Convention_Fortran
4815 || Convention (gnat_subprog) == Convention_COBOL)
4816 && (INTEGRAL_TYPE_P (gnu_param_type)
4817 || FLOAT_TYPE_P (gnu_param_type)))
4818 || (!foreign
4819 && default_pass_by_ref (gnu_param_type)))))
4821 gnu_param_type = build_reference_type (gnu_param_type);
4822 by_ref = true;
4825 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
4826 else if (!in_param)
4827 *cico = true;
4829 if (mech == By_Copy && (by_ref || by_component_ptr))
4830 post_error ("?cannot pass & by copy", gnat_param);
4832 /* If this is an Out parameter that isn't passed by reference and isn't
4833 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
4834 it will be a VAR_DECL created when we process the procedure, so just
4835 return its type. For the special parameter of a valued procedure,
4836 never pass it in.
4838 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
4839 Out parameters with discriminants or implicit initial values to be
4840 handled like In Out parameters. These type are normally built as
4841 aggregates, hence passed by reference, except for some packed arrays
4842 which end up encoded in special integer types.
4844 The exception we need to make is then for packed arrays of records
4845 with discriminants or implicit initial values. We have no light/easy
4846 way to check for the latter case, so we merely check for packed arrays
4847 of records. This may lead to useless copy-in operations, but in very
4848 rare cases only, as these would be exceptions in a set of already
4849 exceptional situations. */
4850 if (Ekind (gnat_param) == E_Out_Parameter
4851 && !by_ref
4852 && (by_return
4853 || (mech != By_Descriptor
4854 && !POINTER_TYPE_P (gnu_param_type)
4855 && !AGGREGATE_TYPE_P (gnu_param_type)))
4856 && !(Is_Array_Type (Etype (gnat_param))
4857 && Is_Packed (Etype (gnat_param))
4858 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
4859 return gnu_param_type;
4861 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
4862 ro_param || by_ref || by_component_ptr);
4863 DECL_BY_REF_P (gnu_param) = by_ref;
4864 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
4865 DECL_BY_DESCRIPTOR_P (gnu_param) = (mech == By_Descriptor);
4866 DECL_POINTS_TO_READONLY_P (gnu_param)
4867 = (ro_param && (by_ref || by_component_ptr));
4869 /* If no Mechanism was specified, indicate what we're using, then
4870 back-annotate it. */
4871 if (mech == Default)
4872 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
4874 Set_Mechanism (gnat_param, mech);
4875 return gnu_param;
4878 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
4880 static bool
4881 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
4883 while (Present (Corresponding_Discriminant (discr1)))
4884 discr1 = Corresponding_Discriminant (discr1);
4886 while (Present (Corresponding_Discriminant (discr2)))
4887 discr2 = Corresponding_Discriminant (discr2);
4889 return
4890 Original_Record_Component (discr1) == Original_Record_Component (discr2);
4893 /* Return true if the array type specified by GNAT_TYPE and GNU_TYPE has
4894 a non-aliased component in the back-end sense. */
4896 static bool
4897 array_type_has_nonaliased_component (Entity_Id gnat_type, tree gnu_type)
4899 /* If the type below this is a multi-array type, then
4900 this does not have aliased components. */
4901 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
4902 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
4903 return true;
4905 if (Has_Aliased_Components (gnat_type))
4906 return false;
4908 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
4911 /* Given GNAT_ENTITY, elaborate all expressions that are required to
4912 be elaborated at the point of its definition, but do nothing else. */
4914 void
4915 elaborate_entity (Entity_Id gnat_entity)
4917 switch (Ekind (gnat_entity))
4919 case E_Signed_Integer_Subtype:
4920 case E_Modular_Integer_Subtype:
4921 case E_Enumeration_Subtype:
4922 case E_Ordinary_Fixed_Point_Subtype:
4923 case E_Decimal_Fixed_Point_Subtype:
4924 case E_Floating_Point_Subtype:
4926 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
4927 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
4929 /* ??? Tests for avoiding static constraint error expression
4930 is needed until the front stops generating bogus conversions
4931 on bounds of real types. */
4933 if (!Raises_Constraint_Error (gnat_lb))
4934 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
4935 1, 0, Needs_Debug_Info (gnat_entity));
4936 if (!Raises_Constraint_Error (gnat_hb))
4937 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
4938 1, 0, Needs_Debug_Info (gnat_entity));
4939 break;
4942 case E_Record_Type:
4944 Node_Id full_definition = Declaration_Node (gnat_entity);
4945 Node_Id record_definition = Type_Definition (full_definition);
4947 /* If this is a record extension, go a level further to find the
4948 record definition. */
4949 if (Nkind (record_definition) == N_Derived_Type_Definition)
4950 record_definition = Record_Extension_Part (record_definition);
4952 break;
4954 case E_Record_Subtype:
4955 case E_Private_Subtype:
4956 case E_Limited_Private_Subtype:
4957 case E_Record_Subtype_With_Private:
4958 if (Is_Constrained (gnat_entity)
4959 && Has_Discriminants (Base_Type (gnat_entity))
4960 && Present (Discriminant_Constraint (gnat_entity)))
4962 Node_Id gnat_discriminant_expr;
4963 Entity_Id gnat_field;
4965 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
4966 gnat_discriminant_expr
4967 = First_Elmt (Discriminant_Constraint (gnat_entity));
4968 Present (gnat_field);
4969 gnat_field = Next_Discriminant (gnat_field),
4970 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
4971 /* ??? For now, ignore access discriminants. */
4972 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
4973 elaborate_expression (Node (gnat_discriminant_expr),
4974 gnat_entity,
4975 get_entity_name (gnat_field), 1, 0, 0);
4977 break;
4982 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
4983 any entities on its entity chain similarly. */
4985 void
4986 mark_out_of_scope (Entity_Id gnat_entity)
4988 Entity_Id gnat_sub_entity;
4989 unsigned int kind = Ekind (gnat_entity);
4991 /* If this has an entity list, process all in the list. */
4992 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
4993 || IN (kind, Private_Kind)
4994 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
4995 || kind == E_Function || kind == E_Generic_Function
4996 || kind == E_Generic_Package || kind == E_Generic_Procedure
4997 || kind == E_Loop || kind == E_Operator || kind == E_Package
4998 || kind == E_Package_Body || kind == E_Procedure
4999 || kind == E_Record_Type || kind == E_Record_Subtype
5000 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
5001 for (gnat_sub_entity = First_Entity (gnat_entity);
5002 Present (gnat_sub_entity);
5003 gnat_sub_entity = Next_Entity (gnat_sub_entity))
5004 if (Scope (gnat_sub_entity) == gnat_entity
5005 && gnat_sub_entity != gnat_entity)
5006 mark_out_of_scope (gnat_sub_entity);
5008 /* Now clear this if it has been defined, but only do so if it isn't
5009 a subprogram or parameter. We could refine this, but it isn't
5010 worth it. If this is statically allocated, it is supposed to
5011 hang around out of cope. */
5012 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
5013 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
5015 save_gnu_tree (gnat_entity, NULL_TREE, true);
5016 save_gnu_tree (gnat_entity, error_mark_node, true);
5020 /* Set the alias set of GNU_NEW_TYPE to be that of GNU_OLD_TYPE. If this
5021 is a multi-dimensional array type, do this recursively. */
5023 static void
5024 copy_alias_set (tree gnu_new_type, tree gnu_old_type)
5026 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
5027 of a one-dimensional array, since the padding has the same alias set
5028 as the field type, but if it's a multi-dimensional array, we need to
5029 see the inner types. */
5030 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
5031 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
5032 || TYPE_IS_PADDING_P (gnu_old_type)))
5033 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
5035 /* We need to be careful here in case GNU_OLD_TYPE is an unconstrained
5036 array. In that case, it doesn't have the same shape as GNU_NEW_TYPE,
5037 so we need to go down to what does. */
5038 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
5039 gnu_old_type
5040 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
5042 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
5043 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
5044 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
5045 copy_alias_set (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type));
5047 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
5048 record_component_aliases (gnu_new_type);
5051 /* Return a TREE_LIST describing the substitutions needed to reflect
5052 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
5053 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
5054 of GNAT_SUBTYPE. The substitutions can be in any order. TREE_PURPOSE
5055 gives the tree for the discriminant and TREE_VALUES is the replacement
5056 value. They are in the form of operands to substitute_in_expr.
5057 DEFINITION is as in gnat_to_gnu_entity. */
5059 static tree
5060 substitution_list (Entity_Id gnat_subtype, Entity_Id gnat_type,
5061 tree gnu_list, bool definition)
5063 Entity_Id gnat_discrim;
5064 Node_Id gnat_value;
5066 if (No (gnat_type))
5067 gnat_type = Implementation_Base_Type (gnat_subtype);
5069 if (Has_Discriminants (gnat_type))
5070 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
5071 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
5072 Present (gnat_discrim);
5073 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
5074 gnat_value = Next_Elmt (gnat_value))
5075 /* Ignore access discriminants. */
5076 if (!Is_Access_Type (Etype (Node (gnat_value))))
5077 gnu_list = tree_cons (gnat_to_gnu_field_decl (gnat_discrim),
5078 elaborate_expression
5079 (Node (gnat_value), gnat_subtype,
5080 get_entity_name (gnat_discrim), definition,
5081 1, 0),
5082 gnu_list);
5084 return gnu_list;
5087 /* Return true if the size represented by GNU_SIZE can be handled by an
5088 allocation. If STATIC_P is true, consider only what can be done with a
5089 static allocation. */
5091 static bool
5092 allocatable_size_p (tree gnu_size, bool static_p)
5094 HOST_WIDE_INT our_size;
5096 /* If this is not a static allocation, the only case we want to forbid
5097 is an overflowing size. That will be converted into a raise a
5098 Storage_Error. */
5099 if (!static_p)
5100 return !(TREE_CODE (gnu_size) == INTEGER_CST
5101 && TREE_OVERFLOW (gnu_size));
5103 /* Otherwise, we need to deal with both variable sizes and constant
5104 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
5105 since assemblers may not like very large sizes. */
5106 if (!host_integerp (gnu_size, 1))
5107 return false;
5109 our_size = tree_low_cst (gnu_size, 1);
5110 return (int) our_size == our_size;
5113 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5114 NAME, ARGS and ERROR_POINT. */
5116 static void
5117 prepend_one_attribute_to (struct attrib ** attr_list,
5118 enum attr_type attr_type,
5119 tree attr_name,
5120 tree attr_args,
5121 Node_Id attr_error_point)
5123 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
5125 attr->type = attr_type;
5126 attr->name = attr_name;
5127 attr->args = attr_args;
5128 attr->error_point = attr_error_point;
5130 attr->next = *attr_list;
5131 *attr_list = attr;
5134 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
5136 static void
5137 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
5139 Node_Id gnat_temp;
5141 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
5142 gnat_temp = Next_Rep_Item (gnat_temp))
5143 if (Nkind (gnat_temp) == N_Pragma)
5145 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
5146 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
5147 enum attr_type etype;
5149 if (Present (gnat_assoc) && Present (First (gnat_assoc))
5150 && Present (Next (First (gnat_assoc)))
5151 && (Nkind (Expression (Next (First (gnat_assoc))))
5152 == N_String_Literal))
5154 gnu_arg0 = get_identifier (TREE_STRING_POINTER
5155 (gnat_to_gnu
5156 (Expression (Next
5157 (First (gnat_assoc))))));
5158 if (Present (Next (Next (First (gnat_assoc))))
5159 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
5160 == N_String_Literal))
5161 gnu_arg1 = get_identifier (TREE_STRING_POINTER
5162 (gnat_to_gnu
5163 (Expression
5164 (Next (Next
5165 (First (gnat_assoc)))))));
5168 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_temp))))
5170 case Pragma_Machine_Attribute:
5171 etype = ATTR_MACHINE_ATTRIBUTE;
5172 break;
5174 case Pragma_Linker_Alias:
5175 etype = ATTR_LINK_ALIAS;
5176 break;
5178 case Pragma_Linker_Section:
5179 etype = ATTR_LINK_SECTION;
5180 break;
5182 case Pragma_Linker_Constructor:
5183 etype = ATTR_LINK_CONSTRUCTOR;
5184 break;
5186 case Pragma_Linker_Destructor:
5187 etype = ATTR_LINK_DESTRUCTOR;
5188 break;
5190 case Pragma_Weak_External:
5191 etype = ATTR_WEAK_EXTERNAL;
5192 break;
5194 default:
5195 continue;
5199 /* Prepend to the list now. Make a list of the argument we might
5200 have, as GCC expects it. */
5201 prepend_one_attribute_to
5202 (attr_list,
5203 etype, gnu_arg0,
5204 (gnu_arg1 != NULL_TREE)
5205 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
5206 Present (Next (First (gnat_assoc)))
5207 ? Expression (Next (First (gnat_assoc))) : gnat_temp);
5211 /* Get the unpadded version of a GNAT type. */
5213 tree
5214 get_unpadded_type (Entity_Id gnat_entity)
5216 tree type = gnat_to_gnu_type (gnat_entity);
5218 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5219 type = TREE_TYPE (TYPE_FIELDS (type));
5221 return type;
5224 /* Called when we need to protect a variable object using a save_expr. */
5226 tree
5227 maybe_variable (tree gnu_operand)
5229 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
5230 || TREE_CODE (gnu_operand) == SAVE_EXPR
5231 || TREE_CODE (gnu_operand) == NULL_EXPR)
5232 return gnu_operand;
5234 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
5236 tree gnu_result = build1 (UNCONSTRAINED_ARRAY_REF,
5237 TREE_TYPE (gnu_operand),
5238 variable_size (TREE_OPERAND (gnu_operand, 0)));
5240 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result)
5241 = TYPE_READONLY (TREE_TYPE (TREE_TYPE (gnu_operand)));
5242 return gnu_result;
5244 else
5245 return variable_size (gnu_operand);
5248 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
5249 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
5250 return the GCC tree to use for that expression. GNU_NAME is the
5251 qualification to use if an external name is appropriate and DEFINITION is
5252 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
5253 we need a result. Otherwise, we are just elaborating this for
5254 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
5255 purposes even if it isn't needed for code generation. */
5257 static tree
5258 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity,
5259 tree gnu_name, bool definition, bool need_value,
5260 bool need_debug)
5262 tree gnu_expr;
5264 /* If we already elaborated this expression (e.g., it was involved
5265 in the definition of a private type), use the old value. */
5266 if (present_gnu_tree (gnat_expr))
5267 return get_gnu_tree (gnat_expr);
5269 /* If we don't need a value and this is static or a discriminant, we
5270 don't need to do anything. */
5271 else if (!need_value
5272 && (Is_OK_Static_Expression (gnat_expr)
5273 || (Nkind (gnat_expr) == N_Identifier
5274 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
5275 return 0;
5277 /* Otherwise, convert this tree to its GCC equivalent. */
5278 gnu_expr
5279 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
5280 gnu_name, definition, need_debug);
5282 /* Save the expression in case we try to elaborate this entity again. Since
5283 this is not a DECL, don't check it. Don't save if it's a discriminant. */
5284 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
5285 save_gnu_tree (gnat_expr, gnu_expr, true);
5287 return need_value ? gnu_expr : error_mark_node;
5290 /* Similar, but take a GNU expression. */
5292 static tree
5293 elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
5294 tree gnu_expr, tree gnu_name, bool definition,
5295 bool need_debug)
5297 tree gnu_decl = NULL_TREE;
5298 /* Skip any conversions and simple arithmetics to see if the expression
5299 is a read-only variable.
5300 ??? This really should remain read-only, but we have to think about
5301 the typing of the tree here. */
5302 tree gnu_inner_expr
5303 = skip_simple_arithmetic (remove_conversions (gnu_expr, true));
5304 bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
5305 bool expr_variable;
5307 /* In most cases, we won't see a naked FIELD_DECL here because a
5308 discriminant reference will have been replaced with a COMPONENT_REF
5309 when the type is being elaborated. However, there are some cases
5310 involving child types where we will. So convert it to a COMPONENT_REF
5311 here. We have to hope it will be at the highest level of the
5312 expression in these cases. */
5313 if (TREE_CODE (gnu_expr) == FIELD_DECL)
5314 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
5315 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
5316 gnu_expr, NULL_TREE);
5318 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
5319 that is read-only, make a variable that is initialized to contain the
5320 bound when the package containing the definition is elaborated. If
5321 this entity is defined at top level and a bound or discriminant value
5322 isn't a constant or a reference to a discriminant, replace the bound
5323 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
5324 rely here on the fact that an expression cannot contain both the
5325 discriminant and some other variable. */
5327 expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
5328 && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
5329 && (TREE_READONLY (gnu_inner_expr)
5330 || DECL_READONLY_ONCE_ELAB (gnu_inner_expr)))
5331 && !CONTAINS_PLACEHOLDER_P (gnu_expr));
5333 /* If this is a static expression or contains a discriminant, we don't
5334 need the variable for debugging (and can't elaborate anyway if a
5335 discriminant). */
5336 if (need_debug
5337 && (Is_OK_Static_Expression (gnat_expr)
5338 || CONTAINS_PLACEHOLDER_P (gnu_expr)))
5339 need_debug = false;
5341 /* Now create the variable if we need it. */
5342 if (need_debug || (expr_variable && expr_global))
5343 gnu_decl
5344 = create_var_decl (create_concat_name (gnat_entity,
5345 IDENTIFIER_POINTER (gnu_name)),
5346 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
5347 !need_debug, Is_Public (gnat_entity),
5348 !definition, false, NULL, gnat_entity);
5350 /* We only need to use this variable if we are in global context since GCC
5351 can do the right thing in the local case. */
5352 if (expr_global && expr_variable)
5353 return gnu_decl;
5354 else if (!expr_variable)
5355 return gnu_expr;
5356 else
5357 return maybe_variable (gnu_expr);
5360 /* Create a record type that contains a SIZE bytes long field of TYPE with a
5361 starting bit position so that it is aligned to ALIGN bits, and leaving at
5362 least ROOM bytes free before the field. BASE_ALIGN is the alignment the
5363 record is guaranteed to get. */
5365 tree
5366 make_aligning_type (tree type, unsigned int align, tree size,
5367 unsigned int base_align, int room)
5369 /* We will be crafting a record type with one field at a position set to be
5370 the next multiple of ALIGN past record'address + room bytes. We use a
5371 record placeholder to express record'address. */
5373 tree record_type = make_node (RECORD_TYPE);
5374 tree record = build0 (PLACEHOLDER_EXPR, record_type);
5376 tree record_addr_st
5377 = convert (sizetype, build_unary_op (ADDR_EXPR, NULL_TREE, record));
5379 /* The diagram below summarizes the shape of what we manipulate:
5381 <--------- pos ---------->
5382 { +------------+-------------+-----------------+
5383 record =>{ |############| ... | field (type) |
5384 { +------------+-------------+-----------------+
5385 |<-- room -->|<- voffset ->|<---- size ----->|
5388 record_addr vblock_addr
5390 Every length is in sizetype bytes there, except "pos" which has to be
5391 set as a bit position in the GCC tree for the record. */
5393 tree room_st = size_int (room);
5394 tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
5395 tree voffset_st, pos, field;
5397 tree name = TYPE_NAME (type);
5399 if (TREE_CODE (name) == TYPE_DECL)
5400 name = DECL_NAME (name);
5402 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
5404 /* Compute VOFFSET and then POS. The next byte position multiple of some
5405 alignment after some address is obtained by "and"ing the alignment minus
5406 1 with the two's complement of the address. */
5408 voffset_st = size_binop (BIT_AND_EXPR,
5409 size_diffop (size_zero_node, vblock_addr_st),
5410 ssize_int ((align / BITS_PER_UNIT) - 1));
5412 /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype. */
5414 pos = size_binop (MULT_EXPR,
5415 convert (bitsizetype,
5416 size_binop (PLUS_EXPR, room_st, voffset_st)),
5417 bitsize_unit_node);
5419 /* Craft the GCC record representation. We exceptionally do everything
5420 manually here because 1) our generic circuitry is not quite ready to
5421 handle the complex position/size expressions we are setting up, 2) we
5422 have a strong simplifying factor at hand: we know the maximum possible
5423 value of voffset, and 3) we have to set/reset at least the sizes in
5424 accordance with this maximum value anyway, as we need them to convey
5425 what should be "alloc"ated for this type.
5427 Use -1 as the 'addressable' indication for the field to prevent the
5428 creation of a bitfield. We don't need one, it would have damaging
5429 consequences on the alignment computation, and create_field_decl would
5430 make one without this special argument, for instance because of the
5431 complex position expression. */
5433 field = create_field_decl (get_identifier ("F"), type, record_type,
5434 1, size, pos, -1);
5435 TYPE_FIELDS (record_type) = field;
5437 TYPE_ALIGN (record_type) = base_align;
5438 TYPE_USER_ALIGN (record_type) = 1;
5440 TYPE_SIZE (record_type)
5441 = size_binop (PLUS_EXPR,
5442 size_binop (MULT_EXPR, convert (bitsizetype, size),
5443 bitsize_unit_node),
5444 bitsize_int (align + room * BITS_PER_UNIT));
5445 TYPE_SIZE_UNIT (record_type)
5446 = size_binop (PLUS_EXPR, size,
5447 size_int (room + align / BITS_PER_UNIT));
5449 TYPE_MODE (record_type) = BLKmode;
5451 copy_alias_set (record_type, type);
5452 return record_type;
5455 /* Return the result of rounding T up to ALIGN. */
5457 static inline unsigned HOST_WIDE_INT
5458 round_up_to_align (unsigned HOST_WIDE_INT t, unsigned int align)
5460 t += align - 1;
5461 t /= align;
5462 t *= align;
5463 return t;
5466 /* TYPE is a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE that is being used
5467 as the field type of a packed record if IN_RECORD is true, or as the
5468 component type of a packed array if IN_RECORD is false. See if we can
5469 rewrite it either as a type that has a non-BLKmode, which we can pack
5470 tighter in the packed record case, or as a smaller type with BLKmode.
5471 If so, return the new type. If not, return the original type. */
5473 static tree
5474 make_packable_type (tree type, bool in_record)
5476 unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
5477 unsigned HOST_WIDE_INT new_size;
5478 tree new_type, old_field, field_list = NULL_TREE;
5480 /* No point in doing anything if the size is zero. */
5481 if (size == 0)
5482 return type;
5484 new_type = make_node (TREE_CODE (type));
5486 /* Copy the name and flags from the old type to that of the new.
5487 Note that we rely on the pointer equality created here for
5488 TYPE_NAME to look through conversions in various places. */
5489 TYPE_NAME (new_type) = TYPE_NAME (type);
5490 TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
5491 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
5492 if (TREE_CODE (type) == RECORD_TYPE)
5493 TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
5495 /* If we are in a record and have a small size, set the alignment to
5496 try for an integral mode. Otherwise set it to try for a smaller
5497 type with BLKmode. */
5498 if (in_record && size <= MAX_FIXED_MODE_SIZE)
5500 TYPE_ALIGN (new_type) = ceil_alignment (size);
5501 new_size = round_up_to_align (size, TYPE_ALIGN (new_type));
5503 else
5505 unsigned HOST_WIDE_INT align;
5507 /* Do not try to shrink the size if the RM size is not constant. */
5508 if (TYPE_CONTAINS_TEMPLATE_P (type)
5509 || !host_integerp (TYPE_ADA_SIZE (type), 1))
5510 return type;
5512 /* Round the RM size up to a unit boundary to get the minimal size
5513 for a BLKmode record. Give up if it's already the size. */
5514 new_size = TREE_INT_CST_LOW (TYPE_ADA_SIZE (type));
5515 new_size = round_up_to_align (new_size, BITS_PER_UNIT);
5516 if (new_size == size)
5517 return type;
5519 align = new_size & -new_size;
5520 TYPE_ALIGN (new_type) = MIN (TYPE_ALIGN (type), align);
5523 TYPE_USER_ALIGN (new_type) = 1;
5525 /* Now copy the fields, keeping the position and size as we don't want
5526 to change the layout by propagating the packedness downwards. */
5527 for (old_field = TYPE_FIELDS (type); old_field;
5528 old_field = TREE_CHAIN (old_field))
5530 tree new_field_type = TREE_TYPE (old_field);
5531 tree new_field, new_size;
5533 if (TYPE_MODE (new_field_type) == BLKmode
5534 && (TREE_CODE (new_field_type) == RECORD_TYPE
5535 || TREE_CODE (new_field_type) == UNION_TYPE
5536 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
5537 && host_integerp (TYPE_SIZE (new_field_type), 1))
5538 new_field_type = make_packable_type (new_field_type, true);
5540 /* However, for the last field in a not already packed record type
5541 that is of an aggregate type, we need to use the RM_Size in the
5542 packable version of the record type, see finish_record_type. */
5543 if (!TREE_CHAIN (old_field)
5544 && !TYPE_PACKED (type)
5545 && (TREE_CODE (new_field_type) == RECORD_TYPE
5546 || TREE_CODE (new_field_type) == UNION_TYPE
5547 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
5548 && !TYPE_IS_FAT_POINTER_P (new_field_type)
5549 && !TYPE_CONTAINS_TEMPLATE_P (new_field_type)
5550 && TYPE_ADA_SIZE (new_field_type))
5551 new_size = TYPE_ADA_SIZE (new_field_type);
5552 else
5553 new_size = DECL_SIZE (old_field);
5555 new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
5556 new_type, TYPE_PACKED (type), new_size,
5557 bit_position (old_field),
5558 !DECL_NONADDRESSABLE_P (old_field));
5560 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
5561 SET_DECL_ORIGINAL_FIELD
5562 (new_field, (DECL_ORIGINAL_FIELD (old_field)
5563 ? DECL_ORIGINAL_FIELD (old_field) : old_field));
5565 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
5566 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
5568 TREE_CHAIN (new_field) = field_list;
5569 field_list = new_field;
5572 finish_record_type (new_type, nreverse (field_list), 2, true);
5573 copy_alias_set (new_type, type);
5575 /* If this is a padding record, we never want to make the size smaller
5576 than what was specified. For QUAL_UNION_TYPE, also copy the size. */
5577 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5578 || TREE_CODE (type) == QUAL_UNION_TYPE)
5580 TYPE_SIZE (new_type) = TYPE_SIZE (type);
5581 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
5583 else
5585 TYPE_SIZE (new_type) = bitsize_int (new_size);
5586 TYPE_SIZE_UNIT (new_type)
5587 = size_int ((new_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
5590 if (!TYPE_CONTAINS_TEMPLATE_P (type))
5591 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (type));
5593 compute_record_mode (new_type);
5595 /* Try harder to get a packable type if necessary, for example
5596 in case the record itself contains a BLKmode field. */
5597 if (in_record && TYPE_MODE (new_type) == BLKmode)
5598 TYPE_MODE (new_type)
5599 = mode_for_size_tree (TYPE_SIZE (new_type), MODE_INT, 1);
5601 /* If neither the mode nor the size has shrunk, return the old type. */
5602 if (TYPE_MODE (new_type) == BLKmode && new_size >= size)
5603 return type;
5605 return new_type;
5608 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
5609 if needed. We have already verified that SIZE and TYPE are large enough.
5611 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
5612 to issue a warning.
5614 IS_USER_TYPE is true if we must complete the original type.
5616 DEFINITION is true if this type is being defined.
5618 SAME_RM_SIZE is true if the RM_Size of the resulting type is to be set
5619 to SIZE too; otherwise, it's set to the RM_Size of the original type. */
5621 tree
5622 maybe_pad_type (tree type, tree size, unsigned int align,
5623 Entity_Id gnat_entity, const char *name_trailer,
5624 bool is_user_type, bool definition, bool same_rm_size)
5626 tree orig_rm_size = same_rm_size ? NULL_TREE : rm_size (type);
5627 tree orig_size = TYPE_SIZE (type);
5628 unsigned int orig_align = align;
5629 tree record, field;
5631 /* If TYPE is a padded type, see if it agrees with any size and alignment
5632 we were given. If so, return the original type. Otherwise, strip
5633 off the padding, since we will either be returning the inner type
5634 or repadding it. If no size or alignment is specified, use that of
5635 the original padded type. */
5636 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5638 if ((!size
5639 || operand_equal_p (round_up (size,
5640 MAX (align, TYPE_ALIGN (type))),
5641 round_up (TYPE_SIZE (type),
5642 MAX (align, TYPE_ALIGN (type))),
5644 && (align == 0 || align == TYPE_ALIGN (type)))
5645 return type;
5647 if (!size)
5648 size = TYPE_SIZE (type);
5649 if (align == 0)
5650 align = TYPE_ALIGN (type);
5652 type = TREE_TYPE (TYPE_FIELDS (type));
5653 orig_size = TYPE_SIZE (type);
5656 /* If the size is either not being changed or is being made smaller (which
5657 is not done here (and is only valid for bitfields anyway), show the size
5658 isn't changing. Likewise, clear the alignment if it isn't being
5659 changed. Then return if we aren't doing anything. */
5660 if (size
5661 && (operand_equal_p (size, orig_size, 0)
5662 || (TREE_CODE (orig_size) == INTEGER_CST
5663 && tree_int_cst_lt (size, orig_size))))
5664 size = NULL_TREE;
5666 if (align == TYPE_ALIGN (type))
5667 align = 0;
5669 if (align == 0 && !size)
5670 return type;
5672 /* If requested, complete the original type and give it a name. */
5673 if (is_user_type)
5674 create_type_decl (get_entity_name (gnat_entity), type,
5675 NULL, !Comes_From_Source (gnat_entity),
5676 !(TYPE_NAME (type)
5677 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
5678 && DECL_IGNORED_P (TYPE_NAME (type))),
5679 gnat_entity);
5681 /* We used to modify the record in place in some cases, but that could
5682 generate incorrect debugging information. So make a new record
5683 type and name. */
5684 record = make_node (RECORD_TYPE);
5685 TYPE_IS_PADDING_P (record) = 1;
5687 if (Present (gnat_entity))
5688 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
5690 TYPE_VOLATILE (record)
5691 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
5693 TYPE_ALIGN (record) = align;
5694 if (orig_align)
5695 TYPE_USER_ALIGN (record) = align;
5697 TYPE_SIZE (record) = size ? size : orig_size;
5698 TYPE_SIZE_UNIT (record)
5699 = convert (sizetype,
5700 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
5701 bitsize_unit_node));
5703 /* If we are changing the alignment and the input type is a record with
5704 BLKmode and a small constant size, try to make a form that has an
5705 integral mode. This might allow the padding record to also have an
5706 integral mode, which will be much more efficient. There is no point
5707 in doing so if a size is specified unless it is also a small constant
5708 size and it is incorrect to do so if we cannot guarantee that the mode
5709 will be naturally aligned since the field must always be addressable.
5711 ??? This might not always be a win when done for a stand-alone object:
5712 since the nominal and the effective type of the object will now have
5713 different modes, a VIEW_CONVERT_EXPR will be required for converting
5714 between them and it might be hard to overcome afterwards, including
5715 at the RTL level when the stand-alone object is accessed as a whole. */
5716 if (align != 0
5717 && TREE_CODE (type) == RECORD_TYPE
5718 && TYPE_MODE (type) == BLKmode
5719 && TREE_CODE (orig_size) == INTEGER_CST
5720 && !TREE_CONSTANT_OVERFLOW (orig_size)
5721 && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
5722 && (!size
5723 || (TREE_CODE (size) == INTEGER_CST
5724 && compare_tree_int (size, MAX_FIXED_MODE_SIZE) <= 0)))
5726 tree packable_type = make_packable_type (type, true);
5727 if (TYPE_MODE (packable_type) != BLKmode
5728 && align >= TYPE_ALIGN (packable_type))
5729 type = packable_type;
5732 /* Now create the field with the original size. */
5733 field = create_field_decl (get_identifier ("F"), type, record, 0,
5734 orig_size, bitsize_zero_node, 1);
5735 DECL_INTERNAL_P (field) = 1;
5737 /* Do not finalize it until after the auxiliary record is built. */
5738 finish_record_type (record, field, 1, true);
5740 /* Set the same size for its RM_size if requested; otherwise reuse
5741 the RM_size of the original type. */
5742 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : orig_rm_size);
5744 /* Unless debugging information isn't being written for the input type,
5745 write a record that shows what we are a subtype of and also make a
5746 variable that indicates our size, if still variable. */
5747 if (TYPE_NAME (record)
5748 && AGGREGATE_TYPE_P (type)
5749 && TREE_CODE (orig_size) != INTEGER_CST
5750 && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
5751 && DECL_IGNORED_P (TYPE_NAME (type))))
5753 tree marker = make_node (RECORD_TYPE);
5754 tree name = TYPE_NAME (record);
5755 tree orig_name = TYPE_NAME (type);
5757 if (TREE_CODE (name) == TYPE_DECL)
5758 name = DECL_NAME (name);
5760 if (TREE_CODE (orig_name) == TYPE_DECL)
5761 orig_name = DECL_NAME (orig_name);
5763 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
5764 finish_record_type (marker,
5765 create_field_decl (orig_name, integer_type_node,
5766 marker, 0, NULL_TREE, NULL_TREE,
5768 0, false);
5770 if (size && TREE_CODE (size) != INTEGER_CST && definition)
5771 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
5772 bitsizetype, TYPE_SIZE (record), false, false, false,
5773 false, NULL, gnat_entity);
5776 rest_of_record_type_compilation (record);
5778 /* If the size was widened explicitly, maybe give a warning. Take the
5779 original size as the maximum size of the input if there was an
5780 unconstrained record involved and round it up to the specified alignment,
5781 if one was specified. */
5782 if (CONTAINS_PLACEHOLDER_P (orig_size))
5783 orig_size = max_size (orig_size, true);
5785 if (align)
5786 orig_size = round_up (orig_size, align);
5788 if (size && Present (gnat_entity)
5789 && !operand_equal_p (size, orig_size, 0)
5790 && !(TREE_CODE (size) == INTEGER_CST
5791 && TREE_CODE (orig_size) == INTEGER_CST
5792 && tree_int_cst_lt (size, orig_size)))
5794 Node_Id gnat_error_node = Empty;
5796 if (Is_Packed_Array_Type (gnat_entity))
5797 gnat_entity = Original_Array_Type (gnat_entity);
5799 if ((Ekind (gnat_entity) == E_Component
5800 || Ekind (gnat_entity) == E_Discriminant)
5801 && Present (Component_Clause (gnat_entity)))
5802 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
5803 else if (Present (Size_Clause (gnat_entity)))
5804 gnat_error_node = Expression (Size_Clause (gnat_entity));
5806 /* Generate message only for entities that come from source, since
5807 if we have an entity created by expansion, the message will be
5808 generated for some other corresponding source entity. */
5809 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
5810 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
5811 gnat_entity,
5812 size_diffop (size, orig_size));
5814 else if (*name_trailer == 'C' && !Is_Internal (gnat_entity))
5815 post_error_ne_tree ("component of& padded{ by ^ bits}?",
5816 gnat_entity, gnat_entity,
5817 size_diffop (size, orig_size));
5820 return record;
5823 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
5824 the value passed against the list of choices. */
5826 tree
5827 choices_to_gnu (tree operand, Node_Id choices)
5829 Node_Id choice;
5830 Node_Id gnat_temp;
5831 tree result = integer_zero_node;
5832 tree this_test, low = 0, high = 0, single = 0;
5834 for (choice = First (choices); Present (choice); choice = Next (choice))
5836 switch (Nkind (choice))
5838 case N_Range:
5839 low = gnat_to_gnu (Low_Bound (choice));
5840 high = gnat_to_gnu (High_Bound (choice));
5842 /* There's no good type to use here, so we might as well use
5843 integer_type_node. */
5844 this_test
5845 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5846 build_binary_op (GE_EXPR, integer_type_node,
5847 operand, low),
5848 build_binary_op (LE_EXPR, integer_type_node,
5849 operand, high));
5851 break;
5853 case N_Subtype_Indication:
5854 gnat_temp = Range_Expression (Constraint (choice));
5855 low = gnat_to_gnu (Low_Bound (gnat_temp));
5856 high = gnat_to_gnu (High_Bound (gnat_temp));
5858 this_test
5859 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5860 build_binary_op (GE_EXPR, integer_type_node,
5861 operand, low),
5862 build_binary_op (LE_EXPR, integer_type_node,
5863 operand, high));
5864 break;
5866 case N_Identifier:
5867 case N_Expanded_Name:
5868 /* This represents either a subtype range, an enumeration
5869 literal, or a constant Ekind says which. If an enumeration
5870 literal or constant, fall through to the next case. */
5871 if (Ekind (Entity (choice)) != E_Enumeration_Literal
5872 && Ekind (Entity (choice)) != E_Constant)
5874 tree type = gnat_to_gnu_type (Entity (choice));
5876 low = TYPE_MIN_VALUE (type);
5877 high = TYPE_MAX_VALUE (type);
5879 this_test
5880 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5881 build_binary_op (GE_EXPR, integer_type_node,
5882 operand, low),
5883 build_binary_op (LE_EXPR, integer_type_node,
5884 operand, high));
5885 break;
5887 /* ... fall through ... */
5888 case N_Character_Literal:
5889 case N_Integer_Literal:
5890 single = gnat_to_gnu (choice);
5891 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
5892 single);
5893 break;
5895 case N_Others_Choice:
5896 this_test = integer_one_node;
5897 break;
5899 default:
5900 gcc_unreachable ();
5903 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
5904 result, this_test);
5907 return result;
5910 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
5911 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
5913 static int
5914 adjust_packed (tree field_type, tree record_type, int packed)
5916 /* If the field contains an item of variable size, we cannot pack it
5917 because we cannot create temporaries of non-fixed size in case
5918 we need to take the address of the field. See addressable_p and
5919 the notes on the addressability issues for further details. */
5920 if (is_variable_size (field_type))
5921 return 0;
5923 /* If the alignment of the record is specified and the field type
5924 is over-aligned, request Storage_Unit alignment for the field. */
5925 if (packed == -2)
5927 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
5928 return -1;
5929 else
5930 return 0;
5933 return packed;
5936 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
5937 placed in GNU_RECORD_TYPE.
5939 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
5940 record has Component_Alignment of Storage_Unit, -2 if the enclosing
5941 record has a specified alignment.
5943 DEFINITION is true if this field is for a record being defined. */
5945 static tree
5946 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
5947 bool definition)
5949 tree gnu_field_id = get_entity_name (gnat_field);
5950 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
5951 tree gnu_field, gnu_size, gnu_pos;
5952 bool needs_strict_alignment
5953 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
5954 || Treat_As_Volatile (gnat_field));
5956 /* If this field requires strict alignment, we cannot pack it because
5957 it would very likely be under-aligned in the record. */
5958 if (needs_strict_alignment)
5959 packed = 0;
5960 else
5961 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
5963 /* If a size is specified, use it. Otherwise, if the record type is packed,
5964 use the official RM size. See "Handling of Type'Size Values" in Einfo
5965 for further details. */
5966 if (Known_Static_Esize (gnat_field))
5967 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5968 gnat_field, FIELD_DECL, false, true);
5969 else if (packed == 1)
5970 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
5971 gnat_field, FIELD_DECL, false, true);
5972 else
5973 gnu_size = NULL_TREE;
5975 /* If we have a specified size that's smaller than that of the field type,
5976 or a position is specified, and the field type is also a record that's
5977 BLKmode, see if we can get either an integral mode form of the type or
5978 a smaller BLKmode form. If we can, show a size was specified for the
5979 field if there wasn't one already, so we know to make this a bitfield
5980 and avoid making things wider.
5982 Doing this is first useful if the record is packed because we may then
5983 place the field at a non-byte-aligned position and so achieve tighter
5984 packing.
5986 This is in addition *required* if the field shares a byte with another
5987 field and the front-end lets the back-end handle the references, because
5988 GCC does not handle BLKmode bitfields properly.
5990 We avoid the transformation if it is not required or potentially useful,
5991 as it might entail an increase of the field's alignment and have ripple
5992 effects on the outer record type. A typical case is a field known to be
5993 byte aligned and not to share a byte with another field.
5995 Besides, we don't even look the possibility of a transformation in cases
5996 known to be in error already, for instance when an invalid size results
5997 from a component clause. */
5999 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6000 && TYPE_MODE (gnu_field_type) == BLKmode
6001 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
6002 && (packed == 1
6003 || (gnu_size
6004 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6005 || Present (Component_Clause (gnat_field))))))
6007 /* See what the alternate type and size would be. */
6008 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6010 bool has_byte_aligned_clause
6011 = Present (Component_Clause (gnat_field))
6012 && (UI_To_Int (Component_Bit_Offset (gnat_field))
6013 % BITS_PER_UNIT == 0);
6015 /* Compute whether we should avoid the substitution. */
6016 bool reject
6017 /* There is no point substituting if there is no change... */
6018 = (gnu_packable_type == gnu_field_type)
6019 /* ... nor when the field is known to be byte aligned and not to
6020 share a byte with another field. */
6021 || (has_byte_aligned_clause
6022 && value_factor_p (gnu_size, BITS_PER_UNIT))
6023 /* The size of an aliased field must be an exact multiple of the
6024 type's alignment, which the substitution might increase. Reject
6025 substitutions that would so invalidate a component clause when the
6026 specified position is byte aligned, as the change would have no
6027 real benefit from the packing standpoint anyway. */
6028 || (Is_Aliased (gnat_field)
6029 && has_byte_aligned_clause
6030 && !value_factor_p (gnu_size, TYPE_ALIGN (gnu_packable_type)));
6032 /* Substitute unless told otherwise. */
6033 if (!reject)
6035 gnu_field_type = gnu_packable_type;
6037 if (!gnu_size)
6038 gnu_size = rm_size (gnu_field_type);
6042 /* If we are packing the record and the field is BLKmode, round the
6043 size up to a byte boundary. */
6044 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6045 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6047 if (Present (Component_Clause (gnat_field)))
6049 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6050 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6051 gnat_field, FIELD_DECL, false, true);
6053 /* Ensure the position does not overlap with the parent subtype,
6054 if there is one. */
6055 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
6057 tree gnu_parent
6058 = gnat_to_gnu_type (Parent_Subtype
6059 (Underlying_Type (Scope (gnat_field))));
6061 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6062 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6064 post_error_ne_tree
6065 ("offset of& must be beyond parent{, minimum allowed is ^}",
6066 First_Bit (Component_Clause (gnat_field)), gnat_field,
6067 TYPE_SIZE_UNIT (gnu_parent));
6071 /* If this field needs strict alignment, ensure the record is
6072 sufficiently aligned and that that position and size are
6073 consistent with the alignment. */
6074 if (needs_strict_alignment)
6076 TYPE_ALIGN (gnu_record_type)
6077 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6079 if (gnu_size
6080 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6082 if (Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
6083 post_error_ne_tree
6084 ("atomic field& must be natural size of type{ (^)}",
6085 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6086 TYPE_SIZE (gnu_field_type));
6088 else if (Is_Aliased (gnat_field))
6089 post_error_ne_tree
6090 ("size of aliased field& must be ^ bits",
6091 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6092 TYPE_SIZE (gnu_field_type));
6094 else if (Strict_Alignment (Etype (gnat_field)))
6095 post_error_ne_tree
6096 ("size of & with aliased or tagged components not ^ bits",
6097 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6098 TYPE_SIZE (gnu_field_type));
6100 gnu_size = NULL_TREE;
6103 if (!integer_zerop (size_binop
6104 (TRUNC_MOD_EXPR, gnu_pos,
6105 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6107 if (Is_Aliased (gnat_field))
6108 post_error_ne_num
6109 ("position of aliased field& must be multiple of ^ bits",
6110 First_Bit (Component_Clause (gnat_field)), gnat_field,
6111 TYPE_ALIGN (gnu_field_type));
6113 else if (Treat_As_Volatile (gnat_field))
6114 post_error_ne_num
6115 ("position of volatile field& must be multiple of ^ bits",
6116 First_Bit (Component_Clause (gnat_field)), gnat_field,
6117 TYPE_ALIGN (gnu_field_type));
6119 else if (Strict_Alignment (Etype (gnat_field)))
6120 post_error_ne_num
6121 ("position of & with aliased or tagged components not multiple of ^ bits",
6122 First_Bit (Component_Clause (gnat_field)), gnat_field,
6123 TYPE_ALIGN (gnu_field_type));
6125 else
6126 gcc_unreachable ();
6128 gnu_pos = NULL_TREE;
6132 if (Is_Atomic (gnat_field))
6133 check_ok_for_atomic (gnu_field_type, gnat_field, false);
6136 /* If the record has rep clauses and this is the tag field, make a rep
6137 clause for it as well. */
6138 else if (Has_Specified_Layout (Scope (gnat_field))
6139 && Chars (gnat_field) == Name_uTag)
6141 gnu_pos = bitsize_zero_node;
6142 gnu_size = TYPE_SIZE (gnu_field_type);
6145 else
6146 gnu_pos = NULL_TREE;
6148 /* We need to make the size the maximum for the type if it is
6149 self-referential and an unconstrained type. In that case, we can't
6150 pack the field since we can't make a copy to align it. */
6151 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6152 && !gnu_size
6153 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6154 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
6156 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6157 packed = 0;
6160 /* If a size is specified, adjust the field's type to it. */
6161 if (gnu_size)
6163 /* If the field's type is justified modular, we would need to remove
6164 the wrapper to (better) meet the layout requirements. However we
6165 can do so only if the field is not aliased to preserve the unique
6166 layout and if the prescribed size is not greater than that of the
6167 packed array to preserve the justification. */
6168 if (!needs_strict_alignment
6169 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6170 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6171 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6172 <= 0)
6173 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6175 gnu_field_type
6176 = make_type_from_size (gnu_field_type, gnu_size,
6177 Has_Biased_Representation (gnat_field));
6178 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6179 "PAD", false, definition, true);
6182 /* Otherwise (or if there was an error), don't specify a position. */
6183 else
6184 gnu_pos = NULL_TREE;
6186 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6187 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6189 /* Now create the decl for the field. */
6190 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6191 packed, gnu_size, gnu_pos,
6192 Is_Aliased (gnat_field));
6193 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6194 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
6196 if (Ekind (gnat_field) == E_Discriminant)
6197 DECL_DISCRIMINANT_NUMBER (gnu_field)
6198 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6200 return gnu_field;
6203 /* Return true if TYPE is a type with variable size, a padding type with a
6204 field of variable size or is a record that has a field such a field. */
6206 static bool
6207 is_variable_size (tree type)
6209 tree field;
6211 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6212 return true;
6214 if (TREE_CODE (type) == RECORD_TYPE
6215 && TYPE_IS_PADDING_P (type)
6216 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6217 return true;
6219 if (TREE_CODE (type) != RECORD_TYPE
6220 && TREE_CODE (type) != UNION_TYPE
6221 && TREE_CODE (type) != QUAL_UNION_TYPE)
6222 return false;
6224 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6225 if (is_variable_size (TREE_TYPE (field)))
6226 return true;
6228 return false;
6231 /* qsort comparer for the bit positions of two record components. */
6233 static int
6234 compare_field_bitpos (const PTR rt1, const PTR rt2)
6236 const_tree const field1 = * (const_tree const *) rt1;
6237 const_tree const field2 = * (const_tree const *) rt2;
6238 const int ret
6239 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6241 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6244 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
6245 of GCC trees for fields that are in the record and have already been
6246 processed. When called from gnat_to_gnu_entity during the processing of a
6247 record type definition, the GCC nodes for the discriminants will be on
6248 the chain. The other calls to this function are recursive calls from
6249 itself for the Component_List of a variant and the chain is empty.
6251 PACKED is 1 if this is for a packed record, -1 if this is for a record
6252 with Component_Alignment of Storage_Unit, -2 if this is for a record
6253 with a specified alignment.
6255 DEFINITION is true if we are defining this record.
6257 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6258 with a rep clause is to be added. If it is nonzero, that is all that
6259 should be done with such fields.
6261 CANCEL_ALIGNMENT, if true, means the alignment should be zeroed before
6262 laying out the record. This means the alignment only serves to force fields
6263 to be bitfields, but not require the record to be that aligned. This is
6264 used for variants.
6266 ALL_REP, if true, means a rep clause was found for all the fields. This
6267 simplifies the logic since we know we're not in the mixed case.
6269 DO_NOT_FINALIZE, if true, means that the record type is expected to be
6270 modified afterwards so it will not be sent to the back-end for finalization.
6272 UNCHECKED_UNION, if true, means that we are building a type for a record
6273 with a Pragma Unchecked_Union.
6275 The processing of the component list fills in the chain with all of the
6276 fields of the record and then the record type is finished. */
6278 static void
6279 components_to_record (tree gnu_record_type, Node_Id component_list,
6280 tree gnu_field_list, int packed, bool definition,
6281 tree *p_gnu_rep_list, bool cancel_alignment,
6282 bool all_rep, bool do_not_finalize, bool unchecked_union)
6284 Node_Id component_decl;
6285 Entity_Id gnat_field;
6286 Node_Id variant_part;
6287 tree gnu_our_rep_list = NULL_TREE;
6288 tree gnu_field, gnu_last;
6289 bool layout_with_rep = false;
6290 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6292 /* For each variable within each component declaration create a GCC field
6293 and add it to the list, skipping any pragmas in the list. */
6294 if (Present (Component_Items (component_list)))
6295 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6296 Present (component_decl);
6297 component_decl = Next_Non_Pragma (component_decl))
6299 gnat_field = Defining_Entity (component_decl);
6301 if (Chars (gnat_field) == Name_uParent)
6302 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
6303 else
6305 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
6306 packed, definition);
6308 /* If this is the _Tag field, put it before any discriminants,
6309 instead of after them as is the case for all other fields.
6310 Ignore field of void type if only annotating. */
6311 if (Chars (gnat_field) == Name_uTag)
6312 gnu_field_list = chainon (gnu_field_list, gnu_field);
6313 else
6315 TREE_CHAIN (gnu_field) = gnu_field_list;
6316 gnu_field_list = gnu_field;
6320 save_gnu_tree (gnat_field, gnu_field, false);
6323 /* At the end of the component list there may be a variant part. */
6324 variant_part = Variant_Part (component_list);
6326 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6327 mutually exclusive and should go in the same memory. To do this we need
6328 to treat each variant as a record whose elements are created from the
6329 component list for the variant. So here we create the records from the
6330 lists for the variants and put them all into the QUAL_UNION_TYPE.
6331 If this is an Unchecked_Union, we make a UNION_TYPE instead or
6332 use GNU_RECORD_TYPE if there are no fields so far. */
6333 if (Present (variant_part))
6335 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
6336 Node_Id variant;
6337 tree gnu_name = TYPE_NAME (gnu_record_type);
6338 tree gnu_var_name
6339 = concat_id_with_name (get_identifier (Get_Name_String
6340 (Chars (Name (variant_part)))),
6341 "XVN");
6342 tree gnu_union_type;
6343 tree gnu_union_name;
6344 tree gnu_union_field;
6345 tree gnu_variant_list = NULL_TREE;
6347 if (TREE_CODE (gnu_name) == TYPE_DECL)
6348 gnu_name = DECL_NAME (gnu_name);
6350 gnu_union_name = concat_id_with_name (gnu_name,
6351 IDENTIFIER_POINTER (gnu_var_name));
6353 /* Reuse an enclosing union if all fields are in the variant part
6354 and there is no representation clause on the record, to match
6355 the layout of C unions. There is an associated check below. */
6356 if (!gnu_field_list
6357 && TREE_CODE (gnu_record_type) == UNION_TYPE
6358 && !TYPE_PACKED (gnu_record_type))
6359 gnu_union_type = gnu_record_type;
6360 else
6362 gnu_union_type
6363 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
6365 TYPE_NAME (gnu_union_type) = gnu_union_name;
6366 TYPE_ALIGN (gnu_union_type) = 0;
6367 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
6370 for (variant = First_Non_Pragma (Variants (variant_part));
6371 Present (variant);
6372 variant = Next_Non_Pragma (variant))
6374 tree gnu_variant_type = make_node (RECORD_TYPE);
6375 tree gnu_inner_name;
6376 tree gnu_qual;
6378 Get_Variant_Encoding (variant);
6379 gnu_inner_name = get_identifier (Name_Buffer);
6380 TYPE_NAME (gnu_variant_type)
6381 = concat_id_with_name (gnu_union_name,
6382 IDENTIFIER_POINTER (gnu_inner_name));
6384 /* Set the alignment of the inner type in case we need to make
6385 inner objects into bitfields, but then clear it out
6386 so the record actually gets only the alignment required. */
6387 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
6388 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
6390 /* Similarly, if the outer record has a size specified and all fields
6391 have record rep clauses, we can propagate the size into the
6392 variant part. */
6393 if (all_rep_and_size)
6395 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
6396 TYPE_SIZE_UNIT (gnu_variant_type)
6397 = TYPE_SIZE_UNIT (gnu_record_type);
6400 /* Create the record type for the variant. Note that we defer
6401 finalizing it until after we are sure to actually use it. */
6402 components_to_record (gnu_variant_type, Component_List (variant),
6403 NULL_TREE, packed, definition,
6404 &gnu_our_rep_list, !all_rep_and_size, all_rep,
6405 true, unchecked_union);
6407 gnu_qual = choices_to_gnu (gnu_discriminant,
6408 Discrete_Choices (variant));
6410 Set_Present_Expr (variant, annotate_value (gnu_qual));
6412 /* If this is an Unchecked_Union and we have exactly one field,
6413 use this field directly to match the layout of C unions. */
6414 if (unchecked_union
6415 && TYPE_FIELDS (gnu_variant_type)
6416 && !TREE_CHAIN (TYPE_FIELDS (gnu_variant_type)))
6417 gnu_field = TYPE_FIELDS (gnu_variant_type);
6418 else
6420 /* Deal with packedness like in gnat_to_gnu_field. */
6421 int field_packed
6422 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
6424 /* Finalize the record type now. We used to throw away
6425 empty records but we no longer do that because we need
6426 them to generate complete debug info for the variant;
6427 otherwise, the union type definition will be lacking
6428 the fields associated with these empty variants. */
6429 rest_of_record_type_compilation (gnu_variant_type);
6431 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
6432 gnu_union_type, field_packed,
6433 (all_rep_and_size
6434 ? TYPE_SIZE (gnu_variant_type)
6435 : 0),
6436 (all_rep_and_size
6437 ? bitsize_zero_node : 0),
6440 DECL_INTERNAL_P (gnu_field) = 1;
6442 if (!unchecked_union)
6443 DECL_QUALIFIER (gnu_field) = gnu_qual;
6446 TREE_CHAIN (gnu_field) = gnu_variant_list;
6447 gnu_variant_list = gnu_field;
6450 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
6451 if (gnu_variant_list)
6453 if (all_rep_and_size)
6455 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
6456 TYPE_SIZE_UNIT (gnu_union_type)
6457 = TYPE_SIZE_UNIT (gnu_record_type);
6460 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
6461 all_rep_and_size ? 1 : 0, false);
6463 /* If GNU_UNION_TYPE is our record type, it means we must have an
6464 Unchecked_Union with no fields. Verify that and, if so, just
6465 return. */
6466 if (gnu_union_type == gnu_record_type)
6468 gcc_assert (unchecked_union
6469 && !gnu_field_list
6470 && !gnu_our_rep_list);
6471 return;
6474 gnu_union_field
6475 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
6476 packed,
6477 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
6478 all_rep ? bitsize_zero_node : 0, 0);
6480 DECL_INTERNAL_P (gnu_union_field) = 1;
6481 TREE_CHAIN (gnu_union_field) = gnu_field_list;
6482 gnu_field_list = gnu_union_field;
6486 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
6487 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
6488 in a separate pass since we want to handle the discriminants but can't
6489 play with them until we've used them in debugging data above.
6491 ??? Note: if we then reorder them, debugging information will be wrong,
6492 but there's nothing that can be done about this at the moment. */
6493 for (gnu_field = gnu_field_list, gnu_last = NULL_TREE; gnu_field; )
6495 if (DECL_FIELD_OFFSET (gnu_field))
6497 tree gnu_next = TREE_CHAIN (gnu_field);
6499 if (!gnu_last)
6500 gnu_field_list = gnu_next;
6501 else
6502 TREE_CHAIN (gnu_last) = gnu_next;
6504 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
6505 gnu_our_rep_list = gnu_field;
6506 gnu_field = gnu_next;
6508 else
6510 gnu_last = gnu_field;
6511 gnu_field = TREE_CHAIN (gnu_field);
6515 /* If we have any items in our rep'ed field list, it is not the case that all
6516 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
6517 set it and ignore the items. */
6518 if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
6519 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
6520 else if (gnu_our_rep_list)
6522 /* Otherwise, sort the fields by bit position and put them into their
6523 own record if we have any fields without rep clauses. */
6524 tree gnu_rep_type
6525 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
6526 int len = list_length (gnu_our_rep_list);
6527 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
6528 int i;
6530 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
6531 gnu_field = TREE_CHAIN (gnu_field), i++)
6532 gnu_arr[i] = gnu_field;
6534 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
6536 /* Put the fields in the list in order of increasing position, which
6537 means we start from the end. */
6538 gnu_our_rep_list = NULL_TREE;
6539 for (i = len - 1; i >= 0; i--)
6541 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
6542 gnu_our_rep_list = gnu_arr[i];
6543 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
6546 if (gnu_field_list)
6548 finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, false);
6549 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
6550 gnu_record_type, 0, 0, 0, 1);
6551 DECL_INTERNAL_P (gnu_field) = 1;
6552 gnu_field_list = chainon (gnu_field_list, gnu_field);
6554 else
6556 layout_with_rep = true;
6557 gnu_field_list = nreverse (gnu_our_rep_list);
6561 if (cancel_alignment)
6562 TYPE_ALIGN (gnu_record_type) = 0;
6564 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
6565 layout_with_rep ? 1 : 0, do_not_finalize);
6568 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
6569 placed into an Esize, Component_Bit_Offset, or Component_Size value
6570 in the GNAT tree. */
6572 static Uint
6573 annotate_value (tree gnu_size)
6575 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
6576 TCode tcode;
6577 Node_Ref_Or_Val ops[3], ret;
6578 int i;
6579 int size;
6580 struct tree_int_map **h = NULL;
6582 /* See if we've already saved the value for this node. */
6583 if (EXPR_P (gnu_size))
6585 struct tree_int_map in;
6586 if (!annotate_value_cache)
6587 annotate_value_cache = htab_create_ggc (512, tree_int_map_hash,
6588 tree_int_map_eq, 0);
6589 in.base.from = gnu_size;
6590 h = (struct tree_int_map **)
6591 htab_find_slot (annotate_value_cache, &in, INSERT);
6593 if (*h)
6594 return (Node_Ref_Or_Val) (*h)->to;
6597 /* If we do not return inside this switch, TCODE will be set to the
6598 code to use for a Create_Node operand and LEN (set above) will be
6599 the number of recursive calls for us to make. */
6601 switch (TREE_CODE (gnu_size))
6603 case INTEGER_CST:
6604 if (TREE_OVERFLOW (gnu_size))
6605 return No_Uint;
6607 /* This may have come from a conversion from some smaller type,
6608 so ensure this is in bitsizetype. */
6609 gnu_size = convert (bitsizetype, gnu_size);
6611 /* For negative values, use NEGATE_EXPR of the supplied value. */
6612 if (tree_int_cst_sgn (gnu_size) < 0)
6614 /* The ridiculous code below is to handle the case of the largest
6615 negative integer. */
6616 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
6617 bool adjust = false;
6618 tree temp;
6620 if (TREE_OVERFLOW (negative_size))
6622 negative_size
6623 = size_binop (MINUS_EXPR, bitsize_zero_node,
6624 size_binop (PLUS_EXPR, gnu_size,
6625 bitsize_one_node));
6626 adjust = true;
6629 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
6630 if (adjust)
6631 temp = build2 (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
6633 return annotate_value (temp);
6636 if (!host_integerp (gnu_size, 1))
6637 return No_Uint;
6639 size = tree_low_cst (gnu_size, 1);
6641 /* This peculiar test is to make sure that the size fits in an int
6642 on machines where HOST_WIDE_INT is not "int". */
6643 if (tree_low_cst (gnu_size, 1) == size)
6644 return UI_From_Int (size);
6645 else
6646 return No_Uint;
6648 case COMPONENT_REF:
6649 /* The only case we handle here is a simple discriminant reference. */
6650 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
6651 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
6652 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
6653 return Create_Node (Discrim_Val,
6654 annotate_value (DECL_DISCRIMINANT_NUMBER
6655 (TREE_OPERAND (gnu_size, 1))),
6656 No_Uint, No_Uint);
6657 else
6658 return No_Uint;
6660 CASE_CONVERT: case NON_LVALUE_EXPR:
6661 return annotate_value (TREE_OPERAND (gnu_size, 0));
6663 /* Now just list the operations we handle. */
6664 case COND_EXPR: tcode = Cond_Expr; break;
6665 case PLUS_EXPR: tcode = Plus_Expr; break;
6666 case MINUS_EXPR: tcode = Minus_Expr; break;
6667 case MULT_EXPR: tcode = Mult_Expr; break;
6668 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
6669 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
6670 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
6671 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
6672 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
6673 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
6674 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
6675 case NEGATE_EXPR: tcode = Negate_Expr; break;
6676 case MIN_EXPR: tcode = Min_Expr; break;
6677 case MAX_EXPR: tcode = Max_Expr; break;
6678 case ABS_EXPR: tcode = Abs_Expr; break;
6679 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
6680 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
6681 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
6682 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
6683 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
6684 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
6685 case BIT_AND_EXPR: tcode = Bit_And_Expr; break;
6686 case LT_EXPR: tcode = Lt_Expr; break;
6687 case LE_EXPR: tcode = Le_Expr; break;
6688 case GT_EXPR: tcode = Gt_Expr; break;
6689 case GE_EXPR: tcode = Ge_Expr; break;
6690 case EQ_EXPR: tcode = Eq_Expr; break;
6691 case NE_EXPR: tcode = Ne_Expr; break;
6693 default:
6694 return No_Uint;
6697 /* Now get each of the operands that's relevant for this code. If any
6698 cannot be expressed as a repinfo node, say we can't. */
6699 for (i = 0; i < 3; i++)
6700 ops[i] = No_Uint;
6702 for (i = 0; i < len; i++)
6704 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
6705 if (ops[i] == No_Uint)
6706 return No_Uint;
6709 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
6711 /* Save the result in the cache. */
6712 if (h)
6714 *h = ggc_alloc (sizeof (struct tree_int_map));
6715 (*h)->base.from = gnu_size;
6716 (*h)->to = ret;
6719 return ret;
6722 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
6723 GCC type, set Component_Bit_Offset and Esize to the position and size
6724 used by Gigi. */
6726 static void
6727 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
6729 tree gnu_list;
6730 tree gnu_entry;
6731 Entity_Id gnat_field;
6733 /* We operate by first making a list of all fields and their positions
6734 (we can get the sizes easily at any time) by a recursive call
6735 and then update all the sizes into the tree. */
6736 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
6737 size_zero_node, bitsize_zero_node,
6738 BIGGEST_ALIGNMENT);
6740 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
6741 gnat_field = Next_Entity (gnat_field))
6742 if ((Ekind (gnat_field) == E_Component
6743 || (Ekind (gnat_field) == E_Discriminant
6744 && !Is_Unchecked_Union (Scope (gnat_field)))))
6746 tree parent_offset = bitsize_zero_node;
6748 gnu_entry = purpose_member (gnat_to_gnu_field_decl (gnat_field),
6749 gnu_list);
6751 if (gnu_entry)
6753 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
6755 /* In this mode the tag and parent components have not been
6756 generated, so we add the appropriate offset to each
6757 component. For a component appearing in the current
6758 extension, the offset is the size of the parent. */
6759 if (Is_Derived_Type (gnat_entity)
6760 && Original_Record_Component (gnat_field) == gnat_field)
6761 parent_offset
6762 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
6763 bitsizetype);
6764 else
6765 parent_offset = bitsize_int (POINTER_SIZE);
6768 Set_Component_Bit_Offset
6769 (gnat_field,
6770 annotate_value
6771 (size_binop (PLUS_EXPR,
6772 bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
6773 TREE_VALUE (TREE_VALUE
6774 (TREE_VALUE (gnu_entry)))),
6775 parent_offset)));
6777 Set_Esize (gnat_field,
6778 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
6780 else if (Is_Tagged_Type (gnat_entity)
6781 && Is_Derived_Type (gnat_entity))
6783 /* If there is no gnu_entry, this is an inherited component whose
6784 position is the same as in the parent type. */
6785 Set_Component_Bit_Offset
6786 (gnat_field,
6787 Component_Bit_Offset (Original_Record_Component (gnat_field)));
6788 Set_Esize (gnat_field,
6789 Esize (Original_Record_Component (gnat_field)));
6794 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
6795 FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
6796 position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
6797 placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position. GNU_POS is
6798 to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
6799 the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
6800 so far. */
6802 static tree
6803 compute_field_positions (tree gnu_type, tree gnu_list, tree gnu_pos,
6804 tree gnu_bitpos, unsigned int offset_align)
6806 tree gnu_field;
6807 tree gnu_result = gnu_list;
6809 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
6810 gnu_field = TREE_CHAIN (gnu_field))
6812 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
6813 DECL_FIELD_BIT_OFFSET (gnu_field));
6814 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
6815 DECL_FIELD_OFFSET (gnu_field));
6816 unsigned int our_offset_align
6817 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
6819 gnu_result
6820 = tree_cons (gnu_field,
6821 tree_cons (gnu_our_offset,
6822 tree_cons (size_int (our_offset_align),
6823 gnu_our_bitpos, NULL_TREE),
6824 NULL_TREE),
6825 gnu_result);
6827 if (DECL_INTERNAL_P (gnu_field))
6828 gnu_result
6829 = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
6830 gnu_our_offset, gnu_our_bitpos,
6831 our_offset_align);
6834 return gnu_result;
6837 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
6838 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
6839 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
6840 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
6841 for the size of a field. COMPONENT_P is true if we are being called
6842 to process the Component_Size of GNAT_OBJECT. This is used for error
6843 message handling and to indicate to use the object size of GNU_TYPE.
6844 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
6845 it means that a size of zero should be treated as an unspecified size. */
6847 static tree
6848 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
6849 enum tree_code kind, bool component_p, bool zero_ok)
6851 Node_Id gnat_error_node;
6852 tree type_size, size;
6854 if (kind == VAR_DECL
6855 /* If a type needs strict alignment, a component of this type in
6856 a packed record cannot be packed and thus uses the type size. */
6857 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
6858 type_size = TYPE_SIZE (gnu_type);
6859 else
6860 type_size = rm_size (gnu_type);
6862 /* Find the node to use for errors. */
6863 if ((Ekind (gnat_object) == E_Component
6864 || Ekind (gnat_object) == E_Discriminant)
6865 && Present (Component_Clause (gnat_object)))
6866 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
6867 else if (Present (Size_Clause (gnat_object)))
6868 gnat_error_node = Expression (Size_Clause (gnat_object));
6869 else
6870 gnat_error_node = gnat_object;
6872 /* Return 0 if no size was specified, either because Esize was not Present or
6873 the specified size was zero. */
6874 if (No (uint_size) || uint_size == No_Uint)
6875 return NULL_TREE;
6877 /* Get the size as a tree. Give an error if a size was specified, but cannot
6878 be represented as in sizetype. */
6879 size = UI_To_gnu (uint_size, bitsizetype);
6880 if (TREE_OVERFLOW (size))
6882 post_error_ne (component_p ? "component size of & is too large"
6883 : "size of & is too large",
6884 gnat_error_node, gnat_object);
6885 return NULL_TREE;
6888 /* Ignore a negative size since that corresponds to our back-annotation.
6889 Also ignore a zero size unless a size clause exists. */
6890 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
6891 return NULL_TREE;
6893 /* The size of objects is always a multiple of a byte. */
6894 if (kind == VAR_DECL
6895 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
6897 if (component_p)
6898 post_error_ne ("component size for& is not a multiple of Storage_Unit",
6899 gnat_error_node, gnat_object);
6900 else
6901 post_error_ne ("size for& is not a multiple of Storage_Unit",
6902 gnat_error_node, gnat_object);
6903 return NULL_TREE;
6906 /* If this is an integral type or a packed array type, the front-end has
6907 verified the size, so we need not do it here (which would entail
6908 checking against the bounds). However, if this is an aliased object, it
6909 may not be smaller than the type of the object. */
6910 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
6911 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
6912 return size;
6914 /* If the object is a record that contains a template, add the size of
6915 the template to the specified size. */
6916 if (TREE_CODE (gnu_type) == RECORD_TYPE
6917 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6918 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
6920 /* Modify the size of the type to be that of the maximum size if it has a
6921 discriminant. */
6922 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
6923 type_size = max_size (type_size, true);
6925 /* If this is an access type or a fat pointer, the minimum size is that given
6926 by the smallest integral mode that's valid for pointers. */
6927 if ((TREE_CODE (gnu_type) == POINTER_TYPE) || TYPE_FAT_POINTER_P (gnu_type))
6929 enum machine_mode p_mode;
6931 for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
6932 !targetm.valid_pointer_mode (p_mode);
6933 p_mode = GET_MODE_WIDER_MODE (p_mode))
6936 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
6939 /* If the size of the object is a constant, the new size must not be
6940 smaller. */
6941 if (TREE_CODE (type_size) != INTEGER_CST
6942 || TREE_OVERFLOW (type_size)
6943 || tree_int_cst_lt (size, type_size))
6945 if (component_p)
6946 post_error_ne_tree
6947 ("component size for& too small{, minimum allowed is ^}",
6948 gnat_error_node, gnat_object, type_size);
6949 else
6950 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
6951 gnat_error_node, gnat_object, type_size);
6953 if (kind == VAR_DECL && !component_p
6954 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
6955 && !tree_int_cst_lt (size, rm_size (gnu_type)))
6956 post_error_ne_tree_2
6957 ("\\size of ^ is not a multiple of alignment (^ bits)",
6958 gnat_error_node, gnat_object, rm_size (gnu_type),
6959 TYPE_ALIGN (gnu_type));
6961 else if (INTEGRAL_TYPE_P (gnu_type))
6962 post_error_ne ("\\size would be legal if & were not aliased!",
6963 gnat_error_node, gnat_object);
6965 return NULL_TREE;
6968 return size;
6971 /* Similarly, but both validate and process a value of RM_Size. This
6972 routine is only called for types. */
6974 static void
6975 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
6977 /* Only give an error if a Value_Size clause was explicitly given.
6978 Otherwise, we'd be duplicating an error on the Size clause. */
6979 Node_Id gnat_attr_node
6980 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
6981 tree old_size = rm_size (gnu_type);
6982 tree size;
6984 /* Get the size as a tree. Do nothing if none was specified, either
6985 because RM_Size was not Present or if the specified size was zero.
6986 Give an error if a size was specified, but cannot be represented as
6987 in sizetype. */
6988 if (No (uint_size) || uint_size == No_Uint)
6989 return;
6991 size = UI_To_gnu (uint_size, bitsizetype);
6992 if (TREE_OVERFLOW (size))
6994 if (Present (gnat_attr_node))
6995 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
6996 gnat_entity);
6998 return;
7001 /* Ignore a negative size since that corresponds to our back-annotation.
7002 Also ignore a zero size unless a size clause exists, a Value_Size
7003 clause exists, or this is an integer type, in which case the
7004 front end will have always set it. */
7005 else if (tree_int_cst_sgn (size) < 0
7006 || (integer_zerop (size) && No (gnat_attr_node)
7007 && !Has_Size_Clause (gnat_entity)
7008 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
7009 return;
7011 /* If the old size is self-referential, get the maximum size. */
7012 if (CONTAINS_PLACEHOLDER_P (old_size))
7013 old_size = max_size (old_size, true);
7015 /* If the size of the object is a constant, the new size must not be
7016 smaller (the front end checks this for scalar types). */
7017 if (TREE_CODE (old_size) != INTEGER_CST
7018 || TREE_OVERFLOW (old_size)
7019 || (AGGREGATE_TYPE_P (gnu_type)
7020 && tree_int_cst_lt (size, old_size)))
7022 if (Present (gnat_attr_node))
7023 post_error_ne_tree
7024 ("Value_Size for& too small{, minimum allowed is ^}",
7025 gnat_attr_node, gnat_entity, old_size);
7027 return;
7030 /* Otherwise, set the RM_Size. */
7031 if (TREE_CODE (gnu_type) == INTEGER_TYPE
7032 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7033 TYPE_RM_SIZE_NUM (gnu_type) = size;
7034 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
7035 TYPE_RM_SIZE_NUM (gnu_type) = size;
7036 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
7037 || TREE_CODE (gnu_type) == UNION_TYPE
7038 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
7039 && !TYPE_IS_FAT_POINTER_P (gnu_type))
7040 SET_TYPE_ADA_SIZE (gnu_type, size);
7043 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
7044 If TYPE is the best type, return it. Otherwise, make a new type. We
7045 only support new integral and pointer types. BIASED_P is nonzero if
7046 we are making a biased type. */
7048 static tree
7049 make_type_from_size (tree type, tree size_tree, bool biased_p)
7051 tree new_type;
7052 unsigned HOST_WIDE_INT size;
7053 bool unsigned_p;
7055 /* If size indicates an error, just return TYPE to avoid propagating the
7056 error. Likewise if it's too large to represent. */
7057 if (!size_tree || !host_integerp (size_tree, 1))
7058 return type;
7060 size = tree_low_cst (size_tree, 1);
7061 switch (TREE_CODE (type))
7063 case INTEGER_TYPE:
7064 case ENUMERAL_TYPE:
7065 /* Only do something if the type is not already the proper size and is
7066 not a packed array type. */
7067 if (TYPE_PACKED_ARRAY_TYPE_P (type)
7068 || (TYPE_PRECISION (type) == size
7069 && biased_p == (TREE_CODE (type) == INTEGER_CST
7070 && TYPE_BIASED_REPRESENTATION_P (type))))
7071 break;
7073 biased_p |= (TREE_CODE (type) == INTEGER_TYPE
7074 && TYPE_BIASED_REPRESENTATION_P (type));
7075 unsigned_p = TYPE_UNSIGNED (type) || biased_p;
7077 size = MIN (size, LONG_LONG_TYPE_SIZE);
7078 new_type
7079 = unsigned_p ? make_unsigned_type (size) : make_signed_type (size);
7080 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
7081 TYPE_MIN_VALUE (new_type)
7082 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
7083 TYPE_MAX_VALUE (new_type)
7084 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
7085 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
7086 TYPE_RM_SIZE_NUM (new_type) = bitsize_int (size);
7087 return new_type;
7089 case RECORD_TYPE:
7090 /* Do something if this is a fat pointer, in which case we
7091 may need to return the thin pointer. */
7092 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
7093 return
7094 build_pointer_type
7095 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
7096 break;
7098 case POINTER_TYPE:
7099 /* Only do something if this is a thin pointer, in which case we
7100 may need to return the fat pointer. */
7101 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
7102 return
7103 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
7105 break;
7107 default:
7108 break;
7111 return type;
7114 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
7115 a type or object whose present alignment is ALIGN. If this alignment is
7116 valid, return it. Otherwise, give an error and return ALIGN. */
7118 static unsigned int
7119 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
7121 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
7122 unsigned int new_align;
7123 Node_Id gnat_error_node;
7125 /* Don't worry about checking alignment if alignment was not specified
7126 by the source program and we already posted an error for this entity. */
7127 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
7128 return align;
7130 /* Post the error on the alignment clause if any. */
7131 if (Present (Alignment_Clause (gnat_entity)))
7132 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
7133 else
7134 gnat_error_node = gnat_entity;
7136 /* Within GCC, an alignment is an integer, so we must make sure a value is
7137 specified that fits in that range. Also, there is an upper bound to
7138 alignments we can support/allow. */
7139 if (!UI_Is_In_Int_Range (alignment)
7140 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
7141 post_error_ne_num ("largest supported alignment for& is ^",
7142 gnat_error_node, gnat_entity, max_allowed_alignment);
7143 else if (!(Present (Alignment_Clause (gnat_entity))
7144 && From_At_Mod (Alignment_Clause (gnat_entity)))
7145 && new_align * BITS_PER_UNIT < align)
7146 post_error_ne_num ("alignment for& must be at least ^",
7147 gnat_error_node, gnat_entity,
7148 align / BITS_PER_UNIT);
7149 else
7151 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
7152 if (new_align > align)
7153 align = new_align;
7156 return align;
7159 /* Return the smallest alignment not less than SIZE. */
7161 static unsigned int
7162 ceil_alignment (unsigned HOST_WIDE_INT size)
7164 return (unsigned int) 1 << (floor_log2 (size - 1) + 1);
7167 /* Verify that OBJECT, a type or decl, is something we can implement
7168 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
7169 if we require atomic components. */
7171 static void
7172 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
7174 Node_Id gnat_error_point = gnat_entity;
7175 Node_Id gnat_node;
7176 enum machine_mode mode;
7177 unsigned int align;
7178 tree size;
7180 /* There are three case of what OBJECT can be. It can be a type, in which
7181 case we take the size, alignment and mode from the type. It can be a
7182 declaration that was indirect, in which case the relevant values are
7183 that of the type being pointed to, or it can be a normal declaration,
7184 in which case the values are of the decl. The code below assumes that
7185 OBJECT is either a type or a decl. */
7186 if (TYPE_P (object))
7188 mode = TYPE_MODE (object);
7189 align = TYPE_ALIGN (object);
7190 size = TYPE_SIZE (object);
7192 else if (DECL_BY_REF_P (object))
7194 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
7195 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
7196 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
7198 else
7200 mode = DECL_MODE (object);
7201 align = DECL_ALIGN (object);
7202 size = DECL_SIZE (object);
7205 /* Consider all floating-point types atomic and any types that that are
7206 represented by integers no wider than a machine word. */
7207 if (GET_MODE_CLASS (mode) == MODE_FLOAT
7208 || ((GET_MODE_CLASS (mode) == MODE_INT
7209 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
7210 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
7211 return;
7213 /* For the moment, also allow anything that has an alignment equal
7214 to its size and which is smaller than a word. */
7215 if (size && TREE_CODE (size) == INTEGER_CST
7216 && compare_tree_int (size, align) == 0
7217 && align <= BITS_PER_WORD)
7218 return;
7220 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
7221 gnat_node = Next_Rep_Item (gnat_node))
7223 if (!comp_p && Nkind (gnat_node) == N_Pragma
7224 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
7225 == Pragma_Atomic))
7226 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
7227 else if (comp_p && Nkind (gnat_node) == N_Pragma
7228 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
7229 == Pragma_Atomic_Components))
7230 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
7233 if (comp_p)
7234 post_error_ne ("atomic access to component of & cannot be guaranteed",
7235 gnat_error_point, gnat_entity);
7236 else
7237 post_error_ne ("atomic access to & cannot be guaranteed",
7238 gnat_error_point, gnat_entity);
7241 /* Check if FTYPE1 and FTYPE2, two potentially different function type nodes,
7242 have compatible signatures so that a call using one type may be safely
7243 issued if the actual target function type is the other. Return 1 if it is
7244 the case, 0 otherwise, and post errors on the incompatibilities.
7246 This is used when an Ada subprogram is mapped onto a GCC builtin, to ensure
7247 that calls to the subprogram will have arguments suitable for the later
7248 underlying builtin expansion. */
7250 static int
7251 compatible_signatures_p (tree ftype1, tree ftype2)
7253 /* As of now, we only perform very trivial tests and consider it's the
7254 programmer's responsibility to ensure the type correctness in the Ada
7255 declaration, as in the regular Import cases.
7257 Mismatches typically result in either error messages from the builtin
7258 expander, internal compiler errors, or in a real call sequence. This
7259 should be refined to issue diagnostics helping error detection and
7260 correction. */
7262 /* Almost fake test, ensuring a use of each argument. */
7263 if (ftype1 == ftype2)
7264 return 1;
7266 return 1;
7269 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a new
7270 type with all size expressions that contain F updated by replacing F
7271 with R. If F is NULL_TREE, always make a new RECORD_TYPE, even if
7272 nothing has changed. */
7274 tree
7275 substitute_in_type (tree t, tree f, tree r)
7277 tree new = t;
7278 tree tem;
7280 switch (TREE_CODE (t))
7282 case INTEGER_TYPE:
7283 case ENUMERAL_TYPE:
7284 case BOOLEAN_TYPE:
7285 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
7286 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
7288 tree low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
7289 tree high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
7291 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
7292 return t;
7294 new = build_range_type (TREE_TYPE (t), low, high);
7295 if (TYPE_INDEX_TYPE (t))
7296 SET_TYPE_INDEX_TYPE
7297 (new, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
7298 return new;
7301 return t;
7303 case REAL_TYPE:
7304 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
7305 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
7307 tree low = NULL_TREE, high = NULL_TREE;
7309 if (TYPE_MIN_VALUE (t))
7310 low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
7311 if (TYPE_MAX_VALUE (t))
7312 high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
7314 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
7315 return t;
7317 t = copy_type (t);
7318 TYPE_MIN_VALUE (t) = low;
7319 TYPE_MAX_VALUE (t) = high;
7321 return t;
7323 case COMPLEX_TYPE:
7324 tem = substitute_in_type (TREE_TYPE (t), f, r);
7325 if (tem == TREE_TYPE (t))
7326 return t;
7328 return build_complex_type (tem);
7330 case OFFSET_TYPE:
7331 case METHOD_TYPE:
7332 case FUNCTION_TYPE:
7333 case LANG_TYPE:
7334 /* Don't know how to do these yet. */
7335 gcc_unreachable ();
7337 case ARRAY_TYPE:
7339 tree component = substitute_in_type (TREE_TYPE (t), f, r);
7340 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
7342 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7343 return t;
7345 new = build_array_type (component, domain);
7346 TYPE_SIZE (new) = 0;
7347 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
7348 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
7349 layout_type (new);
7350 TYPE_ALIGN (new) = TYPE_ALIGN (t);
7351 TYPE_USER_ALIGN (new) = TYPE_USER_ALIGN (t);
7353 /* If we had bounded the sizes of T by a constant, bound the sizes of
7354 NEW by the same constant. */
7355 if (TREE_CODE (TYPE_SIZE (t)) == MIN_EXPR)
7356 TYPE_SIZE (new)
7357 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE (t), 1),
7358 TYPE_SIZE (new));
7359 if (TREE_CODE (TYPE_SIZE_UNIT (t)) == MIN_EXPR)
7360 TYPE_SIZE_UNIT (new)
7361 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE_UNIT (t), 1),
7362 TYPE_SIZE_UNIT (new));
7363 return new;
7366 case RECORD_TYPE:
7367 case UNION_TYPE:
7368 case QUAL_UNION_TYPE:
7370 tree field;
7371 bool changed_field
7372 = (f == NULL_TREE && !TREE_CONSTANT (TYPE_SIZE (t)));
7373 bool field_has_rep = false;
7374 tree last_field = NULL_TREE;
7376 tree new = copy_type (t);
7378 /* Start out with no fields, make new fields, and chain them
7379 in. If we haven't actually changed the type of any field,
7380 discard everything we've done and return the old type. */
7382 TYPE_FIELDS (new) = NULL_TREE;
7383 TYPE_SIZE (new) = NULL_TREE;
7385 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
7387 tree new_field = copy_node (field);
7389 TREE_TYPE (new_field)
7390 = substitute_in_type (TREE_TYPE (new_field), f, r);
7392 if (DECL_HAS_REP_P (field) && !DECL_INTERNAL_P (field))
7393 field_has_rep = true;
7394 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
7395 changed_field = true;
7397 /* If this is an internal field and the type of this field is
7398 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
7399 the type just has one element, treat that as the field.
7400 But don't do this if we are processing a QUAL_UNION_TYPE. */
7401 if (TREE_CODE (t) != QUAL_UNION_TYPE
7402 && DECL_INTERNAL_P (new_field)
7403 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
7404 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
7406 if (!TYPE_FIELDS (TREE_TYPE (new_field)))
7407 continue;
7409 if (!TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))))
7411 tree next_new_field
7412 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
7414 /* Make sure omitting the union doesn't change
7415 the layout. */
7416 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
7417 new_field = next_new_field;
7421 DECL_CONTEXT (new_field) = new;
7422 SET_DECL_ORIGINAL_FIELD (new_field,
7423 (DECL_ORIGINAL_FIELD (field)
7424 ? DECL_ORIGINAL_FIELD (field) : field));
7426 /* If the size of the old field was set at a constant,
7427 propagate the size in case the type's size was variable.
7428 (This occurs in the case of a variant or discriminated
7429 record with a default size used as a field of another
7430 record.) */
7431 DECL_SIZE (new_field)
7432 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
7433 ? DECL_SIZE (field) : NULL_TREE;
7434 DECL_SIZE_UNIT (new_field)
7435 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
7436 ? DECL_SIZE_UNIT (field) : NULL_TREE;
7438 if (TREE_CODE (t) == QUAL_UNION_TYPE)
7440 tree new_q = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
7442 if (new_q != DECL_QUALIFIER (new_field))
7443 changed_field = true;
7445 /* Do the substitution inside the qualifier and if we find
7446 that this field will not be present, omit it. */
7447 DECL_QUALIFIER (new_field) = new_q;
7449 if (integer_zerop (DECL_QUALIFIER (new_field)))
7450 continue;
7453 if (!last_field)
7454 TYPE_FIELDS (new) = new_field;
7455 else
7456 TREE_CHAIN (last_field) = new_field;
7458 last_field = new_field;
7460 /* If this is a qualified type and this field will always be
7461 present, we are done. */
7462 if (TREE_CODE (t) == QUAL_UNION_TYPE
7463 && integer_onep (DECL_QUALIFIER (new_field)))
7464 break;
7467 /* If this used to be a qualified union type, but we now know what
7468 field will be present, make this a normal union. */
7469 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
7470 && (!TYPE_FIELDS (new)
7471 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
7472 TREE_SET_CODE (new, UNION_TYPE);
7473 else if (!changed_field)
7474 return t;
7476 gcc_assert (!field_has_rep);
7477 layout_type (new);
7479 /* If the size was originally a constant use it. */
7480 if (TYPE_SIZE (t) && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
7481 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
7483 TYPE_SIZE (new) = TYPE_SIZE (t);
7484 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
7485 SET_TYPE_ADA_SIZE (new, TYPE_ADA_SIZE (t));
7488 return new;
7491 default:
7492 return t;
7496 /* Return the "RM size" of GNU_TYPE. This is the actual number of bits
7497 needed to represent the object. */
7499 tree
7500 rm_size (tree gnu_type)
7502 /* For integer types, this is the precision. For record types, we store
7503 the size explicitly. For other types, this is just the size. */
7505 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
7506 return TYPE_RM_SIZE (gnu_type);
7507 else if (TREE_CODE (gnu_type) == RECORD_TYPE
7508 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7509 /* Return the rm_size of the actual data plus the size of the template. */
7510 return
7511 size_binop (PLUS_EXPR,
7512 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
7513 DECL_SIZE (TYPE_FIELDS (gnu_type)));
7514 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
7515 || TREE_CODE (gnu_type) == UNION_TYPE
7516 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
7517 && !TYPE_IS_FAT_POINTER_P (gnu_type)
7518 && TYPE_ADA_SIZE (gnu_type))
7519 return TYPE_ADA_SIZE (gnu_type);
7520 else
7521 return TYPE_SIZE (gnu_type);
7524 /* Return an identifier representing the external name to be used for
7525 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
7526 and the specified suffix. */
7528 tree
7529 create_concat_name (Entity_Id gnat_entity, const char *suffix)
7531 Entity_Kind kind = Ekind (gnat_entity);
7533 const char *str = (!suffix ? "" : suffix);
7534 String_Template temp = {1, strlen (str)};
7535 Fat_Pointer fp = {str, &temp};
7537 Get_External_Name_With_Suffix (gnat_entity, fp);
7539 /* A variable using the Stdcall convention (meaning we are running
7540 on a Windows box) live in a DLL. Here we adjust its name to use
7541 the jump-table, the _imp__NAME contains the address for the NAME
7542 variable. */
7543 if ((kind == E_Variable || kind == E_Constant)
7544 && Has_Stdcall_Convention (gnat_entity))
7546 const char *prefix = "_imp__";
7547 int k, plen = strlen (prefix);
7549 for (k = 0; k <= Name_Len; k++)
7550 Name_Buffer [Name_Len - k + plen] = Name_Buffer [Name_Len - k];
7551 strncpy (Name_Buffer, prefix, plen);
7554 return get_identifier (Name_Buffer);
7557 /* Return the name to be used for GNAT_ENTITY. If a type, create a
7558 fully-qualified name, possibly with type information encoding.
7559 Otherwise, return the name. */
7561 tree
7562 get_entity_name (Entity_Id gnat_entity)
7564 Get_Encoded_Name (gnat_entity);
7565 return get_identifier (Name_Buffer);
7568 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
7569 string, return a new IDENTIFIER_NODE that is the concatenation of
7570 the name in GNU_ID and SUFFIX. */
7572 tree
7573 concat_id_with_name (tree gnu_id, const char *suffix)
7575 int len = IDENTIFIER_LENGTH (gnu_id);
7577 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id), len);
7578 strncpy (Name_Buffer + len, "___", 3);
7579 len += 3;
7580 strcpy (Name_Buffer + len, suffix);
7581 return get_identifier (Name_Buffer);
7584 #include "gt-ada-decl.h"