Update FSF address
[gcc.git] / gcc / ada / trans.c
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * T R A N S *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2005, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 2, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License distributed with GNAT; see file COPYING. If not, write *
19 * to the Free Software Foundation, 51 Franklin Street, Fifth Floor, *
20 * Boston, MA 02110-1301, USA. *
21 * *
22 * GNAT was originally developed by the GNAT team at New York University. *
23 * Extensive contributions were provided by Ada Core Technologies Inc. *
24 * *
25 ****************************************************************************/
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "real.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "rtl.h"
36 #include "expr.h"
37 #include "ggc.h"
38 #include "function.h"
39 #include "except.h"
40 #include "debug.h"
41 #include "output.h"
42 #include "tree-gimple.h"
43 #include "ada.h"
44 #include "types.h"
45 #include "atree.h"
46 #include "elists.h"
47 #include "namet.h"
48 #include "nlists.h"
49 #include "snames.h"
50 #include "stringt.h"
51 #include "uintp.h"
52 #include "urealp.h"
53 #include "fe.h"
54 #include "sinfo.h"
55 #include "einfo.h"
56 #include "ada-tree.h"
57 #include "gigi.h"
58
59 int max_gnat_nodes;
60 int number_names;
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;
69
70 /* Current filename without path. */
71 const char *ref_filename;
72
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;
77
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. */
82
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. */
88 };
89
90 static GTY(()) struct stmt_group *current_stmt_group;
91
92 /* List of unused struct stmt_group nodes. */
93 static GTY((deletable)) struct stmt_group *stmt_group_free_list;
94
95 /* A structure used to record information on elaboration procedures
96 we've made and need to process.
97
98 ??? gnat_node should be Node_Id, but gengtype gets confused. */
99
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. */
104 };
105
106 static GTY(()) struct elab_info *elab_info_list;
107
108 /* Free list of TREE_LIST nodes used for stacks. */
109 static GTY((deletable)) tree gnu_stack_free_list;
110
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;
116
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;
120
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;
124
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;
128
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;
132
133 /* Map GNAT tree codes to GCC tree codes for simple expressions. */
134 static enum tree_code gnu_codes[Number_Node_Kinds];
135
136 /* Current node being treated, in case abort called. */
137 Node_Id error_gnat_node;
138
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);
167
168 \f
169 /* This is the main program of the back-end. It sets up all the table
170 structures and then generates code. */
171
172 void
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)
181 {
182 tree gnu_standard_long_long_float;
183 tree gnu_standard_exception_type;
184 struct elab_info *info;
185
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;
196
197 type_annotate_only = (gigi_operating_mode == 1);
198
199 init_gnat_to_gnu ();
200 gnat_compute_largest_alignment ();
201 init_dummy_type ();
202
203 /* If we are just annotating types, give VOID_TYPE zero sizes to avoid
204 errors. */
205 if (type_annotate_only)
206 {
207 TYPE_SIZE (void_type_node) = bitsize_zero_node;
208 TYPE_SIZE_UNIT (void_type_node) = size_zero_node;
209 }
210
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
213 may be subtypes. */
214 save_gnu_tree (Base_Type (standard_integer), TYPE_NAME (integer_type_node),
215 false);
216
217 gnu_except_ptr_stack = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
218
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);
223
224 init_gigi_decls (gnu_standard_long_long_float, gnu_standard_exception_type);
225
226 /* Process any Pragma Ident for the main unit. */
227 #ifdef ASM_OUTPUT_IDENT
228 if (Present (Ident_String (Main_Unit)))
229 ASM_OUTPUT_IDENT
230 (asm_out_file,
231 TREE_STRING_POINTER (gnat_to_gnu (Ident_String (Main_Unit))));
232 #endif
233
234 /* If we are using the GCC exception mechanism, let GCC know. */
235 if (Exception_Mechanism == GCC_ZCX)
236 gnat_init_gcc_eh ();
237
238 gcc_assert (Nkind (gnat_root) == N_Compilation_Unit);
239 Compilation_Unit_to_gnu (gnat_root);
240
241 /* Now see if we have any elaboration procedures to deal with. */
242 for (info = elab_info_list; info; info = info->next)
243 {
244 tree gnu_body = DECL_SAVED_TREE (info->elab_proc);
245 tree gnu_stmts;
246
247 /* Mark everything we have as not visited. */
248 walk_tree_without_duplicates (&gnu_body, mark_unvisited, NULL);
249
250 /* Set the current function to be the elaboration procedure and gimplify
251 what we have. */
252 current_function_decl = info->elab_proc;
253 gimplify_body (&gnu_body, info->elab_proc, true);
254
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);
260
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);
264 else
265 {
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);
270 }
271 }
272 }
273 \f
274 /* Perform initializations for this module. */
275
276 void
277 gnat_init_stmt_group ()
278 {
279 /* Initialize ourselves. */
280 init_code_table ();
281 start_stmt_group ();
282
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"));
286
287 gcc_assert (Exception_Mechanism != Front_End_ZCX);
288 }
289 \f
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. */
293
294 static tree
295 Identifier_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
296 {
297 tree gnu_result_type;
298 tree gnu_result;
299 Node_Id gnat_temp, gnat_temp_type;
300
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);
313
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)));
331
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.
335
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))
344 {
345 gnat_temp = Full_View (gnat_temp);
346 gnat_temp_type = Etype (gnat_temp);
347 gnu_result_type = get_unpadded_type (gnat_temp_type);
348 }
349 else
350 {
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);
363 else
364 gnat_temp_type = Etype (gnat_node);
365
366 gnu_result_type = get_unpadded_type (gnat_temp_type);
367 }
368
369 gnu_result = gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0);
370
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.
374
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.
378
379 ??? Also, for now all we can do is make it volatile. But we only
380 do this for SJLJ. */
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;
384
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))))
394 {
395 bool ro = DECL_POINTS_TO_READONLY_P (gnu_result);
396 tree renamed_obj;
397
398 if (TREE_CODE (gnu_result) == PARM_DECL
399 && DECL_BY_COMPONENT_PTR_P (gnu_result))
400 gnu_result
401 = build_unary_op (INDIRECT_REF, NULL_TREE,
402 convert (build_pointer_type (gnu_result_type),
403 gnu_result));
404
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;
415 else
416 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE,
417 fold (gnu_result));
418
419 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result) = ro;
420 }
421
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))
427 {
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));
432 }
433
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)
438 {
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))
444 == Attr_Address)
445 || (Get_Attribute_Id (Attribute_Name (gnat_temp))
446 == Attr_Access)
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);
452 }
453
454 *gnu_result_type_p = gnu_result_type;
455 return gnu_result;
456 }
457 \f
458 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Pragma. Return
459 any statements we generate. */
460
461 static tree
462 Pragma_to_gnu (Node_Id gnat_node)
463 {
464 Node_Id gnat_temp;
465 tree gnu_result = alloc_stmt_list ();
466
467 /* Check for (and ignore) unrecognized pragma and do nothing if we are just
468 annotating types. */
469 if (type_annotate_only || !Is_Pragma_Name (Chars (gnat_node)))
470 return gnu_result;
471
472 switch (Get_Pragma_Id (Chars (gnat_node)))
473 {
474 case Pragma_Inspection_Point:
475 /* Do nothing at top level: all such variables are already viewable. */
476 if (global_bindings_p ())
477 break;
478
479 for (gnat_temp = First (Pragma_Argument_Associations (gnat_node));
480 Present (gnat_temp);
481 gnat_temp = Next (gnat_temp))
482 {
483 tree gnu_expr = gnat_to_gnu (Expression (gnat_temp));
484
485 if (TREE_CODE (gnu_expr) == UNCONSTRAINED_ARRAY_REF)
486 gnu_expr = TREE_OPERAND (gnu_expr, 0);
487
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);
491 }
492 break;
493
494 case Pragma_Optimize:
495 switch (Chars (Expression
496 (First (Pragma_Argument_Associations (gnat_node)))))
497 {
498 case Name_Time: case Name_Space:
499 if (optimize == 0)
500 post_error ("insufficient -O value?", gnat_node);
501 break;
502
503 case Name_Off:
504 if (optimize != 0)
505 post_error ("must specify -O0?", gnat_node);
506 break;
507
508 default:
509 gcc_unreachable ();
510 }
511 break;
512
513 case Pragma_Reviewable:
514 if (write_symbols == NO_DEBUG)
515 post_error ("must specify -g?", gnat_node);
516 break;
517 }
518
519 return gnu_result;
520 }
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. */
524
525 static tree
526 Attribute_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, int attribute)
527 {
528 tree gnu_result = error_mark_node;
529 tree gnu_result_type;
530 tree gnu_expr;
531 bool prefix_unused = false;
532 tree gnu_prefix = gnat_to_gnu (Prefix (gnat_node));
533 tree gnu_type = TREE_TYPE (gnu_prefix);
534
535 /* If the input is a NULL_EXPR, make a new one. */
536 if (TREE_CODE (gnu_prefix) == NULL_EXPR)
537 {
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));
541 }
542
543 switch (attribute)
544 {
545 case Attr_Pos:
546 case Attr_Val:
547 /* These are just conversions until since representation clauses for
548 enumerations are handled in the front end. */
549 {
550 bool checkp = Do_Range_Check (First (Expressions (gnat_node)));
551
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);
556 }
557 break;
558
559 case Attr_Pred:
560 case Attr_Succ:
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));
565
566 if (Do_Range_Check (First (Expressions (gnat_node))))
567 {
568 gnu_expr = protect_multiple_eval (gnu_expr);
569 gnu_expr
570 = emit_check
571 (build_binary_op (EQ_EXPR, integer_type_node,
572 gnu_expr,
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);
577 }
578
579 gnu_result
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));
584 break;
585
586 case Attr_Address:
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));
592
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);
597
598 /* ... fall through ... */
599
600 case Attr_Access:
601 case Attr_Unchecked_Access:
602 case Attr_Code_Address:
603 gnu_result_type = get_unpadded_type (Etype (gnat_node));
604 gnu_result
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);
610
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)
614 {
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;
620
621 if (TREE_CODE (gnu_expr) == ADDR_EXPR)
622 TREE_STATIC (gnu_expr) = TREE_CONSTANT (gnu_expr) = 1;
623 }
624 break;
625
626 case Attr_Pool_Address:
627 {
628 tree gnu_obj_type;
629 tree gnu_ptr = gnu_prefix;
630
631 gnu_result_type = get_unpadded_type (Etype (gnat_node));
632
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
635 template address.*/
636 if (TYPE_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
637 gnu_ptr
638 = convert (build_pointer_type
639 (TYPE_OBJECT_RECORD_TYPE
640 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
641 gnu_ptr);
642
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))
646 {
647 tree gnu_char_ptr_type = build_pointer_type (char_type_node);
648 tree gnu_pos = byte_position (TYPE_FIELDS (gnu_obj_type));
649 tree gnu_byte_offset
650 = convert (gnu_char_ptr_type,
651 size_diffop (size_zero_node, gnu_pos));
652
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);
656 }
657
658 gnu_result = convert (gnu_result_type, gnu_ptr);
659 }
660 break;
661
662 case Attr_Size:
663 case Attr_Object_Size:
664 case Attr_Value_Size:
665 case Attr_Max_Size_In_Storage_Elements:
666 gnu_expr = gnu_prefix;
667
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)
672 ;
673
674 gnu_prefix = remove_conversions (gnu_prefix, true);
675 prefix_unused = true;
676 gnu_type = TREE_TYPE (gnu_prefix);
677
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
682 template. */
683 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
684 {
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)));
688 }
689
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
694 type. */
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)
700 {
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)
710 {
711 gnu_result = rm_size (gnu_type);
712 if (!(CONTAINS_PLACEHOLDER_P
713 (DECL_SIZE (TREE_OPERAND (gnu_expr, 1)))))
714 gnu_result
715 = size_binop (MAX_EXPR, gnu_result,
716 DECL_SIZE (TREE_OPERAND (gnu_expr, 1)));
717 }
718 else
719 gnu_result = TYPE_SIZE (gnu_type);
720 }
721 else
722 gnu_result = rm_size (gnu_type);
723
724 gcc_assert (gnu_result);
725
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
728 object. */
729 if (CONTAINS_PLACEHOLDER_P (gnu_result))
730 {
731 if (TREE_CODE (gnu_prefix) != TYPE_DECL)
732 gnu_result = substitute_placeholder_in_expr (gnu_result, gnu_expr);
733 else
734 gnu_result = max_size (gnu_result, true);
735 }
736
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)));
742
743 gnu_result_type = get_unpadded_type (Etype (gnat_node));
744
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.
748
749 ??? Size should be calculated in storage elements directly. */
750
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)));
755 break;
756
757 case Attr_Alignment:
758 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
759 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))
760 == RECORD_TYPE)
761 && (TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))))
762 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
763
764 gnu_type = TREE_TYPE (gnu_prefix);
765 gnu_result_type = get_unpadded_type (Etype (gnat_node));
766 prefix_unused = true;
767
768 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
769 gnu_result = size_int (DECL_ALIGN (TREE_OPERAND (gnu_prefix, 1)));
770 else
771 gnu_result = size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT);
772 break;
773
774 case Attr_First:
775 case Attr_Last:
776 case Attr_Range_Length:
777 prefix_unused = true;
778
779 if (INTEGRAL_TYPE_P (gnu_type) || TREE_CODE (gnu_type) == REAL_TYPE)
780 {
781 gnu_result_type = get_unpadded_type (Etype (gnat_node));
782
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);
787 else
788 gnu_result
789 = build_binary_op
790 (MAX_EXPR, get_base_type (gnu_result_type),
791 build_binary_op
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));
801
802 break;
803 }
804
805 /* ... fall through ... */
806
807 case Attr_Length:
808 {
809 int Dimension = (Present (Expressions (gnat_node))
810 ? UI_To_Int (Intval (First (Expressions (gnat_node))))
811 : 1);
812
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));
819
820 if (TYPE_CONVENTION_FORTRAN_P (gnu_type))
821 {
822 int ndim;
823 tree gnu_type_temp;
824
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))
829 ;
830
831 Dimension = ndim + 1 - Dimension;
832 }
833
834 for (; Dimension > 1; Dimension--)
835 gnu_type = TREE_TYPE (gnu_type);
836
837 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
838 if (attribute == Attr_First)
839 gnu_result
840 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
841 else if (attribute == Attr_Last)
842 gnu_result
843 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
844 else
845 /* 'Length or 'Range_Length. */
846 {
847 tree gnu_compute_type
848 = gnat_signed_or_unsigned_type (0,
849 get_base_type (gnu_result_type));
850
851 gnu_result
852 = build_binary_op
853 (MAX_EXPR, gnu_compute_type,
854 build_binary_op
855 (PLUS_EXPR, gnu_compute_type,
856 build_binary_op
857 (MINUS_EXPR, gnu_compute_type,
858 convert (gnu_compute_type,
859 TYPE_MAX_VALUE
860 (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)))),
861 convert (gnu_compute_type,
862 TYPE_MIN_VALUE
863 (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))))),
864 convert (gnu_compute_type, integer_one_node)),
865 convert (gnu_compute_type, integer_zero_node));
866 }
867
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,
872 gnu_prefix);
873 break;
874 }
875
876 case Attr_Bit_Position:
877 case Attr_Position:
878 case Attr_First_Bit:
879 case Attr_Last_Bit:
880 case Attr_Bit:
881 {
882 HOST_WIDE_INT bitsize;
883 HOST_WIDE_INT bitpos;
884 tree gnu_offset;
885 tree gnu_field_bitpos;
886 tree gnu_field_offset;
887 tree gnu_inner;
888 enum machine_mode mode;
889 int unsignedp, volatilep;
890
891 gnu_result_type = get_unpadded_type (Etype (gnat_node));
892 gnu_prefix = remove_conversions (gnu_prefix, true);
893 prefix_unused = true;
894
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)
900 {
901 gnu_result = integer_zero_node;
902 break;
903 }
904
905 else
906 gcc_assert (TREE_CODE (gnu_prefix) == COMPONENT_REF
907 || (attribute == Attr_Bit_Position
908 && TREE_CODE (gnu_prefix) == FIELD_DECL));
909
910 get_inner_reference (gnu_prefix, &bitsize, &bitpos, &gnu_offset,
911 &mode, &unsignedp, &volatilep, false);
912
913 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
914 {
915 gnu_field_bitpos = bit_position (TREE_OPERAND (gnu_prefix, 1));
916 gnu_field_offset = byte_position (TREE_OPERAND (gnu_prefix, 1));
917
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))
922 {
923 gnu_field_bitpos
924 = size_binop (PLUS_EXPR, gnu_field_bitpos,
925 bit_position (TREE_OPERAND (gnu_inner, 1)));
926 gnu_field_offset
927 = size_binop (PLUS_EXPR, gnu_field_offset,
928 byte_position (TREE_OPERAND (gnu_inner, 1)));
929 }
930 }
931 else if (TREE_CODE (gnu_prefix) == FIELD_DECL)
932 {
933 gnu_field_bitpos = bit_position (gnu_prefix);
934 gnu_field_offset = byte_position (gnu_prefix);
935 }
936 else
937 {
938 gnu_field_bitpos = bitsize_zero_node;
939 gnu_field_offset = size_zero_node;
940 }
941
942 switch (attribute)
943 {
944 case Attr_Position:
945 gnu_result = gnu_field_offset;
946 break;
947
948 case Attr_First_Bit:
949 case Attr_Bit:
950 gnu_result = size_int (bitpos % BITS_PER_UNIT);
951 break;
952
953 case Attr_Last_Bit:
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,
958 bitsize_one_node);
959 break;
960
961 case Attr_Bit_Position:
962 gnu_result = gnu_field_bitpos;
963 break;
964 }
965
966 /* If this has a PLACEHOLDER_EXPR, qualify it by the object
967 we are handling. */
968 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
969 break;
970 }
971
972 case Attr_Min:
973 case Attr_Max:
974 {
975 tree gnu_lhs = gnat_to_gnu (First (Expressions (gnat_node)));
976 tree gnu_rhs = gnat_to_gnu (Next (First (Expressions (gnat_node))));
977
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);
982 }
983 break;
984
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));
989 break;
990
991 case Attr_Component_Size:
992 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
993 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))
994 == RECORD_TYPE)
995 && (TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))))
996 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
997
998 gnu_prefix = maybe_implicit_deref (gnu_prefix);
999 gnu_type = TREE_TYPE (gnu_prefix);
1000
1001 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
1002 gnu_type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type))));
1003
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);
1007
1008 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
1009
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;
1014 break;
1015
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));
1020 gnu_result
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;
1025 break;
1026
1027 case Attr_Mechanism_Code:
1028 {
1029 int code;
1030 Entity_Id gnat_obj = Entity (Prefix (gnat_node));
1031
1032 prefix_unused = true;
1033 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1034 if (Present (Expressions (gnat_node)))
1035 {
1036 int i = UI_To_Int (Intval (First (Expressions (gnat_node))));
1037
1038 for (gnat_obj = First_Formal (gnat_obj); i > 1;
1039 i--, gnat_obj = Next_Formal (gnat_obj))
1040 ;
1041 }
1042
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))
1048 == PARM_DECL)
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));
1053 }
1054 break;
1055
1056 default:
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;
1063 break;
1064 }
1065
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));
1074
1075 *gnu_result_type_p = gnu_result_type;
1076 return gnu_result;
1077 }
1078 \f
1079 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Case_Statement,
1080 to a GCC tree, which is returned. */
1081
1082 static tree
1083 Case_Statement_to_gnu (Node_Id gnat_node)
1084 {
1085 tree gnu_result;
1086 tree gnu_expr;
1087 Node_Id gnat_when;
1088
1089 gnu_expr = gnat_to_gnu (Expression (gnat_node));
1090 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
1091
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.
1100
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);
1107
1108 /* We build a SWITCH_EXPR that contains the code with interspersed
1109 CASE_LABEL_EXPRs for each label. */
1110
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))
1116 {
1117 Node_Id gnat_choice;
1118
1119 /* First compile all the different case choices for the current WHEN
1120 alternative. */
1121 for (gnat_choice = First (Discrete_Choices (gnat_when));
1122 Present (gnat_choice); gnat_choice = Next (gnat_choice))
1123 {
1124 tree gnu_low = NULL_TREE, gnu_high = NULL_TREE;
1125
1126 switch (Nkind (gnat_choice))
1127 {
1128 case N_Range:
1129 gnu_low = gnat_to_gnu (Low_Bound (gnat_choice));
1130 gnu_high = gnat_to_gnu (High_Bound (gnat_choice));
1131 break;
1132
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))));
1138 break;
1139
1140 case N_Identifier:
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))
1146 {
1147 tree gnu_type = get_unpadded_type (Entity (gnat_choice));
1148
1149 gnu_low = fold (TYPE_MIN_VALUE (gnu_type));
1150 gnu_high = fold (TYPE_MAX_VALUE (gnu_type));
1151 break;
1152 }
1153
1154 /* ... fall through ... */
1155
1156 case N_Character_Literal:
1157 case N_Integer_Literal:
1158 gnu_low = gnat_to_gnu (gnat_choice);
1159 break;
1160
1161 case N_Others_Choice:
1162 break;
1163
1164 default:
1165 gcc_unreachable ();
1166 }
1167
1168 add_stmt_with_node (build3 (CASE_LABEL_EXPR, void_type_node,
1169 gnu_low, gnu_high,
1170 create_artificial_label ()),
1171 gnat_choice);
1172 }
1173
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)));
1180 }
1181
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);
1188
1189 return gnu_result;
1190 }
1191 \f
1192 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Loop_Statement,
1193 to a GCC tree, which is returned. */
1194
1195 static tree
1196 Loop_Statement_to_gnu (Node_Id gnat_node)
1197 {
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;
1204 tree gnu_result;
1205
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);
1210
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));
1215
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))
1219 ;
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));
1224 else
1225 {
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));
1240
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))
1248 {
1249 gnu_cond_expr
1250 = build3 (COND_EXPR, void_type_node,
1251 build_binary_op (LE_EXPR, integer_type_node,
1252 gnu_low, gnu_high),
1253 NULL_TREE, alloc_stmt_list ());
1254 annotate_with_node (gnu_cond_expr, gnat_loop_spec);
1255 }
1256
1257 /* Open a new nesting level that will surround the loop to declare the
1258 loop index variable. */
1259 start_stmt_group ();
1260 gnat_pushlevel ();
1261
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);
1266
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);
1270
1271 /* Set either the top or bottom exit condition as appropriate depending
1272 on whether or not we know an overflow cannot occur. */
1273 if (gnu_cond_expr)
1274 LOOP_STMT_BOT_COND (gnu_loop_stmt)
1275 = build_binary_op (NE_EXPR, integer_type_node,
1276 gnu_loop_var, gnu_last);
1277 else
1278 LOOP_STMT_TOP_COND (gnu_loop_stmt)
1279 = build_binary_op (end_code, integer_type_node,
1280 gnu_loop_var, gnu_last);
1281
1282 LOOP_STMT_UPDATE (gnu_loop_stmt)
1283 = build_binary_op (reversep ? PREDECREMENT_EXPR
1284 : PREINCREMENT_EXPR,
1285 TREE_TYPE (gnu_loop_var),
1286 gnu_loop_var,
1287 convert (TREE_TYPE (gnu_loop_var),
1288 integer_one_node));
1289 annotate_with_node (LOOP_STMT_UPDATE (gnu_loop_stmt),
1290 gnat_iter_scheme);
1291 }
1292
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
1295 LOOP_STMT. */
1296 if (Present (Identifier (gnat_node)))
1297 save_gnu_tree (Entity (Identifier (gnat_node)),
1298 LOOP_STMT_LABEL (gnu_loop_stmt), true);
1299
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);
1304
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". */
1307 if (gnu_loop_var)
1308 {
1309 add_stmt (gnu_loop_stmt);
1310 gnat_poplevel ();
1311 gnu_loop_stmt = end_stmt_group ();
1312 }
1313
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. */
1316 if (gnu_cond_expr)
1317 {
1318 COND_EXPR_THEN (gnu_cond_expr) = gnu_loop_stmt;
1319 gnu_result = gnu_cond_expr;
1320 recalculate_side_effects (gnu_cond_expr);
1321 }
1322 else
1323 gnu_result = gnu_loop_stmt;
1324
1325 pop_stack (&gnu_loop_label_stack);
1326
1327 return gnu_result;
1328 }
1329 \f
1330 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Subprogram_Body. We
1331 don't return anything. */
1332
1333 static void
1334 Subprogram_Body_to_gnu (Node_Id gnat_node)
1335 {
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;
1352 tree gnu_cico_list;
1353 tree gnu_result;
1354
1355 /* If this is a generic object or if it has been eliminated,
1356 ignore it. */
1357 if (Ekind (gnat_subprog_id) == E_Generic_Procedure
1358 || Ekind (gnat_subprog_id) == E_Generic_Function
1359 || Is_Eliminated (gnat_subprog_id))
1360 return;
1361
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))
1365 {
1366 write_symbols = NO_DEBUG;
1367 debug_hooks = &do_nothing_debug_hooks;
1368 }
1369
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. */
1376 gnu_subprog_decl
1377 = gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE,
1378 Acts_As_Spec (gnat_node)
1379 && !present_gnu_tree (gnat_subprog_id));
1380
1381 gnu_subprog_type = TREE_TYPE (gnu_subprog_decl);
1382
1383 /* Set the line number in the decl to correspond to that of the body so that
1384 the line number notes are written
1385 correctly. */
1386 Sloc_to_locus (Sloc (gnat_node), &DECL_SOURCE_LOCATION (gnu_subprog_decl));
1387
1388 begin_subprog_body (gnu_subprog_decl);
1389 gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
1390
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);
1396
1397 /* Get a tree corresponding to the code for the subprogram. */
1398 start_stmt_group ();
1399 gnat_pushlevel ();
1400
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))
1410 {
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))
1415 ;
1416
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));
1421 }
1422
1423 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
1424
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)));
1428 gnat_poplevel ();
1429 gnu_result = end_stmt_group ();
1430
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))
1435 {
1436 tree gnu_retval;
1437
1438 start_stmt_group ();
1439 gnat_pushlevel ();
1440 add_stmt (gnu_result);
1441 add_stmt (build1 (LABEL_EXPR, void_type_node,
1442 TREE_VALUE (gnu_return_label_stack)));
1443
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);
1447 else
1448 gnu_retval = gnat_build_constructor (TREE_TYPE (gnu_subprog_type),
1449 gnu_cico_list);
1450
1451 if (DECL_P (gnu_retval) && DECL_BY_REF_P (gnu_retval))
1452 gnu_retval = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_retval);
1453
1454 add_stmt_with_node
1455 (build1 (RETURN_EXPR, void_type_node,
1456 build2 (MODIFY_EXPR, TREE_TYPE (gnu_retval),
1457 DECL_RESULT (current_function_decl), gnu_retval)),
1458 gnat_node);
1459 gnat_poplevel ();
1460 gnu_result = end_stmt_group ();
1461 }
1462
1463 pop_stack (&gnu_return_label_stack);
1464
1465 /* Initialize the information node for the function and set the
1466 end location. */
1467 allocate_struct_function (current_function_decl);
1468 Sloc_to_locus
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);
1473
1474 end_subprog_body (gnu_result);
1475
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);
1483
1484 mark_out_of_scope (Defining_Unit_Name (Specification (gnat_node)));
1485 write_symbols = save_write_symbols;
1486 debug_hooks = save_debug_hooks;
1487 }
1488 \f
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. */
1494
1495 static tree
1496 call_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, tree gnu_target)
1497 {
1498 tree gnu_result;
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
1502 subprogram. */
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,
1507 gnu_subprog_node);
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;
1515
1516 switch (Nkind (Name (gnat_node)))
1517 {
1518 case N_Identifier:
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)));
1524 }
1525
1526 gcc_assert (TREE_CODE (gnu_subprog_type) == FUNCTION_TYPE);
1527
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))
1532 {
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));
1537
1538 if (Nkind (gnat_node) == N_Function_Call && !gnu_target)
1539 {
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));
1543 }
1544 else
1545 return build_call_raise (PE_Stubbed_Subprogram_Called);
1546 }
1547
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
1551 of the type. */
1552 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type))
1553 {
1554 tree gnu_real_ret_type
1555 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (gnu_subprog_type)));
1556
1557 if (!gnu_target)
1558 {
1559 tree gnu_obj_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,
1563 false, false);
1564
1565 gnu_target = create_tmp_var_raw (gnu_obj_type, "LR");
1566 gnat_pushdecl (gnu_target, gnat_node);
1567 }
1568
1569 gnu_actual_list
1570 = tree_cons (NULL_TREE,
1571 build_unary_op (ADDR_EXPR, NULL_TREE,
1572 unchecked_convert (gnu_real_ret_type,
1573 gnu_target,
1574 false)),
1575 NULL_TREE);
1576
1577 }
1578
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. */
1587 gnat_formal = 0;
1588 else
1589 gnat_formal = First_Formal (Entity (Name (gnat_node)));
1590
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))
1599 {
1600 tree gnu_formal
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));
1613 tree gnu_actual;
1614 tree gnu_formal_type;
1615
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.
1619
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)
1625 {
1626 gnu_name = gnat_stabilize_reference (gnu_name, true);
1627 if (!addressable_p (gnu_name)
1628 && gnu_formal
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))))))
1633 {
1634 tree gnu_copy = gnu_name;
1635 tree gnu_temp;
1636
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. */
1644
1645 if (Is_Valued_Procedure (Entity (Name (gnat_node))))
1646 {
1647 post_error
1648 ("?possible violation of implicit assumption",
1649 gnat_actual);
1650 post_error_ne
1651 ("?made by pragma Import_Valued_Procedure on &",
1652 gnat_actual, Entity (Name (gnat_node)));
1653 post_error_ne
1654 ("?because of misalignment of &",
1655 gnat_actual, gnat_formal);
1656 }
1657
1658 /* Remove any unpadding on the actual and make a copy. But if
1659 the actual is a justified modular type, first convert
1660 to it. */
1661 if (TREE_CODE (gnu_name) == COMPONENT_REF
1662 && ((TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_name, 0)))
1663 == RECORD_TYPE)
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);
1670
1671 gnu_actual = save_expr (gnu_name);
1672
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)
1678 {
1679 TREE_ADDRESSABLE (gnu_temp) = 1;
1680 TREE_READONLY (gnu_temp) = 0;
1681 }
1682
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);
1688 }
1689 }
1690
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)),
1698 gnu_actual);
1699
1700 /* Unless this is an In parameter, we must remove any LJM building
1701 from GNU_NAME. */
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))),
1707 gnu_name);
1708
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));
1713
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)
1718 {
1719 gnu_actual
1720 = unchecked_convert (gnat_to_gnu_type (Etype (gnat_actual)),
1721 gnu_actual,
1722 (Nkind (gnat_actual)
1723 == N_Unchecked_Type_Conversion)
1724 && No_Truncation (gnat_actual));
1725
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));
1732 }
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)),
1737 gnu_actual);
1738
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
1742 reference. */
1743 if (gnu_formal
1744 && TREE_CODE (gnu_formal) == PARM_DECL && DECL_BY_REF_P (gnu_formal))
1745 {
1746 if (Ekind (gnat_formal) != E_In_Parameter)
1747 {
1748 gnu_actual = gnu_name;
1749
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)),
1755 gnu_actual);
1756
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)),
1769 gnu_actual);
1770 }
1771
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)))
1779 == RECORD_TYPE)
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);
1784
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);
1789 }
1790 else if (gnu_formal && TREE_CODE (gnu_formal) == PARM_DECL
1791 && DECL_BY_COMPONENT_PTR_P (gnu_formal))
1792 {
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);
1796
1797 if (TREE_CODE (gnu_formal_type) == RECORD_TYPE
1798 && TYPE_IS_PADDING_P (gnu_formal_type))
1799 {
1800 gnu_formal_type = TREE_TYPE (TYPE_FIELDS (gnu_formal_type));
1801 gnu_actual = convert (gnu_formal_type, gnu_actual);
1802 }
1803
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,
1812 gnu_actual));
1813 }
1814 else if (gnu_formal && TREE_CODE (gnu_formal) == PARM_DECL
1815 && DECL_BY_DESCRIPTOR_P (gnu_formal))
1816 {
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)),
1822 integer_zero_node);
1823 else
1824 gnu_actual = build_unary_op (ADDR_EXPR, NULL_TREE,
1825 fill_vms_descriptor (gnu_actual,
1826 gnat_formal));
1827 }
1828 else
1829 {
1830 tree gnu_actual_size = TYPE_SIZE (TREE_TYPE (gnu_actual));
1831
1832 if (Ekind (gnat_formal) != E_In_Parameter)
1833 gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list);
1834
1835 if (!gnu_formal || TREE_CODE (gnu_formal) != PARM_DECL)
1836 continue;
1837
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,
1844 BITS_PER_WORD))
1845 gnu_actual
1846 = unchecked_convert (DECL_ARG_TYPE (gnu_formal),
1847 convert (gnat_type_for_size
1848 (tree_low_cst (gnu_actual_size, 1),
1849 1),
1850 integer_zero_node),
1851 false);
1852 else
1853 gnu_actual = convert (DECL_ARG_TYPE (gnu_formal), gnu_actual);
1854 }
1855
1856 gnu_actual_list = tree_cons (NULL_TREE, gnu_actual, gnu_actual_list);
1857 }
1858
1859 gnu_subprog_call = build3 (CALL_EXPR, TREE_TYPE (gnu_subprog_type),
1860 gnu_subprog_addr, nreverse (gnu_actual_list),
1861 NULL_TREE);
1862
1863 /* If we return by passing a target, we emit the call and return the target
1864 as our result. */
1865 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type))
1866 {
1867 add_stmt_with_node (gnu_subprog_call, gnat_node);
1868 *gnu_result_type_p
1869 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (gnu_subprog_type)));
1870 return unchecked_convert (*gnu_result_type_p, gnu_target, false);
1871 }
1872
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)
1877 {
1878 gnu_result = gnu_subprog_call;
1879
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);
1885
1886 if (gnu_target)
1887 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
1888 gnu_target, gnu_result);
1889 else
1890 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
1891
1892 return gnu_result;
1893 }
1894
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)
1899 {
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);
1904
1905 if (length > 1)
1906 {
1907 tree gnu_name;
1908
1909 gnu_subprog_call = save_expr (gnu_subprog_call);
1910 gnu_name_list = nreverse (gnu_name_list);
1911
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),
1918 &gnu_before_list);
1919 }
1920
1921 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
1922 gnat_formal = First_Formal (Etype (Name (gnat_node)));
1923 else
1924 gnat_formal = First_Formal (Entity (Name (gnat_node)));
1925
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)
1940 {
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. */
1944 tree gnu_result
1945 = length == 1 ? gnu_subprog_call
1946 : build_component_ref (gnu_subprog_call, NULL_TREE,
1947 TREE_PURPOSE (scalar_return_list),
1948 false);
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. */
1954 tree gnu_actual
1955 = maybe_unconstrained_array (TREE_VALUE (gnu_name_list));
1956
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))),
1962 gnu_result);
1963
1964 /* If the result is a type conversion, do it. */
1965 if (Nkind (gnat_actual) == N_Type_Conversion)
1966 gnu_result
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));
1972
1973 else if (unchecked_conversion)
1974 gnu_result = unchecked_convert (TREE_TYPE (gnu_actual),
1975 gnu_result,
1976 No_Truncation (gnat_actual));
1977 else
1978 {
1979 if (Do_Range_Check (gnat_actual))
1980 gnu_result = emit_range_check (gnu_result,
1981 Etype (gnat_actual));
1982
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);
1986 }
1987
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);
1994 }
1995 }
1996 else
1997 {
1998 annotate_with_node (gnu_subprog_call, gnat_node);
1999 append_to_statement_list (gnu_subprog_call, &gnu_before_list);
2000 }
2001
2002 append_to_statement_list (gnu_after_list, &gnu_before_list);
2003 return gnu_before_list;
2004 }
2005 \f
2006 /* Subroutine of gnat_to_gnu to translate gnat_node, an
2007 N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */
2008
2009 static tree
2010 Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node)
2011 {
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);
2018 bool setjmp_longjmp
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. */
2024 tree gnu_result;
2025 tree gnu_expr;
2026 Node_Id gnat_temp;
2027
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.
2032
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.
2039
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)
2043 {
2044 start_stmt_group ();
2045 gnat_pushlevel ();
2046 }
2047
2048 /* If we are to call a function when exiting this block add a cleanup
2049 to the binding level we made above. */
2050 if (at_end)
2051 add_cleanup (build_call_0_expr (gnat_to_gnu (At_End_Proc (gnat_node))));
2052
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. */
2056 if (setjmp_longjmp)
2057 {
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,
2062 gnat_node);
2063 gnu_jmpbuf_decl = create_var_decl (get_identifier ("JMP_BUF"),
2064 NULL_TREE, jmpbuf_type,
2065 NULL_TREE, false, false, false, false,
2066 NULL, gnat_node);
2067
2068 set_block_jmpbuf_decl (gnu_jmpbuf_decl);
2069
2070 /* When we exit this block, restore the saved value. */
2071 add_cleanup (build_call_1_expr (set_jmpbuf_decl, gnu_jmpsave_decl));
2072 }
2073
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 ();
2077
2078 if (setjmp_longjmp)
2079 add_stmt (build_call_1_expr (set_jmpbuf_decl,
2080 build_unary_op (ADDR_EXPR, NULL_TREE,
2081 gnu_jmpbuf_decl)));
2082
2083 if (Present (First_Real_Statement (gnat_node)))
2084 process_decls (Statements (gnat_node), Empty,
2085 First_Real_Statement (gnat_node), true, true);
2086
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 ();
2094
2095 /* Now generate code for the two exception models, if either is relevant for
2096 this block. */
2097 if (setjmp_longjmp)
2098 {
2099 tree *gnu_else_ptr = 0;
2100 tree gnu_handler;
2101
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 ();
2105 gnat_pushlevel ();
2106
2107 push_stack (&gnu_except_ptr_stack, NULL_TREE,
2108 create_var_decl (get_identifier ("EXCEPT_PTR"),
2109 NULL_TREE,
2110 build_pointer_type (except_type_node),
2111 build_call_0_expr (get_excptr_decl), false,
2112 false, false, false, NULL, gnat_node));
2113
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
2116 together here. */
2117 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
2118 Present (gnat_temp); gnat_temp = Next_Non_Pragma (gnat_temp))
2119 {
2120 gnu_expr = gnat_to_gnu (gnat_temp);
2121
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. */
2125 if (!gnu_else_ptr)
2126 add_stmt (gnu_expr);
2127 else
2128 *gnu_else_ptr = gnu_expr;
2129
2130 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
2131 }
2132
2133 /* If none of the exception handlers did anything, re-raise but do not
2134 defer abortion. */
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);
2138
2139 if (gnu_else_ptr)
2140 *gnu_else_ptr = gnu_expr;
2141 else
2142 add_stmt (gnu_expr);
2143
2144 /* End the binding level dedicated to the exception handlers and get the
2145 whole statement group. */
2146 pop_stack (&gnu_except_ptr_stack);
2147 gnat_poplevel ();
2148 gnu_handler = end_stmt_group ();
2149
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,
2154 gnu_jmpsave_decl),
2155 gnat_node);
2156 add_stmt (gnu_handler);
2157 gnu_handler = end_stmt_group ();
2158
2159 /* This block is now "if (setjmp) ... <handlers> else <block>". */
2160 gnu_result = build3 (COND_EXPR, void_type_node,
2161 (build_call_1_expr
2162 (setjmp_decl,
2163 build_unary_op (ADDR_EXPR, NULL_TREE,
2164 gnu_jmpbuf_decl))),
2165 gnu_handler, gnu_inner_block);
2166 }
2167 else if (gcc_zcx)
2168 {
2169 tree gnu_handlers;
2170
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 ();
2178
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);
2182 }
2183 else
2184 gnu_result = gnu_inner_block;
2185
2186 /* Now close our outer block, if we had to make one. */
2187 if (binding_for_block)
2188 {
2189 add_stmt (gnu_result);
2190 gnat_poplevel ();
2191 gnu_result = end_stmt_group ();
2192 }
2193
2194 return gnu_result;
2195 }
2196 \f
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. */
2200
2201 static tree
2202 Exception_Handler_to_gnu_sjlj (Node_Id gnat_node)
2203 {
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);
2210 Node_Id gnat_temp;
2211
2212 for (gnat_temp = First (Exception_Choices (gnat_node));
2213 gnat_temp; gnat_temp = Next (gnat_temp))
2214 {
2215 tree this_choice;
2216
2217 if (Nkind (gnat_temp) == N_Others_Choice)
2218 {
2219 if (All_Others (gnat_temp))
2220 this_choice = integer_one_node;
2221 else
2222 this_choice
2223 = build_binary_op
2224 (EQ_EXPR, integer_type_node,
2225 convert
2226 (integer_type_node,
2227 build_component_ref
2228 (build_unary_op
2229 (INDIRECT_REF, NULL_TREE,
2230 TREE_VALUE (gnu_except_ptr_stack)),
2231 get_identifier ("not_handled_by_others"), NULL_TREE,
2232 false)),
2233 integer_zero_node);
2234 }
2235
2236 else if (Nkind (gnat_temp) == N_Identifier
2237 || Nkind (gnat_temp) == N_Expanded_Name)
2238 {
2239 Entity_Id gnat_ex_id = Entity (gnat_temp);
2240 tree gnu_expr;
2241
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);
2246
2247 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, 0);
2248
2249 this_choice
2250 = build_binary_op
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)));
2254
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
2257 match. */
2258 if (Is_Non_Ada_Error (Entity (gnat_temp)))
2259 {
2260 tree gnu_comp
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);
2265
2266 this_choice
2267 = build_binary_op
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')),
2271 this_choice);
2272 }
2273 }
2274 else
2275 gcc_unreachable ();
2276
2277 gnu_choice = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
2278 gnu_choice, this_choice);
2279 }
2280
2281 return build3 (COND_EXPR, void_type_node, gnu_choice, gnu_body, NULL_TREE);
2282 }
2283 \f
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. */
2286
2287 static tree
2288 Exception_Handler_to_gnu_zcx (Node_Id gnat_node)
2289 {
2290 tree gnu_etypes_list = NULL_TREE;
2291 tree gnu_expr;
2292 tree gnu_etype;
2293 tree gnu_current_exc_ptr;
2294 tree gnu_incoming_exc_ptr;
2295 Node_Id gnat_temp;
2296
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.
2299
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".
2302
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
2305 currently. */
2306 for (gnat_temp = First (Exception_Choices (gnat_node));
2307 gnat_temp; gnat_temp = Next (gnat_temp))
2308 {
2309 if (Nkind (gnat_temp) == N_Others_Choice)
2310 {
2311 tree gnu_expr
2312 = All_Others (gnat_temp) ? all_others_decl : others_decl;
2313
2314 gnu_etype
2315 = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
2316 }
2317 else if (Nkind (gnat_temp) == N_Identifier
2318 || Nkind (gnat_temp) == N_Expanded_Name)
2319 {
2320 Entity_Id gnat_ex_id = Entity (gnat_temp);
2321
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);
2326
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);
2329
2330 /* The Non_Ada_Error case for VMS exceptions is handled
2331 by the personality routine. */
2332 }
2333 else
2334 gcc_unreachable ();
2335
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);
2344 }
2345
2346 start_stmt_group ();
2347 gnat_pushlevel ();
2348
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
2351 exit path.
2352
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.
2357
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.
2363
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,
2370 gnat_node);
2371
2372 add_stmt_with_node (build_call_1_expr (begin_handler_decl,
2373 gnu_incoming_exc_ptr),
2374 gnat_node);
2375 add_cleanup (build_call_1_expr (end_handler_decl, gnu_incoming_exc_ptr));
2376 add_stmt_list (Statements (gnat_node));
2377 gnat_poplevel ();
2378
2379 return build2 (CATCH_EXPR, void_type_node, gnu_etypes_list,
2380 end_stmt_group ());
2381 }
2382 \f
2383 /* Subroutine of gnat_to_gnu to generate code for an N_Compilation unit. */
2384
2385 static void
2386 Compilation_Unit_to_gnu (Node_Id gnat_node)
2387 {
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,
2398 gnat_unit_entity);
2399 struct elab_info *info;
2400
2401 push_stack (&gnu_elab_proc_stack, NULL_TREE, gnu_elab_proc_decl);
2402
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);
2406 cfun = 0;
2407
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)));
2413
2414 process_inlined_subprograms (gnat_node);
2415
2416 if (type_annotate_only)
2417 {
2418 elaborate_all_entities (gnat_node);
2419
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)
2423 return;
2424 }
2425
2426 process_decls (Declarations (Aux_Decls_Node (gnat_node)), Empty, Empty,
2427 true, true);
2428 add_stmt (gnat_to_gnu (Unit (gnat_node)));
2429
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)));
2433
2434 /* Save away what we've made so far and record this potential elaboration
2435 procedure. */
2436 info = (struct elab_info *) ggc_alloc (sizeof (struct elab_info));
2437 set_current_block_context (gnu_elab_proc_decl);
2438 gnat_poplevel ();
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;
2444
2445 /* Generate elaboration code for this unit, if necessary, and say whether
2446 we did or not. */
2447 pop_stack (&gnu_elab_proc_stack);
2448 }
2449 \f
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. */
2458
2459 tree
2460 gnat_to_gnu (Node_Id gnat_node)
2461 {
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;
2465 tree gnu_expr;
2466 tree gnu_lhs, gnu_rhs;
2467 Node_Id gnat_temp;
2468
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);
2472
2473 if (type_annotate_only
2474 && IN (Nkind (gnat_node), N_Statement_Other_Than_Procedure_Call))
2475 return alloc_stmt_list ();
2476
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));
2485
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))))
2500 {
2501 current_function_decl = TREE_VALUE (gnu_elab_proc_stack);
2502 start_stmt_group ();
2503 gnat_pushlevel ();
2504 went_into_elab_proc = true;
2505 }
2506
2507 switch (Nkind (gnat_node))
2508 {
2509 /********************************/
2510 /* Chapter 2: Lexical Elements: */
2511 /********************************/
2512
2513 case N_Identifier:
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);
2518 break;
2519
2520 case N_Integer_Literal:
2521 {
2522 tree gnu_type;
2523
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));
2527
2528 if (TREE_CODE (gnu_type) == RECORD_TYPE
2529 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2530 gnu_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2531
2532 gnu_result = UI_To_gnu (Intval (gnat_node), gnu_type);
2533
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
2538 we don't. */
2539 gcc_assert (!TREE_CONSTANT_OVERFLOW (gnu_result));
2540 }
2541 break;
2542
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)));
2552 else
2553 gnu_result
2554 = force_fit_type
2555 (build_int_cst
2556 (gnu_result_type, UI_To_CC (Char_Literal_Value (gnat_node))),
2557 false, false, false);
2558 break;
2559
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))
2564 {
2565 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2566 gnu_result = UI_To_gnu (Corresponding_Integer_Value (gnat_node),
2567 gnu_result_type);
2568 gcc_assert (!TREE_CONSTANT_OVERFLOW (gnu_result));
2569 }
2570
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))))
2574 gcc_unreachable ();
2575
2576 else
2577 {
2578 Ureal ur_realval = Realval (gnat_node);
2579
2580 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2581
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);
2587 else
2588 {
2589 if (!Is_Machine_Number (gnat_node))
2590 ur_realval
2591 = Machine (Base_Type (Underlying_Type (Etype (gnat_node))),
2592 ur_realval, Round_Even, gnat_node);
2593
2594 gnu_result
2595 = UI_To_gnu (Numerator (ur_realval), gnu_result_type);
2596
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)))
2602 gnu_result
2603 = build_binary_op (RDIV_EXPR,
2604 get_base_type (gnu_result_type),
2605 gnu_result,
2606 UI_To_gnu (Denominator (ur_realval),
2607 gnu_result_type));
2608 else
2609 {
2610 REAL_VALUE_TYPE tmp;
2611
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);
2616 }
2617 }
2618
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)))
2622 gnu_result
2623 = build_unary_op (NEGATE_EXPR, get_base_type (gnu_result_type),
2624 gnu_result);
2625 }
2626
2627 break;
2628
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)
2632 {
2633 String_Id gnat_string = Strval (gnat_node);
2634 int length = String_Length (gnat_string);
2635 char *string = (char *) alloca (length + 1);
2636 int i;
2637
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);
2642
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. */
2645 string[i] = 0;
2646
2647 gnu_result = build_string (length, string);
2648
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;
2652 }
2653 else
2654 {
2655 /* Build a list consisting of each character, then make
2656 the aggregate. */
2657 String_Id gnat_string = Strval (gnat_node);
2658 int length = String_Length (gnat_string);
2659 int i;
2660 tree gnu_list = NULL_TREE;
2661 tree gnu_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
2662
2663 for (i = 0; i < length; i++)
2664 {
2665 gnu_list
2666 = tree_cons (gnu_idx,
2667 build_int_cst (TREE_TYPE (gnu_result_type),
2668 Get_String_Char (gnat_string,
2669 i + 1)),
2670 gnu_list);
2671
2672 gnu_idx = int_const_binop (PLUS_EXPR, gnu_idx, integer_one_node,
2673 0);
2674 }
2675
2676 gnu_result
2677 = gnat_build_constructor (gnu_result_type, nreverse (gnu_list));
2678 }
2679 break;
2680
2681 case N_Pragma:
2682 gnu_result = Pragma_to_gnu (gnat_node);
2683 break;
2684
2685 /**************************************/
2686 /* Chapter 3: Declarations and Types: */
2687 /**************************************/
2688
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 ();
2697 break;
2698
2699 case N_Object_Declaration:
2700 case N_Exception_Declaration:
2701 gnat_temp = Defining_Entity (gnat_node);
2702 gnu_result = alloc_stmt_list ();
2703
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))))
2711 break;
2712
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))))
2718 {
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));
2722
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
2725 is frozen. */
2726 if (Present (Freeze_Node (gnat_temp)))
2727 {
2728 if ((Is_Public (gnat_temp) || global_bindings_p ())
2729 && !TREE_CONSTANT (gnu_expr))
2730 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);
2735 else
2736 gnu_expr = maybe_variable (gnu_expr);
2737
2738 save_gnu_tree (gnat_node, gnu_expr, true);
2739 }
2740 }
2741 else
2742 gnu_expr = NULL_TREE;
2743
2744 if (type_annotate_only && gnu_expr && TREE_CODE (gnu_expr) == ERROR_MARK)
2745 gnu_expr = NULL_TREE;
2746
2747 if (No (Freeze_Node (gnat_temp)))
2748 gnat_to_gnu_entity (gnat_temp, gnu_expr, 1);
2749 break;
2750
2751 case N_Object_Renaming_Declaration:
2752 gnat_temp = Defining_Entity (gnat_node);
2753
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)))))
2763 gnu_result
2764 = gnat_to_gnu_entity (gnat_temp,
2765 gnat_to_gnu (Renamed_Object (gnat_temp)), 1);
2766 else
2767 gnu_result = alloc_stmt_list ();
2768 break;
2769
2770 case N_Implicit_Label_Declaration:
2771 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
2772 gnu_result = alloc_stmt_list ();
2773 break;
2774
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 ();
2781 break;
2782
2783 /*************************************/
2784 /* Chapter 4: Names and Expressions: */
2785 /*************************************/
2786
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);
2791 break;
2792
2793 case N_Indexed_Component:
2794 {
2795 tree gnu_array_object = gnat_to_gnu (Prefix (gnat_node));
2796 tree gnu_type;
2797 int ndim;
2798 int i;
2799 Node_Id *gnat_expr_array;
2800
2801 gnu_array_object = maybe_implicit_deref (gnu_array_object);
2802 gnu_array_object = maybe_unconstrained_array (gnu_array_object);
2803
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)))
2807 gnu_array_object
2808 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_array_object))),
2809 gnu_array_object);
2810
2811 gnu_result = gnu_array_object;
2812
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))
2820 ;
2821
2822 gnat_expr_array = (Node_Id *) alloca (ndim * sizeof (Node_Id));
2823
2824 if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object)))
2825 for (i = ndim - 1, gnat_temp = First (Expressions (gnat_node));
2826 i >= 0;
2827 i--, gnat_temp = Next (gnat_temp))
2828 gnat_expr_array[i] = gnat_temp;
2829 else
2830 for (i = 0, gnat_temp = First (Expressions (gnat_node));
2831 i < ndim;
2832 i++, gnat_temp = Next (gnat_temp))
2833 gnat_expr_array[i] = gnat_temp;
2834
2835 for (i = 0, gnu_type = TREE_TYPE (gnu_array_object);
2836 i < ndim; i++, gnu_type = TREE_TYPE (gnu_type))
2837 {
2838 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
2839 gnat_temp = gnat_expr_array[i];
2840 gnu_expr = gnat_to_gnu (gnat_temp);
2841
2842 if (Do_Range_Check (gnat_temp))
2843 gnu_expr
2844 = emit_index_check
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))));
2848
2849 gnu_result = build_binary_op (ARRAY_REF, NULL_TREE,
2850 gnu_result, gnu_expr);
2851 }
2852 }
2853
2854 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2855 break;
2856
2857 case N_Slice:
2858 {
2859 tree gnu_type;
2860 Node_Id gnat_range_node = Discrete_Range (gnat_node);
2861
2862 gnu_result = gnat_to_gnu (Prefix (gnat_node));
2863 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2864
2865 /* Do any implicit dereferences of the prefix and do any needed
2866 range check. */
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))
2871 {
2872 /* Get the bounds of the slice. */
2873 tree gnu_index_type
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;
2878
2879 /* Check to see that the minimum slice value is in range */
2880 gnu_expr_l
2881 = emit_index_check
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))));
2885
2886 /* Check to see that the maximum slice value is in range */
2887 gnu_expr_h
2888 = emit_index_check
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))));
2892
2893 /* Derive a good type to convert everything too */
2894 gnu_expr_type = get_base_type (TREE_TYPE (gnu_expr_l));
2895
2896 /* Build a compound expression that does the range checks */
2897 gnu_expr
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));
2901
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 */
2905 gnu_expr
2906 = fold (build3 (COND_EXPR, gnu_expr_type,
2907 build_binary_op (GE_EXPR, gnu_expr_type,
2908 convert (gnu_expr_type,
2909 gnu_max_expr),
2910 convert (gnu_expr_type,
2911 gnu_min_expr)),
2912 gnu_expr, gnu_min_expr));
2913 }
2914 else
2915 gnu_expr = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
2916
2917 gnu_result = build_binary_op (ARRAY_RANGE_REF, gnu_result_type,
2918 gnu_result, gnu_expr);
2919 }
2920 break;
2921
2922 case N_Selected_Component:
2923 {
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));
2927 tree gnu_field;
2928
2929 while (IN (Ekind (gnat_pref_type), Incomplete_Or_Private_Kind)
2930 || IN (Ekind (gnat_pref_type), Access_Kind))
2931 {
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);
2936 }
2937
2938 gnu_prefix = maybe_implicit_deref (gnu_prefix);
2939
2940 /* For discriminant references in tagged types always substitute the
2941 corresponding discriminant as the actual selected component. */
2942
2943 if (Is_Tagged_Type (gnat_pref_type))
2944 while (Present (Corresponding_Discriminant (gnat_field)))
2945 gnat_field = Corresponding_Discriminant (gnat_field);
2946
2947 /* For discriminant references of untagged types always substitute the
2948 corresponding stored discriminant. */
2949
2950 else if (Present (Corresponding_Discriminant (gnat_field)))
2951 gnat_field = Original_Record_Component (gnat_field);
2952
2953 /* Handle extracting the real or imaginary part of a complex.
2954 The real part is the first field and the imaginary the last. */
2955
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);
2960 else
2961 {
2962 gnu_field = gnat_to_gnu_field_decl (gnat_field);
2963
2964 /* If there are discriminants, the prefix might be
2965 evaluated more than once, which is a problem if it has
2966 side-effects. */
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);
2972
2973 gnu_result
2974 = build_component_ref (gnu_prefix, NULL_TREE, gnu_field,
2975 (Nkind (Parent (gnat_node))
2976 == N_Attribute_Reference));
2977 }
2978
2979 gcc_assert (gnu_result);
2980 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2981 }
2982 break;
2983
2984 case N_Attribute_Reference:
2985 {
2986 /* The attribute designator (like an enumeration value). */
2987 int attribute = Get_Attribute_Id (Attribute_Name (gnat_node));
2988
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,
2998 gnat_node));
2999
3000 gnu_result = Attribute_to_gnu (gnat_node, &gnu_result_type, attribute);
3001 }
3002 break;
3003
3004 case N_Reference:
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));
3009 break;
3010
3011 case N_Aggregate:
3012 case N_Extension_Aggregate:
3013 {
3014 tree gnu_aggr_type;
3015
3016 /* ??? It is wrong to evaluate the type now, but there doesn't
3017 seem to be any other practical way of doing it. */
3018
3019 gcc_assert (!Expansion_Delayed (gnat_node));
3020
3021 gnu_aggr_type = gnu_result_type
3022 = get_unpadded_type (Etype (gnat_node));
3023
3024 if (TREE_CODE (gnu_result_type) == RECORD_TYPE
3025 && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type))
3026 gnu_aggr_type
3027 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_result_type)));
3028
3029 if (Null_Record_Present (gnat_node))
3030 gnu_result = gnat_build_constructor (gnu_aggr_type, NULL_TREE);
3031
3032 else if (TREE_CODE (gnu_aggr_type) == UNION_TYPE
3033 && TYPE_UNCHECKED_UNION_P (gnu_aggr_type))
3034 {
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. */
3038 Node_Id gnat_assoc
3039 = Next (First (Component_Associations (gnat_node)));
3040 Entity_Id gnat_field = Entity (First (Choices (gnat_assoc)));
3041 tree gnu_field_type
3042 = TREE_TYPE (gnat_to_gnu_entity (gnat_field, NULL_TREE, 0));
3043
3044 gnu_result = convert (gnu_field_type,
3045 gnat_to_gnu (Expression (gnat_assoc)));
3046 }
3047 else if (TREE_CODE (gnu_aggr_type) == RECORD_TYPE
3048 || TREE_CODE (gnu_aggr_type) == UNION_TYPE)
3049 gnu_result
3050 = assoc_to_constructor (First (Component_Associations (gnat_node)),
3051 gnu_aggr_type);
3052 else if (TREE_CODE (gnu_aggr_type) == ARRAY_TYPE)
3053 gnu_result = pos_to_constructor (First (Expressions (gnat_node)),
3054 gnu_aggr_type,
3055 Component_Type (Etype (gnat_node)));
3056 else if (TREE_CODE (gnu_aggr_type) == COMPLEX_TYPE)
3057 gnu_result
3058 = build_binary_op
3059 (COMPLEX_EXPR, gnu_aggr_type,
3060 gnat_to_gnu (Expression (First
3061 (Component_Associations (gnat_node)))),
3062 gnat_to_gnu (Expression
3063 (Next
3064 (First (Component_Associations (gnat_node))))));
3065 else
3066 gcc_unreachable ();
3067
3068 gnu_result = convert (gnu_result_type, gnu_result);
3069 }
3070 break;
3071
3072 case N_Null:
3073 gnu_result = null_pointer_node;
3074 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3075 break;
3076
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));
3082
3083 gnu_result
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));
3089 break;
3090
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));
3094
3095 /* If the result is a pointer type, see if we are improperly
3096 converting to a stricter alignment. */
3097
3098 if (STRICT_ALIGNMENT && POINTER_TYPE_P (gnu_result_type)
3099 && IN (Ekind (Etype (gnat_node)), Access_Kind))
3100 {
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);
3104
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);
3110 }
3111
3112 gnu_result = unchecked_convert (gnu_result_type, gnu_result,
3113 No_Truncation (gnat_node));
3114 break;
3115
3116 case N_In:
3117 case N_Not_In:
3118 {
3119 tree gnu_object = gnat_to_gnu (Left_Opnd (gnat_node));
3120 Node_Id gnat_range = Right_Opnd (gnat_node);
3121 tree gnu_low;
3122 tree gnu_high;
3123
3124 /* GNAT_RANGE is either an N_Range node or an identifier
3125 denoting a subtype. */
3126 if (Nkind (gnat_range) == N_Range)
3127 {
3128 gnu_low = gnat_to_gnu (Low_Bound (gnat_range));
3129 gnu_high = gnat_to_gnu (High_Bound (gnat_range));
3130 }
3131 else if (Nkind (gnat_range) == N_Identifier
3132 || Nkind (gnat_range) == N_Expanded_Name)
3133 {
3134 tree gnu_range_type = get_unpadded_type (Entity (gnat_range));
3135
3136 gnu_low = TYPE_MIN_VALUE (gnu_range_type);
3137 gnu_high = TYPE_MAX_VALUE (gnu_range_type);
3138 }
3139 else
3140 gcc_unreachable ();
3141
3142 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3143
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);
3150 else
3151 {
3152 gnu_object = protect_multiple_eval (gnu_object);
3153 gnu_result
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));
3159 }
3160
3161 if (Nkind (gnat_node) == N_Not_In)
3162 gnu_result = invert_truthvalue (gnu_result);
3163 }
3164 break;
3165
3166 case N_Op_Divide:
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)
3171 ? RDIV_EXPR
3172 : (Rounded_Result (gnat_node)
3173 ? ROUND_DIV_EXPR : TRUNC_DIV_EXPR),
3174 gnu_result_type, gnu_lhs, gnu_rhs);
3175 break;
3176
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
3180 set up. */
3181 if (IN (Ekind (Underlying_Type (Etype (gnat_node))),
3182 Modular_Integer_Kind))
3183 {
3184 enum tree_code code
3185 = (Nkind (gnat_node) == N_Op_Or ? BIT_IOR_EXPR
3186 : Nkind (gnat_node) == N_Op_And ? BIT_AND_EXPR
3187 : BIT_XOR_EXPR);
3188
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,
3193 gnu_lhs, gnu_rhs);
3194 break;
3195 }
3196
3197 /* ... fall through ... */
3198
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:
3209 {
3210 enum tree_code code = gnu_codes[Nkind (gnat_node)];
3211 tree gnu_type;
3212
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));
3216
3217 /* If this is a comparison operator, convert any references to
3218 an unconstrained array value into a reference to the
3219 actual array. */
3220 if (TREE_CODE_CLASS (code) == tcc_comparison)
3221 {
3222 gnu_lhs = maybe_unconstrained_array (gnu_lhs);
3223 gnu_rhs = maybe_unconstrained_array (gnu_rhs);
3224 }
3225
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))));
3232
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))
3237 {
3238 tree gnu_count_type = get_base_type (TREE_TYPE (gnu_rhs));
3239 tree gnu_max_shift
3240 = convert (gnu_count_type, TYPE_SIZE (gnu_type));
3241
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)
3247 gnu_rhs
3248 = build_binary_op
3249 (MIN_EXPR, gnu_count_type,
3250 build_binary_op (MINUS_EXPR,
3251 gnu_count_type,
3252 gnu_max_shift,
3253 convert (gnu_count_type,
3254 integer_one_node)),
3255 gnu_rhs);
3256 }
3257
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);
3266
3267 if (gnu_type != gnu_result_type)
3268 {
3269 gnu_lhs = convert (gnu_type, gnu_lhs);
3270 gnu_rhs = convert (gnu_type, gnu_rhs);
3271 }
3272
3273 gnu_result = build_binary_op (code, gnu_type, gnu_lhs, gnu_rhs);
3274
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))
3281 gnu_result
3282 = build_cond_expr
3283 (gnu_type,
3284 build_binary_op (GE_EXPR, integer_type_node,
3285 gnu_rhs,
3286 convert (TREE_TYPE (gnu_rhs),
3287 TYPE_SIZE (gnu_type))),
3288 convert (gnu_type, integer_zero_node),
3289 gnu_result);
3290 }
3291 break;
3292
3293 case N_Conditional_Expression:
3294 {
3295 tree gnu_cond = gnat_to_gnu (First (Expressions (gnat_node)));
3296 tree gnu_true = gnat_to_gnu (Next (First (Expressions (gnat_node))));
3297 tree gnu_false
3298 = gnat_to_gnu (Next (Next (First (Expressions (gnat_node)))));
3299
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);
3304 }
3305 break;
3306
3307 case N_Op_Plus:
3308 gnu_result = gnat_to_gnu (Right_Opnd (gnat_node));
3309 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3310 break;
3311
3312 case N_Op_Not:
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))
3317 {
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,
3321 gnu_expr);
3322 break;
3323 }
3324
3325 /* ... fall through ... */
3326
3327 case N_Op_Minus: case N_Op_Abs:
3328 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
3329
3330 if (Ekind (Etype (gnat_node)) != E_Private_Type)
3331 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3332 else
3333 gnu_result_type = get_unpadded_type (Base_Type
3334 (Full_View (Etype (gnat_node))));
3335
3336 gnu_result = build_unary_op (gnu_codes[Nkind (gnat_node)],
3337 gnu_result_type, gnu_expr);
3338 break;
3339
3340 case N_Allocator:
3341 {
3342 tree gnu_init = 0;
3343 tree gnu_type;
3344 bool ignore_init_type = false;
3345
3346 gnat_temp = Expression (gnat_node);
3347
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)
3356 {
3357 Entity_Id gnat_desig_type
3358 = Designated_Type (Underlying_Type (Etype (gnat_node)));
3359
3360 ignore_init_type = Has_Constrained_Partial_View (gnat_desig_type);
3361 gnu_init = gnat_to_gnu (Expression (gnat_temp));
3362
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);
3366
3367 if (Is_Elementary_Type (gnat_desig_type)
3368 || Is_Constrained (gnat_desig_type))
3369 {
3370 gnu_type = gnat_to_gnu_type (gnat_desig_type);
3371 gnu_init = convert (gnu_type, gnu_init);
3372 }
3373 else
3374 {
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);
3378
3379 gnu_init = convert (gnu_type, gnu_init);
3380 }
3381 }
3382 else
3383 gcc_unreachable ();
3384
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,
3389 ignore_init_type);
3390 }
3391 break;
3392
3393 /***************************/
3394 /* Chapter 5: Statements: */
3395 /***************************/
3396
3397 case N_Label:
3398 gnu_result = build1 (LABEL_EXPR, void_type_node,
3399 gnat_to_gnu (Identifier (gnat_node)));
3400 break;
3401
3402 case N_Null_Statement:
3403 gnu_result = alloc_stmt_list ();
3404 break;
3405
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)));
3412
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);
3422 else
3423 {
3424 gnu_rhs
3425 = maybe_unconstrained_array (gnat_to_gnu (Expression (gnat_node)));
3426
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)));
3430
3431 gnu_result
3432 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_lhs, gnu_rhs);
3433 }
3434 break;
3435
3436 case N_If_Statement:
3437 {
3438 tree *gnu_else_ptr; /* Point to put next "else if" or "else". */
3439
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);
3448
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))
3455 {
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);
3465 }
3466
3467 *gnu_else_ptr = build_stmt_group (Else_Statements (gnat_node), false);
3468 }
3469 break;
3470
3471 case N_Case_Statement:
3472 gnu_result = Case_Statement_to_gnu (gnat_node);
3473 break;
3474
3475 case N_Loop_Statement:
3476 gnu_result = Loop_Statement_to_gnu (gnat_node);
3477 break;
3478
3479 case N_Block_Statement:
3480 start_stmt_group ();
3481 gnat_pushlevel ();
3482 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
3483 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
3484 gnat_poplevel ();
3485 gnu_result = end_stmt_group ();
3486
3487 if (Present (Identifier (gnat_node)))
3488 mark_out_of_scope (Entity (Identifier (gnat_node)));
3489 break;
3490
3491 case N_Exit_Statement:
3492 gnu_result
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)));
3499 break;
3500
3501 case N_Return_Statement:
3502 {
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. */
3508 tree gnu_lhs;
3509 /* Avoid passing error_mark_node to RETURN_EXPR. */
3510 gnu_result = NULL_TREE;
3511
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.
3516
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.
3520
3521 But if we have a return label defined, convert this into
3522 a branch to that label. */
3523
3524 if (TREE_VALUE (gnu_return_label_stack))
3525 {
3526 gnu_result = build1 (GOTO_EXPR, void_type_node,
3527 TREE_VALUE (gnu_return_label_stack));
3528 break;
3529 }
3530
3531 else if (TYPE_CI_CO_LIST (gnu_subprog_type))
3532 {
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));
3536 else
3537 gnu_ret_val
3538 = gnat_build_constructor (TREE_TYPE (gnu_subprog_type),
3539 TYPE_CI_CO_LIST (gnu_subprog_type));
3540 }
3541
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. */
3546
3547 else if (Present (Expression (gnat_node)))
3548 {
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)
3553 {
3554 gnu_lhs
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);
3559 }
3560 else
3561 {
3562 gnu_ret_val = gnat_to_gnu (Expression (gnat_node));
3563
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. */
3567 gnu_lhs
3568 = build_unary_op (INDIRECT_REF, TREE_TYPE (gnu_ret_val),
3569 DECL_ARGUMENTS (current_function_decl));
3570 else
3571 gnu_lhs = DECL_RESULT (current_function_decl);
3572
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)))
3578 == RECORD_TYPE)
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);
3584
3585 if (TYPE_RETURNS_BY_REF_P (gnu_subprog_type)
3586 || By_Ref (gnat_node))
3587 gnu_ret_val
3588 = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_ret_val);
3589
3590 else if (TYPE_RETURNS_UNCONSTRAINED_P (gnu_subprog_type))
3591 {
3592 gnu_ret_val = maybe_unconstrained_array (gnu_ret_val);
3593
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))
3600 gnu_ret_val
3601 = build_allocator (TREE_TYPE (gnu_ret_val),
3602 gnu_ret_val,
3603 TREE_TYPE (gnu_subprog_type),
3604 0, -1, gnat_node, false);
3605 else
3606 gnu_ret_val
3607 = build_allocator (TREE_TYPE (gnu_ret_val),
3608 gnu_ret_val,
3609 TREE_TYPE (gnu_subprog_type),
3610 Procedure_To_Call (gnat_node),
3611 Storage_Pool (gnat_node),
3612 gnat_node, false);
3613 }
3614 }
3615 }
3616
3617 if (gnu_ret_val)
3618 gnu_result = build2 (MODIFY_EXPR, TREE_TYPE (gnu_ret_val),
3619 gnu_lhs, gnu_ret_val);
3620
3621 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type))
3622 {
3623 add_stmt_with_node (gnu_result, gnat_node);
3624 gnu_result = NULL_TREE;
3625 }
3626
3627 gnu_result = build1 (RETURN_EXPR, void_type_node, gnu_result);
3628 }
3629 break;
3630
3631 case N_Goto_Statement:
3632 gnu_result = build1 (GOTO_EXPR, void_type_node,
3633 gnat_to_gnu (Name (gnat_node)));
3634 break;
3635
3636 /****************************/
3637 /* Chapter 6: Subprograms: */
3638 /****************************/
3639
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
3644 node here. */
3645
3646 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
3647 gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node)),
3648 NULL_TREE, 1);
3649 gnu_result = alloc_stmt_list ();
3650 break;
3651
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). */
3656 for (gnat_temp
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);
3663
3664 gnu_result = alloc_stmt_list ();
3665 break;
3666
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));
3672 break;
3673
3674 case N_Subprogram_Body:
3675 Subprogram_Body_to_gnu (gnat_node);
3676 gnu_result = alloc_stmt_list ();
3677 break;
3678
3679 case N_Function_Call:
3680 case N_Procedure_Call_Statement:
3681 gnu_result = call_to_gnu (gnat_node, &gnu_result_type, NULL_TREE);
3682 break;
3683
3684 /*************************/
3685 /* Chapter 7: Packages: */
3686 /*************************/
3687
3688 case N_Package_Declaration:
3689 gnu_result = gnat_to_gnu (Specification (gnat_node));
3690 break;
3691
3692 case N_Package_Specification:
3693
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 ();
3698 break;
3699
3700 case N_Package_Body:
3701
3702 /* If this is the body of a generic package - do nothing */
3703 if (Ekind (Corresponding_Spec (gnat_node)) == E_Generic_Package)
3704 {
3705 gnu_result = alloc_stmt_list ();
3706 break;
3707 }
3708
3709 start_stmt_group ();
3710 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
3711
3712 if (Present (Handled_Statement_Sequence (gnat_node)))
3713 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
3714
3715 gnu_result = end_stmt_group ();
3716 break;
3717
3718 /*********************************/
3719 /* Chapter 8: Visibility Rules: */
3720 /*********************************/
3721
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 ();
3726 break;
3727
3728 /***********************/
3729 /* Chapter 9: Tasks: */
3730 /***********************/
3731
3732 case N_Protected_Type_Declaration:
3733 gnu_result = alloc_stmt_list ();
3734 break;
3735
3736 case N_Single_Task_Declaration:
3737 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
3738 gnu_result = alloc_stmt_list ();
3739 break;
3740
3741 /***********************************************************/
3742 /* Chapter 10: Program Structure and Compilation Issues: */
3743 /***********************************************************/
3744
3745 case N_Compilation_Unit:
3746
3747 /* This is not called for the main unit, which is handled in function
3748 gigi above. */
3749 start_stmt_group ();
3750 gnat_pushlevel ();
3751
3752 Compilation_Unit_to_gnu (gnat_node);
3753 gnu_result = alloc_stmt_list ();
3754 break;
3755
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)));
3762 break;
3763
3764 case N_Subunit:
3765 gnu_result = gnat_to_gnu (Proper_Body (gnat_node));
3766 break;
3767
3768 /***************************/
3769 /* Chapter 11: Exceptions: */
3770 /***************************/
3771
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 ());
3781
3782 gnu_result = Handled_Sequence_Of_Statements_to_gnu (gnat_node);
3783 break;
3784
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);
3790 else
3791 gcc_unreachable ();
3792
3793 break;
3794
3795 /*******************************/
3796 /* Chapter 12: Generic Units: */
3797 /*******************************/
3798
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 ();
3810 break;
3811
3812 /***************************************************/
3813 /* Chapter 13: Representation Clauses and */
3814 /* Implementation-Dependent Features: */
3815 /***************************************************/
3816
3817 case N_Attribute_Definition_Clause:
3818
3819 gnu_result = alloc_stmt_list ();
3820
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)))))
3826 break;
3827
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);
3833 break;
3834
3835 case N_Enumeration_Representation_Clause:
3836 case N_Record_Representation_Clause:
3837 case N_At_Clause:
3838 /* We do nothing with these. SEM puts the information elsewhere. */
3839 gnu_result = alloc_stmt_list ();
3840 break;
3841
3842 case N_Code_Statement:
3843 if (!type_annotate_only)
3844 {
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;
3848 char *clobber;
3849
3850 /* First process inputs, then outputs, then clobbers. */
3851 Setup_Asm_Inputs (gnat_node);
3852 while (Present (gnat_temp = Asm_Input_Value ()))
3853 {
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 ()));
3857
3858 gnu_input_list
3859 = tree_cons (gnu_constr, gnu_value, gnu_input_list);
3860 Next_Asm_Input ();
3861 }
3862
3863 Setup_Asm_Outputs (gnat_node);
3864 while (Present (gnat_temp = Asm_Output_Variable ()))
3865 {
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 ()));
3869
3870 gnu_output_list
3871 = tree_cons (gnu_constr, gnu_value, gnu_output_list);
3872 Next_Asm_Output ();
3873 }
3874
3875 Clobber_Setup (gnat_node);
3876 while ((clobber = Clobber_Get_Next ()))
3877 gnu_clobber_list
3878 = tree_cons (NULL_TREE,
3879 build_string (strlen (clobber) + 1, clobber),
3880 gnu_clobber_list);
3881
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);
3888 }
3889 else
3890 gnu_result = alloc_stmt_list ();
3891
3892 break;
3893
3894 /***************************************************/
3895 /* Added Nodes */
3896 /***************************************************/
3897
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 ();
3903 break;
3904
3905 case N_Itype_Reference:
3906 if (!present_gnu_tree (Itype (gnat_node)))
3907 process_type (Itype (gnat_node));
3908
3909 gnu_result = alloc_stmt_list ();
3910 break;
3911
3912 case N_Free_Statement:
3913 if (!type_annotate_only)
3914 {
3915 tree gnu_ptr = gnat_to_gnu (Expression (gnat_node));
3916 tree gnu_obj_type;
3917 tree gnu_obj_size;
3918 int align;
3919
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,
3927 gnu_ptr));
3928
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)))
3934 gnu_ptr
3935 = convert (build_pointer_type
3936 (TYPE_OBJECT_RECORD_TYPE
3937 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
3938 gnu_ptr);
3939
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);
3943
3944 if (TREE_CODE (gnu_obj_type) == RECORD_TYPE
3945 && TYPE_CONTAINS_TEMPLATE_P (gnu_obj_type))
3946 {
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));
3952
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);
3956 }
3957
3958 gnu_result = build_call_alloc_dealloc (gnu_ptr, gnu_obj_size, align,
3959 Procedure_To_Call (gnat_node),
3960 Storage_Pool (gnat_node),
3961 gnat_node);
3962 }
3963 break;
3964
3965 case N_Raise_Constraint_Error:
3966 case N_Raise_Program_Error:
3967 case N_Raise_Storage_Error:
3968 if (type_annotate_only)
3969 {
3970 gnu_result = alloc_stmt_list ();
3971 break;
3972 }
3973
3974 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3975 gnu_result = build_call_raise (UI_To_Int (Reason (gnat_node)));
3976
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
3979 is one. */
3980 if (TREE_CODE (gnu_result_type) == VOID_TYPE)
3981 {
3982 annotate_with_node (gnu_result, gnat_node);
3983
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 ());
3988 }
3989 else
3990 gnu_result = build1 (NULL_EXPR, gnu_result_type, gnu_result);
3991 break;
3992
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. */
3999 {
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));
4002
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)))))
4010 {
4011 post_error_ne
4012 ("?possible aliasing problem for type&",
4013 gnat_node, Target_Type (gnat_node));
4014 post_error
4015 ("\\?use -fno-strict-aliasing switch for references",
4016 gnat_node);
4017 post_error_ne
4018 ("\\?or use `pragma No_Strict_Aliasing (&);`",
4019 gnat_node, Target_Type (gnat_node));
4020 }
4021
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))
4025 {
4026 tree array_type
4027 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_target_type)));
4028
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))))
4033 {
4034 post_error_ne
4035 ("?possible aliasing problem for type&",
4036 gnat_node, Target_Type (gnat_node));
4037 post_error
4038 ("\\?use -fno-strict-aliasing switch for references",
4039 gnat_node);
4040 }
4041 }
4042 }
4043 gnu_result = alloc_stmt_list ();
4044 break;
4045
4046 case N_Raise_Statement:
4047 case N_Function_Specification:
4048 case N_Procedure_Specification:
4049 case N_Op_Concat:
4050 case N_Component_Association:
4051 case N_Task_Body:
4052 default:
4053 gcc_assert (type_annotate_only);
4054 gnu_result = alloc_stmt_list ();
4055 }
4056
4057 /* If we pushed our level as part of processing the elaboration routine,
4058 pop it back now. */
4059 if (went_into_elab_proc)
4060 {
4061 add_stmt (gnu_result);
4062 gnat_poplevel ();
4063 gnu_result = end_stmt_group ();
4064 current_function_decl = NULL_TREE;
4065 }
4066
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);
4072
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)
4076 return gnu_result;
4077
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))
4081 {
4082 post_error ("Constraint_Error will be raised at run-time?", gnat_node);
4083
4084 gnu_result
4085 = build1 (NULL_EXPR, gnu_result_type,
4086 build_call_raise (CE_Overflow_Check_Failed));
4087 }
4088
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);
4096
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
4116 type. */
4117
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)))
4133 != INTEGER_CST))
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))))
4140 {
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))),
4151 gnu_result);
4152 }
4153
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)))
4167 == MODE_INT)))
4168 {
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))),
4173 gnu_result);
4174 }
4175
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);
4181
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);
4187
4188 return gnu_result;
4189 }
4190 \f
4191 /* Record the current code position in GNAT_NODE. */
4192
4193 static void
4194 record_code_position (Node_Id gnat_node)
4195 {
4196 tree stmt_stmt = build1 (STMT_STMT, void_type_node, NULL_TREE);
4197
4198 add_stmt_with_node (stmt_stmt, gnat_node);
4199 save_gnu_tree (gnat_node, stmt_stmt, true);
4200 }
4201
4202 /* Insert the code for GNAT_NODE at the position saved for that node. */
4203
4204 static void
4205 insert_code_for (Node_Id gnat_node)
4206 {
4207 STMT_STMT_STMT (get_gnu_tree (gnat_node)) = gnat_to_gnu (gnat_node);
4208 save_gnu_tree (gnat_node, NULL_TREE, true);
4209 }
4210 \f
4211 /* Start a new statement group chained to the previous group. */
4212
4213 static void
4214 start_stmt_group ()
4215 {
4216 struct stmt_group *group = stmt_group_free_list;
4217
4218 /* First see if we can get one from the free list. */
4219 if (group)
4220 stmt_group_free_list = group->previous;
4221 else
4222 group = (struct stmt_group *) ggc_alloc (sizeof (struct stmt_group));
4223
4224 group->previous = current_stmt_group;
4225 group->stmt_list = group->block = group->cleanups = NULL_TREE;
4226 current_stmt_group = group;
4227 }
4228
4229 /* Add GNU_STMT to the current statement group. */
4230
4231 void
4232 add_stmt (tree gnu_stmt)
4233 {
4234 append_to_statement_list (gnu_stmt, &current_stmt_group->stmt_list);
4235
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);
4240
4241 }
4242
4243 /* Similar, but set the location of GNU_STMT to that of GNAT_NODE. */
4244
4245 void
4246 add_stmt_with_node (tree gnu_stmt, Node_Id gnat_node)
4247 {
4248 if (Present (gnat_node))
4249 annotate_with_node (gnu_stmt, gnat_node);
4250 add_stmt (gnu_stmt);
4251 }
4252
4253 /* Add a declaration statement for GNU_DECL to the current statement group.
4254 Get SLOC from Entity_Id. */
4255
4256 void
4257 add_decl_expr (tree gnu_decl, Entity_Id gnat_entity)
4258 {
4259 tree gnu_stmt;
4260
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))
4268 return;
4269
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);
4277 else
4278 {
4279 walk_tree (&gnu_stmt, mark_visited, NULL, NULL);
4280 if (TREE_CODE (gnu_decl) == VAR_DECL
4281 || TREE_CODE (gnu_decl) == CONST_DECL)
4282 {
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);
4286 }
4287 }
4288
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))
4292 {
4293 tree gnu_init = DECL_INITIAL (gnu_decl);
4294 tree gnu_lhs = NULL_TREE;
4295
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)))
4301 gnu_lhs
4302 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_decl))), gnu_decl);
4303
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)))
4310 gnu_lhs = gnu_decl;
4311
4312 if (gnu_lhs)
4313 {
4314 tree gnu_assign_stmt
4315 = build_binary_op (MODIFY_EXPR, NULL_TREE,
4316 gnu_lhs, DECL_INITIAL (gnu_decl));
4317
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);
4323 }
4324 }
4325 }
4326
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
4330 subprogram. */
4331
4332 static tree
4333 mark_visited (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
4334 {
4335 if (TREE_VISITED (*tp))
4336 *walk_subtrees = 0;
4337
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;
4342
4343 if (TYPE_P (*tp))
4344 TYPE_SIZES_GIMPLIFIED (*tp) = 1;
4345
4346 return NULL_TREE;
4347 }
4348
4349 /* Likewise, but to mark as unvisited. */
4350
4351 static tree
4352 mark_unvisited (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
4353 void *data ATTRIBUTE_UNUSED)
4354 {
4355 TREE_VISITED (*tp) = 0;
4356
4357 return NULL_TREE;
4358 }
4359
4360 /* Add GNU_CLEANUP, a cleanup action, to the current code group. */
4361
4362 static void
4363 add_cleanup (tree gnu_cleanup)
4364 {
4365 append_to_statement_list (gnu_cleanup, &current_stmt_group->cleanups);
4366 }
4367
4368 /* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */
4369
4370 void
4371 set_block_for_group (tree gnu_block)
4372 {
4373 gcc_assert (!current_stmt_group->block);
4374 current_stmt_group->block = gnu_block;
4375 }
4376
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. */
4380
4381 static tree
4382 end_stmt_group ()
4383 {
4384 struct stmt_group *group = current_stmt_group;
4385 tree gnu_retval = group->stmt_list;
4386
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 ();
4393
4394 if (group->cleanups)
4395 gnu_retval = build2 (TRY_FINALLY_EXPR, void_type_node, gnu_retval,
4396 group->cleanups);
4397
4398 if (current_stmt_group->block)
4399 gnu_retval = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (group->block),
4400 gnu_retval, group->block);
4401
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;
4406
4407 return gnu_retval;
4408 }
4409
4410 /* Add a list of statements from GNAT_LIST, a possibly-empty list of
4411 statements.*/
4412
4413 static void
4414 add_stmt_list (List_Id gnat_list)
4415 {
4416 Node_Id gnat_node;
4417
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));
4422 }
4423
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. */
4426
4427 static tree
4428 build_stmt_group (List_Id gnat_list, bool binding_p)
4429 {
4430 start_stmt_group ();
4431 if (binding_p)
4432 gnat_pushlevel ();
4433
4434 add_stmt_list (gnat_list);
4435 if (binding_p)
4436 gnat_poplevel ();
4437
4438 return end_stmt_group ();
4439 }
4440 \f
4441 /* Push and pop routines for stacks. We keep a free list around so we
4442 don't waste tree nodes. */
4443
4444 static void
4445 push_stack (tree *gnu_stack_ptr, tree gnu_purpose, tree gnu_value)
4446 {
4447 tree gnu_node = gnu_stack_free_list;
4448
4449 if (gnu_node)
4450 {
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;
4455 }
4456 else
4457 gnu_node = tree_cons (gnu_purpose, gnu_value, *gnu_stack_ptr);
4458
4459 *gnu_stack_ptr = gnu_node;
4460 }
4461
4462 static void
4463 pop_stack (tree *gnu_stack_ptr)
4464 {
4465 tree gnu_node = *gnu_stack_ptr;
4466
4467 *gnu_stack_ptr = TREE_CHAIN (gnu_node);
4468 TREE_CHAIN (gnu_node) = gnu_stack_free_list;
4469 gnu_stack_free_list = gnu_node;
4470 }
4471 \f
4472 /* GNU_STMT is a statement. We generate code for that statement. */
4473
4474 void
4475 gnat_expand_stmt (tree gnu_stmt)
4476 {
4477 #if 0
4478 tree gnu_elmt, gnu_elmt_2;
4479 #endif
4480
4481 switch (TREE_CODE (gnu_stmt))
4482 {
4483 #if 0
4484 case USE_STMT:
4485 /* First write a volatile ASM_INPUT to prevent anything from being
4486 moved. */
4487 gnu_elmt = gen_rtx_ASM_INPUT (VOIDmode, "");
4488 MEM_VOLATILE_P (gnu_elmt) = 1;
4489 emit_insn (gnu_elmt);
4490
4491 gnu_elmt = expand_expr (TREE_OPERAND (gnu_stmt, 0), NULL_RTX, VOIDmode,
4492 modifier);
4493 emit_insn (gen_rtx_USE (VOIDmode, ));
4494 return target;
4495 #endif
4496
4497 default:
4498 gcc_unreachable ();
4499 }
4500 }
4501 \f
4502 /* Generate GIMPLE in place for the expression at *EXPR_P. */
4503
4504 int
4505 gnat_gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p ATTRIBUTE_UNUSED)
4506 {
4507 tree expr = *expr_p;
4508
4509 if (IS_ADA_STMT (expr))
4510 return gnat_gimplify_stmt (expr_p);
4511
4512 switch (TREE_CODE (expr))
4513 {
4514 case NULL_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
4517 dereference it. */
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));
4522 else
4523 {
4524 *expr_p = create_tmp_var (TREE_TYPE (expr), NULL);
4525 TREE_NO_WARNING (*expr_p) = 1;
4526 }
4527
4528 append_to_statement_list (TREE_OPERAND (expr, 0), pre_p);
4529 return GS_OK;
4530
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);
4535 return GS_OK;
4536
4537 case ADDR_EXPR:
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)))
4544 {
4545 tree new_var
4546 = create_tmp_var (TREE_TYPE (TREE_OPERAND (expr, 0)), "C");
4547
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);
4552
4553 TREE_OPERAND (expr, 0) = new_var;
4554 recompute_tree_invarant_for_addr_expr (expr);
4555 return GS_ALL_DONE;
4556 }
4557 return GS_UNHANDLED;
4558
4559 case COMPONENT_REF:
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)))
4564 {
4565 TREE_OPERAND (*expr_p, 1)
4566 = DECL_ORIGINAL_FIELD (TREE_OPERAND (*expr_p, 1));
4567 return GS_OK;
4568 }
4569
4570 /* ... fall through ... */
4571
4572 default:
4573 return GS_UNHANDLED;
4574 }
4575 }
4576
4577 /* Generate GIMPLE in place for the statement at *STMT_P. */
4578
4579 static enum gimplify_status
4580 gnat_gimplify_stmt (tree *stmt_p)
4581 {
4582 tree stmt = *stmt_p;
4583
4584 switch (TREE_CODE (stmt))
4585 {
4586 case STMT_STMT:
4587 *stmt_p = STMT_STMT_STMT (stmt);
4588 return GS_OK;
4589
4590 case USE_STMT:
4591 *stmt_p = NULL_TREE;
4592 return GS_ALL_DONE;
4593
4594 case LOOP_STMT:
4595 {
4596 tree gnu_start_label = create_artificial_label ();
4597 tree gnu_end_label = LOOP_STMT_LABEL (stmt);
4598
4599 /* Set to emit the statements of the loop. */
4600 *stmt_p = NULL_TREE;
4601
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,
4608 gnu_start_label),
4609 stmt_p);
4610
4611 if (LOOP_STMT_TOP_COND (stmt))
4612 append_to_statement_list (build3 (COND_EXPR, void_type_node,
4613 LOOP_STMT_TOP_COND (stmt),
4614 alloc_stmt_list (),
4615 build1 (GOTO_EXPR,
4616 void_type_node,
4617 gnu_end_label)),
4618 stmt_p);
4619
4620 append_to_statement_list (LOOP_STMT_BODY (stmt), stmt_p);
4621
4622 if (LOOP_STMT_BOT_COND (stmt))
4623 append_to_statement_list (build3 (COND_EXPR, void_type_node,
4624 LOOP_STMT_BOT_COND (stmt),
4625 alloc_stmt_list (),
4626 build1 (GOTO_EXPR,
4627 void_type_node,
4628 gnu_end_label)),
4629 stmt_p);
4630
4631 if (LOOP_STMT_UPDATE (stmt))
4632 append_to_statement_list (LOOP_STMT_UPDATE (stmt), stmt_p);
4633
4634 append_to_statement_list (build1 (GOTO_EXPR, void_type_node,
4635 gnu_start_label),
4636 stmt_p);
4637 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
4638 gnu_end_label),
4639 stmt_p);
4640 return GS_OK;
4641 }
4642
4643 case EXIT_STMT:
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 ());
4650 return GS_OK;
4651
4652 default:
4653 gcc_unreachable ();
4654 }
4655 }
4656 \f
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. */
4661
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.
4673
4674 Second, this routine is called in type_annotate mode, to compute DDA
4675 information for types in withed units, for ASIS use */
4676
4677 static void
4678 elaborate_all_entities (Node_Id gnat_node)
4679 {
4680 Entity_Id gnat_with_clause, gnat_entity;
4681
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 */
4685
4686 if (!present_gnu_tree (gnat_node))
4687 save_gnu_tree (gnat_node, integer_zero_node, true);
4688
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
4691 the spec twice. */
4692
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)))
4699 {
4700 elaborate_all_entities (Library_Unit (gnat_with_clause));
4701
4702 if (Ekind (Entity (Name (gnat_with_clause))) == E_Package)
4703 {
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);
4720 }
4721 else if (Ekind (Entity (Name (gnat_with_clause))) == E_Generic_Package)
4722 {
4723 Node_Id gnat_body
4724 = Corresponding_Body (Unit (Library_Unit (gnat_with_clause)));
4725
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);
4730
4731 /* If body is available, elaborate its context. */
4732 if (Present (gnat_body))
4733 elaborate_all_entities (gnat_body);
4734 }
4735 }
4736
4737 if (Nkind (Unit (gnat_node)) == N_Package_Body && type_annotate_only)
4738 elaborate_all_entities (Library_Unit (gnat_node));
4739 }
4740 \f
4741 /* Do the processing of N_Freeze_Entity, GNAT_NODE. */
4742
4743 static void
4744 process_freeze_entity (Node_Id gnat_node)
4745 {
4746 Entity_Id gnat_entity = Entity (gnat_node);
4747 tree gnu_old;
4748 tree gnu_new;
4749 tree gnu_init
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;
4753
4754 /* If this is a package, need to generate code for the package. */
4755 if (Ekind (gnat_entity) == E_Package)
4756 {
4757 insert_code_for
4758 (Parent (Corresponding_Body
4759 (Parent (Declaration_Node (gnat_entity)))));
4760 return;
4761 }
4762
4763 /* Check for old definition after the above call. This Freeze_Node
4764 might be for one its Itypes. */
4765 gnu_old
4766 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : 0;
4767
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)))
4771 gnu_old = 0;
4772
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))))
4778 return;
4779
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
4783 purposes. */
4784 if ((gnu_old
4785 && TREE_CODE (gnu_old) == FUNCTION_DECL
4786 && (Ekind (gnat_entity) == E_Function
4787 || Ekind (gnat_entity) == E_Procedure))
4788 || (gnu_old
4789 && (TREE_CODE (TREE_TYPE (gnu_old)) == FUNCTION_TYPE
4790 && Ekind (gnat_entity) == E_Subprogram_Type)))
4791 return;
4792
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. */
4798 if (gnu_old
4799 && !(TREE_CODE (gnu_old) == TYPE_DECL
4800 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old))))
4801 {
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));
4806 return;
4807 }
4808
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. */
4813 if (gnu_old)
4814 {
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);
4823 }
4824
4825 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
4826 && Present (Full_View (gnat_entity)))
4827 {
4828 gnu_new = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 1);
4829
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)));
4833
4834 if (Unknown_Esize (gnat_entity))
4835 Set_Esize (gnat_entity, Esize (Full_View (gnat_entity)));
4836
4837 if (Unknown_RM_Size (gnat_entity))
4838 Set_RM_Size (gnat_entity, RM_Size (Full_View (gnat_entity)));
4839
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);
4848 }
4849 else
4850 gnu_new = gnat_to_gnu_entity (gnat_entity, gnu_init, 1);
4851
4852 /* If we've made any pointers to the old version of this type, we
4853 have to update them. */
4854 if (gnu_old)
4855 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
4856 TREE_TYPE (gnu_new));
4857 }
4858 \f
4859 /* Process the list of inlined subprograms of GNAT_NODE, which is an
4860 N_Compilation_Unit. */
4861
4862 static void
4863 process_inlined_subprograms (Node_Id gnat_node)
4864 {
4865 Entity_Id gnat_entity;
4866 Node_Id gnat_body;
4867
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))
4874 {
4875 gnat_body = Parent (Declaration_Node (gnat_entity));
4876
4877 if (Nkind (gnat_body) != N_Subprogram_Body)
4878 {
4879 /* ??? This really should always be Present. */
4880 if (No (Corresponding_Body (gnat_body)))
4881 continue;
4882
4883 gnat_body
4884 = Parent (Declaration_Node (Corresponding_Body (gnat_body)));
4885 }
4886
4887 if (Present (gnat_body))
4888 {
4889 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4890 add_stmt (gnat_to_gnu (gnat_body));
4891 }
4892 }
4893 }
4894 \f
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.
4899
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.
4903
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. */
4907
4908 static void
4909 process_decls (List_Id gnat_decls, List_Id gnat_decls2,
4910 Node_Id gnat_end_list, bool pass1p, bool pass2p)
4911 {
4912 List_Id gnat_decl_array[2];
4913 Node_Id gnat_decl;
4914 int i;
4915
4916 gnat_decl_array[0] = gnat_decls, gnat_decl_array[1] = gnat_decls2;
4917
4918 if (pass1p)
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))
4923 {
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);
4932
4933 /* Similarly for any declarations in the actions of a
4934 freeze node. */
4935 else if (Nkind (gnat_decl) == N_Freeze_Entity)
4936 {
4937 process_freeze_entity (gnat_decl);
4938 process_decls (Actions (gnat_decl), Empty, Empty, true, false);
4939 }
4940
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);
4947
4948 else if (Nkind (gnat_decl) == N_Package_Body_Stub
4949 && Present (Library_Unit (gnat_decl))
4950 && Present (Freeze_Node
4951 (Corresponding_Spec
4952 (Proper_Body (Unit
4953 (Library_Unit (gnat_decl)))))))
4954 record_code_position
4955 (Proper_Body (Unit (Library_Unit (gnat_decl))));
4956
4957 /* We defer most subprogram bodies to the second pass. */
4958 else if (Nkind (gnat_decl) == N_Subprogram_Body)
4959 {
4960 if (Acts_As_Spec (gnat_decl))
4961 {
4962 Node_Id gnat_subprog_id = Defining_Entity (gnat_decl);
4963
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);
4967 }
4968 }
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)
4973 {
4974 Node_Id gnat_subprog_id =
4975 Defining_Entity (Specification (gnat_decl));
4976
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);
4981 }
4982
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)
4987 ;
4988 else
4989 add_stmt (gnat_to_gnu (gnat_decl));
4990 }
4991
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. */
4995 if (pass2p)
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))
5000 {
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));
5006
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);
5013
5014 else if (Nkind (gnat_decl) == N_Freeze_Entity)
5015 process_decls (Actions (gnat_decl), Empty, Empty, false, true);
5016 }
5017 }
5018 \f
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. */
5022
5023 static tree
5024 emit_range_check (tree gnu_expr, Entity_Id gnat_range_type)
5025 {
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));
5030
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))))
5037 return gnu_expr;
5038
5039 /* Checked expressions must be evaluated only once. */
5040 gnu_expr = protect_multiple_eval (gnu_expr);
5041
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. */
5047 return emit_check
5048 (build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
5049 invert_truthvalue
5050 (build_binary_op (GE_EXPR, integer_type_node,
5051 convert (gnu_compare_type, gnu_expr),
5052 convert (gnu_compare_type, gnu_low))),
5053 invert_truthvalue
5054 (build_binary_op (LE_EXPR, integer_type_node,
5055 convert (gnu_compare_type, gnu_expr),
5056 convert (gnu_compare_type,
5057 gnu_high)))),
5058 gnu_expr, CE_Range_Check_Failed);
5059 }
5060 \f
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 */
5072
5073 static tree
5074 emit_index_check (tree gnu_array_object,
5075 tree gnu_expr,
5076 tree gnu_low,
5077 tree gnu_high)
5078 {
5079 tree gnu_expr_check;
5080
5081 /* Checked expressions must be evaluated only once. */
5082 gnu_expr = protect_multiple_eval (gnu_expr);
5083
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);
5087
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);
5092
5093 /* There's no good type to use here, so we might as well use
5094 integer_type_node. */
5095 return emit_check
5096 (build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
5097 build_binary_op (LT_EXPR, integer_type_node,
5098 gnu_expr_check,
5099 convert (TREE_TYPE (gnu_expr_check),
5100 gnu_low)),
5101 build_binary_op (GT_EXPR, integer_type_node,
5102 gnu_expr_check,
5103 convert (TREE_TYPE (gnu_expr_check),
5104 gnu_high))),
5105 gnu_expr, CE_Index_Check_Failed);
5106 }
5107 \f
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. */
5112
5113 static tree
5114 emit_check (tree gnu_cond, tree gnu_expr, int reason)
5115 {
5116 tree gnu_call;
5117 tree gnu_result;
5118
5119 gnu_call = build_call_raise (reason);
5120
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
5124 out. */
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),
5128 gnu_expr));
5129
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))
5134 gnu_result
5135 = build2 (COMPOUND_EXPR, TREE_TYPE (gnu_expr), gnu_expr, gnu_result);
5136 else
5137 TREE_SIDE_EFFECTS (gnu_result) = 0;
5138
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);
5143 }
5144 \f
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. */
5150
5151 static tree
5152 convert_with_check (Entity_Id gnat_type, tree gnu_expr, bool overflowp,
5153 bool rangep, bool truncatep)
5154 {
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;
5161
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);
5169
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
5174 (unpacked) type. */
5175 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
5176 gnu_result = convert (gnu_in_basetype, gnu_result);
5177
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. */
5181 if (overflowp
5182 && !(FLOAT_TYPE_P (gnu_base_type) && INTEGRAL_TYPE_P (gnu_in_basetype)))
5183 {
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);
5191
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);
5197
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);
5201
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);
5204
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);
5207
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))
5223 : 1))
5224 gnu_cond
5225 = invert_truthvalue
5226 (build_binary_op (GE_EXPR, integer_type_node,
5227 gnu_input, convert (gnu_in_basetype,
5228 gnu_out_lb)));
5229
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))
5235 : 1))
5236 gnu_cond
5237 = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node, gnu_cond,
5238 invert_truthvalue
5239 (build_binary_op (LE_EXPR, integer_type_node,
5240 gnu_input,
5241 convert (gnu_in_basetype,
5242 gnu_out_ub))));
5243
5244 if (!integer_zerop (gnu_cond))
5245 gnu_result = emit_check (gnu_cond, gnu_input,
5246 CE_Overflow_Check_Failed);
5247 }
5248
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)
5252 && !truncatep)
5253 {
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;
5258
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.
5263
5264 FIXME: For maximum efficiency, this should only be done for machines
5265 and types where intermediates may have extra precision. */
5266
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));
5272
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);
5279
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.
5290
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).
5295 */
5296
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);
5302 gnu_add_pred_half
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);
5308 }
5309
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);
5314 else
5315 gnu_result = convert (gnu_ada_base_type, gnu_result);
5316
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. */
5320
5321 if (rangep
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);
5325
5326 return convert (gnu_type, gnu_result);
5327 }
5328 \f
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
5332 cases. */
5333
5334 static bool
5335 addressable_p (tree gnu_expr)
5336 {
5337 switch (TREE_CODE (gnu_expr))
5338 {
5339 case VAR_DECL:
5340 case PARM_DECL:
5341 case FUNCTION_DECL:
5342 case RESULT_DECL:
5343 /* All DECLs are addressable: if they are in a register, we can force
5344 them to memory. */
5345 return true;
5346
5347 case UNCONSTRAINED_ARRAY_REF:
5348 case INDIRECT_REF:
5349 case CONSTRUCTOR:
5350 case NULL_EXPR:
5351 case SAVE_EXPR:
5352 return true;
5353
5354 case COMPONENT_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)));
5359
5360 case ARRAY_REF: case ARRAY_RANGE_REF:
5361 case REALPART_EXPR: case IMAGPART_EXPR:
5362 case NOP_EXPR:
5363 return addressable_p (TREE_OPERAND (gnu_expr, 0));
5364
5365 case CONVERT_EXPR:
5366 return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr))
5367 && addressable_p (TREE_OPERAND (gnu_expr, 0)));
5368
5369 case VIEW_CONVERT_EXPR:
5370 {
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));
5374
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)));
5385 }
5386
5387 default:
5388 return false;
5389 }
5390 }
5391 \f
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. */
5395
5396 void
5397 process_type (Entity_Id gnat_entity)
5398 {
5399 tree gnu_old
5400 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : 0;
5401 tree gnu_new;
5402
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))))
5414 {
5415 elaborate_entity (gnat_entity);
5416
5417 if (!gnu_old)
5418 {
5419 tree gnu_decl = create_type_decl (get_entity_name (gnat_entity),
5420 make_dummy_type (gnat_entity),
5421 NULL, false, false, gnat_entity);
5422
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);
5427 }
5428
5429 return;
5430 }
5431
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
5435 pointers. */
5436 if (gnu_old)
5437 {
5438 if (TREE_CODE (gnu_old) != TYPE_DECL
5439 || !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old)))
5440 {
5441 /* If this was a withed access type, this is not an error
5442 and merely indicates we've already elaborated the type
5443 already. */
5444 gcc_assert (Is_Type (gnat_entity) && From_With_Type (gnat_entity));
5445 return;
5446 }
5447
5448 save_gnu_tree (gnat_entity, NULL_TREE, false);
5449 }
5450
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);
5454
5455 /* If we have an old type and we've made pointers to this type,
5456 update those pointers. */
5457 if (gnu_old)
5458 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
5459 TREE_TYPE (gnu_new));
5460
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
5463 on the type. */
5464 /* ??? Including protected types here is a guess. */
5465
5466 if (IN (Ekind (gnat_entity), Record_Kind)
5467 && Is_Concurrent_Record_Type (gnat_entity)
5468 && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity)))
5469 {
5470 tree gnu_task_old
5471 = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity));
5472
5473 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
5474 NULL_TREE, false);
5475 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
5476 gnu_new, false);
5477
5478 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old)),
5479 TREE_TYPE (gnu_new));
5480 }
5481 }
5482 \f
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.
5485
5486 Return a CONSTRUCTOR to build the record. */
5487
5488 static tree
5489 assoc_to_constructor (Node_Id gnat_assoc, tree gnu_type)
5490 {
5491 tree gnu_list, gnu_result;
5492
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
5496 has no fields. */
5497
5498 for (gnu_list = NULL_TREE; Present (gnat_assoc);
5499 gnat_assoc = Next (gnat_assoc))
5500 {
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));
5504
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)));
5508
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))))
5513 continue;
5514
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));
5519
5520 gnu_expr = convert (TREE_TYPE (gnu_field), gnu_expr);
5521
5522 /* Add the field and expression to the list. */
5523 gnu_list = tree_cons (gnu_field, gnu_expr, gnu_list);
5524 }
5525
5526 gnu_result = extract_values (gnu_list, gnu_type);
5527
5528 #ifdef ENABLE_CHECKING
5529 {
5530 tree gnu_field;
5531
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));
5535 }
5536 #endif
5537
5538 return gnu_result;
5539 }
5540
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. */
5546
5547 static tree
5548 pos_to_constructor (Node_Id gnat_expr, tree gnu_array_type,
5549 Entity_Id gnat_component_type)
5550 {
5551 tree gnu_expr_list = NULL_TREE;
5552 tree gnu_index = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type));
5553 tree gnu_expr;
5554
5555 for ( ; Present (gnat_expr); gnat_expr = Next (gnat_expr))
5556 {
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
5559 case). */
5560
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);
5567 else
5568 {
5569 gnu_expr = gnat_to_gnu (gnat_expr);
5570
5571 /* before assigning the element to the array make sure it is
5572 in range */
5573 if (Do_Range_Check (gnat_expr))
5574 gnu_expr = emit_range_check (gnu_expr, gnat_component_type);
5575 }
5576
5577 gnu_expr_list
5578 = tree_cons (gnu_index, convert (TREE_TYPE (gnu_array_type), gnu_expr),
5579 gnu_expr_list);
5580
5581 gnu_index = int_const_binop (PLUS_EXPR, gnu_index, integer_one_node, 0);
5582 }
5583
5584 return gnat_build_constructor (gnu_array_type, nreverse (gnu_expr_list));
5585 }
5586 \f
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. */
5591
5592 static tree
5593 extract_values (tree values, tree record_type)
5594 {
5595 tree result = NULL_TREE;
5596 tree field, tem;
5597
5598 for (field = TYPE_FIELDS (record_type); field; field = TREE_CHAIN (field))
5599 {
5600 tree value = 0;
5601
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)))
5605 {
5606 value = TREE_VALUE (tem);
5607 TREE_ADDRESSABLE (tem) = 1;
5608 }
5609
5610 else if (DECL_INTERNAL_P (field))
5611 {
5612 value = extract_values (values, TREE_TYPE (field));
5613 if (TREE_CODE (value) == CONSTRUCTOR && !CONSTRUCTOR_ELTS (value))
5614 value = 0;
5615 }
5616 else
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))
5621 {
5622 value = convert (TREE_TYPE (field), TREE_VALUE (tem));
5623 TREE_ADDRESSABLE (tem) = 1;
5624 }
5625
5626 if (!value)
5627 continue;
5628
5629 result = tree_cons (field, value, result);
5630 }
5631
5632 return gnat_build_constructor (record_type, nreverse (result));
5633 }
5634 \f
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. */
5637
5638 static tree
5639 maybe_implicit_deref (tree exp)
5640 {
5641 /* If the type is a pointer, dereference it. */
5642
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);
5645
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);
5650
5651 return exp;
5652 }
5653 \f
5654 /* Protect EXP from multiple evaluation. This may make a SAVE_EXPR. */
5655
5656 tree
5657 protect_multiple_eval (tree exp)
5658 {
5659 tree type = TREE_TYPE (exp);
5660
5661 /* If this has no side effects, we don't need to do anything. */
5662 if (!TREE_SIDE_EFFECTS (exp))
5663 return exp;
5664
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)));
5676
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);
5681
5682 /* Otherwise, dereference, protect the address, and re-reference. */
5683 else
5684 return
5685 build_unary_op (INDIRECT_REF, type,
5686 save_expr (build_unary_op (ADDR_EXPR,
5687 build_reference_type (type),
5688 exp)));
5689 }
5690 \f
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. */
5694
5695 tree
5696 gnat_stabilize_reference (tree ref, bool force)
5697 {
5698 tree type = TREE_TYPE (ref);
5699 enum tree_code code = TREE_CODE (ref);
5700 tree result;
5701
5702 switch (code)
5703 {
5704 case VAR_DECL:
5705 case PARM_DECL:
5706 case RESULT_DECL:
5707 /* No action is needed in this case. */
5708 return ref;
5709
5710 case NOP_EXPR:
5711 case CONVERT_EXPR:
5712 case FLOAT_EXPR:
5713 case FIX_TRUNC_EXPR:
5714 case FIX_FLOOR_EXPR:
5715 case FIX_ROUND_EXPR:
5716 case FIX_CEIL_EXPR:
5717 case VIEW_CONVERT_EXPR:
5718 case ADDR_EXPR:
5719 result
5720 = build1 (code, type,
5721 gnat_stabilize_reference (TREE_OPERAND (ref, 0), force));
5722 break;
5723
5724 case INDIRECT_REF:
5725 case UNCONSTRAINED_ARRAY_REF:
5726 result = build1 (code, type,
5727 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 0),
5728 force));
5729 break;
5730
5731 case COMPONENT_REF:
5732 result = build3 (COMPONENT_REF, type,
5733 gnat_stabilize_reference (TREE_OPERAND (ref, 0),
5734 force),
5735 TREE_OPERAND (ref, 1), NULL_TREE);
5736 break;
5737
5738 case BIT_FIELD_REF:
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),
5742 force),
5743 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 2),
5744 force));
5745 break;
5746
5747 case ARRAY_REF:
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),
5752 force),
5753 NULL_TREE, NULL_TREE);
5754 break;
5755
5756 case COMPOUND_EXPR:
5757 result = build2 (COMPOUND_EXPR, type,
5758 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 0),
5759 force),
5760 gnat_stabilize_reference (TREE_OPERAND (ref, 1),
5761 force));
5762 break;
5763
5764 /* If arg isn't a kind of lvalue we recognize, make no change.
5765 Caller should recognize the error for an invalid lvalue. */
5766 default:
5767 return ref;
5768
5769 case ERROR_MARK:
5770 return error_mark_node;
5771 }
5772
5773 TREE_READONLY (result) = TREE_READONLY (ref);
5774
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.
5779
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. */
5783
5784 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
5785 TREE_SIDE_EFFECTS (result) |= TREE_SIDE_EFFECTS (ref);
5786
5787 return result;
5788 }
5789
5790 /* Similar to stabilize_reference_1 in tree.c, but supports an extra
5791 arg to force a SAVE_EXPR for everything. */
5792
5793 static tree
5794 gnat_stabilize_reference_1 (tree e, bool force)
5795 {
5796 enum tree_code code = TREE_CODE (e);
5797 tree type = TREE_TYPE (e);
5798 tree result;
5799
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. */
5804
5805 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
5806 return e;
5807
5808 switch (TREE_CODE_CLASS (code))
5809 {
5810 case tcc_exceptional:
5811 case tcc_type:
5812 case tcc_declaration:
5813 case tcc_comparison:
5814 case tcc_statement:
5815 case tcc_expression:
5816 case tcc_reference:
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),
5824 force),
5825 TREE_OPERAND (e, 1), TREE_OPERAND (e, 2));
5826 else if (TREE_SIDE_EFFECTS (e) || force)
5827 return save_expr (e);
5828 else
5829 return e;
5830 break;
5831
5832 case tcc_constant:
5833 /* Constants need no processing. In fact, we should never reach
5834 here. */
5835 return e;
5836
5837 case tcc_binary:
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),
5842 force));
5843 break;
5844
5845 case tcc_unary:
5846 /* Recursively stabilize each operand. */
5847 result = build1 (code, type,
5848 gnat_stabilize_reference_1 (TREE_OPERAND (e, 0),
5849 force));
5850 break;
5851
5852 default:
5853 gcc_unreachable ();
5854 }
5855
5856 TREE_READONLY (result) = TREE_READONLY (e);
5857
5858 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
5859 TREE_SIDE_EFFECTS (result) |= TREE_SIDE_EFFECTS (e);
5860 return result;
5861 }
5862 \f
5863 extern char *__gnat_to_canonical_file_spec (char *);
5864
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
5869 directory). */
5870
5871 bool
5872 Sloc_to_locus (Source_Ptr Sloc, location_t *locus)
5873 {
5874 /* If node not from source code, ignore. */
5875 if (Sloc < 0)
5876 return false;
5877
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. */
5883 locus->file
5884 = IDENTIFIER_POINTER
5885 (get_identifier
5886 (__gnat_to_canonical_file_spec
5887 (Get_Name_String (Full_Debug_Name (Get_Source_File_Index (Sloc))))));
5888
5889 locus->line = Get_Logical_Line_Number (Sloc);
5890
5891 ref_filename
5892 = IDENTIFIER_POINTER
5893 (get_identifier
5894 (Get_Name_String (Debug_Source_Name (Get_Source_File_Index (Sloc)))));;
5895
5896 return true;
5897 }
5898
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. */
5901
5902 static void
5903 annotate_with_node (tree node, Node_Id gnat_node)
5904 {
5905 location_t locus;
5906
5907 if (!Sloc_to_locus (Sloc (gnat_node), &locus))
5908 return;
5909
5910 annotate_with_locus (node, locus);
5911 }
5912 \f
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. */
5916
5917 void
5918 post_error (const char *msg, Node_Id node)
5919 {
5920 String_Template temp;
5921 Fat_Pointer fp;
5922
5923 temp.Low_Bound = 1, temp.High_Bound = strlen (msg);
5924 fp.Array = msg, fp.Bounds = &temp;
5925 if (Present (node))
5926 Error_Msg_N (fp, node);
5927 }
5928
5929 /* Similar, but NODE is the node at which to post the error and ENT
5930 is the node to use for the "&" substitution. */
5931
5932 void
5933 post_error_ne (const char *msg, Node_Id node, Entity_Id ent)
5934 {
5935 String_Template temp;
5936 Fat_Pointer fp;
5937
5938 temp.Low_Bound = 1, temp.High_Bound = strlen (msg);
5939 fp.Array = msg, fp.Bounds = &temp;
5940 if (Present (node))
5941 Error_Msg_NE (fp, node, ent);
5942 }
5943
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 ^. */
5946
5947 void
5948 post_error_ne_num (const char *msg, Node_Id node, Entity_Id ent, int n)
5949 {
5950 String_Template temp;
5951 Fat_Pointer fp;
5952
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);
5956
5957 if (Present (node))
5958 Error_Msg_NE (fp, node, ent);
5959 }
5960 \f
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. */
5966
5967 void
5968 post_error_ne_tree (const char *msg, Node_Id node, Entity_Id ent, tree t)
5969 {
5970 char *newmsg = alloca (strlen (msg) + 1);
5971 String_Template temp = {1, 0};
5972 Fat_Pointer fp;
5973 char start_yes, end_yes, start_no, end_no;
5974 const char *p;
5975 char *q;
5976
5977 fp.Array = newmsg, fp.Bounds = &temp;
5978
5979 if (host_integerp (t, 1)
5980 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_INT
5981 &&
5982 compare_tree_int
5983 (t, (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_INT - 1)) - 1)) < 0
5984 #endif
5985 )
5986 {
5987 Error_Msg_Uint_1 = UI_From_Int (tree_low_cst (t, 1));
5988 start_yes = '{', end_yes = '}', start_no = '[', end_no = ']';
5989 }
5990 else
5991 start_yes = '[', end_yes = ']', start_no = '{', end_no = '}';
5992
5993 for (p = msg, q = newmsg; *p; p++)
5994 {
5995 if (*p == start_yes)
5996 for (p++; *p != end_yes; p++)
5997 *q++ = *p;
5998 else if (*p == start_no)
5999 for (p++; *p != end_no; p++)
6000 ;
6001 else
6002 *q++ = *p;
6003 }
6004
6005 *q = 0;
6006
6007 temp.High_Bound = strlen (newmsg);
6008 if (Present (node))
6009 Error_Msg_NE (fp, node, ent);
6010 }
6011
6012 /* Similar to post_error_ne_tree, except that NUM is a second
6013 integer to write in the message. */
6014
6015 void
6016 post_error_ne_tree_2 (const char *msg,
6017 Node_Id node,
6018 Entity_Id ent,
6019 tree t,
6020 int num)
6021 {
6022 Error_Msg_Uint_2 = UI_From_Int (num);
6023 post_error_ne_tree (msg, node, ent, t);
6024 }
6025 \f
6026 /* Initialize the table that maps GNAT codes to GCC codes for simple
6027 binary and unary operations. */
6028
6029 void
6030 init_code_table (void)
6031 {
6032 gnu_codes[N_And_Then] = TRUTH_ANDIF_EXPR;
6033 gnu_codes[N_Or_Else] = TRUTH_ORIF_EXPR;
6034
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;
6057 }
6058
6059 #include "gt-ada-trans.h"