Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / ada / decl.c
blob6edda454a0c81742732112081b5368f52de76c9f
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2005, 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 2, 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 distributed with GNAT; see file COPYING. If not, write *
19 * to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, *
20 * MA 02111-1307, USA. *
21 * *
22 * GNAT was originally developed by the GNAT team at New York University. *
23 * Extensive contributions were provided by Ada Core Technologies Inc. *
24 * *
25 ****************************************************************************/
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "flags.h"
33 #include "toplev.h"
34 #include "convert.h"
35 #include "ggc.h"
36 #include "obstack.h"
37 #include "target.h"
39 #include "ada.h"
40 #include "types.h"
41 #include "atree.h"
42 #include "elists.h"
43 #include "namet.h"
44 #include "nlists.h"
45 #include "repinfo.h"
46 #include "snames.h"
47 #include "stringt.h"
48 #include "uintp.h"
49 #include "fe.h"
50 #include "sinfo.h"
51 #include "einfo.h"
52 #include "ada-tree.h"
53 #include "gigi.h"
55 /* Provide default values for the macros controlling stack checking.
56 This is copied from GCC's expr.h. */
58 #ifndef STACK_CHECK_BUILTIN
59 #define STACK_CHECK_BUILTIN 0
60 #endif
61 #ifndef STACK_CHECK_PROBE_INTERVAL
62 #define STACK_CHECK_PROBE_INTERVAL 4096
63 #endif
64 #ifndef STACK_CHECK_MAX_FRAME_SIZE
65 #define STACK_CHECK_MAX_FRAME_SIZE \
66 (STACK_CHECK_PROBE_INTERVAL - UNITS_PER_WORD)
67 #endif
68 #ifndef STACK_CHECK_MAX_VAR_SIZE
69 #define STACK_CHECK_MAX_VAR_SIZE (STACK_CHECK_MAX_FRAME_SIZE / 100)
70 #endif
72 /* These two variables are used to defer recursively expanding incomplete
73 types while we are processing a record or subprogram type. */
75 static int defer_incomplete_level = 0;
76 static struct incomplete
78 struct incomplete *next;
79 tree old_type;
80 Entity_Id full_type;
81 } *defer_incomplete_list = 0;
83 static void copy_alias_set (tree, tree);
84 static tree substitution_list (Entity_Id, Entity_Id, tree, bool);
85 static bool allocatable_size_p (tree, bool);
86 static void prepend_attributes (Entity_Id, struct attrib **);
87 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
88 static bool is_variable_size (tree);
89 static tree elaborate_expression_1 (Node_Id, Entity_Id, tree, tree,
90 bool, bool);
91 static tree make_packable_type (tree);
92 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
93 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
94 bool, bool);
95 static int compare_field_bitpos (const PTR, const PTR);
96 static Uint annotate_value (tree);
97 static void annotate_rep (Entity_Id, tree);
98 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
99 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
100 static void set_rm_size (Uint, tree, Entity_Id);
101 static tree make_type_from_size (tree, tree, bool);
102 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
103 static void check_ok_for_atomic (tree, Entity_Id, bool);
104 static int compatible_signatures_p (tree ftype1, tree ftype2);
106 /* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
107 GCC type corresponding to that entity. GNAT_ENTITY is assumed to
108 refer to an Ada type. */
110 tree
111 gnat_to_gnu_type (Entity_Id gnat_entity)
113 tree gnu_decl;
115 /* The back end never attempts to annotate generic types */
116 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
117 return void_type_node;
119 /* Convert the ada entity type into a GCC TYPE_DECL node. */
120 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
121 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
122 return TREE_TYPE (gnu_decl);
125 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
126 entity, this routine returns the equivalent GCC tree for that entity
127 (an ..._DECL node) and associates the ..._DECL node with the input GNAT
128 defining identifier.
130 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
131 initial value (in GCC tree form). This is optional for variables.
132 For renamed entities, GNU_EXPR gives the object being renamed.
134 DEFINITION is nonzero if this call is intended for a definition. This is
135 used for separate compilation where it necessary to know whether an
136 external declaration or a definition should be created if the GCC equivalent
137 was not created previously. The value of 1 is normally used for a non-zero
138 DEFINITION, but a value of 2 is used in special circumstances, defined in
139 the code. */
141 tree
142 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
144 tree gnu_entity_id;
145 tree gnu_type = NULL_TREE;
146 /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
147 GNAT tree. This node will be associated with the GNAT node by calling
148 the save_gnu_tree routine at the end of the `switch' statement. */
149 tree gnu_decl = NULL_TREE;
150 /* true if we have already saved gnu_decl as a gnat association. */
151 bool saved = false;
152 /* Nonzero if we incremented defer_incomplete_level. */
153 bool this_deferred = false;
154 /* Nonzero if we incremented force_global. */
155 bool this_global = false;
156 /* Nonzero if we should check to see if elaborated during processing. */
157 bool maybe_present = false;
158 /* Nonzero if we made GNU_DECL and its type here. */
159 bool this_made_decl = false;
160 struct attrib *attr_list = NULL;
161 bool debug_info_p = (Needs_Debug_Info (gnat_entity)
162 || debug_info_level == DINFO_LEVEL_VERBOSE);
163 Entity_Kind kind = Ekind (gnat_entity);
164 Entity_Id gnat_temp;
165 unsigned int esize
166 = ((Known_Esize (gnat_entity)
167 && UI_Is_In_Int_Range (Esize (gnat_entity)))
168 ? MIN (UI_To_Int (Esize (gnat_entity)),
169 IN (kind, Float_Kind)
170 ? fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE)
171 : IN (kind, Access_Kind) ? POINTER_SIZE * 2
172 : LONG_LONG_TYPE_SIZE)
173 : LONG_LONG_TYPE_SIZE);
174 tree gnu_size = 0;
175 bool imported_p
176 = ((Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)))
177 || From_With_Type (gnat_entity));
178 unsigned int align = 0;
180 /* Since a use of an Itype is a definition, process it as such if it
181 is not in a with'ed unit. */
183 if (!definition && Is_Itype (gnat_entity)
184 && !present_gnu_tree (gnat_entity)
185 && In_Extended_Main_Code_Unit (gnat_entity))
187 /* Ensure that we are in a subprogram mentioned in the Scope
188 chain of this entity, our current scope is global,
189 or that we encountered a task or entry (where we can't currently
190 accurately check scoping). */
191 if (!current_function_decl
192 || DECL_ELABORATION_PROC_P (current_function_decl))
194 process_type (gnat_entity);
195 return get_gnu_tree (gnat_entity);
198 for (gnat_temp = Scope (gnat_entity);
199 Present (gnat_temp); gnat_temp = Scope (gnat_temp))
201 if (Is_Type (gnat_temp))
202 gnat_temp = Underlying_Type (gnat_temp);
204 if (Ekind (gnat_temp) == E_Subprogram_Body)
205 gnat_temp
206 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
208 if (IN (Ekind (gnat_temp), Subprogram_Kind)
209 && Present (Protected_Body_Subprogram (gnat_temp)))
210 gnat_temp = Protected_Body_Subprogram (gnat_temp);
212 if (Ekind (gnat_temp) == E_Entry
213 || Ekind (gnat_temp) == E_Entry_Family
214 || Ekind (gnat_temp) == E_Task_Type
215 || (IN (Ekind (gnat_temp), Subprogram_Kind)
216 && present_gnu_tree (gnat_temp)
217 && (current_function_decl
218 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
220 process_type (gnat_entity);
221 return get_gnu_tree (gnat_entity);
225 /* This abort means the entity "gnat_entity" has an incorrect scope,
226 i.e. that its scope does not correspond to the subprogram in which
227 it is declared */
228 gcc_unreachable ();
231 /* If this is entity 0, something went badly wrong. */
232 gcc_assert (Present (gnat_entity));
234 /* If we've already processed this entity, return what we got last time.
235 If we are defining the node, we should not have already processed it.
236 In that case, we will abort below when we try to save a new GCC tree for
237 this object. We also need to handle the case of getting a dummy type
238 when a Full_View exists. */
240 if (present_gnu_tree (gnat_entity)
241 && (! definition
242 || (Is_Type (gnat_entity) && imported_p)))
244 gnu_decl = get_gnu_tree (gnat_entity);
246 if (TREE_CODE (gnu_decl) == TYPE_DECL
247 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
248 && IN (kind, Incomplete_Or_Private_Kind)
249 && Present (Full_View (gnat_entity)))
251 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
252 NULL_TREE, 0);
254 save_gnu_tree (gnat_entity, NULL_TREE, false);
255 save_gnu_tree (gnat_entity, gnu_decl, false);
258 return gnu_decl;
261 /* If this is a numeric or enumeral type, or an access type, a nonzero
262 Esize must be specified unless it was specified by the programmer. */
263 gcc_assert (!Unknown_Esize (gnat_entity)
264 || Has_Size_Clause (gnat_entity)
265 || (!IN (kind, Numeric_Kind) && !IN (kind, Enumeration_Kind)
266 && (!IN (kind, Access_Kind)
267 || kind == E_Access_Protected_Subprogram_Type
268 || kind == E_Access_Subtype)));
270 /* Likewise, RM_Size must be specified for all discrete and fixed-point
271 types. */
272 gcc_assert (!IN (kind, Discrete_Or_Fixed_Point_Kind)
273 || !Unknown_RM_Size (gnat_entity));
275 /* Get the name of the entity and set up the line number and filename of
276 the original definition for use in any decl we make. */
277 gnu_entity_id = get_entity_name (gnat_entity);
278 Sloc_to_locus (Sloc (gnat_entity), &input_location);
280 /* If we get here, it means we have not yet done anything with this
281 entity. If we are not defining it here, it must be external,
282 otherwise we should have defined it already. */
283 gcc_assert (definition || Is_Public (gnat_entity) || type_annotate_only
284 || kind == E_Discriminant || kind == E_Component
285 || kind == E_Label
286 || (kind == E_Constant && Present (Full_View (gnat_entity)))
287 || IN (kind, Type_Kind));
289 /* For cases when we are not defining (i.e., we are referencing from
290 another compilation unit) Public entities, show we are at global level
291 for the purpose of computing scopes. Don't do this for components or
292 discriminants since the relevant test is whether or not the record is
293 being defined. But do this for Imported functions or procedures in
294 all cases. */
295 if ((!definition && Is_Public (gnat_entity)
296 && !Is_Statically_Allocated (gnat_entity)
297 && kind != E_Discriminant && kind != E_Component)
298 || (Is_Imported (gnat_entity)
299 && (kind == E_Function || kind == E_Procedure)))
300 force_global++, this_global = true;
302 /* Handle any attributes directly attached to the entity. */
303 if (Has_Gigi_Rep_Item (gnat_entity))
304 prepend_attributes (gnat_entity, &attr_list);
306 /* Machine_Attributes on types are expected to be propagated to subtypes.
307 The corresponding Gigi_Rep_Items are only attached to the first subtype
308 though, so we handle the propagation here. */
309 if (Is_Type (gnat_entity) && Base_Type (gnat_entity) != gnat_entity
310 && !Is_First_Subtype (gnat_entity)
311 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
312 prepend_attributes (First_Subtype (Base_Type (gnat_entity)), &attr_list);
314 switch (kind)
316 case E_Constant:
317 /* If this is a use of a deferred constant, get its full
318 declaration. */
319 if (!definition && Present (Full_View (gnat_entity)))
321 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
322 gnu_expr, definition);
323 saved = true;
324 break;
327 /* If we have an external constant that we are not defining,
328 get the expression that is was defined to represent. We
329 may throw that expression away later if it is not a
330 constant.
331 Do not retrieve the expression if it is an aggregate, because
332 in complex instantiation contexts it may not be expanded */
334 if (!definition
335 && Present (Expression (Declaration_Node (gnat_entity)))
336 && !No_Initialization (Declaration_Node (gnat_entity))
337 && (Nkind (Expression (Declaration_Node (gnat_entity)))
338 != N_Aggregate))
339 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
341 /* Ignore deferred constant definitions; they are processed fully in the
342 front-end. For deferred constant references, get the full
343 definition. On the other hand, constants that are renamings are
344 handled like variable renamings. If No_Initialization is set, this is
345 not a deferred constant but a constant whose value is built
346 manually. */
348 if (definition && !gnu_expr
349 && !No_Initialization (Declaration_Node (gnat_entity))
350 && No (Renamed_Object (gnat_entity)))
352 gnu_decl = error_mark_node;
353 saved = true;
354 break;
356 else if (!definition && IN (kind, Incomplete_Or_Private_Kind)
357 && Present (Full_View (gnat_entity)))
359 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
360 NULL_TREE, 0);
361 saved = true;
362 break;
365 goto object;
367 case E_Exception:
368 /* We used to special case VMS exceptions here to directly map them to
369 their associated condition code. Since this code had to be masked
370 dynamically to strip off the severity bits, this caused trouble in
371 the GCC/ZCX case because the "type" pointers we store in the tables
372 have to be static. We now don't special case here anymore, and let
373 the regular processing take place, which leaves us with a regular
374 exception data object for VMS exceptions too. The condition code
375 mapping is taken care of by the front end and the bitmasking by the
376 runtime library. */
377 goto object;
379 case E_Discriminant:
380 case E_Component:
382 /* The GNAT record where the component was defined. */
383 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
385 /* If the variable is an inherited record component (in the case of
386 extended record types), just return the inherited entity, which
387 must be a FIELD_DECL. Likewise for discriminants.
388 For discriminants of untagged records which have explicit
389 stored discriminants, return the entity for the corresponding
390 stored discriminant. Also use Original_Record_Component
391 if the record has a private extension. */
393 if ((Base_Type (gnat_record) == gnat_record
394 || Ekind (Scope (gnat_entity)) == E_Private_Subtype
395 || Ekind (Scope (gnat_entity)) == E_Record_Subtype_With_Private
396 || Ekind (Scope (gnat_entity)) == E_Record_Type_With_Private)
397 && Present (Original_Record_Component (gnat_entity))
398 && Original_Record_Component (gnat_entity) != gnat_entity)
400 gnu_decl
401 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
402 gnu_expr, definition);
403 saved = true;
404 break;
407 /* If the enclosing record has explicit stored discriminants,
408 then it is an untagged record. If the Corresponding_Discriminant
409 is not empty then this must be a renamed discriminant and its
410 Original_Record_Component must point to the corresponding explicit
411 stored discriminant (i.e., we should have taken the previous
412 branch). */
414 else if (Present (Corresponding_Discriminant (gnat_entity))
415 && Is_Tagged_Type (gnat_record))
417 /* A tagged record has no explicit stored discriminants. */
419 gcc_assert (First_Discriminant (gnat_record)
420 == First_Stored_Discriminant (gnat_record));
421 gnu_decl
422 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
423 gnu_expr, definition);
424 saved = true;
425 break;
428 /* If the enclosing record has explicit stored discriminants,
429 then it is an untagged record. If the Corresponding_Discriminant
430 is not empty then this must be a renamed discriminant and its
431 Original_Record_Component must point to the corresponding explicit
432 stored discriminant (i.e., we should have taken the first
433 branch). */
435 else if (Present (Corresponding_Discriminant (gnat_entity))
436 && (First_Discriminant (gnat_record)
437 != First_Stored_Discriminant (gnat_record)))
438 gcc_unreachable ();
440 /* Otherwise, if we are not defining this and we have no GCC type
441 for the containing record, make one for it. Then we should
442 have made our own equivalent. */
443 else if (!definition && !present_gnu_tree (gnat_record))
445 /* ??? If this is in a record whose scope is a protected
446 type and we have an Original_Record_Component, use it.
447 This is a workaround for major problems in protected type
448 handling. */
450 Entity_Id Scop = Scope (Scope (gnat_entity));
451 if ((Is_Protected_Type (Scop)
452 || (Is_Private_Type (Scop)
453 && Present (Full_View (Scop))
454 && Is_Protected_Type (Full_View (Scop))))
455 && Present (Original_Record_Component (gnat_entity)))
457 gnu_decl
458 = gnat_to_gnu_entity (Original_Record_Component
459 (gnat_entity),
460 gnu_expr, definition);
461 saved = true;
462 break;
465 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
466 gnu_decl = get_gnu_tree (gnat_entity);
467 saved = true;
468 break;
471 else
472 /* Here we have no GCC type and this is a reference rather than a
473 definition. This should never happen. Most likely the cause is a
474 reference before declaration in the gnat tree for gnat_entity. */
475 gcc_unreachable ();
478 case E_Loop_Parameter:
479 case E_Out_Parameter:
480 case E_Variable:
482 /* Simple variables, loop variables, OUT parameters, and exceptions. */
483 object:
485 bool used_by_ref = false;
486 bool const_flag
487 = ((kind == E_Constant || kind == E_Variable)
488 && !Is_Statically_Allocated (gnat_entity)
489 && Is_True_Constant (gnat_entity)
490 && (((Nkind (Declaration_Node (gnat_entity))
491 == N_Object_Declaration)
492 && Present (Expression (Declaration_Node (gnat_entity))))
493 || Present (Renamed_Object (gnat_entity))));
494 bool inner_const_flag = const_flag;
495 bool static_p = Is_Statically_Allocated (gnat_entity);
496 tree gnu_ext_name = NULL_TREE;
498 if (Present (Renamed_Object (gnat_entity)) && !definition)
500 if (kind == E_Exception)
501 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
502 NULL_TREE, 0);
503 else
504 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
507 /* Get the type after elaborating the renamed object. */
508 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
510 /* If this is a loop variable, its type should be the base type.
511 This is because the code for processing a loop determines whether
512 a normal loop end test can be done by comparing the bounds of the
513 loop against those of the base type, which is presumed to be the
514 size used for computation. But this is not correct when the size
515 of the subtype is smaller than the type. */
516 if (kind == E_Loop_Parameter)
517 gnu_type = get_base_type (gnu_type);
519 /* Reject non-renamed objects whose types are unconstrained arrays or
520 any object whose type is a dummy type or VOID_TYPE. */
522 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
523 && No (Renamed_Object (gnat_entity)))
524 || TYPE_IS_DUMMY_P (gnu_type)
525 || TREE_CODE (gnu_type) == VOID_TYPE)
527 gcc_assert (type_annotate_only);
528 return error_mark_node;
531 /* If an alignment is specified, use it if valid. Note that
532 exceptions are objects but don't have alignments. We must do this
533 before we validate the size, since the alignment can affect the
534 size. */
535 if (kind != E_Exception && Known_Alignment (gnat_entity))
537 gcc_assert (Present (Alignment (gnat_entity)));
538 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
539 TYPE_ALIGN (gnu_type));
540 gnu_type = maybe_pad_type (gnu_type, NULL_TREE, align,
541 gnat_entity, "PAD", 0, definition, 1);
544 /* If we are defining the object, see if it has a Size value and
545 validate it if so. If we are not defining the object and a Size
546 clause applies, simply retrieve the value. We don't want to ignore
547 the clause and it is expected to have been validated already. Then
548 get the new type, if any. */
549 if (definition)
550 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
551 gnat_entity, VAR_DECL, false,
552 Has_Size_Clause (gnat_entity));
553 else if (Has_Size_Clause (gnat_entity))
554 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
556 if (gnu_size)
558 gnu_type
559 = make_type_from_size (gnu_type, gnu_size,
560 Has_Biased_Representation (gnat_entity));
562 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
563 gnu_size = NULL_TREE;
566 /* If this object has self-referential size, it must be a record with
567 a default value. We are supposed to allocate an object of the
568 maximum size in this case unless it is a constant with an
569 initializing expression, in which case we can get the size from
570 that. Note that the resulting size may still be a variable, so
571 this may end up with an indirect allocation. */
573 if (No (Renamed_Object (gnat_entity))
574 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
576 if (gnu_expr && kind == E_Constant)
577 gnu_size
578 = SUBSTITUTE_PLACEHOLDER_IN_EXPR
579 (TYPE_SIZE (TREE_TYPE (gnu_expr)), gnu_expr);
581 /* We may have no GNU_EXPR because No_Initialization is
582 set even though there's an Expression. */
583 else if (kind == E_Constant
584 && (Nkind (Declaration_Node (gnat_entity))
585 == N_Object_Declaration)
586 && Present (Expression (Declaration_Node (gnat_entity))))
587 gnu_size
588 = TYPE_SIZE (gnat_to_gnu_type
589 (Etype
590 (Expression (Declaration_Node (gnat_entity)))));
591 else
592 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
595 /* If the size is zero bytes, make it one byte since some linkers have
596 trouble with zero-sized objects. If the object will have a
597 template, that will make it nonzero so don't bother. Also avoid
598 doing that for an object renaming or an object with an address
599 clause, as we would lose useful information on the view size
600 (e.g. for null array slices) and we are not allocating the object
601 here anyway. */
602 if (((gnu_size && integer_zerop (gnu_size))
603 || (TYPE_SIZE (gnu_type) && integer_zerop (TYPE_SIZE (gnu_type))))
604 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
605 || !Is_Array_Type (Etype (gnat_entity)))
606 && !Present (Renamed_Object (gnat_entity))
607 && !Present (Address_Clause (gnat_entity)))
608 gnu_size = bitsize_unit_node;
610 /* If this is an atomic object with no specified size and alignment,
611 but where the size of the type is a constant, set the alignment to
612 the lowest power of two greater than the size, or to the
613 biggest meaningful alignment, whichever is smaller. */
615 if (Is_Atomic (gnat_entity) && !gnu_size && align == 0
616 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
618 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
619 || 0 <= compare_tree_int (TYPE_SIZE (gnu_type),
620 BIGGEST_ALIGNMENT))
621 align = BIGGEST_ALIGNMENT;
622 else
623 align = ((unsigned int) 1
624 << (floor_log2 (tree_low_cst
625 (TYPE_SIZE (gnu_type), 1) - 1)
626 + 1));
629 /* If the object is set to have atomic components, find the component
630 type and validate it.
632 ??? Note that we ignore Has_Volatile_Components on objects; it's
633 not at all clear what to do in that case. */
635 if (Has_Atomic_Components (gnat_entity))
637 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
638 ? TREE_TYPE (gnu_type) : gnu_type);
640 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
641 && TYPE_MULTI_ARRAY_P (gnu_inner))
642 gnu_inner = TREE_TYPE (gnu_inner);
644 check_ok_for_atomic (gnu_inner, gnat_entity, true);
647 /* Now check if the type of the object allows atomic access. Note
648 that we must test the type, even if this object has size and
649 alignment to allow such access, because we will be going
650 inside the padded record to assign to the object. We could fix
651 this by always copying via an intermediate value, but it's not
652 clear it's worth the effort. */
653 if (Is_Atomic (gnat_entity))
654 check_ok_for_atomic (gnu_type, gnat_entity, false);
656 /* If this is an aliased object with an unconstrained nominal subtype,
657 make a type that includes the template. */
658 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
659 && Is_Array_Type (Etype (gnat_entity))
660 && !type_annotate_only)
662 tree gnu_fat
663 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
664 tree gnu_temp_type
665 = TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat))));
667 gnu_type
668 = build_unc_object_type (gnu_temp_type, gnu_type,
669 concat_id_with_name (gnu_entity_id,
670 "UNC"));
673 #ifdef MINIMUM_ATOMIC_ALIGNMENT
674 /* If the size is a constant and no alignment is specified, force
675 the alignment to be the minimum valid atomic alignment. The
676 restriction on constant size avoids problems with variable-size
677 temporaries; if the size is variable, there's no issue with
678 atomic access. Also don't do this for a constant, since it isn't
679 necessary and can interfere with constant replacement. Finally,
680 do not do it for Out parameters since that creates an
681 size inconsistency with In parameters. */
682 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
683 && !FLOAT_TYPE_P (gnu_type)
684 && !const_flag && No (Renamed_Object (gnat_entity))
685 && !imported_p && No (Address_Clause (gnat_entity))
686 && kind != E_Out_Parameter
687 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
688 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
689 align = MINIMUM_ATOMIC_ALIGNMENT;
690 #endif
692 /* Make a new type with the desired size and alignment, if needed. */
693 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
694 "PAD", false, definition, true);
696 /* Make a volatile version of this object's type if we are to
697 make the object volatile. Note that 13.3(19) says that we
698 should treat other types of objects as volatile as well. */
699 if ((Treat_As_Volatile (gnat_entity)
700 || Is_Exported (gnat_entity)
701 || Is_Imported (gnat_entity)
702 || Present (Address_Clause (gnat_entity)))
703 && !TYPE_VOLATILE (gnu_type))
704 gnu_type = build_qualified_type (gnu_type,
705 (TYPE_QUALS (gnu_type)
706 | TYPE_QUAL_VOLATILE));
708 /* Convert the expression to the type of the object except in the
709 case where the object's type is unconstrained or the object's type
710 is a padded record whose field is of self-referential size. In
711 the former case, converting will generate unnecessary evaluations
712 of the CONSTRUCTOR to compute the size and in the latter case, we
713 want to only copy the actual data. */
714 if (gnu_expr
715 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
716 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
717 && !(TREE_CODE (gnu_type) == RECORD_TYPE
718 && TYPE_IS_PADDING_P (gnu_type)
719 && (CONTAINS_PLACEHOLDER_P
720 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
721 gnu_expr = convert (gnu_type, gnu_expr);
723 /* See if this is a renaming. If this is a constant renaming, treat
724 it as a normal variable whose initial value is what is being
725 renamed. We cannot do this if the type is unconstrained or
726 class-wide.
728 Otherwise, if what we are renaming is a reference, we can simply
729 return a stabilized version of that reference, after forcing any
730 SAVE_EXPRs to be evaluated. But, if this is at global level, we
731 can only do this if we know no SAVE_EXPRs will be made.
733 Otherwise, make this into a constant pointer to the object we are
734 to rename. */
736 if (Present (Renamed_Object (gnat_entity)))
738 /* If the renamed object had padding, strip off the reference
739 to the inner object and reset our type. */
740 if (TREE_CODE (gnu_expr) == COMPONENT_REF
741 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
742 == RECORD_TYPE)
743 && (TYPE_IS_PADDING_P
744 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
746 gnu_expr = TREE_OPERAND (gnu_expr, 0);
747 gnu_type = TREE_TYPE (gnu_expr);
750 if (const_flag
751 && !TREE_SIDE_EFFECTS (gnu_expr)
752 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
753 && TYPE_MODE (gnu_type) != BLKmode
754 && Ekind (Etype (gnat_entity)) != E_Class_Wide_Type
755 && !Is_Array_Type (Etype (gnat_entity)))
758 /* If this is a declaration or reference that we can stabilize,
759 just use that declaration or reference as this entity unless
760 the latter has to be materialized. */
761 else if ((DECL_P (gnu_expr) || REFERENCE_CLASS_P (gnu_expr))
762 && !Materialize_Entity (gnat_entity)
763 && (!global_bindings_p ()
764 || (staticp (gnu_expr)
765 && !TREE_SIDE_EFFECTS (gnu_expr))))
767 gnu_decl = gnat_stabilize_reference (gnu_expr, true);
768 save_gnu_tree (gnat_entity, gnu_decl, true);
769 saved = true;
770 break;
773 /* Otherwise, make this into a constant pointer to the object we
774 are to rename.
776 Stabilize it if we are not at the global level since in this
777 case the renaming evaluation may directly dereference the
778 initial value we make here instead of the pointer we will
779 assign it to. We don't want variables in the expression to be
780 evaluated every time the renaming is used, since the value of
781 these variables may change in between.
783 If we are at the global level and the value is not constant,
784 create_var_decl generates a mere elaboration assignment and
785 does not attach the initial expression to the declaration.
786 There is no possible direct initial-value dereference then. */
787 else
789 inner_const_flag = TREE_READONLY (gnu_expr);
790 const_flag = true;
791 gnu_type = build_reference_type (gnu_type);
792 gnu_expr = build_unary_op (ADDR_EXPR, gnu_type, gnu_expr);
794 if (!global_bindings_p ())
796 bool has_side_effects = TREE_SIDE_EFFECTS (gnu_expr);
798 gnu_expr = gnat_stabilize_reference (gnu_expr, true);
800 /* If the original expression had side effects, put a
801 SAVE_EXPR around this whole thing. */
802 if (has_side_effects)
803 gnu_expr = save_expr (gnu_expr);
805 add_stmt (gnu_expr);
808 gnu_size = NULL_TREE;
809 used_by_ref = true;
813 /* If this is an aliased object whose nominal subtype is unconstrained,
814 the object is a record that contains both the template and
815 the object. If there is an initializer, it will have already
816 been converted to the right type, but we need to create the
817 template if there is no initializer. */
818 else if (definition && TREE_CODE (gnu_type) == RECORD_TYPE
819 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
820 /* Beware that padding might have been introduced
821 via maybe_pad_type above. */
822 || (TYPE_IS_PADDING_P (gnu_type)
823 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
824 == RECORD_TYPE
825 && TYPE_CONTAINS_TEMPLATE_P
826 (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
827 && !gnu_expr)
829 tree template_field
830 = TYPE_IS_PADDING_P (gnu_type)
831 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
832 : TYPE_FIELDS (gnu_type);
834 gnu_expr
835 = gnat_build_constructor
836 (gnu_type,
837 tree_cons
838 (template_field,
839 build_template (TREE_TYPE (template_field),
840 TREE_TYPE (TREE_CHAIN (template_field)),
841 NULL_TREE),
842 NULL_TREE));
845 /* If this is a pointer and it does not have an initializing
846 expression, initialize it to NULL, unless the obect is
847 imported. */
848 if (definition
849 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
850 && !Is_Imported (gnat_entity) && !gnu_expr)
851 gnu_expr = integer_zero_node;
853 /* If we are defining the object and it has an Address clause we must
854 get the address expression from the saved GCC tree for the
855 object if the object has a Freeze_Node. Otherwise, we elaborate
856 the address expression here since the front-end has guaranteed
857 in that case that the elaboration has no effects. Note that
858 only the latter mechanism is currently in use. */
859 if (definition && Present (Address_Clause (gnat_entity)))
861 tree gnu_address
862 = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
863 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
865 save_gnu_tree (gnat_entity, NULL_TREE, false);
867 /* Ignore the size. It's either meaningless or was handled
868 above. */
869 gnu_size = NULL_TREE;
870 gnu_type = build_reference_type (gnu_type);
871 gnu_address = convert (gnu_type, gnu_address);
872 used_by_ref = true;
873 const_flag = !Is_Public (gnat_entity);
875 /* If we don't have an initializing expression for the underlying
876 variable, the initializing expression for the pointer is the
877 specified address. Otherwise, we have to make a COMPOUND_EXPR
878 to assign both the address and the initial value. */
879 if (!gnu_expr)
880 gnu_expr = gnu_address;
881 else
882 gnu_expr
883 = build2 (COMPOUND_EXPR, gnu_type,
884 build_binary_op
885 (MODIFY_EXPR, NULL_TREE,
886 build_unary_op (INDIRECT_REF, NULL_TREE,
887 gnu_address),
888 gnu_expr),
889 gnu_address);
892 /* If it has an address clause and we are not defining it, mark it
893 as an indirect object. Likewise for Stdcall objects that are
894 imported. */
895 if ((!definition && Present (Address_Clause (gnat_entity)))
896 || (Is_Imported (gnat_entity)
897 && Convention (gnat_entity) == Convention_Stdcall))
899 gnu_type = build_reference_type (gnu_type);
900 gnu_size = NULL_TREE;
901 used_by_ref = true;
904 /* If we are at top level and this object is of variable size,
905 make the actual type a hidden pointer to the real type and
906 make the initializer be a memory allocation and initialization.
907 Likewise for objects we aren't defining (presumed to be
908 external references from other packages), but there we do
909 not set up an initialization.
911 If the object's size overflows, make an allocator too, so that
912 Storage_Error gets raised. Note that we will never free
913 such memory, so we presume it never will get allocated. */
915 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
916 global_bindings_p () || !definition
917 || static_p)
918 || (gnu_size
919 && ! allocatable_size_p (gnu_size,
920 global_bindings_p () || !definition
921 || static_p)))
923 gnu_type = build_reference_type (gnu_type);
924 gnu_size = NULL_TREE;
925 used_by_ref = true;
926 const_flag = true;
928 /* In case this was a aliased object whose nominal subtype is
929 unconstrained, the pointer above will be a thin pointer and
930 build_allocator will automatically make the template.
932 If we have a template initializer only (that we made above),
933 pretend there is none and rely on what build_allocator creates
934 again anyway. Otherwise (if we have a full initializer), get
935 the data part and feed that to build_allocator. */
937 if (definition)
939 tree gnu_alloc_type = TREE_TYPE (gnu_type);
941 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
942 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
944 gnu_alloc_type
945 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
947 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
949 TREE_CHAIN (CONSTRUCTOR_ELTS (gnu_expr)) == NULL_TREE)
950 gnu_expr = 0;
951 else
952 gnu_expr
953 = build_component_ref
954 (gnu_expr, NULL_TREE,
955 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
956 false);
959 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
960 && TREE_CONSTANT_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
961 && !Is_Imported (gnat_entity))
962 post_error ("Storage_Error will be raised at run-time?",
963 gnat_entity);
965 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr,
966 gnu_type, 0, 0, gnat_entity);
968 else
970 gnu_expr = NULL_TREE;
971 const_flag = false;
975 /* If this object would go into the stack and has an alignment
976 larger than the default largest alignment, make a variable
977 to hold the "aligning type" with a modified initial value,
978 if any, then point to it and make that the value of this
979 variable, which is now indirect. */
980 if (!global_bindings_p () && !static_p && definition
981 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
983 tree gnu_new_type
984 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
985 TYPE_SIZE_UNIT (gnu_type));
986 tree gnu_new_var;
988 gnu_new_var
989 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
990 NULL_TREE, gnu_new_type, gnu_expr, false,
991 false, false, false, NULL, gnat_entity);
993 if (gnu_expr)
994 add_stmt_with_node
995 (build_binary_op (MODIFY_EXPR, NULL_TREE,
996 build_component_ref
997 (gnu_new_var, NULL_TREE,
998 TYPE_FIELDS (gnu_new_type), false),
999 gnu_expr),
1000 gnat_entity);
1002 gnu_type = build_reference_type (gnu_type);
1003 gnu_expr
1004 = build_unary_op
1005 (ADDR_EXPR, gnu_type,
1006 build_component_ref (gnu_new_var, NULL_TREE,
1007 TYPE_FIELDS (gnu_new_type), false));
1009 gnu_size = NULL_TREE;
1010 used_by_ref = true;
1011 const_flag = true;
1014 /* Convert the expression to the type of the object except in the
1015 case where the object's type is unconstrained or the object's type
1016 is a padded record whose field is of self-referential size. In
1017 the former case, converting will generate unnecessary evaluations
1018 of the CONSTRUCTOR to compute the size and in the latter case, we
1019 want to only copy the actual data. */
1020 if (gnu_expr
1021 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1022 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1023 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1024 && TYPE_IS_PADDING_P (gnu_type)
1025 && (CONTAINS_PLACEHOLDER_P
1026 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1027 gnu_expr = convert (gnu_type, gnu_expr);
1029 /* If this name is external or there was a name specified, use it,
1030 unless this is a VMS exception object since this would conflict
1031 with the symbol we need to export in addition. Don't use the
1032 Interface_Name if there is an address clause (see CD30005). */
1033 if (!Is_VMS_Exception (gnat_entity)
1034 && ((Present (Interface_Name (gnat_entity))
1035 && No (Address_Clause (gnat_entity)))
1036 || (Is_Public (gnat_entity)
1037 && (!Is_Imported (gnat_entity)
1038 || Is_Exported (gnat_entity)))))
1039 gnu_ext_name = create_concat_name (gnat_entity, 0);
1041 if (const_flag)
1043 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1044 | TYPE_QUAL_CONST));
1045 if (gnu_expr)
1046 gnu_expr = convert (gnu_type, gnu_expr);
1049 /* If this is constant initialized to a static constant and the
1050 object has an aggregrate type, force it to be statically
1051 allocated. */
1052 if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1053 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1054 && (AGGREGATE_TYPE_P (gnu_type)
1055 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1056 && TYPE_IS_PADDING_P (gnu_type))))
1057 static_p = true;
1059 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1060 gnu_expr, const_flag,
1061 Is_Public (gnat_entity),
1062 imported_p || !definition,
1063 static_p, attr_list, gnat_entity);
1064 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1065 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1067 /* If we have an address clause and we've made this indirect, it's
1068 not enough to merely mark the type as volatile since volatile
1069 references only conflict with other volatile references while this
1070 reference must conflict with all other references. So ensure that
1071 the dereferenced value has alias set 0. */
1072 if (Present (Address_Clause (gnat_entity)) && used_by_ref)
1073 DECL_POINTER_ALIAS_SET (gnu_decl) = 0;
1075 if (definition && DECL_SIZE (gnu_decl)
1076 && get_block_jmpbuf_decl ()
1077 && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1078 || (flag_stack_check && !STACK_CHECK_BUILTIN
1079 && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1080 STACK_CHECK_MAX_VAR_SIZE))))
1081 add_stmt_with_node (build_call_1_expr
1082 (update_setjmp_buf_decl,
1083 build_unary_op (ADDR_EXPR, NULL_TREE,
1084 get_block_jmpbuf_decl ())),
1085 gnat_entity);
1087 /* If this is a public constant or we're not optimizing and we're not
1088 making a VAR_DECL for it, make one just for export or debugger
1089 use. Likewise if the address is taken or if the object or type is
1090 aliased. */
1091 if (definition && TREE_CODE (gnu_decl) == CONST_DECL
1092 && (Is_Public (gnat_entity)
1093 || optimize == 0
1094 || Address_Taken (gnat_entity)
1095 || Is_Aliased (gnat_entity)
1096 || Is_Aliased (Etype (gnat_entity))))
1098 tree gnu_corr_var
1099 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1100 gnu_expr, false, Is_Public (gnat_entity),
1101 false, static_p, NULL, gnat_entity);
1103 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1106 /* If this is declared in a block that contains an block with an
1107 exception handler, we must force this variable in memory to
1108 suppress an invalid optimization. */
1109 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1110 && Exception_Mechanism != GCC_ZCX)
1111 TREE_ADDRESSABLE (gnu_decl) = 1;
1113 /* Back-annotate the Alignment of the object if not already in the
1114 tree. Likewise for Esize if the object is of a constant size.
1115 But if the "object" is actually a pointer to an object, the
1116 alignment and size are the same as teh type, so don't back-annotate
1117 the values for the pointer. */
1118 if (!used_by_ref && Unknown_Alignment (gnat_entity))
1119 Set_Alignment (gnat_entity,
1120 UI_From_Int (DECL_ALIGN (gnu_decl) / BITS_PER_UNIT));
1122 if (!used_by_ref && Unknown_Esize (gnat_entity)
1123 && DECL_SIZE (gnu_decl))
1125 tree gnu_back_size = DECL_SIZE (gnu_decl);
1127 if (TREE_CODE (TREE_TYPE (gnu_decl)) == RECORD_TYPE
1128 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_decl)))
1129 gnu_back_size
1130 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN
1131 (TYPE_FIELDS (TREE_TYPE (gnu_decl)))));
1133 Set_Esize (gnat_entity, annotate_value (gnu_back_size));
1136 break;
1138 case E_Void:
1139 /* Return a TYPE_DECL for "void" that we previously made. */
1140 gnu_decl = void_type_decl_node;
1141 break;
1143 case E_Enumeration_Type:
1144 /* A special case, for the types Character and Wide_Character in
1145 Standard, we do not list all the literals. So if the literals
1146 are not specified, make this an unsigned type. */
1147 if (No (First_Literal (gnat_entity)))
1149 gnu_type = make_unsigned_type (esize);
1150 break;
1153 /* Normal case of non-character type, or non-Standard character type */
1155 /* Here we have a list of enumeral constants in First_Literal.
1156 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1157 the list to be places into TYPE_FIELDS. Each node in the list
1158 is a TREE_LIST node whose TREE_VALUE is the literal name
1159 and whose TREE_PURPOSE is the value of the literal.
1161 Esize contains the number of bits needed to represent the enumeral
1162 type, Type_Low_Bound also points to the first literal and
1163 Type_High_Bound points to the last literal. */
1165 Entity_Id gnat_literal;
1166 tree gnu_literal_list = NULL_TREE;
1168 if (Is_Unsigned_Type (gnat_entity))
1169 gnu_type = make_unsigned_type (esize);
1170 else
1171 gnu_type = make_signed_type (esize);
1173 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1175 for (gnat_literal = First_Literal (gnat_entity);
1176 Present (gnat_literal);
1177 gnat_literal = Next_Literal (gnat_literal))
1179 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1180 gnu_type);
1181 tree gnu_literal
1182 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1183 gnu_type, gnu_value, true, false, false,
1184 false, NULL, gnat_literal);
1186 save_gnu_tree (gnat_literal, gnu_literal, false);
1187 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1188 gnu_value, gnu_literal_list);
1191 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1193 /* Note that the bounds are updated at the end of this function
1194 because to avoid an infinite recursion when we get the bounds of
1195 this type, since those bounds are objects of this type. */
1197 break;
1199 case E_Signed_Integer_Type:
1200 case E_Ordinary_Fixed_Point_Type:
1201 case E_Decimal_Fixed_Point_Type:
1202 /* For integer types, just make a signed type the appropriate number
1203 of bits. */
1204 gnu_type = make_signed_type (esize);
1205 break;
1207 case E_Modular_Integer_Type:
1208 /* For modular types, make the unsigned type of the proper number of
1209 bits and then set up the modulus, if required. */
1211 enum machine_mode mode;
1212 tree gnu_modulus;
1213 tree gnu_high = 0;
1215 if (Is_Packed_Array_Type (gnat_entity))
1216 esize = UI_To_Int (RM_Size (gnat_entity));
1218 /* Find the smallest mode at least ESIZE bits wide and make a class
1219 using that mode. */
1221 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1222 GET_MODE_BITSIZE (mode) < esize;
1223 mode = GET_MODE_WIDER_MODE (mode))
1226 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1227 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1228 = Is_Packed_Array_Type (gnat_entity);
1230 /* Get the modulus in this type. If it overflows, assume it is because
1231 it is equal to 2**Esize. Note that there is no overflow checking
1232 done on unsigned type, so we detect the overflow by looking for
1233 a modulus of zero, which is otherwise invalid. */
1234 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1236 if (!integer_zerop (gnu_modulus))
1238 TYPE_MODULAR_P (gnu_type) = 1;
1239 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1240 gnu_high = fold (build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1241 convert (gnu_type, integer_one_node)));
1244 /* If we have to set TYPE_PRECISION different from its natural value,
1245 make a subtype to do do. Likewise if there is a modulus and
1246 it is not one greater than TYPE_MAX_VALUE. */
1247 if (TYPE_PRECISION (gnu_type) != esize
1248 || (TYPE_MODULAR_P (gnu_type)
1249 && !tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1251 tree gnu_subtype = make_node (INTEGER_TYPE);
1253 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1254 TREE_TYPE (gnu_subtype) = gnu_type;
1255 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1256 TYPE_MAX_VALUE (gnu_subtype)
1257 = TYPE_MODULAR_P (gnu_type)
1258 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1259 TYPE_PRECISION (gnu_subtype) = esize;
1260 TYPE_UNSIGNED (gnu_subtype) = 1;
1261 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1262 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1263 = Is_Packed_Array_Type (gnat_entity);
1264 layout_type (gnu_subtype);
1266 gnu_type = gnu_subtype;
1269 break;
1271 case E_Signed_Integer_Subtype:
1272 case E_Enumeration_Subtype:
1273 case E_Modular_Integer_Subtype:
1274 case E_Ordinary_Fixed_Point_Subtype:
1275 case E_Decimal_Fixed_Point_Subtype:
1277 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1278 that we do not want to call build_range_type since we would
1279 like each subtype node to be distinct. This will be important
1280 when memory aliasing is implemented.
1282 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1283 parent type; this fact is used by the arithmetic conversion
1284 functions.
1286 We elaborate the Ancestor_Subtype if it is not in the current
1287 unit and one of our bounds is non-static. We do this to ensure
1288 consistent naming in the case where several subtypes share the same
1289 bounds by always elaborating the first such subtype first, thus
1290 using its name. */
1292 if (definition == 0
1293 && Present (Ancestor_Subtype (gnat_entity))
1294 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1295 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1296 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1297 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1298 gnu_expr, definition);
1300 gnu_type = make_node (INTEGER_TYPE);
1301 if (Is_Packed_Array_Type (gnat_entity))
1303 esize = UI_To_Int (RM_Size (gnat_entity));
1304 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1307 TYPE_PRECISION (gnu_type) = esize;
1308 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1310 TYPE_MIN_VALUE (gnu_type)
1311 = convert (TREE_TYPE (gnu_type),
1312 elaborate_expression (Type_Low_Bound (gnat_entity),
1313 gnat_entity,
1314 get_identifier ("L"), definition, 1,
1315 Needs_Debug_Info (gnat_entity)));
1317 TYPE_MAX_VALUE (gnu_type)
1318 = convert (TREE_TYPE (gnu_type),
1319 elaborate_expression (Type_High_Bound (gnat_entity),
1320 gnat_entity,
1321 get_identifier ("U"), definition, 1,
1322 Needs_Debug_Info (gnat_entity)));
1324 /* One of the above calls might have caused us to be elaborated,
1325 so don't blow up if so. */
1326 if (present_gnu_tree (gnat_entity))
1328 maybe_present = true;
1329 break;
1332 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1333 = Has_Biased_Representation (gnat_entity);
1335 /* This should be an unsigned type if the lower bound is constant
1336 and non-negative or if the base type is unsigned; a signed type
1337 otherwise. */
1338 TYPE_UNSIGNED (gnu_type)
1339 = (TYPE_UNSIGNED (TREE_TYPE (gnu_type))
1340 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1341 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1342 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1343 || Is_Unsigned_Type (gnat_entity));
1345 layout_type (gnu_type);
1347 /* If the type we are dealing with is to represent a packed array,
1348 we need to have the bits left justified on big-endian targets
1349 and right justified on little-endian targets. We also need to
1350 ensure that when the value is read (e.g. for comparison of two
1351 such values), we only get the good bits, since the unused bits
1352 are uninitialized. Both goals are accomplished by wrapping the
1353 modular value in an enclosing struct. */
1354 if (Is_Packed_Array_Type (gnat_entity))
1356 tree gnu_field_type = gnu_type;
1357 tree gnu_field;
1359 TYPE_RM_SIZE_NUM (gnu_field_type)
1360 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1361 gnu_type = make_node (RECORD_TYPE);
1362 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1363 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1364 TYPE_PACKED (gnu_type) = 1;
1366 /* Create a stripped-down declaration of the original type, mainly
1367 for debugging. */
1368 create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1369 NULL, true, debug_info_p, gnat_entity);
1371 /* Don't notify the field as "addressable", since we won't be taking
1372 it's address and it would prevent create_field_decl from making a
1373 bitfield. */
1374 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1375 gnu_field_type, gnu_type, 1, 0, 0, 0);
1377 finish_record_type (gnu_type, gnu_field, false, false);
1378 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1379 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1382 break;
1384 case E_Floating_Point_Type:
1385 /* If this is a VAX floating-point type, use an integer of the proper
1386 size. All the operations will be handled with ASM statements. */
1387 if (Vax_Float (gnat_entity))
1389 gnu_type = make_signed_type (esize);
1390 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1391 SET_TYPE_DIGITS_VALUE (gnu_type,
1392 UI_To_gnu (Digits_Value (gnat_entity),
1393 sizetype));
1394 break;
1397 /* The type of the Low and High bounds can be our type if this is
1398 a type from Standard, so set them at the end of the function. */
1399 gnu_type = make_node (REAL_TYPE);
1400 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1401 layout_type (gnu_type);
1402 break;
1404 case E_Floating_Point_Subtype:
1405 if (Vax_Float (gnat_entity))
1407 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1408 break;
1412 if (definition == 0
1413 && Present (Ancestor_Subtype (gnat_entity))
1414 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1415 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1416 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1417 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1418 gnu_expr, definition);
1420 gnu_type = make_node (REAL_TYPE);
1421 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1422 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1424 TYPE_MIN_VALUE (gnu_type)
1425 = convert (TREE_TYPE (gnu_type),
1426 elaborate_expression (Type_Low_Bound (gnat_entity),
1427 gnat_entity, get_identifier ("L"),
1428 definition, 1,
1429 Needs_Debug_Info (gnat_entity)));
1431 TYPE_MAX_VALUE (gnu_type)
1432 = convert (TREE_TYPE (gnu_type),
1433 elaborate_expression (Type_High_Bound (gnat_entity),
1434 gnat_entity, get_identifier ("U"),
1435 definition, 1,
1436 Needs_Debug_Info (gnat_entity)));
1438 /* One of the above calls might have caused us to be elaborated,
1439 so don't blow up if so. */
1440 if (present_gnu_tree (gnat_entity))
1442 maybe_present = true;
1443 break;
1446 layout_type (gnu_type);
1448 break;
1450 /* Array and String Types and Subtypes
1452 Unconstrained array types are represented by E_Array_Type and
1453 constrained array types are represented by E_Array_Subtype. There
1454 are no actual objects of an unconstrained array type; all we have
1455 are pointers to that type.
1457 The following fields are defined on array types and subtypes:
1459 Component_Type Component type of the array.
1460 Number_Dimensions Number of dimensions (an int).
1461 First_Index Type of first index. */
1463 case E_String_Type:
1464 case E_Array_Type:
1466 tree gnu_template_fields = NULL_TREE;
1467 tree gnu_template_type = make_node (RECORD_TYPE);
1468 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1469 tree gnu_fat_type = make_node (RECORD_TYPE);
1470 int ndim = Number_Dimensions (gnat_entity);
1471 int firstdim
1472 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1473 int nextdim
1474 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1475 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1476 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1477 tree gnu_comp_size = 0;
1478 tree gnu_max_size = size_one_node;
1479 tree gnu_max_size_unit;
1480 int index;
1481 Entity_Id gnat_ind_subtype;
1482 Entity_Id gnat_ind_base_subtype;
1483 tree gnu_template_reference;
1484 tree tem;
1486 TYPE_NAME (gnu_template_type)
1487 = create_concat_name (gnat_entity, "XUB");
1488 TYPE_NAME (gnu_fat_type) = create_concat_name (gnat_entity, "XUP");
1489 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1490 TYPE_READONLY (gnu_template_type) = 1;
1492 /* Make a node for the array. If we are not defining the array
1493 suppress expanding incomplete types and save the node as the type
1494 for GNAT_ENTITY. */
1495 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1496 if (!definition)
1498 defer_incomplete_level++;
1499 this_deferred = this_made_decl = true;
1500 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
1501 !Comes_From_Source (gnat_entity),
1502 debug_info_p, gnat_entity);
1503 save_gnu_tree (gnat_entity, gnu_decl, false);
1504 saved = true;
1507 /* Build the fat pointer type. Use a "void *" object instead of
1508 a pointer to the array type since we don't have the array type
1509 yet (it will reference the fat pointer via the bounds). */
1510 tem = chainon (chainon (NULL_TREE,
1511 create_field_decl (get_identifier ("P_ARRAY"),
1512 ptr_void_type_node,
1513 gnu_fat_type, 0, 0, 0, 0)),
1514 create_field_decl (get_identifier ("P_BOUNDS"),
1515 gnu_ptr_template,
1516 gnu_fat_type, 0, 0, 0, 0));
1518 /* Make sure we can put this into a register. */
1519 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1520 finish_record_type (gnu_fat_type, tem, false, true);
1522 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1523 is the fat pointer. This will be used to access the individual
1524 fields once we build them. */
1525 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1526 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1527 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1528 gnu_template_reference
1529 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1530 TREE_READONLY (gnu_template_reference) = 1;
1532 /* Now create the GCC type for each index and add the fields for
1533 that index to the template. */
1534 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1535 gnat_ind_base_subtype
1536 = First_Index (Implementation_Base_Type (gnat_entity));
1537 index < ndim && index >= 0;
1538 index += nextdim,
1539 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1540 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1542 char field_name[10];
1543 tree gnu_ind_subtype
1544 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1545 tree gnu_base_subtype
1546 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1547 tree gnu_base_min
1548 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1549 tree gnu_base_max
1550 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1551 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1553 /* Make the FIELD_DECLs for the minimum and maximum of this
1554 type and then make extractions of that field from the
1555 template. */
1556 sprintf (field_name, "LB%d", index);
1557 gnu_min_field = create_field_decl (get_identifier (field_name),
1558 gnu_ind_subtype,
1559 gnu_template_type, 0, 0, 0, 0);
1560 field_name[0] = 'U';
1561 gnu_max_field = create_field_decl (get_identifier (field_name),
1562 gnu_ind_subtype,
1563 gnu_template_type, 0, 0, 0, 0);
1565 Sloc_to_locus (Sloc (gnat_entity),
1566 &DECL_SOURCE_LOCATION (gnu_min_field));
1567 Sloc_to_locus (Sloc (gnat_entity),
1568 &DECL_SOURCE_LOCATION (gnu_max_field));
1569 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1571 /* We can't use build_component_ref here since the template
1572 type isn't complete yet. */
1573 gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1574 gnu_template_reference, gnu_min_field,
1575 NULL_TREE);
1576 gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1577 gnu_template_reference, gnu_max_field,
1578 NULL_TREE);
1579 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1581 /* Make a range type with the new ranges, but using
1582 the Ada subtype. Then we convert to sizetype. */
1583 gnu_index_types[index]
1584 = create_index_type (convert (sizetype, gnu_min),
1585 convert (sizetype, gnu_max),
1586 build_range_type (gnu_ind_subtype,
1587 gnu_min, gnu_max));
1588 /* Update the maximum size of the array, in elements. */
1589 gnu_max_size
1590 = size_binop (MULT_EXPR, gnu_max_size,
1591 size_binop (PLUS_EXPR, size_one_node,
1592 size_binop (MINUS_EXPR, gnu_base_max,
1593 gnu_base_min)));
1595 TYPE_NAME (gnu_index_types[index])
1596 = create_concat_name (gnat_entity, field_name);
1599 for (index = 0; index < ndim; index++)
1600 gnu_template_fields
1601 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1603 /* Install all the fields into the template. */
1604 finish_record_type (gnu_template_type, gnu_template_fields,
1605 false, false);
1606 TYPE_READONLY (gnu_template_type) = 1;
1608 /* Now make the array of arrays and update the pointer to the array
1609 in the fat pointer. Note that it is the first field. */
1611 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1613 /* Get and validate any specified Component_Size, but if Packed,
1614 ignore it since the front end will have taken care of it. */
1615 gnu_comp_size
1616 = validate_size (Component_Size (gnat_entity), tem,
1617 gnat_entity,
1618 (Is_Bit_Packed_Array (gnat_entity)
1619 ? TYPE_DECL : VAR_DECL),
1620 true, Has_Component_Size_Clause (gnat_entity));
1622 if (Has_Atomic_Components (gnat_entity))
1623 check_ok_for_atomic (tem, gnat_entity, true);
1625 /* If the component type is a RECORD_TYPE that has a self-referential
1626 size, use the maxium size. */
1627 if (!gnu_comp_size && TREE_CODE (tem) == RECORD_TYPE
1628 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1629 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1631 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
1633 tem = make_type_from_size (tem, gnu_comp_size, false);
1634 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1635 "C_PAD", false, definition, true);
1638 if (Has_Volatile_Components (gnat_entity))
1639 tem = build_qualified_type (tem,
1640 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1642 /* If Component_Size is not already specified, annotate it with the
1643 size of the component. */
1644 if (Unknown_Component_Size (gnat_entity))
1645 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1647 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1648 size_binop (MULT_EXPR, gnu_max_size,
1649 TYPE_SIZE_UNIT (tem)));
1650 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1651 size_binop (MULT_EXPR,
1652 convert (bitsizetype,
1653 gnu_max_size),
1654 TYPE_SIZE (tem)));
1656 for (index = ndim - 1; index >= 0; index--)
1658 tem = build_array_type (tem, gnu_index_types[index]);
1659 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1661 /* If the type below this an multi-array type, then this
1662 does not not have aliased components.
1664 ??? Otherwise, for now, we say that any component of aggregate
1665 type is addressable because the front end may take 'Reference
1666 of it. But we have to make it addressable if it must be passed
1667 by reference or it that is the default. */
1668 TYPE_NONALIASED_COMPONENT (tem)
1669 = ((TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
1670 && TYPE_MULTI_ARRAY_P (TREE_TYPE (tem))) ? 1
1671 : (!Has_Aliased_Components (gnat_entity)
1672 && !AGGREGATE_TYPE_P (TREE_TYPE (tem))));
1675 /* If an alignment is specified, use it if valid. But ignore it for
1676 types that represent the unpacked base type for packed arrays. */
1677 if (No (Packed_Array_Type (gnat_entity))
1678 && Known_Alignment (gnat_entity))
1680 gcc_assert (Present (Alignment (gnat_entity)));
1681 TYPE_ALIGN (tem)
1682 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1683 TYPE_ALIGN (tem));
1686 TYPE_CONVENTION_FORTRAN_P (tem)
1687 = (Convention (gnat_entity) == Convention_Fortran);
1688 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1690 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1691 corresponding fat pointer. */
1692 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1693 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1694 TYPE_MODE (gnu_type) = BLKmode;
1695 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1696 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
1698 /* If the maximum size doesn't overflow, use it. */
1699 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1700 && !TREE_OVERFLOW (gnu_max_size))
1701 TYPE_SIZE (tem)
1702 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1703 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
1704 && !TREE_OVERFLOW (gnu_max_size_unit))
1705 TYPE_SIZE_UNIT (tem)
1706 = size_binop (MIN_EXPR, gnu_max_size_unit,
1707 TYPE_SIZE_UNIT (tem));
1709 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1710 tem, NULL, !Comes_From_Source (gnat_entity),
1711 debug_info_p, gnat_entity);
1713 /* Create a record type for the object and its template and
1714 set the template at a negative offset. */
1715 tem = build_unc_object_type (gnu_template_type, tem,
1716 create_concat_name (gnat_entity, "XUT"));
1717 DECL_FIELD_OFFSET (TYPE_FIELDS (tem))
1718 = size_binop (MINUS_EXPR, size_zero_node,
1719 byte_position (TREE_CHAIN (TYPE_FIELDS (tem))));
1720 DECL_FIELD_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem))) = size_zero_node;
1721 DECL_FIELD_BIT_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem)))
1722 = bitsize_zero_node;
1723 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
1724 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1726 /* Give the thin pointer type a name. */
1727 create_type_decl (create_concat_name (gnat_entity, "XUX"),
1728 build_pointer_type (tem), NULL,
1729 !Comes_From_Source (gnat_entity), debug_info_p,
1730 gnat_entity);
1732 break;
1734 case E_String_Subtype:
1735 case E_Array_Subtype:
1737 /* This is the actual data type for array variables. Multidimensional
1738 arrays are implemented in the gnu tree as arrays of arrays. Note
1739 that for the moment arrays which have sparse enumeration subtypes as
1740 index components create sparse arrays, which is obviously space
1741 inefficient but so much easier to code for now.
1743 Also note that the subtype never refers to the unconstrained
1744 array type, which is somewhat at variance with Ada semantics.
1746 First check to see if this is simply a renaming of the array
1747 type. If so, the result is the array type. */
1749 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1750 if (!Is_Constrained (gnat_entity))
1751 break;
1752 else
1754 int index;
1755 int array_dim = Number_Dimensions (gnat_entity);
1756 int first_dim
1757 = ((Convention (gnat_entity) == Convention_Fortran)
1758 ? array_dim - 1 : 0);
1759 int next_dim
1760 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
1761 Entity_Id gnat_ind_subtype;
1762 Entity_Id gnat_ind_base_subtype;
1763 tree gnu_base_type = gnu_type;
1764 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
1765 tree gnu_comp_size = NULL_TREE;
1766 tree gnu_max_size = size_one_node;
1767 tree gnu_max_size_unit;
1768 bool need_index_type_struct = false;
1769 bool max_overflow = false;
1771 /* First create the gnu types for each index. Create types for
1772 debugging information to point to the index types if the
1773 are not integer types, have variable bounds, or are
1774 wider than sizetype. */
1776 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1777 gnat_ind_base_subtype
1778 = First_Index (Implementation_Base_Type (gnat_entity));
1779 index < array_dim && index >= 0;
1780 index += next_dim,
1781 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1782 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1784 tree gnu_index_subtype
1785 = get_unpadded_type (Etype (gnat_ind_subtype));
1786 tree gnu_min
1787 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
1788 tree gnu_max
1789 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
1790 tree gnu_base_subtype
1791 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1792 tree gnu_base_min
1793 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1794 tree gnu_base_max
1795 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1796 tree gnu_base_type = get_base_type (gnu_base_subtype);
1797 tree gnu_base_base_min
1798 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
1799 tree gnu_base_base_max
1800 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
1801 tree gnu_high;
1802 tree gnu_this_max;
1804 /* If the minimum and maximum values both overflow in
1805 SIZETYPE, but the difference in the original type
1806 does not overflow in SIZETYPE, ignore the overflow
1807 indications. */
1808 if ((TYPE_PRECISION (gnu_index_subtype)
1809 > TYPE_PRECISION (sizetype)
1810 || TYPE_UNSIGNED (gnu_index_subtype)
1811 != TYPE_UNSIGNED (sizetype))
1812 && TREE_CODE (gnu_min) == INTEGER_CST
1813 && TREE_CODE (gnu_max) == INTEGER_CST
1814 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
1815 && (!TREE_OVERFLOW
1816 (fold (build2 (MINUS_EXPR, gnu_index_subtype,
1817 TYPE_MAX_VALUE (gnu_index_subtype),
1818 TYPE_MIN_VALUE (gnu_index_subtype))))))
1819 TREE_OVERFLOW (gnu_min) = TREE_OVERFLOW (gnu_max)
1820 = TREE_CONSTANT_OVERFLOW (gnu_min)
1821 = TREE_CONSTANT_OVERFLOW (gnu_max) = 0;
1823 /* Similarly, if the range is null, use bounds of 1..0 for
1824 the sizetype bounds. */
1825 else if ((TYPE_PRECISION (gnu_index_subtype)
1826 > TYPE_PRECISION (sizetype)
1827 || TYPE_UNSIGNED (gnu_index_subtype)
1828 != TYPE_UNSIGNED (sizetype))
1829 && TREE_CODE (gnu_min) == INTEGER_CST
1830 && TREE_CODE (gnu_max) == INTEGER_CST
1831 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
1832 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
1833 TYPE_MIN_VALUE (gnu_index_subtype)))
1834 gnu_min = size_one_node, gnu_max = size_zero_node;
1836 /* Now compute the size of this bound. We need to provide
1837 GCC with an upper bound to use but have to deal with the
1838 "superflat" case. There are three ways to do this. If we
1839 can prove that the array can never be superflat, we can
1840 just use the high bound of the index subtype. If we can
1841 prove that the low bound minus one can't overflow, we
1842 can do this as MAX (hb, lb - 1). Otherwise, we have to use
1843 the expression hb >= lb ? hb : lb - 1. */
1844 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
1846 /* See if the base array type is already flat. If it is, we
1847 are probably compiling an ACVC test, but it will cause the
1848 code below to malfunction if we don't handle it specially. */
1849 if (TREE_CODE (gnu_base_min) == INTEGER_CST
1850 && TREE_CODE (gnu_base_max) == INTEGER_CST
1851 && !TREE_CONSTANT_OVERFLOW (gnu_base_min)
1852 && !TREE_CONSTANT_OVERFLOW (gnu_base_max)
1853 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
1854 gnu_high = size_zero_node, gnu_min = size_one_node;
1856 /* If gnu_high is now an integer which overflowed, the array
1857 cannot be superflat. */
1858 else if (TREE_CODE (gnu_high) == INTEGER_CST
1859 && TREE_OVERFLOW (gnu_high))
1860 gnu_high = gnu_max;
1861 else if (TYPE_UNSIGNED (gnu_base_subtype)
1862 || TREE_CODE (gnu_high) == INTEGER_CST)
1863 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
1864 else
1865 gnu_high
1866 = build_cond_expr
1867 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
1868 gnu_max, gnu_min),
1869 gnu_max, gnu_high);
1871 gnu_index_type[index]
1872 = create_index_type (gnu_min, gnu_high, gnu_index_subtype);
1874 /* Also compute the maximum size of the array. Here we
1875 see if any constraint on the index type of the base type
1876 can be used in the case of self-referential bound on
1877 the index type of the subtype. We look for a non-"infinite"
1878 and non-self-referential bound from any type involved and
1879 handle each bound separately. */
1881 if ((TREE_CODE (gnu_min) == INTEGER_CST
1882 && !TREE_OVERFLOW (gnu_min)
1883 && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
1884 || !CONTAINS_PLACEHOLDER_P (gnu_min))
1885 gnu_base_min = gnu_min;
1887 if ((TREE_CODE (gnu_max) == INTEGER_CST
1888 && !TREE_OVERFLOW (gnu_max)
1889 && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
1890 || !CONTAINS_PLACEHOLDER_P (gnu_max))
1891 gnu_base_max = gnu_max;
1893 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
1894 && TREE_CONSTANT_OVERFLOW (gnu_base_min))
1895 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
1896 || (TREE_CODE (gnu_base_max) == INTEGER_CST
1897 && TREE_CONSTANT_OVERFLOW (gnu_base_max))
1898 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
1899 max_overflow = true;
1901 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
1902 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
1904 gnu_this_max
1905 = size_binop (MAX_EXPR,
1906 size_binop (PLUS_EXPR, size_one_node,
1907 size_binop (MINUS_EXPR, gnu_base_max,
1908 gnu_base_min)),
1909 size_zero_node);
1911 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1912 && TREE_CONSTANT_OVERFLOW (gnu_this_max))
1913 max_overflow = true;
1915 gnu_max_size
1916 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1918 if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
1919 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
1920 != INTEGER_CST)
1921 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
1922 || (TREE_TYPE (gnu_index_subtype)
1923 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
1924 != INTEGER_TYPE))
1925 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
1926 || (TYPE_PRECISION (gnu_index_subtype)
1927 > TYPE_PRECISION (sizetype)))
1928 need_index_type_struct = true;
1931 /* Then flatten: create the array of arrays. */
1933 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
1935 /* One of the above calls might have caused us to be elaborated,
1936 so don't blow up if so. */
1937 if (present_gnu_tree (gnat_entity))
1939 maybe_present = true;
1940 break;
1943 /* Get and validate any specified Component_Size, but if Packed,
1944 ignore it since the front end will have taken care of it. */
1945 gnu_comp_size
1946 = validate_size (Component_Size (gnat_entity), gnu_type,
1947 gnat_entity,
1948 (Is_Bit_Packed_Array (gnat_entity)
1949 ? TYPE_DECL : VAR_DECL),
1950 true, Has_Component_Size_Clause (gnat_entity));
1952 /* If the component type is a RECORD_TYPE that has a self-referential
1953 size, use the maxium size. */
1954 if (!gnu_comp_size && TREE_CODE (gnu_type) == RECORD_TYPE
1955 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
1956 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
1958 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
1960 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
1961 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
1962 gnat_entity, "C_PAD", false,
1963 definition, true);
1966 if (Has_Volatile_Components (Base_Type (gnat_entity)))
1967 gnu_type = build_qualified_type (gnu_type,
1968 (TYPE_QUALS (gnu_type)
1969 | TYPE_QUAL_VOLATILE));
1971 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
1972 TYPE_SIZE_UNIT (gnu_type));
1973 gnu_max_size = size_binop (MULT_EXPR,
1974 convert (bitsizetype, gnu_max_size),
1975 TYPE_SIZE (gnu_type));
1977 for (index = array_dim - 1; index >= 0; index --)
1979 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
1980 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
1981 /* If the type below this an multi-array type, then this
1982 does not not have aliased components.
1984 ??? Otherwise, for now, we say that any component of aggregate
1985 type is addressable because the front end may take 'Reference
1986 of it. But we have to make it addressable if it must be passed
1987 by reference or it that is the default. */
1988 TYPE_NONALIASED_COMPONENT (gnu_type)
1989 = ((TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
1990 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type))) ? 1
1991 : (!Has_Aliased_Components (gnat_entity)
1992 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_type))));
1995 /* If we are at file level and this is a multi-dimensional array, we
1996 need to make a variable corresponding to the stride of the
1997 inner dimensions. */
1998 if (global_bindings_p () && array_dim > 1)
2000 tree gnu_str_name = get_identifier ("ST");
2001 tree gnu_arr_type;
2003 for (gnu_arr_type = TREE_TYPE (gnu_type);
2004 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2005 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2006 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
2008 tree eltype = TREE_TYPE (gnu_arr_type);
2010 TYPE_SIZE (gnu_arr_type)
2011 = elaborate_expression_1 (gnat_entity, gnat_entity,
2012 TYPE_SIZE (gnu_arr_type),
2013 gnu_str_name, definition, 0);
2015 /* ??? For now, store the size as a multiple of the
2016 alignment of the element type in bytes so that we
2017 can see the alignment from the tree. */
2018 TYPE_SIZE_UNIT (gnu_arr_type)
2019 = build_binary_op
2020 (MULT_EXPR, sizetype,
2021 elaborate_expression_1
2022 (gnat_entity, gnat_entity,
2023 build_binary_op (EXACT_DIV_EXPR, sizetype,
2024 TYPE_SIZE_UNIT (gnu_arr_type),
2025 size_int (TYPE_ALIGN (eltype)
2026 / BITS_PER_UNIT)),
2027 concat_id_with_name (gnu_str_name, "A_U"),
2028 definition, 0),
2029 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2033 /* If we need to write out a record type giving the names of
2034 the bounds, do it now. */
2035 if (need_index_type_struct && debug_info_p)
2037 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
2038 tree gnu_field_list = NULL_TREE;
2039 tree gnu_field;
2041 TYPE_NAME (gnu_bound_rec_type)
2042 = create_concat_name (gnat_entity, "XA");
2044 for (index = array_dim - 1; index >= 0; index--)
2046 tree gnu_type_name
2047 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
2049 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
2050 gnu_type_name = DECL_NAME (gnu_type_name);
2052 gnu_field = create_field_decl (gnu_type_name,
2053 integer_type_node,
2054 gnu_bound_rec_type,
2055 0, NULL_TREE, NULL_TREE, 0);
2056 TREE_CHAIN (gnu_field) = gnu_field_list;
2057 gnu_field_list = gnu_field;
2060 finish_record_type (gnu_bound_rec_type, gnu_field_list,
2061 false, false);
2064 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2065 = (Convention (gnat_entity) == Convention_Fortran);
2066 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2067 = Is_Packed_Array_Type (gnat_entity);
2069 /* If our size depends on a placeholder and the maximum size doesn't
2070 overflow, use it. */
2071 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2072 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2073 && TREE_OVERFLOW (gnu_max_size))
2074 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2075 && TREE_OVERFLOW (gnu_max_size_unit))
2076 && !max_overflow)
2078 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2079 TYPE_SIZE (gnu_type));
2080 TYPE_SIZE_UNIT (gnu_type)
2081 = size_binop (MIN_EXPR, gnu_max_size_unit,
2082 TYPE_SIZE_UNIT (gnu_type));
2085 /* Set our alias set to that of our base type. This gives all
2086 array subtypes the same alias set. */
2087 copy_alias_set (gnu_type, gnu_base_type);
2090 /* If this is a packed type, make this type the same as the packed
2091 array type, but do some adjusting in the type first. */
2093 if (Present (Packed_Array_Type (gnat_entity)))
2095 Entity_Id gnat_index;
2096 tree gnu_inner_type;
2098 /* First finish the type we had been making so that we output
2099 debugging information for it */
2100 gnu_type
2101 = build_qualified_type (gnu_type,
2102 (TYPE_QUALS (gnu_type)
2103 | (TYPE_QUAL_VOLATILE
2104 * Treat_As_Volatile (gnat_entity))));
2105 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2106 !Comes_From_Source (gnat_entity),
2107 debug_info_p, gnat_entity);
2108 if (!Comes_From_Source (gnat_entity))
2109 DECL_ARTIFICIAL (gnu_decl) = 1;
2111 /* Save it as our equivalent in case the call below elaborates
2112 this type again. */
2113 save_gnu_tree (gnat_entity, gnu_decl, false);
2115 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2116 NULL_TREE, 0);
2117 this_made_decl = true;
2118 gnu_inner_type = gnu_type = TREE_TYPE (gnu_decl);
2119 save_gnu_tree (gnat_entity, NULL_TREE, false);
2121 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2122 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2123 || TYPE_IS_PADDING_P (gnu_inner_type)))
2124 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2126 /* We need to point the type we just made to our index type so
2127 the actual bounds can be put into a template. */
2129 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2130 && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2131 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2132 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2134 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2136 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2137 If it is, we need to make another type. */
2138 if (TYPE_MODULAR_P (gnu_inner_type))
2140 tree gnu_subtype;
2142 gnu_subtype = make_node (INTEGER_TYPE);
2144 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2145 TYPE_MIN_VALUE (gnu_subtype)
2146 = TYPE_MIN_VALUE (gnu_inner_type);
2147 TYPE_MAX_VALUE (gnu_subtype)
2148 = TYPE_MAX_VALUE (gnu_inner_type);
2149 TYPE_PRECISION (gnu_subtype)
2150 = TYPE_PRECISION (gnu_inner_type);
2151 TYPE_UNSIGNED (gnu_subtype)
2152 = TYPE_UNSIGNED (gnu_inner_type);
2153 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2154 layout_type (gnu_subtype);
2156 gnu_inner_type = gnu_subtype;
2159 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2162 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2164 for (gnat_index = First_Index (gnat_entity);
2165 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2166 SET_TYPE_ACTUAL_BOUNDS
2167 (gnu_inner_type,
2168 tree_cons (NULL_TREE,
2169 get_unpadded_type (Etype (gnat_index)),
2170 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2172 if (Convention (gnat_entity) != Convention_Fortran)
2173 SET_TYPE_ACTUAL_BOUNDS
2174 (gnu_inner_type,
2175 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2177 if (TREE_CODE (gnu_type) == RECORD_TYPE
2178 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2179 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2183 /* Abort if packed array with no packed array type field set. */
2184 else
2185 gcc_assert (!Is_Packed (gnat_entity));
2187 break;
2189 case E_String_Literal_Subtype:
2190 /* Create the type for a string literal. */
2192 Entity_Id gnat_full_type
2193 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2194 && Present (Full_View (Etype (gnat_entity)))
2195 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2196 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2197 tree gnu_string_array_type
2198 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2199 tree gnu_string_index_type
2200 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2201 (TYPE_DOMAIN (gnu_string_array_type))));
2202 tree gnu_lower_bound
2203 = convert (gnu_string_index_type,
2204 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2205 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2206 tree gnu_length = ssize_int (length - 1);
2207 tree gnu_upper_bound
2208 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2209 gnu_lower_bound,
2210 convert (gnu_string_index_type, gnu_length));
2211 tree gnu_range_type
2212 = build_range_type (gnu_string_index_type,
2213 gnu_lower_bound, gnu_upper_bound);
2214 tree gnu_index_type
2215 = create_index_type (convert (sizetype,
2216 TYPE_MIN_VALUE (gnu_range_type)),
2217 convert (sizetype,
2218 TYPE_MAX_VALUE (gnu_range_type)),
2219 gnu_range_type);
2221 gnu_type
2222 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2223 gnu_index_type);
2225 break;
2227 /* Record Types and Subtypes
2229 The following fields are defined on record types:
2231 Has_Discriminants True if the record has discriminants
2232 First_Discriminant Points to head of list of discriminants
2233 First_Entity Points to head of list of fields
2234 Is_Tagged_Type True if the record is tagged
2236 Implementation of Ada records and discriminated records:
2238 A record type definition is transformed into the equivalent of a C
2239 struct definition. The fields that are the discriminants which are
2240 found in the Full_Type_Declaration node and the elements of the
2241 Component_List found in the Record_Type_Definition node. The
2242 Component_List can be a recursive structure since each Variant of
2243 the Variant_Part of the Component_List has a Component_List.
2245 Processing of a record type definition comprises starting the list of
2246 field declarations here from the discriminants and the calling the
2247 function components_to_record to add the rest of the fields from the
2248 component list and return the gnu type node. The function
2249 components_to_record will call itself recursively as it traverses
2250 the tree. */
2252 case E_Record_Type:
2253 if (Has_Complex_Representation (gnat_entity))
2255 gnu_type
2256 = build_complex_type
2257 (get_unpadded_type
2258 (Etype (Defining_Entity
2259 (First (Component_Items
2260 (Component_List
2261 (Type_Definition
2262 (Declaration_Node (gnat_entity)))))))));
2264 break;
2268 Node_Id full_definition = Declaration_Node (gnat_entity);
2269 Node_Id record_definition = Type_Definition (full_definition);
2270 Entity_Id gnat_field;
2271 tree gnu_field;
2272 tree gnu_field_list = NULL_TREE;
2273 tree gnu_get_parent;
2274 int packed = (Is_Packed (gnat_entity) ? 1
2275 : (Component_Alignment (gnat_entity)
2276 == Calign_Storage_Unit) ? -1
2277 : 0);
2278 bool has_rep = Has_Specified_Layout (gnat_entity);
2279 bool all_rep = has_rep;
2280 bool is_extension
2281 = (Is_Tagged_Type (gnat_entity)
2282 && Nkind (record_definition) == N_Derived_Type_Definition);
2284 /* See if all fields have a rep clause. Stop when we find one
2285 that doesn't. */
2286 for (gnat_field = First_Entity (gnat_entity);
2287 Present (gnat_field) && all_rep;
2288 gnat_field = Next_Entity (gnat_field))
2289 if ((Ekind (gnat_field) == E_Component
2290 || Ekind (gnat_field) == E_Discriminant)
2291 && No (Component_Clause (gnat_field)))
2292 all_rep = false;
2294 /* If this is a record extension, go a level further to find the
2295 record definition. Also, verify we have a Parent_Subtype. */
2296 if (is_extension)
2298 if (!type_annotate_only
2299 || Present (Record_Extension_Part (record_definition)))
2300 record_definition = Record_Extension_Part (record_definition);
2302 gcc_assert (type_annotate_only
2303 || Present (Parent_Subtype (gnat_entity)));
2306 /* Make a node for the record. If we are not defining the record,
2307 suppress expanding incomplete types and save the node as the type
2308 for GNAT_ENTITY. We use the same RECORD_TYPE as for a dummy type
2309 and reset TYPE_DUMMY_P to show it's no longer a dummy.
2311 It is very tempting to delay resetting this bit until we are done
2312 with completing the type, e.g. to let possible intermediate
2313 elaboration of access types designating the record know it is not
2314 complete and arrange for update_pointer_to to fix things up later.
2316 It would be wrong, however, because dummy types are expected only
2317 to be created for Ada incomplete or private types, which is not
2318 what we have here. Doing so would make other parts of gigi think
2319 we are dealing with a really incomplete or private type, and have
2320 nasty side effects, typically on the generation of the associated
2321 debugging information. */
2322 gnu_type = make_dummy_type (gnat_entity);
2323 TYPE_DUMMY_P (gnu_type) = 0;
2325 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL && debug_info_p)
2326 DECL_IGNORED_P (TYPE_NAME (gnu_type)) = 0;
2328 TYPE_ALIGN (gnu_type) = 0;
2329 TYPE_PACKED (gnu_type) = packed || has_rep;
2331 if (!definition)
2333 defer_incomplete_level++;
2334 this_deferred = true;
2335 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2336 !Comes_From_Source (gnat_entity),
2337 debug_info_p, gnat_entity);
2338 save_gnu_tree (gnat_entity, gnu_decl, false);
2339 this_made_decl = saved = true;
2342 /* If both a size and rep clause was specified, put the size in
2343 the record type now so that it can get the proper mode. */
2344 if (has_rep && Known_Esize (gnat_entity))
2345 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2347 /* Always set the alignment here so that it can be used to
2348 set the mode, if it is making the alignment stricter. If
2349 it is invalid, it will be checked again below. If this is to
2350 be Atomic, choose a default alignment of a word unless we know
2351 the size and it's smaller. */
2352 if (Known_Alignment (gnat_entity))
2353 TYPE_ALIGN (gnu_type)
2354 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2355 else if (Is_Atomic (gnat_entity))
2356 TYPE_ALIGN (gnu_type)
2357 = (esize >= BITS_PER_WORD ? BITS_PER_WORD
2358 : 1 << ((floor_log2 (esize) - 1) + 1));
2360 /* If we have a Parent_Subtype, make a field for the parent. If
2361 this record has rep clauses, force the position to zero. */
2362 if (Present (Parent_Subtype (gnat_entity)))
2364 tree gnu_parent;
2366 /* A major complexity here is that the parent subtype will
2367 reference our discriminants. But those must reference
2368 the parent component of this record. So here we will
2369 initialize each of those components to a COMPONENT_REF.
2370 The first operand of that COMPONENT_REF is another
2371 COMPONENT_REF which will be filled in below, once
2372 the parent type can be safely built. */
2374 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2375 build0 (PLACEHOLDER_EXPR, gnu_type),
2376 build_decl (FIELD_DECL, NULL_TREE,
2377 NULL_TREE),
2378 NULL_TREE);
2380 if (Has_Discriminants (gnat_entity))
2381 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2382 Present (gnat_field);
2383 gnat_field = Next_Stored_Discriminant (gnat_field))
2384 if (Present (Corresponding_Discriminant (gnat_field)))
2385 save_gnu_tree
2386 (gnat_field,
2387 build3 (COMPONENT_REF,
2388 get_unpadded_type (Etype (gnat_field)),
2389 gnu_get_parent,
2390 gnat_to_gnu_entity (Corresponding_Discriminant
2391 (gnat_field),
2392 NULL_TREE, 0),
2393 NULL_TREE),
2394 true);
2396 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_entity));
2398 gnu_field_list
2399 = create_field_decl (get_identifier
2400 (Get_Name_String (Name_uParent)),
2401 gnu_parent, gnu_type, 0,
2402 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2403 has_rep ? bitsize_zero_node : 0, 1);
2404 DECL_INTERNAL_P (gnu_field_list) = 1;
2406 TREE_TYPE (gnu_get_parent) = gnu_parent;
2407 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2410 /* Add the fields for the discriminants into the record. */
2411 if (!Is_Unchecked_Union (gnat_entity)
2412 && Has_Discriminants (gnat_entity))
2413 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2414 Present (gnat_field);
2415 gnat_field = Next_Stored_Discriminant (gnat_field))
2417 /* If this is a record extension and this discriminant
2418 is the renaming of another discriminant, we've already
2419 handled the discriminant above. */
2420 if (Present (Parent_Subtype (gnat_entity))
2421 && Present (Corresponding_Discriminant (gnat_field)))
2422 continue;
2424 gnu_field
2425 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2427 /* Make an expression using a PLACEHOLDER_EXPR from the
2428 FIELD_DECL node just created and link that with the
2429 corresponding GNAT defining identifier. Then add to the
2430 list of fields. */
2431 save_gnu_tree (gnat_field,
2432 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2433 build0 (PLACEHOLDER_EXPR,
2434 DECL_CONTEXT (gnu_field)),
2435 gnu_field, NULL_TREE),
2436 true);
2438 TREE_CHAIN (gnu_field) = gnu_field_list;
2439 gnu_field_list = gnu_field;
2442 /* Put the discriminants into the record (backwards), so we can
2443 know the appropriate discriminant to use for the names of the
2444 variants. */
2445 TYPE_FIELDS (gnu_type) = gnu_field_list;
2447 /* Add the listed fields into the record and finish up. */
2448 components_to_record (gnu_type, Component_List (record_definition),
2449 gnu_field_list, packed, definition, NULL,
2450 false, all_rep);
2452 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2453 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2455 /* If this is an extension type, reset the tree for any
2456 inherited discriminants. Also remove the PLACEHOLDER_EXPR
2457 for non-inherited discriminants. */
2458 if (!Is_Unchecked_Union (gnat_entity)
2459 && Has_Discriminants (gnat_entity))
2460 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2461 Present (gnat_field);
2462 gnat_field = Next_Stored_Discriminant (gnat_field))
2464 if (Present (Parent_Subtype (gnat_entity))
2465 && Present (Corresponding_Discriminant (gnat_field)))
2466 save_gnu_tree (gnat_field, NULL_TREE, false);
2467 else
2469 gnu_field = get_gnu_tree (gnat_field);
2470 save_gnu_tree (gnat_field, NULL_TREE, false);
2471 save_gnu_tree (gnat_field, TREE_OPERAND (gnu_field, 1),
2472 false);
2476 /* If it is a tagged record force the type to BLKmode to insure
2477 that these objects will always be placed in memory. Do the
2478 same thing for limited record types. */
2479 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2480 TYPE_MODE (gnu_type) = BLKmode;
2482 /* If this is a derived type, we must make the alias set of this type
2483 the same as that of the type we are derived from. We assume here
2484 that the other type is already frozen. */
2485 if (Etype (gnat_entity) != gnat_entity
2486 && !(Is_Private_Type (Etype (gnat_entity))
2487 && Full_View (Etype (gnat_entity)) == gnat_entity))
2488 copy_alias_set (gnu_type, gnat_to_gnu_type (Etype (gnat_entity)));
2490 /* Fill in locations of fields. */
2491 annotate_rep (gnat_entity, gnu_type);
2493 /* If there are any entities in the chain corresponding to
2494 components that we did not elaborate, ensure we elaborate their
2495 types if they are Itypes. */
2496 for (gnat_temp = First_Entity (gnat_entity);
2497 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2498 if ((Ekind (gnat_temp) == E_Component
2499 || Ekind (gnat_temp) == E_Discriminant)
2500 && Is_Itype (Etype (gnat_temp))
2501 && !present_gnu_tree (gnat_temp))
2502 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2504 break;
2506 case E_Class_Wide_Subtype:
2507 /* If an equivalent type is present, that is what we should use.
2508 Otherwise, fall through to handle this like a record subtype
2509 since it may have constraints. */
2511 if (Present (Equivalent_Type (gnat_entity)))
2513 gnu_decl = gnat_to_gnu_entity (Equivalent_Type (gnat_entity),
2514 NULL_TREE, 0);
2515 maybe_present = true;
2516 break;
2519 /* ... fall through ... */
2521 case E_Record_Subtype:
2523 /* If Cloned_Subtype is Present it means this record subtype has
2524 identical layout to that type or subtype and we should use
2525 that GCC type for this one. The front end guarantees that
2526 the component list is shared. */
2527 if (Present (Cloned_Subtype (gnat_entity)))
2529 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2530 NULL_TREE, 0);
2531 maybe_present = true;
2534 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2535 changing the type, make a new type with each field having the
2536 type of the field in the new subtype but having the position
2537 computed by transforming every discriminant reference according
2538 to the constraints. We don't see any difference between
2539 private and nonprivate type here since derivations from types should
2540 have been deferred until the completion of the private type. */
2541 else
2543 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2544 tree gnu_base_type;
2545 tree gnu_orig_type;
2547 if (!definition)
2548 defer_incomplete_level++, this_deferred = true;
2550 /* Get the base type initially for its alignment and sizes. But
2551 if it is a padded type, we do all the other work with the
2552 unpadded type. */
2553 gnu_type = gnu_orig_type = gnu_base_type
2554 = gnat_to_gnu_type (gnat_base_type);
2556 if (TREE_CODE (gnu_type) == RECORD_TYPE
2557 && TYPE_IS_PADDING_P (gnu_type))
2558 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2560 if (present_gnu_tree (gnat_entity))
2562 maybe_present = true;
2563 break;
2566 /* When the type has discriminants, and these discriminants
2567 affect the shape of what it built, factor them in.
2569 If we are making a subtype of an Unchecked_Union (must be an
2570 Itype), just return the type.
2572 We can't just use Is_Constrained because private subtypes without
2573 discriminants of full types with discriminants with default
2574 expressions are Is_Constrained but aren't constrained! */
2576 if (IN (Ekind (gnat_base_type), Record_Kind)
2577 && !Is_For_Access_Subtype (gnat_entity)
2578 && !Is_Unchecked_Union (gnat_base_type)
2579 && Is_Constrained (gnat_entity)
2580 && Stored_Constraint (gnat_entity) != No_Elist
2581 && Present (Discriminant_Constraint (gnat_entity)))
2583 Entity_Id gnat_field;
2584 Entity_Id gnat_root_type;
2585 tree gnu_field_list = 0;
2586 tree gnu_pos_list
2587 = compute_field_positions (gnu_orig_type, NULL_TREE,
2588 size_zero_node, bitsize_zero_node,
2589 BIGGEST_ALIGNMENT);
2590 tree gnu_subst_list
2591 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2592 definition);
2593 bool possibly_overlapping_fields = false;
2594 tree gnu_temp;
2596 /* If this is a derived type, we may be seeing fields from any
2597 original records, so add those positions and discriminant
2598 substitutions to our lists. */
2599 for (gnat_root_type = gnat_base_type;
2600 Underlying_Type (Etype (gnat_root_type)) != gnat_root_type;
2601 gnat_root_type = Underlying_Type (Etype (gnat_root_type)))
2603 gnu_pos_list
2604 = compute_field_positions
2605 (gnat_to_gnu_type (Etype (gnat_root_type)),
2606 gnu_pos_list, size_zero_node, bitsize_zero_node,
2607 BIGGEST_ALIGNMENT);
2609 if (Present (Parent_Subtype (gnat_root_type)))
2611 gnu_subst_list
2612 = substitution_list (Parent_Subtype (gnat_root_type),
2613 Empty, gnu_subst_list,
2614 definition);
2616 /* If there's a _Parent field, it may overlap the
2617 fields we have that appear to be in this record but
2618 actually are from the parent. So make note of that
2619 fact and later we'll make a UNION_TYPE instead of
2620 a RECORD_TYPE, since the latter may not have
2621 overlapping fields. */
2622 possibly_overlapping_fields = true;
2626 gnu_type = make_node (possibly_overlapping_fields
2627 ? UNION_TYPE : RECORD_TYPE);
2628 TYPE_NAME (gnu_type) = gnu_entity_id;
2629 TYPE_STUB_DECL (gnu_type)
2630 = create_type_decl (NULL_TREE, gnu_type, NULL, false, false,
2631 gnat_entity);
2632 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2634 for (gnat_field = First_Entity (gnat_entity);
2635 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2636 if (Ekind (gnat_field) == E_Component
2637 || Ekind (gnat_field) == E_Discriminant)
2639 tree gnu_old_field
2640 = gnat_to_gnu_entity
2641 (Original_Record_Component (gnat_field), NULL_TREE, 0);
2642 tree gnu_offset
2643 = TREE_VALUE (purpose_member (gnu_old_field,
2644 gnu_pos_list));
2645 tree gnu_pos = TREE_PURPOSE (gnu_offset);
2646 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
2647 tree gnu_field_type
2648 = gnat_to_gnu_type (Etype (gnat_field));
2649 tree gnu_size = TYPE_SIZE (gnu_field_type);
2650 tree gnu_new_pos = 0;
2651 unsigned int offset_align
2652 = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
2654 tree gnu_field;
2656 /* If there was a component clause, the field types must be
2657 the same for the type and subtype, so copy the data from
2658 the old field to avoid recomputation here. Also if the
2659 field is justified modular and the optimization in
2660 gnat_to_gnu_field was applied. */
2661 if (Present (Component_Clause
2662 (Original_Record_Component (gnat_field)))
2663 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
2664 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
2665 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
2666 == TREE_TYPE (gnu_old_field)))
2668 gnu_size = DECL_SIZE (gnu_old_field);
2669 gnu_field_type = TREE_TYPE (gnu_old_field);
2672 /* If this was a bitfield, get the size from the old field.
2673 Also ensure the type can be placed into a bitfield. */
2674 else if (DECL_BIT_FIELD (gnu_old_field))
2676 gnu_size = DECL_SIZE (gnu_old_field);
2677 if (TYPE_MODE (gnu_field_type) == BLKmode
2678 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2679 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2680 gnu_field_type = make_packable_type (gnu_field_type);
2683 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
2684 for (gnu_temp = gnu_subst_list;
2685 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2686 gnu_pos = substitute_in_expr (gnu_pos,
2687 TREE_PURPOSE (gnu_temp),
2688 TREE_VALUE (gnu_temp));
2690 /* If the size is now a constant, we can set it as the
2691 size of the field when we make it. Otherwise, we need
2692 to deal with it specially. */
2693 if (TREE_CONSTANT (gnu_pos))
2694 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2696 gnu_field
2697 = create_field_decl
2698 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2699 0, gnu_size, gnu_new_pos,
2700 !DECL_NONADDRESSABLE_P (gnu_old_field));
2702 if (!TREE_CONSTANT (gnu_pos))
2704 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
2705 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2706 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
2707 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
2708 DECL_SIZE (gnu_field) = gnu_size;
2709 DECL_SIZE_UNIT (gnu_field)
2710 = convert (sizetype,
2711 size_binop (CEIL_DIV_EXPR, gnu_size,
2712 bitsize_unit_node));
2713 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2716 DECL_INTERNAL_P (gnu_field)
2717 = DECL_INTERNAL_P (gnu_old_field);
2718 SET_DECL_ORIGINAL_FIELD
2719 (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
2720 ? DECL_ORIGINAL_FIELD (gnu_old_field)
2721 : gnu_old_field));
2722 DECL_DISCRIMINANT_NUMBER (gnu_field)
2723 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2724 TREE_THIS_VOLATILE (gnu_field)
2725 = TREE_THIS_VOLATILE (gnu_old_field);
2726 TREE_CHAIN (gnu_field) = gnu_field_list;
2727 gnu_field_list = gnu_field;
2728 save_gnu_tree (gnat_field, gnu_field, false);
2731 finish_record_type (gnu_type, nreverse (gnu_field_list),
2732 true, false);
2734 /* Now set the size, alignment and alias set of the new type to
2735 match that of the old one, doing any substitutions, as
2736 above. */
2737 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2738 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2739 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
2740 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
2741 copy_alias_set (gnu_type, gnu_base_type);
2743 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2744 for (gnu_temp = gnu_subst_list;
2745 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2746 TYPE_SIZE (gnu_type)
2747 = substitute_in_expr (TYPE_SIZE (gnu_type),
2748 TREE_PURPOSE (gnu_temp),
2749 TREE_VALUE (gnu_temp));
2751 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
2752 for (gnu_temp = gnu_subst_list;
2753 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2754 TYPE_SIZE_UNIT (gnu_type)
2755 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
2756 TREE_PURPOSE (gnu_temp),
2757 TREE_VALUE (gnu_temp));
2759 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
2760 for (gnu_temp = gnu_subst_list;
2761 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2762 SET_TYPE_ADA_SIZE
2763 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
2764 TREE_PURPOSE (gnu_temp),
2765 TREE_VALUE (gnu_temp)));
2767 /* Recompute the mode of this record type now that we know its
2768 actual size. */
2769 compute_record_mode (gnu_type);
2771 /* Fill in locations of fields. */
2772 annotate_rep (gnat_entity, gnu_type);
2775 /* If we've made a new type, record it and make an XVS type to show
2776 what this is a subtype of. Some debuggers require the XVS
2777 type to be output first, so do it in that order. */
2778 if (gnu_type != gnu_orig_type)
2780 if (debug_info_p)
2782 tree gnu_subtype_marker = make_node (RECORD_TYPE);
2783 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
2785 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
2786 gnu_orig_name = DECL_NAME (gnu_orig_name);
2788 TYPE_NAME (gnu_subtype_marker)
2789 = create_concat_name (gnat_entity, "XVS");
2790 finish_record_type (gnu_subtype_marker,
2791 create_field_decl (gnu_orig_name,
2792 integer_type_node,
2793 gnu_subtype_marker,
2794 0, NULL_TREE,
2795 NULL_TREE, 0),
2796 false, false);
2799 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2800 TYPE_NAME (gnu_type) = gnu_entity_id;
2801 TYPE_STUB_DECL (gnu_type)
2802 = create_type_decl (TYPE_NAME (gnu_type), gnu_type,
2803 NULL, true, debug_info_p, gnat_entity);
2806 /* Otherwise, go down all the components in the new type and
2807 make them equivalent to those in the base type. */
2808 else
2809 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
2810 gnat_temp = Next_Entity (gnat_temp))
2811 if ((Ekind (gnat_temp) == E_Discriminant
2812 && !Is_Unchecked_Union (gnat_base_type))
2813 || Ekind (gnat_temp) == E_Component)
2814 save_gnu_tree (gnat_temp,
2815 get_gnu_tree
2816 (Original_Record_Component (gnat_temp)), false);
2818 break;
2820 case E_Access_Subprogram_Type:
2821 case E_Anonymous_Access_Subprogram_Type:
2822 /* If we are not defining this entity, and we have incomplete
2823 entities being processed above us, make a dummy type and
2824 fill it in later. */
2825 if (!definition && defer_incomplete_level != 0)
2827 struct incomplete *p
2828 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2830 gnu_type
2831 = build_pointer_type
2832 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2833 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2834 !Comes_From_Source (gnat_entity),
2835 debug_info_p, gnat_entity);
2836 save_gnu_tree (gnat_entity, gnu_decl, false);
2837 this_made_decl = saved = true;
2839 p->old_type = TREE_TYPE (gnu_type);
2840 p->full_type = Directly_Designated_Type (gnat_entity);
2841 p->next = defer_incomplete_list;
2842 defer_incomplete_list = p;
2843 break;
2846 /* ... fall through ... */
2848 case E_Allocator_Type:
2849 case E_Access_Type:
2850 case E_Access_Attribute_Type:
2851 case E_Anonymous_Access_Type:
2852 case E_General_Access_Type:
2854 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
2855 Entity_Id gnat_desig_full
2856 = ((IN (Ekind (Etype (gnat_desig_type)),
2857 Incomplete_Or_Private_Kind))
2858 ? Full_View (gnat_desig_type) : 0);
2859 /* We want to know if we'll be seeing the freeze node for any
2860 incomplete type we may be pointing to. */
2861 bool in_main_unit
2862 = (Present (gnat_desig_full)
2863 ? In_Extended_Main_Code_Unit (gnat_desig_full)
2864 : In_Extended_Main_Code_Unit (gnat_desig_type));
2865 bool got_fat_p = false;
2866 bool made_dummy = false;
2867 tree gnu_desig_type = NULL_TREE;
2868 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
2870 if (!targetm.valid_pointer_mode (p_mode))
2871 p_mode = ptr_mode;
2873 if (No (gnat_desig_full)
2874 && (Ekind (gnat_desig_type) == E_Class_Wide_Type
2875 || (Ekind (gnat_desig_type) == E_Class_Wide_Subtype
2876 && Present (Equivalent_Type (gnat_desig_type)))))
2878 if (Present (Equivalent_Type (gnat_desig_type)))
2880 gnat_desig_full = Equivalent_Type (gnat_desig_type);
2881 if (IN (Ekind (gnat_desig_full), Incomplete_Or_Private_Kind))
2882 gnat_desig_full = Full_View (gnat_desig_full);
2884 else if (IN (Ekind (Root_Type (gnat_desig_type)),
2885 Incomplete_Or_Private_Kind))
2886 gnat_desig_full = Full_View (Root_Type (gnat_desig_type));
2889 if (Present (gnat_desig_full) && Is_Concurrent_Type (gnat_desig_full))
2890 gnat_desig_full = Corresponding_Record_Type (gnat_desig_full);
2892 /* If either the designated type or its full view is an
2893 unconstrained array subtype, replace it with the type it's a
2894 subtype of. This avoids problems with multiple copies of
2895 unconstrained array types. */
2896 if (Ekind (gnat_desig_type) == E_Array_Subtype
2897 && !Is_Constrained (gnat_desig_type))
2898 gnat_desig_type = Etype (gnat_desig_type);
2899 if (Present (gnat_desig_full)
2900 && Ekind (gnat_desig_full) == E_Array_Subtype
2901 && !Is_Constrained (gnat_desig_full))
2902 gnat_desig_full = Etype (gnat_desig_full);
2904 /* If the designated type is a subtype of an incomplete record type,
2905 use the parent type to avoid order of elaboration issues. This
2906 can lose some code efficiency, but there is no alternative. */
2907 if (Present (gnat_desig_full)
2908 && Ekind (gnat_desig_full) == E_Record_Subtype
2909 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)
2910 gnat_desig_full = Etype (gnat_desig_full);
2912 /* If we are pointing to an incomplete type whose completion is an
2913 unconstrained array, make a fat pointer type instead of a pointer
2914 to VOID. The two types in our fields will be pointers to VOID and
2915 will be replaced in update_pointer_to. Similiarly, if the type
2916 itself is a dummy type or an unconstrained array. Also make
2917 a dummy TYPE_OBJECT_RECORD_TYPE in case we have any thin
2918 pointers to it. */
2920 if ((Present (gnat_desig_full)
2921 && Is_Array_Type (gnat_desig_full)
2922 && !Is_Constrained (gnat_desig_full))
2923 || (present_gnu_tree (gnat_desig_type)
2924 && TYPE_IS_DUMMY_P (TREE_TYPE
2925 (get_gnu_tree (gnat_desig_type)))
2926 && Is_Array_Type (gnat_desig_type)
2927 && !Is_Constrained (gnat_desig_type))
2928 || (present_gnu_tree (gnat_desig_type)
2929 && (TREE_CODE (TREE_TYPE (get_gnu_tree (gnat_desig_type)))
2930 == UNCONSTRAINED_ARRAY_TYPE)
2931 && !(TYPE_POINTER_TO (TREE_TYPE
2932 (get_gnu_tree (gnat_desig_type)))))
2933 || (No (gnat_desig_full) && !in_main_unit
2934 && defer_incomplete_level
2935 && !present_gnu_tree (gnat_desig_type)
2936 && Is_Array_Type (gnat_desig_type)
2937 && !Is_Constrained (gnat_desig_type)))
2939 tree gnu_old
2940 = (present_gnu_tree (gnat_desig_type)
2941 ? gnat_to_gnu_type (gnat_desig_type)
2942 : make_dummy_type (gnat_desig_type));
2943 tree fields;
2945 /* Show the dummy we get will be a fat pointer. */
2946 got_fat_p = made_dummy = true;
2948 /* If the call above got something that has a pointer, that
2949 pointer is our type. This could have happened either
2950 because the type was elaborated or because somebody
2951 else executed the code below. */
2952 gnu_type = TYPE_POINTER_TO (gnu_old);
2953 if (!gnu_type)
2955 gnu_type = make_node (RECORD_TYPE);
2956 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
2957 TYPE_POINTER_TO (gnu_old) = gnu_type;
2959 Sloc_to_locus (Sloc (gnat_entity), &input_location);
2960 fields
2961 = chainon (chainon (NULL_TREE,
2962 create_field_decl
2963 (get_identifier ("P_ARRAY"),
2964 ptr_void_type_node, gnu_type,
2965 0, 0, 0, 0)),
2966 create_field_decl (get_identifier ("P_BOUNDS"),
2967 ptr_void_type_node,
2968 gnu_type, 0, 0, 0, 0));
2970 /* Make sure we can place this into a register. */
2971 TYPE_ALIGN (gnu_type)
2972 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
2973 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
2974 finish_record_type (gnu_type, fields, false, true);
2976 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
2977 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
2978 = concat_id_with_name (get_entity_name (gnat_desig_type),
2979 "XUT");
2980 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
2984 /* If we already know what the full type is, use it. */
2985 else if (Present (gnat_desig_full)
2986 && present_gnu_tree (gnat_desig_full))
2987 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
2989 /* Get the type of the thing we are to point to and build a pointer
2990 to it. If it is a reference to an incomplete or private type with a
2991 full view that is a record, make a dummy type node and get the
2992 actual type later when we have verified it is safe. */
2993 else if (!in_main_unit
2994 && !present_gnu_tree (gnat_desig_type)
2995 && Present (gnat_desig_full)
2996 && !present_gnu_tree (gnat_desig_full)
2997 && Is_Record_Type (gnat_desig_full))
2999 gnu_desig_type = make_dummy_type (gnat_desig_type);
3000 made_dummy = true;
3003 /* Likewise if we are pointing to a record or array and we are to defer
3004 elaborating incomplete types. We do this since this access type
3005 may be the full view of some private type. Note that the
3006 unconstrained array case is handled above. */
3007 else if ((!in_main_unit || imported_p) && defer_incomplete_level != 0
3008 && !present_gnu_tree (gnat_desig_type)
3009 && ((Is_Record_Type (gnat_desig_type)
3010 || Is_Array_Type (gnat_desig_type))
3011 || (Present (gnat_desig_full)
3012 && (Is_Record_Type (gnat_desig_full)
3013 || Is_Array_Type (gnat_desig_full)))))
3015 gnu_desig_type = make_dummy_type (gnat_desig_type);
3016 made_dummy = true;
3018 else if (gnat_desig_type == gnat_entity)
3020 gnu_type
3021 = build_pointer_type_for_mode (make_node (VOID_TYPE),
3022 p_mode,
3023 No_Strict_Aliasing (gnat_entity));
3024 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3026 else
3027 gnu_desig_type = gnat_to_gnu_type (gnat_desig_type);
3029 /* It is possible that the above call to gnat_to_gnu_type resolved our
3030 type. If so, just return it. */
3031 if (present_gnu_tree (gnat_entity))
3033 maybe_present = true;
3034 break;
3037 /* If we have a GCC type for the designated type, possibly modify it
3038 if we are pointing only to constant objects and then make a pointer
3039 to it. Don't do this for unconstrained arrays. */
3040 if (!gnu_type && gnu_desig_type)
3042 if (Is_Access_Constant (gnat_entity)
3043 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3045 gnu_desig_type
3046 = build_qualified_type
3047 (gnu_desig_type,
3048 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3050 /* Some extra processing is required if we are building a
3051 pointer to an incomplete type (in the GCC sense). We might
3052 have such a type if we just made a dummy, or directly out
3053 of the call to gnat_to_gnu_type above if we are processing
3054 an access type for a record component designating the
3055 record type itself. */
3056 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3058 /* We must ensure that the pointer to variant we make will
3059 be processed by update_pointer_to when the initial type
3060 is completed. Pretend we made a dummy and let further
3061 processing act as usual. */
3062 made_dummy = true;
3064 /* We must ensure that update_pointer_to will not retrieve
3065 the dummy variant when building a properly qualified
3066 version of the complete type. We take advantage of the
3067 fact that get_qualified_type is requiring TYPE_NAMEs to
3068 match to influence build_qualified_type and then also
3069 update_pointer_to here. */
3070 TYPE_NAME (gnu_desig_type)
3071 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3075 gnu_type
3076 = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3077 No_Strict_Aliasing (gnat_entity));
3080 /* If we are not defining this object and we made a dummy pointer,
3081 save our current definition, evaluate the actual type, and replace
3082 the tentative type we made with the actual one. If we are to defer
3083 actually looking up the actual type, make an entry in the
3084 deferred list. */
3086 if (!in_main_unit && made_dummy)
3088 tree gnu_old_type
3089 = TYPE_FAT_POINTER_P (gnu_type)
3090 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3092 if (esize == POINTER_SIZE
3093 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
3094 gnu_type
3095 = build_pointer_type
3096 (TYPE_OBJECT_RECORD_TYPE
3097 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3099 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3100 !Comes_From_Source (gnat_entity),
3101 debug_info_p, gnat_entity);
3102 save_gnu_tree (gnat_entity, gnu_decl, false);
3103 this_made_decl = saved = true;
3105 if (defer_incomplete_level == 0)
3106 /* Note that the call to gnat_to_gnu_type here might have
3107 updated gnu_old_type directly, in which case it is not a
3108 dummy type any more when we get into update_pointer_to.
3110 This may happen for instance when the designated type is a
3111 record type, because their elaboration starts with an
3112 initial node from make_dummy_type, which may yield the same
3113 node as the one we got.
3115 Besides, variants of this non-dummy type might have been
3116 created along the way. update_pointer_to is expected to
3117 properly take care of those situations. */
3118 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
3119 gnat_to_gnu_type (gnat_desig_type));
3120 else
3122 struct incomplete *p
3123 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3125 p->old_type = gnu_old_type;
3126 p->full_type = gnat_desig_type;
3127 p->next = defer_incomplete_list;
3128 defer_incomplete_list = p;
3132 break;
3134 case E_Access_Protected_Subprogram_Type:
3135 case E_Anonymous_Access_Protected_Subprogram_Type:
3136 if (type_annotate_only && No (Equivalent_Type (gnat_entity)))
3137 gnu_type = build_pointer_type (void_type_node);
3138 else
3139 /* The runtime representation is the equivalent type. */
3140 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3142 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3143 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3144 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3145 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3146 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3147 NULL_TREE, 0);
3149 break;
3151 case E_Access_Subtype:
3153 /* We treat this as identical to its base type; any constraint is
3154 meaningful only to the front end.
3156 The designated type must be elaborated as well, if it does
3157 not have its own freeze node. Designated (sub)types created
3158 for constrained components of records with discriminants are
3159 not frozen by the front end and thus not elaborated by gigi,
3160 because their use may appear before the base type is frozen,
3161 and because it is not clear that they are needed anywhere in
3162 Gigi. With the current model, there is no correct place where
3163 they could be elaborated. */
3165 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3166 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3167 && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3168 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3169 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3171 /* If we are not defining this entity, and we have incomplete
3172 entities being processed above us, make a dummy type and
3173 elaborate it later. */
3174 if (!definition && defer_incomplete_level != 0)
3176 struct incomplete *p
3177 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3178 tree gnu_ptr_type
3179 = build_pointer_type
3180 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3182 p->old_type = TREE_TYPE (gnu_ptr_type);
3183 p->full_type = Directly_Designated_Type (gnat_entity);
3184 p->next = defer_incomplete_list;
3185 defer_incomplete_list = p;
3187 else if (IN (Ekind (Base_Type
3188 (Directly_Designated_Type (gnat_entity))),
3189 Incomplete_Or_Private_Kind))
3191 else
3192 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3193 NULL_TREE, 0);
3196 maybe_present = true;
3197 break;
3199 /* Subprogram Entities
3201 The following access functions are defined for subprograms (functions
3202 or procedures):
3204 First_Formal The first formal parameter.
3205 Is_Imported Indicates that the subprogram has appeared in
3206 an INTERFACE or IMPORT pragma. For now we
3207 assume that the external language is C.
3208 Is_Inlined True if the subprogram is to be inlined.
3210 In addition for function subprograms we have:
3212 Etype Return type of the function.
3214 Each parameter is first checked by calling must_pass_by_ref on its
3215 type to determine if it is passed by reference. For parameters which
3216 are copied in, if they are Ada IN OUT or OUT parameters, their return
3217 value becomes part of a record which becomes the return type of the
3218 function (C function - note that this applies only to Ada procedures
3219 so there is no Ada return type). Additional code to store back the
3220 parameters will be generated on the caller side. This transformation
3221 is done here, not in the front-end.
3223 The intended result of the transformation can be seen from the
3224 equivalent source rewritings that follow:
3226 struct temp {int a,b};
3227 procedure P (A,B: IN OUT ...) is temp P (int A,B) {
3228 .. ..
3229 end P; return {A,B};
3231 procedure call
3234 temp t;
3235 P(X,Y); t = P(X,Y);
3236 X = t.a , Y = t.b;
3239 For subprogram types we need to perform mainly the same conversions to
3240 GCC form that are needed for procedures and function declarations. The
3241 only difference is that at the end, we make a type declaration instead
3242 of a function declaration. */
3244 case E_Subprogram_Type:
3245 case E_Function:
3246 case E_Procedure:
3248 /* The first GCC parameter declaration (a PARM_DECL node). The
3249 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3250 actually is the head of this parameter list. */
3251 tree gnu_param_list = NULL_TREE;
3252 /* The type returned by a function. If the subprogram is a procedure
3253 this type should be void_type_node. */
3254 tree gnu_return_type = void_type_node;
3255 /* List of fields in return type of procedure with copy in copy out
3256 parameters. */
3257 tree gnu_field_list = NULL_TREE;
3258 /* Non-null for subprograms containing parameters passed by copy in
3259 copy out (Ada IN OUT or OUT parameters not passed by reference),
3260 in which case it is the list of nodes used to specify the values of
3261 the in out/out parameters that are returned as a record upon
3262 procedure return. The TREE_PURPOSE of an element of this list is
3263 a field of the record and the TREE_VALUE is the PARM_DECL
3264 corresponding to that field. This list will be saved in the
3265 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3266 tree gnu_return_list = NULL_TREE;
3267 /* If an import pragma asks to map this subprogram to a GCC builtin,
3268 this is the builtin DECL node. */
3269 tree gnu_builtin_decl = NULL_TREE;
3270 Entity_Id gnat_param;
3271 bool inline_flag = Is_Inlined (gnat_entity);
3272 bool public_flag = Is_Public (gnat_entity);
3273 bool extern_flag
3274 = (Is_Public (gnat_entity) && !definition) || imported_p;
3275 bool pure_flag = Is_Pure (gnat_entity);
3276 bool volatile_flag = No_Return (gnat_entity);
3277 bool returns_by_ref = false;
3278 bool returns_unconstrained = false;
3279 bool returns_by_target_ptr = false;
3280 tree gnu_ext_name = create_concat_name (gnat_entity, 0);
3281 bool has_copy_in_out = false;
3282 int parmnum;
3284 if (kind == E_Subprogram_Type && !definition)
3285 /* A parameter may refer to this type, so defer completion
3286 of any incomplete types. */
3287 defer_incomplete_level++, this_deferred = true;
3289 /* If the subprogram has an alias, it is probably inherited, so
3290 we can use the original one. If the original "subprogram"
3291 is actually an enumeration literal, it may be the first use
3292 of its type, so we must elaborate that type now. */
3293 if (Present (Alias (gnat_entity)))
3295 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3296 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3298 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3299 gnu_expr, 0);
3301 /* Elaborate any Itypes in the parameters of this entity. */
3302 for (gnat_temp = First_Formal (gnat_entity);
3303 Present (gnat_temp);
3304 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3305 if (Is_Itype (Etype (gnat_temp)))
3306 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3308 break;
3311 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3312 corresponding DECL node.
3314 We still want the parameter associations to take place because the
3315 proper generation of calls depends on it (a GNAT parameter without
3316 a corresponding GCC tree has a very specific meaning), so we don't
3317 just break here. */
3318 if (Convention (gnat_entity) == Convention_Intrinsic)
3319 gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3321 /* ??? What if we don't find the builtin node above ? warn ? err ?
3322 In the current state we neither warn nor err, and calls will just
3323 be handled as for regular subprograms. */
3325 if (kind == E_Function || kind == E_Subprogram_Type)
3326 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3328 /* If this function returns by reference, make the actual
3329 return type of this function the pointer and mark the decl. */
3330 if (Returns_By_Ref (gnat_entity))
3332 returns_by_ref = true;
3333 gnu_return_type = build_pointer_type (gnu_return_type);
3336 /* If the Mechanism is By_Reference, ensure the return type uses
3337 the machine's by-reference mechanism, which may not the same
3338 as above (e.g., it might be by passing a fake parameter). */
3339 else if (kind == E_Function
3340 && Mechanism (gnat_entity) == By_Reference)
3342 gnu_return_type = copy_type (gnu_return_type);
3343 TREE_ADDRESSABLE (gnu_return_type) = 1;
3346 /* If we are supposed to return an unconstrained array,
3347 actually return a fat pointer and make a note of that. Return
3348 a pointer to an unconstrained record of variable size. */
3349 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3351 gnu_return_type = TREE_TYPE (gnu_return_type);
3352 returns_unconstrained = true;
3355 /* If the type requires a transient scope, the result is allocated
3356 on the secondary stack, so the result type of the function is
3357 just a pointer. */
3358 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3360 gnu_return_type = build_pointer_type (gnu_return_type);
3361 returns_unconstrained = true;
3364 /* If the type is a padded type and the underlying type would not
3365 be passed by reference or this function has a foreign convention,
3366 return the underlying type. */
3367 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3368 && TYPE_IS_PADDING_P (gnu_return_type)
3369 && (!default_pass_by_ref (TREE_TYPE
3370 (TYPE_FIELDS (gnu_return_type)))
3371 || Has_Foreign_Convention (gnat_entity)))
3372 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3374 /* If the return type is unconstrained, that means it must have a
3375 maximum size. We convert the function into a procedure and its
3376 caller will pass a pointer to an object of that maximum size as the
3377 first parameter when we call the function. */
3378 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type)))
3380 returns_by_target_ptr = true;
3381 gnu_param_list
3382 = create_param_decl (get_identifier ("TARGET"),
3383 build_reference_type (gnu_return_type),
3384 true);
3385 gnu_return_type = void_type_node;
3388 /* If the return type has a size that overflows, we cannot have
3389 a function that returns that type. This usage doesn't make
3390 sense anyway, so give an error here. */
3391 if (TYPE_SIZE_UNIT (gnu_return_type)
3392 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
3394 post_error ("cannot return type whose size overflows",
3395 gnat_entity);
3396 gnu_return_type = copy_node (gnu_return_type);
3397 TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
3398 TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
3399 TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
3400 TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
3403 /* Look at all our parameters and get the type of
3404 each. While doing this, build a copy-out structure if
3405 we need one. */
3407 for (gnat_param = First_Formal (gnat_entity), parmnum = 0;
3408 Present (gnat_param);
3409 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3411 tree gnu_param_name = get_entity_name (gnat_param);
3412 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3413 tree gnu_param, gnu_field;
3414 bool by_ref_p = false;
3415 bool by_descr_p = false;
3416 bool by_component_ptr_p = false;
3417 bool copy_in_copy_out_flag = false;
3418 bool req_by_copy = false, req_by_ref = false;
3420 /* Builtins are expanded inline and there is no real call sequence
3421 involved. so the type expected by the underlying expander is
3422 always the type of each argument "as is". */
3423 if (gnu_builtin_decl)
3424 req_by_copy = 1;
3426 /* Otherwise, see if a Mechanism was supplied that forced this
3427 parameter to be passed one way or another. */
3428 else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3429 req_by_copy = true;
3430 else if (Mechanism (gnat_param) == Default)
3432 else if (Mechanism (gnat_param) == By_Copy)
3433 req_by_copy = true;
3434 else if (Mechanism (gnat_param) == By_Reference)
3435 req_by_ref = true;
3436 else if (Mechanism (gnat_param) <= By_Descriptor)
3437 by_descr_p = true;
3438 else if (Mechanism (gnat_param) > 0)
3440 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3441 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3442 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3443 Mechanism (gnat_param)))
3444 req_by_ref = true;
3445 else
3446 req_by_copy = true;
3448 else
3449 post_error ("unsupported mechanism for&", gnat_param);
3451 /* If this is either a foreign function or if the
3452 underlying type won't be passed by refererence, strip off
3453 possible padding type. */
3454 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
3455 && TYPE_IS_PADDING_P (gnu_param_type)
3456 && (req_by_ref || Has_Foreign_Convention (gnat_entity)
3457 || !must_pass_by_ref (TREE_TYPE (TYPE_FIELDS
3458 (gnu_param_type)))))
3459 gnu_param_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
3461 /* If this is an IN parameter it is read-only, so make a variant
3462 of the type that is read-only.
3464 ??? However, if this is an unconstrained array, that type can
3465 be very complex. So skip it for now. Likewise for any other
3466 self-referential type. */
3467 if (Ekind (gnat_param) == E_In_Parameter
3468 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
3469 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
3470 gnu_param_type
3471 = build_qualified_type (gnu_param_type,
3472 (TYPE_QUALS (gnu_param_type)
3473 | TYPE_QUAL_CONST));
3475 /* For foreign conventions, pass arrays as a pointer to the
3476 underlying type. First check for unconstrained array and get
3477 the underlying array. Then get the component type and build
3478 a pointer to it. */
3479 if (Has_Foreign_Convention (gnat_entity)
3480 && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
3481 gnu_param_type
3482 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3483 (TREE_TYPE (gnu_param_type))));
3485 if (by_descr_p)
3486 gnu_param_type
3487 = build_pointer_type
3488 (build_vms_descriptor (gnu_param_type,
3489 Mechanism (gnat_param), gnat_entity));
3491 else if (Has_Foreign_Convention (gnat_entity)
3492 && !req_by_copy
3493 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
3495 /* Strip off any multi-dimensional entries, then strip
3496 off the last array to get the component type. */
3497 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
3498 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
3499 gnu_param_type = TREE_TYPE (gnu_param_type);
3501 by_component_ptr_p = true;
3502 gnu_param_type = TREE_TYPE (gnu_param_type);
3504 if (Ekind (gnat_param) == E_In_Parameter)
3505 gnu_param_type
3506 = build_qualified_type (gnu_param_type,
3507 (TYPE_QUALS (gnu_param_type)
3508 | TYPE_QUAL_CONST));
3510 gnu_param_type = build_pointer_type (gnu_param_type);
3513 /* Fat pointers are passed as thin pointers for foreign
3514 conventions. */
3515 else if (Has_Foreign_Convention (gnat_entity)
3516 && TYPE_FAT_POINTER_P (gnu_param_type))
3517 gnu_param_type
3518 = make_type_from_size (gnu_param_type,
3519 size_int (POINTER_SIZE), false);
3521 /* If we must pass or were requested to pass by reference, do so.
3522 If we were requested to pass by copy, do so.
3523 Otherwise, for foreign conventions, pass all in out parameters
3524 or aggregates by reference. For COBOL and Fortran, pass
3525 all integer and FP types that way too. For Convention Ada,
3526 use the standard Ada default. */
3527 else if (must_pass_by_ref (gnu_param_type) || req_by_ref
3528 || (!req_by_copy
3529 && ((Has_Foreign_Convention (gnat_entity)
3530 && (Ekind (gnat_param) != E_In_Parameter
3531 || AGGREGATE_TYPE_P (gnu_param_type)))
3532 || (((Convention (gnat_entity)
3533 == Convention_Fortran)
3534 || (Convention (gnat_entity)
3535 == Convention_COBOL))
3536 && (INTEGRAL_TYPE_P (gnu_param_type)
3537 || FLOAT_TYPE_P (gnu_param_type)))
3538 /* For convention Ada, see if we pass by reference
3539 by default. */
3540 || (!Has_Foreign_Convention (gnat_entity)
3541 && default_pass_by_ref (gnu_param_type)))))
3543 gnu_param_type = build_reference_type (gnu_param_type);
3544 by_ref_p = true;
3547 else if (Ekind (gnat_param) != E_In_Parameter)
3548 copy_in_copy_out_flag = true;
3550 if (req_by_copy && (by_ref_p || by_component_ptr_p))
3551 post_error ("?cannot pass & by copy", gnat_param);
3553 /* If this is an OUT parameter that isn't passed by reference
3554 and isn't a pointer or aggregate, we don't make a PARM_DECL
3555 for it. Instead, it will be a VAR_DECL created when we process
3556 the procedure. For the special parameter of Valued_Procedure,
3557 never pass it in.
3559 An exception is made to cover the RM-6.4.1 rule requiring "by
3560 copy" out parameters with discriminants or implicit initial
3561 values to be handled like in out parameters. These type are
3562 normally built as aggregates, and hence passed by reference,
3563 except for some packed arrays which end up encoded in special
3564 integer types.
3566 The exception we need to make is then for packed arrays of
3567 records with discriminants or implicit initial values. We have
3568 no light/easy way to check for the latter case, so we merely
3569 check for packed arrays of records. This may lead to useless
3570 copy-in operations, but in very rare cases only, as these would
3571 be exceptions in a set of already exceptional situations. */
3572 if (Ekind (gnat_param) == E_Out_Parameter && !by_ref_p
3573 && ((Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3574 || (!by_descr_p
3575 && !POINTER_TYPE_P (gnu_param_type)
3576 && !AGGREGATE_TYPE_P (gnu_param_type)))
3577 && !(Is_Array_Type (Etype (gnat_param))
3578 && Is_Packed (Etype (gnat_param))
3579 && Is_Composite_Type (Component_Type
3580 (Etype (gnat_param)))))
3581 gnu_param = NULL_TREE;
3582 else
3584 gnu_param
3585 = create_param_decl
3586 (gnu_param_name, gnu_param_type,
3587 by_ref_p || by_component_ptr_p
3588 || Ekind (gnat_param) == E_In_Parameter);
3590 DECL_BY_REF_P (gnu_param) = by_ref_p;
3591 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr_p;
3592 DECL_BY_DESCRIPTOR_P (gnu_param) = by_descr_p;
3593 DECL_POINTS_TO_READONLY_P (gnu_param)
3594 = (Ekind (gnat_param) == E_In_Parameter
3595 && (by_ref_p || by_component_ptr_p));
3596 Sloc_to_locus (Sloc (gnat_param),
3597 &DECL_SOURCE_LOCATION (gnu_param));
3598 save_gnu_tree (gnat_param, gnu_param, false);
3599 gnu_param_list = chainon (gnu_param, gnu_param_list);
3601 /* If a parameter is a pointer, this function may modify
3602 memory through it and thus shouldn't be considered
3603 a pure function. Also, the memory may be modified
3604 between two calls, so they can't be CSE'ed. The latter
3605 case also handles by-ref parameters. */
3606 if (POINTER_TYPE_P (gnu_param_type)
3607 || TYPE_FAT_POINTER_P (gnu_param_type))
3608 pure_flag = false;
3611 if (copy_in_copy_out_flag)
3613 if (!has_copy_in_out)
3615 gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
3616 gnu_return_type = make_node (RECORD_TYPE);
3617 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3618 has_copy_in_out = true;
3621 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3622 gnu_return_type, 0, 0, 0, 0);
3623 Sloc_to_locus (Sloc (gnat_param),
3624 &DECL_SOURCE_LOCATION (gnu_field));
3625 TREE_CHAIN (gnu_field) = gnu_field_list;
3626 gnu_field_list = gnu_field;
3627 gnu_return_list = tree_cons (gnu_field, gnu_param,
3628 gnu_return_list);
3632 /* Do not compute record for out parameters if subprogram is
3633 stubbed since structures are incomplete for the back-end. */
3634 if (gnu_field_list
3635 && Convention (gnat_entity) != Convention_Stubbed)
3636 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3637 false, false);
3639 /* If we have a CICO list but it has only one entry, we convert
3640 this function into a function that simply returns that one
3641 object. */
3642 if (list_length (gnu_return_list) == 1)
3643 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3645 #ifdef _WIN32
3646 if (Convention (gnat_entity) == Convention_Stdcall)
3648 struct attrib *attr
3649 = (struct attrib *) xmalloc (sizeof (struct attrib));
3651 attr->next = attr_list;
3652 attr->type = ATTR_MACHINE_ATTRIBUTE;
3653 attr->name = get_identifier ("stdcall");
3654 attr->args = NULL_TREE;
3655 attr->error_point = gnat_entity;
3656 attr_list = attr;
3658 #endif
3660 /* Both lists ware built in reverse. */
3661 gnu_param_list = nreverse (gnu_param_list);
3662 gnu_return_list = nreverse (gnu_return_list);
3664 gnu_type
3665 = create_subprog_type (gnu_return_type, gnu_param_list,
3666 gnu_return_list, returns_unconstrained,
3667 returns_by_ref,
3668 Function_Returns_With_DSP (gnat_entity),
3669 returns_by_target_ptr);
3671 /* A subprogram (something that doesn't return anything) shouldn't
3672 be considered Pure since there would be no reason for such a
3673 subprogram. Note that procedures with Out (or In Out) parameters
3674 have already been converted into a function with a return type. */
3675 if (TREE_CODE (gnu_return_type) == VOID_TYPE)
3676 pure_flag = false;
3678 gnu_type
3679 = build_qualified_type (gnu_type,
3680 (TYPE_QUALS (gnu_type)
3681 | (TYPE_QUAL_CONST * pure_flag)
3682 | (TYPE_QUAL_VOLATILE * volatile_flag)));
3684 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3686 /* If we have a builtin decl for that function, check the signatures
3687 compatibilities. If the signatures are compatible, use the builtin
3688 decl. If they are not, we expect the checker predicate to have
3689 posted the appropriate errors, and just continue with what we have
3690 so far. */
3691 if (gnu_builtin_decl)
3693 tree gnu_builtin_type = TREE_TYPE (gnu_builtin_decl);
3695 if (compatible_signatures_p (gnu_type, gnu_builtin_type))
3697 gnu_decl = gnu_builtin_decl;
3698 gnu_type = gnu_builtin_type;
3699 break;
3703 /* If there was no specified Interface_Name and the external and
3704 internal names of the subprogram are the same, only use the
3705 internal name to allow disambiguation of nested subprograms. */
3706 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_id)
3707 gnu_ext_name = NULL_TREE;
3709 /* If we are defining the subprogram and it has an Address clause
3710 we must get the address expression from the saved GCC tree for the
3711 subprogram if it has a Freeze_Node. Otherwise, we elaborate
3712 the address expression here since the front-end has guaranteed
3713 in that case that the elaboration has no effects. If there is
3714 an Address clause and we are not defining the object, just
3715 make it a constant. */
3716 if (Present (Address_Clause (gnat_entity)))
3718 tree gnu_address = NULL_TREE;
3720 if (definition)
3721 gnu_address
3722 = (present_gnu_tree (gnat_entity)
3723 ? get_gnu_tree (gnat_entity)
3724 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
3726 save_gnu_tree (gnat_entity, NULL_TREE, false);
3728 gnu_type = build_reference_type (gnu_type);
3729 if (gnu_address)
3730 gnu_address = convert (gnu_type, gnu_address);
3732 gnu_decl
3733 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
3734 gnu_address, false, Is_Public (gnat_entity),
3735 extern_flag, false, NULL, gnat_entity);
3736 DECL_BY_REF_P (gnu_decl) = 1;
3739 else if (kind == E_Subprogram_Type)
3740 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3741 !Comes_From_Source (gnat_entity),
3742 debug_info_p, gnat_entity);
3743 else
3745 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
3746 gnu_type, gnu_param_list,
3747 inline_flag, public_flag,
3748 extern_flag, attr_list,
3749 gnat_entity);
3750 DECL_STUBBED_P (gnu_decl)
3751 = Convention (gnat_entity) == Convention_Stubbed;
3754 break;
3756 case E_Incomplete_Type:
3757 case E_Private_Type:
3758 case E_Limited_Private_Type:
3759 case E_Record_Type_With_Private:
3760 case E_Private_Subtype:
3761 case E_Limited_Private_Subtype:
3762 case E_Record_Subtype_With_Private:
3764 /* If this type does not have a full view in the unit we are
3765 compiling, then just get the type from its Etype. */
3766 if (No (Full_View (gnat_entity)))
3768 /* If this is an incomplete type with no full view, it must
3769 be a Taft Amendement type, so just return a dummy type. */
3770 if (kind == E_Incomplete_Type)
3771 gnu_type = make_dummy_type (gnat_entity);
3773 else if (Present (Underlying_Full_View (gnat_entity)))
3774 gnu_decl = gnat_to_gnu_entity (Underlying_Full_View (gnat_entity),
3775 NULL_TREE, 0);
3776 else
3778 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
3779 NULL_TREE, 0);
3780 maybe_present = true;
3783 break;
3786 /* Otherwise, if we are not defining the type now, get the
3787 type from the full view. But always get the type from the full
3788 view for define on use types, since otherwise we won't see them! */
3790 else if (!definition
3791 || (Is_Itype (Full_View (gnat_entity))
3792 && No (Freeze_Node (gnat_entity)))
3793 || (Is_Itype (gnat_entity)
3794 && No (Freeze_Node (Full_View (gnat_entity)))))
3796 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
3797 NULL_TREE, 0);
3798 maybe_present = true;
3799 break;
3802 /* For incomplete types, make a dummy type entry which will be
3803 replaced later. */
3804 gnu_type = make_dummy_type (gnat_entity);
3806 /* Save this type as the full declaration's type so we can do any needed
3807 updates when we see it. */
3808 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3809 !Comes_From_Source (gnat_entity),
3810 debug_info_p, gnat_entity);
3811 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
3812 break;
3814 /* Simple class_wide types are always viewed as their root_type
3815 by Gigi unless an Equivalent_Type is specified. */
3816 case E_Class_Wide_Type:
3817 if (Present (Equivalent_Type (gnat_entity)))
3818 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3819 else
3820 gnu_type = gnat_to_gnu_type (Root_Type (gnat_entity));
3822 maybe_present = true;
3823 break;
3825 case E_Task_Type:
3826 case E_Task_Subtype:
3827 case E_Protected_Type:
3828 case E_Protected_Subtype:
3829 if (type_annotate_only && No (Corresponding_Record_Type (gnat_entity)))
3830 gnu_type = void_type_node;
3831 else
3832 gnu_type = gnat_to_gnu_type (Corresponding_Record_Type (gnat_entity));
3834 maybe_present = true;
3835 break;
3837 case E_Label:
3838 gnu_decl = create_label_decl (gnu_entity_id);
3839 break;
3841 case E_Block:
3842 case E_Loop:
3843 /* Nothing at all to do here, so just return an ERROR_MARK and claim
3844 we've already saved it, so we don't try to. */
3845 gnu_decl = error_mark_node;
3846 saved = true;
3847 break;
3849 default:
3850 gcc_unreachable ();
3853 /* If we had a case where we evaluated another type and it might have
3854 defined this one, handle it here. */
3855 if (maybe_present && present_gnu_tree (gnat_entity))
3857 gnu_decl = get_gnu_tree (gnat_entity);
3858 saved = true;
3861 /* If we are processing a type and there is either no decl for it or
3862 we just made one, do some common processing for the type, such as
3863 handling alignment and possible padding. */
3865 if ((!gnu_decl || this_made_decl) && IN (kind, Type_Kind))
3867 if (Is_Tagged_Type (gnat_entity)
3868 || Is_Class_Wide_Equivalent_Type (gnat_entity))
3869 TYPE_ALIGN_OK (gnu_type) = 1;
3871 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
3872 TYPE_BY_REFERENCE_P (gnu_type) = 1;
3874 /* ??? Don't set the size for a String_Literal since it is either
3875 confirming or we don't handle it properly (if the low bound is
3876 non-constant). */
3877 if (!gnu_size && kind != E_String_Literal_Subtype)
3878 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
3879 TYPE_DECL, false,
3880 Has_Size_Clause (gnat_entity));
3882 /* If a size was specified, see if we can make a new type of that size
3883 by rearranging the type, for example from a fat to a thin pointer. */
3884 if (gnu_size)
3886 gnu_type
3887 = make_type_from_size (gnu_type, gnu_size,
3888 Has_Biased_Representation (gnat_entity));
3890 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
3891 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
3892 gnu_size = 0;
3895 /* If the alignment hasn't already been processed and this is
3896 not an unconstrained array, see if an alignment is specified.
3897 If not, we pick a default alignment for atomic objects. */
3898 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
3900 else if (Known_Alignment (gnat_entity))
3901 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
3902 TYPE_ALIGN (gnu_type));
3903 else if (Is_Atomic (gnat_entity) && !gnu_size
3904 && host_integerp (TYPE_SIZE (gnu_type), 1)
3905 && integer_pow2p (TYPE_SIZE (gnu_type)))
3906 align = MIN (BIGGEST_ALIGNMENT,
3907 tree_low_cst (TYPE_SIZE (gnu_type), 1));
3908 else if (Is_Atomic (gnat_entity) && gnu_size
3909 && host_integerp (gnu_size, 1)
3910 && integer_pow2p (gnu_size))
3911 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
3913 /* See if we need to pad the type. If we did, and made a record,
3914 the name of the new type may be changed. So get it back for
3915 us when we make the new TYPE_DECL below. */
3916 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity, "PAD",
3917 true, definition, false);
3918 if (TREE_CODE (gnu_type) == RECORD_TYPE
3919 && TYPE_IS_PADDING_P (gnu_type))
3921 gnu_entity_id = TYPE_NAME (gnu_type);
3922 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
3923 gnu_entity_id = DECL_NAME (gnu_entity_id);
3926 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
3928 /* If we are at global level, GCC will have applied variable_size to
3929 the type, but that won't have done anything. So, if it's not
3930 a constant or self-referential, call elaborate_expression_1 to
3931 make a variable for the size rather than calculating it each time.
3932 Handle both the RM size and the actual size. */
3933 if (global_bindings_p ()
3934 && TYPE_SIZE (gnu_type)
3935 && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
3936 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3938 if (TREE_CODE (gnu_type) == RECORD_TYPE
3939 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
3940 TYPE_SIZE (gnu_type), 0))
3942 TYPE_SIZE (gnu_type)
3943 = elaborate_expression_1 (gnat_entity, gnat_entity,
3944 TYPE_SIZE (gnu_type),
3945 get_identifier ("SIZE"),
3946 definition, 0);
3947 SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
3949 else
3951 TYPE_SIZE (gnu_type)
3952 = elaborate_expression_1 (gnat_entity, gnat_entity,
3953 TYPE_SIZE (gnu_type),
3954 get_identifier ("SIZE"),
3955 definition, 0);
3957 /* ??? For now, store the size as a multiple of the alignment
3958 in bytes so that we can see the alignment from the tree. */
3959 TYPE_SIZE_UNIT (gnu_type)
3960 = build_binary_op
3961 (MULT_EXPR, sizetype,
3962 elaborate_expression_1
3963 (gnat_entity, gnat_entity,
3964 build_binary_op (EXACT_DIV_EXPR, sizetype,
3965 TYPE_SIZE_UNIT (gnu_type),
3966 size_int (TYPE_ALIGN (gnu_type)
3967 / BITS_PER_UNIT)),
3968 get_identifier ("SIZE_A_UNIT"),
3969 definition, 0),
3970 size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
3972 if (TREE_CODE (gnu_type) == RECORD_TYPE)
3973 SET_TYPE_ADA_SIZE
3974 (gnu_type,
3975 elaborate_expression_1 (gnat_entity,
3976 gnat_entity,
3977 TYPE_ADA_SIZE (gnu_type),
3978 get_identifier ("RM_SIZE"),
3979 definition, 0));
3983 /* If this is a record type or subtype, call elaborate_expression_1 on
3984 any field position. Do this for both global and local types.
3985 Skip any fields that we haven't made trees for to avoid problems with
3986 class wide types. */
3987 if (IN (kind, Record_Kind))
3988 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3989 gnat_temp = Next_Entity (gnat_temp))
3990 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
3992 tree gnu_field = get_gnu_tree (gnat_temp);
3994 /* ??? Unfortunately, GCC needs to be able to prove the
3995 alignment of this offset and if it's a variable, it can't.
3996 In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
3997 right now, we have to put in an explicit multiply and
3998 divide by that value. */
3999 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4000 DECL_FIELD_OFFSET (gnu_field)
4001 = build_binary_op
4002 (MULT_EXPR, sizetype,
4003 elaborate_expression_1
4004 (gnat_temp, gnat_temp,
4005 build_binary_op (EXACT_DIV_EXPR, sizetype,
4006 DECL_FIELD_OFFSET (gnu_field),
4007 size_int (DECL_OFFSET_ALIGN (gnu_field)
4008 / BITS_PER_UNIT)),
4009 get_identifier ("OFFSET"),
4010 definition, 0),
4011 size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
4014 gnu_type = build_qualified_type (gnu_type,
4015 (TYPE_QUALS (gnu_type)
4016 | (TYPE_QUAL_VOLATILE
4017 * Treat_As_Volatile (gnat_entity))));
4019 if (Is_Atomic (gnat_entity))
4020 check_ok_for_atomic (gnu_type, gnat_entity, false);
4022 if (Known_Alignment (gnat_entity))
4023 TYPE_USER_ALIGN (gnu_type) = 1;
4025 if (!gnu_decl)
4026 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4027 !Comes_From_Source (gnat_entity),
4028 debug_info_p, gnat_entity);
4029 else
4030 TREE_TYPE (gnu_decl) = gnu_type;
4033 if (IN (kind, Type_Kind) && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4035 gnu_type = TREE_TYPE (gnu_decl);
4037 /* Back-annotate the Alignment of the type if not already in the
4038 tree. Likewise for sizes. */
4039 if (Unknown_Alignment (gnat_entity))
4040 Set_Alignment (gnat_entity,
4041 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4043 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4045 /* If the size is self-referential, we annotate the maximum
4046 value of that size. */
4047 tree gnu_size = TYPE_SIZE (gnu_type);
4049 if (CONTAINS_PLACEHOLDER_P (gnu_size))
4050 gnu_size = max_size (gnu_size, true);
4052 Set_Esize (gnat_entity, annotate_value (gnu_size));
4054 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4056 /* In this mode the tag and the parent components are not
4057 generated by the front-end, so the sizes must be adjusted
4058 explicitly now. */
4060 int size_offset;
4061 int new_size;
4063 if (Is_Derived_Type (gnat_entity))
4065 size_offset
4066 = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
4067 Set_Alignment (gnat_entity,
4068 Alignment (Etype (Base_Type (gnat_entity))));
4070 else
4071 size_offset = POINTER_SIZE;
4073 new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
4074 Set_Esize (gnat_entity,
4075 UI_From_Int (((new_size + (POINTER_SIZE - 1))
4076 / POINTER_SIZE) * POINTER_SIZE));
4077 Set_RM_Size (gnat_entity, Esize (gnat_entity));
4081 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4082 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4085 if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4086 DECL_ARTIFICIAL (gnu_decl) = 1;
4088 if (!debug_info_p && DECL_P (gnu_decl)
4089 && TREE_CODE (gnu_decl) != FUNCTION_DECL)
4090 DECL_IGNORED_P (gnu_decl) = 1;
4092 /* If we haven't already, associate the ..._DECL node that we just made with
4093 the input GNAT entity node. */
4094 if (!saved)
4095 save_gnu_tree (gnat_entity, gnu_decl, false);
4097 /* If this is an enumeral or floating-point type, we were not able to set
4098 the bounds since they refer to the type. These bounds are always static.
4100 For enumeration types, also write debugging information and declare the
4101 enumeration literal table, if needed. */
4103 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4104 || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4106 tree gnu_scalar_type = gnu_type;
4108 /* If this is a padded type, we need to use the underlying type. */
4109 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
4110 && TYPE_IS_PADDING_P (gnu_scalar_type))
4111 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4113 /* If this is a floating point type and we haven't set a floating
4114 point type yet, use this in the evaluation of the bounds. */
4115 if (!longest_float_type_node && kind == E_Floating_Point_Type)
4116 longest_float_type_node = gnu_type;
4118 TYPE_MIN_VALUE (gnu_scalar_type)
4119 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4120 TYPE_MAX_VALUE (gnu_scalar_type)
4121 = gnat_to_gnu (Type_High_Bound (gnat_entity));
4123 if (TREE_CODE (gnu_scalar_type) == ENUMERAL_TYPE)
4125 TYPE_STUB_DECL (gnu_scalar_type) = gnu_decl;
4127 /* Since this has both a typedef and a tag, avoid outputting
4128 the name twice. */
4129 DECL_ARTIFICIAL (gnu_decl) = 1;
4130 rest_of_type_compilation (gnu_scalar_type, global_bindings_p ());
4134 /* If we deferred processing of incomplete types, re-enable it. If there
4135 were no other disables and we have some to process, do so. */
4136 if (this_deferred && --defer_incomplete_level == 0 && defer_incomplete_list)
4138 struct incomplete *incp = defer_incomplete_list;
4139 struct incomplete *next;
4141 defer_incomplete_list = NULL;
4142 for (; incp; incp = next)
4144 next = incp->next;
4146 if (incp->old_type)
4147 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4148 gnat_to_gnu_type (incp->full_type));
4149 free (incp);
4153 /* If we are not defining this type, see if it's in the incomplete list.
4154 If so, handle that list entry now. */
4155 else if (!definition)
4157 struct incomplete *incp;
4159 for (incp = defer_incomplete_list; incp; incp = incp->next)
4160 if (incp->old_type && incp->full_type == gnat_entity)
4162 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4163 TREE_TYPE (gnu_decl));
4164 incp->old_type = NULL_TREE;
4168 if (this_global)
4169 force_global--;
4171 if (Is_Packed_Array_Type (gnat_entity)
4172 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
4173 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
4174 && !present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
4175 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
4177 return gnu_decl;
4180 /* Given GNAT_ENTITY, elaborate all expressions that are required to
4181 be elaborated at the point of its definition, but do nothing else. */
4183 void
4184 elaborate_entity (Entity_Id gnat_entity)
4186 switch (Ekind (gnat_entity))
4188 case E_Signed_Integer_Subtype:
4189 case E_Modular_Integer_Subtype:
4190 case E_Enumeration_Subtype:
4191 case E_Ordinary_Fixed_Point_Subtype:
4192 case E_Decimal_Fixed_Point_Subtype:
4193 case E_Floating_Point_Subtype:
4195 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
4196 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
4198 /* ??? Tests for avoiding static constaint error expression
4199 is needed until the front stops generating bogus conversions
4200 on bounds of real types. */
4202 if (!Raises_Constraint_Error (gnat_lb))
4203 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
4204 1, 0, Needs_Debug_Info (gnat_entity));
4205 if (!Raises_Constraint_Error (gnat_hb))
4206 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
4207 1, 0, Needs_Debug_Info (gnat_entity));
4208 break;
4211 case E_Record_Type:
4213 Node_Id full_definition = Declaration_Node (gnat_entity);
4214 Node_Id record_definition = Type_Definition (full_definition);
4216 /* If this is a record extension, go a level further to find the
4217 record definition. */
4218 if (Nkind (record_definition) == N_Derived_Type_Definition)
4219 record_definition = Record_Extension_Part (record_definition);
4221 break;
4223 case E_Record_Subtype:
4224 case E_Private_Subtype:
4225 case E_Limited_Private_Subtype:
4226 case E_Record_Subtype_With_Private:
4227 if (Is_Constrained (gnat_entity)
4228 && Has_Discriminants (Base_Type (gnat_entity))
4229 && Present (Discriminant_Constraint (gnat_entity)))
4231 Node_Id gnat_discriminant_expr;
4232 Entity_Id gnat_field;
4234 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
4235 gnat_discriminant_expr
4236 = First_Elmt (Discriminant_Constraint (gnat_entity));
4237 Present (gnat_field);
4238 gnat_field = Next_Discriminant (gnat_field),
4239 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
4240 /* ??? For now, ignore access discriminants. */
4241 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
4242 elaborate_expression (Node (gnat_discriminant_expr),
4243 gnat_entity,
4244 get_entity_name (gnat_field), 1, 0, 0);
4246 break;
4251 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
4252 any entities on its entity chain similarly. */
4254 void
4255 mark_out_of_scope (Entity_Id gnat_entity)
4257 Entity_Id gnat_sub_entity;
4258 unsigned int kind = Ekind (gnat_entity);
4260 /* If this has an entity list, process all in the list. */
4261 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
4262 || IN (kind, Private_Kind)
4263 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
4264 || kind == E_Function || kind == E_Generic_Function
4265 || kind == E_Generic_Package || kind == E_Generic_Procedure
4266 || kind == E_Loop || kind == E_Operator || kind == E_Package
4267 || kind == E_Package_Body || kind == E_Procedure
4268 || kind == E_Record_Type || kind == E_Record_Subtype
4269 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
4270 for (gnat_sub_entity = First_Entity (gnat_entity);
4271 Present (gnat_sub_entity);
4272 gnat_sub_entity = Next_Entity (gnat_sub_entity))
4273 if (Scope (gnat_sub_entity) == gnat_entity
4274 && gnat_sub_entity != gnat_entity)
4275 mark_out_of_scope (gnat_sub_entity);
4277 /* Now clear this if it has been defined, but only do so if it isn't
4278 a subprogram or parameter. We could refine this, but it isn't
4279 worth it. If this is statically allocated, it is supposed to
4280 hang around out of cope. */
4281 if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
4282 && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
4284 save_gnu_tree (gnat_entity, NULL_TREE, true);
4285 save_gnu_tree (gnat_entity, error_mark_node, true);
4289 /* Set the alias set of GNU_NEW_TYPE to be that of GNU_OLD_TYPE. If this
4290 is a multi-dimensional array type, do this recursively. */
4292 static void
4293 copy_alias_set (tree gnu_new_type, tree gnu_old_type)
4295 if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
4296 && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
4297 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
4299 /* We need to be careful here in case GNU_OLD_TYPE is an unconstrained
4300 array. In that case, it doesn't have the same shape as GNU_NEW_TYPE,
4301 so we need to go down to what does. */
4302 if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
4303 gnu_old_type
4304 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
4306 copy_alias_set (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type));
4309 TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
4310 record_component_aliases (gnu_new_type);
4313 /* Return a TREE_LIST describing the substitutions needed to reflect
4314 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
4315 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
4316 of GNAT_SUBTYPE. The substitions can be in any order. TREE_PURPOSE
4317 gives the tree for the discriminant and TREE_VALUES is the replacement
4318 value. They are in the form of operands to substitute_in_expr.
4319 DEFINITION is as in gnat_to_gnu_entity. */
4321 static tree
4322 substitution_list (Entity_Id gnat_subtype, Entity_Id gnat_type,
4323 tree gnu_list, bool definition)
4325 Entity_Id gnat_discrim;
4326 Node_Id gnat_value;
4328 if (No (gnat_type))
4329 gnat_type = Implementation_Base_Type (gnat_subtype);
4331 if (Has_Discriminants (gnat_type))
4332 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
4333 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
4334 Present (gnat_discrim);
4335 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
4336 gnat_value = Next_Elmt (gnat_value))
4337 /* Ignore access discriminants. */
4338 if (!Is_Access_Type (Etype (Node (gnat_value))))
4339 gnu_list = tree_cons (gnat_to_gnu_entity (gnat_discrim, NULL_TREE, 0),
4340 elaborate_expression
4341 (Node (gnat_value), gnat_subtype,
4342 get_entity_name (gnat_discrim), definition,
4343 1, 0),
4344 gnu_list);
4346 return gnu_list;
4349 /* For the following two functions: for each GNAT entity, the GCC
4350 tree node used as a dummy for that entity, if any. */
4352 static GTY((length ("max_gnat_nodes"))) tree * dummy_node_table;
4354 /* Initialize the above table. */
4356 void
4357 init_dummy_type (void)
4359 Node_Id gnat_node;
4361 dummy_node_table = (tree *) ggc_alloc (max_gnat_nodes * sizeof (tree));
4363 for (gnat_node = 0; gnat_node < max_gnat_nodes; gnat_node++)
4364 dummy_node_table[gnat_node] = NULL_TREE;
4366 dummy_node_table -= First_Node_Id;
4369 /* Make a dummy type corresponding to GNAT_TYPE. */
4371 tree
4372 make_dummy_type (Entity_Id gnat_type)
4374 Entity_Id gnat_underlying;
4375 tree gnu_type;
4377 /* Find a full type for GNAT_TYPE, taking into account any class wide
4378 types. */
4379 if (Is_Class_Wide_Type (gnat_type) && Present (Equivalent_Type (gnat_type)))
4380 gnat_type = Equivalent_Type (gnat_type);
4381 else if (Ekind (gnat_type) == E_Class_Wide_Type)
4382 gnat_type = Root_Type (gnat_type);
4384 for (gnat_underlying = gnat_type;
4385 (IN (Ekind (gnat_underlying), Incomplete_Or_Private_Kind)
4386 && Present (Full_View (gnat_underlying)));
4387 gnat_underlying = Full_View (gnat_underlying))
4390 /* If it there already a dummy type, use that one. Else make one. */
4391 if (dummy_node_table[gnat_underlying])
4392 return dummy_node_table[gnat_underlying];
4394 /* If this is a record, make this a RECORD_TYPE or UNION_TYPE; else make
4395 it a VOID_TYPE. */
4396 if (Is_Unchecked_Union (gnat_underlying))
4398 gnu_type = make_node (UNION_TYPE);
4399 TYPE_UNCHECKED_UNION_P (gnu_type) = 1;
4401 else if (Is_Record_Type (gnat_underlying))
4402 gnu_type = make_node (RECORD_TYPE);
4403 else
4404 gnu_type = make_node (ENUMERAL_TYPE);
4406 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
4407 TYPE_DUMMY_P (gnu_type) = 1;
4408 if (AGGREGATE_TYPE_P (gnu_type))
4409 TYPE_STUB_DECL (gnu_type) = build_decl (TYPE_DECL, NULL_TREE, gnu_type);
4411 dummy_node_table[gnat_underlying] = gnu_type;
4413 return gnu_type;
4416 /* Return true if the size represented by GNU_SIZE can be handled by an
4417 allocation. If STATIC_P is true, consider only what can be done with a
4418 static allocation. */
4420 static bool
4421 allocatable_size_p (tree gnu_size, bool static_p)
4423 HOST_WIDE_INT our_size;
4425 /* If this is not a static allocation, the only case we want to forbid
4426 is an overflowing size. That will be converted into a raise a
4427 Storage_Error. */
4428 if (!static_p)
4429 return !(TREE_CODE (gnu_size) == INTEGER_CST
4430 && TREE_CONSTANT_OVERFLOW (gnu_size));
4432 /* Otherwise, we need to deal with both variable sizes and constant
4433 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
4434 since assemblers may not like very large sizes. */
4435 if (!host_integerp (gnu_size, 1))
4436 return false;
4438 our_size = tree_low_cst (gnu_size, 1);
4439 return (int) our_size == our_size;
4442 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
4444 static void
4445 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
4447 Node_Id gnat_temp;
4449 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
4450 gnat_temp = Next_Rep_Item (gnat_temp))
4451 if (Nkind (gnat_temp) == N_Pragma)
4453 struct attrib *attr;
4454 tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
4455 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
4456 enum attr_type etype;
4458 if (Present (gnat_assoc) && Present (First (gnat_assoc))
4459 && Present (Next (First (gnat_assoc)))
4460 && (Nkind (Expression (Next (First (gnat_assoc))))
4461 == N_String_Literal))
4463 gnu_arg0 = get_identifier (TREE_STRING_POINTER
4464 (gnat_to_gnu
4465 (Expression (Next
4466 (First (gnat_assoc))))));
4467 if (Present (Next (Next (First (gnat_assoc))))
4468 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
4469 == N_String_Literal))
4470 gnu_arg1 = get_identifier (TREE_STRING_POINTER
4471 (gnat_to_gnu
4472 (Expression
4473 (Next (Next
4474 (First (gnat_assoc)))))));
4477 switch (Get_Pragma_Id (Chars (gnat_temp)))
4479 case Pragma_Machine_Attribute:
4480 etype = ATTR_MACHINE_ATTRIBUTE;
4481 break;
4483 case Pragma_Linker_Alias:
4484 etype = ATTR_LINK_ALIAS;
4485 break;
4487 case Pragma_Linker_Section:
4488 etype = ATTR_LINK_SECTION;
4489 break;
4491 case Pragma_Weak_External:
4492 etype = ATTR_WEAK_EXTERNAL;
4493 break;
4495 default:
4496 continue;
4499 attr = (struct attrib *) xmalloc (sizeof (struct attrib));
4500 attr->next = *attr_list;
4501 attr->type = etype;
4502 attr->name = gnu_arg0;
4504 /* If we have an argument specified together with an attribute name,
4505 make it a single TREE_VALUE entry in a list of arguments, as GCC
4506 expects it. */
4507 if (gnu_arg1 != NULL_TREE)
4508 attr->args = build_tree_list (NULL_TREE, gnu_arg1);
4509 else
4510 attr->args = NULL_TREE;
4512 attr->error_point
4513 = Present (Next (First (gnat_assoc)))
4514 ? Expression (Next (First (gnat_assoc))) : gnat_temp;
4515 *attr_list = attr;
4519 /* Get the unpadded version of a GNAT type. */
4521 tree
4522 get_unpadded_type (Entity_Id gnat_entity)
4524 tree type = gnat_to_gnu_type (gnat_entity);
4526 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4527 type = TREE_TYPE (TYPE_FIELDS (type));
4529 return type;
4532 /* Called when we need to protect a variable object using a save_expr. */
4534 tree
4535 maybe_variable (tree gnu_operand)
4537 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
4538 || TREE_CODE (gnu_operand) == SAVE_EXPR
4539 || TREE_CODE (gnu_operand) == NULL_EXPR)
4540 return gnu_operand;
4542 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
4544 tree gnu_result = build1 (UNCONSTRAINED_ARRAY_REF,
4545 TREE_TYPE (gnu_operand),
4546 variable_size (TREE_OPERAND (gnu_operand, 0)));
4548 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result)
4549 = TYPE_READONLY (TREE_TYPE (TREE_TYPE (gnu_operand)));
4550 return gnu_result;
4552 else
4553 return variable_size (gnu_operand);
4556 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
4557 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
4558 return the GCC tree to use for that expression. GNU_NAME is the
4559 qualification to use if an external name is appropriate and DEFINITION is
4560 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
4561 we need a result. Otherwise, we are just elaborating this for
4562 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
4563 purposes even if it isn't needed for code generation. */
4565 static tree
4566 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity,
4567 tree gnu_name, bool definition, bool need_value,
4568 bool need_debug)
4570 tree gnu_expr;
4572 /* If we already elaborated this expression (e.g., it was involved
4573 in the definition of a private type), use the old value. */
4574 if (present_gnu_tree (gnat_expr))
4575 return get_gnu_tree (gnat_expr);
4577 /* If we don't need a value and this is static or a discriment, we
4578 don't need to do anything. */
4579 else if (!need_value
4580 && (Is_OK_Static_Expression (gnat_expr)
4581 || (Nkind (gnat_expr) == N_Identifier
4582 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
4583 return 0;
4585 /* Otherwise, convert this tree to its GCC equivalant. */
4586 gnu_expr
4587 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
4588 gnu_name, definition, need_debug);
4590 /* Save the expression in case we try to elaborate this entity again. Since
4591 this is not a DECL, don't check it. Don't save if it's a discriminant. */
4592 if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
4593 save_gnu_tree (gnat_expr, gnu_expr, true);
4595 return need_value ? gnu_expr : error_mark_node;
4598 /* Similar, but take a GNU expression. */
4600 static tree
4601 elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
4602 tree gnu_expr, tree gnu_name, bool definition,
4603 bool need_debug)
4605 tree gnu_decl = NULL_TREE;
4606 /* Strip any conversions to see if the expression is a readonly variable.
4607 ??? This really should remain readonly, but we have to think about
4608 the typing of the tree here. */
4609 tree gnu_inner_expr = remove_conversions (gnu_expr, true);
4610 bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
4611 bool expr_variable;
4613 /* In most cases, we won't see a naked FIELD_DECL here because a
4614 discriminant reference will have been replaced with a COMPONENT_REF
4615 when the type is being elaborated. However, there are some cases
4616 involving child types where we will. So convert it to a COMPONENT_REF
4617 here. We have to hope it will be at the highest level of the
4618 expression in these cases. */
4619 if (TREE_CODE (gnu_expr) == FIELD_DECL)
4620 gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
4621 build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
4622 gnu_expr, NULL_TREE);
4624 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
4625 that is a constant, make a variable that is initialized to contain the
4626 bound when the package containing the definition is elaborated. If
4627 this entity is defined at top level and a bound or discriminant value
4628 isn't a constant or a reference to a discriminant, replace the bound
4629 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
4630 rely here on the fact that an expression cannot contain both the
4631 discriminant and some other variable. */
4633 expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
4634 && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
4635 && TREE_READONLY (gnu_inner_expr))
4636 && !CONTAINS_PLACEHOLDER_P (gnu_expr));
4638 /* If this is a static expression or contains a discriminant, we don't
4639 need the variable for debugging (and can't elaborate anyway if a
4640 discriminant). */
4641 if (need_debug
4642 && (Is_OK_Static_Expression (gnat_expr)
4643 || CONTAINS_PLACEHOLDER_P (gnu_expr)))
4644 need_debug = false;
4646 /* Now create the variable if we need it. */
4647 if (need_debug || (expr_variable && expr_global))
4648 gnu_decl
4649 = create_var_decl (create_concat_name (gnat_entity,
4650 IDENTIFIER_POINTER (gnu_name)),
4651 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true,
4652 Is_Public (gnat_entity), !definition, false, NULL,
4653 gnat_entity);
4655 /* We only need to use this variable if we are in global context since GCC
4656 can do the right thing in the local case. */
4657 if (expr_global && expr_variable)
4658 return gnu_decl;
4659 else if (!expr_variable)
4660 return gnu_expr;
4661 else
4662 return maybe_variable (gnu_expr);
4665 /* Create a record type that contains a field of TYPE with a starting bit
4666 position so that it is aligned to ALIGN bits and is SIZE bytes long. */
4668 tree
4669 make_aligning_type (tree type, int align, tree size)
4671 tree record_type = make_node (RECORD_TYPE);
4672 tree place = build0 (PLACEHOLDER_EXPR, record_type);
4673 tree size_addr_place = convert (sizetype,
4674 build_unary_op (ADDR_EXPR, NULL_TREE,
4675 place));
4676 tree name = TYPE_NAME (type);
4677 tree pos, field;
4679 if (TREE_CODE (name) == TYPE_DECL)
4680 name = DECL_NAME (name);
4682 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
4684 /* The bit position is obtained by "and"ing the alignment minus 1
4685 with the two's complement of the address and multiplying
4686 by the number of bits per unit. Do all this in sizetype. */
4687 pos = size_binop (MULT_EXPR,
4688 convert (bitsizetype,
4689 size_binop (BIT_AND_EXPR,
4690 size_diffop (size_zero_node,
4691 size_addr_place),
4692 ssize_int ((align / BITS_PER_UNIT)
4693 - 1))),
4694 bitsize_unit_node);
4696 /* Create the field, with -1 as the 'addressable' indication to avoid the
4697 creation of a bitfield. We don't need one, it would have damaging
4698 consequences on the alignment computation, and create_field_decl would
4699 make one without this special argument, for instance because of the
4700 complex position expression. */
4701 field = create_field_decl (get_identifier ("F"), type, record_type, 1, size,
4702 pos, -1);
4704 finish_record_type (record_type, field, true, false);
4705 TYPE_ALIGN (record_type) = BIGGEST_ALIGNMENT;
4706 TYPE_SIZE (record_type)
4707 = size_binop (PLUS_EXPR,
4708 size_binop (MULT_EXPR, convert (bitsizetype, size),
4709 bitsize_unit_node),
4710 bitsize_int (align));
4711 TYPE_SIZE_UNIT (record_type)
4712 = size_binop (PLUS_EXPR, size, size_int (align / BITS_PER_UNIT));
4713 copy_alias_set (record_type, type);
4714 return record_type;
4717 /* TYPE is a RECORD_TYPE, UNION_TYPE, or QUAL_UNION_TYPE, with BLKmode that's
4718 being used as the field type of a packed record. See if we can rewrite it
4719 as a record that has a non-BLKmode type, which we can pack tighter. If so,
4720 return the new type. If not, return the original type. */
4722 static tree
4723 make_packable_type (tree type)
4725 tree new_type = make_node (TREE_CODE (type));
4726 tree field_list = NULL_TREE;
4727 tree old_field;
4729 /* Copy the name and flags from the old type to that of the new and set
4730 the alignment to try for an integral type. For QUAL_UNION_TYPE,
4731 also copy the size. */
4732 TYPE_NAME (new_type) = TYPE_NAME (type);
4733 TYPE_JUSTIFIED_MODULAR_P (new_type)
4734 = TYPE_JUSTIFIED_MODULAR_P (type);
4735 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
4737 if (TREE_CODE (type) == RECORD_TYPE)
4738 TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
4739 else if (TREE_CODE (type) == QUAL_UNION_TYPE)
4741 TYPE_SIZE (new_type) = TYPE_SIZE (type);
4742 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
4745 TYPE_ALIGN (new_type)
4746 = ((HOST_WIDE_INT) 1
4747 << (floor_log2 (tree_low_cst (TYPE_SIZE (type), 1) - 1) + 1));
4749 /* Now copy the fields, keeping the position and size. */
4750 for (old_field = TYPE_FIELDS (type); old_field;
4751 old_field = TREE_CHAIN (old_field))
4753 tree new_field_type = TREE_TYPE (old_field);
4754 tree new_field;
4756 if (TYPE_MODE (new_field_type) == BLKmode
4757 && (TREE_CODE (new_field_type) == RECORD_TYPE
4758 || TREE_CODE (new_field_type) == UNION_TYPE
4759 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
4760 && host_integerp (TYPE_SIZE (new_field_type), 1))
4761 new_field_type = make_packable_type (new_field_type);
4763 new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
4764 new_type, TYPE_PACKED (type),
4765 DECL_SIZE (old_field),
4766 bit_position (old_field),
4767 !DECL_NONADDRESSABLE_P (old_field));
4769 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
4770 SET_DECL_ORIGINAL_FIELD
4771 (new_field, (DECL_ORIGINAL_FIELD (old_field)
4772 ? DECL_ORIGINAL_FIELD (old_field) : old_field));
4774 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
4775 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
4777 TREE_CHAIN (new_field) = field_list;
4778 field_list = new_field;
4781 finish_record_type (new_type, nreverse (field_list), true, true);
4782 copy_alias_set (new_type, type);
4783 return TYPE_MODE (new_type) == BLKmode ? type : new_type;
4786 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
4787 if needed. We have already verified that SIZE and TYPE are large enough.
4789 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
4790 to issue a warning.
4792 IS_USER_TYPE is true if we must be sure we complete the original type.
4794 DEFINITION is true if this type is being defined.
4796 SAME_RM_SIZE is true if the RM_Size of the resulting type is to be
4797 set to its TYPE_SIZE; otherwise, it's set to the RM_Size of the original
4798 type. */
4800 tree
4801 maybe_pad_type (tree type, tree size, unsigned int align,
4802 Entity_Id gnat_entity, const char *name_trailer,
4803 bool is_user_type, bool definition, bool same_rm_size)
4805 tree orig_size = TYPE_SIZE (type);
4806 tree record;
4807 tree field;
4809 /* If TYPE is a padded type, see if it agrees with any size and alignment
4810 we were given. If so, return the original type. Otherwise, strip
4811 off the padding, since we will either be returning the inner type
4812 or repadding it. If no size or alignment is specified, use that of
4813 the original padded type. */
4815 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4817 if ((!size
4818 || operand_equal_p (round_up (size,
4819 MAX (align, TYPE_ALIGN (type))),
4820 round_up (TYPE_SIZE (type),
4821 MAX (align, TYPE_ALIGN (type))),
4823 && (align == 0 || align == TYPE_ALIGN (type)))
4824 return type;
4826 if (!size)
4827 size = TYPE_SIZE (type);
4828 if (align == 0)
4829 align = TYPE_ALIGN (type);
4831 type = TREE_TYPE (TYPE_FIELDS (type));
4832 orig_size = TYPE_SIZE (type);
4835 /* If the size is either not being changed or is being made smaller (which
4836 is not done here (and is only valid for bitfields anyway), show the size
4837 isn't changing. Likewise, clear the alignment if it isn't being
4838 changed. Then return if we aren't doing anything. */
4840 if (size
4841 && (operand_equal_p (size, orig_size, 0)
4842 || (TREE_CODE (orig_size) == INTEGER_CST
4843 && tree_int_cst_lt (size, orig_size))))
4844 size = NULL_TREE;
4846 if (align == TYPE_ALIGN (type))
4847 align = 0;
4849 if (align == 0 && !size)
4850 return type;
4852 /* We used to modify the record in place in some cases, but that could
4853 generate incorrect debugging information. So make a new record
4854 type and name. */
4855 record = make_node (RECORD_TYPE);
4857 if (Present (gnat_entity))
4858 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
4860 /* If we were making a type, complete the original type and give it a
4861 name. */
4862 if (is_user_type)
4863 create_type_decl (get_entity_name (gnat_entity), type,
4864 NULL, !Comes_From_Source (gnat_entity),
4865 !(TYPE_NAME (type)
4866 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4867 && DECL_IGNORED_P (TYPE_NAME (type))),
4868 gnat_entity);
4870 /* If we are changing the alignment and the input type is a record with
4871 BLKmode and a small constant size, try to make a form that has an
4872 integral mode. That might allow this record to have an integral mode,
4873 which will be much more efficient. There is no point in doing this if a
4874 size is specified unless it is also smaller than the biggest alignment
4875 and it is incorrect to do this if the size of the original type is not a
4876 multiple of the alignment. */
4877 if (align != 0
4878 && TREE_CODE (type) == RECORD_TYPE
4879 && TYPE_MODE (type) == BLKmode
4880 && host_integerp (orig_size, 1)
4881 && compare_tree_int (orig_size, BIGGEST_ALIGNMENT) <= 0
4882 && (!size
4883 || (TREE_CODE (size) == INTEGER_CST
4884 && compare_tree_int (size, BIGGEST_ALIGNMENT) <= 0))
4885 && tree_low_cst (orig_size, 1) % align == 0)
4886 type = make_packable_type (type);
4888 field = create_field_decl (get_identifier ("F"), type, record, 0,
4889 NULL_TREE, bitsize_zero_node, 1);
4891 DECL_INTERNAL_P (field) = 1;
4892 TYPE_SIZE (record) = size ? size : orig_size;
4893 TYPE_SIZE_UNIT (record)
4894 = (size ? convert (sizetype,
4895 size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node))
4896 : TYPE_SIZE_UNIT (type));
4898 TYPE_ALIGN (record) = align;
4899 TYPE_IS_PADDING_P (record) = 1;
4900 TYPE_VOLATILE (record)
4901 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
4902 finish_record_type (record, field, true, false);
4904 /* Keep the RM_Size of the padded record as that of the old record
4905 if requested. */
4906 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : rm_size (type));
4908 /* Unless debugging information isn't being written for the input type,
4909 write a record that shows what we are a subtype of and also make a
4910 variable that indicates our size, if variable. */
4911 if (TYPE_NAME (record) && AGGREGATE_TYPE_P (type)
4912 && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
4913 || !DECL_IGNORED_P (TYPE_NAME (type))))
4915 tree marker = make_node (RECORD_TYPE);
4916 tree name = (TREE_CODE (TYPE_NAME (record)) == TYPE_DECL
4917 ? DECL_NAME (TYPE_NAME (record))
4918 : TYPE_NAME (record));
4919 tree orig_name = TYPE_NAME (type);
4921 if (TREE_CODE (orig_name) == TYPE_DECL)
4922 orig_name = DECL_NAME (orig_name);
4924 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
4925 finish_record_type (marker,
4926 create_field_decl (orig_name, integer_type_node,
4927 marker, 0, NULL_TREE, NULL_TREE,
4929 false, false);
4931 if (size && TREE_CODE (size) != INTEGER_CST && definition)
4932 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
4933 sizetype, TYPE_SIZE (record), false, false, false,
4934 false, NULL, gnat_entity);
4937 type = record;
4939 if (CONTAINS_PLACEHOLDER_P (orig_size))
4940 orig_size = max_size (orig_size, true);
4942 /* If the size was widened explicitly, maybe give a warning. */
4943 if (size && Present (gnat_entity)
4944 && !operand_equal_p (size, orig_size, 0)
4945 && !(TREE_CODE (size) == INTEGER_CST
4946 && TREE_CODE (orig_size) == INTEGER_CST
4947 && tree_int_cst_lt (size, orig_size)))
4949 Node_Id gnat_error_node = Empty;
4951 if (Is_Packed_Array_Type (gnat_entity))
4952 gnat_entity = Associated_Node_For_Itype (gnat_entity);
4954 if ((Ekind (gnat_entity) == E_Component
4955 || Ekind (gnat_entity) == E_Discriminant)
4956 && Present (Component_Clause (gnat_entity)))
4957 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
4958 else if (Present (Size_Clause (gnat_entity)))
4959 gnat_error_node = Expression (Size_Clause (gnat_entity));
4961 /* Generate message only for entities that come from source, since
4962 if we have an entity created by expansion, the message will be
4963 generated for some other corresponding source entity. */
4964 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
4965 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
4966 gnat_entity,
4967 size_diffop (size, orig_size));
4969 else if (*name_trailer == 'C' && !Is_Internal (gnat_entity))
4970 post_error_ne_tree ("component of& padded{ by ^ bits}?",
4971 gnat_entity, gnat_entity,
4972 size_diffop (size, orig_size));
4975 return type;
4978 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
4979 the value passed against the list of choices. */
4981 tree
4982 choices_to_gnu (tree operand, Node_Id choices)
4984 Node_Id choice;
4985 Node_Id gnat_temp;
4986 tree result = integer_zero_node;
4987 tree this_test, low = 0, high = 0, single = 0;
4989 for (choice = First (choices); Present (choice); choice = Next (choice))
4991 switch (Nkind (choice))
4993 case N_Range:
4994 low = gnat_to_gnu (Low_Bound (choice));
4995 high = gnat_to_gnu (High_Bound (choice));
4997 /* There's no good type to use here, so we might as well use
4998 integer_type_node. */
4999 this_test
5000 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5001 build_binary_op (GE_EXPR, integer_type_node,
5002 operand, low),
5003 build_binary_op (LE_EXPR, integer_type_node,
5004 operand, high));
5006 break;
5008 case N_Subtype_Indication:
5009 gnat_temp = Range_Expression (Constraint (choice));
5010 low = gnat_to_gnu (Low_Bound (gnat_temp));
5011 high = gnat_to_gnu (High_Bound (gnat_temp));
5013 this_test
5014 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5015 build_binary_op (GE_EXPR, integer_type_node,
5016 operand, low),
5017 build_binary_op (LE_EXPR, integer_type_node,
5018 operand, high));
5019 break;
5021 case N_Identifier:
5022 case N_Expanded_Name:
5023 /* This represents either a subtype range, an enumeration
5024 literal, or a constant Ekind says which. If an enumeration
5025 literal or constant, fall through to the next case. */
5026 if (Ekind (Entity (choice)) != E_Enumeration_Literal
5027 && Ekind (Entity (choice)) != E_Constant)
5029 tree type = gnat_to_gnu_type (Entity (choice));
5031 low = TYPE_MIN_VALUE (type);
5032 high = TYPE_MAX_VALUE (type);
5034 this_test
5035 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
5036 build_binary_op (GE_EXPR, integer_type_node,
5037 operand, low),
5038 build_binary_op (LE_EXPR, integer_type_node,
5039 operand, high));
5040 break;
5042 /* ... fall through ... */
5043 case N_Character_Literal:
5044 case N_Integer_Literal:
5045 single = gnat_to_gnu (choice);
5046 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
5047 single);
5048 break;
5050 case N_Others_Choice:
5051 this_test = integer_one_node;
5052 break;
5054 default:
5055 gcc_unreachable ();
5058 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
5059 result, this_test);
5062 return result;
5065 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
5066 placed in GNU_RECORD_TYPE.
5068 PACKED is 1 if the enclosing record is packed and -1 if the enclosing
5069 record has a Component_Alignment of Storage_Unit.
5071 DEFINITION is true if this field is for a record being defined. */
5073 static tree
5074 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
5075 bool definition)
5077 tree gnu_field_id = get_entity_name (gnat_field);
5078 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
5079 tree gnu_orig_field_type = gnu_field_type;
5080 tree gnu_pos = 0;
5081 tree gnu_size = 0;
5082 tree gnu_field;
5083 bool needs_strict_alignment
5084 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
5085 || Treat_As_Volatile (gnat_field));
5087 /* If this field requires strict alignment or contains an item of
5088 variable sized, pretend it isn't packed. */
5089 if (needs_strict_alignment || is_variable_size (gnu_field_type))
5090 packed = 0;
5092 /* For packed records, this is one of the few occasions on which we use
5093 the official RM size for discrete or fixed-point components, instead
5094 of the normal GNAT size stored in Esize. See description in Einfo:
5095 "Handling of Type'Size Values" for further details. */
5097 if (packed == 1)
5098 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
5099 gnat_field, FIELD_DECL, false, true);
5101 if (Known_Static_Esize (gnat_field))
5102 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5103 gnat_field, FIELD_DECL, false, true);
5105 /* If the field's type is justified modular and the size of the packed
5106 array it wraps is the same as that of the field, we can make the field
5107 the type of the inner object. Note that we may need to do so if the
5108 record is packed or the field has a component clause, but these cases
5109 are handled later. */
5110 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5111 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
5112 && tree_int_cst_equal (TYPE_SIZE (gnu_field_type),
5113 TYPE_ADA_SIZE (gnu_field_type)))
5114 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
5116 /* If we are packing this record, have a specified size that's smaller than
5117 that of the field type, or a position is specified, and the field type
5118 is also a record that's BLKmode and with a small constant size, see if
5119 we can get a better form of the type that allows more packing. If we
5120 can, show a size was specified for it if there wasn't one so we know to
5121 make this a bitfield and avoid making things wider. */
5122 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5123 && TYPE_MODE (gnu_field_type) == BLKmode
5124 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
5125 && compare_tree_int (TYPE_SIZE (gnu_field_type), BIGGEST_ALIGNMENT) <= 0
5126 && (packed == 1
5127 || (gnu_size && tree_int_cst_lt (gnu_size,
5128 TYPE_SIZE (gnu_field_type)))
5129 || Present (Component_Clause (gnat_field))))
5131 gnu_field_type = make_packable_type (gnu_field_type);
5133 if (gnu_field_type != gnu_orig_field_type && !gnu_size)
5134 gnu_size = rm_size (gnu_field_type);
5137 /* If we are packing the record and the field is BLKmode, round the
5138 size up to a byte boundary. */
5139 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
5140 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
5142 if (Present (Component_Clause (gnat_field)))
5144 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
5145 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
5146 gnat_field, FIELD_DECL, false, true);
5148 /* Ensure the position does not overlap with the parent subtype,
5149 if there is one. */
5150 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
5152 tree gnu_parent
5153 = gnat_to_gnu_type (Parent_Subtype
5154 (Underlying_Type (Scope (gnat_field))));
5156 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
5157 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
5159 post_error_ne_tree
5160 ("offset of& must be beyond parent{, minimum allowed is ^}",
5161 First_Bit (Component_Clause (gnat_field)), gnat_field,
5162 TYPE_SIZE_UNIT (gnu_parent));
5166 /* If this field needs strict alignment, ensure the record is
5167 sufficiently aligned and that that position and size are
5168 consistent with the alignment. */
5169 if (needs_strict_alignment)
5171 tree gnu_min_size = round_up (rm_size (gnu_field_type),
5172 TYPE_ALIGN (gnu_field_type));
5174 TYPE_ALIGN (gnu_record_type)
5175 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
5177 /* If Atomic, the size must match exactly and if aliased, the size
5178 must not be less than the rounded size. */
5179 if ((Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
5180 && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
5182 post_error_ne_tree
5183 ("atomic field& must be natural size of type{ (^)}",
5184 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5185 TYPE_SIZE (gnu_field_type));
5187 gnu_size = NULL_TREE;
5190 else if (Is_Aliased (gnat_field)
5191 && gnu_size && tree_int_cst_lt (gnu_size, gnu_min_size))
5193 post_error_ne_tree
5194 ("size of aliased field& too small{, minimum required is ^}",
5195 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5196 gnu_min_size);
5197 gnu_size = NULL_TREE;
5200 if (!integer_zerop (size_binop
5201 (TRUNC_MOD_EXPR, gnu_pos,
5202 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
5204 if (Is_Aliased (gnat_field))
5205 post_error_ne_num
5206 ("position of aliased field& must be multiple of ^ bits",
5207 First_Bit (Component_Clause (gnat_field)), gnat_field,
5208 TYPE_ALIGN (gnu_field_type));
5210 else if (Treat_As_Volatile (gnat_field))
5211 post_error_ne_num
5212 ("position of volatile field& must be multiple of ^ bits",
5213 First_Bit (Component_Clause (gnat_field)), gnat_field,
5214 TYPE_ALIGN (gnu_field_type));
5216 else if (Strict_Alignment (Etype (gnat_field)))
5217 post_error_ne_num
5218 ("position of & with aliased or tagged components not multiple of ^ bits",
5219 First_Bit (Component_Clause (gnat_field)), gnat_field,
5220 TYPE_ALIGN (gnu_field_type));
5221 else
5222 gcc_unreachable ();
5224 gnu_pos = NULL_TREE;
5228 if (Is_Atomic (gnat_field))
5229 check_ok_for_atomic (gnu_field_type, gnat_field, false);
5232 /* If the record has rep clauses and this is the tag field, make a rep
5233 clause for it as well. */
5234 else if (Has_Specified_Layout (Scope (gnat_field))
5235 && Chars (gnat_field) == Name_uTag)
5237 gnu_pos = bitsize_zero_node;
5238 gnu_size = TYPE_SIZE (gnu_field_type);
5241 /* We need to make the size the maximum for the type if it is
5242 self-referential and an unconstrained type. In that case, we can't
5243 pack the field since we can't make a copy to align it. */
5244 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5245 && !gnu_size
5246 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
5247 && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
5249 gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
5250 packed = 0;
5253 /* If no size is specified (or if there was an error), don't specify a
5254 position. */
5255 if (!gnu_size)
5256 gnu_pos = NULL_TREE;
5257 else
5259 /* If the field's type is justified modular, we would need to remove
5260 the wrapper to (better) meet the layout requirements. However we
5261 can do so only if the field is not aliased to preserve the unique
5262 layout and if the prescribed size is not greater than that of the
5263 packed array to preserve the justification. */
5264 if (!needs_strict_alignment
5265 && TREE_CODE (gnu_field_type) == RECORD_TYPE
5266 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
5267 && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
5268 <= 0)
5269 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
5271 gnu_field_type
5272 = make_type_from_size (gnu_field_type, gnu_size,
5273 Has_Biased_Representation (gnat_field));
5274 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
5275 "PAD", false, definition, true);
5278 gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
5279 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
5281 /* Now create the decl for the field. */
5282 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
5283 packed, gnu_size, gnu_pos,
5284 Is_Aliased (gnat_field));
5285 Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
5286 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
5288 if (Ekind (gnat_field) == E_Discriminant)
5289 DECL_DISCRIMINANT_NUMBER (gnu_field)
5290 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
5292 return gnu_field;
5295 /* Return true if TYPE is a type with variable size, a padding type with a
5296 field of variable size or is a record that has a field such a field. */
5298 static bool
5299 is_variable_size (tree type)
5301 tree field;
5303 /* We need not be concerned about this at all if we don't have
5304 strict alignment. */
5305 if (!STRICT_ALIGNMENT)
5306 return false;
5307 else if (!TREE_CONSTANT (TYPE_SIZE (type)))
5308 return true;
5309 else if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type)
5310 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
5311 return true;
5312 else if (TREE_CODE (type) != RECORD_TYPE
5313 && TREE_CODE (type) != UNION_TYPE
5314 && TREE_CODE (type) != QUAL_UNION_TYPE)
5315 return false;
5317 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5318 if (is_variable_size (TREE_TYPE (field)))
5319 return true;
5321 return false;
5324 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
5325 of GCC trees for fields that are in the record and have already been
5326 processed. When called from gnat_to_gnu_entity during the processing of a
5327 record type definition, the GCC nodes for the discriminants will be on
5328 the chain. The other calls to this function are recursive calls from
5329 itself for the Component_List of a variant and the chain is empty.
5331 PACKED is 1 if this is for a record with "pragma pack" and -1 is this is
5332 for a record type with "pragma component_alignment (storage_unit)".
5334 DEFINITION is true if we are defining this record.
5336 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
5337 with a rep clause is to be added. If it is nonzero, that is all that
5338 should be done with such fields.
5340 CANCEL_ALIGNMENT, if true, means the alignment should be zeroed before
5341 laying out the record. This means the alignment only serves to force fields
5342 to be bitfields, but not require the record to be that aligned. This is
5343 used for variants.
5345 ALL_REP, if true, means a rep clause was found for all the fields. This
5346 simplifies the logic since we know we're not in the mixed case.
5348 The processing of the component list fills in the chain with all of the
5349 fields of the record and then the record type is finished. */
5351 static void
5352 components_to_record (tree gnu_record_type, Node_Id component_list,
5353 tree gnu_field_list, int packed, bool definition,
5354 tree *p_gnu_rep_list, bool cancel_alignment,
5355 bool all_rep)
5357 Node_Id component_decl;
5358 Entity_Id gnat_field;
5359 Node_Id variant_part;
5360 Node_Id variant;
5361 tree gnu_our_rep_list = NULL_TREE;
5362 tree gnu_field, gnu_last;
5363 bool layout_with_rep = false;
5364 bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
5366 /* For each variable within each component declaration create a GCC field
5367 and add it to the list, skipping any pragmas in the list. */
5369 if (Present (Component_Items (component_list)))
5370 for (component_decl = First_Non_Pragma (Component_Items (component_list));
5371 Present (component_decl);
5372 component_decl = Next_Non_Pragma (component_decl))
5374 gnat_field = Defining_Entity (component_decl);
5376 if (Chars (gnat_field) == Name_uParent)
5377 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
5378 else
5380 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
5381 packed, definition);
5383 /* If this is the _Tag field, put it before any discriminants,
5384 instead of after them as is the case for all other fields.
5385 Ignore field of void type if only annotating. */
5386 if (Chars (gnat_field) == Name_uTag)
5387 gnu_field_list = chainon (gnu_field_list, gnu_field);
5388 else
5390 TREE_CHAIN (gnu_field) = gnu_field_list;
5391 gnu_field_list = gnu_field;
5395 save_gnu_tree (gnat_field, gnu_field, false);
5398 /* At the end of the component list there may be a variant part. */
5399 variant_part = Variant_Part (component_list);
5401 /* If this is an unchecked union, each variant must have exactly one
5402 component, each of which becomes one component of this union. */
5403 if (TREE_CODE (gnu_record_type) == UNION_TYPE
5404 && TYPE_UNCHECKED_UNION_P (gnu_record_type)
5405 && Present (variant_part))
5406 for (variant = First_Non_Pragma (Variants (variant_part));
5407 Present (variant);
5408 variant = Next_Non_Pragma (variant))
5410 component_decl
5411 = First_Non_Pragma (Component_Items (Component_List (variant)));
5412 gnat_field = Defining_Entity (component_decl);
5413 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
5414 definition);
5415 TREE_CHAIN (gnu_field) = gnu_field_list;
5416 gnu_field_list = gnu_field;
5417 save_gnu_tree (gnat_field, gnu_field, false);
5420 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
5421 mutually exclusive and should go in the same memory. To do this we need
5422 to treat each variant as a record whose elements are created from the
5423 component list for the variant. So here we create the records from the
5424 lists for the variants and put them all into the QUAL_UNION_TYPE. */
5425 else if (Present (variant_part))
5427 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
5428 Node_Id variant;
5429 tree gnu_union_type = make_node (QUAL_UNION_TYPE);
5430 tree gnu_union_field;
5431 tree gnu_variant_list = NULL_TREE;
5432 tree gnu_name = TYPE_NAME (gnu_record_type);
5433 tree gnu_var_name
5434 = concat_id_with_name
5435 (get_identifier (Get_Name_String (Chars (Name (variant_part)))),
5436 "XVN");
5438 if (TREE_CODE (gnu_name) == TYPE_DECL)
5439 gnu_name = DECL_NAME (gnu_name);
5441 TYPE_NAME (gnu_union_type)
5442 = concat_id_with_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
5443 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
5445 for (variant = First_Non_Pragma (Variants (variant_part));
5446 Present (variant);
5447 variant = Next_Non_Pragma (variant))
5449 tree gnu_variant_type = make_node (RECORD_TYPE);
5450 tree gnu_inner_name;
5451 tree gnu_qual;
5453 Get_Variant_Encoding (variant);
5454 gnu_inner_name = get_identifier (Name_Buffer);
5455 TYPE_NAME (gnu_variant_type)
5456 = concat_id_with_name (TYPE_NAME (gnu_union_type),
5457 IDENTIFIER_POINTER (gnu_inner_name));
5459 /* Set the alignment of the inner type in case we need to make
5460 inner objects into bitfields, but then clear it out
5461 so the record actually gets only the alignment required. */
5462 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
5463 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
5465 /* Similarly, if the outer record has a size specified and all fields
5466 have record rep clauses, we can propagate the size into the
5467 variant part. */
5468 if (all_rep_and_size)
5470 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
5471 TYPE_SIZE_UNIT (gnu_variant_type)
5472 = TYPE_SIZE_UNIT (gnu_record_type);
5475 components_to_record (gnu_variant_type, Component_List (variant),
5476 NULL_TREE, packed, definition,
5477 &gnu_our_rep_list, !all_rep_and_size, all_rep);
5479 gnu_qual = choices_to_gnu (gnu_discriminant,
5480 Discrete_Choices (variant));
5482 Set_Present_Expr (variant, annotate_value (gnu_qual));
5483 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
5484 gnu_union_type, 0,
5485 (all_rep_and_size
5486 ? TYPE_SIZE (gnu_record_type) : 0),
5487 (all_rep_and_size
5488 ? bitsize_zero_node : 0),
5491 DECL_INTERNAL_P (gnu_field) = 1;
5492 DECL_QUALIFIER (gnu_field) = gnu_qual;
5493 TREE_CHAIN (gnu_field) = gnu_variant_list;
5494 gnu_variant_list = gnu_field;
5497 /* We use to delete the empty variants from the end. However,
5498 we no longer do that because we need them to generate complete
5499 debugging information for the variant record. Otherwise,
5500 the union type definition will be missing the fields associated
5501 to these empty variants. */
5503 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
5504 if (gnu_variant_list)
5506 if (all_rep_and_size)
5508 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
5509 TYPE_SIZE_UNIT (gnu_union_type)
5510 = TYPE_SIZE_UNIT (gnu_record_type);
5513 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
5514 all_rep_and_size, false);
5516 gnu_union_field
5517 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
5518 packed,
5519 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
5520 all_rep ? bitsize_zero_node : 0, 0);
5522 DECL_INTERNAL_P (gnu_union_field) = 1;
5523 TREE_CHAIN (gnu_union_field) = gnu_field_list;
5524 gnu_field_list = gnu_union_field;
5528 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
5529 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
5530 in a separate pass since we want to handle the discriminants but can't
5531 play with them until we've used them in debugging data above.
5533 ??? Note: if we then reorder them, debugging information will be wrong,
5534 but there's nothing that can be done about this at the moment. */
5536 for (gnu_field = gnu_field_list, gnu_last = NULL_TREE; gnu_field; )
5538 if (DECL_FIELD_OFFSET (gnu_field))
5540 tree gnu_next = TREE_CHAIN (gnu_field);
5542 if (!gnu_last)
5543 gnu_field_list = gnu_next;
5544 else
5545 TREE_CHAIN (gnu_last) = gnu_next;
5547 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
5548 gnu_our_rep_list = gnu_field;
5549 gnu_field = gnu_next;
5551 else
5553 gnu_last = gnu_field;
5554 gnu_field = TREE_CHAIN (gnu_field);
5558 /* If we have any items in our rep'ed field list, it is not the case that all
5559 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
5560 set it and ignore the items. Otherwise, sort the fields by bit position
5561 and put them into their own record if we have any fields without
5562 rep clauses. */
5563 if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
5564 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
5565 else if (gnu_our_rep_list)
5567 tree gnu_rep_type
5568 = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
5569 int len = list_length (gnu_our_rep_list);
5570 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
5571 int i;
5573 /* Set DECL_SECTION_NAME to increasing integers so we have a
5574 stable sort. */
5575 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
5576 gnu_field = TREE_CHAIN (gnu_field), i++)
5578 gnu_arr[i] = gnu_field;
5579 DECL_SECTION_NAME (gnu_field) = size_int (i);
5582 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
5584 /* Put the fields in the list in order of increasing position, which
5585 means we start from the end. */
5586 gnu_our_rep_list = NULL_TREE;
5587 for (i = len - 1; i >= 0; i--)
5589 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
5590 gnu_our_rep_list = gnu_arr[i];
5591 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
5592 DECL_SECTION_NAME (gnu_arr[i]) = NULL_TREE;
5595 if (gnu_field_list)
5597 finish_record_type (gnu_rep_type, gnu_our_rep_list, true, false);
5598 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
5599 gnu_record_type, 0, 0, 0, 1);
5600 DECL_INTERNAL_P (gnu_field) = 1;
5601 gnu_field_list = chainon (gnu_field_list, gnu_field);
5603 else
5605 layout_with_rep = true;
5606 gnu_field_list = nreverse (gnu_our_rep_list);
5610 if (cancel_alignment)
5611 TYPE_ALIGN (gnu_record_type) = 0;
5613 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
5614 layout_with_rep, false);
5617 /* Called via qsort from the above. Returns -1, 1, depending on the
5618 bit positions and ordinals of the two fields. */
5620 static int
5621 compare_field_bitpos (const PTR rt1, const PTR rt2)
5623 tree *t1 = (tree *) rt1;
5624 tree *t2 = (tree *) rt2;
5626 if (tree_int_cst_equal (bit_position (*t1), bit_position (*t2)))
5627 return
5628 (tree_int_cst_lt (DECL_SECTION_NAME (*t1), DECL_SECTION_NAME (*t2))
5629 ? -1 : 1);
5630 else if (tree_int_cst_lt (bit_position (*t1), bit_position (*t2)))
5631 return -1;
5632 else
5633 return 1;
5636 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
5637 placed into an Esize, Component_Bit_Offset, or Component_Size value
5638 in the GNAT tree. */
5640 static Uint
5641 annotate_value (tree gnu_size)
5643 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
5644 TCode tcode;
5645 Node_Ref_Or_Val ops[3], ret;
5646 int i;
5647 int size;
5649 /* If back annotation is suppressed by the front end, return No_Uint */
5650 if (!Back_Annotate_Rep_Info)
5651 return No_Uint;
5653 /* See if we've already saved the value for this node. */
5654 if (EXPR_P (gnu_size) && TREE_COMPLEXITY (gnu_size))
5655 return (Node_Ref_Or_Val) TREE_COMPLEXITY (gnu_size);
5657 /* If we do not return inside this switch, TCODE will be set to the
5658 code to use for a Create_Node operand and LEN (set above) will be
5659 the number of recursive calls for us to make. */
5661 switch (TREE_CODE (gnu_size))
5663 case INTEGER_CST:
5664 if (TREE_OVERFLOW (gnu_size))
5665 return No_Uint;
5667 /* This may have come from a conversion from some smaller type,
5668 so ensure this is in bitsizetype. */
5669 gnu_size = convert (bitsizetype, gnu_size);
5671 /* For negative values, use NEGATE_EXPR of the supplied value. */
5672 if (tree_int_cst_sgn (gnu_size) < 0)
5674 /* The rediculous code below is to handle the case of the largest
5675 negative integer. */
5676 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
5677 bool adjust = false;
5678 tree temp;
5680 if (TREE_CONSTANT_OVERFLOW (negative_size))
5682 negative_size
5683 = size_binop (MINUS_EXPR, bitsize_zero_node,
5684 size_binop (PLUS_EXPR, gnu_size,
5685 bitsize_one_node));
5686 adjust = true;
5689 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
5690 if (adjust)
5691 temp = build2 (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
5693 return annotate_value (temp);
5696 if (!host_integerp (gnu_size, 1))
5697 return No_Uint;
5699 size = tree_low_cst (gnu_size, 1);
5701 /* This peculiar test is to make sure that the size fits in an int
5702 on machines where HOST_WIDE_INT is not "int". */
5703 if (tree_low_cst (gnu_size, 1) == size)
5704 return UI_From_Int (size);
5705 else
5706 return No_Uint;
5708 case COMPONENT_REF:
5709 /* The only case we handle here is a simple discriminant reference. */
5710 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
5711 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
5712 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
5713 return Create_Node (Discrim_Val,
5714 annotate_value (DECL_DISCRIMINANT_NUMBER
5715 (TREE_OPERAND (gnu_size, 1))),
5716 No_Uint, No_Uint);
5717 else
5718 return No_Uint;
5720 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
5721 return annotate_value (TREE_OPERAND (gnu_size, 0));
5723 /* Now just list the operations we handle. */
5724 case COND_EXPR: tcode = Cond_Expr; break;
5725 case PLUS_EXPR: tcode = Plus_Expr; break;
5726 case MINUS_EXPR: tcode = Minus_Expr; break;
5727 case MULT_EXPR: tcode = Mult_Expr; break;
5728 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
5729 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
5730 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
5731 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
5732 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
5733 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
5734 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
5735 case NEGATE_EXPR: tcode = Negate_Expr; break;
5736 case MIN_EXPR: tcode = Min_Expr; break;
5737 case MAX_EXPR: tcode = Max_Expr; break;
5738 case ABS_EXPR: tcode = Abs_Expr; break;
5739 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
5740 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
5741 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
5742 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
5743 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
5744 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
5745 case LT_EXPR: tcode = Lt_Expr; break;
5746 case LE_EXPR: tcode = Le_Expr; break;
5747 case GT_EXPR: tcode = Gt_Expr; break;
5748 case GE_EXPR: tcode = Ge_Expr; break;
5749 case EQ_EXPR: tcode = Eq_Expr; break;
5750 case NE_EXPR: tcode = Ne_Expr; break;
5752 default:
5753 return No_Uint;
5756 /* Now get each of the operands that's relevant for this code. If any
5757 cannot be expressed as a repinfo node, say we can't. */
5758 for (i = 0; i < 3; i++)
5759 ops[i] = No_Uint;
5761 for (i = 0; i < len; i++)
5763 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
5764 if (ops[i] == No_Uint)
5765 return No_Uint;
5768 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
5769 TREE_COMPLEXITY (gnu_size) = ret;
5770 return ret;
5773 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
5774 GCC type, set Component_Bit_Offset and Esize to the position and size
5775 used by Gigi. */
5777 static void
5778 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
5780 tree gnu_list;
5781 tree gnu_entry;
5782 Entity_Id gnat_field;
5784 /* We operate by first making a list of all field and their positions
5785 (we can get the sizes easily at any time) by a recursive call
5786 and then update all the sizes into the tree. */
5787 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
5788 size_zero_node, bitsize_zero_node,
5789 BIGGEST_ALIGNMENT);
5791 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
5792 gnat_field = Next_Entity (gnat_field))
5793 if ((Ekind (gnat_field) == E_Component
5794 || (Ekind (gnat_field) == E_Discriminant
5795 && !Is_Unchecked_Union (Scope (gnat_field)))))
5797 tree parent_offset = bitsize_zero_node;
5799 gnu_entry
5800 = purpose_member (gnat_to_gnu_entity (gnat_field, NULL_TREE, 0),
5801 gnu_list);
5803 if (gnu_entry)
5805 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
5807 /* In this mode the tag and parent components have not been
5808 generated, so we add the appropriate offset to each
5809 component. For a component appearing in the current
5810 extension, the offset is the size of the parent. */
5811 if (Is_Derived_Type (gnat_entity)
5812 && Original_Record_Component (gnat_field) == gnat_field)
5813 parent_offset
5814 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
5815 bitsizetype);
5816 else
5817 parent_offset = bitsize_int (POINTER_SIZE);
5820 Set_Component_Bit_Offset
5821 (gnat_field,
5822 annotate_value
5823 (size_binop (PLUS_EXPR,
5824 bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
5825 TREE_VALUE (TREE_VALUE
5826 (TREE_VALUE (gnu_entry)))),
5827 parent_offset)));
5829 Set_Esize (gnat_field,
5830 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
5832 else if (type_annotate_only
5833 && Is_Tagged_Type (gnat_entity)
5834 && Is_Derived_Type (gnat_entity))
5836 /* If there is no gnu_entry, this is an inherited component whose
5837 position is the same as in the parent type. */
5838 Set_Component_Bit_Offset
5839 (gnat_field,
5840 Component_Bit_Offset (Original_Record_Component (gnat_field)));
5841 Set_Esize (gnat_field,
5842 Esize (Original_Record_Component (gnat_field)));
5847 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
5848 FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
5849 position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
5850 placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position. GNU_POS is
5851 to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
5852 the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
5853 so far. */
5855 static tree
5856 compute_field_positions (tree gnu_type, tree gnu_list, tree gnu_pos,
5857 tree gnu_bitpos, unsigned int offset_align)
5859 tree gnu_field;
5860 tree gnu_result = gnu_list;
5862 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
5863 gnu_field = TREE_CHAIN (gnu_field))
5865 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
5866 DECL_FIELD_BIT_OFFSET (gnu_field));
5867 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
5868 DECL_FIELD_OFFSET (gnu_field));
5869 unsigned int our_offset_align
5870 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
5872 gnu_result
5873 = tree_cons (gnu_field,
5874 tree_cons (gnu_our_offset,
5875 tree_cons (size_int (our_offset_align),
5876 gnu_our_bitpos, NULL_TREE),
5877 NULL_TREE),
5878 gnu_result);
5880 if (DECL_INTERNAL_P (gnu_field))
5881 gnu_result
5882 = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
5883 gnu_our_offset, gnu_our_bitpos,
5884 our_offset_align);
5887 return gnu_result;
5890 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
5891 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
5892 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
5893 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
5894 for the size of a field. COMPONENT_P is true if we are being called
5895 to process the Component_Size of GNAT_OBJECT. This is used for error
5896 message handling and to indicate to use the object size of GNU_TYPE.
5897 ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
5898 it means that a size of zero should be treated as an unspecified size. */
5900 static tree
5901 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
5902 enum tree_code kind, bool component_p, bool zero_ok)
5904 Node_Id gnat_error_node;
5905 tree type_size
5906 = kind == VAR_DECL ? TYPE_SIZE (gnu_type) : rm_size (gnu_type);
5907 tree size;
5909 /* Find the node to use for errors. */
5910 if ((Ekind (gnat_object) == E_Component
5911 || Ekind (gnat_object) == E_Discriminant)
5912 && Present (Component_Clause (gnat_object)))
5913 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
5914 else if (Present (Size_Clause (gnat_object)))
5915 gnat_error_node = Expression (Size_Clause (gnat_object));
5916 else
5917 gnat_error_node = gnat_object;
5919 /* Return 0 if no size was specified, either because Esize was not Present or
5920 the specified size was zero. */
5921 if (No (uint_size) || uint_size == No_Uint)
5922 return NULL_TREE;
5924 /* Get the size as a tree. Give an error if a size was specified, but cannot
5925 be represented as in sizetype. */
5926 size = UI_To_gnu (uint_size, bitsizetype);
5927 if (TREE_OVERFLOW (size))
5929 post_error_ne (component_p ? "component size of & is too large"
5930 : "size of & is too large",
5931 gnat_error_node, gnat_object);
5932 return NULL_TREE;
5935 /* Ignore a negative size since that corresponds to our back-annotation.
5936 Also ignore a zero size unless a size clause exists. */
5937 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
5938 return NULL_TREE;
5940 /* The size of objects is always a multiple of a byte. */
5941 if (kind == VAR_DECL
5942 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
5944 if (component_p)
5945 post_error_ne ("component size for& is not a multiple of Storage_Unit",
5946 gnat_error_node, gnat_object);
5947 else
5948 post_error_ne ("size for& is not a multiple of Storage_Unit",
5949 gnat_error_node, gnat_object);
5950 return NULL_TREE;
5953 /* If this is an integral type or a packed array type, the front-end has
5954 verified the size, so we need not do it here (which would entail
5955 checking against the bounds). However, if this is an aliased object, it
5956 may not be smaller than the type of the object. */
5957 if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
5958 && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
5959 return size;
5961 /* If the object is a record that contains a template, add the size of
5962 the template to the specified size. */
5963 if (TREE_CODE (gnu_type) == RECORD_TYPE
5964 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
5965 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
5967 /* Modify the size of the type to be that of the maximum size if it has a
5968 discriminant or the size of a thin pointer if this is a fat pointer. */
5969 if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
5970 type_size = max_size (type_size, true);
5971 else if (TYPE_FAT_POINTER_P (gnu_type))
5972 type_size = bitsize_int (POINTER_SIZE);
5974 /* If this is an access type, the minimum size is that given by the smallest
5975 integral mode that's valid for pointers. */
5976 if (TREE_CODE (gnu_type) == POINTER_TYPE)
5978 enum machine_mode p_mode;
5980 for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
5981 !targetm.valid_pointer_mode (p_mode);
5982 p_mode = GET_MODE_WIDER_MODE (p_mode))
5985 type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
5988 /* If the size of the object is a constant, the new size must not be
5989 smaller. */
5990 if (TREE_CODE (type_size) != INTEGER_CST
5991 || TREE_OVERFLOW (type_size)
5992 || tree_int_cst_lt (size, type_size))
5994 if (component_p)
5995 post_error_ne_tree
5996 ("component size for& too small{, minimum allowed is ^}",
5997 gnat_error_node, gnat_object, type_size);
5998 else
5999 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
6000 gnat_error_node, gnat_object, type_size);
6002 if (kind == VAR_DECL && !component_p
6003 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
6004 && !tree_int_cst_lt (size, rm_size (gnu_type)))
6005 post_error_ne_tree_2
6006 ("\\size of ^ is not a multiple of alignment (^ bits)",
6007 gnat_error_node, gnat_object, rm_size (gnu_type),
6008 TYPE_ALIGN (gnu_type));
6010 else if (INTEGRAL_TYPE_P (gnu_type))
6011 post_error_ne ("\\size would be legal if & were not aliased!",
6012 gnat_error_node, gnat_object);
6014 return NULL_TREE;
6017 return size;
6020 /* Similarly, but both validate and process a value of RM_Size. This
6021 routine is only called for types. */
6023 static void
6024 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
6026 /* Only give an error if a Value_Size clause was explicitly given.
6027 Otherwise, we'd be duplicating an error on the Size clause. */
6028 Node_Id gnat_attr_node
6029 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
6030 tree old_size = rm_size (gnu_type);
6031 tree size;
6033 /* Get the size as a tree. Do nothing if none was specified, either
6034 because RM_Size was not Present or if the specified size was zero.
6035 Give an error if a size was specified, but cannot be represented as
6036 in sizetype. */
6037 if (No (uint_size) || uint_size == No_Uint)
6038 return;
6040 size = UI_To_gnu (uint_size, bitsizetype);
6041 if (TREE_OVERFLOW (size))
6043 if (Present (gnat_attr_node))
6044 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
6045 gnat_entity);
6047 return;
6050 /* Ignore a negative size since that corresponds to our back-annotation.
6051 Also ignore a zero size unless a size clause exists, a Value_Size
6052 clause exists, or this is an integer type, in which case the
6053 front end will have always set it. */
6054 else if (tree_int_cst_sgn (size) < 0
6055 || (integer_zerop (size) && No (gnat_attr_node)
6056 && !Has_Size_Clause (gnat_entity)
6057 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
6058 return;
6060 /* If the old size is self-referential, get the maximum size. */
6061 if (CONTAINS_PLACEHOLDER_P (old_size))
6062 old_size = max_size (old_size, true);
6064 /* If the size of the object is a constant, the new size must not be
6065 smaller (the front end checks this for scalar types). */
6066 if (TREE_CODE (old_size) != INTEGER_CST
6067 || TREE_OVERFLOW (old_size)
6068 || (AGGREGATE_TYPE_P (gnu_type)
6069 && tree_int_cst_lt (size, old_size)))
6071 if (Present (gnat_attr_node))
6072 post_error_ne_tree
6073 ("Value_Size for& too small{, minimum allowed is ^}",
6074 gnat_attr_node, gnat_entity, old_size);
6076 return;
6079 /* Otherwise, set the RM_Size. */
6080 if (TREE_CODE (gnu_type) == INTEGER_TYPE
6081 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
6082 TYPE_RM_SIZE_NUM (gnu_type) = size;
6083 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
6084 TYPE_RM_SIZE_NUM (gnu_type) = size;
6085 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6086 || TREE_CODE (gnu_type) == UNION_TYPE
6087 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6088 && !TYPE_IS_FAT_POINTER_P (gnu_type))
6089 SET_TYPE_ADA_SIZE (gnu_type, size);
6092 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
6093 If TYPE is the best type, return it. Otherwise, make a new type. We
6094 only support new integral and pointer types. BIASED_P is nonzero if
6095 we are making a biased type. */
6097 static tree
6098 make_type_from_size (tree type, tree size_tree, bool biased_p)
6100 tree new_type;
6101 unsigned HOST_WIDE_INT size;
6102 bool unsigned_p;
6104 /* If size indicates an error, just return TYPE to avoid propagating the
6105 error. Likewise if it's too large to represent. */
6106 if (!size_tree || !host_integerp (size_tree, 1))
6107 return type;
6109 size = tree_low_cst (size_tree, 1);
6110 switch (TREE_CODE (type))
6112 case INTEGER_TYPE:
6113 case ENUMERAL_TYPE:
6114 /* Only do something if the type is not already the proper size and is
6115 not a packed array type. */
6116 if (TYPE_PACKED_ARRAY_TYPE_P (type)
6117 || (TYPE_PRECISION (type) == size
6118 && biased_p == (TREE_CODE (type) == INTEGER_CST
6119 && TYPE_BIASED_REPRESENTATION_P (type))))
6120 break;
6122 biased_p |= (TREE_CODE (type) == INTEGER_TYPE
6123 && TYPE_BIASED_REPRESENTATION_P (type));
6124 unsigned_p = TYPE_UNSIGNED (type) || biased_p;
6126 size = MIN (size, LONG_LONG_TYPE_SIZE);
6127 new_type
6128 = unsigned_p ? make_unsigned_type (size) : make_signed_type (size);
6129 TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
6130 TYPE_MIN_VALUE (new_type)
6131 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
6132 TYPE_MAX_VALUE (new_type)
6133 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
6134 TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
6135 TYPE_RM_SIZE_NUM (new_type) = bitsize_int (size);
6136 return new_type;
6138 case RECORD_TYPE:
6139 /* Do something if this is a fat pointer, in which case we
6140 may need to return the thin pointer. */
6141 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
6142 return
6143 build_pointer_type
6144 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
6145 break;
6147 case POINTER_TYPE:
6148 /* Only do something if this is a thin pointer, in which case we
6149 may need to return the fat pointer. */
6150 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
6151 return
6152 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
6154 break;
6156 default:
6157 break;
6160 return type;
6163 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
6164 a type or object whose present alignment is ALIGN. If this alignment is
6165 valid, return it. Otherwise, give an error and return ALIGN. */
6167 static unsigned int
6168 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
6170 Node_Id gnat_error_node = gnat_entity;
6171 unsigned int new_align;
6173 #ifndef MAX_OFILE_ALIGNMENT
6174 #define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
6175 #endif
6177 if (Present (Alignment_Clause (gnat_entity)))
6178 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
6180 /* Don't worry about checking alignment if alignment was not specified
6181 by the source program and we already posted an error for this entity. */
6183 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
6184 return align;
6186 /* Within GCC, an alignment is an integer, so we must make sure a
6187 value is specified that fits in that range. Also, alignments of
6188 more than MAX_OFILE_ALIGNMENT can't be supported. */
6190 if (! UI_Is_In_Int_Range (alignment)
6191 || ((new_align = UI_To_Int (alignment))
6192 > MAX_OFILE_ALIGNMENT / BITS_PER_UNIT))
6193 post_error_ne_num ("largest supported alignment for& is ^",
6194 gnat_error_node, gnat_entity,
6195 MAX_OFILE_ALIGNMENT / BITS_PER_UNIT);
6196 else if (!(Present (Alignment_Clause (gnat_entity))
6197 && From_At_Mod (Alignment_Clause (gnat_entity)))
6198 && new_align * BITS_PER_UNIT < align)
6199 post_error_ne_num ("alignment for& must be at least ^",
6200 gnat_error_node, gnat_entity,
6201 align / BITS_PER_UNIT);
6202 else
6203 align = MAX (align, new_align == 0 ? 1 : new_align * BITS_PER_UNIT);
6205 return align;
6208 /* Verify that OBJECT, a type or decl, is something we can implement
6209 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
6210 if we require atomic components. */
6212 static void
6213 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
6215 Node_Id gnat_error_point = gnat_entity;
6216 Node_Id gnat_node;
6217 enum machine_mode mode;
6218 unsigned int align;
6219 tree size;
6221 /* There are three case of what OBJECT can be. It can be a type, in which
6222 case we take the size, alignment and mode from the type. It can be a
6223 declaration that was indirect, in which case the relevant values are
6224 that of the type being pointed to, or it can be a normal declaration,
6225 in which case the values are of the decl. The code below assumes that
6226 OBJECT is either a type or a decl. */
6227 if (TYPE_P (object))
6229 mode = TYPE_MODE (object);
6230 align = TYPE_ALIGN (object);
6231 size = TYPE_SIZE (object);
6233 else if (DECL_BY_REF_P (object))
6235 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
6236 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
6237 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
6239 else
6241 mode = DECL_MODE (object);
6242 align = DECL_ALIGN (object);
6243 size = DECL_SIZE (object);
6246 /* Consider all floating-point types atomic and any types that that are
6247 represented by integers no wider than a machine word. */
6248 if (GET_MODE_CLASS (mode) == MODE_FLOAT
6249 || ((GET_MODE_CLASS (mode) == MODE_INT
6250 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
6251 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
6252 return;
6254 /* For the moment, also allow anything that has an alignment equal
6255 to its size and which is smaller than a word. */
6256 if (size && TREE_CODE (size) == INTEGER_CST
6257 && compare_tree_int (size, align) == 0
6258 && align <= BITS_PER_WORD)
6259 return;
6261 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
6262 gnat_node = Next_Rep_Item (gnat_node))
6264 if (!comp_p && Nkind (gnat_node) == N_Pragma
6265 && Get_Pragma_Id (Chars (gnat_node)) == Pragma_Atomic)
6266 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6267 else if (comp_p && Nkind (gnat_node) == N_Pragma
6268 && (Get_Pragma_Id (Chars (gnat_node))
6269 == Pragma_Atomic_Components))
6270 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6273 if (comp_p)
6274 post_error_ne ("atomic access to component of & cannot be guaranteed",
6275 gnat_error_point, gnat_entity);
6276 else
6277 post_error_ne ("atomic access to & cannot be guaranteed",
6278 gnat_error_point, gnat_entity);
6281 /* Check if FTYPE1 and FTYPE2, two potentially different function type nodes,
6282 have compatible signatures so that a call using one type may be safely
6283 issued if the actual target function type is the other. Return 1 if it is
6284 the case, 0 otherwise, and post errors on the incompatibilities.
6286 This is used when an Ada subprogram is mapped onto a GCC builtin, to ensure
6287 that calls to the subprogram will have arguments suitable for the later
6288 underlying builtin expansion. */
6290 static int
6291 compatible_signatures_p (tree ftype1, tree ftype2)
6293 /* As of now, we only perform very trivial tests and consider it's the
6294 programmer's responsability to ensure the type correctness in the Ada
6295 declaration, as in the regular Import cases.
6297 Mismatches typically result in either error messages from the builtin
6298 expander, internal compiler errors, or in a real call sequence. This
6299 should be refined to issue diagnostics helping error detection and
6300 correction. */
6302 /* Almost fake test, ensuring a use of each argument. */
6303 if (ftype1 == ftype2)
6304 return 1;
6306 return 1;
6309 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a new type
6310 with all size expressions that contain F updated by replacing F with R.
6311 This is identical to GCC's substitute_in_type except that it knows about
6312 TYPE_INDEX_TYPE. If F is NULL_TREE, always make a new RECORD_TYPE, even if
6313 nothing has changed. */
6315 tree
6316 gnat_substitute_in_type (tree t, tree f, tree r)
6318 tree new = t;
6319 tree tem;
6321 switch (TREE_CODE (t))
6323 case INTEGER_TYPE:
6324 case ENUMERAL_TYPE:
6325 case BOOLEAN_TYPE:
6326 case CHAR_TYPE:
6327 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6328 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
6330 tree low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
6331 tree high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
6333 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6334 return t;
6336 new = build_range_type (TREE_TYPE (t), low, high);
6337 if (TYPE_INDEX_TYPE (t))
6338 SET_TYPE_INDEX_TYPE
6339 (new, gnat_substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
6340 return new;
6343 return t;
6345 case REAL_TYPE:
6346 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6347 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
6349 tree low = NULL_TREE, high = NULL_TREE;
6351 if (TYPE_MIN_VALUE (t))
6352 low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
6353 if (TYPE_MAX_VALUE (t))
6354 high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
6356 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6357 return t;
6359 t = copy_type (t);
6360 TYPE_MIN_VALUE (t) = low;
6361 TYPE_MAX_VALUE (t) = high;
6363 return t;
6365 case COMPLEX_TYPE:
6366 tem = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6367 if (tem == TREE_TYPE (t))
6368 return t;
6370 return build_complex_type (tem);
6372 case OFFSET_TYPE:
6373 case METHOD_TYPE:
6374 case FILE_TYPE:
6375 case FUNCTION_TYPE:
6376 case LANG_TYPE:
6377 /* Don't know how to do these yet. */
6378 gcc_unreachable ();
6380 case ARRAY_TYPE:
6382 tree component = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6383 tree domain = gnat_substitute_in_type (TYPE_DOMAIN (t), f, r);
6385 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6386 return t;
6388 new = build_array_type (component, domain);
6389 TYPE_SIZE (new) = 0;
6390 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
6391 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
6392 layout_type (new);
6393 TYPE_ALIGN (new) = TYPE_ALIGN (t);
6395 /* If we had bounded the sizes of T by a constant, bound the sizes of
6396 NEW by the same constant. */
6397 if (TREE_CODE (TYPE_SIZE (t)) == MIN_EXPR)
6398 TYPE_SIZE (new)
6399 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE (t), 1),
6400 TYPE_SIZE (new));
6401 if (TREE_CODE (TYPE_SIZE_UNIT (t)) == MIN_EXPR)
6402 TYPE_SIZE_UNIT (new)
6403 = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE_UNIT (t), 1),
6404 TYPE_SIZE_UNIT (new));
6405 return new;
6408 case RECORD_TYPE:
6409 case UNION_TYPE:
6410 case QUAL_UNION_TYPE:
6412 tree field;
6413 bool changed_field
6414 = (f == NULL_TREE && !TREE_CONSTANT (TYPE_SIZE (t)));
6415 bool field_has_rep = false;
6416 tree last_field = NULL_TREE;
6418 tree new = copy_type (t);
6420 /* Start out with no fields, make new fields, and chain them
6421 in. If we haven't actually changed the type of any field,
6422 discard everything we've done and return the old type. */
6424 TYPE_FIELDS (new) = NULL_TREE;
6425 TYPE_SIZE (new) = NULL_TREE;
6427 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6429 tree new_field = copy_node (field);
6431 TREE_TYPE (new_field)
6432 = gnat_substitute_in_type (TREE_TYPE (new_field), f, r);
6434 if (DECL_HAS_REP_P (field) && !DECL_INTERNAL_P (field))
6435 field_has_rep = true;
6436 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
6437 changed_field = true;
6439 /* If this is an internal field and the type of this field is
6440 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
6441 the type just has one element, treat that as the field.
6442 But don't do this if we are processing a QUAL_UNION_TYPE. */
6443 if (TREE_CODE (t) != QUAL_UNION_TYPE
6444 && DECL_INTERNAL_P (new_field)
6445 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
6446 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
6448 if (!TYPE_FIELDS (TREE_TYPE (new_field)))
6449 continue;
6451 if (!TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))))
6453 tree next_new_field
6454 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
6456 /* Make sure omitting the union doesn't change
6457 the layout. */
6458 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
6459 new_field = next_new_field;
6463 DECL_CONTEXT (new_field) = new;
6464 SET_DECL_ORIGINAL_FIELD (new_field,
6465 (DECL_ORIGINAL_FIELD (field)
6466 ? DECL_ORIGINAL_FIELD (field) : field));
6468 /* If the size of the old field was set at a constant,
6469 propagate the size in case the type's size was variable.
6470 (This occurs in the case of a variant or discriminated
6471 record with a default size used as a field of another
6472 record.) */
6473 DECL_SIZE (new_field)
6474 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
6475 ? DECL_SIZE (field) : NULL_TREE;
6476 DECL_SIZE_UNIT (new_field)
6477 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
6478 ? DECL_SIZE_UNIT (field) : NULL_TREE;
6480 if (TREE_CODE (t) == QUAL_UNION_TYPE)
6482 tree new_q = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
6484 if (new_q != DECL_QUALIFIER (new_field))
6485 changed_field = true;
6487 /* Do the substitution inside the qualifier and if we find
6488 that this field will not be present, omit it. */
6489 DECL_QUALIFIER (new_field) = new_q;
6491 if (integer_zerop (DECL_QUALIFIER (new_field)))
6492 continue;
6495 if (!last_field)
6496 TYPE_FIELDS (new) = new_field;
6497 else
6498 TREE_CHAIN (last_field) = new_field;
6500 last_field = new_field;
6502 /* If this is a qualified type and this field will always be
6503 present, we are done. */
6504 if (TREE_CODE (t) == QUAL_UNION_TYPE
6505 && integer_onep (DECL_QUALIFIER (new_field)))
6506 break;
6509 /* If this used to be a qualified union type, but we now know what
6510 field will be present, make this a normal union. */
6511 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
6512 && (!TYPE_FIELDS (new)
6513 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
6514 TREE_SET_CODE (new, UNION_TYPE);
6515 else if (!changed_field)
6516 return t;
6518 gcc_assert (!field_has_rep);
6519 layout_type (new);
6521 /* If the size was originally a constant use it. */
6522 if (TYPE_SIZE (t) && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
6523 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
6525 TYPE_SIZE (new) = TYPE_SIZE (t);
6526 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
6527 SET_TYPE_ADA_SIZE (new, TYPE_ADA_SIZE (t));
6530 return new;
6533 default:
6534 return t;
6538 /* Return the "RM size" of GNU_TYPE. This is the actual number of bits
6539 needed to represent the object. */
6541 tree
6542 rm_size (tree gnu_type)
6544 /* For integer types, this is the precision. For record types, we store
6545 the size explicitly. For other types, this is just the size. */
6547 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
6548 return TYPE_RM_SIZE (gnu_type);
6549 else if (TREE_CODE (gnu_type) == RECORD_TYPE
6550 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6551 /* Return the rm_size of the actual data plus the size of the template. */
6552 return
6553 size_binop (PLUS_EXPR,
6554 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
6555 DECL_SIZE (TYPE_FIELDS (gnu_type)));
6556 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6557 || TREE_CODE (gnu_type) == UNION_TYPE
6558 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6559 && !TYPE_IS_FAT_POINTER_P (gnu_type)
6560 && TYPE_ADA_SIZE (gnu_type))
6561 return TYPE_ADA_SIZE (gnu_type);
6562 else
6563 return TYPE_SIZE (gnu_type);
6566 /* Return an identifier representing the external name to be used for
6567 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
6568 and the specified suffix. */
6570 tree
6571 create_concat_name (Entity_Id gnat_entity, const char *suffix)
6573 const char *str = (!suffix ? "" : suffix);
6574 String_Template temp = {1, strlen (str)};
6575 Fat_Pointer fp = {str, &temp};
6577 Get_External_Name_With_Suffix (gnat_entity, fp);
6579 #ifdef _WIN32
6580 /* A variable using the Stdcall convention (meaning we are running
6581 on a Windows box) live in a DLL. Here we adjust its name to use
6582 the jump-table, the _imp__NAME contains the address for the NAME
6583 variable. */
6585 Entity_Kind kind = Ekind (gnat_entity);
6586 const char *prefix = "_imp__";
6587 int plen = strlen (prefix);
6589 if ((kind == E_Variable || kind == E_Constant)
6590 && Convention (gnat_entity) == Convention_Stdcall)
6592 int k;
6593 for (k = 0; k <= Name_Len; k++)
6594 Name_Buffer [Name_Len - k + plen] = Name_Buffer [Name_Len - k];
6595 strncpy (Name_Buffer, prefix, plen);
6598 #endif
6600 return get_identifier (Name_Buffer);
6603 /* Return the name to be used for GNAT_ENTITY. If a type, create a
6604 fully-qualified name, possibly with type information encoding.
6605 Otherwise, return the name. */
6607 tree
6608 get_entity_name (Entity_Id gnat_entity)
6610 Get_Encoded_Name (gnat_entity);
6611 return get_identifier (Name_Buffer);
6614 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
6615 string, return a new IDENTIFIER_NODE that is the concatenation of
6616 the name in GNU_ID and SUFFIX. */
6618 tree
6619 concat_id_with_name (tree gnu_id, const char *suffix)
6621 int len = IDENTIFIER_LENGTH (gnu_id);
6623 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id),
6624 IDENTIFIER_LENGTH (gnu_id));
6625 strncpy (Name_Buffer + len, "___", 3);
6626 len += 3;
6627 strcpy (Name_Buffer + len, suffix);
6628 return get_identifier (Name_Buffer);
6631 #include "gt-ada-decl.h"