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