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