Remove some compile time warnings about duplicate definitions.
[official-gcc.git] / gcc / ada / decl.c
blob6207cb7605442ad840f19dd7bb26990ab727c3fc
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 if (Unknown_Alignment (gnat_entity))
1065 Set_Alignment (gnat_entity,
1066 UI_From_Int (DECL_ALIGN (gnu_decl) / BITS_PER_UNIT));
1068 if (Unknown_Esize (gnat_entity)
1069 && DECL_SIZE (gnu_decl) != 0)
1071 tree gnu_back_size = DECL_SIZE (gnu_decl);
1073 if (TREE_CODE (TREE_TYPE (gnu_decl)) == RECORD_TYPE
1074 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_decl)))
1075 gnu_back_size
1076 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN
1077 (TYPE_FIELDS (TREE_TYPE (gnu_decl)))));
1079 Set_Esize (gnat_entity, annotate_value (gnu_back_size));
1082 break;
1084 case E_Void:
1085 /* Return a TYPE_DECL for "void" that we previously made. */
1086 gnu_decl = void_type_decl_node;
1087 break;
1089 case E_Enumeration_Type:
1090 /* A special case, for the types Character and Wide_Character in
1091 Standard, we do not list all the literals. So if the literals
1092 are not specified, make this an unsigned type. */
1093 if (No (First_Literal (gnat_entity)))
1095 gnu_type = make_unsigned_type (esize);
1096 break;
1099 /* Normal case of non-character type, or non-Standard character type */
1101 /* Here we have a list of enumeral constants in First_Literal.
1102 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1103 the list to be places into TYPE_FIELDS. Each node in the list
1104 is a TREE_LIST node whose TREE_VALUE is the literal name
1105 and whose TREE_PURPOSE is the value of the literal.
1107 Esize contains the number of bits needed to represent the enumeral
1108 type, Type_Low_Bound also points to the first literal and
1109 Type_High_Bound points to the last literal. */
1111 Entity_Id gnat_literal;
1112 tree gnu_literal_list = NULL_TREE;
1114 if (Is_Unsigned_Type (gnat_entity))
1115 gnu_type = make_unsigned_type (esize);
1116 else
1117 gnu_type = make_signed_type (esize);
1119 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1121 for (gnat_literal = First_Literal (gnat_entity);
1122 Present (gnat_literal);
1123 gnat_literal = Next_Literal (gnat_literal))
1125 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1126 gnu_type);
1127 tree gnu_literal
1128 = create_var_decl (get_entity_name (gnat_literal),
1129 0, gnu_type, gnu_value, 1, 0, 0, 0, 0);
1131 save_gnu_tree (gnat_literal, gnu_literal, 0);
1132 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1133 gnu_value, gnu_literal_list);
1136 TYPE_FIELDS (gnu_type) = nreverse (gnu_literal_list);
1138 /* Note that the bounds are updated at the end of this function
1139 because to avoid an infinite recursion when we get the bounds of
1140 this type, since those bounds are objects of this type. */
1142 break;
1144 case E_Signed_Integer_Type:
1145 case E_Ordinary_Fixed_Point_Type:
1146 case E_Decimal_Fixed_Point_Type:
1147 /* For integer types, just make a signed type the appropriate number
1148 of bits. */
1149 gnu_type = make_signed_type (esize);
1150 break;
1152 case E_Modular_Integer_Type:
1153 /* For modular types, make the unsigned type of the proper number of
1154 bits and then set up the modulus, if required. */
1156 enum machine_mode mode;
1157 tree gnu_modulus;
1158 tree gnu_high = 0;
1160 if (Is_Packed_Array_Type (gnat_entity))
1161 esize = UI_To_Int (RM_Size (gnat_entity));
1163 /* Find the smallest mode at least ESIZE bits wide and make a class
1164 using that mode. */
1166 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1167 GET_MODE_BITSIZE (mode) < esize;
1168 mode = GET_MODE_WIDER_MODE (mode))
1171 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1172 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1173 = Is_Packed_Array_Type (gnat_entity);
1175 /* Get the modulus in this type. If it overflows, assume it is because
1176 it is equal to 2**Esize. Note that there is no overflow checking
1177 done on unsigned type, so we detect the overflow by looking for
1178 a modulus of zero, which is otherwise invalid. */
1179 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1181 if (! integer_zerop (gnu_modulus))
1183 TYPE_MODULAR_P (gnu_type) = 1;
1184 TYPE_MODULUS (gnu_type) = gnu_modulus;
1185 gnu_high = fold (build (MINUS_EXPR, gnu_type, gnu_modulus,
1186 convert (gnu_type, integer_one_node)));
1189 /* If we have to set TYPE_PRECISION different from its natural value,
1190 make a subtype to do do. Likewise if there is a modulus and
1191 it is not one greater than TYPE_MAX_VALUE. */
1192 if (TYPE_PRECISION (gnu_type) != esize
1193 || (TYPE_MODULAR_P (gnu_type)
1194 && ! tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1196 tree gnu_subtype = make_node (INTEGER_TYPE);
1198 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1199 TREE_TYPE (gnu_subtype) = gnu_type;
1200 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1201 TYPE_MAX_VALUE (gnu_subtype)
1202 = TYPE_MODULAR_P (gnu_type)
1203 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1204 TYPE_PRECISION (gnu_subtype) = esize;
1205 TREE_UNSIGNED (gnu_subtype) = 1;
1206 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1207 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1208 = Is_Packed_Array_Type (gnat_entity);
1209 layout_type (gnu_subtype);
1211 gnu_type = gnu_subtype;
1214 break;
1216 case E_Signed_Integer_Subtype:
1217 case E_Enumeration_Subtype:
1218 case E_Modular_Integer_Subtype:
1219 case E_Ordinary_Fixed_Point_Subtype:
1220 case E_Decimal_Fixed_Point_Subtype:
1222 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1223 that we do not want to call build_range_type since we would
1224 like each subtype node to be distinct. This will be important
1225 when memory aliasing is implemented.
1227 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1228 parent type; this fact is used by the arithmetic conversion
1229 functions.
1231 We elaborate the Ancestor_Subtype if it is not in the current
1232 unit and one of our bounds is non-static. We do this to ensure
1233 consistent naming in the case where several subtypes share the same
1234 bounds by always elaborating the first such subtype first, thus
1235 using its name. */
1237 if (definition == 0
1238 && Present (Ancestor_Subtype (gnat_entity))
1239 && ! In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1240 && (! Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1241 || ! Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1242 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1243 gnu_expr, definition);
1245 gnu_type = make_node (INTEGER_TYPE);
1246 if (Is_Packed_Array_Type (gnat_entity))
1249 esize = UI_To_Int (RM_Size (gnat_entity));
1250 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1253 TYPE_PRECISION (gnu_type) = esize;
1254 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1256 TYPE_MIN_VALUE (gnu_type)
1257 = convert (TREE_TYPE (gnu_type),
1258 elaborate_expression (Type_Low_Bound (gnat_entity),
1259 gnat_entity,
1260 get_identifier ("L"), definition, 1,
1261 Needs_Debug_Info (gnat_entity)));
1263 TYPE_MAX_VALUE (gnu_type)
1264 = convert (TREE_TYPE (gnu_type),
1265 elaborate_expression (Type_High_Bound (gnat_entity),
1266 gnat_entity,
1267 get_identifier ("U"), definition, 1,
1268 Needs_Debug_Info (gnat_entity)));
1270 /* One of the above calls might have caused us to be elaborated,
1271 so don't blow up if so. */
1272 if (present_gnu_tree (gnat_entity))
1274 maybe_present = 1;
1275 break;
1278 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1279 = Has_Biased_Representation (gnat_entity);
1281 /* This should be an unsigned type if the lower bound is constant
1282 and non-negative or if the base type is unsigned; a signed type
1283 otherwise. */
1284 TREE_UNSIGNED (gnu_type)
1285 = (TREE_UNSIGNED (TREE_TYPE (gnu_type))
1286 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1287 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1288 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1289 || Is_Unsigned_Type (gnat_entity));
1291 layout_type (gnu_type);
1293 if (Is_Packed_Array_Type (gnat_entity) && BYTES_BIG_ENDIAN)
1295 tree gnu_field_type = gnu_type;
1296 tree gnu_field;
1298 TYPE_RM_SIZE_INT (gnu_field_type)
1299 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1300 gnu_type = make_node (RECORD_TYPE);
1301 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "LJM");
1302 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1303 TYPE_PACKED (gnu_type) = 1;
1304 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1305 gnu_field_type, gnu_type, 1, 0, 0, 1),
1306 finish_record_type (gnu_type, gnu_field, 0, 0);
1307 TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1308 TYPE_ADA_SIZE (gnu_type) = bitsize_int (esize);
1311 break;
1313 case E_Floating_Point_Type:
1314 /* If this is a VAX floating-point type, use an integer of the proper
1315 size. All the operations will be handled with ASM statements. */
1316 if (Vax_Float (gnat_entity))
1318 gnu_type = make_signed_type (esize);
1319 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1320 TYPE_DIGITS_VALUE (gnu_type)
1321 = UI_To_Int (Digits_Value (gnat_entity));
1322 break;
1325 /* The type of the Low and High bounds can be our type if this is
1326 a type from Standard, so set them at the end of the function. */
1327 gnu_type = make_node (REAL_TYPE);
1328 TYPE_PRECISION (gnu_type) = esize;
1329 layout_type (gnu_type);
1330 break;
1332 case E_Floating_Point_Subtype:
1333 if (Vax_Float (gnat_entity))
1335 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1336 break;
1340 enum machine_mode mode;
1342 if (definition == 0
1343 && Present (Ancestor_Subtype (gnat_entity))
1344 && ! In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1345 && (! Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1346 || ! Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1347 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1348 gnu_expr, definition);
1350 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
1351 (GET_MODE_WIDER_MODE (mode) != VOIDmode
1352 && GET_MODE_BITSIZE (GET_MODE_WIDER_MODE (mode)) <= esize);
1353 mode = GET_MODE_WIDER_MODE (mode))
1356 gnu_type = make_node (REAL_TYPE);
1357 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1358 TYPE_PRECISION (gnu_type) = GET_MODE_BITSIZE (mode);
1360 TYPE_MIN_VALUE (gnu_type)
1361 = convert (TREE_TYPE (gnu_type),
1362 elaborate_expression (Type_Low_Bound (gnat_entity),
1363 gnat_entity, get_identifier ("L"),
1364 definition, 1,
1365 Needs_Debug_Info (gnat_entity)));
1367 TYPE_MAX_VALUE (gnu_type)
1368 = convert (TREE_TYPE (gnu_type),
1369 elaborate_expression (Type_High_Bound (gnat_entity),
1370 gnat_entity, get_identifier ("U"),
1371 definition, 1,
1372 Needs_Debug_Info (gnat_entity)));
1374 /* One of the above calls might have caused us to be elaborated,
1375 so don't blow up if so. */
1376 if (present_gnu_tree (gnat_entity))
1378 maybe_present = 1;
1379 break;
1382 layout_type (gnu_type);
1384 break;
1386 /* Array and String Types and Subtypes
1388 Unconstrained array types are represented by E_Array_Type and
1389 constrained array types are represented by E_Array_Subtype. There
1390 are no actual objects of an unconstrained array type; all we have
1391 are pointers to that type.
1393 The following fields are defined on array types and subtypes:
1395 Component_Type Component type of the array.
1396 Number_Dimensions Number of dimensions (an int).
1397 First_Index Type of first index. */
1399 case E_String_Type:
1400 case E_Array_Type:
1402 tree gnu_template_fields = NULL_TREE;
1403 tree gnu_template_type = make_node (RECORD_TYPE);
1404 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1405 tree gnu_fat_type = make_node (RECORD_TYPE);
1406 int ndim = Number_Dimensions (gnat_entity);
1407 int firstdim
1408 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1409 int nextdim
1410 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1411 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1412 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1413 tree gnu_comp_size = 0;
1414 tree gnu_max_size = size_one_node;
1415 tree gnu_max_size_unit;
1416 int index;
1417 Entity_Id gnat_ind_subtype;
1418 Entity_Id gnat_ind_base_subtype;
1419 tree gnu_template_reference;
1420 tree tem;
1422 TYPE_NAME (gnu_template_type)
1423 = create_concat_name (gnat_entity, "XUB");
1424 TYPE_NAME (gnu_fat_type) = create_concat_name (gnat_entity, "XUP");
1425 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1426 TREE_READONLY (gnu_template_type) = 1;
1428 /* Make a node for the array. If we are not defining the array
1429 suppress expanding incomplete types and save the node as the type
1430 for GNAT_ENTITY. */
1431 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1432 if (! definition)
1434 defer_incomplete_level++;
1435 this_deferred = this_made_decl = 1;
1436 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
1437 ! Comes_From_Source (gnat_entity),
1438 debug_info_p);
1439 save_gnu_tree (gnat_entity, gnu_decl, 0);
1440 saved = 1;
1443 /* Build the fat pointer type. Use a "void *" object instead of
1444 a pointer to the array type since we don't have the array type
1445 yet (it will reference the fat pointer via the bounds). */
1446 tem = chainon (chainon (NULL_TREE,
1447 create_field_decl (get_identifier ("P_ARRAY"),
1448 ptr_void_type_node,
1449 gnu_fat_type, 0, 0, 0, 0)),
1450 create_field_decl (get_identifier ("P_BOUNDS"),
1451 gnu_ptr_template,
1452 gnu_fat_type, 0, 0, 0, 0));
1454 /* Make sure we can put this into a register. */
1455 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1456 finish_record_type (gnu_fat_type, tem, 0, 1);
1458 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1459 is the fat pointer. This will be used to access the individual
1460 fields once we build them. */
1461 tem = build (COMPONENT_REF, gnu_ptr_template,
1462 build (PLACEHOLDER_EXPR, gnu_fat_type),
1463 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
1464 gnu_template_reference
1465 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1466 TREE_READONLY (gnu_template_reference) = 1;
1468 /* Now create the GCC type for each index and add the fields for
1469 that index to the template. */
1470 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1471 gnat_ind_base_subtype
1472 = First_Index (Implementation_Base_Type (gnat_entity));
1473 index < ndim && index >= 0;
1474 index += nextdim,
1475 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1476 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1478 char field_name[10];
1479 tree gnu_ind_subtype
1480 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1481 tree gnu_base_subtype
1482 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1483 tree gnu_base_min
1484 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1485 tree gnu_base_max
1486 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1487 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1489 /* Make the FIELD_DECLs for the minimum and maximum of this
1490 type and then make extractions of that field from the
1491 template. */
1492 set_lineno (gnat_entity, 0);
1493 sprintf (field_name, "LB%d", index);
1494 gnu_min_field = create_field_decl (get_identifier (field_name),
1495 gnu_ind_subtype,
1496 gnu_template_type, 0, 0, 0, 0);
1497 field_name[0] = 'U';
1498 gnu_max_field = create_field_decl (get_identifier (field_name),
1499 gnu_ind_subtype,
1500 gnu_template_type, 0, 0, 0, 0);
1502 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1504 /* We can't use build_component_ref here since the template
1505 type isn't complete yet. */
1506 gnu_min = build (COMPONENT_REF, gnu_ind_subtype,
1507 gnu_template_reference, gnu_min_field);
1508 gnu_max = build (COMPONENT_REF, gnu_ind_subtype,
1509 gnu_template_reference, gnu_max_field);
1510 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1512 /* Make a range type with the new ranges, but using
1513 the Ada subtype. Then we convert to sizetype. */
1514 gnu_index_types[index]
1515 = create_index_type (convert (sizetype, gnu_min),
1516 convert (sizetype, gnu_max),
1517 build_range_type (gnu_ind_subtype,
1518 gnu_min, gnu_max));
1519 /* Update the maximum size of the array, in elements. */
1520 gnu_max_size
1521 = size_binop (MULT_EXPR, gnu_max_size,
1522 size_binop (PLUS_EXPR, size_one_node,
1523 size_binop (MINUS_EXPR, gnu_base_max,
1524 gnu_base_min)));
1526 TYPE_NAME (gnu_index_types[index])
1527 = create_concat_name (gnat_entity, field_name);
1530 for (index = 0; index < ndim; index++)
1531 gnu_template_fields
1532 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1534 /* Install all the fields into the template. */
1535 finish_record_type (gnu_template_type, gnu_template_fields, 0, 0);
1536 TREE_READONLY (gnu_template_type) = 1;
1538 /* Now make the array of arrays and update the pointer to the array
1539 in the fat pointer. Note that it is the first field. */
1541 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1543 /* Get and validate any specified Component_Size, but if Packed,
1544 ignore it since the front end will have taken care of it. Also,
1545 allow sizes not a multiple of Storage_Unit if packed. */
1546 gnu_comp_size
1547 = validate_size (Component_Size (gnat_entity), tem,
1548 gnat_entity,
1549 (Is_Bit_Packed_Array (gnat_entity)
1550 ? TYPE_DECL : VAR_DECL), 1,
1551 Has_Component_Size_Clause (gnat_entity));
1553 if (Has_Atomic_Components (gnat_entity))
1554 check_ok_for_atomic (tem, gnat_entity, 1);
1556 /* If the component type is a RECORD_TYPE that has a self-referential
1557 size, use the maxium size. */
1558 if (gnu_comp_size == 0 && TREE_CODE (tem) == RECORD_TYPE
1559 && TREE_CODE (TYPE_SIZE (tem)) != INTEGER_CST
1560 && contains_placeholder_p (TYPE_SIZE (tem)))
1561 gnu_comp_size = max_size (TYPE_SIZE (tem), 1);
1563 if (! Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size != 0)
1565 tem = make_type_from_size (tem, gnu_comp_size, 0);
1566 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1567 "C_PAD", 0, definition, 1);
1570 if (Has_Volatile_Components (gnat_entity))
1571 tem = build_qualified_type (tem,
1572 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1574 /* If Component_Size is not already specified, annotate it with the
1575 size of the component. */
1576 if (Unknown_Component_Size (gnat_entity))
1577 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1579 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1580 size_binop (MULT_EXPR, gnu_max_size,
1581 TYPE_SIZE_UNIT (tem)));
1582 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1583 size_binop (MULT_EXPR,
1584 convert (bitsizetype,
1585 gnu_max_size),
1586 TYPE_SIZE (tem)));
1588 for (index = ndim - 1; index >= 0; index--)
1590 tem = build_array_type (tem, gnu_index_types[index]);
1591 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1592 TYPE_NONALIASED_COMPONENT (tem)
1593 = ! Has_Aliased_Components (gnat_entity);
1596 /* If an alignment is specified, use it if valid. But ignore it for
1597 types that represent the unpacked base type for packed arrays. */
1598 if (No (Packed_Array_Type (gnat_entity))
1599 && Known_Alignment (gnat_entity))
1601 if (No (Alignment (gnat_entity)))
1602 gigi_abort (124);
1604 TYPE_ALIGN (tem)
1605 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1606 TYPE_ALIGN (tem));
1609 TYPE_CONVENTION_FORTRAN_P (tem)
1610 = (Convention (gnat_entity) == Convention_Fortran);
1611 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1613 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1614 corresponding fat pointer. */
1615 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1616 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1617 TYPE_MODE (gnu_type) = BLKmode;
1618 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1619 TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type) = gnu_type;
1621 /* If the maximum size doesn't overflow, use it. */
1622 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1623 && ! TREE_OVERFLOW (gnu_max_size))
1625 TYPE_SIZE (tem)
1626 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1627 TYPE_SIZE_UNIT (tem)
1628 = size_binop (MIN_EXPR, gnu_max_size_unit,
1629 TYPE_SIZE_UNIT (tem));
1632 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1633 tem, 0, ! Comes_From_Source (gnat_entity),
1634 debug_info_p);
1635 rest_of_type_compilation (gnu_fat_type, global_bindings_p ());
1637 /* Create a record type for the object and its template and
1638 set the template at a negative offset. */
1639 tem = build_unc_object_type (gnu_template_type, tem,
1640 create_concat_name (gnat_entity, "XUT"));
1641 DECL_FIELD_OFFSET (TYPE_FIELDS (tem))
1642 = size_binop (MINUS_EXPR, size_zero_node,
1643 byte_position (TREE_CHAIN (TYPE_FIELDS (tem))));
1644 DECL_FIELD_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem))) = size_zero_node;
1645 DECL_FIELD_BIT_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem)))
1646 = bitsize_zero_node;
1647 TYPE_UNCONSTRAINED_ARRAY (tem) = gnu_type;
1648 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1650 /* Give the thin pointer type a name. */
1651 create_type_decl (create_concat_name (gnat_entity, "XUX"),
1652 build_pointer_type (tem), 0,
1653 ! Comes_From_Source (gnat_entity), debug_info_p);
1655 break;
1657 case E_String_Subtype:
1658 case E_Array_Subtype:
1660 /* This is the actual data type for array variables. Multidimensional
1661 arrays are implemented in the gnu tree as arrays of arrays. Note
1662 that for the moment arrays which have sparse enumeration subtypes as
1663 index components create sparse arrays, which is obviously space
1664 inefficient but so much easier to code for now.
1666 Also note that the subtype never refers to the unconstrained
1667 array type, which is somewhat at variance with Ada semantics.
1669 First check to see if this is simply a renaming of the array
1670 type. If so, the result is the array type. */
1672 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1673 if (! Is_Constrained (gnat_entity))
1674 break;
1675 else
1677 int index;
1678 int array_dim = Number_Dimensions (gnat_entity);
1679 int first_dim
1680 = ((Convention (gnat_entity) == Convention_Fortran)
1681 ? array_dim - 1 : 0);
1682 int next_dim
1683 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
1684 Entity_Id gnat_ind_subtype;
1685 Entity_Id gnat_ind_base_subtype;
1686 tree gnu_base_type = gnu_type;
1687 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
1688 tree gnu_comp_size = 0;
1689 tree gnu_max_size = size_one_node;
1690 tree gnu_max_size_unit;
1691 int need_index_type_struct = 0;
1692 int max_overflow = 0;
1694 /* First create the gnu types for each index. Create types for
1695 debugging information to point to the index types if the
1696 are not integer types, have variable bounds, or are
1697 wider than sizetype. */
1699 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1700 gnat_ind_base_subtype
1701 = First_Index (Implementation_Base_Type (gnat_entity));
1702 index < array_dim && index >= 0;
1703 index += next_dim,
1704 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1705 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1707 tree gnu_index_subtype
1708 = get_unpadded_type (Etype (gnat_ind_subtype));
1709 tree gnu_min
1710 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
1711 tree gnu_max
1712 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
1713 tree gnu_base_subtype
1714 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1715 tree gnu_base_min
1716 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1717 tree gnu_base_max
1718 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1719 tree gnu_base_type = get_base_type (gnu_base_subtype);
1720 tree gnu_base_base_min
1721 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
1722 tree gnu_base_base_max
1723 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
1724 tree gnu_high;
1725 tree gnu_this_max;
1727 /* If the minimum and maximum values both overflow in
1728 SIZETYPE, but the difference in the original type
1729 does not overflow in SIZETYPE, ignore the overflow
1730 indications. */
1731 if ((TYPE_PRECISION (gnu_index_subtype)
1732 > TYPE_PRECISION (sizetype))
1733 && TREE_CODE (gnu_min) == INTEGER_CST
1734 && TREE_CODE (gnu_max) == INTEGER_CST
1735 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
1736 && (! TREE_OVERFLOW
1737 (fold (build (MINUS_EXPR, gnu_index_subtype,
1738 TYPE_MAX_VALUE (gnu_index_subtype),
1739 TYPE_MIN_VALUE (gnu_index_subtype))))))
1740 TREE_OVERFLOW (gnu_min) = TREE_OVERFLOW (gnu_max)
1741 = TREE_CONSTANT_OVERFLOW (gnu_min)
1742 = TREE_CONSTANT_OVERFLOW (gnu_max) = 0;
1744 /* Similarly, if the range is null, use bounds of 1..0 for
1745 the sizetype bounds. */
1746 else if ((TYPE_PRECISION (gnu_index_subtype)
1747 > TYPE_PRECISION (sizetype))
1748 && TREE_CODE (gnu_min) == INTEGER_CST
1749 && TREE_CODE (gnu_max) == INTEGER_CST
1750 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
1751 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
1752 TYPE_MIN_VALUE (gnu_index_subtype)))
1753 gnu_min = size_one_node, gnu_max = size_zero_node;
1755 /* Now compute the size of this bound. We need to provide
1756 GCC with an upper bound to use but have to deal with the
1757 "superflat" case. There are three ways to do this. If we
1758 can prove that the array can never be superflat, we can
1759 just use the high bound of the index subtype. If we can
1760 prove that the low bound minus one can't overflow, we
1761 can do this as MAX (hb, lb - 1). Otherwise, we have to use
1762 the expression hb >= lb ? hb : lb - 1. */
1763 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
1765 /* See if the base array type is already flat. If it is, we
1766 are probably compiling an ACVC test, but it will cause the
1767 code below to malfunction if we don't handle it specially. */
1768 if (TREE_CODE (gnu_base_min) == INTEGER_CST
1769 && TREE_CODE (gnu_base_max) == INTEGER_CST
1770 && ! TREE_CONSTANT_OVERFLOW (gnu_base_min)
1771 && ! TREE_CONSTANT_OVERFLOW (gnu_base_max)
1772 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
1773 gnu_high = size_zero_node, gnu_min = size_one_node;
1775 /* If gnu_high is now an integer which overflowed, the array
1776 cannot be superflat. */
1777 else if (TREE_CODE (gnu_high) == INTEGER_CST
1778 && TREE_OVERFLOW (gnu_high))
1779 gnu_high = gnu_max;
1780 else if (TREE_UNSIGNED (gnu_base_subtype)
1781 || TREE_CODE (gnu_high) == INTEGER_CST)
1782 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
1783 else
1784 gnu_high
1785 = build_cond_expr
1786 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
1787 gnu_max, gnu_min),
1788 gnu_max, gnu_high);
1790 gnu_index_type[index]
1791 = create_index_type (gnu_min, gnu_high, gnu_index_subtype);
1793 /* Also compute the maximum size of the array. Here we
1794 see if any constraint on the index type of the base type
1795 can be used in the case of self-referential bound on
1796 the index type of the subtype. We look for a non-"infinite"
1797 and non-self-referential bound from any type involved and
1798 handle each bound separately. */
1800 if ((TREE_CODE (gnu_min) == INTEGER_CST
1801 && ! TREE_OVERFLOW (gnu_min)
1802 && ! operand_equal_p (gnu_min, gnu_base_base_min, 0))
1803 || (TREE_CODE (gnu_min) != INTEGER_CST
1804 && ! contains_placeholder_p (gnu_min)))
1805 gnu_base_min = gnu_min;
1807 if ((TREE_CODE (gnu_max) == INTEGER_CST
1808 && ! TREE_OVERFLOW (gnu_max)
1809 && ! operand_equal_p (gnu_max, gnu_base_base_max, 0))
1810 || (TREE_CODE (gnu_max) != INTEGER_CST
1811 && ! contains_placeholder_p (gnu_max)))
1812 gnu_base_max = gnu_max;
1814 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
1815 && TREE_CONSTANT_OVERFLOW (gnu_base_min))
1816 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
1817 || (TREE_CODE (gnu_base_max) == INTEGER_CST
1818 && TREE_CONSTANT_OVERFLOW (gnu_base_max))
1819 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
1820 max_overflow = 1;
1822 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
1823 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
1825 gnu_this_max
1826 = size_binop (MAX_EXPR,
1827 size_binop (PLUS_EXPR, size_one_node,
1828 size_binop (MINUS_EXPR, gnu_base_max,
1829 gnu_base_min)),
1830 size_zero_node);
1832 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1833 && TREE_CONSTANT_OVERFLOW (gnu_this_max))
1834 max_overflow = 1;
1836 gnu_max_size
1837 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1839 if (! integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
1840 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
1841 != INTEGER_CST)
1842 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
1843 || (TREE_TYPE (gnu_index_subtype) != 0
1844 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
1845 != INTEGER_TYPE))
1846 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
1847 || (TYPE_PRECISION (gnu_index_subtype)
1848 > TYPE_PRECISION (sizetype)))
1849 need_index_type_struct = 1;
1852 /* Then flatten: create the array of arrays. */
1854 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
1856 /* One of the above calls might have caused us to be elaborated,
1857 so don't blow up if so. */
1858 if (present_gnu_tree (gnat_entity))
1860 maybe_present = 1;
1861 break;
1864 /* Get and validate any specified Component_Size, but if Packed,
1865 ignore it since the front end will have taken care of it. Also,
1866 allow sizes not a multiple of Storage_Unit if packed. */
1867 gnu_comp_size
1868 = validate_size (Component_Size (gnat_entity), gnu_type,
1869 gnat_entity,
1870 (Is_Bit_Packed_Array (gnat_entity)
1871 ? TYPE_DECL : VAR_DECL),
1872 1, Has_Component_Size_Clause (gnat_entity));
1874 /* If the component type is a RECORD_TYPE that has a self-referential
1875 size, use the maxium size. */
1876 if (gnu_comp_size == 0 && TREE_CODE (gnu_type) == RECORD_TYPE
1877 && TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
1878 && contains_placeholder_p (TYPE_SIZE (gnu_type)))
1879 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), 1);
1881 if (! Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size != 0)
1883 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, 0);
1884 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
1885 gnat_entity, "C_PAD", 0,
1886 definition, 1);
1889 if (Has_Volatile_Components (Base_Type (gnat_entity)))
1890 gnu_type = build_qualified_type (gnu_type,
1891 (TYPE_QUALS (gnu_type)
1892 | TYPE_QUAL_VOLATILE));
1894 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
1895 TYPE_SIZE_UNIT (gnu_type));
1896 gnu_max_size = size_binop (MULT_EXPR,
1897 convert (bitsizetype, gnu_max_size),
1898 TYPE_SIZE (gnu_type));
1900 /* We don't want any array types shared for two reasons: first,
1901 we want to keep differently-named types distinct; second,
1902 setting TYPE_MULTI_ARRAY_TYPE of one type can clobber
1903 another. */
1904 debug_no_type_hash = 1;
1905 for (index = array_dim - 1; index >= 0; index --)
1907 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
1908 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
1909 TYPE_NONALIASED_COMPONENT (gnu_type)
1910 = ! Has_Aliased_Components (gnat_entity);
1913 /* If we are at file level and this is a multi-dimensional array, we
1914 need to make a variable corresponding to the stride of the
1915 inner dimensions. */
1916 if (global_bindings_p () && array_dim > 1)
1918 tree gnu_str_name = get_identifier ("ST");
1919 tree gnu_arr_type;
1921 for (gnu_arr_type = TREE_TYPE (gnu_type);
1922 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
1923 gnu_arr_type = TREE_TYPE (gnu_arr_type),
1924 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
1926 TYPE_SIZE (gnu_arr_type)
1927 = elaborate_expression_1 (gnat_entity, gnat_entity,
1928 TYPE_SIZE (gnu_arr_type),
1929 gnu_str_name, definition, 0);
1930 TYPE_SIZE_UNIT (gnu_arr_type)
1931 = elaborate_expression_1
1932 (gnat_entity, gnat_entity, TYPE_SIZE_UNIT (gnu_arr_type),
1933 concat_id_with_name (gnu_str_name, "U"), definition, 0);
1937 /* If we need to write out a record type giving the names of
1938 the bounds, do it now. */
1939 if (need_index_type_struct && debug_info_p)
1941 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
1942 tree gnu_field_list = 0;
1943 tree gnu_field;
1945 TYPE_NAME (gnu_bound_rec_type)
1946 = create_concat_name (gnat_entity, "XA");
1948 for (index = array_dim - 1; index >= 0; index--)
1950 tree gnu_type_name
1951 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
1953 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
1954 gnu_type_name = DECL_NAME (gnu_type_name);
1956 gnu_field = create_field_decl (gnu_type_name,
1957 integer_type_node,
1958 gnu_bound_rec_type,
1959 0, NULL_TREE, NULL_TREE, 0);
1960 TREE_CHAIN (gnu_field) = gnu_field_list;
1961 gnu_field_list = gnu_field;
1964 finish_record_type (gnu_bound_rec_type, gnu_field_list, 0, 0);
1967 debug_no_type_hash = 0;
1968 TYPE_CONVENTION_FORTRAN_P (gnu_type)
1969 = (Convention (gnat_entity) == Convention_Fortran);
1971 /* If our size depends on a placeholder and the maximum size doesn't
1972 overflow, use it. */
1973 if (TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
1974 && contains_placeholder_p (TYPE_SIZE (gnu_type))
1975 && ! (TREE_CODE (gnu_max_size) == INTEGER_CST
1976 && TREE_OVERFLOW (gnu_max_size))
1977 && ! max_overflow)
1979 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
1980 TYPE_SIZE (gnu_type));
1981 TYPE_SIZE_UNIT (gnu_type)
1982 = size_binop (MIN_EXPR, gnu_max_size_unit,
1983 TYPE_SIZE_UNIT (gnu_type));
1986 /* Set our alias set to that of our base type. This gives all
1987 array subtypes the same alias set. */
1988 TYPE_ALIAS_SET (gnu_type) = get_alias_set (gnu_base_type);
1989 record_component_aliases (gnu_type);
1992 /* If this is a packed type, make this type the same as the packed
1993 array type, but do some adjusting in the type first. */
1995 if (Present (Packed_Array_Type (gnat_entity)))
1997 Entity_Id gnat_index;
1998 tree gnu_inner_type;
2000 /* First finish the type we had been making so that we output
2001 debugging information for it */
2002 gnu_type = build_qualified_type (gnu_type,
2003 (TYPE_QUALS (gnu_type)
2004 | (TYPE_QUAL_VOLATILE
2005 * Is_Volatile (gnat_entity))));
2006 set_lineno (gnat_entity, 0);
2007 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2008 ! Comes_From_Source (gnat_entity),
2009 debug_info_p);
2010 if (! Comes_From_Source (gnat_entity))
2011 DECL_ARTIFICIAL (gnu_decl) = 1;
2013 /* Save it as our equivalent in case the call below elaborates
2014 this type again. */
2015 save_gnu_tree (gnat_entity, gnu_decl, 0);
2017 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2018 NULL_TREE, 0);
2019 this_made_decl = 1;
2020 gnu_inner_type = gnu_type = TREE_TYPE (gnu_decl);
2021 save_gnu_tree (gnat_entity, NULL_TREE, 0);
2023 if (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2024 && (TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_inner_type)
2025 || TYPE_IS_PADDING_P (gnu_inner_type)))
2026 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2028 /* We need to point the type we just made to our index type so
2029 the actual bounds can be put into a template. */
2031 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2032 && TYPE_ACTUAL_BOUNDS (gnu_inner_type) == 0)
2033 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2034 && ! TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2036 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2038 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2039 If it is, we need to make another type. */
2040 if (TYPE_MODULAR_P (gnu_inner_type))
2042 tree gnu_subtype;
2044 gnu_subtype = make_node (INTEGER_TYPE);
2046 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2047 TYPE_MIN_VALUE (gnu_subtype)
2048 = TYPE_MIN_VALUE (gnu_inner_type);
2049 TYPE_MAX_VALUE (gnu_subtype)
2050 = TYPE_MAX_VALUE (gnu_inner_type);
2051 TYPE_PRECISION (gnu_subtype)
2052 = TYPE_PRECISION (gnu_inner_type);
2053 TREE_UNSIGNED (gnu_subtype)
2054 = TREE_UNSIGNED (gnu_inner_type);
2055 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2056 layout_type (gnu_subtype);
2058 gnu_inner_type = gnu_subtype;
2061 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2064 TYPE_ACTUAL_BOUNDS (gnu_inner_type) = NULL_TREE;
2066 for (gnat_index = First_Index (gnat_entity);
2067 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2068 TYPE_ACTUAL_BOUNDS (gnu_inner_type)
2069 = tree_cons (NULL_TREE,
2070 get_unpadded_type (Etype (gnat_index)),
2071 TYPE_ACTUAL_BOUNDS (gnu_inner_type));
2073 if (Convention (gnat_entity) != Convention_Fortran)
2074 TYPE_ACTUAL_BOUNDS (gnu_inner_type)
2075 = nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type));
2077 if (TREE_CODE (gnu_type) == RECORD_TYPE
2078 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_type))
2079 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2083 /* Abort if packed array with no packed array type field set. */
2084 else if (Is_Packed (gnat_entity))
2085 gigi_abort (107);
2087 break;
2089 case E_String_Literal_Subtype:
2090 /* Create the type for a string literal. */
2092 Entity_Id gnat_full_type
2093 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2094 && Present (Full_View (Etype (gnat_entity)))
2095 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2096 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2097 tree gnu_string_array_type
2098 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2099 tree gnu_string_index_type
2100 = TREE_TYPE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_string_array_type)));
2101 tree gnu_lower_bound
2102 = convert (gnu_string_index_type,
2103 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2104 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2105 tree gnu_length = ssize_int (length - 1);
2106 tree gnu_upper_bound
2107 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2108 gnu_lower_bound,
2109 convert (gnu_string_index_type, gnu_length));
2110 tree gnu_range_type
2111 = build_range_type (gnu_string_index_type,
2112 gnu_lower_bound, gnu_upper_bound);
2113 tree gnu_index_type
2114 = create_index_type (convert (sizetype,
2115 TYPE_MIN_VALUE (gnu_range_type)),
2116 convert (sizetype,
2117 TYPE_MAX_VALUE (gnu_range_type)),
2118 gnu_range_type);
2120 gnu_type
2121 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2122 gnu_index_type);
2124 break;
2126 /* Record Types and Subtypes
2128 The following fields are defined on record types:
2130 Has_Discriminants True if the record has discriminants
2131 First_Discriminant Points to head of list of discriminants
2132 First_Entity Points to head of list of fields
2133 Is_Tagged_Type True if the record is tagged
2135 Implementation of Ada records and discriminated records:
2137 A record type definition is transformed into the equivalent of a C
2138 struct definition. The fields that are the discriminants which are
2139 found in the Full_Type_Declaration node and the elements of the
2140 Component_List found in the Record_Type_Definition node. The
2141 Component_List can be a recursive structure since each Variant of
2142 the Variant_Part of the Component_List has a Component_List.
2144 Processing of a record type definition comprises starting the list of
2145 field declarations here from the discriminants and the calling the
2146 function components_to_record to add the rest of the fields from the
2147 component list and return the gnu type node. The function
2148 components_to_record will call itself recursively as it traverses
2149 the tree. */
2151 case E_Record_Type:
2152 #if 0
2153 if (Has_Complex_Representation (gnat_entity))
2155 gnu_type
2156 = build_complex_type
2157 (get_unpadded_type
2158 (Etype (Defining_Entity
2159 (First (Component_Items
2160 (Component_List
2161 (Type_Definition
2162 (Declaration_Node (gnat_entity)))))))));
2164 /* ??? For now, don't use Complex if the real type is shorter than
2165 a word. */
2166 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (gnu_type)))
2167 >= BITS_PER_WORD)
2168 break;
2170 #endif
2173 Node_Id full_definition = Declaration_Node (gnat_entity);
2174 Node_Id record_definition = Type_Definition (full_definition);
2175 Entity_Id gnat_field;
2176 tree gnu_field;
2177 tree gnu_field_list = NULL_TREE;
2178 tree gnu_get_parent;
2179 int packed = (Is_Packed (gnat_entity) ? 1
2180 : (Component_Alignment (gnat_entity)
2181 == Calign_Storage_Unit) ? -1
2182 : 0);
2183 int has_rep = Has_Specified_Layout (gnat_entity);
2184 int all_rep = has_rep;
2185 int is_extension
2186 = (Is_Tagged_Type (gnat_entity)
2187 && Nkind (record_definition) == N_Derived_Type_Definition);
2189 /* See if all fields have a rep clause. Stop when we find one
2190 that doesn't. */
2191 for (gnat_field = First_Entity (gnat_entity);
2192 Present (gnat_field) && all_rep;
2193 gnat_field = Next_Entity (gnat_field))
2194 if ((Ekind (gnat_field) == E_Component
2195 || Ekind (gnat_field) == E_Discriminant)
2196 && No (Component_Clause (gnat_field)))
2197 all_rep = 0;
2199 /* If this is a record extension, go a level further to find the
2200 record definition. Also, verify we have a Parent_Subtype. */
2201 if (is_extension)
2203 if (! type_annotate_only
2204 || Present (Record_Extension_Part (record_definition)))
2205 record_definition = Record_Extension_Part (record_definition);
2207 if (! type_annotate_only && No (Parent_Subtype (gnat_entity)))
2208 gigi_abort (121);
2211 /* Make a node for the record. If we are not defining the record,
2212 suppress expanding incomplete types and save the node as the type
2213 for GNAT_ENTITY. We use the same RECORD_TYPE as was made
2214 for a dummy type and then show it's no longer a dummy. */
2215 gnu_type = make_dummy_type (gnat_entity);
2216 TYPE_DUMMY_P (gnu_type) = 0;
2217 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL && debug_info_p)
2218 DECL_IGNORED_P (TYPE_NAME (gnu_type)) = 0;
2220 TYPE_ALIGN (gnu_type) = 0;
2221 TYPE_PACKED (gnu_type) = packed != 0 || has_rep;
2223 if (! definition)
2225 defer_incomplete_level++;
2226 this_deferred = 1;
2227 set_lineno (gnat_entity, 0);
2228 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2229 ! Comes_From_Source (gnat_entity),
2230 debug_info_p);
2231 save_gnu_tree (gnat_entity, gnu_decl, 0);
2232 this_made_decl = saved = 1;
2235 /* If both a size and rep clause was specified, put the size in
2236 the record type now so that it can get the proper mode. */
2237 if (has_rep && Known_Esize (gnat_entity))
2238 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2240 /* Always set the alignment here so that it can be used to
2241 set the mode, if it is making the alignment stricter. If
2242 it is invalid, it will be checked again below. If this is to
2243 be Atomic, choose a default alignment of a word. */
2245 if (Known_Alignment (gnat_entity))
2246 TYPE_ALIGN (gnu_type)
2247 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2248 else if (Is_Atomic (gnat_entity))
2249 TYPE_ALIGN (gnu_type) = BITS_PER_WORD;
2251 /* If we have a Parent_Subtype, make a field for the parent. If
2252 this record has rep clauses, force the position to zero. */
2253 if (Present (Parent_Subtype (gnat_entity)))
2255 tree gnu_parent;
2257 /* A major complexity here is that the parent subtype will
2258 reference our discriminants. But those must reference
2259 the parent component of this record. So here we will
2260 initialize each of those components to a COMPONENT_REF.
2261 The first operand of that COMPONENT_REF is another
2262 COMPONENT_REF which will be filled in below, once
2263 the parent type can be safely built. */
2265 gnu_get_parent = build (COMPONENT_REF, void_type_node,
2266 build (PLACEHOLDER_EXPR, gnu_type),
2267 build_decl (FIELD_DECL, NULL_TREE,
2268 NULL_TREE));
2270 if (Has_Discriminants (gnat_entity))
2271 for (gnat_field = First_Girder_Discriminant (gnat_entity);
2272 Present (gnat_field);
2273 gnat_field = Next_Girder_Discriminant (gnat_field))
2274 if (Present (Corresponding_Discriminant (gnat_field)))
2275 save_gnu_tree
2276 (gnat_field,
2277 build (COMPONENT_REF,
2278 get_unpadded_type (Etype (gnat_field)),
2279 gnu_get_parent,
2280 gnat_to_gnu_entity (Corresponding_Discriminant
2281 (gnat_field),
2282 NULL_TREE, 0)),
2285 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_entity));
2287 gnu_field_list
2288 = create_field_decl (get_identifier
2289 (Get_Name_String (Name_uParent)),
2290 gnu_parent, gnu_type, 0,
2291 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2292 has_rep ? bitsize_zero_node : 0, 1);
2293 DECL_INTERNAL_P (gnu_field_list) = 1;
2295 TREE_TYPE (gnu_get_parent) = gnu_parent;
2296 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2299 /* Add the fields for the discriminants into the record. */
2300 if (! Is_Unchecked_Union (gnat_entity)
2301 && Has_Discriminants (gnat_entity))
2302 for (gnat_field = First_Girder_Discriminant (gnat_entity);
2303 Present (gnat_field);
2304 gnat_field = Next_Girder_Discriminant (gnat_field))
2306 /* If this is a record extension and this discriminant
2307 is the renaming of another discriminant, we've already
2308 handled the discriminant above. */
2309 if (Present (Parent_Subtype (gnat_entity))
2310 && Present (Corresponding_Discriminant (gnat_field)))
2311 continue;
2313 gnu_field
2314 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2316 /* Make an expression using a PLACEHOLDER_EXPR from the
2317 FIELD_DECL node just created and link that with the
2318 corresponding GNAT defining identifier. Then add to the
2319 list of fields. */
2320 save_gnu_tree (gnat_field,
2321 build (COMPONENT_REF, TREE_TYPE (gnu_field),
2322 build (PLACEHOLDER_EXPR,
2323 DECL_CONTEXT (gnu_field)),
2324 gnu_field),
2327 TREE_CHAIN (gnu_field) = gnu_field_list;
2328 gnu_field_list = gnu_field;
2331 /* Put the discriminants into the record (backwards), so we can
2332 know the appropriate discriminant to use for the names of the
2333 variants. */
2334 TYPE_FIELDS (gnu_type) = gnu_field_list;
2336 /* Add the listed fields into the record and finish up. */
2337 components_to_record (gnu_type, Component_List (record_definition),
2338 gnu_field_list, packed, definition, 0,
2339 0, all_rep);
2341 TYPE_DUMMY_P (gnu_type) = 0;
2342 TYPE_VOLATILE (gnu_type) = Is_Volatile (gnat_entity);
2343 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2345 /* If this is an extension type, reset the tree for any
2346 inherited discriminants. Also remove the PLACEHOLDER_EXPR
2347 for non-inherited discriminants. */
2348 if (! Is_Unchecked_Union (gnat_entity)
2349 && Has_Discriminants (gnat_entity))
2350 for (gnat_field = First_Girder_Discriminant (gnat_entity);
2351 Present (gnat_field);
2352 gnat_field = Next_Girder_Discriminant (gnat_field))
2354 if (Present (Parent_Subtype (gnat_entity))
2355 && Present (Corresponding_Discriminant (gnat_field)))
2356 save_gnu_tree (gnat_field, NULL_TREE, 0);
2357 else
2359 gnu_field = get_gnu_tree (gnat_field);
2360 save_gnu_tree (gnat_field, NULL_TREE, 0);
2361 save_gnu_tree (gnat_field, TREE_OPERAND (gnu_field, 1), 0);
2365 /* If it is a tagged record force the type to BLKmode to insure
2366 that these objects will always be placed in memory. Do the
2367 same thing for limited record types. */
2369 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2370 TYPE_MODE (gnu_type) = BLKmode;
2372 /* Fill in locations of fields. */
2373 annotate_rep (gnat_entity, gnu_type);
2375 /* If there are any entities in the chain corresponding to
2376 components that we did not elaborate, ensure we elaborate their
2377 types if they are Itypes. */
2378 for (gnat_temp = First_Entity (gnat_entity);
2379 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2380 if ((Ekind (gnat_temp) == E_Component
2381 || Ekind (gnat_temp) == E_Discriminant)
2382 && Is_Itype (Etype (gnat_temp))
2383 && ! present_gnu_tree (gnat_temp))
2384 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2386 break;
2388 case E_Class_Wide_Subtype:
2389 /* If an equivalent type is present, that is what we should use.
2390 Otherwise, fall through to handle this like a record subtype
2391 since it may have constraints. */
2393 if (Present (Equivalent_Type (gnat_entity)))
2395 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
2396 maybe_present = 1;
2397 break;
2400 /* ... fall through ... */
2402 case E_Record_Subtype:
2404 /* If Cloned_Subtype is Present it means this record subtype has
2405 identical layout to that type or subtype and we should use
2406 that GCC type for this one. The front end guarantees that
2407 the component list is shared. */
2408 if (Present (Cloned_Subtype (gnat_entity)))
2410 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2411 NULL_TREE, 0);
2412 maybe_present = 1;
2415 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2416 changing the type, make a new type with each field having the
2417 type of the field in the new subtype but having the position
2418 computed by transforming every discriminant reference according
2419 to the constraints. We don't see any difference between
2420 private and nonprivate type here since derivations from types should
2421 have been deferred until the completion of the private type. */
2422 else
2424 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2425 tree gnu_base_type;
2426 tree gnu_orig_type;
2428 if (! definition)
2429 defer_incomplete_level++, this_deferred = 1;
2431 /* Get the base type initially for its alignment and sizes. But
2432 if it is a padded type, we do all the other work with the
2433 unpadded type. */
2434 gnu_type = gnu_orig_type = gnu_base_type
2435 = gnat_to_gnu_type (gnat_base_type);
2437 if (TREE_CODE (gnu_type) == RECORD_TYPE
2438 && TYPE_IS_PADDING_P (gnu_type))
2439 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2441 if (present_gnu_tree (gnat_entity))
2443 maybe_present = 1;
2444 break;
2447 /* When the type has discriminants, and these discriminants
2448 affect the shape of what it built, factor them in.
2450 If we are making a subtype of an Unchecked_Union (must be an
2451 Itype), just return the type.
2453 We can't just use Is_Constrained because private subtypes without
2454 discriminants of full types with discriminants with default
2455 expressions are Is_Constrained but aren't constrained! */
2457 if (IN (Ekind (gnat_base_type), Record_Kind)
2458 && ! Is_For_Access_Subtype (gnat_entity)
2459 && ! Is_Unchecked_Union (gnat_base_type)
2460 && Is_Constrained (gnat_entity)
2461 && Girder_Constraint (gnat_entity) != No_Elist
2462 && Present (Discriminant_Constraint (gnat_entity)))
2464 Entity_Id gnat_field;
2465 Entity_Id gnat_root_type;
2466 tree gnu_field_list = 0;
2467 tree gnu_pos_list
2468 = compute_field_positions (gnu_orig_type, NULL_TREE,
2469 size_zero_node, bitsize_zero_node);
2470 tree gnu_subst_list
2471 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2472 definition);
2473 tree gnu_temp;
2475 /* If this is a derived type, we may be seeing fields from any
2476 original records, so add those positions and discriminant
2477 substitutions to our lists. */
2478 for (gnat_root_type = gnat_base_type;
2479 Underlying_Type (Etype (gnat_root_type)) != gnat_root_type;
2480 gnat_root_type = Underlying_Type (Etype (gnat_root_type)))
2482 gnu_pos_list
2483 = compute_field_positions
2484 (gnat_to_gnu_type (Etype (gnat_root_type)),
2485 gnu_pos_list, size_zero_node, bitsize_zero_node);
2487 if (Present (Parent_Subtype (gnat_root_type)))
2488 gnu_subst_list
2489 = substitution_list (Parent_Subtype (gnat_root_type),
2490 Empty, gnu_subst_list, definition);
2493 gnu_type = make_node (RECORD_TYPE);
2494 TYPE_NAME (gnu_type) = gnu_entity_id;
2495 TYPE_STUB_DECL (gnu_type)
2496 = pushdecl (build_decl (TYPE_DECL, NULL_TREE, gnu_type));
2497 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2499 for (gnat_field = First_Entity (gnat_entity);
2500 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2501 if (Ekind (gnat_field) == E_Component
2502 || Ekind (gnat_field) == E_Discriminant)
2504 tree gnu_old_field
2505 = gnat_to_gnu_entity
2506 (Original_Record_Component (gnat_field), NULL_TREE, 0);
2507 tree gnu_offset
2508 = TREE_VALUE (purpose_member (gnu_old_field,
2509 gnu_pos_list));
2510 tree gnu_pos = TREE_PURPOSE (gnu_offset);
2511 tree gnu_bitpos = TREE_VALUE (gnu_offset);
2512 tree gnu_field_type
2513 = gnat_to_gnu_type (Etype (gnat_field));
2514 tree gnu_size = TYPE_SIZE (gnu_field_type);
2515 tree gnu_new_pos = 0;
2516 tree gnu_field;
2518 /* If there was a component clause, the field types must be
2519 the same for the type and subtype, so copy the data from
2520 the old field to avoid recomputation here. */
2521 if (Present (Component_Clause
2522 (Original_Record_Component (gnat_field))))
2524 gnu_size = DECL_SIZE (gnu_old_field);
2525 gnu_field_type = TREE_TYPE (gnu_old_field);
2528 /* If this was a bitfield, get the size from the old field.
2529 Also ensure the type can be placed into a bitfield. */
2530 else if (DECL_BIT_FIELD (gnu_old_field))
2532 gnu_size = DECL_SIZE (gnu_old_field);
2533 if (TYPE_MODE (gnu_field_type) == BLKmode
2534 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2535 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2536 gnu_field_type = make_packable_type (gnu_field_type);
2539 if (TREE_CODE (gnu_pos) != INTEGER_CST
2540 && contains_placeholder_p (gnu_pos))
2541 for (gnu_temp = gnu_subst_list;
2542 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2543 gnu_pos = substitute_in_expr (gnu_pos,
2544 TREE_PURPOSE (gnu_temp),
2545 TREE_VALUE (gnu_temp));
2547 /* If the size is now a constant, we can set it as the
2548 size of the field when we make it. Otherwise, we need
2549 to deal with it specially. */
2550 if (TREE_CONSTANT (gnu_pos))
2551 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2553 gnu_field
2554 = create_field_decl
2555 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2556 0, gnu_size, gnu_new_pos,
2557 ! DECL_NONADDRESSABLE_P (gnu_old_field));
2559 if (! TREE_CONSTANT (gnu_pos))
2561 normalize_offset (&gnu_pos, &gnu_bitpos,
2562 DECL_OFFSET_ALIGN (gnu_old_field));
2563 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2564 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
2565 SET_DECL_OFFSET_ALIGN
2566 (gnu_field, DECL_OFFSET_ALIGN (gnu_old_field));
2567 DECL_SIZE (gnu_field) = gnu_size;
2568 DECL_SIZE_UNIT (gnu_field)
2569 = convert (sizetype,
2570 size_binop (CEIL_DIV_EXPR, gnu_size,
2571 bitsize_unit_node));
2572 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2575 DECL_INTERNAL_P (gnu_field)
2576 = DECL_INTERNAL_P (gnu_old_field);
2577 DECL_ORIGINAL_FIELD (gnu_field)
2578 = DECL_ORIGINAL_FIELD (gnu_old_field) != 0
2579 ? DECL_ORIGINAL_FIELD (gnu_old_field) : gnu_old_field;
2580 DECL_DISCRIMINANT_NUMBER (gnu_field)
2581 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2582 TREE_THIS_VOLATILE (gnu_field)
2583 = TREE_THIS_VOLATILE (gnu_old_field);
2584 TREE_CHAIN (gnu_field) = gnu_field_list;
2585 gnu_field_list = gnu_field;
2586 save_gnu_tree (gnat_field, gnu_field, 0);
2589 finish_record_type (gnu_type, nreverse (gnu_field_list), 1, 0);
2591 /* Now set the size, alignment and alias set of the new type to
2592 match that of the old one, doing any substitutions, as
2593 above. */
2594 TYPE_ALIAS_SET (gnu_type) = get_alias_set (gnu_base_type);
2595 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2596 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2597 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
2598 TYPE_ADA_SIZE (gnu_type) = TYPE_ADA_SIZE (gnu_base_type);
2600 if (TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
2601 && contains_placeholder_p (TYPE_SIZE (gnu_type)))
2602 for (gnu_temp = gnu_subst_list;
2603 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2604 TYPE_SIZE (gnu_type)
2605 = substitute_in_expr (TYPE_SIZE (gnu_type),
2606 TREE_PURPOSE (gnu_temp),
2607 TREE_VALUE (gnu_temp));
2609 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_type)) != INTEGER_CST
2610 && contains_placeholder_p (TYPE_SIZE_UNIT (gnu_type)))
2611 for (gnu_temp = gnu_subst_list;
2612 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2613 TYPE_SIZE_UNIT (gnu_type)
2614 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
2615 TREE_PURPOSE (gnu_temp),
2616 TREE_VALUE (gnu_temp));
2618 if (TYPE_ADA_SIZE (gnu_type) != 0
2619 && TREE_CODE (TYPE_ADA_SIZE (gnu_type)) != INTEGER_CST
2620 && contains_placeholder_p (TYPE_ADA_SIZE (gnu_type)))
2621 for (gnu_temp = gnu_subst_list;
2622 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2623 TYPE_ADA_SIZE (gnu_type)
2624 = substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
2625 TREE_PURPOSE (gnu_temp),
2626 TREE_VALUE (gnu_temp));
2628 /* Recompute the mode of this record type now that we know its
2629 actual size. */
2630 compute_record_mode (gnu_type);
2632 /* Fill in locations of fields. */
2633 annotate_rep (gnat_entity, gnu_type);
2636 /* If we've made a new type, record it and make an XVS type to show
2637 what this is a subtype of. Some debuggers require the XVS
2638 type to be output first, so do it in that order. */
2639 if (gnu_type != gnu_orig_type)
2641 if (debug_info_p)
2643 tree gnu_subtype_marker = make_node (RECORD_TYPE);
2644 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
2646 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
2647 gnu_orig_name = DECL_NAME (gnu_orig_name);
2649 TYPE_NAME (gnu_subtype_marker)
2650 = create_concat_name (gnat_entity, "XVS");
2651 finish_record_type (gnu_subtype_marker,
2652 create_field_decl (gnu_orig_name,
2653 integer_type_node,
2654 gnu_subtype_marker,
2655 0, NULL_TREE,
2656 NULL_TREE, 0),
2657 0, 0);
2660 TYPE_VOLATILE (gnu_type) = Is_Volatile (gnat_entity);
2661 TYPE_NAME (gnu_type) = gnu_entity_id;
2662 TYPE_STUB_DECL (gnu_type)
2663 = pushdecl (build_decl (TYPE_DECL, TYPE_NAME (gnu_type),
2664 gnu_type));
2665 DECL_ARTIFICIAL (TYPE_STUB_DECL (gnu_type)) = 1;
2666 DECL_IGNORED_P (TYPE_STUB_DECL (gnu_type)) = ! debug_info_p;
2667 rest_of_type_compilation (gnu_type, global_bindings_p ());
2670 /* Otherwise, go down all the components in the new type and
2671 make them equivalent to those in the base type. */
2672 else
2673 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
2674 gnat_temp = Next_Entity (gnat_temp))
2675 if ((Ekind (gnat_temp) == E_Discriminant
2676 && ! Is_Unchecked_Union (gnat_base_type))
2677 || Ekind (gnat_temp) == E_Component)
2678 save_gnu_tree (gnat_temp,
2679 get_gnu_tree
2680 (Original_Record_Component (gnat_temp)), 0);
2682 break;
2684 case E_Access_Subprogram_Type:
2685 /* If we are not defining this entity, and we have incomplete
2686 entities being processed above us, make a dummy type and
2687 fill it in later. */
2688 if (! definition && defer_incomplete_level != 0)
2690 struct incomplete *p
2691 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2693 gnu_type
2694 = build_pointer_type
2695 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2696 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2697 ! Comes_From_Source (gnat_entity),
2698 debug_info_p);
2699 save_gnu_tree (gnat_entity, gnu_decl, 0);
2700 this_made_decl = saved = 1;
2702 p->old_type = TREE_TYPE (gnu_type);
2703 p->full_type = Directly_Designated_Type (gnat_entity);
2704 p->next = defer_incomplete_list;
2705 defer_incomplete_list = p;
2706 break;
2709 /* ... fall through ... */
2711 case E_Allocator_Type:
2712 case E_Access_Type:
2713 case E_Access_Attribute_Type:
2714 case E_Anonymous_Access_Type:
2715 case E_General_Access_Type:
2717 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
2718 Entity_Id gnat_desig_full
2719 = ((IN (Ekind (Etype (gnat_desig_type)),
2720 Incomplete_Or_Private_Kind))
2721 ? Full_View (gnat_desig_type) : 0);
2722 /* We want to know if we'll be seeing the freeze node for any
2723 incomplete type we may be pointing to. */
2724 int in_main_unit
2725 = (Present (gnat_desig_full)
2726 ? In_Extended_Main_Code_Unit (gnat_desig_full)
2727 : In_Extended_Main_Code_Unit (gnat_desig_type));
2728 int got_fat_p = 0;
2729 int made_dummy = 0;
2730 tree gnu_desig_type = 0;
2732 if (No (gnat_desig_full)
2733 && (Ekind (gnat_desig_type) == E_Class_Wide_Type
2734 || (Ekind (gnat_desig_type) == E_Class_Wide_Subtype
2735 && Present (Equivalent_Type (gnat_desig_type)))))
2737 if (Present (Equivalent_Type (gnat_desig_type)))
2739 gnat_desig_full = Equivalent_Type (gnat_desig_type);
2740 if (IN (Ekind (gnat_desig_full), Incomplete_Or_Private_Kind))
2741 gnat_desig_full = Full_View (gnat_desig_full);
2743 else if (IN (Ekind (Root_Type (gnat_desig_type)),
2744 Incomplete_Or_Private_Kind))
2745 gnat_desig_full = Full_View (Root_Type (gnat_desig_type));
2748 if (Present (gnat_desig_full) && Is_Concurrent_Type (gnat_desig_full))
2749 gnat_desig_full = Corresponding_Record_Type (gnat_desig_full);
2751 /* If either the designated type or its full view is an
2752 unconstrained array subtype, replace it with the type it's a
2753 subtype of. This avoids problems with multiple copies of
2754 unconstrained array types. */
2755 if (Ekind (gnat_desig_type) == E_Array_Subtype
2756 && ! Is_Constrained (gnat_desig_type))
2757 gnat_desig_type = Etype (gnat_desig_type);
2758 if (Present (gnat_desig_full)
2759 && Ekind (gnat_desig_full) == E_Array_Subtype
2760 && ! Is_Constrained (gnat_desig_full))
2761 gnat_desig_full = Etype (gnat_desig_full);
2763 /* If we are pointing to an incomplete type whose completion is an
2764 unconstrained array, make a fat pointer type instead of a pointer
2765 to VOID. The two types in our fields will be pointers to VOID and
2766 will be replaced in update_pointer_to. Similiarly, if the type
2767 itself is a dummy type or an unconstrained array. Also make
2768 a dummy TYPE_OBJECT_RECORD_TYPE in case we have any thin
2769 pointers to it. */
2771 if ((Present (gnat_desig_full)
2772 && Is_Array_Type (gnat_desig_full)
2773 && ! Is_Constrained (gnat_desig_full))
2774 || (present_gnu_tree (gnat_desig_type)
2775 && TYPE_IS_DUMMY_P (TREE_TYPE
2776 (get_gnu_tree (gnat_desig_type)))
2777 && Is_Array_Type (gnat_desig_type)
2778 && ! Is_Constrained (gnat_desig_type))
2779 || (present_gnu_tree (gnat_desig_type)
2780 && (TREE_CODE (TREE_TYPE (get_gnu_tree (gnat_desig_type)))
2781 == UNCONSTRAINED_ARRAY_TYPE)
2782 && (TYPE_POINTER_TO (TREE_TYPE
2783 (get_gnu_tree (gnat_desig_type)))
2784 == 0))
2785 || (No (gnat_desig_full) && ! in_main_unit
2786 && defer_incomplete_level != 0
2787 && ! present_gnu_tree (gnat_desig_type)
2788 && Is_Array_Type (gnat_desig_type)
2789 && ! Is_Constrained (gnat_desig_type)))
2791 tree gnu_old
2792 = (present_gnu_tree (gnat_desig_type)
2793 ? gnat_to_gnu_type (gnat_desig_type)
2794 : make_dummy_type (gnat_desig_type));
2795 tree fields;
2797 /* Show the dummy we get will be a fat pointer. */
2798 got_fat_p = made_dummy = 1;
2800 /* If the call above got something that has a pointer, that
2801 pointer is our type. This could have happened either
2802 because the type was elaborated or because somebody
2803 else executed the code below. */
2804 gnu_type = TYPE_POINTER_TO (gnu_old);
2805 if (gnu_type == 0)
2807 gnu_type = make_node (RECORD_TYPE);
2808 TYPE_UNCONSTRAINED_ARRAY (gnu_type) = gnu_old;
2809 TYPE_POINTER_TO (gnu_old) = gnu_type;
2811 set_lineno (gnat_entity, 0);
2812 fields
2813 = chainon (chainon (NULL_TREE,
2814 create_field_decl
2815 (get_identifier ("P_ARRAY"),
2816 ptr_void_type_node, gnu_type,
2817 0, 0, 0, 0)),
2818 create_field_decl (get_identifier ("P_BOUNDS"),
2819 ptr_void_type_node,
2820 gnu_type, 0, 0, 0, 0));
2822 /* Make sure we can place this into a register. */
2823 TYPE_ALIGN (gnu_type)
2824 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
2825 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
2826 finish_record_type (gnu_type, fields, 0, 1);
2828 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
2829 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
2830 = concat_id_with_name (get_entity_name (gnat_desig_type),
2831 "XUT");
2832 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
2836 /* If we already know what the full type is, use it. */
2837 else if (Present (gnat_desig_full)
2838 && present_gnu_tree (gnat_desig_full))
2839 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
2841 /* Get the type of the thing we are to point to and build a pointer
2842 to it. If it is a reference to an incomplete or private type with a
2843 full view that is a record, make a dummy type node and get the
2844 actual type later when we have verified it is safe. */
2845 else if (! in_main_unit
2846 && ! present_gnu_tree (gnat_desig_type)
2847 && Present (gnat_desig_full)
2848 && ! present_gnu_tree (gnat_desig_full)
2849 && Is_Record_Type (gnat_desig_full))
2851 gnu_desig_type = make_dummy_type (gnat_desig_type);
2852 made_dummy = 1;
2855 /* Likewise if we are pointing to a record or array and we are to defer
2856 elaborating incomplete types. We do this since this access type
2857 may be the full view of some private type. Note that the
2858 unconstrained array case is handled above. */
2859 else if ((! in_main_unit || imported_p) && defer_incomplete_level != 0
2860 && ! present_gnu_tree (gnat_desig_type)
2861 && ((Is_Record_Type (gnat_desig_type)
2862 || Is_Array_Type (gnat_desig_type))
2863 || (Present (gnat_desig_full)
2864 && (Is_Record_Type (gnat_desig_full)
2865 || Is_Array_Type (gnat_desig_full)))))
2867 gnu_desig_type = make_dummy_type (gnat_desig_type);
2868 made_dummy = 1;
2870 else if (gnat_desig_type == gnat_entity)
2872 gnu_type = build_pointer_type (make_node (VOID_TYPE));
2873 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
2875 else
2876 gnu_desig_type = gnat_to_gnu_type (gnat_desig_type);
2878 /* It is possible that the above call to gnat_to_gnu_type resolved our
2879 type. If so, just return it. */
2880 if (present_gnu_tree (gnat_entity))
2882 maybe_present = 1;
2883 break;
2886 /* If we have a GCC type for the designated type, possibly
2887 modify it if we are pointing only to constant objects and then
2888 make a pointer to it. Don't do this for unconstrained arrays. */
2889 if (gnu_type == 0 && gnu_desig_type != 0)
2891 if (Is_Access_Constant (gnat_entity)
2892 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
2893 gnu_desig_type
2894 = build_qualified_type (gnu_desig_type,
2895 (TYPE_QUALS (gnu_desig_type)
2896 | TYPE_QUAL_CONST));
2898 gnu_type = build_pointer_type (gnu_desig_type);
2901 /* If we are not defining this object and we made a dummy pointer,
2902 save our current definition, evaluate the actual type, and replace
2903 the tentative type we made with the actual one. If we are to defer
2904 actually looking up the actual type, make an entry in the
2905 deferred list. */
2907 if (! in_main_unit && made_dummy)
2909 tree gnu_old_type
2910 = TYPE_FAT_POINTER_P (gnu_type)
2911 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
2913 if (esize == POINTER_SIZE
2914 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
2915 gnu_type
2916 = build_pointer_type
2917 (TYPE_OBJECT_RECORD_TYPE
2918 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
2920 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2921 ! Comes_From_Source (gnat_entity),
2922 debug_info_p);
2923 save_gnu_tree (gnat_entity, gnu_decl, 0);
2924 this_made_decl = saved = 1;
2926 if (defer_incomplete_level == 0)
2927 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
2928 gnat_to_gnu_type (gnat_desig_type));
2929 else
2931 struct incomplete *p
2932 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2934 p->old_type = gnu_old_type;
2935 p->full_type = gnat_desig_type;
2936 p->next = defer_incomplete_list;
2937 defer_incomplete_list = p;
2941 break;
2943 case E_Access_Protected_Subprogram_Type:
2944 if (type_annotate_only && No (Equivalent_Type (gnat_entity)))
2945 gnu_type = build_pointer_type (void_type_node);
2946 else
2947 /* The runtime representation is the equivalent type. */
2948 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
2950 if (Is_Itype (Directly_Designated_Type (gnat_entity))
2951 && ! present_gnu_tree (Directly_Designated_Type (gnat_entity))
2952 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
2953 && ! Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
2954 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
2955 NULL_TREE, 0);
2957 break;
2959 case E_Access_Subtype:
2961 /* We treat this as identical to its base type; any constraint is
2962 meaningful only to the front end.
2964 The designated type must be elaborated as well, if it does
2965 not have its own freeze node. Designated (sub)types created
2966 for constrained components of records with discriminants are
2967 not frozen by the front end and thus not elaborated by gigi,
2968 because their use may appear before the base type is frozen,
2969 and because it is not clear that they are needed anywhere in
2970 Gigi. With the current model, there is no correct place where
2971 they could be elaborated. */
2973 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2974 if (Is_Itype (Directly_Designated_Type (gnat_entity))
2975 && ! present_gnu_tree (Directly_Designated_Type (gnat_entity))
2976 && Is_Frozen (Directly_Designated_Type (gnat_entity))
2977 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
2979 /* If we are not defining this entity, and we have incomplete
2980 entities being processed above us, make a dummy type and
2981 elaborate it later. */
2982 if (! definition && defer_incomplete_level != 0)
2984 struct incomplete *p
2985 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2986 tree gnu_ptr_type
2987 = build_pointer_type
2988 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2990 p->old_type = TREE_TYPE (gnu_ptr_type);
2991 p->full_type = Directly_Designated_Type (gnat_entity);
2992 p->next = defer_incomplete_list;
2993 defer_incomplete_list = p;
2995 else
2996 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
2997 NULL_TREE, 0);
3000 maybe_present = 1;
3001 break;
3003 /* Subprogram Entities
3005 The following access functions are defined for subprograms (functions
3006 or procedures):
3008 First_Formal The first formal parameter.
3009 Is_Imported Indicates that the subprogram has appeared in
3010 an INTERFACE or IMPORT pragma. For now we
3011 assume that the external language is C.
3012 Is_Inlined True if the subprogram is to be inlined.
3014 In addition for function subprograms we have:
3016 Etype Return type of the function.
3018 Each parameter is first checked by calling must_pass_by_ref on its
3019 type to determine if it is passed by reference. For parameters which
3020 are copied in, if they are Ada IN OUT or OUT parameters, their return
3021 value becomes part of a record which becomes the return type of the
3022 function (C function - note that this applies only to Ada procedures
3023 so there is no Ada return type). Additional code to store back the
3024 parameters will be generated on the caller side. This transformation
3025 is done here, not in the front-end.
3027 The intended result of the transformation can be seen from the
3028 equivalent source rewritings that follow:
3030 struct temp {int a,b};
3031 procedure P (A,B: IN OUT ...) is temp P (int A,B) {
3032 .. ..
3033 end P; return {A,B};
3035 procedure call
3038 temp t;
3039 P(X,Y); t = P(X,Y);
3040 X = t.a , Y = t.b;
3043 For subprogram types we need to perform mainly the same conversions to
3044 GCC form that are needed for procedures and function declarations. The
3045 only difference is that at the end, we make a type declaration instead
3046 of a function declaration. */
3048 case E_Subprogram_Type:
3049 case E_Function:
3050 case E_Procedure:
3052 /* The first GCC parameter declaration (a PARM_DECL node). The
3053 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3054 actually is the head of this parameter list. */
3055 tree gnu_param_list = NULL_TREE;
3056 /* The type returned by a function. If the subprogram is a procedure
3057 this type should be void_type_node. */
3058 tree gnu_return_type = void_type_node;
3059 /* List of fields in return type of procedure with copy in copy out
3060 parameters. */
3061 tree gnu_field_list = NULL_TREE;
3062 /* Non-null for subprograms containing parameters passed by copy in
3063 copy out (Ada IN OUT or OUT parameters not passed by reference),
3064 in which case it is the list of nodes used to specify the values of
3065 the in out/out parameters that are returned as a record upon
3066 procedure return. The TREE_PURPOSE of an element of this list is
3067 a field of the record and the TREE_VALUE is the PARM_DECL
3068 corresponding to that field. This list will be saved in the
3069 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3070 tree gnu_return_list = NULL_TREE;
3071 Entity_Id gnat_param;
3072 int inline_flag = Is_Inlined (gnat_entity);
3073 int public_flag = Is_Public (gnat_entity);
3074 int extern_flag
3075 = (Is_Public (gnat_entity) && !definition) || imported_p;
3076 int pure_flag = Is_Pure (gnat_entity);
3077 int volatile_flag = No_Return (gnat_entity);
3078 int returns_by_ref = 0;
3079 int returns_unconstrained = 0;
3080 tree gnu_ext_name = NULL_TREE;
3081 int has_copy_in_out = 0;
3082 int parmnum;
3084 if (kind == E_Subprogram_Type && ! definition)
3085 /* A parameter may refer to this type, so defer completion
3086 of any incomplete types. */
3087 defer_incomplete_level++, this_deferred = 1;
3089 /* If the subprogram has an alias, it is probably inherited, so
3090 we can use the original one. If the original "subprogram"
3091 is actually an enumeration literal, it may be the first use
3092 of its type, so we must elaborate that type now. */
3093 if (Present (Alias (gnat_entity)))
3095 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3096 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3098 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3099 gnu_expr, 0);
3101 /* Elaborate any Itypes in the parameters of this entity. */
3102 for (gnat_temp = First_Formal (gnat_entity);
3103 Present (gnat_temp);
3104 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3105 if (Is_Itype (Etype (gnat_temp)))
3106 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3108 break;
3111 if (kind == E_Function || kind == E_Subprogram_Type)
3112 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3114 /* If this function returns by reference, make the actual
3115 return type of this function the pointer and mark the decl. */
3116 if (Returns_By_Ref (gnat_entity))
3118 returns_by_ref = 1;
3120 gnu_return_type = build_pointer_type (gnu_return_type);
3123 /* If we are supposed to return an unconstrained array,
3124 actually return a fat pointer and make a note of that. Return
3125 a pointer to an unconstrained record of variable size. */
3126 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3128 gnu_return_type = TREE_TYPE (gnu_return_type);
3129 returns_unconstrained = 1;
3132 /* If the type requires a transient scope, the result is allocated
3133 on the secondary stack, so the result type of the function is
3134 just a pointer. */
3135 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3137 gnu_return_type = build_pointer_type (gnu_return_type);
3138 returns_unconstrained = 1;
3141 /* If the type is a padded type and the underlying type would not
3142 be passed by reference or this function has a foreign convention,
3143 return the underlying type. */
3144 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3145 && TYPE_IS_PADDING_P (gnu_return_type)
3146 && (! default_pass_by_ref (TREE_TYPE
3147 (TYPE_FIELDS (gnu_return_type)))
3148 || Has_Foreign_Convention (gnat_entity)))
3149 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3151 /* Look at all our parameters and get the type of
3152 each. While doing this, build a copy-out structure if
3153 we need one. */
3155 for (gnat_param = First_Formal (gnat_entity), parmnum = 0;
3156 Present (gnat_param);
3157 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3159 tree gnu_param_name = get_entity_name (gnat_param);
3160 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3161 tree gnu_param, gnu_field;
3162 int by_ref_p = 0;
3163 int by_descr_p = 0;
3164 int by_component_ptr_p = 0;
3165 int copy_in_copy_out_flag = 0;
3166 int req_by_copy = 0, req_by_ref = 0;
3168 /* See if a Mechanism was supplied that forced this
3169 parameter to be passed one way or another. */
3170 if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3171 req_by_copy = 1;
3172 else if (Mechanism (gnat_param) == Default)
3174 else if (Mechanism (gnat_param) == By_Copy)
3175 req_by_copy = 1;
3176 else if (Mechanism (gnat_param) == By_Reference)
3177 req_by_ref = 1;
3178 else if (Mechanism (gnat_param) <= By_Descriptor)
3179 by_descr_p = 1;
3180 else if (Mechanism (gnat_param) > 0)
3182 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3183 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3184 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3185 Mechanism (gnat_param)))
3186 req_by_ref = 1;
3187 else
3188 req_by_copy = 1;
3190 else
3191 post_error ("unsupported mechanism for&", gnat_param);
3193 /* If this is either a foreign function or if the
3194 underlying type won't be passed by refererence, strip off
3195 possible padding type. */
3196 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
3197 && TYPE_IS_PADDING_P (gnu_param_type)
3198 && (req_by_ref || Has_Foreign_Convention (gnat_entity)
3199 || ! must_pass_by_ref (TREE_TYPE (TYPE_FIELDS
3200 (gnu_param_type)))))
3201 gnu_param_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
3203 /* If this is an IN parameter it is read-only, so make a variant
3204 of the type that is read-only.
3206 ??? However, if this is an unconstrained array, that type can
3207 be very complex. So skip it for now. Likewise for any other
3208 self-referential type. */
3209 if (Ekind (gnat_param) == E_In_Parameter
3210 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
3211 && ! (TYPE_SIZE (gnu_param_type) != 0
3212 && TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3213 && contains_placeholder_p (TYPE_SIZE (gnu_param_type))))
3214 gnu_param_type
3215 = build_qualified_type (gnu_param_type,
3216 (TYPE_QUALS (gnu_param_type)
3217 | TYPE_QUAL_CONST));
3219 /* For foreign conventions, pass arrays as a pointer to the
3220 underlying type. First check for unconstrained array and get
3221 the underlying array. Then get the component type and build
3222 a pointer to it. */
3223 if (Has_Foreign_Convention (gnat_entity)
3224 && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
3225 gnu_param_type
3226 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3227 (TREE_TYPE (gnu_param_type))));
3229 if (by_descr_p)
3230 gnu_param_type
3231 = build_pointer_type
3232 (build_vms_descriptor (gnu_param_type,
3233 Mechanism (gnat_param),
3234 gnat_entity));
3236 else if (Has_Foreign_Convention (gnat_entity)
3237 && ! req_by_copy
3238 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
3240 /* Strip off any multi-dimensional entries, then strip
3241 off the last array to get the component type. */
3242 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
3243 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
3244 gnu_param_type = TREE_TYPE (gnu_param_type);
3246 by_component_ptr_p = 1;
3247 gnu_param_type = TREE_TYPE (gnu_param_type);
3249 if (Ekind (gnat_param) == E_In_Parameter)
3250 gnu_param_type
3251 = build_qualified_type (gnu_param_type,
3252 (TYPE_QUALS (gnu_param_type)
3253 | TYPE_QUAL_CONST));
3255 gnu_param_type = build_pointer_type (gnu_param_type);
3258 /* Fat pointers are passed as thin pointers for foreign
3259 conventions. */
3260 else if (Has_Foreign_Convention (gnat_entity)
3261 && TYPE_FAT_POINTER_P (gnu_param_type))
3262 gnu_param_type
3263 = make_type_from_size (gnu_param_type,
3264 size_int (POINTER_SIZE), 0);
3266 /* If we must pass or were requested to pass by reference, do so.
3267 If we were requested to pass by copy, do so.
3268 Otherwise, for foreign conventions, pass all in out parameters
3269 or aggregates by reference. For COBOL and Fortran, pass
3270 all integer and FP types that way too. For Convention Ada,
3271 use the standard Ada default. */
3272 else if (must_pass_by_ref (gnu_param_type) || req_by_ref
3273 || (! req_by_copy
3274 && ((Has_Foreign_Convention (gnat_entity)
3275 && (Ekind (gnat_param) != E_In_Parameter
3276 || AGGREGATE_TYPE_P (gnu_param_type)))
3277 || (((Convention (gnat_entity)
3278 == Convention_Fortran)
3279 || (Convention (gnat_entity)
3280 == Convention_COBOL))
3281 && (INTEGRAL_TYPE_P (gnu_param_type)
3282 || FLOAT_TYPE_P (gnu_param_type)))
3283 /* For convention Ada, see if we pass by reference
3284 by default. */
3285 || (! Has_Foreign_Convention (gnat_entity)
3286 && default_pass_by_ref (gnu_param_type)))))
3288 gnu_param_type = build_reference_type (gnu_param_type);
3289 by_ref_p = 1;
3292 else if (Ekind (gnat_param) != E_In_Parameter)
3293 copy_in_copy_out_flag = 1;
3295 if (req_by_copy && (by_ref_p || by_component_ptr_p))
3296 post_error ("?cannot pass & by copy", gnat_param);
3298 /* If this is an OUT parameter that isn't passed by reference
3299 and isn't a pointer or aggregate, we don't make a PARM_DECL
3300 for it. Instead, it will be a VAR_DECL created when we process
3301 the procedure. For the special parameter of Valued_Procedure,
3302 never pass it in. */
3303 if (Ekind (gnat_param) == E_Out_Parameter && ! by_ref_p
3304 && ((Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3305 || (! by_descr_p
3306 && ! POINTER_TYPE_P (gnu_param_type)
3307 && ! AGGREGATE_TYPE_P (gnu_param_type))))
3308 gnu_param = 0;
3309 else
3311 set_lineno (gnat_param, 0);
3312 gnu_param
3313 = create_param_decl
3314 (gnu_param_name, gnu_param_type,
3315 by_ref_p || by_component_ptr_p
3316 || Ekind (gnat_param) == E_In_Parameter);
3318 DECL_BY_REF_P (gnu_param) = by_ref_p;
3319 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr_p;
3320 DECL_BY_DESCRIPTOR_P (gnu_param) = by_descr_p;
3321 DECL_POINTS_TO_READONLY_P (gnu_param)
3322 = (Ekind (gnat_param) == E_In_Parameter
3323 && (by_ref_p || by_component_ptr_p));
3324 save_gnu_tree (gnat_param, gnu_param, 0);
3325 gnu_param_list = chainon (gnu_param, gnu_param_list);
3327 /* If a parameter is a pointer, this function may modify
3328 memory through it and thus shouldn't be considered
3329 a pure function. Also, the memory may be modified
3330 between two calls, so they can't be CSE'ed. The latter
3331 case also handles by-ref parameters. */
3332 if (POINTER_TYPE_P (gnu_param_type)
3333 || TYPE_FAT_POINTER_P (gnu_param_type))
3334 pure_flag = 0;
3337 if (copy_in_copy_out_flag)
3339 if (! has_copy_in_out)
3341 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
3342 gigi_abort (111);
3344 gnu_return_type = make_node (RECORD_TYPE);
3345 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3346 has_copy_in_out = 1;
3349 set_lineno (gnat_param, 0);
3350 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3351 gnu_return_type, 0, 0, 0, 0);
3352 TREE_CHAIN (gnu_field) = gnu_field_list;
3353 gnu_field_list = gnu_field;
3354 gnu_return_list = tree_cons (gnu_field, gnu_param,
3355 gnu_return_list);
3359 /* Do not compute record for out parameters if subprogram is
3360 stubbed since structures are incomplete for the back-end. */
3361 if (gnu_field_list != 0
3362 && Convention (gnat_entity) != Convention_Stubbed)
3363 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3364 0, 0);
3366 /* If we have a CICO list but it has only one entry, we convert
3367 this function into a function that simply returns that one
3368 object. */
3369 if (list_length (gnu_return_list) == 1)
3370 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3372 if (Convention (gnat_entity) == Convention_Stdcall)
3374 struct attrib *attr
3375 = (struct attrib *) xmalloc (sizeof (struct attrib));
3377 attr->next = attr_list;
3378 attr->type = ATTR_MACHINE_ATTRIBUTE;
3379 attr->name = get_identifier ("stdcall");
3380 attr->arg = NULL_TREE;
3381 attr->error_point = gnat_entity;
3382 attr_list = attr;
3385 /* Both lists ware built in reverse. */
3386 gnu_param_list = nreverse (gnu_param_list);
3387 gnu_return_list = nreverse (gnu_return_list);
3389 gnu_type
3390 = create_subprog_type (gnu_return_type, gnu_param_list,
3391 gnu_return_list, returns_unconstrained,
3392 returns_by_ref,
3393 Function_Returns_With_DSP (gnat_entity));
3395 /* ??? For now, don't consider nested functions pure. */
3396 if (! global_bindings_p ())
3397 pure_flag = 0;
3399 gnu_type
3400 = build_qualified_type (gnu_type,
3401 (TYPE_QUALS (gnu_type)
3402 | (TYPE_QUAL_CONST * pure_flag)
3403 | (TYPE_QUAL_VOLATILE * volatile_flag)));
3405 /* Top-level or external functions need to have an assembler name.
3406 This is passed to create_subprog_decl through the ext_name argument.
3407 For Pragma Interface subprograms with no Pragma Interface_Name, the
3408 simple name already in entity_name is correct, and this is what is
3409 gotten when ext_name is NULL. If Interface_Name is specified, then
3410 the name is extracted from the N_String_Literal node containing the
3411 string specified in the Pragma. If there is no Pragma Interface,
3412 then the Ada fully qualified name is created. */
3414 if (Present (Interface_Name (gnat_entity))
3415 || ! (Is_Imported (gnat_entity) || Is_Exported (gnat_entity)))
3416 gnu_ext_name = create_concat_name (gnat_entity, 0);
3418 set_lineno (gnat_entity, 0);
3420 /* If we are defining the subprogram and it has an Address clause
3421 we must get the address expression from the saved GCC tree for the
3422 subprogram if it has a Freeze_Node. Otherwise, we elaborate
3423 the address expression here since the front-end has guaranteed
3424 in that case that the elaboration has no effects. If there is
3425 an Address clause and we are not defining the object, just
3426 make it a constant. */
3427 if (Present (Address_Clause (gnat_entity)))
3429 tree gnu_address = 0;
3431 if (definition)
3432 gnu_address
3433 = (present_gnu_tree (gnat_entity)
3434 ? get_gnu_tree (gnat_entity)
3435 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
3437 save_gnu_tree (gnat_entity, NULL_TREE, 0);
3439 gnu_type = build_reference_type (gnu_type);
3440 if (gnu_address != 0)
3441 gnu_address = convert (gnu_type, gnu_address);
3443 gnu_decl
3444 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
3445 gnu_address, 0, Is_Public (gnat_entity),
3446 extern_flag, 0, 0);
3447 DECL_BY_REF_P (gnu_decl) = 1;
3450 else if (kind == E_Subprogram_Type)
3451 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3452 ! Comes_From_Source (gnat_entity),
3453 debug_info_p);
3454 else
3456 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
3457 gnu_type, gnu_param_list,
3458 inline_flag, public_flag,
3459 extern_flag, attr_list);
3460 DECL_STUBBED_P (gnu_decl)
3461 = Convention (gnat_entity) == Convention_Stubbed;
3464 break;
3466 case E_Incomplete_Type:
3467 case E_Private_Type:
3468 case E_Limited_Private_Type:
3469 case E_Record_Type_With_Private:
3470 case E_Private_Subtype:
3471 case E_Limited_Private_Subtype:
3472 case E_Record_Subtype_With_Private:
3474 /* If this type does not have a full view in the unit we are
3475 compiling, then just get the type from its Etype. */
3476 if (No (Full_View (gnat_entity)))
3478 /* If this is an incomplete type with no full view, it must
3479 be a Taft Amendement type, so just return a dummy type. */
3480 if (kind == E_Incomplete_Type)
3481 gnu_type = make_dummy_type (gnat_entity);
3483 else if (Present (Underlying_Full_View (gnat_entity)))
3484 gnu_decl = gnat_to_gnu_entity (Underlying_Full_View (gnat_entity),
3485 NULL_TREE, 0);
3486 else
3488 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
3489 NULL_TREE, 0);
3490 maybe_present = 1;
3493 break;
3496 /* Otherwise, if we are not defining the type now, get the
3497 type from the full view. But always get the type from the full
3498 view for define on use types, since otherwise we won't see them! */
3500 else if (! definition
3501 || (Is_Itype (Full_View (gnat_entity))
3502 && No (Freeze_Node (gnat_entity)))
3503 || (Is_Itype (gnat_entity)
3504 && No (Freeze_Node (Full_View (gnat_entity)))))
3506 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
3507 NULL_TREE, 0);
3508 maybe_present = 1;
3509 break;
3512 /* For incomplete types, make a dummy type entry which will be
3513 replaced later. */
3514 gnu_type = make_dummy_type (gnat_entity);
3516 /* Save this type as the full declaration's type so we can do any needed
3517 updates when we see it. */
3518 set_lineno (gnat_entity, 0);
3519 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3520 ! Comes_From_Source (gnat_entity),
3521 debug_info_p);
3522 save_gnu_tree (Full_View (gnat_entity), gnu_decl, 0);
3523 break;
3525 /* Simple class_wide types are always viewed as their root_type
3526 by Gigi unless an Equivalent_Type is specified. */
3527 case E_Class_Wide_Type:
3528 if (Present (Equivalent_Type (gnat_entity)))
3529 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3530 else
3531 gnu_type = gnat_to_gnu_type (Root_Type (gnat_entity));
3533 maybe_present = 1;
3534 break;
3536 case E_Task_Type:
3537 case E_Task_Subtype:
3538 case E_Protected_Type:
3539 case E_Protected_Subtype:
3540 if (type_annotate_only && No (Corresponding_Record_Type (gnat_entity)))
3541 gnu_type = void_type_node;
3542 else
3543 gnu_type = gnat_to_gnu_type (Corresponding_Record_Type (gnat_entity));
3545 maybe_present = 1;
3546 break;
3548 case E_Label:
3549 gnu_decl = create_label_decl (gnu_entity_id);
3550 break;
3552 case E_Block:
3553 case E_Loop:
3554 /* Nothing at all to do here, so just return an ERROR_MARK and claim
3555 we've already saved it, so we don't try to. */
3556 gnu_decl = error_mark_node;
3557 saved = 1;
3558 break;
3560 default:
3561 gigi_abort (113);
3564 /* If we had a case where we evaluated another type and it might have
3565 defined this one, handle it here. */
3566 if (maybe_present && present_gnu_tree (gnat_entity))
3568 gnu_decl = get_gnu_tree (gnat_entity);
3569 saved = 1;
3572 /* If we are processing a type and there is either no decl for it or
3573 we just made one, do some common processing for the type, such as
3574 handling alignment and possible padding. */
3576 if ((gnu_decl == 0 || this_made_decl) && IN (kind, Type_Kind))
3578 if (Is_Tagged_Type (gnat_entity))
3579 TYPE_ALIGN_OK_P (gnu_type) = 1;
3581 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
3582 TYPE_BY_REFERENCE_P (gnu_type) = 1;
3584 /* ??? Don't set the size for a String_Literal since it is either
3585 confirming or we don't handle it properly (if the low bound is
3586 non-constant). */
3587 if (gnu_size == 0 && kind != E_String_Literal_Subtype)
3588 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
3589 TYPE_DECL, 0, Has_Size_Clause (gnat_entity));
3591 /* If a size was specified, see if we can make a new type of that size
3592 by rearranging the type, for example from a fat to a thin pointer. */
3593 if (gnu_size != 0)
3595 gnu_type
3596 = make_type_from_size (gnu_type, gnu_size,
3597 Has_Biased_Representation (gnat_entity));
3599 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
3600 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
3601 gnu_size = 0;
3604 /* If the alignment hasn't already been processed and this is
3605 not an unconstrained array, see if an alignment is specified.
3606 If not, we pick a default alignment for atomic objects. */
3607 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
3609 else if (Known_Alignment (gnat_entity))
3610 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
3611 TYPE_ALIGN (gnu_type));
3612 else if (Is_Atomic (gnat_entity) && gnu_size == 0
3613 && host_integerp (TYPE_SIZE (gnu_type), 1)
3614 && integer_pow2p (TYPE_SIZE (gnu_type)))
3615 align = MIN (BIGGEST_ALIGNMENT,
3616 tree_low_cst (TYPE_SIZE (gnu_type), 1));
3617 else if (Is_Atomic (gnat_entity) && gnu_size != 0
3618 && host_integerp (gnu_size, 1)
3619 && integer_pow2p (gnu_size))
3620 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
3622 /* See if we need to pad the type. If we did, and made a record,
3623 the name of the new type may be changed. So get it back for
3624 us when we make the new TYPE_DECL below. */
3625 gnu_type = maybe_pad_type (gnu_type, gnu_size, align,
3626 gnat_entity, "PAD", 1, definition, 0);
3627 if (TREE_CODE (gnu_type) == RECORD_TYPE
3628 && TYPE_IS_PADDING_P (gnu_type))
3630 gnu_entity_id = TYPE_NAME (gnu_type);
3631 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
3632 gnu_entity_id = DECL_NAME (gnu_entity_id);
3635 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
3637 /* If we are at global level, GCC will have applied variable_size to
3638 the type, but that won't have done anything. So, if it's not
3639 a constant or self-referential, call elaborate_expression_1 to
3640 make a variable for the size rather than calculating it each time.
3641 Handle both the RM size and the actual size. */
3642 if (global_bindings_p ()
3643 && TYPE_SIZE (gnu_type) != 0
3644 && TREE_CODE (TYPE_SIZE (gnu_type)) != INTEGER_CST
3645 && ! contains_placeholder_p (TYPE_SIZE (gnu_type)))
3647 if (TREE_CODE (gnu_type) == RECORD_TYPE
3648 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
3649 TYPE_SIZE (gnu_type), 0))
3650 TYPE_ADA_SIZE (gnu_type) = TYPE_SIZE (gnu_type)
3651 = elaborate_expression_1 (gnat_entity, gnat_entity,
3652 TYPE_SIZE (gnu_type),
3653 get_identifier ("SIZE"),
3654 definition, 0);
3655 else if (TREE_CODE (gnu_type) == RECORD_TYPE)
3657 TYPE_ADA_SIZE (gnu_type)
3658 = elaborate_expression_1 (gnat_entity, gnat_entity,
3659 TYPE_ADA_SIZE (gnu_type),
3660 get_identifier ("RM_SIZE"),
3661 definition, 0);
3662 TYPE_SIZE (gnu_type)
3663 = elaborate_expression_1 (gnat_entity, gnat_entity,
3664 TYPE_SIZE (gnu_type),
3665 get_identifier ("SIZE"),
3666 definition, 0);
3667 TYPE_SIZE_UNIT (gnu_type)
3668 = elaborate_expression_1 (gnat_entity, gnat_entity,
3669 TYPE_SIZE_UNIT (gnu_type),
3670 get_identifier ("SIZE_UNIT"),
3671 definition, 0);
3673 else
3675 TYPE_SIZE (gnu_type)
3676 = elaborate_expression_1 (gnat_entity, gnat_entity,
3677 TYPE_SIZE (gnu_type),
3678 get_identifier ("SIZE"),
3679 definition, 0);
3680 TYPE_SIZE_UNIT (gnu_type)
3681 = elaborate_expression_1 (gnat_entity, gnat_entity,
3682 TYPE_SIZE_UNIT (gnu_type),
3683 get_identifier ("SIZE_UNIT"),
3684 definition, 0);
3688 /* If this is a record type or subtype, call elaborate_expression_1 on
3689 any field position. Do this for both global and local types.
3690 Skip any fields that we haven't made trees for to avoid problems with
3691 class wide types. */
3692 if (IN (kind, Record_Kind))
3693 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3694 gnat_temp = Next_Entity (gnat_temp))
3695 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
3697 tree gnu_field = get_gnu_tree (gnat_temp);
3699 if (TREE_CODE (DECL_FIELD_OFFSET (gnu_field)) != INTEGER_CST
3700 && ! contains_placeholder_p (DECL_FIELD_OFFSET (gnu_field)))
3701 DECL_FIELD_OFFSET (gnu_field)
3702 = elaborate_expression_1 (gnat_temp, gnat_temp,
3703 DECL_FIELD_OFFSET (gnu_field),
3704 get_identifier ("OFFSET"),
3705 definition, 0);
3708 gnu_type = build_qualified_type (gnu_type,
3709 (TYPE_QUALS (gnu_type)
3710 | (TYPE_QUAL_VOLATILE
3711 * Is_Volatile (gnat_entity))));
3713 if (Is_Atomic (gnat_entity))
3714 check_ok_for_atomic (gnu_type, gnat_entity, 0);
3716 if (Known_Alignment (gnat_entity))
3717 TYPE_USER_ALIGN (gnu_type) = 1;
3719 if (gnu_decl == 0)
3721 set_lineno (gnat_entity, 0);
3722 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3723 ! Comes_From_Source (gnat_entity),
3724 debug_info_p);
3726 else
3727 TREE_TYPE (gnu_decl) = gnu_type;
3730 if (IN (kind, Type_Kind) && ! TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
3732 gnu_type = TREE_TYPE (gnu_decl);
3734 /* Back-annotate the Alignment of the type if not already in the
3735 tree. Likewise for sizes. */
3736 if (Unknown_Alignment (gnat_entity))
3737 Set_Alignment (gnat_entity,
3738 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
3740 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type) != 0)
3742 /* If the size is self-referential, we annotate the maximum
3743 value of that size. */
3744 tree gnu_size = TYPE_SIZE (gnu_type);
3746 if (contains_placeholder_p (gnu_size))
3747 gnu_size = max_size (gnu_size, 1);
3749 Set_Esize (gnat_entity, annotate_value (gnu_size));
3752 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type) != 0)
3753 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
3756 if (! Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
3757 DECL_ARTIFICIAL (gnu_decl) = 1;
3759 if (! debug_info_p && DECL_P (gnu_decl)
3760 && TREE_CODE (gnu_decl) != FUNCTION_DECL)
3761 DECL_IGNORED_P (gnu_decl) = 1;
3763 /* If this decl is really indirect, adjust it. */
3764 if (TREE_CODE (gnu_decl) == VAR_DECL)
3765 adjust_decl_rtl (gnu_decl);
3767 /* If we haven't already, associate the ..._DECL node that we just made with
3768 the input GNAT entity node. */
3769 if (! saved)
3770 save_gnu_tree (gnat_entity, gnu_decl, 0);
3772 /* If this is an enumeral or floating-point type, we were not able to set
3773 the bounds since they refer to the type. These bounds are always static.
3775 For enumeration types, also write debugging information and declare the
3776 enumeration literal table, if needed. */
3778 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
3779 || (kind == E_Floating_Point_Type && ! Vax_Float (gnat_entity)))
3781 tree gnu_scalar_type = gnu_type;
3783 /* If this is a padded type, we need to use the underlying type. */
3784 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
3785 && TYPE_IS_PADDING_P (gnu_scalar_type))
3786 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
3788 /* If this is a floating point type and we haven't set a floating
3789 point type yet, use this in the evaluation of the bounds. */
3790 if (longest_float_type_node == 0 && kind == E_Floating_Point_Type)
3791 longest_float_type_node = gnu_type;
3793 TYPE_MIN_VALUE (gnu_scalar_type)
3794 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
3795 TYPE_MAX_VALUE (gnu_scalar_type)
3796 = gnat_to_gnu (Type_High_Bound (gnat_entity));
3798 if (kind == E_Enumeration_Type)
3800 TYPE_STUB_DECL (gnu_scalar_type) = gnu_decl;
3802 /* Since this has both a typedef and a tag, avoid outputting
3803 the name twice. */
3804 DECL_ARTIFICIAL (gnu_decl) = 1;
3805 rest_of_type_compilation (gnu_scalar_type, global_bindings_p ());
3809 /* If we deferred processing of incomplete types, re-enable it. If there
3810 were no other disables and we have some to process, do so. */
3811 if (this_deferred && --defer_incomplete_level == 0
3812 && defer_incomplete_list != 0)
3814 struct incomplete *incp = defer_incomplete_list;
3815 struct incomplete *next;
3817 defer_incomplete_list = 0;
3818 for (; incp; incp = next)
3820 next = incp->next;
3822 if (incp->old_type != 0)
3823 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
3824 gnat_to_gnu_type (incp->full_type));
3825 free (incp);
3829 /* If we are not defining this type, see if it's in the incomplete list.
3830 If so, handle that list entry now. */
3831 else if (! definition)
3833 struct incomplete *incp;
3835 for (incp = defer_incomplete_list; incp; incp = incp->next)
3836 if (incp->old_type != 0 && incp->full_type == gnat_entity)
3838 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
3839 TREE_TYPE (gnu_decl));
3840 incp->old_type = 0;
3844 if (this_global)
3845 force_global--;
3847 if (Is_Packed_Array_Type (gnat_entity)
3848 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
3849 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
3850 && ! present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
3851 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
3853 return gnu_decl;
3856 /* Given GNAT_ENTITY, elaborate all expressions that are required to
3857 be elaborated at the point of its definition, but do nothing else. */
3859 void
3860 elaborate_entity (gnat_entity)
3861 Entity_Id gnat_entity;
3863 switch (Ekind (gnat_entity))
3865 case E_Signed_Integer_Subtype:
3866 case E_Modular_Integer_Subtype:
3867 case E_Enumeration_Subtype:
3868 case E_Ordinary_Fixed_Point_Subtype:
3869 case E_Decimal_Fixed_Point_Subtype:
3870 case E_Floating_Point_Subtype:
3872 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
3873 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
3875 /* ??? Tests for avoiding static constaint error expression
3876 is needed until the front stops generating bogus conversions
3877 on bounds of real types. */
3879 if (! Raises_Constraint_Error (gnat_lb))
3880 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
3881 1, 0, Needs_Debug_Info (gnat_entity));
3882 if (! Raises_Constraint_Error (gnat_hb))
3883 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
3884 1, 0, Needs_Debug_Info (gnat_entity));
3885 break;
3888 case E_Record_Type:
3890 Node_Id full_definition = Declaration_Node (gnat_entity);
3891 Node_Id record_definition = Type_Definition (full_definition);
3893 /* If this is a record extension, go a level further to find the
3894 record definition. */
3895 if (Nkind (record_definition) == N_Derived_Type_Definition)
3896 record_definition = Record_Extension_Part (record_definition);
3898 break;
3900 case E_Record_Subtype:
3901 case E_Private_Subtype:
3902 case E_Limited_Private_Subtype:
3903 case E_Record_Subtype_With_Private:
3904 if (Is_Constrained (gnat_entity)
3905 && Has_Discriminants (Base_Type (gnat_entity))
3906 && Present (Discriminant_Constraint (gnat_entity)))
3908 Node_Id gnat_discriminant_expr;
3909 Entity_Id gnat_field;
3911 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
3912 gnat_discriminant_expr
3913 = First_Elmt (Discriminant_Constraint (gnat_entity));
3914 Present (gnat_field);
3915 gnat_field = Next_Discriminant (gnat_field),
3916 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
3917 /* ??? For now, ignore access discriminants. */
3918 if (! Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
3919 elaborate_expression (Node (gnat_discriminant_expr),
3920 gnat_entity,
3921 get_entity_name (gnat_field), 1, 0, 0);
3923 break;
3928 /* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
3929 any entities on its entity chain similarly. */
3931 void
3932 mark_out_of_scope (gnat_entity)
3933 Entity_Id gnat_entity;
3935 Entity_Id gnat_sub_entity;
3936 unsigned int kind = Ekind (gnat_entity);
3938 /* If this has an entity list, process all in the list. */
3939 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
3940 || IN (kind, Private_Kind)
3941 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
3942 || kind == E_Function || kind == E_Generic_Function
3943 || kind == E_Generic_Package || kind == E_Generic_Procedure
3944 || kind == E_Loop || kind == E_Operator || kind == E_Package
3945 || kind == E_Package_Body || kind == E_Procedure
3946 || kind == E_Record_Type || kind == E_Record_Subtype
3947 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
3948 for (gnat_sub_entity = First_Entity (gnat_entity);
3949 Present (gnat_sub_entity);
3950 gnat_sub_entity = Next_Entity (gnat_sub_entity))
3951 if (Scope (gnat_sub_entity) == gnat_entity
3952 && gnat_sub_entity != gnat_entity)
3953 mark_out_of_scope (gnat_sub_entity);
3955 /* Now clear this if it has been defined, but only do so if it isn't
3956 a subprogram or parameter. We could refine this, but it isn't
3957 worth it. If this is statically allocated, it is supposed to
3958 hang around out of cope. */
3959 if (present_gnu_tree (gnat_entity) && ! Is_Statically_Allocated (gnat_entity)
3960 && kind != E_Procedure && kind != E_Function && ! IN (kind, Formal_Kind))
3962 save_gnu_tree (gnat_entity, NULL_TREE, 1);
3963 save_gnu_tree (gnat_entity, error_mark_node, 1);
3967 /* Return a TREE_LIST describing the substitutions needed to reflect
3968 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
3969 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
3970 of GNAT_SUBTYPE. The substitions can be in any order. TREE_PURPOSE
3971 gives the tree for the discriminant and TREE_VALUES is the replacement
3972 value. They are in the form of operands to substitute_in_expr.
3973 DEFINITION is as in gnat_to_gnu_entity. */
3975 static tree
3976 substitution_list (gnat_subtype, gnat_type, gnu_list, definition)
3977 Entity_Id gnat_subtype;
3978 Entity_Id gnat_type;
3979 tree gnu_list;
3980 int definition;
3982 Entity_Id gnat_discrim;
3983 Node_Id gnat_value;
3985 if (No (gnat_type))
3986 gnat_type = Implementation_Base_Type (gnat_subtype);
3988 if (Has_Discriminants (gnat_type))
3989 for (gnat_discrim = First_Girder_Discriminant (gnat_type),
3990 gnat_value = First_Elmt (Girder_Constraint (gnat_subtype));
3991 Present (gnat_discrim);
3992 gnat_discrim = Next_Girder_Discriminant (gnat_discrim),
3993 gnat_value = Next_Elmt (gnat_value))
3994 /* Ignore access discriminants. */
3995 if (! Is_Access_Type (Etype (Node (gnat_value))))
3996 gnu_list = tree_cons (gnat_to_gnu_entity (gnat_discrim, NULL_TREE, 0),
3997 elaborate_expression
3998 (Node (gnat_value), gnat_subtype,
3999 get_entity_name (gnat_discrim), definition,
4000 1, 0),
4001 gnu_list);
4003 return gnu_list;
4006 /* For the following two functions: for each GNAT entity, the GCC
4007 tree node used as a dummy for that entity, if any. */
4009 static tree *dummy_node_table;
4011 /* Initialize the above table. */
4013 void
4014 init_dummy_type ()
4016 Node_Id gnat_node;
4018 dummy_node_table = (tree *) xmalloc (max_gnat_nodes * sizeof (tree));
4019 ggc_add_tree_root (dummy_node_table, max_gnat_nodes);
4021 for (gnat_node = 0; gnat_node < max_gnat_nodes; gnat_node++)
4022 dummy_node_table[gnat_node] = NULL_TREE;
4024 dummy_node_table -= First_Node_Id;
4027 /* Make a dummy type corresponding to GNAT_TYPE. */
4029 tree
4030 make_dummy_type (gnat_type)
4031 Entity_Id gnat_type;
4033 Entity_Id gnat_underlying;
4034 tree gnu_type;
4036 /* Find a full type for GNAT_TYPE, taking into account any class wide
4037 types. */
4038 if (Is_Class_Wide_Type (gnat_type) && Present (Equivalent_Type (gnat_type)))
4039 gnat_type = Equivalent_Type (gnat_type);
4040 else if (Ekind (gnat_type) == E_Class_Wide_Type)
4041 gnat_type = Root_Type (gnat_type);
4043 for (gnat_underlying = gnat_type;
4044 (IN (Ekind (gnat_underlying), Incomplete_Or_Private_Kind)
4045 && Present (Full_View (gnat_underlying)));
4046 gnat_underlying = Full_View (gnat_underlying))
4049 /* If it there already a dummy type, use that one. Else make one. */
4050 if (dummy_node_table[gnat_underlying])
4051 return dummy_node_table[gnat_underlying];
4053 /* If this is a record, make this a RECORD_TYPE or UNION_TYPE; else make
4054 it a VOID_TYPE. */
4055 if (Is_Record_Type (gnat_underlying))
4056 gnu_type = make_node (Is_Unchecked_Union (gnat_underlying)
4057 ? UNION_TYPE : RECORD_TYPE);
4058 else
4059 gnu_type = make_node (ENUMERAL_TYPE);
4061 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
4062 if (AGGREGATE_TYPE_P (gnu_type))
4063 TYPE_STUB_DECL (gnu_type)
4064 = pushdecl (build_decl (TYPE_DECL, NULL_TREE, gnu_type));
4066 TYPE_DUMMY_P (gnu_type) = 1;
4067 dummy_node_table[gnat_underlying] = gnu_type;
4069 return gnu_type;
4072 /* Return 1 if the size represented by GNU_SIZE can be handled by an
4073 allocation. If STATIC_P is non-zero, consider only what can be
4074 done with a static allocation. */
4076 static int
4077 allocatable_size_p (gnu_size, static_p)
4078 tree gnu_size;
4079 int static_p;
4081 /* If this is not a static allocation, the only case we want to forbid
4082 is an overflowing size. That will be converted into a raise a
4083 Storage_Error. */
4084 if (! static_p)
4085 return ! (TREE_CODE (gnu_size) == INTEGER_CST
4086 && TREE_CONSTANT_OVERFLOW (gnu_size));
4088 /* Otherwise, we need to deal with both variable sizes and constant
4089 sizes that won't fit in a host int. */
4090 return host_integerp (gnu_size, 1);
4093 /* Return a list of attributes for GNAT_ENTITY, if any. */
4095 static struct attrib *
4096 build_attr_list (gnat_entity)
4097 Entity_Id gnat_entity;
4099 struct attrib *attr_list = 0;
4100 Node_Id gnat_temp;
4102 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
4103 gnat_temp = Next_Rep_Item (gnat_temp))
4104 if (Nkind (gnat_temp) == N_Pragma)
4106 struct attrib *attr;
4107 tree gnu_arg0 = 0, gnu_arg1 = 0;
4108 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
4109 enum attr_type etype;
4111 if (Present (gnat_assoc) && Present (First (gnat_assoc))
4112 && Present (Next (First (gnat_assoc)))
4113 && (Nkind (Expression (Next (First (gnat_assoc))))
4114 == N_String_Literal))
4116 gnu_arg0 = get_identifier (TREE_STRING_POINTER
4117 (gnat_to_gnu
4118 (Expression (Next
4119 (First (gnat_assoc))))));
4120 if (Present (Next (Next (First (gnat_assoc))))
4121 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
4122 == N_String_Literal))
4123 gnu_arg1 = get_identifier (TREE_STRING_POINTER
4124 (gnat_to_gnu
4125 (Expression
4126 (Next (Next
4127 (First (gnat_assoc)))))));
4130 switch (Get_Pragma_Id (Chars (gnat_temp)))
4132 case Pragma_Machine_Attribute:
4133 etype = ATTR_MACHINE_ATTRIBUTE;
4134 break;
4136 case Pragma_Linker_Alias:
4137 etype = ATTR_LINK_ALIAS;
4138 break;
4140 case Pragma_Linker_Section:
4141 etype = ATTR_LINK_SECTION;
4142 break;
4144 case Pragma_Weak_External:
4145 etype = ATTR_WEAK_EXTERNAL;
4146 break;
4148 default:
4149 continue;
4152 attr = (struct attrib *) xmalloc (sizeof (struct attrib));
4153 attr->next = attr_list;
4154 attr->type = etype;
4155 attr->name = gnu_arg0;
4156 attr->arg = gnu_arg1;
4157 attr->error_point
4158 = Present (Next (First (gnat_assoc)))
4159 ? Expression (Next (First (gnat_assoc))) : gnat_temp;
4160 attr_list = attr;
4163 return attr_list;
4166 /* Get the unpadded version of a GNAT type. */
4168 tree
4169 get_unpadded_type (gnat_entity)
4170 Entity_Id gnat_entity;
4172 tree type = gnat_to_gnu_type (gnat_entity);
4174 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4175 type = TREE_TYPE (TYPE_FIELDS (type));
4177 return type;
4180 /* Called when we need to protect a variable object using a save_expr. */
4182 tree
4183 maybe_variable (gnu_operand, gnat_node)
4184 tree gnu_operand;
4185 Node_Id gnat_node;
4187 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
4188 || TREE_CODE (gnu_operand) == SAVE_EXPR
4189 || TREE_CODE (gnu_operand) == NULL_EXPR)
4190 return gnu_operand;
4192 /* If we will be generating code, make sure we are at the proper
4193 line number. */
4194 if (! global_bindings_p () && ! TREE_CONSTANT (gnu_operand)
4195 && ! contains_placeholder_p (gnu_operand))
4196 set_lineno (gnat_node, 1);
4198 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
4199 return build1 (UNCONSTRAINED_ARRAY_REF, TREE_TYPE (gnu_operand),
4200 variable_size (TREE_OPERAND (gnu_operand, 0)));
4201 else
4202 return variable_size (gnu_operand);
4205 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
4206 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
4207 return the GCC tree to use for that expression. GNU_NAME is the
4208 qualification to use if an external name is appropriate and DEFINITION is
4209 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
4210 we need a result. Otherwise, we are just elaborating this for
4211 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
4212 purposes even if it isn't needed for code generation. */
4214 static tree
4215 elaborate_expression (gnat_expr, gnat_entity, gnu_name, definition,
4216 need_value, need_debug)
4217 Node_Id gnat_expr;
4218 Entity_Id gnat_entity;
4219 tree gnu_name;
4220 int definition;
4221 int need_value;
4222 int need_debug;
4224 tree gnu_expr;
4226 /* If we already elaborated this expression (e.g., it was involved
4227 in the definition of a private type), use the old value. */
4228 if (present_gnu_tree (gnat_expr))
4229 return get_gnu_tree (gnat_expr);
4231 /* If we don't need a value and this is static or a discriment, we
4232 don't need to do anything. */
4233 else if (! need_value
4234 && (Is_OK_Static_Expression (gnat_expr)
4235 || (Nkind (gnat_expr) == N_Identifier
4236 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
4237 return 0;
4239 /* Otherwise, convert this tree to its GCC equivalant. */
4240 gnu_expr
4241 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
4242 gnu_name, definition, need_debug);
4244 /* Save the expression in case we try to elaborate this entity again.
4245 Since this is not a DECL, don't check it. If this is a constant,
4246 don't save it since GNAT_EXPR might be used more than once. Also,
4247 don't save if it's a discriminant. */
4248 if (! TREE_CONSTANT (gnu_expr) && ! contains_placeholder_p (gnu_expr))
4249 save_gnu_tree (gnat_expr, gnu_expr, 1);
4251 return need_value ? gnu_expr : error_mark_node;
4254 /* Similar, but take a GNU expression. */
4256 static tree
4257 elaborate_expression_1 (gnat_expr, gnat_entity, gnu_expr, gnu_name, definition,
4258 need_debug)
4259 Node_Id gnat_expr;
4260 Entity_Id gnat_entity;
4261 tree gnu_expr;
4262 tree gnu_name;
4263 int definition;
4264 int need_debug;
4266 tree gnu_decl = 0;
4267 tree gnu_inner_expr = gnu_expr;
4268 int expr_variable;
4269 int expr_global = Is_Public (gnat_entity) || global_bindings_p ();
4271 /* Strip any conversions to see if the expression is a readonly variable.
4272 ??? This really should remain readonly, but we have to think about
4273 the typing of the tree here. */
4274 while (TREE_CODE (gnu_inner_expr) == NOP_EXPR
4275 && TREE_CODE (gnu_inner_expr) == CONVERT_EXPR)
4276 gnu_inner_expr = TREE_OPERAND (gnu_inner_expr, 0);
4278 /* In most cases, we won't see a naked FIELD_DECL here because a
4279 discriminant reference will have been replaced with a COMPONENT_REF
4280 when the type is being elaborated. However, there are some cases
4281 involving child types where we will. So convert it to a COMPONENT_REF
4282 here. We have to hope it will be at the highest level of the
4283 expression in these cases. */
4284 if (TREE_CODE (gnu_expr) == FIELD_DECL)
4285 gnu_expr = build (COMPONENT_REF, TREE_TYPE (gnu_expr),
4286 build (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
4287 gnu_expr);
4289 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
4290 that is a constant, make a variable that is initialized to contain the
4291 bound when the package containing the definition is elaborated. If
4292 this entity is defined at top level and a bound or discriminant value
4293 isn't a constant or a reference to a discriminant, replace the bound
4294 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
4295 rely here on the fact that an expression cannot contain both the
4296 discriminant and some other variable. */
4298 expr_variable = (TREE_CODE_CLASS (TREE_CODE (gnu_expr)) != 'c'
4299 && ! (TREE_CODE (gnu_inner_expr) == VAR_DECL
4300 && TREE_READONLY (gnu_inner_expr))
4301 && ! contains_placeholder_p (gnu_expr));
4303 /* If this is a static expression or contains a discriminant, we don't
4304 need the variable for debugging (and can't elaborate anyway if a
4305 discriminant). */
4306 if (need_debug
4307 && (Is_OK_Static_Expression (gnat_expr)
4308 || contains_placeholder_p (gnu_expr)))
4309 need_debug = 0;
4311 /* Now create the variable if we need it. */
4312 if (need_debug || (expr_variable && expr_global))
4314 set_lineno (gnat_entity, ! global_bindings_p ());
4315 gnu_decl
4316 = create_var_decl (create_concat_name (gnat_entity,
4317 IDENTIFIER_POINTER (gnu_name)),
4318 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, 1,
4319 Is_Public (gnat_entity), ! definition, 0, 0);
4322 /* We only need to use this variable if we are in global context since GCC
4323 can do the right thing in the local case. */
4324 if (expr_global && expr_variable)
4325 return gnu_decl;
4326 else
4327 return maybe_variable (gnu_expr, gnat_expr);
4330 /* Create a record type that contains a field of TYPE with a starting bit
4331 position so that it is aligned to ALIGN bits and is SIZE bytes long. */
4333 tree
4334 make_aligning_type (type, align, size)
4335 tree type;
4336 int align;
4337 tree size;
4339 tree record_type = make_node (RECORD_TYPE);
4340 tree place = build (PLACEHOLDER_EXPR, record_type);
4341 tree size_addr_place = convert (sizetype,
4342 build_unary_op (ADDR_EXPR, NULL_TREE,
4343 place));
4344 tree name = TYPE_NAME (type);
4345 tree pos, field;
4347 if (TREE_CODE (name) == TYPE_DECL)
4348 name = DECL_NAME (name);
4350 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
4352 /* The bit position is obtained by "and"ing the alignment minus 1
4353 with the two's complement of the address and multiplying
4354 by the number of bits per unit. Do all this in sizetype. */
4356 pos = size_binop (MULT_EXPR,
4357 convert (bitsizetype,
4358 size_binop (BIT_AND_EXPR,
4359 size_diffop (size_zero_node,
4360 size_addr_place),
4361 ssize_int ((align / BITS_PER_UNIT)
4362 - 1))),
4363 bitsize_unit_node);
4365 field = create_field_decl (get_identifier ("F"), type, record_type,
4366 1, size, pos, 1);
4367 DECL_BIT_FIELD (field) = 0;
4369 finish_record_type (record_type, field, 1, 0);
4370 TYPE_ALIGN (record_type) = BIGGEST_ALIGNMENT;
4371 TYPE_SIZE (record_type)
4372 = size_binop (PLUS_EXPR,
4373 size_binop (MULT_EXPR, convert (bitsizetype, size),
4374 bitsize_unit_node),
4375 bitsize_int (align));
4376 TYPE_SIZE_UNIT (record_type)
4377 = size_binop (PLUS_EXPR, size, size_int (align / BITS_PER_UNIT));
4379 return record_type;
4382 /* TYPE is a RECORD_TYPE with BLKmode that's being used as the field
4383 type of a packed record. See if we can rewrite it as a record that has
4384 a non-BLKmode type, which we can pack tighter. If so, return the
4385 new type. If not, return the original type. */
4387 static tree
4388 make_packable_type (type)
4389 tree type;
4391 tree new_type = make_node (RECORD_TYPE);
4392 tree field_list = NULL_TREE;
4393 tree old_field;
4395 /* Copy the name and flags from the old type to that of the new and set
4396 the alignment to try for an integral type. */
4397 TYPE_NAME (new_type) = TYPE_NAME (type);
4398 TYPE_LEFT_JUSTIFIED_MODULAR_P (new_type)
4399 = TYPE_LEFT_JUSTIFIED_MODULAR_P (type);
4400 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
4402 TYPE_ALIGN (new_type)
4403 = ((HOST_WIDE_INT) 1
4404 << (floor_log2 (tree_low_cst (TYPE_SIZE (type), 1) - 1) + 1));
4406 /* Now copy the fields, keeping the position and size. */
4407 for (old_field = TYPE_FIELDS (type); old_field != 0;
4408 old_field = TREE_CHAIN (old_field))
4410 tree new_field
4411 = create_field_decl (DECL_NAME (old_field), TREE_TYPE (old_field),
4412 new_type, TYPE_PACKED (type),
4413 DECL_SIZE (old_field),
4414 bit_position (old_field),
4415 ! DECL_NONADDRESSABLE_P (old_field));
4417 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
4418 DECL_ORIGINAL_FIELD (new_field)
4419 = (DECL_ORIGINAL_FIELD (old_field) != 0
4420 ? DECL_ORIGINAL_FIELD (old_field) : old_field);
4421 TREE_CHAIN (new_field) = field_list;
4422 field_list = new_field;
4425 finish_record_type (new_type, nreverse (field_list), 1, 1);
4426 return TYPE_MODE (new_type) == BLKmode ? type : new_type;
4429 /* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
4430 if needed. We have already verified that SIZE and TYPE are large enough.
4432 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
4433 to issue a warning.
4435 IS_USER_TYPE is nonzero if we must be sure we complete the original type.
4437 DEFINITION is nonzero if this type is being defined.
4439 SAME_RM_SIZE is nonzero if the RM_Size of the resulting type is to be
4440 set to its TYPE_SIZE; otherwise, it's set to the RM_Size of the original
4441 type. */
4443 static tree
4444 maybe_pad_type (type, size, align, gnat_entity, name_trailer,
4445 is_user_type, definition, same_rm_size)
4446 tree type;
4447 tree size;
4448 unsigned int align;
4449 Entity_Id gnat_entity;
4450 const char *name_trailer;
4451 int is_user_type;
4452 int definition;
4453 int same_rm_size;
4455 tree orig_size = TYPE_SIZE (type);
4456 tree record;
4457 tree field;
4459 /* If TYPE is a padded type, see if it agrees with any size and alignment
4460 we were given. If so, return the original type. Otherwise, strip
4461 off the padding, since we will either be returning the inner type
4462 or repadding it. If no size or alignment is specified, use that of
4463 the original padded type. */
4465 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4467 if ((size == 0
4468 || operand_equal_p (round_up (size,
4469 MAX (align, TYPE_ALIGN (type))),
4470 round_up (TYPE_SIZE (type),
4471 MAX (align, TYPE_ALIGN (type))),
4473 && (align == 0 || align == TYPE_ALIGN (type)))
4474 return type;
4476 if (size == 0)
4477 size = TYPE_SIZE (type);
4478 if (align == 0)
4479 align = TYPE_ALIGN (type);
4481 type = TREE_TYPE (TYPE_FIELDS (type));
4482 orig_size = TYPE_SIZE (type);
4485 /* If the size is either not being changed or is being made smaller (which
4486 is not done here (and is only valid for bitfields anyway), show the size
4487 isn't changing. Likewise, clear the alignment if it isn't being
4488 changed. Then return if we aren't doing anything. */
4490 if (size != 0
4491 && (operand_equal_p (size, orig_size, 0)
4492 || (TREE_CODE (orig_size) == INTEGER_CST
4493 && tree_int_cst_lt (size, orig_size))))
4494 size = 0;
4496 if (align == TYPE_ALIGN (type))
4497 align = 0;
4499 if (align == 0 && size == 0)
4500 return type;
4502 /* We used to modify the record in place in some cases, but that could
4503 generate incorrect debugging information. So make a new record
4504 type and name. */
4505 record = make_node (RECORD_TYPE);
4507 if (Present (gnat_entity))
4508 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
4510 /* If we were making a type, complete the original type and give it a
4511 name. */
4512 if (is_user_type)
4513 create_type_decl (get_entity_name (gnat_entity), type,
4514 0, ! Comes_From_Source (gnat_entity),
4515 ! (TYPE_NAME (type) != 0
4516 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4517 && DECL_IGNORED_P (TYPE_NAME (type))));
4519 /* If we are changing the alignment and the input type is a record with
4520 BLKmode and a small constant size, try to make a form that has an
4521 integral mode. That might allow this record to have an integral mode,
4522 which will be much more efficient. There is no point in doing this if a
4523 size is specified unless it is also smaller than the biggest alignment
4524 and it is incorrect to do this if the size of the original type is not a
4525 multiple of the alignment. */
4526 if (align != 0
4527 && TREE_CODE (type) == RECORD_TYPE
4528 && TYPE_MODE (type) == BLKmode
4529 && host_integerp (orig_size, 1)
4530 && compare_tree_int (orig_size, BIGGEST_ALIGNMENT) <= 0
4531 && (size == 0
4532 || (TREE_CODE (size) == INTEGER_CST
4533 && compare_tree_int (size, BIGGEST_ALIGNMENT) <= 0))
4534 && tree_low_cst (orig_size, 1) % align == 0)
4535 type = make_packable_type (type);
4537 field = create_field_decl (get_identifier ("F"), type, record, 0,
4538 NULL_TREE, bitsize_zero_node, 1);
4540 DECL_INTERNAL_P (field) = 1;
4541 TYPE_SIZE (record) = size != 0 ? size : orig_size;
4542 TYPE_SIZE_UNIT (record)
4543 = convert (sizetype,
4544 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
4545 bitsize_unit_node));
4546 TYPE_ALIGN (record) = align;
4547 TYPE_IS_PADDING_P (record) = 1;
4548 TYPE_VOLATILE (record)
4549 = Present (gnat_entity) && Is_Volatile (gnat_entity);
4550 finish_record_type (record, field, 1, 0);
4552 /* Keep the RM_Size of the padded record as that of the old record
4553 if requested. */
4554 TYPE_ADA_SIZE (record) = same_rm_size ? size : rm_size (type);
4556 /* Unless debugging information isn't being written for the input type,
4557 write a record that shows what we are a subtype of and also make a
4558 variable that indicates our size, if variable. */
4559 if (TYPE_NAME (record) != 0
4560 && AGGREGATE_TYPE_P (type)
4561 && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
4562 || ! DECL_IGNORED_P (TYPE_NAME (type))))
4564 tree marker = make_node (RECORD_TYPE);
4565 tree name = DECL_NAME (TYPE_NAME (record));
4566 tree orig_name = TYPE_NAME (type);
4568 if (TREE_CODE (orig_name) == TYPE_DECL)
4569 orig_name = DECL_NAME (orig_name);
4571 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
4572 finish_record_type (marker,
4573 create_field_decl (orig_name, integer_type_node,
4574 marker, 0, NULL_TREE, NULL_TREE,
4576 0, 0);
4578 if (size != 0 && TREE_CODE (size) != INTEGER_CST && definition)
4579 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
4580 sizetype, TYPE_SIZE (record), 0, 0, 0, 0,
4584 type = record;
4586 if (TREE_CODE (orig_size) != INTEGER_CST
4587 && contains_placeholder_p (orig_size))
4588 orig_size = max_size (orig_size, 1);
4590 /* If the size was widened explicitly, maybe give a warning. */
4591 if (size != 0 && Present (gnat_entity)
4592 && ! operand_equal_p (size, orig_size, 0)
4593 && ! (TREE_CODE (size) == INTEGER_CST
4594 && TREE_CODE (orig_size) == INTEGER_CST
4595 && tree_int_cst_lt (size, orig_size)))
4597 Node_Id gnat_error_node = Empty;
4599 if (Is_Packed_Array_Type (gnat_entity))
4600 gnat_entity = Associated_Node_For_Itype (gnat_entity);
4602 if ((Ekind (gnat_entity) == E_Component
4603 || Ekind (gnat_entity) == E_Discriminant)
4604 && Present (Component_Clause (gnat_entity)))
4605 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
4606 else if (Present (Size_Clause (gnat_entity)))
4607 gnat_error_node = Expression (Size_Clause (gnat_entity));
4609 /* Generate message only for entities that come from source, since
4610 if we have an entity created by expansion, the message will be
4611 generated for some other corresponding source entity. */
4612 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
4613 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
4614 gnat_entity,
4615 size_diffop (size, orig_size));
4617 else if (*name_trailer == 'C' && ! Is_Internal (gnat_entity))
4618 post_error_ne_tree ("component of& padded{ by ^ bits}?",
4619 gnat_entity, gnat_entity,
4620 size_diffop (size, orig_size));
4623 return type;
4626 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
4627 the value passed against the list of choices. */
4629 tree
4630 choices_to_gnu (operand, choices)
4631 tree operand;
4632 Node_Id choices;
4634 Node_Id choice;
4635 Node_Id gnat_temp;
4636 tree result = integer_zero_node;
4637 tree this_test, low = 0, high = 0, single = 0;
4639 for (choice = First (choices); Present (choice); choice = Next (choice))
4641 switch (Nkind (choice))
4643 case N_Range:
4644 low = gnat_to_gnu (Low_Bound (choice));
4645 high = gnat_to_gnu (High_Bound (choice));
4647 /* There's no good type to use here, so we might as well use
4648 integer_type_node. */
4649 this_test
4650 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4651 build_binary_op (GE_EXPR, integer_type_node,
4652 operand, low),
4653 build_binary_op (LE_EXPR, integer_type_node,
4654 operand, high));
4656 break;
4658 case N_Subtype_Indication:
4659 gnat_temp = Range_Expression (Constraint (choice));
4660 low = gnat_to_gnu (Low_Bound (gnat_temp));
4661 high = gnat_to_gnu (High_Bound (gnat_temp));
4663 this_test
4664 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4665 build_binary_op (GE_EXPR, integer_type_node,
4666 operand, low),
4667 build_binary_op (LE_EXPR, integer_type_node,
4668 operand, high));
4669 break;
4671 case N_Identifier:
4672 case N_Expanded_Name:
4673 /* This represents either a subtype range, an enumeration
4674 literal, or a constant Ekind says which. If an enumeration
4675 literal or constant, fall through to the next case. */
4676 if (Ekind (Entity (choice)) != E_Enumeration_Literal
4677 && Ekind (Entity (choice)) != E_Constant)
4679 tree type = gnat_to_gnu_type (Entity (choice));
4681 low = TYPE_MIN_VALUE (type);
4682 high = TYPE_MAX_VALUE (type);
4684 this_test
4685 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4686 build_binary_op (GE_EXPR, integer_type_node,
4687 operand, low),
4688 build_binary_op (LE_EXPR, integer_type_node,
4689 operand, high));
4690 break;
4692 /* ... fall through ... */
4693 case N_Character_Literal:
4694 case N_Integer_Literal:
4695 single = gnat_to_gnu (choice);
4696 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
4697 single);
4698 break;
4700 case N_Others_Choice:
4701 this_test = integer_one_node;
4702 break;
4704 default:
4705 gigi_abort (114);
4708 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
4709 result, this_test);
4712 return result;
4715 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
4716 placed in GNU_RECORD_TYPE.
4718 PACKED is 1 if the enclosing record is packed and -1 if the enclosing
4719 record has a Component_Alignment of Storage_Unit.
4721 DEFINITION is nonzero if this field is for a record being defined. */
4723 static tree
4724 gnat_to_gnu_field (gnat_field, gnu_record_type, packed, definition)
4725 Entity_Id gnat_field;
4726 tree gnu_record_type;
4727 int packed;
4728 int definition;
4730 tree gnu_field_id = get_entity_name (gnat_field);
4731 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
4732 tree gnu_orig_field_type = gnu_field_type;
4733 tree gnu_pos = 0;
4734 tree gnu_size = 0;
4735 tree gnu_field;
4736 int needs_strict_alignment
4737 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
4738 || Is_Volatile (gnat_field));
4740 /* If this field requires strict alignment pretend it isn't packed. */
4741 if (needs_strict_alignment)
4742 packed = 0;
4744 /* For packed records, this is one of the few occasions on which we use
4745 the official RM size for discrete or fixed-point components, instead
4746 of the normal GNAT size stored in Esize. See description in Einfo:
4747 "Handling of Type'Size Values" for further details. */
4749 if (packed == 1)
4750 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
4751 gnat_field, FIELD_DECL, 0, 1);
4753 if (Known_Static_Esize (gnat_field))
4754 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
4755 gnat_field, FIELD_DECL, 0, 1);
4757 /* If we are packing this record and the field type is also a record
4758 that's BLKmode and with a small constant size, see if we can get a
4759 better form of the type that allows more packing. If we can, show
4760 a size was specified for it if there wasn't one so we know to
4761 make this a bitfield and avoid making things wider. */
4762 if (packed && TREE_CODE (gnu_field_type) == RECORD_TYPE
4763 && TYPE_MODE (gnu_field_type) == BLKmode
4764 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
4765 && compare_tree_int (TYPE_SIZE (gnu_field_type), BIGGEST_ALIGNMENT) <= 0)
4767 gnu_field_type = make_packable_type (gnu_field_type);
4769 if (gnu_field_type != gnu_orig_field_type && gnu_size == 0)
4770 gnu_size = rm_size (gnu_field_type);
4773 if (Present (Component_Clause (gnat_field)))
4775 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
4776 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
4777 gnat_field, FIELD_DECL, 0, 1);
4779 /* Ensure the position does not overlap with the parent subtype,
4780 if there is one. */
4781 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
4783 tree gnu_parent
4784 = gnat_to_gnu_type (Parent_Subtype
4785 (Underlying_Type (Scope (gnat_field))));
4787 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
4788 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
4790 post_error_ne_tree
4791 ("offset of& must be beyond parent{, minimum allowed is ^}",
4792 First_Bit (Component_Clause (gnat_field)), gnat_field,
4793 TYPE_SIZE_UNIT (gnu_parent));
4797 /* If this field needs strict alignment, ensure the record is
4798 sufficiently aligned and that that position and size are
4799 consistent with the alignment. */
4800 if (needs_strict_alignment)
4802 tree gnu_min_size = round_up (rm_size (gnu_field_type),
4803 TYPE_ALIGN (gnu_field_type));
4805 TYPE_ALIGN (gnu_record_type)
4806 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
4808 /* If Atomic, the size must match exactly and if aliased, the size
4809 must not be less than the rounded size. */
4810 if ((Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
4811 && ! operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
4813 post_error_ne_tree
4814 ("atomic field& must be natural size of type{ (^)}",
4815 Last_Bit (Component_Clause (gnat_field)), gnat_field,
4816 TYPE_SIZE (gnu_field_type));
4818 gnu_size = 0;
4821 else if (Is_Aliased (gnat_field)
4822 && gnu_size != 0
4823 && tree_int_cst_lt (gnu_size, gnu_min_size))
4825 post_error_ne_tree
4826 ("size of aliased field& too small{, minimum required is ^}",
4827 Last_Bit (Component_Clause (gnat_field)), gnat_field,
4828 gnu_min_size);
4829 gnu_size = 0;
4832 if (! integer_zerop (size_binop
4833 (TRUNC_MOD_EXPR, gnu_pos,
4834 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
4836 if (Is_Aliased (gnat_field))
4837 post_error_ne_num
4838 ("position of aliased field& must be multiple of ^ bits",
4839 Component_Clause (gnat_field), gnat_field,
4840 TYPE_ALIGN (gnu_field_type));
4842 else if (Is_Volatile (gnat_field))
4843 post_error_ne_num
4844 ("position of volatile field& must be multiple of ^ bits",
4845 First_Bit (Component_Clause (gnat_field)), gnat_field,
4846 TYPE_ALIGN (gnu_field_type));
4848 else if (Strict_Alignment (Etype (gnat_field)))
4849 post_error_ne_num
4850 ("position of & with aliased or tagged components not multiple of ^ bits",
4851 First_Bit (Component_Clause (gnat_field)), gnat_field,
4852 TYPE_ALIGN (gnu_field_type));
4853 else
4854 gigi_abort (124);
4856 gnu_pos = 0;
4859 /* If an error set the size to zero, show we have no position
4860 either. */
4861 if (gnu_size == 0)
4862 gnu_pos = 0;
4865 if (Is_Atomic (gnat_field))
4866 check_ok_for_atomic (gnu_field_type, gnat_field, 0);
4868 if (gnu_pos !=0 && TYPE_MODE (gnu_field_type) == BLKmode
4869 && (! integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
4870 bitsize_unit_node))))
4872 /* Try to see if we can make this a packable type. If we
4873 can, it's OK. */
4874 if (TREE_CODE (gnu_field_type) == RECORD_TYPE)
4875 gnu_field_type = make_packable_type (gnu_field_type);
4877 if (TYPE_MODE (gnu_field_type) == BLKmode)
4879 post_error_ne ("fields of& must start at storage unit boundary",
4880 First_Bit (Component_Clause (gnat_field)),
4881 Etype (gnat_field));
4882 gnu_pos = 0;
4887 /* If the record has rep clauses and this is the tag field, make a rep
4888 clause for it as well. */
4889 else if (Has_Specified_Layout (Scope (gnat_field))
4890 && Chars (gnat_field) == Name_uTag)
4892 gnu_pos = bitsize_zero_node;
4893 gnu_size = TYPE_SIZE (gnu_field_type);
4896 /* We need to make the size the maximum for the type if it is
4897 self-referential and an unconstrained type. */
4898 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
4899 && gnu_size == 0
4900 && ! TREE_CONSTANT (TYPE_SIZE (gnu_field_type))
4901 && contains_placeholder_p (TYPE_SIZE (gnu_field_type))
4902 && ! Is_Constrained (Underlying_Type (Etype (gnat_field))))
4903 gnu_size = max_size (TYPE_SIZE (gnu_field_type), 1);
4905 /* If no size is specified (or if there was an error), don't specify a
4906 position. */
4907 if (gnu_size == 0)
4908 gnu_pos = 0;
4909 else
4911 /* Unless this field is aliased, we can remove any left-justified
4912 modular type since it's only needed in the unchecked conversion
4913 case, which doesn't apply here. */
4914 if (! needs_strict_alignment
4915 && TREE_CODE (gnu_field_type) == RECORD_TYPE
4916 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_field_type))
4917 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
4919 gnu_field_type
4920 = make_type_from_size (gnu_field_type, gnu_size,
4921 Has_Biased_Representation (gnat_field));
4922 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0,
4923 gnat_field, "PAD", 0, definition, 1);
4926 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
4927 && TYPE_CONTAINS_TEMPLATE_P (gnu_field_type))
4928 gigi_abort (118);
4930 set_lineno (gnat_field, 0);
4931 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
4932 packed, gnu_size, gnu_pos,
4933 Is_Aliased (gnat_field));
4935 TREE_THIS_VOLATILE (gnu_field) = Is_Volatile (gnat_field);
4937 if (Ekind (gnat_field) == E_Discriminant)
4938 DECL_DISCRIMINANT_NUMBER (gnu_field)
4939 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
4941 return gnu_field;
4944 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
4945 of GCC trees for fields that are in the record and have already been
4946 processed. When called from gnat_to_gnu_entity during the processing of a
4947 record type definition, the GCC nodes for the discriminants will be on
4948 the chain. The other calls to this function are recursive calls from
4949 itself for the Component_List of a variant and the chain is empty.
4951 PACKED is 1 if this is for a record with "pragma pack" and -1 is this is
4952 for a record type with "pragma component_alignment (storage_unit)".
4954 FINISH_RECORD is nonzero if this call will supply all of the remaining
4955 fields of the record.
4957 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
4958 with a rep clause is to be added. If it is nonzero, that is all that
4959 should be done with such fields.
4961 CANCEL_ALIGNMENT, if nonzero, means the alignment should be zeroed
4962 before laying out the record. This means the alignment only serves
4963 to force fields to be bitfields, but not require the record to be
4964 that aligned. This is used for variants.
4966 ALL_REP, if nonzero, means that a rep clause was found for all the
4967 fields. This simplifies the logic since we know we're not in the mixed
4968 case.
4970 The processing of the component list fills in the chain with all of the
4971 fields of the record and then the record type is finished. */
4973 static void
4974 components_to_record (gnu_record_type, component_list, gnu_field_list, packed,
4975 definition, p_gnu_rep_list, cancel_alignment, all_rep)
4976 tree gnu_record_type;
4977 Node_Id component_list;
4978 tree gnu_field_list;
4979 int packed;
4980 int definition;
4981 tree *p_gnu_rep_list;
4982 int cancel_alignment;
4983 int all_rep;
4985 Node_Id component_decl;
4986 Entity_Id gnat_field;
4987 Node_Id variant_part;
4988 Node_Id variant;
4989 tree gnu_our_rep_list = NULL_TREE;
4990 tree gnu_field, gnu_last;
4991 int layout_with_rep = 0;
4993 /* For each variable within each component declaration create a GCC field
4994 and add it to the list, skipping any pragmas in the list. */
4996 if (Present (Component_Items (component_list)))
4997 for (component_decl = First_Non_Pragma (Component_Items (component_list));
4998 Present (component_decl);
4999 component_decl = Next_Non_Pragma (component_decl))
5001 gnat_field = Defining_Entity (component_decl);
5003 if (Chars (gnat_field) == Name_uParent)
5004 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
5005 else
5007 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
5008 packed, definition);
5010 /* If this is the _Tag field, put it before any discriminants,
5011 instead of after them as is the case for all other fields. */
5012 if (Chars (gnat_field) == Name_uTag)
5013 gnu_field_list = chainon (gnu_field_list, gnu_field);
5014 else
5016 TREE_CHAIN (gnu_field) = gnu_field_list;
5017 gnu_field_list = gnu_field;
5021 save_gnu_tree (gnat_field, gnu_field, 0);
5024 /* At the end of the component list there may be a variant part. */
5025 variant_part = Variant_Part (component_list);
5027 /* If this is an unchecked union, each variant must have exactly one
5028 component, each of which becomes one component of this union. */
5029 if (TREE_CODE (gnu_record_type) == UNION_TYPE && Present (variant_part))
5030 for (variant = First_Non_Pragma (Variants (variant_part));
5031 Present (variant);
5032 variant = Next_Non_Pragma (variant))
5034 component_decl
5035 = First_Non_Pragma (Component_Items (Component_List (variant)));
5036 gnat_field = Defining_Entity (component_decl);
5037 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
5038 definition);
5039 TREE_CHAIN (gnu_field) = gnu_field_list;
5040 gnu_field_list = gnu_field;
5041 save_gnu_tree (gnat_field, gnu_field, 0);
5044 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
5045 mutually exclusive and should go in the same memory. To do this we need
5046 to treat each variant as a record whose elements are created from the
5047 component list for the variant. So here we create the records from the
5048 lists for the variants and put them all into the QUAL_UNION_TYPE. */
5049 else if (Present (variant_part))
5051 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
5052 Node_Id variant;
5053 tree gnu_union_type = make_node (QUAL_UNION_TYPE);
5054 tree gnu_union_field;
5055 tree gnu_variant_list = NULL_TREE;
5056 tree gnu_name = TYPE_NAME (gnu_record_type);
5057 tree gnu_var_name
5058 = concat_id_with_name
5059 (get_identifier (Get_Name_String (Chars (Name (variant_part)))),
5060 "XVN");
5062 if (TREE_CODE (gnu_name) == TYPE_DECL)
5063 gnu_name = DECL_NAME (gnu_name);
5065 TYPE_NAME (gnu_union_type)
5066 = concat_id_with_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
5067 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
5069 for (variant = First_Non_Pragma (Variants (variant_part));
5070 Present (variant);
5071 variant = Next_Non_Pragma (variant))
5073 tree gnu_variant_type = make_node (RECORD_TYPE);
5074 tree gnu_inner_name;
5075 tree gnu_qual;
5077 Get_Variant_Encoding (variant);
5078 gnu_inner_name = get_identifier (Name_Buffer);
5079 TYPE_NAME (gnu_variant_type)
5080 = concat_id_with_name (TYPE_NAME (gnu_union_type),
5081 IDENTIFIER_POINTER (gnu_inner_name));
5083 /* Set the alignment of the inner type in case we need to make
5084 inner objects into bitfields, but then clear it out
5085 so the record actually gets only the alignment required. */
5086 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
5087 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
5088 components_to_record (gnu_variant_type, Component_List (variant),
5089 NULL_TREE, packed, definition,
5090 &gnu_our_rep_list, 1, all_rep);
5092 gnu_qual = choices_to_gnu (gnu_discriminant,
5093 Discrete_Choices (variant));
5095 Set_Present_Expr (variant, annotate_value (gnu_qual));
5096 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
5097 gnu_union_type, 0, 0, 0, 1);
5098 DECL_INTERNAL_P (gnu_field) = 1;
5099 DECL_QUALIFIER (gnu_field) = gnu_qual;
5100 TREE_CHAIN (gnu_field) = gnu_variant_list;
5101 gnu_variant_list = gnu_field;
5104 /* We can delete any empty variants from the end. This may leave none
5105 left. Note we cannot delete variants from anywhere else. */
5106 while (gnu_variant_list != 0
5107 && TYPE_FIELDS (TREE_TYPE (gnu_variant_list)) == 0)
5108 gnu_variant_list = TREE_CHAIN (gnu_variant_list);
5110 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
5111 if (gnu_variant_list != 0)
5113 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
5114 0, 0);
5116 gnu_union_field
5117 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
5118 packed,
5119 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
5120 all_rep ? bitsize_zero_node : 0, 1);
5122 DECL_INTERNAL_P (gnu_union_field) = 1;
5123 TREE_CHAIN (gnu_union_field) = gnu_field_list;
5124 gnu_field_list = gnu_union_field;
5128 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
5129 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
5130 in a separate pass since we want to handle the discriminants but can't
5131 play with them until we've used them in debugging data above.
5133 ??? Note: if we then reorder them, debugging information will be wrong,
5134 but there's nothing that can be done about this at the moment. */
5136 for (gnu_field = gnu_field_list, gnu_last = 0; gnu_field; )
5138 if (DECL_FIELD_OFFSET (gnu_field) != 0)
5140 tree gnu_next = TREE_CHAIN (gnu_field);
5142 if (gnu_last == 0)
5143 gnu_field_list = gnu_next;
5144 else
5145 TREE_CHAIN (gnu_last) = gnu_next;
5147 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
5148 gnu_our_rep_list = gnu_field;
5149 gnu_field = gnu_next;
5151 else
5153 gnu_last = gnu_field;
5154 gnu_field = TREE_CHAIN (gnu_field);
5158 /* If we have any items in our rep'ed field list, it is not the case that all
5159 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
5160 set it and ignore the items. Otherwise, sort the fields by bit position
5161 and put them into their own record if we have any fields without
5162 rep clauses. */
5163 if (gnu_our_rep_list != 0 && p_gnu_rep_list != 0 && ! all_rep)
5164 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
5165 else if (gnu_our_rep_list != 0)
5167 tree gnu_rep_type
5168 = gnu_field_list == 0 ? gnu_record_type : make_node (RECORD_TYPE);
5169 int len = list_length (gnu_our_rep_list);
5170 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
5171 int i;
5173 /* Set DECL_SECTION_NAME to increasing integers so we have a
5174 stable sort. */
5175 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
5176 gnu_field = TREE_CHAIN (gnu_field), i++)
5178 gnu_arr[i] = gnu_field;
5179 DECL_SECTION_NAME (gnu_field) = size_int (i);
5182 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
5184 /* Put the fields in the list in order of increasing position, which
5185 means we start from the end. */
5186 gnu_our_rep_list = NULL_TREE;
5187 for (i = len - 1; i >= 0; i--)
5189 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
5190 gnu_our_rep_list = gnu_arr[i];
5191 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
5192 DECL_SECTION_NAME (gnu_arr[i]) = 0;
5195 if (gnu_field_list != 0)
5197 finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, 0);
5198 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
5199 gnu_record_type, 0, 0, 0, 1);
5200 DECL_INTERNAL_P (gnu_field) = 1;
5201 gnu_field_list = chainon (gnu_field_list, gnu_field);
5203 else
5205 layout_with_rep = 1;
5206 gnu_field_list = nreverse (gnu_our_rep_list);
5210 if (cancel_alignment)
5211 TYPE_ALIGN (gnu_record_type) = 0;
5213 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
5214 layout_with_rep, 0);
5217 /* Called via qsort from the above. Returns -1, 1, depending on the
5218 bit positions and ordinals of the two fields. */
5220 static int
5221 compare_field_bitpos (rt1, rt2)
5222 const PTR rt1;
5223 const PTR rt2;
5225 tree *t1 = (tree *) rt1;
5226 tree *t2 = (tree *) rt2;
5228 if (tree_int_cst_equal (bit_position (*t1), bit_position (*t2)))
5229 return
5230 (tree_int_cst_lt (DECL_SECTION_NAME (*t1), DECL_SECTION_NAME (*t2))
5231 ? -1 : 1);
5232 else if (tree_int_cst_lt (bit_position (*t1), bit_position (*t2)))
5233 return -1;
5234 else
5235 return 1;
5238 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
5239 placed into an Esize, Component_Bit_Offset, or Component_Size value
5240 in the GNAT tree. */
5242 static Uint
5243 annotate_value (gnu_size)
5244 tree gnu_size;
5246 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
5247 TCode tcode;
5248 Node_Ref_Or_Val ops[3];
5249 int i;
5250 int size;
5252 /* If we do not return inside this switch, TCODE will be set to the
5253 code to use for a Create_Node operand and LEN (set above) will be
5254 the number of recursive calls for us to make. */
5256 switch (TREE_CODE (gnu_size))
5258 case INTEGER_CST:
5259 if (TREE_OVERFLOW (gnu_size))
5260 return No_Uint;
5262 /* This may have come from a conversion from some smaller type,
5263 so ensure this is in bitsizetype. */
5264 gnu_size = convert (bitsizetype, gnu_size);
5266 /* For negative values, use NEGATE_EXPR of the supplied value. */
5267 if (tree_int_cst_sgn (gnu_size) < 0)
5269 /* The rediculous code below is to handle the case of the largest
5270 negative integer. */
5271 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
5272 int adjust = 0;
5273 tree temp;
5275 if (TREE_CONSTANT_OVERFLOW (negative_size))
5277 negative_size
5278 = size_binop (MINUS_EXPR, bitsize_zero_node,
5279 size_binop (PLUS_EXPR, gnu_size,
5280 bitsize_one_node));
5281 adjust = 1;
5284 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
5285 if (adjust)
5286 temp = build (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
5288 return annotate_value (temp);
5291 if (! host_integerp (gnu_size, 1))
5292 return No_Uint;
5294 size = tree_low_cst (gnu_size, 1);
5296 /* This peculiar test is to make sure that the size fits in an int
5297 on machines where HOST_WIDE_INT is not "int". */
5298 if (tree_low_cst (gnu_size, 1) == size)
5299 return UI_From_Int (size);
5300 else
5301 return No_Uint;
5303 case COMPONENT_REF:
5304 /* The only case we handle here is a simple discriminant reference. */
5305 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
5306 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
5307 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)) != 0)
5308 return Create_Node (Discrim_Val,
5309 annotate_value (DECL_DISCRIMINANT_NUMBER
5310 (TREE_OPERAND (gnu_size, 1))),
5311 No_Uint, No_Uint);
5312 else
5313 return No_Uint;
5315 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
5316 return annotate_value (TREE_OPERAND (gnu_size, 0));
5318 /* Now just list the operations we handle. */
5319 case COND_EXPR: tcode = Cond_Expr; break;
5320 case PLUS_EXPR: tcode = Plus_Expr; break;
5321 case MINUS_EXPR: tcode = Minus_Expr; break;
5322 case MULT_EXPR: tcode = Mult_Expr; break;
5323 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
5324 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
5325 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
5326 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
5327 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
5328 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
5329 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
5330 case NEGATE_EXPR: tcode = Negate_Expr; break;
5331 case MIN_EXPR: tcode = Min_Expr; break;
5332 case MAX_EXPR: tcode = Max_Expr; break;
5333 case ABS_EXPR: tcode = Abs_Expr; break;
5334 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
5335 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
5336 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
5337 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
5338 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
5339 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
5340 case LT_EXPR: tcode = Lt_Expr; break;
5341 case LE_EXPR: tcode = Le_Expr; break;
5342 case GT_EXPR: tcode = Gt_Expr; break;
5343 case GE_EXPR: tcode = Ge_Expr; break;
5344 case EQ_EXPR: tcode = Eq_Expr; break;
5345 case NE_EXPR: tcode = Ne_Expr; break;
5347 default:
5348 return No_Uint;
5351 /* Now get each of the operands that's relevant for this code. If any
5352 cannot be expressed as a repinfo node, say we can't. */
5353 for (i = 0; i < 3; i++)
5354 ops[i] = No_Uint;
5356 for (i = 0; i < len; i++)
5358 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
5359 if (ops[i] == No_Uint)
5360 return No_Uint;
5363 return Create_Node (tcode, ops[0], ops[1], ops[2]);
5366 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
5367 GCC type, set Component_Bit_Offset and Esize to the position and size
5368 used by Gigi. */
5370 static void
5371 annotate_rep (gnat_entity, gnu_type)
5372 Entity_Id gnat_entity;
5373 tree gnu_type;
5375 tree gnu_list;
5376 tree gnu_entry;
5377 Entity_Id gnat_field;
5379 /* We operate by first making a list of all field and their positions
5380 (we can get the sizes easily at any time) by a recursive call
5381 and then update all the sizes into the tree. */
5382 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
5383 size_zero_node, bitsize_zero_node);
5385 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
5386 gnat_field = Next_Entity (gnat_field))
5387 if ((Ekind (gnat_field) == E_Component
5388 || (Ekind (gnat_field) == E_Discriminant
5389 && ! Is_Unchecked_Union (Scope (gnat_field))))
5390 && 0 != (gnu_entry = purpose_member (gnat_to_gnu_entity (gnat_field,
5391 NULL_TREE, 0),
5392 gnu_list)))
5394 Set_Component_Bit_Offset
5395 (gnat_field,
5396 annotate_value (bit_from_pos
5397 (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
5398 TREE_VALUE (TREE_VALUE (gnu_entry)))));
5400 Set_Esize (gnat_field,
5401 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
5405 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is
5406 the FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the
5407 byte position and TREE_VALUE being the bit position. GNU_POS is to
5408 be added to the position, GNU_BITPOS to the bit position, and GNU_LIST
5409 is the entries so far. */
5411 static tree
5412 compute_field_positions (gnu_type, gnu_list, gnu_pos, gnu_bitpos)
5413 tree gnu_type;
5414 tree gnu_list;
5415 tree gnu_pos;
5416 tree gnu_bitpos;
5418 tree gnu_field;
5419 tree gnu_result = gnu_list;
5421 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
5422 gnu_field = TREE_CHAIN (gnu_field))
5424 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
5425 DECL_FIELD_BIT_OFFSET (gnu_field));
5426 tree gnu_our_pos = size_binop (PLUS_EXPR, gnu_pos,
5427 DECL_FIELD_OFFSET (gnu_field));
5429 gnu_result
5430 = tree_cons (gnu_field,
5431 tree_cons (gnu_our_pos, gnu_our_bitpos, NULL_TREE),
5432 gnu_result);
5434 if (DECL_INTERNAL_P (gnu_field))
5435 gnu_result
5436 = compute_field_positions (TREE_TYPE (gnu_field),
5437 gnu_result, gnu_our_pos, gnu_our_bitpos);
5440 return gnu_result;
5443 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
5444 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
5445 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
5446 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
5447 for the size of a field. COMPONENT_P is true if we are being called
5448 to process the Component_Size of GNAT_OBJECT. This is used for error
5449 message handling and to indicate to use the object size of GNU_TYPE.
5450 ZERO_OK is nonzero if a size of zero is permitted; if ZERO_OK is zero,
5451 it means that a size of zero should be treated as an unspecified size. */
5453 static tree
5454 validate_size (uint_size, gnu_type, gnat_object, kind, component_p, zero_ok)
5455 Uint uint_size;
5456 tree gnu_type;
5457 Entity_Id gnat_object;
5458 enum tree_code kind;
5459 int component_p;
5460 int zero_ok;
5462 Node_Id gnat_error_node;
5463 tree type_size
5464 = kind == VAR_DECL ? TYPE_SIZE (gnu_type) : rm_size (gnu_type);
5465 tree size;
5467 if (type_size != 0 && TREE_CODE (type_size) != INTEGER_CST
5468 && contains_placeholder_p (type_size))
5469 type_size = max_size (type_size, 1);
5471 if (TYPE_FAT_POINTER_P (gnu_type))
5472 type_size = bitsize_int (POINTER_SIZE);
5474 if ((Ekind (gnat_object) == E_Component
5475 || Ekind (gnat_object) == E_Discriminant)
5476 && Present (Component_Clause (gnat_object)))
5477 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
5478 else if (Present (Size_Clause (gnat_object)))
5479 gnat_error_node = Expression (Size_Clause (gnat_object));
5480 else
5481 gnat_error_node = gnat_object;
5483 /* Don't give errors on packed array types; we'll be giving the error on
5484 the type itself soon enough. */
5485 if (Is_Packed_Array_Type (gnat_object))
5486 gnat_error_node = Empty;
5488 /* Get the size as a tree. Return 0 if none was specified, either because
5489 Esize was not Present or if the specified size was zero. Give an error
5490 if a size was specified, but cannot be represented as in sizetype. If
5491 the size is negative, it was a back-annotation of a variable size and
5492 should be treated as not specified. */
5493 if (No (uint_size) || uint_size == No_Uint)
5494 return 0;
5496 size = UI_To_gnu (uint_size, bitsizetype);
5497 if (TREE_OVERFLOW (size))
5499 if (component_p)
5500 post_error_ne ("component size of & is too large",
5501 gnat_error_node, gnat_object);
5502 else
5503 post_error_ne ("size of & is too large", gnat_error_node, gnat_object);
5505 return 0;
5508 /* Ignore a negative size since that corresponds to our back-annotation.
5509 Also ignore a zero size unless a size clause exists. */
5510 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && ! zero_ok))
5511 return 0;
5513 /* The size of objects is always a multiple of a byte. */
5514 if (kind == VAR_DECL
5515 && ! integer_zerop (size_binop (TRUNC_MOD_EXPR, size,
5516 bitsize_unit_node)))
5518 if (component_p)
5519 post_error_ne ("component size for& is not a multiple of Storage_Unit",
5520 gnat_error_node, gnat_object);
5521 else
5522 post_error_ne ("size for& is not a multiple of Storage_Unit",
5523 gnat_error_node, gnat_object);
5524 return 0;
5527 /* If this is an integral type, the front-end has verified the size, so we
5528 need not do it here (which would entail checking against the bounds).
5529 However, if this is an aliased object, it may not be smaller than the
5530 type of the object. */
5531 if (INTEGRAL_TYPE_P (gnu_type) && ! TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
5532 && ! (kind == VAR_DECL && Is_Aliased (gnat_object)))
5533 return size;
5535 /* If the object is a record that contains a template, add the size of
5536 the template to the specified size. */
5537 if (TREE_CODE (gnu_type) == RECORD_TYPE
5538 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
5539 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
5541 /* If the size of the object is a constant, the new size must not be
5542 smaller. */
5543 if (TREE_CODE (type_size) != INTEGER_CST
5544 || TREE_OVERFLOW (type_size)
5545 || tree_int_cst_lt (size, type_size))
5547 if (component_p)
5548 post_error_ne_tree
5549 ("component size for& too small{, minimum allowed is ^}",
5550 gnat_error_node, gnat_object, type_size);
5551 else
5552 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
5553 gnat_error_node, gnat_object, type_size);
5555 if (kind == VAR_DECL && ! component_p
5556 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
5557 && ! tree_int_cst_lt (size, rm_size (gnu_type)))
5558 post_error_ne_tree_2
5559 ("\\size of ^ is not a multiple of alignment (^ bits)",
5560 gnat_error_node, gnat_object, rm_size (gnu_type),
5561 TYPE_ALIGN (gnu_type));
5563 else if (INTEGRAL_TYPE_P (gnu_type))
5564 post_error_ne ("\\size would be legal if & were not aliased!",
5565 gnat_error_node, gnat_object);
5567 return 0;
5570 return size;
5573 /* Similarly, but both validate and process a value of RM_Size. This
5574 routine is only called for types. */
5576 static void
5577 set_rm_size (uint_size, gnu_type, gnat_entity)
5578 Uint uint_size;
5579 tree gnu_type;
5580 Entity_Id gnat_entity;
5582 /* Only give an error if a Value_Size clause was explicitly given.
5583 Otherwise, we'd be duplicating an error on the Size clause. */
5584 Node_Id gnat_attr_node
5585 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
5586 tree old_size = rm_size (gnu_type);
5587 tree size;
5589 /* Get the size as a tree. Do nothing if none was specified, either
5590 because RM_Size was not Present or if the specified size was zero.
5591 Give an error if a size was specified, but cannot be represented as
5592 in sizetype. */
5593 if (No (uint_size) || uint_size == No_Uint)
5594 return;
5596 size = UI_To_gnu (uint_size, bitsizetype);
5597 if (TREE_OVERFLOW (size))
5599 if (Present (gnat_attr_node))
5600 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
5601 gnat_entity);
5603 return;
5606 /* Ignore a negative size since that corresponds to our back-annotation.
5607 Also ignore a zero size unless a size clause exists, a Value_Size
5608 clause exists, or this is an integer type, in which case the
5609 front end will have always set it. */
5610 else if (tree_int_cst_sgn (size) < 0
5611 || (integer_zerop (size) && No (gnat_attr_node)
5612 && ! Has_Size_Clause (gnat_entity)
5613 && ! Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
5614 return;
5616 /* If the old size is self-referential, get the maximum size. */
5617 if (TREE_CODE (old_size) != INTEGER_CST
5618 && contains_placeholder_p (old_size))
5619 old_size = max_size (old_size, 1);
5621 /* If the size of the object is a constant, the new size must not be
5622 smaller (the front end checks this for scalar types). */
5623 if (TREE_CODE (old_size) != INTEGER_CST
5624 || TREE_OVERFLOW (old_size)
5625 || (AGGREGATE_TYPE_P (gnu_type)
5626 && tree_int_cst_lt (size, old_size)))
5628 if (Present (gnat_attr_node))
5629 post_error_ne_tree
5630 ("Value_Size for& too small{, minimum allowed is ^}",
5631 gnat_attr_node, gnat_entity, old_size);
5633 return;
5636 /* Otherwise, set the RM_Size. */
5637 if (TREE_CODE (gnu_type) == INTEGER_TYPE
5638 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
5639 TYPE_RM_SIZE_INT (gnu_type) = size;
5640 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
5641 TYPE_RM_SIZE_ENUM (gnu_type) = size;
5642 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
5643 || TREE_CODE (gnu_type) == UNION_TYPE
5644 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
5645 && ! TYPE_IS_FAT_POINTER_P (gnu_type))
5646 TYPE_ADA_SIZE (gnu_type) = size;
5649 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
5650 If TYPE is the best type, return it. Otherwise, make a new type. We
5651 only support new integral and pointer types. BIASED_P is nonzero if
5652 we are making a biased type. */
5654 static tree
5655 make_type_from_size (type, size_tree, biased_p)
5656 tree type;
5657 tree size_tree;
5658 int biased_p;
5660 tree new_type;
5661 unsigned HOST_WIDE_INT size;
5663 /* If size indicates an error, just return TYPE to avoid propagating the
5664 error. Likewise if it's too large to represent. */
5665 if (size_tree == 0 || ! host_integerp (size_tree, 1))
5666 return type;
5668 size = tree_low_cst (size_tree, 1);
5669 switch (TREE_CODE (type))
5671 case INTEGER_TYPE:
5672 case ENUMERAL_TYPE:
5673 /* Only do something if the type is not already the proper size and is
5674 not a packed array type. */
5675 if (TYPE_PACKED_ARRAY_TYPE_P (type)
5676 || (TYPE_PRECISION (type) == size
5677 && biased_p == (TREE_CODE (type) == INTEGER_CST
5678 && TYPE_BIASED_REPRESENTATION_P (type))))
5679 break;
5681 size = MIN (size, LONG_LONG_TYPE_SIZE);
5682 new_type = make_signed_type (size);
5683 TREE_TYPE (new_type)
5684 = TREE_TYPE (type) != 0 ? TREE_TYPE (type) : type;
5685 TYPE_MIN_VALUE (new_type)
5686 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
5687 TYPE_MAX_VALUE (new_type)
5688 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
5689 TYPE_BIASED_REPRESENTATION_P (new_type)
5690 = ((TREE_CODE (type) == INTEGER_TYPE
5691 && TYPE_BIASED_REPRESENTATION_P (type))
5692 || biased_p);
5693 TREE_UNSIGNED (new_type)
5694 = TREE_UNSIGNED (type) | TYPE_BIASED_REPRESENTATION_P (new_type);
5695 TYPE_RM_SIZE_INT (new_type) = bitsize_int (size);
5696 return new_type;
5698 case RECORD_TYPE:
5699 /* Do something if this is a fat pointer, in which case we
5700 may need to return the thin pointer. */
5701 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
5702 return
5703 build_pointer_type
5704 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
5705 break;
5707 case POINTER_TYPE:
5708 /* Only do something if this is a thin pointer, in which case we
5709 may need to return the fat pointer. */
5710 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
5711 return
5712 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
5714 break;
5716 default:
5717 break;
5720 return type;
5723 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
5724 a type or object whose present alignment is ALIGN. If this alignment is
5725 valid, return it. Otherwise, give an error and return ALIGN. */
5727 static unsigned int
5728 validate_alignment (alignment, gnat_entity, align)
5729 Uint alignment;
5730 Entity_Id gnat_entity;
5731 unsigned int align;
5733 Node_Id gnat_error_node = gnat_entity;
5734 unsigned int new_align;
5736 #ifndef MAX_OFILE_ALIGNMENT
5737 #define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
5738 #endif
5740 if (Present (Alignment_Clause (gnat_entity)))
5741 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
5743 /* Within GCC, an alignment is an integer, so we must make sure a
5744 value is specified that fits in that range. Also, alignments of
5745 more than MAX_OFILE_ALIGNMENT can't be supported. */
5747 if (! UI_Is_In_Int_Range (alignment)
5748 || ((new_align = UI_To_Int (alignment))
5749 > MAX_OFILE_ALIGNMENT / BITS_PER_UNIT))
5750 post_error_ne_num ("largest supported alignment for& is ^",
5751 gnat_error_node, gnat_entity,
5752 MAX_OFILE_ALIGNMENT / BITS_PER_UNIT);
5753 else if (! (Present (Alignment_Clause (gnat_entity))
5754 && From_At_Mod (Alignment_Clause (gnat_entity)))
5755 && new_align * BITS_PER_UNIT < align)
5756 post_error_ne_num ("alignment for& must be at least ^",
5757 gnat_error_node, gnat_entity,
5758 align / BITS_PER_UNIT);
5759 else
5760 align = MAX (align, new_align == 0 ? 1 : new_align * BITS_PER_UNIT);
5762 return align;
5765 /* Verify that OBJECT, a type or decl, is something we can implement
5766 atomically. If not, give an error for GNAT_ENTITY. COMP_P is nonzero
5767 if we require atomic components. */
5769 static void
5770 check_ok_for_atomic (object, gnat_entity, comp_p)
5771 tree object;
5772 Entity_Id gnat_entity;
5773 int comp_p;
5775 Node_Id gnat_error_point = gnat_entity;
5776 Node_Id gnat_node;
5777 enum machine_mode mode;
5778 unsigned int align;
5779 tree size;
5781 /* There are three case of what OBJECT can be. It can be a type, in which
5782 case we take the size, alignment and mode from the type. It can be a
5783 declaration that was indirect, in which case the relevant values are
5784 that of the type being pointed to, or it can be a normal declaration,
5785 in which case the values are of the decl. The code below assumes that
5786 OBJECT is either a type or a decl. */
5787 if (TYPE_P (object))
5789 mode = TYPE_MODE (object);
5790 align = TYPE_ALIGN (object);
5791 size = TYPE_SIZE (object);
5793 else if (DECL_BY_REF_P (object))
5795 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
5796 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
5797 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
5799 else
5801 mode = DECL_MODE (object);
5802 align = DECL_ALIGN (object);
5803 size = DECL_SIZE (object);
5806 /* Consider all floating-point types atomic and any types that that are
5807 represented by integers no wider than a machine word. */
5808 if (GET_MODE_CLASS (mode) == MODE_FLOAT
5809 || ((GET_MODE_CLASS (mode) == MODE_INT
5810 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5811 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
5812 return;
5814 /* For the moment, also allow anything that has an alignment equal
5815 to its size and which is smaller than a word. */
5816 if (TREE_CODE (size) == INTEGER_CST
5817 && compare_tree_int (size, align) == 0
5818 && align <= BITS_PER_WORD)
5819 return;
5821 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
5822 gnat_node = Next_Rep_Item (gnat_node))
5824 if (! comp_p && Nkind (gnat_node) == N_Pragma
5825 && Get_Pragma_Id (Chars (gnat_node)) == Pragma_Atomic)
5826 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
5827 else if (comp_p && Nkind (gnat_node) == N_Pragma
5828 && (Get_Pragma_Id (Chars (gnat_node))
5829 == Pragma_Atomic_Components))
5830 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
5833 if (comp_p)
5834 post_error_ne ("atomic access to component of & cannot be guaranteed",
5835 gnat_error_point, gnat_entity);
5836 else
5837 post_error_ne ("atomic access to & cannot be guaranteed",
5838 gnat_error_point, gnat_entity);
5841 /* Given a type T, a FIELD_DECL F, and a replacement value R,
5842 return a new type with all size expressions that contain F
5843 updated by replacing F with R. This is identical to GCC's
5844 substitute_in_type except that it knows about TYPE_INDEX_TYPE.
5845 If F is NULL_TREE, always make a new RECORD_TYPE, even if nothing has
5846 changed. */
5848 tree
5849 gnat_substitute_in_type (t, f, r)
5850 tree t, f, r;
5852 tree new = t;
5853 tree tem;
5855 switch (TREE_CODE (t))
5857 case INTEGER_TYPE:
5858 case ENUMERAL_TYPE:
5859 case BOOLEAN_TYPE:
5860 case CHAR_TYPE:
5861 if ((TREE_CODE (TYPE_MIN_VALUE (t)) != INTEGER_CST
5862 && contains_placeholder_p (TYPE_MIN_VALUE (t)))
5863 || (TREE_CODE (TYPE_MAX_VALUE (t)) != INTEGER_CST
5864 && contains_placeholder_p (TYPE_MAX_VALUE (t))))
5866 tree low = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
5867 tree high = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
5869 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
5870 return t;
5872 new = build_range_type (TREE_TYPE (t), low, high);
5873 if (TYPE_INDEX_TYPE (t))
5874 TYPE_INDEX_TYPE (new)
5875 = gnat_substitute_in_type (TYPE_INDEX_TYPE (t), f, r);
5876 return new;
5879 return t;
5881 case REAL_TYPE:
5882 if ((TYPE_MIN_VALUE (t) != 0
5883 && TREE_CODE (TYPE_MIN_VALUE (t)) != REAL_CST
5884 && contains_placeholder_p (TYPE_MIN_VALUE (t)))
5885 || (TYPE_MAX_VALUE (t) != 0
5886 && TREE_CODE (TYPE_MAX_VALUE (t)) != REAL_CST
5887 && contains_placeholder_p (TYPE_MAX_VALUE (t))))
5889 tree low = 0, high = 0;
5891 if (TYPE_MIN_VALUE (t))
5892 low = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
5893 if (TYPE_MAX_VALUE (t))
5894 high = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
5896 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
5897 return t;
5899 t = copy_type (t);
5900 TYPE_MIN_VALUE (t) = low;
5901 TYPE_MAX_VALUE (t) = high;
5903 return t;
5905 case COMPLEX_TYPE:
5906 tem = gnat_substitute_in_type (TREE_TYPE (t), f, r);
5907 if (tem == TREE_TYPE (t))
5908 return t;
5910 return build_complex_type (tem);
5912 case OFFSET_TYPE:
5913 case METHOD_TYPE:
5914 case FILE_TYPE:
5915 case SET_TYPE:
5916 case FUNCTION_TYPE:
5917 case LANG_TYPE:
5918 /* Don't know how to do these yet. */
5919 abort ();
5921 case ARRAY_TYPE:
5923 tree component = gnat_substitute_in_type (TREE_TYPE (t), f, r);
5924 tree domain = gnat_substitute_in_type (TYPE_DOMAIN (t), f, r);
5926 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
5927 return t;
5929 new = build_array_type (component, domain);
5930 TYPE_SIZE (new) = 0;
5931 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
5932 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
5933 layout_type (new);
5934 TYPE_ALIGN (new) = TYPE_ALIGN (t);
5935 return new;
5938 case RECORD_TYPE:
5939 case UNION_TYPE:
5940 case QUAL_UNION_TYPE:
5942 tree field;
5943 int changed_field
5944 = (f == NULL_TREE && ! TREE_CONSTANT (TYPE_SIZE (t)));
5945 int field_has_rep = 0;
5946 tree last_field = 0;
5948 tree new = copy_type (t);
5950 /* Start out with no fields, make new fields, and chain them
5951 in. If we haven't actually changed the type of any field,
5952 discard everything we've done and return the old type. */
5954 TYPE_FIELDS (new) = 0;
5955 TYPE_SIZE (new) = 0;
5957 for (field = TYPE_FIELDS (t); field;
5958 field = TREE_CHAIN (field))
5960 tree new_field = copy_node (field);
5962 TREE_TYPE (new_field)
5963 = gnat_substitute_in_type (TREE_TYPE (new_field), f, r);
5965 if (DECL_HAS_REP_P (field) && ! DECL_INTERNAL_P (field))
5966 field_has_rep = 1;
5967 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
5968 changed_field = 1;
5970 /* If this is an internal field and the type of this field is
5971 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
5972 the type just has one element, treat that as the field.
5973 But don't do this if we are processing a QUAL_UNION_TYPE. */
5974 if (TREE_CODE (t) != QUAL_UNION_TYPE
5975 && DECL_INTERNAL_P (new_field)
5976 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
5977 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
5979 if (TYPE_FIELDS (TREE_TYPE (new_field)) == 0)
5980 continue;
5982 if (TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))) == 0)
5984 tree next_new_field
5985 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
5987 /* Make sure omitting the union doesn't change
5988 the layout. */
5989 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
5990 new_field = next_new_field;
5994 DECL_CONTEXT (new_field) = new;
5995 DECL_ORIGINAL_FIELD (new_field)
5996 = DECL_ORIGINAL_FIELD (field) != 0
5997 ? DECL_ORIGINAL_FIELD (field) : field;
5999 /* If the size of the old field was set at a constant,
6000 propagate the size in case the type's size was variable.
6001 (This occurs in the case of a variant or discriminated
6002 record with a default size used as a field of another
6003 record.) */
6004 DECL_SIZE (new_field)
6005 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
6006 ? DECL_SIZE (field) : 0;
6007 DECL_SIZE_UNIT (new_field)
6008 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
6009 ? DECL_SIZE_UNIT (field) : 0;
6011 if (TREE_CODE (t) == QUAL_UNION_TYPE)
6013 tree new_q = substitute_in_expr (DECL_QUALIFIER (field), f, r);
6015 if (new_q != DECL_QUALIFIER (new_field))
6016 changed_field = 1;
6018 /* Do the substitution inside the qualifier and if we find
6019 that this field will not be present, omit it. */
6020 DECL_QUALIFIER (new_field) = new_q;
6022 if (integer_zerop (DECL_QUALIFIER (new_field)))
6023 continue;
6026 if (last_field == 0)
6027 TYPE_FIELDS (new) = new_field;
6028 else
6029 TREE_CHAIN (last_field) = new_field;
6031 last_field = new_field;
6033 /* If this is a qualified type and this field will always be
6034 present, we are done. */
6035 if (TREE_CODE (t) == QUAL_UNION_TYPE
6036 && integer_onep (DECL_QUALIFIER (new_field)))
6037 break;
6040 /* If this used to be a qualified union type, but we now know what
6041 field will be present, make this a normal union. */
6042 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
6043 && (TYPE_FIELDS (new) == 0
6044 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
6045 TREE_SET_CODE (new, UNION_TYPE);
6046 else if (! changed_field)
6047 return t;
6049 if (field_has_rep)
6050 gigi_abort (117);
6052 layout_type (new);
6054 /* If the size was originally a constant use it. */
6055 if (TYPE_SIZE (t) != 0 && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
6056 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
6058 TYPE_SIZE (new) = TYPE_SIZE (t);
6059 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
6060 TYPE_ADA_SIZE (new) = TYPE_ADA_SIZE (t);
6063 return new;
6066 default:
6067 return t;
6071 /* Return the "RM size" of GNU_TYPE. This is the actual number of bits
6072 needed to represent the object. */
6074 tree
6075 rm_size (gnu_type)
6076 tree gnu_type;
6078 /* For integer types, this is the precision. For record types, we store
6079 the size explicitly. For other types, this is just the size. */
6081 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type) != 0)
6082 return TYPE_RM_SIZE (gnu_type);
6083 else if (TREE_CODE (gnu_type) == RECORD_TYPE
6084 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6085 /* Return the rm_size of the actual data plus the size of the template. */
6086 return
6087 size_binop (PLUS_EXPR,
6088 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
6089 DECL_SIZE (TYPE_FIELDS (gnu_type)));
6090 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6091 || TREE_CODE (gnu_type) == UNION_TYPE
6092 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6093 && ! TYPE_IS_FAT_POINTER_P (gnu_type)
6094 && TYPE_ADA_SIZE (gnu_type) != 0)
6095 return TYPE_ADA_SIZE (gnu_type);
6096 else
6097 return TYPE_SIZE (gnu_type);
6100 /* Return an identifier representing the external name to be used for
6101 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
6102 and the specified suffix. */
6104 tree
6105 create_concat_name (gnat_entity, suffix)
6106 Entity_Id gnat_entity;
6107 const char *suffix;
6109 const char *str = (suffix == 0 ? "" : suffix);
6110 String_Template temp = {1, strlen (str)};
6111 Fat_Pointer fp = {str, &temp};
6113 Get_External_Name_With_Suffix (gnat_entity, fp);
6115 return get_identifier (Name_Buffer);
6118 /* Return the name to be used for GNAT_ENTITY. If a type, create a
6119 fully-qualified name, possibly with type information encoding.
6120 Otherwise, return the name. */
6122 tree
6123 get_entity_name (gnat_entity)
6124 Entity_Id gnat_entity;
6126 Get_Encoded_Name (gnat_entity);
6127 return get_identifier (Name_Buffer);
6130 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
6131 string, return a new IDENTIFIER_NODE that is the concatenation of
6132 the name in GNU_ID and SUFFIX. */
6134 tree
6135 concat_id_with_name (gnu_id, suffix)
6136 tree gnu_id;
6137 const char *suffix;
6139 int len = IDENTIFIER_LENGTH (gnu_id);
6141 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id),
6142 IDENTIFIER_LENGTH (gnu_id));
6143 strncpy (Name_Buffer + len, "___", 3);
6144 len += 3;
6145 strcpy (Name_Buffer + len, suffix);
6146 return get_identifier (Name_Buffer);