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