1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2005, 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, 51 Franklin Street, Fifth Floor, *
20 * Boston, MA 02110-1301, 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"
42 #include "tree-gimple.h"
61 struct Node
*Nodes_Ptr
;
62 Node_Id
*Next_Node_Ptr
;
63 Node_Id
*Prev_Node_Ptr
;
64 struct Elist_Header
*Elists_Ptr
;
65 struct Elmt_Item
*Elmts_Ptr
;
66 struct String_Entry
*Strings_Ptr
;
67 Char_Code
*String_Chars_Ptr
;
68 struct List_Header
*List_Headers_Ptr
;
70 /* Current filename without path. */
71 const char *ref_filename
;
73 /* If true, then gigi is being called on an analyzed but unexpanded
74 tree, and the only purpose of the call is to properly annotate
75 types with representation information. */
76 bool type_annotate_only
;
78 /* A structure used to gather together information about a statement group.
79 We use this to gather related statements, for example the "then" part
80 of a IF. In the case where it represents a lexical scope, we may also
81 have a BLOCK node corresponding to it and/or cleanups. */
83 struct stmt_group
GTY((chain_next ("%h.previous"))) {
84 struct stmt_group
*previous
; /* Previous code group. */
85 tree stmt_list
; /* List of statements for this code group. */
86 tree block
; /* BLOCK for this code group, if any. */
87 tree cleanups
; /* Cleanups for this code group, if any. */
90 static GTY(()) struct stmt_group
*current_stmt_group
;
92 /* List of unused struct stmt_group nodes. */
93 static GTY((deletable
)) struct stmt_group
*stmt_group_free_list
;
95 /* A structure used to record information on elaboration procedures
96 we've made and need to process.
98 ??? gnat_node should be Node_Id, but gengtype gets confused. */
100 struct elab_info
GTY((chain_next ("%h.next"))) {
101 struct elab_info
*next
; /* Pointer to next in chain. */
102 tree elab_proc
; /* Elaboration procedure. */
103 int gnat_node
; /* The N_Compilation_Unit. */
106 static GTY(()) struct elab_info
*elab_info_list
;
108 /* Free list of TREE_LIST nodes used for stacks. */
109 static GTY((deletable
)) tree gnu_stack_free_list
;
111 /* List of TREE_LIST nodes representing a stack of exception pointer
112 variables. TREE_VALUE is the VAR_DECL that stores the address of
113 the raised exception. Nonzero means we are in an exception
114 handler. Not used in the zero-cost case. */
115 static GTY(()) tree gnu_except_ptr_stack
;
117 /* List of TREE_LIST nodes used to store the current elaboration procedure
118 decl. TREE_VALUE is the decl. */
119 static GTY(()) tree gnu_elab_proc_stack
;
121 /* Variable that stores a list of labels to be used as a goto target instead of
122 a return in some functions. See processing for N_Subprogram_Body. */
123 static GTY(()) tree gnu_return_label_stack
;
125 /* List of TREE_LIST nodes representing a stack of LOOP_STMT nodes.
126 TREE_VALUE of each entry is the label of the corresponding LOOP_STMT. */
127 static GTY(()) tree gnu_loop_label_stack
;
129 /* List of TREE_LIST nodes representing labels for switch statements.
130 TREE_VALUE of each entry is the label at the end of the switch. */
131 static GTY(()) tree gnu_switch_label_stack
;
133 /* Map GNAT tree codes to GCC tree codes for simple expressions. */
134 static enum tree_code gnu_codes
[Number_Node_Kinds
];
136 /* Current node being treated, in case abort called. */
137 Node_Id error_gnat_node
;
139 static void Compilation_Unit_to_gnu (Node_Id
);
140 static void record_code_position (Node_Id
);
141 static void insert_code_for (Node_Id
);
142 static void start_stmt_group (void);
143 static void add_cleanup (tree
);
144 static tree
mark_visited (tree
*, int *, void *);
145 static tree
mark_unvisited (tree
*, int *, void *);
146 static tree
end_stmt_group (void);
147 static void add_stmt_list (List_Id
);
148 static tree
build_stmt_group (List_Id
, bool);
149 static void push_stack (tree
*, tree
, tree
);
150 static void pop_stack (tree
*);
151 static enum gimplify_status
gnat_gimplify_stmt (tree
*);
152 static void elaborate_all_entities (Node_Id
);
153 static void process_freeze_entity (Node_Id
);
154 static void process_inlined_subprograms (Node_Id
);
155 static void process_decls (List_Id
, List_Id
, Node_Id
, bool, bool);
156 static tree
emit_range_check (tree
, Node_Id
);
157 static tree
emit_index_check (tree
, tree
, tree
, tree
);
158 static tree
emit_check (tree
, tree
, int);
159 static tree
convert_with_check (Entity_Id
, tree
, bool, bool, bool);
160 static bool addressable_p (tree
);
161 static tree
assoc_to_constructor (Node_Id
, tree
);
162 static tree
extract_values (tree
, tree
);
163 static tree
pos_to_constructor (Node_Id
, tree
, Entity_Id
);
164 static tree
maybe_implicit_deref (tree
);
165 static tree
gnat_stabilize_reference_1 (tree
, bool);
166 static void annotate_with_node (tree
, Node_Id
);
169 /* This is the main program of the back-end. It sets up all the table
170 structures and then generates code. */
173 gigi (Node_Id gnat_root
, int max_gnat_node
, int number_name
,
174 struct Node
*nodes_ptr
, Node_Id
*next_node_ptr
, Node_Id
*prev_node_ptr
,
175 struct Elist_Header
*elists_ptr
, struct Elmt_Item
*elmts_ptr
,
176 struct String_Entry
*strings_ptr
, Char_Code
*string_chars_ptr
,
177 struct List_Header
*list_headers_ptr
, Int number_units ATTRIBUTE_UNUSED
,
178 char *file_info_ptr ATTRIBUTE_UNUSED
, Entity_Id standard_integer
,
179 Entity_Id standard_long_long_float
, Entity_Id standard_exception_type
,
180 Int gigi_operating_mode
)
182 tree gnu_standard_long_long_float
;
183 tree gnu_standard_exception_type
;
184 struct elab_info
*info
;
186 max_gnat_nodes
= max_gnat_node
;
187 number_names
= number_name
;
188 Nodes_Ptr
= nodes_ptr
;
189 Next_Node_Ptr
= next_node_ptr
;
190 Prev_Node_Ptr
= prev_node_ptr
;
191 Elists_Ptr
= elists_ptr
;
192 Elmts_Ptr
= elmts_ptr
;
193 Strings_Ptr
= strings_ptr
;
194 String_Chars_Ptr
= string_chars_ptr
;
195 List_Headers_Ptr
= list_headers_ptr
;
197 type_annotate_only
= (gigi_operating_mode
== 1);
200 gnat_compute_largest_alignment ();
203 /* If we are just annotating types, give VOID_TYPE zero sizes to avoid
205 if (type_annotate_only
)
207 TYPE_SIZE (void_type_node
) = bitsize_zero_node
;
208 TYPE_SIZE_UNIT (void_type_node
) = size_zero_node
;
211 /* Save the type we made for integer as the type for Standard.Integer.
212 Then make the rest of the standard types. Note that some of these
214 save_gnu_tree (Base_Type (standard_integer
), TYPE_NAME (integer_type_node
),
217 gnu_except_ptr_stack
= tree_cons (NULL_TREE
, NULL_TREE
, NULL_TREE
);
219 gnu_standard_long_long_float
220 = gnat_to_gnu_entity (Base_Type (standard_long_long_float
), NULL_TREE
, 0);
221 gnu_standard_exception_type
222 = gnat_to_gnu_entity (Base_Type (standard_exception_type
), NULL_TREE
, 0);
224 init_gigi_decls (gnu_standard_long_long_float
, gnu_standard_exception_type
);
226 /* Process any Pragma Ident for the main unit. */
227 #ifdef ASM_OUTPUT_IDENT
228 if (Present (Ident_String (Main_Unit
)))
231 TREE_STRING_POINTER (gnat_to_gnu (Ident_String (Main_Unit
))));
234 /* If we are using the GCC exception mechanism, let GCC know. */
235 if (Exception_Mechanism
== GCC_ZCX
)
238 gcc_assert (Nkind (gnat_root
) == N_Compilation_Unit
);
239 Compilation_Unit_to_gnu (gnat_root
);
241 /* Now see if we have any elaboration procedures to deal with. */
242 for (info
= elab_info_list
; info
; info
= info
->next
)
244 tree gnu_body
= DECL_SAVED_TREE (info
->elab_proc
);
247 /* Mark everything we have as not visited. */
248 walk_tree_without_duplicates (&gnu_body
, mark_unvisited
, NULL
);
250 /* Set the current function to be the elaboration procedure and gimplify
252 current_function_decl
= info
->elab_proc
;
253 gimplify_body (&gnu_body
, info
->elab_proc
, true);
255 /* We should have a BIND_EXPR, but it may or may not have any statements
256 in it. If it doesn't have any, we have nothing to do. */
257 gnu_stmts
= gnu_body
;
258 if (TREE_CODE (gnu_stmts
) == BIND_EXPR
)
259 gnu_stmts
= BIND_EXPR_BODY (gnu_stmts
);
261 /* If there are no statements, there is no elaboration code. */
262 if (!gnu_stmts
|| !STATEMENT_LIST_HEAD (gnu_stmts
))
263 Set_Has_No_Elaboration_Code (info
->gnat_node
, 1);
266 /* Otherwise, compile the function. Note that we'll be gimplifying
267 it twice, but that's fine for the nodes we use. */
268 begin_subprog_body (info
->elab_proc
);
269 end_subprog_body (gnu_body
);
274 /* Perform initializations for this module. */
277 gnat_init_stmt_group ()
279 /* Initialize ourselves. */
283 /* Enable GNAT stack checking method if needed */
284 if (!Stack_Check_Probes_On_Target
)
285 set_stack_check_libfunc (gen_rtx_SYMBOL_REF (Pmode
, "_gnat_stack_check"));
287 gcc_assert (Exception_Mechanism
!= Front_End_ZCX
);
290 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Identifier,
291 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to
292 where we should place the result type. */
295 Identifier_to_gnu (Node_Id gnat_node
, tree
*gnu_result_type_p
)
297 tree gnu_result_type
;
299 Node_Id gnat_temp
, gnat_temp_type
;
301 /* If the Etype of this node does not equal the Etype of the Entity,
302 something is wrong with the entity map, probably in generic
303 instantiation. However, this does not apply to types. Since we sometime
304 have strange Ekind's, just do this test for objects. Also, if the Etype of
305 the Entity is private, the Etype of the N_Identifier is allowed to be the
306 full type and also we consider a packed array type to be the same as the
307 original type. Similarly, a class-wide type is equivalent to a subtype of
308 itself. Finally, if the types are Itypes, one may be a copy of the other,
309 which is also legal. */
310 gnat_temp
= (Nkind (gnat_node
) == N_Defining_Identifier
311 ? gnat_node
: Entity (gnat_node
));
312 gnat_temp_type
= Etype (gnat_temp
);
314 gcc_assert (Etype (gnat_node
) == gnat_temp_type
315 || (Is_Packed (gnat_temp_type
)
316 && Etype (gnat_node
) == Packed_Array_Type (gnat_temp_type
))
317 || (Is_Class_Wide_Type (Etype (gnat_node
)))
318 || (IN (Ekind (gnat_temp_type
), Private_Kind
)
319 && Present (Full_View (gnat_temp_type
))
320 && ((Etype (gnat_node
) == Full_View (gnat_temp_type
))
321 || (Is_Packed (Full_View (gnat_temp_type
))
322 && (Etype (gnat_node
)
323 == Packed_Array_Type (Full_View
324 (gnat_temp_type
))))))
325 || (Is_Itype (Etype (gnat_node
)) && Is_Itype (gnat_temp_type
))
326 || !(Ekind (gnat_temp
) == E_Variable
327 || Ekind (gnat_temp
) == E_Component
328 || Ekind (gnat_temp
) == E_Constant
329 || Ekind (gnat_temp
) == E_Loop_Parameter
330 || IN (Ekind (gnat_temp
), Formal_Kind
)));
332 /* If this is a reference to a deferred constant whose partial view is an
333 unconstrained private type, the proper type is on the full view of the
334 constant, not on the full view of the type, which may be unconstrained.
336 This may be a reference to a type, for example in the prefix of the
337 attribute Position, generated for dispatching code (see Make_DT in
338 exp_disp,adb). In that case we need the type itself, not is parent,
339 in particular if it is a derived type */
340 if (Is_Private_Type (gnat_temp_type
)
341 && Has_Unknown_Discriminants (gnat_temp_type
)
342 && Present (Full_View (gnat_temp
))
343 && !Is_Type (gnat_temp
))
345 gnat_temp
= Full_View (gnat_temp
);
346 gnat_temp_type
= Etype (gnat_temp
);
347 gnu_result_type
= get_unpadded_type (gnat_temp_type
);
351 /* Expand the type of this identifier first, in case it is an enumeral
352 literal, which only get made when the type is expanded. There is no
353 order-of-elaboration issue here. We want to use the Actual_Subtype if
354 it has already been elaborated, otherwise the Etype. Avoid using
355 Actual_Subtype for packed arrays to simplify things. */
356 if ((Ekind (gnat_temp
) == E_Constant
357 || Ekind (gnat_temp
) == E_Variable
|| Is_Formal (gnat_temp
))
358 && !(Is_Array_Type (Etype (gnat_temp
))
359 && Present (Packed_Array_Type (Etype (gnat_temp
))))
360 && Present (Actual_Subtype (gnat_temp
))
361 && present_gnu_tree (Actual_Subtype (gnat_temp
)))
362 gnat_temp_type
= Actual_Subtype (gnat_temp
);
364 gnat_temp_type
= Etype (gnat_node
);
366 gnu_result_type
= get_unpadded_type (gnat_temp_type
);
369 gnu_result
= gnat_to_gnu_entity (gnat_temp
, NULL_TREE
, 0);
371 /* If we are in an exception handler, force this variable into memory to
372 ensure optimization does not remove stores that appear redundant but are
373 actually needed in case an exception occurs.
375 ??? Note that we need not do this if the variable is declared within the
376 handler, only if it is referenced in the handler and declared in an
377 enclosing block, but we have no way of testing that right now.
379 ??? Also, for now all we can do is make it volatile. But we only
381 if (TREE_VALUE (gnu_except_ptr_stack
)
382 && TREE_CODE (gnu_result
) == VAR_DECL
)
383 TREE_THIS_VOLATILE (gnu_result
) = TREE_SIDE_EFFECTS (gnu_result
) = 1;
385 /* Some objects (such as parameters passed by reference, globals of
386 variable size, and renamed objects) actually represent the address
387 of the object. In that case, we must do the dereference. Likewise,
388 deal with parameters to foreign convention subprograms. Call fold
389 here since GNU_RESULT may be a CONST_DECL. */
390 if (DECL_P (gnu_result
)
391 && (DECL_BY_REF_P (gnu_result
)
392 || (TREE_CODE (gnu_result
) == PARM_DECL
393 && DECL_BY_COMPONENT_PTR_P (gnu_result
))))
395 bool ro
= DECL_POINTS_TO_READONLY_P (gnu_result
);
398 if (TREE_CODE (gnu_result
) == PARM_DECL
399 && DECL_BY_COMPONENT_PTR_P (gnu_result
))
401 = build_unary_op (INDIRECT_REF
, NULL_TREE
,
402 convert (build_pointer_type (gnu_result_type
),
405 /* If it's a renaming pointer and we are at the right binding level,
406 we can reference the renamed object directly, since the renamed
407 expression has been protected against multiple evaluations. */
408 else if (TREE_CODE (gnu_result
) == VAR_DECL
409 && (renamed_obj
= DECL_RENAMED_OBJECT (gnu_result
)) != 0
410 && (! DECL_RENAMING_GLOBAL_P (gnu_result
)
411 || global_bindings_p ())
412 /* Make sure it's an lvalue like INDIRECT_REF. */
413 && (DECL_P (renamed_obj
) || REFERENCE_CLASS_P (renamed_obj
)))
414 gnu_result
= renamed_obj
;
416 gnu_result
= build_unary_op (INDIRECT_REF
, NULL_TREE
,
419 TREE_READONLY (gnu_result
) = TREE_STATIC (gnu_result
) = ro
;
422 /* The GNAT tree has the type of a function as the type of its result. Also
423 use the type of the result if the Etype is a subtype which is nominally
424 unconstrained. But remove any padding from the resulting type. */
425 if (TREE_CODE (TREE_TYPE (gnu_result
)) == FUNCTION_TYPE
426 || Is_Constr_Subt_For_UN_Aliased (gnat_temp_type
))
428 gnu_result_type
= TREE_TYPE (gnu_result
);
429 if (TREE_CODE (gnu_result_type
) == RECORD_TYPE
430 && TYPE_IS_PADDING_P (gnu_result_type
))
431 gnu_result_type
= TREE_TYPE (TYPE_FIELDS (gnu_result_type
));
434 /* We always want to return the underlying INTEGER_CST for an enumeration
435 literal to avoid the need to call fold in lots of places. But don't do
436 this is the parent will be taking the address of this object. */
437 if (TREE_CODE (gnu_result
) == CONST_DECL
)
439 gnat_temp
= Parent (gnat_node
);
440 if (!DECL_CONST_CORRESPONDING_VAR (gnu_result
)
441 || (Nkind (gnat_temp
) != N_Reference
442 && !(Nkind (gnat_temp
) == N_Attribute_Reference
443 && ((Get_Attribute_Id (Attribute_Name (gnat_temp
))
445 || (Get_Attribute_Id (Attribute_Name (gnat_temp
))
447 || (Get_Attribute_Id (Attribute_Name (gnat_temp
))
448 == Attr_Unchecked_Access
)
449 || (Get_Attribute_Id (Attribute_Name (gnat_temp
))
450 == Attr_Unrestricted_Access
)))))
451 gnu_result
= DECL_INITIAL (gnu_result
);
454 *gnu_result_type_p
= gnu_result_type
;
458 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Pragma. Return
459 any statements we generate. */
462 Pragma_to_gnu (Node_Id gnat_node
)
465 tree gnu_result
= alloc_stmt_list ();
467 /* Check for (and ignore) unrecognized pragma and do nothing if we are just
469 if (type_annotate_only
|| !Is_Pragma_Name (Chars (gnat_node
)))
472 switch (Get_Pragma_Id (Chars (gnat_node
)))
474 case Pragma_Inspection_Point
:
475 /* Do nothing at top level: all such variables are already viewable. */
476 if (global_bindings_p ())
479 for (gnat_temp
= First (Pragma_Argument_Associations (gnat_node
));
481 gnat_temp
= Next (gnat_temp
))
483 tree gnu_expr
= gnat_to_gnu (Expression (gnat_temp
));
485 if (TREE_CODE (gnu_expr
) == UNCONSTRAINED_ARRAY_REF
)
486 gnu_expr
= TREE_OPERAND (gnu_expr
, 0);
488 gnu_expr
= build1 (USE_STMT
, void_type_node
, gnu_expr
);
489 annotate_with_node (gnu_expr
, gnat_node
);
490 append_to_statement_list (gnu_expr
, &gnu_result
);
494 case Pragma_Optimize
:
495 switch (Chars (Expression
496 (First (Pragma_Argument_Associations (gnat_node
)))))
498 case Name_Time
: case Name_Space
:
500 post_error ("insufficient -O value?", gnat_node
);
505 post_error ("must specify -O0?", gnat_node
);
513 case Pragma_Reviewable
:
514 if (write_symbols
== NO_DEBUG
)
515 post_error ("must specify -g?", gnat_node
);
521 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Attribute,
522 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to
523 where we should place the result type. ATTRIBUTE is the attribute ID. */
526 Attribute_to_gnu (Node_Id gnat_node
, tree
*gnu_result_type_p
, int attribute
)
528 tree gnu_result
= error_mark_node
;
529 tree gnu_result_type
;
531 bool prefix_unused
= false;
532 tree gnu_prefix
= gnat_to_gnu (Prefix (gnat_node
));
533 tree gnu_type
= TREE_TYPE (gnu_prefix
);
535 /* If the input is a NULL_EXPR, make a new one. */
536 if (TREE_CODE (gnu_prefix
) == NULL_EXPR
)
538 *gnu_result_type_p
= get_unpadded_type (Etype (gnat_node
));
539 return build1 (NULL_EXPR
, *gnu_result_type_p
,
540 TREE_OPERAND (gnu_prefix
, 0));
547 /* These are just conversions until since representation clauses for
548 enumerations are handled in the front end. */
550 bool checkp
= Do_Range_Check (First (Expressions (gnat_node
)));
552 gnu_result
= gnat_to_gnu (First (Expressions (gnat_node
)));
553 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
554 gnu_result
= convert_with_check (Etype (gnat_node
), gnu_result
,
555 checkp
, checkp
, true);
561 /* These just add or subject the constant 1. Representation clauses for
562 enumerations are handled in the front-end. */
563 gnu_expr
= gnat_to_gnu (First (Expressions (gnat_node
)));
564 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
566 if (Do_Range_Check (First (Expressions (gnat_node
))))
568 gnu_expr
= protect_multiple_eval (gnu_expr
);
571 (build_binary_op (EQ_EXPR
, integer_type_node
,
573 attribute
== Attr_Pred
574 ? TYPE_MIN_VALUE (gnu_result_type
)
575 : TYPE_MAX_VALUE (gnu_result_type
)),
576 gnu_expr
, CE_Range_Check_Failed
);
580 = build_binary_op (attribute
== Attr_Pred
581 ? MINUS_EXPR
: PLUS_EXPR
,
582 gnu_result_type
, gnu_expr
,
583 convert (gnu_result_type
, integer_one_node
));
587 case Attr_Unrestricted_Access
:
588 /* Conversions don't change something's address but can cause us to miss
589 the COMPONENT_REF case below, so strip them off. */
590 gnu_prefix
= remove_conversions (gnu_prefix
,
591 !Must_Be_Byte_Aligned (gnat_node
));
593 /* If we are taking 'Address of an unconstrained object, this is the
594 pointer to the underlying array. */
595 if (attribute
== Attr_Address
)
596 gnu_prefix
= maybe_unconstrained_array (gnu_prefix
);
598 /* ... fall through ... */
601 case Attr_Unchecked_Access
:
602 case Attr_Code_Address
:
603 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
605 = build_unary_op (((attribute
== Attr_Address
606 || attribute
== Attr_Unrestricted_Access
)
607 && !Must_Be_Byte_Aligned (gnat_node
))
608 ? ATTR_ADDR_EXPR
: ADDR_EXPR
,
609 gnu_result_type
, gnu_prefix
);
611 /* For 'Code_Address, find an inner ADDR_EXPR and mark it so that we
612 don't try to build a trampoline. */
613 if (attribute
== Attr_Code_Address
)
615 for (gnu_expr
= gnu_result
;
616 TREE_CODE (gnu_expr
) == NOP_EXPR
617 || TREE_CODE (gnu_expr
) == CONVERT_EXPR
;
618 gnu_expr
= TREE_OPERAND (gnu_expr
, 0))
619 TREE_CONSTANT (gnu_expr
) = 1;
621 if (TREE_CODE (gnu_expr
) == ADDR_EXPR
)
622 TREE_STATIC (gnu_expr
) = TREE_CONSTANT (gnu_expr
) = 1;
626 case Attr_Pool_Address
:
629 tree gnu_ptr
= gnu_prefix
;
631 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
633 /* If this is an unconstrained array, we know the object must have been
634 allocated with the template in front of the object. So compute the
636 if (TYPE_FAT_POINTER_P (TREE_TYPE (gnu_ptr
)))
638 = convert (build_pointer_type
639 (TYPE_OBJECT_RECORD_TYPE
640 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr
)))),
643 gnu_obj_type
= TREE_TYPE (TREE_TYPE (gnu_ptr
));
644 if (TREE_CODE (gnu_obj_type
) == RECORD_TYPE
645 && TYPE_CONTAINS_TEMPLATE_P (gnu_obj_type
))
647 tree gnu_char_ptr_type
= build_pointer_type (char_type_node
);
648 tree gnu_pos
= byte_position (TYPE_FIELDS (gnu_obj_type
));
650 = convert (gnu_char_ptr_type
,
651 size_diffop (size_zero_node
, gnu_pos
));
653 gnu_ptr
= convert (gnu_char_ptr_type
, gnu_ptr
);
654 gnu_ptr
= build_binary_op (MINUS_EXPR
, gnu_char_ptr_type
,
655 gnu_ptr
, gnu_byte_offset
);
658 gnu_result
= convert (gnu_result_type
, gnu_ptr
);
663 case Attr_Object_Size
:
664 case Attr_Value_Size
:
665 case Attr_Max_Size_In_Storage_Elements
:
666 gnu_expr
= gnu_prefix
;
668 /* Remove NOPS from gnu_expr and conversions from gnu_prefix.
669 We only use GNU_EXPR to see if a COMPONENT_REF was involved. */
670 while (TREE_CODE (gnu_expr
) == NOP_EXPR
)
671 gnu_expr
= TREE_OPERAND (gnu_expr
, 0)
674 gnu_prefix
= remove_conversions (gnu_prefix
, true);
675 prefix_unused
= true;
676 gnu_type
= TREE_TYPE (gnu_prefix
);
678 /* Replace an unconstrained array type with the type of the underlying
679 array. We can't do this with a call to maybe_unconstrained_array
680 since we may have a TYPE_DECL. For 'Max_Size_In_Storage_Elements,
681 use the record type that will be used to allocate the object and its
683 if (TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
)
685 gnu_type
= TYPE_OBJECT_RECORD_TYPE (gnu_type
);
686 if (attribute
!= Attr_Max_Size_In_Storage_Elements
)
687 gnu_type
= TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type
)));
690 /* If we're looking for the size of a field, return the field size.
691 Otherwise, if the prefix is an object, or if 'Object_Size or
692 'Max_Size_In_Storage_Elements has been specified, the result is the
693 GCC size of the type. Otherwise, the result is the RM_Size of the
695 if (TREE_CODE (gnu_prefix
) == COMPONENT_REF
)
696 gnu_result
= DECL_SIZE (TREE_OPERAND (gnu_prefix
, 1));
697 else if (TREE_CODE (gnu_prefix
) != TYPE_DECL
698 || attribute
== Attr_Object_Size
699 || attribute
== Attr_Max_Size_In_Storage_Elements
)
701 /* If this is a padded type, the GCC size isn't relevant to the
702 programmer. Normally, what we want is the RM_Size, which was set
703 from the specified size, but if it was not set, we want the size
704 of the relevant field. Using the MAX of those two produces the
705 right result in all case. Don't use the size of the field if it's
706 a self-referential type, since that's never what's wanted. */
707 if (TREE_CODE (gnu_type
) == RECORD_TYPE
708 && TYPE_IS_PADDING_P (gnu_type
)
709 && TREE_CODE (gnu_expr
) == COMPONENT_REF
)
711 gnu_result
= rm_size (gnu_type
);
712 if (!(CONTAINS_PLACEHOLDER_P
713 (DECL_SIZE (TREE_OPERAND (gnu_expr
, 1)))))
715 = size_binop (MAX_EXPR
, gnu_result
,
716 DECL_SIZE (TREE_OPERAND (gnu_expr
, 1)));
719 gnu_result
= TYPE_SIZE (gnu_type
);
722 gnu_result
= rm_size (gnu_type
);
724 gcc_assert (gnu_result
);
726 /* Deal with a self-referential size by returning the maximum size for a
727 type and by qualifying the size with the object for 'Size of an
729 if (CONTAINS_PLACEHOLDER_P (gnu_result
))
731 if (TREE_CODE (gnu_prefix
) != TYPE_DECL
)
732 gnu_result
= substitute_placeholder_in_expr (gnu_result
, gnu_expr
);
734 gnu_result
= max_size (gnu_result
, true);
737 /* If the type contains a template, subtract its size. */
738 if (TREE_CODE (gnu_type
) == RECORD_TYPE
739 && TYPE_CONTAINS_TEMPLATE_P (gnu_type
))
740 gnu_result
= size_binop (MINUS_EXPR
, gnu_result
,
741 DECL_SIZE (TYPE_FIELDS (gnu_type
)));
743 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
745 /* Always perform division using unsigned arithmetic as the size cannot
746 be negative, but may be an overflowed positive value. This provides
747 correct results for sizes up to 512 MB.
749 ??? Size should be calculated in storage elements directly. */
751 if (attribute
== Attr_Max_Size_In_Storage_Elements
)
752 gnu_result
= convert (sizetype
,
753 fold (build2 (CEIL_DIV_EXPR
, bitsizetype
,
754 gnu_result
, bitsize_unit_node
)));
758 if (TREE_CODE (gnu_prefix
) == COMPONENT_REF
759 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_prefix
, 0)))
761 && (TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix
, 0)))))
762 gnu_prefix
= TREE_OPERAND (gnu_prefix
, 0);
764 gnu_type
= TREE_TYPE (gnu_prefix
);
765 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
766 prefix_unused
= true;
768 if (TREE_CODE (gnu_prefix
) == COMPONENT_REF
)
769 gnu_result
= size_int (DECL_ALIGN (TREE_OPERAND (gnu_prefix
, 1)));
771 gnu_result
= size_int (TYPE_ALIGN (gnu_type
) / BITS_PER_UNIT
);
776 case Attr_Range_Length
:
777 prefix_unused
= true;
779 if (INTEGRAL_TYPE_P (gnu_type
) || TREE_CODE (gnu_type
) == REAL_TYPE
)
781 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
783 if (attribute
== Attr_First
)
784 gnu_result
= TYPE_MIN_VALUE (gnu_type
);
785 else if (attribute
== Attr_Last
)
786 gnu_result
= TYPE_MAX_VALUE (gnu_type
);
790 (MAX_EXPR
, get_base_type (gnu_result_type
),
792 (PLUS_EXPR
, get_base_type (gnu_result_type
),
793 build_binary_op (MINUS_EXPR
,
794 get_base_type (gnu_result_type
),
795 convert (gnu_result_type
,
796 TYPE_MAX_VALUE (gnu_type
)),
797 convert (gnu_result_type
,
798 TYPE_MIN_VALUE (gnu_type
))),
799 convert (gnu_result_type
, integer_one_node
)),
800 convert (gnu_result_type
, integer_zero_node
));
805 /* ... fall through ... */
809 int Dimension
= (Present (Expressions (gnat_node
))
810 ? UI_To_Int (Intval (First (Expressions (gnat_node
))))
813 /* Make sure any implicit dereference gets done. */
814 gnu_prefix
= maybe_implicit_deref (gnu_prefix
);
815 gnu_prefix
= maybe_unconstrained_array (gnu_prefix
);
816 gnu_type
= TREE_TYPE (gnu_prefix
);
817 prefix_unused
= true;
818 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
820 if (TYPE_CONVENTION_FORTRAN_P (gnu_type
))
825 for (ndim
= 1, gnu_type_temp
= gnu_type
;
826 TREE_CODE (TREE_TYPE (gnu_type_temp
)) == ARRAY_TYPE
827 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type_temp
));
828 ndim
++, gnu_type_temp
= TREE_TYPE (gnu_type_temp
))
831 Dimension
= ndim
+ 1 - Dimension
;
834 for (; Dimension
> 1; Dimension
--)
835 gnu_type
= TREE_TYPE (gnu_type
);
837 gcc_assert (TREE_CODE (gnu_type
) == ARRAY_TYPE
);
838 if (attribute
== Attr_First
)
840 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
)));
841 else if (attribute
== Attr_Last
)
843 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
)));
845 /* 'Length or 'Range_Length. */
847 tree gnu_compute_type
848 = gnat_signed_or_unsigned_type (0,
849 get_base_type (gnu_result_type
));
853 (MAX_EXPR
, gnu_compute_type
,
855 (PLUS_EXPR
, gnu_compute_type
,
857 (MINUS_EXPR
, gnu_compute_type
,
858 convert (gnu_compute_type
,
860 (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
)))),
861 convert (gnu_compute_type
,
863 (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
))))),
864 convert (gnu_compute_type
, integer_one_node
)),
865 convert (gnu_compute_type
, integer_zero_node
));
868 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
869 handling. Note that these attributes could not have been used on
870 an unconstrained array type. */
871 gnu_result
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result
,
876 case Attr_Bit_Position
:
882 HOST_WIDE_INT bitsize
;
883 HOST_WIDE_INT bitpos
;
885 tree gnu_field_bitpos
;
886 tree gnu_field_offset
;
888 enum machine_mode mode
;
889 int unsignedp
, volatilep
;
891 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
892 gnu_prefix
= remove_conversions (gnu_prefix
, true);
893 prefix_unused
= true;
895 /* We can have 'Bit on any object, but if it isn't a COMPONENT_REF,
896 the result is 0. Don't allow 'Bit on a bare component, though. */
897 if (attribute
== Attr_Bit
898 && TREE_CODE (gnu_prefix
) != COMPONENT_REF
899 && TREE_CODE (gnu_prefix
) != FIELD_DECL
)
901 gnu_result
= integer_zero_node
;
906 gcc_assert (TREE_CODE (gnu_prefix
) == COMPONENT_REF
907 || (attribute
== Attr_Bit_Position
908 && TREE_CODE (gnu_prefix
) == FIELD_DECL
));
910 get_inner_reference (gnu_prefix
, &bitsize
, &bitpos
, &gnu_offset
,
911 &mode
, &unsignedp
, &volatilep
, false);
913 if (TREE_CODE (gnu_prefix
) == COMPONENT_REF
)
915 gnu_field_bitpos
= bit_position (TREE_OPERAND (gnu_prefix
, 1));
916 gnu_field_offset
= byte_position (TREE_OPERAND (gnu_prefix
, 1));
918 for (gnu_inner
= TREE_OPERAND (gnu_prefix
, 0);
919 TREE_CODE (gnu_inner
) == COMPONENT_REF
920 && DECL_INTERNAL_P (TREE_OPERAND (gnu_inner
, 1));
921 gnu_inner
= TREE_OPERAND (gnu_inner
, 0))
924 = size_binop (PLUS_EXPR
, gnu_field_bitpos
,
925 bit_position (TREE_OPERAND (gnu_inner
, 1)));
927 = size_binop (PLUS_EXPR
, gnu_field_offset
,
928 byte_position (TREE_OPERAND (gnu_inner
, 1)));
931 else if (TREE_CODE (gnu_prefix
) == FIELD_DECL
)
933 gnu_field_bitpos
= bit_position (gnu_prefix
);
934 gnu_field_offset
= byte_position (gnu_prefix
);
938 gnu_field_bitpos
= bitsize_zero_node
;
939 gnu_field_offset
= size_zero_node
;
945 gnu_result
= gnu_field_offset
;
950 gnu_result
= size_int (bitpos
% BITS_PER_UNIT
);
954 gnu_result
= bitsize_int (bitpos
% BITS_PER_UNIT
);
955 gnu_result
= size_binop (PLUS_EXPR
, gnu_result
,
956 TYPE_SIZE (TREE_TYPE (gnu_prefix
)));
957 gnu_result
= size_binop (MINUS_EXPR
, gnu_result
,
961 case Attr_Bit_Position
:
962 gnu_result
= gnu_field_bitpos
;
966 /* If this has a PLACEHOLDER_EXPR, qualify it by the object
968 gnu_result
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result
, gnu_prefix
);
975 tree gnu_lhs
= gnat_to_gnu (First (Expressions (gnat_node
)));
976 tree gnu_rhs
= gnat_to_gnu (Next (First (Expressions (gnat_node
))));
978 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
979 gnu_result
= build_binary_op (attribute
== Attr_Min
980 ? MIN_EXPR
: MAX_EXPR
,
981 gnu_result_type
, gnu_lhs
, gnu_rhs
);
985 case Attr_Passed_By_Reference
:
986 gnu_result
= size_int (default_pass_by_ref (gnu_type
)
987 || must_pass_by_ref (gnu_type
));
988 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
991 case Attr_Component_Size
:
992 if (TREE_CODE (gnu_prefix
) == COMPONENT_REF
993 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_prefix
, 0)))
995 && (TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix
, 0)))))
996 gnu_prefix
= TREE_OPERAND (gnu_prefix
, 0);
998 gnu_prefix
= maybe_implicit_deref (gnu_prefix
);
999 gnu_type
= TREE_TYPE (gnu_prefix
);
1001 if (TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
)
1002 gnu_type
= TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type
))));
1004 while (TREE_CODE (TREE_TYPE (gnu_type
)) == ARRAY_TYPE
1005 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type
)))
1006 gnu_type
= TREE_TYPE (gnu_type
);
1008 gcc_assert (TREE_CODE (gnu_type
) == ARRAY_TYPE
);
1010 /* Note this size cannot be self-referential. */
1011 gnu_result
= TYPE_SIZE (TREE_TYPE (gnu_type
));
1012 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1013 prefix_unused
= true;
1016 case Attr_Null_Parameter
:
1017 /* This is just a zero cast to the pointer type for
1018 our prefix and dereferenced. */
1019 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1021 = build_unary_op (INDIRECT_REF
, NULL_TREE
,
1022 convert (build_pointer_type (gnu_result_type
),
1023 integer_zero_node
));
1024 TREE_PRIVATE (gnu_result
) = 1;
1027 case Attr_Mechanism_Code
:
1030 Entity_Id gnat_obj
= Entity (Prefix (gnat_node
));
1032 prefix_unused
= true;
1033 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1034 if (Present (Expressions (gnat_node
)))
1036 int i
= UI_To_Int (Intval (First (Expressions (gnat_node
))));
1038 for (gnat_obj
= First_Formal (gnat_obj
); i
> 1;
1039 i
--, gnat_obj
= Next_Formal (gnat_obj
))
1043 code
= Mechanism (gnat_obj
);
1044 if (code
== Default
)
1045 code
= ((present_gnu_tree (gnat_obj
)
1046 && (DECL_BY_REF_P (get_gnu_tree (gnat_obj
))
1047 || ((TREE_CODE (get_gnu_tree (gnat_obj
))
1049 && (DECL_BY_COMPONENT_PTR_P
1050 (get_gnu_tree (gnat_obj
))))))
1051 ? By_Reference
: By_Copy
);
1052 gnu_result
= convert (gnu_result_type
, size_int (- code
));
1057 /* Say we have an unimplemented attribute. Then set the value to be
1058 returned to be a zero and hope that's something we can convert to the
1059 type of this attribute. */
1060 post_error ("unimplemented attribute", gnat_node
);
1061 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1062 gnu_result
= integer_zero_node
;
1066 /* If this is an attribute where the prefix was unused, force a use of it if
1067 it has a side-effect. But don't do it if the prefix is just an entity
1068 name. However, if an access check is needed, we must do it. See second
1069 example in AARM 11.6(5.e). */
1070 if (prefix_unused
&& TREE_SIDE_EFFECTS (gnu_prefix
)
1071 && !Is_Entity_Name (Prefix (gnat_node
)))
1072 gnu_result
= fold (build2 (COMPOUND_EXPR
, TREE_TYPE (gnu_result
),
1073 gnu_prefix
, gnu_result
));
1075 *gnu_result_type_p
= gnu_result_type
;
1079 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Case_Statement,
1080 to a GCC tree, which is returned. */
1083 Case_Statement_to_gnu (Node_Id gnat_node
)
1089 gnu_expr
= gnat_to_gnu (Expression (gnat_node
));
1090 gnu_expr
= convert (get_base_type (TREE_TYPE (gnu_expr
)), gnu_expr
);
1092 /* The range of values in a case statement is determined by the rules in
1093 RM 5.4(7-9). In almost all cases, this range is represented by the Etype
1094 of the expression. One exception arises in the case of a simple name that
1095 is parenthesized. This still has the Etype of the name, but since it is
1096 not a name, para 7 does not apply, and we need to go to the base type.
1097 This is the only case where parenthesization affects the dynamic
1098 semantics (i.e. the range of possible values at runtime that is covered
1099 by the others alternative.
1101 Another exception is if the subtype of the expression is non-static. In
1102 that case, we also have to use the base type. */
1103 if (Paren_Count (Expression (gnat_node
)) != 0
1104 || !Is_OK_Static_Subtype (Underlying_Type
1105 (Etype (Expression (gnat_node
)))))
1106 gnu_expr
= convert (get_base_type (TREE_TYPE (gnu_expr
)), gnu_expr
);
1108 /* We build a SWITCH_EXPR that contains the code with interspersed
1109 CASE_LABEL_EXPRs for each label. */
1111 push_stack (&gnu_switch_label_stack
, NULL_TREE
, create_artificial_label ());
1112 start_stmt_group ();
1113 for (gnat_when
= First_Non_Pragma (Alternatives (gnat_node
));
1114 Present (gnat_when
);
1115 gnat_when
= Next_Non_Pragma (gnat_when
))
1117 Node_Id gnat_choice
;
1119 /* First compile all the different case choices for the current WHEN
1121 for (gnat_choice
= First (Discrete_Choices (gnat_when
));
1122 Present (gnat_choice
); gnat_choice
= Next (gnat_choice
))
1124 tree gnu_low
= NULL_TREE
, gnu_high
= NULL_TREE
;
1126 switch (Nkind (gnat_choice
))
1129 gnu_low
= gnat_to_gnu (Low_Bound (gnat_choice
));
1130 gnu_high
= gnat_to_gnu (High_Bound (gnat_choice
));
1133 case N_Subtype_Indication
:
1134 gnu_low
= gnat_to_gnu (Low_Bound (Range_Expression
1135 (Constraint (gnat_choice
))));
1136 gnu_high
= gnat_to_gnu (High_Bound (Range_Expression
1137 (Constraint (gnat_choice
))));
1141 case N_Expanded_Name
:
1142 /* This represents either a subtype range or a static value of
1143 some kind; Ekind says which. If a static value, fall through
1144 to the next case. */
1145 if (IN (Ekind (Entity (gnat_choice
)), Type_Kind
))
1147 tree gnu_type
= get_unpadded_type (Entity (gnat_choice
));
1149 gnu_low
= fold (TYPE_MIN_VALUE (gnu_type
));
1150 gnu_high
= fold (TYPE_MAX_VALUE (gnu_type
));
1154 /* ... fall through ... */
1156 case N_Character_Literal
:
1157 case N_Integer_Literal
:
1158 gnu_low
= gnat_to_gnu (gnat_choice
);
1161 case N_Others_Choice
:
1168 add_stmt_with_node (build3 (CASE_LABEL_EXPR
, void_type_node
,
1170 create_artificial_label ()),
1174 /* Push a binding level here in case variables are declared since we want
1175 them to be local to this set of statements instead of the block
1176 containing the Case statement. */
1177 add_stmt (build_stmt_group (Statements (gnat_when
), true));
1178 add_stmt (build1 (GOTO_EXPR
, void_type_node
,
1179 TREE_VALUE (gnu_switch_label_stack
)));
1182 /* Now emit a definition of the label all the cases branched to. */
1183 add_stmt (build1 (LABEL_EXPR
, void_type_node
,
1184 TREE_VALUE (gnu_switch_label_stack
)));
1185 gnu_result
= build3 (SWITCH_EXPR
, TREE_TYPE (gnu_expr
), gnu_expr
,
1186 end_stmt_group (), NULL_TREE
);
1187 pop_stack (&gnu_switch_label_stack
);
1192 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Loop_Statement,
1193 to a GCC tree, which is returned. */
1196 Loop_Statement_to_gnu (Node_Id gnat_node
)
1198 /* ??? It would be nice to use "build" here, but there's no build5. */
1199 tree gnu_loop_stmt
= build_nt (LOOP_STMT
, NULL_TREE
, NULL_TREE
,
1200 NULL_TREE
, NULL_TREE
, NULL_TREE
);
1201 tree gnu_loop_var
= NULL_TREE
;
1202 Node_Id gnat_iter_scheme
= Iteration_Scheme (gnat_node
);
1203 tree gnu_cond_expr
= NULL_TREE
;
1206 TREE_TYPE (gnu_loop_stmt
) = void_type_node
;
1207 TREE_SIDE_EFFECTS (gnu_loop_stmt
) = 1;
1208 LOOP_STMT_LABEL (gnu_loop_stmt
) = create_artificial_label ();
1209 annotate_with_node (gnu_loop_stmt
, gnat_node
);
1211 /* Save the end label of this LOOP_STMT in a stack so that the corresponding
1212 N_Exit_Statement can find it. */
1213 push_stack (&gnu_loop_label_stack
, NULL_TREE
,
1214 LOOP_STMT_LABEL (gnu_loop_stmt
));
1216 /* Set the condition that under which the loop should continue.
1217 For "LOOP .... END LOOP;" the condition is always true. */
1218 if (No (gnat_iter_scheme
))
1220 /* The case "WHILE condition LOOP ..... END LOOP;" */
1221 else if (Present (Condition (gnat_iter_scheme
)))
1222 LOOP_STMT_TOP_COND (gnu_loop_stmt
)
1223 = gnat_to_gnu (Condition (gnat_iter_scheme
));
1226 /* We have an iteration scheme. */
1227 Node_Id gnat_loop_spec
= Loop_Parameter_Specification (gnat_iter_scheme
);
1228 Entity_Id gnat_loop_var
= Defining_Entity (gnat_loop_spec
);
1229 Entity_Id gnat_type
= Etype (gnat_loop_var
);
1230 tree gnu_type
= get_unpadded_type (gnat_type
);
1231 tree gnu_low
= TYPE_MIN_VALUE (gnu_type
);
1232 tree gnu_high
= TYPE_MAX_VALUE (gnu_type
);
1233 bool reversep
= Reverse_Present (gnat_loop_spec
);
1234 tree gnu_first
= reversep
? gnu_high
: gnu_low
;
1235 tree gnu_last
= reversep
? gnu_low
: gnu_high
;
1236 enum tree_code end_code
= reversep
? GE_EXPR
: LE_EXPR
;
1237 tree gnu_base_type
= get_base_type (gnu_type
);
1238 tree gnu_limit
= (reversep
? TYPE_MIN_VALUE (gnu_base_type
)
1239 : TYPE_MAX_VALUE (gnu_base_type
));
1241 /* We know the loop variable will not overflow if GNU_LAST is a constant
1242 and is not equal to GNU_LIMIT. If it might overflow, we have to move
1243 the limit test to the end of the loop. In that case, we have to test
1244 for an empty loop outside the loop. */
1245 if (TREE_CODE (gnu_last
) != INTEGER_CST
1246 || TREE_CODE (gnu_limit
) != INTEGER_CST
1247 || tree_int_cst_equal (gnu_last
, gnu_limit
))
1250 = build3 (COND_EXPR
, void_type_node
,
1251 build_binary_op (LE_EXPR
, integer_type_node
,
1253 NULL_TREE
, alloc_stmt_list ());
1254 annotate_with_node (gnu_cond_expr
, gnat_loop_spec
);
1257 /* Open a new nesting level that will surround the loop to declare the
1258 loop index variable. */
1259 start_stmt_group ();
1262 /* Declare the loop index and set it to its initial value. */
1263 gnu_loop_var
= gnat_to_gnu_entity (gnat_loop_var
, gnu_first
, 1);
1264 if (DECL_BY_REF_P (gnu_loop_var
))
1265 gnu_loop_var
= build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_loop_var
);
1267 /* The loop variable might be a padded type, so use `convert' to get a
1268 reference to the inner variable if so. */
1269 gnu_loop_var
= convert (get_base_type (gnu_type
), gnu_loop_var
);
1271 /* Set either the top or bottom exit condition as appropriate depending
1272 on whether or not we know an overflow cannot occur. */
1274 LOOP_STMT_BOT_COND (gnu_loop_stmt
)
1275 = build_binary_op (NE_EXPR
, integer_type_node
,
1276 gnu_loop_var
, gnu_last
);
1278 LOOP_STMT_TOP_COND (gnu_loop_stmt
)
1279 = build_binary_op (end_code
, integer_type_node
,
1280 gnu_loop_var
, gnu_last
);
1282 LOOP_STMT_UPDATE (gnu_loop_stmt
)
1283 = build_binary_op (reversep
? PREDECREMENT_EXPR
1284 : PREINCREMENT_EXPR
,
1285 TREE_TYPE (gnu_loop_var
),
1287 convert (TREE_TYPE (gnu_loop_var
),
1289 annotate_with_node (LOOP_STMT_UPDATE (gnu_loop_stmt
),
1293 /* If the loop was named, have the name point to this loop. In this case,
1294 the association is not a ..._DECL node, but the end label from this
1296 if (Present (Identifier (gnat_node
)))
1297 save_gnu_tree (Entity (Identifier (gnat_node
)),
1298 LOOP_STMT_LABEL (gnu_loop_stmt
), true);
1300 /* Make the loop body into its own block, so any allocated storage will be
1301 released every iteration. This is needed for stack allocation. */
1302 LOOP_STMT_BODY (gnu_loop_stmt
)
1303 = build_stmt_group (Statements (gnat_node
), true);
1305 /* If we declared a variable, then we are in a statement group for that
1306 declaration. Add the LOOP_STMT to it and make that the "loop". */
1309 add_stmt (gnu_loop_stmt
);
1311 gnu_loop_stmt
= end_stmt_group ();
1314 /* If we have an outer COND_EXPR, that's our result and this loop is its
1315 "true" statement. Otherwise, the result is the LOOP_STMT. */
1318 COND_EXPR_THEN (gnu_cond_expr
) = gnu_loop_stmt
;
1319 gnu_result
= gnu_cond_expr
;
1320 recalculate_side_effects (gnu_cond_expr
);
1323 gnu_result
= gnu_loop_stmt
;
1325 pop_stack (&gnu_loop_label_stack
);
1330 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Subprogram_Body. We
1331 don't return anything. */
1334 Subprogram_Body_to_gnu (Node_Id gnat_node
)
1336 /* Save debug output mode in case it is reset. */
1337 enum debug_info_type save_write_symbols
= write_symbols
;
1338 const struct gcc_debug_hooks
*const save_debug_hooks
= debug_hooks
;
1339 /* Defining identifier of a parameter to the subprogram. */
1340 Entity_Id gnat_param
;
1341 /* The defining identifier for the subprogram body. Note that if a
1342 specification has appeared before for this body, then the identifier
1343 occurring in that specification will also be a defining identifier and all
1344 the calls to this subprogram will point to that specification. */
1345 Entity_Id gnat_subprog_id
1346 = (Present (Corresponding_Spec (gnat_node
))
1347 ? Corresponding_Spec (gnat_node
) : Defining_Entity (gnat_node
));
1348 /* The FUNCTION_DECL node corresponding to the subprogram spec. */
1349 tree gnu_subprog_decl
;
1350 /* The FUNCTION_TYPE node corresponding to the subprogram spec. */
1351 tree gnu_subprog_type
;
1355 /* If this is a generic object or if it has been eliminated,
1357 if (Ekind (gnat_subprog_id
) == E_Generic_Procedure
1358 || Ekind (gnat_subprog_id
) == E_Generic_Function
1359 || Is_Eliminated (gnat_subprog_id
))
1362 /* If debug information is suppressed for the subprogram, turn debug
1363 mode off for the duration of processing. */
1364 if (!Needs_Debug_Info (gnat_subprog_id
))
1366 write_symbols
= NO_DEBUG
;
1367 debug_hooks
= &do_nothing_debug_hooks
;
1370 /* If this subprogram acts as its own spec, define it. Otherwise, just get
1371 the already-elaborated tree node. However, if this subprogram had its
1372 elaboration deferred, we will already have made a tree node for it. So
1373 treat it as not being defined in that case. Such a subprogram cannot
1374 have an address clause or a freeze node, so this test is safe, though it
1375 does disable some otherwise-useful error checking. */
1377 = gnat_to_gnu_entity (gnat_subprog_id
, NULL_TREE
,
1378 Acts_As_Spec (gnat_node
)
1379 && !present_gnu_tree (gnat_subprog_id
));
1381 gnu_subprog_type
= TREE_TYPE (gnu_subprog_decl
);
1383 /* Set the line number in the decl to correspond to that of the body so that
1384 the line number notes are written
1386 Sloc_to_locus (Sloc (gnat_node
), &DECL_SOURCE_LOCATION (gnu_subprog_decl
));
1388 begin_subprog_body (gnu_subprog_decl
);
1389 gnu_cico_list
= TYPE_CI_CO_LIST (gnu_subprog_type
);
1391 /* If there are OUT parameters, we need to ensure that the return statement
1392 properly copies them out. We do this by making a new block and converting
1393 any inner return into a goto to a label at the end of the block. */
1394 push_stack (&gnu_return_label_stack
, NULL_TREE
,
1395 gnu_cico_list
? create_artificial_label () : NULL_TREE
);
1397 /* Get a tree corresponding to the code for the subprogram. */
1398 start_stmt_group ();
1401 /* See if there are any parameters for which we don't yet have GCC entities.
1402 These must be for OUT parameters for which we will be making VAR_DECL
1403 nodes here. Fill them in to TYPE_CI_CO_LIST, which must contain the empty
1404 entry as well. We can match up the entries because TYPE_CI_CO_LIST is in
1405 the order of the parameters. */
1406 for (gnat_param
= First_Formal (gnat_subprog_id
);
1407 Present (gnat_param
);
1408 gnat_param
= Next_Formal_With_Extras (gnat_param
))
1409 if (!present_gnu_tree (gnat_param
))
1411 /* Skip any entries that have been already filled in; they must
1412 correspond to IN OUT parameters. */
1413 for (; gnu_cico_list
&& TREE_VALUE (gnu_cico_list
);
1414 gnu_cico_list
= TREE_CHAIN (gnu_cico_list
))
1417 /* Do any needed references for padded types. */
1418 TREE_VALUE (gnu_cico_list
)
1419 = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_list
)),
1420 gnat_to_gnu_entity (gnat_param
, NULL_TREE
, 1));
1423 process_decls (Declarations (gnat_node
), Empty
, Empty
, true, true);
1425 /* Generate the code of the subprogram itself. A return statement will be
1426 present and any OUT parameters will be handled there. */
1427 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node
)));
1429 gnu_result
= end_stmt_group ();
1431 /* If we made a special return label, we need to make a block that contains
1432 the definition of that label and the copying to the return value. That
1433 block first contains the function, then the label and copy statement. */
1434 if (TREE_VALUE (gnu_return_label_stack
))
1438 start_stmt_group ();
1440 add_stmt (gnu_result
);
1441 add_stmt (build1 (LABEL_EXPR
, void_type_node
,
1442 TREE_VALUE (gnu_return_label_stack
)));
1444 gnu_cico_list
= TYPE_CI_CO_LIST (gnu_subprog_type
);
1445 if (list_length (gnu_cico_list
) == 1)
1446 gnu_retval
= TREE_VALUE (gnu_cico_list
);
1448 gnu_retval
= gnat_build_constructor (TREE_TYPE (gnu_subprog_type
),
1451 if (DECL_P (gnu_retval
) && DECL_BY_REF_P (gnu_retval
))
1452 gnu_retval
= build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_retval
);
1455 (build1 (RETURN_EXPR
, void_type_node
,
1456 build2 (MODIFY_EXPR
, TREE_TYPE (gnu_retval
),
1457 DECL_RESULT (current_function_decl
), gnu_retval
)),
1460 gnu_result
= end_stmt_group ();
1463 pop_stack (&gnu_return_label_stack
);
1465 /* Initialize the information node for the function and set the
1467 allocate_struct_function (current_function_decl
);
1469 ((Present (End_Label (Handled_Statement_Sequence (gnat_node
)))
1470 ? Sloc (End_Label (Handled_Statement_Sequence (gnat_node
)))
1471 : Sloc (gnat_node
)),
1472 &cfun
->function_end_locus
);
1474 end_subprog_body (gnu_result
);
1476 /* Disconnect the trees for parameters that we made variables for from the
1477 GNAT entities since these are unusable after we end the function. */
1478 for (gnat_param
= First_Formal (gnat_subprog_id
);
1479 Present (gnat_param
);
1480 gnat_param
= Next_Formal_With_Extras (gnat_param
))
1481 if (TREE_CODE (get_gnu_tree (gnat_param
)) == VAR_DECL
)
1482 save_gnu_tree (gnat_param
, NULL_TREE
, false);
1484 mark_out_of_scope (Defining_Unit_Name (Specification (gnat_node
)));
1485 write_symbols
= save_write_symbols
;
1486 debug_hooks
= save_debug_hooks
;
1489 /* Subroutine of gnat_to_gnu to translate gnat_node, either an N_Function_Call
1490 or an N_Procedure_Call_Statement, to a GCC tree, which is returned.
1491 GNU_RESULT_TYPE_P is a pointer to where we should place the result type.
1492 If GNU_TARGET is non-null, this must be a function call and the result
1493 of the call is to be placed into that object. */
1496 call_to_gnu (Node_Id gnat_node
, tree
*gnu_result_type_p
, tree gnu_target
)
1499 /* The GCC node corresponding to the GNAT subprogram name. This can either
1500 be a FUNCTION_DECL node if we are dealing with a standard subprogram call,
1501 or an indirect reference expression (an INDIRECT_REF node) pointing to a
1503 tree gnu_subprog_node
= gnat_to_gnu (Name (gnat_node
));
1504 /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */
1505 tree gnu_subprog_type
= TREE_TYPE (gnu_subprog_node
);
1506 tree gnu_subprog_addr
= build_unary_op (ADDR_EXPR
, NULL_TREE
,
1508 Entity_Id gnat_formal
;
1509 Node_Id gnat_actual
;
1510 tree gnu_actual_list
= NULL_TREE
;
1511 tree gnu_name_list
= NULL_TREE
;
1512 tree gnu_before_list
= NULL_TREE
;
1513 tree gnu_after_list
= NULL_TREE
;
1514 tree gnu_subprog_call
;
1516 switch (Nkind (Name (gnat_node
)))
1519 case N_Operator_Symbol
:
1520 case N_Expanded_Name
:
1521 case N_Attribute_Reference
:
1522 if (Is_Eliminated (Entity (Name (gnat_node
))))
1523 Eliminate_Error_Msg (gnat_node
, Entity (Name (gnat_node
)));
1526 gcc_assert (TREE_CODE (gnu_subprog_type
) == FUNCTION_TYPE
);
1528 /* If we are calling a stubbed function, make this into a raise of
1529 Program_Error. Elaborate all our args first. */
1530 if (TREE_CODE (gnu_subprog_node
) == FUNCTION_DECL
1531 && DECL_STUBBED_P (gnu_subprog_node
))
1533 for (gnat_actual
= First_Actual (gnat_node
);
1534 Present (gnat_actual
);
1535 gnat_actual
= Next_Actual (gnat_actual
))
1536 add_stmt (gnat_to_gnu (gnat_actual
));
1538 if (Nkind (gnat_node
) == N_Function_Call
&& !gnu_target
)
1540 *gnu_result_type_p
= TREE_TYPE (gnu_subprog_type
);
1541 return build1 (NULL_EXPR
, *gnu_result_type_p
,
1542 build_call_raise (PE_Stubbed_Subprogram_Called
));
1545 return build_call_raise (PE_Stubbed_Subprogram_Called
);
1548 /* If we are calling by supplying a pointer to a target, set up that
1549 pointer as the first argument. Use GNU_TARGET if one was passed;
1550 otherwise, make a target by building a variable of the maximum size
1552 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type
))
1554 tree gnu_real_ret_type
1555 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (gnu_subprog_type
)));
1560 = maybe_pad_type (gnu_real_ret_type
,
1561 max_size (TYPE_SIZE (gnu_real_ret_type
), true),
1562 0, Etype (Name (gnat_node
)), "PAD", false,
1565 gnu_target
= create_tmp_var_raw (gnu_obj_type
, "LR");
1566 gnat_pushdecl (gnu_target
, gnat_node
);
1570 = tree_cons (NULL_TREE
,
1571 build_unary_op (ADDR_EXPR
, NULL_TREE
,
1572 unchecked_convert (gnu_real_ret_type
,
1579 /* The only way we can be making a call via an access type is if Name is an
1580 explicit dereference. In that case, get the list of formal args from the
1581 type the access type is pointing to. Otherwise, get the formals from
1582 entity being called. */
1583 if (Nkind (Name (gnat_node
)) == N_Explicit_Dereference
)
1584 gnat_formal
= First_Formal (Etype (Name (gnat_node
)));
1585 else if (Nkind (Name (gnat_node
)) == N_Attribute_Reference
)
1586 /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */
1589 gnat_formal
= First_Formal (Entity (Name (gnat_node
)));
1591 /* Create the list of the actual parameters as GCC expects it, namely a chain
1592 of TREE_LIST nodes in which the TREE_VALUE field of each node is a
1593 parameter-expression and the TREE_PURPOSE field is null. Skip OUT
1594 parameters not passed by reference and don't need to be copied in. */
1595 for (gnat_actual
= First_Actual (gnat_node
);
1596 Present (gnat_actual
);
1597 gnat_formal
= Next_Formal_With_Extras (gnat_formal
),
1598 gnat_actual
= Next_Actual (gnat_actual
))
1601 = (present_gnu_tree (gnat_formal
)
1602 ? get_gnu_tree (gnat_formal
) : NULL_TREE
);
1603 /* We treat a conversion between aggregate types as if it is an
1604 unchecked conversion. */
1605 bool unchecked_convert_p
1606 = (Nkind (gnat_actual
) == N_Unchecked_Type_Conversion
1607 || (Nkind (gnat_actual
) == N_Type_Conversion
1608 && Is_Composite_Type (Underlying_Type (Etype (gnat_formal
)))));
1609 Node_Id gnat_name
= (unchecked_convert_p
1610 ? Expression (gnat_actual
) : gnat_actual
);
1611 tree gnu_name
= gnat_to_gnu (gnat_name
);
1612 tree gnu_name_type
= gnat_to_gnu_type (Etype (gnat_name
));
1614 tree gnu_formal_type
;
1616 /* If it's possible we may need to use this expression twice, make sure
1617 than any side-effects are handled via SAVE_EXPRs. Likewise if we need
1618 to force side-effects before the call.
1620 ??? This is more conservative than we need since we don't need to do
1621 this for pass-by-ref with no conversion. If we are passing a
1622 non-addressable Out or In Out parameter by reference, pass the address
1623 of a copy and set up to copy back out after the call. */
1624 if (Ekind (gnat_formal
) != E_In_Parameter
)
1626 gnu_name
= gnat_stabilize_reference (gnu_name
, true);
1627 if (!addressable_p (gnu_name
)
1629 && (DECL_BY_REF_P (gnu_formal
)
1630 || (TREE_CODE (gnu_formal
) == PARM_DECL
1631 && (DECL_BY_COMPONENT_PTR_P (gnu_formal
)
1632 || (DECL_BY_DESCRIPTOR_P (gnu_formal
))))))
1634 tree gnu_copy
= gnu_name
;
1637 /* For users of Starlet we issue a warning because the
1638 interface apparently assumes that by-ref parameters
1639 outlive the procedure invocation. The code still
1640 will not work as intended, but we cannot do much
1641 better since other low-level parts of the back-end
1642 would allocate temporaries at will because of the
1643 misalignment if we did not do so here. */
1645 if (Is_Valued_Procedure (Entity (Name (gnat_node
))))
1648 ("?possible violation of implicit assumption",
1651 ("?made by pragma Import_Valued_Procedure on &",
1652 gnat_actual
, Entity (Name (gnat_node
)));
1654 ("?because of misalignment of &",
1655 gnat_actual
, gnat_formal
);
1658 /* Remove any unpadding on the actual and make a copy. But if
1659 the actual is a justified modular type, first convert
1661 if (TREE_CODE (gnu_name
) == COMPONENT_REF
1662 && ((TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_name
, 0)))
1664 && (TYPE_IS_PADDING_P
1665 (TREE_TYPE (TREE_OPERAND (gnu_name
, 0))))))
1666 gnu_name
= gnu_copy
= TREE_OPERAND (gnu_name
, 0);
1667 else if (TREE_CODE (gnu_name_type
) == RECORD_TYPE
1668 && (TYPE_JUSTIFIED_MODULAR_P (gnu_name_type
)))
1669 gnu_name
= convert (gnu_name_type
, gnu_name
);
1671 gnu_actual
= save_expr (gnu_name
);
1673 /* Since we're going to take the address of the SAVE_EXPR, we
1674 don't want it to be marked as unchanging. So set
1675 TREE_ADDRESSABLE. */
1676 gnu_temp
= skip_simple_arithmetic (gnu_actual
);
1677 if (TREE_CODE (gnu_temp
) == SAVE_EXPR
)
1679 TREE_ADDRESSABLE (gnu_temp
) = 1;
1680 TREE_READONLY (gnu_temp
) = 0;
1683 /* Set up to move the copy back to the original. */
1684 gnu_temp
= build2 (MODIFY_EXPR
, TREE_TYPE (gnu_copy
),
1685 gnu_copy
, gnu_actual
);
1686 annotate_with_node (gnu_temp
, gnat_actual
);
1687 append_to_statement_list (gnu_temp
, &gnu_after_list
);
1691 /* If this was a procedure call, we may not have removed any padding.
1692 So do it here for the part we will use as an input, if any. */
1693 gnu_actual
= gnu_name
;
1694 if (Ekind (gnat_formal
) != E_Out_Parameter
1695 && TREE_CODE (TREE_TYPE (gnu_actual
)) == RECORD_TYPE
1696 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual
)))
1697 gnu_actual
= convert (get_unpadded_type (Etype (gnat_actual
)),
1700 /* Unless this is an In parameter, we must remove any LJM building
1702 if (Ekind (gnat_formal
) != E_In_Parameter
1703 && TREE_CODE (gnu_name
) == CONSTRUCTOR
1704 && TREE_CODE (TREE_TYPE (gnu_name
)) == RECORD_TYPE
1705 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (gnu_name
)))
1706 gnu_name
= convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_name
))),
1709 if (Ekind (gnat_formal
) != E_Out_Parameter
1710 && !unchecked_convert_p
1711 && Do_Range_Check (gnat_actual
))
1712 gnu_actual
= emit_range_check (gnu_actual
, Etype (gnat_formal
));
1714 /* Do any needed conversions. We need only check for unchecked
1715 conversion since normal conversions will be handled by just
1716 converting to the formal type. */
1717 if (unchecked_convert_p
)
1720 = unchecked_convert (gnat_to_gnu_type (Etype (gnat_actual
)),
1722 (Nkind (gnat_actual
)
1723 == N_Unchecked_Type_Conversion
)
1724 && No_Truncation (gnat_actual
));
1726 /* One we've done the unchecked conversion, we still must ensure that
1727 the object is in range of the formal's type. */
1728 if (Ekind (gnat_formal
) != E_Out_Parameter
1729 && Do_Range_Check (gnat_actual
))
1730 gnu_actual
= emit_range_check (gnu_actual
,
1731 Etype (gnat_formal
));
1733 else if (TREE_CODE (gnu_actual
) != SAVE_EXPR
)
1734 /* We may have suppressed a conversion to the Etype of the actual since
1735 the parent is a procedure call. So add the conversion here. */
1736 gnu_actual
= convert (gnat_to_gnu_type (Etype (gnat_actual
)),
1739 /* If we have not saved a GCC object for the formal, it means it is an
1740 OUT parameter not passed by reference and that does not need to be
1741 copied in. Otherwise, look at the PARM_DECL to see if it is passed by
1744 && TREE_CODE (gnu_formal
) == PARM_DECL
&& DECL_BY_REF_P (gnu_formal
))
1746 if (Ekind (gnat_formal
) != E_In_Parameter
)
1748 gnu_actual
= gnu_name
;
1750 /* If we have a padded type, be sure we've removed padding. */
1751 if (TREE_CODE (TREE_TYPE (gnu_actual
)) == RECORD_TYPE
1752 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual
))
1753 && TREE_CODE (gnu_actual
) != SAVE_EXPR
)
1754 gnu_actual
= convert (get_unpadded_type (Etype (gnat_actual
)),
1757 /* If we have the constructed subtype of an aliased object
1758 with an unconstrained nominal subtype, the type of the
1759 actual includes the template, although it is formally
1760 constrained. So we need to convert it back to the real
1761 constructed subtype to retrieve the constrained part
1762 and takes its address. */
1763 if (TREE_CODE (TREE_TYPE (gnu_actual
)) == RECORD_TYPE
1764 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual
))
1765 && TREE_CODE (gnu_actual
) != SAVE_EXPR
1766 && Is_Constr_Subt_For_UN_Aliased (Etype (gnat_actual
))
1767 && Is_Array_Type (Etype (gnat_actual
)))
1768 gnu_actual
= convert (gnat_to_gnu_type (Etype (gnat_actual
)),
1772 /* Otherwise, if we have a non-addressable COMPONENT_REF of a
1773 variable-size type see if it's doing a unpadding operation. If
1774 so, remove that operation since we have no way of allocating the
1775 required temporary. */
1776 if (TREE_CODE (gnu_actual
) == COMPONENT_REF
1777 && !TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual
)))
1778 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_actual
, 0)))
1780 && TYPE_IS_PADDING_P (TREE_TYPE
1781 (TREE_OPERAND (gnu_actual
, 0)))
1782 && !addressable_p (gnu_actual
))
1783 gnu_actual
= TREE_OPERAND (gnu_actual
, 0);
1785 /* The symmetry of the paths to the type of an entity is broken here
1786 since arguments don't know that they will be passed by ref. */
1787 gnu_formal_type
= TREE_TYPE (get_gnu_tree (gnat_formal
));
1788 gnu_actual
= build_unary_op (ADDR_EXPR
, gnu_formal_type
, gnu_actual
);
1790 else if (gnu_formal
&& TREE_CODE (gnu_formal
) == PARM_DECL
1791 && DECL_BY_COMPONENT_PTR_P (gnu_formal
))
1793 gnu_formal_type
= TREE_TYPE (get_gnu_tree (gnat_formal
));
1794 gnu_actual
= maybe_implicit_deref (gnu_actual
);
1795 gnu_actual
= maybe_unconstrained_array (gnu_actual
);
1797 if (TREE_CODE (gnu_formal_type
) == RECORD_TYPE
1798 && TYPE_IS_PADDING_P (gnu_formal_type
))
1800 gnu_formal_type
= TREE_TYPE (TYPE_FIELDS (gnu_formal_type
));
1801 gnu_actual
= convert (gnu_formal_type
, gnu_actual
);
1804 /* Take the address of the object and convert to the proper pointer
1805 type. We'd like to actually compute the address of the beginning
1806 of the array using an ADDR_EXPR of an ARRAY_REF, but there's a
1807 possibility that the ARRAY_REF might return a constant and we'd be
1808 getting the wrong address. Neither approach is exactly correct,
1809 but this is the most likely to work in all cases. */
1810 gnu_actual
= convert (gnu_formal_type
,
1811 build_unary_op (ADDR_EXPR
, NULL_TREE
,
1814 else if (gnu_formal
&& TREE_CODE (gnu_formal
) == PARM_DECL
1815 && DECL_BY_DESCRIPTOR_P (gnu_formal
))
1817 /* If arg is 'Null_Parameter, pass zero descriptor. */
1818 if ((TREE_CODE (gnu_actual
) == INDIRECT_REF
1819 || TREE_CODE (gnu_actual
) == UNCONSTRAINED_ARRAY_REF
)
1820 && TREE_PRIVATE (gnu_actual
))
1821 gnu_actual
= convert (DECL_ARG_TYPE (get_gnu_tree (gnat_formal
)),
1824 gnu_actual
= build_unary_op (ADDR_EXPR
, NULL_TREE
,
1825 fill_vms_descriptor (gnu_actual
,
1830 tree gnu_actual_size
= TYPE_SIZE (TREE_TYPE (gnu_actual
));
1832 if (Ekind (gnat_formal
) != E_In_Parameter
)
1833 gnu_name_list
= tree_cons (NULL_TREE
, gnu_name
, gnu_name_list
);
1835 if (!gnu_formal
|| TREE_CODE (gnu_formal
) != PARM_DECL
)
1838 /* If this is 'Null_Parameter, pass a zero even though we are
1839 dereferencing it. */
1840 else if (TREE_CODE (gnu_actual
) == INDIRECT_REF
1841 && TREE_PRIVATE (gnu_actual
)
1842 && host_integerp (gnu_actual_size
, 1)
1843 && 0 >= compare_tree_int (gnu_actual_size
,
1846 = unchecked_convert (DECL_ARG_TYPE (gnu_formal
),
1847 convert (gnat_type_for_size
1848 (tree_low_cst (gnu_actual_size
, 1),
1853 gnu_actual
= convert (DECL_ARG_TYPE (gnu_formal
), gnu_actual
);
1856 gnu_actual_list
= tree_cons (NULL_TREE
, gnu_actual
, gnu_actual_list
);
1859 gnu_subprog_call
= build3 (CALL_EXPR
, TREE_TYPE (gnu_subprog_type
),
1860 gnu_subprog_addr
, nreverse (gnu_actual_list
),
1863 /* If we return by passing a target, we emit the call and return the target
1865 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type
))
1867 add_stmt_with_node (gnu_subprog_call
, gnat_node
);
1869 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (gnu_subprog_type
)));
1870 return unchecked_convert (*gnu_result_type_p
, gnu_target
, false);
1873 /* If it is a function call, the result is the call expression unless
1874 a target is specified, in which case we copy the result into the target
1875 and return the assignment statement. */
1876 else if (Nkind (gnat_node
) == N_Function_Call
)
1878 gnu_result
= gnu_subprog_call
;
1880 /* If the function returns an unconstrained array or by reference,
1881 we have to de-dereference the pointer. */
1882 if (TYPE_RETURNS_UNCONSTRAINED_P (gnu_subprog_type
)
1883 || TYPE_RETURNS_BY_REF_P (gnu_subprog_type
))
1884 gnu_result
= build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_result
);
1887 gnu_result
= build_binary_op (MODIFY_EXPR
, NULL_TREE
,
1888 gnu_target
, gnu_result
);
1890 *gnu_result_type_p
= get_unpadded_type (Etype (gnat_node
));
1895 /* If this is the case where the GNAT tree contains a procedure call
1896 but the Ada procedure has copy in copy out parameters, the special
1897 parameter passing mechanism must be used. */
1898 else if (TYPE_CI_CO_LIST (gnu_subprog_type
) != NULL_TREE
)
1900 /* List of FIELD_DECLs associated with the PARM_DECLs of the copy
1901 in copy out parameters. */
1902 tree scalar_return_list
= TYPE_CI_CO_LIST (gnu_subprog_type
);
1903 int length
= list_length (scalar_return_list
);
1909 gnu_subprog_call
= save_expr (gnu_subprog_call
);
1910 gnu_name_list
= nreverse (gnu_name_list
);
1912 /* If any of the names had side-effects, ensure they are all
1913 evaluated before the call. */
1914 for (gnu_name
= gnu_name_list
; gnu_name
;
1915 gnu_name
= TREE_CHAIN (gnu_name
))
1916 if (TREE_SIDE_EFFECTS (TREE_VALUE (gnu_name
)))
1917 append_to_statement_list (TREE_VALUE (gnu_name
),
1921 if (Nkind (Name (gnat_node
)) == N_Explicit_Dereference
)
1922 gnat_formal
= First_Formal (Etype (Name (gnat_node
)));
1924 gnat_formal
= First_Formal (Entity (Name (gnat_node
)));
1926 for (gnat_actual
= First_Actual (gnat_node
);
1927 Present (gnat_actual
);
1928 gnat_formal
= Next_Formal_With_Extras (gnat_formal
),
1929 gnat_actual
= Next_Actual (gnat_actual
))
1930 /* If we are dealing with a copy in copy out parameter, we must
1931 retrieve its value from the record returned in the call. */
1932 if (!(present_gnu_tree (gnat_formal
)
1933 && TREE_CODE (get_gnu_tree (gnat_formal
)) == PARM_DECL
1934 && (DECL_BY_REF_P (get_gnu_tree (gnat_formal
))
1935 || (TREE_CODE (get_gnu_tree (gnat_formal
)) == PARM_DECL
1936 && ((DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal
))
1937 || (DECL_BY_DESCRIPTOR_P
1938 (get_gnu_tree (gnat_formal
))))))))
1939 && Ekind (gnat_formal
) != E_In_Parameter
)
1941 /* Get the value to assign to this OUT or IN OUT parameter. It is
1942 either the result of the function if there is only a single such
1943 parameter or the appropriate field from the record returned. */
1945 = length
== 1 ? gnu_subprog_call
1946 : build_component_ref (gnu_subprog_call
, NULL_TREE
,
1947 TREE_PURPOSE (scalar_return_list
),
1949 bool unchecked_conversion
= (Nkind (gnat_actual
)
1950 == N_Unchecked_Type_Conversion
);
1951 /* If the actual is a conversion, get the inner expression, which
1952 will be the real destination, and convert the result to the
1953 type of the actual parameter. */
1955 = maybe_unconstrained_array (TREE_VALUE (gnu_name_list
));
1957 /* If the result is a padded type, remove the padding. */
1958 if (TREE_CODE (TREE_TYPE (gnu_result
)) == RECORD_TYPE
1959 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result
)))
1960 gnu_result
= convert (TREE_TYPE (TYPE_FIELDS
1961 (TREE_TYPE (gnu_result
))),
1964 /* If the result is a type conversion, do it. */
1965 if (Nkind (gnat_actual
) == N_Type_Conversion
)
1967 = convert_with_check
1968 (Etype (Expression (gnat_actual
)), gnu_result
,
1969 Do_Overflow_Check (gnat_actual
),
1970 Do_Range_Check (Expression (gnat_actual
)),
1971 Float_Truncate (gnat_actual
));
1973 else if (unchecked_conversion
)
1974 gnu_result
= unchecked_convert (TREE_TYPE (gnu_actual
),
1976 No_Truncation (gnat_actual
));
1979 if (Do_Range_Check (gnat_actual
))
1980 gnu_result
= emit_range_check (gnu_result
,
1981 Etype (gnat_actual
));
1983 if (!(!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual
)))
1984 && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_result
)))))
1985 gnu_result
= convert (TREE_TYPE (gnu_actual
), gnu_result
);
1988 gnu_result
= build_binary_op (MODIFY_EXPR
, NULL_TREE
,
1989 gnu_actual
, gnu_result
);
1990 annotate_with_node (gnu_result
, gnat_actual
);
1991 append_to_statement_list (gnu_result
, &gnu_before_list
);
1992 scalar_return_list
= TREE_CHAIN (scalar_return_list
);
1993 gnu_name_list
= TREE_CHAIN (gnu_name_list
);
1998 annotate_with_node (gnu_subprog_call
, gnat_node
);
1999 append_to_statement_list (gnu_subprog_call
, &gnu_before_list
);
2002 append_to_statement_list (gnu_after_list
, &gnu_before_list
);
2003 return gnu_before_list
;
2006 /* Subroutine of gnat_to_gnu to translate gnat_node, an
2007 N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */
2010 Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node
)
2012 tree gnu_jmpsave_decl
= NULL_TREE
;
2013 tree gnu_jmpbuf_decl
= NULL_TREE
;
2014 /* If just annotating, ignore all EH and cleanups. */
2015 bool gcc_zcx
= (!type_annotate_only
2016 && Present (Exception_Handlers (gnat_node
))
2017 && Exception_Mechanism
== GCC_ZCX
);
2019 = (!type_annotate_only
&& Present (Exception_Handlers (gnat_node
))
2020 && Exception_Mechanism
== Setjmp_Longjmp
);
2021 bool at_end
= !type_annotate_only
&& Present (At_End_Proc (gnat_node
));
2022 bool binding_for_block
= (at_end
|| gcc_zcx
|| setjmp_longjmp
);
2023 tree gnu_inner_block
; /* The statement(s) for the block itself. */
2028 /* The GCC exception handling mechanism can handle both ZCX and SJLJ schemes
2029 and we have our own SJLJ mechanism. To call the GCC mechanism, we call
2030 add_cleanup, and when we leave the binding, end_stmt_group will create
2031 the TRY_FINALLY_EXPR.
2033 ??? The region level calls down there have been specifically put in place
2034 for a ZCX context and currently the order in which things are emitted
2035 (region/handlers) is different from the SJLJ case. Instead of putting
2036 other calls with different conditions at other places for the SJLJ case,
2037 it seems cleaner to reorder things for the SJLJ case and generalize the
2038 condition to make it not ZCX specific.
2040 If there are any exceptions or cleanup processing involved, we need an
2041 outer statement group (for Setjmp_Longjmp) and binding level. */
2042 if (binding_for_block
)
2044 start_stmt_group ();
2048 /* If we are to call a function when exiting this block add a cleanup
2049 to the binding level we made above. */
2051 add_cleanup (build_call_0_expr (gnat_to_gnu (At_End_Proc (gnat_node
))));
2053 /* If using setjmp_longjmp, make the variables for the setjmp buffer and save
2054 area for address of previous buffer. Do this first since we need to have
2055 the setjmp buf known for any decls in this block. */
2058 gnu_jmpsave_decl
= create_var_decl (get_identifier ("JMPBUF_SAVE"),
2059 NULL_TREE
, jmpbuf_ptr_type
,
2060 build_call_0_expr (get_jmpbuf_decl
),
2061 false, false, false, false, NULL
,
2063 gnu_jmpbuf_decl
= create_var_decl (get_identifier ("JMP_BUF"),
2064 NULL_TREE
, jmpbuf_type
,
2065 NULL_TREE
, false, false, false, false,
2068 set_block_jmpbuf_decl (gnu_jmpbuf_decl
);
2070 /* When we exit this block, restore the saved value. */
2071 add_cleanup (build_call_1_expr (set_jmpbuf_decl
, gnu_jmpsave_decl
));
2074 /* Now build the tree for the declarations and statements inside this block.
2075 If this is SJLJ, set our jmp_buf as the current buffer. */
2076 start_stmt_group ();
2079 add_stmt (build_call_1_expr (set_jmpbuf_decl
,
2080 build_unary_op (ADDR_EXPR
, NULL_TREE
,
2083 if (Present (First_Real_Statement (gnat_node
)))
2084 process_decls (Statements (gnat_node
), Empty
,
2085 First_Real_Statement (gnat_node
), true, true);
2087 /* Generate code for each statement in the block. */
2088 for (gnat_temp
= (Present (First_Real_Statement (gnat_node
))
2089 ? First_Real_Statement (gnat_node
)
2090 : First (Statements (gnat_node
)));
2091 Present (gnat_temp
); gnat_temp
= Next (gnat_temp
))
2092 add_stmt (gnat_to_gnu (gnat_temp
));
2093 gnu_inner_block
= end_stmt_group ();
2095 /* Now generate code for the two exception models, if either is relevant for
2099 tree
*gnu_else_ptr
= 0;
2102 /* Make a binding level for the exception handling declarations and code
2103 and set up gnu_except_ptr_stack for the handlers to use. */
2104 start_stmt_group ();
2107 push_stack (&gnu_except_ptr_stack
, NULL_TREE
,
2108 create_var_decl (get_identifier ("EXCEPT_PTR"),
2110 build_pointer_type (except_type_node
),
2111 build_call_0_expr (get_excptr_decl
), false,
2112 false, false, false, NULL
, gnat_node
));
2114 /* Generate code for each handler. The N_Exception_Handler case does the
2115 real work and returns a COND_EXPR for each handler, which we chain
2117 for (gnat_temp
= First_Non_Pragma (Exception_Handlers (gnat_node
));
2118 Present (gnat_temp
); gnat_temp
= Next_Non_Pragma (gnat_temp
))
2120 gnu_expr
= gnat_to_gnu (gnat_temp
);
2122 /* If this is the first one, set it as the outer one. Otherwise,
2123 point the "else" part of the previous handler to us. Then point
2124 to our "else" part. */
2126 add_stmt (gnu_expr
);
2128 *gnu_else_ptr
= gnu_expr
;
2130 gnu_else_ptr
= &COND_EXPR_ELSE (gnu_expr
);
2133 /* If none of the exception handlers did anything, re-raise but do not
2135 gnu_expr
= build_call_1_expr (raise_nodefer_decl
,
2136 TREE_VALUE (gnu_except_ptr_stack
));
2137 annotate_with_node (gnu_expr
, gnat_node
);
2140 *gnu_else_ptr
= gnu_expr
;
2142 add_stmt (gnu_expr
);
2144 /* End the binding level dedicated to the exception handlers and get the
2145 whole statement group. */
2146 pop_stack (&gnu_except_ptr_stack
);
2148 gnu_handler
= end_stmt_group ();
2150 /* If the setjmp returns 1, we restore our incoming longjmp value and
2151 then check the handlers. */
2152 start_stmt_group ();
2153 add_stmt_with_node (build_call_1_expr (set_jmpbuf_decl
,
2156 add_stmt (gnu_handler
);
2157 gnu_handler
= end_stmt_group ();
2159 /* This block is now "if (setjmp) ... <handlers> else <block>". */
2160 gnu_result
= build3 (COND_EXPR
, void_type_node
,
2163 build_unary_op (ADDR_EXPR
, NULL_TREE
,
2165 gnu_handler
, gnu_inner_block
);
2171 /* First make a block containing the handlers. */
2172 start_stmt_group ();
2173 for (gnat_temp
= First_Non_Pragma (Exception_Handlers (gnat_node
));
2174 Present (gnat_temp
);
2175 gnat_temp
= Next_Non_Pragma (gnat_temp
))
2176 add_stmt (gnat_to_gnu (gnat_temp
));
2177 gnu_handlers
= end_stmt_group ();
2179 /* Now make the TRY_CATCH_EXPR for the block. */
2180 gnu_result
= build2 (TRY_CATCH_EXPR
, void_type_node
,
2181 gnu_inner_block
, gnu_handlers
);
2184 gnu_result
= gnu_inner_block
;
2186 /* Now close our outer block, if we had to make one. */
2187 if (binding_for_block
)
2189 add_stmt (gnu_result
);
2191 gnu_result
= end_stmt_group ();
2197 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
2198 to a GCC tree, which is returned. This is the variant for Setjmp_Longjmp
2199 exception handling. */
2202 Exception_Handler_to_gnu_sjlj (Node_Id gnat_node
)
2204 /* Unless this is "Others" or the special "Non-Ada" exception for Ada, make
2205 an "if" statement to select the proper exceptions. For "Others", exclude
2206 exceptions where Handled_By_Others is nonzero unless the All_Others flag
2207 is set. For "Non-ada", accept an exception if "Lang" is 'V'. */
2208 tree gnu_choice
= integer_zero_node
;
2209 tree gnu_body
= build_stmt_group (Statements (gnat_node
), false);
2212 for (gnat_temp
= First (Exception_Choices (gnat_node
));
2213 gnat_temp
; gnat_temp
= Next (gnat_temp
))
2217 if (Nkind (gnat_temp
) == N_Others_Choice
)
2219 if (All_Others (gnat_temp
))
2220 this_choice
= integer_one_node
;
2224 (EQ_EXPR
, integer_type_node
,
2229 (INDIRECT_REF
, NULL_TREE
,
2230 TREE_VALUE (gnu_except_ptr_stack
)),
2231 get_identifier ("not_handled_by_others"), NULL_TREE
,
2236 else if (Nkind (gnat_temp
) == N_Identifier
2237 || Nkind (gnat_temp
) == N_Expanded_Name
)
2239 Entity_Id gnat_ex_id
= Entity (gnat_temp
);
2242 /* Exception may be a renaming. Recover original exception which is
2243 the one elaborated and registered. */
2244 if (Present (Renamed_Object (gnat_ex_id
)))
2245 gnat_ex_id
= Renamed_Object (gnat_ex_id
);
2247 gnu_expr
= gnat_to_gnu_entity (gnat_ex_id
, NULL_TREE
, 0);
2251 (EQ_EXPR
, integer_type_node
, TREE_VALUE (gnu_except_ptr_stack
),
2252 convert (TREE_TYPE (TREE_VALUE (gnu_except_ptr_stack
)),
2253 build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_expr
)));
2255 /* If this is the distinguished exception "Non_Ada_Error" (and we are
2256 in VMS mode), also allow a non-Ada exception (a VMS condition) t
2258 if (Is_Non_Ada_Error (Entity (gnat_temp
)))
2261 = build_component_ref
2262 (build_unary_op (INDIRECT_REF
, NULL_TREE
,
2263 TREE_VALUE (gnu_except_ptr_stack
)),
2264 get_identifier ("lang"), NULL_TREE
, false);
2268 (TRUTH_ORIF_EXPR
, integer_type_node
,
2269 build_binary_op (EQ_EXPR
, integer_type_node
, gnu_comp
,
2270 build_int_cst (TREE_TYPE (gnu_comp
), 'V')),
2277 gnu_choice
= build_binary_op (TRUTH_ORIF_EXPR
, integer_type_node
,
2278 gnu_choice
, this_choice
);
2281 return build3 (COND_EXPR
, void_type_node
, gnu_choice
, gnu_body
, NULL_TREE
);
2284 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
2285 to a GCC tree, which is returned. This is the variant for ZCX. */
2288 Exception_Handler_to_gnu_zcx (Node_Id gnat_node
)
2290 tree gnu_etypes_list
= NULL_TREE
;
2293 tree gnu_current_exc_ptr
;
2294 tree gnu_incoming_exc_ptr
;
2297 /* We build a TREE_LIST of nodes representing what exception types this
2298 handler can catch, with special cases for others and all others cases.
2300 Each exception type is actually identified by a pointer to the exception
2301 id, or to a dummy object for "others" and "all others".
2303 Care should be taken to ensure that the control flow impact of "others"
2304 and "all others" is known to GCC. lang_eh_type_covers is doing the trick
2306 for (gnat_temp
= First (Exception_Choices (gnat_node
));
2307 gnat_temp
; gnat_temp
= Next (gnat_temp
))
2309 if (Nkind (gnat_temp
) == N_Others_Choice
)
2312 = All_Others (gnat_temp
) ? all_others_decl
: others_decl
;
2315 = build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_expr
);
2317 else if (Nkind (gnat_temp
) == N_Identifier
2318 || Nkind (gnat_temp
) == N_Expanded_Name
)
2320 Entity_Id gnat_ex_id
= Entity (gnat_temp
);
2322 /* Exception may be a renaming. Recover original exception which is
2323 the one elaborated and registered. */
2324 if (Present (Renamed_Object (gnat_ex_id
)))
2325 gnat_ex_id
= Renamed_Object (gnat_ex_id
);
2327 gnu_expr
= gnat_to_gnu_entity (gnat_ex_id
, NULL_TREE
, 0);
2328 gnu_etype
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_expr
);
2330 /* The Non_Ada_Error case for VMS exceptions is handled
2331 by the personality routine. */
2336 /* The GCC interface expects NULL to be passed for catch all handlers, so
2337 it would be quite tempting to set gnu_etypes_list to NULL if gnu_etype
2338 is integer_zero_node. It would not work, however, because GCC's
2339 notion of "catch all" is stronger than our notion of "others". Until
2340 we correctly use the cleanup interface as well, doing that would
2341 prevent the "all others" handlers from being seen, because nothing
2342 can be caught beyond a catch all from GCC's point of view. */
2343 gnu_etypes_list
= tree_cons (NULL_TREE
, gnu_etype
, gnu_etypes_list
);
2346 start_stmt_group ();
2349 /* Expand a call to the begin_handler hook at the beginning of the handler,
2350 and arrange for a call to the end_handler hook to occur on every possible
2353 The hooks expect a pointer to the low level occurrence. This is required
2354 for our stack management scheme because a raise inside the handler pushes
2355 a new occurrence on top of the stack, which means that this top does not
2356 necessarily match the occurrence this handler was dealing with.
2358 The EXC_PTR_EXPR object references the exception occurrence being
2359 propagated. Upon handler entry, this is the exception for which the
2360 handler is triggered. This might not be the case upon handler exit,
2361 however, as we might have a new occurrence propagated by the handler's
2362 body, and the end_handler hook called as a cleanup in this context.
2364 We use a local variable to retrieve the incoming value at handler entry
2365 time, and reuse it to feed the end_handler hook's argument at exit. */
2366 gnu_current_exc_ptr
= build0 (EXC_PTR_EXPR
, ptr_type_node
);
2367 gnu_incoming_exc_ptr
= create_var_decl (get_identifier ("EXPTR"), NULL_TREE
,
2368 ptr_type_node
, gnu_current_exc_ptr
,
2369 false, false, false, false, NULL
,
2372 add_stmt_with_node (build_call_1_expr (begin_handler_decl
,
2373 gnu_incoming_exc_ptr
),
2375 add_cleanup (build_call_1_expr (end_handler_decl
, gnu_incoming_exc_ptr
));
2376 add_stmt_list (Statements (gnat_node
));
2379 return build2 (CATCH_EXPR
, void_type_node
, gnu_etypes_list
,
2383 /* Subroutine of gnat_to_gnu to generate code for an N_Compilation unit. */
2386 Compilation_Unit_to_gnu (Node_Id gnat_node
)
2388 /* Make the decl for the elaboration procedure. */
2389 bool body_p
= (Defining_Entity (Unit (gnat_node
)),
2390 Nkind (Unit (gnat_node
)) == N_Package_Body
2391 || Nkind (Unit (gnat_node
)) == N_Subprogram_Body
);
2392 Entity_Id gnat_unit_entity
= Defining_Entity (Unit (gnat_node
));
2393 tree gnu_elab_proc_decl
2394 = create_subprog_decl
2395 (create_concat_name (gnat_unit_entity
,
2396 body_p
? "elabb" : "elabs"),
2397 NULL_TREE
, void_ftype
, NULL_TREE
, false, true, false, NULL
,
2399 struct elab_info
*info
;
2401 push_stack (&gnu_elab_proc_stack
, NULL_TREE
, gnu_elab_proc_decl
);
2403 DECL_ELABORATION_PROC_P (gnu_elab_proc_decl
) = 1;
2404 allocate_struct_function (gnu_elab_proc_decl
);
2405 Sloc_to_locus (Sloc (gnat_unit_entity
), &cfun
->function_end_locus
);
2408 /* For a body, first process the spec if there is one. */
2409 if (Nkind (Unit (gnat_node
)) == N_Package_Body
2410 || (Nkind (Unit (gnat_node
)) == N_Subprogram_Body
2411 && !Acts_As_Spec (gnat_node
)))
2412 add_stmt (gnat_to_gnu (Library_Unit (gnat_node
)));
2414 process_inlined_subprograms (gnat_node
);
2416 if (type_annotate_only
)
2418 elaborate_all_entities (gnat_node
);
2420 if (Nkind (Unit (gnat_node
)) == N_Subprogram_Declaration
2421 || Nkind (Unit (gnat_node
)) == N_Generic_Package_Declaration
2422 || Nkind (Unit (gnat_node
)) == N_Generic_Subprogram_Declaration
)
2426 process_decls (Declarations (Aux_Decls_Node (gnat_node
)), Empty
, Empty
,
2428 add_stmt (gnat_to_gnu (Unit (gnat_node
)));
2430 /* Process any pragmas and actions following the unit. */
2431 add_stmt_list (Pragmas_After (Aux_Decls_Node (gnat_node
)));
2432 add_stmt_list (Actions (Aux_Decls_Node (gnat_node
)));
2434 /* Save away what we've made so far and record this potential elaboration
2436 info
= (struct elab_info
*) ggc_alloc (sizeof (struct elab_info
));
2437 set_current_block_context (gnu_elab_proc_decl
);
2439 DECL_SAVED_TREE (gnu_elab_proc_decl
) = end_stmt_group ();
2440 info
->next
= elab_info_list
;
2441 info
->elab_proc
= gnu_elab_proc_decl
;
2442 info
->gnat_node
= gnat_node
;
2443 elab_info_list
= info
;
2445 /* Generate elaboration code for this unit, if necessary, and say whether
2447 pop_stack (&gnu_elab_proc_stack
);
2450 /* This function is the driver of the GNAT to GCC tree transformation
2451 process. It is the entry point of the tree transformer. GNAT_NODE is the
2452 root of some GNAT tree. Return the root of the corresponding GCC tree.
2453 If this is an expression, return the GCC equivalent of the expression. If
2454 it is a statement, return the statement. In the case when called for a
2455 statement, it may also add statements to the current statement group, in
2456 which case anything it returns is to be interpreted as occurring after
2457 anything `it already added. */
2460 gnat_to_gnu (Node_Id gnat_node
)
2462 bool went_into_elab_proc
= false;
2463 tree gnu_result
= error_mark_node
; /* Default to no value. */
2464 tree gnu_result_type
= void_type_node
;
2466 tree gnu_lhs
, gnu_rhs
;
2469 /* Save node number for error message and set location information. */
2470 error_gnat_node
= gnat_node
;
2471 Sloc_to_locus (Sloc (gnat_node
), &input_location
);
2473 if (type_annotate_only
2474 && IN (Nkind (gnat_node
), N_Statement_Other_Than_Procedure_Call
))
2475 return alloc_stmt_list ();
2477 /* If this node is a non-static subexpression and we are only
2478 annotating types, make this into a NULL_EXPR. */
2479 if (type_annotate_only
2480 && IN (Nkind (gnat_node
), N_Subexpr
)
2481 && Nkind (gnat_node
) != N_Identifier
2482 && !Compile_Time_Known_Value (gnat_node
))
2483 return build1 (NULL_EXPR
, get_unpadded_type (Etype (gnat_node
)),
2484 build_call_raise (CE_Range_Check_Failed
));
2486 /* If this is a Statement and we are at top level, it must be part of
2487 the elaboration procedure, so mark us as being in that procedure
2488 and push our context. */
2489 if (!current_function_decl
2490 && ((IN (Nkind (gnat_node
), N_Statement_Other_Than_Procedure_Call
)
2491 && Nkind (gnat_node
) != N_Null_Statement
)
2492 || Nkind (gnat_node
) == N_Procedure_Call_Statement
2493 || Nkind (gnat_node
) == N_Label
2494 || Nkind (gnat_node
) == N_Implicit_Label_Declaration
2495 || Nkind (gnat_node
) == N_Handled_Sequence_Of_Statements
2496 || ((Nkind (gnat_node
) == N_Raise_Constraint_Error
2497 || Nkind (gnat_node
) == N_Raise_Storage_Error
2498 || Nkind (gnat_node
) == N_Raise_Program_Error
)
2499 && (Ekind (Etype (gnat_node
)) == E_Void
))))
2501 current_function_decl
= TREE_VALUE (gnu_elab_proc_stack
);
2502 start_stmt_group ();
2504 went_into_elab_proc
= true;
2507 switch (Nkind (gnat_node
))
2509 /********************************/
2510 /* Chapter 2: Lexical Elements: */
2511 /********************************/
2514 case N_Expanded_Name
:
2515 case N_Operator_Symbol
:
2516 case N_Defining_Identifier
:
2517 gnu_result
= Identifier_to_gnu (gnat_node
, &gnu_result_type
);
2520 case N_Integer_Literal
:
2524 /* Get the type of the result, looking inside any padding and
2525 justified modular types. Then get the value in that type. */
2526 gnu_type
= gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2528 if (TREE_CODE (gnu_type
) == RECORD_TYPE
2529 && TYPE_JUSTIFIED_MODULAR_P (gnu_type
))
2530 gnu_type
= TREE_TYPE (TYPE_FIELDS (gnu_type
));
2532 gnu_result
= UI_To_gnu (Intval (gnat_node
), gnu_type
);
2534 /* If the result overflows (meaning it doesn't fit in its base type),
2535 abort. We would like to check that the value is within the range
2536 of the subtype, but that causes problems with subtypes whose usage
2537 will raise Constraint_Error and with biased representation, so
2539 gcc_assert (!TREE_CONSTANT_OVERFLOW (gnu_result
));
2543 case N_Character_Literal
:
2544 /* If a Entity is present, it means that this was one of the
2545 literals in a user-defined character type. In that case,
2546 just return the value in the CONST_DECL. Otherwise, use the
2547 character code. In that case, the base type should be an
2548 INTEGER_TYPE, but we won't bother checking for that. */
2549 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2550 if (Present (Entity (gnat_node
)))
2551 gnu_result
= DECL_INITIAL (get_gnu_tree (Entity (gnat_node
)));
2556 (gnu_result_type
, UI_To_CC (Char_Literal_Value (gnat_node
))),
2557 false, false, false);
2560 case N_Real_Literal
:
2561 /* If this is of a fixed-point type, the value we want is the
2562 value of the corresponding integer. */
2563 if (IN (Ekind (Underlying_Type (Etype (gnat_node
))), Fixed_Point_Kind
))
2565 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2566 gnu_result
= UI_To_gnu (Corresponding_Integer_Value (gnat_node
),
2568 gcc_assert (!TREE_CONSTANT_OVERFLOW (gnu_result
));
2571 /* We should never see a Vax_Float type literal, since the front end
2572 is supposed to transform these using appropriate conversions */
2573 else if (Vax_Float (Underlying_Type (Etype (gnat_node
))))
2578 Ureal ur_realval
= Realval (gnat_node
);
2580 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2582 /* If the real value is zero, so is the result. Otherwise,
2583 convert it to a machine number if it isn't already. That
2584 forces BASE to 0 or 2 and simplifies the rest of our logic. */
2585 if (UR_Is_Zero (ur_realval
))
2586 gnu_result
= convert (gnu_result_type
, integer_zero_node
);
2589 if (!Is_Machine_Number (gnat_node
))
2591 = Machine (Base_Type (Underlying_Type (Etype (gnat_node
))),
2592 ur_realval
, Round_Even
, gnat_node
);
2595 = UI_To_gnu (Numerator (ur_realval
), gnu_result_type
);
2597 /* If we have a base of zero, divide by the denominator.
2598 Otherwise, the base must be 2 and we scale the value, which
2599 we know can fit in the mantissa of the type (hence the use
2600 of that type above). */
2601 if (No (Rbase (ur_realval
)))
2603 = build_binary_op (RDIV_EXPR
,
2604 get_base_type (gnu_result_type
),
2606 UI_To_gnu (Denominator (ur_realval
),
2610 REAL_VALUE_TYPE tmp
;
2612 gcc_assert (Rbase (ur_realval
) == 2);
2613 real_ldexp (&tmp
, &TREE_REAL_CST (gnu_result
),
2614 - UI_To_Int (Denominator (ur_realval
)));
2615 gnu_result
= build_real (gnu_result_type
, tmp
);
2619 /* Now see if we need to negate the result. Do it this way to
2620 properly handle -0. */
2621 if (UR_Is_Negative (Realval (gnat_node
)))
2623 = build_unary_op (NEGATE_EXPR
, get_base_type (gnu_result_type
),
2629 case N_String_Literal
:
2630 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2631 if (TYPE_PRECISION (TREE_TYPE (gnu_result_type
)) == HOST_BITS_PER_CHAR
)
2633 String_Id gnat_string
= Strval (gnat_node
);
2634 int length
= String_Length (gnat_string
);
2635 char *string
= (char *) alloca (length
+ 1);
2638 /* Build the string with the characters in the literal. Note
2639 that Ada strings are 1-origin. */
2640 for (i
= 0; i
< length
; i
++)
2641 string
[i
] = Get_String_Char (gnat_string
, i
+ 1);
2643 /* Put a null at the end of the string in case it's in a context
2644 where GCC will want to treat it as a C string. */
2647 gnu_result
= build_string (length
, string
);
2649 /* Strings in GCC don't normally have types, but we want
2650 this to not be converted to the array type. */
2651 TREE_TYPE (gnu_result
) = gnu_result_type
;
2655 /* Build a list consisting of each character, then make
2657 String_Id gnat_string
= Strval (gnat_node
);
2658 int length
= String_Length (gnat_string
);
2660 tree gnu_list
= NULL_TREE
;
2661 tree gnu_idx
= TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type
));
2663 for (i
= 0; i
< length
; i
++)
2666 = tree_cons (gnu_idx
,
2667 build_int_cst (TREE_TYPE (gnu_result_type
),
2668 Get_String_Char (gnat_string
,
2672 gnu_idx
= int_const_binop (PLUS_EXPR
, gnu_idx
, integer_one_node
,
2677 = gnat_build_constructor (gnu_result_type
, nreverse (gnu_list
));
2682 gnu_result
= Pragma_to_gnu (gnat_node
);
2685 /**************************************/
2686 /* Chapter 3: Declarations and Types: */
2687 /**************************************/
2689 case N_Subtype_Declaration
:
2690 case N_Full_Type_Declaration
:
2691 case N_Incomplete_Type_Declaration
:
2692 case N_Private_Type_Declaration
:
2693 case N_Private_Extension_Declaration
:
2694 case N_Task_Type_Declaration
:
2695 process_type (Defining_Entity (gnat_node
));
2696 gnu_result
= alloc_stmt_list ();
2699 case N_Object_Declaration
:
2700 case N_Exception_Declaration
:
2701 gnat_temp
= Defining_Entity (gnat_node
);
2702 gnu_result
= alloc_stmt_list ();
2704 /* If we are just annotating types and this object has an unconstrained
2705 or task type, don't elaborate it. */
2706 if (type_annotate_only
2707 && (((Is_Array_Type (Etype (gnat_temp
))
2708 || Is_Record_Type (Etype (gnat_temp
)))
2709 && !Is_Constrained (Etype (gnat_temp
)))
2710 || Is_Concurrent_Type (Etype (gnat_temp
))))
2713 if (Present (Expression (gnat_node
))
2714 && !(Nkind (gnat_node
) == N_Object_Declaration
2715 && No_Initialization (gnat_node
))
2716 && (!type_annotate_only
2717 || Compile_Time_Known_Value (Expression (gnat_node
))))
2719 gnu_expr
= gnat_to_gnu (Expression (gnat_node
));
2720 if (Do_Range_Check (Expression (gnat_node
)))
2721 gnu_expr
= emit_range_check (gnu_expr
, Etype (gnat_temp
));
2723 /* If this object has its elaboration delayed, we must force
2724 evaluation of GNU_EXPR right now and save it for when the object
2726 if (Present (Freeze_Node (gnat_temp
)))
2728 if ((Is_Public (gnat_temp
) || global_bindings_p ())
2729 && !TREE_CONSTANT (gnu_expr
))
2731 = create_var_decl (create_concat_name (gnat_temp
, "init"),
2732 NULL_TREE
, TREE_TYPE (gnu_expr
),
2733 gnu_expr
, false, Is_Public (gnat_temp
),
2734 false, false, NULL
, gnat_temp
);
2736 gnu_expr
= maybe_variable (gnu_expr
);
2738 save_gnu_tree (gnat_node
, gnu_expr
, true);
2742 gnu_expr
= NULL_TREE
;
2744 if (type_annotate_only
&& gnu_expr
&& TREE_CODE (gnu_expr
) == ERROR_MARK
)
2745 gnu_expr
= NULL_TREE
;
2747 if (No (Freeze_Node (gnat_temp
)))
2748 gnat_to_gnu_entity (gnat_temp
, gnu_expr
, 1);
2751 case N_Object_Renaming_Declaration
:
2752 gnat_temp
= Defining_Entity (gnat_node
);
2754 /* Don't do anything if this renaming is handled by the front end or if
2755 we are just annotating types and this object has a composite or task
2756 type, don't elaborate it. We return the result in case it has any
2757 SAVE_EXPRs in it that need to be evaluated here. */
2758 if (!Is_Renaming_Of_Object (gnat_temp
)
2759 && ! (type_annotate_only
2760 && (Is_Array_Type (Etype (gnat_temp
))
2761 || Is_Record_Type (Etype (gnat_temp
))
2762 || Is_Concurrent_Type (Etype (gnat_temp
)))))
2764 = gnat_to_gnu_entity (gnat_temp
,
2765 gnat_to_gnu (Renamed_Object (gnat_temp
)), 1);
2767 gnu_result
= alloc_stmt_list ();
2770 case N_Implicit_Label_Declaration
:
2771 gnat_to_gnu_entity (Defining_Entity (gnat_node
), NULL_TREE
, 1);
2772 gnu_result
= alloc_stmt_list ();
2775 case N_Exception_Renaming_Declaration
:
2776 case N_Number_Declaration
:
2777 case N_Package_Renaming_Declaration
:
2778 case N_Subprogram_Renaming_Declaration
:
2779 /* These are fully handled in the front end. */
2780 gnu_result
= alloc_stmt_list ();
2783 /*************************************/
2784 /* Chapter 4: Names and Expressions: */
2785 /*************************************/
2787 case N_Explicit_Dereference
:
2788 gnu_result
= gnat_to_gnu (Prefix (gnat_node
));
2789 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2790 gnu_result
= build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_result
);
2793 case N_Indexed_Component
:
2795 tree gnu_array_object
= gnat_to_gnu (Prefix (gnat_node
));
2799 Node_Id
*gnat_expr_array
;
2801 gnu_array_object
= maybe_implicit_deref (gnu_array_object
);
2802 gnu_array_object
= maybe_unconstrained_array (gnu_array_object
);
2804 /* If we got a padded type, remove it too. */
2805 if (TREE_CODE (TREE_TYPE (gnu_array_object
)) == RECORD_TYPE
2806 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_array_object
)))
2808 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_array_object
))),
2811 gnu_result
= gnu_array_object
;
2813 /* First compute the number of dimensions of the array, then
2814 fill the expression array, the order depending on whether
2815 this is a Convention_Fortran array or not. */
2816 for (ndim
= 1, gnu_type
= TREE_TYPE (gnu_array_object
);
2817 TREE_CODE (TREE_TYPE (gnu_type
)) == ARRAY_TYPE
2818 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type
));
2819 ndim
++, gnu_type
= TREE_TYPE (gnu_type
))
2822 gnat_expr_array
= (Node_Id
*) alloca (ndim
* sizeof (Node_Id
));
2824 if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object
)))
2825 for (i
= ndim
- 1, gnat_temp
= First (Expressions (gnat_node
));
2827 i
--, gnat_temp
= Next (gnat_temp
))
2828 gnat_expr_array
[i
] = gnat_temp
;
2830 for (i
= 0, gnat_temp
= First (Expressions (gnat_node
));
2832 i
++, gnat_temp
= Next (gnat_temp
))
2833 gnat_expr_array
[i
] = gnat_temp
;
2835 for (i
= 0, gnu_type
= TREE_TYPE (gnu_array_object
);
2836 i
< ndim
; i
++, gnu_type
= TREE_TYPE (gnu_type
))
2838 gcc_assert (TREE_CODE (gnu_type
) == ARRAY_TYPE
);
2839 gnat_temp
= gnat_expr_array
[i
];
2840 gnu_expr
= gnat_to_gnu (gnat_temp
);
2842 if (Do_Range_Check (gnat_temp
))
2845 (gnu_array_object
, gnu_expr
,
2846 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
))),
2847 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
))));
2849 gnu_result
= build_binary_op (ARRAY_REF
, NULL_TREE
,
2850 gnu_result
, gnu_expr
);
2854 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2860 Node_Id gnat_range_node
= Discrete_Range (gnat_node
);
2862 gnu_result
= gnat_to_gnu (Prefix (gnat_node
));
2863 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2865 /* Do any implicit dereferences of the prefix and do any needed
2867 gnu_result
= maybe_implicit_deref (gnu_result
);
2868 gnu_result
= maybe_unconstrained_array (gnu_result
);
2869 gnu_type
= TREE_TYPE (gnu_result
);
2870 if (Do_Range_Check (gnat_range_node
))
2872 /* Get the bounds of the slice. */
2874 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_result_type
));
2875 tree gnu_min_expr
= TYPE_MIN_VALUE (gnu_index_type
);
2876 tree gnu_max_expr
= TYPE_MAX_VALUE (gnu_index_type
);
2877 tree gnu_expr_l
, gnu_expr_h
, gnu_expr_type
;
2879 /* Check to see that the minimum slice value is in range */
2882 (gnu_result
, gnu_min_expr
,
2883 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
))),
2884 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
))));
2886 /* Check to see that the maximum slice value is in range */
2889 (gnu_result
, gnu_max_expr
,
2890 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
))),
2891 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
))));
2893 /* Derive a good type to convert everything too */
2894 gnu_expr_type
= get_base_type (TREE_TYPE (gnu_expr_l
));
2896 /* Build a compound expression that does the range checks */
2898 = build_binary_op (COMPOUND_EXPR
, gnu_expr_type
,
2899 convert (gnu_expr_type
, gnu_expr_h
),
2900 convert (gnu_expr_type
, gnu_expr_l
));
2902 /* Build a conditional expression that returns the range checks
2903 expression if the slice range is not null (max >= min) or
2904 returns the min if the slice range is null */
2906 = fold (build3 (COND_EXPR
, gnu_expr_type
,
2907 build_binary_op (GE_EXPR
, gnu_expr_type
,
2908 convert (gnu_expr_type
,
2910 convert (gnu_expr_type
,
2912 gnu_expr
, gnu_min_expr
));
2915 gnu_expr
= TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type
));
2917 gnu_result
= build_binary_op (ARRAY_RANGE_REF
, gnu_result_type
,
2918 gnu_result
, gnu_expr
);
2922 case N_Selected_Component
:
2924 tree gnu_prefix
= gnat_to_gnu (Prefix (gnat_node
));
2925 Entity_Id gnat_field
= Entity (Selector_Name (gnat_node
));
2926 Entity_Id gnat_pref_type
= Etype (Prefix (gnat_node
));
2929 while (IN (Ekind (gnat_pref_type
), Incomplete_Or_Private_Kind
)
2930 || IN (Ekind (gnat_pref_type
), Access_Kind
))
2932 if (IN (Ekind (gnat_pref_type
), Incomplete_Or_Private_Kind
))
2933 gnat_pref_type
= Underlying_Type (gnat_pref_type
);
2934 else if (IN (Ekind (gnat_pref_type
), Access_Kind
))
2935 gnat_pref_type
= Designated_Type (gnat_pref_type
);
2938 gnu_prefix
= maybe_implicit_deref (gnu_prefix
);
2940 /* For discriminant references in tagged types always substitute the
2941 corresponding discriminant as the actual selected component. */
2943 if (Is_Tagged_Type (gnat_pref_type
))
2944 while (Present (Corresponding_Discriminant (gnat_field
)))
2945 gnat_field
= Corresponding_Discriminant (gnat_field
);
2947 /* For discriminant references of untagged types always substitute the
2948 corresponding stored discriminant. */
2950 else if (Present (Corresponding_Discriminant (gnat_field
)))
2951 gnat_field
= Original_Record_Component (gnat_field
);
2953 /* Handle extracting the real or imaginary part of a complex.
2954 The real part is the first field and the imaginary the last. */
2956 if (TREE_CODE (TREE_TYPE (gnu_prefix
)) == COMPLEX_TYPE
)
2957 gnu_result
= build_unary_op (Present (Next_Entity (gnat_field
))
2958 ? REALPART_EXPR
: IMAGPART_EXPR
,
2959 NULL_TREE
, gnu_prefix
);
2962 gnu_field
= gnat_to_gnu_field_decl (gnat_field
);
2964 /* If there are discriminants, the prefix might be
2965 evaluated more than once, which is a problem if it has
2967 if (Has_Discriminants (Is_Access_Type (Etype (Prefix (gnat_node
)))
2968 ? Designated_Type (Etype
2969 (Prefix (gnat_node
)))
2970 : Etype (Prefix (gnat_node
))))
2971 gnu_prefix
= gnat_stabilize_reference (gnu_prefix
, 0);
2974 = build_component_ref (gnu_prefix
, NULL_TREE
, gnu_field
,
2975 (Nkind (Parent (gnat_node
))
2976 == N_Attribute_Reference
));
2979 gcc_assert (gnu_result
);
2980 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2984 case N_Attribute_Reference
:
2986 /* The attribute designator (like an enumeration value). */
2987 int attribute
= Get_Attribute_Id (Attribute_Name (gnat_node
));
2989 /* The Elab_Spec and Elab_Body attributes are special in that
2990 Prefix is a unit, not an object with a GCC equivalent. Similarly
2991 for Elaborated, since that variable isn't otherwise known. */
2992 if (attribute
== Attr_Elab_Body
|| attribute
== Attr_Elab_Spec
)
2993 return (create_subprog_decl
2994 (create_concat_name (Entity (Prefix (gnat_node
)),
2995 attribute
== Attr_Elab_Body
2996 ? "elabb" : "elabs"),
2997 NULL_TREE
, void_ftype
, NULL_TREE
, false, true, true, NULL
,
3000 gnu_result
= Attribute_to_gnu (gnat_node
, &gnu_result_type
, attribute
);
3005 /* Like 'Access as far as we are concerned. */
3006 gnu_result
= gnat_to_gnu (Prefix (gnat_node
));
3007 gnu_result
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_result
);
3008 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
3012 case N_Extension_Aggregate
:
3016 /* ??? It is wrong to evaluate the type now, but there doesn't
3017 seem to be any other practical way of doing it. */
3019 gcc_assert (!Expansion_Delayed (gnat_node
));
3021 gnu_aggr_type
= gnu_result_type
3022 = get_unpadded_type (Etype (gnat_node
));
3024 if (TREE_CODE (gnu_result_type
) == RECORD_TYPE
3025 && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type
))
3027 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_result_type
)));
3029 if (Null_Record_Present (gnat_node
))
3030 gnu_result
= gnat_build_constructor (gnu_aggr_type
, NULL_TREE
);
3032 else if (TREE_CODE (gnu_aggr_type
) == UNION_TYPE
3033 && TYPE_UNCHECKED_UNION_P (gnu_aggr_type
))
3035 /* The first element is the discrimant, which we ignore. The
3036 next is the field we're building. Convert the expression
3037 to the type of the field and then to the union type. */
3039 = Next (First (Component_Associations (gnat_node
)));
3040 Entity_Id gnat_field
= Entity (First (Choices (gnat_assoc
)));
3042 = TREE_TYPE (gnat_to_gnu_entity (gnat_field
, NULL_TREE
, 0));
3044 gnu_result
= convert (gnu_field_type
,
3045 gnat_to_gnu (Expression (gnat_assoc
)));
3047 else if (TREE_CODE (gnu_aggr_type
) == RECORD_TYPE
3048 || TREE_CODE (gnu_aggr_type
) == UNION_TYPE
)
3050 = assoc_to_constructor (First (Component_Associations (gnat_node
)),
3052 else if (TREE_CODE (gnu_aggr_type
) == ARRAY_TYPE
)
3053 gnu_result
= pos_to_constructor (First (Expressions (gnat_node
)),
3055 Component_Type (Etype (gnat_node
)));
3056 else if (TREE_CODE (gnu_aggr_type
) == COMPLEX_TYPE
)
3059 (COMPLEX_EXPR
, gnu_aggr_type
,
3060 gnat_to_gnu (Expression (First
3061 (Component_Associations (gnat_node
)))),
3062 gnat_to_gnu (Expression
3064 (First (Component_Associations (gnat_node
))))));
3068 gnu_result
= convert (gnu_result_type
, gnu_result
);
3073 gnu_result
= null_pointer_node
;
3074 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
3077 case N_Type_Conversion
:
3078 case N_Qualified_Expression
:
3079 /* Get the operand expression. */
3080 gnu_result
= gnat_to_gnu (Expression (gnat_node
));
3081 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
3084 = convert_with_check (Etype (gnat_node
), gnu_result
,
3085 Do_Overflow_Check (gnat_node
),
3086 Do_Range_Check (Expression (gnat_node
)),
3087 Nkind (gnat_node
) == N_Type_Conversion
3088 && Float_Truncate (gnat_node
));
3091 case N_Unchecked_Type_Conversion
:
3092 gnu_result
= gnat_to_gnu (Expression (gnat_node
));
3093 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
3095 /* If the result is a pointer type, see if we are improperly
3096 converting to a stricter alignment. */
3098 if (STRICT_ALIGNMENT
&& POINTER_TYPE_P (gnu_result_type
)
3099 && IN (Ekind (Etype (gnat_node
)), Access_Kind
))
3101 unsigned int align
= known_alignment (gnu_result
);
3102 tree gnu_obj_type
= TREE_TYPE (gnu_result_type
);
3103 unsigned int oalign
= TYPE_ALIGN (gnu_obj_type
);
3105 if (align
!= 0 && align
< oalign
&& !TYPE_ALIGN_OK (gnu_obj_type
))
3106 post_error_ne_tree_2
3107 ("?source alignment (^) < alignment of & (^)",
3108 gnat_node
, Designated_Type (Etype (gnat_node
)),
3109 size_int (align
/ BITS_PER_UNIT
), oalign
/ BITS_PER_UNIT
);
3112 gnu_result
= unchecked_convert (gnu_result_type
, gnu_result
,
3113 No_Truncation (gnat_node
));
3119 tree gnu_object
= gnat_to_gnu (Left_Opnd (gnat_node
));
3120 Node_Id gnat_range
= Right_Opnd (gnat_node
);
3124 /* GNAT_RANGE is either an N_Range node or an identifier
3125 denoting a subtype. */
3126 if (Nkind (gnat_range
) == N_Range
)
3128 gnu_low
= gnat_to_gnu (Low_Bound (gnat_range
));
3129 gnu_high
= gnat_to_gnu (High_Bound (gnat_range
));
3131 else if (Nkind (gnat_range
) == N_Identifier
3132 || Nkind (gnat_range
) == N_Expanded_Name
)
3134 tree gnu_range_type
= get_unpadded_type (Entity (gnat_range
));
3136 gnu_low
= TYPE_MIN_VALUE (gnu_range_type
);
3137 gnu_high
= TYPE_MAX_VALUE (gnu_range_type
);
3142 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
3144 /* If LOW and HIGH are identical, perform an equality test.
3145 Otherwise, ensure that GNU_OBJECT is only evaluated once
3146 and perform a full range test. */
3147 if (operand_equal_p (gnu_low
, gnu_high
, 0))
3148 gnu_result
= build_binary_op (EQ_EXPR
, gnu_result_type
,
3149 gnu_object
, gnu_low
);
3152 gnu_object
= protect_multiple_eval (gnu_object
);
3154 = build_binary_op (TRUTH_ANDIF_EXPR
, gnu_result_type
,
3155 build_binary_op (GE_EXPR
, gnu_result_type
,
3156 gnu_object
, gnu_low
),
3157 build_binary_op (LE_EXPR
, gnu_result_type
,
3158 gnu_object
, gnu_high
));
3161 if (Nkind (gnat_node
) == N_Not_In
)
3162 gnu_result
= invert_truthvalue (gnu_result
);
3167 gnu_lhs
= gnat_to_gnu (Left_Opnd (gnat_node
));
3168 gnu_rhs
= gnat_to_gnu (Right_Opnd (gnat_node
));
3169 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
3170 gnu_result
= build_binary_op (FLOAT_TYPE_P (gnu_result_type
)
3172 : (Rounded_Result (gnat_node
)
3173 ? ROUND_DIV_EXPR
: TRUNC_DIV_EXPR
),
3174 gnu_result_type
, gnu_lhs
, gnu_rhs
);
3177 case N_Op_Or
: case N_Op_And
: case N_Op_Xor
:
3178 /* These can either be operations on booleans or on modular types.
3179 Fall through for boolean types since that's the way GNU_CODES is
3181 if (IN (Ekind (Underlying_Type (Etype (gnat_node
))),
3182 Modular_Integer_Kind
))
3185 = (Nkind (gnat_node
) == N_Op_Or
? BIT_IOR_EXPR
3186 : Nkind (gnat_node
) == N_Op_And
? BIT_AND_EXPR
3189 gnu_lhs
= gnat_to_gnu (Left_Opnd (gnat_node
));
3190 gnu_rhs
= gnat_to_gnu (Right_Opnd (gnat_node
));
3191 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
3192 gnu_result
= build_binary_op (code
, gnu_result_type
,
3197 /* ... fall through ... */
3199 case N_Op_Eq
: case N_Op_Ne
: case N_Op_Lt
:
3200 case N_Op_Le
: case N_Op_Gt
: case N_Op_Ge
:
3201 case N_Op_Add
: case N_Op_Subtract
: case N_Op_Multiply
:
3202 case N_Op_Mod
: case N_Op_Rem
:
3203 case N_Op_Rotate_Left
:
3204 case N_Op_Rotate_Right
:
3205 case N_Op_Shift_Left
:
3206 case N_Op_Shift_Right
:
3207 case N_Op_Shift_Right_Arithmetic
:
3208 case N_And_Then
: case N_Or_Else
:
3210 enum tree_code code
= gnu_codes
[Nkind (gnat_node
)];
3213 gnu_lhs
= gnat_to_gnu (Left_Opnd (gnat_node
));
3214 gnu_rhs
= gnat_to_gnu (Right_Opnd (gnat_node
));
3215 gnu_type
= gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
3217 /* If this is a comparison operator, convert any references to
3218 an unconstrained array value into a reference to the
3220 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3222 gnu_lhs
= maybe_unconstrained_array (gnu_lhs
);
3223 gnu_rhs
= maybe_unconstrained_array (gnu_rhs
);
3226 /* If the result type is a private type, its full view may be a
3227 numeric subtype. The representation we need is that of its base
3228 type, given that it is the result of an arithmetic operation. */
3229 else if (Is_Private_Type (Etype (gnat_node
)))
3230 gnu_type
= gnu_result_type
3231 = get_unpadded_type (Base_Type (Full_View (Etype (gnat_node
))));
3233 /* If this is a shift whose count is not guaranteed to be correct,
3234 we need to adjust the shift count. */
3235 if (IN (Nkind (gnat_node
), N_Op_Shift
)
3236 && !Shift_Count_OK (gnat_node
))
3238 tree gnu_count_type
= get_base_type (TREE_TYPE (gnu_rhs
));
3240 = convert (gnu_count_type
, TYPE_SIZE (gnu_type
));
3242 if (Nkind (gnat_node
) == N_Op_Rotate_Left
3243 || Nkind (gnat_node
) == N_Op_Rotate_Right
)
3244 gnu_rhs
= build_binary_op (TRUNC_MOD_EXPR
, gnu_count_type
,
3245 gnu_rhs
, gnu_max_shift
);
3246 else if (Nkind (gnat_node
) == N_Op_Shift_Right_Arithmetic
)
3249 (MIN_EXPR
, gnu_count_type
,
3250 build_binary_op (MINUS_EXPR
,
3253 convert (gnu_count_type
,
3258 /* For right shifts, the type says what kind of shift to do,
3259 so we may need to choose a different type. */
3260 if (Nkind (gnat_node
) == N_Op_Shift_Right
3261 && !TYPE_UNSIGNED (gnu_type
))
3262 gnu_type
= gnat_unsigned_type (gnu_type
);
3263 else if (Nkind (gnat_node
) == N_Op_Shift_Right_Arithmetic
3264 && TYPE_UNSIGNED (gnu_type
))
3265 gnu_type
= gnat_signed_type (gnu_type
);
3267 if (gnu_type
!= gnu_result_type
)
3269 gnu_lhs
= convert (gnu_type
, gnu_lhs
);
3270 gnu_rhs
= convert (gnu_type
, gnu_rhs
);
3273 gnu_result
= build_binary_op (code
, gnu_type
, gnu_lhs
, gnu_rhs
);
3275 /* If this is a logical shift with the shift count not verified,
3276 we must return zero if it is too large. We cannot compensate
3277 above in this case. */
3278 if ((Nkind (gnat_node
) == N_Op_Shift_Left
3279 || Nkind (gnat_node
) == N_Op_Shift_Right
)
3280 && !Shift_Count_OK (gnat_node
))
3284 build_binary_op (GE_EXPR
, integer_type_node
,
3286 convert (TREE_TYPE (gnu_rhs
),
3287 TYPE_SIZE (gnu_type
))),
3288 convert (gnu_type
, integer_zero_node
),
3293 case N_Conditional_Expression
:
3295 tree gnu_cond
= gnat_to_gnu (First (Expressions (gnat_node
)));
3296 tree gnu_true
= gnat_to_gnu (Next (First (Expressions (gnat_node
))));
3298 = gnat_to_gnu (Next (Next (First (Expressions (gnat_node
)))));
3300 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
3301 gnu_result
= build_cond_expr (gnu_result_type
,
3302 gnat_truthvalue_conversion (gnu_cond
),
3303 gnu_true
, gnu_false
);
3308 gnu_result
= gnat_to_gnu (Right_Opnd (gnat_node
));
3309 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
3313 /* This case can apply to a boolean or a modular type.
3314 Fall through for a boolean operand since GNU_CODES is set
3315 up to handle this. */
3316 if (IN (Ekind (Etype (gnat_node
)), Modular_Integer_Kind
))
3318 gnu_expr
= gnat_to_gnu (Right_Opnd (gnat_node
));
3319 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
3320 gnu_result
= build_unary_op (BIT_NOT_EXPR
, gnu_result_type
,
3325 /* ... fall through ... */
3327 case N_Op_Minus
: case N_Op_Abs
:
3328 gnu_expr
= gnat_to_gnu (Right_Opnd (gnat_node
));
3330 if (Ekind (Etype (gnat_node
)) != E_Private_Type
)
3331 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
3333 gnu_result_type
= get_unpadded_type (Base_Type
3334 (Full_View (Etype (gnat_node
))));
3336 gnu_result
= build_unary_op (gnu_codes
[Nkind (gnat_node
)],
3337 gnu_result_type
, gnu_expr
);
3344 bool ignore_init_type
= false;
3346 gnat_temp
= Expression (gnat_node
);
3348 /* The Expression operand can either be an N_Identifier or
3349 Expanded_Name, which must represent a type, or a
3350 N_Qualified_Expression, which contains both the object type and an
3351 initial value for the object. */
3352 if (Nkind (gnat_temp
) == N_Identifier
3353 || Nkind (gnat_temp
) == N_Expanded_Name
)
3354 gnu_type
= gnat_to_gnu_type (Entity (gnat_temp
));
3355 else if (Nkind (gnat_temp
) == N_Qualified_Expression
)
3357 Entity_Id gnat_desig_type
3358 = Designated_Type (Underlying_Type (Etype (gnat_node
)));
3360 ignore_init_type
= Has_Constrained_Partial_View (gnat_desig_type
);
3361 gnu_init
= gnat_to_gnu (Expression (gnat_temp
));
3363 gnu_init
= maybe_unconstrained_array (gnu_init
);
3364 if (Do_Range_Check (Expression (gnat_temp
)))
3365 gnu_init
= emit_range_check (gnu_init
, gnat_desig_type
);
3367 if (Is_Elementary_Type (gnat_desig_type
)
3368 || Is_Constrained (gnat_desig_type
))
3370 gnu_type
= gnat_to_gnu_type (gnat_desig_type
);
3371 gnu_init
= convert (gnu_type
, gnu_init
);
3375 gnu_type
= gnat_to_gnu_type (Etype (Expression (gnat_temp
)));
3376 if (TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
)
3377 gnu_type
= TREE_TYPE (gnu_init
);
3379 gnu_init
= convert (gnu_type
, gnu_init
);
3385 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
3386 return build_allocator (gnu_type
, gnu_init
, gnu_result_type
,
3387 Procedure_To_Call (gnat_node
),
3388 Storage_Pool (gnat_node
), gnat_node
,
3393 /***************************/
3394 /* Chapter 5: Statements: */
3395 /***************************/
3398 gnu_result
= build1 (LABEL_EXPR
, void_type_node
,
3399 gnat_to_gnu (Identifier (gnat_node
)));
3402 case N_Null_Statement
:
3403 gnu_result
= alloc_stmt_list ();
3406 case N_Assignment_Statement
:
3407 /* Get the LHS and RHS of the statement and convert any reference to an
3408 unconstrained array into a reference to the underlying array.
3409 If we are not to do range checking and the RHS is an N_Function_Call,
3410 pass the LHS to the call function. */
3411 gnu_lhs
= maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node
)));
3413 /* If the type has a size that overflows, convert this into raise of
3414 Storage_Error: execution shouldn't have gotten here anyway. */
3415 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_lhs
))) == INTEGER_CST
3416 && TREE_OVERFLOW (TYPE_SIZE (TREE_TYPE (gnu_lhs
))))
3417 gnu_result
= build_call_raise (SE_Object_Too_Large
);
3418 else if (Nkind (Expression (gnat_node
)) == N_Function_Call
3419 && !Do_Range_Check (Expression (gnat_node
)))
3420 gnu_result
= call_to_gnu (Expression (gnat_node
),
3421 &gnu_result_type
, gnu_lhs
);
3425 = maybe_unconstrained_array (gnat_to_gnu (Expression (gnat_node
)));
3427 /* If range check is needed, emit code to generate it */
3428 if (Do_Range_Check (Expression (gnat_node
)))
3429 gnu_rhs
= emit_range_check (gnu_rhs
, Etype (Name (gnat_node
)));
3432 = build_binary_op (MODIFY_EXPR
, NULL_TREE
, gnu_lhs
, gnu_rhs
);
3436 case N_If_Statement
:
3438 tree
*gnu_else_ptr
; /* Point to put next "else if" or "else". */
3440 /* Make the outer COND_EXPR. Avoid non-determinism. */
3441 gnu_result
= build3 (COND_EXPR
, void_type_node
,
3442 gnat_to_gnu (Condition (gnat_node
)),
3443 NULL_TREE
, NULL_TREE
);
3444 COND_EXPR_THEN (gnu_result
)
3445 = build_stmt_group (Then_Statements (gnat_node
), false);
3446 TREE_SIDE_EFFECTS (gnu_result
) = 1;
3447 gnu_else_ptr
= &COND_EXPR_ELSE (gnu_result
);
3449 /* Now make a COND_EXPR for each of the "else if" parts. Put each
3450 into the previous "else" part and point to where to put any
3451 outer "else". Also avoid non-determinism. */
3452 if (Present (Elsif_Parts (gnat_node
)))
3453 for (gnat_temp
= First (Elsif_Parts (gnat_node
));
3454 Present (gnat_temp
); gnat_temp
= Next (gnat_temp
))
3456 gnu_expr
= build3 (COND_EXPR
, void_type_node
,
3457 gnat_to_gnu (Condition (gnat_temp
)),
3458 NULL_TREE
, NULL_TREE
);
3459 COND_EXPR_THEN (gnu_expr
)
3460 = build_stmt_group (Then_Statements (gnat_temp
), false);
3461 TREE_SIDE_EFFECTS (gnu_expr
) = 1;
3462 annotate_with_node (gnu_expr
, gnat_temp
);
3463 *gnu_else_ptr
= gnu_expr
;
3464 gnu_else_ptr
= &COND_EXPR_ELSE (gnu_expr
);
3467 *gnu_else_ptr
= build_stmt_group (Else_Statements (gnat_node
), false);
3471 case N_Case_Statement
:
3472 gnu_result
= Case_Statement_to_gnu (gnat_node
);
3475 case N_Loop_Statement
:
3476 gnu_result
= Loop_Statement_to_gnu (gnat_node
);
3479 case N_Block_Statement
:
3480 start_stmt_group ();
3482 process_decls (Declarations (gnat_node
), Empty
, Empty
, true, true);
3483 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node
)));
3485 gnu_result
= end_stmt_group ();
3487 if (Present (Identifier (gnat_node
)))
3488 mark_out_of_scope (Entity (Identifier (gnat_node
)));
3491 case N_Exit_Statement
:
3493 = build2 (EXIT_STMT
, void_type_node
,
3494 (Present (Condition (gnat_node
))
3495 ? gnat_to_gnu (Condition (gnat_node
)) : NULL_TREE
),
3496 (Present (Name (gnat_node
))
3497 ? get_gnu_tree (Entity (Name (gnat_node
)))
3498 : TREE_VALUE (gnu_loop_label_stack
)));
3501 case N_Return_Statement
:
3503 /* The gnu function type of the subprogram currently processed. */
3504 tree gnu_subprog_type
= TREE_TYPE (current_function_decl
);
3505 /* The return value from the subprogram. */
3506 tree gnu_ret_val
= NULL_TREE
;
3507 /* The place to put the return value. */
3509 /* Avoid passing error_mark_node to RETURN_EXPR. */
3510 gnu_result
= NULL_TREE
;
3512 /* If we are dealing with a "return;" from an Ada procedure with
3513 parameters passed by copy in copy out, we need to return a record
3514 containing the final values of these parameters. If the list
3515 contains only one entry, return just that entry.
3517 For a full description of the copy in copy out parameter mechanism,
3518 see the part of the gnat_to_gnu_entity routine dealing with the
3519 translation of subprograms.
3521 But if we have a return label defined, convert this into
3522 a branch to that label. */
3524 if (TREE_VALUE (gnu_return_label_stack
))
3526 gnu_result
= build1 (GOTO_EXPR
, void_type_node
,
3527 TREE_VALUE (gnu_return_label_stack
));
3531 else if (TYPE_CI_CO_LIST (gnu_subprog_type
))
3533 gnu_lhs
= DECL_RESULT (current_function_decl
);
3534 if (list_length (TYPE_CI_CO_LIST (gnu_subprog_type
)) == 1)
3535 gnu_ret_val
= TREE_VALUE (TYPE_CI_CO_LIST (gnu_subprog_type
));
3538 = gnat_build_constructor (TREE_TYPE (gnu_subprog_type
),
3539 TYPE_CI_CO_LIST (gnu_subprog_type
));
3542 /* If the Ada subprogram is a function, we just need to return the
3543 expression. If the subprogram returns an unconstrained
3544 array, we have to allocate a new version of the result and
3545 return it. If we return by reference, return a pointer. */
3547 else if (Present (Expression (gnat_node
)))
3549 /* If the current function returns by target pointer and we
3550 are doing a call, pass that target to the call. */
3551 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type
)
3552 && Nkind (Expression (gnat_node
)) == N_Function_Call
)
3555 = build_unary_op (INDIRECT_REF
, NULL_TREE
,
3556 DECL_ARGUMENTS (current_function_decl
));
3557 gnu_result
= call_to_gnu (Expression (gnat_node
),
3558 &gnu_result_type
, gnu_lhs
);
3562 gnu_ret_val
= gnat_to_gnu (Expression (gnat_node
));
3564 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type
))
3565 /* The original return type was unconstrained so dereference
3566 the TARGET pointer in the actual return value's type. */
3568 = build_unary_op (INDIRECT_REF
, TREE_TYPE (gnu_ret_val
),
3569 DECL_ARGUMENTS (current_function_decl
));
3571 gnu_lhs
= DECL_RESULT (current_function_decl
);
3573 /* Do not remove the padding from GNU_RET_VAL if the inner
3574 type is self-referential since we want to allocate the fixed
3575 size in that case. */
3576 if (TREE_CODE (gnu_ret_val
) == COMPONENT_REF
3577 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_ret_val
, 0)))
3579 && (TYPE_IS_PADDING_P
3580 (TREE_TYPE (TREE_OPERAND (gnu_ret_val
, 0))))
3581 && (CONTAINS_PLACEHOLDER_P
3582 (TYPE_SIZE (TREE_TYPE (gnu_ret_val
)))))
3583 gnu_ret_val
= TREE_OPERAND (gnu_ret_val
, 0);
3585 if (TYPE_RETURNS_BY_REF_P (gnu_subprog_type
)
3586 || By_Ref (gnat_node
))
3588 = build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_ret_val
);
3590 else if (TYPE_RETURNS_UNCONSTRAINED_P (gnu_subprog_type
))
3592 gnu_ret_val
= maybe_unconstrained_array (gnu_ret_val
);
3594 /* We have two cases: either the function returns with
3595 depressed stack or not. If not, we allocate on the
3596 secondary stack. If so, we allocate in the stack frame.
3597 if no copy is needed, the front end will set By_Ref,
3598 which we handle in the case above. */
3599 if (TYPE_RETURNS_STACK_DEPRESSED (gnu_subprog_type
))
3601 = build_allocator (TREE_TYPE (gnu_ret_val
),
3603 TREE_TYPE (gnu_subprog_type
),
3604 0, -1, gnat_node
, false);
3607 = build_allocator (TREE_TYPE (gnu_ret_val
),
3609 TREE_TYPE (gnu_subprog_type
),
3610 Procedure_To_Call (gnat_node
),
3611 Storage_Pool (gnat_node
),
3618 gnu_result
= build2 (MODIFY_EXPR
, TREE_TYPE (gnu_ret_val
),
3619 gnu_lhs
, gnu_ret_val
);
3621 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type
))
3623 add_stmt_with_node (gnu_result
, gnat_node
);
3624 gnu_result
= NULL_TREE
;
3627 gnu_result
= build1 (RETURN_EXPR
, void_type_node
, gnu_result
);
3631 case N_Goto_Statement
:
3632 gnu_result
= build1 (GOTO_EXPR
, void_type_node
,
3633 gnat_to_gnu (Name (gnat_node
)));
3636 /****************************/
3637 /* Chapter 6: Subprograms: */
3638 /****************************/
3640 case N_Subprogram_Declaration
:
3641 /* Unless there is a freeze node, declare the subprogram. We consider
3642 this a "definition" even though we're not generating code for
3643 the subprogram because we will be making the corresponding GCC
3646 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node
)))))
3647 gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node
)),
3649 gnu_result
= alloc_stmt_list ();
3652 case N_Abstract_Subprogram_Declaration
:
3653 /* This subprogram doesn't exist for code generation purposes, but we
3654 have to elaborate the types of any parameters, unless they are
3655 imported types (nothing to generate in this case). */
3657 = First_Formal (Defining_Entity (Specification (gnat_node
)));
3658 Present (gnat_temp
);
3659 gnat_temp
= Next_Formal_With_Extras (gnat_temp
))
3660 if (Is_Itype (Etype (gnat_temp
))
3661 && !From_With_Type (Etype (gnat_temp
)))
3662 gnat_to_gnu_entity (Etype (gnat_temp
), NULL_TREE
, 0);
3664 gnu_result
= alloc_stmt_list ();
3667 case N_Defining_Program_Unit_Name
:
3668 /* For a child unit identifier go up a level to get the
3669 specification. We get this when we try to find the spec of
3670 a child unit package that is the compilation unit being compiled. */
3671 gnu_result
= gnat_to_gnu (Parent (gnat_node
));
3674 case N_Subprogram_Body
:
3675 Subprogram_Body_to_gnu (gnat_node
);
3676 gnu_result
= alloc_stmt_list ();
3679 case N_Function_Call
:
3680 case N_Procedure_Call_Statement
:
3681 gnu_result
= call_to_gnu (gnat_node
, &gnu_result_type
, NULL_TREE
);
3684 /*************************/
3685 /* Chapter 7: Packages: */
3686 /*************************/
3688 case N_Package_Declaration
:
3689 gnu_result
= gnat_to_gnu (Specification (gnat_node
));
3692 case N_Package_Specification
:
3694 start_stmt_group ();
3695 process_decls (Visible_Declarations (gnat_node
),
3696 Private_Declarations (gnat_node
), Empty
, true, true);
3697 gnu_result
= end_stmt_group ();
3700 case N_Package_Body
:
3702 /* If this is the body of a generic package - do nothing */
3703 if (Ekind (Corresponding_Spec (gnat_node
)) == E_Generic_Package
)
3705 gnu_result
= alloc_stmt_list ();
3709 start_stmt_group ();
3710 process_decls (Declarations (gnat_node
), Empty
, Empty
, true, true);
3712 if (Present (Handled_Statement_Sequence (gnat_node
)))
3713 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node
)));
3715 gnu_result
= end_stmt_group ();
3718 /*********************************/
3719 /* Chapter 8: Visibility Rules: */
3720 /*********************************/
3722 case N_Use_Package_Clause
:
3723 case N_Use_Type_Clause
:
3724 /* Nothing to do here - but these may appear in list of declarations */
3725 gnu_result
= alloc_stmt_list ();
3728 /***********************/
3729 /* Chapter 9: Tasks: */
3730 /***********************/
3732 case N_Protected_Type_Declaration
:
3733 gnu_result
= alloc_stmt_list ();
3736 case N_Single_Task_Declaration
:
3737 gnat_to_gnu_entity (Defining_Entity (gnat_node
), NULL_TREE
, 1);
3738 gnu_result
= alloc_stmt_list ();
3741 /***********************************************************/
3742 /* Chapter 10: Program Structure and Compilation Issues: */
3743 /***********************************************************/
3745 case N_Compilation_Unit
:
3747 /* This is not called for the main unit, which is handled in function
3749 start_stmt_group ();
3752 Compilation_Unit_to_gnu (gnat_node
);
3753 gnu_result
= alloc_stmt_list ();
3756 case N_Subprogram_Body_Stub
:
3757 case N_Package_Body_Stub
:
3758 case N_Protected_Body_Stub
:
3759 case N_Task_Body_Stub
:
3760 /* Simply process whatever unit is being inserted. */
3761 gnu_result
= gnat_to_gnu (Unit (Library_Unit (gnat_node
)));
3765 gnu_result
= gnat_to_gnu (Proper_Body (gnat_node
));
3768 /***************************/
3769 /* Chapter 11: Exceptions: */
3770 /***************************/
3772 case N_Handled_Sequence_Of_Statements
:
3773 /* If there is an At_End procedure attached to this node, and the EH
3774 mechanism is SJLJ, we must have at least a corresponding At_End
3775 handler, unless the No_Exception_Handlers restriction is set. */
3776 gcc_assert (type_annotate_only
3777 || Exception_Mechanism
!= Setjmp_Longjmp
3778 || No (At_End_Proc (gnat_node
))
3779 || Present (Exception_Handlers (gnat_node
))
3780 || No_Exception_Handlers_Set ());
3782 gnu_result
= Handled_Sequence_Of_Statements_to_gnu (gnat_node
);
3785 case N_Exception_Handler
:
3786 if (Exception_Mechanism
== Setjmp_Longjmp
)
3787 gnu_result
= Exception_Handler_to_gnu_sjlj (gnat_node
);
3788 else if (Exception_Mechanism
== GCC_ZCX
)
3789 gnu_result
= Exception_Handler_to_gnu_zcx (gnat_node
);
3795 /*******************************/
3796 /* Chapter 12: Generic Units: */
3797 /*******************************/
3799 case N_Generic_Function_Renaming_Declaration
:
3800 case N_Generic_Package_Renaming_Declaration
:
3801 case N_Generic_Procedure_Renaming_Declaration
:
3802 case N_Generic_Package_Declaration
:
3803 case N_Generic_Subprogram_Declaration
:
3804 case N_Package_Instantiation
:
3805 case N_Procedure_Instantiation
:
3806 case N_Function_Instantiation
:
3807 /* These nodes can appear on a declaration list but there is nothing to
3808 to be done with them. */
3809 gnu_result
= alloc_stmt_list ();
3812 /***************************************************/
3813 /* Chapter 13: Representation Clauses and */
3814 /* Implementation-Dependent Features: */
3815 /***************************************************/
3817 case N_Attribute_Definition_Clause
:
3819 gnu_result
= alloc_stmt_list ();
3821 /* The only one we need deal with is for 'Address. For the others, SEM
3822 puts the information elsewhere. We need only deal with 'Address
3823 if the object has a Freeze_Node (which it never will currently). */
3824 if (Get_Attribute_Id (Chars (gnat_node
)) != Attr_Address
3825 || No (Freeze_Node (Entity (Name (gnat_node
)))))
3828 /* Get the value to use as the address and save it as the
3829 equivalent for GNAT_TEMP. When the object is frozen,
3830 gnat_to_gnu_entity will do the right thing. */
3831 save_gnu_tree (Entity (Name (gnat_node
)),
3832 gnat_to_gnu (Expression (gnat_node
)), true);
3835 case N_Enumeration_Representation_Clause
:
3836 case N_Record_Representation_Clause
:
3838 /* We do nothing with these. SEM puts the information elsewhere. */
3839 gnu_result
= alloc_stmt_list ();
3842 case N_Code_Statement
:
3843 if (!type_annotate_only
)
3845 tree gnu_template
= gnat_to_gnu (Asm_Template (gnat_node
));
3846 tree gnu_input_list
= NULL_TREE
, gnu_output_list
= NULL_TREE
;
3847 tree gnu_clobber_list
= NULL_TREE
;
3850 /* First process inputs, then outputs, then clobbers. */
3851 Setup_Asm_Inputs (gnat_node
);
3852 while (Present (gnat_temp
= Asm_Input_Value ()))
3854 tree gnu_value
= gnat_to_gnu (gnat_temp
);
3855 tree gnu_constr
= build_tree_list (NULL_TREE
, gnat_to_gnu
3856 (Asm_Input_Constraint ()));
3859 = tree_cons (gnu_constr
, gnu_value
, gnu_input_list
);
3863 Setup_Asm_Outputs (gnat_node
);
3864 while (Present (gnat_temp
= Asm_Output_Variable ()))
3866 tree gnu_value
= gnat_to_gnu (gnat_temp
);
3867 tree gnu_constr
= build_tree_list (NULL_TREE
, gnat_to_gnu
3868 (Asm_Output_Constraint ()));
3871 = tree_cons (gnu_constr
, gnu_value
, gnu_output_list
);
3875 Clobber_Setup (gnat_node
);
3876 while ((clobber
= Clobber_Get_Next ()))
3878 = tree_cons (NULL_TREE
,
3879 build_string (strlen (clobber
) + 1, clobber
),
3882 gnu_input_list
= nreverse (gnu_input_list
);
3883 gnu_output_list
= nreverse (gnu_output_list
);
3884 gnu_result
= build4 (ASM_EXPR
, void_type_node
,
3885 gnu_template
, gnu_output_list
,
3886 gnu_input_list
, gnu_clobber_list
);
3887 ASM_VOLATILE_P (gnu_result
) = Is_Asm_Volatile (gnat_node
);
3890 gnu_result
= alloc_stmt_list ();
3894 /***************************************************/
3896 /***************************************************/
3898 case N_Freeze_Entity
:
3899 start_stmt_group ();
3900 process_freeze_entity (gnat_node
);
3901 process_decls (Actions (gnat_node
), Empty
, Empty
, true, true);
3902 gnu_result
= end_stmt_group ();
3905 case N_Itype_Reference
:
3906 if (!present_gnu_tree (Itype (gnat_node
)))
3907 process_type (Itype (gnat_node
));
3909 gnu_result
= alloc_stmt_list ();
3912 case N_Free_Statement
:
3913 if (!type_annotate_only
)
3915 tree gnu_ptr
= gnat_to_gnu (Expression (gnat_node
));
3920 /* If this is a thin pointer, we must dereference it to create
3921 a fat pointer, then go back below to a thin pointer. The
3922 reason for this is that we need a fat pointer someplace in
3923 order to properly compute the size. */
3924 if (TYPE_THIN_POINTER_P (TREE_TYPE (gnu_ptr
)))
3925 gnu_ptr
= build_unary_op (ADDR_EXPR
, NULL_TREE
,
3926 build_unary_op (INDIRECT_REF
, NULL_TREE
,
3929 /* If this is an unconstrained array, we know the object must
3930 have been allocated with the template in front of the object.
3931 So pass the template address, but get the total size. Do this
3932 by converting to a thin pointer. */
3933 if (TYPE_FAT_POINTER_P (TREE_TYPE (gnu_ptr
)))
3935 = convert (build_pointer_type
3936 (TYPE_OBJECT_RECORD_TYPE
3937 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr
)))),
3940 gnu_obj_type
= TREE_TYPE (TREE_TYPE (gnu_ptr
));
3941 gnu_obj_size
= TYPE_SIZE_UNIT (gnu_obj_type
);
3942 align
= TYPE_ALIGN (gnu_obj_type
);
3944 if (TREE_CODE (gnu_obj_type
) == RECORD_TYPE
3945 && TYPE_CONTAINS_TEMPLATE_P (gnu_obj_type
))
3947 tree gnu_char_ptr_type
= build_pointer_type (char_type_node
);
3948 tree gnu_pos
= byte_position (TYPE_FIELDS (gnu_obj_type
));
3949 tree gnu_byte_offset
3950 = convert (gnu_char_ptr_type
,
3951 size_diffop (size_zero_node
, gnu_pos
));
3953 gnu_ptr
= convert (gnu_char_ptr_type
, gnu_ptr
);
3954 gnu_ptr
= build_binary_op (MINUS_EXPR
, gnu_char_ptr_type
,
3955 gnu_ptr
, gnu_byte_offset
);
3958 gnu_result
= build_call_alloc_dealloc (gnu_ptr
, gnu_obj_size
, align
,
3959 Procedure_To_Call (gnat_node
),
3960 Storage_Pool (gnat_node
),
3965 case N_Raise_Constraint_Error
:
3966 case N_Raise_Program_Error
:
3967 case N_Raise_Storage_Error
:
3968 if (type_annotate_only
)
3970 gnu_result
= alloc_stmt_list ();
3974 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
3975 gnu_result
= build_call_raise (UI_To_Int (Reason (gnat_node
)));
3977 /* If the type is VOID, this is a statement, so we need to
3978 generate the code for the call. Handle a Condition, if there
3980 if (TREE_CODE (gnu_result_type
) == VOID_TYPE
)
3982 annotate_with_node (gnu_result
, gnat_node
);
3984 if (Present (Condition (gnat_node
)))
3985 gnu_result
= build3 (COND_EXPR
, void_type_node
,
3986 gnat_to_gnu (Condition (gnat_node
)),
3987 gnu_result
, alloc_stmt_list ());
3990 gnu_result
= build1 (NULL_EXPR
, gnu_result_type
, gnu_result
);
3993 case N_Validate_Unchecked_Conversion
:
3994 /* If the result is a pointer type, see if we are either converting
3995 from a non-pointer or from a pointer to a type with a different
3996 alias set and warn if so. If the result defined in the same unit as
3997 this unchecked conversion, we can allow this because we can know to
3998 make that type have alias set 0. */
4000 tree gnu_source_type
= gnat_to_gnu_type (Source_Type (gnat_node
));
4001 tree gnu_target_type
= gnat_to_gnu_type (Target_Type (gnat_node
));
4003 if (POINTER_TYPE_P (gnu_target_type
)
4004 && !In_Same_Source_Unit (Target_Type (gnat_node
), gnat_node
)
4005 && get_alias_set (TREE_TYPE (gnu_target_type
)) != 0
4006 && !No_Strict_Aliasing (Underlying_Type (Target_Type (gnat_node
)))
4007 && (!POINTER_TYPE_P (gnu_source_type
)
4008 || (get_alias_set (TREE_TYPE (gnu_source_type
))
4009 != get_alias_set (TREE_TYPE (gnu_target_type
)))))
4012 ("?possible aliasing problem for type&",
4013 gnat_node
, Target_Type (gnat_node
));
4015 ("\\?use -fno-strict-aliasing switch for references",
4018 ("\\?or use `pragma No_Strict_Aliasing (&);`",
4019 gnat_node
, Target_Type (gnat_node
));
4022 /* The No_Strict_Aliasing flag is not propagated to the back-end for
4023 fat pointers so unconditionally warn in problematic cases. */
4024 else if (TYPE_FAT_POINTER_P (gnu_target_type
))
4027 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_target_type
)));
4029 if (get_alias_set (array_type
) != 0
4030 && (!TYPE_FAT_POINTER_P (gnu_source_type
)
4031 || (get_alias_set (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_source_type
))))
4032 != get_alias_set (array_type
))))
4035 ("?possible aliasing problem for type&",
4036 gnat_node
, Target_Type (gnat_node
));
4038 ("\\?use -fno-strict-aliasing switch for references",
4043 gnu_result
= alloc_stmt_list ();
4046 case N_Raise_Statement
:
4047 case N_Function_Specification
:
4048 case N_Procedure_Specification
:
4050 case N_Component_Association
:
4053 gcc_assert (type_annotate_only
);
4054 gnu_result
= alloc_stmt_list ();
4057 /* If we pushed our level as part of processing the elaboration routine,
4059 if (went_into_elab_proc
)
4061 add_stmt (gnu_result
);
4063 gnu_result
= end_stmt_group ();
4064 current_function_decl
= NULL_TREE
;
4067 /* Set the location information into the result. Note that we may have
4068 no result if we tried to build a CALL_EXPR node to a procedure with
4069 no side-effects and optimization is enabled. */
4070 if (gnu_result
&& EXPR_P (gnu_result
))
4071 annotate_with_node (gnu_result
, gnat_node
);
4073 /* If we're supposed to return something of void_type, it means we have
4074 something we're elaborating for effect, so just return. */
4075 if (TREE_CODE (gnu_result_type
) == VOID_TYPE
)
4078 /* If the result is a constant that overflows, raise constraint error. */
4079 else if (TREE_CODE (gnu_result
) == INTEGER_CST
4080 && TREE_CONSTANT_OVERFLOW (gnu_result
))
4082 post_error ("Constraint_Error will be raised at run-time?", gnat_node
);
4085 = build1 (NULL_EXPR
, gnu_result_type
,
4086 build_call_raise (CE_Overflow_Check_Failed
));
4089 /* If our result has side-effects and is of an unconstrained type,
4090 make a SAVE_EXPR so that we can be sure it will only be referenced
4091 once. Note we must do this before any conversions. */
4092 if (TREE_SIDE_EFFECTS (gnu_result
)
4093 && (TREE_CODE (gnu_result_type
) == UNCONSTRAINED_ARRAY_TYPE
4094 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type
))))
4095 gnu_result
= gnat_stabilize_reference (gnu_result
, 0);
4097 /* Now convert the result to the proper type. If the type is void or if
4098 we have no result, return error_mark_node to show we have no result.
4099 If the type of the result is correct or if we have a label (which doesn't
4100 have any well-defined type), return our result. Also don't do the
4101 conversion if the "desired" type involves a PLACEHOLDER_EXPR in its size
4102 since those are the cases where the front end may have the type wrong due
4103 to "instantiating" the unconstrained record with discriminant values
4104 or if this is a FIELD_DECL. If this is the Name of an assignment
4105 statement or a parameter of a procedure call, return what we have since
4106 the RHS has to be converted to our type there in that case, unless
4107 GNU_RESULT_TYPE has a simpler size. Similarly, if the two types are
4108 record types with the same name, the expression type has integral mode,
4109 and GNU_RESULT_TYPE BLKmode, don't convert. This will be the case when
4110 we are converting from a packable type to its actual type and we need
4111 those conversions to be NOPs in order for assignments into these types to
4112 work properly if the inner object is a bitfield and hence can't have
4113 its address taken. Finally, don't convert integral types that are the
4114 operand of an unchecked conversion since we need to ignore those
4115 conversions (for 'Valid). Otherwise, convert the result to the proper
4118 if (Present (Parent (gnat_node
))
4119 && ((Nkind (Parent (gnat_node
)) == N_Assignment_Statement
4120 && Name (Parent (gnat_node
)) == gnat_node
)
4121 || (Nkind (Parent (gnat_node
)) == N_Procedure_Call_Statement
4122 && Name (Parent (gnat_node
)) != gnat_node
)
4123 || (Nkind (Parent (gnat_node
)) == N_Unchecked_Type_Conversion
4124 && !AGGREGATE_TYPE_P (gnu_result_type
)
4125 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_result
)))
4126 || Nkind (Parent (gnat_node
)) == N_Parameter_Association
)
4127 && !(TYPE_SIZE (gnu_result_type
)
4128 && TYPE_SIZE (TREE_TYPE (gnu_result
))
4129 && (AGGREGATE_TYPE_P (gnu_result_type
)
4130 == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result
)))
4131 && ((TREE_CODE (TYPE_SIZE (gnu_result_type
)) == INTEGER_CST
4132 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result
)))
4134 || (TREE_CODE (TYPE_SIZE (gnu_result_type
)) != INTEGER_CST
4135 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type
))
4136 && (CONTAINS_PLACEHOLDER_P
4137 (TYPE_SIZE (TREE_TYPE (gnu_result
))))))
4138 && !(TREE_CODE (gnu_result_type
) == RECORD_TYPE
4139 && TYPE_JUSTIFIED_MODULAR_P (gnu_result_type
))))
4141 /* In this case remove padding only if the inner object is of
4142 self-referential size: in that case it must be an object of
4143 unconstrained type with a default discriminant. In other cases,
4144 we want to avoid copying too much data. */
4145 if (TREE_CODE (TREE_TYPE (gnu_result
)) == RECORD_TYPE
4146 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result
))
4147 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE
4148 (TREE_TYPE (TYPE_FIELDS
4149 (TREE_TYPE (gnu_result
))))))
4150 gnu_result
= convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result
))),
4154 else if (TREE_CODE (gnu_result
) == LABEL_DECL
4155 || TREE_CODE (gnu_result
) == FIELD_DECL
4156 || TREE_CODE (gnu_result
) == ERROR_MARK
4157 || (TYPE_SIZE (gnu_result_type
)
4158 && TREE_CODE (TYPE_SIZE (gnu_result_type
)) != INTEGER_CST
4159 && TREE_CODE (gnu_result
) != INDIRECT_REF
4160 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type
)))
4161 || ((TYPE_NAME (gnu_result_type
)
4162 == TYPE_NAME (TREE_TYPE (gnu_result
)))
4163 && TREE_CODE (gnu_result_type
) == RECORD_TYPE
4164 && TREE_CODE (TREE_TYPE (gnu_result
)) == RECORD_TYPE
4165 && TYPE_MODE (gnu_result_type
) == BLKmode
4166 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (gnu_result
)))
4169 /* Remove any padding record, but do nothing more in this case. */
4170 if (TREE_CODE (TREE_TYPE (gnu_result
)) == RECORD_TYPE
4171 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result
)))
4172 gnu_result
= convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result
))),
4176 else if (gnu_result
== error_mark_node
4177 || gnu_result_type
== void_type_node
)
4178 gnu_result
= error_mark_node
;
4179 else if (gnu_result_type
!= TREE_TYPE (gnu_result
))
4180 gnu_result
= convert (gnu_result_type
, gnu_result
);
4182 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on GNU_RESULT. */
4183 while ((TREE_CODE (gnu_result
) == NOP_EXPR
4184 || TREE_CODE (gnu_result
) == NON_LVALUE_EXPR
)
4185 && TREE_TYPE (TREE_OPERAND (gnu_result
, 0)) == TREE_TYPE (gnu_result
))
4186 gnu_result
= TREE_OPERAND (gnu_result
, 0);
4191 /* Record the current code position in GNAT_NODE. */
4194 record_code_position (Node_Id gnat_node
)
4196 tree stmt_stmt
= build1 (STMT_STMT
, void_type_node
, NULL_TREE
);
4198 add_stmt_with_node (stmt_stmt
, gnat_node
);
4199 save_gnu_tree (gnat_node
, stmt_stmt
, true);
4202 /* Insert the code for GNAT_NODE at the position saved for that node. */
4205 insert_code_for (Node_Id gnat_node
)
4207 STMT_STMT_STMT (get_gnu_tree (gnat_node
)) = gnat_to_gnu (gnat_node
);
4208 save_gnu_tree (gnat_node
, NULL_TREE
, true);
4211 /* Start a new statement group chained to the previous group. */
4216 struct stmt_group
*group
= stmt_group_free_list
;
4218 /* First see if we can get one from the free list. */
4220 stmt_group_free_list
= group
->previous
;
4222 group
= (struct stmt_group
*) ggc_alloc (sizeof (struct stmt_group
));
4224 group
->previous
= current_stmt_group
;
4225 group
->stmt_list
= group
->block
= group
->cleanups
= NULL_TREE
;
4226 current_stmt_group
= group
;
4229 /* Add GNU_STMT to the current statement group. */
4232 add_stmt (tree gnu_stmt
)
4234 append_to_statement_list (gnu_stmt
, ¤t_stmt_group
->stmt_list
);
4236 /* If we're at top level, show everything in here is in use in case
4237 any of it is shared by a subprogram. */
4238 if (global_bindings_p ())
4239 walk_tree (&gnu_stmt
, mark_visited
, NULL
, NULL
);
4243 /* Similar, but set the location of GNU_STMT to that of GNAT_NODE. */
4246 add_stmt_with_node (tree gnu_stmt
, Node_Id gnat_node
)
4248 if (Present (gnat_node
))
4249 annotate_with_node (gnu_stmt
, gnat_node
);
4250 add_stmt (gnu_stmt
);
4253 /* Add a declaration statement for GNU_DECL to the current statement group.
4254 Get SLOC from Entity_Id. */
4257 add_decl_expr (tree gnu_decl
, Entity_Id gnat_entity
)
4261 /* If this is a variable that Gigi is to ignore, we may have been given
4262 an ERROR_MARK. So test for it. We also might have been given a
4263 reference for a renaming. So only do something for a decl. Also
4264 ignore a TYPE_DECL for an UNCONSTRAINED_ARRAY_TYPE. */
4265 if (!DECL_P (gnu_decl
)
4266 || (TREE_CODE (gnu_decl
) == TYPE_DECL
4267 && TREE_CODE (TREE_TYPE (gnu_decl
)) == UNCONSTRAINED_ARRAY_TYPE
))
4270 /* If we are global, we don't want to actually output the DECL_EXPR for
4271 this decl since we already have evaluated the expressions in the
4272 sizes and positions as globals and doing it again would be wrong.
4273 But we do have to mark everything as used. */
4274 gnu_stmt
= build1 (DECL_EXPR
, void_type_node
, gnu_decl
);
4275 if (!global_bindings_p ())
4276 add_stmt_with_node (gnu_stmt
, gnat_entity
);
4279 walk_tree (&gnu_stmt
, mark_visited
, NULL
, NULL
);
4280 if (TREE_CODE (gnu_decl
) == VAR_DECL
4281 || TREE_CODE (gnu_decl
) == CONST_DECL
)
4283 walk_tree (&DECL_SIZE (gnu_decl
), mark_visited
, NULL
, NULL
);
4284 walk_tree (&DECL_SIZE_UNIT (gnu_decl
), mark_visited
, NULL
, NULL
);
4285 walk_tree (&DECL_INITIAL (gnu_decl
), mark_visited
, NULL
, NULL
);
4289 /* If this is a DECL_EXPR for a variable with DECL_INITIAL set,
4290 there are two cases we need to handle here. */
4291 if (TREE_CODE (gnu_decl
) == VAR_DECL
&& DECL_INITIAL (gnu_decl
))
4293 tree gnu_init
= DECL_INITIAL (gnu_decl
);
4294 tree gnu_lhs
= NULL_TREE
;
4296 /* If this is a DECL_EXPR for a variable with DECL_INITIAL set
4297 and decl has a padded type, convert it to the unpadded type so the
4298 assignment is done properly. */
4299 if (TREE_CODE (TREE_TYPE (gnu_decl
)) == RECORD_TYPE
4300 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_decl
)))
4302 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_decl
))), gnu_decl
);
4304 /* Otherwise, if this is going into memory and the initializer isn't
4305 valid for the assembler and loader. Gimplification could do this,
4306 but would be run too late if -fno-unit-at-a-time. */
4307 else if (TREE_STATIC (gnu_decl
)
4308 && !initializer_constant_valid_p (gnu_init
,
4309 TREE_TYPE (gnu_decl
)))
4314 tree gnu_assign_stmt
4315 = build_binary_op (MODIFY_EXPR
, NULL_TREE
,
4316 gnu_lhs
, DECL_INITIAL (gnu_decl
));
4318 DECL_INITIAL (gnu_decl
) = 0;
4319 TREE_READONLY (gnu_decl
) = 0;
4320 annotate_with_locus (gnu_assign_stmt
,
4321 DECL_SOURCE_LOCATION (gnu_decl
));
4322 add_stmt (gnu_assign_stmt
);
4327 /* Utility function to mark nodes with TREE_VISITED and types as having their
4328 sized gimplified. Called from walk_tree. We use this to indicate all
4329 variable sizes and positions in global types may not be shared by any
4333 mark_visited (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
4335 if (TREE_VISITED (*tp
))
4338 /* Don't mark a dummy type as visited because we want to mark its sizes
4339 and fields once it's filled in. */
4340 else if (!TYPE_IS_DUMMY_P (*tp
))
4341 TREE_VISITED (*tp
) = 1;
4344 TYPE_SIZES_GIMPLIFIED (*tp
) = 1;
4349 /* Likewise, but to mark as unvisited. */
4352 mark_unvisited (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
4353 void *data ATTRIBUTE_UNUSED
)
4355 TREE_VISITED (*tp
) = 0;
4360 /* Add GNU_CLEANUP, a cleanup action, to the current code group. */
4363 add_cleanup (tree gnu_cleanup
)
4365 append_to_statement_list (gnu_cleanup
, ¤t_stmt_group
->cleanups
);
4368 /* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */
4371 set_block_for_group (tree gnu_block
)
4373 gcc_assert (!current_stmt_group
->block
);
4374 current_stmt_group
->block
= gnu_block
;
4377 /* Return code corresponding to the current code group. It is normally
4378 a STATEMENT_LIST, but may also be a BIND_EXPR or TRY_FINALLY_EXPR if
4379 BLOCK or cleanups were set. */
4384 struct stmt_group
*group
= current_stmt_group
;
4385 tree gnu_retval
= group
->stmt_list
;
4387 /* If this is a null list, allocate a new STATEMENT_LIST. Then, if there
4388 are cleanups, make a TRY_FINALLY_EXPR. Last, if there is a BLOCK,
4389 make a BIND_EXPR. Note that we nest in that because the cleanup may
4390 reference variables in the block. */
4391 if (gnu_retval
== NULL_TREE
)
4392 gnu_retval
= alloc_stmt_list ();
4394 if (group
->cleanups
)
4395 gnu_retval
= build2 (TRY_FINALLY_EXPR
, void_type_node
, gnu_retval
,
4398 if (current_stmt_group
->block
)
4399 gnu_retval
= build3 (BIND_EXPR
, void_type_node
, BLOCK_VARS (group
->block
),
4400 gnu_retval
, group
->block
);
4402 /* Remove this group from the stack and add it to the free list. */
4403 current_stmt_group
= group
->previous
;
4404 group
->previous
= stmt_group_free_list
;
4405 stmt_group_free_list
= group
;
4410 /* Add a list of statements from GNAT_LIST, a possibly-empty list of
4414 add_stmt_list (List_Id gnat_list
)
4418 if (Present (gnat_list
))
4419 for (gnat_node
= First (gnat_list
); Present (gnat_node
);
4420 gnat_node
= Next (gnat_node
))
4421 add_stmt (gnat_to_gnu (gnat_node
));
4424 /* Build a tree from GNAT_LIST, a possibly-empty list of statements.
4425 If BINDING_P is true, push and pop a binding level around the list. */
4428 build_stmt_group (List_Id gnat_list
, bool binding_p
)
4430 start_stmt_group ();
4434 add_stmt_list (gnat_list
);
4438 return end_stmt_group ();
4441 /* Push and pop routines for stacks. We keep a free list around so we
4442 don't waste tree nodes. */
4445 push_stack (tree
*gnu_stack_ptr
, tree gnu_purpose
, tree gnu_value
)
4447 tree gnu_node
= gnu_stack_free_list
;
4451 gnu_stack_free_list
= TREE_CHAIN (gnu_node
);
4452 TREE_CHAIN (gnu_node
) = *gnu_stack_ptr
;
4453 TREE_PURPOSE (gnu_node
) = gnu_purpose
;
4454 TREE_VALUE (gnu_node
) = gnu_value
;
4457 gnu_node
= tree_cons (gnu_purpose
, gnu_value
, *gnu_stack_ptr
);
4459 *gnu_stack_ptr
= gnu_node
;
4463 pop_stack (tree
*gnu_stack_ptr
)
4465 tree gnu_node
= *gnu_stack_ptr
;
4467 *gnu_stack_ptr
= TREE_CHAIN (gnu_node
);
4468 TREE_CHAIN (gnu_node
) = gnu_stack_free_list
;
4469 gnu_stack_free_list
= gnu_node
;
4472 /* GNU_STMT is a statement. We generate code for that statement. */
4475 gnat_expand_stmt (tree gnu_stmt
)
4478 tree gnu_elmt
, gnu_elmt_2
;
4481 switch (TREE_CODE (gnu_stmt
))
4485 /* First write a volatile ASM_INPUT to prevent anything from being
4487 gnu_elmt
= gen_rtx_ASM_INPUT (VOIDmode
, "");
4488 MEM_VOLATILE_P (gnu_elmt
) = 1;
4489 emit_insn (gnu_elmt
);
4491 gnu_elmt
= expand_expr (TREE_OPERAND (gnu_stmt
, 0), NULL_RTX
, VOIDmode
,
4493 emit_insn (gen_rtx_USE (VOIDmode
, ));
4502 /* Generate GIMPLE in place for the expression at *EXPR_P. */
4505 gnat_gimplify_expr (tree
*expr_p
, tree
*pre_p
, tree
*post_p ATTRIBUTE_UNUSED
)
4507 tree expr
= *expr_p
;
4509 if (IS_ADA_STMT (expr
))
4510 return gnat_gimplify_stmt (expr_p
);
4512 switch (TREE_CODE (expr
))
4515 /* If this is for a scalar, just make a VAR_DECL for it. If for
4516 an aggregate, get a null pointer of the appropriate type and
4518 if (AGGREGATE_TYPE_P (TREE_TYPE (expr
)))
4519 *expr_p
= build1 (INDIRECT_REF
, TREE_TYPE (expr
),
4520 convert (build_pointer_type (TREE_TYPE (expr
)),
4521 integer_zero_node
));
4524 *expr_p
= create_tmp_var (TREE_TYPE (expr
), NULL
);
4525 TREE_NO_WARNING (*expr_p
) = 1;
4528 append_to_statement_list (TREE_OPERAND (expr
, 0), pre_p
);
4531 case UNCONSTRAINED_ARRAY_REF
:
4532 /* We should only do this if we are just elaborating for side-effects,
4533 but we can't know that yet. */
4534 *expr_p
= TREE_OPERAND (*expr_p
, 0);
4538 /* If we're taking the address of a constant CONSTRUCTOR, force it to
4539 be put into static memory. We know it's going to be readonly given
4540 the semantics we have and it's required to be static memory in
4541 the case when the reference is in an elaboration procedure. */
4542 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == CONSTRUCTOR
4543 && TREE_CONSTANT (TREE_OPERAND (expr
, 0)))
4546 = create_tmp_var (TREE_TYPE (TREE_OPERAND (expr
, 0)), "C");
4548 TREE_READONLY (new_var
) = 1;
4549 TREE_STATIC (new_var
) = 1;
4550 TREE_ADDRESSABLE (new_var
) = 1;
4551 DECL_INITIAL (new_var
) = TREE_OPERAND (expr
, 0);
4553 TREE_OPERAND (expr
, 0) = new_var
;
4554 recompute_tree_invarant_for_addr_expr (expr
);
4557 return GS_UNHANDLED
;
4560 /* We have a kludge here. If the FIELD_DECL is from a fat pointer and is
4561 from an early dummy type, replace it with the proper FIELD_DECL. */
4562 if (TYPE_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (*expr_p
, 0)))
4563 && DECL_ORIGINAL_FIELD (TREE_OPERAND (*expr_p
, 1)))
4565 TREE_OPERAND (*expr_p
, 1)
4566 = DECL_ORIGINAL_FIELD (TREE_OPERAND (*expr_p
, 1));
4570 /* ... fall through ... */
4573 return GS_UNHANDLED
;
4577 /* Generate GIMPLE in place for the statement at *STMT_P. */
4579 static enum gimplify_status
4580 gnat_gimplify_stmt (tree
*stmt_p
)
4582 tree stmt
= *stmt_p
;
4584 switch (TREE_CODE (stmt
))
4587 *stmt_p
= STMT_STMT_STMT (stmt
);
4591 *stmt_p
= NULL_TREE
;
4596 tree gnu_start_label
= create_artificial_label ();
4597 tree gnu_end_label
= LOOP_STMT_LABEL (stmt
);
4599 /* Set to emit the statements of the loop. */
4600 *stmt_p
= NULL_TREE
;
4602 /* We first emit the start label and then a conditional jump to
4603 the end label if there's a top condition, then the body of the
4604 loop, then a conditional branch to the end label, then the update,
4605 if any, and finally a jump to the start label and the definition
4606 of the end label. */
4607 append_to_statement_list (build1 (LABEL_EXPR
, void_type_node
,
4611 if (LOOP_STMT_TOP_COND (stmt
))
4612 append_to_statement_list (build3 (COND_EXPR
, void_type_node
,
4613 LOOP_STMT_TOP_COND (stmt
),
4620 append_to_statement_list (LOOP_STMT_BODY (stmt
), stmt_p
);
4622 if (LOOP_STMT_BOT_COND (stmt
))
4623 append_to_statement_list (build3 (COND_EXPR
, void_type_node
,
4624 LOOP_STMT_BOT_COND (stmt
),
4631 if (LOOP_STMT_UPDATE (stmt
))
4632 append_to_statement_list (LOOP_STMT_UPDATE (stmt
), stmt_p
);
4634 append_to_statement_list (build1 (GOTO_EXPR
, void_type_node
,
4637 append_to_statement_list (build1 (LABEL_EXPR
, void_type_node
,
4644 /* Build a statement to jump to the corresponding end label, then
4645 see if it needs to be conditional. */
4646 *stmt_p
= build1 (GOTO_EXPR
, void_type_node
, EXIT_STMT_LABEL (stmt
));
4647 if (EXIT_STMT_COND (stmt
))
4648 *stmt_p
= build3 (COND_EXPR
, void_type_node
,
4649 EXIT_STMT_COND (stmt
), *stmt_p
, alloc_stmt_list ());
4657 /* Force references to each of the entities in packages GNAT_NODE with's
4658 so that the debugging information for all of them are identical
4659 in all clients. Operate recursively on anything it with's, but check
4660 that we aren't elaborating something more than once. */
4662 /* The reason for this routine's existence is two-fold.
4663 First, with some debugging formats, notably MDEBUG on SGI
4664 IRIX, the linker will remove duplicate debugging information if two
4665 clients have identical debugguing information. With the normal scheme
4666 of elaboration, this does not usually occur, since entities in with'ed
4667 packages are elaborated on demand, and if clients have different usage
4668 patterns, the normal case, then the order and selection of entities
4669 will differ. In most cases however, it seems that linkers do not know
4670 how to eliminate duplicate debugging information, even if it is
4671 identical, so the use of this routine would increase the total amount
4672 of debugging information in the final executable.
4674 Second, this routine is called in type_annotate mode, to compute DDA
4675 information for types in withed units, for ASIS use */
4678 elaborate_all_entities (Node_Id gnat_node
)
4680 Entity_Id gnat_with_clause
, gnat_entity
;
4682 /* Process each unit only once. As we trace the context of all relevant
4683 units transitively, including generic bodies, we may encounter the
4684 same generic unit repeatedly */
4686 if (!present_gnu_tree (gnat_node
))
4687 save_gnu_tree (gnat_node
, integer_zero_node
, true);
4689 /* Save entities in all context units. A body may have an implicit_with
4690 on its own spec, if the context includes a child unit, so don't save
4693 for (gnat_with_clause
= First (Context_Items (gnat_node
));
4694 Present (gnat_with_clause
);
4695 gnat_with_clause
= Next (gnat_with_clause
))
4696 if (Nkind (gnat_with_clause
) == N_With_Clause
4697 && !present_gnu_tree (Library_Unit (gnat_with_clause
))
4698 && Library_Unit (gnat_with_clause
) != Library_Unit (Cunit (Main_Unit
)))
4700 elaborate_all_entities (Library_Unit (gnat_with_clause
));
4702 if (Ekind (Entity (Name (gnat_with_clause
))) == E_Package
)
4704 for (gnat_entity
= First_Entity (Entity (Name (gnat_with_clause
)));
4705 Present (gnat_entity
);
4706 gnat_entity
= Next_Entity (gnat_entity
))
4707 if (Is_Public (gnat_entity
)
4708 && Convention (gnat_entity
) != Convention_Intrinsic
4709 && Ekind (gnat_entity
) != E_Package
4710 && Ekind (gnat_entity
) != E_Package_Body
4711 && Ekind (gnat_entity
) != E_Operator
4712 && !(IN (Ekind (gnat_entity
), Type_Kind
)
4713 && !Is_Frozen (gnat_entity
))
4714 && !((Ekind (gnat_entity
) == E_Procedure
4715 || Ekind (gnat_entity
) == E_Function
)
4716 && Is_Intrinsic_Subprogram (gnat_entity
))
4717 && !IN (Ekind (gnat_entity
), Named_Kind
)
4718 && !IN (Ekind (gnat_entity
), Generic_Unit_Kind
))
4719 gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, 0);
4721 else if (Ekind (Entity (Name (gnat_with_clause
))) == E_Generic_Package
)
4724 = Corresponding_Body (Unit (Library_Unit (gnat_with_clause
)));
4726 /* Retrieve compilation unit node of generic body. */
4727 while (Present (gnat_body
)
4728 && Nkind (gnat_body
) != N_Compilation_Unit
)
4729 gnat_body
= Parent (gnat_body
);
4731 /* If body is available, elaborate its context. */
4732 if (Present (gnat_body
))
4733 elaborate_all_entities (gnat_body
);
4737 if (Nkind (Unit (gnat_node
)) == N_Package_Body
&& type_annotate_only
)
4738 elaborate_all_entities (Library_Unit (gnat_node
));
4741 /* Do the processing of N_Freeze_Entity, GNAT_NODE. */
4744 process_freeze_entity (Node_Id gnat_node
)
4746 Entity_Id gnat_entity
= Entity (gnat_node
);
4750 = (Nkind (Declaration_Node (gnat_entity
)) == N_Object_Declaration
4751 && present_gnu_tree (Declaration_Node (gnat_entity
)))
4752 ? get_gnu_tree (Declaration_Node (gnat_entity
)) : NULL_TREE
;
4754 /* If this is a package, need to generate code for the package. */
4755 if (Ekind (gnat_entity
) == E_Package
)
4758 (Parent (Corresponding_Body
4759 (Parent (Declaration_Node (gnat_entity
)))));
4763 /* Check for old definition after the above call. This Freeze_Node
4764 might be for one its Itypes. */
4766 = present_gnu_tree (gnat_entity
) ? get_gnu_tree (gnat_entity
) : 0;
4768 /* If this entity has an Address representation clause, GNU_OLD is the
4769 address, so discard it here. */
4770 if (Present (Address_Clause (gnat_entity
)))
4773 /* Don't do anything for class-wide types they are always
4774 transformed into their root type. */
4775 if (Ekind (gnat_entity
) == E_Class_Wide_Type
4776 || (Ekind (gnat_entity
) == E_Class_Wide_Subtype
4777 && Present (Equivalent_Type (gnat_entity
))))
4780 /* Don't do anything for subprograms that may have been elaborated before
4781 their freeze nodes. This can happen, for example because of an inner call
4782 in an instance body, or a previous compilation of a spec for inlining
4785 && TREE_CODE (gnu_old
) == FUNCTION_DECL
4786 && (Ekind (gnat_entity
) == E_Function
4787 || Ekind (gnat_entity
) == E_Procedure
))
4789 && (TREE_CODE (TREE_TYPE (gnu_old
)) == FUNCTION_TYPE
4790 && Ekind (gnat_entity
) == E_Subprogram_Type
)))
4793 /* If we have a non-dummy type old tree, we have nothing to do. Unless
4794 this is the public view of a private type whose full view was not
4795 delayed, this node was never delayed as it should have been.
4796 Also allow this to happen for concurrent types since we may have
4797 frozen both the Corresponding_Record_Type and this type. */
4799 && !(TREE_CODE (gnu_old
) == TYPE_DECL
4800 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old
))))
4802 gcc_assert ((IN (Ekind (gnat_entity
), Incomplete_Or_Private_Kind
)
4803 && Present (Full_View (gnat_entity
))
4804 && No (Freeze_Node (Full_View (gnat_entity
))))
4805 || Is_Concurrent_Type (gnat_entity
));
4809 /* Reset the saved tree, if any, and elaborate the object or type for real.
4810 If there is a full declaration, elaborate it and copy the type to
4811 GNAT_ENTITY. Likewise if this is the record subtype corresponding to
4812 a class wide type or subtype. */
4815 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
4816 if (IN (Ekind (gnat_entity
), Incomplete_Or_Private_Kind
)
4817 && Present (Full_View (gnat_entity
))
4818 && present_gnu_tree (Full_View (gnat_entity
)))
4819 save_gnu_tree (Full_View (gnat_entity
), NULL_TREE
, false);
4820 if (Present (Class_Wide_Type (gnat_entity
))
4821 && Class_Wide_Type (gnat_entity
) != gnat_entity
)
4822 save_gnu_tree (Class_Wide_Type (gnat_entity
), NULL_TREE
, false);
4825 if (IN (Ekind (gnat_entity
), Incomplete_Or_Private_Kind
)
4826 && Present (Full_View (gnat_entity
)))
4828 gnu_new
= gnat_to_gnu_entity (Full_View (gnat_entity
), NULL_TREE
, 1);
4830 /* Propagate back-annotations from full view to partial view. */
4831 if (Unknown_Alignment (gnat_entity
))
4832 Set_Alignment (gnat_entity
, Alignment (Full_View (gnat_entity
)));
4834 if (Unknown_Esize (gnat_entity
))
4835 Set_Esize (gnat_entity
, Esize (Full_View (gnat_entity
)));
4837 if (Unknown_RM_Size (gnat_entity
))
4838 Set_RM_Size (gnat_entity
, RM_Size (Full_View (gnat_entity
)));
4840 /* The above call may have defined this entity (the simplest example
4841 of this is when we have a private enumeral type since the bounds
4842 will have the public view. */
4843 if (!present_gnu_tree (gnat_entity
))
4844 save_gnu_tree (gnat_entity
, gnu_new
, false);
4845 if (Present (Class_Wide_Type (gnat_entity
))
4846 && Class_Wide_Type (gnat_entity
) != gnat_entity
)
4847 save_gnu_tree (Class_Wide_Type (gnat_entity
), gnu_new
, false);
4850 gnu_new
= gnat_to_gnu_entity (gnat_entity
, gnu_init
, 1);
4852 /* If we've made any pointers to the old version of this type, we
4853 have to update them. */
4855 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old
)),
4856 TREE_TYPE (gnu_new
));
4859 /* Process the list of inlined subprograms of GNAT_NODE, which is an
4860 N_Compilation_Unit. */
4863 process_inlined_subprograms (Node_Id gnat_node
)
4865 Entity_Id gnat_entity
;
4868 /* If we can inline, generate RTL for all the inlined subprograms.
4869 Define the entity first so we set DECL_EXTERNAL. */
4870 if (optimize
> 0 && !flag_really_no_inline
)
4871 for (gnat_entity
= First_Inlined_Subprogram (gnat_node
);
4872 Present (gnat_entity
);
4873 gnat_entity
= Next_Inlined_Subprogram (gnat_entity
))
4875 gnat_body
= Parent (Declaration_Node (gnat_entity
));
4877 if (Nkind (gnat_body
) != N_Subprogram_Body
)
4879 /* ??? This really should always be Present. */
4880 if (No (Corresponding_Body (gnat_body
)))
4884 = Parent (Declaration_Node (Corresponding_Body (gnat_body
)));
4887 if (Present (gnat_body
))
4889 gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, 0);
4890 add_stmt (gnat_to_gnu (gnat_body
));
4895 /* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present.
4896 We make two passes, one to elaborate anything other than bodies (but
4897 we declare a function if there was no spec). The second pass
4898 elaborates the bodies.
4900 GNAT_END_LIST gives the element in the list past the end. Normally,
4901 this is Empty, but can be First_Real_Statement for a
4902 Handled_Sequence_Of_Statements.
4904 We make a complete pass through both lists if PASS1P is true, then make
4905 the second pass over both lists if PASS2P is true. The lists usually
4906 correspond to the public and private parts of a package. */
4909 process_decls (List_Id gnat_decls
, List_Id gnat_decls2
,
4910 Node_Id gnat_end_list
, bool pass1p
, bool pass2p
)
4912 List_Id gnat_decl_array
[2];
4916 gnat_decl_array
[0] = gnat_decls
, gnat_decl_array
[1] = gnat_decls2
;
4919 for (i
= 0; i
<= 1; i
++)
4920 if (Present (gnat_decl_array
[i
]))
4921 for (gnat_decl
= First (gnat_decl_array
[i
]);
4922 gnat_decl
!= gnat_end_list
; gnat_decl
= Next (gnat_decl
))
4924 /* For package specs, we recurse inside the declarations,
4925 thus taking the two pass approach inside the boundary. */
4926 if (Nkind (gnat_decl
) == N_Package_Declaration
4927 && (Nkind (Specification (gnat_decl
)
4928 == N_Package_Specification
)))
4929 process_decls (Visible_Declarations (Specification (gnat_decl
)),
4930 Private_Declarations (Specification (gnat_decl
)),
4931 Empty
, true, false);
4933 /* Similarly for any declarations in the actions of a
4935 else if (Nkind (gnat_decl
) == N_Freeze_Entity
)
4937 process_freeze_entity (gnat_decl
);
4938 process_decls (Actions (gnat_decl
), Empty
, Empty
, true, false);
4941 /* Package bodies with freeze nodes get their elaboration deferred
4942 until the freeze node, but the code must be placed in the right
4943 place, so record the code position now. */
4944 else if (Nkind (gnat_decl
) == N_Package_Body
4945 && Present (Freeze_Node (Corresponding_Spec (gnat_decl
))))
4946 record_code_position (gnat_decl
);
4948 else if (Nkind (gnat_decl
) == N_Package_Body_Stub
4949 && Present (Library_Unit (gnat_decl
))
4950 && Present (Freeze_Node
4953 (Library_Unit (gnat_decl
)))))))
4954 record_code_position
4955 (Proper_Body (Unit (Library_Unit (gnat_decl
))));
4957 /* We defer most subprogram bodies to the second pass. */
4958 else if (Nkind (gnat_decl
) == N_Subprogram_Body
)
4960 if (Acts_As_Spec (gnat_decl
))
4962 Node_Id gnat_subprog_id
= Defining_Entity (gnat_decl
);
4964 if (Ekind (gnat_subprog_id
) != E_Generic_Procedure
4965 && Ekind (gnat_subprog_id
) != E_Generic_Function
)
4966 gnat_to_gnu_entity (gnat_subprog_id
, NULL_TREE
, 1);
4969 /* For bodies and stubs that act as their own specs, the entity
4970 itself must be elaborated in the first pass, because it may
4971 be used in other declarations. */
4972 else if (Nkind (gnat_decl
) == N_Subprogram_Body_Stub
)
4974 Node_Id gnat_subprog_id
=
4975 Defining_Entity (Specification (gnat_decl
));
4977 if (Ekind (gnat_subprog_id
) != E_Subprogram_Body
4978 && Ekind (gnat_subprog_id
) != E_Generic_Procedure
4979 && Ekind (gnat_subprog_id
) != E_Generic_Function
)
4980 gnat_to_gnu_entity (gnat_subprog_id
, NULL_TREE
, 1);
4983 /* Concurrent stubs stand for the corresponding subprogram bodies,
4984 which are deferred like other bodies. */
4985 else if (Nkind (gnat_decl
) == N_Task_Body_Stub
4986 || Nkind (gnat_decl
) == N_Protected_Body_Stub
)
4989 add_stmt (gnat_to_gnu (gnat_decl
));
4992 /* Here we elaborate everything we deferred above except for package bodies,
4993 which are elaborated at their freeze nodes. Note that we must also
4994 go inside things (package specs and freeze nodes) the first pass did. */
4996 for (i
= 0; i
<= 1; i
++)
4997 if (Present (gnat_decl_array
[i
]))
4998 for (gnat_decl
= First (gnat_decl_array
[i
]);
4999 gnat_decl
!= gnat_end_list
; gnat_decl
= Next (gnat_decl
))
5001 if (Nkind (gnat_decl
) == N_Subprogram_Body
5002 || Nkind (gnat_decl
) == N_Subprogram_Body_Stub
5003 || Nkind (gnat_decl
) == N_Task_Body_Stub
5004 || Nkind (gnat_decl
) == N_Protected_Body_Stub
)
5005 add_stmt (gnat_to_gnu (gnat_decl
));
5007 else if (Nkind (gnat_decl
) == N_Package_Declaration
5008 && (Nkind (Specification (gnat_decl
)
5009 == N_Package_Specification
)))
5010 process_decls (Visible_Declarations (Specification (gnat_decl
)),
5011 Private_Declarations (Specification (gnat_decl
)),
5012 Empty
, false, true);
5014 else if (Nkind (gnat_decl
) == N_Freeze_Entity
)
5015 process_decls (Actions (gnat_decl
), Empty
, Empty
, false, true);
5019 /* Emit code for a range check. GNU_EXPR is the expression to be checked,
5020 GNAT_RANGE_TYPE the gnat type or subtype containing the bounds against
5021 which we have to check. */
5024 emit_range_check (tree gnu_expr
, Entity_Id gnat_range_type
)
5026 tree gnu_range_type
= get_unpadded_type (gnat_range_type
);
5027 tree gnu_low
= TYPE_MIN_VALUE (gnu_range_type
);
5028 tree gnu_high
= TYPE_MAX_VALUE (gnu_range_type
);
5029 tree gnu_compare_type
= get_base_type (TREE_TYPE (gnu_expr
));
5031 /* If GNU_EXPR has an integral type that is narrower than GNU_RANGE_TYPE,
5032 we can't do anything since we might be truncating the bounds. No
5033 check is needed in this case. */
5034 if (INTEGRAL_TYPE_P (TREE_TYPE (gnu_expr
))
5035 && (TYPE_PRECISION (gnu_compare_type
)
5036 < TYPE_PRECISION (get_base_type (gnu_range_type
))))
5039 /* Checked expressions must be evaluated only once. */
5040 gnu_expr
= protect_multiple_eval (gnu_expr
);
5042 /* There's no good type to use here, so we might as well use
5043 integer_type_node. Note that the form of the check is
5044 (not (expr >= lo)) or (not (expr >= hi))
5045 the reason for this slightly convoluted form is that NaN's
5046 are not considered to be in range in the float case. */
5048 (build_binary_op (TRUTH_ORIF_EXPR
, integer_type_node
,
5050 (build_binary_op (GE_EXPR
, integer_type_node
,
5051 convert (gnu_compare_type
, gnu_expr
),
5052 convert (gnu_compare_type
, gnu_low
))),
5054 (build_binary_op (LE_EXPR
, integer_type_node
,
5055 convert (gnu_compare_type
, gnu_expr
),
5056 convert (gnu_compare_type
,
5058 gnu_expr
, CE_Range_Check_Failed
);
5061 /* Emit code for an index check. GNU_ARRAY_OBJECT is the array object
5062 which we are about to index, GNU_EXPR is the index expression to be
5063 checked, GNU_LOW and GNU_HIGH are the lower and upper bounds
5064 against which GNU_EXPR has to be checked. Note that for index
5065 checking we cannot use the emit_range_check function (although very
5066 similar code needs to be generated in both cases) since for index
5067 checking the array type against which we are checking the indeces
5068 may be unconstrained and consequently we need to retrieve the
5069 actual index bounds from the array object itself
5070 (GNU_ARRAY_OBJECT). The place where we need to do that is in
5071 subprograms having unconstrained array formal parameters */
5074 emit_index_check (tree gnu_array_object
,
5079 tree gnu_expr_check
;
5081 /* Checked expressions must be evaluated only once. */
5082 gnu_expr
= protect_multiple_eval (gnu_expr
);
5084 /* Must do this computation in the base type in case the expression's
5085 type is an unsigned subtypes. */
5086 gnu_expr_check
= convert (get_base_type (TREE_TYPE (gnu_expr
)), gnu_expr
);
5088 /* If GNU_LOW or GNU_HIGH are a PLACEHOLDER_EXPR, qualify them by
5089 the object we are handling. */
5090 gnu_low
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_low
, gnu_array_object
);
5091 gnu_high
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_high
, gnu_array_object
);
5093 /* There's no good type to use here, so we might as well use
5094 integer_type_node. */
5096 (build_binary_op (TRUTH_ORIF_EXPR
, integer_type_node
,
5097 build_binary_op (LT_EXPR
, integer_type_node
,
5099 convert (TREE_TYPE (gnu_expr_check
),
5101 build_binary_op (GT_EXPR
, integer_type_node
,
5103 convert (TREE_TYPE (gnu_expr_check
),
5105 gnu_expr
, CE_Index_Check_Failed
);
5108 /* GNU_COND contains the condition corresponding to an access, discriminant or
5109 range check of value GNU_EXPR. Build a COND_EXPR that returns GNU_EXPR if
5110 GNU_COND is false and raises a CONSTRAINT_ERROR if GNU_COND is true.
5111 REASON is the code that says why the exception was raised. */
5114 emit_check (tree gnu_cond
, tree gnu_expr
, int reason
)
5119 gnu_call
= build_call_raise (reason
);
5121 /* Use an outer COMPOUND_EXPR to make sure that GNU_EXPR will get evaluated
5122 in front of the comparison in case it ends up being a SAVE_EXPR. Put the
5123 whole thing inside its own SAVE_EXPR so the inner SAVE_EXPR doesn't leak
5125 gnu_result
= fold (build3 (COND_EXPR
, TREE_TYPE (gnu_expr
), gnu_cond
,
5126 build2 (COMPOUND_EXPR
, TREE_TYPE (gnu_expr
),
5127 gnu_call
, gnu_expr
),
5130 /* If GNU_EXPR has side effects, make the outer COMPOUND_EXPR and
5131 protect it. Otherwise, show GNU_RESULT has no side effects: we
5132 don't need to evaluate it just for the check. */
5133 if (TREE_SIDE_EFFECTS (gnu_expr
))
5135 = build2 (COMPOUND_EXPR
, TREE_TYPE (gnu_expr
), gnu_expr
, gnu_result
);
5137 TREE_SIDE_EFFECTS (gnu_result
) = 0;
5139 /* ??? Unfortunately, if we don't put a SAVE_EXPR around this whole thing,
5140 we will repeatedly do the test. It would be nice if GCC was able
5141 to optimize this and only do it once. */
5142 return save_expr (gnu_result
);
5145 /* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing
5146 overflow checks if OVERFLOW_P is nonzero and range checks if
5147 RANGE_P is nonzero. GNAT_TYPE is known to be an integral type.
5148 If TRUNCATE_P is nonzero, do a float to integer conversion with
5149 truncation; otherwise round. */
5152 convert_with_check (Entity_Id gnat_type
, tree gnu_expr
, bool overflowp
,
5153 bool rangep
, bool truncatep
)
5155 tree gnu_type
= get_unpadded_type (gnat_type
);
5156 tree gnu_in_type
= TREE_TYPE (gnu_expr
);
5157 tree gnu_in_basetype
= get_base_type (gnu_in_type
);
5158 tree gnu_base_type
= get_base_type (gnu_type
);
5159 tree gnu_ada_base_type
= get_ada_base_type (gnu_type
);
5160 tree gnu_result
= gnu_expr
;
5162 /* If we are not doing any checks, the output is an integral type, and
5163 the input is not a floating type, just do the conversion. This
5164 shortcut is required to avoid problems with packed array types
5165 and simplifies code in all cases anyway. */
5166 if (!rangep
&& !overflowp
&& INTEGRAL_TYPE_P (gnu_base_type
)
5167 && !FLOAT_TYPE_P (gnu_in_type
))
5168 return convert (gnu_type
, gnu_expr
);
5170 /* First convert the expression to its base type. This
5171 will never generate code, but makes the tests below much simpler.
5172 But don't do this if converting from an integer type to an unconstrained
5173 array type since then we need to get the bounds from the original
5175 if (TREE_CODE (gnu_type
) != UNCONSTRAINED_ARRAY_TYPE
)
5176 gnu_result
= convert (gnu_in_basetype
, gnu_result
);
5178 /* If overflow checks are requested, we need to be sure the result will
5179 fit in the output base type. But don't do this if the input
5180 is integer and the output floating-point. */
5182 && !(FLOAT_TYPE_P (gnu_base_type
) && INTEGRAL_TYPE_P (gnu_in_basetype
)))
5184 /* Ensure GNU_EXPR only gets evaluated once. */
5185 tree gnu_input
= protect_multiple_eval (gnu_result
);
5186 tree gnu_cond
= integer_zero_node
;
5187 tree gnu_in_lb
= TYPE_MIN_VALUE (gnu_in_basetype
);
5188 tree gnu_in_ub
= TYPE_MAX_VALUE (gnu_in_basetype
);
5189 tree gnu_out_lb
= TYPE_MIN_VALUE (gnu_base_type
);
5190 tree gnu_out_ub
= TYPE_MAX_VALUE (gnu_base_type
);
5192 /* Convert the lower bounds to signed types, so we're sure we're
5193 comparing them properly. Likewise, convert the upper bounds
5194 to unsigned types. */
5195 if (INTEGRAL_TYPE_P (gnu_in_basetype
) && TYPE_UNSIGNED (gnu_in_basetype
))
5196 gnu_in_lb
= convert (gnat_signed_type (gnu_in_basetype
), gnu_in_lb
);
5198 if (INTEGRAL_TYPE_P (gnu_in_basetype
)
5199 && !TYPE_UNSIGNED (gnu_in_basetype
))
5200 gnu_in_ub
= convert (gnat_unsigned_type (gnu_in_basetype
), gnu_in_ub
);
5202 if (INTEGRAL_TYPE_P (gnu_base_type
) && TYPE_UNSIGNED (gnu_base_type
))
5203 gnu_out_lb
= convert (gnat_signed_type (gnu_base_type
), gnu_out_lb
);
5205 if (INTEGRAL_TYPE_P (gnu_base_type
) && !TYPE_UNSIGNED (gnu_base_type
))
5206 gnu_out_ub
= convert (gnat_unsigned_type (gnu_base_type
), gnu_out_ub
);
5208 /* Check each bound separately and only if the result bound
5209 is tighter than the bound on the input type. Note that all the
5210 types are base types, so the bounds must be constant. Also,
5211 the comparison is done in the base type of the input, which
5212 always has the proper signedness. First check for input
5213 integer (which means output integer), output float (which means
5214 both float), or mixed, in which case we always compare.
5215 Note that we have to do the comparison which would *fail* in the
5216 case of an error since if it's an FP comparison and one of the
5217 values is a NaN or Inf, the comparison will fail. */
5218 if (INTEGRAL_TYPE_P (gnu_in_basetype
)
5219 ? tree_int_cst_lt (gnu_in_lb
, gnu_out_lb
)
5220 : (FLOAT_TYPE_P (gnu_base_type
)
5221 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_in_lb
),
5222 TREE_REAL_CST (gnu_out_lb
))
5226 (build_binary_op (GE_EXPR
, integer_type_node
,
5227 gnu_input
, convert (gnu_in_basetype
,
5230 if (INTEGRAL_TYPE_P (gnu_in_basetype
)
5231 ? tree_int_cst_lt (gnu_out_ub
, gnu_in_ub
)
5232 : (FLOAT_TYPE_P (gnu_base_type
)
5233 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_out_ub
),
5234 TREE_REAL_CST (gnu_in_lb
))
5237 = build_binary_op (TRUTH_ORIF_EXPR
, integer_type_node
, gnu_cond
,
5239 (build_binary_op (LE_EXPR
, integer_type_node
,
5241 convert (gnu_in_basetype
,
5244 if (!integer_zerop (gnu_cond
))
5245 gnu_result
= emit_check (gnu_cond
, gnu_input
,
5246 CE_Overflow_Check_Failed
);
5249 /* Now convert to the result base type. If this is a non-truncating
5250 float-to-integer conversion, round. */
5251 if (INTEGRAL_TYPE_P (gnu_ada_base_type
) && FLOAT_TYPE_P (gnu_in_basetype
)
5254 REAL_VALUE_TYPE half_minus_pred_half
, pred_half
;
5255 tree gnu_conv
, gnu_zero
, gnu_comp
, gnu_saved_result
, calc_type
;
5256 tree gnu_pred_half
, gnu_add_pred_half
, gnu_subtract_pred_half
;
5257 const struct real_format
*fmt
;
5259 /* The following calculations depend on proper rounding to even
5260 of each arithmetic operation. In order to prevent excess
5261 precision from spoiling this property, use the widest hardware
5262 floating-point type.
5264 FIXME: For maximum efficiency, this should only be done for machines
5265 and types where intermediates may have extra precision. */
5267 calc_type
= longest_float_type_node
;
5268 /* FIXME: Should not have padding in the first place */
5269 if (TREE_CODE (calc_type
) == RECORD_TYPE
5270 && TYPE_IS_PADDING_P (calc_type
))
5271 calc_type
= TREE_TYPE (TYPE_FIELDS (calc_type
));
5273 /* Compute the exact value calc_type'Pred (0.5) at compile time. */
5274 fmt
= REAL_MODE_FORMAT (TYPE_MODE (calc_type
));
5275 real_2expN (&half_minus_pred_half
, -(fmt
->p
) - 1);
5276 REAL_ARITHMETIC (pred_half
, MINUS_EXPR
, dconsthalf
,
5277 half_minus_pred_half
);
5278 gnu_pred_half
= build_real (calc_type
, pred_half
);
5280 /* If the input is strictly negative, subtract this value
5281 and otherwise add it from the input. For 0.5, the result
5282 is exactly between 1.0 and the machine number preceding 1.0
5283 (for calc_type). Since the last bit of 1.0 is even, this 0.5
5284 will round to 1.0, while all other number with an absolute
5285 value less than 0.5 round to 0.0. For larger numbers exactly
5286 halfway between integers, rounding will always be correct as
5287 the true mathematical result will be closer to the higher
5288 integer compared to the lower one. So, this constant works
5289 for all floating-point numbers.
5291 The reason to use the same constant with subtract/add instead
5292 of a positive and negative constant is to allow the comparison
5293 to be scheduled in parallel with retrieval of the constant and
5294 conversion of the input to the calc_type (if necessary).
5297 gnu_zero
= convert (gnu_in_basetype
, integer_zero_node
);
5298 gnu_saved_result
= save_expr (gnu_result
);
5299 gnu_conv
= convert (calc_type
, gnu_saved_result
);
5300 gnu_comp
= build2 (GE_EXPR
, integer_type_node
,
5301 gnu_saved_result
, gnu_zero
);
5303 = build2 (PLUS_EXPR
, calc_type
, gnu_conv
, gnu_pred_half
);
5304 gnu_subtract_pred_half
5305 = build2 (MINUS_EXPR
, calc_type
, gnu_conv
, gnu_pred_half
);
5306 gnu_result
= build3 (COND_EXPR
, calc_type
, gnu_comp
,
5307 gnu_add_pred_half
, gnu_subtract_pred_half
);
5310 if (TREE_CODE (gnu_ada_base_type
) == INTEGER_TYPE
5311 && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_ada_base_type
)
5312 && TREE_CODE (gnu_result
) == UNCONSTRAINED_ARRAY_REF
)
5313 gnu_result
= unchecked_convert (gnu_ada_base_type
, gnu_result
, false);
5315 gnu_result
= convert (gnu_ada_base_type
, gnu_result
);
5317 /* Finally, do the range check if requested. Note that if the
5318 result type is a modular type, the range check is actually
5319 an overflow check. */
5322 || (TREE_CODE (gnu_base_type
) == INTEGER_TYPE
5323 && TYPE_MODULAR_P (gnu_base_type
) && overflowp
))
5324 gnu_result
= emit_range_check (gnu_result
, gnat_type
);
5326 return convert (gnu_type
, gnu_result
);
5329 /* Return 1 if GNU_EXPR can be directly addressed. This is the case unless
5330 it is an expression involving computation or if it involves a bitfield
5331 reference. This returns the same as gnat_mark_addressable in most
5335 addressable_p (tree gnu_expr
)
5337 switch (TREE_CODE (gnu_expr
))
5343 /* All DECLs are addressable: if they are in a register, we can force
5347 case UNCONSTRAINED_ARRAY_REF
:
5355 return (!DECL_BIT_FIELD (TREE_OPERAND (gnu_expr
, 1))
5356 && (!DECL_NONADDRESSABLE_P (TREE_OPERAND (gnu_expr
, 1))
5357 || !flag_strict_aliasing
)
5358 && addressable_p (TREE_OPERAND (gnu_expr
, 0)));
5360 case ARRAY_REF
: case ARRAY_RANGE_REF
:
5361 case REALPART_EXPR
: case IMAGPART_EXPR
:
5363 return addressable_p (TREE_OPERAND (gnu_expr
, 0));
5366 return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr
))
5367 && addressable_p (TREE_OPERAND (gnu_expr
, 0)));
5369 case VIEW_CONVERT_EXPR
:
5371 /* This is addressable if we can avoid a copy. */
5372 tree type
= TREE_TYPE (gnu_expr
);
5373 tree inner_type
= TREE_TYPE (TREE_OPERAND (gnu_expr
, 0));
5375 return (((TYPE_MODE (type
) == TYPE_MODE (inner_type
)
5376 && (TYPE_ALIGN (type
) <= TYPE_ALIGN (inner_type
)
5377 || TYPE_ALIGN (inner_type
) >= BIGGEST_ALIGNMENT
))
5378 || ((TYPE_MODE (type
) == BLKmode
5379 || TYPE_MODE (inner_type
) == BLKmode
)
5380 && (TYPE_ALIGN (type
) <= TYPE_ALIGN (inner_type
)
5381 || TYPE_ALIGN (inner_type
) >= BIGGEST_ALIGNMENT
5382 || TYPE_ALIGN_OK (type
)
5383 || TYPE_ALIGN_OK (inner_type
))))
5384 && addressable_p (TREE_OPERAND (gnu_expr
, 0)));
5392 /* Do the processing for the declaration of a GNAT_ENTITY, a type. If
5393 a separate Freeze node exists, delay the bulk of the processing. Otherwise
5394 make a GCC type for GNAT_ENTITY and set up the correspondence. */
5397 process_type (Entity_Id gnat_entity
)
5400 = present_gnu_tree (gnat_entity
) ? get_gnu_tree (gnat_entity
) : 0;
5403 /* If we are to delay elaboration of this type, just do any
5404 elaborations needed for expressions within the declaration and
5405 make a dummy type entry for this node and its Full_View (if
5406 any) in case something points to it. Don't do this if it
5407 has already been done (the only way that can happen is if
5408 the private completion is also delayed). */
5409 if (Present (Freeze_Node (gnat_entity
))
5410 || (IN (Ekind (gnat_entity
), Incomplete_Or_Private_Kind
)
5411 && Present (Full_View (gnat_entity
))
5412 && Freeze_Node (Full_View (gnat_entity
))
5413 && !present_gnu_tree (Full_View (gnat_entity
))))
5415 elaborate_entity (gnat_entity
);
5419 tree gnu_decl
= create_type_decl (get_entity_name (gnat_entity
),
5420 make_dummy_type (gnat_entity
),
5421 NULL
, false, false, gnat_entity
);
5423 save_gnu_tree (gnat_entity
, gnu_decl
, false);
5424 if (IN (Ekind (gnat_entity
), Incomplete_Or_Private_Kind
)
5425 && Present (Full_View (gnat_entity
)))
5426 save_gnu_tree (Full_View (gnat_entity
), gnu_decl
, false);
5432 /* If we saved away a dummy type for this node it means that this
5433 made the type that corresponds to the full type of an incomplete
5434 type. Clear that type for now and then update the type in the
5438 if (TREE_CODE (gnu_old
) != TYPE_DECL
5439 || !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old
)))
5441 /* If this was a withed access type, this is not an error
5442 and merely indicates we've already elaborated the type
5444 gcc_assert (Is_Type (gnat_entity
) && From_With_Type (gnat_entity
));
5448 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
5451 /* Now fully elaborate the type. */
5452 gnu_new
= gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, 1);
5453 gcc_assert (TREE_CODE (gnu_new
) == TYPE_DECL
);
5455 /* If we have an old type and we've made pointers to this type,
5456 update those pointers. */
5458 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old
)),
5459 TREE_TYPE (gnu_new
));
5461 /* If this is a record type corresponding to a task or protected type
5462 that is a completion of an incomplete type, perform a similar update
5464 /* ??? Including protected types here is a guess. */
5466 if (IN (Ekind (gnat_entity
), Record_Kind
)
5467 && Is_Concurrent_Record_Type (gnat_entity
)
5468 && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity
)))
5471 = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity
));
5473 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity
),
5475 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity
),
5478 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old
)),
5479 TREE_TYPE (gnu_new
));
5483 /* GNAT_ASSOC is the front of the Component_Associations of an N_Aggregate.
5484 GNU_TYPE is the GCC type of the corresponding record.
5486 Return a CONSTRUCTOR to build the record. */
5489 assoc_to_constructor (Node_Id gnat_assoc
, tree gnu_type
)
5491 tree gnu_list
, gnu_result
;
5493 /* We test for GNU_FIELD being empty in the case where a variant
5494 was the last thing since we don't take things off GNAT_ASSOC in
5495 that case. We check GNAT_ASSOC in case we have a variant, but it
5498 for (gnu_list
= NULL_TREE
; Present (gnat_assoc
);
5499 gnat_assoc
= Next (gnat_assoc
))
5501 Node_Id gnat_field
= First (Choices (gnat_assoc
));
5502 tree gnu_field
= gnat_to_gnu_field_decl (Entity (gnat_field
));
5503 tree gnu_expr
= gnat_to_gnu (Expression (gnat_assoc
));
5505 /* The expander is supposed to put a single component selector name
5506 in every record component association */
5507 gcc_assert (No (Next (gnat_field
)));
5509 /* Ignore fields that have Corresponding_Discriminants since we'll
5510 be setting that field in the parent. */
5511 if (Present (Corresponding_Discriminant (Entity (gnat_field
)))
5512 && Is_Tagged_Type (Scope (Entity (gnat_field
))))
5515 /* Before assigning a value in an aggregate make sure range checks
5516 are done if required. Then convert to the type of the field. */
5517 if (Do_Range_Check (Expression (gnat_assoc
)))
5518 gnu_expr
= emit_range_check (gnu_expr
, Etype (gnat_field
));
5520 gnu_expr
= convert (TREE_TYPE (gnu_field
), gnu_expr
);
5522 /* Add the field and expression to the list. */
5523 gnu_list
= tree_cons (gnu_field
, gnu_expr
, gnu_list
);
5526 gnu_result
= extract_values (gnu_list
, gnu_type
);
5528 #ifdef ENABLE_CHECKING
5532 /* Verify every enty in GNU_LIST was used. */
5533 for (gnu_field
= gnu_list
; gnu_field
; gnu_field
= TREE_CHAIN (gnu_field
))
5534 gcc_assert (TREE_ADDRESSABLE (gnu_field
));
5541 /* Builds a possibly nested constructor for array aggregates. GNAT_EXPR
5542 is the first element of an array aggregate. It may itself be an
5543 aggregate (an array or record aggregate). GNU_ARRAY_TYPE is the gnu type
5544 corresponding to the array aggregate. GNAT_COMPONENT_TYPE is the type
5545 of the array component. It is needed for range checking. */
5548 pos_to_constructor (Node_Id gnat_expr
, tree gnu_array_type
,
5549 Entity_Id gnat_component_type
)
5551 tree gnu_expr_list
= NULL_TREE
;
5552 tree gnu_index
= TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type
));
5555 for ( ; Present (gnat_expr
); gnat_expr
= Next (gnat_expr
))
5557 /* If the expression is itself an array aggregate then first build the
5558 innermost constructor if it is part of our array (multi-dimensional
5561 if (Nkind (gnat_expr
) == N_Aggregate
5562 && TREE_CODE (TREE_TYPE (gnu_array_type
)) == ARRAY_TYPE
5563 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type
)))
5564 gnu_expr
= pos_to_constructor (First (Expressions (gnat_expr
)),
5565 TREE_TYPE (gnu_array_type
),
5566 gnat_component_type
);
5569 gnu_expr
= gnat_to_gnu (gnat_expr
);
5571 /* before assigning the element to the array make sure it is
5573 if (Do_Range_Check (gnat_expr
))
5574 gnu_expr
= emit_range_check (gnu_expr
, gnat_component_type
);
5578 = tree_cons (gnu_index
, convert (TREE_TYPE (gnu_array_type
), gnu_expr
),
5581 gnu_index
= int_const_binop (PLUS_EXPR
, gnu_index
, integer_one_node
, 0);
5584 return gnat_build_constructor (gnu_array_type
, nreverse (gnu_expr_list
));
5587 /* Subroutine of assoc_to_constructor: VALUES is a list of field associations,
5588 some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting
5589 of the associations that are from RECORD_TYPE. If we see an internal
5590 record, make a recursive call to fill it in as well. */
5593 extract_values (tree values
, tree record_type
)
5595 tree result
= NULL_TREE
;
5598 for (field
= TYPE_FIELDS (record_type
); field
; field
= TREE_CHAIN (field
))
5602 /* _Parent is an internal field, but may have values in the aggregate,
5603 so check for values first. */
5604 if ((tem
= purpose_member (field
, values
)))
5606 value
= TREE_VALUE (tem
);
5607 TREE_ADDRESSABLE (tem
) = 1;
5610 else if (DECL_INTERNAL_P (field
))
5612 value
= extract_values (values
, TREE_TYPE (field
));
5613 if (TREE_CODE (value
) == CONSTRUCTOR
&& !CONSTRUCTOR_ELTS (value
))
5617 /* If we have a record subtype, the names will match, but not the
5618 actual FIELD_DECLs. */
5619 for (tem
= values
; tem
; tem
= TREE_CHAIN (tem
))
5620 if (DECL_NAME (TREE_PURPOSE (tem
)) == DECL_NAME (field
))
5622 value
= convert (TREE_TYPE (field
), TREE_VALUE (tem
));
5623 TREE_ADDRESSABLE (tem
) = 1;
5629 result
= tree_cons (field
, value
, result
);
5632 return gnat_build_constructor (record_type
, nreverse (result
));
5635 /* EXP is to be treated as an array or record. Handle the cases when it is
5636 an access object and perform the required dereferences. */
5639 maybe_implicit_deref (tree exp
)
5641 /* If the type is a pointer, dereference it. */
5643 if (POINTER_TYPE_P (TREE_TYPE (exp
)) || TYPE_FAT_POINTER_P (TREE_TYPE (exp
)))
5644 exp
= build_unary_op (INDIRECT_REF
, NULL_TREE
, exp
);
5646 /* If we got a padded type, remove it too. */
5647 if (TREE_CODE (TREE_TYPE (exp
)) == RECORD_TYPE
5648 && TYPE_IS_PADDING_P (TREE_TYPE (exp
)))
5649 exp
= convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp
))), exp
);
5654 /* Protect EXP from multiple evaluation. This may make a SAVE_EXPR. */
5657 protect_multiple_eval (tree exp
)
5659 tree type
= TREE_TYPE (exp
);
5661 /* If this has no side effects, we don't need to do anything. */
5662 if (!TREE_SIDE_EFFECTS (exp
))
5665 /* If it is a conversion, protect what's inside the conversion.
5666 Similarly, if we're indirectly referencing something, we only
5667 actually need to protect the address since the data itself can't
5668 change in these situations. */
5669 else if (TREE_CODE (exp
) == NON_LVALUE_EXPR
5670 || TREE_CODE (exp
) == NOP_EXPR
|| TREE_CODE (exp
) == CONVERT_EXPR
5671 || TREE_CODE (exp
) == VIEW_CONVERT_EXPR
5672 || TREE_CODE (exp
) == INDIRECT_REF
5673 || TREE_CODE (exp
) == UNCONSTRAINED_ARRAY_REF
)
5674 return build1 (TREE_CODE (exp
), type
,
5675 protect_multiple_eval (TREE_OPERAND (exp
, 0)));
5677 /* If EXP is a fat pointer or something that can be placed into a register,
5678 just make a SAVE_EXPR. */
5679 if (TYPE_FAT_POINTER_P (type
) || TYPE_MODE (type
) != BLKmode
)
5680 return save_expr (exp
);
5682 /* Otherwise, dereference, protect the address, and re-reference. */
5685 build_unary_op (INDIRECT_REF
, type
,
5686 save_expr (build_unary_op (ADDR_EXPR
,
5687 build_reference_type (type
),
5691 /* This is equivalent to stabilize_reference in GCC's tree.c, but we know
5692 how to handle our new nodes and we take an extra argument that says
5693 whether to force evaluation of everything. */
5696 gnat_stabilize_reference (tree ref
, bool force
)
5698 tree type
= TREE_TYPE (ref
);
5699 enum tree_code code
= TREE_CODE (ref
);
5707 /* No action is needed in this case. */
5713 case FIX_TRUNC_EXPR
:
5714 case FIX_FLOOR_EXPR
:
5715 case FIX_ROUND_EXPR
:
5717 case VIEW_CONVERT_EXPR
:
5720 = build1 (code
, type
,
5721 gnat_stabilize_reference (TREE_OPERAND (ref
, 0), force
));
5725 case UNCONSTRAINED_ARRAY_REF
:
5726 result
= build1 (code
, type
,
5727 gnat_stabilize_reference_1 (TREE_OPERAND (ref
, 0),
5732 result
= build3 (COMPONENT_REF
, type
,
5733 gnat_stabilize_reference (TREE_OPERAND (ref
, 0),
5735 TREE_OPERAND (ref
, 1), NULL_TREE
);
5739 result
= build3 (BIT_FIELD_REF
, type
,
5740 gnat_stabilize_reference (TREE_OPERAND (ref
, 0), force
),
5741 gnat_stabilize_reference_1 (TREE_OPERAND (ref
, 1),
5743 gnat_stabilize_reference_1 (TREE_OPERAND (ref
, 2),
5748 case ARRAY_RANGE_REF
:
5749 result
= build4 (code
, type
,
5750 gnat_stabilize_reference (TREE_OPERAND (ref
, 0), force
),
5751 gnat_stabilize_reference_1 (TREE_OPERAND (ref
, 1),
5753 NULL_TREE
, NULL_TREE
);
5757 result
= build2 (COMPOUND_EXPR
, type
,
5758 gnat_stabilize_reference_1 (TREE_OPERAND (ref
, 0),
5760 gnat_stabilize_reference (TREE_OPERAND (ref
, 1),
5764 /* If arg isn't a kind of lvalue we recognize, make no change.
5765 Caller should recognize the error for an invalid lvalue. */
5770 return error_mark_node
;
5773 TREE_READONLY (result
) = TREE_READONLY (ref
);
5775 /* TREE_THIS_VOLATILE and TREE_SIDE_EFFECTS attached to the initial
5776 expression may not be sustained across some paths, such as the way via
5777 build1 for INDIRECT_REF. We re-populate those flags here for the general
5778 case, which is consistent with the GCC version of this routine.
5780 Special care should be taken regarding TREE_SIDE_EFFECTS, because some
5781 paths introduce side effects where there was none initially (e.g. calls
5782 to save_expr), and we also want to keep track of that. */
5784 TREE_THIS_VOLATILE (result
) = TREE_THIS_VOLATILE (ref
);
5785 TREE_SIDE_EFFECTS (result
) |= TREE_SIDE_EFFECTS (ref
);
5790 /* Similar to stabilize_reference_1 in tree.c, but supports an extra
5791 arg to force a SAVE_EXPR for everything. */
5794 gnat_stabilize_reference_1 (tree e
, bool force
)
5796 enum tree_code code
= TREE_CODE (e
);
5797 tree type
= TREE_TYPE (e
);
5800 /* We cannot ignore const expressions because it might be a reference
5801 to a const array but whose index contains side-effects. But we can
5802 ignore things that are actual constant or that already have been
5803 handled by this function. */
5805 if (TREE_CONSTANT (e
) || code
== SAVE_EXPR
)
5808 switch (TREE_CODE_CLASS (code
))
5810 case tcc_exceptional
:
5812 case tcc_declaration
:
5813 case tcc_comparison
:
5815 case tcc_expression
:
5817 /* If this is a COMPONENT_REF of a fat pointer, save the entire
5818 fat pointer. This may be more efficient, but will also allow
5819 us to more easily find the match for the PLACEHOLDER_EXPR. */
5820 if (code
== COMPONENT_REF
5821 && TYPE_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (e
, 0))))
5822 result
= build3 (COMPONENT_REF
, type
,
5823 gnat_stabilize_reference_1 (TREE_OPERAND (e
, 0),
5825 TREE_OPERAND (e
, 1), TREE_OPERAND (e
, 2));
5826 else if (TREE_SIDE_EFFECTS (e
) || force
)
5827 return save_expr (e
);
5833 /* Constants need no processing. In fact, we should never reach
5838 /* Recursively stabilize each operand. */
5839 result
= build2 (code
, type
,
5840 gnat_stabilize_reference_1 (TREE_OPERAND (e
, 0), force
),
5841 gnat_stabilize_reference_1 (TREE_OPERAND (e
, 1),
5846 /* Recursively stabilize each operand. */
5847 result
= build1 (code
, type
,
5848 gnat_stabilize_reference_1 (TREE_OPERAND (e
, 0),
5856 TREE_READONLY (result
) = TREE_READONLY (e
);
5858 TREE_THIS_VOLATILE (result
) = TREE_THIS_VOLATILE (e
);
5859 TREE_SIDE_EFFECTS (result
) |= TREE_SIDE_EFFECTS (e
);
5863 extern char *__gnat_to_canonical_file_spec (char *);
5865 /* Convert Sloc into *LOCUS (a location_t). Return true if this Sloc
5866 corresponds to a source code location and false if it doesn't. In the
5867 latter case, we don't update *LOCUS. We also set the Gigi global variable
5868 REF_FILENAME to the reference file name as given by sinput (i.e no
5872 Sloc_to_locus (Source_Ptr Sloc
, location_t
*locus
)
5874 /* If node not from source code, ignore. */
5878 /* Use the identifier table to make a hashed, permanent copy of the filename,
5879 since the name table gets reallocated after Gigi returns but before all
5880 the debugging information is output. The __gnat_to_canonical_file_spec
5881 call translates filenames from pragmas Source_Reference that contain host
5882 style syntax not understood by gdb. */
5884 = IDENTIFIER_POINTER
5886 (__gnat_to_canonical_file_spec
5887 (Get_Name_String (Full_Debug_Name (Get_Source_File_Index (Sloc
))))));
5889 locus
->line
= Get_Logical_Line_Number (Sloc
);
5892 = IDENTIFIER_POINTER
5894 (Get_Name_String (Debug_Source_Name (Get_Source_File_Index (Sloc
)))));;
5899 /* Similar to annotate_with_locus, but start with the Sloc of GNAT_NODE and
5900 don't do anything if it doesn't correspond to a source location. */
5903 annotate_with_node (tree node
, Node_Id gnat_node
)
5907 if (!Sloc_to_locus (Sloc (gnat_node
), &locus
))
5910 annotate_with_locus (node
, locus
);
5913 /* Post an error message. MSG is the error message, properly annotated.
5914 NODE is the node at which to post the error and the node to use for the
5915 "&" substitution. */
5918 post_error (const char *msg
, Node_Id node
)
5920 String_Template temp
;
5923 temp
.Low_Bound
= 1, temp
.High_Bound
= strlen (msg
);
5924 fp
.Array
= msg
, fp
.Bounds
= &temp
;
5926 Error_Msg_N (fp
, node
);
5929 /* Similar, but NODE is the node at which to post the error and ENT
5930 is the node to use for the "&" substitution. */
5933 post_error_ne (const char *msg
, Node_Id node
, Entity_Id ent
)
5935 String_Template temp
;
5938 temp
.Low_Bound
= 1, temp
.High_Bound
= strlen (msg
);
5939 fp
.Array
= msg
, fp
.Bounds
= &temp
;
5941 Error_Msg_NE (fp
, node
, ent
);
5944 /* Similar, but NODE is the node at which to post the error, ENT is the node
5945 to use for the "&" substitution, and N is the number to use for the ^. */
5948 post_error_ne_num (const char *msg
, Node_Id node
, Entity_Id ent
, int n
)
5950 String_Template temp
;
5953 temp
.Low_Bound
= 1, temp
.High_Bound
= strlen (msg
);
5954 fp
.Array
= msg
, fp
.Bounds
= &temp
;
5955 Error_Msg_Uint_1
= UI_From_Int (n
);
5958 Error_Msg_NE (fp
, node
, ent
);
5961 /* Similar to post_error_ne_num, but T is a GCC tree representing the
5962 number to write. If the tree represents a constant that fits within
5963 a host integer, the text inside curly brackets in MSG will be output
5964 (presumably including a '^'). Otherwise that text will not be output
5965 and the text inside square brackets will be output instead. */
5968 post_error_ne_tree (const char *msg
, Node_Id node
, Entity_Id ent
, tree t
)
5970 char *newmsg
= alloca (strlen (msg
) + 1);
5971 String_Template temp
= {1, 0};
5973 char start_yes
, end_yes
, start_no
, end_no
;
5977 fp
.Array
= newmsg
, fp
.Bounds
= &temp
;
5979 if (host_integerp (t
, 1)
5980 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_INT
5983 (t
, (((unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_INT
- 1)) - 1)) < 0
5987 Error_Msg_Uint_1
= UI_From_Int (tree_low_cst (t
, 1));
5988 start_yes
= '{', end_yes
= '}', start_no
= '[', end_no
= ']';
5991 start_yes
= '[', end_yes
= ']', start_no
= '{', end_no
= '}';
5993 for (p
= msg
, q
= newmsg
; *p
; p
++)
5995 if (*p
== start_yes
)
5996 for (p
++; *p
!= end_yes
; p
++)
5998 else if (*p
== start_no
)
5999 for (p
++; *p
!= end_no
; p
++)
6007 temp
.High_Bound
= strlen (newmsg
);
6009 Error_Msg_NE (fp
, node
, ent
);
6012 /* Similar to post_error_ne_tree, except that NUM is a second
6013 integer to write in the message. */
6016 post_error_ne_tree_2 (const char *msg
,
6022 Error_Msg_Uint_2
= UI_From_Int (num
);
6023 post_error_ne_tree (msg
, node
, ent
, t
);
6026 /* Initialize the table that maps GNAT codes to GCC codes for simple
6027 binary and unary operations. */
6030 init_code_table (void)
6032 gnu_codes
[N_And_Then
] = TRUTH_ANDIF_EXPR
;
6033 gnu_codes
[N_Or_Else
] = TRUTH_ORIF_EXPR
;
6035 gnu_codes
[N_Op_And
] = TRUTH_AND_EXPR
;
6036 gnu_codes
[N_Op_Or
] = TRUTH_OR_EXPR
;
6037 gnu_codes
[N_Op_Xor
] = TRUTH_XOR_EXPR
;
6038 gnu_codes
[N_Op_Eq
] = EQ_EXPR
;
6039 gnu_codes
[N_Op_Ne
] = NE_EXPR
;
6040 gnu_codes
[N_Op_Lt
] = LT_EXPR
;
6041 gnu_codes
[N_Op_Le
] = LE_EXPR
;
6042 gnu_codes
[N_Op_Gt
] = GT_EXPR
;
6043 gnu_codes
[N_Op_Ge
] = GE_EXPR
;
6044 gnu_codes
[N_Op_Add
] = PLUS_EXPR
;
6045 gnu_codes
[N_Op_Subtract
] = MINUS_EXPR
;
6046 gnu_codes
[N_Op_Multiply
] = MULT_EXPR
;
6047 gnu_codes
[N_Op_Mod
] = FLOOR_MOD_EXPR
;
6048 gnu_codes
[N_Op_Rem
] = TRUNC_MOD_EXPR
;
6049 gnu_codes
[N_Op_Minus
] = NEGATE_EXPR
;
6050 gnu_codes
[N_Op_Abs
] = ABS_EXPR
;
6051 gnu_codes
[N_Op_Not
] = TRUTH_NOT_EXPR
;
6052 gnu_codes
[N_Op_Rotate_Left
] = LROTATE_EXPR
;
6053 gnu_codes
[N_Op_Rotate_Right
] = RROTATE_EXPR
;
6054 gnu_codes
[N_Op_Shift_Left
] = LSHIFT_EXPR
;
6055 gnu_codes
[N_Op_Shift_Right
] = RSHIFT_EXPR
;
6056 gnu_codes
[N_Op_Shift_Right_Arithmetic
] = RSHIFT_EXPR
;
6059 #include "gt-ada-trans.h"