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