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