* gcc-interface/decl.c (gnat_to_gnu_entity) <E_Array_Type>: Do not make
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blob7a18d32752ae71828a2356a71a144f3d23cacb16
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2011, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License along with GCC; see the file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
20 * *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
23 * *
24 ****************************************************************************/
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "flags.h"
32 #include "toplev.h"
33 #include "ggc.h"
34 #include "target.h"
35 #include "tree-inline.h"
37 #include "ada.h"
38 #include "types.h"
39 #include "atree.h"
40 #include "elists.h"
41 #include "namet.h"
42 #include "nlists.h"
43 #include "repinfo.h"
44 #include "snames.h"
45 #include "stringt.h"
46 #include "uintp.h"
47 #include "fe.h"
48 #include "sinfo.h"
49 #include "einfo.h"
50 #include "ada-tree.h"
51 #include "gigi.h"
53 /* Convention_Stdcall should be processed in a specific way on 32 bits
54 Windows targets only. The macro below is a helper to avoid having to
55 check for a Windows specific attribute throughout this unit. */
57 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
58 #ifdef TARGET_64BIT
59 #define Has_Stdcall_Convention(E) \
60 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
61 #else
62 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
63 #endif
64 #else
65 #define Has_Stdcall_Convention(E) 0
66 #endif
68 /* Stack realignment is necessary for functions with foreign conventions when
69 the ABI doesn't mandate as much as what the compiler assumes - that is, up
70 to PREFERRED_STACK_BOUNDARY.
72 Such realignment can be requested with a dedicated function type attribute
73 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
74 characterize the situations where the attribute should be set. We rely on
75 compiler configuration settings for 'main' to decide. */
77 #ifdef MAIN_STACK_BOUNDARY
78 #define FOREIGN_FORCE_REALIGN_STACK \
79 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
80 #else
81 #define FOREIGN_FORCE_REALIGN_STACK 0
82 #endif
84 struct incomplete
86 struct incomplete *next;
87 tree old_type;
88 Entity_Id full_type;
91 /* These variables are used to defer recursively expanding incomplete types
92 while we are processing an array, a record or a subprogram type. */
93 static int defer_incomplete_level = 0;
94 static struct incomplete *defer_incomplete_list;
96 /* This variable is used to delay expanding From_With_Type types until the
97 end of the spec. */
98 static struct incomplete *defer_limited_with;
100 /* These variables are used to defer finalizing types. The element of the
101 list is the TYPE_DECL associated with the type. */
102 static int defer_finalize_level = 0;
103 static VEC (tree,heap) *defer_finalize_list;
105 typedef struct subst_pair_d {
106 tree discriminant;
107 tree replacement;
108 } subst_pair;
110 DEF_VEC_O(subst_pair);
111 DEF_VEC_ALLOC_O(subst_pair,heap);
113 typedef struct variant_desc_d {
114 /* The type of the variant. */
115 tree type;
117 /* The associated field. */
118 tree field;
120 /* The value of the qualifier. */
121 tree qual;
123 /* The record associated with this variant. */
124 tree record;
125 } variant_desc;
127 DEF_VEC_O(variant_desc);
128 DEF_VEC_ALLOC_O(variant_desc,heap);
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 enum alias_set_op
136 ALIAS_SET_COPY,
137 ALIAS_SET_SUBSET,
138 ALIAS_SET_SUPERSET
141 static void relate_alias_sets (tree, tree, enum alias_set_op);
143 static bool allocatable_size_p (tree, bool);
144 static void prepend_one_attribute_to (struct attrib **,
145 enum attr_type, tree, tree, Node_Id);
146 static void prepend_attributes (Entity_Id, struct attrib **);
147 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
148 static bool is_variable_size (tree);
149 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
150 static tree elaborate_expression_2 (tree, Entity_Id, tree, bool, bool,
151 unsigned int);
152 static tree make_packable_type (tree, bool);
153 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
154 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
155 bool *);
156 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
157 static bool same_discriminant_p (Entity_Id, Entity_Id);
158 static bool array_type_has_nonaliased_component (tree, Entity_Id);
159 static bool compile_time_known_address_p (Node_Id);
160 static bool cannot_be_superflat_p (Node_Id);
161 static bool constructor_address_p (tree);
162 static void components_to_record (tree, Node_Id, tree, int, bool, bool, bool,
163 bool, bool, bool, bool, tree *);
164 static Uint annotate_value (tree);
165 static void annotate_rep (Entity_Id, tree);
166 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
167 static VEC(subst_pair,heap) *build_subst_list (Entity_Id, Entity_Id, bool);
168 static VEC(variant_desc,heap) *build_variant_list (tree,
169 VEC(subst_pair,heap) *,
170 VEC(variant_desc,heap) *);
171 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
172 static void set_rm_size (Uint, tree, Entity_Id);
173 static tree make_type_from_size (tree, tree, bool);
174 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
175 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
176 static void check_ok_for_atomic (tree, Entity_Id, bool);
177 static tree create_field_decl_from (tree, tree, tree, tree, tree,
178 VEC(subst_pair,heap) *);
179 static tree get_rep_part (tree);
180 static tree get_variant_part (tree);
181 static tree create_variant_part_from (tree, VEC(variant_desc,heap) *, tree,
182 tree, VEC(subst_pair,heap) *);
183 static void copy_and_substitute_in_size (tree, tree, VEC(subst_pair,heap) *);
184 static void rest_of_type_decl_compilation_no_defer (tree);
186 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
187 to pass around calls performing profile compatibility checks. */
189 typedef struct {
190 Entity_Id gnat_entity; /* The Ada subprogram entity. */
191 tree ada_fntype; /* The corresponding GCC type node. */
192 tree btin_fntype; /* The GCC builtin function type node. */
193 } intrin_binding_t;
195 static bool intrin_profiles_compatible_p (intrin_binding_t *);
197 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
198 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
199 and associate the ..._DECL node with the input GNAT defining identifier.
201 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
202 initial value (in GCC tree form). This is optional for a variable. For
203 a renamed entity, GNU_EXPR gives the object being renamed.
205 DEFINITION is nonzero if this call is intended for a definition. This is
206 used for separate compilation where it is necessary to know whether an
207 external declaration or a definition must be created if the GCC equivalent
208 was not created previously. The value of 1 is normally used for a nonzero
209 DEFINITION, but a value of 2 is used in special circumstances, defined in
210 the code. */
212 tree
213 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
215 /* Contains the kind of the input GNAT node. */
216 const Entity_Kind kind = Ekind (gnat_entity);
217 /* True if this is a type. */
218 const bool is_type = IN (kind, Type_Kind);
219 /* True if debug info is requested for this entity. */
220 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
221 /* True if this entity is to be considered as imported. */
222 const bool imported_p
223 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
224 /* For a type, contains the equivalent GNAT node to be used in gigi. */
225 Entity_Id gnat_equiv_type = Empty;
226 /* Temporary used to walk the GNAT tree. */
227 Entity_Id gnat_temp;
228 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
229 This node will be associated with the GNAT node by calling at the end
230 of the `switch' statement. */
231 tree gnu_decl = NULL_TREE;
232 /* Contains the GCC type to be used for the GCC node. */
233 tree gnu_type = NULL_TREE;
234 /* Contains the GCC size tree to be used for the GCC node. */
235 tree gnu_size = NULL_TREE;
236 /* Contains the GCC name to be used for the GCC node. */
237 tree gnu_entity_name;
238 /* True if we have already saved gnu_decl as a GNAT association. */
239 bool saved = false;
240 /* True if we incremented defer_incomplete_level. */
241 bool this_deferred = false;
242 /* True if we incremented force_global. */
243 bool this_global = false;
244 /* True if we should check to see if elaborated during processing. */
245 bool maybe_present = false;
246 /* True if we made GNU_DECL and its type here. */
247 bool this_made_decl = false;
248 /* Size and alignment of the GCC node, if meaningful. */
249 unsigned int esize = 0, align = 0;
250 /* Contains the list of attributes directly attached to the entity. */
251 struct attrib *attr_list = NULL;
253 /* Since a use of an Itype is a definition, process it as such if it
254 is not in a with'ed unit. */
255 if (!definition
256 && is_type
257 && Is_Itype (gnat_entity)
258 && !present_gnu_tree (gnat_entity)
259 && In_Extended_Main_Code_Unit (gnat_entity))
261 /* Ensure that we are in a subprogram mentioned in the Scope chain of
262 this entity, our current scope is global, or we encountered a task
263 or entry (where we can't currently accurately check scoping). */
264 if (!current_function_decl
265 || DECL_ELABORATION_PROC_P (current_function_decl))
267 process_type (gnat_entity);
268 return get_gnu_tree (gnat_entity);
271 for (gnat_temp = Scope (gnat_entity);
272 Present (gnat_temp);
273 gnat_temp = Scope (gnat_temp))
275 if (Is_Type (gnat_temp))
276 gnat_temp = Underlying_Type (gnat_temp);
278 if (Ekind (gnat_temp) == E_Subprogram_Body)
279 gnat_temp
280 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
282 if (IN (Ekind (gnat_temp), Subprogram_Kind)
283 && Present (Protected_Body_Subprogram (gnat_temp)))
284 gnat_temp = Protected_Body_Subprogram (gnat_temp);
286 if (Ekind (gnat_temp) == E_Entry
287 || Ekind (gnat_temp) == E_Entry_Family
288 || Ekind (gnat_temp) == E_Task_Type
289 || (IN (Ekind (gnat_temp), Subprogram_Kind)
290 && present_gnu_tree (gnat_temp)
291 && (current_function_decl
292 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
294 process_type (gnat_entity);
295 return get_gnu_tree (gnat_entity);
299 /* This abort means the Itype has an incorrect scope, i.e. that its
300 scope does not correspond to the subprogram it is declared in. */
301 gcc_unreachable ();
304 /* If we've already processed this entity, return what we got last time.
305 If we are defining the node, we should not have already processed it.
306 In that case, we will abort below when we try to save a new GCC tree
307 for this object. We also need to handle the case of getting a dummy
308 type when a Full_View exists. */
309 if ((!definition || (is_type && imported_p))
310 && present_gnu_tree (gnat_entity))
312 gnu_decl = get_gnu_tree (gnat_entity);
314 if (TREE_CODE (gnu_decl) == TYPE_DECL
315 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
316 && IN (kind, Incomplete_Or_Private_Kind)
317 && Present (Full_View (gnat_entity)))
319 gnu_decl
320 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
321 save_gnu_tree (gnat_entity, NULL_TREE, false);
322 save_gnu_tree (gnat_entity, gnu_decl, false);
325 return gnu_decl;
328 /* If this is a numeric or enumeral type, or an access type, a nonzero
329 Esize must be specified unless it was specified by the programmer. */
330 gcc_assert (!Unknown_Esize (gnat_entity)
331 || Has_Size_Clause (gnat_entity)
332 || (!IN (kind, Numeric_Kind)
333 && !IN (kind, Enumeration_Kind)
334 && (!IN (kind, Access_Kind)
335 || kind == E_Access_Protected_Subprogram_Type
336 || kind == E_Anonymous_Access_Protected_Subprogram_Type
337 || kind == E_Access_Subtype)));
339 /* The RM size must be specified for all discrete and fixed-point types. */
340 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
341 && Unknown_RM_Size (gnat_entity)));
343 /* If we get here, it means we have not yet done anything with this entity.
344 If we are not defining it, it must be a type or an entity that is defined
345 elsewhere or externally, otherwise we should have defined it already. */
346 gcc_assert (definition
347 || type_annotate_only
348 || is_type
349 || kind == E_Discriminant
350 || kind == E_Component
351 || kind == E_Label
352 || (kind == E_Constant && Present (Full_View (gnat_entity)))
353 || Is_Public (gnat_entity));
355 /* Get the name of the entity and set up the line number and filename of
356 the original definition for use in any decl we make. */
357 gnu_entity_name = get_entity_name (gnat_entity);
358 Sloc_to_locus (Sloc (gnat_entity), &input_location);
360 /* For cases when we are not defining (i.e., we are referencing from
361 another compilation unit) public entities, show we are at global level
362 for the purpose of computing scopes. Don't do this for components or
363 discriminants since the relevant test is whether or not the record is
364 being defined. Don't do this for constants either as we'll look into
365 their defining expression in the local context. */
366 if (!definition
367 && kind != E_Component
368 && kind != E_Discriminant
369 && kind != E_Constant
370 && Is_Public (gnat_entity)
371 && !Is_Statically_Allocated (gnat_entity))
372 force_global++, this_global = true;
374 /* Handle any attributes directly attached to the entity. */
375 if (Has_Gigi_Rep_Item (gnat_entity))
376 prepend_attributes (gnat_entity, &attr_list);
378 /* Do some common processing for types. */
379 if (is_type)
381 /* Compute the equivalent type to be used in gigi. */
382 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
384 /* Machine_Attributes on types are expected to be propagated to
385 subtypes. The corresponding Gigi_Rep_Items are only attached
386 to the first subtype though, so we handle the propagation here. */
387 if (Base_Type (gnat_entity) != gnat_entity
388 && !Is_First_Subtype (gnat_entity)
389 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
390 prepend_attributes (First_Subtype (Base_Type (gnat_entity)),
391 &attr_list);
393 /* Compute a default value for the size of the type. */
394 if (Known_Esize (gnat_entity)
395 && UI_Is_In_Int_Range (Esize (gnat_entity)))
397 unsigned int max_esize;
398 esize = UI_To_Int (Esize (gnat_entity));
400 if (IN (kind, Float_Kind))
401 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
402 else if (IN (kind, Access_Kind))
403 max_esize = POINTER_SIZE * 2;
404 else
405 max_esize = LONG_LONG_TYPE_SIZE;
407 if (esize > max_esize)
408 esize = max_esize;
410 else
411 esize = LONG_LONG_TYPE_SIZE;
414 switch (kind)
416 case E_Constant:
417 /* If this is a use of a deferred constant without address clause,
418 get its full definition. */
419 if (!definition
420 && No (Address_Clause (gnat_entity))
421 && Present (Full_View (gnat_entity)))
423 gnu_decl
424 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
425 saved = true;
426 break;
429 /* If we have an external constant that we are not defining, get the
430 expression that is was defined to represent. We may throw it away
431 later if it is not a constant. But do not retrieve the expression
432 if it is an allocator because the designated type might be dummy
433 at this point. */
434 if (!definition
435 && !No_Initialization (Declaration_Node (gnat_entity))
436 && Present (Expression (Declaration_Node (gnat_entity)))
437 && Nkind (Expression (Declaration_Node (gnat_entity)))
438 != N_Allocator)
440 bool went_into_elab_proc = false;
442 /* The expression may contain N_Expression_With_Actions nodes and
443 thus object declarations from other units. In this case, even
444 though the expression will eventually be discarded since not a
445 constant, the declarations would be stuck either in the global
446 varpool or in the current scope. Therefore we force the local
447 context and create a fake scope that we'll zap at the end. */
448 if (!current_function_decl)
450 current_function_decl = get_elaboration_procedure ();
451 went_into_elab_proc = true;
453 gnat_pushlevel ();
455 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
457 gnat_zaplevel ();
458 if (went_into_elab_proc)
459 current_function_decl = NULL_TREE;
462 /* Ignore deferred constant definitions without address clause since
463 they are processed fully in the front-end. If No_Initialization
464 is set, this is not a deferred constant but a constant whose value
465 is built manually. And constants that are renamings are handled
466 like variables. */
467 if (definition
468 && !gnu_expr
469 && No (Address_Clause (gnat_entity))
470 && !No_Initialization (Declaration_Node (gnat_entity))
471 && No (Renamed_Object (gnat_entity)))
473 gnu_decl = error_mark_node;
474 saved = true;
475 break;
478 /* Ignore constant definitions already marked with the error node. See
479 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
480 if (definition
481 && gnu_expr
482 && present_gnu_tree (gnat_entity)
483 && get_gnu_tree (gnat_entity) == error_mark_node)
485 maybe_present = true;
486 break;
489 goto object;
491 case E_Exception:
492 /* We used to special case VMS exceptions here to directly map them to
493 their associated condition code. Since this code had to be masked
494 dynamically to strip off the severity bits, this caused trouble in
495 the GCC/ZCX case because the "type" pointers we store in the tables
496 have to be static. We now don't special case here anymore, and let
497 the regular processing take place, which leaves us with a regular
498 exception data object for VMS exceptions too. The condition code
499 mapping is taken care of by the front end and the bitmasking by the
500 run-time library. */
501 goto object;
503 case E_Discriminant:
504 case E_Component:
506 /* The GNAT record where the component was defined. */
507 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
509 /* If the variable is an inherited record component (in the case of
510 extended record types), just return the inherited entity, which
511 must be a FIELD_DECL. Likewise for discriminants.
512 For discriminants of untagged records which have explicit
513 stored discriminants, return the entity for the corresponding
514 stored discriminant. Also use Original_Record_Component
515 if the record has a private extension. */
516 if (Present (Original_Record_Component (gnat_entity))
517 && Original_Record_Component (gnat_entity) != gnat_entity)
519 gnu_decl
520 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
521 gnu_expr, definition);
522 saved = true;
523 break;
526 /* If the enclosing record has explicit stored discriminants,
527 then it is an untagged record. If the Corresponding_Discriminant
528 is not empty then this must be a renamed discriminant and its
529 Original_Record_Component must point to the corresponding explicit
530 stored discriminant (i.e. we should have taken the previous
531 branch). */
532 else if (Present (Corresponding_Discriminant (gnat_entity))
533 && Is_Tagged_Type (gnat_record))
535 /* A tagged record has no explicit stored discriminants. */
536 gcc_assert (First_Discriminant (gnat_record)
537 == First_Stored_Discriminant (gnat_record));
538 gnu_decl
539 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
540 gnu_expr, definition);
541 saved = true;
542 break;
545 else if (Present (CR_Discriminant (gnat_entity))
546 && type_annotate_only)
548 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
549 gnu_expr, definition);
550 saved = true;
551 break;
554 /* If the enclosing record has explicit stored discriminants, then
555 it is an untagged record. If the Corresponding_Discriminant
556 is not empty then this must be a renamed discriminant and its
557 Original_Record_Component must point to the corresponding explicit
558 stored discriminant (i.e. we should have taken the first
559 branch). */
560 else if (Present (Corresponding_Discriminant (gnat_entity))
561 && (First_Discriminant (gnat_record)
562 != First_Stored_Discriminant (gnat_record)))
563 gcc_unreachable ();
565 /* Otherwise, if we are not defining this and we have no GCC type
566 for the containing record, make one for it. Then we should
567 have made our own equivalent. */
568 else if (!definition && !present_gnu_tree (gnat_record))
570 /* ??? If this is in a record whose scope is a protected
571 type and we have an Original_Record_Component, use it.
572 This is a workaround for major problems in protected type
573 handling. */
574 Entity_Id Scop = Scope (Scope (gnat_entity));
575 if ((Is_Protected_Type (Scop)
576 || (Is_Private_Type (Scop)
577 && Present (Full_View (Scop))
578 && Is_Protected_Type (Full_View (Scop))))
579 && Present (Original_Record_Component (gnat_entity)))
581 gnu_decl
582 = gnat_to_gnu_entity (Original_Record_Component
583 (gnat_entity),
584 gnu_expr, 0);
585 saved = true;
586 break;
589 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
590 gnu_decl = get_gnu_tree (gnat_entity);
591 saved = true;
592 break;
595 else
596 /* Here we have no GCC type and this is a reference rather than a
597 definition. This should never happen. Most likely the cause is
598 reference before declaration in the gnat tree for gnat_entity. */
599 gcc_unreachable ();
602 case E_Loop_Parameter:
603 case E_Out_Parameter:
604 case E_Variable:
606 /* Simple variables, loop variables, Out parameters and exceptions. */
607 object:
609 bool const_flag
610 = ((kind == E_Constant || kind == E_Variable)
611 && Is_True_Constant (gnat_entity)
612 && !Treat_As_Volatile (gnat_entity)
613 && (((Nkind (Declaration_Node (gnat_entity))
614 == N_Object_Declaration)
615 && Present (Expression (Declaration_Node (gnat_entity))))
616 || Present (Renamed_Object (gnat_entity))
617 || imported_p));
618 bool inner_const_flag = const_flag;
619 bool static_p = Is_Statically_Allocated (gnat_entity);
620 bool mutable_p = false;
621 bool used_by_ref = false;
622 tree gnu_ext_name = NULL_TREE;
623 tree renamed_obj = NULL_TREE;
624 tree gnu_object_size;
626 if (Present (Renamed_Object (gnat_entity)) && !definition)
628 if (kind == E_Exception)
629 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
630 NULL_TREE, 0);
631 else
632 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
635 /* Get the type after elaborating the renamed object. */
636 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
638 /* If this is a standard exception definition, then use the standard
639 exception type. This is necessary to make sure that imported and
640 exported views of exceptions are properly merged in LTO mode. */
641 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
642 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
643 gnu_type = except_type_node;
645 /* For a debug renaming declaration, build a debug-only entity. */
646 if (Present (Debug_Renaming_Link (gnat_entity)))
648 /* Force a non-null value to make sure the symbol is retained. */
649 tree value = build1 (INDIRECT_REF, gnu_type,
650 build1 (NOP_EXPR,
651 build_pointer_type (gnu_type),
652 integer_minus_one_node));
653 gnu_decl = build_decl (input_location,
654 VAR_DECL, gnu_entity_name, gnu_type);
655 SET_DECL_VALUE_EXPR (gnu_decl, value);
656 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
657 gnat_pushdecl (gnu_decl, gnat_entity);
658 break;
661 /* If this is a loop variable, its type should be the base type.
662 This is because the code for processing a loop determines whether
663 a normal loop end test can be done by comparing the bounds of the
664 loop against those of the base type, which is presumed to be the
665 size used for computation. But this is not correct when the size
666 of the subtype is smaller than the type. */
667 if (kind == E_Loop_Parameter)
668 gnu_type = get_base_type (gnu_type);
670 /* Reject non-renamed objects whose type is an unconstrained array or
671 any object whose type is a dummy type or void. */
672 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
673 && No (Renamed_Object (gnat_entity)))
674 || TYPE_IS_DUMMY_P (gnu_type)
675 || TREE_CODE (gnu_type) == VOID_TYPE)
677 gcc_assert (type_annotate_only);
678 if (this_global)
679 force_global--;
680 return error_mark_node;
683 /* If an alignment is specified, use it if valid. Note that exceptions
684 are objects but don't have an alignment. We must do this before we
685 validate the size, since the alignment can affect the size. */
686 if (kind != E_Exception && Known_Alignment (gnat_entity))
688 gcc_assert (Present (Alignment (gnat_entity)));
690 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
691 TYPE_ALIGN (gnu_type));
693 /* No point in changing the type if there is an address clause
694 as the final type of the object will be a reference type. */
695 if (Present (Address_Clause (gnat_entity)))
696 align = 0;
697 else
699 tree orig_type = gnu_type;
701 gnu_type
702 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
703 false, false, definition, true);
705 /* If a padding record was made, declare it now since it will
706 never be declared otherwise. This is necessary to ensure
707 that its subtrees are properly marked. */
708 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
709 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL, true,
710 debug_info_p, gnat_entity);
714 /* If we are defining the object, see if it has a Size and validate it
715 if so. If we are not defining the object and a Size clause applies,
716 simply retrieve the value. We don't want to ignore the clause and
717 it is expected to have been validated already. Then get the new
718 type, if any. */
719 if (definition)
720 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
721 gnat_entity, VAR_DECL, false,
722 Has_Size_Clause (gnat_entity));
723 else if (Has_Size_Clause (gnat_entity))
724 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
726 if (gnu_size)
728 gnu_type
729 = make_type_from_size (gnu_type, gnu_size,
730 Has_Biased_Representation (gnat_entity));
732 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
733 gnu_size = NULL_TREE;
736 /* If this object has self-referential size, it must be a record with
737 a default discriminant. We are supposed to allocate an object of
738 the maximum size in this case, unless it is a constant with an
739 initializing expression, in which case we can get the size from
740 that. Note that the resulting size may still be a variable, so
741 this may end up with an indirect allocation. */
742 if (No (Renamed_Object (gnat_entity))
743 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
745 if (gnu_expr && kind == E_Constant)
747 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
748 if (CONTAINS_PLACEHOLDER_P (size))
750 /* If the initializing expression is itself a constant,
751 despite having a nominal type with self-referential
752 size, we can get the size directly from it. */
753 if (TREE_CODE (gnu_expr) == COMPONENT_REF
754 && TYPE_IS_PADDING_P
755 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
756 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
757 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
758 || DECL_READONLY_ONCE_ELAB
759 (TREE_OPERAND (gnu_expr, 0))))
760 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
761 else
762 gnu_size
763 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
765 else
766 gnu_size = size;
768 /* We may have no GNU_EXPR because No_Initialization is
769 set even though there's an Expression. */
770 else if (kind == E_Constant
771 && (Nkind (Declaration_Node (gnat_entity))
772 == N_Object_Declaration)
773 && Present (Expression (Declaration_Node (gnat_entity))))
774 gnu_size
775 = TYPE_SIZE (gnat_to_gnu_type
776 (Etype
777 (Expression (Declaration_Node (gnat_entity)))));
778 else
780 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
781 mutable_p = true;
785 /* If the size is zero byte, make it one byte since some linkers have
786 troubles with zero-sized objects. If the object will have a
787 template, that will make it nonzero so don't bother. Also avoid
788 doing that for an object renaming or an object with an address
789 clause, as we would lose useful information on the view size
790 (e.g. for null array slices) and we are not allocating the object
791 here anyway. */
792 if (((gnu_size
793 && integer_zerop (gnu_size)
794 && !TREE_OVERFLOW (gnu_size))
795 || (TYPE_SIZE (gnu_type)
796 && integer_zerop (TYPE_SIZE (gnu_type))
797 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
798 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
799 || !Is_Array_Type (Etype (gnat_entity)))
800 && No (Renamed_Object (gnat_entity))
801 && No (Address_Clause (gnat_entity)))
802 gnu_size = bitsize_unit_node;
804 /* If this is an object with no specified size and alignment, and
805 if either it is atomic or we are not optimizing alignment for
806 space and it is composite and not an exception, an Out parameter
807 or a reference to another object, and the size of its type is a
808 constant, set the alignment to the smallest one which is not
809 smaller than the size, with an appropriate cap. */
810 if (!gnu_size && align == 0
811 && (Is_Atomic (gnat_entity)
812 || (!Optimize_Alignment_Space (gnat_entity)
813 && kind != E_Exception
814 && kind != E_Out_Parameter
815 && Is_Composite_Type (Etype (gnat_entity))
816 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
817 && !Is_Exported (gnat_entity)
818 && !imported_p
819 && No (Renamed_Object (gnat_entity))
820 && No (Address_Clause (gnat_entity))))
821 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
823 /* No point in jumping through all the hoops needed in order
824 to support BIGGEST_ALIGNMENT if we don't really have to.
825 So we cap to the smallest alignment that corresponds to
826 a known efficient memory access pattern of the target. */
827 unsigned int align_cap = Is_Atomic (gnat_entity)
828 ? BIGGEST_ALIGNMENT
829 : get_mode_alignment (ptr_mode);
831 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
832 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
833 align = align_cap;
834 else
835 align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
837 /* But make sure not to under-align the object. */
838 if (align <= TYPE_ALIGN (gnu_type))
839 align = 0;
841 /* And honor the minimum valid atomic alignment, if any. */
842 #ifdef MINIMUM_ATOMIC_ALIGNMENT
843 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
844 align = MINIMUM_ATOMIC_ALIGNMENT;
845 #endif
848 /* If the object is set to have atomic components, find the component
849 type and validate it.
851 ??? Note that we ignore Has_Volatile_Components on objects; it's
852 not at all clear what to do in that case. */
853 if (Has_Atomic_Components (gnat_entity))
855 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
856 ? TREE_TYPE (gnu_type) : gnu_type);
858 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
859 && TYPE_MULTI_ARRAY_P (gnu_inner))
860 gnu_inner = TREE_TYPE (gnu_inner);
862 check_ok_for_atomic (gnu_inner, gnat_entity, true);
865 /* Now check if the type of the object allows atomic access. Note
866 that we must test the type, even if this object has size and
867 alignment to allow such access, because we will be going inside
868 the padded record to assign to the object. We could fix this by
869 always copying via an intermediate value, but it's not clear it's
870 worth the effort. */
871 if (Is_Atomic (gnat_entity))
872 check_ok_for_atomic (gnu_type, gnat_entity, false);
874 /* If this is an aliased object with an unconstrained nominal subtype,
875 make a type that includes the template. */
876 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
877 && Is_Array_Type (Etype (gnat_entity))
878 && !type_annotate_only)
880 tree gnu_fat
881 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
882 gnu_type
883 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
884 concat_name (gnu_entity_name,
885 "UNC"),
886 debug_info_p);
889 #ifdef MINIMUM_ATOMIC_ALIGNMENT
890 /* If the size is a constant and no alignment is specified, force
891 the alignment to be the minimum valid atomic alignment. The
892 restriction on constant size avoids problems with variable-size
893 temporaries; if the size is variable, there's no issue with
894 atomic access. Also don't do this for a constant, since it isn't
895 necessary and can interfere with constant replacement. Finally,
896 do not do it for Out parameters since that creates an
897 size inconsistency with In parameters. */
898 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
899 && !FLOAT_TYPE_P (gnu_type)
900 && !const_flag && No (Renamed_Object (gnat_entity))
901 && !imported_p && No (Address_Clause (gnat_entity))
902 && kind != E_Out_Parameter
903 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
904 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
905 align = MINIMUM_ATOMIC_ALIGNMENT;
906 #endif
908 /* Make a new type with the desired size and alignment, if needed.
909 But do not take into account alignment promotions to compute the
910 size of the object. */
911 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
912 if (gnu_size || align > 0)
914 tree orig_type = gnu_type;
916 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
917 false, false, definition,
918 gnu_size ? true : false);
920 /* If a padding record was made, declare it now since it will
921 never be declared otherwise. This is necessary to ensure
922 that its subtrees are properly marked. */
923 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
924 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL, true,
925 debug_info_p, gnat_entity);
928 /* If this is a renaming, avoid as much as possible to create a new
929 object. However, in several cases, creating it is required.
930 This processing needs to be applied to the raw expression so
931 as to make it more likely to rename the underlying object. */
932 if (Present (Renamed_Object (gnat_entity)))
934 bool create_normal_object = false;
936 /* If the renamed object had padding, strip off the reference
937 to the inner object and reset our type. */
938 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
939 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
940 /* Strip useless conversions around the object. */
941 || (TREE_CODE (gnu_expr) == NOP_EXPR
942 && gnat_types_compatible_p
943 (TREE_TYPE (gnu_expr),
944 TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
946 gnu_expr = TREE_OPERAND (gnu_expr, 0);
947 gnu_type = TREE_TYPE (gnu_expr);
950 /* Case 1: If this is a constant renaming stemming from a function
951 call, treat it as a normal object whose initial value is what
952 is being renamed. RM 3.3 says that the result of evaluating a
953 function call is a constant object. As a consequence, it can
954 be the inner object of a constant renaming. In this case, the
955 renaming must be fully instantiated, i.e. it cannot be a mere
956 reference to (part of) an existing object. */
957 if (const_flag)
959 tree inner_object = gnu_expr;
960 while (handled_component_p (inner_object))
961 inner_object = TREE_OPERAND (inner_object, 0);
962 if (TREE_CODE (inner_object) == CALL_EXPR)
963 create_normal_object = true;
966 /* Otherwise, see if we can proceed with a stabilized version of
967 the renamed entity or if we need to make a new object. */
968 if (!create_normal_object)
970 tree maybe_stable_expr = NULL_TREE;
971 bool stable = false;
973 /* Case 2: If the renaming entity need not be materialized and
974 the renamed expression is something we can stabilize, use
975 that for the renaming. At the global level, we can only do
976 this if we know no SAVE_EXPRs need be made, because the
977 expression we return might be used in arbitrary conditional
978 branches so we must force the evaluation of the SAVE_EXPRs
979 immediately and this requires a proper function context.
980 Note that an external constant is at the global level. */
981 if (!Materialize_Entity (gnat_entity)
982 && (!((!definition && kind == E_Constant)
983 || global_bindings_p ())
984 || (staticp (gnu_expr)
985 && !TREE_SIDE_EFFECTS (gnu_expr))))
987 maybe_stable_expr
988 = gnat_stabilize_reference (gnu_expr, true, &stable);
990 if (stable)
992 /* ??? No DECL_EXPR is created so we need to mark
993 the expression manually lest it is shared. */
994 if ((!definition && kind == E_Constant)
995 || global_bindings_p ())
996 MARK_VISITED (maybe_stable_expr);
997 gnu_decl = maybe_stable_expr;
998 save_gnu_tree (gnat_entity, gnu_decl, true);
999 saved = true;
1000 annotate_object (gnat_entity, gnu_type, NULL_TREE,
1001 false, false);
1002 break;
1005 /* The stabilization failed. Keep maybe_stable_expr
1006 untouched here to let the pointer case below know
1007 about that failure. */
1010 /* Case 3: If this is a constant renaming and creating a
1011 new object is allowed and cheap, treat it as a normal
1012 object whose initial value is what is being renamed. */
1013 if (const_flag
1014 && !Is_Composite_Type
1015 (Underlying_Type (Etype (gnat_entity))))
1018 /* Case 4: Make this into a constant pointer to the object we
1019 are to rename and attach the object to the pointer if it is
1020 something we can stabilize.
1022 From the proper scope, attached objects will be referenced
1023 directly instead of indirectly via the pointer to avoid
1024 subtle aliasing problems with non-addressable entities.
1025 They have to be stable because we must not evaluate the
1026 variables in the expression every time the renaming is used.
1027 The pointer is called a "renaming" pointer in this case.
1029 In the rare cases where we cannot stabilize the renamed
1030 object, we just make a "bare" pointer, and the renamed
1031 entity is always accessed indirectly through it. */
1032 else
1034 gnu_type = build_reference_type (gnu_type);
1035 inner_const_flag = TREE_READONLY (gnu_expr);
1036 const_flag = true;
1038 /* If the previous attempt at stabilizing failed, there
1039 is no point in trying again and we reuse the result
1040 without attaching it to the pointer. In this case it
1041 will only be used as the initializing expression of
1042 the pointer and thus needs no special treatment with
1043 regard to multiple evaluations. */
1044 if (maybe_stable_expr)
1047 /* Otherwise, try to stabilize and attach the expression
1048 to the pointer if the stabilization succeeds.
1050 Note that this might introduce SAVE_EXPRs and we don't
1051 check whether we're at the global level or not. This
1052 is fine since we are building a pointer initializer and
1053 neither the pointer nor the initializing expression can
1054 be accessed before the pointer elaboration has taken
1055 place in a correct program.
1057 These SAVE_EXPRs will be evaluated at the right place
1058 by either the evaluation of the initializer for the
1059 non-global case or the elaboration code for the global
1060 case, and will be attached to the elaboration procedure
1061 in the latter case. */
1062 else
1064 maybe_stable_expr
1065 = gnat_stabilize_reference (gnu_expr, true, &stable);
1067 if (stable)
1068 renamed_obj = maybe_stable_expr;
1070 /* Attaching is actually performed downstream, as soon
1071 as we have a VAR_DECL for the pointer we make. */
1074 gnu_expr = build_unary_op (ADDR_EXPR, gnu_type,
1075 maybe_stable_expr);
1077 gnu_size = NULL_TREE;
1078 used_by_ref = true;
1083 /* Make a volatile version of this object's type if we are to make
1084 the object volatile. We also interpret 13.3(19) conservatively
1085 and disallow any optimizations for such a non-constant object. */
1086 if ((Treat_As_Volatile (gnat_entity)
1087 || (!const_flag
1088 && gnu_type != except_type_node
1089 && (Is_Exported (gnat_entity)
1090 || imported_p
1091 || Present (Address_Clause (gnat_entity)))))
1092 && !TYPE_VOLATILE (gnu_type))
1093 gnu_type = build_qualified_type (gnu_type,
1094 (TYPE_QUALS (gnu_type)
1095 | TYPE_QUAL_VOLATILE));
1097 /* If we are defining an aliased object whose nominal subtype is
1098 unconstrained, the object is a record that contains both the
1099 template and the object. If there is an initializer, it will
1100 have already been converted to the right type, but we need to
1101 create the template if there is no initializer. */
1102 if (definition
1103 && !gnu_expr
1104 && TREE_CODE (gnu_type) == RECORD_TYPE
1105 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1106 /* Beware that padding might have been introduced above. */
1107 || (TYPE_PADDING_P (gnu_type)
1108 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1109 == RECORD_TYPE
1110 && TYPE_CONTAINS_TEMPLATE_P
1111 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1113 tree template_field
1114 = TYPE_PADDING_P (gnu_type)
1115 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1116 : TYPE_FIELDS (gnu_type);
1117 VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, 1);
1118 tree t = build_template (TREE_TYPE (template_field),
1119 TREE_TYPE (DECL_CHAIN (template_field)),
1120 NULL_TREE);
1121 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1122 gnu_expr = gnat_build_constructor (gnu_type, v);
1125 /* Convert the expression to the type of the object except in the
1126 case where the object's type is unconstrained or the object's type
1127 is a padded record whose field is of self-referential size. In
1128 the former case, converting will generate unnecessary evaluations
1129 of the CONSTRUCTOR to compute the size and in the latter case, we
1130 want to only copy the actual data. */
1131 if (gnu_expr
1132 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1133 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1134 && !(TYPE_IS_PADDING_P (gnu_type)
1135 && CONTAINS_PLACEHOLDER_P
1136 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1137 gnu_expr = convert (gnu_type, gnu_expr);
1139 /* If this is a pointer that doesn't have an initializing expression,
1140 initialize it to NULL, unless the object is imported. */
1141 if (definition
1142 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1143 && !gnu_expr
1144 && !Is_Imported (gnat_entity))
1145 gnu_expr = integer_zero_node;
1147 /* If we are defining the object and it has an Address clause, we must
1148 either get the address expression from the saved GCC tree for the
1149 object if it has a Freeze node, or elaborate the address expression
1150 here since the front-end has guaranteed that the elaboration has no
1151 effects in this case. */
1152 if (definition && Present (Address_Clause (gnat_entity)))
1154 Node_Id gnat_expr = Expression (Address_Clause (gnat_entity));
1155 tree gnu_address
1156 = present_gnu_tree (gnat_entity)
1157 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1159 save_gnu_tree (gnat_entity, NULL_TREE, false);
1161 /* Ignore the size. It's either meaningless or was handled
1162 above. */
1163 gnu_size = NULL_TREE;
1164 /* Convert the type of the object to a reference type that can
1165 alias everything as per 13.3(19). */
1166 gnu_type
1167 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1168 gnu_address = convert (gnu_type, gnu_address);
1169 used_by_ref = true;
1170 const_flag
1171 = !Is_Public (gnat_entity)
1172 || compile_time_known_address_p (gnat_expr);
1174 /* If this is a deferred constant, the initializer is attached to
1175 the full view. */
1176 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1177 gnu_expr
1178 = gnat_to_gnu
1179 (Expression (Declaration_Node (Full_View (gnat_entity))));
1181 /* If we don't have an initializing expression for the underlying
1182 variable, the initializing expression for the pointer is the
1183 specified address. Otherwise, we have to make a COMPOUND_EXPR
1184 to assign both the address and the initial value. */
1185 if (!gnu_expr)
1186 gnu_expr = gnu_address;
1187 else
1188 gnu_expr
1189 = build2 (COMPOUND_EXPR, gnu_type,
1190 build_binary_op
1191 (MODIFY_EXPR, NULL_TREE,
1192 build_unary_op (INDIRECT_REF, NULL_TREE,
1193 gnu_address),
1194 gnu_expr),
1195 gnu_address);
1198 /* If it has an address clause and we are not defining it, mark it
1199 as an indirect object. Likewise for Stdcall objects that are
1200 imported. */
1201 if ((!definition && Present (Address_Clause (gnat_entity)))
1202 || (Is_Imported (gnat_entity)
1203 && Has_Stdcall_Convention (gnat_entity)))
1205 /* Convert the type of the object to a reference type that can
1206 alias everything as per 13.3(19). */
1207 gnu_type
1208 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1209 gnu_size = NULL_TREE;
1211 /* No point in taking the address of an initializing expression
1212 that isn't going to be used. */
1213 gnu_expr = NULL_TREE;
1215 /* If it has an address clause whose value is known at compile
1216 time, make the object a CONST_DECL. This will avoid a
1217 useless dereference. */
1218 if (Present (Address_Clause (gnat_entity)))
1220 Node_Id gnat_address
1221 = Expression (Address_Clause (gnat_entity));
1223 if (compile_time_known_address_p (gnat_address))
1225 gnu_expr = gnat_to_gnu (gnat_address);
1226 const_flag = true;
1230 used_by_ref = true;
1233 /* If we are at top level and this object is of variable size,
1234 make the actual type a hidden pointer to the real type and
1235 make the initializer be a memory allocation and initialization.
1236 Likewise for objects we aren't defining (presumed to be
1237 external references from other packages), but there we do
1238 not set up an initialization.
1240 If the object's size overflows, make an allocator too, so that
1241 Storage_Error gets raised. Note that we will never free
1242 such memory, so we presume it never will get allocated. */
1243 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1244 global_bindings_p ()
1245 || !definition
1246 || static_p)
1247 || (gnu_size && !allocatable_size_p (gnu_size,
1248 global_bindings_p ()
1249 || !definition
1250 || static_p)))
1252 gnu_type = build_reference_type (gnu_type);
1253 gnu_size = NULL_TREE;
1254 used_by_ref = true;
1256 /* In case this was a aliased object whose nominal subtype is
1257 unconstrained, the pointer above will be a thin pointer and
1258 build_allocator will automatically make the template.
1260 If we have a template initializer only (that we made above),
1261 pretend there is none and rely on what build_allocator creates
1262 again anyway. Otherwise (if we have a full initializer), get
1263 the data part and feed that to build_allocator.
1265 If we are elaborating a mutable object, tell build_allocator to
1266 ignore a possibly simpler size from the initializer, if any, as
1267 we must allocate the maximum possible size in this case. */
1268 if (definition && !imported_p)
1270 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1272 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1273 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1275 gnu_alloc_type
1276 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1278 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1279 && 1 == VEC_length (constructor_elt,
1280 CONSTRUCTOR_ELTS (gnu_expr)))
1281 gnu_expr = 0;
1282 else
1283 gnu_expr
1284 = build_component_ref
1285 (gnu_expr, NULL_TREE,
1286 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1287 false);
1290 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1291 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type)))
1292 post_error ("?`Storage_Error` will be raised at run time!",
1293 gnat_entity);
1295 gnu_expr
1296 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1297 Empty, Empty, gnat_entity, mutable_p);
1298 const_flag = true;
1300 else
1302 gnu_expr = NULL_TREE;
1303 const_flag = false;
1307 /* If this object would go into the stack and has an alignment larger
1308 than the largest stack alignment the back-end can honor, resort to
1309 a variable of "aligning type". */
1310 if (!global_bindings_p () && !static_p && definition
1311 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1313 /* Create the new variable. No need for extra room before the
1314 aligned field as this is in automatic storage. */
1315 tree gnu_new_type
1316 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1317 TYPE_SIZE_UNIT (gnu_type),
1318 BIGGEST_ALIGNMENT, 0);
1319 tree gnu_new_var
1320 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1321 NULL_TREE, gnu_new_type, NULL_TREE, false,
1322 false, false, false, NULL, gnat_entity);
1324 /* Initialize the aligned field if we have an initializer. */
1325 if (gnu_expr)
1326 add_stmt_with_node
1327 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1328 build_component_ref
1329 (gnu_new_var, NULL_TREE,
1330 TYPE_FIELDS (gnu_new_type), false),
1331 gnu_expr),
1332 gnat_entity);
1334 /* And setup this entity as a reference to the aligned field. */
1335 gnu_type = build_reference_type (gnu_type);
1336 gnu_expr
1337 = build_unary_op
1338 (ADDR_EXPR, gnu_type,
1339 build_component_ref (gnu_new_var, NULL_TREE,
1340 TYPE_FIELDS (gnu_new_type), false));
1342 gnu_size = NULL_TREE;
1343 used_by_ref = true;
1344 const_flag = true;
1347 if (const_flag)
1348 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1349 | TYPE_QUAL_CONST));
1351 /* Convert the expression to the type of the object except in the
1352 case where the object's type is unconstrained or the object's type
1353 is a padded record whose field is of self-referential size. In
1354 the former case, converting will generate unnecessary evaluations
1355 of the CONSTRUCTOR to compute the size and in the latter case, we
1356 want to only copy the actual data. */
1357 if (gnu_expr
1358 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1359 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1360 && !(TYPE_IS_PADDING_P (gnu_type)
1361 && CONTAINS_PLACEHOLDER_P
1362 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1363 gnu_expr = convert (gnu_type, gnu_expr);
1365 /* If this name is external or there was a name specified, use it,
1366 unless this is a VMS exception object since this would conflict
1367 with the symbol we need to export in addition. Don't use the
1368 Interface_Name if there is an address clause (see CD30005). */
1369 if (!Is_VMS_Exception (gnat_entity)
1370 && ((Present (Interface_Name (gnat_entity))
1371 && No (Address_Clause (gnat_entity)))
1372 || (Is_Public (gnat_entity)
1373 && (!Is_Imported (gnat_entity)
1374 || Is_Exported (gnat_entity)))))
1375 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1377 /* If this is an aggregate constant initialized to a constant, force it
1378 to be statically allocated. This saves an initialization copy. */
1379 if (!static_p
1380 && const_flag
1381 && gnu_expr && TREE_CONSTANT (gnu_expr)
1382 && AGGREGATE_TYPE_P (gnu_type)
1383 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1384 && !(TYPE_IS_PADDING_P (gnu_type)
1385 && !host_integerp (TYPE_SIZE_UNIT
1386 (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
1387 static_p = true;
1389 /* Now create the variable or the constant and set various flags. */
1390 gnu_decl
1391 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1392 gnu_expr, const_flag, Is_Public (gnat_entity),
1393 imported_p || !definition, static_p, attr_list,
1394 gnat_entity);
1395 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1396 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1398 /* If we are defining an Out parameter and optimization isn't enabled,
1399 create a fake PARM_DECL for debugging purposes and make it point to
1400 the VAR_DECL. Suppress debug info for the latter but make sure it
1401 will live on the stack so that it can be accessed from within the
1402 debugger through the PARM_DECL. */
1403 if (kind == E_Out_Parameter && definition && !optimize && debug_info_p)
1405 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1406 gnat_pushdecl (param, gnat_entity);
1407 SET_DECL_VALUE_EXPR (param, gnu_decl);
1408 DECL_HAS_VALUE_EXPR_P (param) = 1;
1409 DECL_IGNORED_P (gnu_decl) = 1;
1410 TREE_ADDRESSABLE (gnu_decl) = 1;
1413 /* If this is a renaming pointer, attach the renamed object to it and
1414 register it if we are at the global level. Note that an external
1415 constant is at the global level. */
1416 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1418 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1419 if ((!definition && kind == E_Constant) || global_bindings_p ())
1421 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1422 record_global_renaming_pointer (gnu_decl);
1426 /* If this is a constant and we are defining it or it generates a real
1427 symbol at the object level and we are referencing it, we may want
1428 or need to have a true variable to represent it:
1429 - if optimization isn't enabled, for debugging purposes,
1430 - if the constant is public and not overlaid on something else,
1431 - if its address is taken,
1432 - if either itself or its type is aliased. */
1433 if (TREE_CODE (gnu_decl) == CONST_DECL
1434 && (definition || Sloc (gnat_entity) > Standard_Location)
1435 && ((!optimize && debug_info_p)
1436 || (Is_Public (gnat_entity)
1437 && No (Address_Clause (gnat_entity)))
1438 || Address_Taken (gnat_entity)
1439 || Is_Aliased (gnat_entity)
1440 || Is_Aliased (Etype (gnat_entity))))
1442 tree gnu_corr_var
1443 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1444 gnu_expr, true, Is_Public (gnat_entity),
1445 !definition, static_p, attr_list,
1446 gnat_entity);
1448 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1450 /* As debugging information will be generated for the variable,
1451 do not generate debugging information for the constant. */
1452 if (debug_info_p)
1453 DECL_IGNORED_P (gnu_decl) = 1;
1454 else
1455 DECL_IGNORED_P (gnu_corr_var) = 1;
1458 /* If this is a constant, even if we don't need a true variable, we
1459 may need to avoid returning the initializer in every case. That
1460 can happen for the address of a (constant) constructor because,
1461 upon dereferencing it, the constructor will be reinjected in the
1462 tree, which may not be valid in every case; see lvalue_required_p
1463 for more details. */
1464 if (TREE_CODE (gnu_decl) == CONST_DECL)
1465 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1467 /* If this object is declared in a block that contains a block with an
1468 exception handler, and we aren't using the GCC exception mechanism,
1469 we must force this variable in memory in order to avoid an invalid
1470 optimization. */
1471 if (Exception_Mechanism != Back_End_Exceptions
1472 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1473 TREE_ADDRESSABLE (gnu_decl) = 1;
1475 /* If we are defining an object with variable size or an object with
1476 fixed size that will be dynamically allocated, and we are using the
1477 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1478 if (definition
1479 && Exception_Mechanism == Setjmp_Longjmp
1480 && get_block_jmpbuf_decl ()
1481 && DECL_SIZE_UNIT (gnu_decl)
1482 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1483 || (flag_stack_check == GENERIC_STACK_CHECK
1484 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1485 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1486 add_stmt_with_node (build_call_1_expr
1487 (update_setjmp_buf_decl,
1488 build_unary_op (ADDR_EXPR, NULL_TREE,
1489 get_block_jmpbuf_decl ())),
1490 gnat_entity);
1492 /* Back-annotate Esize and Alignment of the object if not already
1493 known. Note that we pick the values of the type, not those of
1494 the object, to shield ourselves from low-level platform-dependent
1495 adjustments like alignment promotion. This is both consistent with
1496 all the treatment above, where alignment and size are set on the
1497 type of the object and not on the object directly, and makes it
1498 possible to support all confirming representation clauses. */
1499 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1500 used_by_ref, false);
1502 break;
1504 case E_Void:
1505 /* Return a TYPE_DECL for "void" that we previously made. */
1506 gnu_decl = TYPE_NAME (void_type_node);
1507 break;
1509 case E_Enumeration_Type:
1510 /* A special case: for the types Character and Wide_Character in
1511 Standard, we do not list all the literals. So if the literals
1512 are not specified, make this an unsigned type. */
1513 if (No (First_Literal (gnat_entity)))
1515 gnu_type = make_unsigned_type (esize);
1516 TYPE_NAME (gnu_type) = gnu_entity_name;
1518 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1519 This is needed by the DWARF-2 back-end to distinguish between
1520 unsigned integer types and character types. */
1521 TYPE_STRING_FLAG (gnu_type) = 1;
1522 break;
1526 /* We have a list of enumeral constants in First_Literal. We make a
1527 CONST_DECL for each one and build into GNU_LITERAL_LIST the list to
1528 be placed into TYPE_FIELDS. Each node in the list is a TREE_LIST
1529 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1530 value of the literal. But when we have a regular boolean type, we
1531 simplify this a little by using a BOOLEAN_TYPE. */
1532 bool is_boolean = Is_Boolean_Type (gnat_entity)
1533 && !Has_Non_Standard_Rep (gnat_entity);
1534 tree gnu_literal_list = NULL_TREE;
1535 Entity_Id gnat_literal;
1537 if (Is_Unsigned_Type (gnat_entity))
1538 gnu_type = make_unsigned_type (esize);
1539 else
1540 gnu_type = make_signed_type (esize);
1542 TREE_SET_CODE (gnu_type, is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1544 for (gnat_literal = First_Literal (gnat_entity);
1545 Present (gnat_literal);
1546 gnat_literal = Next_Literal (gnat_literal))
1548 tree gnu_value
1549 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1550 tree gnu_literal
1551 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1552 gnu_type, gnu_value, true, false, false,
1553 false, NULL, gnat_literal);
1554 /* Do not generate debug info for individual enumerators. */
1555 DECL_IGNORED_P (gnu_literal) = 1;
1556 save_gnu_tree (gnat_literal, gnu_literal, false);
1557 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1558 gnu_value, gnu_literal_list);
1561 if (!is_boolean)
1562 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1564 /* Note that the bounds are updated at the end of this function
1565 to avoid an infinite recursion since they refer to the type. */
1567 goto discrete_type;
1569 case E_Signed_Integer_Type:
1570 case E_Ordinary_Fixed_Point_Type:
1571 case E_Decimal_Fixed_Point_Type:
1572 /* For integer types, just make a signed type the appropriate number
1573 of bits. */
1574 gnu_type = make_signed_type (esize);
1575 goto discrete_type;
1577 case E_Modular_Integer_Type:
1579 /* For modular types, make the unsigned type of the proper number
1580 of bits and then set up the modulus, if required. */
1581 tree gnu_modulus, gnu_high = NULL_TREE;
1583 /* Packed array types are supposed to be subtypes only. */
1584 gcc_assert (!Is_Packed_Array_Type (gnat_entity));
1586 gnu_type = make_unsigned_type (esize);
1588 /* Get the modulus in this type. If it overflows, assume it is because
1589 it is equal to 2**Esize. Note that there is no overflow checking
1590 done on unsigned type, so we detect the overflow by looking for
1591 a modulus of zero, which is otherwise invalid. */
1592 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1594 if (!integer_zerop (gnu_modulus))
1596 TYPE_MODULAR_P (gnu_type) = 1;
1597 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1598 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1599 convert (gnu_type, integer_one_node));
1602 /* If the upper bound is not maximal, make an extra subtype. */
1603 if (gnu_high
1604 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1606 tree gnu_subtype = make_unsigned_type (esize);
1607 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1608 TREE_TYPE (gnu_subtype) = gnu_type;
1609 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1610 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1611 gnu_type = gnu_subtype;
1614 goto discrete_type;
1616 case E_Signed_Integer_Subtype:
1617 case E_Enumeration_Subtype:
1618 case E_Modular_Integer_Subtype:
1619 case E_Ordinary_Fixed_Point_Subtype:
1620 case E_Decimal_Fixed_Point_Subtype:
1622 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1623 not want to call create_range_type since we would like each subtype
1624 node to be distinct. ??? Historically this was in preparation for
1625 when memory aliasing is implemented, but that's obsolete now given
1626 the call to relate_alias_sets below.
1628 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1629 this fact is used by the arithmetic conversion functions.
1631 We elaborate the Ancestor_Subtype if it is not in the current unit
1632 and one of our bounds is non-static. We do this to ensure consistent
1633 naming in the case where several subtypes share the same bounds, by
1634 elaborating the first such subtype first, thus using its name. */
1636 if (!definition
1637 && Present (Ancestor_Subtype (gnat_entity))
1638 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1639 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1640 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1641 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1643 /* Set the precision to the Esize except for bit-packed arrays. */
1644 if (Is_Packed_Array_Type (gnat_entity)
1645 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1646 esize = UI_To_Int (RM_Size (gnat_entity));
1648 /* This should be an unsigned type if the base type is unsigned or
1649 if the lower bound is constant and non-negative or if the type
1650 is biased. */
1651 if (Is_Unsigned_Type (Etype (gnat_entity))
1652 || Is_Unsigned_Type (gnat_entity)
1653 || Has_Biased_Representation (gnat_entity))
1654 gnu_type = make_unsigned_type (esize);
1655 else
1656 gnu_type = make_signed_type (esize);
1657 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1659 SET_TYPE_RM_MIN_VALUE
1660 (gnu_type,
1661 convert (TREE_TYPE (gnu_type),
1662 elaborate_expression (Type_Low_Bound (gnat_entity),
1663 gnat_entity, get_identifier ("L"),
1664 definition, true,
1665 Needs_Debug_Info (gnat_entity))));
1667 SET_TYPE_RM_MAX_VALUE
1668 (gnu_type,
1669 convert (TREE_TYPE (gnu_type),
1670 elaborate_expression (Type_High_Bound (gnat_entity),
1671 gnat_entity, get_identifier ("U"),
1672 definition, true,
1673 Needs_Debug_Info (gnat_entity))));
1675 /* One of the above calls might have caused us to be elaborated,
1676 so don't blow up if so. */
1677 if (present_gnu_tree (gnat_entity))
1679 maybe_present = true;
1680 break;
1683 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1684 = Has_Biased_Representation (gnat_entity);
1686 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1687 TYPE_STUB_DECL (gnu_type)
1688 = create_type_stub_decl (gnu_entity_name, gnu_type);
1690 /* Inherit our alias set from what we're a subtype of. Subtypes
1691 are not different types and a pointer can designate any instance
1692 within a subtype hierarchy. */
1693 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1695 /* For a packed array, make the original array type a parallel type. */
1696 if (debug_info_p
1697 && Is_Packed_Array_Type (gnat_entity)
1698 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1699 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1700 gnat_to_gnu_type
1701 (Original_Array_Type (gnat_entity)));
1703 discrete_type:
1705 /* We have to handle clauses that under-align the type specially. */
1706 if ((Present (Alignment_Clause (gnat_entity))
1707 || (Is_Packed_Array_Type (gnat_entity)
1708 && Present
1709 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1710 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1712 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1713 if (align >= TYPE_ALIGN (gnu_type))
1714 align = 0;
1717 /* If the type we are dealing with represents a bit-packed array,
1718 we need to have the bits left justified on big-endian targets
1719 and right justified on little-endian targets. We also need to
1720 ensure that when the value is read (e.g. for comparison of two
1721 such values), we only get the good bits, since the unused bits
1722 are uninitialized. Both goals are accomplished by wrapping up
1723 the modular type in an enclosing record type. */
1724 if (Is_Packed_Array_Type (gnat_entity)
1725 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1727 tree gnu_field_type, gnu_field;
1729 /* Set the RM size before wrapping up the original type. */
1730 SET_TYPE_RM_SIZE (gnu_type,
1731 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1732 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1734 /* Create a stripped-down declaration, mainly for debugging. */
1735 create_type_decl (gnu_entity_name, gnu_type, NULL, true,
1736 debug_info_p, gnat_entity);
1738 /* Now save it and build the enclosing record type. */
1739 gnu_field_type = gnu_type;
1741 gnu_type = make_node (RECORD_TYPE);
1742 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1743 TYPE_PACKED (gnu_type) = 1;
1744 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1745 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1746 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1748 /* Propagate the alignment of the modular type to the record type,
1749 unless there is an alignment clause that under-aligns the type.
1750 This means that bit-packed arrays are given "ceil" alignment for
1751 their size by default, which may seem counter-intuitive but makes
1752 it possible to overlay them on modular types easily. */
1753 TYPE_ALIGN (gnu_type)
1754 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1756 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1758 /* Don't declare the field as addressable since we won't be taking
1759 its address and this would prevent create_field_decl from making
1760 a bitfield. */
1761 gnu_field
1762 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1763 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1765 /* Do not emit debug info until after the parallel type is added. */
1766 finish_record_type (gnu_type, gnu_field, 2, false);
1767 compute_record_mode (gnu_type);
1768 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1770 if (debug_info_p)
1772 /* Make the original array type a parallel type. */
1773 if (present_gnu_tree (Original_Array_Type (gnat_entity)))
1774 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1775 gnat_to_gnu_type
1776 (Original_Array_Type (gnat_entity)));
1778 rest_of_record_type_compilation (gnu_type);
1782 /* If the type we are dealing with has got a smaller alignment than the
1783 natural one, we need to wrap it up in a record type and under-align
1784 the latter. We reuse the padding machinery for this purpose. */
1785 else if (align > 0)
1787 tree gnu_field_type, gnu_field;
1789 /* Set the RM size before wrapping up the type. */
1790 SET_TYPE_RM_SIZE (gnu_type,
1791 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1793 /* Create a stripped-down declaration, mainly for debugging. */
1794 create_type_decl (gnu_entity_name, gnu_type, NULL, true,
1795 debug_info_p, gnat_entity);
1797 /* Now save it and build the enclosing record type. */
1798 gnu_field_type = gnu_type;
1800 gnu_type = make_node (RECORD_TYPE);
1801 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1802 TYPE_PACKED (gnu_type) = 1;
1803 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1804 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1805 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1806 TYPE_ALIGN (gnu_type) = align;
1807 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1809 /* Don't declare the field as addressable since we won't be taking
1810 its address and this would prevent create_field_decl from making
1811 a bitfield. */
1812 gnu_field
1813 = create_field_decl (get_identifier ("F"), gnu_field_type,
1814 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1816 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1817 compute_record_mode (gnu_type);
1818 TYPE_PADDING_P (gnu_type) = 1;
1821 break;
1823 case E_Floating_Point_Type:
1824 /* If this is a VAX floating-point type, use an integer of the proper
1825 size. All the operations will be handled with ASM statements. */
1826 if (Vax_Float (gnat_entity))
1828 gnu_type = make_signed_type (esize);
1829 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1830 SET_TYPE_DIGITS_VALUE (gnu_type,
1831 UI_To_gnu (Digits_Value (gnat_entity),
1832 sizetype));
1833 break;
1836 /* The type of the Low and High bounds can be our type if this is
1837 a type from Standard, so set them at the end of the function. */
1838 gnu_type = make_node (REAL_TYPE);
1839 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1840 layout_type (gnu_type);
1841 break;
1843 case E_Floating_Point_Subtype:
1844 if (Vax_Float (gnat_entity))
1846 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1847 break;
1851 if (!definition
1852 && Present (Ancestor_Subtype (gnat_entity))
1853 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1854 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1855 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1856 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1857 gnu_expr, 0);
1859 gnu_type = make_node (REAL_TYPE);
1860 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1861 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1862 TYPE_GCC_MIN_VALUE (gnu_type)
1863 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1864 TYPE_GCC_MAX_VALUE (gnu_type)
1865 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1866 layout_type (gnu_type);
1868 SET_TYPE_RM_MIN_VALUE
1869 (gnu_type,
1870 convert (TREE_TYPE (gnu_type),
1871 elaborate_expression (Type_Low_Bound (gnat_entity),
1872 gnat_entity, get_identifier ("L"),
1873 definition, true,
1874 Needs_Debug_Info (gnat_entity))));
1876 SET_TYPE_RM_MAX_VALUE
1877 (gnu_type,
1878 convert (TREE_TYPE (gnu_type),
1879 elaborate_expression (Type_High_Bound (gnat_entity),
1880 gnat_entity, get_identifier ("U"),
1881 definition, true,
1882 Needs_Debug_Info (gnat_entity))));
1884 /* One of the above calls might have caused us to be elaborated,
1885 so don't blow up if so. */
1886 if (present_gnu_tree (gnat_entity))
1888 maybe_present = true;
1889 break;
1892 /* Inherit our alias set from what we're a subtype of, as for
1893 integer subtypes. */
1894 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1896 break;
1898 /* Array and String Types and Subtypes
1900 Unconstrained array types are represented by E_Array_Type and
1901 constrained array types are represented by E_Array_Subtype. There
1902 are no actual objects of an unconstrained array type; all we have
1903 are pointers to that type.
1905 The following fields are defined on array types and subtypes:
1907 Component_Type Component type of the array.
1908 Number_Dimensions Number of dimensions (an int).
1909 First_Index Type of first index. */
1911 case E_String_Type:
1912 case E_Array_Type:
1914 const bool convention_fortran_p
1915 = (Convention (gnat_entity) == Convention_Fortran);
1916 const int ndim = Number_Dimensions (gnat_entity);
1917 tree gnu_template_type = make_node (RECORD_TYPE);
1918 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1919 tree gnu_template_reference, gnu_template_fields, gnu_fat_type;
1920 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
1921 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
1922 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem, t;
1923 Entity_Id gnat_index, gnat_name;
1924 int index;
1926 /* We complete an existing dummy fat pointer type in place. This both
1927 avoids further complex adjustments in update_pointer_to and yields
1928 better debugging information in DWARF by leveraging the support for
1929 incomplete declarations of "tagged" types in the DWARF back-end. */
1930 gnu_type = get_dummy_type (gnat_entity);
1931 if (gnu_type && TYPE_POINTER_TO (gnu_type))
1933 gnu_fat_type = TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type));
1934 TYPE_NAME (gnu_fat_type) = NULL_TREE;
1935 /* Save the contents of the dummy type for update_pointer_to. */
1936 TYPE_POINTER_TO (gnu_type) = copy_type (gnu_fat_type);
1938 else
1939 gnu_fat_type = make_node (RECORD_TYPE);
1941 /* Make a node for the array. If we are not defining the array
1942 suppress expanding incomplete types. */
1943 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1945 if (!definition)
1947 defer_incomplete_level++;
1948 this_deferred = true;
1951 /* Build the fat pointer type. Use a "void *" object instead of
1952 a pointer to the array type since we don't have the array type
1953 yet (it will reference the fat pointer via the bounds). */
1955 = create_field_decl (get_identifier ("P_ARRAY"), ptr_void_type_node,
1956 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
1957 DECL_CHAIN (tem)
1958 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template,
1959 gnu_fat_type, NULL_TREE, NULL_TREE, 0, 0);
1961 if (COMPLETE_TYPE_P (gnu_fat_type))
1963 /* We are going to lay it out again so reset the alias set. */
1964 alias_set_type alias_set = TYPE_ALIAS_SET (gnu_fat_type);
1965 TYPE_ALIAS_SET (gnu_fat_type) = -1;
1966 finish_fat_pointer_type (gnu_fat_type, tem);
1967 TYPE_ALIAS_SET (gnu_fat_type) = alias_set;
1968 for (t = gnu_fat_type; t; t = TYPE_NEXT_VARIANT (t))
1970 TYPE_FIELDS (t) = tem;
1971 SET_TYPE_UNCONSTRAINED_ARRAY (t, gnu_type);
1974 else
1976 finish_fat_pointer_type (gnu_fat_type, tem);
1977 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
1980 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1981 is the fat pointer. This will be used to access the individual
1982 fields once we build them. */
1983 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1984 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1985 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1986 gnu_template_reference
1987 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1988 TREE_READONLY (gnu_template_reference) = 1;
1990 /* Now create the GCC type for each index and add the fields for that
1991 index to the template. */
1992 for (index = (convention_fortran_p ? ndim - 1 : 0),
1993 gnat_index = First_Index (gnat_entity);
1994 0 <= index && index < ndim;
1995 index += (convention_fortran_p ? - 1 : 1),
1996 gnat_index = Next_Index (gnat_index))
1998 char field_name[16];
1999 tree gnu_index_base_type
2000 = get_unpadded_type (Base_Type (Etype (gnat_index)));
2001 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
2002 tree gnu_min, gnu_max, gnu_high;
2004 /* Make the FIELD_DECLs for the low and high bounds of this
2005 type and then make extractions of these fields from the
2006 template. */
2007 sprintf (field_name, "LB%d", index);
2008 gnu_lb_field = create_field_decl (get_identifier (field_name),
2009 gnu_index_base_type,
2010 gnu_template_type, NULL_TREE,
2011 NULL_TREE, 0, 0);
2012 Sloc_to_locus (Sloc (gnat_entity),
2013 &DECL_SOURCE_LOCATION (gnu_lb_field));
2015 field_name[0] = 'U';
2016 gnu_hb_field = create_field_decl (get_identifier (field_name),
2017 gnu_index_base_type,
2018 gnu_template_type, NULL_TREE,
2019 NULL_TREE, 0, 0);
2020 Sloc_to_locus (Sloc (gnat_entity),
2021 &DECL_SOURCE_LOCATION (gnu_hb_field));
2023 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
2025 /* We can't use build_component_ref here since the template type
2026 isn't complete yet. */
2027 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
2028 gnu_template_reference, gnu_lb_field,
2029 NULL_TREE);
2030 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
2031 gnu_template_reference, gnu_hb_field,
2032 NULL_TREE);
2033 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
2035 gnu_min = convert (sizetype, gnu_orig_min);
2036 gnu_max = convert (sizetype, gnu_orig_max);
2038 /* Compute the size of this dimension. See the E_Array_Subtype
2039 case below for the rationale. */
2040 gnu_high
2041 = build3 (COND_EXPR, sizetype,
2042 build2 (GE_EXPR, boolean_type_node,
2043 gnu_orig_max, gnu_orig_min),
2044 gnu_max,
2045 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2047 /* Make a range type with the new range in the Ada base type.
2048 Then make an index type with the size range in sizetype. */
2049 gnu_index_types[index]
2050 = create_index_type (gnu_min, gnu_high,
2051 create_range_type (gnu_index_base_type,
2052 gnu_orig_min,
2053 gnu_orig_max),
2054 gnat_entity);
2056 /* Update the maximum size of the array in elements. */
2057 if (gnu_max_size)
2059 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2060 tree gnu_min
2061 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2062 tree gnu_max
2063 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2064 tree gnu_this_max
2065 = size_binop (MAX_EXPR,
2066 size_binop (PLUS_EXPR, size_one_node,
2067 size_binop (MINUS_EXPR,
2068 gnu_max, gnu_min)),
2069 size_zero_node);
2071 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2072 && TREE_OVERFLOW (gnu_this_max))
2073 gnu_max_size = NULL_TREE;
2074 else
2075 gnu_max_size
2076 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2079 TYPE_NAME (gnu_index_types[index])
2080 = create_concat_name (gnat_entity, field_name);
2083 /* Install all the fields into the template. */
2084 TYPE_NAME (gnu_template_type)
2085 = create_concat_name (gnat_entity, "XUB");
2086 gnu_template_fields = NULL_TREE;
2087 for (index = 0; index < ndim; index++)
2088 gnu_template_fields
2089 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2090 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2091 debug_info_p);
2092 TYPE_READONLY (gnu_template_type) = 1;
2094 /* Now make the array of arrays and update the pointer to the array
2095 in the fat pointer. Note that it is the first field. */
2097 = gnat_to_gnu_component_type (gnat_entity, definition, debug_info_p);
2099 /* If Component_Size is not already specified, annotate it with the
2100 size of the component. */
2101 if (Unknown_Component_Size (gnat_entity))
2102 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
2104 /* Compute the maximum size of the array in units and bits. */
2105 if (gnu_max_size)
2107 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2108 TYPE_SIZE_UNIT (tem));
2109 gnu_max_size = size_binop (MULT_EXPR,
2110 convert (bitsizetype, gnu_max_size),
2111 TYPE_SIZE (tem));
2113 else
2114 gnu_max_size_unit = NULL_TREE;
2116 /* Now build the array type. */
2117 for (index = ndim - 1; index >= 0; index--)
2119 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2120 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2121 if (array_type_has_nonaliased_component (tem, gnat_entity))
2122 TYPE_NONALIASED_COMPONENT (tem) = 1;
2125 /* If an alignment is specified, use it if valid. But ignore it
2126 for the original type of packed array types. If the alignment
2127 was requested with an explicit alignment clause, state so. */
2128 if (No (Packed_Array_Type (gnat_entity))
2129 && Known_Alignment (gnat_entity))
2131 TYPE_ALIGN (tem)
2132 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2133 TYPE_ALIGN (tem));
2134 if (Present (Alignment_Clause (gnat_entity)))
2135 TYPE_USER_ALIGN (tem) = 1;
2138 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2140 /* Adjust the type of the pointer-to-array field of the fat pointer
2141 and record the aliasing relationships if necessary. */
2142 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2143 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type))
2144 record_component_aliases (gnu_fat_type);
2146 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2147 corresponding fat pointer. */
2148 TREE_TYPE (gnu_type) = gnu_fat_type;
2149 TYPE_POINTER_TO (gnu_type) = gnu_fat_type;
2150 TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2151 SET_TYPE_MODE (gnu_type, BLKmode);
2152 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2154 /* If the maximum size doesn't overflow, use it. */
2155 if (gnu_max_size
2156 && TREE_CODE (gnu_max_size) == INTEGER_CST
2157 && !TREE_OVERFLOW (gnu_max_size)
2158 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2159 && !TREE_OVERFLOW (gnu_max_size_unit))
2161 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2162 TYPE_SIZE (tem));
2163 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2164 TYPE_SIZE_UNIT (tem));
2167 create_type_decl (create_concat_name (gnat_entity, "XUA"),
2168 tem, NULL, !Comes_From_Source (gnat_entity),
2169 debug_info_p, gnat_entity);
2171 /* Give the fat pointer type a name. If this is a packed type, tell
2172 the debugger how to interpret the underlying bits. */
2173 if (Present (Packed_Array_Type (gnat_entity)))
2174 gnat_name = Packed_Array_Type (gnat_entity);
2175 else
2176 gnat_name = gnat_entity;
2177 create_type_decl (create_concat_name (gnat_name, "XUP"),
2178 gnu_fat_type, NULL, !Comes_From_Source (gnat_entity),
2179 debug_info_p, gnat_entity);
2181 /* Create the type to be used as what a thin pointer designates:
2182 a record type for the object and its template with the fields
2183 shifted to have the template at a negative offset. */
2184 tem = build_unc_object_type (gnu_template_type, tem,
2185 create_concat_name (gnat_name, "XUT"),
2186 debug_info_p);
2187 shift_unc_components_for_thin_pointers (tem);
2189 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2190 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2192 break;
2194 case E_String_Subtype:
2195 case E_Array_Subtype:
2197 /* This is the actual data type for array variables. Multidimensional
2198 arrays are implemented as arrays of arrays. Note that arrays which
2199 have sparse enumeration subtypes as index components create sparse
2200 arrays, which is obviously space inefficient but so much easier to
2201 code for now.
2203 Also note that the subtype never refers to the unconstrained array
2204 type, which is somewhat at variance with Ada semantics.
2206 First check to see if this is simply a renaming of the array type.
2207 If so, the result is the array type. */
2209 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2210 if (!Is_Constrained (gnat_entity))
2212 else
2214 Entity_Id gnat_index, gnat_base_index;
2215 const bool convention_fortran_p
2216 = (Convention (gnat_entity) == Convention_Fortran);
2217 const int ndim = Number_Dimensions (gnat_entity);
2218 tree gnu_base_type = gnu_type;
2219 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2220 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2221 bool need_index_type_struct = false;
2222 int index;
2224 /* First create the GCC type for each index and find out whether
2225 special types are needed for debugging information. */
2226 for (index = (convention_fortran_p ? ndim - 1 : 0),
2227 gnat_index = First_Index (gnat_entity),
2228 gnat_base_index
2229 = First_Index (Implementation_Base_Type (gnat_entity));
2230 0 <= index && index < ndim;
2231 index += (convention_fortran_p ? - 1 : 1),
2232 gnat_index = Next_Index (gnat_index),
2233 gnat_base_index = Next_Index (gnat_base_index))
2235 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2236 tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type);
2237 tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type);
2238 tree gnu_min = convert (sizetype, gnu_orig_min);
2239 tree gnu_max = convert (sizetype, gnu_orig_max);
2240 tree gnu_base_index_type
2241 = get_unpadded_type (Etype (gnat_base_index));
2242 tree gnu_base_orig_min = TYPE_MIN_VALUE (gnu_base_index_type);
2243 tree gnu_base_orig_max = TYPE_MAX_VALUE (gnu_base_index_type);
2244 tree gnu_high;
2246 /* See if the base array type is already flat. If it is, we
2247 are probably compiling an ACATS test but it will cause the
2248 code below to malfunction if we don't handle it specially. */
2249 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2250 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2251 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2253 gnu_min = size_one_node;
2254 gnu_max = size_zero_node;
2255 gnu_high = gnu_max;
2258 /* Similarly, if one of the values overflows in sizetype and the
2259 range is null, use 1..0 for the sizetype bounds. */
2260 else if (TREE_CODE (gnu_min) == INTEGER_CST
2261 && TREE_CODE (gnu_max) == INTEGER_CST
2262 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2263 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2265 gnu_min = size_one_node;
2266 gnu_max = size_zero_node;
2267 gnu_high = gnu_max;
2270 /* If the minimum and maximum values both overflow in sizetype,
2271 but the difference in the original type does not overflow in
2272 sizetype, ignore the overflow indication. */
2273 else if (TREE_CODE (gnu_min) == INTEGER_CST
2274 && TREE_CODE (gnu_max) == INTEGER_CST
2275 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2276 && !TREE_OVERFLOW
2277 (convert (sizetype,
2278 fold_build2 (MINUS_EXPR, gnu_index_type,
2279 gnu_orig_max,
2280 gnu_orig_min))))
2282 TREE_OVERFLOW (gnu_min) = 0;
2283 TREE_OVERFLOW (gnu_max) = 0;
2284 gnu_high = gnu_max;
2287 /* Compute the size of this dimension in the general case. We
2288 need to provide GCC with an upper bound to use but have to
2289 deal with the "superflat" case. There are three ways to do
2290 this. If we can prove that the array can never be superflat,
2291 we can just use the high bound of the index type. */
2292 else if ((Nkind (gnat_index) == N_Range
2293 && cannot_be_superflat_p (gnat_index))
2294 /* Packed Array Types are never superflat. */
2295 || Is_Packed_Array_Type (gnat_entity))
2296 gnu_high = gnu_max;
2298 /* Otherwise, if the high bound is constant but the low bound is
2299 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2300 lower bound. Note that the comparison must be done in the
2301 original type to avoid any overflow during the conversion. */
2302 else if (TREE_CODE (gnu_max) == INTEGER_CST
2303 && TREE_CODE (gnu_min) != INTEGER_CST)
2305 gnu_high = gnu_max;
2306 gnu_min
2307 = build_cond_expr (sizetype,
2308 build_binary_op (GE_EXPR,
2309 boolean_type_node,
2310 gnu_orig_max,
2311 gnu_orig_min),
2312 gnu_min,
2313 size_binop (PLUS_EXPR, gnu_max,
2314 size_one_node));
2317 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2318 in all the other cases. Note that, here as well as above,
2319 the condition used in the comparison must be equivalent to
2320 the condition (length != 0). This is relied upon in order
2321 to optimize array comparisons in compare_arrays. */
2322 else
2323 gnu_high
2324 = build_cond_expr (sizetype,
2325 build_binary_op (GE_EXPR,
2326 boolean_type_node,
2327 gnu_orig_max,
2328 gnu_orig_min),
2329 gnu_max,
2330 size_binop (MINUS_EXPR, gnu_min,
2331 size_one_node));
2333 /* Reuse the index type for the range type. Then make an index
2334 type with the size range in sizetype. */
2335 gnu_index_types[index]
2336 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2337 gnat_entity);
2339 /* Update the maximum size of the array in elements. Here we
2340 see if any constraint on the index type of the base type
2341 can be used in the case of self-referential bound on the
2342 index type of the subtype. We look for a non-"infinite"
2343 and non-self-referential bound from any type involved and
2344 handle each bound separately. */
2345 if (gnu_max_size)
2347 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2348 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2349 tree gnu_base_index_base_type
2350 = get_base_type (gnu_base_index_type);
2351 tree gnu_base_base_min
2352 = convert (sizetype,
2353 TYPE_MIN_VALUE (gnu_base_index_base_type));
2354 tree gnu_base_base_max
2355 = convert (sizetype,
2356 TYPE_MAX_VALUE (gnu_base_index_base_type));
2358 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2359 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2360 && !TREE_OVERFLOW (gnu_base_min)))
2361 gnu_base_min = gnu_min;
2363 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2364 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2365 && !TREE_OVERFLOW (gnu_base_max)))
2366 gnu_base_max = gnu_max;
2368 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2369 && TREE_OVERFLOW (gnu_base_min))
2370 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2371 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2372 && TREE_OVERFLOW (gnu_base_max))
2373 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2374 gnu_max_size = NULL_TREE;
2375 else
2377 tree gnu_this_max
2378 = size_binop (MAX_EXPR,
2379 size_binop (PLUS_EXPR, size_one_node,
2380 size_binop (MINUS_EXPR,
2381 gnu_base_max,
2382 gnu_base_min)),
2383 size_zero_node);
2385 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2386 && TREE_OVERFLOW (gnu_this_max))
2387 gnu_max_size = NULL_TREE;
2388 else
2389 gnu_max_size
2390 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2394 /* We need special types for debugging information to point to
2395 the index types if they have variable bounds, are not integer
2396 types, are biased or are wider than sizetype. */
2397 if (!integer_onep (gnu_orig_min)
2398 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2399 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2400 || (TREE_TYPE (gnu_index_type)
2401 && TREE_CODE (TREE_TYPE (gnu_index_type))
2402 != INTEGER_TYPE)
2403 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type)
2404 || compare_tree_int (rm_size (gnu_index_type),
2405 TYPE_PRECISION (sizetype)) > 0)
2406 need_index_type_struct = true;
2409 /* Then flatten: create the array of arrays. For an array type
2410 used to implement a packed array, get the component type from
2411 the original array type since the representation clauses that
2412 can affect it are on the latter. */
2413 if (Is_Packed_Array_Type (gnat_entity)
2414 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2416 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2417 for (index = ndim - 1; index >= 0; index--)
2418 gnu_type = TREE_TYPE (gnu_type);
2420 /* One of the above calls might have caused us to be elaborated,
2421 so don't blow up if so. */
2422 if (present_gnu_tree (gnat_entity))
2424 maybe_present = true;
2425 break;
2428 else
2430 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2431 debug_info_p);
2433 /* One of the above calls might have caused us to be elaborated,
2434 so don't blow up if so. */
2435 if (present_gnu_tree (gnat_entity))
2437 maybe_present = true;
2438 break;
2442 /* Compute the maximum size of the array in units and bits. */
2443 if (gnu_max_size)
2445 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2446 TYPE_SIZE_UNIT (gnu_type));
2447 gnu_max_size = size_binop (MULT_EXPR,
2448 convert (bitsizetype, gnu_max_size),
2449 TYPE_SIZE (gnu_type));
2451 else
2452 gnu_max_size_unit = NULL_TREE;
2454 /* Now build the array type. */
2455 for (index = ndim - 1; index >= 0; index --)
2457 gnu_type = build_nonshared_array_type (gnu_type,
2458 gnu_index_types[index]);
2459 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2460 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2461 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2464 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2465 TYPE_STUB_DECL (gnu_type)
2466 = create_type_stub_decl (gnu_entity_name, gnu_type);
2468 /* If we are at file level and this is a multi-dimensional array,
2469 we need to make a variable corresponding to the stride of the
2470 inner dimensions. */
2471 if (global_bindings_p () && ndim > 1)
2473 tree gnu_st_name = get_identifier ("ST");
2474 tree gnu_arr_type;
2476 for (gnu_arr_type = TREE_TYPE (gnu_type);
2477 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2478 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2479 gnu_st_name = concat_name (gnu_st_name, "ST"))
2481 tree eltype = TREE_TYPE (gnu_arr_type);
2483 TYPE_SIZE (gnu_arr_type)
2484 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2485 gnat_entity, gnu_st_name,
2486 definition, false);
2488 /* ??? For now, store the size as a multiple of the
2489 alignment of the element type in bytes so that we
2490 can see the alignment from the tree. */
2491 TYPE_SIZE_UNIT (gnu_arr_type)
2492 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2493 gnat_entity,
2494 concat_name (gnu_st_name, "A_U"),
2495 definition, false,
2496 TYPE_ALIGN (eltype));
2498 /* ??? create_type_decl is not invoked on the inner types so
2499 the MULT_EXPR node built above will never be marked. */
2500 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2504 /* If we need to write out a record type giving the names of the
2505 bounds for debugging purposes, do it now and make the record
2506 type a parallel type. This is not needed for a packed array
2507 since the bounds are conveyed by the original array type. */
2508 if (need_index_type_struct
2509 && debug_info_p
2510 && !Is_Packed_Array_Type (gnat_entity))
2512 tree gnu_bound_rec = make_node (RECORD_TYPE);
2513 tree gnu_field_list = NULL_TREE;
2514 tree gnu_field;
2516 TYPE_NAME (gnu_bound_rec)
2517 = create_concat_name (gnat_entity, "XA");
2519 for (index = ndim - 1; index >= 0; index--)
2521 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2522 tree gnu_index_name = TYPE_NAME (gnu_index);
2524 if (TREE_CODE (gnu_index_name) == TYPE_DECL)
2525 gnu_index_name = DECL_NAME (gnu_index_name);
2527 /* Make sure to reference the types themselves, and not just
2528 their names, as the debugger may fall back on them. */
2529 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2530 gnu_bound_rec, NULL_TREE,
2531 NULL_TREE, 0, 0);
2532 DECL_CHAIN (gnu_field) = gnu_field_list;
2533 gnu_field_list = gnu_field;
2536 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2537 add_parallel_type (TYPE_STUB_DECL (gnu_type), gnu_bound_rec);
2540 /* If this is a packed array type, make the original array type a
2541 parallel type. Otherwise, do it for the base array type if it
2542 isn't artificial to make sure it is kept in the debug info. */
2543 if (debug_info_p)
2545 if (Is_Packed_Array_Type (gnat_entity)
2546 && present_gnu_tree (Original_Array_Type (gnat_entity)))
2547 add_parallel_type (TYPE_STUB_DECL (gnu_type),
2548 gnat_to_gnu_type
2549 (Original_Array_Type (gnat_entity)));
2550 else
2552 tree gnu_base_decl
2553 = gnat_to_gnu_entity (Etype (gnat_entity), NULL_TREE, 0);
2554 if (!DECL_ARTIFICIAL (gnu_base_decl))
2555 add_parallel_type (TYPE_STUB_DECL (gnu_type),
2556 TREE_TYPE (TREE_TYPE (gnu_base_decl)));
2560 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2561 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2562 = (Is_Packed_Array_Type (gnat_entity)
2563 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2565 /* If the size is self-referential and the maximum size doesn't
2566 overflow, use it. */
2567 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2568 && gnu_max_size
2569 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2570 && TREE_OVERFLOW (gnu_max_size))
2571 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2572 && TREE_OVERFLOW (gnu_max_size_unit)))
2574 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2575 TYPE_SIZE (gnu_type));
2576 TYPE_SIZE_UNIT (gnu_type)
2577 = size_binop (MIN_EXPR, gnu_max_size_unit,
2578 TYPE_SIZE_UNIT (gnu_type));
2581 /* Set our alias set to that of our base type. This gives all
2582 array subtypes the same alias set. */
2583 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2585 /* If this is a packed type, make this type the same as the packed
2586 array type, but do some adjusting in the type first. */
2587 if (Present (Packed_Array_Type (gnat_entity)))
2589 Entity_Id gnat_index;
2590 tree gnu_inner;
2592 /* First finish the type we had been making so that we output
2593 debugging information for it. */
2594 if (Treat_As_Volatile (gnat_entity))
2595 gnu_type
2596 = build_qualified_type (gnu_type,
2597 TYPE_QUALS (gnu_type)
2598 | TYPE_QUAL_VOLATILE);
2600 /* Make it artificial only if the base type was artificial too.
2601 That's sort of "morally" true and will make it possible for
2602 the debugger to look it up by name in DWARF, which is needed
2603 in order to decode the packed array type. */
2604 gnu_decl
2605 = create_type_decl (gnu_entity_name, gnu_type, attr_list,
2606 !Comes_From_Source (Etype (gnat_entity))
2607 && !Comes_From_Source (gnat_entity),
2608 debug_info_p, gnat_entity);
2610 /* Save it as our equivalent in case the call below elaborates
2611 this type again. */
2612 save_gnu_tree (gnat_entity, gnu_decl, false);
2614 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2615 NULL_TREE, 0);
2616 this_made_decl = true;
2617 gnu_type = TREE_TYPE (gnu_decl);
2618 save_gnu_tree (gnat_entity, NULL_TREE, false);
2620 gnu_inner = gnu_type;
2621 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2622 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2623 || TYPE_PADDING_P (gnu_inner)))
2624 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2626 /* We need to attach the index type to the type we just made so
2627 that the actual bounds can later be put into a template. */
2628 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2629 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2630 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2631 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2633 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2635 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2636 TYPE_MODULUS for modular types so we make an extra
2637 subtype if necessary. */
2638 if (TYPE_MODULAR_P (gnu_inner))
2640 tree gnu_subtype
2641 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2642 TREE_TYPE (gnu_subtype) = gnu_inner;
2643 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2644 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2645 TYPE_MIN_VALUE (gnu_inner));
2646 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2647 TYPE_MAX_VALUE (gnu_inner));
2648 gnu_inner = gnu_subtype;
2651 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2653 #ifdef ENABLE_CHECKING
2654 /* Check for other cases of overloading. */
2655 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2656 #endif
2659 for (gnat_index = First_Index (gnat_entity);
2660 Present (gnat_index);
2661 gnat_index = Next_Index (gnat_index))
2662 SET_TYPE_ACTUAL_BOUNDS
2663 (gnu_inner,
2664 tree_cons (NULL_TREE,
2665 get_unpadded_type (Etype (gnat_index)),
2666 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2668 if (Convention (gnat_entity) != Convention_Fortran)
2669 SET_TYPE_ACTUAL_BOUNDS
2670 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2672 if (TREE_CODE (gnu_type) == RECORD_TYPE
2673 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2674 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2678 else
2679 /* Abort if packed array with no Packed_Array_Type field set. */
2680 gcc_assert (!Is_Packed (gnat_entity));
2682 break;
2684 case E_String_Literal_Subtype:
2685 /* Create the type for a string literal. */
2687 Entity_Id gnat_full_type
2688 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2689 && Present (Full_View (Etype (gnat_entity)))
2690 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2691 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2692 tree gnu_string_array_type
2693 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2694 tree gnu_string_index_type
2695 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2696 (TYPE_DOMAIN (gnu_string_array_type))));
2697 tree gnu_lower_bound
2698 = convert (gnu_string_index_type,
2699 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2700 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2701 tree gnu_length = ssize_int (length - 1);
2702 tree gnu_upper_bound
2703 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2704 gnu_lower_bound,
2705 convert (gnu_string_index_type, gnu_length));
2706 tree gnu_index_type
2707 = create_index_type (convert (sizetype, gnu_lower_bound),
2708 convert (sizetype, gnu_upper_bound),
2709 create_range_type (gnu_string_index_type,
2710 gnu_lower_bound,
2711 gnu_upper_bound),
2712 gnat_entity);
2714 gnu_type
2715 = build_nonshared_array_type (gnat_to_gnu_type
2716 (Component_Type (gnat_entity)),
2717 gnu_index_type);
2718 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2719 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2720 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2722 break;
2724 /* Record Types and Subtypes
2726 The following fields are defined on record types:
2728 Has_Discriminants True if the record has discriminants
2729 First_Discriminant Points to head of list of discriminants
2730 First_Entity Points to head of list of fields
2731 Is_Tagged_Type True if the record is tagged
2733 Implementation of Ada records and discriminated records:
2735 A record type definition is transformed into the equivalent of a C
2736 struct definition. The fields that are the discriminants which are
2737 found in the Full_Type_Declaration node and the elements of the
2738 Component_List found in the Record_Type_Definition node. The
2739 Component_List can be a recursive structure since each Variant of
2740 the Variant_Part of the Component_List has a Component_List.
2742 Processing of a record type definition comprises starting the list of
2743 field declarations here from the discriminants and the calling the
2744 function components_to_record to add the rest of the fields from the
2745 component list and return the gnu type node. The function
2746 components_to_record will call itself recursively as it traverses
2747 the tree. */
2749 case E_Record_Type:
2750 if (Has_Complex_Representation (gnat_entity))
2752 gnu_type
2753 = build_complex_type
2754 (get_unpadded_type
2755 (Etype (Defining_Entity
2756 (First (Component_Items
2757 (Component_List
2758 (Type_Definition
2759 (Declaration_Node (gnat_entity)))))))));
2761 break;
2765 Node_Id full_definition = Declaration_Node (gnat_entity);
2766 Node_Id record_definition = Type_Definition (full_definition);
2767 Entity_Id gnat_field;
2768 tree gnu_field, gnu_field_list = NULL_TREE, gnu_get_parent;
2769 /* Set PACKED in keeping with gnat_to_gnu_field. */
2770 int packed
2771 = Is_Packed (gnat_entity)
2773 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2774 ? -1
2775 : (Known_Alignment (gnat_entity)
2776 || (Strict_Alignment (gnat_entity)
2777 && Known_Static_Esize (gnat_entity)))
2778 ? -2
2779 : 0;
2780 bool has_discr = Has_Discriminants (gnat_entity);
2781 bool has_rep = Has_Specified_Layout (gnat_entity);
2782 bool all_rep = has_rep;
2783 bool is_extension
2784 = (Is_Tagged_Type (gnat_entity)
2785 && Nkind (record_definition) == N_Derived_Type_Definition);
2786 bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2788 /* See if all fields have a rep clause. Stop when we find one
2789 that doesn't. */
2790 if (all_rep)
2791 for (gnat_field = First_Entity (gnat_entity);
2792 Present (gnat_field);
2793 gnat_field = Next_Entity (gnat_field))
2794 if ((Ekind (gnat_field) == E_Component
2795 || Ekind (gnat_field) == E_Discriminant)
2796 && No (Component_Clause (gnat_field)))
2798 all_rep = false;
2799 break;
2802 /* If this is a record extension, go a level further to find the
2803 record definition. Also, verify we have a Parent_Subtype. */
2804 if (is_extension)
2806 if (!type_annotate_only
2807 || Present (Record_Extension_Part (record_definition)))
2808 record_definition = Record_Extension_Part (record_definition);
2810 gcc_assert (type_annotate_only
2811 || Present (Parent_Subtype (gnat_entity)));
2814 /* Make a node for the record. If we are not defining the record,
2815 suppress expanding incomplete types. */
2816 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2817 TYPE_NAME (gnu_type) = gnu_entity_name;
2818 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2820 if (!definition)
2822 defer_incomplete_level++;
2823 this_deferred = true;
2826 /* If both a size and rep clause was specified, put the size in
2827 the record type now so that it can get the proper mode. */
2828 if (has_rep && Known_Esize (gnat_entity))
2829 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2831 /* Always set the alignment here so that it can be used to
2832 set the mode, if it is making the alignment stricter. If
2833 it is invalid, it will be checked again below. If this is to
2834 be Atomic, choose a default alignment of a word unless we know
2835 the size and it's smaller. */
2836 if (Known_Alignment (gnat_entity))
2837 TYPE_ALIGN (gnu_type)
2838 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2839 else if (Is_Atomic (gnat_entity))
2840 TYPE_ALIGN (gnu_type)
2841 = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2842 /* If a type needs strict alignment, the minimum size will be the
2843 type size instead of the RM size (see validate_size). Cap the
2844 alignment, lest it causes this type size to become too large. */
2845 else if (Strict_Alignment (gnat_entity)
2846 && Known_Static_Esize (gnat_entity))
2848 unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2849 unsigned int raw_align = raw_size & -raw_size;
2850 if (raw_align < BIGGEST_ALIGNMENT)
2851 TYPE_ALIGN (gnu_type) = raw_align;
2853 else
2854 TYPE_ALIGN (gnu_type) = 0;
2856 /* If we have a Parent_Subtype, make a field for the parent. If
2857 this record has rep clauses, force the position to zero. */
2858 if (Present (Parent_Subtype (gnat_entity)))
2860 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2861 tree gnu_parent;
2863 /* A major complexity here is that the parent subtype will
2864 reference our discriminants in its Discriminant_Constraint
2865 list. But those must reference the parent component of this
2866 record which is of the parent subtype we have not built yet!
2867 To break the circle we first build a dummy COMPONENT_REF which
2868 represents the "get to the parent" operation and initialize
2869 each of those discriminants to a COMPONENT_REF of the above
2870 dummy parent referencing the corresponding discriminant of the
2871 base type of the parent subtype. */
2872 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2873 build0 (PLACEHOLDER_EXPR, gnu_type),
2874 build_decl (input_location,
2875 FIELD_DECL, NULL_TREE,
2876 void_type_node),
2877 NULL_TREE);
2879 if (has_discr)
2880 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2881 Present (gnat_field);
2882 gnat_field = Next_Stored_Discriminant (gnat_field))
2883 if (Present (Corresponding_Discriminant (gnat_field)))
2885 tree gnu_field
2886 = gnat_to_gnu_field_decl (Corresponding_Discriminant
2887 (gnat_field));
2888 save_gnu_tree
2889 (gnat_field,
2890 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2891 gnu_get_parent, gnu_field, NULL_TREE),
2892 true);
2895 /* Then we build the parent subtype. If it has discriminants but
2896 the type itself has unknown discriminants, this means that it
2897 doesn't contain information about how the discriminants are
2898 derived from those of the ancestor type, so it cannot be used
2899 directly. Instead it is built by cloning the parent subtype
2900 of the underlying record view of the type, for which the above
2901 derivation of discriminants has been made explicit. */
2902 if (Has_Discriminants (gnat_parent)
2903 && Has_Unknown_Discriminants (gnat_entity))
2905 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
2907 /* If we are defining the type, the underlying record
2908 view must already have been elaborated at this point.
2909 Otherwise do it now as its parent subtype cannot be
2910 technically elaborated on its own. */
2911 if (definition)
2912 gcc_assert (present_gnu_tree (gnat_uview));
2913 else
2914 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
2916 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
2918 /* Substitute the "get to the parent" of the type for that
2919 of its underlying record view in the cloned type. */
2920 for (gnat_field = First_Stored_Discriminant (gnat_uview);
2921 Present (gnat_field);
2922 gnat_field = Next_Stored_Discriminant (gnat_field))
2923 if (Present (Corresponding_Discriminant (gnat_field)))
2925 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
2926 tree gnu_ref
2927 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2928 gnu_get_parent, gnu_field, NULL_TREE);
2929 gnu_parent
2930 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
2933 else
2934 gnu_parent = gnat_to_gnu_type (gnat_parent);
2936 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2937 initially built. The discriminants must reference the fields
2938 of the parent subtype and not those of its base type for the
2939 placeholder machinery to properly work. */
2940 if (has_discr)
2942 /* The actual parent subtype is the full view. */
2943 if (IN (Ekind (gnat_parent), Private_Kind))
2945 if (Present (Full_View (gnat_parent)))
2946 gnat_parent = Full_View (gnat_parent);
2947 else
2948 gnat_parent = Underlying_Full_View (gnat_parent);
2951 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2952 Present (gnat_field);
2953 gnat_field = Next_Stored_Discriminant (gnat_field))
2954 if (Present (Corresponding_Discriminant (gnat_field)))
2956 Entity_Id field = Empty;
2957 for (field = First_Stored_Discriminant (gnat_parent);
2958 Present (field);
2959 field = Next_Stored_Discriminant (field))
2960 if (same_discriminant_p (gnat_field, field))
2961 break;
2962 gcc_assert (Present (field));
2963 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2964 = gnat_to_gnu_field_decl (field);
2968 /* The "get to the parent" COMPONENT_REF must be given its
2969 proper type... */
2970 TREE_TYPE (gnu_get_parent) = gnu_parent;
2972 /* ...and reference the _Parent field of this record. */
2973 gnu_field
2974 = create_field_decl (parent_name_id,
2975 gnu_parent, gnu_type,
2976 has_rep
2977 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
2978 has_rep
2979 ? bitsize_zero_node : NULL_TREE,
2980 0, 1);
2981 DECL_INTERNAL_P (gnu_field) = 1;
2982 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
2983 TYPE_FIELDS (gnu_type) = gnu_field;
2986 /* Make the fields for the discriminants and put them into the record
2987 unless it's an Unchecked_Union. */
2988 if (has_discr)
2989 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2990 Present (gnat_field);
2991 gnat_field = Next_Stored_Discriminant (gnat_field))
2993 /* If this is a record extension and this discriminant is the
2994 renaming of another discriminant, we've handled it above. */
2995 if (Present (Parent_Subtype (gnat_entity))
2996 && Present (Corresponding_Discriminant (gnat_field)))
2997 continue;
2999 gnu_field
3000 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
3001 debug_info_p);
3003 /* Make an expression using a PLACEHOLDER_EXPR from the
3004 FIELD_DECL node just created and link that with the
3005 corresponding GNAT defining identifier. */
3006 save_gnu_tree (gnat_field,
3007 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
3008 build0 (PLACEHOLDER_EXPR, gnu_type),
3009 gnu_field, NULL_TREE),
3010 true);
3012 if (!is_unchecked_union)
3014 DECL_CHAIN (gnu_field) = gnu_field_list;
3015 gnu_field_list = gnu_field;
3019 /* Add the fields into the record type and finish it up. */
3020 components_to_record (gnu_type, Component_List (record_definition),
3021 gnu_field_list, packed, definition, false,
3022 all_rep, is_unchecked_union, debug_info_p,
3023 false, OK_To_Reorder_Components (gnat_entity),
3024 NULL);
3026 /* If it is passed by reference, force BLKmode to ensure that objects
3027 of this type will always be put in memory. */
3028 if (Is_By_Reference_Type (gnat_entity))
3029 SET_TYPE_MODE (gnu_type, BLKmode);
3031 /* We used to remove the associations of the discriminants and _Parent
3032 for validity checking but we may need them if there's a Freeze_Node
3033 for a subtype used in this record. */
3034 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3036 /* Fill in locations of fields. */
3037 annotate_rep (gnat_entity, gnu_type);
3039 /* If there are any entities in the chain corresponding to components
3040 that we did not elaborate, ensure we elaborate their types if they
3041 are Itypes. */
3042 for (gnat_temp = First_Entity (gnat_entity);
3043 Present (gnat_temp);
3044 gnat_temp = Next_Entity (gnat_temp))
3045 if ((Ekind (gnat_temp) == E_Component
3046 || Ekind (gnat_temp) == E_Discriminant)
3047 && Is_Itype (Etype (gnat_temp))
3048 && !present_gnu_tree (gnat_temp))
3049 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3051 /* If this is a record type associated with an exception definition,
3052 equate its fields to those of the standard exception type. This
3053 will make it possible to convert between them. */
3054 if (gnu_entity_name == exception_data_name_id)
3056 tree gnu_std_field;
3057 for (gnu_field = TYPE_FIELDS (gnu_type),
3058 gnu_std_field = TYPE_FIELDS (except_type_node);
3059 gnu_field;
3060 gnu_field = DECL_CHAIN (gnu_field),
3061 gnu_std_field = DECL_CHAIN (gnu_std_field))
3062 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3063 gcc_assert (!gnu_std_field);
3066 break;
3068 case E_Class_Wide_Subtype:
3069 /* If an equivalent type is present, that is what we should use.
3070 Otherwise, fall through to handle this like a record subtype
3071 since it may have constraints. */
3072 if (gnat_equiv_type != gnat_entity)
3074 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3075 maybe_present = true;
3076 break;
3079 /* ... fall through ... */
3081 case E_Record_Subtype:
3082 /* If Cloned_Subtype is Present it means this record subtype has
3083 identical layout to that type or subtype and we should use
3084 that GCC type for this one. The front end guarantees that
3085 the component list is shared. */
3086 if (Present (Cloned_Subtype (gnat_entity)))
3088 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3089 NULL_TREE, 0);
3090 maybe_present = true;
3091 break;
3094 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3095 changing the type, make a new type with each field having the type of
3096 the field in the new subtype but the position computed by transforming
3097 every discriminant reference according to the constraints. We don't
3098 see any difference between private and non-private type here since
3099 derivations from types should have been deferred until the completion
3100 of the private type. */
3101 else
3103 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3104 tree gnu_base_type;
3106 if (!definition)
3108 defer_incomplete_level++;
3109 this_deferred = true;
3112 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3114 if (present_gnu_tree (gnat_entity))
3116 maybe_present = true;
3117 break;
3120 /* If this is a record subtype associated with a dispatch table,
3121 strip the suffix. This is necessary to make sure 2 different
3122 subtypes associated with the imported and exported views of a
3123 dispatch table are properly merged in LTO mode. */
3124 if (Is_Dispatch_Table_Entity (gnat_entity))
3126 char *p;
3127 Get_Encoded_Name (gnat_entity);
3128 p = strchr (Name_Buffer, '_');
3129 gcc_assert (p);
3130 strcpy (p+2, "dtS");
3131 gnu_entity_name = get_identifier (Name_Buffer);
3134 /* When the subtype has discriminants and these discriminants affect
3135 the initial shape it has inherited, factor them in. But for an
3136 Unchecked_Union (it must be an Itype), just return the type.
3137 We can't just test Is_Constrained because private subtypes without
3138 discriminants of types with discriminants with default expressions
3139 are Is_Constrained but aren't constrained! */
3140 if (IN (Ekind (gnat_base_type), Record_Kind)
3141 && !Is_Unchecked_Union (gnat_base_type)
3142 && !Is_For_Access_Subtype (gnat_entity)
3143 && Is_Constrained (gnat_entity)
3144 && Has_Discriminants (gnat_entity)
3145 && Present (Discriminant_Constraint (gnat_entity))
3146 && Stored_Constraint (gnat_entity) != No_Elist)
3148 VEC(subst_pair,heap) *gnu_subst_list
3149 = build_subst_list (gnat_entity, gnat_base_type, definition);
3150 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part, t;
3151 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3152 bool selected_variant = false;
3153 Entity_Id gnat_field;
3154 VEC(variant_desc,heap) *gnu_variant_list;
3156 gnu_type = make_node (RECORD_TYPE);
3157 TYPE_NAME (gnu_type) = gnu_entity_name;
3159 /* Set the size, alignment and alias set of the new type to
3160 match that of the old one, doing required substitutions. */
3161 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3162 gnu_subst_list);
3164 if (TYPE_IS_PADDING_P (gnu_base_type))
3165 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3166 else
3167 gnu_unpad_base_type = gnu_base_type;
3169 /* Look for a REP part in the base type. */
3170 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3172 /* Look for a variant part in the base type. */
3173 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3175 /* If there is a variant part, we must compute whether the
3176 constraints statically select a particular variant. If
3177 so, we simply drop the qualified union and flatten the
3178 list of fields. Otherwise we'll build a new qualified
3179 union for the variants that are still relevant. */
3180 if (gnu_variant_part)
3182 variant_desc *v;
3183 unsigned ix;
3185 gnu_variant_list
3186 = build_variant_list (TREE_TYPE (gnu_variant_part),
3187 gnu_subst_list, NULL);
3189 /* If all the qualifiers are unconditionally true, the
3190 innermost variant is statically selected. */
3191 selected_variant = true;
3192 FOR_EACH_VEC_ELT_REVERSE (variant_desc, gnu_variant_list,
3193 ix, v)
3194 if (!integer_onep (v->qual))
3196 selected_variant = false;
3197 break;
3200 /* Otherwise, create the new variants. */
3201 if (!selected_variant)
3202 FOR_EACH_VEC_ELT_REVERSE (variant_desc, gnu_variant_list,
3203 ix, v)
3205 tree old_variant = v->type;
3206 tree new_variant = make_node (RECORD_TYPE);
3207 TYPE_NAME (new_variant)
3208 = DECL_NAME (TYPE_NAME (old_variant));
3209 copy_and_substitute_in_size (new_variant, old_variant,
3210 gnu_subst_list);
3211 v->record = new_variant;
3214 else
3216 gnu_variant_list = NULL;
3217 selected_variant = false;
3220 gnu_pos_list
3221 = build_position_list (gnu_unpad_base_type,
3222 gnu_variant_list && !selected_variant,
3223 size_zero_node, bitsize_zero_node,
3224 BIGGEST_ALIGNMENT, NULL_TREE);
3226 for (gnat_field = First_Entity (gnat_entity);
3227 Present (gnat_field);
3228 gnat_field = Next_Entity (gnat_field))
3229 if ((Ekind (gnat_field) == E_Component
3230 || Ekind (gnat_field) == E_Discriminant)
3231 && !(Present (Corresponding_Discriminant (gnat_field))
3232 && Is_Tagged_Type (gnat_base_type))
3233 && Underlying_Type (Scope (Original_Record_Component
3234 (gnat_field)))
3235 == gnat_base_type)
3237 Name_Id gnat_name = Chars (gnat_field);
3238 Entity_Id gnat_old_field
3239 = Original_Record_Component (gnat_field);
3240 tree gnu_old_field
3241 = gnat_to_gnu_field_decl (gnat_old_field);
3242 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3243 tree gnu_field, gnu_field_type, gnu_size;
3244 tree gnu_cont_type, gnu_last = NULL_TREE;
3246 /* If the type is the same, retrieve the GCC type from the
3247 old field to take into account possible adjustments. */
3248 if (Etype (gnat_field) == Etype (gnat_old_field))
3249 gnu_field_type = TREE_TYPE (gnu_old_field);
3250 else
3251 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3253 /* If there was a component clause, the field types must be
3254 the same for the type and subtype, so copy the data from
3255 the old field to avoid recomputation here. Also if the
3256 field is justified modular and the optimization in
3257 gnat_to_gnu_field was applied. */
3258 if (Present (Component_Clause (gnat_old_field))
3259 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3260 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3261 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3262 == TREE_TYPE (gnu_old_field)))
3264 gnu_size = DECL_SIZE (gnu_old_field);
3265 gnu_field_type = TREE_TYPE (gnu_old_field);
3268 /* If the old field was packed and of constant size, we
3269 have to get the old size here, as it might differ from
3270 what the Etype conveys and the latter might overlap
3271 onto the following field. Try to arrange the type for
3272 possible better packing along the way. */
3273 else if (DECL_PACKED (gnu_old_field)
3274 && TREE_CODE (DECL_SIZE (gnu_old_field))
3275 == INTEGER_CST)
3277 gnu_size = DECL_SIZE (gnu_old_field);
3278 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
3279 && !TYPE_FAT_POINTER_P (gnu_field_type)
3280 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
3281 gnu_field_type
3282 = make_packable_type (gnu_field_type, true);
3285 else
3286 gnu_size = TYPE_SIZE (gnu_field_type);
3288 /* If the context of the old field is the base type or its
3289 REP part (if any), put the field directly in the new
3290 type; otherwise look up the context in the variant list
3291 and put the field either in the new type if there is a
3292 selected variant or in one of the new variants. */
3293 if (gnu_context == gnu_unpad_base_type
3294 || (gnu_rep_part
3295 && gnu_context == TREE_TYPE (gnu_rep_part)))
3296 gnu_cont_type = gnu_type;
3297 else
3299 variant_desc *v;
3300 unsigned ix;
3302 t = NULL_TREE;
3303 FOR_EACH_VEC_ELT_REVERSE (variant_desc,
3304 gnu_variant_list, ix, v)
3305 if (v->type == gnu_context)
3307 t = v->type;
3308 break;
3310 if (t)
3312 if (selected_variant)
3313 gnu_cont_type = gnu_type;
3314 else
3315 gnu_cont_type = v->record;
3317 else
3318 /* The front-end may pass us "ghost" components if
3319 it fails to recognize that a constrained subtype
3320 is statically constrained. Discard them. */
3321 continue;
3324 /* Now create the new field modeled on the old one. */
3325 gnu_field
3326 = create_field_decl_from (gnu_old_field, gnu_field_type,
3327 gnu_cont_type, gnu_size,
3328 gnu_pos_list, gnu_subst_list);
3330 /* Put it in one of the new variants directly. */
3331 if (gnu_cont_type != gnu_type)
3333 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3334 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3337 /* To match the layout crafted in components_to_record,
3338 if this is the _Tag or _Parent field, put it before
3339 any other fields. */
3340 else if (gnat_name == Name_uTag
3341 || gnat_name == Name_uParent)
3342 gnu_field_list = chainon (gnu_field_list, gnu_field);
3344 /* Similarly, if this is the _Controller field, put
3345 it before the other fields except for the _Tag or
3346 _Parent field. */
3347 else if (gnat_name == Name_uController && gnu_last)
3349 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
3350 DECL_CHAIN (gnu_last) = gnu_field;
3353 /* Otherwise, if this is a regular field, put it after
3354 the other fields. */
3355 else
3357 DECL_CHAIN (gnu_field) = gnu_field_list;
3358 gnu_field_list = gnu_field;
3359 if (!gnu_last)
3360 gnu_last = gnu_field;
3363 save_gnu_tree (gnat_field, gnu_field, false);
3366 /* If there is a variant list and no selected variant, we need
3367 to create the nest of variant parts from the old nest. */
3368 if (gnu_variant_list && !selected_variant)
3370 tree new_variant_part
3371 = create_variant_part_from (gnu_variant_part,
3372 gnu_variant_list, gnu_type,
3373 gnu_pos_list, gnu_subst_list);
3374 DECL_CHAIN (new_variant_part) = gnu_field_list;
3375 gnu_field_list = new_variant_part;
3378 /* Now go through the entities again looking for Itypes that
3379 we have not elaborated but should (e.g., Etypes of fields
3380 that have Original_Components). */
3381 for (gnat_field = First_Entity (gnat_entity);
3382 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3383 if ((Ekind (gnat_field) == E_Discriminant
3384 || Ekind (gnat_field) == E_Component)
3385 && !present_gnu_tree (Etype (gnat_field)))
3386 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3388 /* Do not emit debug info for the type yet since we're going to
3389 modify it below. */
3390 gnu_field_list = nreverse (gnu_field_list);
3391 finish_record_type (gnu_type, gnu_field_list, 2, false);
3393 /* See the E_Record_Type case for the rationale. */
3394 if (Is_By_Reference_Type (gnat_entity))
3395 SET_TYPE_MODE (gnu_type, BLKmode);
3396 else
3397 compute_record_mode (gnu_type);
3399 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3401 /* Fill in locations of fields. */
3402 annotate_rep (gnat_entity, gnu_type);
3404 /* If debugging information is being written for the type, write
3405 a record that shows what we are a subtype of and also make a
3406 variable that indicates our size, if still variable. */
3407 if (debug_info_p)
3409 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3410 tree gnu_unpad_base_name = TYPE_NAME (gnu_unpad_base_type);
3411 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3413 if (TREE_CODE (gnu_unpad_base_name) == TYPE_DECL)
3414 gnu_unpad_base_name = DECL_NAME (gnu_unpad_base_name);
3416 TYPE_NAME (gnu_subtype_marker)
3417 = create_concat_name (gnat_entity, "XVS");
3418 finish_record_type (gnu_subtype_marker,
3419 create_field_decl (gnu_unpad_base_name,
3420 build_reference_type
3421 (gnu_unpad_base_type),
3422 gnu_subtype_marker,
3423 NULL_TREE, NULL_TREE,
3424 0, 0),
3425 0, true);
3427 add_parallel_type (TYPE_STUB_DECL (gnu_type),
3428 gnu_subtype_marker);
3430 if (definition
3431 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3432 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3433 TYPE_SIZE_UNIT (gnu_subtype_marker)
3434 = create_var_decl (create_concat_name (gnat_entity,
3435 "XVZ"),
3436 NULL_TREE, sizetype, gnu_size_unit,
3437 false, false, false, false, NULL,
3438 gnat_entity);
3441 VEC_free (variant_desc, heap, gnu_variant_list);
3442 VEC_free (subst_pair, heap, gnu_subst_list);
3444 /* Now we can finalize it. */
3445 rest_of_record_type_compilation (gnu_type);
3448 /* Otherwise, go down all the components in the new type and make
3449 them equivalent to those in the base type. */
3450 else
3452 gnu_type = gnu_base_type;
3454 for (gnat_temp = First_Entity (gnat_entity);
3455 Present (gnat_temp);
3456 gnat_temp = Next_Entity (gnat_temp))
3457 if ((Ekind (gnat_temp) == E_Discriminant
3458 && !Is_Unchecked_Union (gnat_base_type))
3459 || Ekind (gnat_temp) == E_Component)
3460 save_gnu_tree (gnat_temp,
3461 gnat_to_gnu_field_decl
3462 (Original_Record_Component (gnat_temp)),
3463 false);
3466 break;
3468 case E_Access_Subprogram_Type:
3469 /* Use the special descriptor type for dispatch tables if needed,
3470 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3471 Note that we are only required to do so for static tables in
3472 order to be compatible with the C++ ABI, but Ada 2005 allows
3473 to extend library level tagged types at the local level so
3474 we do it in the non-static case as well. */
3475 if (TARGET_VTABLE_USES_DESCRIPTORS
3476 && Is_Dispatch_Table_Entity (gnat_entity))
3478 gnu_type = fdesc_type_node;
3479 gnu_size = TYPE_SIZE (gnu_type);
3480 break;
3483 /* ... fall through ... */
3485 case E_Anonymous_Access_Subprogram_Type:
3486 /* If we are not defining this entity, and we have incomplete
3487 entities being processed above us, make a dummy type and
3488 fill it in later. */
3489 if (!definition && defer_incomplete_level != 0)
3491 struct incomplete *p
3492 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3494 gnu_type
3495 = build_pointer_type
3496 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3497 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
3498 !Comes_From_Source (gnat_entity),
3499 debug_info_p, gnat_entity);
3500 this_made_decl = true;
3501 gnu_type = TREE_TYPE (gnu_decl);
3502 save_gnu_tree (gnat_entity, gnu_decl, false);
3503 saved = true;
3505 p->old_type = TREE_TYPE (gnu_type);
3506 p->full_type = Directly_Designated_Type (gnat_entity);
3507 p->next = defer_incomplete_list;
3508 defer_incomplete_list = p;
3509 break;
3512 /* ... fall through ... */
3514 case E_Allocator_Type:
3515 case E_Access_Type:
3516 case E_Access_Attribute_Type:
3517 case E_Anonymous_Access_Type:
3518 case E_General_Access_Type:
3520 /* The designated type and its equivalent type for gigi. */
3521 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3522 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3523 /* Whether it comes from a limited with. */
3524 bool is_from_limited_with
3525 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3526 && From_With_Type (gnat_desig_equiv));
3527 /* The "full view" of the designated type. If this is an incomplete
3528 entity from a limited with, treat its non-limited view as the full
3529 view. Otherwise, if this is an incomplete or private type, use the
3530 full view. In the former case, we might point to a private type,
3531 in which case, we need its full view. Also, we want to look at the
3532 actual type used for the representation, so this takes a total of
3533 three steps. */
3534 Entity_Id gnat_desig_full_direct_first
3535 = (is_from_limited_with
3536 ? Non_Limited_View (gnat_desig_equiv)
3537 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3538 ? Full_View (gnat_desig_equiv) : Empty));
3539 Entity_Id gnat_desig_full_direct
3540 = ((is_from_limited_with
3541 && Present (gnat_desig_full_direct_first)
3542 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3543 ? Full_View (gnat_desig_full_direct_first)
3544 : gnat_desig_full_direct_first);
3545 Entity_Id gnat_desig_full
3546 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3547 /* The type actually used to represent the designated type, either
3548 gnat_desig_full or gnat_desig_equiv. */
3549 Entity_Id gnat_desig_rep;
3550 /* True if this is a pointer to an unconstrained array. */
3551 bool is_unconstrained_array;
3552 /* We want to know if we'll be seeing the freeze node for any
3553 incomplete type we may be pointing to. */
3554 bool in_main_unit
3555 = (Present (gnat_desig_full)
3556 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3557 : In_Extended_Main_Code_Unit (gnat_desig_type));
3558 /* True if we make a dummy type here. */
3559 bool made_dummy = false;
3560 /* The mode to be used for the pointer type. */
3561 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3562 /* The GCC type used for the designated type. */
3563 tree gnu_desig_type = NULL_TREE;
3565 if (!targetm.valid_pointer_mode (p_mode))
3566 p_mode = ptr_mode;
3568 /* If either the designated type or its full view is an unconstrained
3569 array subtype, replace it with the type it's a subtype of. This
3570 avoids problems with multiple copies of unconstrained array types.
3571 Likewise, if the designated type is a subtype of an incomplete
3572 record type, use the parent type to avoid order of elaboration
3573 issues. This can lose some code efficiency, but there is no
3574 alternative. */
3575 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3576 && !Is_Constrained (gnat_desig_equiv))
3577 gnat_desig_equiv = Etype (gnat_desig_equiv);
3578 if (Present (gnat_desig_full)
3579 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3580 && !Is_Constrained (gnat_desig_full))
3581 || (Ekind (gnat_desig_full) == E_Record_Subtype
3582 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3583 gnat_desig_full = Etype (gnat_desig_full);
3585 /* Set the type that's actually the representation of the designated
3586 type and also flag whether we have a unconstrained array. */
3587 gnat_desig_rep
3588 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3589 is_unconstrained_array
3590 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3592 /* If we are pointing to an incomplete type whose completion is an
3593 unconstrained array, make dummy fat and thin pointer types to it.
3594 Likewise if the type itself is dummy or an unconstrained array. */
3595 if (is_unconstrained_array
3596 && (Present (gnat_desig_full)
3597 || (present_gnu_tree (gnat_desig_equiv)
3598 && TYPE_IS_DUMMY_P
3599 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3600 || (!in_main_unit
3601 && defer_incomplete_level != 0
3602 && !present_gnu_tree (gnat_desig_equiv))
3603 || (in_main_unit
3604 && is_from_limited_with
3605 && Present (Freeze_Node (gnat_desig_equiv)))))
3607 if (present_gnu_tree (gnat_desig_rep))
3608 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3609 else
3611 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3612 made_dummy = true;
3615 /* If the call above got something that has a pointer, the pointer
3616 is our type. This could have happened either because the type
3617 was elaborated or because somebody else executed the code. */
3618 if (!TYPE_POINTER_TO (gnu_desig_type))
3619 build_dummy_unc_pointer_types (gnat_desig_equiv, gnu_desig_type);
3620 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3623 /* If we already know what the full type is, use it. */
3624 else if (Present (gnat_desig_full)
3625 && present_gnu_tree (gnat_desig_full))
3626 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3628 /* Get the type of the thing we are to point to and build a pointer to
3629 it. If it is a reference to an incomplete or private type with a
3630 full view that is a record, make a dummy type node and get the
3631 actual type later when we have verified it is safe. */
3632 else if ((!in_main_unit
3633 && !present_gnu_tree (gnat_desig_equiv)
3634 && Present (gnat_desig_full)
3635 && !present_gnu_tree (gnat_desig_full)
3636 && Is_Record_Type (gnat_desig_full))
3637 /* Likewise if we are pointing to a record or array and we are
3638 to defer elaborating incomplete types. We do this as this
3639 access type may be the full view of a private type. Note
3640 that the unconstrained array case is handled above. */
3641 || ((!in_main_unit || imported_p)
3642 && defer_incomplete_level != 0
3643 && !present_gnu_tree (gnat_desig_equiv)
3644 && (Is_Record_Type (gnat_desig_rep)
3645 || Is_Array_Type (gnat_desig_rep)))
3646 /* If this is a reference from a limited_with type back to our
3647 main unit and there's a freeze node for it, either we have
3648 already processed the declaration and made the dummy type,
3649 in which case we just reuse the latter, or we have not yet,
3650 in which case we make the dummy type and it will be reused
3651 when the declaration is finally processed. In both cases,
3652 the pointer eventually created below will be automatically
3653 adjusted when the freeze node is processed. Note that the
3654 unconstrained array case is handled above. */
3655 || (in_main_unit
3656 && is_from_limited_with
3657 && Present (Freeze_Node (gnat_desig_rep))))
3659 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3660 made_dummy = true;
3663 /* Otherwise handle the case of a pointer to itself. */
3664 else if (gnat_desig_equiv == gnat_entity)
3666 gnu_type
3667 = build_pointer_type_for_mode (void_type_node, p_mode,
3668 No_Strict_Aliasing (gnat_entity));
3669 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3672 /* If expansion is disabled, the equivalent type of a concurrent type
3673 is absent, so build a dummy pointer type. */
3674 else if (type_annotate_only && No (gnat_desig_equiv))
3675 gnu_type = ptr_void_type_node;
3677 /* Finally, handle the default case where we can just elaborate our
3678 designated type. */
3679 else
3680 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3682 /* It is possible that a call to gnat_to_gnu_type above resolved our
3683 type. If so, just return it. */
3684 if (present_gnu_tree (gnat_entity))
3686 maybe_present = true;
3687 break;
3690 /* If we have not done it yet, build the pointer type the usual way. */
3691 if (!gnu_type)
3693 /* Modify the designated type if we are pointing only to constant
3694 objects, but don't do it for unconstrained arrays. */
3695 if (Is_Access_Constant (gnat_entity)
3696 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3698 gnu_desig_type
3699 = build_qualified_type
3700 (gnu_desig_type,
3701 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3703 /* Some extra processing is required if we are building a
3704 pointer to an incomplete type (in the GCC sense). We might
3705 have such a type if we just made a dummy, or directly out
3706 of the call to gnat_to_gnu_type above if we are processing
3707 an access type for a record component designating the
3708 record type itself. */
3709 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3711 /* We must ensure that the pointer to variant we make will
3712 be processed by update_pointer_to when the initial type
3713 is completed. Pretend we made a dummy and let further
3714 processing act as usual. */
3715 made_dummy = true;
3717 /* We must ensure that update_pointer_to will not retrieve
3718 the dummy variant when building a properly qualified
3719 version of the complete type. We take advantage of the
3720 fact that get_qualified_type is requiring TYPE_NAMEs to
3721 match to influence build_qualified_type and then also
3722 update_pointer_to here. */
3723 TYPE_NAME (gnu_desig_type)
3724 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3728 gnu_type
3729 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3730 No_Strict_Aliasing (gnat_entity));
3733 /* If we are not defining this object and we have made a dummy pointer,
3734 save our current definition, evaluate the actual type, and replace
3735 the tentative type we made with the actual one. If we are to defer
3736 actually looking up the actual type, make an entry in the deferred
3737 list. If this is from a limited with, we may have to defer to the
3738 end of the current unit. */
3739 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3741 tree gnu_old_desig_type;
3743 if (TYPE_IS_FAT_POINTER_P (gnu_type))
3745 gnu_old_desig_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
3746 if (esize == POINTER_SIZE)
3747 gnu_type = build_pointer_type
3748 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type));
3750 else
3751 gnu_old_desig_type = TREE_TYPE (gnu_type);
3753 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
3754 !Comes_From_Source (gnat_entity),
3755 debug_info_p, gnat_entity);
3756 this_made_decl = true;
3757 gnu_type = TREE_TYPE (gnu_decl);
3758 save_gnu_tree (gnat_entity, gnu_decl, false);
3759 saved = true;
3761 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3762 update gnu_old_desig_type directly, in which case it will not be
3763 a dummy type any more when we get into update_pointer_to.
3765 This can happen e.g. when the designated type is a record type,
3766 because their elaboration starts with an initial node from
3767 make_dummy_type, which may be the same node as the one we got.
3769 Besides, variants of this non-dummy type might have been created
3770 along the way. update_pointer_to is expected to properly take
3771 care of those situations. */
3772 if (defer_incomplete_level == 0 && !is_from_limited_with)
3774 defer_finalize_level++;
3775 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
3776 gnat_to_gnu_type (gnat_desig_equiv));
3777 defer_finalize_level--;
3779 else
3781 struct incomplete *p = XNEW (struct incomplete);
3782 struct incomplete **head
3783 = (is_from_limited_with
3784 ? &defer_limited_with : &defer_incomplete_list);
3785 p->old_type = gnu_old_desig_type;
3786 p->full_type = gnat_desig_equiv;
3787 p->next = *head;
3788 *head = p;
3792 break;
3794 case E_Access_Protected_Subprogram_Type:
3795 case E_Anonymous_Access_Protected_Subprogram_Type:
3796 if (type_annotate_only && No (gnat_equiv_type))
3797 gnu_type = ptr_void_type_node;
3798 else
3800 /* The run-time representation is the equivalent type. */
3801 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3802 maybe_present = true;
3805 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3806 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3807 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3808 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3809 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3810 NULL_TREE, 0);
3812 break;
3814 case E_Access_Subtype:
3816 /* We treat this as identical to its base type; any constraint is
3817 meaningful only to the front end.
3819 The designated type must be elaborated as well, if it does
3820 not have its own freeze node. Designated (sub)types created
3821 for constrained components of records with discriminants are
3822 not frozen by the front end and thus not elaborated by gigi,
3823 because their use may appear before the base type is frozen,
3824 and because it is not clear that they are needed anywhere in
3825 Gigi. With the current model, there is no correct place where
3826 they could be elaborated. */
3828 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3829 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3830 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3831 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3832 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3834 /* If we are not defining this entity, and we have incomplete
3835 entities being processed above us, make a dummy type and
3836 elaborate it later. */
3837 if (!definition && defer_incomplete_level != 0)
3839 struct incomplete *p
3840 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3841 tree gnu_ptr_type
3842 = build_pointer_type
3843 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3845 p->old_type = TREE_TYPE (gnu_ptr_type);
3846 p->full_type = Directly_Designated_Type (gnat_entity);
3847 p->next = defer_incomplete_list;
3848 defer_incomplete_list = p;
3850 else if (!IN (Ekind (Base_Type
3851 (Directly_Designated_Type (gnat_entity))),
3852 Incomplete_Or_Private_Kind))
3853 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3854 NULL_TREE, 0);
3857 maybe_present = true;
3858 break;
3860 /* Subprogram Entities
3862 The following access functions are defined for subprograms:
3864 Etype Return type or Standard_Void_Type.
3865 First_Formal The first formal parameter.
3866 Is_Imported Indicates that the subprogram has appeared in
3867 an INTERFACE or IMPORT pragma. For now we
3868 assume that the external language is C.
3869 Is_Exported Likewise but for an EXPORT pragma.
3870 Is_Inlined True if the subprogram is to be inlined.
3872 Each parameter is first checked by calling must_pass_by_ref on its
3873 type to determine if it is passed by reference. For parameters which
3874 are copied in, if they are Ada In Out or Out parameters, their return
3875 value becomes part of a record which becomes the return type of the
3876 function (C function - note that this applies only to Ada procedures
3877 so there is no Ada return type). Additional code to store back the
3878 parameters will be generated on the caller side. This transformation
3879 is done here, not in the front-end.
3881 The intended result of the transformation can be seen from the
3882 equivalent source rewritings that follow:
3884 struct temp {int a,b};
3885 procedure P (A,B: In Out ...) is temp P (int A,B)
3886 begin {
3887 .. ..
3888 end P; return {A,B};
3891 temp t;
3892 P(X,Y); t = P(X,Y);
3893 X = t.a , Y = t.b;
3895 For subprogram types we need to perform mainly the same conversions to
3896 GCC form that are needed for procedures and function declarations. The
3897 only difference is that at the end, we make a type declaration instead
3898 of a function declaration. */
3900 case E_Subprogram_Type:
3901 case E_Function:
3902 case E_Procedure:
3904 /* The type returned by a function or else Standard_Void_Type for a
3905 procedure. */
3906 Entity_Id gnat_return_type = Etype (gnat_entity);
3907 tree gnu_return_type;
3908 /* The first GCC parameter declaration (a PARM_DECL node). The
3909 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
3910 actually is the head of this parameter list. */
3911 tree gnu_param_list = NULL_TREE;
3912 /* Likewise for the stub associated with an exported procedure. */
3913 tree gnu_stub_param_list = NULL_TREE;
3914 /* Non-null for subprograms containing parameters passed by copy-in
3915 copy-out (Ada In Out or Out parameters not passed by reference),
3916 in which case it is the list of nodes used to specify the values
3917 of the In Out/Out parameters that are returned as a record upon
3918 procedure return. The TREE_PURPOSE of an element of this list is
3919 a field of the record and the TREE_VALUE is the PARM_DECL
3920 corresponding to that field. This list will be saved in the
3921 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3922 tree gnu_cico_list = NULL_TREE;
3923 /* List of fields in return type of procedure with copy-in copy-out
3924 parameters. */
3925 tree gnu_field_list = NULL_TREE;
3926 /* If an import pragma asks to map this subprogram to a GCC builtin,
3927 this is the builtin DECL node. */
3928 tree gnu_builtin_decl = NULL_TREE;
3929 /* For the stub associated with an exported procedure. */
3930 tree gnu_stub_type = NULL_TREE, gnu_stub_name = NULL_TREE;
3931 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
3932 Entity_Id gnat_param;
3933 bool inline_flag = Is_Inlined (gnat_entity);
3934 bool public_flag = Is_Public (gnat_entity) || imported_p;
3935 bool extern_flag
3936 = (Is_Public (gnat_entity) && !definition) || imported_p;
3937 /* The semantics of "pure" in Ada essentially matches that of "const"
3938 in the back-end. In particular, both properties are orthogonal to
3939 the "nothrow" property if the EH circuitry is explicit in the
3940 internal representation of the back-end. If we are to completely
3941 hide the EH circuitry from it, we need to declare that calls to pure
3942 Ada subprograms that can throw have side effects since they can
3943 trigger an "abnormal" transfer of control flow; thus they can be
3944 neither "const" nor "pure" in the back-end sense. */
3945 bool const_flag
3946 = (Exception_Mechanism == Back_End_Exceptions
3947 && Is_Pure (gnat_entity));
3948 bool volatile_flag = No_Return (gnat_entity);
3949 bool return_by_direct_ref_p = false;
3950 bool return_by_invisi_ref_p = false;
3951 bool return_unconstrained_p = false;
3952 bool has_stub = false;
3953 int parmnum;
3955 /* A parameter may refer to this type, so defer completion of any
3956 incomplete types. */
3957 if (kind == E_Subprogram_Type && !definition)
3959 defer_incomplete_level++;
3960 this_deferred = true;
3963 /* If the subprogram has an alias, it is probably inherited, so
3964 we can use the original one. If the original "subprogram"
3965 is actually an enumeration literal, it may be the first use
3966 of its type, so we must elaborate that type now. */
3967 if (Present (Alias (gnat_entity)))
3969 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3970 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3972 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity), gnu_expr, 0);
3974 /* Elaborate any Itypes in the parameters of this entity. */
3975 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3976 Present (gnat_temp);
3977 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3978 if (Is_Itype (Etype (gnat_temp)))
3979 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3981 break;
3984 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3985 corresponding DECL node. Proper generation of calls later on need
3986 proper parameter associations so we don't "break;" here. */
3987 if (Convention (gnat_entity) == Convention_Intrinsic
3988 && Present (Interface_Name (gnat_entity)))
3990 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3992 /* Inability to find the builtin decl most often indicates a
3993 genuine mistake, but imports of unregistered intrinsics are
3994 sometimes issued on purpose to allow hooking in alternate
3995 bodies. We post a warning conditioned on Wshadow in this case,
3996 to let developers be notified on demand without risking false
3997 positives with common default sets of options. */
3999 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
4000 post_error ("?gcc intrinsic not found for&!", gnat_entity);
4003 /* ??? What if we don't find the builtin node above ? warn ? err ?
4004 In the current state we neither warn nor err, and calls will just
4005 be handled as for regular subprograms. */
4007 /* Look into the return type and get its associated GCC tree. If it
4008 is not void, compute various flags for the subprogram type. */
4009 if (Ekind (gnat_return_type) == E_Void)
4010 gnu_return_type = void_type_node;
4011 else
4013 gnu_return_type = gnat_to_gnu_type (gnat_return_type);
4015 /* If this function returns by reference, make the actual return
4016 type the pointer type and make a note of that. */
4017 if (Returns_By_Ref (gnat_entity))
4019 gnu_return_type = build_pointer_type (gnu_return_type);
4020 return_by_direct_ref_p = true;
4023 /* If we are supposed to return an unconstrained array type, make
4024 the actual return type the fat pointer type. */
4025 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4027 gnu_return_type = TREE_TYPE (gnu_return_type);
4028 return_unconstrained_p = true;
4031 /* Likewise, if the return type requires a transient scope, the
4032 return value will be allocated on the secondary stack so the
4033 actual return type is the pointer type. */
4034 else if (Requires_Transient_Scope (gnat_return_type))
4036 gnu_return_type = build_pointer_type (gnu_return_type);
4037 return_unconstrained_p = true;
4040 /* If the Mechanism is By_Reference, ensure this function uses the
4041 target's by-invisible-reference mechanism, which may not be the
4042 same as above (e.g. it might be passing an extra parameter). */
4043 else if (kind == E_Function
4044 && Mechanism (gnat_entity) == By_Reference)
4045 return_by_invisi_ref_p = true;
4047 /* Likewise, if the return type is itself By_Reference. */
4048 else if (TREE_ADDRESSABLE (gnu_return_type))
4049 return_by_invisi_ref_p = true;
4051 /* If the type is a padded type and the underlying type would not
4052 be passed by reference or the function has a foreign convention,
4053 return the underlying type. */
4054 else if (TYPE_IS_PADDING_P (gnu_return_type)
4055 && (!default_pass_by_ref
4056 (TREE_TYPE (TYPE_FIELDS (gnu_return_type)))
4057 || Has_Foreign_Convention (gnat_entity)))
4058 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4060 /* If the return type is unconstrained, that means it must have a
4061 maximum size. Use the padded type as the effective return type.
4062 And ensure the function uses the target's by-invisible-reference
4063 mechanism to avoid copying too much data when it returns. */
4064 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4066 gnu_return_type
4067 = maybe_pad_type (gnu_return_type,
4068 max_size (TYPE_SIZE (gnu_return_type),
4069 true),
4070 0, gnat_entity, false, false, false, true);
4071 return_by_invisi_ref_p = true;
4074 /* If the return type has a size that overflows, we cannot have
4075 a function that returns that type. This usage doesn't make
4076 sense anyway, so give an error here. */
4077 if (TYPE_SIZE_UNIT (gnu_return_type)
4078 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type))
4079 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
4081 post_error ("cannot return type whose size overflows",
4082 gnat_entity);
4083 gnu_return_type = copy_node (gnu_return_type);
4084 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4085 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4086 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4087 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4091 /* Loop over the parameters and get their associated GCC tree. While
4092 doing this, build a copy-in copy-out structure if we need one. */
4093 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4094 Present (gnat_param);
4095 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4097 tree gnu_param_name = get_entity_name (gnat_param);
4098 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
4099 tree gnu_param, gnu_field;
4100 bool copy_in_copy_out = false;
4101 Mechanism_Type mech = Mechanism (gnat_param);
4103 /* Builtins are expanded inline and there is no real call sequence
4104 involved. So the type expected by the underlying expander is
4105 always the type of each argument "as is". */
4106 if (gnu_builtin_decl)
4107 mech = By_Copy;
4108 /* Handle the first parameter of a valued procedure specially. */
4109 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4110 mech = By_Copy_Return;
4111 /* Otherwise, see if a Mechanism was supplied that forced this
4112 parameter to be passed one way or another. */
4113 else if (mech == Default
4114 || mech == By_Copy || mech == By_Reference)
4116 else if (By_Descriptor_Last <= mech && mech <= By_Descriptor)
4117 mech = By_Descriptor;
4119 else if (By_Short_Descriptor_Last <= mech &&
4120 mech <= By_Short_Descriptor)
4121 mech = By_Short_Descriptor;
4123 else if (mech > 0)
4125 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4126 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4127 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4128 mech))
4129 mech = By_Reference;
4130 else
4131 mech = By_Copy;
4133 else
4135 post_error ("unsupported mechanism for&", gnat_param);
4136 mech = Default;
4139 gnu_param
4140 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4141 Has_Foreign_Convention (gnat_entity),
4142 &copy_in_copy_out);
4144 /* We are returned either a PARM_DECL or a type if no parameter
4145 needs to be passed; in either case, adjust the type. */
4146 if (DECL_P (gnu_param))
4147 gnu_param_type = TREE_TYPE (gnu_param);
4148 else
4150 gnu_param_type = gnu_param;
4151 gnu_param = NULL_TREE;
4154 /* The failure of this assertion will very likely come from an
4155 order of elaboration issue for the type of the parameter. */
4156 gcc_assert (kind == E_Subprogram_Type
4157 || !TYPE_IS_DUMMY_P (gnu_param_type));
4159 if (gnu_param)
4161 /* If it's an exported subprogram, we build a parameter list
4162 in parallel, in case we need to emit a stub for it. */
4163 if (Is_Exported (gnat_entity))
4165 gnu_stub_param_list
4166 = chainon (gnu_param, gnu_stub_param_list);
4167 /* Change By_Descriptor parameter to By_Reference for
4168 the internal version of an exported subprogram. */
4169 if (mech == By_Descriptor || mech == By_Short_Descriptor)
4171 gnu_param
4172 = gnat_to_gnu_param (gnat_param, By_Reference,
4173 gnat_entity, false,
4174 &copy_in_copy_out);
4175 has_stub = true;
4177 else
4178 gnu_param = copy_node (gnu_param);
4181 gnu_param_list = chainon (gnu_param, gnu_param_list);
4182 Sloc_to_locus (Sloc (gnat_param),
4183 &DECL_SOURCE_LOCATION (gnu_param));
4184 save_gnu_tree (gnat_param, gnu_param, false);
4186 /* If a parameter is a pointer, this function may modify
4187 memory through it and thus shouldn't be considered
4188 a const function. Also, the memory may be modified
4189 between two calls, so they can't be CSE'ed. The latter
4190 case also handles by-ref parameters. */
4191 if (POINTER_TYPE_P (gnu_param_type)
4192 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4193 const_flag = false;
4196 if (copy_in_copy_out)
4198 if (!gnu_cico_list)
4200 tree gnu_new_ret_type = make_node (RECORD_TYPE);
4202 /* If this is a function, we also need a field for the
4203 return value to be placed. */
4204 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
4206 gnu_field
4207 = create_field_decl (get_identifier ("RETVAL"),
4208 gnu_return_type,
4209 gnu_new_ret_type, NULL_TREE,
4210 NULL_TREE, 0, 0);
4211 Sloc_to_locus (Sloc (gnat_entity),
4212 &DECL_SOURCE_LOCATION (gnu_field));
4213 gnu_field_list = gnu_field;
4214 gnu_cico_list
4215 = tree_cons (gnu_field, void_type_node, NULL_TREE);
4218 gnu_return_type = gnu_new_ret_type;
4219 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4220 /* Set a default alignment to speed up accesses. */
4221 TYPE_ALIGN (gnu_return_type)
4222 = get_mode_alignment (ptr_mode);
4225 gnu_field
4226 = create_field_decl (gnu_param_name, gnu_param_type,
4227 gnu_return_type, NULL_TREE, NULL_TREE,
4228 0, 0);
4229 Sloc_to_locus (Sloc (gnat_param),
4230 &DECL_SOURCE_LOCATION (gnu_field));
4231 DECL_CHAIN (gnu_field) = gnu_field_list;
4232 gnu_field_list = gnu_field;
4233 gnu_cico_list
4234 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4238 /* Do not compute record for out parameters if subprogram is
4239 stubbed since structures are incomplete for the back-end. */
4240 if (gnu_field_list && Convention (gnat_entity) != Convention_Stubbed)
4241 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4242 0, debug_info_p);
4244 /* If we have a CICO list but it has only one entry, we convert
4245 this function into a function that simply returns that one
4246 object. */
4247 if (list_length (gnu_cico_list) == 1)
4248 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4250 if (Has_Stdcall_Convention (gnat_entity))
4251 prepend_one_attribute_to
4252 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4253 get_identifier ("stdcall"), NULL_TREE,
4254 gnat_entity);
4256 /* If we should request stack realignment for a foreign convention
4257 subprogram, do so. Note that this applies to task entry points in
4258 particular. */
4259 if (FOREIGN_FORCE_REALIGN_STACK
4260 && Has_Foreign_Convention (gnat_entity))
4261 prepend_one_attribute_to
4262 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4263 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4264 gnat_entity);
4266 /* The lists have been built in reverse. */
4267 gnu_param_list = nreverse (gnu_param_list);
4268 if (has_stub)
4269 gnu_stub_param_list = nreverse (gnu_stub_param_list);
4270 gnu_cico_list = nreverse (gnu_cico_list);
4272 if (kind == E_Function)
4273 Set_Mechanism (gnat_entity, return_unconstrained_p
4274 || return_by_direct_ref_p
4275 || return_by_invisi_ref_p
4276 ? By_Reference : By_Copy);
4277 gnu_type
4278 = create_subprog_type (gnu_return_type, gnu_param_list,
4279 gnu_cico_list, return_unconstrained_p,
4280 return_by_direct_ref_p,
4281 return_by_invisi_ref_p);
4283 if (has_stub)
4284 gnu_stub_type
4285 = create_subprog_type (gnu_return_type, gnu_stub_param_list,
4286 gnu_cico_list, return_unconstrained_p,
4287 return_by_direct_ref_p,
4288 return_by_invisi_ref_p);
4290 /* A subprogram (something that doesn't return anything) shouldn't
4291 be considered const since there would be no reason for such a
4292 subprogram. Note that procedures with Out (or In Out) parameters
4293 have already been converted into a function with a return type. */
4294 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4295 const_flag = false;
4297 gnu_type
4298 = build_qualified_type (gnu_type,
4299 TYPE_QUALS (gnu_type)
4300 | (TYPE_QUAL_CONST * const_flag)
4301 | (TYPE_QUAL_VOLATILE * volatile_flag));
4303 if (has_stub)
4304 gnu_stub_type
4305 = build_qualified_type (gnu_stub_type,
4306 TYPE_QUALS (gnu_stub_type)
4307 | (TYPE_QUAL_CONST * const_flag)
4308 | (TYPE_QUAL_VOLATILE * volatile_flag));
4310 /* If we have a builtin decl for that function, use it. Check if the
4311 profiles are compatible and warn if they are not. The checker is
4312 expected to post extra diagnostics in this case. */
4313 if (gnu_builtin_decl)
4315 intrin_binding_t inb;
4317 inb.gnat_entity = gnat_entity;
4318 inb.ada_fntype = gnu_type;
4319 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4321 if (!intrin_profiles_compatible_p (&inb))
4322 post_error
4323 ("?profile of& doesn''t match the builtin it binds!",
4324 gnat_entity);
4326 gnu_decl = gnu_builtin_decl;
4327 gnu_type = TREE_TYPE (gnu_builtin_decl);
4328 break;
4331 /* If there was no specified Interface_Name and the external and
4332 internal names of the subprogram are the same, only use the
4333 internal name to allow disambiguation of nested subprograms. */
4334 if (No (Interface_Name (gnat_entity))
4335 && gnu_ext_name == gnu_entity_name)
4336 gnu_ext_name = NULL_TREE;
4338 /* If we are defining the subprogram and it has an Address clause
4339 we must get the address expression from the saved GCC tree for the
4340 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4341 the address expression here since the front-end has guaranteed
4342 in that case that the elaboration has no effects. If there is
4343 an Address clause and we are not defining the object, just
4344 make it a constant. */
4345 if (Present (Address_Clause (gnat_entity)))
4347 tree gnu_address = NULL_TREE;
4349 if (definition)
4350 gnu_address
4351 = (present_gnu_tree (gnat_entity)
4352 ? get_gnu_tree (gnat_entity)
4353 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4355 save_gnu_tree (gnat_entity, NULL_TREE, false);
4357 /* Convert the type of the object to a reference type that can
4358 alias everything as per 13.3(19). */
4359 gnu_type
4360 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4361 if (gnu_address)
4362 gnu_address = convert (gnu_type, gnu_address);
4364 gnu_decl
4365 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4366 gnu_address, false, Is_Public (gnat_entity),
4367 extern_flag, false, NULL, gnat_entity);
4368 DECL_BY_REF_P (gnu_decl) = 1;
4371 else if (kind == E_Subprogram_Type)
4372 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
4373 !Comes_From_Source (gnat_entity),
4374 debug_info_p, gnat_entity);
4375 else
4377 if (has_stub)
4379 gnu_stub_name = gnu_ext_name;
4380 gnu_ext_name = create_concat_name (gnat_entity, "internal");
4381 public_flag = false;
4384 gnu_decl = create_subprog_decl (gnu_entity_name, gnu_ext_name,
4385 gnu_type, gnu_param_list,
4386 inline_flag, public_flag,
4387 extern_flag, attr_list,
4388 gnat_entity);
4389 if (has_stub)
4391 tree gnu_stub_decl
4392 = create_subprog_decl (gnu_entity_name, gnu_stub_name,
4393 gnu_stub_type, gnu_stub_param_list,
4394 inline_flag, true,
4395 extern_flag, attr_list,
4396 gnat_entity);
4397 SET_DECL_FUNCTION_STUB (gnu_decl, gnu_stub_decl);
4400 /* This is unrelated to the stub built right above. */
4401 DECL_STUBBED_P (gnu_decl)
4402 = Convention (gnat_entity) == Convention_Stubbed;
4405 break;
4407 case E_Incomplete_Type:
4408 case E_Incomplete_Subtype:
4409 case E_Private_Type:
4410 case E_Private_Subtype:
4411 case E_Limited_Private_Type:
4412 case E_Limited_Private_Subtype:
4413 case E_Record_Type_With_Private:
4414 case E_Record_Subtype_With_Private:
4416 /* Get the "full view" of this entity. If this is an incomplete
4417 entity from a limited with, treat its non-limited view as the
4418 full view. Otherwise, use either the full view or the underlying
4419 full view, whichever is present. This is used in all the tests
4420 below. */
4421 Entity_Id full_view
4422 = (IN (kind, Incomplete_Kind) && From_With_Type (gnat_entity))
4423 ? Non_Limited_View (gnat_entity)
4424 : Present (Full_View (gnat_entity))
4425 ? Full_View (gnat_entity)
4426 : Underlying_Full_View (gnat_entity);
4428 /* If this is an incomplete type with no full view, it must be a Taft
4429 Amendment type, in which case we return a dummy type. Otherwise,
4430 just get the type from its Etype. */
4431 if (No (full_view))
4433 if (kind == E_Incomplete_Type)
4435 gnu_type = make_dummy_type (gnat_entity);
4436 gnu_decl = TYPE_STUB_DECL (gnu_type);
4438 else
4440 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4441 NULL_TREE, 0);
4442 maybe_present = true;
4444 break;
4447 /* If we already made a type for the full view, reuse it. */
4448 else if (present_gnu_tree (full_view))
4450 gnu_decl = get_gnu_tree (full_view);
4451 break;
4454 /* Otherwise, if we are not defining the type now, get the type
4455 from the full view. But always get the type from the full view
4456 for define on use types, since otherwise we won't see them! */
4457 else if (!definition
4458 || (Is_Itype (full_view)
4459 && No (Freeze_Node (gnat_entity)))
4460 || (Is_Itype (gnat_entity)
4461 && No (Freeze_Node (full_view))))
4463 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4464 maybe_present = true;
4465 break;
4468 /* For incomplete types, make a dummy type entry which will be
4469 replaced later. Save it as the full declaration's type so
4470 we can do any needed updates when we see it. */
4471 gnu_type = make_dummy_type (gnat_entity);
4472 gnu_decl = TYPE_STUB_DECL (gnu_type);
4473 if (Has_Completion_In_Body (gnat_entity))
4474 DECL_TAFT_TYPE_P (gnu_decl) = 1;
4475 save_gnu_tree (full_view, gnu_decl, 0);
4476 break;
4479 case E_Class_Wide_Type:
4480 /* Class-wide types are always transformed into their root type. */
4481 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4482 maybe_present = true;
4483 break;
4485 case E_Task_Type:
4486 case E_Task_Subtype:
4487 case E_Protected_Type:
4488 case E_Protected_Subtype:
4489 /* Concurrent types are always transformed into their record type. */
4490 if (type_annotate_only && No (gnat_equiv_type))
4491 gnu_type = void_type_node;
4492 else
4493 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4494 maybe_present = true;
4495 break;
4497 case E_Label:
4498 gnu_decl = create_label_decl (gnu_entity_name);
4499 break;
4501 case E_Block:
4502 case E_Loop:
4503 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4504 we've already saved it, so we don't try to. */
4505 gnu_decl = error_mark_node;
4506 saved = true;
4507 break;
4509 default:
4510 gcc_unreachable ();
4513 /* If we had a case where we evaluated another type and it might have
4514 defined this one, handle it here. */
4515 if (maybe_present && present_gnu_tree (gnat_entity))
4517 gnu_decl = get_gnu_tree (gnat_entity);
4518 saved = true;
4521 /* If we are processing a type and there is either no decl for it or
4522 we just made one, do some common processing for the type, such as
4523 handling alignment and possible padding. */
4524 if (is_type && (!gnu_decl || this_made_decl))
4526 /* Tell the middle-end that objects of tagged types are guaranteed to
4527 be properly aligned. This is necessary because conversions to the
4528 class-wide type are translated into conversions to the root type,
4529 which can be less aligned than some of its derived types. */
4530 if (Is_Tagged_Type (gnat_entity)
4531 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4532 TYPE_ALIGN_OK (gnu_type) = 1;
4534 /* If the type is passed by reference, objects of this type must be
4535 fully addressable and cannot be copied. */
4536 if (Is_By_Reference_Type (gnat_entity))
4537 TREE_ADDRESSABLE (gnu_type) = 1;
4539 /* ??? Don't set the size for a String_Literal since it is either
4540 confirming or we don't handle it properly (if the low bound is
4541 non-constant). */
4542 if (!gnu_size && kind != E_String_Literal_Subtype)
4543 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
4544 TYPE_DECL, false,
4545 Has_Size_Clause (gnat_entity));
4547 /* If a size was specified, see if we can make a new type of that size
4548 by rearranging the type, for example from a fat to a thin pointer. */
4549 if (gnu_size)
4551 gnu_type
4552 = make_type_from_size (gnu_type, gnu_size,
4553 Has_Biased_Representation (gnat_entity));
4555 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4556 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4557 gnu_size = 0;
4560 /* If the alignment hasn't already been processed and this is
4561 not an unconstrained array, see if an alignment is specified.
4562 If not, we pick a default alignment for atomic objects. */
4563 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4565 else if (Known_Alignment (gnat_entity))
4567 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4568 TYPE_ALIGN (gnu_type));
4570 /* Warn on suspiciously large alignments. This should catch
4571 errors about the (alignment,byte)/(size,bit) discrepancy. */
4572 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4574 tree size;
4576 /* If a size was specified, take it into account. Otherwise
4577 use the RM size for records as the type size has already
4578 been adjusted to the alignment. */
4579 if (gnu_size)
4580 size = gnu_size;
4581 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
4582 || TREE_CODE (gnu_type) == UNION_TYPE
4583 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
4584 && !TYPE_FAT_POINTER_P (gnu_type))
4585 size = rm_size (gnu_type);
4586 else
4587 size = TYPE_SIZE (gnu_type);
4589 /* Consider an alignment as suspicious if the alignment/size
4590 ratio is greater or equal to the byte/bit ratio. */
4591 if (host_integerp (size, 1)
4592 && align >= TREE_INT_CST_LOW (size) * BITS_PER_UNIT)
4593 post_error_ne ("?suspiciously large alignment specified for&",
4594 Expression (Alignment_Clause (gnat_entity)),
4595 gnat_entity);
4598 else if (Is_Atomic (gnat_entity) && !gnu_size
4599 && host_integerp (TYPE_SIZE (gnu_type), 1)
4600 && integer_pow2p (TYPE_SIZE (gnu_type)))
4601 align = MIN (BIGGEST_ALIGNMENT,
4602 tree_low_cst (TYPE_SIZE (gnu_type), 1));
4603 else if (Is_Atomic (gnat_entity) && gnu_size
4604 && host_integerp (gnu_size, 1)
4605 && integer_pow2p (gnu_size))
4606 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
4608 /* See if we need to pad the type. If we did, and made a record,
4609 the name of the new type may be changed. So get it back for
4610 us when we make the new TYPE_DECL below. */
4611 if (gnu_size || align > 0)
4612 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4613 false, !gnu_decl, definition, false);
4615 if (TYPE_IS_PADDING_P (gnu_type))
4617 gnu_entity_name = TYPE_NAME (gnu_type);
4618 if (TREE_CODE (gnu_entity_name) == TYPE_DECL)
4619 gnu_entity_name = DECL_NAME (gnu_entity_name);
4622 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4624 /* If we are at global level, GCC will have applied variable_size to
4625 the type, but that won't have done anything. So, if it's not
4626 a constant or self-referential, call elaborate_expression_1 to
4627 make a variable for the size rather than calculating it each time.
4628 Handle both the RM size and the actual size. */
4629 if (global_bindings_p ()
4630 && TYPE_SIZE (gnu_type)
4631 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4632 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4634 tree size = TYPE_SIZE (gnu_type);
4636 TYPE_SIZE (gnu_type)
4637 = elaborate_expression_1 (size, gnat_entity,
4638 get_identifier ("SIZE"),
4639 definition, false);
4641 /* ??? For now, store the size as a multiple of the alignment in
4642 bytes so that we can see the alignment from the tree. */
4643 TYPE_SIZE_UNIT (gnu_type)
4644 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4645 get_identifier ("SIZE_A_UNIT"),
4646 definition, false,
4647 TYPE_ALIGN (gnu_type));
4649 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4650 may not be marked by the call to create_type_decl below. */
4651 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4653 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4655 tree variant_part = get_variant_part (gnu_type);
4656 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4658 if (variant_part)
4660 tree union_type = TREE_TYPE (variant_part);
4661 tree offset = DECL_FIELD_OFFSET (variant_part);
4663 /* If the position of the variant part is constant, subtract
4664 it from the size of the type of the parent to get the new
4665 size. This manual CSE reduces the data size. */
4666 if (TREE_CODE (offset) == INTEGER_CST)
4668 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4669 TYPE_SIZE (union_type)
4670 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4671 bit_from_pos (offset, bitpos));
4672 TYPE_SIZE_UNIT (union_type)
4673 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4674 byte_from_pos (offset, bitpos));
4676 else
4678 TYPE_SIZE (union_type)
4679 = elaborate_expression_1 (TYPE_SIZE (union_type),
4680 gnat_entity,
4681 get_identifier ("VSIZE"),
4682 definition, false);
4684 /* ??? For now, store the size as a multiple of the
4685 alignment in bytes so that we can see the alignment
4686 from the tree. */
4687 TYPE_SIZE_UNIT (union_type)
4688 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4689 gnat_entity,
4690 get_identifier
4691 ("VSIZE_A_UNIT"),
4692 definition, false,
4693 TYPE_ALIGN (union_type));
4695 /* ??? For now, store the offset as a multiple of the
4696 alignment in bytes so that we can see the alignment
4697 from the tree. */
4698 DECL_FIELD_OFFSET (variant_part)
4699 = elaborate_expression_2 (offset,
4700 gnat_entity,
4701 get_identifier ("VOFFSET"),
4702 definition, false,
4703 DECL_OFFSET_ALIGN
4704 (variant_part));
4707 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4708 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4711 if (operand_equal_p (ada_size, size, 0))
4712 ada_size = TYPE_SIZE (gnu_type);
4713 else
4714 ada_size
4715 = elaborate_expression_1 (ada_size, gnat_entity,
4716 get_identifier ("RM_SIZE"),
4717 definition, false);
4718 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4722 /* If this is a record type or subtype, call elaborate_expression_1 on
4723 any field position. Do this for both global and local types.
4724 Skip any fields that we haven't made trees for to avoid problems with
4725 class wide types. */
4726 if (IN (kind, Record_Kind))
4727 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4728 gnat_temp = Next_Entity (gnat_temp))
4729 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4731 tree gnu_field = get_gnu_tree (gnat_temp);
4733 /* ??? For now, store the offset as a multiple of the alignment
4734 in bytes so that we can see the alignment from the tree. */
4735 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4737 DECL_FIELD_OFFSET (gnu_field)
4738 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4739 gnat_temp,
4740 get_identifier ("OFFSET"),
4741 definition, false,
4742 DECL_OFFSET_ALIGN (gnu_field));
4744 /* ??? The context of gnu_field is not necessarily gnu_type
4745 so the MULT_EXPR node built above may not be marked by
4746 the call to create_type_decl below. */
4747 if (global_bindings_p ())
4748 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4752 if (Treat_As_Volatile (gnat_entity))
4753 gnu_type
4754 = build_qualified_type (gnu_type,
4755 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
4757 if (Is_Atomic (gnat_entity))
4758 check_ok_for_atomic (gnu_type, gnat_entity, false);
4760 if (Present (Alignment_Clause (gnat_entity)))
4761 TYPE_USER_ALIGN (gnu_type) = 1;
4763 if (Universal_Aliasing (gnat_entity))
4764 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
4766 if (!gnu_decl)
4767 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
4768 !Comes_From_Source (gnat_entity),
4769 debug_info_p, gnat_entity);
4770 else
4772 TREE_TYPE (gnu_decl) = gnu_type;
4773 TYPE_STUB_DECL (gnu_type) = gnu_decl;
4777 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4779 gnu_type = TREE_TYPE (gnu_decl);
4781 /* If this is a derived type, relate its alias set to that of its parent
4782 to avoid troubles when a call to an inherited primitive is inlined in
4783 a context where a derived object is accessed. The inlined code works
4784 on the parent view so the resulting code may access the same object
4785 using both the parent and the derived alias sets, which thus have to
4786 conflict. As the same issue arises with component references, the
4787 parent alias set also has to conflict with composite types enclosing
4788 derived components. For instance, if we have:
4790 type D is new T;
4791 type R is record
4792 Component : D;
4793 end record;
4795 we want T to conflict with both D and R, in addition to R being a
4796 superset of D by record/component construction.
4798 One way to achieve this is to perform an alias set copy from the
4799 parent to the derived type. This is not quite appropriate, though,
4800 as we don't want separate derived types to conflict with each other:
4802 type I1 is new Integer;
4803 type I2 is new Integer;
4805 We want I1 and I2 to both conflict with Integer but we do not want
4806 I1 to conflict with I2, and an alias set copy on derivation would
4807 have that effect.
4809 The option chosen is to make the alias set of the derived type a
4810 superset of that of its parent type. It trivially fulfills the
4811 simple requirement for the Integer derivation example above, and
4812 the component case as well by superset transitivity:
4814 superset superset
4815 R ----------> D ----------> T
4817 However, for composite types, conversions between derived types are
4818 translated into VIEW_CONVERT_EXPRs so a sequence like:
4820 type Comp1 is new Comp;
4821 type Comp2 is new Comp;
4822 procedure Proc (C : Comp1);
4824 C : Comp2;
4825 Proc (Comp1 (C));
4827 is translated into:
4829 C : Comp2;
4830 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
4832 and gimplified into:
4834 C : Comp2;
4835 Comp1 *C.0;
4836 C.0 = (Comp1 *) &C;
4837 Proc (C.0);
4839 i.e. generates code involving type punning. Therefore, Comp1 needs
4840 to conflict with Comp2 and an alias set copy is required.
4842 The language rules ensure the parent type is already frozen here. */
4843 if (Is_Derived_Type (gnat_entity))
4845 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_entity));
4846 relate_alias_sets (gnu_type, gnu_parent_type,
4847 Is_Composite_Type (gnat_entity)
4848 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
4851 /* Back-annotate the Alignment of the type if not already in the
4852 tree. Likewise for sizes. */
4853 if (Unknown_Alignment (gnat_entity))
4855 unsigned int double_align, align;
4856 bool is_capped_double, align_clause;
4858 /* If the default alignment of "double" or larger scalar types is
4859 specifically capped and this is not an array with an alignment
4860 clause on the component type, return the cap. */
4861 if ((double_align = double_float_alignment) > 0)
4862 is_capped_double
4863 = is_double_float_or_array (gnat_entity, &align_clause);
4864 else if ((double_align = double_scalar_alignment) > 0)
4865 is_capped_double
4866 = is_double_scalar_or_array (gnat_entity, &align_clause);
4867 else
4868 is_capped_double = align_clause = false;
4870 if (is_capped_double && !align_clause)
4871 align = double_align;
4872 else
4873 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
4875 Set_Alignment (gnat_entity, UI_From_Int (align));
4878 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4880 tree gnu_size = TYPE_SIZE (gnu_type);
4882 /* If the size is self-referential, annotate the maximum value. */
4883 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4884 gnu_size = max_size (gnu_size, true);
4886 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4888 /* In this mode, the tag and the parent components are not
4889 generated by the front-end so the sizes must be adjusted. */
4890 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
4891 Uint uint_size;
4893 if (Is_Derived_Type (gnat_entity))
4895 offset = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
4896 bitsizetype);
4897 Set_Alignment (gnat_entity,
4898 Alignment (Etype (Base_Type (gnat_entity))));
4900 else
4901 offset = pointer_size;
4903 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
4904 gnu_size = size_binop (MULT_EXPR, pointer_size,
4905 size_binop (CEIL_DIV_EXPR,
4906 gnu_size,
4907 pointer_size));
4908 uint_size = annotate_value (gnu_size);
4909 Set_Esize (gnat_entity, uint_size);
4910 Set_RM_Size (gnat_entity, uint_size);
4912 else
4913 Set_Esize (gnat_entity, annotate_value (gnu_size));
4916 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4917 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4920 /* If we really have a ..._DECL node, set a couple of flags on it. But we
4921 cannot do that if we are reusing the ..._DECL node made for a renamed
4922 object, since the predicates don't apply to it but to GNAT_ENTITY. */
4923 if (DECL_P (gnu_decl) && !(Present (Renamed_Object (gnat_entity)) && saved))
4925 if (!Comes_From_Source (gnat_entity))
4926 DECL_ARTIFICIAL (gnu_decl) = 1;
4928 if (!debug_info_p && TREE_CODE (gnu_decl) != FUNCTION_DECL)
4929 DECL_IGNORED_P (gnu_decl) = 1;
4932 /* If we haven't already, associate the ..._DECL node that we just made with
4933 the input GNAT entity node. */
4934 if (!saved)
4935 save_gnu_tree (gnat_entity, gnu_decl, false);
4937 /* If this is an enumeration or floating-point type, we were not able to set
4938 the bounds since they refer to the type. These are always static. */
4939 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4940 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4942 tree gnu_scalar_type = gnu_type;
4943 tree gnu_low_bound, gnu_high_bound;
4945 /* If this is a padded type, we need to use the underlying type. */
4946 if (TYPE_IS_PADDING_P (gnu_scalar_type))
4947 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4949 /* If this is a floating point type and we haven't set a floating
4950 point type yet, use this in the evaluation of the bounds. */
4951 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4952 longest_float_type_node = gnu_scalar_type;
4954 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4955 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
4957 if (kind == E_Enumeration_Type)
4959 /* Enumeration types have specific RM bounds. */
4960 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
4961 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
4963 /* Write full debugging information. Since this has both a
4964 typedef and a tag, avoid outputting the name twice. */
4965 DECL_ARTIFICIAL (gnu_decl) = 1;
4966 rest_of_type_decl_compilation (gnu_decl);
4969 else
4971 /* Floating-point types don't have specific RM bounds. */
4972 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
4973 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
4977 /* If we deferred processing of incomplete types, re-enable it. If there
4978 were no other disables and we have deferred types to process, do so. */
4979 if (this_deferred
4980 && --defer_incomplete_level == 0
4981 && defer_incomplete_list)
4983 struct incomplete *p, *next;
4985 /* We are back to level 0 for the deferring of incomplete types.
4986 But processing these incomplete types below may itself require
4987 deferring, so preserve what we have and restart from scratch. */
4988 p = defer_incomplete_list;
4989 defer_incomplete_list = NULL;
4991 /* For finalization, however, all types must be complete so we
4992 cannot do the same because deferred incomplete types may end up
4993 referencing each other. Process them all recursively first. */
4994 defer_finalize_level++;
4996 for (; p; p = next)
4998 next = p->next;
5000 if (p->old_type)
5001 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5002 gnat_to_gnu_type (p->full_type));
5003 free (p);
5006 defer_finalize_level--;
5009 /* If all the deferred incomplete types have been processed, we can proceed
5010 with the finalization of the deferred types. */
5011 if (defer_incomplete_level == 0
5012 && defer_finalize_level == 0
5013 && defer_finalize_list)
5015 unsigned int i;
5016 tree t;
5018 FOR_EACH_VEC_ELT (tree, defer_finalize_list, i, t)
5019 rest_of_type_decl_compilation_no_defer (t);
5021 VEC_free (tree, heap, defer_finalize_list);
5024 /* If we are not defining this type, see if it's on one of the lists of
5025 incomplete types. If so, handle the list entry now. */
5026 if (is_type && !definition)
5028 struct incomplete *p;
5030 for (p = defer_incomplete_list; p; p = p->next)
5031 if (p->old_type && p->full_type == gnat_entity)
5033 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5034 TREE_TYPE (gnu_decl));
5035 p->old_type = NULL_TREE;
5038 for (p = defer_limited_with; p; p = p->next)
5039 if (p->old_type && Non_Limited_View (p->full_type) == gnat_entity)
5041 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5042 TREE_TYPE (gnu_decl));
5043 p->old_type = NULL_TREE;
5047 if (this_global)
5048 force_global--;
5050 /* If this is a packed array type whose original array type is itself
5051 an Itype without freeze node, make sure the latter is processed. */
5052 if (Is_Packed_Array_Type (gnat_entity)
5053 && Is_Itype (Original_Array_Type (gnat_entity))
5054 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5055 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5056 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5058 return gnu_decl;
5061 /* Similar, but if the returned value is a COMPONENT_REF, return the
5062 FIELD_DECL. */
5064 tree
5065 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5067 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5069 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5070 gnu_field = TREE_OPERAND (gnu_field, 1);
5072 return gnu_field;
5075 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5076 the GCC type corresponding to that entity. */
5078 tree
5079 gnat_to_gnu_type (Entity_Id gnat_entity)
5081 tree gnu_decl;
5083 /* The back end never attempts to annotate generic types. */
5084 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5085 return void_type_node;
5087 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5088 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5090 return TREE_TYPE (gnu_decl);
5093 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5094 the unpadded version of the GCC type corresponding to that entity. */
5096 tree
5097 get_unpadded_type (Entity_Id gnat_entity)
5099 tree type = gnat_to_gnu_type (gnat_entity);
5101 if (TYPE_IS_PADDING_P (type))
5102 type = TREE_TYPE (TYPE_FIELDS (type));
5104 return type;
5107 /* Wrap up compilation of DECL, a TYPE_DECL, possibly deferring it.
5108 Every TYPE_DECL generated for a type definition must be passed
5109 to this function once everything else has been done for it. */
5111 void
5112 rest_of_type_decl_compilation (tree decl)
5114 /* We need to defer finalizing the type if incomplete types
5115 are being deferred or if they are being processed. */
5116 if (defer_incomplete_level != 0 || defer_finalize_level != 0)
5117 VEC_safe_push (tree, heap, defer_finalize_list, decl);
5118 else
5119 rest_of_type_decl_compilation_no_defer (decl);
5122 /* Same as above but without deferring the compilation. This
5123 function should not be invoked directly on a TYPE_DECL. */
5125 static void
5126 rest_of_type_decl_compilation_no_defer (tree decl)
5128 const int toplev = global_bindings_p ();
5129 tree t = TREE_TYPE (decl);
5131 rest_of_decl_compilation (decl, toplev, 0);
5133 /* Now process all the variants. This is needed for STABS. */
5134 for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
5136 if (t == TREE_TYPE (decl))
5137 continue;
5139 if (!TYPE_STUB_DECL (t))
5140 TYPE_STUB_DECL (t) = create_type_stub_decl (DECL_NAME (decl), t);
5142 rest_of_type_compilation (t, toplev);
5146 /* Finalize the processing of From_With_Type incomplete types. */
5148 void
5149 finalize_from_with_types (void)
5151 struct incomplete *p, *next;
5153 p = defer_limited_with;
5154 defer_limited_with = NULL;
5156 for (; p; p = next)
5158 next = p->next;
5160 if (p->old_type)
5161 update_pointer_to (TYPE_MAIN_VARIANT (p->old_type),
5162 gnat_to_gnu_type (p->full_type));
5163 free (p);
5167 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5168 kind of type (such E_Task_Type) that has a different type which Gigi
5169 uses for its representation. If the type does not have a special type
5170 for its representation, return GNAT_ENTITY. If a type is supposed to
5171 exist, but does not, abort unless annotating types, in which case
5172 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5174 Entity_Id
5175 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5177 Entity_Id gnat_equiv = gnat_entity;
5179 if (No (gnat_entity))
5180 return gnat_entity;
5182 switch (Ekind (gnat_entity))
5184 case E_Class_Wide_Subtype:
5185 if (Present (Equivalent_Type (gnat_entity)))
5186 gnat_equiv = Equivalent_Type (gnat_entity);
5187 break;
5189 case E_Access_Protected_Subprogram_Type:
5190 case E_Anonymous_Access_Protected_Subprogram_Type:
5191 gnat_equiv = Equivalent_Type (gnat_entity);
5192 break;
5194 case E_Class_Wide_Type:
5195 gnat_equiv = Root_Type (gnat_entity);
5196 break;
5198 case E_Task_Type:
5199 case E_Task_Subtype:
5200 case E_Protected_Type:
5201 case E_Protected_Subtype:
5202 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5203 break;
5205 default:
5206 break;
5209 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5210 return gnat_equiv;
5213 /* Return a GCC tree for a type corresponding to the component type of the
5214 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5215 is for an array being defined. DEBUG_INFO_P is true if we need to write
5216 debug information for other types that we may create in the process. */
5218 static tree
5219 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5220 bool debug_info_p)
5222 tree gnu_type = gnat_to_gnu_type (Component_Type (gnat_array));
5223 tree gnu_comp_size;
5225 /* Try to get a smaller form of the component if needed. */
5226 if ((Is_Packed (gnat_array)
5227 || Has_Component_Size_Clause (gnat_array))
5228 && !Is_Bit_Packed_Array (gnat_array)
5229 && !Has_Aliased_Components (gnat_array)
5230 && !Strict_Alignment (Component_Type (gnat_array))
5231 && TREE_CODE (gnu_type) == RECORD_TYPE
5232 && !TYPE_FAT_POINTER_P (gnu_type)
5233 && host_integerp (TYPE_SIZE (gnu_type), 1))
5234 gnu_type = make_packable_type (gnu_type, false);
5236 if (Has_Atomic_Components (gnat_array))
5237 check_ok_for_atomic (gnu_type, gnat_array, true);
5239 /* Get and validate any specified Component_Size. */
5240 gnu_comp_size
5241 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5242 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5243 true, Has_Component_Size_Clause (gnat_array));
5245 /* If the array has aliased components and the component size can be zero,
5246 force at least unit size to ensure that the components have distinct
5247 addresses. */
5248 if (!gnu_comp_size
5249 && Has_Aliased_Components (gnat_array)
5250 && (integer_zerop (TYPE_SIZE (gnu_type))
5251 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5252 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5253 gnu_comp_size
5254 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5256 /* If the component type is a RECORD_TYPE that has a self-referential size,
5257 then use the maximum size for the component size. */
5258 if (!gnu_comp_size
5259 && TREE_CODE (gnu_type) == RECORD_TYPE
5260 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5261 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5263 /* Honor the component size. This is not needed for bit-packed arrays. */
5264 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5266 tree orig_type = gnu_type;
5267 unsigned int max_align;
5269 /* If an alignment is specified, use it as a cap on the component type
5270 so that it can be honored for the whole type. But ignore it for the
5271 original type of packed array types. */
5272 if (No (Packed_Array_Type (gnat_array)) && Known_Alignment (gnat_array))
5273 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5274 else
5275 max_align = 0;
5277 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5278 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5279 gnu_type = orig_type;
5280 else
5281 orig_type = gnu_type;
5283 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5284 true, false, definition, true);
5286 /* If a padding record was made, declare it now since it will never be
5287 declared otherwise. This is necessary to ensure that its subtrees
5288 are properly marked. */
5289 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5290 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL, true,
5291 debug_info_p, gnat_array);
5294 if (Has_Volatile_Components (Base_Type (gnat_array)))
5295 gnu_type
5296 = build_qualified_type (gnu_type,
5297 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
5299 return gnu_type;
5302 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5303 using MECH as its passing mechanism, to be placed in the parameter
5304 list built for GNAT_SUBPROG. Assume a foreign convention for the
5305 latter if FOREIGN is true. Also set CICO to true if the parameter
5306 must use the copy-in copy-out implementation mechanism.
5308 The returned tree is a PARM_DECL, except for those cases where no
5309 parameter needs to be actually passed to the subprogram; the type
5310 of this "shadow" parameter is then returned instead. */
5312 static tree
5313 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5314 Entity_Id gnat_subprog, bool foreign, bool *cico)
5316 tree gnu_param_name = get_entity_name (gnat_param);
5317 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5318 tree gnu_param_type_alt = NULL_TREE;
5319 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5320 /* The parameter can be indirectly modified if its address is taken. */
5321 bool ro_param = in_param && !Address_Taken (gnat_param);
5322 bool by_return = false, by_component_ptr = false;
5323 bool by_ref = false, by_double_ref = false;
5324 tree gnu_param;
5326 /* Copy-return is used only for the first parameter of a valued procedure.
5327 It's a copy mechanism for which a parameter is never allocated. */
5328 if (mech == By_Copy_Return)
5330 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5331 mech = By_Copy;
5332 by_return = true;
5335 /* If this is either a foreign function or if the underlying type won't
5336 be passed by reference, strip off possible padding type. */
5337 if (TYPE_IS_PADDING_P (gnu_param_type))
5339 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5341 if (mech == By_Reference
5342 || foreign
5343 || (!must_pass_by_ref (unpadded_type)
5344 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
5345 gnu_param_type = unpadded_type;
5348 /* If this is a read-only parameter, make a variant of the type that is
5349 read-only. ??? However, if this is an unconstrained array, that type
5350 can be very complex, so skip it for now. Likewise for any other
5351 self-referential type. */
5352 if (ro_param
5353 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5354 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5355 gnu_param_type = build_qualified_type (gnu_param_type,
5356 (TYPE_QUALS (gnu_param_type)
5357 | TYPE_QUAL_CONST));
5359 /* For foreign conventions, pass arrays as pointers to the element type.
5360 First check for unconstrained array and get the underlying array. */
5361 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5362 gnu_param_type
5363 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5365 /* For GCC builtins, pass Address integer types as (void *) */
5366 if (Convention (gnat_subprog) == Convention_Intrinsic
5367 && Present (Interface_Name (gnat_subprog))
5368 && Is_Descendent_Of_Address (Etype (gnat_param)))
5369 gnu_param_type = ptr_void_type_node;
5371 /* VMS descriptors are themselves passed by reference. */
5372 if (mech == By_Short_Descriptor ||
5373 (mech == By_Descriptor && TARGET_ABI_OPEN_VMS && !TARGET_MALLOC64))
5374 gnu_param_type
5375 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5376 Mechanism (gnat_param),
5377 gnat_subprog));
5378 else if (mech == By_Descriptor)
5380 /* Build both a 32-bit and 64-bit descriptor, one of which will be
5381 chosen in fill_vms_descriptor. */
5382 gnu_param_type_alt
5383 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5384 Mechanism (gnat_param),
5385 gnat_subprog));
5386 gnu_param_type
5387 = build_pointer_type (build_vms_descriptor (gnu_param_type,
5388 Mechanism (gnat_param),
5389 gnat_subprog));
5392 /* Arrays are passed as pointers to element type for foreign conventions. */
5393 else if (foreign
5394 && mech != By_Copy
5395 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5397 /* Strip off any multi-dimensional entries, then strip
5398 off the last array to get the component type. */
5399 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5400 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5401 gnu_param_type = TREE_TYPE (gnu_param_type);
5403 by_component_ptr = true;
5404 gnu_param_type = TREE_TYPE (gnu_param_type);
5406 if (ro_param)
5407 gnu_param_type = build_qualified_type (gnu_param_type,
5408 (TYPE_QUALS (gnu_param_type)
5409 | TYPE_QUAL_CONST));
5411 gnu_param_type = build_pointer_type (gnu_param_type);
5414 /* Fat pointers are passed as thin pointers for foreign conventions. */
5415 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5416 gnu_param_type
5417 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5419 /* If we must pass or were requested to pass by reference, do so.
5420 If we were requested to pass by copy, do so.
5421 Otherwise, for foreign conventions, pass In Out or Out parameters
5422 or aggregates by reference. For COBOL and Fortran, pass all
5423 integer and FP types that way too. For Convention Ada, use
5424 the standard Ada default. */
5425 else if (must_pass_by_ref (gnu_param_type)
5426 || mech == By_Reference
5427 || (mech != By_Copy
5428 && ((foreign
5429 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5430 || (foreign
5431 && (Convention (gnat_subprog) == Convention_Fortran
5432 || Convention (gnat_subprog) == Convention_COBOL)
5433 && (INTEGRAL_TYPE_P (gnu_param_type)
5434 || FLOAT_TYPE_P (gnu_param_type)))
5435 || (!foreign
5436 && default_pass_by_ref (gnu_param_type)))))
5438 gnu_param_type = build_reference_type (gnu_param_type);
5439 by_ref = true;
5441 /* In some ABIs, e.g. SPARC 32-bit, fat pointer types are themselves
5442 passed by reference. Pass them by explicit reference, this will
5443 generate more debuggable code at -O0. */
5444 if (TYPE_IS_FAT_POINTER_P (gnu_param_type)
5445 && targetm.calls.pass_by_reference (NULL,
5446 TYPE_MODE (gnu_param_type),
5447 gnu_param_type,
5448 true))
5450 gnu_param_type = build_reference_type (gnu_param_type);
5451 by_double_ref = true;
5455 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5456 else if (!in_param)
5457 *cico = true;
5459 if (mech == By_Copy && (by_ref || by_component_ptr))
5460 post_error ("?cannot pass & by copy", gnat_param);
5462 /* If this is an Out parameter that isn't passed by reference and isn't
5463 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5464 it will be a VAR_DECL created when we process the procedure, so just
5465 return its type. For the special parameter of a valued procedure,
5466 never pass it in.
5468 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5469 Out parameters with discriminants or implicit initial values to be
5470 handled like In Out parameters. These type are normally built as
5471 aggregates, hence passed by reference, except for some packed arrays
5472 which end up encoded in special integer types.
5474 The exception we need to make is then for packed arrays of records
5475 with discriminants or implicit initial values. We have no light/easy
5476 way to check for the latter case, so we merely check for packed arrays
5477 of records. This may lead to useless copy-in operations, but in very
5478 rare cases only, as these would be exceptions in a set of already
5479 exceptional situations. */
5480 if (Ekind (gnat_param) == E_Out_Parameter
5481 && !by_ref
5482 && (by_return
5483 || (mech != By_Descriptor
5484 && mech != By_Short_Descriptor
5485 && !POINTER_TYPE_P (gnu_param_type)
5486 && !AGGREGATE_TYPE_P (gnu_param_type)))
5487 && !(Is_Array_Type (Etype (gnat_param))
5488 && Is_Packed (Etype (gnat_param))
5489 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5490 return gnu_param_type;
5492 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5493 ro_param || by_ref || by_component_ptr);
5494 DECL_BY_REF_P (gnu_param) = by_ref;
5495 DECL_BY_DOUBLE_REF_P (gnu_param) = by_double_ref;
5496 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5497 DECL_BY_DESCRIPTOR_P (gnu_param) = (mech == By_Descriptor ||
5498 mech == By_Short_Descriptor);
5499 DECL_POINTS_TO_READONLY_P (gnu_param)
5500 = (ro_param && (by_ref || by_component_ptr));
5502 /* Save the alternate descriptor type, if any. */
5503 if (gnu_param_type_alt)
5504 SET_DECL_PARM_ALT_TYPE (gnu_param, gnu_param_type_alt);
5506 /* If no Mechanism was specified, indicate what we're using, then
5507 back-annotate it. */
5508 if (mech == Default)
5509 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5511 Set_Mechanism (gnat_param, mech);
5512 return gnu_param;
5515 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5517 static bool
5518 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5520 while (Present (Corresponding_Discriminant (discr1)))
5521 discr1 = Corresponding_Discriminant (discr1);
5523 while (Present (Corresponding_Discriminant (discr2)))
5524 discr2 = Corresponding_Discriminant (discr2);
5526 return
5527 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5530 /* Return true if the array type GNU_TYPE, which represents a dimension of
5531 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5533 static bool
5534 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5536 /* If the array type is not the innermost dimension of the GNAT type,
5537 then it has a non-aliased component. */
5538 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5539 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5540 return true;
5542 /* If the array type has an aliased component in the front-end sense,
5543 then it also has an aliased component in the back-end sense. */
5544 if (Has_Aliased_Components (gnat_type))
5545 return false;
5547 /* If this is a derived type, then it has a non-aliased component if
5548 and only if its parent type also has one. */
5549 if (Is_Derived_Type (gnat_type))
5551 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5552 int index;
5553 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5554 gnu_parent_type
5555 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5556 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5557 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5558 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5561 /* Otherwise, rely exclusively on properties of the element type. */
5562 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5565 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5567 static bool
5568 compile_time_known_address_p (Node_Id gnat_address)
5570 /* Catch System'To_Address. */
5571 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5572 gnat_address = Expression (gnat_address);
5574 return Compile_Time_Known_Value (gnat_address);
5577 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5578 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5580 static bool
5581 cannot_be_superflat_p (Node_Id gnat_range)
5583 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5584 Node_Id scalar_range;
5585 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5587 /* If the low bound is not constant, try to find an upper bound. */
5588 while (Nkind (gnat_lb) != N_Integer_Literal
5589 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5590 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5591 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5592 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5593 || Nkind (scalar_range) == N_Range))
5594 gnat_lb = High_Bound (scalar_range);
5596 /* If the high bound is not constant, try to find a lower bound. */
5597 while (Nkind (gnat_hb) != N_Integer_Literal
5598 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5599 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5600 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5601 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5602 || Nkind (scalar_range) == N_Range))
5603 gnat_hb = Low_Bound (scalar_range);
5605 /* If we have failed to find constant bounds, punt. */
5606 if (Nkind (gnat_lb) != N_Integer_Literal
5607 || Nkind (gnat_hb) != N_Integer_Literal)
5608 return false;
5610 /* We need at least a signed 64-bit type to catch most cases. */
5611 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5612 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5613 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5614 return false;
5616 /* If the low bound is the smallest integer, nothing can be smaller. */
5617 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5618 if (TREE_OVERFLOW (gnu_lb_minus_one))
5619 return true;
5621 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5624 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5626 static bool
5627 constructor_address_p (tree gnu_expr)
5629 while (TREE_CODE (gnu_expr) == NOP_EXPR
5630 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5631 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5632 gnu_expr = TREE_OPERAND (gnu_expr, 0);
5634 return (TREE_CODE (gnu_expr) == ADDR_EXPR
5635 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5638 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5639 be elaborated at the point of its definition, but do nothing else. */
5641 void
5642 elaborate_entity (Entity_Id gnat_entity)
5644 switch (Ekind (gnat_entity))
5646 case E_Signed_Integer_Subtype:
5647 case E_Modular_Integer_Subtype:
5648 case E_Enumeration_Subtype:
5649 case E_Ordinary_Fixed_Point_Subtype:
5650 case E_Decimal_Fixed_Point_Subtype:
5651 case E_Floating_Point_Subtype:
5653 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5654 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5656 /* ??? Tests to avoid Constraint_Error in static expressions
5657 are needed until after the front stops generating bogus
5658 conversions on bounds of real types. */
5659 if (!Raises_Constraint_Error (gnat_lb))
5660 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5661 true, false, Needs_Debug_Info (gnat_entity));
5662 if (!Raises_Constraint_Error (gnat_hb))
5663 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5664 true, false, Needs_Debug_Info (gnat_entity));
5665 break;
5668 case E_Record_Type:
5670 Node_Id full_definition = Declaration_Node (gnat_entity);
5671 Node_Id record_definition = Type_Definition (full_definition);
5673 /* If this is a record extension, go a level further to find the
5674 record definition. */
5675 if (Nkind (record_definition) == N_Derived_Type_Definition)
5676 record_definition = Record_Extension_Part (record_definition);
5678 break;
5680 case E_Record_Subtype:
5681 case E_Private_Subtype:
5682 case E_Limited_Private_Subtype:
5683 case E_Record_Subtype_With_Private:
5684 if (Is_Constrained (gnat_entity)
5685 && Has_Discriminants (gnat_entity)
5686 && Present (Discriminant_Constraint (gnat_entity)))
5688 Node_Id gnat_discriminant_expr;
5689 Entity_Id gnat_field;
5691 for (gnat_field
5692 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
5693 gnat_discriminant_expr
5694 = First_Elmt (Discriminant_Constraint (gnat_entity));
5695 Present (gnat_field);
5696 gnat_field = Next_Discriminant (gnat_field),
5697 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
5698 /* ??? For now, ignore access discriminants. */
5699 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
5700 elaborate_expression (Node (gnat_discriminant_expr),
5701 gnat_entity, get_entity_name (gnat_field),
5702 true, false, false);
5704 break;
5709 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
5710 any entities on its entity chain similarly. */
5712 void
5713 mark_out_of_scope (Entity_Id gnat_entity)
5715 Entity_Id gnat_sub_entity;
5716 unsigned int kind = Ekind (gnat_entity);
5718 /* If this has an entity list, process all in the list. */
5719 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
5720 || IN (kind, Private_Kind)
5721 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
5722 || kind == E_Function || kind == E_Generic_Function
5723 || kind == E_Generic_Package || kind == E_Generic_Procedure
5724 || kind == E_Loop || kind == E_Operator || kind == E_Package
5725 || kind == E_Package_Body || kind == E_Procedure
5726 || kind == E_Record_Type || kind == E_Record_Subtype
5727 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
5728 for (gnat_sub_entity = First_Entity (gnat_entity);
5729 Present (gnat_sub_entity);
5730 gnat_sub_entity = Next_Entity (gnat_sub_entity))
5731 if (Scope (gnat_sub_entity) == gnat_entity
5732 && gnat_sub_entity != gnat_entity)
5733 mark_out_of_scope (gnat_sub_entity);
5735 /* Now clear this if it has been defined, but only do so if it isn't
5736 a subprogram or parameter. We could refine this, but it isn't
5737 worth it. If this is statically allocated, it is supposed to
5738 hang around out of cope. */
5739 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
5740 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
5742 save_gnu_tree (gnat_entity, NULL_TREE, true);
5743 save_gnu_tree (gnat_entity, error_mark_node, true);
5747 /* Relate the alias sets of GNU_NEW_TYPE and GNU_OLD_TYPE according to OP.
5748 If this is a multi-dimensional array type, do this recursively.
5750 OP may be
5751 - ALIAS_SET_COPY: the new set is made a copy of the old one.
5752 - ALIAS_SET_SUPERSET: the new set is made a superset of the old one.
5753 - ALIAS_SET_SUBSET: the new set is made a subset of the old one. */
5755 static void
5756 relate_alias_sets (tree gnu_new_type, tree gnu_old_type, enum alias_set_op op)
5758 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
5759 of a one-dimensional array, since the padding has the same alias set
5760 as the field type, but if it's a multi-dimensional array, we need to
5761 see the inner types. */
5762 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
5763 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
5764 || TYPE_PADDING_P (gnu_old_type)))
5765 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
5767 /* Unconstrained array types are deemed incomplete and would thus be given
5768 alias set 0. Retrieve the underlying array type. */
5769 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
5770 gnu_old_type
5771 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
5772 if (TREE_CODE (gnu_new_type) == UNCONSTRAINED_ARRAY_TYPE)
5773 gnu_new_type
5774 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_new_type))));
5776 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
5777 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
5778 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
5779 relate_alias_sets (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type), op);
5781 switch (op)
5783 case ALIAS_SET_COPY:
5784 /* The alias set shouldn't be copied between array types with different
5785 aliasing settings because this can break the aliasing relationship
5786 between the array type and its element type. */
5787 #ifndef ENABLE_CHECKING
5788 if (flag_strict_aliasing)
5789 #endif
5790 gcc_assert (!(TREE_CODE (gnu_new_type) == ARRAY_TYPE
5791 && TREE_CODE (gnu_old_type) == ARRAY_TYPE
5792 && TYPE_NONALIASED_COMPONENT (gnu_new_type)
5793 != TYPE_NONALIASED_COMPONENT (gnu_old_type)));
5795 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
5796 break;
5798 case ALIAS_SET_SUBSET:
5799 case ALIAS_SET_SUPERSET:
5801 alias_set_type old_set = get_alias_set (gnu_old_type);
5802 alias_set_type new_set = get_alias_set (gnu_new_type);
5804 /* Do nothing if the alias sets conflict. This ensures that we
5805 never call record_alias_subset several times for the same pair
5806 or at all for alias set 0. */
5807 if (!alias_sets_conflict_p (old_set, new_set))
5809 if (op == ALIAS_SET_SUBSET)
5810 record_alias_subset (old_set, new_set);
5811 else
5812 record_alias_subset (new_set, old_set);
5815 break;
5817 default:
5818 gcc_unreachable ();
5821 record_component_aliases (gnu_new_type);
5824 /* Return true if the size represented by GNU_SIZE can be handled by an
5825 allocation. If STATIC_P is true, consider only what can be done with a
5826 static allocation. */
5828 static bool
5829 allocatable_size_p (tree gnu_size, bool static_p)
5831 HOST_WIDE_INT our_size;
5833 /* If this is not a static allocation, the only case we want to forbid
5834 is an overflowing size. That will be converted into a raise a
5835 Storage_Error. */
5836 if (!static_p)
5837 return !(TREE_CODE (gnu_size) == INTEGER_CST
5838 && TREE_OVERFLOW (gnu_size));
5840 /* Otherwise, we need to deal with both variable sizes and constant
5841 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
5842 since assemblers may not like very large sizes. */
5843 if (!host_integerp (gnu_size, 1))
5844 return false;
5846 our_size = tree_low_cst (gnu_size, 1);
5847 return (int) our_size == our_size;
5850 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5851 NAME, ARGS and ERROR_POINT. */
5853 static void
5854 prepend_one_attribute_to (struct attrib ** attr_list,
5855 enum attr_type attr_type,
5856 tree attr_name,
5857 tree attr_args,
5858 Node_Id attr_error_point)
5860 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
5862 attr->type = attr_type;
5863 attr->name = attr_name;
5864 attr->args = attr_args;
5865 attr->error_point = attr_error_point;
5867 attr->next = *attr_list;
5868 *attr_list = attr;
5871 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
5873 static void
5874 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
5876 Node_Id gnat_temp;
5878 /* Attributes are stored as Representation Item pragmas. */
5880 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
5881 gnat_temp = Next_Rep_Item (gnat_temp))
5882 if (Nkind (gnat_temp) == N_Pragma)
5884 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
5885 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
5886 enum attr_type etype;
5888 /* Map the kind of pragma at hand. Skip if this is not one
5889 we know how to handle. */
5891 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_temp))))
5893 case Pragma_Machine_Attribute:
5894 etype = ATTR_MACHINE_ATTRIBUTE;
5895 break;
5897 case Pragma_Linker_Alias:
5898 etype = ATTR_LINK_ALIAS;
5899 break;
5901 case Pragma_Linker_Section:
5902 etype = ATTR_LINK_SECTION;
5903 break;
5905 case Pragma_Linker_Constructor:
5906 etype = ATTR_LINK_CONSTRUCTOR;
5907 break;
5909 case Pragma_Linker_Destructor:
5910 etype = ATTR_LINK_DESTRUCTOR;
5911 break;
5913 case Pragma_Weak_External:
5914 etype = ATTR_WEAK_EXTERNAL;
5915 break;
5917 case Pragma_Thread_Local_Storage:
5918 etype = ATTR_THREAD_LOCAL_STORAGE;
5919 break;
5921 default:
5922 continue;
5925 /* See what arguments we have and turn them into GCC trees for
5926 attribute handlers. These expect identifier for strings. We
5927 handle at most two arguments, static expressions only. */
5929 if (Present (gnat_assoc) && Present (First (gnat_assoc)))
5931 Node_Id gnat_arg0 = Next (First (gnat_assoc));
5932 Node_Id gnat_arg1 = Empty;
5934 if (Present (gnat_arg0)
5935 && Is_Static_Expression (Expression (gnat_arg0)))
5937 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
5939 if (TREE_CODE (gnu_arg0) == STRING_CST)
5940 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
5942 gnat_arg1 = Next (gnat_arg0);
5945 if (Present (gnat_arg1)
5946 && Is_Static_Expression (Expression (gnat_arg1)))
5948 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
5950 if (TREE_CODE (gnu_arg1) == STRING_CST)
5951 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
5955 /* Prepend to the list now. Make a list of the argument we might
5956 have, as GCC expects it. */
5957 prepend_one_attribute_to
5958 (attr_list,
5959 etype, gnu_arg0,
5960 (gnu_arg1 != NULL_TREE)
5961 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
5962 Present (Next (First (gnat_assoc)))
5963 ? Expression (Next (First (gnat_assoc))) : gnat_temp);
5967 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
5968 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
5969 return the GCC tree to use for that expression. GNU_NAME is the suffix
5970 to use if a variable needs to be created and DEFINITION is true if this
5971 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
5972 otherwise, we are just elaborating the expression for side-effects. If
5973 NEED_DEBUG is true, we need a variable for debugging purposes even if it
5974 isn't needed for code generation. */
5976 static tree
5977 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
5978 bool definition, bool need_value, bool need_debug)
5980 tree gnu_expr;
5982 /* If we already elaborated this expression (e.g. it was involved
5983 in the definition of a private type), use the old value. */
5984 if (present_gnu_tree (gnat_expr))
5985 return get_gnu_tree (gnat_expr);
5987 /* If we don't need a value and this is static or a discriminant,
5988 we don't need to do anything. */
5989 if (!need_value
5990 && (Is_OK_Static_Expression (gnat_expr)
5991 || (Nkind (gnat_expr) == N_Identifier
5992 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
5993 return NULL_TREE;
5995 /* If it's a static expression, we don't need a variable for debugging. */
5996 if (need_debug && Is_OK_Static_Expression (gnat_expr))
5997 need_debug = false;
5999 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6000 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
6001 gnu_name, definition, need_debug);
6003 /* Save the expression in case we try to elaborate this entity again. Since
6004 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6005 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6006 save_gnu_tree (gnat_expr, gnu_expr, true);
6008 return need_value ? gnu_expr : error_mark_node;
6011 /* Similar, but take a GNU expression and always return a result. */
6013 static tree
6014 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6015 bool definition, bool need_debug)
6017 const bool expr_global_p = Is_Public (gnat_entity) || global_bindings_p ();
6018 bool expr_variable_p, use_variable;
6020 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6021 reference will have been replaced with a COMPONENT_REF when the type
6022 is being elaborated. However, there are some cases involving child
6023 types where we will. So convert it to a COMPONENT_REF. We hope it
6024 will be at the highest level of the expression in these cases. */
6025 if (TREE_CODE (gnu_expr) == FIELD_DECL)
6026 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6027 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6028 gnu_expr, NULL_TREE);
6030 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6031 that an expression cannot contain both a discriminant and a variable. */
6032 if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6033 return gnu_expr;
6035 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6036 a variable that is initialized to contain the expression when the package
6037 containing the definition is elaborated. If this entity is defined at top
6038 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6039 if this is necessary. */
6040 if (CONSTANT_CLASS_P (gnu_expr))
6041 expr_variable_p = false;
6042 else
6044 /* Skip any conversions and simple arithmetics to see if the expression
6045 is based on a read-only variable.
6046 ??? This really should remain read-only, but we have to think about
6047 the typing of the tree here. */
6048 tree inner
6049 = skip_simple_arithmetic (remove_conversions (gnu_expr, true));
6051 if (handled_component_p (inner))
6053 HOST_WIDE_INT bitsize, bitpos;
6054 tree offset;
6055 enum machine_mode mode;
6056 int unsignedp, volatilep;
6058 inner = get_inner_reference (inner, &bitsize, &bitpos, &offset,
6059 &mode, &unsignedp, &volatilep, false);
6060 /* If the offset is variable, err on the side of caution. */
6061 if (offset)
6062 inner = NULL_TREE;
6065 expr_variable_p
6066 = !(inner
6067 && TREE_CODE (inner) == VAR_DECL
6068 && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6071 /* We only need to use the variable if we are in a global context since GCC
6072 can do the right thing in the local case. However, when not optimizing,
6073 use it for bounds of loop iteration scheme to avoid code duplication. */
6074 use_variable = expr_variable_p
6075 && (expr_global_p
6076 || (!optimize
6077 && Is_Itype (gnat_entity)
6078 && Nkind (Associated_Node_For_Itype (gnat_entity))
6079 == N_Loop_Parameter_Specification));
6081 /* Now create it, possibly only for debugging purposes. */
6082 if (use_variable || need_debug)
6084 tree gnu_decl
6085 = create_var_decl (create_concat_name (gnat_entity,
6086 IDENTIFIER_POINTER (gnu_name)),
6087 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
6088 !need_debug, Is_Public (gnat_entity),
6089 !definition, expr_global_p, NULL, gnat_entity);
6091 if (use_variable)
6092 return gnu_decl;
6095 return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6098 /* Similar, but take an alignment factor and make it explicit in the tree. */
6100 static tree
6101 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6102 bool definition, bool need_debug, unsigned int align)
6104 tree unit_align = size_int (align / BITS_PER_UNIT);
6105 return
6106 size_binop (MULT_EXPR,
6107 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6108 gnu_expr,
6109 unit_align),
6110 gnat_entity, gnu_name, definition,
6111 need_debug),
6112 unit_align);
6115 /* Create a record type that contains a SIZE bytes long field of TYPE with a
6116 starting bit position so that it is aligned to ALIGN bits, and leaving at
6117 least ROOM bytes free before the field. BASE_ALIGN is the alignment the
6118 record is guaranteed to get. */
6120 tree
6121 make_aligning_type (tree type, unsigned int align, tree size,
6122 unsigned int base_align, int room)
6124 /* We will be crafting a record type with one field at a position set to be
6125 the next multiple of ALIGN past record'address + room bytes. We use a
6126 record placeholder to express record'address. */
6127 tree record_type = make_node (RECORD_TYPE);
6128 tree record = build0 (PLACEHOLDER_EXPR, record_type);
6130 tree record_addr_st
6131 = convert (sizetype, build_unary_op (ADDR_EXPR, NULL_TREE, record));
6133 /* The diagram below summarizes the shape of what we manipulate:
6135 <--------- pos ---------->
6136 { +------------+-------------+-----------------+
6137 record =>{ |############| ... | field (type) |
6138 { +------------+-------------+-----------------+
6139 |<-- room -->|<- voffset ->|<---- size ----->|
6142 record_addr vblock_addr
6144 Every length is in sizetype bytes there, except "pos" which has to be
6145 set as a bit position in the GCC tree for the record. */
6146 tree room_st = size_int (room);
6147 tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
6148 tree voffset_st, pos, field;
6150 tree name = TYPE_NAME (type);
6152 if (TREE_CODE (name) == TYPE_DECL)
6153 name = DECL_NAME (name);
6154 name = concat_name (name, "ALIGN");
6155 TYPE_NAME (record_type) = name;
6157 /* Compute VOFFSET and then POS. The next byte position multiple of some
6158 alignment after some address is obtained by "and"ing the alignment minus
6159 1 with the two's complement of the address. */
6160 voffset_st = size_binop (BIT_AND_EXPR,
6161 fold_build1 (NEGATE_EXPR, sizetype, vblock_addr_st),
6162 size_int ((align / BITS_PER_UNIT) - 1));
6164 /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype. */
6165 pos = size_binop (MULT_EXPR,
6166 convert (bitsizetype,
6167 size_binop (PLUS_EXPR, room_st, voffset_st)),
6168 bitsize_unit_node);
6170 /* Craft the GCC record representation. We exceptionally do everything
6171 manually here because 1) our generic circuitry is not quite ready to
6172 handle the complex position/size expressions we are setting up, 2) we
6173 have a strong simplifying factor at hand: we know the maximum possible
6174 value of voffset, and 3) we have to set/reset at least the sizes in
6175 accordance with this maximum value anyway, as we need them to convey
6176 what should be "alloc"ated for this type.
6178 Use -1 as the 'addressable' indication for the field to prevent the
6179 creation of a bitfield. We don't need one, it would have damaging
6180 consequences on the alignment computation, and create_field_decl would
6181 make one without this special argument, for instance because of the
6182 complex position expression. */
6183 field = create_field_decl (get_identifier ("F"), type, record_type, size,
6184 pos, 1, -1);
6185 TYPE_FIELDS (record_type) = field;
6187 TYPE_ALIGN (record_type) = base_align;
6188 TYPE_USER_ALIGN (record_type) = 1;
6190 TYPE_SIZE (record_type)
6191 = size_binop (PLUS_EXPR,
6192 size_binop (MULT_EXPR, convert (bitsizetype, size),
6193 bitsize_unit_node),
6194 bitsize_int (align + room * BITS_PER_UNIT));
6195 TYPE_SIZE_UNIT (record_type)
6196 = size_binop (PLUS_EXPR, size,
6197 size_int (room + align / BITS_PER_UNIT));
6199 SET_TYPE_MODE (record_type, BLKmode);
6200 relate_alias_sets (record_type, type, ALIAS_SET_COPY);
6202 /* Declare it now since it will never be declared otherwise. This is
6203 necessary to ensure that its subtrees are properly marked. */
6204 create_type_decl (name, record_type, NULL, true, false, Empty);
6206 return record_type;
6209 /* Return the result of rounding T up to ALIGN. */
6211 static inline unsigned HOST_WIDE_INT
6212 round_up_to_align (unsigned HOST_WIDE_INT t, unsigned int align)
6214 t += align - 1;
6215 t /= align;
6216 t *= align;
6217 return t;
6220 /* TYPE is a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE that is being used
6221 as the field type of a packed record if IN_RECORD is true, or as the
6222 component type of a packed array if IN_RECORD is false. See if we can
6223 rewrite it either as a type that has a non-BLKmode, which we can pack
6224 tighter in the packed record case, or as a smaller type. If so, return
6225 the new type. If not, return the original type. */
6227 static tree
6228 make_packable_type (tree type, bool in_record)
6230 unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
6231 unsigned HOST_WIDE_INT new_size;
6232 tree new_type, old_field, field_list = NULL_TREE;
6234 /* No point in doing anything if the size is zero. */
6235 if (size == 0)
6236 return type;
6238 new_type = make_node (TREE_CODE (type));
6240 /* Copy the name and flags from the old type to that of the new.
6241 Note that we rely on the pointer equality created here for
6242 TYPE_NAME to look through conversions in various places. */
6243 TYPE_NAME (new_type) = TYPE_NAME (type);
6244 TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
6245 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
6246 if (TREE_CODE (type) == RECORD_TYPE)
6247 TYPE_PADDING_P (new_type) = TYPE_PADDING_P (type);
6249 /* If we are in a record and have a small size, set the alignment to
6250 try for an integral mode. Otherwise set it to try for a smaller
6251 type with BLKmode. */
6252 if (in_record && size <= MAX_FIXED_MODE_SIZE)
6254 TYPE_ALIGN (new_type) = ceil_alignment (size);
6255 new_size = round_up_to_align (size, TYPE_ALIGN (new_type));
6257 else
6259 unsigned HOST_WIDE_INT align;
6261 /* Do not try to shrink the size if the RM size is not constant. */
6262 if (TYPE_CONTAINS_TEMPLATE_P (type)
6263 || !host_integerp (TYPE_ADA_SIZE (type), 1))
6264 return type;
6266 /* Round the RM size up to a unit boundary to get the minimal size
6267 for a BLKmode record. Give up if it's already the size. */
6268 new_size = TREE_INT_CST_LOW (TYPE_ADA_SIZE (type));
6269 new_size = round_up_to_align (new_size, BITS_PER_UNIT);
6270 if (new_size == size)
6271 return type;
6273 align = new_size & -new_size;
6274 TYPE_ALIGN (new_type) = MIN (TYPE_ALIGN (type), align);
6277 TYPE_USER_ALIGN (new_type) = 1;
6279 /* Now copy the fields, keeping the position and size as we don't want
6280 to change the layout by propagating the packedness downwards. */
6281 for (old_field = TYPE_FIELDS (type); old_field;
6282 old_field = DECL_CHAIN (old_field))
6284 tree new_field_type = TREE_TYPE (old_field);
6285 tree new_field, new_size;
6287 if ((TREE_CODE (new_field_type) == RECORD_TYPE
6288 || TREE_CODE (new_field_type) == UNION_TYPE
6289 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
6290 && !TYPE_FAT_POINTER_P (new_field_type)
6291 && host_integerp (TYPE_SIZE (new_field_type), 1))
6292 new_field_type = make_packable_type (new_field_type, true);
6294 /* However, for the last field in a not already packed record type
6295 that is of an aggregate type, we need to use the RM size in the
6296 packable version of the record type, see finish_record_type. */
6297 if (!DECL_CHAIN (old_field)
6298 && !TYPE_PACKED (type)
6299 && (TREE_CODE (new_field_type) == RECORD_TYPE
6300 || TREE_CODE (new_field_type) == UNION_TYPE
6301 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
6302 && !TYPE_FAT_POINTER_P (new_field_type)
6303 && !TYPE_CONTAINS_TEMPLATE_P (new_field_type)
6304 && TYPE_ADA_SIZE (new_field_type))
6305 new_size = TYPE_ADA_SIZE (new_field_type);
6306 else
6307 new_size = DECL_SIZE (old_field);
6309 new_field
6310 = create_field_decl (DECL_NAME (old_field), new_field_type, new_type,
6311 new_size, bit_position (old_field),
6312 TYPE_PACKED (type),
6313 !DECL_NONADDRESSABLE_P (old_field));
6315 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
6316 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
6317 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
6318 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
6320 DECL_CHAIN (new_field) = field_list;
6321 field_list = new_field;
6324 finish_record_type (new_type, nreverse (field_list), 2, false);
6325 relate_alias_sets (new_type, type, ALIAS_SET_COPY);
6327 /* If this is a padding record, we never want to make the size smaller
6328 than what was specified. For QUAL_UNION_TYPE, also copy the size. */
6329 if (TYPE_IS_PADDING_P (type) || TREE_CODE (type) == QUAL_UNION_TYPE)
6331 TYPE_SIZE (new_type) = TYPE_SIZE (type);
6332 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
6333 new_size = size;
6335 else
6337 TYPE_SIZE (new_type) = bitsize_int (new_size);
6338 TYPE_SIZE_UNIT (new_type)
6339 = size_int ((new_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
6342 if (!TYPE_CONTAINS_TEMPLATE_P (type))
6343 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (type));
6345 compute_record_mode (new_type);
6347 /* Try harder to get a packable type if necessary, for example
6348 in case the record itself contains a BLKmode field. */
6349 if (in_record && TYPE_MODE (new_type) == BLKmode)
6350 SET_TYPE_MODE (new_type,
6351 mode_for_size_tree (TYPE_SIZE (new_type), MODE_INT, 1));
6353 /* If neither the mode nor the size has shrunk, return the old type. */
6354 if (TYPE_MODE (new_type) == BLKmode && new_size >= size)
6355 return type;
6357 return new_type;
6360 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
6361 if needed. We have already verified that SIZE and TYPE are large enough.
6362 GNAT_ENTITY is used to name the resulting record and to issue a warning.
6363 IS_COMPONENT_TYPE is true if this is being done for the component type
6364 of an array. IS_USER_TYPE is true if we must complete the original type.
6365 DEFINITION is true if this type is being defined. SAME_RM_SIZE is true
6366 if the RM size of the resulting type is to be set to SIZE too; otherwise,
6367 it's set to the RM size of the original type. */
6369 tree
6370 maybe_pad_type (tree type, tree size, unsigned int align,
6371 Entity_Id gnat_entity, bool is_component_type,
6372 bool is_user_type, bool definition, bool same_rm_size)
6374 tree orig_rm_size = same_rm_size ? NULL_TREE : rm_size (type);
6375 tree orig_size = TYPE_SIZE (type);
6376 tree record, field;
6378 /* If TYPE is a padded type, see if it agrees with any size and alignment
6379 we were given. If so, return the original type. Otherwise, strip
6380 off the padding, since we will either be returning the inner type
6381 or repadding it. If no size or alignment is specified, use that of
6382 the original padded type. */
6383 if (TYPE_IS_PADDING_P (type))
6385 if ((!size
6386 || operand_equal_p (round_up (size,
6387 MAX (align, TYPE_ALIGN (type))),
6388 round_up (TYPE_SIZE (type),
6389 MAX (align, TYPE_ALIGN (type))),
6391 && (align == 0 || align == TYPE_ALIGN (type)))
6392 return type;
6394 if (!size)
6395 size = TYPE_SIZE (type);
6396 if (align == 0)
6397 align = TYPE_ALIGN (type);
6399 type = TREE_TYPE (TYPE_FIELDS (type));
6400 orig_size = TYPE_SIZE (type);
6403 /* If the size is either not being changed or is being made smaller (which
6404 is not done here and is only valid for bitfields anyway), show the size
6405 isn't changing. Likewise, clear the alignment if it isn't being
6406 changed. Then return if we aren't doing anything. */
6407 if (size
6408 && (operand_equal_p (size, orig_size, 0)
6409 || (TREE_CODE (orig_size) == INTEGER_CST
6410 && tree_int_cst_lt (size, orig_size))))
6411 size = NULL_TREE;
6413 if (align == TYPE_ALIGN (type))
6414 align = 0;
6416 if (align == 0 && !size)
6417 return type;
6419 /* If requested, complete the original type and give it a name. */
6420 if (is_user_type)
6421 create_type_decl (get_entity_name (gnat_entity), type,
6422 NULL, !Comes_From_Source (gnat_entity),
6423 !(TYPE_NAME (type)
6424 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6425 && DECL_IGNORED_P (TYPE_NAME (type))),
6426 gnat_entity);
6428 /* We used to modify the record in place in some cases, but that could
6429 generate incorrect debugging information. So make a new record
6430 type and name. */
6431 record = make_node (RECORD_TYPE);
6432 TYPE_PADDING_P (record) = 1;
6434 if (Present (gnat_entity))
6435 TYPE_NAME (record) = create_concat_name (gnat_entity, "PAD");
6437 TYPE_VOLATILE (record)
6438 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
6440 TYPE_ALIGN (record) = align;
6441 TYPE_SIZE (record) = size ? size : orig_size;
6442 TYPE_SIZE_UNIT (record)
6443 = convert (sizetype,
6444 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
6445 bitsize_unit_node));
6447 /* If we are changing the alignment and the input type is a record with
6448 BLKmode and a small constant size, try to make a form that has an
6449 integral mode. This might allow the padding record to also have an
6450 integral mode, which will be much more efficient. There is no point
6451 in doing so if a size is specified unless it is also a small constant
6452 size and it is incorrect to do so if we cannot guarantee that the mode
6453 will be naturally aligned since the field must always be addressable.
6455 ??? This might not always be a win when done for a stand-alone object:
6456 since the nominal and the effective type of the object will now have
6457 different modes, a VIEW_CONVERT_EXPR will be required for converting
6458 between them and it might be hard to overcome afterwards, including
6459 at the RTL level when the stand-alone object is accessed as a whole. */
6460 if (align != 0
6461 && TREE_CODE (type) == RECORD_TYPE
6462 && TYPE_MODE (type) == BLKmode
6463 && TREE_CODE (orig_size) == INTEGER_CST
6464 && !TREE_OVERFLOW (orig_size)
6465 && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
6466 && (!size
6467 || (TREE_CODE (size) == INTEGER_CST
6468 && compare_tree_int (size, MAX_FIXED_MODE_SIZE) <= 0)))
6470 tree packable_type = make_packable_type (type, true);
6471 if (TYPE_MODE (packable_type) != BLKmode
6472 && align >= TYPE_ALIGN (packable_type))
6473 type = packable_type;
6476 /* Now create the field with the original size. */
6477 field = create_field_decl (get_identifier ("F"), type, record, orig_size,
6478 bitsize_zero_node, 0, 1);
6479 DECL_INTERNAL_P (field) = 1;
6481 /* Do not emit debug info until after the auxiliary record is built. */
6482 finish_record_type (record, field, 1, false);
6484 /* Set the same size for its RM size if requested; otherwise reuse
6485 the RM size of the original type. */
6486 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : orig_rm_size);
6488 /* Unless debugging information isn't being written for the input type,
6489 write a record that shows what we are a subtype of and also make a
6490 variable that indicates our size, if still variable. */
6491 if (TREE_CODE (orig_size) != INTEGER_CST
6492 && TYPE_NAME (record)
6493 && TYPE_NAME (type)
6494 && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6495 && DECL_IGNORED_P (TYPE_NAME (type))))
6497 tree marker = make_node (RECORD_TYPE);
6498 tree name = TYPE_NAME (record);
6499 tree orig_name = TYPE_NAME (type);
6501 if (TREE_CODE (name) == TYPE_DECL)
6502 name = DECL_NAME (name);
6504 if (TREE_CODE (orig_name) == TYPE_DECL)
6505 orig_name = DECL_NAME (orig_name);
6507 TYPE_NAME (marker) = concat_name (name, "XVS");
6508 finish_record_type (marker,
6509 create_field_decl (orig_name,
6510 build_reference_type (type),
6511 marker, NULL_TREE, NULL_TREE,
6512 0, 0),
6513 0, true);
6515 add_parallel_type (TYPE_STUB_DECL (record), marker);
6517 if (definition && size && TREE_CODE (size) != INTEGER_CST)
6518 TYPE_SIZE_UNIT (marker)
6519 = create_var_decl (concat_name (name, "XVZ"), NULL_TREE, sizetype,
6520 TYPE_SIZE_UNIT (record), false, false, false,
6521 false, NULL, gnat_entity);
6524 rest_of_record_type_compilation (record);
6526 /* If the size was widened explicitly, maybe give a warning. Take the
6527 original size as the maximum size of the input if there was an
6528 unconstrained record involved and round it up to the specified alignment,
6529 if one was specified. */
6530 if (CONTAINS_PLACEHOLDER_P (orig_size))
6531 orig_size = max_size (orig_size, true);
6533 if (align)
6534 orig_size = round_up (orig_size, align);
6536 if (Present (gnat_entity)
6537 && size
6538 && TREE_CODE (size) != MAX_EXPR
6539 && TREE_CODE (size) != COND_EXPR
6540 && !operand_equal_p (size, orig_size, 0)
6541 && !(TREE_CODE (size) == INTEGER_CST
6542 && TREE_CODE (orig_size) == INTEGER_CST
6543 && (TREE_OVERFLOW (size)
6544 || TREE_OVERFLOW (orig_size)
6545 || tree_int_cst_lt (size, orig_size))))
6547 Node_Id gnat_error_node = Empty;
6549 if (Is_Packed_Array_Type (gnat_entity))
6550 gnat_entity = Original_Array_Type (gnat_entity);
6552 if ((Ekind (gnat_entity) == E_Component
6553 || Ekind (gnat_entity) == E_Discriminant)
6554 && Present (Component_Clause (gnat_entity)))
6555 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
6556 else if (Present (Size_Clause (gnat_entity)))
6557 gnat_error_node = Expression (Size_Clause (gnat_entity));
6559 /* Generate message only for entities that come from source, since
6560 if we have an entity created by expansion, the message will be
6561 generated for some other corresponding source entity. */
6562 if (Comes_From_Source (gnat_entity))
6564 if (Present (gnat_error_node))
6565 post_error_ne_tree ("{^ }bits of & unused?",
6566 gnat_error_node, gnat_entity,
6567 size_diffop (size, orig_size));
6568 else if (is_component_type)
6569 post_error_ne_tree ("component of& padded{ by ^ bits}?",
6570 gnat_entity, gnat_entity,
6571 size_diffop (size, orig_size));
6575 return record;
6578 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6579 the value passed against the list of choices. */
6581 tree
6582 choices_to_gnu (tree operand, Node_Id choices)
6584 Node_Id choice;
6585 Node_Id gnat_temp;
6586 tree result = integer_zero_node;
6587 tree this_test, low = 0, high = 0, single = 0;
6589 for (choice = First (choices); Present (choice); choice = Next (choice))
6591 switch (Nkind (choice))
6593 case N_Range:
6594 low = gnat_to_gnu (Low_Bound (choice));
6595 high = gnat_to_gnu (High_Bound (choice));
6597 this_test
6598 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6599 build_binary_op (GE_EXPR, boolean_type_node,
6600 operand, low),
6601 build_binary_op (LE_EXPR, boolean_type_node,
6602 operand, high));
6604 break;
6606 case N_Subtype_Indication:
6607 gnat_temp = Range_Expression (Constraint (choice));
6608 low = gnat_to_gnu (Low_Bound (gnat_temp));
6609 high = gnat_to_gnu (High_Bound (gnat_temp));
6611 this_test
6612 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6613 build_binary_op (GE_EXPR, boolean_type_node,
6614 operand, low),
6615 build_binary_op (LE_EXPR, boolean_type_node,
6616 operand, high));
6617 break;
6619 case N_Identifier:
6620 case N_Expanded_Name:
6621 /* This represents either a subtype range, an enumeration
6622 literal, or a constant Ekind says which. If an enumeration
6623 literal or constant, fall through to the next case. */
6624 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6625 && Ekind (Entity (choice)) != E_Constant)
6627 tree type = gnat_to_gnu_type (Entity (choice));
6629 low = TYPE_MIN_VALUE (type);
6630 high = TYPE_MAX_VALUE (type);
6632 this_test
6633 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6634 build_binary_op (GE_EXPR, boolean_type_node,
6635 operand, low),
6636 build_binary_op (LE_EXPR, boolean_type_node,
6637 operand, high));
6638 break;
6641 /* ... fall through ... */
6643 case N_Character_Literal:
6644 case N_Integer_Literal:
6645 single = gnat_to_gnu (choice);
6646 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6647 single);
6648 break;
6650 case N_Others_Choice:
6651 this_test = integer_one_node;
6652 break;
6654 default:
6655 gcc_unreachable ();
6658 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6659 this_test);
6662 return result;
6665 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6666 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6668 static int
6669 adjust_packed (tree field_type, tree record_type, int packed)
6671 /* If the field contains an item of variable size, we cannot pack it
6672 because we cannot create temporaries of non-fixed size in case
6673 we need to take the address of the field. See addressable_p and
6674 the notes on the addressability issues for further details. */
6675 if (is_variable_size (field_type))
6676 return 0;
6678 /* If the alignment of the record is specified and the field type
6679 is over-aligned, request Storage_Unit alignment for the field. */
6680 if (packed == -2)
6682 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6683 return -1;
6684 else
6685 return 0;
6688 return packed;
6691 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6692 placed in GNU_RECORD_TYPE.
6694 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6695 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6696 record has a specified alignment.
6698 DEFINITION is true if this field is for a record being defined.
6700 DEBUG_INFO_P is true if we need to write debug information for types
6701 that we may create in the process. */
6703 static tree
6704 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6705 bool definition, bool debug_info_p)
6707 tree gnu_field_id = get_entity_name (gnat_field);
6708 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
6709 tree gnu_field, gnu_size, gnu_pos;
6710 bool needs_strict_alignment
6711 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
6712 || Treat_As_Volatile (gnat_field));
6714 /* If this field requires strict alignment, we cannot pack it because
6715 it would very likely be under-aligned in the record. */
6716 if (needs_strict_alignment)
6717 packed = 0;
6718 else
6719 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6721 /* If a size is specified, use it. Otherwise, if the record type is packed,
6722 use the official RM size. See "Handling of Type'Size Values" in Einfo
6723 for further details. */
6724 if (Known_Static_Esize (gnat_field))
6725 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6726 gnat_field, FIELD_DECL, false, true);
6727 else if (packed == 1)
6728 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
6729 gnat_field, FIELD_DECL, false, true);
6730 else
6731 gnu_size = NULL_TREE;
6733 /* If we have a specified size that is smaller than that of the field's type,
6734 or a position is specified, and the field's type is a record that doesn't
6735 require strict alignment, see if we can get either an integral mode form
6736 of the type or a smaller form. If we can, show a size was specified for
6737 the field if there wasn't one already, so we know to make this a bitfield
6738 and avoid making things wider.
6740 Changing to an integral mode form is useful when the record is packed as
6741 we can then place the field at a non-byte-aligned position and so achieve
6742 tighter packing. This is in addition required if the field shares a byte
6743 with another field and the front-end lets the back-end handle the access
6744 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6746 Changing to a smaller form is required if the specified size is smaller
6747 than that of the field's type and the type contains sub-fields that are
6748 padded, in order to avoid generating accesses to these sub-fields that
6749 are wider than the field.
6751 We avoid the transformation if it is not required or potentially useful,
6752 as it might entail an increase of the field's alignment and have ripple
6753 effects on the outer record type. A typical case is a field known to be
6754 byte-aligned and not to share a byte with another field. */
6755 if (!needs_strict_alignment
6756 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6757 && !TYPE_FAT_POINTER_P (gnu_field_type)
6758 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
6759 && (packed == 1
6760 || (gnu_size
6761 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6762 || (Present (Component_Clause (gnat_field))
6763 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6764 % BITS_PER_UNIT == 0
6765 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6767 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6768 if (gnu_packable_type != gnu_field_type)
6770 gnu_field_type = gnu_packable_type;
6771 if (!gnu_size)
6772 gnu_size = rm_size (gnu_field_type);
6776 /* If we are packing the record and the field is BLKmode, round the
6777 size up to a byte boundary. */
6778 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6779 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6781 if (Present (Component_Clause (gnat_field)))
6783 Entity_Id gnat_parent
6784 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6786 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6787 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6788 gnat_field, FIELD_DECL, false, true);
6790 /* Ensure the position does not overlap with the parent subtype, if there
6791 is one. This test is omitted if the parent of the tagged type has a
6792 full rep clause since, in this case, component clauses are allowed to
6793 overlay the space allocated for the parent type and the front-end has
6794 checked that there are no overlapping components. */
6795 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6797 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6799 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6800 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6802 post_error_ne_tree
6803 ("offset of& must be beyond parent{, minimum allowed is ^}",
6804 First_Bit (Component_Clause (gnat_field)), gnat_field,
6805 TYPE_SIZE_UNIT (gnu_parent));
6809 /* If this field needs strict alignment, ensure the record is
6810 sufficiently aligned and that that position and size are
6811 consistent with the alignment. */
6812 if (needs_strict_alignment)
6814 TYPE_ALIGN (gnu_record_type)
6815 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6817 if (gnu_size
6818 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6820 if (Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
6821 post_error_ne_tree
6822 ("atomic field& must be natural size of type{ (^)}",
6823 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6824 TYPE_SIZE (gnu_field_type));
6826 else if (Is_Aliased (gnat_field))
6827 post_error_ne_tree
6828 ("size of aliased field& must be ^ bits",
6829 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6830 TYPE_SIZE (gnu_field_type));
6832 else if (Strict_Alignment (Etype (gnat_field)))
6833 post_error_ne_tree
6834 ("size of & with aliased or tagged components not ^ bits",
6835 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6836 TYPE_SIZE (gnu_field_type));
6838 gnu_size = NULL_TREE;
6841 if (!integer_zerop (size_binop
6842 (TRUNC_MOD_EXPR, gnu_pos,
6843 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6845 if (Is_Aliased (gnat_field))
6846 post_error_ne_num
6847 ("position of aliased field& must be multiple of ^ bits",
6848 First_Bit (Component_Clause (gnat_field)), gnat_field,
6849 TYPE_ALIGN (gnu_field_type));
6851 else if (Treat_As_Volatile (gnat_field))
6852 post_error_ne_num
6853 ("position of volatile field& must be multiple of ^ bits",
6854 First_Bit (Component_Clause (gnat_field)), gnat_field,
6855 TYPE_ALIGN (gnu_field_type));
6857 else if (Strict_Alignment (Etype (gnat_field)))
6858 post_error_ne_num
6859 ("position of & with aliased or tagged components not multiple of ^ bits",
6860 First_Bit (Component_Clause (gnat_field)), gnat_field,
6861 TYPE_ALIGN (gnu_field_type));
6863 else
6864 gcc_unreachable ();
6866 gnu_pos = NULL_TREE;
6870 if (Is_Atomic (gnat_field))
6871 check_ok_for_atomic (gnu_field_type, gnat_field, false);
6874 /* If the record has rep clauses and this is the tag field, make a rep
6875 clause for it as well. */
6876 else if (Has_Specified_Layout (Scope (gnat_field))
6877 && Chars (gnat_field) == Name_uTag)
6879 gnu_pos = bitsize_zero_node;
6880 gnu_size = TYPE_SIZE (gnu_field_type);
6883 else
6884 gnu_pos = NULL_TREE;
6886 /* We need to make the size the maximum for the type if it is
6887 self-referential and an unconstrained type. In that case, we can't
6888 pack the field since we can't make a copy to align it. */
6889 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6890 && !gnu_size
6891 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6892 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
6894 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6895 packed = 0;
6898 /* If a size is specified, adjust the field's type to it. */
6899 if (gnu_size)
6901 tree orig_field_type;
6903 /* If the field's type is justified modular, we would need to remove
6904 the wrapper to (better) meet the layout requirements. However we
6905 can do so only if the field is not aliased to preserve the unique
6906 layout and if the prescribed size is not greater than that of the
6907 packed array to preserve the justification. */
6908 if (!needs_strict_alignment
6909 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6910 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6911 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6912 <= 0)
6913 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6915 gnu_field_type
6916 = make_type_from_size (gnu_field_type, gnu_size,
6917 Has_Biased_Representation (gnat_field));
6919 orig_field_type = gnu_field_type;
6920 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6921 false, false, definition, true);
6923 /* If a padding record was made, declare it now since it will never be
6924 declared otherwise. This is necessary to ensure that its subtrees
6925 are properly marked. */
6926 if (gnu_field_type != orig_field_type
6927 && !DECL_P (TYPE_NAME (gnu_field_type)))
6928 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, NULL,
6929 true, debug_info_p, gnat_field);
6932 /* Otherwise (or if there was an error), don't specify a position. */
6933 else
6934 gnu_pos = NULL_TREE;
6936 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6937 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6939 /* Now create the decl for the field. */
6940 gnu_field
6941 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6942 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6943 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6944 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
6946 if (Ekind (gnat_field) == E_Discriminant)
6947 DECL_DISCRIMINANT_NUMBER (gnu_field)
6948 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6950 return gnu_field;
6953 /* Return true if TYPE is a type with variable size, a padding type with a
6954 field of variable size or is a record that has a field such a field. */
6956 static bool
6957 is_variable_size (tree type)
6959 tree field;
6961 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6962 return true;
6964 if (TYPE_IS_PADDING_P (type)
6965 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6966 return true;
6968 if (TREE_CODE (type) != RECORD_TYPE
6969 && TREE_CODE (type) != UNION_TYPE
6970 && TREE_CODE (type) != QUAL_UNION_TYPE)
6971 return false;
6973 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6974 if (is_variable_size (TREE_TYPE (field)))
6975 return true;
6977 return false;
6980 /* qsort comparer for the bit positions of two record components. */
6982 static int
6983 compare_field_bitpos (const PTR rt1, const PTR rt2)
6985 const_tree const field1 = * (const_tree const *) rt1;
6986 const_tree const field2 = * (const_tree const *) rt2;
6987 const int ret
6988 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6990 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6993 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set
6994 the result as the field list of GNU_RECORD_TYPE and finish it up. When
6995 called from gnat_to_gnu_entity during the processing of a record type
6996 definition, the GCC node for the parent, if any, will be the single field
6997 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6998 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6999 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
7001 PACKED is 1 if this is for a packed record, -1 if this is for a record
7002 with Component_Alignment of Storage_Unit, -2 if this is for a record
7003 with a specified alignment.
7005 DEFINITION is true if we are defining this record type.
7007 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
7008 out the record. This means the alignment only serves to force fields to
7009 be bitfields, but not to require the record to be that aligned. This is
7010 used for variants.
7012 ALL_REP is true if a rep clause is present for all the fields.
7014 UNCHECKED_UNION is true if we are building this type for a record with a
7015 Pragma Unchecked_Union.
7017 DEBUG_INFO is true if we need to write debug information about the type.
7019 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
7020 mean that its contents may be unused as well, only the container itself.
7022 REORDER is true if we are permitted to reorder components of this type.
7024 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
7025 with a rep clause is to be added; in this case, that is all that should
7026 be done with such fields. */
7028 static void
7029 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
7030 tree gnu_field_list, int packed, bool definition,
7031 bool cancel_alignment, bool all_rep,
7032 bool unchecked_union, bool debug_info,
7033 bool maybe_unused, bool reorder,
7034 tree *p_gnu_rep_list)
7036 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
7037 bool layout_with_rep = false;
7038 Node_Id component_decl, variant_part;
7039 tree gnu_field, gnu_next, gnu_last;
7040 tree gnu_variant_part = NULL_TREE;
7041 tree gnu_rep_list = NULL_TREE;
7042 tree gnu_var_list = NULL_TREE;
7043 tree gnu_self_list = NULL_TREE;
7045 /* For each component referenced in a component declaration create a GCC
7046 field and add it to the list, skipping pragmas in the GNAT list. */
7047 gnu_last = tree_last (gnu_field_list);
7048 if (Present (Component_Items (gnat_component_list)))
7049 for (component_decl
7050 = First_Non_Pragma (Component_Items (gnat_component_list));
7051 Present (component_decl);
7052 component_decl = Next_Non_Pragma (component_decl))
7054 Entity_Id gnat_field = Defining_Entity (component_decl);
7055 Name_Id gnat_name = Chars (gnat_field);
7057 /* If present, the _Parent field must have been created as the single
7058 field of the record type. Put it before any other fields. */
7059 if (gnat_name == Name_uParent)
7061 gnu_field = TYPE_FIELDS (gnu_record_type);
7062 gnu_field_list = chainon (gnu_field_list, gnu_field);
7064 else
7066 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7067 definition, debug_info);
7069 /* If this is the _Tag field, put it before any other fields. */
7070 if (gnat_name == Name_uTag)
7071 gnu_field_list = chainon (gnu_field_list, gnu_field);
7073 /* If this is the _Controller field, put it before the other
7074 fields except for the _Tag or _Parent field. */
7075 else if (gnat_name == Name_uController && gnu_last)
7077 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7078 DECL_CHAIN (gnu_last) = gnu_field;
7081 /* If this is a regular field, put it after the other fields. */
7082 else
7084 DECL_CHAIN (gnu_field) = gnu_field_list;
7085 gnu_field_list = gnu_field;
7086 if (!gnu_last)
7087 gnu_last = gnu_field;
7091 save_gnu_tree (gnat_field, gnu_field, false);
7094 /* At the end of the component list there may be a variant part. */
7095 variant_part = Variant_Part (gnat_component_list);
7097 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7098 mutually exclusive and should go in the same memory. To do this we need
7099 to treat each variant as a record whose elements are created from the
7100 component list for the variant. So here we create the records from the
7101 lists for the variants and put them all into the QUAL_UNION_TYPE.
7102 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7103 use GNU_RECORD_TYPE if there are no fields so far. */
7104 if (Present (variant_part))
7106 Node_Id gnat_discr = Name (variant_part), variant;
7107 tree gnu_discr = gnat_to_gnu (gnat_discr);
7108 tree gnu_name = TYPE_NAME (gnu_record_type);
7109 tree gnu_var_name
7110 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7111 "XVN");
7112 tree gnu_union_type, gnu_union_name;
7113 tree gnu_variant_list = NULL_TREE;
7115 if (TREE_CODE (gnu_name) == TYPE_DECL)
7116 gnu_name = DECL_NAME (gnu_name);
7118 gnu_union_name
7119 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7121 /* Reuse an enclosing union if all fields are in the variant part
7122 and there is no representation clause on the record, to match
7123 the layout of C unions. There is an associated check below. */
7124 if (!gnu_field_list
7125 && TREE_CODE (gnu_record_type) == UNION_TYPE
7126 && !TYPE_PACKED (gnu_record_type))
7127 gnu_union_type = gnu_record_type;
7128 else
7130 gnu_union_type
7131 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7133 TYPE_NAME (gnu_union_type) = gnu_union_name;
7134 TYPE_ALIGN (gnu_union_type) = 0;
7135 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7138 for (variant = First_Non_Pragma (Variants (variant_part));
7139 Present (variant);
7140 variant = Next_Non_Pragma (variant))
7142 tree gnu_variant_type = make_node (RECORD_TYPE);
7143 tree gnu_inner_name;
7144 tree gnu_qual;
7146 Get_Variant_Encoding (variant);
7147 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7148 TYPE_NAME (gnu_variant_type)
7149 = concat_name (gnu_union_name,
7150 IDENTIFIER_POINTER (gnu_inner_name));
7152 /* Set the alignment of the inner type in case we need to make
7153 inner objects into bitfields, but then clear it out so the
7154 record actually gets only the alignment required. */
7155 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7156 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7158 /* Similarly, if the outer record has a size specified and all
7159 fields have record rep clauses, we can propagate the size
7160 into the variant part. */
7161 if (all_rep_and_size)
7163 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7164 TYPE_SIZE_UNIT (gnu_variant_type)
7165 = TYPE_SIZE_UNIT (gnu_record_type);
7168 /* Add the fields into the record type for the variant. Note that
7169 we aren't sure to really use it at this point, see below. */
7170 components_to_record (gnu_variant_type, Component_List (variant),
7171 NULL_TREE, packed, definition,
7172 !all_rep_and_size, all_rep,
7173 unchecked_union, debug_info,
7174 true, reorder, &gnu_rep_list);
7176 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7178 Set_Present_Expr (variant, annotate_value (gnu_qual));
7180 /* If this is an Unchecked_Union and we have exactly one field,
7181 use this field directly to match the layout of C unions. */
7182 if (unchecked_union
7183 && TYPE_FIELDS (gnu_variant_type)
7184 && !DECL_CHAIN (TYPE_FIELDS (gnu_variant_type)))
7185 gnu_field = TYPE_FIELDS (gnu_variant_type);
7186 else
7188 /* Deal with packedness like in gnat_to_gnu_field. */
7189 int field_packed
7190 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7192 /* Finalize the record type now. We used to throw away
7193 empty records but we no longer do that because we need
7194 them to generate complete debug info for the variant;
7195 otherwise, the union type definition will be lacking
7196 the fields associated with these empty variants. */
7197 rest_of_record_type_compilation (gnu_variant_type);
7198 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7199 NULL, true, debug_info, gnat_component_list);
7201 gnu_field
7202 = create_field_decl (gnu_inner_name, gnu_variant_type,
7203 gnu_union_type,
7204 all_rep_and_size
7205 ? TYPE_SIZE (gnu_variant_type) : 0,
7206 all_rep_and_size
7207 ? bitsize_zero_node : 0,
7208 field_packed, 0);
7210 DECL_INTERNAL_P (gnu_field) = 1;
7212 if (!unchecked_union)
7213 DECL_QUALIFIER (gnu_field) = gnu_qual;
7216 DECL_CHAIN (gnu_field) = gnu_variant_list;
7217 gnu_variant_list = gnu_field;
7220 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7221 if (gnu_variant_list)
7223 int union_field_packed;
7225 if (all_rep_and_size)
7227 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7228 TYPE_SIZE_UNIT (gnu_union_type)
7229 = TYPE_SIZE_UNIT (gnu_record_type);
7232 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7233 all_rep_and_size ? 1 : 0, debug_info);
7235 /* If GNU_UNION_TYPE is our record type, it means we must have an
7236 Unchecked_Union with no fields. Verify that and, if so, just
7237 return. */
7238 if (gnu_union_type == gnu_record_type)
7240 gcc_assert (unchecked_union
7241 && !gnu_field_list
7242 && !gnu_rep_list);
7243 return;
7246 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type,
7247 NULL, true, debug_info, gnat_component_list);
7249 /* Deal with packedness like in gnat_to_gnu_field. */
7250 union_field_packed
7251 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7253 gnu_variant_part
7254 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7255 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
7256 all_rep ? bitsize_zero_node : 0,
7257 union_field_packed, 0);
7259 DECL_INTERNAL_P (gnu_variant_part) = 1;
7260 DECL_CHAIN (gnu_variant_part) = gnu_field_list;
7261 gnu_field_list = gnu_variant_part;
7265 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7266 permitted to reorder components, self-referential sizes or variable sizes.
7267 If they do, pull them out and put them onto the appropriate list. We have
7268 to do this in a separate pass since we want to handle the discriminants
7269 but can't play with them until we've used them in debugging data above.
7271 ??? If we reorder them, debugging information will be wrong but there is
7272 nothing that can be done about this at the moment. */
7273 gnu_last = NULL_TREE;
7275 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7276 do { \
7277 if (gnu_last) \
7278 DECL_CHAIN (gnu_last) = gnu_next; \
7279 else \
7280 gnu_field_list = gnu_next; \
7282 DECL_CHAIN (gnu_field) = (LIST); \
7283 (LIST) = gnu_field; \
7284 } while (0)
7286 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7288 gnu_next = DECL_CHAIN (gnu_field);
7290 if (DECL_FIELD_OFFSET (gnu_field))
7292 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7293 continue;
7296 if (reorder)
7298 /* Pull out the variant part and put it onto GNU_SELF_LIST. */
7299 if (gnu_field == gnu_variant_part)
7301 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7302 continue;
7305 /* Skip internal fields and fields with fixed size. */
7306 if (!DECL_INTERNAL_P (gnu_field)
7307 && !(DECL_SIZE (gnu_field)
7308 && TREE_CODE (DECL_SIZE (gnu_field)) == INTEGER_CST))
7310 tree type_size = TYPE_SIZE (TREE_TYPE (gnu_field));
7312 if (CONTAINS_PLACEHOLDER_P (type_size))
7314 MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7315 continue;
7318 if (TREE_CODE (type_size) != INTEGER_CST)
7320 MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7321 continue;
7326 gnu_last = gnu_field;
7329 #undef MOVE_FROM_FIELD_LIST_TO
7331 /* If permitted, we reorder the components as follows:
7333 1) all fixed length fields,
7334 2) all fields whose length doesn't depend on discriminants,
7335 3) all fields whose length depends on discriminants,
7336 4) the variant part,
7338 within the record and within each variant recursively. */
7339 if (reorder)
7340 gnu_field_list
7341 = chainon (nreverse (gnu_self_list),
7342 chainon (nreverse (gnu_var_list), gnu_field_list));
7344 /* If we have any fields in our rep'ed field list and it is not the case that
7345 all the fields in the record have rep clauses and P_REP_LIST is nonzero,
7346 set it and ignore these fields. */
7347 if (gnu_rep_list && p_gnu_rep_list && !all_rep)
7348 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7350 /* Otherwise, sort the fields by bit position and put them into their own
7351 record, before the others, if we also have fields without rep clauses. */
7352 else if (gnu_rep_list)
7354 tree gnu_rep_type
7355 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
7356 int i, len = list_length (gnu_rep_list);
7357 tree *gnu_arr = XALLOCAVEC (tree, len);
7359 for (gnu_field = gnu_rep_list, i = 0;
7360 gnu_field;
7361 gnu_field = DECL_CHAIN (gnu_field), i++)
7362 gnu_arr[i] = gnu_field;
7364 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7366 /* Put the fields in the list in order of increasing position, which
7367 means we start from the end. */
7368 gnu_rep_list = NULL_TREE;
7369 for (i = len - 1; i >= 0; i--)
7371 DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7372 gnu_rep_list = gnu_arr[i];
7373 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7376 if (gnu_field_list)
7378 finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7379 gnu_field
7380 = create_field_decl (get_identifier ("REP"), gnu_rep_type,
7381 gnu_record_type, NULL_TREE, NULL_TREE, 0, 1);
7382 DECL_INTERNAL_P (gnu_field) = 1;
7383 gnu_field_list = chainon (gnu_field_list, gnu_field);
7385 else
7387 layout_with_rep = true;
7388 gnu_field_list = nreverse (gnu_rep_list);
7392 if (cancel_alignment)
7393 TYPE_ALIGN (gnu_record_type) = 0;
7395 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
7396 layout_with_rep ? 1 : 0, debug_info && !maybe_unused);
7399 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7400 placed into an Esize, Component_Bit_Offset, or Component_Size value
7401 in the GNAT tree. */
7403 static Uint
7404 annotate_value (tree gnu_size)
7406 TCode tcode;
7407 Node_Ref_Or_Val ops[3], ret;
7408 struct tree_int_map **h = NULL;
7409 int i;
7411 /* See if we've already saved the value for this node. */
7412 if (EXPR_P (gnu_size))
7414 struct tree_int_map in;
7415 if (!annotate_value_cache)
7416 annotate_value_cache = htab_create_ggc (512, tree_int_map_hash,
7417 tree_int_map_eq, 0);
7418 in.base.from = gnu_size;
7419 h = (struct tree_int_map **)
7420 htab_find_slot (annotate_value_cache, &in, INSERT);
7422 if (*h)
7423 return (Node_Ref_Or_Val) (*h)->to;
7426 /* If we do not return inside this switch, TCODE will be set to the
7427 code to use for a Create_Node operand and LEN (set above) will be
7428 the number of recursive calls for us to make. */
7430 switch (TREE_CODE (gnu_size))
7432 case INTEGER_CST:
7433 if (TREE_OVERFLOW (gnu_size))
7434 return No_Uint;
7436 /* This may come from a conversion from some smaller type, so ensure
7437 this is in bitsizetype. */
7438 gnu_size = convert (bitsizetype, gnu_size);
7440 /* For a negative value, build NEGATE_EXPR of the opposite. Such values
7441 appear in expressions containing aligning patterns. Note that, since
7442 sizetype is sign-extended but nonetheless unsigned, we don't directly
7443 use tree_int_cst_sgn. */
7444 if (TREE_INT_CST_HIGH (gnu_size) < 0)
7446 tree op_size = fold_build1 (NEGATE_EXPR, bitsizetype, gnu_size);
7447 return annotate_value (build1 (NEGATE_EXPR, bitsizetype, op_size));
7450 return UI_From_gnu (gnu_size);
7452 case COMPONENT_REF:
7453 /* The only case we handle here is a simple discriminant reference. */
7454 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
7455 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
7456 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7457 return Create_Node (Discrim_Val,
7458 annotate_value (DECL_DISCRIMINANT_NUMBER
7459 (TREE_OPERAND (gnu_size, 1))),
7460 No_Uint, No_Uint);
7461 else
7462 return No_Uint;
7464 CASE_CONVERT: case NON_LVALUE_EXPR:
7465 return annotate_value (TREE_OPERAND (gnu_size, 0));
7467 /* Now just list the operations we handle. */
7468 case COND_EXPR: tcode = Cond_Expr; break;
7469 case PLUS_EXPR: tcode = Plus_Expr; break;
7470 case MINUS_EXPR: tcode = Minus_Expr; break;
7471 case MULT_EXPR: tcode = Mult_Expr; break;
7472 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7473 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7474 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7475 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7476 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7477 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7478 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7479 case NEGATE_EXPR: tcode = Negate_Expr; break;
7480 case MIN_EXPR: tcode = Min_Expr; break;
7481 case MAX_EXPR: tcode = Max_Expr; break;
7482 case ABS_EXPR: tcode = Abs_Expr; break;
7483 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7484 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7485 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7486 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7487 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7488 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7489 case BIT_AND_EXPR: tcode = Bit_And_Expr; break;
7490 case LT_EXPR: tcode = Lt_Expr; break;
7491 case LE_EXPR: tcode = Le_Expr; break;
7492 case GT_EXPR: tcode = Gt_Expr; break;
7493 case GE_EXPR: tcode = Ge_Expr; break;
7494 case EQ_EXPR: tcode = Eq_Expr; break;
7495 case NE_EXPR: tcode = Ne_Expr; break;
7497 case CALL_EXPR:
7499 tree t = maybe_inline_call_in_expr (gnu_size);
7500 if (t)
7501 return annotate_value (t);
7504 /* Fall through... */
7506 default:
7507 return No_Uint;
7510 /* Now get each of the operands that's relevant for this code. If any
7511 cannot be expressed as a repinfo node, say we can't. */
7512 for (i = 0; i < 3; i++)
7513 ops[i] = No_Uint;
7515 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7517 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7518 if (ops[i] == No_Uint)
7519 return No_Uint;
7522 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7524 /* Save the result in the cache. */
7525 if (h)
7527 *h = ggc_alloc_tree_int_map ();
7528 (*h)->base.from = gnu_size;
7529 (*h)->to = ret;
7532 return ret;
7535 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7536 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7537 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7538 BY_REF is true if the object is used by reference and BY_DOUBLE_REF is
7539 true if the object is used by double reference. */
7541 void
7542 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref,
7543 bool by_double_ref)
7545 if (by_ref)
7547 if (by_double_ref)
7548 gnu_type = TREE_TYPE (gnu_type);
7550 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7551 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7552 else
7553 gnu_type = TREE_TYPE (gnu_type);
7556 if (Unknown_Esize (gnat_entity))
7558 if (TREE_CODE (gnu_type) == RECORD_TYPE
7559 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7560 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7561 else if (!size)
7562 size = TYPE_SIZE (gnu_type);
7564 if (size)
7565 Set_Esize (gnat_entity, annotate_value (size));
7568 if (Unknown_Alignment (gnat_entity))
7569 Set_Alignment (gnat_entity,
7570 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7573 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7574 Return NULL_TREE if there is no such element in the list. */
7576 static tree
7577 purpose_member_field (const_tree elem, tree list)
7579 while (list)
7581 tree field = TREE_PURPOSE (list);
7582 if (SAME_FIELD_P (field, elem))
7583 return list;
7584 list = TREE_CHAIN (list);
7586 return NULL_TREE;
7589 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7590 set Component_Bit_Offset and Esize of the components to the position and
7591 size used by Gigi. */
7593 static void
7594 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7596 Entity_Id gnat_field;
7597 tree gnu_list;
7599 /* We operate by first making a list of all fields and their position (we
7600 can get the size easily) and then update all the sizes in the tree. */
7601 gnu_list
7602 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7603 BIGGEST_ALIGNMENT, NULL_TREE);
7605 for (gnat_field = First_Entity (gnat_entity);
7606 Present (gnat_field);
7607 gnat_field = Next_Entity (gnat_field))
7608 if (Ekind (gnat_field) == E_Component
7609 || (Ekind (gnat_field) == E_Discriminant
7610 && !Is_Unchecked_Union (Scope (gnat_field))))
7612 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7613 gnu_list);
7614 if (t)
7616 tree parent_offset;
7618 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
7620 /* In this mode the tag and parent components are not
7621 generated, so we add the appropriate offset to each
7622 component. For a component appearing in the current
7623 extension, the offset is the size of the parent. */
7624 if (Is_Derived_Type (gnat_entity)
7625 && Original_Record_Component (gnat_field) == gnat_field)
7626 parent_offset
7627 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7628 bitsizetype);
7629 else
7630 parent_offset = bitsize_int (POINTER_SIZE);
7632 else
7633 parent_offset = bitsize_zero_node;
7635 Set_Component_Bit_Offset
7636 (gnat_field,
7637 annotate_value
7638 (size_binop (PLUS_EXPR,
7639 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7640 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7641 parent_offset)));
7643 Set_Esize (gnat_field,
7644 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7646 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7648 /* If there is no entry, this is an inherited component whose
7649 position is the same as in the parent type. */
7650 Set_Component_Bit_Offset
7651 (gnat_field,
7652 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7654 Set_Esize (gnat_field,
7655 Esize (Original_Record_Component (gnat_field)));
7660 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7661 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7662 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7663 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7664 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7665 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7666 pre-existing list to be chained to the newly created entries. */
7668 static tree
7669 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7670 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7672 tree gnu_field;
7674 for (gnu_field = TYPE_FIELDS (gnu_type);
7675 gnu_field;
7676 gnu_field = DECL_CHAIN (gnu_field))
7678 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7679 DECL_FIELD_BIT_OFFSET (gnu_field));
7680 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7681 DECL_FIELD_OFFSET (gnu_field));
7682 unsigned int our_offset_align
7683 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7684 tree v = make_tree_vec (3);
7686 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7687 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7688 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7689 gnu_list = tree_cons (gnu_field, v, gnu_list);
7691 /* Recurse on internal fields, flattening the nested fields except for
7692 those in the variant part, if requested. */
7693 if (DECL_INTERNAL_P (gnu_field))
7695 tree gnu_field_type = TREE_TYPE (gnu_field);
7696 if (do_not_flatten_variant
7697 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7698 gnu_list
7699 = build_position_list (gnu_field_type, do_not_flatten_variant,
7700 size_zero_node, bitsize_zero_node,
7701 BIGGEST_ALIGNMENT, gnu_list);
7702 else
7703 gnu_list
7704 = build_position_list (gnu_field_type, do_not_flatten_variant,
7705 gnu_our_offset, gnu_our_bitpos,
7706 our_offset_align, gnu_list);
7710 return gnu_list;
7713 /* Return a VEC describing the substitutions needed to reflect the
7714 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7715 be in any order. The values in an element of the VEC are in the form
7716 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7717 a definition of GNAT_SUBTYPE. */
7719 static VEC(subst_pair,heap) *
7720 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7722 VEC(subst_pair,heap) *gnu_vec = NULL;
7723 Entity_Id gnat_discrim;
7724 Node_Id gnat_value;
7726 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7727 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
7728 Present (gnat_discrim);
7729 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7730 gnat_value = Next_Elmt (gnat_value))
7731 /* Ignore access discriminants. */
7732 if (!Is_Access_Type (Etype (Node (gnat_value))))
7734 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7735 tree replacement = convert (TREE_TYPE (gnu_field),
7736 elaborate_expression
7737 (Node (gnat_value), gnat_subtype,
7738 get_entity_name (gnat_discrim),
7739 definition, true, false));
7740 subst_pair *s = VEC_safe_push (subst_pair, heap, gnu_vec, NULL);
7741 s->discriminant = gnu_field;
7742 s->replacement = replacement;
7745 return gnu_vec;
7748 /* Scan all fields in QUAL_UNION_TYPE and return a VEC describing the
7749 variants of QUAL_UNION_TYPE that are still relevant after applying
7750 the substitutions described in SUBST_LIST. VARIANT_LIST is a
7751 pre-existing VEC onto which newly created entries should be
7752 pushed. */
7754 static VEC(variant_desc,heap) *
7755 build_variant_list (tree qual_union_type, VEC(subst_pair,heap) *subst_list,
7756 VEC(variant_desc,heap) *variant_list)
7758 tree gnu_field;
7760 for (gnu_field = TYPE_FIELDS (qual_union_type);
7761 gnu_field;
7762 gnu_field = DECL_CHAIN (gnu_field))
7764 tree qual = DECL_QUALIFIER (gnu_field);
7765 unsigned ix;
7766 subst_pair *s;
7768 FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
7769 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7771 /* If the new qualifier is not unconditionally false, its variant may
7772 still be accessed. */
7773 if (!integer_zerop (qual))
7775 variant_desc *v;
7776 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7778 v = VEC_safe_push (variant_desc, heap, variant_list, NULL);
7779 v->type = variant_type;
7780 v->field = gnu_field;
7781 v->qual = qual;
7782 v->record = NULL_TREE;
7784 /* Recurse on the variant subpart of the variant, if any. */
7785 variant_subpart = get_variant_part (variant_type);
7786 if (variant_subpart)
7787 variant_list = build_variant_list (TREE_TYPE (variant_subpart),
7788 subst_list, variant_list);
7790 /* If the new qualifier is unconditionally true, the subsequent
7791 variants cannot be accessed. */
7792 if (integer_onep (qual))
7793 break;
7797 return variant_list;
7800 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7801 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7802 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7803 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7804 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7805 true if we are being called to process the Component_Size of GNAT_OBJECT;
7806 this is used only for error messages. ZERO_OK is true if a size of zero
7807 is permitted; if ZERO_OK is false, it means that a size of zero should be
7808 treated as an unspecified size. */
7810 static tree
7811 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7812 enum tree_code kind, bool component_p, bool zero_ok)
7814 Node_Id gnat_error_node;
7815 tree type_size, size;
7817 /* Return 0 if no size was specified. */
7818 if (uint_size == No_Uint)
7819 return NULL_TREE;
7821 /* Ignore a negative size since that corresponds to our back-annotation. */
7822 if (UI_Lt (uint_size, Uint_0))
7823 return NULL_TREE;
7825 /* Find the node to use for error messages. */
7826 if ((Ekind (gnat_object) == E_Component
7827 || Ekind (gnat_object) == E_Discriminant)
7828 && Present (Component_Clause (gnat_object)))
7829 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7830 else if (Present (Size_Clause (gnat_object)))
7831 gnat_error_node = Expression (Size_Clause (gnat_object));
7832 else
7833 gnat_error_node = gnat_object;
7835 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7836 but cannot be represented in bitsizetype. */
7837 size = UI_To_gnu (uint_size, bitsizetype);
7838 if (TREE_OVERFLOW (size))
7840 if (component_p)
7841 post_error_ne ("component size for& is too large", gnat_error_node,
7842 gnat_object);
7843 else
7844 post_error_ne ("size for& is too large", gnat_error_node,
7845 gnat_object);
7846 return NULL_TREE;
7849 /* Ignore a zero size if it is not permitted. */
7850 if (!zero_ok && integer_zerop (size))
7851 return NULL_TREE;
7853 /* The size of objects is always a multiple of a byte. */
7854 if (kind == VAR_DECL
7855 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7857 if (component_p)
7858 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7859 gnat_error_node, gnat_object);
7860 else
7861 post_error_ne ("size for& is not a multiple of Storage_Unit",
7862 gnat_error_node, gnat_object);
7863 return NULL_TREE;
7866 /* If this is an integral type or a packed array type, the front-end has
7867 already verified the size, so we need not do it here (which would mean
7868 checking against the bounds). However, if this is an aliased object,
7869 it may not be smaller than the type of the object. */
7870 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7871 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7872 return size;
7874 /* If the object is a record that contains a template, add the size of the
7875 template to the specified size. */
7876 if (TREE_CODE (gnu_type) == RECORD_TYPE
7877 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7878 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7880 if (kind == VAR_DECL
7881 /* If a type needs strict alignment, a component of this type in
7882 a packed record cannot be packed and thus uses the type size. */
7883 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7884 type_size = TYPE_SIZE (gnu_type);
7885 else
7886 type_size = rm_size (gnu_type);
7888 /* Modify the size of a discriminated type to be the maximum size. */
7889 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7890 type_size = max_size (type_size, true);
7892 /* If this is an access type or a fat pointer, the minimum size is that given
7893 by the smallest integral mode that's valid for pointers. */
7894 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7896 enum machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7897 while (!targetm.valid_pointer_mode (p_mode))
7898 p_mode = GET_MODE_WIDER_MODE (p_mode);
7899 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7902 /* Issue an error either if the default size of the object isn't a constant
7903 or if the new size is smaller than it. */
7904 if (TREE_CODE (type_size) != INTEGER_CST
7905 || TREE_OVERFLOW (type_size)
7906 || tree_int_cst_lt (size, type_size))
7908 if (component_p)
7909 post_error_ne_tree
7910 ("component size for& too small{, minimum allowed is ^}",
7911 gnat_error_node, gnat_object, type_size);
7912 else
7913 post_error_ne_tree
7914 ("size for& too small{, minimum allowed is ^}",
7915 gnat_error_node, gnat_object, type_size);
7916 return NULL_TREE;
7919 return size;
7922 /* Similarly, but both validate and process a value of RM size. This routine
7923 is only called for types. */
7925 static void
7926 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7928 Node_Id gnat_attr_node;
7929 tree old_size, size;
7931 /* Do nothing if no size was specified. */
7932 if (uint_size == No_Uint)
7933 return;
7935 /* Ignore a negative size since that corresponds to our back-annotation. */
7936 if (UI_Lt (uint_size, Uint_0))
7937 return;
7939 /* Only issue an error if a Value_Size clause was explicitly given.
7940 Otherwise, we'd be duplicating an error on the Size clause. */
7941 gnat_attr_node
7942 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
7944 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7945 but cannot be represented in bitsizetype. */
7946 size = UI_To_gnu (uint_size, bitsizetype);
7947 if (TREE_OVERFLOW (size))
7949 if (Present (gnat_attr_node))
7950 post_error_ne ("Value_Size for& is too large", gnat_attr_node,
7951 gnat_entity);
7952 return;
7955 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
7956 exists, or this is an integer type, in which case the front-end will
7957 have always set it. */
7958 if (No (gnat_attr_node)
7959 && integer_zerop (size)
7960 && !Has_Size_Clause (gnat_entity)
7961 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7962 return;
7964 old_size = rm_size (gnu_type);
7966 /* If the old size is self-referential, get the maximum size. */
7967 if (CONTAINS_PLACEHOLDER_P (old_size))
7968 old_size = max_size (old_size, true);
7970 /* Issue an error either if the old size of the object isn't a constant or
7971 if the new size is smaller than it. The front-end has already verified
7972 this for scalar and packed array types. */
7973 if (TREE_CODE (old_size) != INTEGER_CST
7974 || TREE_OVERFLOW (old_size)
7975 || (AGGREGATE_TYPE_P (gnu_type)
7976 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
7977 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
7978 && !(TYPE_IS_PADDING_P (gnu_type)
7979 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
7980 && TYPE_PACKED_ARRAY_TYPE_P
7981 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
7982 && tree_int_cst_lt (size, old_size)))
7984 if (Present (gnat_attr_node))
7985 post_error_ne_tree
7986 ("Value_Size for& too small{, minimum allowed is ^}",
7987 gnat_attr_node, gnat_entity, old_size);
7988 return;
7991 /* Otherwise, set the RM size proper for integral types... */
7992 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
7993 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7994 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
7995 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
7996 SET_TYPE_RM_SIZE (gnu_type, size);
7998 /* ...or the Ada size for record and union types. */
7999 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
8000 || TREE_CODE (gnu_type) == UNION_TYPE
8001 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
8002 && !TYPE_FAT_POINTER_P (gnu_type))
8003 SET_TYPE_ADA_SIZE (gnu_type, size);
8006 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
8007 If TYPE is the best type, return it. Otherwise, make a new type. We
8008 only support new integral and pointer types. FOR_BIASED is true if
8009 we are making a biased type. */
8011 static tree
8012 make_type_from_size (tree type, tree size_tree, bool for_biased)
8014 unsigned HOST_WIDE_INT size;
8015 bool biased_p;
8016 tree new_type;
8018 /* If size indicates an error, just return TYPE to avoid propagating
8019 the error. Likewise if it's too large to represent. */
8020 if (!size_tree || !host_integerp (size_tree, 1))
8021 return type;
8023 size = tree_low_cst (size_tree, 1);
8025 switch (TREE_CODE (type))
8027 case INTEGER_TYPE:
8028 case ENUMERAL_TYPE:
8029 case BOOLEAN_TYPE:
8030 biased_p = (TREE_CODE (type) == INTEGER_TYPE
8031 && TYPE_BIASED_REPRESENTATION_P (type));
8033 /* Integer types with precision 0 are forbidden. */
8034 if (size == 0)
8035 size = 1;
8037 /* Only do something if the type is not a packed array type and
8038 doesn't already have the proper size. */
8039 if (TYPE_PACKED_ARRAY_TYPE_P (type)
8040 || (TYPE_PRECISION (type) == size && biased_p == for_biased))
8041 break;
8043 biased_p |= for_biased;
8044 if (size > LONG_LONG_TYPE_SIZE)
8045 size = LONG_LONG_TYPE_SIZE;
8047 if (TYPE_UNSIGNED (type) || biased_p)
8048 new_type = make_unsigned_type (size);
8049 else
8050 new_type = make_signed_type (size);
8051 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
8052 SET_TYPE_RM_MIN_VALUE (new_type,
8053 convert (TREE_TYPE (new_type),
8054 TYPE_MIN_VALUE (type)));
8055 SET_TYPE_RM_MAX_VALUE (new_type,
8056 convert (TREE_TYPE (new_type),
8057 TYPE_MAX_VALUE (type)));
8058 /* Copy the name to show that it's essentially the same type and
8059 not a subrange type. */
8060 TYPE_NAME (new_type) = TYPE_NAME (type);
8061 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
8062 SET_TYPE_RM_SIZE (new_type, bitsize_int (size));
8063 return new_type;
8065 case RECORD_TYPE:
8066 /* Do something if this is a fat pointer, in which case we
8067 may need to return the thin pointer. */
8068 if (TYPE_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
8070 enum machine_mode p_mode = mode_for_size (size, MODE_INT, 0);
8071 if (!targetm.valid_pointer_mode (p_mode))
8072 p_mode = ptr_mode;
8073 return
8074 build_pointer_type_for_mode
8075 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)),
8076 p_mode, 0);
8078 break;
8080 case POINTER_TYPE:
8081 /* Only do something if this is a thin pointer, in which case we
8082 may need to return the fat pointer. */
8083 if (TYPE_IS_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
8084 return
8085 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
8086 break;
8088 default:
8089 break;
8092 return type;
8095 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8096 a type or object whose present alignment is ALIGN. If this alignment is
8097 valid, return it. Otherwise, give an error and return ALIGN. */
8099 static unsigned int
8100 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
8102 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
8103 unsigned int new_align;
8104 Node_Id gnat_error_node;
8106 /* Don't worry about checking alignment if alignment was not specified
8107 by the source program and we already posted an error for this entity. */
8108 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
8109 return align;
8111 /* Post the error on the alignment clause if any. Note, for the implicit
8112 base type of an array type, the alignment clause is on the first
8113 subtype. */
8114 if (Present (Alignment_Clause (gnat_entity)))
8115 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
8117 else if (Is_Itype (gnat_entity)
8118 && Is_Array_Type (gnat_entity)
8119 && Etype (gnat_entity) == gnat_entity
8120 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8121 gnat_error_node =
8122 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8124 else
8125 gnat_error_node = gnat_entity;
8127 /* Within GCC, an alignment is an integer, so we must make sure a value is
8128 specified that fits in that range. Also, there is an upper bound to
8129 alignments we can support/allow. */
8130 if (!UI_Is_In_Int_Range (alignment)
8131 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8132 post_error_ne_num ("largest supported alignment for& is ^",
8133 gnat_error_node, gnat_entity, max_allowed_alignment);
8134 else if (!(Present (Alignment_Clause (gnat_entity))
8135 && From_At_Mod (Alignment_Clause (gnat_entity)))
8136 && new_align * BITS_PER_UNIT < align)
8138 unsigned int double_align;
8139 bool is_capped_double, align_clause;
8141 /* If the default alignment of "double" or larger scalar types is
8142 specifically capped and the new alignment is above the cap, do
8143 not post an error and change the alignment only if there is an
8144 alignment clause; this makes it possible to have the associated
8145 GCC type overaligned by default for performance reasons. */
8146 if ((double_align = double_float_alignment) > 0)
8148 Entity_Id gnat_type
8149 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8150 is_capped_double
8151 = is_double_float_or_array (gnat_type, &align_clause);
8153 else if ((double_align = double_scalar_alignment) > 0)
8155 Entity_Id gnat_type
8156 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8157 is_capped_double
8158 = is_double_scalar_or_array (gnat_type, &align_clause);
8160 else
8161 is_capped_double = align_clause = false;
8163 if (is_capped_double && new_align >= double_align)
8165 if (align_clause)
8166 align = new_align * BITS_PER_UNIT;
8168 else
8170 if (is_capped_double)
8171 align = double_align * BITS_PER_UNIT;
8173 post_error_ne_num ("alignment for& must be at least ^",
8174 gnat_error_node, gnat_entity,
8175 align / BITS_PER_UNIT);
8178 else
8180 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8181 if (new_align > align)
8182 align = new_align;
8185 return align;
8188 /* Return the smallest alignment not less than SIZE. */
8190 static unsigned int
8191 ceil_alignment (unsigned HOST_WIDE_INT size)
8193 return (unsigned int) 1 << (floor_log2 (size - 1) + 1);
8196 /* Verify that OBJECT, a type or decl, is something we can implement
8197 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
8198 if we require atomic components. */
8200 static void
8201 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
8203 Node_Id gnat_error_point = gnat_entity;
8204 Node_Id gnat_node;
8205 enum machine_mode mode;
8206 unsigned int align;
8207 tree size;
8209 /* There are three case of what OBJECT can be. It can be a type, in which
8210 case we take the size, alignment and mode from the type. It can be a
8211 declaration that was indirect, in which case the relevant values are
8212 that of the type being pointed to, or it can be a normal declaration,
8213 in which case the values are of the decl. The code below assumes that
8214 OBJECT is either a type or a decl. */
8215 if (TYPE_P (object))
8217 /* If this is an anonymous base type, nothing to check. Error will be
8218 reported on the source type. */
8219 if (!Comes_From_Source (gnat_entity))
8220 return;
8222 mode = TYPE_MODE (object);
8223 align = TYPE_ALIGN (object);
8224 size = TYPE_SIZE (object);
8226 else if (DECL_BY_REF_P (object))
8228 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
8229 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
8230 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
8232 else
8234 mode = DECL_MODE (object);
8235 align = DECL_ALIGN (object);
8236 size = DECL_SIZE (object);
8239 /* Consider all floating-point types atomic and any types that that are
8240 represented by integers no wider than a machine word. */
8241 if (GET_MODE_CLASS (mode) == MODE_FLOAT
8242 || ((GET_MODE_CLASS (mode) == MODE_INT
8243 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
8244 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8245 return;
8247 /* For the moment, also allow anything that has an alignment equal
8248 to its size and which is smaller than a word. */
8249 if (size && TREE_CODE (size) == INTEGER_CST
8250 && compare_tree_int (size, align) == 0
8251 && align <= BITS_PER_WORD)
8252 return;
8254 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
8255 gnat_node = Next_Rep_Item (gnat_node))
8257 if (!comp_p && Nkind (gnat_node) == N_Pragma
8258 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8259 == Pragma_Atomic))
8260 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8261 else if (comp_p && Nkind (gnat_node) == N_Pragma
8262 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8263 == Pragma_Atomic_Components))
8264 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8267 if (comp_p)
8268 post_error_ne ("atomic access to component of & cannot be guaranteed",
8269 gnat_error_point, gnat_entity);
8270 else
8271 post_error_ne ("atomic access to & cannot be guaranteed",
8272 gnat_error_point, gnat_entity);
8276 /* Helper for the intrin compatibility checks family. Evaluate whether
8277 two types are definitely incompatible. */
8279 static bool
8280 intrin_types_incompatible_p (tree t1, tree t2)
8282 enum tree_code code;
8284 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8285 return false;
8287 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8288 return true;
8290 if (TREE_CODE (t1) != TREE_CODE (t2))
8291 return true;
8293 code = TREE_CODE (t1);
8295 switch (code)
8297 case INTEGER_TYPE:
8298 case REAL_TYPE:
8299 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8301 case POINTER_TYPE:
8302 case REFERENCE_TYPE:
8303 /* Assume designated types are ok. We'd need to account for char * and
8304 void * variants to do better, which could rapidly get messy and isn't
8305 clearly worth the effort. */
8306 return false;
8308 default:
8309 break;
8312 return false;
8315 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8316 on the Ada/builtin argument lists for the INB binding. */
8318 static bool
8319 intrin_arglists_compatible_p (intrin_binding_t * inb)
8321 tree ada_args = TYPE_ARG_TYPES (inb->ada_fntype);
8322 tree btin_args = TYPE_ARG_TYPES (inb->btin_fntype);
8324 /* Sequence position of the last argument we checked. */
8325 int argpos = 0;
8327 while (ada_args != 0 || btin_args != 0)
8329 tree ada_type, btin_type;
8331 /* If one list is shorter than the other, they fail to match. */
8332 if (ada_args == 0 || btin_args == 0)
8333 return false;
8335 ada_type = TREE_VALUE (ada_args);
8336 btin_type = TREE_VALUE (btin_args);
8338 /* If we're done with the Ada args and not with the internal builtin
8339 args, or the other way around, complain. */
8340 if (ada_type == void_type_node
8341 && btin_type != void_type_node)
8343 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8344 return false;
8347 if (btin_type == void_type_node
8348 && ada_type != void_type_node)
8350 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8351 inb->gnat_entity, inb->gnat_entity, argpos);
8352 return false;
8355 /* Otherwise, check that types match for the current argument. */
8356 argpos ++;
8357 if (intrin_types_incompatible_p (ada_type, btin_type))
8359 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8360 inb->gnat_entity, inb->gnat_entity, argpos);
8361 return false;
8364 ada_args = TREE_CHAIN (ada_args);
8365 btin_args = TREE_CHAIN (btin_args);
8368 return true;
8371 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8372 on the Ada/builtin return values for the INB binding. */
8374 static bool
8375 intrin_return_compatible_p (intrin_binding_t * inb)
8377 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8378 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8380 /* Accept function imported as procedure, common and convenient. */
8381 if (VOID_TYPE_P (ada_return_type)
8382 && !VOID_TYPE_P (btin_return_type))
8383 return true;
8385 /* Check return types compatibility otherwise. Note that this
8386 handles void/void as well. */
8387 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8389 post_error ("?intrinsic binding type mismatch on return value!",
8390 inb->gnat_entity);
8391 return false;
8394 return true;
8397 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8398 compatible. Issue relevant warnings when they are not.
8400 This is intended as a light check to diagnose the most obvious cases, not
8401 as a full fledged type compatibility predicate. It is the programmer's
8402 responsibility to ensure correctness of the Ada declarations in Imports,
8403 especially when binding straight to a compiler internal. */
8405 static bool
8406 intrin_profiles_compatible_p (intrin_binding_t * inb)
8408 /* Check compatibility on return values and argument lists, each responsible
8409 for posting warnings as appropriate. Ensure use of the proper sloc for
8410 this purpose. */
8412 bool arglists_compatible_p, return_compatible_p;
8413 location_t saved_location = input_location;
8415 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8417 return_compatible_p = intrin_return_compatible_p (inb);
8418 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8420 input_location = saved_location;
8422 return return_compatible_p && arglists_compatible_p;
8425 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8426 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8427 specified size for this field. POS_LIST is a position list describing
8428 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8429 to this layout. */
8431 static tree
8432 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8433 tree size, tree pos_list,
8434 VEC(subst_pair,heap) *subst_list)
8436 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8437 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8438 unsigned int offset_align = tree_low_cst (TREE_VEC_ELT (t, 1), 1);
8439 tree new_pos, new_field;
8440 unsigned ix;
8441 subst_pair *s;
8443 if (CONTAINS_PLACEHOLDER_P (pos))
8444 FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
8445 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8447 /* If the position is now a constant, we can set it as the position of the
8448 field when we make it. Otherwise, we need to deal with it specially. */
8449 if (TREE_CONSTANT (pos))
8450 new_pos = bit_from_pos (pos, bitpos);
8451 else
8452 new_pos = NULL_TREE;
8454 new_field
8455 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8456 size, new_pos, DECL_PACKED (old_field),
8457 !DECL_NONADDRESSABLE_P (old_field));
8459 if (!new_pos)
8461 normalize_offset (&pos, &bitpos, offset_align);
8462 DECL_FIELD_OFFSET (new_field) = pos;
8463 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8464 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8465 DECL_SIZE (new_field) = size;
8466 DECL_SIZE_UNIT (new_field)
8467 = convert (sizetype,
8468 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8469 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8472 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8473 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8474 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8475 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8477 return new_field;
8480 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8482 static tree
8483 get_rep_part (tree record_type)
8485 tree field = TYPE_FIELDS (record_type);
8487 /* The REP part is the first field, internal, another record, and its name
8488 doesn't start with an underscore (i.e. is not generated by the FE). */
8489 if (DECL_INTERNAL_P (field)
8490 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8491 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] != '_')
8492 return field;
8494 return NULL_TREE;
8497 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8499 static tree
8500 get_variant_part (tree record_type)
8502 tree field;
8504 /* The variant part is the only internal field that is a qualified union. */
8505 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8506 if (DECL_INTERNAL_P (field)
8507 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8508 return field;
8510 return NULL_TREE;
8513 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8514 the list of variants to be used and RECORD_TYPE is the type of the parent.
8515 POS_LIST is a position list describing the layout of fields present in
8516 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8517 layout. */
8519 static tree
8520 create_variant_part_from (tree old_variant_part,
8521 VEC(variant_desc,heap) *variant_list,
8522 tree record_type, tree pos_list,
8523 VEC(subst_pair,heap) *subst_list)
8525 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8526 tree old_union_type = TREE_TYPE (old_variant_part);
8527 tree new_union_type, new_variant_part;
8528 tree union_field_list = NULL_TREE;
8529 variant_desc *v;
8530 unsigned ix;
8532 /* First create the type of the variant part from that of the old one. */
8533 new_union_type = make_node (QUAL_UNION_TYPE);
8534 TYPE_NAME (new_union_type) = DECL_NAME (TYPE_NAME (old_union_type));
8536 /* If the position of the variant part is constant, subtract it from the
8537 size of the type of the parent to get the new size. This manual CSE
8538 reduces the code size when not optimizing. */
8539 if (TREE_CODE (offset) == INTEGER_CST)
8541 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8542 tree first_bit = bit_from_pos (offset, bitpos);
8543 TYPE_SIZE (new_union_type)
8544 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8545 TYPE_SIZE_UNIT (new_union_type)
8546 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8547 byte_from_pos (offset, bitpos));
8548 SET_TYPE_ADA_SIZE (new_union_type,
8549 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8550 first_bit));
8551 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8552 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8554 else
8555 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8557 /* Now finish up the new variants and populate the union type. */
8558 FOR_EACH_VEC_ELT_REVERSE (variant_desc, variant_list, ix, v)
8560 tree old_field = v->field, new_field;
8561 tree old_variant, old_variant_subpart, new_variant, field_list;
8563 /* Skip variants that don't belong to this nesting level. */
8564 if (DECL_CONTEXT (old_field) != old_union_type)
8565 continue;
8567 /* Retrieve the list of fields already added to the new variant. */
8568 new_variant = v->record;
8569 field_list = TYPE_FIELDS (new_variant);
8571 /* If the old variant had a variant subpart, we need to create a new
8572 variant subpart and add it to the field list. */
8573 old_variant = v->type;
8574 old_variant_subpart = get_variant_part (old_variant);
8575 if (old_variant_subpart)
8577 tree new_variant_subpart
8578 = create_variant_part_from (old_variant_subpart, variant_list,
8579 new_variant, pos_list, subst_list);
8580 DECL_CHAIN (new_variant_subpart) = field_list;
8581 field_list = new_variant_subpart;
8584 /* Finish up the new variant and create the field. No need for debug
8585 info thanks to the XVS type. */
8586 finish_record_type (new_variant, nreverse (field_list), 2, false);
8587 compute_record_mode (new_variant);
8588 create_type_decl (TYPE_NAME (new_variant), new_variant, NULL,
8589 true, false, Empty);
8591 new_field
8592 = create_field_decl_from (old_field, new_variant, new_union_type,
8593 TYPE_SIZE (new_variant),
8594 pos_list, subst_list);
8595 DECL_QUALIFIER (new_field) = v->qual;
8596 DECL_INTERNAL_P (new_field) = 1;
8597 DECL_CHAIN (new_field) = union_field_list;
8598 union_field_list = new_field;
8601 /* Finish up the union type and create the variant part. No need for debug
8602 info thanks to the XVS type. */
8603 finish_record_type (new_union_type, union_field_list, 2, false);
8604 compute_record_mode (new_union_type);
8605 create_type_decl (TYPE_NAME (new_union_type), new_union_type, NULL,
8606 true, false, Empty);
8608 new_variant_part
8609 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8610 TYPE_SIZE (new_union_type),
8611 pos_list, subst_list);
8612 DECL_INTERNAL_P (new_variant_part) = 1;
8614 /* With multiple discriminants it is possible for an inner variant to be
8615 statically selected while outer ones are not; in this case, the list
8616 of fields of the inner variant is not flattened and we end up with a
8617 qualified union with a single member. Drop the useless container. */
8618 if (!DECL_CHAIN (union_field_list))
8620 DECL_CONTEXT (union_field_list) = record_type;
8621 DECL_FIELD_OFFSET (union_field_list)
8622 = DECL_FIELD_OFFSET (new_variant_part);
8623 DECL_FIELD_BIT_OFFSET (union_field_list)
8624 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8625 SET_DECL_OFFSET_ALIGN (union_field_list,
8626 DECL_OFFSET_ALIGN (new_variant_part));
8627 new_variant_part = union_field_list;
8630 return new_variant_part;
8633 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8634 which are both RECORD_TYPE, after applying the substitutions described
8635 in SUBST_LIST. */
8637 static void
8638 copy_and_substitute_in_size (tree new_type, tree old_type,
8639 VEC(subst_pair,heap) *subst_list)
8641 unsigned ix;
8642 subst_pair *s;
8644 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8645 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8646 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8647 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8648 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8650 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8651 FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
8652 TYPE_SIZE (new_type)
8653 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8654 s->discriminant, s->replacement);
8656 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8657 FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
8658 TYPE_SIZE_UNIT (new_type)
8659 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8660 s->discriminant, s->replacement);
8662 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8663 FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
8664 SET_TYPE_ADA_SIZE
8665 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8666 s->discriminant, s->replacement));
8668 /* Finalize the size. */
8669 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8670 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8673 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8674 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8675 updated by replacing F with R.
8677 The function doesn't update the layout of the type, i.e. it assumes
8678 that the substitution is purely formal. That's why the replacement
8679 value R must itself contain a PLACEHOLDER_EXPR. */
8681 tree
8682 substitute_in_type (tree t, tree f, tree r)
8684 tree nt;
8686 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8688 switch (TREE_CODE (t))
8690 case INTEGER_TYPE:
8691 case ENUMERAL_TYPE:
8692 case BOOLEAN_TYPE:
8693 case REAL_TYPE:
8695 /* First the domain types of arrays. */
8696 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8697 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8699 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8700 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8702 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8703 return t;
8705 nt = copy_type (t);
8706 TYPE_GCC_MIN_VALUE (nt) = low;
8707 TYPE_GCC_MAX_VALUE (nt) = high;
8709 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8710 SET_TYPE_INDEX_TYPE
8711 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8713 return nt;
8716 /* Then the subtypes. */
8717 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8718 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8720 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8721 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8723 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8724 return t;
8726 nt = copy_type (t);
8727 SET_TYPE_RM_MIN_VALUE (nt, low);
8728 SET_TYPE_RM_MAX_VALUE (nt, high);
8730 return nt;
8733 return t;
8735 case COMPLEX_TYPE:
8736 nt = substitute_in_type (TREE_TYPE (t), f, r);
8737 if (nt == TREE_TYPE (t))
8738 return t;
8740 return build_complex_type (nt);
8742 case FUNCTION_TYPE:
8743 /* These should never show up here. */
8744 gcc_unreachable ();
8746 case ARRAY_TYPE:
8748 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8749 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8751 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8752 return t;
8754 nt = build_nonshared_array_type (component, domain);
8755 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8756 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8757 SET_TYPE_MODE (nt, TYPE_MODE (t));
8758 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8759 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8760 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8761 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8762 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8763 return nt;
8766 case RECORD_TYPE:
8767 case UNION_TYPE:
8768 case QUAL_UNION_TYPE:
8770 bool changed_field = false;
8771 tree field;
8773 /* Start out with no fields, make new fields, and chain them
8774 in. If we haven't actually changed the type of any field,
8775 discard everything we've done and return the old type. */
8776 nt = copy_type (t);
8777 TYPE_FIELDS (nt) = NULL_TREE;
8779 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8781 tree new_field = copy_node (field), new_n;
8783 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8784 if (new_n != TREE_TYPE (field))
8786 TREE_TYPE (new_field) = new_n;
8787 changed_field = true;
8790 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8791 if (new_n != DECL_FIELD_OFFSET (field))
8793 DECL_FIELD_OFFSET (new_field) = new_n;
8794 changed_field = true;
8797 /* Do the substitution inside the qualifier, if any. */
8798 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8800 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8801 if (new_n != DECL_QUALIFIER (field))
8803 DECL_QUALIFIER (new_field) = new_n;
8804 changed_field = true;
8808 DECL_CONTEXT (new_field) = nt;
8809 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8811 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8812 TYPE_FIELDS (nt) = new_field;
8815 if (!changed_field)
8816 return t;
8818 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8819 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8820 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8821 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8822 return nt;
8825 default:
8826 return t;
8830 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8831 needed to represent the object. */
8833 tree
8834 rm_size (tree gnu_type)
8836 /* For integral types, we store the RM size explicitly. */
8837 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8838 return TYPE_RM_SIZE (gnu_type);
8840 /* Return the RM size of the actual data plus the size of the template. */
8841 if (TREE_CODE (gnu_type) == RECORD_TYPE
8842 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8843 return
8844 size_binop (PLUS_EXPR,
8845 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8846 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8848 /* For record types, we store the size explicitly. */
8849 if ((TREE_CODE (gnu_type) == RECORD_TYPE
8850 || TREE_CODE (gnu_type) == UNION_TYPE
8851 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
8852 && !TYPE_FAT_POINTER_P (gnu_type)
8853 && TYPE_ADA_SIZE (gnu_type))
8854 return TYPE_ADA_SIZE (gnu_type);
8856 /* For other types, this is just the size. */
8857 return TYPE_SIZE (gnu_type);
8860 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8861 fully-qualified name, possibly with type information encoding.
8862 Otherwise, return the name. */
8864 tree
8865 get_entity_name (Entity_Id gnat_entity)
8867 Get_Encoded_Name (gnat_entity);
8868 return get_identifier_with_length (Name_Buffer, Name_Len);
8871 /* Return an identifier representing the external name to be used for
8872 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8873 and the specified suffix. */
8875 tree
8876 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8878 Entity_Kind kind = Ekind (gnat_entity);
8880 if (suffix)
8882 String_Template temp = {1, strlen (suffix)};
8883 Fat_Pointer fp = {suffix, &temp};
8884 Get_External_Name_With_Suffix (gnat_entity, fp);
8886 else
8887 Get_External_Name (gnat_entity, 0);
8889 /* A variable using the Stdcall convention lives in a DLL. We adjust
8890 its name to use the jump table, the _imp__NAME contains the address
8891 for the NAME variable. */
8892 if ((kind == E_Variable || kind == E_Constant)
8893 && Has_Stdcall_Convention (gnat_entity))
8895 const int len = 6 + Name_Len;
8896 char *new_name = (char *) alloca (len + 1);
8897 strcpy (new_name, "_imp__");
8898 strcat (new_name, Name_Buffer);
8899 return get_identifier_with_length (new_name, len);
8902 return get_identifier_with_length (Name_Buffer, Name_Len);
8905 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8906 string, return a new IDENTIFIER_NODE that is the concatenation of
8907 the name followed by "___" and the specified suffix. */
8909 tree
8910 concat_name (tree gnu_name, const char *suffix)
8912 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8913 char *new_name = (char *) alloca (len + 1);
8914 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8915 strcat (new_name, "___");
8916 strcat (new_name, suffix);
8917 return get_identifier_with_length (new_name, len);
8920 #include "gt-ada-decl.h"