Merged trunk at revision 161680 into branch.
[official-gcc.git] / gcc / ada / gcc-interface / decl.c
blob6952060259d0bc20893d2401353cc46dcf57b5ee
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 /* FIXME: Still need to include rtl.h here (via expr.h) because this file
27 actually generates RTL (search for gen_rtx_* in gnat_to_gnu_entity). */
28 #undef IN_GCC_FRONTEND
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "tree.h"
35 #include "flags.h"
36 #include "toplev.h"
37 #include "ggc.h"
38 #include "target.h"
39 #include "expr.h"
40 #include "tree-inline.h"
42 #include "ada.h"
43 #include "types.h"
44 #include "atree.h"
45 #include "elists.h"
46 #include "namet.h"
47 #include "nlists.h"
48 #include "repinfo.h"
49 #include "snames.h"
50 #include "stringt.h"
51 #include "uintp.h"
52 #include "fe.h"
53 #include "sinfo.h"
54 #include "einfo.h"
55 #include "ada-tree.h"
56 #include "gigi.h"
58 /* Convention_Stdcall should be processed in a specific way on Windows targets
59 only. The macro below is a helper to avoid having to check for a Windows
60 specific attribute throughout this unit. */
62 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
63 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
64 #else
65 #define Has_Stdcall_Convention(E) (0)
66 #endif
68 /* Stack realignment for functions with foreign conventions is provided on a
69 per back-end basis now, as it is handled by the prologue expanders and not
70 as part of the function's body any more. It might be requested by way of a
71 dedicated function type attribute on the targets that support it.
73 We need a way to avoid setting the attribute on the targets that don't
74 support it and use FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN for this purpose.
76 It is defined on targets where the circuitry is available, and indicates
77 whether the realignment is needed for 'main'. We use this to decide for
78 foreign subprograms as well.
80 It is not defined on targets where the circuitry is not implemented, and
81 we just never set the attribute in these cases.
83 Whether it is defined on all targets that would need it in theory is
84 not entirely clear. We currently trust the base GCC settings for this
85 purpose. */
87 #ifndef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
88 #define FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN 0
89 #endif
91 struct incomplete
93 struct incomplete *next;
94 tree old_type;
95 Entity_Id full_type;
98 /* These variables are used to defer recursively expanding incomplete types
99 while we are processing an array, a record or a subprogram type. */
100 static int defer_incomplete_level = 0;
101 static struct incomplete *defer_incomplete_list;
103 /* This variable is used to delay expanding From_With_Type types until the
104 end of the spec. */
105 static struct incomplete *defer_limited_with;
107 /* These variables are used to defer finalizing types. The element of the
108 list is the TYPE_DECL associated with the type. */
109 static int defer_finalize_level = 0;
110 static VEC (tree,heap) *defer_finalize_list;
112 /* A hash table used to cache the result of annotate_value. */
113 static GTY ((if_marked ("tree_int_map_marked_p"),
114 param_is (struct tree_int_map))) htab_t annotate_value_cache;
116 enum alias_set_op
118 ALIAS_SET_COPY,
119 ALIAS_SET_SUBSET,
120 ALIAS_SET_SUPERSET
123 static void relate_alias_sets (tree, tree, enum alias_set_op);
125 static bool allocatable_size_p (tree, bool);
126 static void prepend_one_attribute_to (struct attrib **,
127 enum attr_type, tree, tree, Node_Id);
128 static void prepend_attributes (Entity_Id, struct attrib **);
129 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
130 static bool is_variable_size (tree);
131 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
132 static tree elaborate_expression_2 (tree, Entity_Id, tree, bool, bool,
133 unsigned int);
134 static tree make_packable_type (tree, bool);
135 static tree gnat_to_gnu_component_type (Entity_Id, bool, bool);
136 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
137 bool *);
138 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool, bool);
139 static bool same_discriminant_p (Entity_Id, Entity_Id);
140 static bool array_type_has_nonaliased_component (tree, Entity_Id);
141 static bool compile_time_known_address_p (Node_Id);
142 static bool cannot_be_superflat_p (Node_Id);
143 static bool constructor_address_p (tree);
144 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
145 bool, bool, bool, bool, bool);
146 static Uint annotate_value (tree);
147 static void annotate_rep (Entity_Id, tree);
148 static tree build_position_list (tree, bool, tree, tree, unsigned int, tree);
149 static tree build_subst_list (Entity_Id, Entity_Id, bool);
150 static tree build_variant_list (tree, tree, tree);
151 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
152 static void set_rm_size (Uint, tree, Entity_Id);
153 static tree make_type_from_size (tree, tree, bool);
154 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
155 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
156 static void check_ok_for_atomic (tree, Entity_Id, bool);
157 static tree create_field_decl_from (tree, tree, tree, tree, tree, tree);
158 static tree get_rep_part (tree);
159 static tree get_variant_part (tree);
160 static tree create_variant_part_from (tree, tree, tree, tree, tree);
161 static void copy_and_substitute_in_size (tree, tree, tree);
162 static void rest_of_type_decl_compilation_no_defer (tree);
164 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
165 to pass around calls performing profile compatibilty checks. */
167 typedef struct {
168 Entity_Id gnat_entity; /* The Ada subprogram entity. */
169 tree ada_fntype; /* The corresponding GCC type node. */
170 tree btin_fntype; /* The GCC builtin function type node. */
171 } intrin_binding_t;
173 static bool intrin_profiles_compatible_p (intrin_binding_t *);
176 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
177 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
178 and associate the ..._DECL node with the input GNAT defining identifier.
180 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
181 initial value (in GCC tree form). This is optional for a variable. For
182 a renamed entity, GNU_EXPR gives the object being renamed.
184 DEFINITION is nonzero if this call is intended for a definition. This is
185 used for separate compilation where it is necessary to know whether an
186 external declaration or a definition must be created if the GCC equivalent
187 was not created previously. The value of 1 is normally used for a nonzero
188 DEFINITION, but a value of 2 is used in special circumstances, defined in
189 the code. */
191 tree
192 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
194 /* Contains the kind of the input GNAT node. */
195 const Entity_Kind kind = Ekind (gnat_entity);
196 /* True if this is a type. */
197 const bool is_type = IN (kind, Type_Kind);
198 /* True if debug info is requested for this entity. */
199 const bool debug_info_p = Needs_Debug_Info (gnat_entity);
200 /* True if this entity is to be considered as imported. */
201 const bool imported_p
202 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
203 /* For a type, contains the equivalent GNAT node to be used in gigi. */
204 Entity_Id gnat_equiv_type = Empty;
205 /* Temporary used to walk the GNAT tree. */
206 Entity_Id gnat_temp;
207 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
208 This node will be associated with the GNAT node by calling at the end
209 of the `switch' statement. */
210 tree gnu_decl = NULL_TREE;
211 /* Contains the GCC type to be used for the GCC node. */
212 tree gnu_type = NULL_TREE;
213 /* Contains the GCC size tree to be used for the GCC node. */
214 tree gnu_size = NULL_TREE;
215 /* Contains the GCC name to be used for the GCC node. */
216 tree gnu_entity_name;
217 /* True if we have already saved gnu_decl as a GNAT association. */
218 bool saved = false;
219 /* True if we incremented defer_incomplete_level. */
220 bool this_deferred = false;
221 /* True if we incremented force_global. */
222 bool this_global = false;
223 /* True if we should check to see if elaborated during processing. */
224 bool maybe_present = false;
225 /* True if we made GNU_DECL and its type here. */
226 bool this_made_decl = false;
227 /* Size and alignment of the GCC node, if meaningful. */
228 unsigned int esize = 0, align = 0;
229 /* Contains the list of attributes directly attached to the entity. */
230 struct attrib *attr_list = NULL;
232 /* Since a use of an Itype is a definition, process it as such if it
233 is not in a with'ed unit. */
234 if (!definition
235 && is_type
236 && Is_Itype (gnat_entity)
237 && !present_gnu_tree (gnat_entity)
238 && In_Extended_Main_Code_Unit (gnat_entity))
240 /* Ensure that we are in a subprogram mentioned in the Scope chain of
241 this entity, our current scope is global, or we encountered a task
242 or entry (where we can't currently accurately check scoping). */
243 if (!current_function_decl
244 || DECL_ELABORATION_PROC_P (current_function_decl))
246 process_type (gnat_entity);
247 return get_gnu_tree (gnat_entity);
250 for (gnat_temp = Scope (gnat_entity);
251 Present (gnat_temp);
252 gnat_temp = Scope (gnat_temp))
254 if (Is_Type (gnat_temp))
255 gnat_temp = Underlying_Type (gnat_temp);
257 if (Ekind (gnat_temp) == E_Subprogram_Body)
258 gnat_temp
259 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
261 if (IN (Ekind (gnat_temp), Subprogram_Kind)
262 && Present (Protected_Body_Subprogram (gnat_temp)))
263 gnat_temp = Protected_Body_Subprogram (gnat_temp);
265 if (Ekind (gnat_temp) == E_Entry
266 || Ekind (gnat_temp) == E_Entry_Family
267 || Ekind (gnat_temp) == E_Task_Type
268 || (IN (Ekind (gnat_temp), Subprogram_Kind)
269 && present_gnu_tree (gnat_temp)
270 && (current_function_decl
271 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
273 process_type (gnat_entity);
274 return get_gnu_tree (gnat_entity);
278 /* This abort means the Itype has an incorrect scope, i.e. that its
279 scope does not correspond to the subprogram it is declared in. */
280 gcc_unreachable ();
283 /* If we've already processed this entity, return what we got last time.
284 If we are defining the node, we should not have already processed it.
285 In that case, we will abort below when we try to save a new GCC tree
286 for this object. We also need to handle the case of getting a dummy
287 type when a Full_View exists. */
288 if ((!definition || (is_type && imported_p))
289 && present_gnu_tree (gnat_entity))
291 gnu_decl = get_gnu_tree (gnat_entity);
293 if (TREE_CODE (gnu_decl) == TYPE_DECL
294 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
295 && IN (kind, Incomplete_Or_Private_Kind)
296 && Present (Full_View (gnat_entity)))
298 gnu_decl
299 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
300 save_gnu_tree (gnat_entity, NULL_TREE, false);
301 save_gnu_tree (gnat_entity, gnu_decl, false);
304 return gnu_decl;
307 /* If this is a numeric or enumeral type, or an access type, a nonzero
308 Esize must be specified unless it was specified by the programmer. */
309 gcc_assert (!Unknown_Esize (gnat_entity)
310 || Has_Size_Clause (gnat_entity)
311 || (!IN (kind, Numeric_Kind)
312 && !IN (kind, Enumeration_Kind)
313 && (!IN (kind, Access_Kind)
314 || kind == E_Access_Protected_Subprogram_Type
315 || kind == E_Anonymous_Access_Protected_Subprogram_Type
316 || kind == E_Access_Subtype)));
318 /* The RM size must be specified for all discrete and fixed-point types. */
319 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
320 && Unknown_RM_Size (gnat_entity)));
322 /* If we get here, it means we have not yet done anything with this entity.
323 If we are not defining it, it must be a type or an entity that is defined
324 elsewhere or externally, otherwise we should have defined it already. */
325 gcc_assert (definition
326 || type_annotate_only
327 || is_type
328 || kind == E_Discriminant
329 || kind == E_Component
330 || kind == E_Label
331 || (kind == E_Constant && Present (Full_View (gnat_entity)))
332 || Is_Public (gnat_entity));
334 /* Get the name of the entity and set up the line number and filename of
335 the original definition for use in any decl we make. */
336 gnu_entity_name = get_entity_name (gnat_entity);
337 Sloc_to_locus (Sloc (gnat_entity), &input_location);
339 /* For cases when we are not defining (i.e., we are referencing from
340 another compilation unit) public entities, show we are at global level
341 for the purpose of computing scopes. Don't do this for components or
342 discriminants since the relevant test is whether or not the record is
343 being defined. */
344 if (!definition
345 && kind != E_Component
346 && kind != E_Discriminant
347 && Is_Public (gnat_entity)
348 && !Is_Statically_Allocated (gnat_entity))
349 force_global++, this_global = true;
351 /* Handle any attributes directly attached to the entity. */
352 if (Has_Gigi_Rep_Item (gnat_entity))
353 prepend_attributes (gnat_entity, &attr_list);
355 /* Do some common processing for types. */
356 if (is_type)
358 /* Compute the equivalent type to be used in gigi. */
359 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
361 /* Machine_Attributes on types are expected to be propagated to
362 subtypes. The corresponding Gigi_Rep_Items are only attached
363 to the first subtype though, so we handle the propagation here. */
364 if (Base_Type (gnat_entity) != gnat_entity
365 && !Is_First_Subtype (gnat_entity)
366 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
367 prepend_attributes (First_Subtype (Base_Type (gnat_entity)),
368 &attr_list);
370 /* Compute a default value for the size of the type. */
371 if (Known_Esize (gnat_entity)
372 && UI_Is_In_Int_Range (Esize (gnat_entity)))
374 unsigned int max_esize;
375 esize = UI_To_Int (Esize (gnat_entity));
377 if (IN (kind, Float_Kind))
378 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
379 else if (IN (kind, Access_Kind))
380 max_esize = POINTER_SIZE * 2;
381 else
382 max_esize = LONG_LONG_TYPE_SIZE;
384 if (esize > max_esize)
385 esize = max_esize;
387 else
388 esize = LONG_LONG_TYPE_SIZE;
391 switch (kind)
393 case E_Constant:
394 /* If this is a use of a deferred constant without address clause,
395 get its full definition. */
396 if (!definition
397 && No (Address_Clause (gnat_entity))
398 && Present (Full_View (gnat_entity)))
400 gnu_decl
401 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
402 saved = true;
403 break;
406 /* If we have an external constant that we are not defining, get the
407 expression that is was defined to represent. We may throw that
408 expression away later if it is not a constant. Do not retrieve the
409 expression if it is an aggregate or allocator, because in complex
410 instantiation contexts it may not be expanded */
411 if (!definition
412 && Present (Expression (Declaration_Node (gnat_entity)))
413 && !No_Initialization (Declaration_Node (gnat_entity))
414 && (Nkind (Expression (Declaration_Node (gnat_entity)))
415 != N_Aggregate)
416 && (Nkind (Expression (Declaration_Node (gnat_entity)))
417 != N_Allocator))
418 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
420 /* Ignore deferred constant definitions without address clause since
421 they are processed fully in the front-end. If No_Initialization
422 is set, this is not a deferred constant but a constant whose value
423 is built manually. And constants that are renamings are handled
424 like variables. */
425 if (definition
426 && !gnu_expr
427 && No (Address_Clause (gnat_entity))
428 && !No_Initialization (Declaration_Node (gnat_entity))
429 && No (Renamed_Object (gnat_entity)))
431 gnu_decl = error_mark_node;
432 saved = true;
433 break;
436 /* Ignore constant definitions already marked with the error node. See
437 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
438 if (definition
439 && gnu_expr
440 && present_gnu_tree (gnat_entity)
441 && get_gnu_tree (gnat_entity) == error_mark_node)
443 maybe_present = true;
444 break;
447 goto object;
449 case E_Exception:
450 /* We used to special case VMS exceptions here to directly map them to
451 their associated condition code. Since this code had to be masked
452 dynamically to strip off the severity bits, this caused trouble in
453 the GCC/ZCX case because the "type" pointers we store in the tables
454 have to be static. We now don't special case here anymore, and let
455 the regular processing take place, which leaves us with a regular
456 exception data object for VMS exceptions too. The condition code
457 mapping is taken care of by the front end and the bitmasking by the
458 run-time library. */
459 goto object;
461 case E_Discriminant:
462 case E_Component:
464 /* The GNAT record where the component was defined. */
465 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
467 /* If the variable is an inherited record component (in the case of
468 extended record types), just return the inherited entity, which
469 must be a FIELD_DECL. Likewise for discriminants.
470 For discriminants of untagged records which have explicit
471 stored discriminants, return the entity for the corresponding
472 stored discriminant. Also use Original_Record_Component
473 if the record has a private extension. */
474 if (Present (Original_Record_Component (gnat_entity))
475 && Original_Record_Component (gnat_entity) != gnat_entity)
477 gnu_decl
478 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
479 gnu_expr, definition);
480 saved = true;
481 break;
484 /* If the enclosing record has explicit stored discriminants,
485 then it is an untagged record. If the Corresponding_Discriminant
486 is not empty then this must be a renamed discriminant and its
487 Original_Record_Component must point to the corresponding explicit
488 stored discriminant (i.e. we should have taken the previous
489 branch). */
490 else if (Present (Corresponding_Discriminant (gnat_entity))
491 && Is_Tagged_Type (gnat_record))
493 /* A tagged record has no explicit stored discriminants. */
494 gcc_assert (First_Discriminant (gnat_record)
495 == First_Stored_Discriminant (gnat_record));
496 gnu_decl
497 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
498 gnu_expr, definition);
499 saved = true;
500 break;
503 else if (Present (CR_Discriminant (gnat_entity))
504 && type_annotate_only)
506 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
507 gnu_expr, definition);
508 saved = true;
509 break;
512 /* If the enclosing record has explicit stored discriminants, then
513 it is an untagged record. If the Corresponding_Discriminant
514 is not empty then this must be a renamed discriminant and its
515 Original_Record_Component must point to the corresponding explicit
516 stored discriminant (i.e. we should have taken the first
517 branch). */
518 else if (Present (Corresponding_Discriminant (gnat_entity))
519 && (First_Discriminant (gnat_record)
520 != First_Stored_Discriminant (gnat_record)))
521 gcc_unreachable ();
523 /* Otherwise, if we are not defining this and we have no GCC type
524 for the containing record, make one for it. Then we should
525 have made our own equivalent. */
526 else if (!definition && !present_gnu_tree (gnat_record))
528 /* ??? If this is in a record whose scope is a protected
529 type and we have an Original_Record_Component, use it.
530 This is a workaround for major problems in protected type
531 handling. */
532 Entity_Id Scop = Scope (Scope (gnat_entity));
533 if ((Is_Protected_Type (Scop)
534 || (Is_Private_Type (Scop)
535 && Present (Full_View (Scop))
536 && Is_Protected_Type (Full_View (Scop))))
537 && Present (Original_Record_Component (gnat_entity)))
539 gnu_decl
540 = gnat_to_gnu_entity (Original_Record_Component
541 (gnat_entity),
542 gnu_expr, 0);
543 saved = true;
544 break;
547 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
548 gnu_decl = get_gnu_tree (gnat_entity);
549 saved = true;
550 break;
553 else
554 /* Here we have no GCC type and this is a reference rather than a
555 definition. This should never happen. Most likely the cause is
556 reference before declaration in the gnat tree for gnat_entity. */
557 gcc_unreachable ();
560 case E_Loop_Parameter:
561 case E_Out_Parameter:
562 case E_Variable:
564 /* Simple variables, loop variables, Out parameters and exceptions. */
565 object:
567 bool const_flag
568 = ((kind == E_Constant || kind == E_Variable)
569 && Is_True_Constant (gnat_entity)
570 && !Treat_As_Volatile (gnat_entity)
571 && (((Nkind (Declaration_Node (gnat_entity))
572 == N_Object_Declaration)
573 && Present (Expression (Declaration_Node (gnat_entity))))
574 || Present (Renamed_Object (gnat_entity))
575 || imported_p));
576 bool inner_const_flag = const_flag;
577 bool static_p = Is_Statically_Allocated (gnat_entity);
578 bool mutable_p = false;
579 bool used_by_ref = false;
580 tree gnu_ext_name = NULL_TREE;
581 tree renamed_obj = NULL_TREE;
582 tree gnu_object_size;
584 if (Present (Renamed_Object (gnat_entity)) && !definition)
586 if (kind == E_Exception)
587 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
588 NULL_TREE, 0);
589 else
590 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
593 /* Get the type after elaborating the renamed object. */
594 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
596 /* If this is a standard exception definition, then use the standard
597 exception type. This is necessary to make sure that imported and
598 exported views of exceptions are properly merged in LTO mode. */
599 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL
600 && DECL_NAME (TYPE_NAME (gnu_type)) == exception_data_name_id)
601 gnu_type = except_type_node;
603 /* For a debug renaming declaration, build a pure debug entity. */
604 if (Present (Debug_Renaming_Link (gnat_entity)))
606 rtx addr;
607 gnu_decl = build_decl (input_location,
608 VAR_DECL, gnu_entity_name, gnu_type);
609 /* The (MEM (CONST (0))) pattern is prescribed by STABS. */
610 if (global_bindings_p ())
611 addr = gen_rtx_CONST (VOIDmode, const0_rtx);
612 else
613 addr = stack_pointer_rtx;
614 SET_DECL_RTL (gnu_decl, gen_rtx_MEM (Pmode, addr));
615 gnat_pushdecl (gnu_decl, gnat_entity);
616 break;
619 /* If this is a loop variable, its type should be the base type.
620 This is because the code for processing a loop determines whether
621 a normal loop end test can be done by comparing the bounds of the
622 loop against those of the base type, which is presumed to be the
623 size used for computation. But this is not correct when the size
624 of the subtype is smaller than the type. */
625 if (kind == E_Loop_Parameter)
626 gnu_type = get_base_type (gnu_type);
628 /* Reject non-renamed objects whose type is an unconstrained array or
629 any object whose type is a dummy type or void. */
630 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
631 && No (Renamed_Object (gnat_entity)))
632 || TYPE_IS_DUMMY_P (gnu_type)
633 || TREE_CODE (gnu_type) == VOID_TYPE)
635 gcc_assert (type_annotate_only);
636 if (this_global)
637 force_global--;
638 return error_mark_node;
641 /* If an alignment is specified, use it if valid. Note that exceptions
642 are objects but don't have an alignment. We must do this before we
643 validate the size, since the alignment can affect the size. */
644 if (kind != E_Exception && Known_Alignment (gnat_entity))
646 gcc_assert (Present (Alignment (gnat_entity)));
647 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
648 TYPE_ALIGN (gnu_type));
650 /* No point in changing the type if there is an address clause
651 as the final type of the object will be a reference type. */
652 if (Present (Address_Clause (gnat_entity)))
653 align = 0;
654 else
655 gnu_type
656 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
657 false, false, definition, true);
660 /* If we are defining the object, see if it has a Size and validate it
661 if so. If we are not defining the object and a Size clause applies,
662 simply retrieve the value. We don't want to ignore the clause and
663 it is expected to have been validated already. Then get the new
664 type, if any. */
665 if (definition)
666 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
667 gnat_entity, VAR_DECL, false,
668 Has_Size_Clause (gnat_entity));
669 else if (Has_Size_Clause (gnat_entity))
670 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
672 if (gnu_size)
674 gnu_type
675 = make_type_from_size (gnu_type, gnu_size,
676 Has_Biased_Representation (gnat_entity));
678 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
679 gnu_size = NULL_TREE;
682 /* If this object has self-referential size, it must be a record with
683 a default discriminant. We are supposed to allocate an object of
684 the maximum size in this case, unless it is a constant with an
685 initializing expression, in which case we can get the size from
686 that. Note that the resulting size may still be a variable, so
687 this may end up with an indirect allocation. */
688 if (No (Renamed_Object (gnat_entity))
689 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
691 if (gnu_expr && kind == E_Constant)
693 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
694 if (CONTAINS_PLACEHOLDER_P (size))
696 /* If the initializing expression is itself a constant,
697 despite having a nominal type with self-referential
698 size, we can get the size directly from it. */
699 if (TREE_CODE (gnu_expr) == COMPONENT_REF
700 && TYPE_IS_PADDING_P
701 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
702 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
703 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
704 || DECL_READONLY_ONCE_ELAB
705 (TREE_OPERAND (gnu_expr, 0))))
706 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
707 else
708 gnu_size
709 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
711 else
712 gnu_size = size;
714 /* We may have no GNU_EXPR because No_Initialization is
715 set even though there's an Expression. */
716 else if (kind == E_Constant
717 && (Nkind (Declaration_Node (gnat_entity))
718 == N_Object_Declaration)
719 && Present (Expression (Declaration_Node (gnat_entity))))
720 gnu_size
721 = TYPE_SIZE (gnat_to_gnu_type
722 (Etype
723 (Expression (Declaration_Node (gnat_entity)))));
724 else
726 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
727 mutable_p = true;
731 /* If the size is zero byte, make it one byte since some linkers have
732 troubles with zero-sized objects. If the object will have a
733 template, that will make it nonzero so don't bother. Also avoid
734 doing that for an object renaming or an object with an address
735 clause, as we would lose useful information on the view size
736 (e.g. for null array slices) and we are not allocating the object
737 here anyway. */
738 if (((gnu_size
739 && integer_zerop (gnu_size)
740 && !TREE_OVERFLOW (gnu_size))
741 || (TYPE_SIZE (gnu_type)
742 && integer_zerop (TYPE_SIZE (gnu_type))
743 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
744 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
745 || !Is_Array_Type (Etype (gnat_entity)))
746 && No (Renamed_Object (gnat_entity))
747 && No (Address_Clause (gnat_entity)))
748 gnu_size = bitsize_unit_node;
750 /* If this is an object with no specified size and alignment, and
751 if either it is atomic or we are not optimizing alignment for
752 space and it is composite and not an exception, an Out parameter
753 or a reference to another object, and the size of its type is a
754 constant, set the alignment to the smallest one which is not
755 smaller than the size, with an appropriate cap. */
756 if (!gnu_size && align == 0
757 && (Is_Atomic (gnat_entity)
758 || (!Optimize_Alignment_Space (gnat_entity)
759 && kind != E_Exception
760 && kind != E_Out_Parameter
761 && Is_Composite_Type (Etype (gnat_entity))
762 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
763 && !Is_Exported (gnat_entity)
764 && !imported_p
765 && No (Renamed_Object (gnat_entity))
766 && No (Address_Clause (gnat_entity))))
767 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
769 /* No point in jumping through all the hoops needed in order
770 to support BIGGEST_ALIGNMENT if we don't really have to.
771 So we cap to the smallest alignment that corresponds to
772 a known efficient memory access pattern of the target. */
773 unsigned int align_cap = Is_Atomic (gnat_entity)
774 ? BIGGEST_ALIGNMENT
775 : get_mode_alignment (ptr_mode);
777 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
778 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
779 align = align_cap;
780 else
781 align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
783 /* But make sure not to under-align the object. */
784 if (align <= TYPE_ALIGN (gnu_type))
785 align = 0;
787 /* And honor the minimum valid atomic alignment, if any. */
788 #ifdef MINIMUM_ATOMIC_ALIGNMENT
789 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
790 align = MINIMUM_ATOMIC_ALIGNMENT;
791 #endif
794 /* If the object is set to have atomic components, find the component
795 type and validate it.
797 ??? Note that we ignore Has_Volatile_Components on objects; it's
798 not at all clear what to do in that case. */
799 if (Has_Atomic_Components (gnat_entity))
801 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
802 ? TREE_TYPE (gnu_type) : gnu_type);
804 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
805 && TYPE_MULTI_ARRAY_P (gnu_inner))
806 gnu_inner = TREE_TYPE (gnu_inner);
808 check_ok_for_atomic (gnu_inner, gnat_entity, true);
811 /* Now check if the type of the object allows atomic access. Note
812 that we must test the type, even if this object has size and
813 alignment to allow such access, because we will be going inside
814 the padded record to assign to the object. We could fix this by
815 always copying via an intermediate value, but it's not clear it's
816 worth the effort. */
817 if (Is_Atomic (gnat_entity))
818 check_ok_for_atomic (gnu_type, gnat_entity, false);
820 /* If this is an aliased object with an unconstrained nominal subtype,
821 make a type that includes the template. */
822 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
823 && Is_Array_Type (Etype (gnat_entity))
824 && !type_annotate_only)
826 tree gnu_fat
827 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
829 gnu_type
830 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
831 concat_name (gnu_entity_name,
832 "UNC"),
833 debug_info_p);
836 #ifdef MINIMUM_ATOMIC_ALIGNMENT
837 /* If the size is a constant and no alignment is specified, force
838 the alignment to be the minimum valid atomic alignment. The
839 restriction on constant size avoids problems with variable-size
840 temporaries; if the size is variable, there's no issue with
841 atomic access. Also don't do this for a constant, since it isn't
842 necessary and can interfere with constant replacement. Finally,
843 do not do it for Out parameters since that creates an
844 size inconsistency with In parameters. */
845 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
846 && !FLOAT_TYPE_P (gnu_type)
847 && !const_flag && No (Renamed_Object (gnat_entity))
848 && !imported_p && No (Address_Clause (gnat_entity))
849 && kind != E_Out_Parameter
850 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
851 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
852 align = MINIMUM_ATOMIC_ALIGNMENT;
853 #endif
855 /* Make a new type with the desired size and alignment, if needed.
856 But do not take into account alignment promotions to compute the
857 size of the object. */
858 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
859 if (gnu_size || align > 0)
860 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
861 false, false, definition,
862 gnu_size ? true : false);
864 /* If this is a renaming, avoid as much as possible to create a new
865 object. However, in several cases, creating it is required.
866 This processing needs to be applied to the raw expression so
867 as to make it more likely to rename the underlying object. */
868 if (Present (Renamed_Object (gnat_entity)))
870 bool create_normal_object = false;
872 /* If the renamed object had padding, strip off the reference
873 to the inner object and reset our type. */
874 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
875 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
876 /* Strip useless conversions around the object. */
877 || (TREE_CODE (gnu_expr) == NOP_EXPR
878 && gnat_types_compatible_p
879 (TREE_TYPE (gnu_expr),
880 TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
882 gnu_expr = TREE_OPERAND (gnu_expr, 0);
883 gnu_type = TREE_TYPE (gnu_expr);
886 /* Case 1: If this is a constant renaming stemming from a function
887 call, treat it as a normal object whose initial value is what
888 is being renamed. RM 3.3 says that the result of evaluating a
889 function call is a constant object. As a consequence, it can
890 be the inner object of a constant renaming. In this case, the
891 renaming must be fully instantiated, i.e. it cannot be a mere
892 reference to (part of) an existing object. */
893 if (const_flag)
895 tree inner_object = gnu_expr;
896 while (handled_component_p (inner_object))
897 inner_object = TREE_OPERAND (inner_object, 0);
898 if (TREE_CODE (inner_object) == CALL_EXPR)
899 create_normal_object = true;
902 /* Otherwise, see if we can proceed with a stabilized version of
903 the renamed entity or if we need to make a new object. */
904 if (!create_normal_object)
906 tree maybe_stable_expr = NULL_TREE;
907 bool stable = false;
909 /* Case 2: If the renaming entity need not be materialized and
910 the renamed expression is something we can stabilize, use
911 that for the renaming. At the global level, we can only do
912 this if we know no SAVE_EXPRs need be made, because the
913 expression we return might be used in arbitrary conditional
914 branches so we must force the SAVE_EXPRs evaluation
915 immediately and this requires a function context. */
916 if (!Materialize_Entity (gnat_entity)
917 && (!global_bindings_p ()
918 || (staticp (gnu_expr)
919 && !TREE_SIDE_EFFECTS (gnu_expr))))
921 maybe_stable_expr
922 = gnat_stabilize_reference (gnu_expr, true, &stable);
924 if (stable)
926 /* ??? No DECL_EXPR is created so we need to mark
927 the expression manually lest it is shared. */
928 if (global_bindings_p ())
929 MARK_VISITED (maybe_stable_expr);
930 gnu_decl = maybe_stable_expr;
931 save_gnu_tree (gnat_entity, gnu_decl, true);
932 saved = true;
933 annotate_object (gnat_entity, gnu_type, NULL_TREE,
934 false);
935 break;
938 /* The stabilization failed. Keep maybe_stable_expr
939 untouched here to let the pointer case below know
940 about that failure. */
943 /* Case 3: If this is a constant renaming and creating a
944 new object is allowed and cheap, treat it as a normal
945 object whose initial value is what is being renamed. */
946 if (const_flag
947 && !Is_Composite_Type
948 (Underlying_Type (Etype (gnat_entity))))
951 /* Case 4: Make this into a constant pointer to the object we
952 are to rename and attach the object to the pointer if it is
953 something we can stabilize.
955 From the proper scope, attached objects will be referenced
956 directly instead of indirectly via the pointer to avoid
957 subtle aliasing problems with non-addressable entities.
958 They have to be stable because we must not evaluate the
959 variables in the expression every time the renaming is used.
960 The pointer is called a "renaming" pointer in this case.
962 In the rare cases where we cannot stabilize the renamed
963 object, we just make a "bare" pointer, and the renamed
964 entity is always accessed indirectly through it. */
965 else
967 gnu_type = build_reference_type (gnu_type);
968 inner_const_flag = TREE_READONLY (gnu_expr);
969 const_flag = true;
971 /* If the previous attempt at stabilizing failed, there
972 is no point in trying again and we reuse the result
973 without attaching it to the pointer. In this case it
974 will only be used as the initializing expression of
975 the pointer and thus needs no special treatment with
976 regard to multiple evaluations. */
977 if (maybe_stable_expr)
980 /* Otherwise, try to stabilize and attach the expression
981 to the pointer if the stabilization succeeds.
983 Note that this might introduce SAVE_EXPRs and we don't
984 check whether we're at the global level or not. This
985 is fine since we are building a pointer initializer and
986 neither the pointer nor the initializing expression can
987 be accessed before the pointer elaboration has taken
988 place in a correct program.
990 These SAVE_EXPRs will be evaluated at the right place
991 by either the evaluation of the initializer for the
992 non-global case or the elaboration code for the global
993 case, and will be attached to the elaboration procedure
994 in the latter case. */
995 else
997 maybe_stable_expr
998 = gnat_stabilize_reference (gnu_expr, true, &stable);
1000 if (stable)
1001 renamed_obj = maybe_stable_expr;
1003 /* Attaching is actually performed downstream, as soon
1004 as we have a VAR_DECL for the pointer we make. */
1007 gnu_expr = build_unary_op (ADDR_EXPR, gnu_type,
1008 maybe_stable_expr);
1010 gnu_size = NULL_TREE;
1011 used_by_ref = true;
1016 /* Make a volatile version of this object's type if we are to make
1017 the object volatile. We also interpret 13.3(19) conservatively
1018 and disallow any optimizations for such a non-constant object. */
1019 if ((Treat_As_Volatile (gnat_entity)
1020 || (!const_flag
1021 && gnu_type != except_type_node
1022 && (Is_Exported (gnat_entity)
1023 || imported_p
1024 || Present (Address_Clause (gnat_entity)))))
1025 && !TYPE_VOLATILE (gnu_type))
1026 gnu_type = build_qualified_type (gnu_type,
1027 (TYPE_QUALS (gnu_type)
1028 | TYPE_QUAL_VOLATILE));
1030 /* If we are defining an aliased object whose nominal subtype is
1031 unconstrained, the object is a record that contains both the
1032 template and the object. If there is an initializer, it will
1033 have already been converted to the right type, but we need to
1034 create the template if there is no initializer. */
1035 if (definition
1036 && !gnu_expr
1037 && TREE_CODE (gnu_type) == RECORD_TYPE
1038 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1039 /* Beware that padding might have been introduced above. */
1040 || (TYPE_PADDING_P (gnu_type)
1041 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1042 == RECORD_TYPE
1043 && TYPE_CONTAINS_TEMPLATE_P
1044 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1046 tree template_field
1047 = TYPE_PADDING_P (gnu_type)
1048 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1049 : TYPE_FIELDS (gnu_type);
1050 VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, 1);
1051 tree t = build_template (TREE_TYPE (template_field),
1052 TREE_TYPE (TREE_CHAIN (template_field)),
1053 NULL_TREE);
1054 CONSTRUCTOR_APPEND_ELT (v, template_field, t);
1055 gnu_expr = gnat_build_constructor (gnu_type, v);
1058 /* Convert the expression to the type of the object except in the
1059 case where the object's type is unconstrained or the object's type
1060 is a padded record whose field is of self-referential size. In
1061 the former case, converting will generate unnecessary evaluations
1062 of the CONSTRUCTOR to compute the size and in the latter case, we
1063 want to only copy the actual data. */
1064 if (gnu_expr
1065 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1066 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1067 && !(TYPE_IS_PADDING_P (gnu_type)
1068 && CONTAINS_PLACEHOLDER_P
1069 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1070 gnu_expr = convert (gnu_type, gnu_expr);
1072 /* If this is a pointer that doesn't have an initializing expression,
1073 initialize it to NULL, unless the object is imported. */
1074 if (definition
1075 && (POINTER_TYPE_P (gnu_type) || TYPE_IS_FAT_POINTER_P (gnu_type))
1076 && !gnu_expr
1077 && !Is_Imported (gnat_entity))
1078 gnu_expr = integer_zero_node;
1080 /* If we are defining the object and it has an Address clause, we must
1081 either get the address expression from the saved GCC tree for the
1082 object if it has a Freeze node, or elaborate the address expression
1083 here since the front-end has guaranteed that the elaboration has no
1084 effects in this case. */
1085 if (definition && Present (Address_Clause (gnat_entity)))
1087 Node_Id gnat_expr = Expression (Address_Clause (gnat_entity));
1088 tree gnu_address
1089 = present_gnu_tree (gnat_entity)
1090 ? get_gnu_tree (gnat_entity) : gnat_to_gnu (gnat_expr);
1092 save_gnu_tree (gnat_entity, NULL_TREE, false);
1094 /* Ignore the size. It's either meaningless or was handled
1095 above. */
1096 gnu_size = NULL_TREE;
1097 /* Convert the type of the object to a reference type that can
1098 alias everything as per 13.3(19). */
1099 gnu_type
1100 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1101 gnu_address = convert (gnu_type, gnu_address);
1102 used_by_ref = true;
1103 const_flag
1104 = !Is_Public (gnat_entity)
1105 || compile_time_known_address_p (gnat_expr);
1107 /* If this is a deferred constant, the initializer is attached to
1108 the full view. */
1109 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1110 gnu_expr
1111 = gnat_to_gnu
1112 (Expression (Declaration_Node (Full_View (gnat_entity))));
1114 /* If we don't have an initializing expression for the underlying
1115 variable, the initializing expression for the pointer is the
1116 specified address. Otherwise, we have to make a COMPOUND_EXPR
1117 to assign both the address and the initial value. */
1118 if (!gnu_expr)
1119 gnu_expr = gnu_address;
1120 else
1121 gnu_expr
1122 = build2 (COMPOUND_EXPR, gnu_type,
1123 build_binary_op
1124 (MODIFY_EXPR, NULL_TREE,
1125 build_unary_op (INDIRECT_REF, NULL_TREE,
1126 gnu_address),
1127 gnu_expr),
1128 gnu_address);
1131 /* If it has an address clause and we are not defining it, mark it
1132 as an indirect object. Likewise for Stdcall objects that are
1133 imported. */
1134 if ((!definition && Present (Address_Clause (gnat_entity)))
1135 || (Is_Imported (gnat_entity)
1136 && Has_Stdcall_Convention (gnat_entity)))
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_size = NULL_TREE;
1144 /* No point in taking the address of an initializing expression
1145 that isn't going to be used. */
1146 gnu_expr = NULL_TREE;
1148 /* If it has an address clause whose value is known at compile
1149 time, make the object a CONST_DECL. This will avoid a
1150 useless dereference. */
1151 if (Present (Address_Clause (gnat_entity)))
1153 Node_Id gnat_address
1154 = Expression (Address_Clause (gnat_entity));
1156 if (compile_time_known_address_p (gnat_address))
1158 gnu_expr = gnat_to_gnu (gnat_address);
1159 const_flag = true;
1163 used_by_ref = true;
1166 /* If we are at top level and this object is of variable size,
1167 make the actual type a hidden pointer to the real type and
1168 make the initializer be a memory allocation and initialization.
1169 Likewise for objects we aren't defining (presumed to be
1170 external references from other packages), but there we do
1171 not set up an initialization.
1173 If the object's size overflows, make an allocator too, so that
1174 Storage_Error gets raised. Note that we will never free
1175 such memory, so we presume it never will get allocated. */
1176 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1177 global_bindings_p ()
1178 || !definition
1179 || static_p)
1180 || (gnu_size && !allocatable_size_p (gnu_size,
1181 global_bindings_p ()
1182 || !definition
1183 || static_p)))
1185 gnu_type = build_reference_type (gnu_type);
1186 gnu_size = NULL_TREE;
1187 used_by_ref = true;
1188 const_flag = true;
1190 /* In case this was a aliased object whose nominal subtype is
1191 unconstrained, the pointer above will be a thin pointer and
1192 build_allocator will automatically make the template.
1194 If we have a template initializer only (that we made above),
1195 pretend there is none and rely on what build_allocator creates
1196 again anyway. Otherwise (if we have a full initializer), get
1197 the data part and feed that to build_allocator.
1199 If we are elaborating a mutable object, tell build_allocator to
1200 ignore a possibly simpler size from the initializer, if any, as
1201 we must allocate the maximum possible size in this case. */
1202 if (definition)
1204 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1206 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1207 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1209 gnu_alloc_type
1210 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1212 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1213 && 1 == VEC_length (constructor_elt,
1214 CONSTRUCTOR_ELTS (gnu_expr)))
1215 gnu_expr = 0;
1216 else
1217 gnu_expr
1218 = build_component_ref
1219 (gnu_expr, NULL_TREE,
1220 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1221 false);
1224 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1225 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1226 && !Is_Imported (gnat_entity))
1227 post_error ("?`Storage_Error` will be raised at run time!",
1228 gnat_entity);
1230 gnu_expr
1231 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1232 Empty, Empty, gnat_entity, mutable_p);
1234 else
1236 gnu_expr = NULL_TREE;
1237 const_flag = false;
1241 /* If this object would go into the stack and has an alignment larger
1242 than the largest stack alignment the back-end can honor, resort to
1243 a variable of "aligning type". */
1244 if (!global_bindings_p () && !static_p && definition
1245 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1247 /* Create the new variable. No need for extra room before the
1248 aligned field as this is in automatic storage. */
1249 tree gnu_new_type
1250 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1251 TYPE_SIZE_UNIT (gnu_type),
1252 BIGGEST_ALIGNMENT, 0);
1253 tree gnu_new_var
1254 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1255 NULL_TREE, gnu_new_type, NULL_TREE, false,
1256 false, false, false, NULL, gnat_entity);
1258 /* Initialize the aligned field if we have an initializer. */
1259 if (gnu_expr)
1260 add_stmt_with_node
1261 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1262 build_component_ref
1263 (gnu_new_var, NULL_TREE,
1264 TYPE_FIELDS (gnu_new_type), false),
1265 gnu_expr),
1266 gnat_entity);
1268 /* And setup this entity as a reference to the aligned field. */
1269 gnu_type = build_reference_type (gnu_type);
1270 gnu_expr
1271 = build_unary_op
1272 (ADDR_EXPR, gnu_type,
1273 build_component_ref (gnu_new_var, NULL_TREE,
1274 TYPE_FIELDS (gnu_new_type), false));
1276 gnu_size = NULL_TREE;
1277 used_by_ref = true;
1278 const_flag = true;
1281 if (const_flag)
1282 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1283 | TYPE_QUAL_CONST));
1285 /* Convert the expression to the type of the object except in the
1286 case where the object's type is unconstrained or the object's type
1287 is a padded record whose field is of self-referential size. In
1288 the former case, converting will generate unnecessary evaluations
1289 of the CONSTRUCTOR to compute the size and in the latter case, we
1290 want to only copy the actual data. */
1291 if (gnu_expr
1292 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1293 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1294 && !(TYPE_IS_PADDING_P (gnu_type)
1295 && CONTAINS_PLACEHOLDER_P
1296 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1297 gnu_expr = convert (gnu_type, gnu_expr);
1299 /* If this name is external or there was a name specified, use it,
1300 unless this is a VMS exception object since this would conflict
1301 with the symbol we need to export in addition. Don't use the
1302 Interface_Name if there is an address clause (see CD30005). */
1303 if (!Is_VMS_Exception (gnat_entity)
1304 && ((Present (Interface_Name (gnat_entity))
1305 && No (Address_Clause (gnat_entity)))
1306 || (Is_Public (gnat_entity)
1307 && (!Is_Imported (gnat_entity)
1308 || Is_Exported (gnat_entity)))))
1309 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1311 /* If this is an aggregate constant initialized to a constant, force it
1312 to be statically allocated. This saves an initialization copy. */
1313 if (!static_p
1314 && const_flag
1315 && gnu_expr && TREE_CONSTANT (gnu_expr)
1316 && AGGREGATE_TYPE_P (gnu_type)
1317 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1318 && !(TYPE_IS_PADDING_P (gnu_type)
1319 && !host_integerp (TYPE_SIZE_UNIT
1320 (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
1321 static_p = true;
1323 /* Now create the variable or the constant and set various flags. */
1324 gnu_decl
1325 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1326 gnu_expr, const_flag, Is_Public (gnat_entity),
1327 imported_p || !definition, static_p, attr_list,
1328 gnat_entity);
1329 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1330 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1332 /* If we are defining an Out parameter and optimization isn't enabled,
1333 create a fake PARM_DECL for debugging purposes and make it point to
1334 the VAR_DECL. Suppress debug info for the latter but make sure it
1335 will live on the stack so that it can be accessed from within the
1336 debugger through the PARM_DECL. */
1337 if (kind == E_Out_Parameter && definition && !optimize && debug_info_p)
1339 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1340 gnat_pushdecl (param, gnat_entity);
1341 SET_DECL_VALUE_EXPR (param, gnu_decl);
1342 DECL_HAS_VALUE_EXPR_P (param) = 1;
1343 DECL_IGNORED_P (gnu_decl) = 1;
1344 TREE_ADDRESSABLE (gnu_decl) = 1;
1347 /* If this is a renaming pointer, attach the renamed object to it and
1348 register it if we are at top level. */
1349 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1351 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1352 if (global_bindings_p ())
1354 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1355 record_global_renaming_pointer (gnu_decl);
1359 /* If this is a constant and we are defining it or it generates a real
1360 symbol at the object level and we are referencing it, we may want
1361 or need to have a true variable to represent it:
1362 - if optimization isn't enabled, for debugging purposes,
1363 - if the constant is public and not overlaid on something else,
1364 - if its address is taken,
1365 - if either itself or its type is aliased. */
1366 if (TREE_CODE (gnu_decl) == CONST_DECL
1367 && (definition || Sloc (gnat_entity) > Standard_Location)
1368 && ((!optimize && debug_info_p)
1369 || (Is_Public (gnat_entity)
1370 && No (Address_Clause (gnat_entity)))
1371 || Address_Taken (gnat_entity)
1372 || Is_Aliased (gnat_entity)
1373 || Is_Aliased (Etype (gnat_entity))))
1375 tree gnu_corr_var
1376 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1377 gnu_expr, true, Is_Public (gnat_entity),
1378 !definition, static_p, attr_list,
1379 gnat_entity);
1381 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1383 /* As debugging information will be generated for the variable,
1384 do not generate debugging information for the constant. */
1385 if (debug_info_p)
1386 DECL_IGNORED_P (gnu_decl) = 1;
1387 else
1388 DECL_IGNORED_P (gnu_corr_var) = 1;
1391 /* If this is a constant, even if we don't need a true variable, we
1392 may need to avoid returning the initializer in every case. That
1393 can happen for the address of a (constant) constructor because,
1394 upon dereferencing it, the constructor will be reinjected in the
1395 tree, which may not be valid in every case; see lvalue_required_p
1396 for more details. */
1397 if (TREE_CODE (gnu_decl) == CONST_DECL)
1398 DECL_CONST_ADDRESS_P (gnu_decl) = constructor_address_p (gnu_expr);
1400 /* If this object is declared in a block that contains a block with an
1401 exception handler, and we aren't using the GCC exception mechanism,
1402 we must force this variable in memory in order to avoid an invalid
1403 optimization. */
1404 if (Exception_Mechanism != Back_End_Exceptions
1405 && Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1406 TREE_ADDRESSABLE (gnu_decl) = 1;
1408 /* If we are defining an object with variable size or an object with
1409 fixed size that will be dynamically allocated, and we are using the
1410 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1411 if (definition
1412 && Exception_Mechanism == Setjmp_Longjmp
1413 && get_block_jmpbuf_decl ()
1414 && DECL_SIZE_UNIT (gnu_decl)
1415 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1416 || (flag_stack_check == GENERIC_STACK_CHECK
1417 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1418 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1419 add_stmt_with_node (build_call_1_expr
1420 (update_setjmp_buf_decl,
1421 build_unary_op (ADDR_EXPR, NULL_TREE,
1422 get_block_jmpbuf_decl ())),
1423 gnat_entity);
1425 /* Back-annotate Esize and Alignment of the object if not already
1426 known. Note that we pick the values of the type, not those of
1427 the object, to shield ourselves from low-level platform-dependent
1428 adjustments like alignment promotion. This is both consistent with
1429 all the treatment above, where alignment and size are set on the
1430 type of the object and not on the object directly, and makes it
1431 possible to support all confirming representation clauses. */
1432 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1433 used_by_ref);
1435 break;
1437 case E_Void:
1438 /* Return a TYPE_DECL for "void" that we previously made. */
1439 gnu_decl = TYPE_NAME (void_type_node);
1440 break;
1442 case E_Enumeration_Type:
1443 /* A special case: for the types Character and Wide_Character in
1444 Standard, we do not list all the literals. So if the literals
1445 are not specified, make this an unsigned type. */
1446 if (No (First_Literal (gnat_entity)))
1448 gnu_type = make_unsigned_type (esize);
1449 TYPE_NAME (gnu_type) = gnu_entity_name;
1451 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1452 This is needed by the DWARF-2 back-end to distinguish between
1453 unsigned integer types and character types. */
1454 TYPE_STRING_FLAG (gnu_type) = 1;
1455 break;
1459 /* We have a list of enumeral constants in First_Literal. We make a
1460 CONST_DECL for each one and build into GNU_LITERAL_LIST the list to
1461 be placed into TYPE_FIELDS. Each node in the list is a TREE_LIST
1462 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1463 value of the literal. But when we have a regular boolean type, we
1464 simplify this a little by using a BOOLEAN_TYPE. */
1465 bool is_boolean = Is_Boolean_Type (gnat_entity)
1466 && !Has_Non_Standard_Rep (gnat_entity);
1467 tree gnu_literal_list = NULL_TREE;
1468 Entity_Id gnat_literal;
1470 if (Is_Unsigned_Type (gnat_entity))
1471 gnu_type = make_unsigned_type (esize);
1472 else
1473 gnu_type = make_signed_type (esize);
1475 TREE_SET_CODE (gnu_type, is_boolean ? BOOLEAN_TYPE : ENUMERAL_TYPE);
1477 for (gnat_literal = First_Literal (gnat_entity);
1478 Present (gnat_literal);
1479 gnat_literal = Next_Literal (gnat_literal))
1481 tree gnu_value
1482 = UI_To_gnu (Enumeration_Rep (gnat_literal), gnu_type);
1483 tree gnu_literal
1484 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1485 gnu_type, gnu_value, true, false, false,
1486 false, NULL, gnat_literal);
1488 save_gnu_tree (gnat_literal, gnu_literal, false);
1489 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1490 gnu_value, gnu_literal_list);
1493 if (!is_boolean)
1494 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1496 /* Note that the bounds are updated at the end of this function
1497 to avoid an infinite recursion since they refer to the type. */
1499 break;
1501 case E_Signed_Integer_Type:
1502 case E_Ordinary_Fixed_Point_Type:
1503 case E_Decimal_Fixed_Point_Type:
1504 /* For integer types, just make a signed type the appropriate number
1505 of bits. */
1506 gnu_type = make_signed_type (esize);
1507 break;
1509 case E_Modular_Integer_Type:
1511 /* For modular types, make the unsigned type of the proper number
1512 of bits and then set up the modulus, if required. */
1513 tree gnu_modulus, gnu_high = NULL_TREE;
1515 /* Packed array types are supposed to be subtypes only. */
1516 gcc_assert (!Is_Packed_Array_Type (gnat_entity));
1518 gnu_type = make_unsigned_type (esize);
1520 /* Get the modulus in this type. If it overflows, assume it is because
1521 it is equal to 2**Esize. Note that there is no overflow checking
1522 done on unsigned type, so we detect the overflow by looking for
1523 a modulus of zero, which is otherwise invalid. */
1524 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1526 if (!integer_zerop (gnu_modulus))
1528 TYPE_MODULAR_P (gnu_type) = 1;
1529 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1530 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1531 convert (gnu_type, integer_one_node));
1534 /* If the upper bound is not maximal, make an extra subtype. */
1535 if (gnu_high
1536 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1538 tree gnu_subtype = make_unsigned_type (esize);
1539 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1540 TREE_TYPE (gnu_subtype) = gnu_type;
1541 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1542 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1543 gnu_type = gnu_subtype;
1546 break;
1548 case E_Signed_Integer_Subtype:
1549 case E_Enumeration_Subtype:
1550 case E_Modular_Integer_Subtype:
1551 case E_Ordinary_Fixed_Point_Subtype:
1552 case E_Decimal_Fixed_Point_Subtype:
1554 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1555 not want to call create_range_type since we would like each subtype
1556 node to be distinct. ??? Historically this was in preparation for
1557 when memory aliasing is implemented, but that's obsolete now given
1558 the call to relate_alias_sets below.
1560 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1561 this fact is used by the arithmetic conversion functions.
1563 We elaborate the Ancestor_Subtype if it is not in the current unit
1564 and one of our bounds is non-static. We do this to ensure consistent
1565 naming in the case where several subtypes share the same bounds, by
1566 elaborating the first such subtype first, thus using its name. */
1568 if (!definition
1569 && Present (Ancestor_Subtype (gnat_entity))
1570 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1571 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1572 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1573 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1575 /* Set the precision to the Esize except for bit-packed arrays. */
1576 if (Is_Packed_Array_Type (gnat_entity)
1577 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1578 esize = UI_To_Int (RM_Size (gnat_entity));
1580 /* This should be an unsigned type if the base type is unsigned or
1581 if the lower bound is constant and non-negative or if the type
1582 is biased. */
1583 if (Is_Unsigned_Type (Etype (gnat_entity))
1584 || Is_Unsigned_Type (gnat_entity)
1585 || Has_Biased_Representation (gnat_entity))
1586 gnu_type = make_unsigned_type (esize);
1587 else
1588 gnu_type = make_signed_type (esize);
1589 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1591 SET_TYPE_RM_MIN_VALUE
1592 (gnu_type,
1593 convert (TREE_TYPE (gnu_type),
1594 elaborate_expression (Type_Low_Bound (gnat_entity),
1595 gnat_entity, get_identifier ("L"),
1596 definition, true,
1597 Needs_Debug_Info (gnat_entity))));
1599 SET_TYPE_RM_MAX_VALUE
1600 (gnu_type,
1601 convert (TREE_TYPE (gnu_type),
1602 elaborate_expression (Type_High_Bound (gnat_entity),
1603 gnat_entity, get_identifier ("U"),
1604 definition, true,
1605 Needs_Debug_Info (gnat_entity))));
1607 /* One of the above calls might have caused us to be elaborated,
1608 so don't blow up if so. */
1609 if (present_gnu_tree (gnat_entity))
1611 maybe_present = true;
1612 break;
1615 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1616 = Has_Biased_Representation (gnat_entity);
1618 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1619 TYPE_STUB_DECL (gnu_type)
1620 = create_type_stub_decl (gnu_entity_name, gnu_type);
1622 /* Inherit our alias set from what we're a subtype of. Subtypes
1623 are not different types and a pointer can designate any instance
1624 within a subtype hierarchy. */
1625 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1627 /* For a packed array, make the original array type a parallel type. */
1628 if (debug_info_p
1629 && Is_Packed_Array_Type (gnat_entity)
1630 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1631 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1632 gnat_to_gnu_type
1633 (Original_Array_Type (gnat_entity)));
1635 /* We have to handle clauses that under-align the type specially. */
1636 if ((Present (Alignment_Clause (gnat_entity))
1637 || (Is_Packed_Array_Type (gnat_entity)
1638 && Present
1639 (Alignment_Clause (Original_Array_Type (gnat_entity)))))
1640 && UI_Is_In_Int_Range (Alignment (gnat_entity)))
1642 align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT;
1643 if (align >= TYPE_ALIGN (gnu_type))
1644 align = 0;
1647 /* If the type we are dealing with represents a bit-packed array,
1648 we need to have the bits left justified on big-endian targets
1649 and right justified on little-endian targets. We also need to
1650 ensure that when the value is read (e.g. for comparison of two
1651 such values), we only get the good bits, since the unused bits
1652 are uninitialized. Both goals are accomplished by wrapping up
1653 the modular type in an enclosing record type. */
1654 if (Is_Packed_Array_Type (gnat_entity)
1655 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1657 tree gnu_field_type, gnu_field;
1659 /* Set the RM size before wrapping up the original type. */
1660 SET_TYPE_RM_SIZE (gnu_type,
1661 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1662 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1664 /* Create a stripped-down declaration, mainly for debugging. */
1665 create_type_decl (gnu_entity_name, gnu_type, NULL, true,
1666 debug_info_p, gnat_entity);
1668 /* Now save it and build the enclosing record type. */
1669 gnu_field_type = gnu_type;
1671 gnu_type = make_node (RECORD_TYPE);
1672 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1673 TYPE_PACKED (gnu_type) = 1;
1674 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1675 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1676 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1678 /* Propagate the alignment of the modular type to the record type,
1679 unless there is an alignment clause that under-aligns the type.
1680 This means that bit-packed arrays are given "ceil" alignment for
1681 their size by default, which may seem counter-intuitive but makes
1682 it possible to overlay them on modular types easily. */
1683 TYPE_ALIGN (gnu_type)
1684 = align > 0 ? align : TYPE_ALIGN (gnu_field_type);
1686 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1688 /* Don't notify the field as "addressable", since we won't be taking
1689 it's address and it would prevent create_field_decl from making a
1690 bitfield. */
1691 gnu_field
1692 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type,
1693 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1695 /* Do not emit debug info until after the parallel type is added. */
1696 finish_record_type (gnu_type, gnu_field, 2, false);
1697 compute_record_mode (gnu_type);
1698 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1700 if (debug_info_p)
1702 /* Make the original array type a parallel type. */
1703 if (present_gnu_tree (Original_Array_Type (gnat_entity)))
1704 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1705 gnat_to_gnu_type
1706 (Original_Array_Type (gnat_entity)));
1708 rest_of_record_type_compilation (gnu_type);
1712 /* If the type we are dealing with has got a smaller alignment than the
1713 natural one, we need to wrap it up in a record type and under-align
1714 the latter. We reuse the padding machinery for this purpose. */
1715 else if (align > 0)
1717 tree gnu_field_type, gnu_field;
1719 /* Set the RM size before wrapping up the type. */
1720 SET_TYPE_RM_SIZE (gnu_type,
1721 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1723 /* Create a stripped-down declaration, mainly for debugging. */
1724 create_type_decl (gnu_entity_name, gnu_type, NULL, true,
1725 debug_info_p, gnat_entity);
1727 /* Now save it and build the enclosing record type. */
1728 gnu_field_type = gnu_type;
1730 gnu_type = make_node (RECORD_TYPE);
1731 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1732 TYPE_PACKED (gnu_type) = 1;
1733 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_field_type);
1734 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_field_type);
1735 SET_TYPE_ADA_SIZE (gnu_type, TYPE_RM_SIZE (gnu_field_type));
1736 TYPE_ALIGN (gnu_type) = align;
1737 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1739 /* Don't notify the field as "addressable", since we won't be taking
1740 it's address and it would prevent create_field_decl from making a
1741 bitfield. */
1742 gnu_field
1743 = create_field_decl (get_identifier ("F"), gnu_field_type,
1744 gnu_type, NULL_TREE, bitsize_zero_node, 1, 0);
1746 finish_record_type (gnu_type, gnu_field, 2, debug_info_p);
1747 compute_record_mode (gnu_type);
1748 TYPE_PADDING_P (gnu_type) = 1;
1751 break;
1753 case E_Floating_Point_Type:
1754 /* If this is a VAX floating-point type, use an integer of the proper
1755 size. All the operations will be handled with ASM statements. */
1756 if (Vax_Float (gnat_entity))
1758 gnu_type = make_signed_type (esize);
1759 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1760 SET_TYPE_DIGITS_VALUE (gnu_type,
1761 UI_To_gnu (Digits_Value (gnat_entity),
1762 sizetype));
1763 break;
1766 /* The type of the Low and High bounds can be our type if this is
1767 a type from Standard, so set them at the end of the function. */
1768 gnu_type = make_node (REAL_TYPE);
1769 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1770 layout_type (gnu_type);
1771 break;
1773 case E_Floating_Point_Subtype:
1774 if (Vax_Float (gnat_entity))
1776 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1777 break;
1781 if (!definition
1782 && Present (Ancestor_Subtype (gnat_entity))
1783 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1784 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1785 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1786 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1787 gnu_expr, 0);
1789 gnu_type = make_node (REAL_TYPE);
1790 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1791 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1792 TYPE_GCC_MIN_VALUE (gnu_type)
1793 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1794 TYPE_GCC_MAX_VALUE (gnu_type)
1795 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1796 layout_type (gnu_type);
1798 SET_TYPE_RM_MIN_VALUE
1799 (gnu_type,
1800 convert (TREE_TYPE (gnu_type),
1801 elaborate_expression (Type_Low_Bound (gnat_entity),
1802 gnat_entity, get_identifier ("L"),
1803 definition, true,
1804 Needs_Debug_Info (gnat_entity))));
1806 SET_TYPE_RM_MAX_VALUE
1807 (gnu_type,
1808 convert (TREE_TYPE (gnu_type),
1809 elaborate_expression (Type_High_Bound (gnat_entity),
1810 gnat_entity, get_identifier ("U"),
1811 definition, true,
1812 Needs_Debug_Info (gnat_entity))));
1814 /* One of the above calls might have caused us to be elaborated,
1815 so don't blow up if so. */
1816 if (present_gnu_tree (gnat_entity))
1818 maybe_present = true;
1819 break;
1822 /* Inherit our alias set from what we're a subtype of, as for
1823 integer subtypes. */
1824 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1826 break;
1828 /* Array and String Types and Subtypes
1830 Unconstrained array types are represented by E_Array_Type and
1831 constrained array types are represented by E_Array_Subtype. There
1832 are no actual objects of an unconstrained array type; all we have
1833 are pointers to that type.
1835 The following fields are defined on array types and subtypes:
1837 Component_Type Component type of the array.
1838 Number_Dimensions Number of dimensions (an int).
1839 First_Index Type of first index. */
1841 case E_String_Type:
1842 case E_Array_Type:
1844 Entity_Id gnat_index, gnat_name;
1845 const bool convention_fortran_p
1846 = (Convention (gnat_entity) == Convention_Fortran);
1847 const int ndim = Number_Dimensions (gnat_entity);
1848 tree gnu_template_fields = NULL_TREE;
1849 tree gnu_template_type = make_node (RECORD_TYPE);
1850 tree gnu_template_reference;
1851 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1852 tree gnu_fat_type = make_node (RECORD_TYPE);
1853 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree));
1854 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree));
1855 tree gnu_max_size = size_one_node, gnu_max_size_unit, tem;
1856 int index;
1858 TYPE_NAME (gnu_template_type)
1859 = create_concat_name (gnat_entity, "XUB");
1861 /* Make a node for the array. If we are not defining the array
1862 suppress expanding incomplete types. */
1863 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1865 if (!definition)
1867 defer_incomplete_level++;
1868 this_deferred = true;
1871 /* Build the fat pointer type. Use a "void *" object instead of
1872 a pointer to the array type since we don't have the array type
1873 yet (it will reference the fat pointer via the bounds). */
1874 tem = chainon (chainon (NULL_TREE,
1875 create_field_decl (get_identifier ("P_ARRAY"),
1876 ptr_void_type_node,
1877 gnu_fat_type, NULL_TREE,
1878 NULL_TREE, 0, 0)),
1879 create_field_decl (get_identifier ("P_BOUNDS"),
1880 gnu_ptr_template,
1881 gnu_fat_type, NULL_TREE,
1882 NULL_TREE, 0, 0));
1884 /* Make sure we can put this into a register. */
1885 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1887 /* Do not emit debug info for this record type since the types of its
1888 fields are still incomplete at this point. */
1889 finish_record_type (gnu_fat_type, tem, 0, false);
1890 TYPE_FAT_POINTER_P (gnu_fat_type) = 1;
1892 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1893 is the fat pointer. This will be used to access the individual
1894 fields once we build them. */
1895 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1896 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1897 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1898 gnu_template_reference
1899 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1900 TREE_READONLY (gnu_template_reference) = 1;
1902 /* Now create the GCC type for each index and add the fields for that
1903 index to the template. */
1904 for (index = (convention_fortran_p ? ndim - 1 : 0),
1905 gnat_index = First_Index (gnat_entity);
1906 0 <= index && index < ndim;
1907 index += (convention_fortran_p ? - 1 : 1),
1908 gnat_index = Next_Index (gnat_index))
1910 char field_name[16];
1911 tree gnu_index_base_type
1912 = get_unpadded_type (Base_Type (Etype (gnat_index)));
1913 tree gnu_lb_field, gnu_hb_field, gnu_orig_min, gnu_orig_max;
1914 tree gnu_min, gnu_max, gnu_high;
1916 /* Make the FIELD_DECLs for the low and high bounds of this
1917 type and then make extractions of these fields from the
1918 template. */
1919 sprintf (field_name, "LB%d", index);
1920 gnu_lb_field = create_field_decl (get_identifier (field_name),
1921 gnu_index_base_type,
1922 gnu_template_type, NULL_TREE,
1923 NULL_TREE, 0, 0);
1924 Sloc_to_locus (Sloc (gnat_entity),
1925 &DECL_SOURCE_LOCATION (gnu_lb_field));
1927 field_name[0] = 'U';
1928 gnu_hb_field = create_field_decl (get_identifier (field_name),
1929 gnu_index_base_type,
1930 gnu_template_type, NULL_TREE,
1931 NULL_TREE, 0, 0);
1932 Sloc_to_locus (Sloc (gnat_entity),
1933 &DECL_SOURCE_LOCATION (gnu_hb_field));
1935 gnu_temp_fields[index] = chainon (gnu_lb_field, gnu_hb_field);
1937 /* We can't use build_component_ref here since the template type
1938 isn't complete yet. */
1939 gnu_orig_min = build3 (COMPONENT_REF, gnu_index_base_type,
1940 gnu_template_reference, gnu_lb_field,
1941 NULL_TREE);
1942 gnu_orig_max = build3 (COMPONENT_REF, gnu_index_base_type,
1943 gnu_template_reference, gnu_hb_field,
1944 NULL_TREE);
1945 TREE_READONLY (gnu_orig_min) = TREE_READONLY (gnu_orig_max) = 1;
1947 gnu_min = convert (sizetype, gnu_orig_min);
1948 gnu_max = convert (sizetype, gnu_orig_max);
1950 /* Compute the size of this dimension. See the E_Array_Subtype
1951 case below for the rationale. */
1952 gnu_high
1953 = build3 (COND_EXPR, sizetype,
1954 build2 (GE_EXPR, boolean_type_node,
1955 gnu_orig_max, gnu_orig_min),
1956 gnu_max,
1957 size_binop (MINUS_EXPR, gnu_min, size_one_node));
1959 /* Make a range type with the new range in the Ada base type.
1960 Then make an index type with the size range in sizetype. */
1961 gnu_index_types[index]
1962 = create_index_type (gnu_min, gnu_high,
1963 create_range_type (gnu_index_base_type,
1964 gnu_orig_min,
1965 gnu_orig_max),
1966 gnat_entity);
1968 /* Update the maximum size of the array in elements. */
1969 if (gnu_max_size)
1971 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
1972 tree gnu_min
1973 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
1974 tree gnu_max
1975 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
1976 tree gnu_this_max
1977 = size_binop (MAX_EXPR,
1978 size_binop (PLUS_EXPR, size_one_node,
1979 size_binop (MINUS_EXPR,
1980 gnu_max, gnu_min)),
1981 size_zero_node);
1983 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1984 && TREE_OVERFLOW (gnu_this_max))
1985 gnu_max_size = NULL_TREE;
1986 else
1987 gnu_max_size
1988 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1991 TYPE_NAME (gnu_index_types[index])
1992 = create_concat_name (gnat_entity, field_name);
1995 for (index = 0; index < ndim; index++)
1996 gnu_template_fields
1997 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1999 /* Install all the fields into the template. */
2000 finish_record_type (gnu_template_type, gnu_template_fields, 0,
2001 debug_info_p);
2002 TYPE_READONLY (gnu_template_type) = 1;
2004 /* Now make the array of arrays and update the pointer to the array
2005 in the fat pointer. Note that it is the first field. */
2006 tem = gnat_to_gnu_component_type (gnat_entity, definition,
2007 debug_info_p);
2009 /* If Component_Size is not already specified, annotate it with the
2010 size of the component. */
2011 if (Unknown_Component_Size (gnat_entity))
2012 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
2014 /* Compute the maximum size of the array in units and bits. */
2015 if (gnu_max_size)
2017 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2018 TYPE_SIZE_UNIT (tem));
2019 gnu_max_size = size_binop (MULT_EXPR,
2020 convert (bitsizetype, gnu_max_size),
2021 TYPE_SIZE (tem));
2023 else
2024 gnu_max_size_unit = NULL_TREE;
2026 /* Now build the array type. */
2027 for (index = ndim - 1; index >= 0; index--)
2029 tem = build_array_type (tem, gnu_index_types[index]);
2030 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2031 if (array_type_has_nonaliased_component (tem, gnat_entity))
2032 TYPE_NONALIASED_COMPONENT (tem) = 1;
2035 /* If an alignment is specified, use it if valid. But ignore it
2036 for the original type of packed array types. If the alignment
2037 was requested with an explicit alignment clause, state so. */
2038 if (No (Packed_Array_Type (gnat_entity))
2039 && Known_Alignment (gnat_entity))
2041 TYPE_ALIGN (tem)
2042 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2043 TYPE_ALIGN (tem));
2044 if (Present (Alignment_Clause (gnat_entity)))
2045 TYPE_USER_ALIGN (tem) = 1;
2048 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2049 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2051 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2052 corresponding fat pointer. */
2053 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
2054 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2055 SET_TYPE_MODE (gnu_type, BLKmode);
2056 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2057 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2059 /* If the maximum size doesn't overflow, use it. */
2060 if (gnu_max_size
2061 && TREE_CODE (gnu_max_size) == INTEGER_CST
2062 && !TREE_OVERFLOW (gnu_max_size)
2063 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2064 && !TREE_OVERFLOW (gnu_max_size_unit))
2066 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2067 TYPE_SIZE (tem));
2068 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2069 TYPE_SIZE_UNIT (tem));
2072 create_type_decl (create_concat_name (gnat_entity, "XUA"),
2073 tem, NULL, !Comes_From_Source (gnat_entity),
2074 debug_info_p, gnat_entity);
2076 /* Give the fat pointer type a name. If this is a packed type, tell
2077 the debugger how to interpret the underlying bits. */
2078 if (Present (Packed_Array_Type (gnat_entity)))
2079 gnat_name = Packed_Array_Type (gnat_entity);
2080 else
2081 gnat_name = gnat_entity;
2082 create_type_decl (create_concat_name (gnat_name, "XUP"),
2083 gnu_fat_type, NULL, true,
2084 debug_info_p, gnat_entity);
2086 /* Create the type to be used as what a thin pointer designates:
2087 a record type for the object and its template with the fields
2088 shifted to have the template at a negative offset. */
2089 tem = build_unc_object_type (gnu_template_type, tem,
2090 create_concat_name (gnat_name, "XUT"),
2091 debug_info_p);
2092 shift_unc_components_for_thin_pointers (tem);
2094 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2095 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2097 break;
2099 case E_String_Subtype:
2100 case E_Array_Subtype:
2102 /* This is the actual data type for array variables. Multidimensional
2103 arrays are implemented as arrays of arrays. Note that arrays which
2104 have sparse enumeration subtypes as index components create sparse
2105 arrays, which is obviously space inefficient but so much easier to
2106 code for now.
2108 Also note that the subtype never refers to the unconstrained array
2109 type, which is somewhat at variance with Ada semantics.
2111 First check to see if this is simply a renaming of the array type.
2112 If so, the result is the array type. */
2114 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2115 if (!Is_Constrained (gnat_entity))
2117 else
2119 Entity_Id gnat_index, gnat_base_index;
2120 const bool convention_fortran_p
2121 = (Convention (gnat_entity) == Convention_Fortran);
2122 const int ndim = Number_Dimensions (gnat_entity);
2123 tree gnu_base_type = gnu_type;
2124 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree));
2125 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2126 bool need_index_type_struct = false;
2127 int index;
2129 /* First create the GCC type for each index and find out whether
2130 special types are needed for debugging information. */
2131 for (index = (convention_fortran_p ? ndim - 1 : 0),
2132 gnat_index = First_Index (gnat_entity),
2133 gnat_base_index
2134 = First_Index (Implementation_Base_Type (gnat_entity));
2135 0 <= index && index < ndim;
2136 index += (convention_fortran_p ? - 1 : 1),
2137 gnat_index = Next_Index (gnat_index),
2138 gnat_base_index = Next_Index (gnat_base_index))
2140 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2141 tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type);
2142 tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type);
2143 tree gnu_min = convert (sizetype, gnu_orig_min);
2144 tree gnu_max = convert (sizetype, gnu_orig_max);
2145 tree gnu_base_index_type
2146 = get_unpadded_type (Etype (gnat_base_index));
2147 tree gnu_base_orig_min = TYPE_MIN_VALUE (gnu_base_index_type);
2148 tree gnu_base_orig_max = TYPE_MAX_VALUE (gnu_base_index_type);
2149 tree gnu_high;
2151 /* See if the base array type is already flat. If it is, we
2152 are probably compiling an ACATS test but it will cause the
2153 code below to malfunction if we don't handle it specially. */
2154 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2155 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2156 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2158 gnu_min = size_one_node;
2159 gnu_max = size_zero_node;
2160 gnu_high = gnu_max;
2163 /* Similarly, if one of the values overflows in sizetype and the
2164 range is null, use 1..0 for the sizetype bounds. */
2165 else if (TREE_CODE (gnu_min) == INTEGER_CST
2166 && TREE_CODE (gnu_max) == INTEGER_CST
2167 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2168 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2170 gnu_min = size_one_node;
2171 gnu_max = size_zero_node;
2172 gnu_high = gnu_max;
2175 /* If the minimum and maximum values both overflow in sizetype,
2176 but the difference in the original type does not overflow in
2177 sizetype, ignore the overflow indication. */
2178 else if (TREE_CODE (gnu_min) == INTEGER_CST
2179 && TREE_CODE (gnu_max) == INTEGER_CST
2180 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2181 && !TREE_OVERFLOW
2182 (convert (sizetype,
2183 fold_build2 (MINUS_EXPR, gnu_index_type,
2184 gnu_orig_max,
2185 gnu_orig_min))))
2187 TREE_OVERFLOW (gnu_min) = 0;
2188 TREE_OVERFLOW (gnu_max) = 0;
2189 gnu_high = gnu_max;
2192 /* Compute the size of this dimension in the general case. We
2193 need to provide GCC with an upper bound to use but have to
2194 deal with the "superflat" case. There are three ways to do
2195 this. If we can prove that the array can never be superflat,
2196 we can just use the high bound of the index type. */
2197 else if ((Nkind (gnat_index) == N_Range
2198 && cannot_be_superflat_p (gnat_index))
2199 /* Packed Array Types are never superflat. */
2200 || Is_Packed_Array_Type (gnat_entity))
2201 gnu_high = gnu_max;
2203 /* Otherwise, if the high bound is constant but the low bound is
2204 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2205 lower bound. Note that the comparison must be done in the
2206 original type to avoid any overflow during the conversion. */
2207 else if (TREE_CODE (gnu_max) == INTEGER_CST
2208 && TREE_CODE (gnu_min) != INTEGER_CST)
2210 gnu_high = gnu_max;
2211 gnu_min
2212 = build_cond_expr (sizetype,
2213 build_binary_op (GE_EXPR,
2214 boolean_type_node,
2215 gnu_orig_max,
2216 gnu_orig_min),
2217 gnu_min,
2218 size_binop (PLUS_EXPR, gnu_max,
2219 size_one_node));
2222 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2223 in all the other cases. Note that, here as well as above,
2224 the condition used in the comparison must be equivalent to
2225 the condition (length != 0). This is relied upon in order
2226 to optimize array comparisons in compare_arrays. */
2227 else
2228 gnu_high
2229 = build_cond_expr (sizetype,
2230 build_binary_op (GE_EXPR,
2231 boolean_type_node,
2232 gnu_orig_max,
2233 gnu_orig_min),
2234 gnu_max,
2235 size_binop (MINUS_EXPR, gnu_min,
2236 size_one_node));
2238 /* Reuse the index type for the range type. Then make an index
2239 type with the size range in sizetype. */
2240 gnu_index_types[index]
2241 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2242 gnat_entity);
2244 /* Update the maximum size of the array in elements. Here we
2245 see if any constraint on the index type of the base type
2246 can be used in the case of self-referential bound on the
2247 index type of the subtype. We look for a non-"infinite"
2248 and non-self-referential bound from any type involved and
2249 handle each bound separately. */
2250 if (gnu_max_size)
2252 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2253 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2254 tree gnu_base_index_base_type
2255 = get_base_type (gnu_base_index_type);
2256 tree gnu_base_base_min
2257 = convert (sizetype,
2258 TYPE_MIN_VALUE (gnu_base_index_base_type));
2259 tree gnu_base_base_max
2260 = convert (sizetype,
2261 TYPE_MAX_VALUE (gnu_base_index_base_type));
2263 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2264 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2265 && !TREE_OVERFLOW (gnu_base_min)))
2266 gnu_base_min = gnu_min;
2268 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2269 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2270 && !TREE_OVERFLOW (gnu_base_max)))
2271 gnu_base_max = gnu_max;
2273 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2274 && TREE_OVERFLOW (gnu_base_min))
2275 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2276 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2277 && TREE_OVERFLOW (gnu_base_max))
2278 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2279 gnu_max_size = NULL_TREE;
2280 else
2282 tree gnu_this_max
2283 = size_binop (MAX_EXPR,
2284 size_binop (PLUS_EXPR, size_one_node,
2285 size_binop (MINUS_EXPR,
2286 gnu_base_max,
2287 gnu_base_min)),
2288 size_zero_node);
2290 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2291 && TREE_OVERFLOW (gnu_this_max))
2292 gnu_max_size = NULL_TREE;
2293 else
2294 gnu_max_size
2295 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2299 /* We need special types for debugging information to point to
2300 the index types if they have variable bounds, are not integer
2301 types, are biased or are wider than sizetype. */
2302 if (!integer_onep (gnu_orig_min)
2303 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2304 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2305 || (TREE_TYPE (gnu_index_type)
2306 && TREE_CODE (TREE_TYPE (gnu_index_type))
2307 != INTEGER_TYPE)
2308 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type)
2309 || compare_tree_int (rm_size (gnu_index_type),
2310 TYPE_PRECISION (sizetype)) > 0)
2311 need_index_type_struct = true;
2314 /* Then flatten: create the array of arrays. For an array type
2315 used to implement a packed array, get the component type from
2316 the original array type since the representation clauses that
2317 can affect it are on the latter. */
2318 if (Is_Packed_Array_Type (gnat_entity)
2319 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2321 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2322 for (index = ndim - 1; index >= 0; index--)
2323 gnu_type = TREE_TYPE (gnu_type);
2325 /* One of the above calls might have caused us to be elaborated,
2326 so don't blow up if so. */
2327 if (present_gnu_tree (gnat_entity))
2329 maybe_present = true;
2330 break;
2333 else
2335 gnu_type = gnat_to_gnu_component_type (gnat_entity, definition,
2336 debug_info_p);
2338 /* One of the above calls might have caused us to be elaborated,
2339 so don't blow up if so. */
2340 if (present_gnu_tree (gnat_entity))
2342 maybe_present = true;
2343 break;
2347 /* Compute the maximum size of the array in units and bits. */
2348 if (gnu_max_size)
2350 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2351 TYPE_SIZE_UNIT (gnu_type));
2352 gnu_max_size = size_binop (MULT_EXPR,
2353 convert (bitsizetype, gnu_max_size),
2354 TYPE_SIZE (gnu_type));
2356 else
2357 gnu_max_size_unit = NULL_TREE;
2359 /* Now build the array type. */
2360 for (index = ndim - 1; index >= 0; index --)
2362 gnu_type = build_array_type (gnu_type, gnu_index_types[index]);
2363 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2364 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2365 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2368 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2369 TYPE_STUB_DECL (gnu_type)
2370 = create_type_stub_decl (gnu_entity_name, gnu_type);
2372 /* If we are at file level and this is a multi-dimensional array,
2373 we need to make a variable corresponding to the stride of the
2374 inner dimensions. */
2375 if (global_bindings_p () && ndim > 1)
2377 tree gnu_st_name = get_identifier ("ST");
2378 tree gnu_arr_type;
2380 for (gnu_arr_type = TREE_TYPE (gnu_type);
2381 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2382 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2383 gnu_st_name = concat_name (gnu_st_name, "ST"))
2385 tree eltype = TREE_TYPE (gnu_arr_type);
2387 TYPE_SIZE (gnu_arr_type)
2388 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2389 gnat_entity, gnu_st_name,
2390 definition, false);
2392 /* ??? For now, store the size as a multiple of the
2393 alignment of the element type in bytes so that we
2394 can see the alignment from the tree. */
2395 TYPE_SIZE_UNIT (gnu_arr_type)
2396 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type),
2397 gnat_entity,
2398 concat_name (gnu_st_name, "A_U"),
2399 definition, false,
2400 TYPE_ALIGN (eltype));
2402 /* ??? create_type_decl is not invoked on the inner types so
2403 the MULT_EXPR node built above will never be marked. */
2404 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type));
2408 /* If we need to write out a record type giving the names of the
2409 bounds for debugging purposes, do it now and make the record
2410 type a parallel type. This is not needed for a packed array
2411 since the bounds are conveyed by the original array type. */
2412 if (need_index_type_struct
2413 && debug_info_p
2414 && !Is_Packed_Array_Type (gnat_entity))
2416 tree gnu_bound_rec = make_node (RECORD_TYPE);
2417 tree gnu_field_list = NULL_TREE;
2418 tree gnu_field;
2420 TYPE_NAME (gnu_bound_rec)
2421 = create_concat_name (gnat_entity, "XA");
2423 for (index = ndim - 1; index >= 0; index--)
2425 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2426 tree gnu_index_name = TYPE_NAME (gnu_index);
2428 if (TREE_CODE (gnu_index_name) == TYPE_DECL)
2429 gnu_index_name = DECL_NAME (gnu_index_name);
2431 /* Make sure to reference the types themselves, and not just
2432 their names, as the debugger may fall back on them. */
2433 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2434 gnu_bound_rec, NULL_TREE,
2435 NULL_TREE, 0, 0);
2436 TREE_CHAIN (gnu_field) = gnu_field_list;
2437 gnu_field_list = gnu_field;
2440 finish_record_type (gnu_bound_rec, gnu_field_list, 0, true);
2441 add_parallel_type (TYPE_STUB_DECL (gnu_type), gnu_bound_rec);
2444 /* Otherwise, for a packed array, make the original array type a
2445 parallel type. */
2446 else if (debug_info_p
2447 && Is_Packed_Array_Type (gnat_entity)
2448 && present_gnu_tree (Original_Array_Type (gnat_entity)))
2449 add_parallel_type (TYPE_STUB_DECL (gnu_type),
2450 gnat_to_gnu_type
2451 (Original_Array_Type (gnat_entity)));
2453 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2454 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2455 = (Is_Packed_Array_Type (gnat_entity)
2456 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2458 /* If the size is self-referential and the maximum size doesn't
2459 overflow, use it. */
2460 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2461 && gnu_max_size
2462 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2463 && TREE_OVERFLOW (gnu_max_size))
2464 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2465 && TREE_OVERFLOW (gnu_max_size_unit)))
2467 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2468 TYPE_SIZE (gnu_type));
2469 TYPE_SIZE_UNIT (gnu_type)
2470 = size_binop (MIN_EXPR, gnu_max_size_unit,
2471 TYPE_SIZE_UNIT (gnu_type));
2474 /* Set our alias set to that of our base type. This gives all
2475 array subtypes the same alias set. */
2476 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2478 /* If this is a packed type, make this type the same as the packed
2479 array type, but do some adjusting in the type first. */
2480 if (Present (Packed_Array_Type (gnat_entity)))
2482 Entity_Id gnat_index;
2483 tree gnu_inner;
2485 /* First finish the type we had been making so that we output
2486 debugging information for it. */
2487 if (Treat_As_Volatile (gnat_entity))
2488 gnu_type
2489 = build_qualified_type (gnu_type,
2490 TYPE_QUALS (gnu_type)
2491 | TYPE_QUAL_VOLATILE);
2493 /* Make it artificial only if the base type was artificial too.
2494 That's sort of "morally" true and will make it possible for
2495 the debugger to look it up by name in DWARF, which is needed
2496 in order to decode the packed array type. */
2497 gnu_decl
2498 = create_type_decl (gnu_entity_name, gnu_type, attr_list,
2499 !Comes_From_Source (Etype (gnat_entity))
2500 && !Comes_From_Source (gnat_entity),
2501 debug_info_p, gnat_entity);
2503 /* Save it as our equivalent in case the call below elaborates
2504 this type again. */
2505 save_gnu_tree (gnat_entity, gnu_decl, false);
2507 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2508 NULL_TREE, 0);
2509 this_made_decl = true;
2510 gnu_type = TREE_TYPE (gnu_decl);
2511 save_gnu_tree (gnat_entity, NULL_TREE, false);
2513 gnu_inner = gnu_type;
2514 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2515 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2516 || TYPE_PADDING_P (gnu_inner)))
2517 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
2519 /* We need to attach the index type to the type we just made so
2520 that the actual bounds can later be put into a template. */
2521 if ((TREE_CODE (gnu_inner) == ARRAY_TYPE
2522 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2523 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2524 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2526 if (TREE_CODE (gnu_inner) == INTEGER_TYPE)
2528 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2529 TYPE_MODULUS for modular types so we make an extra
2530 subtype if necessary. */
2531 if (TYPE_MODULAR_P (gnu_inner))
2533 tree gnu_subtype
2534 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2535 TREE_TYPE (gnu_subtype) = gnu_inner;
2536 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2537 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2538 TYPE_MIN_VALUE (gnu_inner));
2539 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2540 TYPE_MAX_VALUE (gnu_inner));
2541 gnu_inner = gnu_subtype;
2544 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2546 #ifdef ENABLE_CHECKING
2547 /* Check for other cases of overloading. */
2548 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2549 #endif
2552 for (gnat_index = First_Index (gnat_entity);
2553 Present (gnat_index);
2554 gnat_index = Next_Index (gnat_index))
2555 SET_TYPE_ACTUAL_BOUNDS
2556 (gnu_inner,
2557 tree_cons (NULL_TREE,
2558 get_unpadded_type (Etype (gnat_index)),
2559 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2561 if (Convention (gnat_entity) != Convention_Fortran)
2562 SET_TYPE_ACTUAL_BOUNDS
2563 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2565 if (TREE_CODE (gnu_type) == RECORD_TYPE
2566 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2567 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2571 else
2572 /* Abort if packed array with no Packed_Array_Type field set. */
2573 gcc_assert (!Is_Packed (gnat_entity));
2575 break;
2577 case E_String_Literal_Subtype:
2578 /* Create the type for a string literal. */
2580 Entity_Id gnat_full_type
2581 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2582 && Present (Full_View (Etype (gnat_entity)))
2583 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2584 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2585 tree gnu_string_array_type
2586 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2587 tree gnu_string_index_type
2588 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2589 (TYPE_DOMAIN (gnu_string_array_type))));
2590 tree gnu_lower_bound
2591 = convert (gnu_string_index_type,
2592 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2593 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2594 tree gnu_length = ssize_int (length - 1);
2595 tree gnu_upper_bound
2596 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2597 gnu_lower_bound,
2598 convert (gnu_string_index_type, gnu_length));
2599 tree gnu_index_type
2600 = create_index_type (convert (sizetype, gnu_lower_bound),
2601 convert (sizetype, gnu_upper_bound),
2602 create_range_type (gnu_string_index_type,
2603 gnu_lower_bound,
2604 gnu_upper_bound),
2605 gnat_entity);
2607 gnu_type
2608 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2609 gnu_index_type);
2610 if (array_type_has_nonaliased_component (gnu_type, gnat_entity))
2611 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2612 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2614 break;
2616 /* Record Types and Subtypes
2618 The following fields are defined on record types:
2620 Has_Discriminants True if the record has discriminants
2621 First_Discriminant Points to head of list of discriminants
2622 First_Entity Points to head of list of fields
2623 Is_Tagged_Type True if the record is tagged
2625 Implementation of Ada records and discriminated records:
2627 A record type definition is transformed into the equivalent of a C
2628 struct definition. The fields that are the discriminants which are
2629 found in the Full_Type_Declaration node and the elements of the
2630 Component_List found in the Record_Type_Definition node. The
2631 Component_List can be a recursive structure since each Variant of
2632 the Variant_Part of the Component_List has a Component_List.
2634 Processing of a record type definition comprises starting the list of
2635 field declarations here from the discriminants and the calling the
2636 function components_to_record to add the rest of the fields from the
2637 component list and return the gnu type node. The function
2638 components_to_record will call itself recursively as it traverses
2639 the tree. */
2641 case E_Record_Type:
2642 if (Has_Complex_Representation (gnat_entity))
2644 gnu_type
2645 = build_complex_type
2646 (get_unpadded_type
2647 (Etype (Defining_Entity
2648 (First (Component_Items
2649 (Component_List
2650 (Type_Definition
2651 (Declaration_Node (gnat_entity)))))))));
2653 break;
2657 Node_Id full_definition = Declaration_Node (gnat_entity);
2658 Node_Id record_definition = Type_Definition (full_definition);
2659 Entity_Id gnat_field;
2660 tree gnu_field, gnu_field_list = NULL_TREE, gnu_get_parent;
2661 /* Set PACKED in keeping with gnat_to_gnu_field. */
2662 int packed
2663 = Is_Packed (gnat_entity)
2665 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2666 ? -1
2667 : (Known_Alignment (gnat_entity)
2668 || (Strict_Alignment (gnat_entity)
2669 && Known_Static_Esize (gnat_entity)))
2670 ? -2
2671 : 0;
2672 bool has_discr = Has_Discriminants (gnat_entity);
2673 bool has_rep = Has_Specified_Layout (gnat_entity);
2674 bool all_rep = has_rep;
2675 bool is_extension
2676 = (Is_Tagged_Type (gnat_entity)
2677 && Nkind (record_definition) == N_Derived_Type_Definition);
2678 bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2680 /* See if all fields have a rep clause. Stop when we find one
2681 that doesn't. */
2682 if (all_rep)
2683 for (gnat_field = First_Entity (gnat_entity);
2684 Present (gnat_field);
2685 gnat_field = Next_Entity (gnat_field))
2686 if ((Ekind (gnat_field) == E_Component
2687 || Ekind (gnat_field) == E_Discriminant)
2688 && No (Component_Clause (gnat_field)))
2690 all_rep = false;
2691 break;
2694 /* If this is a record extension, go a level further to find the
2695 record definition. Also, verify we have a Parent_Subtype. */
2696 if (is_extension)
2698 if (!type_annotate_only
2699 || Present (Record_Extension_Part (record_definition)))
2700 record_definition = Record_Extension_Part (record_definition);
2702 gcc_assert (type_annotate_only
2703 || Present (Parent_Subtype (gnat_entity)));
2706 /* Make a node for the record. If we are not defining the record,
2707 suppress expanding incomplete types. */
2708 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2709 TYPE_NAME (gnu_type) = gnu_entity_name;
2710 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2712 if (!definition)
2714 defer_incomplete_level++;
2715 this_deferred = true;
2718 /* If both a size and rep clause was specified, put the size in
2719 the record type now so that it can get the proper mode. */
2720 if (has_rep && Known_Esize (gnat_entity))
2721 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2723 /* Always set the alignment here so that it can be used to
2724 set the mode, if it is making the alignment stricter. If
2725 it is invalid, it will be checked again below. If this is to
2726 be Atomic, choose a default alignment of a word unless we know
2727 the size and it's smaller. */
2728 if (Known_Alignment (gnat_entity))
2729 TYPE_ALIGN (gnu_type)
2730 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2731 else if (Is_Atomic (gnat_entity))
2732 TYPE_ALIGN (gnu_type)
2733 = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2734 /* If a type needs strict alignment, the minimum size will be the
2735 type size instead of the RM size (see validate_size). Cap the
2736 alignment, lest it causes this type size to become too large. */
2737 else if (Strict_Alignment (gnat_entity)
2738 && Known_Static_Esize (gnat_entity))
2740 unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2741 unsigned int raw_align = raw_size & -raw_size;
2742 if (raw_align < BIGGEST_ALIGNMENT)
2743 TYPE_ALIGN (gnu_type) = raw_align;
2745 else
2746 TYPE_ALIGN (gnu_type) = 0;
2748 /* If we have a Parent_Subtype, make a field for the parent. If
2749 this record has rep clauses, force the position to zero. */
2750 if (Present (Parent_Subtype (gnat_entity)))
2752 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2753 tree gnu_parent;
2755 /* A major complexity here is that the parent subtype will
2756 reference our discriminants in its Discriminant_Constraint
2757 list. But those must reference the parent component of this
2758 record which is of the parent subtype we have not built yet!
2759 To break the circle we first build a dummy COMPONENT_REF which
2760 represents the "get to the parent" operation and initialize
2761 each of those discriminants to a COMPONENT_REF of the above
2762 dummy parent referencing the corresponding discriminant of the
2763 base type of the parent subtype. */
2764 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2765 build0 (PLACEHOLDER_EXPR, gnu_type),
2766 build_decl (input_location,
2767 FIELD_DECL, NULL_TREE,
2768 void_type_node),
2769 NULL_TREE);
2771 if (has_discr)
2772 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2773 Present (gnat_field);
2774 gnat_field = Next_Stored_Discriminant (gnat_field))
2775 if (Present (Corresponding_Discriminant (gnat_field)))
2777 tree gnu_field
2778 = gnat_to_gnu_field_decl (Corresponding_Discriminant
2779 (gnat_field));
2780 save_gnu_tree
2781 (gnat_field,
2782 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2783 gnu_get_parent, gnu_field, NULL_TREE),
2784 true);
2787 /* Then we build the parent subtype. If it has discriminants but
2788 the type itself has unknown discriminants, this means that it
2789 doesn't contain information about how the discriminants are
2790 derived from those of the ancestor type, so it cannot be used
2791 directly. Instead it is built by cloning the parent subtype
2792 of the underlying record view of the type, for which the above
2793 derivation of discriminants has been made explicit. */
2794 if (Has_Discriminants (gnat_parent)
2795 && Has_Unknown_Discriminants (gnat_entity))
2797 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
2799 /* If we are defining the type, the underlying record
2800 view must already have been elaborated at this point.
2801 Otherwise do it now as its parent subtype cannot be
2802 technically elaborated on its own. */
2803 if (definition)
2804 gcc_assert (present_gnu_tree (gnat_uview));
2805 else
2806 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
2808 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
2810 /* Substitute the "get to the parent" of the type for that
2811 of its underlying record view in the cloned type. */
2812 for (gnat_field = First_Stored_Discriminant (gnat_uview);
2813 Present (gnat_field);
2814 gnat_field = Next_Stored_Discriminant (gnat_field))
2815 if (Present (Corresponding_Discriminant (gnat_field)))
2817 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
2818 tree gnu_ref
2819 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2820 gnu_get_parent, gnu_field, NULL_TREE);
2821 gnu_parent
2822 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
2825 else
2826 gnu_parent = gnat_to_gnu_type (gnat_parent);
2828 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2829 initially built. The discriminants must reference the fields
2830 of the parent subtype and not those of its base type for the
2831 placeholder machinery to properly work. */
2832 if (has_discr)
2834 /* The actual parent subtype is the full view. */
2835 if (IN (Ekind (gnat_parent), Private_Kind))
2837 if (Present (Full_View (gnat_parent)))
2838 gnat_parent = Full_View (gnat_parent);
2839 else
2840 gnat_parent = Underlying_Full_View (gnat_parent);
2843 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2844 Present (gnat_field);
2845 gnat_field = Next_Stored_Discriminant (gnat_field))
2846 if (Present (Corresponding_Discriminant (gnat_field)))
2848 Entity_Id field = Empty;
2849 for (field = First_Stored_Discriminant (gnat_parent);
2850 Present (field);
2851 field = Next_Stored_Discriminant (field))
2852 if (same_discriminant_p (gnat_field, field))
2853 break;
2854 gcc_assert (Present (field));
2855 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2856 = gnat_to_gnu_field_decl (field);
2860 /* The "get to the parent" COMPONENT_REF must be given its
2861 proper type... */
2862 TREE_TYPE (gnu_get_parent) = gnu_parent;
2864 /* ...and reference the _Parent field of this record. */
2865 gnu_field
2866 = create_field_decl (parent_name_id,
2867 gnu_parent, gnu_type,
2868 has_rep
2869 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
2870 has_rep
2871 ? bitsize_zero_node : NULL_TREE,
2872 0, 1);
2873 DECL_INTERNAL_P (gnu_field) = 1;
2874 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
2875 TYPE_FIELDS (gnu_type) = gnu_field;
2878 /* Make the fields for the discriminants and put them into the record
2879 unless it's an Unchecked_Union. */
2880 if (has_discr)
2881 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2882 Present (gnat_field);
2883 gnat_field = Next_Stored_Discriminant (gnat_field))
2885 /* If this is a record extension and this discriminant is the
2886 renaming of another discriminant, we've handled it above. */
2887 if (Present (Parent_Subtype (gnat_entity))
2888 && Present (Corresponding_Discriminant (gnat_field)))
2889 continue;
2891 gnu_field
2892 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition,
2893 debug_info_p);
2895 /* Make an expression using a PLACEHOLDER_EXPR from the
2896 FIELD_DECL node just created and link that with the
2897 corresponding GNAT defining identifier. */
2898 save_gnu_tree (gnat_field,
2899 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2900 build0 (PLACEHOLDER_EXPR, gnu_type),
2901 gnu_field, NULL_TREE),
2902 true);
2904 if (!is_unchecked_union)
2906 TREE_CHAIN (gnu_field) = gnu_field_list;
2907 gnu_field_list = gnu_field;
2911 /* Add the fields into the record type and finish it up. */
2912 components_to_record (gnu_type, Component_List (record_definition),
2913 gnu_field_list, packed, definition, NULL,
2914 false, all_rep, is_unchecked_union,
2915 debug_info_p, false);
2917 /* If it is passed by reference, force BLKmode to ensure that objects
2918 of this type will always be put in memory. */
2919 if (Is_By_Reference_Type (gnat_entity))
2920 SET_TYPE_MODE (gnu_type, BLKmode);
2922 /* We used to remove the associations of the discriminants and _Parent
2923 for validity checking but we may need them if there's a Freeze_Node
2924 for a subtype used in this record. */
2925 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2927 /* Fill in locations of fields. */
2928 annotate_rep (gnat_entity, gnu_type);
2930 /* If there are any entities in the chain corresponding to components
2931 that we did not elaborate, ensure we elaborate their types if they
2932 are Itypes. */
2933 for (gnat_temp = First_Entity (gnat_entity);
2934 Present (gnat_temp);
2935 gnat_temp = Next_Entity (gnat_temp))
2936 if ((Ekind (gnat_temp) == E_Component
2937 || Ekind (gnat_temp) == E_Discriminant)
2938 && Is_Itype (Etype (gnat_temp))
2939 && !present_gnu_tree (gnat_temp))
2940 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2942 /* If this is a record type associated with an exception definition,
2943 equate its fields to those of the standard exception type. This
2944 will make it possible to convert between them. */
2945 if (gnu_entity_name == exception_data_name_id)
2947 tree gnu_std_field;
2948 for (gnu_field = TYPE_FIELDS (gnu_type),
2949 gnu_std_field = TYPE_FIELDS (except_type_node);
2950 gnu_field;
2951 gnu_field = TREE_CHAIN (gnu_field),
2952 gnu_std_field = TREE_CHAIN (gnu_std_field))
2953 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field, gnu_std_field);
2954 gcc_assert (!gnu_std_field);
2957 break;
2959 case E_Class_Wide_Subtype:
2960 /* If an equivalent type is present, that is what we should use.
2961 Otherwise, fall through to handle this like a record subtype
2962 since it may have constraints. */
2963 if (gnat_equiv_type != gnat_entity)
2965 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
2966 maybe_present = true;
2967 break;
2970 /* ... fall through ... */
2972 case E_Record_Subtype:
2973 /* If Cloned_Subtype is Present it means this record subtype has
2974 identical layout to that type or subtype and we should use
2975 that GCC type for this one. The front end guarantees that
2976 the component list is shared. */
2977 if (Present (Cloned_Subtype (gnat_entity)))
2979 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2980 NULL_TREE, 0);
2981 maybe_present = true;
2982 break;
2985 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2986 changing the type, make a new type with each field having the type of
2987 the field in the new subtype but the position computed by transforming
2988 every discriminant reference according to the constraints. We don't
2989 see any difference between private and non-private type here since
2990 derivations from types should have been deferred until the completion
2991 of the private type. */
2992 else
2994 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2995 tree gnu_base_type;
2997 if (!definition)
2999 defer_incomplete_level++;
3000 this_deferred = true;
3003 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3005 if (present_gnu_tree (gnat_entity))
3007 maybe_present = true;
3008 break;
3011 /* If this is a record subtype associated with a dispatch table,
3012 strip the suffix. This is necessary to make sure 2 different
3013 subtypes associated with the imported and exported views of a
3014 dispatch table are properly merged in LTO mode. */
3015 if (Is_Dispatch_Table_Entity (gnat_entity))
3017 char *p;
3018 Get_Encoded_Name (gnat_entity);
3019 p = strchr (Name_Buffer, '_');
3020 gcc_assert (p);
3021 strcpy (p+2, "dtS");
3022 gnu_entity_name = get_identifier (Name_Buffer);
3025 /* When the subtype has discriminants and these discriminants affect
3026 the initial shape it has inherited, factor them in. But for an
3027 Unchecked_Union (it must be an Itype), just return the type.
3028 We can't just test Is_Constrained because private subtypes without
3029 discriminants of types with discriminants with default expressions
3030 are Is_Constrained but aren't constrained! */
3031 if (IN (Ekind (gnat_base_type), Record_Kind)
3032 && !Is_Unchecked_Union (gnat_base_type)
3033 && !Is_For_Access_Subtype (gnat_entity)
3034 && Is_Constrained (gnat_entity)
3035 && Has_Discriminants (gnat_entity)
3036 && Present (Discriminant_Constraint (gnat_entity))
3037 && Stored_Constraint (gnat_entity) != No_Elist)
3039 tree gnu_subst_list
3040 = build_subst_list (gnat_entity, gnat_base_type, definition);
3041 tree gnu_unpad_base_type, gnu_rep_part, gnu_variant_part, t;
3042 tree gnu_variant_list, gnu_pos_list, gnu_field_list = NULL_TREE;
3043 bool selected_variant = false;
3044 Entity_Id gnat_field;
3046 gnu_type = make_node (RECORD_TYPE);
3047 TYPE_NAME (gnu_type) = gnu_entity_name;
3049 /* Set the size, alignment and alias set of the new type to
3050 match that of the old one, doing required substitutions. */
3051 copy_and_substitute_in_size (gnu_type, gnu_base_type,
3052 gnu_subst_list);
3054 if (TYPE_IS_PADDING_P (gnu_base_type))
3055 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3056 else
3057 gnu_unpad_base_type = gnu_base_type;
3059 /* Look for a REP part in the base type. */
3060 gnu_rep_part = get_rep_part (gnu_unpad_base_type);
3062 /* Look for a variant part in the base type. */
3063 gnu_variant_part = get_variant_part (gnu_unpad_base_type);
3065 /* If there is a variant part, we must compute whether the
3066 constraints statically select a particular variant. If
3067 so, we simply drop the qualified union and flatten the
3068 list of fields. Otherwise we'll build a new qualified
3069 union for the variants that are still relevant. */
3070 if (gnu_variant_part)
3072 gnu_variant_list
3073 = build_variant_list (TREE_TYPE (gnu_variant_part),
3074 gnu_subst_list, NULL_TREE);
3076 /* If all the qualifiers are unconditionally true, the
3077 innermost variant is statically selected. */
3078 selected_variant = true;
3079 for (t = gnu_variant_list; t; t = TREE_CHAIN (t))
3080 if (!integer_onep (TREE_VEC_ELT (TREE_VALUE (t), 1)))
3082 selected_variant = false;
3083 break;
3086 /* Otherwise, create the new variants. */
3087 if (!selected_variant)
3088 for (t = gnu_variant_list; t; t = TREE_CHAIN (t))
3090 tree old_variant = TREE_PURPOSE (t);
3091 tree new_variant = make_node (RECORD_TYPE);
3092 TYPE_NAME (new_variant)
3093 = DECL_NAME (TYPE_NAME (old_variant));
3094 copy_and_substitute_in_size (new_variant, old_variant,
3095 gnu_subst_list);
3096 TREE_VEC_ELT (TREE_VALUE (t), 2) = new_variant;
3099 else
3101 gnu_variant_list = NULL_TREE;
3102 selected_variant = false;
3105 gnu_pos_list
3106 = build_position_list (gnu_unpad_base_type,
3107 gnu_variant_list && !selected_variant,
3108 size_zero_node, bitsize_zero_node,
3109 BIGGEST_ALIGNMENT, NULL_TREE);
3111 for (gnat_field = First_Entity (gnat_entity);
3112 Present (gnat_field);
3113 gnat_field = Next_Entity (gnat_field))
3114 if ((Ekind (gnat_field) == E_Component
3115 || Ekind (gnat_field) == E_Discriminant)
3116 && !(Present (Corresponding_Discriminant (gnat_field))
3117 && Is_Tagged_Type (gnat_base_type))
3118 && Underlying_Type (Scope (Original_Record_Component
3119 (gnat_field)))
3120 == gnat_base_type)
3122 Name_Id gnat_name = Chars (gnat_field);
3123 Entity_Id gnat_old_field
3124 = Original_Record_Component (gnat_field);
3125 tree gnu_old_field
3126 = gnat_to_gnu_field_decl (gnat_old_field);
3127 tree gnu_context = DECL_CONTEXT (gnu_old_field);
3128 tree gnu_field, gnu_field_type, gnu_size;
3129 tree gnu_cont_type, gnu_last = NULL_TREE;
3131 /* If the type is the same, retrieve the GCC type from the
3132 old field to take into account possible adjustments. */
3133 if (Etype (gnat_field) == Etype (gnat_old_field))
3134 gnu_field_type = TREE_TYPE (gnu_old_field);
3135 else
3136 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3138 /* If there was a component clause, the field types must be
3139 the same for the type and subtype, so copy the data from
3140 the old field to avoid recomputation here. Also if the
3141 field is justified modular and the optimization in
3142 gnat_to_gnu_field was applied. */
3143 if (Present (Component_Clause (gnat_old_field))
3144 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3145 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3146 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3147 == TREE_TYPE (gnu_old_field)))
3149 gnu_size = DECL_SIZE (gnu_old_field);
3150 gnu_field_type = TREE_TYPE (gnu_old_field);
3153 /* If the old field was packed and of constant size, we
3154 have to get the old size here, as it might differ from
3155 what the Etype conveys and the latter might overlap
3156 onto the following field. Try to arrange the type for
3157 possible better packing along the way. */
3158 else if (DECL_PACKED (gnu_old_field)
3159 && TREE_CODE (DECL_SIZE (gnu_old_field))
3160 == INTEGER_CST)
3162 gnu_size = DECL_SIZE (gnu_old_field);
3163 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
3164 && !TYPE_FAT_POINTER_P (gnu_field_type)
3165 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
3166 gnu_field_type
3167 = make_packable_type (gnu_field_type, true);
3170 else
3171 gnu_size = TYPE_SIZE (gnu_field_type);
3173 /* If the context of the old field is the base type or its
3174 REP part (if any), put the field directly in the new
3175 type; otherwise look up the context in the variant list
3176 and put the field either in the new type if there is a
3177 selected variant or in one of the new variants. */
3178 if (gnu_context == gnu_unpad_base_type
3179 || (gnu_rep_part
3180 && gnu_context == TREE_TYPE (gnu_rep_part)))
3181 gnu_cont_type = gnu_type;
3182 else
3184 t = purpose_member (gnu_context, gnu_variant_list);
3185 if (t)
3187 if (selected_variant)
3188 gnu_cont_type = gnu_type;
3189 else
3190 gnu_cont_type = TREE_VEC_ELT (TREE_VALUE (t), 2);
3192 else
3193 /* The front-end may pass us "ghost" components if
3194 it fails to recognize that a constrained subtype
3195 is statically constrained. Discard them. */
3196 continue;
3199 /* Now create the new field modeled on the old one. */
3200 gnu_field
3201 = create_field_decl_from (gnu_old_field, gnu_field_type,
3202 gnu_cont_type, gnu_size,
3203 gnu_pos_list, gnu_subst_list);
3205 /* Put it in one of the new variants directly. */
3206 if (gnu_cont_type != gnu_type)
3208 TREE_CHAIN (gnu_field) = TYPE_FIELDS (gnu_cont_type);
3209 TYPE_FIELDS (gnu_cont_type) = gnu_field;
3212 /* To match the layout crafted in components_to_record,
3213 if this is the _Tag or _Parent field, put it before
3214 any other fields. */
3215 else if (gnat_name == Name_uTag
3216 || gnat_name == Name_uParent)
3217 gnu_field_list = chainon (gnu_field_list, gnu_field);
3219 /* Similarly, if this is the _Controller field, put
3220 it before the other fields except for the _Tag or
3221 _Parent field. */
3222 else if (gnat_name == Name_uController && gnu_last)
3224 TREE_CHAIN (gnu_field) = TREE_CHAIN (gnu_last);
3225 TREE_CHAIN (gnu_last) = gnu_field;
3228 /* Otherwise, if this is a regular field, put it after
3229 the other fields. */
3230 else
3232 TREE_CHAIN (gnu_field) = gnu_field_list;
3233 gnu_field_list = gnu_field;
3234 if (!gnu_last)
3235 gnu_last = gnu_field;
3238 save_gnu_tree (gnat_field, gnu_field, false);
3241 /* If there is a variant list and no selected variant, we need
3242 to create the nest of variant parts from the old nest. */
3243 if (gnu_variant_list && !selected_variant)
3245 tree new_variant_part
3246 = create_variant_part_from (gnu_variant_part,
3247 gnu_variant_list, gnu_type,
3248 gnu_pos_list, gnu_subst_list);
3249 TREE_CHAIN (new_variant_part) = gnu_field_list;
3250 gnu_field_list = new_variant_part;
3253 /* Now go through the entities again looking for Itypes that
3254 we have not elaborated but should (e.g., Etypes of fields
3255 that have Original_Components). */
3256 for (gnat_field = First_Entity (gnat_entity);
3257 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3258 if ((Ekind (gnat_field) == E_Discriminant
3259 || Ekind (gnat_field) == E_Component)
3260 && !present_gnu_tree (Etype (gnat_field)))
3261 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3263 /* Do not emit debug info for the type yet since we're going to
3264 modify it below. */
3265 gnu_field_list = nreverse (gnu_field_list);
3266 finish_record_type (gnu_type, gnu_field_list, 2, false);
3268 /* See the E_Record_Type case for the rationale. */
3269 if (Is_By_Reference_Type (gnat_entity))
3270 SET_TYPE_MODE (gnu_type, BLKmode);
3271 else
3272 compute_record_mode (gnu_type);
3274 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3276 /* Fill in locations of fields. */
3277 annotate_rep (gnat_entity, gnu_type);
3279 /* If debugging information is being written for the type, write
3280 a record that shows what we are a subtype of and also make a
3281 variable that indicates our size, if still variable. */
3282 if (debug_info_p)
3284 tree gnu_subtype_marker = make_node (RECORD_TYPE);
3285 tree gnu_unpad_base_name = TYPE_NAME (gnu_unpad_base_type);
3286 tree gnu_size_unit = TYPE_SIZE_UNIT (gnu_type);
3288 if (TREE_CODE (gnu_unpad_base_name) == TYPE_DECL)
3289 gnu_unpad_base_name = DECL_NAME (gnu_unpad_base_name);
3291 TYPE_NAME (gnu_subtype_marker)
3292 = create_concat_name (gnat_entity, "XVS");
3293 finish_record_type (gnu_subtype_marker,
3294 create_field_decl (gnu_unpad_base_name,
3295 build_reference_type
3296 (gnu_unpad_base_type),
3297 gnu_subtype_marker,
3298 NULL_TREE, NULL_TREE,
3299 0, 0),
3300 0, true);
3302 add_parallel_type (TYPE_STUB_DECL (gnu_type),
3303 gnu_subtype_marker);
3305 if (definition
3306 && TREE_CODE (gnu_size_unit) != INTEGER_CST
3307 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit))
3308 TYPE_SIZE_UNIT (gnu_subtype_marker)
3309 = create_var_decl (create_concat_name (gnat_entity,
3310 "XVZ"),
3311 NULL_TREE, sizetype, gnu_size_unit,
3312 false, false, false, false, NULL,
3313 gnat_entity);
3316 /* Now we can finalize it. */
3317 rest_of_record_type_compilation (gnu_type);
3320 /* Otherwise, go down all the components in the new type and make
3321 them equivalent to those in the base type. */
3322 else
3324 gnu_type = gnu_base_type;
3326 for (gnat_temp = First_Entity (gnat_entity);
3327 Present (gnat_temp);
3328 gnat_temp = Next_Entity (gnat_temp))
3329 if ((Ekind (gnat_temp) == E_Discriminant
3330 && !Is_Unchecked_Union (gnat_base_type))
3331 || Ekind (gnat_temp) == E_Component)
3332 save_gnu_tree (gnat_temp,
3333 gnat_to_gnu_field_decl
3334 (Original_Record_Component (gnat_temp)),
3335 false);
3338 break;
3340 case E_Access_Subprogram_Type:
3341 /* Use the special descriptor type for dispatch tables if needed,
3342 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3343 Note that we are only required to do so for static tables in
3344 order to be compatible with the C++ ABI, but Ada 2005 allows
3345 to extend library level tagged types at the local level so
3346 we do it in the non-static case as well. */
3347 if (TARGET_VTABLE_USES_DESCRIPTORS
3348 && Is_Dispatch_Table_Entity (gnat_entity))
3350 gnu_type = fdesc_type_node;
3351 gnu_size = TYPE_SIZE (gnu_type);
3352 break;
3355 /* ... fall through ... */
3357 case E_Anonymous_Access_Subprogram_Type:
3358 /* If we are not defining this entity, and we have incomplete
3359 entities being processed above us, make a dummy type and
3360 fill it in later. */
3361 if (!definition && defer_incomplete_level != 0)
3363 struct incomplete *p
3364 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3366 gnu_type
3367 = build_pointer_type
3368 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3369 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
3370 !Comes_From_Source (gnat_entity),
3371 debug_info_p, gnat_entity);
3372 this_made_decl = true;
3373 gnu_type = TREE_TYPE (gnu_decl);
3374 save_gnu_tree (gnat_entity, gnu_decl, false);
3375 saved = true;
3377 p->old_type = TREE_TYPE (gnu_type);
3378 p->full_type = Directly_Designated_Type (gnat_entity);
3379 p->next = defer_incomplete_list;
3380 defer_incomplete_list = p;
3381 break;
3384 /* ... fall through ... */
3386 case E_Allocator_Type:
3387 case E_Access_Type:
3388 case E_Access_Attribute_Type:
3389 case E_Anonymous_Access_Type:
3390 case E_General_Access_Type:
3392 /* The designated type and its equivalent type for gigi. */
3393 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3394 Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3395 /* Whether it comes from a limited with. */
3396 bool is_from_limited_with
3397 = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3398 && From_With_Type (gnat_desig_equiv));
3399 /* The "full view" of the designated type. If this is an incomplete
3400 entity from a limited with, treat its non-limited view as the full
3401 view. Otherwise, if this is an incomplete or private type, use the
3402 full view. In the former case, we might point to a private type,
3403 in which case, we need its full view. Also, we want to look at the
3404 actual type used for the representation, so this takes a total of
3405 three steps. */
3406 Entity_Id gnat_desig_full_direct_first
3407 = (is_from_limited_with
3408 ? Non_Limited_View (gnat_desig_equiv)
3409 : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3410 ? Full_View (gnat_desig_equiv) : Empty));
3411 Entity_Id gnat_desig_full_direct
3412 = ((is_from_limited_with
3413 && Present (gnat_desig_full_direct_first)
3414 && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3415 ? Full_View (gnat_desig_full_direct_first)
3416 : gnat_desig_full_direct_first);
3417 Entity_Id gnat_desig_full
3418 = Gigi_Equivalent_Type (gnat_desig_full_direct);
3419 /* The type actually used to represent the designated type, either
3420 gnat_desig_full or gnat_desig_equiv. */
3421 Entity_Id gnat_desig_rep;
3422 /* True if this is a pointer to an unconstrained array. */
3423 bool is_unconstrained_array;
3424 /* We want to know if we'll be seeing the freeze node for any
3425 incomplete type we may be pointing to. */
3426 bool in_main_unit
3427 = (Present (gnat_desig_full)
3428 ? In_Extended_Main_Code_Unit (gnat_desig_full)
3429 : In_Extended_Main_Code_Unit (gnat_desig_type));
3430 /* True if we make a dummy type here. */
3431 bool made_dummy = false;
3432 /* True if the dummy type is a fat pointer. */
3433 bool got_fat_p = false;
3434 /* The mode to be used for the pointer type. */
3435 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3436 /* The GCC type used for the designated type. */
3437 tree gnu_desig_type = NULL_TREE;
3439 if (!targetm.valid_pointer_mode (p_mode))
3440 p_mode = ptr_mode;
3442 /* If either the designated type or its full view is an unconstrained
3443 array subtype, replace it with the type it's a subtype of. This
3444 avoids problems with multiple copies of unconstrained array types.
3445 Likewise, if the designated type is a subtype of an incomplete
3446 record type, use the parent type to avoid order of elaboration
3447 issues. This can lose some code efficiency, but there is no
3448 alternative. */
3449 if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3450 && !Is_Constrained (gnat_desig_equiv))
3451 gnat_desig_equiv = Etype (gnat_desig_equiv);
3452 if (Present (gnat_desig_full)
3453 && ((Ekind (gnat_desig_full) == E_Array_Subtype
3454 && !Is_Constrained (gnat_desig_full))
3455 || (Ekind (gnat_desig_full) == E_Record_Subtype
3456 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3457 gnat_desig_full = Etype (gnat_desig_full);
3459 /* Set the type that's actually the representation of the designated
3460 type and also flag whether we have a unconstrained array. */
3461 gnat_desig_rep
3462 = Present (gnat_desig_full) ? gnat_desig_full : gnat_desig_equiv;
3463 is_unconstrained_array
3464 = Is_Array_Type (gnat_desig_rep) && !Is_Constrained (gnat_desig_rep);
3466 /* If we are pointing to an incomplete type whose completion is an
3467 unconstrained array, make a fat pointer type. The two types in our
3468 fields will be pointers to dummy nodes and will be replaced in
3469 update_pointer_to. Similarly, if the type itself is a dummy type or
3470 an unconstrained array. Also make a dummy TYPE_OBJECT_RECORD_TYPE
3471 in case we have any thin pointers to it. */
3472 if (is_unconstrained_array
3473 && (Present (gnat_desig_full)
3474 || (present_gnu_tree (gnat_desig_equiv)
3475 && TYPE_IS_DUMMY_P
3476 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv))))
3477 || (!in_main_unit
3478 && defer_incomplete_level
3479 && !present_gnu_tree (gnat_desig_equiv))
3480 || (in_main_unit
3481 && is_from_limited_with
3482 && Present (Freeze_Node (gnat_desig_equiv)))))
3484 if (present_gnu_tree (gnat_desig_rep))
3485 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_rep));
3486 else
3488 gnu_desig_type = make_dummy_type (gnat_desig_rep);
3489 /* Show the dummy we get will be a fat pointer. */
3490 got_fat_p = made_dummy = true;
3493 /* If the call above got something that has a pointer, the pointer
3494 is our type. This could have happened either because the type
3495 was elaborated or because somebody else executed the code. */
3496 gnu_type = TYPE_POINTER_TO (gnu_desig_type);
3497 if (!gnu_type)
3499 tree gnu_template_type = make_node (ENUMERAL_TYPE);
3500 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
3501 tree gnu_array_type = make_node (ENUMERAL_TYPE);
3502 tree gnu_ptr_array = build_pointer_type (gnu_array_type);
3503 tree fields;
3505 TYPE_NAME (gnu_template_type)
3506 = create_concat_name (gnat_desig_equiv, "XUB");
3507 TYPE_DUMMY_P (gnu_template_type) = 1;
3509 TYPE_NAME (gnu_array_type)
3510 = create_concat_name (gnat_desig_equiv, "XUA");
3511 TYPE_DUMMY_P (gnu_array_type) = 1;
3513 gnu_type = make_node (RECORD_TYPE);
3514 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_desig_type);
3515 TYPE_POINTER_TO (gnu_desig_type) = gnu_type;
3517 fields
3518 = create_field_decl (get_identifier ("P_ARRAY"),
3519 gnu_ptr_array, gnu_type,
3520 NULL_TREE, NULL_TREE, 0, 0);
3521 TREE_CHAIN (fields)
3522 = create_field_decl (get_identifier ("P_BOUNDS"),
3523 gnu_ptr_template, gnu_type,
3524 NULL_TREE, NULL_TREE, 0, 0);
3526 /* Make sure we can place this into a register. */
3527 TYPE_ALIGN (gnu_type)
3528 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
3529 TYPE_FAT_POINTER_P (gnu_type) = 1;
3531 /* Do not emit debug info for this record type since the types
3532 of its fields are incomplete. */
3533 finish_record_type (gnu_type, fields, 0, false);
3535 TYPE_OBJECT_RECORD_TYPE (gnu_desig_type)
3536 = make_node (RECORD_TYPE);
3537 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type))
3538 = create_concat_name (gnat_desig_equiv, "XUT");
3539 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type)) = 1;
3543 /* If we already know what the full type is, use it. */
3544 else if (Present (gnat_desig_full)
3545 && present_gnu_tree (gnat_desig_full))
3546 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3548 /* Get the type of the thing we are to point to and build a pointer to
3549 it. If it is a reference to an incomplete or private type with a
3550 full view that is a record, make a dummy type node and get the
3551 actual type later when we have verified it is safe. */
3552 else if ((!in_main_unit
3553 && !present_gnu_tree (gnat_desig_equiv)
3554 && Present (gnat_desig_full)
3555 && !present_gnu_tree (gnat_desig_full)
3556 && Is_Record_Type (gnat_desig_full))
3557 /* Likewise if we are pointing to a record or array and we are
3558 to defer elaborating incomplete types. We do this as this
3559 access type may be the full view of a private type. Note
3560 that the unconstrained array case is handled above. */
3561 || ((!in_main_unit || imported_p)
3562 && defer_incomplete_level
3563 && !present_gnu_tree (gnat_desig_equiv)
3564 && (Is_Record_Type (gnat_desig_rep)
3565 || Is_Array_Type (gnat_desig_rep)))
3566 /* If this is a reference from a limited_with type back to our
3567 main unit and there's a freeze node for it, either we have
3568 already processed the declaration and made the dummy type,
3569 in which case we just reuse the latter, or we have not yet,
3570 in which case we make the dummy type and it will be reused
3571 when the declaration is finally processed. In both cases,
3572 the pointer eventually created below will be automatically
3573 adjusted when the freeze node is processed. Note that the
3574 unconstrained array case is handled above. */
3575 || (in_main_unit
3576 && is_from_limited_with
3577 && Present (Freeze_Node (gnat_desig_rep))))
3579 gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3580 made_dummy = true;
3583 /* Otherwise handle the case of a pointer to itself. */
3584 else if (gnat_desig_equiv == gnat_entity)
3586 gnu_type
3587 = build_pointer_type_for_mode (void_type_node, p_mode,
3588 No_Strict_Aliasing (gnat_entity));
3589 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3592 /* If expansion is disabled, the equivalent type of a concurrent type
3593 is absent, so build a dummy pointer type. */
3594 else if (type_annotate_only && No (gnat_desig_equiv))
3595 gnu_type = ptr_void_type_node;
3597 /* Finally, handle the default case where we can just elaborate our
3598 designated type. */
3599 else
3600 gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3602 /* It is possible that a call to gnat_to_gnu_type above resolved our
3603 type. If so, just return it. */
3604 if (present_gnu_tree (gnat_entity))
3606 maybe_present = true;
3607 break;
3610 /* If we have not done it yet, build the pointer type the usual way. */
3611 if (!gnu_type)
3613 /* Modify the designated type if we are pointing only to constant
3614 objects, but don't do it for unconstrained arrays. */
3615 if (Is_Access_Constant (gnat_entity)
3616 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3618 gnu_desig_type
3619 = build_qualified_type
3620 (gnu_desig_type,
3621 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3623 /* Some extra processing is required if we are building a
3624 pointer to an incomplete type (in the GCC sense). We might
3625 have such a type if we just made a dummy, or directly out
3626 of the call to gnat_to_gnu_type above if we are processing
3627 an access type for a record component designating the
3628 record type itself. */
3629 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3631 /* We must ensure that the pointer to variant we make will
3632 be processed by update_pointer_to when the initial type
3633 is completed. Pretend we made a dummy and let further
3634 processing act as usual. */
3635 made_dummy = true;
3637 /* We must ensure that update_pointer_to will not retrieve
3638 the dummy variant when building a properly qualified
3639 version of the complete type. We take advantage of the
3640 fact that get_qualified_type is requiring TYPE_NAMEs to
3641 match to influence build_qualified_type and then also
3642 update_pointer_to here. */
3643 TYPE_NAME (gnu_desig_type)
3644 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3648 gnu_type
3649 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3650 No_Strict_Aliasing (gnat_entity));
3653 /* If we are not defining this object and we have made a dummy pointer,
3654 save our current definition, evaluate the actual type, and replace
3655 the tentative type we made with the actual one. If we are to defer
3656 actually looking up the actual type, make an entry in the deferred
3657 list. If this is from a limited with, we have to defer to the end
3658 of the current spec in two cases: first if the designated type is
3659 in the current unit and second if the access type itself is. */
3660 if ((!in_main_unit || is_from_limited_with) && made_dummy)
3662 bool is_from_limited_with_in_main_unit
3663 = (is_from_limited_with
3664 && (in_main_unit
3665 || In_Extended_Main_Code_Unit (gnat_entity)));
3666 tree gnu_old_desig_type
3667 = TYPE_IS_FAT_POINTER_P (gnu_type)
3668 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3670 if (esize == POINTER_SIZE
3671 && (got_fat_p || TYPE_IS_FAT_POINTER_P (gnu_type)))
3672 gnu_type
3673 = build_pointer_type
3674 (TYPE_OBJECT_RECORD_TYPE
3675 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3677 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
3678 !Comes_From_Source (gnat_entity),
3679 debug_info_p, gnat_entity);
3680 this_made_decl = true;
3681 gnu_type = TREE_TYPE (gnu_decl);
3682 save_gnu_tree (gnat_entity, gnu_decl, false);
3683 saved = true;
3685 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3686 update gnu_old_desig_type directly, in which case it will not be
3687 a dummy type any more when we get into update_pointer_to.
3689 This can happen e.g. when the designated type is a record type,
3690 because their elaboration starts with an initial node from
3691 make_dummy_type, which may be the same node as the one we got.
3693 Besides, variants of this non-dummy type might have been created
3694 along the way. update_pointer_to is expected to properly take
3695 care of those situations. */
3696 if (!defer_incomplete_level && !is_from_limited_with_in_main_unit)
3697 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type),
3698 gnat_to_gnu_type (gnat_desig_equiv));
3699 else
3701 struct incomplete *p = XNEW (struct incomplete);
3702 struct incomplete **head
3703 = (is_from_limited_with_in_main_unit
3704 ? &defer_limited_with : &defer_incomplete_list);
3705 p->old_type = gnu_old_desig_type;
3706 p->full_type = gnat_desig_equiv;
3707 p->next = *head;
3708 *head = p;
3712 break;
3714 case E_Access_Protected_Subprogram_Type:
3715 case E_Anonymous_Access_Protected_Subprogram_Type:
3716 if (type_annotate_only && No (gnat_equiv_type))
3717 gnu_type = ptr_void_type_node;
3718 else
3720 /* The run-time representation is the equivalent type. */
3721 gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3722 maybe_present = true;
3725 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3726 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3727 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3728 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3729 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3730 NULL_TREE, 0);
3732 break;
3734 case E_Access_Subtype:
3736 /* We treat this as identical to its base type; any constraint is
3737 meaningful only to the front end.
3739 The designated type must be elaborated as well, if it does
3740 not have its own freeze node. Designated (sub)types created
3741 for constrained components of records with discriminants are
3742 not frozen by the front end and thus not elaborated by gigi,
3743 because their use may appear before the base type is frozen,
3744 and because it is not clear that they are needed anywhere in
3745 Gigi. With the current model, there is no correct place where
3746 they could be elaborated. */
3748 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3749 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3750 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3751 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3752 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3754 /* If we are not defining this entity, and we have incomplete
3755 entities being processed above us, make a dummy type and
3756 elaborate it later. */
3757 if (!definition && defer_incomplete_level != 0)
3759 struct incomplete *p
3760 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3761 tree gnu_ptr_type
3762 = build_pointer_type
3763 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3765 p->old_type = TREE_TYPE (gnu_ptr_type);
3766 p->full_type = Directly_Designated_Type (gnat_entity);
3767 p->next = defer_incomplete_list;
3768 defer_incomplete_list = p;
3770 else if (!IN (Ekind (Base_Type
3771 (Directly_Designated_Type (gnat_entity))),
3772 Incomplete_Or_Private_Kind))
3773 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3774 NULL_TREE, 0);
3777 maybe_present = true;
3778 break;
3780 /* Subprogram Entities
3782 The following access functions are defined for subprograms (functions
3783 or procedures):
3785 First_Formal The first formal parameter.
3786 Is_Imported Indicates that the subprogram has appeared in
3787 an INTERFACE or IMPORT pragma. For now we
3788 assume that the external language is C.
3789 Is_Exported Likewise but for an EXPORT pragma.
3790 Is_Inlined True if the subprogram is to be inlined.
3792 In addition for function subprograms we have:
3794 Etype Return type of the function.
3796 Each parameter is first checked by calling must_pass_by_ref on its
3797 type to determine if it is passed by reference. For parameters which
3798 are copied in, if they are Ada In Out or Out parameters, their return
3799 value becomes part of a record which becomes the return type of the
3800 function (C function - note that this applies only to Ada procedures
3801 so there is no Ada return type). Additional code to store back the
3802 parameters will be generated on the caller side. This transformation
3803 is done here, not in the front-end.
3805 The intended result of the transformation can be seen from the
3806 equivalent source rewritings that follow:
3808 struct temp {int a,b};
3809 procedure P (A,B: In Out ...) is temp P (int A,B)
3810 begin {
3811 .. ..
3812 end P; return {A,B};
3815 temp t;
3816 P(X,Y); t = P(X,Y);
3817 X = t.a , Y = t.b;
3819 For subprogram types we need to perform mainly the same conversions to
3820 GCC form that are needed for procedures and function declarations. The
3821 only difference is that at the end, we make a type declaration instead
3822 of a function declaration. */
3824 case E_Subprogram_Type:
3825 case E_Function:
3826 case E_Procedure:
3828 /* The first GCC parameter declaration (a PARM_DECL node). The
3829 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3830 actually is the head of this parameter list. */
3831 tree gnu_param_list = NULL_TREE;
3832 /* Likewise for the stub associated with an exported procedure. */
3833 tree gnu_stub_param_list = NULL_TREE;
3834 /* The type returned by a function. If the subprogram is a procedure
3835 this type should be void_type_node. */
3836 tree gnu_return_type = void_type_node;
3837 /* List of fields in return type of procedure with copy-in copy-out
3838 parameters. */
3839 tree gnu_field_list = NULL_TREE;
3840 /* Non-null for subprograms containing parameters passed by copy-in
3841 copy-out (Ada In Out or Out parameters not passed by reference),
3842 in which case it is the list of nodes used to specify the values
3843 of the In Out/Out parameters that are returned as a record upon
3844 procedure return. The TREE_PURPOSE of an element of this list is
3845 a field of the record and the TREE_VALUE is the PARM_DECL
3846 corresponding to that field. This list will be saved in the
3847 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3848 tree gnu_cico_list = NULL_TREE;
3849 /* If an import pragma asks to map this subprogram to a GCC builtin,
3850 this is the builtin DECL node. */
3851 tree gnu_builtin_decl = NULL_TREE;
3852 /* For the stub associated with an exported procedure. */
3853 tree gnu_stub_type = NULL_TREE, gnu_stub_name = NULL_TREE;
3854 tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
3855 Entity_Id gnat_param;
3856 bool inline_flag = Is_Inlined (gnat_entity);
3857 bool public_flag = Is_Public (gnat_entity) || imported_p;
3858 bool extern_flag
3859 = (Is_Public (gnat_entity) && !definition) || imported_p;
3861 /* The semantics of "pure" in Ada essentially matches that of "const"
3862 in the back-end. In particular, both properties are orthogonal to
3863 the "nothrow" property if the EH circuitry is explicit in the
3864 internal representation of the back-end. If we are to completely
3865 hide the EH circuitry from it, we need to declare that calls to pure
3866 Ada subprograms that can throw have side effects since they can
3867 trigger an "abnormal" transfer of control flow; thus they can be
3868 neither "const" nor "pure" in the back-end sense. */
3869 bool const_flag
3870 = (Exception_Mechanism == Back_End_Exceptions
3871 && Is_Pure (gnat_entity));
3873 bool volatile_flag = No_Return (gnat_entity);
3874 bool return_by_direct_ref_p = false;
3875 bool return_by_invisi_ref_p = false;
3876 bool return_unconstrained_p = false;
3877 bool has_copy_in_out = false;
3878 bool has_stub = false;
3879 int parmnum;
3881 /* A parameter may refer to this type, so defer completion of any
3882 incomplete types. */
3883 if (kind == E_Subprogram_Type && !definition)
3885 defer_incomplete_level++;
3886 this_deferred = true;
3889 /* If the subprogram has an alias, it is probably inherited, so
3890 we can use the original one. If the original "subprogram"
3891 is actually an enumeration literal, it may be the first use
3892 of its type, so we must elaborate that type now. */
3893 if (Present (Alias (gnat_entity)))
3895 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3896 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3898 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3899 gnu_expr, 0);
3901 /* Elaborate any Itypes in the parameters of this entity. */
3902 for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3903 Present (gnat_temp);
3904 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3905 if (Is_Itype (Etype (gnat_temp)))
3906 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3908 break;
3911 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3912 corresponding DECL node. Proper generation of calls later on need
3913 proper parameter associations so we don't "break;" here. */
3914 if (Convention (gnat_entity) == Convention_Intrinsic
3915 && Present (Interface_Name (gnat_entity)))
3917 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3919 /* Unability to find the builtin decl most often indicates a
3920 genuine mistake, but imports of unregistered intrinsics are
3921 sometimes issued on purpose to allow hooking in alternate
3922 bodies. We post a warning conditioned on Wshadow in this case,
3923 to let developers be notified on demand without risking false
3924 positives with common default sets of options. */
3926 if (gnu_builtin_decl == NULL_TREE && warn_shadow)
3927 post_error ("?gcc intrinsic not found for&!", gnat_entity);
3930 /* ??? What if we don't find the builtin node above ? warn ? err ?
3931 In the current state we neither warn nor err, and calls will just
3932 be handled as for regular subprograms. */
3934 if (kind == E_Function || kind == E_Subprogram_Type)
3935 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3937 /* If this function returns by reference, make the actual return
3938 type of this function the pointer and mark the decl. */
3939 if (Returns_By_Ref (gnat_entity))
3941 gnu_return_type = build_pointer_type (gnu_return_type);
3942 return_by_direct_ref_p = true;
3945 /* If the Mechanism is By_Reference, ensure this function uses the
3946 target's by-invisible-reference mechanism, which may not be the
3947 same as above (e.g. it might be passing an extra parameter).
3949 Prior to GCC 4, this was handled by just setting TREE_ADDRESSABLE
3950 on the result type. Everything required to pass by invisible
3951 reference using the target's mechanism (e.g. an extra parameter)
3952 was handled at RTL expansion time.
3954 This doesn't work with GCC 4 any more for several reasons. First,
3955 the gimplification process might need to create temporaries of this
3956 type and the gimplifier ICEs on such attempts; that's why the flag
3957 is now set on the function type instead. Second, the middle-end
3958 now also relies on a different attribute, DECL_BY_REFERENCE on the
3959 RESULT_DECL, and expects the by-invisible-reference-ness to be made
3960 explicit in the function body. */
3961 else if (kind == E_Function && Mechanism (gnat_entity) == By_Reference)
3962 return_by_invisi_ref_p = true;
3964 /* If we are supposed to return an unconstrained array, actually return
3965 a fat pointer and make a note of that. */
3966 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3968 gnu_return_type = TREE_TYPE (gnu_return_type);
3969 return_unconstrained_p = true;
3972 /* If the type requires a transient scope, the result is allocated
3973 on the secondary stack, so the result type of the function is
3974 just a pointer. */
3975 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3977 gnu_return_type = build_pointer_type (gnu_return_type);
3978 return_unconstrained_p = true;
3981 /* If the type is a padded type and the underlying type would not
3982 be passed by reference or this function has a foreign convention,
3983 return the underlying type. */
3984 else if (TYPE_IS_PADDING_P (gnu_return_type)
3985 && (!default_pass_by_ref (TREE_TYPE
3986 (TYPE_FIELDS (gnu_return_type)))
3987 || Has_Foreign_Convention (gnat_entity)))
3988 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3990 /* If the return type is unconstrained, that means it must have a
3991 maximum size. Use the padded type as the effective return type.
3992 And ensure the function uses the target's by-invisible-reference
3993 mechanism to avoid copying too much data when it returns. */
3994 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
3996 gnu_return_type
3997 = maybe_pad_type (gnu_return_type,
3998 max_size (TYPE_SIZE (gnu_return_type), true),
3999 0, gnat_entity, false, false, false, true);
4000 return_by_invisi_ref_p = true;
4003 /* If the return type has a size that overflows, we cannot have
4004 a function that returns that type. This usage doesn't make
4005 sense anyway, so give an error here. */
4006 if (TYPE_SIZE_UNIT (gnu_return_type)
4007 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type))
4008 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
4010 post_error ("cannot return type whose size overflows",
4011 gnat_entity);
4012 gnu_return_type = copy_node (gnu_return_type);
4013 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
4014 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
4015 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
4016 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
4019 /* Look at all our parameters and get the type of
4020 each. While doing this, build a copy-out structure if
4021 we need one. */
4023 /* Loop over the parameters and get their associated GCC tree.
4024 While doing this, build a copy-out structure if we need one. */
4025 for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
4026 Present (gnat_param);
4027 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
4029 tree gnu_param_name = get_entity_name (gnat_param);
4030 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
4031 tree gnu_param, gnu_field;
4032 bool copy_in_copy_out = false;
4033 Mechanism_Type mech = Mechanism (gnat_param);
4035 /* Builtins are expanded inline and there is no real call sequence
4036 involved. So the type expected by the underlying expander is
4037 always the type of each argument "as is". */
4038 if (gnu_builtin_decl)
4039 mech = By_Copy;
4040 /* Handle the first parameter of a valued procedure specially. */
4041 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
4042 mech = By_Copy_Return;
4043 /* Otherwise, see if a Mechanism was supplied that forced this
4044 parameter to be passed one way or another. */
4045 else if (mech == Default
4046 || mech == By_Copy || mech == By_Reference)
4048 else if (By_Descriptor_Last <= mech && mech <= By_Descriptor)
4049 mech = By_Descriptor;
4051 else if (By_Short_Descriptor_Last <= mech &&
4052 mech <= By_Short_Descriptor)
4053 mech = By_Short_Descriptor;
4055 else if (mech > 0)
4057 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
4058 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
4059 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
4060 mech))
4061 mech = By_Reference;
4062 else
4063 mech = By_Copy;
4065 else
4067 post_error ("unsupported mechanism for&", gnat_param);
4068 mech = Default;
4071 gnu_param
4072 = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
4073 Has_Foreign_Convention (gnat_entity),
4074 &copy_in_copy_out);
4076 /* We are returned either a PARM_DECL or a type if no parameter
4077 needs to be passed; in either case, adjust the type. */
4078 if (DECL_P (gnu_param))
4079 gnu_param_type = TREE_TYPE (gnu_param);
4080 else
4082 gnu_param_type = gnu_param;
4083 gnu_param = NULL_TREE;
4086 if (gnu_param)
4088 /* If it's an exported subprogram, we build a parameter list
4089 in parallel, in case we need to emit a stub for it. */
4090 if (Is_Exported (gnat_entity))
4092 gnu_stub_param_list
4093 = chainon (gnu_param, gnu_stub_param_list);
4094 /* Change By_Descriptor parameter to By_Reference for
4095 the internal version of an exported subprogram. */
4096 if (mech == By_Descriptor || mech == By_Short_Descriptor)
4098 gnu_param
4099 = gnat_to_gnu_param (gnat_param, By_Reference,
4100 gnat_entity, false,
4101 &copy_in_copy_out);
4102 has_stub = true;
4104 else
4105 gnu_param = copy_node (gnu_param);
4108 gnu_param_list = chainon (gnu_param, gnu_param_list);
4109 Sloc_to_locus (Sloc (gnat_param),
4110 &DECL_SOURCE_LOCATION (gnu_param));
4111 save_gnu_tree (gnat_param, gnu_param, false);
4113 /* If a parameter is a pointer, this function may modify
4114 memory through it and thus shouldn't be considered
4115 a const function. Also, the memory may be modified
4116 between two calls, so they can't be CSE'ed. The latter
4117 case also handles by-ref parameters. */
4118 if (POINTER_TYPE_P (gnu_param_type)
4119 || TYPE_IS_FAT_POINTER_P (gnu_param_type))
4120 const_flag = false;
4123 if (copy_in_copy_out)
4125 if (!has_copy_in_out)
4127 gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
4128 gnu_return_type = make_node (RECORD_TYPE);
4129 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4130 /* Set a default alignment to speed up accesses. */
4131 TYPE_ALIGN (gnu_return_type)
4132 = get_mode_alignment (ptr_mode);
4133 has_copy_in_out = true;
4136 gnu_field
4137 = create_field_decl (gnu_param_name, gnu_param_type,
4138 gnu_return_type, NULL_TREE, NULL_TREE,
4139 0, 0);
4140 Sloc_to_locus (Sloc (gnat_param),
4141 &DECL_SOURCE_LOCATION (gnu_field));
4142 TREE_CHAIN (gnu_field) = gnu_field_list;
4143 gnu_field_list = gnu_field;
4144 gnu_cico_list
4145 = tree_cons (gnu_field, gnu_param, gnu_cico_list);
4149 /* Do not compute record for out parameters if subprogram is
4150 stubbed since structures are incomplete for the back-end. */
4151 if (gnu_field_list && Convention (gnat_entity) != Convention_Stubbed)
4152 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4153 0, debug_info_p);
4155 /* If we have a CICO list but it has only one entry, we convert
4156 this function into a function that simply returns that one
4157 object. */
4158 if (list_length (gnu_cico_list) == 1)
4159 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_cico_list));
4161 if (Has_Stdcall_Convention (gnat_entity))
4162 prepend_one_attribute_to
4163 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4164 get_identifier ("stdcall"), NULL_TREE,
4165 gnat_entity);
4167 /* If we are on a target where stack realignment is needed for 'main'
4168 to honor GCC's implicit expectations (stack alignment greater than
4169 what the base ABI guarantees), ensure we do the same for foreign
4170 convention subprograms as they might be used as callbacks from code
4171 breaking such expectations. Note that this applies to task entry
4172 points in particular. */
4173 if (FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
4174 && Has_Foreign_Convention (gnat_entity))
4175 prepend_one_attribute_to
4176 (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4177 get_identifier ("force_align_arg_pointer"), NULL_TREE,
4178 gnat_entity);
4180 /* The lists have been built in reverse. */
4181 gnu_param_list = nreverse (gnu_param_list);
4182 if (has_stub)
4183 gnu_stub_param_list = nreverse (gnu_stub_param_list);
4184 gnu_cico_list = nreverse (gnu_cico_list);
4186 if (Ekind (gnat_entity) == E_Function)
4187 Set_Mechanism (gnat_entity, return_unconstrained_p
4188 || return_by_direct_ref_p
4189 || return_by_invisi_ref_p
4190 ? By_Reference : By_Copy);
4191 gnu_type
4192 = create_subprog_type (gnu_return_type, gnu_param_list,
4193 gnu_cico_list, return_unconstrained_p,
4194 return_by_direct_ref_p,
4195 return_by_invisi_ref_p);
4197 if (has_stub)
4198 gnu_stub_type
4199 = create_subprog_type (gnu_return_type, gnu_stub_param_list,
4200 gnu_cico_list, return_unconstrained_p,
4201 return_by_direct_ref_p,
4202 return_by_invisi_ref_p);
4204 /* A subprogram (something that doesn't return anything) shouldn't
4205 be considered const since there would be no reason for such a
4206 subprogram. Note that procedures with Out (or In Out) parameters
4207 have already been converted into a function with a return type. */
4208 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4209 const_flag = false;
4211 gnu_type
4212 = build_qualified_type (gnu_type,
4213 TYPE_QUALS (gnu_type)
4214 | (TYPE_QUAL_CONST * const_flag)
4215 | (TYPE_QUAL_VOLATILE * volatile_flag));
4217 if (has_stub)
4218 gnu_stub_type
4219 = build_qualified_type (gnu_stub_type,
4220 TYPE_QUALS (gnu_stub_type)
4221 | (TYPE_QUAL_CONST * const_flag)
4222 | (TYPE_QUAL_VOLATILE * volatile_flag));
4224 /* If we have a builtin decl for that function, use it. Check if the
4225 profiles are compatible and warn if they are not. The checker is
4226 expected to post extra diagnostics in this case. */
4227 if (gnu_builtin_decl)
4229 intrin_binding_t inb;
4231 inb.gnat_entity = gnat_entity;
4232 inb.ada_fntype = gnu_type;
4233 inb.btin_fntype = TREE_TYPE (gnu_builtin_decl);
4235 if (!intrin_profiles_compatible_p (&inb))
4236 post_error
4237 ("?profile of& doesn''t match the builtin it binds!",
4238 gnat_entity);
4240 gnu_decl = gnu_builtin_decl;
4241 gnu_type = TREE_TYPE (gnu_builtin_decl);
4242 break;
4245 /* If there was no specified Interface_Name and the external and
4246 internal names of the subprogram are the same, only use the
4247 internal name to allow disambiguation of nested subprograms. */
4248 if (No (Interface_Name (gnat_entity))
4249 && gnu_ext_name == gnu_entity_name)
4250 gnu_ext_name = NULL_TREE;
4252 /* If we are defining the subprogram and it has an Address clause
4253 we must get the address expression from the saved GCC tree for the
4254 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4255 the address expression here since the front-end has guaranteed
4256 in that case that the elaboration has no effects. If there is
4257 an Address clause and we are not defining the object, just
4258 make it a constant. */
4259 if (Present (Address_Clause (gnat_entity)))
4261 tree gnu_address = NULL_TREE;
4263 if (definition)
4264 gnu_address
4265 = (present_gnu_tree (gnat_entity)
4266 ? get_gnu_tree (gnat_entity)
4267 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4269 save_gnu_tree (gnat_entity, NULL_TREE, false);
4271 /* Convert the type of the object to a reference type that can
4272 alias everything as per 13.3(19). */
4273 gnu_type
4274 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4275 if (gnu_address)
4276 gnu_address = convert (gnu_type, gnu_address);
4278 gnu_decl
4279 = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
4280 gnu_address, false, Is_Public (gnat_entity),
4281 extern_flag, false, NULL, gnat_entity);
4282 DECL_BY_REF_P (gnu_decl) = 1;
4285 else if (kind == E_Subprogram_Type)
4286 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
4287 !Comes_From_Source (gnat_entity),
4288 debug_info_p, gnat_entity);
4289 else
4291 if (has_stub)
4293 gnu_stub_name = gnu_ext_name;
4294 gnu_ext_name = create_concat_name (gnat_entity, "internal");
4295 public_flag = false;
4298 gnu_decl = create_subprog_decl (gnu_entity_name, gnu_ext_name,
4299 gnu_type, gnu_param_list,
4300 inline_flag, public_flag,
4301 extern_flag, attr_list,
4302 gnat_entity);
4303 if (has_stub)
4305 tree gnu_stub_decl
4306 = create_subprog_decl (gnu_entity_name, gnu_stub_name,
4307 gnu_stub_type, gnu_stub_param_list,
4308 inline_flag, true,
4309 extern_flag, attr_list,
4310 gnat_entity);
4311 SET_DECL_FUNCTION_STUB (gnu_decl, gnu_stub_decl);
4314 /* This is unrelated to the stub built right above. */
4315 DECL_STUBBED_P (gnu_decl)
4316 = Convention (gnat_entity) == Convention_Stubbed;
4319 break;
4321 case E_Incomplete_Type:
4322 case E_Incomplete_Subtype:
4323 case E_Private_Type:
4324 case E_Private_Subtype:
4325 case E_Limited_Private_Type:
4326 case E_Limited_Private_Subtype:
4327 case E_Record_Type_With_Private:
4328 case E_Record_Subtype_With_Private:
4330 /* Get the "full view" of this entity. If this is an incomplete
4331 entity from a limited with, treat its non-limited view as the
4332 full view. Otherwise, use either the full view or the underlying
4333 full view, whichever is present. This is used in all the tests
4334 below. */
4335 Entity_Id full_view
4336 = (IN (Ekind (gnat_entity), Incomplete_Kind)
4337 && From_With_Type (gnat_entity))
4338 ? Non_Limited_View (gnat_entity)
4339 : Present (Full_View (gnat_entity))
4340 ? Full_View (gnat_entity)
4341 : Underlying_Full_View (gnat_entity);
4343 /* If this is an incomplete type with no full view, it must be a Taft
4344 Amendment type, in which case we return a dummy type. Otherwise,
4345 just get the type from its Etype. */
4346 if (No (full_view))
4348 if (kind == E_Incomplete_Type)
4350 gnu_type = make_dummy_type (gnat_entity);
4351 gnu_decl = TYPE_STUB_DECL (gnu_type);
4353 else
4355 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4356 NULL_TREE, 0);
4357 maybe_present = true;
4359 break;
4362 /* If we already made a type for the full view, reuse it. */
4363 else if (present_gnu_tree (full_view))
4365 gnu_decl = get_gnu_tree (full_view);
4366 break;
4369 /* Otherwise, if we are not defining the type now, get the type
4370 from the full view. But always get the type from the full view
4371 for define on use types, since otherwise we won't see them! */
4372 else if (!definition
4373 || (Is_Itype (full_view)
4374 && No (Freeze_Node (gnat_entity)))
4375 || (Is_Itype (gnat_entity)
4376 && No (Freeze_Node (full_view))))
4378 gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4379 maybe_present = true;
4380 break;
4383 /* For incomplete types, make a dummy type entry which will be
4384 replaced later. Save it as the full declaration's type so
4385 we can do any needed updates when we see it. */
4386 gnu_type = make_dummy_type (gnat_entity);
4387 gnu_decl = TYPE_STUB_DECL (gnu_type);
4388 save_gnu_tree (full_view, gnu_decl, 0);
4389 break;
4392 case E_Class_Wide_Type:
4393 /* Class-wide types are always transformed into their root type. */
4394 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4395 maybe_present = true;
4396 break;
4398 case E_Task_Type:
4399 case E_Task_Subtype:
4400 case E_Protected_Type:
4401 case E_Protected_Subtype:
4402 /* Concurrent types are always transformed into their record type. */
4403 if (type_annotate_only && No (gnat_equiv_type))
4404 gnu_type = void_type_node;
4405 else
4406 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4407 maybe_present = true;
4408 break;
4410 case E_Label:
4411 gnu_decl = create_label_decl (gnu_entity_name);
4412 break;
4414 case E_Block:
4415 case E_Loop:
4416 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4417 we've already saved it, so we don't try to. */
4418 gnu_decl = error_mark_node;
4419 saved = true;
4420 break;
4422 default:
4423 gcc_unreachable ();
4426 /* If we had a case where we evaluated another type and it might have
4427 defined this one, handle it here. */
4428 if (maybe_present && present_gnu_tree (gnat_entity))
4430 gnu_decl = get_gnu_tree (gnat_entity);
4431 saved = true;
4434 /* If we are processing a type and there is either no decl for it or
4435 we just made one, do some common processing for the type, such as
4436 handling alignment and possible padding. */
4437 if (is_type && (!gnu_decl || this_made_decl))
4439 /* Tell the middle-end that objects of tagged types are guaranteed to
4440 be properly aligned. This is necessary because conversions to the
4441 class-wide type are translated into conversions to the root type,
4442 which can be less aligned than some of its derived types. */
4443 if (Is_Tagged_Type (gnat_entity)
4444 || Is_Class_Wide_Equivalent_Type (gnat_entity))
4445 TYPE_ALIGN_OK (gnu_type) = 1;
4447 /* If the type is passed by reference, objects of this type must be
4448 fully addressable and cannot be copied. */
4449 if (Is_By_Reference_Type (gnat_entity))
4450 TREE_ADDRESSABLE (gnu_type) = 1;
4452 /* ??? Don't set the size for a String_Literal since it is either
4453 confirming or we don't handle it properly (if the low bound is
4454 non-constant). */
4455 if (!gnu_size && kind != E_String_Literal_Subtype)
4456 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
4457 TYPE_DECL, false,
4458 Has_Size_Clause (gnat_entity));
4460 /* If a size was specified, see if we can make a new type of that size
4461 by rearranging the type, for example from a fat to a thin pointer. */
4462 if (gnu_size)
4464 gnu_type
4465 = make_type_from_size (gnu_type, gnu_size,
4466 Has_Biased_Representation (gnat_entity));
4468 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4469 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4470 gnu_size = 0;
4473 /* If the alignment hasn't already been processed and this is
4474 not an unconstrained array, see if an alignment is specified.
4475 If not, we pick a default alignment for atomic objects. */
4476 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4478 else if (Known_Alignment (gnat_entity))
4480 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4481 TYPE_ALIGN (gnu_type));
4483 /* Warn on suspiciously large alignments. This should catch
4484 errors about the (alignment,byte)/(size,bit) discrepancy. */
4485 if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4487 tree size;
4489 /* If a size was specified, take it into account. Otherwise
4490 use the RM size for records as the type size has already
4491 been adjusted to the alignment. */
4492 if (gnu_size)
4493 size = gnu_size;
4494 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
4495 || TREE_CODE (gnu_type) == UNION_TYPE
4496 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
4497 && !TYPE_FAT_POINTER_P (gnu_type))
4498 size = rm_size (gnu_type);
4499 else
4500 size = TYPE_SIZE (gnu_type);
4502 /* Consider an alignment as suspicious if the alignment/size
4503 ratio is greater or equal to the byte/bit ratio. */
4504 if (host_integerp (size, 1)
4505 && align >= TREE_INT_CST_LOW (size) * BITS_PER_UNIT)
4506 post_error_ne ("?suspiciously large alignment specified for&",
4507 Expression (Alignment_Clause (gnat_entity)),
4508 gnat_entity);
4511 else if (Is_Atomic (gnat_entity) && !gnu_size
4512 && host_integerp (TYPE_SIZE (gnu_type), 1)
4513 && integer_pow2p (TYPE_SIZE (gnu_type)))
4514 align = MIN (BIGGEST_ALIGNMENT,
4515 tree_low_cst (TYPE_SIZE (gnu_type), 1));
4516 else if (Is_Atomic (gnat_entity) && gnu_size
4517 && host_integerp (gnu_size, 1)
4518 && integer_pow2p (gnu_size))
4519 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
4521 /* See if we need to pad the type. If we did, and made a record,
4522 the name of the new type may be changed. So get it back for
4523 us when we make the new TYPE_DECL below. */
4524 if (gnu_size || align > 0)
4525 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4526 false, !gnu_decl, definition, false);
4528 if (TYPE_IS_PADDING_P (gnu_type))
4530 gnu_entity_name = TYPE_NAME (gnu_type);
4531 if (TREE_CODE (gnu_entity_name) == TYPE_DECL)
4532 gnu_entity_name = DECL_NAME (gnu_entity_name);
4535 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4537 /* If we are at global level, GCC will have applied variable_size to
4538 the type, but that won't have done anything. So, if it's not
4539 a constant or self-referential, call elaborate_expression_1 to
4540 make a variable for the size rather than calculating it each time.
4541 Handle both the RM size and the actual size. */
4542 if (global_bindings_p ()
4543 && TYPE_SIZE (gnu_type)
4544 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4545 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4547 tree size = TYPE_SIZE (gnu_type);
4549 TYPE_SIZE (gnu_type)
4550 = elaborate_expression_1 (size, gnat_entity,
4551 get_identifier ("SIZE"),
4552 definition, false);
4554 /* ??? For now, store the size as a multiple of the alignment in
4555 bytes so that we can see the alignment from the tree. */
4556 TYPE_SIZE_UNIT (gnu_type)
4557 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type), gnat_entity,
4558 get_identifier ("SIZE_A_UNIT"),
4559 definition, false,
4560 TYPE_ALIGN (gnu_type));
4562 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4563 may not be marked by the call to create_type_decl below. */
4564 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type));
4566 if (TREE_CODE (gnu_type) == RECORD_TYPE)
4568 tree variant_part = get_variant_part (gnu_type);
4569 tree ada_size = TYPE_ADA_SIZE (gnu_type);
4571 if (variant_part)
4573 tree union_type = TREE_TYPE (variant_part);
4574 tree offset = DECL_FIELD_OFFSET (variant_part);
4576 /* If the position of the variant part is constant, subtract
4577 it from the size of the type of the parent to get the new
4578 size. This manual CSE reduces the data size. */
4579 if (TREE_CODE (offset) == INTEGER_CST)
4581 tree bitpos = DECL_FIELD_BIT_OFFSET (variant_part);
4582 TYPE_SIZE (union_type)
4583 = size_binop (MINUS_EXPR, TYPE_SIZE (gnu_type),
4584 bit_from_pos (offset, bitpos));
4585 TYPE_SIZE_UNIT (union_type)
4586 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (gnu_type),
4587 byte_from_pos (offset, bitpos));
4589 else
4591 TYPE_SIZE (union_type)
4592 = elaborate_expression_1 (TYPE_SIZE (union_type),
4593 gnat_entity,
4594 get_identifier ("VSIZE"),
4595 definition, false);
4597 /* ??? For now, store the size as a multiple of the
4598 alignment in bytes so that we can see the alignment
4599 from the tree. */
4600 TYPE_SIZE_UNIT (union_type)
4601 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type),
4602 gnat_entity,
4603 get_identifier
4604 ("VSIZE_A_UNIT"),
4605 definition, false,
4606 TYPE_ALIGN (union_type));
4608 /* ??? For now, store the offset as a multiple of the
4609 alignment in bytes so that we can see the alignment
4610 from the tree. */
4611 DECL_FIELD_OFFSET (variant_part)
4612 = elaborate_expression_2 (offset,
4613 gnat_entity,
4614 get_identifier ("VOFFSET"),
4615 definition, false,
4616 DECL_OFFSET_ALIGN
4617 (variant_part));
4620 DECL_SIZE (variant_part) = TYPE_SIZE (union_type);
4621 DECL_SIZE_UNIT (variant_part) = TYPE_SIZE_UNIT (union_type);
4624 if (operand_equal_p (ada_size, size, 0))
4625 ada_size = TYPE_SIZE (gnu_type);
4626 else
4627 ada_size
4628 = elaborate_expression_1 (ada_size, gnat_entity,
4629 get_identifier ("RM_SIZE"),
4630 definition, false);
4631 SET_TYPE_ADA_SIZE (gnu_type, ada_size);
4635 /* If this is a record type or subtype, call elaborate_expression_1 on
4636 any field position. Do this for both global and local types.
4637 Skip any fields that we haven't made trees for to avoid problems with
4638 class wide types. */
4639 if (IN (kind, Record_Kind))
4640 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4641 gnat_temp = Next_Entity (gnat_temp))
4642 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4644 tree gnu_field = get_gnu_tree (gnat_temp);
4646 /* ??? For now, store the offset as a multiple of the alignment
4647 in bytes so that we can see the alignment from the tree. */
4648 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4650 DECL_FIELD_OFFSET (gnu_field)
4651 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field),
4652 gnat_temp,
4653 get_identifier ("OFFSET"),
4654 definition, false,
4655 DECL_OFFSET_ALIGN (gnu_field));
4657 /* ??? The context of gnu_field is not necessarily gnu_type
4658 so the MULT_EXPR node built above may not be marked by
4659 the call to create_type_decl below. */
4660 if (global_bindings_p ())
4661 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field));
4665 if (Treat_As_Volatile (gnat_entity))
4666 gnu_type
4667 = build_qualified_type (gnu_type,
4668 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
4670 if (Is_Atomic (gnat_entity))
4671 check_ok_for_atomic (gnu_type, gnat_entity, false);
4673 if (Present (Alignment_Clause (gnat_entity)))
4674 TYPE_USER_ALIGN (gnu_type) = 1;
4676 if (Universal_Aliasing (gnat_entity))
4677 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
4679 if (!gnu_decl)
4680 gnu_decl = create_type_decl (gnu_entity_name, gnu_type, attr_list,
4681 !Comes_From_Source (gnat_entity),
4682 debug_info_p, gnat_entity);
4683 else
4685 TREE_TYPE (gnu_decl) = gnu_type;
4686 TYPE_STUB_DECL (gnu_type) = gnu_decl;
4690 if (is_type && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4692 gnu_type = TREE_TYPE (gnu_decl);
4694 /* If this is a derived type, relate its alias set to that of its parent
4695 to avoid troubles when a call to an inherited primitive is inlined in
4696 a context where a derived object is accessed. The inlined code works
4697 on the parent view so the resulting code may access the same object
4698 using both the parent and the derived alias sets, which thus have to
4699 conflict. As the same issue arises with component references, the
4700 parent alias set also has to conflict with composite types enclosing
4701 derived components. For instance, if we have:
4703 type D is new T;
4704 type R is record
4705 Component : D;
4706 end record;
4708 we want T to conflict with both D and R, in addition to R being a
4709 superset of D by record/component construction.
4711 One way to achieve this is to perform an alias set copy from the
4712 parent to the derived type. This is not quite appropriate, though,
4713 as we don't want separate derived types to conflict with each other:
4715 type I1 is new Integer;
4716 type I2 is new Integer;
4718 We want I1 and I2 to both conflict with Integer but we do not want
4719 I1 to conflict with I2, and an alias set copy on derivation would
4720 have that effect.
4722 The option chosen is to make the alias set of the derived type a
4723 superset of that of its parent type. It trivially fulfills the
4724 simple requirement for the Integer derivation example above, and
4725 the component case as well by superset transitivity:
4727 superset superset
4728 R ----------> D ----------> T
4730 However, for composite types, conversions between derived types are
4731 translated into VIEW_CONVERT_EXPRs so a sequence like:
4733 type Comp1 is new Comp;
4734 type Comp2 is new Comp;
4735 procedure Proc (C : Comp1);
4737 C : Comp2;
4738 Proc (Comp1 (C));
4740 is translated into:
4742 C : Comp2;
4743 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
4745 and gimplified into:
4747 C : Comp2;
4748 Comp1 *C.0;
4749 C.0 = (Comp1 *) &C;
4750 Proc (C.0);
4752 i.e. generates code involving type punning. Therefore, Comp1 needs
4753 to conflict with Comp2 and an alias set copy is required.
4755 The language rules ensure the parent type is already frozen here. */
4756 if (Is_Derived_Type (gnat_entity))
4758 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_entity));
4759 relate_alias_sets (gnu_type, gnu_parent_type,
4760 Is_Composite_Type (gnat_entity)
4761 ? ALIAS_SET_COPY : ALIAS_SET_SUPERSET);
4764 /* Back-annotate the Alignment of the type if not already in the
4765 tree. Likewise for sizes. */
4766 if (Unknown_Alignment (gnat_entity))
4768 unsigned int double_align, align;
4769 bool is_capped_double, align_clause;
4771 /* If the default alignment of "double" or larger scalar types is
4772 specifically capped and this is not an array with an alignment
4773 clause on the component type, return the cap. */
4774 if ((double_align = double_float_alignment) > 0)
4775 is_capped_double
4776 = is_double_float_or_array (gnat_entity, &align_clause);
4777 else if ((double_align = double_scalar_alignment) > 0)
4778 is_capped_double
4779 = is_double_scalar_or_array (gnat_entity, &align_clause);
4780 else
4781 is_capped_double = align_clause = false;
4783 if (is_capped_double && !align_clause)
4784 align = double_align;
4785 else
4786 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
4788 Set_Alignment (gnat_entity, UI_From_Int (align));
4791 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4793 tree gnu_size = TYPE_SIZE (gnu_type);
4795 /* If the size is self-referential, annotate the maximum value. */
4796 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4797 gnu_size = max_size (gnu_size, true);
4799 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4801 /* In this mode, the tag and the parent components are not
4802 generated by the front-end so the sizes must be adjusted. */
4803 tree pointer_size = bitsize_int (POINTER_SIZE), offset;
4804 Uint uint_size;
4806 if (Is_Derived_Type (gnat_entity))
4808 offset = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
4809 bitsizetype);
4810 Set_Alignment (gnat_entity,
4811 Alignment (Etype (Base_Type (gnat_entity))));
4813 else
4814 offset = pointer_size;
4816 gnu_size = size_binop (PLUS_EXPR, gnu_size, offset);
4817 gnu_size = size_binop (MULT_EXPR, pointer_size,
4818 size_binop (CEIL_DIV_EXPR,
4819 gnu_size,
4820 pointer_size));
4821 uint_size = annotate_value (gnu_size);
4822 Set_Esize (gnat_entity, uint_size);
4823 Set_RM_Size (gnat_entity, uint_size);
4825 else
4826 Set_Esize (gnat_entity, annotate_value (gnu_size));
4829 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4830 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4833 if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4834 DECL_ARTIFICIAL (gnu_decl) = 1;
4836 if (!debug_info_p && DECL_P (gnu_decl)
4837 && TREE_CODE (gnu_decl) != FUNCTION_DECL
4838 && No (Renamed_Object (gnat_entity)))
4839 DECL_IGNORED_P (gnu_decl) = 1;
4841 /* If we haven't already, associate the ..._DECL node that we just made with
4842 the input GNAT entity node. */
4843 if (!saved)
4844 save_gnu_tree (gnat_entity, gnu_decl, false);
4846 /* If this is an enumeration or floating-point type, we were not able to set
4847 the bounds since they refer to the type. These are always static. */
4848 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4849 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4851 tree gnu_scalar_type = gnu_type;
4852 tree gnu_low_bound, gnu_high_bound;
4854 /* If this is a padded type, we need to use the underlying type. */
4855 if (TYPE_IS_PADDING_P (gnu_scalar_type))
4856 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4858 /* If this is a floating point type and we haven't set a floating
4859 point type yet, use this in the evaluation of the bounds. */
4860 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4861 longest_float_type_node = gnu_scalar_type;
4863 gnu_low_bound = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4864 gnu_high_bound = gnat_to_gnu (Type_High_Bound (gnat_entity));
4866 if (kind == E_Enumeration_Type)
4868 /* Enumeration types have specific RM bounds. */
4869 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type, gnu_low_bound);
4870 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type, gnu_high_bound);
4872 /* Write full debugging information. Since this has both a
4873 typedef and a tag, avoid outputting the name twice. */
4874 DECL_ARTIFICIAL (gnu_decl) = 1;
4875 rest_of_type_decl_compilation (gnu_decl);
4878 else
4880 /* Floating-point types don't have specific RM bounds. */
4881 TYPE_GCC_MIN_VALUE (gnu_scalar_type) = gnu_low_bound;
4882 TYPE_GCC_MAX_VALUE (gnu_scalar_type) = gnu_high_bound;
4886 /* If we deferred processing of incomplete types, re-enable it. If there
4887 were no other disables and we have some to process, do so. */
4888 if (this_deferred && --defer_incomplete_level == 0)
4890 if (defer_incomplete_list)
4892 struct incomplete *incp, *next;
4894 /* We are back to level 0 for the deferring of incomplete types.
4895 But processing these incomplete types below may itself require
4896 deferring, so preserve what we have and restart from scratch. */
4897 incp = defer_incomplete_list;
4898 defer_incomplete_list = NULL;
4900 /* For finalization, however, all types must be complete so we
4901 cannot do the same because deferred incomplete types may end up
4902 referencing each other. Process them all recursively first. */
4903 defer_finalize_level++;
4905 for (; incp; incp = next)
4907 next = incp->next;
4909 if (incp->old_type)
4910 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4911 gnat_to_gnu_type (incp->full_type));
4912 free (incp);
4915 defer_finalize_level--;
4918 /* All the deferred incomplete types have been processed so we can
4919 now proceed with the finalization of the deferred types. */
4920 if (defer_finalize_level == 0 && defer_finalize_list)
4922 unsigned int i;
4923 tree t;
4925 for (i = 0; VEC_iterate (tree, defer_finalize_list, i, t); i++)
4926 rest_of_type_decl_compilation_no_defer (t);
4928 VEC_free (tree, heap, defer_finalize_list);
4932 /* If we are not defining this type, see if it's in the incomplete list.
4933 If so, handle that list entry now. */
4934 else if (!definition)
4936 struct incomplete *incp;
4938 for (incp = defer_incomplete_list; incp; incp = incp->next)
4939 if (incp->old_type && incp->full_type == gnat_entity)
4941 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4942 TREE_TYPE (gnu_decl));
4943 incp->old_type = NULL_TREE;
4947 if (this_global)
4948 force_global--;
4950 /* If this is a packed array type whose original array type is itself
4951 an Itype without freeze node, make sure the latter is processed. */
4952 if (Is_Packed_Array_Type (gnat_entity)
4953 && Is_Itype (Original_Array_Type (gnat_entity))
4954 && No (Freeze_Node (Original_Array_Type (gnat_entity)))
4955 && !present_gnu_tree (Original_Array_Type (gnat_entity)))
4956 gnat_to_gnu_entity (Original_Array_Type (gnat_entity), NULL_TREE, 0);
4958 return gnu_decl;
4961 /* Similar, but if the returned value is a COMPONENT_REF, return the
4962 FIELD_DECL. */
4964 tree
4965 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4967 tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4969 if (TREE_CODE (gnu_field) == COMPONENT_REF)
4970 gnu_field = TREE_OPERAND (gnu_field, 1);
4972 return gnu_field;
4975 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4976 the GCC type corresponding to that entity. */
4978 tree
4979 gnat_to_gnu_type (Entity_Id gnat_entity)
4981 tree gnu_decl;
4983 /* The back end never attempts to annotate generic types. */
4984 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
4985 return void_type_node;
4987 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4988 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
4990 return TREE_TYPE (gnu_decl);
4993 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
4994 the unpadded version of the GCC type corresponding to that entity. */
4996 tree
4997 get_unpadded_type (Entity_Id gnat_entity)
4999 tree type = gnat_to_gnu_type (gnat_entity);
5001 if (TYPE_IS_PADDING_P (type))
5002 type = TREE_TYPE (TYPE_FIELDS (type));
5004 return type;
5007 /* Wrap up compilation of DECL, a TYPE_DECL, possibly deferring it.
5008 Every TYPE_DECL generated for a type definition must be passed
5009 to this function once everything else has been done for it. */
5011 void
5012 rest_of_type_decl_compilation (tree decl)
5014 /* We need to defer finalizing the type if incomplete types
5015 are being deferred or if they are being processed. */
5016 if (defer_incomplete_level || defer_finalize_level)
5017 VEC_safe_push (tree, heap, defer_finalize_list, decl);
5018 else
5019 rest_of_type_decl_compilation_no_defer (decl);
5022 /* Same as above but without deferring the compilation. This
5023 function should not be invoked directly on a TYPE_DECL. */
5025 static void
5026 rest_of_type_decl_compilation_no_defer (tree decl)
5028 const int toplev = global_bindings_p ();
5029 tree t = TREE_TYPE (decl);
5031 rest_of_decl_compilation (decl, toplev, 0);
5033 /* Now process all the variants. This is needed for STABS. */
5034 for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
5036 if (t == TREE_TYPE (decl))
5037 continue;
5039 if (!TYPE_STUB_DECL (t))
5040 TYPE_STUB_DECL (t) = create_type_stub_decl (DECL_NAME (decl), t);
5042 rest_of_type_compilation (t, toplev);
5046 /* Finalize any From_With_Type incomplete types. We do this after processing
5047 our compilation unit and after processing its spec, if this is a body. */
5049 void
5050 finalize_from_with_types (void)
5052 struct incomplete *incp = defer_limited_with;
5053 struct incomplete *next;
5055 defer_limited_with = 0;
5056 for (; incp; incp = next)
5058 next = incp->next;
5060 if (incp->old_type != 0)
5061 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
5062 gnat_to_gnu_type (incp->full_type));
5063 free (incp);
5067 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5068 kind of type (such E_Task_Type) that has a different type which Gigi
5069 uses for its representation. If the type does not have a special type
5070 for its representation, return GNAT_ENTITY. If a type is supposed to
5071 exist, but does not, abort unless annotating types, in which case
5072 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5074 Entity_Id
5075 Gigi_Equivalent_Type (Entity_Id gnat_entity)
5077 Entity_Id gnat_equiv = gnat_entity;
5079 if (No (gnat_entity))
5080 return gnat_entity;
5082 switch (Ekind (gnat_entity))
5084 case E_Class_Wide_Subtype:
5085 if (Present (Equivalent_Type (gnat_entity)))
5086 gnat_equiv = Equivalent_Type (gnat_entity);
5087 break;
5089 case E_Access_Protected_Subprogram_Type:
5090 case E_Anonymous_Access_Protected_Subprogram_Type:
5091 gnat_equiv = Equivalent_Type (gnat_entity);
5092 break;
5094 case E_Class_Wide_Type:
5095 gnat_equiv = Root_Type (gnat_entity);
5096 break;
5098 case E_Task_Type:
5099 case E_Task_Subtype:
5100 case E_Protected_Type:
5101 case E_Protected_Subtype:
5102 gnat_equiv = Corresponding_Record_Type (gnat_entity);
5103 break;
5105 default:
5106 break;
5109 gcc_assert (Present (gnat_equiv) || type_annotate_only);
5110 return gnat_equiv;
5113 /* Return a GCC tree for a type corresponding to the component type of the
5114 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5115 is for an array being defined. DEBUG_INFO_P is true if we need to write
5116 debug information for other types that we may create in the process. */
5118 static tree
5119 gnat_to_gnu_component_type (Entity_Id gnat_array, bool definition,
5120 bool debug_info_p)
5122 tree gnu_type = gnat_to_gnu_type (Component_Type (gnat_array));
5123 tree gnu_comp_size;
5125 /* Try to get a smaller form of the component if needed. */
5126 if ((Is_Packed (gnat_array)
5127 || Has_Component_Size_Clause (gnat_array))
5128 && !Is_Bit_Packed_Array (gnat_array)
5129 && !Has_Aliased_Components (gnat_array)
5130 && !Strict_Alignment (Component_Type (gnat_array))
5131 && TREE_CODE (gnu_type) == RECORD_TYPE
5132 && !TYPE_FAT_POINTER_P (gnu_type)
5133 && host_integerp (TYPE_SIZE (gnu_type), 1))
5134 gnu_type = make_packable_type (gnu_type, false);
5136 if (Has_Atomic_Components (gnat_array))
5137 check_ok_for_atomic (gnu_type, gnat_array, true);
5139 /* Get and validate any specified Component_Size. */
5140 gnu_comp_size
5141 = validate_size (Component_Size (gnat_array), gnu_type, gnat_array,
5142 Is_Bit_Packed_Array (gnat_array) ? TYPE_DECL : VAR_DECL,
5143 true, Has_Component_Size_Clause (gnat_array));
5145 /* If the array has aliased components and the component size can be zero,
5146 force at least unit size to ensure that the components have distinct
5147 addresses. */
5148 if (!gnu_comp_size
5149 && Has_Aliased_Components (gnat_array)
5150 && (integer_zerop (TYPE_SIZE (gnu_type))
5151 || (TREE_CODE (gnu_type) == ARRAY_TYPE
5152 && !TREE_CONSTANT (TYPE_SIZE (gnu_type)))))
5153 gnu_comp_size
5154 = size_binop (MAX_EXPR, TYPE_SIZE (gnu_type), bitsize_unit_node);
5156 /* If the component type is a RECORD_TYPE that has a self-referential size,
5157 then use the maximum size for the component size. */
5158 if (!gnu_comp_size
5159 && TREE_CODE (gnu_type) == RECORD_TYPE
5160 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
5161 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
5163 /* Honor the component size. This is not needed for bit-packed arrays. */
5164 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_array))
5166 tree orig_type = gnu_type;
5167 unsigned int max_align;
5169 /* If an alignment is specified, use it as a cap on the component type
5170 so that it can be honored for the whole type. But ignore it for the
5171 original type of packed array types. */
5172 if (No (Packed_Array_Type (gnat_array)) && Known_Alignment (gnat_array))
5173 max_align = validate_alignment (Alignment (gnat_array), gnat_array, 0);
5174 else
5175 max_align = 0;
5177 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
5178 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
5179 gnu_type = orig_type;
5180 else
5181 orig_type = gnu_type;
5183 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0, gnat_array,
5184 true, false, definition, true);
5186 /* If a padding record was made, declare it now since it will never be
5187 declared otherwise. This is necessary to ensure that its subtrees
5188 are properly marked. */
5189 if (gnu_type != orig_type && !DECL_P (TYPE_NAME (gnu_type)))
5190 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL, true,
5191 debug_info_p, gnat_array);
5194 if (Has_Volatile_Components (Base_Type (gnat_array)))
5195 gnu_type
5196 = build_qualified_type (gnu_type,
5197 TYPE_QUALS (gnu_type) | TYPE_QUAL_VOLATILE);
5199 return gnu_type;
5202 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5203 using MECH as its passing mechanism, to be placed in the parameter
5204 list built for GNAT_SUBPROG. Assume a foreign convention for the
5205 latter if FOREIGN is true. Also set CICO to true if the parameter
5206 must use the copy-in copy-out implementation mechanism.
5208 The returned tree is a PARM_DECL, except for those cases where no
5209 parameter needs to be actually passed to the subprogram; the type
5210 of this "shadow" parameter is then returned instead. */
5212 static tree
5213 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
5214 Entity_Id gnat_subprog, bool foreign, bool *cico)
5216 tree gnu_param_name = get_entity_name (gnat_param);
5217 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
5218 tree gnu_param_type_alt = NULL_TREE;
5219 bool in_param = (Ekind (gnat_param) == E_In_Parameter);
5220 /* The parameter can be indirectly modified if its address is taken. */
5221 bool ro_param = in_param && !Address_Taken (gnat_param);
5222 bool by_return = false, by_component_ptr = false, by_ref = false;
5223 tree gnu_param;
5225 /* Copy-return is used only for the first parameter of a valued procedure.
5226 It's a copy mechanism for which a parameter is never allocated. */
5227 if (mech == By_Copy_Return)
5229 gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
5230 mech = By_Copy;
5231 by_return = true;
5234 /* If this is either a foreign function or if the underlying type won't
5235 be passed by reference, strip off possible padding type. */
5236 if (TYPE_IS_PADDING_P (gnu_param_type))
5238 tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
5240 if (mech == By_Reference
5241 || foreign
5242 || (!must_pass_by_ref (unpadded_type)
5243 && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
5244 gnu_param_type = unpadded_type;
5247 /* If this is a read-only parameter, make a variant of the type that is
5248 read-only. ??? However, if this is an unconstrained array, that type
5249 can be very complex, so skip it for now. Likewise for any other
5250 self-referential type. */
5251 if (ro_param
5252 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
5253 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
5254 gnu_param_type = build_qualified_type (gnu_param_type,
5255 (TYPE_QUALS (gnu_param_type)
5256 | TYPE_QUAL_CONST));
5258 /* For foreign conventions, pass arrays as pointers to the element type.
5259 First check for unconstrained array and get the underlying array. */
5260 if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
5261 gnu_param_type
5262 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
5264 /* For GCC builtins, pass Address integer types as (void *) */
5265 if (Convention (gnat_subprog) == Convention_Intrinsic
5266 && Present (Interface_Name (gnat_subprog))
5267 && Is_Descendent_Of_Address (Etype (gnat_param)))
5268 gnu_param_type = ptr_void_type_node;
5270 /* VMS descriptors are themselves passed by reference. */
5271 if (mech == By_Short_Descriptor ||
5272 (mech == By_Descriptor && TARGET_ABI_OPEN_VMS && !TARGET_MALLOC64))
5273 gnu_param_type
5274 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5275 Mechanism (gnat_param),
5276 gnat_subprog));
5277 else if (mech == By_Descriptor)
5279 /* Build both a 32-bit and 64-bit descriptor, one of which will be
5280 chosen in fill_vms_descriptor. */
5281 gnu_param_type_alt
5282 = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
5283 Mechanism (gnat_param),
5284 gnat_subprog));
5285 gnu_param_type
5286 = build_pointer_type (build_vms_descriptor (gnu_param_type,
5287 Mechanism (gnat_param),
5288 gnat_subprog));
5291 /* Arrays are passed as pointers to element type for foreign conventions. */
5292 else if (foreign
5293 && mech != By_Copy
5294 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
5296 /* Strip off any multi-dimensional entries, then strip
5297 off the last array to get the component type. */
5298 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
5299 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
5300 gnu_param_type = TREE_TYPE (gnu_param_type);
5302 by_component_ptr = true;
5303 gnu_param_type = TREE_TYPE (gnu_param_type);
5305 if (ro_param)
5306 gnu_param_type = build_qualified_type (gnu_param_type,
5307 (TYPE_QUALS (gnu_param_type)
5308 | TYPE_QUAL_CONST));
5310 gnu_param_type = build_pointer_type (gnu_param_type);
5313 /* Fat pointers are passed as thin pointers for foreign conventions. */
5314 else if (foreign && TYPE_IS_FAT_POINTER_P (gnu_param_type))
5315 gnu_param_type
5316 = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
5318 /* If we must pass or were requested to pass by reference, do so.
5319 If we were requested to pass by copy, do so.
5320 Otherwise, for foreign conventions, pass In Out or Out parameters
5321 or aggregates by reference. For COBOL and Fortran, pass all
5322 integer and FP types that way too. For Convention Ada, use
5323 the standard Ada default. */
5324 else if (must_pass_by_ref (gnu_param_type)
5325 || mech == By_Reference
5326 || (mech != By_Copy
5327 && ((foreign
5328 && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
5329 || (foreign
5330 && (Convention (gnat_subprog) == Convention_Fortran
5331 || Convention (gnat_subprog) == Convention_COBOL)
5332 && (INTEGRAL_TYPE_P (gnu_param_type)
5333 || FLOAT_TYPE_P (gnu_param_type)))
5334 || (!foreign
5335 && default_pass_by_ref (gnu_param_type)))))
5337 gnu_param_type = build_reference_type (gnu_param_type);
5338 by_ref = true;
5341 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5342 else if (!in_param)
5343 *cico = true;
5345 if (mech == By_Copy && (by_ref || by_component_ptr))
5346 post_error ("?cannot pass & by copy", gnat_param);
5348 /* If this is an Out parameter that isn't passed by reference and isn't
5349 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5350 it will be a VAR_DECL created when we process the procedure, so just
5351 return its type. For the special parameter of a valued procedure,
5352 never pass it in.
5354 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5355 Out parameters with discriminants or implicit initial values to be
5356 handled like In Out parameters. These type are normally built as
5357 aggregates, hence passed by reference, except for some packed arrays
5358 which end up encoded in special integer types.
5360 The exception we need to make is then for packed arrays of records
5361 with discriminants or implicit initial values. We have no light/easy
5362 way to check for the latter case, so we merely check for packed arrays
5363 of records. This may lead to useless copy-in operations, but in very
5364 rare cases only, as these would be exceptions in a set of already
5365 exceptional situations. */
5366 if (Ekind (gnat_param) == E_Out_Parameter
5367 && !by_ref
5368 && (by_return
5369 || (mech != By_Descriptor
5370 && mech != By_Short_Descriptor
5371 && !POINTER_TYPE_P (gnu_param_type)
5372 && !AGGREGATE_TYPE_P (gnu_param_type)))
5373 && !(Is_Array_Type (Etype (gnat_param))
5374 && Is_Packed (Etype (gnat_param))
5375 && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5376 return gnu_param_type;
5378 gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5379 ro_param || by_ref || by_component_ptr);
5380 DECL_BY_REF_P (gnu_param) = by_ref;
5381 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5382 DECL_BY_DESCRIPTOR_P (gnu_param) = (mech == By_Descriptor ||
5383 mech == By_Short_Descriptor);
5384 DECL_POINTS_TO_READONLY_P (gnu_param)
5385 = (ro_param && (by_ref || by_component_ptr));
5387 /* Save the alternate descriptor type, if any. */
5388 if (gnu_param_type_alt)
5389 SET_DECL_PARM_ALT_TYPE (gnu_param, gnu_param_type_alt);
5391 /* If no Mechanism was specified, indicate what we're using, then
5392 back-annotate it. */
5393 if (mech == Default)
5394 mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5396 Set_Mechanism (gnat_param, mech);
5397 return gnu_param;
5400 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5402 static bool
5403 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5405 while (Present (Corresponding_Discriminant (discr1)))
5406 discr1 = Corresponding_Discriminant (discr1);
5408 while (Present (Corresponding_Discriminant (discr2)))
5409 discr2 = Corresponding_Discriminant (discr2);
5411 return
5412 Original_Record_Component (discr1) == Original_Record_Component (discr2);
5415 /* Return true if the array type GNU_TYPE, which represents a dimension of
5416 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5418 static bool
5419 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5421 /* If the array type is not the innermost dimension of the GNAT type,
5422 then it has a non-aliased component. */
5423 if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5424 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5425 return true;
5427 /* If the array type has an aliased component in the front-end sense,
5428 then it also has an aliased component in the back-end sense. */
5429 if (Has_Aliased_Components (gnat_type))
5430 return false;
5432 /* If this is a derived type, then it has a non-aliased component if
5433 and only if its parent type also has one. */
5434 if (Is_Derived_Type (gnat_type))
5436 tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5437 int index;
5438 if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5439 gnu_parent_type
5440 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5441 for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5442 gnu_parent_type = TREE_TYPE (gnu_parent_type);
5443 return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5446 /* Otherwise, rely exclusively on properties of the element type. */
5447 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5450 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5452 static bool
5453 compile_time_known_address_p (Node_Id gnat_address)
5455 /* Catch System'To_Address. */
5456 if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5457 gnat_address = Expression (gnat_address);
5459 return Compile_Time_Known_Value (gnat_address);
5462 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5463 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5465 static bool
5466 cannot_be_superflat_p (Node_Id gnat_range)
5468 Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5469 Node_Id scalar_range;
5470 tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5472 /* If the low bound is not constant, try to find an upper bound. */
5473 while (Nkind (gnat_lb) != N_Integer_Literal
5474 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5475 || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5476 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5477 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5478 || Nkind (scalar_range) == N_Range))
5479 gnat_lb = High_Bound (scalar_range);
5481 /* If the high bound is not constant, try to find a lower bound. */
5482 while (Nkind (gnat_hb) != N_Integer_Literal
5483 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5484 || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5485 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5486 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5487 || Nkind (scalar_range) == N_Range))
5488 gnat_hb = Low_Bound (scalar_range);
5490 /* If we have failed to find constant bounds, punt. */
5491 if (Nkind (gnat_lb) != N_Integer_Literal
5492 || Nkind (gnat_hb) != N_Integer_Literal)
5493 return false;
5495 /* We need at least a signed 64-bit type to catch most cases. */
5496 gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5497 gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5498 if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5499 return false;
5501 /* If the low bound is the smallest integer, nothing can be smaller. */
5502 gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5503 if (TREE_OVERFLOW (gnu_lb_minus_one))
5504 return true;
5506 return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5509 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5511 static bool
5512 constructor_address_p (tree gnu_expr)
5514 while (TREE_CODE (gnu_expr) == NOP_EXPR
5515 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5516 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5517 gnu_expr = TREE_OPERAND (gnu_expr, 0);
5519 return (TREE_CODE (gnu_expr) == ADDR_EXPR
5520 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5523 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5524 be elaborated at the point of its definition, but do nothing else. */
5526 void
5527 elaborate_entity (Entity_Id gnat_entity)
5529 switch (Ekind (gnat_entity))
5531 case E_Signed_Integer_Subtype:
5532 case E_Modular_Integer_Subtype:
5533 case E_Enumeration_Subtype:
5534 case E_Ordinary_Fixed_Point_Subtype:
5535 case E_Decimal_Fixed_Point_Subtype:
5536 case E_Floating_Point_Subtype:
5538 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5539 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5541 /* ??? Tests to avoid Constraint_Error in static expressions
5542 are needed until after the front stops generating bogus
5543 conversions on bounds of real types. */
5544 if (!Raises_Constraint_Error (gnat_lb))
5545 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5546 true, false, Needs_Debug_Info (gnat_entity));
5547 if (!Raises_Constraint_Error (gnat_hb))
5548 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5549 true, false, Needs_Debug_Info (gnat_entity));
5550 break;
5553 case E_Record_Type:
5555 Node_Id full_definition = Declaration_Node (gnat_entity);
5556 Node_Id record_definition = Type_Definition (full_definition);
5558 /* If this is a record extension, go a level further to find the
5559 record definition. */
5560 if (Nkind (record_definition) == N_Derived_Type_Definition)
5561 record_definition = Record_Extension_Part (record_definition);
5563 break;
5565 case E_Record_Subtype:
5566 case E_Private_Subtype:
5567 case E_Limited_Private_Subtype:
5568 case E_Record_Subtype_With_Private:
5569 if (Is_Constrained (gnat_entity)
5570 && Has_Discriminants (gnat_entity)
5571 && Present (Discriminant_Constraint (gnat_entity)))
5573 Node_Id gnat_discriminant_expr;
5574 Entity_Id gnat_field;
5576 for (gnat_field
5577 = First_Discriminant (Implementation_Base_Type (gnat_entity)),
5578 gnat_discriminant_expr
5579 = First_Elmt (Discriminant_Constraint (gnat_entity));
5580 Present (gnat_field);
5581 gnat_field = Next_Discriminant (gnat_field),
5582 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
5583 /* ??? For now, ignore access discriminants. */
5584 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
5585 elaborate_expression (Node (gnat_discriminant_expr),
5586 gnat_entity, get_entity_name (gnat_field),
5587 true, false, false);
5589 break;
5594 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
5595 any entities on its entity chain similarly. */
5597 void
5598 mark_out_of_scope (Entity_Id gnat_entity)
5600 Entity_Id gnat_sub_entity;
5601 unsigned int kind = Ekind (gnat_entity);
5603 /* If this has an entity list, process all in the list. */
5604 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
5605 || IN (kind, Private_Kind)
5606 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
5607 || kind == E_Function || kind == E_Generic_Function
5608 || kind == E_Generic_Package || kind == E_Generic_Procedure
5609 || kind == E_Loop || kind == E_Operator || kind == E_Package
5610 || kind == E_Package_Body || kind == E_Procedure
5611 || kind == E_Record_Type || kind == E_Record_Subtype
5612 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
5613 for (gnat_sub_entity = First_Entity (gnat_entity);
5614 Present (gnat_sub_entity);
5615 gnat_sub_entity = Next_Entity (gnat_sub_entity))
5616 if (Scope (gnat_sub_entity) == gnat_entity
5617 && gnat_sub_entity != gnat_entity)
5618 mark_out_of_scope (gnat_sub_entity);
5620 /* Now clear this if it has been defined, but only do so if it isn't
5621 a subprogram or parameter. We could refine this, but it isn't
5622 worth it. If this is statically allocated, it is supposed to
5623 hang around out of cope. */
5624 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
5625 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
5627 save_gnu_tree (gnat_entity, NULL_TREE, true);
5628 save_gnu_tree (gnat_entity, error_mark_node, true);
5632 /* Relate the alias sets of GNU_NEW_TYPE and GNU_OLD_TYPE according to OP.
5633 If this is a multi-dimensional array type, do this recursively.
5635 OP may be
5636 - ALIAS_SET_COPY: the new set is made a copy of the old one.
5637 - ALIAS_SET_SUPERSET: the new set is made a superset of the old one.
5638 - ALIAS_SET_SUBSET: the new set is made a subset of the old one. */
5640 static void
5641 relate_alias_sets (tree gnu_new_type, tree gnu_old_type, enum alias_set_op op)
5643 /* Remove any padding from GNU_OLD_TYPE. It doesn't matter in the case
5644 of a one-dimensional array, since the padding has the same alias set
5645 as the field type, but if it's a multi-dimensional array, we need to
5646 see the inner types. */
5647 while (TREE_CODE (gnu_old_type) == RECORD_TYPE
5648 && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
5649 || TYPE_PADDING_P (gnu_old_type)))
5650 gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
5652 /* Unconstrained array types are deemed incomplete and would thus be given
5653 alias set 0. Retrieve the underlying array type. */
5654 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
5655 gnu_old_type
5656 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
5657 if (TREE_CODE (gnu_new_type) == UNCONSTRAINED_ARRAY_TYPE)
5658 gnu_new_type
5659 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_new_type))));
5661 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
5662 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
5663 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
5664 relate_alias_sets (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type), op);
5666 switch (op)
5668 case ALIAS_SET_COPY:
5669 /* The alias set shouldn't be copied between array types with different
5670 aliasing settings because this can break the aliasing relationship
5671 between the array type and its element type. */
5672 #ifndef ENABLE_CHECKING
5673 if (flag_strict_aliasing)
5674 #endif
5675 gcc_assert (!(TREE_CODE (gnu_new_type) == ARRAY_TYPE
5676 && TREE_CODE (gnu_old_type) == ARRAY_TYPE
5677 && TYPE_NONALIASED_COMPONENT (gnu_new_type)
5678 != TYPE_NONALIASED_COMPONENT (gnu_old_type)));
5680 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
5681 break;
5683 case ALIAS_SET_SUBSET:
5684 case ALIAS_SET_SUPERSET:
5686 alias_set_type old_set = get_alias_set (gnu_old_type);
5687 alias_set_type new_set = get_alias_set (gnu_new_type);
5689 /* Do nothing if the alias sets conflict. This ensures that we
5690 never call record_alias_subset several times for the same pair
5691 or at all for alias set 0. */
5692 if (!alias_sets_conflict_p (old_set, new_set))
5694 if (op == ALIAS_SET_SUBSET)
5695 record_alias_subset (old_set, new_set);
5696 else
5697 record_alias_subset (new_set, old_set);
5700 break;
5702 default:
5703 gcc_unreachable ();
5706 record_component_aliases (gnu_new_type);
5709 /* Return true if the size represented by GNU_SIZE can be handled by an
5710 allocation. If STATIC_P is true, consider only what can be done with a
5711 static allocation. */
5713 static bool
5714 allocatable_size_p (tree gnu_size, bool static_p)
5716 HOST_WIDE_INT our_size;
5718 /* If this is not a static allocation, the only case we want to forbid
5719 is an overflowing size. That will be converted into a raise a
5720 Storage_Error. */
5721 if (!static_p)
5722 return !(TREE_CODE (gnu_size) == INTEGER_CST
5723 && TREE_OVERFLOW (gnu_size));
5725 /* Otherwise, we need to deal with both variable sizes and constant
5726 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
5727 since assemblers may not like very large sizes. */
5728 if (!host_integerp (gnu_size, 1))
5729 return false;
5731 our_size = tree_low_cst (gnu_size, 1);
5732 return (int) our_size == our_size;
5735 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5736 NAME, ARGS and ERROR_POINT. */
5738 static void
5739 prepend_one_attribute_to (struct attrib ** attr_list,
5740 enum attr_type attr_type,
5741 tree attr_name,
5742 tree attr_args,
5743 Node_Id attr_error_point)
5745 struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
5747 attr->type = attr_type;
5748 attr->name = attr_name;
5749 attr->args = attr_args;
5750 attr->error_point = attr_error_point;
5752 attr->next = *attr_list;
5753 *attr_list = attr;
5756 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
5758 static void
5759 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
5761 Node_Id gnat_temp;
5763 /* Attributes are stored as Representation Item pragmas. */
5765 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
5766 gnat_temp = Next_Rep_Item (gnat_temp))
5767 if (Nkind (gnat_temp) == N_Pragma)
5769 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
5770 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
5771 enum attr_type etype;
5773 /* Map the kind of pragma at hand. Skip if this is not one
5774 we know how to handle. */
5776 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_temp))))
5778 case Pragma_Machine_Attribute:
5779 etype = ATTR_MACHINE_ATTRIBUTE;
5780 break;
5782 case Pragma_Linker_Alias:
5783 etype = ATTR_LINK_ALIAS;
5784 break;
5786 case Pragma_Linker_Section:
5787 etype = ATTR_LINK_SECTION;
5788 break;
5790 case Pragma_Linker_Constructor:
5791 etype = ATTR_LINK_CONSTRUCTOR;
5792 break;
5794 case Pragma_Linker_Destructor:
5795 etype = ATTR_LINK_DESTRUCTOR;
5796 break;
5798 case Pragma_Weak_External:
5799 etype = ATTR_WEAK_EXTERNAL;
5800 break;
5802 case Pragma_Thread_Local_Storage:
5803 etype = ATTR_THREAD_LOCAL_STORAGE;
5804 break;
5806 default:
5807 continue;
5810 /* See what arguments we have and turn them into GCC trees for
5811 attribute handlers. These expect identifier for strings. We
5812 handle at most two arguments, static expressions only. */
5814 if (Present (gnat_assoc) && Present (First (gnat_assoc)))
5816 Node_Id gnat_arg0 = Next (First (gnat_assoc));
5817 Node_Id gnat_arg1 = Empty;
5819 if (Present (gnat_arg0)
5820 && Is_Static_Expression (Expression (gnat_arg0)))
5822 gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
5824 if (TREE_CODE (gnu_arg0) == STRING_CST)
5825 gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
5827 gnat_arg1 = Next (gnat_arg0);
5830 if (Present (gnat_arg1)
5831 && Is_Static_Expression (Expression (gnat_arg1)))
5833 gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
5835 if (TREE_CODE (gnu_arg1) == STRING_CST)
5836 gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
5840 /* Prepend to the list now. Make a list of the argument we might
5841 have, as GCC expects it. */
5842 prepend_one_attribute_to
5843 (attr_list,
5844 etype, gnu_arg0,
5845 (gnu_arg1 != NULL_TREE)
5846 ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
5847 Present (Next (First (gnat_assoc)))
5848 ? Expression (Next (First (gnat_assoc))) : gnat_temp);
5852 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
5853 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
5854 return the GCC tree to use for that expression. GNU_NAME is the suffix
5855 to use if a variable needs to be created and DEFINITION is true if this
5856 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
5857 otherwise, we are just elaborating the expression for side-effects. If
5858 NEED_DEBUG is true, we need a variable for debugging purposes even if it
5859 isn't needed for code generation. */
5861 static tree
5862 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
5863 bool definition, bool need_value, bool need_debug)
5865 tree gnu_expr;
5867 /* If we already elaborated this expression (e.g. it was involved
5868 in the definition of a private type), use the old value. */
5869 if (present_gnu_tree (gnat_expr))
5870 return get_gnu_tree (gnat_expr);
5872 /* If we don't need a value and this is static or a discriminant,
5873 we don't need to do anything. */
5874 if (!need_value
5875 && (Is_OK_Static_Expression (gnat_expr)
5876 || (Nkind (gnat_expr) == N_Identifier
5877 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
5878 return NULL_TREE;
5880 /* If it's a static expression, we don't need a variable for debugging. */
5881 if (need_debug && Is_OK_Static_Expression (gnat_expr))
5882 need_debug = false;
5884 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
5885 gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
5886 gnu_name, definition, need_debug);
5888 /* Save the expression in case we try to elaborate this entity again. Since
5889 it's not a DECL, don't check it. Don't save if it's a discriminant. */
5890 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
5891 save_gnu_tree (gnat_expr, gnu_expr, true);
5893 return need_value ? gnu_expr : error_mark_node;
5896 /* Similar, but take a GNU expression and always return a result. */
5898 static tree
5899 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
5900 bool definition, bool need_debug)
5902 /* Skip any conversions and simple arithmetics to see if the expression
5903 is a read-only variable.
5904 ??? This really should remain read-only, but we have to think about
5905 the typing of the tree here. */
5906 tree gnu_inner_expr
5907 = skip_simple_arithmetic (remove_conversions (gnu_expr, true));
5908 tree gnu_decl = NULL_TREE;
5909 bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
5910 bool expr_variable;
5912 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
5913 reference will have been replaced with a COMPONENT_REF when the type
5914 is being elaborated. However, there are some cases involving child
5915 types where we will. So convert it to a COMPONENT_REF. We hope it
5916 will be at the highest level of the expression in these cases. */
5917 if (TREE_CODE (gnu_expr) == FIELD_DECL)
5918 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
5919 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
5920 gnu_expr, NULL_TREE);
5922 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
5923 that is read-only, make a variable that is initialized to contain the
5924 bound when the package containing the definition is elaborated. If
5925 this entity is defined at top level and a bound or discriminant value
5926 isn't a constant or a reference to a discriminant, replace the bound
5927 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
5928 rely here on the fact that an expression cannot contain both the
5929 discriminant and some other variable. */
5930 expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
5931 && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
5932 && (TREE_READONLY (gnu_inner_expr)
5933 || DECL_READONLY_ONCE_ELAB (gnu_inner_expr)))
5934 && !CONTAINS_PLACEHOLDER_P (gnu_expr));
5936 /* If GNU_EXPR contains a discriminant, we can't elaborate a variable. */
5937 if (need_debug && CONTAINS_PLACEHOLDER_P (gnu_expr))
5938 need_debug = false;
5940 /* Now create the variable if we need it. */
5941 if (need_debug || (expr_variable && expr_global))
5942 gnu_decl
5943 = create_var_decl (create_concat_name (gnat_entity,
5944 IDENTIFIER_POINTER (gnu_name)),
5945 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
5946 !need_debug, Is_Public (gnat_entity),
5947 !definition, false, NULL, gnat_entity);
5949 /* We only need to use this variable if we are in global context since GCC
5950 can do the right thing in the local case. */
5951 if (expr_global && expr_variable)
5952 return gnu_decl;
5954 return expr_variable ? gnat_save_expr (gnu_expr) : gnu_expr;
5957 /* Similar, but take an alignment factor and make it explicit in the tree. */
5959 static tree
5960 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
5961 bool definition, bool need_debug, unsigned int align)
5963 tree unit_align = size_int (align / BITS_PER_UNIT);
5964 return
5965 size_binop (MULT_EXPR,
5966 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
5967 gnu_expr,
5968 unit_align),
5969 gnat_entity, gnu_name, definition,
5970 need_debug),
5971 unit_align);
5974 /* Create a record type that contains a SIZE bytes long field of TYPE with a
5975 starting bit position so that it is aligned to ALIGN bits, and leaving at
5976 least ROOM bytes free before the field. BASE_ALIGN is the alignment the
5977 record is guaranteed to get. */
5979 tree
5980 make_aligning_type (tree type, unsigned int align, tree size,
5981 unsigned int base_align, int room)
5983 /* We will be crafting a record type with one field at a position set to be
5984 the next multiple of ALIGN past record'address + room bytes. We use a
5985 record placeholder to express record'address. */
5986 tree record_type = make_node (RECORD_TYPE);
5987 tree record = build0 (PLACEHOLDER_EXPR, record_type);
5989 tree record_addr_st
5990 = convert (sizetype, build_unary_op (ADDR_EXPR, NULL_TREE, record));
5992 /* The diagram below summarizes the shape of what we manipulate:
5994 <--------- pos ---------->
5995 { +------------+-------------+-----------------+
5996 record =>{ |############| ... | field (type) |
5997 { +------------+-------------+-----------------+
5998 |<-- room -->|<- voffset ->|<---- size ----->|
6001 record_addr vblock_addr
6003 Every length is in sizetype bytes there, except "pos" which has to be
6004 set as a bit position in the GCC tree for the record. */
6005 tree room_st = size_int (room);
6006 tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
6007 tree voffset_st, pos, field;
6009 tree name = TYPE_NAME (type);
6011 if (TREE_CODE (name) == TYPE_DECL)
6012 name = DECL_NAME (name);
6013 name = concat_name (name, "ALIGN");
6014 TYPE_NAME (record_type) = name;
6016 /* Compute VOFFSET and then POS. The next byte position multiple of some
6017 alignment after some address is obtained by "and"ing the alignment minus
6018 1 with the two's complement of the address. */
6019 voffset_st = size_binop (BIT_AND_EXPR,
6020 fold_build1 (NEGATE_EXPR, sizetype, vblock_addr_st),
6021 size_int ((align / BITS_PER_UNIT) - 1));
6023 /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype. */
6024 pos = size_binop (MULT_EXPR,
6025 convert (bitsizetype,
6026 size_binop (PLUS_EXPR, room_st, voffset_st)),
6027 bitsize_unit_node);
6029 /* Craft the GCC record representation. We exceptionally do everything
6030 manually here because 1) our generic circuitry is not quite ready to
6031 handle the complex position/size expressions we are setting up, 2) we
6032 have a strong simplifying factor at hand: we know the maximum possible
6033 value of voffset, and 3) we have to set/reset at least the sizes in
6034 accordance with this maximum value anyway, as we need them to convey
6035 what should be "alloc"ated for this type.
6037 Use -1 as the 'addressable' indication for the field to prevent the
6038 creation of a bitfield. We don't need one, it would have damaging
6039 consequences on the alignment computation, and create_field_decl would
6040 make one without this special argument, for instance because of the
6041 complex position expression. */
6042 field = create_field_decl (get_identifier ("F"), type, record_type, size,
6043 pos, 1, -1);
6044 TYPE_FIELDS (record_type) = field;
6046 TYPE_ALIGN (record_type) = base_align;
6047 TYPE_USER_ALIGN (record_type) = 1;
6049 TYPE_SIZE (record_type)
6050 = size_binop (PLUS_EXPR,
6051 size_binop (MULT_EXPR, convert (bitsizetype, size),
6052 bitsize_unit_node),
6053 bitsize_int (align + room * BITS_PER_UNIT));
6054 TYPE_SIZE_UNIT (record_type)
6055 = size_binop (PLUS_EXPR, size,
6056 size_int (room + align / BITS_PER_UNIT));
6058 SET_TYPE_MODE (record_type, BLKmode);
6059 relate_alias_sets (record_type, type, ALIAS_SET_COPY);
6061 /* Declare it now since it will never be declared otherwise. This is
6062 necessary to ensure that its subtrees are properly marked. */
6063 create_type_decl (name, record_type, NULL, true, false, Empty);
6065 return record_type;
6068 /* Return the result of rounding T up to ALIGN. */
6070 static inline unsigned HOST_WIDE_INT
6071 round_up_to_align (unsigned HOST_WIDE_INT t, unsigned int align)
6073 t += align - 1;
6074 t /= align;
6075 t *= align;
6076 return t;
6079 /* TYPE is a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE that is being used
6080 as the field type of a packed record if IN_RECORD is true, or as the
6081 component type of a packed array if IN_RECORD is false. See if we can
6082 rewrite it either as a type that has a non-BLKmode, which we can pack
6083 tighter in the packed record case, or as a smaller type. If so, return
6084 the new type. If not, return the original type. */
6086 static tree
6087 make_packable_type (tree type, bool in_record)
6089 unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
6090 unsigned HOST_WIDE_INT new_size;
6091 tree new_type, old_field, field_list = NULL_TREE;
6093 /* No point in doing anything if the size is zero. */
6094 if (size == 0)
6095 return type;
6097 new_type = make_node (TREE_CODE (type));
6099 /* Copy the name and flags from the old type to that of the new.
6100 Note that we rely on the pointer equality created here for
6101 TYPE_NAME to look through conversions in various places. */
6102 TYPE_NAME (new_type) = TYPE_NAME (type);
6103 TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
6104 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
6105 if (TREE_CODE (type) == RECORD_TYPE)
6106 TYPE_PADDING_P (new_type) = TYPE_PADDING_P (type);
6108 /* If we are in a record and have a small size, set the alignment to
6109 try for an integral mode. Otherwise set it to try for a smaller
6110 type with BLKmode. */
6111 if (in_record && size <= MAX_FIXED_MODE_SIZE)
6113 TYPE_ALIGN (new_type) = ceil_alignment (size);
6114 new_size = round_up_to_align (size, TYPE_ALIGN (new_type));
6116 else
6118 unsigned HOST_WIDE_INT align;
6120 /* Do not try to shrink the size if the RM size is not constant. */
6121 if (TYPE_CONTAINS_TEMPLATE_P (type)
6122 || !host_integerp (TYPE_ADA_SIZE (type), 1))
6123 return type;
6125 /* Round the RM size up to a unit boundary to get the minimal size
6126 for a BLKmode record. Give up if it's already the size. */
6127 new_size = TREE_INT_CST_LOW (TYPE_ADA_SIZE (type));
6128 new_size = round_up_to_align (new_size, BITS_PER_UNIT);
6129 if (new_size == size)
6130 return type;
6132 align = new_size & -new_size;
6133 TYPE_ALIGN (new_type) = MIN (TYPE_ALIGN (type), align);
6136 TYPE_USER_ALIGN (new_type) = 1;
6138 /* Now copy the fields, keeping the position and size as we don't want
6139 to change the layout by propagating the packedness downwards. */
6140 for (old_field = TYPE_FIELDS (type); old_field;
6141 old_field = TREE_CHAIN (old_field))
6143 tree new_field_type = TREE_TYPE (old_field);
6144 tree new_field, new_size;
6146 if ((TREE_CODE (new_field_type) == RECORD_TYPE
6147 || TREE_CODE (new_field_type) == UNION_TYPE
6148 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
6149 && !TYPE_FAT_POINTER_P (new_field_type)
6150 && host_integerp (TYPE_SIZE (new_field_type), 1))
6151 new_field_type = make_packable_type (new_field_type, true);
6153 /* However, for the last field in a not already packed record type
6154 that is of an aggregate type, we need to use the RM size in the
6155 packable version of the record type, see finish_record_type. */
6156 if (!TREE_CHAIN (old_field)
6157 && !TYPE_PACKED (type)
6158 && (TREE_CODE (new_field_type) == RECORD_TYPE
6159 || TREE_CODE (new_field_type) == UNION_TYPE
6160 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
6161 && !TYPE_FAT_POINTER_P (new_field_type)
6162 && !TYPE_CONTAINS_TEMPLATE_P (new_field_type)
6163 && TYPE_ADA_SIZE (new_field_type))
6164 new_size = TYPE_ADA_SIZE (new_field_type);
6165 else
6166 new_size = DECL_SIZE (old_field);
6168 new_field
6169 = create_field_decl (DECL_NAME (old_field), new_field_type, new_type,
6170 new_size, bit_position (old_field),
6171 TYPE_PACKED (type),
6172 !DECL_NONADDRESSABLE_P (old_field));
6174 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
6175 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
6176 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
6177 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
6179 TREE_CHAIN (new_field) = field_list;
6180 field_list = new_field;
6183 finish_record_type (new_type, nreverse (field_list), 2, false);
6184 relate_alias_sets (new_type, type, ALIAS_SET_COPY);
6186 /* If this is a padding record, we never want to make the size smaller
6187 than what was specified. For QUAL_UNION_TYPE, also copy the size. */
6188 if (TYPE_IS_PADDING_P (type) || TREE_CODE (type) == QUAL_UNION_TYPE)
6190 TYPE_SIZE (new_type) = TYPE_SIZE (type);
6191 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
6192 new_size = size;
6194 else
6196 TYPE_SIZE (new_type) = bitsize_int (new_size);
6197 TYPE_SIZE_UNIT (new_type)
6198 = size_int ((new_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
6201 if (!TYPE_CONTAINS_TEMPLATE_P (type))
6202 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (type));
6204 compute_record_mode (new_type);
6206 /* Try harder to get a packable type if necessary, for example
6207 in case the record itself contains a BLKmode field. */
6208 if (in_record && TYPE_MODE (new_type) == BLKmode)
6209 SET_TYPE_MODE (new_type,
6210 mode_for_size_tree (TYPE_SIZE (new_type), MODE_INT, 1));
6212 /* If neither the mode nor the size has shrunk, return the old type. */
6213 if (TYPE_MODE (new_type) == BLKmode && new_size >= size)
6214 return type;
6216 return new_type;
6219 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
6220 if needed. We have already verified that SIZE and TYPE are large enough.
6221 GNAT_ENTITY is used to name the resulting record and to issue a warning.
6222 IS_COMPONENT_TYPE is true if this is being done for the component type
6223 of an array. IS_USER_TYPE is true if we must complete the original type.
6224 DEFINITION is true if this type is being defined. SAME_RM_SIZE is true
6225 if the RM size of the resulting type is to be set to SIZE too; otherwise,
6226 it's set to the RM size of the original type. */
6228 tree
6229 maybe_pad_type (tree type, tree size, unsigned int align,
6230 Entity_Id gnat_entity, bool is_component_type,
6231 bool is_user_type, bool definition, bool same_rm_size)
6233 tree orig_rm_size = same_rm_size ? NULL_TREE : rm_size (type);
6234 tree orig_size = TYPE_SIZE (type);
6235 tree record, field;
6237 /* If TYPE is a padded type, see if it agrees with any size and alignment
6238 we were given. If so, return the original type. Otherwise, strip
6239 off the padding, since we will either be returning the inner type
6240 or repadding it. If no size or alignment is specified, use that of
6241 the original padded type. */
6242 if (TYPE_IS_PADDING_P (type))
6244 if ((!size
6245 || operand_equal_p (round_up (size,
6246 MAX (align, TYPE_ALIGN (type))),
6247 round_up (TYPE_SIZE (type),
6248 MAX (align, TYPE_ALIGN (type))),
6250 && (align == 0 || align == TYPE_ALIGN (type)))
6251 return type;
6253 if (!size)
6254 size = TYPE_SIZE (type);
6255 if (align == 0)
6256 align = TYPE_ALIGN (type);
6258 type = TREE_TYPE (TYPE_FIELDS (type));
6259 orig_size = TYPE_SIZE (type);
6262 /* If the size is either not being changed or is being made smaller (which
6263 is not done here and is only valid for bitfields anyway), show the size
6264 isn't changing. Likewise, clear the alignment if it isn't being
6265 changed. Then return if we aren't doing anything. */
6266 if (size
6267 && (operand_equal_p (size, orig_size, 0)
6268 || (TREE_CODE (orig_size) == INTEGER_CST
6269 && tree_int_cst_lt (size, orig_size))))
6270 size = NULL_TREE;
6272 if (align == TYPE_ALIGN (type))
6273 align = 0;
6275 if (align == 0 && !size)
6276 return type;
6278 /* If requested, complete the original type and give it a name. */
6279 if (is_user_type)
6280 create_type_decl (get_entity_name (gnat_entity), type,
6281 NULL, !Comes_From_Source (gnat_entity),
6282 !(TYPE_NAME (type)
6283 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6284 && DECL_IGNORED_P (TYPE_NAME (type))),
6285 gnat_entity);
6287 /* We used to modify the record in place in some cases, but that could
6288 generate incorrect debugging information. So make a new record
6289 type and name. */
6290 record = make_node (RECORD_TYPE);
6291 TYPE_PADDING_P (record) = 1;
6293 if (Present (gnat_entity))
6294 TYPE_NAME (record) = create_concat_name (gnat_entity, "PAD");
6296 TYPE_VOLATILE (record)
6297 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
6299 TYPE_ALIGN (record) = align;
6300 TYPE_SIZE (record) = size ? size : orig_size;
6301 TYPE_SIZE_UNIT (record)
6302 = convert (sizetype,
6303 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
6304 bitsize_unit_node));
6306 /* If we are changing the alignment and the input type is a record with
6307 BLKmode and a small constant size, try to make a form that has an
6308 integral mode. This might allow the padding record to also have an
6309 integral mode, which will be much more efficient. There is no point
6310 in doing so if a size is specified unless it is also a small constant
6311 size and it is incorrect to do so if we cannot guarantee that the mode
6312 will be naturally aligned since the field must always be addressable.
6314 ??? This might not always be a win when done for a stand-alone object:
6315 since the nominal and the effective type of the object will now have
6316 different modes, a VIEW_CONVERT_EXPR will be required for converting
6317 between them and it might be hard to overcome afterwards, including
6318 at the RTL level when the stand-alone object is accessed as a whole. */
6319 if (align != 0
6320 && TREE_CODE (type) == RECORD_TYPE
6321 && TYPE_MODE (type) == BLKmode
6322 && TREE_CODE (orig_size) == INTEGER_CST
6323 && !TREE_OVERFLOW (orig_size)
6324 && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
6325 && (!size
6326 || (TREE_CODE (size) == INTEGER_CST
6327 && compare_tree_int (size, MAX_FIXED_MODE_SIZE) <= 0)))
6329 tree packable_type = make_packable_type (type, true);
6330 if (TYPE_MODE (packable_type) != BLKmode
6331 && align >= TYPE_ALIGN (packable_type))
6332 type = packable_type;
6335 /* Now create the field with the original size. */
6336 field = create_field_decl (get_identifier ("F"), type, record, orig_size,
6337 bitsize_zero_node, 0, 1);
6338 DECL_INTERNAL_P (field) = 1;
6340 /* Do not emit debug info until after the auxiliary record is built. */
6341 finish_record_type (record, field, 1, false);
6343 /* Set the same size for its RM size if requested; otherwise reuse
6344 the RM size of the original type. */
6345 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : orig_rm_size);
6347 /* Unless debugging information isn't being written for the input type,
6348 write a record that shows what we are a subtype of and also make a
6349 variable that indicates our size, if still variable. */
6350 if (TREE_CODE (orig_size) != INTEGER_CST
6351 && TYPE_NAME (record)
6352 && TYPE_NAME (type)
6353 && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6354 && DECL_IGNORED_P (TYPE_NAME (type))))
6356 tree marker = make_node (RECORD_TYPE);
6357 tree name = TYPE_NAME (record);
6358 tree orig_name = TYPE_NAME (type);
6360 if (TREE_CODE (name) == TYPE_DECL)
6361 name = DECL_NAME (name);
6363 if (TREE_CODE (orig_name) == TYPE_DECL)
6364 orig_name = DECL_NAME (orig_name);
6366 TYPE_NAME (marker) = concat_name (name, "XVS");
6367 finish_record_type (marker,
6368 create_field_decl (orig_name,
6369 build_reference_type (type),
6370 marker, NULL_TREE, NULL_TREE,
6371 0, 0),
6372 0, true);
6374 add_parallel_type (TYPE_STUB_DECL (record), marker);
6376 if (definition && size && TREE_CODE (size) != INTEGER_CST)
6377 TYPE_SIZE_UNIT (marker)
6378 = create_var_decl (concat_name (name, "XVZ"), NULL_TREE, sizetype,
6379 TYPE_SIZE_UNIT (record), false, false, false,
6380 false, NULL, gnat_entity);
6383 rest_of_record_type_compilation (record);
6385 /* If the size was widened explicitly, maybe give a warning. Take the
6386 original size as the maximum size of the input if there was an
6387 unconstrained record involved and round it up to the specified alignment,
6388 if one was specified. */
6389 if (CONTAINS_PLACEHOLDER_P (orig_size))
6390 orig_size = max_size (orig_size, true);
6392 if (align)
6393 orig_size = round_up (orig_size, align);
6395 if (Present (gnat_entity)
6396 && size
6397 && TREE_CODE (size) != MAX_EXPR
6398 && TREE_CODE (size) != COND_EXPR
6399 && !operand_equal_p (size, orig_size, 0)
6400 && !(TREE_CODE (size) == INTEGER_CST
6401 && TREE_CODE (orig_size) == INTEGER_CST
6402 && (TREE_OVERFLOW (size)
6403 || TREE_OVERFLOW (orig_size)
6404 || tree_int_cst_lt (size, orig_size))))
6406 Node_Id gnat_error_node = Empty;
6408 if (Is_Packed_Array_Type (gnat_entity))
6409 gnat_entity = Original_Array_Type (gnat_entity);
6411 if ((Ekind (gnat_entity) == E_Component
6412 || Ekind (gnat_entity) == E_Discriminant)
6413 && Present (Component_Clause (gnat_entity)))
6414 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
6415 else if (Present (Size_Clause (gnat_entity)))
6416 gnat_error_node = Expression (Size_Clause (gnat_entity));
6418 /* Generate message only for entities that come from source, since
6419 if we have an entity created by expansion, the message will be
6420 generated for some other corresponding source entity. */
6421 if (Comes_From_Source (gnat_entity))
6423 if (Present (gnat_error_node))
6424 post_error_ne_tree ("{^ }bits of & unused?",
6425 gnat_error_node, gnat_entity,
6426 size_diffop (size, orig_size));
6427 else if (is_component_type)
6428 post_error_ne_tree ("component of& padded{ by ^ bits}?",
6429 gnat_entity, gnat_entity,
6430 size_diffop (size, orig_size));
6434 return record;
6437 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6438 the value passed against the list of choices. */
6440 tree
6441 choices_to_gnu (tree operand, Node_Id choices)
6443 Node_Id choice;
6444 Node_Id gnat_temp;
6445 tree result = integer_zero_node;
6446 tree this_test, low = 0, high = 0, single = 0;
6448 for (choice = First (choices); Present (choice); choice = Next (choice))
6450 switch (Nkind (choice))
6452 case N_Range:
6453 low = gnat_to_gnu (Low_Bound (choice));
6454 high = gnat_to_gnu (High_Bound (choice));
6456 this_test
6457 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6458 build_binary_op (GE_EXPR, boolean_type_node,
6459 operand, low),
6460 build_binary_op (LE_EXPR, boolean_type_node,
6461 operand, high));
6463 break;
6465 case N_Subtype_Indication:
6466 gnat_temp = Range_Expression (Constraint (choice));
6467 low = gnat_to_gnu (Low_Bound (gnat_temp));
6468 high = gnat_to_gnu (High_Bound (gnat_temp));
6470 this_test
6471 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6472 build_binary_op (GE_EXPR, boolean_type_node,
6473 operand, low),
6474 build_binary_op (LE_EXPR, boolean_type_node,
6475 operand, high));
6476 break;
6478 case N_Identifier:
6479 case N_Expanded_Name:
6480 /* This represents either a subtype range, an enumeration
6481 literal, or a constant Ekind says which. If an enumeration
6482 literal or constant, fall through to the next case. */
6483 if (Ekind (Entity (choice)) != E_Enumeration_Literal
6484 && Ekind (Entity (choice)) != E_Constant)
6486 tree type = gnat_to_gnu_type (Entity (choice));
6488 low = TYPE_MIN_VALUE (type);
6489 high = TYPE_MAX_VALUE (type);
6491 this_test
6492 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6493 build_binary_op (GE_EXPR, boolean_type_node,
6494 operand, low),
6495 build_binary_op (LE_EXPR, boolean_type_node,
6496 operand, high));
6497 break;
6500 /* ... fall through ... */
6502 case N_Character_Literal:
6503 case N_Integer_Literal:
6504 single = gnat_to_gnu (choice);
6505 this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6506 single);
6507 break;
6509 case N_Others_Choice:
6510 this_test = integer_one_node;
6511 break;
6513 default:
6514 gcc_unreachable ();
6517 result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6518 this_test);
6521 return result;
6524 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6525 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6527 static int
6528 adjust_packed (tree field_type, tree record_type, int packed)
6530 /* If the field contains an item of variable size, we cannot pack it
6531 because we cannot create temporaries of non-fixed size in case
6532 we need to take the address of the field. See addressable_p and
6533 the notes on the addressability issues for further details. */
6534 if (is_variable_size (field_type))
6535 return 0;
6537 /* If the alignment of the record is specified and the field type
6538 is over-aligned, request Storage_Unit alignment for the field. */
6539 if (packed == -2)
6541 if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6542 return -1;
6543 else
6544 return 0;
6547 return packed;
6550 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6551 placed in GNU_RECORD_TYPE.
6553 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6554 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6555 record has a specified alignment.
6557 DEFINITION is true if this field is for a record being defined.
6559 DEBUG_INFO_P is true if we need to write debug information for types
6560 that we may create in the process. */
6562 static tree
6563 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6564 bool definition, bool debug_info_p)
6566 tree gnu_field_id = get_entity_name (gnat_field);
6567 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
6568 tree gnu_field, gnu_size, gnu_pos;
6569 bool needs_strict_alignment
6570 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
6571 || Treat_As_Volatile (gnat_field));
6573 /* If this field requires strict alignment, we cannot pack it because
6574 it would very likely be under-aligned in the record. */
6575 if (needs_strict_alignment)
6576 packed = 0;
6577 else
6578 packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6580 /* If a size is specified, use it. Otherwise, if the record type is packed,
6581 use the official RM size. See "Handling of Type'Size Values" in Einfo
6582 for further details. */
6583 if (Known_Static_Esize (gnat_field))
6584 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6585 gnat_field, FIELD_DECL, false, true);
6586 else if (packed == 1)
6587 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
6588 gnat_field, FIELD_DECL, false, true);
6589 else
6590 gnu_size = NULL_TREE;
6592 /* If we have a specified size that is smaller than that of the field's type,
6593 or a position is specified, and the field's type is a record that doesn't
6594 require strict alignment, see if we can get either an integral mode form
6595 of the type or a smaller form. If we can, show a size was specified for
6596 the field if there wasn't one already, so we know to make this a bitfield
6597 and avoid making things wider.
6599 Changing to an integral mode form is useful when the record is packed as
6600 we can then place the field at a non-byte-aligned position and so achieve
6601 tighter packing. This is in addition required if the field shares a byte
6602 with another field and the front-end lets the back-end handle the access
6603 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6605 Changing to a smaller form is required if the specified size is smaller
6606 than that of the field's type and the type contains sub-fields that are
6607 padded, in order to avoid generating accesses to these sub-fields that
6608 are wider than the field.
6610 We avoid the transformation if it is not required or potentially useful,
6611 as it might entail an increase of the field's alignment and have ripple
6612 effects on the outer record type. A typical case is a field known to be
6613 byte-aligned and not to share a byte with another field. */
6614 if (!needs_strict_alignment
6615 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6616 && !TYPE_FAT_POINTER_P (gnu_field_type)
6617 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
6618 && (packed == 1
6619 || (gnu_size
6620 && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6621 || (Present (Component_Clause (gnat_field))
6622 && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6623 % BITS_PER_UNIT == 0
6624 && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6626 tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6627 if (gnu_packable_type != gnu_field_type)
6629 gnu_field_type = gnu_packable_type;
6630 if (!gnu_size)
6631 gnu_size = rm_size (gnu_field_type);
6635 /* If we are packing the record and the field is BLKmode, round the
6636 size up to a byte boundary. */
6637 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6638 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6640 if (Present (Component_Clause (gnat_field)))
6642 Entity_Id gnat_parent
6643 = Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6645 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6646 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6647 gnat_field, FIELD_DECL, false, true);
6649 /* Ensure the position does not overlap with the parent subtype, if there
6650 is one. This test is omitted if the parent of the tagged type has a
6651 full rep clause since, in this case, component clauses are allowed to
6652 overlay the space allocated for the parent type and the front-end has
6653 checked that there are no overlapping components. */
6654 if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6656 tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6658 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6659 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6661 post_error_ne_tree
6662 ("offset of& must be beyond parent{, minimum allowed is ^}",
6663 First_Bit (Component_Clause (gnat_field)), gnat_field,
6664 TYPE_SIZE_UNIT (gnu_parent));
6668 /* If this field needs strict alignment, ensure the record is
6669 sufficiently aligned and that that position and size are
6670 consistent with the alignment. */
6671 if (needs_strict_alignment)
6673 TYPE_ALIGN (gnu_record_type)
6674 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6676 if (gnu_size
6677 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6679 if (Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
6680 post_error_ne_tree
6681 ("atomic field& must be natural size of type{ (^)}",
6682 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6683 TYPE_SIZE (gnu_field_type));
6685 else if (Is_Aliased (gnat_field))
6686 post_error_ne_tree
6687 ("size of aliased field& must be ^ bits",
6688 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6689 TYPE_SIZE (gnu_field_type));
6691 else if (Strict_Alignment (Etype (gnat_field)))
6692 post_error_ne_tree
6693 ("size of & with aliased or tagged components not ^ bits",
6694 Last_Bit (Component_Clause (gnat_field)), gnat_field,
6695 TYPE_SIZE (gnu_field_type));
6697 gnu_size = NULL_TREE;
6700 if (!integer_zerop (size_binop
6701 (TRUNC_MOD_EXPR, gnu_pos,
6702 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6704 if (Is_Aliased (gnat_field))
6705 post_error_ne_num
6706 ("position of aliased field& must be multiple of ^ bits",
6707 First_Bit (Component_Clause (gnat_field)), gnat_field,
6708 TYPE_ALIGN (gnu_field_type));
6710 else if (Treat_As_Volatile (gnat_field))
6711 post_error_ne_num
6712 ("position of volatile field& must be multiple of ^ bits",
6713 First_Bit (Component_Clause (gnat_field)), gnat_field,
6714 TYPE_ALIGN (gnu_field_type));
6716 else if (Strict_Alignment (Etype (gnat_field)))
6717 post_error_ne_num
6718 ("position of & with aliased or tagged components not multiple of ^ bits",
6719 First_Bit (Component_Clause (gnat_field)), gnat_field,
6720 TYPE_ALIGN (gnu_field_type));
6722 else
6723 gcc_unreachable ();
6725 gnu_pos = NULL_TREE;
6729 if (Is_Atomic (gnat_field))
6730 check_ok_for_atomic (gnu_field_type, gnat_field, false);
6733 /* If the record has rep clauses and this is the tag field, make a rep
6734 clause for it as well. */
6735 else if (Has_Specified_Layout (Scope (gnat_field))
6736 && Chars (gnat_field) == Name_uTag)
6738 gnu_pos = bitsize_zero_node;
6739 gnu_size = TYPE_SIZE (gnu_field_type);
6742 else
6743 gnu_pos = NULL_TREE;
6745 /* We need to make the size the maximum for the type if it is
6746 self-referential and an unconstrained type. In that case, we can't
6747 pack the field since we can't make a copy to align it. */
6748 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6749 && !gnu_size
6750 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6751 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
6753 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6754 packed = 0;
6757 /* If a size is specified, adjust the field's type to it. */
6758 if (gnu_size)
6760 tree orig_field_type;
6762 /* If the field's type is justified modular, we would need to remove
6763 the wrapper to (better) meet the layout requirements. However we
6764 can do so only if the field is not aliased to preserve the unique
6765 layout and if the prescribed size is not greater than that of the
6766 packed array to preserve the justification. */
6767 if (!needs_strict_alignment
6768 && TREE_CODE (gnu_field_type) == RECORD_TYPE
6769 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6770 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6771 <= 0)
6772 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6774 gnu_field_type
6775 = make_type_from_size (gnu_field_type, gnu_size,
6776 Has_Biased_Representation (gnat_field));
6778 orig_field_type = gnu_field_type;
6779 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6780 false, false, definition, true);
6782 /* If a padding record was made, declare it now since it will never be
6783 declared otherwise. This is necessary to ensure that its subtrees
6784 are properly marked. */
6785 if (gnu_field_type != orig_field_type
6786 && !DECL_P (TYPE_NAME (gnu_field_type)))
6787 create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, NULL,
6788 true, debug_info_p, gnat_field);
6791 /* Otherwise (or if there was an error), don't specify a position. */
6792 else
6793 gnu_pos = NULL_TREE;
6795 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6796 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6798 /* Now create the decl for the field. */
6799 gnu_field
6800 = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6801 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6802 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6803 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
6805 if (Ekind (gnat_field) == E_Discriminant)
6806 DECL_DISCRIMINANT_NUMBER (gnu_field)
6807 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6809 return gnu_field;
6812 /* Return true if TYPE is a type with variable size, a padding type with a
6813 field of variable size or is a record that has a field such a field. */
6815 static bool
6816 is_variable_size (tree type)
6818 tree field;
6820 if (!TREE_CONSTANT (TYPE_SIZE (type)))
6821 return true;
6823 if (TYPE_IS_PADDING_P (type)
6824 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6825 return true;
6827 if (TREE_CODE (type) != RECORD_TYPE
6828 && TREE_CODE (type) != UNION_TYPE
6829 && TREE_CODE (type) != QUAL_UNION_TYPE)
6830 return false;
6832 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6833 if (is_variable_size (TREE_TYPE (field)))
6834 return true;
6836 return false;
6839 /* qsort comparer for the bit positions of two record components. */
6841 static int
6842 compare_field_bitpos (const PTR rt1, const PTR rt2)
6844 const_tree const field1 = * (const_tree const *) rt1;
6845 const_tree const field2 = * (const_tree const *) rt2;
6846 const int ret
6847 = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6849 return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6852 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set
6853 the result as the field list of GNU_RECORD_TYPE and finish it up. When
6854 called from gnat_to_gnu_entity during the processing of a record type
6855 definition, the GCC node for the parent, if any, will be the single field
6856 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6857 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6858 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6860 PACKED is 1 if this is for a packed record, -1 if this is for a record
6861 with Component_Alignment of Storage_Unit, -2 if this is for a record
6862 with a specified alignment.
6864 DEFINITION is true if we are defining this record type.
6866 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6867 with a rep clause is to be added; in this case, that is all that should
6868 be done with such fields.
6870 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6871 out the record. This means the alignment only serves to force fields to
6872 be bitfields, but not to require the record to be that aligned. This is
6873 used for variants.
6875 ALL_REP is true if a rep clause is present for all the fields.
6877 UNCHECKED_UNION is true if we are building this type for a record with a
6878 Pragma Unchecked_Union.
6880 DEBUG_INFO_P is true if we need to write debug information about the type.
6882 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6883 mean that its contents may be unused as well, but only the container. */
6886 static void
6887 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6888 tree gnu_field_list, int packed, bool definition,
6889 tree *p_gnu_rep_list, bool cancel_alignment,
6890 bool all_rep, bool unchecked_union, bool debug_info_p,
6891 bool maybe_unused)
6893 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6894 bool layout_with_rep = false;
6895 Node_Id component_decl, variant_part;
6896 tree gnu_our_rep_list = NULL_TREE;
6897 tree gnu_field, gnu_next, gnu_last = tree_last (gnu_field_list);
6899 /* For each component referenced in a component declaration create a GCC
6900 field and add it to the list, skipping pragmas in the GNAT list. */
6901 if (Present (Component_Items (gnat_component_list)))
6902 for (component_decl
6903 = First_Non_Pragma (Component_Items (gnat_component_list));
6904 Present (component_decl);
6905 component_decl = Next_Non_Pragma (component_decl))
6907 Entity_Id gnat_field = Defining_Entity (component_decl);
6908 Name_Id gnat_name = Chars (gnat_field);
6910 /* If present, the _Parent field must have been created as the single
6911 field of the record type. Put it before any other fields. */
6912 if (gnat_name == Name_uParent)
6914 gnu_field = TYPE_FIELDS (gnu_record_type);
6915 gnu_field_list = chainon (gnu_field_list, gnu_field);
6917 else
6919 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
6920 definition, debug_info_p);
6922 /* If this is the _Tag field, put it before any other fields. */
6923 if (gnat_name == Name_uTag)
6924 gnu_field_list = chainon (gnu_field_list, gnu_field);
6926 /* If this is the _Controller field, put it before the other
6927 fields except for the _Tag or _Parent field. */
6928 else if (gnat_name == Name_uController && gnu_last)
6930 TREE_CHAIN (gnu_field) = TREE_CHAIN (gnu_last);
6931 TREE_CHAIN (gnu_last) = gnu_field;
6934 /* If this is a regular field, put it after the other fields. */
6935 else
6937 TREE_CHAIN (gnu_field) = gnu_field_list;
6938 gnu_field_list = gnu_field;
6939 if (!gnu_last)
6940 gnu_last = gnu_field;
6944 save_gnu_tree (gnat_field, gnu_field, false);
6947 /* At the end of the component list there may be a variant part. */
6948 variant_part = Variant_Part (gnat_component_list);
6950 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6951 mutually exclusive and should go in the same memory. To do this we need
6952 to treat each variant as a record whose elements are created from the
6953 component list for the variant. So here we create the records from the
6954 lists for the variants and put them all into the QUAL_UNION_TYPE.
6955 If this is an Unchecked_Union, we make a UNION_TYPE instead or
6956 use GNU_RECORD_TYPE if there are no fields so far. */
6957 if (Present (variant_part))
6959 Node_Id gnat_discr = Name (variant_part), variant;
6960 tree gnu_discr = gnat_to_gnu (gnat_discr);
6961 tree gnu_name = TYPE_NAME (gnu_record_type);
6962 tree gnu_var_name
6963 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
6964 "XVN");
6965 tree gnu_union_type, gnu_union_name, gnu_union_field;
6966 tree gnu_variant_list = NULL_TREE;
6968 if (TREE_CODE (gnu_name) == TYPE_DECL)
6969 gnu_name = DECL_NAME (gnu_name);
6971 gnu_union_name
6972 = concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
6974 /* Reuse an enclosing union if all fields are in the variant part
6975 and there is no representation clause on the record, to match
6976 the layout of C unions. There is an associated check below. */
6977 if (!gnu_field_list
6978 && TREE_CODE (gnu_record_type) == UNION_TYPE
6979 && !TYPE_PACKED (gnu_record_type))
6980 gnu_union_type = gnu_record_type;
6981 else
6983 gnu_union_type
6984 = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
6986 TYPE_NAME (gnu_union_type) = gnu_union_name;
6987 TYPE_ALIGN (gnu_union_type) = 0;
6988 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
6991 for (variant = First_Non_Pragma (Variants (variant_part));
6992 Present (variant);
6993 variant = Next_Non_Pragma (variant))
6995 tree gnu_variant_type = make_node (RECORD_TYPE);
6996 tree gnu_inner_name;
6997 tree gnu_qual;
6999 Get_Variant_Encoding (variant);
7000 gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
7001 TYPE_NAME (gnu_variant_type)
7002 = concat_name (gnu_union_name,
7003 IDENTIFIER_POINTER (gnu_inner_name));
7005 /* Set the alignment of the inner type in case we need to make
7006 inner objects into bitfields, but then clear it out so the
7007 record actually gets only the alignment required. */
7008 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
7009 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
7011 /* Similarly, if the outer record has a size specified and all
7012 fields have record rep clauses, we can propagate the size
7013 into the variant part. */
7014 if (all_rep_and_size)
7016 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
7017 TYPE_SIZE_UNIT (gnu_variant_type)
7018 = TYPE_SIZE_UNIT (gnu_record_type);
7021 /* Add the fields into the record type for the variant. Note that
7022 we aren't sure to really use it at this point, see below. */
7023 components_to_record (gnu_variant_type, Component_List (variant),
7024 NULL_TREE, packed, definition,
7025 &gnu_our_rep_list, !all_rep_and_size, all_rep,
7026 unchecked_union, debug_info_p, true);
7028 gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
7030 Set_Present_Expr (variant, annotate_value (gnu_qual));
7032 /* If this is an Unchecked_Union and we have exactly one field,
7033 use this field directly to match the layout of C unions. */
7034 if (unchecked_union
7035 && TYPE_FIELDS (gnu_variant_type)
7036 && !TREE_CHAIN (TYPE_FIELDS (gnu_variant_type)))
7037 gnu_field = TYPE_FIELDS (gnu_variant_type);
7038 else
7040 /* Deal with packedness like in gnat_to_gnu_field. */
7041 int field_packed
7042 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7044 /* Finalize the record type now. We used to throw away
7045 empty records but we no longer do that because we need
7046 them to generate complete debug info for the variant;
7047 otherwise, the union type definition will be lacking
7048 the fields associated with these empty variants. */
7049 rest_of_record_type_compilation (gnu_variant_type);
7050 create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7051 NULL, true, debug_info_p, gnat_component_list);
7053 gnu_field
7054 = create_field_decl (gnu_inner_name, gnu_variant_type,
7055 gnu_union_type,
7056 all_rep_and_size
7057 ? TYPE_SIZE (gnu_variant_type) : 0,
7058 all_rep_and_size
7059 ? bitsize_zero_node : 0,
7060 field_packed, 0);
7062 DECL_INTERNAL_P (gnu_field) = 1;
7064 if (!unchecked_union)
7065 DECL_QUALIFIER (gnu_field) = gnu_qual;
7068 TREE_CHAIN (gnu_field) = gnu_variant_list;
7069 gnu_variant_list = gnu_field;
7072 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7073 if (gnu_variant_list)
7075 int union_field_packed;
7077 if (all_rep_and_size)
7079 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7080 TYPE_SIZE_UNIT (gnu_union_type)
7081 = TYPE_SIZE_UNIT (gnu_record_type);
7084 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7085 all_rep_and_size ? 1 : 0, debug_info_p);
7087 /* If GNU_UNION_TYPE is our record type, it means we must have an
7088 Unchecked_Union with no fields. Verify that and, if so, just
7089 return. */
7090 if (gnu_union_type == gnu_record_type)
7092 gcc_assert (unchecked_union
7093 && !gnu_field_list
7094 && !gnu_our_rep_list);
7095 return;
7098 create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type,
7099 NULL, true, debug_info_p, gnat_component_list);
7101 /* Deal with packedness like in gnat_to_gnu_field. */
7102 union_field_packed
7103 = adjust_packed (gnu_union_type, gnu_record_type, packed);
7105 gnu_union_field
7106 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7107 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
7108 all_rep ? bitsize_zero_node : 0,
7109 union_field_packed, 0);
7111 DECL_INTERNAL_P (gnu_union_field) = 1;
7112 TREE_CHAIN (gnu_union_field) = gnu_field_list;
7113 gnu_field_list = gnu_union_field;
7117 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
7118 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do
7119 this in a separate pass since we want to handle the discriminants but
7120 can't play with them until we've used them in debugging data above.
7122 ??? If we then reorder them, debugging information will be wrong but
7123 there's nothing that can be done about this at the moment. */
7124 gnu_last = NULL_TREE;
7125 for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7127 gnu_next = TREE_CHAIN (gnu_field);
7129 if (DECL_FIELD_OFFSET (gnu_field))
7131 if (!gnu_last)
7132 gnu_field_list = gnu_next;
7133 else
7134 TREE_CHAIN (gnu_last) = gnu_next;
7136 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
7137 gnu_our_rep_list = gnu_field;
7139 else
7140 gnu_last = gnu_field;
7143 /* If we have any fields in our rep'ed field list and it is not the case that
7144 all the fields in the record have rep clauses and P_REP_LIST is nonzero,
7145 set it and ignore these fields. */
7146 if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
7147 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
7149 /* Otherwise, sort the fields by bit position and put them into their own
7150 record, before the others, if we also have fields without rep clauses. */
7151 else if (gnu_our_rep_list)
7153 tree gnu_rep_type
7154 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
7155 int i, len = list_length (gnu_our_rep_list);
7156 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
7158 for (gnu_field = gnu_our_rep_list, i = 0;
7159 gnu_field;
7160 gnu_field = TREE_CHAIN (gnu_field), i++)
7161 gnu_arr[i] = gnu_field;
7163 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7165 /* Put the fields in the list in order of increasing position, which
7166 means we start from the end. */
7167 gnu_our_rep_list = NULL_TREE;
7168 for (i = len - 1; i >= 0; i--)
7170 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
7171 gnu_our_rep_list = gnu_arr[i];
7172 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7175 if (gnu_field_list)
7177 finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, debug_info_p);
7178 gnu_field
7179 = create_field_decl (get_identifier ("REP"), gnu_rep_type,
7180 gnu_record_type, NULL_TREE, NULL_TREE, 0, 1);
7181 DECL_INTERNAL_P (gnu_field) = 1;
7182 gnu_field_list = chainon (gnu_field_list, gnu_field);
7184 else
7186 layout_with_rep = true;
7187 gnu_field_list = nreverse (gnu_our_rep_list);
7191 if (cancel_alignment)
7192 TYPE_ALIGN (gnu_record_type) = 0;
7194 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
7195 layout_with_rep ? 1 : 0, debug_info_p && !maybe_unused);
7198 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7199 placed into an Esize, Component_Bit_Offset, or Component_Size value
7200 in the GNAT tree. */
7202 static Uint
7203 annotate_value (tree gnu_size)
7205 TCode tcode;
7206 Node_Ref_Or_Val ops[3], ret;
7207 struct tree_int_map **h = NULL;
7208 int i;
7210 /* See if we've already saved the value for this node. */
7211 if (EXPR_P (gnu_size))
7213 struct tree_int_map in;
7214 if (!annotate_value_cache)
7215 annotate_value_cache = htab_create_ggc (512, tree_int_map_hash,
7216 tree_int_map_eq, 0);
7217 in.base.from = gnu_size;
7218 h = (struct tree_int_map **)
7219 htab_find_slot (annotate_value_cache, &in, INSERT);
7221 if (*h)
7222 return (Node_Ref_Or_Val) (*h)->to;
7225 /* If we do not return inside this switch, TCODE will be set to the
7226 code to use for a Create_Node operand and LEN (set above) will be
7227 the number of recursive calls for us to make. */
7229 switch (TREE_CODE (gnu_size))
7231 case INTEGER_CST:
7232 if (TREE_OVERFLOW (gnu_size))
7233 return No_Uint;
7235 /* This may come from a conversion from some smaller type, so ensure
7236 this is in bitsizetype. */
7237 gnu_size = convert (bitsizetype, gnu_size);
7239 /* For a negative value, build NEGATE_EXPR of the opposite. Such values
7240 appear in expressions containing aligning patterns. Note that, since
7241 sizetype is sign-extended but nonetheless unsigned, we don't directly
7242 use tree_int_cst_sgn. */
7243 if (TREE_INT_CST_HIGH (gnu_size) < 0)
7245 tree op_size = fold_build1 (NEGATE_EXPR, bitsizetype, gnu_size);
7246 return annotate_value (build1 (NEGATE_EXPR, bitsizetype, op_size));
7249 return UI_From_gnu (gnu_size);
7251 case COMPONENT_REF:
7252 /* The only case we handle here is a simple discriminant reference. */
7253 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
7254 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
7255 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7256 return Create_Node (Discrim_Val,
7257 annotate_value (DECL_DISCRIMINANT_NUMBER
7258 (TREE_OPERAND (gnu_size, 1))),
7259 No_Uint, No_Uint);
7260 else
7261 return No_Uint;
7263 CASE_CONVERT: case NON_LVALUE_EXPR:
7264 return annotate_value (TREE_OPERAND (gnu_size, 0));
7266 /* Now just list the operations we handle. */
7267 case COND_EXPR: tcode = Cond_Expr; break;
7268 case PLUS_EXPR: tcode = Plus_Expr; break;
7269 case MINUS_EXPR: tcode = Minus_Expr; break;
7270 case MULT_EXPR: tcode = Mult_Expr; break;
7271 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
7272 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
7273 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
7274 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
7275 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
7276 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
7277 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
7278 case NEGATE_EXPR: tcode = Negate_Expr; break;
7279 case MIN_EXPR: tcode = Min_Expr; break;
7280 case MAX_EXPR: tcode = Max_Expr; break;
7281 case ABS_EXPR: tcode = Abs_Expr; break;
7282 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
7283 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
7284 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
7285 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
7286 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
7287 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
7288 case BIT_AND_EXPR: tcode = Bit_And_Expr; break;
7289 case LT_EXPR: tcode = Lt_Expr; break;
7290 case LE_EXPR: tcode = Le_Expr; break;
7291 case GT_EXPR: tcode = Gt_Expr; break;
7292 case GE_EXPR: tcode = Ge_Expr; break;
7293 case EQ_EXPR: tcode = Eq_Expr; break;
7294 case NE_EXPR: tcode = Ne_Expr; break;
7296 case CALL_EXPR:
7298 tree t = maybe_inline_call_in_expr (gnu_size);
7299 if (t)
7300 return annotate_value (t);
7303 /* Fall through... */
7305 default:
7306 return No_Uint;
7309 /* Now get each of the operands that's relevant for this code. If any
7310 cannot be expressed as a repinfo node, say we can't. */
7311 for (i = 0; i < 3; i++)
7312 ops[i] = No_Uint;
7314 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7316 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7317 if (ops[i] == No_Uint)
7318 return No_Uint;
7321 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7323 /* Save the result in the cache. */
7324 if (h)
7326 *h = ggc_alloc_tree_int_map ();
7327 (*h)->base.from = gnu_size;
7328 (*h)->to = ret;
7331 return ret;
7334 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7335 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7336 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7337 BY_REF is true if the object is used by reference. */
7339 void
7340 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7342 if (by_ref)
7344 if (TYPE_IS_FAT_POINTER_P (gnu_type))
7345 gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7346 else
7347 gnu_type = TREE_TYPE (gnu_type);
7350 if (Unknown_Esize (gnat_entity))
7352 if (TREE_CODE (gnu_type) == RECORD_TYPE
7353 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7354 size = TYPE_SIZE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type))));
7355 else if (!size)
7356 size = TYPE_SIZE (gnu_type);
7358 if (size)
7359 Set_Esize (gnat_entity, annotate_value (size));
7362 if (Unknown_Alignment (gnat_entity))
7363 Set_Alignment (gnat_entity,
7364 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7367 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7368 Return NULL_TREE if there is no such element in the list. */
7370 static tree
7371 purpose_member_field (const_tree elem, tree list)
7373 while (list)
7375 tree field = TREE_PURPOSE (list);
7376 if (SAME_FIELD_P (field, elem))
7377 return list;
7378 list = TREE_CHAIN (list);
7380 return NULL_TREE;
7383 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7384 set Component_Bit_Offset and Esize of the components to the position and
7385 size used by Gigi. */
7387 static void
7388 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7390 Entity_Id gnat_field;
7391 tree gnu_list;
7393 /* We operate by first making a list of all fields and their position (we
7394 can get the size easily) and then update all the sizes in the tree. */
7395 gnu_list
7396 = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7397 BIGGEST_ALIGNMENT, NULL_TREE);
7399 for (gnat_field = First_Entity (gnat_entity);
7400 Present (gnat_field);
7401 gnat_field = Next_Entity (gnat_field))
7402 if (Ekind (gnat_field) == E_Component
7403 || (Ekind (gnat_field) == E_Discriminant
7404 && !Is_Unchecked_Union (Scope (gnat_field))))
7406 tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7407 gnu_list);
7408 if (t)
7410 tree parent_offset;
7412 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
7414 /* In this mode the tag and parent components are not
7415 generated, so we add the appropriate offset to each
7416 component. For a component appearing in the current
7417 extension, the offset is the size of the parent. */
7418 if (Is_Derived_Type (gnat_entity)
7419 && Original_Record_Component (gnat_field) == gnat_field)
7420 parent_offset
7421 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7422 bitsizetype);
7423 else
7424 parent_offset = bitsize_int (POINTER_SIZE);
7426 else
7427 parent_offset = bitsize_zero_node;
7429 Set_Component_Bit_Offset
7430 (gnat_field,
7431 annotate_value
7432 (size_binop (PLUS_EXPR,
7433 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7434 TREE_VEC_ELT (TREE_VALUE (t), 2)),
7435 parent_offset)));
7437 Set_Esize (gnat_field,
7438 annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7440 else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7442 /* If there is no entry, this is an inherited component whose
7443 position is the same as in the parent type. */
7444 Set_Component_Bit_Offset
7445 (gnat_field,
7446 Component_Bit_Offset (Original_Record_Component (gnat_field)));
7448 Set_Esize (gnat_field,
7449 Esize (Original_Record_Component (gnat_field)));
7454 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7455 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7456 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7457 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7458 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7459 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7460 pre-existing list to be chained to the newly created entries. */
7462 static tree
7463 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7464 tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7466 tree gnu_field;
7468 for (gnu_field = TYPE_FIELDS (gnu_type);
7469 gnu_field;
7470 gnu_field = TREE_CHAIN (gnu_field))
7472 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7473 DECL_FIELD_BIT_OFFSET (gnu_field));
7474 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7475 DECL_FIELD_OFFSET (gnu_field));
7476 unsigned int our_offset_align
7477 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7478 tree v = make_tree_vec (3);
7480 TREE_VEC_ELT (v, 0) = gnu_our_offset;
7481 TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7482 TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7483 gnu_list = tree_cons (gnu_field, v, gnu_list);
7485 /* Recurse on internal fields, flattening the nested fields except for
7486 those in the variant part, if requested. */
7487 if (DECL_INTERNAL_P (gnu_field))
7489 tree gnu_field_type = TREE_TYPE (gnu_field);
7490 if (do_not_flatten_variant
7491 && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7492 gnu_list
7493 = build_position_list (gnu_field_type, do_not_flatten_variant,
7494 size_zero_node, bitsize_zero_node,
7495 BIGGEST_ALIGNMENT, gnu_list);
7496 else
7497 gnu_list
7498 = build_position_list (gnu_field_type, do_not_flatten_variant,
7499 gnu_our_offset, gnu_our_bitpos,
7500 our_offset_align, gnu_list);
7504 return gnu_list;
7507 /* Return a TREE_LIST describing the substitutions needed to reflect the
7508 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7509 be in any order. TREE_PURPOSE gives the tree for the discriminant and
7510 TREE_VALUE is the replacement value. They are in the form of operands
7511 to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for a definition
7512 of GNAT_SUBTYPE. */
7514 static tree
7515 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7517 tree gnu_list = NULL_TREE;
7518 Entity_Id gnat_discrim;
7519 Node_Id gnat_value;
7521 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7522 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
7523 Present (gnat_discrim);
7524 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7525 gnat_value = Next_Elmt (gnat_value))
7526 /* Ignore access discriminants. */
7527 if (!Is_Access_Type (Etype (Node (gnat_value))))
7529 tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7530 gnu_list = tree_cons (gnu_field,
7531 convert (TREE_TYPE (gnu_field),
7532 elaborate_expression
7533 (Node (gnat_value), gnat_subtype,
7534 get_entity_name (gnat_discrim),
7535 definition, true, false)),
7536 gnu_list);
7539 return gnu_list;
7542 /* Scan all fields in QUAL_UNION_TYPE and return a TREE_LIST describing the
7543 variants of QUAL_UNION_TYPE that are still relevant after applying the
7544 substitutions described in SUBST_LIST. TREE_PURPOSE is the type of the
7545 variant and TREE_VALUE is a TREE_VEC containing the field, the new value
7546 of the qualifier and NULL_TREE respectively. GNU_LIST is a pre-existing
7547 list to be chained to the newly created entries. */
7549 static tree
7550 build_variant_list (tree qual_union_type, tree subst_list, tree gnu_list)
7552 tree gnu_field;
7554 for (gnu_field = TYPE_FIELDS (qual_union_type);
7555 gnu_field;
7556 gnu_field = TREE_CHAIN (gnu_field))
7558 tree t, qual = DECL_QUALIFIER (gnu_field);
7560 for (t = subst_list; t; t = TREE_CHAIN (t))
7561 qual = SUBSTITUTE_IN_EXPR (qual, TREE_PURPOSE (t), TREE_VALUE (t));
7563 /* If the new qualifier is not unconditionally false, its variant may
7564 still be accessed. */
7565 if (!integer_zerop (qual))
7567 tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7568 tree v = make_tree_vec (3);
7569 TREE_VEC_ELT (v, 0) = gnu_field;
7570 TREE_VEC_ELT (v, 1) = qual;
7571 TREE_VEC_ELT (v, 2) = NULL_TREE;
7572 gnu_list = tree_cons (variant_type, v, gnu_list);
7574 /* Recurse on the variant subpart of the variant, if any. */
7575 variant_subpart = get_variant_part (variant_type);
7576 if (variant_subpart)
7577 gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
7578 subst_list, gnu_list);
7580 /* If the new qualifier is unconditionally true, the subsequent
7581 variants cannot be accessed. */
7582 if (integer_onep (qual))
7583 break;
7587 return gnu_list;
7590 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7591 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
7592 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
7593 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
7594 for the size of a field. COMPONENT_P is true if we are being called
7595 to process the Component_Size of GNAT_OBJECT. This is used for error
7596 message handling and to indicate to use the object size of GNU_TYPE.
7597 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
7598 it means that a size of zero should be treated as an unspecified size. */
7600 static tree
7601 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7602 enum tree_code kind, bool component_p, bool zero_ok)
7604 Node_Id gnat_error_node;
7605 tree type_size, size;
7607 /* Return 0 if no size was specified. */
7608 if (uint_size == No_Uint)
7609 return NULL_TREE;
7611 /* Ignore a negative size since that corresponds to our back-annotation. */
7612 if (UI_Lt (uint_size, Uint_0))
7613 return NULL_TREE;
7615 /* Find the node to use for errors. */
7616 if ((Ekind (gnat_object) == E_Component
7617 || Ekind (gnat_object) == E_Discriminant)
7618 && Present (Component_Clause (gnat_object)))
7619 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7620 else if (Present (Size_Clause (gnat_object)))
7621 gnat_error_node = Expression (Size_Clause (gnat_object));
7622 else
7623 gnat_error_node = gnat_object;
7625 /* Get the size as a tree. Issue an error if a size was specified but
7626 cannot be represented in sizetype. */
7627 size = UI_To_gnu (uint_size, bitsizetype);
7628 if (TREE_OVERFLOW (size))
7630 if (component_p)
7631 post_error_ne ("component size of & is too large", gnat_error_node,
7632 gnat_object);
7633 else
7634 post_error_ne ("size of & is too large", gnat_error_node,
7635 gnat_object);
7636 return NULL_TREE;
7639 /* Ignore a zero size if it is not permitted. */
7640 if (!zero_ok && integer_zerop (size))
7641 return NULL_TREE;
7643 /* The size of objects is always a multiple of a byte. */
7644 if (kind == VAR_DECL
7645 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7647 if (component_p)
7648 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7649 gnat_error_node, gnat_object);
7650 else
7651 post_error_ne ("size for& is not a multiple of Storage_Unit",
7652 gnat_error_node, gnat_object);
7653 return NULL_TREE;
7656 /* If this is an integral type or a packed array type, the front-end has
7657 verified the size, so we need not do it here (which would entail
7658 checking against the bounds). However, if this is an aliased object,
7659 it may not be smaller than the type of the object. */
7660 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7661 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7662 return size;
7664 /* If the object is a record that contains a template, add the size of
7665 the template to the specified size. */
7666 if (TREE_CODE (gnu_type) == RECORD_TYPE
7667 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7668 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7670 if (kind == VAR_DECL
7671 /* If a type needs strict alignment, a component of this type in
7672 a packed record cannot be packed and thus uses the type size. */
7673 || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7674 type_size = TYPE_SIZE (gnu_type);
7675 else
7676 type_size = rm_size (gnu_type);
7678 /* Modify the size of the type to be that of the maximum size if it has a
7679 discriminant. */
7680 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7681 type_size = max_size (type_size, true);
7683 /* If this is an access type or a fat pointer, the minimum size is that given
7684 by the smallest integral mode that's valid for pointers. */
7685 if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7687 enum machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7688 while (!targetm.valid_pointer_mode (p_mode))
7689 p_mode = GET_MODE_WIDER_MODE (p_mode);
7690 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7693 /* If the size of the object is a constant, the new size must not be
7694 smaller. */
7695 if (TREE_CODE (type_size) != INTEGER_CST
7696 || TREE_OVERFLOW (type_size)
7697 || tree_int_cst_lt (size, type_size))
7699 if (component_p)
7700 post_error_ne_tree
7701 ("component size for& too small{, minimum allowed is ^}",
7702 gnat_error_node, gnat_object, type_size);
7703 else
7704 post_error_ne_tree
7705 ("size for& too small{, minimum allowed is ^}",
7706 gnat_error_node, gnat_object, type_size);
7708 size = NULL_TREE;
7711 return size;
7714 /* Similarly, but both validate and process a value of RM size. This
7715 routine is only called for types. */
7717 static void
7718 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7720 Node_Id gnat_attr_node;
7721 tree old_size, size;
7723 /* Do nothing if no size was specified. */
7724 if (uint_size == No_Uint)
7725 return;
7727 /* Ignore a negative size since that corresponds to our back-annotation. */
7728 if (UI_Lt (uint_size, Uint_0))
7729 return;
7731 /* Only issue an error if a Value_Size clause was explicitly given.
7732 Otherwise, we'd be duplicating an error on the Size clause. */
7733 gnat_attr_node
7734 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
7736 /* Get the size as a tree. Issue an error if a size was specified but
7737 cannot be represented in sizetype. */
7738 size = UI_To_gnu (uint_size, bitsizetype);
7739 if (TREE_OVERFLOW (size))
7741 if (Present (gnat_attr_node))
7742 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
7743 gnat_entity);
7744 return;
7747 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
7748 exists, or this is an integer type, in which case the front-end will
7749 have always set it. */
7750 if (No (gnat_attr_node)
7751 && integer_zerop (size)
7752 && !Has_Size_Clause (gnat_entity)
7753 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7754 return;
7756 old_size = rm_size (gnu_type);
7758 /* If the old size is self-referential, get the maximum size. */
7759 if (CONTAINS_PLACEHOLDER_P (old_size))
7760 old_size = max_size (old_size, true);
7762 /* If the size of the object is a constant, the new size must not be smaller
7763 (the front-end has verified this for scalar and packed array types). */
7764 if (TREE_CODE (old_size) != INTEGER_CST
7765 || TREE_OVERFLOW (old_size)
7766 || (AGGREGATE_TYPE_P (gnu_type)
7767 && !(TREE_CODE (gnu_type) == ARRAY_TYPE
7768 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
7769 && !(TYPE_IS_PADDING_P (gnu_type)
7770 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
7771 && TYPE_PACKED_ARRAY_TYPE_P
7772 (TREE_TYPE (TYPE_FIELDS (gnu_type))))
7773 && tree_int_cst_lt (size, old_size)))
7775 if (Present (gnat_attr_node))
7776 post_error_ne_tree
7777 ("Value_Size for& too small{, minimum allowed is ^}",
7778 gnat_attr_node, gnat_entity, old_size);
7779 return;
7782 /* Otherwise, set the RM size proper for integral types... */
7783 if ((TREE_CODE (gnu_type) == INTEGER_TYPE
7784 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7785 || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
7786 || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
7787 SET_TYPE_RM_SIZE (gnu_type, size);
7789 /* ...or the Ada size for record and union types. */
7790 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
7791 || TREE_CODE (gnu_type) == UNION_TYPE
7792 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
7793 && !TYPE_FAT_POINTER_P (gnu_type))
7794 SET_TYPE_ADA_SIZE (gnu_type, size);
7797 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
7798 If TYPE is the best type, return it. Otherwise, make a new type. We
7799 only support new integral and pointer types. FOR_BIASED is true if
7800 we are making a biased type. */
7802 static tree
7803 make_type_from_size (tree type, tree size_tree, bool for_biased)
7805 unsigned HOST_WIDE_INT size;
7806 bool biased_p;
7807 tree new_type;
7809 /* If size indicates an error, just return TYPE to avoid propagating
7810 the error. Likewise if it's too large to represent. */
7811 if (!size_tree || !host_integerp (size_tree, 1))
7812 return type;
7814 size = tree_low_cst (size_tree, 1);
7816 switch (TREE_CODE (type))
7818 case INTEGER_TYPE:
7819 case ENUMERAL_TYPE:
7820 case BOOLEAN_TYPE:
7821 biased_p = (TREE_CODE (type) == INTEGER_TYPE
7822 && TYPE_BIASED_REPRESENTATION_P (type));
7824 /* Integer types with precision 0 are forbidden. */
7825 if (size == 0)
7826 size = 1;
7828 /* Only do something if the type is not a packed array type and
7829 doesn't already have the proper size. */
7830 if (TYPE_PACKED_ARRAY_TYPE_P (type)
7831 || (TYPE_PRECISION (type) == size && biased_p == for_biased))
7832 break;
7834 biased_p |= for_biased;
7835 if (size > LONG_LONG_TYPE_SIZE)
7836 size = LONG_LONG_TYPE_SIZE;
7838 if (TYPE_UNSIGNED (type) || biased_p)
7839 new_type = make_unsigned_type (size);
7840 else
7841 new_type = make_signed_type (size);
7842 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
7843 SET_TYPE_RM_MIN_VALUE (new_type,
7844 convert (TREE_TYPE (new_type),
7845 TYPE_MIN_VALUE (type)));
7846 SET_TYPE_RM_MAX_VALUE (new_type,
7847 convert (TREE_TYPE (new_type),
7848 TYPE_MAX_VALUE (type)));
7849 /* Copy the name to show that it's essentially the same type and
7850 not a subrange type. */
7851 TYPE_NAME (new_type) = TYPE_NAME (type);
7852 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
7853 SET_TYPE_RM_SIZE (new_type, bitsize_int (size));
7854 return new_type;
7856 case RECORD_TYPE:
7857 /* Do something if this is a fat pointer, in which case we
7858 may need to return the thin pointer. */
7859 if (TYPE_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
7861 enum machine_mode p_mode = mode_for_size (size, MODE_INT, 0);
7862 if (!targetm.valid_pointer_mode (p_mode))
7863 p_mode = ptr_mode;
7864 return
7865 build_pointer_type_for_mode
7866 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)),
7867 p_mode, 0);
7869 break;
7871 case POINTER_TYPE:
7872 /* Only do something if this is a thin pointer, in which case we
7873 may need to return the fat pointer. */
7874 if (TYPE_IS_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
7875 return
7876 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
7877 break;
7879 default:
7880 break;
7883 return type;
7886 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
7887 a type or object whose present alignment is ALIGN. If this alignment is
7888 valid, return it. Otherwise, give an error and return ALIGN. */
7890 static unsigned int
7891 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
7893 unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
7894 unsigned int new_align;
7895 Node_Id gnat_error_node;
7897 /* Don't worry about checking alignment if alignment was not specified
7898 by the source program and we already posted an error for this entity. */
7899 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
7900 return align;
7902 /* Post the error on the alignment clause if any. Note, for the implicit
7903 base type of an array type, the alignment clause is on the first
7904 subtype. */
7905 if (Present (Alignment_Clause (gnat_entity)))
7906 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
7908 else if (Is_Itype (gnat_entity)
7909 && Is_Array_Type (gnat_entity)
7910 && Etype (gnat_entity) == gnat_entity
7911 && Present (Alignment_Clause (First_Subtype (gnat_entity))))
7912 gnat_error_node =
7913 Expression (Alignment_Clause (First_Subtype (gnat_entity)));
7915 else
7916 gnat_error_node = gnat_entity;
7918 /* Within GCC, an alignment is an integer, so we must make sure a value is
7919 specified that fits in that range. Also, there is an upper bound to
7920 alignments we can support/allow. */
7921 if (!UI_Is_In_Int_Range (alignment)
7922 || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
7923 post_error_ne_num ("largest supported alignment for& is ^",
7924 gnat_error_node, gnat_entity, max_allowed_alignment);
7925 else if (!(Present (Alignment_Clause (gnat_entity))
7926 && From_At_Mod (Alignment_Clause (gnat_entity)))
7927 && new_align * BITS_PER_UNIT < align)
7929 unsigned int double_align;
7930 bool is_capped_double, align_clause;
7932 /* If the default alignment of "double" or larger scalar types is
7933 specifically capped and the new alignment is above the cap, do
7934 not post an error and change the alignment only if there is an
7935 alignment clause; this makes it possible to have the associated
7936 GCC type overaligned by default for performance reasons. */
7937 if ((double_align = double_float_alignment) > 0)
7939 Entity_Id gnat_type
7940 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
7941 is_capped_double
7942 = is_double_float_or_array (gnat_type, &align_clause);
7944 else if ((double_align = double_scalar_alignment) > 0)
7946 Entity_Id gnat_type
7947 = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
7948 is_capped_double
7949 = is_double_scalar_or_array (gnat_type, &align_clause);
7951 else
7952 is_capped_double = align_clause = false;
7954 if (is_capped_double && new_align >= double_align)
7956 if (align_clause)
7957 align = new_align * BITS_PER_UNIT;
7959 else
7961 if (is_capped_double)
7962 align = double_align * BITS_PER_UNIT;
7964 post_error_ne_num ("alignment for& must be at least ^",
7965 gnat_error_node, gnat_entity,
7966 align / BITS_PER_UNIT);
7969 else
7971 new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
7972 if (new_align > align)
7973 align = new_align;
7976 return align;
7979 /* Return the smallest alignment not less than SIZE. */
7981 static unsigned int
7982 ceil_alignment (unsigned HOST_WIDE_INT size)
7984 return (unsigned int) 1 << (floor_log2 (size - 1) + 1);
7987 /* Verify that OBJECT, a type or decl, is something we can implement
7988 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
7989 if we require atomic components. */
7991 static void
7992 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
7994 Node_Id gnat_error_point = gnat_entity;
7995 Node_Id gnat_node;
7996 enum machine_mode mode;
7997 unsigned int align;
7998 tree size;
8000 /* There are three case of what OBJECT can be. It can be a type, in which
8001 case we take the size, alignment and mode from the type. It can be a
8002 declaration that was indirect, in which case the relevant values are
8003 that of the type being pointed to, or it can be a normal declaration,
8004 in which case the values are of the decl. The code below assumes that
8005 OBJECT is either a type or a decl. */
8006 if (TYPE_P (object))
8008 /* If this is an anonymous base type, nothing to check. Error will be
8009 reported on the source type. */
8010 if (!Comes_From_Source (gnat_entity))
8011 return;
8013 mode = TYPE_MODE (object);
8014 align = TYPE_ALIGN (object);
8015 size = TYPE_SIZE (object);
8017 else if (DECL_BY_REF_P (object))
8019 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
8020 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
8021 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
8023 else
8025 mode = DECL_MODE (object);
8026 align = DECL_ALIGN (object);
8027 size = DECL_SIZE (object);
8030 /* Consider all floating-point types atomic and any types that that are
8031 represented by integers no wider than a machine word. */
8032 if (GET_MODE_CLASS (mode) == MODE_FLOAT
8033 || ((GET_MODE_CLASS (mode) == MODE_INT
8034 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
8035 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8036 return;
8038 /* For the moment, also allow anything that has an alignment equal
8039 to its size and which is smaller than a word. */
8040 if (size && TREE_CODE (size) == INTEGER_CST
8041 && compare_tree_int (size, align) == 0
8042 && align <= BITS_PER_WORD)
8043 return;
8045 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
8046 gnat_node = Next_Rep_Item (gnat_node))
8048 if (!comp_p && Nkind (gnat_node) == N_Pragma
8049 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8050 == Pragma_Atomic))
8051 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8052 else if (comp_p && Nkind (gnat_node) == N_Pragma
8053 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8054 == Pragma_Atomic_Components))
8055 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8058 if (comp_p)
8059 post_error_ne ("atomic access to component of & cannot be guaranteed",
8060 gnat_error_point, gnat_entity);
8061 else
8062 post_error_ne ("atomic access to & cannot be guaranteed",
8063 gnat_error_point, gnat_entity);
8067 /* Helper for the intrin compatibility checks family. Evaluate whether
8068 two types are definitely incompatible. */
8070 static bool
8071 intrin_types_incompatible_p (tree t1, tree t2)
8073 enum tree_code code;
8075 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8076 return false;
8078 if (TYPE_MODE (t1) != TYPE_MODE (t2))
8079 return true;
8081 if (TREE_CODE (t1) != TREE_CODE (t2))
8082 return true;
8084 code = TREE_CODE (t1);
8086 switch (code)
8088 case INTEGER_TYPE:
8089 case REAL_TYPE:
8090 return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8092 case POINTER_TYPE:
8093 case REFERENCE_TYPE:
8094 /* Assume designated types are ok. We'd need to account for char * and
8095 void * variants to do better, which could rapidly get messy and isn't
8096 clearly worth the effort. */
8097 return false;
8099 default:
8100 break;
8103 return false;
8106 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8107 on the Ada/builtin argument lists for the INB binding. */
8109 static bool
8110 intrin_arglists_compatible_p (intrin_binding_t * inb)
8112 tree ada_args = TYPE_ARG_TYPES (inb->ada_fntype);
8113 tree btin_args = TYPE_ARG_TYPES (inb->btin_fntype);
8115 /* Sequence position of the last argument we checked. */
8116 int argpos = 0;
8118 while (ada_args != 0 || btin_args != 0)
8120 tree ada_type, btin_type;
8122 /* If one list is shorter than the other, they fail to match. */
8123 if (ada_args == 0 || btin_args == 0)
8124 return false;
8126 ada_type = TREE_VALUE (ada_args);
8127 btin_type = TREE_VALUE (btin_args);
8129 /* If we're done with the Ada args and not with the internal builtin
8130 args, or the other way around, complain. */
8131 if (ada_type == void_type_node
8132 && btin_type != void_type_node)
8134 post_error ("?Ada arguments list too short!", inb->gnat_entity);
8135 return false;
8138 if (btin_type == void_type_node
8139 && ada_type != void_type_node)
8141 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8142 inb->gnat_entity, inb->gnat_entity, argpos);
8143 return false;
8146 /* Otherwise, check that types match for the current argument. */
8147 argpos ++;
8148 if (intrin_types_incompatible_p (ada_type, btin_type))
8150 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8151 inb->gnat_entity, inb->gnat_entity, argpos);
8152 return false;
8155 ada_args = TREE_CHAIN (ada_args);
8156 btin_args = TREE_CHAIN (btin_args);
8159 return true;
8162 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8163 on the Ada/builtin return values for the INB binding. */
8165 static bool
8166 intrin_return_compatible_p (intrin_binding_t * inb)
8168 tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8169 tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8171 /* Accept function imported as procedure, common and convenient. */
8172 if (VOID_TYPE_P (ada_return_type)
8173 && !VOID_TYPE_P (btin_return_type))
8174 return true;
8176 /* Check return types compatibility otherwise. Note that this
8177 handles void/void as well. */
8178 if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8180 post_error ("?intrinsic binding type mismatch on return value!",
8181 inb->gnat_entity);
8182 return false;
8185 return true;
8188 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8189 compatible. Issue relevant warnings when they are not.
8191 This is intended as a light check to diagnose the most obvious cases, not
8192 as a full fledged type compatiblity predicate. It is the programmer's
8193 responsibility to ensure correctness of the Ada declarations in Imports,
8194 especially when binding straight to a compiler internal. */
8196 static bool
8197 intrin_profiles_compatible_p (intrin_binding_t * inb)
8199 /* Check compatibility on return values and argument lists, each responsible
8200 for posting warnings as appropriate. Ensure use of the proper sloc for
8201 this purpose. */
8203 bool arglists_compatible_p, return_compatible_p;
8204 location_t saved_location = input_location;
8206 Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8208 return_compatible_p = intrin_return_compatible_p (inb);
8209 arglists_compatible_p = intrin_arglists_compatible_p (inb);
8211 input_location = saved_location;
8213 return return_compatible_p && arglists_compatible_p;
8216 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8217 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8218 specified size for this field. POS_LIST is a position list describing
8219 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8220 to this layout. */
8222 static tree
8223 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8224 tree size, tree pos_list, tree subst_list)
8226 tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8227 tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8228 unsigned int offset_align = tree_low_cst (TREE_VEC_ELT (t, 1), 1);
8229 tree new_pos, new_field;
8231 if (CONTAINS_PLACEHOLDER_P (pos))
8232 for (t = subst_list; t; t = TREE_CHAIN (t))
8233 pos = SUBSTITUTE_IN_EXPR (pos, TREE_PURPOSE (t), TREE_VALUE (t));
8235 /* If the position is now a constant, we can set it as the position of the
8236 field when we make it. Otherwise, we need to deal with it specially. */
8237 if (TREE_CONSTANT (pos))
8238 new_pos = bit_from_pos (pos, bitpos);
8239 else
8240 new_pos = NULL_TREE;
8242 new_field
8243 = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8244 size, new_pos, DECL_PACKED (old_field),
8245 !DECL_NONADDRESSABLE_P (old_field));
8247 if (!new_pos)
8249 normalize_offset (&pos, &bitpos, offset_align);
8250 DECL_FIELD_OFFSET (new_field) = pos;
8251 DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8252 SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8253 DECL_SIZE (new_field) = size;
8254 DECL_SIZE_UNIT (new_field)
8255 = convert (sizetype,
8256 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8257 layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8260 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8261 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8262 DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8263 TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8265 return new_field;
8268 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8270 static tree
8271 get_rep_part (tree record_type)
8273 tree field = TYPE_FIELDS (record_type);
8275 /* The REP part is the first field, internal, another record, and its name
8276 doesn't start with an underscore (i.e. is not generated by the FE). */
8277 if (DECL_INTERNAL_P (field)
8278 && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8279 && IDENTIFIER_POINTER (DECL_NAME (field)) [0] != '_')
8280 return field;
8282 return NULL_TREE;
8285 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8287 static tree
8288 get_variant_part (tree record_type)
8290 tree field;
8292 /* The variant part is the only internal field that is a qualified union. */
8293 for (field = TYPE_FIELDS (record_type); field; field = TREE_CHAIN (field))
8294 if (DECL_INTERNAL_P (field)
8295 && TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8296 return field;
8298 return NULL_TREE;
8301 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8302 the list of variants to be used and RECORD_TYPE is the type of the parent.
8303 POS_LIST is a position list describing the layout of fields present in
8304 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8305 layout. */
8307 static tree
8308 create_variant_part_from (tree old_variant_part, tree variant_list,
8309 tree record_type, tree pos_list, tree subst_list)
8311 tree offset = DECL_FIELD_OFFSET (old_variant_part);
8312 tree old_union_type = TREE_TYPE (old_variant_part);
8313 tree new_union_type, new_variant_part, t;
8314 tree union_field_list = NULL_TREE;
8316 /* First create the type of the variant part from that of the old one. */
8317 new_union_type = make_node (QUAL_UNION_TYPE);
8318 TYPE_NAME (new_union_type) = DECL_NAME (TYPE_NAME (old_union_type));
8320 /* If the position of the variant part is constant, subtract it from the
8321 size of the type of the parent to get the new size. This manual CSE
8322 reduces the code size when not optimizing. */
8323 if (TREE_CODE (offset) == INTEGER_CST)
8325 tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8326 tree first_bit = bit_from_pos (offset, bitpos);
8327 TYPE_SIZE (new_union_type)
8328 = size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8329 TYPE_SIZE_UNIT (new_union_type)
8330 = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8331 byte_from_pos (offset, bitpos));
8332 SET_TYPE_ADA_SIZE (new_union_type,
8333 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8334 first_bit));
8335 TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8336 relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8338 else
8339 copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8341 /* Now finish up the new variants and populate the union type. */
8342 for (t = variant_list; t; t = TREE_CHAIN (t))
8344 tree old_field = TREE_VEC_ELT (TREE_VALUE (t), 0), new_field;
8345 tree old_variant, old_variant_subpart, new_variant, field_list;
8347 /* Skip variants that don't belong to this nesting level. */
8348 if (DECL_CONTEXT (old_field) != old_union_type)
8349 continue;
8351 /* Retrieve the list of fields already added to the new variant. */
8352 new_variant = TREE_VEC_ELT (TREE_VALUE (t), 2);
8353 field_list = TYPE_FIELDS (new_variant);
8355 /* If the old variant had a variant subpart, we need to create a new
8356 variant subpart and add it to the field list. */
8357 old_variant = TREE_PURPOSE (t);
8358 old_variant_subpart = get_variant_part (old_variant);
8359 if (old_variant_subpart)
8361 tree new_variant_subpart
8362 = create_variant_part_from (old_variant_subpart, variant_list,
8363 new_variant, pos_list, subst_list);
8364 TREE_CHAIN (new_variant_subpart) = field_list;
8365 field_list = new_variant_subpart;
8368 /* Finish up the new variant and create the field. No need for debug
8369 info thanks to the XVS type. */
8370 finish_record_type (new_variant, nreverse (field_list), 2, false);
8371 compute_record_mode (new_variant);
8372 create_type_decl (TYPE_NAME (new_variant), new_variant, NULL,
8373 true, false, Empty);
8375 new_field
8376 = create_field_decl_from (old_field, new_variant, new_union_type,
8377 TYPE_SIZE (new_variant),
8378 pos_list, subst_list);
8379 DECL_QUALIFIER (new_field) = TREE_VEC_ELT (TREE_VALUE (t), 1);
8380 DECL_INTERNAL_P (new_field) = 1;
8381 TREE_CHAIN (new_field) = union_field_list;
8382 union_field_list = new_field;
8385 /* Finish up the union type and create the variant part. No need for debug
8386 info thanks to the XVS type. */
8387 finish_record_type (new_union_type, union_field_list, 2, false);
8388 compute_record_mode (new_union_type);
8389 create_type_decl (TYPE_NAME (new_union_type), new_union_type, NULL,
8390 true, false, Empty);
8392 new_variant_part
8393 = create_field_decl_from (old_variant_part, new_union_type, record_type,
8394 TYPE_SIZE (new_union_type),
8395 pos_list, subst_list);
8396 DECL_INTERNAL_P (new_variant_part) = 1;
8398 /* With multiple discriminants it is possible for an inner variant to be
8399 statically selected while outer ones are not; in this case, the list
8400 of fields of the inner variant is not flattened and we end up with a
8401 qualified union with a single member. Drop the useless container. */
8402 if (!TREE_CHAIN (union_field_list))
8404 DECL_CONTEXT (union_field_list) = record_type;
8405 DECL_FIELD_OFFSET (union_field_list)
8406 = DECL_FIELD_OFFSET (new_variant_part);
8407 DECL_FIELD_BIT_OFFSET (union_field_list)
8408 = DECL_FIELD_BIT_OFFSET (new_variant_part);
8409 SET_DECL_OFFSET_ALIGN (union_field_list,
8410 DECL_OFFSET_ALIGN (new_variant_part));
8411 new_variant_part = union_field_list;
8414 return new_variant_part;
8417 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8418 which are both RECORD_TYPE, after applying the substitutions described
8419 in SUBST_LIST. */
8421 static void
8422 copy_and_substitute_in_size (tree new_type, tree old_type, tree subst_list)
8424 tree t;
8426 TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8427 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8428 SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8429 TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8430 relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8432 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8433 for (t = subst_list; t; t = TREE_CHAIN (t))
8434 TYPE_SIZE (new_type)
8435 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8436 TREE_PURPOSE (t),
8437 TREE_VALUE (t));
8439 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8440 for (t = subst_list; t; t = TREE_CHAIN (t))
8441 TYPE_SIZE_UNIT (new_type)
8442 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8443 TREE_PURPOSE (t),
8444 TREE_VALUE (t));
8446 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8447 for (t = subst_list; t; t = TREE_CHAIN (t))
8448 SET_TYPE_ADA_SIZE
8449 (new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8450 TREE_PURPOSE (t),
8451 TREE_VALUE (t)));
8453 /* Finalize the size. */
8454 TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8455 TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8458 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8459 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8460 updated by replacing F with R.
8462 The function doesn't update the layout of the type, i.e. it assumes
8463 that the substitution is purely formal. That's why the replacement
8464 value R must itself contain a PLACEHOLDER_EXPR. */
8466 tree
8467 substitute_in_type (tree t, tree f, tree r)
8469 tree nt;
8471 gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8473 switch (TREE_CODE (t))
8475 case INTEGER_TYPE:
8476 case ENUMERAL_TYPE:
8477 case BOOLEAN_TYPE:
8478 case REAL_TYPE:
8480 /* First the domain types of arrays. */
8481 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8482 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8484 tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8485 tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8487 if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8488 return t;
8490 nt = copy_type (t);
8491 TYPE_GCC_MIN_VALUE (nt) = low;
8492 TYPE_GCC_MAX_VALUE (nt) = high;
8494 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8495 SET_TYPE_INDEX_TYPE
8496 (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8498 return nt;
8501 /* Then the subtypes. */
8502 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8503 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8505 tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8506 tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8508 if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8509 return t;
8511 nt = copy_type (t);
8512 SET_TYPE_RM_MIN_VALUE (nt, low);
8513 SET_TYPE_RM_MAX_VALUE (nt, high);
8515 return nt;
8518 return t;
8520 case COMPLEX_TYPE:
8521 nt = substitute_in_type (TREE_TYPE (t), f, r);
8522 if (nt == TREE_TYPE (t))
8523 return t;
8525 return build_complex_type (nt);
8527 case OFFSET_TYPE:
8528 case METHOD_TYPE:
8529 case FUNCTION_TYPE:
8530 case LANG_TYPE:
8531 /* These should never show up here. */
8532 gcc_unreachable ();
8534 case ARRAY_TYPE:
8536 tree component = substitute_in_type (TREE_TYPE (t), f, r);
8537 tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8539 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8540 return t;
8542 nt = build_array_type (component, domain);
8543 TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8544 TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8545 SET_TYPE_MODE (nt, TYPE_MODE (t));
8546 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8547 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8548 TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8549 TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8550 TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8551 return nt;
8554 case RECORD_TYPE:
8555 case UNION_TYPE:
8556 case QUAL_UNION_TYPE:
8558 bool changed_field = false;
8559 tree field;
8561 /* Start out with no fields, make new fields, and chain them
8562 in. If we haven't actually changed the type of any field,
8563 discard everything we've done and return the old type. */
8564 nt = copy_type (t);
8565 TYPE_FIELDS (nt) = NULL_TREE;
8567 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
8569 tree new_field = copy_node (field), new_n;
8571 new_n = substitute_in_type (TREE_TYPE (field), f, r);
8572 if (new_n != TREE_TYPE (field))
8574 TREE_TYPE (new_field) = new_n;
8575 changed_field = true;
8578 new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8579 if (new_n != DECL_FIELD_OFFSET (field))
8581 DECL_FIELD_OFFSET (new_field) = new_n;
8582 changed_field = true;
8585 /* Do the substitution inside the qualifier, if any. */
8586 if (TREE_CODE (t) == QUAL_UNION_TYPE)
8588 new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8589 if (new_n != DECL_QUALIFIER (field))
8591 DECL_QUALIFIER (new_field) = new_n;
8592 changed_field = true;
8596 DECL_CONTEXT (new_field) = nt;
8597 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8599 TREE_CHAIN (new_field) = TYPE_FIELDS (nt);
8600 TYPE_FIELDS (nt) = new_field;
8603 if (!changed_field)
8604 return t;
8606 TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8607 TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8608 TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8609 SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8610 return nt;
8613 default:
8614 return t;
8618 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8619 needed to represent the object. */
8621 tree
8622 rm_size (tree gnu_type)
8624 /* For integral types, we store the RM size explicitly. */
8625 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8626 return TYPE_RM_SIZE (gnu_type);
8628 /* Return the RM size of the actual data plus the size of the template. */
8629 if (TREE_CODE (gnu_type) == RECORD_TYPE
8630 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8631 return
8632 size_binop (PLUS_EXPR,
8633 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
8634 DECL_SIZE (TYPE_FIELDS (gnu_type)));
8636 /* For record types, we store the size explicitly. */
8637 if ((TREE_CODE (gnu_type) == RECORD_TYPE
8638 || TREE_CODE (gnu_type) == UNION_TYPE
8639 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
8640 && !TYPE_FAT_POINTER_P (gnu_type)
8641 && TYPE_ADA_SIZE (gnu_type))
8642 return TYPE_ADA_SIZE (gnu_type);
8644 /* For other types, this is just the size. */
8645 return TYPE_SIZE (gnu_type);
8648 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8649 fully-qualified name, possibly with type information encoding.
8650 Otherwise, return the name. */
8652 tree
8653 get_entity_name (Entity_Id gnat_entity)
8655 Get_Encoded_Name (gnat_entity);
8656 return get_identifier_with_length (Name_Buffer, Name_Len);
8659 /* Return an identifier representing the external name to be used for
8660 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8661 and the specified suffix. */
8663 tree
8664 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8666 Entity_Kind kind = Ekind (gnat_entity);
8668 if (suffix)
8670 String_Template temp = {1, strlen (suffix)};
8671 Fat_Pointer fp = {suffix, &temp};
8672 Get_External_Name_With_Suffix (gnat_entity, fp);
8674 else
8675 Get_External_Name (gnat_entity, 0);
8677 /* A variable using the Stdcall convention lives in a DLL. We adjust
8678 its name to use the jump table, the _imp__NAME contains the address
8679 for the NAME variable. */
8680 if ((kind == E_Variable || kind == E_Constant)
8681 && Has_Stdcall_Convention (gnat_entity))
8683 const int len = 6 + Name_Len;
8684 char *new_name = (char *) alloca (len + 1);
8685 strcpy (new_name, "_imp__");
8686 strcat (new_name, Name_Buffer);
8687 return get_identifier_with_length (new_name, len);
8690 return get_identifier_with_length (Name_Buffer, Name_Len);
8693 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8694 string, return a new IDENTIFIER_NODE that is the concatenation of
8695 the name followed by "___" and the specified suffix. */
8697 tree
8698 concat_name (tree gnu_name, const char *suffix)
8700 const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8701 char *new_name = (char *) alloca (len + 1);
8702 strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8703 strcat (new_name, "___");
8704 strcat (new_name, suffix);
8705 return get_identifier_with_length (new_name, len);
8708 #include "gt-ada-decl.h"