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