* gcc-interface/decl.c (prepend_one_attribute_pragma): Call
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blobaed49b7e9dbcf889f8479ce19aef7c051ae127d6
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2014, 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 "stringpool.h"
32 #include "stor-layout.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "ggc.h"
36 #include "target.h"
37 #include "tree-inline.h"
38 #include "diagnostic-core.h"
40 #include "ada.h"
41 #include "types.h"
42 #include "atree.h"
43 #include "elists.h"
44 #include "namet.h"
45 #include "nlists.h"
46 #include "repinfo.h"
47 #include "snames.h"
48 #include "stringt.h"
49 #include "uintp.h"
50 #include "fe.h"
51 #include "sinfo.h"
52 #include "einfo.h"
53 #include "ada-tree.h"
54 #include "gigi.h"
56 /* "stdcall" and "thiscall" conventions should be processed in a specific way
57 on 32-bit x86/Windows only. The macros below are helpers to avoid having
58 to check for a Windows specific attribute throughout this unit. */
60 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
61 #ifdef TARGET_64BIT
62 #define Has_Stdcall_Convention(E) \
63 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
64 #define Has_Thiscall_Convention(E) \
65 (!TARGET_64BIT && is_cplusplus_method (E))
66 #else
67 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
68 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
69 #endif
70 #else
71 #define Has_Stdcall_Convention(E) 0
72 #define Has_Thiscall_Convention(E) 0
73 #endif
75 /* Stack realignment is necessary for functions with foreign conventions when
76 the ABI doesn't mandate as much as what the compiler assumes - that is, up
77 to PREFERRED_STACK_BOUNDARY.
79 Such realignment can be requested with a dedicated function type attribute
80 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
81 characterize the situations where the attribute should be set. We rely on
82 compiler configuration settings for 'main' to decide. */
84 #ifdef MAIN_STACK_BOUNDARY
85 #define FOREIGN_FORCE_REALIGN_STACK \
86 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
87 #else
88 #define FOREIGN_FORCE_REALIGN_STACK 0
89 #endif
91 struct incomplete
93 struct incomplete *next;
94 tree old_type;
95 Entity_Id full_type;
98 /* These variables are used to defer recursively expanding incomplete types
99 while we are processing an array, a record or a subprogram type. */
100 static int defer_incomplete_level = 0;
101 static struct incomplete *defer_incomplete_list;
103 /* This variable is used to delay expanding From_Limited_With types until the
104 end of the spec. */
105 static struct incomplete *defer_limited_with;
107 typedef struct subst_pair_d {
108 tree discriminant;
109 tree replacement;
110 } subst_pair;
113 typedef struct variant_desc_d {
114 /* The type of the variant. */
115 tree type;
117 /* The associated field. */
118 tree field;
120 /* The value of the qualifier. */
121 tree qual;
123 /* The type of the variant after transformation. */
124 tree new_type;
125 } variant_desc;
128 /* A hash table used to cache the result of annotate_value. */
129 static GTY ((if_marked ("tree_int_map_marked_p"),
130 param_is (struct tree_int_map))) htab_t annotate_value_cache;
132 static bool allocatable_size_p (tree, bool);
133 static void prepend_one_attribute (struct attrib **,
134 enum attr_type, tree, tree, Node_Id);
135 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
136 static void prepend_attributes (struct attrib **, Entity_Id);
137 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
138 static bool type_has_variable_size (tree);
139 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
140 static tree elaborate_expression_2 (tree, Entity_Id, tree, bool, bool,
141 unsigned int);
142 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
143 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
144 bool *);
145 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
146 static bool same_discriminant_p (Entity_Id, Entity_Id);
147 static bool array_type_has_nonaliased_component (tree, Entity_Id);
148 static bool compile_time_known_address_p (Node_Id);
149 static bool cannot_be_superflat_p (Node_Id);
150 static bool constructor_address_p (tree);
151 static int compare_field_bitpos (const PTR, const PTR);
152 static bool components_to_record (tree, Node_Id, tree, int, bool, bool, bool,
153 bool, bool, bool, bool, bool, tree, tree *);
154 static Uint annotate_value (tree);
155 static void annotate_rep (Entity_Id, tree);
156 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
157 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
158 static vec<variant_desc> build_variant_list (tree,
159 vec<subst_pair> ,
160 vec<variant_desc> );
161 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
162 static void set_rm_size (Uint, tree, Entity_Id);
163 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
164 static void check_ok_for_atomic (tree, Entity_Id, bool);
165 static tree create_field_decl_from (tree, tree, tree, tree, tree,
166 vec<subst_pair> );
167 static tree create_rep_part (tree, tree, tree);
168 static tree get_rep_part (tree);
169 static tree create_variant_part_from (tree, vec<variant_desc> , tree,
170 tree, vec<subst_pair> );
171 static void copy_and_substitute_in_size (tree, tree, vec<subst_pair> );
173 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
174 to pass around calls performing profile compatibility checks. */
176 typedef struct {
177 Entity_Id gnat_entity; /* The Ada subprogram entity. */
178 tree ada_fntype; /* The corresponding GCC type node. */
179 tree btin_fntype; /* The GCC builtin function type node. */
180 } intrin_binding_t;
182 static bool intrin_profiles_compatible_p (intrin_binding_t *);
184 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
185 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
186 and associate the ..._DECL node with the input GNAT defining identifier.
188 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
189 initial value (in GCC tree form). This is optional for a variable. For
190 a renamed entity, GNU_EXPR gives the object being renamed.
192 DEFINITION is nonzero if this call is intended for a definition. This is
193 used for separate compilation where it is necessary to know whether an
194 external declaration or a definition must be created if the GCC equivalent
195 was not created previously. The value of 1 is normally used for a nonzero
196 DEFINITION, but a value of 2 is used in special circumstances, defined in
197 the code. */
199 tree
200 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
202 /* Contains the kind of the input GNAT node. */
203 const Entity_Kind kind = Ekind (gnat_entity);
204 /* True if this is a type. */
205 const bool is_type = IN (kind, Type_Kind);
206 /* True if debug info is requested for this entity. */
207 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
208 /* True if this entity is to be considered as imported. */
209 const bool imported_p
210 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
211 /* For a type, contains the equivalent GNAT node to be used in gigi. */
212 Entity_Id gnat_equiv_type = Empty;
213 /* Temporary used to walk the GNAT tree. */
214 Entity_Id gnat_temp;
215 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
216 This node will be associated with the GNAT node by calling at the end
217 of the `switch' statement. */
218 tree gnu_decl = NULL_TREE;
219 /* Contains the GCC type to be used for the GCC node. */
220 tree gnu_type = NULL_TREE;
221 /* Contains the GCC size tree to be used for the GCC node. */
222 tree gnu_size = NULL_TREE;
223 /* Contains the GCC name to be used for the GCC node. */
224 tree gnu_entity_name;
225 /* True if we have already saved gnu_decl as a GNAT association. */
226 bool saved = false;
227 /* True if we incremented defer_incomplete_level. */
228 bool this_deferred = false;
229 /* True if we incremented force_global. */
230 bool this_global = false;
231 /* True if we should check to see if elaborated during processing. */
232 bool maybe_present = false;
233 /* True if we made GNU_DECL and its type here. */
234 bool this_made_decl = false;
235 /* Size and alignment of the GCC node, if meaningful. */
236 unsigned int esize = 0, align = 0;
237 /* Contains the list of attributes directly attached to the entity. */
238 struct attrib *attr_list = NULL;
240 /* Since a use of an Itype is a definition, process it as such if it
241 is not in a with'ed unit. */
242 if (!definition
243 && is_type
244 && Is_Itype (gnat_entity)
245 && !present_gnu_tree (gnat_entity)
246 && In_Extended_Main_Code_Unit (gnat_entity))
248 /* Ensure that we are in a subprogram mentioned in the Scope chain of
249 this entity, our current scope is global, or we encountered a task
250 or entry (where we can't currently accurately check scoping). */
251 if (!current_function_decl
252 || DECL_ELABORATION_PROC_P (current_function_decl))
254 process_type (gnat_entity);
255 return get_gnu_tree (gnat_entity);
258 for (gnat_temp = Scope (gnat_entity);
259 Present (gnat_temp);
260 gnat_temp = Scope (gnat_temp))
262 if (Is_Type (gnat_temp))
263 gnat_temp = Underlying_Type (gnat_temp);
265 if (Ekind (gnat_temp) == E_Subprogram_Body)
266 gnat_temp
267 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
269 if (IN (Ekind (gnat_temp), Subprogram_Kind)
270 && Present (Protected_Body_Subprogram (gnat_temp)))
271 gnat_temp = Protected_Body_Subprogram (gnat_temp);
273 if (Ekind (gnat_temp) == E_Entry
274 || Ekind (gnat_temp) == E_Entry_Family
275 || Ekind (gnat_temp) == E_Task_Type
276 || (IN (Ekind (gnat_temp), Subprogram_Kind)
277 && present_gnu_tree (gnat_temp)
278 && (current_function_decl
279 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
281 process_type (gnat_entity);
282 return get_gnu_tree (gnat_entity);
286 /* This abort means the Itype has an incorrect scope, i.e. that its
287 scope does not correspond to the subprogram it is declared in. */
288 gcc_unreachable ();
291 /* If we've already processed this entity, return what we got last time.
292 If we are defining the node, we should not have already processed it.
293 In that case, we will abort below when we try to save a new GCC tree
294 for this object. We also need to handle the case of getting a dummy
295 type when a Full_View exists but be careful so as not to trigger its
296 premature elaboration. */
297 if ((!definition || (is_type && imported_p))
298 && present_gnu_tree (gnat_entity))
300 gnu_decl = get_gnu_tree (gnat_entity);
302 if (TREE_CODE (gnu_decl) == TYPE_DECL
303 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
304 && IN (kind, Incomplete_Or_Private_Kind)
305 && Present (Full_View (gnat_entity))
306 && (present_gnu_tree (Full_View (gnat_entity))
307 || No (Freeze_Node (Full_View (gnat_entity)))))
309 gnu_decl
310 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
311 save_gnu_tree (gnat_entity, NULL_TREE, false);
312 save_gnu_tree (gnat_entity, gnu_decl, false);
315 return gnu_decl;
318 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
319 must be specified unless it was specified by the programmer. Exceptions
320 are for access-to-protected-subprogram types and all access subtypes, as
321 another GNAT type is used to lay out the GCC type for them. */
322 gcc_assert (!Unknown_Esize (gnat_entity)
323 || Has_Size_Clause (gnat_entity)
324 || (!IN (kind, Numeric_Kind)
325 && !IN (kind, Enumeration_Kind)
326 && (!IN (kind, Access_Kind)
327 || kind == E_Access_Protected_Subprogram_Type
328 || kind == E_Anonymous_Access_Protected_Subprogram_Type
329 || kind == E_Access_Subtype
330 || type_annotate_only)));
332 /* The RM size must be specified for all discrete and fixed-point types. */
333 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
334 && Unknown_RM_Size (gnat_entity)));
336 /* If we get here, it means we have not yet done anything with this entity.
337 If we are not defining it, it must be a type or an entity that is defined
338 elsewhere or externally, otherwise we should have defined it already. */
339 gcc_assert (definition
340 || type_annotate_only
341 || is_type
342 || kind == E_Discriminant
343 || kind == E_Component
344 || kind == E_Label
345 || (kind == E_Constant && Present (Full_View (gnat_entity)))
346 || Is_Public (gnat_entity));
348 /* Get the name of the entity and set up the line number and filename of
349 the original definition for use in any decl we make. */
350 gnu_entity_name = get_entity_name (gnat_entity);
351 Sloc_to_locus (Sloc (gnat_entity), &input_location);
353 /* For cases when we are not defining (i.e., we are referencing from
354 another compilation unit) public entities, show we are at global level
355 for the purpose of computing scopes. Don't do this for components or
356 discriminants since the relevant test is whether or not the record is
357 being defined. */
358 if (!definition
359 && kind != E_Component
360 && kind != E_Discriminant
361 && Is_Public (gnat_entity)
362 && !Is_Statically_Allocated (gnat_entity))
363 force_global++, this_global = true;
365 /* Handle any attributes directly attached to the entity. */
366 if (Has_Gigi_Rep_Item (gnat_entity))
367 prepend_attributes (&attr_list, gnat_entity);
369 /* Do some common processing for types. */
370 if (is_type)
372 /* Compute the equivalent type to be used in gigi. */
373 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
375 /* Machine_Attributes on types are expected to be propagated to
376 subtypes. The corresponding Gigi_Rep_Items are only attached
377 to the first subtype though, so we handle the propagation here. */
378 if (Base_Type (gnat_entity) != gnat_entity
379 && !Is_First_Subtype (gnat_entity)
380 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
381 prepend_attributes (&attr_list,
382 First_Subtype (Base_Type (gnat_entity)));
384 /* Compute a default value for the size of an elementary type. */
385 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
387 unsigned int max_esize;
389 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
390 esize = UI_To_Int (Esize (gnat_entity));
392 if (IN (kind, Float_Kind))
393 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
394 else if (IN (kind, Access_Kind))
395 max_esize = POINTER_SIZE * 2;
396 else
397 max_esize = LONG_LONG_TYPE_SIZE;
399 if (esize > max_esize)
400 esize = max_esize;
404 switch (kind)
406 case E_Constant:
407 /* If this is a use of a deferred constant without address clause,
408 get its full definition. */
409 if (!definition
410 && No (Address_Clause (gnat_entity))
411 && Present (Full_View (gnat_entity)))
413 gnu_decl
414 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
415 saved = true;
416 break;
419 /* If we have an external constant that we are not defining, get the
420 expression that is was defined to represent. We may throw it away
421 later if it is not a constant. But do not retrieve the expression
422 if it is an allocator because the designated type might be dummy
423 at this point. */
424 if (!definition
425 && !No_Initialization (Declaration_Node (gnat_entity))
426 && Present (Expression (Declaration_Node (gnat_entity)))
427 && Nkind (Expression (Declaration_Node (gnat_entity)))
428 != N_Allocator)
430 bool went_into_elab_proc = false;
431 int save_force_global = force_global;
433 /* The expression may contain N_Expression_With_Actions nodes and
434 thus object declarations from other units. In this case, even
435 though the expression will eventually be discarded since not a
436 constant, the declarations would be stuck either in the global
437 varpool or in the current scope. Therefore we force the local
438 context and create a fake scope that we'll zap at the end. */
439 if (!current_function_decl)
441 current_function_decl = get_elaboration_procedure ();
442 went_into_elab_proc = true;
444 force_global = 0;
445 gnat_pushlevel ();
447 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
449 gnat_zaplevel ();
450 force_global = save_force_global;
451 if (went_into_elab_proc)
452 current_function_decl = NULL_TREE;
455 /* Ignore deferred constant definitions without address clause since
456 they are processed fully in the front-end. If No_Initialization
457 is set, this is not a deferred constant but a constant whose value
458 is built manually. And constants that are renamings are handled
459 like variables. */
460 if (definition
461 && !gnu_expr
462 && No (Address_Clause (gnat_entity))
463 && !No_Initialization (Declaration_Node (gnat_entity))
464 && No (Renamed_Object (gnat_entity)))
466 gnu_decl = error_mark_node;
467 saved = true;
468 break;
471 /* Ignore constant definitions already marked with the error node. See
472 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
473 if (definition
474 && gnu_expr
475 && present_gnu_tree (gnat_entity)
476 && get_gnu_tree (gnat_entity) == error_mark_node)
478 maybe_present = true;
479 break;
482 goto object;
484 case E_Exception:
485 /* We used to special case VMS exceptions here to directly map them to
486 their associated condition code. Since this code had to be masked
487 dynamically to strip off the severity bits, this caused trouble in
488 the GCC/ZCX case because the "type" pointers we store in the tables
489 have to be static. We now don't special case here anymore, and let
490 the regular processing take place, which leaves us with a regular
491 exception data object for VMS exceptions too. The condition code
492 mapping is taken care of by the front end and the bitmasking by the
493 run-time library. */
494 goto object;
496 case E_Component:
497 case E_Discriminant:
499 /* The GNAT record where the component was defined. */
500 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
502 /* If the entity is an inherited component (in the case of extended
503 tagged record types), just return the original entity, which must
504 be a FIELD_DECL. Likewise for discriminants. If the entity is a
505 non-girder discriminant (in the case of derived untagged record
506 types), return the stored discriminant it renames. */
507 if (Present (Original_Record_Component (gnat_entity))
508 && Original_Record_Component (gnat_entity) != gnat_entity)
510 gnu_decl
511 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
512 gnu_expr, definition);
513 saved = true;
514 break;
517 /* If this is a discriminant of an extended tagged type used to rename
518 a discriminant of the parent type, return the latter. */
519 else if (Present (Corresponding_Discriminant (gnat_entity)))
521 /* If the derived type is untagged, then this is a non-girder
522 discriminant and its Original_Record_Component must point to
523 the stored discriminant it renames (i.e. we should have taken
524 the previous branch). */
525 gcc_assert (Is_Tagged_Type (gnat_record));
527 gnu_decl
528 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
529 gnu_expr, definition);
530 saved = true;
531 break;
534 /* Otherwise, if we are not defining this and we have no GCC type
535 for the containing record, make one for it. Then we should
536 have made our own equivalent. */
537 else if (!definition && !present_gnu_tree (gnat_record))
539 /* ??? If this is in a record whose scope is a protected
540 type and we have an Original_Record_Component, use it.
541 This is a workaround for major problems in protected type
542 handling. */
543 Entity_Id Scop = Scope (Scope (gnat_entity));
544 if ((Is_Protected_Type (Scop)
545 || (Is_Private_Type (Scop)
546 && Present (Full_View (Scop))
547 && Is_Protected_Type (Full_View (Scop))))
548 && Present (Original_Record_Component (gnat_entity)))
550 gnu_decl
551 = gnat_to_gnu_entity (Original_Record_Component
552 (gnat_entity),
553 gnu_expr, 0);
554 saved = true;
555 break;
558 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
559 gnu_decl = get_gnu_tree (gnat_entity);
560 saved = true;
561 break;
564 else
565 /* Here we have no GCC type and this is a reference rather than a
566 definition. This should never happen. Most likely the cause is
567 reference before declaration in the GNAT tree for gnat_entity. */
568 gcc_unreachable ();
571 case E_Loop_Parameter:
572 case E_Out_Parameter:
573 case E_Variable:
575 /* Simple variables, loop variables, Out parameters and exceptions. */
576 object:
578 /* Always create a variable for volatile objects and variables seen
579 constant but with a Linker_Section pragma. */
580 bool const_flag
581 = ((kind == E_Constant || kind == E_Variable)
582 && Is_True_Constant (gnat_entity)
583 && !(kind == E_Variable
584 && Present (Linker_Section_Pragma (gnat_entity)))
585 && !Treat_As_Volatile (gnat_entity)
586 && (((Nkind (Declaration_Node (gnat_entity))
587 == N_Object_Declaration)
588 && Present (Expression (Declaration_Node (gnat_entity))))
589 || Present (Renamed_Object (gnat_entity))
590 || imported_p));
591 bool inner_const_flag = const_flag;
592 bool static_p = Is_Statically_Allocated (gnat_entity);
593 bool mutable_p = false;
594 bool used_by_ref = false;
595 tree gnu_ext_name = NULL_TREE;
596 tree renamed_obj = NULL_TREE;
597 tree gnu_object_size;
599 if (Present (Renamed_Object (gnat_entity)) && !definition)
601 if (kind == E_Exception)
602 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
603 NULL_TREE, 0);
604 else
605 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
608 /* Get the type after elaborating the renamed object. */
609 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
611 /* If this is a standard exception definition, then use the standard
612 exception type. This is necessary to make sure that imported and
613 exported views of exceptions are properly merged in LTO mode. */
614 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
615 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
616 gnu_type = except_type_node;
618 /* For a debug renaming declaration, build a debug-only entity. */
619 if (Present (Debug_Renaming_Link (gnat_entity)))
621 /* Force a non-null value to make sure the symbol is retained. */
622 tree value = build1 (INDIRECT_REF, gnu_type,
623 build1 (NOP_EXPR,
624 build_pointer_type (gnu_type),
625 integer_minus_one_node));
626 gnu_decl = build_decl (input_location,
627 VAR_DECL, gnu_entity_name, gnu_type);
628 SET_DECL_VALUE_EXPR (gnu_decl, value);
629 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
630 gnat_pushdecl (gnu_decl, gnat_entity);
631 break;
634 /* If this is a loop variable, its type should be the base type.
635 This is because the code for processing a loop determines whether
636 a normal loop end test can be done by comparing the bounds of the
637 loop against those of the base type, which is presumed to be the
638 size used for computation. But this is not correct when the size
639 of the subtype is smaller than the type. */
640 if (kind == E_Loop_Parameter)
641 gnu_type = get_base_type (gnu_type);
643 /* Reject non-renamed objects whose type is an unconstrained array or
644 any object whose type is a dummy type or void. */
645 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
646 && No (Renamed_Object (gnat_entity)))
647 || TYPE_IS_DUMMY_P (gnu_type)
648 || TREE_CODE (gnu_type) == VOID_TYPE)
650 gcc_assert (type_annotate_only);
651 if (this_global)
652 force_global--;
653 return error_mark_node;
656 /* If an alignment is specified, use it if valid. Note that exceptions
657 are objects but don't have an alignment. We must do this before we
658 validate the size, since the alignment can affect the size. */
659 if (kind != E_Exception && Known_Alignment (gnat_entity))
661 gcc_assert (Present (Alignment (gnat_entity)));
663 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
664 TYPE_ALIGN (gnu_type));
666 /* No point in changing the type if there is an address clause
667 as the final type of the object will be a reference type. */
668 if (Present (Address_Clause (gnat_entity)))
669 align = 0;
670 else
672 tree orig_type = gnu_type;
674 gnu_type
675 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
676 false, false, definition, true);
678 /* If a padding record was made, declare it now since it will
679 never be declared otherwise. This is necessary to ensure
680 that its subtrees are properly marked. */
681 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
682 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
683 debug_info_p, gnat_entity);
687 /* If we are defining the object, see if it has a Size and validate it
688 if so. If we are not defining the object and a Size clause applies,
689 simply retrieve the value. We don't want to ignore the clause and
690 it is expected to have been validated already. Then get the new
691 type, if any. */
692 if (definition)
693 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
694 gnat_entity, VAR_DECL, false,
695 Has_Size_Clause (gnat_entity));
696 else if (Has_Size_Clause (gnat_entity))
697 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
699 if (gnu_size)
701 gnu_type
702 = make_type_from_size (gnu_type, gnu_size,
703 Has_Biased_Representation (gnat_entity));
705 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
706 gnu_size = NULL_TREE;
709 /* If this object has self-referential size, it must be a record with
710 a default discriminant. We are supposed to allocate an object of
711 the maximum size in this case, unless it is a constant with an
712 initializing expression, in which case we can get the size from
713 that. Note that the resulting size may still be a variable, so
714 this may end up with an indirect allocation. */
715 if (No (Renamed_Object (gnat_entity))
716 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
718 if (gnu_expr && kind == E_Constant)
720 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
721 if (CONTAINS_PLACEHOLDER_P (size))
723 /* If the initializing expression is itself a constant,
724 despite having a nominal type with self-referential
725 size, we can get the size directly from it. */
726 if (TREE_CODE (gnu_expr) == COMPONENT_REF
727 && TYPE_IS_PADDING_P
728 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
729 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
730 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
731 || DECL_READONLY_ONCE_ELAB
732 (TREE_OPERAND (gnu_expr, 0))))
733 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
734 else
735 gnu_size
736 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
738 else
739 gnu_size = size;
741 /* We may have no GNU_EXPR because No_Initialization is
742 set even though there's an Expression. */
743 else if (kind == E_Constant
744 && (Nkind (Declaration_Node (gnat_entity))
745 == N_Object_Declaration)
746 && Present (Expression (Declaration_Node (gnat_entity))))
747 gnu_size
748 = TYPE_SIZE (gnat_to_gnu_type
749 (Etype
750 (Expression (Declaration_Node (gnat_entity)))));
751 else
753 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
754 mutable_p = true;
757 /* If we are at global level and the size isn't constant, call
758 elaborate_expression_1 to make a variable for it rather than
759 calculating it each time. */
760 if (global_bindings_p () && !TREE_CONSTANT (gnu_size))
761 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
762 get_identifier ("SIZE"),
763 definition, false);
766 /* If the size is zero byte, make it one byte since some linkers have
767 troubles with zero-sized objects. If the object will have a
768 template, that will make it nonzero so don't bother. Also avoid
769 doing that for an object renaming or an object with an address
770 clause, as we would lose useful information on the view size
771 (e.g. for null array slices) and we are not allocating the object
772 here anyway. */
773 if (((gnu_size
774 && integer_zerop (gnu_size)
775 && !TREE_OVERFLOW (gnu_size))
776 || (TYPE_SIZE (gnu_type)
777 && integer_zerop (TYPE_SIZE (gnu_type))
778 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
779 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
780 && No (Renamed_Object (gnat_entity))
781 && No (Address_Clause (gnat_entity)))
782 gnu_size = bitsize_unit_node;
784 /* If this is an object with no specified size and alignment, and
785 if either it is atomic or we are not optimizing alignment for
786 space and it is composite and not an exception, an Out parameter
787 or a reference to another object, and the size of its type is a
788 constant, set the alignment to the smallest one which is not
789 smaller than the size, with an appropriate cap. */
790 if (!gnu_size && align == 0
791 && (Is_Atomic (gnat_entity)
792 || (!Optimize_Alignment_Space (gnat_entity)
793 && kind != E_Exception
794 && kind != E_Out_Parameter
795 && Is_Composite_Type (Etype (gnat_entity))
796 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
797 && !Is_Exported (gnat_entity)
798 && !imported_p
799 && No (Renamed_Object (gnat_entity))
800 && No (Address_Clause (gnat_entity))))
801 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
803 unsigned int size_cap, align_cap;
805 /* No point in promoting the alignment if this doesn't prevent
806 BLKmode access to the object, in particular block copy, as
807 this will for example disable the NRV optimization for it.
808 No point in jumping through all the hoops needed in order
809 to support BIGGEST_ALIGNMENT if we don't really have to.
810 So we cap to the smallest alignment that corresponds to
811 a known efficient memory access pattern of the target. */
812 if (Is_Atomic (gnat_entity))
814 size_cap = UINT_MAX;
815 align_cap = BIGGEST_ALIGNMENT;
817 else
819 size_cap = MAX_FIXED_MODE_SIZE;
820 align_cap = get_mode_alignment (ptr_mode);
823 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
824 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
825 align = 0;
826 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
827 align = align_cap;
828 else
829 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
831 /* But make sure not to under-align the object. */
832 if (align <= TYPE_ALIGN (gnu_type))
833 align = 0;
835 /* And honor the minimum valid atomic alignment, if any. */
836 #ifdef MINIMUM_ATOMIC_ALIGNMENT
837 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
838 align = MINIMUM_ATOMIC_ALIGNMENT;
839 #endif
842 /* If the object is set to have atomic components, find the component
843 type and validate it.
845 ??? Note that we ignore Has_Volatile_Components on objects; it's
846 not at all clear what to do in that case. */
847 if (Has_Atomic_Components (gnat_entity))
849 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
850 ? TREE_TYPE (gnu_type) : gnu_type);
852 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
853 && TYPE_MULTI_ARRAY_P (gnu_inner))
854 gnu_inner = TREE_TYPE (gnu_inner);
856 check_ok_for_atomic (gnu_inner, gnat_entity, true);
859 /* Now check if the type of the object allows atomic access. Note
860 that we must test the type, even if this object has size and
861 alignment to allow such access, because we will be going inside
862 the padded record to assign to the object. We could fix this by
863 always copying via an intermediate value, but it's not clear it's
864 worth the effort. */
865 if (Is_Atomic (gnat_entity))
866 check_ok_for_atomic (gnu_type, gnat_entity, false);
868 /* If this is an aliased object with an unconstrained nominal subtype,
869 make a type that includes the template. */
870 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
871 && (Is_Array_Type (Etype (gnat_entity))
872 || (Is_Private_Type (Etype (gnat_entity))
873 && Is_Array_Type (Full_View (Etype (gnat_entity)))))
874 && !type_annotate_only)
876 tree gnu_array
877 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
878 gnu_type
879 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
880 gnu_type,
881 concat_name (gnu_entity_name,
882 "UNC"),
883 debug_info_p);
886 /* ??? If this is an object of CW type initialized to a value, try to
887 ensure that the object is sufficient aligned for this value, but
888 without pessimizing the allocation. This is a kludge necessary
889 because we don't support dynamic alignment. */
890 if (align == 0
891 && Ekind (Etype (gnat_entity)) == E_Class_Wide_Subtype
892 && No (Renamed_Object (gnat_entity))
893 && No (Address_Clause (gnat_entity)))
894 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
896 #ifdef MINIMUM_ATOMIC_ALIGNMENT
897 /* If the size is a constant and no alignment is specified, force
898 the alignment to be the minimum valid atomic alignment. The
899 restriction on constant size avoids problems with variable-size
900 temporaries; if the size is variable, there's no issue with
901 atomic access. Also don't do this for a constant, since it isn't
902 necessary and can interfere with constant replacement. Finally,
903 do not do it for Out parameters since that creates an
904 size inconsistency with In parameters. */
905 if (align == 0
906 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
907 && !FLOAT_TYPE_P (gnu_type)
908 && !const_flag && No (Renamed_Object (gnat_entity))
909 && !imported_p && No (Address_Clause (gnat_entity))
910 && kind != E_Out_Parameter
911 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
912 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
913 align = MINIMUM_ATOMIC_ALIGNMENT;
914 #endif
916 /* Make a new type with the desired size and alignment, if needed.
917 But do not take into account alignment promotions to compute the
918 size of the object. */
919 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
920 if (gnu_size || align > 0)
922 tree orig_type = gnu_type;
924 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
925 false, false, definition, true);
927 /* If a padding record was made, declare it now since it will
928 never be declared otherwise. This is necessary to ensure
929 that its subtrees are properly marked. */
930 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
931 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
932 debug_info_p, gnat_entity);
935 /* If this is a renaming, avoid as much as possible to create a new
936 object. However, in several cases, creating it is required.
937 This processing needs to be applied to the raw expression so
938 as to make it more likely to rename the underlying object. */
939 if (Present (Renamed_Object (gnat_entity)))
941 bool create_normal_object = false;
943 /* If the renamed object had padding, strip off the reference
944 to the inner object and reset our type. */
945 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
946 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
947 /* Strip useless conversions around the object. */
948 || gnat_useless_type_conversion (gnu_expr))
950 gnu_expr = TREE_OPERAND (gnu_expr, 0);
951 gnu_type = TREE_TYPE (gnu_expr);
954 /* Or else, if the renamed object has an unconstrained type with
955 default discriminant, use the padded type. */
956 else if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_expr))
957 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_expr)))
958 == gnu_type
959 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
960 gnu_type = TREE_TYPE (gnu_expr);
962 /* Case 1: If this is a constant renaming stemming from a function
963 call, treat it as a normal object whose initial value is what
964 is being renamed. RM 3.3 says that the result of evaluating a
965 function call is a constant object. As a consequence, it can
966 be the inner object of a constant renaming. In this case, the
967 renaming must be fully instantiated, i.e. it cannot be a mere
968 reference to (part of) an existing object. */
969 if (const_flag)
971 tree inner_object = gnu_expr;
972 while (handled_component_p (inner_object))
973 inner_object = TREE_OPERAND (inner_object, 0);
974 if (TREE_CODE (inner_object) == CALL_EXPR)
975 create_normal_object = true;
978 /* Otherwise, see if we can proceed with a stabilized version of
979 the renamed entity or if we need to make a new object. */
980 if (!create_normal_object)
982 tree maybe_stable_expr = NULL_TREE;
983 bool stable = false;
985 /* Case 2: If the renaming entity need not be materialized and
986 the renamed expression is something we can stabilize, use
987 that for the renaming. At the global level, we can only do
988 this if we know no SAVE_EXPRs need be made, because the
989 expression we return might be used in arbitrary conditional
990 branches so we must force the evaluation of the SAVE_EXPRs
991 immediately and this requires a proper function context.
992 Note that an external constant is at the global level. */
993 if (!Materialize_Entity (gnat_entity)
994 && (!((!definition && kind == E_Constant)
995 || global_bindings_p ())
996 || (staticp (gnu_expr)
997 && !TREE_SIDE_EFFECTS (gnu_expr))))
999 maybe_stable_expr
1000 = gnat_stabilize_reference (gnu_expr, true, &stable);
1002 if (stable)
1004 /* ??? No DECL_EXPR is created so we need to mark
1005 the expression manually lest it is shared. */
1006 if ((!definition && kind == E_Constant)
1007 || global_bindings_p ())
1008 MARK_VISITED (maybe_stable_expr);
1009 gnu_decl = maybe_stable_expr;
1010 save_gnu_tree (gnat_entity, gnu_decl, true);
1011 saved = true;
1012 annotate_object (gnat_entity, gnu_type, NULL_TREE,
1013 false);
1014 /* This assertion will fail if the renamed object
1015 isn't aligned enough as to make it possible to
1016 honor the alignment set on the renaming. */
1017 if (align)
1019 unsigned int renamed_align
1020 = DECL_P (gnu_decl)
1021 ? DECL_ALIGN (gnu_decl)
1022 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1023 gcc_assert (renamed_align >= align);
1025 break;
1028 /* The stabilization failed. Keep maybe_stable_expr
1029 untouched here to let the pointer case below know
1030 about that failure. */
1033 /* Case 3: If this is a constant renaming and creating a
1034 new object is allowed and cheap, treat it as a normal
1035 object whose initial value is what is being renamed. */
1036 if (const_flag
1037 && !Is_Composite_Type
1038 (Underlying_Type (Etype (gnat_entity))))
1041 /* Case 4: Make this into a constant pointer to the object we
1042 are to rename and attach the object to the pointer if it is
1043 something we can stabilize.
1045 From the proper scope, attached objects will be referenced
1046 directly instead of indirectly via the pointer to avoid
1047 subtle aliasing problems with non-addressable entities.
1048 They have to be stable because we must not evaluate the
1049 variables in the expression every time the renaming is used.
1050 The pointer is called a "renaming" pointer in this case.
1052 In the rare cases where we cannot stabilize the renamed
1053 object, we just make a "bare" pointer, and the renamed
1054 entity is always accessed indirectly through it. */
1055 else
1057 /* We need to preserve the volatileness of the renamed
1058 object through the indirection. */
1059 if (TREE_THIS_VOLATILE (gnu_expr)
1060 && !TYPE_VOLATILE (gnu_type))
1061 gnu_type
1062 = build_qualified_type (gnu_type,
1063 (TYPE_QUALS (gnu_type)
1064 | TYPE_QUAL_VOLATILE));
1065 gnu_type = build_reference_type (gnu_type);
1066 inner_const_flag = TREE_READONLY (gnu_expr);
1067 const_flag = true;
1069 /* If the previous attempt at stabilizing failed, there
1070 is no point in trying again and we reuse the result
1071 without attaching it to the pointer. In this case it
1072 will only be used as the initializing expression of
1073 the pointer and thus needs no special treatment with
1074 regard to multiple evaluations. */
1075 if (maybe_stable_expr)
1078 /* Otherwise, try to stabilize and attach the expression
1079 to the pointer if the stabilization succeeds.
1081 Note that this might introduce SAVE_EXPRs and we don't
1082 check whether we're at the global level or not. This
1083 is fine since we are building a pointer initializer and
1084 neither the pointer nor the initializing expression can
1085 be accessed before the pointer elaboration has taken
1086 place in a correct program.
1088 These SAVE_EXPRs will be evaluated at the right place
1089 by either the evaluation of the initializer for the
1090 non-global case or the elaboration code for the global
1091 case, and will be attached to the elaboration procedure
1092 in the latter case. */
1093 else
1095 maybe_stable_expr
1096 = gnat_stabilize_reference (gnu_expr, true, &stable);
1098 if (stable)
1099 renamed_obj = maybe_stable_expr;
1101 /* Attaching is actually performed downstream, as soon
1102 as we have a VAR_DECL for the pointer we make. */
1105 if (type_annotate_only
1106 && TREE_CODE (maybe_stable_expr) == ERROR_MARK)
1107 gnu_expr = NULL_TREE;
1108 else
1109 gnu_expr = build_unary_op (ADDR_EXPR, gnu_type,
1110 maybe_stable_expr);
1112 gnu_size = NULL_TREE;
1113 used_by_ref = true;
1118 /* Make a volatile version of this object's type if we are to make
1119 the object volatile. We also interpret 13.3(19) conservatively
1120 and disallow any optimizations for such a non-constant object. */
1121 if ((Treat_As_Volatile (gnat_entity)
1122 || (!const_flag
1123 && gnu_type != except_type_node
1124 && (Is_Exported (gnat_entity)
1125 || imported_p
1126 || Present (Address_Clause (gnat_entity)))))
1127 && !TYPE_VOLATILE (gnu_type))
1128 gnu_type = build_qualified_type (gnu_type,
1129 (TYPE_QUALS (gnu_type)
1130 | TYPE_QUAL_VOLATILE));
1132 /* If we are defining an aliased object whose nominal subtype is
1133 unconstrained, the object is a record that contains both the
1134 template and the object. If there is an initializer, it will
1135 have already been converted to the right type, but we need to
1136 create the template if there is no initializer. */
1137 if (definition
1138 && !gnu_expr
1139 && TREE_CODE (gnu_type) == RECORD_TYPE
1140 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1141 /* Beware that padding might have been introduced above. */
1142 || (TYPE_PADDING_P (gnu_type)
1143 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1144 == RECORD_TYPE
1145 && TYPE_CONTAINS_TEMPLATE_P
1146 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1148 tree template_field
1149 = TYPE_PADDING_P (gnu_type)
1150 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1151 : TYPE_FIELDS (gnu_type);
1152 vec<constructor_elt, va_gc> *v;
1153 vec_alloc (v, 1);
1154 tree t = build_template (TREE_TYPE (template_field),
1155 TREE_TYPE (DECL_CHAIN (template_field)),
1156 NULL_TREE);
1157 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1158 gnu_expr = gnat_build_constructor (gnu_type, v);
1161 /* Convert the expression to the type of the object except in the
1162 case where the object's type is unconstrained or the object's type
1163 is a padded record whose field is of self-referential size. In
1164 the former case, converting will generate unnecessary evaluations
1165 of the CONSTRUCTOR to compute the size and in the latter case, we
1166 want to only copy the actual data. Also don't convert to a record
1167 type with a variant part from a record type without one, to keep
1168 the object simpler. */
1169 if (gnu_expr
1170 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1171 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1172 && !(TYPE_IS_PADDING_P (gnu_type)
1173 && CONTAINS_PLACEHOLDER_P
1174 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1175 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1176 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1177 && get_variant_part (gnu_type) != NULL_TREE
1178 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1179 gnu_expr = convert (gnu_type, gnu_expr);
1181 /* If this is a pointer that doesn't have an initializing expression,
1182 initialize it to NULL, unless the object is imported. */
1183 if (definition
1184 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1185 && !gnu_expr
1186 && !Is_Imported (gnat_entity))
1187 gnu_expr = integer_zero_node;
1189 /* If we are defining the object and it has an Address clause, we must
1190 either get the address expression from the saved GCC tree for the
1191 object if it has a Freeze node, or elaborate the address expression
1192 here since the front-end has guaranteed that the elaboration has no
1193 effects in this case. */
1194 if (definition && Present (Address_Clause (gnat_entity)))
1196 Node_Id gnat_expr = Expression (Address_Clause (gnat_entity));
1197 tree gnu_address
1198 = present_gnu_tree (gnat_entity)
1199 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1201 save_gnu_tree (gnat_entity, NULL_TREE, false);
1203 /* Ignore the size. It's either meaningless or was handled
1204 above. */
1205 gnu_size = NULL_TREE;
1206 /* Convert the type of the object to a reference type that can
1207 alias everything as per 13.3(19). */
1208 gnu_type
1209 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1210 gnu_address = convert (gnu_type, gnu_address);
1211 used_by_ref = true;
1212 const_flag
1213 = !Is_Public (gnat_entity)
1214 || compile_time_known_address_p (gnat_expr);
1216 /* If this is a deferred constant, the initializer is attached to
1217 the full view. */
1218 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1219 gnu_expr
1220 = gnat_to_gnu
1221 (Expression (Declaration_Node (Full_View (gnat_entity))));
1223 /* If we don't have an initializing expression for the underlying
1224 variable, the initializing expression for the pointer is the
1225 specified address. Otherwise, we have to make a COMPOUND_EXPR
1226 to assign both the address and the initial value. */
1227 if (!gnu_expr)
1228 gnu_expr = gnu_address;
1229 else
1230 gnu_expr
1231 = build2 (COMPOUND_EXPR, gnu_type,
1232 build_binary_op
1233 (MODIFY_EXPR, NULL_TREE,
1234 build_unary_op (INDIRECT_REF, NULL_TREE,
1235 gnu_address),
1236 gnu_expr),
1237 gnu_address);
1240 /* If it has an address clause and we are not defining it, mark it
1241 as an indirect object. Likewise for Stdcall objects that are
1242 imported. */
1243 if ((!definition && Present (Address_Clause (gnat_entity)))
1244 || (Is_Imported (gnat_entity)
1245 && Has_Stdcall_Convention (gnat_entity)))
1247 /* Convert the type of the object to a reference type that can
1248 alias everything as per 13.3(19). */
1249 gnu_type
1250 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1251 gnu_size = NULL_TREE;
1253 /* No point in taking the address of an initializing expression
1254 that isn't going to be used. */
1255 gnu_expr = NULL_TREE;
1257 /* If it has an address clause whose value is known at compile
1258 time, make the object a CONST_DECL. This will avoid a
1259 useless dereference. */
1260 if (Present (Address_Clause (gnat_entity)))
1262 Node_Id gnat_address
1263 = Expression (Address_Clause (gnat_entity));
1265 if (compile_time_known_address_p (gnat_address))
1267 gnu_expr = gnat_to_gnu (gnat_address);
1268 const_flag = true;
1272 used_by_ref = true;
1275 /* If we are at top level and this object is of variable size,
1276 make the actual type a hidden pointer to the real type and
1277 make the initializer be a memory allocation and initialization.
1278 Likewise for objects we aren't defining (presumed to be
1279 external references from other packages), but there we do
1280 not set up an initialization.
1282 If the object's size overflows, make an allocator too, so that
1283 Storage_Error gets raised. Note that we will never free
1284 such memory, so we presume it never will get allocated. */
1285 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1286 global_bindings_p ()
1287 || !definition
1288 || static_p)
1289 || (gnu_size
1290 && !allocatable_size_p (convert (sizetype,
1291 size_binop
1292 (CEIL_DIV_EXPR, gnu_size,
1293 bitsize_unit_node)),
1294 global_bindings_p ()
1295 || !definition
1296 || static_p)))
1298 gnu_type = build_reference_type (gnu_type);
1299 gnu_size = NULL_TREE;
1300 used_by_ref = true;
1302 /* In case this was a aliased object whose nominal subtype is
1303 unconstrained, the pointer above will be a thin pointer and
1304 build_allocator will automatically make the template.
1306 If we have a template initializer only (that we made above),
1307 pretend there is none and rely on what build_allocator creates
1308 again anyway. Otherwise (if we have a full initializer), get
1309 the data part and feed that to build_allocator.
1311 If we are elaborating a mutable object, tell build_allocator to
1312 ignore a possibly simpler size from the initializer, if any, as
1313 we must allocate the maximum possible size in this case. */
1314 if (definition && !imported_p)
1316 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1318 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1319 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1321 gnu_alloc_type
1322 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1324 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1325 && 1 == vec_safe_length (CONSTRUCTOR_ELTS (gnu_expr)))
1326 gnu_expr = 0;
1327 else
1328 gnu_expr
1329 = build_component_ref
1330 (gnu_expr, NULL_TREE,
1331 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1332 false);
1335 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1336 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1337 post_error ("?`Storage_Error` will be raised at run time!",
1338 gnat_entity);
1340 gnu_expr
1341 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1342 Empty, Empty, gnat_entity, mutable_p);
1343 const_flag = true;
1345 else
1347 gnu_expr = NULL_TREE;
1348 const_flag = false;
1352 /* If this object would go into the stack and has an alignment larger
1353 than the largest stack alignment the back-end can honor, resort to
1354 a variable of "aligning type". */
1355 if (!global_bindings_p () && !static_p && definition
1356 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1358 /* Create the new variable. No need for extra room before the
1359 aligned field as this is in automatic storage. */
1360 tree gnu_new_type
1361 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1362 TYPE_SIZE_UNIT (gnu_type),
1363 BIGGEST_ALIGNMENT, 0, gnat_entity);
1364 tree gnu_new_var
1365 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1366 NULL_TREE, gnu_new_type, NULL_TREE, false,
1367 false, false, false, NULL, gnat_entity);
1369 /* Initialize the aligned field if we have an initializer. */
1370 if (gnu_expr)
1371 add_stmt_with_node
1372 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1373 build_component_ref
1374 (gnu_new_var, NULL_TREE,
1375 TYPE_FIELDS (gnu_new_type), false),
1376 gnu_expr),
1377 gnat_entity);
1379 /* And setup this entity as a reference to the aligned field. */
1380 gnu_type = build_reference_type (gnu_type);
1381 gnu_expr
1382 = build_unary_op
1383 (ADDR_EXPR, gnu_type,
1384 build_component_ref (gnu_new_var, NULL_TREE,
1385 TYPE_FIELDS (gnu_new_type), false));
1387 gnu_size = NULL_TREE;
1388 used_by_ref = true;
1389 const_flag = true;
1392 /* If this is an aliased object with an unconstrained nominal subtype,
1393 we make its type a thin reference, i.e. the reference counterpart
1394 of a thin pointer, so that it points to the array part. This is
1395 aimed at making it easier for the debugger to decode the object.
1396 Note that we have to do that this late because of the couple of
1397 allocation adjustments that might be made just above. */
1398 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1399 && (Is_Array_Type (Etype (gnat_entity))
1400 || (Is_Private_Type (Etype (gnat_entity))
1401 && Is_Array_Type (Full_View (Etype (gnat_entity)))))
1402 && !type_annotate_only)
1404 tree gnu_array
1405 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
1407 /* In case the object with the template has already been allocated
1408 just above, we have nothing to do here. */
1409 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1411 tree gnu_unc_var
1412 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1413 NULL_TREE, gnu_type, gnu_expr,
1414 const_flag, Is_Public (gnat_entity),
1415 imported_p || !definition, static_p,
1416 NULL, gnat_entity);
1417 gnu_expr
1418 = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1419 TREE_CONSTANT (gnu_expr) = 1;
1421 gnu_size = NULL_TREE;
1422 used_by_ref = true;
1423 const_flag = true;
1426 gnu_type
1427 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1430 if (const_flag)
1431 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1432 | TYPE_QUAL_CONST));
1434 /* Convert the expression to the type of the object except in the
1435 case where the object's type is unconstrained or the object's type
1436 is a padded record whose field is of self-referential size. In
1437 the former case, converting will generate unnecessary evaluations
1438 of the CONSTRUCTOR to compute the size and in the latter case, we
1439 want to only copy the actual data. Also don't convert to a record
1440 type with a variant part from a record type without one, to keep
1441 the object simpler. */
1442 if (gnu_expr
1443 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1444 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1445 && !(TYPE_IS_PADDING_P (gnu_type)
1446 && CONTAINS_PLACEHOLDER_P
1447 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1448 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1449 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1450 && get_variant_part (gnu_type) != NULL_TREE
1451 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1452 gnu_expr = convert (gnu_type, gnu_expr);
1454 /* If this name is external or there was a name specified, use it,
1455 unless this is a VMS exception object since this would conflict
1456 with the symbol we need to export in addition. Don't use the
1457 Interface_Name if there is an address clause (see CD30005). */
1458 if (!Is_VMS_Exception (gnat_entity)
1459 && ((Present (Interface_Name (gnat_entity))
1460 && No (Address_Clause (gnat_entity)))
1461 || (Is_Public (gnat_entity)
1462 && (!Is_Imported (gnat_entity)
1463 || Is_Exported (gnat_entity)))))
1464 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1466 /* If this is an aggregate constant initialized to a constant, force it
1467 to be statically allocated. This saves an initialization copy. */
1468 if (!static_p
1469 && const_flag
1470 && gnu_expr && TREE_CONSTANT (gnu_expr)
1471 && AGGREGATE_TYPE_P (gnu_type)
1472 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1473 && !(TYPE_IS_PADDING_P (gnu_type)
1474 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1475 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1476 static_p = true;
1478 /* Deal with a pragma Linker_Section on a constant or variable. */
1479 if ((kind == E_Constant || kind == E_Variable)
1480 && Present (Linker_Section_Pragma (gnat_entity)))
1481 prepend_one_attribute_pragma (&attr_list,
1482 Linker_Section_Pragma (gnat_entity));
1484 /* Now create the variable or the constant and set various flags. */
1485 gnu_decl
1486 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1487 gnu_expr, const_flag, Is_Public (gnat_entity),
1488 imported_p || !definition, static_p, attr_list,
1489 gnat_entity);
1490 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1491 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1492 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1494 /* If we are defining an Out parameter and optimization isn't enabled,
1495 create a fake PARM_DECL for debugging purposes and make it point to
1496 the VAR_DECL. Suppress debug info for the latter but make sure it
1497 will live in memory so that it can be accessed from within the
1498 debugger through the PARM_DECL. */
1499 if (kind == E_Out_Parameter
1500 && definition
1501 && debug_info_p
1502 && !optimize
1503 && !flag_generate_lto)
1505 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1506 gnat_pushdecl (param, gnat_entity);
1507 SET_DECL_VALUE_EXPR (param, gnu_decl);
1508 DECL_HAS_VALUE_EXPR_P (param) = 1;
1509 DECL_IGNORED_P (gnu_decl) = 1;
1510 TREE_ADDRESSABLE (gnu_decl) = 1;
1513 /* If this is a loop parameter, set the corresponding flag. */
1514 else if (kind == E_Loop_Parameter)
1515 DECL_LOOP_PARM_P (gnu_decl) = 1;
1517 /* If this is a renaming pointer, attach the renamed object to it and
1518 register it if we are at the global level. Note that an external
1519 constant is at the global level. */
1520 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1522 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1523 if ((!definition && kind == E_Constant) || global_bindings_p ())
1525 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1526 record_global_renaming_pointer (gnu_decl);
1530 /* If this is a constant and we are defining it or it generates a real
1531 symbol at the object level and we are referencing it, we may want
1532 or need to have a true variable to represent it:
1533 - if optimization isn't enabled, for debugging purposes,
1534 - if the constant is public and not overlaid on something else,
1535 - if its address is taken,
1536 - if either itself or its type is aliased. */
1537 if (TREE_CODE (gnu_decl) == CONST_DECL
1538 && (definition || Sloc (gnat_entity) > Standard_Location)
1539 && ((!optimize && debug_info_p)
1540 || (Is_Public (gnat_entity)
1541 && No (Address_Clause (gnat_entity)))
1542 || Address_Taken (gnat_entity)
1543 || Is_Aliased (gnat_entity)
1544 || Is_Aliased (Etype (gnat_entity))))
1546 tree gnu_corr_var
1547 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1548 gnu_expr, true, Is_Public (gnat_entity),
1549 !definition, static_p, attr_list,
1550 gnat_entity);
1552 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1554 /* As debugging information will be generated for the variable,
1555 do not generate debugging information for the constant. */
1556 if (debug_info_p)
1557 DECL_IGNORED_P (gnu_decl) = 1;
1558 else
1559 DECL_IGNORED_P (gnu_corr_var) = 1;
1562 /* If this is a constant, even if we don't need a true variable, we
1563 may need to avoid returning the initializer in every case. That
1564 can happen for the address of a (constant) constructor because,
1565 upon dereferencing it, the constructor will be reinjected in the
1566 tree, which may not be valid in every case; see lvalue_required_p
1567 for more details. */
1568 if (TREE_CODE (gnu_decl) == CONST_DECL)
1569 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1571 /* If this object is declared in a block that contains a block with an
1572 exception handler, and we aren't using the GCC exception mechanism,
1573 we must force this variable in memory in order to avoid an invalid
1574 optimization. */
1575 if (Exception_Mechanism != Back_End_Exceptions
1576 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1577 TREE_ADDRESSABLE (gnu_decl) = 1;
1579 /* If this is a local variable with non-BLKmode and aggregate type,
1580 and optimization isn't enabled, then force it in memory so that
1581 a register won't be allocated to it with possible subparts left
1582 uninitialized and reaching the register allocator. */
1583 else if (TREE_CODE (gnu_decl) == VAR_DECL
1584 && !DECL_EXTERNAL (gnu_decl)
1585 && !TREE_STATIC (gnu_decl)
1586 && DECL_MODE (gnu_decl) != BLKmode
1587 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1588 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1589 && !optimize)
1590 TREE_ADDRESSABLE (gnu_decl) = 1;
1592 /* If we are defining an object with variable size or an object with
1593 fixed size that will be dynamically allocated, and we are using the
1594 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1595 if (definition
1596 && Exception_Mechanism == Setjmp_Longjmp
1597 && get_block_jmpbuf_decl ()
1598 && DECL_SIZE_UNIT (gnu_decl)
1599 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1600 || (flag_stack_check == GENERIC_STACK_CHECK
1601 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1602 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1603 add_stmt_with_node (build_call_n_expr
1604 (update_setjmp_buf_decl, 1,
1605 build_unary_op (ADDR_EXPR, NULL_TREE,
1606 get_block_jmpbuf_decl ())),
1607 gnat_entity);
1609 /* Back-annotate Esize and Alignment of the object if not already
1610 known. Note that we pick the values of the type, not those of
1611 the object, to shield ourselves from low-level platform-dependent
1612 adjustments like alignment promotion. This is both consistent with
1613 all the treatment above, where alignment and size are set on the
1614 type of the object and not on the object directly, and makes it
1615 possible to support all confirming representation clauses. */
1616 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1617 used_by_ref);
1619 break;
1621 case E_Void:
1622 /* Return a TYPE_DECL for "void" that we previously made. */
1623 gnu_decl = TYPE_NAME (void_type_node);
1624 break;
1626 case E_Enumeration_Type:
1627 /* A special case: for the types Character and Wide_Character in
1628 Standard, we do not list all the literals. So if the literals
1629 are not specified, make this an unsigned integer type. */
1630 if (No (First_Literal (gnat_entity)))
1632 gnu_type = make_unsigned_type (esize);
1633 TYPE_NAME (gnu_type) = gnu_entity_name;
1635 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1636 This is needed by the DWARF-2 back-end to distinguish between
1637 unsigned integer types and character types. */
1638 TYPE_STRING_FLAG (gnu_type) = 1;
1640 else
1642 /* We have a list of enumeral constants in First_Literal. We make a
1643 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1644 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1645 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1646 value of the literal. But when we have a regular boolean type, we
1647 simplify this a little by using a BOOLEAN_TYPE. */
1648 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1649 && !Has_Non_Standard_Rep (gnat_entity);
1650 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1651 tree gnu_list = NULL_TREE;
1652 Entity_Id gnat_literal;
1654 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1655 TYPE_PRECISION (gnu_type) = esize;
1656 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1657 set_min_and_max_values_for_integral_type (gnu_type, esize,
1658 is_unsigned);
1659 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1660 layout_type (gnu_type);
1662 for (gnat_literal = First_Literal (gnat_entity);
1663 Present (gnat_literal);
1664 gnat_literal = Next_Literal (gnat_literal))
1666 tree gnu_value
1667 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1668 tree gnu_literal
1669 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1670 gnu_type, gnu_value, true, false, false,
1671 false, NULL, gnat_literal);
1672 /* Do not generate debug info for individual enumerators. */
1673 DECL_IGNORED_P (gnu_literal) = 1;
1674 save_gnu_tree (gnat_literal, gnu_literal, false);
1675 gnu_list
1676 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1679 if (!is_boolean)
1680 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1682 /* Note that the bounds are updated at the end of this function
1683 to avoid an infinite recursion since they refer to the type. */
1684 goto discrete_type;
1686 break;
1688 case E_Signed_Integer_Type:
1689 case E_Ordinary_Fixed_Point_Type:
1690 case E_Decimal_Fixed_Point_Type:
1691 /* For integer types, just make a signed type the appropriate number
1692 of bits. */
1693 gnu_type = make_signed_type (esize);
1694 goto discrete_type;
1696 case E_Modular_Integer_Type:
1698 /* For modular types, make the unsigned type of the proper number
1699 of bits and then set up the modulus, if required. */
1700 tree gnu_modulus, gnu_high = NULL_TREE;
1702 /* Packed array types are supposed to be subtypes only. */
1703 gcc_assert (!Is_Packed_Array_Type (gnat_entity));
1705 gnu_type = make_unsigned_type (esize);
1707 /* Get the modulus in this type. If it overflows, assume it is because
1708 it is equal to 2**Esize. Note that there is no overflow checking
1709 done on unsigned type, so we detect the overflow by looking for
1710 a modulus of zero, which is otherwise invalid. */
1711 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1713 if (!integer_zerop (gnu_modulus))
1715 TYPE_MODULAR_P (gnu_type) = 1;
1716 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1717 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1718 convert (gnu_type, integer_one_node));
1721 /* If the upper bound is not maximal, make an extra subtype. */
1722 if (gnu_high
1723 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1725 tree gnu_subtype = make_unsigned_type (esize);
1726 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1727 TREE_TYPE (gnu_subtype) = gnu_type;
1728 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1729 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1730 gnu_type = gnu_subtype;
1733 goto discrete_type;
1735 case E_Signed_Integer_Subtype:
1736 case E_Enumeration_Subtype:
1737 case E_Modular_Integer_Subtype:
1738 case E_Ordinary_Fixed_Point_Subtype:
1739 case E_Decimal_Fixed_Point_Subtype:
1741 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1742 not want to call create_range_type since we would like each subtype
1743 node to be distinct. ??? Historically this was in preparation for
1744 when memory aliasing is implemented, but that's obsolete now given
1745 the call to relate_alias_sets below.
1747 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1748 this fact is used by the arithmetic conversion functions.
1750 We elaborate the Ancestor_Subtype if it is not in the current unit
1751 and one of our bounds is non-static. We do this to ensure consistent
1752 naming in the case where several subtypes share the same bounds, by
1753 elaborating the first such subtype first, thus using its name. */
1755 if (!definition
1756 && Present (Ancestor_Subtype (gnat_entity))
1757 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1758 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1759 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1760 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1762 /* Set the precision to the Esize except for bit-packed arrays. */
1763 if (Is_Packed_Array_Type (gnat_entity)
1764 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1765 esize = UI_To_Int (RM_Size (gnat_entity));
1767 /* This should be an unsigned type if the base type is unsigned or
1768 if the lower bound is constant and non-negative or if the type
1769 is biased. */
1770 if (Is_Unsigned_Type (Etype (gnat_entity))
1771 || Is_Unsigned_Type (gnat_entity)
1772 || Has_Biased_Representation (gnat_entity))
1773 gnu_type = make_unsigned_type (esize);
1774 else
1775 gnu_type = make_signed_type (esize);
1776 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1778 SET_TYPE_RM_MIN_VALUE
1779 (gnu_type,
1780 convert (TREE_TYPE (gnu_type),
1781 elaborate_expression (Type_Low_Bound (gnat_entity),
1782 gnat_entity, get_identifier ("L"),
1783 definition, true,
1784 Needs_Debug_Info (gnat_entity))));
1786 SET_TYPE_RM_MAX_VALUE
1787 (gnu_type,
1788 convert (TREE_TYPE (gnu_type),
1789 elaborate_expression (Type_High_Bound (gnat_entity),
1790 gnat_entity, get_identifier ("U"),
1791 definition, true,
1792 Needs_Debug_Info (gnat_entity))));
1794 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1795 = Has_Biased_Representation (gnat_entity);
1797 /* Inherit our alias set from what we're a subtype of. Subtypes
1798 are not different types and a pointer can designate any instance
1799 within a subtype hierarchy. */
1800 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1802 /* One of the above calls might have caused us to be elaborated,
1803 so don't blow up if so. */
1804 if (present_gnu_tree (gnat_entity))
1806 maybe_present = true;
1807 break;
1810 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1811 TYPE_STUB_DECL (gnu_type)
1812 = create_type_stub_decl (gnu_entity_name, gnu_type);
1814 /* For a packed array, make the original array type a parallel type. */
1815 if (debug_info_p
1816 && Is_Packed_Array_Type (gnat_entity)
1817 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1818 add_parallel_type (gnu_type,
1819 gnat_to_gnu_type
1820 (Original_Array_Type (gnat_entity)));
1822 discrete_type:
1824 /* We have to handle clauses that under-align the type specially. */
1825 if ((Present (Alignment_Clause (gnat_entity))
1826 || (Is_Packed_Array_Type (gnat_entity)
1827 && Present
1828 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1829 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1831 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1832 if (align >= TYPE_ALIGN (gnu_type))
1833 align = 0;
1836 /* If the type we are dealing with represents a bit-packed array,
1837 we need to have the bits left justified on big-endian targets
1838 and right justified on little-endian targets. We also need to
1839 ensure that when the value is read (e.g. for comparison of two
1840 such values), we only get the good bits, since the unused bits
1841 are uninitialized. Both goals are accomplished by wrapping up
1842 the modular type in an enclosing record type. */
1843 if (Is_Packed_Array_Type (gnat_entity)
1844 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1846 tree gnu_field_type, gnu_field;
1848 /* Set the RM size before wrapping up the original type. */
1849 SET_TYPE_RM_SIZE (gnu_type,
1850 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1851 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1853 /* Create a stripped-down declaration, mainly for debugging. */
1854 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1855 gnat_entity);
1857 /* Now save it and build the enclosing record type. */
1858 gnu_field_type = gnu_type;
1860 gnu_type = make_node (RECORD_TYPE);
1861 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1862 TYPE_PACKED (gnu_type) = 1;
1863 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1864 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1865 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1867 /* Propagate the alignment of the modular type to the record type,
1868 unless there is an alignment clause that under-aligns the type.
1869 This means that bit-packed arrays are given "ceil" alignment for
1870 their size by default, which may seem counter-intuitive but makes
1871 it possible to overlay them on modular types easily. */
1872 TYPE_ALIGN (gnu_type)
1873 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1875 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1877 /* Don't declare the field as addressable since we won't be taking
1878 its address and this would prevent create_field_decl from making
1879 a bitfield. */
1880 gnu_field
1881 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1882 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1884 /* Do not emit debug info until after the parallel type is added. */
1885 finish_record_type (gnu_type, gnu_field, 2, false);
1886 compute_record_mode (gnu_type);
1887 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1889 if (debug_info_p)
1891 /* Make the original array type a parallel type. */
1892 if (present_gnu_tree (Original_Array_Type (gnat_entity)))
1893 add_parallel_type (gnu_type,
1894 gnat_to_gnu_type
1895 (Original_Array_Type (gnat_entity)));
1897 rest_of_record_type_compilation (gnu_type);
1901 /* If the type we are dealing with has got a smaller alignment than the
1902 natural one, we need to wrap it up in a record type and misalign the
1903 latter; we reuse the padding machinery for this purpose. Note that,
1904 even if the record type is marked as packed because of misalignment,
1905 we don't pack the field so as to give it the size of the type. */
1906 else if (align > 0)
1908 tree gnu_field_type, gnu_field;
1910 /* Set the RM size before wrapping up the type. */
1911 SET_TYPE_RM_SIZE (gnu_type,
1912 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1914 /* Create a stripped-down declaration, mainly for debugging. */
1915 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1916 gnat_entity);
1918 /* Now save it and build the enclosing record type. */
1919 gnu_field_type = gnu_type;
1921 gnu_type = make_node (RECORD_TYPE);
1922 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1923 TYPE_PACKED (gnu_type) = 1;
1924 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1925 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1926 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1927 TYPE_ALIGN (gnu_type) = align;
1928 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1930 /* Don't declare the field as addressable since we won't be taking
1931 its address and this would prevent create_field_decl from making
1932 a bitfield. */
1933 gnu_field
1934 = create_field_decl (get_identifier ("F"), gnu_field_type,
1935 gnu_type, TYPE_SIZE (gnu_field_type),
1936 bitsize_zero_node, 0, 0);
1938 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1939 compute_record_mode (gnu_type);
1940 TYPE_PADDING_P (gnu_type) = 1;
1943 break;
1945 case E_Floating_Point_Type:
1946 /* If this is a VAX floating-point type, use an integer of the proper
1947 size. All the operations will be handled with ASM statements. */
1948 if (Vax_Float (gnat_entity))
1950 gnu_type = make_signed_type (esize);
1951 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1952 SET_TYPE_DIGITS_VALUE (gnu_type,
1953 UI_To_gnu (Digits_Value (gnat_entity),
1954 sizetype));
1955 break;
1958 /* The type of the Low and High bounds can be our type if this is
1959 a type from Standard, so set them at the end of the function. */
1960 gnu_type = make_node (REAL_TYPE);
1961 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1962 layout_type (gnu_type);
1963 break;
1965 case E_Floating_Point_Subtype:
1966 if (Vax_Float (gnat_entity))
1968 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1969 break;
1972 /* See the E_Signed_Integer_Subtype case for the rationale. */
1973 if (!definition
1974 && Present (Ancestor_Subtype (gnat_entity))
1975 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1976 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1977 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1978 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1980 gnu_type = make_node (REAL_TYPE);
1981 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1982 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1983 TYPE_GCC_MIN_VALUE (gnu_type)
1984 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1985 TYPE_GCC_MAX_VALUE (gnu_type)
1986 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1987 layout_type (gnu_type);
1989 SET_TYPE_RM_MIN_VALUE
1990 (gnu_type,
1991 convert (TREE_TYPE (gnu_type),
1992 elaborate_expression (Type_Low_Bound (gnat_entity),
1993 gnat_entity, get_identifier ("L"),
1994 definition, true,
1995 Needs_Debug_Info (gnat_entity))));
1997 SET_TYPE_RM_MAX_VALUE
1998 (gnu_type,
1999 convert (TREE_TYPE (gnu_type),
2000 elaborate_expression (Type_High_Bound (gnat_entity),
2001 gnat_entity, get_identifier ("U"),
2002 definition, true,
2003 Needs_Debug_Info (gnat_entity))));
2005 /* Inherit our alias set from what we're a subtype of, as for
2006 integer subtypes. */
2007 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
2009 /* One of the above calls might have caused us to be elaborated,
2010 so don't blow up if so. */
2011 maybe_present = true;
2012 break;
2014 /* Array and String Types and Subtypes
2016 Unconstrained array types are represented by E_Array_Type and
2017 constrained array types are represented by E_Array_Subtype. There
2018 are no actual objects of an unconstrained array type; all we have
2019 are pointers to that type.
2021 The following fields are defined on array types and subtypes:
2023 Component_Type Component type of the array.
2024 Number_Dimensions Number of dimensions (an int).
2025 First_Index Type of first index. */
2027 case E_String_Type:
2028 case E_Array_Type:
2030 const bool convention_fortran_p
2031 = (Convention (gnat_entity) == Convention_Fortran);
2032 const int ndim = Number_Dimensions (gnat_entity);
2033 tree gnu_template_type;
2034 tree gnu_ptr_template;
2035 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
2036 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2037 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
2038 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
2039 Entity_Id gnat_index, gnat_name;
2040 int index;
2041 tree comp_type;
2043 /* Create the type for the component now, as it simplifies breaking
2044 type reference loops. */
2045 comp_type
2046 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2047 if (present_gnu_tree (gnat_entity))
2049 /* As a side effect, the type may have been translated. */
2050 maybe_present = true;
2051 break;
2054 /* We complete an existing dummy fat pointer type in place. This both
2055 avoids further complex adjustments in update_pointer_to and yields
2056 better debugging information in DWARF by leveraging the support for
2057 incomplete declarations of "tagged" types in the DWARF back-end. */
2058 gnu_type = get_dummy_type (gnat_entity);
2059 if (gnu_type && TYPE_POINTER_TO (gnu_type))
2061 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2062 TYPE_NAME (gnu_fat_type) = NULL_TREE;
2063 /* Save the contents of the dummy type for update_pointer_to. */
2064 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2065 gnu_ptr_template =
2066 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2067 gnu_template_type = TREE_TYPE (gnu_ptr_template);
2069 else
2071 gnu_fat_type = make_node (RECORD_TYPE);
2072 gnu_template_type = make_node (RECORD_TYPE);
2073 gnu_ptr_template = build_pointer_type (gnu_template_type);
2076 /* Make a node for the array. If we are not defining the array
2077 suppress expanding incomplete types. */
2078 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2080 if (!definition)
2082 defer_incomplete_level++;
2083 this_deferred = true;
2086 /* Build the fat pointer type. Use a "void *" object instead of
2087 a pointer to the array type since we don't have the array type
2088 yet (it will reference the fat pointer via the bounds). */
2090 = create_field_decl (get_identifier ("P_ARRAY"), ptr_void_type_node,
2091 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2092 DECL_CHAIN (tem)
2093 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2094 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2096 if (COMPLETE_TYPE_P (gnu_fat_type))
2098 /* We are going to lay it out again so reset the alias set. */
2099 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2100 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2101 finish_fat_pointer_type (gnu_fat_type, tem);
2102 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2103 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2105 TYPE_FIELDS (t) = tem;
2106 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2109 else
2111 finish_fat_pointer_type (gnu_fat_type, tem);
2112 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2115 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2116 is the fat pointer. This will be used to access the individual
2117 fields once we build them. */
2118 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2119 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2120 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2121 gnu_template_reference
2122 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2123 TREE_READONLY (gnu_template_reference) = 1;
2124 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2126 /* Now create the GCC type for each index and add the fields for that
2127 index to the template. */
2128 for (index = (convention_fortran_p ? ndim - 1 : 0),
2129 gnat_index = First_Index (gnat_entity);
2130 0 <= index && index < ndim;
2131 index += (convention_fortran_p ? - 1 : 1),
2132 gnat_index = Next_Index (gnat_index))
2134 char field_name[16];
2135 tree gnu_index_base_type
2136 = get_unpadded_type (Base_Type (Etype (gnat_index)));
2137 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2138 tree gnu_min, gnu_max, gnu_high;
2140 /* Make the FIELD_DECLs for the low and high bounds of this
2141 type and then make extractions of these fields from the
2142 template. */
2143 sprintf (field_name, "LB%d", index);
2144 gnu_lb_field = create_field_decl (get_identifier (field_name),
2145 gnu_index_base_type,
2146 gnu_template_type, NULL_TREE,
2147 NULL_TREE, 0, 0);
2148 Sloc_to_locus (Sloc (gnat_entity),
2149 &DECL_SOURCE_LOCATION (gnu_lb_field));
2151 field_name[0] = 'U';
2152 gnu_hb_field = create_field_decl (get_identifier (field_name),
2153 gnu_index_base_type,
2154 gnu_template_type, NULL_TREE,
2155 NULL_TREE, 0, 0);
2156 Sloc_to_locus (Sloc (gnat_entity),
2157 &DECL_SOURCE_LOCATION (gnu_hb_field));
2159 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2161 /* We can't use build_component_ref here since the template type
2162 isn't complete yet. */
2163 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2164 gnu_template_reference, gnu_lb_field,
2165 NULL_TREE);
2166 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2167 gnu_template_reference, gnu_hb_field,
2168 NULL_TREE);
2169 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2171 gnu_min = convert (sizetype, gnu_orig_min);
2172 gnu_max = convert (sizetype, gnu_orig_max);
2174 /* Compute the size of this dimension. See the E_Array_Subtype
2175 case below for the rationale. */
2176 gnu_high
2177 = build3 (COND_EXPR, sizetype,
2178 build2 (GE_EXPR, boolean_type_node,
2179 gnu_orig_max, gnu_orig_min),
2180 gnu_max,
2181 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2183 /* Make a range type with the new range in the Ada base type.
2184 Then make an index type with the size range in sizetype. */
2185 gnu_index_types[index]
2186 = create_index_type (gnu_min, gnu_high,
2187 create_range_type (gnu_index_base_type,
2188 gnu_orig_min,
2189 gnu_orig_max),
2190 gnat_entity);
2192 /* Update the maximum size of the array in elements. */
2193 if (gnu_max_size)
2195 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2196 tree gnu_min
2197 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2198 tree gnu_max
2199 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2200 tree gnu_this_max
2201 = size_binop (MAX_EXPR,
2202 size_binop (PLUS_EXPR, size_one_node,
2203 size_binop (MINUS_EXPR,
2204 gnu_max, gnu_min)),
2205 size_zero_node);
2207 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2208 && TREE_OVERFLOW (gnu_this_max))
2209 gnu_max_size = NULL_TREE;
2210 else
2211 gnu_max_size
2212 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2215 TYPE_NAME (gnu_index_types[index])
2216 = create_concat_name (gnat_entity, field_name);
2219 /* Install all the fields into the template. */
2220 TYPE_NAME (gnu_template_type)
2221 = create_concat_name (gnat_entity, "XUB");
2222 gnu_template_fields = NULL_TREE;
2223 for (index = 0; index < ndim; index++)
2224 gnu_template_fields
2225 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2226 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2227 debug_info_p);
2228 TYPE_READONLY (gnu_template_type) = 1;
2230 /* If Component_Size is not already specified, annotate it with the
2231 size of the component. */
2232 if (Unknown_Component_Size (gnat_entity))
2233 Set_Component_Size (gnat_entity,
2234 annotate_value (TYPE_SIZE (comp_type)));
2236 /* Compute the maximum size of the array in units and bits. */
2237 if (gnu_max_size)
2239 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2240 TYPE_SIZE_UNIT (comp_type));
2241 gnu_max_size = size_binop (MULT_EXPR,
2242 convert (bitsizetype, gnu_max_size),
2243 TYPE_SIZE (comp_type));
2245 else
2246 gnu_max_size_unit = NULL_TREE;
2248 /* Now build the array type. */
2249 tem = comp_type;
2250 for (index = ndim - 1; index >= 0; index--)
2252 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2253 if (Reverse_Storage_Order (gnat_entity))
2254 sorry ("non-default Scalar_Storage_Order");
2255 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2256 if (array_type_has_nonaliased_component (tem, gnat_entity))
2257 TYPE_NONALIASED_COMPONENT (tem) = 1;
2259 /* If it is passed by reference, force BLKmode to ensure that
2260 objects of this type will always be put in memory. */
2261 if (TYPE_MODE (tem) != BLKmode
2262 && Is_By_Reference_Type (gnat_entity))
2263 SET_TYPE_MODE (tem, BLKmode);
2266 /* If an alignment is specified, use it if valid. But ignore it
2267 for the original type of packed array types. If the alignment
2268 was requested with an explicit alignment clause, state so. */
2269 if (No (Packed_Array_Type (gnat_entity))
2270 && Known_Alignment (gnat_entity))
2272 TYPE_ALIGN (tem)
2273 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2274 TYPE_ALIGN (tem));
2275 if (Present (Alignment_Clause (gnat_entity)))
2276 TYPE_USER_ALIGN (tem) = 1;
2279 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2281 /* Adjust the type of the pointer-to-array field of the fat pointer
2282 and record the aliasing relationships if necessary. */
2283 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2284 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2285 record_component_aliases (gnu_fat_type);
2287 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2288 corresponding fat pointer. */
2289 TREE_TYPE (gnu_type) = gnu_fat_type;
2290 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2291 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2292 SET_TYPE_MODE (gnu_type, BLKmode);
2293 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2295 /* If the maximum size doesn't overflow, use it. */
2296 if (gnu_max_size
2297 && TREE_CODE (gnu_max_size) == INTEGER_CST
2298 && !TREE_OVERFLOW (gnu_max_size)
2299 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2300 && !TREE_OVERFLOW (gnu_max_size_unit))
2302 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2303 TYPE_SIZE (tem));
2304 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2305 TYPE_SIZE_UNIT (tem));
2308 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2309 !Comes_From_Source (gnat_entity), debug_info_p,
2310 gnat_entity);
2312 /* Give the fat pointer type a name. If this is a packed type, tell
2313 the debugger how to interpret the underlying bits. */
2314 if (Present (Packed_Array_Type (gnat_entity)))
2315 gnat_name = Packed_Array_Type (gnat_entity);
2316 else
2317 gnat_name = gnat_entity;
2318 create_type_decl (create_concat_name (gnat_name, "XUP"), gnu_fat_type,
2319 !Comes_From_Source (gnat_entity), debug_info_p,
2320 gnat_entity);
2322 /* Create the type to be designated by thin pointers: a record type for
2323 the array and its template. We used to shift the fields to have the
2324 template at a negative offset, but this was somewhat of a kludge; we
2325 now shift thin pointer values explicitly but only those which have a
2326 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE. */
2327 tem = build_unc_object_type (gnu_template_type, tem,
2328 create_concat_name (gnat_name, "XUT"),
2329 debug_info_p);
2331 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2332 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2334 break;
2336 case E_String_Subtype:
2337 case E_Array_Subtype:
2339 /* This is the actual data type for array variables. Multidimensional
2340 arrays are implemented as arrays of arrays. Note that arrays which
2341 have sparse enumeration subtypes as index components create sparse
2342 arrays, which is obviously space inefficient but so much easier to
2343 code for now.
2345 Also note that the subtype never refers to the unconstrained array
2346 type, which is somewhat at variance with Ada semantics.
2348 First check to see if this is simply a renaming of the array type.
2349 If so, the result is the array type. */
2351 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2352 if (!Is_Constrained (gnat_entity))
2354 else
2356 Entity_Id gnat_index, gnat_base_index;
2357 const bool convention_fortran_p
2358 = (Convention (gnat_entity) == Convention_Fortran);
2359 const int ndim = Number_Dimensions (gnat_entity);
2360 tree gnu_base_type = gnu_type;
2361 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2362 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2363 bool need_index_type_struct = false;
2364 int index;
2366 /* First create the GCC type for each index and find out whether
2367 special types are needed for debugging information. */
2368 for (index = (convention_fortran_p ? ndim - 1 : 0),
2369 gnat_index = First_Index (gnat_entity),
2370 gnat_base_index
2371 = First_Index (Implementation_Base_Type (gnat_entity));
2372 0 <= index && index < ndim;
2373 index += (convention_fortran_p ? - 1 : 1),
2374 gnat_index = Next_Index (gnat_index),
2375 gnat_base_index = Next_Index (gnat_base_index))
2377 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2378 tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type);
2379 tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type);
2380 tree gnu_min = convert (sizetype, gnu_orig_min);
2381 tree gnu_max = convert (sizetype, gnu_orig_max);
2382 tree gnu_base_index_type
2383 = get_unpadded_type (Etype (gnat_base_index));
2384 tree gnu_base_orig_min = TYPE_MIN_VALUE (gnu_base_index_type);
2385 tree gnu_base_orig_max = TYPE_MAX_VALUE (gnu_base_index_type);
2386 tree gnu_high;
2388 /* See if the base array type is already flat. If it is, we
2389 are probably compiling an ACATS test but it will cause the
2390 code below to malfunction if we don't handle it specially. */
2391 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2392 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2393 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2395 gnu_min = size_one_node;
2396 gnu_max = size_zero_node;
2397 gnu_high = gnu_max;
2400 /* Similarly, if one of the values overflows in sizetype and the
2401 range is null, use 1..0 for the sizetype bounds. */
2402 else if (TREE_CODE (gnu_min) == INTEGER_CST
2403 && TREE_CODE (gnu_max) == INTEGER_CST
2404 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2405 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2407 gnu_min = size_one_node;
2408 gnu_max = size_zero_node;
2409 gnu_high = gnu_max;
2412 /* If the minimum and maximum values both overflow in sizetype,
2413 but the difference in the original type does not overflow in
2414 sizetype, ignore the overflow indication. */
2415 else if (TREE_CODE (gnu_min) == INTEGER_CST
2416 && TREE_CODE (gnu_max) == INTEGER_CST
2417 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2418 && !TREE_OVERFLOW
2419 (convert (sizetype,
2420 fold_build2 (MINUS_EXPR, gnu_index_type,
2421 gnu_orig_max,
2422 gnu_orig_min))))
2424 TREE_OVERFLOW (gnu_min) = 0;
2425 TREE_OVERFLOW (gnu_max) = 0;
2426 gnu_high = gnu_max;
2429 /* Compute the size of this dimension in the general case. We
2430 need to provide GCC with an upper bound to use but have to
2431 deal with the "superflat" case. There are three ways to do
2432 this. If we can prove that the array can never be superflat,
2433 we can just use the high bound of the index type. */
2434 else if ((Nkind (gnat_index) == N_Range
2435 && cannot_be_superflat_p (gnat_index))
2436 /* Packed Array Types are never superflat. */
2437 || Is_Packed_Array_Type (gnat_entity))
2438 gnu_high = gnu_max;
2440 /* Otherwise, if the high bound is constant but the low bound is
2441 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2442 lower bound. Note that the comparison must be done in the
2443 original type to avoid any overflow during the conversion. */
2444 else if (TREE_CODE (gnu_max) == INTEGER_CST
2445 && TREE_CODE (gnu_min) != INTEGER_CST)
2447 gnu_high = gnu_max;
2448 gnu_min
2449 = build_cond_expr (sizetype,
2450 build_binary_op (GE_EXPR,
2451 boolean_type_node,
2452 gnu_orig_max,
2453 gnu_orig_min),
2454 gnu_min,
2455 int_const_binop (PLUS_EXPR, gnu_max,
2456 size_one_node));
2459 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2460 in all the other cases. Note that, here as well as above,
2461 the condition used in the comparison must be equivalent to
2462 the condition (length != 0). This is relied upon in order
2463 to optimize array comparisons in compare_arrays. Moreover
2464 we use int_const_binop for the shift by 1 if the bound is
2465 constant to avoid any unwanted overflow. */
2466 else
2467 gnu_high
2468 = build_cond_expr (sizetype,
2469 build_binary_op (GE_EXPR,
2470 boolean_type_node,
2471 gnu_orig_max,
2472 gnu_orig_min),
2473 gnu_max,
2474 TREE_CODE (gnu_min) == INTEGER_CST
2475 ? int_const_binop (MINUS_EXPR, gnu_min,
2476 size_one_node)
2477 : size_binop (MINUS_EXPR, gnu_min,
2478 size_one_node));
2480 /* Reuse the index type for the range type. Then make an index
2481 type with the size range in sizetype. */
2482 gnu_index_types[index]
2483 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2484 gnat_entity);
2486 /* Update the maximum size of the array in elements. Here we
2487 see if any constraint on the index type of the base type
2488 can be used in the case of self-referential bound on the
2489 index type of the subtype. We look for a non-"infinite"
2490 and non-self-referential bound from any type involved and
2491 handle each bound separately. */
2492 if (gnu_max_size)
2494 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2495 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2496 tree gnu_base_index_base_type
2497 = get_base_type (gnu_base_index_type);
2498 tree gnu_base_base_min
2499 = convert (sizetype,
2500 TYPE_MIN_VALUE (gnu_base_index_base_type));
2501 tree gnu_base_base_max
2502 = convert (sizetype,
2503 TYPE_MAX_VALUE (gnu_base_index_base_type));
2505 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2506 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2507 && !TREE_OVERFLOW (gnu_base_min)))
2508 gnu_base_min = gnu_min;
2510 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2511 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2512 && !TREE_OVERFLOW (gnu_base_max)))
2513 gnu_base_max = gnu_max;
2515 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2516 && TREE_OVERFLOW (gnu_base_min))
2517 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2518 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2519 && TREE_OVERFLOW (gnu_base_max))
2520 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2521 gnu_max_size = NULL_TREE;
2522 else
2524 tree gnu_this_max
2525 = size_binop (MAX_EXPR,
2526 size_binop (PLUS_EXPR, size_one_node,
2527 size_binop (MINUS_EXPR,
2528 gnu_base_max,
2529 gnu_base_min)),
2530 size_zero_node);
2532 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2533 && TREE_OVERFLOW (gnu_this_max))
2534 gnu_max_size = NULL_TREE;
2535 else
2536 gnu_max_size
2537 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2541 /* We need special types for debugging information to point to
2542 the index types if they have variable bounds, are not integer
2543 types, are biased or are wider than sizetype. */
2544 if (!integer_onep (gnu_orig_min)
2545 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2546 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2547 || (TREE_TYPE (gnu_index_type)
2548 && TREE_CODE (TREE_TYPE (gnu_index_type))
2549 != INTEGER_TYPE)
2550 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type)
2551 || compare_tree_int (rm_size (gnu_index_type),
2552 TYPE_PRECISION (sizetype)) > 0)
2553 need_index_type_struct = true;
2556 /* Then flatten: create the array of arrays. For an array type
2557 used to implement a packed array, get the component type from
2558 the original array type since the representation clauses that
2559 can affect it are on the latter. */
2560 if (Is_Packed_Array_Type (gnat_entity)
2561 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2563 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2564 for (index = ndim - 1; index >= 0; index--)
2565 gnu_type = TREE_TYPE (gnu_type);
2567 /* One of the above calls might have caused us to be elaborated,
2568 so don't blow up if so. */
2569 if (present_gnu_tree (gnat_entity))
2571 maybe_present = true;
2572 break;
2575 else
2577 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2578 debug_info_p);
2580 /* One of the above calls might have caused us to be elaborated,
2581 so don't blow up if so. */
2582 if (present_gnu_tree (gnat_entity))
2584 maybe_present = true;
2585 break;
2589 /* Compute the maximum size of the array in units and bits. */
2590 if (gnu_max_size)
2592 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2593 TYPE_SIZE_UNIT (gnu_type));
2594 gnu_max_size = size_binop (MULT_EXPR,
2595 convert (bitsizetype, gnu_max_size),
2596 TYPE_SIZE (gnu_type));
2598 else
2599 gnu_max_size_unit = NULL_TREE;
2601 /* Now build the array type. */
2602 for (index = ndim - 1; index >= 0; index --)
2604 gnu_type = build_nonshared_array_type (gnu_type,
2605 gnu_index_types[index]);
2606 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2607 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2608 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2610 /* See the E_Array_Type case for the rationale. */
2611 if (TYPE_MODE (gnu_type) != BLKmode
2612 && Is_By_Reference_Type (gnat_entity))
2613 SET_TYPE_MODE (gnu_type, BLKmode);
2616 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2617 TYPE_STUB_DECL (gnu_type)
2618 = create_type_stub_decl (gnu_entity_name, gnu_type);
2620 /* If we are at file level and this is a multi-dimensional array,
2621 we need to make a variable corresponding to the stride of the
2622 inner dimensions. */
2623 if (global_bindings_p () && ndim > 1)
2625 tree gnu_st_name = get_identifier ("ST");
2626 tree gnu_arr_type;
2628 for (gnu_arr_type = TREE_TYPE (gnu_type);
2629 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2630 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2631 gnu_st_name = concat_name (gnu_st_name, "ST"))
2633 tree eltype = TREE_TYPE (gnu_arr_type);
2635 TYPE_SIZE (gnu_arr_type)
2636 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2637 gnat_entity, gnu_st_name,
2638 definition, false);
2640 /* ??? For now, store the size as a multiple of the
2641 alignment of the element type in bytes so that we
2642 can see the alignment from the tree. */
2643 TYPE_SIZE_UNIT (gnu_arr_type)
2644 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2645 gnat_entity,
2646 concat_name (gnu_st_name, "A_U"),
2647 definition, false,
2648 TYPE_ALIGN (eltype));
2650 /* ??? create_type_decl is not invoked on the inner types so
2651 the MULT_EXPR node built above will never be marked. */
2652 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2656 /* If we need to write out a record type giving the names of the
2657 bounds for debugging purposes, do it now and make the record
2658 type a parallel type. This is not needed for a packed array
2659 since the bounds are conveyed by the original array type. */
2660 if (need_index_type_struct
2661 && debug_info_p
2662 && !Is_Packed_Array_Type (gnat_entity))
2664 tree gnu_bound_rec = make_node (RECORD_TYPE);
2665 tree gnu_field_list = NULL_TREE;
2666 tree gnu_field;
2668 TYPE_NAME (gnu_bound_rec)
2669 = create_concat_name (gnat_entity, "XA");
2671 for (index = ndim - 1; index >= 0; index--)
2673 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2674 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2676 /* Make sure to reference the types themselves, and not just
2677 their names, as the debugger may fall back on them. */
2678 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2679 gnu_bound_rec, NULL_TREE,
2680 NULL_TREE, 0, 0);
2681 DECL_CHAIN (gnu_field) = gnu_field_list;
2682 gnu_field_list = gnu_field;
2685 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2686 add_parallel_type (gnu_type, gnu_bound_rec);
2689 /* If this is a packed array type, make the original array type a
2690 parallel type. Otherwise, do it for the base array type if it
2691 isn't artificial to make sure it is kept in the debug info. */
2692 if (debug_info_p)
2694 if (Is_Packed_Array_Type (gnat_entity)
2695 && present_gnu_tree (Original_Array_Type (gnat_entity)))
2696 add_parallel_type (gnu_type,
2697 gnat_to_gnu_type
2698 (Original_Array_Type (gnat_entity)));
2699 else
2701 tree gnu_base_decl
2702 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2703 if (!DECL_ARTIFICIAL (gnu_base_decl))
2704 add_parallel_type (gnu_type,
2705 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2709 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2710 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2711 = (Is_Packed_Array_Type (gnat_entity)
2712 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2714 /* If the size is self-referential and the maximum size doesn't
2715 overflow, use it. */
2716 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2717 && gnu_max_size
2718 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2719 && TREE_OVERFLOW (gnu_max_size))
2720 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2721 && TREE_OVERFLOW (gnu_max_size_unit)))
2723 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2724 TYPE_SIZE (gnu_type));
2725 TYPE_SIZE_UNIT (gnu_type)
2726 = size_binop (MIN_EXPR, gnu_max_size_unit,
2727 TYPE_SIZE_UNIT (gnu_type));
2730 /* Set our alias set to that of our base type. This gives all
2731 array subtypes the same alias set. */
2732 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2734 /* If this is a packed type, make this type the same as the packed
2735 array type, but do some adjusting in the type first. */
2736 if (Present (Packed_Array_Type (gnat_entity)))
2738 Entity_Id gnat_index;
2739 tree gnu_inner;
2741 /* First finish the type we had been making so that we output
2742 debugging information for it. */
2743 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2744 if (Treat_As_Volatile (gnat_entity))
2745 gnu_type
2746 = build_qualified_type (gnu_type,
2747 TYPE_QUALS (gnu_type)
2748 | TYPE_QUAL_VOLATILE);
2749 /* Make it artificial only if the base type was artificial too.
2750 That's sort of "morally" true and will make it possible for
2751 the debugger to look it up by name in DWARF, which is needed
2752 in order to decode the packed array type. */
2753 gnu_decl
2754 = create_type_decl (gnu_entity_name, gnu_type,
2755 !Comes_From_Source (Etype (gnat_entity))
2756 && !Comes_From_Source (gnat_entity),
2757 debug_info_p, gnat_entity);
2759 /* Save it as our equivalent in case the call below elaborates
2760 this type again. */
2761 save_gnu_tree (gnat_entity, gnu_decl, false);
2763 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2764 NULL_TREE, 0);
2765 this_made_decl = true;
2766 gnu_type = TREE_TYPE (gnu_decl);
2767 save_gnu_tree (gnat_entity, NULL_TREE, false);
2769 gnu_inner = gnu_type;
2770 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2771 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2772 || TYPE_PADDING_P (gnu_inner)))
2773 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2775 /* We need to attach the index type to the type we just made so
2776 that the actual bounds can later be put into a template. */
2777 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2778 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2779 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2780 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2782 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2784 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2785 TYPE_MODULUS for modular types so we make an extra
2786 subtype if necessary. */
2787 if (TYPE_MODULAR_P (gnu_inner))
2789 tree gnu_subtype
2790 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2791 TREE_TYPE (gnu_subtype) = gnu_inner;
2792 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2793 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2794 TYPE_MIN_VALUE (gnu_inner));
2795 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2796 TYPE_MAX_VALUE (gnu_inner));
2797 gnu_inner = gnu_subtype;
2800 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2802 #ifdef ENABLE_CHECKING
2803 /* Check for other cases of overloading. */
2804 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2805 #endif
2808 for (gnat_index = First_Index (gnat_entity);
2809 Present (gnat_index);
2810 gnat_index = Next_Index (gnat_index))
2811 SET_TYPE_ACTUAL_BOUNDS
2812 (gnu_inner,
2813 tree_cons (NULL_TREE,
2814 get_unpadded_type (Etype (gnat_index)),
2815 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2817 if (Convention (gnat_entity) != Convention_Fortran)
2818 SET_TYPE_ACTUAL_BOUNDS
2819 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2821 if (TREE_CODE (gnu_type) == RECORD_TYPE
2822 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2823 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2827 else
2828 /* Abort if packed array with no Packed_Array_Type field set. */
2829 gcc_assert (!Is_Packed (gnat_entity));
2831 break;
2833 case E_String_Literal_Subtype:
2834 /* Create the type for a string literal. */
2836 Entity_Id gnat_full_type
2837 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2838 && Present (Full_View (Etype (gnat_entity)))
2839 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2840 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2841 tree gnu_string_array_type
2842 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2843 tree gnu_string_index_type
2844 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2845 (TYPE_DOMAIN (gnu_string_array_type))));
2846 tree gnu_lower_bound
2847 = convert (gnu_string_index_type,
2848 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2849 tree gnu_length
2850 = UI_To_gnu (String_Literal_Length (gnat_entity),
2851 gnu_string_index_type);
2852 tree gnu_upper_bound
2853 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2854 gnu_lower_bound,
2855 int_const_binop (MINUS_EXPR, gnu_length,
2856 integer_one_node));
2857 tree gnu_index_type
2858 = create_index_type (convert (sizetype, gnu_lower_bound),
2859 convert (sizetype, gnu_upper_bound),
2860 create_range_type (gnu_string_index_type,
2861 gnu_lower_bound,
2862 gnu_upper_bound),
2863 gnat_entity);
2865 gnu_type
2866 = build_nonshared_array_type (gnat_to_gnu_type
2867 (Component_Type (gnat_entity)),
2868 gnu_index_type);
2869 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2870 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2871 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2873 break;
2875 /* Record Types and Subtypes
2877 The following fields are defined on record types:
2879 Has_Discriminants True if the record has discriminants
2880 First_Discriminant Points to head of list of discriminants
2881 First_Entity Points to head of list of fields
2882 Is_Tagged_Type True if the record is tagged
2884 Implementation of Ada records and discriminated records:
2886 A record type definition is transformed into the equivalent of a C
2887 struct definition. The fields that are the discriminants which are
2888 found in the Full_Type_Declaration node and the elements of the
2889 Component_List found in the Record_Type_Definition node. The
2890 Component_List can be a recursive structure since each Variant of
2891 the Variant_Part of the Component_List has a Component_List.
2893 Processing of a record type definition comprises starting the list of
2894 field declarations here from the discriminants and the calling the
2895 function components_to_record to add the rest of the fields from the
2896 component list and return the gnu type node. The function
2897 components_to_record will call itself recursively as it traverses
2898 the tree. */
2900 case E_Record_Type:
2901 if (Has_Complex_Representation (gnat_entity))
2903 gnu_type
2904 = build_complex_type
2905 (get_unpadded_type
2906 (Etype (Defining_Entity
2907 (First (Component_Items
2908 (Component_List
2909 (Type_Definition
2910 (Declaration_Node (gnat_entity)))))))));
2912 break;
2916 Node_Id full_definition = Declaration_Node (gnat_entity);
2917 Node_Id record_definition = Type_Definition (full_definition);
2918 Node_Id gnat_constr;
2919 Entity_Id gnat_field;
2920 tree gnu_field, gnu_field_list = NULL_TREE;
2921 tree gnu_get_parent;
2922 /* Set PACKED in keeping with gnat_to_gnu_field. */
2923 const int packed
2924 = Is_Packed (gnat_entity)
2926 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2927 ? -1
2928 : (Known_Alignment (gnat_entity)
2929 || (Strict_Alignment (gnat_entity)
2930 && Known_RM_Size (gnat_entity)))
2931 ? -2
2932 : 0;
2933 const bool has_discr = Has_Discriminants (gnat_entity);
2934 const bool has_rep = Has_Specified_Layout (gnat_entity);
2935 const bool is_extension
2936 = (Is_Tagged_Type (gnat_entity)
2937 && Nkind (record_definition) == N_Derived_Type_Definition);
2938 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2939 bool all_rep = has_rep;
2941 /* See if all fields have a rep clause. Stop when we find one
2942 that doesn't. */
2943 if (all_rep)
2944 for (gnat_field = First_Entity (gnat_entity);
2945 Present (gnat_field);
2946 gnat_field = Next_Entity (gnat_field))
2947 if ((Ekind (gnat_field) == E_Component
2948 || Ekind (gnat_field) == E_Discriminant)
2949 && No (Component_Clause (gnat_field)))
2951 all_rep = false;
2952 break;
2955 /* If this is a record extension, go a level further to find the
2956 record definition. Also, verify we have a Parent_Subtype. */
2957 if (is_extension)
2959 if (!type_annotate_only
2960 || Present (Record_Extension_Part (record_definition)))
2961 record_definition = Record_Extension_Part (record_definition);
2963 gcc_assert (type_annotate_only
2964 || Present (Parent_Subtype (gnat_entity)));
2967 /* Make a node for the record. If we are not defining the record,
2968 suppress expanding incomplete types. */
2969 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2970 TYPE_NAME (gnu_type) = gnu_entity_name;
2971 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2972 if (Reverse_Storage_Order (gnat_entity))
2973 sorry ("non-default Scalar_Storage_Order");
2974 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
2976 if (!definition)
2978 defer_incomplete_level++;
2979 this_deferred = true;
2982 /* If both a size and rep clause was specified, put the size in
2983 the record type now so that it can get the proper mode. */
2984 if (has_rep && Known_RM_Size (gnat_entity))
2985 TYPE_SIZE (gnu_type)
2986 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
2988 /* Always set the alignment here so that it can be used to
2989 set the mode, if it is making the alignment stricter. If
2990 it is invalid, it will be checked again below. If this is to
2991 be Atomic, choose a default alignment of a word unless we know
2992 the size and it's smaller. */
2993 if (Known_Alignment (gnat_entity))
2994 TYPE_ALIGN (gnu_type)
2995 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2996 else if (Is_Atomic (gnat_entity) && Known_Esize (gnat_entity))
2998 unsigned int size = UI_To_Int (Esize (gnat_entity));
2999 TYPE_ALIGN (gnu_type)
3000 = size >= BITS_PER_WORD ? BITS_PER_WORD : ceil_pow2 (size);
3002 /* If a type needs strict alignment, the minimum size will be the
3003 type size instead of the RM size (see validate_size). Cap the
3004 alignment, lest it causes this type size to become too large. */
3005 else if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
3007 unsigned int raw_size = UI_To_Int (RM_Size (gnat_entity));
3008 unsigned int raw_align = raw_size & -raw_size;
3009 if (raw_align < BIGGEST_ALIGNMENT)
3010 TYPE_ALIGN (gnu_type) = raw_align;
3012 else
3013 TYPE_ALIGN (gnu_type) = 0;
3015 /* If we have a Parent_Subtype, make a field for the parent. If
3016 this record has rep clauses, force the position to zero. */
3017 if (Present (Parent_Subtype (gnat_entity)))
3019 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
3020 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
3021 tree gnu_parent;
3023 /* A major complexity here is that the parent subtype will
3024 reference our discriminants in its Stored_Constraint list.
3025 But those must reference the parent component of this record
3026 which is precisely of the parent subtype we have not built yet!
3027 To break the circle we first build a dummy COMPONENT_REF which
3028 represents the "get to the parent" operation and initialize
3029 each of those discriminants to a COMPONENT_REF of the above
3030 dummy parent referencing the corresponding discriminant of the
3031 base type of the parent subtype. */
3032 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3033 build0 (PLACEHOLDER_EXPR, gnu_type),
3034 build_decl (input_location,
3035 FIELD_DECL, NULL_TREE,
3036 gnu_dummy_parent_type),
3037 NULL_TREE);
3039 if (has_discr)
3040 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3041 Present (gnat_field);
3042 gnat_field = Next_Stored_Discriminant (gnat_field))
3043 if (Present (Corresponding_Discriminant (gnat_field)))
3045 tree gnu_field
3046 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3047 (gnat_field));
3048 save_gnu_tree
3049 (gnat_field,
3050 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3051 gnu_get_parent, gnu_field, NULL_TREE),
3052 true);
3055 /* Then we build the parent subtype. If it has discriminants but
3056 the type itself has unknown discriminants, this means that it
3057 doesn't contain information about how the discriminants are
3058 derived from those of the ancestor type, so it cannot be used
3059 directly. Instead it is built by cloning the parent subtype
3060 of the underlying record view of the type, for which the above
3061 derivation of discriminants has been made explicit. */
3062 if (Has_Discriminants (gnat_parent)
3063 && Has_Unknown_Discriminants (gnat_entity))
3065 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3067 /* If we are defining the type, the underlying record
3068 view must already have been elaborated at this point.
3069 Otherwise do it now as its parent subtype cannot be
3070 technically elaborated on its own. */
3071 if (definition)
3072 gcc_assert (present_gnu_tree (gnat_uview));
3073 else
3074 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
3076 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3078 /* Substitute the "get to the parent" of the type for that
3079 of its underlying record view in the cloned type. */
3080 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3081 Present (gnat_field);
3082 gnat_field = Next_Stored_Discriminant (gnat_field))
3083 if (Present (Corresponding_Discriminant (gnat_field)))
3085 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3086 tree gnu_ref
3087 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3088 gnu_get_parent, gnu_field, NULL_TREE);
3089 gnu_parent
3090 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3093 else
3094 gnu_parent = gnat_to_gnu_type (gnat_parent);
3096 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3097 initially built. The discriminants must reference the fields
3098 of the parent subtype and not those of its base type for the
3099 placeholder machinery to properly work. */
3100 if (has_discr)
3102 /* The actual parent subtype is the full view. */
3103 if (IN (Ekind (gnat_parent), Private_Kind))
3105 if (Present (Full_View (gnat_parent)))
3106 gnat_parent = Full_View (gnat_parent);
3107 else
3108 gnat_parent = Underlying_Full_View (gnat_parent);
3111 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3112 Present (gnat_field);
3113 gnat_field = Next_Stored_Discriminant (gnat_field))
3114 if (Present (Corresponding_Discriminant (gnat_field)))
3116 Entity_Id field = Empty;
3117 for (field = First_Stored_Discriminant (gnat_parent);
3118 Present (field);
3119 field = Next_Stored_Discriminant (field))
3120 if (same_discriminant_p (gnat_field, field))
3121 break;
3122 gcc_assert (Present (field));
3123 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3124 = gnat_to_gnu_field_decl (field);
3128 /* The "get to the parent" COMPONENT_REF must be given its
3129 proper type... */
3130 TREE_TYPE (gnu_get_parent) = gnu_parent;
3132 /* ...and reference the _Parent field of this record. */
3133 gnu_field
3134 = create_field_decl (parent_name_id,
3135 gnu_parent, gnu_type,
3136 has_rep
3137 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3138 has_rep
3139 ? bitsize_zero_node : NULL_TREE,
3140 0, 1);
3141 DECL_INTERNAL_P (gnu_field) = 1;
3142 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3143 TYPE_FIELDS (gnu_type) = gnu_field;
3146 /* Make the fields for the discriminants and put them into the record
3147 unless it's an Unchecked_Union. */
3148 if (has_discr)
3149 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3150 Present (gnat_field);
3151 gnat_field = Next_Stored_Discriminant (gnat_field))
3153 /* If this is a record extension and this discriminant is the
3154 renaming of another discriminant, we've handled it above. */
3155 if (Present (Parent_Subtype (gnat_entity))
3156 && Present (Corresponding_Discriminant (gnat_field)))
3157 continue;
3159 gnu_field
3160 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3161 debug_info_p);
3163 /* Make an expression using a PLACEHOLDER_EXPR from the
3164 FIELD_DECL node just created and link that with the
3165 corresponding GNAT defining identifier. */
3166 save_gnu_tree (gnat_field,
3167 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3168 build0 (PLACEHOLDER_EXPR, gnu_type),
3169 gnu_field, NULL_TREE),
3170 true);
3172 if (!is_unchecked_union)
3174 DECL_CHAIN (gnu_field) = gnu_field_list;
3175 gnu_field_list = gnu_field;
3179 /* If we have a derived untagged type that renames discriminants in
3180 the root type, the (stored) discriminants are a just copy of the
3181 discriminants of the root type. This means that any constraints
3182 added by the renaming in the derivation are disregarded as far
3183 as the layout of the derived type is concerned. To rescue them,
3184 we change the type of the (stored) discriminants to a subtype
3185 with the bounds of the type of the visible discriminants. */
3186 if (has_discr
3187 && !is_extension
3188 && Stored_Constraint (gnat_entity) != No_Elist)
3189 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3190 gnat_constr != No_Elmt;
3191 gnat_constr = Next_Elmt (gnat_constr))
3192 if (Nkind (Node (gnat_constr)) == N_Identifier
3193 /* Ignore access discriminants. */
3194 && !Is_Access_Type (Etype (Node (gnat_constr)))
3195 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3197 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3198 tree gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3199 tree gnu_ref
3200 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3201 NULL_TREE, 0);
3203 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3204 just above for one of the stored discriminants. */
3205 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3207 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3209 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3210 tree gnu_subtype
3211 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3212 ? make_unsigned_type (prec) : make_signed_type (prec);
3213 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3214 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3215 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3216 TYPE_MIN_VALUE (gnu_discr_type));
3217 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3218 TYPE_MAX_VALUE (gnu_discr_type));
3219 TREE_TYPE (gnu_ref)
3220 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3224 /* Add the fields into the record type and finish it up. */
3225 components_to_record (gnu_type, Component_List (record_definition),
3226 gnu_field_list, packed, definition, false,
3227 all_rep, is_unchecked_union,
3228 !Comes_From_Source (gnat_entity), debug_info_p,
3229 false, OK_To_Reorder_Components (gnat_entity),
3230 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3232 /* If it is passed by reference, force BLKmode to ensure that objects
3233 of this type will always be put in memory. */
3234 if (TYPE_MODE (gnu_type) != BLKmode
3235 && Is_By_Reference_Type (gnat_entity))
3236 SET_TYPE_MODE (gnu_type, BLKmode);
3238 /* We used to remove the associations of the discriminants and _Parent
3239 for validity checking but we may need them if there's a Freeze_Node
3240 for a subtype used in this record. */
3241 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3243 /* Fill in locations of fields. */
3244 annotate_rep (gnat_entity, gnu_type);
3246 /* If there are any entities in the chain corresponding to components
3247 that we did not elaborate, ensure we elaborate their types if they
3248 are Itypes. */
3249 for (gnat_temp = First_Entity (gnat_entity);
3250 Present (gnat_temp);
3251 gnat_temp = Next_Entity (gnat_temp))
3252 if ((Ekind (gnat_temp) == E_Component
3253 || Ekind (gnat_temp) == E_Discriminant)
3254 && Is_Itype (Etype (gnat_temp))
3255 && !present_gnu_tree (gnat_temp))
3256 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3258 /* If this is a record type associated with an exception definition,
3259 equate its fields to those of the standard exception type. This
3260 will make it possible to convert between them. */
3261 if (gnu_entity_name == exception_data_name_id)
3263 tree gnu_std_field;
3264 for (gnu_field = TYPE_FIELDS (gnu_type),
3265 gnu_std_field = TYPE_FIELDS (except_type_node);
3266 gnu_field;
3267 gnu_field = DECL_CHAIN (gnu_field),
3268 gnu_std_field = DECL_CHAIN (gnu_std_field))
3269 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3270 gcc_assert (!gnu_std_field);
3273 break;
3275 case E_Class_Wide_Subtype:
3276 /* If an equivalent type is present, that is what we should use.
3277 Otherwise, fall through to handle this like a record subtype
3278 since it may have constraints. */
3279 if (gnat_equiv_type != gnat_entity)
3281 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3282 maybe_present = true;
3283 break;
3286 /* ... fall through ... */
3288 case E_Record_Subtype:
3289 /* If Cloned_Subtype is Present it means this record subtype has
3290 identical layout to that type or subtype and we should use
3291 that GCC type for this one. The front end guarantees that
3292 the component list is shared. */
3293 if (Present (Cloned_Subtype (gnat_entity)))
3295 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3296 NULL_TREE, 0);
3297 maybe_present = true;
3298 break;
3301 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3302 changing the type, make a new type with each field having the type of
3303 the field in the new subtype but the position computed by transforming
3304 every discriminant reference according to the constraints. We don't
3305 see any difference between private and non-private type here since
3306 derivations from types should have been deferred until the completion
3307 of the private type. */
3308 else
3310 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3311 tree gnu_base_type;
3313 if (!definition)
3315 defer_incomplete_level++;
3316 this_deferred = true;
3319 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3321 if (present_gnu_tree (gnat_entity))
3323 maybe_present = true;
3324 break;
3327 /* If this is a record subtype associated with a dispatch table,
3328 strip the suffix. This is necessary to make sure 2 different
3329 subtypes associated with the imported and exported views of a
3330 dispatch table are properly merged in LTO mode. */
3331 if (Is_Dispatch_Table_Entity (gnat_entity))
3333 char *p;
3334 Get_Encoded_Name (gnat_entity);
3335 p = strchr (Name_Buffer, '_');
3336 gcc_assert (p);
3337 strcpy (p+2, "dtS");
3338 gnu_entity_name = get_identifier (Name_Buffer);
3341 /* When the subtype has discriminants and these discriminants affect
3342 the initial shape it has inherited, factor them in. But for an
3343 Unchecked_Union (it must be an Itype), just return the type.
3344 We can't just test Is_Constrained because private subtypes without
3345 discriminants of types with discriminants with default expressions
3346 are Is_Constrained but aren't constrained! */
3347 if (IN (Ekind (gnat_base_type), Record_Kind)
3348 && !Is_Unchecked_Union (gnat_base_type)
3349 && !Is_For_Access_Subtype (gnat_entity)
3350 && Has_Discriminants (gnat_entity)
3351 && Is_Constrained (gnat_entity)
3352 && Stored_Constraint (gnat_entity) != No_Elist)
3354 vec<subst_pair> gnu_subst_list
3355 = build_subst_list (gnat_entity, gnat_base_type, definition);
3356 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3357 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3358 bool selected_variant = false, all_constant_pos = true;
3359 Entity_Id gnat_field;
3360 vec<variant_desc> gnu_variant_list;
3362 gnu_type = make_node (RECORD_TYPE);
3363 TYPE_NAME (gnu_type) = gnu_entity_name;
3364 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3365 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3367 /* Set the size, alignment and alias set of the new type to
3368 match that of the old one, doing required substitutions. */
3369 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3370 gnu_subst_list);
3372 if (TYPE_IS_PADDING_P (gnu_base_type))
3373 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3374 else
3375 gnu_unpad_base_type = gnu_base_type;
3377 /* Look for REP and variant parts in the base type. */
3378 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3379 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3381 /* If there is a variant part, we must compute whether the
3382 constraints statically select a particular variant. If
3383 so, we simply drop the qualified union and flatten the
3384 list of fields. Otherwise we'll build a new qualified
3385 union for the variants that are still relevant. */
3386 if (gnu_variant_part)
3388 variant_desc *v;
3389 unsigned int i;
3391 gnu_variant_list
3392 = build_variant_list (TREE_TYPE (gnu_variant_part),
3393 gnu_subst_list,
3394 vNULL);
3396 /* If all the qualifiers are unconditionally true, the
3397 innermost variant is statically selected. */
3398 selected_variant = true;
3399 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3400 if (!integer_onep (v->qual))
3402 selected_variant = false;
3403 break;
3406 /* Otherwise, create the new variants. */
3407 if (!selected_variant)
3408 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3410 tree old_variant = v->type;
3411 tree new_variant = make_node (RECORD_TYPE);
3412 tree suffix
3413 = concat_name (DECL_NAME (gnu_variant_part),
3414 IDENTIFIER_POINTER
3415 (DECL_NAME (v->field)));
3416 TYPE_NAME (new_variant)
3417 = concat_name (TYPE_NAME (gnu_type),
3418 IDENTIFIER_POINTER (suffix));
3419 copy_and_substitute_in_size (new_variant, old_variant,
3420 gnu_subst_list);
3421 v->new_type = new_variant;
3424 else
3426 gnu_variant_list.create (0);
3427 selected_variant = false;
3430 /* Make a list of fields and their position in the base type. */
3431 gnu_pos_list
3432 = build_position_list (gnu_unpad_base_type,
3433 gnu_variant_list.exists ()
3434 && !selected_variant,
3435 size_zero_node, bitsize_zero_node,
3436 BIGGEST_ALIGNMENT, NULL_TREE);
3438 /* Now go down every component in the subtype and compute its
3439 size and position from those of the component in the base
3440 type and from the constraints of the subtype. */
3441 for (gnat_field = First_Entity (gnat_entity);
3442 Present (gnat_field);
3443 gnat_field = Next_Entity (gnat_field))
3444 if ((Ekind (gnat_field) == E_Component
3445 || Ekind (gnat_field) == E_Discriminant)
3446 && !(Present (Corresponding_Discriminant (gnat_field))
3447 && Is_Tagged_Type (gnat_base_type))
3448 && Underlying_Type
3449 (Scope (Original_Record_Component (gnat_field)))
3450 == gnat_base_type)
3452 Name_Id gnat_name = Chars (gnat_field);
3453 Entity_Id gnat_old_field
3454 = Original_Record_Component (gnat_field);
3455 tree gnu_old_field
3456 = gnat_to_gnu_field_decl (gnat_old_field);
3457 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3458 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3459 tree gnu_cont_type, gnu_last = NULL_TREE;
3461 /* If the type is the same, retrieve the GCC type from the
3462 old field to take into account possible adjustments. */
3463 if (Etype (gnat_field) == Etype (gnat_old_field))
3464 gnu_field_type = TREE_TYPE (gnu_old_field);
3465 else
3466 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3468 /* If there was a component clause, the field types must be
3469 the same for the type and subtype, so copy the data from
3470 the old field to avoid recomputation here. Also if the
3471 field is justified modular and the optimization in
3472 gnat_to_gnu_field was applied. */
3473 if (Present (Component_Clause (gnat_old_field))
3474 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3475 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3476 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3477 == TREE_TYPE (gnu_old_field)))
3479 gnu_size = DECL_SIZE (gnu_old_field);
3480 gnu_field_type = TREE_TYPE (gnu_old_field);
3483 /* If the old field was packed and of constant size, we
3484 have to get the old size here, as it might differ from
3485 what the Etype conveys and the latter might overlap
3486 onto the following field. Try to arrange the type for
3487 possible better packing along the way. */
3488 else if (DECL_PACKED (gnu_old_field)
3489 && TREE_CODE (DECL_SIZE (gnu_old_field))
3490 == INTEGER_CST)
3492 gnu_size = DECL_SIZE (gnu_old_field);
3493 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3494 && !TYPE_FAT_POINTER_P (gnu_field_type)
3495 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3496 gnu_field_type
3497 = make_packable_type (gnu_field_type, true);
3500 else
3501 gnu_size = TYPE_SIZE (gnu_field_type);
3503 /* If the context of the old field is the base type or its
3504 REP part (if any), put the field directly in the new
3505 type; otherwise look up the context in the variant list
3506 and put the field either in the new type if there is a
3507 selected variant or in one of the new variants. */
3508 if (gnu_context == gnu_unpad_base_type
3509 || (gnu_rep_part
3510 && gnu_context == TREE_TYPE (gnu_rep_part)))
3511 gnu_cont_type = gnu_type;
3512 else
3514 variant_desc *v;
3515 unsigned int i;
3516 tree rep_part;
3518 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3519 if (gnu_context == v->type
3520 || ((rep_part = get_rep_part (v->type))
3521 && gnu_context == TREE_TYPE (rep_part)))
3522 break;
3523 if (v)
3525 if (selected_variant)
3526 gnu_cont_type = gnu_type;
3527 else
3528 gnu_cont_type = v->new_type;
3530 else
3531 /* The front-end may pass us "ghost" components if
3532 it fails to recognize that a constrained subtype
3533 is statically constrained. Discard them. */
3534 continue;
3537 /* Now create the new field modeled on the old one. */
3538 gnu_field
3539 = create_field_decl_from (gnu_old_field, gnu_field_type,
3540 gnu_cont_type, gnu_size,
3541 gnu_pos_list, gnu_subst_list);
3542 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3544 /* Put it in one of the new variants directly. */
3545 if (gnu_cont_type != gnu_type)
3547 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3548 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3551 /* To match the layout crafted in components_to_record,
3552 if this is the _Tag or _Parent field, put it before
3553 any other fields. */
3554 else if (gnat_name == Name_uTag
3555 || gnat_name == Name_uParent)
3556 gnu_field_list = chainon (gnu_field_list, gnu_field);
3558 /* Similarly, if this is the _Controller field, put
3559 it before the other fields except for the _Tag or
3560 _Parent field. */
3561 else if (gnat_name == Name_uController && gnu_last)
3563 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3564 DECL_CHAIN (gnu_last) = gnu_field;
3567 /* Otherwise, if this is a regular field, put it after
3568 the other fields. */
3569 else
3571 DECL_CHAIN (gnu_field) = gnu_field_list;
3572 gnu_field_list = gnu_field;
3573 if (!gnu_last)
3574 gnu_last = gnu_field;
3575 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3576 all_constant_pos = false;
3579 save_gnu_tree (gnat_field, gnu_field, false);
3582 /* If there is a variant list, a selected variant and the fields
3583 all have a constant position, put them in order of increasing
3584 position to match that of constant CONSTRUCTORs. Likewise if
3585 there is no variant list but a REP part, since the latter has
3586 been flattened in the process. */
3587 if (((gnu_variant_list.exists () && selected_variant)
3588 || (!gnu_variant_list.exists () && gnu_rep_part))
3589 && all_constant_pos)
3591 const int len = list_length (gnu_field_list);
3592 tree *field_arr = XALLOCAVEC (tree, len), t;
3593 int i;
3595 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3596 field_arr[i] = t;
3598 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3600 gnu_field_list = NULL_TREE;
3601 for (i = 0; i < len; i++)
3603 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3604 gnu_field_list = field_arr[i];
3608 /* If there is a variant list and no selected variant, we need
3609 to create the nest of variant parts from the old nest. */
3610 else if (gnu_variant_list.exists () && !selected_variant)
3612 tree new_variant_part
3613 = create_variant_part_from (gnu_variant_part,
3614 gnu_variant_list, gnu_type,
3615 gnu_pos_list, gnu_subst_list);
3616 DECL_CHAIN (new_variant_part) = gnu_field_list;
3617 gnu_field_list = new_variant_part;
3620 /* Now go through the entities again looking for Itypes that
3621 we have not elaborated but should (e.g., Etypes of fields
3622 that have Original_Components). */
3623 for (gnat_field = First_Entity (gnat_entity);
3624 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3625 if ((Ekind (gnat_field) == E_Discriminant
3626 || Ekind (gnat_field) == E_Component)
3627 && !present_gnu_tree (Etype (gnat_field)))
3628 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3630 /* Do not emit debug info for the type yet since we're going to
3631 modify it below. */
3632 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3633 false);
3634 compute_record_mode (gnu_type);
3636 /* See the E_Record_Type case for the rationale. */
3637 if (TYPE_MODE (gnu_type) != BLKmode
3638 && Is_By_Reference_Type (gnat_entity))
3639 SET_TYPE_MODE (gnu_type, BLKmode);
3641 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3643 /* Fill in locations of fields. */
3644 annotate_rep (gnat_entity, gnu_type);
3646 /* If debugging information is being written for the type, write
3647 a record that shows what we are a subtype of and also make a
3648 variable that indicates our size, if still variable. */
3649 if (debug_info_p)
3651 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3652 tree gnu_unpad_base_name
3653 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3654 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3656 TYPE_NAME (gnu_subtype_marker)
3657 = create_concat_name (gnat_entity, "XVS");
3658 finish_record_type (gnu_subtype_marker,
3659 create_field_decl (gnu_unpad_base_name,
3660 build_reference_type
3661 (gnu_unpad_base_type),
3662 gnu_subtype_marker,
3663 NULL_TREE, NULL_TREE,
3664 0, 0),
3665 0, true);
3667 add_parallel_type (gnu_type, gnu_subtype_marker);
3669 if (definition
3670 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3671 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3672 TYPE_SIZE_UNIT (gnu_subtype_marker)
3673 = create_var_decl (create_concat_name (gnat_entity,
3674 "XVZ"),
3675 NULL_TREE, sizetype, gnu_size_unit,
3676 false, false, false, false, NULL,
3677 gnat_entity);
3680 gnu_variant_list.release ();
3681 gnu_subst_list.release ();
3683 /* Now we can finalize it. */
3684 rest_of_record_type_compilation (gnu_type);
3687 /* Otherwise, go down all the components in the new type and make
3688 them equivalent to those in the base type. */
3689 else
3691 gnu_type = gnu_base_type;
3693 for (gnat_temp = First_Entity (gnat_entity);
3694 Present (gnat_temp);
3695 gnat_temp = Next_Entity (gnat_temp))
3696 if ((Ekind (gnat_temp) == E_Discriminant
3697 && !Is_Unchecked_Union (gnat_base_type))
3698 || Ekind (gnat_temp) == E_Component)
3699 save_gnu_tree (gnat_temp,
3700 gnat_to_gnu_field_decl
3701 (Original_Record_Component (gnat_temp)),
3702 false);
3705 break;
3707 case E_Access_Subprogram_Type:
3708 /* Use the special descriptor type for dispatch tables if needed,
3709 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3710 Note that we are only required to do so for static tables in
3711 order to be compatible with the C++ ABI, but Ada 2005 allows
3712 to extend library level tagged types at the local level so
3713 we do it in the non-static case as well. */
3714 if (TARGET_VTABLE_USES_DESCRIPTORS
3715 && Is_Dispatch_Table_Entity (gnat_entity))
3717 gnu_type = fdesc_type_node;
3718 gnu_size = TYPE_SIZE (gnu_type);
3719 break;
3722 /* ... fall through ... */
3724 case E_Anonymous_Access_Subprogram_Type:
3725 /* If we are not defining this entity, and we have incomplete
3726 entities being processed above us, make a dummy type and
3727 fill it in later. */
3728 if (!definition && defer_incomplete_level != 0)
3730 struct incomplete *p = XNEW (struct incomplete);
3732 gnu_type
3733 = build_pointer_type
3734 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3735 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3736 !Comes_From_Source (gnat_entity),
3737 debug_info_p, gnat_entity);
3738 this_made_decl = true;
3739 gnu_type = TREE_TYPE (gnu_decl);
3740 save_gnu_tree (gnat_entity, gnu_decl, false);
3741 saved = true;
3743 p->old_type = TREE_TYPE (gnu_type);
3744 p->full_type = Directly_Designated_Type (gnat_entity);
3745 p->next = defer_incomplete_list;
3746 defer_incomplete_list = p;
3747 break;
3750 /* ... fall through ... */
3752 case E_Allocator_Type:
3753 case E_Access_Type:
3754 case E_Access_Attribute_Type:
3755 case E_Anonymous_Access_Type:
3756 case E_General_Access_Type:
3758 /* The designated type and its equivalent type for gigi. */
3759 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3760 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3761 /* Whether it comes from a limited with. */
3762 bool is_from_limited_with
3763 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3764 && From_Limited_With (gnat_desig_equiv));
3765 /* The "full view" of the designated type. If this is an incomplete
3766 entity from a limited with, treat its non-limited view as the full
3767 view. Otherwise, if this is an incomplete or private type, use the
3768 full view. In the former case, we might point to a private type,
3769 in which case, we need its full view. Also, we want to look at the
3770 actual type used for the representation, so this takes a total of
3771 three steps. */
3772 Entity_Id gnat_desig_full_direct_first
3773 = (is_from_limited_with
3774 ? Non_Limited_View (gnat_desig_equiv)
3775 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3776 ? Full_View (gnat_desig_equiv) : Empty));
3777 Entity_Id gnat_desig_full_direct
3778 = ((is_from_limited_with
3779 && Present (gnat_desig_full_direct_first)
3780 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3781 ? Full_View (gnat_desig_full_direct_first)
3782 : gnat_desig_full_direct_first);
3783 Entity_Id gnat_desig_full
3784 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3785 /* The type actually used to represent the designated type, either
3786 gnat_desig_full or gnat_desig_equiv. */
3787 Entity_Id gnat_desig_rep;
3788 /* True if this is a pointer to an unconstrained array. */
3789 bool is_unconstrained_array;
3790 /* We want to know if we'll be seeing the freeze node for any
3791 incomplete type we may be pointing to. */
3792 bool in_main_unit
3793 = (Present (gnat_desig_full)
3794 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3795 : In_Extended_Main_Code_Unit (gnat_desig_type));
3796 /* True if we make a dummy type here. */
3797 bool made_dummy = false;
3798 /* The mode to be used for the pointer type. */
3799 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3800 /* The GCC type used for the designated type. */
3801 tree gnu_desig_type = NULL_TREE;
3803 if (!targetm.valid_pointer_mode (p_mode))
3804 p_mode = ptr_mode;
3806 /* If either the designated type or its full view is an unconstrained
3807 array subtype, replace it with the type it's a subtype of. This
3808 avoids problems with multiple copies of unconstrained array types.
3809 Likewise, if the designated type is a subtype of an incomplete
3810 record type, use the parent type to avoid order of elaboration
3811 issues. This can lose some code efficiency, but there is no
3812 alternative. */
3813 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3814 && !Is_Constrained (gnat_desig_equiv))
3815 gnat_desig_equiv = Etype (gnat_desig_equiv);
3816 if (Present (gnat_desig_full)
3817 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3818 && !Is_Constrained (gnat_desig_full))
3819 || (Ekind (gnat_desig_full) == E_Record_Subtype
3820 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3821 gnat_desig_full = Etype (gnat_desig_full);
3823 /* Set the type that's actually the representation of the designated
3824 type and also flag whether we have a unconstrained array. */
3825 gnat_desig_rep
3826 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3827 is_unconstrained_array
3828 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3830 /* If we are pointing to an incomplete type whose completion is an
3831 unconstrained array, make dummy fat and thin pointer types to it.
3832 Likewise if the type itself is dummy or an unconstrained array. */
3833 if (is_unconstrained_array
3834 && (Present (gnat_desig_full)
3835 || (present_gnu_tree (gnat_desig_equiv)
3836 && TYPE_IS_DUMMY_P
3837 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3838 || (!in_main_unit
3839 && defer_incomplete_level != 0
3840 && !present_gnu_tree (gnat_desig_equiv))
3841 || (in_main_unit
3842 && is_from_limited_with
3843 && Present (Freeze_Node (gnat_desig_equiv)))))
3845 if (present_gnu_tree (gnat_desig_rep))
3846 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3847 else
3849 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3850 made_dummy = true;
3853 /* If the call above got something that has a pointer, the pointer
3854 is our type. This could have happened either because the type
3855 was elaborated or because somebody else executed the code. */
3856 if (!TYPE_POINTER_TO (gnu_desig_type))
3857 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3858 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3861 /* If we already know what the full type is, use it. */
3862 else if (Present (gnat_desig_full)
3863 && present_gnu_tree (gnat_desig_full))
3864 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3866 /* Get the type of the thing we are to point to and build a pointer to
3867 it. If it is a reference to an incomplete or private type with a
3868 full view that is a record, make a dummy type node and get the
3869 actual type later when we have verified it is safe. */
3870 else if ((!in_main_unit
3871 && !present_gnu_tree (gnat_desig_equiv)
3872 && Present (gnat_desig_full)
3873 && !present_gnu_tree (gnat_desig_full)
3874 && Is_Record_Type (gnat_desig_full))
3875 /* Likewise if we are pointing to a record or array and we are
3876 to defer elaborating incomplete types. We do this as this
3877 access type may be the full view of a private type. Note
3878 that the unconstrained array case is handled above. */
3879 || ((!in_main_unit || imported_p)
3880 && defer_incomplete_level != 0
3881 && !present_gnu_tree (gnat_desig_equiv)
3882 && (Is_Record_Type (gnat_desig_rep)
3883 || Is_Array_Type (gnat_desig_rep)))
3884 /* If this is a reference from a limited_with type back to our
3885 main unit and there's a freeze node for it, either we have
3886 already processed the declaration and made the dummy type,
3887 in which case we just reuse the latter, or we have not yet,
3888 in which case we make the dummy type and it will be reused
3889 when the declaration is finally processed. In both cases,
3890 the pointer eventually created below will be automatically
3891 adjusted when the freeze node is processed. Note that the
3892 unconstrained array case is handled above. */
3893 || (in_main_unit
3894 && is_from_limited_with
3895 && Present (Freeze_Node (gnat_desig_rep))))
3897 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3898 made_dummy = true;
3901 /* Otherwise handle the case of a pointer to itself. */
3902 else if (gnat_desig_equiv == gnat_entity)
3904 gnu_type
3905 = build_pointer_type_for_mode (void_type_node, p_mode,
3906 No_Strict_Aliasing (gnat_entity));
3907 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3910 /* If expansion is disabled, the equivalent type of a concurrent type
3911 is absent, so build a dummy pointer type. */
3912 else if (type_annotate_only && No (gnat_desig_equiv))
3913 gnu_type = ptr_void_type_node;
3915 /* Finally, handle the default case where we can just elaborate our
3916 designated type. */
3917 else
3918 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3920 /* It is possible that a call to gnat_to_gnu_type above resolved our
3921 type. If so, just return it. */
3922 if (present_gnu_tree (gnat_entity))
3924 maybe_present = true;
3925 break;
3928 /* If we haven't done it yet, build the pointer type the usual way. */
3929 if (!gnu_type)
3931 /* Modify the designated type if we are pointing only to constant
3932 objects, but don't do it for unconstrained arrays. */
3933 if (Is_Access_Constant (gnat_entity)
3934 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3936 gnu_desig_type
3937 = build_qualified_type
3938 (gnu_desig_type,
3939 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3941 /* Some extra processing is required if we are building a
3942 pointer to an incomplete type (in the GCC sense). We might
3943 have such a type if we just made a dummy, or directly out
3944 of the call to gnat_to_gnu_type above if we are processing
3945 an access type for a record component designating the
3946 record type itself. */
3947 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3949 /* We must ensure that the pointer to variant we make will
3950 be processed by update_pointer_to when the initial type
3951 is completed. Pretend we made a dummy and let further
3952 processing act as usual. */
3953 made_dummy = true;
3955 /* We must ensure that update_pointer_to will not retrieve
3956 the dummy variant when building a properly qualified
3957 version of the complete type. We take advantage of the
3958 fact that get_qualified_type is requiring TYPE_NAMEs to
3959 match to influence build_qualified_type and then also
3960 update_pointer_to here. */
3961 TYPE_NAME (gnu_desig_type)
3962 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3966 gnu_type
3967 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3968 No_Strict_Aliasing (gnat_entity));
3971 /* If we are not defining this object and we have made a dummy pointer,
3972 save our current definition, evaluate the actual type, and replace
3973 the tentative type we made with the actual one. If we are to defer
3974 actually looking up the actual type, make an entry in the deferred
3975 list. If this is from a limited with, we may have to defer to the
3976 end of the current unit. */
3977 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3979 tree gnu_old_desig_type;
3981 if (TYPE_IS_FAT_POINTER_P (gnu_type))
3983 gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
3984 if (esize == POINTER_SIZE)
3985 gnu_type = build_pointer_type
3986 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
3988 else
3989 gnu_old_desig_type = TREE_TYPE (gnu_type);
3991 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3992 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3993 !Comes_From_Source (gnat_entity),
3994 debug_info_p, gnat_entity);
3995 this_made_decl = true;
3996 gnu_type = TREE_TYPE (gnu_decl);
3997 save_gnu_tree (gnat_entity, gnu_decl, false);
3998 saved = true;
4000 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
4001 update gnu_old_desig_type directly, in which case it will not be
4002 a dummy type any more when we get into update_pointer_to.
4004 This can happen e.g. when the designated type is a record type,
4005 because their elaboration starts with an initial node from
4006 make_dummy_type, which may be the same node as the one we got.
4008 Besides, variants of this non-dummy type might have been created
4009 along the way. update_pointer_to is expected to properly take
4010 care of those situations. */
4011 if (defer_incomplete_level == 0 && !is_from_limited_with)
4013 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
4014 gnat_to_gnu_type (gnat_desig_equiv));
4016 else
4018 struct incomplete *p = XNEW (struct incomplete);
4019 struct incomplete **head
4020 = (is_from_limited_with
4021 ? &defer_limited_with : &defer_incomplete_list);
4022 p->old_type = gnu_old_desig_type;
4023 p->full_type = gnat_desig_equiv;
4024 p->next = *head;
4025 *head = p;
4029 break;
4031 case E_Access_Protected_Subprogram_Type:
4032 case E_Anonymous_Access_Protected_Subprogram_Type:
4033 if (type_annotate_only && No (gnat_equiv_type))
4034 gnu_type = ptr_void_type_node;
4035 else
4037 /* The run-time representation is the equivalent type. */
4038 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4039 maybe_present = true;
4042 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4043 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4044 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
4045 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
4046 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4047 NULL_TREE, 0);
4049 break;
4051 case E_Access_Subtype:
4053 /* We treat this as identical to its base type; any constraint is
4054 meaningful only to the front-end.
4056 The designated type must be elaborated as well, if it does
4057 not have its own freeze node. Designated (sub)types created
4058 for constrained components of records with discriminants are
4059 not frozen by the front-end and thus not elaborated by gigi,
4060 because their use may appear before the base type is frozen,
4061 and because it is not clear that they are needed anywhere in
4062 gigi. With the current model, there is no correct place where
4063 they could be elaborated. */
4065 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
4066 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4067 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4068 && Is_Frozen (Directly_Designated_Type (gnat_entity))
4069 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4071 /* If we are not defining this entity, and we have incomplete
4072 entities being processed above us, make a dummy type and
4073 elaborate it later. */
4074 if (!definition && defer_incomplete_level != 0)
4076 struct incomplete *p = XNEW (struct incomplete);
4078 p->old_type
4079 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4080 p->full_type = Directly_Designated_Type (gnat_entity);
4081 p->next = defer_incomplete_list;
4082 defer_incomplete_list = p;
4084 else if (!IN (Ekind (Base_Type
4085 (Directly_Designated_Type (gnat_entity))),
4086 Incomplete_Or_Private_Kind))
4087 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4088 NULL_TREE, 0);
4091 maybe_present = true;
4092 break;
4094 /* Subprogram Entities
4096 The following access functions are defined for subprograms:
4098 Etype Return type or Standard_Void_Type.
4099 First_Formal The first formal parameter.
4100 Is_Imported Indicates that the subprogram has appeared in
4101 an INTERFACE or IMPORT pragma. For now we
4102 assume that the external language is C.
4103 Is_Exported Likewise but for an EXPORT pragma.
4104 Is_Inlined True if the subprogram is to be inlined.
4106 Each parameter is first checked by calling must_pass_by_ref on its
4107 type to determine if it is passed by reference. For parameters which
4108 are copied in, if they are Ada In Out or Out parameters, their return
4109 value becomes part of a record which becomes the return type of the
4110 function (C function - note that this applies only to Ada procedures
4111 so there is no Ada return type). Additional code to store back the
4112 parameters will be generated on the caller side. This transformation
4113 is done here, not in the front-end.
4115 The intended result of the transformation can be seen from the
4116 equivalent source rewritings that follow:
4118 struct temp {int a,b};
4119 procedure P (A,B: In Out ...) is temp P (int A,B)
4120 begin {
4121 .. ..
4122 end P; return {A,B};
4125 temp t;
4126 P(X,Y); t = P(X,Y);
4127 X = t.a , Y = t.b;
4129 For subprogram types we need to perform mainly the same conversions to
4130 GCC form that are needed for procedures and function declarations. The
4131 only difference is that at the end, we make a type declaration instead
4132 of a function declaration. */
4134 case E_Subprogram_Type:
4135 case E_Function:
4136 case E_Procedure:
4138 /* The type returned by a function or else Standard_Void_Type for a
4139 procedure. */
4140 Entity_Id gnat_return_type = Etype (gnat_entity);
4141 tree gnu_return_type;
4142 /* The first GCC parameter declaration (a PARM_DECL node). The
4143 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4144 actually is the head of this parameter list. */
4145 tree gnu_param_list = NULL_TREE;
4146 /* Likewise for the stub associated with an exported procedure. */
4147 tree gnu_stub_param_list = NULL_TREE;
4148 /* Non-null for subprograms containing parameters passed by copy-in
4149 copy-out (Ada In Out or Out parameters not passed by reference),
4150 in which case it is the list of nodes used to specify the values
4151 of the In Out/Out parameters that are returned as a record upon
4152 procedure return. The TREE_PURPOSE of an element of this list is
4153 a field of the record and the TREE_VALUE is the PARM_DECL
4154 corresponding to that field. This list will be saved in the
4155 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4156 tree gnu_cico_list = NULL_TREE;
4157 /* List of fields in return type of procedure with copy-in copy-out
4158 parameters. */
4159 tree gnu_field_list = NULL_TREE;
4160 /* If an import pragma asks to map this subprogram to a GCC builtin,
4161 this is the builtin DECL node. */
4162 tree gnu_builtin_decl = NULL_TREE;
4163 /* For the stub associated with an exported procedure. */
4164 tree gnu_stub_type = NULL_TREE, gnu_stub_name = NULL_TREE;
4165 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4166 Entity_Id gnat_param;
4167 enum inline_status_t inline_status
4168 = Has_Pragma_No_Inline (gnat_entity)
4169 ? is_suppressed
4170 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4171 bool public_flag = Is_Public (gnat_entity) || imported_p;
4172 bool extern_flag
4173 = (Is_Public (gnat_entity) && !definition) || imported_p;
4174 bool artificial_flag = !Comes_From_Source (gnat_entity);
4175 /* The semantics of "pure" in Ada essentially matches that of "const"
4176 in the back-end. In particular, both properties are orthogonal to
4177 the "nothrow" property if the EH circuitry is explicit in the
4178 internal representation of the back-end. If we are to completely
4179 hide the EH circuitry from it, we need to declare that calls to pure
4180 Ada subprograms that can throw have side effects since they can
4181 trigger an "abnormal" transfer of control flow; thus they can be
4182 neither "const" nor "pure" in the back-end sense. */
4183 bool const_flag
4184 = (Exception_Mechanism == Back_End_Exceptions
4185 && Is_Pure (gnat_entity));
4186 bool volatile_flag = No_Return (gnat_entity);
4187 bool return_by_direct_ref_p = false;
4188 bool return_by_invisi_ref_p = false;
4189 bool return_unconstrained_p = false;
4190 bool has_stub = false;
4191 int parmnum;
4193 /* A parameter may refer to this type, so defer completion of any
4194 incomplete types. */
4195 if (kind == E_Subprogram_Type && !definition)
4197 defer_incomplete_level++;
4198 this_deferred = true;
4201 /* If the subprogram has an alias, it is probably inherited, so
4202 we can use the original one. If the original "subprogram"
4203 is actually an enumeration literal, it may be the first use
4204 of its type, so we must elaborate that type now. */
4205 if (Present (Alias (gnat_entity)))
4207 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4208 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
4210 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
4212 /* Elaborate any Itypes in the parameters of this entity. */
4213 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4214 Present (gnat_temp);
4215 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4216 if (Is_Itype (Etype (gnat_temp)))
4217 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4219 break;
4222 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4223 corresponding DECL node. Proper generation of calls later on need
4224 proper parameter associations so we don't "break;" here. */
4225 if (Convention (gnat_entity) == Convention_Intrinsic
4226 && Present (Interface_Name (gnat_entity)))
4228 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4230 /* Inability to find the builtin decl most often indicates a
4231 genuine mistake, but imports of unregistered intrinsics are
4232 sometimes issued on purpose to allow hooking in alternate
4233 bodies. We post a warning conditioned on Wshadow in this case,
4234 to let developers be notified on demand without risking false
4235 positives with common default sets of options. */
4237 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
4238 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4241 /* ??? What if we don't find the builtin node above ? warn ? err ?
4242 In the current state we neither warn nor err, and calls will just
4243 be handled as for regular subprograms. */
4245 /* Look into the return type and get its associated GCC tree. If it
4246 is not void, compute various flags for the subprogram type. */
4247 if (Ekind (gnat_return_type) == E_Void)
4248 gnu_return_type = void_type_node;
4249 else
4251 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4252 context may now appear in parameter and result profiles. If
4253 we are only annotating types, break circularities here. */
4254 if (type_annotate_only
4255 && IN (Ekind (gnat_return_type), Incomplete_Kind)
4256 && From_Limited_With (gnat_return_type)
4257 && In_Extended_Main_Code_Unit
4258 (Non_Limited_View (gnat_return_type))
4259 && !present_gnu_tree (Non_Limited_View (gnat_return_type)))
4260 gnu_return_type = ptr_void_type_node;
4261 else
4262 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4264 /* If this function returns by reference, make the actual return
4265 type the pointer type and make a note of that. */
4266 if (Returns_By_Ref (gnat_entity))
4268 gnu_return_type = build_pointer_type (gnu_return_type);
4269 return_by_direct_ref_p = true;
4272 /* If we are supposed to return an unconstrained array type, make
4273 the actual return type the fat pointer type. */
4274 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4276 gnu_return_type = TREE_TYPE (gnu_return_type);
4277 return_unconstrained_p = true;
4280 /* Likewise, if the return type requires a transient scope, the
4281 return value will be allocated on the secondary stack so the
4282 actual return type is the pointer type. */
4283 else if (Requires_Transient_Scope (gnat_return_type))
4285 gnu_return_type = build_pointer_type (gnu_return_type);
4286 return_unconstrained_p = true;
4289 /* If the Mechanism is By_Reference, ensure this function uses the
4290 target's by-invisible-reference mechanism, which may not be the
4291 same as above (e.g. it might be passing an extra parameter). */
4292 else if (kind == E_Function
4293 && Mechanism (gnat_entity) == By_Reference)
4294 return_by_invisi_ref_p = true;
4296 /* Likewise, if the return type is itself By_Reference. */
4297 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
4298 return_by_invisi_ref_p = true;
4300 /* If the type is a padded type and the underlying type would not
4301 be passed by reference or the function has a foreign convention,
4302 return the underlying type. */
4303 else if (TYPE_IS_PADDING_P (gnu_return_type)
4304 && (!default_pass_by_ref
4305 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4306 || Has_Foreign_Convention (gnat_entity)))
4307 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4309 /* If the return type is unconstrained, that means it must have a
4310 maximum size. Use the padded type as the effective return type.
4311 And ensure the function uses the target's by-invisible-reference
4312 mechanism to avoid copying too much data when it returns. */
4313 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4315 tree orig_type = gnu_return_type;
4317 gnu_return_type
4318 = maybe_pad_type (gnu_return_type,
4319 max_size (TYPE_SIZE (gnu_return_type),
4320 true),
4321 0, gnat_entity, false, false, false, true);
4323 /* Declare it now since it will never be declared otherwise.
4324 This is necessary to ensure that its subtrees are properly
4325 marked. */
4326 if (gnu_return_type != orig_type
4327 && !DECL_P (TYPE_NAME (gnu_return_type)))
4328 create_type_decl (TYPE_NAME (gnu_return_type),
4329 gnu_return_type, true, debug_info_p,
4330 gnat_entity);
4332 return_by_invisi_ref_p = true;
4335 /* If the return type has a size that overflows, we cannot have
4336 a function that returns that type. This usage doesn't make
4337 sense anyway, so give an error here. */
4338 if (TYPE_SIZE_UNIT (gnu_return_type)
4339 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
4340 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
4342 post_error ("cannot return type whose size overflows",
4343 gnat_entity);
4344 gnu_return_type = copy_node (gnu_return_type);
4345 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4346 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4347 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4348 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4352 /* Loop over the parameters and get their associated GCC tree. While
4353 doing this, build a copy-in copy-out structure if we need one. */
4354 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4355 Present (gnat_param);
4356 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4358 Entity_Id gnat_param_type = Etype (gnat_param);
4359 tree gnu_param_name = get_entity_name (gnat_param);
4360 tree gnu_param_type, gnu_param, gnu_field;
4361 Mechanism_Type mech = Mechanism (gnat_param);
4362 bool copy_in_copy_out = false, fake_param_type;
4364 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4365 context may now appear in parameter and result profiles. If
4366 we are only annotating types, break circularities here. */
4367 if (type_annotate_only
4368 && IN (Ekind (gnat_param_type), Incomplete_Kind)
4369 && From_Limited_With (Etype (gnat_param_type))
4370 && In_Extended_Main_Code_Unit
4371 (Non_Limited_View (gnat_param_type))
4372 && !present_gnu_tree (Non_Limited_View (gnat_param_type)))
4374 gnu_param_type = ptr_void_type_node;
4375 fake_param_type = true;
4377 else
4379 gnu_param_type = gnat_to_gnu_type (gnat_param_type);
4380 fake_param_type = false;
4383 /* Builtins are expanded inline and there is no real call sequence
4384 involved. So the type expected by the underlying expander is
4385 always the type of each argument "as is". */
4386 if (gnu_builtin_decl)
4387 mech = By_Copy;
4388 /* Handle the first parameter of a valued procedure specially. */
4389 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4390 mech = By_Copy_Return;
4391 /* Otherwise, see if a Mechanism was supplied that forced this
4392 parameter to be passed one way or another. */
4393 else if (mech == Default
4394 || mech == By_Copy || mech == By_Reference)
4396 else if (By_Descriptor_Last <= mech && mech <= By_Descriptor)
4397 mech = By_Descriptor;
4399 else if (By_Short_Descriptor_Last <= mech &&
4400 mech <= By_Short_Descriptor)
4401 mech = By_Short_Descriptor;
4403 else if (mech > 0)
4405 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4406 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4407 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4408 mech))
4409 mech = By_Reference;
4410 else
4411 mech = By_Copy;
4413 else
4415 post_error ("unsupported mechanism for&", gnat_param);
4416 mech = Default;
4419 /* Do not call gnat_to_gnu_param for a fake parameter type since
4420 it will try to use the real type again. */
4421 if (fake_param_type)
4423 if (Ekind (gnat_param) == E_Out_Parameter)
4424 gnu_param = NULL_TREE;
4425 else
4427 gnu_param
4428 = create_param_decl (gnu_param_name, gnu_param_type,
4429 false);
4430 Set_Mechanism (gnat_param,
4431 mech == Default ? By_Copy : mech);
4432 if (Ekind (gnat_param) == E_In_Out_Parameter)
4433 copy_in_copy_out = true;
4436 else
4437 gnu_param
4438 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4439 Has_Foreign_Convention (gnat_entity),
4440 &copy_in_copy_out);
4442 /* We are returned either a PARM_DECL or a type if no parameter
4443 needs to be passed; in either case, adjust the type. */
4444 if (DECL_P (gnu_param))
4445 gnu_param_type = TREE_TYPE (gnu_param);
4446 else
4448 gnu_param_type = gnu_param;
4449 gnu_param = NULL_TREE;
4452 /* The failure of this assertion will very likely come from an
4453 order of elaboration issue for the type of the parameter. */
4454 gcc_assert (kind == E_Subprogram_Type
4455 || !TYPE_IS_DUMMY_P (gnu_param_type)
4456 || type_annotate_only);
4458 if (gnu_param)
4460 /* If it's an exported subprogram, we build a parameter list
4461 in parallel, in case we need to emit a stub for it. */
4462 if (Is_Exported (gnat_entity))
4464 gnu_stub_param_list
4465 = chainon (gnu_param, gnu_stub_param_list);
4466 /* Change By_Descriptor parameter to By_Reference for
4467 the internal version of an exported subprogram. */
4468 if (mech == By_Descriptor || mech == By_Short_Descriptor)
4470 gnu_param
4471 = gnat_to_gnu_param (gnat_param, By_Reference,
4472 gnat_entity, false,
4473 &copy_in_copy_out);
4474 has_stub = true;
4476 else
4477 gnu_param = copy_node (gnu_param);
4480 gnu_param_list = chainon (gnu_param, gnu_param_list);
4481 Sloc_to_locus (Sloc (gnat_param),
4482 &DECL_SOURCE_LOCATION (gnu_param));
4483 save_gnu_tree (gnat_param, gnu_param, false);
4485 /* If a parameter is a pointer, this function may modify
4486 memory through it and thus shouldn't be considered
4487 a const function. Also, the memory may be modified
4488 between two calls, so they can't be CSE'ed. The latter
4489 case also handles by-ref parameters. */
4490 if (POINTER_TYPE_P (gnu_param_type)
4491 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4492 const_flag = false;
4495 if (copy_in_copy_out)
4497 if (!gnu_cico_list)
4499 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4501 /* If this is a function, we also need a field for the
4502 return value to be placed. */
4503 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4505 gnu_field
4506 = create_field_decl (get_identifier ("RETVAL"),
4507 gnu_return_type,
4508 gnu_new_ret_type, NULL_TREE,
4509 NULL_TREE, 0, 0);
4510 Sloc_to_locus (Sloc (gnat_entity),
4511 &DECL_SOURCE_LOCATION (gnu_field));
4512 gnu_field_list = gnu_field;
4513 gnu_cico_list
4514 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4517 gnu_return_type = gnu_new_ret_type;
4518 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4519 /* Set a default alignment to speed up accesses. But we
4520 shouldn't increase the size of the structure too much,
4521 lest it doesn't fit in return registers anymore. */
4522 TYPE_ALIGN (gnu_return_type)
4523 = get_mode_alignment (ptr_mode);
4526 gnu_field
4527 = create_field_decl (gnu_param_name, gnu_param_type,
4528 gnu_return_type, NULL_TREE, NULL_TREE,
4529 0, 0);
4530 Sloc_to_locus (Sloc (gnat_param),
4531 &DECL_SOURCE_LOCATION (gnu_field));
4532 DECL_CHAIN (gnu_field) = gnu_field_list;
4533 gnu_field_list = gnu_field;
4534 gnu_cico_list
4535 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4539 if (gnu_cico_list)
4541 /* If we have a CICO list but it has only one entry, we convert
4542 this function into a function that returns this object. */
4543 if (list_length (gnu_cico_list) == 1)
4544 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4546 /* Do not finalize the return type if the subprogram is stubbed
4547 since structures are incomplete for the back-end. */
4548 else if (Convention (gnat_entity) != Convention_Stubbed)
4550 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4551 0, false);
4553 /* Try to promote the mode of the return type if it is passed
4554 in registers, again to speed up accesses. */
4555 if (TYPE_MODE (gnu_return_type) == BLKmode
4556 && !targetm.calls.return_in_memory (gnu_return_type,
4557 NULL_TREE))
4559 unsigned int size
4560 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
4561 unsigned int i = BITS_PER_UNIT;
4562 enum machine_mode mode;
4564 while (i < size)
4565 i <<= 1;
4566 mode = mode_for_size (i, MODE_INT, 0);
4567 if (mode != BLKmode)
4569 SET_TYPE_MODE (gnu_return_type, mode);
4570 TYPE_ALIGN (gnu_return_type)
4571 = GET_MODE_ALIGNMENT (mode);
4572 TYPE_SIZE (gnu_return_type)
4573 = bitsize_int (GET_MODE_BITSIZE (mode));
4574 TYPE_SIZE_UNIT (gnu_return_type)
4575 = size_int (GET_MODE_SIZE (mode));
4579 if (debug_info_p)
4580 rest_of_record_type_compilation (gnu_return_type);
4584 /* Deal with platform-specific calling conventions. */
4585 if (Has_Stdcall_Convention (gnat_entity))
4586 prepend_one_attribute
4587 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4588 get_identifier ("stdcall"), NULL_TREE,
4589 gnat_entity);
4590 else if (Has_Thiscall_Convention (gnat_entity))
4591 prepend_one_attribute
4592 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4593 get_identifier ("thiscall"), NULL_TREE,
4594 gnat_entity);
4596 /* If we should request stack realignment for a foreign convention
4597 subprogram, do so. Note that this applies to task entry points
4598 in particular. */
4599 if (FOREIGN_FORCE_REALIGN_STACK
4600 && Has_Foreign_Convention (gnat_entity))
4601 prepend_one_attribute
4602 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4603 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4604 gnat_entity);
4606 /* Deal with a pragma Linker_Section on a subprogram. */
4607 if ((kind == E_Function || kind == E_Procedure)
4608 && Present (Linker_Section_Pragma (gnat_entity)))
4609 prepend_one_attribute_pragma (&attr_list,
4610 Linker_Section_Pragma (gnat_entity));
4612 /* The lists have been built in reverse. */
4613 gnu_param_list = nreverse (gnu_param_list);
4614 if (has_stub)
4615 gnu_stub_param_list = nreverse (gnu_stub_param_list);
4616 gnu_cico_list = nreverse (gnu_cico_list);
4618 if (kind == E_Function)
4619 Set_Mechanism (gnat_entity, return_unconstrained_p
4620 || return_by_direct_ref_p
4621 || return_by_invisi_ref_p
4622 ? By_Reference : By_Copy);
4623 gnu_type
4624 = create_subprog_type (gnu_return_type, gnu_param_list,
4625 gnu_cico_list, return_unconstrained_p,
4626 return_by_direct_ref_p,
4627 return_by_invisi_ref_p);
4629 if (has_stub)
4630 gnu_stub_type
4631 = create_subprog_type (gnu_return_type, gnu_stub_param_list,
4632 gnu_cico_list, return_unconstrained_p,
4633 return_by_direct_ref_p,
4634 return_by_invisi_ref_p);
4636 /* A subprogram (something that doesn't return anything) shouldn't
4637 be considered const since there would be no reason for such a
4638 subprogram. Note that procedures with Out (or In Out) parameters
4639 have already been converted into a function with a return type. */
4640 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4641 const_flag = false;
4643 gnu_type
4644 = build_qualified_type (gnu_type,
4645 TYPE_QUALS (gnu_type)
4646 | (TYPE_QUAL_CONST * const_flag)
4647 | (TYPE_QUAL_VOLATILE * volatile_flag));
4649 if (has_stub)
4650 gnu_stub_type
4651 = build_qualified_type (gnu_stub_type,
4652 TYPE_QUALS (gnu_stub_type)
4653 | (TYPE_QUAL_CONST * const_flag)
4654 | (TYPE_QUAL_VOLATILE * volatile_flag));
4656 /* If we have a builtin decl for that function, use it. Check if the
4657 profiles are compatible and warn if they are not. The checker is
4658 expected to post extra diagnostics in this case. */
4659 if (gnu_builtin_decl)
4661 intrin_binding_t inb;
4663 inb.gnat_entity = gnat_entity;
4664 inb.ada_fntype = gnu_type;
4665 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4667 if (!intrin_profiles_compatible_p (&inb))
4668 post_error
4669 ("?profile of& doesn''t match the builtin it binds!",
4670 gnat_entity);
4672 gnu_decl = gnu_builtin_decl;
4673 gnu_type = TREE_TYPE (gnu_builtin_decl);
4674 break;
4677 /* If there was no specified Interface_Name and the external and
4678 internal names of the subprogram are the same, only use the
4679 internal name to allow disambiguation of nested subprograms. */
4680 if (No (Interface_Name (gnat_entity))
4681 && gnu_ext_name == gnu_entity_name)
4682 gnu_ext_name = NULL_TREE;
4684 /* If we are defining the subprogram and it has an Address clause
4685 we must get the address expression from the saved GCC tree for the
4686 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4687 the address expression here since the front-end has guaranteed
4688 in that case that the elaboration has no effects. If there is
4689 an Address clause and we are not defining the object, just
4690 make it a constant. */
4691 if (Present (Address_Clause (gnat_entity)))
4693 tree gnu_address = NULL_TREE;
4695 if (definition)
4696 gnu_address
4697 = (present_gnu_tree (gnat_entity)
4698 ? get_gnu_tree (gnat_entity)
4699 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4701 save_gnu_tree (gnat_entity, NULL_TREE, false);
4703 /* Convert the type of the object to a reference type that can
4704 alias everything as per 13.3(19). */
4705 gnu_type
4706 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4707 if (gnu_address)
4708 gnu_address = convert (gnu_type, gnu_address);
4710 gnu_decl
4711 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4712 gnu_address, false, Is_Public (gnat_entity),
4713 extern_flag, false, NULL, gnat_entity);
4714 DECL_BY_REF_P (gnu_decl) = 1;
4717 else if (kind == E_Subprogram_Type)
4719 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4720 gnu_decl
4721 = create_type_decl (gnu_entity_name, gnu_type, artificial_flag,
4722 debug_info_p, gnat_entity);
4724 else
4726 if (has_stub)
4728 gnu_stub_name = gnu_ext_name;
4729 gnu_ext_name = create_concat_name (gnat_entity, "internal");
4730 public_flag = false;
4731 artificial_flag = true;
4734 gnu_decl
4735 = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4736 gnu_param_list, inline_status,
4737 public_flag, extern_flag, artificial_flag,
4738 attr_list, gnat_entity);
4739 if (has_stub)
4741 tree gnu_stub_decl
4742 = create_subprog_decl (gnu_entity_name, gnu_stub_name,
4743 gnu_stub_type, gnu_stub_param_list,
4744 inline_status, true, extern_flag,
4745 false, attr_list, gnat_entity);
4746 SET_DECL_FUNCTION_STUB (gnu_decl, gnu_stub_decl);
4749 /* This is unrelated to the stub built right above. */
4750 DECL_STUBBED_P (gnu_decl)
4751 = Convention (gnat_entity) == Convention_Stubbed;
4754 break;
4756 case E_Incomplete_Type:
4757 case E_Incomplete_Subtype:
4758 case E_Private_Type:
4759 case E_Private_Subtype:
4760 case E_Limited_Private_Type:
4761 case E_Limited_Private_Subtype:
4762 case E_Record_Type_With_Private:
4763 case E_Record_Subtype_With_Private:
4765 /* Get the "full view" of this entity. If this is an incomplete
4766 entity from a limited with, treat its non-limited view as the
4767 full view. Otherwise, use either the full view or the underlying
4768 full view, whichever is present. This is used in all the tests
4769 below. */
4770 Entity_Id full_view
4771 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity))
4772 ? Non_Limited_View (gnat_entity)
4773 : Present (Full_View (gnat_entity))
4774 ? Full_View (gnat_entity)
4775 : Underlying_Full_View (gnat_entity);
4777 /* If this is an incomplete type with no full view, it must be a Taft
4778 Amendment type, in which case we return a dummy type. Otherwise,
4779 just get the type from its Etype. */
4780 if (No (full_view))
4782 if (kind == E_Incomplete_Type)
4784 gnu_type = make_dummy_type (gnat_entity);
4785 gnu_decl = TYPE_STUB_DECL (gnu_type);
4787 else
4789 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4790 NULL_TREE, 0);
4791 maybe_present = true;
4793 break;
4796 /* If we already made a type for the full view, reuse it. */
4797 else if (present_gnu_tree (full_view))
4799 gnu_decl = get_gnu_tree (full_view);
4800 break;
4803 /* Otherwise, if we are not defining the type now, get the type
4804 from the full view. But always get the type from the full view
4805 for define on use types, since otherwise we won't see them! */
4806 else if (!definition
4807 || (Is_Itype (full_view) && No (Freeze_Node (gnat_entity)))
4808 || (Is_Itype (gnat_entity) && No (Freeze_Node (full_view))))
4810 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4811 maybe_present = true;
4812 break;
4815 /* For incomplete types, make a dummy type entry which will be
4816 replaced later. Save it as the full declaration's type so
4817 we can do any needed updates when we see it. */
4818 gnu_type = make_dummy_type (gnat_entity);
4819 gnu_decl = TYPE_STUB_DECL (gnu_type);
4820 if (Has_Completion_In_Body (gnat_entity))
4821 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4822 save_gnu_tree (full_view, gnu_decl, 0);
4823 break;
4826 case E_Class_Wide_Type:
4827 /* Class-wide types are always transformed into their root type. */
4828 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4829 maybe_present = true;
4830 break;
4832 case E_Task_Type:
4833 case E_Task_Subtype:
4834 case E_Protected_Type:
4835 case E_Protected_Subtype:
4836 /* Concurrent types are always transformed into their record type. */
4837 if (type_annotate_only && No (gnat_equiv_type))
4838 gnu_type = void_type_node;
4839 else
4840 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4841 maybe_present = true;
4842 break;
4844 case E_Label:
4845 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4846 break;
4848 case E_Block:
4849 case E_Loop:
4850 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4851 we've already saved it, so we don't try to. */
4852 gnu_decl = error_mark_node;
4853 saved = true;
4854 break;
4856 case E_Abstract_State:
4857 /* This is a SPARK annotation that only reaches here when compiling in
4858 ASIS mode and has no characteristics to annotate. */
4859 gcc_assert (type_annotate_only);
4860 return error_mark_node;
4862 default:
4863 gcc_unreachable ();
4866 /* If we had a case where we evaluated another type and it might have
4867 defined this one, handle it here. */
4868 if (maybe_present && present_gnu_tree (gnat_entity))
4870 gnu_decl = get_gnu_tree (gnat_entity);
4871 saved = true;
4874 /* If we are processing a type and there is either no decl for it or
4875 we just made one, do some common processing for the type, such as
4876 handling alignment and possible padding. */
4877 if (is_type && (!gnu_decl || this_made_decl))
4879 /* Process the attributes, if not already done. Note that the type is
4880 already defined so we cannot pass true for IN_PLACE here. */
4881 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4883 /* Tell the middle-end that objects of tagged types are guaranteed to
4884 be properly aligned. This is necessary because conversions to the
4885 class-wide type are translated into conversions to the root type,
4886 which can be less aligned than some of its derived types. */
4887 if (Is_Tagged_Type (gnat_entity)
4888 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4889 TYPE_ALIGN_OK (gnu_type) = 1;
4891 /* Record whether the type is passed by reference. */
4892 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4893 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4895 /* ??? Don't set the size for a String_Literal since it is either
4896 confirming or we don't handle it properly (if the low bound is
4897 non-constant). */
4898 if (!gnu_size && kind != E_String_Literal_Subtype)
4900 Uint gnat_size = Known_Esize (gnat_entity)
4901 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4902 gnu_size
4903 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4904 false, Has_Size_Clause (gnat_entity));
4907 /* If a size was specified, see if we can make a new type of that size
4908 by rearranging the type, for example from a fat to a thin pointer. */
4909 if (gnu_size)
4911 gnu_type
4912 = make_type_from_size (gnu_type, gnu_size,
4913 Has_Biased_Representation (gnat_entity));
4915 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4916 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4917 gnu_size = NULL_TREE;
4920 /* If the alignment hasn't already been processed and this is
4921 not an unconstrained array, see if an alignment is specified.
4922 If not, we pick a default alignment for atomic objects. */
4923 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4925 else if (Known_Alignment (gnat_entity))
4927 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4928 TYPE_ALIGN (gnu_type));
4930 /* Warn on suspiciously large alignments. This should catch
4931 errors about the (alignment,byte)/(size,bit) discrepancy. */
4932 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4934 tree size;
4936 /* If a size was specified, take it into account. Otherwise
4937 use the RM size for records or unions as the type size has
4938 already been adjusted to the alignment. */
4939 if (gnu_size)
4940 size = gnu_size;
4941 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4942 && !TYPE_FAT_POINTER_P (gnu_type))
4943 size = rm_size (gnu_type);
4944 else
4945 size = TYPE_SIZE (gnu_type);
4947 /* Consider an alignment as suspicious if the alignment/size
4948 ratio is greater or equal to the byte/bit ratio. */
4949 if (tree_fits_uhwi_p (size)
4950 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4951 post_error_ne ("?suspiciously large alignment specified for&",
4952 Expression (Alignment_Clause (gnat_entity)),
4953 gnat_entity);
4956 else if (Is_Atomic (gnat_entity) && !gnu_size
4957 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4958 && integer_pow2p (TYPE_SIZE (gnu_type)))
4959 align = MIN (BIGGEST_ALIGNMENT,
4960 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4961 else if (Is_Atomic (gnat_entity) && gnu_size
4962 && tree_fits_uhwi_p (gnu_size)
4963 && integer_pow2p (gnu_size))
4964 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4966 /* See if we need to pad the type. If we did, and made a record,
4967 the name of the new type may be changed. So get it back for
4968 us when we make the new TYPE_DECL below. */
4969 if (gnu_size || align > 0)
4970 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4971 false, !gnu_decl, definition, false);
4973 if (TYPE_IS_PADDING_P (gnu_type))
4974 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4976 /* Now set the RM size of the type. We cannot do it before padding
4977 because we need to accept arbitrary RM sizes on integral types. */
4978 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4980 /* If we are at global level, GCC will have applied variable_size to
4981 the type, but that won't have done anything. So, if it's not
4982 a constant or self-referential, call elaborate_expression_1 to
4983 make a variable for the size rather than calculating it each time.
4984 Handle both the RM size and the actual size. */
4985 if (global_bindings_p ()
4986 && TYPE_SIZE (gnu_type)
4987 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4988 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4990 tree size = TYPE_SIZE (gnu_type);
4992 TYPE_SIZE (gnu_type)
4993 = elaborate_expression_1 (size, gnat_entity,
4994 get_identifier ("SIZE"),
4995 definition, false);
4997 /* ??? For now, store the size as a multiple of the alignment in
4998 bytes so that we can see the alignment from the tree. */
4999 TYPE_SIZE_UNIT (gnu_type)
5000 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
5001 get_identifier ("SIZE_A_UNIT"),
5002 definition, false,
5003 TYPE_ALIGN (gnu_type));
5005 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
5006 may not be marked by the call to create_type_decl below. */
5007 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
5009 if (TREE_CODE (gnu_type) == RECORD_TYPE)
5011 tree variant_part = get_variant_part (gnu_type);
5012 tree ada_size = TYPE_ADA_SIZE (gnu_type);
5014 if (variant_part)
5016 tree union_type = TREE_TYPE (variant_part);
5017 tree offset = DECL_FIELD_OFFSET (variant_part);
5019 /* If the position of the variant part is constant, subtract
5020 it from the size of the type of the parent to get the new
5021 size. This manual CSE reduces the data size. */
5022 if (TREE_CODE (offset) == INTEGER_CST)
5024 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
5025 TYPE_SIZE (union_type)
5026 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
5027 bit_from_pos (offset, bitpos));
5028 TYPE_SIZE_UNIT (union_type)
5029 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
5030 byte_from_pos (offset, bitpos));
5032 else
5034 TYPE_SIZE (union_type)
5035 = elaborate_expression_1 (TYPE_SIZE (union_type),
5036 gnat_entity,
5037 get_identifier ("VSIZE"),
5038 definition, false);
5040 /* ??? For now, store the size as a multiple of the
5041 alignment in bytes so that we can see the alignment
5042 from the tree. */
5043 TYPE_SIZE_UNIT (union_type)
5044 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
5045 gnat_entity,
5046 get_identifier
5047 ("VSIZE_A_UNIT"),
5048 definition, false,
5049 TYPE_ALIGN (union_type));
5051 /* ??? For now, store the offset as a multiple of the
5052 alignment in bytes so that we can see the alignment
5053 from the tree. */
5054 DECL_FIELD_OFFSET (variant_part)
5055 = elaborate_expression_2 (offset,
5056 gnat_entity,
5057 get_identifier ("VOFFSET"),
5058 definition, false,
5059 DECL_OFFSET_ALIGN
5060 (variant_part));
5063 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
5064 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
5067 if (operand_equal_p (ada_size, size, 0))
5068 ada_size = TYPE_SIZE (gnu_type);
5069 else
5070 ada_size
5071 = elaborate_expression_1 (ada_size, gnat_entity,
5072 get_identifier ("RM_SIZE"),
5073 definition, false);
5074 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
5078 /* If this is a record type or subtype, call elaborate_expression_2 on
5079 any field position. Do this for both global and local types.
5080 Skip any fields that we haven't made trees for to avoid problems with
5081 class wide types. */
5082 if (IN (kind, Record_Kind))
5083 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
5084 gnat_temp = Next_Entity (gnat_temp))
5085 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
5087 tree gnu_field = get_gnu_tree (gnat_temp);
5089 /* ??? For now, store the offset as a multiple of the alignment
5090 in bytes so that we can see the alignment from the tree. */
5091 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
5093 DECL_FIELD_OFFSET (gnu_field)
5094 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
5095 gnat_temp,
5096 get_identifier ("OFFSET"),
5097 definition, false,
5098 DECL_OFFSET_ALIGN (gnu_field));
5100 /* ??? The context of gnu_field is not necessarily gnu_type
5101 so the MULT_EXPR node built above may not be marked by
5102 the call to create_type_decl below. */
5103 if (global_bindings_p ())
5104 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
5108 if (Treat_As_Volatile (gnat_entity))
5109 gnu_type
5110 = build_qualified_type (gnu_type,
5111 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
5113 if (Is_Atomic (gnat_entity))
5114 check_ok_for_atomic (gnu_type, gnat_entity, false);
5116 if (Present (Alignment_Clause (gnat_entity)))
5117 TYPE_USER_ALIGN (gnu_type) = 1;
5119 if (Universal_Aliasing (gnat_entity))
5120 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
5122 if (!gnu_decl)
5123 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
5124 !Comes_From_Source (gnat_entity),
5125 debug_info_p, gnat_entity);
5126 else
5128 TREE_TYPE (gnu_decl) = gnu_type;
5129 TYPE_STUB_DECL (gnu_type) = gnu_decl;
5133 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
5135 gnu_type = TREE_TYPE (gnu_decl);
5137 /* If this is a derived type, relate its alias set to that of its parent
5138 to avoid troubles when a call to an inherited primitive is inlined in
5139 a context where a derived object is accessed. The inlined code works
5140 on the parent view so the resulting code may access the same object
5141 using both the parent and the derived alias sets, which thus have to
5142 conflict. As the same issue arises with component references, the
5143 parent alias set also has to conflict with composite types enclosing
5144 derived components. For instance, if we have:
5146 type D is new T;
5147 type R is record
5148 Component : D;
5149 end record;
5151 we want T to conflict with both D and R, in addition to R being a
5152 superset of D by record/component construction.
5154 One way to achieve this is to perform an alias set copy from the
5155 parent to the derived type. This is not quite appropriate, though,
5156 as we don't want separate derived types to conflict with each other:
5158 type I1 is new Integer;
5159 type I2 is new Integer;
5161 We want I1 and I2 to both conflict with Integer but we do not want
5162 I1 to conflict with I2, and an alias set copy on derivation would
5163 have that effect.
5165 The option chosen is to make the alias set of the derived type a
5166 superset of that of its parent type. It trivially fulfills the
5167 simple requirement for the Integer derivation example above, and
5168 the component case as well by superset transitivity:
5170 superset superset
5171 R ----------> D ----------> T
5173 However, for composite types, conversions between derived types are
5174 translated into VIEW_CONVERT_EXPRs so a sequence like:
5176 type Comp1 is new Comp;
5177 type Comp2 is new Comp;
5178 procedure Proc (C : Comp1);
5180 C : Comp2;
5181 Proc (Comp1 (C));
5183 is translated into:
5185 C : Comp2;
5186 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5188 and gimplified into:
5190 C : Comp2;
5191 Comp1 *C.0;
5192 C.0 = (Comp1 *) &C;
5193 Proc (C.0);
5195 i.e. generates code involving type punning. Therefore, Comp1 needs
5196 to conflict with Comp2 and an alias set copy is required.
5198 The language rules ensure the parent type is already frozen here. */
5199 if (Is_Derived_Type (gnat_entity) && !type_annotate_only)
5201 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_entity));
5202 relate_alias_sets (gnu_type, gnu_parent_type,
5203 Is_Composite_Type (gnat_entity)
5204 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
5207 /* Back-annotate the Alignment of the type if not already in the
5208 tree. Likewise for sizes. */
5209 if (Unknown_Alignment (gnat_entity))
5211 unsigned int double_align, align;
5212 bool is_capped_double, align_clause;
5214 /* If the default alignment of "double" or larger scalar types is
5215 specifically capped and this is not an array with an alignment
5216 clause on the component type, return the cap. */
5217 if ((double_align = double_float_alignment) > 0)
5218 is_capped_double
5219 = is_double_float_or_array (gnat_entity, &align_clause);
5220 else if ((double_align = double_scalar_alignment) > 0)
5221 is_capped_double
5222 = is_double_scalar_or_array (gnat_entity, &align_clause);
5223 else
5224 is_capped_double = align_clause = false;
5226 if (is_capped_double && !align_clause)
5227 align = double_align;
5228 else
5229 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
5231 Set_Alignment (gnat_entity, UI_From_Int (align));
5234 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
5236 tree gnu_size = TYPE_SIZE (gnu_type);
5238 /* If the size is self-referential, annotate the maximum value. */
5239 if (CONTAINS_PLACEHOLDER_P (gnu_size))
5240 gnu_size = max_size (gnu_size, true);
5242 /* If we are just annotating types and the type is tagged, the tag
5243 and the parent components are not generated by the front-end so
5244 sizes must be adjusted if there is no representation clause. */
5245 if (type_annotate_only
5246 && Is_Tagged_Type (gnat_entity)
5247 && !VOID_TYPE_P (gnu_type)
5248 && (!TYPE_FIELDS (gnu_type)
5249 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
5251 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
5252 Uint uint_size;
5254 if (Is_Derived_Type (gnat_entity))
5256 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
5257 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
5258 Set_Alignment (gnat_entity, Alignment (gnat_parent));
5260 else
5261 offset = pointer_size;
5263 if (TYPE_FIELDS (gnu_type))
5264 offset
5265 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
5267 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
5268 gnu_size = round_up (gnu_size, POINTER_SIZE);
5269 uint_size = annotate_value (gnu_size);
5270 Set_Esize (gnat_entity, uint_size);
5271 Set_RM_Size (gnat_entity, uint_size);
5273 else
5274 Set_Esize (gnat_entity, annotate_value (gnu_size));
5277 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
5278 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
5281 /* If we really have a ..._DECL node, set a couple of flags on it. But we
5282 cannot do so if we are reusing the ..._DECL node made for an equivalent
5283 type or an alias or a renamed object as the predicates don't apply to it
5284 but to GNAT_ENTITY. */
5285 if (DECL_P (gnu_decl)
5286 && !(is_type && gnat_equiv_type != gnat_entity)
5287 && !Present (Alias (gnat_entity))
5288 && !(Present (Renamed_Object (gnat_entity)) && saved))
5290 if (!Comes_From_Source (gnat_entity))
5291 DECL_ARTIFICIAL (gnu_decl) = 1;
5293 if (!debug_info_p)
5294 DECL_IGNORED_P (gnu_decl) = 1;
5297 /* If we haven't already, associate the ..._DECL node that we just made with
5298 the input GNAT entity node. */
5299 if (!saved)
5300 save_gnu_tree (gnat_entity, gnu_decl, false);
5302 /* If this is an enumeration or floating-point type, we were not able to set
5303 the bounds since they refer to the type. These are always static. */
5304 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
5305 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
5307 tree gnu_scalar_type = gnu_type;
5308 tree gnu_low_bound, gnu_high_bound;
5310 /* If this is a padded type, we need to use the underlying type. */
5311 if (TYPE_IS_PADDING_P (gnu_scalar_type))
5312 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
5314 /* If this is a floating point type and we haven't set a floating
5315 point type yet, use this in the evaluation of the bounds. */
5316 if (!longest_float_type_node && kind == E_Floating_Point_Type)
5317 longest_float_type_node = gnu_scalar_type;
5319 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
5320 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
5322 if (kind == E_Enumeration_Type)
5324 /* Enumeration types have specific RM bounds. */
5325 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
5326 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
5328 else
5330 /* Floating-point types don't have specific RM bounds. */
5331 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
5332 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
5336 /* If we deferred processing of incomplete types, re-enable it. If there
5337 were no other disables and we have deferred types to process, do so. */
5338 if (this_deferred
5339 && --defer_incomplete_level == 0
5340 && defer_incomplete_list)
5342 struct incomplete *p, *next;
5344 /* We are back to level 0 for the deferring of incomplete types.
5345 But processing these incomplete types below may itself require
5346 deferring, so preserve what we have and restart from scratch. */
5347 p = defer_incomplete_list;
5348 defer_incomplete_list = NULL;
5350 for (; p; p = next)
5352 next = p->next;
5354 if (p->old_type)
5355 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5356 gnat_to_gnu_type (p->full_type));
5357 free (p);
5361 /* If we are not defining this type, see if it's on one of the lists of
5362 incomplete types. If so, handle the list entry now. */
5363 if (is_type && !definition)
5365 struct incomplete *p;
5367 for (p = defer_incomplete_list; p; p = p->next)
5368 if (p->old_type && p->full_type == gnat_entity)
5370 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5371 TREE_TYPE (gnu_decl));
5372 p->old_type = NULL_TREE;
5375 for (p = defer_limited_with; p; p = p->next)
5376 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5378 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5379 TREE_TYPE (gnu_decl));
5380 p->old_type = NULL_TREE;
5384 if (this_global)
5385 force_global--;
5387 /* If this is a packed array type whose original array type is itself
5388 an Itype without freeze node, make sure the latter is processed. */
5389 if (Is_Packed_Array_Type (gnat_entity)
5390 && Is_Itype (Original_Array_Type (gnat_entity))
5391 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5392 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5393 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5395 return gnu_decl;
5398 /* Similar, but if the returned value is a COMPONENT_REF, return the
5399 FIELD_DECL. */
5401 tree
5402 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5404 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5406 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5407 gnu_field = TREE_OPERAND (gnu_field, 1);
5409 return gnu_field;
5412 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5413 the GCC type corresponding to that entity. */
5415 tree
5416 gnat_to_gnu_type (Entity_Id gnat_entity)
5418 tree gnu_decl;
5420 /* The back end never attempts to annotate generic types. */
5421 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5422 return void_type_node;
5424 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5425 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5427 return TREE_TYPE (gnu_decl);
5430 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5431 the unpadded version of the GCC type corresponding to that entity. */
5433 tree
5434 get_unpadded_type (Entity_Id gnat_entity)
5436 tree type = gnat_to_gnu_type (gnat_entity);
5438 if (TYPE_IS_PADDING_P (type))
5439 type = TREE_TYPE (TYPE_FIELDS (type));
5441 return type;
5444 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5445 type has been changed to that of the parameterless procedure, except if an
5446 alias is already present, in which case it is returned instead. */
5448 tree
5449 get_minimal_subprog_decl (Entity_Id gnat_entity)
5451 tree gnu_entity_name, gnu_ext_name;
5452 struct attrib *attr_list = NULL;
5454 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5455 of the handling applied here. */
5457 while (Present (Alias (gnat_entity)))
5459 gnat_entity = Alias (gnat_entity);
5460 if (present_gnu_tree (gnat_entity))
5461 return get_gnu_tree (gnat_entity);
5464 gnu_entity_name = get_entity_name (gnat_entity);
5465 gnu_ext_name = create_concat_name (gnat_entity, NULL);
5467 if (Has_Stdcall_Convention (gnat_entity))
5468 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5469 get_identifier ("stdcall"), NULL_TREE,
5470 gnat_entity);
5471 else if (Has_Thiscall_Convention (gnat_entity))
5472 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5473 get_identifier ("thiscall"), NULL_TREE,
5474 gnat_entity);
5476 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_name)
5477 gnu_ext_name = NULL_TREE;
5479 return
5480 create_subprog_decl (gnu_entity_name, gnu_ext_name, void_ftype, NULL_TREE,
5481 is_disabled, true, true, true, attr_list, gnat_entity);
5484 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5485 a C++ imported method or equivalent.
5487 We use the predicate on 32-bit x86/Windows to find out whether we need to
5488 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5489 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5491 bool
5492 is_cplusplus_method (Entity_Id gnat_entity)
5494 if (Convention (gnat_entity) != Convention_CPP)
5495 return false;
5497 /* This is the main case: C++ method imported as a primitive operation. */
5498 if (Is_Dispatching_Operation (gnat_entity))
5499 return true;
5501 /* A thunk needs to be handled like its associated primitive operation. */
5502 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5503 return true;
5505 /* C++ classes with no virtual functions can be imported as limited
5506 record types, but we need to return true for the constructors. */
5507 if (Is_Constructor (gnat_entity))
5508 return true;
5510 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5511 if (Is_Dispatch_Table_Entity (gnat_entity))
5512 return true;
5514 return false;
5517 /* Finalize the processing of From_Limited_With incomplete types. */
5519 void
5520 finalize_from_limited_with (void)
5522 struct incomplete *p, *next;
5524 p = defer_limited_with;
5525 defer_limited_with = NULL;
5527 for (; p; p = next)
5529 next = p->next;
5531 if (p->old_type)
5532 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5533 gnat_to_gnu_type (p->full_type));
5534 free (p);
5538 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5539 kind of type (such E_Task_Type) that has a different type which Gigi
5540 uses for its representation. If the type does not have a special type
5541 for its representation, return GNAT_ENTITY. If a type is supposed to
5542 exist, but does not, abort unless annotating types, in which case
5543 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5545 Entity_Id
5546 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5548 Entity_Id gnat_equiv = gnat_entity;
5550 if (No (gnat_entity))
5551 return gnat_entity;
5553 switch (Ekind (gnat_entity))
5555 case E_Class_Wide_Subtype:
5556 if (Present (Equivalent_Type (gnat_entity)))
5557 gnat_equiv = Equivalent_Type (gnat_entity);
5558 break;
5560 case E_Access_Protected_Subprogram_Type:
5561 case E_Anonymous_Access_Protected_Subprogram_Type:
5562 gnat_equiv = Equivalent_Type (gnat_entity);
5563 break;
5565 case E_Class_Wide_Type:
5566 gnat_equiv = Root_Type (gnat_entity);
5567 break;
5569 case E_Task_Type:
5570 case E_Task_Subtype:
5571 case E_Protected_Type:
5572 case E_Protected_Subtype:
5573 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5574 break;
5576 default:
5577 break;
5580 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5582 return gnat_equiv;
5585 /* Return a GCC tree for a type corresponding to the component type of the
5586 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5587 is for an array being defined. DEBUG_INFO_P is true if we need to write
5588 debug information for other types that we may create in the process. */
5590 static tree
5591 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5592 bool debug_info_p)
5594 const Entity_Id gnat_type = Component_Type (gnat_array);
5595 tree gnu_type = gnat_to_gnu_type (gnat_type);
5596 tree gnu_comp_size;
5598 /* Try to get a smaller form of the component if needed. */
5599 if ((Is_Packed (gnat_array)
5600 || Has_Component_Size_Clause (gnat_array))
5601 && !Is_Bit_Packed_Array (gnat_array)
5602 && !Has_Aliased_Components (gnat_array)
5603 && !Strict_Alignment (gnat_type)
5604 && RECORD_OR_UNION_TYPE_P (gnu_type)
5605 && !TYPE_FAT_POINTER_P (gnu_type)
5606 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5607 gnu_type = make_packable_type (gnu_type, false);
5609 if (Has_Atomic_Components (gnat_array))
5610 check_ok_for_atomic (gnu_type, gnat_array, true);
5612 /* Get and validate any specified Component_Size. */
5613 gnu_comp_size
5614 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5615 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5616 true, Has_Component_Size_Clause (gnat_array));
5618 /* If the array has aliased components and the component size can be zero,
5619 force at least unit size to ensure that the components have distinct
5620 addresses. */
5621 if (!gnu_comp_size
5622 && Has_Aliased_Components (gnat_array)
5623 && (integer_zerop (TYPE_SIZE (gnu_type))
5624 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5625 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5626 gnu_comp_size
5627 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5629 /* If the component type is a RECORD_TYPE that has a self-referential size,
5630 then use the maximum size for the component size. */
5631 if (!gnu_comp_size
5632 && TREE_CODE (gnu_type) == RECORD_TYPE
5633 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5634 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5636 /* Honor the component size. This is not needed for bit-packed arrays. */
5637 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5639 tree orig_type = gnu_type;
5640 unsigned int max_align;
5642 /* If an alignment is specified, use it as a cap on the component type
5643 so that it can be honored for the whole type. But ignore it for the
5644 original type of packed array types. */
5645 if (No (Packed_Array_Type (gnat_array)) && Known_Alignment (gnat_array))
5646 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5647 else
5648 max_align = 0;
5650 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5651 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5652 gnu_type = orig_type;
5653 else
5654 orig_type = gnu_type;
5656 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5657 true, false, definition, true);
5659 /* If a padding record was made, declare it now since it will never be
5660 declared otherwise. This is necessary to ensure that its subtrees
5661 are properly marked. */
5662 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5663 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5664 gnat_array);
5667 if (Has_Volatile_Components (gnat_array))
5668 gnu_type
5669 = build_qualified_type (gnu_type,
5670 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
5672 return gnu_type;
5675 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5676 using MECH as its passing mechanism, to be placed in the parameter
5677 list built for GNAT_SUBPROG. Assume a foreign convention for the
5678 latter if FOREIGN is true. Also set CICO to true if the parameter
5679 must use the copy-in copy-out implementation mechanism.
5681 The returned tree is a PARM_DECL, except for those cases where no
5682 parameter needs to be actually passed to the subprogram; the type
5683 of this "shadow" parameter is then returned instead. */
5685 static tree
5686 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5687 Entity_Id gnat_subprog, bool foreign, bool *cico)
5689 tree gnu_param_name = get_entity_name (gnat_param);
5690 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5691 tree gnu_param_type_alt = NULL_TREE;
5692 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5693 /* The parameter can be indirectly modified if its address is taken. */
5694 bool ro_param = in_param && !Address_Taken (gnat_param);
5695 bool by_return = false, by_component_ptr = false;
5696 bool by_ref = false;
5697 tree gnu_param;
5699 /* Copy-return is used only for the first parameter of a valued procedure.
5700 It's a copy mechanism for which a parameter is never allocated. */
5701 if (mech == By_Copy_Return)
5703 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5704 mech = By_Copy;
5705 by_return = true;
5708 /* If this is either a foreign function or if the underlying type won't
5709 be passed by reference, strip off possible padding type. */
5710 if (TYPE_IS_PADDING_P (gnu_param_type))
5712 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5714 if (mech == By_Reference
5715 || foreign
5716 || (!must_pass_by_ref (unpadded_type)
5717 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
5718 gnu_param_type = unpadded_type;
5721 /* If this is a read-only parameter, make a variant of the type that is
5722 read-only. ??? However, if this is an unconstrained array, that type
5723 can be very complex, so skip it for now. Likewise for any other
5724 self-referential type. */
5725 if (ro_param
5726 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5727 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5728 gnu_param_type = build_qualified_type (gnu_param_type,
5729 (TYPE_QUALS (gnu_param_type)
5730 | TYPE_QUAL_CONST));
5732 /* For foreign conventions, pass arrays as pointers to the element type.
5733 First check for unconstrained array and get the underlying array. */
5734 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5735 gnu_param_type
5736 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5738 /* For GCC builtins, pass Address integer types as (void *) */
5739 if (Convention (gnat_subprog) == Convention_Intrinsic
5740 && Present (Interface_Name (gnat_subprog))
5741 && Is_Descendent_Of_Address (Etype (gnat_param)))
5742 gnu_param_type = ptr_void_type_node;
5744 /* VMS descriptors are themselves passed by reference. */
5745 if (mech == By_Short_Descriptor ||
5746 (mech == By_Descriptor && TARGET_ABI_OPEN_VMS && !flag_vms_malloc64))
5747 gnu_param_type
5748 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5749 Mechanism (gnat_param),
5750 gnat_subprog));
5751 else if (mech == By_Descriptor)
5753 /* Build both a 32-bit and 64-bit descriptor, one of which will be
5754 chosen in fill_vms_descriptor. */
5755 gnu_param_type_alt
5756 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5757 Mechanism (gnat_param),
5758 gnat_subprog));
5759 gnu_param_type
5760 = build_pointer_type (build_vms_descriptor (gnu_param_type,
5761 Mechanism (gnat_param),
5762 gnat_subprog));
5765 /* Arrays are passed as pointers to element type for foreign conventions. */
5766 else if (foreign
5767 && mech != By_Copy
5768 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5770 /* Strip off any multi-dimensional entries, then strip
5771 off the last array to get the component type. */
5772 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5773 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5774 gnu_param_type = TREE_TYPE (gnu_param_type);
5776 by_component_ptr = true;
5777 gnu_param_type = TREE_TYPE (gnu_param_type);
5779 if (ro_param)
5780 gnu_param_type = build_qualified_type (gnu_param_type,
5781 (TYPE_QUALS (gnu_param_type)
5782 | TYPE_QUAL_CONST));
5784 gnu_param_type = build_pointer_type (gnu_param_type);
5787 /* Fat pointers are passed as thin pointers for foreign conventions. */
5788 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5789 gnu_param_type
5790 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5792 /* If we must pass or were requested to pass by reference, do so.
5793 If we were requested to pass by copy, do so.
5794 Otherwise, for foreign conventions, pass In Out or Out parameters
5795 or aggregates by reference. For COBOL and Fortran, pass all
5796 integer and FP types that way too. For Convention Ada, use
5797 the standard Ada default. */
5798 else if (must_pass_by_ref (gnu_param_type)
5799 || mech == By_Reference
5800 || (mech != By_Copy
5801 && ((foreign
5802 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5803 || (foreign
5804 && (Convention (gnat_subprog) == Convention_Fortran
5805 || Convention (gnat_subprog) == Convention_COBOL)
5806 && (INTEGRAL_TYPE_P (gnu_param_type)
5807 || FLOAT_TYPE_P (gnu_param_type)))
5808 || (!foreign
5809 && default_pass_by_ref (gnu_param_type)))))
5811 /* We take advantage of 6.2(12) by considering that references built for
5812 parameters whose type isn't by-ref and for which the mechanism hasn't
5813 been forced to by-ref are restrict-qualified in the C sense. */
5814 bool restrict_p
5815 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5816 gnu_param_type = build_reference_type (gnu_param_type);
5817 if (restrict_p)
5818 gnu_param_type
5819 = build_qualified_type (gnu_param_type, TYPE_QUAL_RESTRICT);
5820 by_ref = true;
5823 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5824 else if (!in_param)
5825 *cico = true;
5827 if (mech == By_Copy && (by_ref || by_component_ptr))
5828 post_error ("?cannot pass & by copy", gnat_param);
5830 /* If this is an Out parameter that isn't passed by reference and isn't
5831 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5832 it will be a VAR_DECL created when we process the procedure, so just
5833 return its type. For the special parameter of a valued procedure,
5834 never pass it in.
5836 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5837 Out parameters with discriminants or implicit initial values to be
5838 handled like In Out parameters. These type are normally built as
5839 aggregates, hence passed by reference, except for some packed arrays
5840 which end up encoded in special integer types. Note that scalars can
5841 be given implicit initial values using the Default_Value aspect.
5843 The exception we need to make is then for packed arrays of records
5844 with discriminants or implicit initial values. We have no light/easy
5845 way to check for the latter case, so we merely check for packed arrays
5846 of records. This may lead to useless copy-in operations, but in very
5847 rare cases only, as these would be exceptions in a set of already
5848 exceptional situations. */
5849 if (Ekind (gnat_param) == E_Out_Parameter
5850 && !by_ref
5851 && (by_return
5852 || (mech != By_Descriptor
5853 && mech != By_Short_Descriptor
5854 && !POINTER_TYPE_P (gnu_param_type)
5855 && !AGGREGATE_TYPE_P (gnu_param_type)
5856 && !Has_Default_Aspect (Etype (gnat_param))))
5857 && !(Is_Array_Type (Etype (gnat_param))
5858 && Is_Packed (Etype (gnat_param))
5859 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5860 return gnu_param_type;
5862 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5863 ro_param || by_ref || by_component_ptr);
5864 DECL_BY_REF_P (gnu_param) = by_ref;
5865 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5866 DECL_BY_DESCRIPTOR_P (gnu_param)
5867 = (mech == By_Descriptor || mech == By_Short_Descriptor);
5868 DECL_POINTS_TO_READONLY_P (gnu_param)
5869 = (ro_param && (by_ref || by_component_ptr));
5870 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5872 /* Save the alternate descriptor type, if any. */
5873 if (gnu_param_type_alt)
5874 SET_DECL_PARM_ALT_TYPE (gnu_param, gnu_param_type_alt);
5876 /* If no Mechanism was specified, indicate what we're using, then
5877 back-annotate it. */
5878 if (mech == Default)
5879 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5881 Set_Mechanism (gnat_param, mech);
5882 return gnu_param;
5885 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5887 static bool
5888 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5890 while (Present (Corresponding_Discriminant (discr1)))
5891 discr1 = Corresponding_Discriminant (discr1);
5893 while (Present (Corresponding_Discriminant (discr2)))
5894 discr2 = Corresponding_Discriminant (discr2);
5896 return
5897 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5900 /* Return true if the array type GNU_TYPE, which represents a dimension of
5901 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5903 static bool
5904 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5906 /* If the array type is not the innermost dimension of the GNAT type,
5907 then it has a non-aliased component. */
5908 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5909 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5910 return true;
5912 /* If the array type has an aliased component in the front-end sense,
5913 then it also has an aliased component in the back-end sense. */
5914 if (Has_Aliased_Components (gnat_type))
5915 return false;
5917 /* If this is a derived type, then it has a non-aliased component if
5918 and only if its parent type also has one. */
5919 if (Is_Derived_Type (gnat_type))
5921 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5922 int index;
5923 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5924 gnu_parent_type
5925 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5926 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5927 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5928 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5931 /* Otherwise, rely exclusively on properties of the element type. */
5932 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5935 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5937 static bool
5938 compile_time_known_address_p (Node_Id gnat_address)
5940 /* Catch System'To_Address. */
5941 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5942 gnat_address = Expression (gnat_address);
5944 return Compile_Time_Known_Value (gnat_address);
5947 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5948 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5950 static bool
5951 cannot_be_superflat_p (Node_Id gnat_range)
5953 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5954 Node_Id scalar_range;
5955 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5957 /* If the low bound is not constant, try to find an upper bound. */
5958 while (Nkind (gnat_lb) != N_Integer_Literal
5959 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5960 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5961 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5962 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5963 || Nkind (scalar_range) == N_Range))
5964 gnat_lb = High_Bound (scalar_range);
5966 /* If the high bound is not constant, try to find a lower bound. */
5967 while (Nkind (gnat_hb) != N_Integer_Literal
5968 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5969 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5970 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5971 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5972 || Nkind (scalar_range) == N_Range))
5973 gnat_hb = Low_Bound (scalar_range);
5975 /* If we have failed to find constant bounds, punt. */
5976 if (Nkind (gnat_lb) != N_Integer_Literal
5977 || Nkind (gnat_hb) != N_Integer_Literal)
5978 return false;
5980 /* We need at least a signed 64-bit type to catch most cases. */
5981 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5982 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5983 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5984 return false;
5986 /* If the low bound is the smallest integer, nothing can be smaller. */
5987 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5988 if (TREE_OVERFLOW (gnu_lb_minus_one))
5989 return true;
5991 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5994 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5996 static bool
5997 constructor_address_p (tree gnu_expr)
5999 while (TREE_CODE (gnu_expr) == NOP_EXPR
6000 || TREE_CODE (gnu_expr) == CONVERT_EXPR
6001 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
6002 gnu_expr = TREE_OPERAND (gnu_expr, 0);
6004 return (TREE_CODE (gnu_expr) == ADDR_EXPR
6005 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
6008 /* Given GNAT_ENTITY, elaborate all expressions that are required to
6009 be elaborated at the point of its definition, but do nothing else. */
6011 void
6012 elaborate_entity (Entity_Id gnat_entity)
6014 switch (Ekind (gnat_entity))
6016 case E_Signed_Integer_Subtype:
6017 case E_Modular_Integer_Subtype:
6018 case E_Enumeration_Subtype:
6019 case E_Ordinary_Fixed_Point_Subtype:
6020 case E_Decimal_Fixed_Point_Subtype:
6021 case E_Floating_Point_Subtype:
6023 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
6024 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
6026 /* ??? Tests to avoid Constraint_Error in static expressions
6027 are needed until after the front stops generating bogus
6028 conversions on bounds of real types. */
6029 if (!Raises_Constraint_Error (gnat_lb))
6030 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
6031 true, false, Needs_Debug_Info (gnat_entity));
6032 if (!Raises_Constraint_Error (gnat_hb))
6033 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
6034 true, false, Needs_Debug_Info (gnat_entity));
6035 break;
6038 case E_Record_Subtype:
6039 case E_Private_Subtype:
6040 case E_Limited_Private_Subtype:
6041 case E_Record_Subtype_With_Private:
6042 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6044 Node_Id gnat_discriminant_expr;
6045 Entity_Id gnat_field;
6047 for (gnat_field
6048 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6049 gnat_discriminant_expr
6050 = First_Elmt (Discriminant_Constraint (gnat_entity));
6051 Present (gnat_field);
6052 gnat_field = Next_Discriminant (gnat_field),
6053 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6054 /* Ignore access discriminants. */
6055 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6056 elaborate_expression (Node (gnat_discriminant_expr),
6057 gnat_entity, get_entity_name (gnat_field),
6058 true, false, false);
6060 break;
6065 /* Return true if the size in units represented by GNU_SIZE can be handled by
6066 an allocation. If STATIC_P is true, consider only what can be done with a
6067 static allocation. */
6069 static bool
6070 allocatable_size_p (tree gnu_size, bool static_p)
6072 /* We can allocate a fixed size if it is a valid for the middle-end. */
6073 if (TREE_CODE (gnu_size) == INTEGER_CST)
6074 return valid_constant_size_p (gnu_size);
6076 /* We can allocate a variable size if this isn't a static allocation. */
6077 else
6078 return !static_p;
6081 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6082 NAME, ARGS and ERROR_POINT. */
6084 static void
6085 prepend_one_attribute (struct attrib **attr_list,
6086 enum attr_type attr_type,
6087 tree attr_name,
6088 tree attr_args,
6089 Node_Id attr_error_point)
6091 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6093 attr->type = attr_type;
6094 attr->name = attr_name;
6095 attr->args = attr_args;
6096 attr->error_point = attr_error_point;
6098 attr->next = *attr_list;
6099 *attr_list = attr;
6102 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6104 static void
6105 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6107 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6108 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6109 enum attr_type etype;
6111 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6112 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6114 case Pragma_Machine_Attribute:
6115 etype = ATTR_MACHINE_ATTRIBUTE;
6116 break;
6118 case Pragma_Linker_Alias:
6119 etype = ATTR_LINK_ALIAS;
6120 break;
6122 case Pragma_Linker_Section:
6123 etype = ATTR_LINK_SECTION;
6124 break;
6126 case Pragma_Linker_Constructor:
6127 etype = ATTR_LINK_CONSTRUCTOR;
6128 break;
6130 case Pragma_Linker_Destructor:
6131 etype = ATTR_LINK_DESTRUCTOR;
6132 break;
6134 case Pragma_Weak_External:
6135 etype = ATTR_WEAK_EXTERNAL;
6136 break;
6138 case Pragma_Thread_Local_Storage:
6139 etype = ATTR_THREAD_LOCAL_STORAGE;
6140 break;
6142 default:
6143 return;
6146 /* See what arguments we have and turn them into GCC trees for attribute
6147 handlers. These expect identifier for strings. We handle at most two
6148 arguments and static expressions only. */
6149 if (Present (gnat_arg) && Present (First (gnat_arg)))
6151 Node_Id gnat_arg0 = Next (First (gnat_arg));
6152 Node_Id gnat_arg1 = Empty;
6154 if (Present (gnat_arg0)
6155 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6157 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6159 if (TREE_CODE (gnu_arg0) == STRING_CST)
6161 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6162 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6163 return;
6166 gnat_arg1 = Next (gnat_arg0);
6169 if (Present (gnat_arg1)
6170 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6172 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6174 if (TREE_CODE (gnu_arg1) == STRING_CST)
6175 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6179 /* Prepend to the list. Make a list of the argument we might have, as GCC
6180 expects it. */
6181 prepend_one_attribute (attr_list, etype, gnu_arg0,
6182 gnu_arg1
6183 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6184 Present (Next (First (gnat_arg)))
6185 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6188 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6190 static void
6191 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6193 Node_Id gnat_temp;
6195 /* Attributes are stored as Representation Item pragmas. */
6196 for (gnat_temp = First_Rep_Item (gnat_entity);
6197 Present (gnat_temp);
6198 gnat_temp = Next_Rep_Item (gnat_temp))
6199 if (Nkind (gnat_temp) == N_Pragma)
6200 prepend_one_attribute_pragma (attr_list, gnat_temp);
6203 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6204 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6205 return the GCC tree to use for that expression. GNU_NAME is the suffix
6206 to use if a variable needs to be created and DEFINITION is true if this
6207 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6208 otherwise, we are just elaborating the expression for side-effects. If
6209 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6210 isn't needed for code generation. */
6212 static tree
6213 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
6214 bool definition, bool need_value, bool need_debug)
6216 tree gnu_expr;
6218 /* If we already elaborated this expression (e.g. it was involved
6219 in the definition of a private type), use the old value. */
6220 if (present_gnu_tree (gnat_expr))
6221 return get_gnu_tree (gnat_expr);
6223 /* If we don't need a value and this is static or a discriminant,
6224 we don't need to do anything. */
6225 if (!need_value
6226 && (Is_OK_Static_Expression (gnat_expr)
6227 || (Nkind (gnat_expr) == N_Identifier
6228 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6229 return NULL_TREE;
6231 /* If it's a static expression, we don't need a variable for debugging. */
6232 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6233 need_debug = false;
6235 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6236 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
6237 gnu_name, definition, need_debug);
6239 /* Save the expression in case we try to elaborate this entity again. Since
6240 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6241 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6242 save_gnu_tree (gnat_expr, gnu_expr, true);
6244 return need_value ? gnu_expr : error_mark_node;
6247 /* Similar, but take a GNU expression and always return a result. */
6249 static tree
6250 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6251 bool definition, bool need_debug)
6253 const bool expr_public_p = Is_Public (gnat_entity);
6254 const bool expr_global_p = expr_public_p || global_bindings_p ();
6255 bool expr_variable_p, use_variable;
6257 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6258 reference will have been replaced with a COMPONENT_REF when the type
6259 is being elaborated. However, there are some cases involving child
6260 types where we will. So convert it to a COMPONENT_REF. We hope it
6261 will be at the highest level of the expression in these cases. */
6262 if (TREE_CODE (gnu_expr) == FIELD_DECL)
6263 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6264 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6265 gnu_expr, NULL_TREE);
6267 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6268 that an expression cannot contain both a discriminant and a variable. */
6269 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6270 return gnu_expr;
6272 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6273 a variable that is initialized to contain the expression when the package
6274 containing the definition is elaborated. If this entity is defined at top
6275 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6276 if this is necessary. */
6277 if (CONSTANT_CLASS_P (gnu_expr))
6278 expr_variable_p = false;
6279 else
6281 /* Skip any conversions and simple constant arithmetics to see if the
6282 expression is based on a read-only variable.
6283 ??? This really should remain read-only, but we have to think about
6284 the typing of the tree here. */
6285 tree inner = remove_conversions (gnu_expr, true);
6287 inner = skip_simple_constant_arithmetic (inner);
6289 if (handled_component_p (inner))
6291 HOST_WIDE_INT bitsize, bitpos;
6292 tree offset;
6293 enum machine_mode mode;
6294 int unsignedp, volatilep;
6296 inner = get_inner_reference (inner, &bitsize, &bitpos, &offset,
6297 &mode, &unsignedp, &volatilep, false);
6298 /* If the offset is variable, err on the side of caution. */
6299 if (offset)
6300 inner = NULL_TREE;
6303 expr_variable_p
6304 = !(inner
6305 && TREE_CODE (inner) == VAR_DECL
6306 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6309 /* We only need to use the variable if we are in a global context since GCC
6310 can do the right thing in the local case. However, when not optimizing,
6311 use it for bounds of loop iteration scheme to avoid code duplication. */
6312 use_variable = expr_variable_p
6313 && (expr_global_p
6314 || (!optimize
6315 && definition
6316 && Is_Itype (gnat_entity)
6317 && Nkind (Associated_Node_For_Itype (gnat_entity))
6318 == N_Loop_Parameter_Specification));
6320 /* Now create it, possibly only for debugging purposes. */
6321 if (use_variable || need_debug)
6323 tree gnu_decl
6324 = create_var_decl_1
6325 (create_concat_name (gnat_entity, IDENTIFIER_POINTER (gnu_name)),
6326 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true, expr_public_p,
6327 !definition, expr_global_p, !need_debug, NULL, gnat_entity);
6329 if (use_variable)
6330 return gnu_decl;
6333 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6336 /* Similar, but take an alignment factor and make it explicit in the tree. */
6338 static tree
6339 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6340 bool definition, bool need_debug, unsigned int align)
6342 tree unit_align = size_int (align / BITS_PER_UNIT);
6343 return
6344 size_binop (MULT_EXPR,
6345 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6346 gnu_expr,
6347 unit_align),
6348 gnat_entity, gnu_name, definition,
6349 need_debug),
6350 unit_align);
6353 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6354 the value passed against the list of choices. */
6356 tree
6357 choices_to_gnu (tree operand, Node_Id choices)
6359 Node_Id choice;
6360 Node_Id gnat_temp;
6361 tree result = boolean_false_node;
6362 tree this_test, low = 0, high = 0, single = 0;
6364 for (choice = First (choices); Present (choice); choice = Next (choice))
6366 switch (Nkind (choice))
6368 case N_Range:
6369 low = gnat_to_gnu (Low_Bound (choice));
6370 high = gnat_to_gnu (High_Bound (choice));
6372 this_test
6373 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6374 build_binary_op (GE_EXPR, boolean_type_node,
6375 operand, low),
6376 build_binary_op (LE_EXPR, boolean_type_node,
6377 operand, high));
6379 break;
6381 case N_Subtype_Indication:
6382 gnat_temp = Range_Expression (Constraint (choice));
6383 low = gnat_to_gnu (Low_Bound (gnat_temp));
6384 high = gnat_to_gnu (High_Bound (gnat_temp));
6386 this_test
6387 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6388 build_binary_op (GE_EXPR, boolean_type_node,
6389 operand, low),
6390 build_binary_op (LE_EXPR, boolean_type_node,
6391 operand, high));
6392 break;
6394 case N_Identifier:
6395 case N_Expanded_Name:
6396 /* This represents either a subtype range, an enumeration
6397 literal, or a constant Ekind says which. If an enumeration
6398 literal or constant, fall through to the next case. */
6399 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6400 && Ekind (Entity (choice)) != E_Constant)
6402 tree type = gnat_to_gnu_type (Entity (choice));
6404 low = TYPE_MIN_VALUE (type);
6405 high = TYPE_MAX_VALUE (type);
6407 this_test
6408 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6409 build_binary_op (GE_EXPR, boolean_type_node,
6410 operand, low),
6411 build_binary_op (LE_EXPR, boolean_type_node,
6412 operand, high));
6413 break;
6416 /* ... fall through ... */
6418 case N_Character_Literal:
6419 case N_Integer_Literal:
6420 single = gnat_to_gnu (choice);
6421 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6422 single);
6423 break;
6425 case N_Others_Choice:
6426 this_test = boolean_true_node;
6427 break;
6429 default:
6430 gcc_unreachable ();
6433 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6434 this_test);
6437 return result;
6440 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6441 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6443 static int
6444 adjust_packed (tree field_type, tree record_type, int packed)
6446 /* If the field contains an item of variable size, we cannot pack it
6447 because we cannot create temporaries of non-fixed size in case
6448 we need to take the address of the field. See addressable_p and
6449 the notes on the addressability issues for further details. */
6450 if (type_has_variable_size (field_type))
6451 return 0;
6453 /* If the alignment of the record is specified and the field type
6454 is over-aligned, request Storage_Unit alignment for the field. */
6455 if (packed == -2)
6457 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6458 return -1;
6459 else
6460 return 0;
6463 return packed;
6466 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6467 placed in GNU_RECORD_TYPE.
6469 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6470 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6471 record has a specified alignment.
6473 DEFINITION is true if this field is for a record being defined.
6475 DEBUG_INFO_P is true if we need to write debug information for types
6476 that we may create in the process. */
6478 static tree
6479 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6480 bool definition, bool debug_info_p)
6482 const Entity_Id gnat_field_type = Etype (gnat_field);
6483 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6484 tree gnu_field_id = get_entity_name (gnat_field);
6485 tree gnu_field, gnu_size, gnu_pos;
6486 bool is_volatile
6487 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6488 bool needs_strict_alignment
6489 = (is_volatile
6490 || Is_Aliased (gnat_field)
6491 || Strict_Alignment (gnat_field_type));
6493 /* If this field requires strict alignment, we cannot pack it because
6494 it would very likely be under-aligned in the record. */
6495 if (needs_strict_alignment)
6496 packed = 0;
6497 else
6498 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6500 /* If a size is specified, use it. Otherwise, if the record type is packed,
6501 use the official RM size. See "Handling of Type'Size Values" in Einfo
6502 for further details. */
6503 if (Known_Esize (gnat_field))
6504 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6505 gnat_field, FIELD_DECL, false, true);
6506 else if (packed == 1)
6507 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6508 gnat_field, FIELD_DECL, false, true);
6509 else
6510 gnu_size = NULL_TREE;
6512 /* If we have a specified size that is smaller than that of the field's type,
6513 or a position is specified, and the field's type is a record that doesn't
6514 require strict alignment, see if we can get either an integral mode form
6515 of the type or a smaller form. If we can, show a size was specified for
6516 the field if there wasn't one already, so we know to make this a bitfield
6517 and avoid making things wider.
6519 Changing to an integral mode form is useful when the record is packed as
6520 we can then place the field at a non-byte-aligned position and so achieve
6521 tighter packing. This is in addition required if the field shares a byte
6522 with another field and the front-end lets the back-end handle the access
6523 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6525 Changing to a smaller form is required if the specified size is smaller
6526 than that of the field's type and the type contains sub-fields that are
6527 padded, in order to avoid generating accesses to these sub-fields that
6528 are wider than the field.
6530 We avoid the transformation if it is not required or potentially useful,
6531 as it might entail an increase of the field's alignment and have ripple
6532 effects on the outer record type. A typical case is a field known to be
6533 byte-aligned and not to share a byte with another field. */
6534 if (!needs_strict_alignment
6535 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6536 && !TYPE_FAT_POINTER_P (gnu_field_type)
6537 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6538 && (packed == 1
6539 || (gnu_size
6540 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6541 || (Present (Component_Clause (gnat_field))
6542 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6543 % BITS_PER_UNIT == 0
6544 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6546 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6547 if (gnu_packable_type != gnu_field_type)
6549 gnu_field_type = gnu_packable_type;
6550 if (!gnu_size)
6551 gnu_size = rm_size (gnu_field_type);
6555 if (Is_Atomic (gnat_field))
6556 check_ok_for_atomic (gnu_field_type, gnat_field, false);
6558 if (Present (Component_Clause (gnat_field)))
6560 Entity_Id gnat_parent
6561 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6563 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6564 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6565 gnat_field, FIELD_DECL, false, true);
6567 /* Ensure the position does not overlap with the parent subtype, if there
6568 is one. This test is omitted if the parent of the tagged type has a
6569 full rep clause since, in this case, component clauses are allowed to
6570 overlay the space allocated for the parent type and the front-end has
6571 checked that there are no overlapping components. */
6572 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6574 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6576 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6577 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6579 post_error_ne_tree
6580 ("offset of& must be beyond parent{, minimum allowed is ^}",
6581 First_Bit (Component_Clause (gnat_field)), gnat_field,
6582 TYPE_SIZE_UNIT (gnu_parent));
6586 /* If this field needs strict alignment, check that the record is
6587 sufficiently aligned and that position and size are consistent with
6588 the alignment. But don't do it if we are just annotating types and
6589 the field's type is tagged, since tagged types aren't fully laid out
6590 in this mode. Also, note that atomic implies volatile so the inner
6591 test sequences ordering is significant here. */
6592 if (needs_strict_alignment
6593 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6595 TYPE_ALIGN (gnu_record_type)
6596 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6598 if (gnu_size
6599 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6601 if (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type))
6602 post_error_ne_tree
6603 ("atomic field& must be natural size of type{ (^)}",
6604 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6605 TYPE_SIZE (gnu_field_type));
6607 else if (is_volatile)
6608 post_error_ne_tree
6609 ("volatile field& must be natural size of type{ (^)}",
6610 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6611 TYPE_SIZE (gnu_field_type));
6613 else if (Is_Aliased (gnat_field))
6614 post_error_ne_tree
6615 ("size of aliased field& must be ^ bits",
6616 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6617 TYPE_SIZE (gnu_field_type));
6619 else if (Strict_Alignment (gnat_field_type))
6620 post_error_ne_tree
6621 ("size of & with aliased or tagged components not ^ bits",
6622 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6623 TYPE_SIZE (gnu_field_type));
6625 else
6626 gcc_unreachable ();
6628 gnu_size = NULL_TREE;
6631 if (!integer_zerop (size_binop
6632 (TRUNC_MOD_EXPR, gnu_pos,
6633 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6635 if (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type))
6636 post_error_ne_num
6637 ("position of atomic field& must be multiple of ^ bits",
6638 First_Bit (Component_Clause (gnat_field)), gnat_field,
6639 TYPE_ALIGN (gnu_field_type));
6641 else if (is_volatile)
6642 post_error_ne_num
6643 ("position of volatile field& must be multiple of ^ bits",
6644 First_Bit (Component_Clause (gnat_field)), gnat_field,
6645 TYPE_ALIGN (gnu_field_type));
6647 else if (Is_Aliased (gnat_field))
6648 post_error_ne_num
6649 ("position of aliased field& must be multiple of ^ bits",
6650 First_Bit (Component_Clause (gnat_field)), gnat_field,
6651 TYPE_ALIGN (gnu_field_type));
6653 else if (Strict_Alignment (gnat_field_type))
6654 post_error_ne
6655 ("position of & is not compatible with alignment required "
6656 "by its components",
6657 First_Bit (Component_Clause (gnat_field)), gnat_field);
6659 else
6660 gcc_unreachable ();
6662 gnu_pos = NULL_TREE;
6667 /* If the record has rep clauses and this is the tag field, make a rep
6668 clause for it as well. */
6669 else if (Has_Specified_Layout (Scope (gnat_field))
6670 && Chars (gnat_field) == Name_uTag)
6672 gnu_pos = bitsize_zero_node;
6673 gnu_size = TYPE_SIZE (gnu_field_type);
6676 else
6678 gnu_pos = NULL_TREE;
6680 /* If we are packing the record and the field is BLKmode, round the
6681 size up to a byte boundary. */
6682 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6683 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6686 /* We need to make the size the maximum for the type if it is
6687 self-referential and an unconstrained type. In that case, we can't
6688 pack the field since we can't make a copy to align it. */
6689 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6690 && !gnu_size
6691 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6692 && !Is_Constrained (Underlying_Type (gnat_field_type)))
6694 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6695 packed = 0;
6698 /* If a size is specified, adjust the field's type to it. */
6699 if (gnu_size)
6701 tree orig_field_type;
6703 /* If the field's type is justified modular, we would need to remove
6704 the wrapper to (better) meet the layout requirements. However we
6705 can do so only if the field is not aliased to preserve the unique
6706 layout and if the prescribed size is not greater than that of the
6707 packed array to preserve the justification. */
6708 if (!needs_strict_alignment
6709 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6710 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6711 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6712 <= 0)
6713 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6715 /* Similarly if the field's type is a misaligned integral type, but
6716 there is no restriction on the size as there is no justification. */
6717 if (!needs_strict_alignment
6718 && TYPE_IS_PADDING_P (gnu_field_type)
6719 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
6720 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6722 gnu_field_type
6723 = make_type_from_size (gnu_field_type, gnu_size,
6724 Has_Biased_Representation (gnat_field));
6726 orig_field_type = gnu_field_type;
6727 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6728 false, false, definition, true);
6730 /* If a padding record was made, declare it now since it will never be
6731 declared otherwise. This is necessary to ensure that its subtrees
6732 are properly marked. */
6733 if (gnu_field_type != orig_field_type
6734 && !DECL_P (TYPE_NAME (gnu_field_type)))
6735 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
6736 debug_info_p, gnat_field);
6739 /* Otherwise (or if there was an error), don't specify a position. */
6740 else
6741 gnu_pos = NULL_TREE;
6743 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6744 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6746 /* Now create the decl for the field. */
6747 gnu_field
6748 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6749 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6750 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6751 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
6752 TREE_THIS_VOLATILE (gnu_field) = TREE_SIDE_EFFECTS (gnu_field) = is_volatile;
6754 if (Ekind (gnat_field) == E_Discriminant)
6755 DECL_DISCRIMINANT_NUMBER (gnu_field)
6756 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6758 return gnu_field;
6761 /* Return true if at least one member of COMPONENT_LIST needs strict
6762 alignment. */
6764 static bool
6765 components_need_strict_alignment (Node_Id component_list)
6767 Node_Id component_decl;
6769 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6770 Present (component_decl);
6771 component_decl = Next_Non_Pragma (component_decl))
6773 Entity_Id gnat_field = Defining_Entity (component_decl);
6775 if (Is_Aliased (gnat_field))
6776 return true;
6778 if (Strict_Alignment (Etype (gnat_field)))
6779 return true;
6782 return false;
6785 /* Return true if TYPE is a type with variable size or a padding type with a
6786 field of variable size or a record that has a field with such a type. */
6788 static bool
6789 type_has_variable_size (tree type)
6791 tree field;
6793 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6794 return true;
6796 if (TYPE_IS_PADDING_P (type)
6797 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6798 return true;
6800 if (!RECORD_OR_UNION_TYPE_P (type))
6801 return false;
6803 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6804 if (type_has_variable_size (TREE_TYPE (field)))
6805 return true;
6807 return false;
6810 /* Return true if FIELD is an artificial field. */
6812 static bool
6813 field_is_artificial (tree field)
6815 /* These fields are generated by the front-end proper. */
6816 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
6817 return true;
6819 /* These fields are generated by gigi. */
6820 if (DECL_INTERNAL_P (field))
6821 return true;
6823 return false;
6826 /* Return true if FIELD is a non-artificial aliased field. */
6828 static bool
6829 field_is_aliased (tree field)
6831 if (field_is_artificial (field))
6832 return false;
6834 return DECL_ALIASED_P (field);
6837 /* Return true if FIELD is a non-artificial field with self-referential
6838 size. */
6840 static bool
6841 field_has_self_size (tree field)
6843 if (field_is_artificial (field))
6844 return false;
6846 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6847 return false;
6849 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
6852 /* Return true if FIELD is a non-artificial field with variable size. */
6854 static bool
6855 field_has_variable_size (tree field)
6857 if (field_is_artificial (field))
6858 return false;
6860 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6861 return false;
6863 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
6866 /* qsort comparer for the bit positions of two record components. */
6868 static int
6869 compare_field_bitpos (const PTR rt1, const PTR rt2)
6871 const_tree const field1 = * (const_tree const *) rt1;
6872 const_tree const field2 = * (const_tree const *) rt2;
6873 const int ret
6874 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6876 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6879 /* Structure holding information for a given variant. */
6880 typedef struct vinfo
6882 /* The record type of the variant. */
6883 tree type;
6885 /* The name of the variant. */
6886 tree name;
6888 /* The qualifier of the variant. */
6889 tree qual;
6891 /* Whether the variant has a rep clause. */
6892 bool has_rep;
6894 /* Whether the variant is packed. */
6895 bool packed;
6897 } vinfo_t;
6899 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
6900 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
6901 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
6902 When called from gnat_to_gnu_entity during the processing of a record type
6903 definition, the GCC node for the parent, if any, will be the single field
6904 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6905 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6906 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6908 PACKED is 1 if this is for a packed record, -1 if this is for a record
6909 with Component_Alignment of Storage_Unit, -2 if this is for a record
6910 with a specified alignment.
6912 DEFINITION is true if we are defining this record type.
6914 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6915 out the record. This means the alignment only serves to force fields to
6916 be bitfields, but not to require the record to be that aligned. This is
6917 used for variants.
6919 ALL_REP is true if a rep clause is present for all the fields.
6921 UNCHECKED_UNION is true if we are building this type for a record with a
6922 Pragma Unchecked_Union.
6924 ARTIFICIAL is true if this is a type that was generated by the compiler.
6926 DEBUG_INFO is true if we need to write debug information about the type.
6928 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6929 mean that its contents may be unused as well, only the container itself.
6931 REORDER is true if we are permitted to reorder components of this type.
6933 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6934 the outer record type down to this variant level. It is nonzero only if
6935 all the fields down to this level have a rep clause and ALL_REP is false.
6937 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6938 with a rep clause is to be added; in this case, that is all that should
6939 be done with such fields and the return value will be false. */
6941 static bool
6942 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6943 tree gnu_field_list, int packed, bool definition,
6944 bool cancel_alignment, bool all_rep,
6945 bool unchecked_union, bool artificial,
6946 bool debug_info, bool maybe_unused, bool reorder,
6947 tree first_free_pos, tree *p_gnu_rep_list)
6949 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6950 bool variants_have_rep = all_rep;
6951 bool layout_with_rep = false;
6952 bool has_self_field = false;
6953 bool has_aliased_after_self_field = false;
6954 Node_Id component_decl, variant_part;
6955 tree gnu_field, gnu_next, gnu_last;
6956 tree gnu_variant_part = NULL_TREE;
6957 tree gnu_rep_list = NULL_TREE;
6958 tree gnu_var_list = NULL_TREE;
6959 tree gnu_self_list = NULL_TREE;
6960 tree gnu_zero_list = NULL_TREE;
6962 /* For each component referenced in a component declaration create a GCC
6963 field and add it to the list, skipping pragmas in the GNAT list. */
6964 gnu_last = tree_last (gnu_field_list);
6965 if (Present (Component_Items (gnat_component_list)))
6966 for (component_decl
6967 = First_Non_Pragma (Component_Items (gnat_component_list));
6968 Present (component_decl);
6969 component_decl = Next_Non_Pragma (component_decl))
6971 Entity_Id gnat_field = Defining_Entity (component_decl);
6972 Name_Id gnat_name = Chars (gnat_field);
6974 /* If present, the _Parent field must have been created as the single
6975 field of the record type. Put it before any other fields. */
6976 if (gnat_name == Name_uParent)
6978 gnu_field = TYPE_FIELDS (gnu_record_type);
6979 gnu_field_list = chainon (gnu_field_list, gnu_field);
6981 else
6983 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
6984 definition, debug_info);
6986 /* If this is the _Tag field, put it before any other fields. */
6987 if (gnat_name == Name_uTag)
6988 gnu_field_list = chainon (gnu_field_list, gnu_field);
6990 /* If this is the _Controller field, put it before the other
6991 fields except for the _Tag or _Parent field. */
6992 else if (gnat_name == Name_uController && gnu_last)
6994 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
6995 DECL_CHAIN (gnu_last) = gnu_field;
6998 /* If this is a regular field, put it after the other fields. */
6999 else
7001 DECL_CHAIN (gnu_field) = gnu_field_list;
7002 gnu_field_list = gnu_field;
7003 if (!gnu_last)
7004 gnu_last = gnu_field;
7006 /* And record information for the final layout. */
7007 if (field_has_self_size (gnu_field))
7008 has_self_field = true;
7009 else if (has_self_field && field_is_aliased (gnu_field))
7010 has_aliased_after_self_field = true;
7014 save_gnu_tree (gnat_field, gnu_field, false);
7017 /* At the end of the component list there may be a variant part. */
7018 variant_part = Variant_Part (gnat_component_list);
7020 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7021 mutually exclusive and should go in the same memory. To do this we need
7022 to treat each variant as a record whose elements are created from the
7023 component list for the variant. So here we create the records from the
7024 lists for the variants and put them all into the QUAL_UNION_TYPE.
7025 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7026 use GNU_RECORD_TYPE if there are no fields so far. */
7027 if (Present (variant_part))
7029 Node_Id gnat_discr = Name (variant_part), variant;
7030 tree gnu_discr = gnat_to_gnu (gnat_discr);
7031 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
7032 tree gnu_var_name
7033 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7034 "XVN");
7035 tree gnu_union_type, gnu_union_name;
7036 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7037 bool union_field_needs_strict_alignment = false;
7038 auto_vec <vinfo_t, 16> variant_types;
7039 vinfo_t *gnu_variant;
7040 unsigned int variants_align = 0;
7041 unsigned int i;
7043 gnu_union_name
7044 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7046 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7047 are all in the variant part, to match the layout of C unions. There
7048 is an associated check below. */
7049 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7050 gnu_union_type = gnu_record_type;
7051 else
7053 gnu_union_type
7054 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7056 TYPE_NAME (gnu_union_type) = gnu_union_name;
7057 TYPE_ALIGN (gnu_union_type) = 0;
7058 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7061 /* If all the fields down to this level have a rep clause, find out
7062 whether all the fields at this level also have one. If so, then
7063 compute the new first free position to be passed downward. */
7064 this_first_free_pos = first_free_pos;
7065 if (this_first_free_pos)
7067 for (gnu_field = gnu_field_list;
7068 gnu_field;
7069 gnu_field = DECL_CHAIN (gnu_field))
7070 if (DECL_FIELD_OFFSET (gnu_field))
7072 tree pos = bit_position (gnu_field);
7073 if (!tree_int_cst_lt (pos, this_first_free_pos))
7074 this_first_free_pos
7075 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7077 else
7079 this_first_free_pos = NULL_TREE;
7080 break;
7084 /* We build the variants in two passes. The bulk of the work is done in
7085 the first pass, that is to say translating the GNAT nodes, building
7086 the container types and computing the associated properties. However
7087 we cannot finish up the container types during this pass because we
7088 don't know where the variant part will be placed until the end. */
7089 for (variant = First_Non_Pragma (Variants (variant_part));
7090 Present (variant);
7091 variant = Next_Non_Pragma (variant))
7093 tree gnu_variant_type = make_node (RECORD_TYPE);
7094 tree gnu_inner_name, gnu_qual;
7095 bool has_rep;
7096 int field_packed;
7097 vinfo_t vinfo;
7099 Get_Variant_Encoding (variant);
7100 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7101 TYPE_NAME (gnu_variant_type)
7102 = concat_name (gnu_union_name,
7103 IDENTIFIER_POINTER (gnu_inner_name));
7105 /* Set the alignment of the inner type in case we need to make
7106 inner objects into bitfields, but then clear it out so the
7107 record actually gets only the alignment required. */
7108 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7109 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7111 /* Similarly, if the outer record has a size specified and all
7112 the fields have a rep clause, we can propagate the size. */
7113 if (all_rep_and_size)
7115 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7116 TYPE_SIZE_UNIT (gnu_variant_type)
7117 = TYPE_SIZE_UNIT (gnu_record_type);
7120 /* Add the fields into the record type for the variant. Note that
7121 we aren't sure to really use it at this point, see below. */
7122 has_rep
7123 = components_to_record (gnu_variant_type, Component_List (variant),
7124 NULL_TREE, packed, definition,
7125 !all_rep_and_size, all_rep,
7126 unchecked_union,
7127 true, debug_info, true, reorder,
7128 this_first_free_pos,
7129 all_rep || this_first_free_pos
7130 ? NULL : &gnu_rep_list);
7132 /* Translate the qualifier and annotate the GNAT node. */
7133 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7134 Set_Present_Expr (variant, annotate_value (gnu_qual));
7136 /* Deal with packedness like in gnat_to_gnu_field. */
7137 if (components_need_strict_alignment (Component_List (variant)))
7139 field_packed = 0;
7140 union_field_needs_strict_alignment = true;
7142 else
7143 field_packed
7144 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7146 /* Push this variant onto the stack for the second pass. */
7147 vinfo.type = gnu_variant_type;
7148 vinfo.name = gnu_inner_name;
7149 vinfo.qual = gnu_qual;
7150 vinfo.has_rep = has_rep;
7151 vinfo.packed = field_packed;
7152 variant_types.safe_push (vinfo);
7154 /* Compute the global properties that will determine the placement of
7155 the variant part. */
7156 variants_have_rep |= has_rep;
7157 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7158 variants_align = TYPE_ALIGN (gnu_variant_type);
7161 /* Round up the first free position to the alignment of the variant part
7162 for the variants without rep clause. This will guarantee a consistent
7163 layout independently of the placement of the variant part. */
7164 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7165 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7167 /* In the second pass, the container types are adjusted if necessary and
7168 finished up, then the corresponding fields of the variant part are
7169 built with their qualifier, unless this is an unchecked union. */
7170 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7172 tree gnu_variant_type = gnu_variant->type;
7173 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7175 /* If this is an Unchecked_Union whose fields are all in the variant
7176 part and we have a single field with no representation clause or
7177 placed at offset zero, use the field directly to match the layout
7178 of C unions. */
7179 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7180 && gnu_field_list
7181 && !DECL_CHAIN (gnu_field_list)
7182 && (!DECL_FIELD_OFFSET (gnu_field_list)
7183 || integer_zerop (bit_position (gnu_field_list))))
7185 gnu_field = gnu_field_list;
7186 DECL_CONTEXT (gnu_field) = gnu_record_type;
7188 else
7190 /* Finalize the variant type now. We used to throw away empty
7191 record types but we no longer do that because we need them to
7192 generate complete debug info for the variant; otherwise, the
7193 union type definition will be lacking the fields associated
7194 with these empty variants. */
7195 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7197 /* The variant part will be at offset 0 so we need to ensure
7198 that the fields are laid out starting from the first free
7199 position at this level. */
7200 tree gnu_rep_type = make_node (RECORD_TYPE);
7201 tree gnu_rep_part;
7202 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7203 gnu_rep_part
7204 = create_rep_part (gnu_rep_type, gnu_variant_type,
7205 this_first_free_pos);
7206 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7207 gnu_field_list = gnu_rep_part;
7208 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7209 false);
7212 if (debug_info)
7213 rest_of_record_type_compilation (gnu_variant_type);
7214 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7215 true, debug_info, gnat_component_list);
7217 gnu_field
7218 = create_field_decl (gnu_variant->name, gnu_variant_type,
7219 gnu_union_type,
7220 all_rep_and_size
7221 ? TYPE_SIZE (gnu_variant_type) : 0,
7222 variants_have_rep ? bitsize_zero_node : 0,
7223 gnu_variant->packed, 0);
7225 DECL_INTERNAL_P (gnu_field) = 1;
7227 if (!unchecked_union)
7228 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7231 DECL_CHAIN (gnu_field) = gnu_variant_list;
7232 gnu_variant_list = gnu_field;
7235 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7236 if (gnu_variant_list)
7238 int union_field_packed;
7240 if (all_rep_and_size)
7242 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7243 TYPE_SIZE_UNIT (gnu_union_type)
7244 = TYPE_SIZE_UNIT (gnu_record_type);
7247 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7248 all_rep_and_size ? 1 : 0, debug_info);
7250 /* If GNU_UNION_TYPE is our record type, it means we must have an
7251 Unchecked_Union with no fields. Verify that and, if so, just
7252 return. */
7253 if (gnu_union_type == gnu_record_type)
7255 gcc_assert (unchecked_union
7256 && !gnu_field_list
7257 && !gnu_rep_list);
7258 return variants_have_rep;
7261 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7262 debug_info, gnat_component_list);
7264 /* Deal with packedness like in gnat_to_gnu_field. */
7265 if (union_field_needs_strict_alignment)
7266 union_field_packed = 0;
7267 else
7268 union_field_packed
7269 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7271 gnu_variant_part
7272 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7273 all_rep_and_size
7274 ? TYPE_SIZE (gnu_union_type) : 0,
7275 variants_have_rep ? bitsize_zero_node : 0,
7276 union_field_packed, 0);
7278 DECL_INTERNAL_P (gnu_variant_part) = 1;
7282 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7283 permitted to reorder components, self-referential sizes or variable sizes.
7284 If they do, pull them out and put them onto the appropriate list. We have
7285 to do this in a separate pass since we want to handle the discriminants
7286 but can't play with them until we've used them in debugging data above.
7288 Similarly, pull out the fields with zero size and no rep clause, as they
7289 would otherwise modify the layout and thus very likely run afoul of the
7290 Ada semantics, which are different from those of C here.
7292 ??? If we reorder them, debugging information will be wrong but there is
7293 nothing that can be done about this at the moment. */
7294 gnu_last = NULL_TREE;
7296 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7297 do { \
7298 if (gnu_last) \
7299 DECL_CHAIN (gnu_last) = gnu_next; \
7300 else \
7301 gnu_field_list = gnu_next; \
7303 DECL_CHAIN (gnu_field) = (LIST); \
7304 (LIST) = gnu_field; \
7305 } while (0)
7307 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7309 gnu_next = DECL_CHAIN (gnu_field);
7311 if (DECL_FIELD_OFFSET (gnu_field))
7313 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7314 continue;
7317 if ((reorder || has_aliased_after_self_field)
7318 && field_has_self_size (gnu_field))
7320 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7321 continue;
7324 if (reorder && field_has_variable_size (gnu_field))
7326 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7327 continue;
7330 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7332 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7333 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7334 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7335 if (field_is_aliased (gnu_field))
7336 TYPE_ALIGN (gnu_record_type)
7337 = MAX (TYPE_ALIGN (gnu_record_type),
7338 TYPE_ALIGN (TREE_TYPE (gnu_field)));
7339 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7340 continue;
7343 gnu_last = gnu_field;
7346 #undef MOVE_FROM_FIELD_LIST_TO
7348 gnu_field_list = nreverse (gnu_field_list);
7350 /* If permitted, we reorder the fields as follows:
7352 1) all fixed length fields,
7353 2) all fields whose length doesn't depend on discriminants,
7354 3) all fields whose length depends on discriminants,
7355 4) the variant part,
7357 within the record and within each variant recursively. */
7358 if (reorder)
7359 gnu_field_list
7360 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7362 /* Otherwise, if there is an aliased field placed after a field whose length
7363 depends on discriminants, we put all the fields of the latter sort, last.
7364 We need to do this in case an object of this record type is mutable. */
7365 else if (has_aliased_after_self_field)
7366 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7368 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7369 in our REP list to the previous level because this level needs them in
7370 order to do a correct layout, i.e. avoid having overlapping fields. */
7371 if (p_gnu_rep_list && gnu_rep_list)
7372 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7374 /* Otherwise, sort the fields by bit position and put them into their own
7375 record, before the others, if we also have fields without rep clause. */
7376 else if (gnu_rep_list)
7378 tree gnu_rep_type, gnu_rep_part;
7379 int i, len = list_length (gnu_rep_list);
7380 tree *gnu_arr = XALLOCAVEC (tree, len);
7382 /* If all the fields have a rep clause, we can do a flat layout. */
7383 layout_with_rep = !gnu_field_list
7384 && (!gnu_variant_part || variants_have_rep);
7385 gnu_rep_type
7386 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7388 for (gnu_field = gnu_rep_list, i = 0;
7389 gnu_field;
7390 gnu_field = DECL_CHAIN (gnu_field), i++)
7391 gnu_arr[i] = gnu_field;
7393 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7395 /* Put the fields in the list in order of increasing position, which
7396 means we start from the end. */
7397 gnu_rep_list = NULL_TREE;
7398 for (i = len - 1; i >= 0; i--)
7400 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7401 gnu_rep_list = gnu_arr[i];
7402 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7405 if (layout_with_rep)
7406 gnu_field_list = gnu_rep_list;
7407 else
7409 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7411 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7412 without rep clause are laid out starting from this position.
7413 Therefore, we force it as a minimal size on the REP part. */
7414 gnu_rep_part
7415 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7417 /* Chain the REP part at the beginning of the field list. */
7418 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7419 gnu_field_list = gnu_rep_part;
7423 /* Chain the variant part at the end of the field list. */
7424 if (gnu_variant_part)
7425 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7427 if (cancel_alignment)
7428 TYPE_ALIGN (gnu_record_type) = 0;
7430 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7432 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7433 debug_info && !maybe_unused);
7435 /* Chain the fields with zero size at the beginning of the field list. */
7436 if (gnu_zero_list)
7437 TYPE_FIELDS (gnu_record_type)
7438 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7440 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7443 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7444 placed into an Esize, Component_Bit_Offset, or Component_Size value
7445 in the GNAT tree. */
7447 static Uint
7448 annotate_value (tree gnu_size)
7450 TCode tcode;
7451 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7452 struct tree_int_map in;
7453 int i;
7455 /* See if we've already saved the value for this node. */
7456 if (EXPR_P (gnu_size))
7458 struct tree_int_map *e;
7460 in.base.from = gnu_size;
7461 e = (struct tree_int_map *) htab_find (annotate_value_cache, &in);
7463 if (e)
7464 return (Node_Ref_Or_Val) e->to;
7466 else
7467 in.base.from = NULL_TREE;
7469 /* If we do not return inside this switch, TCODE will be set to the
7470 code to use for a Create_Node operand and LEN (set above) will be
7471 the number of recursive calls for us to make. */
7473 switch (TREE_CODE (gnu_size))
7475 case INTEGER_CST:
7476 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7478 case COMPONENT_REF:
7479 /* The only case we handle here is a simple discriminant reference. */
7480 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7482 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7484 /* Climb up the chain of successive extensions, if any. */
7485 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7486 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7487 == parent_name_id)
7488 gnu_size = TREE_OPERAND (gnu_size, 0);
7490 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7491 return
7492 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7495 return No_Uint;
7497 CASE_CONVERT: case NON_LVALUE_EXPR:
7498 return annotate_value (TREE_OPERAND (gnu_size, 0));
7500 /* Now just list the operations we handle. */
7501 case COND_EXPR: tcode = Cond_Expr; break;
7502 case PLUS_EXPR: tcode = Plus_Expr; break;
7503 case MINUS_EXPR: tcode = Minus_Expr; break;
7504 case MULT_EXPR: tcode = Mult_Expr; break;
7505 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7506 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7507 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7508 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7509 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7510 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7511 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7512 case NEGATE_EXPR: tcode = Negate_Expr; break;
7513 case MIN_EXPR: tcode = Min_Expr; break;
7514 case MAX_EXPR: tcode = Max_Expr; break;
7515 case ABS_EXPR: tcode = Abs_Expr; break;
7516 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7517 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7518 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7519 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7520 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7521 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7522 case LT_EXPR: tcode = Lt_Expr; break;
7523 case LE_EXPR: tcode = Le_Expr; break;
7524 case GT_EXPR: tcode = Gt_Expr; break;
7525 case GE_EXPR: tcode = Ge_Expr; break;
7526 case EQ_EXPR: tcode = Eq_Expr; break;
7527 case NE_EXPR: tcode = Ne_Expr; break;
7529 case BIT_AND_EXPR:
7530 tcode = Bit_And_Expr;
7531 /* For negative values, build NEGATE_EXPR of the opposite. Such values
7532 appear in expressions containing aligning patterns. Note that, since
7533 sizetype is unsigned, we have to jump through some hoops. */
7534 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7536 tree op1 = TREE_OPERAND (gnu_size, 1);
7537 double_int signed_op1
7538 = tree_to_double_int (op1).sext (TYPE_PRECISION (sizetype));
7539 if (signed_op1.is_negative ())
7541 op1 = double_int_to_tree (sizetype, -signed_op1);
7542 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7545 break;
7547 case CALL_EXPR:
7548 /* In regular mode, inline back only if symbolic annotation is requested
7549 in order to avoid memory explosion on big discriminated record types.
7550 But not in ASIS mode, as symbolic annotation is required for DDA. */
7551 if (List_Representation_Info == 3 || type_annotate_only)
7553 tree t = maybe_inline_call_in_expr (gnu_size);
7554 if (t)
7555 return annotate_value (t);
7557 else
7558 return Uint_Minus_1;
7560 /* Fall through... */
7562 default:
7563 return No_Uint;
7566 /* Now get each of the operands that's relevant for this code. If any
7567 cannot be expressed as a repinfo node, say we can't. */
7568 for (i = 0; i < 3; i++)
7569 ops[i] = No_Uint;
7571 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7573 if (i == 1 && pre_op1 != No_Uint)
7574 ops[i] = pre_op1;
7575 else
7576 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7577 if (ops[i] == No_Uint)
7578 return No_Uint;
7581 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7583 /* Save the result in the cache. */
7584 if (in.base.from)
7586 struct tree_int_map **h;
7587 /* We can't assume the hash table data hasn't moved since the initial
7588 look up, so we have to search again. Allocating and inserting an
7589 entry at that point would be an alternative, but then we'd better
7590 discard the entry if we decided not to cache it. */
7591 h = (struct tree_int_map **)
7592 htab_find_slot (annotate_value_cache, &in, INSERT);
7593 gcc_assert (!*h);
7594 *h = ggc_alloc_tree_int_map ();
7595 (*h)->base.from = gnu_size;
7596 (*h)->to = ret;
7599 return ret;
7602 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7603 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7604 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7605 BY_REF is true if the object is used by reference. */
7607 void
7608 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7610 if (by_ref)
7612 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7613 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7614 else
7615 gnu_type = TREE_TYPE (gnu_type);
7618 if (Unknown_Esize (gnat_entity))
7620 if (TREE_CODE (gnu_type) == RECORD_TYPE
7621 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7622 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7623 else if (!size)
7624 size = TYPE_SIZE (gnu_type);
7626 if (size)
7627 Set_Esize (gnat_entity, annotate_value (size));
7630 if (Unknown_Alignment (gnat_entity))
7631 Set_Alignment (gnat_entity,
7632 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7635 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7636 Return NULL_TREE if there is no such element in the list. */
7638 static tree
7639 purpose_member_field (const_tree elem, tree list)
7641 while (list)
7643 tree field = TREE_PURPOSE (list);
7644 if (SAME_FIELD_P (field, elem))
7645 return list;
7646 list = TREE_CHAIN (list);
7648 return NULL_TREE;
7651 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7652 set Component_Bit_Offset and Esize of the components to the position and
7653 size used by Gigi. */
7655 static void
7656 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7658 Entity_Id gnat_field;
7659 tree gnu_list;
7661 /* We operate by first making a list of all fields and their position (we
7662 can get the size easily) and then update all the sizes in the tree. */
7663 gnu_list
7664 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7665 BIGGEST_ALIGNMENT, NULL_TREE);
7667 for (gnat_field = First_Entity (gnat_entity);
7668 Present (gnat_field);
7669 gnat_field = Next_Entity (gnat_field))
7670 if (Ekind (gnat_field) == E_Component
7671 || (Ekind (gnat_field) == E_Discriminant
7672 && !Is_Unchecked_Union (Scope (gnat_field))))
7674 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7675 gnu_list);
7676 if (t)
7678 tree parent_offset;
7680 /* If we are just annotating types and the type is tagged, the tag
7681 and the parent components are not generated by the front-end so
7682 we need to add the appropriate offset to each component without
7683 representation clause. */
7684 if (type_annotate_only
7685 && Is_Tagged_Type (gnat_entity)
7686 && No (Component_Clause (gnat_field)))
7688 /* For a component appearing in the current extension, the
7689 offset is the size of the parent. */
7690 if (Is_Derived_Type (gnat_entity)
7691 && Original_Record_Component (gnat_field) == gnat_field)
7692 parent_offset
7693 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7694 bitsizetype);
7695 else
7696 parent_offset = bitsize_int (POINTER_SIZE);
7698 if (TYPE_FIELDS (gnu_type))
7699 parent_offset
7700 = round_up (parent_offset,
7701 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
7703 else
7704 parent_offset = bitsize_zero_node;
7706 Set_Component_Bit_Offset
7707 (gnat_field,
7708 annotate_value
7709 (size_binop (PLUS_EXPR,
7710 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7711 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7712 parent_offset)));
7714 Set_Esize (gnat_field,
7715 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7717 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7719 /* If there is no entry, this is an inherited component whose
7720 position is the same as in the parent type. */
7721 Set_Component_Bit_Offset
7722 (gnat_field,
7723 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7725 Set_Esize (gnat_field,
7726 Esize (Original_Record_Component (gnat_field)));
7731 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7732 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7733 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7734 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7735 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7736 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7737 pre-existing list to be chained to the newly created entries. */
7739 static tree
7740 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7741 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7743 tree gnu_field;
7745 for (gnu_field = TYPE_FIELDS (gnu_type);
7746 gnu_field;
7747 gnu_field = DECL_CHAIN (gnu_field))
7749 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7750 DECL_FIELD_BIT_OFFSET (gnu_field));
7751 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7752 DECL_FIELD_OFFSET (gnu_field));
7753 unsigned int our_offset_align
7754 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7755 tree v = make_tree_vec (3);
7757 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7758 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7759 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7760 gnu_list = tree_cons (gnu_field, v, gnu_list);
7762 /* Recurse on internal fields, flattening the nested fields except for
7763 those in the variant part, if requested. */
7764 if (DECL_INTERNAL_P (gnu_field))
7766 tree gnu_field_type = TREE_TYPE (gnu_field);
7767 if (do_not_flatten_variant
7768 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7769 gnu_list
7770 = build_position_list (gnu_field_type, do_not_flatten_variant,
7771 size_zero_node, bitsize_zero_node,
7772 BIGGEST_ALIGNMENT, gnu_list);
7773 else
7774 gnu_list
7775 = build_position_list (gnu_field_type, do_not_flatten_variant,
7776 gnu_our_offset, gnu_our_bitpos,
7777 our_offset_align, gnu_list);
7781 return gnu_list;
7784 /* Return a list describing the substitutions needed to reflect the
7785 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7786 be in any order. The values in an element of the list are in the form
7787 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7788 a definition of GNAT_SUBTYPE. */
7790 static vec<subst_pair>
7791 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7793 vec<subst_pair> gnu_list = vNULL;
7794 Entity_Id gnat_discrim;
7795 Node_Id gnat_constr;
7797 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7798 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
7799 Present (gnat_discrim);
7800 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7801 gnat_constr = Next_Elmt (gnat_constr))
7802 /* Ignore access discriminants. */
7803 if (!Is_Access_Type (Etype (Node (gnat_constr))))
7805 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7806 tree replacement = convert (TREE_TYPE (gnu_field),
7807 elaborate_expression
7808 (Node (gnat_constr), gnat_subtype,
7809 get_entity_name (gnat_discrim),
7810 definition, true, false));
7811 subst_pair s = {gnu_field, replacement};
7812 gnu_list.safe_push (s);
7815 return gnu_list;
7818 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7819 variants of QUAL_UNION_TYPE that are still relevant after applying
7820 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
7821 list to be prepended to the newly created entries. */
7823 static vec<variant_desc>
7824 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
7825 vec<variant_desc> gnu_list)
7827 tree gnu_field;
7829 for (gnu_field = TYPE_FIELDS (qual_union_type);
7830 gnu_field;
7831 gnu_field = DECL_CHAIN (gnu_field))
7833 tree qual = DECL_QUALIFIER (gnu_field);
7834 unsigned int i;
7835 subst_pair *s;
7837 FOR_EACH_VEC_ELT (subst_list, i, s)
7838 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7840 /* If the new qualifier is not unconditionally false, its variant may
7841 still be accessed. */
7842 if (!integer_zerop (qual))
7844 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7845 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
7847 gnu_list.safe_push (v);
7849 /* Recurse on the variant subpart of the variant, if any. */
7850 variant_subpart = get_variant_part (variant_type);
7851 if (variant_subpart)
7852 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
7853 subst_list, gnu_list);
7855 /* If the new qualifier is unconditionally true, the subsequent
7856 variants cannot be accessed. */
7857 if (integer_onep (qual))
7858 break;
7862 return gnu_list;
7865 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7866 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7867 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7868 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7869 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7870 true if we are being called to process the Component_Size of GNAT_OBJECT;
7871 this is used only for error messages. ZERO_OK is true if a size of zero
7872 is permitted; if ZERO_OK is false, it means that a size of zero should be
7873 treated as an unspecified size. */
7875 static tree
7876 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7877 enum tree_code kind, bool component_p, bool zero_ok)
7879 Node_Id gnat_error_node;
7880 tree type_size, size;
7882 /* Return 0 if no size was specified. */
7883 if (uint_size == No_Uint)
7884 return NULL_TREE;
7886 /* Ignore a negative size since that corresponds to our back-annotation. */
7887 if (UI_Lt (uint_size, Uint_0))
7888 return NULL_TREE;
7890 /* Find the node to use for error messages. */
7891 if ((Ekind (gnat_object) == E_Component
7892 || Ekind (gnat_object) == E_Discriminant)
7893 && Present (Component_Clause (gnat_object)))
7894 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7895 else if (Present (Size_Clause (gnat_object)))
7896 gnat_error_node = Expression (Size_Clause (gnat_object));
7897 else
7898 gnat_error_node = gnat_object;
7900 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7901 but cannot be represented in bitsizetype. */
7902 size = UI_To_gnu (uint_size, bitsizetype);
7903 if (TREE_OVERFLOW (size))
7905 if (component_p)
7906 post_error_ne ("component size for& is too large", gnat_error_node,
7907 gnat_object);
7908 else
7909 post_error_ne ("size for& is too large", gnat_error_node,
7910 gnat_object);
7911 return NULL_TREE;
7914 /* Ignore a zero size if it is not permitted. */
7915 if (!zero_ok && integer_zerop (size))
7916 return NULL_TREE;
7918 /* The size of objects is always a multiple of a byte. */
7919 if (kind == VAR_DECL
7920 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7922 if (component_p)
7923 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7924 gnat_error_node, gnat_object);
7925 else
7926 post_error_ne ("size for& is not a multiple of Storage_Unit",
7927 gnat_error_node, gnat_object);
7928 return NULL_TREE;
7931 /* If this is an integral type or a packed array type, the front-end has
7932 already verified the size, so we need not do it here (which would mean
7933 checking against the bounds). However, if this is an aliased object,
7934 it may not be smaller than the type of the object. */
7935 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7936 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7937 return size;
7939 /* If the object is a record that contains a template, add the size of the
7940 template to the specified size. */
7941 if (TREE_CODE (gnu_type) == RECORD_TYPE
7942 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7943 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7945 if (kind == VAR_DECL
7946 /* If a type needs strict alignment, a component of this type in
7947 a packed record cannot be packed and thus uses the type size. */
7948 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7949 type_size = TYPE_SIZE (gnu_type);
7950 else
7951 type_size = rm_size (gnu_type);
7953 /* Modify the size of a discriminated type to be the maximum size. */
7954 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7955 type_size = max_size (type_size, true);
7957 /* If this is an access type or a fat pointer, the minimum size is that given
7958 by the smallest integral mode that's valid for pointers. */
7959 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7961 enum machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7962 while (!targetm.valid_pointer_mode (p_mode))
7963 p_mode = GET_MODE_WIDER_MODE (p_mode);
7964 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7967 /* Issue an error either if the default size of the object isn't a constant
7968 or if the new size is smaller than it. */
7969 if (TREE_CODE (type_size) != INTEGER_CST
7970 || TREE_OVERFLOW (type_size)
7971 || tree_int_cst_lt (size, type_size))
7973 if (component_p)
7974 post_error_ne_tree
7975 ("component size for& too small{, minimum allowed is ^}",
7976 gnat_error_node, gnat_object, type_size);
7977 else
7978 post_error_ne_tree
7979 ("size for& too small{, minimum allowed is ^}",
7980 gnat_error_node, gnat_object, type_size);
7981 return NULL_TREE;
7984 return size;
7987 /* Similarly, but both validate and process a value of RM size. This routine
7988 is only called for types. */
7990 static void
7991 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7993 Node_Id gnat_attr_node;
7994 tree old_size, size;
7996 /* Do nothing if no size was specified. */
7997 if (uint_size == No_Uint)
7998 return;
8000 /* Ignore a negative size since that corresponds to our back-annotation. */
8001 if (UI_Lt (uint_size, Uint_0))
8002 return;
8004 /* Only issue an error if a Value_Size clause was explicitly given.
8005 Otherwise, we'd be duplicating an error on the Size clause. */
8006 gnat_attr_node
8007 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
8009 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8010 but cannot be represented in bitsizetype. */
8011 size = UI_To_gnu (uint_size, bitsizetype);
8012 if (TREE_OVERFLOW (size))
8014 if (Present (gnat_attr_node))
8015 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8016 gnat_entity);
8017 return;
8020 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8021 exists, or this is an integer type, in which case the front-end will
8022 have always set it. */
8023 if (No (gnat_attr_node)
8024 && integer_zerop (size)
8025 && !Has_Size_Clause (gnat_entity)
8026 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8027 return;
8029 old_size = rm_size (gnu_type);
8031 /* If the old size is self-referential, get the maximum size. */
8032 if (CONTAINS_PLACEHOLDER_P (old_size))
8033 old_size = max_size (old_size, true);
8035 /* Issue an error either if the old size of the object isn't a constant or
8036 if the new size is smaller than it. The front-end has already verified
8037 this for scalar and packed array types. */
8038 if (TREE_CODE (old_size) != INTEGER_CST
8039 || TREE_OVERFLOW (old_size)
8040 || (AGGREGATE_TYPE_P (gnu_type)
8041 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8042 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8043 && !(TYPE_IS_PADDING_P (gnu_type)
8044 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8045 && TYPE_PACKED_ARRAY_TYPE_P
8046 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8047 && tree_int_cst_lt (size, old_size)))
8049 if (Present (gnat_attr_node))
8050 post_error_ne_tree
8051 ("Value_Size for& too small{, minimum allowed is ^}",
8052 gnat_attr_node, gnat_entity, old_size);
8053 return;
8056 /* Otherwise, set the RM size proper for integral types... */
8057 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8058 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8059 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8060 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8061 SET_TYPE_RM_SIZE (gnu_type, size);
8063 /* ...or the Ada size for record and union types. */
8064 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8065 && !TYPE_FAT_POINTER_P (gnu_type))
8066 SET_TYPE_ADA_SIZE (gnu_type, size);
8069 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8070 a type or object whose present alignment is ALIGN. If this alignment is
8071 valid, return it. Otherwise, give an error and return ALIGN. */
8073 static unsigned int
8074 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8076 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8077 unsigned int new_align;
8078 Node_Id gnat_error_node;
8080 /* Don't worry about checking alignment if alignment was not specified
8081 by the source program and we already posted an error for this entity. */
8082 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8083 return align;
8085 /* Post the error on the alignment clause if any. Note, for the implicit
8086 base type of an array type, the alignment clause is on the first
8087 subtype. */
8088 if (Present (Alignment_Clause (gnat_entity)))
8089 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8091 else if (Is_Itype (gnat_entity)
8092 && Is_Array_Type (gnat_entity)
8093 && Etype (gnat_entity) == gnat_entity
8094 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8095 gnat_error_node =
8096 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8098 else
8099 gnat_error_node = gnat_entity;
8101 /* Within GCC, an alignment is an integer, so we must make sure a value is
8102 specified that fits in that range. Also, there is an upper bound to
8103 alignments we can support/allow. */
8104 if (!UI_Is_In_Int_Range (alignment)
8105 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8106 post_error_ne_num ("largest supported alignment for& is ^",
8107 gnat_error_node, gnat_entity, max_allowed_alignment);
8108 else if (!(Present (Alignment_Clause (gnat_entity))
8109 && From_At_Mod (Alignment_Clause (gnat_entity)))
8110 && new_align * BITS_PER_UNIT < align)
8112 unsigned int double_align;
8113 bool is_capped_double, align_clause;
8115 /* If the default alignment of "double" or larger scalar types is
8116 specifically capped and the new alignment is above the cap, do
8117 not post an error and change the alignment only if there is an
8118 alignment clause; this makes it possible to have the associated
8119 GCC type overaligned by default for performance reasons. */
8120 if ((double_align = double_float_alignment) > 0)
8122 Entity_Id gnat_type
8123 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8124 is_capped_double
8125 = is_double_float_or_array (gnat_type, &align_clause);
8127 else if ((double_align = double_scalar_alignment) > 0)
8129 Entity_Id gnat_type
8130 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8131 is_capped_double
8132 = is_double_scalar_or_array (gnat_type, &align_clause);
8134 else
8135 is_capped_double = align_clause = false;
8137 if (is_capped_double && new_align >= double_align)
8139 if (align_clause)
8140 align = new_align * BITS_PER_UNIT;
8142 else
8144 if (is_capped_double)
8145 align = double_align * BITS_PER_UNIT;
8147 post_error_ne_num ("alignment for& must be at least ^",
8148 gnat_error_node, gnat_entity,
8149 align / BITS_PER_UNIT);
8152 else
8154 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8155 if (new_align > align)
8156 align = new_align;
8159 return align;
8162 /* Verify that OBJECT, a type or decl, is something we can implement
8163 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
8164 if we require atomic components. */
8166 static void
8167 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
8169 Node_Id gnat_error_point = gnat_entity;
8170 Node_Id gnat_node;
8171 enum machine_mode mode;
8172 unsigned int align;
8173 tree size;
8175 /* There are three case of what OBJECT can be. It can be a type, in which
8176 case we take the size, alignment and mode from the type. It can be a
8177 declaration that was indirect, in which case the relevant values are
8178 that of the type being pointed to, or it can be a normal declaration,
8179 in which case the values are of the decl. The code below assumes that
8180 OBJECT is either a type or a decl. */
8181 if (TYPE_P (object))
8183 /* If this is an anonymous base type, nothing to check. Error will be
8184 reported on the source type. */
8185 if (!Comes_From_Source (gnat_entity))
8186 return;
8188 mode = TYPE_MODE (object);
8189 align = TYPE_ALIGN (object);
8190 size = TYPE_SIZE (object);
8192 else if (DECL_BY_REF_P (object))
8194 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
8195 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
8196 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
8198 else
8200 mode = DECL_MODE (object);
8201 align = DECL_ALIGN (object);
8202 size = DECL_SIZE (object);
8205 /* Consider all floating-point types atomic and any types that that are
8206 represented by integers no wider than a machine word. */
8207 if (GET_MODE_CLASS (mode) == MODE_FLOAT
8208 || ((GET_MODE_CLASS (mode) == MODE_INT
8209 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
8210 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8211 return;
8213 /* For the moment, also allow anything that has an alignment equal
8214 to its size and which is smaller than a word. */
8215 if (size && TREE_CODE (size) == INTEGER_CST
8216 && compare_tree_int (size, align) == 0
8217 && align <= BITS_PER_WORD)
8218 return;
8220 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
8221 gnat_node = Next_Rep_Item (gnat_node))
8223 if (!comp_p && Nkind (gnat_node) == N_Pragma
8224 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8225 == Pragma_Atomic))
8226 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8227 else if (comp_p && Nkind (gnat_node) == N_Pragma
8228 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8229 == Pragma_Atomic_Components))
8230 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8233 if (comp_p)
8234 post_error_ne ("atomic access to component of & cannot be guaranteed",
8235 gnat_error_point, gnat_entity);
8236 else
8237 post_error_ne ("atomic access to & cannot be guaranteed",
8238 gnat_error_point, gnat_entity);
8242 /* Helper for the intrin compatibility checks family. Evaluate whether
8243 two types are definitely incompatible. */
8245 static bool
8246 intrin_types_incompatible_p (tree t1, tree t2)
8248 enum tree_code code;
8250 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8251 return false;
8253 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8254 return true;
8256 if (TREE_CODE (t1) != TREE_CODE (t2))
8257 return true;
8259 code = TREE_CODE (t1);
8261 switch (code)
8263 case INTEGER_TYPE:
8264 case REAL_TYPE:
8265 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8267 case POINTER_TYPE:
8268 case REFERENCE_TYPE:
8269 /* Assume designated types are ok. We'd need to account for char * and
8270 void * variants to do better, which could rapidly get messy and isn't
8271 clearly worth the effort. */
8272 return false;
8274 default:
8275 break;
8278 return false;
8281 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8282 on the Ada/builtin argument lists for the INB binding. */
8284 static bool
8285 intrin_arglists_compatible_p (intrin_binding_t * inb)
8287 function_args_iterator ada_iter, btin_iter;
8289 function_args_iter_init (&ada_iter, inb->ada_fntype);
8290 function_args_iter_init (&btin_iter, inb->btin_fntype);
8292 /* Sequence position of the last argument we checked. */
8293 int argpos = 0;
8295 while (1)
8297 tree ada_type = function_args_iter_cond (&ada_iter);
8298 tree btin_type = function_args_iter_cond (&btin_iter);
8300 /* If we've exhausted both lists simultaneously, we're done. */
8301 if (ada_type == NULL_TREE && btin_type == NULL_TREE)
8302 break;
8304 /* If one list is shorter than the other, they fail to match. */
8305 if (ada_type == NULL_TREE || btin_type == NULL_TREE)
8306 return false;
8308 /* If we're done with the Ada args and not with the internal builtin
8309 args, or the other way around, complain. */
8310 if (ada_type == void_type_node
8311 && btin_type != void_type_node)
8313 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8314 return false;
8317 if (btin_type == void_type_node
8318 && ada_type != void_type_node)
8320 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8321 inb->gnat_entity, inb->gnat_entity, argpos);
8322 return false;
8325 /* Otherwise, check that types match for the current argument. */
8326 argpos ++;
8327 if (intrin_types_incompatible_p (ada_type, btin_type))
8329 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8330 inb->gnat_entity, inb->gnat_entity, argpos);
8331 return false;
8335 function_args_iter_next (&ada_iter);
8336 function_args_iter_next (&btin_iter);
8339 return true;
8342 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8343 on the Ada/builtin return values for the INB binding. */
8345 static bool
8346 intrin_return_compatible_p (intrin_binding_t * inb)
8348 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8349 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8351 /* Accept function imported as procedure, common and convenient. */
8352 if (VOID_TYPE_P (ada_return_type)
8353 && !VOID_TYPE_P (btin_return_type))
8354 return true;
8356 /* If return type is Address (integer type), map it to void *. */
8357 if (Is_Descendent_Of_Address (Etype (inb->gnat_entity)))
8358 ada_return_type = ptr_void_type_node;
8360 /* Check return types compatibility otherwise. Note that this
8361 handles void/void as well. */
8362 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8364 post_error ("?intrinsic binding type mismatch on return value!",
8365 inb->gnat_entity);
8366 return false;
8369 return true;
8372 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8373 compatible. Issue relevant warnings when they are not.
8375 This is intended as a light check to diagnose the most obvious cases, not
8376 as a full fledged type compatibility predicate. It is the programmer's
8377 responsibility to ensure correctness of the Ada declarations in Imports,
8378 especially when binding straight to a compiler internal. */
8380 static bool
8381 intrin_profiles_compatible_p (intrin_binding_t * inb)
8383 /* Check compatibility on return values and argument lists, each responsible
8384 for posting warnings as appropriate. Ensure use of the proper sloc for
8385 this purpose. */
8387 bool arglists_compatible_p, return_compatible_p;
8388 location_t saved_location = input_location;
8390 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8392 return_compatible_p = intrin_return_compatible_p (inb);
8393 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8395 input_location = saved_location;
8397 return return_compatible_p && arglists_compatible_p;
8400 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8401 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8402 specified size for this field. POS_LIST is a position list describing
8403 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8404 to this layout. */
8406 static tree
8407 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8408 tree size, tree pos_list,
8409 vec<subst_pair> subst_list)
8411 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8412 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8413 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8414 tree new_pos, new_field;
8415 unsigned int i;
8416 subst_pair *s;
8418 if (CONTAINS_PLACEHOLDER_P (pos))
8419 FOR_EACH_VEC_ELT (subst_list, i, s)
8420 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8422 /* If the position is now a constant, we can set it as the position of the
8423 field when we make it. Otherwise, we need to deal with it specially. */
8424 if (TREE_CONSTANT (pos))
8425 new_pos = bit_from_pos (pos, bitpos);
8426 else
8427 new_pos = NULL_TREE;
8429 new_field
8430 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8431 size, new_pos, DECL_PACKED (old_field),
8432 !DECL_NONADDRESSABLE_P (old_field));
8434 if (!new_pos)
8436 normalize_offset (&pos, &bitpos, offset_align);
8437 /* Finalize the position. */
8438 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8439 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8440 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8441 DECL_SIZE (new_field) = size;
8442 DECL_SIZE_UNIT (new_field)
8443 = convert (sizetype,
8444 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8445 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8448 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8449 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8450 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8451 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8453 return new_field;
8456 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8457 it is the minimal size the REP_PART must have. */
8459 static tree
8460 create_rep_part (tree rep_type, tree record_type, tree min_size)
8462 tree field;
8464 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8465 min_size = NULL_TREE;
8467 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8468 min_size, NULL_TREE, 0, 1);
8469 DECL_INTERNAL_P (field) = 1;
8471 return field;
8474 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8476 static tree
8477 get_rep_part (tree record_type)
8479 tree field = TYPE_FIELDS (record_type);
8481 /* The REP part is the first field, internal, another record, and its name
8482 starts with an 'R'. */
8483 if (field
8484 && DECL_INTERNAL_P (field)
8485 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8486 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8487 return field;
8489 return NULL_TREE;
8492 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8494 tree
8495 get_variant_part (tree record_type)
8497 tree field;
8499 /* The variant part is the only internal field that is a qualified union. */
8500 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8501 if (DECL_INTERNAL_P (field)
8502 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8503 return field;
8505 return NULL_TREE;
8508 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8509 the list of variants to be used and RECORD_TYPE is the type of the parent.
8510 POS_LIST is a position list describing the layout of fields present in
8511 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8512 layout. */
8514 static tree
8515 create_variant_part_from (tree old_variant_part,
8516 vec<variant_desc> variant_list,
8517 tree record_type, tree pos_list,
8518 vec<subst_pair> subst_list)
8520 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8521 tree old_union_type = TREE_TYPE (old_variant_part);
8522 tree new_union_type, new_variant_part;
8523 tree union_field_list = NULL_TREE;
8524 variant_desc *v;
8525 unsigned int i;
8527 /* First create the type of the variant part from that of the old one. */
8528 new_union_type = make_node (QUAL_UNION_TYPE);
8529 TYPE_NAME (new_union_type)
8530 = concat_name (TYPE_NAME (record_type),
8531 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8533 /* If the position of the variant part is constant, subtract it from the
8534 size of the type of the parent to get the new size. This manual CSE
8535 reduces the code size when not optimizing. */
8536 if (TREE_CODE (offset) == INTEGER_CST)
8538 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8539 tree first_bit = bit_from_pos (offset, bitpos);
8540 TYPE_SIZE (new_union_type)
8541 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8542 TYPE_SIZE_UNIT (new_union_type)
8543 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8544 byte_from_pos (offset, bitpos));
8545 SET_TYPE_ADA_SIZE (new_union_type,
8546 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8547 first_bit));
8548 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8549 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8551 else
8552 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8554 /* Now finish up the new variants and populate the union type. */
8555 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8557 tree old_field = v->field, new_field;
8558 tree old_variant, old_variant_subpart, new_variant, field_list;
8560 /* Skip variants that don't belong to this nesting level. */
8561 if (DECL_CONTEXT (old_field) != old_union_type)
8562 continue;
8564 /* Retrieve the list of fields already added to the new variant. */
8565 new_variant = v->new_type;
8566 field_list = TYPE_FIELDS (new_variant);
8568 /* If the old variant had a variant subpart, we need to create a new
8569 variant subpart and add it to the field list. */
8570 old_variant = v->type;
8571 old_variant_subpart = get_variant_part (old_variant);
8572 if (old_variant_subpart)
8574 tree new_variant_subpart
8575 = create_variant_part_from (old_variant_subpart, variant_list,
8576 new_variant, pos_list, subst_list);
8577 DECL_CHAIN (new_variant_subpart) = field_list;
8578 field_list = new_variant_subpart;
8581 /* Finish up the new variant and create the field. No need for debug
8582 info thanks to the XVS type. */
8583 finish_record_type (new_variant, nreverse (field_list), 2, false);
8584 compute_record_mode (new_variant);
8585 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
8586 Empty);
8588 new_field
8589 = create_field_decl_from (old_field, new_variant, new_union_type,
8590 TYPE_SIZE (new_variant),
8591 pos_list, subst_list);
8592 DECL_QUALIFIER (new_field) = v->qual;
8593 DECL_INTERNAL_P (new_field) = 1;
8594 DECL_CHAIN (new_field) = union_field_list;
8595 union_field_list = new_field;
8598 /* Finish up the union type and create the variant part. No need for debug
8599 info thanks to the XVS type. Note that we don't reverse the field list
8600 because VARIANT_LIST has been traversed in reverse order. */
8601 finish_record_type (new_union_type, union_field_list, 2, false);
8602 compute_record_mode (new_union_type);
8603 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
8604 Empty);
8606 new_variant_part
8607 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8608 TYPE_SIZE (new_union_type),
8609 pos_list, subst_list);
8610 DECL_INTERNAL_P (new_variant_part) = 1;
8612 /* With multiple discriminants it is possible for an inner variant to be
8613 statically selected while outer ones are not; in this case, the list
8614 of fields of the inner variant is not flattened and we end up with a
8615 qualified union with a single member. Drop the useless container. */
8616 if (!DECL_CHAIN (union_field_list))
8618 DECL_CONTEXT (union_field_list) = record_type;
8619 DECL_FIELD_OFFSET (union_field_list)
8620 = DECL_FIELD_OFFSET (new_variant_part);
8621 DECL_FIELD_BIT_OFFSET (union_field_list)
8622 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8623 SET_DECL_OFFSET_ALIGN (union_field_list,
8624 DECL_OFFSET_ALIGN (new_variant_part));
8625 new_variant_part = union_field_list;
8628 return new_variant_part;
8631 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8632 which are both RECORD_TYPE, after applying the substitutions described
8633 in SUBST_LIST. */
8635 static void
8636 copy_and_substitute_in_size (tree new_type, tree old_type,
8637 vec<subst_pair> subst_list)
8639 unsigned int i;
8640 subst_pair *s;
8642 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8643 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8644 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8645 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8646 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8648 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8649 FOR_EACH_VEC_ELT (subst_list, i, s)
8650 TYPE_SIZE (new_type)
8651 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8652 s->discriminant, s->replacement);
8654 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8655 FOR_EACH_VEC_ELT (subst_list, i, s)
8656 TYPE_SIZE_UNIT (new_type)
8657 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8658 s->discriminant, s->replacement);
8660 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8661 FOR_EACH_VEC_ELT (subst_list, i, s)
8662 SET_TYPE_ADA_SIZE
8663 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8664 s->discriminant, s->replacement));
8666 /* Finalize the size. */
8667 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8668 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8671 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8672 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8673 updated by replacing F with R.
8675 The function doesn't update the layout of the type, i.e. it assumes
8676 that the substitution is purely formal. That's why the replacement
8677 value R must itself contain a PLACEHOLDER_EXPR. */
8679 tree
8680 substitute_in_type (tree t, tree f, tree r)
8682 tree nt;
8684 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8686 switch (TREE_CODE (t))
8688 case INTEGER_TYPE:
8689 case ENUMERAL_TYPE:
8690 case BOOLEAN_TYPE:
8691 case REAL_TYPE:
8693 /* First the domain types of arrays. */
8694 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8695 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8697 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8698 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8700 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8701 return t;
8703 nt = copy_type (t);
8704 TYPE_GCC_MIN_VALUE (nt) = low;
8705 TYPE_GCC_MAX_VALUE (nt) = high;
8707 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8708 SET_TYPE_INDEX_TYPE
8709 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8711 return nt;
8714 /* Then the subtypes. */
8715 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8716 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8718 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8719 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8721 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8722 return t;
8724 nt = copy_type (t);
8725 SET_TYPE_RM_MIN_VALUE (nt, low);
8726 SET_TYPE_RM_MAX_VALUE (nt, high);
8728 return nt;
8731 return t;
8733 case COMPLEX_TYPE:
8734 nt = substitute_in_type (TREE_TYPE (t), f, r);
8735 if (nt == TREE_TYPE (t))
8736 return t;
8738 return build_complex_type (nt);
8740 case FUNCTION_TYPE:
8741 /* These should never show up here. */
8742 gcc_unreachable ();
8744 case ARRAY_TYPE:
8746 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8747 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8749 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8750 return t;
8752 nt = build_nonshared_array_type (component, domain);
8753 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8754 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8755 SET_TYPE_MODE (nt, TYPE_MODE (t));
8756 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8757 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8758 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8759 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8760 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8761 return nt;
8764 case RECORD_TYPE:
8765 case UNION_TYPE:
8766 case QUAL_UNION_TYPE:
8768 bool changed_field = false;
8769 tree field;
8771 /* Start out with no fields, make new fields, and chain them
8772 in. If we haven't actually changed the type of any field,
8773 discard everything we've done and return the old type. */
8774 nt = copy_type (t);
8775 TYPE_FIELDS (nt) = NULL_TREE;
8777 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8779 tree new_field = copy_node (field), new_n;
8781 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8782 if (new_n != TREE_TYPE (field))
8784 TREE_TYPE (new_field) = new_n;
8785 changed_field = true;
8788 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8789 if (new_n != DECL_FIELD_OFFSET (field))
8791 DECL_FIELD_OFFSET (new_field) = new_n;
8792 changed_field = true;
8795 /* Do the substitution inside the qualifier, if any. */
8796 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8798 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8799 if (new_n != DECL_QUALIFIER (field))
8801 DECL_QUALIFIER (new_field) = new_n;
8802 changed_field = true;
8806 DECL_CONTEXT (new_field) = nt;
8807 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8809 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8810 TYPE_FIELDS (nt) = new_field;
8813 if (!changed_field)
8814 return t;
8816 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8817 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8818 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8819 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8820 return nt;
8823 default:
8824 return t;
8828 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8829 needed to represent the object. */
8831 tree
8832 rm_size (tree gnu_type)
8834 /* For integral types, we store the RM size explicitly. */
8835 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8836 return TYPE_RM_SIZE (gnu_type);
8838 /* Return the RM size of the actual data plus the size of the template. */
8839 if (TREE_CODE (gnu_type) == RECORD_TYPE
8840 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8841 return
8842 size_binop (PLUS_EXPR,
8843 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8844 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8846 /* For record or union types, we store the size explicitly. */
8847 if (RECORD_OR_UNION_TYPE_P (gnu_type)
8848 && !TYPE_FAT_POINTER_P (gnu_type)
8849 && TYPE_ADA_SIZE (gnu_type))
8850 return TYPE_ADA_SIZE (gnu_type);
8852 /* For other types, this is just the size. */
8853 return TYPE_SIZE (gnu_type);
8856 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8857 fully-qualified name, possibly with type information encoding.
8858 Otherwise, return the name. */
8860 tree
8861 get_entity_name (Entity_Id gnat_entity)
8863 Get_Encoded_Name (gnat_entity);
8864 return get_identifier_with_length (Name_Buffer, Name_Len);
8867 /* Return an identifier representing the external name to be used for
8868 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8869 and the specified suffix. */
8871 tree
8872 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8874 Entity_Kind kind = Ekind (gnat_entity);
8876 if (suffix)
8878 String_Template temp = {1, (int) strlen (suffix)};
8879 Fat_Pointer fp = {suffix, &temp};
8880 Get_External_Name_With_Suffix (gnat_entity, fp);
8882 else
8883 Get_External_Name (gnat_entity, 0);
8885 /* A variable using the Stdcall convention lives in a DLL. We adjust
8886 its name to use the jump table, the _imp__NAME contains the address
8887 for the NAME variable. */
8888 if ((kind == E_Variable || kind == E_Constant)
8889 && Has_Stdcall_Convention (gnat_entity))
8891 const int len = 6 + Name_Len;
8892 char *new_name = (char *) alloca (len + 1);
8893 strcpy (new_name, "_imp__");
8894 strcat (new_name, Name_Buffer);
8895 return get_identifier_with_length (new_name, len);
8898 return get_identifier_with_length (Name_Buffer, Name_Len);
8901 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8902 string, return a new IDENTIFIER_NODE that is the concatenation of
8903 the name followed by "___" and the specified suffix. */
8905 tree
8906 concat_name (tree gnu_name, const char *suffix)
8908 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8909 char *new_name = (char *) alloca (len + 1);
8910 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8911 strcat (new_name, "___");
8912 strcat (new_name, suffix);
8913 return get_identifier_with_length (new_name, len);
8916 /* Initialize data structures of the decl.c module. */
8918 void
8919 init_gnat_decl (void)
8921 /* Initialize the cache of annotated values. */
8922 annotate_value_cache
8923 = htab_create_ggc (512, tree_int_map_hash, tree_int_map_eq, 0);
8926 /* Destroy data structures of the decl.c module. */
8928 void
8929 destroy_gnat_decl (void)
8931 /* Destroy the cache of annotated values. */
8932 htab_delete (annotate_value_cache);
8933 annotate_value_cache = NULL;
8936 #include "gt-ada-decl.h"