* gcc-interface/decl.c (annotate_value): Fix thinko in latest change.
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blob43ed0b33c3bde96e14c8e985187b2f05e079ff12
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 #define STDCALL_PREFIX "_imp__"
77 /* Stack realignment is necessary for functions with foreign conventions when
78 the ABI doesn't mandate as much as what the compiler assumes - that is, up
79 to PREFERRED_STACK_BOUNDARY.
81 Such realignment can be requested with a dedicated function type attribute
82 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
83 characterize the situations where the attribute should be set. We rely on
84 compiler configuration settings for 'main' to decide. */
86 #ifdef MAIN_STACK_BOUNDARY
87 #define FOREIGN_FORCE_REALIGN_STACK \
88 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
89 #else
90 #define FOREIGN_FORCE_REALIGN_STACK 0
91 #endif
93 struct incomplete
95 struct incomplete *next;
96 tree old_type;
97 Entity_Id full_type;
100 /* These variables are used to defer recursively expanding incomplete types
101 while we are processing an array, a record or a subprogram type. */
102 static int defer_incomplete_level = 0;
103 static struct incomplete *defer_incomplete_list;
105 /* This variable is used to delay expanding From_Limited_With types until the
106 end of the spec. */
107 static struct incomplete *defer_limited_with;
109 typedef struct subst_pair_d {
110 tree discriminant;
111 tree replacement;
112 } subst_pair;
115 typedef struct variant_desc_d {
116 /* The type of the variant. */
117 tree type;
119 /* The associated field. */
120 tree field;
122 /* The value of the qualifier. */
123 tree qual;
125 /* The type of the variant after transformation. */
126 tree new_type;
127 } variant_desc;
130 /* A hash table used to cache the result of annotate_value. */
131 static GTY ((if_marked ("tree_int_map_marked_p"),
132 param_is (struct tree_int_map))) htab_t annotate_value_cache;
134 static bool allocatable_size_p (tree, bool);
135 static void prepend_one_attribute (struct attrib **,
136 enum attr_type, tree, tree, Node_Id);
137 static void prepend_one_attribute_pragma (struct attrib **, Node_Id);
138 static void prepend_attributes (struct attrib **, Entity_Id);
139 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
140 static bool type_has_variable_size (tree);
141 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
142 static tree elaborate_expression_2 (tree, Entity_Id, tree, bool, bool,
143 unsigned int);
144 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
145 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
146 bool *);
147 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
148 static bool same_discriminant_p (Entity_Id, Entity_Id);
149 static bool array_type_has_nonaliased_component (tree, Entity_Id);
150 static bool compile_time_known_address_p (Node_Id);
151 static bool cannot_be_superflat_p (Node_Id);
152 static bool constructor_address_p (tree);
153 static int compare_field_bitpos (const PTR, const PTR);
154 static bool components_to_record (tree, Node_Id, tree, int, bool, bool, bool,
155 bool, bool, bool, bool, bool, tree, tree *);
156 static Uint annotate_value (tree);
157 static void annotate_rep (Entity_Id, tree);
158 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
159 static vec<subst_pair> build_subst_list (Entity_Id, Entity_Id, bool);
160 static vec<variant_desc> build_variant_list (tree,
161 vec<subst_pair> ,
162 vec<variant_desc> );
163 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
164 static void set_rm_size (Uint, tree, Entity_Id);
165 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
166 static void check_ok_for_atomic (tree, Entity_Id, bool);
167 static tree create_field_decl_from (tree, tree, tree, tree, tree,
168 vec<subst_pair> );
169 static tree create_rep_part (tree, tree, tree);
170 static tree get_rep_part (tree);
171 static tree create_variant_part_from (tree, vec<variant_desc> , tree,
172 tree, vec<subst_pair> );
173 static void copy_and_substitute_in_size (tree, tree, vec<subst_pair> );
175 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
176 to pass around calls performing profile compatibility checks. */
178 typedef struct {
179 Entity_Id gnat_entity; /* The Ada subprogram entity. */
180 tree ada_fntype; /* The corresponding GCC type node. */
181 tree btin_fntype; /* The GCC builtin function type node. */
182 } intrin_binding_t;
184 static bool intrin_profiles_compatible_p (intrin_binding_t *);
186 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
187 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
188 and associate the ..._DECL node with the input GNAT defining identifier.
190 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
191 initial value (in GCC tree form). This is optional for a variable. For
192 a renamed entity, GNU_EXPR gives the object being renamed.
194 DEFINITION is nonzero if this call is intended for a definition. This is
195 used for separate compilation where it is necessary to know whether an
196 external declaration or a definition must be created if the GCC equivalent
197 was not created previously. The value of 1 is normally used for a nonzero
198 DEFINITION, but a value of 2 is used in special circumstances, defined in
199 the code. */
201 tree
202 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
204 /* Contains the kind of the input GNAT node. */
205 const Entity_Kind kind = Ekind (gnat_entity);
206 /* True if this is a type. */
207 const bool is_type = IN (kind, Type_Kind);
208 /* True if debug info is requested for this entity. */
209 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
210 /* True if this entity is to be considered as imported. */
211 const bool imported_p
212 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
213 /* For a type, contains the equivalent GNAT node to be used in gigi. */
214 Entity_Id gnat_equiv_type = Empty;
215 /* Temporary used to walk the GNAT tree. */
216 Entity_Id gnat_temp;
217 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
218 This node will be associated with the GNAT node by calling at the end
219 of the `switch' statement. */
220 tree gnu_decl = NULL_TREE;
221 /* Contains the GCC type to be used for the GCC node. */
222 tree gnu_type = NULL_TREE;
223 /* Contains the GCC size tree to be used for the GCC node. */
224 tree gnu_size = NULL_TREE;
225 /* Contains the GCC name to be used for the GCC node. */
226 tree gnu_entity_name;
227 /* True if we have already saved gnu_decl as a GNAT association. */
228 bool saved = false;
229 /* True if we incremented defer_incomplete_level. */
230 bool this_deferred = false;
231 /* True if we incremented force_global. */
232 bool this_global = false;
233 /* True if we should check to see if elaborated during processing. */
234 bool maybe_present = false;
235 /* True if we made GNU_DECL and its type here. */
236 bool this_made_decl = false;
237 /* Size and alignment of the GCC node, if meaningful. */
238 unsigned int esize = 0, align = 0;
239 /* Contains the list of attributes directly attached to the entity. */
240 struct attrib *attr_list = NULL;
242 /* Since a use of an Itype is a definition, process it as such if it
243 is not in a with'ed unit. */
244 if (!definition
245 && is_type
246 && Is_Itype (gnat_entity)
247 && !present_gnu_tree (gnat_entity)
248 && In_Extended_Main_Code_Unit (gnat_entity))
250 /* Ensure that we are in a subprogram mentioned in the Scope chain of
251 this entity, our current scope is global, or we encountered a task
252 or entry (where we can't currently accurately check scoping). */
253 if (!current_function_decl
254 || DECL_ELABORATION_PROC_P (current_function_decl))
256 process_type (gnat_entity);
257 return get_gnu_tree (gnat_entity);
260 for (gnat_temp = Scope (gnat_entity);
261 Present (gnat_temp);
262 gnat_temp = Scope (gnat_temp))
264 if (Is_Type (gnat_temp))
265 gnat_temp = Underlying_Type (gnat_temp);
267 if (Ekind (gnat_temp) == E_Subprogram_Body)
268 gnat_temp
269 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
271 if (IN (Ekind (gnat_temp), Subprogram_Kind)
272 && Present (Protected_Body_Subprogram (gnat_temp)))
273 gnat_temp = Protected_Body_Subprogram (gnat_temp);
275 if (Ekind (gnat_temp) == E_Entry
276 || Ekind (gnat_temp) == E_Entry_Family
277 || Ekind (gnat_temp) == E_Task_Type
278 || (IN (Ekind (gnat_temp), Subprogram_Kind)
279 && present_gnu_tree (gnat_temp)
280 && (current_function_decl
281 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
283 process_type (gnat_entity);
284 return get_gnu_tree (gnat_entity);
288 /* This abort means the Itype has an incorrect scope, i.e. that its
289 scope does not correspond to the subprogram it is declared in. */
290 gcc_unreachable ();
293 /* If we've already processed this entity, return what we got last time.
294 If we are defining the node, we should not have already processed it.
295 In that case, we will abort below when we try to save a new GCC tree
296 for this object. We also need to handle the case of getting a dummy
297 type when a Full_View exists but be careful so as not to trigger its
298 premature elaboration. */
299 if ((!definition || (is_type && imported_p))
300 && present_gnu_tree (gnat_entity))
302 gnu_decl = get_gnu_tree (gnat_entity);
304 if (TREE_CODE (gnu_decl) == TYPE_DECL
305 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
306 && IN (kind, Incomplete_Or_Private_Kind)
307 && Present (Full_View (gnat_entity))
308 && (present_gnu_tree (Full_View (gnat_entity))
309 || No (Freeze_Node (Full_View (gnat_entity)))))
311 gnu_decl
312 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
313 save_gnu_tree (gnat_entity, NULL_TREE, false);
314 save_gnu_tree (gnat_entity, gnu_decl, false);
317 return gnu_decl;
320 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
321 must be specified unless it was specified by the programmer. Exceptions
322 are for access-to-protected-subprogram types and all access subtypes, as
323 another GNAT type is used to lay out the GCC type for them. */
324 gcc_assert (!Unknown_Esize (gnat_entity)
325 || Has_Size_Clause (gnat_entity)
326 || (!IN (kind, Numeric_Kind)
327 && !IN (kind, Enumeration_Kind)
328 && (!IN (kind, Access_Kind)
329 || kind == E_Access_Protected_Subprogram_Type
330 || kind == E_Anonymous_Access_Protected_Subprogram_Type
331 || kind == E_Access_Subtype
332 || type_annotate_only)));
334 /* The RM size must be specified for all discrete and fixed-point types. */
335 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
336 && Unknown_RM_Size (gnat_entity)));
338 /* If we get here, it means we have not yet done anything with this entity.
339 If we are not defining it, it must be a type or an entity that is defined
340 elsewhere or externally, otherwise we should have defined it already. */
341 gcc_assert (definition
342 || type_annotate_only
343 || is_type
344 || kind == E_Discriminant
345 || kind == E_Component
346 || kind == E_Label
347 || (kind == E_Constant && Present (Full_View (gnat_entity)))
348 || Is_Public (gnat_entity));
350 /* Get the name of the entity and set up the line number and filename of
351 the original definition for use in any decl we make. */
352 gnu_entity_name = get_entity_name (gnat_entity);
353 Sloc_to_locus (Sloc (gnat_entity), &input_location);
355 /* For cases when we are not defining (i.e., we are referencing from
356 another compilation unit) public entities, show we are at global level
357 for the purpose of computing scopes. Don't do this for components or
358 discriminants since the relevant test is whether or not the record is
359 being defined. */
360 if (!definition
361 && kind != E_Component
362 && kind != E_Discriminant
363 && Is_Public (gnat_entity)
364 && !Is_Statically_Allocated (gnat_entity))
365 force_global++, this_global = true;
367 /* Handle any attributes directly attached to the entity. */
368 if (Has_Gigi_Rep_Item (gnat_entity))
369 prepend_attributes (&attr_list, gnat_entity);
371 /* Do some common processing for types. */
372 if (is_type)
374 /* Compute the equivalent type to be used in gigi. */
375 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
377 /* Machine_Attributes on types are expected to be propagated to
378 subtypes. The corresponding Gigi_Rep_Items are only attached
379 to the first subtype though, so we handle the propagation here. */
380 if (Base_Type (gnat_entity) != gnat_entity
381 && !Is_First_Subtype (gnat_entity)
382 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
383 prepend_attributes (&attr_list,
384 First_Subtype (Base_Type (gnat_entity)));
386 /* Compute a default value for the size of an elementary type. */
387 if (Known_Esize (gnat_entity) && Is_Elementary_Type (gnat_entity))
389 unsigned int max_esize;
391 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity)));
392 esize = UI_To_Int (Esize (gnat_entity));
394 if (IN (kind, Float_Kind))
395 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
396 else if (IN (kind, Access_Kind))
397 max_esize = POINTER_SIZE * 2;
398 else
399 max_esize = LONG_LONG_TYPE_SIZE;
401 if (esize > max_esize)
402 esize = max_esize;
406 switch (kind)
408 case E_Constant:
409 /* If this is a use of a deferred constant without address clause,
410 get its full definition. */
411 if (!definition
412 && No (Address_Clause (gnat_entity))
413 && Present (Full_View (gnat_entity)))
415 gnu_decl
416 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
417 saved = true;
418 break;
421 /* If we have an external constant that we are not defining, get the
422 expression that is was defined to represent. We may throw it away
423 later if it is not a constant. But do not retrieve the expression
424 if it is an allocator because the designated type might be dummy
425 at this point. */
426 if (!definition
427 && !No_Initialization (Declaration_Node (gnat_entity))
428 && Present (Expression (Declaration_Node (gnat_entity)))
429 && Nkind (Expression (Declaration_Node (gnat_entity)))
430 != N_Allocator)
432 bool went_into_elab_proc = false;
433 int save_force_global = force_global;
435 /* The expression may contain N_Expression_With_Actions nodes and
436 thus object declarations from other units. In this case, even
437 though the expression will eventually be discarded since not a
438 constant, the declarations would be stuck either in the global
439 varpool or in the current scope. Therefore we force the local
440 context and create a fake scope that we'll zap at the end. */
441 if (!current_function_decl)
443 current_function_decl = get_elaboration_procedure ();
444 went_into_elab_proc = true;
446 force_global = 0;
447 gnat_pushlevel ();
449 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
451 gnat_zaplevel ();
452 force_global = save_force_global;
453 if (went_into_elab_proc)
454 current_function_decl = NULL_TREE;
457 /* Ignore deferred constant definitions without address clause since
458 they are processed fully in the front-end. If No_Initialization
459 is set, this is not a deferred constant but a constant whose value
460 is built manually. And constants that are renamings are handled
461 like variables. */
462 if (definition
463 && !gnu_expr
464 && No (Address_Clause (gnat_entity))
465 && !No_Initialization (Declaration_Node (gnat_entity))
466 && No (Renamed_Object (gnat_entity)))
468 gnu_decl = error_mark_node;
469 saved = true;
470 break;
473 /* Ignore constant definitions already marked with the error node. See
474 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
475 if (definition
476 && gnu_expr
477 && present_gnu_tree (gnat_entity)
478 && get_gnu_tree (gnat_entity) == error_mark_node)
480 maybe_present = true;
481 break;
484 goto object;
486 case E_Exception:
487 /* We used to special case VMS exceptions here to directly map them to
488 their associated condition code. Since this code had to be masked
489 dynamically to strip off the severity bits, this caused trouble in
490 the GCC/ZCX case because the "type" pointers we store in the tables
491 have to be static. We now don't special case here anymore, and let
492 the regular processing take place, which leaves us with a regular
493 exception data object for VMS exceptions too. The condition code
494 mapping is taken care of by the front end and the bitmasking by the
495 run-time library. */
496 goto object;
498 case E_Component:
499 case E_Discriminant:
501 /* The GNAT record where the component was defined. */
502 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
504 /* If the entity is an inherited component (in the case of extended
505 tagged record types), just return the original entity, which must
506 be a FIELD_DECL. Likewise for discriminants. If the entity is a
507 non-girder discriminant (in the case of derived untagged record
508 types), return the stored discriminant it renames. */
509 if (Present (Original_Record_Component (gnat_entity))
510 && Original_Record_Component (gnat_entity) != gnat_entity)
512 gnu_decl
513 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
514 gnu_expr, definition);
515 saved = true;
516 break;
519 /* If this is a discriminant of an extended tagged type used to rename
520 a discriminant of the parent type, return the latter. */
521 else if (Present (Corresponding_Discriminant (gnat_entity)))
523 /* If the derived type is untagged, then this is a non-girder
524 discriminant and its Original_Record_Component must point to
525 the stored discriminant it renames (i.e. we should have taken
526 the previous branch). */
527 gcc_assert (Is_Tagged_Type (gnat_record));
529 gnu_decl
530 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
531 gnu_expr, definition);
532 saved = true;
533 break;
536 /* Otherwise, if we are not defining this and we have no GCC type
537 for the containing record, make one for it. Then we should
538 have made our own equivalent. */
539 else if (!definition && !present_gnu_tree (gnat_record))
541 /* ??? If this is in a record whose scope is a protected
542 type and we have an Original_Record_Component, use it.
543 This is a workaround for major problems in protected type
544 handling. */
545 Entity_Id Scop = Scope (Scope (gnat_entity));
546 if ((Is_Protected_Type (Scop)
547 || (Is_Private_Type (Scop)
548 && Present (Full_View (Scop))
549 && Is_Protected_Type (Full_View (Scop))))
550 && Present (Original_Record_Component (gnat_entity)))
552 gnu_decl
553 = gnat_to_gnu_entity (Original_Record_Component
554 (gnat_entity),
555 gnu_expr, 0);
556 saved = true;
557 break;
560 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
561 gnu_decl = get_gnu_tree (gnat_entity);
562 saved = true;
563 break;
566 else
567 /* Here we have no GCC type and this is a reference rather than a
568 definition. This should never happen. Most likely the cause is
569 reference before declaration in the GNAT tree for gnat_entity. */
570 gcc_unreachable ();
573 case E_Loop_Parameter:
574 case E_Out_Parameter:
575 case E_Variable:
577 /* Simple variables, loop variables, Out parameters and exceptions. */
578 object:
580 /* Always create a variable for volatile objects and variables seen
581 constant but with a Linker_Section pragma. */
582 bool const_flag
583 = ((kind == E_Constant || kind == E_Variable)
584 && Is_True_Constant (gnat_entity)
585 && !(kind == E_Variable
586 && Present (Linker_Section_Pragma (gnat_entity)))
587 && !Treat_As_Volatile (gnat_entity)
588 && (((Nkind (Declaration_Node (gnat_entity))
589 == N_Object_Declaration)
590 && Present (Expression (Declaration_Node (gnat_entity))))
591 || Present (Renamed_Object (gnat_entity))
592 || imported_p));
593 bool inner_const_flag = const_flag;
594 bool static_p = Is_Statically_Allocated (gnat_entity);
595 bool mutable_p = false;
596 bool used_by_ref = false;
597 tree gnu_ext_name = NULL_TREE;
598 tree renamed_obj = NULL_TREE;
599 tree gnu_object_size;
601 if (Present (Renamed_Object (gnat_entity)) && !definition)
603 if (kind == E_Exception)
604 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
605 NULL_TREE, 0);
606 else
607 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
610 /* Get the type after elaborating the renamed object. */
611 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
613 /* If this is a standard exception definition, then use the standard
614 exception type. This is necessary to make sure that imported and
615 exported views of exceptions are properly merged in LTO mode. */
616 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
617 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
618 gnu_type = except_type_node;
620 /* For a debug renaming declaration, build a debug-only entity. */
621 if (Present (Debug_Renaming_Link (gnat_entity)))
623 /* Force a non-null value to make sure the symbol is retained. */
624 tree value = build1 (INDIRECT_REF, gnu_type,
625 build1 (NOP_EXPR,
626 build_pointer_type (gnu_type),
627 integer_minus_one_node));
628 gnu_decl = build_decl (input_location,
629 VAR_DECL, gnu_entity_name, gnu_type);
630 SET_DECL_VALUE_EXPR (gnu_decl, value);
631 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
632 gnat_pushdecl (gnu_decl, gnat_entity);
633 break;
636 /* If this is a loop variable, its type should be the base type.
637 This is because the code for processing a loop determines whether
638 a normal loop end test can be done by comparing the bounds of the
639 loop against those of the base type, which is presumed to be the
640 size used for computation. But this is not correct when the size
641 of the subtype is smaller than the type. */
642 if (kind == E_Loop_Parameter)
643 gnu_type = get_base_type (gnu_type);
645 /* Reject non-renamed objects whose type is an unconstrained array or
646 any object whose type is a dummy type or void. */
647 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
648 && No (Renamed_Object (gnat_entity)))
649 || TYPE_IS_DUMMY_P (gnu_type)
650 || TREE_CODE (gnu_type) == VOID_TYPE)
652 gcc_assert (type_annotate_only);
653 if (this_global)
654 force_global--;
655 return error_mark_node;
658 /* If an alignment is specified, use it if valid. Note that exceptions
659 are objects but don't have an alignment. We must do this before we
660 validate the size, since the alignment can affect the size. */
661 if (kind != E_Exception && Known_Alignment (gnat_entity))
663 gcc_assert (Present (Alignment (gnat_entity)));
665 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
666 TYPE_ALIGN (gnu_type));
668 /* No point in changing the type if there is an address clause
669 as the final type of the object will be a reference type. */
670 if (Present (Address_Clause (gnat_entity)))
671 align = 0;
672 else
674 tree orig_type = gnu_type;
676 gnu_type
677 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
678 false, false, definition, true);
680 /* If a padding record was made, declare it now since it will
681 never be declared otherwise. This is necessary to ensure
682 that its subtrees are properly marked. */
683 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
684 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
685 debug_info_p, gnat_entity);
689 /* If we are defining the object, see if it has a Size and validate it
690 if so. If we are not defining the object and a Size clause applies,
691 simply retrieve the value. We don't want to ignore the clause and
692 it is expected to have been validated already. Then get the new
693 type, if any. */
694 if (definition)
695 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
696 gnat_entity, VAR_DECL, false,
697 Has_Size_Clause (gnat_entity));
698 else if (Has_Size_Clause (gnat_entity))
699 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
701 if (gnu_size)
703 gnu_type
704 = make_type_from_size (gnu_type, gnu_size,
705 Has_Biased_Representation (gnat_entity));
707 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
708 gnu_size = NULL_TREE;
711 /* If this object has self-referential size, it must be a record with
712 a default discriminant. We are supposed to allocate an object of
713 the maximum size in this case, unless it is a constant with an
714 initializing expression, in which case we can get the size from
715 that. Note that the resulting size may still be a variable, so
716 this may end up with an indirect allocation. */
717 if (No (Renamed_Object (gnat_entity))
718 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
720 if (gnu_expr && kind == E_Constant)
722 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
723 if (CONTAINS_PLACEHOLDER_P (size))
725 /* If the initializing expression is itself a constant,
726 despite having a nominal type with self-referential
727 size, we can get the size directly from it. */
728 if (TREE_CODE (gnu_expr) == COMPONENT_REF
729 && TYPE_IS_PADDING_P
730 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
731 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
732 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
733 || DECL_READONLY_ONCE_ELAB
734 (TREE_OPERAND (gnu_expr, 0))))
735 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
736 else
737 gnu_size
738 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
740 else
741 gnu_size = size;
743 /* We may have no GNU_EXPR because No_Initialization is
744 set even though there's an Expression. */
745 else if (kind == E_Constant
746 && (Nkind (Declaration_Node (gnat_entity))
747 == N_Object_Declaration)
748 && Present (Expression (Declaration_Node (gnat_entity))))
749 gnu_size
750 = TYPE_SIZE (gnat_to_gnu_type
751 (Etype
752 (Expression (Declaration_Node (gnat_entity)))));
753 else
755 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
756 mutable_p = true;
759 /* If we are at global level and the size isn't constant, call
760 elaborate_expression_1 to make a variable for it rather than
761 calculating it each time. */
762 if (global_bindings_p () && !TREE_CONSTANT (gnu_size))
763 gnu_size = elaborate_expression_1 (gnu_size, gnat_entity,
764 get_identifier ("SIZE"),
765 definition, false);
768 /* If the size is zero byte, make it one byte since some linkers have
769 troubles with zero-sized objects. If the object will have a
770 template, that will make it nonzero so don't bother. Also avoid
771 doing that for an object renaming or an object with an address
772 clause, as we would lose useful information on the view size
773 (e.g. for null array slices) and we are not allocating the object
774 here anyway. */
775 if (((gnu_size
776 && integer_zerop (gnu_size)
777 && !TREE_OVERFLOW (gnu_size))
778 || (TYPE_SIZE (gnu_type)
779 && integer_zerop (TYPE_SIZE (gnu_type))
780 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
781 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
782 && No (Renamed_Object (gnat_entity))
783 && No (Address_Clause (gnat_entity)))
784 gnu_size = bitsize_unit_node;
786 /* If this is an object with no specified size and alignment, and
787 if either it is atomic or we are not optimizing alignment for
788 space and it is composite and not an exception, an Out parameter
789 or a reference to another object, and the size of its type is a
790 constant, set the alignment to the smallest one which is not
791 smaller than the size, with an appropriate cap. */
792 if (!gnu_size && align == 0
793 && (Is_Atomic (gnat_entity)
794 || (!Optimize_Alignment_Space (gnat_entity)
795 && kind != E_Exception
796 && kind != E_Out_Parameter
797 && Is_Composite_Type (Etype (gnat_entity))
798 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
799 && !Is_Exported (gnat_entity)
800 && !imported_p
801 && No (Renamed_Object (gnat_entity))
802 && No (Address_Clause (gnat_entity))))
803 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
805 unsigned int size_cap, align_cap;
807 /* No point in promoting the alignment if this doesn't prevent
808 BLKmode access to the object, in particular block copy, as
809 this will for example disable the NRV optimization for it.
810 No point in jumping through all the hoops needed in order
811 to support BIGGEST_ALIGNMENT if we don't really have to.
812 So we cap to the smallest alignment that corresponds to
813 a known efficient memory access pattern of the target. */
814 if (Is_Atomic (gnat_entity))
816 size_cap = UINT_MAX;
817 align_cap = BIGGEST_ALIGNMENT;
819 else
821 size_cap = MAX_FIXED_MODE_SIZE;
822 align_cap = get_mode_alignment (ptr_mode);
825 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
826 || compare_tree_int (TYPE_SIZE (gnu_type), size_cap) > 0)
827 align = 0;
828 else if (compare_tree_int (TYPE_SIZE (gnu_type), align_cap) > 0)
829 align = align_cap;
830 else
831 align = ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type)));
833 /* But make sure not to under-align the object. */
834 if (align <= TYPE_ALIGN (gnu_type))
835 align = 0;
837 /* And honor the minimum valid atomic alignment, if any. */
838 #ifdef MINIMUM_ATOMIC_ALIGNMENT
839 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
840 align = MINIMUM_ATOMIC_ALIGNMENT;
841 #endif
844 /* If the object is set to have atomic components, find the component
845 type and validate it.
847 ??? Note that we ignore Has_Volatile_Components on objects; it's
848 not at all clear what to do in that case. */
849 if (Has_Atomic_Components (gnat_entity))
851 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
852 ? TREE_TYPE (gnu_type) : gnu_type);
854 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
855 && TYPE_MULTI_ARRAY_P (gnu_inner))
856 gnu_inner = TREE_TYPE (gnu_inner);
858 check_ok_for_atomic (gnu_inner, gnat_entity, true);
861 /* Now check if the type of the object allows atomic access. Note
862 that we must test the type, even if this object has size and
863 alignment to allow such access, because we will be going inside
864 the padded record to assign to the object. We could fix this by
865 always copying via an intermediate value, but it's not clear it's
866 worth the effort. */
867 if (Is_Atomic (gnat_entity))
868 check_ok_for_atomic (gnu_type, gnat_entity, false);
870 /* If this is an aliased object with an unconstrained nominal subtype,
871 make a type that includes the template. */
872 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
873 && (Is_Array_Type (Etype (gnat_entity))
874 || (Is_Private_Type (Etype (gnat_entity))
875 && Is_Array_Type (Full_View (Etype (gnat_entity)))))
876 && !type_annotate_only)
878 tree gnu_array
879 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
880 gnu_type
881 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array),
882 gnu_type,
883 concat_name (gnu_entity_name,
884 "UNC"),
885 debug_info_p);
888 /* ??? If this is an object of CW type initialized to a value, try to
889 ensure that the object is sufficient aligned for this value, but
890 without pessimizing the allocation. This is a kludge necessary
891 because we don't support dynamic alignment. */
892 if (align == 0
893 && Ekind (Etype (gnat_entity)) == E_Class_Wide_Subtype
894 && No (Renamed_Object (gnat_entity))
895 && No (Address_Clause (gnat_entity)))
896 align = get_target_system_allocator_alignment () * BITS_PER_UNIT;
898 #ifdef MINIMUM_ATOMIC_ALIGNMENT
899 /* If the size is a constant and no alignment is specified, force
900 the alignment to be the minimum valid atomic alignment. The
901 restriction on constant size avoids problems with variable-size
902 temporaries; if the size is variable, there's no issue with
903 atomic access. Also don't do this for a constant, since it isn't
904 necessary and can interfere with constant replacement. Finally,
905 do not do it for Out parameters since that creates an
906 size inconsistency with In parameters. */
907 if (align == 0
908 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
909 && !FLOAT_TYPE_P (gnu_type)
910 && !const_flag && No (Renamed_Object (gnat_entity))
911 && !imported_p && No (Address_Clause (gnat_entity))
912 && kind != E_Out_Parameter
913 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
914 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
915 align = MINIMUM_ATOMIC_ALIGNMENT;
916 #endif
918 /* Make a new type with the desired size and alignment, if needed.
919 But do not take into account alignment promotions to compute the
920 size of the object. */
921 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
922 if (gnu_size || align > 0)
924 tree orig_type = gnu_type;
926 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
927 false, false, definition, true);
929 /* If a padding record was made, declare it now since it will
930 never be declared otherwise. This is necessary to ensure
931 that its subtrees are properly marked. */
932 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
933 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true,
934 debug_info_p, gnat_entity);
937 /* If this is a renaming, avoid as much as possible to create a new
938 object. However, in several cases, creating it is required.
939 This processing needs to be applied to the raw expression so
940 as to make it more likely to rename the underlying object. */
941 if (Present (Renamed_Object (gnat_entity)))
943 bool create_normal_object = false;
945 /* If the renamed object had padding, strip off the reference
946 to the inner object and reset our type. */
947 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
948 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
949 /* Strip useless conversions around the object. */
950 || gnat_useless_type_conversion (gnu_expr))
952 gnu_expr = TREE_OPERAND (gnu_expr, 0);
953 gnu_type = TREE_TYPE (gnu_expr);
956 /* Or else, if the renamed object has an unconstrained type with
957 default discriminant, use the padded type. */
958 else if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_expr))
959 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_expr)))
960 == gnu_type
961 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
962 gnu_type = TREE_TYPE (gnu_expr);
964 /* Case 1: If this is a constant renaming stemming from a function
965 call, treat it as a normal object whose initial value is what is
966 being renamed. RM 3.3 says that the result of evaluating a
967 function call is a constant object. Treat constant literals
968 the same way. As a consequence, it can be the inner object of
969 a constant renaming. In this case, the renaming must be fully
970 instantiated, i.e. it cannot be a mere reference to (part of) an
971 existing object. */
972 if (const_flag)
974 tree inner_object = gnu_expr;
975 while (handled_component_p (inner_object))
976 inner_object = TREE_OPERAND (inner_object, 0);
977 if (TREE_CODE (inner_object) == CALL_EXPR
978 || CONSTANT_CLASS_P (inner_object))
979 create_normal_object = true;
982 /* Otherwise, see if we can proceed with a stabilized version of
983 the renamed entity or if we need to make a new object. */
984 if (!create_normal_object)
986 tree maybe_stable_expr = NULL_TREE;
987 bool stable = false;
989 /* Case 2: If the renaming entity need not be materialized and
990 the renamed expression is something we can stabilize, use
991 that for the renaming. At the global level, we can only do
992 this if we know no SAVE_EXPRs need be made, because the
993 expression we return might be used in arbitrary conditional
994 branches so we must force the evaluation of the SAVE_EXPRs
995 immediately and this requires a proper function context.
996 Note that an external constant is at the global level. */
997 if (!Materialize_Entity (gnat_entity)
998 && (!((!definition && kind == E_Constant)
999 || global_bindings_p ())
1000 || (staticp (gnu_expr)
1001 && !TREE_SIDE_EFFECTS (gnu_expr))))
1003 maybe_stable_expr
1004 = gnat_stabilize_reference (gnu_expr, true, &stable);
1006 if (stable)
1008 /* ??? No DECL_EXPR is created so we need to mark
1009 the expression manually lest it is shared. */
1010 if ((!definition && kind == E_Constant)
1011 || global_bindings_p ())
1012 MARK_VISITED (maybe_stable_expr);
1013 gnu_decl = maybe_stable_expr;
1014 save_gnu_tree (gnat_entity, gnu_decl, true);
1015 saved = true;
1016 annotate_object (gnat_entity, gnu_type, NULL_TREE,
1017 false);
1018 /* This assertion will fail if the renamed object
1019 isn't aligned enough as to make it possible to
1020 honor the alignment set on the renaming. */
1021 if (align)
1023 unsigned int renamed_align
1024 = DECL_P (gnu_decl)
1025 ? DECL_ALIGN (gnu_decl)
1026 : TYPE_ALIGN (TREE_TYPE (gnu_decl));
1027 gcc_assert (renamed_align >= align);
1029 break;
1032 /* The stabilization failed. Keep maybe_stable_expr
1033 untouched here to let the pointer case below know
1034 about that failure. */
1037 /* Case 3: Make this into a constant pointer to the object we
1038 are to rename and attach the object to the pointer if it is
1039 something we can stabilize.
1041 From the proper scope, attached objects will be referenced
1042 directly instead of indirectly via the pointer to avoid
1043 subtle aliasing problems with non-addressable entities.
1044 They have to be stable because we must not evaluate the
1045 variables in the expression every time the renaming is used.
1046 The pointer is called a "renaming" pointer in this case.
1048 In the rare cases where we cannot stabilize the renamed
1049 object, we just make a "bare" pointer and the renamed
1050 object will always be accessed indirectly through it.
1052 Note that we need to preserve the volatility of the renamed
1053 object through the indirection. */
1054 if (TREE_THIS_VOLATILE (gnu_expr) && !TYPE_VOLATILE (gnu_type))
1055 gnu_type = build_qualified_type (gnu_type,
1056 (TYPE_QUALS (gnu_type)
1057 | TYPE_QUAL_VOLATILE));
1058 gnu_type = build_reference_type (gnu_type);
1059 inner_const_flag = TREE_READONLY (gnu_expr);
1060 const_flag = true;
1062 /* If the previous attempt at stabilizing failed, there is
1063 no point in trying again and we reuse the result without
1064 attaching it to the pointer. In this case it will only
1065 be used as the initializing expression of the pointer and
1066 thus needs no special treatment with regard to multiple
1067 evaluations.
1069 Otherwise, try to stabilize and attach the expression to
1070 the pointer if the stabilization succeeds.
1072 Note that this might introduce SAVE_EXPRs and we don't
1073 check whether we are at the global level or not. This
1074 is fine since we are building a pointer initializer and
1075 neither the pointer nor the initializing expression can
1076 be accessed before the pointer elaboration has taken
1077 place in a correct program.
1079 These SAVE_EXPRs will be evaluated at the right place
1080 by either the evaluation of the initializer for the
1081 non-global case or the elaboration code for the global
1082 case, and will be attached to the elaboration procedure
1083 in the latter case. */
1084 if (!maybe_stable_expr)
1086 maybe_stable_expr
1087 = gnat_stabilize_reference (gnu_expr, true, &stable);
1089 if (stable)
1090 renamed_obj = maybe_stable_expr;
1093 if (type_annotate_only
1094 && TREE_CODE (maybe_stable_expr) == ERROR_MARK)
1095 gnu_expr = NULL_TREE;
1096 else
1097 gnu_expr
1098 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
1100 gnu_size = NULL_TREE;
1101 used_by_ref = true;
1105 /* Make a volatile version of this object's type if we are to make
1106 the object volatile. We also interpret 13.3(19) conservatively
1107 and disallow any optimizations for such a non-constant object. */
1108 if ((Treat_As_Volatile (gnat_entity)
1109 || (!const_flag
1110 && gnu_type != except_type_node
1111 && (Is_Exported (gnat_entity)
1112 || imported_p
1113 || Present (Address_Clause (gnat_entity)))))
1114 && !TYPE_VOLATILE (gnu_type))
1115 gnu_type = build_qualified_type (gnu_type,
1116 (TYPE_QUALS (gnu_type)
1117 | TYPE_QUAL_VOLATILE));
1119 /* If we are defining an aliased object whose nominal subtype is
1120 unconstrained, the object is a record that contains both the
1121 template and the object. If there is an initializer, it will
1122 have already been converted to the right type, but we need to
1123 create the template if there is no initializer. */
1124 if (definition
1125 && !gnu_expr
1126 && TREE_CODE (gnu_type) == RECORD_TYPE
1127 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1128 /* Beware that padding might have been introduced above. */
1129 || (TYPE_PADDING_P (gnu_type)
1130 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1131 == RECORD_TYPE
1132 && TYPE_CONTAINS_TEMPLATE_P
1133 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1135 tree template_field
1136 = TYPE_PADDING_P (gnu_type)
1137 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1138 : TYPE_FIELDS (gnu_type);
1139 vec<constructor_elt, va_gc> *v;
1140 vec_alloc (v, 1);
1141 tree t = build_template (TREE_TYPE (template_field),
1142 TREE_TYPE (DECL_CHAIN (template_field)),
1143 NULL_TREE);
1144 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1145 gnu_expr = gnat_build_constructor (gnu_type, v);
1148 /* Convert the expression to the type of the object except in the
1149 case where the object's type is unconstrained or the object's type
1150 is a padded record whose field is of self-referential size. In
1151 the former case, converting will generate unnecessary evaluations
1152 of the CONSTRUCTOR to compute the size and in the latter case, we
1153 want to only copy the actual data. Also don't convert to a record
1154 type with a variant part from a record type without one, to keep
1155 the object simpler. */
1156 if (gnu_expr
1157 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1158 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1159 && !(TYPE_IS_PADDING_P (gnu_type)
1160 && CONTAINS_PLACEHOLDER_P
1161 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1162 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1163 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1164 && get_variant_part (gnu_type) != NULL_TREE
1165 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1166 gnu_expr = convert (gnu_type, gnu_expr);
1168 /* If this is a pointer that doesn't have an initializing expression,
1169 initialize it to NULL, unless the object is imported. */
1170 if (definition
1171 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1172 && !gnu_expr
1173 && !Is_Imported (gnat_entity))
1174 gnu_expr = integer_zero_node;
1176 /* If we are defining the object and it has an Address clause, we must
1177 either get the address expression from the saved GCC tree for the
1178 object if it has a Freeze node, or elaborate the address expression
1179 here since the front-end has guaranteed that the elaboration has no
1180 effects in this case. */
1181 if (definition && Present (Address_Clause (gnat_entity)))
1183 Node_Id gnat_expr = Expression (Address_Clause (gnat_entity));
1184 tree gnu_address
1185 = present_gnu_tree (gnat_entity)
1186 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1188 save_gnu_tree (gnat_entity, NULL_TREE, false);
1190 /* Ignore the size. It's either meaningless or was handled
1191 above. */
1192 gnu_size = NULL_TREE;
1193 /* Convert the type of the object to a reference type that can
1194 alias everything as per 13.3(19). */
1195 gnu_type
1196 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1197 gnu_address = convert (gnu_type, gnu_address);
1198 used_by_ref = true;
1199 const_flag
1200 = !Is_Public (gnat_entity)
1201 || compile_time_known_address_p (gnat_expr);
1203 /* If this is a deferred constant, the initializer is attached to
1204 the full view. */
1205 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1206 gnu_expr
1207 = gnat_to_gnu
1208 (Expression (Declaration_Node (Full_View (gnat_entity))));
1210 /* If we don't have an initializing expression for the underlying
1211 variable, the initializing expression for the pointer is the
1212 specified address. Otherwise, we have to make a COMPOUND_EXPR
1213 to assign both the address and the initial value. */
1214 if (!gnu_expr)
1215 gnu_expr = gnu_address;
1216 else
1217 gnu_expr
1218 = build2 (COMPOUND_EXPR, gnu_type,
1219 build_binary_op
1220 (MODIFY_EXPR, NULL_TREE,
1221 build_unary_op (INDIRECT_REF, NULL_TREE,
1222 gnu_address),
1223 gnu_expr),
1224 gnu_address);
1227 /* If it has an address clause and we are not defining it, mark it
1228 as an indirect object. Likewise for Stdcall objects that are
1229 imported. */
1230 if ((!definition && Present (Address_Clause (gnat_entity)))
1231 || (Is_Imported (gnat_entity)
1232 && Has_Stdcall_Convention (gnat_entity)))
1234 /* Convert the type of the object to a reference type that can
1235 alias everything as per 13.3(19). */
1236 gnu_type
1237 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1238 gnu_size = NULL_TREE;
1240 /* No point in taking the address of an initializing expression
1241 that isn't going to be used. */
1242 gnu_expr = NULL_TREE;
1244 /* If it has an address clause whose value is known at compile
1245 time, make the object a CONST_DECL. This will avoid a
1246 useless dereference. */
1247 if (Present (Address_Clause (gnat_entity)))
1249 Node_Id gnat_address
1250 = Expression (Address_Clause (gnat_entity));
1252 if (compile_time_known_address_p (gnat_address))
1254 gnu_expr = gnat_to_gnu (gnat_address);
1255 const_flag = true;
1259 used_by_ref = true;
1262 /* If we are at top level and this object is of variable size,
1263 make the actual type a hidden pointer to the real type and
1264 make the initializer be a memory allocation and initialization.
1265 Likewise for objects we aren't defining (presumed to be
1266 external references from other packages), but there we do
1267 not set up an initialization.
1269 If the object's size overflows, make an allocator too, so that
1270 Storage_Error gets raised. Note that we will never free
1271 such memory, so we presume it never will get allocated. */
1272 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1273 global_bindings_p ()
1274 || !definition
1275 || static_p)
1276 || (gnu_size
1277 && !allocatable_size_p (convert (sizetype,
1278 size_binop
1279 (CEIL_DIV_EXPR, gnu_size,
1280 bitsize_unit_node)),
1281 global_bindings_p ()
1282 || !definition
1283 || static_p)))
1285 gnu_type = build_reference_type (gnu_type);
1286 gnu_size = NULL_TREE;
1287 used_by_ref = true;
1289 /* In case this was a aliased object whose nominal subtype is
1290 unconstrained, the pointer above will be a thin pointer and
1291 build_allocator will automatically make the template.
1293 If we have a template initializer only (that we made above),
1294 pretend there is none and rely on what build_allocator creates
1295 again anyway. Otherwise (if we have a full initializer), get
1296 the data part and feed that to build_allocator.
1298 If we are elaborating a mutable object, tell build_allocator to
1299 ignore a possibly simpler size from the initializer, if any, as
1300 we must allocate the maximum possible size in this case. */
1301 if (definition && !imported_p)
1303 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1305 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1306 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1308 gnu_alloc_type
1309 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1311 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1312 && 1 == vec_safe_length (CONSTRUCTOR_ELTS (gnu_expr)))
1313 gnu_expr = 0;
1314 else
1315 gnu_expr
1316 = build_component_ref
1317 (gnu_expr, NULL_TREE,
1318 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1319 false);
1322 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1323 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type)))
1324 post_error ("?`Storage_Error` will be raised at run time!",
1325 gnat_entity);
1327 gnu_expr
1328 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1329 Empty, Empty, gnat_entity, mutable_p);
1330 const_flag = true;
1332 else
1334 gnu_expr = NULL_TREE;
1335 const_flag = false;
1339 /* If this object would go into the stack and has an alignment larger
1340 than the largest stack alignment the back-end can honor, resort to
1341 a variable of "aligning type". */
1342 if (!global_bindings_p () && !static_p && definition
1343 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1345 /* Create the new variable. No need for extra room before the
1346 aligned field as this is in automatic storage. */
1347 tree gnu_new_type
1348 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1349 TYPE_SIZE_UNIT (gnu_type),
1350 BIGGEST_ALIGNMENT, 0, gnat_entity);
1351 tree gnu_new_var
1352 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1353 NULL_TREE, gnu_new_type, NULL_TREE, false,
1354 false, false, false, NULL, gnat_entity);
1356 /* Initialize the aligned field if we have an initializer. */
1357 if (gnu_expr)
1358 add_stmt_with_node
1359 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1360 build_component_ref
1361 (gnu_new_var, NULL_TREE,
1362 TYPE_FIELDS (gnu_new_type), false),
1363 gnu_expr),
1364 gnat_entity);
1366 /* And setup this entity as a reference to the aligned field. */
1367 gnu_type = build_reference_type (gnu_type);
1368 gnu_expr
1369 = build_unary_op
1370 (ADDR_EXPR, gnu_type,
1371 build_component_ref (gnu_new_var, NULL_TREE,
1372 TYPE_FIELDS (gnu_new_type), false));
1374 gnu_size = NULL_TREE;
1375 used_by_ref = true;
1376 const_flag = true;
1379 /* If this is an aliased object with an unconstrained nominal subtype,
1380 we make its type a thin reference, i.e. the reference counterpart
1381 of a thin pointer, so that it points to the array part. This is
1382 aimed at making it easier for the debugger to decode the object.
1383 Note that we have to do that this late because of the couple of
1384 allocation adjustments that might be made just above. */
1385 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
1386 && (Is_Array_Type (Etype (gnat_entity))
1387 || (Is_Private_Type (Etype (gnat_entity))
1388 && Is_Array_Type (Full_View (Etype (gnat_entity)))))
1389 && !type_annotate_only)
1391 tree gnu_array
1392 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity)));
1394 /* In case the object with the template has already been allocated
1395 just above, we have nothing to do here. */
1396 if (!TYPE_IS_THIN_POINTER_P (gnu_type))
1398 tree gnu_unc_var
1399 = create_var_decl (concat_name (gnu_entity_name, "UNC"),
1400 NULL_TREE, gnu_type, gnu_expr,
1401 const_flag, Is_Public (gnat_entity),
1402 imported_p || !definition, static_p,
1403 NULL, gnat_entity);
1404 gnu_expr
1405 = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_unc_var);
1406 TREE_CONSTANT (gnu_expr) = 1;
1408 gnu_size = NULL_TREE;
1409 used_by_ref = true;
1410 const_flag = true;
1413 gnu_type
1414 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array));
1417 if (const_flag)
1418 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1419 | TYPE_QUAL_CONST));
1421 /* Convert the expression to the type of the object except in the
1422 case where the object's type is unconstrained or the object's type
1423 is a padded record whose field is of self-referential size. In
1424 the former case, converting will generate unnecessary evaluations
1425 of the CONSTRUCTOR to compute the size and in the latter case, we
1426 want to only copy the actual data. Also don't convert to a record
1427 type with a variant part from a record type without one, to keep
1428 the object simpler. */
1429 if (gnu_expr
1430 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1431 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1432 && !(TYPE_IS_PADDING_P (gnu_type)
1433 && CONTAINS_PLACEHOLDER_P
1434 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
1435 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1436 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1437 && get_variant_part (gnu_type) != NULL_TREE
1438 && get_variant_part (TREE_TYPE (gnu_expr)) == NULL_TREE))
1439 gnu_expr = convert (gnu_type, gnu_expr);
1441 /* If this name is external or there was a name specified, use it,
1442 unless this is a VMS exception object since this would conflict
1443 with the symbol we need to export in addition. Don't use the
1444 Interface_Name if there is an address clause (see CD30005). */
1445 if (!Is_VMS_Exception (gnat_entity)
1446 && ((Present (Interface_Name (gnat_entity))
1447 && No (Address_Clause (gnat_entity)))
1448 || (Is_Public (gnat_entity)
1449 && (!Is_Imported (gnat_entity)
1450 || Is_Exported (gnat_entity)))))
1451 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1453 /* If this is an aggregate constant initialized to a constant, force it
1454 to be statically allocated. This saves an initialization copy. */
1455 if (!static_p
1456 && const_flag
1457 && gnu_expr && TREE_CONSTANT (gnu_expr)
1458 && AGGREGATE_TYPE_P (gnu_type)
1459 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type))
1460 && !(TYPE_IS_PADDING_P (gnu_type)
1461 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1462 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1463 static_p = true;
1465 /* Deal with a pragma Linker_Section on a constant or variable. */
1466 if ((kind == E_Constant || kind == E_Variable)
1467 && Present (Linker_Section_Pragma (gnat_entity)))
1468 prepend_one_attribute_pragma (&attr_list,
1469 Linker_Section_Pragma (gnat_entity));
1471 /* Now create the variable or the constant and set various flags. */
1472 gnu_decl
1473 = create_var_decl_1 (gnu_entity_name, gnu_ext_name, gnu_type,
1474 gnu_expr, const_flag, Is_Public (gnat_entity),
1475 imported_p || !definition, static_p,
1476 !renamed_obj, attr_list, gnat_entity);
1477 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1478 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1479 DECL_CAN_NEVER_BE_NULL_P (gnu_decl) = Can_Never_Be_Null (gnat_entity);
1481 /* If we are defining an Out parameter and optimization isn't enabled,
1482 create a fake PARM_DECL for debugging purposes and make it point to
1483 the VAR_DECL. Suppress debug info for the latter but make sure it
1484 will live in memory so that it can be accessed from within the
1485 debugger through the PARM_DECL. */
1486 if (kind == E_Out_Parameter
1487 && definition
1488 && debug_info_p
1489 && !optimize
1490 && !flag_generate_lto)
1492 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1493 gnat_pushdecl (param, gnat_entity);
1494 SET_DECL_VALUE_EXPR (param, gnu_decl);
1495 DECL_HAS_VALUE_EXPR_P (param) = 1;
1496 DECL_IGNORED_P (gnu_decl) = 1;
1497 TREE_ADDRESSABLE (gnu_decl) = 1;
1500 /* If this is a loop parameter, set the corresponding flag. */
1501 else if (kind == E_Loop_Parameter)
1502 DECL_LOOP_PARM_P (gnu_decl) = 1;
1504 /* If this is a renaming pointer, attach the renamed object to it and
1505 register it if we are at the global level. Note that an external
1506 constant is at the global level. */
1507 if (renamed_obj)
1509 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1510 if ((!definition && kind == E_Constant) || global_bindings_p ())
1512 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1513 record_global_renaming_pointer (gnu_decl);
1517 /* If this is a constant and we are defining it or it generates a real
1518 symbol at the object level and we are referencing it, we may want
1519 or need to have a true variable to represent it:
1520 - if optimization isn't enabled, for debugging purposes,
1521 - if the constant is public and not overlaid on something else,
1522 - if its address is taken,
1523 - if either itself or its type is aliased. */
1524 if (TREE_CODE (gnu_decl) == CONST_DECL
1525 && (definition || Sloc (gnat_entity) > Standard_Location)
1526 && ((!optimize && debug_info_p)
1527 || (Is_Public (gnat_entity)
1528 && No (Address_Clause (gnat_entity)))
1529 || Address_Taken (gnat_entity)
1530 || Is_Aliased (gnat_entity)
1531 || Is_Aliased (Etype (gnat_entity))))
1533 tree gnu_corr_var
1534 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1535 gnu_expr, true, Is_Public (gnat_entity),
1536 !definition, static_p, attr_list,
1537 gnat_entity);
1539 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1541 /* As debugging information will be generated for the variable,
1542 do not generate debugging information for the constant. */
1543 if (debug_info_p)
1544 DECL_IGNORED_P (gnu_decl) = 1;
1545 else
1546 DECL_IGNORED_P (gnu_corr_var) = 1;
1549 /* If this is a constant, even if we don't need a true variable, we
1550 may need to avoid returning the initializer in every case. That
1551 can happen for the address of a (constant) constructor because,
1552 upon dereferencing it, the constructor will be reinjected in the
1553 tree, which may not be valid in every case; see lvalue_required_p
1554 for more details. */
1555 if (TREE_CODE (gnu_decl) == CONST_DECL)
1556 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1558 /* If this object is declared in a block that contains a block with an
1559 exception handler, and we aren't using the GCC exception mechanism,
1560 we must force this variable in memory in order to avoid an invalid
1561 optimization. */
1562 if (Exception_Mechanism != Back_End_Exceptions
1563 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1564 TREE_ADDRESSABLE (gnu_decl) = 1;
1566 /* If this is a local variable with non-BLKmode and aggregate type,
1567 and optimization isn't enabled, then force it in memory so that
1568 a register won't be allocated to it with possible subparts left
1569 uninitialized and reaching the register allocator. */
1570 else if (TREE_CODE (gnu_decl) == VAR_DECL
1571 && !DECL_EXTERNAL (gnu_decl)
1572 && !TREE_STATIC (gnu_decl)
1573 && DECL_MODE (gnu_decl) != BLKmode
1574 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl))
1575 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl))
1576 && !optimize)
1577 TREE_ADDRESSABLE (gnu_decl) = 1;
1579 /* If we are defining an object with variable size or an object with
1580 fixed size that will be dynamically allocated, and we are using the
1581 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1582 if (definition
1583 && Exception_Mechanism == Setjmp_Longjmp
1584 && get_block_jmpbuf_decl ()
1585 && DECL_SIZE_UNIT (gnu_decl)
1586 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1587 || (flag_stack_check == GENERIC_STACK_CHECK
1588 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1589 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1590 add_stmt_with_node (build_call_n_expr
1591 (update_setjmp_buf_decl, 1,
1592 build_unary_op (ADDR_EXPR, NULL_TREE,
1593 get_block_jmpbuf_decl ())),
1594 gnat_entity);
1596 /* Back-annotate Esize and Alignment of the object if not already
1597 known. Note that we pick the values of the type, not those of
1598 the object, to shield ourselves from low-level platform-dependent
1599 adjustments like alignment promotion. This is both consistent with
1600 all the treatment above, where alignment and size are set on the
1601 type of the object and not on the object directly, and makes it
1602 possible to support all confirming representation clauses. */
1603 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1604 used_by_ref);
1606 break;
1608 case E_Void:
1609 /* Return a TYPE_DECL for "void" that we previously made. */
1610 gnu_decl = TYPE_NAME (void_type_node);
1611 break;
1613 case E_Enumeration_Type:
1614 /* A special case: for the types Character and Wide_Character in
1615 Standard, we do not list all the literals. So if the literals
1616 are not specified, make this an unsigned integer type. */
1617 if (No (First_Literal (gnat_entity)))
1619 gnu_type = make_unsigned_type (esize);
1620 TYPE_NAME (gnu_type) = gnu_entity_name;
1622 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1623 This is needed by the DWARF-2 back-end to distinguish between
1624 unsigned integer types and character types. */
1625 TYPE_STRING_FLAG (gnu_type) = 1;
1627 else
1629 /* We have a list of enumeral constants in First_Literal. We make a
1630 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1631 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1632 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1633 value of the literal. But when we have a regular boolean type, we
1634 simplify this a little by using a BOOLEAN_TYPE. */
1635 const bool is_boolean = Is_Boolean_Type (gnat_entity)
1636 && !Has_Non_Standard_Rep (gnat_entity);
1637 const bool is_unsigned = Is_Unsigned_Type (gnat_entity);
1638 tree gnu_list = NULL_TREE;
1639 Entity_Id gnat_literal;
1641 gnu_type = make_node (is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1642 TYPE_PRECISION (gnu_type) = esize;
1643 TYPE_UNSIGNED (gnu_type) = is_unsigned;
1644 set_min_and_max_values_for_integral_type (gnu_type, esize,
1645 TYPE_SIGN (gnu_type));
1646 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
1647 layout_type (gnu_type);
1649 for (gnat_literal = First_Literal (gnat_entity);
1650 Present (gnat_literal);
1651 gnat_literal = Next_Literal (gnat_literal))
1653 tree gnu_value
1654 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1655 tree gnu_literal
1656 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1657 gnu_type, gnu_value, true, false, false,
1658 false, NULL, gnat_literal);
1659 /* Do not generate debug info for individual enumerators. */
1660 DECL_IGNORED_P (gnu_literal) = 1;
1661 save_gnu_tree (gnat_literal, gnu_literal, false);
1662 gnu_list
1663 = tree_cons (DECL_NAME (gnu_literal), gnu_value, gnu_list);
1666 if (!is_boolean)
1667 TYPE_VALUES (gnu_type) = nreverse (gnu_list);
1669 /* Note that the bounds are updated at the end of this function
1670 to avoid an infinite recursion since they refer to the type. */
1671 goto discrete_type;
1673 break;
1675 case E_Signed_Integer_Type:
1676 case E_Ordinary_Fixed_Point_Type:
1677 case E_Decimal_Fixed_Point_Type:
1678 /* For integer types, just make a signed type the appropriate number
1679 of bits. */
1680 gnu_type = make_signed_type (esize);
1681 goto discrete_type;
1683 case E_Modular_Integer_Type:
1685 /* For modular types, make the unsigned type of the proper number
1686 of bits and then set up the modulus, if required. */
1687 tree gnu_modulus, gnu_high = NULL_TREE;
1689 /* Packed array types are supposed to be subtypes only. */
1690 gcc_assert (!Is_Packed_Array_Type (gnat_entity));
1692 gnu_type = make_unsigned_type (esize);
1694 /* Get the modulus in this type. If it overflows, assume it is because
1695 it is equal to 2**Esize. Note that there is no overflow checking
1696 done on unsigned type, so we detect the overflow by looking for
1697 a modulus of zero, which is otherwise invalid. */
1698 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1700 if (!integer_zerop (gnu_modulus))
1702 TYPE_MODULAR_P (gnu_type) = 1;
1703 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1704 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1705 convert (gnu_type, integer_one_node));
1708 /* If the upper bound is not maximal, make an extra subtype. */
1709 if (gnu_high
1710 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1712 tree gnu_subtype = make_unsigned_type (esize);
1713 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1714 TREE_TYPE (gnu_subtype) = gnu_type;
1715 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1716 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1717 gnu_type = gnu_subtype;
1720 goto discrete_type;
1722 case E_Signed_Integer_Subtype:
1723 case E_Enumeration_Subtype:
1724 case E_Modular_Integer_Subtype:
1725 case E_Ordinary_Fixed_Point_Subtype:
1726 case E_Decimal_Fixed_Point_Subtype:
1728 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1729 not want to call create_range_type since we would like each subtype
1730 node to be distinct. ??? Historically this was in preparation for
1731 when memory aliasing is implemented, but that's obsolete now given
1732 the call to relate_alias_sets below.
1734 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1735 this fact is used by the arithmetic conversion functions.
1737 We elaborate the Ancestor_Subtype if it is not in the current unit
1738 and one of our bounds is non-static. We do this to ensure consistent
1739 naming in the case where several subtypes share the same bounds, by
1740 elaborating the first such subtype first, thus using its name. */
1742 if (!definition
1743 && Present (Ancestor_Subtype (gnat_entity))
1744 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1745 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1746 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1747 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1749 /* Set the precision to the Esize except for bit-packed arrays. */
1750 if (Is_Packed_Array_Type (gnat_entity)
1751 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1752 esize = UI_To_Int (RM_Size (gnat_entity));
1754 /* This should be an unsigned type if the base type is unsigned or
1755 if the lower bound is constant and non-negative or if the type
1756 is biased. */
1757 if (Is_Unsigned_Type (Etype (gnat_entity))
1758 || Is_Unsigned_Type (gnat_entity)
1759 || Has_Biased_Representation (gnat_entity))
1760 gnu_type = make_unsigned_type (esize);
1761 else
1762 gnu_type = make_signed_type (esize);
1763 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1765 SET_TYPE_RM_MIN_VALUE
1766 (gnu_type,
1767 convert (TREE_TYPE (gnu_type),
1768 elaborate_expression (Type_Low_Bound (gnat_entity),
1769 gnat_entity, get_identifier ("L"),
1770 definition, true,
1771 Needs_Debug_Info (gnat_entity))));
1773 SET_TYPE_RM_MAX_VALUE
1774 (gnu_type,
1775 convert (TREE_TYPE (gnu_type),
1776 elaborate_expression (Type_High_Bound (gnat_entity),
1777 gnat_entity, get_identifier ("U"),
1778 definition, true,
1779 Needs_Debug_Info (gnat_entity))));
1781 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1782 = Has_Biased_Representation (gnat_entity);
1784 /* Inherit our alias set from what we're a subtype of. Subtypes
1785 are not different types and a pointer can designate any instance
1786 within a subtype hierarchy. */
1787 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1789 /* One of the above calls might have caused us to be elaborated,
1790 so don't blow up if so. */
1791 if (present_gnu_tree (gnat_entity))
1793 maybe_present = true;
1794 break;
1797 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1798 TYPE_STUB_DECL (gnu_type)
1799 = create_type_stub_decl (gnu_entity_name, gnu_type);
1801 /* For a packed array, make the original array type a parallel type. */
1802 if (debug_info_p
1803 && Is_Packed_Array_Type (gnat_entity)
1804 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1805 add_parallel_type (gnu_type,
1806 gnat_to_gnu_type
1807 (Original_Array_Type (gnat_entity)));
1809 discrete_type:
1811 /* We have to handle clauses that under-align the type specially. */
1812 if ((Present (Alignment_Clause (gnat_entity))
1813 || (Is_Packed_Array_Type (gnat_entity)
1814 && Present
1815 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1816 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1818 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1819 if (align >= TYPE_ALIGN (gnu_type))
1820 align = 0;
1823 /* If the type we are dealing with represents a bit-packed array,
1824 we need to have the bits left justified on big-endian targets
1825 and right justified on little-endian targets. We also need to
1826 ensure that when the value is read (e.g. for comparison of two
1827 such values), we only get the good bits, since the unused bits
1828 are uninitialized. Both goals are accomplished by wrapping up
1829 the modular type in an enclosing record type. */
1830 if (Is_Packed_Array_Type (gnat_entity)
1831 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1833 tree gnu_field_type, gnu_field;
1835 /* Set the RM size before wrapping up the original type. */
1836 SET_TYPE_RM_SIZE (gnu_type,
1837 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1838 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1840 /* Create a stripped-down declaration, mainly for debugging. */
1841 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1842 gnat_entity);
1844 /* Now save it and build the enclosing record type. */
1845 gnu_field_type = gnu_type;
1847 gnu_type = make_node (RECORD_TYPE);
1848 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1849 TYPE_PACKED (gnu_type) = 1;
1850 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1851 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1852 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1854 /* Propagate the alignment of the modular type to the record type,
1855 unless there is an alignment clause that under-aligns the type.
1856 This means that bit-packed arrays are given "ceil" alignment for
1857 their size by default, which may seem counter-intuitive but makes
1858 it possible to overlay them on modular types easily. */
1859 TYPE_ALIGN (gnu_type)
1860 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1862 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1864 /* Don't declare the field as addressable since we won't be taking
1865 its address and this would prevent create_field_decl from making
1866 a bitfield. */
1867 gnu_field
1868 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1869 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1871 /* Do not emit debug info until after the parallel type is added. */
1872 finish_record_type (gnu_type, gnu_field, 2, false);
1873 compute_record_mode (gnu_type);
1874 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1876 if (debug_info_p)
1878 /* Make the original array type a parallel type. */
1879 if (present_gnu_tree (Original_Array_Type (gnat_entity)))
1880 add_parallel_type (gnu_type,
1881 gnat_to_gnu_type
1882 (Original_Array_Type (gnat_entity)));
1884 rest_of_record_type_compilation (gnu_type);
1888 /* If the type we are dealing with has got a smaller alignment than the
1889 natural one, we need to wrap it up in a record type and misalign the
1890 latter; we reuse the padding machinery for this purpose. Note that,
1891 even if the record type is marked as packed because of misalignment,
1892 we don't pack the field so as to give it the size of the type. */
1893 else if (align > 0)
1895 tree gnu_field_type, gnu_field;
1897 /* Set the RM size before wrapping up the type. */
1898 SET_TYPE_RM_SIZE (gnu_type,
1899 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1901 /* Create a stripped-down declaration, mainly for debugging. */
1902 create_type_decl (gnu_entity_name, gnu_type, true, debug_info_p,
1903 gnat_entity);
1905 /* Now save it and build the enclosing record type. */
1906 gnu_field_type = gnu_type;
1908 gnu_type = make_node (RECORD_TYPE);
1909 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1910 TYPE_PACKED (gnu_type) = 1;
1911 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1912 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1913 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1914 TYPE_ALIGN (gnu_type) = align;
1915 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1917 /* Don't declare the field as addressable since we won't be taking
1918 its address and this would prevent create_field_decl from making
1919 a bitfield. */
1920 gnu_field
1921 = create_field_decl (get_identifier ("F"), gnu_field_type,
1922 gnu_type, TYPE_SIZE (gnu_field_type),
1923 bitsize_zero_node, 0, 0);
1925 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1926 compute_record_mode (gnu_type);
1927 TYPE_PADDING_P (gnu_type) = 1;
1930 break;
1932 case E_Floating_Point_Type:
1933 /* If this is a VAX floating-point type, use an integer of the proper
1934 size. All the operations will be handled with ASM statements. */
1935 if (Vax_Float (gnat_entity))
1937 gnu_type = make_signed_type (esize);
1938 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1939 SET_TYPE_DIGITS_VALUE (gnu_type,
1940 UI_To_gnu (Digits_Value (gnat_entity),
1941 sizetype));
1942 break;
1945 /* The type of the Low and High bounds can be our type if this is
1946 a type from Standard, so set them at the end of the function. */
1947 gnu_type = make_node (REAL_TYPE);
1948 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1949 layout_type (gnu_type);
1950 break;
1952 case E_Floating_Point_Subtype:
1953 if (Vax_Float (gnat_entity))
1955 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1956 break;
1959 /* See the E_Signed_Integer_Subtype case for the rationale. */
1960 if (!definition
1961 && Present (Ancestor_Subtype (gnat_entity))
1962 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1963 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1964 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1965 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1967 gnu_type = make_node (REAL_TYPE);
1968 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1969 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1970 TYPE_GCC_MIN_VALUE (gnu_type)
1971 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1972 TYPE_GCC_MAX_VALUE (gnu_type)
1973 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1974 layout_type (gnu_type);
1976 SET_TYPE_RM_MIN_VALUE
1977 (gnu_type,
1978 convert (TREE_TYPE (gnu_type),
1979 elaborate_expression (Type_Low_Bound (gnat_entity),
1980 gnat_entity, get_identifier ("L"),
1981 definition, true,
1982 Needs_Debug_Info (gnat_entity))));
1984 SET_TYPE_RM_MAX_VALUE
1985 (gnu_type,
1986 convert (TREE_TYPE (gnu_type),
1987 elaborate_expression (Type_High_Bound (gnat_entity),
1988 gnat_entity, get_identifier ("U"),
1989 definition, true,
1990 Needs_Debug_Info (gnat_entity))));
1992 /* Inherit our alias set from what we're a subtype of, as for
1993 integer subtypes. */
1994 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1996 /* One of the above calls might have caused us to be elaborated,
1997 so don't blow up if so. */
1998 maybe_present = true;
1999 break;
2001 /* Array and String Types and Subtypes
2003 Unconstrained array types are represented by E_Array_Type and
2004 constrained array types are represented by E_Array_Subtype. There
2005 are no actual objects of an unconstrained array type; all we have
2006 are pointers to that type.
2008 The following fields are defined on array types and subtypes:
2010 Component_Type Component type of the array.
2011 Number_Dimensions Number of dimensions (an int).
2012 First_Index Type of first index. */
2014 case E_String_Type:
2015 case E_Array_Type:
2017 const bool convention_fortran_p
2018 = (Convention (gnat_entity) == Convention_Fortran);
2019 const int ndim = Number_Dimensions (gnat_entity);
2020 tree gnu_template_type;
2021 tree gnu_ptr_template;
2022 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
2023 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2024 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
2025 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
2026 Entity_Id gnat_index, gnat_name;
2027 int index;
2028 tree comp_type;
2030 /* Create the type for the component now, as it simplifies breaking
2031 type reference loops. */
2032 comp_type
2033 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2034 if (present_gnu_tree (gnat_entity))
2036 /* As a side effect, the type may have been translated. */
2037 maybe_present = true;
2038 break;
2041 /* We complete an existing dummy fat pointer type in place. This both
2042 avoids further complex adjustments in update_pointer_to and yields
2043 better debugging information in DWARF by leveraging the support for
2044 incomplete declarations of "tagged" types in the DWARF back-end. */
2045 gnu_type = get_dummy_type (gnat_entity);
2046 if (gnu_type && TYPE_POINTER_TO (gnu_type))
2048 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
2049 TYPE_NAME (gnu_fat_type) = NULL_TREE;
2050 /* Save the contents of the dummy type for update_pointer_to. */
2051 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
2052 gnu_ptr_template =
2053 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
2054 gnu_template_type = TREE_TYPE (gnu_ptr_template);
2056 else
2058 gnu_fat_type = make_node (RECORD_TYPE);
2059 gnu_template_type = make_node (RECORD_TYPE);
2060 gnu_ptr_template = build_pointer_type (gnu_template_type);
2063 /* Make a node for the array. If we are not defining the array
2064 suppress expanding incomplete types. */
2065 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
2067 if (!definition)
2069 defer_incomplete_level++;
2070 this_deferred = true;
2073 /* Build the fat pointer type. Use a "void *" object instead of
2074 a pointer to the array type since we don't have the array type
2075 yet (it will reference the fat pointer via the bounds). */
2077 = create_field_decl (get_identifier ("P_ARRAY"), ptr_void_type_node,
2078 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2079 DECL_CHAIN (tem)
2080 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
2081 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
2083 if (COMPLETE_TYPE_P (gnu_fat_type))
2085 /* We are going to lay it out again so reset the alias set. */
2086 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
2087 TYPE_ALIAS_SET (gnu_fat_type) = -1;
2088 finish_fat_pointer_type (gnu_fat_type, tem);
2089 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
2090 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
2092 TYPE_FIELDS (t) = tem;
2093 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
2096 else
2098 finish_fat_pointer_type (gnu_fat_type, tem);
2099 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2102 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2103 is the fat pointer. This will be used to access the individual
2104 fields once we build them. */
2105 tem = build3 (COMPONENT_REF, gnu_ptr_template,
2106 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
2107 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
2108 gnu_template_reference
2109 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
2110 TREE_READONLY (gnu_template_reference) = 1;
2111 TREE_THIS_NOTRAP (gnu_template_reference) = 1;
2113 /* Now create the GCC type for each index and add the fields for that
2114 index to the template. */
2115 for (index = (convention_fortran_p ? ndim - 1 : 0),
2116 gnat_index = First_Index (gnat_entity);
2117 0 <= index && index < ndim;
2118 index += (convention_fortran_p ? - 1 : 1),
2119 gnat_index = Next_Index (gnat_index))
2121 char field_name[16];
2122 tree gnu_index_base_type
2123 = get_unpadded_type (Base_Type (Etype (gnat_index)));
2124 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2125 tree gnu_min, gnu_max, gnu_high;
2127 /* Make the FIELD_DECLs for the low and high bounds of this
2128 type and then make extractions of these fields from the
2129 template. */
2130 sprintf (field_name, "LB%d", index);
2131 gnu_lb_field = create_field_decl (get_identifier (field_name),
2132 gnu_index_base_type,
2133 gnu_template_type, NULL_TREE,
2134 NULL_TREE, 0, 0);
2135 Sloc_to_locus (Sloc (gnat_entity),
2136 &DECL_SOURCE_LOCATION (gnu_lb_field));
2138 field_name[0] = 'U';
2139 gnu_hb_field = create_field_decl (get_identifier (field_name),
2140 gnu_index_base_type,
2141 gnu_template_type, NULL_TREE,
2142 NULL_TREE, 0, 0);
2143 Sloc_to_locus (Sloc (gnat_entity),
2144 &DECL_SOURCE_LOCATION (gnu_hb_field));
2146 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2148 /* We can't use build_component_ref here since the template type
2149 isn't complete yet. */
2150 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2151 gnu_template_reference, gnu_lb_field,
2152 NULL_TREE);
2153 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2154 gnu_template_reference, gnu_hb_field,
2155 NULL_TREE);
2156 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2158 gnu_min = convert (sizetype, gnu_orig_min);
2159 gnu_max = convert (sizetype, gnu_orig_max);
2161 /* Compute the size of this dimension. See the E_Array_Subtype
2162 case below for the rationale. */
2163 gnu_high
2164 = build3 (COND_EXPR, sizetype,
2165 build2 (GE_EXPR, boolean_type_node,
2166 gnu_orig_max, gnu_orig_min),
2167 gnu_max,
2168 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2170 /* Make a range type with the new range in the Ada base type.
2171 Then make an index type with the size range in sizetype. */
2172 gnu_index_types[index]
2173 = create_index_type (gnu_min, gnu_high,
2174 create_range_type (gnu_index_base_type,
2175 gnu_orig_min,
2176 gnu_orig_max),
2177 gnat_entity);
2179 /* Update the maximum size of the array in elements. */
2180 if (gnu_max_size)
2182 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2183 tree gnu_min
2184 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2185 tree gnu_max
2186 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2187 tree gnu_this_max
2188 = size_binop (MAX_EXPR,
2189 size_binop (PLUS_EXPR, size_one_node,
2190 size_binop (MINUS_EXPR,
2191 gnu_max, gnu_min)),
2192 size_zero_node);
2194 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2195 && TREE_OVERFLOW (gnu_this_max))
2196 gnu_max_size = NULL_TREE;
2197 else
2198 gnu_max_size
2199 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2202 TYPE_NAME (gnu_index_types[index])
2203 = create_concat_name (gnat_entity, field_name);
2206 /* Install all the fields into the template. */
2207 TYPE_NAME (gnu_template_type)
2208 = create_concat_name (gnat_entity, "XUB");
2209 gnu_template_fields = NULL_TREE;
2210 for (index = 0; index < ndim; index++)
2211 gnu_template_fields
2212 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2213 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2214 debug_info_p);
2215 TYPE_READONLY (gnu_template_type) = 1;
2217 /* If Component_Size is not already specified, annotate it with the
2218 size of the component. */
2219 if (Unknown_Component_Size (gnat_entity))
2220 Set_Component_Size (gnat_entity,
2221 annotate_value (TYPE_SIZE (comp_type)));
2223 /* Compute the maximum size of the array in units and bits. */
2224 if (gnu_max_size)
2226 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2227 TYPE_SIZE_UNIT (comp_type));
2228 gnu_max_size = size_binop (MULT_EXPR,
2229 convert (bitsizetype, gnu_max_size),
2230 TYPE_SIZE (comp_type));
2232 else
2233 gnu_max_size_unit = NULL_TREE;
2235 /* Now build the array type. */
2236 tem = comp_type;
2237 for (index = ndim - 1; index >= 0; index--)
2239 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2240 if (Reverse_Storage_Order (gnat_entity))
2241 sorry ("non-default Scalar_Storage_Order");
2242 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2243 if (array_type_has_nonaliased_component (tem, gnat_entity))
2244 TYPE_NONALIASED_COMPONENT (tem) = 1;
2246 /* If it is passed by reference, force BLKmode to ensure that
2247 objects of this type will always be put in memory. */
2248 if (TYPE_MODE (tem) != BLKmode
2249 && Is_By_Reference_Type (gnat_entity))
2250 SET_TYPE_MODE (tem, BLKmode);
2253 /* If an alignment is specified, use it if valid. But ignore it
2254 for the original type of packed array types. If the alignment
2255 was requested with an explicit alignment clause, state so. */
2256 if (No (Packed_Array_Type (gnat_entity))
2257 && Known_Alignment (gnat_entity))
2259 TYPE_ALIGN (tem)
2260 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2261 TYPE_ALIGN (tem));
2262 if (Present (Alignment_Clause (gnat_entity)))
2263 TYPE_USER_ALIGN (tem) = 1;
2266 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2268 /* Adjust the type of the pointer-to-array field of the fat pointer
2269 and record the aliasing relationships if necessary. */
2270 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2271 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2272 record_component_aliases (gnu_fat_type);
2274 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2275 corresponding fat pointer. */
2276 TREE_TYPE (gnu_type) = gnu_fat_type;
2277 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2278 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2279 SET_TYPE_MODE (gnu_type, BLKmode);
2280 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2282 /* If the maximum size doesn't overflow, use it. */
2283 if (gnu_max_size
2284 && TREE_CODE (gnu_max_size) == INTEGER_CST
2285 && !TREE_OVERFLOW (gnu_max_size)
2286 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2287 && !TREE_OVERFLOW (gnu_max_size_unit))
2289 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2290 TYPE_SIZE (tem));
2291 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2292 TYPE_SIZE_UNIT (tem));
2295 create_type_decl (create_concat_name (gnat_entity, "XUA"), tem,
2296 !Comes_From_Source (gnat_entity), debug_info_p,
2297 gnat_entity);
2299 /* Give the fat pointer type a name. If this is a packed type, tell
2300 the debugger how to interpret the underlying bits. */
2301 if (Present (Packed_Array_Type (gnat_entity)))
2302 gnat_name = Packed_Array_Type (gnat_entity);
2303 else
2304 gnat_name = gnat_entity;
2305 create_type_decl (create_concat_name (gnat_name, "XUP"), gnu_fat_type,
2306 !Comes_From_Source (gnat_entity), debug_info_p,
2307 gnat_entity);
2309 /* Create the type to be designated by thin pointers: a record type for
2310 the array and its template. We used to shift the fields to have the
2311 template at a negative offset, but this was somewhat of a kludge; we
2312 now shift thin pointer values explicitly but only those which have a
2313 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE. */
2314 tem = build_unc_object_type (gnu_template_type, tem,
2315 create_concat_name (gnat_name, "XUT"),
2316 debug_info_p);
2318 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2319 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2321 break;
2323 case E_String_Subtype:
2324 case E_Array_Subtype:
2326 /* This is the actual data type for array variables. Multidimensional
2327 arrays are implemented as arrays of arrays. Note that arrays which
2328 have sparse enumeration subtypes as index components create sparse
2329 arrays, which is obviously space inefficient but so much easier to
2330 code for now.
2332 Also note that the subtype never refers to the unconstrained array
2333 type, which is somewhat at variance with Ada semantics.
2335 First check to see if this is simply a renaming of the array type.
2336 If so, the result is the array type. */
2338 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2339 if (!Is_Constrained (gnat_entity))
2341 else
2343 Entity_Id gnat_index, gnat_base_index;
2344 const bool convention_fortran_p
2345 = (Convention (gnat_entity) == Convention_Fortran);
2346 const int ndim = Number_Dimensions (gnat_entity);
2347 tree gnu_base_type = gnu_type;
2348 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2349 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2350 bool need_index_type_struct = false;
2351 int index;
2353 /* First create the GCC type for each index and find out whether
2354 special types are needed for debugging information. */
2355 for (index = (convention_fortran_p ? ndim - 1 : 0),
2356 gnat_index = First_Index (gnat_entity),
2357 gnat_base_index
2358 = First_Index (Implementation_Base_Type (gnat_entity));
2359 0 <= index && index < ndim;
2360 index += (convention_fortran_p ? - 1 : 1),
2361 gnat_index = Next_Index (gnat_index),
2362 gnat_base_index = Next_Index (gnat_base_index))
2364 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2365 tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type);
2366 tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type);
2367 tree gnu_min = convert (sizetype, gnu_orig_min);
2368 tree gnu_max = convert (sizetype, gnu_orig_max);
2369 tree gnu_base_index_type
2370 = get_unpadded_type (Etype (gnat_base_index));
2371 tree gnu_base_orig_min = TYPE_MIN_VALUE (gnu_base_index_type);
2372 tree gnu_base_orig_max = TYPE_MAX_VALUE (gnu_base_index_type);
2373 tree gnu_high;
2375 /* See if the base array type is already flat. If it is, we
2376 are probably compiling an ACATS test but it will cause the
2377 code below to malfunction if we don't handle it specially. */
2378 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2379 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2380 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2382 gnu_min = size_one_node;
2383 gnu_max = size_zero_node;
2384 gnu_high = gnu_max;
2387 /* Similarly, if one of the values overflows in sizetype and the
2388 range is null, use 1..0 for the sizetype bounds. */
2389 else if (TREE_CODE (gnu_min) == INTEGER_CST
2390 && TREE_CODE (gnu_max) == INTEGER_CST
2391 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2392 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2394 gnu_min = size_one_node;
2395 gnu_max = size_zero_node;
2396 gnu_high = gnu_max;
2399 /* If the minimum and maximum values both overflow in sizetype,
2400 but the difference in the original type does not overflow in
2401 sizetype, ignore the overflow indication. */
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_OVERFLOW
2406 (convert (sizetype,
2407 fold_build2 (MINUS_EXPR, gnu_index_type,
2408 gnu_orig_max,
2409 gnu_orig_min))))
2411 TREE_OVERFLOW (gnu_min) = 0;
2412 TREE_OVERFLOW (gnu_max) = 0;
2413 gnu_high = gnu_max;
2416 /* Compute the size of this dimension in the general case. We
2417 need to provide GCC with an upper bound to use but have to
2418 deal with the "superflat" case. There are three ways to do
2419 this. If we can prove that the array can never be superflat,
2420 we can just use the high bound of the index type. */
2421 else if ((Nkind (gnat_index) == N_Range
2422 && cannot_be_superflat_p (gnat_index))
2423 /* Packed Array Types are never superflat. */
2424 || Is_Packed_Array_Type (gnat_entity))
2425 gnu_high = gnu_max;
2427 /* Otherwise, if the high bound is constant but the low bound is
2428 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2429 lower bound. Note that the comparison must be done in the
2430 original type to avoid any overflow during the conversion. */
2431 else if (TREE_CODE (gnu_max) == INTEGER_CST
2432 && TREE_CODE (gnu_min) != INTEGER_CST)
2434 gnu_high = gnu_max;
2435 gnu_min
2436 = build_cond_expr (sizetype,
2437 build_binary_op (GE_EXPR,
2438 boolean_type_node,
2439 gnu_orig_max,
2440 gnu_orig_min),
2441 gnu_min,
2442 int_const_binop (PLUS_EXPR, gnu_max,
2443 size_one_node));
2446 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2447 in all the other cases. Note that, here as well as above,
2448 the condition used in the comparison must be equivalent to
2449 the condition (length != 0). This is relied upon in order
2450 to optimize array comparisons in compare_arrays. Moreover
2451 we use int_const_binop for the shift by 1 if the bound is
2452 constant to avoid any unwanted overflow. */
2453 else
2454 gnu_high
2455 = build_cond_expr (sizetype,
2456 build_binary_op (GE_EXPR,
2457 boolean_type_node,
2458 gnu_orig_max,
2459 gnu_orig_min),
2460 gnu_max,
2461 TREE_CODE (gnu_min) == INTEGER_CST
2462 ? int_const_binop (MINUS_EXPR, gnu_min,
2463 size_one_node)
2464 : size_binop (MINUS_EXPR, gnu_min,
2465 size_one_node));
2467 /* Reuse the index type for the range type. Then make an index
2468 type with the size range in sizetype. */
2469 gnu_index_types[index]
2470 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2471 gnat_entity);
2473 /* Update the maximum size of the array in elements. Here we
2474 see if any constraint on the index type of the base type
2475 can be used in the case of self-referential bound on the
2476 index type of the subtype. We look for a non-"infinite"
2477 and non-self-referential bound from any type involved and
2478 handle each bound separately. */
2479 if (gnu_max_size)
2481 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2482 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2483 tree gnu_base_index_base_type
2484 = get_base_type (gnu_base_index_type);
2485 tree gnu_base_base_min
2486 = convert (sizetype,
2487 TYPE_MIN_VALUE (gnu_base_index_base_type));
2488 tree gnu_base_base_max
2489 = convert (sizetype,
2490 TYPE_MAX_VALUE (gnu_base_index_base_type));
2492 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2493 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2494 && !TREE_OVERFLOW (gnu_base_min)))
2495 gnu_base_min = gnu_min;
2497 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2498 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2499 && !TREE_OVERFLOW (gnu_base_max)))
2500 gnu_base_max = gnu_max;
2502 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2503 && TREE_OVERFLOW (gnu_base_min))
2504 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2505 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2506 && TREE_OVERFLOW (gnu_base_max))
2507 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2508 gnu_max_size = NULL_TREE;
2509 else
2511 tree gnu_this_max
2512 = size_binop (MAX_EXPR,
2513 size_binop (PLUS_EXPR, size_one_node,
2514 size_binop (MINUS_EXPR,
2515 gnu_base_max,
2516 gnu_base_min)),
2517 size_zero_node);
2519 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2520 && TREE_OVERFLOW (gnu_this_max))
2521 gnu_max_size = NULL_TREE;
2522 else
2523 gnu_max_size
2524 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2528 /* We need special types for debugging information to point to
2529 the index types if they have variable bounds, are not integer
2530 types, are biased or are wider than sizetype. */
2531 if (!integer_onep (gnu_orig_min)
2532 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2533 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2534 || (TREE_TYPE (gnu_index_type)
2535 && TREE_CODE (TREE_TYPE (gnu_index_type))
2536 != INTEGER_TYPE)
2537 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type)
2538 || compare_tree_int (rm_size (gnu_index_type),
2539 TYPE_PRECISION (sizetype)) > 0)
2540 need_index_type_struct = true;
2543 /* Then flatten: create the array of arrays. For an array type
2544 used to implement a packed array, get the component type from
2545 the original array type since the representation clauses that
2546 can affect it are on the latter. */
2547 if (Is_Packed_Array_Type (gnat_entity)
2548 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2550 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2551 for (index = ndim - 1; index >= 0; index--)
2552 gnu_type = TREE_TYPE (gnu_type);
2554 /* One of the above calls might have caused us to be elaborated,
2555 so don't blow up if so. */
2556 if (present_gnu_tree (gnat_entity))
2558 maybe_present = true;
2559 break;
2562 else
2564 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2565 debug_info_p);
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;
2576 /* Compute the maximum size of the array in units and bits. */
2577 if (gnu_max_size)
2579 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2580 TYPE_SIZE_UNIT (gnu_type));
2581 gnu_max_size = size_binop (MULT_EXPR,
2582 convert (bitsizetype, gnu_max_size),
2583 TYPE_SIZE (gnu_type));
2585 else
2586 gnu_max_size_unit = NULL_TREE;
2588 /* Now build the array type. */
2589 for (index = ndim - 1; index >= 0; index --)
2591 gnu_type = build_nonshared_array_type (gnu_type,
2592 gnu_index_types[index]);
2593 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2594 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2595 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2597 /* See the E_Array_Type case for the rationale. */
2598 if (TYPE_MODE (gnu_type) != BLKmode
2599 && Is_By_Reference_Type (gnat_entity))
2600 SET_TYPE_MODE (gnu_type, BLKmode);
2603 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2604 TYPE_STUB_DECL (gnu_type)
2605 = create_type_stub_decl (gnu_entity_name, gnu_type);
2607 /* If we are at file level and this is a multi-dimensional array,
2608 we need to make a variable corresponding to the stride of the
2609 inner dimensions. */
2610 if (global_bindings_p () && ndim > 1)
2612 tree gnu_st_name = get_identifier ("ST");
2613 tree gnu_arr_type;
2615 for (gnu_arr_type = TREE_TYPE (gnu_type);
2616 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2617 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2618 gnu_st_name = concat_name (gnu_st_name, "ST"))
2620 tree eltype = TREE_TYPE (gnu_arr_type);
2622 TYPE_SIZE (gnu_arr_type)
2623 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2624 gnat_entity, gnu_st_name,
2625 definition, false);
2627 /* ??? For now, store the size as a multiple of the
2628 alignment of the element type in bytes so that we
2629 can see the alignment from the tree. */
2630 TYPE_SIZE_UNIT (gnu_arr_type)
2631 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2632 gnat_entity,
2633 concat_name (gnu_st_name, "A_U"),
2634 definition, false,
2635 TYPE_ALIGN (eltype));
2637 /* ??? create_type_decl is not invoked on the inner types so
2638 the MULT_EXPR node built above will never be marked. */
2639 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2643 /* If we need to write out a record type giving the names of the
2644 bounds for debugging purposes, do it now and make the record
2645 type a parallel type. This is not needed for a packed array
2646 since the bounds are conveyed by the original array type. */
2647 if (need_index_type_struct
2648 && debug_info_p
2649 && !Is_Packed_Array_Type (gnat_entity))
2651 tree gnu_bound_rec = make_node (RECORD_TYPE);
2652 tree gnu_field_list = NULL_TREE;
2653 tree gnu_field;
2655 TYPE_NAME (gnu_bound_rec)
2656 = create_concat_name (gnat_entity, "XA");
2658 for (index = ndim - 1; index >= 0; index--)
2660 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2661 tree gnu_index_name = TYPE_IDENTIFIER (gnu_index);
2663 /* Make sure to reference the types themselves, and not just
2664 their names, as the debugger may fall back on them. */
2665 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2666 gnu_bound_rec, NULL_TREE,
2667 NULL_TREE, 0, 0);
2668 DECL_CHAIN (gnu_field) = gnu_field_list;
2669 gnu_field_list = gnu_field;
2672 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2673 add_parallel_type (gnu_type, gnu_bound_rec);
2676 /* If this is a packed array type, make the original array type a
2677 parallel type. Otherwise, do it for the base array type if it
2678 isn't artificial to make sure it is kept in the debug info. */
2679 if (debug_info_p)
2681 if (Is_Packed_Array_Type (gnat_entity)
2682 && present_gnu_tree (Original_Array_Type (gnat_entity)))
2683 add_parallel_type (gnu_type,
2684 gnat_to_gnu_type
2685 (Original_Array_Type (gnat_entity)));
2686 else
2688 tree gnu_base_decl
2689 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2690 if (!DECL_ARTIFICIAL (gnu_base_decl))
2691 add_parallel_type (gnu_type,
2692 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2696 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2697 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2698 = (Is_Packed_Array_Type (gnat_entity)
2699 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2701 /* If the size is self-referential and the maximum size doesn't
2702 overflow, use it. */
2703 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2704 && gnu_max_size
2705 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2706 && TREE_OVERFLOW (gnu_max_size))
2707 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2708 && TREE_OVERFLOW (gnu_max_size_unit)))
2710 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2711 TYPE_SIZE (gnu_type));
2712 TYPE_SIZE_UNIT (gnu_type)
2713 = size_binop (MIN_EXPR, gnu_max_size_unit,
2714 TYPE_SIZE_UNIT (gnu_type));
2717 /* Set our alias set to that of our base type. This gives all
2718 array subtypes the same alias set. */
2719 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2721 /* If this is a packed type, make this type the same as the packed
2722 array type, but do some adjusting in the type first. */
2723 if (Present (Packed_Array_Type (gnat_entity)))
2725 Entity_Id gnat_index;
2726 tree gnu_inner;
2728 /* First finish the type we had been making so that we output
2729 debugging information for it. */
2730 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
2731 if (Treat_As_Volatile (gnat_entity))
2732 gnu_type
2733 = build_qualified_type (gnu_type,
2734 TYPE_QUALS (gnu_type)
2735 | TYPE_QUAL_VOLATILE);
2736 /* Make it artificial only if the base type was artificial too.
2737 That's sort of "morally" true and will make it possible for
2738 the debugger to look it up by name in DWARF, which is needed
2739 in order to decode the packed array type. */
2740 gnu_decl
2741 = create_type_decl (gnu_entity_name, gnu_type,
2742 !Comes_From_Source (Etype (gnat_entity))
2743 && !Comes_From_Source (gnat_entity),
2744 debug_info_p, gnat_entity);
2746 /* Save it as our equivalent in case the call below elaborates
2747 this type again. */
2748 save_gnu_tree (gnat_entity, gnu_decl, false);
2750 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2751 NULL_TREE, 0);
2752 this_made_decl = true;
2753 gnu_type = TREE_TYPE (gnu_decl);
2754 save_gnu_tree (gnat_entity, NULL_TREE, false);
2756 gnu_inner = gnu_type;
2757 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2758 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2759 || TYPE_PADDING_P (gnu_inner)))
2760 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2762 /* We need to attach the index type to the type we just made so
2763 that the actual bounds can later be put into a template. */
2764 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2765 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2766 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2767 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2769 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2771 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2772 TYPE_MODULUS for modular types so we make an extra
2773 subtype if necessary. */
2774 if (TYPE_MODULAR_P (gnu_inner))
2776 tree gnu_subtype
2777 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2778 TREE_TYPE (gnu_subtype) = gnu_inner;
2779 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2780 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2781 TYPE_MIN_VALUE (gnu_inner));
2782 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2783 TYPE_MAX_VALUE (gnu_inner));
2784 gnu_inner = gnu_subtype;
2787 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2789 #ifdef ENABLE_CHECKING
2790 /* Check for other cases of overloading. */
2791 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2792 #endif
2795 for (gnat_index = First_Index (gnat_entity);
2796 Present (gnat_index);
2797 gnat_index = Next_Index (gnat_index))
2798 SET_TYPE_ACTUAL_BOUNDS
2799 (gnu_inner,
2800 tree_cons (NULL_TREE,
2801 get_unpadded_type (Etype (gnat_index)),
2802 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2804 if (Convention (gnat_entity) != Convention_Fortran)
2805 SET_TYPE_ACTUAL_BOUNDS
2806 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2808 if (TREE_CODE (gnu_type) == RECORD_TYPE
2809 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2810 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2814 else
2815 /* Abort if packed array with no Packed_Array_Type field set. */
2816 gcc_assert (!Is_Packed (gnat_entity));
2818 break;
2820 case E_String_Literal_Subtype:
2821 /* Create the type for a string literal. */
2823 Entity_Id gnat_full_type
2824 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2825 && Present (Full_View (Etype (gnat_entity)))
2826 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2827 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2828 tree gnu_string_array_type
2829 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2830 tree gnu_string_index_type
2831 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2832 (TYPE_DOMAIN (gnu_string_array_type))));
2833 tree gnu_lower_bound
2834 = convert (gnu_string_index_type,
2835 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2836 tree gnu_length
2837 = UI_To_gnu (String_Literal_Length (gnat_entity),
2838 gnu_string_index_type);
2839 tree gnu_upper_bound
2840 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2841 gnu_lower_bound,
2842 int_const_binop (MINUS_EXPR, gnu_length,
2843 integer_one_node));
2844 tree gnu_index_type
2845 = create_index_type (convert (sizetype, gnu_lower_bound),
2846 convert (sizetype, gnu_upper_bound),
2847 create_range_type (gnu_string_index_type,
2848 gnu_lower_bound,
2849 gnu_upper_bound),
2850 gnat_entity);
2852 gnu_type
2853 = build_nonshared_array_type (gnat_to_gnu_type
2854 (Component_Type (gnat_entity)),
2855 gnu_index_type);
2856 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2857 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2858 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2860 break;
2862 /* Record Types and Subtypes
2864 The following fields are defined on record types:
2866 Has_Discriminants True if the record has discriminants
2867 First_Discriminant Points to head of list of discriminants
2868 First_Entity Points to head of list of fields
2869 Is_Tagged_Type True if the record is tagged
2871 Implementation of Ada records and discriminated records:
2873 A record type definition is transformed into the equivalent of a C
2874 struct definition. The fields that are the discriminants which are
2875 found in the Full_Type_Declaration node and the elements of the
2876 Component_List found in the Record_Type_Definition node. The
2877 Component_List can be a recursive structure since each Variant of
2878 the Variant_Part of the Component_List has a Component_List.
2880 Processing of a record type definition comprises starting the list of
2881 field declarations here from the discriminants and the calling the
2882 function components_to_record to add the rest of the fields from the
2883 component list and return the gnu type node. The function
2884 components_to_record will call itself recursively as it traverses
2885 the tree. */
2887 case E_Record_Type:
2888 if (Has_Complex_Representation (gnat_entity))
2890 gnu_type
2891 = build_complex_type
2892 (get_unpadded_type
2893 (Etype (Defining_Entity
2894 (First (Component_Items
2895 (Component_List
2896 (Type_Definition
2897 (Declaration_Node (gnat_entity)))))))));
2899 break;
2903 Node_Id full_definition = Declaration_Node (gnat_entity);
2904 Node_Id record_definition = Type_Definition (full_definition);
2905 Node_Id gnat_constr;
2906 Entity_Id gnat_field;
2907 tree gnu_field, gnu_field_list = NULL_TREE;
2908 tree gnu_get_parent;
2909 /* Set PACKED in keeping with gnat_to_gnu_field. */
2910 const int packed
2911 = Is_Packed (gnat_entity)
2913 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2914 ? -1
2915 : (Known_Alignment (gnat_entity)
2916 || (Strict_Alignment (gnat_entity)
2917 && Known_RM_Size (gnat_entity)))
2918 ? -2
2919 : 0;
2920 const bool has_discr = Has_Discriminants (gnat_entity);
2921 const bool has_rep = Has_Specified_Layout (gnat_entity);
2922 const bool is_extension
2923 = (Is_Tagged_Type (gnat_entity)
2924 && Nkind (record_definition) == N_Derived_Type_Definition);
2925 const bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2926 bool all_rep = has_rep;
2928 /* See if all fields have a rep clause. Stop when we find one
2929 that doesn't. */
2930 if (all_rep)
2931 for (gnat_field = First_Entity (gnat_entity);
2932 Present (gnat_field);
2933 gnat_field = Next_Entity (gnat_field))
2934 if ((Ekind (gnat_field) == E_Component
2935 || Ekind (gnat_field) == E_Discriminant)
2936 && No (Component_Clause (gnat_field)))
2938 all_rep = false;
2939 break;
2942 /* If this is a record extension, go a level further to find the
2943 record definition. Also, verify we have a Parent_Subtype. */
2944 if (is_extension)
2946 if (!type_annotate_only
2947 || Present (Record_Extension_Part (record_definition)))
2948 record_definition = Record_Extension_Part (record_definition);
2950 gcc_assert (type_annotate_only
2951 || Present (Parent_Subtype (gnat_entity)));
2954 /* Make a node for the record. If we are not defining the record,
2955 suppress expanding incomplete types. */
2956 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2957 TYPE_NAME (gnu_type) = gnu_entity_name;
2958 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2959 if (Reverse_Storage_Order (gnat_entity))
2960 sorry ("non-default Scalar_Storage_Order");
2961 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
2963 if (!definition)
2965 defer_incomplete_level++;
2966 this_deferred = true;
2969 /* If both a size and rep clause was specified, put the size in
2970 the record type now so that it can get the proper mode. */
2971 if (has_rep && Known_RM_Size (gnat_entity))
2972 TYPE_SIZE (gnu_type)
2973 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
2975 /* Always set the alignment here so that it can be used to
2976 set the mode, if it is making the alignment stricter. If
2977 it is invalid, it will be checked again below. If this is to
2978 be Atomic, choose a default alignment of a word unless we know
2979 the size and it's smaller. */
2980 if (Known_Alignment (gnat_entity))
2981 TYPE_ALIGN (gnu_type)
2982 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2983 else if (Is_Atomic (gnat_entity) && Known_Esize (gnat_entity))
2985 unsigned int size = UI_To_Int (Esize (gnat_entity));
2986 TYPE_ALIGN (gnu_type)
2987 = size >= BITS_PER_WORD ? BITS_PER_WORD : ceil_pow2 (size);
2989 /* If a type needs strict alignment, the minimum size will be the
2990 type size instead of the RM size (see validate_size). Cap the
2991 alignment, lest it causes this type size to become too large. */
2992 else if (Strict_Alignment (gnat_entity) && Known_RM_Size (gnat_entity))
2994 unsigned int raw_size = UI_To_Int (RM_Size (gnat_entity));
2995 unsigned int raw_align = raw_size & -raw_size;
2996 if (raw_align < BIGGEST_ALIGNMENT)
2997 TYPE_ALIGN (gnu_type) = raw_align;
2999 else
3000 TYPE_ALIGN (gnu_type) = 0;
3002 /* If we have a Parent_Subtype, make a field for the parent. If
3003 this record has rep clauses, force the position to zero. */
3004 if (Present (Parent_Subtype (gnat_entity)))
3006 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
3007 tree gnu_dummy_parent_type = make_node (RECORD_TYPE);
3008 tree gnu_parent;
3010 /* A major complexity here is that the parent subtype will
3011 reference our discriminants in its Stored_Constraint list.
3012 But those must reference the parent component of this record
3013 which is precisely of the parent subtype we have not built yet!
3014 To break the circle we first build a dummy COMPONENT_REF which
3015 represents the "get to the parent" operation and initialize
3016 each of those discriminants to a COMPONENT_REF of the above
3017 dummy parent referencing the corresponding discriminant of the
3018 base type of the parent subtype. */
3019 gnu_get_parent = build3 (COMPONENT_REF, gnu_dummy_parent_type,
3020 build0 (PLACEHOLDER_EXPR, gnu_type),
3021 build_decl (input_location,
3022 FIELD_DECL, NULL_TREE,
3023 gnu_dummy_parent_type),
3024 NULL_TREE);
3026 if (has_discr)
3027 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3028 Present (gnat_field);
3029 gnat_field = Next_Stored_Discriminant (gnat_field))
3030 if (Present (Corresponding_Discriminant (gnat_field)))
3032 tree gnu_field
3033 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3034 (gnat_field));
3035 save_gnu_tree
3036 (gnat_field,
3037 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3038 gnu_get_parent, gnu_field, NULL_TREE),
3039 true);
3042 /* Then we build the parent subtype. If it has discriminants but
3043 the type itself has unknown discriminants, this means that it
3044 doesn't contain information about how the discriminants are
3045 derived from those of the ancestor type, so it cannot be used
3046 directly. Instead it is built by cloning the parent subtype
3047 of the underlying record view of the type, for which the above
3048 derivation of discriminants has been made explicit. */
3049 if (Has_Discriminants (gnat_parent)
3050 && Has_Unknown_Discriminants (gnat_entity))
3052 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
3054 /* If we are defining the type, the underlying record
3055 view must already have been elaborated at this point.
3056 Otherwise do it now as its parent subtype cannot be
3057 technically elaborated on its own. */
3058 if (definition)
3059 gcc_assert (present_gnu_tree (gnat_uview));
3060 else
3061 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
3063 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
3065 /* Substitute the "get to the parent" of the type for that
3066 of its underlying record view in the cloned type. */
3067 for (gnat_field = First_Stored_Discriminant (gnat_uview);
3068 Present (gnat_field);
3069 gnat_field = Next_Stored_Discriminant (gnat_field))
3070 if (Present (Corresponding_Discriminant (gnat_field)))
3072 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
3073 tree gnu_ref
3074 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3075 gnu_get_parent, gnu_field, NULL_TREE);
3076 gnu_parent
3077 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
3080 else
3081 gnu_parent = gnat_to_gnu_type (gnat_parent);
3083 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3084 initially built. The discriminants must reference the fields
3085 of the parent subtype and not those of its base type for the
3086 placeholder machinery to properly work. */
3087 if (has_discr)
3089 /* The actual parent subtype is the full view. */
3090 if (IN (Ekind (gnat_parent), Private_Kind))
3092 if (Present (Full_View (gnat_parent)))
3093 gnat_parent = Full_View (gnat_parent);
3094 else
3095 gnat_parent = Underlying_Full_View (gnat_parent);
3098 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3099 Present (gnat_field);
3100 gnat_field = Next_Stored_Discriminant (gnat_field))
3101 if (Present (Corresponding_Discriminant (gnat_field)))
3103 Entity_Id field = Empty;
3104 for (field = First_Stored_Discriminant (gnat_parent);
3105 Present (field);
3106 field = Next_Stored_Discriminant (field))
3107 if (same_discriminant_p (gnat_field, field))
3108 break;
3109 gcc_assert (Present (field));
3110 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
3111 = gnat_to_gnu_field_decl (field);
3115 /* The "get to the parent" COMPONENT_REF must be given its
3116 proper type... */
3117 TREE_TYPE (gnu_get_parent) = gnu_parent;
3119 /* ...and reference the _Parent field of this record. */
3120 gnu_field
3121 = create_field_decl (parent_name_id,
3122 gnu_parent, gnu_type,
3123 has_rep
3124 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
3125 has_rep
3126 ? bitsize_zero_node : NULL_TREE,
3127 0, 1);
3128 DECL_INTERNAL_P (gnu_field) = 1;
3129 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
3130 TYPE_FIELDS (gnu_type) = gnu_field;
3133 /* Make the fields for the discriminants and put them into the record
3134 unless it's an Unchecked_Union. */
3135 if (has_discr)
3136 for (gnat_field = First_Stored_Discriminant (gnat_entity);
3137 Present (gnat_field);
3138 gnat_field = Next_Stored_Discriminant (gnat_field))
3140 /* If this is a record extension and this discriminant is the
3141 renaming of another discriminant, we've handled it above. */
3142 if (Present (Parent_Subtype (gnat_entity))
3143 && Present (Corresponding_Discriminant (gnat_field)))
3144 continue;
3146 gnu_field
3147 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3148 debug_info_p);
3150 /* Make an expression using a PLACEHOLDER_EXPR from the
3151 FIELD_DECL node just created and link that with the
3152 corresponding GNAT defining identifier. */
3153 save_gnu_tree (gnat_field,
3154 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3155 build0 (PLACEHOLDER_EXPR, gnu_type),
3156 gnu_field, NULL_TREE),
3157 true);
3159 if (!is_unchecked_union)
3161 DECL_CHAIN (gnu_field) = gnu_field_list;
3162 gnu_field_list = gnu_field;
3166 /* If we have a derived untagged type that renames discriminants in
3167 the root type, the (stored) discriminants are a just copy of the
3168 discriminants of the root type. This means that any constraints
3169 added by the renaming in the derivation are disregarded as far
3170 as the layout of the derived type is concerned. To rescue them,
3171 we change the type of the (stored) discriminants to a subtype
3172 with the bounds of the type of the visible discriminants. */
3173 if (has_discr
3174 && !is_extension
3175 && Stored_Constraint (gnat_entity) != No_Elist)
3176 for (gnat_constr = First_Elmt (Stored_Constraint (gnat_entity));
3177 gnat_constr != No_Elmt;
3178 gnat_constr = Next_Elmt (gnat_constr))
3179 if (Nkind (Node (gnat_constr)) == N_Identifier
3180 /* Ignore access discriminants. */
3181 && !Is_Access_Type (Etype (Node (gnat_constr)))
3182 && Ekind (Entity (Node (gnat_constr))) == E_Discriminant)
3184 Entity_Id gnat_discr = Entity (Node (gnat_constr));
3185 tree gnu_discr_type = gnat_to_gnu_type (Etype (gnat_discr));
3186 tree gnu_ref
3187 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr),
3188 NULL_TREE, 0);
3190 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3191 just above for one of the stored discriminants. */
3192 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref, 0)) == gnu_type);
3194 if (gnu_discr_type != TREE_TYPE (gnu_ref))
3196 const unsigned prec = TYPE_PRECISION (TREE_TYPE (gnu_ref));
3197 tree gnu_subtype
3198 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref))
3199 ? make_unsigned_type (prec) : make_signed_type (prec);
3200 TREE_TYPE (gnu_subtype) = TREE_TYPE (gnu_ref);
3201 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
3202 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
3203 TYPE_MIN_VALUE (gnu_discr_type));
3204 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
3205 TYPE_MAX_VALUE (gnu_discr_type));
3206 TREE_TYPE (gnu_ref)
3207 = TREE_TYPE (TREE_OPERAND (gnu_ref, 1)) = gnu_subtype;
3211 /* Add the fields into the record type and finish it up. */
3212 components_to_record (gnu_type, Component_List (record_definition),
3213 gnu_field_list, packed, definition, false,
3214 all_rep, is_unchecked_union,
3215 !Comes_From_Source (gnat_entity), debug_info_p,
3216 false, OK_To_Reorder_Components (gnat_entity),
3217 all_rep ? NULL_TREE : bitsize_zero_node, NULL);
3219 /* If it is passed by reference, force BLKmode to ensure that objects
3220 of this type will always be put in memory. */
3221 if (TYPE_MODE (gnu_type) != BLKmode
3222 && Is_By_Reference_Type (gnat_entity))
3223 SET_TYPE_MODE (gnu_type, BLKmode);
3225 /* We used to remove the associations of the discriminants and _Parent
3226 for validity checking but we may need them if there's a Freeze_Node
3227 for a subtype used in this record. */
3228 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3230 /* Fill in locations of fields. */
3231 annotate_rep (gnat_entity, gnu_type);
3233 /* If there are any entities in the chain corresponding to components
3234 that we did not elaborate, ensure we elaborate their types if they
3235 are Itypes. */
3236 for (gnat_temp = First_Entity (gnat_entity);
3237 Present (gnat_temp);
3238 gnat_temp = Next_Entity (gnat_temp))
3239 if ((Ekind (gnat_temp) == E_Component
3240 || Ekind (gnat_temp) == E_Discriminant)
3241 && Is_Itype (Etype (gnat_temp))
3242 && !present_gnu_tree (gnat_temp))
3243 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3245 /* If this is a record type associated with an exception definition,
3246 equate its fields to those of the standard exception type. This
3247 will make it possible to convert between them. */
3248 if (gnu_entity_name == exception_data_name_id)
3250 tree gnu_std_field;
3251 for (gnu_field = TYPE_FIELDS (gnu_type),
3252 gnu_std_field = TYPE_FIELDS (except_type_node);
3253 gnu_field;
3254 gnu_field = DECL_CHAIN (gnu_field),
3255 gnu_std_field = DECL_CHAIN (gnu_std_field))
3256 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3257 gcc_assert (!gnu_std_field);
3260 break;
3262 case E_Class_Wide_Subtype:
3263 /* If an equivalent type is present, that is what we should use.
3264 Otherwise, fall through to handle this like a record subtype
3265 since it may have constraints. */
3266 if (gnat_equiv_type != gnat_entity)
3268 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3269 maybe_present = true;
3270 break;
3273 /* ... fall through ... */
3275 case E_Record_Subtype:
3276 /* If Cloned_Subtype is Present it means this record subtype has
3277 identical layout to that type or subtype and we should use
3278 that GCC type for this one. The front end guarantees that
3279 the component list is shared. */
3280 if (Present (Cloned_Subtype (gnat_entity)))
3282 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3283 NULL_TREE, 0);
3284 maybe_present = true;
3285 break;
3288 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3289 changing the type, make a new type with each field having the type of
3290 the field in the new subtype but the position computed by transforming
3291 every discriminant reference according to the constraints. We don't
3292 see any difference between private and non-private type here since
3293 derivations from types should have been deferred until the completion
3294 of the private type. */
3295 else
3297 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3298 tree gnu_base_type;
3300 if (!definition)
3302 defer_incomplete_level++;
3303 this_deferred = true;
3306 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3308 if (present_gnu_tree (gnat_entity))
3310 maybe_present = true;
3311 break;
3314 /* If this is a record subtype associated with a dispatch table,
3315 strip the suffix. This is necessary to make sure 2 different
3316 subtypes associated with the imported and exported views of a
3317 dispatch table are properly merged in LTO mode. */
3318 if (Is_Dispatch_Table_Entity (gnat_entity))
3320 char *p;
3321 Get_Encoded_Name (gnat_entity);
3322 p = strchr (Name_Buffer, '_');
3323 gcc_assert (p);
3324 strcpy (p+2, "dtS");
3325 gnu_entity_name = get_identifier (Name_Buffer);
3328 /* When the subtype has discriminants and these discriminants affect
3329 the initial shape it has inherited, factor them in. But for an
3330 Unchecked_Union (it must be an Itype), just return the type.
3331 We can't just test Is_Constrained because private subtypes without
3332 discriminants of types with discriminants with default expressions
3333 are Is_Constrained but aren't constrained! */
3334 if (IN (Ekind (gnat_base_type), Record_Kind)
3335 && !Is_Unchecked_Union (gnat_base_type)
3336 && !Is_For_Access_Subtype (gnat_entity)
3337 && Has_Discriminants (gnat_entity)
3338 && Is_Constrained (gnat_entity)
3339 && Stored_Constraint (gnat_entity) != No_Elist)
3341 vec<subst_pair> gnu_subst_list
3342 = build_subst_list (gnat_entity, gnat_base_type, definition);
3343 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part;
3344 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3345 bool selected_variant = false, all_constant_pos = true;
3346 Entity_Id gnat_field;
3347 vec<variant_desc> gnu_variant_list;
3349 gnu_type = make_node (RECORD_TYPE);
3350 TYPE_NAME (gnu_type) = gnu_entity_name;
3351 TYPE_PACKED (gnu_type) = TYPE_PACKED (gnu_base_type);
3352 process_attributes (&gnu_type, &attr_list, true, gnat_entity);
3354 /* Set the size, alignment and alias set of the new type to
3355 match that of the old one, doing required substitutions. */
3356 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3357 gnu_subst_list);
3359 if (TYPE_IS_PADDING_P (gnu_base_type))
3360 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3361 else
3362 gnu_unpad_base_type = gnu_base_type;
3364 /* Look for REP and variant parts in the base type. */
3365 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3366 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3368 /* If there is a variant part, we must compute whether the
3369 constraints statically select a particular variant. If
3370 so, we simply drop the qualified union and flatten the
3371 list of fields. Otherwise we'll build a new qualified
3372 union for the variants that are still relevant. */
3373 if (gnu_variant_part)
3375 variant_desc *v;
3376 unsigned int i;
3378 gnu_variant_list
3379 = build_variant_list (TREE_TYPE (gnu_variant_part),
3380 gnu_subst_list,
3381 vNULL);
3383 /* If all the qualifiers are unconditionally true, the
3384 innermost variant is statically selected. */
3385 selected_variant = true;
3386 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3387 if (!integer_onep (v->qual))
3389 selected_variant = false;
3390 break;
3393 /* Otherwise, create the new variants. */
3394 if (!selected_variant)
3395 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3397 tree old_variant = v->type;
3398 tree new_variant = make_node (RECORD_TYPE);
3399 tree suffix
3400 = concat_name (DECL_NAME (gnu_variant_part),
3401 IDENTIFIER_POINTER
3402 (DECL_NAME (v->field)));
3403 TYPE_NAME (new_variant)
3404 = concat_name (TYPE_NAME (gnu_type),
3405 IDENTIFIER_POINTER (suffix));
3406 copy_and_substitute_in_size (new_variant, old_variant,
3407 gnu_subst_list);
3408 v->new_type = new_variant;
3411 else
3413 gnu_variant_list.create (0);
3414 selected_variant = false;
3417 /* Make a list of fields and their position in the base type. */
3418 gnu_pos_list
3419 = build_position_list (gnu_unpad_base_type,
3420 gnu_variant_list.exists ()
3421 && !selected_variant,
3422 size_zero_node, bitsize_zero_node,
3423 BIGGEST_ALIGNMENT, NULL_TREE);
3425 /* Now go down every component in the subtype and compute its
3426 size and position from those of the component in the base
3427 type and from the constraints of the subtype. */
3428 for (gnat_field = First_Entity (gnat_entity);
3429 Present (gnat_field);
3430 gnat_field = Next_Entity (gnat_field))
3431 if ((Ekind (gnat_field) == E_Component
3432 || Ekind (gnat_field) == E_Discriminant)
3433 && !(Present (Corresponding_Discriminant (gnat_field))
3434 && Is_Tagged_Type (gnat_base_type))
3435 && Underlying_Type
3436 (Scope (Original_Record_Component (gnat_field)))
3437 == gnat_base_type)
3439 Name_Id gnat_name = Chars (gnat_field);
3440 Entity_Id gnat_old_field
3441 = Original_Record_Component (gnat_field);
3442 tree gnu_old_field
3443 = gnat_to_gnu_field_decl (gnat_old_field);
3444 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3445 tree gnu_field, gnu_field_type, gnu_size, gnu_pos;
3446 tree gnu_cont_type, gnu_last = NULL_TREE;
3448 /* If the type is the same, retrieve the GCC type from the
3449 old field to take into account possible adjustments. */
3450 if (Etype (gnat_field) == Etype (gnat_old_field))
3451 gnu_field_type = TREE_TYPE (gnu_old_field);
3452 else
3453 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3455 /* If there was a component clause, the field types must be
3456 the same for the type and subtype, so copy the data from
3457 the old field to avoid recomputation here. Also if the
3458 field is justified modular and the optimization in
3459 gnat_to_gnu_field was applied. */
3460 if (Present (Component_Clause (gnat_old_field))
3461 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3462 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3463 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3464 == TREE_TYPE (gnu_old_field)))
3466 gnu_size = DECL_SIZE (gnu_old_field);
3467 gnu_field_type = TREE_TYPE (gnu_old_field);
3470 /* If the old field was packed and of constant size, we
3471 have to get the old size here, as it might differ from
3472 what the Etype conveys and the latter might overlap
3473 onto the following field. Try to arrange the type for
3474 possible better packing along the way. */
3475 else if (DECL_PACKED (gnu_old_field)
3476 && TREE_CODE (DECL_SIZE (gnu_old_field))
3477 == INTEGER_CST)
3479 gnu_size = DECL_SIZE (gnu_old_field);
3480 if (RECORD_OR_UNION_TYPE_P (gnu_field_type)
3481 && !TYPE_FAT_POINTER_P (gnu_field_type)
3482 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type)))
3483 gnu_field_type
3484 = make_packable_type (gnu_field_type, true);
3487 else
3488 gnu_size = TYPE_SIZE (gnu_field_type);
3490 /* If the context of the old field is the base type or its
3491 REP part (if any), put the field directly in the new
3492 type; otherwise look up the context in the variant list
3493 and put the field either in the new type if there is a
3494 selected variant or in one of the new variants. */
3495 if (gnu_context == gnu_unpad_base_type
3496 || (gnu_rep_part
3497 && gnu_context == TREE_TYPE (gnu_rep_part)))
3498 gnu_cont_type = gnu_type;
3499 else
3501 variant_desc *v;
3502 unsigned int i;
3503 tree rep_part;
3505 FOR_EACH_VEC_ELT (gnu_variant_list, i, v)
3506 if (gnu_context == v->type
3507 || ((rep_part = get_rep_part (v->type))
3508 && gnu_context == TREE_TYPE (rep_part)))
3509 break;
3510 if (v)
3512 if (selected_variant)
3513 gnu_cont_type = gnu_type;
3514 else
3515 gnu_cont_type = v->new_type;
3517 else
3518 /* The front-end may pass us "ghost" components if
3519 it fails to recognize that a constrained subtype
3520 is statically constrained. Discard them. */
3521 continue;
3524 /* Now create the new field modeled on the old one. */
3525 gnu_field
3526 = create_field_decl_from (gnu_old_field, gnu_field_type,
3527 gnu_cont_type, gnu_size,
3528 gnu_pos_list, gnu_subst_list);
3529 gnu_pos = DECL_FIELD_OFFSET (gnu_field);
3531 /* Put it in one of the new variants directly. */
3532 if (gnu_cont_type != gnu_type)
3534 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3535 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3538 /* To match the layout crafted in components_to_record,
3539 if this is the _Tag or _Parent field, put it before
3540 any other fields. */
3541 else if (gnat_name == Name_uTag
3542 || gnat_name == Name_uParent)
3543 gnu_field_list = chainon (gnu_field_list, gnu_field);
3545 /* Similarly, if this is the _Controller field, put
3546 it before the other fields except for the _Tag or
3547 _Parent field. */
3548 else if (gnat_name == Name_uController && gnu_last)
3550 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3551 DECL_CHAIN (gnu_last) = gnu_field;
3554 /* Otherwise, if this is a regular field, put it after
3555 the other fields. */
3556 else
3558 DECL_CHAIN (gnu_field) = gnu_field_list;
3559 gnu_field_list = gnu_field;
3560 if (!gnu_last)
3561 gnu_last = gnu_field;
3562 if (TREE_CODE (gnu_pos) != INTEGER_CST)
3563 all_constant_pos = false;
3566 save_gnu_tree (gnat_field, gnu_field, false);
3569 /* If there is a variant list, a selected variant and the fields
3570 all have a constant position, put them in order of increasing
3571 position to match that of constant CONSTRUCTORs. Likewise if
3572 there is no variant list but a REP part, since the latter has
3573 been flattened in the process. */
3574 if (((gnu_variant_list.exists () && selected_variant)
3575 || (!gnu_variant_list.exists () && gnu_rep_part))
3576 && all_constant_pos)
3578 const int len = list_length (gnu_field_list);
3579 tree *field_arr = XALLOCAVEC (tree, len), t;
3580 int i;
3582 for (t = gnu_field_list, i = 0; t; t = DECL_CHAIN (t), i++)
3583 field_arr[i] = t;
3585 qsort (field_arr, len, sizeof (tree), compare_field_bitpos);
3587 gnu_field_list = NULL_TREE;
3588 for (i = 0; i < len; i++)
3590 DECL_CHAIN (field_arr[i]) = gnu_field_list;
3591 gnu_field_list = field_arr[i];
3595 /* If there is a variant list and no selected variant, we need
3596 to create the nest of variant parts from the old nest. */
3597 else if (gnu_variant_list.exists () && !selected_variant)
3599 tree new_variant_part
3600 = create_variant_part_from (gnu_variant_part,
3601 gnu_variant_list, gnu_type,
3602 gnu_pos_list, gnu_subst_list);
3603 DECL_CHAIN (new_variant_part) = gnu_field_list;
3604 gnu_field_list = new_variant_part;
3607 /* Now go through the entities again looking for Itypes that
3608 we have not elaborated but should (e.g., Etypes of fields
3609 that have Original_Components). */
3610 for (gnat_field = First_Entity (gnat_entity);
3611 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3612 if ((Ekind (gnat_field) == E_Discriminant
3613 || Ekind (gnat_field) == E_Component)
3614 && !present_gnu_tree (Etype (gnat_field)))
3615 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3617 /* Do not emit debug info for the type yet since we're going to
3618 modify it below. */
3619 finish_record_type (gnu_type, nreverse (gnu_field_list), 2,
3620 false);
3621 compute_record_mode (gnu_type);
3623 /* See the E_Record_Type case for the rationale. */
3624 if (TYPE_MODE (gnu_type) != BLKmode
3625 && Is_By_Reference_Type (gnat_entity))
3626 SET_TYPE_MODE (gnu_type, BLKmode);
3628 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3630 /* Fill in locations of fields. */
3631 annotate_rep (gnat_entity, gnu_type);
3633 /* If debugging information is being written for the type, write
3634 a record that shows what we are a subtype of and also make a
3635 variable that indicates our size, if still variable. */
3636 if (debug_info_p)
3638 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3639 tree gnu_unpad_base_name
3640 = TYPE_IDENTIFIER (gnu_unpad_base_type);
3641 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3643 TYPE_NAME (gnu_subtype_marker)
3644 = create_concat_name (gnat_entity, "XVS");
3645 finish_record_type (gnu_subtype_marker,
3646 create_field_decl (gnu_unpad_base_name,
3647 build_reference_type
3648 (gnu_unpad_base_type),
3649 gnu_subtype_marker,
3650 NULL_TREE, NULL_TREE,
3651 0, 0),
3652 0, true);
3654 add_parallel_type (gnu_type, gnu_subtype_marker);
3656 if (definition
3657 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3658 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3659 TYPE_SIZE_UNIT (gnu_subtype_marker)
3660 = create_var_decl (create_concat_name (gnat_entity,
3661 "XVZ"),
3662 NULL_TREE, sizetype, gnu_size_unit,
3663 false, false, false, false, NULL,
3664 gnat_entity);
3667 gnu_variant_list.release ();
3668 gnu_subst_list.release ();
3670 /* Now we can finalize it. */
3671 rest_of_record_type_compilation (gnu_type);
3674 /* Otherwise, go down all the components in the new type and make
3675 them equivalent to those in the base type. */
3676 else
3678 gnu_type = gnu_base_type;
3680 for (gnat_temp = First_Entity (gnat_entity);
3681 Present (gnat_temp);
3682 gnat_temp = Next_Entity (gnat_temp))
3683 if ((Ekind (gnat_temp) == E_Discriminant
3684 && !Is_Unchecked_Union (gnat_base_type))
3685 || Ekind (gnat_temp) == E_Component)
3686 save_gnu_tree (gnat_temp,
3687 gnat_to_gnu_field_decl
3688 (Original_Record_Component (gnat_temp)),
3689 false);
3692 break;
3694 case E_Access_Subprogram_Type:
3695 /* Use the special descriptor type for dispatch tables if needed,
3696 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3697 Note that we are only required to do so for static tables in
3698 order to be compatible with the C++ ABI, but Ada 2005 allows
3699 to extend library level tagged types at the local level so
3700 we do it in the non-static case as well. */
3701 if (TARGET_VTABLE_USES_DESCRIPTORS
3702 && Is_Dispatch_Table_Entity (gnat_entity))
3704 gnu_type = fdesc_type_node;
3705 gnu_size = TYPE_SIZE (gnu_type);
3706 break;
3709 /* ... fall through ... */
3711 case E_Anonymous_Access_Subprogram_Type:
3712 /* If we are not defining this entity, and we have incomplete
3713 entities being processed above us, make a dummy type and
3714 fill it in later. */
3715 if (!definition && defer_incomplete_level != 0)
3717 struct incomplete *p = XNEW (struct incomplete);
3719 gnu_type
3720 = build_pointer_type
3721 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3722 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3723 !Comes_From_Source (gnat_entity),
3724 debug_info_p, gnat_entity);
3725 this_made_decl = true;
3726 gnu_type = TREE_TYPE (gnu_decl);
3727 save_gnu_tree (gnat_entity, gnu_decl, false);
3728 saved = true;
3730 p->old_type = TREE_TYPE (gnu_type);
3731 p->full_type = Directly_Designated_Type (gnat_entity);
3732 p->next = defer_incomplete_list;
3733 defer_incomplete_list = p;
3734 break;
3737 /* ... fall through ... */
3739 case E_Allocator_Type:
3740 case E_Access_Type:
3741 case E_Access_Attribute_Type:
3742 case E_Anonymous_Access_Type:
3743 case E_General_Access_Type:
3745 /* The designated type and its equivalent type for gigi. */
3746 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3747 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3748 /* Whether it comes from a limited with. */
3749 bool is_from_limited_with
3750 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3751 && From_Limited_With (gnat_desig_equiv));
3752 /* The "full view" of the designated type. If this is an incomplete
3753 entity from a limited with, treat its non-limited view as the full
3754 view. Otherwise, if this is an incomplete or private type, use the
3755 full view. In the former case, we might point to a private type,
3756 in which case, we need its full view. Also, we want to look at the
3757 actual type used for the representation, so this takes a total of
3758 three steps. */
3759 Entity_Id gnat_desig_full_direct_first
3760 = (is_from_limited_with
3761 ? Non_Limited_View (gnat_desig_equiv)
3762 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3763 ? Full_View (gnat_desig_equiv) : Empty));
3764 Entity_Id gnat_desig_full_direct
3765 = ((is_from_limited_with
3766 && Present (gnat_desig_full_direct_first)
3767 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3768 ? Full_View (gnat_desig_full_direct_first)
3769 : gnat_desig_full_direct_first);
3770 Entity_Id gnat_desig_full
3771 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3772 /* The type actually used to represent the designated type, either
3773 gnat_desig_full or gnat_desig_equiv. */
3774 Entity_Id gnat_desig_rep;
3775 /* True if this is a pointer to an unconstrained array. */
3776 bool is_unconstrained_array;
3777 /* We want to know if we'll be seeing the freeze node for any
3778 incomplete type we may be pointing to. */
3779 bool in_main_unit
3780 = (Present (gnat_desig_full)
3781 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3782 : In_Extended_Main_Code_Unit (gnat_desig_type));
3783 /* True if we make a dummy type here. */
3784 bool made_dummy = false;
3785 /* The mode to be used for the pointer type. */
3786 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3787 /* The GCC type used for the designated type. */
3788 tree gnu_desig_type = NULL_TREE;
3790 if (!targetm.valid_pointer_mode (p_mode))
3791 p_mode = ptr_mode;
3793 /* If either the designated type or its full view is an unconstrained
3794 array subtype, replace it with the type it's a subtype of. This
3795 avoids problems with multiple copies of unconstrained array types.
3796 Likewise, if the designated type is a subtype of an incomplete
3797 record type, use the parent type to avoid order of elaboration
3798 issues. This can lose some code efficiency, but there is no
3799 alternative. */
3800 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3801 && !Is_Constrained (gnat_desig_equiv))
3802 gnat_desig_equiv = Etype (gnat_desig_equiv);
3803 if (Present (gnat_desig_full)
3804 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3805 && !Is_Constrained (gnat_desig_full))
3806 || (Ekind (gnat_desig_full) == E_Record_Subtype
3807 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3808 gnat_desig_full = Etype (gnat_desig_full);
3810 /* Set the type that's actually the representation of the designated
3811 type and also flag whether we have a unconstrained array. */
3812 gnat_desig_rep
3813 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3814 is_unconstrained_array
3815 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3817 /* If we are pointing to an incomplete type whose completion is an
3818 unconstrained array, make dummy fat and thin pointer types to it.
3819 Likewise if the type itself is dummy or an unconstrained array. */
3820 if (is_unconstrained_array
3821 && (Present (gnat_desig_full)
3822 || (present_gnu_tree (gnat_desig_equiv)
3823 && TYPE_IS_DUMMY_P
3824 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3825 || (!in_main_unit
3826 && defer_incomplete_level != 0
3827 && !present_gnu_tree (gnat_desig_equiv))
3828 || (in_main_unit
3829 && is_from_limited_with
3830 && Present (Freeze_Node (gnat_desig_equiv)))))
3832 if (present_gnu_tree (gnat_desig_rep))
3833 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3834 else
3836 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3837 made_dummy = true;
3840 /* If the call above got something that has a pointer, the pointer
3841 is our type. This could have happened either because the type
3842 was elaborated or because somebody else executed the code. */
3843 if (!TYPE_POINTER_TO (gnu_desig_type))
3844 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3845 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3848 /* If we already know what the full type is, use it. */
3849 else if (Present (gnat_desig_full)
3850 && present_gnu_tree (gnat_desig_full))
3851 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3853 /* Get the type of the thing we are to point to and build a pointer to
3854 it. If it is a reference to an incomplete or private type with a
3855 full view that is a record, make a dummy type node and get the
3856 actual type later when we have verified it is safe. */
3857 else if ((!in_main_unit
3858 && !present_gnu_tree (gnat_desig_equiv)
3859 && Present (gnat_desig_full)
3860 && !present_gnu_tree (gnat_desig_full)
3861 && Is_Record_Type (gnat_desig_full))
3862 /* Likewise if we are pointing to a record or array and we are
3863 to defer elaborating incomplete types. We do this as this
3864 access type may be the full view of a private type. Note
3865 that the unconstrained array case is handled above. */
3866 || ((!in_main_unit || imported_p)
3867 && defer_incomplete_level != 0
3868 && !present_gnu_tree (gnat_desig_equiv)
3869 && (Is_Record_Type (gnat_desig_rep)
3870 || Is_Array_Type (gnat_desig_rep)))
3871 /* If this is a reference from a limited_with type back to our
3872 main unit and there's a freeze node for it, either we have
3873 already processed the declaration and made the dummy type,
3874 in which case we just reuse the latter, or we have not yet,
3875 in which case we make the dummy type and it will be reused
3876 when the declaration is finally processed. In both cases,
3877 the pointer eventually created below will be automatically
3878 adjusted when the freeze node is processed. Note that the
3879 unconstrained array case is handled above. */
3880 || (in_main_unit
3881 && is_from_limited_with
3882 && Present (Freeze_Node (gnat_desig_rep))))
3884 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3885 made_dummy = true;
3888 /* Otherwise handle the case of a pointer to itself. */
3889 else if (gnat_desig_equiv == gnat_entity)
3891 gnu_type
3892 = build_pointer_type_for_mode (void_type_node, p_mode,
3893 No_Strict_Aliasing (gnat_entity));
3894 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3897 /* If expansion is disabled, the equivalent type of a concurrent type
3898 is absent, so build a dummy pointer type. */
3899 else if (type_annotate_only && No (gnat_desig_equiv))
3900 gnu_type = ptr_void_type_node;
3902 /* Finally, handle the default case where we can just elaborate our
3903 designated type. */
3904 else
3905 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3907 /* It is possible that a call to gnat_to_gnu_type above resolved our
3908 type. If so, just return it. */
3909 if (present_gnu_tree (gnat_entity))
3911 maybe_present = true;
3912 break;
3915 /* If we haven't done it yet, build the pointer type the usual way. */
3916 if (!gnu_type)
3918 /* Modify the designated type if we are pointing only to constant
3919 objects, but don't do it for unconstrained arrays. */
3920 if (Is_Access_Constant (gnat_entity)
3921 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3923 gnu_desig_type
3924 = build_qualified_type
3925 (gnu_desig_type,
3926 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3928 /* Some extra processing is required if we are building a
3929 pointer to an incomplete type (in the GCC sense). We might
3930 have such a type if we just made a dummy, or directly out
3931 of the call to gnat_to_gnu_type above if we are processing
3932 an access type for a record component designating the
3933 record type itself. */
3934 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3936 /* We must ensure that the pointer to variant we make will
3937 be processed by update_pointer_to when the initial type
3938 is completed. Pretend we made a dummy and let further
3939 processing act as usual. */
3940 made_dummy = true;
3942 /* We must ensure that update_pointer_to will not retrieve
3943 the dummy variant when building a properly qualified
3944 version of the complete type. We take advantage of the
3945 fact that get_qualified_type is requiring TYPE_NAMEs to
3946 match to influence build_qualified_type and then also
3947 update_pointer_to here. */
3948 TYPE_NAME (gnu_desig_type)
3949 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3953 gnu_type
3954 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3955 No_Strict_Aliasing (gnat_entity));
3958 /* If we are not defining this object and we have made a dummy pointer,
3959 save our current definition, evaluate the actual type, and replace
3960 the tentative type we made with the actual one. If we are to defer
3961 actually looking up the actual type, make an entry in the deferred
3962 list. If this is from a limited with, we may have to defer to the
3963 end of the current unit. */
3964 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3966 tree gnu_old_desig_type;
3968 if (TYPE_IS_FAT_POINTER_P (gnu_type))
3970 gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
3971 if (esize == POINTER_SIZE)
3972 gnu_type = build_pointer_type
3973 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
3975 else
3976 gnu_old_desig_type = TREE_TYPE (gnu_type);
3978 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
3979 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
3980 !Comes_From_Source (gnat_entity),
3981 debug_info_p, gnat_entity);
3982 this_made_decl = true;
3983 gnu_type = TREE_TYPE (gnu_decl);
3984 save_gnu_tree (gnat_entity, gnu_decl, false);
3985 saved = true;
3987 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3988 update gnu_old_desig_type directly, in which case it will not be
3989 a dummy type any more when we get into update_pointer_to.
3991 This can happen e.g. when the designated type is a record type,
3992 because their elaboration starts with an initial node from
3993 make_dummy_type, which may be the same node as the one we got.
3995 Besides, variants of this non-dummy type might have been created
3996 along the way. update_pointer_to is expected to properly take
3997 care of those situations. */
3998 if (defer_incomplete_level == 0 && !is_from_limited_with)
4000 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
4001 gnat_to_gnu_type (gnat_desig_equiv));
4003 else
4005 struct incomplete *p = XNEW (struct incomplete);
4006 struct incomplete **head
4007 = (is_from_limited_with
4008 ? &defer_limited_with : &defer_incomplete_list);
4009 p->old_type = gnu_old_desig_type;
4010 p->full_type = gnat_desig_equiv;
4011 p->next = *head;
4012 *head = p;
4016 break;
4018 case E_Access_Protected_Subprogram_Type:
4019 case E_Anonymous_Access_Protected_Subprogram_Type:
4020 if (type_annotate_only && No (gnat_equiv_type))
4021 gnu_type = ptr_void_type_node;
4022 else
4024 /* The run-time representation is the equivalent type. */
4025 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4026 maybe_present = true;
4029 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4030 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4031 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
4032 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
4033 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4034 NULL_TREE, 0);
4036 break;
4038 case E_Access_Subtype:
4040 /* We treat this as identical to its base type; any constraint is
4041 meaningful only to the front-end.
4043 The designated type must be elaborated as well, if it does
4044 not have its own freeze node. Designated (sub)types created
4045 for constrained components of records with discriminants are
4046 not frozen by the front-end and thus not elaborated by gigi,
4047 because their use may appear before the base type is frozen,
4048 and because it is not clear that they are needed anywhere in
4049 gigi. With the current model, there is no correct place where
4050 they could be elaborated. */
4052 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
4053 if (Is_Itype (Directly_Designated_Type (gnat_entity))
4054 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
4055 && Is_Frozen (Directly_Designated_Type (gnat_entity))
4056 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
4058 /* If we are not defining this entity, and we have incomplete
4059 entities being processed above us, make a dummy type and
4060 elaborate it later. */
4061 if (!definition && defer_incomplete_level != 0)
4063 struct incomplete *p = XNEW (struct incomplete);
4065 p->old_type
4066 = make_dummy_type (Directly_Designated_Type (gnat_entity));
4067 p->full_type = Directly_Designated_Type (gnat_entity);
4068 p->next = defer_incomplete_list;
4069 defer_incomplete_list = p;
4071 else if (!IN (Ekind (Base_Type
4072 (Directly_Designated_Type (gnat_entity))),
4073 Incomplete_Or_Private_Kind))
4074 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
4075 NULL_TREE, 0);
4078 maybe_present = true;
4079 break;
4081 /* Subprogram Entities
4083 The following access functions are defined for subprograms:
4085 Etype Return type or Standard_Void_Type.
4086 First_Formal The first formal parameter.
4087 Is_Imported Indicates that the subprogram has appeared in
4088 an INTERFACE or IMPORT pragma. For now we
4089 assume that the external language is C.
4090 Is_Exported Likewise but for an EXPORT pragma.
4091 Is_Inlined True if the subprogram is to be inlined.
4093 Each parameter is first checked by calling must_pass_by_ref on its
4094 type to determine if it is passed by reference. For parameters which
4095 are copied in, if they are Ada In Out or Out parameters, their return
4096 value becomes part of a record which becomes the return type of the
4097 function (C function - note that this applies only to Ada procedures
4098 so there is no Ada return type). Additional code to store back the
4099 parameters will be generated on the caller side. This transformation
4100 is done here, not in the front-end.
4102 The intended result of the transformation can be seen from the
4103 equivalent source rewritings that follow:
4105 struct temp {int a,b};
4106 procedure P (A,B: In Out ...) is temp P (int A,B)
4107 begin {
4108 .. ..
4109 end P; return {A,B};
4112 temp t;
4113 P(X,Y); t = P(X,Y);
4114 X = t.a , Y = t.b;
4116 For subprogram types we need to perform mainly the same conversions to
4117 GCC form that are needed for procedures and function declarations. The
4118 only difference is that at the end, we make a type declaration instead
4119 of a function declaration. */
4121 case E_Subprogram_Type:
4122 case E_Function:
4123 case E_Procedure:
4125 /* The type returned by a function or else Standard_Void_Type for a
4126 procedure. */
4127 Entity_Id gnat_return_type = Etype (gnat_entity);
4128 tree gnu_return_type;
4129 /* The first GCC parameter declaration (a PARM_DECL node). The
4130 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4131 actually is the head of this parameter list. */
4132 tree gnu_param_list = NULL_TREE;
4133 /* Likewise for the stub associated with an exported procedure. */
4134 tree gnu_stub_param_list = NULL_TREE;
4135 /* Non-null for subprograms containing parameters passed by copy-in
4136 copy-out (Ada In Out or Out parameters not passed by reference),
4137 in which case it is the list of nodes used to specify the values
4138 of the In Out/Out parameters that are returned as a record upon
4139 procedure return. The TREE_PURPOSE of an element of this list is
4140 a field of the record and the TREE_VALUE is the PARM_DECL
4141 corresponding to that field. This list will be saved in the
4142 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4143 tree gnu_cico_list = NULL_TREE;
4144 /* List of fields in return type of procedure with copy-in copy-out
4145 parameters. */
4146 tree gnu_field_list = NULL_TREE;
4147 /* If an import pragma asks to map this subprogram to a GCC builtin,
4148 this is the builtin DECL node. */
4149 tree gnu_builtin_decl = NULL_TREE;
4150 /* For the stub associated with an exported procedure. */
4151 tree gnu_stub_type = NULL_TREE, gnu_stub_name = NULL_TREE;
4152 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
4153 Entity_Id gnat_param;
4154 enum inline_status_t inline_status
4155 = Has_Pragma_No_Inline (gnat_entity)
4156 ? is_suppressed
4157 : (Is_Inlined (gnat_entity) ? is_enabled : is_disabled);
4158 bool public_flag = Is_Public (gnat_entity) || imported_p;
4159 bool extern_flag
4160 = (Is_Public (gnat_entity) && !definition) || imported_p;
4161 bool artificial_flag = !Comes_From_Source (gnat_entity);
4162 /* The semantics of "pure" in Ada essentially matches that of "const"
4163 in the back-end. In particular, both properties are orthogonal to
4164 the "nothrow" property if the EH circuitry is explicit in the
4165 internal representation of the back-end. If we are to completely
4166 hide the EH circuitry from it, we need to declare that calls to pure
4167 Ada subprograms that can throw have side effects since they can
4168 trigger an "abnormal" transfer of control flow; thus they can be
4169 neither "const" nor "pure" in the back-end sense. */
4170 bool const_flag
4171 = (Exception_Mechanism == Back_End_Exceptions
4172 && Is_Pure (gnat_entity));
4173 bool volatile_flag = No_Return (gnat_entity);
4174 bool return_by_direct_ref_p = false;
4175 bool return_by_invisi_ref_p = false;
4176 bool return_unconstrained_p = false;
4177 bool has_stub = false;
4178 int parmnum;
4180 /* A parameter may refer to this type, so defer completion of any
4181 incomplete types. */
4182 if (kind == E_Subprogram_Type && !definition)
4184 defer_incomplete_level++;
4185 this_deferred = true;
4188 /* If the subprogram has an alias, it is probably inherited, so
4189 we can use the original one. If the original "subprogram"
4190 is actually an enumeration literal, it may be the first use
4191 of its type, so we must elaborate that type now. */
4192 if (Present (Alias (gnat_entity)))
4194 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
4195 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
4197 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
4199 /* Elaborate any Itypes in the parameters of this entity. */
4200 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
4201 Present (gnat_temp);
4202 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4203 if (Is_Itype (Etype (gnat_temp)))
4204 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4206 break;
4209 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4210 corresponding DECL node. Proper generation of calls later on need
4211 proper parameter associations so we don't "break;" here. */
4212 if (Convention (gnat_entity) == Convention_Intrinsic
4213 && Present (Interface_Name (gnat_entity)))
4215 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
4217 /* Inability to find the builtin decl most often indicates a
4218 genuine mistake, but imports of unregistered intrinsics are
4219 sometimes issued on purpose to allow hooking in alternate
4220 bodies. We post a warning conditioned on Wshadow in this case,
4221 to let developers be notified on demand without risking false
4222 positives with common default sets of options. */
4224 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
4225 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4228 /* ??? What if we don't find the builtin node above ? warn ? err ?
4229 In the current state we neither warn nor err, and calls will just
4230 be handled as for regular subprograms. */
4232 /* Look into the return type and get its associated GCC tree. If it
4233 is not void, compute various flags for the subprogram type. */
4234 if (Ekind (gnat_return_type) == E_Void)
4235 gnu_return_type = void_type_node;
4236 else
4238 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4239 context may now appear in parameter and result profiles. If
4240 we are only annotating types, break circularities here. */
4241 if (type_annotate_only
4242 && IN (Ekind (gnat_return_type), Incomplete_Kind)
4243 && From_Limited_With (gnat_return_type)
4244 && In_Extended_Main_Code_Unit
4245 (Non_Limited_View (gnat_return_type))
4246 && !present_gnu_tree (Non_Limited_View (gnat_return_type)))
4247 gnu_return_type = ptr_void_type_node;
4248 else
4249 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4251 /* If this function returns by reference, make the actual return
4252 type the pointer type and make a note of that. */
4253 if (Returns_By_Ref (gnat_entity))
4255 gnu_return_type = build_pointer_type (gnu_return_type);
4256 return_by_direct_ref_p = true;
4259 /* If we are supposed to return an unconstrained array type, make
4260 the actual return type the fat pointer type. */
4261 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4263 gnu_return_type = TREE_TYPE (gnu_return_type);
4264 return_unconstrained_p = true;
4267 /* Likewise, if the return type requires a transient scope, the
4268 return value will be allocated on the secondary stack so the
4269 actual return type is the pointer type. */
4270 else if (Requires_Transient_Scope (gnat_return_type))
4272 gnu_return_type = build_pointer_type (gnu_return_type);
4273 return_unconstrained_p = true;
4276 /* If the Mechanism is By_Reference, ensure this function uses the
4277 target's by-invisible-reference mechanism, which may not be the
4278 same as above (e.g. it might be passing an extra parameter). */
4279 else if (kind == E_Function
4280 && Mechanism (gnat_entity) == By_Reference)
4281 return_by_invisi_ref_p = true;
4283 /* Likewise, if the return type is itself By_Reference. */
4284 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type))
4285 return_by_invisi_ref_p = true;
4287 /* If the type is a padded type and the underlying type would not
4288 be passed by reference or the function has a foreign convention,
4289 return the underlying type. */
4290 else if (TYPE_IS_PADDING_P (gnu_return_type)
4291 && (!default_pass_by_ref
4292 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4293 || Has_Foreign_Convention (gnat_entity)))
4294 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4296 /* If the return type is unconstrained, that means it must have a
4297 maximum size. Use the padded type as the effective return type.
4298 And ensure the function uses the target's by-invisible-reference
4299 mechanism to avoid copying too much data when it returns. */
4300 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4302 tree orig_type = gnu_return_type;
4304 gnu_return_type
4305 = maybe_pad_type (gnu_return_type,
4306 max_size (TYPE_SIZE (gnu_return_type),
4307 true),
4308 0, gnat_entity, false, false, false, true);
4310 /* Declare it now since it will never be declared otherwise.
4311 This is necessary to ensure that its subtrees are properly
4312 marked. */
4313 if (gnu_return_type != orig_type
4314 && !DECL_P (TYPE_NAME (gnu_return_type)))
4315 create_type_decl (TYPE_NAME (gnu_return_type),
4316 gnu_return_type, true, debug_info_p,
4317 gnat_entity);
4319 return_by_invisi_ref_p = true;
4322 /* If the return type has a size that overflows, we cannot have
4323 a function that returns that type. This usage doesn't make
4324 sense anyway, so give an error here. */
4325 if (TYPE_SIZE_UNIT (gnu_return_type)
4326 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type)) == INTEGER_CST
4327 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type)))
4329 post_error ("cannot return type whose size overflows",
4330 gnat_entity);
4331 gnu_return_type = copy_node (gnu_return_type);
4332 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4333 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4334 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4335 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4339 /* Loop over the parameters and get their associated GCC tree. While
4340 doing this, build a copy-in copy-out structure if we need one. */
4341 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4342 Present (gnat_param);
4343 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4345 Entity_Id gnat_param_type = Etype (gnat_param);
4346 tree gnu_param_name = get_entity_name (gnat_param);
4347 tree gnu_param_type, gnu_param, gnu_field;
4348 Mechanism_Type mech = Mechanism (gnat_param);
4349 bool copy_in_copy_out = false, fake_param_type;
4351 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4352 context may now appear in parameter and result profiles. If
4353 we are only annotating types, break circularities here. */
4354 if (type_annotate_only
4355 && IN (Ekind (gnat_param_type), Incomplete_Kind)
4356 && From_Limited_With (Etype (gnat_param_type))
4357 && In_Extended_Main_Code_Unit
4358 (Non_Limited_View (gnat_param_type))
4359 && !present_gnu_tree (Non_Limited_View (gnat_param_type)))
4361 gnu_param_type = ptr_void_type_node;
4362 fake_param_type = true;
4364 else
4366 gnu_param_type = gnat_to_gnu_type (gnat_param_type);
4367 fake_param_type = false;
4370 /* Builtins are expanded inline and there is no real call sequence
4371 involved. So the type expected by the underlying expander is
4372 always the type of each argument "as is". */
4373 if (gnu_builtin_decl)
4374 mech = By_Copy;
4375 /* Handle the first parameter of a valued procedure specially. */
4376 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4377 mech = By_Copy_Return;
4378 /* Otherwise, see if a Mechanism was supplied that forced this
4379 parameter to be passed one way or another. */
4380 else if (mech == Default
4381 || mech == By_Copy || mech == By_Reference)
4383 else if (By_Descriptor_Last <= mech && mech <= By_Descriptor)
4384 mech = By_Descriptor;
4386 else if (By_Short_Descriptor_Last <= mech &&
4387 mech <= By_Short_Descriptor)
4388 mech = By_Short_Descriptor;
4390 else if (mech > 0)
4392 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4393 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4394 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4395 mech))
4396 mech = By_Reference;
4397 else
4398 mech = By_Copy;
4400 else
4402 post_error ("unsupported mechanism for&", gnat_param);
4403 mech = Default;
4406 /* Do not call gnat_to_gnu_param for a fake parameter type since
4407 it will try to use the real type again. */
4408 if (fake_param_type)
4410 if (Ekind (gnat_param) == E_Out_Parameter)
4411 gnu_param = NULL_TREE;
4412 else
4414 gnu_param
4415 = create_param_decl (gnu_param_name, gnu_param_type,
4416 false);
4417 Set_Mechanism (gnat_param,
4418 mech == Default ? By_Copy : mech);
4419 if (Ekind (gnat_param) == E_In_Out_Parameter)
4420 copy_in_copy_out = true;
4423 else
4424 gnu_param
4425 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4426 Has_Foreign_Convention (gnat_entity),
4427 &copy_in_copy_out);
4429 /* We are returned either a PARM_DECL or a type if no parameter
4430 needs to be passed; in either case, adjust the type. */
4431 if (DECL_P (gnu_param))
4432 gnu_param_type = TREE_TYPE (gnu_param);
4433 else
4435 gnu_param_type = gnu_param;
4436 gnu_param = NULL_TREE;
4439 /* The failure of this assertion will very likely come from an
4440 order of elaboration issue for the type of the parameter. */
4441 gcc_assert (kind == E_Subprogram_Type
4442 || !TYPE_IS_DUMMY_P (gnu_param_type)
4443 || type_annotate_only);
4445 if (gnu_param)
4447 /* If it's an exported subprogram, we build a parameter list
4448 in parallel, in case we need to emit a stub for it. */
4449 if (Is_Exported (gnat_entity))
4451 gnu_stub_param_list
4452 = chainon (gnu_param, gnu_stub_param_list);
4453 /* Change By_Descriptor parameter to By_Reference for
4454 the internal version of an exported subprogram. */
4455 if (mech == By_Descriptor || mech == By_Short_Descriptor)
4457 gnu_param
4458 = gnat_to_gnu_param (gnat_param, By_Reference,
4459 gnat_entity, false,
4460 &copy_in_copy_out);
4461 has_stub = true;
4463 else
4464 gnu_param = copy_node (gnu_param);
4467 gnu_param_list = chainon (gnu_param, gnu_param_list);
4468 Sloc_to_locus (Sloc (gnat_param),
4469 &DECL_SOURCE_LOCATION (gnu_param));
4470 save_gnu_tree (gnat_param, gnu_param, false);
4472 /* If a parameter is a pointer, this function may modify
4473 memory through it and thus shouldn't be considered
4474 a const function. Also, the memory may be modified
4475 between two calls, so they can't be CSE'ed. The latter
4476 case also handles by-ref parameters. */
4477 if (POINTER_TYPE_P (gnu_param_type)
4478 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4479 const_flag = false;
4482 if (copy_in_copy_out)
4484 if (!gnu_cico_list)
4486 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4488 /* If this is a function, we also need a field for the
4489 return value to be placed. */
4490 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4492 gnu_field
4493 = create_field_decl (get_identifier ("RETVAL"),
4494 gnu_return_type,
4495 gnu_new_ret_type, NULL_TREE,
4496 NULL_TREE, 0, 0);
4497 Sloc_to_locus (Sloc (gnat_entity),
4498 &DECL_SOURCE_LOCATION (gnu_field));
4499 gnu_field_list = gnu_field;
4500 gnu_cico_list
4501 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4504 gnu_return_type = gnu_new_ret_type;
4505 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4506 /* Set a default alignment to speed up accesses. But we
4507 shouldn't increase the size of the structure too much,
4508 lest it doesn't fit in return registers anymore. */
4509 TYPE_ALIGN (gnu_return_type)
4510 = get_mode_alignment (ptr_mode);
4513 gnu_field
4514 = create_field_decl (gnu_param_name, gnu_param_type,
4515 gnu_return_type, NULL_TREE, NULL_TREE,
4516 0, 0);
4517 Sloc_to_locus (Sloc (gnat_param),
4518 &DECL_SOURCE_LOCATION (gnu_field));
4519 DECL_CHAIN (gnu_field) = gnu_field_list;
4520 gnu_field_list = gnu_field;
4521 gnu_cico_list
4522 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4526 if (gnu_cico_list)
4528 /* If we have a CICO list but it has only one entry, we convert
4529 this function into a function that returns this object. */
4530 if (list_length (gnu_cico_list) == 1)
4531 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4533 /* Do not finalize the return type if the subprogram is stubbed
4534 since structures are incomplete for the back-end. */
4535 else if (Convention (gnat_entity) != Convention_Stubbed)
4537 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4538 0, false);
4540 /* Try to promote the mode of the return type if it is passed
4541 in registers, again to speed up accesses. */
4542 if (TYPE_MODE (gnu_return_type) == BLKmode
4543 && !targetm.calls.return_in_memory (gnu_return_type,
4544 NULL_TREE))
4546 unsigned int size
4547 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type));
4548 unsigned int i = BITS_PER_UNIT;
4549 enum machine_mode mode;
4551 while (i < size)
4552 i <<= 1;
4553 mode = mode_for_size (i, MODE_INT, 0);
4554 if (mode != BLKmode)
4556 SET_TYPE_MODE (gnu_return_type, mode);
4557 TYPE_ALIGN (gnu_return_type)
4558 = GET_MODE_ALIGNMENT (mode);
4559 TYPE_SIZE (gnu_return_type)
4560 = bitsize_int (GET_MODE_BITSIZE (mode));
4561 TYPE_SIZE_UNIT (gnu_return_type)
4562 = size_int (GET_MODE_SIZE (mode));
4566 if (debug_info_p)
4567 rest_of_record_type_compilation (gnu_return_type);
4571 /* Deal with platform-specific calling conventions. */
4572 if (Has_Stdcall_Convention (gnat_entity))
4573 prepend_one_attribute
4574 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4575 get_identifier ("stdcall"), NULL_TREE,
4576 gnat_entity);
4577 else if (Has_Thiscall_Convention (gnat_entity))
4578 prepend_one_attribute
4579 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4580 get_identifier ("thiscall"), NULL_TREE,
4581 gnat_entity);
4583 /* If we should request stack realignment for a foreign convention
4584 subprogram, do so. Note that this applies to task entry points
4585 in particular. */
4586 if (FOREIGN_FORCE_REALIGN_STACK
4587 && Has_Foreign_Convention (gnat_entity))
4588 prepend_one_attribute
4589 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4590 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4591 gnat_entity);
4593 /* Deal with a pragma Linker_Section on a subprogram. */
4594 if ((kind == E_Function || kind == E_Procedure)
4595 && Present (Linker_Section_Pragma (gnat_entity)))
4596 prepend_one_attribute_pragma (&attr_list,
4597 Linker_Section_Pragma (gnat_entity));
4599 /* The lists have been built in reverse. */
4600 gnu_param_list = nreverse (gnu_param_list);
4601 if (has_stub)
4602 gnu_stub_param_list = nreverse (gnu_stub_param_list);
4603 gnu_cico_list = nreverse (gnu_cico_list);
4605 if (kind == E_Function)
4606 Set_Mechanism (gnat_entity, return_unconstrained_p
4607 || return_by_direct_ref_p
4608 || return_by_invisi_ref_p
4609 ? By_Reference : By_Copy);
4610 gnu_type
4611 = create_subprog_type (gnu_return_type, gnu_param_list,
4612 gnu_cico_list, return_unconstrained_p,
4613 return_by_direct_ref_p,
4614 return_by_invisi_ref_p);
4616 if (has_stub)
4617 gnu_stub_type
4618 = create_subprog_type (gnu_return_type, gnu_stub_param_list,
4619 gnu_cico_list, return_unconstrained_p,
4620 return_by_direct_ref_p,
4621 return_by_invisi_ref_p);
4623 /* A subprogram (something that doesn't return anything) shouldn't
4624 be considered const since there would be no reason for such a
4625 subprogram. Note that procedures with Out (or In Out) parameters
4626 have already been converted into a function with a return type. */
4627 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4628 const_flag = false;
4630 gnu_type
4631 = build_qualified_type (gnu_type,
4632 TYPE_QUALS (gnu_type)
4633 | (TYPE_QUAL_CONST * const_flag)
4634 | (TYPE_QUAL_VOLATILE * volatile_flag));
4636 if (has_stub)
4637 gnu_stub_type
4638 = build_qualified_type (gnu_stub_type,
4639 TYPE_QUALS (gnu_stub_type)
4640 | (TYPE_QUAL_CONST * const_flag)
4641 | (TYPE_QUAL_VOLATILE * volatile_flag));
4643 /* If we have a builtin decl for that function, use it. Check if the
4644 profiles are compatible and warn if they are not. The checker is
4645 expected to post extra diagnostics in this case. */
4646 if (gnu_builtin_decl)
4648 intrin_binding_t inb;
4650 inb.gnat_entity = gnat_entity;
4651 inb.ada_fntype = gnu_type;
4652 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4654 if (!intrin_profiles_compatible_p (&inb))
4655 post_error
4656 ("?profile of& doesn''t match the builtin it binds!",
4657 gnat_entity);
4659 gnu_decl = gnu_builtin_decl;
4660 gnu_type = TREE_TYPE (gnu_builtin_decl);
4661 break;
4664 /* If there was no specified Interface_Name and the external and
4665 internal names of the subprogram are the same, only use the
4666 internal name to allow disambiguation of nested subprograms. */
4667 if (No (Interface_Name (gnat_entity))
4668 && gnu_ext_name == gnu_entity_name)
4669 gnu_ext_name = NULL_TREE;
4671 /* If we are defining the subprogram and it has an Address clause
4672 we must get the address expression from the saved GCC tree for the
4673 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4674 the address expression here since the front-end has guaranteed
4675 in that case that the elaboration has no effects. If there is
4676 an Address clause and we are not defining the object, just
4677 make it a constant. */
4678 if (Present (Address_Clause (gnat_entity)))
4680 tree gnu_address = NULL_TREE;
4682 if (definition)
4683 gnu_address
4684 = (present_gnu_tree (gnat_entity)
4685 ? get_gnu_tree (gnat_entity)
4686 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4688 save_gnu_tree (gnat_entity, NULL_TREE, false);
4690 /* Convert the type of the object to a reference type that can
4691 alias everything as per 13.3(19). */
4692 gnu_type
4693 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4694 if (gnu_address)
4695 gnu_address = convert (gnu_type, gnu_address);
4697 gnu_decl
4698 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4699 gnu_address, false, Is_Public (gnat_entity),
4700 extern_flag, false, NULL, gnat_entity);
4701 DECL_BY_REF_P (gnu_decl) = 1;
4704 else if (kind == E_Subprogram_Type)
4706 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4707 gnu_decl
4708 = create_type_decl (gnu_entity_name, gnu_type, artificial_flag,
4709 debug_info_p, gnat_entity);
4711 else
4713 if (has_stub)
4715 gnu_stub_name = gnu_ext_name;
4716 gnu_ext_name = create_concat_name (gnat_entity, "internal");
4717 public_flag = false;
4718 artificial_flag = true;
4721 gnu_decl
4722 = create_subprog_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4723 gnu_param_list, inline_status,
4724 public_flag, extern_flag, artificial_flag,
4725 attr_list, gnat_entity);
4726 if (has_stub)
4728 tree gnu_stub_decl
4729 = create_subprog_decl (gnu_entity_name, gnu_stub_name,
4730 gnu_stub_type, gnu_stub_param_list,
4731 inline_status, true, extern_flag,
4732 false, attr_list, gnat_entity);
4733 SET_DECL_FUNCTION_STUB (gnu_decl, gnu_stub_decl);
4736 /* This is unrelated to the stub built right above. */
4737 DECL_STUBBED_P (gnu_decl)
4738 = Convention (gnat_entity) == Convention_Stubbed;
4741 break;
4743 case E_Incomplete_Type:
4744 case E_Incomplete_Subtype:
4745 case E_Private_Type:
4746 case E_Private_Subtype:
4747 case E_Limited_Private_Type:
4748 case E_Limited_Private_Subtype:
4749 case E_Record_Type_With_Private:
4750 case E_Record_Subtype_With_Private:
4752 /* Get the "full view" of this entity. If this is an incomplete
4753 entity from a limited with, treat its non-limited view as the
4754 full view. Otherwise, use either the full view or the underlying
4755 full view, whichever is present. This is used in all the tests
4756 below. */
4757 Entity_Id full_view
4758 = (IN (kind, Incomplete_Kind) && From_Limited_With (gnat_entity))
4759 ? Non_Limited_View (gnat_entity)
4760 : Present (Full_View (gnat_entity))
4761 ? Full_View (gnat_entity)
4762 : Underlying_Full_View (gnat_entity);
4764 /* If this is an incomplete type with no full view, it must be a Taft
4765 Amendment type, in which case we return a dummy type. Otherwise,
4766 just get the type from its Etype. */
4767 if (No (full_view))
4769 if (kind == E_Incomplete_Type)
4771 gnu_type = make_dummy_type (gnat_entity);
4772 gnu_decl = TYPE_STUB_DECL (gnu_type);
4774 else
4776 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4777 NULL_TREE, 0);
4778 maybe_present = true;
4780 break;
4783 /* If we already made a type for the full view, reuse it. */
4784 else if (present_gnu_tree (full_view))
4786 gnu_decl = get_gnu_tree (full_view);
4787 break;
4790 /* Otherwise, if we are not defining the type now, get the type
4791 from the full view. But always get the type from the full view
4792 for define on use types, since otherwise we won't see them! */
4793 else if (!definition
4794 || (Is_Itype (full_view) && No (Freeze_Node (gnat_entity)))
4795 || (Is_Itype (gnat_entity) && No (Freeze_Node (full_view))))
4797 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4798 maybe_present = true;
4799 break;
4802 /* For incomplete types, make a dummy type entry which will be
4803 replaced later. Save it as the full declaration's type so
4804 we can do any needed updates when we see it. */
4805 gnu_type = make_dummy_type (gnat_entity);
4806 gnu_decl = TYPE_STUB_DECL (gnu_type);
4807 if (Has_Completion_In_Body (gnat_entity))
4808 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4809 save_gnu_tree (full_view, gnu_decl, 0);
4810 break;
4813 case E_Class_Wide_Type:
4814 /* Class-wide types are always transformed into their root type. */
4815 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4816 maybe_present = true;
4817 break;
4819 case E_Task_Type:
4820 case E_Task_Subtype:
4821 case E_Protected_Type:
4822 case E_Protected_Subtype:
4823 /* Concurrent types are always transformed into their record type. */
4824 if (type_annotate_only && No (gnat_equiv_type))
4825 gnu_type = void_type_node;
4826 else
4827 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4828 maybe_present = true;
4829 break;
4831 case E_Label:
4832 gnu_decl = create_label_decl (gnu_entity_name, gnat_entity);
4833 break;
4835 case E_Block:
4836 case E_Loop:
4837 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4838 we've already saved it, so we don't try to. */
4839 gnu_decl = error_mark_node;
4840 saved = true;
4841 break;
4843 case E_Abstract_State:
4844 /* This is a SPARK annotation that only reaches here when compiling in
4845 ASIS mode and has no characteristics to annotate. */
4846 gcc_assert (type_annotate_only);
4847 return error_mark_node;
4849 default:
4850 gcc_unreachable ();
4853 /* If we had a case where we evaluated another type and it might have
4854 defined this one, handle it here. */
4855 if (maybe_present && present_gnu_tree (gnat_entity))
4857 gnu_decl = get_gnu_tree (gnat_entity);
4858 saved = true;
4861 /* If we are processing a type and there is either no decl for it or
4862 we just made one, do some common processing for the type, such as
4863 handling alignment and possible padding. */
4864 if (is_type && (!gnu_decl || this_made_decl))
4866 /* Process the attributes, if not already done. Note that the type is
4867 already defined so we cannot pass true for IN_PLACE here. */
4868 process_attributes (&gnu_type, &attr_list, false, gnat_entity);
4870 /* Tell the middle-end that objects of tagged types are guaranteed to
4871 be properly aligned. This is necessary because conversions to the
4872 class-wide type are translated into conversions to the root type,
4873 which can be less aligned than some of its derived types. */
4874 if (Is_Tagged_Type (gnat_entity)
4875 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4876 TYPE_ALIGN_OK (gnu_type) = 1;
4878 /* Record whether the type is passed by reference. */
4879 if (!VOID_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4880 TYPE_BY_REFERENCE_P (gnu_type) = 1;
4882 /* ??? Don't set the size for a String_Literal since it is either
4883 confirming or we don't handle it properly (if the low bound is
4884 non-constant). */
4885 if (!gnu_size && kind != E_String_Literal_Subtype)
4887 Uint gnat_size = Known_Esize (gnat_entity)
4888 ? Esize (gnat_entity) : RM_Size (gnat_entity);
4889 gnu_size
4890 = validate_size (gnat_size, gnu_type, gnat_entity, TYPE_DECL,
4891 false, Has_Size_Clause (gnat_entity));
4894 /* If a size was specified, see if we can make a new type of that size
4895 by rearranging the type, for example from a fat to a thin pointer. */
4896 if (gnu_size)
4898 gnu_type
4899 = make_type_from_size (gnu_type, gnu_size,
4900 Has_Biased_Representation (gnat_entity));
4902 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4903 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4904 gnu_size = NULL_TREE;
4907 /* If the alignment hasn't already been processed and this is
4908 not an unconstrained array, see if an alignment is specified.
4909 If not, we pick a default alignment for atomic objects. */
4910 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4912 else if (Known_Alignment (gnat_entity))
4914 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4915 TYPE_ALIGN (gnu_type));
4917 /* Warn on suspiciously large alignments. This should catch
4918 errors about the (alignment,byte)/(size,bit) discrepancy. */
4919 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4921 tree size;
4923 /* If a size was specified, take it into account. Otherwise
4924 use the RM size for records or unions as the type size has
4925 already been adjusted to the alignment. */
4926 if (gnu_size)
4927 size = gnu_size;
4928 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
4929 && !TYPE_FAT_POINTER_P (gnu_type))
4930 size = rm_size (gnu_type);
4931 else
4932 size = TYPE_SIZE (gnu_type);
4934 /* Consider an alignment as suspicious if the alignment/size
4935 ratio is greater or equal to the byte/bit ratio. */
4936 if (tree_fits_uhwi_p (size)
4937 && align >= tree_to_uhwi (size) * BITS_PER_UNIT)
4938 post_error_ne ("?suspiciously large alignment specified for&",
4939 Expression (Alignment_Clause (gnat_entity)),
4940 gnat_entity);
4943 else if (Is_Atomic (gnat_entity) && !gnu_size
4944 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type))
4945 && integer_pow2p (TYPE_SIZE (gnu_type)))
4946 align = MIN (BIGGEST_ALIGNMENT,
4947 tree_to_uhwi (TYPE_SIZE (gnu_type)));
4948 else if (Is_Atomic (gnat_entity) && gnu_size
4949 && tree_fits_uhwi_p (gnu_size)
4950 && integer_pow2p (gnu_size))
4951 align = MIN (BIGGEST_ALIGNMENT, tree_to_uhwi (gnu_size));
4953 /* See if we need to pad the type. If we did, and made a record,
4954 the name of the new type may be changed. So get it back for
4955 us when we make the new TYPE_DECL below. */
4956 if (gnu_size || align > 0)
4957 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4958 false, !gnu_decl, definition, false);
4960 if (TYPE_IS_PADDING_P (gnu_type))
4961 gnu_entity_name = TYPE_IDENTIFIER (gnu_type);
4963 /* Now set the RM size of the type. We cannot do it before padding
4964 because we need to accept arbitrary RM sizes on integral types. */
4965 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4967 /* If we are at global level, GCC will have applied variable_size to
4968 the type, but that won't have done anything. So, if it's not
4969 a constant or self-referential, call elaborate_expression_1 to
4970 make a variable for the size rather than calculating it each time.
4971 Handle both the RM size and the actual size. */
4972 if (global_bindings_p ()
4973 && TYPE_SIZE (gnu_type)
4974 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4975 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4977 tree size = TYPE_SIZE (gnu_type);
4979 TYPE_SIZE (gnu_type)
4980 = elaborate_expression_1 (size, gnat_entity,
4981 get_identifier ("SIZE"),
4982 definition, false);
4984 /* ??? For now, store the size as a multiple of the alignment in
4985 bytes so that we can see the alignment from the tree. */
4986 TYPE_SIZE_UNIT (gnu_type)
4987 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4988 get_identifier ("SIZE_A_UNIT"),
4989 definition, false,
4990 TYPE_ALIGN (gnu_type));
4992 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4993 may not be marked by the call to create_type_decl below. */
4994 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4996 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4998 tree variant_part = get_variant_part (gnu_type);
4999 tree ada_size = TYPE_ADA_SIZE (gnu_type);
5001 if (variant_part)
5003 tree union_type = TREE_TYPE (variant_part);
5004 tree offset = DECL_FIELD_OFFSET (variant_part);
5006 /* If the position of the variant part is constant, subtract
5007 it from the size of the type of the parent to get the new
5008 size. This manual CSE reduces the data size. */
5009 if (TREE_CODE (offset) == INTEGER_CST)
5011 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
5012 TYPE_SIZE (union_type)
5013 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
5014 bit_from_pos (offset, bitpos));
5015 TYPE_SIZE_UNIT (union_type)
5016 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
5017 byte_from_pos (offset, bitpos));
5019 else
5021 TYPE_SIZE (union_type)
5022 = elaborate_expression_1 (TYPE_SIZE (union_type),
5023 gnat_entity,
5024 get_identifier ("VSIZE"),
5025 definition, false);
5027 /* ??? For now, store the size as a multiple of the
5028 alignment in bytes so that we can see the alignment
5029 from the tree. */
5030 TYPE_SIZE_UNIT (union_type)
5031 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
5032 gnat_entity,
5033 get_identifier
5034 ("VSIZE_A_UNIT"),
5035 definition, false,
5036 TYPE_ALIGN (union_type));
5038 /* ??? For now, store the offset as a multiple of the
5039 alignment in bytes so that we can see the alignment
5040 from the tree. */
5041 DECL_FIELD_OFFSET (variant_part)
5042 = elaborate_expression_2 (offset,
5043 gnat_entity,
5044 get_identifier ("VOFFSET"),
5045 definition, false,
5046 DECL_OFFSET_ALIGN
5047 (variant_part));
5050 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
5051 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
5054 if (operand_equal_p (ada_size, size, 0))
5055 ada_size = TYPE_SIZE (gnu_type);
5056 else
5057 ada_size
5058 = elaborate_expression_1 (ada_size, gnat_entity,
5059 get_identifier ("RM_SIZE"),
5060 definition, false);
5061 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
5065 /* If this is a record type or subtype, call elaborate_expression_2 on
5066 any field position. Do this for both global and local types.
5067 Skip any fields that we haven't made trees for to avoid problems with
5068 class wide types. */
5069 if (IN (kind, Record_Kind))
5070 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
5071 gnat_temp = Next_Entity (gnat_temp))
5072 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
5074 tree gnu_field = get_gnu_tree (gnat_temp);
5076 /* ??? For now, store the offset as a multiple of the alignment
5077 in bytes so that we can see the alignment from the tree. */
5078 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
5080 DECL_FIELD_OFFSET (gnu_field)
5081 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
5082 gnat_temp,
5083 get_identifier ("OFFSET"),
5084 definition, false,
5085 DECL_OFFSET_ALIGN (gnu_field));
5087 /* ??? The context of gnu_field is not necessarily gnu_type
5088 so the MULT_EXPR node built above may not be marked by
5089 the call to create_type_decl below. */
5090 if (global_bindings_p ())
5091 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
5095 if (Treat_As_Volatile (gnat_entity))
5096 gnu_type
5097 = build_qualified_type (gnu_type,
5098 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
5100 if (Is_Atomic (gnat_entity))
5101 check_ok_for_atomic (gnu_type, gnat_entity, false);
5103 if (Present (Alignment_Clause (gnat_entity)))
5104 TYPE_USER_ALIGN (gnu_type) = 1;
5106 if (Universal_Aliasing (gnat_entity))
5107 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
5109 if (!gnu_decl)
5110 gnu_decl = create_type_decl (gnu_entity_name, gnu_type,
5111 !Comes_From_Source (gnat_entity),
5112 debug_info_p, gnat_entity);
5113 else
5115 TREE_TYPE (gnu_decl) = gnu_type;
5116 TYPE_STUB_DECL (gnu_type) = gnu_decl;
5120 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
5122 gnu_type = TREE_TYPE (gnu_decl);
5124 /* If this is a derived type, relate its alias set to that of its parent
5125 to avoid troubles when a call to an inherited primitive is inlined in
5126 a context where a derived object is accessed. The inlined code works
5127 on the parent view so the resulting code may access the same object
5128 using both the parent and the derived alias sets, which thus have to
5129 conflict. As the same issue arises with component references, the
5130 parent alias set also has to conflict with composite types enclosing
5131 derived components. For instance, if we have:
5133 type D is new T;
5134 type R is record
5135 Component : D;
5136 end record;
5138 we want T to conflict with both D and R, in addition to R being a
5139 superset of D by record/component construction.
5141 One way to achieve this is to perform an alias set copy from the
5142 parent to the derived type. This is not quite appropriate, though,
5143 as we don't want separate derived types to conflict with each other:
5145 type I1 is new Integer;
5146 type I2 is new Integer;
5148 We want I1 and I2 to both conflict with Integer but we do not want
5149 I1 to conflict with I2, and an alias set copy on derivation would
5150 have that effect.
5152 The option chosen is to make the alias set of the derived type a
5153 superset of that of its parent type. It trivially fulfills the
5154 simple requirement for the Integer derivation example above, and
5155 the component case as well by superset transitivity:
5157 superset superset
5158 R ----------> D ----------> T
5160 However, for composite types, conversions between derived types are
5161 translated into VIEW_CONVERT_EXPRs so a sequence like:
5163 type Comp1 is new Comp;
5164 type Comp2 is new Comp;
5165 procedure Proc (C : Comp1);
5167 C : Comp2;
5168 Proc (Comp1 (C));
5170 is translated into:
5172 C : Comp2;
5173 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5175 and gimplified into:
5177 C : Comp2;
5178 Comp1 *C.0;
5179 C.0 = (Comp1 *) &C;
5180 Proc (C.0);
5182 i.e. generates code involving type punning. Therefore, Comp1 needs
5183 to conflict with Comp2 and an alias set copy is required.
5185 The language rules ensure the parent type is already frozen here. */
5186 if (Is_Derived_Type (gnat_entity) && !type_annotate_only)
5188 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_entity));
5189 relate_alias_sets (gnu_type, gnu_parent_type,
5190 Is_Composite_Type (gnat_entity)
5191 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
5194 /* Back-annotate the Alignment of the type if not already in the
5195 tree. Likewise for sizes. */
5196 if (Unknown_Alignment (gnat_entity))
5198 unsigned int double_align, align;
5199 bool is_capped_double, align_clause;
5201 /* If the default alignment of "double" or larger scalar types is
5202 specifically capped and this is not an array with an alignment
5203 clause on the component type, return the cap. */
5204 if ((double_align = double_float_alignment) > 0)
5205 is_capped_double
5206 = is_double_float_or_array (gnat_entity, &align_clause);
5207 else if ((double_align = double_scalar_alignment) > 0)
5208 is_capped_double
5209 = is_double_scalar_or_array (gnat_entity, &align_clause);
5210 else
5211 is_capped_double = align_clause = false;
5213 if (is_capped_double && !align_clause)
5214 align = double_align;
5215 else
5216 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
5218 Set_Alignment (gnat_entity, UI_From_Int (align));
5221 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
5223 tree gnu_size = TYPE_SIZE (gnu_type);
5225 /* If the size is self-referential, annotate the maximum value. */
5226 if (CONTAINS_PLACEHOLDER_P (gnu_size))
5227 gnu_size = max_size (gnu_size, true);
5229 /* If we are just annotating types and the type is tagged, the tag
5230 and the parent components are not generated by the front-end so
5231 sizes must be adjusted if there is no representation clause. */
5232 if (type_annotate_only
5233 && Is_Tagged_Type (gnat_entity)
5234 && !VOID_TYPE_P (gnu_type)
5235 && (!TYPE_FIELDS (gnu_type)
5236 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type)))))
5238 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
5239 Uint uint_size;
5241 if (Is_Derived_Type (gnat_entity))
5243 Entity_Id gnat_parent = Etype (Base_Type (gnat_entity));
5244 offset = UI_To_gnu (Esize (gnat_parent), bitsizetype);
5245 Set_Alignment (gnat_entity, Alignment (gnat_parent));
5247 else
5248 offset = pointer_size;
5250 if (TYPE_FIELDS (gnu_type))
5251 offset
5252 = round_up (offset, DECL_ALIGN (TYPE_FIELDS (gnu_type)));
5254 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
5255 gnu_size = round_up (gnu_size, POINTER_SIZE);
5256 uint_size = annotate_value (gnu_size);
5257 Set_Esize (gnat_entity, uint_size);
5258 Set_RM_Size (gnat_entity, uint_size);
5260 else
5261 Set_Esize (gnat_entity, annotate_value (gnu_size));
5264 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
5265 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
5268 /* If we really have a ..._DECL node, set a couple of flags on it. But we
5269 cannot do so if we are reusing the ..._DECL node made for an equivalent
5270 type or an alias or a renamed object as the predicates don't apply to it
5271 but to GNAT_ENTITY. */
5272 if (DECL_P (gnu_decl)
5273 && !(is_type && gnat_equiv_type != gnat_entity)
5274 && !Present (Alias (gnat_entity))
5275 && !(Present (Renamed_Object (gnat_entity)) && saved))
5277 if (!Comes_From_Source (gnat_entity))
5278 DECL_ARTIFICIAL (gnu_decl) = 1;
5280 if (!debug_info_p)
5281 DECL_IGNORED_P (gnu_decl) = 1;
5284 /* If we haven't already, associate the ..._DECL node that we just made with
5285 the input GNAT entity node. */
5286 if (!saved)
5287 save_gnu_tree (gnat_entity, gnu_decl, false);
5289 /* If this is an enumeration or floating-point type, we were not able to set
5290 the bounds since they refer to the type. These are always static. */
5291 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
5292 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
5294 tree gnu_scalar_type = gnu_type;
5295 tree gnu_low_bound, gnu_high_bound;
5297 /* If this is a padded type, we need to use the underlying type. */
5298 if (TYPE_IS_PADDING_P (gnu_scalar_type))
5299 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
5301 /* If this is a floating point type and we haven't set a floating
5302 point type yet, use this in the evaluation of the bounds. */
5303 if (!longest_float_type_node && kind == E_Floating_Point_Type)
5304 longest_float_type_node = gnu_scalar_type;
5306 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
5307 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
5309 if (kind == E_Enumeration_Type)
5311 /* Enumeration types have specific RM bounds. */
5312 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
5313 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
5315 else
5317 /* Floating-point types don't have specific RM bounds. */
5318 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
5319 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
5323 /* If we deferred processing of incomplete types, re-enable it. If there
5324 were no other disables and we have deferred types to process, do so. */
5325 if (this_deferred
5326 && --defer_incomplete_level == 0
5327 && defer_incomplete_list)
5329 struct incomplete *p, *next;
5331 /* We are back to level 0 for the deferring of incomplete types.
5332 But processing these incomplete types below may itself require
5333 deferring, so preserve what we have and restart from scratch. */
5334 p = defer_incomplete_list;
5335 defer_incomplete_list = NULL;
5337 for (; p; p = next)
5339 next = p->next;
5341 if (p->old_type)
5342 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5343 gnat_to_gnu_type (p->full_type));
5344 free (p);
5348 /* If we are not defining this type, see if it's on one of the lists of
5349 incomplete types. If so, handle the list entry now. */
5350 if (is_type && !definition)
5352 struct incomplete *p;
5354 for (p = defer_incomplete_list; p; p = p->next)
5355 if (p->old_type && p->full_type == gnat_entity)
5357 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5358 TREE_TYPE (gnu_decl));
5359 p->old_type = NULL_TREE;
5362 for (p = defer_limited_with; p; p = p->next)
5363 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5365 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5366 TREE_TYPE (gnu_decl));
5367 p->old_type = NULL_TREE;
5371 if (this_global)
5372 force_global--;
5374 /* If this is a packed array type whose original array type is itself
5375 an Itype without freeze node, make sure the latter is processed. */
5376 if (Is_Packed_Array_Type (gnat_entity)
5377 && Is_Itype (Original_Array_Type (gnat_entity))
5378 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5379 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5380 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5382 return gnu_decl;
5385 /* Similar, but if the returned value is a COMPONENT_REF, return the
5386 FIELD_DECL. */
5388 tree
5389 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5391 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5393 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5394 gnu_field = TREE_OPERAND (gnu_field, 1);
5396 return gnu_field;
5399 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5400 the GCC type corresponding to that entity. */
5402 tree
5403 gnat_to_gnu_type (Entity_Id gnat_entity)
5405 tree gnu_decl;
5407 /* The back end never attempts to annotate generic types. */
5408 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5409 return void_type_node;
5411 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5412 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5414 return TREE_TYPE (gnu_decl);
5417 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5418 the unpadded version of the GCC type corresponding to that entity. */
5420 tree
5421 get_unpadded_type (Entity_Id gnat_entity)
5423 tree type = gnat_to_gnu_type (gnat_entity);
5425 if (TYPE_IS_PADDING_P (type))
5426 type = TREE_TYPE (TYPE_FIELDS (type));
5428 return type;
5431 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5432 type has been changed to that of the parameterless procedure, except if an
5433 alias is already present, in which case it is returned instead. */
5435 tree
5436 get_minimal_subprog_decl (Entity_Id gnat_entity)
5438 tree gnu_entity_name, gnu_ext_name;
5439 struct attrib *attr_list = NULL;
5441 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5442 of the handling applied here. */
5444 while (Present (Alias (gnat_entity)))
5446 gnat_entity = Alias (gnat_entity);
5447 if (present_gnu_tree (gnat_entity))
5448 return get_gnu_tree (gnat_entity);
5451 gnu_entity_name = get_entity_name (gnat_entity);
5452 gnu_ext_name = create_concat_name (gnat_entity, NULL);
5454 if (Has_Stdcall_Convention (gnat_entity))
5455 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5456 get_identifier ("stdcall"), NULL_TREE,
5457 gnat_entity);
5458 else if (Has_Thiscall_Convention (gnat_entity))
5459 prepend_one_attribute (&attr_list, ATTR_MACHINE_ATTRIBUTE,
5460 get_identifier ("thiscall"), NULL_TREE,
5461 gnat_entity);
5463 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_name)
5464 gnu_ext_name = NULL_TREE;
5466 return
5467 create_subprog_decl (gnu_entity_name, gnu_ext_name, void_ftype, NULL_TREE,
5468 is_disabled, true, true, true, attr_list, gnat_entity);
5471 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5472 a C++ imported method or equivalent.
5474 We use the predicate on 32-bit x86/Windows to find out whether we need to
5475 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5476 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5478 bool
5479 is_cplusplus_method (Entity_Id gnat_entity)
5481 if (Convention (gnat_entity) != Convention_CPP)
5482 return false;
5484 /* This is the main case: C++ method imported as a primitive operation. */
5485 if (Is_Dispatching_Operation (gnat_entity))
5486 return true;
5488 /* A thunk needs to be handled like its associated primitive operation. */
5489 if (Is_Subprogram (gnat_entity) && Is_Thunk (gnat_entity))
5490 return true;
5492 /* C++ classes with no virtual functions can be imported as limited
5493 record types, but we need to return true for the constructors. */
5494 if (Is_Constructor (gnat_entity))
5495 return true;
5497 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5498 if (Is_Dispatch_Table_Entity (gnat_entity))
5499 return true;
5501 return false;
5504 /* Finalize the processing of From_Limited_With incomplete types. */
5506 void
5507 finalize_from_limited_with (void)
5509 struct incomplete *p, *next;
5511 p = defer_limited_with;
5512 defer_limited_with = NULL;
5514 for (; p; p = next)
5516 next = p->next;
5518 if (p->old_type)
5519 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5520 gnat_to_gnu_type (p->full_type));
5521 free (p);
5525 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5526 kind of type (such E_Task_Type) that has a different type which Gigi
5527 uses for its representation. If the type does not have a special type
5528 for its representation, return GNAT_ENTITY. If a type is supposed to
5529 exist, but does not, abort unless annotating types, in which case
5530 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5532 Entity_Id
5533 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5535 Entity_Id gnat_equiv = gnat_entity;
5537 if (No (gnat_entity))
5538 return gnat_entity;
5540 switch (Ekind (gnat_entity))
5542 case E_Class_Wide_Subtype:
5543 if (Present (Equivalent_Type (gnat_entity)))
5544 gnat_equiv = Equivalent_Type (gnat_entity);
5545 break;
5547 case E_Access_Protected_Subprogram_Type:
5548 case E_Anonymous_Access_Protected_Subprogram_Type:
5549 gnat_equiv = Equivalent_Type (gnat_entity);
5550 break;
5552 case E_Class_Wide_Type:
5553 gnat_equiv = Root_Type (gnat_entity);
5554 break;
5556 case E_Task_Type:
5557 case E_Task_Subtype:
5558 case E_Protected_Type:
5559 case E_Protected_Subtype:
5560 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5561 break;
5563 default:
5564 break;
5567 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5569 return gnat_equiv;
5572 /* Return a GCC tree for a type corresponding to the component type of the
5573 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5574 is for an array being defined. DEBUG_INFO_P is true if we need to write
5575 debug information for other types that we may create in the process. */
5577 static tree
5578 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5579 bool debug_info_p)
5581 const Entity_Id gnat_type = Component_Type (gnat_array);
5582 tree gnu_type = gnat_to_gnu_type (gnat_type);
5583 tree gnu_comp_size;
5585 /* Try to get a smaller form of the component if needed. */
5586 if ((Is_Packed (gnat_array)
5587 || Has_Component_Size_Clause (gnat_array))
5588 && !Is_Bit_Packed_Array (gnat_array)
5589 && !Has_Aliased_Components (gnat_array)
5590 && !Strict_Alignment (gnat_type)
5591 && RECORD_OR_UNION_TYPE_P (gnu_type)
5592 && !TYPE_FAT_POINTER_P (gnu_type)
5593 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type)))
5594 gnu_type = make_packable_type (gnu_type, false);
5596 if (Has_Atomic_Components (gnat_array))
5597 check_ok_for_atomic (gnu_type, gnat_array, true);
5599 /* Get and validate any specified Component_Size. */
5600 gnu_comp_size
5601 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5602 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5603 true, Has_Component_Size_Clause (gnat_array));
5605 /* If the array has aliased components and the component size can be zero,
5606 force at least unit size to ensure that the components have distinct
5607 addresses. */
5608 if (!gnu_comp_size
5609 && Has_Aliased_Components (gnat_array)
5610 && (integer_zerop (TYPE_SIZE (gnu_type))
5611 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5612 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5613 gnu_comp_size
5614 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5616 /* If the component type is a RECORD_TYPE that has a self-referential size,
5617 then use the maximum size for the component size. */
5618 if (!gnu_comp_size
5619 && TREE_CODE (gnu_type) == RECORD_TYPE
5620 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5621 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5623 /* Honor the component size. This is not needed for bit-packed arrays. */
5624 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5626 tree orig_type = gnu_type;
5627 unsigned int max_align;
5629 /* If an alignment is specified, use it as a cap on the component type
5630 so that it can be honored for the whole type. But ignore it for the
5631 original type of packed array types. */
5632 if (No (Packed_Array_Type (gnat_array)) && Known_Alignment (gnat_array))
5633 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5634 else
5635 max_align = 0;
5637 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5638 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5639 gnu_type = orig_type;
5640 else
5641 orig_type = gnu_type;
5643 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5644 true, false, definition, true);
5646 /* If a padding record was made, declare it now since it will never be
5647 declared otherwise. This is necessary to ensure that its subtrees
5648 are properly marked. */
5649 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5650 create_type_decl (TYPE_NAME (gnu_type), gnu_type, true, debug_info_p,
5651 gnat_array);
5654 if (Has_Volatile_Components (gnat_array))
5655 gnu_type
5656 = build_qualified_type (gnu_type,
5657 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
5659 return gnu_type;
5662 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5663 using MECH as its passing mechanism, to be placed in the parameter
5664 list built for GNAT_SUBPROG. Assume a foreign convention for the
5665 latter if FOREIGN is true. Also set CICO to true if the parameter
5666 must use the copy-in copy-out implementation mechanism.
5668 The returned tree is a PARM_DECL, except for those cases where no
5669 parameter needs to be actually passed to the subprogram; the type
5670 of this "shadow" parameter is then returned instead. */
5672 static tree
5673 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5674 Entity_Id gnat_subprog, bool foreign, bool *cico)
5676 tree gnu_param_name = get_entity_name (gnat_param);
5677 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5678 tree gnu_param_type_alt = NULL_TREE;
5679 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5680 /* The parameter can be indirectly modified if its address is taken. */
5681 bool ro_param = in_param && !Address_Taken (gnat_param);
5682 bool by_return = false, by_component_ptr = false;
5683 bool by_ref = false;
5684 tree gnu_param;
5686 /* Copy-return is used only for the first parameter of a valued procedure.
5687 It's a copy mechanism for which a parameter is never allocated. */
5688 if (mech == By_Copy_Return)
5690 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5691 mech = By_Copy;
5692 by_return = true;
5695 /* If this is either a foreign function or if the underlying type won't
5696 be passed by reference, strip off possible padding type. */
5697 if (TYPE_IS_PADDING_P (gnu_param_type))
5699 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5701 if (mech == By_Reference
5702 || foreign
5703 || (!must_pass_by_ref (unpadded_type)
5704 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
5705 gnu_param_type = unpadded_type;
5708 /* If this is a read-only parameter, make a variant of the type that is
5709 read-only. ??? However, if this is an unconstrained array, that type
5710 can be very complex, so skip it for now. Likewise for any other
5711 self-referential type. */
5712 if (ro_param
5713 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5714 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5715 gnu_param_type = build_qualified_type (gnu_param_type,
5716 (TYPE_QUALS (gnu_param_type)
5717 | TYPE_QUAL_CONST));
5719 /* For foreign conventions, pass arrays as pointers to the element type.
5720 First check for unconstrained array and get the underlying array. */
5721 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5722 gnu_param_type
5723 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5725 /* For GCC builtins, pass Address integer types as (void *) */
5726 if (Convention (gnat_subprog) == Convention_Intrinsic
5727 && Present (Interface_Name (gnat_subprog))
5728 && Is_Descendent_Of_Address (Etype (gnat_param)))
5729 gnu_param_type = ptr_void_type_node;
5731 /* VMS descriptors are themselves passed by reference. */
5732 if (mech == By_Short_Descriptor ||
5733 (mech == By_Descriptor && TARGET_ABI_OPEN_VMS && !flag_vms_malloc64))
5734 gnu_param_type
5735 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5736 Mechanism (gnat_param),
5737 gnat_subprog));
5738 else if (mech == By_Descriptor)
5740 /* Build both a 32-bit and 64-bit descriptor, one of which will be
5741 chosen in fill_vms_descriptor. */
5742 gnu_param_type_alt
5743 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5744 Mechanism (gnat_param),
5745 gnat_subprog));
5746 gnu_param_type
5747 = build_pointer_type (build_vms_descriptor (gnu_param_type,
5748 Mechanism (gnat_param),
5749 gnat_subprog));
5752 /* Arrays are passed as pointers to element type for foreign conventions. */
5753 else if (foreign
5754 && mech != By_Copy
5755 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5757 /* Strip off any multi-dimensional entries, then strip
5758 off the last array to get the component type. */
5759 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5760 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5761 gnu_param_type = TREE_TYPE (gnu_param_type);
5763 by_component_ptr = true;
5764 gnu_param_type = TREE_TYPE (gnu_param_type);
5766 if (ro_param)
5767 gnu_param_type = build_qualified_type (gnu_param_type,
5768 (TYPE_QUALS (gnu_param_type)
5769 | TYPE_QUAL_CONST));
5771 gnu_param_type = build_pointer_type (gnu_param_type);
5774 /* Fat pointers are passed as thin pointers for foreign conventions. */
5775 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5776 gnu_param_type
5777 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5779 /* If we must pass or were requested to pass by reference, do so.
5780 If we were requested to pass by copy, do so.
5781 Otherwise, for foreign conventions, pass In Out or Out parameters
5782 or aggregates by reference. For COBOL and Fortran, pass all
5783 integer and FP types that way too. For Convention Ada, use
5784 the standard Ada default. */
5785 else if (must_pass_by_ref (gnu_param_type)
5786 || mech == By_Reference
5787 || (mech != By_Copy
5788 && ((foreign
5789 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5790 || (foreign
5791 && (Convention (gnat_subprog) == Convention_Fortran
5792 || Convention (gnat_subprog) == Convention_COBOL)
5793 && (INTEGRAL_TYPE_P (gnu_param_type)
5794 || FLOAT_TYPE_P (gnu_param_type)))
5795 || (!foreign
5796 && default_pass_by_ref (gnu_param_type)))))
5798 /* We take advantage of 6.2(12) by considering that references built for
5799 parameters whose type isn't by-ref and for which the mechanism hasn't
5800 been forced to by-ref are restrict-qualified in the C sense. */
5801 bool restrict_p
5802 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type) && mech != By_Reference;
5803 gnu_param_type = build_reference_type (gnu_param_type);
5804 if (restrict_p)
5805 gnu_param_type
5806 = build_qualified_type (gnu_param_type, TYPE_QUAL_RESTRICT);
5807 by_ref = true;
5810 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5811 else if (!in_param)
5812 *cico = true;
5814 if (mech == By_Copy && (by_ref || by_component_ptr))
5815 post_error ("?cannot pass & by copy", gnat_param);
5817 /* If this is an Out parameter that isn't passed by reference and isn't
5818 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5819 it will be a VAR_DECL created when we process the procedure, so just
5820 return its type. For the special parameter of a valued procedure,
5821 never pass it in.
5823 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5824 Out parameters with discriminants or implicit initial values to be
5825 handled like In Out parameters. These type are normally built as
5826 aggregates, hence passed by reference, except for some packed arrays
5827 which end up encoded in special integer types. Note that scalars can
5828 be given implicit initial values using the Default_Value aspect.
5830 The exception we need to make is then for packed arrays of records
5831 with discriminants or implicit initial values. We have no light/easy
5832 way to check for the latter case, so we merely check for packed arrays
5833 of records. This may lead to useless copy-in operations, but in very
5834 rare cases only, as these would be exceptions in a set of already
5835 exceptional situations. */
5836 if (Ekind (gnat_param) == E_Out_Parameter
5837 && !by_ref
5838 && (by_return
5839 || (mech != By_Descriptor
5840 && mech != By_Short_Descriptor
5841 && !POINTER_TYPE_P (gnu_param_type)
5842 && !AGGREGATE_TYPE_P (gnu_param_type)
5843 && !Has_Default_Aspect (Etype (gnat_param))))
5844 && !(Is_Array_Type (Etype (gnat_param))
5845 && Is_Packed (Etype (gnat_param))
5846 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5847 return gnu_param_type;
5849 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5850 ro_param || by_ref || by_component_ptr);
5851 DECL_BY_REF_P (gnu_param) = by_ref;
5852 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5853 DECL_BY_DESCRIPTOR_P (gnu_param)
5854 = (mech == By_Descriptor || mech == By_Short_Descriptor);
5855 DECL_POINTS_TO_READONLY_P (gnu_param)
5856 = (ro_param && (by_ref || by_component_ptr));
5857 DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5859 /* Save the alternate descriptor type, if any. */
5860 if (gnu_param_type_alt)
5861 SET_DECL_PARM_ALT_TYPE (gnu_param, gnu_param_type_alt);
5863 /* If no Mechanism was specified, indicate what we're using, then
5864 back-annotate it. */
5865 if (mech == Default)
5866 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5868 Set_Mechanism (gnat_param, mech);
5869 return gnu_param;
5872 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5874 static bool
5875 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5877 while (Present (Corresponding_Discriminant (discr1)))
5878 discr1 = Corresponding_Discriminant (discr1);
5880 while (Present (Corresponding_Discriminant (discr2)))
5881 discr2 = Corresponding_Discriminant (discr2);
5883 return
5884 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5887 /* Return true if the array type GNU_TYPE, which represents a dimension of
5888 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5890 static bool
5891 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5893 /* If the array type is not the innermost dimension of the GNAT type,
5894 then it has a non-aliased component. */
5895 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5896 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5897 return true;
5899 /* If the array type has an aliased component in the front-end sense,
5900 then it also has an aliased component in the back-end sense. */
5901 if (Has_Aliased_Components (gnat_type))
5902 return false;
5904 /* If this is a derived type, then it has a non-aliased component if
5905 and only if its parent type also has one. */
5906 if (Is_Derived_Type (gnat_type))
5908 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5909 int index;
5910 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5911 gnu_parent_type
5912 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5913 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5914 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5915 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5918 /* Otherwise, rely exclusively on properties of the element type. */
5919 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5922 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5924 static bool
5925 compile_time_known_address_p (Node_Id gnat_address)
5927 /* Catch System'To_Address. */
5928 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5929 gnat_address = Expression (gnat_address);
5931 return Compile_Time_Known_Value (gnat_address);
5934 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5935 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5937 static bool
5938 cannot_be_superflat_p (Node_Id gnat_range)
5940 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5941 Node_Id scalar_range;
5942 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5944 /* If the low bound is not constant, try to find an upper bound. */
5945 while (Nkind (gnat_lb) != N_Integer_Literal
5946 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5947 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5948 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5949 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5950 || Nkind (scalar_range) == N_Range))
5951 gnat_lb = High_Bound (scalar_range);
5953 /* If the high bound is not constant, try to find a lower bound. */
5954 while (Nkind (gnat_hb) != N_Integer_Literal
5955 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5956 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5957 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5958 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5959 || Nkind (scalar_range) == N_Range))
5960 gnat_hb = Low_Bound (scalar_range);
5962 /* If we have failed to find constant bounds, punt. */
5963 if (Nkind (gnat_lb) != N_Integer_Literal
5964 || Nkind (gnat_hb) != N_Integer_Literal)
5965 return false;
5967 /* We need at least a signed 64-bit type to catch most cases. */
5968 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5969 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5970 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5971 return false;
5973 /* If the low bound is the smallest integer, nothing can be smaller. */
5974 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5975 if (TREE_OVERFLOW (gnu_lb_minus_one))
5976 return true;
5978 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5981 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5983 static bool
5984 constructor_address_p (tree gnu_expr)
5986 while (TREE_CODE (gnu_expr) == NOP_EXPR
5987 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5988 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5989 gnu_expr = TREE_OPERAND (gnu_expr, 0);
5991 return (TREE_CODE (gnu_expr) == ADDR_EXPR
5992 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5995 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5996 be elaborated at the point of its definition, but do nothing else. */
5998 void
5999 elaborate_entity (Entity_Id gnat_entity)
6001 switch (Ekind (gnat_entity))
6003 case E_Signed_Integer_Subtype:
6004 case E_Modular_Integer_Subtype:
6005 case E_Enumeration_Subtype:
6006 case E_Ordinary_Fixed_Point_Subtype:
6007 case E_Decimal_Fixed_Point_Subtype:
6008 case E_Floating_Point_Subtype:
6010 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
6011 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
6013 /* ??? Tests to avoid Constraint_Error in static expressions
6014 are needed until after the front stops generating bogus
6015 conversions on bounds of real types. */
6016 if (!Raises_Constraint_Error (gnat_lb))
6017 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
6018 true, false, Needs_Debug_Info (gnat_entity));
6019 if (!Raises_Constraint_Error (gnat_hb))
6020 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
6021 true, false, Needs_Debug_Info (gnat_entity));
6022 break;
6025 case E_Record_Subtype:
6026 case E_Private_Subtype:
6027 case E_Limited_Private_Subtype:
6028 case E_Record_Subtype_With_Private:
6029 if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
6031 Node_Id gnat_discriminant_expr;
6032 Entity_Id gnat_field;
6034 for (gnat_field
6035 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
6036 gnat_discriminant_expr
6037 = First_Elmt (Discriminant_Constraint (gnat_entity));
6038 Present (gnat_field);
6039 gnat_field = Next_Discriminant (gnat_field),
6040 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
6041 /* Ignore access discriminants. */
6042 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
6043 elaborate_expression (Node (gnat_discriminant_expr),
6044 gnat_entity, get_entity_name (gnat_field),
6045 true, false, false);
6047 break;
6052 /* Return true if the size in units represented by GNU_SIZE can be handled by
6053 an allocation. If STATIC_P is true, consider only what can be done with a
6054 static allocation. */
6056 static bool
6057 allocatable_size_p (tree gnu_size, bool static_p)
6059 /* We can allocate a fixed size if it is a valid for the middle-end. */
6060 if (TREE_CODE (gnu_size) == INTEGER_CST)
6061 return valid_constant_size_p (gnu_size);
6063 /* We can allocate a variable size if this isn't a static allocation. */
6064 else
6065 return !static_p;
6068 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6069 NAME, ARGS and ERROR_POINT. */
6071 static void
6072 prepend_one_attribute (struct attrib **attr_list,
6073 enum attr_type attr_type,
6074 tree attr_name,
6075 tree attr_args,
6076 Node_Id attr_error_point)
6078 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
6080 attr->type = attr_type;
6081 attr->name = attr_name;
6082 attr->args = attr_args;
6083 attr->error_point = attr_error_point;
6085 attr->next = *attr_list;
6086 *attr_list = attr;
6089 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6091 static void
6092 prepend_one_attribute_pragma (struct attrib **attr_list, Node_Id gnat_pragma)
6094 const Node_Id gnat_arg = Pragma_Argument_Associations (gnat_pragma);
6095 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6096 enum attr_type etype;
6098 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6099 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma))))
6101 case Pragma_Machine_Attribute:
6102 etype = ATTR_MACHINE_ATTRIBUTE;
6103 break;
6105 case Pragma_Linker_Alias:
6106 etype = ATTR_LINK_ALIAS;
6107 break;
6109 case Pragma_Linker_Section:
6110 etype = ATTR_LINK_SECTION;
6111 break;
6113 case Pragma_Linker_Constructor:
6114 etype = ATTR_LINK_CONSTRUCTOR;
6115 break;
6117 case Pragma_Linker_Destructor:
6118 etype = ATTR_LINK_DESTRUCTOR;
6119 break;
6121 case Pragma_Weak_External:
6122 etype = ATTR_WEAK_EXTERNAL;
6123 break;
6125 case Pragma_Thread_Local_Storage:
6126 etype = ATTR_THREAD_LOCAL_STORAGE;
6127 break;
6129 default:
6130 return;
6133 /* See what arguments we have and turn them into GCC trees for attribute
6134 handlers. These expect identifier for strings. We handle at most two
6135 arguments and static expressions only. */
6136 if (Present (gnat_arg) && Present (First (gnat_arg)))
6138 Node_Id gnat_arg0 = Next (First (gnat_arg));
6139 Node_Id gnat_arg1 = Empty;
6141 if (Present (gnat_arg0)
6142 && Is_OK_Static_Expression (Expression (gnat_arg0)))
6144 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6146 if (TREE_CODE (gnu_arg0) == STRING_CST)
6148 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6149 if (IDENTIFIER_LENGTH (gnu_arg0) == 0)
6150 return;
6153 gnat_arg1 = Next (gnat_arg0);
6156 if (Present (gnat_arg1)
6157 && Is_OK_Static_Expression (Expression (gnat_arg1)))
6159 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6161 if (TREE_CODE (gnu_arg1) == STRING_CST)
6162 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6166 /* Prepend to the list. Make a list of the argument we might have, as GCC
6167 expects it. */
6168 prepend_one_attribute (attr_list, etype, gnu_arg0,
6169 gnu_arg1
6170 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6171 Present (Next (First (gnat_arg)))
6172 ? Expression (Next (First (gnat_arg))) : gnat_pragma);
6175 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6177 static void
6178 prepend_attributes (struct attrib **attr_list, Entity_Id gnat_entity)
6180 Node_Id gnat_temp;
6182 /* Attributes are stored as Representation Item pragmas. */
6183 for (gnat_temp = First_Rep_Item (gnat_entity);
6184 Present (gnat_temp);
6185 gnat_temp = Next_Rep_Item (gnat_temp))
6186 if (Nkind (gnat_temp) == N_Pragma)
6187 prepend_one_attribute_pragma (attr_list, gnat_temp);
6190 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6191 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6192 return the GCC tree to use for that expression. GNU_NAME is the suffix
6193 to use if a variable needs to be created and DEFINITION is true if this
6194 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6195 otherwise, we are just elaborating the expression for side-effects. If
6196 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6197 isn't needed for code generation. */
6199 static tree
6200 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
6201 bool definition, bool need_value, bool need_debug)
6203 tree gnu_expr;
6205 /* If we already elaborated this expression (e.g. it was involved
6206 in the definition of a private type), use the old value. */
6207 if (present_gnu_tree (gnat_expr))
6208 return get_gnu_tree (gnat_expr);
6210 /* If we don't need a value and this is static or a discriminant,
6211 we don't need to do anything. */
6212 if (!need_value
6213 && (Is_OK_Static_Expression (gnat_expr)
6214 || (Nkind (gnat_expr) == N_Identifier
6215 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6216 return NULL_TREE;
6218 /* If it's a static expression, we don't need a variable for debugging. */
6219 if (need_debug && Is_OK_Static_Expression (gnat_expr))
6220 need_debug = false;
6222 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6223 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
6224 gnu_name, definition, need_debug);
6226 /* Save the expression in case we try to elaborate this entity again. Since
6227 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6228 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6229 save_gnu_tree (gnat_expr, gnu_expr, true);
6231 return need_value ? gnu_expr : error_mark_node;
6234 /* Similar, but take a GNU expression and always return a result. */
6236 static tree
6237 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6238 bool definition, bool need_debug)
6240 const bool expr_public_p = Is_Public (gnat_entity);
6241 const bool expr_global_p = expr_public_p || global_bindings_p ();
6242 bool expr_variable_p, use_variable;
6244 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6245 reference will have been replaced with a COMPONENT_REF when the type
6246 is being elaborated. However, there are some cases involving child
6247 types where we will. So convert it to a COMPONENT_REF. We hope it
6248 will be at the highest level of the expression in these cases. */
6249 if (TREE_CODE (gnu_expr) == FIELD_DECL)
6250 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6251 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6252 gnu_expr, NULL_TREE);
6254 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6255 that an expression cannot contain both a discriminant and a variable. */
6256 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6257 return gnu_expr;
6259 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6260 a variable that is initialized to contain the expression when the package
6261 containing the definition is elaborated. If this entity is defined at top
6262 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6263 if this is necessary. */
6264 if (CONSTANT_CLASS_P (gnu_expr))
6265 expr_variable_p = false;
6266 else
6268 /* Skip any conversions and simple constant arithmetics to see if the
6269 expression is based on a read-only variable.
6270 ??? This really should remain read-only, but we have to think about
6271 the typing of the tree here. */
6272 tree inner = remove_conversions (gnu_expr, true);
6274 inner = skip_simple_constant_arithmetic (inner);
6276 if (handled_component_p (inner))
6278 HOST_WIDE_INT bitsize, bitpos;
6279 tree offset;
6280 enum machine_mode mode;
6281 int unsignedp, volatilep;
6283 inner = get_inner_reference (inner, &bitsize, &bitpos, &offset,
6284 &mode, &unsignedp, &volatilep, false);
6285 /* If the offset is variable, err on the side of caution. */
6286 if (offset)
6287 inner = NULL_TREE;
6290 expr_variable_p
6291 = !(inner
6292 && TREE_CODE (inner) == VAR_DECL
6293 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6296 /* We only need to use the variable if we are in a global context since GCC
6297 can do the right thing in the local case. However, when not optimizing,
6298 use it for bounds of loop iteration scheme to avoid code duplication. */
6299 use_variable = expr_variable_p
6300 && (expr_global_p
6301 || (!optimize
6302 && definition
6303 && Is_Itype (gnat_entity)
6304 && Nkind (Associated_Node_For_Itype (gnat_entity))
6305 == N_Loop_Parameter_Specification));
6307 /* Now create it, possibly only for debugging purposes. */
6308 if (use_variable || need_debug)
6310 tree gnu_decl
6311 = create_var_decl_1
6312 (create_concat_name (gnat_entity, IDENTIFIER_POINTER (gnu_name)),
6313 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true, expr_public_p,
6314 !definition, expr_global_p, !need_debug, NULL, gnat_entity);
6316 if (use_variable)
6317 return gnu_decl;
6320 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6323 /* Similar, but take an alignment factor and make it explicit in the tree. */
6325 static tree
6326 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6327 bool definition, bool need_debug, unsigned int align)
6329 tree unit_align = size_int (align / BITS_PER_UNIT);
6330 return
6331 size_binop (MULT_EXPR,
6332 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6333 gnu_expr,
6334 unit_align),
6335 gnat_entity, gnu_name, definition,
6336 need_debug),
6337 unit_align);
6340 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6341 the value passed against the list of choices. */
6343 tree
6344 choices_to_gnu (tree operand, Node_Id choices)
6346 Node_Id choice;
6347 Node_Id gnat_temp;
6348 tree result = boolean_false_node;
6349 tree this_test, low = 0, high = 0, single = 0;
6351 for (choice = First (choices); Present (choice); choice = Next (choice))
6353 switch (Nkind (choice))
6355 case N_Range:
6356 low = gnat_to_gnu (Low_Bound (choice));
6357 high = gnat_to_gnu (High_Bound (choice));
6359 this_test
6360 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6361 build_binary_op (GE_EXPR, boolean_type_node,
6362 operand, low),
6363 build_binary_op (LE_EXPR, boolean_type_node,
6364 operand, high));
6366 break;
6368 case N_Subtype_Indication:
6369 gnat_temp = Range_Expression (Constraint (choice));
6370 low = gnat_to_gnu (Low_Bound (gnat_temp));
6371 high = gnat_to_gnu (High_Bound (gnat_temp));
6373 this_test
6374 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6375 build_binary_op (GE_EXPR, boolean_type_node,
6376 operand, low),
6377 build_binary_op (LE_EXPR, boolean_type_node,
6378 operand, high));
6379 break;
6381 case N_Identifier:
6382 case N_Expanded_Name:
6383 /* This represents either a subtype range, an enumeration
6384 literal, or a constant Ekind says which. If an enumeration
6385 literal or constant, fall through to the next case. */
6386 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6387 && Ekind (Entity (choice)) != E_Constant)
6389 tree type = gnat_to_gnu_type (Entity (choice));
6391 low = TYPE_MIN_VALUE (type);
6392 high = TYPE_MAX_VALUE (type);
6394 this_test
6395 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6396 build_binary_op (GE_EXPR, boolean_type_node,
6397 operand, low),
6398 build_binary_op (LE_EXPR, boolean_type_node,
6399 operand, high));
6400 break;
6403 /* ... fall through ... */
6405 case N_Character_Literal:
6406 case N_Integer_Literal:
6407 single = gnat_to_gnu (choice);
6408 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6409 single);
6410 break;
6412 case N_Others_Choice:
6413 this_test = boolean_true_node;
6414 break;
6416 default:
6417 gcc_unreachable ();
6420 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6421 this_test);
6424 return result;
6427 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6428 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6430 static int
6431 adjust_packed (tree field_type, tree record_type, int packed)
6433 /* If the field contains an item of variable size, we cannot pack it
6434 because we cannot create temporaries of non-fixed size in case
6435 we need to take the address of the field. See addressable_p and
6436 the notes on the addressability issues for further details. */
6437 if (type_has_variable_size (field_type))
6438 return 0;
6440 /* If the alignment of the record is specified and the field type
6441 is over-aligned, request Storage_Unit alignment for the field. */
6442 if (packed == -2)
6444 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6445 return -1;
6446 else
6447 return 0;
6450 return packed;
6453 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6454 placed in GNU_RECORD_TYPE.
6456 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6457 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6458 record has a specified alignment.
6460 DEFINITION is true if this field is for a record being defined.
6462 DEBUG_INFO_P is true if we need to write debug information for types
6463 that we may create in the process. */
6465 static tree
6466 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6467 bool definition, bool debug_info_p)
6469 const Entity_Id gnat_field_type = Etype (gnat_field);
6470 tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6471 tree gnu_field_id = get_entity_name (gnat_field);
6472 tree gnu_field, gnu_size, gnu_pos;
6473 bool is_volatile
6474 = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6475 bool needs_strict_alignment
6476 = (is_volatile
6477 || Is_Aliased (gnat_field)
6478 || Strict_Alignment (gnat_field_type));
6480 /* If this field requires strict alignment, we cannot pack it because
6481 it would very likely be under-aligned in the record. */
6482 if (needs_strict_alignment)
6483 packed = 0;
6484 else
6485 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6487 /* If a size is specified, use it. Otherwise, if the record type is packed,
6488 use the official RM size. See "Handling of Type'Size Values" in Einfo
6489 for further details. */
6490 if (Known_Esize (gnat_field))
6491 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6492 gnat_field, FIELD_DECL, false, true);
6493 else if (packed == 1)
6494 gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6495 gnat_field, FIELD_DECL, false, true);
6496 else
6497 gnu_size = NULL_TREE;
6499 /* If we have a specified size that is smaller than that of the field's type,
6500 or a position is specified, and the field's type is a record that doesn't
6501 require strict alignment, see if we can get either an integral mode form
6502 of the type or a smaller form. If we can, show a size was specified for
6503 the field if there wasn't one already, so we know to make this a bitfield
6504 and avoid making things wider.
6506 Changing to an integral mode form is useful when the record is packed as
6507 we can then place the field at a non-byte-aligned position and so achieve
6508 tighter packing. This is in addition required if the field shares a byte
6509 with another field and the front-end lets the back-end handle the access
6510 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6512 Changing to a smaller form is required if the specified size is smaller
6513 than that of the field's type and the type contains sub-fields that are
6514 padded, in order to avoid generating accesses to these sub-fields that
6515 are wider than the field.
6517 We avoid the transformation if it is not required or potentially useful,
6518 as it might entail an increase of the field's alignment and have ripple
6519 effects on the outer record type. A typical case is a field known to be
6520 byte-aligned and not to share a byte with another field. */
6521 if (!needs_strict_alignment
6522 && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6523 && !TYPE_FAT_POINTER_P (gnu_field_type)
6524 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type))
6525 && (packed == 1
6526 || (gnu_size
6527 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6528 || (Present (Component_Clause (gnat_field))
6529 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6530 % BITS_PER_UNIT == 0
6531 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6533 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6534 if (gnu_packable_type != gnu_field_type)
6536 gnu_field_type = gnu_packable_type;
6537 if (!gnu_size)
6538 gnu_size = rm_size (gnu_field_type);
6542 if (Is_Atomic (gnat_field))
6543 check_ok_for_atomic (gnu_field_type, gnat_field, false);
6545 if (Present (Component_Clause (gnat_field)))
6547 Entity_Id gnat_parent
6548 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6550 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6551 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6552 gnat_field, FIELD_DECL, false, true);
6554 /* Ensure the position does not overlap with the parent subtype, if there
6555 is one. This test is omitted if the parent of the tagged type has a
6556 full rep clause since, in this case, component clauses are allowed to
6557 overlay the space allocated for the parent type and the front-end has
6558 checked that there are no overlapping components. */
6559 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6561 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6563 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6564 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6566 post_error_ne_tree
6567 ("offset of& must be beyond parent{, minimum allowed is ^}",
6568 First_Bit (Component_Clause (gnat_field)), gnat_field,
6569 TYPE_SIZE_UNIT (gnu_parent));
6573 /* If this field needs strict alignment, check that the record is
6574 sufficiently aligned and that position and size are consistent with
6575 the alignment. But don't do it if we are just annotating types and
6576 the field's type is tagged, since tagged types aren't fully laid out
6577 in this mode. Also, note that atomic implies volatile so the inner
6578 test sequences ordering is significant here. */
6579 if (needs_strict_alignment
6580 && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6582 TYPE_ALIGN (gnu_record_type)
6583 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6585 if (gnu_size
6586 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6588 if (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type))
6589 post_error_ne_tree
6590 ("atomic field& must be natural size of type{ (^)}",
6591 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6592 TYPE_SIZE (gnu_field_type));
6594 else if (is_volatile)
6595 post_error_ne_tree
6596 ("volatile field& must be natural size of type{ (^)}",
6597 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6598 TYPE_SIZE (gnu_field_type));
6600 else if (Is_Aliased (gnat_field))
6601 post_error_ne_tree
6602 ("size of aliased field& must be ^ bits",
6603 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6604 TYPE_SIZE (gnu_field_type));
6606 else if (Strict_Alignment (gnat_field_type))
6607 post_error_ne_tree
6608 ("size of & with aliased or tagged components not ^ bits",
6609 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6610 TYPE_SIZE (gnu_field_type));
6612 else
6613 gcc_unreachable ();
6615 gnu_size = NULL_TREE;
6618 if (!integer_zerop (size_binop
6619 (TRUNC_MOD_EXPR, gnu_pos,
6620 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6622 if (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type))
6623 post_error_ne_num
6624 ("position of atomic field& must be multiple of ^ bits",
6625 First_Bit (Component_Clause (gnat_field)), gnat_field,
6626 TYPE_ALIGN (gnu_field_type));
6628 else if (is_volatile)
6629 post_error_ne_num
6630 ("position of volatile field& must be multiple of ^ bits",
6631 First_Bit (Component_Clause (gnat_field)), gnat_field,
6632 TYPE_ALIGN (gnu_field_type));
6634 else if (Is_Aliased (gnat_field))
6635 post_error_ne_num
6636 ("position of aliased field& must be multiple of ^ bits",
6637 First_Bit (Component_Clause (gnat_field)), gnat_field,
6638 TYPE_ALIGN (gnu_field_type));
6640 else if (Strict_Alignment (gnat_field_type))
6641 post_error_ne
6642 ("position of & is not compatible with alignment required "
6643 "by its components",
6644 First_Bit (Component_Clause (gnat_field)), gnat_field);
6646 else
6647 gcc_unreachable ();
6649 gnu_pos = NULL_TREE;
6654 /* If the record has rep clauses and this is the tag field, make a rep
6655 clause for it as well. */
6656 else if (Has_Specified_Layout (Scope (gnat_field))
6657 && Chars (gnat_field) == Name_uTag)
6659 gnu_pos = bitsize_zero_node;
6660 gnu_size = TYPE_SIZE (gnu_field_type);
6663 else
6665 gnu_pos = NULL_TREE;
6667 /* If we are packing the record and the field is BLKmode, round the
6668 size up to a byte boundary. */
6669 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6670 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6673 /* We need to make the size the maximum for the type if it is
6674 self-referential and an unconstrained type. In that case, we can't
6675 pack the field since we can't make a copy to align it. */
6676 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6677 && !gnu_size
6678 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6679 && !Is_Constrained (Underlying_Type (gnat_field_type)))
6681 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6682 packed = 0;
6685 /* If a size is specified, adjust the field's type to it. */
6686 if (gnu_size)
6688 tree orig_field_type;
6690 /* If the field's type is justified modular, we would need to remove
6691 the wrapper to (better) meet the layout requirements. However we
6692 can do so only if the field is not aliased to preserve the unique
6693 layout and if the prescribed size is not greater than that of the
6694 packed array to preserve the justification. */
6695 if (!needs_strict_alignment
6696 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6697 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6698 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6699 <= 0)
6700 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6702 /* Similarly if the field's type is a misaligned integral type, but
6703 there is no restriction on the size as there is no justification. */
6704 if (!needs_strict_alignment
6705 && TYPE_IS_PADDING_P (gnu_field_type)
6706 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
6707 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6709 gnu_field_type
6710 = make_type_from_size (gnu_field_type, gnu_size,
6711 Has_Biased_Representation (gnat_field));
6713 orig_field_type = gnu_field_type;
6714 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6715 false, false, definition, true);
6717 /* If a padding record was made, declare it now since it will never be
6718 declared otherwise. This is necessary to ensure that its subtrees
6719 are properly marked. */
6720 if (gnu_field_type != orig_field_type
6721 && !DECL_P (TYPE_NAME (gnu_field_type)))
6722 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, true,
6723 debug_info_p, gnat_field);
6726 /* Otherwise (or if there was an error), don't specify a position. */
6727 else
6728 gnu_pos = NULL_TREE;
6730 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6731 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6733 /* Now create the decl for the field. */
6734 gnu_field
6735 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6736 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6737 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6738 DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
6739 TREE_THIS_VOLATILE (gnu_field) = TREE_SIDE_EFFECTS (gnu_field) = is_volatile;
6741 if (Ekind (gnat_field) == E_Discriminant)
6742 DECL_DISCRIMINANT_NUMBER (gnu_field)
6743 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6745 return gnu_field;
6748 /* Return true if at least one member of COMPONENT_LIST needs strict
6749 alignment. */
6751 static bool
6752 components_need_strict_alignment (Node_Id component_list)
6754 Node_Id component_decl;
6756 for (component_decl = First_Non_Pragma (Component_Items (component_list));
6757 Present (component_decl);
6758 component_decl = Next_Non_Pragma (component_decl))
6760 Entity_Id gnat_field = Defining_Entity (component_decl);
6762 if (Is_Aliased (gnat_field))
6763 return true;
6765 if (Strict_Alignment (Etype (gnat_field)))
6766 return true;
6769 return false;
6772 /* Return true if TYPE is a type with variable size or a padding type with a
6773 field of variable size or a record that has a field with such a type. */
6775 static bool
6776 type_has_variable_size (tree type)
6778 tree field;
6780 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6781 return true;
6783 if (TYPE_IS_PADDING_P (type)
6784 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6785 return true;
6787 if (!RECORD_OR_UNION_TYPE_P (type))
6788 return false;
6790 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6791 if (type_has_variable_size (TREE_TYPE (field)))
6792 return true;
6794 return false;
6797 /* Return true if FIELD is an artificial field. */
6799 static bool
6800 field_is_artificial (tree field)
6802 /* These fields are generated by the front-end proper. */
6803 if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
6804 return true;
6806 /* These fields are generated by gigi. */
6807 if (DECL_INTERNAL_P (field))
6808 return true;
6810 return false;
6813 /* Return true if FIELD is a non-artificial aliased field. */
6815 static bool
6816 field_is_aliased (tree field)
6818 if (field_is_artificial (field))
6819 return false;
6821 return DECL_ALIASED_P (field);
6824 /* Return true if FIELD is a non-artificial field with self-referential
6825 size. */
6827 static bool
6828 field_has_self_size (tree field)
6830 if (field_is_artificial (field))
6831 return false;
6833 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6834 return false;
6836 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
6839 /* Return true if FIELD is a non-artificial field with variable size. */
6841 static bool
6842 field_has_variable_size (tree field)
6844 if (field_is_artificial (field))
6845 return false;
6847 if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6848 return false;
6850 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
6853 /* qsort comparer for the bit positions of two record components. */
6855 static int
6856 compare_field_bitpos (const PTR rt1, const PTR rt2)
6858 const_tree const field1 = * (const_tree const *) rt1;
6859 const_tree const field2 = * (const_tree const *) rt2;
6860 const int ret
6861 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6863 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6866 /* Structure holding information for a given variant. */
6867 typedef struct vinfo
6869 /* The record type of the variant. */
6870 tree type;
6872 /* The name of the variant. */
6873 tree name;
6875 /* The qualifier of the variant. */
6876 tree qual;
6878 /* Whether the variant has a rep clause. */
6879 bool has_rep;
6881 /* Whether the variant is packed. */
6882 bool packed;
6884 } vinfo_t;
6886 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
6887 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
6888 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
6889 When called from gnat_to_gnu_entity during the processing of a record type
6890 definition, the GCC node for the parent, if any, will be the single field
6891 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6892 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6893 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6895 PACKED is 1 if this is for a packed record, -1 if this is for a record
6896 with Component_Alignment of Storage_Unit, -2 if this is for a record
6897 with a specified alignment.
6899 DEFINITION is true if we are defining this record type.
6901 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6902 out the record. This means the alignment only serves to force fields to
6903 be bitfields, but not to require the record to be that aligned. This is
6904 used for variants.
6906 ALL_REP is true if a rep clause is present for all the fields.
6908 UNCHECKED_UNION is true if we are building this type for a record with a
6909 Pragma Unchecked_Union.
6911 ARTIFICIAL is true if this is a type that was generated by the compiler.
6913 DEBUG_INFO is true if we need to write debug information about the type.
6915 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6916 mean that its contents may be unused as well, only the container itself.
6918 REORDER is true if we are permitted to reorder components of this type.
6920 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6921 the outer record type down to this variant level. It is nonzero only if
6922 all the fields down to this level have a rep clause and ALL_REP is false.
6924 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6925 with a rep clause is to be added; in this case, that is all that should
6926 be done with such fields and the return value will be false. */
6928 static bool
6929 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6930 tree gnu_field_list, int packed, bool definition,
6931 bool cancel_alignment, bool all_rep,
6932 bool unchecked_union, bool artificial,
6933 bool debug_info, bool maybe_unused, bool reorder,
6934 tree first_free_pos, tree *p_gnu_rep_list)
6936 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6937 bool variants_have_rep = all_rep;
6938 bool layout_with_rep = false;
6939 bool has_self_field = false;
6940 bool has_aliased_after_self_field = false;
6941 Node_Id component_decl, variant_part;
6942 tree gnu_field, gnu_next, gnu_last;
6943 tree gnu_variant_part = NULL_TREE;
6944 tree gnu_rep_list = NULL_TREE;
6945 tree gnu_var_list = NULL_TREE;
6946 tree gnu_self_list = NULL_TREE;
6947 tree gnu_zero_list = NULL_TREE;
6949 /* For each component referenced in a component declaration create a GCC
6950 field and add it to the list, skipping pragmas in the GNAT list. */
6951 gnu_last = tree_last (gnu_field_list);
6952 if (Present (Component_Items (gnat_component_list)))
6953 for (component_decl
6954 = First_Non_Pragma (Component_Items (gnat_component_list));
6955 Present (component_decl);
6956 component_decl = Next_Non_Pragma (component_decl))
6958 Entity_Id gnat_field = Defining_Entity (component_decl);
6959 Name_Id gnat_name = Chars (gnat_field);
6961 /* If present, the _Parent field must have been created as the single
6962 field of the record type. Put it before any other fields. */
6963 if (gnat_name == Name_uParent)
6965 gnu_field = TYPE_FIELDS (gnu_record_type);
6966 gnu_field_list = chainon (gnu_field_list, gnu_field);
6968 else
6970 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
6971 definition, debug_info);
6973 /* If this is the _Tag field, put it before any other fields. */
6974 if (gnat_name == Name_uTag)
6975 gnu_field_list = chainon (gnu_field_list, gnu_field);
6977 /* If this is the _Controller field, put it before the other
6978 fields except for the _Tag or _Parent field. */
6979 else if (gnat_name == Name_uController && gnu_last)
6981 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
6982 DECL_CHAIN (gnu_last) = gnu_field;
6985 /* If this is a regular field, put it after the other fields. */
6986 else
6988 DECL_CHAIN (gnu_field) = gnu_field_list;
6989 gnu_field_list = gnu_field;
6990 if (!gnu_last)
6991 gnu_last = gnu_field;
6993 /* And record information for the final layout. */
6994 if (field_has_self_size (gnu_field))
6995 has_self_field = true;
6996 else if (has_self_field && field_is_aliased (gnu_field))
6997 has_aliased_after_self_field = true;
7001 save_gnu_tree (gnat_field, gnu_field, false);
7004 /* At the end of the component list there may be a variant part. */
7005 variant_part = Variant_Part (gnat_component_list);
7007 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7008 mutually exclusive and should go in the same memory. To do this we need
7009 to treat each variant as a record whose elements are created from the
7010 component list for the variant. So here we create the records from the
7011 lists for the variants and put them all into the QUAL_UNION_TYPE.
7012 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7013 use GNU_RECORD_TYPE if there are no fields so far. */
7014 if (Present (variant_part))
7016 Node_Id gnat_discr = Name (variant_part), variant;
7017 tree gnu_discr = gnat_to_gnu (gnat_discr);
7018 tree gnu_name = TYPE_IDENTIFIER (gnu_record_type);
7019 tree gnu_var_name
7020 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7021 "XVN");
7022 tree gnu_union_type, gnu_union_name;
7023 tree this_first_free_pos, gnu_variant_list = NULL_TREE;
7024 bool union_field_needs_strict_alignment = false;
7025 auto_vec <vinfo_t, 16> variant_types;
7026 vinfo_t *gnu_variant;
7027 unsigned int variants_align = 0;
7028 unsigned int i;
7030 gnu_union_name
7031 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7033 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7034 are all in the variant part, to match the layout of C unions. There
7035 is an associated check below. */
7036 if (TREE_CODE (gnu_record_type) == UNION_TYPE)
7037 gnu_union_type = gnu_record_type;
7038 else
7040 gnu_union_type
7041 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7043 TYPE_NAME (gnu_union_type) = gnu_union_name;
7044 TYPE_ALIGN (gnu_union_type) = 0;
7045 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7048 /* If all the fields down to this level have a rep clause, find out
7049 whether all the fields at this level also have one. If so, then
7050 compute the new first free position to be passed downward. */
7051 this_first_free_pos = first_free_pos;
7052 if (this_first_free_pos)
7054 for (gnu_field = gnu_field_list;
7055 gnu_field;
7056 gnu_field = DECL_CHAIN (gnu_field))
7057 if (DECL_FIELD_OFFSET (gnu_field))
7059 tree pos = bit_position (gnu_field);
7060 if (!tree_int_cst_lt (pos, this_first_free_pos))
7061 this_first_free_pos
7062 = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
7064 else
7066 this_first_free_pos = NULL_TREE;
7067 break;
7071 /* We build the variants in two passes. The bulk of the work is done in
7072 the first pass, that is to say translating the GNAT nodes, building
7073 the container types and computing the associated properties. However
7074 we cannot finish up the container types during this pass because we
7075 don't know where the variant part will be placed until the end. */
7076 for (variant = First_Non_Pragma (Variants (variant_part));
7077 Present (variant);
7078 variant = Next_Non_Pragma (variant))
7080 tree gnu_variant_type = make_node (RECORD_TYPE);
7081 tree gnu_inner_name, gnu_qual;
7082 bool has_rep;
7083 int field_packed;
7084 vinfo_t vinfo;
7086 Get_Variant_Encoding (variant);
7087 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7088 TYPE_NAME (gnu_variant_type)
7089 = concat_name (gnu_union_name,
7090 IDENTIFIER_POINTER (gnu_inner_name));
7092 /* Set the alignment of the inner type in case we need to make
7093 inner objects into bitfields, but then clear it out so the
7094 record actually gets only the alignment required. */
7095 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7096 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7098 /* Similarly, if the outer record has a size specified and all
7099 the fields have a rep clause, we can propagate the size. */
7100 if (all_rep_and_size)
7102 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7103 TYPE_SIZE_UNIT (gnu_variant_type)
7104 = TYPE_SIZE_UNIT (gnu_record_type);
7107 /* Add the fields into the record type for the variant. Note that
7108 we aren't sure to really use it at this point, see below. */
7109 has_rep
7110 = components_to_record (gnu_variant_type, Component_List (variant),
7111 NULL_TREE, packed, definition,
7112 !all_rep_and_size, all_rep,
7113 unchecked_union,
7114 true, debug_info, true, reorder,
7115 this_first_free_pos,
7116 all_rep || this_first_free_pos
7117 ? NULL : &gnu_rep_list);
7119 /* Translate the qualifier and annotate the GNAT node. */
7120 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7121 Set_Present_Expr (variant, annotate_value (gnu_qual));
7123 /* Deal with packedness like in gnat_to_gnu_field. */
7124 if (components_need_strict_alignment (Component_List (variant)))
7126 field_packed = 0;
7127 union_field_needs_strict_alignment = true;
7129 else
7130 field_packed
7131 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7133 /* Push this variant onto the stack for the second pass. */
7134 vinfo.type = gnu_variant_type;
7135 vinfo.name = gnu_inner_name;
7136 vinfo.qual = gnu_qual;
7137 vinfo.has_rep = has_rep;
7138 vinfo.packed = field_packed;
7139 variant_types.safe_push (vinfo);
7141 /* Compute the global properties that will determine the placement of
7142 the variant part. */
7143 variants_have_rep |= has_rep;
7144 if (!field_packed && TYPE_ALIGN (gnu_variant_type) > variants_align)
7145 variants_align = TYPE_ALIGN (gnu_variant_type);
7148 /* Round up the first free position to the alignment of the variant part
7149 for the variants without rep clause. This will guarantee a consistent
7150 layout independently of the placement of the variant part. */
7151 if (variants_have_rep && variants_align > 0 && this_first_free_pos)
7152 this_first_free_pos = round_up (this_first_free_pos, variants_align);
7154 /* In the second pass, the container types are adjusted if necessary and
7155 finished up, then the corresponding fields of the variant part are
7156 built with their qualifier, unless this is an unchecked union. */
7157 FOR_EACH_VEC_ELT (variant_types, i, gnu_variant)
7159 tree gnu_variant_type = gnu_variant->type;
7160 tree gnu_field_list = TYPE_FIELDS (gnu_variant_type);
7162 /* If this is an Unchecked_Union whose fields are all in the variant
7163 part and we have a single field with no representation clause or
7164 placed at offset zero, use the field directly to match the layout
7165 of C unions. */
7166 if (TREE_CODE (gnu_record_type) == UNION_TYPE
7167 && gnu_field_list
7168 && !DECL_CHAIN (gnu_field_list)
7169 && (!DECL_FIELD_OFFSET (gnu_field_list)
7170 || integer_zerop (bit_position (gnu_field_list))))
7172 gnu_field = gnu_field_list;
7173 DECL_CONTEXT (gnu_field) = gnu_record_type;
7175 else
7177 /* Finalize the variant type now. We used to throw away empty
7178 record types but we no longer do that because we need them to
7179 generate complete debug info for the variant; otherwise, the
7180 union type definition will be lacking the fields associated
7181 with these empty variants. */
7182 if (gnu_field_list && variants_have_rep && !gnu_variant->has_rep)
7184 /* The variant part will be at offset 0 so we need to ensure
7185 that the fields are laid out starting from the first free
7186 position at this level. */
7187 tree gnu_rep_type = make_node (RECORD_TYPE);
7188 tree gnu_rep_part;
7189 finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7190 gnu_rep_part
7191 = create_rep_part (gnu_rep_type, gnu_variant_type,
7192 this_first_free_pos);
7193 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7194 gnu_field_list = gnu_rep_part;
7195 finish_record_type (gnu_variant_type, gnu_field_list, 0,
7196 false);
7199 if (debug_info)
7200 rest_of_record_type_compilation (gnu_variant_type);
7201 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7202 true, debug_info, gnat_component_list);
7204 gnu_field
7205 = create_field_decl (gnu_variant->name, gnu_variant_type,
7206 gnu_union_type,
7207 all_rep_and_size
7208 ? TYPE_SIZE (gnu_variant_type) : 0,
7209 variants_have_rep ? bitsize_zero_node : 0,
7210 gnu_variant->packed, 0);
7212 DECL_INTERNAL_P (gnu_field) = 1;
7214 if (!unchecked_union)
7215 DECL_QUALIFIER (gnu_field) = gnu_variant->qual;
7218 DECL_CHAIN (gnu_field) = gnu_variant_list;
7219 gnu_variant_list = gnu_field;
7222 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7223 if (gnu_variant_list)
7225 int union_field_packed;
7227 if (all_rep_and_size)
7229 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7230 TYPE_SIZE_UNIT (gnu_union_type)
7231 = TYPE_SIZE_UNIT (gnu_record_type);
7234 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7235 all_rep_and_size ? 1 : 0, debug_info);
7237 /* If GNU_UNION_TYPE is our record type, it means we must have an
7238 Unchecked_Union with no fields. Verify that and, if so, just
7239 return. */
7240 if (gnu_union_type == gnu_record_type)
7242 gcc_assert (unchecked_union
7243 && !gnu_field_list
7244 && !gnu_rep_list);
7245 return variants_have_rep;
7248 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type, true,
7249 debug_info, gnat_component_list);
7251 /* Deal with packedness like in gnat_to_gnu_field. */
7252 if (union_field_needs_strict_alignment)
7253 union_field_packed = 0;
7254 else
7255 union_field_packed
7256 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7258 gnu_variant_part
7259 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7260 all_rep_and_size
7261 ? TYPE_SIZE (gnu_union_type) : 0,
7262 variants_have_rep ? bitsize_zero_node : 0,
7263 union_field_packed, 0);
7265 DECL_INTERNAL_P (gnu_variant_part) = 1;
7269 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7270 permitted to reorder components, self-referential sizes or variable sizes.
7271 If they do, pull them out and put them onto the appropriate list. We have
7272 to do this in a separate pass since we want to handle the discriminants
7273 but can't play with them until we've used them in debugging data above.
7275 Similarly, pull out the fields with zero size and no rep clause, as they
7276 would otherwise modify the layout and thus very likely run afoul of the
7277 Ada semantics, which are different from those of C here.
7279 ??? If we reorder them, debugging information will be wrong but there is
7280 nothing that can be done about this at the moment. */
7281 gnu_last = NULL_TREE;
7283 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7284 do { \
7285 if (gnu_last) \
7286 DECL_CHAIN (gnu_last) = gnu_next; \
7287 else \
7288 gnu_field_list = gnu_next; \
7290 DECL_CHAIN (gnu_field) = (LIST); \
7291 (LIST) = gnu_field; \
7292 } while (0)
7294 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7296 gnu_next = DECL_CHAIN (gnu_field);
7298 if (DECL_FIELD_OFFSET (gnu_field))
7300 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7301 continue;
7304 if ((reorder || has_aliased_after_self_field)
7305 && field_has_self_size (gnu_field))
7307 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7308 continue;
7311 if (reorder && field_has_variable_size (gnu_field))
7313 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7314 continue;
7317 if (DECL_SIZE (gnu_field) && integer_zerop (DECL_SIZE (gnu_field)))
7319 DECL_FIELD_OFFSET (gnu_field) = size_zero_node;
7320 SET_DECL_OFFSET_ALIGN (gnu_field, BIGGEST_ALIGNMENT);
7321 DECL_FIELD_BIT_OFFSET (gnu_field) = bitsize_zero_node;
7322 if (field_is_aliased (gnu_field))
7323 TYPE_ALIGN (gnu_record_type)
7324 = MAX (TYPE_ALIGN (gnu_record_type),
7325 TYPE_ALIGN (TREE_TYPE (gnu_field)));
7326 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list);
7327 continue;
7330 gnu_last = gnu_field;
7333 #undef MOVE_FROM_FIELD_LIST_TO
7335 gnu_field_list = nreverse (gnu_field_list);
7337 /* If permitted, we reorder the fields as follows:
7339 1) all fixed length fields,
7340 2) all fields whose length doesn't depend on discriminants,
7341 3) all fields whose length depends on discriminants,
7342 4) the variant part,
7344 within the record and within each variant recursively. */
7345 if (reorder)
7346 gnu_field_list
7347 = chainon (gnu_field_list, chainon (gnu_var_list, gnu_self_list));
7349 /* Otherwise, if there is an aliased field placed after a field whose length
7350 depends on discriminants, we put all the fields of the latter sort, last.
7351 We need to do this in case an object of this record type is mutable. */
7352 else if (has_aliased_after_self_field)
7353 gnu_field_list = chainon (gnu_field_list, gnu_self_list);
7355 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7356 in our REP list to the previous level because this level needs them in
7357 order to do a correct layout, i.e. avoid having overlapping fields. */
7358 if (p_gnu_rep_list && gnu_rep_list)
7359 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7361 /* Otherwise, sort the fields by bit position and put them into their own
7362 record, before the others, if we also have fields without rep clause. */
7363 else if (gnu_rep_list)
7365 tree gnu_rep_type, gnu_rep_part;
7366 int i, len = list_length (gnu_rep_list);
7367 tree *gnu_arr = XALLOCAVEC (tree, len);
7369 /* If all the fields have a rep clause, we can do a flat layout. */
7370 layout_with_rep = !gnu_field_list
7371 && (!gnu_variant_part || variants_have_rep);
7372 gnu_rep_type
7373 = layout_with_rep ? gnu_record_type : make_node (RECORD_TYPE);
7375 for (gnu_field = gnu_rep_list, i = 0;
7376 gnu_field;
7377 gnu_field = DECL_CHAIN (gnu_field), i++)
7378 gnu_arr[i] = gnu_field;
7380 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7382 /* Put the fields in the list in order of increasing position, which
7383 means we start from the end. */
7384 gnu_rep_list = NULL_TREE;
7385 for (i = len - 1; i >= 0; i--)
7387 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7388 gnu_rep_list = gnu_arr[i];
7389 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7392 if (layout_with_rep)
7393 gnu_field_list = gnu_rep_list;
7394 else
7396 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7398 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7399 without rep clause are laid out starting from this position.
7400 Therefore, we force it as a minimal size on the REP part. */
7401 gnu_rep_part
7402 = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7404 /* Chain the REP part at the beginning of the field list. */
7405 DECL_CHAIN (gnu_rep_part) = gnu_field_list;
7406 gnu_field_list = gnu_rep_part;
7410 /* Chain the variant part at the end of the field list. */
7411 if (gnu_variant_part)
7412 gnu_field_list = chainon (gnu_field_list, gnu_variant_part);
7414 if (cancel_alignment)
7415 TYPE_ALIGN (gnu_record_type) = 0;
7417 TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7419 finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0,
7420 debug_info && !maybe_unused);
7422 /* Chain the fields with zero size at the beginning of the field list. */
7423 if (gnu_zero_list)
7424 TYPE_FIELDS (gnu_record_type)
7425 = chainon (gnu_zero_list, TYPE_FIELDS (gnu_record_type));
7427 return (gnu_rep_list && !p_gnu_rep_list) || variants_have_rep;
7430 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7431 placed into an Esize, Component_Bit_Offset, or Component_Size value
7432 in the GNAT tree. */
7434 static Uint
7435 annotate_value (tree gnu_size)
7437 TCode tcode;
7438 Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7439 struct tree_int_map in;
7440 int i;
7442 /* See if we've already saved the value for this node. */
7443 if (EXPR_P (gnu_size))
7445 struct tree_int_map *e;
7447 in.base.from = gnu_size;
7448 e = (struct tree_int_map *) htab_find (annotate_value_cache, &in);
7450 if (e)
7451 return (Node_Ref_Or_Val) e->to;
7453 else
7454 in.base.from = NULL_TREE;
7456 /* If we do not return inside this switch, TCODE will be set to the
7457 code to use for a Create_Node operand and LEN (set above) will be
7458 the number of recursive calls for us to make. */
7460 switch (TREE_CODE (gnu_size))
7462 case INTEGER_CST:
7463 return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7465 case COMPONENT_REF:
7466 /* The only case we handle here is a simple discriminant reference. */
7467 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7469 tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7471 /* Climb up the chain of successive extensions, if any. */
7472 while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7473 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7474 == parent_name_id)
7475 gnu_size = TREE_OPERAND (gnu_size, 0);
7477 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7478 return
7479 Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7482 return No_Uint;
7484 CASE_CONVERT: case NON_LVALUE_EXPR:
7485 return annotate_value (TREE_OPERAND (gnu_size, 0));
7487 /* Now just list the operations we handle. */
7488 case COND_EXPR: tcode = Cond_Expr; break;
7489 case PLUS_EXPR: tcode = Plus_Expr; break;
7490 case MINUS_EXPR: tcode = Minus_Expr; break;
7491 case MULT_EXPR: tcode = Mult_Expr; break;
7492 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7493 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7494 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7495 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7496 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7497 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7498 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7499 case NEGATE_EXPR: tcode = Negate_Expr; break;
7500 case MIN_EXPR: tcode = Min_Expr; break;
7501 case MAX_EXPR: tcode = Max_Expr; break;
7502 case ABS_EXPR: tcode = Abs_Expr; break;
7503 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7504 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7505 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7506 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7507 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7508 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7509 case LT_EXPR: tcode = Lt_Expr; break;
7510 case LE_EXPR: tcode = Le_Expr; break;
7511 case GT_EXPR: tcode = Gt_Expr; break;
7512 case GE_EXPR: tcode = Ge_Expr; break;
7513 case EQ_EXPR: tcode = Eq_Expr; break;
7514 case NE_EXPR: tcode = Ne_Expr; break;
7516 case BIT_AND_EXPR:
7517 tcode = Bit_And_Expr;
7518 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
7519 Such values appear in expressions with aligning patterns. Note that,
7520 since sizetype is unsigned, we have to jump through some hoops. */
7521 if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7523 tree op1 = TREE_OPERAND (gnu_size, 1);
7524 wide_int signed_op1 = wi::sext (op1, TYPE_PRECISION (sizetype));
7525 if (wi::neg_p (signed_op1))
7527 op1 = wide_int_to_tree (sizetype, wi::neg (signed_op1));
7528 pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7531 break;
7533 case CALL_EXPR:
7534 /* In regular mode, inline back only if symbolic annotation is requested
7535 in order to avoid memory explosion on big discriminated record types.
7536 But not in ASIS mode, as symbolic annotation is required for DDA. */
7537 if (List_Representation_Info == 3 || type_annotate_only)
7539 tree t = maybe_inline_call_in_expr (gnu_size);
7540 if (t)
7541 return annotate_value (t);
7543 else
7544 return Uint_Minus_1;
7546 /* Fall through... */
7548 default:
7549 return No_Uint;
7552 /* Now get each of the operands that's relevant for this code. If any
7553 cannot be expressed as a repinfo node, say we can't. */
7554 for (i = 0; i < 3; i++)
7555 ops[i] = No_Uint;
7557 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7559 if (i == 1 && pre_op1 != No_Uint)
7560 ops[i] = pre_op1;
7561 else
7562 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7563 if (ops[i] == No_Uint)
7564 return No_Uint;
7567 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7569 /* Save the result in the cache. */
7570 if (in.base.from)
7572 struct tree_int_map **h;
7573 /* We can't assume the hash table data hasn't moved since the initial
7574 look up, so we have to search again. Allocating and inserting an
7575 entry at that point would be an alternative, but then we'd better
7576 discard the entry if we decided not to cache it. */
7577 h = (struct tree_int_map **)
7578 htab_find_slot (annotate_value_cache, &in, INSERT);
7579 gcc_assert (!*h);
7580 *h = ggc_alloc_tree_int_map ();
7581 (*h)->base.from = gnu_size;
7582 (*h)->to = ret;
7585 return ret;
7588 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7589 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7590 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7591 BY_REF is true if the object is used by reference. */
7593 void
7594 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7596 if (by_ref)
7598 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7599 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7600 else
7601 gnu_type = TREE_TYPE (gnu_type);
7604 if (Unknown_Esize (gnat_entity))
7606 if (TREE_CODE (gnu_type) == RECORD_TYPE
7607 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7608 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7609 else if (!size)
7610 size = TYPE_SIZE (gnu_type);
7612 if (size)
7613 Set_Esize (gnat_entity, annotate_value (size));
7616 if (Unknown_Alignment (gnat_entity))
7617 Set_Alignment (gnat_entity,
7618 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7621 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7622 Return NULL_TREE if there is no such element in the list. */
7624 static tree
7625 purpose_member_field (const_tree elem, tree list)
7627 while (list)
7629 tree field = TREE_PURPOSE (list);
7630 if (SAME_FIELD_P (field, elem))
7631 return list;
7632 list = TREE_CHAIN (list);
7634 return NULL_TREE;
7637 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7638 set Component_Bit_Offset and Esize of the components to the position and
7639 size used by Gigi. */
7641 static void
7642 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7644 Entity_Id gnat_field;
7645 tree gnu_list;
7647 /* We operate by first making a list of all fields and their position (we
7648 can get the size easily) and then update all the sizes in the tree. */
7649 gnu_list
7650 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7651 BIGGEST_ALIGNMENT, NULL_TREE);
7653 for (gnat_field = First_Entity (gnat_entity);
7654 Present (gnat_field);
7655 gnat_field = Next_Entity (gnat_field))
7656 if (Ekind (gnat_field) == E_Component
7657 || (Ekind (gnat_field) == E_Discriminant
7658 && !Is_Unchecked_Union (Scope (gnat_field))))
7660 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7661 gnu_list);
7662 if (t)
7664 tree parent_offset;
7666 /* If we are just annotating types and the type is tagged, the tag
7667 and the parent components are not generated by the front-end so
7668 we need to add the appropriate offset to each component without
7669 representation clause. */
7670 if (type_annotate_only
7671 && Is_Tagged_Type (gnat_entity)
7672 && No (Component_Clause (gnat_field)))
7674 /* For a component appearing in the current extension, the
7675 offset is the size of the parent. */
7676 if (Is_Derived_Type (gnat_entity)
7677 && Original_Record_Component (gnat_field) == gnat_field)
7678 parent_offset
7679 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7680 bitsizetype);
7681 else
7682 parent_offset = bitsize_int (POINTER_SIZE);
7684 if (TYPE_FIELDS (gnu_type))
7685 parent_offset
7686 = round_up (parent_offset,
7687 DECL_ALIGN (TYPE_FIELDS (gnu_type)));
7689 else
7690 parent_offset = bitsize_zero_node;
7692 Set_Component_Bit_Offset
7693 (gnat_field,
7694 annotate_value
7695 (size_binop (PLUS_EXPR,
7696 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7697 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7698 parent_offset)));
7700 Set_Esize (gnat_field,
7701 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7703 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7705 /* If there is no entry, this is an inherited component whose
7706 position is the same as in the parent type. */
7707 Set_Component_Bit_Offset
7708 (gnat_field,
7709 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7711 Set_Esize (gnat_field,
7712 Esize (Original_Record_Component (gnat_field)));
7717 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7718 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7719 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7720 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7721 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7722 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7723 pre-existing list to be chained to the newly created entries. */
7725 static tree
7726 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7727 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7729 tree gnu_field;
7731 for (gnu_field = TYPE_FIELDS (gnu_type);
7732 gnu_field;
7733 gnu_field = DECL_CHAIN (gnu_field))
7735 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7736 DECL_FIELD_BIT_OFFSET (gnu_field));
7737 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7738 DECL_FIELD_OFFSET (gnu_field));
7739 unsigned int our_offset_align
7740 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7741 tree v = make_tree_vec (3);
7743 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7744 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7745 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7746 gnu_list = tree_cons (gnu_field, v, gnu_list);
7748 /* Recurse on internal fields, flattening the nested fields except for
7749 those in the variant part, if requested. */
7750 if (DECL_INTERNAL_P (gnu_field))
7752 tree gnu_field_type = TREE_TYPE (gnu_field);
7753 if (do_not_flatten_variant
7754 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7755 gnu_list
7756 = build_position_list (gnu_field_type, do_not_flatten_variant,
7757 size_zero_node, bitsize_zero_node,
7758 BIGGEST_ALIGNMENT, gnu_list);
7759 else
7760 gnu_list
7761 = build_position_list (gnu_field_type, do_not_flatten_variant,
7762 gnu_our_offset, gnu_our_bitpos,
7763 our_offset_align, gnu_list);
7767 return gnu_list;
7770 /* Return a list describing the substitutions needed to reflect the
7771 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7772 be in any order. The values in an element of the list are in the form
7773 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7774 a definition of GNAT_SUBTYPE. */
7776 static vec<subst_pair>
7777 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7779 vec<subst_pair> gnu_list = vNULL;
7780 Entity_Id gnat_discrim;
7781 Node_Id gnat_constr;
7783 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7784 gnat_constr = First_Elmt (Stored_Constraint (gnat_subtype));
7785 Present (gnat_discrim);
7786 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7787 gnat_constr = Next_Elmt (gnat_constr))
7788 /* Ignore access discriminants. */
7789 if (!Is_Access_Type (Etype (Node (gnat_constr))))
7791 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7792 tree replacement = convert (TREE_TYPE (gnu_field),
7793 elaborate_expression
7794 (Node (gnat_constr), gnat_subtype,
7795 get_entity_name (gnat_discrim),
7796 definition, true, false));
7797 subst_pair s = {gnu_field, replacement};
7798 gnu_list.safe_push (s);
7801 return gnu_list;
7804 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7805 variants of QUAL_UNION_TYPE that are still relevant after applying
7806 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
7807 list to be prepended to the newly created entries. */
7809 static vec<variant_desc>
7810 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
7811 vec<variant_desc> gnu_list)
7813 tree gnu_field;
7815 for (gnu_field = TYPE_FIELDS (qual_union_type);
7816 gnu_field;
7817 gnu_field = DECL_CHAIN (gnu_field))
7819 tree qual = DECL_QUALIFIER (gnu_field);
7820 unsigned int i;
7821 subst_pair *s;
7823 FOR_EACH_VEC_ELT (subst_list, i, s)
7824 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7826 /* If the new qualifier is not unconditionally false, its variant may
7827 still be accessed. */
7828 if (!integer_zerop (qual))
7830 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7831 variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
7833 gnu_list.safe_push (v);
7835 /* Recurse on the variant subpart of the variant, if any. */
7836 variant_subpart = get_variant_part (variant_type);
7837 if (variant_subpart)
7838 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
7839 subst_list, gnu_list);
7841 /* If the new qualifier is unconditionally true, the subsequent
7842 variants cannot be accessed. */
7843 if (integer_onep (qual))
7844 break;
7848 return gnu_list;
7851 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7852 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7853 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7854 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7855 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7856 true if we are being called to process the Component_Size of GNAT_OBJECT;
7857 this is used only for error messages. ZERO_OK is true if a size of zero
7858 is permitted; if ZERO_OK is false, it means that a size of zero should be
7859 treated as an unspecified size. */
7861 static tree
7862 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7863 enum tree_code kind, bool component_p, bool zero_ok)
7865 Node_Id gnat_error_node;
7866 tree type_size, size;
7868 /* Return 0 if no size was specified. */
7869 if (uint_size == No_Uint)
7870 return NULL_TREE;
7872 /* Ignore a negative size since that corresponds to our back-annotation. */
7873 if (UI_Lt (uint_size, Uint_0))
7874 return NULL_TREE;
7876 /* Find the node to use for error messages. */
7877 if ((Ekind (gnat_object) == E_Component
7878 || Ekind (gnat_object) == E_Discriminant)
7879 && Present (Component_Clause (gnat_object)))
7880 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7881 else if (Present (Size_Clause (gnat_object)))
7882 gnat_error_node = Expression (Size_Clause (gnat_object));
7883 else
7884 gnat_error_node = gnat_object;
7886 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7887 but cannot be represented in bitsizetype. */
7888 size = UI_To_gnu (uint_size, bitsizetype);
7889 if (TREE_OVERFLOW (size))
7891 if (component_p)
7892 post_error_ne ("component size for& is too large", gnat_error_node,
7893 gnat_object);
7894 else
7895 post_error_ne ("size for& is too large", gnat_error_node,
7896 gnat_object);
7897 return NULL_TREE;
7900 /* Ignore a zero size if it is not permitted. */
7901 if (!zero_ok && integer_zerop (size))
7902 return NULL_TREE;
7904 /* The size of objects is always a multiple of a byte. */
7905 if (kind == VAR_DECL
7906 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7908 if (component_p)
7909 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7910 gnat_error_node, gnat_object);
7911 else
7912 post_error_ne ("size for& is not a multiple of Storage_Unit",
7913 gnat_error_node, gnat_object);
7914 return NULL_TREE;
7917 /* If this is an integral type or a packed array type, the front-end has
7918 already verified the size, so we need not do it here (which would mean
7919 checking against the bounds). However, if this is an aliased object,
7920 it may not be smaller than the type of the object. */
7921 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7922 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7923 return size;
7925 /* If the object is a record that contains a template, add the size of the
7926 template to the specified size. */
7927 if (TREE_CODE (gnu_type) == RECORD_TYPE
7928 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7929 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7931 if (kind == VAR_DECL
7932 /* If a type needs strict alignment, a component of this type in
7933 a packed record cannot be packed and thus uses the type size. */
7934 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7935 type_size = TYPE_SIZE (gnu_type);
7936 else
7937 type_size = rm_size (gnu_type);
7939 /* Modify the size of a discriminated type to be the maximum size. */
7940 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7941 type_size = max_size (type_size, true);
7943 /* If this is an access type or a fat pointer, the minimum size is that given
7944 by the smallest integral mode that's valid for pointers. */
7945 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7947 enum machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7948 while (!targetm.valid_pointer_mode (p_mode))
7949 p_mode = GET_MODE_WIDER_MODE (p_mode);
7950 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7953 /* Issue an error either if the default size of the object isn't a constant
7954 or if the new size is smaller than it. */
7955 if (TREE_CODE (type_size) != INTEGER_CST
7956 || TREE_OVERFLOW (type_size)
7957 || tree_int_cst_lt (size, type_size))
7959 if (component_p)
7960 post_error_ne_tree
7961 ("component size for& too small{, minimum allowed is ^}",
7962 gnat_error_node, gnat_object, type_size);
7963 else
7964 post_error_ne_tree
7965 ("size for& too small{, minimum allowed is ^}",
7966 gnat_error_node, gnat_object, type_size);
7967 return NULL_TREE;
7970 return size;
7973 /* Similarly, but both validate and process a value of RM size. This routine
7974 is only called for types. */
7976 static void
7977 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7979 Node_Id gnat_attr_node;
7980 tree old_size, size;
7982 /* Do nothing if no size was specified. */
7983 if (uint_size == No_Uint)
7984 return;
7986 /* Ignore a negative size since that corresponds to our back-annotation. */
7987 if (UI_Lt (uint_size, Uint_0))
7988 return;
7990 /* Only issue an error if a Value_Size clause was explicitly given.
7991 Otherwise, we'd be duplicating an error on the Size clause. */
7992 gnat_attr_node
7993 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
7995 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7996 but cannot be represented in bitsizetype. */
7997 size = UI_To_gnu (uint_size, bitsizetype);
7998 if (TREE_OVERFLOW (size))
8000 if (Present (gnat_attr_node))
8001 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
8002 gnat_entity);
8003 return;
8006 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8007 exists, or this is an integer type, in which case the front-end will
8008 have always set it. */
8009 if (No (gnat_attr_node)
8010 && integer_zerop (size)
8011 && !Has_Size_Clause (gnat_entity)
8012 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8013 return;
8015 old_size = rm_size (gnu_type);
8017 /* If the old size is self-referential, get the maximum size. */
8018 if (CONTAINS_PLACEHOLDER_P (old_size))
8019 old_size = max_size (old_size, true);
8021 /* Issue an error either if the old size of the object isn't a constant or
8022 if the new size is smaller than it. The front-end has already verified
8023 this for scalar and packed array types. */
8024 if (TREE_CODE (old_size) != INTEGER_CST
8025 || TREE_OVERFLOW (old_size)
8026 || (AGGREGATE_TYPE_P (gnu_type)
8027 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
8028 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
8029 && !(TYPE_IS_PADDING_P (gnu_type)
8030 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
8031 && TYPE_PACKED_ARRAY_TYPE_P
8032 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
8033 && tree_int_cst_lt (size, old_size)))
8035 if (Present (gnat_attr_node))
8036 post_error_ne_tree
8037 ("Value_Size for& too small{, minimum allowed is ^}",
8038 gnat_attr_node, gnat_entity, old_size);
8039 return;
8042 /* Otherwise, set the RM size proper for integral types... */
8043 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
8044 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
8045 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
8046 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
8047 SET_TYPE_RM_SIZE (gnu_type, size);
8049 /* ...or the Ada size for record and union types. */
8050 else if (RECORD_OR_UNION_TYPE_P (gnu_type)
8051 && !TYPE_FAT_POINTER_P (gnu_type))
8052 SET_TYPE_ADA_SIZE (gnu_type, size);
8055 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8056 a type or object whose present alignment is ALIGN. If this alignment is
8057 valid, return it. Otherwise, give an error and return ALIGN. */
8059 static unsigned int
8060 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8062 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8063 unsigned int new_align;
8064 Node_Id gnat_error_node;
8066 /* Don't worry about checking alignment if alignment was not specified
8067 by the source program and we already posted an error for this entity. */
8068 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8069 return align;
8071 /* Post the error on the alignment clause if any. Note, for the implicit
8072 base type of an array type, the alignment clause is on the first
8073 subtype. */
8074 if (Present (Alignment_Clause (gnat_entity)))
8075 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8077 else if (Is_Itype (gnat_entity)
8078 && Is_Array_Type (gnat_entity)
8079 && Etype (gnat_entity) == gnat_entity
8080 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8081 gnat_error_node =
8082 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8084 else
8085 gnat_error_node = gnat_entity;
8087 /* Within GCC, an alignment is an integer, so we must make sure a value is
8088 specified that fits in that range. Also, there is an upper bound to
8089 alignments we can support/allow. */
8090 if (!UI_Is_In_Int_Range (alignment)
8091 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8092 post_error_ne_num ("largest supported alignment for& is ^",
8093 gnat_error_node, gnat_entity, max_allowed_alignment);
8094 else if (!(Present (Alignment_Clause (gnat_entity))
8095 && From_At_Mod (Alignment_Clause (gnat_entity)))
8096 && new_align * BITS_PER_UNIT < align)
8098 unsigned int double_align;
8099 bool is_capped_double, align_clause;
8101 /* If the default alignment of "double" or larger scalar types is
8102 specifically capped and the new alignment is above the cap, do
8103 not post an error and change the alignment only if there is an
8104 alignment clause; this makes it possible to have the associated
8105 GCC type overaligned by default for performance reasons. */
8106 if ((double_align = double_float_alignment) > 0)
8108 Entity_Id gnat_type
8109 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8110 is_capped_double
8111 = is_double_float_or_array (gnat_type, &align_clause);
8113 else if ((double_align = double_scalar_alignment) > 0)
8115 Entity_Id gnat_type
8116 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8117 is_capped_double
8118 = is_double_scalar_or_array (gnat_type, &align_clause);
8120 else
8121 is_capped_double = align_clause = false;
8123 if (is_capped_double && new_align >= double_align)
8125 if (align_clause)
8126 align = new_align * BITS_PER_UNIT;
8128 else
8130 if (is_capped_double)
8131 align = double_align * BITS_PER_UNIT;
8133 post_error_ne_num ("alignment for& must be at least ^",
8134 gnat_error_node, gnat_entity,
8135 align / BITS_PER_UNIT);
8138 else
8140 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8141 if (new_align > align)
8142 align = new_align;
8145 return align;
8148 /* Verify that OBJECT, a type or decl, is something we can implement
8149 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
8150 if we require atomic components. */
8152 static void
8153 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
8155 Node_Id gnat_error_point = gnat_entity;
8156 Node_Id gnat_node;
8157 enum machine_mode mode;
8158 unsigned int align;
8159 tree size;
8161 /* There are three case of what OBJECT can be. It can be a type, in which
8162 case we take the size, alignment and mode from the type. It can be a
8163 declaration that was indirect, in which case the relevant values are
8164 that of the type being pointed to, or it can be a normal declaration,
8165 in which case the values are of the decl. The code below assumes that
8166 OBJECT is either a type or a decl. */
8167 if (TYPE_P (object))
8169 /* If this is an anonymous base type, nothing to check. Error will be
8170 reported on the source type. */
8171 if (!Comes_From_Source (gnat_entity))
8172 return;
8174 mode = TYPE_MODE (object);
8175 align = TYPE_ALIGN (object);
8176 size = TYPE_SIZE (object);
8178 else if (DECL_BY_REF_P (object))
8180 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
8181 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
8182 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
8184 else
8186 mode = DECL_MODE (object);
8187 align = DECL_ALIGN (object);
8188 size = DECL_SIZE (object);
8191 /* Consider all floating-point types atomic and any types that that are
8192 represented by integers no wider than a machine word. */
8193 if (GET_MODE_CLASS (mode) == MODE_FLOAT
8194 || ((GET_MODE_CLASS (mode) == MODE_INT
8195 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
8196 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8197 return;
8199 /* For the moment, also allow anything that has an alignment equal
8200 to its size and which is smaller than a word. */
8201 if (size && TREE_CODE (size) == INTEGER_CST
8202 && compare_tree_int (size, align) == 0
8203 && align <= BITS_PER_WORD)
8204 return;
8206 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
8207 gnat_node = Next_Rep_Item (gnat_node))
8209 if (!comp_p && Nkind (gnat_node) == N_Pragma
8210 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8211 == Pragma_Atomic))
8212 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8213 else if (comp_p && Nkind (gnat_node) == N_Pragma
8214 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8215 == Pragma_Atomic_Components))
8216 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8219 if (comp_p)
8220 post_error_ne ("atomic access to component of & cannot be guaranteed",
8221 gnat_error_point, gnat_entity);
8222 else
8223 post_error_ne ("atomic access to & cannot be guaranteed",
8224 gnat_error_point, gnat_entity);
8228 /* Helper for the intrin compatibility checks family. Evaluate whether
8229 two types are definitely incompatible. */
8231 static bool
8232 intrin_types_incompatible_p (tree t1, tree t2)
8234 enum tree_code code;
8236 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8237 return false;
8239 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8240 return true;
8242 if (TREE_CODE (t1) != TREE_CODE (t2))
8243 return true;
8245 code = TREE_CODE (t1);
8247 switch (code)
8249 case INTEGER_TYPE:
8250 case REAL_TYPE:
8251 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8253 case POINTER_TYPE:
8254 case REFERENCE_TYPE:
8255 /* Assume designated types are ok. We'd need to account for char * and
8256 void * variants to do better, which could rapidly get messy and isn't
8257 clearly worth the effort. */
8258 return false;
8260 default:
8261 break;
8264 return false;
8267 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8268 on the Ada/builtin argument lists for the INB binding. */
8270 static bool
8271 intrin_arglists_compatible_p (intrin_binding_t * inb)
8273 function_args_iterator ada_iter, btin_iter;
8275 function_args_iter_init (&ada_iter, inb->ada_fntype);
8276 function_args_iter_init (&btin_iter, inb->btin_fntype);
8278 /* Sequence position of the last argument we checked. */
8279 int argpos = 0;
8281 while (1)
8283 tree ada_type = function_args_iter_cond (&ada_iter);
8284 tree btin_type = function_args_iter_cond (&btin_iter);
8286 /* If we've exhausted both lists simultaneously, we're done. */
8287 if (ada_type == NULL_TREE && btin_type == NULL_TREE)
8288 break;
8290 /* If one list is shorter than the other, they fail to match. */
8291 if (ada_type == NULL_TREE || btin_type == NULL_TREE)
8292 return false;
8294 /* If we're done with the Ada args and not with the internal builtin
8295 args, or the other way around, complain. */
8296 if (ada_type == void_type_node
8297 && btin_type != void_type_node)
8299 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8300 return false;
8303 if (btin_type == void_type_node
8304 && ada_type != void_type_node)
8306 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8307 inb->gnat_entity, inb->gnat_entity, argpos);
8308 return false;
8311 /* Otherwise, check that types match for the current argument. */
8312 argpos ++;
8313 if (intrin_types_incompatible_p (ada_type, btin_type))
8315 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8316 inb->gnat_entity, inb->gnat_entity, argpos);
8317 return false;
8321 function_args_iter_next (&ada_iter);
8322 function_args_iter_next (&btin_iter);
8325 return true;
8328 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8329 on the Ada/builtin return values for the INB binding. */
8331 static bool
8332 intrin_return_compatible_p (intrin_binding_t * inb)
8334 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8335 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8337 /* Accept function imported as procedure, common and convenient. */
8338 if (VOID_TYPE_P (ada_return_type)
8339 && !VOID_TYPE_P (btin_return_type))
8340 return true;
8342 /* If return type is Address (integer type), map it to void *. */
8343 if (Is_Descendent_Of_Address (Etype (inb->gnat_entity)))
8344 ada_return_type = ptr_void_type_node;
8346 /* Check return types compatibility otherwise. Note that this
8347 handles void/void as well. */
8348 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8350 post_error ("?intrinsic binding type mismatch on return value!",
8351 inb->gnat_entity);
8352 return false;
8355 return true;
8358 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8359 compatible. Issue relevant warnings when they are not.
8361 This is intended as a light check to diagnose the most obvious cases, not
8362 as a full fledged type compatibility predicate. It is the programmer's
8363 responsibility to ensure correctness of the Ada declarations in Imports,
8364 especially when binding straight to a compiler internal. */
8366 static bool
8367 intrin_profiles_compatible_p (intrin_binding_t * inb)
8369 /* Check compatibility on return values and argument lists, each responsible
8370 for posting warnings as appropriate. Ensure use of the proper sloc for
8371 this purpose. */
8373 bool arglists_compatible_p, return_compatible_p;
8374 location_t saved_location = input_location;
8376 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8378 return_compatible_p = intrin_return_compatible_p (inb);
8379 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8381 input_location = saved_location;
8383 return return_compatible_p && arglists_compatible_p;
8386 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8387 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8388 specified size for this field. POS_LIST is a position list describing
8389 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8390 to this layout. */
8392 static tree
8393 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8394 tree size, tree pos_list,
8395 vec<subst_pair> subst_list)
8397 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8398 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8399 unsigned int offset_align = tree_to_uhwi (TREE_VEC_ELT (t, 1));
8400 tree new_pos, new_field;
8401 unsigned int i;
8402 subst_pair *s;
8404 if (CONTAINS_PLACEHOLDER_P (pos))
8405 FOR_EACH_VEC_ELT (subst_list, i, s)
8406 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8408 /* If the position is now a constant, we can set it as the position of the
8409 field when we make it. Otherwise, we need to deal with it specially. */
8410 if (TREE_CONSTANT (pos))
8411 new_pos = bit_from_pos (pos, bitpos);
8412 else
8413 new_pos = NULL_TREE;
8415 new_field
8416 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8417 size, new_pos, DECL_PACKED (old_field),
8418 !DECL_NONADDRESSABLE_P (old_field));
8420 if (!new_pos)
8422 normalize_offset (&pos, &bitpos, offset_align);
8423 /* Finalize the position. */
8424 DECL_FIELD_OFFSET (new_field) = variable_size (pos);
8425 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8426 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8427 DECL_SIZE (new_field) = size;
8428 DECL_SIZE_UNIT (new_field)
8429 = convert (sizetype,
8430 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8431 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8434 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8435 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8436 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8437 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8439 return new_field;
8442 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8443 it is the minimal size the REP_PART must have. */
8445 static tree
8446 create_rep_part (tree rep_type, tree record_type, tree min_size)
8448 tree field;
8450 if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8451 min_size = NULL_TREE;
8453 field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8454 min_size, NULL_TREE, 0, 1);
8455 DECL_INTERNAL_P (field) = 1;
8457 return field;
8460 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8462 static tree
8463 get_rep_part (tree record_type)
8465 tree field = TYPE_FIELDS (record_type);
8467 /* The REP part is the first field, internal, another record, and its name
8468 starts with an 'R'. */
8469 if (field
8470 && DECL_INTERNAL_P (field)
8471 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8472 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8473 return field;
8475 return NULL_TREE;
8478 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8480 tree
8481 get_variant_part (tree record_type)
8483 tree field;
8485 /* The variant part is the only internal field that is a qualified union. */
8486 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8487 if (DECL_INTERNAL_P (field)
8488 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8489 return field;
8491 return NULL_TREE;
8494 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8495 the list of variants to be used and RECORD_TYPE is the type of the parent.
8496 POS_LIST is a position list describing the layout of fields present in
8497 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8498 layout. */
8500 static tree
8501 create_variant_part_from (tree old_variant_part,
8502 vec<variant_desc> variant_list,
8503 tree record_type, tree pos_list,
8504 vec<subst_pair> subst_list)
8506 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8507 tree old_union_type = TREE_TYPE (old_variant_part);
8508 tree new_union_type, new_variant_part;
8509 tree union_field_list = NULL_TREE;
8510 variant_desc *v;
8511 unsigned int i;
8513 /* First create the type of the variant part from that of the old one. */
8514 new_union_type = make_node (QUAL_UNION_TYPE);
8515 TYPE_NAME (new_union_type)
8516 = concat_name (TYPE_NAME (record_type),
8517 IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8519 /* If the position of the variant part is constant, subtract it from the
8520 size of the type of the parent to get the new size. This manual CSE
8521 reduces the code size when not optimizing. */
8522 if (TREE_CODE (offset) == INTEGER_CST)
8524 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8525 tree first_bit = bit_from_pos (offset, bitpos);
8526 TYPE_SIZE (new_union_type)
8527 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8528 TYPE_SIZE_UNIT (new_union_type)
8529 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8530 byte_from_pos (offset, bitpos));
8531 SET_TYPE_ADA_SIZE (new_union_type,
8532 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8533 first_bit));
8534 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8535 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8537 else
8538 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8540 /* Now finish up the new variants and populate the union type. */
8541 FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8543 tree old_field = v->field, new_field;
8544 tree old_variant, old_variant_subpart, new_variant, field_list;
8546 /* Skip variants that don't belong to this nesting level. */
8547 if (DECL_CONTEXT (old_field) != old_union_type)
8548 continue;
8550 /* Retrieve the list of fields already added to the new variant. */
8551 new_variant = v->new_type;
8552 field_list = TYPE_FIELDS (new_variant);
8554 /* If the old variant had a variant subpart, we need to create a new
8555 variant subpart and add it to the field list. */
8556 old_variant = v->type;
8557 old_variant_subpart = get_variant_part (old_variant);
8558 if (old_variant_subpart)
8560 tree new_variant_subpart
8561 = create_variant_part_from (old_variant_subpart, variant_list,
8562 new_variant, pos_list, subst_list);
8563 DECL_CHAIN (new_variant_subpart) = field_list;
8564 field_list = new_variant_subpart;
8567 /* Finish up the new variant and create the field. No need for debug
8568 info thanks to the XVS type. */
8569 finish_record_type (new_variant, nreverse (field_list), 2, false);
8570 compute_record_mode (new_variant);
8571 create_type_decl (TYPE_NAME (new_variant), new_variant, true, false,
8572 Empty);
8574 new_field
8575 = create_field_decl_from (old_field, new_variant, new_union_type,
8576 TYPE_SIZE (new_variant),
8577 pos_list, subst_list);
8578 DECL_QUALIFIER (new_field) = v->qual;
8579 DECL_INTERNAL_P (new_field) = 1;
8580 DECL_CHAIN (new_field) = union_field_list;
8581 union_field_list = new_field;
8584 /* Finish up the union type and create the variant part. No need for debug
8585 info thanks to the XVS type. Note that we don't reverse the field list
8586 because VARIANT_LIST has been traversed in reverse order. */
8587 finish_record_type (new_union_type, union_field_list, 2, false);
8588 compute_record_mode (new_union_type);
8589 create_type_decl (TYPE_NAME (new_union_type), new_union_type, true, false,
8590 Empty);
8592 new_variant_part
8593 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8594 TYPE_SIZE (new_union_type),
8595 pos_list, subst_list);
8596 DECL_INTERNAL_P (new_variant_part) = 1;
8598 /* With multiple discriminants it is possible for an inner variant to be
8599 statically selected while outer ones are not; in this case, the list
8600 of fields of the inner variant is not flattened and we end up with a
8601 qualified union with a single member. Drop the useless container. */
8602 if (!DECL_CHAIN (union_field_list))
8604 DECL_CONTEXT (union_field_list) = record_type;
8605 DECL_FIELD_OFFSET (union_field_list)
8606 = DECL_FIELD_OFFSET (new_variant_part);
8607 DECL_FIELD_BIT_OFFSET (union_field_list)
8608 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8609 SET_DECL_OFFSET_ALIGN (union_field_list,
8610 DECL_OFFSET_ALIGN (new_variant_part));
8611 new_variant_part = union_field_list;
8614 return new_variant_part;
8617 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8618 which are both RECORD_TYPE, after applying the substitutions described
8619 in SUBST_LIST. */
8621 static void
8622 copy_and_substitute_in_size (tree new_type, tree old_type,
8623 vec<subst_pair> subst_list)
8625 unsigned int i;
8626 subst_pair *s;
8628 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8629 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8630 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8631 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8632 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8634 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8635 FOR_EACH_VEC_ELT (subst_list, i, s)
8636 TYPE_SIZE (new_type)
8637 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8638 s->discriminant, s->replacement);
8640 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8641 FOR_EACH_VEC_ELT (subst_list, i, s)
8642 TYPE_SIZE_UNIT (new_type)
8643 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8644 s->discriminant, s->replacement);
8646 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8647 FOR_EACH_VEC_ELT (subst_list, i, s)
8648 SET_TYPE_ADA_SIZE
8649 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8650 s->discriminant, s->replacement));
8652 /* Finalize the size. */
8653 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8654 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8657 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8658 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8659 updated by replacing F with R.
8661 The function doesn't update the layout of the type, i.e. it assumes
8662 that the substitution is purely formal. That's why the replacement
8663 value R must itself contain a PLACEHOLDER_EXPR. */
8665 tree
8666 substitute_in_type (tree t, tree f, tree r)
8668 tree nt;
8670 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8672 switch (TREE_CODE (t))
8674 case INTEGER_TYPE:
8675 case ENUMERAL_TYPE:
8676 case BOOLEAN_TYPE:
8677 case REAL_TYPE:
8679 /* First the domain types of arrays. */
8680 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8681 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8683 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8684 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8686 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8687 return t;
8689 nt = copy_type (t);
8690 TYPE_GCC_MIN_VALUE (nt) = low;
8691 TYPE_GCC_MAX_VALUE (nt) = high;
8693 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8694 SET_TYPE_INDEX_TYPE
8695 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8697 return nt;
8700 /* Then the subtypes. */
8701 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8702 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8704 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8705 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8707 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8708 return t;
8710 nt = copy_type (t);
8711 SET_TYPE_RM_MIN_VALUE (nt, low);
8712 SET_TYPE_RM_MAX_VALUE (nt, high);
8714 return nt;
8717 return t;
8719 case COMPLEX_TYPE:
8720 nt = substitute_in_type (TREE_TYPE (t), f, r);
8721 if (nt == TREE_TYPE (t))
8722 return t;
8724 return build_complex_type (nt);
8726 case FUNCTION_TYPE:
8727 /* These should never show up here. */
8728 gcc_unreachable ();
8730 case ARRAY_TYPE:
8732 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8733 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8735 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8736 return t;
8738 nt = build_nonshared_array_type (component, domain);
8739 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8740 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8741 SET_TYPE_MODE (nt, TYPE_MODE (t));
8742 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8743 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8744 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8745 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8746 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8747 return nt;
8750 case RECORD_TYPE:
8751 case UNION_TYPE:
8752 case QUAL_UNION_TYPE:
8754 bool changed_field = false;
8755 tree field;
8757 /* Start out with no fields, make new fields, and chain them
8758 in. If we haven't actually changed the type of any field,
8759 discard everything we've done and return the old type. */
8760 nt = copy_type (t);
8761 TYPE_FIELDS (nt) = NULL_TREE;
8763 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8765 tree new_field = copy_node (field), new_n;
8767 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8768 if (new_n != TREE_TYPE (field))
8770 TREE_TYPE (new_field) = new_n;
8771 changed_field = true;
8774 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8775 if (new_n != DECL_FIELD_OFFSET (field))
8777 DECL_FIELD_OFFSET (new_field) = new_n;
8778 changed_field = true;
8781 /* Do the substitution inside the qualifier, if any. */
8782 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8784 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8785 if (new_n != DECL_QUALIFIER (field))
8787 DECL_QUALIFIER (new_field) = new_n;
8788 changed_field = true;
8792 DECL_CONTEXT (new_field) = nt;
8793 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8795 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8796 TYPE_FIELDS (nt) = new_field;
8799 if (!changed_field)
8800 return t;
8802 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8803 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8804 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8805 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8806 return nt;
8809 default:
8810 return t;
8814 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8815 needed to represent the object. */
8817 tree
8818 rm_size (tree gnu_type)
8820 /* For integral types, we store the RM size explicitly. */
8821 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8822 return TYPE_RM_SIZE (gnu_type);
8824 /* Return the RM size of the actual data plus the size of the template. */
8825 if (TREE_CODE (gnu_type) == RECORD_TYPE
8826 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8827 return
8828 size_binop (PLUS_EXPR,
8829 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8830 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8832 /* For record or union types, we store the size explicitly. */
8833 if (RECORD_OR_UNION_TYPE_P (gnu_type)
8834 && !TYPE_FAT_POINTER_P (gnu_type)
8835 && TYPE_ADA_SIZE (gnu_type))
8836 return TYPE_ADA_SIZE (gnu_type);
8838 /* For other types, this is just the size. */
8839 return TYPE_SIZE (gnu_type);
8842 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8843 fully-qualified name, possibly with type information encoding.
8844 Otherwise, return the name. */
8846 tree
8847 get_entity_name (Entity_Id gnat_entity)
8849 Get_Encoded_Name (gnat_entity);
8850 return get_identifier_with_length (Name_Buffer, Name_Len);
8853 /* Return an identifier representing the external name to be used for
8854 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8855 and the specified suffix. */
8857 tree
8858 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8860 const Entity_Kind kind = Ekind (gnat_entity);
8861 const bool has_suffix = (suffix != NULL);
8862 String_Template temp = {1, has_suffix ? strlen (suffix) : 0};
8863 String_Pointer sp = {suffix, &temp};
8865 Get_External_Name (gnat_entity, has_suffix, sp);
8867 /* A variable using the Stdcall convention lives in a DLL. We adjust
8868 its name to use the jump table, the _imp__NAME contains the address
8869 for the NAME variable. */
8870 if ((kind == E_Variable || kind == E_Constant)
8871 && Has_Stdcall_Convention (gnat_entity))
8873 const int len = strlen (STDCALL_PREFIX) + Name_Len;
8874 char *new_name = (char *) alloca (len + 1);
8875 strcpy (new_name, STDCALL_PREFIX);
8876 strcat (new_name, Name_Buffer);
8877 return get_identifier_with_length (new_name, len);
8880 return get_identifier_with_length (Name_Buffer, Name_Len);
8883 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8884 string, return a new IDENTIFIER_NODE that is the concatenation of
8885 the name followed by "___" and the specified suffix. */
8887 tree
8888 concat_name (tree gnu_name, const char *suffix)
8890 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8891 char *new_name = (char *) alloca (len + 1);
8892 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8893 strcat (new_name, "___");
8894 strcat (new_name, suffix);
8895 return get_identifier_with_length (new_name, len);
8898 /* Initialize data structures of the decl.c module. */
8900 void
8901 init_gnat_decl (void)
8903 /* Initialize the cache of annotated values. */
8904 annotate_value_cache
8905 = htab_create_ggc (512, tree_int_map_hash, tree_int_map_eq, 0);
8908 /* Destroy data structures of the decl.c module. */
8910 void
8911 destroy_gnat_decl (void)
8913 /* Destroy the cache of annotated values. */
8914 htab_delete (annotate_value_cache);
8915 annotate_value_cache = NULL;
8918 #include "gt-ada-decl.h"