2002-02-06 Aldy Hernandez <aldyh@redhat.com>
[official-gcc.git] / gcc / ada / decl.c
blob9cf7815b7c0d2959f7b85c42edc69b08a9a8b894
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * D E C L *
6 * *
7 * C Implementation File *
8 * *
9 * $Revision$
10 * *
11 * Copyright (C) 1992-2001, Free Software Foundation, Inc. *
12 * *
13 * GNAT is free software; you can redistribute it and/or modify it under *
14 * terms of the GNU General Public License as published by the Free Soft- *
15 * ware Foundation; either version 2, or (at your option) any later ver- *
16 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
17 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
19 * for more details. You should have received a copy of the GNU General *
20 * Public License distributed with GNAT; see file COPYING. If not, write *
21 * to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, *
22 * MA 02111-1307, USA. *
23 * *
24 * GNAT was originally developed by the GNAT team at New York University. *
25 * It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). *
26 * *
27 ****************************************************************************/
29 #include "config.h"
30 #include "system.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"
38 #include "ada.h"
39 #include "types.h"
40 #include "atree.h"
41 #include "elists.h"
42 #include "namet.h"
43 #include "nlists.h"
44 #include "repinfo.h"
45 #include "snames.h"
46 #include "stringt.h"
47 #include "uintp.h"
48 #include "fe.h"
49 #include "sinfo.h"
50 #include "einfo.h"
51 #include "ada-tree.h"
52 #include "gigi.h"
54 /* Setting this to 1 suppresses hashing of types. */
55 extern int debug_no_type_hash;
57 /* Provide default values for the macros controlling stack checking.
58 This is copied from GCC's expr.h. */
60 #ifndef STACK_CHECK_BUILTIN
61 #define STACK_CHECK_BUILTIN 0
62 #endif
63 #ifndef STACK_CHECK_PROBE_INTERVAL
64 #define STACK_CHECK_PROBE_INTERVAL 4096
65 #endif
66 #ifndef STACK_CHECK_MAX_FRAME_SIZE
67 #define STACK_CHECK_MAX_FRAME_SIZE \
68 (STACK_CHECK_PROBE_INTERVAL - UNITS_PER_WORD)
69 #endif
70 #ifndef STACK_CHECK_MAX_VAR_SIZE
71 #define STACK_CHECK_MAX_VAR_SIZE (STACK_CHECK_MAX_FRAME_SIZE / 100)
72 #endif
74 /* These two variables are used to defer recursively expanding incomplete
75 types while we are processing a record or subprogram type. */
77 static int defer_incomplete_level = 0;
78 static struct incomplete
80 struct incomplete *next;
81 tree old_type;
82 Entity_Id full_type;
83 } *defer_incomplete_list = 0;
85 static tree substitution_list PARAMS ((Entity_Id, Entity_Id,
86 tree, int));
87 static int allocatable_size_p PARAMS ((tree, int));
88 static struct attrib *build_attr_list PARAMS ((Entity_Id));
89 static tree elaborate_expression PARAMS ((Node_Id, Entity_Id, tree,
90 int, int, int));
91 static tree elaborate_expression_1 PARAMS ((Node_Id, Entity_Id, tree,
92 tree, int, int));
93 static tree make_packable_type PARAMS ((tree));
94 static tree maybe_pad_type PARAMS ((tree, tree, unsigned int,
95 Entity_Id, const char *, int,
96 int, int));
97 static tree gnat_to_gnu_field PARAMS ((Entity_Id, tree, int, int));
98 static void components_to_record PARAMS ((tree, Node_Id, tree, int,
99 int, tree *, int, int));
100 static int compare_field_bitpos PARAMS ((const PTR, const PTR));
101 static Uint annotate_value PARAMS ((tree));
102 static void annotate_rep PARAMS ((Entity_Id, tree));
103 static tree compute_field_positions PARAMS ((tree, tree, tree, tree));
104 static tree validate_size PARAMS ((Uint, tree, Entity_Id,
105 enum tree_code, int, int));
106 static void set_rm_size PARAMS ((Uint, tree, Entity_Id));
107 static tree make_type_from_size PARAMS ((tree, tree, int));
108 static unsigned int validate_alignment PARAMS ((Uint, Entity_Id,
109 unsigned int));
110 static void check_ok_for_atomic PARAMS ((tree, Entity_Id, int));
112 /* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
113 GCC type corresponding to that entity. GNAT_ENTITY is assumed to
114 refer to an Ada type. */
116 tree
117 gnat_to_gnu_type (gnat_entity)
118 Entity_Id gnat_entity;
120 tree gnu_decl;
122 /* Convert the ada entity type into a GCC TYPE_DECL node. */
123 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
124 if (TREE_CODE (gnu_decl) != TYPE_DECL)
125 gigi_abort (101);
127 return TREE_TYPE (gnu_decl);
130 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
131 entity, this routine returns the equivalent GCC tree for that entity
132 (an ..._DECL node) and associates the ..._DECL node with the input GNAT
133 defining identifier.
135 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
136 initial value (in GCC tree form). This is optional for variables.
137 For renamed entities, GNU_EXPR gives the object being renamed.
139 DEFINITION is nonzero if this call is intended for a definition. This is
140 used for separate compilation where it necessary to know whether an
141 external declaration or a definition should be created if the GCC equivalent
142 was not created previously. The value of 1 is normally used for a non-zero
143 DEFINITION, but a value of 2 is used in special circumstances, defined in
144 the code. */
146 tree
147 gnat_to_gnu_entity (gnat_entity, gnu_expr, definition)
148 Entity_Id gnat_entity;
149 tree gnu_expr;
150 int definition;
152 tree gnu_entity_id;
153 tree gnu_type = 0;
154 /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
155 GNAT tree. This node will be associated with the GNAT node by calling
156 the save_gnu_tree routine at the end of the `switch' statement. */
157 tree gnu_decl = 0;
158 /* Nonzero if we have already saved gnu_decl as a gnat association. */
159 int saved = 0;
160 /* Nonzero if we incremented defer_incomplete_level. */
161 int this_deferred = 0;
162 /* Nonzero if we incremented force_global. */
163 int this_global = 0;
164 /* Nonzero if we should check to see if elaborated during processing. */
165 int maybe_present = 0;
166 /* Nonzero if we made GNU_DECL and its type here. */
167 int this_made_decl = 0;
168 struct attrib *attr_list = 0;
169 int debug_info_p = (Needs_Debug_Info (gnat_entity)
170 || debug_info_level == DINFO_LEVEL_VERBOSE);
171 Entity_Kind kind = Ekind (gnat_entity);
172 Entity_Id gnat_temp;
173 unsigned int esize
174 = ((Known_Esize (gnat_entity)
175 && UI_Is_In_Int_Range (Esize (gnat_entity)))
176 ? MIN (UI_To_Int (Esize (gnat_entity)),
177 IN (kind, Float_Kind)
178 ? LONG_DOUBLE_TYPE_SIZE
179 : IN (kind, Access_Kind) ? POINTER_SIZE * 2
180 : LONG_LONG_TYPE_SIZE)
181 : LONG_LONG_TYPE_SIZE);
182 tree gnu_size = 0;
183 int imported_p
184 = ((Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)))
185 || From_With_Type (gnat_entity));
186 unsigned int align = 0;
188 /* Since a use of an Itype is a definition, process it as such if it
189 is not in a with'ed unit. */
191 if (! definition && Is_Itype (gnat_entity)
192 && ! present_gnu_tree (gnat_entity)
193 && In_Extended_Main_Code_Unit (gnat_entity))
195 /* Ensure that we are in a subprogram mentioned in the Scope
196 chain of this entity, our current scope is global,
197 or that we encountered a task or entry (where we can't currently
198 accurately check scoping). */
199 if (current_function_decl == 0
200 || DECL_ELABORATION_PROC_P (current_function_decl))
202 process_type (gnat_entity);
203 return get_gnu_tree (gnat_entity);
206 for (gnat_temp = Scope (gnat_entity);
207 Present (gnat_temp); gnat_temp = Scope (gnat_temp))
209 if (Is_Type (gnat_temp))
210 gnat_temp = Underlying_Type (gnat_temp);
212 if (Ekind (gnat_temp) == E_Subprogram_Body)
213 gnat_temp
214 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
216 if (IN (Ekind (gnat_temp), Subprogram_Kind)
217 && Present (Protected_Body_Subprogram (gnat_temp)))
218 gnat_temp = Protected_Body_Subprogram (gnat_temp);
220 if (Ekind (gnat_temp) == E_Entry
221 || Ekind (gnat_temp) == E_Entry_Family
222 || Ekind (gnat_temp) == E_Task_Type
223 || (IN (Ekind (gnat_temp), Subprogram_Kind)
224 && present_gnu_tree (gnat_temp)
225 && (current_function_decl
226 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
228 process_type (gnat_entity);
229 return get_gnu_tree (gnat_entity);
233 /* gigi abort 122 means that the entity "gnat_entity" has an incorrect
234 scope, i.e. that its scope does not correspond to the subprogram
235 in which it is declared */
236 gigi_abort (122);
239 /* If this is entity 0, something went badly wrong. */
240 if (gnat_entity == 0)
241 gigi_abort (102);
243 /* If we've already processed this entity, return what we got last time.
244 If we are defining the node, we should not have already processed it.
245 In that case, we will abort below when we try to save a new GCC tree for
246 this object. We also need to handle the case of getting a dummy type
247 when a Full_View exists. */
249 if (present_gnu_tree (gnat_entity)
250 && (! definition
251 || (Is_Type (gnat_entity) && imported_p)))
253 gnu_decl = get_gnu_tree (gnat_entity);
255 if (TREE_CODE (gnu_decl) == TYPE_DECL
256 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
257 && IN (kind, Incomplete_Or_Private_Kind)
258 && Present (Full_View (gnat_entity)))
260 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
261 NULL_TREE, 0);
263 save_gnu_tree (gnat_entity, NULL_TREE, 0);
264 save_gnu_tree (gnat_entity, gnu_decl, 0);
267 return gnu_decl;
270 /* If this is a numeric or enumeral type, or an access type, a nonzero
271 Esize must be specified unless it was specified by the programmer. */
272 if ((IN (kind, Numeric_Kind) || IN (kind, Enumeration_Kind)
273 || (IN (kind, Access_Kind)
274 && kind != E_Access_Protected_Subprogram_Type
275 && kind != E_Access_Subtype))
276 && Unknown_Esize (gnat_entity)
277 && ! Has_Size_Clause (gnat_entity))
278 gigi_abort (109);
280 /* Likewise, RM_Size must be specified for all discrete and fixed-point
281 types. */
282 if (IN (kind, Discrete_Or_Fixed_Point_Kind)
283 && Unknown_RM_Size (gnat_entity))
284 gigi_abort (123);
286 /* Get the name of the entity and set up the line number and filename of
287 the original definition for use in any decl we make. */
289 gnu_entity_id = get_entity_name (gnat_entity);
290 set_lineno (gnat_entity, 0);
292 /* If we get here, it means we have not yet done anything with this
293 entity. If we are not defining it here, it must be external,
294 otherwise we should have defined it already. */
295 if (! definition && ! Is_Public (gnat_entity)
296 && ! type_annotate_only
297 && kind != E_Discriminant && kind != E_Component
298 && kind != E_Label
299 && ! (kind == E_Constant && Present (Full_View (gnat_entity)))
300 #if 1
301 && !IN (kind, Type_Kind)
302 #endif
304 gigi_abort (116);
306 /* For cases when we are not defining (i.e., we are referencing from
307 another compilation unit) Public entities, show we are at global level
308 for the purpose of computing sizes. Don't do this for components or
309 discriminants since the relevant test is whether or not the record is
310 being defined. */
311 if (! definition && Is_Public (gnat_entity)
312 && ! Is_Statically_Allocated (gnat_entity)
313 && kind != E_Discriminant && kind != E_Component)
314 force_global++, this_global = 1;
316 /* Handle any attributes. */
317 if (Has_Gigi_Rep_Item (gnat_entity))
318 attr_list = build_attr_list (gnat_entity);
320 switch (kind)
322 case E_Constant:
323 /* If this is a use of a deferred constant, get its full
324 declaration. */
325 if (! definition && Present (Full_View (gnat_entity)))
327 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
328 gnu_expr, definition);
329 saved = 1;
330 break;
333 /* If we have an external constant that we are not defining,
334 get the expression that is was defined to represent. We
335 may throw that expression away later if it is not a
336 constant. */
337 if (! definition
338 && Present (Expression (Declaration_Node (gnat_entity)))
339 && ! No_Initialization (Declaration_Node (gnat_entity)))
340 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
342 /* Ignore deferred constant definitions; they are processed fully in the
343 front-end. For deferred constant references, get the full
344 definition. On the other hand, constants that are renamings are
345 handled like variable renamings. If No_Initialization is set, this is
346 not a deferred constant but a constant whose value is built
347 manually. */
349 if (definition && gnu_expr == 0
350 && ! No_Initialization (Declaration_Node (gnat_entity))
351 && No (Renamed_Object (gnat_entity)))
353 gnu_decl = error_mark_node;
354 saved = 1;
355 break;
357 else if (! definition && IN (kind, Incomplete_Or_Private_Kind)
358 && Present (Full_View (gnat_entity)))
360 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
361 NULL_TREE, 0);
362 saved = 1;
363 break;
366 goto object;
368 case E_Exception:
369 /* If this is not a VMS exception, treat it as a normal object.
370 Otherwise, make an object at the specific address of character
371 type, point to it, and convert it to integer, and mask off
372 the lower 3 bits. */
373 if (! Is_VMS_Exception (gnat_entity))
374 goto object;
376 /* Allocate the global object that we use to get the value of the
377 exception. */
378 gnu_decl = create_var_decl (gnu_entity_id,
379 (Present (Interface_Name (gnat_entity))
380 ? create_concat_name (gnat_entity, 0)
381 : NULL_TREE),
382 char_type_node, NULL_TREE, 0, 0, 1, 1,
385 /* Now return the expression giving the desired value. */
386 gnu_decl
387 = build_binary_op (BIT_AND_EXPR, integer_type_node,
388 convert (integer_type_node,
389 build_unary_op (ADDR_EXPR, NULL_TREE,
390 gnu_decl)),
391 build_unary_op (NEGATE_EXPR, integer_type_node,
392 build_int_2 (7, 0)));
394 save_gnu_tree (gnat_entity, gnu_decl, 1);
395 saved = 1;
396 break;
398 case E_Discriminant:
399 case E_Component:
401 /* The GNAT record where the component was defined. */
402 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
404 /* If the variable is an inherited record component (in the case of
405 extended record types), just return the inherited entity, which
406 must be a FIELD_DECL. Likewise for discriminants.
407 For discriminants of untagged records which have explicit
408 girder discriminants, return the entity for the corresponding
409 girder discriminant. Also use Original_Record_Component
410 if the record has a private extension. */
412 if ((Base_Type (gnat_record) == gnat_record
413 || Ekind (Scope (gnat_entity)) == E_Record_Subtype_With_Private
414 || Ekind (Scope (gnat_entity)) == E_Record_Type_With_Private)
415 && Present (Original_Record_Component (gnat_entity))
416 && Original_Record_Component (gnat_entity) != gnat_entity)
418 gnu_decl
419 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
420 gnu_expr, definition);
421 saved = 1;
422 break;
425 /* If the enclosing record has explicit girder discriminants,
426 then it is an untagged record. If the Corresponding_Discriminant
427 is not empty then this must be a renamed discriminant and its
428 Original_Record_Component must point to the corresponding explicit
429 girder discriminant (i.e., we should have taken the previous
430 branch). */
432 else if (Present (Corresponding_Discriminant (gnat_entity))
433 && Is_Tagged_Type (gnat_record))
435 /* A tagged record has no explicit girder discriminants. */
437 if (First_Discriminant (gnat_record)
438 != First_Girder_Discriminant (gnat_record))
439 gigi_abort (119);
441 gnu_decl
442 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
443 gnu_expr, definition);
444 saved = 1;
445 break;
448 /* If the enclosing record has explicit girder discriminants,
449 then it is an untagged record. If the Corresponding_Discriminant
450 is not empty then this must be a renamed discriminant and its
451 Original_Record_Component must point to the corresponding explicit
452 girder discriminant (i.e., we should have taken the first
453 branch). */
455 else if (Present (Corresponding_Discriminant (gnat_entity))
456 && (First_Discriminant (gnat_record)
457 != First_Girder_Discriminant (gnat_record)))
458 gigi_abort (120);
460 /* Otherwise, if we are not defining this and we have no GCC type
461 for the containing record, make one for it. Then we should
462 have made our own equivalent. */
463 else if (! definition && ! present_gnu_tree (gnat_record))
465 /* ??? If this is in a record whose scope is a protected
466 type and we have an Original_Record_Component, use it.
467 This is a workaround for major problems in protected type
468 handling. */
469 if (Is_Protected_Type (Scope (Scope (gnat_entity)))
470 && Present (Original_Record_Component (gnat_entity)))
472 gnu_decl
473 = gnat_to_gnu_entity (Original_Record_Component
474 (gnat_entity),
475 gnu_expr, definition);
476 saved = 1;
477 break;
480 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
481 gnu_decl = get_gnu_tree (gnat_entity);
482 saved = 1;
483 break;
486 /* Here we have no GCC type and this is a reference rather than a
487 definition. This should never happen. Most likely the cause is a
488 reference before declaration in the gnat tree for gnat_entity. */
489 else
490 gigi_abort (103);
493 case E_Loop_Parameter:
494 case E_Out_Parameter:
495 case E_Variable:
497 /* Simple variables, loop variables, OUT parameters, and exceptions. */
498 object:
500 int used_by_ref = 0;
501 int const_flag
502 = ((kind == E_Constant || kind == E_Variable)
503 && ! Is_Statically_Allocated (gnat_entity)
504 && Is_True_Constant (gnat_entity)
505 && (((Nkind (Declaration_Node (gnat_entity))
506 == N_Object_Declaration)
507 && Present (Expression (Declaration_Node (gnat_entity))))
508 || Present (Renamed_Object (gnat_entity))));
509 int inner_const_flag = const_flag;
510 int static_p = Is_Statically_Allocated (gnat_entity);
511 tree gnu_ext_name = NULL_TREE;
513 if (Present (Renamed_Object (gnat_entity)) && ! definition)
515 if (kind == E_Exception)
516 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
517 NULL_TREE, 0);
518 else
519 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
522 /* Get the type after elaborating the renamed object. */
523 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
525 /* If this is a loop variable, its type should be the base type.
526 This is because the code for processing a loop determines whether
527 a normal loop end test can be done by comparing the bounds of the
528 loop against those of the base type, which is presumed to be the
529 size used for computation. But this is not correct when the size
530 of the subtype is smaller than the type. */
531 if (kind == E_Loop_Parameter)
532 gnu_type = get_base_type (gnu_type);
534 /* Reject non-renamed objects whose types are unconstrained arrays or
535 any object whose type is a dummy type or VOID_TYPE. */
537 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
538 && No (Renamed_Object (gnat_entity)))
539 || TYPE_IS_DUMMY_P (gnu_type)
540 || TREE_CODE (gnu_type) == VOID_TYPE)
542 if (type_annotate_only)
543 return error_mark_node;
544 else
545 gigi_abort (104);
548 /* If we are defining the object, see if it has a Size value and
549 validate it if so. Then get the new type, if any. */
550 if (definition)
551 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
552 gnat_entity, VAR_DECL, 0,
553 Has_Size_Clause (gnat_entity));
555 if (gnu_size != 0)
557 gnu_type
558 = make_type_from_size (gnu_type, gnu_size,
559 Has_Biased_Representation (gnat_entity));
561 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
562 gnu_size = 0;
565 /* If this object has self-referential size, it must be a record with
566 a default value. We are supposed to allocate an object of the
567 maximum size in this case unless it is a constant with an
568 initializing expression, in which case we can get the size from
569 that. Note that the resulting size may still be a variable, so
570 this may end up with an indirect allocation. */
572 if (No (Renamed_Object (gnat_entity))
573 && TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
574 && contains_placeholder_p (TYPE_SIZE (gnu_type)))
576 if (gnu_expr != 0 && kind == E_Constant)
578 gnu_size = TYPE_SIZE (TREE_TYPE (gnu_expr));
579 if (TREE_CODE (gnu_size) != INTEGER_CST
580 && contains_placeholder_p (gnu_size))
582 tree gnu_temp = gnu_expr;
584 /* Strip off any conversions in GNU_EXPR since
585 they can't be changing the size to allocate. */
586 while (TREE_CODE (gnu_temp) == UNCHECKED_CONVERT_EXPR)
587 gnu_temp = TREE_OPERAND (gnu_temp, 0);
589 gnu_size = TYPE_SIZE (TREE_TYPE (gnu_temp));
590 if (TREE_CODE (gnu_size) != INTEGER_CST
591 && contains_placeholder_p (gnu_size))
592 gnu_size = build (WITH_RECORD_EXPR, bitsizetype,
593 gnu_size, gnu_temp);
597 /* We may have no GNU_EXPR because No_Initialization is
598 set even though there's an Expression. */
599 else if (kind == E_Constant
600 && (Nkind (Declaration_Node (gnat_entity))
601 == N_Object_Declaration)
602 && Present (Expression (Declaration_Node (gnat_entity))))
603 gnu_size
604 = TYPE_SIZE (gnat_to_gnu_type
605 (Etype
606 (Expression (Declaration_Node (gnat_entity)))));
607 else
608 gnu_size = max_size (TYPE_SIZE (gnu_type), 1);
611 /* If the size is zero bytes, make it one byte since some linkers
612 have trouble with zero-sized objects. But if this will have a
613 template, that will make it nonzero. */
614 if (((gnu_size != 0 && integer_zerop (gnu_size))
615 || (TYPE_SIZE (gnu_type) != 0
616 && integer_zerop (TYPE_SIZE (gnu_type))))
617 && (! Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
618 || ! Is_Array_Type (Etype (gnat_entity))))
619 gnu_size = bitsize_unit_node;
621 /* If an alignment is specified, use it if valid. Note that
622 exceptions are objects but don't have alignments. */
623 if (kind != E_Exception && Known_Alignment (gnat_entity))
625 if (No (Alignment (gnat_entity)))
626 gigi_abort (125);
628 align
629 = validate_alignment (Alignment (gnat_entity), gnat_entity,
630 TYPE_ALIGN (gnu_type));
633 /* If this is an atomic object with no specified size and alignment,
634 but where the size of the type is a constant, set the alignment to
635 the lowest power of two greater than the size, or to the
636 biggest meaningful alignment, whichever is smaller. */
638 if (Is_Atomic (gnat_entity) && gnu_size == 0 && align == 0
639 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
641 if (! host_integerp (TYPE_SIZE (gnu_type), 1)
642 || 0 <= compare_tree_int (TYPE_SIZE (gnu_type),
643 BIGGEST_ALIGNMENT))
644 align = BIGGEST_ALIGNMENT;
645 else
646 align = ((unsigned int) 1
647 << (floor_log2 (tree_low_cst
648 (TYPE_SIZE (gnu_type), 1) - 1)
649 + 1));
652 #ifdef MINIMUM_ATOMIC_ALIGNMENT
653 /* If the size is a constant and no alignment is specified, force
654 the alignment to be the minimum valid atomic alignment. The
655 restriction on constant size avoids problems with variable-size
656 temporaries; if the size is variable, there's no issue with
657 atomic access. Also don't do this for a constant, since it isn't
658 necessary and can interfere with constant replacement. Finally,
659 do not do it for Out parameters since that creates an
660 size inconsistency with In parameters. */
661 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
662 && ! FLOAT_TYPE_P (gnu_type)
663 && ! const_flag && No (Renamed_Object (gnat_entity))
664 && ! imported_p && No (Address_Clause (gnat_entity))
665 && kind != E_Out_Parameter
666 && (gnu_size != 0 ? TREE_CODE (gnu_size) == INTEGER_CST
667 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
668 align = MINIMUM_ATOMIC_ALIGNMENT;
669 #endif
671 /* If the object is set to have atomic components, find the component
672 type and validate it.
674 ??? Note that we ignore Has_Volatile_Components on objects; it's
675 not at all clear what to do in that case. */
677 if (Has_Atomic_Components (gnat_entity))
679 tree gnu_inner
680 = (TREE_CODE (gnu_type) == ARRAY_TYPE
681 ? TREE_TYPE (gnu_type) : gnu_type);
683 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
684 && TYPE_MULTI_ARRAY_P (gnu_inner))
685 gnu_inner = TREE_TYPE (gnu_inner);
687 check_ok_for_atomic (gnu_inner, gnat_entity, 1);
690 /* Make a new type with the desired size and alignment, if needed. */
691 gnu_type = maybe_pad_type (gnu_type, gnu_size, align,
692 gnat_entity, "PAD", 0, definition, 1);
694 /* Make a volatile version of this object's type if we are to
695 make the object volatile. Note that 13.3(19) says that we
696 should treat other types of objects as volatile as well. */
697 if ((Is_Volatile (gnat_entity)
698 || Is_Exported (gnat_entity)
699 || Is_Imported (gnat_entity)
700 || Present (Address_Clause (gnat_entity)))
701 && ! TYPE_VOLATILE (gnu_type))
702 gnu_type = build_qualified_type (gnu_type,
703 (TYPE_QUALS (gnu_type)
704 | TYPE_QUAL_VOLATILE));
706 /* If this is an aliased object with an unconstrained nominal subtype,
707 make a type that includes the template. */
708 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
709 && Is_Array_Type (Etype (gnat_entity))
710 && ! type_annotate_only)
712 tree gnu_fat
713 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
714 tree gnu_temp_type
715 = TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat))));
717 gnu_type
718 = build_unc_object_type (gnu_temp_type, gnu_type,
719 concat_id_with_name (gnu_entity_id,
720 "UNC"));
723 /* Convert the expression to the type of the object except in the
724 case where the object's type is unconstrained or the object's type
725 is a padded record whose field is of self-referential size. In
726 the former case, converting will generate unnecessary evaluations
727 of the CONSTRUCTOR to compute the size and in the latter case, we
728 want to only copy the actual data. */
729 if (gnu_expr != 0
730 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
731 && ! (TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
732 && contains_placeholder_p (TYPE_SIZE (gnu_type)))
733 && ! (TREE_CODE (gnu_type) == RECORD_TYPE
734 && TYPE_IS_PADDING_P (gnu_type)
735 && (contains_placeholder_p
736 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
737 gnu_expr = convert (gnu_type, gnu_expr);
739 /* See if this is a renaming. If this is a constant renaming,
740 treat it as a normal variable whose initial value is what
741 is being renamed. We cannot do this if the type is
742 unconstrained or class-wide.
744 Otherwise, if what we are renaming is a reference, we can simply
745 return a stabilized version of that reference, after forcing
746 any SAVE_EXPRs to be evaluated. But, if this is at global level,
747 we can only do this if we know no SAVE_EXPRs will be made.
748 Otherwise, make this into a constant pointer to the object we are
749 to rename. */
751 if (Present (Renamed_Object (gnat_entity)))
753 /* If the renamed object had padding, strip off the reference
754 to the inner object and reset our type. */
755 if (TREE_CODE (gnu_expr) == COMPONENT_REF
756 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
757 == RECORD_TYPE)
758 && (TYPE_IS_PADDING_P
759 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
761 gnu_expr = TREE_OPERAND (gnu_expr, 0);
762 gnu_type = TREE_TYPE (gnu_expr);
765 if (const_flag
766 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
767 && TYPE_MODE (gnu_type) != BLKmode
768 && Ekind (Etype (gnat_entity)) != E_Class_Wide_Type
769 && !Is_Array_Type (Etype (gnat_entity)))
772 /* If this is a declaration or reference, we can just use that
773 declaration or reference as this entity. */
774 else if ((DECL_P (gnu_expr)
775 || TREE_CODE_CLASS (TREE_CODE (gnu_expr)) == 'r')
776 && ! Materialize_Entity (gnat_entity)
777 && (! global_bindings_p ()
778 || (staticp (gnu_expr)
779 && ! TREE_SIDE_EFFECTS (gnu_expr))))
781 set_lineno (gnat_entity, ! global_bindings_p ());
782 gnu_decl = gnat_stabilize_reference (gnu_expr, 1);
783 save_gnu_tree (gnat_entity, gnu_decl, 1);
784 saved = 1;
786 if (! global_bindings_p ())
787 expand_expr_stmt (build1 (CONVERT_EXPR, void_type_node,
788 gnu_decl));
789 break;
791 else
793 inner_const_flag = TREE_READONLY (gnu_expr);
794 const_flag = 1;
795 gnu_type = build_reference_type (gnu_type);
796 gnu_expr = build_unary_op (ADDR_EXPR, gnu_type, gnu_expr);
797 gnu_size = 0;
798 used_by_ref = 1;
802 /* If this is an aliased object whose nominal subtype is unconstrained,
803 the object is a record that contains both the template and
804 the object. If there is an initializer, it will have already
805 been converted to the right type, but we need to create the
806 template if there is no initializer. */
807 else if (definition && TREE_CODE (gnu_type) == RECORD_TYPE
808 && TYPE_CONTAINS_TEMPLATE_P (gnu_type)
809 && gnu_expr == 0)
810 gnu_expr
811 = build_constructor
812 (gnu_type,
813 tree_cons
814 (TYPE_FIELDS (gnu_type),
815 build_template
816 (TREE_TYPE (TYPE_FIELDS (gnu_type)),
817 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type))),
818 NULL_TREE),
819 NULL_TREE));
821 /* If this is a pointer and it does not have an initializing
822 expression, initialize it to NULL. */
823 if (definition
824 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
825 && gnu_expr == 0)
826 gnu_expr = integer_zero_node;
828 /* If we are defining the object and it has an Address clause we must
829 get the address expression from the saved GCC tree for the
830 object if the object has a Freeze_Node. Otherwise, we elaborate
831 the address expression here since the front-end has guaranteed
832 in that case that the elaboration has no effects. Note that
833 only the latter mechanism is currently in use. */
834 if (definition && Present (Address_Clause (gnat_entity)))
836 tree gnu_address
837 = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
838 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
840 save_gnu_tree (gnat_entity, NULL_TREE, 0);
842 /* Ignore the size. It's either meaningless or was handled
843 above. */
844 gnu_size = 0;
845 gnu_type = build_reference_type (gnu_type);
846 gnu_address = convert (gnu_type, gnu_address);
847 used_by_ref = 1;
848 const_flag = ! Is_Public (gnat_entity);
850 /* If we don't have an initializing expression for the underlying
851 variable, the initializing expression for the pointer is the
852 specified address. Otherwise, we have to make a COMPOUND_EXPR
853 to assign both the address and the initial value. */
854 if (gnu_expr == 0)
855 gnu_expr = gnu_address;
856 else
857 gnu_expr
858 = build (COMPOUND_EXPR, gnu_type,
859 build_binary_op
860 (MODIFY_EXPR, NULL_TREE,
861 build_unary_op (INDIRECT_REF, NULL_TREE,
862 gnu_address),
863 gnu_expr),
864 gnu_address);
867 /* If it has an address clause and we are not defining it, mark it
868 as an indirect object. Likewise for Stdcall objects that are
869 imported. */
870 if ((! definition && Present (Address_Clause (gnat_entity)))
871 || (Is_Imported (gnat_entity)
872 && Convention (gnat_entity) == Convention_Stdcall))
874 gnu_type = build_reference_type (gnu_type);
875 gnu_size = 0;
876 used_by_ref = 1;
879 /* If we are at top level and this object is of variable size,
880 make the actual type a hidden pointer to the real type and
881 make the initializer be a memory allocation and initialization.
882 Likewise for objects we aren't defining (presumed to be
883 external references from other packages), but there we do
884 not set up an initialization.
886 If the object's size overflows, make an allocator too, so that
887 Storage_Error gets raised. Note that we will never free
888 such memory, so we presume it never will get allocated. */
890 if (! allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
891 global_bindings_p () || ! definition
892 || static_p)
893 || (gnu_size != 0
894 && ! allocatable_size_p (gnu_size,
895 global_bindings_p () || ! definition
896 || static_p)))
898 gnu_type = build_reference_type (gnu_type);
899 gnu_size = 0;
900 used_by_ref = 1;
901 const_flag = 1;
903 /* Get the data part of GNU_EXPR in case this was a
904 aliased object whose nominal subtype is unconstrained.
905 In that case the pointer above will be a thin pointer and
906 build_allocator will automatically make the template and
907 constructor already made above. */
909 if (definition)
911 tree gnu_alloc_type = TREE_TYPE (gnu_type);
913 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
914 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
916 gnu_alloc_type
917 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
918 gnu_expr
919 = build_component_ref
920 (gnu_expr, NULL_TREE,
921 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))));
924 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
925 && TREE_CONSTANT_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
926 && ! Is_Imported (gnat_entity))
927 post_error ("Storage_Error will be raised at run-time?",
928 gnat_entity);
930 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr,
931 gnu_type, 0, 0);
933 else
935 gnu_expr = 0;
936 const_flag = 0;
940 /* If this object would go into the stack and has an alignment
941 larger than the default largest alignment, make a variable
942 to hold the "aligning type" with a modified initial value,
943 if any, then point to it and make that the value of this
944 variable, which is now indirect. */
946 if (! global_bindings_p () && ! static_p && definition
947 && ! imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
949 tree gnu_new_type
950 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
951 TYPE_SIZE_UNIT (gnu_type));
952 tree gnu_new_var;
954 if (gnu_expr != 0)
955 gnu_expr
956 = build_constructor (gnu_new_type,
957 tree_cons (TYPE_FIELDS (gnu_new_type),
958 gnu_expr, NULL_TREE));
959 set_lineno (gnat_entity, 1);
960 gnu_new_var
961 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
962 NULL_TREE, gnu_new_type, gnu_expr,
963 0, 0, 0, 0, 0);
965 gnu_type = build_reference_type (gnu_type);
966 gnu_expr
967 = build_unary_op
968 (ADDR_EXPR, gnu_type,
969 build_component_ref (gnu_new_var, NULL_TREE,
970 TYPE_FIELDS (gnu_new_type)));
972 gnu_size = 0;
973 used_by_ref = 1;
974 const_flag = 1;
977 /* Convert the expression to the type of the object except in the
978 case where the object's type is unconstrained or the object's type
979 is a padded record whose field is of self-referential size. In
980 the former case, converting will generate unnecessary evaluations
981 of the CONSTRUCTOR to compute the size and in the latter case, we
982 want to only copy the actual data. */
983 if (gnu_expr != 0
984 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
985 && ! (TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
986 && contains_placeholder_p (TYPE_SIZE (gnu_type)))
987 && ! (TREE_CODE (gnu_type) == RECORD_TYPE
988 && TYPE_IS_PADDING_P (gnu_type)
989 && (contains_placeholder_p
990 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
991 gnu_expr = convert (gnu_type, gnu_expr);
993 /* This name is external or there was a name specified, use it.
994 Don't use the Interface_Name if there is an address clause.
995 (see CD30005). */
996 if ((Present (Interface_Name (gnat_entity))
997 && No (Address_Clause (gnat_entity)))
998 || (Is_Public (gnat_entity)
999 && (! Is_Imported (gnat_entity) || Is_Exported (gnat_entity))))
1000 gnu_ext_name = create_concat_name (gnat_entity, 0);
1002 if (const_flag)
1003 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1004 | TYPE_QUAL_CONST));
1006 /* If this is constant initialized to a static constant and the
1007 object has an aggregrate type, force it to be statically
1008 allocated. */
1009 if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1010 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1011 && (AGGREGATE_TYPE_P (gnu_type)
1012 && ! (TREE_CODE (gnu_type) == RECORD_TYPE
1013 && TYPE_IS_PADDING_P (gnu_type))))
1014 static_p = 1;
1016 set_lineno (gnat_entity, ! global_bindings_p ());
1017 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1018 gnu_expr, const_flag,
1019 Is_Public (gnat_entity),
1020 imported_p || !definition,
1021 static_p, attr_list);
1023 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1024 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1026 if (definition && DECL_SIZE (gnu_decl) != 0
1027 && gnu_block_stack != 0
1028 && TREE_VALUE (gnu_block_stack) != 0
1029 && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1030 || (flag_stack_check && ! STACK_CHECK_BUILTIN
1031 && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1032 STACK_CHECK_MAX_VAR_SIZE))))
1033 update_setjmp_buf (TREE_VALUE (gnu_block_stack));
1035 /* If this is a public constant or we're not optimizing and we're not
1036 making a VAR_DECL for it, make one just for export or debugger
1037 use. Likewise if the address is taken or if the object or type is
1038 aliased. */
1039 if (definition && TREE_CODE (gnu_decl) == CONST_DECL
1040 && (Is_Public (gnat_entity)
1041 || optimize == 0
1042 || Address_Taken (gnat_entity)
1043 || Is_Aliased (gnat_entity)
1044 || Is_Aliased (Etype (gnat_entity))))
1045 DECL_CONST_CORRESPONDING_VAR (gnu_decl)
1046 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1047 gnu_expr, 0, Is_Public (gnat_entity), 0,
1048 static_p, 0);
1050 if (Is_Atomic (gnat_entity))
1051 check_ok_for_atomic (gnu_decl, gnat_entity, 0);
1053 /* If this is declared in a block that contains an block with an
1054 exception handler, we must force this variable in memory to
1055 suppress an invalid optimization. */
1056 if (Has_Nested_Block_With_Handler (Scope (gnat_entity)))
1058 mark_addressable (gnu_decl);
1059 flush_addressof (gnu_decl);
1062 /* Back-annotate the Alignment of the object if not already in the
1063 tree. Likewise for Esize if the object is of a constant size.
1064 But if the "object" is actually a pointer to an object, the
1065 alignment and size are the same as teh type, so don't back-annotate
1066 the values for the pointer. */
1067 if (! used_by_ref && Unknown_Alignment (gnat_entity))
1068 Set_Alignment (gnat_entity,
1069 UI_From_Int (DECL_ALIGN (gnu_decl) / BITS_PER_UNIT));
1071 if (! used_by_ref && Unknown_Esize (gnat_entity)
1072 && DECL_SIZE (gnu_decl) != 0)
1074 tree gnu_back_size = DECL_SIZE (gnu_decl);
1076 if (TREE_CODE (TREE_TYPE (gnu_decl)) == RECORD_TYPE
1077 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_decl)))
1078 gnu_back_size
1079 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN
1080 (TYPE_FIELDS (TREE_TYPE (gnu_decl)))));
1082 Set_Esize (gnat_entity, annotate_value (gnu_back_size));
1085 break;
1087 case E_Void:
1088 /* Return a TYPE_DECL for "void" that we previously made. */
1089 gnu_decl = void_type_decl_node;
1090 break;
1092 case E_Enumeration_Type:
1093 /* A special case, for the types Character and Wide_Character in
1094 Standard, we do not list all the literals. So if the literals
1095 are not specified, make this an unsigned type. */
1096 if (No (First_Literal (gnat_entity)))
1098 gnu_type = make_unsigned_type (esize);
1099 break;
1102 /* Normal case of non-character type, or non-Standard character type */
1104 /* Here we have a list of enumeral constants in First_Literal.
1105 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1106 the list to be places into TYPE_FIELDS. Each node in the list
1107 is a TREE_LIST node whose TREE_VALUE is the literal name
1108 and whose TREE_PURPOSE is the value of the literal.
1110 Esize contains the number of bits needed to represent the enumeral
1111 type, Type_Low_Bound also points to the first literal and
1112 Type_High_Bound points to the last literal. */
1114 Entity_Id gnat_literal;
1115 tree gnu_literal_list = NULL_TREE;
1117 if (Is_Unsigned_Type (gnat_entity))
1118 gnu_type = make_unsigned_type (esize);
1119 else
1120 gnu_type = make_signed_type (esize);
1122 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1124 for (gnat_literal = First_Literal (gnat_entity);
1125 Present (gnat_literal);
1126 gnat_literal = Next_Literal (gnat_literal))
1128 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1129 gnu_type);
1130 tree gnu_literal
1131 = create_var_decl (get_entity_name (gnat_literal),
1132 0, gnu_type, gnu_value, 1, 0, 0, 0, 0);
1134 save_gnu_tree (gnat_literal, gnu_literal, 0);
1135 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1136 gnu_value, gnu_literal_list);
1139 TYPE_FIELDS (gnu_type) = nreverse (gnu_literal_list);
1141 /* Note that the bounds are updated at the end of this function
1142 because to avoid an infinite recursion when we get the bounds of
1143 this type, since those bounds are objects of this type. */
1145 break;
1147 case E_Signed_Integer_Type:
1148 case E_Ordinary_Fixed_Point_Type:
1149 case E_Decimal_Fixed_Point_Type:
1150 /* For integer types, just make a signed type the appropriate number
1151 of bits. */
1152 gnu_type = make_signed_type (esize);
1153 break;
1155 case E_Modular_Integer_Type:
1156 /* For modular types, make the unsigned type of the proper number of
1157 bits and then set up the modulus, if required. */
1159 enum machine_mode mode;
1160 tree gnu_modulus;
1161 tree gnu_high = 0;
1163 if (Is_Packed_Array_Type (gnat_entity))
1164 esize = UI_To_Int (RM_Size (gnat_entity));
1166 /* Find the smallest mode at least ESIZE bits wide and make a class
1167 using that mode. */
1169 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1170 GET_MODE_BITSIZE (mode) < esize;
1171 mode = GET_MODE_WIDER_MODE (mode))
1174 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1175 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1176 = Is_Packed_Array_Type (gnat_entity);
1178 /* Get the modulus in this type. If it overflows, assume it is because
1179 it is equal to 2**Esize. Note that there is no overflow checking
1180 done on unsigned type, so we detect the overflow by looking for
1181 a modulus of zero, which is otherwise invalid. */
1182 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1184 if (! integer_zerop (gnu_modulus))
1186 TYPE_MODULAR_P (gnu_type) = 1;
1187 TYPE_MODULUS (gnu_type) = gnu_modulus;
1188 gnu_high = fold (build (MINUS_EXPR, gnu_type, gnu_modulus,
1189 convert (gnu_type, integer_one_node)));
1192 /* If we have to set TYPE_PRECISION different from its natural value,
1193 make a subtype to do do. Likewise if there is a modulus and
1194 it is not one greater than TYPE_MAX_VALUE. */
1195 if (TYPE_PRECISION (gnu_type) != esize
1196 || (TYPE_MODULAR_P (gnu_type)
1197 && ! tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1199 tree gnu_subtype = make_node (INTEGER_TYPE);
1201 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1202 TREE_TYPE (gnu_subtype) = gnu_type;
1203 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1204 TYPE_MAX_VALUE (gnu_subtype)
1205 = TYPE_MODULAR_P (gnu_type)
1206 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1207 TYPE_PRECISION (gnu_subtype) = esize;
1208 TREE_UNSIGNED (gnu_subtype) = 1;
1209 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1210 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1211 = Is_Packed_Array_Type (gnat_entity);
1212 layout_type (gnu_subtype);
1214 gnu_type = gnu_subtype;
1217 break;
1219 case E_Signed_Integer_Subtype:
1220 case E_Enumeration_Subtype:
1221 case E_Modular_Integer_Subtype:
1222 case E_Ordinary_Fixed_Point_Subtype:
1223 case E_Decimal_Fixed_Point_Subtype:
1225 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1226 that we do not want to call build_range_type since we would
1227 like each subtype node to be distinct. This will be important
1228 when memory aliasing is implemented.
1230 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1231 parent type; this fact is used by the arithmetic conversion
1232 functions.
1234 We elaborate the Ancestor_Subtype if it is not in the current
1235 unit and one of our bounds is non-static. We do this to ensure
1236 consistent naming in the case where several subtypes share the same
1237 bounds by always elaborating the first such subtype first, thus
1238 using its name. */
1240 if (definition == 0
1241 && Present (Ancestor_Subtype (gnat_entity))
1242 && ! In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1243 && (! Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1244 || ! Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1245 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1246 gnu_expr, definition);
1248 gnu_type = make_node (INTEGER_TYPE);
1249 if (Is_Packed_Array_Type (gnat_entity))
1252 esize = UI_To_Int (RM_Size (gnat_entity));
1253 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1256 TYPE_PRECISION (gnu_type) = esize;
1257 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1259 TYPE_MIN_VALUE (gnu_type)
1260 = convert (TREE_TYPE (gnu_type),
1261 elaborate_expression (Type_Low_Bound (gnat_entity),
1262 gnat_entity,
1263 get_identifier ("L"), definition, 1,
1264 Needs_Debug_Info (gnat_entity)));
1266 TYPE_MAX_VALUE (gnu_type)
1267 = convert (TREE_TYPE (gnu_type),
1268 elaborate_expression (Type_High_Bound (gnat_entity),
1269 gnat_entity,
1270 get_identifier ("U"), definition, 1,
1271 Needs_Debug_Info (gnat_entity)));
1273 /* One of the above calls might have caused us to be elaborated,
1274 so don't blow up if so. */
1275 if (present_gnu_tree (gnat_entity))
1277 maybe_present = 1;
1278 break;
1281 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1282 = Has_Biased_Representation (gnat_entity);
1284 /* This should be an unsigned type if the lower bound is constant
1285 and non-negative or if the base type is unsigned; a signed type
1286 otherwise. */
1287 TREE_UNSIGNED (gnu_type)
1288 = (TREE_UNSIGNED (TREE_TYPE (gnu_type))
1289 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1290 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1291 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1292 || Is_Unsigned_Type (gnat_entity));
1294 layout_type (gnu_type);
1296 if (Is_Packed_Array_Type (gnat_entity) && BYTES_BIG_ENDIAN)
1298 tree gnu_field_type = gnu_type;
1299 tree gnu_field;
1301 TYPE_RM_SIZE_INT (gnu_field_type)
1302 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1303 gnu_type = make_node (RECORD_TYPE);
1304 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "LJM");
1305 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1306 TYPE_PACKED (gnu_type) = 1;
1307 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1308 gnu_field_type, gnu_type, 1, 0, 0, 1),
1309 finish_record_type (gnu_type, gnu_field, 0, 0);
1310 TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1311 TYPE_ADA_SIZE (gnu_type) = bitsize_int (esize);
1314 break;
1316 case E_Floating_Point_Type:
1317 /* If this is a VAX floating-point type, use an integer of the proper
1318 size. All the operations will be handled with ASM statements. */
1319 if (Vax_Float (gnat_entity))
1321 gnu_type = make_signed_type (esize);
1322 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1323 TYPE_DIGITS_VALUE (gnu_type)
1324 = UI_To_Int (Digits_Value (gnat_entity));
1325 break;
1328 /* The type of the Low and High bounds can be our type if this is
1329 a type from Standard, so set them at the end of the function. */
1330 gnu_type = make_node (REAL_TYPE);
1331 TYPE_PRECISION (gnu_type) = esize;
1332 layout_type (gnu_type);
1333 break;
1335 case E_Floating_Point_Subtype:
1336 if (Vax_Float (gnat_entity))
1338 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1339 break;
1343 enum machine_mode mode;
1345 if (definition == 0
1346 && Present (Ancestor_Subtype (gnat_entity))
1347 && ! In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1348 && (! Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1349 || ! Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1350 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1351 gnu_expr, definition);
1353 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
1354 (GET_MODE_WIDER_MODE (mode) != VOIDmode
1355 && GET_MODE_BITSIZE (GET_MODE_WIDER_MODE (mode)) <= esize);
1356 mode = GET_MODE_WIDER_MODE (mode))
1359 gnu_type = make_node (REAL_TYPE);
1360 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1361 TYPE_PRECISION (gnu_type) = GET_MODE_BITSIZE (mode);
1363 TYPE_MIN_VALUE (gnu_type)
1364 = convert (TREE_TYPE (gnu_type),
1365 elaborate_expression (Type_Low_Bound (gnat_entity),
1366 gnat_entity, get_identifier ("L"),
1367 definition, 1,
1368 Needs_Debug_Info (gnat_entity)));
1370 TYPE_MAX_VALUE (gnu_type)
1371 = convert (TREE_TYPE (gnu_type),
1372 elaborate_expression (Type_High_Bound (gnat_entity),
1373 gnat_entity, get_identifier ("U"),
1374 definition, 1,
1375 Needs_Debug_Info (gnat_entity)));
1377 /* One of the above calls might have caused us to be elaborated,
1378 so don't blow up if so. */
1379 if (present_gnu_tree (gnat_entity))
1381 maybe_present = 1;
1382 break;
1385 layout_type (gnu_type);
1387 break;
1389 /* Array and String Types and Subtypes
1391 Unconstrained array types are represented by E_Array_Type and
1392 constrained array types are represented by E_Array_Subtype. There
1393 are no actual objects of an unconstrained array type; all we have
1394 are pointers to that type.
1396 The following fields are defined on array types and subtypes:
1398 Component_Type Component type of the array.
1399 Number_Dimensions Number of dimensions (an int).
1400 First_Index Type of first index. */
1402 case E_String_Type:
1403 case E_Array_Type:
1405 tree gnu_template_fields = NULL_TREE;
1406 tree gnu_template_type = make_node (RECORD_TYPE);
1407 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1408 tree gnu_fat_type = make_node (RECORD_TYPE);
1409 int ndim = Number_Dimensions (gnat_entity);
1410 int firstdim
1411 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1412 int nextdim
1413 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1414 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1415 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1416 tree gnu_comp_size = 0;
1417 tree gnu_max_size = size_one_node;
1418 tree gnu_max_size_unit;
1419 int index;
1420 Entity_Id gnat_ind_subtype;
1421 Entity_Id gnat_ind_base_subtype;
1422 tree gnu_template_reference;
1423 tree tem;
1425 TYPE_NAME (gnu_template_type)
1426 = create_concat_name (gnat_entity, "XUB");
1427 TYPE_NAME (gnu_fat_type) = create_concat_name (gnat_entity, "XUP");
1428 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1429 TREE_READONLY (gnu_template_type) = 1;
1431 /* Make a node for the array. If we are not defining the array
1432 suppress expanding incomplete types and save the node as the type
1433 for GNAT_ENTITY. */
1434 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1435 if (! definition)
1437 defer_incomplete_level++;
1438 this_deferred = this_made_decl = 1;
1439 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
1440 ! Comes_From_Source (gnat_entity),
1441 debug_info_p);
1442 save_gnu_tree (gnat_entity, gnu_decl, 0);
1443 saved = 1;
1446 /* Build the fat pointer type. Use a "void *" object instead of
1447 a pointer to the array type since we don't have the array type
1448 yet (it will reference the fat pointer via the bounds). */
1449 tem = chainon (chainon (NULL_TREE,
1450 create_field_decl (get_identifier ("P_ARRAY"),
1451 ptr_void_type_node,
1452 gnu_fat_type, 0, 0, 0, 0)),
1453 create_field_decl (get_identifier ("P_BOUNDS"),
1454 gnu_ptr_template,
1455 gnu_fat_type, 0, 0, 0, 0));
1457 /* Make sure we can put this into a register. */
1458 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1459 finish_record_type (gnu_fat_type, tem, 0, 1);
1461 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1462 is the fat pointer. This will be used to access the individual
1463 fields once we build them. */
1464 tem = build (COMPONENT_REF, gnu_ptr_template,
1465 build (PLACEHOLDER_EXPR, gnu_fat_type),
1466 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
1467 gnu_template_reference
1468 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1469 TREE_READONLY (gnu_template_reference) = 1;
1471 /* Now create the GCC type for each index and add the fields for
1472 that index to the template. */
1473 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1474 gnat_ind_base_subtype
1475 = First_Index (Implementation_Base_Type (gnat_entity));
1476 index < ndim && index >= 0;
1477 index += nextdim,
1478 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1479 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1481 char field_name[10];
1482 tree gnu_ind_subtype
1483 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1484 tree gnu_base_subtype
1485 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1486 tree gnu_base_min
1487 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1488 tree gnu_base_max
1489 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1490 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1492 /* Make the FIELD_DECLs for the minimum and maximum of this
1493 type and then make extractions of that field from the
1494 template. */
1495 set_lineno (gnat_entity, 0);
1496 sprintf (field_name, "LB%d", index);
1497 gnu_min_field = create_field_decl (get_identifier (field_name),
1498 gnu_ind_subtype,
1499 gnu_template_type, 0, 0, 0, 0);
1500 field_name[0] = 'U';
1501 gnu_max_field = create_field_decl (get_identifier (field_name),
1502 gnu_ind_subtype,
1503 gnu_template_type, 0, 0, 0, 0);
1505 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1507 /* We can't use build_component_ref here since the template
1508 type isn't complete yet. */
1509 gnu_min = build (COMPONENT_REF, gnu_ind_subtype,
1510 gnu_template_reference, gnu_min_field);
1511 gnu_max = build (COMPONENT_REF, gnu_ind_subtype,
1512 gnu_template_reference, gnu_max_field);
1513 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1515 /* Make a range type with the new ranges, but using
1516 the Ada subtype. Then we convert to sizetype. */
1517 gnu_index_types[index]
1518 = create_index_type (convert (sizetype, gnu_min),
1519 convert (sizetype, gnu_max),
1520 build_range_type (gnu_ind_subtype,
1521 gnu_min, gnu_max));
1522 /* Update the maximum size of the array, in elements. */
1523 gnu_max_size
1524 = size_binop (MULT_EXPR, gnu_max_size,
1525 size_binop (PLUS_EXPR, size_one_node,
1526 size_binop (MINUS_EXPR, gnu_base_max,
1527 gnu_base_min)));
1529 TYPE_NAME (gnu_index_types[index])
1530 = create_concat_name (gnat_entity, field_name);
1533 for (index = 0; index < ndim; index++)
1534 gnu_template_fields
1535 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1537 /* Install all the fields into the template. */
1538 finish_record_type (gnu_template_type, gnu_template_fields, 0, 0);
1539 TREE_READONLY (gnu_template_type) = 1;
1541 /* Now make the array of arrays and update the pointer to the array
1542 in the fat pointer. Note that it is the first field. */
1544 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1546 /* Get and validate any specified Component_Size, but if Packed,
1547 ignore it since the front end will have taken care of it. Also,
1548 allow sizes not a multiple of Storage_Unit if packed. */
1549 gnu_comp_size
1550 = validate_size (Component_Size (gnat_entity), tem,
1551 gnat_entity,
1552 (Is_Bit_Packed_Array (gnat_entity)
1553 ? TYPE_DECL : VAR_DECL), 1,
1554 Has_Component_Size_Clause (gnat_entity));
1556 if (Has_Atomic_Components (gnat_entity))
1557 check_ok_for_atomic (tem, gnat_entity, 1);
1559 /* If the component type is a RECORD_TYPE that has a self-referential
1560 size, use the maxium size. */
1561 if (gnu_comp_size == 0 && TREE_CODE (tem) == RECORD_TYPE
1562 && TREE_CODE (TYPE_SIZE (tem)) != INTEGER_CST
1563 && contains_placeholder_p (TYPE_SIZE (tem)))
1564 gnu_comp_size = max_size (TYPE_SIZE (tem), 1);
1566 if (! Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size != 0)
1568 tem = make_type_from_size (tem, gnu_comp_size, 0);
1569 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1570 "C_PAD", 0, definition, 1);
1573 if (Has_Volatile_Components (gnat_entity))
1574 tem = build_qualified_type (tem,
1575 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1577 /* If Component_Size is not already specified, annotate it with the
1578 size of the component. */
1579 if (Unknown_Component_Size (gnat_entity))
1580 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1582 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1583 size_binop (MULT_EXPR, gnu_max_size,
1584 TYPE_SIZE_UNIT (tem)));
1585 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1586 size_binop (MULT_EXPR,
1587 convert (bitsizetype,
1588 gnu_max_size),
1589 TYPE_SIZE (tem)));
1591 for (index = ndim - 1; index >= 0; index--)
1593 tem = build_array_type (tem, gnu_index_types[index]);
1594 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1595 TYPE_NONALIASED_COMPONENT (tem)
1596 = ! Has_Aliased_Components (gnat_entity);
1599 /* If an alignment is specified, use it if valid. But ignore it for
1600 types that represent the unpacked base type for packed arrays. */
1601 if (No (Packed_Array_Type (gnat_entity))
1602 && Known_Alignment (gnat_entity))
1604 if (No (Alignment (gnat_entity)))
1605 gigi_abort (124);
1607 TYPE_ALIGN (tem)
1608 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1609 TYPE_ALIGN (tem));
1612 TYPE_CONVENTION_FORTRAN_P (tem)
1613 = (Convention (gnat_entity) == Convention_Fortran);
1614 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1616 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1617 corresponding fat pointer. */
1618 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1619 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1620 TYPE_MODE (gnu_type) = BLKmode;
1621 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1622 TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type) = gnu_type;
1624 /* If the maximum size doesn't overflow, use it. */
1625 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1626 && ! TREE_OVERFLOW (gnu_max_size))
1628 TYPE_SIZE (tem)
1629 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1630 TYPE_SIZE_UNIT (tem)
1631 = size_binop (MIN_EXPR, gnu_max_size_unit,
1632 TYPE_SIZE_UNIT (tem));
1635 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1636 tem, 0, ! Comes_From_Source (gnat_entity),
1637 debug_info_p);
1638 rest_of_type_compilation (gnu_fat_type, global_bindings_p ());
1640 /* Create a record type for the object and its template and
1641 set the template at a negative offset. */
1642 tem = build_unc_object_type (gnu_template_type, tem,
1643 create_concat_name (gnat_entity, "XUT"));
1644 DECL_FIELD_OFFSET (TYPE_FIELDS (tem))
1645 = size_binop (MINUS_EXPR, size_zero_node,
1646 byte_position (TREE_CHAIN (TYPE_FIELDS (tem))));
1647 DECL_FIELD_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem))) = size_zero_node;
1648 DECL_FIELD_BIT_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem)))
1649 = bitsize_zero_node;
1650 TYPE_UNCONSTRAINED_ARRAY (tem) = gnu_type;
1651 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1653 /* Give the thin pointer type a name. */
1654 create_type_decl (create_concat_name (gnat_entity, "XUX"),
1655 build_pointer_type (tem), 0,
1656 ! Comes_From_Source (gnat_entity), debug_info_p);
1658 break;
1660 case E_String_Subtype:
1661 case E_Array_Subtype:
1663 /* This is the actual data type for array variables. Multidimensional
1664 arrays are implemented in the gnu tree as arrays of arrays. Note
1665 that for the moment arrays which have sparse enumeration subtypes as
1666 index components create sparse arrays, which is obviously space
1667 inefficient but so much easier to code for now.
1669 Also note that the subtype never refers to the unconstrained
1670 array type, which is somewhat at variance with Ada semantics.
1672 First check to see if this is simply a renaming of the array
1673 type. If so, the result is the array type. */
1675 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1676 if (! Is_Constrained (gnat_entity))
1677 break;
1678 else
1680 int index;
1681 int array_dim = Number_Dimensions (gnat_entity);
1682 int first_dim
1683 = ((Convention (gnat_entity) == Convention_Fortran)
1684 ? array_dim - 1 : 0);
1685 int next_dim
1686 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
1687 Entity_Id gnat_ind_subtype;
1688 Entity_Id gnat_ind_base_subtype;
1689 tree gnu_base_type = gnu_type;
1690 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
1691 tree gnu_comp_size = 0;
1692 tree gnu_max_size = size_one_node;
1693 tree gnu_max_size_unit;
1694 int need_index_type_struct = 0;
1695 int max_overflow = 0;
1697 /* First create the gnu types for each index. Create types for
1698 debugging information to point to the index types if the
1699 are not integer types, have variable bounds, or are
1700 wider than sizetype. */
1702 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1703 gnat_ind_base_subtype
1704 = First_Index (Implementation_Base_Type (gnat_entity));
1705 index < array_dim && index >= 0;
1706 index += next_dim,
1707 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1708 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1710 tree gnu_index_subtype
1711 = get_unpadded_type (Etype (gnat_ind_subtype));
1712 tree gnu_min
1713 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
1714 tree gnu_max
1715 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
1716 tree gnu_base_subtype
1717 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1718 tree gnu_base_min
1719 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1720 tree gnu_base_max
1721 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1722 tree gnu_base_type = get_base_type (gnu_base_subtype);
1723 tree gnu_base_base_min
1724 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
1725 tree gnu_base_base_max
1726 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
1727 tree gnu_high;
1728 tree gnu_this_max;
1730 /* If the minimum and maximum values both overflow in
1731 SIZETYPE, but the difference in the original type
1732 does not overflow in SIZETYPE, ignore the overflow
1733 indications. */
1734 if ((TYPE_PRECISION (gnu_index_subtype)
1735 > TYPE_PRECISION (sizetype))
1736 && TREE_CODE (gnu_min) == INTEGER_CST
1737 && TREE_CODE (gnu_max) == INTEGER_CST
1738 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
1739 && (! TREE_OVERFLOW
1740 (fold (build (MINUS_EXPR, gnu_index_subtype,
1741 TYPE_MAX_VALUE (gnu_index_subtype),
1742 TYPE_MIN_VALUE (gnu_index_subtype))))))
1743 TREE_OVERFLOW (gnu_min) = TREE_OVERFLOW (gnu_max)
1744 = TREE_CONSTANT_OVERFLOW (gnu_min)
1745 = TREE_CONSTANT_OVERFLOW (gnu_max) = 0;
1747 /* Similarly, if the range is null, use bounds of 1..0 for
1748 the sizetype bounds. */
1749 else if ((TYPE_PRECISION (gnu_index_subtype)
1750 > TYPE_PRECISION (sizetype))
1751 && TREE_CODE (gnu_min) == INTEGER_CST
1752 && TREE_CODE (gnu_max) == INTEGER_CST
1753 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
1754 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
1755 TYPE_MIN_VALUE (gnu_index_subtype)))
1756 gnu_min = size_one_node, gnu_max = size_zero_node;
1758 /* Now compute the size of this bound. We need to provide
1759 GCC with an upper bound to use but have to deal with the
1760 "superflat" case. There are three ways to do this. If we
1761 can prove that the array can never be superflat, we can
1762 just use the high bound of the index subtype. If we can
1763 prove that the low bound minus one can't overflow, we
1764 can do this as MAX (hb, lb - 1). Otherwise, we have to use
1765 the expression hb >= lb ? hb : lb - 1. */
1766 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
1768 /* See if the base array type is already flat. If it is, we
1769 are probably compiling an ACVC test, but it will cause the
1770 code below to malfunction if we don't handle it specially. */
1771 if (TREE_CODE (gnu_base_min) == INTEGER_CST
1772 && TREE_CODE (gnu_base_max) == INTEGER_CST
1773 && ! TREE_CONSTANT_OVERFLOW (gnu_base_min)
1774 && ! TREE_CONSTANT_OVERFLOW (gnu_base_max)
1775 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
1776 gnu_high = size_zero_node, gnu_min = size_one_node;
1778 /* If gnu_high is now an integer which overflowed, the array
1779 cannot be superflat. */
1780 else if (TREE_CODE (gnu_high) == INTEGER_CST
1781 && TREE_OVERFLOW (gnu_high))
1782 gnu_high = gnu_max;
1783 else if (TREE_UNSIGNED (gnu_base_subtype)
1784 || TREE_CODE (gnu_high) == INTEGER_CST)
1785 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
1786 else
1787 gnu_high
1788 = build_cond_expr
1789 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
1790 gnu_max, gnu_min),
1791 gnu_max, gnu_high);
1793 gnu_index_type[index]
1794 = create_index_type (gnu_min, gnu_high, gnu_index_subtype);
1796 /* Also compute the maximum size of the array. Here we
1797 see if any constraint on the index type of the base type
1798 can be used in the case of self-referential bound on
1799 the index type of the subtype. We look for a non-"infinite"
1800 and non-self-referential bound from any type involved and
1801 handle each bound separately. */
1803 if ((TREE_CODE (gnu_min) == INTEGER_CST
1804 && ! TREE_OVERFLOW (gnu_min)
1805 && ! operand_equal_p (gnu_min, gnu_base_base_min, 0))
1806 || (TREE_CODE (gnu_min) != INTEGER_CST
1807 && ! contains_placeholder_p (gnu_min)))
1808 gnu_base_min = gnu_min;
1810 if ((TREE_CODE (gnu_max) == INTEGER_CST
1811 && ! TREE_OVERFLOW (gnu_max)
1812 && ! operand_equal_p (gnu_max, gnu_base_base_max, 0))
1813 || (TREE_CODE (gnu_max) != INTEGER_CST
1814 && ! contains_placeholder_p (gnu_max)))
1815 gnu_base_max = gnu_max;
1817 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
1818 && TREE_CONSTANT_OVERFLOW (gnu_base_min))
1819 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
1820 || (TREE_CODE (gnu_base_max) == INTEGER_CST
1821 && TREE_CONSTANT_OVERFLOW (gnu_base_max))
1822 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
1823 max_overflow = 1;
1825 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
1826 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
1828 gnu_this_max
1829 = size_binop (MAX_EXPR,
1830 size_binop (PLUS_EXPR, size_one_node,
1831 size_binop (MINUS_EXPR, gnu_base_max,
1832 gnu_base_min)),
1833 size_zero_node);
1835 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1836 && TREE_CONSTANT_OVERFLOW (gnu_this_max))
1837 max_overflow = 1;
1839 gnu_max_size
1840 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1842 if (! integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
1843 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
1844 != INTEGER_CST)
1845 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
1846 || (TREE_TYPE (gnu_index_subtype) != 0
1847 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
1848 != INTEGER_TYPE))
1849 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
1850 || (TYPE_PRECISION (gnu_index_subtype)
1851 > TYPE_PRECISION (sizetype)))
1852 need_index_type_struct = 1;
1855 /* Then flatten: create the array of arrays. */
1857 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
1859 /* One of the above calls might have caused us to be elaborated,
1860 so don't blow up if so. */
1861 if (present_gnu_tree (gnat_entity))
1863 maybe_present = 1;
1864 break;
1867 /* Get and validate any specified Component_Size, but if Packed,
1868 ignore it since the front end will have taken care of it. Also,
1869 allow sizes not a multiple of Storage_Unit if packed. */
1870 gnu_comp_size
1871 = validate_size (Component_Size (gnat_entity), gnu_type,
1872 gnat_entity,
1873 (Is_Bit_Packed_Array (gnat_entity)
1874 ? TYPE_DECL : VAR_DECL),
1875 1, Has_Component_Size_Clause (gnat_entity));
1877 /* If the component type is a RECORD_TYPE that has a self-referential
1878 size, use the maxium size. */
1879 if (gnu_comp_size == 0 && TREE_CODE (gnu_type) == RECORD_TYPE
1880 && TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
1881 && contains_placeholder_p (TYPE_SIZE (gnu_type)))
1882 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), 1);
1884 if (! Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size != 0)
1886 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, 0);
1887 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
1888 gnat_entity, "C_PAD", 0,
1889 definition, 1);
1892 if (Has_Volatile_Components (Base_Type (gnat_entity)))
1893 gnu_type = build_qualified_type (gnu_type,
1894 (TYPE_QUALS (gnu_type)
1895 | TYPE_QUAL_VOLATILE));
1897 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
1898 TYPE_SIZE_UNIT (gnu_type));
1899 gnu_max_size = size_binop (MULT_EXPR,
1900 convert (bitsizetype, gnu_max_size),
1901 TYPE_SIZE (gnu_type));
1903 /* We don't want any array types shared for two reasons: first,
1904 we want to keep differently-named types distinct; second,
1905 setting TYPE_MULTI_ARRAY_TYPE of one type can clobber
1906 another. */
1907 debug_no_type_hash = 1;
1908 for (index = array_dim - 1; index >= 0; index --)
1910 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
1911 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
1912 TYPE_NONALIASED_COMPONENT (gnu_type)
1913 = ! Has_Aliased_Components (gnat_entity);
1916 /* If we are at file level and this is a multi-dimensional array, we
1917 need to make a variable corresponding to the stride of the
1918 inner dimensions. */
1919 if (global_bindings_p () && array_dim > 1)
1921 tree gnu_str_name = get_identifier ("ST");
1922 tree gnu_arr_type;
1924 for (gnu_arr_type = TREE_TYPE (gnu_type);
1925 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
1926 gnu_arr_type = TREE_TYPE (gnu_arr_type),
1927 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
1929 TYPE_SIZE (gnu_arr_type)
1930 = elaborate_expression_1 (gnat_entity, gnat_entity,
1931 TYPE_SIZE (gnu_arr_type),
1932 gnu_str_name, definition, 0);
1933 TYPE_SIZE_UNIT (gnu_arr_type)
1934 = elaborate_expression_1
1935 (gnat_entity, gnat_entity, TYPE_SIZE_UNIT (gnu_arr_type),
1936 concat_id_with_name (gnu_str_name, "U"), definition, 0);
1940 /* If we need to write out a record type giving the names of
1941 the bounds, do it now. */
1942 if (need_index_type_struct && debug_info_p)
1944 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
1945 tree gnu_field_list = 0;
1946 tree gnu_field;
1948 TYPE_NAME (gnu_bound_rec_type)
1949 = create_concat_name (gnat_entity, "XA");
1951 for (index = array_dim - 1; index >= 0; index--)
1953 tree gnu_type_name
1954 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
1956 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
1957 gnu_type_name = DECL_NAME (gnu_type_name);
1959 gnu_field = create_field_decl (gnu_type_name,
1960 integer_type_node,
1961 gnu_bound_rec_type,
1962 0, NULL_TREE, NULL_TREE, 0);
1963 TREE_CHAIN (gnu_field) = gnu_field_list;
1964 gnu_field_list = gnu_field;
1967 finish_record_type (gnu_bound_rec_type, gnu_field_list, 0, 0);
1970 debug_no_type_hash = 0;
1971 TYPE_CONVENTION_FORTRAN_P (gnu_type)
1972 = (Convention (gnat_entity) == Convention_Fortran);
1974 /* If our size depends on a placeholder and the maximum size doesn't
1975 overflow, use it. */
1976 if (TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
1977 && contains_placeholder_p (TYPE_SIZE (gnu_type))
1978 && ! (TREE_CODE (gnu_max_size) == INTEGER_CST
1979 && TREE_OVERFLOW (gnu_max_size))
1980 && ! max_overflow)
1982 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
1983 TYPE_SIZE (gnu_type));
1984 TYPE_SIZE_UNIT (gnu_type)
1985 = size_binop (MIN_EXPR, gnu_max_size_unit,
1986 TYPE_SIZE_UNIT (gnu_type));
1989 /* Set our alias set to that of our base type. This gives all
1990 array subtypes the same alias set. */
1991 TYPE_ALIAS_SET (gnu_type) = get_alias_set (gnu_base_type);
1992 record_component_aliases (gnu_type);
1995 /* If this is a packed type, make this type the same as the packed
1996 array type, but do some adjusting in the type first. */
1998 if (Present (Packed_Array_Type (gnat_entity)))
2000 Entity_Id gnat_index;
2001 tree gnu_inner_type;
2003 /* First finish the type we had been making so that we output
2004 debugging information for it */
2005 gnu_type = build_qualified_type (gnu_type,
2006 (TYPE_QUALS (gnu_type)
2007 | (TYPE_QUAL_VOLATILE
2008 * Is_Volatile (gnat_entity))));
2009 set_lineno (gnat_entity, 0);
2010 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2011 ! Comes_From_Source (gnat_entity),
2012 debug_info_p);
2013 if (! Comes_From_Source (gnat_entity))
2014 DECL_ARTIFICIAL (gnu_decl) = 1;
2016 /* Save it as our equivalent in case the call below elaborates
2017 this type again. */
2018 save_gnu_tree (gnat_entity, gnu_decl, 0);
2020 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2021 NULL_TREE, 0);
2022 this_made_decl = 1;
2023 gnu_inner_type = gnu_type = TREE_TYPE (gnu_decl);
2024 save_gnu_tree (gnat_entity, NULL_TREE, 0);
2026 if (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2027 && (TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_inner_type)
2028 || TYPE_IS_PADDING_P (gnu_inner_type)))
2029 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2031 /* We need to point the type we just made to our index type so
2032 the actual bounds can be put into a template. */
2034 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2035 && TYPE_ACTUAL_BOUNDS (gnu_inner_type) == 0)
2036 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2037 && ! TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2039 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2041 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2042 If it is, we need to make another type. */
2043 if (TYPE_MODULAR_P (gnu_inner_type))
2045 tree gnu_subtype;
2047 gnu_subtype = make_node (INTEGER_TYPE);
2049 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2050 TYPE_MIN_VALUE (gnu_subtype)
2051 = TYPE_MIN_VALUE (gnu_inner_type);
2052 TYPE_MAX_VALUE (gnu_subtype)
2053 = TYPE_MAX_VALUE (gnu_inner_type);
2054 TYPE_PRECISION (gnu_subtype)
2055 = TYPE_PRECISION (gnu_inner_type);
2056 TREE_UNSIGNED (gnu_subtype)
2057 = TREE_UNSIGNED (gnu_inner_type);
2058 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2059 layout_type (gnu_subtype);
2061 gnu_inner_type = gnu_subtype;
2064 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2067 TYPE_ACTUAL_BOUNDS (gnu_inner_type) = NULL_TREE;
2069 for (gnat_index = First_Index (gnat_entity);
2070 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2071 TYPE_ACTUAL_BOUNDS (gnu_inner_type)
2072 = tree_cons (NULL_TREE,
2073 get_unpadded_type (Etype (gnat_index)),
2074 TYPE_ACTUAL_BOUNDS (gnu_inner_type));
2076 if (Convention (gnat_entity) != Convention_Fortran)
2077 TYPE_ACTUAL_BOUNDS (gnu_inner_type)
2078 = nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type));
2080 if (TREE_CODE (gnu_type) == RECORD_TYPE
2081 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_type))
2082 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2086 /* Abort if packed array with no packed array type field set. */
2087 else if (Is_Packed (gnat_entity))
2088 gigi_abort (107);
2090 break;
2092 case E_String_Literal_Subtype:
2093 /* Create the type for a string literal. */
2095 Entity_Id gnat_full_type
2096 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2097 && Present (Full_View (Etype (gnat_entity)))
2098 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2099 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2100 tree gnu_string_array_type
2101 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2102 tree gnu_string_index_type
2103 = TREE_TYPE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_string_array_type)));
2104 tree gnu_lower_bound
2105 = convert (gnu_string_index_type,
2106 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2107 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2108 tree gnu_length = ssize_int (length - 1);
2109 tree gnu_upper_bound
2110 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2111 gnu_lower_bound,
2112 convert (gnu_string_index_type, gnu_length));
2113 tree gnu_range_type
2114 = build_range_type (gnu_string_index_type,
2115 gnu_lower_bound, gnu_upper_bound);
2116 tree gnu_index_type
2117 = create_index_type (convert (sizetype,
2118 TYPE_MIN_VALUE (gnu_range_type)),
2119 convert (sizetype,
2120 TYPE_MAX_VALUE (gnu_range_type)),
2121 gnu_range_type);
2123 gnu_type
2124 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2125 gnu_index_type);
2127 break;
2129 /* Record Types and Subtypes
2131 The following fields are defined on record types:
2133 Has_Discriminants True if the record has discriminants
2134 First_Discriminant Points to head of list of discriminants
2135 First_Entity Points to head of list of fields
2136 Is_Tagged_Type True if the record is tagged
2138 Implementation of Ada records and discriminated records:
2140 A record type definition is transformed into the equivalent of a C
2141 struct definition. The fields that are the discriminants which are
2142 found in the Full_Type_Declaration node and the elements of the
2143 Component_List found in the Record_Type_Definition node. The
2144 Component_List can be a recursive structure since each Variant of
2145 the Variant_Part of the Component_List has a Component_List.
2147 Processing of a record type definition comprises starting the list of
2148 field declarations here from the discriminants and the calling the
2149 function components_to_record to add the rest of the fields from the
2150 component list and return the gnu type node. The function
2151 components_to_record will call itself recursively as it traverses
2152 the tree. */
2154 case E_Record_Type:
2155 #if 0
2156 if (Has_Complex_Representation (gnat_entity))
2158 gnu_type
2159 = build_complex_type
2160 (get_unpadded_type
2161 (Etype (Defining_Entity
2162 (First (Component_Items
2163 (Component_List
2164 (Type_Definition
2165 (Declaration_Node (gnat_entity)))))))));
2167 /* ??? For now, don't use Complex if the real type is shorter than
2168 a word. */
2169 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (gnu_type)))
2170 >= BITS_PER_WORD)
2171 break;
2173 #endif
2176 Node_Id full_definition = Declaration_Node (gnat_entity);
2177 Node_Id record_definition = Type_Definition (full_definition);
2178 Entity_Id gnat_field;
2179 tree gnu_field;
2180 tree gnu_field_list = NULL_TREE;
2181 tree gnu_get_parent;
2182 int packed = (Is_Packed (gnat_entity) ? 1
2183 : (Component_Alignment (gnat_entity)
2184 == Calign_Storage_Unit) ? -1
2185 : 0);
2186 int has_rep = Has_Specified_Layout (gnat_entity);
2187 int all_rep = has_rep;
2188 int is_extension
2189 = (Is_Tagged_Type (gnat_entity)
2190 && Nkind (record_definition) == N_Derived_Type_Definition);
2192 /* See if all fields have a rep clause. Stop when we find one
2193 that doesn't. */
2194 for (gnat_field = First_Entity (gnat_entity);
2195 Present (gnat_field) && all_rep;
2196 gnat_field = Next_Entity (gnat_field))
2197 if ((Ekind (gnat_field) == E_Component
2198 || Ekind (gnat_field) == E_Discriminant)
2199 && No (Component_Clause (gnat_field)))
2200 all_rep = 0;
2202 /* If this is a record extension, go a level further to find the
2203 record definition. Also, verify we have a Parent_Subtype. */
2204 if (is_extension)
2206 if (! type_annotate_only
2207 || Present (Record_Extension_Part (record_definition)))
2208 record_definition = Record_Extension_Part (record_definition);
2210 if (! type_annotate_only && No (Parent_Subtype (gnat_entity)))
2211 gigi_abort (121);
2214 /* Make a node for the record. If we are not defining the record,
2215 suppress expanding incomplete types and save the node as the type
2216 for GNAT_ENTITY. We use the same RECORD_TYPE as was made
2217 for a dummy type and then show it's no longer a dummy. */
2218 gnu_type = make_dummy_type (gnat_entity);
2219 TYPE_DUMMY_P (gnu_type) = 0;
2220 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL && debug_info_p)
2221 DECL_IGNORED_P (TYPE_NAME (gnu_type)) = 0;
2223 TYPE_ALIGN (gnu_type) = 0;
2224 TYPE_PACKED (gnu_type) = packed != 0 || has_rep;
2226 if (! definition)
2228 defer_incomplete_level++;
2229 this_deferred = 1;
2230 set_lineno (gnat_entity, 0);
2231 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2232 ! Comes_From_Source (gnat_entity),
2233 debug_info_p);
2234 save_gnu_tree (gnat_entity, gnu_decl, 0);
2235 this_made_decl = saved = 1;
2238 /* If both a size and rep clause was specified, put the size in
2239 the record type now so that it can get the proper mode. */
2240 if (has_rep && Known_Esize (gnat_entity))
2241 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2243 /* Always set the alignment here so that it can be used to
2244 set the mode, if it is making the alignment stricter. If
2245 it is invalid, it will be checked again below. If this is to
2246 be Atomic, choose a default alignment of a word. */
2248 if (Known_Alignment (gnat_entity))
2249 TYPE_ALIGN (gnu_type)
2250 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2251 else if (Is_Atomic (gnat_entity))
2252 TYPE_ALIGN (gnu_type) = BITS_PER_WORD;
2254 /* If we have a Parent_Subtype, make a field for the parent. If
2255 this record has rep clauses, force the position to zero. */
2256 if (Present (Parent_Subtype (gnat_entity)))
2258 tree gnu_parent;
2260 /* A major complexity here is that the parent subtype will
2261 reference our discriminants. But those must reference
2262 the parent component of this record. So here we will
2263 initialize each of those components to a COMPONENT_REF.
2264 The first operand of that COMPONENT_REF is another
2265 COMPONENT_REF which will be filled in below, once
2266 the parent type can be safely built. */
2268 gnu_get_parent = build (COMPONENT_REF, void_type_node,
2269 build (PLACEHOLDER_EXPR, gnu_type),
2270 build_decl (FIELD_DECL, NULL_TREE,
2271 NULL_TREE));
2273 if (Has_Discriminants (gnat_entity))
2274 for (gnat_field = First_Girder_Discriminant (gnat_entity);
2275 Present (gnat_field);
2276 gnat_field = Next_Girder_Discriminant (gnat_field))
2277 if (Present (Corresponding_Discriminant (gnat_field)))
2278 save_gnu_tree
2279 (gnat_field,
2280 build (COMPONENT_REF,
2281 get_unpadded_type (Etype (gnat_field)),
2282 gnu_get_parent,
2283 gnat_to_gnu_entity (Corresponding_Discriminant
2284 (gnat_field),
2285 NULL_TREE, 0)),
2288 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_entity));
2290 gnu_field_list
2291 = create_field_decl (get_identifier
2292 (Get_Name_String (Name_uParent)),
2293 gnu_parent, gnu_type, 0,
2294 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2295 has_rep ? bitsize_zero_node : 0, 1);
2296 DECL_INTERNAL_P (gnu_field_list) = 1;
2298 TREE_TYPE (gnu_get_parent) = gnu_parent;
2299 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2302 /* Add the fields for the discriminants into the record. */
2303 if (! Is_Unchecked_Union (gnat_entity)
2304 && Has_Discriminants (gnat_entity))
2305 for (gnat_field = First_Girder_Discriminant (gnat_entity);
2306 Present (gnat_field);
2307 gnat_field = Next_Girder_Discriminant (gnat_field))
2309 /* If this is a record extension and this discriminant
2310 is the renaming of another discriminant, we've already
2311 handled the discriminant above. */
2312 if (Present (Parent_Subtype (gnat_entity))
2313 && Present (Corresponding_Discriminant (gnat_field)))
2314 continue;
2316 gnu_field
2317 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2319 /* Make an expression using a PLACEHOLDER_EXPR from the
2320 FIELD_DECL node just created and link that with the
2321 corresponding GNAT defining identifier. Then add to the
2322 list of fields. */
2323 save_gnu_tree (gnat_field,
2324 build (COMPONENT_REF, TREE_TYPE (gnu_field),
2325 build (PLACEHOLDER_EXPR,
2326 DECL_CONTEXT (gnu_field)),
2327 gnu_field),
2330 TREE_CHAIN (gnu_field) = gnu_field_list;
2331 gnu_field_list = gnu_field;
2334 /* Put the discriminants into the record (backwards), so we can
2335 know the appropriate discriminant to use for the names of the
2336 variants. */
2337 TYPE_FIELDS (gnu_type) = gnu_field_list;
2339 /* Add the listed fields into the record and finish up. */
2340 components_to_record (gnu_type, Component_List (record_definition),
2341 gnu_field_list, packed, definition, 0,
2342 0, all_rep);
2344 TYPE_DUMMY_P (gnu_type) = 0;
2345 TYPE_VOLATILE (gnu_type) = Is_Volatile (gnat_entity);
2346 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2348 /* If this is an extension type, reset the tree for any
2349 inherited discriminants. Also remove the PLACEHOLDER_EXPR
2350 for non-inherited discriminants. */
2351 if (! Is_Unchecked_Union (gnat_entity)
2352 && Has_Discriminants (gnat_entity))
2353 for (gnat_field = First_Girder_Discriminant (gnat_entity);
2354 Present (gnat_field);
2355 gnat_field = Next_Girder_Discriminant (gnat_field))
2357 if (Present (Parent_Subtype (gnat_entity))
2358 && Present (Corresponding_Discriminant (gnat_field)))
2359 save_gnu_tree (gnat_field, NULL_TREE, 0);
2360 else
2362 gnu_field = get_gnu_tree (gnat_field);
2363 save_gnu_tree (gnat_field, NULL_TREE, 0);
2364 save_gnu_tree (gnat_field, TREE_OPERAND (gnu_field, 1), 0);
2368 /* If it is a tagged record force the type to BLKmode to insure
2369 that these objects will always be placed in memory. Do the
2370 same thing for limited record types. */
2372 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2373 TYPE_MODE (gnu_type) = BLKmode;
2375 /* Fill in locations of fields. */
2376 annotate_rep (gnat_entity, gnu_type);
2378 /* If there are any entities in the chain corresponding to
2379 components that we did not elaborate, ensure we elaborate their
2380 types if they are Itypes. */
2381 for (gnat_temp = First_Entity (gnat_entity);
2382 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2383 if ((Ekind (gnat_temp) == E_Component
2384 || Ekind (gnat_temp) == E_Discriminant)
2385 && Is_Itype (Etype (gnat_temp))
2386 && ! present_gnu_tree (gnat_temp))
2387 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2389 break;
2391 case E_Class_Wide_Subtype:
2392 /* If an equivalent type is present, that is what we should use.
2393 Otherwise, fall through to handle this like a record subtype
2394 since it may have constraints. */
2396 if (Present (Equivalent_Type (gnat_entity)))
2398 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
2399 maybe_present = 1;
2400 break;
2403 /* ... fall through ... */
2405 case E_Record_Subtype:
2407 /* If Cloned_Subtype is Present it means this record subtype has
2408 identical layout to that type or subtype and we should use
2409 that GCC type for this one. The front end guarantees that
2410 the component list is shared. */
2411 if (Present (Cloned_Subtype (gnat_entity)))
2413 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2414 NULL_TREE, 0);
2415 maybe_present = 1;
2418 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2419 changing the type, make a new type with each field having the
2420 type of the field in the new subtype but having the position
2421 computed by transforming every discriminant reference according
2422 to the constraints. We don't see any difference between
2423 private and nonprivate type here since derivations from types should
2424 have been deferred until the completion of the private type. */
2425 else
2427 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2428 tree gnu_base_type;
2429 tree gnu_orig_type;
2431 if (! definition)
2432 defer_incomplete_level++, this_deferred = 1;
2434 /* Get the base type initially for its alignment and sizes. But
2435 if it is a padded type, we do all the other work with the
2436 unpadded type. */
2437 gnu_type = gnu_orig_type = gnu_base_type
2438 = gnat_to_gnu_type (gnat_base_type);
2440 if (TREE_CODE (gnu_type) == RECORD_TYPE
2441 && TYPE_IS_PADDING_P (gnu_type))
2442 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2444 if (present_gnu_tree (gnat_entity))
2446 maybe_present = 1;
2447 break;
2450 /* When the type has discriminants, and these discriminants
2451 affect the shape of what it built, factor them in.
2453 If we are making a subtype of an Unchecked_Union (must be an
2454 Itype), just return the type.
2456 We can't just use Is_Constrained because private subtypes without
2457 discriminants of full types with discriminants with default
2458 expressions are Is_Constrained but aren't constrained! */
2460 if (IN (Ekind (gnat_base_type), Record_Kind)
2461 && ! Is_For_Access_Subtype (gnat_entity)
2462 && ! Is_Unchecked_Union (gnat_base_type)
2463 && Is_Constrained (gnat_entity)
2464 && Girder_Constraint (gnat_entity) != No_Elist
2465 && Present (Discriminant_Constraint (gnat_entity)))
2467 Entity_Id gnat_field;
2468 Entity_Id gnat_root_type;
2469 tree gnu_field_list = 0;
2470 tree gnu_pos_list
2471 = compute_field_positions (gnu_orig_type, NULL_TREE,
2472 size_zero_node, bitsize_zero_node);
2473 tree gnu_subst_list
2474 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2475 definition);
2476 tree gnu_temp;
2478 /* If this is a derived type, we may be seeing fields from any
2479 original records, so add those positions and discriminant
2480 substitutions to our lists. */
2481 for (gnat_root_type = gnat_base_type;
2482 Underlying_Type (Etype (gnat_root_type)) != gnat_root_type;
2483 gnat_root_type = Underlying_Type (Etype (gnat_root_type)))
2485 gnu_pos_list
2486 = compute_field_positions
2487 (gnat_to_gnu_type (Etype (gnat_root_type)),
2488 gnu_pos_list, size_zero_node, bitsize_zero_node);
2490 if (Present (Parent_Subtype (gnat_root_type)))
2491 gnu_subst_list
2492 = substitution_list (Parent_Subtype (gnat_root_type),
2493 Empty, gnu_subst_list, definition);
2496 gnu_type = make_node (RECORD_TYPE);
2497 TYPE_NAME (gnu_type) = gnu_entity_id;
2498 TYPE_STUB_DECL (gnu_type)
2499 = pushdecl (build_decl (TYPE_DECL, NULL_TREE, gnu_type));
2500 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2502 for (gnat_field = First_Entity (gnat_entity);
2503 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2504 if (Ekind (gnat_field) == E_Component
2505 || Ekind (gnat_field) == E_Discriminant)
2507 tree gnu_old_field
2508 = gnat_to_gnu_entity
2509 (Original_Record_Component (gnat_field), NULL_TREE, 0);
2510 tree gnu_offset
2511 = TREE_VALUE (purpose_member (gnu_old_field,
2512 gnu_pos_list));
2513 tree gnu_pos = TREE_PURPOSE (gnu_offset);
2514 tree gnu_bitpos = TREE_VALUE (gnu_offset);
2515 tree gnu_field_type
2516 = gnat_to_gnu_type (Etype (gnat_field));
2517 tree gnu_size = TYPE_SIZE (gnu_field_type);
2518 tree gnu_new_pos = 0;
2519 tree gnu_field;
2521 /* If there was a component clause, the field types must be
2522 the same for the type and subtype, so copy the data from
2523 the old field to avoid recomputation here. */
2524 if (Present (Component_Clause
2525 (Original_Record_Component (gnat_field))))
2527 gnu_size = DECL_SIZE (gnu_old_field);
2528 gnu_field_type = TREE_TYPE (gnu_old_field);
2531 /* If this was a bitfield, get the size from the old field.
2532 Also ensure the type can be placed into a bitfield. */
2533 else if (DECL_BIT_FIELD (gnu_old_field))
2535 gnu_size = DECL_SIZE (gnu_old_field);
2536 if (TYPE_MODE (gnu_field_type) == BLKmode
2537 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2538 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2539 gnu_field_type = make_packable_type (gnu_field_type);
2542 if (TREE_CODE (gnu_pos) != INTEGER_CST
2543 && contains_placeholder_p (gnu_pos))
2544 for (gnu_temp = gnu_subst_list;
2545 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2546 gnu_pos = substitute_in_expr (gnu_pos,
2547 TREE_PURPOSE (gnu_temp),
2548 TREE_VALUE (gnu_temp));
2550 /* If the size is now a constant, we can set it as the
2551 size of the field when we make it. Otherwise, we need
2552 to deal with it specially. */
2553 if (TREE_CONSTANT (gnu_pos))
2554 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2556 gnu_field
2557 = create_field_decl
2558 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2559 0, gnu_size, gnu_new_pos,
2560 ! DECL_NONADDRESSABLE_P (gnu_old_field));
2562 if (! TREE_CONSTANT (gnu_pos))
2564 normalize_offset (&gnu_pos, &gnu_bitpos,
2565 DECL_OFFSET_ALIGN (gnu_old_field));
2566 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2567 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
2568 SET_DECL_OFFSET_ALIGN
2569 (gnu_field, DECL_OFFSET_ALIGN (gnu_old_field));
2570 DECL_SIZE (gnu_field) = gnu_size;
2571 DECL_SIZE_UNIT (gnu_field)
2572 = convert (sizetype,
2573 size_binop (CEIL_DIV_EXPR, gnu_size,
2574 bitsize_unit_node));
2575 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2578 DECL_INTERNAL_P (gnu_field)
2579 = DECL_INTERNAL_P (gnu_old_field);
2580 DECL_ORIGINAL_FIELD (gnu_field)
2581 = DECL_ORIGINAL_FIELD (gnu_old_field) != 0
2582 ? DECL_ORIGINAL_FIELD (gnu_old_field) : gnu_old_field;
2583 DECL_DISCRIMINANT_NUMBER (gnu_field)
2584 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2585 TREE_THIS_VOLATILE (gnu_field)
2586 = TREE_THIS_VOLATILE (gnu_old_field);
2587 TREE_CHAIN (gnu_field) = gnu_field_list;
2588 gnu_field_list = gnu_field;
2589 save_gnu_tree (gnat_field, gnu_field, 0);
2592 finish_record_type (gnu_type, nreverse (gnu_field_list), 1, 0);
2594 /* Now set the size, alignment and alias set of the new type to
2595 match that of the old one, doing any substitutions, as
2596 above. */
2597 TYPE_ALIAS_SET (gnu_type) = get_alias_set (gnu_base_type);
2598 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2599 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2600 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
2601 TYPE_ADA_SIZE (gnu_type) = TYPE_ADA_SIZE (gnu_base_type);
2603 if (TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
2604 && contains_placeholder_p (TYPE_SIZE (gnu_type)))
2605 for (gnu_temp = gnu_subst_list;
2606 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2607 TYPE_SIZE (gnu_type)
2608 = substitute_in_expr (TYPE_SIZE (gnu_type),
2609 TREE_PURPOSE (gnu_temp),
2610 TREE_VALUE (gnu_temp));
2612 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_type)) != INTEGER_CST
2613 && contains_placeholder_p (TYPE_SIZE_UNIT (gnu_type)))
2614 for (gnu_temp = gnu_subst_list;
2615 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2616 TYPE_SIZE_UNIT (gnu_type)
2617 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
2618 TREE_PURPOSE (gnu_temp),
2619 TREE_VALUE (gnu_temp));
2621 if (TYPE_ADA_SIZE (gnu_type) != 0
2622 && TREE_CODE (TYPE_ADA_SIZE (gnu_type)) != INTEGER_CST
2623 && contains_placeholder_p (TYPE_ADA_SIZE (gnu_type)))
2624 for (gnu_temp = gnu_subst_list;
2625 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2626 TYPE_ADA_SIZE (gnu_type)
2627 = substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
2628 TREE_PURPOSE (gnu_temp),
2629 TREE_VALUE (gnu_temp));
2631 /* Recompute the mode of this record type now that we know its
2632 actual size. */
2633 compute_record_mode (gnu_type);
2635 /* Fill in locations of fields. */
2636 annotate_rep (gnat_entity, gnu_type);
2639 /* If we've made a new type, record it and make an XVS type to show
2640 what this is a subtype of. Some debuggers require the XVS
2641 type to be output first, so do it in that order. */
2642 if (gnu_type != gnu_orig_type)
2644 if (debug_info_p)
2646 tree gnu_subtype_marker = make_node (RECORD_TYPE);
2647 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
2649 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
2650 gnu_orig_name = DECL_NAME (gnu_orig_name);
2652 TYPE_NAME (gnu_subtype_marker)
2653 = create_concat_name (gnat_entity, "XVS");
2654 finish_record_type (gnu_subtype_marker,
2655 create_field_decl (gnu_orig_name,
2656 integer_type_node,
2657 gnu_subtype_marker,
2658 0, NULL_TREE,
2659 NULL_TREE, 0),
2660 0, 0);
2663 TYPE_VOLATILE (gnu_type) = Is_Volatile (gnat_entity);
2664 TYPE_NAME (gnu_type) = gnu_entity_id;
2665 TYPE_STUB_DECL (gnu_type)
2666 = pushdecl (build_decl (TYPE_DECL, TYPE_NAME (gnu_type),
2667 gnu_type));
2668 DECL_ARTIFICIAL (TYPE_STUB_DECL (gnu_type)) = 1;
2669 DECL_IGNORED_P (TYPE_STUB_DECL (gnu_type)) = ! debug_info_p;
2670 rest_of_type_compilation (gnu_type, global_bindings_p ());
2673 /* Otherwise, go down all the components in the new type and
2674 make them equivalent to those in the base type. */
2675 else
2676 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
2677 gnat_temp = Next_Entity (gnat_temp))
2678 if ((Ekind (gnat_temp) == E_Discriminant
2679 && ! Is_Unchecked_Union (gnat_base_type))
2680 || Ekind (gnat_temp) == E_Component)
2681 save_gnu_tree (gnat_temp,
2682 get_gnu_tree
2683 (Original_Record_Component (gnat_temp)), 0);
2685 break;
2687 case E_Access_Subprogram_Type:
2688 /* If we are not defining this entity, and we have incomplete
2689 entities being processed above us, make a dummy type and
2690 fill it in later. */
2691 if (! definition && defer_incomplete_level != 0)
2693 struct incomplete *p
2694 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2696 gnu_type
2697 = build_pointer_type
2698 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2699 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2700 ! Comes_From_Source (gnat_entity),
2701 debug_info_p);
2702 save_gnu_tree (gnat_entity, gnu_decl, 0);
2703 this_made_decl = saved = 1;
2705 p->old_type = TREE_TYPE (gnu_type);
2706 p->full_type = Directly_Designated_Type (gnat_entity);
2707 p->next = defer_incomplete_list;
2708 defer_incomplete_list = p;
2709 break;
2712 /* ... fall through ... */
2714 case E_Allocator_Type:
2715 case E_Access_Type:
2716 case E_Access_Attribute_Type:
2717 case E_Anonymous_Access_Type:
2718 case E_General_Access_Type:
2720 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
2721 Entity_Id gnat_desig_full
2722 = ((IN (Ekind (Etype (gnat_desig_type)),
2723 Incomplete_Or_Private_Kind))
2724 ? Full_View (gnat_desig_type) : 0);
2725 /* We want to know if we'll be seeing the freeze node for any
2726 incomplete type we may be pointing to. */
2727 int in_main_unit
2728 = (Present (gnat_desig_full)
2729 ? In_Extended_Main_Code_Unit (gnat_desig_full)
2730 : In_Extended_Main_Code_Unit (gnat_desig_type));
2731 int got_fat_p = 0;
2732 int made_dummy = 0;
2733 tree gnu_desig_type = 0;
2735 if (No (gnat_desig_full)
2736 && (Ekind (gnat_desig_type) == E_Class_Wide_Type
2737 || (Ekind (gnat_desig_type) == E_Class_Wide_Subtype
2738 && Present (Equivalent_Type (gnat_desig_type)))))
2740 if (Present (Equivalent_Type (gnat_desig_type)))
2742 gnat_desig_full = Equivalent_Type (gnat_desig_type);
2743 if (IN (Ekind (gnat_desig_full), Incomplete_Or_Private_Kind))
2744 gnat_desig_full = Full_View (gnat_desig_full);
2746 else if (IN (Ekind (Root_Type (gnat_desig_type)),
2747 Incomplete_Or_Private_Kind))
2748 gnat_desig_full = Full_View (Root_Type (gnat_desig_type));
2751 if (Present (gnat_desig_full) && Is_Concurrent_Type (gnat_desig_full))
2752 gnat_desig_full = Corresponding_Record_Type (gnat_desig_full);
2754 /* If either the designated type or its full view is an
2755 unconstrained array subtype, replace it with the type it's a
2756 subtype of. This avoids problems with multiple copies of
2757 unconstrained array types. */
2758 if (Ekind (gnat_desig_type) == E_Array_Subtype
2759 && ! Is_Constrained (gnat_desig_type))
2760 gnat_desig_type = Etype (gnat_desig_type);
2761 if (Present (gnat_desig_full)
2762 && Ekind (gnat_desig_full) == E_Array_Subtype
2763 && ! Is_Constrained (gnat_desig_full))
2764 gnat_desig_full = Etype (gnat_desig_full);
2766 /* If we are pointing to an incomplete type whose completion is an
2767 unconstrained array, make a fat pointer type instead of a pointer
2768 to VOID. The two types in our fields will be pointers to VOID and
2769 will be replaced in update_pointer_to. Similiarly, if the type
2770 itself is a dummy type or an unconstrained array. Also make
2771 a dummy TYPE_OBJECT_RECORD_TYPE in case we have any thin
2772 pointers to it. */
2774 if ((Present (gnat_desig_full)
2775 && Is_Array_Type (gnat_desig_full)
2776 && ! Is_Constrained (gnat_desig_full))
2777 || (present_gnu_tree (gnat_desig_type)
2778 && TYPE_IS_DUMMY_P (TREE_TYPE
2779 (get_gnu_tree (gnat_desig_type)))
2780 && Is_Array_Type (gnat_desig_type)
2781 && ! Is_Constrained (gnat_desig_type))
2782 || (present_gnu_tree (gnat_desig_type)
2783 && (TREE_CODE (TREE_TYPE (get_gnu_tree (gnat_desig_type)))
2784 == UNCONSTRAINED_ARRAY_TYPE)
2785 && (TYPE_POINTER_TO (TREE_TYPE
2786 (get_gnu_tree (gnat_desig_type)))
2787 == 0))
2788 || (No (gnat_desig_full) && ! in_main_unit
2789 && defer_incomplete_level != 0
2790 && ! present_gnu_tree (gnat_desig_type)
2791 && Is_Array_Type (gnat_desig_type)
2792 && ! Is_Constrained (gnat_desig_type)))
2794 tree gnu_old
2795 = (present_gnu_tree (gnat_desig_type)
2796 ? gnat_to_gnu_type (gnat_desig_type)
2797 : make_dummy_type (gnat_desig_type));
2798 tree fields;
2800 /* Show the dummy we get will be a fat pointer. */
2801 got_fat_p = made_dummy = 1;
2803 /* If the call above got something that has a pointer, that
2804 pointer is our type. This could have happened either
2805 because the type was elaborated or because somebody
2806 else executed the code below. */
2807 gnu_type = TYPE_POINTER_TO (gnu_old);
2808 if (gnu_type == 0)
2810 gnu_type = make_node (RECORD_TYPE);
2811 TYPE_UNCONSTRAINED_ARRAY (gnu_type) = gnu_old;
2812 TYPE_POINTER_TO (gnu_old) = gnu_type;
2814 set_lineno (gnat_entity, 0);
2815 fields
2816 = chainon (chainon (NULL_TREE,
2817 create_field_decl
2818 (get_identifier ("P_ARRAY"),
2819 ptr_void_type_node, gnu_type,
2820 0, 0, 0, 0)),
2821 create_field_decl (get_identifier ("P_BOUNDS"),
2822 ptr_void_type_node,
2823 gnu_type, 0, 0, 0, 0));
2825 /* Make sure we can place this into a register. */
2826 TYPE_ALIGN (gnu_type)
2827 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
2828 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
2829 finish_record_type (gnu_type, fields, 0, 1);
2831 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
2832 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
2833 = concat_id_with_name (get_entity_name (gnat_desig_type),
2834 "XUT");
2835 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
2839 /* If we already know what the full type is, use it. */
2840 else if (Present (gnat_desig_full)
2841 && present_gnu_tree (gnat_desig_full))
2842 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
2844 /* Get the type of the thing we are to point to and build a pointer
2845 to it. If it is a reference to an incomplete or private type with a
2846 full view that is a record, make a dummy type node and get the
2847 actual type later when we have verified it is safe. */
2848 else if (! in_main_unit
2849 && ! present_gnu_tree (gnat_desig_type)
2850 && Present (gnat_desig_full)
2851 && ! present_gnu_tree (gnat_desig_full)
2852 && Is_Record_Type (gnat_desig_full))
2854 gnu_desig_type = make_dummy_type (gnat_desig_type);
2855 made_dummy = 1;
2858 /* Likewise if we are pointing to a record or array and we are to defer
2859 elaborating incomplete types. We do this since this access type
2860 may be the full view of some private type. Note that the
2861 unconstrained array case is handled above. */
2862 else if ((! in_main_unit || imported_p) && defer_incomplete_level != 0
2863 && ! present_gnu_tree (gnat_desig_type)
2864 && ((Is_Record_Type (gnat_desig_type)
2865 || Is_Array_Type (gnat_desig_type))
2866 || (Present (gnat_desig_full)
2867 && (Is_Record_Type (gnat_desig_full)
2868 || Is_Array_Type (gnat_desig_full)))))
2870 gnu_desig_type = make_dummy_type (gnat_desig_type);
2871 made_dummy = 1;
2873 else if (gnat_desig_type == gnat_entity)
2875 gnu_type = build_pointer_type (make_node (VOID_TYPE));
2876 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
2878 else
2879 gnu_desig_type = gnat_to_gnu_type (gnat_desig_type);
2881 /* It is possible that the above call to gnat_to_gnu_type resolved our
2882 type. If so, just return it. */
2883 if (present_gnu_tree (gnat_entity))
2885 maybe_present = 1;
2886 break;
2889 /* If we have a GCC type for the designated type, possibly
2890 modify it if we are pointing only to constant objects and then
2891 make a pointer to it. Don't do this for unconstrained arrays. */
2892 if (gnu_type == 0 && gnu_desig_type != 0)
2894 if (Is_Access_Constant (gnat_entity)
2895 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
2896 gnu_desig_type
2897 = build_qualified_type (gnu_desig_type,
2898 (TYPE_QUALS (gnu_desig_type)
2899 | TYPE_QUAL_CONST));
2901 gnu_type = build_pointer_type (gnu_desig_type);
2904 /* If we are not defining this object and we made a dummy pointer,
2905 save our current definition, evaluate the actual type, and replace
2906 the tentative type we made with the actual one. If we are to defer
2907 actually looking up the actual type, make an entry in the
2908 deferred list. */
2910 if (! in_main_unit && made_dummy)
2912 tree gnu_old_type
2913 = TYPE_FAT_POINTER_P (gnu_type)
2914 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
2916 if (esize == POINTER_SIZE
2917 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
2918 gnu_type
2919 = build_pointer_type
2920 (TYPE_OBJECT_RECORD_TYPE
2921 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
2923 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2924 ! Comes_From_Source (gnat_entity),
2925 debug_info_p);
2926 save_gnu_tree (gnat_entity, gnu_decl, 0);
2927 this_made_decl = saved = 1;
2929 if (defer_incomplete_level == 0)
2930 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
2931 gnat_to_gnu_type (gnat_desig_type));
2932 else
2934 struct incomplete *p
2935 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2937 p->old_type = gnu_old_type;
2938 p->full_type = gnat_desig_type;
2939 p->next = defer_incomplete_list;
2940 defer_incomplete_list = p;
2944 break;
2946 case E_Access_Protected_Subprogram_Type:
2947 if (type_annotate_only && No (Equivalent_Type (gnat_entity)))
2948 gnu_type = build_pointer_type (void_type_node);
2949 else
2950 /* The runtime representation is the equivalent type. */
2951 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
2953 if (Is_Itype (Directly_Designated_Type (gnat_entity))
2954 && ! present_gnu_tree (Directly_Designated_Type (gnat_entity))
2955 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
2956 && ! Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
2957 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
2958 NULL_TREE, 0);
2960 break;
2962 case E_Access_Subtype:
2964 /* We treat this as identical to its base type; any constraint is
2965 meaningful only to the front end.
2967 The designated type must be elaborated as well, if it does
2968 not have its own freeze node. Designated (sub)types created
2969 for constrained components of records with discriminants are
2970 not frozen by the front end and thus not elaborated by gigi,
2971 because their use may appear before the base type is frozen,
2972 and because it is not clear that they are needed anywhere in
2973 Gigi. With the current model, there is no correct place where
2974 they could be elaborated. */
2976 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2977 if (Is_Itype (Directly_Designated_Type (gnat_entity))
2978 && ! present_gnu_tree (Directly_Designated_Type (gnat_entity))
2979 && Is_Frozen (Directly_Designated_Type (gnat_entity))
2980 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
2982 /* If we are not defining this entity, and we have incomplete
2983 entities being processed above us, make a dummy type and
2984 elaborate it later. */
2985 if (! definition && defer_incomplete_level != 0)
2987 struct incomplete *p
2988 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2989 tree gnu_ptr_type
2990 = build_pointer_type
2991 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2993 p->old_type = TREE_TYPE (gnu_ptr_type);
2994 p->full_type = Directly_Designated_Type (gnat_entity);
2995 p->next = defer_incomplete_list;
2996 defer_incomplete_list = p;
2998 else
2999 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3000 NULL_TREE, 0);
3003 maybe_present = 1;
3004 break;
3006 /* Subprogram Entities
3008 The following access functions are defined for subprograms (functions
3009 or procedures):
3011 First_Formal The first formal parameter.
3012 Is_Imported Indicates that the subprogram has appeared in
3013 an INTERFACE or IMPORT pragma. For now we
3014 assume that the external language is C.
3015 Is_Inlined True if the subprogram is to be inlined.
3017 In addition for function subprograms we have:
3019 Etype Return type of the function.
3021 Each parameter is first checked by calling must_pass_by_ref on its
3022 type to determine if it is passed by reference. For parameters which
3023 are copied in, if they are Ada IN OUT or OUT parameters, their return
3024 value becomes part of a record which becomes the return type of the
3025 function (C function - note that this applies only to Ada procedures
3026 so there is no Ada return type). Additional code to store back the
3027 parameters will be generated on the caller side. This transformation
3028 is done here, not in the front-end.
3030 The intended result of the transformation can be seen from the
3031 equivalent source rewritings that follow:
3033 struct temp {int a,b};
3034 procedure P (A,B: IN OUT ...) is temp P (int A,B) {
3035 .. ..
3036 end P; return {A,B};
3038 procedure call
3041 temp t;
3042 P(X,Y); t = P(X,Y);
3043 X = t.a , Y = t.b;
3046 For subprogram types we need to perform mainly the same conversions to
3047 GCC form that are needed for procedures and function declarations. The
3048 only difference is that at the end, we make a type declaration instead
3049 of a function declaration. */
3051 case E_Subprogram_Type:
3052 case E_Function:
3053 case E_Procedure:
3055 /* The first GCC parameter declaration (a PARM_DECL node). The
3056 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3057 actually is the head of this parameter list. */
3058 tree gnu_param_list = NULL_TREE;
3059 /* The type returned by a function. If the subprogram is a procedure
3060 this type should be void_type_node. */
3061 tree gnu_return_type = void_type_node;
3062 /* List of fields in return type of procedure with copy in copy out
3063 parameters. */
3064 tree gnu_field_list = NULL_TREE;
3065 /* Non-null for subprograms containing parameters passed by copy in
3066 copy out (Ada IN OUT or OUT parameters not passed by reference),
3067 in which case it is the list of nodes used to specify the values of
3068 the in out/out parameters that are returned as a record upon
3069 procedure return. The TREE_PURPOSE of an element of this list is
3070 a field of the record and the TREE_VALUE is the PARM_DECL
3071 corresponding to that field. This list will be saved in the
3072 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3073 tree gnu_return_list = NULL_TREE;
3074 Entity_Id gnat_param;
3075 int inline_flag = Is_Inlined (gnat_entity);
3076 int public_flag = Is_Public (gnat_entity);
3077 int extern_flag
3078 = (Is_Public (gnat_entity) && !definition) || imported_p;
3079 int pure_flag = Is_Pure (gnat_entity);
3080 int volatile_flag = No_Return (gnat_entity);
3081 int returns_by_ref = 0;
3082 int returns_unconstrained = 0;
3083 tree gnu_ext_name = NULL_TREE;
3084 int has_copy_in_out = 0;
3085 int parmnum;
3087 if (kind == E_Subprogram_Type && ! definition)
3088 /* A parameter may refer to this type, so defer completion
3089 of any incomplete types. */
3090 defer_incomplete_level++, this_deferred = 1;
3092 /* If the subprogram has an alias, it is probably inherited, so
3093 we can use the original one. If the original "subprogram"
3094 is actually an enumeration literal, it may be the first use
3095 of its type, so we must elaborate that type now. */
3096 if (Present (Alias (gnat_entity)))
3098 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3099 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3101 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3102 gnu_expr, 0);
3104 /* Elaborate any Itypes in the parameters of this entity. */
3105 for (gnat_temp = First_Formal (gnat_entity);
3106 Present (gnat_temp);
3107 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3108 if (Is_Itype (Etype (gnat_temp)))
3109 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3111 break;
3114 if (kind == E_Function || kind == E_Subprogram_Type)
3115 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3117 /* If this function returns by reference, make the actual
3118 return type of this function the pointer and mark the decl. */
3119 if (Returns_By_Ref (gnat_entity))
3121 returns_by_ref = 1;
3123 gnu_return_type = build_pointer_type (gnu_return_type);
3126 /* If we are supposed to return an unconstrained array,
3127 actually return a fat pointer and make a note of that. Return
3128 a pointer to an unconstrained record of variable size. */
3129 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3131 gnu_return_type = TREE_TYPE (gnu_return_type);
3132 returns_unconstrained = 1;
3135 /* If the type requires a transient scope, the result is allocated
3136 on the secondary stack, so the result type of the function is
3137 just a pointer. */
3138 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3140 gnu_return_type = build_pointer_type (gnu_return_type);
3141 returns_unconstrained = 1;
3144 /* If the type is a padded type and the underlying type would not
3145 be passed by reference or this function has a foreign convention,
3146 return the underlying type. */
3147 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3148 && TYPE_IS_PADDING_P (gnu_return_type)
3149 && (! default_pass_by_ref (TREE_TYPE
3150 (TYPE_FIELDS (gnu_return_type)))
3151 || Has_Foreign_Convention (gnat_entity)))
3152 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3154 /* Look at all our parameters and get the type of
3155 each. While doing this, build a copy-out structure if
3156 we need one. */
3158 for (gnat_param = First_Formal (gnat_entity), parmnum = 0;
3159 Present (gnat_param);
3160 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3162 tree gnu_param_name = get_entity_name (gnat_param);
3163 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3164 tree gnu_param, gnu_field;
3165 int by_ref_p = 0;
3166 int by_descr_p = 0;
3167 int by_component_ptr_p = 0;
3168 int copy_in_copy_out_flag = 0;
3169 int req_by_copy = 0, req_by_ref = 0;
3171 /* See if a Mechanism was supplied that forced this
3172 parameter to be passed one way or another. */
3173 if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3174 req_by_copy = 1;
3175 else if (Mechanism (gnat_param) == Default)
3177 else if (Mechanism (gnat_param) == By_Copy)
3178 req_by_copy = 1;
3179 else if (Mechanism (gnat_param) == By_Reference)
3180 req_by_ref = 1;
3181 else if (Mechanism (gnat_param) <= By_Descriptor)
3182 by_descr_p = 1;
3183 else if (Mechanism (gnat_param) > 0)
3185 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3186 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3187 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3188 Mechanism (gnat_param)))
3189 req_by_ref = 1;
3190 else
3191 req_by_copy = 1;
3193 else
3194 post_error ("unsupported mechanism for&", gnat_param);
3196 /* If this is either a foreign function or if the
3197 underlying type won't be passed by refererence, strip off
3198 possible padding type. */
3199 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
3200 && TYPE_IS_PADDING_P (gnu_param_type)
3201 && (req_by_ref || Has_Foreign_Convention (gnat_entity)
3202 || ! must_pass_by_ref (TREE_TYPE (TYPE_FIELDS
3203 (gnu_param_type)))))
3204 gnu_param_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
3206 /* If this is an IN parameter it is read-only, so make a variant
3207 of the type that is read-only.
3209 ??? However, if this is an unconstrained array, that type can
3210 be very complex. So skip it for now. Likewise for any other
3211 self-referential type. */
3212 if (Ekind (gnat_param) == E_In_Parameter
3213 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
3214 && ! (TYPE_SIZE (gnu_param_type) != 0
3215 && TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3216 && contains_placeholder_p (TYPE_SIZE (gnu_param_type))))
3217 gnu_param_type
3218 = build_qualified_type (gnu_param_type,
3219 (TYPE_QUALS (gnu_param_type)
3220 | TYPE_QUAL_CONST));
3222 /* For foreign conventions, pass arrays as a pointer to the
3223 underlying type. First check for unconstrained array and get
3224 the underlying array. Then get the component type and build
3225 a pointer to it. */
3226 if (Has_Foreign_Convention (gnat_entity)
3227 && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
3228 gnu_param_type
3229 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3230 (TREE_TYPE (gnu_param_type))));
3232 if (by_descr_p)
3233 gnu_param_type
3234 = build_pointer_type
3235 (build_vms_descriptor (gnu_param_type,
3236 Mechanism (gnat_param),
3237 gnat_entity));
3239 else if (Has_Foreign_Convention (gnat_entity)
3240 && ! req_by_copy
3241 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
3243 /* Strip off any multi-dimensional entries, then strip
3244 off the last array to get the component type. */
3245 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
3246 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
3247 gnu_param_type = TREE_TYPE (gnu_param_type);
3249 by_component_ptr_p = 1;
3250 gnu_param_type = TREE_TYPE (gnu_param_type);
3252 if (Ekind (gnat_param) == E_In_Parameter)
3253 gnu_param_type
3254 = build_qualified_type (gnu_param_type,
3255 (TYPE_QUALS (gnu_param_type)
3256 | TYPE_QUAL_CONST));
3258 gnu_param_type = build_pointer_type (gnu_param_type);
3261 /* Fat pointers are passed as thin pointers for foreign
3262 conventions. */
3263 else if (Has_Foreign_Convention (gnat_entity)
3264 && TYPE_FAT_POINTER_P (gnu_param_type))
3265 gnu_param_type
3266 = make_type_from_size (gnu_param_type,
3267 size_int (POINTER_SIZE), 0);
3269 /* If we must pass or were requested to pass by reference, do so.
3270 If we were requested to pass by copy, do so.
3271 Otherwise, for foreign conventions, pass all in out parameters
3272 or aggregates by reference. For COBOL and Fortran, pass
3273 all integer and FP types that way too. For Convention Ada,
3274 use the standard Ada default. */
3275 else if (must_pass_by_ref (gnu_param_type) || req_by_ref
3276 || (! req_by_copy
3277 && ((Has_Foreign_Convention (gnat_entity)
3278 && (Ekind (gnat_param) != E_In_Parameter
3279 || AGGREGATE_TYPE_P (gnu_param_type)))
3280 || (((Convention (gnat_entity)
3281 == Convention_Fortran)
3282 || (Convention (gnat_entity)
3283 == Convention_COBOL))
3284 && (INTEGRAL_TYPE_P (gnu_param_type)
3285 || FLOAT_TYPE_P (gnu_param_type)))
3286 /* For convention Ada, see if we pass by reference
3287 by default. */
3288 || (! Has_Foreign_Convention (gnat_entity)
3289 && default_pass_by_ref (gnu_param_type)))))
3291 gnu_param_type = build_reference_type (gnu_param_type);
3292 by_ref_p = 1;
3295 else if (Ekind (gnat_param) != E_In_Parameter)
3296 copy_in_copy_out_flag = 1;
3298 if (req_by_copy && (by_ref_p || by_component_ptr_p))
3299 post_error ("?cannot pass & by copy", gnat_param);
3301 /* If this is an OUT parameter that isn't passed by reference
3302 and isn't a pointer or aggregate, we don't make a PARM_DECL
3303 for it. Instead, it will be a VAR_DECL created when we process
3304 the procedure. For the special parameter of Valued_Procedure,
3305 never pass it in. */
3306 if (Ekind (gnat_param) == E_Out_Parameter && ! by_ref_p
3307 && ((Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3308 || (! by_descr_p
3309 && ! POINTER_TYPE_P (gnu_param_type)
3310 && ! AGGREGATE_TYPE_P (gnu_param_type))))
3311 gnu_param = 0;
3312 else
3314 set_lineno (gnat_param, 0);
3315 gnu_param
3316 = create_param_decl
3317 (gnu_param_name, gnu_param_type,
3318 by_ref_p || by_component_ptr_p
3319 || Ekind (gnat_param) == E_In_Parameter);
3321 DECL_BY_REF_P (gnu_param) = by_ref_p;
3322 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr_p;
3323 DECL_BY_DESCRIPTOR_P (gnu_param) = by_descr_p;
3324 DECL_POINTS_TO_READONLY_P (gnu_param)
3325 = (Ekind (gnat_param) == E_In_Parameter
3326 && (by_ref_p || by_component_ptr_p));
3327 save_gnu_tree (gnat_param, gnu_param, 0);
3328 gnu_param_list = chainon (gnu_param, gnu_param_list);
3330 /* If a parameter is a pointer, this function may modify
3331 memory through it and thus shouldn't be considered
3332 a pure function. Also, the memory may be modified
3333 between two calls, so they can't be CSE'ed. The latter
3334 case also handles by-ref parameters. */
3335 if (POINTER_TYPE_P (gnu_param_type)
3336 || TYPE_FAT_POINTER_P (gnu_param_type))
3337 pure_flag = 0;
3340 if (copy_in_copy_out_flag)
3342 if (! has_copy_in_out)
3344 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
3345 gigi_abort (111);
3347 gnu_return_type = make_node (RECORD_TYPE);
3348 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3349 has_copy_in_out = 1;
3352 set_lineno (gnat_param, 0);
3353 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3354 gnu_return_type, 0, 0, 0, 0);
3355 TREE_CHAIN (gnu_field) = gnu_field_list;
3356 gnu_field_list = gnu_field;
3357 gnu_return_list = tree_cons (gnu_field, gnu_param,
3358 gnu_return_list);
3362 /* Do not compute record for out parameters if subprogram is
3363 stubbed since structures are incomplete for the back-end. */
3364 if (gnu_field_list != 0
3365 && Convention (gnat_entity) != Convention_Stubbed)
3366 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3367 0, 0);
3369 /* If we have a CICO list but it has only one entry, we convert
3370 this function into a function that simply returns that one
3371 object. */
3372 if (list_length (gnu_return_list) == 1)
3373 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3375 if (Convention (gnat_entity) == Convention_Stdcall)
3377 struct attrib *attr
3378 = (struct attrib *) xmalloc (sizeof (struct attrib));
3380 attr->next = attr_list;
3381 attr->type = ATTR_MACHINE_ATTRIBUTE;
3382 attr->name = get_identifier ("stdcall");
3383 attr->arg = NULL_TREE;
3384 attr->error_point = gnat_entity;
3385 attr_list = attr;
3388 /* Both lists ware built in reverse. */
3389 gnu_param_list = nreverse (gnu_param_list);
3390 gnu_return_list = nreverse (gnu_return_list);
3392 gnu_type
3393 = create_subprog_type (gnu_return_type, gnu_param_list,
3394 gnu_return_list, returns_unconstrained,
3395 returns_by_ref,
3396 Function_Returns_With_DSP (gnat_entity));
3398 /* ??? For now, don't consider nested functions pure. */
3399 if (! global_bindings_p ())
3400 pure_flag = 0;
3402 gnu_type
3403 = build_qualified_type (gnu_type,
3404 (TYPE_QUALS (gnu_type)
3405 | (TYPE_QUAL_CONST * pure_flag)
3406 | (TYPE_QUAL_VOLATILE * volatile_flag)));
3408 /* Top-level or external functions need to have an assembler name.
3409 This is passed to create_subprog_decl through the ext_name argument.
3410 For Pragma Interface subprograms with no Pragma Interface_Name, the
3411 simple name already in entity_name is correct, and this is what is
3412 gotten when ext_name is NULL. If Interface_Name is specified, then
3413 the name is extracted from the N_String_Literal node containing the
3414 string specified in the Pragma. If there is no Pragma Interface,
3415 then the Ada fully qualified name is created. */
3417 if (Present (Interface_Name (gnat_entity))
3418 || ! (Is_Imported (gnat_entity) || Is_Exported (gnat_entity)))
3419 gnu_ext_name = create_concat_name (gnat_entity, 0);
3421 set_lineno (gnat_entity, 0);
3423 /* If we are defining the subprogram and it has an Address clause
3424 we must get the address expression from the saved GCC tree for the
3425 subprogram if it has a Freeze_Node. Otherwise, we elaborate
3426 the address expression here since the front-end has guaranteed
3427 in that case that the elaboration has no effects. If there is
3428 an Address clause and we are not defining the object, just
3429 make it a constant. */
3430 if (Present (Address_Clause (gnat_entity)))
3432 tree gnu_address = 0;
3434 if (definition)
3435 gnu_address
3436 = (present_gnu_tree (gnat_entity)
3437 ? get_gnu_tree (gnat_entity)
3438 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
3440 save_gnu_tree (gnat_entity, NULL_TREE, 0);
3442 gnu_type = build_reference_type (gnu_type);
3443 if (gnu_address != 0)
3444 gnu_address = convert (gnu_type, gnu_address);
3446 gnu_decl
3447 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
3448 gnu_address, 0, Is_Public (gnat_entity),
3449 extern_flag, 0, 0);
3450 DECL_BY_REF_P (gnu_decl) = 1;
3453 else if (kind == E_Subprogram_Type)
3454 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3455 ! Comes_From_Source (gnat_entity),
3456 debug_info_p);
3457 else
3459 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
3460 gnu_type, gnu_param_list,
3461 inline_flag, public_flag,
3462 extern_flag, attr_list);
3463 DECL_STUBBED_P (gnu_decl)
3464 = Convention (gnat_entity) == Convention_Stubbed;
3467 break;
3469 case E_Incomplete_Type:
3470 case E_Private_Type:
3471 case E_Limited_Private_Type:
3472 case E_Record_Type_With_Private:
3473 case E_Private_Subtype:
3474 case E_Limited_Private_Subtype:
3475 case E_Record_Subtype_With_Private:
3477 /* If this type does not have a full view in the unit we are
3478 compiling, then just get the type from its Etype. */
3479 if (No (Full_View (gnat_entity)))
3481 /* If this is an incomplete type with no full view, it must
3482 be a Taft Amendement type, so just return a dummy type. */
3483 if (kind == E_Incomplete_Type)
3484 gnu_type = make_dummy_type (gnat_entity);
3486 else if (Present (Underlying_Full_View (gnat_entity)))
3487 gnu_decl = gnat_to_gnu_entity (Underlying_Full_View (gnat_entity),
3488 NULL_TREE, 0);
3489 else
3491 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
3492 NULL_TREE, 0);
3493 maybe_present = 1;
3496 break;
3499 /* Otherwise, if we are not defining the type now, get the
3500 type from the full view. But always get the type from the full
3501 view for define on use types, since otherwise we won't see them! */
3503 else if (! definition
3504 || (Is_Itype (Full_View (gnat_entity))
3505 && No (Freeze_Node (gnat_entity)))
3506 || (Is_Itype (gnat_entity)
3507 && No (Freeze_Node (Full_View (gnat_entity)))))
3509 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
3510 NULL_TREE, 0);
3511 maybe_present = 1;
3512 break;
3515 /* For incomplete types, make a dummy type entry which will be
3516 replaced later. */
3517 gnu_type = make_dummy_type (gnat_entity);
3519 /* Save this type as the full declaration's type so we can do any needed
3520 updates when we see it. */
3521 set_lineno (gnat_entity, 0);
3522 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3523 ! Comes_From_Source (gnat_entity),
3524 debug_info_p);
3525 save_gnu_tree (Full_View (gnat_entity), gnu_decl, 0);
3526 break;
3528 /* Simple class_wide types are always viewed as their root_type
3529 by Gigi unless an Equivalent_Type is specified. */
3530 case E_Class_Wide_Type:
3531 if (Present (Equivalent_Type (gnat_entity)))
3532 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3533 else
3534 gnu_type = gnat_to_gnu_type (Root_Type (gnat_entity));
3536 maybe_present = 1;
3537 break;
3539 case E_Task_Type:
3540 case E_Task_Subtype:
3541 case E_Protected_Type:
3542 case E_Protected_Subtype:
3543 if (type_annotate_only && No (Corresponding_Record_Type (gnat_entity)))
3544 gnu_type = void_type_node;
3545 else
3546 gnu_type = gnat_to_gnu_type (Corresponding_Record_Type (gnat_entity));
3548 maybe_present = 1;
3549 break;
3551 case E_Label:
3552 gnu_decl = create_label_decl (gnu_entity_id);
3553 break;
3555 case E_Block:
3556 case E_Loop:
3557 /* Nothing at all to do here, so just return an ERROR_MARK and claim
3558 we've already saved it, so we don't try to. */
3559 gnu_decl = error_mark_node;
3560 saved = 1;
3561 break;
3563 default:
3564 gigi_abort (113);
3567 /* If we had a case where we evaluated another type and it might have
3568 defined this one, handle it here. */
3569 if (maybe_present && present_gnu_tree (gnat_entity))
3571 gnu_decl = get_gnu_tree (gnat_entity);
3572 saved = 1;
3575 /* If we are processing a type and there is either no decl for it or
3576 we just made one, do some common processing for the type, such as
3577 handling alignment and possible padding. */
3579 if ((gnu_decl == 0 || this_made_decl) && IN (kind, Type_Kind))
3581 if (Is_Tagged_Type (gnat_entity))
3582 TYPE_ALIGN_OK_P (gnu_type) = 1;
3584 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
3585 TYPE_BY_REFERENCE_P (gnu_type) = 1;
3587 /* ??? Don't set the size for a String_Literal since it is either
3588 confirming or we don't handle it properly (if the low bound is
3589 non-constant). */
3590 if (gnu_size == 0 && kind != E_String_Literal_Subtype)
3591 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
3592 TYPE_DECL, 0, Has_Size_Clause (gnat_entity));
3594 /* If a size was specified, see if we can make a new type of that size
3595 by rearranging the type, for example from a fat to a thin pointer. */
3596 if (gnu_size != 0)
3598 gnu_type
3599 = make_type_from_size (gnu_type, gnu_size,
3600 Has_Biased_Representation (gnat_entity));
3602 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
3603 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
3604 gnu_size = 0;
3607 /* If the alignment hasn't already been processed and this is
3608 not an unconstrained array, see if an alignment is specified.
3609 If not, we pick a default alignment for atomic objects. */
3610 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
3612 else if (Known_Alignment (gnat_entity))
3613 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
3614 TYPE_ALIGN (gnu_type));
3615 else if (Is_Atomic (gnat_entity) && gnu_size == 0
3616 && host_integerp (TYPE_SIZE (gnu_type), 1)
3617 && integer_pow2p (TYPE_SIZE (gnu_type)))
3618 align = MIN (BIGGEST_ALIGNMENT,
3619 tree_low_cst (TYPE_SIZE (gnu_type), 1));
3620 else if (Is_Atomic (gnat_entity) && gnu_size != 0
3621 && host_integerp (gnu_size, 1)
3622 && integer_pow2p (gnu_size))
3623 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
3625 /* See if we need to pad the type. If we did, and made a record,
3626 the name of the new type may be changed. So get it back for
3627 us when we make the new TYPE_DECL below. */
3628 gnu_type = maybe_pad_type (gnu_type, gnu_size, align,
3629 gnat_entity, "PAD", 1, definition, 0);
3630 if (TREE_CODE (gnu_type) == RECORD_TYPE
3631 && TYPE_IS_PADDING_P (gnu_type))
3633 gnu_entity_id = TYPE_NAME (gnu_type);
3634 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
3635 gnu_entity_id = DECL_NAME (gnu_entity_id);
3638 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
3640 /* If we are at global level, GCC will have applied variable_size to
3641 the type, but that won't have done anything. So, if it's not
3642 a constant or self-referential, call elaborate_expression_1 to
3643 make a variable for the size rather than calculating it each time.
3644 Handle both the RM size and the actual size. */
3645 if (global_bindings_p ()
3646 && TYPE_SIZE (gnu_type) != 0
3647 && TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
3648 && ! contains_placeholder_p (TYPE_SIZE (gnu_type)))
3650 if (TREE_CODE (gnu_type) == RECORD_TYPE
3651 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
3652 TYPE_SIZE (gnu_type), 0))
3653 TYPE_ADA_SIZE (gnu_type) = TYPE_SIZE (gnu_type)
3654 = elaborate_expression_1 (gnat_entity, gnat_entity,
3655 TYPE_SIZE (gnu_type),
3656 get_identifier ("SIZE"),
3657 definition, 0);
3658 else if (TREE_CODE (gnu_type) == RECORD_TYPE)
3660 TYPE_ADA_SIZE (gnu_type)
3661 = elaborate_expression_1 (gnat_entity, gnat_entity,
3662 TYPE_ADA_SIZE (gnu_type),
3663 get_identifier ("RM_SIZE"),
3664 definition, 0);
3665 TYPE_SIZE (gnu_type)
3666 = elaborate_expression_1 (gnat_entity, gnat_entity,
3667 TYPE_SIZE (gnu_type),
3668 get_identifier ("SIZE"),
3669 definition, 0);
3670 TYPE_SIZE_UNIT (gnu_type)
3671 = elaborate_expression_1 (gnat_entity, gnat_entity,
3672 TYPE_SIZE_UNIT (gnu_type),
3673 get_identifier ("SIZE_UNIT"),
3674 definition, 0);
3676 else
3678 TYPE_SIZE (gnu_type)
3679 = elaborate_expression_1 (gnat_entity, gnat_entity,
3680 TYPE_SIZE (gnu_type),
3681 get_identifier ("SIZE"),
3682 definition, 0);
3683 TYPE_SIZE_UNIT (gnu_type)
3684 = elaborate_expression_1 (gnat_entity, gnat_entity,
3685 TYPE_SIZE_UNIT (gnu_type),
3686 get_identifier ("SIZE_UNIT"),
3687 definition, 0);
3691 /* If this is a record type or subtype, call elaborate_expression_1 on
3692 any field position. Do this for both global and local types.
3693 Skip any fields that we haven't made trees for to avoid problems with
3694 class wide types. */
3695 if (IN (kind, Record_Kind))
3696 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3697 gnat_temp = Next_Entity (gnat_temp))
3698 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
3700 tree gnu_field = get_gnu_tree (gnat_temp);
3702 if (TREE_CODE (DECL_FIELD_OFFSET (gnu_field)) != INTEGER_CST
3703 && ! contains_placeholder_p (DECL_FIELD_OFFSET (gnu_field)))
3704 DECL_FIELD_OFFSET (gnu_field)
3705 = elaborate_expression_1 (gnat_temp, gnat_temp,
3706 DECL_FIELD_OFFSET (gnu_field),
3707 get_identifier ("OFFSET"),
3708 definition, 0);
3711 gnu_type = build_qualified_type (gnu_type,
3712 (TYPE_QUALS (gnu_type)
3713 | (TYPE_QUAL_VOLATILE
3714 * Is_Volatile (gnat_entity))));
3716 if (Is_Atomic (gnat_entity))
3717 check_ok_for_atomic (gnu_type, gnat_entity, 0);
3719 if (Known_Alignment (gnat_entity))
3720 TYPE_USER_ALIGN (gnu_type) = 1;
3722 if (gnu_decl == 0)
3724 set_lineno (gnat_entity, 0);
3725 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3726 ! Comes_From_Source (gnat_entity),
3727 debug_info_p);
3729 else
3730 TREE_TYPE (gnu_decl) = gnu_type;
3733 if (IN (kind, Type_Kind) && ! TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
3735 gnu_type = TREE_TYPE (gnu_decl);
3737 /* Back-annotate the Alignment of the type if not already in the
3738 tree. Likewise for sizes. */
3739 if (Unknown_Alignment (gnat_entity))
3740 Set_Alignment (gnat_entity,
3741 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
3743 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type) != 0)
3745 /* If the size is self-referential, we annotate the maximum
3746 value of that size. */
3747 tree gnu_size = TYPE_SIZE (gnu_type);
3749 if (contains_placeholder_p (gnu_size))
3750 gnu_size = max_size (gnu_size, 1);
3752 Set_Esize (gnat_entity, annotate_value (gnu_size));
3755 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type) != 0)
3756 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
3759 if (! Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
3760 DECL_ARTIFICIAL (gnu_decl) = 1;
3762 if (! debug_info_p && DECL_P (gnu_decl)
3763 && TREE_CODE (gnu_decl) != FUNCTION_DECL)
3764 DECL_IGNORED_P (gnu_decl) = 1;
3766 /* If this decl is really indirect, adjust it. */
3767 if (TREE_CODE (gnu_decl) == VAR_DECL)
3768 adjust_decl_rtl (gnu_decl);
3770 /* If we haven't already, associate the ..._DECL node that we just made with
3771 the input GNAT entity node. */
3772 if (! saved)
3773 save_gnu_tree (gnat_entity, gnu_decl, 0);
3775 /* If this is an enumeral or floating-point type, we were not able to set
3776 the bounds since they refer to the type. These bounds are always static.
3778 For enumeration types, also write debugging information and declare the
3779 enumeration literal table, if needed. */
3781 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
3782 || (kind == E_Floating_Point_Type && ! Vax_Float (gnat_entity)))
3784 tree gnu_scalar_type = gnu_type;
3786 /* If this is a padded type, we need to use the underlying type. */
3787 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
3788 && TYPE_IS_PADDING_P (gnu_scalar_type))
3789 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
3791 /* If this is a floating point type and we haven't set a floating
3792 point type yet, use this in the evaluation of the bounds. */
3793 if (longest_float_type_node == 0 && kind == E_Floating_Point_Type)
3794 longest_float_type_node = gnu_type;
3796 TYPE_MIN_VALUE (gnu_scalar_type)
3797 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
3798 TYPE_MAX_VALUE (gnu_scalar_type)
3799 = gnat_to_gnu (Type_High_Bound (gnat_entity));
3801 if (kind == E_Enumeration_Type)
3803 TYPE_STUB_DECL (gnu_scalar_type) = gnu_decl;
3805 /* Since this has both a typedef and a tag, avoid outputting
3806 the name twice. */
3807 DECL_ARTIFICIAL (gnu_decl) = 1;
3808 rest_of_type_compilation (gnu_scalar_type, global_bindings_p ());
3812 /* If we deferred processing of incomplete types, re-enable it. If there
3813 were no other disables and we have some to process, do so. */
3814 if (this_deferred && --defer_incomplete_level == 0
3815 && defer_incomplete_list != 0)
3817 struct incomplete *incp = defer_incomplete_list;
3818 struct incomplete *next;
3820 defer_incomplete_list = 0;
3821 for (; incp; incp = next)
3823 next = incp->next;
3825 if (incp->old_type != 0)
3826 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
3827 gnat_to_gnu_type (incp->full_type));
3828 free (incp);
3832 /* If we are not defining this type, see if it's in the incomplete list.
3833 If so, handle that list entry now. */
3834 else if (! definition)
3836 struct incomplete *incp;
3838 for (incp = defer_incomplete_list; incp; incp = incp->next)
3839 if (incp->old_type != 0 && incp->full_type == gnat_entity)
3841 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
3842 TREE_TYPE (gnu_decl));
3843 incp->old_type = 0;
3847 if (this_global)
3848 force_global--;
3850 if (Is_Packed_Array_Type (gnat_entity)
3851 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
3852 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
3853 && ! present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
3854 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
3856 return gnu_decl;
3859 /* Given GNAT_ENTITY, elaborate all expressions that are required to
3860 be elaborated at the point of its definition, but do nothing else. */
3862 void
3863 elaborate_entity (gnat_entity)
3864 Entity_Id gnat_entity;
3866 switch (Ekind (gnat_entity))
3868 case E_Signed_Integer_Subtype:
3869 case E_Modular_Integer_Subtype:
3870 case E_Enumeration_Subtype:
3871 case E_Ordinary_Fixed_Point_Subtype:
3872 case E_Decimal_Fixed_Point_Subtype:
3873 case E_Floating_Point_Subtype:
3875 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
3876 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
3878 /* ??? Tests for avoiding static constaint error expression
3879 is needed until the front stops generating bogus conversions
3880 on bounds of real types. */
3882 if (! Raises_Constraint_Error (gnat_lb))
3883 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
3884 1, 0, Needs_Debug_Info (gnat_entity));
3885 if (! Raises_Constraint_Error (gnat_hb))
3886 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
3887 1, 0, Needs_Debug_Info (gnat_entity));
3888 break;
3891 case E_Record_Type:
3893 Node_Id full_definition = Declaration_Node (gnat_entity);
3894 Node_Id record_definition = Type_Definition (full_definition);
3896 /* If this is a record extension, go a level further to find the
3897 record definition. */
3898 if (Nkind (record_definition) == N_Derived_Type_Definition)
3899 record_definition = Record_Extension_Part (record_definition);
3901 break;
3903 case E_Record_Subtype:
3904 case E_Private_Subtype:
3905 case E_Limited_Private_Subtype:
3906 case E_Record_Subtype_With_Private:
3907 if (Is_Constrained (gnat_entity)
3908 && Has_Discriminants (Base_Type (gnat_entity))
3909 && Present (Discriminant_Constraint (gnat_entity)))
3911 Node_Id gnat_discriminant_expr;
3912 Entity_Id gnat_field;
3914 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
3915 gnat_discriminant_expr
3916 = First_Elmt (Discriminant_Constraint (gnat_entity));
3917 Present (gnat_field);
3918 gnat_field = Next_Discriminant (gnat_field),
3919 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
3920 /* ??? For now, ignore access discriminants. */
3921 if (! Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
3922 elaborate_expression (Node (gnat_discriminant_expr),
3923 gnat_entity,
3924 get_entity_name (gnat_field), 1, 0, 0);
3926 break;
3931 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
3932 any entities on its entity chain similarly. */
3934 void
3935 mark_out_of_scope (gnat_entity)
3936 Entity_Id gnat_entity;
3938 Entity_Id gnat_sub_entity;
3939 unsigned int kind = Ekind (gnat_entity);
3941 /* If this has an entity list, process all in the list. */
3942 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
3943 || IN (kind, Private_Kind)
3944 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
3945 || kind == E_Function || kind == E_Generic_Function
3946 || kind == E_Generic_Package || kind == E_Generic_Procedure
3947 || kind == E_Loop || kind == E_Operator || kind == E_Package
3948 || kind == E_Package_Body || kind == E_Procedure
3949 || kind == E_Record_Type || kind == E_Record_Subtype
3950 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
3951 for (gnat_sub_entity = First_Entity (gnat_entity);
3952 Present (gnat_sub_entity);
3953 gnat_sub_entity = Next_Entity (gnat_sub_entity))
3954 if (Scope (gnat_sub_entity) == gnat_entity
3955 && gnat_sub_entity != gnat_entity)
3956 mark_out_of_scope (gnat_sub_entity);
3958 /* Now clear this if it has been defined, but only do so if it isn't
3959 a subprogram or parameter. We could refine this, but it isn't
3960 worth it. If this is statically allocated, it is supposed to
3961 hang around out of cope. */
3962 if (present_gnu_tree (gnat_entity) && ! Is_Statically_Allocated (gnat_entity)
3963 && kind != E_Procedure && kind != E_Function && ! IN (kind, Formal_Kind))
3965 save_gnu_tree (gnat_entity, NULL_TREE, 1);
3966 save_gnu_tree (gnat_entity, error_mark_node, 1);
3970 /* Return a TREE_LIST describing the substitutions needed to reflect
3971 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
3972 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
3973 of GNAT_SUBTYPE. The substitions can be in any order. TREE_PURPOSE
3974 gives the tree for the discriminant and TREE_VALUES is the replacement
3975 value. They are in the form of operands to substitute_in_expr.
3976 DEFINITION is as in gnat_to_gnu_entity. */
3978 static tree
3979 substitution_list (gnat_subtype, gnat_type, gnu_list, definition)
3980 Entity_Id gnat_subtype;
3981 Entity_Id gnat_type;
3982 tree gnu_list;
3983 int definition;
3985 Entity_Id gnat_discrim;
3986 Node_Id gnat_value;
3988 if (No (gnat_type))
3989 gnat_type = Implementation_Base_Type (gnat_subtype);
3991 if (Has_Discriminants (gnat_type))
3992 for (gnat_discrim = First_Girder_Discriminant (gnat_type),
3993 gnat_value = First_Elmt (Girder_Constraint (gnat_subtype));
3994 Present (gnat_discrim);
3995 gnat_discrim = Next_Girder_Discriminant (gnat_discrim),
3996 gnat_value = Next_Elmt (gnat_value))
3997 /* Ignore access discriminants. */
3998 if (! Is_Access_Type (Etype (Node (gnat_value))))
3999 gnu_list = tree_cons (gnat_to_gnu_entity (gnat_discrim, NULL_TREE, 0),
4000 elaborate_expression
4001 (Node (gnat_value), gnat_subtype,
4002 get_entity_name (gnat_discrim), definition,
4003 1, 0),
4004 gnu_list);
4006 return gnu_list;
4009 /* For the following two functions: for each GNAT entity, the GCC
4010 tree node used as a dummy for that entity, if any. */
4012 static tree *dummy_node_table;
4014 /* Initialize the above table. */
4016 void
4017 init_dummy_type ()
4019 Node_Id gnat_node;
4021 dummy_node_table = (tree *) xmalloc (max_gnat_nodes * sizeof (tree));
4022 ggc_add_tree_root (dummy_node_table, max_gnat_nodes);
4024 for (gnat_node = 0; gnat_node < max_gnat_nodes; gnat_node++)
4025 dummy_node_table[gnat_node] = NULL_TREE;
4027 dummy_node_table -= First_Node_Id;
4030 /* Make a dummy type corresponding to GNAT_TYPE. */
4032 tree
4033 make_dummy_type (gnat_type)
4034 Entity_Id gnat_type;
4036 Entity_Id gnat_underlying;
4037 tree gnu_type;
4039 /* Find a full type for GNAT_TYPE, taking into account any class wide
4040 types. */
4041 if (Is_Class_Wide_Type (gnat_type) && Present (Equivalent_Type (gnat_type)))
4042 gnat_type = Equivalent_Type (gnat_type);
4043 else if (Ekind (gnat_type) == E_Class_Wide_Type)
4044 gnat_type = Root_Type (gnat_type);
4046 for (gnat_underlying = gnat_type;
4047 (IN (Ekind (gnat_underlying), Incomplete_Or_Private_Kind)
4048 && Present (Full_View (gnat_underlying)));
4049 gnat_underlying = Full_View (gnat_underlying))
4052 /* If it there already a dummy type, use that one. Else make one. */
4053 if (dummy_node_table[gnat_underlying])
4054 return dummy_node_table[gnat_underlying];
4056 /* If this is a record, make this a RECORD_TYPE or UNION_TYPE; else make
4057 it a VOID_TYPE. */
4058 if (Is_Record_Type (gnat_underlying))
4059 gnu_type = make_node (Is_Unchecked_Union (gnat_underlying)
4060 ? UNION_TYPE : RECORD_TYPE);
4061 else
4062 gnu_type = make_node (ENUMERAL_TYPE);
4064 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
4065 if (AGGREGATE_TYPE_P (gnu_type))
4066 TYPE_STUB_DECL (gnu_type)
4067 = pushdecl (build_decl (TYPE_DECL, NULL_TREE, gnu_type));
4069 TYPE_DUMMY_P (gnu_type) = 1;
4070 dummy_node_table[gnat_underlying] = gnu_type;
4072 return gnu_type;
4075 /* Return 1 if the size represented by GNU_SIZE can be handled by an
4076 allocation. If STATIC_P is non-zero, consider only what can be
4077 done with a static allocation. */
4079 static int
4080 allocatable_size_p (gnu_size, static_p)
4081 tree gnu_size;
4082 int static_p;
4084 /* If this is not a static allocation, the only case we want to forbid
4085 is an overflowing size. That will be converted into a raise a
4086 Storage_Error. */
4087 if (! static_p)
4088 return ! (TREE_CODE (gnu_size) == INTEGER_CST
4089 && TREE_CONSTANT_OVERFLOW (gnu_size));
4091 /* Otherwise, we need to deal with both variable sizes and constant
4092 sizes that won't fit in a host int. */
4093 return host_integerp (gnu_size, 1);
4096 /* Return a list of attributes for GNAT_ENTITY, if any. */
4098 static struct attrib *
4099 build_attr_list (gnat_entity)
4100 Entity_Id gnat_entity;
4102 struct attrib *attr_list = 0;
4103 Node_Id gnat_temp;
4105 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
4106 gnat_temp = Next_Rep_Item (gnat_temp))
4107 if (Nkind (gnat_temp) == N_Pragma)
4109 struct attrib *attr;
4110 tree gnu_arg0 = 0, gnu_arg1 = 0;
4111 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
4112 enum attr_type etype;
4114 if (Present (gnat_assoc) && Present (First (gnat_assoc))
4115 && Present (Next (First (gnat_assoc)))
4116 && (Nkind (Expression (Next (First (gnat_assoc))))
4117 == N_String_Literal))
4119 gnu_arg0 = get_identifier (TREE_STRING_POINTER
4120 (gnat_to_gnu
4121 (Expression (Next
4122 (First (gnat_assoc))))));
4123 if (Present (Next (Next (First (gnat_assoc))))
4124 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
4125 == N_String_Literal))
4126 gnu_arg1 = get_identifier (TREE_STRING_POINTER
4127 (gnat_to_gnu
4128 (Expression
4129 (Next (Next
4130 (First (gnat_assoc)))))));
4133 switch (Get_Pragma_Id (Chars (gnat_temp)))
4135 case Pragma_Machine_Attribute:
4136 etype = ATTR_MACHINE_ATTRIBUTE;
4137 break;
4139 case Pragma_Linker_Alias:
4140 etype = ATTR_LINK_ALIAS;
4141 break;
4143 case Pragma_Linker_Section:
4144 etype = ATTR_LINK_SECTION;
4145 break;
4147 case Pragma_Weak_External:
4148 etype = ATTR_WEAK_EXTERNAL;
4149 break;
4151 default:
4152 continue;
4155 attr = (struct attrib *) xmalloc (sizeof (struct attrib));
4156 attr->next = attr_list;
4157 attr->type = etype;
4158 attr->name = gnu_arg0;
4159 attr->arg = gnu_arg1;
4160 attr->error_point
4161 = Present (Next (First (gnat_assoc)))
4162 ? Expression (Next (First (gnat_assoc))) : gnat_temp;
4163 attr_list = attr;
4166 return attr_list;
4169 /* Get the unpadded version of a GNAT type. */
4171 tree
4172 get_unpadded_type (gnat_entity)
4173 Entity_Id gnat_entity;
4175 tree type = gnat_to_gnu_type (gnat_entity);
4177 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4178 type = TREE_TYPE (TYPE_FIELDS (type));
4180 return type;
4183 /* Called when we need to protect a variable object using a save_expr. */
4185 tree
4186 maybe_variable (gnu_operand, gnat_node)
4187 tree gnu_operand;
4188 Node_Id gnat_node;
4190 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
4191 || TREE_CODE (gnu_operand) == SAVE_EXPR
4192 || TREE_CODE (gnu_operand) == NULL_EXPR)
4193 return gnu_operand;
4195 /* If we will be generating code, make sure we are at the proper
4196 line number. */
4197 if (! global_bindings_p () && ! TREE_CONSTANT (gnu_operand)
4198 && ! contains_placeholder_p (gnu_operand))
4199 set_lineno (gnat_node, 1);
4201 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
4202 return build1 (UNCONSTRAINED_ARRAY_REF, TREE_TYPE (gnu_operand),
4203 variable_size (TREE_OPERAND (gnu_operand, 0)));
4204 else
4205 return variable_size (gnu_operand);
4208 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
4209 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
4210 return the GCC tree to use for that expression. GNU_NAME is the
4211 qualification to use if an external name is appropriate and DEFINITION is
4212 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
4213 we need a result. Otherwise, we are just elaborating this for
4214 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
4215 purposes even if it isn't needed for code generation. */
4217 static tree
4218 elaborate_expression (gnat_expr, gnat_entity, gnu_name, definition,
4219 need_value, need_debug)
4220 Node_Id gnat_expr;
4221 Entity_Id gnat_entity;
4222 tree gnu_name;
4223 int definition;
4224 int need_value;
4225 int need_debug;
4227 tree gnu_expr;
4229 /* If we already elaborated this expression (e.g., it was involved
4230 in the definition of a private type), use the old value. */
4231 if (present_gnu_tree (gnat_expr))
4232 return get_gnu_tree (gnat_expr);
4234 /* If we don't need a value and this is static or a discriment, we
4235 don't need to do anything. */
4236 else if (! need_value
4237 && (Is_OK_Static_Expression (gnat_expr)
4238 || (Nkind (gnat_expr) == N_Identifier
4239 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
4240 return 0;
4242 /* Otherwise, convert this tree to its GCC equivalant. */
4243 gnu_expr
4244 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
4245 gnu_name, definition, need_debug);
4247 /* Save the expression in case we try to elaborate this entity again.
4248 Since this is not a DECL, don't check it. If this is a constant,
4249 don't save it since GNAT_EXPR might be used more than once. Also,
4250 don't save if it's a discriminant. */
4251 if (! TREE_CONSTANT (gnu_expr) && ! contains_placeholder_p (gnu_expr))
4252 save_gnu_tree (gnat_expr, gnu_expr, 1);
4254 return need_value ? gnu_expr : error_mark_node;
4257 /* Similar, but take a GNU expression. */
4259 static tree
4260 elaborate_expression_1 (gnat_expr, gnat_entity, gnu_expr, gnu_name, definition,
4261 need_debug)
4262 Node_Id gnat_expr;
4263 Entity_Id gnat_entity;
4264 tree gnu_expr;
4265 tree gnu_name;
4266 int definition;
4267 int need_debug;
4269 tree gnu_decl = 0;
4270 tree gnu_inner_expr = gnu_expr;
4271 int expr_variable;
4272 int expr_global = Is_Public (gnat_entity) || global_bindings_p ();
4274 /* Strip any conversions to see if the expression is a readonly variable.
4275 ??? This really should remain readonly, but we have to think about
4276 the typing of the tree here. */
4277 while (TREE_CODE (gnu_inner_expr) == NOP_EXPR
4278 && TREE_CODE (gnu_inner_expr) == CONVERT_EXPR)
4279 gnu_inner_expr = TREE_OPERAND (gnu_inner_expr, 0);
4281 /* In most cases, we won't see a naked FIELD_DECL here because a
4282 discriminant reference will have been replaced with a COMPONENT_REF
4283 when the type is being elaborated. However, there are some cases
4284 involving child types where we will. So convert it to a COMPONENT_REF
4285 here. We have to hope it will be at the highest level of the
4286 expression in these cases. */
4287 if (TREE_CODE (gnu_expr) == FIELD_DECL)
4288 gnu_expr = build (COMPONENT_REF, TREE_TYPE (gnu_expr),
4289 build (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
4290 gnu_expr);
4292 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
4293 that is a constant, make a variable that is initialized to contain the
4294 bound when the package containing the definition is elaborated. If
4295 this entity is defined at top level and a bound or discriminant value
4296 isn't a constant or a reference to a discriminant, replace the bound
4297 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
4298 rely here on the fact that an expression cannot contain both the
4299 discriminant and some other variable. */
4301 expr_variable = (TREE_CODE_CLASS (TREE_CODE (gnu_expr)) != 'c'
4302 && ! (TREE_CODE (gnu_inner_expr) == VAR_DECL
4303 && TREE_READONLY (gnu_inner_expr))
4304 && ! contains_placeholder_p (gnu_expr));
4306 /* If this is a static expression or contains a discriminant, we don't
4307 need the variable for debugging (and can't elaborate anyway if a
4308 discriminant). */
4309 if (need_debug
4310 && (Is_OK_Static_Expression (gnat_expr)
4311 || contains_placeholder_p (gnu_expr)))
4312 need_debug = 0;
4314 /* Now create the variable if we need it. */
4315 if (need_debug || (expr_variable && expr_global))
4317 set_lineno (gnat_entity, ! global_bindings_p ());
4318 gnu_decl
4319 = create_var_decl (create_concat_name (gnat_entity,
4320 IDENTIFIER_POINTER (gnu_name)),
4321 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, 1,
4322 Is_Public (gnat_entity), ! definition, 0, 0);
4325 /* We only need to use this variable if we are in global context since GCC
4326 can do the right thing in the local case. */
4327 if (expr_global && expr_variable)
4328 return gnu_decl;
4329 else
4330 return maybe_variable (gnu_expr, gnat_expr);
4333 /* Create a record type that contains a field of TYPE with a starting bit
4334 position so that it is aligned to ALIGN bits and is SIZE bytes long. */
4336 tree
4337 make_aligning_type (type, align, size)
4338 tree type;
4339 int align;
4340 tree size;
4342 tree record_type = make_node (RECORD_TYPE);
4343 tree place = build (PLACEHOLDER_EXPR, record_type);
4344 tree size_addr_place = convert (sizetype,
4345 build_unary_op (ADDR_EXPR, NULL_TREE,
4346 place));
4347 tree name = TYPE_NAME (type);
4348 tree pos, field;
4350 if (TREE_CODE (name) == TYPE_DECL)
4351 name = DECL_NAME (name);
4353 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
4355 /* The bit position is obtained by "and"ing the alignment minus 1
4356 with the two's complement of the address and multiplying
4357 by the number of bits per unit. Do all this in sizetype. */
4359 pos = size_binop (MULT_EXPR,
4360 convert (bitsizetype,
4361 size_binop (BIT_AND_EXPR,
4362 size_diffop (size_zero_node,
4363 size_addr_place),
4364 ssize_int ((align / BITS_PER_UNIT)
4365 - 1))),
4366 bitsize_unit_node);
4368 field = create_field_decl (get_identifier ("F"), type, record_type,
4369 1, size, pos, 1);
4370 DECL_BIT_FIELD (field) = 0;
4372 finish_record_type (record_type, field, 1, 0);
4373 TYPE_ALIGN (record_type) = BIGGEST_ALIGNMENT;
4374 TYPE_SIZE (record_type)
4375 = size_binop (PLUS_EXPR,
4376 size_binop (MULT_EXPR, convert (bitsizetype, size),
4377 bitsize_unit_node),
4378 bitsize_int (align));
4379 TYPE_SIZE_UNIT (record_type)
4380 = size_binop (PLUS_EXPR, size, size_int (align / BITS_PER_UNIT));
4382 return record_type;
4385 /* TYPE is a RECORD_TYPE with BLKmode that's being used as the field
4386 type of a packed record. See if we can rewrite it as a record that has
4387 a non-BLKmode type, which we can pack tighter. If so, return the
4388 new type. If not, return the original type. */
4390 static tree
4391 make_packable_type (type)
4392 tree type;
4394 tree new_type = make_node (RECORD_TYPE);
4395 tree field_list = NULL_TREE;
4396 tree old_field;
4398 /* Copy the name and flags from the old type to that of the new and set
4399 the alignment to try for an integral type. */
4400 TYPE_NAME (new_type) = TYPE_NAME (type);
4401 TYPE_LEFT_JUSTIFIED_MODULAR_P (new_type)
4402 = TYPE_LEFT_JUSTIFIED_MODULAR_P (type);
4403 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
4405 TYPE_ALIGN (new_type)
4406 = ((HOST_WIDE_INT) 1
4407 << (floor_log2 (tree_low_cst (TYPE_SIZE (type), 1) - 1) + 1));
4409 /* Now copy the fields, keeping the position and size. */
4410 for (old_field = TYPE_FIELDS (type); old_field != 0;
4411 old_field = TREE_CHAIN (old_field))
4413 tree new_field
4414 = create_field_decl (DECL_NAME (old_field), TREE_TYPE (old_field),
4415 new_type, TYPE_PACKED (type),
4416 DECL_SIZE (old_field),
4417 bit_position (old_field),
4418 ! DECL_NONADDRESSABLE_P (old_field));
4420 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
4421 DECL_ORIGINAL_FIELD (new_field)
4422 = (DECL_ORIGINAL_FIELD (old_field) != 0
4423 ? DECL_ORIGINAL_FIELD (old_field) : old_field);
4424 TREE_CHAIN (new_field) = field_list;
4425 field_list = new_field;
4428 finish_record_type (new_type, nreverse (field_list), 1, 1);
4429 return TYPE_MODE (new_type) == BLKmode ? type : new_type;
4432 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
4433 if needed. We have already verified that SIZE and TYPE are large enough.
4435 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
4436 to issue a warning.
4438 IS_USER_TYPE is nonzero if we must be sure we complete the original type.
4440 DEFINITION is nonzero if this type is being defined.
4442 SAME_RM_SIZE is nonzero if the RM_Size of the resulting type is to be
4443 set to its TYPE_SIZE; otherwise, it's set to the RM_Size of the original
4444 type. */
4446 static tree
4447 maybe_pad_type (type, size, align, gnat_entity, name_trailer,
4448 is_user_type, definition, same_rm_size)
4449 tree type;
4450 tree size;
4451 unsigned int align;
4452 Entity_Id gnat_entity;
4453 const char *name_trailer;
4454 int is_user_type;
4455 int definition;
4456 int same_rm_size;
4458 tree orig_size = TYPE_SIZE (type);
4459 tree record;
4460 tree field;
4462 /* If TYPE is a padded type, see if it agrees with any size and alignment
4463 we were given. If so, return the original type. Otherwise, strip
4464 off the padding, since we will either be returning the inner type
4465 or repadding it. If no size or alignment is specified, use that of
4466 the original padded type. */
4468 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4470 if ((size == 0
4471 || operand_equal_p (round_up (size,
4472 MAX (align, TYPE_ALIGN (type))),
4473 round_up (TYPE_SIZE (type),
4474 MAX (align, TYPE_ALIGN (type))),
4476 && (align == 0 || align == TYPE_ALIGN (type)))
4477 return type;
4479 if (size == 0)
4480 size = TYPE_SIZE (type);
4481 if (align == 0)
4482 align = TYPE_ALIGN (type);
4484 type = TREE_TYPE (TYPE_FIELDS (type));
4485 orig_size = TYPE_SIZE (type);
4488 /* If the size is either not being changed or is being made smaller (which
4489 is not done here (and is only valid for bitfields anyway), show the size
4490 isn't changing. Likewise, clear the alignment if it isn't being
4491 changed. Then return if we aren't doing anything. */
4493 if (size != 0
4494 && (operand_equal_p (size, orig_size, 0)
4495 || (TREE_CODE (orig_size) == INTEGER_CST
4496 && tree_int_cst_lt (size, orig_size))))
4497 size = 0;
4499 if (align == TYPE_ALIGN (type))
4500 align = 0;
4502 if (align == 0 && size == 0)
4503 return type;
4505 /* We used to modify the record in place in some cases, but that could
4506 generate incorrect debugging information. So make a new record
4507 type and name. */
4508 record = make_node (RECORD_TYPE);
4510 if (Present (gnat_entity))
4511 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
4513 /* If we were making a type, complete the original type and give it a
4514 name. */
4515 if (is_user_type)
4516 create_type_decl (get_entity_name (gnat_entity), type,
4517 0, ! Comes_From_Source (gnat_entity),
4518 ! (TYPE_NAME (type) != 0
4519 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4520 && DECL_IGNORED_P (TYPE_NAME (type))));
4522 /* If we are changing the alignment and the input type is a record with
4523 BLKmode and a small constant size, try to make a form that has an
4524 integral mode. That might allow this record to have an integral mode,
4525 which will be much more efficient. There is no point in doing this if a
4526 size is specified unless it is also smaller than the biggest alignment
4527 and it is incorrect to do this if the size of the original type is not a
4528 multiple of the alignment. */
4529 if (align != 0
4530 && TREE_CODE (type) == RECORD_TYPE
4531 && TYPE_MODE (type) == BLKmode
4532 && host_integerp (orig_size, 1)
4533 && compare_tree_int (orig_size, BIGGEST_ALIGNMENT) <= 0
4534 && (size == 0
4535 || (TREE_CODE (size) == INTEGER_CST
4536 && compare_tree_int (size, BIGGEST_ALIGNMENT) <= 0))
4537 && tree_low_cst (orig_size, 1) % align == 0)
4538 type = make_packable_type (type);
4540 field = create_field_decl (get_identifier ("F"), type, record, 0,
4541 NULL_TREE, bitsize_zero_node, 1);
4543 DECL_INTERNAL_P (field) = 1;
4544 TYPE_SIZE (record) = size != 0 ? size : orig_size;
4545 TYPE_SIZE_UNIT (record)
4546 = convert (sizetype,
4547 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
4548 bitsize_unit_node));
4549 TYPE_ALIGN (record) = align;
4550 TYPE_IS_PADDING_P (record) = 1;
4551 TYPE_VOLATILE (record)
4552 = Present (gnat_entity) && Is_Volatile (gnat_entity);
4553 finish_record_type (record, field, 1, 0);
4555 /* Keep the RM_Size of the padded record as that of the old record
4556 if requested. */
4557 TYPE_ADA_SIZE (record) = same_rm_size ? size : rm_size (type);
4559 /* Unless debugging information isn't being written for the input type,
4560 write a record that shows what we are a subtype of and also make a
4561 variable that indicates our size, if variable. */
4562 if (TYPE_NAME (record) != 0
4563 && AGGREGATE_TYPE_P (type)
4564 && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
4565 || ! DECL_IGNORED_P (TYPE_NAME (type))))
4567 tree marker = make_node (RECORD_TYPE);
4568 tree name = DECL_NAME (TYPE_NAME (record));
4569 tree orig_name = TYPE_NAME (type);
4571 if (TREE_CODE (orig_name) == TYPE_DECL)
4572 orig_name = DECL_NAME (orig_name);
4574 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
4575 finish_record_type (marker,
4576 create_field_decl (orig_name, integer_type_node,
4577 marker, 0, NULL_TREE, NULL_TREE,
4579 0, 0);
4581 if (size != 0 && TREE_CODE (size) != INTEGER_CST && definition)
4582 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
4583 sizetype, TYPE_SIZE (record), 0, 0, 0, 0,
4587 type = record;
4589 if (TREE_CODE (orig_size) != INTEGER_CST
4590 && contains_placeholder_p (orig_size))
4591 orig_size = max_size (orig_size, 1);
4593 /* If the size was widened explicitly, maybe give a warning. */
4594 if (size != 0 && Present (gnat_entity)
4595 && ! operand_equal_p (size, orig_size, 0)
4596 && ! (TREE_CODE (size) == INTEGER_CST
4597 && TREE_CODE (orig_size) == INTEGER_CST
4598 && tree_int_cst_lt (size, orig_size)))
4600 Node_Id gnat_error_node = Empty;
4602 if (Is_Packed_Array_Type (gnat_entity))
4603 gnat_entity = Associated_Node_For_Itype (gnat_entity);
4605 if ((Ekind (gnat_entity) == E_Component
4606 || Ekind (gnat_entity) == E_Discriminant)
4607 && Present (Component_Clause (gnat_entity)))
4608 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
4609 else if (Present (Size_Clause (gnat_entity)))
4610 gnat_error_node = Expression (Size_Clause (gnat_entity));
4612 /* Generate message only for entities that come from source, since
4613 if we have an entity created by expansion, the message will be
4614 generated for some other corresponding source entity. */
4615 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
4616 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
4617 gnat_entity,
4618 size_diffop (size, orig_size));
4620 else if (*name_trailer == 'C' && ! Is_Internal (gnat_entity))
4621 post_error_ne_tree ("component of& padded{ by ^ bits}?",
4622 gnat_entity, gnat_entity,
4623 size_diffop (size, orig_size));
4626 return type;
4629 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
4630 the value passed against the list of choices. */
4632 tree
4633 choices_to_gnu (operand, choices)
4634 tree operand;
4635 Node_Id choices;
4637 Node_Id choice;
4638 Node_Id gnat_temp;
4639 tree result = integer_zero_node;
4640 tree this_test, low = 0, high = 0, single = 0;
4642 for (choice = First (choices); Present (choice); choice = Next (choice))
4644 switch (Nkind (choice))
4646 case N_Range:
4647 low = gnat_to_gnu (Low_Bound (choice));
4648 high = gnat_to_gnu (High_Bound (choice));
4650 /* There's no good type to use here, so we might as well use
4651 integer_type_node. */
4652 this_test
4653 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4654 build_binary_op (GE_EXPR, integer_type_node,
4655 operand, low),
4656 build_binary_op (LE_EXPR, integer_type_node,
4657 operand, high));
4659 break;
4661 case N_Subtype_Indication:
4662 gnat_temp = Range_Expression (Constraint (choice));
4663 low = gnat_to_gnu (Low_Bound (gnat_temp));
4664 high = gnat_to_gnu (High_Bound (gnat_temp));
4666 this_test
4667 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4668 build_binary_op (GE_EXPR, integer_type_node,
4669 operand, low),
4670 build_binary_op (LE_EXPR, integer_type_node,
4671 operand, high));
4672 break;
4674 case N_Identifier:
4675 case N_Expanded_Name:
4676 /* This represents either a subtype range, an enumeration
4677 literal, or a constant Ekind says which. If an enumeration
4678 literal or constant, fall through to the next case. */
4679 if (Ekind (Entity (choice)) != E_Enumeration_Literal
4680 && Ekind (Entity (choice)) != E_Constant)
4682 tree type = gnat_to_gnu_type (Entity (choice));
4684 low = TYPE_MIN_VALUE (type);
4685 high = TYPE_MAX_VALUE (type);
4687 this_test
4688 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4689 build_binary_op (GE_EXPR, integer_type_node,
4690 operand, low),
4691 build_binary_op (LE_EXPR, integer_type_node,
4692 operand, high));
4693 break;
4695 /* ... fall through ... */
4696 case N_Character_Literal:
4697 case N_Integer_Literal:
4698 single = gnat_to_gnu (choice);
4699 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
4700 single);
4701 break;
4703 case N_Others_Choice:
4704 this_test = integer_one_node;
4705 break;
4707 default:
4708 gigi_abort (114);
4711 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
4712 result, this_test);
4715 return result;
4718 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
4719 placed in GNU_RECORD_TYPE.
4721 PACKED is 1 if the enclosing record is packed and -1 if the enclosing
4722 record has a Component_Alignment of Storage_Unit.
4724 DEFINITION is nonzero if this field is for a record being defined. */
4726 static tree
4727 gnat_to_gnu_field (gnat_field, gnu_record_type, packed, definition)
4728 Entity_Id gnat_field;
4729 tree gnu_record_type;
4730 int packed;
4731 int definition;
4733 tree gnu_field_id = get_entity_name (gnat_field);
4734 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
4735 tree gnu_orig_field_type = gnu_field_type;
4736 tree gnu_pos = 0;
4737 tree gnu_size = 0;
4738 tree gnu_field;
4739 int needs_strict_alignment
4740 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
4741 || Is_Volatile (gnat_field));
4743 /* If this field requires strict alignment pretend it isn't packed. */
4744 if (needs_strict_alignment)
4745 packed = 0;
4747 /* For packed records, this is one of the few occasions on which we use
4748 the official RM size for discrete or fixed-point components, instead
4749 of the normal GNAT size stored in Esize. See description in Einfo:
4750 "Handling of Type'Size Values" for further details. */
4752 if (packed == 1)
4753 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
4754 gnat_field, FIELD_DECL, 0, 1);
4756 if (Known_Static_Esize (gnat_field))
4757 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
4758 gnat_field, FIELD_DECL, 0, 1);
4760 /* If we are packing this record and the field type is also a record
4761 that's BLKmode and with a small constant size, see if we can get a
4762 better form of the type that allows more packing. If we can, show
4763 a size was specified for it if there wasn't one so we know to
4764 make this a bitfield and avoid making things wider. */
4765 if (packed && TREE_CODE (gnu_field_type) == RECORD_TYPE
4766 && TYPE_MODE (gnu_field_type) == BLKmode
4767 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
4768 && compare_tree_int (TYPE_SIZE (gnu_field_type), BIGGEST_ALIGNMENT) <= 0)
4770 gnu_field_type = make_packable_type (gnu_field_type);
4772 if (gnu_field_type != gnu_orig_field_type && gnu_size == 0)
4773 gnu_size = rm_size (gnu_field_type);
4776 if (Present (Component_Clause (gnat_field)))
4778 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
4779 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
4780 gnat_field, FIELD_DECL, 0, 1);
4782 /* Ensure the position does not overlap with the parent subtype,
4783 if there is one. */
4784 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
4786 tree gnu_parent
4787 = gnat_to_gnu_type (Parent_Subtype
4788 (Underlying_Type (Scope (gnat_field))));
4790 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
4791 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
4793 post_error_ne_tree
4794 ("offset of& must be beyond parent{, minimum allowed is ^}",
4795 First_Bit (Component_Clause (gnat_field)), gnat_field,
4796 TYPE_SIZE_UNIT (gnu_parent));
4800 /* If this field needs strict alignment, ensure the record is
4801 sufficiently aligned and that that position and size are
4802 consistent with the alignment. */
4803 if (needs_strict_alignment)
4805 tree gnu_min_size = round_up (rm_size (gnu_field_type),
4806 TYPE_ALIGN (gnu_field_type));
4808 TYPE_ALIGN (gnu_record_type)
4809 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
4811 /* If Atomic, the size must match exactly and if aliased, the size
4812 must not be less than the rounded size. */
4813 if ((Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
4814 && ! operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
4816 post_error_ne_tree
4817 ("atomic field& must be natural size of type{ (^)}",
4818 Last_Bit (Component_Clause (gnat_field)), gnat_field,
4819 TYPE_SIZE (gnu_field_type));
4821 gnu_size = 0;
4824 else if (Is_Aliased (gnat_field)
4825 && gnu_size != 0
4826 && tree_int_cst_lt (gnu_size, gnu_min_size))
4828 post_error_ne_tree
4829 ("size of aliased field& too small{, minimum required is ^}",
4830 Last_Bit (Component_Clause (gnat_field)), gnat_field,
4831 gnu_min_size);
4832 gnu_size = 0;
4835 if (! integer_zerop (size_binop
4836 (TRUNC_MOD_EXPR, gnu_pos,
4837 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
4839 if (Is_Aliased (gnat_field))
4840 post_error_ne_num
4841 ("position of aliased field& must be multiple of ^ bits",
4842 Component_Clause (gnat_field), gnat_field,
4843 TYPE_ALIGN (gnu_field_type));
4845 else if (Is_Volatile (gnat_field))
4846 post_error_ne_num
4847 ("position of volatile field& must be multiple of ^ bits",
4848 First_Bit (Component_Clause (gnat_field)), gnat_field,
4849 TYPE_ALIGN (gnu_field_type));
4851 else if (Strict_Alignment (Etype (gnat_field)))
4852 post_error_ne_num
4853 ("position of & with aliased or tagged components not multiple of ^ bits",
4854 First_Bit (Component_Clause (gnat_field)), gnat_field,
4855 TYPE_ALIGN (gnu_field_type));
4856 else
4857 gigi_abort (124);
4859 gnu_pos = 0;
4862 /* If an error set the size to zero, show we have no position
4863 either. */
4864 if (gnu_size == 0)
4865 gnu_pos = 0;
4868 if (Is_Atomic (gnat_field))
4869 check_ok_for_atomic (gnu_field_type, gnat_field, 0);
4871 if (gnu_pos !=0 && TYPE_MODE (gnu_field_type) == BLKmode
4872 && (! integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
4873 bitsize_unit_node))))
4875 /* Try to see if we can make this a packable type. If we
4876 can, it's OK. */
4877 if (TREE_CODE (gnu_field_type) == RECORD_TYPE)
4878 gnu_field_type = make_packable_type (gnu_field_type);
4880 if (TYPE_MODE (gnu_field_type) == BLKmode)
4882 post_error_ne ("fields of& must start at storage unit boundary",
4883 First_Bit (Component_Clause (gnat_field)),
4884 Etype (gnat_field));
4885 gnu_pos = 0;
4890 /* If the record has rep clauses and this is the tag field, make a rep
4891 clause for it as well. */
4892 else if (Has_Specified_Layout (Scope (gnat_field))
4893 && Chars (gnat_field) == Name_uTag)
4895 gnu_pos = bitsize_zero_node;
4896 gnu_size = TYPE_SIZE (gnu_field_type);
4899 /* We need to make the size the maximum for the type if it is
4900 self-referential and an unconstrained type. */
4901 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
4902 && gnu_size == 0
4903 && ! TREE_CONSTANT (TYPE_SIZE (gnu_field_type))
4904 && contains_placeholder_p (TYPE_SIZE (gnu_field_type))
4905 && ! Is_Constrained (Underlying_Type (Etype (gnat_field))))
4906 gnu_size = max_size (TYPE_SIZE (gnu_field_type), 1);
4908 /* If no size is specified (or if there was an error), don't specify a
4909 position. */
4910 if (gnu_size == 0)
4911 gnu_pos = 0;
4912 else
4914 /* Unless this field is aliased, we can remove any left-justified
4915 modular type since it's only needed in the unchecked conversion
4916 case, which doesn't apply here. */
4917 if (! needs_strict_alignment
4918 && TREE_CODE (gnu_field_type) == RECORD_TYPE
4919 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_field_type))
4920 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
4922 gnu_field_type
4923 = make_type_from_size (gnu_field_type, gnu_size,
4924 Has_Biased_Representation (gnat_field));
4925 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0,
4926 gnat_field, "PAD", 0, definition, 1);
4929 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
4930 && TYPE_CONTAINS_TEMPLATE_P (gnu_field_type))
4931 gigi_abort (118);
4933 set_lineno (gnat_field, 0);
4934 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
4935 packed, gnu_size, gnu_pos,
4936 Is_Aliased (gnat_field));
4938 TREE_THIS_VOLATILE (gnu_field) = Is_Volatile (gnat_field);
4940 if (Ekind (gnat_field) == E_Discriminant)
4941 DECL_DISCRIMINANT_NUMBER (gnu_field)
4942 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
4944 return gnu_field;
4947 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
4948 of GCC trees for fields that are in the record and have already been
4949 processed. When called from gnat_to_gnu_entity during the processing of a
4950 record type definition, the GCC nodes for the discriminants will be on
4951 the chain. The other calls to this function are recursive calls from
4952 itself for the Component_List of a variant and the chain is empty.
4954 PACKED is 1 if this is for a record with "pragma pack" and -1 is this is
4955 for a record type with "pragma component_alignment (storage_unit)".
4957 FINISH_RECORD is nonzero if this call will supply all of the remaining
4958 fields of the record.
4960 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
4961 with a rep clause is to be added. If it is nonzero, that is all that
4962 should be done with such fields.
4964 CANCEL_ALIGNMENT, if nonzero, means the alignment should be zeroed
4965 before laying out the record. This means the alignment only serves
4966 to force fields to be bitfields, but not require the record to be
4967 that aligned. This is used for variants.
4969 ALL_REP, if nonzero, means that a rep clause was found for all the
4970 fields. This simplifies the logic since we know we're not in the mixed
4971 case.
4973 The processing of the component list fills in the chain with all of the
4974 fields of the record and then the record type is finished. */
4976 static void
4977 components_to_record (gnu_record_type, component_list, gnu_field_list, packed,
4978 definition, p_gnu_rep_list, cancel_alignment, all_rep)
4979 tree gnu_record_type;
4980 Node_Id component_list;
4981 tree gnu_field_list;
4982 int packed;
4983 int definition;
4984 tree *p_gnu_rep_list;
4985 int cancel_alignment;
4986 int all_rep;
4988 Node_Id component_decl;
4989 Entity_Id gnat_field;
4990 Node_Id variant_part;
4991 Node_Id variant;
4992 tree gnu_our_rep_list = NULL_TREE;
4993 tree gnu_field, gnu_last;
4994 int layout_with_rep = 0;
4996 /* For each variable within each component declaration create a GCC field
4997 and add it to the list, skipping any pragmas in the list. */
4999 if (Present (Component_Items (component_list)))
5000 for (component_decl = First_Non_Pragma (Component_Items (component_list));
5001 Present (component_decl);
5002 component_decl = Next_Non_Pragma (component_decl))
5004 gnat_field = Defining_Entity (component_decl);
5006 if (Chars (gnat_field) == Name_uParent)
5007 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
5008 else
5010 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
5011 packed, definition);
5013 /* If this is the _Tag field, put it before any discriminants,
5014 instead of after them as is the case for all other fields. */
5015 if (Chars (gnat_field) == Name_uTag)
5016 gnu_field_list = chainon (gnu_field_list, gnu_field);
5017 else
5019 TREE_CHAIN (gnu_field) = gnu_field_list;
5020 gnu_field_list = gnu_field;
5024 save_gnu_tree (gnat_field, gnu_field, 0);
5027 /* At the end of the component list there may be a variant part. */
5028 variant_part = Variant_Part (component_list);
5030 /* If this is an unchecked union, each variant must have exactly one
5031 component, each of which becomes one component of this union. */
5032 if (TREE_CODE (gnu_record_type) == UNION_TYPE && Present (variant_part))
5033 for (variant = First_Non_Pragma (Variants (variant_part));
5034 Present (variant);
5035 variant = Next_Non_Pragma (variant))
5037 component_decl
5038 = First_Non_Pragma (Component_Items (Component_List (variant)));
5039 gnat_field = Defining_Entity (component_decl);
5040 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
5041 definition);
5042 TREE_CHAIN (gnu_field) = gnu_field_list;
5043 gnu_field_list = gnu_field;
5044 save_gnu_tree (gnat_field, gnu_field, 0);
5047 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
5048 mutually exclusive and should go in the same memory. To do this we need
5049 to treat each variant as a record whose elements are created from the
5050 component list for the variant. So here we create the records from the
5051 lists for the variants and put them all into the QUAL_UNION_TYPE. */
5052 else if (Present (variant_part))
5054 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
5055 Node_Id variant;
5056 tree gnu_union_type = make_node (QUAL_UNION_TYPE);
5057 tree gnu_union_field;
5058 tree gnu_variant_list = NULL_TREE;
5059 tree gnu_name = TYPE_NAME (gnu_record_type);
5060 tree gnu_var_name
5061 = concat_id_with_name
5062 (get_identifier (Get_Name_String (Chars (Name (variant_part)))),
5063 "XVN");
5065 if (TREE_CODE (gnu_name) == TYPE_DECL)
5066 gnu_name = DECL_NAME (gnu_name);
5068 TYPE_NAME (gnu_union_type)
5069 = concat_id_with_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
5070 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
5072 for (variant = First_Non_Pragma (Variants (variant_part));
5073 Present (variant);
5074 variant = Next_Non_Pragma (variant))
5076 tree gnu_variant_type = make_node (RECORD_TYPE);
5077 tree gnu_inner_name;
5078 tree gnu_qual;
5080 Get_Variant_Encoding (variant);
5081 gnu_inner_name = get_identifier (Name_Buffer);
5082 TYPE_NAME (gnu_variant_type)
5083 = concat_id_with_name (TYPE_NAME (gnu_union_type),
5084 IDENTIFIER_POINTER (gnu_inner_name));
5086 /* Set the alignment of the inner type in case we need to make
5087 inner objects into bitfields, but then clear it out
5088 so the record actually gets only the alignment required. */
5089 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
5090 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
5091 components_to_record (gnu_variant_type, Component_List (variant),
5092 NULL_TREE, packed, definition,
5093 &gnu_our_rep_list, 1, all_rep);
5095 gnu_qual = choices_to_gnu (gnu_discriminant,
5096 Discrete_Choices (variant));
5098 Set_Present_Expr (variant, annotate_value (gnu_qual));
5099 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
5100 gnu_union_type, 0, 0, 0, 1);
5101 DECL_INTERNAL_P (gnu_field) = 1;
5102 DECL_QUALIFIER (gnu_field) = gnu_qual;
5103 TREE_CHAIN (gnu_field) = gnu_variant_list;
5104 gnu_variant_list = gnu_field;
5107 /* We can delete any empty variants from the end. This may leave none
5108 left. Note we cannot delete variants from anywhere else. */
5109 while (gnu_variant_list != 0
5110 && TYPE_FIELDS (TREE_TYPE (gnu_variant_list)) == 0)
5111 gnu_variant_list = TREE_CHAIN (gnu_variant_list);
5113 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
5114 if (gnu_variant_list != 0)
5116 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
5117 0, 0);
5119 gnu_union_field
5120 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
5121 packed,
5122 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
5123 all_rep ? bitsize_zero_node : 0, 1);
5125 DECL_INTERNAL_P (gnu_union_field) = 1;
5126 TREE_CHAIN (gnu_union_field) = gnu_field_list;
5127 gnu_field_list = gnu_union_field;
5131 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
5132 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
5133 in a separate pass since we want to handle the discriminants but can't
5134 play with them until we've used them in debugging data above.
5136 ??? Note: if we then reorder them, debugging information will be wrong,
5137 but there's nothing that can be done about this at the moment. */
5139 for (gnu_field = gnu_field_list, gnu_last = 0; gnu_field; )
5141 if (DECL_FIELD_OFFSET (gnu_field) != 0)
5143 tree gnu_next = TREE_CHAIN (gnu_field);
5145 if (gnu_last == 0)
5146 gnu_field_list = gnu_next;
5147 else
5148 TREE_CHAIN (gnu_last) = gnu_next;
5150 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
5151 gnu_our_rep_list = gnu_field;
5152 gnu_field = gnu_next;
5154 else
5156 gnu_last = gnu_field;
5157 gnu_field = TREE_CHAIN (gnu_field);
5161 /* If we have any items in our rep'ed field list, it is not the case that all
5162 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
5163 set it and ignore the items. Otherwise, sort the fields by bit position
5164 and put them into their own record if we have any fields without
5165 rep clauses. */
5166 if (gnu_our_rep_list != 0 && p_gnu_rep_list != 0 && ! all_rep)
5167 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
5168 else if (gnu_our_rep_list != 0)
5170 tree gnu_rep_type
5171 = gnu_field_list == 0 ? gnu_record_type : make_node (RECORD_TYPE);
5172 int len = list_length (gnu_our_rep_list);
5173 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
5174 int i;
5176 /* Set DECL_SECTION_NAME to increasing integers so we have a
5177 stable sort. */
5178 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
5179 gnu_field = TREE_CHAIN (gnu_field), i++)
5181 gnu_arr[i] = gnu_field;
5182 DECL_SECTION_NAME (gnu_field) = size_int (i);
5185 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
5187 /* Put the fields in the list in order of increasing position, which
5188 means we start from the end. */
5189 gnu_our_rep_list = NULL_TREE;
5190 for (i = len - 1; i >= 0; i--)
5192 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
5193 gnu_our_rep_list = gnu_arr[i];
5194 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
5195 DECL_SECTION_NAME (gnu_arr[i]) = 0;
5198 if (gnu_field_list != 0)
5200 finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, 0);
5201 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
5202 gnu_record_type, 0, 0, 0, 1);
5203 DECL_INTERNAL_P (gnu_field) = 1;
5204 gnu_field_list = chainon (gnu_field_list, gnu_field);
5206 else
5208 layout_with_rep = 1;
5209 gnu_field_list = nreverse (gnu_our_rep_list);
5213 if (cancel_alignment)
5214 TYPE_ALIGN (gnu_record_type) = 0;
5216 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
5217 layout_with_rep, 0);
5220 /* Called via qsort from the above. Returns -1, 1, depending on the
5221 bit positions and ordinals of the two fields. */
5223 static int
5224 compare_field_bitpos (rt1, rt2)
5225 const PTR rt1;
5226 const PTR rt2;
5228 tree *t1 = (tree *) rt1;
5229 tree *t2 = (tree *) rt2;
5231 if (tree_int_cst_equal (bit_position (*t1), bit_position (*t2)))
5232 return
5233 (tree_int_cst_lt (DECL_SECTION_NAME (*t1), DECL_SECTION_NAME (*t2))
5234 ? -1 : 1);
5235 else if (tree_int_cst_lt (bit_position (*t1), bit_position (*t2)))
5236 return -1;
5237 else
5238 return 1;
5241 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
5242 placed into an Esize, Component_Bit_Offset, or Component_Size value
5243 in the GNAT tree. */
5245 static Uint
5246 annotate_value (gnu_size)
5247 tree gnu_size;
5249 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
5250 TCode tcode;
5251 Node_Ref_Or_Val ops[3];
5252 int i;
5253 int size;
5255 /* If we do not return inside this switch, TCODE will be set to the
5256 code to use for a Create_Node operand and LEN (set above) will be
5257 the number of recursive calls for us to make. */
5259 switch (TREE_CODE (gnu_size))
5261 case INTEGER_CST:
5262 if (TREE_OVERFLOW (gnu_size))
5263 return No_Uint;
5265 /* This may have come from a conversion from some smaller type,
5266 so ensure this is in bitsizetype. */
5267 gnu_size = convert (bitsizetype, gnu_size);
5269 /* For negative values, use NEGATE_EXPR of the supplied value. */
5270 if (tree_int_cst_sgn (gnu_size) < 0)
5272 /* The rediculous code below is to handle the case of the largest
5273 negative integer. */
5274 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
5275 int adjust = 0;
5276 tree temp;
5278 if (TREE_CONSTANT_OVERFLOW (negative_size))
5280 negative_size
5281 = size_binop (MINUS_EXPR, bitsize_zero_node,
5282 size_binop (PLUS_EXPR, gnu_size,
5283 bitsize_one_node));
5284 adjust = 1;
5287 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
5288 if (adjust)
5289 temp = build (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
5291 return annotate_value (temp);
5294 if (! host_integerp (gnu_size, 1))
5295 return No_Uint;
5297 size = tree_low_cst (gnu_size, 1);
5299 /* This peculiar test is to make sure that the size fits in an int
5300 on machines where HOST_WIDE_INT is not "int". */
5301 if (tree_low_cst (gnu_size, 1) == size)
5302 return UI_From_Int (size);
5303 else
5304 return No_Uint;
5306 case COMPONENT_REF:
5307 /* The only case we handle here is a simple discriminant reference. */
5308 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
5309 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
5310 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)) != 0)
5311 return Create_Node (Discrim_Val,
5312 annotate_value (DECL_DISCRIMINANT_NUMBER
5313 (TREE_OPERAND (gnu_size, 1))),
5314 No_Uint, No_Uint);
5315 else
5316 return No_Uint;
5318 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
5319 return annotate_value (TREE_OPERAND (gnu_size, 0));
5321 /* Now just list the operations we handle. */
5322 case COND_EXPR: tcode = Cond_Expr; break;
5323 case PLUS_EXPR: tcode = Plus_Expr; break;
5324 case MINUS_EXPR: tcode = Minus_Expr; break;
5325 case MULT_EXPR: tcode = Mult_Expr; break;
5326 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
5327 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
5328 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
5329 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
5330 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
5331 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
5332 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
5333 case NEGATE_EXPR: tcode = Negate_Expr; break;
5334 case MIN_EXPR: tcode = Min_Expr; break;
5335 case MAX_EXPR: tcode = Max_Expr; break;
5336 case ABS_EXPR: tcode = Abs_Expr; break;
5337 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
5338 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
5339 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
5340 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
5341 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
5342 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
5343 case LT_EXPR: tcode = Lt_Expr; break;
5344 case LE_EXPR: tcode = Le_Expr; break;
5345 case GT_EXPR: tcode = Gt_Expr; break;
5346 case GE_EXPR: tcode = Ge_Expr; break;
5347 case EQ_EXPR: tcode = Eq_Expr; break;
5348 case NE_EXPR: tcode = Ne_Expr; break;
5350 default:
5351 return No_Uint;
5354 /* Now get each of the operands that's relevant for this code. If any
5355 cannot be expressed as a repinfo node, say we can't. */
5356 for (i = 0; i < 3; i++)
5357 ops[i] = No_Uint;
5359 for (i = 0; i < len; i++)
5361 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
5362 if (ops[i] == No_Uint)
5363 return No_Uint;
5366 return Create_Node (tcode, ops[0], ops[1], ops[2]);
5369 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
5370 GCC type, set Component_Bit_Offset and Esize to the position and size
5371 used by Gigi. */
5373 static void
5374 annotate_rep (gnat_entity, gnu_type)
5375 Entity_Id gnat_entity;
5376 tree gnu_type;
5378 tree gnu_list;
5379 tree gnu_entry;
5380 Entity_Id gnat_field;
5382 /* We operate by first making a list of all field and their positions
5383 (we can get the sizes easily at any time) by a recursive call
5384 and then update all the sizes into the tree. */
5385 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
5386 size_zero_node, bitsize_zero_node);
5388 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
5389 gnat_field = Next_Entity (gnat_field))
5390 if ((Ekind (gnat_field) == E_Component
5391 || (Ekind (gnat_field) == E_Discriminant
5392 && ! Is_Unchecked_Union (Scope (gnat_field))))
5393 && 0 != (gnu_entry = purpose_member (gnat_to_gnu_entity (gnat_field,
5394 NULL_TREE, 0),
5395 gnu_list)))
5397 Set_Component_Bit_Offset
5398 (gnat_field,
5399 annotate_value (bit_from_pos
5400 (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
5401 TREE_VALUE (TREE_VALUE (gnu_entry)))));
5403 Set_Esize (gnat_field,
5404 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
5408 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is
5409 the FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the
5410 byte position and TREE_VALUE being the bit position. GNU_POS is to
5411 be added to the position, GNU_BITPOS to the bit position, and GNU_LIST
5412 is the entries so far. */
5414 static tree
5415 compute_field_positions (gnu_type, gnu_list, gnu_pos, gnu_bitpos)
5416 tree gnu_type;
5417 tree gnu_list;
5418 tree gnu_pos;
5419 tree gnu_bitpos;
5421 tree gnu_field;
5422 tree gnu_result = gnu_list;
5424 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
5425 gnu_field = TREE_CHAIN (gnu_field))
5427 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
5428 DECL_FIELD_BIT_OFFSET (gnu_field));
5429 tree gnu_our_pos = size_binop (PLUS_EXPR, gnu_pos,
5430 DECL_FIELD_OFFSET (gnu_field));
5432 gnu_result
5433 = tree_cons (gnu_field,
5434 tree_cons (gnu_our_pos, gnu_our_bitpos, NULL_TREE),
5435 gnu_result);
5437 if (DECL_INTERNAL_P (gnu_field))
5438 gnu_result
5439 = compute_field_positions (TREE_TYPE (gnu_field),
5440 gnu_result, gnu_our_pos, gnu_our_bitpos);
5443 return gnu_result;
5446 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
5447 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
5448 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
5449 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
5450 for the size of a field. COMPONENT_P is true if we are being called
5451 to process the Component_Size of GNAT_OBJECT. This is used for error
5452 message handling and to indicate to use the object size of GNU_TYPE.
5453 ZERO_OK is nonzero if a size of zero is permitted; if ZERO_OK is zero,
5454 it means that a size of zero should be treated as an unspecified size. */
5456 static tree
5457 validate_size (uint_size, gnu_type, gnat_object, kind, component_p, zero_ok)
5458 Uint uint_size;
5459 tree gnu_type;
5460 Entity_Id gnat_object;
5461 enum tree_code kind;
5462 int component_p;
5463 int zero_ok;
5465 Node_Id gnat_error_node;
5466 tree type_size
5467 = kind == VAR_DECL ? TYPE_SIZE (gnu_type) : rm_size (gnu_type);
5468 tree size;
5470 if (type_size != 0 && TREE_CODE (type_size) != INTEGER_CST
5471 && contains_placeholder_p (type_size))
5472 type_size = max_size (type_size, 1);
5474 if (TYPE_FAT_POINTER_P (gnu_type))
5475 type_size = bitsize_int (POINTER_SIZE);
5477 if ((Ekind (gnat_object) == E_Component
5478 || Ekind (gnat_object) == E_Discriminant)
5479 && Present (Component_Clause (gnat_object)))
5480 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
5481 else if (Present (Size_Clause (gnat_object)))
5482 gnat_error_node = Expression (Size_Clause (gnat_object));
5483 else
5484 gnat_error_node = gnat_object;
5486 /* Don't give errors on packed array types; we'll be giving the error on
5487 the type itself soon enough. */
5488 if (Is_Packed_Array_Type (gnat_object))
5489 gnat_error_node = Empty;
5491 /* Get the size as a tree. Return 0 if none was specified, either because
5492 Esize was not Present or if the specified size was zero. Give an error
5493 if a size was specified, but cannot be represented as in sizetype. If
5494 the size is negative, it was a back-annotation of a variable size and
5495 should be treated as not specified. */
5496 if (No (uint_size) || uint_size == No_Uint)
5497 return 0;
5499 size = UI_To_gnu (uint_size, bitsizetype);
5500 if (TREE_OVERFLOW (size))
5502 if (component_p)
5503 post_error_ne ("component size of & is too large",
5504 gnat_error_node, gnat_object);
5505 else
5506 post_error_ne ("size of & is too large", gnat_error_node, gnat_object);
5508 return 0;
5511 /* Ignore a negative size since that corresponds to our back-annotation.
5512 Also ignore a zero size unless a size clause exists. */
5513 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && ! zero_ok))
5514 return 0;
5516 /* The size of objects is always a multiple of a byte. */
5517 if (kind == VAR_DECL
5518 && ! integer_zerop (size_binop (TRUNC_MOD_EXPR, size,
5519 bitsize_unit_node)))
5521 if (component_p)
5522 post_error_ne ("component size for& is not a multiple of Storage_Unit",
5523 gnat_error_node, gnat_object);
5524 else
5525 post_error_ne ("size for& is not a multiple of Storage_Unit",
5526 gnat_error_node, gnat_object);
5527 return 0;
5530 /* If this is an integral type, the front-end has verified the size, so we
5531 need not do it here (which would entail checking against the bounds).
5532 However, if this is an aliased object, it may not be smaller than the
5533 type of the object. */
5534 if (INTEGRAL_TYPE_P (gnu_type) && ! TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
5535 && ! (kind == VAR_DECL && Is_Aliased (gnat_object)))
5536 return size;
5538 /* If the object is a record that contains a template, add the size of
5539 the template to the specified size. */
5540 if (TREE_CODE (gnu_type) == RECORD_TYPE
5541 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
5542 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
5544 /* If the size of the object is a constant, the new size must not be
5545 smaller. */
5546 if (TREE_CODE (type_size) != INTEGER_CST
5547 || TREE_OVERFLOW (type_size)
5548 || tree_int_cst_lt (size, type_size))
5550 if (component_p)
5551 post_error_ne_tree
5552 ("component size for& too small{, minimum allowed is ^}",
5553 gnat_error_node, gnat_object, type_size);
5554 else
5555 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
5556 gnat_error_node, gnat_object, type_size);
5558 if (kind == VAR_DECL && ! component_p
5559 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
5560 && ! tree_int_cst_lt (size, rm_size (gnu_type)))
5561 post_error_ne_tree_2
5562 ("\\size of ^ is not a multiple of alignment (^ bits)",
5563 gnat_error_node, gnat_object, rm_size (gnu_type),
5564 TYPE_ALIGN (gnu_type));
5566 else if (INTEGRAL_TYPE_P (gnu_type))
5567 post_error_ne ("\\size would be legal if & were not aliased!",
5568 gnat_error_node, gnat_object);
5570 return 0;
5573 return size;
5576 /* Similarly, but both validate and process a value of RM_Size. This
5577 routine is only called for types. */
5579 static void
5580 set_rm_size (uint_size, gnu_type, gnat_entity)
5581 Uint uint_size;
5582 tree gnu_type;
5583 Entity_Id gnat_entity;
5585 /* Only give an error if a Value_Size clause was explicitly given.
5586 Otherwise, we'd be duplicating an error on the Size clause. */
5587 Node_Id gnat_attr_node
5588 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
5589 tree old_size = rm_size (gnu_type);
5590 tree size;
5592 /* Get the size as a tree. Do nothing if none was specified, either
5593 because RM_Size was not Present or if the specified size was zero.
5594 Give an error if a size was specified, but cannot be represented as
5595 in sizetype. */
5596 if (No (uint_size) || uint_size == No_Uint)
5597 return;
5599 size = UI_To_gnu (uint_size, bitsizetype);
5600 if (TREE_OVERFLOW (size))
5602 if (Present (gnat_attr_node))
5603 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
5604 gnat_entity);
5606 return;
5609 /* Ignore a negative size since that corresponds to our back-annotation.
5610 Also ignore a zero size unless a size clause exists, a Value_Size
5611 clause exists, or this is an integer type, in which case the
5612 front end will have always set it. */
5613 else if (tree_int_cst_sgn (size) < 0
5614 || (integer_zerop (size) && No (gnat_attr_node)
5615 && ! Has_Size_Clause (gnat_entity)
5616 && ! Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
5617 return;
5619 /* If the old size is self-referential, get the maximum size. */
5620 if (TREE_CODE (old_size) != INTEGER_CST
5621 && contains_placeholder_p (old_size))
5622 old_size = max_size (old_size, 1);
5624 /* If the size of the object is a constant, the new size must not be
5625 smaller (the front end checks this for scalar types). */
5626 if (TREE_CODE (old_size) != INTEGER_CST
5627 || TREE_OVERFLOW (old_size)
5628 || (AGGREGATE_TYPE_P (gnu_type)
5629 && tree_int_cst_lt (size, old_size)))
5631 if (Present (gnat_attr_node))
5632 post_error_ne_tree
5633 ("Value_Size for& too small{, minimum allowed is ^}",
5634 gnat_attr_node, gnat_entity, old_size);
5636 return;
5639 /* Otherwise, set the RM_Size. */
5640 if (TREE_CODE (gnu_type) == INTEGER_TYPE
5641 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
5642 TYPE_RM_SIZE_INT (gnu_type) = size;
5643 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
5644 TYPE_RM_SIZE_ENUM (gnu_type) = size;
5645 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
5646 || TREE_CODE (gnu_type) == UNION_TYPE
5647 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
5648 && ! TYPE_IS_FAT_POINTER_P (gnu_type))
5649 TYPE_ADA_SIZE (gnu_type) = size;
5652 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
5653 If TYPE is the best type, return it. Otherwise, make a new type. We
5654 only support new integral and pointer types. BIASED_P is nonzero if
5655 we are making a biased type. */
5657 static tree
5658 make_type_from_size (type, size_tree, biased_p)
5659 tree type;
5660 tree size_tree;
5661 int biased_p;
5663 tree new_type;
5664 unsigned HOST_WIDE_INT size;
5666 /* If size indicates an error, just return TYPE to avoid propagating the
5667 error. Likewise if it's too large to represent. */
5668 if (size_tree == 0 || ! host_integerp (size_tree, 1))
5669 return type;
5671 size = tree_low_cst (size_tree, 1);
5672 switch (TREE_CODE (type))
5674 case INTEGER_TYPE:
5675 case ENUMERAL_TYPE:
5676 /* Only do something if the type is not already the proper size and is
5677 not a packed array type. */
5678 if (TYPE_PACKED_ARRAY_TYPE_P (type)
5679 || (TYPE_PRECISION (type) == size
5680 && biased_p == (TREE_CODE (type) == INTEGER_CST
5681 && TYPE_BIASED_REPRESENTATION_P (type))))
5682 break;
5684 size = MIN (size, LONG_LONG_TYPE_SIZE);
5685 new_type = make_signed_type (size);
5686 TREE_TYPE (new_type)
5687 = TREE_TYPE (type) != 0 ? TREE_TYPE (type) : type;
5688 TYPE_MIN_VALUE (new_type)
5689 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
5690 TYPE_MAX_VALUE (new_type)
5691 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
5692 TYPE_BIASED_REPRESENTATION_P (new_type)
5693 = ((TREE_CODE (type) == INTEGER_TYPE
5694 && TYPE_BIASED_REPRESENTATION_P (type))
5695 || biased_p);
5696 TREE_UNSIGNED (new_type)
5697 = TREE_UNSIGNED (type) | TYPE_BIASED_REPRESENTATION_P (new_type);
5698 TYPE_RM_SIZE_INT (new_type) = bitsize_int (size);
5699 return new_type;
5701 case RECORD_TYPE:
5702 /* Do something if this is a fat pointer, in which case we
5703 may need to return the thin pointer. */
5704 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
5705 return
5706 build_pointer_type
5707 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
5708 break;
5710 case POINTER_TYPE:
5711 /* Only do something if this is a thin pointer, in which case we
5712 may need to return the fat pointer. */
5713 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
5714 return
5715 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
5717 break;
5719 default:
5720 break;
5723 return type;
5726 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
5727 a type or object whose present alignment is ALIGN. If this alignment is
5728 valid, return it. Otherwise, give an error and return ALIGN. */
5730 static unsigned int
5731 validate_alignment (alignment, gnat_entity, align)
5732 Uint alignment;
5733 Entity_Id gnat_entity;
5734 unsigned int align;
5736 Node_Id gnat_error_node = gnat_entity;
5737 unsigned int new_align;
5739 #ifndef MAX_OFILE_ALIGNMENT
5740 #define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
5741 #endif
5743 if (Present (Alignment_Clause (gnat_entity)))
5744 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
5746 /* Within GCC, an alignment is an integer, so we must make sure a
5747 value is specified that fits in that range. Also, alignments of
5748 more than MAX_OFILE_ALIGNMENT can't be supported. */
5750 if (! UI_Is_In_Int_Range (alignment)
5751 || ((new_align = UI_To_Int (alignment))
5752 > MAX_OFILE_ALIGNMENT / BITS_PER_UNIT))
5753 post_error_ne_num ("largest supported alignment for& is ^",
5754 gnat_error_node, gnat_entity,
5755 MAX_OFILE_ALIGNMENT / BITS_PER_UNIT);
5756 else if (! (Present (Alignment_Clause (gnat_entity))
5757 && From_At_Mod (Alignment_Clause (gnat_entity)))
5758 && new_align * BITS_PER_UNIT < align)
5759 post_error_ne_num ("alignment for& must be at least ^",
5760 gnat_error_node, gnat_entity,
5761 align / BITS_PER_UNIT);
5762 else
5763 align = MAX (align, new_align == 0 ? 1 : new_align * BITS_PER_UNIT);
5765 return align;
5768 /* Verify that OBJECT, a type or decl, is something we can implement
5769 atomically. If not, give an error for GNAT_ENTITY. COMP_P is nonzero
5770 if we require atomic components. */
5772 static void
5773 check_ok_for_atomic (object, gnat_entity, comp_p)
5774 tree object;
5775 Entity_Id gnat_entity;
5776 int comp_p;
5778 Node_Id gnat_error_point = gnat_entity;
5779 Node_Id gnat_node;
5780 enum machine_mode mode;
5781 unsigned int align;
5782 tree size;
5784 /* There are three case of what OBJECT can be. It can be a type, in which
5785 case we take the size, alignment and mode from the type. It can be a
5786 declaration that was indirect, in which case the relevant values are
5787 that of the type being pointed to, or it can be a normal declaration,
5788 in which case the values are of the decl. The code below assumes that
5789 OBJECT is either a type or a decl. */
5790 if (TYPE_P (object))
5792 mode = TYPE_MODE (object);
5793 align = TYPE_ALIGN (object);
5794 size = TYPE_SIZE (object);
5796 else if (DECL_BY_REF_P (object))
5798 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
5799 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
5800 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
5802 else
5804 mode = DECL_MODE (object);
5805 align = DECL_ALIGN (object);
5806 size = DECL_SIZE (object);
5809 /* Consider all floating-point types atomic and any types that that are
5810 represented by integers no wider than a machine word. */
5811 if (GET_MODE_CLASS (mode) == MODE_FLOAT
5812 || ((GET_MODE_CLASS (mode) == MODE_INT
5813 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5814 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
5815 return;
5817 /* For the moment, also allow anything that has an alignment equal
5818 to its size and which is smaller than a word. */
5819 if (TREE_CODE (size) == INTEGER_CST
5820 && compare_tree_int (size, align) == 0
5821 && align <= BITS_PER_WORD)
5822 return;
5824 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
5825 gnat_node = Next_Rep_Item (gnat_node))
5827 if (! comp_p && Nkind (gnat_node) == N_Pragma
5828 && Get_Pragma_Id (Chars (gnat_node)) == Pragma_Atomic)
5829 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
5830 else if (comp_p && Nkind (gnat_node) == N_Pragma
5831 && (Get_Pragma_Id (Chars (gnat_node))
5832 == Pragma_Atomic_Components))
5833 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
5836 if (comp_p)
5837 post_error_ne ("atomic access to component of & cannot be guaranteed",
5838 gnat_error_point, gnat_entity);
5839 else
5840 post_error_ne ("atomic access to & cannot be guaranteed",
5841 gnat_error_point, gnat_entity);
5844 /* Given a type T, a FIELD_DECL F, and a replacement value R,
5845 return a new type with all size expressions that contain F
5846 updated by replacing F with R. This is identical to GCC's
5847 substitute_in_type except that it knows about TYPE_INDEX_TYPE.
5848 If F is NULL_TREE, always make a new RECORD_TYPE, even if nothing has
5849 changed. */
5851 tree
5852 gnat_substitute_in_type (t, f, r)
5853 tree t, f, r;
5855 tree new = t;
5856 tree tem;
5858 switch (TREE_CODE (t))
5860 case INTEGER_TYPE:
5861 case ENUMERAL_TYPE:
5862 case BOOLEAN_TYPE:
5863 case CHAR_TYPE:
5864 if ((TREE_CODE (TYPE_MIN_VALUE (t)) != INTEGER_CST
5865 && contains_placeholder_p (TYPE_MIN_VALUE (t)))
5866 || (TREE_CODE (TYPE_MAX_VALUE (t)) != INTEGER_CST
5867 && contains_placeholder_p (TYPE_MAX_VALUE (t))))
5869 tree low = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
5870 tree high = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
5872 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
5873 return t;
5875 new = build_range_type (TREE_TYPE (t), low, high);
5876 if (TYPE_INDEX_TYPE (t))
5877 TYPE_INDEX_TYPE (new)
5878 = gnat_substitute_in_type (TYPE_INDEX_TYPE (t), f, r);
5879 return new;
5882 return t;
5884 case REAL_TYPE:
5885 if ((TYPE_MIN_VALUE (t) != 0
5886 && TREE_CODE (TYPE_MIN_VALUE (t)) != REAL_CST
5887 && contains_placeholder_p (TYPE_MIN_VALUE (t)))
5888 || (TYPE_MAX_VALUE (t) != 0
5889 && TREE_CODE (TYPE_MAX_VALUE (t)) != REAL_CST
5890 && contains_placeholder_p (TYPE_MAX_VALUE (t))))
5892 tree low = 0, high = 0;
5894 if (TYPE_MIN_VALUE (t))
5895 low = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
5896 if (TYPE_MAX_VALUE (t))
5897 high = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
5899 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
5900 return t;
5902 t = copy_type (t);
5903 TYPE_MIN_VALUE (t) = low;
5904 TYPE_MAX_VALUE (t) = high;
5906 return t;
5908 case COMPLEX_TYPE:
5909 tem = gnat_substitute_in_type (TREE_TYPE (t), f, r);
5910 if (tem == TREE_TYPE (t))
5911 return t;
5913 return build_complex_type (tem);
5915 case OFFSET_TYPE:
5916 case METHOD_TYPE:
5917 case FILE_TYPE:
5918 case SET_TYPE:
5919 case FUNCTION_TYPE:
5920 case LANG_TYPE:
5921 /* Don't know how to do these yet. */
5922 abort ();
5924 case ARRAY_TYPE:
5926 tree component = gnat_substitute_in_type (TREE_TYPE (t), f, r);
5927 tree domain = gnat_substitute_in_type (TYPE_DOMAIN (t), f, r);
5929 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
5930 return t;
5932 new = build_array_type (component, domain);
5933 TYPE_SIZE (new) = 0;
5934 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
5935 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
5936 layout_type (new);
5937 TYPE_ALIGN (new) = TYPE_ALIGN (t);
5938 return new;
5941 case RECORD_TYPE:
5942 case UNION_TYPE:
5943 case QUAL_UNION_TYPE:
5945 tree field;
5946 int changed_field
5947 = (f == NULL_TREE && ! TREE_CONSTANT (TYPE_SIZE (t)));
5948 int field_has_rep = 0;
5949 tree last_field = 0;
5951 tree new = copy_type (t);
5953 /* Start out with no fields, make new fields, and chain them
5954 in. If we haven't actually changed the type of any field,
5955 discard everything we've done and return the old type. */
5957 TYPE_FIELDS (new) = 0;
5958 TYPE_SIZE (new) = 0;
5960 for (field = TYPE_FIELDS (t); field;
5961 field = TREE_CHAIN (field))
5963 tree new_field = copy_node (field);
5965 TREE_TYPE (new_field)
5966 = gnat_substitute_in_type (TREE_TYPE (new_field), f, r);
5968 if (DECL_HAS_REP_P (field) && ! DECL_INTERNAL_P (field))
5969 field_has_rep = 1;
5970 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
5971 changed_field = 1;
5973 /* If this is an internal field and the type of this field is
5974 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
5975 the type just has one element, treat that as the field.
5976 But don't do this if we are processing a QUAL_UNION_TYPE. */
5977 if (TREE_CODE (t) != QUAL_UNION_TYPE
5978 && DECL_INTERNAL_P (new_field)
5979 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
5980 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
5982 if (TYPE_FIELDS (TREE_TYPE (new_field)) == 0)
5983 continue;
5985 if (TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))) == 0)
5987 tree next_new_field
5988 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
5990 /* Make sure omitting the union doesn't change
5991 the layout. */
5992 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
5993 new_field = next_new_field;
5997 DECL_CONTEXT (new_field) = new;
5998 DECL_ORIGINAL_FIELD (new_field)
5999 = DECL_ORIGINAL_FIELD (field) != 0
6000 ? DECL_ORIGINAL_FIELD (field) : field;
6002 /* If the size of the old field was set at a constant,
6003 propagate the size in case the type's size was variable.
6004 (This occurs in the case of a variant or discriminated
6005 record with a default size used as a field of another
6006 record.) */
6007 DECL_SIZE (new_field)
6008 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
6009 ? DECL_SIZE (field) : 0;
6010 DECL_SIZE_UNIT (new_field)
6011 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
6012 ? DECL_SIZE_UNIT (field) : 0;
6014 if (TREE_CODE (t) == QUAL_UNION_TYPE)
6016 tree new_q = substitute_in_expr (DECL_QUALIFIER (field), f, r);
6018 if (new_q != DECL_QUALIFIER (new_field))
6019 changed_field = 1;
6021 /* Do the substitution inside the qualifier and if we find
6022 that this field will not be present, omit it. */
6023 DECL_QUALIFIER (new_field) = new_q;
6025 if (integer_zerop (DECL_QUALIFIER (new_field)))
6026 continue;
6029 if (last_field == 0)
6030 TYPE_FIELDS (new) = new_field;
6031 else
6032 TREE_CHAIN (last_field) = new_field;
6034 last_field = new_field;
6036 /* If this is a qualified type and this field will always be
6037 present, we are done. */
6038 if (TREE_CODE (t) == QUAL_UNION_TYPE
6039 && integer_onep (DECL_QUALIFIER (new_field)))
6040 break;
6043 /* If this used to be a qualified union type, but we now know what
6044 field will be present, make this a normal union. */
6045 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
6046 && (TYPE_FIELDS (new) == 0
6047 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
6048 TREE_SET_CODE (new, UNION_TYPE);
6049 else if (! changed_field)
6050 return t;
6052 if (field_has_rep)
6053 gigi_abort (117);
6055 layout_type (new);
6057 /* If the size was originally a constant use it. */
6058 if (TYPE_SIZE (t) != 0 && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
6059 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
6061 TYPE_SIZE (new) = TYPE_SIZE (t);
6062 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
6063 TYPE_ADA_SIZE (new) = TYPE_ADA_SIZE (t);
6066 return new;
6069 default:
6070 return t;
6074 /* Return the "RM size" of GNU_TYPE. This is the actual number of bits
6075 needed to represent the object. */
6077 tree
6078 rm_size (gnu_type)
6079 tree gnu_type;
6081 /* For integer types, this is the precision. For record types, we store
6082 the size explicitly. For other types, this is just the size. */
6084 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type) != 0)
6085 return TYPE_RM_SIZE (gnu_type);
6086 else if (TREE_CODE (gnu_type) == RECORD_TYPE
6087 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6088 /* Return the rm_size of the actual data plus the size of the template. */
6089 return
6090 size_binop (PLUS_EXPR,
6091 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
6092 DECL_SIZE (TYPE_FIELDS (gnu_type)));
6093 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6094 || TREE_CODE (gnu_type) == UNION_TYPE
6095 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6096 && ! TYPE_IS_FAT_POINTER_P (gnu_type)
6097 && TYPE_ADA_SIZE (gnu_type) != 0)
6098 return TYPE_ADA_SIZE (gnu_type);
6099 else
6100 return TYPE_SIZE (gnu_type);
6103 /* Return an identifier representing the external name to be used for
6104 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
6105 and the specified suffix. */
6107 tree
6108 create_concat_name (gnat_entity, suffix)
6109 Entity_Id gnat_entity;
6110 const char *suffix;
6112 const char *str = (suffix == 0 ? "" : suffix);
6113 String_Template temp = {1, strlen (str)};
6114 Fat_Pointer fp = {str, &temp};
6116 Get_External_Name_With_Suffix (gnat_entity, fp);
6118 return get_identifier (Name_Buffer);
6121 /* Return the name to be used for GNAT_ENTITY. If a type, create a
6122 fully-qualified name, possibly with type information encoding.
6123 Otherwise, return the name. */
6125 tree
6126 get_entity_name (gnat_entity)
6127 Entity_Id gnat_entity;
6129 Get_Encoded_Name (gnat_entity);
6130 return get_identifier (Name_Buffer);
6133 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
6134 string, return a new IDENTIFIER_NODE that is the concatenation of
6135 the name in GNU_ID and SUFFIX. */
6137 tree
6138 concat_id_with_name (gnu_id, suffix)
6139 tree gnu_id;
6140 const char *suffix;
6142 int len = IDENTIFIER_LENGTH (gnu_id);
6144 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id),
6145 IDENTIFIER_LENGTH (gnu_id));
6146 strncpy (Name_Buffer + len, "___", 3);
6147 len += 3;
6148 strcpy (Name_Buffer + len, suffix);
6149 return get_identifier (Name_Buffer);