]> gcc.gnu.org Git - gcc.git/blame - gcc/ada/decl.c
3psoccon.ads, [...]: Files added.
[gcc.git] / gcc / ada / decl.c
CommitLineData
70482933
RK
1/****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
984bc4c3 5 * D E C L *
70482933
RK
6 * *
7 * C Implementation File *
8 * *
fbf5a39b 9 * Copyright (C) 1992-2003, Free Software Foundation, Inc. *
70482933
RK
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 2, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License distributed with GNAT; see file COPYING. If not, write *
19 * to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, *
20 * MA 02111-1307, USA. *
21 * *
22 * GNAT was originally developed by the GNAT team at New York University. *
71ff80dc 23 * Extensive contributions were provided by Ada Core Technologies Inc. *
70482933
RK
24 * *
25 ****************************************************************************/
26
27#include "config.h"
28#include "system.h"
4977bab6
ZW
29#include "coretypes.h"
30#include "tm.h"
70482933
RK
31#include "tree.h"
32#include "flags.h"
33#include "toplev.h"
34#include "convert.h"
35#include "ggc.h"
36#include "obstack.h"
37
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"
53
54/* Setting this to 1 suppresses hashing of types. */
55extern int debug_no_type_hash;
56
57/* Provide default values for the macros controlling stack checking.
58 This is copied from GCC's expr.h. */
59
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
73
74/* These two variables are used to defer recursively expanding incomplete
75 types while we are processing a record or subprogram type. */
76
77static int defer_incomplete_level = 0;
78static struct incomplete
79{
80 struct incomplete *next;
81 tree old_type;
82 Entity_Id full_type;
83} *defer_incomplete_list = 0;
84
85static tree substitution_list PARAMS ((Entity_Id, Entity_Id,
86 tree, int));
87static int allocatable_size_p PARAMS ((tree, int));
88static struct attrib *build_attr_list PARAMS ((Entity_Id));
89static tree elaborate_expression PARAMS ((Node_Id, Entity_Id, tree,
90 int, int, int));
fbf5a39b 91static int is_variable_size PARAMS ((tree));
70482933
RK
92static tree elaborate_expression_1 PARAMS ((Node_Id, Entity_Id, tree,
93 tree, int, int));
94static tree make_packable_type PARAMS ((tree));
95static tree maybe_pad_type PARAMS ((tree, tree, unsigned int,
96 Entity_Id, const char *, int,
97 int, int));
98static tree gnat_to_gnu_field PARAMS ((Entity_Id, tree, int, int));
99static void components_to_record PARAMS ((tree, Node_Id, tree, int,
100 int, tree *, int, int));
101static int compare_field_bitpos PARAMS ((const PTR, const PTR));
102static Uint annotate_value PARAMS ((tree));
103static void annotate_rep PARAMS ((Entity_Id, tree));
07fc65c4
GB
104static tree compute_field_positions PARAMS ((tree, tree, tree, tree,
105 unsigned int));
70482933
RK
106static tree validate_size PARAMS ((Uint, tree, Entity_Id,
107 enum tree_code, int, int));
108static void set_rm_size PARAMS ((Uint, tree, Entity_Id));
109static tree make_type_from_size PARAMS ((tree, tree, int));
110static unsigned int validate_alignment PARAMS ((Uint, Entity_Id,
111 unsigned int));
112static void check_ok_for_atomic PARAMS ((tree, Entity_Id, int));
113\f
114/* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
115 GCC type corresponding to that entity. GNAT_ENTITY is assumed to
116 refer to an Ada type. */
117
118tree
119gnat_to_gnu_type (gnat_entity)
120 Entity_Id gnat_entity;
121{
122 tree gnu_decl;
123
124 /* Convert the ada entity type into a GCC TYPE_DECL node. */
125 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
126 if (TREE_CODE (gnu_decl) != TYPE_DECL)
127 gigi_abort (101);
128
129 return TREE_TYPE (gnu_decl);
130}
131\f
132/* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
133 entity, this routine returns the equivalent GCC tree for that entity
134 (an ..._DECL node) and associates the ..._DECL node with the input GNAT
135 defining identifier.
136
137 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
138 initial value (in GCC tree form). This is optional for variables.
139 For renamed entities, GNU_EXPR gives the object being renamed.
140
141 DEFINITION is nonzero if this call is intended for a definition. This is
142 used for separate compilation where it necessary to know whether an
143 external declaration or a definition should be created if the GCC equivalent
144 was not created previously. The value of 1 is normally used for a non-zero
145 DEFINITION, but a value of 2 is used in special circumstances, defined in
146 the code. */
147
148tree
149gnat_to_gnu_entity (gnat_entity, gnu_expr, definition)
150 Entity_Id gnat_entity;
151 tree gnu_expr;
152 int definition;
153{
154 tree gnu_entity_id;
155 tree gnu_type = 0;
156 /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
157 GNAT tree. This node will be associated with the GNAT node by calling
158 the save_gnu_tree routine at the end of the `switch' statement. */
159 tree gnu_decl = 0;
160 /* Nonzero if we have already saved gnu_decl as a gnat association. */
161 int saved = 0;
162 /* Nonzero if we incremented defer_incomplete_level. */
163 int this_deferred = 0;
164 /* Nonzero if we incremented force_global. */
165 int this_global = 0;
166 /* Nonzero if we should check to see if elaborated during processing. */
167 int maybe_present = 0;
168 /* Nonzero if we made GNU_DECL and its type here. */
169 int this_made_decl = 0;
170 struct attrib *attr_list = 0;
171 int debug_info_p = (Needs_Debug_Info (gnat_entity)
172 || debug_info_level == DINFO_LEVEL_VERBOSE);
173 Entity_Kind kind = Ekind (gnat_entity);
174 Entity_Id gnat_temp;
175 unsigned int esize
176 = ((Known_Esize (gnat_entity)
177 && UI_Is_In_Int_Range (Esize (gnat_entity)))
178 ? MIN (UI_To_Int (Esize (gnat_entity)),
179 IN (kind, Float_Kind)
180 ? LONG_DOUBLE_TYPE_SIZE
181 : IN (kind, Access_Kind) ? POINTER_SIZE * 2
182 : LONG_LONG_TYPE_SIZE)
183 : LONG_LONG_TYPE_SIZE);
184 tree gnu_size = 0;
185 int imported_p
186 = ((Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)))
187 || From_With_Type (gnat_entity));
188 unsigned int align = 0;
189
190 /* Since a use of an Itype is a definition, process it as such if it
191 is not in a with'ed unit. */
192
193 if (! definition && Is_Itype (gnat_entity)
194 && ! present_gnu_tree (gnat_entity)
195 && In_Extended_Main_Code_Unit (gnat_entity))
196 {
197 /* Ensure that we are in a subprogram mentioned in the Scope
198 chain of this entity, our current scope is global,
199 or that we encountered a task or entry (where we can't currently
200 accurately check scoping). */
201 if (current_function_decl == 0
202 || DECL_ELABORATION_PROC_P (current_function_decl))
203 {
204 process_type (gnat_entity);
205 return get_gnu_tree (gnat_entity);
206 }
207
208 for (gnat_temp = Scope (gnat_entity);
209 Present (gnat_temp); gnat_temp = Scope (gnat_temp))
210 {
211 if (Is_Type (gnat_temp))
212 gnat_temp = Underlying_Type (gnat_temp);
213
214 if (Ekind (gnat_temp) == E_Subprogram_Body)
215 gnat_temp
216 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
217
218 if (IN (Ekind (gnat_temp), Subprogram_Kind)
219 && Present (Protected_Body_Subprogram (gnat_temp)))
220 gnat_temp = Protected_Body_Subprogram (gnat_temp);
221
222 if (Ekind (gnat_temp) == E_Entry
223 || Ekind (gnat_temp) == E_Entry_Family
224 || Ekind (gnat_temp) == E_Task_Type
225 || (IN (Ekind (gnat_temp), Subprogram_Kind)
226 && present_gnu_tree (gnat_temp)
227 && (current_function_decl
228 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
229 {
230 process_type (gnat_entity);
231 return get_gnu_tree (gnat_entity);
232 }
233 }
234
235 /* gigi abort 122 means that the entity "gnat_entity" has an incorrect
236 scope, i.e. that its scope does not correspond to the subprogram
237 in which it is declared */
238 gigi_abort (122);
239 }
240
241 /* If this is entity 0, something went badly wrong. */
242 if (gnat_entity == 0)
243 gigi_abort (102);
244
245 /* If we've already processed this entity, return what we got last time.
246 If we are defining the node, we should not have already processed it.
247 In that case, we will abort below when we try to save a new GCC tree for
248 this object. We also need to handle the case of getting a dummy type
249 when a Full_View exists. */
250
251 if (present_gnu_tree (gnat_entity)
252 && (! definition
253 || (Is_Type (gnat_entity) && imported_p)))
254 {
255 gnu_decl = get_gnu_tree (gnat_entity);
256
257 if (TREE_CODE (gnu_decl) == TYPE_DECL
258 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
259 && IN (kind, Incomplete_Or_Private_Kind)
260 && Present (Full_View (gnat_entity)))
261 {
262 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
263 NULL_TREE, 0);
264
265 save_gnu_tree (gnat_entity, NULL_TREE, 0);
266 save_gnu_tree (gnat_entity, gnu_decl, 0);
267 }
268
269 return gnu_decl;
270 }
271
272 /* If this is a numeric or enumeral type, or an access type, a nonzero
273 Esize must be specified unless it was specified by the programmer. */
274 if ((IN (kind, Numeric_Kind) || IN (kind, Enumeration_Kind)
275 || (IN (kind, Access_Kind)
276 && kind != E_Access_Protected_Subprogram_Type
277 && kind != E_Access_Subtype))
278 && Unknown_Esize (gnat_entity)
279 && ! Has_Size_Clause (gnat_entity))
280 gigi_abort (109);
281
282 /* Likewise, RM_Size must be specified for all discrete and fixed-point
283 types. */
284 if (IN (kind, Discrete_Or_Fixed_Point_Kind)
285 && Unknown_RM_Size (gnat_entity))
286 gigi_abort (123);
287
288 /* Get the name of the entity and set up the line number and filename of
289 the original definition for use in any decl we make. */
290
291 gnu_entity_id = get_entity_name (gnat_entity);
292 set_lineno (gnat_entity, 0);
293
294 /* If we get here, it means we have not yet done anything with this
295 entity. If we are not defining it here, it must be external,
296 otherwise we should have defined it already. */
297 if (! definition && ! Is_Public (gnat_entity)
298 && ! type_annotate_only
299 && kind != E_Discriminant && kind != E_Component
300 && kind != E_Label
301 && ! (kind == E_Constant && Present (Full_View (gnat_entity)))
302#if 1
303 && !IN (kind, Type_Kind)
304#endif
305 )
306 gigi_abort (116);
307
308 /* For cases when we are not defining (i.e., we are referencing from
309 another compilation unit) Public entities, show we are at global level
310 for the purpose of computing sizes. Don't do this for components or
311 discriminants since the relevant test is whether or not the record is
312 being defined. */
313 if (! definition && Is_Public (gnat_entity)
314 && ! Is_Statically_Allocated (gnat_entity)
315 && kind != E_Discriminant && kind != E_Component)
316 force_global++, this_global = 1;
317
318 /* Handle any attributes. */
319 if (Has_Gigi_Rep_Item (gnat_entity))
320 attr_list = build_attr_list (gnat_entity);
321
322 switch (kind)
323 {
324 case E_Constant:
325 /* If this is a use of a deferred constant, get its full
326 declaration. */
327 if (! definition && Present (Full_View (gnat_entity)))
328 {
329 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
330 gnu_expr, definition);
331 saved = 1;
332 break;
333 }
334
335 /* If we have an external constant that we are not defining,
336 get the expression that is was defined to represent. We
337 may throw that expression away later if it is not a
fbf5a39b
AC
338 constant.
339 Do not retrieve the expression if it is an aggregate, because
340 in complex instantiation contexts it may not be expanded */
341
70482933
RK
342 if (! definition
343 && Present (Expression (Declaration_Node (gnat_entity)))
fbf5a39b
AC
344 && ! No_Initialization (Declaration_Node (gnat_entity))
345 && Nkind (Expression (Declaration_Node (gnat_entity)))
346 != N_Aggregate)
70482933
RK
347 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
348
349 /* Ignore deferred constant definitions; they are processed fully in the
350 front-end. For deferred constant references, get the full
351 definition. On the other hand, constants that are renamings are
352 handled like variable renamings. If No_Initialization is set, this is
353 not a deferred constant but a constant whose value is built
354 manually. */
355
356 if (definition && gnu_expr == 0
357 && ! No_Initialization (Declaration_Node (gnat_entity))
358 && No (Renamed_Object (gnat_entity)))
359 {
360 gnu_decl = error_mark_node;
361 saved = 1;
362 break;
363 }
364 else if (! definition && IN (kind, Incomplete_Or_Private_Kind)
365 && Present (Full_View (gnat_entity)))
366 {
367 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
368 NULL_TREE, 0);
369 saved = 1;
370 break;
371 }
372
373 goto object;
374
375 case E_Exception:
376 /* If this is not a VMS exception, treat it as a normal object.
377 Otherwise, make an object at the specific address of character
378 type, point to it, and convert it to integer, and mask off
379 the lower 3 bits. */
380 if (! Is_VMS_Exception (gnat_entity))
381 goto object;
382
383 /* Allocate the global object that we use to get the value of the
384 exception. */
385 gnu_decl = create_var_decl (gnu_entity_id,
386 (Present (Interface_Name (gnat_entity))
387 ? create_concat_name (gnat_entity, 0)
388 : NULL_TREE),
389 char_type_node, NULL_TREE, 0, 0, 1, 1,
390 0);
391
392 /* Now return the expression giving the desired value. */
393 gnu_decl
394 = build_binary_op (BIT_AND_EXPR, integer_type_node,
395 convert (integer_type_node,
396 build_unary_op (ADDR_EXPR, NULL_TREE,
397 gnu_decl)),
398 build_unary_op (NEGATE_EXPR, integer_type_node,
399 build_int_2 (7, 0)));
400
401 save_gnu_tree (gnat_entity, gnu_decl, 1);
402 saved = 1;
403 break;
404
405 case E_Discriminant:
406 case E_Component:
407 {
408 /* The GNAT record where the component was defined. */
409 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
410
411 /* If the variable is an inherited record component (in the case of
412 extended record types), just return the inherited entity, which
413 must be a FIELD_DECL. Likewise for discriminants.
414 For discriminants of untagged records which have explicit
fbf5a39b
AC
415 stored discriminants, return the entity for the corresponding
416 stored discriminant. Also use Original_Record_Component
70482933
RK
417 if the record has a private extension. */
418
419 if ((Base_Type (gnat_record) == gnat_record
fbf5a39b 420 || Ekind (Scope (gnat_entity)) == E_Private_Subtype
70482933
RK
421 || Ekind (Scope (gnat_entity)) == E_Record_Subtype_With_Private
422 || Ekind (Scope (gnat_entity)) == E_Record_Type_With_Private)
423 && Present (Original_Record_Component (gnat_entity))
424 && Original_Record_Component (gnat_entity) != gnat_entity)
425 {
426 gnu_decl
427 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
428 gnu_expr, definition);
429 saved = 1;
430 break;
431 }
432
fbf5a39b 433 /* If the enclosing record has explicit stored discriminants,
70482933
RK
434 then it is an untagged record. If the Corresponding_Discriminant
435 is not empty then this must be a renamed discriminant and its
436 Original_Record_Component must point to the corresponding explicit
fbf5a39b 437 stored discriminant (i.e., we should have taken the previous
70482933
RK
438 branch). */
439
440 else if (Present (Corresponding_Discriminant (gnat_entity))
441 && Is_Tagged_Type (gnat_record))
442 {
fbf5a39b 443 /* A tagged record has no explicit stored discriminants. */
70482933
RK
444
445 if (First_Discriminant (gnat_record)
fbf5a39b 446 != First_Stored_Discriminant (gnat_record))
70482933
RK
447 gigi_abort (119);
448
449 gnu_decl
450 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
451 gnu_expr, definition);
452 saved = 1;
453 break;
454 }
455
fbf5a39b 456 /* If the enclosing record has explicit stored discriminants,
70482933
RK
457 then it is an untagged record. If the Corresponding_Discriminant
458 is not empty then this must be a renamed discriminant and its
459 Original_Record_Component must point to the corresponding explicit
fbf5a39b 460 stored discriminant (i.e., we should have taken the first
70482933
RK
461 branch). */
462
463 else if (Present (Corresponding_Discriminant (gnat_entity))
464 && (First_Discriminant (gnat_record)
fbf5a39b 465 != First_Stored_Discriminant (gnat_record)))
70482933
RK
466 gigi_abort (120);
467
468 /* Otherwise, if we are not defining this and we have no GCC type
469 for the containing record, make one for it. Then we should
470 have made our own equivalent. */
471 else if (! definition && ! present_gnu_tree (gnat_record))
472 {
473 /* ??? If this is in a record whose scope is a protected
474 type and we have an Original_Record_Component, use it.
475 This is a workaround for major problems in protected type
476 handling. */
fbf5a39b
AC
477
478 Entity_Id Scop = Scope (Scope (gnat_entity));
479 if ((Is_Protected_Type (Scop)
480 || (Is_Private_Type (Scop)
481 && Present (Full_View (Scop))
482 && Is_Protected_Type (Full_View (Scop))))
70482933 483 && Present (Original_Record_Component (gnat_entity)))
fbf5a39b 484 {
70482933
RK
485 gnu_decl
486 = gnat_to_gnu_entity (Original_Record_Component
487 (gnat_entity),
488 gnu_expr, definition);
489 saved = 1;
490 break;
491 }
492
493 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
494 gnu_decl = get_gnu_tree (gnat_entity);
495 saved = 1;
496 break;
497 }
498
499 /* Here we have no GCC type and this is a reference rather than a
500 definition. This should never happen. Most likely the cause is a
501 reference before declaration in the gnat tree for gnat_entity. */
502 else
503 gigi_abort (103);
504 }
505
506 case E_Loop_Parameter:
507 case E_Out_Parameter:
508 case E_Variable:
509
510 /* Simple variables, loop variables, OUT parameters, and exceptions. */
511 object:
512 {
513 int used_by_ref = 0;
514 int const_flag
515 = ((kind == E_Constant || kind == E_Variable)
516 && ! Is_Statically_Allocated (gnat_entity)
517 && Is_True_Constant (gnat_entity)
518 && (((Nkind (Declaration_Node (gnat_entity))
519 == N_Object_Declaration)
520 && Present (Expression (Declaration_Node (gnat_entity))))
521 || Present (Renamed_Object (gnat_entity))));
522 int inner_const_flag = const_flag;
523 int static_p = Is_Statically_Allocated (gnat_entity);
524 tree gnu_ext_name = NULL_TREE;
525
526 if (Present (Renamed_Object (gnat_entity)) && ! definition)
527 {
528 if (kind == E_Exception)
529 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
530 NULL_TREE, 0);
531 else
532 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
533 }
534
535 /* Get the type after elaborating the renamed object. */
536 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
537
538 /* If this is a loop variable, its type should be the base type.
539 This is because the code for processing a loop determines whether
540 a normal loop end test can be done by comparing the bounds of the
541 loop against those of the base type, which is presumed to be the
542 size used for computation. But this is not correct when the size
543 of the subtype is smaller than the type. */
544 if (kind == E_Loop_Parameter)
545 gnu_type = get_base_type (gnu_type);
546
547 /* Reject non-renamed objects whose types are unconstrained arrays or
548 any object whose type is a dummy type or VOID_TYPE. */
549
550 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
551 && No (Renamed_Object (gnat_entity)))
552 || TYPE_IS_DUMMY_P (gnu_type)
553 || TREE_CODE (gnu_type) == VOID_TYPE)
554 {
555 if (type_annotate_only)
556 return error_mark_node;
557 else
558 gigi_abort (104);
559 }
560
561 /* If we are defining the object, see if it has a Size value and
562 validate it if so. Then get the new type, if any. */
563 if (definition)
564 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
565 gnat_entity, VAR_DECL, 0,
566 Has_Size_Clause (gnat_entity));
567
568 if (gnu_size != 0)
569 {
570 gnu_type
571 = make_type_from_size (gnu_type, gnu_size,
572 Has_Biased_Representation (gnat_entity));
573
574 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
575 gnu_size = 0;
576 }
577
578 /* If this object has self-referential size, it must be a record with
579 a default value. We are supposed to allocate an object of the
580 maximum size in this case unless it is a constant with an
581 initializing expression, in which case we can get the size from
582 that. Note that the resulting size may still be a variable, so
583 this may end up with an indirect allocation. */
584
585 if (No (Renamed_Object (gnat_entity))
fbf5a39b 586 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
70482933
RK
587 {
588 if (gnu_expr != 0 && kind == E_Constant)
589 {
590 gnu_size = TYPE_SIZE (TREE_TYPE (gnu_expr));
fbf5a39b
AC
591 if (CONTAINS_PLACEHOLDER_P (gnu_size))
592 gnu_size = build (WITH_RECORD_EXPR, bitsizetype,
593 gnu_size, gnu_expr);
70482933
RK
594 }
595
596 /* We may have no GNU_EXPR because No_Initialization is
597 set even though there's an Expression. */
598 else if (kind == E_Constant
599 && (Nkind (Declaration_Node (gnat_entity))
600 == N_Object_Declaration)
601 && Present (Expression (Declaration_Node (gnat_entity))))
602 gnu_size
603 = TYPE_SIZE (gnat_to_gnu_type
604 (Etype
605 (Expression (Declaration_Node (gnat_entity)))));
606 else
607 gnu_size = max_size (TYPE_SIZE (gnu_type), 1);
608 }
609
fbf5a39b
AC
610 /* If the size is zero bytes, make it one byte since some linkers have
611 trouble with zero-sized objects. If the object will have a
612 template, that will make it nonzero so don't bother. Also avoid
613 doing that for an object renaming or an object with an address
614 clause, as we would lose useful information on the view size
615 (e.g. for null array slices) and we are not allocating the object
616 here anyway. */
70482933
RK
617 if (((gnu_size != 0 && integer_zerop (gnu_size))
618 || (TYPE_SIZE (gnu_type) != 0
619 && integer_zerop (TYPE_SIZE (gnu_type))))
620 && (! Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
fbf5a39b
AC
621 || ! Is_Array_Type (Etype (gnat_entity)))
622 && ! Present (Renamed_Object (gnat_entity))
623 && ! Present (Address_Clause (gnat_entity)))
70482933
RK
624 gnu_size = bitsize_unit_node;
625
626 /* If an alignment is specified, use it if valid. Note that
627 exceptions are objects but don't have alignments. */
628 if (kind != E_Exception && Known_Alignment (gnat_entity))
629 {
630 if (No (Alignment (gnat_entity)))
631 gigi_abort (125);
632
633 align
634 = validate_alignment (Alignment (gnat_entity), gnat_entity,
635 TYPE_ALIGN (gnu_type));
636 }
637
638 /* If this is an atomic object with no specified size and alignment,
639 but where the size of the type is a constant, set the alignment to
640 the lowest power of two greater than the size, or to the
641 biggest meaningful alignment, whichever is smaller. */
642
643 if (Is_Atomic (gnat_entity) && gnu_size == 0 && align == 0
644 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
645 {
646 if (! host_integerp (TYPE_SIZE (gnu_type), 1)
647 || 0 <= compare_tree_int (TYPE_SIZE (gnu_type),
648 BIGGEST_ALIGNMENT))
649 align = BIGGEST_ALIGNMENT;
650 else
651 align = ((unsigned int) 1
652 << (floor_log2 (tree_low_cst
653 (TYPE_SIZE (gnu_type), 1) - 1)
654 + 1));
655 }
656
70482933
RK
657 /* If the object is set to have atomic components, find the component
658 type and validate it.
659
660 ??? Note that we ignore Has_Volatile_Components on objects; it's
661 not at all clear what to do in that case. */
662
663 if (Has_Atomic_Components (gnat_entity))
664 {
665 tree gnu_inner
666 = (TREE_CODE (gnu_type) == ARRAY_TYPE
667 ? TREE_TYPE (gnu_type) : gnu_type);
668
669 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
670 && TYPE_MULTI_ARRAY_P (gnu_inner))
671 gnu_inner = TREE_TYPE (gnu_inner);
672
673 check_ok_for_atomic (gnu_inner, gnat_entity, 1);
674 }
675
07fc65c4
GB
676 /* Now check if the type of the object allows atomic access. Note
677 that we must test the type, even if this object has size and
678 alignment to allow such access, because we will be going
679 inside the padded record to assign to the object. We could fix
680 this by always copying via an intermediate value, but it's not
681 clear it's worth the effort. */
682 if (Is_Atomic (gnat_entity))
683 check_ok_for_atomic (gnu_type, gnat_entity, 0);
684
70482933
RK
685 /* If this is an aliased object with an unconstrained nominal subtype,
686 make a type that includes the template. */
687 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
688 && Is_Array_Type (Etype (gnat_entity))
689 && ! type_annotate_only)
690 {
691 tree gnu_fat
692 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
693 tree gnu_temp_type
694 = TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat))));
695
696 gnu_type
697 = build_unc_object_type (gnu_temp_type, gnu_type,
698 concat_id_with_name (gnu_entity_id,
699 "UNC"));
700 }
701
fbf5a39b
AC
702#ifdef MINIMUM_ATOMIC_ALIGNMENT
703 /* If the size is a constant and no alignment is specified, force
704 the alignment to be the minimum valid atomic alignment. The
705 restriction on constant size avoids problems with variable-size
706 temporaries; if the size is variable, there's no issue with
707 atomic access. Also don't do this for a constant, since it isn't
708 necessary and can interfere with constant replacement. Finally,
709 do not do it for Out parameters since that creates an
710 size inconsistency with In parameters. */
711 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
712 && ! FLOAT_TYPE_P (gnu_type)
713 && ! const_flag && No (Renamed_Object (gnat_entity))
714 && ! imported_p && No (Address_Clause (gnat_entity))
715 && kind != E_Out_Parameter
716 && (gnu_size != 0 ? TREE_CODE (gnu_size) == INTEGER_CST
717 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
718 align = MINIMUM_ATOMIC_ALIGNMENT;
719#endif
720
721 /* Make a new type with the desired size and alignment, if needed. */
722 gnu_type = maybe_pad_type (gnu_type, gnu_size, align,
723 gnat_entity, "PAD", 0, definition, 1);
724
725 /* Make a volatile version of this object's type if we are to
726 make the object volatile. Note that 13.3(19) says that we
727 should treat other types of objects as volatile as well. */
728 if ((Treat_As_Volatile (gnat_entity)
729 || Is_Exported (gnat_entity)
730 || Is_Imported (gnat_entity)
731 || Present (Address_Clause (gnat_entity)))
732 && ! TYPE_VOLATILE (gnu_type))
733 gnu_type = build_qualified_type (gnu_type,
734 (TYPE_QUALS (gnu_type)
735 | TYPE_QUAL_VOLATILE));
736
70482933
RK
737 /* Convert the expression to the type of the object except in the
738 case where the object's type is unconstrained or the object's type
739 is a padded record whose field is of self-referential size. In
740 the former case, converting will generate unnecessary evaluations
741 of the CONSTRUCTOR to compute the size and in the latter case, we
742 want to only copy the actual data. */
743 if (gnu_expr != 0
744 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
fbf5a39b 745 && ! CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
70482933
RK
746 && ! (TREE_CODE (gnu_type) == RECORD_TYPE
747 && TYPE_IS_PADDING_P (gnu_type)
fbf5a39b 748 && (CONTAINS_PLACEHOLDER_P
70482933
RK
749 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
750 gnu_expr = convert (gnu_type, gnu_expr);
751
752 /* See if this is a renaming. If this is a constant renaming,
753 treat it as a normal variable whose initial value is what
754 is being renamed. We cannot do this if the type is
755 unconstrained or class-wide.
756
757 Otherwise, if what we are renaming is a reference, we can simply
758 return a stabilized version of that reference, after forcing
759 any SAVE_EXPRs to be evaluated. But, if this is at global level,
760 we can only do this if we know no SAVE_EXPRs will be made.
761 Otherwise, make this into a constant pointer to the object we are
762 to rename. */
763
764 if (Present (Renamed_Object (gnat_entity)))
765 {
766 /* If the renamed object had padding, strip off the reference
767 to the inner object and reset our type. */
768 if (TREE_CODE (gnu_expr) == COMPONENT_REF
769 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
770 == RECORD_TYPE)
771 && (TYPE_IS_PADDING_P
772 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
773 {
774 gnu_expr = TREE_OPERAND (gnu_expr, 0);
775 gnu_type = TREE_TYPE (gnu_expr);
776 }
777
778 if (const_flag
779 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
780 && TYPE_MODE (gnu_type) != BLKmode
781 && Ekind (Etype (gnat_entity)) != E_Class_Wide_Type
782 && !Is_Array_Type (Etype (gnat_entity)))
783 ;
784
785 /* If this is a declaration or reference, we can just use that
786 declaration or reference as this entity. */
787 else if ((DECL_P (gnu_expr)
788 || TREE_CODE_CLASS (TREE_CODE (gnu_expr)) == 'r')
789 && ! Materialize_Entity (gnat_entity)
790 && (! global_bindings_p ()
791 || (staticp (gnu_expr)
792 && ! TREE_SIDE_EFFECTS (gnu_expr))))
793 {
794 set_lineno (gnat_entity, ! global_bindings_p ());
795 gnu_decl = gnat_stabilize_reference (gnu_expr, 1);
796 save_gnu_tree (gnat_entity, gnu_decl, 1);
797 saved = 1;
798
799 if (! global_bindings_p ())
800 expand_expr_stmt (build1 (CONVERT_EXPR, void_type_node,
801 gnu_decl));
802 break;
803 }
804 else
805 {
806 inner_const_flag = TREE_READONLY (gnu_expr);
807 const_flag = 1;
808 gnu_type = build_reference_type (gnu_type);
809 gnu_expr = build_unary_op (ADDR_EXPR, gnu_type, gnu_expr);
810 gnu_size = 0;
811 used_by_ref = 1;
812 }
813 }
814
815 /* If this is an aliased object whose nominal subtype is unconstrained,
816 the object is a record that contains both the template and
817 the object. If there is an initializer, it will have already
818 been converted to the right type, but we need to create the
819 template if there is no initializer. */
820 else if (definition && TREE_CODE (gnu_type) == RECORD_TYPE
fbf5a39b
AC
821 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
822 /* Beware that padding might have been introduced
823 via maybe_pad_type above. */
824 || (TYPE_IS_PADDING_P (gnu_type)
825 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
826 == RECORD_TYPE
827 && TYPE_CONTAINS_TEMPLATE_P
828 (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
70482933 829 && gnu_expr == 0)
fbf5a39b
AC
830 {
831 tree template_field
832 = TYPE_IS_PADDING_P (gnu_type)
833 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
834 : TYPE_FIELDS (gnu_type);
835
836 gnu_expr
837 = gnat_build_constructor
70482933
RK
838 (gnu_type,
839 tree_cons
fbf5a39b
AC
840 (template_field,
841 build_template (TREE_TYPE (template_field),
842 TREE_TYPE (TREE_CHAIN (template_field)),
843 NULL_TREE),
70482933 844 NULL_TREE));
fbf5a39b 845 }
70482933
RK
846
847 /* If this is a pointer and it does not have an initializing
fbf5a39b
AC
848 expression, initialize it to NULL, unless the obect is
849 imported. */
70482933
RK
850 if (definition
851 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
fbf5a39b 852 && !Is_Imported (gnat_entity)
70482933
RK
853 && gnu_expr == 0)
854 gnu_expr = integer_zero_node;
855
856 /* If we are defining the object and it has an Address clause we must
857 get the address expression from the saved GCC tree for the
858 object if the object has a Freeze_Node. Otherwise, we elaborate
859 the address expression here since the front-end has guaranteed
860 in that case that the elaboration has no effects. Note that
861 only the latter mechanism is currently in use. */
862 if (definition && Present (Address_Clause (gnat_entity)))
863 {
864 tree gnu_address
865 = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
866 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
867
868 save_gnu_tree (gnat_entity, NULL_TREE, 0);
869
870 /* Ignore the size. It's either meaningless or was handled
871 above. */
872 gnu_size = 0;
873 gnu_type = build_reference_type (gnu_type);
874 gnu_address = convert (gnu_type, gnu_address);
875 used_by_ref = 1;
876 const_flag = ! Is_Public (gnat_entity);
877
878 /* If we don't have an initializing expression for the underlying
879 variable, the initializing expression for the pointer is the
880 specified address. Otherwise, we have to make a COMPOUND_EXPR
881 to assign both the address and the initial value. */
882 if (gnu_expr == 0)
883 gnu_expr = gnu_address;
884 else
885 gnu_expr
886 = build (COMPOUND_EXPR, gnu_type,
887 build_binary_op
888 (MODIFY_EXPR, NULL_TREE,
889 build_unary_op (INDIRECT_REF, NULL_TREE,
890 gnu_address),
891 gnu_expr),
892 gnu_address);
893 }
894
895 /* If it has an address clause and we are not defining it, mark it
896 as an indirect object. Likewise for Stdcall objects that are
897 imported. */
898 if ((! definition && Present (Address_Clause (gnat_entity)))
899 || (Is_Imported (gnat_entity)
900 && Convention (gnat_entity) == Convention_Stdcall))
901 {
902 gnu_type = build_reference_type (gnu_type);
903 gnu_size = 0;
904 used_by_ref = 1;
905 }
906
907 /* If we are at top level and this object is of variable size,
908 make the actual type a hidden pointer to the real type and
909 make the initializer be a memory allocation and initialization.
910 Likewise for objects we aren't defining (presumed to be
911 external references from other packages), but there we do
912 not set up an initialization.
913
914 If the object's size overflows, make an allocator too, so that
915 Storage_Error gets raised. Note that we will never free
916 such memory, so we presume it never will get allocated. */
917
918 if (! allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
919 global_bindings_p () || ! definition
920 || static_p)
921 || (gnu_size != 0
922 && ! allocatable_size_p (gnu_size,
923 global_bindings_p () || ! definition
924 || static_p)))
925 {
926 gnu_type = build_reference_type (gnu_type);
927 gnu_size = 0;
928 used_by_ref = 1;
929 const_flag = 1;
930
931 /* Get the data part of GNU_EXPR in case this was a
932 aliased object whose nominal subtype is unconstrained.
933 In that case the pointer above will be a thin pointer and
934 build_allocator will automatically make the template and
935 constructor already made above. */
936
937 if (definition)
938 {
939 tree gnu_alloc_type = TREE_TYPE (gnu_type);
940
941 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
942 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
943 {
944 gnu_alloc_type
945 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
946 gnu_expr
947 = build_component_ref
948 (gnu_expr, NULL_TREE,
949 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))));
950 }
951
952 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
953 && TREE_CONSTANT_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
954 && ! Is_Imported (gnat_entity))
955 post_error ("Storage_Error will be raised at run-time?",
956 gnat_entity);
957
958 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr,
fbf5a39b 959 gnu_type, 0, 0, gnat_entity);
70482933
RK
960 }
961 else
962 {
963 gnu_expr = 0;
964 const_flag = 0;
965 }
966 }
967
968 /* If this object would go into the stack and has an alignment
969 larger than the default largest alignment, make a variable
970 to hold the "aligning type" with a modified initial value,
971 if any, then point to it and make that the value of this
972 variable, which is now indirect. */
973
974 if (! global_bindings_p () && ! static_p && definition
975 && ! imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
976 {
977 tree gnu_new_type
978 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
979 TYPE_SIZE_UNIT (gnu_type));
980 tree gnu_new_var;
981
70482933
RK
982 set_lineno (gnat_entity, 1);
983 gnu_new_var
984 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
985 NULL_TREE, gnu_new_type, gnu_expr,
986 0, 0, 0, 0, 0);
987
fbf5a39b
AC
988 if (gnu_expr != 0)
989 expand_expr_stmt
990 (build_binary_op
991 (MODIFY_EXPR, NULL_TREE,
992 build_component_ref (gnu_new_var, NULL_TREE,
993 TYPE_FIELDS (gnu_new_type)),
994 gnu_expr));
995
70482933
RK
996 gnu_type = build_reference_type (gnu_type);
997 gnu_expr
998 = build_unary_op
999 (ADDR_EXPR, gnu_type,
1000 build_component_ref (gnu_new_var, NULL_TREE,
1001 TYPE_FIELDS (gnu_new_type)));
1002
1003 gnu_size = 0;
1004 used_by_ref = 1;
1005 const_flag = 1;
1006 }
1007
1008 /* Convert the expression to the type of the object except in the
1009 case where the object's type is unconstrained or the object's type
1010 is a padded record whose field is of self-referential size. In
1011 the former case, converting will generate unnecessary evaluations
1012 of the CONSTRUCTOR to compute the size and in the latter case, we
1013 want to only copy the actual data. */
1014 if (gnu_expr != 0
1015 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
fbf5a39b 1016 && ! CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
70482933
RK
1017 && ! (TREE_CODE (gnu_type) == RECORD_TYPE
1018 && TYPE_IS_PADDING_P (gnu_type)
fbf5a39b 1019 && (CONTAINS_PLACEHOLDER_P
70482933
RK
1020 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1021 gnu_expr = convert (gnu_type, gnu_expr);
1022
1023 /* This name is external or there was a name specified, use it.
1024 Don't use the Interface_Name if there is an address clause.
1025 (see CD30005). */
1026 if ((Present (Interface_Name (gnat_entity))
1027 && No (Address_Clause (gnat_entity)))
1028 || (Is_Public (gnat_entity)
1029 && (! Is_Imported (gnat_entity) || Is_Exported (gnat_entity))))
1030 gnu_ext_name = create_concat_name (gnat_entity, 0);
1031
1032 if (const_flag)
1033 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1034 | TYPE_QUAL_CONST));
1035
1036 /* If this is constant initialized to a static constant and the
1037 object has an aggregrate type, force it to be statically
1038 allocated. */
1039 if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1040 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1041 && (AGGREGATE_TYPE_P (gnu_type)
1042 && ! (TREE_CODE (gnu_type) == RECORD_TYPE
1043 && TYPE_IS_PADDING_P (gnu_type))))
1044 static_p = 1;
1045
1046 set_lineno (gnat_entity, ! global_bindings_p ());
1047 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1048 gnu_expr, const_flag,
1049 Is_Public (gnat_entity),
1050 imported_p || !definition,
1051 static_p, attr_list);
1052
1053 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1054 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1055
1056 if (definition && DECL_SIZE (gnu_decl) != 0
1057 && gnu_block_stack != 0
1058 && TREE_VALUE (gnu_block_stack) != 0
1059 && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1060 || (flag_stack_check && ! STACK_CHECK_BUILTIN
1061 && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1062 STACK_CHECK_MAX_VAR_SIZE))))
1063 update_setjmp_buf (TREE_VALUE (gnu_block_stack));
1064
c2d7fe59
RK
1065 /* If this is a public constant or we're not optimizing and we're not
1066 making a VAR_DECL for it, make one just for export or debugger
1067 use. Likewise if the address is taken or if the object or type is
1068 aliased. */
70482933
RK
1069 if (definition && TREE_CODE (gnu_decl) == CONST_DECL
1070 && (Is_Public (gnat_entity)
c2d7fe59 1071 || optimize == 0
70482933
RK
1072 || Address_Taken (gnat_entity)
1073 || Is_Aliased (gnat_entity)
1074 || Is_Aliased (Etype (gnat_entity))))
fbf5a39b 1075 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl,
e2500fed 1076 create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
70482933 1077 gnu_expr, 0, Is_Public (gnat_entity), 0,
e2500fed 1078 static_p, 0));
70482933 1079
70482933
RK
1080 /* If this is declared in a block that contains an block with an
1081 exception handler, we must force this variable in memory to
1082 suppress an invalid optimization. */
07fc65c4
GB
1083 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1084 && Exception_Mechanism != GCC_ZCX)
70482933 1085 {
dffd7eb6 1086 gnat_mark_addressable (gnu_decl);
70482933
RK
1087 flush_addressof (gnu_decl);
1088 }
1089
1090 /* Back-annotate the Alignment of the object if not already in the
79503fdd
GB
1091 tree. Likewise for Esize if the object is of a constant size.
1092 But if the "object" is actually a pointer to an object, the
1093 alignment and size are the same as teh type, so don't back-annotate
1094 the values for the pointer. */
1095 if (! used_by_ref && Unknown_Alignment (gnat_entity))
70482933
RK
1096 Set_Alignment (gnat_entity,
1097 UI_From_Int (DECL_ALIGN (gnu_decl) / BITS_PER_UNIT));
1098
79503fdd 1099 if (! used_by_ref && Unknown_Esize (gnat_entity)
70482933
RK
1100 && DECL_SIZE (gnu_decl) != 0)
1101 {
1102 tree gnu_back_size = DECL_SIZE (gnu_decl);
1103
1104 if (TREE_CODE (TREE_TYPE (gnu_decl)) == RECORD_TYPE
1105 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_decl)))
1106 gnu_back_size
1107 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN
1108 (TYPE_FIELDS (TREE_TYPE (gnu_decl)))));
1109
1110 Set_Esize (gnat_entity, annotate_value (gnu_back_size));
1111 }
1112 }
1113 break;
1114
1115 case E_Void:
1116 /* Return a TYPE_DECL for "void" that we previously made. */
1117 gnu_decl = void_type_decl_node;
1118 break;
1119
1120 case E_Enumeration_Type:
1121 /* A special case, for the types Character and Wide_Character in
1122 Standard, we do not list all the literals. So if the literals
1123 are not specified, make this an unsigned type. */
1124 if (No (First_Literal (gnat_entity)))
1125 {
1126 gnu_type = make_unsigned_type (esize);
1127 break;
1128 }
1129
1130 /* Normal case of non-character type, or non-Standard character type */
1131 {
1132 /* Here we have a list of enumeral constants in First_Literal.
1133 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1134 the list to be places into TYPE_FIELDS. Each node in the list
1135 is a TREE_LIST node whose TREE_VALUE is the literal name
1136 and whose TREE_PURPOSE is the value of the literal.
1137
1138 Esize contains the number of bits needed to represent the enumeral
1139 type, Type_Low_Bound also points to the first literal and
1140 Type_High_Bound points to the last literal. */
1141
1142 Entity_Id gnat_literal;
1143 tree gnu_literal_list = NULL_TREE;
1144
1145 if (Is_Unsigned_Type (gnat_entity))
1146 gnu_type = make_unsigned_type (esize);
1147 else
1148 gnu_type = make_signed_type (esize);
1149
1150 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1151
1152 for (gnat_literal = First_Literal (gnat_entity);
1153 Present (gnat_literal);
1154 gnat_literal = Next_Literal (gnat_literal))
1155 {
1156 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1157 gnu_type);
1158 tree gnu_literal
1159 = create_var_decl (get_entity_name (gnat_literal),
1160 0, gnu_type, gnu_value, 1, 0, 0, 0, 0);
1161
1162 save_gnu_tree (gnat_literal, gnu_literal, 0);
1163 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1164 gnu_value, gnu_literal_list);
1165 }
1166
1167 TYPE_FIELDS (gnu_type) = nreverse (gnu_literal_list);
1168
1169 /* Note that the bounds are updated at the end of this function
1170 because to avoid an infinite recursion when we get the bounds of
1171 this type, since those bounds are objects of this type. */
1172 }
1173 break;
1174
1175 case E_Signed_Integer_Type:
1176 case E_Ordinary_Fixed_Point_Type:
1177 case E_Decimal_Fixed_Point_Type:
1178 /* For integer types, just make a signed type the appropriate number
1179 of bits. */
1180 gnu_type = make_signed_type (esize);
1181 break;
1182
1183 case E_Modular_Integer_Type:
1184 /* For modular types, make the unsigned type of the proper number of
1185 bits and then set up the modulus, if required. */
1186 {
1187 enum machine_mode mode;
1188 tree gnu_modulus;
1189 tree gnu_high = 0;
1190
1191 if (Is_Packed_Array_Type (gnat_entity))
1192 esize = UI_To_Int (RM_Size (gnat_entity));
1193
1194 /* Find the smallest mode at least ESIZE bits wide and make a class
1195 using that mode. */
1196
1197 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1198 GET_MODE_BITSIZE (mode) < esize;
1199 mode = GET_MODE_WIDER_MODE (mode))
1200 ;
1201
1202 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1203 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1204 = Is_Packed_Array_Type (gnat_entity);
1205
1206 /* Get the modulus in this type. If it overflows, assume it is because
1207 it is equal to 2**Esize. Note that there is no overflow checking
1208 done on unsigned type, so we detect the overflow by looking for
1209 a modulus of zero, which is otherwise invalid. */
1210 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1211
1212 if (! integer_zerop (gnu_modulus))
1213 {
1214 TYPE_MODULAR_P (gnu_type) = 1;
e2500fed 1215 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
70482933
RK
1216 gnu_high = fold (build (MINUS_EXPR, gnu_type, gnu_modulus,
1217 convert (gnu_type, integer_one_node)));
1218 }
1219
1220 /* If we have to set TYPE_PRECISION different from its natural value,
1221 make a subtype to do do. Likewise if there is a modulus and
1222 it is not one greater than TYPE_MAX_VALUE. */
1223 if (TYPE_PRECISION (gnu_type) != esize
1224 || (TYPE_MODULAR_P (gnu_type)
1225 && ! tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1226 {
1227 tree gnu_subtype = make_node (INTEGER_TYPE);
1228
1229 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1230 TREE_TYPE (gnu_subtype) = gnu_type;
1231 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1232 TYPE_MAX_VALUE (gnu_subtype)
1233 = TYPE_MODULAR_P (gnu_type)
1234 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1235 TYPE_PRECISION (gnu_subtype) = esize;
1236 TREE_UNSIGNED (gnu_subtype) = 1;
1237 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1238 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1239 = Is_Packed_Array_Type (gnat_entity);
1240 layout_type (gnu_subtype);
1241
1242 gnu_type = gnu_subtype;
1243 }
1244 }
1245 break;
1246
1247 case E_Signed_Integer_Subtype:
1248 case E_Enumeration_Subtype:
1249 case E_Modular_Integer_Subtype:
1250 case E_Ordinary_Fixed_Point_Subtype:
1251 case E_Decimal_Fixed_Point_Subtype:
1252
1253 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1254 that we do not want to call build_range_type since we would
1255 like each subtype node to be distinct. This will be important
1256 when memory aliasing is implemented.
1257
1258 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1259 parent type; this fact is used by the arithmetic conversion
1260 functions.
1261
1262 We elaborate the Ancestor_Subtype if it is not in the current
1263 unit and one of our bounds is non-static. We do this to ensure
1264 consistent naming in the case where several subtypes share the same
1265 bounds by always elaborating the first such subtype first, thus
1266 using its name. */
1267
1268 if (definition == 0
1269 && Present (Ancestor_Subtype (gnat_entity))
1270 && ! In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1271 && (! Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1272 || ! Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1273 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1274 gnu_expr, definition);
1275
1276 gnu_type = make_node (INTEGER_TYPE);
1277 if (Is_Packed_Array_Type (gnat_entity))
1278 {
70482933
RK
1279 esize = UI_To_Int (RM_Size (gnat_entity));
1280 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1281 }
1282
1283 TYPE_PRECISION (gnu_type) = esize;
1284 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1285
1286 TYPE_MIN_VALUE (gnu_type)
1287 = convert (TREE_TYPE (gnu_type),
1288 elaborate_expression (Type_Low_Bound (gnat_entity),
1289 gnat_entity,
1290 get_identifier ("L"), definition, 1,
1291 Needs_Debug_Info (gnat_entity)));
1292
1293 TYPE_MAX_VALUE (gnu_type)
1294 = convert (TREE_TYPE (gnu_type),
1295 elaborate_expression (Type_High_Bound (gnat_entity),
1296 gnat_entity,
1297 get_identifier ("U"), definition, 1,
1298 Needs_Debug_Info (gnat_entity)));
1299
1300 /* One of the above calls might have caused us to be elaborated,
1301 so don't blow up if so. */
1302 if (present_gnu_tree (gnat_entity))
1303 {
1304 maybe_present = 1;
1305 break;
1306 }
1307
1308 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1309 = Has_Biased_Representation (gnat_entity);
1310
1311 /* This should be an unsigned type if the lower bound is constant
1312 and non-negative or if the base type is unsigned; a signed type
1313 otherwise. */
1314 TREE_UNSIGNED (gnu_type)
1315 = (TREE_UNSIGNED (TREE_TYPE (gnu_type))
1316 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1317 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1318 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1319 || Is_Unsigned_Type (gnat_entity));
1320
1321 layout_type (gnu_type);
1322
1323 if (Is_Packed_Array_Type (gnat_entity) && BYTES_BIG_ENDIAN)
1324 {
1325 tree gnu_field_type = gnu_type;
1326 tree gnu_field;
1327
1328 TYPE_RM_SIZE_INT (gnu_field_type)
1329 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1330 gnu_type = make_node (RECORD_TYPE);
1331 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "LJM");
1332 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1333 TYPE_PACKED (gnu_type) = 1;
1334 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1335 gnu_field_type, gnu_type, 1, 0, 0, 1),
1336 finish_record_type (gnu_type, gnu_field, 0, 0);
1337 TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_type) = 1;
e2500fed 1338 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
70482933
RK
1339 }
1340
1341 break;
1342
1343 case E_Floating_Point_Type:
1344 /* If this is a VAX floating-point type, use an integer of the proper
1345 size. All the operations will be handled with ASM statements. */
1346 if (Vax_Float (gnat_entity))
1347 {
1348 gnu_type = make_signed_type (esize);
1349 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
fbf5a39b 1350 SET_TYPE_DIGITS_VALUE (gnu_type,
e2500fed 1351 UI_To_Int (Digits_Value (gnat_entity)));
70482933
RK
1352 break;
1353 }
1354
1355 /* The type of the Low and High bounds can be our type if this is
1356 a type from Standard, so set them at the end of the function. */
1357 gnu_type = make_node (REAL_TYPE);
1358 TYPE_PRECISION (gnu_type) = esize;
1359 layout_type (gnu_type);
1360 break;
1361
1362 case E_Floating_Point_Subtype:
1363 if (Vax_Float (gnat_entity))
1364 {
1365 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1366 break;
1367 }
1368
1369 {
1370 enum machine_mode mode;
1371
1372 if (definition == 0
1373 && Present (Ancestor_Subtype (gnat_entity))
1374 && ! In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1375 && (! Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1376 || ! Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1377 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1378 gnu_expr, definition);
1379
1380 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
1381 (GET_MODE_WIDER_MODE (mode) != VOIDmode
1382 && GET_MODE_BITSIZE (GET_MODE_WIDER_MODE (mode)) <= esize);
1383 mode = GET_MODE_WIDER_MODE (mode))
1384 ;
1385
1386 gnu_type = make_node (REAL_TYPE);
1387 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1388 TYPE_PRECISION (gnu_type) = GET_MODE_BITSIZE (mode);
1389
1390 TYPE_MIN_VALUE (gnu_type)
1391 = convert (TREE_TYPE (gnu_type),
1392 elaborate_expression (Type_Low_Bound (gnat_entity),
1393 gnat_entity, get_identifier ("L"),
1394 definition, 1,
1395 Needs_Debug_Info (gnat_entity)));
1396
1397 TYPE_MAX_VALUE (gnu_type)
1398 = convert (TREE_TYPE (gnu_type),
1399 elaborate_expression (Type_High_Bound (gnat_entity),
1400 gnat_entity, get_identifier ("U"),
1401 definition, 1,
1402 Needs_Debug_Info (gnat_entity)));
1403
1404 /* One of the above calls might have caused us to be elaborated,
1405 so don't blow up if so. */
1406 if (present_gnu_tree (gnat_entity))
1407 {
1408 maybe_present = 1;
1409 break;
1410 }
1411
1412 layout_type (gnu_type);
1413 }
1414 break;
1415
1416 /* Array and String Types and Subtypes
1417
1418 Unconstrained array types are represented by E_Array_Type and
1419 constrained array types are represented by E_Array_Subtype. There
1420 are no actual objects of an unconstrained array type; all we have
1421 are pointers to that type.
1422
1423 The following fields are defined on array types and subtypes:
1424
1425 Component_Type Component type of the array.
1426 Number_Dimensions Number of dimensions (an int).
1427 First_Index Type of first index. */
1428
1429 case E_String_Type:
1430 case E_Array_Type:
1431 {
1432 tree gnu_template_fields = NULL_TREE;
1433 tree gnu_template_type = make_node (RECORD_TYPE);
1434 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1435 tree gnu_fat_type = make_node (RECORD_TYPE);
1436 int ndim = Number_Dimensions (gnat_entity);
1437 int firstdim
1438 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1439 int nextdim
1440 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1441 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1442 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1443 tree gnu_comp_size = 0;
1444 tree gnu_max_size = size_one_node;
1445 tree gnu_max_size_unit;
1446 int index;
1447 Entity_Id gnat_ind_subtype;
1448 Entity_Id gnat_ind_base_subtype;
1449 tree gnu_template_reference;
1450 tree tem;
1451
1452 TYPE_NAME (gnu_template_type)
1453 = create_concat_name (gnat_entity, "XUB");
1454 TYPE_NAME (gnu_fat_type) = create_concat_name (gnat_entity, "XUP");
1455 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1456 TREE_READONLY (gnu_template_type) = 1;
1457
1458 /* Make a node for the array. If we are not defining the array
1459 suppress expanding incomplete types and save the node as the type
1460 for GNAT_ENTITY. */
1461 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1462 if (! definition)
1463 {
1464 defer_incomplete_level++;
1465 this_deferred = this_made_decl = 1;
1466 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
1467 ! Comes_From_Source (gnat_entity),
1468 debug_info_p);
1469 save_gnu_tree (gnat_entity, gnu_decl, 0);
1470 saved = 1;
1471 }
1472
1473 /* Build the fat pointer type. Use a "void *" object instead of
1474 a pointer to the array type since we don't have the array type
1475 yet (it will reference the fat pointer via the bounds). */
1476 tem = chainon (chainon (NULL_TREE,
1477 create_field_decl (get_identifier ("P_ARRAY"),
1478 ptr_void_type_node,
1479 gnu_fat_type, 0, 0, 0, 0)),
1480 create_field_decl (get_identifier ("P_BOUNDS"),
1481 gnu_ptr_template,
1482 gnu_fat_type, 0, 0, 0, 0));
1483
1484 /* Make sure we can put this into a register. */
1485 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1486 finish_record_type (gnu_fat_type, tem, 0, 1);
1487
1488 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1489 is the fat pointer. This will be used to access the individual
1490 fields once we build them. */
1491 tem = build (COMPONENT_REF, gnu_ptr_template,
1492 build (PLACEHOLDER_EXPR, gnu_fat_type),
1493 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)));
1494 gnu_template_reference
1495 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1496 TREE_READONLY (gnu_template_reference) = 1;
1497
1498 /* Now create the GCC type for each index and add the fields for
1499 that index to the template. */
1500 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1501 gnat_ind_base_subtype
1502 = First_Index (Implementation_Base_Type (gnat_entity));
1503 index < ndim && index >= 0;
1504 index += nextdim,
1505 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1506 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1507 {
1508 char field_name[10];
1509 tree gnu_ind_subtype
1510 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1511 tree gnu_base_subtype
1512 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1513 tree gnu_base_min
1514 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1515 tree gnu_base_max
1516 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1517 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1518
1519 /* Make the FIELD_DECLs for the minimum and maximum of this
1520 type and then make extractions of that field from the
1521 template. */
1522 set_lineno (gnat_entity, 0);
1523 sprintf (field_name, "LB%d", index);
1524 gnu_min_field = create_field_decl (get_identifier (field_name),
1525 gnu_ind_subtype,
1526 gnu_template_type, 0, 0, 0, 0);
1527 field_name[0] = 'U';
1528 gnu_max_field = create_field_decl (get_identifier (field_name),
1529 gnu_ind_subtype,
1530 gnu_template_type, 0, 0, 0, 0);
1531
1532 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1533
1534 /* We can't use build_component_ref here since the template
1535 type isn't complete yet. */
1536 gnu_min = build (COMPONENT_REF, gnu_ind_subtype,
1537 gnu_template_reference, gnu_min_field);
1538 gnu_max = build (COMPONENT_REF, gnu_ind_subtype,
1539 gnu_template_reference, gnu_max_field);
1540 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1541
1542 /* Make a range type with the new ranges, but using
1543 the Ada subtype. Then we convert to sizetype. */
1544 gnu_index_types[index]
1545 = create_index_type (convert (sizetype, gnu_min),
1546 convert (sizetype, gnu_max),
1547 build_range_type (gnu_ind_subtype,
1548 gnu_min, gnu_max));
1549 /* Update the maximum size of the array, in elements. */
1550 gnu_max_size
1551 = size_binop (MULT_EXPR, gnu_max_size,
1552 size_binop (PLUS_EXPR, size_one_node,
1553 size_binop (MINUS_EXPR, gnu_base_max,
1554 gnu_base_min)));
1555
70482933
RK
1556 TYPE_NAME (gnu_index_types[index])
1557 = create_concat_name (gnat_entity, field_name);
1558 }
1559
1560 for (index = 0; index < ndim; index++)
1561 gnu_template_fields
1562 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1563
1564 /* Install all the fields into the template. */
1565 finish_record_type (gnu_template_type, gnu_template_fields, 0, 0);
1566 TREE_READONLY (gnu_template_type) = 1;
1567
1568 /* Now make the array of arrays and update the pointer to the array
1569 in the fat pointer. Note that it is the first field. */
1570
1571 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1572
1573 /* Get and validate any specified Component_Size, but if Packed,
1574 ignore it since the front end will have taken care of it. Also,
1575 allow sizes not a multiple of Storage_Unit if packed. */
1576 gnu_comp_size
1577 = validate_size (Component_Size (gnat_entity), tem,
1578 gnat_entity,
1579 (Is_Bit_Packed_Array (gnat_entity)
1580 ? TYPE_DECL : VAR_DECL), 1,
1581 Has_Component_Size_Clause (gnat_entity));
1582
1583 if (Has_Atomic_Components (gnat_entity))
1584 check_ok_for_atomic (tem, gnat_entity, 1);
1585
1586 /* If the component type is a RECORD_TYPE that has a self-referential
1587 size, use the maxium size. */
1588 if (gnu_comp_size == 0 && TREE_CODE (tem) == RECORD_TYPE
fbf5a39b 1589 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
70482933
RK
1590 gnu_comp_size = max_size (TYPE_SIZE (tem), 1);
1591
1592 if (! Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size != 0)
1593 {
1594 tem = make_type_from_size (tem, gnu_comp_size, 0);
1595 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1596 "C_PAD", 0, definition, 1);
1597 }
1598
1599 if (Has_Volatile_Components (gnat_entity))
1600 tem = build_qualified_type (tem,
1601 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1602
1603 /* If Component_Size is not already specified, annotate it with the
1604 size of the component. */
1605 if (Unknown_Component_Size (gnat_entity))
1606 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1607
1608 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1609 size_binop (MULT_EXPR, gnu_max_size,
1610 TYPE_SIZE_UNIT (tem)));
1611 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1612 size_binop (MULT_EXPR,
1613 convert (bitsizetype,
1614 gnu_max_size),
1615 TYPE_SIZE (tem)));
1616
1617 for (index = ndim - 1; index >= 0; index--)
1618 {
1619 tem = build_array_type (tem, gnu_index_types[index]);
1620 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
fbf5a39b
AC
1621
1622 /* ??? For now, we say that any component of aggregate type is
1623 addressable because the front end may take 'Reference of it.
1624 But we have to make it addressable if it must be passed by
1625 reference or it that is the default. */
70482933 1626 TYPE_NONALIASED_COMPONENT (tem)
fbf5a39b
AC
1627 = (! Has_Aliased_Components (gnat_entity)
1628 && ! AGGREGATE_TYPE_P (TREE_TYPE (tem)));
70482933
RK
1629 }
1630
1631 /* If an alignment is specified, use it if valid. But ignore it for
1632 types that represent the unpacked base type for packed arrays. */
1633 if (No (Packed_Array_Type (gnat_entity))
1634 && Known_Alignment (gnat_entity))
1635 {
1636 if (No (Alignment (gnat_entity)))
1637 gigi_abort (124);
1638
1639 TYPE_ALIGN (tem)
1640 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1641 TYPE_ALIGN (tem));
1642 }
1643
1644 TYPE_CONVENTION_FORTRAN_P (tem)
1645 = (Convention (gnat_entity) == Convention_Fortran);
1646 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1647
1648 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1649 corresponding fat pointer. */
1650 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1651 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1652 TYPE_MODE (gnu_type) = BLKmode;
1653 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
e2500fed 1654 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
70482933
RK
1655
1656 /* If the maximum size doesn't overflow, use it. */
1657 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1658 && ! TREE_OVERFLOW (gnu_max_size))
07fc65c4
GB
1659 TYPE_SIZE (tem)
1660 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1661 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
1662 && ! TREE_OVERFLOW (gnu_max_size_unit))
1663 TYPE_SIZE_UNIT (tem)
1664 = size_binop (MIN_EXPR, gnu_max_size_unit,
1665 TYPE_SIZE_UNIT (tem));
70482933
RK
1666
1667 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1668 tem, 0, ! Comes_From_Source (gnat_entity),
1669 debug_info_p);
1670 rest_of_type_compilation (gnu_fat_type, global_bindings_p ());
1671
70482933
RK
1672 /* Create a record type for the object and its template and
1673 set the template at a negative offset. */
1674 tem = build_unc_object_type (gnu_template_type, tem,
1675 create_concat_name (gnat_entity, "XUT"));
1676 DECL_FIELD_OFFSET (TYPE_FIELDS (tem))
1677 = size_binop (MINUS_EXPR, size_zero_node,
1678 byte_position (TREE_CHAIN (TYPE_FIELDS (tem))));
1679 DECL_FIELD_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem))) = size_zero_node;
1680 DECL_FIELD_BIT_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem)))
1681 = bitsize_zero_node;
e2500fed 1682 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
70482933
RK
1683 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1684
1685 /* Give the thin pointer type a name. */
1686 create_type_decl (create_concat_name (gnat_entity, "XUX"),
1687 build_pointer_type (tem), 0,
1688 ! Comes_From_Source (gnat_entity), debug_info_p);
1689 }
1690 break;
1691
1692 case E_String_Subtype:
1693 case E_Array_Subtype:
1694
1695 /* This is the actual data type for array variables. Multidimensional
1696 arrays are implemented in the gnu tree as arrays of arrays. Note
1697 that for the moment arrays which have sparse enumeration subtypes as
1698 index components create sparse arrays, which is obviously space
1699 inefficient but so much easier to code for now.
1700
1701 Also note that the subtype never refers to the unconstrained
1702 array type, which is somewhat at variance with Ada semantics.
1703
1704 First check to see if this is simply a renaming of the array
1705 type. If so, the result is the array type. */
1706
1707 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1708 if (! Is_Constrained (gnat_entity))
1709 break;
1710 else
1711 {
1712 int index;
1713 int array_dim = Number_Dimensions (gnat_entity);
1714 int first_dim
1715 = ((Convention (gnat_entity) == Convention_Fortran)
1716 ? array_dim - 1 : 0);
1717 int next_dim
1718 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
1719 Entity_Id gnat_ind_subtype;
1720 Entity_Id gnat_ind_base_subtype;
1721 tree gnu_base_type = gnu_type;
1722 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
1723 tree gnu_comp_size = 0;
1724 tree gnu_max_size = size_one_node;
1725 tree gnu_max_size_unit;
1726 int need_index_type_struct = 0;
1727 int max_overflow = 0;
1728
1729 /* First create the gnu types for each index. Create types for
1730 debugging information to point to the index types if the
1731 are not integer types, have variable bounds, or are
1732 wider than sizetype. */
1733
1734 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1735 gnat_ind_base_subtype
1736 = First_Index (Implementation_Base_Type (gnat_entity));
1737 index < array_dim && index >= 0;
1738 index += next_dim,
1739 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1740 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1741 {
1742 tree gnu_index_subtype
1743 = get_unpadded_type (Etype (gnat_ind_subtype));
1744 tree gnu_min
1745 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
1746 tree gnu_max
1747 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
1748 tree gnu_base_subtype
1749 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1750 tree gnu_base_min
1751 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1752 tree gnu_base_max
1753 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1754 tree gnu_base_type = get_base_type (gnu_base_subtype);
1755 tree gnu_base_base_min
1756 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
1757 tree gnu_base_base_max
1758 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
1759 tree gnu_high;
1760 tree gnu_this_max;
1761
1762 /* If the minimum and maximum values both overflow in
1763 SIZETYPE, but the difference in the original type
1764 does not overflow in SIZETYPE, ignore the overflow
1765 indications. */
1766 if ((TYPE_PRECISION (gnu_index_subtype)
1767 > TYPE_PRECISION (sizetype))
1768 && TREE_CODE (gnu_min) == INTEGER_CST
1769 && TREE_CODE (gnu_max) == INTEGER_CST
1770 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
1771 && (! TREE_OVERFLOW
1772 (fold (build (MINUS_EXPR, gnu_index_subtype,
1773 TYPE_MAX_VALUE (gnu_index_subtype),
1774 TYPE_MIN_VALUE (gnu_index_subtype))))))
1775 TREE_OVERFLOW (gnu_min) = TREE_OVERFLOW (gnu_max)
1776 = TREE_CONSTANT_OVERFLOW (gnu_min)
1777 = TREE_CONSTANT_OVERFLOW (gnu_max) = 0;
1778
1779 /* Similarly, if the range is null, use bounds of 1..0 for
1780 the sizetype bounds. */
1781 else if ((TYPE_PRECISION (gnu_index_subtype)
1782 > TYPE_PRECISION (sizetype))
1783 && TREE_CODE (gnu_min) == INTEGER_CST
1784 && TREE_CODE (gnu_max) == INTEGER_CST
1785 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
1786 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
1787 TYPE_MIN_VALUE (gnu_index_subtype)))
1788 gnu_min = size_one_node, gnu_max = size_zero_node;
1789
1790 /* Now compute the size of this bound. We need to provide
1791 GCC with an upper bound to use but have to deal with the
1792 "superflat" case. There are three ways to do this. If we
1793 can prove that the array can never be superflat, we can
1794 just use the high bound of the index subtype. If we can
1795 prove that the low bound minus one can't overflow, we
1796 can do this as MAX (hb, lb - 1). Otherwise, we have to use
1797 the expression hb >= lb ? hb : lb - 1. */
1798 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
1799
1800 /* See if the base array type is already flat. If it is, we
1801 are probably compiling an ACVC test, but it will cause the
1802 code below to malfunction if we don't handle it specially. */
1803 if (TREE_CODE (gnu_base_min) == INTEGER_CST
1804 && TREE_CODE (gnu_base_max) == INTEGER_CST
1805 && ! TREE_CONSTANT_OVERFLOW (gnu_base_min)
1806 && ! TREE_CONSTANT_OVERFLOW (gnu_base_max)
1807 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
1808 gnu_high = size_zero_node, gnu_min = size_one_node;
1809
1810 /* If gnu_high is now an integer which overflowed, the array
1811 cannot be superflat. */
1812 else if (TREE_CODE (gnu_high) == INTEGER_CST
1813 && TREE_OVERFLOW (gnu_high))
1814 gnu_high = gnu_max;
1815 else if (TREE_UNSIGNED (gnu_base_subtype)
1816 || TREE_CODE (gnu_high) == INTEGER_CST)
1817 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
1818 else
1819 gnu_high
1820 = build_cond_expr
1821 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
1822 gnu_max, gnu_min),
1823 gnu_max, gnu_high);
1824
1825 gnu_index_type[index]
1826 = create_index_type (gnu_min, gnu_high, gnu_index_subtype);
1827
1828 /* Also compute the maximum size of the array. Here we
1829 see if any constraint on the index type of the base type
1830 can be used in the case of self-referential bound on
1831 the index type of the subtype. We look for a non-"infinite"
1832 and non-self-referential bound from any type involved and
1833 handle each bound separately. */
1834
1835 if ((TREE_CODE (gnu_min) == INTEGER_CST
1836 && ! TREE_OVERFLOW (gnu_min)
1837 && ! operand_equal_p (gnu_min, gnu_base_base_min, 0))
fbf5a39b 1838 || ! CONTAINS_PLACEHOLDER_P (gnu_min))
70482933
RK
1839 gnu_base_min = gnu_min;
1840
1841 if ((TREE_CODE (gnu_max) == INTEGER_CST
1842 && ! TREE_OVERFLOW (gnu_max)
1843 && ! operand_equal_p (gnu_max, gnu_base_base_max, 0))
fbf5a39b 1844 || ! CONTAINS_PLACEHOLDER_P (gnu_max))
70482933
RK
1845 gnu_base_max = gnu_max;
1846
1847 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
1848 && TREE_CONSTANT_OVERFLOW (gnu_base_min))
1849 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
1850 || (TREE_CODE (gnu_base_max) == INTEGER_CST
1851 && TREE_CONSTANT_OVERFLOW (gnu_base_max))
1852 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
1853 max_overflow = 1;
1854
1855 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
1856 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
1857
1858 gnu_this_max
1859 = size_binop (MAX_EXPR,
1860 size_binop (PLUS_EXPR, size_one_node,
1861 size_binop (MINUS_EXPR, gnu_base_max,
1862 gnu_base_min)),
1863 size_zero_node);
1864
1865 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1866 && TREE_CONSTANT_OVERFLOW (gnu_this_max))
1867 max_overflow = 1;
1868
1869 gnu_max_size
1870 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1871
1872 if (! integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
1873 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
1874 != INTEGER_CST)
1875 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
1876 || (TREE_TYPE (gnu_index_subtype) != 0
1877 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
1878 != INTEGER_TYPE))
1879 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
1880 || (TYPE_PRECISION (gnu_index_subtype)
1881 > TYPE_PRECISION (sizetype)))
1882 need_index_type_struct = 1;
1883 }
1884
1885 /* Then flatten: create the array of arrays. */
1886
1887 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
1888
1889 /* One of the above calls might have caused us to be elaborated,
1890 so don't blow up if so. */
1891 if (present_gnu_tree (gnat_entity))
1892 {
1893 maybe_present = 1;
1894 break;
1895 }
1896
1897 /* Get and validate any specified Component_Size, but if Packed,
1898 ignore it since the front end will have taken care of it. Also,
1899 allow sizes not a multiple of Storage_Unit if packed. */
1900 gnu_comp_size
1901 = validate_size (Component_Size (gnat_entity), gnu_type,
1902 gnat_entity,
1903 (Is_Bit_Packed_Array (gnat_entity)
1904 ? TYPE_DECL : VAR_DECL),
1905 1, Has_Component_Size_Clause (gnat_entity));
1906
1907 /* If the component type is a RECORD_TYPE that has a self-referential
1908 size, use the maxium size. */
1909 if (gnu_comp_size == 0 && TREE_CODE (gnu_type) == RECORD_TYPE
fbf5a39b 1910 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
70482933
RK
1911 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), 1);
1912
1913 if (! Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size != 0)
1914 {
1915 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, 0);
1916 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
1917 gnat_entity, "C_PAD", 0,
1918 definition, 1);
1919 }
1920
1921 if (Has_Volatile_Components (Base_Type (gnat_entity)))
1922 gnu_type = build_qualified_type (gnu_type,
1923 (TYPE_QUALS (gnu_type)
1924 | TYPE_QUAL_VOLATILE));
1925
1926 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
1927 TYPE_SIZE_UNIT (gnu_type));
1928 gnu_max_size = size_binop (MULT_EXPR,
1929 convert (bitsizetype, gnu_max_size),
1930 TYPE_SIZE (gnu_type));
1931
1932 /* We don't want any array types shared for two reasons: first,
1933 we want to keep differently-named types distinct; second,
1934 setting TYPE_MULTI_ARRAY_TYPE of one type can clobber
1935 another. */
1936 debug_no_type_hash = 1;
1937 for (index = array_dim - 1; index >= 0; index --)
1938 {
1939 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
1940 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
fbf5a39b
AC
1941 /* ??? For now, we say that any component of aggregate type is
1942 addressable because the front end may take 'Reference.
1943 But we have to make it addressable if it must be passed by
1944 reference or it that is the default. */
70482933 1945 TYPE_NONALIASED_COMPONENT (gnu_type)
fbf5a39b
AC
1946 = (! Has_Aliased_Components (gnat_entity)
1947 && ! AGGREGATE_TYPE_P (TREE_TYPE (gnu_type)));
70482933
RK
1948 }
1949
1950 /* If we are at file level and this is a multi-dimensional array, we
1951 need to make a variable corresponding to the stride of the
1952 inner dimensions. */
1953 if (global_bindings_p () && array_dim > 1)
1954 {
1955 tree gnu_str_name = get_identifier ("ST");
1956 tree gnu_arr_type;
1957
1958 for (gnu_arr_type = TREE_TYPE (gnu_type);
1959 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
1960 gnu_arr_type = TREE_TYPE (gnu_arr_type),
1961 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
1962 {
1963 TYPE_SIZE (gnu_arr_type)
1964 = elaborate_expression_1 (gnat_entity, gnat_entity,
1965 TYPE_SIZE (gnu_arr_type),
1966 gnu_str_name, definition, 0);
1967 TYPE_SIZE_UNIT (gnu_arr_type)
1968 = elaborate_expression_1
1969 (gnat_entity, gnat_entity, TYPE_SIZE_UNIT (gnu_arr_type),
1970 concat_id_with_name (gnu_str_name, "U"), definition, 0);
1971 }
1972 }
1973
1974 /* If we need to write out a record type giving the names of
1975 the bounds, do it now. */
1976 if (need_index_type_struct && debug_info_p)
1977 {
1978 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
1979 tree gnu_field_list = 0;
1980 tree gnu_field;
1981
1982 TYPE_NAME (gnu_bound_rec_type)
1983 = create_concat_name (gnat_entity, "XA");
1984
1985 for (index = array_dim - 1; index >= 0; index--)
1986 {
1987 tree gnu_type_name
1988 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
1989
1990 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
1991 gnu_type_name = DECL_NAME (gnu_type_name);
1992
1993 gnu_field = create_field_decl (gnu_type_name,
1994 integer_type_node,
1995 gnu_bound_rec_type,
1996 0, NULL_TREE, NULL_TREE, 0);
1997 TREE_CHAIN (gnu_field) = gnu_field_list;
1998 gnu_field_list = gnu_field;
1999 }
2000
2001 finish_record_type (gnu_bound_rec_type, gnu_field_list, 0, 0);
2002 }
2003
2004 debug_no_type_hash = 0;
2005 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2006 = (Convention (gnat_entity) == Convention_Fortran);
2007
2008 /* If our size depends on a placeholder and the maximum size doesn't
2009 overflow, use it. */
fbf5a39b 2010 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
70482933
RK
2011 && ! (TREE_CODE (gnu_max_size) == INTEGER_CST
2012 && TREE_OVERFLOW (gnu_max_size))
07fc65c4
GB
2013 && ! (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2014 && TREE_OVERFLOW (gnu_max_size_unit))
70482933
RK
2015 && ! max_overflow)
2016 {
2017 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2018 TYPE_SIZE (gnu_type));
2019 TYPE_SIZE_UNIT (gnu_type)
2020 = size_binop (MIN_EXPR, gnu_max_size_unit,
2021 TYPE_SIZE_UNIT (gnu_type));
2022 }
2023
2024 /* Set our alias set to that of our base type. This gives all
2025 array subtypes the same alias set. */
2026 TYPE_ALIAS_SET (gnu_type) = get_alias_set (gnu_base_type);
2027 record_component_aliases (gnu_type);
2028 }
2029
2030 /* If this is a packed type, make this type the same as the packed
2031 array type, but do some adjusting in the type first. */
2032
2033 if (Present (Packed_Array_Type (gnat_entity)))
2034 {
2035 Entity_Id gnat_index;
2036 tree gnu_inner_type;
2037
2038 /* First finish the type we had been making so that we output
2039 debugging information for it */
2040 gnu_type = build_qualified_type (gnu_type,
2041 (TYPE_QUALS (gnu_type)
2042 | (TYPE_QUAL_VOLATILE
fbf5a39b 2043 * Treat_As_Volatile (gnat_entity))));
70482933
RK
2044 set_lineno (gnat_entity, 0);
2045 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2046 ! Comes_From_Source (gnat_entity),
2047 debug_info_p);
2048 if (! Comes_From_Source (gnat_entity))
2049 DECL_ARTIFICIAL (gnu_decl) = 1;
2050
2051 /* Save it as our equivalent in case the call below elaborates
2052 this type again. */
2053 save_gnu_tree (gnat_entity, gnu_decl, 0);
2054
2055 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2056 NULL_TREE, 0);
2057 this_made_decl = 1;
2058 gnu_inner_type = gnu_type = TREE_TYPE (gnu_decl);
2059 save_gnu_tree (gnat_entity, NULL_TREE, 0);
2060
07fc65c4
GB
2061 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2062 && (TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_inner_type)
2063 || TYPE_IS_PADDING_P (gnu_inner_type)))
70482933
RK
2064 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2065
2066 /* We need to point the type we just made to our index type so
2067 the actual bounds can be put into a template. */
2068
2069 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2070 && TYPE_ACTUAL_BOUNDS (gnu_inner_type) == 0)
2071 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2072 && ! TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2073 {
2074 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2075 {
2076 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2077 If it is, we need to make another type. */
2078 if (TYPE_MODULAR_P (gnu_inner_type))
2079 {
2080 tree gnu_subtype;
2081
2082 gnu_subtype = make_node (INTEGER_TYPE);
2083
2084 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2085 TYPE_MIN_VALUE (gnu_subtype)
2086 = TYPE_MIN_VALUE (gnu_inner_type);
2087 TYPE_MAX_VALUE (gnu_subtype)
2088 = TYPE_MAX_VALUE (gnu_inner_type);
2089 TYPE_PRECISION (gnu_subtype)
2090 = TYPE_PRECISION (gnu_inner_type);
2091 TREE_UNSIGNED (gnu_subtype)
2092 = TREE_UNSIGNED (gnu_inner_type);
2093 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2094 layout_type (gnu_subtype);
2095
2096 gnu_inner_type = gnu_subtype;
2097 }
2098
2099 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2100 }
2101
e2500fed 2102 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
70482933
RK
2103
2104 for (gnat_index = First_Index (gnat_entity);
2105 Present (gnat_index); gnat_index = Next_Index (gnat_index))
e2500fed
GK
2106 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type,
2107 tree_cons (NULL_TREE,
70482933 2108 get_unpadded_type (Etype (gnat_index)),
e2500fed 2109 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
70482933
RK
2110
2111 if (Convention (gnat_entity) != Convention_Fortran)
e2500fed
GK
2112 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type,
2113 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
70482933
RK
2114
2115 if (TREE_CODE (gnu_type) == RECORD_TYPE
2116 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_type))
2117 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2118 }
2119 }
2120
2121 /* Abort if packed array with no packed array type field set. */
2122 else if (Is_Packed (gnat_entity))
2123 gigi_abort (107);
2124
2125 break;
2126
2127 case E_String_Literal_Subtype:
2128 /* Create the type for a string literal. */
2129 {
2130 Entity_Id gnat_full_type
2131 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2132 && Present (Full_View (Etype (gnat_entity)))
2133 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2134 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2135 tree gnu_string_array_type
2136 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2137 tree gnu_string_index_type
fbf5a39b
AC
2138 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2139 (TYPE_DOMAIN (gnu_string_array_type))));
70482933
RK
2140 tree gnu_lower_bound
2141 = convert (gnu_string_index_type,
2142 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2143 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2144 tree gnu_length = ssize_int (length - 1);
2145 tree gnu_upper_bound
2146 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2147 gnu_lower_bound,
2148 convert (gnu_string_index_type, gnu_length));
2149 tree gnu_range_type
2150 = build_range_type (gnu_string_index_type,
2151 gnu_lower_bound, gnu_upper_bound);
2152 tree gnu_index_type
2153 = create_index_type (convert (sizetype,
2154 TYPE_MIN_VALUE (gnu_range_type)),
2155 convert (sizetype,
2156 TYPE_MAX_VALUE (gnu_range_type)),
2157 gnu_range_type);
2158
2159 gnu_type
2160 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2161 gnu_index_type);
2162 }
2163 break;
2164
2165 /* Record Types and Subtypes
2166
2167 The following fields are defined on record types:
2168
2169 Has_Discriminants True if the record has discriminants
2170 First_Discriminant Points to head of list of discriminants
2171 First_Entity Points to head of list of fields
2172 Is_Tagged_Type True if the record is tagged
2173
2174 Implementation of Ada records and discriminated records:
2175
2176 A record type definition is transformed into the equivalent of a C
2177 struct definition. The fields that are the discriminants which are
2178 found in the Full_Type_Declaration node and the elements of the
2179 Component_List found in the Record_Type_Definition node. The
2180 Component_List can be a recursive structure since each Variant of
2181 the Variant_Part of the Component_List has a Component_List.
2182
2183 Processing of a record type definition comprises starting the list of
2184 field declarations here from the discriminants and the calling the
2185 function components_to_record to add the rest of the fields from the
2186 component list and return the gnu type node. The function
2187 components_to_record will call itself recursively as it traverses
2188 the tree. */
2189
2190 case E_Record_Type:
70482933
RK
2191 if (Has_Complex_Representation (gnat_entity))
2192 {
2193 gnu_type
2194 = build_complex_type
2195 (get_unpadded_type
2196 (Etype (Defining_Entity
2197 (First (Component_Items
2198 (Component_List
2199 (Type_Definition
2200 (Declaration_Node (gnat_entity)))))))));
2201
07fc65c4 2202 break;
70482933 2203 }
70482933
RK
2204
2205 {
2206 Node_Id full_definition = Declaration_Node (gnat_entity);
2207 Node_Id record_definition = Type_Definition (full_definition);
2208 Entity_Id gnat_field;
2209 tree gnu_field;
2210 tree gnu_field_list = NULL_TREE;
2211 tree gnu_get_parent;
2212 int packed = (Is_Packed (gnat_entity) ? 1
2213 : (Component_Alignment (gnat_entity)
2214 == Calign_Storage_Unit) ? -1
2215 : 0);
2216 int has_rep = Has_Specified_Layout (gnat_entity);
2217 int all_rep = has_rep;
2218 int is_extension
2219 = (Is_Tagged_Type (gnat_entity)
2220 && Nkind (record_definition) == N_Derived_Type_Definition);
2221
2222 /* See if all fields have a rep clause. Stop when we find one
2223 that doesn't. */
2224 for (gnat_field = First_Entity (gnat_entity);
2225 Present (gnat_field) && all_rep;
2226 gnat_field = Next_Entity (gnat_field))
2227 if ((Ekind (gnat_field) == E_Component
2228 || Ekind (gnat_field) == E_Discriminant)
2229 && No (Component_Clause (gnat_field)))
2230 all_rep = 0;
2231
2232 /* If this is a record extension, go a level further to find the
2233 record definition. Also, verify we have a Parent_Subtype. */
2234 if (is_extension)
2235 {
2236 if (! type_annotate_only
2237 || Present (Record_Extension_Part (record_definition)))
2238 record_definition = Record_Extension_Part (record_definition);
2239
2240 if (! type_annotate_only && No (Parent_Subtype (gnat_entity)))
2241 gigi_abort (121);
2242 }
2243
2244 /* Make a node for the record. If we are not defining the record,
2245 suppress expanding incomplete types and save the node as the type
fbf5a39b
AC
2246 for GNAT_ENTITY. We use the same RECORD_TYPE as for a dummy type
2247 and reset TYPE_DUMMY_P to show it's no longer a dummy.
2248
2249 It is very tempting to delay resetting this bit until we are done
2250 with completing the type, e.g. to let possible intermediate
2251 elaboration of access types designating the record know it is not
2252 complete and arrange for update_pointer_to to fix things up later.
2253
2254 It would be wrong, however, because dummy types are expected only
2255 to be created for Ada incomplete or private types, which is not
2256 what we have here. Doing so would make other parts of gigi think
2257 we are dealing with a really incomplete or private type, and have
2258 nasty side effects, typically on the generation of the associated
2259 debugging information. */
70482933
RK
2260 gnu_type = make_dummy_type (gnat_entity);
2261 TYPE_DUMMY_P (gnu_type) = 0;
fbf5a39b 2262
70482933
RK
2263 if (TREE_CODE (TYPE_NAME (gnu_type)) == TYPE_DECL && debug_info_p)
2264 DECL_IGNORED_P (TYPE_NAME (gnu_type)) = 0;
2265
2266 TYPE_ALIGN (gnu_type) = 0;
2267 TYPE_PACKED (gnu_type) = packed != 0 || has_rep;
2268
2269 if (! definition)
2270 {
2271 defer_incomplete_level++;
2272 this_deferred = 1;
2273 set_lineno (gnat_entity, 0);
2274 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2275 ! Comes_From_Source (gnat_entity),
2276 debug_info_p);
2277 save_gnu_tree (gnat_entity, gnu_decl, 0);
2278 this_made_decl = saved = 1;
2279 }
2280
2281 /* If both a size and rep clause was specified, put the size in
2282 the record type now so that it can get the proper mode. */
2283 if (has_rep && Known_Esize (gnat_entity))
2284 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2285
2286 /* Always set the alignment here so that it can be used to
2287 set the mode, if it is making the alignment stricter. If
2288 it is invalid, it will be checked again below. If this is to
fbf5a39b
AC
2289 be Atomic, choose a default alignment of a word unless we know
2290 the size and it's smaller. */
70482933
RK
2291 if (Known_Alignment (gnat_entity))
2292 TYPE_ALIGN (gnu_type)
2293 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2294 else if (Is_Atomic (gnat_entity))
fbf5a39b
AC
2295 TYPE_ALIGN (gnu_type)
2296 = (esize >= BITS_PER_WORD ? BITS_PER_WORD
2297 : 1 << ((floor_log2 (esize) - 1) + 1));
70482933
RK
2298
2299 /* If we have a Parent_Subtype, make a field for the parent. If
2300 this record has rep clauses, force the position to zero. */
2301 if (Present (Parent_Subtype (gnat_entity)))
2302 {
2303 tree gnu_parent;
2304
2305 /* A major complexity here is that the parent subtype will
2306 reference our discriminants. But those must reference
2307 the parent component of this record. So here we will
2308 initialize each of those components to a COMPONENT_REF.
2309 The first operand of that COMPONENT_REF is another
2310 COMPONENT_REF which will be filled in below, once
2311 the parent type can be safely built. */
2312
2313 gnu_get_parent = build (COMPONENT_REF, void_type_node,
2314 build (PLACEHOLDER_EXPR, gnu_type),
2315 build_decl (FIELD_DECL, NULL_TREE,
2316 NULL_TREE));
2317
2318 if (Has_Discriminants (gnat_entity))
fbf5a39b 2319 for (gnat_field = First_Stored_Discriminant (gnat_entity);
70482933 2320 Present (gnat_field);
fbf5a39b 2321 gnat_field = Next_Stored_Discriminant (gnat_field))
70482933
RK
2322 if (Present (Corresponding_Discriminant (gnat_field)))
2323 save_gnu_tree
2324 (gnat_field,
2325 build (COMPONENT_REF,
2326 get_unpadded_type (Etype (gnat_field)),
2327 gnu_get_parent,
2328 gnat_to_gnu_entity (Corresponding_Discriminant
2329 (gnat_field),
2330 NULL_TREE, 0)),
2331 1);
2332
2333 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_entity));
2334
2335 gnu_field_list
2336 = create_field_decl (get_identifier
2337 (Get_Name_String (Name_uParent)),
2338 gnu_parent, gnu_type, 0,
2339 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2340 has_rep ? bitsize_zero_node : 0, 1);
2341 DECL_INTERNAL_P (gnu_field_list) = 1;
2342
2343 TREE_TYPE (gnu_get_parent) = gnu_parent;
2344 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2345 }
2346
2347 /* Add the fields for the discriminants into the record. */
2348 if (! Is_Unchecked_Union (gnat_entity)
2349 && Has_Discriminants (gnat_entity))
fbf5a39b 2350 for (gnat_field = First_Stored_Discriminant (gnat_entity);
70482933 2351 Present (gnat_field);
fbf5a39b 2352 gnat_field = Next_Stored_Discriminant (gnat_field))
70482933
RK
2353 {
2354 /* If this is a record extension and this discriminant
2355 is the renaming of another discriminant, we've already
2356 handled the discriminant above. */
2357 if (Present (Parent_Subtype (gnat_entity))
2358 && Present (Corresponding_Discriminant (gnat_field)))
2359 continue;
2360
2361 gnu_field
2362 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2363
2364 /* Make an expression using a PLACEHOLDER_EXPR from the
2365 FIELD_DECL node just created and link that with the
2366 corresponding GNAT defining identifier. Then add to the
2367 list of fields. */
2368 save_gnu_tree (gnat_field,
2369 build (COMPONENT_REF, TREE_TYPE (gnu_field),
2370 build (PLACEHOLDER_EXPR,
2371 DECL_CONTEXT (gnu_field)),
2372 gnu_field),
2373 1);
2374
2375 TREE_CHAIN (gnu_field) = gnu_field_list;
2376 gnu_field_list = gnu_field;
2377 }
2378
2379 /* Put the discriminants into the record (backwards), so we can
2380 know the appropriate discriminant to use for the names of the
2381 variants. */
2382 TYPE_FIELDS (gnu_type) = gnu_field_list;
2383
2384 /* Add the listed fields into the record and finish up. */
2385 components_to_record (gnu_type, Component_List (record_definition),
2386 gnu_field_list, packed, definition, 0,
2387 0, all_rep);
2388
fbf5a39b 2389 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
70482933
RK
2390 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2391
2392 /* If this is an extension type, reset the tree for any
2393 inherited discriminants. Also remove the PLACEHOLDER_EXPR
2394 for non-inherited discriminants. */
2395 if (! Is_Unchecked_Union (gnat_entity)
2396 && Has_Discriminants (gnat_entity))
fbf5a39b 2397 for (gnat_field = First_Stored_Discriminant (gnat_entity);
70482933 2398 Present (gnat_field);
fbf5a39b 2399 gnat_field = Next_Stored_Discriminant (gnat_field))
70482933
RK
2400 {
2401 if (Present (Parent_Subtype (gnat_entity))
2402 && Present (Corresponding_Discriminant (gnat_field)))
2403 save_gnu_tree (gnat_field, NULL_TREE, 0);
2404 else
2405 {
2406 gnu_field = get_gnu_tree (gnat_field);
2407 save_gnu_tree (gnat_field, NULL_TREE, 0);
2408 save_gnu_tree (gnat_field, TREE_OPERAND (gnu_field, 1), 0);
2409 }
2410 }
2411
2412 /* If it is a tagged record force the type to BLKmode to insure
2413 that these objects will always be placed in memory. Do the
2414 same thing for limited record types. */
70482933
RK
2415 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2416 TYPE_MODE (gnu_type) = BLKmode;
2417
fbf5a39b
AC
2418 /* If this is a derived type, we must make the alias set of this type
2419 the same as that of the type we are derived from. We assume here
2420 that the other type is already frozen. */
2421 if (Etype (gnat_entity) != gnat_entity
2422 && ! (Is_Private_Type (Etype (gnat_entity))
2423 && Full_View (Etype (gnat_entity)) == gnat_entity))
2424 {
2425 TYPE_ALIAS_SET (gnu_type)
2426 = get_alias_set (gnat_to_gnu_type (Etype (gnat_entity)));
2427 record_component_aliases (gnu_type);
2428 }
2429
70482933
RK
2430 /* Fill in locations of fields. */
2431 annotate_rep (gnat_entity, gnu_type);
2432
2433 /* If there are any entities in the chain corresponding to
2434 components that we did not elaborate, ensure we elaborate their
2435 types if they are Itypes. */
2436 for (gnat_temp = First_Entity (gnat_entity);
2437 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2438 if ((Ekind (gnat_temp) == E_Component
2439 || Ekind (gnat_temp) == E_Discriminant)
2440 && Is_Itype (Etype (gnat_temp))
2441 && ! present_gnu_tree (gnat_temp))
2442 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2443 }
2444 break;
2445
2446 case E_Class_Wide_Subtype:
2447 /* If an equivalent type is present, that is what we should use.
2448 Otherwise, fall through to handle this like a record subtype
2449 since it may have constraints. */
2450
2451 if (Present (Equivalent_Type (gnat_entity)))
2452 {
fbf5a39b
AC
2453 gnu_decl = gnat_to_gnu_entity (Equivalent_Type (gnat_entity),
2454 NULL_TREE, 0);
70482933
RK
2455 maybe_present = 1;
2456 break;
2457 }
2458
2459 /* ... fall through ... */
2460
2461 case E_Record_Subtype:
2462
2463 /* If Cloned_Subtype is Present it means this record subtype has
2464 identical layout to that type or subtype and we should use
2465 that GCC type for this one. The front end guarantees that
2466 the component list is shared. */
2467 if (Present (Cloned_Subtype (gnat_entity)))
2468 {
2469 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2470 NULL_TREE, 0);
2471 maybe_present = 1;
2472 }
2473
2474 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2475 changing the type, make a new type with each field having the
2476 type of the field in the new subtype but having the position
2477 computed by transforming every discriminant reference according
2478 to the constraints. We don't see any difference between
2479 private and nonprivate type here since derivations from types should
2480 have been deferred until the completion of the private type. */
2481 else
2482 {
2483 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2484 tree gnu_base_type;
2485 tree gnu_orig_type;
2486
2487 if (! definition)
2488 defer_incomplete_level++, this_deferred = 1;
2489
2490 /* Get the base type initially for its alignment and sizes. But
2491 if it is a padded type, we do all the other work with the
2492 unpadded type. */
2493 gnu_type = gnu_orig_type = gnu_base_type
2494 = gnat_to_gnu_type (gnat_base_type);
2495
2496 if (TREE_CODE (gnu_type) == RECORD_TYPE
2497 && TYPE_IS_PADDING_P (gnu_type))
2498 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2499
2500 if (present_gnu_tree (gnat_entity))
2501 {
2502 maybe_present = 1;
2503 break;
2504 }
2505
2506 /* When the type has discriminants, and these discriminants
2507 affect the shape of what it built, factor them in.
2508
2509 If we are making a subtype of an Unchecked_Union (must be an
2510 Itype), just return the type.
2511
2512 We can't just use Is_Constrained because private subtypes without
2513 discriminants of full types with discriminants with default
2514 expressions are Is_Constrained but aren't constrained! */
2515
2516 if (IN (Ekind (gnat_base_type), Record_Kind)
2517 && ! Is_For_Access_Subtype (gnat_entity)
2518 && ! Is_Unchecked_Union (gnat_base_type)
2519 && Is_Constrained (gnat_entity)
fbf5a39b 2520 && Stored_Constraint (gnat_entity) != No_Elist
70482933
RK
2521 && Present (Discriminant_Constraint (gnat_entity)))
2522 {
2523 Entity_Id gnat_field;
2524 Entity_Id gnat_root_type;
2525 tree gnu_field_list = 0;
2526 tree gnu_pos_list
2527 = compute_field_positions (gnu_orig_type, NULL_TREE,
07fc65c4
GB
2528 size_zero_node, bitsize_zero_node,
2529 BIGGEST_ALIGNMENT);
70482933
RK
2530 tree gnu_subst_list
2531 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2532 definition);
2533 tree gnu_temp;
2534
2535 /* If this is a derived type, we may be seeing fields from any
2536 original records, so add those positions and discriminant
2537 substitutions to our lists. */
2538 for (gnat_root_type = gnat_base_type;
2539 Underlying_Type (Etype (gnat_root_type)) != gnat_root_type;
2540 gnat_root_type = Underlying_Type (Etype (gnat_root_type)))
2541 {
2542 gnu_pos_list
2543 = compute_field_positions
2544 (gnat_to_gnu_type (Etype (gnat_root_type)),
07fc65c4
GB
2545 gnu_pos_list, size_zero_node, bitsize_zero_node,
2546 BIGGEST_ALIGNMENT);
70482933
RK
2547
2548 if (Present (Parent_Subtype (gnat_root_type)))
2549 gnu_subst_list
2550 = substitution_list (Parent_Subtype (gnat_root_type),
2551 Empty, gnu_subst_list, definition);
2552 }
2553
2554 gnu_type = make_node (RECORD_TYPE);
2555 TYPE_NAME (gnu_type) = gnu_entity_id;
2556 TYPE_STUB_DECL (gnu_type)
2557 = pushdecl (build_decl (TYPE_DECL, NULL_TREE, gnu_type));
2558 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2559
2560 for (gnat_field = First_Entity (gnat_entity);
2561 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2562 if (Ekind (gnat_field) == E_Component
2563 || Ekind (gnat_field) == E_Discriminant)
2564 {
2565 tree gnu_old_field
2566 = gnat_to_gnu_entity
2567 (Original_Record_Component (gnat_field), NULL_TREE, 0);
2568 tree gnu_offset
2569 = TREE_VALUE (purpose_member (gnu_old_field,
2570 gnu_pos_list));
2571 tree gnu_pos = TREE_PURPOSE (gnu_offset);
07fc65c4 2572 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
70482933
RK
2573 tree gnu_field_type
2574 = gnat_to_gnu_type (Etype (gnat_field));
2575 tree gnu_size = TYPE_SIZE (gnu_field_type);
2576 tree gnu_new_pos = 0;
07fc65c4
GB
2577 unsigned int offset_align
2578 = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
2579 1);
70482933
RK
2580 tree gnu_field;
2581
2582 /* If there was a component clause, the field types must be
2583 the same for the type and subtype, so copy the data from
2584 the old field to avoid recomputation here. */
2585 if (Present (Component_Clause
2586 (Original_Record_Component (gnat_field))))
2587 {
2588 gnu_size = DECL_SIZE (gnu_old_field);
2589 gnu_field_type = TREE_TYPE (gnu_old_field);
2590 }
2591
2592 /* If this was a bitfield, get the size from the old field.
2593 Also ensure the type can be placed into a bitfield. */
2594 else if (DECL_BIT_FIELD (gnu_old_field))
2595 {
2596 gnu_size = DECL_SIZE (gnu_old_field);
2597 if (TYPE_MODE (gnu_field_type) == BLKmode
2598 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2599 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2600 gnu_field_type = make_packable_type (gnu_field_type);
2601 }
2602
fbf5a39b 2603 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
70482933
RK
2604 for (gnu_temp = gnu_subst_list;
2605 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2606 gnu_pos = substitute_in_expr (gnu_pos,
2607 TREE_PURPOSE (gnu_temp),
2608 TREE_VALUE (gnu_temp));
2609
2610 /* If the size is now a constant, we can set it as the
2611 size of the field when we make it. Otherwise, we need
2612 to deal with it specially. */
2613 if (TREE_CONSTANT (gnu_pos))
2614 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2615
2616 gnu_field
2617 = create_field_decl
2618 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2619 0, gnu_size, gnu_new_pos,
2620 ! DECL_NONADDRESSABLE_P (gnu_old_field));
2621
2622 if (! TREE_CONSTANT (gnu_pos))
2623 {
07fc65c4 2624 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
70482933
RK
2625 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2626 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
07fc65c4 2627 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
70482933
RK
2628 DECL_SIZE (gnu_field) = gnu_size;
2629 DECL_SIZE_UNIT (gnu_field)
2630 = convert (sizetype,
2631 size_binop (CEIL_DIV_EXPR, gnu_size,
2632 bitsize_unit_node));
2633 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2634 }
2635
2636 DECL_INTERNAL_P (gnu_field)
2637 = DECL_INTERNAL_P (gnu_old_field);
e2500fed
GK
2638 SET_DECL_ORIGINAL_FIELD (gnu_field,
2639 (DECL_ORIGINAL_FIELD (gnu_old_field) != 0
fbf5a39b 2640 ? DECL_ORIGINAL_FIELD (gnu_old_field)
e2500fed 2641 : gnu_old_field));
70482933
RK
2642 DECL_DISCRIMINANT_NUMBER (gnu_field)
2643 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2644 TREE_THIS_VOLATILE (gnu_field)
2645 = TREE_THIS_VOLATILE (gnu_old_field);
2646 TREE_CHAIN (gnu_field) = gnu_field_list;
2647 gnu_field_list = gnu_field;
2648 save_gnu_tree (gnat_field, gnu_field, 0);
2649 }
2650
2651 finish_record_type (gnu_type, nreverse (gnu_field_list), 1, 0);
2652
2653 /* Now set the size, alignment and alias set of the new type to
2654 match that of the old one, doing any substitutions, as
2655 above. */
70482933
RK
2656 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2657 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2658 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
e2500fed 2659 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
fbf5a39b
AC
2660 TYPE_ALIAS_SET (gnu_type) = get_alias_set (gnu_base_type);
2661 record_component_aliases (gnu_type);
70482933 2662
fbf5a39b 2663 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
70482933
RK
2664 for (gnu_temp = gnu_subst_list;
2665 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2666 TYPE_SIZE (gnu_type)
2667 = substitute_in_expr (TYPE_SIZE (gnu_type),
2668 TREE_PURPOSE (gnu_temp),
2669 TREE_VALUE (gnu_temp));
2670
fbf5a39b 2671 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
70482933
RK
2672 for (gnu_temp = gnu_subst_list;
2673 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2674 TYPE_SIZE_UNIT (gnu_type)
2675 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
2676 TREE_PURPOSE (gnu_temp),
2677 TREE_VALUE (gnu_temp));
2678
2679 if (TYPE_ADA_SIZE (gnu_type) != 0
fbf5a39b 2680 && CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
70482933
RK
2681 for (gnu_temp = gnu_subst_list;
2682 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
e2500fed
GK
2683 SET_TYPE_ADA_SIZE (gnu_type,
2684 substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
70482933 2685 TREE_PURPOSE (gnu_temp),
e2500fed 2686 TREE_VALUE (gnu_temp)));
70482933
RK
2687
2688 /* Recompute the mode of this record type now that we know its
2689 actual size. */
2690 compute_record_mode (gnu_type);
2691
2692 /* Fill in locations of fields. */
2693 annotate_rep (gnat_entity, gnu_type);
2694 }
2695
2696 /* If we've made a new type, record it and make an XVS type to show
2697 what this is a subtype of. Some debuggers require the XVS
2698 type to be output first, so do it in that order. */
2699 if (gnu_type != gnu_orig_type)
2700 {
2701 if (debug_info_p)
2702 {
2703 tree gnu_subtype_marker = make_node (RECORD_TYPE);
2704 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
2705
2706 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
2707 gnu_orig_name = DECL_NAME (gnu_orig_name);
2708
2709 TYPE_NAME (gnu_subtype_marker)
2710 = create_concat_name (gnat_entity, "XVS");
2711 finish_record_type (gnu_subtype_marker,
2712 create_field_decl (gnu_orig_name,
2713 integer_type_node,
2714 gnu_subtype_marker,
2715 0, NULL_TREE,
2716 NULL_TREE, 0),
2717 0, 0);
2718 }
2719
fbf5a39b 2720 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
70482933
RK
2721 TYPE_NAME (gnu_type) = gnu_entity_id;
2722 TYPE_STUB_DECL (gnu_type)
2723 = pushdecl (build_decl (TYPE_DECL, TYPE_NAME (gnu_type),
2724 gnu_type));
2725 DECL_ARTIFICIAL (TYPE_STUB_DECL (gnu_type)) = 1;
2726 DECL_IGNORED_P (TYPE_STUB_DECL (gnu_type)) = ! debug_info_p;
2727 rest_of_type_compilation (gnu_type, global_bindings_p ());
2728 }
2729
2730 /* Otherwise, go down all the components in the new type and
2731 make them equivalent to those in the base type. */
2732 else
2733 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
2734 gnat_temp = Next_Entity (gnat_temp))
2735 if ((Ekind (gnat_temp) == E_Discriminant
2736 && ! Is_Unchecked_Union (gnat_base_type))
2737 || Ekind (gnat_temp) == E_Component)
2738 save_gnu_tree (gnat_temp,
2739 get_gnu_tree
2740 (Original_Record_Component (gnat_temp)), 0);
2741 }
2742 break;
2743
2744 case E_Access_Subprogram_Type:
2745 /* If we are not defining this entity, and we have incomplete
2746 entities being processed above us, make a dummy type and
2747 fill it in later. */
2748 if (! definition && defer_incomplete_level != 0)
2749 {
2750 struct incomplete *p
2751 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2752
2753 gnu_type
2754 = build_pointer_type
2755 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2756 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2757 ! Comes_From_Source (gnat_entity),
2758 debug_info_p);
2759 save_gnu_tree (gnat_entity, gnu_decl, 0);
2760 this_made_decl = saved = 1;
2761
2762 p->old_type = TREE_TYPE (gnu_type);
2763 p->full_type = Directly_Designated_Type (gnat_entity);
2764 p->next = defer_incomplete_list;
2765 defer_incomplete_list = p;
2766 break;
2767 }
2768
2769 /* ... fall through ... */
2770
2771 case E_Allocator_Type:
2772 case E_Access_Type:
2773 case E_Access_Attribute_Type:
2774 case E_Anonymous_Access_Type:
2775 case E_General_Access_Type:
2776 {
2777 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
2778 Entity_Id gnat_desig_full
2779 = ((IN (Ekind (Etype (gnat_desig_type)),
2780 Incomplete_Or_Private_Kind))
2781 ? Full_View (gnat_desig_type) : 0);
2782 /* We want to know if we'll be seeing the freeze node for any
2783 incomplete type we may be pointing to. */
2784 int in_main_unit
2785 = (Present (gnat_desig_full)
2786 ? In_Extended_Main_Code_Unit (gnat_desig_full)
2787 : In_Extended_Main_Code_Unit (gnat_desig_type));
2788 int got_fat_p = 0;
2789 int made_dummy = 0;
7a3a8c06 2790 tree gnu_desig_type = 0;
70482933
RK
2791
2792 if (No (gnat_desig_full)
2793 && (Ekind (gnat_desig_type) == E_Class_Wide_Type
2794 || (Ekind (gnat_desig_type) == E_Class_Wide_Subtype
2795 && Present (Equivalent_Type (gnat_desig_type)))))
2796 {
2797 if (Present (Equivalent_Type (gnat_desig_type)))
2798 {
2799 gnat_desig_full = Equivalent_Type (gnat_desig_type);
2800 if (IN (Ekind (gnat_desig_full), Incomplete_Or_Private_Kind))
2801 gnat_desig_full = Full_View (gnat_desig_full);
2802 }
2803 else if (IN (Ekind (Root_Type (gnat_desig_type)),
2804 Incomplete_Or_Private_Kind))
2805 gnat_desig_full = Full_View (Root_Type (gnat_desig_type));
2806 }
2807
2808 if (Present (gnat_desig_full) && Is_Concurrent_Type (gnat_desig_full))
2809 gnat_desig_full = Corresponding_Record_Type (gnat_desig_full);
2810
2811 /* If either the designated type or its full view is an
2812 unconstrained array subtype, replace it with the type it's a
2813 subtype of. This avoids problems with multiple copies of
2814 unconstrained array types. */
2815 if (Ekind (gnat_desig_type) == E_Array_Subtype
2816 && ! Is_Constrained (gnat_desig_type))
2817 gnat_desig_type = Etype (gnat_desig_type);
2818 if (Present (gnat_desig_full)
2819 && Ekind (gnat_desig_full) == E_Array_Subtype
2820 && ! Is_Constrained (gnat_desig_full))
2821 gnat_desig_full = Etype (gnat_desig_full);
2822
07fc65c4
GB
2823 /* If the designated type is a subtype of an incomplete record type,
2824 use the parent type to avoid order of elaboration issues. This
2825 can lose some code efficiency, but there is no alternative. */
2826 if (Present (gnat_desig_full)
2827 && Ekind (gnat_desig_full) == E_Record_Subtype
2828 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)
2829 gnat_desig_full = Etype (gnat_desig_full);
2830
70482933
RK
2831 /* If we are pointing to an incomplete type whose completion is an
2832 unconstrained array, make a fat pointer type instead of a pointer
2833 to VOID. The two types in our fields will be pointers to VOID and
2834 will be replaced in update_pointer_to. Similiarly, if the type
2835 itself is a dummy type or an unconstrained array. Also make
2836 a dummy TYPE_OBJECT_RECORD_TYPE in case we have any thin
2837 pointers to it. */
2838
2839 if ((Present (gnat_desig_full)
2840 && Is_Array_Type (gnat_desig_full)
2841 && ! Is_Constrained (gnat_desig_full))
2842 || (present_gnu_tree (gnat_desig_type)
2843 && TYPE_IS_DUMMY_P (TREE_TYPE
2844 (get_gnu_tree (gnat_desig_type)))
2845 && Is_Array_Type (gnat_desig_type)
2846 && ! Is_Constrained (gnat_desig_type))
2847 || (present_gnu_tree (gnat_desig_type)
2848 && (TREE_CODE (TREE_TYPE (get_gnu_tree (gnat_desig_type)))
2849 == UNCONSTRAINED_ARRAY_TYPE)
2850 && (TYPE_POINTER_TO (TREE_TYPE
2851 (get_gnu_tree (gnat_desig_type)))
2852 == 0))
2853 || (No (gnat_desig_full) && ! in_main_unit
2854 && defer_incomplete_level != 0
2855 && ! present_gnu_tree (gnat_desig_type)
2856 && Is_Array_Type (gnat_desig_type)
2857 && ! Is_Constrained (gnat_desig_type)))
2858 {
2859 tree gnu_old
2860 = (present_gnu_tree (gnat_desig_type)
2861 ? gnat_to_gnu_type (gnat_desig_type)
2862 : make_dummy_type (gnat_desig_type));
2863 tree fields;
2864
2865 /* Show the dummy we get will be a fat pointer. */
2866 got_fat_p = made_dummy = 1;
2867
2868 /* If the call above got something that has a pointer, that
2869 pointer is our type. This could have happened either
2870 because the type was elaborated or because somebody
2871 else executed the code below. */
2872 gnu_type = TYPE_POINTER_TO (gnu_old);
2873 if (gnu_type == 0)
2874 {
2875 gnu_type = make_node (RECORD_TYPE);
e2500fed 2876 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
70482933
RK
2877 TYPE_POINTER_TO (gnu_old) = gnu_type;
2878
2879 set_lineno (gnat_entity, 0);
2880 fields
2881 = chainon (chainon (NULL_TREE,
2882 create_field_decl
2883 (get_identifier ("P_ARRAY"),
2884 ptr_void_type_node, gnu_type,
2885 0, 0, 0, 0)),
2886 create_field_decl (get_identifier ("P_BOUNDS"),
2887 ptr_void_type_node,
2888 gnu_type, 0, 0, 0, 0));
2889
2890 /* Make sure we can place this into a register. */
2891 TYPE_ALIGN (gnu_type)
2892 = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
2893 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
2894 finish_record_type (gnu_type, fields, 0, 1);
2895
2896 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
2897 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
2898 = concat_id_with_name (get_entity_name (gnat_desig_type),
2899 "XUT");
2900 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
2901 }
2902 }
2903
2904 /* If we already know what the full type is, use it. */
2905 else if (Present (gnat_desig_full)
2906 && present_gnu_tree (gnat_desig_full))
7a3a8c06 2907 gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
70482933
RK
2908
2909 /* Get the type of the thing we are to point to and build a pointer
2910 to it. If it is a reference to an incomplete or private type with a
2911 full view that is a record, make a dummy type node and get the
2912 actual type later when we have verified it is safe. */
2913 else if (! in_main_unit
2914 && ! present_gnu_tree (gnat_desig_type)
2915 && Present (gnat_desig_full)
2916 && ! present_gnu_tree (gnat_desig_full)
2917 && Is_Record_Type (gnat_desig_full))
2918 {
7a3a8c06 2919 gnu_desig_type = make_dummy_type (gnat_desig_type);
70482933
RK
2920 made_dummy = 1;
2921 }
2922
2923 /* Likewise if we are pointing to a record or array and we are to defer
2924 elaborating incomplete types. We do this since this access type
2925 may be the full view of some private type. Note that the
2926 unconstrained array case is handled above. */
2927 else if ((! in_main_unit || imported_p) && defer_incomplete_level != 0
2928 && ! present_gnu_tree (gnat_desig_type)
2929 && ((Is_Record_Type (gnat_desig_type)
2930 || Is_Array_Type (gnat_desig_type))
2931 || (Present (gnat_desig_full)
2932 && (Is_Record_Type (gnat_desig_full)
2933 || Is_Array_Type (gnat_desig_full)))))
2934 {
7a3a8c06 2935 gnu_desig_type = make_dummy_type (gnat_desig_type);
70482933
RK
2936 made_dummy = 1;
2937 }
2938 else if (gnat_desig_type == gnat_entity)
2939 {
2940 gnu_type = build_pointer_type (make_node (VOID_TYPE));
2941 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
2942 }
2943 else
7a3a8c06 2944 gnu_desig_type = gnat_to_gnu_type (gnat_desig_type);
70482933
RK
2945
2946 /* It is possible that the above call to gnat_to_gnu_type resolved our
2947 type. If so, just return it. */
2948 if (present_gnu_tree (gnat_entity))
2949 {
2950 maybe_present = 1;
2951 break;
2952 }
2953
fbf5a39b
AC
2954 /* If we have a GCC type for the designated type, possibly modify it
2955 if we are pointing only to constant objects and then make a pointer
2956 to it. Don't do this for unconstrained arrays. */
7a3a8c06
RK
2957 if (gnu_type == 0 && gnu_desig_type != 0)
2958 {
2959 if (Is_Access_Constant (gnat_entity)
2960 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
fbf5a39b
AC
2961 {
2962 gnu_desig_type
2963 = build_qualified_type
2964 (gnu_desig_type,
2965 TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
2966
2967 /* Some extra processing is required if we are building a
2968 pointer to an incomplete type (in the GCC sense). We might
2969 have such a type if we just made a dummy, or directly out
2970 of the call to gnat_to_gnu_type above if we are processing
2971 an access type for a record component designating the
2972 record type itself. */
2973 if (! COMPLETE_TYPE_P (gnu_desig_type))
2974 {
2975 /* We must ensure that the pointer to variant we make will
2976 be processed by update_pointer_to when the initial type
2977 is completed. Pretend we made a dummy and let further
2978 processing act as usual. */
2979 made_dummy = 1;
2980
2981 /* We must ensure that update_pointer_to will not retrieve
2982 the dummy variant when building a properly qualified
2983 version of the complete type. We take advantage of the
2984 fact that get_qualified_type is requiring TYPE_NAMEs to
2985 match to influence build_qualified_type and then also
2986 update_pointer_to here. */
2987 TYPE_NAME (gnu_desig_type)
2988 = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
2989 }
2990 }
7a3a8c06
RK
2991
2992 gnu_type = build_pointer_type (gnu_desig_type);
2993 }
2994
70482933
RK
2995 /* If we are not defining this object and we made a dummy pointer,
2996 save our current definition, evaluate the actual type, and replace
2997 the tentative type we made with the actual one. If we are to defer
2998 actually looking up the actual type, make an entry in the
2999 deferred list. */
3000
3001 if (! in_main_unit && made_dummy)
3002 {
3003 tree gnu_old_type
3004 = TYPE_FAT_POINTER_P (gnu_type)
3005 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3006
3007 if (esize == POINTER_SIZE
3008 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
3009 gnu_type
3010 = build_pointer_type
3011 (TYPE_OBJECT_RECORD_TYPE
3012 (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3013
3014 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3015 ! Comes_From_Source (gnat_entity),
3016 debug_info_p);
3017 save_gnu_tree (gnat_entity, gnu_decl, 0);
3018 this_made_decl = saved = 1;
3019
3020 if (defer_incomplete_level == 0)
fbf5a39b
AC
3021 {
3022 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
3023 gnat_to_gnu_type (gnat_desig_type));
3024 /* Note that the call to gnat_to_gnu_type here might have
3025 updated gnu_old_type directly, in which case it is not a
3026 dummy type any more when we get into update_pointer_to.
3027
3028 This may happen for instance when the designated type is a
3029 record type, because their elaboration starts with an
3030 initial node from make_dummy_type, which may yield the same
3031 node as the one we got.
3032
3033 Besides, variants of this non-dummy type might have been
3034 created along the way. update_pointer_to is expected to
3035 properly take care of those situations. */
3036 }
70482933
RK
3037 else
3038 {
3039 struct incomplete *p
3040 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3041
3042 p->old_type = gnu_old_type;
3043 p->full_type = gnat_desig_type;
3044 p->next = defer_incomplete_list;
3045 defer_incomplete_list = p;
3046 }
3047 }
3048 }
3049 break;
3050
3051 case E_Access_Protected_Subprogram_Type:
3052 if (type_annotate_only && No (Equivalent_Type (gnat_entity)))
3053 gnu_type = build_pointer_type (void_type_node);
3054 else
3055 /* The runtime representation is the equivalent type. */
3056 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3057
3058 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3059 && ! present_gnu_tree (Directly_Designated_Type (gnat_entity))
3060 && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3061 && ! Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3062 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3063 NULL_TREE, 0);
3064
3065 break;
3066
3067 case E_Access_Subtype:
3068
3069 /* We treat this as identical to its base type; any constraint is
3070 meaningful only to the front end.
3071
3072 The designated type must be elaborated as well, if it does
3073 not have its own freeze node. Designated (sub)types created
3074 for constrained components of records with discriminants are
3075 not frozen by the front end and thus not elaborated by gigi,
3076 because their use may appear before the base type is frozen,
3077 and because it is not clear that they are needed anywhere in
3078 Gigi. With the current model, there is no correct place where
3079 they could be elaborated. */
3080
3081 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3082 if (Is_Itype (Directly_Designated_Type (gnat_entity))
3083 && ! present_gnu_tree (Directly_Designated_Type (gnat_entity))
3084 && Is_Frozen (Directly_Designated_Type (gnat_entity))
3085 && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3086 {
3087 /* If we are not defining this entity, and we have incomplete
3088 entities being processed above us, make a dummy type and
3089 elaborate it later. */
3090 if (! definition && defer_incomplete_level != 0)
3091 {
3092 struct incomplete *p
3093 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3094 tree gnu_ptr_type
3095 = build_pointer_type
3096 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3097
3098 p->old_type = TREE_TYPE (gnu_ptr_type);
3099 p->full_type = Directly_Designated_Type (gnat_entity);
3100 p->next = defer_incomplete_list;
3101 defer_incomplete_list = p;
3102 }
fbf5a39b
AC
3103 else if
3104 (IN (Ekind (Base_Type (Directly_Designated_Type (gnat_entity))),
3105 Incomplete_Or_Private_Kind))
3106 { ;}
70482933
RK
3107 else
3108 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3109 NULL_TREE, 0);
3110 }
3111
3112 maybe_present = 1;
3113 break;
3114
3115 /* Subprogram Entities
3116
3117 The following access functions are defined for subprograms (functions
3118 or procedures):
3119
3120 First_Formal The first formal parameter.
3121 Is_Imported Indicates that the subprogram has appeared in
3122 an INTERFACE or IMPORT pragma. For now we
3123 assume that the external language is C.
3124 Is_Inlined True if the subprogram is to be inlined.
3125
3126 In addition for function subprograms we have:
3127
3128 Etype Return type of the function.
3129
3130 Each parameter is first checked by calling must_pass_by_ref on its
3131 type to determine if it is passed by reference. For parameters which
3132 are copied in, if they are Ada IN OUT or OUT parameters, their return
3133 value becomes part of a record which becomes the return type of the
3134 function (C function - note that this applies only to Ada procedures
3135 so there is no Ada return type). Additional code to store back the
3136 parameters will be generated on the caller side. This transformation
3137 is done here, not in the front-end.
3138
3139 The intended result of the transformation can be seen from the
3140 equivalent source rewritings that follow:
3141
3142 struct temp {int a,b};
3143 procedure P (A,B: IN OUT ...) is temp P (int A,B) {
3144 .. ..
3145 end P; return {A,B};
3146 }
3147 procedure call
3148
3149 {
3150 temp t;
3151 P(X,Y); t = P(X,Y);
3152 X = t.a , Y = t.b;
3153 }
3154
3155 For subprogram types we need to perform mainly the same conversions to
3156 GCC form that are needed for procedures and function declarations. The
3157 only difference is that at the end, we make a type declaration instead
3158 of a function declaration. */
3159
3160 case E_Subprogram_Type:
3161 case E_Function:
3162 case E_Procedure:
3163 {
3164 /* The first GCC parameter declaration (a PARM_DECL node). The
3165 PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3166 actually is the head of this parameter list. */
3167 tree gnu_param_list = NULL_TREE;
3168 /* The type returned by a function. If the subprogram is a procedure
3169 this type should be void_type_node. */
3170 tree gnu_return_type = void_type_node;
3171 /* List of fields in return type of procedure with copy in copy out
3172 parameters. */
3173 tree gnu_field_list = NULL_TREE;
3174 /* Non-null for subprograms containing parameters passed by copy in
3175 copy out (Ada IN OUT or OUT parameters not passed by reference),
3176 in which case it is the list of nodes used to specify the values of
3177 the in out/out parameters that are returned as a record upon
3178 procedure return. The TREE_PURPOSE of an element of this list is
3179 a field of the record and the TREE_VALUE is the PARM_DECL
3180 corresponding to that field. This list will be saved in the
3181 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
3182 tree gnu_return_list = NULL_TREE;
3183 Entity_Id gnat_param;
3184 int inline_flag = Is_Inlined (gnat_entity);
3185 int public_flag = Is_Public (gnat_entity);
3186 int extern_flag
3187 = (Is_Public (gnat_entity) && !definition) || imported_p;
3188 int pure_flag = Is_Pure (gnat_entity);
3189 int volatile_flag = No_Return (gnat_entity);
3190 int returns_by_ref = 0;
3191 int returns_unconstrained = 0;
fbf5a39b 3192 tree gnu_ext_name = create_concat_name (gnat_entity, 0);
70482933
RK
3193 int has_copy_in_out = 0;
3194 int parmnum;
3195
3196 if (kind == E_Subprogram_Type && ! definition)
3197 /* A parameter may refer to this type, so defer completion
3198 of any incomplete types. */
3199 defer_incomplete_level++, this_deferred = 1;
3200
3201 /* If the subprogram has an alias, it is probably inherited, so
3202 we can use the original one. If the original "subprogram"
3203 is actually an enumeration literal, it may be the first use
3204 of its type, so we must elaborate that type now. */
3205 if (Present (Alias (gnat_entity)))
3206 {
3207 if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3208 gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3209
3210 gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3211 gnu_expr, 0);
3212
3213 /* Elaborate any Itypes in the parameters of this entity. */
3214 for (gnat_temp = First_Formal (gnat_entity);
3215 Present (gnat_temp);
3216 gnat_temp = Next_Formal_With_Extras (gnat_temp))
3217 if (Is_Itype (Etype (gnat_temp)))
3218 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3219
3220 break;
3221 }
3222
3223 if (kind == E_Function || kind == E_Subprogram_Type)
3224 gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3225
3226 /* If this function returns by reference, make the actual
3227 return type of this function the pointer and mark the decl. */
3228 if (Returns_By_Ref (gnat_entity))
3229 {
3230 returns_by_ref = 1;
70482933
RK
3231 gnu_return_type = build_pointer_type (gnu_return_type);
3232 }
3233
fbf5a39b
AC
3234 /* If the Mechanism is By_Reference, ensure the return type uses
3235 the machine's by-reference mechanism, which may not the same
3236 as above (e.g., it might be by passing a fake parameter). */
3237 else if (kind == E_Function
3238 && Mechanism (gnat_entity) == By_Reference)
3239 {
3240 gnu_return_type = copy_type (gnu_return_type);
3241 TREE_ADDRESSABLE (gnu_return_type) = 1;
3242 }
3243
70482933
RK
3244 /* If we are supposed to return an unconstrained array,
3245 actually return a fat pointer and make a note of that. Return
3246 a pointer to an unconstrained record of variable size. */
3247 else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3248 {
3249 gnu_return_type = TREE_TYPE (gnu_return_type);
3250 returns_unconstrained = 1;
3251 }
3252
fbf5a39b
AC
3253 /* If the type requires a transient scope, the result is allocated
3254 on the secondary stack, so the result type of the function is
3255 just a pointer. */
70482933
RK
3256 else if (Requires_Transient_Scope (Etype (gnat_entity)))
3257 {
3258 gnu_return_type = build_pointer_type (gnu_return_type);
3259 returns_unconstrained = 1;
3260 }
3261
3262 /* If the type is a padded type and the underlying type would not
3263 be passed by reference or this function has a foreign convention,
3264 return the underlying type. */
3265 else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3266 && TYPE_IS_PADDING_P (gnu_return_type)
3267 && (! default_pass_by_ref (TREE_TYPE
3268 (TYPE_FIELDS (gnu_return_type)))
3269 || Has_Foreign_Convention (gnat_entity)))
3270 gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3271
3272 /* Look at all our parameters and get the type of
3273 each. While doing this, build a copy-out structure if
3274 we need one. */
3275
3276 for (gnat_param = First_Formal (gnat_entity), parmnum = 0;
3277 Present (gnat_param);
3278 gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3279 {
3280 tree gnu_param_name = get_entity_name (gnat_param);
3281 tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3282 tree gnu_param, gnu_field;
3283 int by_ref_p = 0;
3284 int by_descr_p = 0;
3285 int by_component_ptr_p = 0;
3286 int copy_in_copy_out_flag = 0;
3287 int req_by_copy = 0, req_by_ref = 0;
3288
3289 /* See if a Mechanism was supplied that forced this
3290 parameter to be passed one way or another. */
3291 if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3292 req_by_copy = 1;
3293 else if (Mechanism (gnat_param) == Default)
3294 ;
3295 else if (Mechanism (gnat_param) == By_Copy)
3296 req_by_copy = 1;
3297 else if (Mechanism (gnat_param) == By_Reference)
3298 req_by_ref = 1;
3299 else if (Mechanism (gnat_param) <= By_Descriptor)
3300 by_descr_p = 1;
3301 else if (Mechanism (gnat_param) > 0)
3302 {
3303 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3304 || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3305 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3306 Mechanism (gnat_param)))
3307 req_by_ref = 1;
3308 else
3309 req_by_copy = 1;
3310 }
3311 else
3312 post_error ("unsupported mechanism for&", gnat_param);
3313
3314 /* If this is either a foreign function or if the
3315 underlying type won't be passed by refererence, strip off
3316 possible padding type. */
3317 if (TREE_CODE (gnu_param_type) == RECORD_TYPE
3318 && TYPE_IS_PADDING_P (gnu_param_type)
3319 && (req_by_ref || Has_Foreign_Convention (gnat_entity)
3320 || ! must_pass_by_ref (TREE_TYPE (TYPE_FIELDS
3321 (gnu_param_type)))))
3322 gnu_param_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
3323
3324 /* If this is an IN parameter it is read-only, so make a variant
3325 of the type that is read-only.
3326
3327 ??? However, if this is an unconstrained array, that type can
3328 be very complex. So skip it for now. Likewise for any other
3329 self-referential type. */
3330 if (Ekind (gnat_param) == E_In_Parameter
3331 && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
3332 && ! (TYPE_SIZE (gnu_param_type) != 0
fbf5a39b 3333 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type))))
70482933
RK
3334 gnu_param_type
3335 = build_qualified_type (gnu_param_type,
3336 (TYPE_QUALS (gnu_param_type)
3337 | TYPE_QUAL_CONST));
3338
3339 /* For foreign conventions, pass arrays as a pointer to the
3340 underlying type. First check for unconstrained array and get
3341 the underlying array. Then get the component type and build
3342 a pointer to it. */
3343 if (Has_Foreign_Convention (gnat_entity)
3344 && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
3345 gnu_param_type
3346 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3347 (TREE_TYPE (gnu_param_type))));
3348
3349 if (by_descr_p)
3350 gnu_param_type
3351 = build_pointer_type
3352 (build_vms_descriptor (gnu_param_type,
3353 Mechanism (gnat_param),
3354 gnat_entity));
3355
3356 else if (Has_Foreign_Convention (gnat_entity)
3357 && ! req_by_copy
3358 && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
3359 {
3360 /* Strip off any multi-dimensional entries, then strip
3361 off the last array to get the component type. */
3362 while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
3363 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
3364 gnu_param_type = TREE_TYPE (gnu_param_type);
3365
3366 by_component_ptr_p = 1;
3367 gnu_param_type = TREE_TYPE (gnu_param_type);
3368
3369 if (Ekind (gnat_param) == E_In_Parameter)
3370 gnu_param_type
3371 = build_qualified_type (gnu_param_type,
3372 (TYPE_QUALS (gnu_param_type)
3373 | TYPE_QUAL_CONST));
3374
3375 gnu_param_type = build_pointer_type (gnu_param_type);
3376 }
3377
3378 /* Fat pointers are passed as thin pointers for foreign
3379 conventions. */
3380 else if (Has_Foreign_Convention (gnat_entity)
3381 && TYPE_FAT_POINTER_P (gnu_param_type))
3382 gnu_param_type
3383 = make_type_from_size (gnu_param_type,
3384 size_int (POINTER_SIZE), 0);
3385
3386 /* If we must pass or were requested to pass by reference, do so.
3387 If we were requested to pass by copy, do so.
3388 Otherwise, for foreign conventions, pass all in out parameters
3389 or aggregates by reference. For COBOL and Fortran, pass
3390 all integer and FP types that way too. For Convention Ada,
3391 use the standard Ada default. */
fbf5a39b 3392 else if (must_pass_by_ref (gnu_param_type) || req_by_ref
70482933
RK
3393 || (! req_by_copy
3394 && ((Has_Foreign_Convention (gnat_entity)
3395 && (Ekind (gnat_param) != E_In_Parameter
3396 || AGGREGATE_TYPE_P (gnu_param_type)))
3397 || (((Convention (gnat_entity)
3398 == Convention_Fortran)
3399 || (Convention (gnat_entity)
3400 == Convention_COBOL))
3401 && (INTEGRAL_TYPE_P (gnu_param_type)
3402 || FLOAT_TYPE_P (gnu_param_type)))
3403 /* For convention Ada, see if we pass by reference
3404 by default. */
3405 || (! Has_Foreign_Convention (gnat_entity)
3406 && default_pass_by_ref (gnu_param_type)))))
3407 {
3408 gnu_param_type = build_reference_type (gnu_param_type);
3409 by_ref_p = 1;
3410 }
3411
fbf5a39b 3412 else if (Ekind (gnat_param) != E_In_Parameter)
70482933
RK
3413 copy_in_copy_out_flag = 1;
3414
3415 if (req_by_copy && (by_ref_p || by_component_ptr_p))
3416 post_error ("?cannot pass & by copy", gnat_param);
3417
3418 /* If this is an OUT parameter that isn't passed by reference
3419 and isn't a pointer or aggregate, we don't make a PARM_DECL
3420 for it. Instead, it will be a VAR_DECL created when we process
3421 the procedure. For the special parameter of Valued_Procedure,
fbf5a39b
AC
3422 never pass it in.
3423
3424 An exception is made to cover the RM-6.4.1 rule requiring "by
3425 copy" out parameters with discriminants or implicit initial
3426 values to be handled like in out parameters. These type are
3427 normally built as aggregates, and hence passed by reference,
3428 except for some packed arrays which end up encoded in special
3429 integer types.
3430
3431 The exception we need to make is then for packed arrays of
3432 records with discriminants or implicit initial values. We have
3433 no light/easy way to check for the latter case, so we merely
3434 check for packed arrays of records. This may lead to useless
3435 copy-in operations, but in very rare cases only, as these would
3436 be exceptions in a set of already exceptional situations. */
70482933
RK
3437 if (Ekind (gnat_param) == E_Out_Parameter && ! by_ref_p
3438 && ((Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3439 || (! by_descr_p
3440 && ! POINTER_TYPE_P (gnu_param_type)
fbf5a39b
AC
3441 && ! AGGREGATE_TYPE_P (gnu_param_type)))
3442 && ! (Is_Array_Type (Etype (gnat_param))
3443 && Is_Packed (Etype (gnat_param))
3444 && Is_Composite_Type (Component_Type
3445 (Etype (gnat_param)))))
70482933
RK
3446 gnu_param = 0;
3447 else
3448 {
3449 set_lineno (gnat_param, 0);
3450 gnu_param
3451 = create_param_decl
3452 (gnu_param_name, gnu_param_type,
3453 by_ref_p || by_component_ptr_p
3454 || Ekind (gnat_param) == E_In_Parameter);
3455
3456 DECL_BY_REF_P (gnu_param) = by_ref_p;
3457 DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr_p;
3458 DECL_BY_DESCRIPTOR_P (gnu_param) = by_descr_p;
3459 DECL_POINTS_TO_READONLY_P (gnu_param)
3460 = (Ekind (gnat_param) == E_In_Parameter
3461 && (by_ref_p || by_component_ptr_p));
3462 save_gnu_tree (gnat_param, gnu_param, 0);
3463 gnu_param_list = chainon (gnu_param, gnu_param_list);
3464
3465 /* If a parameter is a pointer, this function may modify
3466 memory through it and thus shouldn't be considered
3467 a pure function. Also, the memory may be modified
3468 between two calls, so they can't be CSE'ed. The latter
3469 case also handles by-ref parameters. */
3470 if (POINTER_TYPE_P (gnu_param_type)
3471 || TYPE_FAT_POINTER_P (gnu_param_type))
3472 pure_flag = 0;
3473 }
3474
fbf5a39b 3475 if (copy_in_copy_out_flag)
70482933
RK
3476 {
3477 if (! has_copy_in_out)
3478 {
3479 if (TREE_CODE (gnu_return_type) != VOID_TYPE)
3480 gigi_abort (111);
3481
3482 gnu_return_type = make_node (RECORD_TYPE);
3483 TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
3484 has_copy_in_out = 1;
3485 }
3486
3487 set_lineno (gnat_param, 0);
3488 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
3489 gnu_return_type, 0, 0, 0, 0);
3490 TREE_CHAIN (gnu_field) = gnu_field_list;
3491 gnu_field_list = gnu_field;
3492 gnu_return_list = tree_cons (gnu_field, gnu_param,
3493 gnu_return_list);
3494 }
3495 }
3496
fbf5a39b
AC
3497 /* Do not compute record for out parameters if subprogram is
3498 stubbed since structures are incomplete for the back-end. */
70482933
RK
3499 if (gnu_field_list != 0
3500 && Convention (gnat_entity) != Convention_Stubbed)
3501 finish_record_type (gnu_return_type, nreverse (gnu_field_list),
3502 0, 0);
3503
3504 /* If we have a CICO list but it has only one entry, we convert
3505 this function into a function that simply returns that one
3506 object. */
3507 if (list_length (gnu_return_list) == 1)
3508 gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
3509
fbf5a39b 3510#ifdef _WIN32
70482933
RK
3511 if (Convention (gnat_entity) == Convention_Stdcall)
3512 {
3513 struct attrib *attr
3514 = (struct attrib *) xmalloc (sizeof (struct attrib));
3515
3516 attr->next = attr_list;
3517 attr->type = ATTR_MACHINE_ATTRIBUTE;
3518 attr->name = get_identifier ("stdcall");
3519 attr->arg = NULL_TREE;
3520 attr->error_point = gnat_entity;
3521 attr_list = attr;
3522 }
fbf5a39b 3523#endif
70482933
RK
3524
3525 /* Both lists ware built in reverse. */
3526 gnu_param_list = nreverse (gnu_param_list);
3527 gnu_return_list = nreverse (gnu_return_list);
3528
3529 gnu_type
3530 = create_subprog_type (gnu_return_type, gnu_param_list,
3531 gnu_return_list, returns_unconstrained,
3532 returns_by_ref,
3533 Function_Returns_With_DSP (gnat_entity));
3534
638e383e 3535 /* ??? For now, don't consider nested functions pure. */
70482933
RK
3536 if (! global_bindings_p ())
3537 pure_flag = 0;
3538
3539 gnu_type
3540 = build_qualified_type (gnu_type,
3541 (TYPE_QUALS (gnu_type)
3542 | (TYPE_QUAL_CONST * pure_flag)
3543 | (TYPE_QUAL_VOLATILE * volatile_flag)));
3544
70482933
RK
3545 set_lineno (gnat_entity, 0);
3546
fbf5a39b
AC
3547 /* If there was no specified Interface_Name and the external and
3548 internal names of the subprogram are the same, only use the
3549 internal name to allow disambiguation of nested subprograms. */
3550 if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_id)
3551 gnu_ext_name = 0;
3552
70482933
RK
3553 /* If we are defining the subprogram and it has an Address clause
3554 we must get the address expression from the saved GCC tree for the
3555 subprogram if it has a Freeze_Node. Otherwise, we elaborate
3556 the address expression here since the front-end has guaranteed
3557 in that case that the elaboration has no effects. If there is
3558 an Address clause and we are not defining the object, just
3559 make it a constant. */
3560 if (Present (Address_Clause (gnat_entity)))
3561 {
3562 tree gnu_address = 0;
3563
3564 if (definition)
3565 gnu_address
3566 = (present_gnu_tree (gnat_entity)
3567 ? get_gnu_tree (gnat_entity)
3568 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
3569
3570 save_gnu_tree (gnat_entity, NULL_TREE, 0);
3571
3572 gnu_type = build_reference_type (gnu_type);
3573 if (gnu_address != 0)
3574 gnu_address = convert (gnu_type, gnu_address);
3575
3576 gnu_decl
3577 = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
3578 gnu_address, 0, Is_Public (gnat_entity),
3579 extern_flag, 0, 0);
3580 DECL_BY_REF_P (gnu_decl) = 1;
3581 }
3582
fbf5a39b
AC
3583 else if (kind == E_Subprogram_Type)
3584 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
70482933
RK
3585 ! Comes_From_Source (gnat_entity),
3586 debug_info_p);
fbf5a39b 3587 else
70482933
RK
3588 {
3589 gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
3590 gnu_type, gnu_param_list,
3591 inline_flag, public_flag,
3592 extern_flag, attr_list);
3593 DECL_STUBBED_P (gnu_decl)
3594 = Convention (gnat_entity) == Convention_Stubbed;
3595 }
3596 }
3597 break;
3598
3599 case E_Incomplete_Type:
3600 case E_Private_Type:
3601 case E_Limited_Private_Type:
3602 case E_Record_Type_With_Private:
3603 case E_Private_Subtype:
3604 case E_Limited_Private_Subtype:
3605 case E_Record_Subtype_With_Private:
3606
3607 /* If this type does not have a full view in the unit we are
3608 compiling, then just get the type from its Etype. */
3609 if (No (Full_View (gnat_entity)))
3610 {
3611 /* If this is an incomplete type with no full view, it must
3612 be a Taft Amendement type, so just return a dummy type. */
3613 if (kind == E_Incomplete_Type)
3614 gnu_type = make_dummy_type (gnat_entity);
3615
3616 else if (Present (Underlying_Full_View (gnat_entity)))
3617 gnu_decl = gnat_to_gnu_entity (Underlying_Full_View (gnat_entity),
3618 NULL_TREE, 0);
3619 else
3620 {
3621 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
3622 NULL_TREE, 0);
3623 maybe_present = 1;
3624 }
3625
3626 break;
3627 }
3628
3629 /* Otherwise, if we are not defining the type now, get the
3630 type from the full view. But always get the type from the full
3631 view for define on use types, since otherwise we won't see them! */
3632
3633 else if (! definition
3634 || (Is_Itype (Full_View (gnat_entity))
3635 && No (Freeze_Node (gnat_entity)))
3636 || (Is_Itype (gnat_entity)
3637 && No (Freeze_Node (Full_View (gnat_entity)))))
3638 {
3639 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
3640 NULL_TREE, 0);
3641 maybe_present = 1;
3642 break;
3643 }
3644
3645 /* For incomplete types, make a dummy type entry which will be
3646 replaced later. */
3647 gnu_type = make_dummy_type (gnat_entity);
3648
3649 /* Save this type as the full declaration's type so we can do any needed
3650 updates when we see it. */
3651 set_lineno (gnat_entity, 0);
3652 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3653 ! Comes_From_Source (gnat_entity),
3654 debug_info_p);
3655 save_gnu_tree (Full_View (gnat_entity), gnu_decl, 0);
3656 break;
3657
3658 /* Simple class_wide types are always viewed as their root_type
3659 by Gigi unless an Equivalent_Type is specified. */
3660 case E_Class_Wide_Type:
3661 if (Present (Equivalent_Type (gnat_entity)))
3662 gnu_type = gnat_to_gnu_type (Equivalent_Type (gnat_entity));
3663 else
3664 gnu_type = gnat_to_gnu_type (Root_Type (gnat_entity));
3665
3666 maybe_present = 1;
3667 break;
3668
3669 case E_Task_Type:
3670 case E_Task_Subtype:
3671 case E_Protected_Type:
3672 case E_Protected_Subtype:
3673 if (type_annotate_only && No (Corresponding_Record_Type (gnat_entity)))
3674 gnu_type = void_type_node;
3675 else
3676 gnu_type = gnat_to_gnu_type (Corresponding_Record_Type (gnat_entity));
3677
3678 maybe_present = 1;
3679 break;
3680
3681 case E_Label:
3682 gnu_decl = create_label_decl (gnu_entity_id);
3683 break;
3684
3685 case E_Block:
3686 case E_Loop:
3687 /* Nothing at all to do here, so just return an ERROR_MARK and claim
3688 we've already saved it, so we don't try to. */
3689 gnu_decl = error_mark_node;
3690 saved = 1;
3691 break;
3692
3693 default:
3694 gigi_abort (113);
3695 }
3696
3697 /* If we had a case where we evaluated another type and it might have
3698 defined this one, handle it here. */
3699 if (maybe_present && present_gnu_tree (gnat_entity))
3700 {
3701 gnu_decl = get_gnu_tree (gnat_entity);
3702 saved = 1;
3703 }
3704
3705 /* If we are processing a type and there is either no decl for it or
3706 we just made one, do some common processing for the type, such as
3707 handling alignment and possible padding. */
3708
3709 if ((gnu_decl == 0 || this_made_decl) && IN (kind, Type_Kind))
3710 {
fbf5a39b
AC
3711 if (Is_Tagged_Type (gnat_entity)
3712 || Is_Class_Wide_Equivalent_Type (gnat_entity))
07fc65c4 3713 TYPE_ALIGN_OK (gnu_type) = 1;
70482933
RK
3714
3715 if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
3716 TYPE_BY_REFERENCE_P (gnu_type) = 1;
3717
3718 /* ??? Don't set the size for a String_Literal since it is either
3719 confirming or we don't handle it properly (if the low bound is
3720 non-constant). */
3721 if (gnu_size == 0 && kind != E_String_Literal_Subtype)
3722 gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
3723 TYPE_DECL, 0, Has_Size_Clause (gnat_entity));
3724
3725 /* If a size was specified, see if we can make a new type of that size
3726 by rearranging the type, for example from a fat to a thin pointer. */
3727 if (gnu_size != 0)
3728 {
3729 gnu_type
3730 = make_type_from_size (gnu_type, gnu_size,
3731 Has_Biased_Representation (gnat_entity));
3732
3733 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
3734 && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
3735 gnu_size = 0;
3736 }
3737
3738 /* If the alignment hasn't already been processed and this is
3739 not an unconstrained array, see if an alignment is specified.
3740 If not, we pick a default alignment for atomic objects. */
3741 if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
3742 ;
3743 else if (Known_Alignment (gnat_entity))
3744 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
3745 TYPE_ALIGN (gnu_type));
3746 else if (Is_Atomic (gnat_entity) && gnu_size == 0
3747 && host_integerp (TYPE_SIZE (gnu_type), 1)
3748 && integer_pow2p (TYPE_SIZE (gnu_type)))
3749 align = MIN (BIGGEST_ALIGNMENT,
3750 tree_low_cst (TYPE_SIZE (gnu_type), 1));
3751 else if (Is_Atomic (gnat_entity) && gnu_size != 0
3752 && host_integerp (gnu_size, 1)
3753 && integer_pow2p (gnu_size))
3754 align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
3755
3756 /* See if we need to pad the type. If we did, and made a record,
3757 the name of the new type may be changed. So get it back for
3758 us when we make the new TYPE_DECL below. */
3759 gnu_type = maybe_pad_type (gnu_type, gnu_size, align,
3760 gnat_entity, "PAD", 1, definition, 0);
3761 if (TREE_CODE (gnu_type) == RECORD_TYPE
3762 && TYPE_IS_PADDING_P (gnu_type))
3763 {
3764 gnu_entity_id = TYPE_NAME (gnu_type);
3765 if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
3766 gnu_entity_id = DECL_NAME (gnu_entity_id);
3767 }
3768
3769 set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
3770
3771 /* If we are at global level, GCC will have applied variable_size to
3772 the type, but that won't have done anything. So, if it's not
3773 a constant or self-referential, call elaborate_expression_1 to
3774 make a variable for the size rather than calculating it each time.
3775 Handle both the RM size and the actual size. */
3776 if (global_bindings_p ()
3777 && TYPE_SIZE (gnu_type) != 0
fbf5a39b
AC
3778 && ! TREE_CONSTANT (TYPE_SIZE (gnu_type))
3779 && ! CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
70482933
RK
3780 {
3781 if (TREE_CODE (gnu_type) == RECORD_TYPE
3782 && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
3783 TYPE_SIZE (gnu_type), 0))
e2500fed
GK
3784 {
3785 TYPE_SIZE (gnu_type)
3786 = elaborate_expression_1 (gnat_entity, gnat_entity,
3787 TYPE_SIZE (gnu_type),
3788 get_identifier ("SIZE"),
3789 definition, 0);
3790 SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
3791 }
70482933
RK
3792 else
3793 {
3794 TYPE_SIZE (gnu_type)
3795 = elaborate_expression_1 (gnat_entity, gnat_entity,
3796 TYPE_SIZE (gnu_type),
3797 get_identifier ("SIZE"),
3798 definition, 0);
07fc65c4
GB
3799
3800 /* ??? For now, store the size as a multiple of the alignment
3801 in bytes so that we can see the alignment from the tree. */
70482933 3802 TYPE_SIZE_UNIT (gnu_type)
07fc65c4
GB
3803 = build_binary_op
3804 (MULT_EXPR, sizetype,
3805 elaborate_expression_1
3806 (gnat_entity, gnat_entity,
3807 build_binary_op (EXACT_DIV_EXPR, sizetype,
3808 TYPE_SIZE_UNIT (gnu_type),
3809 size_int (TYPE_ALIGN (gnu_type)
3810 / BITS_PER_UNIT)),
3811 get_identifier ("SIZE_A_UNIT"),
3812 definition, 0),
3813 size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
3814
3815 if (TREE_CODE (gnu_type) == RECORD_TYPE)
e2500fed
GK
3816 SET_TYPE_ADA_SIZE (gnu_type,
3817 elaborate_expression_1 (gnat_entity, gnat_entity,
07fc65c4
GB
3818 TYPE_ADA_SIZE (gnu_type),
3819 get_identifier ("RM_SIZE"),
e2500fed 3820 definition, 0));
70482933
RK
3821 }
3822 }
3823
3824 /* If this is a record type or subtype, call elaborate_expression_1 on
3825 any field position. Do this for both global and local types.
3826 Skip any fields that we haven't made trees for to avoid problems with
3827 class wide types. */
3828 if (IN (kind, Record_Kind))
3829 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3830 gnat_temp = Next_Entity (gnat_temp))
3831 if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
3832 {
3833 tree gnu_field = get_gnu_tree (gnat_temp);
3834
07fc65c4
GB
3835 /* ??? Unfortunately, GCC needs to be able to prove the
3836 alignment of this offset and if it's a variable, it can't.
fbf5a39b 3837 In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
07fc65c4
GB
3838 right now, we have to put in an explicit multiply and
3839 divide by that value. */
fbf5a39b 3840 if (! CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
70482933 3841 DECL_FIELD_OFFSET (gnu_field)
07fc65c4
GB
3842 = build_binary_op
3843 (MULT_EXPR, sizetype,
3844 elaborate_expression_1
fbf5a39b 3845 (gnat_temp, gnat_temp,
07fc65c4
GB
3846 build_binary_op (EXACT_DIV_EXPR, sizetype,
3847 DECL_FIELD_OFFSET (gnu_field),
3848 size_int (DECL_OFFSET_ALIGN (gnu_field)
3849 / BITS_PER_UNIT)),
3850 get_identifier ("OFFSET"),
3851 definition, 0),
3852 size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
70482933
RK
3853 }
3854
3855 gnu_type = build_qualified_type (gnu_type,
3856 (TYPE_QUALS (gnu_type)
3857 | (TYPE_QUAL_VOLATILE
fbf5a39b 3858 * Treat_As_Volatile (gnat_entity))));
70482933
RK
3859
3860 if (Is_Atomic (gnat_entity))
3861 check_ok_for_atomic (gnu_type, gnat_entity, 0);
3862
3863 if (Known_Alignment (gnat_entity))
3864 TYPE_USER_ALIGN (gnu_type) = 1;
3865
3866 if (gnu_decl == 0)
3867 {
3868 set_lineno (gnat_entity, 0);
3869 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3870 ! Comes_From_Source (gnat_entity),
3871 debug_info_p);
3872 }
3873 else
3874 TREE_TYPE (gnu_decl) = gnu_type;
3875 }
3876
3877 if (IN (kind, Type_Kind) && ! TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
3878 {
3879 gnu_type = TREE_TYPE (gnu_decl);
3880
3881 /* Back-annotate the Alignment of the type if not already in the
3882 tree. Likewise for sizes. */
3883 if (Unknown_Alignment (gnat_entity))
3884 Set_Alignment (gnat_entity,
3885 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
3886
3887 if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type) != 0)
3888 {
3889 /* If the size is self-referential, we annotate the maximum
3890 value of that size. */
3891 tree gnu_size = TYPE_SIZE (gnu_type);
3892
fbf5a39b 3893 if (CONTAINS_PLACEHOLDER_P (gnu_size))
70482933
RK
3894 gnu_size = max_size (gnu_size, 1);
3895
3896 Set_Esize (gnat_entity, annotate_value (gnu_size));
fbf5a39b
AC
3897
3898 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
3899 {
3900 /* In this mode the tag and the parent components are not
3901 generated by the front-end, so the sizes must be adjusted
3902 explicitly now. */
3903
3904 int size_offset;
3905 int new_size;
3906
3907 if (Is_Derived_Type (gnat_entity))
3908 {
3909 size_offset
3910 = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
3911 Set_Alignment (gnat_entity,
3912 Alignment (Etype (Base_Type (gnat_entity))));
3913 }
3914 else
3915 size_offset = POINTER_SIZE;
3916
3917 new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
3918 Set_Esize (gnat_entity,
3919 UI_From_Int (((new_size + (POINTER_SIZE - 1))
3920 / POINTER_SIZE) * POINTER_SIZE));
3921 Set_RM_Size (gnat_entity, Esize (gnat_entity));
3922 }
70482933
RK
3923 }
3924
3925 if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type) != 0)
3926 Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
3927 }
3928
3929 if (! Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
3930 DECL_ARTIFICIAL (gnu_decl) = 1;
3931
3932 if (! debug_info_p && DECL_P (gnu_decl)
3933 && TREE_CODE (gnu_decl) != FUNCTION_DECL)
3934 DECL_IGNORED_P (gnu_decl) = 1;
3935
3936 /* If this decl is really indirect, adjust it. */
3937 if (TREE_CODE (gnu_decl) == VAR_DECL)
3938 adjust_decl_rtl (gnu_decl);
3939
3940 /* If we haven't already, associate the ..._DECL node that we just made with
3941 the input GNAT entity node. */
3942 if (! saved)
3943 save_gnu_tree (gnat_entity, gnu_decl, 0);
3944
3945 /* If this is an enumeral or floating-point type, we were not able to set
3946 the bounds since they refer to the type. These bounds are always static.
3947
3948 For enumeration types, also write debugging information and declare the
3949 enumeration literal table, if needed. */
3950
3951 if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
3952 || (kind == E_Floating_Point_Type && ! Vax_Float (gnat_entity)))
3953 {
3954 tree gnu_scalar_type = gnu_type;
3955
3956 /* If this is a padded type, we need to use the underlying type. */
3957 if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
3958 && TYPE_IS_PADDING_P (gnu_scalar_type))
3959 gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
3960
3961 /* If this is a floating point type and we haven't set a floating
3962 point type yet, use this in the evaluation of the bounds. */
3963 if (longest_float_type_node == 0 && kind == E_Floating_Point_Type)
3964 longest_float_type_node = gnu_type;
3965
3966 TYPE_MIN_VALUE (gnu_scalar_type)
3967 = gnat_to_gnu (Type_Low_Bound (gnat_entity));
3968 TYPE_MAX_VALUE (gnu_scalar_type)
3969 = gnat_to_gnu (Type_High_Bound (gnat_entity));
3970
3971 if (kind == E_Enumeration_Type)
3972 {
3973 TYPE_STUB_DECL (gnu_scalar_type) = gnu_decl;
3974
3975 /* Since this has both a typedef and a tag, avoid outputting
3976 the name twice. */
3977 DECL_ARTIFICIAL (gnu_decl) = 1;
3978 rest_of_type_compilation (gnu_scalar_type, global_bindings_p ());
3979 }
3980 }
3981
3982 /* If we deferred processing of incomplete types, re-enable it. If there
3983 were no other disables and we have some to process, do so. */
3984 if (this_deferred && --defer_incomplete_level == 0
3985 && defer_incomplete_list != 0)
3986 {
3987 struct incomplete *incp = defer_incomplete_list;
3988 struct incomplete *next;
3989
3990 defer_incomplete_list = 0;
3991 for (; incp; incp = next)
3992 {
3993 next = incp->next;
3994
3995 if (incp->old_type != 0)
7a3a8c06 3996 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
70482933
RK
3997 gnat_to_gnu_type (incp->full_type));
3998 free (incp);
3999 }
4000 }
4001
4002 /* If we are not defining this type, see if it's in the incomplete list.
4003 If so, handle that list entry now. */
4004 else if (! definition)
4005 {
4006 struct incomplete *incp;
4007
4008 for (incp = defer_incomplete_list; incp; incp = incp->next)
4009 if (incp->old_type != 0 && incp->full_type == gnat_entity)
4010 {
7a3a8c06
RK
4011 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4012 TREE_TYPE (gnu_decl));
70482933
RK
4013 incp->old_type = 0;
4014 }
4015 }
4016
4017 if (this_global)
4018 force_global--;
4019
4020 if (Is_Packed_Array_Type (gnat_entity)
4021 && Is_Itype (Associated_Node_For_Itype (gnat_entity))
4022 && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
4023 && ! present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
4024 gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
4025
4026 return gnu_decl;
4027}
4028\f
4029/* Given GNAT_ENTITY, elaborate all expressions that are required to
4030 be elaborated at the point of its definition, but do nothing else. */
4031
4032void
4033elaborate_entity (gnat_entity)
4034 Entity_Id gnat_entity;
4035{
4036 switch (Ekind (gnat_entity))
4037 {
4038 case E_Signed_Integer_Subtype:
4039 case E_Modular_Integer_Subtype:
4040 case E_Enumeration_Subtype:
4041 case E_Ordinary_Fixed_Point_Subtype:
4042 case E_Decimal_Fixed_Point_Subtype:
4043 case E_Floating_Point_Subtype:
4044 {
4045 Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
4046 Node_Id gnat_hb = Type_High_Bound (gnat_entity);
4047
4048 /* ??? Tests for avoiding static constaint error expression
4049 is needed until the front stops generating bogus conversions
4050 on bounds of real types. */
4051
4052 if (! Raises_Constraint_Error (gnat_lb))
4053 elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
4054 1, 0, Needs_Debug_Info (gnat_entity));
4055 if (! Raises_Constraint_Error (gnat_hb))
4056 elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
4057 1, 0, Needs_Debug_Info (gnat_entity));
4058 break;
4059 }
4060
4061 case E_Record_Type:
4062 {
4063 Node_Id full_definition = Declaration_Node (gnat_entity);
4064 Node_Id record_definition = Type_Definition (full_definition);
4065
4066 /* If this is a record extension, go a level further to find the
4067 record definition. */
4068 if (Nkind (record_definition) == N_Derived_Type_Definition)
4069 record_definition = Record_Extension_Part (record_definition);
4070 }
4071 break;
4072
4073 case E_Record_Subtype:
4074 case E_Private_Subtype:
4075 case E_Limited_Private_Subtype:
4076 case E_Record_Subtype_With_Private:
4077 if (Is_Constrained (gnat_entity)
4078 && Has_Discriminants (Base_Type (gnat_entity))
4079 && Present (Discriminant_Constraint (gnat_entity)))
4080 {
4081 Node_Id gnat_discriminant_expr;
4082 Entity_Id gnat_field;
4083
4084 for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
4085 gnat_discriminant_expr
4086 = First_Elmt (Discriminant_Constraint (gnat_entity));
4087 Present (gnat_field);
4088 gnat_field = Next_Discriminant (gnat_field),
4089 gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
4090 /* ??? For now, ignore access discriminants. */
4091 if (! Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
4092 elaborate_expression (Node (gnat_discriminant_expr),
4093 gnat_entity,
4094 get_entity_name (gnat_field), 1, 0, 0);
4095 }
4096 break;
4097
4098 }
4099}
4100\f
4101/* Mark GNAT_ENTITY as going out of scope at this point. Recursively mark
4102 any entities on its entity chain similarly. */
4103
4104void
4105mark_out_of_scope (gnat_entity)
4106 Entity_Id gnat_entity;
4107{
4108 Entity_Id gnat_sub_entity;
4109 unsigned int kind = Ekind (gnat_entity);
4110
4111 /* If this has an entity list, process all in the list. */
4112 if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
4113 || IN (kind, Private_Kind)
4114 || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
4115 || kind == E_Function || kind == E_Generic_Function
4116 || kind == E_Generic_Package || kind == E_Generic_Procedure
4117 || kind == E_Loop || kind == E_Operator || kind == E_Package
4118 || kind == E_Package_Body || kind == E_Procedure
4119 || kind == E_Record_Type || kind == E_Record_Subtype
4120 || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
4121 for (gnat_sub_entity = First_Entity (gnat_entity);
4122 Present (gnat_sub_entity);
4123 gnat_sub_entity = Next_Entity (gnat_sub_entity))
4124 if (Scope (gnat_sub_entity) == gnat_entity
4125 && gnat_sub_entity != gnat_entity)
4126 mark_out_of_scope (gnat_sub_entity);
4127
4128 /* Now clear this if it has been defined, but only do so if it isn't
4129 a subprogram or parameter. We could refine this, but it isn't
4130 worth it. If this is statically allocated, it is supposed to
4131 hang around out of cope. */
4132 if (present_gnu_tree (gnat_entity) && ! Is_Statically_Allocated (gnat_entity)
4133 && kind != E_Procedure && kind != E_Function && ! IN (kind, Formal_Kind))
4134 {
4135 save_gnu_tree (gnat_entity, NULL_TREE, 1);
4136 save_gnu_tree (gnat_entity, error_mark_node, 1);
4137 }
4138}
4139\f
4140/* Return a TREE_LIST describing the substitutions needed to reflect
4141 discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
4142 them to GNU_LIST. If GNAT_TYPE is not specified, use the base type
4143 of GNAT_SUBTYPE. The substitions can be in any order. TREE_PURPOSE
4144 gives the tree for the discriminant and TREE_VALUES is the replacement
4145 value. They are in the form of operands to substitute_in_expr.
4146 DEFINITION is as in gnat_to_gnu_entity. */
4147
4148static tree
4149substitution_list (gnat_subtype, gnat_type, gnu_list, definition)
4150 Entity_Id gnat_subtype;
4151 Entity_Id gnat_type;
4152 tree gnu_list;
4153 int definition;
4154{
4155 Entity_Id gnat_discrim;
4156 Node_Id gnat_value;
4157
4158 if (No (gnat_type))
4159 gnat_type = Implementation_Base_Type (gnat_subtype);
4160
4161 if (Has_Discriminants (gnat_type))
fbf5a39b
AC
4162 for (gnat_discrim = First_Stored_Discriminant (gnat_type),
4163 gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
70482933 4164 Present (gnat_discrim);
fbf5a39b 4165 gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
70482933
RK
4166 gnat_value = Next_Elmt (gnat_value))
4167 /* Ignore access discriminants. */
4168 if (! Is_Access_Type (Etype (Node (gnat_value))))
4169 gnu_list = tree_cons (gnat_to_gnu_entity (gnat_discrim, NULL_TREE, 0),
4170 elaborate_expression
4171 (Node (gnat_value), gnat_subtype,
4172 get_entity_name (gnat_discrim), definition,
4173 1, 0),
4174 gnu_list);
4175
4176 return gnu_list;
4177}
4178\f
4179/* For the following two functions: for each GNAT entity, the GCC
4180 tree node used as a dummy for that entity, if any. */
4181
e2500fed 4182static GTY((length ("max_gnat_nodes"))) tree * dummy_node_table;
70482933
RK
4183
4184/* Initialize the above table. */
4185
4186void
4187init_dummy_type ()
4188{
4189 Node_Id gnat_node;
4190
e2500fed 4191 dummy_node_table = (tree *) ggc_alloc (max_gnat_nodes * sizeof (tree));
70482933
RK
4192
4193 for (gnat_node = 0; gnat_node < max_gnat_nodes; gnat_node++)
4194 dummy_node_table[gnat_node] = NULL_TREE;
4195
4196 dummy_node_table -= First_Node_Id;
4197}
4198
4199/* Make a dummy type corresponding to GNAT_TYPE. */
4200
4201tree
4202make_dummy_type (gnat_type)
4203 Entity_Id gnat_type;
4204{
4205 Entity_Id gnat_underlying;
4206 tree gnu_type;
4207
4208 /* Find a full type for GNAT_TYPE, taking into account any class wide
4209 types. */
4210 if (Is_Class_Wide_Type (gnat_type) && Present (Equivalent_Type (gnat_type)))
4211 gnat_type = Equivalent_Type (gnat_type);
4212 else if (Ekind (gnat_type) == E_Class_Wide_Type)
4213 gnat_type = Root_Type (gnat_type);
4214
4215 for (gnat_underlying = gnat_type;
4216 (IN (Ekind (gnat_underlying), Incomplete_Or_Private_Kind)
4217 && Present (Full_View (gnat_underlying)));
4218 gnat_underlying = Full_View (gnat_underlying))
4219 ;
4220
4221 /* If it there already a dummy type, use that one. Else make one. */
4222 if (dummy_node_table[gnat_underlying])
4223 return dummy_node_table[gnat_underlying];
4224
4225 /* If this is a record, make this a RECORD_TYPE or UNION_TYPE; else make
4226 it a VOID_TYPE. */
4227 if (Is_Record_Type (gnat_underlying))
4228 gnu_type = make_node (Is_Unchecked_Union (gnat_underlying)
4229 ? UNION_TYPE : RECORD_TYPE);
4230 else
4231 gnu_type = make_node (ENUMERAL_TYPE);
4232
4233 TYPE_NAME (gnu_type) = get_entity_name (gnat_type);
4234 if (AGGREGATE_TYPE_P (gnu_type))
4235 TYPE_STUB_DECL (gnu_type)
4236 = pushdecl (build_decl (TYPE_DECL, NULL_TREE, gnu_type));
4237
4238 TYPE_DUMMY_P (gnu_type) = 1;
4239 dummy_node_table[gnat_underlying] = gnu_type;
4240
4241 return gnu_type;
4242}
4243\f
4244/* Return 1 if the size represented by GNU_SIZE can be handled by an
4245 allocation. If STATIC_P is non-zero, consider only what can be
4246 done with a static allocation. */
4247
4248static int
4249allocatable_size_p (gnu_size, static_p)
4250 tree gnu_size;
4251 int static_p;
4252{
fbf5a39b
AC
4253 HOST_WIDE_INT our_size;
4254
70482933
RK
4255 /* If this is not a static allocation, the only case we want to forbid
4256 is an overflowing size. That will be converted into a raise a
4257 Storage_Error. */
4258 if (! static_p)
4259 return ! (TREE_CODE (gnu_size) == INTEGER_CST
4260 && TREE_CONSTANT_OVERFLOW (gnu_size));
4261
4262 /* Otherwise, we need to deal with both variable sizes and constant
fbf5a39b
AC
4263 sizes that won't fit in a host int. We use int instead of HOST_WIDE_INT
4264 since assemblers may not like very large sizes. */
4265 if (!host_integerp (gnu_size, 1))
4266 return 0;
4267
4268 our_size = tree_low_cst (gnu_size, 1);
4269 return (int) our_size == our_size;
70482933
RK
4270}
4271\f
4272/* Return a list of attributes for GNAT_ENTITY, if any. */
4273
4274static struct attrib *
4275build_attr_list (gnat_entity)
4276 Entity_Id gnat_entity;
4277{
4278 struct attrib *attr_list = 0;
4279 Node_Id gnat_temp;
4280
4281 for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
4282 gnat_temp = Next_Rep_Item (gnat_temp))
4283 if (Nkind (gnat_temp) == N_Pragma)
4284 {
4285 struct attrib *attr;
4286 tree gnu_arg0 = 0, gnu_arg1 = 0;
4287 Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
4288 enum attr_type etype;
4289
4290 if (Present (gnat_assoc) && Present (First (gnat_assoc))
4291 && Present (Next (First (gnat_assoc)))
4292 && (Nkind (Expression (Next (First (gnat_assoc))))
4293 == N_String_Literal))
4294 {
4295 gnu_arg0 = get_identifier (TREE_STRING_POINTER
4296 (gnat_to_gnu
4297 (Expression (Next
4298 (First (gnat_assoc))))));
4299 if (Present (Next (Next (First (gnat_assoc))))
4300 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
4301 == N_String_Literal))
4302 gnu_arg1 = get_identifier (TREE_STRING_POINTER
4303 (gnat_to_gnu
4304 (Expression
4305 (Next (Next
4306 (First (gnat_assoc)))))));
4307 }
4308
4309 switch (Get_Pragma_Id (Chars (gnat_temp)))
4310 {
4311 case Pragma_Machine_Attribute:
4312 etype = ATTR_MACHINE_ATTRIBUTE;
4313 break;
4314
4315 case Pragma_Linker_Alias:
4316 etype = ATTR_LINK_ALIAS;
4317 break;
4318
4319 case Pragma_Linker_Section:
4320 etype = ATTR_LINK_SECTION;
4321 break;
4322
4323 case Pragma_Weak_External:
4324 etype = ATTR_WEAK_EXTERNAL;
4325 break;
4326
4327 default:
4328 continue;
4329 }
4330
4331 attr = (struct attrib *) xmalloc (sizeof (struct attrib));
4332 attr->next = attr_list;
4333 attr->type = etype;
4334 attr->name = gnu_arg0;
4335 attr->arg = gnu_arg1;
4336 attr->error_point
4337 = Present (Next (First (gnat_assoc)))
4338 ? Expression (Next (First (gnat_assoc))) : gnat_temp;
4339 attr_list = attr;
4340 }
4341
4342 return attr_list;
4343}
4344\f
4345/* Get the unpadded version of a GNAT type. */
4346
4347tree
4348get_unpadded_type (gnat_entity)
4349 Entity_Id gnat_entity;
4350{
4351 tree type = gnat_to_gnu_type (gnat_entity);
4352
4353 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4354 type = TREE_TYPE (TYPE_FIELDS (type));
4355
4356 return type;
4357}
4358\f
4359/* Called when we need to protect a variable object using a save_expr. */
4360
4361tree
4362maybe_variable (gnu_operand, gnat_node)
4363 tree gnu_operand;
4364 Node_Id gnat_node;
4365{
4366 if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
4367 || TREE_CODE (gnu_operand) == SAVE_EXPR
4368 || TREE_CODE (gnu_operand) == NULL_EXPR)
4369 return gnu_operand;
4370
4371 /* If we will be generating code, make sure we are at the proper
4372 line number. */
fbf5a39b 4373 if (! global_bindings_p () && ! CONTAINS_PLACEHOLDER_P (gnu_operand))
70482933
RK
4374 set_lineno (gnat_node, 1);
4375
4376 if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
4377 return build1 (UNCONSTRAINED_ARRAY_REF, TREE_TYPE (gnu_operand),
4378 variable_size (TREE_OPERAND (gnu_operand, 0)));
4379 else
4380 return variable_size (gnu_operand);
4381}
4382\f
4383/* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
4384 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
4385 return the GCC tree to use for that expression. GNU_NAME is the
4386 qualification to use if an external name is appropriate and DEFINITION is
4387 nonzero if this is a definition of GNAT_ENTITY. If NEED_VALUE is nonzero,
4388 we need a result. Otherwise, we are just elaborating this for
4389 side-effects. If NEED_DEBUG is nonzero we need the symbol for debugging
4390 purposes even if it isn't needed for code generation. */
4391
4392static tree
4393elaborate_expression (gnat_expr, gnat_entity, gnu_name, definition,
4394 need_value, need_debug)
4395 Node_Id gnat_expr;
4396 Entity_Id gnat_entity;
4397 tree gnu_name;
4398 int definition;
4399 int need_value;
4400 int need_debug;
4401{
4402 tree gnu_expr;
4403
4404 /* If we already elaborated this expression (e.g., it was involved
4405 in the definition of a private type), use the old value. */
4406 if (present_gnu_tree (gnat_expr))
4407 return get_gnu_tree (gnat_expr);
4408
4409 /* If we don't need a value and this is static or a discriment, we
4410 don't need to do anything. */
4411 else if (! need_value
4412 && (Is_OK_Static_Expression (gnat_expr)
4413 || (Nkind (gnat_expr) == N_Identifier
4414 && Ekind (Entity (gnat_expr)) == E_Discriminant)))
4415 return 0;
4416
4417 /* Otherwise, convert this tree to its GCC equivalant. */
4418 gnu_expr
4419 = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
4420 gnu_name, definition, need_debug);
4421
4422 /* Save the expression in case we try to elaborate this entity again.
4423 Since this is not a DECL, don't check it. If this is a constant,
4424 don't save it since GNAT_EXPR might be used more than once. Also,
4425 don't save if it's a discriminant. */
fbf5a39b 4426 if (! CONTAINS_PLACEHOLDER_P (gnu_expr))
70482933
RK
4427 save_gnu_tree (gnat_expr, gnu_expr, 1);
4428
4429 return need_value ? gnu_expr : error_mark_node;
4430}
4431
4432/* Similar, but take a GNU expression. */
4433
4434static tree
4435elaborate_expression_1 (gnat_expr, gnat_entity, gnu_expr, gnu_name, definition,
4436 need_debug)
4437 Node_Id gnat_expr;
4438 Entity_Id gnat_entity;
4439 tree gnu_expr;
4440 tree gnu_name;
4441 int definition;
4442 int need_debug;
4443{
4444 tree gnu_decl = 0;
70482933
RK
4445 /* Strip any conversions to see if the expression is a readonly variable.
4446 ??? This really should remain readonly, but we have to think about
4447 the typing of the tree here. */
07fc65c4
GB
4448 tree gnu_inner_expr = remove_conversions (gnu_expr, 1);
4449 int expr_global = Is_Public (gnat_entity) || global_bindings_p ();
4450 int expr_variable;
70482933
RK
4451
4452 /* In most cases, we won't see a naked FIELD_DECL here because a
4453 discriminant reference will have been replaced with a COMPONENT_REF
4454 when the type is being elaborated. However, there are some cases
4455 involving child types where we will. So convert it to a COMPONENT_REF
4456 here. We have to hope it will be at the highest level of the
4457 expression in these cases. */
4458 if (TREE_CODE (gnu_expr) == FIELD_DECL)
4459 gnu_expr = build (COMPONENT_REF, TREE_TYPE (gnu_expr),
4460 build (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
4461 gnu_expr);
4462
70482933
RK
4463 /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
4464 that is a constant, make a variable that is initialized to contain the
4465 bound when the package containing the definition is elaborated. If
4466 this entity is defined at top level and a bound or discriminant value
4467 isn't a constant or a reference to a discriminant, replace the bound
4468 by the variable; otherwise use a SAVE_EXPR if needed. Note that we
4469 rely here on the fact that an expression cannot contain both the
4470 discriminant and some other variable. */
4471
4472 expr_variable = (TREE_CODE_CLASS (TREE_CODE (gnu_expr)) != 'c'
4473 && ! (TREE_CODE (gnu_inner_expr) == VAR_DECL
4474 && TREE_READONLY (gnu_inner_expr))
fbf5a39b 4475 && ! CONTAINS_PLACEHOLDER_P (gnu_expr));
70482933
RK
4476
4477 /* If this is a static expression or contains a discriminant, we don't
4478 need the variable for debugging (and can't elaborate anyway if a
4479 discriminant). */
4480 if (need_debug
4481 && (Is_OK_Static_Expression (gnat_expr)
fbf5a39b 4482 || CONTAINS_PLACEHOLDER_P (gnu_expr)))
70482933
RK
4483 need_debug = 0;
4484
4485 /* Now create the variable if we need it. */
4486 if (need_debug || (expr_variable && expr_global))
4487 {
4488 set_lineno (gnat_entity, ! global_bindings_p ());
4489 gnu_decl
4490 = create_var_decl (create_concat_name (gnat_entity,
4491 IDENTIFIER_POINTER (gnu_name)),
4492 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, 1,
4493 Is_Public (gnat_entity), ! definition, 0, 0);
4494 }
4495
4496 /* We only need to use this variable if we are in global context since GCC
4497 can do the right thing in the local case. */
4498 if (expr_global && expr_variable)
4499 return gnu_decl;
07fc65c4
GB
4500 else if (! expr_variable)
4501 return gnu_expr;
70482933
RK
4502 else
4503 return maybe_variable (gnu_expr, gnat_expr);
4504}
4505\f
4506/* Create a record type that contains a field of TYPE with a starting bit
4507 position so that it is aligned to ALIGN bits and is SIZE bytes long. */
4508
4509tree
4510make_aligning_type (type, align, size)
4511 tree type;
4512 int align;
4513 tree size;
4514{
4515 tree record_type = make_node (RECORD_TYPE);
4516 tree place = build (PLACEHOLDER_EXPR, record_type);
4517 tree size_addr_place = convert (sizetype,
4518 build_unary_op (ADDR_EXPR, NULL_TREE,
4519 place));
4520 tree name = TYPE_NAME (type);
4521 tree pos, field;
4522
4523 if (TREE_CODE (name) == TYPE_DECL)
4524 name = DECL_NAME (name);
4525
4526 TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
4527
4528 /* The bit position is obtained by "and"ing the alignment minus 1
4529 with the two's complement of the address and multiplying
4530 by the number of bits per unit. Do all this in sizetype. */
4531
4532 pos = size_binop (MULT_EXPR,
4533 convert (bitsizetype,
fbf5a39b 4534 size_binop (BIT_AND_EXPR,
70482933
RK
4535 size_diffop (size_zero_node,
4536 size_addr_place),
4537 ssize_int ((align / BITS_PER_UNIT)
4538 - 1))),
4539 bitsize_unit_node);
4540
4541 field = create_field_decl (get_identifier ("F"), type, record_type,
4542 1, size, pos, 1);
4543 DECL_BIT_FIELD (field) = 0;
4544
4545 finish_record_type (record_type, field, 1, 0);
4546 TYPE_ALIGN (record_type) = BIGGEST_ALIGNMENT;
4547 TYPE_SIZE (record_type)
4548 = size_binop (PLUS_EXPR,
4549 size_binop (MULT_EXPR, convert (bitsizetype, size),
4550 bitsize_unit_node),
4551 bitsize_int (align));
4552 TYPE_SIZE_UNIT (record_type)
4553 = size_binop (PLUS_EXPR, size, size_int (align / BITS_PER_UNIT));
fbf5a39b 4554 TYPE_ALIAS_SET (record_type) = get_alias_set (type);
70482933
RK
4555 return record_type;
4556}
4557\f
fbf5a39b
AC
4558/* TYPE is a RECORD_TYPE, UNION_TYPE, or QUAL_UNION_TYPE, with BLKmode that's
4559 being used as the field type of a packed record. See if we can rewrite it
4560 as a record that has a non-BLKmode type, which we can pack tighter. If so,
4561 return the new type. If not, return the original type. */
70482933
RK
4562
4563static tree
4564make_packable_type (type)
4565 tree type;
4566{
fbf5a39b 4567 tree new_type = make_node (TREE_CODE (type));
70482933
RK
4568 tree field_list = NULL_TREE;
4569 tree old_field;
4570
4571 /* Copy the name and flags from the old type to that of the new and set
fbf5a39b
AC
4572 the alignment to try for an integral type. For QUAL_UNION_TYPE,
4573 also copy the size. */
70482933
RK
4574 TYPE_NAME (new_type) = TYPE_NAME (type);
4575 TYPE_LEFT_JUSTIFIED_MODULAR_P (new_type)
4576 = TYPE_LEFT_JUSTIFIED_MODULAR_P (type);
4577 TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
fbf5a39b
AC
4578 TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
4579 if (TREE_CODE (type) == QUAL_UNION_TYPE)
4580 {
4581 TYPE_SIZE (new_type) = TYPE_SIZE (type);
4582 TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
4583 }
70482933
RK
4584
4585 TYPE_ALIGN (new_type)
4586 = ((HOST_WIDE_INT) 1
4587 << (floor_log2 (tree_low_cst (TYPE_SIZE (type), 1) - 1) + 1));
4588
4589 /* Now copy the fields, keeping the position and size. */
4590 for (old_field = TYPE_FIELDS (type); old_field != 0;
4591 old_field = TREE_CHAIN (old_field))
4592 {
fbf5a39b
AC
4593 tree new_field_type = TREE_TYPE (old_field);
4594 tree new_field;
4595
4596 if (TYPE_MODE (new_field_type) == BLKmode
4597 && (TREE_CODE (new_field_type) == RECORD_TYPE
4598 || TREE_CODE (new_field_type) == UNION_TYPE
4599 || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
4600 && host_integerp (TYPE_SIZE (new_field_type), 1))
4601 new_field_type = make_packable_type (new_field_type);
4602
4603 new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
4604 new_type, TYPE_PACKED (type),
4605 DECL_SIZE (old_field),
4606 bit_position (old_field),
4607 ! DECL_NONADDRESSABLE_P (old_field));
70482933
RK
4608
4609 DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
e2500fed
GK
4610 SET_DECL_ORIGINAL_FIELD (new_field,
4611 (DECL_ORIGINAL_FIELD (old_field) != 0
4612 ? DECL_ORIGINAL_FIELD (old_field) : old_field));
fbf5a39b
AC
4613
4614 if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
4615 DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
4616
70482933
RK
4617 TREE_CHAIN (new_field) = field_list;
4618 field_list = new_field;
4619 }
4620
4621 finish_record_type (new_type, nreverse (field_list), 1, 1);
fbf5a39b 4622 TYPE_ALIAS_SET (new_type) = get_alias_set (type);
70482933
RK
4623 return TYPE_MODE (new_type) == BLKmode ? type : new_type;
4624}
4625\f
4626/* Ensure that TYPE has SIZE and ALIGN. Make and return a new padded type
4627 if needed. We have already verified that SIZE and TYPE are large enough.
4628
4629 GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
4630 to issue a warning.
4631
4632 IS_USER_TYPE is nonzero if we must be sure we complete the original type.
4633
4634 DEFINITION is nonzero if this type is being defined.
4635
4636 SAME_RM_SIZE is nonzero if the RM_Size of the resulting type is to be
4637 set to its TYPE_SIZE; otherwise, it's set to the RM_Size of the original
4638 type. */
4639
4640static tree
4641maybe_pad_type (type, size, align, gnat_entity, name_trailer,
4642 is_user_type, definition, same_rm_size)
4643 tree type;
4644 tree size;
4645 unsigned int align;
4646 Entity_Id gnat_entity;
4647 const char *name_trailer;
4648 int is_user_type;
4649 int definition;
4650 int same_rm_size;
4651{
4652 tree orig_size = TYPE_SIZE (type);
4653 tree record;
4654 tree field;
4655
4656 /* If TYPE is a padded type, see if it agrees with any size and alignment
4657 we were given. If so, return the original type. Otherwise, strip
4658 off the padding, since we will either be returning the inner type
4659 or repadding it. If no size or alignment is specified, use that of
4660 the original padded type. */
4661
4662 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
4663 {
4664 if ((size == 0
4665 || operand_equal_p (round_up (size,
4666 MAX (align, TYPE_ALIGN (type))),
4667 round_up (TYPE_SIZE (type),
4668 MAX (align, TYPE_ALIGN (type))),
4669 0))
4670 && (align == 0 || align == TYPE_ALIGN (type)))
4671 return type;
4672
4673 if (size == 0)
4674 size = TYPE_SIZE (type);
4675 if (align == 0)
4676 align = TYPE_ALIGN (type);
4677
4678 type = TREE_TYPE (TYPE_FIELDS (type));
4679 orig_size = TYPE_SIZE (type);
4680 }
4681
4682 /* If the size is either not being changed or is being made smaller (which
4683 is not done here (and is only valid for bitfields anyway), show the size
4684 isn't changing. Likewise, clear the alignment if it isn't being
4685 changed. Then return if we aren't doing anything. */
4686
4687 if (size != 0
4688 && (operand_equal_p (size, orig_size, 0)
4689 || (TREE_CODE (orig_size) == INTEGER_CST
4690 && tree_int_cst_lt (size, orig_size))))
4691 size = 0;
4692
4693 if (align == TYPE_ALIGN (type))
4694 align = 0;
4695
4696 if (align == 0 && size == 0)
4697 return type;
4698
4699 /* We used to modify the record in place in some cases, but that could
4700 generate incorrect debugging information. So make a new record
4701 type and name. */
4702 record = make_node (RECORD_TYPE);
4703
4704 if (Present (gnat_entity))
4705 TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
4706
4707 /* If we were making a type, complete the original type and give it a
4708 name. */
4709 if (is_user_type)
4710 create_type_decl (get_entity_name (gnat_entity), type,
4711 0, ! Comes_From_Source (gnat_entity),
4712 ! (TYPE_NAME (type) != 0
4713 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
4714 && DECL_IGNORED_P (TYPE_NAME (type))));
4715
4716 /* If we are changing the alignment and the input type is a record with
4717 BLKmode and a small constant size, try to make a form that has an
4718 integral mode. That might allow this record to have an integral mode,
4719 which will be much more efficient. There is no point in doing this if a
4720 size is specified unless it is also smaller than the biggest alignment
4721 and it is incorrect to do this if the size of the original type is not a
4722 multiple of the alignment. */
4723 if (align != 0
4724 && TREE_CODE (type) == RECORD_TYPE
4725 && TYPE_MODE (type) == BLKmode
4726 && host_integerp (orig_size, 1)
4727 && compare_tree_int (orig_size, BIGGEST_ALIGNMENT) <= 0
4728 && (size == 0
4729 || (TREE_CODE (size) == INTEGER_CST
4730 && compare_tree_int (size, BIGGEST_ALIGNMENT) <= 0))
4731 && tree_low_cst (orig_size, 1) % align == 0)
4732 type = make_packable_type (type);
4733
4734 field = create_field_decl (get_identifier ("F"), type, record, 0,
4735 NULL_TREE, bitsize_zero_node, 1);
4736
4737 DECL_INTERNAL_P (field) = 1;
4738 TYPE_SIZE (record) = size != 0 ? size : orig_size;
4739 TYPE_SIZE_UNIT (record)
4740 = convert (sizetype,
4741 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
4742 bitsize_unit_node));
4743 TYPE_ALIGN (record) = align;
4744 TYPE_IS_PADDING_P (record) = 1;
4745 TYPE_VOLATILE (record)
fbf5a39b 4746 = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
70482933
RK
4747 finish_record_type (record, field, 1, 0);
4748
4749 /* Keep the RM_Size of the padded record as that of the old record
4750 if requested. */
e2500fed 4751 SET_TYPE_ADA_SIZE (record, same_rm_size ? size : rm_size (type));
70482933
RK
4752
4753 /* Unless debugging information isn't being written for the input type,
4754 write a record that shows what we are a subtype of and also make a
4755 variable that indicates our size, if variable. */
4756 if (TYPE_NAME (record) != 0
4757 && AGGREGATE_TYPE_P (type)
4758 && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
4759 || ! DECL_IGNORED_P (TYPE_NAME (type))))
4760 {
4761 tree marker = make_node (RECORD_TYPE);
4762 tree name = DECL_NAME (TYPE_NAME (record));
4763 tree orig_name = TYPE_NAME (type);
4764
4765 if (TREE_CODE (orig_name) == TYPE_DECL)
4766 orig_name = DECL_NAME (orig_name);
4767
4768 TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
4769 finish_record_type (marker,
4770 create_field_decl (orig_name, integer_type_node,
4771 marker, 0, NULL_TREE, NULL_TREE,
4772 0),
4773 0, 0);
4774
4775 if (size != 0 && TREE_CODE (size) != INTEGER_CST && definition)
4776 create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
4777 sizetype, TYPE_SIZE (record), 0, 0, 0, 0,
4778 0);
4779 }
4780
4781 type = record;
4782
fbf5a39b 4783 if (CONTAINS_PLACEHOLDER_P (orig_size))
70482933
RK
4784 orig_size = max_size (orig_size, 1);
4785
4786 /* If the size was widened explicitly, maybe give a warning. */
4787 if (size != 0 && Present (gnat_entity)
4788 && ! operand_equal_p (size, orig_size, 0)
4789 && ! (TREE_CODE (size) == INTEGER_CST
4790 && TREE_CODE (orig_size) == INTEGER_CST
4791 && tree_int_cst_lt (size, orig_size)))
4792 {
4793 Node_Id gnat_error_node = Empty;
4794
4795 if (Is_Packed_Array_Type (gnat_entity))
4796 gnat_entity = Associated_Node_For_Itype (gnat_entity);
4797
4798 if ((Ekind (gnat_entity) == E_Component
4799 || Ekind (gnat_entity) == E_Discriminant)
4800 && Present (Component_Clause (gnat_entity)))
4801 gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
4802 else if (Present (Size_Clause (gnat_entity)))
4803 gnat_error_node = Expression (Size_Clause (gnat_entity));
4804
4805 /* Generate message only for entities that come from source, since
4806 if we have an entity created by expansion, the message will be
4807 generated for some other corresponding source entity. */
4808 if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
4809 post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
4810 gnat_entity,
4811 size_diffop (size, orig_size));
4812
4813 else if (*name_trailer == 'C' && ! Is_Internal (gnat_entity))
4814 post_error_ne_tree ("component of& padded{ by ^ bits}?",
4815 gnat_entity, gnat_entity,
4816 size_diffop (size, orig_size));
4817 }
4818
4819 return type;
4820}
4821\f
4822/* Given a GNU tree and a GNAT list of choices, generate an expression to test
4823 the value passed against the list of choices. */
4824
4825tree
4826choices_to_gnu (operand, choices)
4827 tree operand;
4828 Node_Id choices;
4829{
4830 Node_Id choice;
4831 Node_Id gnat_temp;
4832 tree result = integer_zero_node;
4833 tree this_test, low = 0, high = 0, single = 0;
4834
4835 for (choice = First (choices); Present (choice); choice = Next (choice))
4836 {
4837 switch (Nkind (choice))
4838 {
4839 case N_Range:
4840 low = gnat_to_gnu (Low_Bound (choice));
4841 high = gnat_to_gnu (High_Bound (choice));
4842
4843 /* There's no good type to use here, so we might as well use
4844 integer_type_node. */
4845 this_test
4846 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4847 build_binary_op (GE_EXPR, integer_type_node,
4848 operand, low),
4849 build_binary_op (LE_EXPR, integer_type_node,
4850 operand, high));
4851
4852 break;
4853
4854 case N_Subtype_Indication:
4855 gnat_temp = Range_Expression (Constraint (choice));
4856 low = gnat_to_gnu (Low_Bound (gnat_temp));
4857 high = gnat_to_gnu (High_Bound (gnat_temp));
4858
4859 this_test
4860 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4861 build_binary_op (GE_EXPR, integer_type_node,
4862 operand, low),
4863 build_binary_op (LE_EXPR, integer_type_node,
4864 operand, high));
4865 break;
4866
4867 case N_Identifier:
4868 case N_Expanded_Name:
4869 /* This represents either a subtype range, an enumeration
4870 literal, or a constant Ekind says which. If an enumeration
4871 literal or constant, fall through to the next case. */
4872 if (Ekind (Entity (choice)) != E_Enumeration_Literal
4873 && Ekind (Entity (choice)) != E_Constant)
4874 {
4875 tree type = gnat_to_gnu_type (Entity (choice));
4876
4877 low = TYPE_MIN_VALUE (type);
4878 high = TYPE_MAX_VALUE (type);
4879
4880 this_test
4881 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
4882 build_binary_op (GE_EXPR, integer_type_node,
4883 operand, low),
4884 build_binary_op (LE_EXPR, integer_type_node,
4885 operand, high));
4886 break;
4887 }
4888 /* ... fall through ... */
4889 case N_Character_Literal:
4890 case N_Integer_Literal:
4891 single = gnat_to_gnu (choice);
4892 this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
4893 single);
4894 break;
4895
4896 case N_Others_Choice:
4897 this_test = integer_one_node;
4898 break;
4899
4900 default:
4901 gigi_abort (114);
4902 }
4903
4904 result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
4905 result, this_test);
4906 }
4907
4908 return result;
4909}
4910\f
4911/* Return a GCC tree for a field corresponding to GNAT_FIELD to be
4912 placed in GNU_RECORD_TYPE.
4913
4914 PACKED is 1 if the enclosing record is packed and -1 if the enclosing
4915 record has a Component_Alignment of Storage_Unit.
4916
4917 DEFINITION is nonzero if this field is for a record being defined. */
4918
4919static tree
4920gnat_to_gnu_field (gnat_field, gnu_record_type, packed, definition)
4921 Entity_Id gnat_field;
4922 tree gnu_record_type;
4923 int packed;
4924 int definition;
4925{
4926 tree gnu_field_id = get_entity_name (gnat_field);
4927 tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
4928 tree gnu_orig_field_type = gnu_field_type;
4929 tree gnu_pos = 0;
4930 tree gnu_size = 0;
4931 tree gnu_field;
4932 int needs_strict_alignment
4933 = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
fbf5a39b 4934 || Treat_As_Volatile (gnat_field));
70482933 4935
fbf5a39b
AC
4936 /* If this field requires strict alignment or contains an item of
4937 variable sized, pretend it isn't packed. */
4938 if (needs_strict_alignment || is_variable_size (gnu_field_type))
70482933
RK
4939 packed = 0;
4940
4941 /* For packed records, this is one of the few occasions on which we use
4942 the official RM size for discrete or fixed-point components, instead
4943 of the normal GNAT size stored in Esize. See description in Einfo:
4944 "Handling of Type'Size Values" for further details. */
4945
4946 if (packed == 1)
4947 gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
4948 gnat_field, FIELD_DECL, 0, 1);
4949
4950 if (Known_Static_Esize (gnat_field))
4951 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
4952 gnat_field, FIELD_DECL, 0, 1);
4953
07fc65c4
GB
4954 /* If the field's type is a left-justified modular type, make the field
4955 the type of the inner object unless it is aliases. We don't need
4956 the the wrapper here and it can prevent packing. */
4957 if (! Is_Aliased (gnat_field) && TREE_CODE (gnu_field_type) == RECORD_TYPE
4958 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_field_type))
4959 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
4960
4961 /* If we are packing this record or we have a specified size that's
4962 smaller than that of the field type and the field type is also a record
70482933
RK
4963 that's BLKmode and with a small constant size, see if we can get a
4964 better form of the type that allows more packing. If we can, show
4965 a size was specified for it if there wasn't one so we know to
4966 make this a bitfield and avoid making things wider. */
07fc65c4 4967 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
70482933
RK
4968 && TYPE_MODE (gnu_field_type) == BLKmode
4969 && host_integerp (TYPE_SIZE (gnu_field_type), 1)
07fc65c4
GB
4970 && compare_tree_int (TYPE_SIZE (gnu_field_type), BIGGEST_ALIGNMENT) <= 0
4971 && (packed
4972 || (gnu_size != 0 && tree_int_cst_lt (gnu_size,
4973 TYPE_SIZE (gnu_field_type)))))
70482933
RK
4974 {
4975 gnu_field_type = make_packable_type (gnu_field_type);
4976
4977 if (gnu_field_type != gnu_orig_field_type && gnu_size == 0)
4978 gnu_size = rm_size (gnu_field_type);
4979 }
4980
fbf5a39b
AC
4981 /* If we are packing the record and the field is BLKmode, round the
4982 size up to a byte boundary. */
4983 if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size != 0)
4984 gnu_size = round_up (gnu_size, BITS_PER_UNIT);
4985
70482933
RK
4986 if (Present (Component_Clause (gnat_field)))
4987 {
4988 gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
4989 gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
4990 gnat_field, FIELD_DECL, 0, 1);
4991
4992 /* Ensure the position does not overlap with the parent subtype,
4993 if there is one. */
4994 if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
4995 {
4996 tree gnu_parent
4997 = gnat_to_gnu_type (Parent_Subtype
4998 (Underlying_Type (Scope (gnat_field))));
4999
5000 if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
5001 && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
5002 {
5003 post_error_ne_tree
5004 ("offset of& must be beyond parent{, minimum allowed is ^}",
5005 First_Bit (Component_Clause (gnat_field)), gnat_field,
5006 TYPE_SIZE_UNIT (gnu_parent));
5007 }
5008 }
5009
5010 /* If this field needs strict alignment, ensure the record is
5011 sufficiently aligned and that that position and size are
5012 consistent with the alignment. */
5013 if (needs_strict_alignment)
5014 {
5015 tree gnu_min_size = round_up (rm_size (gnu_field_type),
5016 TYPE_ALIGN (gnu_field_type));
5017
5018 TYPE_ALIGN (gnu_record_type)
5019 = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
5020
5021 /* If Atomic, the size must match exactly and if aliased, the size
5022 must not be less than the rounded size. */
5023 if ((Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
5024 && ! operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
5025 {
5026 post_error_ne_tree
5027 ("atomic field& must be natural size of type{ (^)}",
5028 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5029 TYPE_SIZE (gnu_field_type));
5030
5031 gnu_size = 0;
5032 }
5033
5034 else if (Is_Aliased (gnat_field)
5035 && gnu_size != 0
5036 && tree_int_cst_lt (gnu_size, gnu_min_size))
5037 {
5038 post_error_ne_tree
5039 ("size of aliased field& too small{, minimum required is ^}",
5040 Last_Bit (Component_Clause (gnat_field)), gnat_field,
5041 gnu_min_size);
5042 gnu_size = 0;
5043 }
5044
fbf5a39b 5045 if (! integer_zerop (size_binop
70482933
RK
5046 (TRUNC_MOD_EXPR, gnu_pos,
5047 bitsize_int (TYPE_ALIGN (gnu_field_type)))))
5048 {
5049 if (Is_Aliased (gnat_field))
5050 post_error_ne_num
5051 ("position of aliased field& must be multiple of ^ bits",
07fc65c4 5052 First_Bit (Component_Clause (gnat_field)), gnat_field,
70482933
RK
5053 TYPE_ALIGN (gnu_field_type));
5054
fbf5a39b 5055 else if (Treat_As_Volatile (gnat_field))
70482933
RK
5056 post_error_ne_num
5057 ("position of volatile field& must be multiple of ^ bits",
5058 First_Bit (Component_Clause (gnat_field)), gnat_field,
5059 TYPE_ALIGN (gnu_field_type));
5060
5061 else if (Strict_Alignment (Etype (gnat_field)))
5062 post_error_ne_num
5063 ("position of & with aliased or tagged components not multiple of ^ bits",
5064 First_Bit (Component_Clause (gnat_field)), gnat_field,
5065 TYPE_ALIGN (gnu_field_type));
5066 else
5067 gigi_abort (124);
5068
5069 gnu_pos = 0;
5070 }
5071
5072 /* If an error set the size to zero, show we have no position
5073 either. */
5074 if (gnu_size == 0)
5075 gnu_pos = 0;
5076 }
5077
5078 if (Is_Atomic (gnat_field))
5079 check_ok_for_atomic (gnu_field_type, gnat_field, 0);
5080
fbf5a39b 5081 if (gnu_pos != 0 && TYPE_MODE (gnu_field_type) == BLKmode
70482933 5082 && (! integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_pos,
fbf5a39b
AC
5083 bitsize_unit_node)))
5084 && TYPE_MODE (gnu_field_type) == BLKmode)
70482933 5085 {
fbf5a39b
AC
5086 post_error_ne ("fields of& must start at storage unit boundary",
5087 First_Bit (Component_Clause (gnat_field)),
5088 Etype (gnat_field));
5089 gnu_pos = 0;
70482933
RK
5090 }
5091 }
5092
5093 /* If the record has rep clauses and this is the tag field, make a rep
5094 clause for it as well. */
5095 else if (Has_Specified_Layout (Scope (gnat_field))
5096 && Chars (gnat_field) == Name_uTag)
5097 {
5098 gnu_pos = bitsize_zero_node;
5099 gnu_size = TYPE_SIZE (gnu_field_type);
5100 }
5101
fbf5a39b
AC
5102 /* If a size is specified and this is a BLKmode field, it must be an
5103 integral number of bytes. */
5104 if (gnu_size != 0 && TYPE_MODE (gnu_field_type) == BLKmode
5105 && ! integer_zerop (size_binop (TRUNC_MOD_EXPR, gnu_size,
5106 bitsize_unit_node)))
5107 {
5108 post_error_ne ("size of fields of& must be multiple of a storage unit",
5109 gnat_field, Etype (gnat_field));
5110 gnu_pos = gnu_size = 0;
5111 }
5112
70482933 5113 /* We need to make the size the maximum for the type if it is
07fc65c4
GB
5114 self-referential and an unconstrained type. In that case, we can't
5115 pack the field since we can't make a copy to align it. */
70482933
RK
5116 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5117 && gnu_size == 0
fbf5a39b 5118 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
70482933 5119 && ! Is_Constrained (Underlying_Type (Etype (gnat_field))))
07fc65c4
GB
5120 {
5121 gnu_size = max_size (TYPE_SIZE (gnu_field_type), 1);
5122 packed = 0;
5123 }
70482933
RK
5124
5125 /* If no size is specified (or if there was an error), don't specify a
5126 position. */
5127 if (gnu_size == 0)
5128 gnu_pos = 0;
5129 else
5130 {
5131 /* Unless this field is aliased, we can remove any left-justified
5132 modular type since it's only needed in the unchecked conversion
5133 case, which doesn't apply here. */
5134 if (! needs_strict_alignment
5135 && TREE_CODE (gnu_field_type) == RECORD_TYPE
5136 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_field_type))
5137 gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
5138
5139 gnu_field_type
5140 = make_type_from_size (gnu_field_type, gnu_size,
5141 Has_Biased_Representation (gnat_field));
5142 gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0,
5143 gnat_field, "PAD", 0, definition, 1);
5144 }
5145
5146 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
5147 && TYPE_CONTAINS_TEMPLATE_P (gnu_field_type))
5148 gigi_abort (118);
5149
fbf5a39b 5150 /* Now create the decl for the field. */
70482933
RK
5151 set_lineno (gnat_field, 0);
5152 gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
5153 packed, gnu_size, gnu_pos,
5154 Is_Aliased (gnat_field));
5155
fbf5a39b 5156 TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
70482933
RK
5157
5158 if (Ekind (gnat_field) == E_Discriminant)
5159 DECL_DISCRIMINANT_NUMBER (gnu_field)
5160 = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
5161
5162 return gnu_field;
5163}
5164\f
fbf5a39b
AC
5165/* Return 1 if TYPE is a type with variable size, a padding type with a field
5166 of variable size or is a record that has a field such a field. */
5167
5168static int
5169is_variable_size (type)
5170 tree type;
5171{
5172 tree field;
5173
5174 /* We need not be concerned about this at all if we don't have
5175 strict alignment. */
5176 if (! STRICT_ALIGNMENT)
5177 return 0;
5178 else if (! TREE_CONSTANT (TYPE_SIZE (type)))
5179 return 1;
5180 else if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type)
5181 && ! TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
5182 return 1;
5183 else if (TREE_CODE (type) != RECORD_TYPE
5184 && TREE_CODE (type) != UNION_TYPE
5185 && TREE_CODE (type) != QUAL_UNION_TYPE)
5186 return 0;
5187
5188 for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
5189 if (is_variable_size (TREE_TYPE (field)))
5190 return 1;
5191
5192 return 0;
5193}
5194\f
70482933
RK
5195/* Return a GCC tree for a record type given a GNAT Component_List and a chain
5196 of GCC trees for fields that are in the record and have already been
5197 processed. When called from gnat_to_gnu_entity during the processing of a
5198 record type definition, the GCC nodes for the discriminants will be on
5199 the chain. The other calls to this function are recursive calls from
5200 itself for the Component_List of a variant and the chain is empty.
5201
5202 PACKED is 1 if this is for a record with "pragma pack" and -1 is this is
5203 for a record type with "pragma component_alignment (storage_unit)".
5204
5205 FINISH_RECORD is nonzero if this call will supply all of the remaining
5206 fields of the record.
5207
5208 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
5209 with a rep clause is to be added. If it is nonzero, that is all that
5210 should be done with such fields.
5211
5212 CANCEL_ALIGNMENT, if nonzero, means the alignment should be zeroed
5213 before laying out the record. This means the alignment only serves
5214 to force fields to be bitfields, but not require the record to be
5215 that aligned. This is used for variants.
5216
5217 ALL_REP, if nonzero, means that a rep clause was found for all the
5218 fields. This simplifies the logic since we know we're not in the mixed
5219 case.
5220
5221 The processing of the component list fills in the chain with all of the
5222 fields of the record and then the record type is finished. */
5223
5224static void
5225components_to_record (gnu_record_type, component_list, gnu_field_list, packed,
5226 definition, p_gnu_rep_list, cancel_alignment, all_rep)
5227 tree gnu_record_type;
5228 Node_Id component_list;
5229 tree gnu_field_list;
5230 int packed;
5231 int definition;
5232 tree *p_gnu_rep_list;
5233 int cancel_alignment;
5234 int all_rep;
5235{
5236 Node_Id component_decl;
5237 Entity_Id gnat_field;
5238 Node_Id variant_part;
5239 Node_Id variant;
5240 tree gnu_our_rep_list = NULL_TREE;
5241 tree gnu_field, gnu_last;
5242 int layout_with_rep = 0;
fbf5a39b 5243 int all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type) != 0;
70482933
RK
5244
5245 /* For each variable within each component declaration create a GCC field
5246 and add it to the list, skipping any pragmas in the list. */
5247
5248 if (Present (Component_Items (component_list)))
5249 for (component_decl = First_Non_Pragma (Component_Items (component_list));
5250 Present (component_decl);
5251 component_decl = Next_Non_Pragma (component_decl))
5252 {
5253 gnat_field = Defining_Entity (component_decl);
5254
5255 if (Chars (gnat_field) == Name_uParent)
5256 gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
5257 else
5258 {
5259 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
5260 packed, definition);
5261
5262 /* If this is the _Tag field, put it before any discriminants,
fbf5a39b
AC
5263 instead of after them as is the case for all other fields.
5264 Ignore field of void type if only annotating. */
70482933
RK
5265 if (Chars (gnat_field) == Name_uTag)
5266 gnu_field_list = chainon (gnu_field_list, gnu_field);
5267 else
5268 {
5269 TREE_CHAIN (gnu_field) = gnu_field_list;
5270 gnu_field_list = gnu_field;
5271 }
5272 }
5273
5274 save_gnu_tree (gnat_field, gnu_field, 0);
5275 }
5276
5277 /* At the end of the component list there may be a variant part. */
5278 variant_part = Variant_Part (component_list);
5279
5280 /* If this is an unchecked union, each variant must have exactly one
5281 component, each of which becomes one component of this union. */
5282 if (TREE_CODE (gnu_record_type) == UNION_TYPE && Present (variant_part))
5283 for (variant = First_Non_Pragma (Variants (variant_part));
5284 Present (variant);
5285 variant = Next_Non_Pragma (variant))
5286 {
5287 component_decl
5288 = First_Non_Pragma (Component_Items (Component_List (variant)));
5289 gnat_field = Defining_Entity (component_decl);
5290 gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
5291 definition);
5292 TREE_CHAIN (gnu_field) = gnu_field_list;
5293 gnu_field_list = gnu_field;
5294 save_gnu_tree (gnat_field, gnu_field, 0);
5295 }
5296
5297 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
5298 mutually exclusive and should go in the same memory. To do this we need
5299 to treat each variant as a record whose elements are created from the
5300 component list for the variant. So here we create the records from the
5301 lists for the variants and put them all into the QUAL_UNION_TYPE. */
5302 else if (Present (variant_part))
5303 {
5304 tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
5305 Node_Id variant;
5306 tree gnu_union_type = make_node (QUAL_UNION_TYPE);
5307 tree gnu_union_field;
5308 tree gnu_variant_list = NULL_TREE;
5309 tree gnu_name = TYPE_NAME (gnu_record_type);
5310 tree gnu_var_name
5311 = concat_id_with_name
5312 (get_identifier (Get_Name_String (Chars (Name (variant_part)))),
5313 "XVN");
5314
5315 if (TREE_CODE (gnu_name) == TYPE_DECL)
5316 gnu_name = DECL_NAME (gnu_name);
5317
5318 TYPE_NAME (gnu_union_type)
5319 = concat_id_with_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
5320 TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
5321
5322 for (variant = First_Non_Pragma (Variants (variant_part));
5323 Present (variant);
5324 variant = Next_Non_Pragma (variant))
5325 {
5326 tree gnu_variant_type = make_node (RECORD_TYPE);
5327 tree gnu_inner_name;
5328 tree gnu_qual;
5329
5330 Get_Variant_Encoding (variant);
5331 gnu_inner_name = get_identifier (Name_Buffer);
5332 TYPE_NAME (gnu_variant_type)
5333 = concat_id_with_name (TYPE_NAME (gnu_union_type),
5334 IDENTIFIER_POINTER (gnu_inner_name));
5335
5336 /* Set the alignment of the inner type in case we need to make
5337 inner objects into bitfields, but then clear it out
5338 so the record actually gets only the alignment required. */
5339 TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
5340 TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
fbf5a39b
AC
5341
5342 /* Similarly, if the outer record has a size specified and all fields
5343 have record rep clauses, we can propagate the size into the
5344 variant part. */
5345 if (all_rep_and_size)
5346 {
5347 TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
5348 TYPE_SIZE_UNIT (gnu_variant_type)
5349 = TYPE_SIZE_UNIT (gnu_record_type);
5350 }
5351
70482933
RK
5352 components_to_record (gnu_variant_type, Component_List (variant),
5353 NULL_TREE, packed, definition,
fbf5a39b 5354 &gnu_our_rep_list, !all_rep_and_size, all_rep);
70482933
RK
5355
5356 gnu_qual = choices_to_gnu (gnu_discriminant,
5357 Discrete_Choices (variant));
5358
5359 Set_Present_Expr (variant, annotate_value (gnu_qual));
5360 gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
fbf5a39b
AC
5361 gnu_union_type, 0,
5362 (all_rep_and_size
5363 ? TYPE_SIZE (gnu_record_type) : 0),
5364 (all_rep_and_size
5365 ? bitsize_zero_node : 0),
5366 1);
5367
70482933
RK
5368 DECL_INTERNAL_P (gnu_field) = 1;
5369 DECL_QUALIFIER (gnu_field) = gnu_qual;
5370 TREE_CHAIN (gnu_field) = gnu_variant_list;
5371 gnu_variant_list = gnu_field;
5372 }
5373
5374 /* We can delete any empty variants from the end. This may leave none
5375 left. Note we cannot delete variants from anywhere else. */
5376 while (gnu_variant_list != 0
5377 && TYPE_FIELDS (TREE_TYPE (gnu_variant_list)) == 0)
5378 gnu_variant_list = TREE_CHAIN (gnu_variant_list);
5379
5380 /* Only make the QUAL_UNION_TYPE if there are any non-empty variants. */
5381 if (gnu_variant_list != 0)
5382 {
fbf5a39b
AC
5383 if (all_rep_and_size)
5384 {
5385 TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
5386 TYPE_SIZE_UNIT (gnu_union_type)
5387 = TYPE_SIZE_UNIT (gnu_record_type);
5388 }
5389
70482933 5390 finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
fbf5a39b 5391 all_rep_and_size, 0);
70482933
RK
5392
5393 gnu_union_field
5394 = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
5395 packed,
5396 all_rep ? TYPE_SIZE (gnu_union_type) : 0,
5397 all_rep ? bitsize_zero_node : 0, 1);
5398
5399 DECL_INTERNAL_P (gnu_union_field) = 1;
5400 TREE_CHAIN (gnu_union_field) = gnu_field_list;
5401 gnu_field_list = gnu_union_field;
5402 }
5403 }
5404
5405 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses. If they
5406 do, pull them out and put them into GNU_OUR_REP_LIST. We have to do this
5407 in a separate pass since we want to handle the discriminants but can't
5408 play with them until we've used them in debugging data above.
5409
5410 ??? Note: if we then reorder them, debugging information will be wrong,
5411 but there's nothing that can be done about this at the moment. */
5412
5413 for (gnu_field = gnu_field_list, gnu_last = 0; gnu_field; )
5414 {
5415 if (DECL_FIELD_OFFSET (gnu_field) != 0)
5416 {
5417 tree gnu_next = TREE_CHAIN (gnu_field);
5418
5419 if (gnu_last == 0)
5420 gnu_field_list = gnu_next;
5421 else
5422 TREE_CHAIN (gnu_last) = gnu_next;
5423
5424 TREE_CHAIN (gnu_field) = gnu_our_rep_list;
5425 gnu_our_rep_list = gnu_field;
5426 gnu_field = gnu_next;
5427 }
5428 else
5429 {
5430 gnu_last = gnu_field;
5431 gnu_field = TREE_CHAIN (gnu_field);
5432 }
5433 }
5434
5435 /* If we have any items in our rep'ed field list, it is not the case that all
5436 the fields in the record have rep clauses, and P_REP_LIST is nonzero,
5437 set it and ignore the items. Otherwise, sort the fields by bit position
5438 and put them into their own record if we have any fields without
5439 rep clauses. */
5440 if (gnu_our_rep_list != 0 && p_gnu_rep_list != 0 && ! all_rep)
5441 *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
5442 else if (gnu_our_rep_list != 0)
5443 {
5444 tree gnu_rep_type
5445 = gnu_field_list == 0 ? gnu_record_type : make_node (RECORD_TYPE);
5446 int len = list_length (gnu_our_rep_list);
5447 tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
5448 int i;
5449
5450 /* Set DECL_SECTION_NAME to increasing integers so we have a
5451 stable sort. */
5452 for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
5453 gnu_field = TREE_CHAIN (gnu_field), i++)
5454 {
5455 gnu_arr[i] = gnu_field;
5456 DECL_SECTION_NAME (gnu_field) = size_int (i);
5457 }
5458
5459 qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
5460
5461 /* Put the fields in the list in order of increasing position, which
5462 means we start from the end. */
5463 gnu_our_rep_list = NULL_TREE;
5464 for (i = len - 1; i >= 0; i--)
5465 {
5466 TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
5467 gnu_our_rep_list = gnu_arr[i];
5468 DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
5469 DECL_SECTION_NAME (gnu_arr[i]) = 0;
5470 }
5471
5472 if (gnu_field_list != 0)
5473 {
5474 finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, 0);
5475 gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
5476 gnu_record_type, 0, 0, 0, 1);
5477 DECL_INTERNAL_P (gnu_field) = 1;
5478 gnu_field_list = chainon (gnu_field_list, gnu_field);
5479 }
5480 else
5481 {
5482 layout_with_rep = 1;
5483 gnu_field_list = nreverse (gnu_our_rep_list);
5484 }
5485 }
5486
5487 if (cancel_alignment)
5488 TYPE_ALIGN (gnu_record_type) = 0;
5489
5490 finish_record_type (gnu_record_type, nreverse (gnu_field_list),
5491 layout_with_rep, 0);
5492}
5493\f
5494/* Called via qsort from the above. Returns -1, 1, depending on the
5495 bit positions and ordinals of the two fields. */
5496
5497static int
5498compare_field_bitpos (rt1, rt2)
5499 const PTR rt1;
5500 const PTR rt2;
5501{
5502 tree *t1 = (tree *) rt1;
5503 tree *t2 = (tree *) rt2;
5504
5505 if (tree_int_cst_equal (bit_position (*t1), bit_position (*t2)))
5506 return
5507 (tree_int_cst_lt (DECL_SECTION_NAME (*t1), DECL_SECTION_NAME (*t2))
5508 ? -1 : 1);
5509 else if (tree_int_cst_lt (bit_position (*t1), bit_position (*t2)))
5510 return -1;
5511 else
5512 return 1;
5513}
5514\f
5515/* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
5516 placed into an Esize, Component_Bit_Offset, or Component_Size value
5517 in the GNAT tree. */
5518
5519static Uint
5520annotate_value (gnu_size)
5521 tree gnu_size;
5522{
5523 int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
5524 TCode tcode;
fbf5a39b 5525 Node_Ref_Or_Val ops[3], ret;
70482933 5526 int i;
45659035 5527 int size;
70482933 5528
fbf5a39b
AC
5529 /* If back annotation is suppressed by the front end, return No_Uint */
5530 if (!Back_Annotate_Rep_Info)
5531 return No_Uint;
5532
5533 /* See if we've already saved the value for this node. */
5534 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (gnu_size)))
5535 && TREE_COMPLEXITY (gnu_size) != 0)
5536 return (Node_Ref_Or_Val) TREE_COMPLEXITY (gnu_size);
5537
70482933
RK
5538 /* If we do not return inside this switch, TCODE will be set to the
5539 code to use for a Create_Node operand and LEN (set above) will be
5540 the number of recursive calls for us to make. */
5541
5542 switch (TREE_CODE (gnu_size))
5543 {
5544 case INTEGER_CST:
5545 if (TREE_OVERFLOW (gnu_size))
5546 return No_Uint;
5547
5548 /* This may have come from a conversion from some smaller type,
5549 so ensure this is in bitsizetype. */
5550 gnu_size = convert (bitsizetype, gnu_size);
5551
5552 /* For negative values, use NEGATE_EXPR of the supplied value. */
5553 if (tree_int_cst_sgn (gnu_size) < 0)
5554 {
5555 /* The rediculous code below is to handle the case of the largest
5556 negative integer. */
5557 tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
5558 int adjust = 0;
5559 tree temp;
5560
5561 if (TREE_CONSTANT_OVERFLOW (negative_size))
5562 {
5563 negative_size
5564 = size_binop (MINUS_EXPR, bitsize_zero_node,
5565 size_binop (PLUS_EXPR, gnu_size,
5566 bitsize_one_node));
5567 adjust = 1;
5568 }
5569
5570 temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
5571 if (adjust)
5572 temp = build (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
5573
5574 return annotate_value (temp);
5575 }
5576
5577 if (! host_integerp (gnu_size, 1))
5578 return No_Uint;
5579
5580 size = tree_low_cst (gnu_size, 1);
5581
5582 /* This peculiar test is to make sure that the size fits in an int
5583 on machines where HOST_WIDE_INT is not "int". */
5584 if (tree_low_cst (gnu_size, 1) == size)
5585 return UI_From_Int (size);
5586 else
5587 return No_Uint;
5588
5589 case COMPONENT_REF:
5590 /* The only case we handle here is a simple discriminant reference. */
5591 if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
5592 && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
5593 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)) != 0)
5594 return Create_Node (Discrim_Val,
5595 annotate_value (DECL_DISCRIMINANT_NUMBER
5596 (TREE_OPERAND (gnu_size, 1))),
5597 No_Uint, No_Uint);
5598 else
5599 return No_Uint;
5600
5601 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
5602 return annotate_value (TREE_OPERAND (gnu_size, 0));
5603
5604 /* Now just list the operations we handle. */
5605 case COND_EXPR: tcode = Cond_Expr; break;
5606 case PLUS_EXPR: tcode = Plus_Expr; break;
5607 case MINUS_EXPR: tcode = Minus_Expr; break;
5608 case MULT_EXPR: tcode = Mult_Expr; break;
5609 case TRUNC_DIV_EXPR: tcode = Trunc_Div_Expr; break;
5610 case CEIL_DIV_EXPR: tcode = Ceil_Div_Expr; break;
5611 case FLOOR_DIV_EXPR: tcode = Floor_Div_Expr; break;
5612 case TRUNC_MOD_EXPR: tcode = Trunc_Mod_Expr; break;
5613 case CEIL_MOD_EXPR: tcode = Ceil_Mod_Expr; break;
5614 case FLOOR_MOD_EXPR: tcode = Floor_Mod_Expr; break;
5615 case EXACT_DIV_EXPR: tcode = Exact_Div_Expr; break;
5616 case NEGATE_EXPR: tcode = Negate_Expr; break;
5617 case MIN_EXPR: tcode = Min_Expr; break;
5618 case MAX_EXPR: tcode = Max_Expr; break;
5619 case ABS_EXPR: tcode = Abs_Expr; break;
5620 case TRUTH_ANDIF_EXPR: tcode = Truth_Andif_Expr; break;
5621 case TRUTH_ORIF_EXPR: tcode = Truth_Orif_Expr; break;
5622 case TRUTH_AND_EXPR: tcode = Truth_And_Expr; break;
5623 case TRUTH_OR_EXPR: tcode = Truth_Or_Expr; break;
5624 case TRUTH_XOR_EXPR: tcode = Truth_Xor_Expr; break;
5625 case TRUTH_NOT_EXPR: tcode = Truth_Not_Expr; break;
5626 case LT_EXPR: tcode = Lt_Expr; break;
5627 case LE_EXPR: tcode = Le_Expr; break;
5628 case GT_EXPR: tcode = Gt_Expr; break;
5629 case GE_EXPR: tcode = Ge_Expr; break;
5630 case EQ_EXPR: tcode = Eq_Expr; break;
5631 case NE_EXPR: tcode = Ne_Expr; break;
5632
5633 default:
5634 return No_Uint;
5635 }
5636
5637 /* Now get each of the operands that's relevant for this code. If any
5638 cannot be expressed as a repinfo node, say we can't. */
5639 for (i = 0; i < 3; i++)
5640 ops[i] = No_Uint;
5641
5642 for (i = 0; i < len; i++)
5643 {
5644 ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
5645 if (ops[i] == No_Uint)
5646 return No_Uint;
5647 }
5648
fbf5a39b
AC
5649 ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
5650 TREE_COMPLEXITY (gnu_size) = ret;
5651 return ret;
70482933
RK
5652}
5653
5654/* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
5655 GCC type, set Component_Bit_Offset and Esize to the position and size
5656 used by Gigi. */
5657
5658static void
5659annotate_rep (gnat_entity, gnu_type)
5660 Entity_Id gnat_entity;
5661 tree gnu_type;
5662{
5663 tree gnu_list;
5664 tree gnu_entry;
5665 Entity_Id gnat_field;
5666
5667 /* We operate by first making a list of all field and their positions
5668 (we can get the sizes easily at any time) by a recursive call
5669 and then update all the sizes into the tree. */
5670 gnu_list = compute_field_positions (gnu_type, NULL_TREE,
07fc65c4
GB
5671 size_zero_node, bitsize_zero_node,
5672 BIGGEST_ALIGNMENT);
70482933
RK
5673
5674 for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
5675 gnat_field = Next_Entity (gnat_field))
5676 if ((Ekind (gnat_field) == E_Component
5677 || (Ekind (gnat_field) == E_Discriminant
fbf5a39b 5678 && ! Is_Unchecked_Union (Scope (gnat_field)))))
70482933 5679 {
fbf5a39b
AC
5680 tree parent_offset = bitsize_zero_node;
5681
5682 gnu_entry
5683 = purpose_member (gnat_to_gnu_entity (gnat_field, NULL_TREE, 0),
5684 gnu_list);
5685
5686 if (gnu_entry)
5687 {
5688 if (type_annotate_only && Is_Tagged_Type (gnat_entity))
5689 {
5690 /* In this mode the tag and parent components have not been
5691 generated, so we add the appropriate offset to each
5692 component. For a component appearing in the current
5693 extension, the offset is the size of the parent. */
5694 if (Is_Derived_Type (gnat_entity)
5695 && Original_Record_Component (gnat_field) == gnat_field)
5696 parent_offset
5697 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
5698 bitsizetype);
5699 else
5700 parent_offset = bitsize_int (POINTER_SIZE);
5701 }
5702
5703 Set_Component_Bit_Offset
5704 (gnat_field,
5705 annotate_value
5706 (size_binop (PLUS_EXPR,
5707 bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
5708 TREE_VALUE (TREE_VALUE
5709 (TREE_VALUE (gnu_entry)))),
5710 parent_offset)));
5711
5712 Set_Esize (gnat_field,
5713 annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
5714 }
5715 else if (type_annotate_only
5716 && Is_Tagged_Type (gnat_entity)
5717 && Is_Derived_Type (gnat_entity))
5718 {
5719 /* If there is no gnu_entry, this is an inherited component whose
5720 position is the same as in the parent type. */
5721 Set_Component_Bit_Offset
5722 (gnat_field,
5723 Component_Bit_Offset (Original_Record_Component (gnat_field)));
5724 Set_Esize (gnat_field,
5725 Esize (Original_Record_Component (gnat_field)));
5726 }
70482933
RK
5727 }
5728}
5729
07fc65c4
GB
5730/* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
5731 FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
5732 position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
5733 placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position. GNU_POS is
5734 to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
5735 the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
5736 so far. */
70482933
RK
5737
5738static tree
07fc65c4 5739compute_field_positions (gnu_type, gnu_list, gnu_pos, gnu_bitpos, offset_align)
70482933
RK
5740 tree gnu_type;
5741 tree gnu_list;
5742 tree gnu_pos;
5743 tree gnu_bitpos;
07fc65c4 5744 unsigned int offset_align;
70482933
RK
5745{
5746 tree gnu_field;
5747 tree gnu_result = gnu_list;
5748
5749 for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
5750 gnu_field = TREE_CHAIN (gnu_field))
5751 {
5752 tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
5753 DECL_FIELD_BIT_OFFSET (gnu_field));
07fc65c4
GB
5754 tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
5755 DECL_FIELD_OFFSET (gnu_field));
5756 unsigned int our_offset_align
5757 = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
70482933
RK
5758
5759 gnu_result
5760 = tree_cons (gnu_field,
07fc65c4
GB
5761 tree_cons (gnu_our_offset,
5762 tree_cons (size_int (our_offset_align),
5763 gnu_our_bitpos, NULL_TREE),
5764 NULL_TREE),
70482933
RK
5765 gnu_result);
5766
5767 if (DECL_INTERNAL_P (gnu_field))
5768 gnu_result
07fc65c4
GB
5769 = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
5770 gnu_our_offset, gnu_our_bitpos,
5771 our_offset_align);
70482933
RK
5772 }
5773
5774 return gnu_result;
5775}
5776\f
5777/* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
5778 corresponding to GNAT_OBJECT. If size is valid, return a tree corresponding
5779 to its value. Otherwise return 0. KIND is VAR_DECL is we are specifying
5780 the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
5781 for the size of a field. COMPONENT_P is true if we are being called
5782 to process the Component_Size of GNAT_OBJECT. This is used for error
5783 message handling and to indicate to use the object size of GNU_TYPE.
5784 ZERO_OK is nonzero if a size of zero is permitted; if ZERO_OK is zero,
5785 it means that a size of zero should be treated as an unspecified size. */
5786
5787static tree
5788validate_size (uint_size, gnu_type, gnat_object, kind, component_p, zero_ok)
5789 Uint uint_size;
5790 tree gnu_type;
5791 Entity_Id gnat_object;
5792 enum tree_code kind;
5793 int component_p;
5794 int zero_ok;
5795{
5796 Node_Id gnat_error_node;
5797 tree type_size
5798 = kind == VAR_DECL ? TYPE_SIZE (gnu_type) : rm_size (gnu_type);
5799 tree size;
5800
fbf5a39b 5801 /* Find the node to use for errors. */
70482933
RK
5802 if ((Ekind (gnat_object) == E_Component
5803 || Ekind (gnat_object) == E_Discriminant)
5804 && Present (Component_Clause (gnat_object)))
5805 gnat_error_node = Last_Bit (Component_Clause (gnat_object));
5806 else if (Present (Size_Clause (gnat_object)))
5807 gnat_error_node = Expression (Size_Clause (gnat_object));
5808 else
5809 gnat_error_node = gnat_object;
5810
5811 /* Don't give errors on packed array types; we'll be giving the error on
5812 the type itself soon enough. */
5813 if (Is_Packed_Array_Type (gnat_object))
5814 gnat_error_node = Empty;
5815
fbf5a39b
AC
5816 /* Return 0 if no size was specified, either because Esize was not Present or
5817 the specified size was zero. */
70482933
RK
5818 if (No (uint_size) || uint_size == No_Uint)
5819 return 0;
5820
fbf5a39b
AC
5821 /* Get the size as a tree. Give an error if a size was specified, but cannot
5822 be represented as in sizetype. */
70482933
RK
5823 size = UI_To_gnu (uint_size, bitsizetype);
5824 if (TREE_OVERFLOW (size))
5825 {
fbf5a39b
AC
5826 post_error_ne (component_p ? "component size of & is too large"
5827 : "size of & is too large",
5828 gnat_error_node, gnat_object);
70482933
RK
5829 return 0;
5830 }
70482933
RK
5831 /* Ignore a negative size since that corresponds to our back-annotation.
5832 Also ignore a zero size unless a size clause exists. */
5833 else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && ! zero_ok))
fbf5a39b 5834 return 0;
70482933
RK
5835
5836 /* The size of objects is always a multiple of a byte. */
5837 if (kind == VAR_DECL
5838 && ! integer_zerop (size_binop (TRUNC_MOD_EXPR, size,
5839 bitsize_unit_node)))
5840 {
5841 if (component_p)
5842 post_error_ne ("component size for& is not a multiple of Storage_Unit",
5843 gnat_error_node, gnat_object);
5844 else
5845 post_error_ne ("size for& is not a multiple of Storage_Unit",
5846 gnat_error_node, gnat_object);
5847 return 0;
5848 }
5849
5850 /* If this is an integral type, the front-end has verified the size, so we
5851 need not do it here (which would entail checking against the bounds).
5852 However, if this is an aliased object, it may not be smaller than the
5853 type of the object. */
a8fee948 5854 if (INTEGRAL_TYPE_P (gnu_type) && ! TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
70482933
RK
5855 && ! (kind == VAR_DECL && Is_Aliased (gnat_object)))
5856 return size;
5857
5858 /* If the object is a record that contains a template, add the size of
5859 the template to the specified size. */
5860 if (TREE_CODE (gnu_type) == RECORD_TYPE
5861 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
5862 size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
5863
fbf5a39b
AC
5864 /* Modify the size of the type to be that of the maximum size if it has a
5865 discriminant or the size of a thin pointer if this is a fat pointer. */
5866 if (type_size != 0 && CONTAINS_PLACEHOLDER_P (type_size))
5867 type_size = max_size (type_size, 1);
5868 else if (TYPE_FAT_POINTER_P (gnu_type))
5869 type_size = bitsize_int (POINTER_SIZE);
5870
70482933
RK
5871 /* If the size of the object is a constant, the new size must not be
5872 smaller. */
5873 if (TREE_CODE (type_size) != INTEGER_CST
5874 || TREE_OVERFLOW (type_size)
5875 || tree_int_cst_lt (size, type_size))
5876 {
5877 if (component_p)
5878 post_error_ne_tree
5879 ("component size for& too small{, minimum allowed is ^}",
5880 gnat_error_node, gnat_object, type_size);
5881 else
5882 post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
5883 gnat_error_node, gnat_object, type_size);
5884
5885 if (kind == VAR_DECL && ! component_p
5886 && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
5887 && ! tree_int_cst_lt (size, rm_size (gnu_type)))
5888 post_error_ne_tree_2
5b09c153 5889 ("\\size of ^ is not a multiple of alignment (^ bits)",
70482933
RK
5890 gnat_error_node, gnat_object, rm_size (gnu_type),
5891 TYPE_ALIGN (gnu_type));
5892
5893 else if (INTEGRAL_TYPE_P (gnu_type))
5894 post_error_ne ("\\size would be legal if & were not aliased!",
5895 gnat_error_node, gnat_object);
5896
5897 return 0;
5898 }
5899
5900 return size;
5901}
5902\f
5903/* Similarly, but both validate and process a value of RM_Size. This
5904 routine is only called for types. */
5905
5906static void
5907set_rm_size (uint_size, gnu_type, gnat_entity)
5908 Uint uint_size;
5909 tree gnu_type;
5910 Entity_Id gnat_entity;
5911{
5912 /* Only give an error if a Value_Size clause was explicitly given.
5913 Otherwise, we'd be duplicating an error on the Size clause. */
5914 Node_Id gnat_attr_node
5915 = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
5916 tree old_size = rm_size (gnu_type);
5917 tree size;
5918
5919 /* Get the size as a tree. Do nothing if none was specified, either
5920 because RM_Size was not Present or if the specified size was zero.
5921 Give an error if a size was specified, but cannot be represented as
5922 in sizetype. */
5923 if (No (uint_size) || uint_size == No_Uint)
5924 return;
5925
5926 size = UI_To_gnu (uint_size, bitsizetype);
5927 if (TREE_OVERFLOW (size))
5928 {
5929 if (Present (gnat_attr_node))
5930 post_error_ne ("Value_Size of & is too large", gnat_attr_node,
5931 gnat_entity);
5932
5933 return;
5934 }
5935
5936 /* Ignore a negative size since that corresponds to our back-annotation.
5937 Also ignore a zero size unless a size clause exists, a Value_Size
5938 clause exists, or this is an integer type, in which case the
5939 front end will have always set it. */
5940 else if (tree_int_cst_sgn (size) < 0
5941 || (integer_zerop (size) && No (gnat_attr_node)
5942 && ! Has_Size_Clause (gnat_entity)
5943 && ! Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
5944 return;
5945
5946 /* If the old size is self-referential, get the maximum size. */
fbf5a39b 5947 if (CONTAINS_PLACEHOLDER_P (old_size))
70482933
RK
5948 old_size = max_size (old_size, 1);
5949
5950 /* If the size of the object is a constant, the new size must not be
5951 smaller (the front end checks this for scalar types). */
5952 if (TREE_CODE (old_size) != INTEGER_CST
5953 || TREE_OVERFLOW (old_size)
5954 || (AGGREGATE_TYPE_P (gnu_type)
5955 && tree_int_cst_lt (size, old_size)))
5956 {
5957 if (Present (gnat_attr_node))
5958 post_error_ne_tree
5959 ("Value_Size for& too small{, minimum allowed is ^}",
5960 gnat_attr_node, gnat_entity, old_size);
5961
5962 return;
5963 }
5964
5965 /* Otherwise, set the RM_Size. */
5966 if (TREE_CODE (gnu_type) == INTEGER_TYPE
5967 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
5968 TYPE_RM_SIZE_INT (gnu_type) = size;
5969 else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE)
e2500fed 5970 SET_TYPE_RM_SIZE_ENUM (gnu_type, size);
70482933
RK
5971 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
5972 || TREE_CODE (gnu_type) == UNION_TYPE
5973 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
5974 && ! TYPE_IS_FAT_POINTER_P (gnu_type))
e2500fed 5975 SET_TYPE_ADA_SIZE (gnu_type, size);
70482933
RK
5976}
5977\f
5978/* Given a type TYPE, return a new type whose size is appropriate for SIZE.
5979 If TYPE is the best type, return it. Otherwise, make a new type. We
5980 only support new integral and pointer types. BIASED_P is nonzero if
5981 we are making a biased type. */
5982
5983static tree
5984make_type_from_size (type, size_tree, biased_p)
5985 tree type;
5986 tree size_tree;
5987 int biased_p;
5988{
5989 tree new_type;
5990 unsigned HOST_WIDE_INT size;
5991
5992 /* If size indicates an error, just return TYPE to avoid propagating the
5993 error. Likewise if it's too large to represent. */
5994 if (size_tree == 0 || ! host_integerp (size_tree, 1))
5995 return type;
5996
5997 size = tree_low_cst (size_tree, 1);
5998 switch (TREE_CODE (type))
5999 {
6000 case INTEGER_TYPE:
6001 case ENUMERAL_TYPE:
6002 /* Only do something if the type is not already the proper size and is
6003 not a packed array type. */
6004 if (TYPE_PACKED_ARRAY_TYPE_P (type)
6005 || (TYPE_PRECISION (type) == size
6006 && biased_p == (TREE_CODE (type) == INTEGER_CST
6007 && TYPE_BIASED_REPRESENTATION_P (type))))
6008 break;
6009
6010 size = MIN (size, LONG_LONG_TYPE_SIZE);
6011 new_type = make_signed_type (size);
6012 TREE_TYPE (new_type)
6013 = TREE_TYPE (type) != 0 ? TREE_TYPE (type) : type;
6014 TYPE_MIN_VALUE (new_type)
6015 = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
6016 TYPE_MAX_VALUE (new_type)
6017 = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
6018 TYPE_BIASED_REPRESENTATION_P (new_type)
6019 = ((TREE_CODE (type) == INTEGER_TYPE
6020 && TYPE_BIASED_REPRESENTATION_P (type))
6021 || biased_p);
6022 TREE_UNSIGNED (new_type)
6023 = TREE_UNSIGNED (type) | TYPE_BIASED_REPRESENTATION_P (new_type);
6024 TYPE_RM_SIZE_INT (new_type) = bitsize_int (size);
6025 return new_type;
6026
6027 case RECORD_TYPE:
6028 /* Do something if this is a fat pointer, in which case we
6029 may need to return the thin pointer. */
6030 if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
6031 return
6032 build_pointer_type
6033 (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)));
6034 break;
6035
6036 case POINTER_TYPE:
6037 /* Only do something if this is a thin pointer, in which case we
6038 may need to return the fat pointer. */
6039 if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
6040 return
6041 build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
6042
6043 break;
6044
6045 default:
6046 break;
6047 }
6048
6049 return type;
6050}
6051\f
6052/* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
6053 a type or object whose present alignment is ALIGN. If this alignment is
6054 valid, return it. Otherwise, give an error and return ALIGN. */
6055
6056static unsigned int
6057validate_alignment (alignment, gnat_entity, align)
6058 Uint alignment;
6059 Entity_Id gnat_entity;
6060 unsigned int align;
6061{
6062 Node_Id gnat_error_node = gnat_entity;
6063 unsigned int new_align;
6064
6065#ifndef MAX_OFILE_ALIGNMENT
6066#define MAX_OFILE_ALIGNMENT BIGGEST_ALIGNMENT
6067#endif
6068
6069 if (Present (Alignment_Clause (gnat_entity)))
6070 gnat_error_node = Expression (Alignment_Clause (gnat_entity));
6071
07fc65c4
GB
6072 /* Don't worry about checking alignment if alignment was not specified
6073 by the source program and we already posted an error for this entity. */
6074
6075 if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
6076 return align;
6077
70482933
RK
6078 /* Within GCC, an alignment is an integer, so we must make sure a
6079 value is specified that fits in that range. Also, alignments of
6080 more than MAX_OFILE_ALIGNMENT can't be supported. */
6081
6082 if (! UI_Is_In_Int_Range (alignment)
6083 || ((new_align = UI_To_Int (alignment))
6084 > MAX_OFILE_ALIGNMENT / BITS_PER_UNIT))
6085 post_error_ne_num ("largest supported alignment for& is ^",
6086 gnat_error_node, gnat_entity,
6087 MAX_OFILE_ALIGNMENT / BITS_PER_UNIT);
6088 else if (! (Present (Alignment_Clause (gnat_entity))
6089 && From_At_Mod (Alignment_Clause (gnat_entity)))
6090 && new_align * BITS_PER_UNIT < align)
6091 post_error_ne_num ("alignment for& must be at least ^",
6092 gnat_error_node, gnat_entity,
6093 align / BITS_PER_UNIT);
6094 else
6095 align = MAX (align, new_align == 0 ? 1 : new_align * BITS_PER_UNIT);
6096
6097 return align;
6098}
6099\f
6100/* Verify that OBJECT, a type or decl, is something we can implement
6101 atomically. If not, give an error for GNAT_ENTITY. COMP_P is nonzero
6102 if we require atomic components. */
6103
6104static void
6105check_ok_for_atomic (object, gnat_entity, comp_p)
6106 tree object;
6107 Entity_Id gnat_entity;
6108 int comp_p;
6109{
6110 Node_Id gnat_error_point = gnat_entity;
6111 Node_Id gnat_node;
6112 enum machine_mode mode;
6113 unsigned int align;
6114 tree size;
6115
6116 /* There are three case of what OBJECT can be. It can be a type, in which
6117 case we take the size, alignment and mode from the type. It can be a
6118 declaration that was indirect, in which case the relevant values are
6119 that of the type being pointed to, or it can be a normal declaration,
6120 in which case the values are of the decl. The code below assumes that
6121 OBJECT is either a type or a decl. */
6122 if (TYPE_P (object))
6123 {
6124 mode = TYPE_MODE (object);
6125 align = TYPE_ALIGN (object);
6126 size = TYPE_SIZE (object);
6127 }
6128 else if (DECL_BY_REF_P (object))
6129 {
6130 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
6131 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
6132 size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
6133 }
6134 else
6135 {
6136 mode = DECL_MODE (object);
6137 align = DECL_ALIGN (object);
6138 size = DECL_SIZE (object);
6139 }
6140
6141 /* Consider all floating-point types atomic and any types that that are
6142 represented by integers no wider than a machine word. */
6143 if (GET_MODE_CLASS (mode) == MODE_FLOAT
6144 || ((GET_MODE_CLASS (mode) == MODE_INT
6145 || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
6146 && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
6147 return;
6148
6149 /* For the moment, also allow anything that has an alignment equal
6150 to its size and which is smaller than a word. */
fbf5a39b 6151 if (size != 0 && TREE_CODE (size) == INTEGER_CST
70482933
RK
6152 && compare_tree_int (size, align) == 0
6153 && align <= BITS_PER_WORD)
6154 return;
6155
6156 for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
6157 gnat_node = Next_Rep_Item (gnat_node))
6158 {
6159 if (! comp_p && Nkind (gnat_node) == N_Pragma
6160 && Get_Pragma_Id (Chars (gnat_node)) == Pragma_Atomic)
6161 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6162 else if (comp_p && Nkind (gnat_node) == N_Pragma
6163 && (Get_Pragma_Id (Chars (gnat_node))
6164 == Pragma_Atomic_Components))
6165 gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
6166 }
6167
6168 if (comp_p)
6169 post_error_ne ("atomic access to component of & cannot be guaranteed",
6170 gnat_error_point, gnat_entity);
6171 else
6172 post_error_ne ("atomic access to & cannot be guaranteed",
6173 gnat_error_point, gnat_entity);
6174}
6175\f
6176/* Given a type T, a FIELD_DECL F, and a replacement value R,
6177 return a new type with all size expressions that contain F
6178 updated by replacing F with R. This is identical to GCC's
6179 substitute_in_type except that it knows about TYPE_INDEX_TYPE.
6180 If F is NULL_TREE, always make a new RECORD_TYPE, even if nothing has
6181 changed. */
6182
6183tree
6184gnat_substitute_in_type (t, f, r)
6185 tree t, f, r;
6186{
6187 tree new = t;
6188 tree tem;
6189
6190 switch (TREE_CODE (t))
6191 {
6192 case INTEGER_TYPE:
6193 case ENUMERAL_TYPE:
6194 case BOOLEAN_TYPE:
6195 case CHAR_TYPE:
fbf5a39b
AC
6196 if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
6197 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
70482933
RK
6198 {
6199 tree low = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
6200 tree high = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
6201
6202 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6203 return t;
6204
6205 new = build_range_type (TREE_TYPE (t), low, high);
6206 if (TYPE_INDEX_TYPE (t))
fbf5a39b 6207 SET_TYPE_INDEX_TYPE (new,
e2500fed 6208 gnat_substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
70482933
RK
6209 return new;
6210 }
6211
6212 return t;
6213
6214 case REAL_TYPE:
6215 if ((TYPE_MIN_VALUE (t) != 0
fbf5a39b 6216 && CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t)))
70482933 6217 || (TYPE_MAX_VALUE (t) != 0
fbf5a39b 6218 && CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t))))
70482933
RK
6219 {
6220 tree low = 0, high = 0;
6221
6222 if (TYPE_MIN_VALUE (t))
6223 low = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
6224 if (TYPE_MAX_VALUE (t))
6225 high = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
6226
6227 if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
6228 return t;
6229
6230 t = copy_type (t);
6231 TYPE_MIN_VALUE (t) = low;
6232 TYPE_MAX_VALUE (t) = high;
6233 }
6234 return t;
6235
6236 case COMPLEX_TYPE:
6237 tem = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6238 if (tem == TREE_TYPE (t))
6239 return t;
6240
6241 return build_complex_type (tem);
6242
6243 case OFFSET_TYPE:
6244 case METHOD_TYPE:
6245 case FILE_TYPE:
6246 case SET_TYPE:
6247 case FUNCTION_TYPE:
6248 case LANG_TYPE:
6249 /* Don't know how to do these yet. */
6250 abort ();
6251
6252 case ARRAY_TYPE:
6253 {
6254 tree component = gnat_substitute_in_type (TREE_TYPE (t), f, r);
6255 tree domain = gnat_substitute_in_type (TYPE_DOMAIN (t), f, r);
6256
6257 if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6258 return t;
6259
6260 new = build_array_type (component, domain);
6261 TYPE_SIZE (new) = 0;
6262 TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
6263 TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
6264 layout_type (new);
6265 TYPE_ALIGN (new) = TYPE_ALIGN (t);
6266 return new;
6267 }
6268
6269 case RECORD_TYPE:
6270 case UNION_TYPE:
6271 case QUAL_UNION_TYPE:
6272 {
6273 tree field;
6274 int changed_field
6275 = (f == NULL_TREE && ! TREE_CONSTANT (TYPE_SIZE (t)));
6276 int field_has_rep = 0;
6277 tree last_field = 0;
6278
6279 tree new = copy_type (t);
6280
6281 /* Start out with no fields, make new fields, and chain them
6282 in. If we haven't actually changed the type of any field,
6283 discard everything we've done and return the old type. */
6284
6285 TYPE_FIELDS (new) = 0;
6286 TYPE_SIZE (new) = 0;
6287
6288 for (field = TYPE_FIELDS (t); field;
6289 field = TREE_CHAIN (field))
6290 {
6291 tree new_field = copy_node (field);
6292
6293 TREE_TYPE (new_field)
6294 = gnat_substitute_in_type (TREE_TYPE (new_field), f, r);
6295
6296 if (DECL_HAS_REP_P (field) && ! DECL_INTERNAL_P (field))
6297 field_has_rep = 1;
6298 else if (TREE_TYPE (new_field) != TREE_TYPE (field))
6299 changed_field = 1;
6300
6301 /* If this is an internal field and the type of this field is
6302 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
6303 the type just has one element, treat that as the field.
6304 But don't do this if we are processing a QUAL_UNION_TYPE. */
6305 if (TREE_CODE (t) != QUAL_UNION_TYPE
6306 && DECL_INTERNAL_P (new_field)
6307 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
6308 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
6309 {
6310 if (TYPE_FIELDS (TREE_TYPE (new_field)) == 0)
6311 continue;
6312
6313 if (TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))) == 0)
6314 {
6315 tree next_new_field
6316 = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
6317
6318 /* Make sure omitting the union doesn't change
6319 the layout. */
6320 DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
6321 new_field = next_new_field;
6322 }
6323 }
6324
6325 DECL_CONTEXT (new_field) = new;
e2500fed
GK
6326 SET_DECL_ORIGINAL_FIELD (new_field,
6327 (DECL_ORIGINAL_FIELD (field) != 0
6328 ? DECL_ORIGINAL_FIELD (field) : field));
70482933
RK
6329
6330 /* If the size of the old field was set at a constant,
6331 propagate the size in case the type's size was variable.
6332 (This occurs in the case of a variant or discriminated
6333 record with a default size used as a field of another
6334 record.) */
6335 DECL_SIZE (new_field)
6336 = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
6337 ? DECL_SIZE (field) : 0;
6338 DECL_SIZE_UNIT (new_field)
6339 = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
6340 ? DECL_SIZE_UNIT (field) : 0;
6341
6342 if (TREE_CODE (t) == QUAL_UNION_TYPE)
6343 {
6344 tree new_q = substitute_in_expr (DECL_QUALIFIER (field), f, r);
6345
6346 if (new_q != DECL_QUALIFIER (new_field))
6347 changed_field = 1;
6348
6349 /* Do the substitution inside the qualifier and if we find
6350 that this field will not be present, omit it. */
6351 DECL_QUALIFIER (new_field) = new_q;
6352
6353 if (integer_zerop (DECL_QUALIFIER (new_field)))
6354 continue;
6355 }
6356
6357 if (last_field == 0)
6358 TYPE_FIELDS (new) = new_field;
6359 else
6360 TREE_CHAIN (last_field) = new_field;
6361
6362 last_field = new_field;
6363
6364 /* If this is a qualified type and this field will always be
6365 present, we are done. */
6366 if (TREE_CODE (t) == QUAL_UNION_TYPE
6367 && integer_onep (DECL_QUALIFIER (new_field)))
6368 break;
6369 }
6370
6371 /* If this used to be a qualified union type, but we now know what
6372 field will be present, make this a normal union. */
6373 if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
6374 && (TYPE_FIELDS (new) == 0
6375 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
6376 TREE_SET_CODE (new, UNION_TYPE);
6377 else if (! changed_field)
6378 return t;
6379
6380 if (field_has_rep)
6381 gigi_abort (117);
6382
6383 layout_type (new);
6384
6385 /* If the size was originally a constant use it. */
6386 if (TYPE_SIZE (t) != 0 && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
6387 && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
6388 {
6389 TYPE_SIZE (new) = TYPE_SIZE (t);
6390 TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
e2500fed 6391 SET_TYPE_ADA_SIZE (new, TYPE_ADA_SIZE (t));
70482933
RK
6392 }
6393
6394 return new;
6395 }
6396
6397 default:
6398 return t;
6399 }
6400}
6401\f
6402/* Return the "RM size" of GNU_TYPE. This is the actual number of bits
6403 needed to represent the object. */
6404
6405tree
6406rm_size (gnu_type)
6407 tree gnu_type;
6408{
6409 /* For integer types, this is the precision. For record types, we store
6410 the size explicitly. For other types, this is just the size. */
6411
6412 if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type) != 0)
6413 return TYPE_RM_SIZE (gnu_type);
6414 else if (TREE_CODE (gnu_type) == RECORD_TYPE
6415 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
6416 /* Return the rm_size of the actual data plus the size of the template. */
6417 return
6418 size_binop (PLUS_EXPR,
6419 rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
6420 DECL_SIZE (TYPE_FIELDS (gnu_type)));
6421 else if ((TREE_CODE (gnu_type) == RECORD_TYPE
6422 || TREE_CODE (gnu_type) == UNION_TYPE
6423 || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
6424 && ! TYPE_IS_FAT_POINTER_P (gnu_type)
6425 && TYPE_ADA_SIZE (gnu_type) != 0)
6426 return TYPE_ADA_SIZE (gnu_type);
6427 else
6428 return TYPE_SIZE (gnu_type);
6429}
6430\f
6431/* Return an identifier representing the external name to be used for
6432 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
6433 and the specified suffix. */
6434
6435tree
6436create_concat_name (gnat_entity, suffix)
6437 Entity_Id gnat_entity;
6438 const char *suffix;
6439{
6440 const char *str = (suffix == 0 ? "" : suffix);
6441 String_Template temp = {1, strlen (str)};
6442 Fat_Pointer fp = {str, &temp};
6443
6444 Get_External_Name_With_Suffix (gnat_entity, fp);
6445
fbf5a39b
AC
6446#ifdef _WIN32
6447 /* A variable using the Stdcall convention (meaning we are running
6448 on a Windows box) live in a DLL. Here we adjust its name to use
6449 the jump-table, the _imp__NAME contains the address for the NAME
6450 variable. */
6451
6452 {
6453 Entity_Kind kind = Ekind (gnat_entity);
6454 char *prefix = "_imp__";
6455 int plen = strlen (prefix);
6456
6457 if ((kind == E_Variable || kind == E_Constant)
6458 && Convention (gnat_entity) == Convention_Stdcall)
6459 {
6460 int k;
6461 for (k = 0; k <= Name_Len; k++)
6462 Name_Buffer [Name_Len - k + plen] = Name_Buffer [Name_Len - k];
6463 strncpy (Name_Buffer, prefix, plen);
6464 }
6465 }
6466#endif
6467
70482933
RK
6468 return get_identifier (Name_Buffer);
6469}
6470
6471/* Return the name to be used for GNAT_ENTITY. If a type, create a
6472 fully-qualified name, possibly with type information encoding.
6473 Otherwise, return the name. */
6474
6475tree
6476get_entity_name (gnat_entity)
6477 Entity_Id gnat_entity;
6478{
6479 Get_Encoded_Name (gnat_entity);
6480 return get_identifier (Name_Buffer);
6481}
6482
6483/* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
6484 string, return a new IDENTIFIER_NODE that is the concatenation of
6485 the name in GNU_ID and SUFFIX. */
6486
6487tree
6488concat_id_with_name (gnu_id, suffix)
6489 tree gnu_id;
6490 const char *suffix;
6491{
6492 int len = IDENTIFIER_LENGTH (gnu_id);
6493
6494 strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id),
6495 IDENTIFIER_LENGTH (gnu_id));
6496 strncpy (Name_Buffer + len, "___", 3);
6497 len += 3;
6498 strcpy (Name_Buffer + len, suffix);
6499 return get_identifier (Name_Buffer);
6500}
e2500fed
GK
6501
6502#include "gt-ada-decl.h"
This page took 1.09624 seconds and 5 git commands to generate.