1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2004, Free Software Foundation, Inc. *
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. *
22 * GNAT was originally developed by the GNAT team at New York University. *
23 * Extensive contributions were provided by Ada Core Technologies Inc. *
25 ****************************************************************************/
29 #include "coretypes.h"
55 #ifndef MAX_FIXED_MODE_SIZE
56 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
59 #ifndef MAX_BITS_PER_WORD
60 #define MAX_BITS_PER_WORD BITS_PER_WORD
63 /* If nonzero, pretend we are allocating at global level. */
66 /* Tree nodes for the various types and decls we create. */
67 tree gnat_std_decls
[(int) ADT_LAST
];
69 /* Functions to call for each of the possible raise reasons. */
70 tree gnat_raise_decls
[(int) LAST_REASON_CODE
+ 1];
72 /* Associates a GNAT tree node to a GCC tree node. It is used in
73 `save_gnu_tree', `get_gnu_tree' and `present_gnu_tree'. See documentation
74 of `save_gnu_tree' for more info. */
75 static GTY((length ("max_gnat_nodes"))) tree
*associate_gnat_to_gnu
;
77 /* This listhead is used to record any global objects that need elaboration.
78 TREE_PURPOSE is the variable to be elaborated and TREE_VALUE is the
79 initial value to assign. */
81 static GTY(()) tree pending_elaborations
;
83 /* This stack allows us to momentarily switch to generating elaboration
84 lists for an inner context. */
86 struct e_stack
GTY(()) {
90 static GTY(()) struct e_stack
*elist_stack
;
92 /* This variable keeps a table for types for each precision so that we only
93 allocate each of them once. Signed and unsigned types are kept separate.
95 Note that these types are only used when fold-const requests something
96 special. Perhaps we should NOT share these types; we'll see how it
98 static GTY(()) tree signed_and_unsigned_types
[2 * MAX_BITS_PER_WORD
+ 1][2];
100 /* Likewise for float types, but record these by mode. */
101 static GTY(()) tree float_types
[NUM_MACHINE_MODES
];
103 /* For each binding contour we allocate a binding_level structure which records
104 the entities defined or declared in that contour. Contours include:
107 one for each subprogram definition
108 one for each compound statement (declare block)
110 Binding contours are used to create GCC tree BLOCK nodes. */
112 struct binding_level
GTY(())
114 /* A chain of ..._DECL nodes for all variables, constants, functions,
115 parameters and type declarations. These ..._DECL nodes are chained
116 through the TREE_CHAIN field. Note that these ..._DECL nodes are stored
117 in the reverse of the order supplied to be compatible with the
120 /* For each level (except the global one), a chain of BLOCK nodes for all
121 the levels that were entered and exited one level down from this one. */
123 /* The BLOCK node for this level, if one has been preallocated.
124 If 0, the BLOCK is allocated (if needed) when the level is popped. */
126 /* The binding level containing this one (the enclosing binding level). */
127 struct binding_level
*level_chain
;
130 /* The binding level currently in effect. */
131 static GTY(()) struct binding_level
*current_binding_level
;
133 /* A chain of binding_level structures awaiting reuse. */
134 static GTY((deletable (""))) struct binding_level
*free_binding_level
;
136 /* The outermost binding level. This binding level is created when the
137 compiler is started and it will exist through the entire compilation. */
138 static struct binding_level
*global_binding_level
;
140 /* Binding level structures are initialized by copying this one. */
141 static struct binding_level clear_binding_level
= {NULL
, NULL
, NULL
, NULL
};
143 struct language_function
GTY(())
148 static tree
merge_sizes (tree
, tree
, tree
, int, int);
149 static tree
compute_related_constant (tree
, tree
);
150 static tree
split_plus (tree
, tree
*);
151 static int value_zerop (tree
);
152 static tree
float_type_for_precision (int, enum machine_mode
);
153 static tree
convert_to_fat_pointer (tree
, tree
);
154 static tree
convert_to_thin_pointer (tree
, tree
);
155 static tree
make_descriptor_field (const char *,tree
, tree
, tree
);
156 static int value_factor_p (tree
, int);
157 static int potential_alignment_gap (tree
, tree
, tree
);
159 /* Initialize the association of GNAT nodes to GCC trees. */
162 init_gnat_to_gnu (void)
164 associate_gnat_to_gnu
165 = (tree
*) ggc_alloc_cleared (max_gnat_nodes
* sizeof (tree
));
167 pending_elaborations
= build_tree_list (NULL_TREE
, NULL_TREE
);
170 /* GNAT_ENTITY is a GNAT tree node for an entity. GNU_DECL is the GCC tree
171 which is to be associated with GNAT_ENTITY. Such GCC tree node is always
172 a ..._DECL node. If NO_CHECK is nonzero, the latter check is suppressed.
174 If GNU_DECL is zero, a previous association is to be reset. */
177 save_gnu_tree (Entity_Id gnat_entity
, tree gnu_decl
, int no_check
)
179 /* Check that GNAT_ENTITY is not already defined and that it is being set
180 to something which is a decl. Raise gigi 401 if not. Usually, this
181 means GNAT_ENTITY is defined twice, but occasionally is due to some
184 && (associate_gnat_to_gnu
[gnat_entity
- First_Node_Id
]
185 || (! no_check
&& ! DECL_P (gnu_decl
))))
188 associate_gnat_to_gnu
[gnat_entity
- First_Node_Id
] = gnu_decl
;
191 /* GNAT_ENTITY is a GNAT tree node for a defining identifier.
192 Return the ..._DECL node that was associated with it. If there is no tree
193 node associated with GNAT_ENTITY, abort.
195 In some cases, such as delayed elaboration or expressions that need to
196 be elaborated only once, GNAT_ENTITY is really not an entity. */
199 get_gnu_tree (Entity_Id gnat_entity
)
201 if (! associate_gnat_to_gnu
[gnat_entity
- First_Node_Id
])
204 return associate_gnat_to_gnu
[gnat_entity
- First_Node_Id
];
207 /* Return nonzero if a GCC tree has been associated with GNAT_ENTITY. */
210 present_gnu_tree (Entity_Id gnat_entity
)
212 return (associate_gnat_to_gnu
[gnat_entity
- First_Node_Id
] != NULL_TREE
);
216 /* Return non-zero if we are currently in the global binding level. */
219 global_bindings_p (void)
221 return (force_global
!= 0 || current_binding_level
== global_binding_level
225 /* Return the list of declarations in the current level. Note that this list
226 is in reverse order (it has to be so for back-end compatibility). */
231 return current_binding_level
->names
;
234 /* Nonzero if the current level needs to have a BLOCK made. */
239 return (current_binding_level
->names
!= 0);
242 /* Enter a new binding level. The input parameter is ignored, but has to be
243 specified for back-end compatibility. */
246 pushlevel (int ignore ATTRIBUTE_UNUSED
)
248 struct binding_level
*newlevel
= NULL
;
250 /* Reuse a struct for this binding level, if there is one. */
251 if (free_binding_level
)
253 newlevel
= free_binding_level
;
254 free_binding_level
= free_binding_level
->level_chain
;
258 = (struct binding_level
*) ggc_alloc (sizeof (struct binding_level
));
260 *newlevel
= clear_binding_level
;
262 /* Add this level to the front of the chain (stack) of levels that are
264 newlevel
->level_chain
= current_binding_level
;
265 current_binding_level
= newlevel
;
268 /* Exit a binding level.
269 Pop the level off, and restore the state of the identifier-decl mappings
270 that were in effect when this level was entered.
272 If KEEP is nonzero, this level had explicit declarations, so
273 and create a "block" (a BLOCK node) for the level
274 to record its declarations and subblocks for symbol table output.
276 If FUNCTIONBODY is nonzero, this level is the body of a function,
277 so create a block as if KEEP were set and also clear out all
280 If REVERSE is nonzero, reverse the order of decls before putting
281 them into the BLOCK. */
284 poplevel (int keep
, int reverse
, int functionbody
)
286 /* Points to a GCC BLOCK tree node. This is the BLOCK node construted for the
287 binding level that we are about to exit and which is returned by this
289 tree block
= NULL_TREE
;
292 tree subblock_chain
= current_binding_level
->blocks
;
294 int block_previously_created
;
296 /* Reverse the list of XXXX_DECL nodes if desired. Note that the ..._DECL
297 nodes chained through the `names' field of current_binding_level are in
298 reverse order except for PARM_DECL node, which are explicitly stored in
300 current_binding_level
->names
301 = decl_chain
= (reverse
) ? nreverse (current_binding_level
->names
)
302 : current_binding_level
->names
;
304 /* Output any nested inline functions within this block which must be
305 compiled because their address is needed. */
306 for (decl_node
= decl_chain
; decl_node
; decl_node
= TREE_CHAIN (decl_node
))
307 if (TREE_CODE (decl_node
) == FUNCTION_DECL
308 && ! TREE_ASM_WRITTEN (decl_node
) && TREE_ADDRESSABLE (decl_node
)
309 && DECL_INITIAL (decl_node
) != 0)
311 push_function_context ();
312 output_inline_function (decl_node
);
313 pop_function_context ();
317 block_previously_created
= (current_binding_level
->this_block
!= 0);
318 if (block_previously_created
)
319 block
= current_binding_level
->this_block
;
320 else if (keep
|| functionbody
)
321 block
= make_node (BLOCK
);
324 BLOCK_VARS (block
) = keep
? decl_chain
: 0;
325 BLOCK_SUBBLOCKS (block
) = subblock_chain
;
328 /* Record the BLOCK node just built as the subblock its enclosing scope. */
329 for (subblock_node
= subblock_chain
; subblock_node
;
330 subblock_node
= TREE_CHAIN (subblock_node
))
331 BLOCK_SUPERCONTEXT (subblock_node
) = block
;
333 /* Clear out the meanings of the local variables of this level. */
335 for (subblock_node
= decl_chain
; subblock_node
;
336 subblock_node
= TREE_CHAIN (subblock_node
))
337 if (DECL_NAME (subblock_node
) != 0)
338 /* If the identifier was used or addressed via a local extern decl,
339 don't forget that fact. */
340 if (DECL_EXTERNAL (subblock_node
))
342 if (TREE_USED (subblock_node
))
343 TREE_USED (DECL_NAME (subblock_node
)) = 1;
344 if (TREE_ADDRESSABLE (subblock_node
))
345 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (subblock_node
)) = 1;
349 /* Pop the current level, and free the structure for reuse. */
350 struct binding_level
*level
= current_binding_level
;
351 current_binding_level
= current_binding_level
->level_chain
;
352 level
->level_chain
= free_binding_level
;
353 free_binding_level
= level
;
358 /* This is the top level block of a function. The ..._DECL chain stored
359 in BLOCK_VARS are the function's parameters (PARM_DECL nodes). Don't
360 leave them in the BLOCK because they are found in the FUNCTION_DECL
362 DECL_INITIAL (current_function_decl
) = block
;
363 BLOCK_VARS (block
) = 0;
367 if (!block_previously_created
)
368 current_binding_level
->blocks
369 = chainon (current_binding_level
->blocks
, block
);
372 /* If we did not make a block for the level just exited, any blocks made for
373 inner levels (since they cannot be recorded as subblocks in that level)
374 must be carried forward so they will later become subblocks of something
376 else if (subblock_chain
)
377 current_binding_level
->blocks
378 = chainon (current_binding_level
->blocks
, subblock_chain
);
380 TREE_USED (block
) = 1;
385 /* Insert BLOCK at the end of the list of subblocks of the
386 current binding level. This is used when a BIND_EXPR is expanded,
387 to handle the BLOCK node inside the BIND_EXPR. */
390 insert_block (tree block
)
392 TREE_USED (block
) = 1;
393 current_binding_level
->blocks
394 = chainon (current_binding_level
->blocks
, block
);
397 /* Set the BLOCK node for the innermost scope
398 (the one we are currently in). */
401 set_block (tree block
)
403 current_binding_level
->this_block
= block
;
404 current_binding_level
->names
= chainon (current_binding_level
->names
,
406 current_binding_level
->blocks
= chainon (current_binding_level
->blocks
,
407 BLOCK_SUBBLOCKS (block
));
410 /* Records a ..._DECL node DECL as belonging to the current lexical scope.
411 Returns the ..._DECL node. */
416 struct binding_level
*b
;
418 /* If at top level, there is no context. But PARM_DECLs always go in the
419 level of its function. */
420 if (global_bindings_p () && TREE_CODE (decl
) != PARM_DECL
)
422 b
= global_binding_level
;
423 DECL_CONTEXT (decl
) = 0;
427 b
= current_binding_level
;
428 DECL_CONTEXT (decl
) = current_function_decl
;
431 /* Put the declaration on the list. The list of declarations is in reverse
432 order. The list will be reversed later if necessary. This needs to be
433 this way for compatibility with the back-end.
435 Don't put TYPE_DECLs for UNCONSTRAINED_ARRAY_TYPE into the list. They
436 will cause trouble with the debugger and aren't needed anyway. */
437 if (TREE_CODE (decl
) != TYPE_DECL
438 || TREE_CODE (TREE_TYPE (decl
)) != UNCONSTRAINED_ARRAY_TYPE
)
440 TREE_CHAIN (decl
) = b
->names
;
444 /* For the declaration of a type, set its name if it either is not already
445 set, was set to an IDENTIFIER_NODE, indicating an internal name,
446 or if the previous type name was not derived from a source name.
447 We'd rather have the type named with a real name and all the pointer
448 types to the same object have the same POINTER_TYPE node. Code in this
449 function in c-decl.c makes a copy of the type node here, but that may
450 cause us trouble with incomplete types, so let's not try it (at least
453 if (TREE_CODE (decl
) == TYPE_DECL
454 && DECL_NAME (decl
) != 0
455 && (TYPE_NAME (TREE_TYPE (decl
)) == 0
456 || TREE_CODE (TYPE_NAME (TREE_TYPE (decl
))) == IDENTIFIER_NODE
457 || (TREE_CODE (TYPE_NAME (TREE_TYPE (decl
))) == TYPE_DECL
458 && DECL_ARTIFICIAL (TYPE_NAME (TREE_TYPE (decl
)))
459 && ! DECL_ARTIFICIAL (decl
))))
460 TYPE_NAME (TREE_TYPE (decl
)) = decl
;
465 /* Do little here. Set up the standard declarations later after the
466 front end has been run. */
469 gnat_init_decl_processing (void)
473 /* Make the binding_level structure for global names. */
474 current_function_decl
= 0;
475 current_binding_level
= 0;
476 free_binding_level
= 0;
478 global_binding_level
= current_binding_level
;
480 build_common_tree_nodes (0);
482 /* In Ada, we use a signed type for SIZETYPE. Use the signed type
483 corresponding to the size of Pmode. In most cases when ptr_mode and
484 Pmode differ, C will use the width of ptr_mode as sizetype. But we get
485 far better code using the width of Pmode. Make this here since we need
486 this before we can expand the GNAT types. */
487 set_sizetype (gnat_type_for_size (GET_MODE_BITSIZE (Pmode
), 0));
488 build_common_tree_nodes_2 (0);
490 pushdecl (build_decl (TYPE_DECL
, get_identifier (SIZE_TYPE
), sizetype
));
492 /* We need to make the integer type before doing anything else.
493 We stitch this in to the appropriate GNAT type later. */
494 pushdecl (build_decl (TYPE_DECL
, get_identifier ("integer"),
496 pushdecl (build_decl (TYPE_DECL
, get_identifier ("unsigned char"),
499 ptr_void_type_node
= build_pointer_type (void_type_node
);
503 /* Create the predefined scalar types such as `integer_type_node' needed
504 in the gcc back-end and initialize the global binding level. */
507 init_gigi_decls (tree long_long_float_type
, tree exception_type
)
512 /* Set the types that GCC and Gigi use from the front end. We would like
513 to do this for char_type_node, but it needs to correspond to the C
515 if (TREE_CODE (TREE_TYPE (long_long_float_type
)) == INTEGER_TYPE
)
517 /* In this case, the builtin floating point types are VAX float,
518 so make up a type for use. */
519 longest_float_type_node
= make_node (REAL_TYPE
);
520 TYPE_PRECISION (longest_float_type_node
) = LONG_DOUBLE_TYPE_SIZE
;
521 layout_type (longest_float_type_node
);
522 pushdecl (build_decl (TYPE_DECL
, get_identifier ("longest float type"),
523 longest_float_type_node
));
526 longest_float_type_node
= TREE_TYPE (long_long_float_type
);
528 except_type_node
= TREE_TYPE (exception_type
);
530 unsigned_type_node
= gnat_type_for_size (INT_TYPE_SIZE
, 1);
531 pushdecl (build_decl (TYPE_DECL
, get_identifier ("unsigned int"),
532 unsigned_type_node
));
535 = pushdecl (build_decl (TYPE_DECL
, get_identifier ("void"),
538 void_ftype
= build_function_type (void_type_node
, NULL_TREE
);
539 ptr_void_ftype
= build_pointer_type (void_ftype
);
541 /* Now declare runtime functions. */
542 endlink
= tree_cons (NULL_TREE
, void_type_node
, NULL_TREE
);
544 /* malloc is a function declaration tree for a function to allocate
546 malloc_decl
= create_subprog_decl (get_identifier ("__gnat_malloc"),
548 build_function_type (ptr_void_type_node
,
549 tree_cons (NULL_TREE
,
552 NULL_TREE
, 0, 1, 1, 0);
554 /* free is a function declaration tree for a function to free memory. */
556 = create_subprog_decl (get_identifier ("__gnat_free"), NULL_TREE
,
557 build_function_type (void_type_node
,
558 tree_cons (NULL_TREE
,
561 NULL_TREE
, 0, 1, 1, 0);
563 /* Make the types and functions used for exception processing. */
565 = build_array_type (gnat_type_for_mode (Pmode
, 0),
566 build_index_type (build_int_2 (5, 0)));
567 pushdecl (build_decl (TYPE_DECL
, get_identifier ("JMPBUF_T"), jmpbuf_type
));
568 jmpbuf_ptr_type
= build_pointer_type (jmpbuf_type
);
570 /* Functions to get and set the jumpbuf pointer for the current thread. */
572 = create_subprog_decl
573 (get_identifier ("system__soft_links__get_jmpbuf_address_soft"),
574 NULL_TREE
, build_function_type (jmpbuf_ptr_type
, NULL_TREE
),
575 NULL_TREE
, 0, 1, 1, 0);
578 = create_subprog_decl
579 (get_identifier ("system__soft_links__set_jmpbuf_address_soft"),
581 build_function_type (void_type_node
,
582 tree_cons (NULL_TREE
, jmpbuf_ptr_type
, endlink
)),
583 NULL_TREE
, 0, 1, 1, 0);
585 /* Function to get the current exception. */
587 = create_subprog_decl
588 (get_identifier ("system__soft_links__get_gnat_exception"),
590 build_function_type (build_pointer_type (except_type_node
), NULL_TREE
),
591 NULL_TREE
, 0, 1, 1, 0);
593 /* Functions that raise exceptions. */
595 = create_subprog_decl
596 (get_identifier ("__gnat_raise_nodefer_with_msg"), NULL_TREE
,
597 build_function_type (void_type_node
,
598 tree_cons (NULL_TREE
,
599 build_pointer_type (except_type_node
),
601 NULL_TREE
, 0, 1, 1, 0);
603 /* Hooks to call when entering/leaving an exception handler. */
605 = create_subprog_decl (get_identifier ("__gnat_begin_handler"), NULL_TREE
,
606 build_function_type (void_type_node
,
607 tree_cons (NULL_TREE
,
610 NULL_TREE
, 0, 1, 1, 0);
613 = create_subprog_decl (get_identifier ("__gnat_end_handler"), NULL_TREE
,
614 build_function_type (void_type_node
,
615 tree_cons (NULL_TREE
,
618 NULL_TREE
, 0, 1, 1, 0);
620 /* If in no exception handlers mode, all raise statements are redirected to
621 __gnat_last_chance_handler. No need to redefine raise_nodefer_decl, since
622 this procedure will never be called in this mode. */
623 if (No_Exception_Handlers_Set ())
626 = create_subprog_decl
627 (get_identifier ("__gnat_last_chance_handler"), NULL_TREE
,
628 build_function_type (void_type_node
,
629 tree_cons (NULL_TREE
,
630 build_pointer_type (char_type_node
),
631 tree_cons (NULL_TREE
,
634 NULL_TREE
, 0, 1, 1, 0);
636 for (i
= 0; i
< ARRAY_SIZE (gnat_raise_decls
); i
++)
637 gnat_raise_decls
[i
] = decl
;
640 /* Otherwise, make one decl for each exception reason. */
641 for (i
= 0; i
< ARRAY_SIZE (gnat_raise_decls
); i
++)
645 sprintf (name
, "__gnat_rcheck_%.2d", i
);
647 = create_subprog_decl
648 (get_identifier (name
), NULL_TREE
,
649 build_function_type (void_type_node
,
650 tree_cons (NULL_TREE
,
653 tree_cons (NULL_TREE
,
656 NULL_TREE
, 0, 1, 1, 0);
659 /* Indicate that these never return. */
660 TREE_THIS_VOLATILE (raise_nodefer_decl
) = 1;
661 TREE_SIDE_EFFECTS (raise_nodefer_decl
) = 1;
662 TREE_TYPE (raise_nodefer_decl
)
663 = build_qualified_type (TREE_TYPE (raise_nodefer_decl
),
666 for (i
= 0; i
< ARRAY_SIZE (gnat_raise_decls
); i
++)
668 TREE_THIS_VOLATILE (gnat_raise_decls
[i
]) = 1;
669 TREE_SIDE_EFFECTS (gnat_raise_decls
[i
]) = 1;
670 TREE_TYPE (gnat_raise_decls
[i
])
671 = build_qualified_type (TREE_TYPE (gnat_raise_decls
[i
]),
675 /* setjmp returns an integer and has one operand, which is a pointer to
678 = create_subprog_decl
679 (get_identifier ("__builtin_setjmp"), NULL_TREE
,
680 build_function_type (integer_type_node
,
681 tree_cons (NULL_TREE
, jmpbuf_ptr_type
, endlink
)),
682 NULL_TREE
, 0, 1, 1, 0);
684 DECL_BUILT_IN_CLASS (setjmp_decl
) = BUILT_IN_NORMAL
;
685 DECL_FUNCTION_CODE (setjmp_decl
) = BUILT_IN_SETJMP
;
687 main_identifier_node
= get_identifier ("main");
690 /* Given a record type (RECORD_TYPE) and a chain of FIELD_DECL
691 nodes (FIELDLIST), finish constructing the record or union type.
692 If HAS_REP is nonzero, this record has a rep clause; don't call
693 layout_type but merely set the size and alignment ourselves.
694 If DEFER_DEBUG is nonzero, do not call the debugging routines
695 on this type; it will be done later. */
698 finish_record_type (tree record_type
,
703 enum tree_code code
= TREE_CODE (record_type
);
704 tree ada_size
= bitsize_zero_node
;
705 tree size
= bitsize_zero_node
;
706 tree size_unit
= size_zero_node
;
710 TYPE_FIELDS (record_type
) = fieldlist
;
712 if (TYPE_NAME (record_type
) != 0
713 && TREE_CODE (TYPE_NAME (record_type
)) == TYPE_DECL
)
714 TYPE_STUB_DECL (record_type
) = TYPE_NAME (record_type
);
716 TYPE_STUB_DECL (record_type
)
717 = pushdecl (build_decl (TYPE_DECL
, TYPE_NAME (record_type
),
720 /* We don't need both the typedef name and the record name output in
721 the debugging information, since they are the same. */
722 DECL_ARTIFICIAL (TYPE_STUB_DECL (record_type
)) = 1;
724 /* Globally initialize the record first. If this is a rep'ed record,
725 that just means some initializations; otherwise, layout the record. */
729 TYPE_ALIGN (record_type
) = MAX (BITS_PER_UNIT
, TYPE_ALIGN (record_type
));
730 TYPE_MODE (record_type
) = BLKmode
;
731 if (TYPE_SIZE (record_type
) == 0)
733 TYPE_SIZE (record_type
) = bitsize_zero_node
;
734 TYPE_SIZE_UNIT (record_type
) = size_zero_node
;
736 /* For all-repped records with a size specified, lay the QUAL_UNION_TYPE
737 out just like a UNION_TYPE, since the size will be fixed. */
738 else if (code
== QUAL_UNION_TYPE
)
743 /* Ensure there isn't a size already set. There can be in an error
744 case where there is a rep clause but all fields have errors and
745 no longer have a position. */
746 TYPE_SIZE (record_type
) = 0;
747 layout_type (record_type
);
750 /* At this point, the position and size of each field is known. It was
751 either set before entry by a rep clause, or by laying out the type above.
753 We now run a pass over the fields (in reverse order for QUAL_UNION_TYPEs)
754 to compute the Ada size; the GCC size and alignment (for rep'ed records
755 that are not padding types); and the mode (for rep'ed records). We also
756 clear the DECL_BIT_FIELD indication for the cases we know have not been
757 handled yet, and adjust DECL_NONADDRESSABLE_P accordingly. */
759 if (code
== QUAL_UNION_TYPE
)
760 fieldlist
= nreverse (fieldlist
);
762 for (field
= fieldlist
; field
; field
= TREE_CHAIN (field
))
764 tree pos
= bit_position (field
);
766 tree type
= TREE_TYPE (field
);
767 tree this_size
= DECL_SIZE (field
);
768 tree this_size_unit
= DECL_SIZE_UNIT (field
);
769 tree this_ada_size
= DECL_SIZE (field
);
771 /* We need to make an XVE/XVU record if any field has variable size,
772 whether or not the record does. For example, if we have an union,
773 it may be that all fields, rounded up to the alignment, have the
774 same size, in which case we'll use that size. But the debug
775 output routines (except Dwarf2) won't be able to output the fields,
776 so we need to make the special record. */
777 if (TREE_CODE (this_size
) != INTEGER_CST
)
780 if ((TREE_CODE (type
) == RECORD_TYPE
|| TREE_CODE (type
) == UNION_TYPE
781 || TREE_CODE (type
) == QUAL_UNION_TYPE
)
782 && ! TYPE_IS_FAT_POINTER_P (type
)
783 && ! TYPE_CONTAINS_TEMPLATE_P (type
)
784 && TYPE_ADA_SIZE (type
) != 0)
785 this_ada_size
= TYPE_ADA_SIZE (type
);
787 /* Clear DECL_BIT_FIELD for the cases layout_decl does not handle. */
788 if (DECL_BIT_FIELD (field
) && !STRICT_ALIGNMENT
789 && value_factor_p (pos
, BITS_PER_UNIT
)
790 && operand_equal_p (this_size
, TYPE_SIZE (type
), 0))
791 DECL_BIT_FIELD (field
) = 0;
793 /* If we still have DECL_BIT_FIELD set at this point, we know the field
794 is technically not addressable. */
795 DECL_NONADDRESSABLE_P (field
) |= DECL_BIT_FIELD (field
);
797 if (has_rep
&& ! DECL_BIT_FIELD (field
))
798 TYPE_ALIGN (record_type
)
799 = MAX (TYPE_ALIGN (record_type
), DECL_ALIGN (field
));
804 ada_size
= size_binop (MAX_EXPR
, ada_size
, this_ada_size
);
805 size
= size_binop (MAX_EXPR
, size
, this_size
);
806 size_unit
= size_binop (MAX_EXPR
, size_unit
, this_size_unit
);
809 case QUAL_UNION_TYPE
:
811 = fold (build (COND_EXPR
, bitsizetype
, DECL_QUALIFIER (field
),
812 this_ada_size
, ada_size
));
813 size
= fold (build (COND_EXPR
, bitsizetype
, DECL_QUALIFIER (field
),
815 size_unit
= fold (build (COND_EXPR
, sizetype
, DECL_QUALIFIER (field
),
816 this_size_unit
, size_unit
));
820 /* Since we know here that all fields are sorted in order of
821 increasing bit position, the size of the record is one
822 higher than the ending bit of the last field processed
823 unless we have a rep clause, since in that case we might
824 have a field outside a QUAL_UNION_TYPE that has a higher ending
825 position. So use a MAX in that case. Also, if this field is a
826 QUAL_UNION_TYPE, we need to take into account the previous size in
827 the case of empty variants. */
829 = merge_sizes (ada_size
, pos
, this_ada_size
,
830 TREE_CODE (type
) == QUAL_UNION_TYPE
, has_rep
);
831 size
= merge_sizes (size
, pos
, this_size
,
832 TREE_CODE (type
) == QUAL_UNION_TYPE
, has_rep
);
834 = merge_sizes (size_unit
, byte_position (field
), this_size_unit
,
835 TREE_CODE (type
) == QUAL_UNION_TYPE
, has_rep
);
843 if (code
== QUAL_UNION_TYPE
)
844 nreverse (fieldlist
);
846 /* If this is a padding record, we never want to make the size smaller than
847 what was specified in it, if any. */
848 if (TREE_CODE (record_type
) == RECORD_TYPE
849 && TYPE_IS_PADDING_P (record_type
) && TYPE_SIZE (record_type
) != 0)
851 size
= TYPE_SIZE (record_type
);
852 size_unit
= TYPE_SIZE_UNIT (record_type
);
855 /* Now set any of the values we've just computed that apply. */
856 if (! TYPE_IS_FAT_POINTER_P (record_type
)
857 && ! TYPE_CONTAINS_TEMPLATE_P (record_type
))
858 SET_TYPE_ADA_SIZE (record_type
, ada_size
);
862 if (! (TREE_CODE (record_type
) == RECORD_TYPE
863 && TYPE_IS_PADDING_P (record_type
)
864 && CONTAINS_PLACEHOLDER_P (size
)))
866 TYPE_SIZE (record_type
) = round_up (size
, TYPE_ALIGN (record_type
));
867 TYPE_SIZE_UNIT (record_type
)
868 = round_up (size_unit
,
869 TYPE_ALIGN (record_type
) / BITS_PER_UNIT
);
872 compute_record_mode (record_type
);
877 /* If this record is of variable size, rename it so that the
878 debugger knows it is and make a new, parallel, record
879 that tells the debugger how the record is laid out. See
880 exp_dbug.ads. But don't do this for records that are padding
881 since they confuse GDB. */
883 && ! (TREE_CODE (record_type
) == RECORD_TYPE
884 && TYPE_IS_PADDING_P (record_type
)))
887 = make_node (TREE_CODE (record_type
) == QUAL_UNION_TYPE
888 ? UNION_TYPE
: TREE_CODE (record_type
));
889 tree orig_id
= DECL_NAME (TYPE_STUB_DECL (record_type
));
891 = concat_id_with_name (orig_id
,
892 TREE_CODE (record_type
) == QUAL_UNION_TYPE
894 tree last_pos
= bitsize_zero_node
;
896 tree prev_old_field
= 0;
898 TYPE_NAME (new_record_type
) = new_id
;
899 TYPE_ALIGN (new_record_type
) = BIGGEST_ALIGNMENT
;
900 TYPE_STUB_DECL (new_record_type
)
901 = pushdecl (build_decl (TYPE_DECL
, new_id
, new_record_type
));
902 DECL_ARTIFICIAL (TYPE_STUB_DECL (new_record_type
)) = 1;
903 DECL_IGNORED_P (TYPE_STUB_DECL (new_record_type
))
904 = DECL_IGNORED_P (TYPE_STUB_DECL (record_type
));
905 TYPE_SIZE (new_record_type
) = size_int (TYPE_ALIGN (record_type
));
907 /* Now scan all the fields, replacing each field with a new
908 field corresponding to the new encoding. */
909 for (old_field
= TYPE_FIELDS (record_type
); old_field
!= 0;
910 old_field
= TREE_CHAIN (old_field
))
912 tree field_type
= TREE_TYPE (old_field
);
913 tree field_name
= DECL_NAME (old_field
);
915 tree curpos
= bit_position (old_field
);
917 unsigned int align
= 0;
920 /* See how the position was modified from the last position.
922 There are two basic cases we support: a value was added
923 to the last position or the last position was rounded to
924 a boundary and they something was added. Check for the
925 first case first. If not, see if there is any evidence
926 of rounding. If so, round the last position and try
929 If this is a union, the position can be taken as zero. */
931 if (TREE_CODE (new_record_type
) == UNION_TYPE
)
932 pos
= bitsize_zero_node
, align
= 0;
934 pos
= compute_related_constant (curpos
, last_pos
);
936 if (pos
== 0 && TREE_CODE (curpos
) == MULT_EXPR
937 && TREE_CODE (TREE_OPERAND (curpos
, 1)) == INTEGER_CST
)
939 align
= TREE_INT_CST_LOW (TREE_OPERAND (curpos
, 1));
940 pos
= compute_related_constant (curpos
,
941 round_up (last_pos
, align
));
943 else if (pos
== 0 && TREE_CODE (curpos
) == PLUS_EXPR
944 && TREE_CODE (TREE_OPERAND (curpos
, 1)) == INTEGER_CST
945 && TREE_CODE (TREE_OPERAND (curpos
, 0)) == MULT_EXPR
946 && host_integerp (TREE_OPERAND
947 (TREE_OPERAND (curpos
, 0), 1),
952 (TREE_OPERAND (TREE_OPERAND (curpos
, 0), 1), 1);
953 pos
= compute_related_constant (curpos
,
954 round_up (last_pos
, align
));
956 else if (potential_alignment_gap (prev_old_field
, old_field
,
959 align
= TYPE_ALIGN (field_type
);
960 pos
= compute_related_constant (curpos
,
961 round_up (last_pos
, align
));
964 /* If we can't compute a position, set it to zero.
966 ??? We really should abort here, but it's too much work
967 to get this correct for all cases. */
970 pos
= bitsize_zero_node
;
972 /* See if this type is variable-size and make a new type
973 and indicate the indirection if so. */
974 if (TREE_CODE (DECL_SIZE (old_field
)) != INTEGER_CST
)
976 field_type
= build_pointer_type (field_type
);
980 /* Make a new field name, if necessary. */
981 if (var
|| align
!= 0)
986 sprintf (suffix
, "XV%c%u", var
? 'L' : 'A',
987 align
/ BITS_PER_UNIT
);
989 strcpy (suffix
, "XVL");
991 field_name
= concat_id_with_name (field_name
, suffix
);
994 new_field
= create_field_decl (field_name
, field_type
,
996 DECL_SIZE (old_field
), pos
, 0);
997 TREE_CHAIN (new_field
) = TYPE_FIELDS (new_record_type
);
998 TYPE_FIELDS (new_record_type
) = new_field
;
1000 /* If old_field is a QUAL_UNION_TYPE, take its size as being
1001 zero. The only time it's not the last field of the record
1002 is when there are other components at fixed positions after
1003 it (meaning there was a rep clause for every field) and we
1004 want to be able to encode them. */
1005 last_pos
= size_binop (PLUS_EXPR
, bit_position (old_field
),
1006 (TREE_CODE (TREE_TYPE (old_field
))
1009 : DECL_SIZE (old_field
));
1010 prev_old_field
= old_field
;
1013 TYPE_FIELDS (new_record_type
)
1014 = nreverse (TYPE_FIELDS (new_record_type
));
1016 rest_of_type_compilation (new_record_type
, global_bindings_p ());
1019 rest_of_type_compilation (record_type
, global_bindings_p ());
1023 /* Utility function of above to merge LAST_SIZE, the previous size of a record
1024 with FIRST_BIT and SIZE that describe a field. SPECIAL is nonzero
1025 if this represents a QUAL_UNION_TYPE in which case we must look for
1026 COND_EXPRs and replace a value of zero with the old size. If HAS_REP
1027 is nonzero, we must take the MAX of the end position of this field
1028 with LAST_SIZE. In all other cases, we use FIRST_BIT plus SIZE.
1030 We return an expression for the size. */
1033 merge_sizes (tree last_size
,
1039 tree type
= TREE_TYPE (last_size
);
1042 if (! special
|| TREE_CODE (size
) != COND_EXPR
)
1044 new = size_binop (PLUS_EXPR
, first_bit
, size
);
1046 new = size_binop (MAX_EXPR
, last_size
, new);
1050 new = fold (build (COND_EXPR
, type
, TREE_OPERAND (size
, 0),
1051 integer_zerop (TREE_OPERAND (size
, 1))
1052 ? last_size
: merge_sizes (last_size
, first_bit
,
1053 TREE_OPERAND (size
, 1),
1055 integer_zerop (TREE_OPERAND (size
, 2))
1056 ? last_size
: merge_sizes (last_size
, first_bit
,
1057 TREE_OPERAND (size
, 2),
1060 /* We don't need any NON_VALUE_EXPRs and they can confuse us (especially
1061 when fed through substitute_in_expr) into thinking that a constant
1062 size is not constant. */
1063 while (TREE_CODE (new) == NON_LVALUE_EXPR
)
1064 new = TREE_OPERAND (new, 0);
1069 /* Utility function of above to see if OP0 and OP1, both of SIZETYPE, are
1070 related by the addition of a constant. Return that constant if so. */
1073 compute_related_constant (tree op0
, tree op1
)
1075 tree op0_var
, op1_var
;
1076 tree op0_con
= split_plus (op0
, &op0_var
);
1077 tree op1_con
= split_plus (op1
, &op1_var
);
1078 tree result
= size_binop (MINUS_EXPR
, op0_con
, op1_con
);
1080 if (operand_equal_p (op0_var
, op1_var
, 0))
1082 else if (operand_equal_p (op0
, size_binop (PLUS_EXPR
, op1_var
, result
), 0))
1088 /* Utility function of above to split a tree OP which may be a sum, into a
1089 constant part, which is returned, and a variable part, which is stored
1090 in *PVAR. *PVAR may be bitsize_zero_node. All operations must be of
1094 split_plus (tree in
, tree
*pvar
)
1096 /* Strip NOPS in order to ease the tree traversal and maximize the
1097 potential for constant or plus/minus discovery. We need to be careful
1098 to always return and set *pvar to bitsizetype trees, but it's worth
1102 *pvar
= convert (bitsizetype
, in
);
1104 if (TREE_CODE (in
) == INTEGER_CST
)
1106 *pvar
= bitsize_zero_node
;
1107 return convert (bitsizetype
, in
);
1109 else if (TREE_CODE (in
) == PLUS_EXPR
|| TREE_CODE (in
) == MINUS_EXPR
)
1111 tree lhs_var
, rhs_var
;
1112 tree lhs_con
= split_plus (TREE_OPERAND (in
, 0), &lhs_var
);
1113 tree rhs_con
= split_plus (TREE_OPERAND (in
, 1), &rhs_var
);
1115 if (lhs_var
== TREE_OPERAND (in
, 0)
1116 && rhs_var
== TREE_OPERAND (in
, 1))
1117 return bitsize_zero_node
;
1119 *pvar
= size_binop (TREE_CODE (in
), lhs_var
, rhs_var
);
1120 return size_binop (TREE_CODE (in
), lhs_con
, rhs_con
);
1123 return bitsize_zero_node
;
1126 /* Return a FUNCTION_TYPE node. RETURN_TYPE is the type returned by the
1127 subprogram. If it is void_type_node, then we are dealing with a procedure,
1128 otherwise we are dealing with a function. PARAM_DECL_LIST is a list of
1129 PARM_DECL nodes that are the subprogram arguments. CICO_LIST is the
1130 copy-in/copy-out list to be stored into TYPE_CICO_LIST.
1131 RETURNS_UNCONSTRAINED is nonzero if the function returns an unconstrained
1132 object. RETURNS_BY_REF is nonzero if the function returns by reference.
1133 RETURNS_WITH_DSP is nonzero if the function is to return with a
1134 depressed stack pointer. */
1137 create_subprog_type (tree return_type
,
1138 tree param_decl_list
,
1140 int returns_unconstrained
,
1142 int returns_with_dsp
)
1144 /* A chain of TREE_LIST nodes whose TREE_VALUEs are the data type nodes of
1145 the subprogram formal parameters. This list is generated by traversing the
1146 input list of PARM_DECL nodes. */
1147 tree param_type_list
= NULL
;
1151 for (param_decl
= param_decl_list
; param_decl
;
1152 param_decl
= TREE_CHAIN (param_decl
))
1153 param_type_list
= tree_cons (NULL_TREE
, TREE_TYPE (param_decl
),
1156 /* The list of the function parameter types has to be terminated by the void
1157 type to signal to the back-end that we are not dealing with a variable
1158 parameter subprogram, but that the subprogram has a fixed number of
1160 param_type_list
= tree_cons (NULL_TREE
, void_type_node
, param_type_list
);
1162 /* The list of argument types has been created in reverse
1164 param_type_list
= nreverse (param_type_list
);
1166 type
= build_function_type (return_type
, param_type_list
);
1168 /* TYPE may have been shared since GCC hashes types. If it has a CICO_LIST
1169 or the new type should, make a copy of TYPE. Likewise for
1170 RETURNS_UNCONSTRAINED and RETURNS_BY_REF. */
1171 if (TYPE_CI_CO_LIST (type
) != 0 || cico_list
!= 0
1172 || TYPE_RETURNS_UNCONSTRAINED_P (type
) != returns_unconstrained
1173 || TYPE_RETURNS_BY_REF_P (type
) != returns_by_ref
)
1174 type
= copy_type (type
);
1176 SET_TYPE_CI_CO_LIST (type
, cico_list
);
1177 TYPE_RETURNS_UNCONSTRAINED_P (type
) = returns_unconstrained
;
1178 TYPE_RETURNS_STACK_DEPRESSED (type
) = returns_with_dsp
;
1179 TYPE_RETURNS_BY_REF_P (type
) = returns_by_ref
;
1183 /* Return a copy of TYPE but safe to modify in any way. */
1186 copy_type (tree type
)
1188 tree
new = copy_node (type
);
1190 /* copy_node clears this field instead of copying it, because it is
1191 aliased with TREE_CHAIN. */
1192 TYPE_STUB_DECL (new) = TYPE_STUB_DECL (type
);
1194 TYPE_POINTER_TO (new) = 0;
1195 TYPE_REFERENCE_TO (new) = 0;
1196 TYPE_MAIN_VARIANT (new) = new;
1197 TYPE_NEXT_VARIANT (new) = 0;
1202 /* Return an INTEGER_TYPE of SIZETYPE with range MIN to MAX and whose
1203 TYPE_INDEX_TYPE is INDEX. */
1206 create_index_type (tree min
, tree max
, tree index
)
1208 /* First build a type for the desired range. */
1209 tree type
= build_index_2_type (min
, max
);
1211 /* If this type has the TYPE_INDEX_TYPE we want, return it. Otherwise, if it
1212 doesn't have TYPE_INDEX_TYPE set, set it to INDEX. If TYPE_INDEX_TYPE
1213 is set, but not to INDEX, make a copy of this type with the requested
1214 index type. Note that we have no way of sharing these types, but that's
1215 only a small hole. */
1216 if (TYPE_INDEX_TYPE (type
) == index
)
1218 else if (TYPE_INDEX_TYPE (type
) != 0)
1219 type
= copy_type (type
);
1221 SET_TYPE_INDEX_TYPE (type
, index
);
1225 /* Return a TYPE_DECL node. TYPE_NAME gives the name of the type (a character
1226 string) and TYPE is a ..._TYPE node giving its data type.
1227 ARTIFICIAL_P is nonzero if this is a declaration that was generated
1228 by the compiler. DEBUG_INFO_P is nonzero if we need to write debugging
1229 information about this type. */
1232 create_type_decl (tree type_name
,
1234 struct attrib
*attr_list
,
1238 tree type_decl
= build_decl (TYPE_DECL
, type_name
, type
);
1239 enum tree_code code
= TREE_CODE (type
);
1241 DECL_ARTIFICIAL (type_decl
) = artificial_p
;
1242 pushdecl (type_decl
);
1243 process_attributes (type_decl
, attr_list
);
1245 /* Pass type declaration information to the debugger unless this is an
1246 UNCONSTRAINED_ARRAY_TYPE, which the debugger does not support,
1247 and ENUMERAL_TYPE or RECORD_TYPE which is handled separately,
1248 a dummy type, which will be completed later, or a type for which
1249 debugging information was not requested. */
1250 if (code
== UNCONSTRAINED_ARRAY_TYPE
|| TYPE_IS_DUMMY_P (type
)
1252 DECL_IGNORED_P (type_decl
) = 1;
1253 else if (code
!= ENUMERAL_TYPE
&& code
!= RECORD_TYPE
1254 && ! ((code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
)
1255 && TYPE_IS_DUMMY_P (TREE_TYPE (type
))))
1256 rest_of_decl_compilation (type_decl
, NULL
, global_bindings_p (), 0);
1261 /* Returns a GCC VAR_DECL node. VAR_NAME gives the name of the variable.
1262 ASM_NAME is its assembler name (if provided). TYPE is its data type
1263 (a GCC ..._TYPE node). VAR_INIT is the GCC tree for an optional initial
1264 expression; NULL_TREE if none.
1266 CONST_FLAG is nonzero if this variable is constant.
1268 PUBLIC_FLAG is nonzero if this definition is to be made visible outside of
1269 the current compilation unit. This flag should be set when processing the
1270 variable definitions in a package specification. EXTERN_FLAG is nonzero
1271 when processing an external variable declaration (as opposed to a
1272 definition: no storage is to be allocated for the variable here).
1274 STATIC_FLAG is only relevant when not at top level. In that case
1275 it indicates whether to always allocate storage to the variable. */
1278 create_var_decl (tree var_name
,
1286 struct attrib
*attr_list
)
1291 : (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (var_init
))
1292 && (global_bindings_p () || static_flag
1293 ? 0 != initializer_constant_valid_p (var_init
,
1294 TREE_TYPE (var_init
))
1295 : TREE_CONSTANT (var_init
))));
1297 = build_decl ((const_flag
&& init_const
1298 /* Only make a CONST_DECL for sufficiently-small objects.
1299 We consider complex double "sufficiently-small" */
1300 && TYPE_SIZE (type
) != 0
1301 && host_integerp (TYPE_SIZE_UNIT (type
), 1)
1302 && 0 >= compare_tree_int (TYPE_SIZE_UNIT (type
),
1303 GET_MODE_SIZE (DCmode
)))
1304 ? CONST_DECL
: VAR_DECL
, var_name
, type
);
1305 tree assign_init
= 0;
1307 /* If this is external, throw away any initializations unless this is a
1308 CONST_DECL (meaning we have a constant); they will be done elsewhere. If
1309 we are defining a global here, leave a constant initialization and save
1310 any variable elaborations for the elaboration routine. Otherwise, if
1311 the initializing expression is not the same as TYPE, generate the
1312 initialization with an assignment statement, since it knows how
1313 to do the required adjustents. If we are just annotating types,
1314 throw away the initialization if it isn't a constant. */
1316 if ((extern_flag
&& TREE_CODE (var_decl
) != CONST_DECL
)
1317 || (type_annotate_only
&& var_init
!= 0 && ! TREE_CONSTANT (var_init
)))
1320 if (global_bindings_p () && var_init
!= 0 && ! init_const
)
1322 add_pending_elaborations (var_decl
, var_init
);
1326 else if (var_init
!= 0
1327 && ((TYPE_MAIN_VARIANT (TREE_TYPE (var_init
))
1328 != TYPE_MAIN_VARIANT (type
))
1329 || (static_flag
&& ! init_const
)))
1330 assign_init
= var_init
, var_init
= 0;
1332 DECL_COMMON (var_decl
) = !flag_no_common
;
1333 DECL_INITIAL (var_decl
) = var_init
;
1334 TREE_READONLY (var_decl
) = const_flag
;
1335 DECL_EXTERNAL (var_decl
) = extern_flag
;
1336 TREE_PUBLIC (var_decl
) = public_flag
|| extern_flag
;
1337 TREE_CONSTANT (var_decl
) = TREE_CODE (var_decl
) == CONST_DECL
;
1338 TREE_THIS_VOLATILE (var_decl
) = TREE_SIDE_EFFECTS (var_decl
)
1339 = TYPE_VOLATILE (type
);
1341 /* At the global binding level we need to allocate static storage for the
1342 variable if and only if its not external. If we are not at the top level
1343 we allocate automatic storage unless requested not to. */
1344 TREE_STATIC (var_decl
) = global_bindings_p () ? !extern_flag
: static_flag
;
1347 SET_DECL_ASSEMBLER_NAME (var_decl
, asm_name
);
1349 process_attributes (var_decl
, attr_list
);
1351 /* Add this decl to the current binding level and generate any
1352 needed code and RTL. */
1353 var_decl
= pushdecl (var_decl
);
1354 expand_decl (var_decl
);
1356 if (DECL_CONTEXT (var_decl
) != 0)
1357 expand_decl_init (var_decl
);
1359 /* If this is volatile, force it into memory. */
1360 if (TREE_SIDE_EFFECTS (var_decl
))
1361 gnat_mark_addressable (var_decl
);
1363 if (TREE_CODE (var_decl
) != CONST_DECL
)
1364 rest_of_decl_compilation (var_decl
, 0, global_bindings_p (), 0);
1366 if (assign_init
!= 0)
1368 /* If VAR_DECL has a padded type, convert it to the unpadded
1369 type so the assignment is done properly. */
1370 tree lhs
= var_decl
;
1372 if (TREE_CODE (TREE_TYPE (lhs
)) == RECORD_TYPE
1373 && TYPE_IS_PADDING_P (TREE_TYPE (lhs
)))
1374 lhs
= convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (lhs
))), lhs
);
1376 expand_expr_stmt (build_binary_op (MODIFY_EXPR
, NULL_TREE
, lhs
,
1383 /* Returns a FIELD_DECL node. FIELD_NAME the field name, FIELD_TYPE is its
1384 type, and RECORD_TYPE is the type of the parent. PACKED is nonzero if
1385 this field is in a record type with a "pragma pack". If SIZE is nonzero
1386 it is the specified size for this field. If POS is nonzero, it is the bit
1387 position. If ADDRESSABLE is nonzero, it means we are allowed to take
1388 the address of this field for aliasing purposes. */
1391 create_field_decl (tree field_name
,
1399 tree field_decl
= build_decl (FIELD_DECL
, field_name
, field_type
);
1401 DECL_CONTEXT (field_decl
) = record_type
;
1402 TREE_READONLY (field_decl
) = TREE_READONLY (field_type
);
1404 /* If FIELD_TYPE is BLKmode, we must ensure this is aligned to at least a
1405 byte boundary since GCC cannot handle less-aligned BLKmode bitfields. */
1406 if (packed
&& TYPE_MODE (field_type
) == BLKmode
)
1407 DECL_ALIGN (field_decl
) = BITS_PER_UNIT
;
1409 /* If a size is specified, use it. Otherwise, if the record type is packed
1410 compute a size to use, which may differ from the object's natural size.
1411 We always set a size in this case to trigger the checks for bitfield
1412 creation below, which is typically required when no position has been
1415 size
= convert (bitsizetype
, size
);
1416 else if (packed
== 1)
1418 size
= rm_size (field_type
);
1420 /* For a constant size larger than MAX_FIXED_MODE_SIZE, round up to
1422 if (TREE_CODE (size
) == INTEGER_CST
1423 && compare_tree_int (size
, MAX_FIXED_MODE_SIZE
) > 0)
1424 size
= round_up (size
, BITS_PER_UNIT
);
1427 /* Make a bitfield if a size is specified for two reasons: first if the size
1428 differs from the natural size. Second, if the alignment is insufficient.
1429 There are a number of ways the latter can be true.
1431 We never make a bitfield if the type of the field has a nonconstant size,
1432 or if it is claimed to be addressable, because no such entity requiring
1433 bitfield operations should reach here.
1435 We do *preventively* make a bitfield when there might be the need for it
1436 but we don't have all the necessary information to decide, as is the case
1437 of a field with no specified position in a packed record.
1439 We also don't look at STRICT_ALIGNMENT here, and rely on later processing
1440 in layout_decl or finish_record_type to clear the bit_field indication if
1441 it is in fact not needed. */
1442 if (size
!= 0 && TREE_CODE (size
) == INTEGER_CST
1443 && TREE_CODE (TYPE_SIZE (field_type
)) == INTEGER_CST
1445 && (! operand_equal_p (TYPE_SIZE (field_type
), size
, 0)
1447 && ! value_zerop (size_binop (TRUNC_MOD_EXPR
, pos
,
1448 bitsize_int (TYPE_ALIGN
1451 || (TYPE_ALIGN (record_type
) != 0
1452 && TYPE_ALIGN (record_type
) < TYPE_ALIGN (field_type
))))
1454 DECL_BIT_FIELD (field_decl
) = 1;
1455 DECL_SIZE (field_decl
) = size
;
1456 if (! packed
&& pos
== 0)
1457 DECL_ALIGN (field_decl
)
1458 = (TYPE_ALIGN (record_type
) != 0
1459 ? MIN (TYPE_ALIGN (record_type
), TYPE_ALIGN (field_type
))
1460 : TYPE_ALIGN (field_type
));
1463 DECL_PACKED (field_decl
) = pos
!= 0 ? DECL_BIT_FIELD (field_decl
) : packed
;
1464 DECL_ALIGN (field_decl
)
1465 = MAX (DECL_ALIGN (field_decl
),
1466 DECL_BIT_FIELD (field_decl
) ? 1
1467 : packed
&& TYPE_MODE (field_type
) != BLKmode
? BITS_PER_UNIT
1468 : TYPE_ALIGN (field_type
));
1472 /* We need to pass in the alignment the DECL is known to have.
1473 This is the lowest-order bit set in POS, but no more than
1474 the alignment of the record, if one is specified. Note
1475 that an alignment of 0 is taken as infinite. */
1476 unsigned int known_align
;
1478 if (host_integerp (pos
, 1))
1479 known_align
= tree_low_cst (pos
, 1) & - tree_low_cst (pos
, 1);
1481 known_align
= BITS_PER_UNIT
;
1483 if (TYPE_ALIGN (record_type
)
1484 && (known_align
== 0 || known_align
> TYPE_ALIGN (record_type
)))
1485 known_align
= TYPE_ALIGN (record_type
);
1487 layout_decl (field_decl
, known_align
);
1488 SET_DECL_OFFSET_ALIGN (field_decl
,
1489 host_integerp (pos
, 1) ? BIGGEST_ALIGNMENT
1491 pos_from_bit (&DECL_FIELD_OFFSET (field_decl
),
1492 &DECL_FIELD_BIT_OFFSET (field_decl
),
1493 DECL_OFFSET_ALIGN (field_decl
), pos
);
1495 DECL_HAS_REP_P (field_decl
) = 1;
1498 /* If the field type is passed by reference, we will have pointers to the
1499 field, so it is addressable. */
1500 if (must_pass_by_ref (field_type
) || default_pass_by_ref (field_type
))
1503 /* ??? For now, we say that any field of aggregate type is addressable
1504 because the front end may take 'Reference of it. */
1505 if (AGGREGATE_TYPE_P (field_type
))
1508 /* Mark the decl as nonaddressable if it is indicated so semantically,
1509 meaning we won't ever attempt to take the address of the field.
1511 It may also be "technically" nonaddressable, meaning that even if we
1512 attempt to take the field's address we will actually get the address of a
1513 copy. This is the case for true bitfields, but the DECL_BIT_FIELD value
1514 we have at this point is not accurate enough, so we don't account for
1515 this here and let finish_record_type decide. */
1516 DECL_NONADDRESSABLE_P (field_decl
) = ! addressable
;
1521 /* Subroutine of previous function: return nonzero if EXP, ignoring any side
1522 effects, has the value of zero. */
1525 value_zerop (tree exp
)
1527 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1528 return value_zerop (TREE_OPERAND (exp
, 1));
1530 return integer_zerop (exp
);
1533 /* Returns a PARM_DECL node. PARAM_NAME is the name of the parameter,
1534 PARAM_TYPE is its type. READONLY is nonzero if the parameter is
1535 readonly (either an IN parameter or an address of a pass-by-ref
1539 create_param_decl (tree param_name
, tree param_type
, int readonly
)
1541 tree param_decl
= build_decl (PARM_DECL
, param_name
, param_type
);
1543 /* Honor the PROMOTE_PROTOTYPES target macro, as not doing so can
1544 lead to various ABI violations. */
1545 #ifdef PROMOTE_PROTOTYPES
1546 if ((TREE_CODE (param_type
) == INTEGER_TYPE
1547 || TREE_CODE (param_type
) == ENUMERAL_TYPE
)
1548 && TYPE_PRECISION (param_type
) < TYPE_PRECISION (integer_type_node
))
1550 /* We have to be careful about biased types here. Make a subtype
1551 of integer_type_node with the proper biasing. */
1552 if (TREE_CODE (param_type
) == INTEGER_TYPE
1553 && TYPE_BIASED_REPRESENTATION_P (param_type
))
1556 = copy_type (build_range_type (integer_type_node
,
1557 TYPE_MIN_VALUE (param_type
),
1558 TYPE_MAX_VALUE (param_type
)));
1560 TYPE_BIASED_REPRESENTATION_P (param_type
) = 1;
1563 param_type
= integer_type_node
;
1567 DECL_ARG_TYPE (param_decl
) = param_type
;
1568 DECL_ARG_TYPE_AS_WRITTEN (param_decl
) = param_type
;
1569 TREE_READONLY (param_decl
) = readonly
;
1573 /* Given a DECL and ATTR_LIST, process the listed attributes. */
1576 process_attributes (tree decl
, struct attrib
*attr_list
)
1578 for (; attr_list
; attr_list
= attr_list
->next
)
1579 switch (attr_list
->type
)
1581 case ATTR_MACHINE_ATTRIBUTE
:
1582 decl_attributes (&decl
, tree_cons (attr_list
->name
, attr_list
->arg
,
1584 ATTR_FLAG_TYPE_IN_PLACE
);
1587 case ATTR_LINK_ALIAS
:
1588 TREE_STATIC (decl
) = 1;
1589 assemble_alias (decl
, attr_list
->name
);
1592 case ATTR_WEAK_EXTERNAL
:
1594 declare_weak (decl
);
1596 post_error ("?weak declarations not supported on this target",
1597 attr_list
->error_point
);
1600 case ATTR_LINK_SECTION
:
1601 if (targetm
.have_named_sections
)
1603 DECL_SECTION_NAME (decl
)
1604 = build_string (IDENTIFIER_LENGTH (attr_list
->name
),
1605 IDENTIFIER_POINTER (attr_list
->name
));
1606 DECL_COMMON (decl
) = 0;
1609 post_error ("?section attributes are not supported for this target",
1610 attr_list
->error_point
);
1615 /* Add some pending elaborations on the list. */
1618 add_pending_elaborations (tree var_decl
, tree var_init
)
1621 Check_Elaboration_Code_Allowed (error_gnat_node
);
1623 pending_elaborations
1624 = chainon (pending_elaborations
, build_tree_list (var_decl
, var_init
));
1627 /* Obtain any pending elaborations and clear the old list. */
1630 get_pending_elaborations (void)
1632 /* Each thing added to the list went on the end; we want it on the
1634 tree result
= TREE_CHAIN (pending_elaborations
);
1636 TREE_CHAIN (pending_elaborations
) = 0;
1640 /* Return true if VALUE is a multiple of FACTOR. FACTOR must be a power
1644 value_factor_p (tree value
, int factor
)
1646 if (host_integerp (value
, 1))
1647 return tree_low_cst (value
, 1) % factor
== 0;
1649 if (TREE_CODE (value
) == MULT_EXPR
)
1650 return (value_factor_p (TREE_OPERAND (value
, 0), factor
)
1651 || value_factor_p (TREE_OPERAND (value
, 1), factor
));
1656 /* Given 2 consecutive field decls PREV_FIELD and CURR_FIELD, return true
1657 unless we can prove these 2 fields are laid out in such a way that no gap
1658 exist between the end of PREV_FIELD and the begining of CURR_FIELD. OFFSET
1659 is the distance in bits between the end of PREV_FIELD and the starting
1660 position of CURR_FIELD. It is ignored if null. */
1663 potential_alignment_gap (tree prev_field
, tree curr_field
, tree offset
)
1665 /* If this is the first field of the record, there cannot be any gap */
1669 /* If the previous field is a union type, then return False: The only
1670 time when such a field is not the last field of the record is when
1671 there are other components at fixed positions after it (meaning there
1672 was a rep clause for every field), in which case we don't want the
1673 alignment constraint to override them. */
1674 if (TREE_CODE (TREE_TYPE (prev_field
)) == QUAL_UNION_TYPE
)
1677 /* If the distance between the end of prev_field and the begining of
1678 curr_field is constant, then there is a gap if the value of this
1679 constant is not null. */
1680 if (offset
&& host_integerp (offset
, 1))
1681 return (!integer_zerop (offset
));
1683 /* If the size and position of the previous field are constant,
1684 then check the sum of this size and position. There will be a gap
1685 iff it is not multiple of the current field alignment. */
1686 if (host_integerp (DECL_SIZE (prev_field
), 1)
1687 && host_integerp (bit_position (prev_field
), 1))
1688 return ((tree_low_cst (bit_position (prev_field
), 1)
1689 + tree_low_cst (DECL_SIZE (prev_field
), 1))
1690 % DECL_ALIGN (curr_field
) != 0);
1692 /* If both the position and size of the previous field are multiples
1693 of the current field alignment, there can not be any gap. */
1694 if (value_factor_p (bit_position (prev_field
), DECL_ALIGN (curr_field
))
1695 && value_factor_p (DECL_SIZE (prev_field
), DECL_ALIGN (curr_field
)))
1698 /* Fallback, return that there may be a potential gap */
1702 /* Return nonzero if there are pending elaborations. */
1705 pending_elaborations_p (void)
1707 return TREE_CHAIN (pending_elaborations
) != 0;
1710 /* Save a copy of the current pending elaboration list and make a new
1714 push_pending_elaborations (void)
1716 struct e_stack
*p
= (struct e_stack
*) ggc_alloc (sizeof (struct e_stack
));
1718 p
->next
= elist_stack
;
1719 p
->elab_list
= pending_elaborations
;
1721 pending_elaborations
= build_tree_list (NULL_TREE
, NULL_TREE
);
1724 /* Pop the stack of pending elaborations. */
1727 pop_pending_elaborations (void)
1729 struct e_stack
*p
= elist_stack
;
1731 pending_elaborations
= p
->elab_list
;
1732 elist_stack
= p
->next
;
1735 /* Return the current position in pending_elaborations so we can insert
1736 elaborations after that point. */
1739 get_elaboration_location (void)
1741 return tree_last (pending_elaborations
);
1744 /* Insert the current elaborations after ELAB, which is in some elaboration
1748 insert_elaboration_list (tree elab
)
1750 tree next
= TREE_CHAIN (elab
);
1752 if (TREE_CHAIN (pending_elaborations
))
1754 TREE_CHAIN (elab
) = TREE_CHAIN (pending_elaborations
);
1755 TREE_CHAIN (tree_last (pending_elaborations
)) = next
;
1756 TREE_CHAIN (pending_elaborations
) = 0;
1760 /* Returns a LABEL_DECL node for LABEL_NAME. */
1763 create_label_decl (tree label_name
)
1765 tree label_decl
= build_decl (LABEL_DECL
, label_name
, void_type_node
);
1767 DECL_CONTEXT (label_decl
) = current_function_decl
;
1768 DECL_MODE (label_decl
) = VOIDmode
;
1769 DECL_SOURCE_LOCATION (label_decl
) = input_location
;
1774 /* Returns a FUNCTION_DECL node. SUBPROG_NAME is the name of the subprogram,
1775 ASM_NAME is its assembler name, SUBPROG_TYPE is its type (a FUNCTION_TYPE
1776 node), PARAM_DECL_LIST is the list of the subprogram arguments (a list of
1777 PARM_DECL nodes chained through the TREE_CHAIN field).
1779 INLINE_FLAG, PUBLIC_FLAG, EXTERN_FLAG, and ATTR_LIST are used to set the
1780 appropriate fields in the FUNCTION_DECL. */
1783 create_subprog_decl (tree subprog_name
,
1786 tree param_decl_list
,
1790 struct attrib
*attr_list
)
1792 tree return_type
= TREE_TYPE (subprog_type
);
1793 tree subprog_decl
= build_decl (FUNCTION_DECL
, subprog_name
, subprog_type
);
1795 /* If this is a function nested inside an inlined external function, it
1796 means we aren't going to compile the outer function unless it is
1797 actually inlined, so do the same for us. */
1798 if (current_function_decl
!= 0 && DECL_INLINE (current_function_decl
)
1799 && DECL_EXTERNAL (current_function_decl
))
1802 DECL_EXTERNAL (subprog_decl
) = extern_flag
;
1803 TREE_PUBLIC (subprog_decl
) = public_flag
;
1804 DECL_INLINE (subprog_decl
) = inline_flag
;
1805 TREE_READONLY (subprog_decl
) = TYPE_READONLY (subprog_type
);
1806 TREE_THIS_VOLATILE (subprog_decl
) = TYPE_VOLATILE (subprog_type
);
1807 TREE_SIDE_EFFECTS (subprog_decl
) = TYPE_VOLATILE (subprog_type
);
1808 DECL_ARGUMENTS (subprog_decl
) = param_decl_list
;
1809 DECL_RESULT (subprog_decl
) = build_decl (RESULT_DECL
, 0, return_type
);
1812 SET_DECL_ASSEMBLER_NAME (subprog_decl
, asm_name
);
1814 process_attributes (subprog_decl
, attr_list
);
1816 /* Add this decl to the current binding level. */
1817 subprog_decl
= pushdecl (subprog_decl
);
1819 /* Output the assembler code and/or RTL for the declaration. */
1820 rest_of_decl_compilation (subprog_decl
, 0, global_bindings_p (), 0);
1822 return subprog_decl
;
1825 /* Count how deep we are into nested functions. This is because
1826 we shouldn't call the backend function context routines unless we
1827 are in a nested function. */
1829 static int function_nesting_depth
;
1831 /* Set up the framework for generating code for SUBPROG_DECL, a subprogram
1832 body. This routine needs to be invoked before processing the declarations
1833 appearing in the subprogram. */
1836 begin_subprog_body (tree subprog_decl
)
1838 tree param_decl_list
;
1842 if (function_nesting_depth
++ != 0)
1843 push_function_context ();
1845 announce_function (subprog_decl
);
1847 /* Make this field nonzero so further routines know that this is not
1848 tentative. error_mark_node is replaced below (in poplevel) with the
1850 DECL_INITIAL (subprog_decl
) = error_mark_node
;
1852 /* This function exists in static storage. This does not mean `static' in
1854 TREE_STATIC (subprog_decl
) = 1;
1856 /* Enter a new binding level. */
1857 current_function_decl
= subprog_decl
;
1860 /* Push all the PARM_DECL nodes onto the current scope (i.e. the scope of the
1861 subprogram body) so that they can be recognized as local variables in the
1864 The list of PARM_DECL nodes is stored in the right order in
1865 DECL_ARGUMENTS. Since ..._DECL nodes get stored in the reverse order in
1866 which they are transmitted to `pushdecl' we need to reverse the list of
1867 PARM_DECLs if we want it to be stored in the right order. The reason why
1868 we want to make sure the PARM_DECLs are stored in the correct order is
1869 that this list will be retrieved in a few lines with a call to `getdecl'
1870 to store it back into the DECL_ARGUMENTS field. */
1871 param_decl_list
= nreverse (DECL_ARGUMENTS (subprog_decl
));
1873 for (param_decl
= param_decl_list
; param_decl
; param_decl
= next_param
)
1875 next_param
= TREE_CHAIN (param_decl
);
1876 TREE_CHAIN (param_decl
) = NULL
;
1877 pushdecl (param_decl
);
1880 /* Store back the PARM_DECL nodes. They appear in the right order. */
1881 DECL_ARGUMENTS (subprog_decl
) = getdecls ();
1883 init_function_start (subprog_decl
);
1884 expand_function_start (subprog_decl
, 0);
1886 /* If this function is `main', emit a call to `__main'
1887 to run global initializers, etc. */
1888 if (DECL_ASSEMBLER_NAME (subprog_decl
) != 0
1889 && MAIN_NAME_P (DECL_ASSEMBLER_NAME (subprog_decl
))
1890 && DECL_CONTEXT (subprog_decl
) == NULL_TREE
)
1891 expand_main_function ();
1894 /* Finish the definition of the current subprogram and compile it all the way
1895 to assembler language output. */
1898 end_subprog_body (void)
1904 BLOCK_SUPERCONTEXT (DECL_INITIAL (current_function_decl
))
1905 = current_function_decl
;
1907 /* Mark the RESULT_DECL as being in this subprogram. */
1908 DECL_CONTEXT (DECL_RESULT (current_function_decl
)) = current_function_decl
;
1910 expand_function_end ();
1912 /* If this is a nested function, push a new GC context. That will keep
1913 local variables on the stack from being collected while we're doing
1914 the compilation of this function. */
1915 if (function_nesting_depth
> 1)
1916 ggc_push_context ();
1918 /* If we're only annotating types, don't actually compile this
1920 if (!type_annotate_only
)
1921 rest_of_compilation (current_function_decl
);
1923 if (function_nesting_depth
> 1)
1926 /* Throw away any VAR_DECLs we made for OUT parameters; they must
1927 not be seen when we call this function and will be in
1928 unallocated memory anyway. */
1929 for (cico_list
= TYPE_CI_CO_LIST (TREE_TYPE (current_function_decl
));
1930 cico_list
!= 0; cico_list
= TREE_CHAIN (cico_list
))
1931 TREE_VALUE (cico_list
) = 0;
1933 if (DECL_SAVED_INSNS (current_function_decl
) == 0)
1935 /* Throw away DECL_RTL in any PARM_DECLs unless this function
1936 was saved for inline, in which case the DECL_RTLs are in
1937 preserved memory. */
1938 for (decl
= DECL_ARGUMENTS (current_function_decl
);
1939 decl
!= 0; decl
= TREE_CHAIN (decl
))
1941 SET_DECL_RTL (decl
, 0);
1942 DECL_INCOMING_RTL (decl
) = 0;
1945 /* Similarly, discard DECL_RTL of the return value. */
1946 SET_DECL_RTL (DECL_RESULT (current_function_decl
), 0);
1948 /* But DECL_INITIAL must remain nonzero so we know this
1949 was an actual function definition unless toplev.c decided not
1951 if (DECL_INITIAL (current_function_decl
) != 0)
1952 DECL_INITIAL (current_function_decl
) = error_mark_node
;
1954 DECL_ARGUMENTS (current_function_decl
) = 0;
1957 /* If we are not at the bottom of the function nesting stack, pop up to
1958 the containing function. Otherwise show we aren't in any function. */
1959 if (--function_nesting_depth
!= 0)
1960 pop_function_context ();
1962 current_function_decl
= 0;
1965 /* Return a definition for a builtin function named NAME and whose data type
1966 is TYPE. TYPE should be a function type with argument types.
1967 FUNCTION_CODE tells later passes how to compile calls to this function.
1968 See tree.h for its possible values.
1970 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
1971 the name to be called if we can't opencode the function. If
1972 ATTRS is nonzero, use that for the function attribute list. */
1975 builtin_function (const char *name
,
1978 enum built_in_class
class,
1979 const char *library_name
,
1982 tree decl
= build_decl (FUNCTION_DECL
, get_identifier (name
), type
);
1984 DECL_EXTERNAL (decl
) = 1;
1985 TREE_PUBLIC (decl
) = 1;
1987 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (library_name
));
1990 DECL_BUILT_IN_CLASS (decl
) = class;
1991 DECL_FUNCTION_CODE (decl
) = function_code
;
1993 decl_attributes (&decl
, attrs
, ATTR_FLAG_BUILT_IN
);
1997 /* Return an integer type with the number of bits of precision given by
1998 PRECISION. UNSIGNEDP is nonzero if the type is unsigned; otherwise
1999 it is a signed type. */
2002 gnat_type_for_size (unsigned precision
, int unsignedp
)
2007 if (precision
<= 2 * MAX_BITS_PER_WORD
2008 && signed_and_unsigned_types
[precision
][unsignedp
] != 0)
2009 return signed_and_unsigned_types
[precision
][unsignedp
];
2012 t
= make_unsigned_type (precision
);
2014 t
= make_signed_type (precision
);
2016 if (precision
<= 2 * MAX_BITS_PER_WORD
)
2017 signed_and_unsigned_types
[precision
][unsignedp
] = t
;
2019 if (TYPE_NAME (t
) == 0)
2021 sprintf (type_name
, "%sSIGNED_%d", unsignedp
? "UN" : "", precision
);
2022 TYPE_NAME (t
) = get_identifier (type_name
);
2028 /* Likewise for floating-point types. */
2031 float_type_for_precision (int precision
, enum machine_mode mode
)
2036 if (float_types
[(int) mode
] != 0)
2037 return float_types
[(int) mode
];
2039 float_types
[(int) mode
] = t
= make_node (REAL_TYPE
);
2040 TYPE_PRECISION (t
) = precision
;
2043 if (TYPE_MODE (t
) != mode
)
2046 if (TYPE_NAME (t
) == 0)
2048 sprintf (type_name
, "FLOAT_%d", precision
);
2049 TYPE_NAME (t
) = get_identifier (type_name
);
2055 /* Return a data type that has machine mode MODE. UNSIGNEDP selects
2056 an unsigned type; otherwise a signed type is returned. */
2059 gnat_type_for_mode (enum machine_mode mode
, int unsignedp
)
2061 if (GET_MODE_CLASS (mode
) == MODE_FLOAT
)
2062 return float_type_for_precision (GET_MODE_PRECISION (mode
), mode
);
2064 return gnat_type_for_size (GET_MODE_BITSIZE (mode
), unsignedp
);
2067 /* Return the unsigned version of a TYPE_NODE, a scalar type. */
2070 gnat_unsigned_type (tree type_node
)
2072 tree type
= gnat_type_for_size (TYPE_PRECISION (type_node
), 1);
2074 if (TREE_CODE (type_node
) == INTEGER_TYPE
&& TYPE_MODULAR_P (type_node
))
2076 type
= copy_node (type
);
2077 TREE_TYPE (type
) = type_node
;
2079 else if (TREE_TYPE (type_node
) != 0
2080 && TREE_CODE (TREE_TYPE (type_node
)) == INTEGER_TYPE
2081 && TYPE_MODULAR_P (TREE_TYPE (type_node
)))
2083 type
= copy_node (type
);
2084 TREE_TYPE (type
) = TREE_TYPE (type_node
);
2090 /* Return the signed version of a TYPE_NODE, a scalar type. */
2093 gnat_signed_type (tree type_node
)
2095 tree type
= gnat_type_for_size (TYPE_PRECISION (type_node
), 0);
2097 if (TREE_CODE (type_node
) == INTEGER_TYPE
&& TYPE_MODULAR_P (type_node
))
2099 type
= copy_node (type
);
2100 TREE_TYPE (type
) = type_node
;
2102 else if (TREE_TYPE (type_node
) != 0
2103 && TREE_CODE (TREE_TYPE (type_node
)) == INTEGER_TYPE
2104 && TYPE_MODULAR_P (TREE_TYPE (type_node
)))
2106 type
= copy_node (type
);
2107 TREE_TYPE (type
) = TREE_TYPE (type_node
);
2113 /* Return a type the same as TYPE except unsigned or signed according to
2117 gnat_signed_or_unsigned_type (int unsignedp
, tree type
)
2119 if (! INTEGRAL_TYPE_P (type
) || TREE_UNSIGNED (type
) == unsignedp
)
2122 return gnat_type_for_size (TYPE_PRECISION (type
), unsignedp
);
2125 /* EXP is an expression for the size of an object. If this size contains
2126 discriminant references, replace them with the maximum (if MAX_P) or
2127 minimum (if ! MAX_P) possible value of the discriminant. */
2130 max_size (tree exp
, int max_p
)
2132 enum tree_code code
= TREE_CODE (exp
);
2133 tree type
= TREE_TYPE (exp
);
2135 switch (TREE_CODE_CLASS (code
))
2142 if (code
== TREE_LIST
)
2143 return tree_cons (TREE_PURPOSE (exp
),
2144 max_size (TREE_VALUE (exp
), max_p
),
2145 TREE_CHAIN (exp
) != 0
2146 ? max_size (TREE_CHAIN (exp
), max_p
) : 0);
2150 /* If this contains a PLACEHOLDER_EXPR, it is the thing we want to
2151 modify. Otherwise, we treat it like a variable. */
2152 if (! CONTAINS_PLACEHOLDER_P (exp
))
2155 type
= TREE_TYPE (TREE_OPERAND (exp
, 1));
2157 max_size (max_p
? TYPE_MAX_VALUE (type
) : TYPE_MIN_VALUE (type
), 1);
2160 return max_p
? size_one_node
: size_zero_node
;
2165 switch (TREE_CODE_LENGTH (code
))
2168 if (code
== NON_LVALUE_EXPR
)
2169 return max_size (TREE_OPERAND (exp
, 0), max_p
);
2172 fold (build1 (code
, type
,
2173 max_size (TREE_OPERAND (exp
, 0),
2174 code
== NEGATE_EXPR
? ! max_p
: max_p
)));
2177 if (code
== RTL_EXPR
)
2179 else if (code
== COMPOUND_EXPR
)
2180 return max_size (TREE_OPERAND (exp
, 1), max_p
);
2181 else if (code
== WITH_RECORD_EXPR
)
2185 tree lhs
= max_size (TREE_OPERAND (exp
, 0), max_p
);
2186 tree rhs
= max_size (TREE_OPERAND (exp
, 1),
2187 code
== MINUS_EXPR
? ! max_p
: max_p
);
2189 /* Special-case wanting the maximum value of a MIN_EXPR.
2190 In that case, if one side overflows, return the other.
2191 sizetype is signed, but we know sizes are non-negative.
2192 Likewise, handle a MINUS_EXPR or PLUS_EXPR with the LHS
2193 overflowing or the maximum possible value and the RHS
2195 if (max_p
&& code
== MIN_EXPR
&& TREE_OVERFLOW (rhs
))
2197 else if (max_p
&& code
== MIN_EXPR
&& TREE_OVERFLOW (lhs
))
2199 else if ((code
== MINUS_EXPR
|| code
== PLUS_EXPR
)
2200 && ((TREE_CONSTANT (lhs
) && TREE_OVERFLOW (lhs
))
2201 || operand_equal_p (lhs
, TYPE_MAX_VALUE (type
), 0))
2202 && ! TREE_CONSTANT (rhs
))
2205 return fold (build (code
, type
, lhs
, rhs
));
2209 if (code
== SAVE_EXPR
)
2211 else if (code
== COND_EXPR
)
2212 return fold (build (MAX_EXPR
, type
,
2213 max_size (TREE_OPERAND (exp
, 1), max_p
),
2214 max_size (TREE_OPERAND (exp
, 2), max_p
)));
2215 else if (code
== CALL_EXPR
&& TREE_OPERAND (exp
, 1) != 0)
2216 return build (CALL_EXPR
, type
, TREE_OPERAND (exp
, 0),
2217 max_size (TREE_OPERAND (exp
, 1), max_p
));
2224 /* Build a template of type TEMPLATE_TYPE from the array bounds of ARRAY_TYPE.
2225 EXPR is an expression that we can use to locate any PLACEHOLDER_EXPRs.
2226 Return a constructor for the template. */
2229 build_template (tree template_type
, tree array_type
, tree expr
)
2231 tree template_elts
= NULL_TREE
;
2232 tree bound_list
= NULL_TREE
;
2235 if (TREE_CODE (array_type
) == RECORD_TYPE
2236 && (TYPE_IS_PADDING_P (array_type
)
2237 || TYPE_LEFT_JUSTIFIED_MODULAR_P (array_type
)))
2238 array_type
= TREE_TYPE (TYPE_FIELDS (array_type
));
2240 if (TREE_CODE (array_type
) == ARRAY_TYPE
2241 || (TREE_CODE (array_type
) == INTEGER_TYPE
2242 && TYPE_HAS_ACTUAL_BOUNDS_P (array_type
)))
2243 bound_list
= TYPE_ACTUAL_BOUNDS (array_type
);
2245 /* First make the list for a CONSTRUCTOR for the template. Go down the
2246 field list of the template instead of the type chain because this
2247 array might be an Ada array of arrays and we can't tell where the
2248 nested arrays stop being the underlying object. */
2250 for (field
= TYPE_FIELDS (template_type
); field
;
2252 ? (bound_list
= TREE_CHAIN (bound_list
))
2253 : (array_type
= TREE_TYPE (array_type
))),
2254 field
= TREE_CHAIN (TREE_CHAIN (field
)))
2256 tree bounds
, min
, max
;
2258 /* If we have a bound list, get the bounds from there. Likewise
2259 for an ARRAY_TYPE. Otherwise, if expr is a PARM_DECL with
2260 DECL_BY_COMPONENT_PTR_P, use the bounds of the field in the template.
2261 This will give us a maximum range. */
2262 if (bound_list
!= 0)
2263 bounds
= TREE_VALUE (bound_list
);
2264 else if (TREE_CODE (array_type
) == ARRAY_TYPE
)
2265 bounds
= TYPE_INDEX_TYPE (TYPE_DOMAIN (array_type
));
2266 else if (expr
!= 0 && TREE_CODE (expr
) == PARM_DECL
2267 && DECL_BY_COMPONENT_PTR_P (expr
))
2268 bounds
= TREE_TYPE (field
);
2272 min
= convert (TREE_TYPE (TREE_CHAIN (field
)), TYPE_MIN_VALUE (bounds
));
2273 max
= convert (TREE_TYPE (field
), TYPE_MAX_VALUE (bounds
));
2275 /* If either MIN or MAX involve a PLACEHOLDER_EXPR, we must
2276 surround them with a WITH_RECORD_EXPR giving EXPR as the
2278 if (CONTAINS_PLACEHOLDER_P (min
))
2279 min
= build (WITH_RECORD_EXPR
, TREE_TYPE (min
), min
, expr
);
2280 if (CONTAINS_PLACEHOLDER_P (max
))
2281 max
= build (WITH_RECORD_EXPR
, TREE_TYPE (max
), max
, expr
);
2283 template_elts
= tree_cons (TREE_CHAIN (field
), max
,
2284 tree_cons (field
, min
, template_elts
));
2287 return gnat_build_constructor (template_type
, nreverse (template_elts
));
2290 /* Build a VMS descriptor from a Mechanism_Type, which must specify
2291 a descriptor type, and the GCC type of an object. Each FIELD_DECL
2292 in the type contains in its DECL_INITIAL the expression to use when
2293 a constructor is made for the type. GNAT_ENTITY is a gnat node used
2294 to print out an error message if the mechanism cannot be applied to
2295 an object of that type and also for the name. */
2298 build_vms_descriptor (tree type
, Mechanism_Type mech
, Entity_Id gnat_entity
)
2300 tree record_type
= make_node (RECORD_TYPE
);
2301 tree field_list
= 0;
2310 /* If TYPE is an unconstrained array, use the underlying array type. */
2311 if (TREE_CODE (type
) == UNCONSTRAINED_ARRAY_TYPE
)
2312 type
= TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (type
))));
2314 /* If this is an array, compute the number of dimensions in the array,
2315 get the index types, and point to the inner type. */
2316 if (TREE_CODE (type
) != ARRAY_TYPE
)
2319 for (ndim
= 1, inner_type
= type
;
2320 TREE_CODE (TREE_TYPE (inner_type
)) == ARRAY_TYPE
2321 && TYPE_MULTI_ARRAY_P (TREE_TYPE (inner_type
));
2322 ndim
++, inner_type
= TREE_TYPE (inner_type
))
2325 idx_arr
= (tree
*) alloca (ndim
* sizeof (tree
));
2327 if (mech
!= By_Descriptor_NCA
2328 && TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_CONVENTION_FORTRAN_P (type
))
2329 for (i
= ndim
- 1, inner_type
= type
;
2331 i
--, inner_type
= TREE_TYPE (inner_type
))
2332 idx_arr
[i
] = TYPE_DOMAIN (inner_type
);
2334 for (i
= 0, inner_type
= type
;
2336 i
++, inner_type
= TREE_TYPE (inner_type
))
2337 idx_arr
[i
] = TYPE_DOMAIN (inner_type
);
2339 /* Now get the DTYPE value. */
2340 switch (TREE_CODE (type
))
2344 if (TYPE_VAX_FLOATING_POINT_P (type
))
2345 switch (tree_low_cst (TYPE_DIGITS_VALUE (type
), 1))
2358 switch (GET_MODE_BITSIZE (TYPE_MODE (type
)))
2361 dtype
= TREE_UNSIGNED (type
) ? 2 : 6;
2364 dtype
= TREE_UNSIGNED (type
) ? 3 : 7;
2367 dtype
= TREE_UNSIGNED (type
) ? 4 : 8;
2370 dtype
= TREE_UNSIGNED (type
) ? 5 : 9;
2373 dtype
= TREE_UNSIGNED (type
) ? 25 : 26;
2379 dtype
= GET_MODE_BITSIZE (TYPE_MODE (type
)) == 32 ? 52 : 53;
2383 if (TREE_CODE (TREE_TYPE (type
)) == INTEGER_TYPE
2384 && TYPE_VAX_FLOATING_POINT_P (type
))
2385 switch (tree_low_cst (TYPE_DIGITS_VALUE (type
), 1))
2397 dtype
= GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type
))) == 32 ? 54: 55;
2408 /* Get the CLASS value. */
2411 case By_Descriptor_A
:
2414 case By_Descriptor_NCA
:
2417 case By_Descriptor_SB
:
2424 /* Make the type for a descriptor for VMS. The first four fields
2425 are the same for all types. */
2428 = chainon (field_list
,
2429 make_descriptor_field
2430 ("LENGTH", gnat_type_for_size (16, 1), record_type
,
2431 size_in_bytes (mech
== By_Descriptor_A
? inner_type
: type
)));
2433 field_list
= chainon (field_list
,
2434 make_descriptor_field ("DTYPE",
2435 gnat_type_for_size (8, 1),
2436 record_type
, size_int (dtype
)));
2437 field_list
= chainon (field_list
,
2438 make_descriptor_field ("CLASS",
2439 gnat_type_for_size (8, 1),
2440 record_type
, size_int (class)));
2443 = chainon (field_list
,
2444 make_descriptor_field ("POINTER",
2445 build_pointer_type (type
),
2448 build_pointer_type (type
),
2449 build (PLACEHOLDER_EXPR
,
2455 case By_Descriptor_S
:
2458 case By_Descriptor_SB
:
2460 = chainon (field_list
,
2461 make_descriptor_field
2462 ("SB_L1", gnat_type_for_size (32, 1), record_type
,
2463 TREE_CODE (type
) == ARRAY_TYPE
2464 ? TYPE_MIN_VALUE (TYPE_DOMAIN (type
)) : size_zero_node
));
2466 = chainon (field_list
,
2467 make_descriptor_field
2468 ("SB_L2", gnat_type_for_size (32, 1), record_type
,
2469 TREE_CODE (type
) == ARRAY_TYPE
2470 ? TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) : size_zero_node
));
2473 case By_Descriptor_A
:
2474 case By_Descriptor_NCA
:
2475 field_list
= chainon (field_list
,
2476 make_descriptor_field ("SCALE",
2477 gnat_type_for_size (8, 1),
2481 field_list
= chainon (field_list
,
2482 make_descriptor_field ("DIGITS",
2483 gnat_type_for_size (8, 1),
2488 = chainon (field_list
,
2489 make_descriptor_field
2490 ("AFLAGS", gnat_type_for_size (8, 1), record_type
,
2491 size_int (mech
== By_Descriptor_NCA
2493 /* Set FL_COLUMN, FL_COEFF, and FL_BOUNDS. */
2494 : (TREE_CODE (type
) == ARRAY_TYPE
2495 && TYPE_CONVENTION_FORTRAN_P (type
)
2498 field_list
= chainon (field_list
,
2499 make_descriptor_field ("DIMCT",
2500 gnat_type_for_size (8, 1),
2504 field_list
= chainon (field_list
,
2505 make_descriptor_field ("ARSIZE",
2506 gnat_type_for_size (32, 1),
2508 size_in_bytes (type
)));
2510 /* Now build a pointer to the 0,0,0... element. */
2511 tem
= build (PLACEHOLDER_EXPR
, type
);
2512 for (i
= 0, inner_type
= type
; i
< ndim
;
2513 i
++, inner_type
= TREE_TYPE (inner_type
))
2514 tem
= build (ARRAY_REF
, TREE_TYPE (inner_type
), tem
,
2515 convert (TYPE_DOMAIN (inner_type
), size_zero_node
));
2518 = chainon (field_list
,
2519 make_descriptor_field
2520 ("A0", build_pointer_type (inner_type
), record_type
,
2521 build1 (ADDR_EXPR
, build_pointer_type (inner_type
), tem
)));
2523 /* Next come the addressing coefficients. */
2525 for (i
= 0; i
< ndim
; i
++)
2529 = size_binop (MULT_EXPR
, tem
,
2530 size_binop (PLUS_EXPR
,
2531 size_binop (MINUS_EXPR
,
2532 TYPE_MAX_VALUE (idx_arr
[i
]),
2533 TYPE_MIN_VALUE (idx_arr
[i
])),
2536 fname
[0] = (mech
== By_Descriptor_NCA
? 'S' : 'M');
2537 fname
[1] = '0' + i
, fname
[2] = 0;
2539 = chainon (field_list
,
2540 make_descriptor_field (fname
,
2541 gnat_type_for_size (32, 1),
2542 record_type
, idx_length
));
2544 if (mech
== By_Descriptor_NCA
)
2548 /* Finally here are the bounds. */
2549 for (i
= 0; i
< ndim
; i
++)
2553 fname
[0] = 'L', fname
[1] = '0' + i
, fname
[2] = 0;
2555 = chainon (field_list
,
2556 make_descriptor_field
2557 (fname
, gnat_type_for_size (32, 1), record_type
,
2558 TYPE_MIN_VALUE (idx_arr
[i
])));
2562 = chainon (field_list
,
2563 make_descriptor_field
2564 (fname
, gnat_type_for_size (32, 1), record_type
,
2565 TYPE_MAX_VALUE (idx_arr
[i
])));
2570 post_error ("unsupported descriptor type for &", gnat_entity
);
2573 finish_record_type (record_type
, field_list
, 0, 1);
2574 pushdecl (build_decl (TYPE_DECL
, create_concat_name (gnat_entity
, "DESC"),
2580 /* Utility routine for above code to make a field. */
2583 make_descriptor_field (const char *name
, tree type
,
2584 tree rec_type
, tree initial
)
2587 = create_field_decl (get_identifier (name
), type
, rec_type
, 0, 0, 0, 0);
2589 DECL_INITIAL (field
) = initial
;
2593 /* Build a type to be used to represent an aliased object whose nominal
2594 type is an unconstrained array. This consists of a RECORD_TYPE containing
2595 a field of TEMPLATE_TYPE and a field of OBJECT_TYPE, which is an
2596 ARRAY_TYPE. If ARRAY_TYPE is that of the unconstrained array, this
2597 is used to represent an arbitrary unconstrained object. Use NAME
2598 as the name of the record. */
2601 build_unc_object_type (tree template_type
, tree object_type
, tree name
)
2603 tree type
= make_node (RECORD_TYPE
);
2604 tree template_field
= create_field_decl (get_identifier ("BOUNDS"),
2605 template_type
, type
, 0, 0, 0, 1);
2606 tree array_field
= create_field_decl (get_identifier ("ARRAY"), object_type
,
2609 TYPE_NAME (type
) = name
;
2610 TYPE_CONTAINS_TEMPLATE_P (type
) = 1;
2611 finish_record_type (type
,
2612 chainon (chainon (NULL_TREE
, template_field
),
2619 /* Update anything previously pointing to OLD_TYPE to point to NEW_TYPE. In
2620 the normal case this is just two adjustments, but we have more to do
2621 if NEW is an UNCONSTRAINED_ARRAY_TYPE. */
2624 update_pointer_to (tree old_type
, tree new_type
)
2626 tree ptr
= TYPE_POINTER_TO (old_type
);
2627 tree ref
= TYPE_REFERENCE_TO (old_type
);
2630 /* If this is the main variant, process all the other variants first. */
2631 if (TYPE_MAIN_VARIANT (old_type
) == old_type
)
2632 for (type
= TYPE_NEXT_VARIANT (old_type
); type
!= 0;
2633 type
= TYPE_NEXT_VARIANT (type
))
2634 update_pointer_to (type
, new_type
);
2636 /* If no pointer or reference, we are done. */
2637 if (ptr
== 0 && ref
== 0)
2640 /* Merge the old type qualifiers in the new type.
2642 Each old variant has qualifiers for specific reasons, and the new
2643 designated type as well. Each set of qualifiers represents useful
2644 information grabbed at some point, and merging the two simply unifies
2645 these inputs into the final type description.
2647 Consider for instance a volatile type frozen after an access to constant
2648 type designating it. After the designated type freeze, we get here with a
2649 volatile new_type and a dummy old_type with a readonly variant, created
2650 when the access type was processed. We shall make a volatile and readonly
2651 designated type, because that's what it really is.
2653 We might also get here for a non-dummy old_type variant with different
2654 qualifiers than the new_type ones, for instance in some cases of pointers
2655 to private record type elaboration (see the comments around the call to
2656 this routine from gnat_to_gnu_entity/E_Access_Type). We have to merge the
2657 qualifiers in thoses cases too, to avoid accidentally discarding the
2658 initial set, and will often end up with old_type == new_type then. */
2659 new_type
= build_qualified_type (new_type
,
2660 TYPE_QUALS (old_type
)
2661 | TYPE_QUALS (new_type
));
2663 /* If the new type and the old one are identical, there is nothing to
2665 if (old_type
== new_type
)
2668 /* Otherwise, first handle the simple case. */
2669 if (TREE_CODE (new_type
) != UNCONSTRAINED_ARRAY_TYPE
)
2672 TREE_TYPE (ptr
) = new_type
;
2673 TYPE_POINTER_TO (new_type
) = ptr
;
2676 TREE_TYPE (ref
) = new_type
;
2677 TYPE_REFERENCE_TO (new_type
) = ref
;
2679 if (ptr
!= 0 && TYPE_NAME (ptr
) != 0
2680 && TREE_CODE (TYPE_NAME (ptr
)) == TYPE_DECL
2681 && TREE_CODE (new_type
) != ENUMERAL_TYPE
)
2682 rest_of_decl_compilation (TYPE_NAME (ptr
), NULL
,
2683 global_bindings_p (), 0);
2684 if (ref
!= 0 && TYPE_NAME (ref
) != 0
2685 && TREE_CODE (TYPE_NAME (ref
)) == TYPE_DECL
2686 && TREE_CODE (new_type
) != ENUMERAL_TYPE
)
2687 rest_of_decl_compilation (TYPE_NAME (ref
), NULL
,
2688 global_bindings_p (), 0);
2691 /* Now deal with the unconstrained array case. In this case the "pointer"
2692 is actually a RECORD_TYPE where the types of both fields are
2693 pointers to void. In that case, copy the field list from the
2694 old type to the new one and update the fields' context. */
2695 else if (TREE_CODE (ptr
) != RECORD_TYPE
|| ! TYPE_IS_FAT_POINTER_P (ptr
))
2700 tree new_obj_rec
= TYPE_OBJECT_RECORD_TYPE (new_type
);
2705 TYPE_FIELDS (ptr
) = TYPE_FIELDS (TYPE_POINTER_TO (new_type
));
2706 DECL_CONTEXT (TYPE_FIELDS (ptr
)) = ptr
;
2707 DECL_CONTEXT (TREE_CHAIN (TYPE_FIELDS (ptr
))) = ptr
;
2709 /* Rework the PLACEHOLDER_EXPR inside the reference to the
2712 ??? This is now the only use of gnat_substitute_in_type, which
2713 is now a very "heavy" routine to do this, so it should be replaced
2715 ptr_temp_type
= TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (ptr
)));
2716 new_ref
= build (COMPONENT_REF
, ptr_temp_type
,
2717 build (PLACEHOLDER_EXPR
, ptr
),
2718 TREE_CHAIN (TYPE_FIELDS (ptr
)));
2721 (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (ptr
))),
2722 gnat_substitute_in_type (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (ptr
))),
2723 TREE_CHAIN (TYPE_FIELDS (ptr
)), new_ref
));
2725 for (var
= TYPE_MAIN_VARIANT (ptr
); var
; var
= TYPE_NEXT_VARIANT (var
))
2726 SET_TYPE_UNCONSTRAINED_ARRAY (var
, new_type
);
2728 TYPE_POINTER_TO (new_type
) = TYPE_REFERENCE_TO (new_type
)
2729 = TREE_TYPE (new_type
) = ptr
;
2731 /* Now handle updating the allocation record, what the thin pointer
2732 points to. Update all pointers from the old record into the new
2733 one, update the types of the fields, and recompute the size. */
2735 update_pointer_to (TYPE_OBJECT_RECORD_TYPE (old_type
), new_obj_rec
);
2737 TREE_TYPE (TYPE_FIELDS (new_obj_rec
)) = TREE_TYPE (ptr_temp_type
);
2738 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (new_obj_rec
)))
2739 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (ptr
)));
2740 DECL_SIZE (TREE_CHAIN (TYPE_FIELDS (new_obj_rec
)))
2741 = TYPE_SIZE (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (ptr
))));
2742 DECL_SIZE_UNIT (TREE_CHAIN (TYPE_FIELDS (new_obj_rec
)))
2743 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (ptr
))));
2745 TYPE_SIZE (new_obj_rec
)
2746 = size_binop (PLUS_EXPR
,
2747 DECL_SIZE (TYPE_FIELDS (new_obj_rec
)),
2748 DECL_SIZE (TREE_CHAIN (TYPE_FIELDS (new_obj_rec
))));
2749 TYPE_SIZE_UNIT (new_obj_rec
)
2750 = size_binop (PLUS_EXPR
,
2751 DECL_SIZE_UNIT (TYPE_FIELDS (new_obj_rec
)),
2752 DECL_SIZE_UNIT (TREE_CHAIN (TYPE_FIELDS (new_obj_rec
))));
2753 rest_of_type_compilation (ptr
, global_bindings_p ());
2757 /* Convert a pointer to a constrained array into a pointer to a fat
2758 pointer. This involves making or finding a template. */
2761 convert_to_fat_pointer (tree type
, tree expr
)
2763 tree template_type
= TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (type
))));
2764 tree
template, template_addr
;
2765 tree etype
= TREE_TYPE (expr
);
2767 /* If EXPR is a constant of zero, we make a fat pointer that has a null
2768 pointer to the template and array. */
2769 if (integer_zerop (expr
))
2771 gnat_build_constructor
2773 tree_cons (TYPE_FIELDS (type
),
2774 convert (TREE_TYPE (TYPE_FIELDS (type
)), expr
),
2775 tree_cons (TREE_CHAIN (TYPE_FIELDS (type
)),
2776 convert (build_pointer_type (template_type
),
2780 /* If EXPR is a thin pointer, make the template and data from the record. */
2782 else if (TYPE_THIN_POINTER_P (etype
))
2784 tree fields
= TYPE_FIELDS (TREE_TYPE (etype
));
2786 expr
= save_expr (expr
);
2787 if (TREE_CODE (expr
) == ADDR_EXPR
)
2788 expr
= TREE_OPERAND (expr
, 0);
2790 expr
= build1 (INDIRECT_REF
, TREE_TYPE (etype
), expr
);
2792 template = build_component_ref (expr
, NULL_TREE
, fields
, 0);
2793 expr
= build_unary_op (ADDR_EXPR
, NULL_TREE
,
2794 build_component_ref (expr
, NULL_TREE
,
2795 TREE_CHAIN (fields
), 0));
2798 /* Otherwise, build the constructor for the template. */
2799 template = build_template (template_type
, TREE_TYPE (etype
), expr
);
2801 template_addr
= build_unary_op (ADDR_EXPR
, NULL_TREE
, template);
2803 /* The result is a CONSTRUCTOR for the fat pointer.
2805 If expr is an argument of a foreign convention subprogram, the type it
2806 points to is directly the component type. In this case, the expression
2807 type may not match the corresponding FIELD_DECL type at this point, so we
2808 call "convert" here to fix that up if necessary. This type consistency is
2809 required, for instance because it ensures that possible later folding of
2810 component_refs against this constructor always yields something of the
2811 same type as the initial reference.
2813 Note that the call to "build_template" above is still fine, because it
2814 will only refer to the provided template_type in this case. */
2816 gnat_build_constructor
2817 (type
, tree_cons (TYPE_FIELDS (type
),
2818 convert (TREE_TYPE (TYPE_FIELDS (type
)), expr
),
2819 tree_cons (TREE_CHAIN (TYPE_FIELDS (type
)),
2820 template_addr
, NULL_TREE
)));
2823 /* Convert to a thin pointer type, TYPE. The only thing we know how to convert
2824 is something that is a fat pointer, so convert to it first if it EXPR
2825 is not already a fat pointer. */
2828 convert_to_thin_pointer (tree type
, tree expr
)
2830 if (! TYPE_FAT_POINTER_P (TREE_TYPE (expr
)))
2832 = convert_to_fat_pointer
2833 (TREE_TYPE (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type
))), expr
);
2835 /* We get the pointer to the data and use a NOP_EXPR to make it the
2838 = build_component_ref (expr
, NULL_TREE
, TYPE_FIELDS (TREE_TYPE (expr
)), 0);
2839 expr
= build1 (NOP_EXPR
, type
, expr
);
2844 /* Create an expression whose value is that of EXPR,
2845 converted to type TYPE. The TREE_TYPE of the value
2846 is always TYPE. This function implements all reasonable
2847 conversions; callers should filter out those that are
2848 not permitted by the language being compiled. */
2851 convert (tree type
, tree expr
)
2853 enum tree_code code
= TREE_CODE (type
);
2854 tree etype
= TREE_TYPE (expr
);
2855 enum tree_code ecode
= TREE_CODE (etype
);
2858 /* If EXPR is already the right type, we are done. */
2861 /* If we're converting between two aggregate types that have the same main
2862 variant, just make a NOP_EXPR. */
2863 else if (AGGREGATE_TYPE_P (type
)
2864 && TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (etype
))
2865 return build1 (NOP_EXPR
, type
, expr
);
2866 /* If EXPR is a WITH_RECORD_EXPR, do the conversion inside and then make a
2868 else if (TREE_CODE (expr
) == WITH_RECORD_EXPR
)
2869 return build (WITH_RECORD_EXPR
, type
,
2870 convert (type
, TREE_OPERAND (expr
, 0)),
2871 TREE_OPERAND (expr
, 1));
2873 /* If the input type has padding, remove it by doing a component reference
2874 to the field. If the output type has padding, make a constructor
2875 to build the record. If both input and output have padding and are
2876 of variable size, do this as an unchecked conversion. */
2877 else if (ecode
== RECORD_TYPE
&& code
== RECORD_TYPE
2878 && TYPE_IS_PADDING_P (type
) && TYPE_IS_PADDING_P (etype
)
2879 && (! TREE_CONSTANT (TYPE_SIZE (type
))
2880 || ! TREE_CONSTANT (TYPE_SIZE (etype
))))
2882 else if (ecode
== RECORD_TYPE
&& TYPE_IS_PADDING_P (etype
))
2884 /* If we have just converted to this padded type, just get
2885 the inner expression. */
2886 if (TREE_CODE (expr
) == CONSTRUCTOR
2887 && CONSTRUCTOR_ELTS (expr
) != 0
2888 && TREE_PURPOSE (CONSTRUCTOR_ELTS (expr
)) == TYPE_FIELDS (etype
))
2889 return TREE_VALUE (CONSTRUCTOR_ELTS (expr
));
2891 return convert (type
, build_component_ref (expr
, NULL_TREE
,
2892 TYPE_FIELDS (etype
), 0));
2894 else if (code
== RECORD_TYPE
&& TYPE_IS_PADDING_P (type
))
2896 /* If we previously converted from another type and our type is
2897 of variable size, remove the conversion to avoid the need for
2898 variable-size temporaries. */
2899 if (TREE_CODE (expr
) == VIEW_CONVERT_EXPR
2900 && ! TREE_CONSTANT (TYPE_SIZE (type
)))
2901 expr
= TREE_OPERAND (expr
, 0);
2903 /* If we are just removing the padding from expr, convert the original
2904 object if we have variable size. That will avoid the need
2905 for some variable-size temporaries. */
2906 if (TREE_CODE (expr
) == COMPONENT_REF
2907 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == RECORD_TYPE
2908 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (expr
, 0)))
2909 && ! TREE_CONSTANT (TYPE_SIZE (type
)))
2910 return convert (type
, TREE_OPERAND (expr
, 0));
2912 /* If the result type is a padded type with a self-referentially-sized
2913 field and the expression type is a record, do this as an
2914 unchecked converstion. */
2915 else if (TREE_CODE (etype
) == RECORD_TYPE
2916 && CONTAINS_PLACEHOLDER_P (DECL_SIZE (TYPE_FIELDS (type
))))
2917 return unchecked_convert (type
, expr
, 0);
2921 gnat_build_constructor (type
,
2922 tree_cons (TYPE_FIELDS (type
),
2924 (TYPE_FIELDS (type
)),
2929 /* If the input is a biased type, adjust first. */
2930 if (ecode
== INTEGER_TYPE
&& TYPE_BIASED_REPRESENTATION_P (etype
))
2931 return convert (type
, fold (build (PLUS_EXPR
, TREE_TYPE (etype
),
2932 fold (build1 (GNAT_NOP_EXPR
,
2933 TREE_TYPE (etype
), expr
)),
2934 TYPE_MIN_VALUE (etype
))));
2936 /* If the input is a left-justified modular type, we need to extract
2937 the actual object before converting it to any other type with the
2938 exception of an unconstrained array. */
2939 if (ecode
== RECORD_TYPE
&& TYPE_LEFT_JUSTIFIED_MODULAR_P (etype
)
2940 && code
!= UNCONSTRAINED_ARRAY_TYPE
)
2941 return convert (type
, build_component_ref (expr
, NULL_TREE
,
2942 TYPE_FIELDS (etype
), 0));
2944 /* If converting to a type that contains a template, convert to the data
2945 type and then build the template. */
2946 if (code
== RECORD_TYPE
&& TYPE_CONTAINS_TEMPLATE_P (type
))
2948 tree obj_type
= TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (type
)));
2950 /* If the source already has a template, get a reference to the
2951 associated array only, as we are going to rebuild a template
2952 for the target type anyway. */
2953 expr
= maybe_unconstrained_array (expr
);
2956 gnat_build_constructor
2958 tree_cons (TYPE_FIELDS (type
),
2959 build_template (TREE_TYPE (TYPE_FIELDS (type
)),
2960 obj_type
, NULL_TREE
),
2961 tree_cons (TREE_CHAIN (TYPE_FIELDS (type
)),
2962 convert (obj_type
, expr
), NULL_TREE
)));
2965 /* There are some special cases of expressions that we process
2967 switch (TREE_CODE (expr
))
2972 case TRANSFORM_EXPR
:
2974 /* Just set its type here. For TRANSFORM_EXPR, we will do the actual
2975 conversion in gnat_expand_expr. NULL_EXPR does not represent
2976 and actual value, so no conversion is needed. */
2977 expr
= copy_node (expr
);
2978 TREE_TYPE (expr
) = type
;
2983 /* If we are converting a STRING_CST to another constrained array type,
2984 just make a new one in the proper type. Likewise for a
2986 if (code
== ecode
&& AGGREGATE_TYPE_P (etype
)
2987 && ! (TREE_CODE (TYPE_SIZE (etype
)) == INTEGER_CST
2988 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
2990 expr
= copy_node (expr
);
2991 TREE_TYPE (expr
) = type
;
2997 /* If we are converting between two aggregate types of the same
2998 kind, size, mode, and alignment, just make a new COMPONENT_REF.
2999 This avoid unneeded conversions which makes reference computations
3001 if (code
== ecode
&& TYPE_MODE (type
) == TYPE_MODE (etype
)
3002 && AGGREGATE_TYPE_P (type
) && AGGREGATE_TYPE_P (etype
)
3003 && TYPE_ALIGN (type
) == TYPE_ALIGN (etype
)
3004 && operand_equal_p (TYPE_SIZE (type
), TYPE_SIZE (etype
), 0))
3005 return build (COMPONENT_REF
, type
, TREE_OPERAND (expr
, 0),
3006 TREE_OPERAND (expr
, 1));
3010 case UNCONSTRAINED_ARRAY_REF
:
3011 /* Convert this to the type of the inner array by getting the address of
3012 the array from the template. */
3013 expr
= build_unary_op (INDIRECT_REF
, NULL_TREE
,
3014 build_component_ref (TREE_OPERAND (expr
, 0),
3015 get_identifier ("P_ARRAY"),
3017 etype
= TREE_TYPE (expr
);
3018 ecode
= TREE_CODE (etype
);
3021 case VIEW_CONVERT_EXPR
:
3022 if (AGGREGATE_TYPE_P (type
) && AGGREGATE_TYPE_P (etype
)
3023 && ! TYPE_FAT_POINTER_P (type
) && ! TYPE_FAT_POINTER_P (etype
))
3024 return convert (type
, TREE_OPERAND (expr
, 0));
3028 /* If both types are record types, just convert the pointer and
3029 make a new INDIRECT_REF.
3031 ??? Disable this for now since it causes problems with the
3032 code in build_binary_op for MODIFY_EXPR which wants to
3033 strip off conversions. But that code really is a mess and
3034 we need to do this a much better way some time. */
3036 && (TREE_CODE (type
) == RECORD_TYPE
3037 || TREE_CODE (type
) == UNION_TYPE
)
3038 && (TREE_CODE (etype
) == RECORD_TYPE
3039 || TREE_CODE (etype
) == UNION_TYPE
)
3040 && ! TYPE_FAT_POINTER_P (type
) && ! TYPE_FAT_POINTER_P (etype
))
3041 return build_unary_op (INDIRECT_REF
, NULL_TREE
,
3042 convert (build_pointer_type (type
),
3043 TREE_OPERAND (expr
, 0)));
3050 /* Check for converting to a pointer to an unconstrained array. */
3051 if (TYPE_FAT_POINTER_P (type
) && ! TYPE_FAT_POINTER_P (etype
))
3052 return convert_to_fat_pointer (type
, expr
);
3054 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (etype
)
3055 || (code
== INTEGER_CST
&& ecode
== INTEGER_CST
3056 && (type
== TREE_TYPE (etype
) || etype
== TREE_TYPE (type
))))
3057 return fold (build1 (NOP_EXPR
, type
, expr
));
3062 return build1 (CONVERT_EXPR
, type
, expr
);
3065 if (TYPE_HAS_ACTUAL_BOUNDS_P (type
)
3066 && (ecode
== ARRAY_TYPE
|| ecode
== UNCONSTRAINED_ARRAY_TYPE
3067 || (ecode
== RECORD_TYPE
&& TYPE_CONTAINS_TEMPLATE_P (etype
))))
3068 return unchecked_convert (type
, expr
, 0);
3069 else if (TYPE_BIASED_REPRESENTATION_P (type
))
3070 return fold (build1 (CONVERT_EXPR
, type
,
3071 fold (build (MINUS_EXPR
, TREE_TYPE (type
),
3072 convert (TREE_TYPE (type
), expr
),
3073 TYPE_MIN_VALUE (type
)))));
3075 /* ... fall through ... */
3078 return fold (convert_to_integer (type
, expr
));
3081 case REFERENCE_TYPE
:
3082 /* If converting between two pointers to records denoting
3083 both a template and type, adjust if needed to account
3084 for any differing offsets, since one might be negative. */
3085 if (TYPE_THIN_POINTER_P (etype
) && TYPE_THIN_POINTER_P (type
))
3088 = size_diffop (bit_position (TYPE_FIELDS (TREE_TYPE (etype
))),
3089 bit_position (TYPE_FIELDS (TREE_TYPE (type
))));
3090 tree byte_diff
= size_binop (CEIL_DIV_EXPR
, bit_diff
,
3091 sbitsize_int (BITS_PER_UNIT
));
3093 expr
= build1 (NOP_EXPR
, type
, expr
);
3094 TREE_CONSTANT (expr
) = TREE_CONSTANT (TREE_OPERAND (expr
, 0));
3095 if (integer_zerop (byte_diff
))
3098 return build_binary_op (PLUS_EXPR
, type
, expr
,
3099 fold (convert_to_pointer (type
, byte_diff
)));
3102 /* If converting to a thin pointer, handle specially. */
3103 if (TYPE_THIN_POINTER_P (type
)
3104 && TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type
)) != 0)
3105 return convert_to_thin_pointer (type
, expr
);
3107 /* If converting fat pointer to normal pointer, get the pointer to the
3108 array and then convert it. */
3109 else if (TYPE_FAT_POINTER_P (etype
))
3110 expr
= build_component_ref (expr
, get_identifier ("P_ARRAY"),
3113 return fold (convert_to_pointer (type
, expr
));
3116 return fold (convert_to_real (type
, expr
));
3119 if (TYPE_LEFT_JUSTIFIED_MODULAR_P (type
) && ! AGGREGATE_TYPE_P (etype
))
3121 gnat_build_constructor
3122 (type
, tree_cons (TYPE_FIELDS (type
),
3123 convert (TREE_TYPE (TYPE_FIELDS (type
)), expr
),
3126 /* ... fall through ... */
3129 /* In these cases, assume the front-end has validated the conversion.
3130 If the conversion is valid, it will be a bit-wise conversion, so
3131 it can be viewed as an unchecked conversion. */
3132 return unchecked_convert (type
, expr
, 0);
3135 /* Just validate that the type is indeed that of a field
3136 of the type. Then make the simple conversion. */
3137 for (tem
= TYPE_FIELDS (type
); tem
; tem
= TREE_CHAIN (tem
))
3139 if (TREE_TYPE (tem
) == etype
)
3140 return build1 (CONVERT_EXPR
, type
, expr
);
3141 else if (TREE_CODE (TREE_TYPE (tem
)) == RECORD_TYPE
3142 && (TYPE_LEFT_JUSTIFIED_MODULAR_P (TREE_TYPE (tem
))
3143 || TYPE_IS_PADDING_P (TREE_TYPE (tem
)))
3144 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (tem
))) == etype
)
3145 return build1 (CONVERT_EXPR
, type
,
3146 convert (TREE_TYPE (tem
), expr
));
3151 case UNCONSTRAINED_ARRAY_TYPE
:
3152 /* If EXPR is a constrained array, take its address, convert it to a
3153 fat pointer, and then dereference it. Likewise if EXPR is a
3154 record containing both a template and a constrained array.
3155 Note that a record representing a left justified modular type
3156 always represents a packed constrained array. */
3157 if (ecode
== ARRAY_TYPE
3158 || (ecode
== INTEGER_TYPE
&& TYPE_HAS_ACTUAL_BOUNDS_P (etype
))
3159 || (ecode
== RECORD_TYPE
&& TYPE_CONTAINS_TEMPLATE_P (etype
))
3160 || (ecode
== RECORD_TYPE
&& TYPE_LEFT_JUSTIFIED_MODULAR_P (etype
)))
3163 (INDIRECT_REF
, NULL_TREE
,
3164 convert_to_fat_pointer (TREE_TYPE (type
),
3165 build_unary_op (ADDR_EXPR
,
3168 /* Do something very similar for converting one unconstrained
3169 array to another. */
3170 else if (ecode
== UNCONSTRAINED_ARRAY_TYPE
)
3172 build_unary_op (INDIRECT_REF
, NULL_TREE
,
3173 convert (TREE_TYPE (type
),
3174 build_unary_op (ADDR_EXPR
,
3180 return fold (convert_to_complex (type
, expr
));
3187 /* Remove all conversions that are done in EXP. This includes converting
3188 from a padded type or to a left-justified modular type. If TRUE_ADDRESS
3189 is nonzero, always return the address of the containing object even if
3190 the address is not bit-aligned. */
3193 remove_conversions (tree exp
, int true_address
)
3195 switch (TREE_CODE (exp
))
3199 && TREE_CODE (TREE_TYPE (exp
)) == RECORD_TYPE
3200 && TYPE_LEFT_JUSTIFIED_MODULAR_P (TREE_TYPE (exp
)))
3201 return remove_conversions (TREE_VALUE (CONSTRUCTOR_ELTS (exp
)), 1);
3205 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp
, 0))) == RECORD_TYPE
3206 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (exp
, 0))))
3207 return remove_conversions (TREE_OPERAND (exp
, 0), true_address
);
3210 case VIEW_CONVERT_EXPR
: case NON_LVALUE_EXPR
:
3211 case NOP_EXPR
: case CONVERT_EXPR
: case GNAT_NOP_EXPR
:
3212 return remove_conversions (TREE_OPERAND (exp
, 0), true_address
);
3221 /* If EXP's type is an UNCONSTRAINED_ARRAY_TYPE, return an expression that
3222 refers to the underlying array. If its type has TYPE_CONTAINS_TEMPLATE_P,
3223 likewise return an expression pointing to the underlying array. */
3226 maybe_unconstrained_array (tree exp
)
3228 enum tree_code code
= TREE_CODE (exp
);
3231 switch (TREE_CODE (TREE_TYPE (exp
)))
3233 case UNCONSTRAINED_ARRAY_TYPE
:
3234 if (code
== UNCONSTRAINED_ARRAY_REF
)
3237 = build_unary_op (INDIRECT_REF
, NULL_TREE
,
3238 build_component_ref (TREE_OPERAND (exp
, 0),
3239 get_identifier ("P_ARRAY"),
3241 TREE_READONLY (new) = TREE_STATIC (new) = TREE_READONLY (exp
);
3245 else if (code
== NULL_EXPR
)
3246 return build1 (NULL_EXPR
,
3247 TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3248 (TREE_TYPE (TREE_TYPE (exp
))))),
3249 TREE_OPERAND (exp
, 0));
3251 else if (code
== WITH_RECORD_EXPR
3252 && (TREE_OPERAND (exp
, 0)
3253 != (new = maybe_unconstrained_array
3254 (TREE_OPERAND (exp
, 0)))))
3255 return build (WITH_RECORD_EXPR
, TREE_TYPE (new), new,
3256 TREE_OPERAND (exp
, 1));
3259 /* If this is a padded type, convert to the unpadded type and see if
3260 it contains a template. */
3261 if (TYPE_IS_PADDING_P (TREE_TYPE (exp
)))
3263 new = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp
))), exp
);
3264 if (TREE_CODE (TREE_TYPE (new)) == RECORD_TYPE
3265 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (new)))
3267 build_component_ref (new, NULL_TREE
,
3268 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new))),
3271 else if (TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (exp
)))
3273 build_component_ref (exp
, NULL_TREE
,
3274 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (exp
))), 0);
3284 /* Return an expression that does an unchecked converstion of EXPR to TYPE.
3285 If NOTRUNC_P is set, truncation operations should be suppressed. */
3288 unchecked_convert (tree type
, tree expr
, int notrunc_p
)
3290 tree etype
= TREE_TYPE (expr
);
3292 /* If the expression is already the right type, we are done. */
3296 /* If EXPR is a WITH_RECORD_EXPR, do the conversion inside and then make a
3298 if (TREE_CODE (expr
) == WITH_RECORD_EXPR
)
3299 return build (WITH_RECORD_EXPR
, type
,
3300 unchecked_convert (type
, TREE_OPERAND (expr
, 0), notrunc_p
),
3301 TREE_OPERAND (expr
, 1));
3303 /* If both types types are integral just do a normal conversion.
3304 Likewise for a conversion to an unconstrained array. */
3305 if ((((INTEGRAL_TYPE_P (type
)
3306 && ! (TREE_CODE (type
) == INTEGER_TYPE
3307 && TYPE_VAX_FLOATING_POINT_P (type
)))
3308 || (POINTER_TYPE_P (type
) && ! TYPE_THIN_POINTER_P (type
))
3309 || (TREE_CODE (type
) == RECORD_TYPE
3310 && TYPE_LEFT_JUSTIFIED_MODULAR_P (type
)))
3311 && ((INTEGRAL_TYPE_P (etype
)
3312 && ! (TREE_CODE (etype
) == INTEGER_TYPE
3313 && TYPE_VAX_FLOATING_POINT_P (etype
)))
3314 || (POINTER_TYPE_P (etype
) && ! TYPE_THIN_POINTER_P (etype
))
3315 || (TREE_CODE (etype
) == RECORD_TYPE
3316 && TYPE_LEFT_JUSTIFIED_MODULAR_P (etype
))))
3317 || TREE_CODE (type
) == UNCONSTRAINED_ARRAY_TYPE
)
3321 if (TREE_CODE (etype
) == INTEGER_TYPE
3322 && TYPE_BIASED_REPRESENTATION_P (etype
))
3324 tree ntype
= copy_type (etype
);
3326 TYPE_BIASED_REPRESENTATION_P (ntype
) = 0;
3327 TYPE_MAIN_VARIANT (ntype
) = ntype
;
3328 expr
= build1 (GNAT_NOP_EXPR
, ntype
, expr
);
3331 if (TREE_CODE (type
) == INTEGER_TYPE
3332 && TYPE_BIASED_REPRESENTATION_P (type
))
3334 rtype
= copy_type (type
);
3335 TYPE_BIASED_REPRESENTATION_P (rtype
) = 0;
3336 TYPE_MAIN_VARIANT (rtype
) = rtype
;
3339 expr
= convert (rtype
, expr
);
3341 expr
= build1 (GNAT_NOP_EXPR
, type
, expr
);
3344 /* If we are converting TO an integral type whose precision is not the
3345 same as its size, first unchecked convert to a record that contains
3346 an object of the output type. Then extract the field. */
3347 else if (INTEGRAL_TYPE_P (type
) && TYPE_RM_SIZE (type
) != 0
3348 && 0 != compare_tree_int (TYPE_RM_SIZE (type
),
3349 GET_MODE_BITSIZE (TYPE_MODE (type
))))
3351 tree rec_type
= make_node (RECORD_TYPE
);
3352 tree field
= create_field_decl (get_identifier ("OBJ"), type
,
3353 rec_type
, 1, 0, 0, 0);
3355 TYPE_FIELDS (rec_type
) = field
;
3356 layout_type (rec_type
);
3358 expr
= unchecked_convert (rec_type
, expr
, notrunc_p
);
3359 expr
= build_component_ref (expr
, NULL_TREE
, field
, 0);
3362 /* Similarly for integral input type whose precision is not equal to its
3364 else if (INTEGRAL_TYPE_P (etype
) && TYPE_RM_SIZE (etype
) != 0
3365 && 0 != compare_tree_int (TYPE_RM_SIZE (etype
),
3366 GET_MODE_BITSIZE (TYPE_MODE (etype
))))
3368 tree rec_type
= make_node (RECORD_TYPE
);
3370 = create_field_decl (get_identifier ("OBJ"), etype
, rec_type
,
3373 TYPE_FIELDS (rec_type
) = field
;
3374 layout_type (rec_type
);
3376 expr
= gnat_build_constructor (rec_type
, build_tree_list (field
, expr
));
3377 expr
= unchecked_convert (type
, expr
, notrunc_p
);
3380 /* We have a special case when we are converting between two
3381 unconstrained array types. In that case, take the address,
3382 convert the fat pointer types, and dereference. */
3383 else if (TREE_CODE (etype
) == UNCONSTRAINED_ARRAY_TYPE
3384 && TREE_CODE (type
) == UNCONSTRAINED_ARRAY_TYPE
)
3385 expr
= build_unary_op (INDIRECT_REF
, NULL_TREE
,
3386 build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (type
),
3387 build_unary_op (ADDR_EXPR
, NULL_TREE
,
3391 expr
= maybe_unconstrained_array (expr
);
3392 etype
= TREE_TYPE (expr
);
3393 expr
= build1 (VIEW_CONVERT_EXPR
, type
, expr
);
3396 /* If the result is an integral type whose size is not equal to
3397 the size of the underlying machine type, sign- or zero-extend
3398 the result. We need not do this in the case where the input is
3399 an integral type of the same precision and signedness or if the output
3400 is a biased type or if both the input and output are unsigned. */
3402 && INTEGRAL_TYPE_P (type
) && TYPE_RM_SIZE (type
) != 0
3403 && ! (TREE_CODE (type
) == INTEGER_TYPE
3404 && TYPE_BIASED_REPRESENTATION_P (type
))
3405 && 0 != compare_tree_int (TYPE_RM_SIZE (type
),
3406 GET_MODE_BITSIZE (TYPE_MODE (type
)))
3407 && ! (INTEGRAL_TYPE_P (etype
)
3408 && TREE_UNSIGNED (type
) == TREE_UNSIGNED (etype
)
3409 && operand_equal_p (TYPE_RM_SIZE (type
),
3410 (TYPE_RM_SIZE (etype
) != 0
3411 ? TYPE_RM_SIZE (etype
) : TYPE_SIZE (etype
)),
3413 && ! (TREE_UNSIGNED (type
) && TREE_UNSIGNED (etype
)))
3415 tree base_type
= gnat_type_for_mode (TYPE_MODE (type
),
3416 TREE_UNSIGNED (type
));
3418 = convert (base_type
,
3419 size_binop (MINUS_EXPR
,
3421 (GET_MODE_BITSIZE (TYPE_MODE (type
))),
3422 TYPE_RM_SIZE (type
)));
3425 build_binary_op (RSHIFT_EXPR
, base_type
,
3426 build_binary_op (LSHIFT_EXPR
, base_type
,
3427 convert (base_type
, expr
),
3432 /* An unchecked conversion should never raise Constraint_Error. The code
3433 below assumes that GCC's conversion routines overflow the same way that
3434 the underlying hardware does. This is probably true. In the rare case
3435 when it is false, we can rely on the fact that such conversions are
3436 erroneous anyway. */
3437 if (TREE_CODE (expr
) == INTEGER_CST
)
3438 TREE_OVERFLOW (expr
) = TREE_CONSTANT_OVERFLOW (expr
) = 0;
3440 /* If the sizes of the types differ and this is an VIEW_CONVERT_EXPR,
3441 show no longer constant. */
3442 if (TREE_CODE (expr
) == VIEW_CONVERT_EXPR
3443 && ! operand_equal_p (TYPE_SIZE_UNIT (type
), TYPE_SIZE_UNIT (etype
), 1))
3444 TREE_CONSTANT (expr
) = 0;
3449 #include "gt-ada-utils.h"
3450 #include "gtype-ada.h"