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