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