* stor-layout.c (place_field): Finalize non-constant offset for the
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blob1e07693591e8afdc8e073afbbe9b02614c77543e
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_NAME (gnu_index);
2676 if (TREE_CODE (gnu_index_name) == TYPE_DECL)
2677 gnu_index_name = DECL_NAME (gnu_index_name);
2679 /* Make sure to reference the types themselves, and not just
2680 their names, as the debugger may fall back on them. */
2681 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2682 gnu_bound_rec, NULL_TREE,
2683 NULL_TREE, 0, 0);
2684 DECL_CHAIN (gnu_field) = gnu_field_list;
2685 gnu_field_list = gnu_field;
2688 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2689 add_parallel_type (gnu_type, gnu_bound_rec);
2692 /* If this is a packed array type, make the original array type a
2693 parallel type. Otherwise, do it for the base array type if it
2694 isn't artificial to make sure it is kept in the debug info. */
2695 if (debug_info_p)
2697 if (Is_Packed_Array_Type (gnat_entity)
2698 && present_gnu_tree (Original_Array_Type (gnat_entity)))
2699 add_parallel_type (gnu_type,
2700 gnat_to_gnu_type
2701 (Original_Array_Type (gnat_entity)));
2702 else
2704 tree gnu_base_decl
2705 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2706 if (!DECL_ARTIFICIAL (gnu_base_decl))
2707 add_parallel_type (gnu_type,
2708 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2712 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2713 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2714 = (Is_Packed_Array_Type (gnat_entity)
2715 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2717 /* If the size is self-referential and the maximum size doesn't
2718 overflow, use it. */
2719 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2720 && gnu_max_size
2721 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2722 && TREE_OVERFLOW (gnu_max_size))
2723 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2724 && TREE_OVERFLOW (gnu_max_size_unit)))
2726 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2727 TYPE_SIZE (gnu_type));
2728 TYPE_SIZE_UNIT (gnu_type)
2729 = size_binop (MIN_EXPR, gnu_max_size_unit,
2730 TYPE_SIZE_UNIT (gnu_type));
2733 /* Set our alias set to that of our base type. This gives all
2734 array subtypes the same alias set. */
2735 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2737 /* If this is a packed type, make this type the same as the packed
2738 array type, but do some adjusting in the type first. */
2739 if (Present (Packed_Array_Type (gnat_entity)))
2741 Entity_Id gnat_index;
2742 tree gnu_inner;
2744 /* First finish the type we had been making so that we output
2745 debugging information for it. */
2746 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2747 if (Treat_As_Volatile (gnat_entity))
2748 gnu_type
2749 = build_qualified_type (gnu_type,
2750 TYPE_QUALS (gnu_type)
2751 | TYPE_QUAL_VOLATILE);
2752 /* Make it artificial only if the base type was artificial too.
2753 That's sort of "morally" true and will make it possible for
2754 the debugger to look it up by name in DWARF, which is needed
2755 in order to decode the packed array type. */
2756 gnu_decl
2757 = create_type_decl (gnu_entity_name, gnu_type,
2758 !Comes_From_Source (Etype (gnat_entity))
2759 && !Comes_From_Source (gnat_entity),
2760 debug_info_p, gnat_entity);
2762 /* Save it as our equivalent in case the call below elaborates
2763 this type again. */
2764 save_gnu_tree (gnat_entity, gnu_decl, false);
2766 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2767 NULL_TREE, 0);
2768 this_made_decl = true;
2769 gnu_type = TREE_TYPE (gnu_decl);
2770 save_gnu_tree (gnat_entity, NULL_TREE, false);
2772 gnu_inner = gnu_type;
2773 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2774 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2775 || TYPE_PADDING_P (gnu_inner)))
2776 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2778 /* We need to attach the index type to the type we just made so
2779 that the actual bounds can later be put into a template. */
2780 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2781 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2782 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2783 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2785 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2787 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2788 TYPE_MODULUS for modular types so we make an extra
2789 subtype if necessary. */
2790 if (TYPE_MODULAR_P (gnu_inner))
2792 tree gnu_subtype
2793 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2794 TREE_TYPE (gnu_subtype) = gnu_inner;
2795 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2796 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2797 TYPE_MIN_VALUE (gnu_inner));
2798 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2799 TYPE_MAX_VALUE (gnu_inner));
2800 gnu_inner = gnu_subtype;
2803 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2805 #ifdef ENABLE_CHECKING
2806 /* Check for other cases of overloading. */
2807 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2808 #endif
2811 for (gnat_index = First_Index (gnat_entity);
2812 Present (gnat_index);
2813 gnat_index = Next_Index (gnat_index))
2814 SET_TYPE_ACTUAL_BOUNDS
2815 (gnu_inner,
2816 tree_cons (NULL_TREE,
2817 get_unpadded_type (Etype (gnat_index)),
2818 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2820 if (Convention (gnat_entity) != Convention_Fortran)
2821 SET_TYPE_ACTUAL_BOUNDS
2822 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2824 if (TREE_CODE (gnu_type) == RECORD_TYPE
2825 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2826 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2830 else
2831 /* Abort if packed array with no Packed_Array_Type field set. */
2832 gcc_assert (!Is_Packed (gnat_entity));
2834 break;
2836 case E_String_Literal_Subtype:
2837 /* Create the type for a string literal. */
2839 Entity_Id gnat_full_type
2840 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2841 && Present (Full_View (Etype (gnat_entity)))
2842 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2843 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2844 tree gnu_string_array_type
2845 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2846 tree gnu_string_index_type
2847 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2848 (TYPE_DOMAIN (gnu_string_array_type))));
2849 tree gnu_lower_bound
2850 = convert (gnu_string_index_type,
2851 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2852 tree gnu_length
2853 = UI_To_gnu (String_Literal_Length (gnat_entity),
2854 gnu_string_index_type);
2855 tree gnu_upper_bound
2856 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2857 gnu_lower_bound,
2858 int_const_binop (MINUS_EXPR, gnu_length,
2859 integer_one_node));
2860 tree gnu_index_type
2861 = create_index_type (convert (sizetype, gnu_lower_bound),
2862 convert (sizetype, gnu_upper_bound),
2863 create_range_type (gnu_string_index_type,
2864 gnu_lower_bound,
2865 gnu_upper_bound),
2866 gnat_entity);
2868 gnu_type
2869 = build_nonshared_array_type (gnat_to_gnu_type
2870 (Component_Type (gnat_entity)),
2871 gnu_index_type);
2872 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2873 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2874 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2876 break;
2878 /* Record Types and Subtypes
2880 The following fields are defined on record types:
2882 Has_Discriminants True if the record has discriminants
2883 First_Discriminant Points to head of list of discriminants
2884 First_Entity Points to head of list of fields
2885 Is_Tagged_Type True if the record is tagged
2887 Implementation of Ada records and discriminated records:
2889 A record type definition is transformed into the equivalent of a C
2890 struct definition. The fields that are the discriminants which are
2891 found in the Full_Type_Declaration node and the elements of the
2892 Component_List found in the Record_Type_Definition node. The
2893 Component_List can be a recursive structure since each Variant of
2894 the Variant_Part of the Component_List has a Component_List.
2896 Processing of a record type definition comprises starting the list of
2897 field declarations here from the discriminants and the calling the
2898 function components_to_record to add the rest of the fields from the
2899 component list and return the gnu type node. The function
2900 components_to_record will call itself recursively as it traverses
2901 the tree. */
2903 case E_Record_Type:
2904 if (Has_Complex_Representation (gnat_entity))
2906 gnu_type
2907 = build_complex_type
2908 (get_unpadded_type
2909 (Etype (Defining_Entity
2910 (First (Component_Items
2911 (Component_List
2912 (Type_Definition
2913 (Declaration_Node (gnat_entity)))))))));
2915 break;
2919 Node_Id full_definition = Declaration_Node (gnat_entity);
2920 Node_Id record_definition = Type_Definition (full_definition);
2921 Node_Id gnat_constr;
2922 Entity_Id gnat_field;
2923 tree gnu_field, gnu_field_list = NULL_TREE;
2924 tree gnu_get_parent;
2925 /* Set PACKED in keeping with gnat_to_gnu_field. */
2926 const int packed
2927 = Is_Packed (gnat_entity)
2929 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2930 ? -1
2931 : (Known_Alignment (gnat_entity)
2932 || (Strict_Alignment (gnat_entity)
2933 && Known_RM_Size (gnat_entity)))
2934 ? -2
2935 : 0;
2936 const bool has_discr = Has_Discriminants (gnat_entity);
2937 const bool has_rep = Has_Specified_Layout (gnat_entity);
2938 const bool is_extension
2939 = (Is_Tagged_Type (gnat_entity)
2940 && Nkind (record_definition) == N_Derived_Type_Definition);
2941 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2942 bool all_rep = has_rep;
2944 /* See if all fields have a rep clause. Stop when we find one
2945 that doesn't. */
2946 if (all_rep)
2947 for (gnat_field = First_Entity (gnat_entity);
2948 Present (gnat_field);
2949 gnat_field = Next_Entity (gnat_field))
2950 if ((Ekind (gnat_field) == E_Component
2951 || Ekind (gnat_field) == E_Discriminant)
2952 && No (Component_Clause (gnat_field)))
2954 all_rep = false;
2955 break;
2958 /* If this is a record extension, go a level further to find the
2959 record definition. Also, verify we have a Parent_Subtype. */
2960 if (is_extension)
2962 if (!type_annotate_only
2963 || Present (Record_Extension_Part (record_definition)))
2964 record_definition = Record_Extension_Part (record_definition);
2966 gcc_assert (type_annotate_only
2967 || Present (Parent_Subtype (gnat_entity)));
2970 /* Make a node for the record. If we are not defining the record,
2971 suppress expanding incomplete types. */
2972 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2973 TYPE_NAME (gnu_type) = gnu_entity_name;
2974 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2975 if (Reverse_Storage_Order (gnat_entity))
2976 sorry ("non-default Scalar_Storage_Order");
2977 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
2979 if (!definition)
2981 defer_incomplete_level++;
2982 this_deferred = true;
2985 /* If both a size and rep clause was specified, put the size in
2986 the record type now so that it can get the proper mode. */
2987 if (has_rep && Known_RM_Size (gnat_entity))
2988 TYPE_SIZE (gnu_type)
2989 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
2991 /* Always set the alignment here so that it can be used to
2992 set the mode, if it is making the alignment stricter. If
2993 it is invalid, it will be checked again below. If this is to
2994 be Atomic, choose a default alignment of a word unless we know
2995 the size and it's smaller. */
2996 if (Known_Alignment (gnat_entity))
2997 TYPE_ALIGN (gnu_type)
2998 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2999 else if (Is_Atomic (gnat_entity) && Known_Esize (gnat_entity))
3001 unsigned int size = UI_To_Int (Esize (gnat_entity));
3002 TYPE_ALIGN (gnu_type)
3003 = size >= BITS_PER_WORD ? BITS_PER_WORD : ceil_pow2 (size);
3005 /* If a type needs strict alignment, the minimum size will be the
3006 type size instead of the RM size (see validate_size). Cap the
3007 alignment, lest it causes this type size to become too large. */
3008 else if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
3010 unsigned int raw_size = UI_To_Int (RM_Size (gnat_entity));
3011 unsigned int raw_align = raw_size & -raw_size;
3012 if (raw_align < BIGGEST_ALIGNMENT)
3013 TYPE_ALIGN (gnu_type) = raw_align;
3015 else
3016 TYPE_ALIGN (gnu_type) = 0;
3018 /* If we have a Parent_Subtype, make a field for the parent. If
3019 this record has rep clauses, force the position to zero. */
3020 if (Present (Parent_Subtype (gnat_entity)))
3022 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
3023 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
3024 tree gnu_parent;
3026 /* A major complexity here is that the parent subtype will
3027 reference our discriminants in its Stored_Constraint list.
3028 But those must reference the parent component of this record
3029 which is precisely of the parent subtype we have not built yet!
3030 To break the circle we first build a dummy COMPONENT_REF which
3031 represents the "get to the parent" operation and initialize
3032 each of those discriminants to a COMPONENT_REF of the above
3033 dummy parent referencing the corresponding discriminant of the
3034 base type of the parent subtype. */
3035 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3036 build0 (PLACEHOLDER_EXPR, gnu_type),
3037 build_decl (input_location,
3038 FIELD_DECL, NULL_TREE,
3039 gnu_dummy_parent_type),
3040 NULL_TREE);
3042 if (has_discr)
3043 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3044 Present (gnat_field);
3045 gnat_field = Next_Stored_Discriminant (gnat_field))
3046 if (Present (Corresponding_Discriminant (gnat_field)))
3048 tree gnu_field
3049 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3050 (gnat_field));
3051 save_gnu_tree
3052 (gnat_field,
3053 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3054 gnu_get_parent, gnu_field, NULL_TREE),
3055 true);
3058 /* Then we build the parent subtype. If it has discriminants but
3059 the type itself has unknown discriminants, this means that it
3060 doesn't contain information about how the discriminants are
3061 derived from those of the ancestor type, so it cannot be used
3062 directly. Instead it is built by cloning the parent subtype
3063 of the underlying record view of the type, for which the above
3064 derivation of discriminants has been made explicit. */
3065 if (Has_Discriminants (gnat_parent)
3066 && Has_Unknown_Discriminants (gnat_entity))
3068 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3070 /* If we are defining the type, the underlying record
3071 view must already have been elaborated at this point.
3072 Otherwise do it now as its parent subtype cannot be
3073 technically elaborated on its own. */
3074 if (definition)
3075 gcc_assert (present_gnu_tree (gnat_uview));
3076 else
3077 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
3079 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3081 /* Substitute the "get to the parent" of the type for that
3082 of its underlying record view in the cloned type. */
3083 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3084 Present (gnat_field);
3085 gnat_field = Next_Stored_Discriminant (gnat_field))
3086 if (Present (Corresponding_Discriminant (gnat_field)))
3088 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3089 tree gnu_ref
3090 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3091 gnu_get_parent, gnu_field, NULL_TREE);
3092 gnu_parent
3093 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3096 else
3097 gnu_parent = gnat_to_gnu_type (gnat_parent);
3099 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3100 initially built. The discriminants must reference the fields
3101 of the parent subtype and not those of its base type for the
3102 placeholder machinery to properly work. */
3103 if (has_discr)
3105 /* The actual parent subtype is the full view. */
3106 if (IN (Ekind (gnat_parent), Private_Kind))
3108 if (Present (Full_View (gnat_parent)))
3109 gnat_parent = Full_View (gnat_parent);
3110 else
3111 gnat_parent = Underlying_Full_View (gnat_parent);
3114 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3115 Present (gnat_field);
3116 gnat_field = Next_Stored_Discriminant (gnat_field))
3117 if (Present (Corresponding_Discriminant (gnat_field)))
3119 Entity_Id field = Empty;
3120 for (field = First_Stored_Discriminant (gnat_parent);
3121 Present (field);
3122 field = Next_Stored_Discriminant (field))
3123 if (same_discriminant_p (gnat_field, field))
3124 break;
3125 gcc_assert (Present (field));
3126 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3127 = gnat_to_gnu_field_decl (field);
3131 /* The "get to the parent" COMPONENT_REF must be given its
3132 proper type... */
3133 TREE_TYPE (gnu_get_parent) = gnu_parent;
3135 /* ...and reference the _Parent field of this record. */
3136 gnu_field
3137 = create_field_decl (parent_name_id,
3138 gnu_parent, gnu_type,
3139 has_rep
3140 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3141 has_rep
3142 ? bitsize_zero_node : NULL_TREE,
3143 0, 1);
3144 DECL_INTERNAL_P (gnu_field) = 1;
3145 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3146 TYPE_FIELDS (gnu_type) = gnu_field;
3149 /* Make the fields for the discriminants and put them into the record
3150 unless it's an Unchecked_Union. */
3151 if (has_discr)
3152 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3153 Present (gnat_field);
3154 gnat_field = Next_Stored_Discriminant (gnat_field))
3156 /* If this is a record extension and this discriminant is the
3157 renaming of another discriminant, we've handled it above. */
3158 if (Present (Parent_Subtype (gnat_entity))
3159 && Present (Corresponding_Discriminant (gnat_field)))
3160 continue;
3162 gnu_field
3163 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3164 debug_info_p);
3166 /* Make an expression using a PLACEHOLDER_EXPR from the
3167 FIELD_DECL node just created and link that with the
3168 corresponding GNAT defining identifier. */
3169 save_gnu_tree (gnat_field,
3170 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3171 build0 (PLACEHOLDER_EXPR, gnu_type),
3172 gnu_field, NULL_TREE),
3173 true);
3175 if (!is_unchecked_union)
3177 DECL_CHAIN (gnu_field) = gnu_field_list;
3178 gnu_field_list = gnu_field;
3182 /* If we have a derived untagged type that renames discriminants in
3183 the root type, the (stored) discriminants are a just copy of the
3184 discriminants of the root type. This means that any constraints
3185 added by the renaming in the derivation are disregarded as far
3186 as the layout of the derived type is concerned. To rescue them,
3187 we change the type of the (stored) discriminants to a subtype
3188 with the bounds of the type of the visible discriminants. */
3189 if (has_discr
3190 && !is_extension
3191 && Stored_Constraint (gnat_entity) != No_Elist)
3192 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3193 gnat_constr != No_Elmt;
3194 gnat_constr = Next_Elmt (gnat_constr))
3195 if (Nkind (Node (gnat_constr)) == N_Identifier
3196 /* Ignore access discriminants. */
3197 && !Is_Access_Type (Etype (Node (gnat_constr)))
3198 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3200 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3201 tree gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3202 tree gnu_ref
3203 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3204 NULL_TREE, 0);
3206 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3207 just above for one of the stored discriminants. */
3208 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3210 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3212 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3213 tree gnu_subtype
3214 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3215 ? make_unsigned_type (prec) : make_signed_type (prec);
3216 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3217 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3218 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3219 TYPE_MIN_VALUE (gnu_discr_type));
3220 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3221 TYPE_MAX_VALUE (gnu_discr_type));
3222 TREE_TYPE (gnu_ref)
3223 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3227 /* Add the fields into the record type and finish it up. */
3228 components_to_record (gnu_type, Component_List (record_definition),
3229 gnu_field_list, packed, definition, false,
3230 all_rep, is_unchecked_union,
3231 !Comes_From_Source (gnat_entity), debug_info_p,
3232 false, OK_To_Reorder_Components (gnat_entity),
3233 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3235 /* If it is passed by reference, force BLKmode to ensure that objects
3236 of this type will always be put in memory. */
3237 if (TYPE_MODE (gnu_type) != BLKmode
3238 && Is_By_Reference_Type (gnat_entity))
3239 SET_TYPE_MODE (gnu_type, BLKmode);
3241 /* We used to remove the associations of the discriminants and _Parent
3242 for validity checking but we may need them if there's a Freeze_Node
3243 for a subtype used in this record. */
3244 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3246 /* Fill in locations of fields. */
3247 annotate_rep (gnat_entity, gnu_type);
3249 /* If there are any entities in the chain corresponding to components
3250 that we did not elaborate, ensure we elaborate their types if they
3251 are Itypes. */
3252 for (gnat_temp = First_Entity (gnat_entity);
3253 Present (gnat_temp);
3254 gnat_temp = Next_Entity (gnat_temp))
3255 if ((Ekind (gnat_temp) == E_Component
3256 || Ekind (gnat_temp) == E_Discriminant)
3257 && Is_Itype (Etype (gnat_temp))
3258 && !present_gnu_tree (gnat_temp))
3259 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3261 /* If this is a record type associated with an exception definition,
3262 equate its fields to those of the standard exception type. This
3263 will make it possible to convert between them. */
3264 if (gnu_entity_name == exception_data_name_id)
3266 tree gnu_std_field;
3267 for (gnu_field = TYPE_FIELDS (gnu_type),
3268 gnu_std_field = TYPE_FIELDS (except_type_node);
3269 gnu_field;
3270 gnu_field = DECL_CHAIN (gnu_field),
3271 gnu_std_field = DECL_CHAIN (gnu_std_field))
3272 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3273 gcc_assert (!gnu_std_field);
3276 break;
3278 case E_Class_Wide_Subtype:
3279 /* If an equivalent type is present, that is what we should use.
3280 Otherwise, fall through to handle this like a record subtype
3281 since it may have constraints. */
3282 if (gnat_equiv_type != gnat_entity)
3284 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3285 maybe_present = true;
3286 break;
3289 /* ... fall through ... */
3291 case E_Record_Subtype:
3292 /* If Cloned_Subtype is Present it means this record subtype has
3293 identical layout to that type or subtype and we should use
3294 that GCC type for this one. The front end guarantees that
3295 the component list is shared. */
3296 if (Present (Cloned_Subtype (gnat_entity)))
3298 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3299 NULL_TREE, 0);
3300 maybe_present = true;
3301 break;
3304 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3305 changing the type, make a new type with each field having the type of
3306 the field in the new subtype but the position computed by transforming
3307 every discriminant reference according to the constraints. We don't
3308 see any difference between private and non-private type here since
3309 derivations from types should have been deferred until the completion
3310 of the private type. */
3311 else
3313 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3314 tree gnu_base_type;
3316 if (!definition)
3318 defer_incomplete_level++;
3319 this_deferred = true;
3322 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3324 if (present_gnu_tree (gnat_entity))
3326 maybe_present = true;
3327 break;
3330 /* If this is a record subtype associated with a dispatch table,
3331 strip the suffix. This is necessary to make sure 2 different
3332 subtypes associated with the imported and exported views of a
3333 dispatch table are properly merged in LTO mode. */
3334 if (Is_Dispatch_Table_Entity (gnat_entity))
3336 char *p;
3337 Get_Encoded_Name (gnat_entity);
3338 p = strchr (Name_Buffer, '_');
3339 gcc_assert (p);
3340 strcpy (p+2, "dtS");
3341 gnu_entity_name = get_identifier (Name_Buffer);
3344 /* When the subtype has discriminants and these discriminants affect
3345 the initial shape it has inherited, factor them in. But for an
3346 Unchecked_Union (it must be an Itype), just return the type.
3347 We can't just test Is_Constrained because private subtypes without
3348 discriminants of types with discriminants with default expressions
3349 are Is_Constrained but aren't constrained! */
3350 if (IN (Ekind (gnat_base_type), Record_Kind)
3351 && !Is_Unchecked_Union (gnat_base_type)
3352 && !Is_For_Access_Subtype (gnat_entity)
3353 && Has_Discriminants (gnat_entity)
3354 && Is_Constrained (gnat_entity)
3355 && Stored_Constraint (gnat_entity) != No_Elist)
3357 vec<subst_pair> gnu_subst_list
3358 = build_subst_list (gnat_entity, gnat_base_type, definition);
3359 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3360 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3361 bool selected_variant = false, all_constant_pos = true;
3362 Entity_Id gnat_field;
3363 vec<variant_desc> gnu_variant_list;
3365 gnu_type = make_node (RECORD_TYPE);
3366 TYPE_NAME (gnu_type) = gnu_entity_name;
3367 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3368 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3370 /* Set the size, alignment and alias set of the new type to
3371 match that of the old one, doing required substitutions. */
3372 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3373 gnu_subst_list);
3375 if (TYPE_IS_PADDING_P (gnu_base_type))
3376 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3377 else
3378 gnu_unpad_base_type = gnu_base_type;
3380 /* Look for REP and variant parts in the base type. */
3381 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3382 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3384 /* If there is a variant part, we must compute whether the
3385 constraints statically select a particular variant. If
3386 so, we simply drop the qualified union and flatten the
3387 list of fields. Otherwise we'll build a new qualified
3388 union for the variants that are still relevant. */
3389 if (gnu_variant_part)
3391 variant_desc *v;
3392 unsigned int i;
3394 gnu_variant_list
3395 = build_variant_list (TREE_TYPE (gnu_variant_part),
3396 gnu_subst_list,
3397 vNULL);
3399 /* If all the qualifiers are unconditionally true, the
3400 innermost variant is statically selected. */
3401 selected_variant = true;
3402 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3403 if (!integer_onep (v->qual))
3405 selected_variant = false;
3406 break;
3409 /* Otherwise, create the new variants. */
3410 if (!selected_variant)
3411 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3413 tree old_variant = v->type;
3414 tree new_variant = make_node (RECORD_TYPE);
3415 tree suffix
3416 = concat_name (DECL_NAME (gnu_variant_part),
3417 IDENTIFIER_POINTER
3418 (DECL_NAME (v->field)));
3419 TYPE_NAME (new_variant)
3420 = concat_name (TYPE_NAME (gnu_type),
3421 IDENTIFIER_POINTER (suffix));
3422 copy_and_substitute_in_size (new_variant, old_variant,
3423 gnu_subst_list);
3424 v->new_type = new_variant;
3427 else
3429 gnu_variant_list.create (0);
3430 selected_variant = false;
3433 /* Make a list of fields and their position in the base type. */
3434 gnu_pos_list
3435 = build_position_list (gnu_unpad_base_type,
3436 gnu_variant_list.exists ()
3437 && !selected_variant,
3438 size_zero_node, bitsize_zero_node,
3439 BIGGEST_ALIGNMENT, NULL_TREE);
3441 /* Now go down every component in the subtype and compute its
3442 size and position from those of the component in the base
3443 type and from the constraints of the subtype. */
3444 for (gnat_field = First_Entity (gnat_entity);
3445 Present (gnat_field);
3446 gnat_field = Next_Entity (gnat_field))
3447 if ((Ekind (gnat_field) == E_Component
3448 || Ekind (gnat_field) == E_Discriminant)
3449 && !(Present (Corresponding_Discriminant (gnat_field))
3450 && Is_Tagged_Type (gnat_base_type))
3451 && Underlying_Type
3452 (Scope (Original_Record_Component (gnat_field)))
3453 == gnat_base_type)
3455 Name_Id gnat_name = Chars (gnat_field);
3456 Entity_Id gnat_old_field
3457 = Original_Record_Component (gnat_field);
3458 tree gnu_old_field
3459 = gnat_to_gnu_field_decl (gnat_old_field);
3460 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3461 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3462 tree gnu_cont_type, gnu_last = NULL_TREE;
3464 /* If the type is the same, retrieve the GCC type from the
3465 old field to take into account possible adjustments. */
3466 if (Etype (gnat_field) == Etype (gnat_old_field))
3467 gnu_field_type = TREE_TYPE (gnu_old_field);
3468 else
3469 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3471 /* If there was a component clause, the field types must be
3472 the same for the type and subtype, so copy the data from
3473 the old field to avoid recomputation here. Also if the
3474 field is justified modular and the optimization in
3475 gnat_to_gnu_field was applied. */
3476 if (Present (Component_Clause (gnat_old_field))
3477 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3478 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3479 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3480 == TREE_TYPE (gnu_old_field)))
3482 gnu_size = DECL_SIZE (gnu_old_field);
3483 gnu_field_type = TREE_TYPE (gnu_old_field);
3486 /* If the old field was packed and of constant size, we
3487 have to get the old size here, as it might differ from
3488 what the Etype conveys and the latter might overlap
3489 onto the following field. Try to arrange the type for
3490 possible better packing along the way. */
3491 else if (DECL_PACKED (gnu_old_field)
3492 && TREE_CODE (DECL_SIZE (gnu_old_field))
3493 == INTEGER_CST)
3495 gnu_size = DECL_SIZE (gnu_old_field);
3496 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3497 && !TYPE_FAT_POINTER_P (gnu_field_type)
3498 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3499 gnu_field_type
3500 = make_packable_type (gnu_field_type, true);
3503 else
3504 gnu_size = TYPE_SIZE (gnu_field_type);
3506 /* If the context of the old field is the base type or its
3507 REP part (if any), put the field directly in the new
3508 type; otherwise look up the context in the variant list
3509 and put the field either in the new type if there is a
3510 selected variant or in one of the new variants. */
3511 if (gnu_context == gnu_unpad_base_type
3512 || (gnu_rep_part
3513 && gnu_context == TREE_TYPE (gnu_rep_part)))
3514 gnu_cont_type = gnu_type;
3515 else
3517 variant_desc *v;
3518 unsigned int i;
3519 tree rep_part;
3521 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3522 if (gnu_context == v->type
3523 || ((rep_part = get_rep_part (v->type))
3524 && gnu_context == TREE_TYPE (rep_part)))
3525 break;
3526 if (v)
3528 if (selected_variant)
3529 gnu_cont_type = gnu_type;
3530 else
3531 gnu_cont_type = v->new_type;
3533 else
3534 /* The front-end may pass us "ghost" components if
3535 it fails to recognize that a constrained subtype
3536 is statically constrained. Discard them. */
3537 continue;
3540 /* Now create the new field modeled on the old one. */
3541 gnu_field
3542 = create_field_decl_from (gnu_old_field, gnu_field_type,
3543 gnu_cont_type, gnu_size,
3544 gnu_pos_list, gnu_subst_list);
3545 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3547 /* Put it in one of the new variants directly. */
3548 if (gnu_cont_type != gnu_type)
3550 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3551 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3554 /* To match the layout crafted in components_to_record,
3555 if this is the _Tag or _Parent field, put it before
3556 any other fields. */
3557 else if (gnat_name == Name_uTag
3558 || gnat_name == Name_uParent)
3559 gnu_field_list = chainon (gnu_field_list, gnu_field);
3561 /* Similarly, if this is the _Controller field, put
3562 it before the other fields except for the _Tag or
3563 _Parent field. */
3564 else if (gnat_name == Name_uController && gnu_last)
3566 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3567 DECL_CHAIN (gnu_last) = gnu_field;
3570 /* Otherwise, if this is a regular field, put it after
3571 the other fields. */
3572 else
3574 DECL_CHAIN (gnu_field) = gnu_field_list;
3575 gnu_field_list = gnu_field;
3576 if (!gnu_last)
3577 gnu_last = gnu_field;
3578 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3579 all_constant_pos = false;
3582 save_gnu_tree (gnat_field, gnu_field, false);
3585 /* If there is a variant list, a selected variant and the fields
3586 all have a constant position, put them in order of increasing
3587 position to match that of constant CONSTRUCTORs. Likewise if
3588 there is no variant list but a REP part, since the latter has
3589 been flattened in the process. */
3590 if (((gnu_variant_list.exists () && selected_variant)
3591 || (!gnu_variant_list.exists () && gnu_rep_part))
3592 && all_constant_pos)
3594 const int len = list_length (gnu_field_list);
3595 tree *field_arr = XALLOCAVEC (tree, len), t;
3596 int i;
3598 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3599 field_arr[i] = t;
3601 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3603 gnu_field_list = NULL_TREE;
3604 for (i = 0; i < len; i++)
3606 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3607 gnu_field_list = field_arr[i];
3611 /* If there is a variant list and no selected variant, we need
3612 to create the nest of variant parts from the old nest. */
3613 else if (gnu_variant_list.exists () && !selected_variant)
3615 tree new_variant_part
3616 = create_variant_part_from (gnu_variant_part,
3617 gnu_variant_list, gnu_type,
3618 gnu_pos_list, gnu_subst_list);
3619 DECL_CHAIN (new_variant_part) = gnu_field_list;
3620 gnu_field_list = new_variant_part;
3623 /* Now go through the entities again looking for Itypes that
3624 we have not elaborated but should (e.g., Etypes of fields
3625 that have Original_Components). */
3626 for (gnat_field = First_Entity (gnat_entity);
3627 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3628 if ((Ekind (gnat_field) == E_Discriminant
3629 || Ekind (gnat_field) == E_Component)
3630 && !present_gnu_tree (Etype (gnat_field)))
3631 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3633 /* Do not emit debug info for the type yet since we're going to
3634 modify it below. */
3635 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3636 false);
3637 compute_record_mode (gnu_type);
3639 /* See the E_Record_Type case for the rationale. */
3640 if (TYPE_MODE (gnu_type) != BLKmode
3641 && Is_By_Reference_Type (gnat_entity))
3642 SET_TYPE_MODE (gnu_type, BLKmode);
3644 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3646 /* Fill in locations of fields. */
3647 annotate_rep (gnat_entity, gnu_type);
3649 /* If debugging information is being written for the type, write
3650 a record that shows what we are a subtype of and also make a
3651 variable that indicates our size, if still variable. */
3652 if (debug_info_p)
3654 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3655 tree gnu_unpad_base_name = TYPE_NAME (gnu_unpad_base_type);
3656 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3658 if (TREE_CODE (gnu_unpad_base_name) == TYPE_DECL)
3659 gnu_unpad_base_name = DECL_NAME (gnu_unpad_base_name);
3661 TYPE_NAME (gnu_subtype_marker)
3662 = create_concat_name (gnat_entity, "XVS");
3663 finish_record_type (gnu_subtype_marker,
3664 create_field_decl (gnu_unpad_base_name,
3665 build_reference_type
3666 (gnu_unpad_base_type),
3667 gnu_subtype_marker,
3668 NULL_TREE, NULL_TREE,
3669 0, 0),
3670 0, true);
3672 add_parallel_type (gnu_type, gnu_subtype_marker);
3674 if (definition
3675 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3676 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3677 TYPE_SIZE_UNIT (gnu_subtype_marker)
3678 = create_var_decl (create_concat_name (gnat_entity,
3679 "XVZ"),
3680 NULL_TREE, sizetype, gnu_size_unit,
3681 false, false, false, false, NULL,
3682 gnat_entity);
3685 gnu_variant_list.release ();
3686 gnu_subst_list.release ();
3688 /* Now we can finalize it. */
3689 rest_of_record_type_compilation (gnu_type);
3692 /* Otherwise, go down all the components in the new type and make
3693 them equivalent to those in the base type. */
3694 else
3696 gnu_type = gnu_base_type;
3698 for (gnat_temp = First_Entity (gnat_entity);
3699 Present (gnat_temp);
3700 gnat_temp = Next_Entity (gnat_temp))
3701 if ((Ekind (gnat_temp) == E_Discriminant
3702 && !Is_Unchecked_Union (gnat_base_type))
3703 || Ekind (gnat_temp) == E_Component)
3704 save_gnu_tree (gnat_temp,
3705 gnat_to_gnu_field_decl
3706 (Original_Record_Component (gnat_temp)),
3707 false);
3710 break;
3712 case E_Access_Subprogram_Type:
3713 /* Use the special descriptor type for dispatch tables if needed,
3714 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3715 Note that we are only required to do so for static tables in
3716 order to be compatible with the C++ ABI, but Ada 2005 allows
3717 to extend library level tagged types at the local level so
3718 we do it in the non-static case as well. */
3719 if (TARGET_VTABLE_USES_DESCRIPTORS
3720 && Is_Dispatch_Table_Entity (gnat_entity))
3722 gnu_type = fdesc_type_node;
3723 gnu_size = TYPE_SIZE (gnu_type);
3724 break;
3727 /* ... fall through ... */
3729 case E_Anonymous_Access_Subprogram_Type:
3730 /* If we are not defining this entity, and we have incomplete
3731 entities being processed above us, make a dummy type and
3732 fill it in later. */
3733 if (!definition && defer_incomplete_level != 0)
3735 struct incomplete *p = XNEW (struct incomplete);
3737 gnu_type
3738 = build_pointer_type
3739 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3740 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3741 !Comes_From_Source (gnat_entity),
3742 debug_info_p, gnat_entity);
3743 this_made_decl = true;
3744 gnu_type = TREE_TYPE (gnu_decl);
3745 save_gnu_tree (gnat_entity, gnu_decl, false);
3746 saved = true;
3748 p->old_type = TREE_TYPE (gnu_type);
3749 p->full_type = Directly_Designated_Type (gnat_entity);
3750 p->next = defer_incomplete_list;
3751 defer_incomplete_list = p;
3752 break;
3755 /* ... fall through ... */
3757 case E_Allocator_Type:
3758 case E_Access_Type:
3759 case E_Access_Attribute_Type:
3760 case E_Anonymous_Access_Type:
3761 case E_General_Access_Type:
3763 /* The designated type and its equivalent type for gigi. */
3764 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3765 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3766 /* Whether it comes from a limited with. */
3767 bool is_from_limited_with
3768 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3769 && From_Limited_With (gnat_desig_equiv));
3770 /* The "full view" of the designated type. If this is an incomplete
3771 entity from a limited with, treat its non-limited view as the full
3772 view. Otherwise, if this is an incomplete or private type, use the
3773 full view. In the former case, we might point to a private type,
3774 in which case, we need its full view. Also, we want to look at the
3775 actual type used for the representation, so this takes a total of
3776 three steps. */
3777 Entity_Id gnat_desig_full_direct_first
3778 = (is_from_limited_with
3779 ? Non_Limited_View (gnat_desig_equiv)
3780 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3781 ? Full_View (gnat_desig_equiv) : Empty));
3782 Entity_Id gnat_desig_full_direct
3783 = ((is_from_limited_with
3784 && Present (gnat_desig_full_direct_first)
3785 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3786 ? Full_View (gnat_desig_full_direct_first)
3787 : gnat_desig_full_direct_first);
3788 Entity_Id gnat_desig_full
3789 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3790 /* The type actually used to represent the designated type, either
3791 gnat_desig_full or gnat_desig_equiv. */
3792 Entity_Id gnat_desig_rep;
3793 /* True if this is a pointer to an unconstrained array. */
3794 bool is_unconstrained_array;
3795 /* We want to know if we'll be seeing the freeze node for any
3796 incomplete type we may be pointing to. */
3797 bool in_main_unit
3798 = (Present (gnat_desig_full)
3799 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3800 : In_Extended_Main_Code_Unit (gnat_desig_type));
3801 /* True if we make a dummy type here. */
3802 bool made_dummy = false;
3803 /* The mode to be used for the pointer type. */
3804 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3805 /* The GCC type used for the designated type. */
3806 tree gnu_desig_type = NULL_TREE;
3808 if (!targetm.valid_pointer_mode (p_mode))
3809 p_mode = ptr_mode;
3811 /* If either the designated type or its full view is an unconstrained
3812 array subtype, replace it with the type it's a subtype of. This
3813 avoids problems with multiple copies of unconstrained array types.
3814 Likewise, if the designated type is a subtype of an incomplete
3815 record type, use the parent type to avoid order of elaboration
3816 issues. This can lose some code efficiency, but there is no
3817 alternative. */
3818 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3819 && !Is_Constrained (gnat_desig_equiv))
3820 gnat_desig_equiv = Etype (gnat_desig_equiv);
3821 if (Present (gnat_desig_full)
3822 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3823 && !Is_Constrained (gnat_desig_full))
3824 || (Ekind (gnat_desig_full) == E_Record_Subtype
3825 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3826 gnat_desig_full = Etype (gnat_desig_full);
3828 /* Set the type that's actually the representation of the designated
3829 type and also flag whether we have a unconstrained array. */
3830 gnat_desig_rep
3831 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3832 is_unconstrained_array
3833 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3835 /* If we are pointing to an incomplete type whose completion is an
3836 unconstrained array, make dummy fat and thin pointer types to it.
3837 Likewise if the type itself is dummy or an unconstrained array. */
3838 if (is_unconstrained_array
3839 && (Present (gnat_desig_full)
3840 || (present_gnu_tree (gnat_desig_equiv)
3841 && TYPE_IS_DUMMY_P
3842 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3843 || (!in_main_unit
3844 && defer_incomplete_level != 0
3845 && !present_gnu_tree (gnat_desig_equiv))
3846 || (in_main_unit
3847 && is_from_limited_with
3848 && Present (Freeze_Node (gnat_desig_equiv)))))
3850 if (present_gnu_tree (gnat_desig_rep))
3851 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3852 else
3854 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3855 made_dummy = true;
3858 /* If the call above got something that has a pointer, the pointer
3859 is our type. This could have happened either because the type
3860 was elaborated or because somebody else executed the code. */
3861 if (!TYPE_POINTER_TO (gnu_desig_type))
3862 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3863 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3866 /* If we already know what the full type is, use it. */
3867 else if (Present (gnat_desig_full)
3868 && present_gnu_tree (gnat_desig_full))
3869 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3871 /* Get the type of the thing we are to point to and build a pointer to
3872 it. If it is a reference to an incomplete or private type with a
3873 full view that is a record, make a dummy type node and get the
3874 actual type later when we have verified it is safe. */
3875 else if ((!in_main_unit
3876 && !present_gnu_tree (gnat_desig_equiv)
3877 && Present (gnat_desig_full)
3878 && !present_gnu_tree (gnat_desig_full)
3879 && Is_Record_Type (gnat_desig_full))
3880 /* Likewise if we are pointing to a record or array and we are
3881 to defer elaborating incomplete types. We do this as this
3882 access type may be the full view of a private type. Note
3883 that the unconstrained array case is handled above. */
3884 || ((!in_main_unit || imported_p)
3885 && defer_incomplete_level != 0
3886 && !present_gnu_tree (gnat_desig_equiv)
3887 && (Is_Record_Type (gnat_desig_rep)
3888 || Is_Array_Type (gnat_desig_rep)))
3889 /* If this is a reference from a limited_with type back to our
3890 main unit and there's a freeze node for it, either we have
3891 already processed the declaration and made the dummy type,
3892 in which case we just reuse the latter, or we have not yet,
3893 in which case we make the dummy type and it will be reused
3894 when the declaration is finally processed. In both cases,
3895 the pointer eventually created below will be automatically
3896 adjusted when the freeze node is processed. Note that the
3897 unconstrained array case is handled above. */
3898 || (in_main_unit
3899 && is_from_limited_with
3900 && Present (Freeze_Node (gnat_desig_rep))))
3902 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3903 made_dummy = true;
3906 /* Otherwise handle the case of a pointer to itself. */
3907 else if (gnat_desig_equiv == gnat_entity)
3909 gnu_type
3910 = build_pointer_type_for_mode (void_type_node, p_mode,
3911 No_Strict_Aliasing (gnat_entity));
3912 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3915 /* If expansion is disabled, the equivalent type of a concurrent type
3916 is absent, so build a dummy pointer type. */
3917 else if (type_annotate_only && No (gnat_desig_equiv))
3918 gnu_type = ptr_void_type_node;
3920 /* Finally, handle the default case where we can just elaborate our
3921 designated type. */
3922 else
3923 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3925 /* It is possible that a call to gnat_to_gnu_type above resolved our
3926 type. If so, just return it. */
3927 if (present_gnu_tree (gnat_entity))
3929 maybe_present = true;
3930 break;
3933 /* If we haven't done it yet, build the pointer type the usual way. */
3934 if (!gnu_type)
3936 /* Modify the designated type if we are pointing only to constant
3937 objects, but don't do it for unconstrained arrays. */
3938 if (Is_Access_Constant (gnat_entity)
3939 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3941 gnu_desig_type
3942 = build_qualified_type
3943 (gnu_desig_type,
3944 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3946 /* Some extra processing is required if we are building a
3947 pointer to an incomplete type (in the GCC sense). We might
3948 have such a type if we just made a dummy, or directly out
3949 of the call to gnat_to_gnu_type above if we are processing
3950 an access type for a record component designating the
3951 record type itself. */
3952 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3954 /* We must ensure that the pointer to variant we make will
3955 be processed by update_pointer_to when the initial type
3956 is completed. Pretend we made a dummy and let further
3957 processing act as usual. */
3958 made_dummy = true;
3960 /* We must ensure that update_pointer_to will not retrieve
3961 the dummy variant when building a properly qualified
3962 version of the complete type. We take advantage of the
3963 fact that get_qualified_type is requiring TYPE_NAMEs to
3964 match to influence build_qualified_type and then also
3965 update_pointer_to here. */
3966 TYPE_NAME (gnu_desig_type)
3967 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3971 gnu_type
3972 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3973 No_Strict_Aliasing (gnat_entity));
3976 /* If we are not defining this object and we have made a dummy pointer,
3977 save our current definition, evaluate the actual type, and replace
3978 the tentative type we made with the actual one. If we are to defer
3979 actually looking up the actual type, make an entry in the deferred
3980 list. If this is from a limited with, we may have to defer to the
3981 end of the current unit. */
3982 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3984 tree gnu_old_desig_type;
3986 if (TYPE_IS_FAT_POINTER_P (gnu_type))
3988 gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
3989 if (esize == POINTER_SIZE)
3990 gnu_type = build_pointer_type
3991 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
3993 else
3994 gnu_old_desig_type = TREE_TYPE (gnu_type);
3996 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3997 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3998 !Comes_From_Source (gnat_entity),
3999 debug_info_p, gnat_entity);
4000 this_made_decl = true;
4001 gnu_type = TREE_TYPE (gnu_decl);
4002 save_gnu_tree (gnat_entity, gnu_decl, false);
4003 saved = true;
4005 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
4006 update gnu_old_desig_type directly, in which case it will not be
4007 a dummy type any more when we get into update_pointer_to.
4009 This can happen e.g. when the designated type is a record type,
4010 because their elaboration starts with an initial node from
4011 make_dummy_type, which may be the same node as the one we got.
4013 Besides, variants of this non-dummy type might have been created
4014 along the way. update_pointer_to is expected to properly take
4015 care of those situations. */
4016 if (defer_incomplete_level == 0 && !is_from_limited_with)
4018 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
4019 gnat_to_gnu_type (gnat_desig_equiv));
4021 else
4023 struct incomplete *p = XNEW (struct incomplete);
4024 struct incomplete **head
4025 = (is_from_limited_with
4026 ? &defer_limited_with : &defer_incomplete_list);
4027 p->old_type = gnu_old_desig_type;
4028 p->full_type = gnat_desig_equiv;
4029 p->next = *head;
4030 *head = p;
4034 break;
4036 case E_Access_Protected_Subprogram_Type:
4037 case E_Anonymous_Access_Protected_Subprogram_Type:
4038 if (type_annotate_only && No (gnat_equiv_type))
4039 gnu_type = ptr_void_type_node;
4040 else
4042 /* The run-time representation is the equivalent type. */
4043 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4044 maybe_present = true;
4047 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4048 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4049 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
4050 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
4051 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4052 NULL_TREE, 0);
4054 break;
4056 case E_Access_Subtype:
4058 /* We treat this as identical to its base type; any constraint is
4059 meaningful only to the front-end.
4061 The designated type must be elaborated as well, if it does
4062 not have its own freeze node. Designated (sub)types created
4063 for constrained components of records with discriminants are
4064 not frozen by the front-end and thus not elaborated by gigi,
4065 because their use may appear before the base type is frozen,
4066 and because it is not clear that they are needed anywhere in
4067 gigi. With the current model, there is no correct place where
4068 they could be elaborated. */
4070 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
4071 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4072 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4073 && Is_Frozen (Directly_Designated_Type (gnat_entity))
4074 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4076 /* If we are not defining this entity, and we have incomplete
4077 entities being processed above us, make a dummy type and
4078 elaborate it later. */
4079 if (!definition && defer_incomplete_level != 0)
4081 struct incomplete *p = XNEW (struct incomplete);
4083 p->old_type
4084 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4085 p->full_type = Directly_Designated_Type (gnat_entity);
4086 p->next = defer_incomplete_list;
4087 defer_incomplete_list = p;
4089 else if (!IN (Ekind (Base_Type
4090 (Directly_Designated_Type (gnat_entity))),
4091 Incomplete_Or_Private_Kind))
4092 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4093 NULL_TREE, 0);
4096 maybe_present = true;
4097 break;
4099 /* Subprogram Entities
4101 The following access functions are defined for subprograms:
4103 Etype Return type or Standard_Void_Type.
4104 First_Formal The first formal parameter.
4105 Is_Imported Indicates that the subprogram has appeared in
4106 an INTERFACE or IMPORT pragma. For now we
4107 assume that the external language is C.
4108 Is_Exported Likewise but for an EXPORT pragma.
4109 Is_Inlined True if the subprogram is to be inlined.
4111 Each parameter is first checked by calling must_pass_by_ref on its
4112 type to determine if it is passed by reference. For parameters which
4113 are copied in, if they are Ada In Out or Out parameters, their return
4114 value becomes part of a record which becomes the return type of the
4115 function (C function - note that this applies only to Ada procedures
4116 so there is no Ada return type). Additional code to store back the
4117 parameters will be generated on the caller side. This transformation
4118 is done here, not in the front-end.
4120 The intended result of the transformation can be seen from the
4121 equivalent source rewritings that follow:
4123 struct temp {int a,b};
4124 procedure P (A,B: In Out ...) is temp P (int A,B)
4125 begin {
4126 .. ..
4127 end P; return {A,B};
4130 temp t;
4131 P(X,Y); t = P(X,Y);
4132 X = t.a , Y = t.b;
4134 For subprogram types we need to perform mainly the same conversions to
4135 GCC form that are needed for procedures and function declarations. The
4136 only difference is that at the end, we make a type declaration instead
4137 of a function declaration. */
4139 case E_Subprogram_Type:
4140 case E_Function:
4141 case E_Procedure:
4143 /* The type returned by a function or else Standard_Void_Type for a
4144 procedure. */
4145 Entity_Id gnat_return_type = Etype (gnat_entity);
4146 tree gnu_return_type;
4147 /* The first GCC parameter declaration (a PARM_DECL node). The
4148 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4149 actually is the head of this parameter list. */
4150 tree gnu_param_list = NULL_TREE;
4151 /* Likewise for the stub associated with an exported procedure. */
4152 tree gnu_stub_param_list = NULL_TREE;
4153 /* Non-null for subprograms containing parameters passed by copy-in
4154 copy-out (Ada In Out or Out parameters not passed by reference),
4155 in which case it is the list of nodes used to specify the values
4156 of the In Out/Out parameters that are returned as a record upon
4157 procedure return. The TREE_PURPOSE of an element of this list is
4158 a field of the record and the TREE_VALUE is the PARM_DECL
4159 corresponding to that field. This list will be saved in the
4160 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4161 tree gnu_cico_list = NULL_TREE;
4162 /* List of fields in return type of procedure with copy-in copy-out
4163 parameters. */
4164 tree gnu_field_list = NULL_TREE;
4165 /* If an import pragma asks to map this subprogram to a GCC builtin,
4166 this is the builtin DECL node. */
4167 tree gnu_builtin_decl = NULL_TREE;
4168 /* For the stub associated with an exported procedure. */
4169 tree gnu_stub_type = NULL_TREE, gnu_stub_name = NULL_TREE;
4170 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4171 Entity_Id gnat_param;
4172 enum inline_status_t inline_status
4173 = Has_Pragma_No_Inline (gnat_entity)
4174 ? is_suppressed
4175 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4176 bool public_flag = Is_Public (gnat_entity) || imported_p;
4177 bool extern_flag
4178 = (Is_Public (gnat_entity) && !definition) || imported_p;
4179 bool artificial_flag = !Comes_From_Source (gnat_entity);
4180 /* The semantics of "pure" in Ada essentially matches that of "const"
4181 in the back-end. In particular, both properties are orthogonal to
4182 the "nothrow" property if the EH circuitry is explicit in the
4183 internal representation of the back-end. If we are to completely
4184 hide the EH circuitry from it, we need to declare that calls to pure
4185 Ada subprograms that can throw have side effects since they can
4186 trigger an "abnormal" transfer of control flow; thus they can be
4187 neither "const" nor "pure" in the back-end sense. */
4188 bool const_flag
4189 = (Exception_Mechanism == Back_End_Exceptions
4190 && Is_Pure (gnat_entity));
4191 bool volatile_flag = No_Return (gnat_entity);
4192 bool return_by_direct_ref_p = false;
4193 bool return_by_invisi_ref_p = false;
4194 bool return_unconstrained_p = false;
4195 bool has_stub = false;
4196 int parmnum;
4198 /* A parameter may refer to this type, so defer completion of any
4199 incomplete types. */
4200 if (kind == E_Subprogram_Type && !definition)
4202 defer_incomplete_level++;
4203 this_deferred = true;
4206 /* If the subprogram has an alias, it is probably inherited, so
4207 we can use the original one. If the original "subprogram"
4208 is actually an enumeration literal, it may be the first use
4209 of its type, so we must elaborate that type now. */
4210 if (Present (Alias (gnat_entity)))
4212 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4213 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
4215 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
4217 /* Elaborate any Itypes in the parameters of this entity. */
4218 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4219 Present (gnat_temp);
4220 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4221 if (Is_Itype (Etype (gnat_temp)))
4222 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4224 break;
4227 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4228 corresponding DECL node. Proper generation of calls later on need
4229 proper parameter associations so we don't "break;" here. */
4230 if (Convention (gnat_entity) == Convention_Intrinsic
4231 && Present (Interface_Name (gnat_entity)))
4233 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4235 /* Inability to find the builtin decl most often indicates a
4236 genuine mistake, but imports of unregistered intrinsics are
4237 sometimes issued on purpose to allow hooking in alternate
4238 bodies. We post a warning conditioned on Wshadow in this case,
4239 to let developers be notified on demand without risking false
4240 positives with common default sets of options. */
4242 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
4243 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4246 /* ??? What if we don't find the builtin node above ? warn ? err ?
4247 In the current state we neither warn nor err, and calls will just
4248 be handled as for regular subprograms. */
4250 /* Look into the return type and get its associated GCC tree. If it
4251 is not void, compute various flags for the subprogram type. */
4252 if (Ekind (gnat_return_type) == E_Void)
4253 gnu_return_type = void_type_node;
4254 else
4256 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4257 context may now appear in parameter and result profiles. If
4258 we are only annotating types, break circularities here. */
4259 if (type_annotate_only
4260 && IN (Ekind (gnat_return_type), Incomplete_Kind)
4261 && From_Limited_With (gnat_return_type)
4262 && In_Extended_Main_Code_Unit
4263 (Non_Limited_View (gnat_return_type))
4264 && !present_gnu_tree (Non_Limited_View (gnat_return_type)))
4265 gnu_return_type = ptr_void_type_node;
4266 else
4267 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4269 /* If this function returns by reference, make the actual return
4270 type the pointer type and make a note of that. */
4271 if (Returns_By_Ref (gnat_entity))
4273 gnu_return_type = build_pointer_type (gnu_return_type);
4274 return_by_direct_ref_p = true;
4277 /* If we are supposed to return an unconstrained array type, make
4278 the actual return type the fat pointer type. */
4279 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4281 gnu_return_type = TREE_TYPE (gnu_return_type);
4282 return_unconstrained_p = true;
4285 /* Likewise, if the return type requires a transient scope, the
4286 return value will be allocated on the secondary stack so the
4287 actual return type is the pointer type. */
4288 else if (Requires_Transient_Scope (gnat_return_type))
4290 gnu_return_type = build_pointer_type (gnu_return_type);
4291 return_unconstrained_p = true;
4294 /* If the Mechanism is By_Reference, ensure this function uses the
4295 target's by-invisible-reference mechanism, which may not be the
4296 same as above (e.g. it might be passing an extra parameter). */
4297 else if (kind == E_Function
4298 && Mechanism (gnat_entity) == By_Reference)
4299 return_by_invisi_ref_p = true;
4301 /* Likewise, if the return type is itself By_Reference. */
4302 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
4303 return_by_invisi_ref_p = true;
4305 /* If the type is a padded type and the underlying type would not
4306 be passed by reference or the function has a foreign convention,
4307 return the underlying type. */
4308 else if (TYPE_IS_PADDING_P (gnu_return_type)
4309 && (!default_pass_by_ref
4310 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4311 || Has_Foreign_Convention (gnat_entity)))
4312 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4314 /* If the return type is unconstrained, that means it must have a
4315 maximum size. Use the padded type as the effective return type.
4316 And ensure the function uses the target's by-invisible-reference
4317 mechanism to avoid copying too much data when it returns. */
4318 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4320 tree orig_type = gnu_return_type;
4322 gnu_return_type
4323 = maybe_pad_type (gnu_return_type,
4324 max_size (TYPE_SIZE (gnu_return_type),
4325 true),
4326 0, gnat_entity, false, false, false, true);
4328 /* Declare it now since it will never be declared otherwise.
4329 This is necessary to ensure that its subtrees are properly
4330 marked. */
4331 if (gnu_return_type != orig_type
4332 && !DECL_P (TYPE_NAME (gnu_return_type)))
4333 create_type_decl (TYPE_NAME (gnu_return_type),
4334 gnu_return_type, true, debug_info_p,
4335 gnat_entity);
4337 return_by_invisi_ref_p = true;
4340 /* If the return type has a size that overflows, we cannot have
4341 a function that returns that type. This usage doesn't make
4342 sense anyway, so give an error here. */
4343 if (TYPE_SIZE_UNIT (gnu_return_type)
4344 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
4345 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
4347 post_error ("cannot return type whose size overflows",
4348 gnat_entity);
4349 gnu_return_type = copy_node (gnu_return_type);
4350 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4351 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4352 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4353 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4357 /* Loop over the parameters and get their associated GCC tree. While
4358 doing this, build a copy-in copy-out structure if we need one. */
4359 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4360 Present (gnat_param);
4361 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4363 Entity_Id gnat_param_type = Etype (gnat_param);
4364 tree gnu_param_name = get_entity_name (gnat_param);
4365 tree gnu_param_type, gnu_param, gnu_field;
4366 Mechanism_Type mech = Mechanism (gnat_param);
4367 bool copy_in_copy_out = false, fake_param_type;
4369 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4370 context may now appear in parameter and result profiles. If
4371 we are only annotating types, break circularities here. */
4372 if (type_annotate_only
4373 && IN (Ekind (gnat_param_type), Incomplete_Kind)
4374 && From_Limited_With (Etype (gnat_param_type))
4375 && In_Extended_Main_Code_Unit
4376 (Non_Limited_View (gnat_param_type))
4377 && !present_gnu_tree (Non_Limited_View (gnat_param_type)))
4379 gnu_param_type = ptr_void_type_node;
4380 fake_param_type = true;
4382 else
4384 gnu_param_type = gnat_to_gnu_type (gnat_param_type);
4385 fake_param_type = false;
4388 /* Builtins are expanded inline and there is no real call sequence
4389 involved. So the type expected by the underlying expander is
4390 always the type of each argument "as is". */
4391 if (gnu_builtin_decl)
4392 mech = By_Copy;
4393 /* Handle the first parameter of a valued procedure specially. */
4394 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4395 mech = By_Copy_Return;
4396 /* Otherwise, see if a Mechanism was supplied that forced this
4397 parameter to be passed one way or another. */
4398 else if (mech == Default
4399 || mech == By_Copy || mech == By_Reference)
4401 else if (By_Descriptor_Last <= mech && mech <= By_Descriptor)
4402 mech = By_Descriptor;
4404 else if (By_Short_Descriptor_Last <= mech &&
4405 mech <= By_Short_Descriptor)
4406 mech = By_Short_Descriptor;
4408 else if (mech > 0)
4410 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4411 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4412 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4413 mech))
4414 mech = By_Reference;
4415 else
4416 mech = By_Copy;
4418 else
4420 post_error ("unsupported mechanism for&", gnat_param);
4421 mech = Default;
4424 /* Do not call gnat_to_gnu_param for a fake parameter type since
4425 it will try to use the real type again. */
4426 if (fake_param_type)
4428 if (Ekind (gnat_param) == E_Out_Parameter)
4429 gnu_param = NULL_TREE;
4430 else
4432 gnu_param
4433 = create_param_decl (gnu_param_name, gnu_param_type,
4434 false);
4435 Set_Mechanism (gnat_param,
4436 mech == Default ? By_Copy : mech);
4437 if (Ekind (gnat_param) == E_In_Out_Parameter)
4438 copy_in_copy_out = true;
4441 else
4442 gnu_param
4443 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4444 Has_Foreign_Convention (gnat_entity),
4445 &copy_in_copy_out);
4447 /* We are returned either a PARM_DECL or a type if no parameter
4448 needs to be passed; in either case, adjust the type. */
4449 if (DECL_P (gnu_param))
4450 gnu_param_type = TREE_TYPE (gnu_param);
4451 else
4453 gnu_param_type = gnu_param;
4454 gnu_param = NULL_TREE;
4457 /* The failure of this assertion will very likely come from an
4458 order of elaboration issue for the type of the parameter. */
4459 gcc_assert (kind == E_Subprogram_Type
4460 || !TYPE_IS_DUMMY_P (gnu_param_type)
4461 || type_annotate_only);
4463 if (gnu_param)
4465 /* If it's an exported subprogram, we build a parameter list
4466 in parallel, in case we need to emit a stub for it. */
4467 if (Is_Exported (gnat_entity))
4469 gnu_stub_param_list
4470 = chainon (gnu_param, gnu_stub_param_list);
4471 /* Change By_Descriptor parameter to By_Reference for
4472 the internal version of an exported subprogram. */
4473 if (mech == By_Descriptor || mech == By_Short_Descriptor)
4475 gnu_param
4476 = gnat_to_gnu_param (gnat_param, By_Reference,
4477 gnat_entity, false,
4478 &copy_in_copy_out);
4479 has_stub = true;
4481 else
4482 gnu_param = copy_node (gnu_param);
4485 gnu_param_list = chainon (gnu_param, gnu_param_list);
4486 Sloc_to_locus (Sloc (gnat_param),
4487 &DECL_SOURCE_LOCATION (gnu_param));
4488 save_gnu_tree (gnat_param, gnu_param, false);
4490 /* If a parameter is a pointer, this function may modify
4491 memory through it and thus shouldn't be considered
4492 a const function. Also, the memory may be modified
4493 between two calls, so they can't be CSE'ed. The latter
4494 case also handles by-ref parameters. */
4495 if (POINTER_TYPE_P (gnu_param_type)
4496 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4497 const_flag = false;
4500 if (copy_in_copy_out)
4502 if (!gnu_cico_list)
4504 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4506 /* If this is a function, we also need a field for the
4507 return value to be placed. */
4508 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4510 gnu_field
4511 = create_field_decl (get_identifier ("RETVAL"),
4512 gnu_return_type,
4513 gnu_new_ret_type, NULL_TREE,
4514 NULL_TREE, 0, 0);
4515 Sloc_to_locus (Sloc (gnat_entity),
4516 &DECL_SOURCE_LOCATION (gnu_field));
4517 gnu_field_list = gnu_field;
4518 gnu_cico_list
4519 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4522 gnu_return_type = gnu_new_ret_type;
4523 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4524 /* Set a default alignment to speed up accesses. But we
4525 shouldn't increase the size of the structure too much,
4526 lest it doesn't fit in return registers anymore. */
4527 TYPE_ALIGN (gnu_return_type)
4528 = get_mode_alignment (ptr_mode);
4531 gnu_field
4532 = create_field_decl (gnu_param_name, gnu_param_type,
4533 gnu_return_type, NULL_TREE, NULL_TREE,
4534 0, 0);
4535 Sloc_to_locus (Sloc (gnat_param),
4536 &DECL_SOURCE_LOCATION (gnu_field));
4537 DECL_CHAIN (gnu_field) = gnu_field_list;
4538 gnu_field_list = gnu_field;
4539 gnu_cico_list
4540 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4544 if (gnu_cico_list)
4546 /* If we have a CICO list but it has only one entry, we convert
4547 this function into a function that returns this object. */
4548 if (list_length (gnu_cico_list) == 1)
4549 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4551 /* Do not finalize the return type if the subprogram is stubbed
4552 since structures are incomplete for the back-end. */
4553 else if (Convention (gnat_entity) != Convention_Stubbed)
4555 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4556 0, false);
4558 /* Try to promote the mode of the return type if it is passed
4559 in registers, again to speed up accesses. */
4560 if (TYPE_MODE (gnu_return_type) == BLKmode
4561 && !targetm.calls.return_in_memory (gnu_return_type,
4562 NULL_TREE))
4564 unsigned int size
4565 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
4566 unsigned int i = BITS_PER_UNIT;
4567 enum machine_mode mode;
4569 while (i < size)
4570 i <<= 1;
4571 mode = mode_for_size (i, MODE_INT, 0);
4572 if (mode != BLKmode)
4574 SET_TYPE_MODE (gnu_return_type, mode);
4575 TYPE_ALIGN (gnu_return_type)
4576 = GET_MODE_ALIGNMENT (mode);
4577 TYPE_SIZE (gnu_return_type)
4578 = bitsize_int (GET_MODE_BITSIZE (mode));
4579 TYPE_SIZE_UNIT (gnu_return_type)
4580 = size_int (GET_MODE_SIZE (mode));
4584 if (debug_info_p)
4585 rest_of_record_type_compilation (gnu_return_type);
4589 /* Deal with platform-specific calling conventions. */
4590 if (Has_Stdcall_Convention (gnat_entity))
4591 prepend_one_attribute
4592 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4593 get_identifier ("stdcall"), NULL_TREE,
4594 gnat_entity);
4595 else if (Has_Thiscall_Convention (gnat_entity))
4596 prepend_one_attribute
4597 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4598 get_identifier ("thiscall"), NULL_TREE,
4599 gnat_entity);
4601 /* If we should request stack realignment for a foreign convention
4602 subprogram, do so. Note that this applies to task entry points
4603 in particular. */
4604 if (FOREIGN_FORCE_REALIGN_STACK
4605 && Has_Foreign_Convention (gnat_entity))
4606 prepend_one_attribute
4607 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4608 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4609 gnat_entity);
4611 /* Deal with a pragma Linker_Section on a subprogram. */
4612 if ((kind == E_Function || kind == E_Procedure)
4613 && Present (Linker_Section_Pragma (gnat_entity)))
4614 prepend_one_attribute_pragma (&attr_list,
4615 Linker_Section_Pragma (gnat_entity));
4617 /* The lists have been built in reverse. */
4618 gnu_param_list = nreverse (gnu_param_list);
4619 if (has_stub)
4620 gnu_stub_param_list = nreverse (gnu_stub_param_list);
4621 gnu_cico_list = nreverse (gnu_cico_list);
4623 if (kind == E_Function)
4624 Set_Mechanism (gnat_entity, return_unconstrained_p
4625 || return_by_direct_ref_p
4626 || return_by_invisi_ref_p
4627 ? By_Reference : By_Copy);
4628 gnu_type
4629 = create_subprog_type (gnu_return_type, gnu_param_list,
4630 gnu_cico_list, return_unconstrained_p,
4631 return_by_direct_ref_p,
4632 return_by_invisi_ref_p);
4634 if (has_stub)
4635 gnu_stub_type
4636 = create_subprog_type (gnu_return_type, gnu_stub_param_list,
4637 gnu_cico_list, return_unconstrained_p,
4638 return_by_direct_ref_p,
4639 return_by_invisi_ref_p);
4641 /* A subprogram (something that doesn't return anything) shouldn't
4642 be considered const since there would be no reason for such a
4643 subprogram. Note that procedures with Out (or In Out) parameters
4644 have already been converted into a function with a return type. */
4645 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4646 const_flag = false;
4648 gnu_type
4649 = build_qualified_type (gnu_type,
4650 TYPE_QUALS (gnu_type)
4651 | (TYPE_QUAL_CONST * const_flag)
4652 | (TYPE_QUAL_VOLATILE * volatile_flag));
4654 if (has_stub)
4655 gnu_stub_type
4656 = build_qualified_type (gnu_stub_type,
4657 TYPE_QUALS (gnu_stub_type)
4658 | (TYPE_QUAL_CONST * const_flag)
4659 | (TYPE_QUAL_VOLATILE * volatile_flag));
4661 /* If we have a builtin decl for that function, use it. Check if the
4662 profiles are compatible and warn if they are not. The checker is
4663 expected to post extra diagnostics in this case. */
4664 if (gnu_builtin_decl)
4666 intrin_binding_t inb;
4668 inb.gnat_entity = gnat_entity;
4669 inb.ada_fntype = gnu_type;
4670 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4672 if (!intrin_profiles_compatible_p (&inb))
4673 post_error
4674 ("?profile of& doesn''t match the builtin it binds!",
4675 gnat_entity);
4677 gnu_decl = gnu_builtin_decl;
4678 gnu_type = TREE_TYPE (gnu_builtin_decl);
4679 break;
4682 /* If there was no specified Interface_Name and the external and
4683 internal names of the subprogram are the same, only use the
4684 internal name to allow disambiguation of nested subprograms. */
4685 if (No (Interface_Name (gnat_entity))
4686 && gnu_ext_name == gnu_entity_name)
4687 gnu_ext_name = NULL_TREE;
4689 /* If we are defining the subprogram and it has an Address clause
4690 we must get the address expression from the saved GCC tree for the
4691 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4692 the address expression here since the front-end has guaranteed
4693 in that case that the elaboration has no effects. If there is
4694 an Address clause and we are not defining the object, just
4695 make it a constant. */
4696 if (Present (Address_Clause (gnat_entity)))
4698 tree gnu_address = NULL_TREE;
4700 if (definition)
4701 gnu_address
4702 = (present_gnu_tree (gnat_entity)
4703 ? get_gnu_tree (gnat_entity)
4704 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4706 save_gnu_tree (gnat_entity, NULL_TREE, false);
4708 /* Convert the type of the object to a reference type that can
4709 alias everything as per 13.3(19). */
4710 gnu_type
4711 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4712 if (gnu_address)
4713 gnu_address = convert (gnu_type, gnu_address);
4715 gnu_decl
4716 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4717 gnu_address, false, Is_Public (gnat_entity),
4718 extern_flag, false, NULL, gnat_entity);
4719 DECL_BY_REF_P (gnu_decl) = 1;
4722 else if (kind == E_Subprogram_Type)
4724 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4725 gnu_decl
4726 = create_type_decl (gnu_entity_name, gnu_type, artificial_flag,
4727 debug_info_p, gnat_entity);
4729 else
4731 if (has_stub)
4733 gnu_stub_name = gnu_ext_name;
4734 gnu_ext_name = create_concat_name (gnat_entity, "internal");
4735 public_flag = false;
4736 artificial_flag = true;
4739 gnu_decl
4740 = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4741 gnu_param_list, inline_status,
4742 public_flag, extern_flag, artificial_flag,
4743 attr_list, gnat_entity);
4744 if (has_stub)
4746 tree gnu_stub_decl
4747 = create_subprog_decl (gnu_entity_name, gnu_stub_name,
4748 gnu_stub_type, gnu_stub_param_list,
4749 inline_status, true, extern_flag,
4750 false, attr_list, gnat_entity);
4751 SET_DECL_FUNCTION_STUB (gnu_decl, gnu_stub_decl);
4754 /* This is unrelated to the stub built right above. */
4755 DECL_STUBBED_P (gnu_decl)
4756 = Convention (gnat_entity) == Convention_Stubbed;
4759 break;
4761 case E_Incomplete_Type:
4762 case E_Incomplete_Subtype:
4763 case E_Private_Type:
4764 case E_Private_Subtype:
4765 case E_Limited_Private_Type:
4766 case E_Limited_Private_Subtype:
4767 case E_Record_Type_With_Private:
4768 case E_Record_Subtype_With_Private:
4770 /* Get the "full view" of this entity. If this is an incomplete
4771 entity from a limited with, treat its non-limited view as the
4772 full view. Otherwise, use either the full view or the underlying
4773 full view, whichever is present. This is used in all the tests
4774 below. */
4775 Entity_Id full_view
4776 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity))
4777 ? Non_Limited_View (gnat_entity)
4778 : Present (Full_View (gnat_entity))
4779 ? Full_View (gnat_entity)
4780 : Underlying_Full_View (gnat_entity);
4782 /* If this is an incomplete type with no full view, it must be a Taft
4783 Amendment type, in which case we return a dummy type. Otherwise,
4784 just get the type from its Etype. */
4785 if (No (full_view))
4787 if (kind == E_Incomplete_Type)
4789 gnu_type = make_dummy_type (gnat_entity);
4790 gnu_decl = TYPE_STUB_DECL (gnu_type);
4792 else
4794 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4795 NULL_TREE, 0);
4796 maybe_present = true;
4798 break;
4801 /* If we already made a type for the full view, reuse it. */
4802 else if (present_gnu_tree (full_view))
4804 gnu_decl = get_gnu_tree (full_view);
4805 break;
4808 /* Otherwise, if we are not defining the type now, get the type
4809 from the full view. But always get the type from the full view
4810 for define on use types, since otherwise we won't see them! */
4811 else if (!definition
4812 || (Is_Itype (full_view) && No (Freeze_Node (gnat_entity)))
4813 || (Is_Itype (gnat_entity) && No (Freeze_Node (full_view))))
4815 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4816 maybe_present = true;
4817 break;
4820 /* For incomplete types, make a dummy type entry which will be
4821 replaced later. Save it as the full declaration's type so
4822 we can do any needed updates when we see it. */
4823 gnu_type = make_dummy_type (gnat_entity);
4824 gnu_decl = TYPE_STUB_DECL (gnu_type);
4825 if (Has_Completion_In_Body (gnat_entity))
4826 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4827 save_gnu_tree (full_view, gnu_decl, 0);
4828 break;
4831 case E_Class_Wide_Type:
4832 /* Class-wide types are always transformed into their root type. */
4833 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4834 maybe_present = true;
4835 break;
4837 case E_Task_Type:
4838 case E_Task_Subtype:
4839 case E_Protected_Type:
4840 case E_Protected_Subtype:
4841 /* Concurrent types are always transformed into their record type. */
4842 if (type_annotate_only && No (gnat_equiv_type))
4843 gnu_type = void_type_node;
4844 else
4845 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4846 maybe_present = true;
4847 break;
4849 case E_Label:
4850 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4851 break;
4853 case E_Block:
4854 case E_Loop:
4855 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4856 we've already saved it, so we don't try to. */
4857 gnu_decl = error_mark_node;
4858 saved = true;
4859 break;
4861 case E_Abstract_State:
4862 /* This is a SPARK annotation that only reaches here when compiling in
4863 ASIS mode and has no characteristics to annotate. */
4864 gcc_assert (type_annotate_only);
4865 return error_mark_node;
4867 default:
4868 gcc_unreachable ();
4871 /* If we had a case where we evaluated another type and it might have
4872 defined this one, handle it here. */
4873 if (maybe_present && present_gnu_tree (gnat_entity))
4875 gnu_decl = get_gnu_tree (gnat_entity);
4876 saved = true;
4879 /* If we are processing a type and there is either no decl for it or
4880 we just made one, do some common processing for the type, such as
4881 handling alignment and possible padding. */
4882 if (is_type && (!gnu_decl || this_made_decl))
4884 /* Process the attributes, if not already done. Note that the type is
4885 already defined so we cannot pass true for IN_PLACE here. */
4886 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4888 /* Tell the middle-end that objects of tagged types are guaranteed to
4889 be properly aligned. This is necessary because conversions to the
4890 class-wide type are translated into conversions to the root type,
4891 which can be less aligned than some of its derived types. */
4892 if (Is_Tagged_Type (gnat_entity)
4893 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4894 TYPE_ALIGN_OK (gnu_type) = 1;
4896 /* Record whether the type is passed by reference. */
4897 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4898 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4900 /* ??? Don't set the size for a String_Literal since it is either
4901 confirming or we don't handle it properly (if the low bound is
4902 non-constant). */
4903 if (!gnu_size && kind != E_String_Literal_Subtype)
4905 Uint gnat_size = Known_Esize (gnat_entity)
4906 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4907 gnu_size
4908 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4909 false, Has_Size_Clause (gnat_entity));
4912 /* If a size was specified, see if we can make a new type of that size
4913 by rearranging the type, for example from a fat to a thin pointer. */
4914 if (gnu_size)
4916 gnu_type
4917 = make_type_from_size (gnu_type, gnu_size,
4918 Has_Biased_Representation (gnat_entity));
4920 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4921 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4922 gnu_size = NULL_TREE;
4925 /* If the alignment hasn't already been processed and this is
4926 not an unconstrained array, see if an alignment is specified.
4927 If not, we pick a default alignment for atomic objects. */
4928 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4930 else if (Known_Alignment (gnat_entity))
4932 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4933 TYPE_ALIGN (gnu_type));
4935 /* Warn on suspiciously large alignments. This should catch
4936 errors about the (alignment,byte)/(size,bit) discrepancy. */
4937 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4939 tree size;
4941 /* If a size was specified, take it into account. Otherwise
4942 use the RM size for records or unions as the type size has
4943 already been adjusted to the alignment. */
4944 if (gnu_size)
4945 size = gnu_size;
4946 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4947 && !TYPE_FAT_POINTER_P (gnu_type))
4948 size = rm_size (gnu_type);
4949 else
4950 size = TYPE_SIZE (gnu_type);
4952 /* Consider an alignment as suspicious if the alignment/size
4953 ratio is greater or equal to the byte/bit ratio. */
4954 if (tree_fits_uhwi_p (size)
4955 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4956 post_error_ne ("?suspiciously large alignment specified for&",
4957 Expression (Alignment_Clause (gnat_entity)),
4958 gnat_entity);
4961 else if (Is_Atomic (gnat_entity) && !gnu_size
4962 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4963 && integer_pow2p (TYPE_SIZE (gnu_type)))
4964 align = MIN (BIGGEST_ALIGNMENT,
4965 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4966 else if (Is_Atomic (gnat_entity) && gnu_size
4967 && tree_fits_uhwi_p (gnu_size)
4968 && integer_pow2p (gnu_size))
4969 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4971 /* See if we need to pad the type. If we did, and made a record,
4972 the name of the new type may be changed. So get it back for
4973 us when we make the new TYPE_DECL below. */
4974 if (gnu_size || align > 0)
4975 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4976 false, !gnu_decl, definition, false);
4978 if (TYPE_IS_PADDING_P (gnu_type))
4980 gnu_entity_name = TYPE_NAME (gnu_type);
4981 if (TREE_CODE (gnu_entity_name) == TYPE_DECL)
4982 gnu_entity_name = DECL_NAME (gnu_entity_name);
4985 /* Now set the RM size of the type. We cannot do it before padding
4986 because we need to accept arbitrary RM sizes on integral types. */
4987 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4989 /* If we are at global level, GCC will have applied variable_size to
4990 the type, but that won't have done anything. So, if it's not
4991 a constant or self-referential, call elaborate_expression_1 to
4992 make a variable for the size rather than calculating it each time.
4993 Handle both the RM size and the actual size. */
4994 if (global_bindings_p ()
4995 && TYPE_SIZE (gnu_type)
4996 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4997 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4999 tree size = TYPE_SIZE (gnu_type);
5001 TYPE_SIZE (gnu_type)
5002 = elaborate_expression_1 (size, gnat_entity,
5003 get_identifier ("SIZE"),
5004 definition, false);
5006 /* ??? For now, store the size as a multiple of the alignment in
5007 bytes so that we can see the alignment from the tree. */
5008 TYPE_SIZE_UNIT (gnu_type)
5009 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
5010 get_identifier ("SIZE_A_UNIT"),
5011 definition, false,
5012 TYPE_ALIGN (gnu_type));
5014 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
5015 may not be marked by the call to create_type_decl below. */
5016 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
5018 if (TREE_CODE (gnu_type) == RECORD_TYPE)
5020 tree variant_part = get_variant_part (gnu_type);
5021 tree ada_size = TYPE_ADA_SIZE (gnu_type);
5023 if (variant_part)
5025 tree union_type = TREE_TYPE (variant_part);
5026 tree offset = DECL_FIELD_OFFSET (variant_part);
5028 /* If the position of the variant part is constant, subtract
5029 it from the size of the type of the parent to get the new
5030 size. This manual CSE reduces the data size. */
5031 if (TREE_CODE (offset) == INTEGER_CST)
5033 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
5034 TYPE_SIZE (union_type)
5035 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
5036 bit_from_pos (offset, bitpos));
5037 TYPE_SIZE_UNIT (union_type)
5038 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
5039 byte_from_pos (offset, bitpos));
5041 else
5043 TYPE_SIZE (union_type)
5044 = elaborate_expression_1 (TYPE_SIZE (union_type),
5045 gnat_entity,
5046 get_identifier ("VSIZE"),
5047 definition, false);
5049 /* ??? For now, store the size as a multiple of the
5050 alignment in bytes so that we can see the alignment
5051 from the tree. */
5052 TYPE_SIZE_UNIT (union_type)
5053 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
5054 gnat_entity,
5055 get_identifier
5056 ("VSIZE_A_UNIT"),
5057 definition, false,
5058 TYPE_ALIGN (union_type));
5060 /* ??? For now, store the offset as a multiple of the
5061 alignment in bytes so that we can see the alignment
5062 from the tree. */
5063 DECL_FIELD_OFFSET (variant_part)
5064 = elaborate_expression_2 (offset,
5065 gnat_entity,
5066 get_identifier ("VOFFSET"),
5067 definition, false,
5068 DECL_OFFSET_ALIGN
5069 (variant_part));
5072 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
5073 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
5076 if (operand_equal_p (ada_size, size, 0))
5077 ada_size = TYPE_SIZE (gnu_type);
5078 else
5079 ada_size
5080 = elaborate_expression_1 (ada_size, gnat_entity,
5081 get_identifier ("RM_SIZE"),
5082 definition, false);
5083 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
5087 /* If this is a record type or subtype, call elaborate_expression_2 on
5088 any field position. Do this for both global and local types.
5089 Skip any fields that we haven't made trees for to avoid problems with
5090 class wide types. */
5091 if (IN (kind, Record_Kind))
5092 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
5093 gnat_temp = Next_Entity (gnat_temp))
5094 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
5096 tree gnu_field = get_gnu_tree (gnat_temp);
5098 /* ??? For now, store the offset as a multiple of the alignment
5099 in bytes so that we can see the alignment from the tree. */
5100 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
5102 DECL_FIELD_OFFSET (gnu_field)
5103 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
5104 gnat_temp,
5105 get_identifier ("OFFSET"),
5106 definition, false,
5107 DECL_OFFSET_ALIGN (gnu_field));
5109 /* ??? The context of gnu_field is not necessarily gnu_type
5110 so the MULT_EXPR node built above may not be marked by
5111 the call to create_type_decl below. */
5112 if (global_bindings_p ())
5113 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
5117 if (Treat_As_Volatile (gnat_entity))
5118 gnu_type
5119 = build_qualified_type (gnu_type,
5120 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
5122 if (Is_Atomic (gnat_entity))
5123 check_ok_for_atomic (gnu_type, gnat_entity, false);
5125 if (Present (Alignment_Clause (gnat_entity)))
5126 TYPE_USER_ALIGN (gnu_type) = 1;
5128 if (Universal_Aliasing (gnat_entity))
5129 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
5131 if (!gnu_decl)
5132 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
5133 !Comes_From_Source (gnat_entity),
5134 debug_info_p, gnat_entity);
5135 else
5137 TREE_TYPE (gnu_decl) = gnu_type;
5138 TYPE_STUB_DECL (gnu_type) = gnu_decl;
5142 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
5144 gnu_type = TREE_TYPE (gnu_decl);
5146 /* If this is a derived type, relate its alias set to that of its parent
5147 to avoid troubles when a call to an inherited primitive is inlined in
5148 a context where a derived object is accessed. The inlined code works
5149 on the parent view so the resulting code may access the same object
5150 using both the parent and the derived alias sets, which thus have to
5151 conflict. As the same issue arises with component references, the
5152 parent alias set also has to conflict with composite types enclosing
5153 derived components. For instance, if we have:
5155 type D is new T;
5156 type R is record
5157 Component : D;
5158 end record;
5160 we want T to conflict with both D and R, in addition to R being a
5161 superset of D by record/component construction.
5163 One way to achieve this is to perform an alias set copy from the
5164 parent to the derived type. This is not quite appropriate, though,
5165 as we don't want separate derived types to conflict with each other:
5167 type I1 is new Integer;
5168 type I2 is new Integer;
5170 We want I1 and I2 to both conflict with Integer but we do not want
5171 I1 to conflict with I2, and an alias set copy on derivation would
5172 have that effect.
5174 The option chosen is to make the alias set of the derived type a
5175 superset of that of its parent type. It trivially fulfills the
5176 simple requirement for the Integer derivation example above, and
5177 the component case as well by superset transitivity:
5179 superset superset
5180 R ----------> D ----------> T
5182 However, for composite types, conversions between derived types are
5183 translated into VIEW_CONVERT_EXPRs so a sequence like:
5185 type Comp1 is new Comp;
5186 type Comp2 is new Comp;
5187 procedure Proc (C : Comp1);
5189 C : Comp2;
5190 Proc (Comp1 (C));
5192 is translated into:
5194 C : Comp2;
5195 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5197 and gimplified into:
5199 C : Comp2;
5200 Comp1 *C.0;
5201 C.0 = (Comp1 *) &C;
5202 Proc (C.0);
5204 i.e. generates code involving type punning. Therefore, Comp1 needs
5205 to conflict with Comp2 and an alias set copy is required.
5207 The language rules ensure the parent type is already frozen here. */
5208 if (Is_Derived_Type (gnat_entity) && !type_annotate_only)
5210 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_entity));
5211 relate_alias_sets (gnu_type, gnu_parent_type,
5212 Is_Composite_Type (gnat_entity)
5213 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
5216 /* Back-annotate the Alignment of the type if not already in the
5217 tree. Likewise for sizes. */
5218 if (Unknown_Alignment (gnat_entity))
5220 unsigned int double_align, align;
5221 bool is_capped_double, align_clause;
5223 /* If the default alignment of "double" or larger scalar types is
5224 specifically capped and this is not an array with an alignment
5225 clause on the component type, return the cap. */
5226 if ((double_align = double_float_alignment) > 0)
5227 is_capped_double
5228 = is_double_float_or_array (gnat_entity, &align_clause);
5229 else if ((double_align = double_scalar_alignment) > 0)
5230 is_capped_double
5231 = is_double_scalar_or_array (gnat_entity, &align_clause);
5232 else
5233 is_capped_double = align_clause = false;
5235 if (is_capped_double && !align_clause)
5236 align = double_align;
5237 else
5238 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
5240 Set_Alignment (gnat_entity, UI_From_Int (align));
5243 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
5245 tree gnu_size = TYPE_SIZE (gnu_type);
5247 /* If the size is self-referential, annotate the maximum value. */
5248 if (CONTAINS_PLACEHOLDER_P (gnu_size))
5249 gnu_size = max_size (gnu_size, true);
5251 /* If we are just annotating types and the type is tagged, the tag
5252 and the parent components are not generated by the front-end so
5253 sizes must be adjusted if there is no representation clause. */
5254 if (type_annotate_only
5255 && Is_Tagged_Type (gnat_entity)
5256 && !VOID_TYPE_P (gnu_type)
5257 && (!TYPE_FIELDS (gnu_type)
5258 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
5260 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
5261 Uint uint_size;
5263 if (Is_Derived_Type (gnat_entity))
5265 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
5266 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
5267 Set_Alignment (gnat_entity, Alignment (gnat_parent));
5269 else
5270 offset = pointer_size;
5272 if (TYPE_FIELDS (gnu_type))
5273 offset
5274 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
5276 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
5277 gnu_size = round_up (gnu_size, POINTER_SIZE);
5278 uint_size = annotate_value (gnu_size);
5279 Set_Esize (gnat_entity, uint_size);
5280 Set_RM_Size (gnat_entity, uint_size);
5282 else
5283 Set_Esize (gnat_entity, annotate_value (gnu_size));
5286 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
5287 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
5290 /* If we really have a ..._DECL node, set a couple of flags on it. But we
5291 cannot do so if we are reusing the ..._DECL node made for an equivalent
5292 type or an alias or a renamed object as the predicates don't apply to it
5293 but to GNAT_ENTITY. */
5294 if (DECL_P (gnu_decl)
5295 && !(is_type && gnat_equiv_type != gnat_entity)
5296 && !Present (Alias (gnat_entity))
5297 && !(Present (Renamed_Object (gnat_entity)) && saved))
5299 if (!Comes_From_Source (gnat_entity))
5300 DECL_ARTIFICIAL (gnu_decl) = 1;
5302 if (!debug_info_p)
5303 DECL_IGNORED_P (gnu_decl) = 1;
5306 /* If we haven't already, associate the ..._DECL node that we just made with
5307 the input GNAT entity node. */
5308 if (!saved)
5309 save_gnu_tree (gnat_entity, gnu_decl, false);
5311 /* If this is an enumeration or floating-point type, we were not able to set
5312 the bounds since they refer to the type. These are always static. */
5313 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
5314 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
5316 tree gnu_scalar_type = gnu_type;
5317 tree gnu_low_bound, gnu_high_bound;
5319 /* If this is a padded type, we need to use the underlying type. */
5320 if (TYPE_IS_PADDING_P (gnu_scalar_type))
5321 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
5323 /* If this is a floating point type and we haven't set a floating
5324 point type yet, use this in the evaluation of the bounds. */
5325 if (!longest_float_type_node && kind == E_Floating_Point_Type)
5326 longest_float_type_node = gnu_scalar_type;
5328 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
5329 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
5331 if (kind == E_Enumeration_Type)
5333 /* Enumeration types have specific RM bounds. */
5334 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
5335 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
5337 else
5339 /* Floating-point types don't have specific RM bounds. */
5340 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
5341 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
5345 /* If we deferred processing of incomplete types, re-enable it. If there
5346 were no other disables and we have deferred types to process, do so. */
5347 if (this_deferred
5348 && --defer_incomplete_level == 0
5349 && defer_incomplete_list)
5351 struct incomplete *p, *next;
5353 /* We are back to level 0 for the deferring of incomplete types.
5354 But processing these incomplete types below may itself require
5355 deferring, so preserve what we have and restart from scratch. */
5356 p = defer_incomplete_list;
5357 defer_incomplete_list = NULL;
5359 for (; p; p = next)
5361 next = p->next;
5363 if (p->old_type)
5364 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5365 gnat_to_gnu_type (p->full_type));
5366 free (p);
5370 /* If we are not defining this type, see if it's on one of the lists of
5371 incomplete types. If so, handle the list entry now. */
5372 if (is_type && !definition)
5374 struct incomplete *p;
5376 for (p = defer_incomplete_list; p; p = p->next)
5377 if (p->old_type && p->full_type == gnat_entity)
5379 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5380 TREE_TYPE (gnu_decl));
5381 p->old_type = NULL_TREE;
5384 for (p = defer_limited_with; p; p = p->next)
5385 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5387 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5388 TREE_TYPE (gnu_decl));
5389 p->old_type = NULL_TREE;
5393 if (this_global)
5394 force_global--;
5396 /* If this is a packed array type whose original array type is itself
5397 an Itype without freeze node, make sure the latter is processed. */
5398 if (Is_Packed_Array_Type (gnat_entity)
5399 && Is_Itype (Original_Array_Type (gnat_entity))
5400 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5401 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5402 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5404 return gnu_decl;
5407 /* Similar, but if the returned value is a COMPONENT_REF, return the
5408 FIELD_DECL. */
5410 tree
5411 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5413 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5415 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5416 gnu_field = TREE_OPERAND (gnu_field, 1);
5418 return gnu_field;
5421 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5422 the GCC type corresponding to that entity. */
5424 tree
5425 gnat_to_gnu_type (Entity_Id gnat_entity)
5427 tree gnu_decl;
5429 /* The back end never attempts to annotate generic types. */
5430 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5431 return void_type_node;
5433 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5434 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5436 return TREE_TYPE (gnu_decl);
5439 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5440 the unpadded version of the GCC type corresponding to that entity. */
5442 tree
5443 get_unpadded_type (Entity_Id gnat_entity)
5445 tree type = gnat_to_gnu_type (gnat_entity);
5447 if (TYPE_IS_PADDING_P (type))
5448 type = TREE_TYPE (TYPE_FIELDS (type));
5450 return type;
5453 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5454 type has been changed to that of the parameterless procedure, except if an
5455 alias is already present, in which case it is returned instead. */
5457 tree
5458 get_minimal_subprog_decl (Entity_Id gnat_entity)
5460 tree gnu_entity_name, gnu_ext_name;
5461 struct attrib *attr_list = NULL;
5463 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5464 of the handling applied here. */
5466 while (Present (Alias (gnat_entity)))
5468 gnat_entity = Alias (gnat_entity);
5469 if (present_gnu_tree (gnat_entity))
5470 return get_gnu_tree (gnat_entity);
5473 gnu_entity_name = get_entity_name (gnat_entity);
5474 gnu_ext_name = create_concat_name (gnat_entity, NULL);
5476 if (Has_Stdcall_Convention (gnat_entity))
5477 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5478 get_identifier ("stdcall"), NULL_TREE,
5479 gnat_entity);
5480 else if (Has_Thiscall_Convention (gnat_entity))
5481 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5482 get_identifier ("thiscall"), NULL_TREE,
5483 gnat_entity);
5485 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_name)
5486 gnu_ext_name = NULL_TREE;
5488 return
5489 create_subprog_decl (gnu_entity_name, gnu_ext_name, void_ftype, NULL_TREE,
5490 is_disabled, true, true, true, attr_list, gnat_entity);
5493 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5494 a C++ imported method or equivalent.
5496 We use the predicate on 32-bit x86/Windows to find out whether we need to
5497 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5498 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5500 bool
5501 is_cplusplus_method (Entity_Id gnat_entity)
5503 if (Convention (gnat_entity) != Convention_CPP)
5504 return false;
5506 /* This is the main case: C++ method imported as a primitive operation. */
5507 if (Is_Dispatching_Operation (gnat_entity))
5508 return true;
5510 /* A thunk needs to be handled like its associated primitive operation. */
5511 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5512 return true;
5514 /* C++ classes with no virtual functions can be imported as limited
5515 record types, but we need to return true for the constructors. */
5516 if (Is_Constructor (gnat_entity))
5517 return true;
5519 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5520 if (Is_Dispatch_Table_Entity (gnat_entity))
5521 return true;
5523 return false;
5526 /* Finalize the processing of From_Limited_With incomplete types. */
5528 void
5529 finalize_from_limited_with (void)
5531 struct incomplete *p, *next;
5533 p = defer_limited_with;
5534 defer_limited_with = NULL;
5536 for (; p; p = next)
5538 next = p->next;
5540 if (p->old_type)
5541 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5542 gnat_to_gnu_type (p->full_type));
5543 free (p);
5547 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5548 kind of type (such E_Task_Type) that has a different type which Gigi
5549 uses for its representation. If the type does not have a special type
5550 for its representation, return GNAT_ENTITY. If a type is supposed to
5551 exist, but does not, abort unless annotating types, in which case
5552 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5554 Entity_Id
5555 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5557 Entity_Id gnat_equiv = gnat_entity;
5559 if (No (gnat_entity))
5560 return gnat_entity;
5562 switch (Ekind (gnat_entity))
5564 case E_Class_Wide_Subtype:
5565 if (Present (Equivalent_Type (gnat_entity)))
5566 gnat_equiv = Equivalent_Type (gnat_entity);
5567 break;
5569 case E_Access_Protected_Subprogram_Type:
5570 case E_Anonymous_Access_Protected_Subprogram_Type:
5571 gnat_equiv = Equivalent_Type (gnat_entity);
5572 break;
5574 case E_Class_Wide_Type:
5575 gnat_equiv = Root_Type (gnat_entity);
5576 break;
5578 case E_Task_Type:
5579 case E_Task_Subtype:
5580 case E_Protected_Type:
5581 case E_Protected_Subtype:
5582 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5583 break;
5585 default:
5586 break;
5589 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5591 return gnat_equiv;
5594 /* Return a GCC tree for a type corresponding to the component type of the
5595 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5596 is for an array being defined. DEBUG_INFO_P is true if we need to write
5597 debug information for other types that we may create in the process. */
5599 static tree
5600 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5601 bool debug_info_p)
5603 const Entity_Id gnat_type = Component_Type (gnat_array);
5604 tree gnu_type = gnat_to_gnu_type (gnat_type);
5605 tree gnu_comp_size;
5607 /* Try to get a smaller form of the component if needed. */
5608 if ((Is_Packed (gnat_array)
5609 || Has_Component_Size_Clause (gnat_array))
5610 && !Is_Bit_Packed_Array (gnat_array)
5611 && !Has_Aliased_Components (gnat_array)
5612 && !Strict_Alignment (gnat_type)
5613 && RECORD_OR_UNION_TYPE_P (gnu_type)
5614 && !TYPE_FAT_POINTER_P (gnu_type)
5615 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5616 gnu_type = make_packable_type (gnu_type, false);
5618 if (Has_Atomic_Components (gnat_array))
5619 check_ok_for_atomic (gnu_type, gnat_array, true);
5621 /* Get and validate any specified Component_Size. */
5622 gnu_comp_size
5623 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5624 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5625 true, Has_Component_Size_Clause (gnat_array));
5627 /* If the array has aliased components and the component size can be zero,
5628 force at least unit size to ensure that the components have distinct
5629 addresses. */
5630 if (!gnu_comp_size
5631 && Has_Aliased_Components (gnat_array)
5632 && (integer_zerop (TYPE_SIZE (gnu_type))
5633 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5634 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5635 gnu_comp_size
5636 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5638 /* If the component type is a RECORD_TYPE that has a self-referential size,
5639 then use the maximum size for the component size. */
5640 if (!gnu_comp_size
5641 && TREE_CODE (gnu_type) == RECORD_TYPE
5642 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5643 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5645 /* Honor the component size. This is not needed for bit-packed arrays. */
5646 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5648 tree orig_type = gnu_type;
5649 unsigned int max_align;
5651 /* If an alignment is specified, use it as a cap on the component type
5652 so that it can be honored for the whole type. But ignore it for the
5653 original type of packed array types. */
5654 if (No (Packed_Array_Type (gnat_array)) && Known_Alignment (gnat_array))
5655 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5656 else
5657 max_align = 0;
5659 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5660 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5661 gnu_type = orig_type;
5662 else
5663 orig_type = gnu_type;
5665 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5666 true, false, definition, true);
5668 /* If a padding record was made, declare it now since it will never be
5669 declared otherwise. This is necessary to ensure that its subtrees
5670 are properly marked. */
5671 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5672 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5673 gnat_array);
5676 if (Has_Volatile_Components (gnat_array))
5677 gnu_type
5678 = build_qualified_type (gnu_type,
5679 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
5681 return gnu_type;
5684 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5685 using MECH as its passing mechanism, to be placed in the parameter
5686 list built for GNAT_SUBPROG. Assume a foreign convention for the
5687 latter if FOREIGN is true. Also set CICO to true if the parameter
5688 must use the copy-in copy-out implementation mechanism.
5690 The returned tree is a PARM_DECL, except for those cases where no
5691 parameter needs to be actually passed to the subprogram; the type
5692 of this "shadow" parameter is then returned instead. */
5694 static tree
5695 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5696 Entity_Id gnat_subprog, bool foreign, bool *cico)
5698 tree gnu_param_name = get_entity_name (gnat_param);
5699 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5700 tree gnu_param_type_alt = NULL_TREE;
5701 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5702 /* The parameter can be indirectly modified if its address is taken. */
5703 bool ro_param = in_param && !Address_Taken (gnat_param);
5704 bool by_return = false, by_component_ptr = false;
5705 bool by_ref = false;
5706 tree gnu_param;
5708 /* Copy-return is used only for the first parameter of a valued procedure.
5709 It's a copy mechanism for which a parameter is never allocated. */
5710 if (mech == By_Copy_Return)
5712 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5713 mech = By_Copy;
5714 by_return = true;
5717 /* If this is either a foreign function or if the underlying type won't
5718 be passed by reference, strip off possible padding type. */
5719 if (TYPE_IS_PADDING_P (gnu_param_type))
5721 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5723 if (mech == By_Reference
5724 || foreign
5725 || (!must_pass_by_ref (unpadded_type)
5726 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
5727 gnu_param_type = unpadded_type;
5730 /* If this is a read-only parameter, make a variant of the type that is
5731 read-only. ??? However, if this is an unconstrained array, that type
5732 can be very complex, so skip it for now. Likewise for any other
5733 self-referential type. */
5734 if (ro_param
5735 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5736 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5737 gnu_param_type = build_qualified_type (gnu_param_type,
5738 (TYPE_QUALS (gnu_param_type)
5739 | TYPE_QUAL_CONST));
5741 /* For foreign conventions, pass arrays as pointers to the element type.
5742 First check for unconstrained array and get the underlying array. */
5743 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5744 gnu_param_type
5745 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5747 /* For GCC builtins, pass Address integer types as (void *) */
5748 if (Convention (gnat_subprog) == Convention_Intrinsic
5749 && Present (Interface_Name (gnat_subprog))
5750 && Is_Descendent_Of_Address (Etype (gnat_param)))
5751 gnu_param_type = ptr_void_type_node;
5753 /* VMS descriptors are themselves passed by reference. */
5754 if (mech == By_Short_Descriptor ||
5755 (mech == By_Descriptor && TARGET_ABI_OPEN_VMS && !flag_vms_malloc64))
5756 gnu_param_type
5757 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5758 Mechanism (gnat_param),
5759 gnat_subprog));
5760 else if (mech == By_Descriptor)
5762 /* Build both a 32-bit and 64-bit descriptor, one of which will be
5763 chosen in fill_vms_descriptor. */
5764 gnu_param_type_alt
5765 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5766 Mechanism (gnat_param),
5767 gnat_subprog));
5768 gnu_param_type
5769 = build_pointer_type (build_vms_descriptor (gnu_param_type,
5770 Mechanism (gnat_param),
5771 gnat_subprog));
5774 /* Arrays are passed as pointers to element type for foreign conventions. */
5775 else if (foreign
5776 && mech != By_Copy
5777 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5779 /* Strip off any multi-dimensional entries, then strip
5780 off the last array to get the component type. */
5781 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5782 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5783 gnu_param_type = TREE_TYPE (gnu_param_type);
5785 by_component_ptr = true;
5786 gnu_param_type = TREE_TYPE (gnu_param_type);
5788 if (ro_param)
5789 gnu_param_type = build_qualified_type (gnu_param_type,
5790 (TYPE_QUALS (gnu_param_type)
5791 | TYPE_QUAL_CONST));
5793 gnu_param_type = build_pointer_type (gnu_param_type);
5796 /* Fat pointers are passed as thin pointers for foreign conventions. */
5797 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5798 gnu_param_type
5799 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5801 /* If we must pass or were requested to pass by reference, do so.
5802 If we were requested to pass by copy, do so.
5803 Otherwise, for foreign conventions, pass In Out or Out parameters
5804 or aggregates by reference. For COBOL and Fortran, pass all
5805 integer and FP types that way too. For Convention Ada, use
5806 the standard Ada default. */
5807 else if (must_pass_by_ref (gnu_param_type)
5808 || mech == By_Reference
5809 || (mech != By_Copy
5810 && ((foreign
5811 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5812 || (foreign
5813 && (Convention (gnat_subprog) == Convention_Fortran
5814 || Convention (gnat_subprog) == Convention_COBOL)
5815 && (INTEGRAL_TYPE_P (gnu_param_type)
5816 || FLOAT_TYPE_P (gnu_param_type)))
5817 || (!foreign
5818 && default_pass_by_ref (gnu_param_type)))))
5820 /* We take advantage of 6.2(12) by considering that references built for
5821 parameters whose type isn't by-ref and for which the mechanism hasn't
5822 been forced to by-ref are restrict-qualified in the C sense. */
5823 bool restrict_p
5824 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5825 gnu_param_type = build_reference_type (gnu_param_type);
5826 if (restrict_p)
5827 gnu_param_type
5828 = build_qualified_type (gnu_param_type, TYPE_QUAL_RESTRICT);
5829 by_ref = true;
5832 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5833 else if (!in_param)
5834 *cico = true;
5836 if (mech == By_Copy && (by_ref || by_component_ptr))
5837 post_error ("?cannot pass & by copy", gnat_param);
5839 /* If this is an Out parameter that isn't passed by reference and isn't
5840 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5841 it will be a VAR_DECL created when we process the procedure, so just
5842 return its type. For the special parameter of a valued procedure,
5843 never pass it in.
5845 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5846 Out parameters with discriminants or implicit initial values to be
5847 handled like In Out parameters. These type are normally built as
5848 aggregates, hence passed by reference, except for some packed arrays
5849 which end up encoded in special integer types. Note that scalars can
5850 be given implicit initial values using the Default_Value aspect.
5852 The exception we need to make is then for packed arrays of records
5853 with discriminants or implicit initial values. We have no light/easy
5854 way to check for the latter case, so we merely check for packed arrays
5855 of records. This may lead to useless copy-in operations, but in very
5856 rare cases only, as these would be exceptions in a set of already
5857 exceptional situations. */
5858 if (Ekind (gnat_param) == E_Out_Parameter
5859 && !by_ref
5860 && (by_return
5861 || (mech != By_Descriptor
5862 && mech != By_Short_Descriptor
5863 && !POINTER_TYPE_P (gnu_param_type)
5864 && !AGGREGATE_TYPE_P (gnu_param_type)
5865 && !Has_Default_Aspect (Etype (gnat_param))))
5866 && !(Is_Array_Type (Etype (gnat_param))
5867 && Is_Packed (Etype (gnat_param))
5868 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5869 return gnu_param_type;
5871 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5872 ro_param || by_ref || by_component_ptr);
5873 DECL_BY_REF_P (gnu_param) = by_ref;
5874 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5875 DECL_BY_DESCRIPTOR_P (gnu_param)
5876 = (mech == By_Descriptor || mech == By_Short_Descriptor);
5877 DECL_POINTS_TO_READONLY_P (gnu_param)
5878 = (ro_param && (by_ref || by_component_ptr));
5879 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5881 /* Save the alternate descriptor type, if any. */
5882 if (gnu_param_type_alt)
5883 SET_DECL_PARM_ALT_TYPE (gnu_param, gnu_param_type_alt);
5885 /* If no Mechanism was specified, indicate what we're using, then
5886 back-annotate it. */
5887 if (mech == Default)
5888 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5890 Set_Mechanism (gnat_param, mech);
5891 return gnu_param;
5894 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5896 static bool
5897 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5899 while (Present (Corresponding_Discriminant (discr1)))
5900 discr1 = Corresponding_Discriminant (discr1);
5902 while (Present (Corresponding_Discriminant (discr2)))
5903 discr2 = Corresponding_Discriminant (discr2);
5905 return
5906 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5909 /* Return true if the array type GNU_TYPE, which represents a dimension of
5910 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5912 static bool
5913 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5915 /* If the array type is not the innermost dimension of the GNAT type,
5916 then it has a non-aliased component. */
5917 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5918 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5919 return true;
5921 /* If the array type has an aliased component in the front-end sense,
5922 then it also has an aliased component in the back-end sense. */
5923 if (Has_Aliased_Components (gnat_type))
5924 return false;
5926 /* If this is a derived type, then it has a non-aliased component if
5927 and only if its parent type also has one. */
5928 if (Is_Derived_Type (gnat_type))
5930 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5931 int index;
5932 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5933 gnu_parent_type
5934 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5935 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5936 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5937 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5940 /* Otherwise, rely exclusively on properties of the element type. */
5941 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5944 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5946 static bool
5947 compile_time_known_address_p (Node_Id gnat_address)
5949 /* Catch System'To_Address. */
5950 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5951 gnat_address = Expression (gnat_address);
5953 return Compile_Time_Known_Value (gnat_address);
5956 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5957 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5959 static bool
5960 cannot_be_superflat_p (Node_Id gnat_range)
5962 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5963 Node_Id scalar_range;
5964 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5966 /* If the low bound is not constant, try to find an upper bound. */
5967 while (Nkind (gnat_lb) != N_Integer_Literal
5968 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5969 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5970 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5971 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5972 || Nkind (scalar_range) == N_Range))
5973 gnat_lb = High_Bound (scalar_range);
5975 /* If the high bound is not constant, try to find a lower bound. */
5976 while (Nkind (gnat_hb) != N_Integer_Literal
5977 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5978 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5979 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5980 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5981 || Nkind (scalar_range) == N_Range))
5982 gnat_hb = Low_Bound (scalar_range);
5984 /* If we have failed to find constant bounds, punt. */
5985 if (Nkind (gnat_lb) != N_Integer_Literal
5986 || Nkind (gnat_hb) != N_Integer_Literal)
5987 return false;
5989 /* We need at least a signed 64-bit type to catch most cases. */
5990 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5991 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5992 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5993 return false;
5995 /* If the low bound is the smallest integer, nothing can be smaller. */
5996 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5997 if (TREE_OVERFLOW (gnu_lb_minus_one))
5998 return true;
6000 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
6003 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
6005 static bool
6006 constructor_address_p (tree gnu_expr)
6008 while (TREE_CODE (gnu_expr) == NOP_EXPR
6009 || TREE_CODE (gnu_expr) == CONVERT_EXPR
6010 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
6011 gnu_expr = TREE_OPERAND (gnu_expr, 0);
6013 return (TREE_CODE (gnu_expr) == ADDR_EXPR
6014 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
6017 /* Given GNAT_ENTITY, elaborate all expressions that are required to
6018 be elaborated at the point of its definition, but do nothing else. */
6020 void
6021 elaborate_entity (Entity_Id gnat_entity)
6023 switch (Ekind (gnat_entity))
6025 case E_Signed_Integer_Subtype:
6026 case E_Modular_Integer_Subtype:
6027 case E_Enumeration_Subtype:
6028 case E_Ordinary_Fixed_Point_Subtype:
6029 case E_Decimal_Fixed_Point_Subtype:
6030 case E_Floating_Point_Subtype:
6032 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
6033 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
6035 /* ??? Tests to avoid Constraint_Error in static expressions
6036 are needed until after the front stops generating bogus
6037 conversions on bounds of real types. */
6038 if (!Raises_Constraint_Error (gnat_lb))
6039 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
6040 true, false, Needs_Debug_Info (gnat_entity));
6041 if (!Raises_Constraint_Error (gnat_hb))
6042 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
6043 true, false, Needs_Debug_Info (gnat_entity));
6044 break;
6047 case E_Record_Subtype:
6048 case E_Private_Subtype:
6049 case E_Limited_Private_Subtype:
6050 case E_Record_Subtype_With_Private:
6051 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6053 Node_Id gnat_discriminant_expr;
6054 Entity_Id gnat_field;
6056 for (gnat_field
6057 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6058 gnat_discriminant_expr
6059 = First_Elmt (Discriminant_Constraint (gnat_entity));
6060 Present (gnat_field);
6061 gnat_field = Next_Discriminant (gnat_field),
6062 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6063 /* Ignore access discriminants. */
6064 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6065 elaborate_expression (Node (gnat_discriminant_expr),
6066 gnat_entity, get_entity_name (gnat_field),
6067 true, false, false);
6069 break;
6074 /* Return true if the size in units represented by GNU_SIZE can be handled by
6075 an allocation. If STATIC_P is true, consider only what can be done with a
6076 static allocation. */
6078 static bool
6079 allocatable_size_p (tree gnu_size, bool static_p)
6081 /* We can allocate a fixed size if it is a valid for the middle-end. */
6082 if (TREE_CODE (gnu_size) == INTEGER_CST)
6083 return valid_constant_size_p (gnu_size);
6085 /* We can allocate a variable size if this isn't a static allocation. */
6086 else
6087 return !static_p;
6090 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6091 NAME, ARGS and ERROR_POINT. */
6093 static void
6094 prepend_one_attribute (struct attrib **attr_list,
6095 enum attr_type attr_type,
6096 tree attr_name,
6097 tree attr_args,
6098 Node_Id attr_error_point)
6100 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6102 attr->type = attr_type;
6103 attr->name = attr_name;
6104 attr->args = attr_args;
6105 attr->error_point = attr_error_point;
6107 attr->next = *attr_list;
6108 *attr_list = attr;
6111 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6113 static void
6114 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6116 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6117 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6118 enum attr_type etype;
6120 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6121 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6123 case Pragma_Machine_Attribute:
6124 etype = ATTR_MACHINE_ATTRIBUTE;
6125 break;
6127 case Pragma_Linker_Alias:
6128 etype = ATTR_LINK_ALIAS;
6129 break;
6131 case Pragma_Linker_Section:
6132 etype = ATTR_LINK_SECTION;
6133 break;
6135 case Pragma_Linker_Constructor:
6136 etype = ATTR_LINK_CONSTRUCTOR;
6137 break;
6139 case Pragma_Linker_Destructor:
6140 etype = ATTR_LINK_DESTRUCTOR;
6141 break;
6143 case Pragma_Weak_External:
6144 etype = ATTR_WEAK_EXTERNAL;
6145 break;
6147 case Pragma_Thread_Local_Storage:
6148 etype = ATTR_THREAD_LOCAL_STORAGE;
6149 break;
6151 default:
6152 return;
6155 /* See what arguments we have and turn them into GCC trees for attribute
6156 handlers. These expect identifier for strings. We handle at most two
6157 arguments and static expressions only. */
6158 if (Present (gnat_arg) && Present (First (gnat_arg)))
6160 Node_Id gnat_arg0 = Next (First (gnat_arg));
6161 Node_Id gnat_arg1 = Empty;
6163 if (Present (gnat_arg0) && Is_Static_Expression (Expression (gnat_arg0)))
6165 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6167 if (TREE_CODE (gnu_arg0) == STRING_CST)
6169 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6170 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6171 return;
6174 gnat_arg1 = Next (gnat_arg0);
6177 if (Present (gnat_arg1) && Is_Static_Expression (Expression (gnat_arg1)))
6179 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6181 if (TREE_CODE (gnu_arg1) == STRING_CST)
6182 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6186 /* Prepend to the list. Make a list of the argument we might have, as GCC
6187 expects it. */
6188 prepend_one_attribute (attr_list, etype, gnu_arg0,
6189 gnu_arg1
6190 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6191 Present (Next (First (gnat_arg)))
6192 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6195 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6197 static void
6198 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6200 Node_Id gnat_temp;
6202 /* Attributes are stored as Representation Item pragmas. */
6203 for (gnat_temp = First_Rep_Item (gnat_entity);
6204 Present (gnat_temp);
6205 gnat_temp = Next_Rep_Item (gnat_temp))
6206 if (Nkind (gnat_temp) == N_Pragma)
6207 prepend_one_attribute_pragma (attr_list, gnat_temp);
6210 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6211 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6212 return the GCC tree to use for that expression. GNU_NAME is the suffix
6213 to use if a variable needs to be created and DEFINITION is true if this
6214 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6215 otherwise, we are just elaborating the expression for side-effects. If
6216 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6217 isn't needed for code generation. */
6219 static tree
6220 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
6221 bool definition, bool need_value, bool need_debug)
6223 tree gnu_expr;
6225 /* If we already elaborated this expression (e.g. it was involved
6226 in the definition of a private type), use the old value. */
6227 if (present_gnu_tree (gnat_expr))
6228 return get_gnu_tree (gnat_expr);
6230 /* If we don't need a value and this is static or a discriminant,
6231 we don't need to do anything. */
6232 if (!need_value
6233 && (Is_OK_Static_Expression (gnat_expr)
6234 || (Nkind (gnat_expr) == N_Identifier
6235 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6236 return NULL_TREE;
6238 /* If it's a static expression, we don't need a variable for debugging. */
6239 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6240 need_debug = false;
6242 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6243 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
6244 gnu_name, definition, need_debug);
6246 /* Save the expression in case we try to elaborate this entity again. Since
6247 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6248 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6249 save_gnu_tree (gnat_expr, gnu_expr, true);
6251 return need_value ? gnu_expr : error_mark_node;
6254 /* Similar, but take a GNU expression and always return a result. */
6256 static tree
6257 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6258 bool definition, bool need_debug)
6260 const bool expr_public_p = Is_Public (gnat_entity);
6261 const bool expr_global_p = expr_public_p || global_bindings_p ();
6262 bool expr_variable_p, use_variable;
6264 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6265 reference will have been replaced with a COMPONENT_REF when the type
6266 is being elaborated. However, there are some cases involving child
6267 types where we will. So convert it to a COMPONENT_REF. We hope it
6268 will be at the highest level of the expression in these cases. */
6269 if (TREE_CODE (gnu_expr) == FIELD_DECL)
6270 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6271 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6272 gnu_expr, NULL_TREE);
6274 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6275 that an expression cannot contain both a discriminant and a variable. */
6276 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6277 return gnu_expr;
6279 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6280 a variable that is initialized to contain the expression when the package
6281 containing the definition is elaborated. If this entity is defined at top
6282 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6283 if this is necessary. */
6284 if (CONSTANT_CLASS_P (gnu_expr))
6285 expr_variable_p = false;
6286 else
6288 /* Skip any conversions and simple constant arithmetics to see if the
6289 expression is based on a read-only variable.
6290 ??? This really should remain read-only, but we have to think about
6291 the typing of the tree here. */
6292 tree inner = remove_conversions (gnu_expr, true);
6294 inner = skip_simple_constant_arithmetic (inner);
6296 if (handled_component_p (inner))
6298 HOST_WIDE_INT bitsize, bitpos;
6299 tree offset;
6300 enum machine_mode mode;
6301 int unsignedp, volatilep;
6303 inner = get_inner_reference (inner, &bitsize, &bitpos, &offset,
6304 &mode, &unsignedp, &volatilep, false);
6305 /* If the offset is variable, err on the side of caution. */
6306 if (offset)
6307 inner = NULL_TREE;
6310 expr_variable_p
6311 = !(inner
6312 && TREE_CODE (inner) == VAR_DECL
6313 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6316 /* We only need to use the variable if we are in a global context since GCC
6317 can do the right thing in the local case. However, when not optimizing,
6318 use it for bounds of loop iteration scheme to avoid code duplication. */
6319 use_variable = expr_variable_p
6320 && (expr_global_p
6321 || (!optimize
6322 && definition
6323 && Is_Itype (gnat_entity)
6324 && Nkind (Associated_Node_For_Itype (gnat_entity))
6325 == N_Loop_Parameter_Specification));
6327 /* Now create it, possibly only for debugging purposes. */
6328 if (use_variable || need_debug)
6330 tree gnu_decl
6331 = create_var_decl_1
6332 (create_concat_name (gnat_entity, IDENTIFIER_POINTER (gnu_name)),
6333 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true, expr_public_p,
6334 !definition, expr_global_p, !need_debug, NULL, gnat_entity);
6336 if (use_variable)
6337 return gnu_decl;
6340 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6343 /* Similar, but take an alignment factor and make it explicit in the tree. */
6345 static tree
6346 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6347 bool definition, bool need_debug, unsigned int align)
6349 tree unit_align = size_int (align / BITS_PER_UNIT);
6350 return
6351 size_binop (MULT_EXPR,
6352 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6353 gnu_expr,
6354 unit_align),
6355 gnat_entity, gnu_name, definition,
6356 need_debug),
6357 unit_align);
6360 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6361 the value passed against the list of choices. */
6363 tree
6364 choices_to_gnu (tree operand, Node_Id choices)
6366 Node_Id choice;
6367 Node_Id gnat_temp;
6368 tree result = boolean_false_node;
6369 tree this_test, low = 0, high = 0, single = 0;
6371 for (choice = First (choices); Present (choice); choice = Next (choice))
6373 switch (Nkind (choice))
6375 case N_Range:
6376 low = gnat_to_gnu (Low_Bound (choice));
6377 high = gnat_to_gnu (High_Bound (choice));
6379 this_test
6380 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6381 build_binary_op (GE_EXPR, boolean_type_node,
6382 operand, low),
6383 build_binary_op (LE_EXPR, boolean_type_node,
6384 operand, high));
6386 break;
6388 case N_Subtype_Indication:
6389 gnat_temp = Range_Expression (Constraint (choice));
6390 low = gnat_to_gnu (Low_Bound (gnat_temp));
6391 high = gnat_to_gnu (High_Bound (gnat_temp));
6393 this_test
6394 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6395 build_binary_op (GE_EXPR, boolean_type_node,
6396 operand, low),
6397 build_binary_op (LE_EXPR, boolean_type_node,
6398 operand, high));
6399 break;
6401 case N_Identifier:
6402 case N_Expanded_Name:
6403 /* This represents either a subtype range, an enumeration
6404 literal, or a constant Ekind says which. If an enumeration
6405 literal or constant, fall through to the next case. */
6406 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6407 && Ekind (Entity (choice)) != E_Constant)
6409 tree type = gnat_to_gnu_type (Entity (choice));
6411 low = TYPE_MIN_VALUE (type);
6412 high = TYPE_MAX_VALUE (type);
6414 this_test
6415 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6416 build_binary_op (GE_EXPR, boolean_type_node,
6417 operand, low),
6418 build_binary_op (LE_EXPR, boolean_type_node,
6419 operand, high));
6420 break;
6423 /* ... fall through ... */
6425 case N_Character_Literal:
6426 case N_Integer_Literal:
6427 single = gnat_to_gnu (choice);
6428 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6429 single);
6430 break;
6432 case N_Others_Choice:
6433 this_test = boolean_true_node;
6434 break;
6436 default:
6437 gcc_unreachable ();
6440 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6441 this_test);
6444 return result;
6447 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6448 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6450 static int
6451 adjust_packed (tree field_type, tree record_type, int packed)
6453 /* If the field contains an item of variable size, we cannot pack it
6454 because we cannot create temporaries of non-fixed size in case
6455 we need to take the address of the field. See addressable_p and
6456 the notes on the addressability issues for further details. */
6457 if (type_has_variable_size (field_type))
6458 return 0;
6460 /* If the alignment of the record is specified and the field type
6461 is over-aligned, request Storage_Unit alignment for the field. */
6462 if (packed == -2)
6464 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6465 return -1;
6466 else
6467 return 0;
6470 return packed;
6473 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6474 placed in GNU_RECORD_TYPE.
6476 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6477 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6478 record has a specified alignment.
6480 DEFINITION is true if this field is for a record being defined.
6482 DEBUG_INFO_P is true if we need to write debug information for types
6483 that we may create in the process. */
6485 static tree
6486 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6487 bool definition, bool debug_info_p)
6489 const Entity_Id gnat_field_type = Etype (gnat_field);
6490 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6491 tree gnu_field_id = get_entity_name (gnat_field);
6492 tree gnu_field, gnu_size, gnu_pos;
6493 bool is_volatile
6494 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6495 bool needs_strict_alignment
6496 = (is_volatile
6497 || Is_Aliased (gnat_field)
6498 || Strict_Alignment (gnat_field_type));
6500 /* If this field requires strict alignment, we cannot pack it because
6501 it would very likely be under-aligned in the record. */
6502 if (needs_strict_alignment)
6503 packed = 0;
6504 else
6505 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6507 /* If a size is specified, use it. Otherwise, if the record type is packed,
6508 use the official RM size. See "Handling of Type'Size Values" in Einfo
6509 for further details. */
6510 if (Known_Esize (gnat_field))
6511 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6512 gnat_field, FIELD_DECL, false, true);
6513 else if (packed == 1)
6514 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6515 gnat_field, FIELD_DECL, false, true);
6516 else
6517 gnu_size = NULL_TREE;
6519 /* If we have a specified size that is smaller than that of the field's type,
6520 or a position is specified, and the field's type is a record that doesn't
6521 require strict alignment, see if we can get either an integral mode form
6522 of the type or a smaller form. If we can, show a size was specified for
6523 the field if there wasn't one already, so we know to make this a bitfield
6524 and avoid making things wider.
6526 Changing to an integral mode form is useful when the record is packed as
6527 we can then place the field at a non-byte-aligned position and so achieve
6528 tighter packing. This is in addition required if the field shares a byte
6529 with another field and the front-end lets the back-end handle the access
6530 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6532 Changing to a smaller form is required if the specified size is smaller
6533 than that of the field's type and the type contains sub-fields that are
6534 padded, in order to avoid generating accesses to these sub-fields that
6535 are wider than the field.
6537 We avoid the transformation if it is not required or potentially useful,
6538 as it might entail an increase of the field's alignment and have ripple
6539 effects on the outer record type. A typical case is a field known to be
6540 byte-aligned and not to share a byte with another field. */
6541 if (!needs_strict_alignment
6542 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6543 && !TYPE_FAT_POINTER_P (gnu_field_type)
6544 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6545 && (packed == 1
6546 || (gnu_size
6547 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6548 || (Present (Component_Clause (gnat_field))
6549 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6550 % BITS_PER_UNIT == 0
6551 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6553 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6554 if (gnu_packable_type != gnu_field_type)
6556 gnu_field_type = gnu_packable_type;
6557 if (!gnu_size)
6558 gnu_size = rm_size (gnu_field_type);
6562 if (Is_Atomic (gnat_field))
6563 check_ok_for_atomic (gnu_field_type, gnat_field, false);
6565 if (Present (Component_Clause (gnat_field)))
6567 Entity_Id gnat_parent
6568 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6570 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6571 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6572 gnat_field, FIELD_DECL, false, true);
6574 /* Ensure the position does not overlap with the parent subtype, if there
6575 is one. This test is omitted if the parent of the tagged type has a
6576 full rep clause since, in this case, component clauses are allowed to
6577 overlay the space allocated for the parent type and the front-end has
6578 checked that there are no overlapping components. */
6579 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6581 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6583 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6584 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6586 post_error_ne_tree
6587 ("offset of& must be beyond parent{, minimum allowed is ^}",
6588 First_Bit (Component_Clause (gnat_field)), gnat_field,
6589 TYPE_SIZE_UNIT (gnu_parent));
6593 /* If this field needs strict alignment, check that the record is
6594 sufficiently aligned and that position and size are consistent with
6595 the alignment. But don't do it if we are just annotating types and
6596 the field's type is tagged, since tagged types aren't fully laid out
6597 in this mode. Also, note that atomic implies volatile so the inner
6598 test sequences ordering is significant here. */
6599 if (needs_strict_alignment
6600 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6602 TYPE_ALIGN (gnu_record_type)
6603 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6605 if (gnu_size
6606 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6608 if (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type))
6609 post_error_ne_tree
6610 ("atomic field& must be natural size of type{ (^)}",
6611 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6612 TYPE_SIZE (gnu_field_type));
6614 else if (is_volatile)
6615 post_error_ne_tree
6616 ("volatile field& must be natural size of type{ (^)}",
6617 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6618 TYPE_SIZE (gnu_field_type));
6620 else if (Is_Aliased (gnat_field))
6621 post_error_ne_tree
6622 ("size of aliased field& must be ^ bits",
6623 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6624 TYPE_SIZE (gnu_field_type));
6626 else if (Strict_Alignment (gnat_field_type))
6627 post_error_ne_tree
6628 ("size of & with aliased or tagged components not ^ bits",
6629 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6630 TYPE_SIZE (gnu_field_type));
6632 else
6633 gcc_unreachable ();
6635 gnu_size = NULL_TREE;
6638 if (!integer_zerop (size_binop
6639 (TRUNC_MOD_EXPR, gnu_pos,
6640 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6642 if (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type))
6643 post_error_ne_num
6644 ("position of atomic field& must be multiple of ^ bits",
6645 First_Bit (Component_Clause (gnat_field)), gnat_field,
6646 TYPE_ALIGN (gnu_field_type));
6648 else if (is_volatile)
6649 post_error_ne_num
6650 ("position of volatile field& must be multiple of ^ bits",
6651 First_Bit (Component_Clause (gnat_field)), gnat_field,
6652 TYPE_ALIGN (gnu_field_type));
6654 else if (Is_Aliased (gnat_field))
6655 post_error_ne_num
6656 ("position of aliased field& must be multiple of ^ bits",
6657 First_Bit (Component_Clause (gnat_field)), gnat_field,
6658 TYPE_ALIGN (gnu_field_type));
6660 else if (Strict_Alignment (gnat_field_type))
6661 post_error_ne
6662 ("position of & is not compatible with alignment required "
6663 "by its components",
6664 First_Bit (Component_Clause (gnat_field)), gnat_field);
6666 else
6667 gcc_unreachable ();
6669 gnu_pos = NULL_TREE;
6674 /* If the record has rep clauses and this is the tag field, make a rep
6675 clause for it as well. */
6676 else if (Has_Specified_Layout (Scope (gnat_field))
6677 && Chars (gnat_field) == Name_uTag)
6679 gnu_pos = bitsize_zero_node;
6680 gnu_size = TYPE_SIZE (gnu_field_type);
6683 else
6685 gnu_pos = NULL_TREE;
6687 /* If we are packing the record and the field is BLKmode, round the
6688 size up to a byte boundary. */
6689 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6690 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6693 /* We need to make the size the maximum for the type if it is
6694 self-referential and an unconstrained type. In that case, we can't
6695 pack the field since we can't make a copy to align it. */
6696 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6697 && !gnu_size
6698 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6699 && !Is_Constrained (Underlying_Type (gnat_field_type)))
6701 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6702 packed = 0;
6705 /* If a size is specified, adjust the field's type to it. */
6706 if (gnu_size)
6708 tree orig_field_type;
6710 /* If the field's type is justified modular, we would need to remove
6711 the wrapper to (better) meet the layout requirements. However we
6712 can do so only if the field is not aliased to preserve the unique
6713 layout and if the prescribed size is not greater than that of the
6714 packed array to preserve the justification. */
6715 if (!needs_strict_alignment
6716 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6717 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6718 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6719 <= 0)
6720 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6722 /* Similarly if the field's type is a misaligned integral type, but
6723 there is no restriction on the size as there is no justification. */
6724 if (!needs_strict_alignment
6725 && TYPE_IS_PADDING_P (gnu_field_type)
6726 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
6727 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6729 gnu_field_type
6730 = make_type_from_size (gnu_field_type, gnu_size,
6731 Has_Biased_Representation (gnat_field));
6733 orig_field_type = gnu_field_type;
6734 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6735 false, false, definition, true);
6737 /* If a padding record was made, declare it now since it will never be
6738 declared otherwise. This is necessary to ensure that its subtrees
6739 are properly marked. */
6740 if (gnu_field_type != orig_field_type
6741 && !DECL_P (TYPE_NAME (gnu_field_type)))
6742 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
6743 debug_info_p, gnat_field);
6746 /* Otherwise (or if there was an error), don't specify a position. */
6747 else
6748 gnu_pos = NULL_TREE;
6750 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6751 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6753 /* Now create the decl for the field. */
6754 gnu_field
6755 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6756 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6757 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6758 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
6759 TREE_THIS_VOLATILE (gnu_field) = TREE_SIDE_EFFECTS (gnu_field) = is_volatile;
6761 if (Ekind (gnat_field) == E_Discriminant)
6762 DECL_DISCRIMINANT_NUMBER (gnu_field)
6763 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6765 return gnu_field;
6768 /* Return true if at least one member of COMPONENT_LIST needs strict
6769 alignment. */
6771 static bool
6772 components_need_strict_alignment (Node_Id component_list)
6774 Node_Id component_decl;
6776 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6777 Present (component_decl);
6778 component_decl = Next_Non_Pragma (component_decl))
6780 Entity_Id gnat_field = Defining_Entity (component_decl);
6782 if (Is_Aliased (gnat_field))
6783 return true;
6785 if (Strict_Alignment (Etype (gnat_field)))
6786 return true;
6789 return false;
6792 /* Return true if TYPE is a type with variable size or a padding type with a
6793 field of variable size or a record that has a field with such a type. */
6795 static bool
6796 type_has_variable_size (tree type)
6798 tree field;
6800 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6801 return true;
6803 if (TYPE_IS_PADDING_P (type)
6804 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6805 return true;
6807 if (!RECORD_OR_UNION_TYPE_P (type))
6808 return false;
6810 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6811 if (type_has_variable_size (TREE_TYPE (field)))
6812 return true;
6814 return false;
6817 /* Return true if FIELD is an artificial field. */
6819 static bool
6820 field_is_artificial (tree field)
6822 /* These fields are generated by the front-end proper. */
6823 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
6824 return true;
6826 /* These fields are generated by gigi. */
6827 if (DECL_INTERNAL_P (field))
6828 return true;
6830 return false;
6833 /* Return true if FIELD is a non-artificial aliased field. */
6835 static bool
6836 field_is_aliased (tree field)
6838 if (field_is_artificial (field))
6839 return false;
6841 return DECL_ALIASED_P (field);
6844 /* Return true if FIELD is a non-artificial field with self-referential
6845 size. */
6847 static bool
6848 field_has_self_size (tree field)
6850 if (field_is_artificial (field))
6851 return false;
6853 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6854 return false;
6856 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
6859 /* Return true if FIELD is a non-artificial field with variable size. */
6861 static bool
6862 field_has_variable_size (tree field)
6864 if (field_is_artificial (field))
6865 return false;
6867 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6868 return false;
6870 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
6873 /* qsort comparer for the bit positions of two record components. */
6875 static int
6876 compare_field_bitpos (const PTR rt1, const PTR rt2)
6878 const_tree const field1 = * (const_tree const *) rt1;
6879 const_tree const field2 = * (const_tree const *) rt2;
6880 const int ret
6881 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6883 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6886 /* Structure holding information for a given variant. */
6887 typedef struct vinfo
6889 /* The record type of the variant. */
6890 tree type;
6892 /* The name of the variant. */
6893 tree name;
6895 /* The qualifier of the variant. */
6896 tree qual;
6898 /* Whether the variant has a rep clause. */
6899 bool has_rep;
6901 /* Whether the variant is packed. */
6902 bool packed;
6904 } vinfo_t;
6906 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
6907 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
6908 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
6909 When called from gnat_to_gnu_entity during the processing of a record type
6910 definition, the GCC node for the parent, if any, will be the single field
6911 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6912 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6913 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6915 PACKED is 1 if this is for a packed record, -1 if this is for a record
6916 with Component_Alignment of Storage_Unit, -2 if this is for a record
6917 with a specified alignment.
6919 DEFINITION is true if we are defining this record type.
6921 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6922 out the record. This means the alignment only serves to force fields to
6923 be bitfields, but not to require the record to be that aligned. This is
6924 used for variants.
6926 ALL_REP is true if a rep clause is present for all the fields.
6928 UNCHECKED_UNION is true if we are building this type for a record with a
6929 Pragma Unchecked_Union.
6931 ARTIFICIAL is true if this is a type that was generated by the compiler.
6933 DEBUG_INFO is true if we need to write debug information about the type.
6935 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6936 mean that its contents may be unused as well, only the container itself.
6938 REORDER is true if we are permitted to reorder components of this type.
6940 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6941 the outer record type down to this variant level. It is nonzero only if
6942 all the fields down to this level have a rep clause and ALL_REP is false.
6944 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6945 with a rep clause is to be added; in this case, that is all that should
6946 be done with such fields and the return value will be false. */
6948 static bool
6949 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6950 tree gnu_field_list, int packed, bool definition,
6951 bool cancel_alignment, bool all_rep,
6952 bool unchecked_union, bool artificial,
6953 bool debug_info, bool maybe_unused, bool reorder,
6954 tree first_free_pos, tree *p_gnu_rep_list)
6956 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6957 bool variants_have_rep = all_rep;
6958 bool layout_with_rep = false;
6959 bool has_self_field = false;
6960 bool has_aliased_after_self_field = false;
6961 Node_Id component_decl, variant_part;
6962 tree gnu_field, gnu_next, gnu_last;
6963 tree gnu_variant_part = NULL_TREE;
6964 tree gnu_rep_list = NULL_TREE;
6965 tree gnu_var_list = NULL_TREE;
6966 tree gnu_self_list = NULL_TREE;
6967 tree gnu_zero_list = NULL_TREE;
6969 /* For each component referenced in a component declaration create a GCC
6970 field and add it to the list, skipping pragmas in the GNAT list. */
6971 gnu_last = tree_last (gnu_field_list);
6972 if (Present (Component_Items (gnat_component_list)))
6973 for (component_decl
6974 = First_Non_Pragma (Component_Items (gnat_component_list));
6975 Present (component_decl);
6976 component_decl = Next_Non_Pragma (component_decl))
6978 Entity_Id gnat_field = Defining_Entity (component_decl);
6979 Name_Id gnat_name = Chars (gnat_field);
6981 /* If present, the _Parent field must have been created as the single
6982 field of the record type. Put it before any other fields. */
6983 if (gnat_name == Name_uParent)
6985 gnu_field = TYPE_FIELDS (gnu_record_type);
6986 gnu_field_list = chainon (gnu_field_list, gnu_field);
6988 else
6990 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
6991 definition, debug_info);
6993 /* If this is the _Tag field, put it before any other fields. */
6994 if (gnat_name == Name_uTag)
6995 gnu_field_list = chainon (gnu_field_list, gnu_field);
6997 /* If this is the _Controller field, put it before the other
6998 fields except for the _Tag or _Parent field. */
6999 else if (gnat_name == Name_uController && gnu_last)
7001 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7002 DECL_CHAIN (gnu_last) = gnu_field;
7005 /* If this is a regular field, put it after the other fields. */
7006 else
7008 DECL_CHAIN (gnu_field) = gnu_field_list;
7009 gnu_field_list = gnu_field;
7010 if (!gnu_last)
7011 gnu_last = gnu_field;
7013 /* And record information for the final layout. */
7014 if (field_has_self_size (gnu_field))
7015 has_self_field = true;
7016 else if (has_self_field && field_is_aliased (gnu_field))
7017 has_aliased_after_self_field = true;
7021 save_gnu_tree (gnat_field, gnu_field, false);
7024 /* At the end of the component list there may be a variant part. */
7025 variant_part = Variant_Part (gnat_component_list);
7027 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7028 mutually exclusive and should go in the same memory. To do this we need
7029 to treat each variant as a record whose elements are created from the
7030 component list for the variant. So here we create the records from the
7031 lists for the variants and put them all into the QUAL_UNION_TYPE.
7032 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7033 use GNU_RECORD_TYPE if there are no fields so far. */
7034 if (Present (variant_part))
7036 Node_Id gnat_discr = Name (variant_part), variant;
7037 tree gnu_discr = gnat_to_gnu (gnat_discr);
7038 tree gnu_name = TYPE_NAME (gnu_record_type);
7039 tree gnu_var_name
7040 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7041 "XVN");
7042 tree gnu_union_type, gnu_union_name;
7043 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7044 bool union_field_needs_strict_alignment = false;
7045 auto_vec <vinfo_t, 16> variant_types;
7046 vinfo_t *gnu_variant;
7047 unsigned int variants_align = 0;
7048 unsigned int i;
7050 if (TREE_CODE (gnu_name) == TYPE_DECL)
7051 gnu_name = DECL_NAME (gnu_name);
7053 gnu_union_name
7054 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7056 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7057 are all in the variant part, to match the layout of C unions. There
7058 is an associated check below. */
7059 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7060 gnu_union_type = gnu_record_type;
7061 else
7063 gnu_union_type
7064 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7066 TYPE_NAME (gnu_union_type) = gnu_union_name;
7067 TYPE_ALIGN (gnu_union_type) = 0;
7068 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7071 /* If all the fields down to this level have a rep clause, find out
7072 whether all the fields at this level also have one. If so, then
7073 compute the new first free position to be passed downward. */
7074 this_first_free_pos = first_free_pos;
7075 if (this_first_free_pos)
7077 for (gnu_field = gnu_field_list;
7078 gnu_field;
7079 gnu_field = DECL_CHAIN (gnu_field))
7080 if (DECL_FIELD_OFFSET (gnu_field))
7082 tree pos = bit_position (gnu_field);
7083 if (!tree_int_cst_lt (pos, this_first_free_pos))
7084 this_first_free_pos
7085 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7087 else
7089 this_first_free_pos = NULL_TREE;
7090 break;
7094 /* We build the variants in two passes. The bulk of the work is done in
7095 the first pass, that is to say translating the GNAT nodes, building
7096 the container types and computing the associated properties. However
7097 we cannot finish up the container types during this pass because we
7098 don't know where the variant part will be placed until the end. */
7099 for (variant = First_Non_Pragma (Variants (variant_part));
7100 Present (variant);
7101 variant = Next_Non_Pragma (variant))
7103 tree gnu_variant_type = make_node (RECORD_TYPE);
7104 tree gnu_inner_name, gnu_qual;
7105 bool has_rep;
7106 int field_packed;
7107 vinfo_t vinfo;
7109 Get_Variant_Encoding (variant);
7110 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7111 TYPE_NAME (gnu_variant_type)
7112 = concat_name (gnu_union_name,
7113 IDENTIFIER_POINTER (gnu_inner_name));
7115 /* Set the alignment of the inner type in case we need to make
7116 inner objects into bitfields, but then clear it out so the
7117 record actually gets only the alignment required. */
7118 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7119 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7121 /* Similarly, if the outer record has a size specified and all
7122 the fields have a rep clause, we can propagate the size. */
7123 if (all_rep_and_size)
7125 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7126 TYPE_SIZE_UNIT (gnu_variant_type)
7127 = TYPE_SIZE_UNIT (gnu_record_type);
7130 /* Add the fields into the record type for the variant. Note that
7131 we aren't sure to really use it at this point, see below. */
7132 has_rep
7133 = components_to_record (gnu_variant_type, Component_List (variant),
7134 NULL_TREE, packed, definition,
7135 !all_rep_and_size, all_rep,
7136 unchecked_union,
7137 true, debug_info, true, reorder,
7138 this_first_free_pos,
7139 all_rep || this_first_free_pos
7140 ? NULL : &gnu_rep_list);
7142 /* Translate the qualifier and annotate the GNAT node. */
7143 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7144 Set_Present_Expr (variant, annotate_value (gnu_qual));
7146 /* Deal with packedness like in gnat_to_gnu_field. */
7147 if (components_need_strict_alignment (Component_List (variant)))
7149 field_packed = 0;
7150 union_field_needs_strict_alignment = true;
7152 else
7153 field_packed
7154 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7156 /* Push this variant onto the stack for the second pass. */
7157 vinfo.type = gnu_variant_type;
7158 vinfo.name = gnu_inner_name;
7159 vinfo.qual = gnu_qual;
7160 vinfo.has_rep = has_rep;
7161 vinfo.packed = field_packed;
7162 variant_types.safe_push (vinfo);
7164 /* Compute the global properties that will determine the placement of
7165 the variant part. */
7166 variants_have_rep |= has_rep;
7167 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7168 variants_align = TYPE_ALIGN (gnu_variant_type);
7171 /* Round up the first free position to the alignment of the variant part
7172 for the variants without rep clause. This will guarantee a consistent
7173 layout independently of the placement of the variant part. */
7174 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7175 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7177 /* In the second pass, the container types are adjusted if necessary and
7178 finished up, then the corresponding fields of the variant part are
7179 built with their qualifier, unless this is an unchecked union. */
7180 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7182 tree gnu_variant_type = gnu_variant->type;
7183 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7185 /* If this is an Unchecked_Union whose fields are all in the variant
7186 part and we have a single field with no representation clause or
7187 placed at offset zero, use the field directly to match the layout
7188 of C unions. */
7189 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7190 && gnu_field_list
7191 && !DECL_CHAIN (gnu_field_list)
7192 && (!DECL_FIELD_OFFSET (gnu_field_list)
7193 || integer_zerop (bit_position (gnu_field_list))))
7195 gnu_field = gnu_field_list;
7196 DECL_CONTEXT (gnu_field) = gnu_record_type;
7198 else
7200 /* Finalize the variant type now. We used to throw away empty
7201 record types but we no longer do that because we need them to
7202 generate complete debug info for the variant; otherwise, the
7203 union type definition will be lacking the fields associated
7204 with these empty variants. */
7205 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7207 /* The variant part will be at offset 0 so we need to ensure
7208 that the fields are laid out starting from the first free
7209 position at this level. */
7210 tree gnu_rep_type = make_node (RECORD_TYPE);
7211 tree gnu_rep_part;
7212 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7213 gnu_rep_part
7214 = create_rep_part (gnu_rep_type, gnu_variant_type,
7215 this_first_free_pos);
7216 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7217 gnu_field_list = gnu_rep_part;
7218 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7219 false);
7222 if (debug_info)
7223 rest_of_record_type_compilation (gnu_variant_type);
7224 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7225 true, debug_info, gnat_component_list);
7227 gnu_field
7228 = create_field_decl (gnu_variant->name, gnu_variant_type,
7229 gnu_union_type,
7230 all_rep_and_size
7231 ? TYPE_SIZE (gnu_variant_type) : 0,
7232 variants_have_rep ? bitsize_zero_node : 0,
7233 gnu_variant->packed, 0);
7235 DECL_INTERNAL_P (gnu_field) = 1;
7237 if (!unchecked_union)
7238 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7241 DECL_CHAIN (gnu_field) = gnu_variant_list;
7242 gnu_variant_list = gnu_field;
7245 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7246 if (gnu_variant_list)
7248 int union_field_packed;
7250 if (all_rep_and_size)
7252 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7253 TYPE_SIZE_UNIT (gnu_union_type)
7254 = TYPE_SIZE_UNIT (gnu_record_type);
7257 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7258 all_rep_and_size ? 1 : 0, debug_info);
7260 /* If GNU_UNION_TYPE is our record type, it means we must have an
7261 Unchecked_Union with no fields. Verify that and, if so, just
7262 return. */
7263 if (gnu_union_type == gnu_record_type)
7265 gcc_assert (unchecked_union
7266 && !gnu_field_list
7267 && !gnu_rep_list);
7268 return variants_have_rep;
7271 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7272 debug_info, gnat_component_list);
7274 /* Deal with packedness like in gnat_to_gnu_field. */
7275 if (union_field_needs_strict_alignment)
7276 union_field_packed = 0;
7277 else
7278 union_field_packed
7279 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7281 gnu_variant_part
7282 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7283 all_rep_and_size
7284 ? TYPE_SIZE (gnu_union_type) : 0,
7285 variants_have_rep ? bitsize_zero_node : 0,
7286 union_field_packed, 0);
7288 DECL_INTERNAL_P (gnu_variant_part) = 1;
7292 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7293 permitted to reorder components, self-referential sizes or variable sizes.
7294 If they do, pull them out and put them onto the appropriate list. We have
7295 to do this in a separate pass since we want to handle the discriminants
7296 but can't play with them until we've used them in debugging data above.
7298 Similarly, pull out the fields with zero size and no rep clause, as they
7299 would otherwise modify the layout and thus very likely run afoul of the
7300 Ada semantics, which are different from those of C here.
7302 ??? If we reorder them, debugging information will be wrong but there is
7303 nothing that can be done about this at the moment. */
7304 gnu_last = NULL_TREE;
7306 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7307 do { \
7308 if (gnu_last) \
7309 DECL_CHAIN (gnu_last) = gnu_next; \
7310 else \
7311 gnu_field_list = gnu_next; \
7313 DECL_CHAIN (gnu_field) = (LIST); \
7314 (LIST) = gnu_field; \
7315 } while (0)
7317 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7319 gnu_next = DECL_CHAIN (gnu_field);
7321 if (DECL_FIELD_OFFSET (gnu_field))
7323 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7324 continue;
7327 if ((reorder || has_aliased_after_self_field)
7328 && field_has_self_size (gnu_field))
7330 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7331 continue;
7334 if (reorder && field_has_variable_size (gnu_field))
7336 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7337 continue;
7340 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7342 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7343 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7344 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7345 if (field_is_aliased (gnu_field))
7346 TYPE_ALIGN (gnu_record_type)
7347 = MAX (TYPE_ALIGN (gnu_record_type),
7348 TYPE_ALIGN (TREE_TYPE (gnu_field)));
7349 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7350 continue;
7353 gnu_last = gnu_field;
7356 #undef MOVE_FROM_FIELD_LIST_TO
7358 gnu_field_list = nreverse (gnu_field_list);
7360 /* If permitted, we reorder the fields as follows:
7362 1) all fixed length fields,
7363 2) all fields whose length doesn't depend on discriminants,
7364 3) all fields whose length depends on discriminants,
7365 4) the variant part,
7367 within the record and within each variant recursively. */
7368 if (reorder)
7369 gnu_field_list
7370 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7372 /* Otherwise, if there is an aliased field placed after a field whose length
7373 depends on discriminants, we put all the fields of the latter sort, last.
7374 We need to do this in case an object of this record type is mutable. */
7375 else if (has_aliased_after_self_field)
7376 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7378 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7379 in our REP list to the previous level because this level needs them in
7380 order to do a correct layout, i.e. avoid having overlapping fields. */
7381 if (p_gnu_rep_list && gnu_rep_list)
7382 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7384 /* Otherwise, sort the fields by bit position and put them into their own
7385 record, before the others, if we also have fields without rep clause. */
7386 else if (gnu_rep_list)
7388 tree gnu_rep_type, gnu_rep_part;
7389 int i, len = list_length (gnu_rep_list);
7390 tree *gnu_arr = XALLOCAVEC (tree, len);
7392 /* If all the fields have a rep clause, we can do a flat layout. */
7393 layout_with_rep = !gnu_field_list
7394 && (!gnu_variant_part || variants_have_rep);
7395 gnu_rep_type
7396 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7398 for (gnu_field = gnu_rep_list, i = 0;
7399 gnu_field;
7400 gnu_field = DECL_CHAIN (gnu_field), i++)
7401 gnu_arr[i] = gnu_field;
7403 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7405 /* Put the fields in the list in order of increasing position, which
7406 means we start from the end. */
7407 gnu_rep_list = NULL_TREE;
7408 for (i = len - 1; i >= 0; i--)
7410 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7411 gnu_rep_list = gnu_arr[i];
7412 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7415 if (layout_with_rep)
7416 gnu_field_list = gnu_rep_list;
7417 else
7419 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7421 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7422 without rep clause are laid out starting from this position.
7423 Therefore, we force it as a minimal size on the REP part. */
7424 gnu_rep_part
7425 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7427 /* Chain the REP part at the beginning of the field list. */
7428 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7429 gnu_field_list = gnu_rep_part;
7433 /* Chain the variant part at the end of the field list. */
7434 if (gnu_variant_part)
7435 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7437 if (cancel_alignment)
7438 TYPE_ALIGN (gnu_record_type) = 0;
7440 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7442 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7443 debug_info && !maybe_unused);
7445 /* Chain the fields with zero size at the beginning of the field list. */
7446 if (gnu_zero_list)
7447 TYPE_FIELDS (gnu_record_type)
7448 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7450 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7453 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7454 placed into an Esize, Component_Bit_Offset, or Component_Size value
7455 in the GNAT tree. */
7457 static Uint
7458 annotate_value (tree gnu_size)
7460 TCode tcode;
7461 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7462 struct tree_int_map in;
7463 int i;
7465 /* See if we've already saved the value for this node. */
7466 if (EXPR_P (gnu_size))
7468 struct tree_int_map *e;
7470 if (!annotate_value_cache)
7471 annotate_value_cache = htab_create_ggc (512, tree_int_map_hash,
7472 tree_int_map_eq, 0);
7473 in.base.from = gnu_size;
7474 e = (struct tree_int_map *)
7475 htab_find (annotate_value_cache, &in);
7477 if (e)
7478 return (Node_Ref_Or_Val) e->to;
7480 else
7481 in.base.from = NULL_TREE;
7483 /* If we do not return inside this switch, TCODE will be set to the
7484 code to use for a Create_Node operand and LEN (set above) will be
7485 the number of recursive calls for us to make. */
7487 switch (TREE_CODE (gnu_size))
7489 case INTEGER_CST:
7490 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7492 case COMPONENT_REF:
7493 /* The only case we handle here is a simple discriminant reference. */
7494 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7496 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7498 /* Climb up the chain of successive extensions, if any. */
7499 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7500 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7501 == parent_name_id)
7502 gnu_size = TREE_OPERAND (gnu_size, 0);
7504 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7505 return
7506 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7509 return No_Uint;
7511 CASE_CONVERT: case NON_LVALUE_EXPR:
7512 return annotate_value (TREE_OPERAND (gnu_size, 0));
7514 /* Now just list the operations we handle. */
7515 case COND_EXPR: tcode = Cond_Expr; break;
7516 case PLUS_EXPR: tcode = Plus_Expr; break;
7517 case MINUS_EXPR: tcode = Minus_Expr; break;
7518 case MULT_EXPR: tcode = Mult_Expr; break;
7519 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7520 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7521 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7522 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7523 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7524 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7525 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7526 case NEGATE_EXPR: tcode = Negate_Expr; break;
7527 case MIN_EXPR: tcode = Min_Expr; break;
7528 case MAX_EXPR: tcode = Max_Expr; break;
7529 case ABS_EXPR: tcode = Abs_Expr; break;
7530 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7531 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7532 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7533 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7534 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7535 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7536 case LT_EXPR: tcode = Lt_Expr; break;
7537 case LE_EXPR: tcode = Le_Expr; break;
7538 case GT_EXPR: tcode = Gt_Expr; break;
7539 case GE_EXPR: tcode = Ge_Expr; break;
7540 case EQ_EXPR: tcode = Eq_Expr; break;
7541 case NE_EXPR: tcode = Ne_Expr; break;
7543 case BIT_AND_EXPR:
7544 tcode = Bit_And_Expr;
7545 /* For negative values, build NEGATE_EXPR of the opposite. Such values
7546 appear in expressions containing aligning patterns. Note that, since
7547 sizetype is unsigned, we have to jump through some hoops. */
7548 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7550 tree op1 = TREE_OPERAND (gnu_size, 1);
7551 double_int signed_op1
7552 = tree_to_double_int (op1).sext (TYPE_PRECISION (sizetype));
7553 if (signed_op1.is_negative ())
7555 op1 = double_int_to_tree (sizetype, -signed_op1);
7556 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7559 break;
7561 case CALL_EXPR:
7563 tree t = maybe_inline_call_in_expr (gnu_size);
7564 if (t)
7565 return annotate_value (t);
7568 /* Fall through... */
7570 default:
7571 return No_Uint;
7574 /* Now get each of the operands that's relevant for this code. If any
7575 cannot be expressed as a repinfo node, say we can't. */
7576 for (i = 0; i < 3; i++)
7577 ops[i] = No_Uint;
7579 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7581 if (i == 1 && pre_op1 != No_Uint)
7582 ops[i] = pre_op1;
7583 else
7584 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7585 if (ops[i] == No_Uint)
7586 return No_Uint;
7589 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7591 /* Save the result in the cache. */
7592 if (in.base.from)
7594 struct tree_int_map **h;
7595 /* We can't assume the hash table data hasn't moved since the
7596 initial look up, so we have to search again. Allocating and
7597 inserting an entry at that point would be an alternative, but
7598 then we'd better discard the entry if we decided not to cache
7599 it. */
7600 h = (struct tree_int_map **)
7601 htab_find_slot (annotate_value_cache, &in, INSERT);
7602 gcc_assert (!*h);
7603 *h = ggc_alloc_tree_int_map ();
7604 (*h)->base.from = gnu_size;
7605 (*h)->to = ret;
7608 return ret;
7611 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7612 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7613 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7614 BY_REF is true if the object is used by reference. */
7616 void
7617 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7619 if (by_ref)
7621 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7622 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7623 else
7624 gnu_type = TREE_TYPE (gnu_type);
7627 if (Unknown_Esize (gnat_entity))
7629 if (TREE_CODE (gnu_type) == RECORD_TYPE
7630 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7631 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7632 else if (!size)
7633 size = TYPE_SIZE (gnu_type);
7635 if (size)
7636 Set_Esize (gnat_entity, annotate_value (size));
7639 if (Unknown_Alignment (gnat_entity))
7640 Set_Alignment (gnat_entity,
7641 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7644 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7645 Return NULL_TREE if there is no such element in the list. */
7647 static tree
7648 purpose_member_field (const_tree elem, tree list)
7650 while (list)
7652 tree field = TREE_PURPOSE (list);
7653 if (SAME_FIELD_P (field, elem))
7654 return list;
7655 list = TREE_CHAIN (list);
7657 return NULL_TREE;
7660 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7661 set Component_Bit_Offset and Esize of the components to the position and
7662 size used by Gigi. */
7664 static void
7665 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7667 Entity_Id gnat_field;
7668 tree gnu_list;
7670 /* We operate by first making a list of all fields and their position (we
7671 can get the size easily) and then update all the sizes in the tree. */
7672 gnu_list
7673 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7674 BIGGEST_ALIGNMENT, NULL_TREE);
7676 for (gnat_field = First_Entity (gnat_entity);
7677 Present (gnat_field);
7678 gnat_field = Next_Entity (gnat_field))
7679 if (Ekind (gnat_field) == E_Component
7680 || (Ekind (gnat_field) == E_Discriminant
7681 && !Is_Unchecked_Union (Scope (gnat_field))))
7683 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7684 gnu_list);
7685 if (t)
7687 tree parent_offset;
7689 /* If we are just annotating types and the type is tagged, the tag
7690 and the parent components are not generated by the front-end so
7691 we need to add the appropriate offset to each component without
7692 representation clause. */
7693 if (type_annotate_only
7694 && Is_Tagged_Type (gnat_entity)
7695 && No (Component_Clause (gnat_field)))
7697 /* For a component appearing in the current extension, the
7698 offset is the size of the parent. */
7699 if (Is_Derived_Type (gnat_entity)
7700 && Original_Record_Component (gnat_field) == gnat_field)
7701 parent_offset
7702 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7703 bitsizetype);
7704 else
7705 parent_offset = bitsize_int (POINTER_SIZE);
7707 if (TYPE_FIELDS (gnu_type))
7708 parent_offset
7709 = round_up (parent_offset,
7710 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
7712 else
7713 parent_offset = bitsize_zero_node;
7715 Set_Component_Bit_Offset
7716 (gnat_field,
7717 annotate_value
7718 (size_binop (PLUS_EXPR,
7719 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7720 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7721 parent_offset)));
7723 Set_Esize (gnat_field,
7724 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7726 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7728 /* If there is no entry, this is an inherited component whose
7729 position is the same as in the parent type. */
7730 Set_Component_Bit_Offset
7731 (gnat_field,
7732 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7734 Set_Esize (gnat_field,
7735 Esize (Original_Record_Component (gnat_field)));
7740 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7741 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7742 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7743 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7744 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7745 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7746 pre-existing list to be chained to the newly created entries. */
7748 static tree
7749 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7750 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7752 tree gnu_field;
7754 for (gnu_field = TYPE_FIELDS (gnu_type);
7755 gnu_field;
7756 gnu_field = DECL_CHAIN (gnu_field))
7758 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7759 DECL_FIELD_BIT_OFFSET (gnu_field));
7760 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7761 DECL_FIELD_OFFSET (gnu_field));
7762 unsigned int our_offset_align
7763 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7764 tree v = make_tree_vec (3);
7766 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7767 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7768 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7769 gnu_list = tree_cons (gnu_field, v, gnu_list);
7771 /* Recurse on internal fields, flattening the nested fields except for
7772 those in the variant part, if requested. */
7773 if (DECL_INTERNAL_P (gnu_field))
7775 tree gnu_field_type = TREE_TYPE (gnu_field);
7776 if (do_not_flatten_variant
7777 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7778 gnu_list
7779 = build_position_list (gnu_field_type, do_not_flatten_variant,
7780 size_zero_node, bitsize_zero_node,
7781 BIGGEST_ALIGNMENT, gnu_list);
7782 else
7783 gnu_list
7784 = build_position_list (gnu_field_type, do_not_flatten_variant,
7785 gnu_our_offset, gnu_our_bitpos,
7786 our_offset_align, gnu_list);
7790 return gnu_list;
7793 /* Return a list describing the substitutions needed to reflect the
7794 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7795 be in any order. The values in an element of the list are in the form
7796 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7797 a definition of GNAT_SUBTYPE. */
7799 static vec<subst_pair>
7800 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7802 vec<subst_pair> gnu_list = vNULL;
7803 Entity_Id gnat_discrim;
7804 Node_Id gnat_constr;
7806 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7807 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
7808 Present (gnat_discrim);
7809 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7810 gnat_constr = Next_Elmt (gnat_constr))
7811 /* Ignore access discriminants. */
7812 if (!Is_Access_Type (Etype (Node (gnat_constr))))
7814 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7815 tree replacement = convert (TREE_TYPE (gnu_field),
7816 elaborate_expression
7817 (Node (gnat_constr), gnat_subtype,
7818 get_entity_name (gnat_discrim),
7819 definition, true, false));
7820 subst_pair s = {gnu_field, replacement};
7821 gnu_list.safe_push (s);
7824 return gnu_list;
7827 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7828 variants of QUAL_UNION_TYPE that are still relevant after applying
7829 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
7830 list to be prepended to the newly created entries. */
7832 static vec<variant_desc>
7833 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
7834 vec<variant_desc> gnu_list)
7836 tree gnu_field;
7838 for (gnu_field = TYPE_FIELDS (qual_union_type);
7839 gnu_field;
7840 gnu_field = DECL_CHAIN (gnu_field))
7842 tree qual = DECL_QUALIFIER (gnu_field);
7843 unsigned int i;
7844 subst_pair *s;
7846 FOR_EACH_VEC_ELT (subst_list, i, s)
7847 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7849 /* If the new qualifier is not unconditionally false, its variant may
7850 still be accessed. */
7851 if (!integer_zerop (qual))
7853 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7854 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
7856 gnu_list.safe_push (v);
7858 /* Recurse on the variant subpart of the variant, if any. */
7859 variant_subpart = get_variant_part (variant_type);
7860 if (variant_subpart)
7861 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
7862 subst_list, gnu_list);
7864 /* If the new qualifier is unconditionally true, the subsequent
7865 variants cannot be accessed. */
7866 if (integer_onep (qual))
7867 break;
7871 return gnu_list;
7874 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7875 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7876 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7877 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7878 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7879 true if we are being called to process the Component_Size of GNAT_OBJECT;
7880 this is used only for error messages. ZERO_OK is true if a size of zero
7881 is permitted; if ZERO_OK is false, it means that a size of zero should be
7882 treated as an unspecified size. */
7884 static tree
7885 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7886 enum tree_code kind, bool component_p, bool zero_ok)
7888 Node_Id gnat_error_node;
7889 tree type_size, size;
7891 /* Return 0 if no size was specified. */
7892 if (uint_size == No_Uint)
7893 return NULL_TREE;
7895 /* Ignore a negative size since that corresponds to our back-annotation. */
7896 if (UI_Lt (uint_size, Uint_0))
7897 return NULL_TREE;
7899 /* Find the node to use for error messages. */
7900 if ((Ekind (gnat_object) == E_Component
7901 || Ekind (gnat_object) == E_Discriminant)
7902 && Present (Component_Clause (gnat_object)))
7903 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7904 else if (Present (Size_Clause (gnat_object)))
7905 gnat_error_node = Expression (Size_Clause (gnat_object));
7906 else
7907 gnat_error_node = gnat_object;
7909 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7910 but cannot be represented in bitsizetype. */
7911 size = UI_To_gnu (uint_size, bitsizetype);
7912 if (TREE_OVERFLOW (size))
7914 if (component_p)
7915 post_error_ne ("component size for& is too large", gnat_error_node,
7916 gnat_object);
7917 else
7918 post_error_ne ("size for& is too large", gnat_error_node,
7919 gnat_object);
7920 return NULL_TREE;
7923 /* Ignore a zero size if it is not permitted. */
7924 if (!zero_ok && integer_zerop (size))
7925 return NULL_TREE;
7927 /* The size of objects is always a multiple of a byte. */
7928 if (kind == VAR_DECL
7929 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7931 if (component_p)
7932 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7933 gnat_error_node, gnat_object);
7934 else
7935 post_error_ne ("size for& is not a multiple of Storage_Unit",
7936 gnat_error_node, gnat_object);
7937 return NULL_TREE;
7940 /* If this is an integral type or a packed array type, the front-end has
7941 already verified the size, so we need not do it here (which would mean
7942 checking against the bounds). However, if this is an aliased object,
7943 it may not be smaller than the type of the object. */
7944 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7945 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7946 return size;
7948 /* If the object is a record that contains a template, add the size of the
7949 template to the specified size. */
7950 if (TREE_CODE (gnu_type) == RECORD_TYPE
7951 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7952 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7954 if (kind == VAR_DECL
7955 /* If a type needs strict alignment, a component of this type in
7956 a packed record cannot be packed and thus uses the type size. */
7957 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7958 type_size = TYPE_SIZE (gnu_type);
7959 else
7960 type_size = rm_size (gnu_type);
7962 /* Modify the size of a discriminated type to be the maximum size. */
7963 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7964 type_size = max_size (type_size, true);
7966 /* If this is an access type or a fat pointer, the minimum size is that given
7967 by the smallest integral mode that's valid for pointers. */
7968 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7970 enum machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7971 while (!targetm.valid_pointer_mode (p_mode))
7972 p_mode = GET_MODE_WIDER_MODE (p_mode);
7973 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7976 /* Issue an error either if the default size of the object isn't a constant
7977 or if the new size is smaller than it. */
7978 if (TREE_CODE (type_size) != INTEGER_CST
7979 || TREE_OVERFLOW (type_size)
7980 || tree_int_cst_lt (size, type_size))
7982 if (component_p)
7983 post_error_ne_tree
7984 ("component size for& too small{, minimum allowed is ^}",
7985 gnat_error_node, gnat_object, type_size);
7986 else
7987 post_error_ne_tree
7988 ("size for& too small{, minimum allowed is ^}",
7989 gnat_error_node, gnat_object, type_size);
7990 return NULL_TREE;
7993 return size;
7996 /* Similarly, but both validate and process a value of RM size. This routine
7997 is only called for types. */
7999 static void
8000 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
8002 Node_Id gnat_attr_node;
8003 tree old_size, size;
8005 /* Do nothing if no size was specified. */
8006 if (uint_size == No_Uint)
8007 return;
8009 /* Ignore a negative size since that corresponds to our back-annotation. */
8010 if (UI_Lt (uint_size, Uint_0))
8011 return;
8013 /* Only issue an error if a Value_Size clause was explicitly given.
8014 Otherwise, we'd be duplicating an error on the Size clause. */
8015 gnat_attr_node
8016 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
8018 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8019 but cannot be represented in bitsizetype. */
8020 size = UI_To_gnu (uint_size, bitsizetype);
8021 if (TREE_OVERFLOW (size))
8023 if (Present (gnat_attr_node))
8024 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8025 gnat_entity);
8026 return;
8029 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8030 exists, or this is an integer type, in which case the front-end will
8031 have always set it. */
8032 if (No (gnat_attr_node)
8033 && integer_zerop (size)
8034 && !Has_Size_Clause (gnat_entity)
8035 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8036 return;
8038 old_size = rm_size (gnu_type);
8040 /* If the old size is self-referential, get the maximum size. */
8041 if (CONTAINS_PLACEHOLDER_P (old_size))
8042 old_size = max_size (old_size, true);
8044 /* Issue an error either if the old size of the object isn't a constant or
8045 if the new size is smaller than it. The front-end has already verified
8046 this for scalar and packed array types. */
8047 if (TREE_CODE (old_size) != INTEGER_CST
8048 || TREE_OVERFLOW (old_size)
8049 || (AGGREGATE_TYPE_P (gnu_type)
8050 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8051 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8052 && !(TYPE_IS_PADDING_P (gnu_type)
8053 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8054 && TYPE_PACKED_ARRAY_TYPE_P
8055 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8056 && tree_int_cst_lt (size, old_size)))
8058 if (Present (gnat_attr_node))
8059 post_error_ne_tree
8060 ("Value_Size for& too small{, minimum allowed is ^}",
8061 gnat_attr_node, gnat_entity, old_size);
8062 return;
8065 /* Otherwise, set the RM size proper for integral types... */
8066 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8067 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8068 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8069 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8070 SET_TYPE_RM_SIZE (gnu_type, size);
8072 /* ...or the Ada size for record and union types. */
8073 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8074 && !TYPE_FAT_POINTER_P (gnu_type))
8075 SET_TYPE_ADA_SIZE (gnu_type, size);
8078 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8079 a type or object whose present alignment is ALIGN. If this alignment is
8080 valid, return it. Otherwise, give an error and return ALIGN. */
8082 static unsigned int
8083 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8085 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8086 unsigned int new_align;
8087 Node_Id gnat_error_node;
8089 /* Don't worry about checking alignment if alignment was not specified
8090 by the source program and we already posted an error for this entity. */
8091 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8092 return align;
8094 /* Post the error on the alignment clause if any. Note, for the implicit
8095 base type of an array type, the alignment clause is on the first
8096 subtype. */
8097 if (Present (Alignment_Clause (gnat_entity)))
8098 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8100 else if (Is_Itype (gnat_entity)
8101 && Is_Array_Type (gnat_entity)
8102 && Etype (gnat_entity) == gnat_entity
8103 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8104 gnat_error_node =
8105 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8107 else
8108 gnat_error_node = gnat_entity;
8110 /* Within GCC, an alignment is an integer, so we must make sure a value is
8111 specified that fits in that range. Also, there is an upper bound to
8112 alignments we can support/allow. */
8113 if (!UI_Is_In_Int_Range (alignment)
8114 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8115 post_error_ne_num ("largest supported alignment for& is ^",
8116 gnat_error_node, gnat_entity, max_allowed_alignment);
8117 else if (!(Present (Alignment_Clause (gnat_entity))
8118 && From_At_Mod (Alignment_Clause (gnat_entity)))
8119 && new_align * BITS_PER_UNIT < align)
8121 unsigned int double_align;
8122 bool is_capped_double, align_clause;
8124 /* If the default alignment of "double" or larger scalar types is
8125 specifically capped and the new alignment is above the cap, do
8126 not post an error and change the alignment only if there is an
8127 alignment clause; this makes it possible to have the associated
8128 GCC type overaligned by default for performance reasons. */
8129 if ((double_align = double_float_alignment) > 0)
8131 Entity_Id gnat_type
8132 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8133 is_capped_double
8134 = is_double_float_or_array (gnat_type, &align_clause);
8136 else if ((double_align = double_scalar_alignment) > 0)
8138 Entity_Id gnat_type
8139 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8140 is_capped_double
8141 = is_double_scalar_or_array (gnat_type, &align_clause);
8143 else
8144 is_capped_double = align_clause = false;
8146 if (is_capped_double && new_align >= double_align)
8148 if (align_clause)
8149 align = new_align * BITS_PER_UNIT;
8151 else
8153 if (is_capped_double)
8154 align = double_align * BITS_PER_UNIT;
8156 post_error_ne_num ("alignment for& must be at least ^",
8157 gnat_error_node, gnat_entity,
8158 align / BITS_PER_UNIT);
8161 else
8163 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8164 if (new_align > align)
8165 align = new_align;
8168 return align;
8171 /* Verify that OBJECT, a type or decl, is something we can implement
8172 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
8173 if we require atomic components. */
8175 static void
8176 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
8178 Node_Id gnat_error_point = gnat_entity;
8179 Node_Id gnat_node;
8180 enum machine_mode mode;
8181 unsigned int align;
8182 tree size;
8184 /* There are three case of what OBJECT can be. It can be a type, in which
8185 case we take the size, alignment and mode from the type. It can be a
8186 declaration that was indirect, in which case the relevant values are
8187 that of the type being pointed to, or it can be a normal declaration,
8188 in which case the values are of the decl. The code below assumes that
8189 OBJECT is either a type or a decl. */
8190 if (TYPE_P (object))
8192 /* If this is an anonymous base type, nothing to check. Error will be
8193 reported on the source type. */
8194 if (!Comes_From_Source (gnat_entity))
8195 return;
8197 mode = TYPE_MODE (object);
8198 align = TYPE_ALIGN (object);
8199 size = TYPE_SIZE (object);
8201 else if (DECL_BY_REF_P (object))
8203 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
8204 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
8205 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
8207 else
8209 mode = DECL_MODE (object);
8210 align = DECL_ALIGN (object);
8211 size = DECL_SIZE (object);
8214 /* Consider all floating-point types atomic and any types that that are
8215 represented by integers no wider than a machine word. */
8216 if (GET_MODE_CLASS (mode) == MODE_FLOAT
8217 || ((GET_MODE_CLASS (mode) == MODE_INT
8218 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
8219 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8220 return;
8222 /* For the moment, also allow anything that has an alignment equal
8223 to its size and which is smaller than a word. */
8224 if (size && TREE_CODE (size) == INTEGER_CST
8225 && compare_tree_int (size, align) == 0
8226 && align <= BITS_PER_WORD)
8227 return;
8229 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
8230 gnat_node = Next_Rep_Item (gnat_node))
8232 if (!comp_p && Nkind (gnat_node) == N_Pragma
8233 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8234 == Pragma_Atomic))
8235 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8236 else if (comp_p && Nkind (gnat_node) == N_Pragma
8237 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8238 == Pragma_Atomic_Components))
8239 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8242 if (comp_p)
8243 post_error_ne ("atomic access to component of & cannot be guaranteed",
8244 gnat_error_point, gnat_entity);
8245 else
8246 post_error_ne ("atomic access to & cannot be guaranteed",
8247 gnat_error_point, gnat_entity);
8251 /* Helper for the intrin compatibility checks family. Evaluate whether
8252 two types are definitely incompatible. */
8254 static bool
8255 intrin_types_incompatible_p (tree t1, tree t2)
8257 enum tree_code code;
8259 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8260 return false;
8262 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8263 return true;
8265 if (TREE_CODE (t1) != TREE_CODE (t2))
8266 return true;
8268 code = TREE_CODE (t1);
8270 switch (code)
8272 case INTEGER_TYPE:
8273 case REAL_TYPE:
8274 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8276 case POINTER_TYPE:
8277 case REFERENCE_TYPE:
8278 /* Assume designated types are ok. We'd need to account for char * and
8279 void * variants to do better, which could rapidly get messy and isn't
8280 clearly worth the effort. */
8281 return false;
8283 default:
8284 break;
8287 return false;
8290 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8291 on the Ada/builtin argument lists for the INB binding. */
8293 static bool
8294 intrin_arglists_compatible_p (intrin_binding_t * inb)
8296 function_args_iterator ada_iter, btin_iter;
8298 function_args_iter_init (&ada_iter, inb->ada_fntype);
8299 function_args_iter_init (&btin_iter, inb->btin_fntype);
8301 /* Sequence position of the last argument we checked. */
8302 int argpos = 0;
8304 while (1)
8306 tree ada_type = function_args_iter_cond (&ada_iter);
8307 tree btin_type = function_args_iter_cond (&btin_iter);
8309 /* If we've exhausted both lists simultaneously, we're done. */
8310 if (ada_type == NULL_TREE && btin_type == NULL_TREE)
8311 break;
8313 /* If one list is shorter than the other, they fail to match. */
8314 if (ada_type == NULL_TREE || btin_type == NULL_TREE)
8315 return false;
8317 /* If we're done with the Ada args and not with the internal builtin
8318 args, or the other way around, complain. */
8319 if (ada_type == void_type_node
8320 && btin_type != void_type_node)
8322 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8323 return false;
8326 if (btin_type == void_type_node
8327 && ada_type != void_type_node)
8329 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8330 inb->gnat_entity, inb->gnat_entity, argpos);
8331 return false;
8334 /* Otherwise, check that types match for the current argument. */
8335 argpos ++;
8336 if (intrin_types_incompatible_p (ada_type, btin_type))
8338 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8339 inb->gnat_entity, inb->gnat_entity, argpos);
8340 return false;
8344 function_args_iter_next (&ada_iter);
8345 function_args_iter_next (&btin_iter);
8348 return true;
8351 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8352 on the Ada/builtin return values for the INB binding. */
8354 static bool
8355 intrin_return_compatible_p (intrin_binding_t * inb)
8357 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8358 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8360 /* Accept function imported as procedure, common and convenient. */
8361 if (VOID_TYPE_P (ada_return_type)
8362 && !VOID_TYPE_P (btin_return_type))
8363 return true;
8365 /* If return type is Address (integer type), map it to void *. */
8366 if (Is_Descendent_Of_Address (Etype (inb->gnat_entity)))
8367 ada_return_type = ptr_void_type_node;
8369 /* Check return types compatibility otherwise. Note that this
8370 handles void/void as well. */
8371 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8373 post_error ("?intrinsic binding type mismatch on return value!",
8374 inb->gnat_entity);
8375 return false;
8378 return true;
8381 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8382 compatible. Issue relevant warnings when they are not.
8384 This is intended as a light check to diagnose the most obvious cases, not
8385 as a full fledged type compatibility predicate. It is the programmer's
8386 responsibility to ensure correctness of the Ada declarations in Imports,
8387 especially when binding straight to a compiler internal. */
8389 static bool
8390 intrin_profiles_compatible_p (intrin_binding_t * inb)
8392 /* Check compatibility on return values and argument lists, each responsible
8393 for posting warnings as appropriate. Ensure use of the proper sloc for
8394 this purpose. */
8396 bool arglists_compatible_p, return_compatible_p;
8397 location_t saved_location = input_location;
8399 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8401 return_compatible_p = intrin_return_compatible_p (inb);
8402 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8404 input_location = saved_location;
8406 return return_compatible_p && arglists_compatible_p;
8409 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8410 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8411 specified size for this field. POS_LIST is a position list describing
8412 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8413 to this layout. */
8415 static tree
8416 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8417 tree size, tree pos_list,
8418 vec<subst_pair> subst_list)
8420 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8421 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8422 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8423 tree new_pos, new_field;
8424 unsigned int i;
8425 subst_pair *s;
8427 if (CONTAINS_PLACEHOLDER_P (pos))
8428 FOR_EACH_VEC_ELT (subst_list, i, s)
8429 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8431 /* If the position is now a constant, we can set it as the position of the
8432 field when we make it. Otherwise, we need to deal with it specially. */
8433 if (TREE_CONSTANT (pos))
8434 new_pos = bit_from_pos (pos, bitpos);
8435 else
8436 new_pos = NULL_TREE;
8438 new_field
8439 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8440 size, new_pos, DECL_PACKED (old_field),
8441 !DECL_NONADDRESSABLE_P (old_field));
8443 if (!new_pos)
8445 normalize_offset (&pos, &bitpos, offset_align);
8446 /* Finalize the position. */
8447 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8448 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8449 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8450 DECL_SIZE (new_field) = size;
8451 DECL_SIZE_UNIT (new_field)
8452 = convert (sizetype,
8453 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8454 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8457 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8458 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8459 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8460 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8462 return new_field;
8465 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8466 it is the minimal size the REP_PART must have. */
8468 static tree
8469 create_rep_part (tree rep_type, tree record_type, tree min_size)
8471 tree field;
8473 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8474 min_size = NULL_TREE;
8476 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8477 min_size, NULL_TREE, 0, 1);
8478 DECL_INTERNAL_P (field) = 1;
8480 return field;
8483 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8485 static tree
8486 get_rep_part (tree record_type)
8488 tree field = TYPE_FIELDS (record_type);
8490 /* The REP part is the first field, internal, another record, and its name
8491 starts with an 'R'. */
8492 if (field
8493 && DECL_INTERNAL_P (field)
8494 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8495 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8496 return field;
8498 return NULL_TREE;
8501 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8503 tree
8504 get_variant_part (tree record_type)
8506 tree field;
8508 /* The variant part is the only internal field that is a qualified union. */
8509 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8510 if (DECL_INTERNAL_P (field)
8511 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8512 return field;
8514 return NULL_TREE;
8517 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8518 the list of variants to be used and RECORD_TYPE is the type of the parent.
8519 POS_LIST is a position list describing the layout of fields present in
8520 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8521 layout. */
8523 static tree
8524 create_variant_part_from (tree old_variant_part,
8525 vec<variant_desc> variant_list,
8526 tree record_type, tree pos_list,
8527 vec<subst_pair> subst_list)
8529 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8530 tree old_union_type = TREE_TYPE (old_variant_part);
8531 tree new_union_type, new_variant_part;
8532 tree union_field_list = NULL_TREE;
8533 variant_desc *v;
8534 unsigned int i;
8536 /* First create the type of the variant part from that of the old one. */
8537 new_union_type = make_node (QUAL_UNION_TYPE);
8538 TYPE_NAME (new_union_type)
8539 = concat_name (TYPE_NAME (record_type),
8540 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8542 /* If the position of the variant part is constant, subtract it from the
8543 size of the type of the parent to get the new size. This manual CSE
8544 reduces the code size when not optimizing. */
8545 if (TREE_CODE (offset) == INTEGER_CST)
8547 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8548 tree first_bit = bit_from_pos (offset, bitpos);
8549 TYPE_SIZE (new_union_type)
8550 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8551 TYPE_SIZE_UNIT (new_union_type)
8552 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8553 byte_from_pos (offset, bitpos));
8554 SET_TYPE_ADA_SIZE (new_union_type,
8555 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8556 first_bit));
8557 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8558 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8560 else
8561 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8563 /* Now finish up the new variants and populate the union type. */
8564 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8566 tree old_field = v->field, new_field;
8567 tree old_variant, old_variant_subpart, new_variant, field_list;
8569 /* Skip variants that don't belong to this nesting level. */
8570 if (DECL_CONTEXT (old_field) != old_union_type)
8571 continue;
8573 /* Retrieve the list of fields already added to the new variant. */
8574 new_variant = v->new_type;
8575 field_list = TYPE_FIELDS (new_variant);
8577 /* If the old variant had a variant subpart, we need to create a new
8578 variant subpart and add it to the field list. */
8579 old_variant = v->type;
8580 old_variant_subpart = get_variant_part (old_variant);
8581 if (old_variant_subpart)
8583 tree new_variant_subpart
8584 = create_variant_part_from (old_variant_subpart, variant_list,
8585 new_variant, pos_list, subst_list);
8586 DECL_CHAIN (new_variant_subpart) = field_list;
8587 field_list = new_variant_subpart;
8590 /* Finish up the new variant and create the field. No need for debug
8591 info thanks to the XVS type. */
8592 finish_record_type (new_variant, nreverse (field_list), 2, false);
8593 compute_record_mode (new_variant);
8594 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
8595 Empty);
8597 new_field
8598 = create_field_decl_from (old_field, new_variant, new_union_type,
8599 TYPE_SIZE (new_variant),
8600 pos_list, subst_list);
8601 DECL_QUALIFIER (new_field) = v->qual;
8602 DECL_INTERNAL_P (new_field) = 1;
8603 DECL_CHAIN (new_field) = union_field_list;
8604 union_field_list = new_field;
8607 /* Finish up the union type and create the variant part. No need for debug
8608 info thanks to the XVS type. Note that we don't reverse the field list
8609 because VARIANT_LIST has been traversed in reverse order. */
8610 finish_record_type (new_union_type, union_field_list, 2, false);
8611 compute_record_mode (new_union_type);
8612 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
8613 Empty);
8615 new_variant_part
8616 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8617 TYPE_SIZE (new_union_type),
8618 pos_list, subst_list);
8619 DECL_INTERNAL_P (new_variant_part) = 1;
8621 /* With multiple discriminants it is possible for an inner variant to be
8622 statically selected while outer ones are not; in this case, the list
8623 of fields of the inner variant is not flattened and we end up with a
8624 qualified union with a single member. Drop the useless container. */
8625 if (!DECL_CHAIN (union_field_list))
8627 DECL_CONTEXT (union_field_list) = record_type;
8628 DECL_FIELD_OFFSET (union_field_list)
8629 = DECL_FIELD_OFFSET (new_variant_part);
8630 DECL_FIELD_BIT_OFFSET (union_field_list)
8631 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8632 SET_DECL_OFFSET_ALIGN (union_field_list,
8633 DECL_OFFSET_ALIGN (new_variant_part));
8634 new_variant_part = union_field_list;
8637 return new_variant_part;
8640 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8641 which are both RECORD_TYPE, after applying the substitutions described
8642 in SUBST_LIST. */
8644 static void
8645 copy_and_substitute_in_size (tree new_type, tree old_type,
8646 vec<subst_pair> subst_list)
8648 unsigned int i;
8649 subst_pair *s;
8651 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8652 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8653 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8654 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8655 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8657 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8658 FOR_EACH_VEC_ELT (subst_list, i, s)
8659 TYPE_SIZE (new_type)
8660 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8661 s->discriminant, s->replacement);
8663 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8664 FOR_EACH_VEC_ELT (subst_list, i, s)
8665 TYPE_SIZE_UNIT (new_type)
8666 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8667 s->discriminant, s->replacement);
8669 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8670 FOR_EACH_VEC_ELT (subst_list, i, s)
8671 SET_TYPE_ADA_SIZE
8672 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8673 s->discriminant, s->replacement));
8675 /* Finalize the size. */
8676 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8677 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8680 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8681 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8682 updated by replacing F with R.
8684 The function doesn't update the layout of the type, i.e. it assumes
8685 that the substitution is purely formal. That's why the replacement
8686 value R must itself contain a PLACEHOLDER_EXPR. */
8688 tree
8689 substitute_in_type (tree t, tree f, tree r)
8691 tree nt;
8693 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8695 switch (TREE_CODE (t))
8697 case INTEGER_TYPE:
8698 case ENUMERAL_TYPE:
8699 case BOOLEAN_TYPE:
8700 case REAL_TYPE:
8702 /* First the domain types of arrays. */
8703 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8704 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8706 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8707 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8709 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8710 return t;
8712 nt = copy_type (t);
8713 TYPE_GCC_MIN_VALUE (nt) = low;
8714 TYPE_GCC_MAX_VALUE (nt) = high;
8716 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8717 SET_TYPE_INDEX_TYPE
8718 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8720 return nt;
8723 /* Then the subtypes. */
8724 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8725 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8727 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8728 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8730 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8731 return t;
8733 nt = copy_type (t);
8734 SET_TYPE_RM_MIN_VALUE (nt, low);
8735 SET_TYPE_RM_MAX_VALUE (nt, high);
8737 return nt;
8740 return t;
8742 case COMPLEX_TYPE:
8743 nt = substitute_in_type (TREE_TYPE (t), f, r);
8744 if (nt == TREE_TYPE (t))
8745 return t;
8747 return build_complex_type (nt);
8749 case FUNCTION_TYPE:
8750 /* These should never show up here. */
8751 gcc_unreachable ();
8753 case ARRAY_TYPE:
8755 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8756 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8758 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8759 return t;
8761 nt = build_nonshared_array_type (component, domain);
8762 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8763 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8764 SET_TYPE_MODE (nt, TYPE_MODE (t));
8765 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8766 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8767 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8768 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8769 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8770 return nt;
8773 case RECORD_TYPE:
8774 case UNION_TYPE:
8775 case QUAL_UNION_TYPE:
8777 bool changed_field = false;
8778 tree field;
8780 /* Start out with no fields, make new fields, and chain them
8781 in. If we haven't actually changed the type of any field,
8782 discard everything we've done and return the old type. */
8783 nt = copy_type (t);
8784 TYPE_FIELDS (nt) = NULL_TREE;
8786 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8788 tree new_field = copy_node (field), new_n;
8790 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8791 if (new_n != TREE_TYPE (field))
8793 TREE_TYPE (new_field) = new_n;
8794 changed_field = true;
8797 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8798 if (new_n != DECL_FIELD_OFFSET (field))
8800 DECL_FIELD_OFFSET (new_field) = new_n;
8801 changed_field = true;
8804 /* Do the substitution inside the qualifier, if any. */
8805 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8807 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8808 if (new_n != DECL_QUALIFIER (field))
8810 DECL_QUALIFIER (new_field) = new_n;
8811 changed_field = true;
8815 DECL_CONTEXT (new_field) = nt;
8816 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8818 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8819 TYPE_FIELDS (nt) = new_field;
8822 if (!changed_field)
8823 return t;
8825 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8826 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8827 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8828 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8829 return nt;
8832 default:
8833 return t;
8837 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8838 needed to represent the object. */
8840 tree
8841 rm_size (tree gnu_type)
8843 /* For integral types, we store the RM size explicitly. */
8844 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8845 return TYPE_RM_SIZE (gnu_type);
8847 /* Return the RM size of the actual data plus the size of the template. */
8848 if (TREE_CODE (gnu_type) == RECORD_TYPE
8849 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8850 return
8851 size_binop (PLUS_EXPR,
8852 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8853 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8855 /* For record or union types, we store the size explicitly. */
8856 if (RECORD_OR_UNION_TYPE_P (gnu_type)
8857 && !TYPE_FAT_POINTER_P (gnu_type)
8858 && TYPE_ADA_SIZE (gnu_type))
8859 return TYPE_ADA_SIZE (gnu_type);
8861 /* For other types, this is just the size. */
8862 return TYPE_SIZE (gnu_type);
8865 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8866 fully-qualified name, possibly with type information encoding.
8867 Otherwise, return the name. */
8869 tree
8870 get_entity_name (Entity_Id gnat_entity)
8872 Get_Encoded_Name (gnat_entity);
8873 return get_identifier_with_length (Name_Buffer, Name_Len);
8876 /* Return an identifier representing the external name to be used for
8877 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8878 and the specified suffix. */
8880 tree
8881 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8883 Entity_Kind kind = Ekind (gnat_entity);
8885 if (suffix)
8887 String_Template temp = {1, (int) strlen (suffix)};
8888 Fat_Pointer fp = {suffix, &temp};
8889 Get_External_Name_With_Suffix (gnat_entity, fp);
8891 else
8892 Get_External_Name (gnat_entity, 0);
8894 /* A variable using the Stdcall convention lives in a DLL. We adjust
8895 its name to use the jump table, the _imp__NAME contains the address
8896 for the NAME variable. */
8897 if ((kind == E_Variable || kind == E_Constant)
8898 && Has_Stdcall_Convention (gnat_entity))
8900 const int len = 6 + Name_Len;
8901 char *new_name = (char *) alloca (len + 1);
8902 strcpy (new_name, "_imp__");
8903 strcat (new_name, Name_Buffer);
8904 return get_identifier_with_length (new_name, len);
8907 return get_identifier_with_length (Name_Buffer, Name_Len);
8910 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8911 string, return a new IDENTIFIER_NODE that is the concatenation of
8912 the name followed by "___" and the specified suffix. */
8914 tree
8915 concat_name (tree gnu_name, const char *suffix)
8917 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8918 char *new_name = (char *) alloca (len + 1);
8919 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8920 strcat (new_name, "___");
8921 strcat (new_name, suffix);
8922 return get_identifier_with_length (new_name, len);
8925 #include "gt-ada-decl.h"