* gcc-interface/decl.c: Fix various nits.
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blob14fcd634b610ad400520cb87c4de1de4961b9262
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 /* Stack realignment for functions with foreign conventions is provided on a
71 per back-end basis now, as it is handled by the prologue expanders and not
72 as part of the function's body any more. It might be requested by way of a
73 dedicated function type attribute on the targets that support it.
75 We need a way to avoid setting the attribute on the targets that don't
76 support it and use FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN for this purpose.
78 It is defined on targets where the circuitry is available, and indicates
79 whether the realignment is needed for 'main'. We use this to decide for
80 foreign subprograms as well.
82 It is not defined on targets where the circuitry is not implemented, and
83 we just never set the attribute in these cases.
85 Whether it is defined on all targets that would need it in theory is
86 not entirely clear. We currently trust the base GCC settings for this
87 purpose. */
89 #ifndef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
90 #define FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN 0
91 #endif
93 struct incomplete
95 struct incomplete *next;
96 tree old_type;
97 Entity_Id full_type;
100 /* These variables are used to defer recursively expanding incomplete types
101 while we are processing an array, a record or a subprogram type. */
102 static int defer_incomplete_level = 0;
103 static struct incomplete *defer_incomplete_list;
105 /* This variable is used to delay expanding From_With_Type types until the
106 end of the spec. */
107 static struct incomplete *defer_limited_with;
109 /* These variables are used to defer finalizing types. The element of the
110 list is the TYPE_DECL associated with the type. */
111 static int defer_finalize_level = 0;
112 static VEC (tree,heap) *defer_finalize_list;
114 /* A hash table used to cache the result of annotate_value. */
115 static GTY ((if_marked ("tree_int_map_marked_p"),
116 param_is (struct tree_int_map))) htab_t annotate_value_cache;
118 static void copy_alias_set (tree, tree);
119 static tree substitution_list (Entity_Id, Entity_Id, tree, bool);
120 static bool allocatable_size_p (tree, bool);
121 static void prepend_one_attribute_to (struct attrib **,
122 enum attr_type, tree, tree, Node_Id);
123 static void prepend_attributes (Entity_Id, struct attrib **);
124 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
125 static bool is_variable_size (tree);
126 static tree elaborate_expression_1 (Node_Id, Entity_Id, tree, tree,
127 bool, bool);
128 static tree make_packable_type (tree, bool);
129 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
130 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
131 bool *);
132 static bool same_discriminant_p (Entity_Id, Entity_Id);
133 static bool array_type_has_nonaliased_component (Entity_Id, tree);
134 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
135 bool, bool, bool, bool);
136 static Uint annotate_value (tree);
137 static void annotate_rep (Entity_Id, tree);
138 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
139 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
140 static void set_rm_size (Uint, tree, Entity_Id);
141 static tree make_type_from_size (tree, tree, bool);
142 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
143 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
144 static void check_ok_for_atomic (tree, Entity_Id, bool);
145 static int compatible_signatures_p (tree ftype1, tree ftype2);
146 static void rest_of_type_decl_compilation_no_defer (tree);
148 /* Return true if GNAT_ADDRESS is a compile time known value.
149 In particular catch System'To_Address. */
151 static bool
152 compile_time_known_address_p (Node_Id gnat_address)
154 return ((Nkind (gnat_address) == N_Unchecked_Type_Conversion
155 && Compile_Time_Known_Value (Expression (gnat_address)))
156 || Compile_Time_Known_Value (gnat_address));
159 /* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
160 GCC type corresponding to that entity. GNAT_ENTITY is assumed to
161 refer to an Ada type. */
163 tree
164 gnat_to_gnu_type (Entity_Id gnat_entity)
166 tree gnu_decl;
168 /* The back end never attempts to annotate generic types */
169 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
170 return void_type_node;
172 /* Convert the ada entity type into a GCC TYPE_DECL node. */
173 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
174 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
175 return TREE_TYPE (gnu_decl);
178 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
179 entity, this routine returns the equivalent GCC tree for that entity
180 (an ..._DECL node) and associates the ..._DECL node with the input GNAT
181 defining identifier.
183 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
184 initial value (in GCC tree form). This is optional for variables.
185 For renamed entities, GNU_EXPR gives the object being renamed.
187 DEFINITION is nonzero if this call is intended for a definition. This is
188 used for separate compilation where it necessary to know whether an
189 external declaration or a definition should be created if the GCC equivalent
190 was not created previously. The value of 1 is normally used for a nonzero
191 DEFINITION, but a value of 2 is used in special circumstances, defined in
192 the code. */
194 tree
195 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
197 Entity_Id gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
198 tree gnu_entity_id;
199 tree gnu_type = NULL_TREE;
200 /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
201 GNAT tree. This node will be associated with the GNAT node by calling
202 the save_gnu_tree routine at the end of the `switch' statement. */
203 tree gnu_decl = NULL_TREE;
204 /* true if we have already saved gnu_decl as a gnat association. */
205 bool saved = false;
206 /* Nonzero if we incremented defer_incomplete_level. */
207 bool this_deferred = false;
208 /* Nonzero if we incremented force_global. */
209 bool this_global = false;
210 /* Nonzero if we should check to see if elaborated during processing. */
211 bool maybe_present = false;
212 /* Nonzero if we made GNU_DECL and its type here. */
213 bool this_made_decl = false;
214 struct attrib *attr_list = NULL;
215 bool debug_info_p = (Needs_Debug_Info (gnat_entity)
216 || debug_info_level == DINFO_LEVEL_VERBOSE);
217 Entity_Kind kind = Ekind (gnat_entity);
218 Entity_Id gnat_temp;
219 unsigned int esize
220 = ((Known_Esize (gnat_entity)
221 && UI_Is_In_Int_Range (Esize (gnat_entity)))
222 ? MIN (UI_To_Int (Esize (gnat_entity)),
223 IN (kind, Float_Kind)
224 ? fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE)
225 : IN (kind, Access_Kind) ? POINTER_SIZE * 2
226 : LONG_LONG_TYPE_SIZE)
227 : LONG_LONG_TYPE_SIZE);
228 tree gnu_size = 0;
229 bool imported_p
230 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
231 unsigned int align = 0;
233 /* Since a use of an Itype is a definition, process it as such if it
234 is not in a with'ed unit. */
236 if (!definition && Is_Itype (gnat_entity)
237 && !present_gnu_tree (gnat_entity)
238 && In_Extended_Main_Code_Unit (gnat_entity))
240 /* Ensure that we are in a subprogram mentioned in the Scope
241 chain of this entity, our current scope is global,
242 or that we encountered a task or entry (where we can't currently
243 accurately check scoping). */
244 if (!current_function_decl
245 || DECL_ELABORATION_PROC_P (current_function_decl))
247 process_type (gnat_entity);
248 return get_gnu_tree (gnat_entity);
251 for (gnat_temp = Scope (gnat_entity);
252 Present (gnat_temp); gnat_temp = Scope (gnat_temp))
254 if (Is_Type (gnat_temp))
255 gnat_temp = Underlying_Type (gnat_temp);
257 if (Ekind (gnat_temp) == E_Subprogram_Body)
258 gnat_temp
259 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
261 if (IN (Ekind (gnat_temp), Subprogram_Kind)
262 && Present (Protected_Body_Subprogram (gnat_temp)))
263 gnat_temp = Protected_Body_Subprogram (gnat_temp);
265 if (Ekind (gnat_temp) == E_Entry
266 || Ekind (gnat_temp) == E_Entry_Family
267 || Ekind (gnat_temp) == E_Task_Type
268 || (IN (Ekind (gnat_temp), Subprogram_Kind)
269 && present_gnu_tree (gnat_temp)
270 && (current_function_decl
271 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
273 process_type (gnat_entity);
274 return get_gnu_tree (gnat_entity);
278 /* This abort means the entity "gnat_entity" has an incorrect scope,
279 i.e. that its scope does not correspond to the subprogram in which
280 it is declared */
281 gcc_unreachable ();
284 /* If this is entity 0, something went badly wrong. */
285 gcc_assert (Present (gnat_entity));
287 /* If we've already processed this entity, return what we got last time.
288 If we are defining the node, we should not have already processed it.
289 In that case, we will abort below when we try to save a new GCC tree for
290 this object. We also need to handle the case of getting a dummy type
291 when a Full_View exists. */
293 if (present_gnu_tree (gnat_entity)
294 && (!definition || (Is_Type (gnat_entity) && imported_p)))
296 gnu_decl = get_gnu_tree (gnat_entity);
298 if (TREE_CODE (gnu_decl) == TYPE_DECL
299 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
300 && IN (kind, Incomplete_Or_Private_Kind)
301 && Present (Full_View (gnat_entity)))
303 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
304 NULL_TREE, 0);
306 save_gnu_tree (gnat_entity, NULL_TREE, false);
307 save_gnu_tree (gnat_entity, gnu_decl, false);
310 return gnu_decl;
313 /* If this is a numeric or enumeral type, or an access type, a nonzero
314 Esize must be specified unless it was specified by the programmer. */
315 gcc_assert (!Unknown_Esize (gnat_entity)
316 || Has_Size_Clause (gnat_entity)
317 || (!IN (kind, Numeric_Kind) && !IN (kind, Enumeration_Kind)
318 && (!IN (kind, Access_Kind)
319 || kind == E_Access_Protected_Subprogram_Type
320 || kind == E_Anonymous_Access_Protected_Subprogram_Type
321 || kind == E_Access_Subtype)));
323 /* Likewise, RM_Size must be specified for all discrete and fixed-point
324 types. */
325 gcc_assert (!IN (kind, Discrete_Or_Fixed_Point_Kind)
326 || !Unknown_RM_Size (gnat_entity));
328 /* Get the name of the entity and set up the line number and filename of
329 the original definition for use in any decl we make. */
330 gnu_entity_id = get_entity_name (gnat_entity);
331 Sloc_to_locus (Sloc (gnat_entity), &input_location);
333 /* If we get here, it means we have not yet done anything with this
334 entity. If we are not defining it here, it must be external,
335 otherwise we should have defined it already. */
336 gcc_assert (definition || Is_Public (gnat_entity) || type_annotate_only
337 || kind == E_Discriminant || kind == E_Component
338 || kind == E_Label
339 || (kind == E_Constant && Present (Full_View (gnat_entity)))
340 || IN (kind, Type_Kind));
342 /* For cases when we are not defining (i.e., we are referencing from
343 another compilation unit) Public entities, show we are at global level
344 for the purpose of computing scopes. Don't do this for components or
345 discriminants since the relevant test is whether or not the record is
346 being defined. But do this for Imported functions or procedures in
347 all cases. */
348 if ((!definition && Is_Public (gnat_entity)
349 && !Is_Statically_Allocated (gnat_entity)
350 && kind != E_Discriminant && kind != E_Component)
351 || (Is_Imported (gnat_entity)
352 && (kind == E_Function || kind == E_Procedure)))
353 force_global++, this_global = true;
355 /* Handle any attributes directly attached to the entity. */
356 if (Has_Gigi_Rep_Item (gnat_entity))
357 prepend_attributes (gnat_entity, &attr_list);
359 /* Machine_Attributes on types are expected to be propagated to subtypes.
360 The corresponding Gigi_Rep_Items are only attached to the first subtype
361 though, so we handle the propagation here. */
362 if (Is_Type (gnat_entity) && Base_Type (gnat_entity) != gnat_entity
363 && !Is_First_Subtype (gnat_entity)
364 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
365 prepend_attributes (First_Subtype (Base_Type (gnat_entity)), &attr_list);
367 switch (kind)
369 case E_Constant:
370 /* If this is a use of a deferred constant without address clause,
371 get its full definition. */
372 if (!definition
373 && No (Address_Clause (gnat_entity))
374 && Present (Full_View (gnat_entity)))
376 gnu_decl
377 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
378 saved = true;
379 break;
382 /* If we have an external constant that we are not defining, get the
383 expression that is was defined to represent. We may throw that
384 expression away later if it is not a constant. Do not retrieve the
385 expression if it is an aggregate or allocator, because in complex
386 instantiation contexts it may not be expanded */
387 if (!definition
388 && Present (Expression (Declaration_Node (gnat_entity)))
389 && !No_Initialization (Declaration_Node (gnat_entity))
390 && (Nkind (Expression (Declaration_Node (gnat_entity)))
391 != N_Aggregate)
392 && (Nkind (Expression (Declaration_Node (gnat_entity)))
393 != N_Allocator))
394 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
396 /* Ignore deferred constant definitions without address clause since
397 they are processed fully in the front-end. If No_Initialization
398 is set, this is not a deferred constant but a constant whose value
399 is built manually. And constants that are renamings are handled
400 like variables. */
401 if (definition
402 && !gnu_expr
403 && No (Address_Clause (gnat_entity))
404 && !No_Initialization (Declaration_Node (gnat_entity))
405 && No (Renamed_Object (gnat_entity)))
407 gnu_decl = error_mark_node;
408 saved = true;
409 break;
412 /* Ignore constant definitions already marked with the error node. See
413 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
414 if (definition
415 && gnu_expr
416 && present_gnu_tree (gnat_entity)
417 && get_gnu_tree (gnat_entity) == error_mark_node)
419 maybe_present = true;
420 break;
423 goto object;
425 case E_Exception:
426 /* We used to special case VMS exceptions here to directly map them to
427 their associated condition code. Since this code had to be masked
428 dynamically to strip off the severity bits, this caused trouble in
429 the GCC/ZCX case because the "type" pointers we store in the tables
430 have to be static. We now don't special case here anymore, and let
431 the regular processing take place, which leaves us with a regular
432 exception data object for VMS exceptions too. The condition code
433 mapping is taken care of by the front end and the bitmasking by the
434 runtime library. */
435 goto object;
437 case E_Discriminant:
438 case E_Component:
440 /* The GNAT record where the component was defined. */
441 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
443 /* If the variable is an inherited record component (in the case of
444 extended record types), just return the inherited entity, which
445 must be a FIELD_DECL. Likewise for discriminants.
446 For discriminants of untagged records which have explicit
447 stored discriminants, return the entity for the corresponding
448 stored discriminant. Also use Original_Record_Component
449 if the record has a private extension. */
451 if (Present (Original_Record_Component (gnat_entity))
452 && Original_Record_Component (gnat_entity) != gnat_entity)
454 gnu_decl
455 = gnat_to_gnu_entity (Original_Record_Component (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 previous
466 branch). */
468 else if (Present (Corresponding_Discriminant (gnat_entity))
469 && Is_Tagged_Type (gnat_record))
471 /* A tagged record has no explicit stored discriminants. */
473 gcc_assert (First_Discriminant (gnat_record)
474 == First_Stored_Discriminant (gnat_record));
475 gnu_decl
476 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
477 gnu_expr, definition);
478 saved = true;
479 break;
482 else if (Present (CR_Discriminant (gnat_entity))
483 && type_annotate_only)
485 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
486 gnu_expr, definition);
487 saved = true;
488 break;
491 /* If the enclosing record has explicit stored discriminants, then
492 it is an untagged record. If the Corresponding_Discriminant
493 is not empty then this must be a renamed discriminant and its
494 Original_Record_Component must point to the corresponding explicit
495 stored discriminant (i.e., we should have taken the first
496 branch). */
498 else if (Present (Corresponding_Discriminant (gnat_entity))
499 && (First_Discriminant (gnat_record)
500 != First_Stored_Discriminant (gnat_record)))
501 gcc_unreachable ();
503 /* Otherwise, if we are not defining this and we have no GCC type
504 for the containing record, make one for it. Then we should
505 have made our own equivalent. */
506 else if (!definition && !present_gnu_tree (gnat_record))
508 /* ??? If this is in a record whose scope is a protected
509 type and we have an Original_Record_Component, use it.
510 This is a workaround for major problems in protected type
511 handling. */
512 Entity_Id Scop = Scope (Scope (gnat_entity));
513 if ((Is_Protected_Type (Scop)
514 || (Is_Private_Type (Scop)
515 && Present (Full_View (Scop))
516 && Is_Protected_Type (Full_View (Scop))))
517 && Present (Original_Record_Component (gnat_entity)))
519 gnu_decl
520 = gnat_to_gnu_entity (Original_Record_Component
521 (gnat_entity),
522 gnu_expr, 0);
523 saved = true;
524 break;
527 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
528 gnu_decl = get_gnu_tree (gnat_entity);
529 saved = true;
530 break;
533 else
534 /* Here we have no GCC type and this is a reference rather than a
535 definition. This should never happen. Most likely the cause is
536 reference before declaration in the gnat tree for gnat_entity. */
537 gcc_unreachable ();
540 case E_Loop_Parameter:
541 case E_Out_Parameter:
542 case E_Variable:
544 /* Simple variables, loop variables, Out parameters, and exceptions. */
545 object:
547 bool used_by_ref = false;
548 bool const_flag
549 = ((kind == E_Constant || kind == E_Variable)
550 && Is_True_Constant (gnat_entity)
551 && !Treat_As_Volatile (gnat_entity)
552 && (((Nkind (Declaration_Node (gnat_entity))
553 == N_Object_Declaration)
554 && Present (Expression (Declaration_Node (gnat_entity))))
555 || Present (Renamed_Object (gnat_entity))));
556 bool inner_const_flag = const_flag;
557 bool static_p = Is_Statically_Allocated (gnat_entity);
558 bool mutable_p = false;
559 tree gnu_ext_name = NULL_TREE;
560 tree renamed_obj = NULL_TREE;
561 tree gnu_object_size;
563 if (Present (Renamed_Object (gnat_entity)) && !definition)
565 if (kind == E_Exception)
566 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
567 NULL_TREE, 0);
568 else
569 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
572 /* Get the type after elaborating the renamed object. */
573 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
575 /* For a debug renaming declaration, build a pure debug entity. */
576 if (Present (Debug_Renaming_Link (gnat_entity)))
578 rtx addr;
579 gnu_decl = build_decl (VAR_DECL, gnu_entity_id, gnu_type);
580 /* The (MEM (CONST (0))) pattern is prescribed by STABS. */
581 if (global_bindings_p ())
582 addr = gen_rtx_CONST (VOIDmode, const0_rtx);
583 else
584 addr = stack_pointer_rtx;
585 SET_DECL_RTL (gnu_decl, gen_rtx_MEM (Pmode, addr));
586 gnat_pushdecl (gnu_decl, gnat_entity);
587 break;
590 /* If this is a loop variable, its type should be the base type.
591 This is because the code for processing a loop determines whether
592 a normal loop end test can be done by comparing the bounds of the
593 loop against those of the base type, which is presumed to be the
594 size used for computation. But this is not correct when the size
595 of the subtype is smaller than the type. */
596 if (kind == E_Loop_Parameter)
597 gnu_type = get_base_type (gnu_type);
599 /* Reject non-renamed objects whose types are unconstrained arrays or
600 any object whose type is a dummy type or VOID_TYPE. */
602 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
603 && No (Renamed_Object (gnat_entity)))
604 || TYPE_IS_DUMMY_P (gnu_type)
605 || TREE_CODE (gnu_type) == VOID_TYPE)
607 gcc_assert (type_annotate_only);
608 if (this_global)
609 force_global--;
610 return error_mark_node;
613 /* If an alignment is specified, use it if valid. Note that
614 exceptions are objects but don't have alignments. We must do this
615 before we validate the size, since the alignment can affect the
616 size. */
617 if (kind != E_Exception && Known_Alignment (gnat_entity))
619 gcc_assert (Present (Alignment (gnat_entity)));
620 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
621 TYPE_ALIGN (gnu_type));
622 gnu_type = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
623 "PAD", false, definition, true);
626 /* If we are defining the object, see if it has a Size value and
627 validate it if so. If we are not defining the object and a Size
628 clause applies, simply retrieve the value. We don't want to ignore
629 the clause and it is expected to have been validated already. Then
630 get the new type, if any. */
631 if (definition)
632 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
633 gnat_entity, VAR_DECL, false,
634 Has_Size_Clause (gnat_entity));
635 else if (Has_Size_Clause (gnat_entity))
636 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
638 if (gnu_size)
640 gnu_type
641 = make_type_from_size (gnu_type, gnu_size,
642 Has_Biased_Representation (gnat_entity));
644 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
645 gnu_size = NULL_TREE;
648 /* If this object has self-referential size, it must be a record with
649 a default value. We are supposed to allocate an object of the
650 maximum size in this case unless it is a constant with an
651 initializing expression, in which case we can get the size from
652 that. Note that the resulting size may still be a variable, so
653 this may end up with an indirect allocation. */
654 if (No (Renamed_Object (gnat_entity))
655 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
657 if (gnu_expr && kind == E_Constant)
659 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
660 if (CONTAINS_PLACEHOLDER_P (size))
662 /* If the initializing expression is itself a constant,
663 despite having a nominal type with self-referential
664 size, we can get the size directly from it. */
665 if (TREE_CODE (gnu_expr) == COMPONENT_REF
666 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
667 == RECORD_TYPE
668 && TYPE_IS_PADDING_P
669 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
670 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
671 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
672 || DECL_READONLY_ONCE_ELAB
673 (TREE_OPERAND (gnu_expr, 0))))
674 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
675 else
676 gnu_size
677 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
679 else
680 gnu_size = size;
682 /* We may have no GNU_EXPR because No_Initialization is
683 set even though there's an Expression. */
684 else if (kind == E_Constant
685 && (Nkind (Declaration_Node (gnat_entity))
686 == N_Object_Declaration)
687 && Present (Expression (Declaration_Node (gnat_entity))))
688 gnu_size
689 = TYPE_SIZE (gnat_to_gnu_type
690 (Etype
691 (Expression (Declaration_Node (gnat_entity)))));
692 else
694 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
695 mutable_p = true;
699 /* If the size is zero bytes, make it one byte since some linkers have
700 trouble with zero-sized objects. If the object will have a
701 template, that will make it nonzero so don't bother. Also avoid
702 doing that for an object renaming or an object with an address
703 clause, as we would lose useful information on the view size
704 (e.g. for null array slices) and we are not allocating the object
705 here anyway. */
706 if (((gnu_size
707 && integer_zerop (gnu_size)
708 && !TREE_OVERFLOW (gnu_size))
709 || (TYPE_SIZE (gnu_type)
710 && integer_zerop (TYPE_SIZE (gnu_type))
711 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
712 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
713 || !Is_Array_Type (Etype (gnat_entity)))
714 && !Present (Renamed_Object (gnat_entity))
715 && !Present (Address_Clause (gnat_entity)))
716 gnu_size = bitsize_unit_node;
718 /* If this is an object with no specified size and alignment, and
719 if either it is atomic or we are not optimizing alignment for
720 space and it is composite and not an exception, an Out parameter
721 or a reference to another object, and the size of its type is a
722 constant, set the alignment to the smallest one which is not
723 smaller than the size, with an appropriate cap. */
724 if (!gnu_size && align == 0
725 && (Is_Atomic (gnat_entity)
726 || (!Optimize_Alignment_Space (gnat_entity)
727 && kind != E_Exception
728 && kind != E_Out_Parameter
729 && Is_Composite_Type (Etype (gnat_entity))
730 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
731 && !imported_p
732 && No (Renamed_Object (gnat_entity))
733 && No (Address_Clause (gnat_entity))))
734 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
736 /* No point in jumping through all the hoops needed in order
737 to support BIGGEST_ALIGNMENT if we don't really have to.
738 So we cap to the smallest alignment that corresponds to
739 a known efficient memory access pattern of the target. */
740 unsigned int align_cap = Is_Atomic (gnat_entity)
741 ? BIGGEST_ALIGNMENT
742 : get_mode_alignment (ptr_mode);
744 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
745 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
746 align = align_cap;
747 else
748 align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
750 /* But make sure not to under-align the object. */
751 if (align <= TYPE_ALIGN (gnu_type))
752 align = 0;
754 /* And honor the minimum valid atomic alignment, if any. */
755 #ifdef MINIMUM_ATOMIC_ALIGNMENT
756 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
757 align = MINIMUM_ATOMIC_ALIGNMENT;
758 #endif
761 /* If the object is set to have atomic components, find the component
762 type and validate it.
764 ??? Note that we ignore Has_Volatile_Components on objects; it's
765 not at all clear what to do in that case. */
767 if (Has_Atomic_Components (gnat_entity))
769 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
770 ? TREE_TYPE (gnu_type) : gnu_type);
772 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
773 && TYPE_MULTI_ARRAY_P (gnu_inner))
774 gnu_inner = TREE_TYPE (gnu_inner);
776 check_ok_for_atomic (gnu_inner, gnat_entity, true);
779 /* Now check if the type of the object allows atomic access. Note
780 that we must test the type, even if this object has size and
781 alignment to allow such access, because we will be going
782 inside the padded record to assign to the object. We could fix
783 this by always copying via an intermediate value, but it's not
784 clear it's worth the effort. */
785 if (Is_Atomic (gnat_entity))
786 check_ok_for_atomic (gnu_type, gnat_entity, false);
788 /* If this is an aliased object with an unconstrained nominal subtype,
789 make a type that includes the template. */
790 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
791 && Is_Array_Type (Etype (gnat_entity))
792 && !type_annotate_only)
794 tree gnu_fat
795 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
797 gnu_type
798 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
799 concat_id_with_name (gnu_entity_id,
800 "UNC"));
803 #ifdef MINIMUM_ATOMIC_ALIGNMENT
804 /* If the size is a constant and no alignment is specified, force
805 the alignment to be the minimum valid atomic alignment. The
806 restriction on constant size avoids problems with variable-size
807 temporaries; if the size is variable, there's no issue with
808 atomic access. Also don't do this for a constant, since it isn't
809 necessary and can interfere with constant replacement. Finally,
810 do not do it for Out parameters since that creates an
811 size inconsistency with In parameters. */
812 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
813 && !FLOAT_TYPE_P (gnu_type)
814 && !const_flag && No (Renamed_Object (gnat_entity))
815 && !imported_p && No (Address_Clause (gnat_entity))
816 && kind != E_Out_Parameter
817 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
818 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
819 align = MINIMUM_ATOMIC_ALIGNMENT;
820 #endif
822 /* Make a new type with the desired size and alignment, if needed.
823 But do not take into account alignment promotions to compute the
824 size of the object. */
825 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
826 if (gnu_size || align > 0)
827 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
828 "PAD", false, definition,
829 gnu_size ? true : false);
831 /* If this is a renaming, avoid as much as possible to create a new
832 object. However, in several cases, creating it is required.
833 This processing needs to be applied to the raw expression so
834 as to make it more likely to rename the underlying object. */
835 if (Present (Renamed_Object (gnat_entity)))
837 bool create_normal_object = false;
839 /* If the renamed object had padding, strip off the reference
840 to the inner object and reset our type. */
841 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
842 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
843 == RECORD_TYPE
844 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
845 /* Strip useless conversions around the object. */
846 || TREE_CODE (gnu_expr) == NOP_EXPR)
848 gnu_expr = TREE_OPERAND (gnu_expr, 0);
849 gnu_type = TREE_TYPE (gnu_expr);
852 /* Case 1: If this is a constant renaming stemming from a function
853 call, treat it as a normal object whose initial value is what
854 is being renamed. RM 3.3 says that the result of evaluating a
855 function call is a constant object. As a consequence, it can
856 be the inner object of a constant renaming. In this case, the
857 renaming must be fully instantiated, i.e. it cannot be a mere
858 reference to (part of) an existing object. */
859 if (const_flag)
861 tree inner_object = gnu_expr;
862 while (handled_component_p (inner_object))
863 inner_object = TREE_OPERAND (inner_object, 0);
864 if (TREE_CODE (inner_object) == CALL_EXPR)
865 create_normal_object = true;
868 /* Otherwise, see if we can proceed with a stabilized version of
869 the renamed entity or if we need to make a new object. */
870 if (!create_normal_object)
872 tree maybe_stable_expr = NULL_TREE;
873 bool stable = false;
875 /* Case 2: If the renaming entity need not be materialized and
876 the renamed expression is something we can stabilize, use
877 that for the renaming. At the global level, we can only do
878 this if we know no SAVE_EXPRs need be made, because the
879 expression we return might be used in arbitrary conditional
880 branches so we must force the SAVE_EXPRs evaluation
881 immediately and this requires a function context. */
882 if (!Materialize_Entity (gnat_entity)
883 && (!global_bindings_p ()
884 || (staticp (gnu_expr)
885 && !TREE_SIDE_EFFECTS (gnu_expr))))
887 maybe_stable_expr
888 = maybe_stabilize_reference (gnu_expr, true, &stable);
890 if (stable)
892 gnu_decl = maybe_stable_expr;
893 /* ??? No DECL_EXPR is created so we need to mark
894 the expression manually lest it is shared. */
895 if (global_bindings_p ())
896 mark_visited (&gnu_decl);
897 save_gnu_tree (gnat_entity, gnu_decl, true);
898 saved = true;
899 break;
902 /* The stabilization failed. Keep maybe_stable_expr
903 untouched here to let the pointer case below know
904 about that failure. */
907 /* Case 3: If this is a constant renaming and creating a
908 new object is allowed and cheap, treat it as a normal
909 object whose initial value is what is being renamed. */
910 if (const_flag && Is_Elementary_Type (Etype (gnat_entity)))
913 /* Case 4: Make this into a constant pointer to the object we
914 are to rename and attach the object to the pointer if it is
915 something we can stabilize.
917 From the proper scope, attached objects will be referenced
918 directly instead of indirectly via the pointer to avoid
919 subtle aliasing problems with non-addressable entities.
920 They have to be stable because we must not evaluate the
921 variables in the expression every time the renaming is used.
922 The pointer is called a "renaming" pointer in this case.
924 In the rare cases where we cannot stabilize the renamed
925 object, we just make a "bare" pointer, and the renamed
926 entity is always accessed indirectly through it. */
927 else
929 gnu_type = build_reference_type (gnu_type);
930 inner_const_flag = TREE_READONLY (gnu_expr);
931 const_flag = true;
933 /* If the previous attempt at stabilizing failed, there
934 is no point in trying again and we reuse the result
935 without attaching it to the pointer. In this case it
936 will only be used as the initializing expression of
937 the pointer and thus needs no special treatment with
938 regard to multiple evaluations. */
939 if (maybe_stable_expr)
942 /* Otherwise, try to stabilize and attach the expression
943 to the pointer if the stabilization succeeds.
945 Note that this might introduce SAVE_EXPRs and we don't
946 check whether we're at the global level or not. This
947 is fine since we are building a pointer initializer and
948 neither the pointer nor the initializing expression can
949 be accessed before the pointer elaboration has taken
950 place in a correct program.
952 These SAVE_EXPRs will be evaluated at the right place
953 by either the evaluation of the initializer for the
954 non-global case or the elaboration code for the global
955 case, and will be attached to the elaboration procedure
956 in the latter case. */
957 else
959 maybe_stable_expr
960 = maybe_stabilize_reference (gnu_expr, true, &stable);
962 if (stable)
963 renamed_obj = maybe_stable_expr;
965 /* Attaching is actually performed downstream, as soon
966 as we have a VAR_DECL for the pointer we make. */
969 gnu_expr
970 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
972 gnu_size = NULL_TREE;
973 used_by_ref = true;
978 /* Make a volatile version of this object's type if we are to make
979 the object volatile. We also interpret 13.3(19) conservatively
980 and disallow any optimizations for an object covered by it. */
981 if ((Treat_As_Volatile (gnat_entity)
982 || (Is_Exported (gnat_entity)
983 /* Exclude exported constants created by the compiler,
984 which should boil down to static dispatch tables and
985 make it possible to put them in read-only memory. */
986 && (Comes_From_Source (gnat_entity) || !const_flag))
987 || Is_Imported (gnat_entity)
988 || Present (Address_Clause (gnat_entity)))
989 && !TYPE_VOLATILE (gnu_type))
990 gnu_type = build_qualified_type (gnu_type,
991 (TYPE_QUALS (gnu_type)
992 | TYPE_QUAL_VOLATILE));
994 /* If we are defining an aliased object whose nominal subtype is
995 unconstrained, the object is a record that contains both the
996 template and the object. If there is an initializer, it will
997 have already been converted to the right type, but we need to
998 create the template if there is no initializer. */
999 if (definition
1000 && !gnu_expr
1001 && TREE_CODE (gnu_type) == RECORD_TYPE
1002 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1003 /* Beware that padding might have been introduced
1004 via maybe_pad_type above. */
1005 || (TYPE_IS_PADDING_P (gnu_type)
1006 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1007 == RECORD_TYPE
1008 && TYPE_CONTAINS_TEMPLATE_P
1009 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1011 tree template_field
1012 = TYPE_IS_PADDING_P (gnu_type)
1013 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1014 : TYPE_FIELDS (gnu_type);
1016 gnu_expr
1017 = gnat_build_constructor
1018 (gnu_type,
1019 tree_cons
1020 (template_field,
1021 build_template (TREE_TYPE (template_field),
1022 TREE_TYPE (TREE_CHAIN (template_field)),
1023 NULL_TREE),
1024 NULL_TREE));
1027 /* Convert the expression to the type of the object except in the
1028 case where the object's type is unconstrained or the object's type
1029 is a padded record whose field is of self-referential size. In
1030 the former case, converting will generate unnecessary evaluations
1031 of the CONSTRUCTOR to compute the size and in the latter case, we
1032 want to only copy the actual data. */
1033 if (gnu_expr
1034 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1035 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1036 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1037 && TYPE_IS_PADDING_P (gnu_type)
1038 && (CONTAINS_PLACEHOLDER_P
1039 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1040 gnu_expr = convert (gnu_type, gnu_expr);
1042 /* If this is a pointer and it does not have an initializing
1043 expression, initialize it to NULL, unless the object is
1044 imported. */
1045 if (definition
1046 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
1047 && !Is_Imported (gnat_entity) && !gnu_expr)
1048 gnu_expr = integer_zero_node;
1050 /* If we are defining the object and it has an Address clause, we must
1051 either get the address expression from the saved GCC tree for the
1052 object if it has a Freeze node, or elaborate the address expression
1053 here since the front-end has guaranteed that the elaboration has no
1054 effects in this case. */
1055 if (definition && Present (Address_Clause (gnat_entity)))
1057 tree gnu_address
1058 = present_gnu_tree (gnat_entity)
1059 ? get_gnu_tree (gnat_entity)
1060 : gnat_to_gnu (Expression (Address_Clause (gnat_entity)));
1062 save_gnu_tree (gnat_entity, NULL_TREE, false);
1064 /* Ignore the size. It's either meaningless or was handled
1065 above. */
1066 gnu_size = NULL_TREE;
1067 /* Convert the type of the object to a reference type that can
1068 alias everything as per 13.3(19). */
1069 gnu_type
1070 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1071 gnu_address = convert (gnu_type, gnu_address);
1072 used_by_ref = true;
1073 const_flag = !Is_Public (gnat_entity)
1074 || compile_time_known_address_p (Expression (Address_Clause
1075 (gnat_entity)));
1077 /* If this is a deferred constant, the initializer is attached to
1078 the full view. */
1079 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1080 gnu_expr
1081 = gnat_to_gnu
1082 (Expression (Declaration_Node (Full_View (gnat_entity))));
1084 /* If we don't have an initializing expression for the underlying
1085 variable, the initializing expression for the pointer is the
1086 specified address. Otherwise, we have to make a COMPOUND_EXPR
1087 to assign both the address and the initial value. */
1088 if (!gnu_expr)
1089 gnu_expr = gnu_address;
1090 else
1091 gnu_expr
1092 = build2 (COMPOUND_EXPR, gnu_type,
1093 build_binary_op
1094 (MODIFY_EXPR, NULL_TREE,
1095 build_unary_op (INDIRECT_REF, NULL_TREE,
1096 gnu_address),
1097 gnu_expr),
1098 gnu_address);
1101 /* If it has an address clause and we are not defining it, mark it
1102 as an indirect object. Likewise for Stdcall objects that are
1103 imported. */
1104 if ((!definition && Present (Address_Clause (gnat_entity)))
1105 || (Is_Imported (gnat_entity)
1106 && Has_Stdcall_Convention (gnat_entity)))
1108 /* Convert the type of the object to a reference type that can
1109 alias everything as per 13.3(19). */
1110 gnu_type
1111 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1112 gnu_size = NULL_TREE;
1114 /* No point in taking the address of an initializing expression
1115 that isn't going to be used. */
1116 gnu_expr = NULL_TREE;
1118 /* If it has an address clause whose value is known at compile
1119 time, make the object a CONST_DECL. This will avoid a
1120 useless dereference. */
1121 if (Present (Address_Clause (gnat_entity)))
1123 Node_Id gnat_address
1124 = Expression (Address_Clause (gnat_entity));
1126 if (compile_time_known_address_p (gnat_address))
1128 gnu_expr = gnat_to_gnu (gnat_address);
1129 const_flag = true;
1133 used_by_ref = true;
1136 /* If we are at top level and this object is of variable size,
1137 make the actual type a hidden pointer to the real type and
1138 make the initializer be a memory allocation and initialization.
1139 Likewise for objects we aren't defining (presumed to be
1140 external references from other packages), but there we do
1141 not set up an initialization.
1143 If the object's size overflows, make an allocator too, so that
1144 Storage_Error gets raised. Note that we will never free
1145 such memory, so we presume it never will get allocated. */
1147 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1148 global_bindings_p () || !definition
1149 || static_p)
1150 || (gnu_size
1151 && ! allocatable_size_p (gnu_size,
1152 global_bindings_p () || !definition
1153 || static_p)))
1155 gnu_type = build_reference_type (gnu_type);
1156 gnu_size = NULL_TREE;
1157 used_by_ref = true;
1158 const_flag = true;
1160 /* In case this was a aliased object whose nominal subtype is
1161 unconstrained, the pointer above will be a thin pointer and
1162 build_allocator will automatically make the template.
1164 If we have a template initializer only (that we made above),
1165 pretend there is none and rely on what build_allocator creates
1166 again anyway. Otherwise (if we have a full initializer), get
1167 the data part and feed that to build_allocator.
1169 If we are elaborating a mutable object, tell build_allocator to
1170 ignore a possibly simpler size from the initializer, if any, as
1171 we must allocate the maximum possible size in this case. */
1173 if (definition)
1175 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1177 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1178 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1180 gnu_alloc_type
1181 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1183 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1184 && 1 == VEC_length (constructor_elt,
1185 CONSTRUCTOR_ELTS (gnu_expr)))
1186 gnu_expr = 0;
1187 else
1188 gnu_expr
1189 = build_component_ref
1190 (gnu_expr, NULL_TREE,
1191 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1192 false);
1195 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1196 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1197 && !Is_Imported (gnat_entity))
1198 post_error ("?Storage_Error will be raised at run-time!",
1199 gnat_entity);
1201 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1202 0, 0, gnat_entity, mutable_p);
1204 else
1206 gnu_expr = NULL_TREE;
1207 const_flag = false;
1211 /* If this object would go into the stack and has an alignment larger
1212 than the largest stack alignment the back-end can honor, resort to
1213 a variable of "aligning type". */
1214 if (!global_bindings_p () && !static_p && definition
1215 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1217 /* Create the new variable. No need for extra room before the
1218 aligned field as this is in automatic storage. */
1219 tree gnu_new_type
1220 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1221 TYPE_SIZE_UNIT (gnu_type),
1222 BIGGEST_ALIGNMENT, 0);
1223 tree gnu_new_var
1224 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1225 NULL_TREE, gnu_new_type, NULL_TREE, false,
1226 false, false, false, NULL, gnat_entity);
1228 /* Initialize the aligned field if we have an initializer. */
1229 if (gnu_expr)
1230 add_stmt_with_node
1231 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1232 build_component_ref
1233 (gnu_new_var, NULL_TREE,
1234 TYPE_FIELDS (gnu_new_type), false),
1235 gnu_expr),
1236 gnat_entity);
1238 /* And setup this entity as a reference to the aligned field. */
1239 gnu_type = build_reference_type (gnu_type);
1240 gnu_expr
1241 = build_unary_op
1242 (ADDR_EXPR, gnu_type,
1243 build_component_ref (gnu_new_var, NULL_TREE,
1244 TYPE_FIELDS (gnu_new_type), false));
1246 gnu_size = NULL_TREE;
1247 used_by_ref = true;
1248 const_flag = true;
1251 if (const_flag)
1252 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1253 | TYPE_QUAL_CONST));
1255 /* Convert the expression to the type of the object except in the
1256 case where the object's type is unconstrained or the object's type
1257 is a padded record whose field is of self-referential size. In
1258 the former case, converting will generate unnecessary evaluations
1259 of the CONSTRUCTOR to compute the size and in the latter case, we
1260 want to only copy the actual data. */
1261 if (gnu_expr
1262 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1263 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1264 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1265 && TYPE_IS_PADDING_P (gnu_type)
1266 && (CONTAINS_PLACEHOLDER_P
1267 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1268 gnu_expr = convert (gnu_type, gnu_expr);
1270 /* If this name is external or there was a name specified, use it,
1271 unless this is a VMS exception object since this would conflict
1272 with the symbol we need to export in addition. Don't use the
1273 Interface_Name if there is an address clause (see CD30005). */
1274 if (!Is_VMS_Exception (gnat_entity)
1275 && ((Present (Interface_Name (gnat_entity))
1276 && No (Address_Clause (gnat_entity)))
1277 || (Is_Public (gnat_entity)
1278 && (!Is_Imported (gnat_entity)
1279 || Is_Exported (gnat_entity)))))
1280 gnu_ext_name = create_concat_name (gnat_entity, 0);
1282 /* If this is constant initialized to a static constant and the
1283 object has an aggregate type, force it to be statically
1284 allocated. This will avoid an initialization copy. */
1285 if (!static_p && const_flag
1286 && gnu_expr && TREE_CONSTANT (gnu_expr)
1287 && AGGREGATE_TYPE_P (gnu_type)
1288 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1289 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1290 && TYPE_IS_PADDING_P (gnu_type)
1291 && !host_integerp (TYPE_SIZE_UNIT
1292 (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
1293 static_p = true;
1295 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1296 gnu_expr, const_flag,
1297 Is_Public (gnat_entity),
1298 imported_p || !definition,
1299 static_p, attr_list, gnat_entity);
1300 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1301 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1302 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1304 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1305 if (global_bindings_p ())
1307 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1308 record_global_renaming_pointer (gnu_decl);
1312 if (definition && DECL_SIZE_UNIT (gnu_decl)
1313 && get_block_jmpbuf_decl ()
1314 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1315 || (flag_stack_check == GENERIC_STACK_CHECK
1316 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1317 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1318 add_stmt_with_node (build_call_1_expr
1319 (update_setjmp_buf_decl,
1320 build_unary_op (ADDR_EXPR, NULL_TREE,
1321 get_block_jmpbuf_decl ())),
1322 gnat_entity);
1324 /* If we are defining an Out parameter and we're not optimizing,
1325 create a fake PARM_DECL for debugging purposes and make it
1326 point to the VAR_DECL. Suppress debug info for the latter
1327 but make sure it will still live on the stack so it can be
1328 accessed from within the debugger through the PARM_DECL. */
1329 if (kind == E_Out_Parameter && definition && !optimize)
1331 tree param = create_param_decl (gnu_entity_id, gnu_type, false);
1332 gnat_pushdecl (param, gnat_entity);
1333 SET_DECL_VALUE_EXPR (param, gnu_decl);
1334 DECL_HAS_VALUE_EXPR_P (param) = 1;
1335 if (debug_info_p)
1336 debug_info_p = false;
1337 else
1338 DECL_IGNORED_P (param) = 1;
1339 TREE_ADDRESSABLE (gnu_decl) = 1;
1342 /* If this is a public constant or we're not optimizing and we're not
1343 making a VAR_DECL for it, make one just for export or debugger use.
1344 Likewise if the address is taken or if either the object or type is
1345 aliased. Make an external declaration for a reference, unless this
1346 is a Standard entity since there no real symbol at the object level
1347 for these. */
1348 if (TREE_CODE (gnu_decl) == CONST_DECL
1349 && (definition || Sloc (gnat_entity) > Standard_Location)
1350 && ((Is_Public (gnat_entity)
1351 && !Present (Address_Clause (gnat_entity)))
1352 || !optimize
1353 || Address_Taken (gnat_entity)
1354 || Is_Aliased (gnat_entity)
1355 || Is_Aliased (Etype (gnat_entity))))
1357 tree gnu_corr_var
1358 = create_true_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1359 gnu_expr, true, Is_Public (gnat_entity),
1360 !definition, static_p, NULL,
1361 gnat_entity);
1363 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1365 /* As debugging information will be generated for the variable,
1366 do not generate information for the constant. */
1367 DECL_IGNORED_P (gnu_decl) = 1;
1370 /* If this is declared in a block that contains a block with an
1371 exception handler, we must force this variable in memory to
1372 suppress an invalid optimization. */
1373 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1374 && Exception_Mechanism != Back_End_Exceptions)
1375 TREE_ADDRESSABLE (gnu_decl) = 1;
1377 gnu_type = TREE_TYPE (gnu_decl);
1379 /* Back-annotate Alignment and Esize of the object if not already
1380 known, except for when the object is actually a pointer to the
1381 real object, since alignment and size of a pointer don't have
1382 anything to do with those of the designated object. Note that
1383 we pick the values of the type, not those of the object, to
1384 shield ourselves from low-level platform-dependent adjustments
1385 like alignment promotion. This is both consistent with all the
1386 treatment above, where alignment and size are set on the type of
1387 the object and not on the object directly, and makes it possible
1388 to support confirming representation clauses in all cases. */
1390 if (!used_by_ref && Unknown_Alignment (gnat_entity))
1391 Set_Alignment (gnat_entity,
1392 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
1394 if (!used_by_ref && Unknown_Esize (gnat_entity))
1396 if (TREE_CODE (gnu_type) == RECORD_TYPE
1397 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1398 gnu_object_size
1399 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type))));
1401 Set_Esize (gnat_entity, annotate_value (gnu_object_size));
1404 break;
1406 case E_Void:
1407 /* Return a TYPE_DECL for "void" that we previously made. */
1408 gnu_decl = void_type_decl_node;
1409 break;
1411 case E_Enumeration_Type:
1412 /* A special case, for the types Character and Wide_Character in
1413 Standard, we do not list all the literals. So if the literals
1414 are not specified, make this an unsigned type. */
1415 if (No (First_Literal (gnat_entity)))
1417 gnu_type = make_unsigned_type (esize);
1418 TYPE_NAME (gnu_type) = gnu_entity_id;
1420 /* Set TYPE_STRING_FLAG for Ada Character and Wide_Character types.
1421 This is needed by the DWARF-2 back-end to distinguish between
1422 unsigned integer types and character types. */
1423 TYPE_STRING_FLAG (gnu_type) = 1;
1424 break;
1427 /* Normal case of non-character type, or non-Standard character type */
1429 /* Here we have a list of enumeral constants in First_Literal.
1430 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1431 the list to be places into TYPE_FIELDS. Each node in the list
1432 is a TREE_LIST node whose TREE_VALUE is the literal name
1433 and whose TREE_PURPOSE is the value of the literal.
1435 Esize contains the number of bits needed to represent the enumeral
1436 type, Type_Low_Bound also points to the first literal and
1437 Type_High_Bound points to the last literal. */
1439 Entity_Id gnat_literal;
1440 tree gnu_literal_list = NULL_TREE;
1442 if (Is_Unsigned_Type (gnat_entity))
1443 gnu_type = make_unsigned_type (esize);
1444 else
1445 gnu_type = make_signed_type (esize);
1447 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1449 for (gnat_literal = First_Literal (gnat_entity);
1450 Present (gnat_literal);
1451 gnat_literal = Next_Literal (gnat_literal))
1453 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1454 gnu_type);
1455 tree gnu_literal
1456 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1457 gnu_type, gnu_value, true, false, false,
1458 false, NULL, gnat_literal);
1460 save_gnu_tree (gnat_literal, gnu_literal, false);
1461 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1462 gnu_value, gnu_literal_list);
1465 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1467 /* Note that the bounds are updated at the end of this function
1468 because to avoid an infinite recursion when we get the bounds of
1469 this type, since those bounds are objects of this type. */
1471 break;
1473 case E_Signed_Integer_Type:
1474 case E_Ordinary_Fixed_Point_Type:
1475 case E_Decimal_Fixed_Point_Type:
1476 /* For integer types, just make a signed type the appropriate number
1477 of bits. */
1478 gnu_type = make_signed_type (esize);
1479 break;
1481 case E_Modular_Integer_Type:
1482 /* For modular types, make the unsigned type of the proper number of
1483 bits and then set up the modulus, if required. */
1485 enum machine_mode mode;
1486 tree gnu_modulus;
1487 tree gnu_high = 0;
1489 if (Is_Packed_Array_Type (gnat_entity))
1490 esize = UI_To_Int (RM_Size (gnat_entity));
1492 /* Find the smallest mode at least ESIZE bits wide and make a class
1493 using that mode. */
1495 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1496 GET_MODE_BITSIZE (mode) < esize;
1497 mode = GET_MODE_WIDER_MODE (mode))
1500 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1501 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1502 = (Is_Packed_Array_Type (gnat_entity)
1503 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
1505 /* Get the modulus in this type. If it overflows, assume it is because
1506 it is equal to 2**Esize. Note that there is no overflow checking
1507 done on unsigned type, so we detect the overflow by looking for
1508 a modulus of zero, which is otherwise invalid. */
1509 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1511 if (!integer_zerop (gnu_modulus))
1513 TYPE_MODULAR_P (gnu_type) = 1;
1514 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1515 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1516 convert (gnu_type, integer_one_node));
1519 /* If we have to set TYPE_PRECISION different from its natural value,
1520 make a subtype to do do. Likewise if there is a modulus and
1521 it is not one greater than TYPE_MAX_VALUE. */
1522 if (TYPE_PRECISION (gnu_type) != esize
1523 || (TYPE_MODULAR_P (gnu_type)
1524 && !tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1526 tree gnu_subtype = make_node (INTEGER_TYPE);
1528 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1529 TREE_TYPE (gnu_subtype) = gnu_type;
1530 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1531 TYPE_MAX_VALUE (gnu_subtype)
1532 = TYPE_MODULAR_P (gnu_type)
1533 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1534 TYPE_PRECISION (gnu_subtype) = esize;
1535 TYPE_UNSIGNED (gnu_subtype) = 1;
1536 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1537 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1538 = (Is_Packed_Array_Type (gnat_entity)
1539 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
1540 layout_type (gnu_subtype);
1542 gnu_type = gnu_subtype;
1545 break;
1547 case E_Signed_Integer_Subtype:
1548 case E_Enumeration_Subtype:
1549 case E_Modular_Integer_Subtype:
1550 case E_Ordinary_Fixed_Point_Subtype:
1551 case E_Decimal_Fixed_Point_Subtype:
1553 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1554 that we do not want to call build_range_type since we would
1555 like each subtype node to be distinct. This will be important
1556 when memory aliasing is implemented.
1558 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1559 parent type; this fact is used by the arithmetic conversion
1560 functions.
1562 We elaborate the Ancestor_Subtype if it is not in the current
1563 unit and one of our bounds is non-static. We do this to ensure
1564 consistent naming in the case where several subtypes share the same
1565 bounds by always elaborating the first such subtype first, thus
1566 using its name. */
1568 if (!definition
1569 && Present (Ancestor_Subtype (gnat_entity))
1570 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1571 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1572 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1573 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1574 gnu_expr, 0);
1576 gnu_type = make_node (INTEGER_TYPE);
1577 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1579 /* Set the precision to the Esize except for bit-packed arrays and
1580 subtypes of Standard.Boolean. */
1581 if (Is_Packed_Array_Type (gnat_entity)
1582 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1584 esize = UI_To_Int (RM_Size (gnat_entity));
1585 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1587 else if (TREE_CODE (TREE_TYPE (gnu_type)) == BOOLEAN_TYPE)
1588 esize = 1;
1590 TYPE_PRECISION (gnu_type) = esize;
1592 TYPE_MIN_VALUE (gnu_type)
1593 = convert (TREE_TYPE (gnu_type),
1594 elaborate_expression (Type_Low_Bound (gnat_entity),
1595 gnat_entity,
1596 get_identifier ("L"), definition, 1,
1597 Needs_Debug_Info (gnat_entity)));
1599 TYPE_MAX_VALUE (gnu_type)
1600 = convert (TREE_TYPE (gnu_type),
1601 elaborate_expression (Type_High_Bound (gnat_entity),
1602 gnat_entity,
1603 get_identifier ("U"), definition, 1,
1604 Needs_Debug_Info (gnat_entity)));
1606 /* One of the above calls might have caused us to be elaborated,
1607 so don't blow up if so. */
1608 if (present_gnu_tree (gnat_entity))
1610 maybe_present = true;
1611 break;
1614 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1615 = Has_Biased_Representation (gnat_entity);
1617 /* This should be an unsigned type if the lower bound is constant
1618 and non-negative or if the base type is unsigned; a signed type
1619 otherwise. */
1620 TYPE_UNSIGNED (gnu_type)
1621 = (TYPE_UNSIGNED (TREE_TYPE (gnu_type))
1622 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1623 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1624 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1625 || Is_Unsigned_Type (gnat_entity));
1627 layout_type (gnu_type);
1629 /* Inherit our alias set from what we're a subtype of. Subtypes
1630 are not different types and a pointer can designate any instance
1631 within a subtype hierarchy. */
1632 copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1634 /* If the type we are dealing with is to represent a packed array,
1635 we need to have the bits left justified on big-endian targets
1636 and right justified on little-endian targets. We also need to
1637 ensure that when the value is read (e.g. for comparison of two
1638 such values), we only get the good bits, since the unused bits
1639 are uninitialized. Both goals are accomplished by wrapping the
1640 modular value in an enclosing struct. */
1641 if (Is_Packed_Array_Type (gnat_entity)
1642 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1644 tree gnu_field_type = gnu_type;
1645 tree gnu_field;
1647 TYPE_RM_SIZE_NUM (gnu_field_type)
1648 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1649 gnu_type = make_node (RECORD_TYPE);
1650 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1652 /* Propagate the alignment of the modular type to the record.
1653 This means that bitpacked arrays have "ceil" alignment for
1654 their size, which may seem counter-intuitive but makes it
1655 possible to easily overlay them on modular types. */
1656 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1657 TYPE_PACKED (gnu_type) = 1;
1659 /* Create a stripped-down declaration of the original type, mainly
1660 for debugging. */
1661 create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1662 NULL, true, debug_info_p, gnat_entity);
1664 /* Don't notify the field as "addressable", since we won't be taking
1665 it's address and it would prevent create_field_decl from making a
1666 bitfield. */
1667 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1668 gnu_field_type, gnu_type, 1, 0, 0, 0);
1670 finish_record_type (gnu_type, gnu_field, 0, false);
1671 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1672 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1674 copy_alias_set (gnu_type, gnu_field_type);
1677 /* If the type we are dealing with has got a smaller alignment than the
1678 natural one, we need to wrap it up in a record type and under-align
1679 the latter. We reuse the padding machinery for this purpose. */
1680 else if (Known_Alignment (gnat_entity)
1681 && UI_Is_In_Int_Range (Alignment (gnat_entity))
1682 && (align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT)
1683 && align < TYPE_ALIGN (gnu_type))
1685 tree gnu_field_type = gnu_type;
1686 tree gnu_field;
1688 gnu_type = make_node (RECORD_TYPE);
1689 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1691 TYPE_ALIGN (gnu_type) = align;
1692 TYPE_PACKED (gnu_type) = 1;
1694 /* Create a stripped-down declaration of the original type, mainly
1695 for debugging. */
1696 create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1697 NULL, true, debug_info_p, gnat_entity);
1699 /* Don't notify the field as "addressable", since we won't be taking
1700 it's address and it would prevent create_field_decl from making a
1701 bitfield. */
1702 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1703 gnu_field_type, gnu_type, 1, 0, 0, 0);
1705 finish_record_type (gnu_type, gnu_field, 0, false);
1706 TYPE_IS_PADDING_P (gnu_type) = 1;
1707 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1709 copy_alias_set (gnu_type, gnu_field_type);
1712 /* Otherwise reset the alignment lest we computed it above. */
1713 else
1714 align = 0;
1716 break;
1718 case E_Floating_Point_Type:
1719 /* If this is a VAX floating-point type, use an integer of the proper
1720 size. All the operations will be handled with ASM statements. */
1721 if (Vax_Float (gnat_entity))
1723 gnu_type = make_signed_type (esize);
1724 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1725 SET_TYPE_DIGITS_VALUE (gnu_type,
1726 UI_To_gnu (Digits_Value (gnat_entity),
1727 sizetype));
1728 break;
1731 /* The type of the Low and High bounds can be our type if this is
1732 a type from Standard, so set them at the end of the function. */
1733 gnu_type = make_node (REAL_TYPE);
1734 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1735 layout_type (gnu_type);
1736 break;
1738 case E_Floating_Point_Subtype:
1739 if (Vax_Float (gnat_entity))
1741 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1742 break;
1746 if (!definition
1747 && Present (Ancestor_Subtype (gnat_entity))
1748 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1749 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1750 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1751 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1752 gnu_expr, 0);
1754 gnu_type = make_node (REAL_TYPE);
1755 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1756 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1758 TYPE_MIN_VALUE (gnu_type)
1759 = convert (TREE_TYPE (gnu_type),
1760 elaborate_expression (Type_Low_Bound (gnat_entity),
1761 gnat_entity, get_identifier ("L"),
1762 definition, 1,
1763 Needs_Debug_Info (gnat_entity)));
1765 TYPE_MAX_VALUE (gnu_type)
1766 = convert (TREE_TYPE (gnu_type),
1767 elaborate_expression (Type_High_Bound (gnat_entity),
1768 gnat_entity, get_identifier ("U"),
1769 definition, 1,
1770 Needs_Debug_Info (gnat_entity)));
1772 /* One of the above calls might have caused us to be elaborated,
1773 so don't blow up if so. */
1774 if (present_gnu_tree (gnat_entity))
1776 maybe_present = true;
1777 break;
1780 layout_type (gnu_type);
1782 /* Inherit our alias set from what we're a subtype of, as for
1783 integer subtypes. */
1784 copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1786 break;
1788 /* Array and String Types and Subtypes
1790 Unconstrained array types are represented by E_Array_Type and
1791 constrained array types are represented by E_Array_Subtype. There
1792 are no actual objects of an unconstrained array type; all we have
1793 are pointers to that type.
1795 The following fields are defined on array types and subtypes:
1797 Component_Type Component type of the array.
1798 Number_Dimensions Number of dimensions (an int).
1799 First_Index Type of first index. */
1801 case E_String_Type:
1802 case E_Array_Type:
1804 tree gnu_template_fields = NULL_TREE;
1805 tree gnu_template_type = make_node (RECORD_TYPE);
1806 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1807 tree gnu_fat_type = make_node (RECORD_TYPE);
1808 int ndim = Number_Dimensions (gnat_entity);
1809 int firstdim
1810 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1811 int nextdim
1812 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1813 int index;
1814 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1815 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1816 tree gnu_comp_size = 0;
1817 tree gnu_max_size = size_one_node;
1818 tree gnu_max_size_unit;
1819 Entity_Id gnat_ind_subtype;
1820 Entity_Id gnat_ind_base_subtype;
1821 tree gnu_template_reference;
1822 tree tem;
1824 TYPE_NAME (gnu_template_type)
1825 = create_concat_name (gnat_entity, "XUB");
1827 /* Make a node for the array. If we are not defining the array
1828 suppress expanding incomplete types. */
1829 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1831 if (!definition)
1832 defer_incomplete_level++, this_deferred = true;
1834 /* Build the fat pointer type. Use a "void *" object instead of
1835 a pointer to the array type since we don't have the array type
1836 yet (it will reference the fat pointer via the bounds). */
1837 tem = chainon (chainon (NULL_TREE,
1838 create_field_decl (get_identifier ("P_ARRAY"),
1839 ptr_void_type_node,
1840 gnu_fat_type, 0, 0, 0, 0)),
1841 create_field_decl (get_identifier ("P_BOUNDS"),
1842 gnu_ptr_template,
1843 gnu_fat_type, 0, 0, 0, 0));
1845 /* Make sure we can put this into a register. */
1846 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1848 /* Do not finalize this record type since the types of its fields
1849 are still incomplete at this point. */
1850 finish_record_type (gnu_fat_type, tem, 0, true);
1851 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1853 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1854 is the fat pointer. This will be used to access the individual
1855 fields once we build them. */
1856 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1857 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1858 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1859 gnu_template_reference
1860 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1861 TREE_READONLY (gnu_template_reference) = 1;
1863 /* Now create the GCC type for each index and add the fields for
1864 that index to the template. */
1865 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1866 gnat_ind_base_subtype
1867 = First_Index (Implementation_Base_Type (gnat_entity));
1868 index < ndim && index >= 0;
1869 index += nextdim,
1870 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1871 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1873 char field_name[10];
1874 tree gnu_ind_subtype
1875 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1876 tree gnu_base_subtype
1877 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1878 tree gnu_base_min
1879 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1880 tree gnu_base_max
1881 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1882 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1884 /* Make the FIELD_DECLs for the minimum and maximum of this
1885 type and then make extractions of that field from the
1886 template. */
1887 sprintf (field_name, "LB%d", index);
1888 gnu_min_field = create_field_decl (get_identifier (field_name),
1889 gnu_ind_subtype,
1890 gnu_template_type, 0, 0, 0, 0);
1891 field_name[0] = 'U';
1892 gnu_max_field = create_field_decl (get_identifier (field_name),
1893 gnu_ind_subtype,
1894 gnu_template_type, 0, 0, 0, 0);
1896 Sloc_to_locus (Sloc (gnat_entity),
1897 &DECL_SOURCE_LOCATION (gnu_min_field));
1898 Sloc_to_locus (Sloc (gnat_entity),
1899 &DECL_SOURCE_LOCATION (gnu_max_field));
1900 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1902 /* We can't use build_component_ref here since the template
1903 type isn't complete yet. */
1904 gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1905 gnu_template_reference, gnu_min_field,
1906 NULL_TREE);
1907 gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1908 gnu_template_reference, gnu_max_field,
1909 NULL_TREE);
1910 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1912 /* Make a range type with the new ranges, but using
1913 the Ada subtype. Then we convert to sizetype. */
1914 gnu_index_types[index]
1915 = create_index_type (convert (sizetype, gnu_min),
1916 convert (sizetype, gnu_max),
1917 build_range_type (gnu_ind_subtype,
1918 gnu_min, gnu_max),
1919 gnat_entity);
1920 /* Update the maximum size of the array, in elements. */
1921 gnu_max_size
1922 = size_binop (MULT_EXPR, gnu_max_size,
1923 size_binop (PLUS_EXPR, size_one_node,
1924 size_binop (MINUS_EXPR, gnu_base_max,
1925 gnu_base_min)));
1927 TYPE_NAME (gnu_index_types[index])
1928 = create_concat_name (gnat_entity, field_name);
1931 for (index = 0; index < ndim; index++)
1932 gnu_template_fields
1933 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1935 /* Install all the fields into the template. */
1936 finish_record_type (gnu_template_type, gnu_template_fields, 0, false);
1937 TYPE_READONLY (gnu_template_type) = 1;
1939 /* Now make the array of arrays and update the pointer to the array
1940 in the fat pointer. Note that it is the first field. */
1941 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1943 /* Try to get a smaller form of the component if needed. */
1944 if ((Is_Packed (gnat_entity)
1945 || Has_Component_Size_Clause (gnat_entity))
1946 && !Is_Bit_Packed_Array (gnat_entity)
1947 && !Has_Aliased_Components (gnat_entity)
1948 && !Strict_Alignment (Component_Type (gnat_entity))
1949 && TREE_CODE (tem) == RECORD_TYPE
1950 && host_integerp (TYPE_SIZE (tem), 1))
1951 tem = make_packable_type (tem, false);
1953 if (Has_Atomic_Components (gnat_entity))
1954 check_ok_for_atomic (tem, gnat_entity, true);
1956 /* Get and validate any specified Component_Size, but if Packed,
1957 ignore it since the front end will have taken care of it. */
1958 gnu_comp_size
1959 = validate_size (Component_Size (gnat_entity), tem,
1960 gnat_entity,
1961 (Is_Bit_Packed_Array (gnat_entity)
1962 ? TYPE_DECL : VAR_DECL),
1963 true, Has_Component_Size_Clause (gnat_entity));
1965 /* If the component type is a RECORD_TYPE that has a self-referential
1966 size, use the maximum size. */
1967 if (!gnu_comp_size && TREE_CODE (tem) == RECORD_TYPE
1968 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1969 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1971 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
1973 tree orig_tem;
1974 tem = make_type_from_size (tem, gnu_comp_size, false);
1975 orig_tem = tem;
1976 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1977 "C_PAD", false, definition, true);
1978 /* If a padding record was made, declare it now since it will
1979 never be declared otherwise. This is necessary to ensure
1980 that its subtrees are properly marked. */
1981 if (tem != orig_tem)
1982 create_type_decl (TYPE_NAME (tem), tem, NULL, true,
1983 debug_info_p, gnat_entity);
1986 if (Has_Volatile_Components (gnat_entity))
1987 tem = build_qualified_type (tem,
1988 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1990 /* If Component_Size is not already specified, annotate it with the
1991 size of the component. */
1992 if (Unknown_Component_Size (gnat_entity))
1993 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1995 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1996 size_binop (MULT_EXPR, gnu_max_size,
1997 TYPE_SIZE_UNIT (tem)));
1998 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1999 size_binop (MULT_EXPR,
2000 convert (bitsizetype,
2001 gnu_max_size),
2002 TYPE_SIZE (tem)));
2004 for (index = ndim - 1; index >= 0; index--)
2006 tem = build_array_type (tem, gnu_index_types[index]);
2007 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2008 if (array_type_has_nonaliased_component (gnat_entity, tem))
2009 TYPE_NONALIASED_COMPONENT (tem) = 1;
2012 /* If an alignment is specified, use it if valid. But ignore it for
2013 types that represent the unpacked base type for packed arrays. If
2014 the alignment was requested with an explicit user alignment clause,
2015 state so. */
2016 if (No (Packed_Array_Type (gnat_entity))
2017 && Known_Alignment (gnat_entity))
2019 gcc_assert (Present (Alignment (gnat_entity)));
2020 TYPE_ALIGN (tem)
2021 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2022 TYPE_ALIGN (tem));
2023 if (Present (Alignment_Clause (gnat_entity)))
2024 TYPE_USER_ALIGN (tem) = 1;
2027 TYPE_CONVENTION_FORTRAN_P (tem)
2028 = (Convention (gnat_entity) == Convention_Fortran);
2029 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2031 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2032 corresponding fat pointer. */
2033 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
2034 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2035 TYPE_MODE (gnu_type) = BLKmode;
2036 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2037 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2039 /* If the maximum size doesn't overflow, use it. */
2040 if (TREE_CODE (gnu_max_size) == INTEGER_CST
2041 && !TREE_OVERFLOW (gnu_max_size))
2042 TYPE_SIZE (tem)
2043 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
2044 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2045 && !TREE_OVERFLOW (gnu_max_size_unit))
2046 TYPE_SIZE_UNIT (tem)
2047 = size_binop (MIN_EXPR, gnu_max_size_unit,
2048 TYPE_SIZE_UNIT (tem));
2050 create_type_decl (create_concat_name (gnat_entity, "XUA"),
2051 tem, NULL, !Comes_From_Source (gnat_entity),
2052 debug_info_p, gnat_entity);
2054 /* Give the fat pointer type a name. */
2055 create_type_decl (create_concat_name (gnat_entity, "XUP"),
2056 gnu_fat_type, NULL, !Comes_From_Source (gnat_entity),
2057 debug_info_p, gnat_entity);
2059 /* Create the type to be used as what a thin pointer designates: an
2060 record type for the object and its template with the field offsets
2061 shifted to have the template at a negative offset. */
2062 tem = build_unc_object_type (gnu_template_type, tem,
2063 create_concat_name (gnat_entity, "XUT"));
2064 shift_unc_components_for_thin_pointers (tem);
2066 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2067 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2069 /* Give the thin pointer type a name. */
2070 create_type_decl (create_concat_name (gnat_entity, "XUX"),
2071 build_pointer_type (tem), NULL,
2072 !Comes_From_Source (gnat_entity), debug_info_p,
2073 gnat_entity);
2075 break;
2077 case E_String_Subtype:
2078 case E_Array_Subtype:
2080 /* This is the actual data type for array variables. Multidimensional
2081 arrays are implemented in the gnu tree as arrays of arrays. Note
2082 that for the moment arrays which have sparse enumeration subtypes as
2083 index components create sparse arrays, which is obviously space
2084 inefficient but so much easier to code for now.
2086 Also note that the subtype never refers to the unconstrained
2087 array type, which is somewhat at variance with Ada semantics.
2089 First check to see if this is simply a renaming of the array
2090 type. If so, the result is the array type. */
2092 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2093 if (!Is_Constrained (gnat_entity))
2094 break;
2095 else
2097 int index;
2098 int array_dim = Number_Dimensions (gnat_entity);
2099 int first_dim
2100 = ((Convention (gnat_entity) == Convention_Fortran)
2101 ? array_dim - 1 : 0);
2102 int next_dim
2103 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
2104 Entity_Id gnat_ind_subtype;
2105 Entity_Id gnat_ind_base_subtype;
2106 tree gnu_base_type = gnu_type;
2107 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
2108 tree gnu_comp_size = NULL_TREE;
2109 tree gnu_max_size = size_one_node;
2110 tree gnu_max_size_unit;
2111 bool need_index_type_struct = false;
2112 bool max_overflow = false;
2114 /* First create the gnu types for each index. Create types for
2115 debugging information to point to the index types if the
2116 are not integer types, have variable bounds, or are
2117 wider than sizetype. */
2119 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
2120 gnat_ind_base_subtype
2121 = First_Index (Implementation_Base_Type (gnat_entity));
2122 index < array_dim && index >= 0;
2123 index += next_dim,
2124 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
2125 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
2127 tree gnu_index_subtype
2128 = get_unpadded_type (Etype (gnat_ind_subtype));
2129 tree gnu_min
2130 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
2131 tree gnu_max
2132 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
2133 tree gnu_base_subtype
2134 = get_unpadded_type (Etype (gnat_ind_base_subtype));
2135 tree gnu_base_min
2136 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
2137 tree gnu_base_max
2138 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
2139 tree gnu_base_type = get_base_type (gnu_base_subtype);
2140 tree gnu_base_base_min
2141 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
2142 tree gnu_base_base_max
2143 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
2144 tree gnu_high;
2145 tree gnu_this_max;
2147 /* If the minimum and maximum values both overflow in
2148 SIZETYPE, but the difference in the original type
2149 does not overflow in SIZETYPE, ignore the overflow
2150 indications. */
2151 if ((TYPE_PRECISION (gnu_index_subtype)
2152 > TYPE_PRECISION (sizetype)
2153 || TYPE_UNSIGNED (gnu_index_subtype)
2154 != TYPE_UNSIGNED (sizetype))
2155 && TREE_CODE (gnu_min) == INTEGER_CST
2156 && TREE_CODE (gnu_max) == INTEGER_CST
2157 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2158 && (!TREE_OVERFLOW
2159 (fold_build2 (MINUS_EXPR, gnu_index_subtype,
2160 TYPE_MAX_VALUE (gnu_index_subtype),
2161 TYPE_MIN_VALUE (gnu_index_subtype)))))
2163 TREE_OVERFLOW (gnu_min) = 0;
2164 TREE_OVERFLOW (gnu_max) = 0;
2167 /* Similarly, if the range is null, use bounds of 1..0 for
2168 the sizetype bounds. */
2169 else if ((TYPE_PRECISION (gnu_index_subtype)
2170 > TYPE_PRECISION (sizetype)
2171 || TYPE_UNSIGNED (gnu_index_subtype)
2172 != TYPE_UNSIGNED (sizetype))
2173 && TREE_CODE (gnu_min) == INTEGER_CST
2174 && TREE_CODE (gnu_max) == INTEGER_CST
2175 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2176 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
2177 TYPE_MIN_VALUE (gnu_index_subtype)))
2178 gnu_min = size_one_node, gnu_max = size_zero_node;
2180 /* Now compute the size of this bound. We need to provide
2181 GCC with an upper bound to use but have to deal with the
2182 "superflat" case. There are three ways to do this. If we
2183 can prove that the array can never be superflat, we can
2184 just use the high bound of the index subtype. If we can
2185 prove that the low bound minus one can't overflow, we
2186 can do this as MAX (hb, lb - 1). Otherwise, we have to use
2187 the expression hb >= lb ? hb : lb - 1. */
2188 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
2190 /* See if the base array type is already flat. If it is, we
2191 are probably compiling an ACVC test, but it will cause the
2192 code below to malfunction if we don't handle it specially. */
2193 if (TREE_CODE (gnu_base_min) == INTEGER_CST
2194 && TREE_CODE (gnu_base_max) == INTEGER_CST
2195 && !TREE_OVERFLOW (gnu_base_min)
2196 && !TREE_OVERFLOW (gnu_base_max)
2197 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
2198 gnu_high = size_zero_node, gnu_min = size_one_node;
2200 /* If gnu_high is now an integer which overflowed, the array
2201 cannot be superflat. */
2202 else if (TREE_CODE (gnu_high) == INTEGER_CST
2203 && TREE_OVERFLOW (gnu_high))
2204 gnu_high = gnu_max;
2205 else if (TYPE_UNSIGNED (gnu_base_subtype)
2206 || TREE_CODE (gnu_high) == INTEGER_CST)
2207 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
2208 else
2209 gnu_high
2210 = build_cond_expr
2211 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
2212 gnu_max, gnu_min),
2213 gnu_max, gnu_high);
2215 gnu_index_type[index]
2216 = create_index_type (gnu_min, gnu_high, gnu_index_subtype,
2217 gnat_entity);
2219 /* Also compute the maximum size of the array. Here we
2220 see if any constraint on the index type of the base type
2221 can be used in the case of self-referential bound on
2222 the index type of the subtype. We look for a non-"infinite"
2223 and non-self-referential bound from any type involved and
2224 handle each bound separately. */
2226 if ((TREE_CODE (gnu_min) == INTEGER_CST
2227 && !TREE_OVERFLOW (gnu_min)
2228 && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
2229 || !CONTAINS_PLACEHOLDER_P (gnu_min)
2230 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2231 && !TREE_OVERFLOW (gnu_base_min)))
2232 gnu_base_min = gnu_min;
2234 if ((TREE_CODE (gnu_max) == INTEGER_CST
2235 && !TREE_OVERFLOW (gnu_max)
2236 && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
2237 || !CONTAINS_PLACEHOLDER_P (gnu_max)
2238 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2239 && !TREE_OVERFLOW (gnu_base_max)))
2240 gnu_base_max = gnu_max;
2242 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2243 && TREE_OVERFLOW (gnu_base_min))
2244 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2245 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2246 && TREE_OVERFLOW (gnu_base_max))
2247 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2248 max_overflow = true;
2250 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
2251 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
2253 gnu_this_max
2254 = size_binop (MAX_EXPR,
2255 size_binop (PLUS_EXPR, size_one_node,
2256 size_binop (MINUS_EXPR, gnu_base_max,
2257 gnu_base_min)),
2258 size_zero_node);
2260 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2261 && TREE_OVERFLOW (gnu_this_max))
2262 max_overflow = true;
2264 gnu_max_size
2265 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2267 if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
2268 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
2269 != INTEGER_CST)
2270 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
2271 || (TREE_TYPE (gnu_index_subtype)
2272 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
2273 != INTEGER_TYPE))
2274 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
2275 || (TYPE_PRECISION (gnu_index_subtype)
2276 > TYPE_PRECISION (sizetype)))
2277 need_index_type_struct = true;
2280 /* Then flatten: create the array of arrays. For an array type
2281 used to implement a packed array, get the component type from
2282 the original array type since the representation clauses that
2283 can affect it are on the latter. */
2284 if (Is_Packed_Array_Type (gnat_entity)
2285 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2287 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2288 for (index = array_dim - 1; index >= 0; index--)
2289 gnu_type = TREE_TYPE (gnu_type);
2291 /* One of the above calls might have caused us to be elaborated,
2292 so don't blow up if so. */
2293 if (present_gnu_tree (gnat_entity))
2295 maybe_present = true;
2296 break;
2299 else
2301 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
2303 /* One of the above calls might have caused us to be elaborated,
2304 so don't blow up if so. */
2305 if (present_gnu_tree (gnat_entity))
2307 maybe_present = true;
2308 break;
2311 /* Try to get a smaller form of the component if needed. */
2312 if ((Is_Packed (gnat_entity)
2313 || Has_Component_Size_Clause (gnat_entity))
2314 && !Is_Bit_Packed_Array (gnat_entity)
2315 && !Has_Aliased_Components (gnat_entity)
2316 && !Strict_Alignment (Component_Type (gnat_entity))
2317 && TREE_CODE (gnu_type) == RECORD_TYPE
2318 && host_integerp (TYPE_SIZE (gnu_type), 1))
2319 gnu_type = make_packable_type (gnu_type, false);
2321 /* Get and validate any specified Component_Size, but if Packed,
2322 ignore it since the front end will have taken care of it. */
2323 gnu_comp_size
2324 = validate_size (Component_Size (gnat_entity), gnu_type,
2325 gnat_entity,
2326 (Is_Bit_Packed_Array (gnat_entity)
2327 ? TYPE_DECL : VAR_DECL), true,
2328 Has_Component_Size_Clause (gnat_entity));
2330 /* If the component type is a RECORD_TYPE that has a
2331 self-referential size, use the maximum size. */
2332 if (!gnu_comp_size
2333 && TREE_CODE (gnu_type) == RECORD_TYPE
2334 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2335 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
2337 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
2339 tree orig_gnu_type;
2340 gnu_type
2341 = make_type_from_size (gnu_type, gnu_comp_size, false);
2342 orig_gnu_type = gnu_type;
2343 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
2344 gnat_entity, "C_PAD", false,
2345 definition, true);
2346 /* If a padding record was made, declare it now since it
2347 will never be declared otherwise. This is necessary
2348 to ensure that its subtrees are properly marked. */
2349 if (gnu_type != orig_gnu_type)
2350 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL,
2351 true, debug_info_p, gnat_entity);
2354 if (Has_Volatile_Components (Base_Type (gnat_entity)))
2355 gnu_type = build_qualified_type (gnu_type,
2356 (TYPE_QUALS (gnu_type)
2357 | TYPE_QUAL_VOLATILE));
2360 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2361 TYPE_SIZE_UNIT (gnu_type));
2362 gnu_max_size = size_binop (MULT_EXPR,
2363 convert (bitsizetype, gnu_max_size),
2364 TYPE_SIZE (gnu_type));
2366 for (index = array_dim - 1; index >= 0; index --)
2368 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
2369 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2370 if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2371 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2374 /* If we are at file level and this is a multi-dimensional array, we
2375 need to make a variable corresponding to the stride of the
2376 inner dimensions. */
2377 if (global_bindings_p () && array_dim > 1)
2379 tree gnu_str_name = get_identifier ("ST");
2380 tree gnu_arr_type;
2382 for (gnu_arr_type = TREE_TYPE (gnu_type);
2383 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2384 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2385 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
2387 tree eltype = TREE_TYPE (gnu_arr_type);
2389 TYPE_SIZE (gnu_arr_type)
2390 = elaborate_expression_1 (gnat_entity, gnat_entity,
2391 TYPE_SIZE (gnu_arr_type),
2392 gnu_str_name, definition, 0);
2394 /* ??? For now, store the size as a multiple of the
2395 alignment of the element type in bytes so that we
2396 can see the alignment from the tree. */
2397 TYPE_SIZE_UNIT (gnu_arr_type)
2398 = build_binary_op
2399 (MULT_EXPR, sizetype,
2400 elaborate_expression_1
2401 (gnat_entity, gnat_entity,
2402 build_binary_op (EXACT_DIV_EXPR, sizetype,
2403 TYPE_SIZE_UNIT (gnu_arr_type),
2404 size_int (TYPE_ALIGN (eltype)
2405 / BITS_PER_UNIT)),
2406 concat_id_with_name (gnu_str_name, "A_U"),
2407 definition, 0),
2408 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2410 /* ??? create_type_decl is not invoked on the inner types so
2411 the MULT_EXPR node built above will never be marked. */
2412 mark_visited (&TYPE_SIZE_UNIT (gnu_arr_type));
2416 /* If we need to write out a record type giving the names of
2417 the bounds, do it now. */
2418 if (need_index_type_struct && debug_info_p)
2420 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
2421 tree gnu_field_list = NULL_TREE;
2422 tree gnu_field;
2424 TYPE_NAME (gnu_bound_rec_type)
2425 = create_concat_name (gnat_entity, "XA");
2427 for (index = array_dim - 1; index >= 0; index--)
2429 tree gnu_type_name
2430 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
2432 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
2433 gnu_type_name = DECL_NAME (gnu_type_name);
2435 gnu_field = create_field_decl (gnu_type_name,
2436 integer_type_node,
2437 gnu_bound_rec_type,
2438 0, NULL_TREE, NULL_TREE, 0);
2439 TREE_CHAIN (gnu_field) = gnu_field_list;
2440 gnu_field_list = gnu_field;
2443 finish_record_type (gnu_bound_rec_type, gnu_field_list,
2444 0, false);
2446 TYPE_STUB_DECL (gnu_type)
2447 = build_decl (TYPE_DECL, NULL_TREE, gnu_type);
2449 add_parallel_type
2450 (TYPE_STUB_DECL (gnu_type), gnu_bound_rec_type);
2453 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2454 = (Convention (gnat_entity) == Convention_Fortran);
2455 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2456 = (Is_Packed_Array_Type (gnat_entity)
2457 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2459 /* If our size depends on a placeholder and the maximum size doesn't
2460 overflow, use it. */
2461 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2462 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2463 && TREE_OVERFLOW (gnu_max_size))
2464 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2465 && TREE_OVERFLOW (gnu_max_size_unit))
2466 && !max_overflow)
2468 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2469 TYPE_SIZE (gnu_type));
2470 TYPE_SIZE_UNIT (gnu_type)
2471 = size_binop (MIN_EXPR, gnu_max_size_unit,
2472 TYPE_SIZE_UNIT (gnu_type));
2475 /* Set our alias set to that of our base type. This gives all
2476 array subtypes the same alias set. */
2477 copy_alias_set (gnu_type, gnu_base_type);
2480 /* If this is a packed type, make this type the same as the packed
2481 array type, but do some adjusting in the type first. */
2483 if (Present (Packed_Array_Type (gnat_entity)))
2485 Entity_Id gnat_index;
2486 tree gnu_inner_type;
2488 /* First finish the type we had been making so that we output
2489 debugging information for it */
2490 gnu_type
2491 = build_qualified_type (gnu_type,
2492 (TYPE_QUALS (gnu_type)
2493 | (TYPE_QUAL_VOLATILE
2494 * Treat_As_Volatile (gnat_entity))));
2495 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2496 !Comes_From_Source (gnat_entity),
2497 debug_info_p, gnat_entity);
2498 if (!Comes_From_Source (gnat_entity))
2499 DECL_ARTIFICIAL (gnu_decl) = 1;
2501 /* Save it as our equivalent in case the call below elaborates
2502 this type again. */
2503 save_gnu_tree (gnat_entity, gnu_decl, false);
2505 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2506 NULL_TREE, 0);
2507 this_made_decl = true;
2508 gnu_type = TREE_TYPE (gnu_decl);
2509 save_gnu_tree (gnat_entity, NULL_TREE, false);
2511 gnu_inner_type = gnu_type;
2512 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2513 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2514 || TYPE_IS_PADDING_P (gnu_inner_type)))
2515 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2517 /* We need to point the type we just made to our index type so
2518 the actual bounds can be put into a template. */
2520 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2521 && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2522 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2523 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2525 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2527 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2528 If it is, we need to make another type. */
2529 if (TYPE_MODULAR_P (gnu_inner_type))
2531 tree gnu_subtype;
2533 gnu_subtype = make_node (INTEGER_TYPE);
2535 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2536 TYPE_MIN_VALUE (gnu_subtype)
2537 = TYPE_MIN_VALUE (gnu_inner_type);
2538 TYPE_MAX_VALUE (gnu_subtype)
2539 = TYPE_MAX_VALUE (gnu_inner_type);
2540 TYPE_PRECISION (gnu_subtype)
2541 = TYPE_PRECISION (gnu_inner_type);
2542 TYPE_UNSIGNED (gnu_subtype)
2543 = TYPE_UNSIGNED (gnu_inner_type);
2544 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2545 layout_type (gnu_subtype);
2547 gnu_inner_type = gnu_subtype;
2550 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2553 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2555 for (gnat_index = First_Index (gnat_entity);
2556 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2557 SET_TYPE_ACTUAL_BOUNDS
2558 (gnu_inner_type,
2559 tree_cons (NULL_TREE,
2560 get_unpadded_type (Etype (gnat_index)),
2561 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2563 if (Convention (gnat_entity) != Convention_Fortran)
2564 SET_TYPE_ACTUAL_BOUNDS
2565 (gnu_inner_type,
2566 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2568 if (TREE_CODE (gnu_type) == RECORD_TYPE
2569 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2570 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2574 /* Abort if packed array with no packed array type field set. */
2575 else
2576 gcc_assert (!Is_Packed (gnat_entity));
2578 break;
2580 case E_String_Literal_Subtype:
2581 /* Create the type for a string literal. */
2583 Entity_Id gnat_full_type
2584 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2585 && Present (Full_View (Etype (gnat_entity)))
2586 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2587 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2588 tree gnu_string_array_type
2589 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2590 tree gnu_string_index_type
2591 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2592 (TYPE_DOMAIN (gnu_string_array_type))));
2593 tree gnu_lower_bound
2594 = convert (gnu_string_index_type,
2595 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2596 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2597 tree gnu_length = ssize_int (length - 1);
2598 tree gnu_upper_bound
2599 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2600 gnu_lower_bound,
2601 convert (gnu_string_index_type, gnu_length));
2602 tree gnu_range_type
2603 = build_range_type (gnu_string_index_type,
2604 gnu_lower_bound, gnu_upper_bound);
2605 tree gnu_index_type
2606 = create_index_type (convert (sizetype,
2607 TYPE_MIN_VALUE (gnu_range_type)),
2608 convert (sizetype,
2609 TYPE_MAX_VALUE (gnu_range_type)),
2610 gnu_range_type, gnat_entity);
2612 gnu_type
2613 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2614 gnu_index_type);
2615 copy_alias_set (gnu_type, gnu_string_type);
2617 break;
2619 /* Record Types and Subtypes
2621 The following fields are defined on record types:
2623 Has_Discriminants True if the record has discriminants
2624 First_Discriminant Points to head of list of discriminants
2625 First_Entity Points to head of list of fields
2626 Is_Tagged_Type True if the record is tagged
2628 Implementation of Ada records and discriminated records:
2630 A record type definition is transformed into the equivalent of a C
2631 struct definition. The fields that are the discriminants which are
2632 found in the Full_Type_Declaration node and the elements of the
2633 Component_List found in the Record_Type_Definition node. The
2634 Component_List can be a recursive structure since each Variant of
2635 the Variant_Part of the Component_List has a Component_List.
2637 Processing of a record type definition comprises starting the list of
2638 field declarations here from the discriminants and the calling the
2639 function components_to_record to add the rest of the fields from the
2640 component list and return the gnu type node. The function
2641 components_to_record will call itself recursively as it traverses
2642 the tree. */
2644 case E_Record_Type:
2645 if (Has_Complex_Representation (gnat_entity))
2647 gnu_type
2648 = build_complex_type
2649 (get_unpadded_type
2650 (Etype (Defining_Entity
2651 (First (Component_Items
2652 (Component_List
2653 (Type_Definition
2654 (Declaration_Node (gnat_entity)))))))));
2656 break;
2660 Node_Id full_definition = Declaration_Node (gnat_entity);
2661 Node_Id record_definition = Type_Definition (full_definition);
2662 Entity_Id gnat_field;
2663 tree gnu_field;
2664 tree gnu_field_list = NULL_TREE;
2665 tree gnu_get_parent;
2666 /* Set PACKED in keeping with gnat_to_gnu_field. */
2667 int packed
2668 = Is_Packed (gnat_entity)
2670 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2671 ? -1
2672 : (Known_Alignment (gnat_entity)
2673 || (Strict_Alignment (gnat_entity)
2674 && Known_Static_Esize (gnat_entity)))
2675 ? -2
2676 : 0;
2677 bool has_rep = Has_Specified_Layout (gnat_entity);
2678 bool all_rep = has_rep;
2679 bool is_extension
2680 = (Is_Tagged_Type (gnat_entity)
2681 && Nkind (record_definition) == N_Derived_Type_Definition);
2683 /* See if all fields have a rep clause. Stop when we find one
2684 that doesn't. */
2685 for (gnat_field = First_Entity (gnat_entity);
2686 Present (gnat_field) && all_rep;
2687 gnat_field = Next_Entity (gnat_field))
2688 if ((Ekind (gnat_field) == E_Component
2689 || Ekind (gnat_field) == E_Discriminant)
2690 && No (Component_Clause (gnat_field)))
2691 all_rep = false;
2693 /* If this is a record extension, go a level further to find the
2694 record definition. Also, verify we have a Parent_Subtype. */
2695 if (is_extension)
2697 if (!type_annotate_only
2698 || Present (Record_Extension_Part (record_definition)))
2699 record_definition = Record_Extension_Part (record_definition);
2701 gcc_assert (type_annotate_only
2702 || Present (Parent_Subtype (gnat_entity)));
2705 /* Make a node for the record. If we are not defining the record,
2706 suppress expanding incomplete types. */
2707 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2708 TYPE_NAME (gnu_type) = gnu_entity_id;
2709 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2711 if (!definition)
2712 defer_incomplete_level++, this_deferred = true;
2714 /* If both a size and rep clause was specified, put the size in
2715 the record type now so that it can get the proper mode. */
2716 if (has_rep && Known_Esize (gnat_entity))
2717 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2719 /* Always set the alignment here so that it can be used to
2720 set the mode, if it is making the alignment stricter. If
2721 it is invalid, it will be checked again below. If this is to
2722 be Atomic, choose a default alignment of a word unless we know
2723 the size and it's smaller. */
2724 if (Known_Alignment (gnat_entity))
2725 TYPE_ALIGN (gnu_type)
2726 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2727 else if (Is_Atomic (gnat_entity))
2728 TYPE_ALIGN (gnu_type)
2729 = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2730 /* If a type needs strict alignment, the minimum size will be the
2731 type size instead of the RM size (see validate_size). Cap the
2732 alignment, lest it causes this type size to become too large. */
2733 else if (Strict_Alignment (gnat_entity)
2734 && Known_Static_Esize (gnat_entity))
2736 unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2737 unsigned int raw_align = raw_size & -raw_size;
2738 if (raw_align < BIGGEST_ALIGNMENT)
2739 TYPE_ALIGN (gnu_type) = raw_align;
2741 else
2742 TYPE_ALIGN (gnu_type) = 0;
2744 /* If we have a Parent_Subtype, make a field for the parent. If
2745 this record has rep clauses, force the position to zero. */
2746 if (Present (Parent_Subtype (gnat_entity)))
2748 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2749 tree gnu_parent;
2751 /* A major complexity here is that the parent subtype will
2752 reference our discriminants in its Discriminant_Constraint
2753 list. But those must reference the parent component of this
2754 record which is of the parent subtype we have not built yet!
2755 To break the circle we first build a dummy COMPONENT_REF which
2756 represents the "get to the parent" operation and initialize
2757 each of those discriminants to a COMPONENT_REF of the above
2758 dummy parent referencing the corresponding discriminant of the
2759 base type of the parent subtype. */
2760 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2761 build0 (PLACEHOLDER_EXPR, gnu_type),
2762 build_decl (FIELD_DECL, NULL_TREE,
2763 void_type_node),
2764 NULL_TREE);
2766 if (Has_Discriminants (gnat_entity))
2767 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2768 Present (gnat_field);
2769 gnat_field = Next_Stored_Discriminant (gnat_field))
2770 if (Present (Corresponding_Discriminant (gnat_field)))
2771 save_gnu_tree
2772 (gnat_field,
2773 build3 (COMPONENT_REF,
2774 get_unpadded_type (Etype (gnat_field)),
2775 gnu_get_parent,
2776 gnat_to_gnu_field_decl (Corresponding_Discriminant
2777 (gnat_field)),
2778 NULL_TREE),
2779 true);
2781 /* Then we build the parent subtype. */
2782 gnu_parent = gnat_to_gnu_type (gnat_parent);
2784 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2785 initially built. The discriminants must reference the fields
2786 of the parent subtype and not those of its base type for the
2787 placeholder machinery to properly work. */
2788 if (Has_Discriminants (gnat_entity))
2789 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2790 Present (gnat_field);
2791 gnat_field = Next_Stored_Discriminant (gnat_field))
2792 if (Present (Corresponding_Discriminant (gnat_field)))
2794 Entity_Id field = Empty;
2795 for (field = First_Stored_Discriminant (gnat_parent);
2796 Present (field);
2797 field = Next_Stored_Discriminant (field))
2798 if (same_discriminant_p (gnat_field, field))
2799 break;
2800 gcc_assert (Present (field));
2801 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2802 = gnat_to_gnu_field_decl (field);
2805 /* The "get to the parent" COMPONENT_REF must be given its
2806 proper type... */
2807 TREE_TYPE (gnu_get_parent) = gnu_parent;
2809 /* ...and reference the _parent field of this record. */
2810 gnu_field_list
2811 = create_field_decl (get_identifier
2812 (Get_Name_String (Name_uParent)),
2813 gnu_parent, gnu_type, 0,
2814 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2815 has_rep ? bitsize_zero_node : 0, 1);
2816 DECL_INTERNAL_P (gnu_field_list) = 1;
2817 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2820 /* Make the fields for the discriminants and put them into the record
2821 unless it's an Unchecked_Union. */
2822 if (Has_Discriminants (gnat_entity))
2823 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2824 Present (gnat_field);
2825 gnat_field = Next_Stored_Discriminant (gnat_field))
2827 /* If this is a record extension and this discriminant
2828 is the renaming of another discriminant, we've already
2829 handled the discriminant above. */
2830 if (Present (Parent_Subtype (gnat_entity))
2831 && Present (Corresponding_Discriminant (gnat_field)))
2832 continue;
2834 gnu_field
2835 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2837 /* Make an expression using a PLACEHOLDER_EXPR from the
2838 FIELD_DECL node just created and link that with the
2839 corresponding GNAT defining identifier. Then add to the
2840 list of fields. */
2841 save_gnu_tree (gnat_field,
2842 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2843 build0 (PLACEHOLDER_EXPR,
2844 DECL_CONTEXT (gnu_field)),
2845 gnu_field, NULL_TREE),
2846 true);
2848 if (!Is_Unchecked_Union (gnat_entity))
2850 TREE_CHAIN (gnu_field) = gnu_field_list;
2851 gnu_field_list = gnu_field;
2855 /* Put the discriminants into the record (backwards), so we can
2856 know the appropriate discriminant to use for the names of the
2857 variants. */
2858 TYPE_FIELDS (gnu_type) = gnu_field_list;
2860 /* Add the listed fields into the record and finish it up. */
2861 components_to_record (gnu_type, Component_List (record_definition),
2862 gnu_field_list, packed, definition, NULL,
2863 false, all_rep, false,
2864 Is_Unchecked_Union (gnat_entity));
2866 /* We used to remove the associations of the discriminants and
2867 _Parent for validity checking, but we may need them if there's
2868 Freeze_Node for a subtype used in this record. */
2869 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2870 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2872 /* If it is a tagged record force the type to BLKmode to insure
2873 that these objects will always be placed in memory. Do the
2874 same thing for limited record types. */
2875 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2876 TYPE_MODE (gnu_type) = BLKmode;
2878 /* If this is a derived type, we must make the alias set of this type
2879 the same as that of the type we are derived from. We assume here
2880 that the other type is already frozen. */
2881 if (Etype (gnat_entity) != gnat_entity
2882 && !(Is_Private_Type (Etype (gnat_entity))
2883 && Full_View (Etype (gnat_entity)) == gnat_entity))
2884 copy_alias_set (gnu_type, gnat_to_gnu_type (Etype (gnat_entity)));
2886 /* Fill in locations of fields. */
2887 annotate_rep (gnat_entity, gnu_type);
2889 /* If there are any entities in the chain corresponding to
2890 components that we did not elaborate, ensure we elaborate their
2891 types if they are Itypes. */
2892 for (gnat_temp = First_Entity (gnat_entity);
2893 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2894 if ((Ekind (gnat_temp) == E_Component
2895 || Ekind (gnat_temp) == E_Discriminant)
2896 && Is_Itype (Etype (gnat_temp))
2897 && !present_gnu_tree (gnat_temp))
2898 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2900 break;
2902 case E_Class_Wide_Subtype:
2903 /* If an equivalent type is present, that is what we should use.
2904 Otherwise, fall through to handle this like a record subtype
2905 since it may have constraints. */
2906 if (gnat_equiv_type != gnat_entity)
2908 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
2909 maybe_present = true;
2910 break;
2913 /* ... fall through ... */
2915 case E_Record_Subtype:
2917 /* If Cloned_Subtype is Present it means this record subtype has
2918 identical layout to that type or subtype and we should use
2919 that GCC type for this one. The front end guarantees that
2920 the component list is shared. */
2921 if (Present (Cloned_Subtype (gnat_entity)))
2923 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2924 NULL_TREE, 0);
2925 maybe_present = true;
2928 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2929 changing the type, make a new type with each field having the
2930 type of the field in the new subtype but having the position
2931 computed by transforming every discriminant reference according
2932 to the constraints. We don't see any difference between
2933 private and nonprivate type here since derivations from types should
2934 have been deferred until the completion of the private type. */
2935 else
2937 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2938 tree gnu_base_type;
2939 tree gnu_orig_type;
2941 if (!definition)
2942 defer_incomplete_level++, this_deferred = true;
2944 /* Get the base type initially for its alignment and sizes. But
2945 if it is a padded type, we do all the other work with the
2946 unpadded type. */
2947 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
2949 if (TREE_CODE (gnu_base_type) == RECORD_TYPE
2950 && TYPE_IS_PADDING_P (gnu_base_type))
2951 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
2952 else
2953 gnu_type = gnu_orig_type = gnu_base_type;
2955 if (present_gnu_tree (gnat_entity))
2957 maybe_present = true;
2958 break;
2961 /* When the type has discriminants, and these discriminants
2962 affect the shape of what it built, factor them in.
2964 If we are making a subtype of an Unchecked_Union (must be an
2965 Itype), just return the type.
2967 We can't just use Is_Constrained because private subtypes without
2968 discriminants of full types with discriminants with default
2969 expressions are Is_Constrained but aren't constrained! */
2971 if (IN (Ekind (gnat_base_type), Record_Kind)
2972 && !Is_For_Access_Subtype (gnat_entity)
2973 && !Is_Unchecked_Union (gnat_base_type)
2974 && Is_Constrained (gnat_entity)
2975 && Stored_Constraint (gnat_entity) != No_Elist
2976 && Present (Discriminant_Constraint (gnat_entity)))
2978 Entity_Id gnat_field;
2979 tree gnu_field_list = 0;
2980 tree gnu_pos_list
2981 = compute_field_positions (gnu_orig_type, NULL_TREE,
2982 size_zero_node, bitsize_zero_node,
2983 BIGGEST_ALIGNMENT);
2984 tree gnu_subst_list
2985 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2986 definition);
2987 tree gnu_temp;
2989 gnu_type = make_node (RECORD_TYPE);
2990 TYPE_NAME (gnu_type) = gnu_entity_id;
2991 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2993 /* Set the size, alignment and alias set of the new type to
2994 match that of the old one, doing required substitutions.
2995 We do it this early because we need the size of the new
2996 type below to discard old fields if necessary. */
2997 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2998 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
2999 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
3000 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
3001 copy_alias_set (gnu_type, gnu_base_type);
3003 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3004 for (gnu_temp = gnu_subst_list;
3005 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3006 TYPE_SIZE (gnu_type)
3007 = substitute_in_expr (TYPE_SIZE (gnu_type),
3008 TREE_PURPOSE (gnu_temp),
3009 TREE_VALUE (gnu_temp));
3011 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
3012 for (gnu_temp = gnu_subst_list;
3013 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3014 TYPE_SIZE_UNIT (gnu_type)
3015 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
3016 TREE_PURPOSE (gnu_temp),
3017 TREE_VALUE (gnu_temp));
3019 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
3020 for (gnu_temp = gnu_subst_list;
3021 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3022 SET_TYPE_ADA_SIZE
3023 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
3024 TREE_PURPOSE (gnu_temp),
3025 TREE_VALUE (gnu_temp)));
3027 for (gnat_field = First_Entity (gnat_entity);
3028 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3029 if ((Ekind (gnat_field) == E_Component
3030 || Ekind (gnat_field) == E_Discriminant)
3031 && (Underlying_Type (Scope (Original_Record_Component
3032 (gnat_field)))
3033 == gnat_base_type)
3034 && (No (Corresponding_Discriminant (gnat_field))
3035 || !Is_Tagged_Type (gnat_base_type)))
3037 tree gnu_old_field
3038 = gnat_to_gnu_field_decl (Original_Record_Component
3039 (gnat_field));
3040 tree gnu_offset
3041 = TREE_VALUE (purpose_member (gnu_old_field,
3042 gnu_pos_list));
3043 tree gnu_pos = TREE_PURPOSE (gnu_offset);
3044 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
3045 tree gnu_field_type
3046 = gnat_to_gnu_type (Etype (gnat_field));
3047 tree gnu_size = TYPE_SIZE (gnu_field_type);
3048 tree gnu_new_pos = NULL_TREE;
3049 unsigned int offset_align
3050 = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
3052 tree gnu_field;
3054 /* If there was a component clause, the field types must be
3055 the same for the type and subtype, so copy the data from
3056 the old field to avoid recomputation here. Also if the
3057 field is justified modular and the optimization in
3058 gnat_to_gnu_field was applied. */
3059 if (Present (Component_Clause
3060 (Original_Record_Component (gnat_field)))
3061 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3062 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3063 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3064 == TREE_TYPE (gnu_old_field)))
3066 gnu_size = DECL_SIZE (gnu_old_field);
3067 gnu_field_type = TREE_TYPE (gnu_old_field);
3070 /* If the old field was packed and of constant size, we
3071 have to get the old size here, as it might differ from
3072 what the Etype conveys and the latter might overlap
3073 onto the following field. Try to arrange the type for
3074 possible better packing along the way. */
3075 else if (DECL_PACKED (gnu_old_field)
3076 && TREE_CODE (DECL_SIZE (gnu_old_field))
3077 == INTEGER_CST)
3079 gnu_size = DECL_SIZE (gnu_old_field);
3080 if (TYPE_MODE (gnu_field_type) == BLKmode
3081 && TREE_CODE (gnu_field_type) == RECORD_TYPE
3082 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
3083 gnu_field_type
3084 = make_packable_type (gnu_field_type, true);
3087 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
3088 for (gnu_temp = gnu_subst_list;
3089 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3090 gnu_pos = substitute_in_expr (gnu_pos,
3091 TREE_PURPOSE (gnu_temp),
3092 TREE_VALUE (gnu_temp));
3094 /* If the position is now a constant, we can set it as the
3095 position of the field when we make it. Otherwise, we need
3096 to deal with it specially below. */
3097 if (TREE_CONSTANT (gnu_pos))
3099 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
3101 /* Discard old fields that are outside the new type.
3102 This avoids confusing code scanning it to decide
3103 how to pass it to functions on some platforms. */
3104 if (TREE_CODE (gnu_new_pos) == INTEGER_CST
3105 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST
3106 && !integer_zerop (gnu_size)
3107 && !tree_int_cst_lt (gnu_new_pos,
3108 TYPE_SIZE (gnu_type)))
3109 continue;
3112 gnu_field
3113 = create_field_decl
3114 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
3115 DECL_PACKED (gnu_old_field), gnu_size, gnu_new_pos,
3116 !DECL_NONADDRESSABLE_P (gnu_old_field));
3118 if (!TREE_CONSTANT (gnu_pos))
3120 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
3121 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
3122 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
3123 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
3124 DECL_SIZE (gnu_field) = gnu_size;
3125 DECL_SIZE_UNIT (gnu_field)
3126 = convert (sizetype,
3127 size_binop (CEIL_DIV_EXPR, gnu_size,
3128 bitsize_unit_node));
3129 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
3132 DECL_INTERNAL_P (gnu_field)
3133 = DECL_INTERNAL_P (gnu_old_field);
3134 SET_DECL_ORIGINAL_FIELD
3135 (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
3136 ? DECL_ORIGINAL_FIELD (gnu_old_field)
3137 : gnu_old_field));
3138 DECL_DISCRIMINANT_NUMBER (gnu_field)
3139 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
3140 TREE_THIS_VOLATILE (gnu_field)
3141 = TREE_THIS_VOLATILE (gnu_old_field);
3142 TREE_CHAIN (gnu_field) = gnu_field_list;
3143 gnu_field_list = gnu_field;
3144 save_gnu_tree (gnat_field, gnu_field, false);
3147 /* Now go through the entities again looking for Itypes that
3148 we have not elaborated but should (e.g., Etypes of fields
3149 that have Original_Components). */
3150 for (gnat_field = First_Entity (gnat_entity);
3151 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3152 if ((Ekind (gnat_field) == E_Discriminant
3153 || Ekind (gnat_field) == E_Component)
3154 && !present_gnu_tree (Etype (gnat_field)))
3155 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3157 /* Do not finalize it since we're going to modify it below. */
3158 gnu_field_list = nreverse (gnu_field_list);
3159 finish_record_type (gnu_type, gnu_field_list, 2, true);
3161 /* Finalize size and mode. */
3162 TYPE_SIZE (gnu_type) = variable_size (TYPE_SIZE (gnu_type));
3163 TYPE_SIZE_UNIT (gnu_type)
3164 = variable_size (TYPE_SIZE_UNIT (gnu_type));
3166 compute_record_mode (gnu_type);
3168 /* Fill in locations of fields. */
3169 annotate_rep (gnat_entity, gnu_type);
3171 /* We've built a new type, make an XVS type to show what this
3172 is a subtype of. Some debuggers require the XVS type to be
3173 output first, so do it in that order. */
3174 if (debug_info_p)
3176 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3177 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
3179 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
3180 gnu_orig_name = DECL_NAME (gnu_orig_name);
3182 TYPE_NAME (gnu_subtype_marker)
3183 = create_concat_name (gnat_entity, "XVS");
3184 finish_record_type (gnu_subtype_marker,
3185 create_field_decl (gnu_orig_name,
3186 integer_type_node,
3187 gnu_subtype_marker,
3188 0, NULL_TREE,
3189 NULL_TREE, 0),
3190 0, false);
3192 add_parallel_type (TYPE_STUB_DECL (gnu_type),
3193 gnu_subtype_marker);
3196 /* Now we can finalize it. */
3197 rest_of_record_type_compilation (gnu_type);
3200 /* Otherwise, go down all the components in the new type and
3201 make them equivalent to those in the base type. */
3202 else
3203 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3204 gnat_temp = Next_Entity (gnat_temp))
3205 if ((Ekind (gnat_temp) == E_Discriminant
3206 && !Is_Unchecked_Union (gnat_base_type))
3207 || Ekind (gnat_temp) == E_Component)
3208 save_gnu_tree (gnat_temp,
3209 gnat_to_gnu_field_decl
3210 (Original_Record_Component (gnat_temp)), false);
3212 break;
3214 case E_Access_Subprogram_Type:
3215 /* Use the special descriptor type for dispatch tables if needed,
3216 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3217 Note that we are only required to do so for static tables in
3218 order to be compatible with the C++ ABI, but Ada 2005 allows
3219 to extend library level tagged types at the local level so
3220 we do it in the non-static case as well. */
3221 if (TARGET_VTABLE_USES_DESCRIPTORS
3222 && Is_Dispatch_Table_Entity (gnat_entity))
3224 gnu_type = fdesc_type_node;
3225 gnu_size = TYPE_SIZE (gnu_type);
3226 break;
3229 /* ... fall through ... */
3231 case E_Anonymous_Access_Subprogram_Type:
3232 /* If we are not defining this entity, and we have incomplete
3233 entities being processed above us, make a dummy type and
3234 fill it in later. */
3235 if (!definition && defer_incomplete_level != 0)
3237 struct incomplete *p
3238 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3240 gnu_type
3241 = build_pointer_type
3242 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3243 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3244 !Comes_From_Source (gnat_entity),
3245 debug_info_p, gnat_entity);
3246 this_made_decl = true;
3247 gnu_type = TREE_TYPE (gnu_decl);
3248 save_gnu_tree (gnat_entity, gnu_decl, false);
3249 saved = true;
3251 p->old_type = TREE_TYPE (gnu_type);
3252 p->full_type = Directly_Designated_Type (gnat_entity);
3253 p->next = defer_incomplete_list;
3254 defer_incomplete_list = p;
3255 break;
3258 /* ... fall through ... */
3260 case E_Allocator_Type:
3261 case E_Access_Type:
3262 case E_Access_Attribute_Type:
3263 case E_Anonymous_Access_Type:
3264 case E_General_Access_Type:
3266 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3267 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3268 bool is_from_limited_with
3269 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3270 && From_With_Type (gnat_desig_equiv));
3272 /* Get the "full view" of this entity. If this is an incomplete
3273 entity from a limited with, treat its non-limited view as the full
3274 view. Otherwise, if this is an incomplete or private type, use the
3275 full view. In the former case, we might point to a private type,
3276 in which case, we need its full view. Also, we want to look at the
3277 actual type used for the representation, so this takes a total of
3278 three steps. */
3279 Entity_Id gnat_desig_full_direct_first
3280 = (is_from_limited_with ? Non_Limited_View (gnat_desig_equiv)
3281 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3282 ? Full_View (gnat_desig_equiv) : Empty));
3283 Entity_Id gnat_desig_full_direct
3284 = ((is_from_limited_with
3285 && Present (gnat_desig_full_direct_first)
3286 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3287 ? Full_View (gnat_desig_full_direct_first)
3288 : gnat_desig_full_direct_first);
3289 Entity_Id gnat_desig_full
3290 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3292 /* This the type actually used to represent the designated type,
3293 either gnat_desig_full or gnat_desig_equiv. */
3294 Entity_Id gnat_desig_rep;
3296 /* Nonzero if this is a pointer to an unconstrained array. */
3297 bool is_unconstrained_array;
3299 /* We want to know if we'll be seeing the freeze node for any
3300 incomplete type we may be pointing to. */
3301 bool in_main_unit
3302 = (Present (gnat_desig_full)
3303 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3304 : In_Extended_Main_Code_Unit (gnat_desig_type));
3306 /* Nonzero if we make a dummy type here. */
3307 bool got_fat_p = false;
3308 /* Nonzero if the dummy is a fat pointer. */
3309 bool made_dummy = false;
3310 tree gnu_desig_type = NULL_TREE;
3311 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3313 if (!targetm.valid_pointer_mode (p_mode))
3314 p_mode = ptr_mode;
3316 /* If either the designated type or its full view is an unconstrained
3317 array subtype, replace it with the type it's a subtype of. This
3318 avoids problems with multiple copies of unconstrained array types.
3319 Likewise, if the designated type is a subtype of an incomplete
3320 record type, use the parent type to avoid order of elaboration
3321 issues. This can lose some code efficiency, but there is no
3322 alternative. */
3323 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3324 && ! Is_Constrained (gnat_desig_equiv))
3325 gnat_desig_equiv = Etype (gnat_desig_equiv);
3326 if (Present (gnat_desig_full)
3327 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3328 && ! Is_Constrained (gnat_desig_full))
3329 || (Ekind (gnat_desig_full) == E_Record_Subtype
3330 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3331 gnat_desig_full = Etype (gnat_desig_full);
3333 /* Now set the type that actually marks the representation of
3334 the designated type and also flag whether we have a unconstrained
3335 array. */
3336 gnat_desig_rep = gnat_desig_full ? gnat_desig_full : gnat_desig_equiv;
3337 is_unconstrained_array
3338 = (Is_Array_Type (gnat_desig_rep)
3339 && ! Is_Constrained (gnat_desig_rep));
3341 /* If we are pointing to an incomplete type whose completion is an
3342 unconstrained array, make a fat pointer type. The two types in our
3343 fields will be pointers to dummy nodes and will be replaced in
3344 update_pointer_to. Similarly, if the type itself is a dummy type or
3345 an unconstrained array. Also make a dummy TYPE_OBJECT_RECORD_TYPE
3346 in case we have any thin pointers to it. */
3347 if (is_unconstrained_array
3348 && (Present (gnat_desig_full)
3349 || (present_gnu_tree (gnat_desig_equiv)
3350 && TYPE_IS_DUMMY_P (TREE_TYPE
3351 (get_gnu_tree (gnat_desig_equiv))))
3352 || (No (gnat_desig_full) && ! in_main_unit
3353 && defer_incomplete_level != 0
3354 && ! present_gnu_tree (gnat_desig_equiv))
3355 || (in_main_unit && is_from_limited_with
3356 && Present (Freeze_Node (gnat_desig_rep)))))
3358 tree gnu_old
3359 = (present_gnu_tree (gnat_desig_rep)
3360 ? TREE_TYPE (get_gnu_tree (gnat_desig_rep))
3361 : make_dummy_type (gnat_desig_rep));
3362 tree fields;
3364 /* Show the dummy we get will be a fat pointer. */
3365 got_fat_p = made_dummy = true;
3367 /* If the call above got something that has a pointer, that
3368 pointer is our type. This could have happened either
3369 because the type was elaborated or because somebody
3370 else executed the code below. */
3371 gnu_type = TYPE_POINTER_TO (gnu_old);
3372 if (!gnu_type)
3374 tree gnu_template_type = make_node (ENUMERAL_TYPE);
3375 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
3376 tree gnu_array_type = make_node (ENUMERAL_TYPE);
3377 tree gnu_ptr_array = build_pointer_type (gnu_array_type);
3379 TYPE_NAME (gnu_template_type)
3380 = concat_id_with_name (get_entity_name (gnat_desig_equiv),
3381 "XUB");
3382 TYPE_DUMMY_P (gnu_template_type) = 1;
3384 TYPE_NAME (gnu_array_type)
3385 = concat_id_with_name (get_entity_name (gnat_desig_equiv),
3386 "XUA");
3387 TYPE_DUMMY_P (gnu_array_type) = 1;
3389 gnu_type = make_node (RECORD_TYPE);
3390 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
3391 TYPE_POINTER_TO (gnu_old) = gnu_type;
3393 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3394 fields
3395 = chainon (chainon (NULL_TREE,
3396 create_field_decl
3397 (get_identifier ("P_ARRAY"),
3398 gnu_ptr_array,
3399 gnu_type, 0, 0, 0, 0)),
3400 create_field_decl (get_identifier ("P_BOUNDS"),
3401 gnu_ptr_template,
3402 gnu_type, 0, 0, 0, 0));
3404 /* Make sure we can place this into a register. */
3405 TYPE_ALIGN (gnu_type)
3406 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
3407 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
3409 /* Do not finalize this record type since the types of
3410 its fields are incomplete. */
3411 finish_record_type (gnu_type, fields, 0, true);
3413 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
3414 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
3415 = concat_id_with_name (get_entity_name (gnat_desig_equiv),
3416 "XUT");
3417 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
3421 /* If we already know what the full type is, use it. */
3422 else if (Present (gnat_desig_full)
3423 && present_gnu_tree (gnat_desig_full))
3424 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3426 /* Get the type of the thing we are to point to and build a pointer
3427 to it. If it is a reference to an incomplete or private type with a
3428 full view that is a record, make a dummy type node and get the
3429 actual type later when we have verified it is safe. */
3430 else if ((! in_main_unit
3431 && ! present_gnu_tree (gnat_desig_equiv)
3432 && Present (gnat_desig_full)
3433 && ! present_gnu_tree (gnat_desig_full)
3434 && Is_Record_Type (gnat_desig_full))
3435 /* Likewise if we are pointing to a record or array and we
3436 are to defer elaborating incomplete types. We do this
3437 since this access type may be the full view of some
3438 private type. Note that the unconstrained array case is
3439 handled above. */
3440 || ((! in_main_unit || imported_p)
3441 && defer_incomplete_level != 0
3442 && ! present_gnu_tree (gnat_desig_equiv)
3443 && ((Is_Record_Type (gnat_desig_rep)
3444 || Is_Array_Type (gnat_desig_rep))))
3445 /* If this is a reference from a limited_with type back to our
3446 main unit and there's a Freeze_Node for it, either we have
3447 already processed the declaration and made the dummy type,
3448 in which case we just reuse the latter, or we have not yet,
3449 in which case we make the dummy type and it will be reused
3450 when the declaration is processed. In both cases, the
3451 pointer eventually created below will be automatically
3452 adjusted when the Freeze_Node is processed. Note that the
3453 unconstrained array case is handled above. */
3454 || (in_main_unit && is_from_limited_with
3455 && Present (Freeze_Node (gnat_desig_rep))))
3457 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3458 made_dummy = true;
3461 /* Otherwise handle the case of a pointer to itself. */
3462 else if (gnat_desig_equiv == gnat_entity)
3464 gnu_type
3465 = build_pointer_type_for_mode (void_type_node, p_mode,
3466 No_Strict_Aliasing (gnat_entity));
3467 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3470 /* If expansion is disabled, the equivalent type of a concurrent
3471 type is absent, so build a dummy pointer type. */
3472 else if (type_annotate_only && No (gnat_desig_equiv))
3473 gnu_type = ptr_void_type_node;
3475 /* Finally, handle the straightforward case where we can just
3476 elaborate our designated type and point to it. */
3477 else
3478 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3480 /* It is possible that a call to gnat_to_gnu_type above resolved our
3481 type. If so, just return it. */
3482 if (present_gnu_tree (gnat_entity))
3484 maybe_present = true;
3485 break;
3488 /* If we have a GCC type for the designated type, possibly modify it
3489 if we are pointing only to constant objects and then make a pointer
3490 to it. Don't do this for unconstrained arrays. */
3491 if (!gnu_type && gnu_desig_type)
3493 if (Is_Access_Constant (gnat_entity)
3494 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3496 gnu_desig_type
3497 = build_qualified_type
3498 (gnu_desig_type,
3499 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3501 /* Some extra processing is required if we are building a
3502 pointer to an incomplete type (in the GCC sense). We might
3503 have such a type if we just made a dummy, or directly out
3504 of the call to gnat_to_gnu_type above if we are processing
3505 an access type for a record component designating the
3506 record type itself. */
3507 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3509 /* We must ensure that the pointer to variant we make will
3510 be processed by update_pointer_to when the initial type
3511 is completed. Pretend we made a dummy and let further
3512 processing act as usual. */
3513 made_dummy = true;
3515 /* We must ensure that update_pointer_to will not retrieve
3516 the dummy variant when building a properly qualified
3517 version of the complete type. We take advantage of the
3518 fact that get_qualified_type is requiring TYPE_NAMEs to
3519 match to influence build_qualified_type and then also
3520 update_pointer_to here. */
3521 TYPE_NAME (gnu_desig_type)
3522 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3526 gnu_type
3527 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3528 No_Strict_Aliasing (gnat_entity));
3531 /* If we are not defining this object and we made a dummy pointer,
3532 save our current definition, evaluate the actual type, and replace
3533 the tentative type we made with the actual one. If we are to defer
3534 actually looking up the actual type, make an entry in the
3535 deferred list. If this is from a limited with, we have to defer
3536 to the end of the current spec in two cases: first if the
3537 designated type is in the current unit and second if the access
3538 type is. */
3539 if ((! in_main_unit || is_from_limited_with) && made_dummy)
3541 tree gnu_old_type
3542 = TYPE_FAT_POINTER_P (gnu_type)
3543 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3545 if (esize == POINTER_SIZE
3546 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
3547 gnu_type
3548 = build_pointer_type
3549 (TYPE_OBJECT_RECORD_TYPE
3550 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3552 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3553 !Comes_From_Source (gnat_entity),
3554 debug_info_p, gnat_entity);
3555 this_made_decl = true;
3556 gnu_type = TREE_TYPE (gnu_decl);
3557 save_gnu_tree (gnat_entity, gnu_decl, false);
3558 saved = true;
3560 if (defer_incomplete_level == 0
3561 && ! (is_from_limited_with
3562 && (in_main_unit
3563 || In_Extended_Main_Code_Unit (gnat_entity))))
3564 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
3565 gnat_to_gnu_type (gnat_desig_equiv));
3567 /* Note that the call to gnat_to_gnu_type here might have
3568 updated gnu_old_type directly, in which case it is not a
3569 dummy type any more when we get into update_pointer_to.
3571 This may happen for instance when the designated type is a
3572 record type, because their elaboration starts with an
3573 initial node from make_dummy_type, which may yield the same
3574 node as the one we got.
3576 Besides, variants of this non-dummy type might have been
3577 created along the way. update_pointer_to is expected to
3578 properly take care of those situations. */
3579 else
3581 struct incomplete *p
3582 = (struct incomplete *) xmalloc (sizeof
3583 (struct incomplete));
3584 struct incomplete **head
3585 = (is_from_limited_with
3586 && (in_main_unit
3587 || In_Extended_Main_Code_Unit (gnat_entity))
3588 ? &defer_limited_with : &defer_incomplete_list);
3590 p->old_type = gnu_old_type;
3591 p->full_type = gnat_desig_equiv;
3592 p->next = *head;
3593 *head = p;
3597 break;
3599 case E_Access_Protected_Subprogram_Type:
3600 case E_Anonymous_Access_Protected_Subprogram_Type:
3601 if (type_annotate_only && No (gnat_equiv_type))
3602 gnu_type = ptr_void_type_node;
3603 else
3605 /* The runtime representation is the equivalent type. */
3606 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3607 maybe_present = true;
3610 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3611 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3612 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3613 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3614 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3615 NULL_TREE, 0);
3617 break;
3619 case E_Access_Subtype:
3621 /* We treat this as identical to its base type; any constraint is
3622 meaningful only to the front end.
3624 The designated type must be elaborated as well, if it does
3625 not have its own freeze node. Designated (sub)types created
3626 for constrained components of records with discriminants are
3627 not frozen by the front end and thus not elaborated by gigi,
3628 because their use may appear before the base type is frozen,
3629 and because it is not clear that they are needed anywhere in
3630 Gigi. With the current model, there is no correct place where
3631 they could be elaborated. */
3633 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3634 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3635 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3636 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3637 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3639 /* If we are not defining this entity, and we have incomplete
3640 entities being processed above us, make a dummy type and
3641 elaborate it later. */
3642 if (!definition && defer_incomplete_level != 0)
3644 struct incomplete *p
3645 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3646 tree gnu_ptr_type
3647 = build_pointer_type
3648 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3650 p->old_type = TREE_TYPE (gnu_ptr_type);
3651 p->full_type = Directly_Designated_Type (gnat_entity);
3652 p->next = defer_incomplete_list;
3653 defer_incomplete_list = p;
3655 else if (!IN (Ekind (Base_Type
3656 (Directly_Designated_Type (gnat_entity))),
3657 Incomplete_Or_Private_Kind))
3658 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3659 NULL_TREE, 0);
3662 maybe_present = true;
3663 break;
3665 /* Subprogram Entities
3667 The following access functions are defined for subprograms (functions
3668 or procedures):
3670 First_Formal The first formal parameter.
3671 Is_Imported Indicates that the subprogram has appeared in
3672 an INTERFACE or IMPORT pragma. For now we
3673 assume that the external language is C.
3674 Is_Exported Likewise but for an EXPORT pragma.
3675 Is_Inlined True if the subprogram is to be inlined.
3677 In addition for function subprograms we have:
3679 Etype Return type of the function.
3681 Each parameter is first checked by calling must_pass_by_ref on its
3682 type to determine if it is passed by reference. For parameters which
3683 are copied in, if they are Ada In Out or Out parameters, their return
3684 value becomes part of a record which becomes the return type of the
3685 function (C function - note that this applies only to Ada procedures
3686 so there is no Ada return type). Additional code to store back the
3687 parameters will be generated on the caller side. This transformation
3688 is done here, not in the front-end.
3690 The intended result of the transformation can be seen from the
3691 equivalent source rewritings that follow:
3693 struct temp {int a,b};
3694 procedure P (A,B: In Out ...) is temp P (int A,B)
3695 begin {
3696 .. ..
3697 end P; return {A,B};
3700 temp t;
3701 P(X,Y); t = P(X,Y);
3702 X = t.a , Y = t.b;
3704 For subprogram types we need to perform mainly the same conversions to
3705 GCC form that are needed for procedures and function declarations. The
3706 only difference is that at the end, we make a type declaration instead
3707 of a function declaration. */
3709 case E_Subprogram_Type:
3710 case E_Function:
3711 case E_Procedure:
3713 /* The first GCC parameter declaration (a PARM_DECL node). The
3714 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3715 actually is the head of this parameter list. */
3716 tree gnu_param_list = NULL_TREE;
3717 /* Likewise for the stub associated with an exported procedure. */
3718 tree gnu_stub_param_list = NULL_TREE;
3719 /* The type returned by a function. If the subprogram is a procedure
3720 this type should be void_type_node. */
3721 tree gnu_return_type = void_type_node;
3722 /* List of fields in return type of procedure with copy-in copy-out
3723 parameters. */
3724 tree gnu_field_list = NULL_TREE;
3725 /* Non-null for subprograms containing parameters passed by copy-in
3726 copy-out (Ada In Out or Out parameters not passed by reference),
3727 in which case it is the list of nodes used to specify the values of
3728 the in out/out parameters that are returned as a record upon
3729 procedure return. The TREE_PURPOSE of an element of this list is
3730 a field of the record and the TREE_VALUE is the PARM_DECL
3731 corresponding to that field. This list will be saved in the
3732 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3733 tree gnu_return_list = NULL_TREE;
3734 /* If an import pragma asks to map this subprogram to a GCC builtin,
3735 this is the builtin DECL node. */
3736 tree gnu_builtin_decl = NULL_TREE;
3737 /* For the stub associated with an exported procedure. */
3738 tree gnu_stub_type = NULL_TREE, gnu_stub_name = NULL_TREE;
3739 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
3740 Entity_Id gnat_param;
3741 bool inline_flag = Is_Inlined (gnat_entity);
3742 bool public_flag = Is_Public (gnat_entity) || imported_p;
3743 bool extern_flag
3744 = (Is_Public (gnat_entity) && !definition) || imported_p;
3746 /* The semantics of "pure" in Ada essentially matches that of "const"
3747 in the back-end. In particular, both properties are orthogonal to
3748 the "nothrow" property if the EH circuitry is explicit in the
3749 internal representation of the back-end. If we are to completely
3750 hide the EH circuitry from it, we need to declare that calls to pure
3751 Ada subprograms that can throw have side effects since they can
3752 trigger an "abnormal" transfer of control flow; thus they can be
3753 neither "const" nor "pure" in the back-end sense. */
3754 bool const_flag
3755 = (Exception_Mechanism == Back_End_Exceptions
3756 && Is_Pure (gnat_entity));
3758 bool volatile_flag = No_Return (gnat_entity);
3759 bool returns_by_ref = false;
3760 bool returns_unconstrained = false;
3761 bool returns_by_target_ptr = false;
3762 bool has_copy_in_out = false;
3763 bool has_stub = false;
3764 int parmnum;
3766 if (kind == E_Subprogram_Type && !definition)
3767 /* A parameter may refer to this type, so defer completion
3768 of any incomplete types. */
3769 defer_incomplete_level++, this_deferred = true;
3771 /* If the subprogram has an alias, it is probably inherited, so
3772 we can use the original one. If the original "subprogram"
3773 is actually an enumeration literal, it may be the first use
3774 of its type, so we must elaborate that type now. */
3775 if (Present (Alias (gnat_entity)))
3777 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3778 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3780 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3781 gnu_expr, 0);
3783 /* Elaborate any Itypes in the parameters of this entity. */
3784 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3785 Present (gnat_temp);
3786 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3787 if (Is_Itype (Etype (gnat_temp)))
3788 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3790 break;
3793 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3794 corresponding DECL node.
3796 We still want the parameter associations to take place because the
3797 proper generation of calls depends on it (a GNAT parameter without
3798 a corresponding GCC tree has a very specific meaning), so we don't
3799 just break here. */
3800 if (Convention (gnat_entity) == Convention_Intrinsic)
3801 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3803 /* ??? What if we don't find the builtin node above ? warn ? err ?
3804 In the current state we neither warn nor err, and calls will just
3805 be handled as for regular subprograms. */
3807 if (kind == E_Function || kind == E_Subprogram_Type)
3808 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3810 /* If this function returns by reference, make the actual
3811 return type of this function the pointer and mark the decl. */
3812 if (Returns_By_Ref (gnat_entity))
3814 returns_by_ref = true;
3815 gnu_return_type = build_pointer_type (gnu_return_type);
3818 /* If the Mechanism is By_Reference, ensure the return type uses
3819 the machine's by-reference mechanism, which may not the same
3820 as above (e.g., it might be by passing a fake parameter). */
3821 else if (kind == E_Function
3822 && Mechanism (gnat_entity) == By_Reference)
3824 TREE_ADDRESSABLE (gnu_return_type) = 1;
3826 /* We expect this bit to be reset by gigi shortly, so can avoid a
3827 type node copy here. This actually also prevents troubles with
3828 the generation of debug information for the function, because
3829 we might have issued such info for this type already, and would
3830 be attaching a distinct type node to the function if we made a
3831 copy here. */
3834 /* If we are supposed to return an unconstrained array,
3835 actually return a fat pointer and make a note of that. Return
3836 a pointer to an unconstrained record of variable size. */
3837 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3839 gnu_return_type = TREE_TYPE (gnu_return_type);
3840 returns_unconstrained = true;
3843 /* If the type requires a transient scope, the result is allocated
3844 on the secondary stack, so the result type of the function is
3845 just a pointer. */
3846 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3848 gnu_return_type = build_pointer_type (gnu_return_type);
3849 returns_unconstrained = true;
3852 /* If the type is a padded type and the underlying type would not
3853 be passed by reference or this function has a foreign convention,
3854 return the underlying type. */
3855 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3856 && TYPE_IS_PADDING_P (gnu_return_type)
3857 && (!default_pass_by_ref (TREE_TYPE
3858 (TYPE_FIELDS (gnu_return_type)))
3859 || Has_Foreign_Convention (gnat_entity)))
3860 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3862 /* If the return type has a non-constant size, we convert the function
3863 into a procedure and its caller will pass a pointer to an object as
3864 the first parameter when we call the function. This can happen for
3865 an unconstrained type with a maximum size or a constrained type with
3866 a size not known at compile time. */
3867 if (TYPE_SIZE_UNIT (gnu_return_type)
3868 && !TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type)))
3870 returns_by_target_ptr = true;
3871 gnu_param_list
3872 = create_param_decl (get_identifier ("TARGET"),
3873 build_reference_type (gnu_return_type),
3874 true);
3875 gnu_return_type = void_type_node;
3878 /* If the return type has a size that overflows, we cannot have
3879 a function that returns that type. This usage doesn't make
3880 sense anyway, so give an error here. */
3881 if (TYPE_SIZE_UNIT (gnu_return_type)
3882 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type))
3883 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
3885 post_error ("cannot return type whose size overflows",
3886 gnat_entity);
3887 gnu_return_type = copy_node (gnu_return_type);
3888 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
3889 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
3890 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
3891 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
3894 /* Look at all our parameters and get the type of
3895 each. While doing this, build a copy-out structure if
3896 we need one. */
3898 /* Loop over the parameters and get their associated GCC tree.
3899 While doing this, build a copy-out structure if we need one. */
3900 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
3901 Present (gnat_param);
3902 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3904 tree gnu_param_name = get_entity_name (gnat_param);
3905 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3906 tree gnu_param, gnu_field;
3907 bool copy_in_copy_out = false;
3908 Mechanism_Type mech = Mechanism (gnat_param);
3910 /* Builtins are expanded inline and there is no real call sequence
3911 involved. So the type expected by the underlying expander is
3912 always the type of each argument "as is". */
3913 if (gnu_builtin_decl)
3914 mech = By_Copy;
3915 /* Handle the first parameter of a valued procedure specially. */
3916 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3917 mech = By_Copy_Return;
3918 /* Otherwise, see if a Mechanism was supplied that forced this
3919 parameter to be passed one way or another. */
3920 else if (mech == Default
3921 || mech == By_Copy || mech == By_Reference)
3923 else if (By_Descriptor_Last <= mech && mech <= By_Descriptor)
3924 mech = By_Descriptor;
3926 else if (By_Short_Descriptor_Last <= mech &&
3927 mech <= By_Short_Descriptor)
3928 mech = By_Short_Descriptor;
3930 else if (mech > 0)
3932 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3933 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3934 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3935 mech))
3936 mech = By_Reference;
3937 else
3938 mech = By_Copy;
3940 else
3942 post_error ("unsupported mechanism for&", gnat_param);
3943 mech = Default;
3946 gnu_param
3947 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
3948 Has_Foreign_Convention (gnat_entity),
3949 &copy_in_copy_out);
3951 /* We are returned either a PARM_DECL or a type if no parameter
3952 needs to be passed; in either case, adjust the type. */
3953 if (DECL_P (gnu_param))
3954 gnu_param_type = TREE_TYPE (gnu_param);
3955 else
3957 gnu_param_type = gnu_param;
3958 gnu_param = NULL_TREE;
3961 if (gnu_param)
3963 /* If it's an exported subprogram, we build a parameter list
3964 in parallel, in case we need to emit a stub for it. */
3965 if (Is_Exported (gnat_entity))
3967 gnu_stub_param_list
3968 = chainon (gnu_param, gnu_stub_param_list);
3969 /* Change By_Descriptor parameter to By_Reference for
3970 the internal version of an exported subprogram. */
3971 if (mech == By_Descriptor || mech == By_Short_Descriptor)
3973 gnu_param
3974 = gnat_to_gnu_param (gnat_param, By_Reference,
3975 gnat_entity, false,
3976 &copy_in_copy_out);
3977 has_stub = true;
3979 else
3980 gnu_param = copy_node (gnu_param);
3983 gnu_param_list = chainon (gnu_param, gnu_param_list);
3984 Sloc_to_locus (Sloc (gnat_param),
3985 &DECL_SOURCE_LOCATION (gnu_param));
3986 save_gnu_tree (gnat_param, gnu_param, false);
3988 /* If a parameter is a pointer, this function may modify
3989 memory through it and thus shouldn't be considered
3990 a const function. Also, the memory may be modified
3991 between two calls, so they can't be CSE'ed. The latter
3992 case also handles by-ref parameters. */
3993 if (POINTER_TYPE_P (gnu_param_type)
3994 || TYPE_FAT_POINTER_P (gnu_param_type))
3995 const_flag = false;
3998 if (copy_in_copy_out)
4000 if (!has_copy_in_out)
4002 gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
4003 gnu_return_type = make_node (RECORD_TYPE);
4004 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4005 has_copy_in_out = true;
4008 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
4009 gnu_return_type, 0, 0, 0, 0);
4010 Sloc_to_locus (Sloc (gnat_param),
4011 &DECL_SOURCE_LOCATION (gnu_field));
4012 TREE_CHAIN (gnu_field) = gnu_field_list;
4013 gnu_field_list = gnu_field;
4014 gnu_return_list = tree_cons (gnu_field, gnu_param,
4015 gnu_return_list);
4019 /* Do not compute record for out parameters if subprogram is
4020 stubbed since structures are incomplete for the back-end. */
4021 if (gnu_field_list && Convention (gnat_entity) != Convention_Stubbed)
4022 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4023 0, false);
4025 /* If we have a CICO list but it has only one entry, we convert
4026 this function into a function that simply returns that one
4027 object. */
4028 if (list_length (gnu_return_list) == 1)
4029 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
4031 if (Has_Stdcall_Convention (gnat_entity))
4032 prepend_one_attribute_to
4033 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4034 get_identifier ("stdcall"), NULL_TREE,
4035 gnat_entity);
4037 /* If we are on a target where stack realignment is needed for 'main'
4038 to honor GCC's implicit expectations (stack alignment greater than
4039 what the base ABI guarantees), ensure we do the same for foreign
4040 convention subprograms as they might be used as callbacks from code
4041 breaking such expectations. Note that this applies to task entry
4042 points in particular. */
4043 if (FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
4044 && Has_Foreign_Convention (gnat_entity))
4045 prepend_one_attribute_to
4046 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4047 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4048 gnat_entity);
4050 /* The lists have been built in reverse. */
4051 gnu_param_list = nreverse (gnu_param_list);
4052 if (has_stub)
4053 gnu_stub_param_list = nreverse (gnu_stub_param_list);
4054 gnu_return_list = nreverse (gnu_return_list);
4056 if (Ekind (gnat_entity) == E_Function)
4057 Set_Mechanism (gnat_entity,
4058 (returns_by_ref || returns_unconstrained
4059 ? By_Reference : By_Copy));
4060 gnu_type
4061 = create_subprog_type (gnu_return_type, gnu_param_list,
4062 gnu_return_list, returns_unconstrained,
4063 returns_by_ref, returns_by_target_ptr);
4065 if (has_stub)
4066 gnu_stub_type
4067 = create_subprog_type (gnu_return_type, gnu_stub_param_list,
4068 gnu_return_list, returns_unconstrained,
4069 returns_by_ref, returns_by_target_ptr);
4071 /* A subprogram (something that doesn't return anything) shouldn't
4072 be considered const since there would be no reason for such a
4073 subprogram. Note that procedures with Out (or In Out) parameters
4074 have already been converted into a function with a return type. */
4075 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4076 const_flag = false;
4078 gnu_type
4079 = build_qualified_type (gnu_type,
4080 TYPE_QUALS (gnu_type)
4081 | (TYPE_QUAL_CONST * const_flag)
4082 | (TYPE_QUAL_VOLATILE * volatile_flag));
4084 Sloc_to_locus (Sloc (gnat_entity), &input_location);
4086 if (has_stub)
4087 gnu_stub_type
4088 = build_qualified_type (gnu_stub_type,
4089 TYPE_QUALS (gnu_stub_type)
4090 | (TYPE_QUAL_CONST * const_flag)
4091 | (TYPE_QUAL_VOLATILE * volatile_flag));
4093 /* If we have a builtin decl for that function, check the signatures
4094 compatibilities. If the signatures are compatible, use the builtin
4095 decl. If they are not, we expect the checker predicate to have
4096 posted the appropriate errors, and just continue with what we have
4097 so far. */
4098 if (gnu_builtin_decl)
4100 tree gnu_builtin_type = TREE_TYPE (gnu_builtin_decl);
4102 if (compatible_signatures_p (gnu_type, gnu_builtin_type))
4104 gnu_decl = gnu_builtin_decl;
4105 gnu_type = gnu_builtin_type;
4106 break;
4110 /* If there was no specified Interface_Name and the external and
4111 internal names of the subprogram are the same, only use the
4112 internal name to allow disambiguation of nested subprograms. */
4113 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_id)
4114 gnu_ext_name = NULL_TREE;
4116 /* If we are defining the subprogram and it has an Address clause
4117 we must get the address expression from the saved GCC tree for the
4118 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4119 the address expression here since the front-end has guaranteed
4120 in that case that the elaboration has no effects. If there is
4121 an Address clause and we are not defining the object, just
4122 make it a constant. */
4123 if (Present (Address_Clause (gnat_entity)))
4125 tree gnu_address = NULL_TREE;
4127 if (definition)
4128 gnu_address
4129 = (present_gnu_tree (gnat_entity)
4130 ? get_gnu_tree (gnat_entity)
4131 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4133 save_gnu_tree (gnat_entity, NULL_TREE, false);
4135 /* Convert the type of the object to a reference type that can
4136 alias everything as per 13.3(19). */
4137 gnu_type
4138 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4139 if (gnu_address)
4140 gnu_address = convert (gnu_type, gnu_address);
4142 gnu_decl
4143 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
4144 gnu_address, false, Is_Public (gnat_entity),
4145 extern_flag, false, NULL, gnat_entity);
4146 DECL_BY_REF_P (gnu_decl) = 1;
4149 else if (kind == E_Subprogram_Type)
4150 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4151 !Comes_From_Source (gnat_entity),
4152 debug_info_p, gnat_entity);
4153 else
4155 if (has_stub)
4157 gnu_stub_name = gnu_ext_name;
4158 gnu_ext_name = create_concat_name (gnat_entity, "internal");
4159 public_flag = false;
4162 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
4163 gnu_type, gnu_param_list,
4164 inline_flag, public_flag,
4165 extern_flag, attr_list,
4166 gnat_entity);
4167 if (has_stub)
4169 tree gnu_stub_decl
4170 = create_subprog_decl (gnu_entity_id, gnu_stub_name,
4171 gnu_stub_type, gnu_stub_param_list,
4172 inline_flag, true,
4173 extern_flag, attr_list,
4174 gnat_entity);
4175 SET_DECL_FUNCTION_STUB (gnu_decl, gnu_stub_decl);
4178 /* This is unrelated to the stub built right above. */
4179 DECL_STUBBED_P (gnu_decl)
4180 = Convention (gnat_entity) == Convention_Stubbed;
4183 break;
4185 case E_Incomplete_Type:
4186 case E_Incomplete_Subtype:
4187 case E_Private_Type:
4188 case E_Private_Subtype:
4189 case E_Limited_Private_Type:
4190 case E_Limited_Private_Subtype:
4191 case E_Record_Type_With_Private:
4192 case E_Record_Subtype_With_Private:
4194 /* Get the "full view" of this entity. If this is an incomplete
4195 entity from a limited with, treat its non-limited view as the
4196 full view. Otherwise, use either the full view or the underlying
4197 full view, whichever is present. This is used in all the tests
4198 below. */
4199 Entity_Id full_view
4200 = (IN (Ekind (gnat_entity), Incomplete_Kind)
4201 && From_With_Type (gnat_entity))
4202 ? Non_Limited_View (gnat_entity)
4203 : Present (Full_View (gnat_entity))
4204 ? Full_View (gnat_entity)
4205 : Underlying_Full_View (gnat_entity);
4207 /* If this is an incomplete type with no full view, it must be a Taft
4208 Amendment type, in which case we return a dummy type. Otherwise,
4209 just get the type from its Etype. */
4210 if (No (full_view))
4212 if (kind == E_Incomplete_Type)
4213 gnu_type = make_dummy_type (gnat_entity);
4214 else
4216 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4217 NULL_TREE, 0);
4218 maybe_present = true;
4220 break;
4223 /* If we already made a type for the full view, reuse it. */
4224 else if (present_gnu_tree (full_view))
4226 gnu_decl = get_gnu_tree (full_view);
4227 break;
4230 /* Otherwise, if we are not defining the type now, get the type
4231 from the full view. But always get the type from the full view
4232 for define on use types, since otherwise we won't see them! */
4233 else if (!definition
4234 || (Is_Itype (full_view)
4235 && No (Freeze_Node (gnat_entity)))
4236 || (Is_Itype (gnat_entity)
4237 && No (Freeze_Node (full_view))))
4239 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4240 maybe_present = true;
4241 break;
4244 /* For incomplete types, make a dummy type entry which will be
4245 replaced later. */
4246 gnu_type = make_dummy_type (gnat_entity);
4248 /* Save this type as the full declaration's type so we can do any
4249 needed updates when we see it. */
4250 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4251 !Comes_From_Source (gnat_entity),
4252 debug_info_p, gnat_entity);
4253 save_gnu_tree (full_view, gnu_decl, 0);
4254 break;
4257 /* Simple class_wide types are always viewed as their root_type
4258 by Gigi unless an Equivalent_Type is specified. */
4259 case E_Class_Wide_Type:
4260 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4261 maybe_present = true;
4262 break;
4264 case E_Task_Type:
4265 case E_Task_Subtype:
4266 case E_Protected_Type:
4267 case E_Protected_Subtype:
4268 if (type_annotate_only && No (gnat_equiv_type))
4269 gnu_type = void_type_node;
4270 else
4271 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4273 maybe_present = true;
4274 break;
4276 case E_Label:
4277 gnu_decl = create_label_decl (gnu_entity_id);
4278 break;
4280 case E_Block:
4281 case E_Loop:
4282 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4283 we've already saved it, so we don't try to. */
4284 gnu_decl = error_mark_node;
4285 saved = true;
4286 break;
4288 default:
4289 gcc_unreachable ();
4292 /* If we had a case where we evaluated another type and it might have
4293 defined this one, handle it here. */
4294 if (maybe_present && present_gnu_tree (gnat_entity))
4296 gnu_decl = get_gnu_tree (gnat_entity);
4297 saved = true;
4300 /* If we are processing a type and there is either no decl for it or
4301 we just made one, do some common processing for the type, such as
4302 handling alignment and possible padding. */
4304 if ((!gnu_decl || this_made_decl) && IN (kind, Type_Kind))
4306 if (Is_Tagged_Type (gnat_entity)
4307 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4308 TYPE_ALIGN_OK (gnu_type) = 1;
4310 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4311 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4313 /* ??? Don't set the size for a String_Literal since it is either
4314 confirming or we don't handle it properly (if the low bound is
4315 non-constant). */
4316 if (!gnu_size && kind != E_String_Literal_Subtype)
4317 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
4318 TYPE_DECL, false,
4319 Has_Size_Clause (gnat_entity));
4321 /* If a size was specified, see if we can make a new type of that size
4322 by rearranging the type, for example from a fat to a thin pointer. */
4323 if (gnu_size)
4325 gnu_type
4326 = make_type_from_size (gnu_type, gnu_size,
4327 Has_Biased_Representation (gnat_entity));
4329 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4330 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4331 gnu_size = 0;
4334 /* If the alignment hasn't already been processed and this is
4335 not an unconstrained array, see if an alignment is specified.
4336 If not, we pick a default alignment for atomic objects. */
4337 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4339 else if (Known_Alignment (gnat_entity))
4341 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4342 TYPE_ALIGN (gnu_type));
4344 /* Warn on suspiciously large alignments. This should catch
4345 errors about the (alignment,byte)/(size,bit) discrepancy. */
4346 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4348 tree size;
4350 /* If a size was specified, take it into account. Otherwise
4351 use the RM size for records as the type size has already
4352 been adjusted to the alignment. */
4353 if (gnu_size)
4354 size = gnu_size;
4355 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
4356 || TREE_CODE (gnu_type) == UNION_TYPE
4357 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
4358 && !TYPE_IS_FAT_POINTER_P (gnu_type))
4359 size = rm_size (gnu_type);
4360 else
4361 size = TYPE_SIZE (gnu_type);
4363 /* Consider an alignment as suspicious if the alignment/size
4364 ratio is greater or equal to the byte/bit ratio. */
4365 if (host_integerp (size, 1)
4366 && align >= TREE_INT_CST_LOW (size) * BITS_PER_UNIT)
4367 post_error_ne ("?suspiciously large alignment specified for&",
4368 Expression (Alignment_Clause (gnat_entity)),
4369 gnat_entity);
4372 else if (Is_Atomic (gnat_entity) && !gnu_size
4373 && host_integerp (TYPE_SIZE (gnu_type), 1)
4374 && integer_pow2p (TYPE_SIZE (gnu_type)))
4375 align = MIN (BIGGEST_ALIGNMENT,
4376 tree_low_cst (TYPE_SIZE (gnu_type), 1));
4377 else if (Is_Atomic (gnat_entity) && gnu_size
4378 && host_integerp (gnu_size, 1)
4379 && integer_pow2p (gnu_size))
4380 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
4382 /* See if we need to pad the type. If we did, and made a record,
4383 the name of the new type may be changed. So get it back for
4384 us when we make the new TYPE_DECL below. */
4385 if (gnu_size || align > 0)
4386 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4387 "PAD", true, definition, false);
4389 if (TREE_CODE (gnu_type) == RECORD_TYPE
4390 && TYPE_IS_PADDING_P (gnu_type))
4392 gnu_entity_id = TYPE_NAME (gnu_type);
4393 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
4394 gnu_entity_id = DECL_NAME (gnu_entity_id);
4397 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4399 /* If we are at global level, GCC will have applied variable_size to
4400 the type, but that won't have done anything. So, if it's not
4401 a constant or self-referential, call elaborate_expression_1 to
4402 make a variable for the size rather than calculating it each time.
4403 Handle both the RM size and the actual size. */
4404 if (global_bindings_p ()
4405 && TYPE_SIZE (gnu_type)
4406 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4407 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4409 if (TREE_CODE (gnu_type) == RECORD_TYPE
4410 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
4411 TYPE_SIZE (gnu_type), 0))
4413 TYPE_SIZE (gnu_type)
4414 = elaborate_expression_1 (gnat_entity, gnat_entity,
4415 TYPE_SIZE (gnu_type),
4416 get_identifier ("SIZE"),
4417 definition, 0);
4418 SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
4420 else
4422 TYPE_SIZE (gnu_type)
4423 = elaborate_expression_1 (gnat_entity, gnat_entity,
4424 TYPE_SIZE (gnu_type),
4425 get_identifier ("SIZE"),
4426 definition, 0);
4428 /* ??? For now, store the size as a multiple of the alignment
4429 in bytes so that we can see the alignment from the tree. */
4430 TYPE_SIZE_UNIT (gnu_type)
4431 = build_binary_op
4432 (MULT_EXPR, sizetype,
4433 elaborate_expression_1
4434 (gnat_entity, gnat_entity,
4435 build_binary_op (EXACT_DIV_EXPR, sizetype,
4436 TYPE_SIZE_UNIT (gnu_type),
4437 size_int (TYPE_ALIGN (gnu_type)
4438 / BITS_PER_UNIT)),
4439 get_identifier ("SIZE_A_UNIT"),
4440 definition, 0),
4441 size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4443 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4444 SET_TYPE_ADA_SIZE
4445 (gnu_type,
4446 elaborate_expression_1 (gnat_entity,
4447 gnat_entity,
4448 TYPE_ADA_SIZE (gnu_type),
4449 get_identifier ("RM_SIZE"),
4450 definition, 0));
4454 /* If this is a record type or subtype, call elaborate_expression_1 on
4455 any field position. Do this for both global and local types.
4456 Skip any fields that we haven't made trees for to avoid problems with
4457 class wide types. */
4458 if (IN (kind, Record_Kind))
4459 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4460 gnat_temp = Next_Entity (gnat_temp))
4461 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4463 tree gnu_field = get_gnu_tree (gnat_temp);
4465 /* ??? Unfortunately, GCC needs to be able to prove the
4466 alignment of this offset and if it's a variable, it can't.
4467 In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
4468 right now, we have to put in an explicit multiply and
4469 divide by that value. */
4470 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4472 DECL_FIELD_OFFSET (gnu_field)
4473 = build_binary_op
4474 (MULT_EXPR, sizetype,
4475 elaborate_expression_1
4476 (gnat_temp, gnat_temp,
4477 build_binary_op (EXACT_DIV_EXPR, sizetype,
4478 DECL_FIELD_OFFSET (gnu_field),
4479 size_int (DECL_OFFSET_ALIGN (gnu_field)
4480 / BITS_PER_UNIT)),
4481 get_identifier ("OFFSET"),
4482 definition, 0),
4483 size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
4485 /* ??? The context of gnu_field is not necessarily gnu_type so
4486 the MULT_EXPR node built above may not be marked by the call
4487 to create_type_decl below. */
4488 if (global_bindings_p ())
4489 mark_visited (&DECL_FIELD_OFFSET (gnu_field));
4493 gnu_type = build_qualified_type (gnu_type,
4494 (TYPE_QUALS (gnu_type)
4495 | (TYPE_QUAL_VOLATILE
4496 * Treat_As_Volatile (gnat_entity))));
4498 if (Is_Atomic (gnat_entity))
4499 check_ok_for_atomic (gnu_type, gnat_entity, false);
4501 if (Present (Alignment_Clause (gnat_entity)))
4502 TYPE_USER_ALIGN (gnu_type) = 1;
4504 if (Universal_Aliasing (gnat_entity))
4505 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
4507 if (!gnu_decl)
4508 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4509 !Comes_From_Source (gnat_entity),
4510 debug_info_p, gnat_entity);
4511 else
4512 TREE_TYPE (gnu_decl) = gnu_type;
4515 if (IN (kind, Type_Kind) && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4517 gnu_type = TREE_TYPE (gnu_decl);
4519 /* Back-annotate the Alignment of the type if not already in the
4520 tree. Likewise for sizes. */
4521 if (Unknown_Alignment (gnat_entity))
4522 Set_Alignment (gnat_entity,
4523 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4525 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4527 /* If the size is self-referential, we annotate the maximum
4528 value of that size. */
4529 tree gnu_size = TYPE_SIZE (gnu_type);
4531 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4532 gnu_size = max_size (gnu_size, true);
4534 Set_Esize (gnat_entity, annotate_value (gnu_size));
4536 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4538 /* In this mode the tag and the parent components are not
4539 generated by the front-end, so the sizes must be adjusted
4540 explicitly now. */
4541 int size_offset, new_size;
4543 if (Is_Derived_Type (gnat_entity))
4545 size_offset
4546 = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
4547 Set_Alignment (gnat_entity,
4548 Alignment (Etype (Base_Type (gnat_entity))));
4550 else
4551 size_offset = POINTER_SIZE;
4553 new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
4554 Set_Esize (gnat_entity,
4555 UI_From_Int (((new_size + (POINTER_SIZE - 1))
4556 / POINTER_SIZE) * POINTER_SIZE));
4557 Set_RM_Size (gnat_entity, Esize (gnat_entity));
4561 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4562 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4565 if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4566 DECL_ARTIFICIAL (gnu_decl) = 1;
4568 if (!debug_info_p && DECL_P (gnu_decl)
4569 && TREE_CODE (gnu_decl) != FUNCTION_DECL
4570 && No (Renamed_Object (gnat_entity)))
4571 DECL_IGNORED_P (gnu_decl) = 1;
4573 /* If we haven't already, associate the ..._DECL node that we just made with
4574 the input GNAT entity node. */
4575 if (!saved)
4576 save_gnu_tree (gnat_entity, gnu_decl, false);
4578 /* If this is an enumeral or floating-point type, we were not able to set
4579 the bounds since they refer to the type. These bounds are always static.
4581 For enumeration types, also write debugging information and declare the
4582 enumeration literal table, if needed. */
4584 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4585 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4587 tree gnu_scalar_type = gnu_type;
4589 /* If this is a padded type, we need to use the underlying type. */
4590 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
4591 && TYPE_IS_PADDING_P (gnu_scalar_type))
4592 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4594 /* If this is a floating point type and we haven't set a floating
4595 point type yet, use this in the evaluation of the bounds. */
4596 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4597 longest_float_type_node = gnu_type;
4599 TYPE_MIN_VALUE (gnu_scalar_type)
4600 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4601 TYPE_MAX_VALUE (gnu_scalar_type)
4602 = gnat_to_gnu (Type_High_Bound (gnat_entity));
4604 if (TREE_CODE (gnu_scalar_type) == ENUMERAL_TYPE)
4606 /* Since this has both a typedef and a tag, avoid outputting
4607 the name twice. */
4608 DECL_ARTIFICIAL (gnu_decl) = 1;
4609 rest_of_type_decl_compilation (gnu_decl);
4613 /* If we deferred processing of incomplete types, re-enable it. If there
4614 were no other disables and we have some to process, do so. */
4615 if (this_deferred && --defer_incomplete_level == 0)
4617 if (defer_incomplete_list)
4619 struct incomplete *incp, *next;
4621 /* We are back to level 0 for the deferring of incomplete types.
4622 But processing these incomplete types below may itself require
4623 deferring, so preserve what we have and restart from scratch. */
4624 incp = defer_incomplete_list;
4625 defer_incomplete_list = NULL;
4627 /* For finalization, however, all types must be complete so we
4628 cannot do the same because deferred incomplete types may end up
4629 referencing each other. Process them all recursively first. */
4630 defer_finalize_level++;
4632 for (; incp; incp = next)
4634 next = incp->next;
4636 if (incp->old_type)
4637 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4638 gnat_to_gnu_type (incp->full_type));
4639 free (incp);
4642 defer_finalize_level--;
4645 /* All the deferred incomplete types have been processed so we can
4646 now proceed with the finalization of the deferred types. */
4647 if (defer_finalize_level == 0 && defer_finalize_list)
4649 unsigned int i;
4650 tree t;
4652 for (i = 0; VEC_iterate (tree, defer_finalize_list, i, t); i++)
4653 rest_of_type_decl_compilation_no_defer (t);
4655 VEC_free (tree, heap, defer_finalize_list);
4659 /* If we are not defining this type, see if it's in the incomplete list.
4660 If so, handle that list entry now. */
4661 else if (!definition)
4663 struct incomplete *incp;
4665 for (incp = defer_incomplete_list; incp; incp = incp->next)
4666 if (incp->old_type && incp->full_type == gnat_entity)
4668 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4669 TREE_TYPE (gnu_decl));
4670 incp->old_type = NULL_TREE;
4674 if (this_global)
4675 force_global--;
4677 if (Is_Packed_Array_Type (gnat_entity)
4678 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
4679 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
4680 && !present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
4681 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
4683 return gnu_decl;
4686 /* Similar, but if the returned value is a COMPONENT_REF, return the
4687 FIELD_DECL. */
4689 tree
4690 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4692 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4694 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4695 gnu_field = TREE_OPERAND (gnu_field, 1);
4697 return gnu_field;
4700 /* Wrap up compilation of DECL, a TYPE_DECL, possibly deferring it.
4701 Every TYPE_DECL generated for a type definition must be passed
4702 to this function once everything else has been done for it. */
4704 void
4705 rest_of_type_decl_compilation (tree decl)
4707 /* We need to defer finalizing the type if incomplete types
4708 are being deferred or if they are being processed. */
4709 if (defer_incomplete_level || defer_finalize_level)
4710 VEC_safe_push (tree, heap, defer_finalize_list, decl);
4711 else
4712 rest_of_type_decl_compilation_no_defer (decl);
4715 /* Same as above but without deferring the compilation. This
4716 function should not be invoked directly on a TYPE_DECL. */
4718 static void
4719 rest_of_type_decl_compilation_no_defer (tree decl)
4721 const int toplev = global_bindings_p ();
4722 tree t = TREE_TYPE (decl);
4724 rest_of_decl_compilation (decl, toplev, 0);
4726 /* Now process all the variants. This is needed for STABS. */
4727 for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
4729 if (t == TREE_TYPE (decl))
4730 continue;
4732 if (!TYPE_STUB_DECL (t))
4734 TYPE_STUB_DECL (t) = build_decl (TYPE_DECL, DECL_NAME (decl), t);
4735 DECL_ARTIFICIAL (TYPE_STUB_DECL (t)) = 1;
4738 rest_of_type_compilation (t, toplev);
4742 /* Finalize any From_With_Type incomplete types. We do this after processing
4743 our compilation unit and after processing its spec, if this is a body. */
4745 void
4746 finalize_from_with_types (void)
4748 struct incomplete *incp = defer_limited_with;
4749 struct incomplete *next;
4751 defer_limited_with = 0;
4752 for (; incp; incp = next)
4754 next = incp->next;
4756 if (incp->old_type != 0)
4757 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4758 gnat_to_gnu_type (incp->full_type));
4759 free (incp);
4763 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
4764 kind of type (such E_Task_Type) that has a different type which Gigi
4765 uses for its representation. If the type does not have a special type
4766 for its representation, return GNAT_ENTITY. If a type is supposed to
4767 exist, but does not, abort unless annotating types, in which case
4768 return Empty. If GNAT_ENTITY is Empty, return Empty. */
4770 Entity_Id
4771 Gigi_Equivalent_Type (Entity_Id gnat_entity)
4773 Entity_Id gnat_equiv = gnat_entity;
4775 if (No (gnat_entity))
4776 return gnat_entity;
4778 switch (Ekind (gnat_entity))
4780 case E_Class_Wide_Subtype:
4781 if (Present (Equivalent_Type (gnat_entity)))
4782 gnat_equiv = Equivalent_Type (gnat_entity);
4783 break;
4785 case E_Access_Protected_Subprogram_Type:
4786 case E_Anonymous_Access_Protected_Subprogram_Type:
4787 gnat_equiv = Equivalent_Type (gnat_entity);
4788 break;
4790 case E_Class_Wide_Type:
4791 gnat_equiv = ((Present (Equivalent_Type (gnat_entity)))
4792 ? Equivalent_Type (gnat_entity)
4793 : Root_Type (gnat_entity));
4794 break;
4796 case E_Task_Type:
4797 case E_Task_Subtype:
4798 case E_Protected_Type:
4799 case E_Protected_Subtype:
4800 gnat_equiv = Corresponding_Record_Type (gnat_entity);
4801 break;
4803 default:
4804 break;
4807 gcc_assert (Present (gnat_equiv) || type_annotate_only);
4808 return gnat_equiv;
4811 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
4812 using MECH as its passing mechanism, to be placed in the parameter
4813 list built for GNAT_SUBPROG. Assume a foreign convention for the
4814 latter if FOREIGN is true. Also set CICO to true if the parameter
4815 must use the copy-in copy-out implementation mechanism.
4817 The returned tree is a PARM_DECL, except for those cases where no
4818 parameter needs to be actually passed to the subprogram; the type
4819 of this "shadow" parameter is then returned instead. */
4821 static tree
4822 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
4823 Entity_Id gnat_subprog, bool foreign, bool *cico)
4825 tree gnu_param_name = get_entity_name (gnat_param);
4826 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
4827 tree gnu_param_type_alt = NULL_TREE;
4828 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
4829 /* The parameter can be indirectly modified if its address is taken. */
4830 bool ro_param = in_param && !Address_Taken (gnat_param);
4831 bool by_return = false, by_component_ptr = false, by_ref = false;
4832 tree gnu_param;
4834 /* Copy-return is used only for the first parameter of a valued procedure.
4835 It's a copy mechanism for which a parameter is never allocated. */
4836 if (mech == By_Copy_Return)
4838 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
4839 mech = By_Copy;
4840 by_return = true;
4843 /* If this is either a foreign function or if the underlying type won't
4844 be passed by reference, strip off possible padding type. */
4845 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
4846 && TYPE_IS_PADDING_P (gnu_param_type))
4848 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
4850 if (mech == By_Reference
4851 || foreign
4852 || (!must_pass_by_ref (unpadded_type)
4853 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
4854 gnu_param_type = unpadded_type;
4857 /* If this is a read-only parameter, make a variant of the type that is
4858 read-only. ??? However, if this is an unconstrained array, that type
4859 can be very complex, so skip it for now. Likewise for any other
4860 self-referential type. */
4861 if (ro_param
4862 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
4863 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
4864 gnu_param_type = build_qualified_type (gnu_param_type,
4865 (TYPE_QUALS (gnu_param_type)
4866 | TYPE_QUAL_CONST));
4868 /* For foreign conventions, pass arrays as pointers to the element type.
4869 First check for unconstrained array and get the underlying array. */
4870 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
4871 gnu_param_type
4872 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
4874 /* VMS descriptors are themselves passed by reference. */
4875 if (mech == By_Short_Descriptor ||
4876 (mech == By_Descriptor && TARGET_ABI_OPEN_VMS && !TARGET_MALLOC64))
4877 gnu_param_type
4878 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
4879 Mechanism (gnat_param),
4880 gnat_subprog));
4881 else if (mech == By_Descriptor)
4883 /* Build both a 32-bit and 64-bit descriptor, one of which will be
4884 chosen in fill_vms_descriptor. */
4885 gnu_param_type_alt
4886 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
4887 Mechanism (gnat_param),
4888 gnat_subprog));
4889 gnu_param_type
4890 = build_pointer_type (build_vms_descriptor (gnu_param_type,
4891 Mechanism (gnat_param),
4892 gnat_subprog));
4895 /* Arrays are passed as pointers to element type for foreign conventions. */
4896 else if (foreign
4897 && mech != By_Copy
4898 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
4900 /* Strip off any multi-dimensional entries, then strip
4901 off the last array to get the component type. */
4902 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
4903 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
4904 gnu_param_type = TREE_TYPE (gnu_param_type);
4906 by_component_ptr = true;
4907 gnu_param_type = TREE_TYPE (gnu_param_type);
4909 if (ro_param)
4910 gnu_param_type = build_qualified_type (gnu_param_type,
4911 (TYPE_QUALS (gnu_param_type)
4912 | TYPE_QUAL_CONST));
4914 gnu_param_type = build_pointer_type (gnu_param_type);
4917 /* Fat pointers are passed as thin pointers for foreign conventions. */
4918 else if (foreign && TYPE_FAT_POINTER_P (gnu_param_type))
4919 gnu_param_type
4920 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
4922 /* If we must pass or were requested to pass by reference, do so.
4923 If we were requested to pass by copy, do so.
4924 Otherwise, for foreign conventions, pass In Out or Out parameters
4925 or aggregates by reference. For COBOL and Fortran, pass all
4926 integer and FP types that way too. For Convention Ada, use
4927 the standard Ada default. */
4928 else if (must_pass_by_ref (gnu_param_type)
4929 || mech == By_Reference
4930 || (mech != By_Copy
4931 && ((foreign
4932 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
4933 || (foreign
4934 && (Convention (gnat_subprog) == Convention_Fortran
4935 || Convention (gnat_subprog) == Convention_COBOL)
4936 && (INTEGRAL_TYPE_P (gnu_param_type)
4937 || FLOAT_TYPE_P (gnu_param_type)))
4938 || (!foreign
4939 && default_pass_by_ref (gnu_param_type)))))
4941 gnu_param_type = build_reference_type (gnu_param_type);
4942 by_ref = true;
4945 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
4946 else if (!in_param)
4947 *cico = true;
4949 if (mech == By_Copy && (by_ref || by_component_ptr))
4950 post_error ("?cannot pass & by copy", gnat_param);
4952 /* If this is an Out parameter that isn't passed by reference and isn't
4953 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
4954 it will be a VAR_DECL created when we process the procedure, so just
4955 return its type. For the special parameter of a valued procedure,
4956 never pass it in.
4958 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
4959 Out parameters with discriminants or implicit initial values to be
4960 handled like In Out parameters. These type are normally built as
4961 aggregates, hence passed by reference, except for some packed arrays
4962 which end up encoded in special integer types.
4964 The exception we need to make is then for packed arrays of records
4965 with discriminants or implicit initial values. We have no light/easy
4966 way to check for the latter case, so we merely check for packed arrays
4967 of records. This may lead to useless copy-in operations, but in very
4968 rare cases only, as these would be exceptions in a set of already
4969 exceptional situations. */
4970 if (Ekind (gnat_param) == E_Out_Parameter
4971 && !by_ref
4972 && (by_return
4973 || (mech != By_Descriptor
4974 && mech != By_Short_Descriptor
4975 && !POINTER_TYPE_P (gnu_param_type)
4976 && !AGGREGATE_TYPE_P (gnu_param_type)))
4977 && !(Is_Array_Type (Etype (gnat_param))
4978 && Is_Packed (Etype (gnat_param))
4979 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
4980 return gnu_param_type;
4982 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
4983 ro_param || by_ref || by_component_ptr);
4984 DECL_BY_REF_P (gnu_param) = by_ref;
4985 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
4986 DECL_BY_DESCRIPTOR_P (gnu_param) = (mech == By_Descriptor ||
4987 mech == By_Short_Descriptor);
4988 DECL_POINTS_TO_READONLY_P (gnu_param)
4989 = (ro_param && (by_ref || by_component_ptr));
4991 /* Save the alternate descriptor type, if any. */
4992 if (gnu_param_type_alt)
4993 SET_DECL_PARM_ALT_TYPE (gnu_param, gnu_param_type_alt);
4995 /* If no Mechanism was specified, indicate what we're using, then
4996 back-annotate it. */
4997 if (mech == Default)
4998 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5000 Set_Mechanism (gnat_param, mech);
5001 return gnu_param;
5004 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5006 static bool
5007 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5009 while (Present (Corresponding_Discriminant (discr1)))
5010 discr1 = Corresponding_Discriminant (discr1);
5012 while (Present (Corresponding_Discriminant (discr2)))
5013 discr2 = Corresponding_Discriminant (discr2);
5015 return
5016 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5019 /* Return true if the array type specified by GNAT_TYPE and GNU_TYPE has
5020 a non-aliased component in the back-end sense. */
5022 static bool
5023 array_type_has_nonaliased_component (Entity_Id gnat_type, tree gnu_type)
5025 /* If the type below this is a multi-array type, then
5026 this does not have aliased components. */
5027 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5028 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5029 return true;
5031 if (Has_Aliased_Components (gnat_type))
5032 return false;
5034 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5037 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5038 be elaborated at the point of its definition, but do nothing else. */
5040 void
5041 elaborate_entity (Entity_Id gnat_entity)
5043 switch (Ekind (gnat_entity))
5045 case E_Signed_Integer_Subtype:
5046 case E_Modular_Integer_Subtype:
5047 case E_Enumeration_Subtype:
5048 case E_Ordinary_Fixed_Point_Subtype:
5049 case E_Decimal_Fixed_Point_Subtype:
5050 case E_Floating_Point_Subtype:
5052 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5053 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5055 /* ??? Tests for avoiding static constraint error expression
5056 is needed until the front stops generating bogus conversions
5057 on bounds of real types. */
5059 if (!Raises_Constraint_Error (gnat_lb))
5060 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5061 1, 0, Needs_Debug_Info (gnat_entity));
5062 if (!Raises_Constraint_Error (gnat_hb))
5063 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5064 1, 0, Needs_Debug_Info (gnat_entity));
5065 break;
5068 case E_Record_Type:
5070 Node_Id full_definition = Declaration_Node (gnat_entity);
5071 Node_Id record_definition = Type_Definition (full_definition);
5073 /* If this is a record extension, go a level further to find the
5074 record definition. */
5075 if (Nkind (record_definition) == N_Derived_Type_Definition)
5076 record_definition = Record_Extension_Part (record_definition);
5078 break;
5080 case E_Record_Subtype:
5081 case E_Private_Subtype:
5082 case E_Limited_Private_Subtype:
5083 case E_Record_Subtype_With_Private:
5084 if (Is_Constrained (gnat_entity)
5085 && Has_Discriminants (Base_Type (gnat_entity))
5086 && Present (Discriminant_Constraint (gnat_entity)))
5088 Node_Id gnat_discriminant_expr;
5089 Entity_Id gnat_field;
5091 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
5092 gnat_discriminant_expr
5093 = First_Elmt (Discriminant_Constraint (gnat_entity));
5094 Present (gnat_field);
5095 gnat_field = Next_Discriminant (gnat_field),
5096 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
5097 /* ??? For now, ignore access discriminants. */
5098 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
5099 elaborate_expression (Node (gnat_discriminant_expr),
5100 gnat_entity,
5101 get_entity_name (gnat_field), 1, 0, 0);
5103 break;
5108 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
5109 any entities on its entity chain similarly. */
5111 void
5112 mark_out_of_scope (Entity_Id gnat_entity)
5114 Entity_Id gnat_sub_entity;
5115 unsigned int kind = Ekind (gnat_entity);
5117 /* If this has an entity list, process all in the list. */
5118 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
5119 || IN (kind, Private_Kind)
5120 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
5121 || kind == E_Function || kind == E_Generic_Function
5122 || kind == E_Generic_Package || kind == E_Generic_Procedure
5123 || kind == E_Loop || kind == E_Operator || kind == E_Package
5124 || kind == E_Package_Body || kind == E_Procedure
5125 || kind == E_Record_Type || kind == E_Record_Subtype
5126 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
5127 for (gnat_sub_entity = First_Entity (gnat_entity);
5128 Present (gnat_sub_entity);
5129 gnat_sub_entity = Next_Entity (gnat_sub_entity))
5130 if (Scope (gnat_sub_entity) == gnat_entity
5131 && gnat_sub_entity != gnat_entity)
5132 mark_out_of_scope (gnat_sub_entity);
5134 /* Now clear this if it has been defined, but only do so if it isn't
5135 a subprogram or parameter. We could refine this, but it isn't
5136 worth it. If this is statically allocated, it is supposed to
5137 hang around out of cope. */
5138 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
5139 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
5141 save_gnu_tree (gnat_entity, NULL_TREE, true);
5142 save_gnu_tree (gnat_entity, error_mark_node, true);
5146 /* Set the alias set of GNU_NEW_TYPE to be that of GNU_OLD_TYPE. If this
5147 is a multi-dimensional array type, do this recursively. */
5149 static void
5150 copy_alias_set (tree gnu_new_type, tree gnu_old_type)
5152 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
5153 of a one-dimensional array, since the padding has the same alias set
5154 as the field type, but if it's a multi-dimensional array, we need to
5155 see the inner types. */
5156 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
5157 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
5158 || TYPE_IS_PADDING_P (gnu_old_type)))
5159 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
5161 /* We need to be careful here in case GNU_OLD_TYPE is an unconstrained
5162 array. In that case, it doesn't have the same shape as GNU_NEW_TYPE,
5163 so we need to go down to what does. */
5164 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
5165 gnu_old_type
5166 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
5168 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
5169 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
5170 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
5171 copy_alias_set (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type));
5173 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
5174 record_component_aliases (gnu_new_type);
5177 /* Return a TREE_LIST describing the substitutions needed to reflect
5178 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
5179 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
5180 of GNAT_SUBTYPE. The substitutions can be in any order. TREE_PURPOSE
5181 gives the tree for the discriminant and TREE_VALUES is the replacement
5182 value. They are in the form of operands to substitute_in_expr.
5183 DEFINITION is as in gnat_to_gnu_entity. */
5185 static tree
5186 substitution_list (Entity_Id gnat_subtype, Entity_Id gnat_type,
5187 tree gnu_list, bool definition)
5189 Entity_Id gnat_discrim;
5190 Node_Id gnat_value;
5192 if (No (gnat_type))
5193 gnat_type = Implementation_Base_Type (gnat_subtype);
5195 if (Has_Discriminants (gnat_type))
5196 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
5197 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
5198 Present (gnat_discrim);
5199 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
5200 gnat_value = Next_Elmt (gnat_value))
5201 /* Ignore access discriminants. */
5202 if (!Is_Access_Type (Etype (Node (gnat_value))))
5203 gnu_list = tree_cons (gnat_to_gnu_field_decl (gnat_discrim),
5204 elaborate_expression
5205 (Node (gnat_value), gnat_subtype,
5206 get_entity_name (gnat_discrim), definition,
5207 1, 0),
5208 gnu_list);
5210 return gnu_list;
5213 /* Return true if the size represented by GNU_SIZE can be handled by an
5214 allocation. If STATIC_P is true, consider only what can be done with a
5215 static allocation. */
5217 static bool
5218 allocatable_size_p (tree gnu_size, bool static_p)
5220 HOST_WIDE_INT our_size;
5222 /* If this is not a static allocation, the only case we want to forbid
5223 is an overflowing size. That will be converted into a raise a
5224 Storage_Error. */
5225 if (!static_p)
5226 return !(TREE_CODE (gnu_size) == INTEGER_CST
5227 && TREE_OVERFLOW (gnu_size));
5229 /* Otherwise, we need to deal with both variable sizes and constant
5230 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
5231 since assemblers may not like very large sizes. */
5232 if (!host_integerp (gnu_size, 1))
5233 return false;
5235 our_size = tree_low_cst (gnu_size, 1);
5236 return (int) our_size == our_size;
5239 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5240 NAME, ARGS and ERROR_POINT. */
5242 static void
5243 prepend_one_attribute_to (struct attrib ** attr_list,
5244 enum attr_type attr_type,
5245 tree attr_name,
5246 tree attr_args,
5247 Node_Id attr_error_point)
5249 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
5251 attr->type = attr_type;
5252 attr->name = attr_name;
5253 attr->args = attr_args;
5254 attr->error_point = attr_error_point;
5256 attr->next = *attr_list;
5257 *attr_list = attr;
5260 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
5262 static void
5263 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
5265 Node_Id gnat_temp;
5267 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
5268 gnat_temp = Next_Rep_Item (gnat_temp))
5269 if (Nkind (gnat_temp) == N_Pragma)
5271 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
5272 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
5273 enum attr_type etype;
5275 if (Present (gnat_assoc) && Present (First (gnat_assoc))
5276 && Present (Next (First (gnat_assoc)))
5277 && (Nkind (Expression (Next (First (gnat_assoc))))
5278 == N_String_Literal))
5280 gnu_arg0 = get_identifier (TREE_STRING_POINTER
5281 (gnat_to_gnu
5282 (Expression (Next
5283 (First (gnat_assoc))))));
5284 if (Present (Next (Next (First (gnat_assoc))))
5285 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
5286 == N_String_Literal))
5287 gnu_arg1 = get_identifier (TREE_STRING_POINTER
5288 (gnat_to_gnu
5289 (Expression
5290 (Next (Next
5291 (First (gnat_assoc)))))));
5294 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_temp))))
5296 case Pragma_Machine_Attribute:
5297 etype = ATTR_MACHINE_ATTRIBUTE;
5298 break;
5300 case Pragma_Linker_Alias:
5301 etype = ATTR_LINK_ALIAS;
5302 break;
5304 case Pragma_Linker_Section:
5305 etype = ATTR_LINK_SECTION;
5306 break;
5308 case Pragma_Linker_Constructor:
5309 etype = ATTR_LINK_CONSTRUCTOR;
5310 break;
5312 case Pragma_Linker_Destructor:
5313 etype = ATTR_LINK_DESTRUCTOR;
5314 break;
5316 case Pragma_Weak_External:
5317 etype = ATTR_WEAK_EXTERNAL;
5318 break;
5320 default:
5321 continue;
5325 /* Prepend to the list now. Make a list of the argument we might
5326 have, as GCC expects it. */
5327 prepend_one_attribute_to
5328 (attr_list,
5329 etype, gnu_arg0,
5330 (gnu_arg1 != NULL_TREE)
5331 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
5332 Present (Next (First (gnat_assoc)))
5333 ? Expression (Next (First (gnat_assoc))) : gnat_temp);
5337 /* Get the unpadded version of a GNAT type. */
5339 tree
5340 get_unpadded_type (Entity_Id gnat_entity)
5342 tree type = gnat_to_gnu_type (gnat_entity);
5344 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5345 type = TREE_TYPE (TYPE_FIELDS (type));
5347 return type;
5350 /* Called when we need to protect a variable object using a save_expr. */
5352 tree
5353 maybe_variable (tree gnu_operand)
5355 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
5356 || TREE_CODE (gnu_operand) == SAVE_EXPR
5357 || TREE_CODE (gnu_operand) == NULL_EXPR)
5358 return gnu_operand;
5360 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
5362 tree gnu_result = build1 (UNCONSTRAINED_ARRAY_REF,
5363 TREE_TYPE (gnu_operand),
5364 variable_size (TREE_OPERAND (gnu_operand, 0)));
5366 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result)
5367 = TYPE_READONLY (TREE_TYPE (TREE_TYPE (gnu_operand)));
5368 return gnu_result;
5370 else
5371 return variable_size (gnu_operand);
5374 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
5375 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
5376 return the GCC tree to use for that expression. GNU_NAME is the
5377 qualification to use if an external name is appropriate and DEFINITION is
5378 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
5379 we need a result. Otherwise, we are just elaborating this for
5380 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
5381 purposes even if it isn't needed for code generation. */
5383 static tree
5384 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity,
5385 tree gnu_name, bool definition, bool need_value,
5386 bool need_debug)
5388 tree gnu_expr;
5390 /* If we already elaborated this expression (e.g., it was involved
5391 in the definition of a private type), use the old value. */
5392 if (present_gnu_tree (gnat_expr))
5393 return get_gnu_tree (gnat_expr);
5395 /* If we don't need a value and this is static or a discriminant, we
5396 don't need to do anything. */
5397 else if (!need_value
5398 && (Is_OK_Static_Expression (gnat_expr)
5399 || (Nkind (gnat_expr) == N_Identifier
5400 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
5401 return 0;
5403 /* Otherwise, convert this tree to its GCC equivalent. */
5404 gnu_expr
5405 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
5406 gnu_name, definition, need_debug);
5408 /* Save the expression in case we try to elaborate this entity again. Since
5409 it's not a DECL, don't check it. Don't save if it's a discriminant. */
5410 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
5411 save_gnu_tree (gnat_expr, gnu_expr, true);
5413 return need_value ? gnu_expr : error_mark_node;
5416 /* Similar, but take a GNU expression. */
5418 static tree
5419 elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
5420 tree gnu_expr, tree gnu_name, bool definition,
5421 bool need_debug)
5423 tree gnu_decl = NULL_TREE;
5424 /* Skip any conversions and simple arithmetics to see if the expression
5425 is a read-only variable.
5426 ??? This really should remain read-only, but we have to think about
5427 the typing of the tree here. */
5428 tree gnu_inner_expr
5429 = skip_simple_arithmetic (remove_conversions (gnu_expr, true));
5430 bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
5431 bool expr_variable;
5433 /* In most cases, we won't see a naked FIELD_DECL here because a
5434 discriminant reference will have been replaced with a COMPONENT_REF
5435 when the type is being elaborated. However, there are some cases
5436 involving child types where we will. So convert it to a COMPONENT_REF
5437 here. We have to hope it will be at the highest level of the
5438 expression in these cases. */
5439 if (TREE_CODE (gnu_expr) == FIELD_DECL)
5440 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
5441 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
5442 gnu_expr, NULL_TREE);
5444 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
5445 that is read-only, make a variable that is initialized to contain the
5446 bound when the package containing the definition is elaborated. If
5447 this entity is defined at top level and a bound or discriminant value
5448 isn't a constant or a reference to a discriminant, replace the bound
5449 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
5450 rely here on the fact that an expression cannot contain both the
5451 discriminant and some other variable. */
5453 expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
5454 && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
5455 && (TREE_READONLY (gnu_inner_expr)
5456 || DECL_READONLY_ONCE_ELAB (gnu_inner_expr)))
5457 && !CONTAINS_PLACEHOLDER_P (gnu_expr));
5459 /* If this is a static expression or contains a discriminant, we don't
5460 need the variable for debugging (and can't elaborate anyway if a
5461 discriminant). */
5462 if (need_debug
5463 && (Is_OK_Static_Expression (gnat_expr)
5464 || CONTAINS_PLACEHOLDER_P (gnu_expr)))
5465 need_debug = false;
5467 /* Now create the variable if we need it. */
5468 if (need_debug || (expr_variable && expr_global))
5469 gnu_decl
5470 = create_var_decl (create_concat_name (gnat_entity,
5471 IDENTIFIER_POINTER (gnu_name)),
5472 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
5473 !need_debug, Is_Public (gnat_entity),
5474 !definition, false, NULL, gnat_entity);
5476 /* We only need to use this variable if we are in global context since GCC
5477 can do the right thing in the local case. */
5478 if (expr_global && expr_variable)
5479 return gnu_decl;
5480 else if (!expr_variable)
5481 return gnu_expr;
5482 else
5483 return maybe_variable (gnu_expr);
5486 /* Create a record type that contains a SIZE bytes long field of TYPE with a
5487 starting bit position so that it is aligned to ALIGN bits, and leaving at
5488 least ROOM bytes free before the field. BASE_ALIGN is the alignment the
5489 record is guaranteed to get. */
5491 tree
5492 make_aligning_type (tree type, unsigned int align, tree size,
5493 unsigned int base_align, int room)
5495 /* We will be crafting a record type with one field at a position set to be
5496 the next multiple of ALIGN past record'address + room bytes. We use a
5497 record placeholder to express record'address. */
5499 tree record_type = make_node (RECORD_TYPE);
5500 tree record = build0 (PLACEHOLDER_EXPR, record_type);
5502 tree record_addr_st
5503 = convert (sizetype, build_unary_op (ADDR_EXPR, NULL_TREE, record));
5505 /* The diagram below summarizes the shape of what we manipulate:
5507 <--------- pos ---------->
5508 { +------------+-------------+-----------------+
5509 record =>{ |############| ... | field (type) |
5510 { +------------+-------------+-----------------+
5511 |<-- room -->|<- voffset ->|<---- size ----->|
5514 record_addr vblock_addr
5516 Every length is in sizetype bytes there, except "pos" which has to be
5517 set as a bit position in the GCC tree for the record. */
5519 tree room_st = size_int (room);
5520 tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
5521 tree voffset_st, pos, field;
5523 tree name = TYPE_NAME (type);
5525 if (TREE_CODE (name) == TYPE_DECL)
5526 name = DECL_NAME (name);
5528 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
5530 /* Compute VOFFSET and then POS. The next byte position multiple of some
5531 alignment after some address is obtained by "and"ing the alignment minus
5532 1 with the two's complement of the address. */
5534 voffset_st = size_binop (BIT_AND_EXPR,
5535 size_diffop (size_zero_node, vblock_addr_st),
5536 ssize_int ((align / BITS_PER_UNIT) - 1));
5538 /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype. */
5540 pos = size_binop (MULT_EXPR,
5541 convert (bitsizetype,
5542 size_binop (PLUS_EXPR, room_st, voffset_st)),
5543 bitsize_unit_node);
5545 /* Craft the GCC record representation. We exceptionally do everything
5546 manually here because 1) our generic circuitry is not quite ready to
5547 handle the complex position/size expressions we are setting up, 2) we
5548 have a strong simplifying factor at hand: we know the maximum possible
5549 value of voffset, and 3) we have to set/reset at least the sizes in
5550 accordance with this maximum value anyway, as we need them to convey
5551 what should be "alloc"ated for this type.
5553 Use -1 as the 'addressable' indication for the field to prevent the
5554 creation of a bitfield. We don't need one, it would have damaging
5555 consequences on the alignment computation, and create_field_decl would
5556 make one without this special argument, for instance because of the
5557 complex position expression. */
5559 field = create_field_decl (get_identifier ("F"), type, record_type,
5560 1, size, pos, -1);
5561 TYPE_FIELDS (record_type) = field;
5563 TYPE_ALIGN (record_type) = base_align;
5564 TYPE_USER_ALIGN (record_type) = 1;
5566 TYPE_SIZE (record_type)
5567 = size_binop (PLUS_EXPR,
5568 size_binop (MULT_EXPR, convert (bitsizetype, size),
5569 bitsize_unit_node),
5570 bitsize_int (align + room * BITS_PER_UNIT));
5571 TYPE_SIZE_UNIT (record_type)
5572 = size_binop (PLUS_EXPR, size,
5573 size_int (room + align / BITS_PER_UNIT));
5575 TYPE_MODE (record_type) = BLKmode;
5577 copy_alias_set (record_type, type);
5578 return record_type;
5581 /* Return the result of rounding T up to ALIGN. */
5583 static inline unsigned HOST_WIDE_INT
5584 round_up_to_align (unsigned HOST_WIDE_INT t, unsigned int align)
5586 t += align - 1;
5587 t /= align;
5588 t *= align;
5589 return t;
5592 /* TYPE is a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE that is being used
5593 as the field type of a packed record if IN_RECORD is true, or as the
5594 component type of a packed array if IN_RECORD is false. See if we can
5595 rewrite it either as a type that has a non-BLKmode, which we can pack
5596 tighter in the packed record case, or as a smaller type with BLKmode.
5597 If so, return the new type. If not, return the original type. */
5599 static tree
5600 make_packable_type (tree type, bool in_record)
5602 unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
5603 unsigned HOST_WIDE_INT new_size;
5604 tree new_type, old_field, field_list = NULL_TREE;
5606 /* No point in doing anything if the size is zero. */
5607 if (size == 0)
5608 return type;
5610 new_type = make_node (TREE_CODE (type));
5612 /* Copy the name and flags from the old type to that of the new.
5613 Note that we rely on the pointer equality created here for
5614 TYPE_NAME to look through conversions in various places. */
5615 TYPE_NAME (new_type) = TYPE_NAME (type);
5616 TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
5617 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
5618 if (TREE_CODE (type) == RECORD_TYPE)
5619 TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
5621 /* If we are in a record and have a small size, set the alignment to
5622 try for an integral mode. Otherwise set it to try for a smaller
5623 type with BLKmode. */
5624 if (in_record && size <= MAX_FIXED_MODE_SIZE)
5626 TYPE_ALIGN (new_type) = ceil_alignment (size);
5627 new_size = round_up_to_align (size, TYPE_ALIGN (new_type));
5629 else
5631 unsigned HOST_WIDE_INT align;
5633 /* Do not try to shrink the size if the RM size is not constant. */
5634 if (TYPE_CONTAINS_TEMPLATE_P (type)
5635 || !host_integerp (TYPE_ADA_SIZE (type), 1))
5636 return type;
5638 /* Round the RM size up to a unit boundary to get the minimal size
5639 for a BLKmode record. Give up if it's already the size. */
5640 new_size = TREE_INT_CST_LOW (TYPE_ADA_SIZE (type));
5641 new_size = round_up_to_align (new_size, BITS_PER_UNIT);
5642 if (new_size == size)
5643 return type;
5645 align = new_size & -new_size;
5646 TYPE_ALIGN (new_type) = MIN (TYPE_ALIGN (type), align);
5649 TYPE_USER_ALIGN (new_type) = 1;
5651 /* Now copy the fields, keeping the position and size as we don't want
5652 to change the layout by propagating the packedness downwards. */
5653 for (old_field = TYPE_FIELDS (type); old_field;
5654 old_field = TREE_CHAIN (old_field))
5656 tree new_field_type = TREE_TYPE (old_field);
5657 tree new_field, new_size;
5659 if (TYPE_MODE (new_field_type) == BLKmode
5660 && (TREE_CODE (new_field_type) == RECORD_TYPE
5661 || TREE_CODE (new_field_type) == UNION_TYPE
5662 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
5663 && host_integerp (TYPE_SIZE (new_field_type), 1))
5664 new_field_type = make_packable_type (new_field_type, true);
5666 /* However, for the last field in a not already packed record type
5667 that is of an aggregate type, we need to use the RM_Size in the
5668 packable version of the record type, see finish_record_type. */
5669 if (!TREE_CHAIN (old_field)
5670 && !TYPE_PACKED (type)
5671 && (TREE_CODE (new_field_type) == RECORD_TYPE
5672 || TREE_CODE (new_field_type) == UNION_TYPE
5673 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
5674 && !TYPE_IS_FAT_POINTER_P (new_field_type)
5675 && !TYPE_CONTAINS_TEMPLATE_P (new_field_type)
5676 && TYPE_ADA_SIZE (new_field_type))
5677 new_size = TYPE_ADA_SIZE (new_field_type);
5678 else
5679 new_size = DECL_SIZE (old_field);
5681 new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
5682 new_type, TYPE_PACKED (type), new_size,
5683 bit_position (old_field),
5684 !DECL_NONADDRESSABLE_P (old_field));
5686 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
5687 SET_DECL_ORIGINAL_FIELD
5688 (new_field, (DECL_ORIGINAL_FIELD (old_field)
5689 ? DECL_ORIGINAL_FIELD (old_field) : old_field));
5691 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
5692 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
5694 TREE_CHAIN (new_field) = field_list;
5695 field_list = new_field;
5698 finish_record_type (new_type, nreverse (field_list), 2, true);
5699 copy_alias_set (new_type, type);
5701 /* If this is a padding record, we never want to make the size smaller
5702 than what was specified. For QUAL_UNION_TYPE, also copy the size. */
5703 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5704 || TREE_CODE (type) == QUAL_UNION_TYPE)
5706 TYPE_SIZE (new_type) = TYPE_SIZE (type);
5707 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
5709 else
5711 TYPE_SIZE (new_type) = bitsize_int (new_size);
5712 TYPE_SIZE_UNIT (new_type)
5713 = size_int ((new_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
5716 if (!TYPE_CONTAINS_TEMPLATE_P (type))
5717 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (type));
5719 compute_record_mode (new_type);
5721 /* Try harder to get a packable type if necessary, for example
5722 in case the record itself contains a BLKmode field. */
5723 if (in_record && TYPE_MODE (new_type) == BLKmode)
5724 TYPE_MODE (new_type)
5725 = mode_for_size_tree (TYPE_SIZE (new_type), MODE_INT, 1);
5727 /* If neither the mode nor the size has shrunk, return the old type. */
5728 if (TYPE_MODE (new_type) == BLKmode && new_size >= size)
5729 return type;
5731 return new_type;
5734 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
5735 if needed. We have already verified that SIZE and TYPE are large enough.
5737 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
5738 to issue a warning.
5740 IS_USER_TYPE is true if we must complete the original type.
5742 DEFINITION is true if this type is being defined.
5744 SAME_RM_SIZE is true if the RM_Size of the resulting type is to be set
5745 to SIZE too; otherwise, it's set to the RM_Size of the original type. */
5747 tree
5748 maybe_pad_type (tree type, tree size, unsigned int align,
5749 Entity_Id gnat_entity, const char *name_trailer,
5750 bool is_user_type, bool definition, bool same_rm_size)
5752 tree orig_rm_size = same_rm_size ? NULL_TREE : rm_size (type);
5753 tree orig_size = TYPE_SIZE (type);
5754 unsigned int orig_align = align;
5755 tree record, field;
5757 /* If TYPE is a padded type, see if it agrees with any size and alignment
5758 we were given. If so, return the original type. Otherwise, strip
5759 off the padding, since we will either be returning the inner type
5760 or repadding it. If no size or alignment is specified, use that of
5761 the original padded type. */
5762 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5764 if ((!size
5765 || operand_equal_p (round_up (size,
5766 MAX (align, TYPE_ALIGN (type))),
5767 round_up (TYPE_SIZE (type),
5768 MAX (align, TYPE_ALIGN (type))),
5770 && (align == 0 || align == TYPE_ALIGN (type)))
5771 return type;
5773 if (!size)
5774 size = TYPE_SIZE (type);
5775 if (align == 0)
5776 align = TYPE_ALIGN (type);
5778 type = TREE_TYPE (TYPE_FIELDS (type));
5779 orig_size = TYPE_SIZE (type);
5782 /* If the size is either not being changed or is being made smaller (which
5783 is not done here (and is only valid for bitfields anyway), show the size
5784 isn't changing. Likewise, clear the alignment if it isn't being
5785 changed. Then return if we aren't doing anything. */
5786 if (size
5787 && (operand_equal_p (size, orig_size, 0)
5788 || (TREE_CODE (orig_size) == INTEGER_CST
5789 && tree_int_cst_lt (size, orig_size))))
5790 size = NULL_TREE;
5792 if (align == TYPE_ALIGN (type))
5793 align = 0;
5795 if (align == 0 && !size)
5796 return type;
5798 /* If requested, complete the original type and give it a name. */
5799 if (is_user_type)
5800 create_type_decl (get_entity_name (gnat_entity), type,
5801 NULL, !Comes_From_Source (gnat_entity),
5802 !(TYPE_NAME (type)
5803 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
5804 && DECL_IGNORED_P (TYPE_NAME (type))),
5805 gnat_entity);
5807 /* We used to modify the record in place in some cases, but that could
5808 generate incorrect debugging information. So make a new record
5809 type and name. */
5810 record = make_node (RECORD_TYPE);
5811 TYPE_IS_PADDING_P (record) = 1;
5813 if (Present (gnat_entity))
5814 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
5816 TYPE_VOLATILE (record)
5817 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
5819 TYPE_ALIGN (record) = align;
5820 if (orig_align)
5821 TYPE_USER_ALIGN (record) = align;
5823 TYPE_SIZE (record) = size ? size : orig_size;
5824 TYPE_SIZE_UNIT (record)
5825 = convert (sizetype,
5826 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
5827 bitsize_unit_node));
5829 /* If we are changing the alignment and the input type is a record with
5830 BLKmode and a small constant size, try to make a form that has an
5831 integral mode. This might allow the padding record to also have an
5832 integral mode, which will be much more efficient. There is no point
5833 in doing so if a size is specified unless it is also a small constant
5834 size and it is incorrect to do so if we cannot guarantee that the mode
5835 will be naturally aligned since the field must always be addressable.
5837 ??? This might not always be a win when done for a stand-alone object:
5838 since the nominal and the effective type of the object will now have
5839 different modes, a VIEW_CONVERT_EXPR will be required for converting
5840 between them and it might be hard to overcome afterwards, including
5841 at the RTL level when the stand-alone object is accessed as a whole. */
5842 if (align != 0
5843 && TREE_CODE (type) == RECORD_TYPE
5844 && TYPE_MODE (type) == BLKmode
5845 && TREE_CODE (orig_size) == INTEGER_CST
5846 && !TREE_CONSTANT_OVERFLOW (orig_size)
5847 && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
5848 && (!size
5849 || (TREE_CODE (size) == INTEGER_CST
5850 && compare_tree_int (size, MAX_FIXED_MODE_SIZE) <= 0)))
5852 tree packable_type = make_packable_type (type, true);
5853 if (TYPE_MODE (packable_type) != BLKmode
5854 && align >= TYPE_ALIGN (packable_type))
5855 type = packable_type;
5858 /* Now create the field with the original size. */
5859 field = create_field_decl (get_identifier ("F"), type, record, 0,
5860 orig_size, bitsize_zero_node, 1);
5861 DECL_INTERNAL_P (field) = 1;
5863 /* Do not finalize it until after the auxiliary record is built. */
5864 finish_record_type (record, field, 1, true);
5866 /* Set the same size for its RM_size if requested; otherwise reuse
5867 the RM_size of the original type. */
5868 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : orig_rm_size);
5870 /* Unless debugging information isn't being written for the input type,
5871 write a record that shows what we are a subtype of and also make a
5872 variable that indicates our size, if still variable. */
5873 if (TYPE_NAME (record)
5874 && AGGREGATE_TYPE_P (type)
5875 && TREE_CODE (orig_size) != INTEGER_CST
5876 && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
5877 && DECL_IGNORED_P (TYPE_NAME (type))))
5879 tree marker = make_node (RECORD_TYPE);
5880 tree name = TYPE_NAME (record);
5881 tree orig_name = TYPE_NAME (type);
5883 if (TREE_CODE (name) == TYPE_DECL)
5884 name = DECL_NAME (name);
5886 if (TREE_CODE (orig_name) == TYPE_DECL)
5887 orig_name = DECL_NAME (orig_name);
5889 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
5890 finish_record_type (marker,
5891 create_field_decl (orig_name, integer_type_node,
5892 marker, 0, NULL_TREE, NULL_TREE,
5894 0, false);
5896 add_parallel_type (TYPE_STUB_DECL (record), marker);
5898 if (size && TREE_CODE (size) != INTEGER_CST && definition)
5899 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
5900 sizetype, TYPE_SIZE_UNIT (record), false, false,
5901 false, false, NULL, gnat_entity);
5904 rest_of_record_type_compilation (record);
5906 /* If the size was widened explicitly, maybe give a warning. Take the
5907 original size as the maximum size of the input if there was an
5908 unconstrained record involved and round it up to the specified alignment,
5909 if one was specified. */
5910 if (CONTAINS_PLACEHOLDER_P (orig_size))
5911 orig_size = max_size (orig_size, true);
5913 if (align)
5914 orig_size = round_up (orig_size, align);
5916 if (size && Present (gnat_entity)
5917 && !operand_equal_p (size, orig_size, 0)
5918 && !(TREE_CODE (size) == INTEGER_CST
5919 && TREE_CODE (orig_size) == INTEGER_CST
5920 && tree_int_cst_lt (size, orig_size)))
5922 Node_Id gnat_error_node = Empty;
5924 if (Is_Packed_Array_Type (gnat_entity))
5925 gnat_entity = Original_Array_Type (gnat_entity);
5927 if ((Ekind (gnat_entity) == E_Component
5928 || Ekind (gnat_entity) == E_Discriminant)
5929 && Present (Component_Clause (gnat_entity)))
5930 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
5931 else if (Present (Size_Clause (gnat_entity)))
5932 gnat_error_node = Expression (Size_Clause (gnat_entity));
5934 /* Generate message only for entities that come from source, since
5935 if we have an entity created by expansion, the message will be
5936 generated for some other corresponding source entity. */
5937 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
5938 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
5939 gnat_entity,
5940 size_diffop (size, orig_size));
5942 else if (*name_trailer == 'C' && !Is_Internal (gnat_entity))
5943 post_error_ne_tree ("component of& padded{ by ^ bits}?",
5944 gnat_entity, gnat_entity,
5945 size_diffop (size, orig_size));
5948 return record;
5951 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
5952 the value passed against the list of choices. */
5954 tree
5955 choices_to_gnu (tree operand, Node_Id choices)
5957 Node_Id choice;
5958 Node_Id gnat_temp;
5959 tree result = integer_zero_node;
5960 tree this_test, low = 0, high = 0, single = 0;
5962 for (choice = First (choices); Present (choice); choice = Next (choice))
5964 switch (Nkind (choice))
5966 case N_Range:
5967 low = gnat_to_gnu (Low_Bound (choice));
5968 high = gnat_to_gnu (High_Bound (choice));
5970 /* There's no good type to use here, so we might as well use
5971 integer_type_node. */
5972 this_test
5973 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5974 build_binary_op (GE_EXPR, integer_type_node,
5975 operand, low),
5976 build_binary_op (LE_EXPR, integer_type_node,
5977 operand, high));
5979 break;
5981 case N_Subtype_Indication:
5982 gnat_temp = Range_Expression (Constraint (choice));
5983 low = gnat_to_gnu (Low_Bound (gnat_temp));
5984 high = gnat_to_gnu (High_Bound (gnat_temp));
5986 this_test
5987 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5988 build_binary_op (GE_EXPR, integer_type_node,
5989 operand, low),
5990 build_binary_op (LE_EXPR, integer_type_node,
5991 operand, high));
5992 break;
5994 case N_Identifier:
5995 case N_Expanded_Name:
5996 /* This represents either a subtype range, an enumeration
5997 literal, or a constant Ekind says which. If an enumeration
5998 literal or constant, fall through to the next case. */
5999 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6000 && Ekind (Entity (choice)) != E_Constant)
6002 tree type = gnat_to_gnu_type (Entity (choice));
6004 low = TYPE_MIN_VALUE (type);
6005 high = TYPE_MAX_VALUE (type);
6007 this_test
6008 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
6009 build_binary_op (GE_EXPR, integer_type_node,
6010 operand, low),
6011 build_binary_op (LE_EXPR, integer_type_node,
6012 operand, high));
6013 break;
6016 /* ... fall through ... */
6018 case N_Character_Literal:
6019 case N_Integer_Literal:
6020 single = gnat_to_gnu (choice);
6021 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
6022 single);
6023 break;
6025 case N_Others_Choice:
6026 this_test = integer_one_node;
6027 break;
6029 default:
6030 gcc_unreachable ();
6033 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
6034 result, this_test);
6037 return result;
6040 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6041 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6043 static int
6044 adjust_packed (tree field_type, tree record_type, int packed)
6046 /* If the field contains an item of variable size, we cannot pack it
6047 because we cannot create temporaries of non-fixed size in case
6048 we need to take the address of the field. See addressable_p and
6049 the notes on the addressability issues for further details. */
6050 if (is_variable_size (field_type))
6051 return 0;
6053 /* If the alignment of the record is specified and the field type
6054 is over-aligned, request Storage_Unit alignment for the field. */
6055 if (packed == -2)
6057 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6058 return -1;
6059 else
6060 return 0;
6063 return packed;
6066 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6067 placed in GNU_RECORD_TYPE.
6069 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6070 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6071 record has a specified alignment.
6073 DEFINITION is true if this field is for a record being defined. */
6075 static tree
6076 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6077 bool definition)
6079 tree gnu_field_id = get_entity_name (gnat_field);
6080 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
6081 tree gnu_field, gnu_size, gnu_pos;
6082 bool needs_strict_alignment
6083 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
6084 || Treat_As_Volatile (gnat_field));
6086 /* If this field requires strict alignment, we cannot pack it because
6087 it would very likely be under-aligned in the record. */
6088 if (needs_strict_alignment)
6089 packed = 0;
6090 else
6091 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6093 /* If a size is specified, use it. Otherwise, if the record type is packed,
6094 use the official RM size. See "Handling of Type'Size Values" in Einfo
6095 for further details. */
6096 if (Known_Static_Esize (gnat_field))
6097 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6098 gnat_field, FIELD_DECL, false, true);
6099 else if (packed == 1)
6100 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
6101 gnat_field, FIELD_DECL, false, true);
6102 else
6103 gnu_size = NULL_TREE;
6105 /* If we have a specified size that's smaller than that of the field type,
6106 or a position is specified, and the field type is also a record that's
6107 BLKmode, see if we can get either an integral mode form of the type or
6108 a smaller BLKmode form. If we can, show a size was specified for the
6109 field if there wasn't one already, so we know to make this a bitfield
6110 and avoid making things wider.
6112 Doing this is first useful if the record is packed because we may then
6113 place the field at a non-byte-aligned position and so achieve tighter
6114 packing.
6116 This is in addition *required* if the field shares a byte with another
6117 field and the front-end lets the back-end handle the references, because
6118 GCC does not handle BLKmode bitfields properly.
6120 We avoid the transformation if it is not required or potentially useful,
6121 as it might entail an increase of the field's alignment and have ripple
6122 effects on the outer record type. A typical case is a field known to be
6123 byte aligned and not to share a byte with another field.
6125 Besides, we don't even look the possibility of a transformation in cases
6126 known to be in error already, for instance when an invalid size results
6127 from a component clause. */
6129 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6130 && TYPE_MODE (gnu_field_type) == BLKmode
6131 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
6132 && (packed == 1
6133 || (gnu_size
6134 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6135 || Present (Component_Clause (gnat_field))))))
6137 /* See what the alternate type and size would be. */
6138 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6140 bool has_byte_aligned_clause
6141 = Present (Component_Clause (gnat_field))
6142 && (UI_To_Int (Component_Bit_Offset (gnat_field))
6143 % BITS_PER_UNIT == 0);
6145 /* Compute whether we should avoid the substitution. */
6146 bool reject
6147 /* There is no point substituting if there is no change... */
6148 = (gnu_packable_type == gnu_field_type)
6149 /* ... nor when the field is known to be byte aligned and not to
6150 share a byte with another field. */
6151 || (has_byte_aligned_clause
6152 && value_factor_p (gnu_size, BITS_PER_UNIT))
6153 /* The size of an aliased field must be an exact multiple of the
6154 type's alignment, which the substitution might increase. Reject
6155 substitutions that would so invalidate a component clause when the
6156 specified position is byte aligned, as the change would have no
6157 real benefit from the packing standpoint anyway. */
6158 || (Is_Aliased (gnat_field)
6159 && has_byte_aligned_clause
6160 && !value_factor_p (gnu_size, TYPE_ALIGN (gnu_packable_type)));
6162 /* Substitute unless told otherwise. */
6163 if (!reject)
6165 gnu_field_type = gnu_packable_type;
6167 if (!gnu_size)
6168 gnu_size = rm_size (gnu_field_type);
6172 /* If we are packing the record and the field is BLKmode, round the
6173 size up to a byte boundary. */
6174 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6175 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6177 if (Present (Component_Clause (gnat_field)))
6179 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6180 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6181 gnat_field, FIELD_DECL, false, true);
6183 /* Ensure the position does not overlap with the parent subtype,
6184 if there is one. */
6185 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
6187 tree gnu_parent
6188 = gnat_to_gnu_type (Parent_Subtype
6189 (Underlying_Type (Scope (gnat_field))));
6191 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6192 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6194 post_error_ne_tree
6195 ("offset of& must be beyond parent{, minimum allowed is ^}",
6196 First_Bit (Component_Clause (gnat_field)), gnat_field,
6197 TYPE_SIZE_UNIT (gnu_parent));
6201 /* If this field needs strict alignment, ensure the record is
6202 sufficiently aligned and that that position and size are
6203 consistent with the alignment. */
6204 if (needs_strict_alignment)
6206 TYPE_ALIGN (gnu_record_type)
6207 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6209 if (gnu_size
6210 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6212 if (Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
6213 post_error_ne_tree
6214 ("atomic field& must be natural size of type{ (^)}",
6215 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6216 TYPE_SIZE (gnu_field_type));
6218 else if (Is_Aliased (gnat_field))
6219 post_error_ne_tree
6220 ("size of aliased field& must be ^ bits",
6221 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6222 TYPE_SIZE (gnu_field_type));
6224 else if (Strict_Alignment (Etype (gnat_field)))
6225 post_error_ne_tree
6226 ("size of & with aliased or tagged components not ^ bits",
6227 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6228 TYPE_SIZE (gnu_field_type));
6230 gnu_size = NULL_TREE;
6233 if (!integer_zerop (size_binop
6234 (TRUNC_MOD_EXPR, gnu_pos,
6235 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6237 if (Is_Aliased (gnat_field))
6238 post_error_ne_num
6239 ("position of aliased field& must be multiple of ^ bits",
6240 First_Bit (Component_Clause (gnat_field)), gnat_field,
6241 TYPE_ALIGN (gnu_field_type));
6243 else if (Treat_As_Volatile (gnat_field))
6244 post_error_ne_num
6245 ("position of volatile field& must be multiple of ^ bits",
6246 First_Bit (Component_Clause (gnat_field)), gnat_field,
6247 TYPE_ALIGN (gnu_field_type));
6249 else if (Strict_Alignment (Etype (gnat_field)))
6250 post_error_ne_num
6251 ("position of & with aliased or tagged components not multiple of ^ bits",
6252 First_Bit (Component_Clause (gnat_field)), gnat_field,
6253 TYPE_ALIGN (gnu_field_type));
6255 else
6256 gcc_unreachable ();
6258 gnu_pos = NULL_TREE;
6262 if (Is_Atomic (gnat_field))
6263 check_ok_for_atomic (gnu_field_type, gnat_field, false);
6266 /* If the record has rep clauses and this is the tag field, make a rep
6267 clause for it as well. */
6268 else if (Has_Specified_Layout (Scope (gnat_field))
6269 && Chars (gnat_field) == Name_uTag)
6271 gnu_pos = bitsize_zero_node;
6272 gnu_size = TYPE_SIZE (gnu_field_type);
6275 else
6276 gnu_pos = NULL_TREE;
6278 /* We need to make the size the maximum for the type if it is
6279 self-referential and an unconstrained type. In that case, we can't
6280 pack the field since we can't make a copy to align it. */
6281 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6282 && !gnu_size
6283 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6284 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
6286 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6287 packed = 0;
6290 /* If a size is specified, adjust the field's type to it. */
6291 if (gnu_size)
6293 /* If the field's type is justified modular, we would need to remove
6294 the wrapper to (better) meet the layout requirements. However we
6295 can do so only if the field is not aliased to preserve the unique
6296 layout and if the prescribed size is not greater than that of the
6297 packed array to preserve the justification. */
6298 if (!needs_strict_alignment
6299 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6300 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6301 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6302 <= 0)
6303 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6305 gnu_field_type
6306 = make_type_from_size (gnu_field_type, gnu_size,
6307 Has_Biased_Representation (gnat_field));
6308 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6309 "PAD", false, definition, true);
6312 /* Otherwise (or if there was an error), don't specify a position. */
6313 else
6314 gnu_pos = NULL_TREE;
6316 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6317 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6319 /* Now create the decl for the field. */
6320 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6321 packed, gnu_size, gnu_pos,
6322 Is_Aliased (gnat_field));
6323 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6324 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
6326 if (Ekind (gnat_field) == E_Discriminant)
6327 DECL_DISCRIMINANT_NUMBER (gnu_field)
6328 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6330 return gnu_field;
6333 /* Return true if TYPE is a type with variable size, a padding type with a
6334 field of variable size or is a record that has a field such a field. */
6336 static bool
6337 is_variable_size (tree type)
6339 tree field;
6341 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6342 return true;
6344 if (TREE_CODE (type) == RECORD_TYPE
6345 && TYPE_IS_PADDING_P (type)
6346 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6347 return true;
6349 if (TREE_CODE (type) != RECORD_TYPE
6350 && TREE_CODE (type) != UNION_TYPE
6351 && TREE_CODE (type) != QUAL_UNION_TYPE)
6352 return false;
6354 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6355 if (is_variable_size (TREE_TYPE (field)))
6356 return true;
6358 return false;
6361 /* qsort comparer for the bit positions of two record components. */
6363 static int
6364 compare_field_bitpos (const PTR rt1, const PTR rt2)
6366 const_tree const field1 = * (const_tree const *) rt1;
6367 const_tree const field2 = * (const_tree const *) rt2;
6368 const int ret
6369 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6371 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6374 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
6375 of GCC trees for fields that are in the record and have already been
6376 processed. When called from gnat_to_gnu_entity during the processing of a
6377 record type definition, the GCC nodes for the discriminants will be on
6378 the chain. The other calls to this function are recursive calls from
6379 itself for the Component_List of a variant and the chain is empty.
6381 PACKED is 1 if this is for a packed record, -1 if this is for a record
6382 with Component_Alignment of Storage_Unit, -2 if this is for a record
6383 with a specified alignment.
6385 DEFINITION is true if we are defining this record.
6387 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6388 with a rep clause is to be added. If it is nonzero, that is all that
6389 should be done with such fields.
6391 CANCEL_ALIGNMENT, if true, means the alignment should be zeroed before
6392 laying out the record. This means the alignment only serves to force fields
6393 to be bitfields, but not require the record to be that aligned. This is
6394 used for variants.
6396 ALL_REP, if true, means a rep clause was found for all the fields. This
6397 simplifies the logic since we know we're not in the mixed case.
6399 DO_NOT_FINALIZE, if true, means that the record type is expected to be
6400 modified afterwards so it will not be sent to the back-end for finalization.
6402 UNCHECKED_UNION, if true, means that we are building a type for a record
6403 with a Pragma Unchecked_Union.
6405 The processing of the component list fills in the chain with all of the
6406 fields of the record and then the record type is finished. */
6408 static void
6409 components_to_record (tree gnu_record_type, Node_Id component_list,
6410 tree gnu_field_list, int packed, bool definition,
6411 tree *p_gnu_rep_list, bool cancel_alignment,
6412 bool all_rep, bool do_not_finalize, bool unchecked_union)
6414 Node_Id component_decl;
6415 Entity_Id gnat_field;
6416 Node_Id variant_part;
6417 tree gnu_our_rep_list = NULL_TREE;
6418 tree gnu_field, gnu_last;
6419 bool layout_with_rep = false;
6420 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6422 /* For each variable within each component declaration create a GCC field
6423 and add it to the list, skipping any pragmas in the list. */
6424 if (Present (Component_Items (component_list)))
6425 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6426 Present (component_decl);
6427 component_decl = Next_Non_Pragma (component_decl))
6429 gnat_field = Defining_Entity (component_decl);
6431 if (Chars (gnat_field) == Name_uParent)
6432 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
6433 else
6435 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
6436 packed, definition);
6438 /* If this is the _Tag field, put it before any discriminants,
6439 instead of after them as is the case for all other fields.
6440 Ignore field of void type if only annotating. */
6441 if (Chars (gnat_field) == Name_uTag)
6442 gnu_field_list = chainon (gnu_field_list, gnu_field);
6443 else
6445 TREE_CHAIN (gnu_field) = gnu_field_list;
6446 gnu_field_list = gnu_field;
6450 save_gnu_tree (gnat_field, gnu_field, false);
6453 /* At the end of the component list there may be a variant part. */
6454 variant_part = Variant_Part (component_list);
6456 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6457 mutually exclusive and should go in the same memory. To do this we need
6458 to treat each variant as a record whose elements are created from the
6459 component list for the variant. So here we create the records from the
6460 lists for the variants and put them all into the QUAL_UNION_TYPE.
6461 If this is an Unchecked_Union, we make a UNION_TYPE instead or
6462 use GNU_RECORD_TYPE if there are no fields so far. */
6463 if (Present (variant_part))
6465 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
6466 Node_Id variant;
6467 tree gnu_name = TYPE_NAME (gnu_record_type);
6468 tree gnu_var_name
6469 = concat_id_with_name (get_identifier (Get_Name_String
6470 (Chars (Name (variant_part)))),
6471 "XVN");
6472 tree gnu_union_type;
6473 tree gnu_union_name;
6474 tree gnu_union_field;
6475 tree gnu_variant_list = NULL_TREE;
6477 if (TREE_CODE (gnu_name) == TYPE_DECL)
6478 gnu_name = DECL_NAME (gnu_name);
6480 gnu_union_name = concat_id_with_name (gnu_name,
6481 IDENTIFIER_POINTER (gnu_var_name));
6483 /* Reuse an enclosing union if all fields are in the variant part
6484 and there is no representation clause on the record, to match
6485 the layout of C unions. There is an associated check below. */
6486 if (!gnu_field_list
6487 && TREE_CODE (gnu_record_type) == UNION_TYPE
6488 && !TYPE_PACKED (gnu_record_type))
6489 gnu_union_type = gnu_record_type;
6490 else
6492 gnu_union_type
6493 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
6495 TYPE_NAME (gnu_union_type) = gnu_union_name;
6496 TYPE_ALIGN (gnu_union_type) = 0;
6497 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
6500 for (variant = First_Non_Pragma (Variants (variant_part));
6501 Present (variant);
6502 variant = Next_Non_Pragma (variant))
6504 tree gnu_variant_type = make_node (RECORD_TYPE);
6505 tree gnu_inner_name;
6506 tree gnu_qual;
6508 Get_Variant_Encoding (variant);
6509 gnu_inner_name = get_identifier (Name_Buffer);
6510 TYPE_NAME (gnu_variant_type)
6511 = concat_id_with_name (gnu_union_name,
6512 IDENTIFIER_POINTER (gnu_inner_name));
6514 /* Set the alignment of the inner type in case we need to make
6515 inner objects into bitfields, but then clear it out
6516 so the record actually gets only the alignment required. */
6517 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
6518 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
6520 /* Similarly, if the outer record has a size specified and all fields
6521 have record rep clauses, we can propagate the size into the
6522 variant part. */
6523 if (all_rep_and_size)
6525 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
6526 TYPE_SIZE_UNIT (gnu_variant_type)
6527 = TYPE_SIZE_UNIT (gnu_record_type);
6530 /* Create the record type for the variant. Note that we defer
6531 finalizing it until after we are sure to actually use it. */
6532 components_to_record (gnu_variant_type, Component_List (variant),
6533 NULL_TREE, packed, definition,
6534 &gnu_our_rep_list, !all_rep_and_size, all_rep,
6535 true, unchecked_union);
6537 gnu_qual = choices_to_gnu (gnu_discriminant,
6538 Discrete_Choices (variant));
6540 Set_Present_Expr (variant, annotate_value (gnu_qual));
6542 /* If this is an Unchecked_Union and we have exactly one field,
6543 use this field directly to match the layout of C unions. */
6544 if (unchecked_union
6545 && TYPE_FIELDS (gnu_variant_type)
6546 && !TREE_CHAIN (TYPE_FIELDS (gnu_variant_type)))
6547 gnu_field = TYPE_FIELDS (gnu_variant_type);
6548 else
6550 /* Deal with packedness like in gnat_to_gnu_field. */
6551 int field_packed
6552 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
6554 /* Finalize the record type now. We used to throw away
6555 empty records but we no longer do that because we need
6556 them to generate complete debug info for the variant;
6557 otherwise, the union type definition will be lacking
6558 the fields associated with these empty variants. */
6559 rest_of_record_type_compilation (gnu_variant_type);
6561 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
6562 gnu_union_type, field_packed,
6563 (all_rep_and_size
6564 ? TYPE_SIZE (gnu_variant_type)
6565 : 0),
6566 (all_rep_and_size
6567 ? bitsize_zero_node : 0),
6570 DECL_INTERNAL_P (gnu_field) = 1;
6572 if (!unchecked_union)
6573 DECL_QUALIFIER (gnu_field) = gnu_qual;
6576 TREE_CHAIN (gnu_field) = gnu_variant_list;
6577 gnu_variant_list = gnu_field;
6580 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
6581 if (gnu_variant_list)
6583 int union_field_packed;
6585 if (all_rep_and_size)
6587 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
6588 TYPE_SIZE_UNIT (gnu_union_type)
6589 = TYPE_SIZE_UNIT (gnu_record_type);
6592 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
6593 all_rep_and_size ? 1 : 0, false);
6595 /* If GNU_UNION_TYPE is our record type, it means we must have an
6596 Unchecked_Union with no fields. Verify that and, if so, just
6597 return. */
6598 if (gnu_union_type == gnu_record_type)
6600 gcc_assert (unchecked_union
6601 && !gnu_field_list
6602 && !gnu_our_rep_list);
6603 return;
6606 /* Deal with packedness like in gnat_to_gnu_field. */
6607 union_field_packed
6608 = adjust_packed (gnu_union_type, gnu_record_type, packed);
6610 gnu_union_field
6611 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
6612 union_field_packed,
6613 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
6614 all_rep ? bitsize_zero_node : 0, 0);
6616 DECL_INTERNAL_P (gnu_union_field) = 1;
6617 TREE_CHAIN (gnu_union_field) = gnu_field_list;
6618 gnu_field_list = gnu_union_field;
6622 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
6623 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
6624 in a separate pass since we want to handle the discriminants but can't
6625 play with them until we've used them in debugging data above.
6627 ??? Note: if we then reorder them, debugging information will be wrong,
6628 but there's nothing that can be done about this at the moment. */
6629 for (gnu_field = gnu_field_list, gnu_last = NULL_TREE; gnu_field; )
6631 if (DECL_FIELD_OFFSET (gnu_field))
6633 tree gnu_next = TREE_CHAIN (gnu_field);
6635 if (!gnu_last)
6636 gnu_field_list = gnu_next;
6637 else
6638 TREE_CHAIN (gnu_last) = gnu_next;
6640 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
6641 gnu_our_rep_list = gnu_field;
6642 gnu_field = gnu_next;
6644 else
6646 gnu_last = gnu_field;
6647 gnu_field = TREE_CHAIN (gnu_field);
6651 /* If we have any items in our rep'ed field list, it is not the case that all
6652 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
6653 set it and ignore the items. */
6654 if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
6655 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
6656 else if (gnu_our_rep_list)
6658 /* Otherwise, sort the fields by bit position and put them into their
6659 own record if we have any fields without rep clauses. */
6660 tree gnu_rep_type
6661 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
6662 int len = list_length (gnu_our_rep_list);
6663 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
6664 int i;
6666 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
6667 gnu_field = TREE_CHAIN (gnu_field), i++)
6668 gnu_arr[i] = gnu_field;
6670 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
6672 /* Put the fields in the list in order of increasing position, which
6673 means we start from the end. */
6674 gnu_our_rep_list = NULL_TREE;
6675 for (i = len - 1; i >= 0; i--)
6677 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
6678 gnu_our_rep_list = gnu_arr[i];
6679 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
6682 if (gnu_field_list)
6684 finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, false);
6685 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
6686 gnu_record_type, 0, 0, 0, 1);
6687 DECL_INTERNAL_P (gnu_field) = 1;
6688 gnu_field_list = chainon (gnu_field_list, gnu_field);
6690 else
6692 layout_with_rep = true;
6693 gnu_field_list = nreverse (gnu_our_rep_list);
6697 if (cancel_alignment)
6698 TYPE_ALIGN (gnu_record_type) = 0;
6700 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
6701 layout_with_rep ? 1 : 0, do_not_finalize);
6704 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
6705 placed into an Esize, Component_Bit_Offset, or Component_Size value
6706 in the GNAT tree. */
6708 static Uint
6709 annotate_value (tree gnu_size)
6711 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
6712 TCode tcode;
6713 Node_Ref_Or_Val ops[3], ret;
6714 int i;
6715 int size;
6716 struct tree_int_map **h = NULL;
6718 /* See if we've already saved the value for this node. */
6719 if (EXPR_P (gnu_size))
6721 struct tree_int_map in;
6722 if (!annotate_value_cache)
6723 annotate_value_cache = htab_create_ggc (512, tree_int_map_hash,
6724 tree_int_map_eq, 0);
6725 in.base.from = gnu_size;
6726 h = (struct tree_int_map **)
6727 htab_find_slot (annotate_value_cache, &in, INSERT);
6729 if (*h)
6730 return (Node_Ref_Or_Val) (*h)->to;
6733 /* If we do not return inside this switch, TCODE will be set to the
6734 code to use for a Create_Node operand and LEN (set above) will be
6735 the number of recursive calls for us to make. */
6737 switch (TREE_CODE (gnu_size))
6739 case INTEGER_CST:
6740 if (TREE_OVERFLOW (gnu_size))
6741 return No_Uint;
6743 /* This may have come from a conversion from some smaller type,
6744 so ensure this is in bitsizetype. */
6745 gnu_size = convert (bitsizetype, gnu_size);
6747 /* For negative values, use NEGATE_EXPR of the supplied value. */
6748 if (tree_int_cst_sgn (gnu_size) < 0)
6750 /* The ridiculous code below is to handle the case of the largest
6751 negative integer. */
6752 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
6753 bool adjust = false;
6754 tree temp;
6756 if (TREE_OVERFLOW (negative_size))
6758 negative_size
6759 = size_binop (MINUS_EXPR, bitsize_zero_node,
6760 size_binop (PLUS_EXPR, gnu_size,
6761 bitsize_one_node));
6762 adjust = true;
6765 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
6766 if (adjust)
6767 temp = build2 (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
6769 return annotate_value (temp);
6772 if (!host_integerp (gnu_size, 1))
6773 return No_Uint;
6775 size = tree_low_cst (gnu_size, 1);
6777 /* This peculiar test is to make sure that the size fits in an int
6778 on machines where HOST_WIDE_INT is not "int". */
6779 if (tree_low_cst (gnu_size, 1) == size)
6780 return UI_From_Int (size);
6781 else
6782 return No_Uint;
6784 case COMPONENT_REF:
6785 /* The only case we handle here is a simple discriminant reference. */
6786 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
6787 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
6788 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
6789 return Create_Node (Discrim_Val,
6790 annotate_value (DECL_DISCRIMINANT_NUMBER
6791 (TREE_OPERAND (gnu_size, 1))),
6792 No_Uint, No_Uint);
6793 else
6794 return No_Uint;
6796 CASE_CONVERT: case NON_LVALUE_EXPR:
6797 return annotate_value (TREE_OPERAND (gnu_size, 0));
6799 /* Now just list the operations we handle. */
6800 case COND_EXPR: tcode = Cond_Expr; break;
6801 case PLUS_EXPR: tcode = Plus_Expr; break;
6802 case MINUS_EXPR: tcode = Minus_Expr; break;
6803 case MULT_EXPR: tcode = Mult_Expr; break;
6804 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
6805 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
6806 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
6807 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
6808 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
6809 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
6810 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
6811 case NEGATE_EXPR: tcode = Negate_Expr; break;
6812 case MIN_EXPR: tcode = Min_Expr; break;
6813 case MAX_EXPR: tcode = Max_Expr; break;
6814 case ABS_EXPR: tcode = Abs_Expr; break;
6815 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
6816 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
6817 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
6818 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
6819 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
6820 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
6821 case BIT_AND_EXPR: tcode = Bit_And_Expr; break;
6822 case LT_EXPR: tcode = Lt_Expr; break;
6823 case LE_EXPR: tcode = Le_Expr; break;
6824 case GT_EXPR: tcode = Gt_Expr; break;
6825 case GE_EXPR: tcode = Ge_Expr; break;
6826 case EQ_EXPR: tcode = Eq_Expr; break;
6827 case NE_EXPR: tcode = Ne_Expr; break;
6829 default:
6830 return No_Uint;
6833 /* Now get each of the operands that's relevant for this code. If any
6834 cannot be expressed as a repinfo node, say we can't. */
6835 for (i = 0; i < 3; i++)
6836 ops[i] = No_Uint;
6838 for (i = 0; i < len; i++)
6840 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
6841 if (ops[i] == No_Uint)
6842 return No_Uint;
6845 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
6847 /* Save the result in the cache. */
6848 if (h)
6850 *h = GGC_NEW (struct tree_int_map);
6851 (*h)->base.from = gnu_size;
6852 (*h)->to = ret;
6855 return ret;
6858 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
6859 GCC type, set Component_Bit_Offset and Esize to the position and size
6860 used by Gigi. */
6862 static void
6863 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
6865 tree gnu_list;
6866 tree gnu_entry;
6867 Entity_Id gnat_field;
6869 /* We operate by first making a list of all fields and their positions
6870 (we can get the sizes easily at any time) by a recursive call
6871 and then update all the sizes into the tree. */
6872 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
6873 size_zero_node, bitsize_zero_node,
6874 BIGGEST_ALIGNMENT);
6876 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
6877 gnat_field = Next_Entity (gnat_field))
6878 if ((Ekind (gnat_field) == E_Component
6879 || (Ekind (gnat_field) == E_Discriminant
6880 && !Is_Unchecked_Union (Scope (gnat_field)))))
6882 tree parent_offset = bitsize_zero_node;
6884 gnu_entry = purpose_member (gnat_to_gnu_field_decl (gnat_field),
6885 gnu_list);
6887 if (gnu_entry)
6889 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
6891 /* In this mode the tag and parent components have not been
6892 generated, so we add the appropriate offset to each
6893 component. For a component appearing in the current
6894 extension, the offset is the size of the parent. */
6895 if (Is_Derived_Type (gnat_entity)
6896 && Original_Record_Component (gnat_field) == gnat_field)
6897 parent_offset
6898 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
6899 bitsizetype);
6900 else
6901 parent_offset = bitsize_int (POINTER_SIZE);
6904 Set_Component_Bit_Offset
6905 (gnat_field,
6906 annotate_value
6907 (size_binop (PLUS_EXPR,
6908 bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
6909 TREE_VALUE (TREE_VALUE
6910 (TREE_VALUE (gnu_entry)))),
6911 parent_offset)));
6913 Set_Esize (gnat_field,
6914 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
6916 else if (Is_Tagged_Type (gnat_entity)
6917 && Is_Derived_Type (gnat_entity))
6919 /* If there is no gnu_entry, this is an inherited component whose
6920 position is the same as in the parent type. */
6921 Set_Component_Bit_Offset
6922 (gnat_field,
6923 Component_Bit_Offset (Original_Record_Component (gnat_field)));
6924 Set_Esize (gnat_field,
6925 Esize (Original_Record_Component (gnat_field)));
6930 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
6931 FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
6932 position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
6933 placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position. GNU_POS is
6934 to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
6935 the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
6936 so far. */
6938 static tree
6939 compute_field_positions (tree gnu_type, tree gnu_list, tree gnu_pos,
6940 tree gnu_bitpos, unsigned int offset_align)
6942 tree gnu_field;
6943 tree gnu_result = gnu_list;
6945 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
6946 gnu_field = TREE_CHAIN (gnu_field))
6948 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
6949 DECL_FIELD_BIT_OFFSET (gnu_field));
6950 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
6951 DECL_FIELD_OFFSET (gnu_field));
6952 unsigned int our_offset_align
6953 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
6955 gnu_result
6956 = tree_cons (gnu_field,
6957 tree_cons (gnu_our_offset,
6958 tree_cons (size_int (our_offset_align),
6959 gnu_our_bitpos, NULL_TREE),
6960 NULL_TREE),
6961 gnu_result);
6963 if (DECL_INTERNAL_P (gnu_field))
6964 gnu_result
6965 = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
6966 gnu_our_offset, gnu_our_bitpos,
6967 our_offset_align);
6970 return gnu_result;
6973 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
6974 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
6975 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
6976 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
6977 for the size of a field. COMPONENT_P is true if we are being called
6978 to process the Component_Size of GNAT_OBJECT. This is used for error
6979 message handling and to indicate to use the object size of GNU_TYPE.
6980 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
6981 it means that a size of zero should be treated as an unspecified size. */
6983 static tree
6984 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
6985 enum tree_code kind, bool component_p, bool zero_ok)
6987 Node_Id gnat_error_node;
6988 tree type_size, size;
6990 if (kind == VAR_DECL
6991 /* If a type needs strict alignment, a component of this type in
6992 a packed record cannot be packed and thus uses the type size. */
6993 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
6994 type_size = TYPE_SIZE (gnu_type);
6995 else
6996 type_size = rm_size (gnu_type);
6998 /* Find the node to use for errors. */
6999 if ((Ekind (gnat_object) == E_Component
7000 || Ekind (gnat_object) == E_Discriminant)
7001 && Present (Component_Clause (gnat_object)))
7002 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7003 else if (Present (Size_Clause (gnat_object)))
7004 gnat_error_node = Expression (Size_Clause (gnat_object));
7005 else
7006 gnat_error_node = gnat_object;
7008 /* Return 0 if no size was specified, either because Esize was not Present or
7009 the specified size was zero. */
7010 if (No (uint_size) || uint_size == No_Uint)
7011 return NULL_TREE;
7013 /* Get the size as a tree. Give an error if a size was specified, but cannot
7014 be represented as in sizetype. */
7015 size = UI_To_gnu (uint_size, bitsizetype);
7016 if (TREE_OVERFLOW (size))
7018 post_error_ne (component_p ? "component size of & is too large"
7019 : "size of & is too large",
7020 gnat_error_node, gnat_object);
7021 return NULL_TREE;
7024 /* Ignore a negative size since that corresponds to our back-annotation.
7025 Also ignore a zero size unless a size clause exists. */
7026 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
7027 return NULL_TREE;
7029 /* The size of objects is always a multiple of a byte. */
7030 if (kind == VAR_DECL
7031 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7033 if (component_p)
7034 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7035 gnat_error_node, gnat_object);
7036 else
7037 post_error_ne ("size for& is not a multiple of Storage_Unit",
7038 gnat_error_node, gnat_object);
7039 return NULL_TREE;
7042 /* If this is an integral type or a packed array type, the front-end has
7043 verified the size, so we need not do it here (which would entail
7044 checking against the bounds). However, if this is an aliased object, it
7045 may not be smaller than the type of the object. */
7046 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7047 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7048 return size;
7050 /* If the object is a record that contains a template, add the size of
7051 the template to the specified size. */
7052 if (TREE_CODE (gnu_type) == RECORD_TYPE
7053 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7054 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7056 /* Modify the size of the type to be that of the maximum size if it has a
7057 discriminant. */
7058 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7059 type_size = max_size (type_size, true);
7061 /* If this is an access type or a fat pointer, the minimum size is that given
7062 by the smallest integral mode that's valid for pointers. */
7063 if ((TREE_CODE (gnu_type) == POINTER_TYPE) || TYPE_FAT_POINTER_P (gnu_type))
7065 enum machine_mode p_mode;
7067 for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7068 !targetm.valid_pointer_mode (p_mode);
7069 p_mode = GET_MODE_WIDER_MODE (p_mode))
7072 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7075 /* If the size of the object is a constant, the new size must not be
7076 smaller. */
7077 if (TREE_CODE (type_size) != INTEGER_CST
7078 || TREE_OVERFLOW (type_size)
7079 || tree_int_cst_lt (size, type_size))
7081 if (component_p)
7082 post_error_ne_tree
7083 ("component size for& too small{, minimum allowed is ^}",
7084 gnat_error_node, gnat_object, type_size);
7085 else
7086 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
7087 gnat_error_node, gnat_object, type_size);
7089 if (kind == VAR_DECL && !component_p
7090 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
7091 && !tree_int_cst_lt (size, rm_size (gnu_type)))
7092 post_error_ne_tree_2
7093 ("\\size of ^ is not a multiple of alignment (^ bits)",
7094 gnat_error_node, gnat_object, rm_size (gnu_type),
7095 TYPE_ALIGN (gnu_type));
7097 else if (INTEGRAL_TYPE_P (gnu_type))
7098 post_error_ne ("\\size would be legal if & were not aliased!",
7099 gnat_error_node, gnat_object);
7101 return NULL_TREE;
7104 return size;
7107 /* Similarly, but both validate and process a value of RM_Size. This
7108 routine is only called for types. */
7110 static void
7111 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7113 /* Only give an error if a Value_Size clause was explicitly given.
7114 Otherwise, we'd be duplicating an error on the Size clause. */
7115 Node_Id gnat_attr_node
7116 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
7117 tree old_size = rm_size (gnu_type);
7118 tree size;
7120 /* Get the size as a tree. Do nothing if none was specified, either
7121 because RM_Size was not Present or if the specified size was zero.
7122 Give an error if a size was specified, but cannot be represented as
7123 in sizetype. */
7124 if (No (uint_size) || uint_size == No_Uint)
7125 return;
7127 size = UI_To_gnu (uint_size, bitsizetype);
7128 if (TREE_OVERFLOW (size))
7130 if (Present (gnat_attr_node))
7131 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
7132 gnat_entity);
7134 return;
7137 /* Ignore a negative size since that corresponds to our back-annotation.
7138 Also ignore a zero size unless a size clause exists, a Value_Size
7139 clause exists, or this is an integer type, in which case the
7140 front end will have always set it. */
7141 else if (tree_int_cst_sgn (size) < 0
7142 || (integer_zerop (size) && No (gnat_attr_node)
7143 && !Has_Size_Clause (gnat_entity)
7144 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
7145 return;
7147 /* If the old size is self-referential, get the maximum size. */
7148 if (CONTAINS_PLACEHOLDER_P (old_size))
7149 old_size = max_size (old_size, true);
7151 /* If the size of the object is a constant, the new size must not be
7152 smaller (the front end checks this for scalar types). */
7153 if (TREE_CODE (old_size) != INTEGER_CST
7154 || TREE_OVERFLOW (old_size)
7155 || (AGGREGATE_TYPE_P (gnu_type)
7156 && tree_int_cst_lt (size, old_size)))
7158 if (Present (gnat_attr_node))
7159 post_error_ne_tree
7160 ("Value_Size for& too small{, minimum allowed is ^}",
7161 gnat_attr_node, gnat_entity, old_size);
7163 return;
7166 /* Otherwise, set the RM_Size. */
7167 if (TREE_CODE (gnu_type) == INTEGER_TYPE
7168 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7169 TYPE_RM_SIZE_NUM (gnu_type) = size;
7170 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE
7171 || TREE_CODE (gnu_type) == BOOLEAN_TYPE)
7172 TYPE_RM_SIZE_NUM (gnu_type) = size;
7173 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
7174 || TREE_CODE (gnu_type) == UNION_TYPE
7175 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
7176 && !TYPE_IS_FAT_POINTER_P (gnu_type))
7177 SET_TYPE_ADA_SIZE (gnu_type, size);
7180 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
7181 If TYPE is the best type, return it. Otherwise, make a new type. We
7182 only support new integral and pointer types. FOR_BIASED is nonzero if
7183 we are making a biased type. */
7185 static tree
7186 make_type_from_size (tree type, tree size_tree, bool for_biased)
7188 unsigned HOST_WIDE_INT size;
7189 bool biased_p;
7190 tree new_type;
7192 /* If size indicates an error, just return TYPE to avoid propagating
7193 the error. Likewise if it's too large to represent. */
7194 if (!size_tree || !host_integerp (size_tree, 1))
7195 return type;
7197 size = tree_low_cst (size_tree, 1);
7199 switch (TREE_CODE (type))
7201 case INTEGER_TYPE:
7202 case ENUMERAL_TYPE:
7203 case BOOLEAN_TYPE:
7204 biased_p = (TREE_CODE (type) == INTEGER_TYPE
7205 && TYPE_BIASED_REPRESENTATION_P (type));
7207 /* Only do something if the type is not a packed array type and
7208 doesn't already have the proper size. */
7209 if (TYPE_PACKED_ARRAY_TYPE_P (type)
7210 || (TYPE_PRECISION (type) == size && biased_p == for_biased))
7211 break;
7213 biased_p |= for_biased;
7214 size = MIN (size, LONG_LONG_TYPE_SIZE);
7216 if (TYPE_UNSIGNED (type) || biased_p)
7217 new_type = make_unsigned_type (size);
7218 else
7219 new_type = make_signed_type (size);
7220 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
7221 TYPE_MIN_VALUE (new_type)
7222 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
7223 TYPE_MAX_VALUE (new_type)
7224 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
7225 /* Propagate the name to avoid creating a fake subrange type. */
7226 if (TYPE_NAME (type))
7228 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
7229 TYPE_NAME (new_type) = DECL_NAME (TYPE_NAME (type));
7230 else
7231 TYPE_NAME (new_type) = TYPE_NAME (type);
7233 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
7234 TYPE_RM_SIZE_NUM (new_type) = bitsize_int (size);
7235 return new_type;
7237 case RECORD_TYPE:
7238 /* Do something if this is a fat pointer, in which case we
7239 may need to return the thin pointer. */
7240 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
7242 enum machine_mode p_mode = mode_for_size (size, MODE_INT, 0);
7243 if (!targetm.valid_pointer_mode (p_mode))
7244 p_mode = ptr_mode;
7245 return
7246 build_pointer_type_for_mode
7247 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)),
7248 p_mode, 0);
7250 break;
7252 case POINTER_TYPE:
7253 /* Only do something if this is a thin pointer, in which case we
7254 may need to return the fat pointer. */
7255 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
7256 return
7257 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
7258 break;
7260 default:
7261 break;
7264 return type;
7267 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
7268 a type or object whose present alignment is ALIGN. If this alignment is
7269 valid, return it. Otherwise, give an error and return ALIGN. */
7271 static unsigned int
7272 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
7274 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
7275 unsigned int new_align;
7276 Node_Id gnat_error_node;
7278 /* Don't worry about checking alignment if alignment was not specified
7279 by the source program and we already posted an error for this entity. */
7280 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
7281 return align;
7283 /* Post the error on the alignment clause if any. */
7284 if (Present (Alignment_Clause (gnat_entity)))
7285 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
7286 else
7287 gnat_error_node = gnat_entity;
7289 /* Within GCC, an alignment is an integer, so we must make sure a value is
7290 specified that fits in that range. Also, there is an upper bound to
7291 alignments we can support/allow. */
7292 if (!UI_Is_In_Int_Range (alignment)
7293 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
7294 post_error_ne_num ("largest supported alignment for& is ^",
7295 gnat_error_node, gnat_entity, max_allowed_alignment);
7296 else if (!(Present (Alignment_Clause (gnat_entity))
7297 && From_At_Mod (Alignment_Clause (gnat_entity)))
7298 && new_align * BITS_PER_UNIT < align)
7299 post_error_ne_num ("alignment for& must be at least ^",
7300 gnat_error_node, gnat_entity,
7301 align / BITS_PER_UNIT);
7302 else
7304 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
7305 if (new_align > align)
7306 align = new_align;
7309 return align;
7312 /* Return the smallest alignment not less than SIZE. */
7314 static unsigned int
7315 ceil_alignment (unsigned HOST_WIDE_INT size)
7317 return (unsigned int) 1 << (floor_log2 (size - 1) + 1);
7320 /* Verify that OBJECT, a type or decl, is something we can implement
7321 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
7322 if we require atomic components. */
7324 static void
7325 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
7327 Node_Id gnat_error_point = gnat_entity;
7328 Node_Id gnat_node;
7329 enum machine_mode mode;
7330 unsigned int align;
7331 tree size;
7333 /* There are three case of what OBJECT can be. It can be a type, in which
7334 case we take the size, alignment and mode from the type. It can be a
7335 declaration that was indirect, in which case the relevant values are
7336 that of the type being pointed to, or it can be a normal declaration,
7337 in which case the values are of the decl. The code below assumes that
7338 OBJECT is either a type or a decl. */
7339 if (TYPE_P (object))
7341 mode = TYPE_MODE (object);
7342 align = TYPE_ALIGN (object);
7343 size = TYPE_SIZE (object);
7345 else if (DECL_BY_REF_P (object))
7347 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
7348 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
7349 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
7351 else
7353 mode = DECL_MODE (object);
7354 align = DECL_ALIGN (object);
7355 size = DECL_SIZE (object);
7358 /* Consider all floating-point types atomic and any types that that are
7359 represented by integers no wider than a machine word. */
7360 if (GET_MODE_CLASS (mode) == MODE_FLOAT
7361 || ((GET_MODE_CLASS (mode) == MODE_INT
7362 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
7363 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
7364 return;
7366 /* For the moment, also allow anything that has an alignment equal
7367 to its size and which is smaller than a word. */
7368 if (size && TREE_CODE (size) == INTEGER_CST
7369 && compare_tree_int (size, align) == 0
7370 && align <= BITS_PER_WORD)
7371 return;
7373 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
7374 gnat_node = Next_Rep_Item (gnat_node))
7376 if (!comp_p && Nkind (gnat_node) == N_Pragma
7377 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
7378 == Pragma_Atomic))
7379 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
7380 else if (comp_p && Nkind (gnat_node) == N_Pragma
7381 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
7382 == Pragma_Atomic_Components))
7383 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
7386 if (comp_p)
7387 post_error_ne ("atomic access to component of & cannot be guaranteed",
7388 gnat_error_point, gnat_entity);
7389 else
7390 post_error_ne ("atomic access to & cannot be guaranteed",
7391 gnat_error_point, gnat_entity);
7394 /* Check if FTYPE1 and FTYPE2, two potentially different function type nodes,
7395 have compatible signatures so that a call using one type may be safely
7396 issued if the actual target function type is the other. Return 1 if it is
7397 the case, 0 otherwise, and post errors on the incompatibilities.
7399 This is used when an Ada subprogram is mapped onto a GCC builtin, to ensure
7400 that calls to the subprogram will have arguments suitable for the later
7401 underlying builtin expansion. */
7403 static int
7404 compatible_signatures_p (tree ftype1, tree ftype2)
7406 /* As of now, we only perform very trivial tests and consider it's the
7407 programmer's responsibility to ensure the type correctness in the Ada
7408 declaration, as in the regular Import cases.
7410 Mismatches typically result in either error messages from the builtin
7411 expander, internal compiler errors, or in a real call sequence. This
7412 should be refined to issue diagnostics helping error detection and
7413 correction. */
7415 /* Almost fake test, ensuring a use of each argument. */
7416 if (ftype1 == ftype2)
7417 return 1;
7419 return 1;
7422 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a new
7423 type with all size expressions that contain F updated by replacing F
7424 with R. If F is NULL_TREE, always make a new RECORD_TYPE, even if
7425 nothing has changed. */
7427 tree
7428 substitute_in_type (tree t, tree f, tree r)
7430 tree new = t;
7431 tree tem;
7433 switch (TREE_CODE (t))
7435 case INTEGER_TYPE:
7436 case ENUMERAL_TYPE:
7437 case BOOLEAN_TYPE:
7438 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
7439 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
7441 tree low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
7442 tree high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
7444 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
7445 return t;
7447 new = build_range_type (TREE_TYPE (t), low, high);
7448 if (TYPE_INDEX_TYPE (t))
7449 SET_TYPE_INDEX_TYPE
7450 (new, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
7451 return new;
7454 return t;
7456 case REAL_TYPE:
7457 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
7458 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
7460 tree low = NULL_TREE, high = NULL_TREE;
7462 if (TYPE_MIN_VALUE (t))
7463 low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
7464 if (TYPE_MAX_VALUE (t))
7465 high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
7467 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
7468 return t;
7470 t = copy_type (t);
7471 TYPE_MIN_VALUE (t) = low;
7472 TYPE_MAX_VALUE (t) = high;
7474 return t;
7476 case COMPLEX_TYPE:
7477 tem = substitute_in_type (TREE_TYPE (t), f, r);
7478 if (tem == TREE_TYPE (t))
7479 return t;
7481 return build_complex_type (tem);
7483 case OFFSET_TYPE:
7484 case METHOD_TYPE:
7485 case FUNCTION_TYPE:
7486 case LANG_TYPE:
7487 /* Don't know how to do these yet. */
7488 gcc_unreachable ();
7490 case ARRAY_TYPE:
7492 tree component = substitute_in_type (TREE_TYPE (t), f, r);
7493 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
7495 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7496 return t;
7498 new = build_array_type (component, domain);
7499 TYPE_SIZE (new) = 0;
7500 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
7501 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
7502 layout_type (new);
7503 TYPE_ALIGN (new) = TYPE_ALIGN (t);
7504 TYPE_USER_ALIGN (new) = TYPE_USER_ALIGN (t);
7506 /* If we had bounded the sizes of T by a constant, bound the sizes of
7507 NEW by the same constant. */
7508 if (TREE_CODE (TYPE_SIZE (t)) == MIN_EXPR)
7509 TYPE_SIZE (new)
7510 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE (t), 1),
7511 TYPE_SIZE (new));
7512 if (TREE_CODE (TYPE_SIZE_UNIT (t)) == MIN_EXPR)
7513 TYPE_SIZE_UNIT (new)
7514 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE_UNIT (t), 1),
7515 TYPE_SIZE_UNIT (new));
7516 return new;
7519 case RECORD_TYPE:
7520 case UNION_TYPE:
7521 case QUAL_UNION_TYPE:
7523 tree field;
7524 bool changed_field
7525 = (f == NULL_TREE && !TREE_CONSTANT (TYPE_SIZE (t)));
7526 bool field_has_rep = false;
7527 tree last_field = NULL_TREE;
7529 tree new = copy_type (t);
7531 /* Start out with no fields, make new fields, and chain them
7532 in. If we haven't actually changed the type of any field,
7533 discard everything we've done and return the old type. */
7535 TYPE_FIELDS (new) = NULL_TREE;
7536 TYPE_SIZE (new) = NULL_TREE;
7538 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
7540 tree new_field = copy_node (field);
7542 TREE_TYPE (new_field)
7543 = substitute_in_type (TREE_TYPE (new_field), f, r);
7545 if (DECL_HAS_REP_P (field) && !DECL_INTERNAL_P (field))
7546 field_has_rep = true;
7547 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
7548 changed_field = true;
7550 /* If this is an internal field and the type of this field is
7551 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
7552 the type just has one element, treat that as the field.
7553 But don't do this if we are processing a QUAL_UNION_TYPE. */
7554 if (TREE_CODE (t) != QUAL_UNION_TYPE
7555 && DECL_INTERNAL_P (new_field)
7556 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
7557 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
7559 if (!TYPE_FIELDS (TREE_TYPE (new_field)))
7560 continue;
7562 if (!TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))))
7564 tree next_new_field
7565 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
7567 /* Make sure omitting the union doesn't change
7568 the layout. */
7569 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
7570 new_field = next_new_field;
7574 DECL_CONTEXT (new_field) = new;
7575 SET_DECL_ORIGINAL_FIELD (new_field,
7576 (DECL_ORIGINAL_FIELD (field)
7577 ? DECL_ORIGINAL_FIELD (field) : field));
7579 /* If the size of the old field was set at a constant,
7580 propagate the size in case the type's size was variable.
7581 (This occurs in the case of a variant or discriminated
7582 record with a default size used as a field of another
7583 record.) */
7584 DECL_SIZE (new_field)
7585 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
7586 ? DECL_SIZE (field) : NULL_TREE;
7587 DECL_SIZE_UNIT (new_field)
7588 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
7589 ? DECL_SIZE_UNIT (field) : NULL_TREE;
7591 if (TREE_CODE (t) == QUAL_UNION_TYPE)
7593 tree new_q = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
7595 if (new_q != DECL_QUALIFIER (new_field))
7596 changed_field = true;
7598 /* Do the substitution inside the qualifier and if we find
7599 that this field will not be present, omit it. */
7600 DECL_QUALIFIER (new_field) = new_q;
7602 if (integer_zerop (DECL_QUALIFIER (new_field)))
7603 continue;
7606 if (!last_field)
7607 TYPE_FIELDS (new) = new_field;
7608 else
7609 TREE_CHAIN (last_field) = new_field;
7611 last_field = new_field;
7613 /* If this is a qualified type and this field will always be
7614 present, we are done. */
7615 if (TREE_CODE (t) == QUAL_UNION_TYPE
7616 && integer_onep (DECL_QUALIFIER (new_field)))
7617 break;
7620 /* If this used to be a qualified union type, but we now know what
7621 field will be present, make this a normal union. */
7622 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
7623 && (!TYPE_FIELDS (new)
7624 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
7625 TREE_SET_CODE (new, UNION_TYPE);
7626 else if (!changed_field)
7627 return t;
7629 gcc_assert (!field_has_rep);
7630 layout_type (new);
7632 /* If the size was originally a constant use it. */
7633 if (TYPE_SIZE (t) && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
7634 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
7636 TYPE_SIZE (new) = TYPE_SIZE (t);
7637 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
7638 SET_TYPE_ADA_SIZE (new, TYPE_ADA_SIZE (t));
7641 return new;
7644 default:
7645 return t;
7649 /* Return the "RM size" of GNU_TYPE. This is the actual number of bits
7650 needed to represent the object. */
7652 tree
7653 rm_size (tree gnu_type)
7655 /* For integer types, this is the precision. For record types, we store
7656 the size explicitly. For other types, this is just the size. */
7658 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
7659 return TYPE_RM_SIZE (gnu_type);
7660 else if (TREE_CODE (gnu_type) == RECORD_TYPE
7661 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7662 /* Return the rm_size of the actual data plus the size of the template. */
7663 return
7664 size_binop (PLUS_EXPR,
7665 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
7666 DECL_SIZE (TYPE_FIELDS (gnu_type)));
7667 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
7668 || TREE_CODE (gnu_type) == UNION_TYPE
7669 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
7670 && !TYPE_IS_FAT_POINTER_P (gnu_type)
7671 && TYPE_ADA_SIZE (gnu_type))
7672 return TYPE_ADA_SIZE (gnu_type);
7673 else
7674 return TYPE_SIZE (gnu_type);
7677 /* Return an identifier representing the external name to be used for
7678 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
7679 and the specified suffix. */
7681 tree
7682 create_concat_name (Entity_Id gnat_entity, const char *suffix)
7684 Entity_Kind kind = Ekind (gnat_entity);
7686 const char *str = (!suffix ? "" : suffix);
7687 String_Template temp = {1, strlen (str)};
7688 Fat_Pointer fp = {str, &temp};
7690 Get_External_Name_With_Suffix (gnat_entity, fp);
7692 /* A variable using the Stdcall convention (meaning we are running
7693 on a Windows box) live in a DLL. Here we adjust its name to use
7694 the jump-table, the _imp__NAME contains the address for the NAME
7695 variable. */
7696 if ((kind == E_Variable || kind == E_Constant)
7697 && Has_Stdcall_Convention (gnat_entity))
7699 const char *prefix = "_imp__";
7700 int k, plen = strlen (prefix);
7702 for (k = 0; k <= Name_Len; k++)
7703 Name_Buffer [Name_Len - k + plen] = Name_Buffer [Name_Len - k];
7704 strncpy (Name_Buffer, prefix, plen);
7707 return get_identifier (Name_Buffer);
7710 /* Return the name to be used for GNAT_ENTITY. If a type, create a
7711 fully-qualified name, possibly with type information encoding.
7712 Otherwise, return the name. */
7714 tree
7715 get_entity_name (Entity_Id gnat_entity)
7717 Get_Encoded_Name (gnat_entity);
7718 return get_identifier (Name_Buffer);
7721 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
7722 string, return a new IDENTIFIER_NODE that is the concatenation of
7723 the name in GNU_ID and SUFFIX. */
7725 tree
7726 concat_id_with_name (tree gnu_id, const char *suffix)
7728 int len = IDENTIFIER_LENGTH (gnu_id);
7730 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id), len);
7731 strncpy (Name_Buffer + len, "___", 3);
7732 len += 3;
7733 strcpy (Name_Buffer + len, suffix);
7734 return get_identifier (Name_Buffer);
7737 #include "gt-ada-decl.h"