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