* gcc-interface/decl.c (gnat_to_gnu_entity) <E_Subprogram_Type>: Add
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blob1719d1cae5adfb83cee12bc092b570faced44cc7
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2010, 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 Windows targets
54 only. The macro below is a helper to avoid having to check for a Windows
55 specific attribute throughout this unit. */
57 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
58 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
59 #else
60 #define Has_Stdcall_Convention(E) (0)
61 #endif
63 /* Stack realignment is necessary for functions with foreign conventions when
64 the ABI doesn't mandate as much as what the compiler assumes - that is, up
65 to PREFERRED_STACK_BOUNDARY.
67 Such realignment can be requested with a dedicated function type attribute
68 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
69 characterize the situations where the attribute should be set. We rely on
70 compiler configuration settings for 'main' to decide. */
72 #ifdef MAIN_STACK_BOUNDARY
73 #define FOREIGN_FORCE_REALIGN_STACK \
74 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
75 #else
76 #define FOREIGN_FORCE_REALIGN_STACK 0
77 #endif
79 struct incomplete
81 struct incomplete *next;
82 tree old_type;
83 Entity_Id full_type;
86 /* These variables are used to defer recursively expanding incomplete types
87 while we are processing an array, a record or a subprogram type. */
88 static int defer_incomplete_level = 0;
89 static struct incomplete *defer_incomplete_list;
91 /* This variable is used to delay expanding From_With_Type types until the
92 end of the spec. */
93 static struct incomplete *defer_limited_with;
95 /* These variables are used to defer finalizing types. The element of the
96 list is the TYPE_DECL associated with the type. */
97 static int defer_finalize_level = 0;
98 static VEC (tree,heap) *defer_finalize_list;
100 typedef struct GTY(()) subst_pair_d {
101 tree discriminant;
102 tree replacement;
103 } subst_pair;
105 DEF_VEC_O(subst_pair);
106 DEF_VEC_ALLOC_O(subst_pair,heap);
108 typedef struct GTY(()) variant_desc_d {
109 /* The type of the variant. */
110 tree type;
112 /* The associated field. */
113 tree field;
115 /* The value of the qualifier. */
116 tree qual;
118 /* The record associated with this variant. */
119 tree record;
120 } variant_desc;
122 DEF_VEC_O(variant_desc);
123 DEF_VEC_ALLOC_O(variant_desc,heap);
125 /* A hash table used to cache the result of annotate_value. */
126 static GTY ((if_marked ("tree_int_map_marked_p"),
127 param_is (struct tree_int_map))) htab_t annotate_value_cache;
129 enum alias_set_op
131 ALIAS_SET_COPY,
132 ALIAS_SET_SUBSET,
133 ALIAS_SET_SUPERSET
136 static void relate_alias_sets (tree, tree, enum alias_set_op);
138 static bool allocatable_size_p (tree, bool);
139 static void prepend_one_attribute_to (struct attrib **,
140 enum attr_type, tree, tree, Node_Id);
141 static void prepend_attributes (Entity_Id, struct attrib **);
142 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
143 static bool is_variable_size (tree);
144 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
145 static tree elaborate_expression_2 (tree, Entity_Id, tree, bool, bool,
146 unsigned int);
147 static tree make_packable_type (tree, bool);
148 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
149 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
150 bool *);
151 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
152 static bool same_discriminant_p (Entity_Id, Entity_Id);
153 static bool array_type_has_nonaliased_component (tree, Entity_Id);
154 static bool compile_time_known_address_p (Node_Id);
155 static bool cannot_be_superflat_p (Node_Id);
156 static bool constructor_address_p (tree);
157 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
158 bool, bool, bool, bool, bool);
159 static Uint annotate_value (tree);
160 static void annotate_rep (Entity_Id, tree);
161 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
162 static VEC(subst_pair,heap) *build_subst_list (Entity_Id, Entity_Id, bool);
163 static VEC(variant_desc,heap) *build_variant_list (tree,
164 VEC(subst_pair,heap) *,
165 VEC(variant_desc,heap) *);
166 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
167 static void set_rm_size (Uint, tree, Entity_Id);
168 static tree make_type_from_size (tree, tree, bool);
169 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
170 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
171 static void check_ok_for_atomic (tree, Entity_Id, bool);
172 static tree create_field_decl_from (tree, tree, tree, tree, tree,
173 VEC(subst_pair,heap) *);
174 static tree get_rep_part (tree);
175 static tree get_variant_part (tree);
176 static tree create_variant_part_from (tree, VEC(variant_desc,heap) *, tree,
177 tree, VEC(subst_pair,heap) *);
178 static void copy_and_substitute_in_size (tree, tree, VEC(subst_pair,heap) *);
179 static void rest_of_type_decl_compilation_no_defer (tree);
181 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
182 to pass around calls performing profile compatibilty checks. */
184 typedef struct {
185 Entity_Id gnat_entity; /* The Ada subprogram entity. */
186 tree ada_fntype; /* The corresponding GCC type node. */
187 tree btin_fntype; /* The GCC builtin function type node. */
188 } intrin_binding_t;
190 static bool intrin_profiles_compatible_p (intrin_binding_t *);
193 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
194 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
195 and associate the ..._DECL node with the input GNAT defining identifier.
197 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
198 initial value (in GCC tree form). This is optional for a variable. For
199 a renamed entity, GNU_EXPR gives the object being renamed.
201 DEFINITION is nonzero if this call is intended for a definition. This is
202 used for separate compilation where it is necessary to know whether an
203 external declaration or a definition must be created if the GCC equivalent
204 was not created previously. The value of 1 is normally used for a nonzero
205 DEFINITION, but a value of 2 is used in special circumstances, defined in
206 the code. */
208 tree
209 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
211 /* Contains the kind of the input GNAT node. */
212 const Entity_Kind kind = Ekind (gnat_entity);
213 /* True if this is a type. */
214 const bool is_type = IN (kind, Type_Kind);
215 /* True if debug info is requested for this entity. */
216 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
217 /* True if this entity is to be considered as imported. */
218 const bool imported_p
219 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
220 /* For a type, contains the equivalent GNAT node to be used in gigi. */
221 Entity_Id gnat_equiv_type = Empty;
222 /* Temporary used to walk the GNAT tree. */
223 Entity_Id gnat_temp;
224 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
225 This node will be associated with the GNAT node by calling at the end
226 of the `switch' statement. */
227 tree gnu_decl = NULL_TREE;
228 /* Contains the GCC type to be used for the GCC node. */
229 tree gnu_type = NULL_TREE;
230 /* Contains the GCC size tree to be used for the GCC node. */
231 tree gnu_size = NULL_TREE;
232 /* Contains the GCC name to be used for the GCC node. */
233 tree gnu_entity_name;
234 /* True if we have already saved gnu_decl as a GNAT association. */
235 bool saved = false;
236 /* True if we incremented defer_incomplete_level. */
237 bool this_deferred = false;
238 /* True if we incremented force_global. */
239 bool this_global = false;
240 /* True if we should check to see if elaborated during processing. */
241 bool maybe_present = false;
242 /* True if we made GNU_DECL and its type here. */
243 bool this_made_decl = false;
244 /* Size and alignment of the GCC node, if meaningful. */
245 unsigned int esize = 0, align = 0;
246 /* Contains the list of attributes directly attached to the entity. */
247 struct attrib *attr_list = NULL;
249 /* Since a use of an Itype is a definition, process it as such if it
250 is not in a with'ed unit. */
251 if (!definition
252 && is_type
253 && Is_Itype (gnat_entity)
254 && !present_gnu_tree (gnat_entity)
255 && In_Extended_Main_Code_Unit (gnat_entity))
257 /* Ensure that we are in a subprogram mentioned in the Scope chain of
258 this entity, our current scope is global, or we encountered a task
259 or entry (where we can't currently accurately check scoping). */
260 if (!current_function_decl
261 || DECL_ELABORATION_PROC_P (current_function_decl))
263 process_type (gnat_entity);
264 return get_gnu_tree (gnat_entity);
267 for (gnat_temp = Scope (gnat_entity);
268 Present (gnat_temp);
269 gnat_temp = Scope (gnat_temp))
271 if (Is_Type (gnat_temp))
272 gnat_temp = Underlying_Type (gnat_temp);
274 if (Ekind (gnat_temp) == E_Subprogram_Body)
275 gnat_temp
276 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
278 if (IN (Ekind (gnat_temp), Subprogram_Kind)
279 && Present (Protected_Body_Subprogram (gnat_temp)))
280 gnat_temp = Protected_Body_Subprogram (gnat_temp);
282 if (Ekind (gnat_temp) == E_Entry
283 || Ekind (gnat_temp) == E_Entry_Family
284 || Ekind (gnat_temp) == E_Task_Type
285 || (IN (Ekind (gnat_temp), Subprogram_Kind)
286 && present_gnu_tree (gnat_temp)
287 && (current_function_decl
288 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
290 process_type (gnat_entity);
291 return get_gnu_tree (gnat_entity);
295 /* This abort means the Itype has an incorrect scope, i.e. that its
296 scope does not correspond to the subprogram it is declared in. */
297 gcc_unreachable ();
300 /* If we've already processed this entity, return what we got last time.
301 If we are defining the node, we should not have already processed it.
302 In that case, we will abort below when we try to save a new GCC tree
303 for this object. We also need to handle the case of getting a dummy
304 type when a Full_View exists. */
305 if ((!definition || (is_type && imported_p))
306 && present_gnu_tree (gnat_entity))
308 gnu_decl = get_gnu_tree (gnat_entity);
310 if (TREE_CODE (gnu_decl) == TYPE_DECL
311 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
312 && IN (kind, Incomplete_Or_Private_Kind)
313 && Present (Full_View (gnat_entity)))
315 gnu_decl
316 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
317 save_gnu_tree (gnat_entity, NULL_TREE, false);
318 save_gnu_tree (gnat_entity, gnu_decl, false);
321 return gnu_decl;
324 /* If this is a numeric or enumeral type, or an access type, a nonzero
325 Esize must be specified unless it was specified by the programmer. */
326 gcc_assert (!Unknown_Esize (gnat_entity)
327 || Has_Size_Clause (gnat_entity)
328 || (!IN (kind, Numeric_Kind)
329 && !IN (kind, Enumeration_Kind)
330 && (!IN (kind, Access_Kind)
331 || kind == E_Access_Protected_Subprogram_Type
332 || kind == E_Anonymous_Access_Protected_Subprogram_Type
333 || kind == E_Access_Subtype)));
335 /* The RM size must be specified for all discrete and fixed-point types. */
336 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
337 && Unknown_RM_Size (gnat_entity)));
339 /* If we get here, it means we have not yet done anything with this entity.
340 If we are not defining it, it must be a type or an entity that is defined
341 elsewhere or externally, otherwise we should have defined it already. */
342 gcc_assert (definition
343 || type_annotate_only
344 || is_type
345 || kind == E_Discriminant
346 || kind == E_Component
347 || kind == E_Label
348 || (kind == E_Constant && Present (Full_View (gnat_entity)))
349 || Is_Public (gnat_entity));
351 /* Get the name of the entity and set up the line number and filename of
352 the original definition for use in any decl we make. */
353 gnu_entity_name = get_entity_name (gnat_entity);
354 Sloc_to_locus (Sloc (gnat_entity), &input_location);
356 /* For cases when we are not defining (i.e., we are referencing from
357 another compilation unit) public entities, show we are at global level
358 for the purpose of computing scopes. Don't do this for components or
359 discriminants since the relevant test is whether or not the record is
360 being defined. Don't do this for constants either as we'll look into
361 their defining expression in the local context. */
362 if (!definition
363 && kind != E_Component
364 && kind != E_Discriminant
365 && kind != E_Constant
366 && Is_Public (gnat_entity)
367 && !Is_Statically_Allocated (gnat_entity))
368 force_global++, this_global = true;
370 /* Handle any attributes directly attached to the entity. */
371 if (Has_Gigi_Rep_Item (gnat_entity))
372 prepend_attributes (gnat_entity, &attr_list);
374 /* Do some common processing for types. */
375 if (is_type)
377 /* Compute the equivalent type to be used in gigi. */
378 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
380 /* Machine_Attributes on types are expected to be propagated to
381 subtypes. The corresponding Gigi_Rep_Items are only attached
382 to the first subtype though, so we handle the propagation here. */
383 if (Base_Type (gnat_entity) != gnat_entity
384 && !Is_First_Subtype (gnat_entity)
385 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
386 prepend_attributes (First_Subtype (Base_Type (gnat_entity)),
387 &attr_list);
389 /* Compute a default value for the size of the type. */
390 if (Known_Esize (gnat_entity)
391 && UI_Is_In_Int_Range (Esize (gnat_entity)))
393 unsigned int max_esize;
394 esize = UI_To_Int (Esize (gnat_entity));
396 if (IN (kind, Float_Kind))
397 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
398 else if (IN (kind, Access_Kind))
399 max_esize = POINTER_SIZE * 2;
400 else
401 max_esize = LONG_LONG_TYPE_SIZE;
403 if (esize > max_esize)
404 esize = max_esize;
406 else
407 esize = LONG_LONG_TYPE_SIZE;
410 switch (kind)
412 case E_Constant:
413 /* If this is a use of a deferred constant without address clause,
414 get its full definition. */
415 if (!definition
416 && No (Address_Clause (gnat_entity))
417 && Present (Full_View (gnat_entity)))
419 gnu_decl
420 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
421 saved = true;
422 break;
425 /* If we have an external constant that we are not defining, get the
426 expression that is was defined to represent. We may throw it away
427 later if it is not a constant. But do not retrieve the expression
428 if it is an allocator because the designated type might be dummy
429 at this point. */
430 if (!definition
431 && !No_Initialization (Declaration_Node (gnat_entity))
432 && Present (Expression (Declaration_Node (gnat_entity)))
433 && Nkind (Expression (Declaration_Node (gnat_entity)))
434 != N_Allocator)
436 bool went_into_elab_proc = false;
438 /* The expression may contain N_Expression_With_Actions nodes and
439 thus object declarations from other units. In this case, even
440 though the expression will eventually be discarded since not a
441 constant, the declarations would be stuck either in the global
442 varpool or in the current scope. Therefore we force the local
443 context and create a fake scope that we'll zap at the end. */
444 if (!current_function_decl)
446 current_function_decl = get_elaboration_procedure ();
447 went_into_elab_proc = true;
449 gnat_pushlevel ();
451 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
453 gnat_zaplevel ();
454 if (went_into_elab_proc)
455 current_function_decl = NULL_TREE;
458 /* Ignore deferred constant definitions without address clause since
459 they are processed fully in the front-end. If No_Initialization
460 is set, this is not a deferred constant but a constant whose value
461 is built manually. And constants that are renamings are handled
462 like variables. */
463 if (definition
464 && !gnu_expr
465 && No (Address_Clause (gnat_entity))
466 && !No_Initialization (Declaration_Node (gnat_entity))
467 && No (Renamed_Object (gnat_entity)))
469 gnu_decl = error_mark_node;
470 saved = true;
471 break;
474 /* Ignore constant definitions already marked with the error node. See
475 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
476 if (definition
477 && gnu_expr
478 && present_gnu_tree (gnat_entity)
479 && get_gnu_tree (gnat_entity) == error_mark_node)
481 maybe_present = true;
482 break;
485 goto object;
487 case E_Exception:
488 /* We used to special case VMS exceptions here to directly map them to
489 their associated condition code. Since this code had to be masked
490 dynamically to strip off the severity bits, this caused trouble in
491 the GCC/ZCX case because the "type" pointers we store in the tables
492 have to be static. We now don't special case here anymore, and let
493 the regular processing take place, which leaves us with a regular
494 exception data object for VMS exceptions too. The condition code
495 mapping is taken care of by the front end and the bitmasking by the
496 run-time library. */
497 goto object;
499 case E_Discriminant:
500 case E_Component:
502 /* The GNAT record where the component was defined. */
503 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
505 /* If the variable is an inherited record component (in the case of
506 extended record types), just return the inherited entity, which
507 must be a FIELD_DECL. Likewise for discriminants.
508 For discriminants of untagged records which have explicit
509 stored discriminants, return the entity for the corresponding
510 stored discriminant. Also use Original_Record_Component
511 if the record has a private extension. */
512 if (Present (Original_Record_Component (gnat_entity))
513 && Original_Record_Component (gnat_entity) != gnat_entity)
515 gnu_decl
516 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
517 gnu_expr, definition);
518 saved = true;
519 break;
522 /* If the enclosing record has explicit stored discriminants,
523 then it is an untagged record. If the Corresponding_Discriminant
524 is not empty then this must be a renamed discriminant and its
525 Original_Record_Component must point to the corresponding explicit
526 stored discriminant (i.e. we should have taken the previous
527 branch). */
528 else if (Present (Corresponding_Discriminant (gnat_entity))
529 && Is_Tagged_Type (gnat_record))
531 /* A tagged record has no explicit stored discriminants. */
532 gcc_assert (First_Discriminant (gnat_record)
533 == First_Stored_Discriminant (gnat_record));
534 gnu_decl
535 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
536 gnu_expr, definition);
537 saved = true;
538 break;
541 else if (Present (CR_Discriminant (gnat_entity))
542 && type_annotate_only)
544 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
545 gnu_expr, definition);
546 saved = true;
547 break;
550 /* If the enclosing record has explicit stored discriminants, then
551 it is an untagged record. If the Corresponding_Discriminant
552 is not empty then this must be a renamed discriminant and its
553 Original_Record_Component must point to the corresponding explicit
554 stored discriminant (i.e. we should have taken the first
555 branch). */
556 else if (Present (Corresponding_Discriminant (gnat_entity))
557 && (First_Discriminant (gnat_record)
558 != First_Stored_Discriminant (gnat_record)))
559 gcc_unreachable ();
561 /* Otherwise, if we are not defining this and we have no GCC type
562 for the containing record, make one for it. Then we should
563 have made our own equivalent. */
564 else if (!definition && !present_gnu_tree (gnat_record))
566 /* ??? If this is in a record whose scope is a protected
567 type and we have an Original_Record_Component, use it.
568 This is a workaround for major problems in protected type
569 handling. */
570 Entity_Id Scop = Scope (Scope (gnat_entity));
571 if ((Is_Protected_Type (Scop)
572 || (Is_Private_Type (Scop)
573 && Present (Full_View (Scop))
574 && Is_Protected_Type (Full_View (Scop))))
575 && Present (Original_Record_Component (gnat_entity)))
577 gnu_decl
578 = gnat_to_gnu_entity (Original_Record_Component
579 (gnat_entity),
580 gnu_expr, 0);
581 saved = true;
582 break;
585 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
586 gnu_decl = get_gnu_tree (gnat_entity);
587 saved = true;
588 break;
591 else
592 /* Here we have no GCC type and this is a reference rather than a
593 definition. This should never happen. Most likely the cause is
594 reference before declaration in the gnat tree for gnat_entity. */
595 gcc_unreachable ();
598 case E_Loop_Parameter:
599 case E_Out_Parameter:
600 case E_Variable:
602 /* Simple variables, loop variables, Out parameters and exceptions. */
603 object:
605 bool const_flag
606 = ((kind == E_Constant || kind == E_Variable)
607 && Is_True_Constant (gnat_entity)
608 && !Treat_As_Volatile (gnat_entity)
609 && (((Nkind (Declaration_Node (gnat_entity))
610 == N_Object_Declaration)
611 && Present (Expression (Declaration_Node (gnat_entity))))
612 || Present (Renamed_Object (gnat_entity))
613 || imported_p));
614 bool inner_const_flag = const_flag;
615 bool static_p = Is_Statically_Allocated (gnat_entity);
616 bool mutable_p = false;
617 bool used_by_ref = false;
618 tree gnu_ext_name = NULL_TREE;
619 tree renamed_obj = NULL_TREE;
620 tree gnu_object_size;
622 if (Present (Renamed_Object (gnat_entity)) && !definition)
624 if (kind == E_Exception)
625 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
626 NULL_TREE, 0);
627 else
628 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
631 /* Get the type after elaborating the renamed object. */
632 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
634 /* If this is a standard exception definition, then use the standard
635 exception type. This is necessary to make sure that imported and
636 exported views of exceptions are properly merged in LTO mode. */
637 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
638 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
639 gnu_type = except_type_node;
641 /* For a debug renaming declaration, build a debug-only entity. */
642 if (Present (Debug_Renaming_Link (gnat_entity)))
644 /* Force a non-null value to make sure the symbol is retained. */
645 tree value = build1 (INDIRECT_REF, gnu_type,
646 build1 (NOP_EXPR,
647 build_pointer_type (gnu_type),
648 integer_minus_one_node));
649 gnu_decl = build_decl (input_location,
650 VAR_DECL, gnu_entity_name, gnu_type);
651 SET_DECL_VALUE_EXPR (gnu_decl, value);
652 DECL_HAS_VALUE_EXPR_P (gnu_decl) = 1;
653 gnat_pushdecl (gnu_decl, gnat_entity);
654 break;
657 /* If this is a loop variable, its type should be the base type.
658 This is because the code for processing a loop determines whether
659 a normal loop end test can be done by comparing the bounds of the
660 loop against those of the base type, which is presumed to be the
661 size used for computation. But this is not correct when the size
662 of the subtype is smaller than the type. */
663 if (kind == E_Loop_Parameter)
664 gnu_type = get_base_type (gnu_type);
666 /* Reject non-renamed objects whose type is an unconstrained array or
667 any object whose type is a dummy type or void. */
668 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
669 && No (Renamed_Object (gnat_entity)))
670 || TYPE_IS_DUMMY_P (gnu_type)
671 || TREE_CODE (gnu_type) == VOID_TYPE)
673 gcc_assert (type_annotate_only);
674 if (this_global)
675 force_global--;
676 return error_mark_node;
679 /* If an alignment is specified, use it if valid. Note that exceptions
680 are objects but don't have an alignment. We must do this before we
681 validate the size, since the alignment can affect the size. */
682 if (kind != E_Exception && Known_Alignment (gnat_entity))
684 gcc_assert (Present (Alignment (gnat_entity)));
685 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
686 TYPE_ALIGN (gnu_type));
688 /* No point in changing the type if there is an address clause
689 as the final type of the object will be a reference type. */
690 if (Present (Address_Clause (gnat_entity)))
691 align = 0;
692 else
693 gnu_type
694 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
695 false, false, definition, true);
698 /* If we are defining the object, see if it has a Size and validate it
699 if so. If we are not defining the object and a Size clause applies,
700 simply retrieve the value. We don't want to ignore the clause and
701 it is expected to have been validated already. Then get the new
702 type, if any. */
703 if (definition)
704 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
705 gnat_entity, VAR_DECL, false,
706 Has_Size_Clause (gnat_entity));
707 else if (Has_Size_Clause (gnat_entity))
708 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
710 if (gnu_size)
712 gnu_type
713 = make_type_from_size (gnu_type, gnu_size,
714 Has_Biased_Representation (gnat_entity));
716 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
717 gnu_size = NULL_TREE;
720 /* If this object has self-referential size, it must be a record with
721 a default discriminant. We are supposed to allocate an object of
722 the maximum size in this case, unless it is a constant with an
723 initializing expression, in which case we can get the size from
724 that. Note that the resulting size may still be a variable, so
725 this may end up with an indirect allocation. */
726 if (No (Renamed_Object (gnat_entity))
727 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
729 if (gnu_expr && kind == E_Constant)
731 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
732 if (CONTAINS_PLACEHOLDER_P (size))
734 /* If the initializing expression is itself a constant,
735 despite having a nominal type with self-referential
736 size, we can get the size directly from it. */
737 if (TREE_CODE (gnu_expr) == COMPONENT_REF
738 && TYPE_IS_PADDING_P
739 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
740 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
741 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
742 || DECL_READONLY_ONCE_ELAB
743 (TREE_OPERAND (gnu_expr, 0))))
744 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
745 else
746 gnu_size
747 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
749 else
750 gnu_size = size;
752 /* We may have no GNU_EXPR because No_Initialization is
753 set even though there's an Expression. */
754 else if (kind == E_Constant
755 && (Nkind (Declaration_Node (gnat_entity))
756 == N_Object_Declaration)
757 && Present (Expression (Declaration_Node (gnat_entity))))
758 gnu_size
759 = TYPE_SIZE (gnat_to_gnu_type
760 (Etype
761 (Expression (Declaration_Node (gnat_entity)))));
762 else
764 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
765 mutable_p = true;
769 /* If the size is zero byte, make it one byte since some linkers have
770 troubles with zero-sized objects. If the object will have a
771 template, that will make it nonzero so don't bother. Also avoid
772 doing that for an object renaming or an object with an address
773 clause, as we would lose useful information on the view size
774 (e.g. for null array slices) and we are not allocating the object
775 here anyway. */
776 if (((gnu_size
777 && integer_zerop (gnu_size)
778 && !TREE_OVERFLOW (gnu_size))
779 || (TYPE_SIZE (gnu_type)
780 && integer_zerop (TYPE_SIZE (gnu_type))
781 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
782 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
783 || !Is_Array_Type (Etype (gnat_entity)))
784 && No (Renamed_Object (gnat_entity))
785 && No (Address_Clause (gnat_entity)))
786 gnu_size = bitsize_unit_node;
788 /* If this is an object with no specified size and alignment, and
789 if either it is atomic or we are not optimizing alignment for
790 space and it is composite and not an exception, an Out parameter
791 or a reference to another object, and the size of its type is a
792 constant, set the alignment to the smallest one which is not
793 smaller than the size, with an appropriate cap. */
794 if (!gnu_size && align == 0
795 && (Is_Atomic (gnat_entity)
796 || (!Optimize_Alignment_Space (gnat_entity)
797 && kind != E_Exception
798 && kind != E_Out_Parameter
799 && Is_Composite_Type (Etype (gnat_entity))
800 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
801 && !Is_Exported (gnat_entity)
802 && !imported_p
803 && No (Renamed_Object (gnat_entity))
804 && No (Address_Clause (gnat_entity))))
805 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
807 /* No point in jumping through all the hoops needed in order
808 to support BIGGEST_ALIGNMENT if we don't really have to.
809 So we cap to the smallest alignment that corresponds to
810 a known efficient memory access pattern of the target. */
811 unsigned int align_cap = Is_Atomic (gnat_entity)
812 ? BIGGEST_ALIGNMENT
813 : get_mode_alignment (ptr_mode);
815 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
816 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
817 align = align_cap;
818 else
819 align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
821 /* But make sure not to under-align the object. */
822 if (align <= TYPE_ALIGN (gnu_type))
823 align = 0;
825 /* And honor the minimum valid atomic alignment, if any. */
826 #ifdef MINIMUM_ATOMIC_ALIGNMENT
827 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
828 align = MINIMUM_ATOMIC_ALIGNMENT;
829 #endif
832 /* If the object is set to have atomic components, find the component
833 type and validate it.
835 ??? Note that we ignore Has_Volatile_Components on objects; it's
836 not at all clear what to do in that case. */
837 if (Has_Atomic_Components (gnat_entity))
839 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
840 ? TREE_TYPE (gnu_type) : gnu_type);
842 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
843 && TYPE_MULTI_ARRAY_P (gnu_inner))
844 gnu_inner = TREE_TYPE (gnu_inner);
846 check_ok_for_atomic (gnu_inner, gnat_entity, true);
849 /* Now check if the type of the object allows atomic access. Note
850 that we must test the type, even if this object has size and
851 alignment to allow such access, because we will be going inside
852 the padded record to assign to the object. We could fix this by
853 always copying via an intermediate value, but it's not clear it's
854 worth the effort. */
855 if (Is_Atomic (gnat_entity))
856 check_ok_for_atomic (gnu_type, gnat_entity, false);
858 /* If this is an aliased object with an unconstrained nominal subtype,
859 make a type that includes the template. */
860 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
861 && Is_Array_Type (Etype (gnat_entity))
862 && !type_annotate_only)
864 tree gnu_fat
865 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
867 gnu_type
868 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
869 concat_name (gnu_entity_name,
870 "UNC"),
871 debug_info_p);
874 #ifdef MINIMUM_ATOMIC_ALIGNMENT
875 /* If the size is a constant and no alignment is specified, force
876 the alignment to be the minimum valid atomic alignment. The
877 restriction on constant size avoids problems with variable-size
878 temporaries; if the size is variable, there's no issue with
879 atomic access. Also don't do this for a constant, since it isn't
880 necessary and can interfere with constant replacement. Finally,
881 do not do it for Out parameters since that creates an
882 size inconsistency with In parameters. */
883 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
884 && !FLOAT_TYPE_P (gnu_type)
885 && !const_flag && No (Renamed_Object (gnat_entity))
886 && !imported_p && No (Address_Clause (gnat_entity))
887 && kind != E_Out_Parameter
888 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
889 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
890 align = MINIMUM_ATOMIC_ALIGNMENT;
891 #endif
893 /* Make a new type with the desired size and alignment, if needed.
894 But do not take into account alignment promotions to compute the
895 size of the object. */
896 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
897 if (gnu_size || align > 0)
898 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
899 false, false, definition,
900 gnu_size ? true : false);
902 /* If this is a renaming, avoid as much as possible to create a new
903 object. However, in several cases, creating it is required.
904 This processing needs to be applied to the raw expression so
905 as to make it more likely to rename the underlying object. */
906 if (Present (Renamed_Object (gnat_entity)))
908 bool create_normal_object = false;
910 /* If the renamed object had padding, strip off the reference
911 to the inner object and reset our type. */
912 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
913 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
914 /* Strip useless conversions around the object. */
915 || (TREE_CODE (gnu_expr) == NOP_EXPR
916 && gnat_types_compatible_p
917 (TREE_TYPE (gnu_expr),
918 TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
920 gnu_expr = TREE_OPERAND (gnu_expr, 0);
921 gnu_type = TREE_TYPE (gnu_expr);
924 /* Case 1: If this is a constant renaming stemming from a function
925 call, treat it as a normal object whose initial value is what
926 is being renamed. RM 3.3 says that the result of evaluating a
927 function call is a constant object. As a consequence, it can
928 be the inner object of a constant renaming. In this case, the
929 renaming must be fully instantiated, i.e. it cannot be a mere
930 reference to (part of) an existing object. */
931 if (const_flag)
933 tree inner_object = gnu_expr;
934 while (handled_component_p (inner_object))
935 inner_object = TREE_OPERAND (inner_object, 0);
936 if (TREE_CODE (inner_object) == CALL_EXPR)
937 create_normal_object = true;
940 /* Otherwise, see if we can proceed with a stabilized version of
941 the renamed entity or if we need to make a new object. */
942 if (!create_normal_object)
944 tree maybe_stable_expr = NULL_TREE;
945 bool stable = false;
947 /* Case 2: If the renaming entity need not be materialized and
948 the renamed expression is something we can stabilize, use
949 that for the renaming. At the global level, we can only do
950 this if we know no SAVE_EXPRs need be made, because the
951 expression we return might be used in arbitrary conditional
952 branches so we must force the evaluation of the SAVE_EXPRs
953 immediately and this requires a proper function context.
954 Note that an external constant is at the global level. */
955 if (!Materialize_Entity (gnat_entity)
956 && (!((!definition && kind == E_Constant)
957 || global_bindings_p ())
958 || (staticp (gnu_expr)
959 && !TREE_SIDE_EFFECTS (gnu_expr))))
961 maybe_stable_expr
962 = gnat_stabilize_reference (gnu_expr, true, &stable);
964 if (stable)
966 /* ??? No DECL_EXPR is created so we need to mark
967 the expression manually lest it is shared. */
968 if ((!definition && kind == E_Constant)
969 || global_bindings_p ())
970 MARK_VISITED (maybe_stable_expr);
971 gnu_decl = maybe_stable_expr;
972 save_gnu_tree (gnat_entity, gnu_decl, true);
973 saved = true;
974 annotate_object (gnat_entity, gnu_type, NULL_TREE,
975 false, false);
976 break;
979 /* The stabilization failed. Keep maybe_stable_expr
980 untouched here to let the pointer case below know
981 about that failure. */
984 /* Case 3: If this is a constant renaming and creating a
985 new object is allowed and cheap, treat it as a normal
986 object whose initial value is what is being renamed. */
987 if (const_flag
988 && !Is_Composite_Type
989 (Underlying_Type (Etype (gnat_entity))))
992 /* Case 4: Make this into a constant pointer to the object we
993 are to rename and attach the object to the pointer if it is
994 something we can stabilize.
996 From the proper scope, attached objects will be referenced
997 directly instead of indirectly via the pointer to avoid
998 subtle aliasing problems with non-addressable entities.
999 They have to be stable because we must not evaluate the
1000 variables in the expression every time the renaming is used.
1001 The pointer is called a "renaming" pointer in this case.
1003 In the rare cases where we cannot stabilize the renamed
1004 object, we just make a "bare" pointer, and the renamed
1005 entity is always accessed indirectly through it. */
1006 else
1008 gnu_type = build_reference_type (gnu_type);
1009 inner_const_flag = TREE_READONLY (gnu_expr);
1010 const_flag = true;
1012 /* If the previous attempt at stabilizing failed, there
1013 is no point in trying again and we reuse the result
1014 without attaching it to the pointer. In this case it
1015 will only be used as the initializing expression of
1016 the pointer and thus needs no special treatment with
1017 regard to multiple evaluations. */
1018 if (maybe_stable_expr)
1021 /* Otherwise, try to stabilize and attach the expression
1022 to the pointer if the stabilization succeeds.
1024 Note that this might introduce SAVE_EXPRs and we don't
1025 check whether we're at the global level or not. This
1026 is fine since we are building a pointer initializer and
1027 neither the pointer nor the initializing expression can
1028 be accessed before the pointer elaboration has taken
1029 place in a correct program.
1031 These SAVE_EXPRs will be evaluated at the right place
1032 by either the evaluation of the initializer for the
1033 non-global case or the elaboration code for the global
1034 case, and will be attached to the elaboration procedure
1035 in the latter case. */
1036 else
1038 maybe_stable_expr
1039 = gnat_stabilize_reference (gnu_expr, true, &stable);
1041 if (stable)
1042 renamed_obj = maybe_stable_expr;
1044 /* Attaching is actually performed downstream, as soon
1045 as we have a VAR_DECL for the pointer we make. */
1048 gnu_expr = build_unary_op (ADDR_EXPR, gnu_type,
1049 maybe_stable_expr);
1051 gnu_size = NULL_TREE;
1052 used_by_ref = true;
1057 /* Make a volatile version of this object's type if we are to make
1058 the object volatile. We also interpret 13.3(19) conservatively
1059 and disallow any optimizations for such a non-constant object. */
1060 if ((Treat_As_Volatile (gnat_entity)
1061 || (!const_flag
1062 && gnu_type != except_type_node
1063 && (Is_Exported (gnat_entity)
1064 || imported_p
1065 || Present (Address_Clause (gnat_entity)))))
1066 && !TYPE_VOLATILE (gnu_type))
1067 gnu_type = build_qualified_type (gnu_type,
1068 (TYPE_QUALS (gnu_type)
1069 | TYPE_QUAL_VOLATILE));
1071 /* If we are defining an aliased object whose nominal subtype is
1072 unconstrained, the object is a record that contains both the
1073 template and the object. If there is an initializer, it will
1074 have already been converted to the right type, but we need to
1075 create the template if there is no initializer. */
1076 if (definition
1077 && !gnu_expr
1078 && TREE_CODE (gnu_type) == RECORD_TYPE
1079 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1080 /* Beware that padding might have been introduced above. */
1081 || (TYPE_PADDING_P (gnu_type)
1082 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1083 == RECORD_TYPE
1084 && TYPE_CONTAINS_TEMPLATE_P
1085 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1087 tree template_field
1088 = TYPE_PADDING_P (gnu_type)
1089 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1090 : TYPE_FIELDS (gnu_type);
1091 VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, 1);
1092 tree t = build_template (TREE_TYPE (template_field),
1093 TREE_TYPE (DECL_CHAIN (template_field)),
1094 NULL_TREE);
1095 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1096 gnu_expr = gnat_build_constructor (gnu_type, v);
1099 /* Convert the expression to the type of the object except in the
1100 case where the object's type is unconstrained or the object's type
1101 is a padded record whose field is of self-referential size. In
1102 the former case, converting will generate unnecessary evaluations
1103 of the CONSTRUCTOR to compute the size and in the latter case, we
1104 want to only copy the actual data. */
1105 if (gnu_expr
1106 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1107 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1108 && !(TYPE_IS_PADDING_P (gnu_type)
1109 && CONTAINS_PLACEHOLDER_P
1110 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1111 gnu_expr = convert (gnu_type, gnu_expr);
1113 /* If this is a pointer that doesn't have an initializing expression,
1114 initialize it to NULL, unless the object is imported. */
1115 if (definition
1116 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1117 && !gnu_expr
1118 && !Is_Imported (gnat_entity))
1119 gnu_expr = integer_zero_node;
1121 /* If we are defining the object and it has an Address clause, we must
1122 either get the address expression from the saved GCC tree for the
1123 object if it has a Freeze node, or elaborate the address expression
1124 here since the front-end has guaranteed that the elaboration has no
1125 effects in this case. */
1126 if (definition && Present (Address_Clause (gnat_entity)))
1128 Node_Id gnat_expr = Expression (Address_Clause (gnat_entity));
1129 tree gnu_address
1130 = present_gnu_tree (gnat_entity)
1131 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1133 save_gnu_tree (gnat_entity, NULL_TREE, false);
1135 /* Ignore the size. It's either meaningless or was handled
1136 above. */
1137 gnu_size = NULL_TREE;
1138 /* Convert the type of the object to a reference type that can
1139 alias everything as per 13.3(19). */
1140 gnu_type
1141 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1142 gnu_address = convert (gnu_type, gnu_address);
1143 used_by_ref = true;
1144 const_flag
1145 = !Is_Public (gnat_entity)
1146 || compile_time_known_address_p (gnat_expr);
1148 /* If this is a deferred constant, the initializer is attached to
1149 the full view. */
1150 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1151 gnu_expr
1152 = gnat_to_gnu
1153 (Expression (Declaration_Node (Full_View (gnat_entity))));
1155 /* If we don't have an initializing expression for the underlying
1156 variable, the initializing expression for the pointer is the
1157 specified address. Otherwise, we have to make a COMPOUND_EXPR
1158 to assign both the address and the initial value. */
1159 if (!gnu_expr)
1160 gnu_expr = gnu_address;
1161 else
1162 gnu_expr
1163 = build2 (COMPOUND_EXPR, gnu_type,
1164 build_binary_op
1165 (MODIFY_EXPR, NULL_TREE,
1166 build_unary_op (INDIRECT_REF, NULL_TREE,
1167 gnu_address),
1168 gnu_expr),
1169 gnu_address);
1172 /* If it has an address clause and we are not defining it, mark it
1173 as an indirect object. Likewise for Stdcall objects that are
1174 imported. */
1175 if ((!definition && Present (Address_Clause (gnat_entity)))
1176 || (Is_Imported (gnat_entity)
1177 && Has_Stdcall_Convention (gnat_entity)))
1179 /* Convert the type of the object to a reference type that can
1180 alias everything as per 13.3(19). */
1181 gnu_type
1182 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1183 gnu_size = NULL_TREE;
1185 /* No point in taking the address of an initializing expression
1186 that isn't going to be used. */
1187 gnu_expr = NULL_TREE;
1189 /* If it has an address clause whose value is known at compile
1190 time, make the object a CONST_DECL. This will avoid a
1191 useless dereference. */
1192 if (Present (Address_Clause (gnat_entity)))
1194 Node_Id gnat_address
1195 = Expression (Address_Clause (gnat_entity));
1197 if (compile_time_known_address_p (gnat_address))
1199 gnu_expr = gnat_to_gnu (gnat_address);
1200 const_flag = true;
1204 used_by_ref = true;
1207 /* If we are at top level and this object is of variable size,
1208 make the actual type a hidden pointer to the real type and
1209 make the initializer be a memory allocation and initialization.
1210 Likewise for objects we aren't defining (presumed to be
1211 external references from other packages), but there we do
1212 not set up an initialization.
1214 If the object's size overflows, make an allocator too, so that
1215 Storage_Error gets raised. Note that we will never free
1216 such memory, so we presume it never will get allocated. */
1217 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1218 global_bindings_p ()
1219 || !definition
1220 || static_p)
1221 || (gnu_size && !allocatable_size_p (gnu_size,
1222 global_bindings_p ()
1223 || !definition
1224 || static_p)))
1226 gnu_type = build_reference_type (gnu_type);
1227 gnu_size = NULL_TREE;
1228 used_by_ref = true;
1230 /* In case this was a aliased object whose nominal subtype is
1231 unconstrained, the pointer above will be a thin pointer and
1232 build_allocator will automatically make the template.
1234 If we have a template initializer only (that we made above),
1235 pretend there is none and rely on what build_allocator creates
1236 again anyway. Otherwise (if we have a full initializer), get
1237 the data part and feed that to build_allocator.
1239 If we are elaborating a mutable object, tell build_allocator to
1240 ignore a possibly simpler size from the initializer, if any, as
1241 we must allocate the maximum possible size in this case. */
1242 if (definition && !imported_p)
1244 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1246 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1247 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1249 gnu_alloc_type
1250 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1252 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1253 && 1 == VEC_length (constructor_elt,
1254 CONSTRUCTOR_ELTS (gnu_expr)))
1255 gnu_expr = 0;
1256 else
1257 gnu_expr
1258 = build_component_ref
1259 (gnu_expr, NULL_TREE,
1260 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1261 false);
1264 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1265 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type)))
1266 post_error ("?`Storage_Error` will be raised at run time!",
1267 gnat_entity);
1269 gnu_expr
1270 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1271 Empty, Empty, gnat_entity, mutable_p);
1272 const_flag = true;
1274 else
1276 gnu_expr = NULL_TREE;
1277 const_flag = false;
1281 /* If this object would go into the stack and has an alignment larger
1282 than the largest stack alignment the back-end can honor, resort to
1283 a variable of "aligning type". */
1284 if (!global_bindings_p () && !static_p && definition
1285 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1287 /* Create the new variable. No need for extra room before the
1288 aligned field as this is in automatic storage. */
1289 tree gnu_new_type
1290 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1291 TYPE_SIZE_UNIT (gnu_type),
1292 BIGGEST_ALIGNMENT, 0);
1293 tree gnu_new_var
1294 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1295 NULL_TREE, gnu_new_type, NULL_TREE, false,
1296 false, false, false, NULL, gnat_entity);
1298 /* Initialize the aligned field if we have an initializer. */
1299 if (gnu_expr)
1300 add_stmt_with_node
1301 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1302 build_component_ref
1303 (gnu_new_var, NULL_TREE,
1304 TYPE_FIELDS (gnu_new_type), false),
1305 gnu_expr),
1306 gnat_entity);
1308 /* And setup this entity as a reference to the aligned field. */
1309 gnu_type = build_reference_type (gnu_type);
1310 gnu_expr
1311 = build_unary_op
1312 (ADDR_EXPR, gnu_type,
1313 build_component_ref (gnu_new_var, NULL_TREE,
1314 TYPE_FIELDS (gnu_new_type), false));
1316 gnu_size = NULL_TREE;
1317 used_by_ref = true;
1318 const_flag = true;
1321 if (const_flag)
1322 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1323 | TYPE_QUAL_CONST));
1325 /* Convert the expression to the type of the object except in the
1326 case where the object's type is unconstrained or the object's type
1327 is a padded record whose field is of self-referential size. In
1328 the former case, converting will generate unnecessary evaluations
1329 of the CONSTRUCTOR to compute the size and in the latter case, we
1330 want to only copy the actual data. */
1331 if (gnu_expr
1332 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1333 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1334 && !(TYPE_IS_PADDING_P (gnu_type)
1335 && CONTAINS_PLACEHOLDER_P
1336 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1337 gnu_expr = convert (gnu_type, gnu_expr);
1339 /* If this name is external or there was a name specified, use it,
1340 unless this is a VMS exception object since this would conflict
1341 with the symbol we need to export in addition. Don't use the
1342 Interface_Name if there is an address clause (see CD30005). */
1343 if (!Is_VMS_Exception (gnat_entity)
1344 && ((Present (Interface_Name (gnat_entity))
1345 && No (Address_Clause (gnat_entity)))
1346 || (Is_Public (gnat_entity)
1347 && (!Is_Imported (gnat_entity)
1348 || Is_Exported (gnat_entity)))))
1349 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1351 /* If this is an aggregate constant initialized to a constant, force it
1352 to be statically allocated. This saves an initialization copy. */
1353 if (!static_p
1354 && const_flag
1355 && gnu_expr && TREE_CONSTANT (gnu_expr)
1356 && AGGREGATE_TYPE_P (gnu_type)
1357 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1358 && !(TYPE_IS_PADDING_P (gnu_type)
1359 && !host_integerp (TYPE_SIZE_UNIT
1360 (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
1361 static_p = true;
1363 /* Now create the variable or the constant and set various flags. */
1364 gnu_decl
1365 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1366 gnu_expr, const_flag, Is_Public (gnat_entity),
1367 imported_p || !definition, static_p, attr_list,
1368 gnat_entity);
1369 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1370 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1372 /* If we are defining an Out parameter and optimization isn't enabled,
1373 create a fake PARM_DECL for debugging purposes and make it point to
1374 the VAR_DECL. Suppress debug info for the latter but make sure it
1375 will live on the stack so that it can be accessed from within the
1376 debugger through the PARM_DECL. */
1377 if (kind == E_Out_Parameter && definition && !optimize && debug_info_p)
1379 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1380 gnat_pushdecl (param, gnat_entity);
1381 SET_DECL_VALUE_EXPR (param, gnu_decl);
1382 DECL_HAS_VALUE_EXPR_P (param) = 1;
1383 DECL_IGNORED_P (gnu_decl) = 1;
1384 TREE_ADDRESSABLE (gnu_decl) = 1;
1387 /* If this is a renaming pointer, attach the renamed object to it and
1388 register it if we are at the global level. Note that an external
1389 constant is at the global level. */
1390 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1392 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1393 if ((!definition && kind == E_Constant) || global_bindings_p ())
1395 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1396 record_global_renaming_pointer (gnu_decl);
1400 /* If this is a constant and we are defining it or it generates a real
1401 symbol at the object level and we are referencing it, we may want
1402 or need to have a true variable to represent it:
1403 - if optimization isn't enabled, for debugging purposes,
1404 - if the constant is public and not overlaid on something else,
1405 - if its address is taken,
1406 - if either itself or its type is aliased. */
1407 if (TREE_CODE (gnu_decl) == CONST_DECL
1408 && (definition || Sloc (gnat_entity) > Standard_Location)
1409 && ((!optimize && debug_info_p)
1410 || (Is_Public (gnat_entity)
1411 && No (Address_Clause (gnat_entity)))
1412 || Address_Taken (gnat_entity)
1413 || Is_Aliased (gnat_entity)
1414 || Is_Aliased (Etype (gnat_entity))))
1416 tree gnu_corr_var
1417 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1418 gnu_expr, true, Is_Public (gnat_entity),
1419 !definition, static_p, attr_list,
1420 gnat_entity);
1422 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1424 /* As debugging information will be generated for the variable,
1425 do not generate debugging information for the constant. */
1426 if (debug_info_p)
1427 DECL_IGNORED_P (gnu_decl) = 1;
1428 else
1429 DECL_IGNORED_P (gnu_corr_var) = 1;
1432 /* If this is a constant, even if we don't need a true variable, we
1433 may need to avoid returning the initializer in every case. That
1434 can happen for the address of a (constant) constructor because,
1435 upon dereferencing it, the constructor will be reinjected in the
1436 tree, which may not be valid in every case; see lvalue_required_p
1437 for more details. */
1438 if (TREE_CODE (gnu_decl) == CONST_DECL)
1439 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1441 /* If this object is declared in a block that contains a block with an
1442 exception handler, and we aren't using the GCC exception mechanism,
1443 we must force this variable in memory in order to avoid an invalid
1444 optimization. */
1445 if (Exception_Mechanism != Back_End_Exceptions
1446 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1447 TREE_ADDRESSABLE (gnu_decl) = 1;
1449 /* If we are defining an object with variable size or an object with
1450 fixed size that will be dynamically allocated, and we are using the
1451 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1452 if (definition
1453 && Exception_Mechanism == Setjmp_Longjmp
1454 && get_block_jmpbuf_decl ()
1455 && DECL_SIZE_UNIT (gnu_decl)
1456 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1457 || (flag_stack_check == GENERIC_STACK_CHECK
1458 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1459 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1460 add_stmt_with_node (build_call_1_expr
1461 (update_setjmp_buf_decl,
1462 build_unary_op (ADDR_EXPR, NULL_TREE,
1463 get_block_jmpbuf_decl ())),
1464 gnat_entity);
1466 /* Back-annotate Esize and Alignment of the object if not already
1467 known. Note that we pick the values of the type, not those of
1468 the object, to shield ourselves from low-level platform-dependent
1469 adjustments like alignment promotion. This is both consistent with
1470 all the treatment above, where alignment and size are set on the
1471 type of the object and not on the object directly, and makes it
1472 possible to support all confirming representation clauses. */
1473 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1474 used_by_ref, false);
1476 break;
1478 case E_Void:
1479 /* Return a TYPE_DECL for "void" that we previously made. */
1480 gnu_decl = TYPE_NAME (void_type_node);
1481 break;
1483 case E_Enumeration_Type:
1484 /* A special case: for the types Character and Wide_Character in
1485 Standard, we do not list all the literals. So if the literals
1486 are not specified, make this an unsigned type. */
1487 if (No (First_Literal (gnat_entity)))
1489 gnu_type = make_unsigned_type (esize);
1490 TYPE_NAME (gnu_type) = gnu_entity_name;
1492 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1493 This is needed by the DWARF-2 back-end to distinguish between
1494 unsigned integer types and character types. */
1495 TYPE_STRING_FLAG (gnu_type) = 1;
1496 break;
1500 /* We have a list of enumeral constants in First_Literal. We make a
1501 CONST_DECL for each one and build into GNU_LITERAL_LIST the list to
1502 be placed into TYPE_FIELDS. Each node in the list is a TREE_LIST
1503 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1504 value of the literal. But when we have a regular boolean type, we
1505 simplify this a little by using a BOOLEAN_TYPE. */
1506 bool is_boolean = Is_Boolean_Type (gnat_entity)
1507 && !Has_Non_Standard_Rep (gnat_entity);
1508 tree gnu_literal_list = NULL_TREE;
1509 Entity_Id gnat_literal;
1511 if (Is_Unsigned_Type (gnat_entity))
1512 gnu_type = make_unsigned_type (esize);
1513 else
1514 gnu_type = make_signed_type (esize);
1516 TREE_SET_CODE (gnu_type, is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1518 for (gnat_literal = First_Literal (gnat_entity);
1519 Present (gnat_literal);
1520 gnat_literal = Next_Literal (gnat_literal))
1522 tree gnu_value
1523 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1524 tree gnu_literal
1525 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1526 gnu_type, gnu_value, true, false, false,
1527 false, NULL, gnat_literal);
1528 /* Do not generate debug info for individual enumerators. */
1529 DECL_IGNORED_P (gnu_literal) = 1;
1530 save_gnu_tree (gnat_literal, gnu_literal, false);
1531 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1532 gnu_value, gnu_literal_list);
1535 if (!is_boolean)
1536 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1538 /* Note that the bounds are updated at the end of this function
1539 to avoid an infinite recursion since they refer to the type. */
1541 goto discrete_type;
1543 case E_Signed_Integer_Type:
1544 case E_Ordinary_Fixed_Point_Type:
1545 case E_Decimal_Fixed_Point_Type:
1546 /* For integer types, just make a signed type the appropriate number
1547 of bits. */
1548 gnu_type = make_signed_type (esize);
1549 goto discrete_type;
1551 case E_Modular_Integer_Type:
1553 /* For modular types, make the unsigned type of the proper number
1554 of bits and then set up the modulus, if required. */
1555 tree gnu_modulus, gnu_high = NULL_TREE;
1557 /* Packed array types are supposed to be subtypes only. */
1558 gcc_assert (!Is_Packed_Array_Type (gnat_entity));
1560 gnu_type = make_unsigned_type (esize);
1562 /* Get the modulus in this type. If it overflows, assume it is because
1563 it is equal to 2**Esize. Note that there is no overflow checking
1564 done on unsigned type, so we detect the overflow by looking for
1565 a modulus of zero, which is otherwise invalid. */
1566 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1568 if (!integer_zerop (gnu_modulus))
1570 TYPE_MODULAR_P (gnu_type) = 1;
1571 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1572 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1573 convert (gnu_type, integer_one_node));
1576 /* If the upper bound is not maximal, make an extra subtype. */
1577 if (gnu_high
1578 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1580 tree gnu_subtype = make_unsigned_type (esize);
1581 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1582 TREE_TYPE (gnu_subtype) = gnu_type;
1583 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1584 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1585 gnu_type = gnu_subtype;
1588 goto discrete_type;
1590 case E_Signed_Integer_Subtype:
1591 case E_Enumeration_Subtype:
1592 case E_Modular_Integer_Subtype:
1593 case E_Ordinary_Fixed_Point_Subtype:
1594 case E_Decimal_Fixed_Point_Subtype:
1596 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1597 not want to call create_range_type since we would like each subtype
1598 node to be distinct. ??? Historically this was in preparation for
1599 when memory aliasing is implemented, but that's obsolete now given
1600 the call to relate_alias_sets below.
1602 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1603 this fact is used by the arithmetic conversion functions.
1605 We elaborate the Ancestor_Subtype if it is not in the current unit
1606 and one of our bounds is non-static. We do this to ensure consistent
1607 naming in the case where several subtypes share the same bounds, by
1608 elaborating the first such subtype first, thus using its name. */
1610 if (!definition
1611 && Present (Ancestor_Subtype (gnat_entity))
1612 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1613 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1614 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1615 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1617 /* Set the precision to the Esize except for bit-packed arrays. */
1618 if (Is_Packed_Array_Type (gnat_entity)
1619 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1620 esize = UI_To_Int (RM_Size (gnat_entity));
1622 /* This should be an unsigned type if the base type is unsigned or
1623 if the lower bound is constant and non-negative or if the type
1624 is biased. */
1625 if (Is_Unsigned_Type (Etype (gnat_entity))
1626 || Is_Unsigned_Type (gnat_entity)
1627 || Has_Biased_Representation (gnat_entity))
1628 gnu_type = make_unsigned_type (esize);
1629 else
1630 gnu_type = make_signed_type (esize);
1631 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1633 SET_TYPE_RM_MIN_VALUE
1634 (gnu_type,
1635 convert (TREE_TYPE (gnu_type),
1636 elaborate_expression (Type_Low_Bound (gnat_entity),
1637 gnat_entity, get_identifier ("L"),
1638 definition, true,
1639 Needs_Debug_Info (gnat_entity))));
1641 SET_TYPE_RM_MAX_VALUE
1642 (gnu_type,
1643 convert (TREE_TYPE (gnu_type),
1644 elaborate_expression (Type_High_Bound (gnat_entity),
1645 gnat_entity, get_identifier ("U"),
1646 definition, true,
1647 Needs_Debug_Info (gnat_entity))));
1649 /* One of the above calls might have caused us to be elaborated,
1650 so don't blow up if so. */
1651 if (present_gnu_tree (gnat_entity))
1653 maybe_present = true;
1654 break;
1657 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1658 = Has_Biased_Representation (gnat_entity);
1660 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1661 TYPE_STUB_DECL (gnu_type)
1662 = create_type_stub_decl (gnu_entity_name, gnu_type);
1664 /* Inherit our alias set from what we're a subtype of. Subtypes
1665 are not different types and a pointer can designate any instance
1666 within a subtype hierarchy. */
1667 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1669 /* For a packed array, make the original array type a parallel type. */
1670 if (debug_info_p
1671 && Is_Packed_Array_Type (gnat_entity)
1672 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1673 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1674 gnat_to_gnu_type
1675 (Original_Array_Type (gnat_entity)));
1677 discrete_type:
1679 /* We have to handle clauses that under-align the type specially. */
1680 if ((Present (Alignment_Clause (gnat_entity))
1681 || (Is_Packed_Array_Type (gnat_entity)
1682 && Present
1683 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1684 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1686 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1687 if (align >= TYPE_ALIGN (gnu_type))
1688 align = 0;
1691 /* If the type we are dealing with represents a bit-packed array,
1692 we need to have the bits left justified on big-endian targets
1693 and right justified on little-endian targets. We also need to
1694 ensure that when the value is read (e.g. for comparison of two
1695 such values), we only get the good bits, since the unused bits
1696 are uninitialized. Both goals are accomplished by wrapping up
1697 the modular type in an enclosing record type. */
1698 if (Is_Packed_Array_Type (gnat_entity)
1699 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1701 tree gnu_field_type, gnu_field;
1703 /* Set the RM size before wrapping up the original type. */
1704 SET_TYPE_RM_SIZE (gnu_type,
1705 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1706 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1708 /* Create a stripped-down declaration, mainly for debugging. */
1709 create_type_decl (gnu_entity_name, gnu_type, NULL, true,
1710 debug_info_p, gnat_entity);
1712 /* Now save it and build the enclosing record type. */
1713 gnu_field_type = gnu_type;
1715 gnu_type = make_node (RECORD_TYPE);
1716 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1717 TYPE_PACKED (gnu_type) = 1;
1718 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1719 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1720 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1722 /* Propagate the alignment of the modular type to the record type,
1723 unless there is an alignment clause that under-aligns the type.
1724 This means that bit-packed arrays are given "ceil" alignment for
1725 their size by default, which may seem counter-intuitive but makes
1726 it possible to overlay them on modular types easily. */
1727 TYPE_ALIGN (gnu_type)
1728 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1730 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1732 /* Don't declare the field as addressable since we won't be taking
1733 its address and this would prevent create_field_decl from making
1734 a bitfield. */
1735 gnu_field
1736 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1737 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1739 /* Do not emit debug info until after the parallel type is added. */
1740 finish_record_type (gnu_type, gnu_field, 2, false);
1741 compute_record_mode (gnu_type);
1742 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1744 if (debug_info_p)
1746 /* Make the original array type a parallel type. */
1747 if (present_gnu_tree (Original_Array_Type (gnat_entity)))
1748 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1749 gnat_to_gnu_type
1750 (Original_Array_Type (gnat_entity)));
1752 rest_of_record_type_compilation (gnu_type);
1756 /* If the type we are dealing with has got a smaller alignment than the
1757 natural one, we need to wrap it up in a record type and under-align
1758 the latter. We reuse the padding machinery for this purpose. */
1759 else if (align > 0)
1761 tree gnu_field_type, gnu_field;
1763 /* Set the RM size before wrapping up the type. */
1764 SET_TYPE_RM_SIZE (gnu_type,
1765 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1767 /* Create a stripped-down declaration, mainly for debugging. */
1768 create_type_decl (gnu_entity_name, gnu_type, NULL, true,
1769 debug_info_p, gnat_entity);
1771 /* Now save it and build the enclosing record type. */
1772 gnu_field_type = gnu_type;
1774 gnu_type = make_node (RECORD_TYPE);
1775 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1776 TYPE_PACKED (gnu_type) = 1;
1777 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1778 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1779 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1780 TYPE_ALIGN (gnu_type) = align;
1781 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1783 /* Don't declare the field as addressable since we won't be taking
1784 its address and this would prevent create_field_decl from making
1785 a bitfield. */
1786 gnu_field
1787 = create_field_decl (get_identifier ("F"), gnu_field_type,
1788 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1790 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1791 compute_record_mode (gnu_type);
1792 TYPE_PADDING_P (gnu_type) = 1;
1795 break;
1797 case E_Floating_Point_Type:
1798 /* If this is a VAX floating-point type, use an integer of the proper
1799 size. All the operations will be handled with ASM statements. */
1800 if (Vax_Float (gnat_entity))
1802 gnu_type = make_signed_type (esize);
1803 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1804 SET_TYPE_DIGITS_VALUE (gnu_type,
1805 UI_To_gnu (Digits_Value (gnat_entity),
1806 sizetype));
1807 break;
1810 /* The type of the Low and High bounds can be our type if this is
1811 a type from Standard, so set them at the end of the function. */
1812 gnu_type = make_node (REAL_TYPE);
1813 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1814 layout_type (gnu_type);
1815 break;
1817 case E_Floating_Point_Subtype:
1818 if (Vax_Float (gnat_entity))
1820 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1821 break;
1825 if (!definition
1826 && Present (Ancestor_Subtype (gnat_entity))
1827 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1828 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1829 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1830 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1831 gnu_expr, 0);
1833 gnu_type = make_node (REAL_TYPE);
1834 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1835 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1836 TYPE_GCC_MIN_VALUE (gnu_type)
1837 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1838 TYPE_GCC_MAX_VALUE (gnu_type)
1839 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1840 layout_type (gnu_type);
1842 SET_TYPE_RM_MIN_VALUE
1843 (gnu_type,
1844 convert (TREE_TYPE (gnu_type),
1845 elaborate_expression (Type_Low_Bound (gnat_entity),
1846 gnat_entity, get_identifier ("L"),
1847 definition, true,
1848 Needs_Debug_Info (gnat_entity))));
1850 SET_TYPE_RM_MAX_VALUE
1851 (gnu_type,
1852 convert (TREE_TYPE (gnu_type),
1853 elaborate_expression (Type_High_Bound (gnat_entity),
1854 gnat_entity, get_identifier ("U"),
1855 definition, true,
1856 Needs_Debug_Info (gnat_entity))));
1858 /* One of the above calls might have caused us to be elaborated,
1859 so don't blow up if so. */
1860 if (present_gnu_tree (gnat_entity))
1862 maybe_present = true;
1863 break;
1866 /* Inherit our alias set from what we're a subtype of, as for
1867 integer subtypes. */
1868 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1870 break;
1872 /* Array and String Types and Subtypes
1874 Unconstrained array types are represented by E_Array_Type and
1875 constrained array types are represented by E_Array_Subtype. There
1876 are no actual objects of an unconstrained array type; all we have
1877 are pointers to that type.
1879 The following fields are defined on array types and subtypes:
1881 Component_Type Component type of the array.
1882 Number_Dimensions Number of dimensions (an int).
1883 First_Index Type of first index. */
1885 case E_String_Type:
1886 case E_Array_Type:
1888 Entity_Id gnat_index, gnat_name;
1889 const bool convention_fortran_p
1890 = (Convention (gnat_entity) == Convention_Fortran);
1891 const int ndim = Number_Dimensions (gnat_entity);
1892 tree gnu_template_fields = NULL_TREE;
1893 tree gnu_template_type = make_node (RECORD_TYPE);
1894 tree gnu_template_reference;
1895 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1896 tree gnu_fat_type = make_node (RECORD_TYPE);
1897 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
1898 tree *gnu_temp_fields = XALLOCAVEC (tree, ndim);
1899 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem;
1900 int index;
1902 TYPE_NAME (gnu_template_type)
1903 = create_concat_name (gnat_entity, "XUB");
1905 /* Make a node for the array. If we are not defining the array
1906 suppress expanding incomplete types. */
1907 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1909 if (!definition)
1911 defer_incomplete_level++;
1912 this_deferred = true;
1915 /* Build the fat pointer type. Use a "void *" object instead of
1916 a pointer to the array type since we don't have the array type
1917 yet (it will reference the fat pointer via the bounds). */
1918 tem = chainon (chainon (NULL_TREE,
1919 create_field_decl (get_identifier ("P_ARRAY"),
1920 ptr_void_type_node,
1921 gnu_fat_type, NULL_TREE,
1922 NULL_TREE, 0, 0)),
1923 create_field_decl (get_identifier ("P_BOUNDS"),
1924 gnu_ptr_template,
1925 gnu_fat_type, NULL_TREE,
1926 NULL_TREE, 0, 0));
1928 /* Make sure we can put this into a register. */
1929 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1931 /* Do not emit debug info for this record type since the types of its
1932 fields are still incomplete at this point. */
1933 finish_record_type (gnu_fat_type, tem, 0, false);
1934 TYPE_FAT_POINTER_P (gnu_fat_type) = 1;
1936 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1937 is the fat pointer. This will be used to access the individual
1938 fields once we build them. */
1939 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1940 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1941 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1942 gnu_template_reference
1943 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1944 TREE_READONLY (gnu_template_reference) = 1;
1946 /* Now create the GCC type for each index and add the fields for that
1947 index to the template. */
1948 for (index = (convention_fortran_p ? ndim - 1 : 0),
1949 gnat_index = First_Index (gnat_entity);
1950 0 <= index && index < ndim;
1951 index += (convention_fortran_p ? - 1 : 1),
1952 gnat_index = Next_Index (gnat_index))
1954 char field_name[16];
1955 tree gnu_index_base_type
1956 = get_unpadded_type (Base_Type (Etype (gnat_index)));
1957 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
1958 tree gnu_min, gnu_max, gnu_high;
1960 /* Make the FIELD_DECLs for the low and high bounds of this
1961 type and then make extractions of these fields from the
1962 template. */
1963 sprintf (field_name, "LB%d", index);
1964 gnu_lb_field = create_field_decl (get_identifier (field_name),
1965 gnu_index_base_type,
1966 gnu_template_type, NULL_TREE,
1967 NULL_TREE, 0, 0);
1968 Sloc_to_locus (Sloc (gnat_entity),
1969 &DECL_SOURCE_LOCATION (gnu_lb_field));
1971 field_name[0] = 'U';
1972 gnu_hb_field = create_field_decl (get_identifier (field_name),
1973 gnu_index_base_type,
1974 gnu_template_type, NULL_TREE,
1975 NULL_TREE, 0, 0);
1976 Sloc_to_locus (Sloc (gnat_entity),
1977 &DECL_SOURCE_LOCATION (gnu_hb_field));
1979 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
1981 /* We can't use build_component_ref here since the template type
1982 isn't complete yet. */
1983 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
1984 gnu_template_reference, gnu_lb_field,
1985 NULL_TREE);
1986 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
1987 gnu_template_reference, gnu_hb_field,
1988 NULL_TREE);
1989 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
1991 gnu_min = convert (sizetype, gnu_orig_min);
1992 gnu_max = convert (sizetype, gnu_orig_max);
1994 /* Compute the size of this dimension. See the E_Array_Subtype
1995 case below for the rationale. */
1996 gnu_high
1997 = build3 (COND_EXPR, sizetype,
1998 build2 (GE_EXPR, boolean_type_node,
1999 gnu_orig_max, gnu_orig_min),
2000 gnu_max,
2001 size_binop (MINUS_EXPR, gnu_min, size_one_node));
2003 /* Make a range type with the new range in the Ada base type.
2004 Then make an index type with the size range in sizetype. */
2005 gnu_index_types[index]
2006 = create_index_type (gnu_min, gnu_high,
2007 create_range_type (gnu_index_base_type,
2008 gnu_orig_min,
2009 gnu_orig_max),
2010 gnat_entity);
2012 /* Update the maximum size of the array in elements. */
2013 if (gnu_max_size)
2015 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2016 tree gnu_min
2017 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
2018 tree gnu_max
2019 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
2020 tree gnu_this_max
2021 = size_binop (MAX_EXPR,
2022 size_binop (PLUS_EXPR, size_one_node,
2023 size_binop (MINUS_EXPR,
2024 gnu_max, gnu_min)),
2025 size_zero_node);
2027 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2028 && TREE_OVERFLOW (gnu_this_max))
2029 gnu_max_size = NULL_TREE;
2030 else
2031 gnu_max_size
2032 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2035 TYPE_NAME (gnu_index_types[index])
2036 = create_concat_name (gnat_entity, field_name);
2039 for (index = 0; index < ndim; index++)
2040 gnu_template_fields
2041 = chainon (gnu_template_fields, gnu_temp_fields[index]);
2043 /* Install all the fields into the template. */
2044 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2045 debug_info_p);
2046 TYPE_READONLY (gnu_template_type) = 1;
2048 /* Now make the array of arrays and update the pointer to the array
2049 in the fat pointer. Note that it is the first field. */
2050 tem = gnat_to_gnu_component_type (gnat_entity, definition,
2051 debug_info_p);
2053 /* If Component_Size is not already specified, annotate it with the
2054 size of the component. */
2055 if (Unknown_Component_Size (gnat_entity))
2056 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
2058 /* Compute the maximum size of the array in units and bits. */
2059 if (gnu_max_size)
2061 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2062 TYPE_SIZE_UNIT (tem));
2063 gnu_max_size = size_binop (MULT_EXPR,
2064 convert (bitsizetype, gnu_max_size),
2065 TYPE_SIZE (tem));
2067 else
2068 gnu_max_size_unit = NULL_TREE;
2070 /* Now build the array type. */
2071 for (index = ndim - 1; index >= 0; index--)
2073 tem = build_nonshared_array_type (tem, gnu_index_types[index]);
2074 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2075 if (array_type_has_nonaliased_component (tem, gnat_entity))
2076 TYPE_NONALIASED_COMPONENT (tem) = 1;
2079 /* If an alignment is specified, use it if valid. But ignore it
2080 for the original type of packed array types. If the alignment
2081 was requested with an explicit alignment clause, state so. */
2082 if (No (Packed_Array_Type (gnat_entity))
2083 && Known_Alignment (gnat_entity))
2085 TYPE_ALIGN (tem)
2086 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2087 TYPE_ALIGN (tem));
2088 if (Present (Alignment_Clause (gnat_entity)))
2089 TYPE_USER_ALIGN (tem) = 1;
2092 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2093 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2095 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2096 corresponding fat pointer. */
2097 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
2098 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2099 SET_TYPE_MODE (gnu_type, BLKmode);
2100 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2101 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2103 /* If the maximum size doesn't overflow, use it. */
2104 if (gnu_max_size
2105 && TREE_CODE (gnu_max_size) == INTEGER_CST
2106 && !TREE_OVERFLOW (gnu_max_size)
2107 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2108 && !TREE_OVERFLOW (gnu_max_size_unit))
2110 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2111 TYPE_SIZE (tem));
2112 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2113 TYPE_SIZE_UNIT (tem));
2116 create_type_decl (create_concat_name (gnat_entity, "XUA"),
2117 tem, NULL, !Comes_From_Source (gnat_entity),
2118 debug_info_p, gnat_entity);
2120 /* Give the fat pointer type a name. If this is a packed type, tell
2121 the debugger how to interpret the underlying bits. */
2122 if (Present (Packed_Array_Type (gnat_entity)))
2123 gnat_name = Packed_Array_Type (gnat_entity);
2124 else
2125 gnat_name = gnat_entity;
2126 create_type_decl (create_concat_name (gnat_name, "XUP"),
2127 gnu_fat_type, NULL, true,
2128 debug_info_p, gnat_entity);
2130 /* Create the type to be used as what a thin pointer designates:
2131 a record type for the object and its template with the fields
2132 shifted to have the template at a negative offset. */
2133 tem = build_unc_object_type (gnu_template_type, tem,
2134 create_concat_name (gnat_name, "XUT"),
2135 debug_info_p);
2136 shift_unc_components_for_thin_pointers (tem);
2138 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2139 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2141 break;
2143 case E_String_Subtype:
2144 case E_Array_Subtype:
2146 /* This is the actual data type for array variables. Multidimensional
2147 arrays are implemented as arrays of arrays. Note that arrays which
2148 have sparse enumeration subtypes as index components create sparse
2149 arrays, which is obviously space inefficient but so much easier to
2150 code for now.
2152 Also note that the subtype never refers to the unconstrained array
2153 type, which is somewhat at variance with Ada semantics.
2155 First check to see if this is simply a renaming of the array type.
2156 If so, the result is the array type. */
2158 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2159 if (!Is_Constrained (gnat_entity))
2161 else
2163 Entity_Id gnat_index, gnat_base_index;
2164 const bool convention_fortran_p
2165 = (Convention (gnat_entity) == Convention_Fortran);
2166 const int ndim = Number_Dimensions (gnat_entity);
2167 tree gnu_base_type = gnu_type;
2168 tree *gnu_index_types = XALLOCAVEC (tree, ndim);
2169 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2170 bool need_index_type_struct = false;
2171 int index;
2173 /* First create the GCC type for each index and find out whether
2174 special types are needed for debugging information. */
2175 for (index = (convention_fortran_p ? ndim - 1 : 0),
2176 gnat_index = First_Index (gnat_entity),
2177 gnat_base_index
2178 = First_Index (Implementation_Base_Type (gnat_entity));
2179 0 <= index && index < ndim;
2180 index += (convention_fortran_p ? - 1 : 1),
2181 gnat_index = Next_Index (gnat_index),
2182 gnat_base_index = Next_Index (gnat_base_index))
2184 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2185 tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type);
2186 tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type);
2187 tree gnu_min = convert (sizetype, gnu_orig_min);
2188 tree gnu_max = convert (sizetype, gnu_orig_max);
2189 tree gnu_base_index_type
2190 = get_unpadded_type (Etype (gnat_base_index));
2191 tree gnu_base_orig_min = TYPE_MIN_VALUE (gnu_base_index_type);
2192 tree gnu_base_orig_max = TYPE_MAX_VALUE (gnu_base_index_type);
2193 tree gnu_high;
2195 /* See if the base array type is already flat. If it is, we
2196 are probably compiling an ACATS test but it will cause the
2197 code below to malfunction if we don't handle it specially. */
2198 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2199 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2200 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2202 gnu_min = size_one_node;
2203 gnu_max = size_zero_node;
2204 gnu_high = gnu_max;
2207 /* Similarly, if one of the values overflows in sizetype and the
2208 range is null, use 1..0 for the sizetype bounds. */
2209 else if (TREE_CODE (gnu_min) == INTEGER_CST
2210 && TREE_CODE (gnu_max) == INTEGER_CST
2211 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2212 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2214 gnu_min = size_one_node;
2215 gnu_max = size_zero_node;
2216 gnu_high = gnu_max;
2219 /* If the minimum and maximum values both overflow in sizetype,
2220 but the difference in the original type does not overflow in
2221 sizetype, ignore the overflow indication. */
2222 else if (TREE_CODE (gnu_min) == INTEGER_CST
2223 && TREE_CODE (gnu_max) == INTEGER_CST
2224 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2225 && !TREE_OVERFLOW
2226 (convert (sizetype,
2227 fold_build2 (MINUS_EXPR, gnu_index_type,
2228 gnu_orig_max,
2229 gnu_orig_min))))
2231 TREE_OVERFLOW (gnu_min) = 0;
2232 TREE_OVERFLOW (gnu_max) = 0;
2233 gnu_high = gnu_max;
2236 /* Compute the size of this dimension in the general case. We
2237 need to provide GCC with an upper bound to use but have to
2238 deal with the "superflat" case. There are three ways to do
2239 this. If we can prove that the array can never be superflat,
2240 we can just use the high bound of the index type. */
2241 else if ((Nkind (gnat_index) == N_Range
2242 && cannot_be_superflat_p (gnat_index))
2243 /* Packed Array Types are never superflat. */
2244 || Is_Packed_Array_Type (gnat_entity))
2245 gnu_high = gnu_max;
2247 /* Otherwise, if the high bound is constant but the low bound is
2248 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2249 lower bound. Note that the comparison must be done in the
2250 original type to avoid any overflow during the conversion. */
2251 else if (TREE_CODE (gnu_max) == INTEGER_CST
2252 && TREE_CODE (gnu_min) != INTEGER_CST)
2254 gnu_high = gnu_max;
2255 gnu_min
2256 = build_cond_expr (sizetype,
2257 build_binary_op (GE_EXPR,
2258 boolean_type_node,
2259 gnu_orig_max,
2260 gnu_orig_min),
2261 gnu_min,
2262 size_binop (PLUS_EXPR, gnu_max,
2263 size_one_node));
2266 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2267 in all the other cases. Note that, here as well as above,
2268 the condition used in the comparison must be equivalent to
2269 the condition (length != 0). This is relied upon in order
2270 to optimize array comparisons in compare_arrays. */
2271 else
2272 gnu_high
2273 = build_cond_expr (sizetype,
2274 build_binary_op (GE_EXPR,
2275 boolean_type_node,
2276 gnu_orig_max,
2277 gnu_orig_min),
2278 gnu_max,
2279 size_binop (MINUS_EXPR, gnu_min,
2280 size_one_node));
2282 /* Reuse the index type for the range type. Then make an index
2283 type with the size range in sizetype. */
2284 gnu_index_types[index]
2285 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2286 gnat_entity);
2288 /* Update the maximum size of the array in elements. Here we
2289 see if any constraint on the index type of the base type
2290 can be used in the case of self-referential bound on the
2291 index type of the subtype. We look for a non-"infinite"
2292 and non-self-referential bound from any type involved and
2293 handle each bound separately. */
2294 if (gnu_max_size)
2296 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2297 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2298 tree gnu_base_index_base_type
2299 = get_base_type (gnu_base_index_type);
2300 tree gnu_base_base_min
2301 = convert (sizetype,
2302 TYPE_MIN_VALUE (gnu_base_index_base_type));
2303 tree gnu_base_base_max
2304 = convert (sizetype,
2305 TYPE_MAX_VALUE (gnu_base_index_base_type));
2307 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2308 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2309 && !TREE_OVERFLOW (gnu_base_min)))
2310 gnu_base_min = gnu_min;
2312 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2313 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2314 && !TREE_OVERFLOW (gnu_base_max)))
2315 gnu_base_max = gnu_max;
2317 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2318 && TREE_OVERFLOW (gnu_base_min))
2319 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2320 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2321 && TREE_OVERFLOW (gnu_base_max))
2322 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2323 gnu_max_size = NULL_TREE;
2324 else
2326 tree gnu_this_max
2327 = size_binop (MAX_EXPR,
2328 size_binop (PLUS_EXPR, size_one_node,
2329 size_binop (MINUS_EXPR,
2330 gnu_base_max,
2331 gnu_base_min)),
2332 size_zero_node);
2334 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2335 && TREE_OVERFLOW (gnu_this_max))
2336 gnu_max_size = NULL_TREE;
2337 else
2338 gnu_max_size
2339 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2343 /* We need special types for debugging information to point to
2344 the index types if they have variable bounds, are not integer
2345 types, are biased or are wider than sizetype. */
2346 if (!integer_onep (gnu_orig_min)
2347 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2348 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2349 || (TREE_TYPE (gnu_index_type)
2350 && TREE_CODE (TREE_TYPE (gnu_index_type))
2351 != INTEGER_TYPE)
2352 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type)
2353 || compare_tree_int (rm_size (gnu_index_type),
2354 TYPE_PRECISION (sizetype)) > 0)
2355 need_index_type_struct = true;
2358 /* Then flatten: create the array of arrays. For an array type
2359 used to implement a packed array, get the component type from
2360 the original array type since the representation clauses that
2361 can affect it are on the latter. */
2362 if (Is_Packed_Array_Type (gnat_entity)
2363 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2365 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2366 for (index = ndim - 1; index >= 0; index--)
2367 gnu_type = TREE_TYPE (gnu_type);
2369 /* One of the above calls might have caused us to be elaborated,
2370 so don't blow up if so. */
2371 if (present_gnu_tree (gnat_entity))
2373 maybe_present = true;
2374 break;
2377 else
2379 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2380 debug_info_p);
2382 /* One of the above calls might have caused us to be elaborated,
2383 so don't blow up if so. */
2384 if (present_gnu_tree (gnat_entity))
2386 maybe_present = true;
2387 break;
2391 /* Compute the maximum size of the array in units and bits. */
2392 if (gnu_max_size)
2394 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2395 TYPE_SIZE_UNIT (gnu_type));
2396 gnu_max_size = size_binop (MULT_EXPR,
2397 convert (bitsizetype, gnu_max_size),
2398 TYPE_SIZE (gnu_type));
2400 else
2401 gnu_max_size_unit = NULL_TREE;
2403 /* Now build the array type. */
2404 for (index = ndim - 1; index >= 0; index --)
2406 gnu_type = build_nonshared_array_type (gnu_type,
2407 gnu_index_types[index]);
2408 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2409 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2410 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2413 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2414 TYPE_STUB_DECL (gnu_type)
2415 = create_type_stub_decl (gnu_entity_name, gnu_type);
2417 /* If we are at file level and this is a multi-dimensional array,
2418 we need to make a variable corresponding to the stride of the
2419 inner dimensions. */
2420 if (global_bindings_p () && ndim > 1)
2422 tree gnu_st_name = get_identifier ("ST");
2423 tree gnu_arr_type;
2425 for (gnu_arr_type = TREE_TYPE (gnu_type);
2426 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2427 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2428 gnu_st_name = concat_name (gnu_st_name, "ST"))
2430 tree eltype = TREE_TYPE (gnu_arr_type);
2432 TYPE_SIZE (gnu_arr_type)
2433 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2434 gnat_entity, gnu_st_name,
2435 definition, false);
2437 /* ??? For now, store the size as a multiple of the
2438 alignment of the element type in bytes so that we
2439 can see the alignment from the tree. */
2440 TYPE_SIZE_UNIT (gnu_arr_type)
2441 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2442 gnat_entity,
2443 concat_name (gnu_st_name, "A_U"),
2444 definition, false,
2445 TYPE_ALIGN (eltype));
2447 /* ??? create_type_decl is not invoked on the inner types so
2448 the MULT_EXPR node built above will never be marked. */
2449 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2453 /* If we need to write out a record type giving the names of the
2454 bounds for debugging purposes, do it now and make the record
2455 type a parallel type. This is not needed for a packed array
2456 since the bounds are conveyed by the original array type. */
2457 if (need_index_type_struct
2458 && debug_info_p
2459 && !Is_Packed_Array_Type (gnat_entity))
2461 tree gnu_bound_rec = make_node (RECORD_TYPE);
2462 tree gnu_field_list = NULL_TREE;
2463 tree gnu_field;
2465 TYPE_NAME (gnu_bound_rec)
2466 = create_concat_name (gnat_entity, "XA");
2468 for (index = ndim - 1; index >= 0; index--)
2470 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2471 tree gnu_index_name = TYPE_NAME (gnu_index);
2473 if (TREE_CODE (gnu_index_name) == TYPE_DECL)
2474 gnu_index_name = DECL_NAME (gnu_index_name);
2476 /* Make sure to reference the types themselves, and not just
2477 their names, as the debugger may fall back on them. */
2478 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2479 gnu_bound_rec, NULL_TREE,
2480 NULL_TREE, 0, 0);
2481 DECL_CHAIN (gnu_field) = gnu_field_list;
2482 gnu_field_list = gnu_field;
2485 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2486 add_parallel_type (TYPE_STUB_DECL (gnu_type), gnu_bound_rec);
2489 /* Otherwise, for a packed array, make the original array type a
2490 parallel type. */
2491 else if (debug_info_p
2492 && Is_Packed_Array_Type (gnat_entity)
2493 && present_gnu_tree (Original_Array_Type (gnat_entity)))
2494 add_parallel_type (TYPE_STUB_DECL (gnu_type),
2495 gnat_to_gnu_type
2496 (Original_Array_Type (gnat_entity)));
2498 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2499 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2500 = (Is_Packed_Array_Type (gnat_entity)
2501 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2503 /* If the size is self-referential and the maximum size doesn't
2504 overflow, use it. */
2505 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2506 && gnu_max_size
2507 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2508 && TREE_OVERFLOW (gnu_max_size))
2509 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2510 && TREE_OVERFLOW (gnu_max_size_unit)))
2512 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2513 TYPE_SIZE (gnu_type));
2514 TYPE_SIZE_UNIT (gnu_type)
2515 = size_binop (MIN_EXPR, gnu_max_size_unit,
2516 TYPE_SIZE_UNIT (gnu_type));
2519 /* Set our alias set to that of our base type. This gives all
2520 array subtypes the same alias set. */
2521 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2523 /* If this is a packed type, make this type the same as the packed
2524 array type, but do some adjusting in the type first. */
2525 if (Present (Packed_Array_Type (gnat_entity)))
2527 Entity_Id gnat_index;
2528 tree gnu_inner;
2530 /* First finish the type we had been making so that we output
2531 debugging information for it. */
2532 if (Treat_As_Volatile (gnat_entity))
2533 gnu_type
2534 = build_qualified_type (gnu_type,
2535 TYPE_QUALS (gnu_type)
2536 | TYPE_QUAL_VOLATILE);
2538 /* Make it artificial only if the base type was artificial too.
2539 That's sort of "morally" true and will make it possible for
2540 the debugger to look it up by name in DWARF, which is needed
2541 in order to decode the packed array type. */
2542 gnu_decl
2543 = create_type_decl (gnu_entity_name, gnu_type, attr_list,
2544 !Comes_From_Source (Etype (gnat_entity))
2545 && !Comes_From_Source (gnat_entity),
2546 debug_info_p, gnat_entity);
2548 /* Save it as our equivalent in case the call below elaborates
2549 this type again. */
2550 save_gnu_tree (gnat_entity, gnu_decl, false);
2552 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2553 NULL_TREE, 0);
2554 this_made_decl = true;
2555 gnu_type = TREE_TYPE (gnu_decl);
2556 save_gnu_tree (gnat_entity, NULL_TREE, false);
2558 gnu_inner = gnu_type;
2559 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2560 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2561 || TYPE_PADDING_P (gnu_inner)))
2562 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2564 /* We need to attach the index type to the type we just made so
2565 that the actual bounds can later be put into a template. */
2566 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2567 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2568 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2569 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2571 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2573 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2574 TYPE_MODULUS for modular types so we make an extra
2575 subtype if necessary. */
2576 if (TYPE_MODULAR_P (gnu_inner))
2578 tree gnu_subtype
2579 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2580 TREE_TYPE (gnu_subtype) = gnu_inner;
2581 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2582 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2583 TYPE_MIN_VALUE (gnu_inner));
2584 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2585 TYPE_MAX_VALUE (gnu_inner));
2586 gnu_inner = gnu_subtype;
2589 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2591 #ifdef ENABLE_CHECKING
2592 /* Check for other cases of overloading. */
2593 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2594 #endif
2597 for (gnat_index = First_Index (gnat_entity);
2598 Present (gnat_index);
2599 gnat_index = Next_Index (gnat_index))
2600 SET_TYPE_ACTUAL_BOUNDS
2601 (gnu_inner,
2602 tree_cons (NULL_TREE,
2603 get_unpadded_type (Etype (gnat_index)),
2604 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2606 if (Convention (gnat_entity) != Convention_Fortran)
2607 SET_TYPE_ACTUAL_BOUNDS
2608 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2610 if (TREE_CODE (gnu_type) == RECORD_TYPE
2611 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2612 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2616 else
2617 /* Abort if packed array with no Packed_Array_Type field set. */
2618 gcc_assert (!Is_Packed (gnat_entity));
2620 break;
2622 case E_String_Literal_Subtype:
2623 /* Create the type for a string literal. */
2625 Entity_Id gnat_full_type
2626 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2627 && Present (Full_View (Etype (gnat_entity)))
2628 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2629 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2630 tree gnu_string_array_type
2631 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2632 tree gnu_string_index_type
2633 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2634 (TYPE_DOMAIN (gnu_string_array_type))));
2635 tree gnu_lower_bound
2636 = convert (gnu_string_index_type,
2637 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2638 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2639 tree gnu_length = ssize_int (length - 1);
2640 tree gnu_upper_bound
2641 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2642 gnu_lower_bound,
2643 convert (gnu_string_index_type, gnu_length));
2644 tree gnu_index_type
2645 = create_index_type (convert (sizetype, gnu_lower_bound),
2646 convert (sizetype, gnu_upper_bound),
2647 create_range_type (gnu_string_index_type,
2648 gnu_lower_bound,
2649 gnu_upper_bound),
2650 gnat_entity);
2652 gnu_type
2653 = build_nonshared_array_type (gnat_to_gnu_type
2654 (Component_Type (gnat_entity)),
2655 gnu_index_type);
2656 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2657 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2658 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2660 break;
2662 /* Record Types and Subtypes
2664 The following fields are defined on record types:
2666 Has_Discriminants True if the record has discriminants
2667 First_Discriminant Points to head of list of discriminants
2668 First_Entity Points to head of list of fields
2669 Is_Tagged_Type True if the record is tagged
2671 Implementation of Ada records and discriminated records:
2673 A record type definition is transformed into the equivalent of a C
2674 struct definition. The fields that are the discriminants which are
2675 found in the Full_Type_Declaration node and the elements of the
2676 Component_List found in the Record_Type_Definition node. The
2677 Component_List can be a recursive structure since each Variant of
2678 the Variant_Part of the Component_List has a Component_List.
2680 Processing of a record type definition comprises starting the list of
2681 field declarations here from the discriminants and the calling the
2682 function components_to_record to add the rest of the fields from the
2683 component list and return the gnu type node. The function
2684 components_to_record will call itself recursively as it traverses
2685 the tree. */
2687 case E_Record_Type:
2688 if (Has_Complex_Representation (gnat_entity))
2690 gnu_type
2691 = build_complex_type
2692 (get_unpadded_type
2693 (Etype (Defining_Entity
2694 (First (Component_Items
2695 (Component_List
2696 (Type_Definition
2697 (Declaration_Node (gnat_entity)))))))));
2699 break;
2703 Node_Id full_definition = Declaration_Node (gnat_entity);
2704 Node_Id record_definition = Type_Definition (full_definition);
2705 Entity_Id gnat_field;
2706 tree gnu_field, gnu_field_list = NULL_TREE, gnu_get_parent;
2707 /* Set PACKED in keeping with gnat_to_gnu_field. */
2708 int packed
2709 = Is_Packed (gnat_entity)
2711 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2712 ? -1
2713 : (Known_Alignment (gnat_entity)
2714 || (Strict_Alignment (gnat_entity)
2715 && Known_Static_Esize (gnat_entity)))
2716 ? -2
2717 : 0;
2718 bool has_discr = Has_Discriminants (gnat_entity);
2719 bool has_rep = Has_Specified_Layout (gnat_entity);
2720 bool all_rep = has_rep;
2721 bool is_extension
2722 = (Is_Tagged_Type (gnat_entity)
2723 && Nkind (record_definition) == N_Derived_Type_Definition);
2724 bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2726 /* See if all fields have a rep clause. Stop when we find one
2727 that doesn't. */
2728 if (all_rep)
2729 for (gnat_field = First_Entity (gnat_entity);
2730 Present (gnat_field);
2731 gnat_field = Next_Entity (gnat_field))
2732 if ((Ekind (gnat_field) == E_Component
2733 || Ekind (gnat_field) == E_Discriminant)
2734 && No (Component_Clause (gnat_field)))
2736 all_rep = false;
2737 break;
2740 /* If this is a record extension, go a level further to find the
2741 record definition. Also, verify we have a Parent_Subtype. */
2742 if (is_extension)
2744 if (!type_annotate_only
2745 || Present (Record_Extension_Part (record_definition)))
2746 record_definition = Record_Extension_Part (record_definition);
2748 gcc_assert (type_annotate_only
2749 || Present (Parent_Subtype (gnat_entity)));
2752 /* Make a node for the record. If we are not defining the record,
2753 suppress expanding incomplete types. */
2754 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2755 TYPE_NAME (gnu_type) = gnu_entity_name;
2756 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2758 if (!definition)
2760 defer_incomplete_level++;
2761 this_deferred = true;
2764 /* If both a size and rep clause was specified, put the size in
2765 the record type now so that it can get the proper mode. */
2766 if (has_rep && Known_Esize (gnat_entity))
2767 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2769 /* Always set the alignment here so that it can be used to
2770 set the mode, if it is making the alignment stricter. If
2771 it is invalid, it will be checked again below. If this is to
2772 be Atomic, choose a default alignment of a word unless we know
2773 the size and it's smaller. */
2774 if (Known_Alignment (gnat_entity))
2775 TYPE_ALIGN (gnu_type)
2776 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2777 else if (Is_Atomic (gnat_entity))
2778 TYPE_ALIGN (gnu_type)
2779 = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2780 /* If a type needs strict alignment, the minimum size will be the
2781 type size instead of the RM size (see validate_size). Cap the
2782 alignment, lest it causes this type size to become too large. */
2783 else if (Strict_Alignment (gnat_entity)
2784 && Known_Static_Esize (gnat_entity))
2786 unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2787 unsigned int raw_align = raw_size & -raw_size;
2788 if (raw_align < BIGGEST_ALIGNMENT)
2789 TYPE_ALIGN (gnu_type) = raw_align;
2791 else
2792 TYPE_ALIGN (gnu_type) = 0;
2794 /* If we have a Parent_Subtype, make a field for the parent. If
2795 this record has rep clauses, force the position to zero. */
2796 if (Present (Parent_Subtype (gnat_entity)))
2798 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2799 tree gnu_parent;
2801 /* A major complexity here is that the parent subtype will
2802 reference our discriminants in its Discriminant_Constraint
2803 list. But those must reference the parent component of this
2804 record which is of the parent subtype we have not built yet!
2805 To break the circle we first build a dummy COMPONENT_REF which
2806 represents the "get to the parent" operation and initialize
2807 each of those discriminants to a COMPONENT_REF of the above
2808 dummy parent referencing the corresponding discriminant of the
2809 base type of the parent subtype. */
2810 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2811 build0 (PLACEHOLDER_EXPR, gnu_type),
2812 build_decl (input_location,
2813 FIELD_DECL, NULL_TREE,
2814 void_type_node),
2815 NULL_TREE);
2817 if (has_discr)
2818 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2819 Present (gnat_field);
2820 gnat_field = Next_Stored_Discriminant (gnat_field))
2821 if (Present (Corresponding_Discriminant (gnat_field)))
2823 tree gnu_field
2824 = gnat_to_gnu_field_decl (Corresponding_Discriminant
2825 (gnat_field));
2826 save_gnu_tree
2827 (gnat_field,
2828 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2829 gnu_get_parent, gnu_field, NULL_TREE),
2830 true);
2833 /* Then we build the parent subtype. If it has discriminants but
2834 the type itself has unknown discriminants, this means that it
2835 doesn't contain information about how the discriminants are
2836 derived from those of the ancestor type, so it cannot be used
2837 directly. Instead it is built by cloning the parent subtype
2838 of the underlying record view of the type, for which the above
2839 derivation of discriminants has been made explicit. */
2840 if (Has_Discriminants (gnat_parent)
2841 && Has_Unknown_Discriminants (gnat_entity))
2843 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
2845 /* If we are defining the type, the underlying record
2846 view must already have been elaborated at this point.
2847 Otherwise do it now as its parent subtype cannot be
2848 technically elaborated on its own. */
2849 if (definition)
2850 gcc_assert (present_gnu_tree (gnat_uview));
2851 else
2852 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
2854 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
2856 /* Substitute the "get to the parent" of the type for that
2857 of its underlying record view in the cloned type. */
2858 for (gnat_field = First_Stored_Discriminant (gnat_uview);
2859 Present (gnat_field);
2860 gnat_field = Next_Stored_Discriminant (gnat_field))
2861 if (Present (Corresponding_Discriminant (gnat_field)))
2863 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
2864 tree gnu_ref
2865 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2866 gnu_get_parent, gnu_field, NULL_TREE);
2867 gnu_parent
2868 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
2871 else
2872 gnu_parent = gnat_to_gnu_type (gnat_parent);
2874 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2875 initially built. The discriminants must reference the fields
2876 of the parent subtype and not those of its base type for the
2877 placeholder machinery to properly work. */
2878 if (has_discr)
2880 /* The actual parent subtype is the full view. */
2881 if (IN (Ekind (gnat_parent), Private_Kind))
2883 if (Present (Full_View (gnat_parent)))
2884 gnat_parent = Full_View (gnat_parent);
2885 else
2886 gnat_parent = Underlying_Full_View (gnat_parent);
2889 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2890 Present (gnat_field);
2891 gnat_field = Next_Stored_Discriminant (gnat_field))
2892 if (Present (Corresponding_Discriminant (gnat_field)))
2894 Entity_Id field = Empty;
2895 for (field = First_Stored_Discriminant (gnat_parent);
2896 Present (field);
2897 field = Next_Stored_Discriminant (field))
2898 if (same_discriminant_p (gnat_field, field))
2899 break;
2900 gcc_assert (Present (field));
2901 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2902 = gnat_to_gnu_field_decl (field);
2906 /* The "get to the parent" COMPONENT_REF must be given its
2907 proper type... */
2908 TREE_TYPE (gnu_get_parent) = gnu_parent;
2910 /* ...and reference the _Parent field of this record. */
2911 gnu_field
2912 = create_field_decl (parent_name_id,
2913 gnu_parent, gnu_type,
2914 has_rep
2915 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
2916 has_rep
2917 ? bitsize_zero_node : NULL_TREE,
2918 0, 1);
2919 DECL_INTERNAL_P (gnu_field) = 1;
2920 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
2921 TYPE_FIELDS (gnu_type) = gnu_field;
2924 /* Make the fields for the discriminants and put them into the record
2925 unless it's an Unchecked_Union. */
2926 if (has_discr)
2927 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2928 Present (gnat_field);
2929 gnat_field = Next_Stored_Discriminant (gnat_field))
2931 /* If this is a record extension and this discriminant is the
2932 renaming of another discriminant, we've handled it above. */
2933 if (Present (Parent_Subtype (gnat_entity))
2934 && Present (Corresponding_Discriminant (gnat_field)))
2935 continue;
2937 gnu_field
2938 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
2939 debug_info_p);
2941 /* Make an expression using a PLACEHOLDER_EXPR from the
2942 FIELD_DECL node just created and link that with the
2943 corresponding GNAT defining identifier. */
2944 save_gnu_tree (gnat_field,
2945 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2946 build0 (PLACEHOLDER_EXPR, gnu_type),
2947 gnu_field, NULL_TREE),
2948 true);
2950 if (!is_unchecked_union)
2952 DECL_CHAIN (gnu_field) = gnu_field_list;
2953 gnu_field_list = gnu_field;
2957 /* Add the fields into the record type and finish it up. */
2958 components_to_record (gnu_type, Component_List (record_definition),
2959 gnu_field_list, packed, definition, NULL,
2960 false, all_rep, is_unchecked_union,
2961 debug_info_p, false);
2963 /* If it is passed by reference, force BLKmode to ensure that objects
2964 of this type will always be put in memory. */
2965 if (Is_By_Reference_Type (gnat_entity))
2966 SET_TYPE_MODE (gnu_type, BLKmode);
2968 /* We used to remove the associations of the discriminants and _Parent
2969 for validity checking but we may need them if there's a Freeze_Node
2970 for a subtype used in this record. */
2971 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2973 /* Fill in locations of fields. */
2974 annotate_rep (gnat_entity, gnu_type);
2976 /* If there are any entities in the chain corresponding to components
2977 that we did not elaborate, ensure we elaborate their types if they
2978 are Itypes. */
2979 for (gnat_temp = First_Entity (gnat_entity);
2980 Present (gnat_temp);
2981 gnat_temp = Next_Entity (gnat_temp))
2982 if ((Ekind (gnat_temp) == E_Component
2983 || Ekind (gnat_temp) == E_Discriminant)
2984 && Is_Itype (Etype (gnat_temp))
2985 && !present_gnu_tree (gnat_temp))
2986 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2988 /* If this is a record type associated with an exception definition,
2989 equate its fields to those of the standard exception type. This
2990 will make it possible to convert between them. */
2991 if (gnu_entity_name == exception_data_name_id)
2993 tree gnu_std_field;
2994 for (gnu_field = TYPE_FIELDS (gnu_type),
2995 gnu_std_field = TYPE_FIELDS (except_type_node);
2996 gnu_field;
2997 gnu_field = DECL_CHAIN (gnu_field),
2998 gnu_std_field = DECL_CHAIN (gnu_std_field))
2999 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
3000 gcc_assert (!gnu_std_field);
3003 break;
3005 case E_Class_Wide_Subtype:
3006 /* If an equivalent type is present, that is what we should use.
3007 Otherwise, fall through to handle this like a record subtype
3008 since it may have constraints. */
3009 if (gnat_equiv_type != gnat_entity)
3011 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3012 maybe_present = true;
3013 break;
3016 /* ... fall through ... */
3018 case E_Record_Subtype:
3019 /* If Cloned_Subtype is Present it means this record subtype has
3020 identical layout to that type or subtype and we should use
3021 that GCC type for this one. The front end guarantees that
3022 the component list is shared. */
3023 if (Present (Cloned_Subtype (gnat_entity)))
3025 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3026 NULL_TREE, 0);
3027 maybe_present = true;
3028 break;
3031 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3032 changing the type, make a new type with each field having the type of
3033 the field in the new subtype but the position computed by transforming
3034 every discriminant reference according to the constraints. We don't
3035 see any difference between private and non-private type here since
3036 derivations from types should have been deferred until the completion
3037 of the private type. */
3038 else
3040 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3041 tree gnu_base_type;
3043 if (!definition)
3045 defer_incomplete_level++;
3046 this_deferred = true;
3049 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3051 if (present_gnu_tree (gnat_entity))
3053 maybe_present = true;
3054 break;
3057 /* If this is a record subtype associated with a dispatch table,
3058 strip the suffix. This is necessary to make sure 2 different
3059 subtypes associated with the imported and exported views of a
3060 dispatch table are properly merged in LTO mode. */
3061 if (Is_Dispatch_Table_Entity (gnat_entity))
3063 char *p;
3064 Get_Encoded_Name (gnat_entity);
3065 p = strchr (Name_Buffer, '_');
3066 gcc_assert (p);
3067 strcpy (p+2, "dtS");
3068 gnu_entity_name = get_identifier (Name_Buffer);
3071 /* When the subtype has discriminants and these discriminants affect
3072 the initial shape it has inherited, factor them in. But for an
3073 Unchecked_Union (it must be an Itype), just return the type.
3074 We can't just test Is_Constrained because private subtypes without
3075 discriminants of types with discriminants with default expressions
3076 are Is_Constrained but aren't constrained! */
3077 if (IN (Ekind (gnat_base_type), Record_Kind)
3078 && !Is_Unchecked_Union (gnat_base_type)
3079 && !Is_For_Access_Subtype (gnat_entity)
3080 && Is_Constrained (gnat_entity)
3081 && Has_Discriminants (gnat_entity)
3082 && Present (Discriminant_Constraint (gnat_entity))
3083 && Stored_Constraint (gnat_entity) != No_Elist)
3085 VEC(subst_pair,heap) *gnu_subst_list
3086 = build_subst_list (gnat_entity, gnat_base_type, definition);
3087 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part, t;
3088 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3089 bool selected_variant = false;
3090 Entity_Id gnat_field;
3091 VEC(variant_desc,heap) *gnu_variant_list;
3093 gnu_type = make_node (RECORD_TYPE);
3094 TYPE_NAME (gnu_type) = gnu_entity_name;
3096 /* Set the size, alignment and alias set of the new type to
3097 match that of the old one, doing required substitutions. */
3098 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3099 gnu_subst_list);
3101 if (TYPE_IS_PADDING_P (gnu_base_type))
3102 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3103 else
3104 gnu_unpad_base_type = gnu_base_type;
3106 /* Look for a REP part in the base type. */
3107 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3109 /* Look for a variant part in the base type. */
3110 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3112 /* If there is a variant part, we must compute whether the
3113 constraints statically select a particular variant. If
3114 so, we simply drop the qualified union and flatten the
3115 list of fields. Otherwise we'll build a new qualified
3116 union for the variants that are still relevant. */
3117 if (gnu_variant_part)
3119 variant_desc *v;
3120 unsigned ix;
3122 gnu_variant_list
3123 = build_variant_list (TREE_TYPE (gnu_variant_part),
3124 gnu_subst_list, NULL);
3126 /* If all the qualifiers are unconditionally true, the
3127 innermost variant is statically selected. */
3128 selected_variant = true;
3129 FOR_EACH_VEC_ELT_REVERSE (variant_desc, gnu_variant_list,
3130 ix, v)
3131 if (!integer_onep (v->qual))
3133 selected_variant = false;
3134 break;
3137 /* Otherwise, create the new variants. */
3138 if (!selected_variant)
3139 FOR_EACH_VEC_ELT_REVERSE (variant_desc, gnu_variant_list,
3140 ix, v)
3142 tree old_variant = v->type;
3143 tree new_variant = make_node (RECORD_TYPE);
3144 TYPE_NAME (new_variant)
3145 = DECL_NAME (TYPE_NAME (old_variant));
3146 copy_and_substitute_in_size (new_variant, old_variant,
3147 gnu_subst_list);
3148 v->record = new_variant;
3151 else
3153 gnu_variant_list = NULL;
3154 selected_variant = false;
3157 gnu_pos_list
3158 = build_position_list (gnu_unpad_base_type,
3159 gnu_variant_list && !selected_variant,
3160 size_zero_node, bitsize_zero_node,
3161 BIGGEST_ALIGNMENT, NULL_TREE);
3163 for (gnat_field = First_Entity (gnat_entity);
3164 Present (gnat_field);
3165 gnat_field = Next_Entity (gnat_field))
3166 if ((Ekind (gnat_field) == E_Component
3167 || Ekind (gnat_field) == E_Discriminant)
3168 && !(Present (Corresponding_Discriminant (gnat_field))
3169 && Is_Tagged_Type (gnat_base_type))
3170 && Underlying_Type (Scope (Original_Record_Component
3171 (gnat_field)))
3172 == gnat_base_type)
3174 Name_Id gnat_name = Chars (gnat_field);
3175 Entity_Id gnat_old_field
3176 = Original_Record_Component (gnat_field);
3177 tree gnu_old_field
3178 = gnat_to_gnu_field_decl (gnat_old_field);
3179 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3180 tree gnu_field, gnu_field_type, gnu_size;
3181 tree gnu_cont_type, gnu_last = NULL_TREE;
3183 /* If the type is the same, retrieve the GCC type from the
3184 old field to take into account possible adjustments. */
3185 if (Etype (gnat_field) == Etype (gnat_old_field))
3186 gnu_field_type = TREE_TYPE (gnu_old_field);
3187 else
3188 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3190 /* If there was a component clause, the field types must be
3191 the same for the type and subtype, so copy the data from
3192 the old field to avoid recomputation here. Also if the
3193 field is justified modular and the optimization in
3194 gnat_to_gnu_field was applied. */
3195 if (Present (Component_Clause (gnat_old_field))
3196 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3197 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3198 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3199 == TREE_TYPE (gnu_old_field)))
3201 gnu_size = DECL_SIZE (gnu_old_field);
3202 gnu_field_type = TREE_TYPE (gnu_old_field);
3205 /* If the old field was packed and of constant size, we
3206 have to get the old size here, as it might differ from
3207 what the Etype conveys and the latter might overlap
3208 onto the following field. Try to arrange the type for
3209 possible better packing along the way. */
3210 else if (DECL_PACKED (gnu_old_field)
3211 && TREE_CODE (DECL_SIZE (gnu_old_field))
3212 == INTEGER_CST)
3214 gnu_size = DECL_SIZE (gnu_old_field);
3215 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
3216 && !TYPE_FAT_POINTER_P (gnu_field_type)
3217 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
3218 gnu_field_type
3219 = make_packable_type (gnu_field_type, true);
3222 else
3223 gnu_size = TYPE_SIZE (gnu_field_type);
3225 /* If the context of the old field is the base type or its
3226 REP part (if any), put the field directly in the new
3227 type; otherwise look up the context in the variant list
3228 and put the field either in the new type if there is a
3229 selected variant or in one of the new variants. */
3230 if (gnu_context == gnu_unpad_base_type
3231 || (gnu_rep_part
3232 && gnu_context == TREE_TYPE (gnu_rep_part)))
3233 gnu_cont_type = gnu_type;
3234 else
3236 variant_desc *v;
3237 unsigned ix;
3239 t = NULL_TREE;
3240 FOR_EACH_VEC_ELT_REVERSE (variant_desc,
3241 gnu_variant_list, ix, v)
3242 if (v->type == gnu_context)
3244 t = v->type;
3245 break;
3247 if (t)
3249 if (selected_variant)
3250 gnu_cont_type = gnu_type;
3251 else
3252 gnu_cont_type = v->record;
3254 else
3255 /* The front-end may pass us "ghost" components if
3256 it fails to recognize that a constrained subtype
3257 is statically constrained. Discard them. */
3258 continue;
3261 /* Now create the new field modeled on the old one. */
3262 gnu_field
3263 = create_field_decl_from (gnu_old_field, gnu_field_type,
3264 gnu_cont_type, gnu_size,
3265 gnu_pos_list, gnu_subst_list);
3267 /* Put it in one of the new variants directly. */
3268 if (gnu_cont_type != gnu_type)
3270 DECL_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3271 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3274 /* To match the layout crafted in components_to_record,
3275 if this is the _Tag or _Parent field, put it before
3276 any other fields. */
3277 else if (gnat_name == Name_uTag
3278 || gnat_name == Name_uParent)
3279 gnu_field_list = chainon (gnu_field_list, gnu_field);
3281 /* Similarly, if this is the _Controller field, put
3282 it before the other fields except for the _Tag or
3283 _Parent field. */
3284 else if (gnat_name == Name_uController && gnu_last)
3286 TREE_CHAIN (gnu_field) = TREE_CHAIN (gnu_last);
3287 TREE_CHAIN (gnu_last) = gnu_field;
3290 /* Otherwise, if this is a regular field, put it after
3291 the other fields. */
3292 else
3294 DECL_CHAIN (gnu_field) = gnu_field_list;
3295 gnu_field_list = gnu_field;
3296 if (!gnu_last)
3297 gnu_last = gnu_field;
3300 save_gnu_tree (gnat_field, gnu_field, false);
3303 /* If there is a variant list and no selected variant, we need
3304 to create the nest of variant parts from the old nest. */
3305 if (gnu_variant_list && !selected_variant)
3307 tree new_variant_part
3308 = create_variant_part_from (gnu_variant_part,
3309 gnu_variant_list, gnu_type,
3310 gnu_pos_list, gnu_subst_list);
3311 DECL_CHAIN (new_variant_part) = gnu_field_list;
3312 gnu_field_list = new_variant_part;
3315 /* Now go through the entities again looking for Itypes that
3316 we have not elaborated but should (e.g., Etypes of fields
3317 that have Original_Components). */
3318 for (gnat_field = First_Entity (gnat_entity);
3319 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3320 if ((Ekind (gnat_field) == E_Discriminant
3321 || Ekind (gnat_field) == E_Component)
3322 && !present_gnu_tree (Etype (gnat_field)))
3323 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3325 /* Do not emit debug info for the type yet since we're going to
3326 modify it below. */
3327 gnu_field_list = nreverse (gnu_field_list);
3328 finish_record_type (gnu_type, gnu_field_list, 2, false);
3330 /* See the E_Record_Type case for the rationale. */
3331 if (Is_By_Reference_Type (gnat_entity))
3332 SET_TYPE_MODE (gnu_type, BLKmode);
3333 else
3334 compute_record_mode (gnu_type);
3336 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3338 /* Fill in locations of fields. */
3339 annotate_rep (gnat_entity, gnu_type);
3341 /* If debugging information is being written for the type, write
3342 a record that shows what we are a subtype of and also make a
3343 variable that indicates our size, if still variable. */
3344 if (debug_info_p)
3346 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3347 tree gnu_unpad_base_name = TYPE_NAME (gnu_unpad_base_type);
3348 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3350 if (TREE_CODE (gnu_unpad_base_name) == TYPE_DECL)
3351 gnu_unpad_base_name = DECL_NAME (gnu_unpad_base_name);
3353 TYPE_NAME (gnu_subtype_marker)
3354 = create_concat_name (gnat_entity, "XVS");
3355 finish_record_type (gnu_subtype_marker,
3356 create_field_decl (gnu_unpad_base_name,
3357 build_reference_type
3358 (gnu_unpad_base_type),
3359 gnu_subtype_marker,
3360 NULL_TREE, NULL_TREE,
3361 0, 0),
3362 0, true);
3364 add_parallel_type (TYPE_STUB_DECL (gnu_type),
3365 gnu_subtype_marker);
3367 if (definition
3368 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3369 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3370 TYPE_SIZE_UNIT (gnu_subtype_marker)
3371 = create_var_decl (create_concat_name (gnat_entity,
3372 "XVZ"),
3373 NULL_TREE, sizetype, gnu_size_unit,
3374 false, false, false, false, NULL,
3375 gnat_entity);
3378 VEC_free (variant_desc, heap, gnu_variant_list);
3379 VEC_free (subst_pair, heap, gnu_subst_list);
3381 /* Now we can finalize it. */
3382 rest_of_record_type_compilation (gnu_type);
3385 /* Otherwise, go down all the components in the new type and make
3386 them equivalent to those in the base type. */
3387 else
3389 gnu_type = gnu_base_type;
3391 for (gnat_temp = First_Entity (gnat_entity);
3392 Present (gnat_temp);
3393 gnat_temp = Next_Entity (gnat_temp))
3394 if ((Ekind (gnat_temp) == E_Discriminant
3395 && !Is_Unchecked_Union (gnat_base_type))
3396 || Ekind (gnat_temp) == E_Component)
3397 save_gnu_tree (gnat_temp,
3398 gnat_to_gnu_field_decl
3399 (Original_Record_Component (gnat_temp)),
3400 false);
3403 break;
3405 case E_Access_Subprogram_Type:
3406 /* Use the special descriptor type for dispatch tables if needed,
3407 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3408 Note that we are only required to do so for static tables in
3409 order to be compatible with the C++ ABI, but Ada 2005 allows
3410 to extend library level tagged types at the local level so
3411 we do it in the non-static case as well. */
3412 if (TARGET_VTABLE_USES_DESCRIPTORS
3413 && Is_Dispatch_Table_Entity (gnat_entity))
3415 gnu_type = fdesc_type_node;
3416 gnu_size = TYPE_SIZE (gnu_type);
3417 break;
3420 /* ... fall through ... */
3422 case E_Anonymous_Access_Subprogram_Type:
3423 /* If we are not defining this entity, and we have incomplete
3424 entities being processed above us, make a dummy type and
3425 fill it in later. */
3426 if (!definition && defer_incomplete_level != 0)
3428 struct incomplete *p
3429 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3431 gnu_type
3432 = build_pointer_type
3433 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3434 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
3435 !Comes_From_Source (gnat_entity),
3436 debug_info_p, gnat_entity);
3437 this_made_decl = true;
3438 gnu_type = TREE_TYPE (gnu_decl);
3439 save_gnu_tree (gnat_entity, gnu_decl, false);
3440 saved = true;
3442 p->old_type = TREE_TYPE (gnu_type);
3443 p->full_type = Directly_Designated_Type (gnat_entity);
3444 p->next = defer_incomplete_list;
3445 defer_incomplete_list = p;
3446 break;
3449 /* ... fall through ... */
3451 case E_Allocator_Type:
3452 case E_Access_Type:
3453 case E_Access_Attribute_Type:
3454 case E_Anonymous_Access_Type:
3455 case E_General_Access_Type:
3457 /* The designated type and its equivalent type for gigi. */
3458 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3459 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3460 /* Whether it comes from a limited with. */
3461 bool is_from_limited_with
3462 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3463 && From_With_Type (gnat_desig_equiv));
3464 /* The "full view" of the designated type. If this is an incomplete
3465 entity from a limited with, treat its non-limited view as the full
3466 view. Otherwise, if this is an incomplete or private type, use the
3467 full view. In the former case, we might point to a private type,
3468 in which case, we need its full view. Also, we want to look at the
3469 actual type used for the representation, so this takes a total of
3470 three steps. */
3471 Entity_Id gnat_desig_full_direct_first
3472 = (is_from_limited_with
3473 ? Non_Limited_View (gnat_desig_equiv)
3474 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3475 ? Full_View (gnat_desig_equiv) : Empty));
3476 Entity_Id gnat_desig_full_direct
3477 = ((is_from_limited_with
3478 && Present (gnat_desig_full_direct_first)
3479 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3480 ? Full_View (gnat_desig_full_direct_first)
3481 : gnat_desig_full_direct_first);
3482 Entity_Id gnat_desig_full
3483 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3484 /* The type actually used to represent the designated type, either
3485 gnat_desig_full or gnat_desig_equiv. */
3486 Entity_Id gnat_desig_rep;
3487 /* True if this is a pointer to an unconstrained array. */
3488 bool is_unconstrained_array;
3489 /* We want to know if we'll be seeing the freeze node for any
3490 incomplete type we may be pointing to. */
3491 bool in_main_unit
3492 = (Present (gnat_desig_full)
3493 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3494 : In_Extended_Main_Code_Unit (gnat_desig_type));
3495 /* True if we make a dummy type here. */
3496 bool made_dummy = false;
3497 /* True if the dummy type is a fat pointer. */
3498 bool got_fat_p = false;
3499 /* The mode to be used for the pointer type. */
3500 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3501 /* The GCC type used for the designated type. */
3502 tree gnu_desig_type = NULL_TREE;
3504 if (!targetm.valid_pointer_mode (p_mode))
3505 p_mode = ptr_mode;
3507 /* If either the designated type or its full view is an unconstrained
3508 array subtype, replace it with the type it's a subtype of. This
3509 avoids problems with multiple copies of unconstrained array types.
3510 Likewise, if the designated type is a subtype of an incomplete
3511 record type, use the parent type to avoid order of elaboration
3512 issues. This can lose some code efficiency, but there is no
3513 alternative. */
3514 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3515 && !Is_Constrained (gnat_desig_equiv))
3516 gnat_desig_equiv = Etype (gnat_desig_equiv);
3517 if (Present (gnat_desig_full)
3518 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3519 && !Is_Constrained (gnat_desig_full))
3520 || (Ekind (gnat_desig_full) == E_Record_Subtype
3521 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3522 gnat_desig_full = Etype (gnat_desig_full);
3524 /* Set the type that's actually the representation of the designated
3525 type and also flag whether we have a unconstrained array. */
3526 gnat_desig_rep
3527 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3528 is_unconstrained_array
3529 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3531 /* If we are pointing to an incomplete type whose completion is an
3532 unconstrained array, make a fat pointer type. The two types in our
3533 fields will be pointers to dummy nodes and will be replaced in
3534 update_pointer_to. Similarly, if the type itself is a dummy type or
3535 an unconstrained array. Also make a dummy TYPE_OBJECT_RECORD_TYPE
3536 in case we have any thin pointers to it. */
3537 if (is_unconstrained_array
3538 && (Present (gnat_desig_full)
3539 || (present_gnu_tree (gnat_desig_equiv)
3540 && TYPE_IS_DUMMY_P
3541 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3542 || (!in_main_unit
3543 && defer_incomplete_level != 0
3544 && !present_gnu_tree (gnat_desig_equiv))
3545 || (in_main_unit
3546 && is_from_limited_with
3547 && Present (Freeze_Node (gnat_desig_equiv)))))
3549 if (present_gnu_tree (gnat_desig_rep))
3550 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3551 else
3553 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3554 /* Show the dummy we get will be a fat pointer. */
3555 got_fat_p = made_dummy = true;
3558 /* If the call above got something that has a pointer, the pointer
3559 is our type. This could have happened either because the type
3560 was elaborated or because somebody else executed the code. */
3561 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3562 if (!gnu_type)
3564 tree gnu_template_type = make_node (RECORD_TYPE);
3565 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
3566 tree gnu_array_type = make_node (ENUMERAL_TYPE);
3567 tree gnu_ptr_array = build_pointer_type (gnu_array_type);
3568 tree fields;
3570 TYPE_NAME (gnu_template_type)
3571 = create_concat_name (gnat_desig_equiv, "XUB");
3572 TYPE_DUMMY_P (gnu_template_type) = 1;
3574 TYPE_NAME (gnu_array_type)
3575 = create_concat_name (gnat_desig_equiv, "XUA");
3576 TYPE_DUMMY_P (gnu_array_type) = 1;
3578 gnu_type = make_node (RECORD_TYPE);
3579 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_desig_type);
3580 TYPE_POINTER_TO (gnu_desig_type) = gnu_type;
3582 fields
3583 = create_field_decl (get_identifier ("P_ARRAY"),
3584 gnu_ptr_array, gnu_type,
3585 NULL_TREE, NULL_TREE, 0, 0);
3586 DECL_CHAIN (fields)
3587 = create_field_decl (get_identifier ("P_BOUNDS"),
3588 gnu_ptr_template, gnu_type,
3589 NULL_TREE, NULL_TREE, 0, 0);
3591 /* Make sure we can place this into a register. */
3592 TYPE_ALIGN (gnu_type)
3593 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
3594 TYPE_FAT_POINTER_P (gnu_type) = 1;
3596 /* Do not emit debug info for this record type since the types
3597 of its fields are incomplete. */
3598 finish_record_type (gnu_type, fields, 0, false);
3600 TYPE_OBJECT_RECORD_TYPE (gnu_desig_type)
3601 = make_node (RECORD_TYPE);
3602 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type))
3603 = create_concat_name (gnat_desig_equiv, "XUT");
3604 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type)) = 1;
3608 /* If we already know what the full type is, use it. */
3609 else if (Present (gnat_desig_full)
3610 && present_gnu_tree (gnat_desig_full))
3611 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3613 /* Get the type of the thing we are to point to and build a pointer to
3614 it. If it is a reference to an incomplete or private type with a
3615 full view that is a record, make a dummy type node and get the
3616 actual type later when we have verified it is safe. */
3617 else if ((!in_main_unit
3618 && !present_gnu_tree (gnat_desig_equiv)
3619 && Present (gnat_desig_full)
3620 && !present_gnu_tree (gnat_desig_full)
3621 && Is_Record_Type (gnat_desig_full))
3622 /* Likewise if we are pointing to a record or array and we are
3623 to defer elaborating incomplete types. We do this as this
3624 access type may be the full view of a private type. Note
3625 that the unconstrained array case is handled above. */
3626 || ((!in_main_unit || imported_p)
3627 && defer_incomplete_level != 0
3628 && !present_gnu_tree (gnat_desig_equiv)
3629 && (Is_Record_Type (gnat_desig_rep)
3630 || Is_Array_Type (gnat_desig_rep)))
3631 /* If this is a reference from a limited_with type back to our
3632 main unit and there's a freeze node for it, either we have
3633 already processed the declaration and made the dummy type,
3634 in which case we just reuse the latter, or we have not yet,
3635 in which case we make the dummy type and it will be reused
3636 when the declaration is finally processed. In both cases,
3637 the pointer eventually created below will be automatically
3638 adjusted when the freeze node is processed. Note that the
3639 unconstrained array case is handled above. */
3640 || (in_main_unit
3641 && is_from_limited_with
3642 && Present (Freeze_Node (gnat_desig_rep))))
3644 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3645 made_dummy = true;
3648 /* Otherwise handle the case of a pointer to itself. */
3649 else if (gnat_desig_equiv == gnat_entity)
3651 gnu_type
3652 = build_pointer_type_for_mode (void_type_node, p_mode,
3653 No_Strict_Aliasing (gnat_entity));
3654 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3657 /* If expansion is disabled, the equivalent type of a concurrent type
3658 is absent, so build a dummy pointer type. */
3659 else if (type_annotate_only && No (gnat_desig_equiv))
3660 gnu_type = ptr_void_type_node;
3662 /* Finally, handle the default case where we can just elaborate our
3663 designated type. */
3664 else
3665 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3667 /* It is possible that a call to gnat_to_gnu_type above resolved our
3668 type. If so, just return it. */
3669 if (present_gnu_tree (gnat_entity))
3671 maybe_present = true;
3672 break;
3675 /* If we have not done it yet, build the pointer type the usual way. */
3676 if (!gnu_type)
3678 /* Modify the designated type if we are pointing only to constant
3679 objects, but don't do it for unconstrained arrays. */
3680 if (Is_Access_Constant (gnat_entity)
3681 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3683 gnu_desig_type
3684 = build_qualified_type
3685 (gnu_desig_type,
3686 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3688 /* Some extra processing is required if we are building a
3689 pointer to an incomplete type (in the GCC sense). We might
3690 have such a type if we just made a dummy, or directly out
3691 of the call to gnat_to_gnu_type above if we are processing
3692 an access type for a record component designating the
3693 record type itself. */
3694 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3696 /* We must ensure that the pointer to variant we make will
3697 be processed by update_pointer_to when the initial type
3698 is completed. Pretend we made a dummy and let further
3699 processing act as usual. */
3700 made_dummy = true;
3702 /* We must ensure that update_pointer_to will not retrieve
3703 the dummy variant when building a properly qualified
3704 version of the complete type. We take advantage of the
3705 fact that get_qualified_type is requiring TYPE_NAMEs to
3706 match to influence build_qualified_type and then also
3707 update_pointer_to here. */
3708 TYPE_NAME (gnu_desig_type)
3709 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3713 gnu_type
3714 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3715 No_Strict_Aliasing (gnat_entity));
3718 /* If we are not defining this object and we have made a dummy pointer,
3719 save our current definition, evaluate the actual type, and replace
3720 the tentative type we made with the actual one. If we are to defer
3721 actually looking up the actual type, make an entry in the deferred
3722 list. If this is from a limited with, we have to defer to the end
3723 of the current spec in two cases: first if the designated type is
3724 in the current unit and second if the access type itself is. */
3725 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3727 bool is_from_limited_with_in_main_unit
3728 = (is_from_limited_with
3729 && (in_main_unit
3730 || In_Extended_Main_Code_Unit (gnat_entity)));
3731 tree gnu_old_desig_type
3732 = TYPE_IS_FAT_POINTER_P (gnu_type)
3733 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3735 if (esize == POINTER_SIZE
3736 && (got_fat_p || TYPE_IS_FAT_POINTER_P (gnu_type)))
3737 gnu_type
3738 = build_pointer_type
3739 (TYPE_OBJECT_RECORD_TYPE
3740 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3742 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
3743 !Comes_From_Source (gnat_entity),
3744 debug_info_p, gnat_entity);
3745 this_made_decl = true;
3746 gnu_type = TREE_TYPE (gnu_decl);
3747 save_gnu_tree (gnat_entity, gnu_decl, false);
3748 saved = true;
3750 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3751 update gnu_old_desig_type directly, in which case it will not be
3752 a dummy type any more when we get into update_pointer_to.
3754 This can happen e.g. when the designated type is a record type,
3755 because their elaboration starts with an initial node from
3756 make_dummy_type, which may be the same node as the one we got.
3758 Besides, variants of this non-dummy type might have been created
3759 along the way. update_pointer_to is expected to properly take
3760 care of those situations. */
3761 if (defer_incomplete_level == 0
3762 && !is_from_limited_with_in_main_unit)
3763 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
3764 gnat_to_gnu_type (gnat_desig_equiv));
3765 else
3767 struct incomplete *p = XNEW (struct incomplete);
3768 struct incomplete **head
3769 = (is_from_limited_with_in_main_unit
3770 ? &defer_limited_with : &defer_incomplete_list);
3771 p->old_type = gnu_old_desig_type;
3772 p->full_type = gnat_desig_equiv;
3773 p->next = *head;
3774 *head = p;
3778 break;
3780 case E_Access_Protected_Subprogram_Type:
3781 case E_Anonymous_Access_Protected_Subprogram_Type:
3782 if (type_annotate_only && No (gnat_equiv_type))
3783 gnu_type = ptr_void_type_node;
3784 else
3786 /* The run-time representation is the equivalent type. */
3787 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3788 maybe_present = true;
3791 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3792 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3793 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3794 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3795 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3796 NULL_TREE, 0);
3798 break;
3800 case E_Access_Subtype:
3802 /* We treat this as identical to its base type; any constraint is
3803 meaningful only to the front end.
3805 The designated type must be elaborated as well, if it does
3806 not have its own freeze node. Designated (sub)types created
3807 for constrained components of records with discriminants are
3808 not frozen by the front end and thus not elaborated by gigi,
3809 because their use may appear before the base type is frozen,
3810 and because it is not clear that they are needed anywhere in
3811 Gigi. With the current model, there is no correct place where
3812 they could be elaborated. */
3814 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3815 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3816 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3817 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3818 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3820 /* If we are not defining this entity, and we have incomplete
3821 entities being processed above us, make a dummy type and
3822 elaborate it later. */
3823 if (!definition && defer_incomplete_level != 0)
3825 struct incomplete *p
3826 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3827 tree gnu_ptr_type
3828 = build_pointer_type
3829 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3831 p->old_type = TREE_TYPE (gnu_ptr_type);
3832 p->full_type = Directly_Designated_Type (gnat_entity);
3833 p->next = defer_incomplete_list;
3834 defer_incomplete_list = p;
3836 else if (!IN (Ekind (Base_Type
3837 (Directly_Designated_Type (gnat_entity))),
3838 Incomplete_Or_Private_Kind))
3839 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3840 NULL_TREE, 0);
3843 maybe_present = true;
3844 break;
3846 /* Subprogram Entities
3848 The following access functions are defined for subprograms (functions
3849 or procedures):
3851 First_Formal The first formal parameter.
3852 Is_Imported Indicates that the subprogram has appeared in
3853 an INTERFACE or IMPORT pragma. For now we
3854 assume that the external language is C.
3855 Is_Exported Likewise but for an EXPORT pragma.
3856 Is_Inlined True if the subprogram is to be inlined.
3858 In addition for function subprograms we have:
3860 Etype Return type of the function.
3862 Each parameter is first checked by calling must_pass_by_ref on its
3863 type to determine if it is passed by reference. For parameters which
3864 are copied in, if they are Ada In Out or Out parameters, their return
3865 value becomes part of a record which becomes the return type of the
3866 function (C function - note that this applies only to Ada procedures
3867 so there is no Ada return type). Additional code to store back the
3868 parameters will be generated on the caller side. This transformation
3869 is done here, not in the front-end.
3871 The intended result of the transformation can be seen from the
3872 equivalent source rewritings that follow:
3874 struct temp {int a,b};
3875 procedure P (A,B: In Out ...) is temp P (int A,B)
3876 begin {
3877 .. ..
3878 end P; return {A,B};
3881 temp t;
3882 P(X,Y); t = P(X,Y);
3883 X = t.a , Y = t.b;
3885 For subprogram types we need to perform mainly the same conversions to
3886 GCC form that are needed for procedures and function declarations. The
3887 only difference is that at the end, we make a type declaration instead
3888 of a function declaration. */
3890 case E_Subprogram_Type:
3891 case E_Function:
3892 case E_Procedure:
3894 /* The first GCC parameter declaration (a PARM_DECL node). The
3895 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3896 actually is the head of this parameter list. */
3897 tree gnu_param_list = NULL_TREE;
3898 /* Likewise for the stub associated with an exported procedure. */
3899 tree gnu_stub_param_list = NULL_TREE;
3900 /* The type returned by a function. If the subprogram is a procedure
3901 this type should be void_type_node. */
3902 tree gnu_return_type = void_type_node;
3903 /* List of fields in return type of procedure with copy-in copy-out
3904 parameters. */
3905 tree gnu_field_list = NULL_TREE;
3906 /* Non-null for subprograms containing parameters passed by copy-in
3907 copy-out (Ada In Out or Out parameters not passed by reference),
3908 in which case it is the list of nodes used to specify the values
3909 of the In Out/Out parameters that are returned as a record upon
3910 procedure return. The TREE_PURPOSE of an element of this list is
3911 a field of the record and the TREE_VALUE is the PARM_DECL
3912 corresponding to that field. This list will be saved in the
3913 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3914 tree gnu_cico_list = NULL_TREE;
3915 /* If an import pragma asks to map this subprogram to a GCC builtin,
3916 this is the builtin DECL node. */
3917 tree gnu_builtin_decl = NULL_TREE;
3918 /* For the stub associated with an exported procedure. */
3919 tree gnu_stub_type = NULL_TREE, gnu_stub_name = NULL_TREE;
3920 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
3921 Entity_Id gnat_param;
3922 bool inline_flag = Is_Inlined (gnat_entity);
3923 bool public_flag = Is_Public (gnat_entity) || imported_p;
3924 bool extern_flag
3925 = (Is_Public (gnat_entity) && !definition) || imported_p;
3927 /* The semantics of "pure" in Ada essentially matches that of "const"
3928 in the back-end. In particular, both properties are orthogonal to
3929 the "nothrow" property if the EH circuitry is explicit in the
3930 internal representation of the back-end. If we are to completely
3931 hide the EH circuitry from it, we need to declare that calls to pure
3932 Ada subprograms that can throw have side effects since they can
3933 trigger an "abnormal" transfer of control flow; thus they can be
3934 neither "const" nor "pure" in the back-end sense. */
3935 bool const_flag
3936 = (Exception_Mechanism == Back_End_Exceptions
3937 && Is_Pure (gnat_entity));
3939 bool volatile_flag = No_Return (gnat_entity);
3940 bool return_by_direct_ref_p = false;
3941 bool return_by_invisi_ref_p = false;
3942 bool return_unconstrained_p = false;
3943 bool has_copy_in_out = false;
3944 bool has_stub = false;
3945 int parmnum;
3947 /* A parameter may refer to this type, so defer completion of any
3948 incomplete types. */
3949 if (kind == E_Subprogram_Type && !definition)
3951 defer_incomplete_level++;
3952 this_deferred = true;
3955 /* If the subprogram has an alias, it is probably inherited, so
3956 we can use the original one. If the original "subprogram"
3957 is actually an enumeration literal, it may be the first use
3958 of its type, so we must elaborate that type now. */
3959 if (Present (Alias (gnat_entity)))
3961 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3962 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3964 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3965 gnu_expr, 0);
3967 /* Elaborate any Itypes in the parameters of this entity. */
3968 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3969 Present (gnat_temp);
3970 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3971 if (Is_Itype (Etype (gnat_temp)))
3972 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3974 break;
3977 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3978 corresponding DECL node. Proper generation of calls later on need
3979 proper parameter associations so we don't "break;" here. */
3980 if (Convention (gnat_entity) == Convention_Intrinsic
3981 && Present (Interface_Name (gnat_entity)))
3983 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3985 /* Unability to find the builtin decl most often indicates a
3986 genuine mistake, but imports of unregistered intrinsics are
3987 sometimes issued on purpose to allow hooking in alternate
3988 bodies. We post a warning conditioned on Wshadow in this case,
3989 to let developers be notified on demand without risking false
3990 positives with common default sets of options. */
3992 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
3993 post_error ("?gcc intrinsic not found for&!", gnat_entity);
3996 /* ??? What if we don't find the builtin node above ? warn ? err ?
3997 In the current state we neither warn nor err, and calls will just
3998 be handled as for regular subprograms. */
4000 if (kind == E_Function || kind == E_Subprogram_Type)
4001 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
4003 /* If this function returns by reference, make the actual return
4004 type of this function the pointer and mark the decl. */
4005 if (Returns_By_Ref (gnat_entity))
4007 gnu_return_type = build_pointer_type (gnu_return_type);
4008 return_by_direct_ref_p = true;
4011 /* If the Mechanism is By_Reference, ensure this function uses the
4012 target's by-invisible-reference mechanism, which may not be the
4013 same as above (e.g. it might be passing an extra parameter).
4015 Prior to GCC 4, this was handled by just setting TREE_ADDRESSABLE
4016 on the result type. Everything required to pass by invisible
4017 reference using the target's mechanism (e.g. an extra parameter)
4018 was handled at RTL expansion time.
4020 This doesn't work with GCC 4 any more for several reasons. First,
4021 the gimplification process might need to create temporaries of this
4022 type and the gimplifier ICEs on such attempts; that's why the flag
4023 is now set on the function type instead. Second, the middle-end
4024 now also relies on a different attribute, DECL_BY_REFERENCE on the
4025 RESULT_DECL, and expects the by-invisible-reference-ness to be made
4026 explicit in the function body. */
4027 else if (kind == E_Function && Mechanism (gnat_entity) == By_Reference)
4028 return_by_invisi_ref_p = true;
4030 /* If we are supposed to return an unconstrained array, actually return
4031 a fat pointer and make a note of that. */
4032 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
4034 gnu_return_type = TREE_TYPE (gnu_return_type);
4035 return_unconstrained_p = true;
4038 /* If the type requires a transient scope, the result is allocated
4039 on the secondary stack, so the result type of the function is
4040 just a pointer. */
4041 else if (Requires_Transient_Scope (Etype (gnat_entity)))
4043 gnu_return_type = build_pointer_type (gnu_return_type);
4044 return_unconstrained_p = true;
4047 /* If the type is a padded type and the underlying type would not
4048 be passed by reference or this function has a foreign convention,
4049 return the underlying type. */
4050 else if (TYPE_IS_PADDING_P (gnu_return_type)
4051 && (!default_pass_by_ref (TREE_TYPE
4052 (TYPE_FIELDS (gnu_return_type)))
4053 || Has_Foreign_Convention (gnat_entity)))
4054 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
4056 /* If the return type is unconstrained, that means it must have a
4057 maximum size. Use the padded type as the effective return type.
4058 And ensure the function uses the target's by-invisible-reference
4059 mechanism to avoid copying too much data when it returns. */
4060 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
4062 gnu_return_type
4063 = maybe_pad_type (gnu_return_type,
4064 max_size (TYPE_SIZE (gnu_return_type), true),
4065 0, gnat_entity, false, false, false, true);
4066 return_by_invisi_ref_p = true;
4069 /* If the return type has a size that overflows, we cannot have
4070 a function that returns that type. This usage doesn't make
4071 sense anyway, so give an error here. */
4072 if (TYPE_SIZE_UNIT (gnu_return_type)
4073 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type))
4074 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
4076 post_error ("cannot return type whose size overflows",
4077 gnat_entity);
4078 gnu_return_type = copy_node (gnu_return_type);
4079 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4080 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4081 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4082 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4085 /* Look at all our parameters and get the type of
4086 each. While doing this, build a copy-out structure if
4087 we need one. */
4089 /* Loop over the parameters and get their associated GCC tree.
4090 While doing this, build a copy-out structure if we need one. */
4091 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4092 Present (gnat_param);
4093 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4095 tree gnu_param_name = get_entity_name (gnat_param);
4096 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
4097 tree gnu_param, gnu_field;
4098 bool copy_in_copy_out = false;
4099 Mechanism_Type mech = Mechanism (gnat_param);
4101 /* Builtins are expanded inline and there is no real call sequence
4102 involved. So the type expected by the underlying expander is
4103 always the type of each argument "as is". */
4104 if (gnu_builtin_decl)
4105 mech = By_Copy;
4106 /* Handle the first parameter of a valued procedure specially. */
4107 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4108 mech = By_Copy_Return;
4109 /* Otherwise, see if a Mechanism was supplied that forced this
4110 parameter to be passed one way or another. */
4111 else if (mech == Default
4112 || mech == By_Copy || mech == By_Reference)
4114 else if (By_Descriptor_Last <= mech && mech <= By_Descriptor)
4115 mech = By_Descriptor;
4117 else if (By_Short_Descriptor_Last <= mech &&
4118 mech <= By_Short_Descriptor)
4119 mech = By_Short_Descriptor;
4121 else if (mech > 0)
4123 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4124 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4125 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4126 mech))
4127 mech = By_Reference;
4128 else
4129 mech = By_Copy;
4131 else
4133 post_error ("unsupported mechanism for&", gnat_param);
4134 mech = Default;
4137 gnu_param
4138 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4139 Has_Foreign_Convention (gnat_entity),
4140 &copy_in_copy_out);
4142 /* We are returned either a PARM_DECL or a type if no parameter
4143 needs to be passed; in either case, adjust the type. */
4144 if (DECL_P (gnu_param))
4145 gnu_param_type = TREE_TYPE (gnu_param);
4146 else
4148 gnu_param_type = gnu_param;
4149 gnu_param = NULL_TREE;
4152 /* The failure of this assertion will very likely come from an
4153 order of elaboration issue for the type of the parameter. */
4154 gcc_assert (kind == E_Subprogram_Type
4155 || !TYPE_IS_DUMMY_P (gnu_param_type));
4157 if (gnu_param)
4159 /* If it's an exported subprogram, we build a parameter list
4160 in parallel, in case we need to emit a stub for it. */
4161 if (Is_Exported (gnat_entity))
4163 gnu_stub_param_list
4164 = chainon (gnu_param, gnu_stub_param_list);
4165 /* Change By_Descriptor parameter to By_Reference for
4166 the internal version of an exported subprogram. */
4167 if (mech == By_Descriptor || mech == By_Short_Descriptor)
4169 gnu_param
4170 = gnat_to_gnu_param (gnat_param, By_Reference,
4171 gnat_entity, false,
4172 &copy_in_copy_out);
4173 has_stub = true;
4175 else
4176 gnu_param = copy_node (gnu_param);
4179 gnu_param_list = chainon (gnu_param, gnu_param_list);
4180 Sloc_to_locus (Sloc (gnat_param),
4181 &DECL_SOURCE_LOCATION (gnu_param));
4182 save_gnu_tree (gnat_param, gnu_param, false);
4184 /* If a parameter is a pointer, this function may modify
4185 memory through it and thus shouldn't be considered
4186 a const function. Also, the memory may be modified
4187 between two calls, so they can't be CSE'ed. The latter
4188 case also handles by-ref parameters. */
4189 if (POINTER_TYPE_P (gnu_param_type)
4190 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4191 const_flag = false;
4194 if (copy_in_copy_out)
4196 if (!has_copy_in_out)
4198 gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
4199 gnu_return_type = make_node (RECORD_TYPE);
4200 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4201 /* Set a default alignment to speed up accesses. */
4202 TYPE_ALIGN (gnu_return_type)
4203 = get_mode_alignment (ptr_mode);
4204 has_copy_in_out = true;
4207 gnu_field
4208 = create_field_decl (gnu_param_name, gnu_param_type,
4209 gnu_return_type, NULL_TREE, NULL_TREE,
4210 0, 0);
4211 Sloc_to_locus (Sloc (gnat_param),
4212 &DECL_SOURCE_LOCATION (gnu_field));
4213 DECL_CHAIN (gnu_field) = gnu_field_list;
4214 gnu_field_list = gnu_field;
4215 gnu_cico_list
4216 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4220 /* Do not compute record for out parameters if subprogram is
4221 stubbed since structures are incomplete for the back-end. */
4222 if (gnu_field_list && Convention (gnat_entity) != Convention_Stubbed)
4223 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4224 0, debug_info_p);
4226 /* If we have a CICO list but it has only one entry, we convert
4227 this function into a function that simply returns that one
4228 object. */
4229 if (list_length (gnu_cico_list) == 1)
4230 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4232 if (Has_Stdcall_Convention (gnat_entity))
4233 prepend_one_attribute_to
4234 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4235 get_identifier ("stdcall"), NULL_TREE,
4236 gnat_entity);
4238 /* If we should request stack realignment for a foreign convention
4239 subprogram, do so. Note that this applies to task entry points in
4240 particular. */
4241 if (FOREIGN_FORCE_REALIGN_STACK
4242 && Has_Foreign_Convention (gnat_entity))
4243 prepend_one_attribute_to
4244 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4245 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4246 gnat_entity);
4248 /* The lists have been built in reverse. */
4249 gnu_param_list = nreverse (gnu_param_list);
4250 if (has_stub)
4251 gnu_stub_param_list = nreverse (gnu_stub_param_list);
4252 gnu_cico_list = nreverse (gnu_cico_list);
4254 if (kind == E_Function)
4255 Set_Mechanism (gnat_entity, return_unconstrained_p
4256 || return_by_direct_ref_p
4257 || return_by_invisi_ref_p
4258 ? By_Reference : By_Copy);
4259 gnu_type
4260 = create_subprog_type (gnu_return_type, gnu_param_list,
4261 gnu_cico_list, return_unconstrained_p,
4262 return_by_direct_ref_p,
4263 return_by_invisi_ref_p);
4265 if (has_stub)
4266 gnu_stub_type
4267 = create_subprog_type (gnu_return_type, gnu_stub_param_list,
4268 gnu_cico_list, return_unconstrained_p,
4269 return_by_direct_ref_p,
4270 return_by_invisi_ref_p);
4272 /* A subprogram (something that doesn't return anything) shouldn't
4273 be considered const since there would be no reason for such a
4274 subprogram. Note that procedures with Out (or In Out) parameters
4275 have already been converted into a function with a return type. */
4276 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4277 const_flag = false;
4279 gnu_type
4280 = build_qualified_type (gnu_type,
4281 TYPE_QUALS (gnu_type)
4282 | (TYPE_QUAL_CONST * const_flag)
4283 | (TYPE_QUAL_VOLATILE * volatile_flag));
4285 if (has_stub)
4286 gnu_stub_type
4287 = build_qualified_type (gnu_stub_type,
4288 TYPE_QUALS (gnu_stub_type)
4289 | (TYPE_QUAL_CONST * const_flag)
4290 | (TYPE_QUAL_VOLATILE * volatile_flag));
4292 /* If we have a builtin decl for that function, use it. Check if the
4293 profiles are compatible and warn if they are not. The checker is
4294 expected to post extra diagnostics in this case. */
4295 if (gnu_builtin_decl)
4297 intrin_binding_t inb;
4299 inb.gnat_entity = gnat_entity;
4300 inb.ada_fntype = gnu_type;
4301 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4303 if (!intrin_profiles_compatible_p (&inb))
4304 post_error
4305 ("?profile of& doesn''t match the builtin it binds!",
4306 gnat_entity);
4308 gnu_decl = gnu_builtin_decl;
4309 gnu_type = TREE_TYPE (gnu_builtin_decl);
4310 break;
4313 /* If there was no specified Interface_Name and the external and
4314 internal names of the subprogram are the same, only use the
4315 internal name to allow disambiguation of nested subprograms. */
4316 if (No (Interface_Name (gnat_entity))
4317 && gnu_ext_name == gnu_entity_name)
4318 gnu_ext_name = NULL_TREE;
4320 /* If we are defining the subprogram and it has an Address clause
4321 we must get the address expression from the saved GCC tree for the
4322 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4323 the address expression here since the front-end has guaranteed
4324 in that case that the elaboration has no effects. If there is
4325 an Address clause and we are not defining the object, just
4326 make it a constant. */
4327 if (Present (Address_Clause (gnat_entity)))
4329 tree gnu_address = NULL_TREE;
4331 if (definition)
4332 gnu_address
4333 = (present_gnu_tree (gnat_entity)
4334 ? get_gnu_tree (gnat_entity)
4335 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4337 save_gnu_tree (gnat_entity, NULL_TREE, false);
4339 /* Convert the type of the object to a reference type that can
4340 alias everything as per 13.3(19). */
4341 gnu_type
4342 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4343 if (gnu_address)
4344 gnu_address = convert (gnu_type, gnu_address);
4346 gnu_decl
4347 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4348 gnu_address, false, Is_Public (gnat_entity),
4349 extern_flag, false, NULL, gnat_entity);
4350 DECL_BY_REF_P (gnu_decl) = 1;
4353 else if (kind == E_Subprogram_Type)
4354 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
4355 !Comes_From_Source (gnat_entity),
4356 debug_info_p, gnat_entity);
4357 else
4359 if (has_stub)
4361 gnu_stub_name = gnu_ext_name;
4362 gnu_ext_name = create_concat_name (gnat_entity, "internal");
4363 public_flag = false;
4366 gnu_decl = create_subprog_decl (gnu_entity_name, gnu_ext_name,
4367 gnu_type, gnu_param_list,
4368 inline_flag, public_flag,
4369 extern_flag, attr_list,
4370 gnat_entity);
4371 if (has_stub)
4373 tree gnu_stub_decl
4374 = create_subprog_decl (gnu_entity_name, gnu_stub_name,
4375 gnu_stub_type, gnu_stub_param_list,
4376 inline_flag, true,
4377 extern_flag, attr_list,
4378 gnat_entity);
4379 SET_DECL_FUNCTION_STUB (gnu_decl, gnu_stub_decl);
4382 /* This is unrelated to the stub built right above. */
4383 DECL_STUBBED_P (gnu_decl)
4384 = Convention (gnat_entity) == Convention_Stubbed;
4387 break;
4389 case E_Incomplete_Type:
4390 case E_Incomplete_Subtype:
4391 case E_Private_Type:
4392 case E_Private_Subtype:
4393 case E_Limited_Private_Type:
4394 case E_Limited_Private_Subtype:
4395 case E_Record_Type_With_Private:
4396 case E_Record_Subtype_With_Private:
4398 /* Get the "full view" of this entity. If this is an incomplete
4399 entity from a limited with, treat its non-limited view as the
4400 full view. Otherwise, use either the full view or the underlying
4401 full view, whichever is present. This is used in all the tests
4402 below. */
4403 Entity_Id full_view
4404 = (IN (kind, Incomplete_Kind) && From_With_Type (gnat_entity))
4405 ? Non_Limited_View (gnat_entity)
4406 : Present (Full_View (gnat_entity))
4407 ? Full_View (gnat_entity)
4408 : Underlying_Full_View (gnat_entity);
4410 /* If this is an incomplete type with no full view, it must be a Taft
4411 Amendment type, in which case we return a dummy type. Otherwise,
4412 just get the type from its Etype. */
4413 if (No (full_view))
4415 if (kind == E_Incomplete_Type)
4417 gnu_type = make_dummy_type (gnat_entity);
4418 gnu_decl = TYPE_STUB_DECL (gnu_type);
4420 else
4422 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4423 NULL_TREE, 0);
4424 maybe_present = true;
4426 break;
4429 /* If we already made a type for the full view, reuse it. */
4430 else if (present_gnu_tree (full_view))
4432 gnu_decl = get_gnu_tree (full_view);
4433 break;
4436 /* Otherwise, if we are not defining the type now, get the type
4437 from the full view. But always get the type from the full view
4438 for define on use types, since otherwise we won't see them! */
4439 else if (!definition
4440 || (Is_Itype (full_view)
4441 && No (Freeze_Node (gnat_entity)))
4442 || (Is_Itype (gnat_entity)
4443 && No (Freeze_Node (full_view))))
4445 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4446 maybe_present = true;
4447 break;
4450 /* For incomplete types, make a dummy type entry which will be
4451 replaced later. Save it as the full declaration's type so
4452 we can do any needed updates when we see it. */
4453 gnu_type = make_dummy_type (gnat_entity);
4454 gnu_decl = TYPE_STUB_DECL (gnu_type);
4455 save_gnu_tree (full_view, gnu_decl, 0);
4456 break;
4459 case E_Class_Wide_Type:
4460 /* Class-wide types are always transformed into their root type. */
4461 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4462 maybe_present = true;
4463 break;
4465 case E_Task_Type:
4466 case E_Task_Subtype:
4467 case E_Protected_Type:
4468 case E_Protected_Subtype:
4469 /* Concurrent types are always transformed into their record type. */
4470 if (type_annotate_only && No (gnat_equiv_type))
4471 gnu_type = void_type_node;
4472 else
4473 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4474 maybe_present = true;
4475 break;
4477 case E_Label:
4478 gnu_decl = create_label_decl (gnu_entity_name);
4479 break;
4481 case E_Block:
4482 case E_Loop:
4483 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4484 we've already saved it, so we don't try to. */
4485 gnu_decl = error_mark_node;
4486 saved = true;
4487 break;
4489 default:
4490 gcc_unreachable ();
4493 /* If we had a case where we evaluated another type and it might have
4494 defined this one, handle it here. */
4495 if (maybe_present && present_gnu_tree (gnat_entity))
4497 gnu_decl = get_gnu_tree (gnat_entity);
4498 saved = true;
4501 /* If we are processing a type and there is either no decl for it or
4502 we just made one, do some common processing for the type, such as
4503 handling alignment and possible padding. */
4504 if (is_type && (!gnu_decl || this_made_decl))
4506 /* Tell the middle-end that objects of tagged types are guaranteed to
4507 be properly aligned. This is necessary because conversions to the
4508 class-wide type are translated into conversions to the root type,
4509 which can be less aligned than some of its derived types. */
4510 if (Is_Tagged_Type (gnat_entity)
4511 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4512 TYPE_ALIGN_OK (gnu_type) = 1;
4514 /* If the type is passed by reference, objects of this type must be
4515 fully addressable and cannot be copied. */
4516 if (Is_By_Reference_Type (gnat_entity))
4517 TREE_ADDRESSABLE (gnu_type) = 1;
4519 /* ??? Don't set the size for a String_Literal since it is either
4520 confirming or we don't handle it properly (if the low bound is
4521 non-constant). */
4522 if (!gnu_size && kind != E_String_Literal_Subtype)
4523 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
4524 TYPE_DECL, false,
4525 Has_Size_Clause (gnat_entity));
4527 /* If a size was specified, see if we can make a new type of that size
4528 by rearranging the type, for example from a fat to a thin pointer. */
4529 if (gnu_size)
4531 gnu_type
4532 = make_type_from_size (gnu_type, gnu_size,
4533 Has_Biased_Representation (gnat_entity));
4535 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4536 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4537 gnu_size = 0;
4540 /* If the alignment hasn't already been processed and this is
4541 not an unconstrained array, see if an alignment is specified.
4542 If not, we pick a default alignment for atomic objects. */
4543 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4545 else if (Known_Alignment (gnat_entity))
4547 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4548 TYPE_ALIGN (gnu_type));
4550 /* Warn on suspiciously large alignments. This should catch
4551 errors about the (alignment,byte)/(size,bit) discrepancy. */
4552 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4554 tree size;
4556 /* If a size was specified, take it into account. Otherwise
4557 use the RM size for records as the type size has already
4558 been adjusted to the alignment. */
4559 if (gnu_size)
4560 size = gnu_size;
4561 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
4562 || TREE_CODE (gnu_type) == UNION_TYPE
4563 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
4564 && !TYPE_FAT_POINTER_P (gnu_type))
4565 size = rm_size (gnu_type);
4566 else
4567 size = TYPE_SIZE (gnu_type);
4569 /* Consider an alignment as suspicious if the alignment/size
4570 ratio is greater or equal to the byte/bit ratio. */
4571 if (host_integerp (size, 1)
4572 && align >= TREE_INT_CST_LOW (size) * BITS_PER_UNIT)
4573 post_error_ne ("?suspiciously large alignment specified for&",
4574 Expression (Alignment_Clause (gnat_entity)),
4575 gnat_entity);
4578 else if (Is_Atomic (gnat_entity) && !gnu_size
4579 && host_integerp (TYPE_SIZE (gnu_type), 1)
4580 && integer_pow2p (TYPE_SIZE (gnu_type)))
4581 align = MIN (BIGGEST_ALIGNMENT,
4582 tree_low_cst (TYPE_SIZE (gnu_type), 1));
4583 else if (Is_Atomic (gnat_entity) && gnu_size
4584 && host_integerp (gnu_size, 1)
4585 && integer_pow2p (gnu_size))
4586 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
4588 /* See if we need to pad the type. If we did, and made a record,
4589 the name of the new type may be changed. So get it back for
4590 us when we make the new TYPE_DECL below. */
4591 if (gnu_size || align > 0)
4592 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4593 false, !gnu_decl, definition, false);
4595 if (TYPE_IS_PADDING_P (gnu_type))
4597 gnu_entity_name = TYPE_NAME (gnu_type);
4598 if (TREE_CODE (gnu_entity_name) == TYPE_DECL)
4599 gnu_entity_name = DECL_NAME (gnu_entity_name);
4602 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4604 /* If we are at global level, GCC will have applied variable_size to
4605 the type, but that won't have done anything. So, if it's not
4606 a constant or self-referential, call elaborate_expression_1 to
4607 make a variable for the size rather than calculating it each time.
4608 Handle both the RM size and the actual size. */
4609 if (global_bindings_p ()
4610 && TYPE_SIZE (gnu_type)
4611 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4612 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4614 tree size = TYPE_SIZE (gnu_type);
4616 TYPE_SIZE (gnu_type)
4617 = elaborate_expression_1 (size, gnat_entity,
4618 get_identifier ("SIZE"),
4619 definition, false);
4621 /* ??? For now, store the size as a multiple of the alignment in
4622 bytes so that we can see the alignment from the tree. */
4623 TYPE_SIZE_UNIT (gnu_type)
4624 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4625 get_identifier ("SIZE_A_UNIT"),
4626 definition, false,
4627 TYPE_ALIGN (gnu_type));
4629 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4630 may not be marked by the call to create_type_decl below. */
4631 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4633 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4635 tree variant_part = get_variant_part (gnu_type);
4636 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4638 if (variant_part)
4640 tree union_type = TREE_TYPE (variant_part);
4641 tree offset = DECL_FIELD_OFFSET (variant_part);
4643 /* If the position of the variant part is constant, subtract
4644 it from the size of the type of the parent to get the new
4645 size. This manual CSE reduces the data size. */
4646 if (TREE_CODE (offset) == INTEGER_CST)
4648 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4649 TYPE_SIZE (union_type)
4650 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4651 bit_from_pos (offset, bitpos));
4652 TYPE_SIZE_UNIT (union_type)
4653 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4654 byte_from_pos (offset, bitpos));
4656 else
4658 TYPE_SIZE (union_type)
4659 = elaborate_expression_1 (TYPE_SIZE (union_type),
4660 gnat_entity,
4661 get_identifier ("VSIZE"),
4662 definition, false);
4664 /* ??? For now, store the size as a multiple of the
4665 alignment in bytes so that we can see the alignment
4666 from the tree. */
4667 TYPE_SIZE_UNIT (union_type)
4668 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4669 gnat_entity,
4670 get_identifier
4671 ("VSIZE_A_UNIT"),
4672 definition, false,
4673 TYPE_ALIGN (union_type));
4675 /* ??? For now, store the offset as a multiple of the
4676 alignment in bytes so that we can see the alignment
4677 from the tree. */
4678 DECL_FIELD_OFFSET (variant_part)
4679 = elaborate_expression_2 (offset,
4680 gnat_entity,
4681 get_identifier ("VOFFSET"),
4682 definition, false,
4683 DECL_OFFSET_ALIGN
4684 (variant_part));
4687 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4688 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4691 if (operand_equal_p (ada_size, size, 0))
4692 ada_size = TYPE_SIZE (gnu_type);
4693 else
4694 ada_size
4695 = elaborate_expression_1 (ada_size, gnat_entity,
4696 get_identifier ("RM_SIZE"),
4697 definition, false);
4698 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4702 /* If this is a record type or subtype, call elaborate_expression_1 on
4703 any field position. Do this for both global and local types.
4704 Skip any fields that we haven't made trees for to avoid problems with
4705 class wide types. */
4706 if (IN (kind, Record_Kind))
4707 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4708 gnat_temp = Next_Entity (gnat_temp))
4709 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4711 tree gnu_field = get_gnu_tree (gnat_temp);
4713 /* ??? For now, store the offset as a multiple of the alignment
4714 in bytes so that we can see the alignment from the tree. */
4715 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4717 DECL_FIELD_OFFSET (gnu_field)
4718 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4719 gnat_temp,
4720 get_identifier ("OFFSET"),
4721 definition, false,
4722 DECL_OFFSET_ALIGN (gnu_field));
4724 /* ??? The context of gnu_field is not necessarily gnu_type
4725 so the MULT_EXPR node built above may not be marked by
4726 the call to create_type_decl below. */
4727 if (global_bindings_p ())
4728 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4732 if (Treat_As_Volatile (gnat_entity))
4733 gnu_type
4734 = build_qualified_type (gnu_type,
4735 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
4737 if (Is_Atomic (gnat_entity))
4738 check_ok_for_atomic (gnu_type, gnat_entity, false);
4740 if (Present (Alignment_Clause (gnat_entity)))
4741 TYPE_USER_ALIGN (gnu_type) = 1;
4743 if (Universal_Aliasing (gnat_entity))
4744 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
4746 if (!gnu_decl)
4747 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
4748 !Comes_From_Source (gnat_entity),
4749 debug_info_p, gnat_entity);
4750 else
4752 TREE_TYPE (gnu_decl) = gnu_type;
4753 TYPE_STUB_DECL (gnu_type) = gnu_decl;
4757 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4759 gnu_type = TREE_TYPE (gnu_decl);
4761 /* If this is a derived type, relate its alias set to that of its parent
4762 to avoid troubles when a call to an inherited primitive is inlined in
4763 a context where a derived object is accessed. The inlined code works
4764 on the parent view so the resulting code may access the same object
4765 using both the parent and the derived alias sets, which thus have to
4766 conflict. As the same issue arises with component references, the
4767 parent alias set also has to conflict with composite types enclosing
4768 derived components. For instance, if we have:
4770 type D is new T;
4771 type R is record
4772 Component : D;
4773 end record;
4775 we want T to conflict with both D and R, in addition to R being a
4776 superset of D by record/component construction.
4778 One way to achieve this is to perform an alias set copy from the
4779 parent to the derived type. This is not quite appropriate, though,
4780 as we don't want separate derived types to conflict with each other:
4782 type I1 is new Integer;
4783 type I2 is new Integer;
4785 We want I1 and I2 to both conflict with Integer but we do not want
4786 I1 to conflict with I2, and an alias set copy on derivation would
4787 have that effect.
4789 The option chosen is to make the alias set of the derived type a
4790 superset of that of its parent type. It trivially fulfills the
4791 simple requirement for the Integer derivation example above, and
4792 the component case as well by superset transitivity:
4794 superset superset
4795 R ----------> D ----------> T
4797 However, for composite types, conversions between derived types are
4798 translated into VIEW_CONVERT_EXPRs so a sequence like:
4800 type Comp1 is new Comp;
4801 type Comp2 is new Comp;
4802 procedure Proc (C : Comp1);
4804 C : Comp2;
4805 Proc (Comp1 (C));
4807 is translated into:
4809 C : Comp2;
4810 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
4812 and gimplified into:
4814 C : Comp2;
4815 Comp1 *C.0;
4816 C.0 = (Comp1 *) &C;
4817 Proc (C.0);
4819 i.e. generates code involving type punning. Therefore, Comp1 needs
4820 to conflict with Comp2 and an alias set copy is required.
4822 The language rules ensure the parent type is already frozen here. */
4823 if (Is_Derived_Type (gnat_entity))
4825 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_entity));
4826 relate_alias_sets (gnu_type, gnu_parent_type,
4827 Is_Composite_Type (gnat_entity)
4828 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
4831 /* Back-annotate the Alignment of the type if not already in the
4832 tree. Likewise for sizes. */
4833 if (Unknown_Alignment (gnat_entity))
4835 unsigned int double_align, align;
4836 bool is_capped_double, align_clause;
4838 /* If the default alignment of "double" or larger scalar types is
4839 specifically capped and this is not an array with an alignment
4840 clause on the component type, return the cap. */
4841 if ((double_align = double_float_alignment) > 0)
4842 is_capped_double
4843 = is_double_float_or_array (gnat_entity, &align_clause);
4844 else if ((double_align = double_scalar_alignment) > 0)
4845 is_capped_double
4846 = is_double_scalar_or_array (gnat_entity, &align_clause);
4847 else
4848 is_capped_double = align_clause = false;
4850 if (is_capped_double && !align_clause)
4851 align = double_align;
4852 else
4853 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
4855 Set_Alignment (gnat_entity, UI_From_Int (align));
4858 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4860 tree gnu_size = TYPE_SIZE (gnu_type);
4862 /* If the size is self-referential, annotate the maximum value. */
4863 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4864 gnu_size = max_size (gnu_size, true);
4866 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4868 /* In this mode, the tag and the parent components are not
4869 generated by the front-end so the sizes must be adjusted. */
4870 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
4871 Uint uint_size;
4873 if (Is_Derived_Type (gnat_entity))
4875 offset = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
4876 bitsizetype);
4877 Set_Alignment (gnat_entity,
4878 Alignment (Etype (Base_Type (gnat_entity))));
4880 else
4881 offset = pointer_size;
4883 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
4884 gnu_size = size_binop (MULT_EXPR, pointer_size,
4885 size_binop (CEIL_DIV_EXPR,
4886 gnu_size,
4887 pointer_size));
4888 uint_size = annotate_value (gnu_size);
4889 Set_Esize (gnat_entity, uint_size);
4890 Set_RM_Size (gnat_entity, uint_size);
4892 else
4893 Set_Esize (gnat_entity, annotate_value (gnu_size));
4896 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4897 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4900 if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4901 DECL_ARTIFICIAL (gnu_decl) = 1;
4903 if (!debug_info_p && DECL_P (gnu_decl)
4904 && TREE_CODE (gnu_decl) != FUNCTION_DECL
4905 && No (Renamed_Object (gnat_entity)))
4906 DECL_IGNORED_P (gnu_decl) = 1;
4908 /* If we haven't already, associate the ..._DECL node that we just made with
4909 the input GNAT entity node. */
4910 if (!saved)
4911 save_gnu_tree (gnat_entity, gnu_decl, false);
4913 /* If this is an enumeration or floating-point type, we were not able to set
4914 the bounds since they refer to the type. These are always static. */
4915 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4916 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4918 tree gnu_scalar_type = gnu_type;
4919 tree gnu_low_bound, gnu_high_bound;
4921 /* If this is a padded type, we need to use the underlying type. */
4922 if (TYPE_IS_PADDING_P (gnu_scalar_type))
4923 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4925 /* If this is a floating point type and we haven't set a floating
4926 point type yet, use this in the evaluation of the bounds. */
4927 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4928 longest_float_type_node = gnu_scalar_type;
4930 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4931 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
4933 if (kind == E_Enumeration_Type)
4935 /* Enumeration types have specific RM bounds. */
4936 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
4937 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
4939 /* Write full debugging information. Since this has both a
4940 typedef and a tag, avoid outputting the name twice. */
4941 DECL_ARTIFICIAL (gnu_decl) = 1;
4942 rest_of_type_decl_compilation (gnu_decl);
4945 else
4947 /* Floating-point types don't have specific RM bounds. */
4948 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
4949 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
4953 /* If we deferred processing of incomplete types, re-enable it. If there
4954 were no other disables and we have some to process, do so. */
4955 if (this_deferred && --defer_incomplete_level == 0)
4957 if (defer_incomplete_list)
4959 struct incomplete *incp, *next;
4961 /* We are back to level 0 for the deferring of incomplete types.
4962 But processing these incomplete types below may itself require
4963 deferring, so preserve what we have and restart from scratch. */
4964 incp = defer_incomplete_list;
4965 defer_incomplete_list = NULL;
4967 /* For finalization, however, all types must be complete so we
4968 cannot do the same because deferred incomplete types may end up
4969 referencing each other. Process them all recursively first. */
4970 defer_finalize_level++;
4972 for (; incp; incp = next)
4974 next = incp->next;
4976 if (incp->old_type)
4977 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4978 gnat_to_gnu_type (incp->full_type));
4979 free (incp);
4982 defer_finalize_level--;
4985 /* All the deferred incomplete types have been processed so we can
4986 now proceed with the finalization of the deferred types. */
4987 if (defer_finalize_level == 0 && defer_finalize_list)
4989 unsigned int i;
4990 tree t;
4992 FOR_EACH_VEC_ELT (tree, defer_finalize_list, i, t)
4993 rest_of_type_decl_compilation_no_defer (t);
4995 VEC_free (tree, heap, defer_finalize_list);
4999 /* If we are not defining this type, see if it's in the incomplete list.
5000 If so, handle that list entry now. */
5001 else if (!definition)
5003 struct incomplete *incp;
5005 for (incp = defer_incomplete_list; incp; incp = incp->next)
5006 if (incp->old_type && incp->full_type == gnat_entity)
5008 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
5009 TREE_TYPE (gnu_decl));
5010 incp->old_type = NULL_TREE;
5014 if (this_global)
5015 force_global--;
5017 /* If this is a packed array type whose original array type is itself
5018 an Itype without freeze node, make sure the latter is processed. */
5019 if (Is_Packed_Array_Type (gnat_entity)
5020 && Is_Itype (Original_Array_Type (gnat_entity))
5021 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
5022 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
5023 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
5025 return gnu_decl;
5028 /* Similar, but if the returned value is a COMPONENT_REF, return the
5029 FIELD_DECL. */
5031 tree
5032 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
5034 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5036 if (TREE_CODE (gnu_field) == COMPONENT_REF)
5037 gnu_field = TREE_OPERAND (gnu_field, 1);
5039 return gnu_field;
5042 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5043 the GCC type corresponding to that entity. */
5045 tree
5046 gnat_to_gnu_type (Entity_Id gnat_entity)
5048 tree gnu_decl;
5050 /* The back end never attempts to annotate generic types. */
5051 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
5052 return void_type_node;
5054 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5055 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
5057 return TREE_TYPE (gnu_decl);
5060 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5061 the unpadded version of the GCC type corresponding to that entity. */
5063 tree
5064 get_unpadded_type (Entity_Id gnat_entity)
5066 tree type = gnat_to_gnu_type (gnat_entity);
5068 if (TYPE_IS_PADDING_P (type))
5069 type = TREE_TYPE (TYPE_FIELDS (type));
5071 return type;
5074 /* Wrap up compilation of DECL, a TYPE_DECL, possibly deferring it.
5075 Every TYPE_DECL generated for a type definition must be passed
5076 to this function once everything else has been done for it. */
5078 void
5079 rest_of_type_decl_compilation (tree decl)
5081 /* We need to defer finalizing the type if incomplete types
5082 are being deferred or if they are being processed. */
5083 if (defer_incomplete_level != 0 || defer_finalize_level != 0)
5084 VEC_safe_push (tree, heap, defer_finalize_list, decl);
5085 else
5086 rest_of_type_decl_compilation_no_defer (decl);
5089 /* Same as above but without deferring the compilation. This
5090 function should not be invoked directly on a TYPE_DECL. */
5092 static void
5093 rest_of_type_decl_compilation_no_defer (tree decl)
5095 const int toplev = global_bindings_p ();
5096 tree t = TREE_TYPE (decl);
5098 rest_of_decl_compilation (decl, toplev, 0);
5100 /* Now process all the variants. This is needed for STABS. */
5101 for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
5103 if (t == TREE_TYPE (decl))
5104 continue;
5106 if (!TYPE_STUB_DECL (t))
5107 TYPE_STUB_DECL (t) = create_type_stub_decl (DECL_NAME (decl), t);
5109 rest_of_type_compilation (t, toplev);
5113 /* Finalize any From_With_Type incomplete types. We do this after processing
5114 our compilation unit and after processing its spec, if this is a body. */
5116 void
5117 finalize_from_with_types (void)
5119 struct incomplete *incp = defer_limited_with;
5120 struct incomplete *next;
5122 defer_limited_with = 0;
5123 for (; incp; incp = next)
5125 next = incp->next;
5127 if (incp->old_type != 0)
5128 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
5129 gnat_to_gnu_type (incp->full_type));
5130 free (incp);
5134 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5135 kind of type (such E_Task_Type) that has a different type which Gigi
5136 uses for its representation. If the type does not have a special type
5137 for its representation, return GNAT_ENTITY. If a type is supposed to
5138 exist, but does not, abort unless annotating types, in which case
5139 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5141 Entity_Id
5142 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5144 Entity_Id gnat_equiv = gnat_entity;
5146 if (No (gnat_entity))
5147 return gnat_entity;
5149 switch (Ekind (gnat_entity))
5151 case E_Class_Wide_Subtype:
5152 if (Present (Equivalent_Type (gnat_entity)))
5153 gnat_equiv = Equivalent_Type (gnat_entity);
5154 break;
5156 case E_Access_Protected_Subprogram_Type:
5157 case E_Anonymous_Access_Protected_Subprogram_Type:
5158 gnat_equiv = Equivalent_Type (gnat_entity);
5159 break;
5161 case E_Class_Wide_Type:
5162 gnat_equiv = Root_Type (gnat_entity);
5163 break;
5165 case E_Task_Type:
5166 case E_Task_Subtype:
5167 case E_Protected_Type:
5168 case E_Protected_Subtype:
5169 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5170 break;
5172 default:
5173 break;
5176 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5177 return gnat_equiv;
5180 /* Return a GCC tree for a type corresponding to the component type of the
5181 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5182 is for an array being defined. DEBUG_INFO_P is true if we need to write
5183 debug information for other types that we may create in the process. */
5185 static tree
5186 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5187 bool debug_info_p)
5189 tree gnu_type = gnat_to_gnu_type (Component_Type (gnat_array));
5190 tree gnu_comp_size;
5192 /* Try to get a smaller form of the component if needed. */
5193 if ((Is_Packed (gnat_array)
5194 || Has_Component_Size_Clause (gnat_array))
5195 && !Is_Bit_Packed_Array (gnat_array)
5196 && !Has_Aliased_Components (gnat_array)
5197 && !Strict_Alignment (Component_Type (gnat_array))
5198 && TREE_CODE (gnu_type) == RECORD_TYPE
5199 && !TYPE_FAT_POINTER_P (gnu_type)
5200 && host_integerp (TYPE_SIZE (gnu_type), 1))
5201 gnu_type = make_packable_type (gnu_type, false);
5203 if (Has_Atomic_Components (gnat_array))
5204 check_ok_for_atomic (gnu_type, gnat_array, true);
5206 /* Get and validate any specified Component_Size. */
5207 gnu_comp_size
5208 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5209 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5210 true, Has_Component_Size_Clause (gnat_array));
5212 /* If the array has aliased components and the component size can be zero,
5213 force at least unit size to ensure that the components have distinct
5214 addresses. */
5215 if (!gnu_comp_size
5216 && Has_Aliased_Components (gnat_array)
5217 && (integer_zerop (TYPE_SIZE (gnu_type))
5218 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5219 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5220 gnu_comp_size
5221 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5223 /* If the component type is a RECORD_TYPE that has a self-referential size,
5224 then use the maximum size for the component size. */
5225 if (!gnu_comp_size
5226 && TREE_CODE (gnu_type) == RECORD_TYPE
5227 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5228 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5230 /* Honor the component size. This is not needed for bit-packed arrays. */
5231 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5233 tree orig_type = gnu_type;
5234 unsigned int max_align;
5236 /* If an alignment is specified, use it as a cap on the component type
5237 so that it can be honored for the whole type. But ignore it for the
5238 original type of packed array types. */
5239 if (No (Packed_Array_Type (gnat_array)) && Known_Alignment (gnat_array))
5240 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5241 else
5242 max_align = 0;
5244 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5245 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5246 gnu_type = orig_type;
5247 else
5248 orig_type = gnu_type;
5250 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5251 true, false, definition, true);
5253 /* If a padding record was made, declare it now since it will never be
5254 declared otherwise. This is necessary to ensure that its subtrees
5255 are properly marked. */
5256 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5257 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL, true,
5258 debug_info_p, gnat_array);
5261 if (Has_Volatile_Components (Base_Type (gnat_array)))
5262 gnu_type
5263 = build_qualified_type (gnu_type,
5264 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
5266 return gnu_type;
5269 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5270 using MECH as its passing mechanism, to be placed in the parameter
5271 list built for GNAT_SUBPROG. Assume a foreign convention for the
5272 latter if FOREIGN is true. Also set CICO to true if the parameter
5273 must use the copy-in copy-out implementation mechanism.
5275 The returned tree is a PARM_DECL, except for those cases where no
5276 parameter needs to be actually passed to the subprogram; the type
5277 of this "shadow" parameter is then returned instead. */
5279 static tree
5280 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5281 Entity_Id gnat_subprog, bool foreign, bool *cico)
5283 tree gnu_param_name = get_entity_name (gnat_param);
5284 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5285 tree gnu_param_type_alt = NULL_TREE;
5286 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5287 /* The parameter can be indirectly modified if its address is taken. */
5288 bool ro_param = in_param && !Address_Taken (gnat_param);
5289 bool by_return = false, by_component_ptr = false;
5290 bool by_ref = false, by_double_ref = false;
5291 tree gnu_param;
5293 /* Copy-return is used only for the first parameter of a valued procedure.
5294 It's a copy mechanism for which a parameter is never allocated. */
5295 if (mech == By_Copy_Return)
5297 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5298 mech = By_Copy;
5299 by_return = true;
5302 /* If this is either a foreign function or if the underlying type won't
5303 be passed by reference, strip off possible padding type. */
5304 if (TYPE_IS_PADDING_P (gnu_param_type))
5306 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5308 if (mech == By_Reference
5309 || foreign
5310 || (!must_pass_by_ref (unpadded_type)
5311 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
5312 gnu_param_type = unpadded_type;
5315 /* If this is a read-only parameter, make a variant of the type that is
5316 read-only. ??? However, if this is an unconstrained array, that type
5317 can be very complex, so skip it for now. Likewise for any other
5318 self-referential type. */
5319 if (ro_param
5320 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5321 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5322 gnu_param_type = build_qualified_type (gnu_param_type,
5323 (TYPE_QUALS (gnu_param_type)
5324 | TYPE_QUAL_CONST));
5326 /* For foreign conventions, pass arrays as pointers to the element type.
5327 First check for unconstrained array and get the underlying array. */
5328 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5329 gnu_param_type
5330 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5332 /* For GCC builtins, pass Address integer types as (void *) */
5333 if (Convention (gnat_subprog) == Convention_Intrinsic
5334 && Present (Interface_Name (gnat_subprog))
5335 && Is_Descendent_Of_Address (Etype (gnat_param)))
5336 gnu_param_type = ptr_void_type_node;
5338 /* VMS descriptors are themselves passed by reference. */
5339 if (mech == By_Short_Descriptor ||
5340 (mech == By_Descriptor && TARGET_ABI_OPEN_VMS && !TARGET_MALLOC64))
5341 gnu_param_type
5342 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5343 Mechanism (gnat_param),
5344 gnat_subprog));
5345 else if (mech == By_Descriptor)
5347 /* Build both a 32-bit and 64-bit descriptor, one of which will be
5348 chosen in fill_vms_descriptor. */
5349 gnu_param_type_alt
5350 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5351 Mechanism (gnat_param),
5352 gnat_subprog));
5353 gnu_param_type
5354 = build_pointer_type (build_vms_descriptor (gnu_param_type,
5355 Mechanism (gnat_param),
5356 gnat_subprog));
5359 /* Arrays are passed as pointers to element type for foreign conventions. */
5360 else if (foreign
5361 && mech != By_Copy
5362 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5364 /* Strip off any multi-dimensional entries, then strip
5365 off the last array to get the component type. */
5366 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5367 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5368 gnu_param_type = TREE_TYPE (gnu_param_type);
5370 by_component_ptr = true;
5371 gnu_param_type = TREE_TYPE (gnu_param_type);
5373 if (ro_param)
5374 gnu_param_type = build_qualified_type (gnu_param_type,
5375 (TYPE_QUALS (gnu_param_type)
5376 | TYPE_QUAL_CONST));
5378 gnu_param_type = build_pointer_type (gnu_param_type);
5381 /* Fat pointers are passed as thin pointers for foreign conventions. */
5382 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5383 gnu_param_type
5384 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5386 /* If we must pass or were requested to pass by reference, do so.
5387 If we were requested to pass by copy, do so.
5388 Otherwise, for foreign conventions, pass In Out or Out parameters
5389 or aggregates by reference. For COBOL and Fortran, pass all
5390 integer and FP types that way too. For Convention Ada, use
5391 the standard Ada default. */
5392 else if (must_pass_by_ref (gnu_param_type)
5393 || mech == By_Reference
5394 || (mech != By_Copy
5395 && ((foreign
5396 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5397 || (foreign
5398 && (Convention (gnat_subprog) == Convention_Fortran
5399 || Convention (gnat_subprog) == Convention_COBOL)
5400 && (INTEGRAL_TYPE_P (gnu_param_type)
5401 || FLOAT_TYPE_P (gnu_param_type)))
5402 || (!foreign
5403 && default_pass_by_ref (gnu_param_type)))))
5405 gnu_param_type = build_reference_type (gnu_param_type);
5406 by_ref = true;
5408 /* In some ABIs, e.g. SPARC 32-bit, fat pointer types are themselves
5409 passed by reference. Pass them by explicit reference, this will
5410 generate more debuggable code at -O0. */
5411 if (TYPE_IS_FAT_POINTER_P (gnu_param_type)
5412 && targetm.calls.pass_by_reference (NULL,
5413 TYPE_MODE (gnu_param_type),
5414 gnu_param_type,
5415 true))
5417 gnu_param_type = build_reference_type (gnu_param_type);
5418 by_double_ref = true;
5422 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5423 else if (!in_param)
5424 *cico = true;
5426 if (mech == By_Copy && (by_ref || by_component_ptr))
5427 post_error ("?cannot pass & by copy", gnat_param);
5429 /* If this is an Out parameter that isn't passed by reference and isn't
5430 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5431 it will be a VAR_DECL created when we process the procedure, so just
5432 return its type. For the special parameter of a valued procedure,
5433 never pass it in.
5435 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5436 Out parameters with discriminants or implicit initial values to be
5437 handled like In Out parameters. These type are normally built as
5438 aggregates, hence passed by reference, except for some packed arrays
5439 which end up encoded in special integer types.
5441 The exception we need to make is then for packed arrays of records
5442 with discriminants or implicit initial values. We have no light/easy
5443 way to check for the latter case, so we merely check for packed arrays
5444 of records. This may lead to useless copy-in operations, but in very
5445 rare cases only, as these would be exceptions in a set of already
5446 exceptional situations. */
5447 if (Ekind (gnat_param) == E_Out_Parameter
5448 && !by_ref
5449 && (by_return
5450 || (mech != By_Descriptor
5451 && mech != By_Short_Descriptor
5452 && !POINTER_TYPE_P (gnu_param_type)
5453 && !AGGREGATE_TYPE_P (gnu_param_type)))
5454 && !(Is_Array_Type (Etype (gnat_param))
5455 && Is_Packed (Etype (gnat_param))
5456 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5457 return gnu_param_type;
5459 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5460 ro_param || by_ref || by_component_ptr);
5461 DECL_BY_REF_P (gnu_param) = by_ref;
5462 DECL_BY_DOUBLE_REF_P (gnu_param) = by_double_ref;
5463 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5464 DECL_BY_DESCRIPTOR_P (gnu_param) = (mech == By_Descriptor ||
5465 mech == By_Short_Descriptor);
5466 DECL_POINTS_TO_READONLY_P (gnu_param)
5467 = (ro_param && (by_ref || by_component_ptr));
5469 /* Save the alternate descriptor type, if any. */
5470 if (gnu_param_type_alt)
5471 SET_DECL_PARM_ALT_TYPE (gnu_param, gnu_param_type_alt);
5473 /* If no Mechanism was specified, indicate what we're using, then
5474 back-annotate it. */
5475 if (mech == Default)
5476 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5478 Set_Mechanism (gnat_param, mech);
5479 return gnu_param;
5482 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5484 static bool
5485 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5487 while (Present (Corresponding_Discriminant (discr1)))
5488 discr1 = Corresponding_Discriminant (discr1);
5490 while (Present (Corresponding_Discriminant (discr2)))
5491 discr2 = Corresponding_Discriminant (discr2);
5493 return
5494 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5497 /* Return true if the array type GNU_TYPE, which represents a dimension of
5498 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5500 static bool
5501 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5503 /* If the array type is not the innermost dimension of the GNAT type,
5504 then it has a non-aliased component. */
5505 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5506 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5507 return true;
5509 /* If the array type has an aliased component in the front-end sense,
5510 then it also has an aliased component in the back-end sense. */
5511 if (Has_Aliased_Components (gnat_type))
5512 return false;
5514 /* If this is a derived type, then it has a non-aliased component if
5515 and only if its parent type also has one. */
5516 if (Is_Derived_Type (gnat_type))
5518 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5519 int index;
5520 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5521 gnu_parent_type
5522 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5523 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5524 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5525 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5528 /* Otherwise, rely exclusively on properties of the element type. */
5529 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5532 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5534 static bool
5535 compile_time_known_address_p (Node_Id gnat_address)
5537 /* Catch System'To_Address. */
5538 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5539 gnat_address = Expression (gnat_address);
5541 return Compile_Time_Known_Value (gnat_address);
5544 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5545 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5547 static bool
5548 cannot_be_superflat_p (Node_Id gnat_range)
5550 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5551 Node_Id scalar_range;
5552 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5554 /* If the low bound is not constant, try to find an upper bound. */
5555 while (Nkind (gnat_lb) != N_Integer_Literal
5556 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5557 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5558 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5559 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5560 || Nkind (scalar_range) == N_Range))
5561 gnat_lb = High_Bound (scalar_range);
5563 /* If the high bound is not constant, try to find a lower bound. */
5564 while (Nkind (gnat_hb) != N_Integer_Literal
5565 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5566 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5567 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5568 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5569 || Nkind (scalar_range) == N_Range))
5570 gnat_hb = Low_Bound (scalar_range);
5572 /* If we have failed to find constant bounds, punt. */
5573 if (Nkind (gnat_lb) != N_Integer_Literal
5574 || Nkind (gnat_hb) != N_Integer_Literal)
5575 return false;
5577 /* We need at least a signed 64-bit type to catch most cases. */
5578 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5579 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5580 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5581 return false;
5583 /* If the low bound is the smallest integer, nothing can be smaller. */
5584 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5585 if (TREE_OVERFLOW (gnu_lb_minus_one))
5586 return true;
5588 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5591 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5593 static bool
5594 constructor_address_p (tree gnu_expr)
5596 while (TREE_CODE (gnu_expr) == NOP_EXPR
5597 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5598 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5599 gnu_expr = TREE_OPERAND (gnu_expr, 0);
5601 return (TREE_CODE (gnu_expr) == ADDR_EXPR
5602 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5605 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5606 be elaborated at the point of its definition, but do nothing else. */
5608 void
5609 elaborate_entity (Entity_Id gnat_entity)
5611 switch (Ekind (gnat_entity))
5613 case E_Signed_Integer_Subtype:
5614 case E_Modular_Integer_Subtype:
5615 case E_Enumeration_Subtype:
5616 case E_Ordinary_Fixed_Point_Subtype:
5617 case E_Decimal_Fixed_Point_Subtype:
5618 case E_Floating_Point_Subtype:
5620 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5621 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5623 /* ??? Tests to avoid Constraint_Error in static expressions
5624 are needed until after the front stops generating bogus
5625 conversions on bounds of real types. */
5626 if (!Raises_Constraint_Error (gnat_lb))
5627 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5628 true, false, Needs_Debug_Info (gnat_entity));
5629 if (!Raises_Constraint_Error (gnat_hb))
5630 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5631 true, false, Needs_Debug_Info (gnat_entity));
5632 break;
5635 case E_Record_Type:
5637 Node_Id full_definition = Declaration_Node (gnat_entity);
5638 Node_Id record_definition = Type_Definition (full_definition);
5640 /* If this is a record extension, go a level further to find the
5641 record definition. */
5642 if (Nkind (record_definition) == N_Derived_Type_Definition)
5643 record_definition = Record_Extension_Part (record_definition);
5645 break;
5647 case E_Record_Subtype:
5648 case E_Private_Subtype:
5649 case E_Limited_Private_Subtype:
5650 case E_Record_Subtype_With_Private:
5651 if (Is_Constrained (gnat_entity)
5652 && Has_Discriminants (gnat_entity)
5653 && Present (Discriminant_Constraint (gnat_entity)))
5655 Node_Id gnat_discriminant_expr;
5656 Entity_Id gnat_field;
5658 for (gnat_field
5659 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
5660 gnat_discriminant_expr
5661 = First_Elmt (Discriminant_Constraint (gnat_entity));
5662 Present (gnat_field);
5663 gnat_field = Next_Discriminant (gnat_field),
5664 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
5665 /* ??? For now, ignore access discriminants. */
5666 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
5667 elaborate_expression (Node (gnat_discriminant_expr),
5668 gnat_entity, get_entity_name (gnat_field),
5669 true, false, false);
5671 break;
5676 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
5677 any entities on its entity chain similarly. */
5679 void
5680 mark_out_of_scope (Entity_Id gnat_entity)
5682 Entity_Id gnat_sub_entity;
5683 unsigned int kind = Ekind (gnat_entity);
5685 /* If this has an entity list, process all in the list. */
5686 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
5687 || IN (kind, Private_Kind)
5688 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
5689 || kind == E_Function || kind == E_Generic_Function
5690 || kind == E_Generic_Package || kind == E_Generic_Procedure
5691 || kind == E_Loop || kind == E_Operator || kind == E_Package
5692 || kind == E_Package_Body || kind == E_Procedure
5693 || kind == E_Record_Type || kind == E_Record_Subtype
5694 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
5695 for (gnat_sub_entity = First_Entity (gnat_entity);
5696 Present (gnat_sub_entity);
5697 gnat_sub_entity = Next_Entity (gnat_sub_entity))
5698 if (Scope (gnat_sub_entity) == gnat_entity
5699 && gnat_sub_entity != gnat_entity)
5700 mark_out_of_scope (gnat_sub_entity);
5702 /* Now clear this if it has been defined, but only do so if it isn't
5703 a subprogram or parameter. We could refine this, but it isn't
5704 worth it. If this is statically allocated, it is supposed to
5705 hang around out of cope. */
5706 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
5707 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
5709 save_gnu_tree (gnat_entity, NULL_TREE, true);
5710 save_gnu_tree (gnat_entity, error_mark_node, true);
5714 /* Relate the alias sets of GNU_NEW_TYPE and GNU_OLD_TYPE according to OP.
5715 If this is a multi-dimensional array type, do this recursively.
5717 OP may be
5718 - ALIAS_SET_COPY: the new set is made a copy of the old one.
5719 - ALIAS_SET_SUPERSET: the new set is made a superset of the old one.
5720 - ALIAS_SET_SUBSET: the new set is made a subset of the old one. */
5722 static void
5723 relate_alias_sets (tree gnu_new_type, tree gnu_old_type, enum alias_set_op op)
5725 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
5726 of a one-dimensional array, since the padding has the same alias set
5727 as the field type, but if it's a multi-dimensional array, we need to
5728 see the inner types. */
5729 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
5730 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
5731 || TYPE_PADDING_P (gnu_old_type)))
5732 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
5734 /* Unconstrained array types are deemed incomplete and would thus be given
5735 alias set 0. Retrieve the underlying array type. */
5736 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
5737 gnu_old_type
5738 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
5739 if (TREE_CODE (gnu_new_type) == UNCONSTRAINED_ARRAY_TYPE)
5740 gnu_new_type
5741 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_new_type))));
5743 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
5744 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
5745 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
5746 relate_alias_sets (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type), op);
5748 switch (op)
5750 case ALIAS_SET_COPY:
5751 /* The alias set shouldn't be copied between array types with different
5752 aliasing settings because this can break the aliasing relationship
5753 between the array type and its element type. */
5754 #ifndef ENABLE_CHECKING
5755 if (flag_strict_aliasing)
5756 #endif
5757 gcc_assert (!(TREE_CODE (gnu_new_type) == ARRAY_TYPE
5758 && TREE_CODE (gnu_old_type) == ARRAY_TYPE
5759 && TYPE_NONALIASED_COMPONENT (gnu_new_type)
5760 != TYPE_NONALIASED_COMPONENT (gnu_old_type)));
5762 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
5763 break;
5765 case ALIAS_SET_SUBSET:
5766 case ALIAS_SET_SUPERSET:
5768 alias_set_type old_set = get_alias_set (gnu_old_type);
5769 alias_set_type new_set = get_alias_set (gnu_new_type);
5771 /* Do nothing if the alias sets conflict. This ensures that we
5772 never call record_alias_subset several times for the same pair
5773 or at all for alias set 0. */
5774 if (!alias_sets_conflict_p (old_set, new_set))
5776 if (op == ALIAS_SET_SUBSET)
5777 record_alias_subset (old_set, new_set);
5778 else
5779 record_alias_subset (new_set, old_set);
5782 break;
5784 default:
5785 gcc_unreachable ();
5788 record_component_aliases (gnu_new_type);
5791 /* Return true if the size represented by GNU_SIZE can be handled by an
5792 allocation. If STATIC_P is true, consider only what can be done with a
5793 static allocation. */
5795 static bool
5796 allocatable_size_p (tree gnu_size, bool static_p)
5798 HOST_WIDE_INT our_size;
5800 /* If this is not a static allocation, the only case we want to forbid
5801 is an overflowing size. That will be converted into a raise a
5802 Storage_Error. */
5803 if (!static_p)
5804 return !(TREE_CODE (gnu_size) == INTEGER_CST
5805 && TREE_OVERFLOW (gnu_size));
5807 /* Otherwise, we need to deal with both variable sizes and constant
5808 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
5809 since assemblers may not like very large sizes. */
5810 if (!host_integerp (gnu_size, 1))
5811 return false;
5813 our_size = tree_low_cst (gnu_size, 1);
5814 return (int) our_size == our_size;
5817 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5818 NAME, ARGS and ERROR_POINT. */
5820 static void
5821 prepend_one_attribute_to (struct attrib ** attr_list,
5822 enum attr_type attr_type,
5823 tree attr_name,
5824 tree attr_args,
5825 Node_Id attr_error_point)
5827 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
5829 attr->type = attr_type;
5830 attr->name = attr_name;
5831 attr->args = attr_args;
5832 attr->error_point = attr_error_point;
5834 attr->next = *attr_list;
5835 *attr_list = attr;
5838 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
5840 static void
5841 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
5843 Node_Id gnat_temp;
5845 /* Attributes are stored as Representation Item pragmas. */
5847 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
5848 gnat_temp = Next_Rep_Item (gnat_temp))
5849 if (Nkind (gnat_temp) == N_Pragma)
5851 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
5852 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
5853 enum attr_type etype;
5855 /* Map the kind of pragma at hand. Skip if this is not one
5856 we know how to handle. */
5858 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_temp))))
5860 case Pragma_Machine_Attribute:
5861 etype = ATTR_MACHINE_ATTRIBUTE;
5862 break;
5864 case Pragma_Linker_Alias:
5865 etype = ATTR_LINK_ALIAS;
5866 break;
5868 case Pragma_Linker_Section:
5869 etype = ATTR_LINK_SECTION;
5870 break;
5872 case Pragma_Linker_Constructor:
5873 etype = ATTR_LINK_CONSTRUCTOR;
5874 break;
5876 case Pragma_Linker_Destructor:
5877 etype = ATTR_LINK_DESTRUCTOR;
5878 break;
5880 case Pragma_Weak_External:
5881 etype = ATTR_WEAK_EXTERNAL;
5882 break;
5884 case Pragma_Thread_Local_Storage:
5885 etype = ATTR_THREAD_LOCAL_STORAGE;
5886 break;
5888 default:
5889 continue;
5892 /* See what arguments we have and turn them into GCC trees for
5893 attribute handlers. These expect identifier for strings. We
5894 handle at most two arguments, static expressions only. */
5896 if (Present (gnat_assoc) && Present (First (gnat_assoc)))
5898 Node_Id gnat_arg0 = Next (First (gnat_assoc));
5899 Node_Id gnat_arg1 = Empty;
5901 if (Present (gnat_arg0)
5902 && Is_Static_Expression (Expression (gnat_arg0)))
5904 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
5906 if (TREE_CODE (gnu_arg0) == STRING_CST)
5907 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
5909 gnat_arg1 = Next (gnat_arg0);
5912 if (Present (gnat_arg1)
5913 && Is_Static_Expression (Expression (gnat_arg1)))
5915 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
5917 if (TREE_CODE (gnu_arg1) == STRING_CST)
5918 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
5922 /* Prepend to the list now. Make a list of the argument we might
5923 have, as GCC expects it. */
5924 prepend_one_attribute_to
5925 (attr_list,
5926 etype, gnu_arg0,
5927 (gnu_arg1 != NULL_TREE)
5928 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
5929 Present (Next (First (gnat_assoc)))
5930 ? Expression (Next (First (gnat_assoc))) : gnat_temp);
5934 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
5935 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
5936 return the GCC tree to use for that expression. GNU_NAME is the suffix
5937 to use if a variable needs to be created and DEFINITION is true if this
5938 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
5939 otherwise, we are just elaborating the expression for side-effects. If
5940 NEED_DEBUG is true, we need a variable for debugging purposes even if it
5941 isn't needed for code generation. */
5943 static tree
5944 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
5945 bool definition, bool need_value, bool need_debug)
5947 tree gnu_expr;
5949 /* If we already elaborated this expression (e.g. it was involved
5950 in the definition of a private type), use the old value. */
5951 if (present_gnu_tree (gnat_expr))
5952 return get_gnu_tree (gnat_expr);
5954 /* If we don't need a value and this is static or a discriminant,
5955 we don't need to do anything. */
5956 if (!need_value
5957 && (Is_OK_Static_Expression (gnat_expr)
5958 || (Nkind (gnat_expr) == N_Identifier
5959 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
5960 return NULL_TREE;
5962 /* If it's a static expression, we don't need a variable for debugging. */
5963 if (need_debug && Is_OK_Static_Expression (gnat_expr))
5964 need_debug = false;
5966 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
5967 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
5968 gnu_name, definition, need_debug);
5970 /* Save the expression in case we try to elaborate this entity again. Since
5971 it's not a DECL, don't check it. Don't save if it's a discriminant. */
5972 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
5973 save_gnu_tree (gnat_expr, gnu_expr, true);
5975 return need_value ? gnu_expr : error_mark_node;
5978 /* Similar, but take a GNU expression and always return a result. */
5980 static tree
5981 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
5982 bool definition, bool need_debug)
5984 /* Skip any conversions and simple arithmetics to see if the expression
5985 is a read-only variable.
5986 ??? This really should remain read-only, but we have to think about
5987 the typing of the tree here. */
5988 tree gnu_inner_expr
5989 = skip_simple_arithmetic (remove_conversions (gnu_expr, true));
5990 tree gnu_decl = NULL_TREE;
5991 bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
5992 bool expr_variable;
5994 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
5995 reference will have been replaced with a COMPONENT_REF when the type
5996 is being elaborated. However, there are some cases involving child
5997 types where we will. So convert it to a COMPONENT_REF. We hope it
5998 will be at the highest level of the expression in these cases. */
5999 if (TREE_CODE (gnu_expr) == FIELD_DECL)
6000 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6001 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6002 gnu_expr, NULL_TREE);
6004 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
6005 that is read-only, make a variable that is initialized to contain the
6006 bound when the package containing the definition is elaborated. If
6007 this entity is defined at top level and a bound or discriminant value
6008 isn't a constant or a reference to a discriminant, replace the bound
6009 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
6010 rely here on the fact that an expression cannot contain both the
6011 discriminant and some other variable. */
6012 expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
6013 && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
6014 && (TREE_READONLY (gnu_inner_expr)
6015 || DECL_READONLY_ONCE_ELAB (gnu_inner_expr)))
6016 && !CONTAINS_PLACEHOLDER_P (gnu_expr));
6018 /* If GNU_EXPR contains a discriminant, we can't elaborate a variable. */
6019 if (need_debug && CONTAINS_PLACEHOLDER_P (gnu_expr))
6020 need_debug = false;
6022 /* Now create the variable if we need it. */
6023 if (need_debug || (expr_variable && expr_global))
6024 gnu_decl
6025 = create_var_decl (create_concat_name (gnat_entity,
6026 IDENTIFIER_POINTER (gnu_name)),
6027 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
6028 !need_debug, Is_Public (gnat_entity),
6029 !definition, expr_global, NULL, gnat_entity);
6031 /* We only need to use this variable if we are in global context since GCC
6032 can do the right thing in the local case. */
6033 if (expr_global && expr_variable)
6034 return gnu_decl;
6036 return expr_variable ? gnat_save_expr (gnu_expr) : gnu_expr;
6039 /* Similar, but take an alignment factor and make it explicit in the tree. */
6041 static tree
6042 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6043 bool definition, bool need_debug, unsigned int align)
6045 tree unit_align = size_int (align / BITS_PER_UNIT);
6046 return
6047 size_binop (MULT_EXPR,
6048 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6049 gnu_expr,
6050 unit_align),
6051 gnat_entity, gnu_name, definition,
6052 need_debug),
6053 unit_align);
6056 /* Create a record type that contains a SIZE bytes long field of TYPE with a
6057 starting bit position so that it is aligned to ALIGN bits, and leaving at
6058 least ROOM bytes free before the field. BASE_ALIGN is the alignment the
6059 record is guaranteed to get. */
6061 tree
6062 make_aligning_type (tree type, unsigned int align, tree size,
6063 unsigned int base_align, int room)
6065 /* We will be crafting a record type with one field at a position set to be
6066 the next multiple of ALIGN past record'address + room bytes. We use a
6067 record placeholder to express record'address. */
6068 tree record_type = make_node (RECORD_TYPE);
6069 tree record = build0 (PLACEHOLDER_EXPR, record_type);
6071 tree record_addr_st
6072 = convert (sizetype, build_unary_op (ADDR_EXPR, NULL_TREE, record));
6074 /* The diagram below summarizes the shape of what we manipulate:
6076 <--------- pos ---------->
6077 { +------------+-------------+-----------------+
6078 record =>{ |############| ... | field (type) |
6079 { +------------+-------------+-----------------+
6080 |<-- room -->|<- voffset ->|<---- size ----->|
6083 record_addr vblock_addr
6085 Every length is in sizetype bytes there, except "pos" which has to be
6086 set as a bit position in the GCC tree for the record. */
6087 tree room_st = size_int (room);
6088 tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
6089 tree voffset_st, pos, field;
6091 tree name = TYPE_NAME (type);
6093 if (TREE_CODE (name) == TYPE_DECL)
6094 name = DECL_NAME (name);
6095 name = concat_name (name, "ALIGN");
6096 TYPE_NAME (record_type) = name;
6098 /* Compute VOFFSET and then POS. The next byte position multiple of some
6099 alignment after some address is obtained by "and"ing the alignment minus
6100 1 with the two's complement of the address. */
6101 voffset_st = size_binop (BIT_AND_EXPR,
6102 fold_build1 (NEGATE_EXPR, sizetype, vblock_addr_st),
6103 size_int ((align / BITS_PER_UNIT) - 1));
6105 /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype. */
6106 pos = size_binop (MULT_EXPR,
6107 convert (bitsizetype,
6108 size_binop (PLUS_EXPR, room_st, voffset_st)),
6109 bitsize_unit_node);
6111 /* Craft the GCC record representation. We exceptionally do everything
6112 manually here because 1) our generic circuitry is not quite ready to
6113 handle the complex position/size expressions we are setting up, 2) we
6114 have a strong simplifying factor at hand: we know the maximum possible
6115 value of voffset, and 3) we have to set/reset at least the sizes in
6116 accordance with this maximum value anyway, as we need them to convey
6117 what should be "alloc"ated for this type.
6119 Use -1 as the 'addressable' indication for the field to prevent the
6120 creation of a bitfield. We don't need one, it would have damaging
6121 consequences on the alignment computation, and create_field_decl would
6122 make one without this special argument, for instance because of the
6123 complex position expression. */
6124 field = create_field_decl (get_identifier ("F"), type, record_type, size,
6125 pos, 1, -1);
6126 TYPE_FIELDS (record_type) = field;
6128 TYPE_ALIGN (record_type) = base_align;
6129 TYPE_USER_ALIGN (record_type) = 1;
6131 TYPE_SIZE (record_type)
6132 = size_binop (PLUS_EXPR,
6133 size_binop (MULT_EXPR, convert (bitsizetype, size),
6134 bitsize_unit_node),
6135 bitsize_int (align + room * BITS_PER_UNIT));
6136 TYPE_SIZE_UNIT (record_type)
6137 = size_binop (PLUS_EXPR, size,
6138 size_int (room + align / BITS_PER_UNIT));
6140 SET_TYPE_MODE (record_type, BLKmode);
6141 relate_alias_sets (record_type, type, ALIAS_SET_COPY);
6143 /* Declare it now since it will never be declared otherwise. This is
6144 necessary to ensure that its subtrees are properly marked. */
6145 create_type_decl (name, record_type, NULL, true, false, Empty);
6147 return record_type;
6150 /* Return the result of rounding T up to ALIGN. */
6152 static inline unsigned HOST_WIDE_INT
6153 round_up_to_align (unsigned HOST_WIDE_INT t, unsigned int align)
6155 t += align - 1;
6156 t /= align;
6157 t *= align;
6158 return t;
6161 /* TYPE is a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE that is being used
6162 as the field type of a packed record if IN_RECORD is true, or as the
6163 component type of a packed array if IN_RECORD is false. See if we can
6164 rewrite it either as a type that has a non-BLKmode, which we can pack
6165 tighter in the packed record case, or as a smaller type. If so, return
6166 the new type. If not, return the original type. */
6168 static tree
6169 make_packable_type (tree type, bool in_record)
6171 unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
6172 unsigned HOST_WIDE_INT new_size;
6173 tree new_type, old_field, field_list = NULL_TREE;
6175 /* No point in doing anything if the size is zero. */
6176 if (size == 0)
6177 return type;
6179 new_type = make_node (TREE_CODE (type));
6181 /* Copy the name and flags from the old type to that of the new.
6182 Note that we rely on the pointer equality created here for
6183 TYPE_NAME to look through conversions in various places. */
6184 TYPE_NAME (new_type) = TYPE_NAME (type);
6185 TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
6186 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
6187 if (TREE_CODE (type) == RECORD_TYPE)
6188 TYPE_PADDING_P (new_type) = TYPE_PADDING_P (type);
6190 /* If we are in a record and have a small size, set the alignment to
6191 try for an integral mode. Otherwise set it to try for a smaller
6192 type with BLKmode. */
6193 if (in_record && size <= MAX_FIXED_MODE_SIZE)
6195 TYPE_ALIGN (new_type) = ceil_alignment (size);
6196 new_size = round_up_to_align (size, TYPE_ALIGN (new_type));
6198 else
6200 unsigned HOST_WIDE_INT align;
6202 /* Do not try to shrink the size if the RM size is not constant. */
6203 if (TYPE_CONTAINS_TEMPLATE_P (type)
6204 || !host_integerp (TYPE_ADA_SIZE (type), 1))
6205 return type;
6207 /* Round the RM size up to a unit boundary to get the minimal size
6208 for a BLKmode record. Give up if it's already the size. */
6209 new_size = TREE_INT_CST_LOW (TYPE_ADA_SIZE (type));
6210 new_size = round_up_to_align (new_size, BITS_PER_UNIT);
6211 if (new_size == size)
6212 return type;
6214 align = new_size & -new_size;
6215 TYPE_ALIGN (new_type) = MIN (TYPE_ALIGN (type), align);
6218 TYPE_USER_ALIGN (new_type) = 1;
6220 /* Now copy the fields, keeping the position and size as we don't want
6221 to change the layout by propagating the packedness downwards. */
6222 for (old_field = TYPE_FIELDS (type); old_field;
6223 old_field = DECL_CHAIN (old_field))
6225 tree new_field_type = TREE_TYPE (old_field);
6226 tree new_field, new_size;
6228 if ((TREE_CODE (new_field_type) == RECORD_TYPE
6229 || TREE_CODE (new_field_type) == UNION_TYPE
6230 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
6231 && !TYPE_FAT_POINTER_P (new_field_type)
6232 && host_integerp (TYPE_SIZE (new_field_type), 1))
6233 new_field_type = make_packable_type (new_field_type, true);
6235 /* However, for the last field in a not already packed record type
6236 that is of an aggregate type, we need to use the RM size in the
6237 packable version of the record type, see finish_record_type. */
6238 if (!DECL_CHAIN (old_field)
6239 && !TYPE_PACKED (type)
6240 && (TREE_CODE (new_field_type) == RECORD_TYPE
6241 || TREE_CODE (new_field_type) == UNION_TYPE
6242 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
6243 && !TYPE_FAT_POINTER_P (new_field_type)
6244 && !TYPE_CONTAINS_TEMPLATE_P (new_field_type)
6245 && TYPE_ADA_SIZE (new_field_type))
6246 new_size = TYPE_ADA_SIZE (new_field_type);
6247 else
6248 new_size = DECL_SIZE (old_field);
6250 new_field
6251 = create_field_decl (DECL_NAME (old_field), new_field_type, new_type,
6252 new_size, bit_position (old_field),
6253 TYPE_PACKED (type),
6254 !DECL_NONADDRESSABLE_P (old_field));
6256 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
6257 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
6258 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
6259 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
6261 DECL_CHAIN (new_field) = field_list;
6262 field_list = new_field;
6265 finish_record_type (new_type, nreverse (field_list), 2, false);
6266 relate_alias_sets (new_type, type, ALIAS_SET_COPY);
6268 /* If this is a padding record, we never want to make the size smaller
6269 than what was specified. For QUAL_UNION_TYPE, also copy the size. */
6270 if (TYPE_IS_PADDING_P (type) || TREE_CODE (type) == QUAL_UNION_TYPE)
6272 TYPE_SIZE (new_type) = TYPE_SIZE (type);
6273 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
6274 new_size = size;
6276 else
6278 TYPE_SIZE (new_type) = bitsize_int (new_size);
6279 TYPE_SIZE_UNIT (new_type)
6280 = size_int ((new_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
6283 if (!TYPE_CONTAINS_TEMPLATE_P (type))
6284 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (type));
6286 compute_record_mode (new_type);
6288 /* Try harder to get a packable type if necessary, for example
6289 in case the record itself contains a BLKmode field. */
6290 if (in_record && TYPE_MODE (new_type) == BLKmode)
6291 SET_TYPE_MODE (new_type,
6292 mode_for_size_tree (TYPE_SIZE (new_type), MODE_INT, 1));
6294 /* If neither the mode nor the size has shrunk, return the old type. */
6295 if (TYPE_MODE (new_type) == BLKmode && new_size >= size)
6296 return type;
6298 return new_type;
6301 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
6302 if needed. We have already verified that SIZE and TYPE are large enough.
6303 GNAT_ENTITY is used to name the resulting record and to issue a warning.
6304 IS_COMPONENT_TYPE is true if this is being done for the component type
6305 of an array. IS_USER_TYPE is true if we must complete the original type.
6306 DEFINITION is true if this type is being defined. SAME_RM_SIZE is true
6307 if the RM size of the resulting type is to be set to SIZE too; otherwise,
6308 it's set to the RM size of the original type. */
6310 tree
6311 maybe_pad_type (tree type, tree size, unsigned int align,
6312 Entity_Id gnat_entity, bool is_component_type,
6313 bool is_user_type, bool definition, bool same_rm_size)
6315 tree orig_rm_size = same_rm_size ? NULL_TREE : rm_size (type);
6316 tree orig_size = TYPE_SIZE (type);
6317 tree record, field;
6319 /* If TYPE is a padded type, see if it agrees with any size and alignment
6320 we were given. If so, return the original type. Otherwise, strip
6321 off the padding, since we will either be returning the inner type
6322 or repadding it. If no size or alignment is specified, use that of
6323 the original padded type. */
6324 if (TYPE_IS_PADDING_P (type))
6326 if ((!size
6327 || operand_equal_p (round_up (size,
6328 MAX (align, TYPE_ALIGN (type))),
6329 round_up (TYPE_SIZE (type),
6330 MAX (align, TYPE_ALIGN (type))),
6332 && (align == 0 || align == TYPE_ALIGN (type)))
6333 return type;
6335 if (!size)
6336 size = TYPE_SIZE (type);
6337 if (align == 0)
6338 align = TYPE_ALIGN (type);
6340 type = TREE_TYPE (TYPE_FIELDS (type));
6341 orig_size = TYPE_SIZE (type);
6344 /* If the size is either not being changed or is being made smaller (which
6345 is not done here and is only valid for bitfields anyway), show the size
6346 isn't changing. Likewise, clear the alignment if it isn't being
6347 changed. Then return if we aren't doing anything. */
6348 if (size
6349 && (operand_equal_p (size, orig_size, 0)
6350 || (TREE_CODE (orig_size) == INTEGER_CST
6351 && tree_int_cst_lt (size, orig_size))))
6352 size = NULL_TREE;
6354 if (align == TYPE_ALIGN (type))
6355 align = 0;
6357 if (align == 0 && !size)
6358 return type;
6360 /* If requested, complete the original type and give it a name. */
6361 if (is_user_type)
6362 create_type_decl (get_entity_name (gnat_entity), type,
6363 NULL, !Comes_From_Source (gnat_entity),
6364 !(TYPE_NAME (type)
6365 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6366 && DECL_IGNORED_P (TYPE_NAME (type))),
6367 gnat_entity);
6369 /* We used to modify the record in place in some cases, but that could
6370 generate incorrect debugging information. So make a new record
6371 type and name. */
6372 record = make_node (RECORD_TYPE);
6373 TYPE_PADDING_P (record) = 1;
6375 if (Present (gnat_entity))
6376 TYPE_NAME (record) = create_concat_name (gnat_entity, "PAD");
6378 TYPE_VOLATILE (record)
6379 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
6381 TYPE_ALIGN (record) = align;
6382 TYPE_SIZE (record) = size ? size : orig_size;
6383 TYPE_SIZE_UNIT (record)
6384 = convert (sizetype,
6385 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
6386 bitsize_unit_node));
6388 /* If we are changing the alignment and the input type is a record with
6389 BLKmode and a small constant size, try to make a form that has an
6390 integral mode. This might allow the padding record to also have an
6391 integral mode, which will be much more efficient. There is no point
6392 in doing so if a size is specified unless it is also a small constant
6393 size and it is incorrect to do so if we cannot guarantee that the mode
6394 will be naturally aligned since the field must always be addressable.
6396 ??? This might not always be a win when done for a stand-alone object:
6397 since the nominal and the effective type of the object will now have
6398 different modes, a VIEW_CONVERT_EXPR will be required for converting
6399 between them and it might be hard to overcome afterwards, including
6400 at the RTL level when the stand-alone object is accessed as a whole. */
6401 if (align != 0
6402 && TREE_CODE (type) == RECORD_TYPE
6403 && TYPE_MODE (type) == BLKmode
6404 && TREE_CODE (orig_size) == INTEGER_CST
6405 && !TREE_OVERFLOW (orig_size)
6406 && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
6407 && (!size
6408 || (TREE_CODE (size) == INTEGER_CST
6409 && compare_tree_int (size, MAX_FIXED_MODE_SIZE) <= 0)))
6411 tree packable_type = make_packable_type (type, true);
6412 if (TYPE_MODE (packable_type) != BLKmode
6413 && align >= TYPE_ALIGN (packable_type))
6414 type = packable_type;
6417 /* Now create the field with the original size. */
6418 field = create_field_decl (get_identifier ("F"), type, record, orig_size,
6419 bitsize_zero_node, 0, 1);
6420 DECL_INTERNAL_P (field) = 1;
6422 /* Do not emit debug info until after the auxiliary record is built. */
6423 finish_record_type (record, field, 1, false);
6425 /* Set the same size for its RM size if requested; otherwise reuse
6426 the RM size of the original type. */
6427 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : orig_rm_size);
6429 /* Unless debugging information isn't being written for the input type,
6430 write a record that shows what we are a subtype of and also make a
6431 variable that indicates our size, if still variable. */
6432 if (TREE_CODE (orig_size) != INTEGER_CST
6433 && TYPE_NAME (record)
6434 && TYPE_NAME (type)
6435 && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6436 && DECL_IGNORED_P (TYPE_NAME (type))))
6438 tree marker = make_node (RECORD_TYPE);
6439 tree name = TYPE_NAME (record);
6440 tree orig_name = TYPE_NAME (type);
6442 if (TREE_CODE (name) == TYPE_DECL)
6443 name = DECL_NAME (name);
6445 if (TREE_CODE (orig_name) == TYPE_DECL)
6446 orig_name = DECL_NAME (orig_name);
6448 TYPE_NAME (marker) = concat_name (name, "XVS");
6449 finish_record_type (marker,
6450 create_field_decl (orig_name,
6451 build_reference_type (type),
6452 marker, NULL_TREE, NULL_TREE,
6453 0, 0),
6454 0, true);
6456 add_parallel_type (TYPE_STUB_DECL (record), marker);
6458 if (definition && size && TREE_CODE (size) != INTEGER_CST)
6459 TYPE_SIZE_UNIT (marker)
6460 = create_var_decl (concat_name (name, "XVZ"), NULL_TREE, sizetype,
6461 TYPE_SIZE_UNIT (record), false, false, false,
6462 false, NULL, gnat_entity);
6465 rest_of_record_type_compilation (record);
6467 /* If the size was widened explicitly, maybe give a warning. Take the
6468 original size as the maximum size of the input if there was an
6469 unconstrained record involved and round it up to the specified alignment,
6470 if one was specified. */
6471 if (CONTAINS_PLACEHOLDER_P (orig_size))
6472 orig_size = max_size (orig_size, true);
6474 if (align)
6475 orig_size = round_up (orig_size, align);
6477 if (Present (gnat_entity)
6478 && size
6479 && TREE_CODE (size) != MAX_EXPR
6480 && TREE_CODE (size) != COND_EXPR
6481 && !operand_equal_p (size, orig_size, 0)
6482 && !(TREE_CODE (size) == INTEGER_CST
6483 && TREE_CODE (orig_size) == INTEGER_CST
6484 && (TREE_OVERFLOW (size)
6485 || TREE_OVERFLOW (orig_size)
6486 || tree_int_cst_lt (size, orig_size))))
6488 Node_Id gnat_error_node = Empty;
6490 if (Is_Packed_Array_Type (gnat_entity))
6491 gnat_entity = Original_Array_Type (gnat_entity);
6493 if ((Ekind (gnat_entity) == E_Component
6494 || Ekind (gnat_entity) == E_Discriminant)
6495 && Present (Component_Clause (gnat_entity)))
6496 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
6497 else if (Present (Size_Clause (gnat_entity)))
6498 gnat_error_node = Expression (Size_Clause (gnat_entity));
6500 /* Generate message only for entities that come from source, since
6501 if we have an entity created by expansion, the message will be
6502 generated for some other corresponding source entity. */
6503 if (Comes_From_Source (gnat_entity))
6505 if (Present (gnat_error_node))
6506 post_error_ne_tree ("{^ }bits of & unused?",
6507 gnat_error_node, gnat_entity,
6508 size_diffop (size, orig_size));
6509 else if (is_component_type)
6510 post_error_ne_tree ("component of& padded{ by ^ bits}?",
6511 gnat_entity, gnat_entity,
6512 size_diffop (size, orig_size));
6516 return record;
6519 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6520 the value passed against the list of choices. */
6522 tree
6523 choices_to_gnu (tree operand, Node_Id choices)
6525 Node_Id choice;
6526 Node_Id gnat_temp;
6527 tree result = integer_zero_node;
6528 tree this_test, low = 0, high = 0, single = 0;
6530 for (choice = First (choices); Present (choice); choice = Next (choice))
6532 switch (Nkind (choice))
6534 case N_Range:
6535 low = gnat_to_gnu (Low_Bound (choice));
6536 high = gnat_to_gnu (High_Bound (choice));
6538 this_test
6539 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6540 build_binary_op (GE_EXPR, boolean_type_node,
6541 operand, low),
6542 build_binary_op (LE_EXPR, boolean_type_node,
6543 operand, high));
6545 break;
6547 case N_Subtype_Indication:
6548 gnat_temp = Range_Expression (Constraint (choice));
6549 low = gnat_to_gnu (Low_Bound (gnat_temp));
6550 high = gnat_to_gnu (High_Bound (gnat_temp));
6552 this_test
6553 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6554 build_binary_op (GE_EXPR, boolean_type_node,
6555 operand, low),
6556 build_binary_op (LE_EXPR, boolean_type_node,
6557 operand, high));
6558 break;
6560 case N_Identifier:
6561 case N_Expanded_Name:
6562 /* This represents either a subtype range, an enumeration
6563 literal, or a constant Ekind says which. If an enumeration
6564 literal or constant, fall through to the next case. */
6565 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6566 && Ekind (Entity (choice)) != E_Constant)
6568 tree type = gnat_to_gnu_type (Entity (choice));
6570 low = TYPE_MIN_VALUE (type);
6571 high = TYPE_MAX_VALUE (type);
6573 this_test
6574 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6575 build_binary_op (GE_EXPR, boolean_type_node,
6576 operand, low),
6577 build_binary_op (LE_EXPR, boolean_type_node,
6578 operand, high));
6579 break;
6582 /* ... fall through ... */
6584 case N_Character_Literal:
6585 case N_Integer_Literal:
6586 single = gnat_to_gnu (choice);
6587 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6588 single);
6589 break;
6591 case N_Others_Choice:
6592 this_test = integer_one_node;
6593 break;
6595 default:
6596 gcc_unreachable ();
6599 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6600 this_test);
6603 return result;
6606 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6607 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6609 static int
6610 adjust_packed (tree field_type, tree record_type, int packed)
6612 /* If the field contains an item of variable size, we cannot pack it
6613 because we cannot create temporaries of non-fixed size in case
6614 we need to take the address of the field. See addressable_p and
6615 the notes on the addressability issues for further details. */
6616 if (is_variable_size (field_type))
6617 return 0;
6619 /* If the alignment of the record is specified and the field type
6620 is over-aligned, request Storage_Unit alignment for the field. */
6621 if (packed == -2)
6623 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6624 return -1;
6625 else
6626 return 0;
6629 return packed;
6632 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6633 placed in GNU_RECORD_TYPE.
6635 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6636 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6637 record has a specified alignment.
6639 DEFINITION is true if this field is for a record being defined.
6641 DEBUG_INFO_P is true if we need to write debug information for types
6642 that we may create in the process. */
6644 static tree
6645 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6646 bool definition, bool debug_info_p)
6648 tree gnu_field_id = get_entity_name (gnat_field);
6649 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
6650 tree gnu_field, gnu_size, gnu_pos;
6651 bool needs_strict_alignment
6652 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
6653 || Treat_As_Volatile (gnat_field));
6655 /* If this field requires strict alignment, we cannot pack it because
6656 it would very likely be under-aligned in the record. */
6657 if (needs_strict_alignment)
6658 packed = 0;
6659 else
6660 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6662 /* If a size is specified, use it. Otherwise, if the record type is packed,
6663 use the official RM size. See "Handling of Type'Size Values" in Einfo
6664 for further details. */
6665 if (Known_Static_Esize (gnat_field))
6666 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6667 gnat_field, FIELD_DECL, false, true);
6668 else if (packed == 1)
6669 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
6670 gnat_field, FIELD_DECL, false, true);
6671 else
6672 gnu_size = NULL_TREE;
6674 /* If we have a specified size that is smaller than that of the field's type,
6675 or a position is specified, and the field's type is a record that doesn't
6676 require strict alignment, see if we can get either an integral mode form
6677 of the type or a smaller form. If we can, show a size was specified for
6678 the field if there wasn't one already, so we know to make this a bitfield
6679 and avoid making things wider.
6681 Changing to an integral mode form is useful when the record is packed as
6682 we can then place the field at a non-byte-aligned position and so achieve
6683 tighter packing. This is in addition required if the field shares a byte
6684 with another field and the front-end lets the back-end handle the access
6685 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6687 Changing to a smaller form is required if the specified size is smaller
6688 than that of the field's type and the type contains sub-fields that are
6689 padded, in order to avoid generating accesses to these sub-fields that
6690 are wider than the field.
6692 We avoid the transformation if it is not required or potentially useful,
6693 as it might entail an increase of the field's alignment and have ripple
6694 effects on the outer record type. A typical case is a field known to be
6695 byte-aligned and not to share a byte with another field. */
6696 if (!needs_strict_alignment
6697 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6698 && !TYPE_FAT_POINTER_P (gnu_field_type)
6699 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
6700 && (packed == 1
6701 || (gnu_size
6702 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6703 || (Present (Component_Clause (gnat_field))
6704 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6705 % BITS_PER_UNIT == 0
6706 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6708 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6709 if (gnu_packable_type != gnu_field_type)
6711 gnu_field_type = gnu_packable_type;
6712 if (!gnu_size)
6713 gnu_size = rm_size (gnu_field_type);
6717 /* If we are packing the record and the field is BLKmode, round the
6718 size up to a byte boundary. */
6719 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6720 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6722 if (Present (Component_Clause (gnat_field)))
6724 Entity_Id gnat_parent
6725 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6727 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6728 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6729 gnat_field, FIELD_DECL, false, true);
6731 /* Ensure the position does not overlap with the parent subtype, if there
6732 is one. This test is omitted if the parent of the tagged type has a
6733 full rep clause since, in this case, component clauses are allowed to
6734 overlay the space allocated for the parent type and the front-end has
6735 checked that there are no overlapping components. */
6736 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6738 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6740 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6741 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6743 post_error_ne_tree
6744 ("offset of& must be beyond parent{, minimum allowed is ^}",
6745 First_Bit (Component_Clause (gnat_field)), gnat_field,
6746 TYPE_SIZE_UNIT (gnu_parent));
6750 /* If this field needs strict alignment, ensure the record is
6751 sufficiently aligned and that that position and size are
6752 consistent with the alignment. */
6753 if (needs_strict_alignment)
6755 TYPE_ALIGN (gnu_record_type)
6756 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6758 if (gnu_size
6759 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6761 if (Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
6762 post_error_ne_tree
6763 ("atomic field& must be natural size of type{ (^)}",
6764 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6765 TYPE_SIZE (gnu_field_type));
6767 else if (Is_Aliased (gnat_field))
6768 post_error_ne_tree
6769 ("size of aliased field& must be ^ bits",
6770 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6771 TYPE_SIZE (gnu_field_type));
6773 else if (Strict_Alignment (Etype (gnat_field)))
6774 post_error_ne_tree
6775 ("size of & with aliased or tagged components not ^ bits",
6776 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6777 TYPE_SIZE (gnu_field_type));
6779 gnu_size = NULL_TREE;
6782 if (!integer_zerop (size_binop
6783 (TRUNC_MOD_EXPR, gnu_pos,
6784 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6786 if (Is_Aliased (gnat_field))
6787 post_error_ne_num
6788 ("position of aliased field& must be multiple of ^ bits",
6789 First_Bit (Component_Clause (gnat_field)), gnat_field,
6790 TYPE_ALIGN (gnu_field_type));
6792 else if (Treat_As_Volatile (gnat_field))
6793 post_error_ne_num
6794 ("position of volatile field& must be multiple of ^ bits",
6795 First_Bit (Component_Clause (gnat_field)), gnat_field,
6796 TYPE_ALIGN (gnu_field_type));
6798 else if (Strict_Alignment (Etype (gnat_field)))
6799 post_error_ne_num
6800 ("position of & with aliased or tagged components not multiple of ^ bits",
6801 First_Bit (Component_Clause (gnat_field)), gnat_field,
6802 TYPE_ALIGN (gnu_field_type));
6804 else
6805 gcc_unreachable ();
6807 gnu_pos = NULL_TREE;
6811 if (Is_Atomic (gnat_field))
6812 check_ok_for_atomic (gnu_field_type, gnat_field, false);
6815 /* If the record has rep clauses and this is the tag field, make a rep
6816 clause for it as well. */
6817 else if (Has_Specified_Layout (Scope (gnat_field))
6818 && Chars (gnat_field) == Name_uTag)
6820 gnu_pos = bitsize_zero_node;
6821 gnu_size = TYPE_SIZE (gnu_field_type);
6824 else
6825 gnu_pos = NULL_TREE;
6827 /* We need to make the size the maximum for the type if it is
6828 self-referential and an unconstrained type. In that case, we can't
6829 pack the field since we can't make a copy to align it. */
6830 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6831 && !gnu_size
6832 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6833 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
6835 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6836 packed = 0;
6839 /* If a size is specified, adjust the field's type to it. */
6840 if (gnu_size)
6842 tree orig_field_type;
6844 /* If the field's type is justified modular, we would need to remove
6845 the wrapper to (better) meet the layout requirements. However we
6846 can do so only if the field is not aliased to preserve the unique
6847 layout and if the prescribed size is not greater than that of the
6848 packed array to preserve the justification. */
6849 if (!needs_strict_alignment
6850 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6851 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6852 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6853 <= 0)
6854 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6856 gnu_field_type
6857 = make_type_from_size (gnu_field_type, gnu_size,
6858 Has_Biased_Representation (gnat_field));
6860 orig_field_type = gnu_field_type;
6861 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6862 false, false, definition, true);
6864 /* If a padding record was made, declare it now since it will never be
6865 declared otherwise. This is necessary to ensure that its subtrees
6866 are properly marked. */
6867 if (gnu_field_type != orig_field_type
6868 && !DECL_P (TYPE_NAME (gnu_field_type)))
6869 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, NULL,
6870 true, debug_info_p, gnat_field);
6873 /* Otherwise (or if there was an error), don't specify a position. */
6874 else
6875 gnu_pos = NULL_TREE;
6877 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6878 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6880 /* Now create the decl for the field. */
6881 gnu_field
6882 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6883 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6884 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6885 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
6887 if (Ekind (gnat_field) == E_Discriminant)
6888 DECL_DISCRIMINANT_NUMBER (gnu_field)
6889 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6891 return gnu_field;
6894 /* Return true if TYPE is a type with variable size, a padding type with a
6895 field of variable size or is a record that has a field such a field. */
6897 static bool
6898 is_variable_size (tree type)
6900 tree field;
6902 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6903 return true;
6905 if (TYPE_IS_PADDING_P (type)
6906 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6907 return true;
6909 if (TREE_CODE (type) != RECORD_TYPE
6910 && TREE_CODE (type) != UNION_TYPE
6911 && TREE_CODE (type) != QUAL_UNION_TYPE)
6912 return false;
6914 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6915 if (is_variable_size (TREE_TYPE (field)))
6916 return true;
6918 return false;
6921 /* qsort comparer for the bit positions of two record components. */
6923 static int
6924 compare_field_bitpos (const PTR rt1, const PTR rt2)
6926 const_tree const field1 = * (const_tree const *) rt1;
6927 const_tree const field2 = * (const_tree const *) rt2;
6928 const int ret
6929 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6931 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6934 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set
6935 the result as the field list of GNU_RECORD_TYPE and finish it up. When
6936 called from gnat_to_gnu_entity during the processing of a record type
6937 definition, the GCC node for the parent, if any, will be the single field
6938 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6939 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6940 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6942 PACKED is 1 if this is for a packed record, -1 if this is for a record
6943 with Component_Alignment of Storage_Unit, -2 if this is for a record
6944 with a specified alignment.
6946 DEFINITION is true if we are defining this record type.
6948 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6949 with a rep clause is to be added; in this case, that is all that should
6950 be done with such fields.
6952 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6953 out the record. This means the alignment only serves to force fields to
6954 be bitfields, but not to require the record to be that aligned. This is
6955 used for variants.
6957 ALL_REP is true if a rep clause is present for all the fields.
6959 UNCHECKED_UNION is true if we are building this type for a record with a
6960 Pragma Unchecked_Union.
6962 DEBUG_INFO_P is true if we need to write debug information about the type.
6964 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6965 mean that its contents may be unused as well, but only the container. */
6968 static void
6969 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6970 tree gnu_field_list, int packed, bool definition,
6971 tree *p_gnu_rep_list, bool cancel_alignment,
6972 bool all_rep, bool unchecked_union, bool debug_info_p,
6973 bool maybe_unused)
6975 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6976 bool layout_with_rep = false;
6977 Node_Id component_decl, variant_part;
6978 tree gnu_our_rep_list = NULL_TREE;
6979 tree gnu_field, gnu_next, gnu_last = tree_last (gnu_field_list);
6981 /* For each component referenced in a component declaration create a GCC
6982 field and add it to the list, skipping pragmas in the GNAT list. */
6983 if (Present (Component_Items (gnat_component_list)))
6984 for (component_decl
6985 = First_Non_Pragma (Component_Items (gnat_component_list));
6986 Present (component_decl);
6987 component_decl = Next_Non_Pragma (component_decl))
6989 Entity_Id gnat_field = Defining_Entity (component_decl);
6990 Name_Id gnat_name = Chars (gnat_field);
6992 /* If present, the _Parent field must have been created as the single
6993 field of the record type. Put it before any other fields. */
6994 if (gnat_name == Name_uParent)
6996 gnu_field = TYPE_FIELDS (gnu_record_type);
6997 gnu_field_list = chainon (gnu_field_list, gnu_field);
6999 else
7001 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
7002 definition, debug_info_p);
7004 /* If this is the _Tag field, put it before any other fields. */
7005 if (gnat_name == Name_uTag)
7006 gnu_field_list = chainon (gnu_field_list, gnu_field);
7008 /* If this is the _Controller field, put it before the other
7009 fields except for the _Tag or _Parent field. */
7010 else if (gnat_name == Name_uController && gnu_last)
7012 DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
7013 DECL_CHAIN (gnu_last) = gnu_field;
7016 /* If this is a regular field, put it after the other fields. */
7017 else
7019 DECL_CHAIN (gnu_field) = gnu_field_list;
7020 gnu_field_list = gnu_field;
7021 if (!gnu_last)
7022 gnu_last = gnu_field;
7026 save_gnu_tree (gnat_field, gnu_field, false);
7029 /* At the end of the component list there may be a variant part. */
7030 variant_part = Variant_Part (gnat_component_list);
7032 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7033 mutually exclusive and should go in the same memory. To do this we need
7034 to treat each variant as a record whose elements are created from the
7035 component list for the variant. So here we create the records from the
7036 lists for the variants and put them all into the QUAL_UNION_TYPE.
7037 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7038 use GNU_RECORD_TYPE if there are no fields so far. */
7039 if (Present (variant_part))
7041 Node_Id gnat_discr = Name (variant_part), variant;
7042 tree gnu_discr = gnat_to_gnu (gnat_discr);
7043 tree gnu_name = TYPE_NAME (gnu_record_type);
7044 tree gnu_var_name
7045 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
7046 "XVN");
7047 tree gnu_union_type, gnu_union_name, gnu_union_field;
7048 tree gnu_variant_list = NULL_TREE;
7050 if (TREE_CODE (gnu_name) == TYPE_DECL)
7051 gnu_name = DECL_NAME (gnu_name);
7053 gnu_union_name
7054 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
7056 /* Reuse an enclosing union if all fields are in the variant part
7057 and there is no representation clause on the record, to match
7058 the layout of C unions. There is an associated check below. */
7059 if (!gnu_field_list
7060 && TREE_CODE (gnu_record_type) == UNION_TYPE
7061 && !TYPE_PACKED (gnu_record_type))
7062 gnu_union_type = gnu_record_type;
7063 else
7065 gnu_union_type
7066 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
7068 TYPE_NAME (gnu_union_type) = gnu_union_name;
7069 TYPE_ALIGN (gnu_union_type) = 0;
7070 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
7073 for (variant = First_Non_Pragma (Variants (variant_part));
7074 Present (variant);
7075 variant = Next_Non_Pragma (variant))
7077 tree gnu_variant_type = make_node (RECORD_TYPE);
7078 tree gnu_inner_name;
7079 tree gnu_qual;
7081 Get_Variant_Encoding (variant);
7082 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7083 TYPE_NAME (gnu_variant_type)
7084 = concat_name (gnu_union_name,
7085 IDENTIFIER_POINTER (gnu_inner_name));
7087 /* Set the alignment of the inner type in case we need to make
7088 inner objects into bitfields, but then clear it out so the
7089 record actually gets only the alignment required. */
7090 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7091 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7093 /* Similarly, if the outer record has a size specified and all
7094 fields have record rep clauses, we can propagate the size
7095 into the variant part. */
7096 if (all_rep_and_size)
7098 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7099 TYPE_SIZE_UNIT (gnu_variant_type)
7100 = TYPE_SIZE_UNIT (gnu_record_type);
7103 /* Add the fields into the record type for the variant. Note that
7104 we aren't sure to really use it at this point, see below. */
7105 components_to_record (gnu_variant_type, Component_List (variant),
7106 NULL_TREE, packed, definition,
7107 &gnu_our_rep_list, !all_rep_and_size, all_rep,
7108 unchecked_union, debug_info_p, true);
7110 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7112 Set_Present_Expr (variant, annotate_value (gnu_qual));
7114 /* If this is an Unchecked_Union and we have exactly one field,
7115 use this field directly to match the layout of C unions. */
7116 if (unchecked_union
7117 && TYPE_FIELDS (gnu_variant_type)
7118 && !DECL_CHAIN (TYPE_FIELDS (gnu_variant_type)))
7119 gnu_field = TYPE_FIELDS (gnu_variant_type);
7120 else
7122 /* Deal with packedness like in gnat_to_gnu_field. */
7123 int field_packed
7124 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7126 /* Finalize the record type now. We used to throw away
7127 empty records but we no longer do that because we need
7128 them to generate complete debug info for the variant;
7129 otherwise, the union type definition will be lacking
7130 the fields associated with these empty variants. */
7131 rest_of_record_type_compilation (gnu_variant_type);
7132 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7133 NULL, true, debug_info_p, gnat_component_list);
7135 gnu_field
7136 = create_field_decl (gnu_inner_name, gnu_variant_type,
7137 gnu_union_type,
7138 all_rep_and_size
7139 ? TYPE_SIZE (gnu_variant_type) : 0,
7140 all_rep_and_size
7141 ? bitsize_zero_node : 0,
7142 field_packed, 0);
7144 DECL_INTERNAL_P (gnu_field) = 1;
7146 if (!unchecked_union)
7147 DECL_QUALIFIER (gnu_field) = gnu_qual;
7150 DECL_CHAIN (gnu_field) = gnu_variant_list;
7151 gnu_variant_list = gnu_field;
7154 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7155 if (gnu_variant_list)
7157 int union_field_packed;
7159 if (all_rep_and_size)
7161 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7162 TYPE_SIZE_UNIT (gnu_union_type)
7163 = TYPE_SIZE_UNIT (gnu_record_type);
7166 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7167 all_rep_and_size ? 1 : 0, debug_info_p);
7169 /* If GNU_UNION_TYPE is our record type, it means we must have an
7170 Unchecked_Union with no fields. Verify that and, if so, just
7171 return. */
7172 if (gnu_union_type == gnu_record_type)
7174 gcc_assert (unchecked_union
7175 && !gnu_field_list
7176 && !gnu_our_rep_list);
7177 return;
7180 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type,
7181 NULL, true, debug_info_p, gnat_component_list);
7183 /* Deal with packedness like in gnat_to_gnu_field. */
7184 union_field_packed
7185 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7187 gnu_union_field
7188 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7189 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
7190 all_rep ? bitsize_zero_node : 0,
7191 union_field_packed, 0);
7193 DECL_INTERNAL_P (gnu_union_field) = 1;
7194 DECL_CHAIN (gnu_union_field) = gnu_field_list;
7195 gnu_field_list = gnu_union_field;
7199 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
7200 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do
7201 this in a separate pass since we want to handle the discriminants but
7202 can't play with them until we've used them in debugging data above.
7204 ??? If we then reorder them, debugging information will be wrong but
7205 there's nothing that can be done about this at the moment. */
7206 gnu_last = NULL_TREE;
7207 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7209 gnu_next = DECL_CHAIN (gnu_field);
7211 if (DECL_FIELD_OFFSET (gnu_field))
7213 if (!gnu_last)
7214 gnu_field_list = gnu_next;
7215 else
7216 DECL_CHAIN (gnu_last) = gnu_next;
7218 DECL_CHAIN (gnu_field) = gnu_our_rep_list;
7219 gnu_our_rep_list = gnu_field;
7221 else
7222 gnu_last = gnu_field;
7225 /* If we have any fields in our rep'ed field list and it is not the case that
7226 all the fields in the record have rep clauses and P_REP_LIST is nonzero,
7227 set it and ignore these fields. */
7228 if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
7229 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
7231 /* Otherwise, sort the fields by bit position and put them into their own
7232 record, before the others, if we also have fields without rep clauses. */
7233 else if (gnu_our_rep_list)
7235 tree gnu_rep_type
7236 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
7237 int i, len = list_length (gnu_our_rep_list);
7238 tree *gnu_arr = XALLOCAVEC (tree, len);
7240 for (gnu_field = gnu_our_rep_list, i = 0;
7241 gnu_field;
7242 gnu_field = DECL_CHAIN (gnu_field), i++)
7243 gnu_arr[i] = gnu_field;
7245 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7247 /* Put the fields in the list in order of increasing position, which
7248 means we start from the end. */
7249 gnu_our_rep_list = NULL_TREE;
7250 for (i = len - 1; i >= 0; i--)
7252 DECL_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
7253 gnu_our_rep_list = gnu_arr[i];
7254 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7257 if (gnu_field_list)
7259 finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, debug_info_p);
7260 gnu_field
7261 = create_field_decl (get_identifier ("REP"), gnu_rep_type,
7262 gnu_record_type, NULL_TREE, NULL_TREE, 0, 1);
7263 DECL_INTERNAL_P (gnu_field) = 1;
7264 gnu_field_list = chainon (gnu_field_list, gnu_field);
7266 else
7268 layout_with_rep = true;
7269 gnu_field_list = nreverse (gnu_our_rep_list);
7273 if (cancel_alignment)
7274 TYPE_ALIGN (gnu_record_type) = 0;
7276 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
7277 layout_with_rep ? 1 : 0, debug_info_p && !maybe_unused);
7280 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7281 placed into an Esize, Component_Bit_Offset, or Component_Size value
7282 in the GNAT tree. */
7284 static Uint
7285 annotate_value (tree gnu_size)
7287 TCode tcode;
7288 Node_Ref_Or_Val ops[3], ret;
7289 struct tree_int_map **h = NULL;
7290 int i;
7292 /* See if we've already saved the value for this node. */
7293 if (EXPR_P (gnu_size))
7295 struct tree_int_map in;
7296 if (!annotate_value_cache)
7297 annotate_value_cache = htab_create_ggc (512, tree_int_map_hash,
7298 tree_int_map_eq, 0);
7299 in.base.from = gnu_size;
7300 h = (struct tree_int_map **)
7301 htab_find_slot (annotate_value_cache, &in, INSERT);
7303 if (*h)
7304 return (Node_Ref_Or_Val) (*h)->to;
7307 /* If we do not return inside this switch, TCODE will be set to the
7308 code to use for a Create_Node operand and LEN (set above) will be
7309 the number of recursive calls for us to make. */
7311 switch (TREE_CODE (gnu_size))
7313 case INTEGER_CST:
7314 if (TREE_OVERFLOW (gnu_size))
7315 return No_Uint;
7317 /* This may come from a conversion from some smaller type, so ensure
7318 this is in bitsizetype. */
7319 gnu_size = convert (bitsizetype, gnu_size);
7321 /* For a negative value, build NEGATE_EXPR of the opposite. Such values
7322 appear in expressions containing aligning patterns. Note that, since
7323 sizetype is sign-extended but nonetheless unsigned, we don't directly
7324 use tree_int_cst_sgn. */
7325 if (TREE_INT_CST_HIGH (gnu_size) < 0)
7327 tree op_size = fold_build1 (NEGATE_EXPR, bitsizetype, gnu_size);
7328 return annotate_value (build1 (NEGATE_EXPR, bitsizetype, op_size));
7331 return UI_From_gnu (gnu_size);
7333 case COMPONENT_REF:
7334 /* The only case we handle here is a simple discriminant reference. */
7335 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
7336 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
7337 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7338 return Create_Node (Discrim_Val,
7339 annotate_value (DECL_DISCRIMINANT_NUMBER
7340 (TREE_OPERAND (gnu_size, 1))),
7341 No_Uint, No_Uint);
7342 else
7343 return No_Uint;
7345 CASE_CONVERT: case NON_LVALUE_EXPR:
7346 return annotate_value (TREE_OPERAND (gnu_size, 0));
7348 /* Now just list the operations we handle. */
7349 case COND_EXPR: tcode = Cond_Expr; break;
7350 case PLUS_EXPR: tcode = Plus_Expr; break;
7351 case MINUS_EXPR: tcode = Minus_Expr; break;
7352 case MULT_EXPR: tcode = Mult_Expr; break;
7353 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7354 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7355 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7356 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7357 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7358 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7359 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7360 case NEGATE_EXPR: tcode = Negate_Expr; break;
7361 case MIN_EXPR: tcode = Min_Expr; break;
7362 case MAX_EXPR: tcode = Max_Expr; break;
7363 case ABS_EXPR: tcode = Abs_Expr; break;
7364 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7365 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7366 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7367 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7368 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7369 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7370 case BIT_AND_EXPR: tcode = Bit_And_Expr; break;
7371 case LT_EXPR: tcode = Lt_Expr; break;
7372 case LE_EXPR: tcode = Le_Expr; break;
7373 case GT_EXPR: tcode = Gt_Expr; break;
7374 case GE_EXPR: tcode = Ge_Expr; break;
7375 case EQ_EXPR: tcode = Eq_Expr; break;
7376 case NE_EXPR: tcode = Ne_Expr; break;
7378 case CALL_EXPR:
7380 tree t = maybe_inline_call_in_expr (gnu_size);
7381 if (t)
7382 return annotate_value (t);
7385 /* Fall through... */
7387 default:
7388 return No_Uint;
7391 /* Now get each of the operands that's relevant for this code. If any
7392 cannot be expressed as a repinfo node, say we can't. */
7393 for (i = 0; i < 3; i++)
7394 ops[i] = No_Uint;
7396 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7398 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7399 if (ops[i] == No_Uint)
7400 return No_Uint;
7403 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7405 /* Save the result in the cache. */
7406 if (h)
7408 *h = ggc_alloc_tree_int_map ();
7409 (*h)->base.from = gnu_size;
7410 (*h)->to = ret;
7413 return ret;
7416 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7417 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7418 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7419 BY_REF is true if the object is used by reference and BY_DOUBLE_REF is
7420 true if the object is used by double reference. */
7422 void
7423 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref,
7424 bool by_double_ref)
7426 if (by_ref)
7428 if (by_double_ref)
7429 gnu_type = TREE_TYPE (gnu_type);
7431 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7432 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7433 else
7434 gnu_type = TREE_TYPE (gnu_type);
7437 if (Unknown_Esize (gnat_entity))
7439 if (TREE_CODE (gnu_type) == RECORD_TYPE
7440 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7441 size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7442 else if (!size)
7443 size = TYPE_SIZE (gnu_type);
7445 if (size)
7446 Set_Esize (gnat_entity, annotate_value (size));
7449 if (Unknown_Alignment (gnat_entity))
7450 Set_Alignment (gnat_entity,
7451 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7454 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7455 Return NULL_TREE if there is no such element in the list. */
7457 static tree
7458 purpose_member_field (const_tree elem, tree list)
7460 while (list)
7462 tree field = TREE_PURPOSE (list);
7463 if (SAME_FIELD_P (field, elem))
7464 return list;
7465 list = TREE_CHAIN (list);
7467 return NULL_TREE;
7470 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7471 set Component_Bit_Offset and Esize of the components to the position and
7472 size used by Gigi. */
7474 static void
7475 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7477 Entity_Id gnat_field;
7478 tree gnu_list;
7480 /* We operate by first making a list of all fields and their position (we
7481 can get the size easily) and then update all the sizes in the tree. */
7482 gnu_list
7483 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7484 BIGGEST_ALIGNMENT, NULL_TREE);
7486 for (gnat_field = First_Entity (gnat_entity);
7487 Present (gnat_field);
7488 gnat_field = Next_Entity (gnat_field))
7489 if (Ekind (gnat_field) == E_Component
7490 || (Ekind (gnat_field) == E_Discriminant
7491 && !Is_Unchecked_Union (Scope (gnat_field))))
7493 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7494 gnu_list);
7495 if (t)
7497 tree parent_offset;
7499 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
7501 /* In this mode the tag and parent components are not
7502 generated, so we add the appropriate offset to each
7503 component. For a component appearing in the current
7504 extension, the offset is the size of the parent. */
7505 if (Is_Derived_Type (gnat_entity)
7506 && Original_Record_Component (gnat_field) == gnat_field)
7507 parent_offset
7508 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7509 bitsizetype);
7510 else
7511 parent_offset = bitsize_int (POINTER_SIZE);
7513 else
7514 parent_offset = bitsize_zero_node;
7516 Set_Component_Bit_Offset
7517 (gnat_field,
7518 annotate_value
7519 (size_binop (PLUS_EXPR,
7520 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7521 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7522 parent_offset)));
7524 Set_Esize (gnat_field,
7525 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7527 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7529 /* If there is no entry, this is an inherited component whose
7530 position is the same as in the parent type. */
7531 Set_Component_Bit_Offset
7532 (gnat_field,
7533 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7535 Set_Esize (gnat_field,
7536 Esize (Original_Record_Component (gnat_field)));
7541 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7542 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7543 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7544 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7545 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7546 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7547 pre-existing list to be chained to the newly created entries. */
7549 static tree
7550 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7551 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7553 tree gnu_field;
7555 for (gnu_field = TYPE_FIELDS (gnu_type);
7556 gnu_field;
7557 gnu_field = DECL_CHAIN (gnu_field))
7559 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7560 DECL_FIELD_BIT_OFFSET (gnu_field));
7561 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7562 DECL_FIELD_OFFSET (gnu_field));
7563 unsigned int our_offset_align
7564 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7565 tree v = make_tree_vec (3);
7567 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7568 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7569 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7570 gnu_list = tree_cons (gnu_field, v, gnu_list);
7572 /* Recurse on internal fields, flattening the nested fields except for
7573 those in the variant part, if requested. */
7574 if (DECL_INTERNAL_P (gnu_field))
7576 tree gnu_field_type = TREE_TYPE (gnu_field);
7577 if (do_not_flatten_variant
7578 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7579 gnu_list
7580 = build_position_list (gnu_field_type, do_not_flatten_variant,
7581 size_zero_node, bitsize_zero_node,
7582 BIGGEST_ALIGNMENT, gnu_list);
7583 else
7584 gnu_list
7585 = build_position_list (gnu_field_type, do_not_flatten_variant,
7586 gnu_our_offset, gnu_our_bitpos,
7587 our_offset_align, gnu_list);
7591 return gnu_list;
7594 /* Return a VEC describing the substitutions needed to reflect the
7595 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7596 be in any order. The values in an element of the VEC are in the form
7597 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7598 a definition of GNAT_SUBTYPE. */
7600 static VEC(subst_pair,heap) *
7601 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7603 VEC(subst_pair,heap) *gnu_vec = NULL;
7604 Entity_Id gnat_discrim;
7605 Node_Id gnat_value;
7607 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7608 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
7609 Present (gnat_discrim);
7610 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7611 gnat_value = Next_Elmt (gnat_value))
7612 /* Ignore access discriminants. */
7613 if (!Is_Access_Type (Etype (Node (gnat_value))))
7615 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7616 tree replacement = convert (TREE_TYPE (gnu_field),
7617 elaborate_expression
7618 (Node (gnat_value), gnat_subtype,
7619 get_entity_name (gnat_discrim),
7620 definition, true, false));
7621 subst_pair *s = VEC_safe_push (subst_pair, heap, gnu_vec, NULL);
7622 s->discriminant = gnu_field;
7623 s->replacement = replacement;
7626 return gnu_vec;
7629 /* Scan all fields in QUAL_UNION_TYPE and return a VEC describing the
7630 variants of QUAL_UNION_TYPE that are still relevant after applying
7631 the substitutions described in SUBST_LIST. VARIANT_LIST is a
7632 pre-existing VEC onto which newly created entries should be
7633 pushed. */
7635 static VEC(variant_desc,heap) *
7636 build_variant_list (tree qual_union_type, VEC(subst_pair,heap) *subst_list,
7637 VEC(variant_desc,heap) *variant_list)
7639 tree gnu_field;
7641 for (gnu_field = TYPE_FIELDS (qual_union_type);
7642 gnu_field;
7643 gnu_field = DECL_CHAIN (gnu_field))
7645 tree qual = DECL_QUALIFIER (gnu_field);
7646 unsigned ix;
7647 subst_pair *s;
7649 FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
7650 qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7652 /* If the new qualifier is not unconditionally false, its variant may
7653 still be accessed. */
7654 if (!integer_zerop (qual))
7656 variant_desc *v;
7657 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7659 v = VEC_safe_push (variant_desc, heap, variant_list, NULL);
7660 v->type = variant_type;
7661 v->field = gnu_field;
7662 v->qual = qual;
7663 v->record = NULL_TREE;
7665 /* Recurse on the variant subpart of the variant, if any. */
7666 variant_subpart = get_variant_part (variant_type);
7667 if (variant_subpart)
7668 variant_list = build_variant_list (TREE_TYPE (variant_subpart),
7669 subst_list, variant_list);
7671 /* If the new qualifier is unconditionally true, the subsequent
7672 variants cannot be accessed. */
7673 if (integer_onep (qual))
7674 break;
7678 return variant_list;
7681 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7682 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
7683 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
7684 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
7685 for the size of a field. COMPONENT_P is true if we are being called
7686 to process the Component_Size of GNAT_OBJECT. This is used for error
7687 message handling and to indicate to use the object size of GNU_TYPE.
7688 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
7689 it means that a size of zero should be treated as an unspecified size. */
7691 static tree
7692 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7693 enum tree_code kind, bool component_p, bool zero_ok)
7695 Node_Id gnat_error_node;
7696 tree type_size, size;
7698 /* Return 0 if no size was specified. */
7699 if (uint_size == No_Uint)
7700 return NULL_TREE;
7702 /* Ignore a negative size since that corresponds to our back-annotation. */
7703 if (UI_Lt (uint_size, Uint_0))
7704 return NULL_TREE;
7706 /* Find the node to use for errors. */
7707 if ((Ekind (gnat_object) == E_Component
7708 || Ekind (gnat_object) == E_Discriminant)
7709 && Present (Component_Clause (gnat_object)))
7710 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7711 else if (Present (Size_Clause (gnat_object)))
7712 gnat_error_node = Expression (Size_Clause (gnat_object));
7713 else
7714 gnat_error_node = gnat_object;
7716 /* Get the size as a tree. Issue an error if a size was specified but
7717 cannot be represented in sizetype. */
7718 size = UI_To_gnu (uint_size, bitsizetype);
7719 if (TREE_OVERFLOW (size))
7721 if (component_p)
7722 post_error_ne ("component size of & is too large", gnat_error_node,
7723 gnat_object);
7724 else
7725 post_error_ne ("size of & is too large", gnat_error_node,
7726 gnat_object);
7727 return NULL_TREE;
7730 /* Ignore a zero size if it is not permitted. */
7731 if (!zero_ok && integer_zerop (size))
7732 return NULL_TREE;
7734 /* The size of objects is always a multiple of a byte. */
7735 if (kind == VAR_DECL
7736 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7738 if (component_p)
7739 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7740 gnat_error_node, gnat_object);
7741 else
7742 post_error_ne ("size for& is not a multiple of Storage_Unit",
7743 gnat_error_node, gnat_object);
7744 return NULL_TREE;
7747 /* If this is an integral type or a packed array type, the front-end has
7748 verified the size, so we need not do it here (which would entail
7749 checking against the bounds). However, if this is an aliased object,
7750 it may not be smaller than the type of the object. */
7751 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7752 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7753 return size;
7755 /* If the object is a record that contains a template, add the size of
7756 the template to the specified size. */
7757 if (TREE_CODE (gnu_type) == RECORD_TYPE
7758 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7759 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7761 if (kind == VAR_DECL
7762 /* If a type needs strict alignment, a component of this type in
7763 a packed record cannot be packed and thus uses the type size. */
7764 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7765 type_size = TYPE_SIZE (gnu_type);
7766 else
7767 type_size = rm_size (gnu_type);
7769 /* Modify the size of the type to be that of the maximum size if it has a
7770 discriminant. */
7771 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7772 type_size = max_size (type_size, true);
7774 /* If this is an access type or a fat pointer, the minimum size is that given
7775 by the smallest integral mode that's valid for pointers. */
7776 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7778 enum machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7779 while (!targetm.valid_pointer_mode (p_mode))
7780 p_mode = GET_MODE_WIDER_MODE (p_mode);
7781 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7784 /* If the size of the object is a constant, the new size must not be
7785 smaller. */
7786 if (TREE_CODE (type_size) != INTEGER_CST
7787 || TREE_OVERFLOW (type_size)
7788 || tree_int_cst_lt (size, type_size))
7790 if (component_p)
7791 post_error_ne_tree
7792 ("component size for& too small{, minimum allowed is ^}",
7793 gnat_error_node, gnat_object, type_size);
7794 else
7795 post_error_ne_tree
7796 ("size for& too small{, minimum allowed is ^}",
7797 gnat_error_node, gnat_object, type_size);
7799 size = NULL_TREE;
7802 return size;
7805 /* Similarly, but both validate and process a value of RM size. This
7806 routine is only called for types. */
7808 static void
7809 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7811 Node_Id gnat_attr_node;
7812 tree old_size, size;
7814 /* Do nothing if no size was specified. */
7815 if (uint_size == No_Uint)
7816 return;
7818 /* Ignore a negative size since that corresponds to our back-annotation. */
7819 if (UI_Lt (uint_size, Uint_0))
7820 return;
7822 /* Only issue an error if a Value_Size clause was explicitly given.
7823 Otherwise, we'd be duplicating an error on the Size clause. */
7824 gnat_attr_node
7825 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
7827 /* Get the size as a tree. Issue an error if a size was specified but
7828 cannot be represented in sizetype. */
7829 size = UI_To_gnu (uint_size, bitsizetype);
7830 if (TREE_OVERFLOW (size))
7832 if (Present (gnat_attr_node))
7833 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
7834 gnat_entity);
7835 return;
7838 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
7839 exists, or this is an integer type, in which case the front-end will
7840 have always set it. */
7841 if (No (gnat_attr_node)
7842 && integer_zerop (size)
7843 && !Has_Size_Clause (gnat_entity)
7844 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7845 return;
7847 old_size = rm_size (gnu_type);
7849 /* If the old size is self-referential, get the maximum size. */
7850 if (CONTAINS_PLACEHOLDER_P (old_size))
7851 old_size = max_size (old_size, true);
7853 /* If the size of the object is a constant, the new size must not be smaller
7854 (the front-end has verified this for scalar and packed array types). */
7855 if (TREE_CODE (old_size) != INTEGER_CST
7856 || TREE_OVERFLOW (old_size)
7857 || (AGGREGATE_TYPE_P (gnu_type)
7858 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
7859 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
7860 && !(TYPE_IS_PADDING_P (gnu_type)
7861 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
7862 && TYPE_PACKED_ARRAY_TYPE_P
7863 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
7864 && tree_int_cst_lt (size, old_size)))
7866 if (Present (gnat_attr_node))
7867 post_error_ne_tree
7868 ("Value_Size for& too small{, minimum allowed is ^}",
7869 gnat_attr_node, gnat_entity, old_size);
7870 return;
7873 /* Otherwise, set the RM size proper for integral types... */
7874 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
7875 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7876 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
7877 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
7878 SET_TYPE_RM_SIZE (gnu_type, size);
7880 /* ...or the Ada size for record and union types. */
7881 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
7882 || TREE_CODE (gnu_type) == UNION_TYPE
7883 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
7884 && !TYPE_FAT_POINTER_P (gnu_type))
7885 SET_TYPE_ADA_SIZE (gnu_type, size);
7888 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
7889 If TYPE is the best type, return it. Otherwise, make a new type. We
7890 only support new integral and pointer types. FOR_BIASED is true if
7891 we are making a biased type. */
7893 static tree
7894 make_type_from_size (tree type, tree size_tree, bool for_biased)
7896 unsigned HOST_WIDE_INT size;
7897 bool biased_p;
7898 tree new_type;
7900 /* If size indicates an error, just return TYPE to avoid propagating
7901 the error. Likewise if it's too large to represent. */
7902 if (!size_tree || !host_integerp (size_tree, 1))
7903 return type;
7905 size = tree_low_cst (size_tree, 1);
7907 switch (TREE_CODE (type))
7909 case INTEGER_TYPE:
7910 case ENUMERAL_TYPE:
7911 case BOOLEAN_TYPE:
7912 biased_p = (TREE_CODE (type) == INTEGER_TYPE
7913 && TYPE_BIASED_REPRESENTATION_P (type));
7915 /* Integer types with precision 0 are forbidden. */
7916 if (size == 0)
7917 size = 1;
7919 /* Only do something if the type is not a packed array type and
7920 doesn't already have the proper size. */
7921 if (TYPE_PACKED_ARRAY_TYPE_P (type)
7922 || (TYPE_PRECISION (type) == size && biased_p == for_biased))
7923 break;
7925 biased_p |= for_biased;
7926 if (size > LONG_LONG_TYPE_SIZE)
7927 size = LONG_LONG_TYPE_SIZE;
7929 if (TYPE_UNSIGNED (type) || biased_p)
7930 new_type = make_unsigned_type (size);
7931 else
7932 new_type = make_signed_type (size);
7933 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
7934 SET_TYPE_RM_MIN_VALUE (new_type,
7935 convert (TREE_TYPE (new_type),
7936 TYPE_MIN_VALUE (type)));
7937 SET_TYPE_RM_MAX_VALUE (new_type,
7938 convert (TREE_TYPE (new_type),
7939 TYPE_MAX_VALUE (type)));
7940 /* Copy the name to show that it's essentially the same type and
7941 not a subrange type. */
7942 TYPE_NAME (new_type) = TYPE_NAME (type);
7943 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
7944 SET_TYPE_RM_SIZE (new_type, bitsize_int (size));
7945 return new_type;
7947 case RECORD_TYPE:
7948 /* Do something if this is a fat pointer, in which case we
7949 may need to return the thin pointer. */
7950 if (TYPE_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
7952 enum machine_mode p_mode = mode_for_size (size, MODE_INT, 0);
7953 if (!targetm.valid_pointer_mode (p_mode))
7954 p_mode = ptr_mode;
7955 return
7956 build_pointer_type_for_mode
7957 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)),
7958 p_mode, 0);
7960 break;
7962 case POINTER_TYPE:
7963 /* Only do something if this is a thin pointer, in which case we
7964 may need to return the fat pointer. */
7965 if (TYPE_IS_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
7966 return
7967 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
7968 break;
7970 default:
7971 break;
7974 return type;
7977 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
7978 a type or object whose present alignment is ALIGN. If this alignment is
7979 valid, return it. Otherwise, give an error and return ALIGN. */
7981 static unsigned int
7982 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
7984 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
7985 unsigned int new_align;
7986 Node_Id gnat_error_node;
7988 /* Don't worry about checking alignment if alignment was not specified
7989 by the source program and we already posted an error for this entity. */
7990 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
7991 return align;
7993 /* Post the error on the alignment clause if any. Note, for the implicit
7994 base type of an array type, the alignment clause is on the first
7995 subtype. */
7996 if (Present (Alignment_Clause (gnat_entity)))
7997 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
7999 else if (Is_Itype (gnat_entity)
8000 && Is_Array_Type (gnat_entity)
8001 && Etype (gnat_entity) == gnat_entity
8002 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
8003 gnat_error_node =
8004 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
8006 else
8007 gnat_error_node = gnat_entity;
8009 /* Within GCC, an alignment is an integer, so we must make sure a value is
8010 specified that fits in that range. Also, there is an upper bound to
8011 alignments we can support/allow. */
8012 if (!UI_Is_In_Int_Range (alignment)
8013 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
8014 post_error_ne_num ("largest supported alignment for& is ^",
8015 gnat_error_node, gnat_entity, max_allowed_alignment);
8016 else if (!(Present (Alignment_Clause (gnat_entity))
8017 && From_At_Mod (Alignment_Clause (gnat_entity)))
8018 && new_align * BITS_PER_UNIT < align)
8020 unsigned int double_align;
8021 bool is_capped_double, align_clause;
8023 /* If the default alignment of "double" or larger scalar types is
8024 specifically capped and the new alignment is above the cap, do
8025 not post an error and change the alignment only if there is an
8026 alignment clause; this makes it possible to have the associated
8027 GCC type overaligned by default for performance reasons. */
8028 if ((double_align = double_float_alignment) > 0)
8030 Entity_Id gnat_type
8031 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8032 is_capped_double
8033 = is_double_float_or_array (gnat_type, &align_clause);
8035 else if ((double_align = double_scalar_alignment) > 0)
8037 Entity_Id gnat_type
8038 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
8039 is_capped_double
8040 = is_double_scalar_or_array (gnat_type, &align_clause);
8042 else
8043 is_capped_double = align_clause = false;
8045 if (is_capped_double && new_align >= double_align)
8047 if (align_clause)
8048 align = new_align * BITS_PER_UNIT;
8050 else
8052 if (is_capped_double)
8053 align = double_align * BITS_PER_UNIT;
8055 post_error_ne_num ("alignment for& must be at least ^",
8056 gnat_error_node, gnat_entity,
8057 align / BITS_PER_UNIT);
8060 else
8062 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
8063 if (new_align > align)
8064 align = new_align;
8067 return align;
8070 /* Return the smallest alignment not less than SIZE. */
8072 static unsigned int
8073 ceil_alignment (unsigned HOST_WIDE_INT size)
8075 return (unsigned int) 1 << (floor_log2 (size - 1) + 1);
8078 /* Verify that OBJECT, a type or decl, is something we can implement
8079 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
8080 if we require atomic components. */
8082 static void
8083 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
8085 Node_Id gnat_error_point = gnat_entity;
8086 Node_Id gnat_node;
8087 enum machine_mode mode;
8088 unsigned int align;
8089 tree size;
8091 /* There are three case of what OBJECT can be. It can be a type, in which
8092 case we take the size, alignment and mode from the type. It can be a
8093 declaration that was indirect, in which case the relevant values are
8094 that of the type being pointed to, or it can be a normal declaration,
8095 in which case the values are of the decl. The code below assumes that
8096 OBJECT is either a type or a decl. */
8097 if (TYPE_P (object))
8099 /* If this is an anonymous base type, nothing to check. Error will be
8100 reported on the source type. */
8101 if (!Comes_From_Source (gnat_entity))
8102 return;
8104 mode = TYPE_MODE (object);
8105 align = TYPE_ALIGN (object);
8106 size = TYPE_SIZE (object);
8108 else if (DECL_BY_REF_P (object))
8110 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
8111 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
8112 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
8114 else
8116 mode = DECL_MODE (object);
8117 align = DECL_ALIGN (object);
8118 size = DECL_SIZE (object);
8121 /* Consider all floating-point types atomic and any types that that are
8122 represented by integers no wider than a machine word. */
8123 if (GET_MODE_CLASS (mode) == MODE_FLOAT
8124 || ((GET_MODE_CLASS (mode) == MODE_INT
8125 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
8126 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8127 return;
8129 /* For the moment, also allow anything that has an alignment equal
8130 to its size and which is smaller than a word. */
8131 if (size && TREE_CODE (size) == INTEGER_CST
8132 && compare_tree_int (size, align) == 0
8133 && align <= BITS_PER_WORD)
8134 return;
8136 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
8137 gnat_node = Next_Rep_Item (gnat_node))
8139 if (!comp_p && Nkind (gnat_node) == N_Pragma
8140 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8141 == Pragma_Atomic))
8142 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8143 else if (comp_p && Nkind (gnat_node) == N_Pragma
8144 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8145 == Pragma_Atomic_Components))
8146 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8149 if (comp_p)
8150 post_error_ne ("atomic access to component of & cannot be guaranteed",
8151 gnat_error_point, gnat_entity);
8152 else
8153 post_error_ne ("atomic access to & cannot be guaranteed",
8154 gnat_error_point, gnat_entity);
8158 /* Helper for the intrin compatibility checks family. Evaluate whether
8159 two types are definitely incompatible. */
8161 static bool
8162 intrin_types_incompatible_p (tree t1, tree t2)
8164 enum tree_code code;
8166 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8167 return false;
8169 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8170 return true;
8172 if (TREE_CODE (t1) != TREE_CODE (t2))
8173 return true;
8175 code = TREE_CODE (t1);
8177 switch (code)
8179 case INTEGER_TYPE:
8180 case REAL_TYPE:
8181 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8183 case POINTER_TYPE:
8184 case REFERENCE_TYPE:
8185 /* Assume designated types are ok. We'd need to account for char * and
8186 void * variants to do better, which could rapidly get messy and isn't
8187 clearly worth the effort. */
8188 return false;
8190 default:
8191 break;
8194 return false;
8197 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8198 on the Ada/builtin argument lists for the INB binding. */
8200 static bool
8201 intrin_arglists_compatible_p (intrin_binding_t * inb)
8203 tree ada_args = TYPE_ARG_TYPES (inb->ada_fntype);
8204 tree btin_args = TYPE_ARG_TYPES (inb->btin_fntype);
8206 /* Sequence position of the last argument we checked. */
8207 int argpos = 0;
8209 while (ada_args != 0 || btin_args != 0)
8211 tree ada_type, btin_type;
8213 /* If one list is shorter than the other, they fail to match. */
8214 if (ada_args == 0 || btin_args == 0)
8215 return false;
8217 ada_type = TREE_VALUE (ada_args);
8218 btin_type = TREE_VALUE (btin_args);
8220 /* If we're done with the Ada args and not with the internal builtin
8221 args, or the other way around, complain. */
8222 if (ada_type == void_type_node
8223 && btin_type != void_type_node)
8225 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8226 return false;
8229 if (btin_type == void_type_node
8230 && ada_type != void_type_node)
8232 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8233 inb->gnat_entity, inb->gnat_entity, argpos);
8234 return false;
8237 /* Otherwise, check that types match for the current argument. */
8238 argpos ++;
8239 if (intrin_types_incompatible_p (ada_type, btin_type))
8241 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8242 inb->gnat_entity, inb->gnat_entity, argpos);
8243 return false;
8246 ada_args = TREE_CHAIN (ada_args);
8247 btin_args = TREE_CHAIN (btin_args);
8250 return true;
8253 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8254 on the Ada/builtin return values for the INB binding. */
8256 static bool
8257 intrin_return_compatible_p (intrin_binding_t * inb)
8259 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8260 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8262 /* Accept function imported as procedure, common and convenient. */
8263 if (VOID_TYPE_P (ada_return_type)
8264 && !VOID_TYPE_P (btin_return_type))
8265 return true;
8267 /* Check return types compatibility otherwise. Note that this
8268 handles void/void as well. */
8269 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8271 post_error ("?intrinsic binding type mismatch on return value!",
8272 inb->gnat_entity);
8273 return false;
8276 return true;
8279 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8280 compatible. Issue relevant warnings when they are not.
8282 This is intended as a light check to diagnose the most obvious cases, not
8283 as a full fledged type compatiblity predicate. It is the programmer's
8284 responsibility to ensure correctness of the Ada declarations in Imports,
8285 especially when binding straight to a compiler internal. */
8287 static bool
8288 intrin_profiles_compatible_p (intrin_binding_t * inb)
8290 /* Check compatibility on return values and argument lists, each responsible
8291 for posting warnings as appropriate. Ensure use of the proper sloc for
8292 this purpose. */
8294 bool arglists_compatible_p, return_compatible_p;
8295 location_t saved_location = input_location;
8297 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8299 return_compatible_p = intrin_return_compatible_p (inb);
8300 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8302 input_location = saved_location;
8304 return return_compatible_p && arglists_compatible_p;
8307 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8308 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8309 specified size for this field. POS_LIST is a position list describing
8310 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8311 to this layout. */
8313 static tree
8314 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8315 tree size, tree pos_list,
8316 VEC(subst_pair,heap) *subst_list)
8318 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8319 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8320 unsigned int offset_align = tree_low_cst (TREE_VEC_ELT (t, 1), 1);
8321 tree new_pos, new_field;
8322 unsigned ix;
8323 subst_pair *s;
8325 if (CONTAINS_PLACEHOLDER_P (pos))
8326 FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
8327 pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8329 /* If the position is now a constant, we can set it as the position of the
8330 field when we make it. Otherwise, we need to deal with it specially. */
8331 if (TREE_CONSTANT (pos))
8332 new_pos = bit_from_pos (pos, bitpos);
8333 else
8334 new_pos = NULL_TREE;
8336 new_field
8337 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8338 size, new_pos, DECL_PACKED (old_field),
8339 !DECL_NONADDRESSABLE_P (old_field));
8341 if (!new_pos)
8343 normalize_offset (&pos, &bitpos, offset_align);
8344 DECL_FIELD_OFFSET (new_field) = pos;
8345 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8346 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8347 DECL_SIZE (new_field) = size;
8348 DECL_SIZE_UNIT (new_field)
8349 = convert (sizetype,
8350 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8351 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8354 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8355 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8356 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8357 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8359 return new_field;
8362 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8364 static tree
8365 get_rep_part (tree record_type)
8367 tree field = TYPE_FIELDS (record_type);
8369 /* The REP part is the first field, internal, another record, and its name
8370 doesn't start with an underscore (i.e. is not generated by the FE). */
8371 if (DECL_INTERNAL_P (field)
8372 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8373 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] != '_')
8374 return field;
8376 return NULL_TREE;
8379 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8381 static tree
8382 get_variant_part (tree record_type)
8384 tree field;
8386 /* The variant part is the only internal field that is a qualified union. */
8387 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8388 if (DECL_INTERNAL_P (field)
8389 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8390 return field;
8392 return NULL_TREE;
8395 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8396 the list of variants to be used and RECORD_TYPE is the type of the parent.
8397 POS_LIST is a position list describing the layout of fields present in
8398 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8399 layout. */
8401 static tree
8402 create_variant_part_from (tree old_variant_part,
8403 VEC(variant_desc,heap) *variant_list,
8404 tree record_type, tree pos_list,
8405 VEC(subst_pair,heap) *subst_list)
8407 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8408 tree old_union_type = TREE_TYPE (old_variant_part);
8409 tree new_union_type, new_variant_part;
8410 tree union_field_list = NULL_TREE;
8411 variant_desc *v;
8412 unsigned ix;
8414 /* First create the type of the variant part from that of the old one. */
8415 new_union_type = make_node (QUAL_UNION_TYPE);
8416 TYPE_NAME (new_union_type) = DECL_NAME (TYPE_NAME (old_union_type));
8418 /* If the position of the variant part is constant, subtract it from the
8419 size of the type of the parent to get the new size. This manual CSE
8420 reduces the code size when not optimizing. */
8421 if (TREE_CODE (offset) == INTEGER_CST)
8423 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8424 tree first_bit = bit_from_pos (offset, bitpos);
8425 TYPE_SIZE (new_union_type)
8426 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8427 TYPE_SIZE_UNIT (new_union_type)
8428 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8429 byte_from_pos (offset, bitpos));
8430 SET_TYPE_ADA_SIZE (new_union_type,
8431 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8432 first_bit));
8433 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8434 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8436 else
8437 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8439 /* Now finish up the new variants and populate the union type. */
8440 FOR_EACH_VEC_ELT_REVERSE (variant_desc, variant_list, ix, v)
8442 tree old_field = v->field, new_field;
8443 tree old_variant, old_variant_subpart, new_variant, field_list;
8445 /* Skip variants that don't belong to this nesting level. */
8446 if (DECL_CONTEXT (old_field) != old_union_type)
8447 continue;
8449 /* Retrieve the list of fields already added to the new variant. */
8450 new_variant = v->record;
8451 field_list = TYPE_FIELDS (new_variant);
8453 /* If the old variant had a variant subpart, we need to create a new
8454 variant subpart and add it to the field list. */
8455 old_variant = v->type;
8456 old_variant_subpart = get_variant_part (old_variant);
8457 if (old_variant_subpart)
8459 tree new_variant_subpart
8460 = create_variant_part_from (old_variant_subpart, variant_list,
8461 new_variant, pos_list, subst_list);
8462 DECL_CHAIN (new_variant_subpart) = field_list;
8463 field_list = new_variant_subpart;
8466 /* Finish up the new variant and create the field. No need for debug
8467 info thanks to the XVS type. */
8468 finish_record_type (new_variant, nreverse (field_list), 2, false);
8469 compute_record_mode (new_variant);
8470 create_type_decl (TYPE_NAME (new_variant), new_variant, NULL,
8471 true, false, Empty);
8473 new_field
8474 = create_field_decl_from (old_field, new_variant, new_union_type,
8475 TYPE_SIZE (new_variant),
8476 pos_list, subst_list);
8477 DECL_QUALIFIER (new_field) = v->qual;
8478 DECL_INTERNAL_P (new_field) = 1;
8479 DECL_CHAIN (new_field) = union_field_list;
8480 union_field_list = new_field;
8483 /* Finish up the union type and create the variant part. No need for debug
8484 info thanks to the XVS type. */
8485 finish_record_type (new_union_type, union_field_list, 2, false);
8486 compute_record_mode (new_union_type);
8487 create_type_decl (TYPE_NAME (new_union_type), new_union_type, NULL,
8488 true, false, Empty);
8490 new_variant_part
8491 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8492 TYPE_SIZE (new_union_type),
8493 pos_list, subst_list);
8494 DECL_INTERNAL_P (new_variant_part) = 1;
8496 /* With multiple discriminants it is possible for an inner variant to be
8497 statically selected while outer ones are not; in this case, the list
8498 of fields of the inner variant is not flattened and we end up with a
8499 qualified union with a single member. Drop the useless container. */
8500 if (!DECL_CHAIN (union_field_list))
8502 DECL_CONTEXT (union_field_list) = record_type;
8503 DECL_FIELD_OFFSET (union_field_list)
8504 = DECL_FIELD_OFFSET (new_variant_part);
8505 DECL_FIELD_BIT_OFFSET (union_field_list)
8506 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8507 SET_DECL_OFFSET_ALIGN (union_field_list,
8508 DECL_OFFSET_ALIGN (new_variant_part));
8509 new_variant_part = union_field_list;
8512 return new_variant_part;
8515 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8516 which are both RECORD_TYPE, after applying the substitutions described
8517 in SUBST_LIST. */
8519 static void
8520 copy_and_substitute_in_size (tree new_type, tree old_type,
8521 VEC(subst_pair,heap) *subst_list)
8523 unsigned ix;
8524 subst_pair *s;
8526 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8527 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8528 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8529 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8530 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8532 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8533 FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
8534 TYPE_SIZE (new_type)
8535 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8536 s->discriminant, s->replacement);
8538 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8539 FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
8540 TYPE_SIZE_UNIT (new_type)
8541 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8542 s->discriminant, s->replacement);
8544 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8545 FOR_EACH_VEC_ELT_REVERSE (subst_pair, subst_list, ix, s)
8546 SET_TYPE_ADA_SIZE
8547 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8548 s->discriminant, s->replacement));
8550 /* Finalize the size. */
8551 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8552 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8555 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8556 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8557 updated by replacing F with R.
8559 The function doesn't update the layout of the type, i.e. it assumes
8560 that the substitution is purely formal. That's why the replacement
8561 value R must itself contain a PLACEHOLDER_EXPR. */
8563 tree
8564 substitute_in_type (tree t, tree f, tree r)
8566 tree nt;
8568 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8570 switch (TREE_CODE (t))
8572 case INTEGER_TYPE:
8573 case ENUMERAL_TYPE:
8574 case BOOLEAN_TYPE:
8575 case REAL_TYPE:
8577 /* First the domain types of arrays. */
8578 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8579 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8581 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8582 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8584 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8585 return t;
8587 nt = copy_type (t);
8588 TYPE_GCC_MIN_VALUE (nt) = low;
8589 TYPE_GCC_MAX_VALUE (nt) = high;
8591 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8592 SET_TYPE_INDEX_TYPE
8593 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8595 return nt;
8598 /* Then the subtypes. */
8599 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8600 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8602 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8603 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8605 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8606 return t;
8608 nt = copy_type (t);
8609 SET_TYPE_RM_MIN_VALUE (nt, low);
8610 SET_TYPE_RM_MAX_VALUE (nt, high);
8612 return nt;
8615 return t;
8617 case COMPLEX_TYPE:
8618 nt = substitute_in_type (TREE_TYPE (t), f, r);
8619 if (nt == TREE_TYPE (t))
8620 return t;
8622 return build_complex_type (nt);
8624 case OFFSET_TYPE:
8625 case METHOD_TYPE:
8626 case FUNCTION_TYPE:
8627 case LANG_TYPE:
8628 /* These should never show up here. */
8629 gcc_unreachable ();
8631 case ARRAY_TYPE:
8633 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8634 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8636 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8637 return t;
8639 nt = build_nonshared_array_type (component, domain);
8640 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8641 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8642 SET_TYPE_MODE (nt, TYPE_MODE (t));
8643 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8644 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8645 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8646 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8647 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8648 return nt;
8651 case RECORD_TYPE:
8652 case UNION_TYPE:
8653 case QUAL_UNION_TYPE:
8655 bool changed_field = false;
8656 tree field;
8658 /* Start out with no fields, make new fields, and chain them
8659 in. If we haven't actually changed the type of any field,
8660 discard everything we've done and return the old type. */
8661 nt = copy_type (t);
8662 TYPE_FIELDS (nt) = NULL_TREE;
8664 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8666 tree new_field = copy_node (field), new_n;
8668 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8669 if (new_n != TREE_TYPE (field))
8671 TREE_TYPE (new_field) = new_n;
8672 changed_field = true;
8675 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8676 if (new_n != DECL_FIELD_OFFSET (field))
8678 DECL_FIELD_OFFSET (new_field) = new_n;
8679 changed_field = true;
8682 /* Do the substitution inside the qualifier, if any. */
8683 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8685 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8686 if (new_n != DECL_QUALIFIER (field))
8688 DECL_QUALIFIER (new_field) = new_n;
8689 changed_field = true;
8693 DECL_CONTEXT (new_field) = nt;
8694 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8696 DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8697 TYPE_FIELDS (nt) = new_field;
8700 if (!changed_field)
8701 return t;
8703 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8704 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8705 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8706 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8707 return nt;
8710 default:
8711 return t;
8715 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8716 needed to represent the object. */
8718 tree
8719 rm_size (tree gnu_type)
8721 /* For integral types, we store the RM size explicitly. */
8722 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8723 return TYPE_RM_SIZE (gnu_type);
8725 /* Return the RM size of the actual data plus the size of the template. */
8726 if (TREE_CODE (gnu_type) == RECORD_TYPE
8727 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8728 return
8729 size_binop (PLUS_EXPR,
8730 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8731 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8733 /* For record types, we store the size explicitly. */
8734 if ((TREE_CODE (gnu_type) == RECORD_TYPE
8735 || TREE_CODE (gnu_type) == UNION_TYPE
8736 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
8737 && !TYPE_FAT_POINTER_P (gnu_type)
8738 && TYPE_ADA_SIZE (gnu_type))
8739 return TYPE_ADA_SIZE (gnu_type);
8741 /* For other types, this is just the size. */
8742 return TYPE_SIZE (gnu_type);
8745 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8746 fully-qualified name, possibly with type information encoding.
8747 Otherwise, return the name. */
8749 tree
8750 get_entity_name (Entity_Id gnat_entity)
8752 Get_Encoded_Name (gnat_entity);
8753 return get_identifier_with_length (Name_Buffer, Name_Len);
8756 /* Return an identifier representing the external name to be used for
8757 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8758 and the specified suffix. */
8760 tree
8761 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8763 Entity_Kind kind = Ekind (gnat_entity);
8765 if (suffix)
8767 String_Template temp = {1, strlen (suffix)};
8768 Fat_Pointer fp = {suffix, &temp};
8769 Get_External_Name_With_Suffix (gnat_entity, fp);
8771 else
8772 Get_External_Name (gnat_entity, 0);
8774 /* A variable using the Stdcall convention lives in a DLL. We adjust
8775 its name to use the jump table, the _imp__NAME contains the address
8776 for the NAME variable. */
8777 if ((kind == E_Variable || kind == E_Constant)
8778 && Has_Stdcall_Convention (gnat_entity))
8780 const int len = 6 + Name_Len;
8781 char *new_name = (char *) alloca (len + 1);
8782 strcpy (new_name, "_imp__");
8783 strcat (new_name, Name_Buffer);
8784 return get_identifier_with_length (new_name, len);
8787 return get_identifier_with_length (Name_Buffer, Name_Len);
8790 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8791 string, return a new IDENTIFIER_NODE that is the concatenation of
8792 the name followed by "___" and the specified suffix. */
8794 tree
8795 concat_name (tree gnu_name, const char *suffix)
8797 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8798 char *new_name = (char *) alloca (len + 1);
8799 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8800 strcat (new_name, "___");
8801 strcat (new_name, suffix);
8802 return get_identifier_with_length (new_name, len);
8805 #include "gt-ada-decl.h"