trans.c (gigi): Use REAL_ARITHMETIC, not REAL_VALUE_ATOF, to initialize dconstp5...
[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 * *
10 * Copyright (C) 1992-2003, Free Software Foundation, Inc. *
11 * *
12 * GNAT is free software; you can redistribute it and/or modify it under *
13 * terms of the GNU General Public License as published by the Free Soft- *
14 * ware Foundation; either version 2, or (at your option) any later ver- *
15 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
16 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
18 * for more details. You should have received a copy of the GNU General *
19 * Public License distributed with GNAT; see file COPYING. If not, write *
20 * to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, *
21 * MA 02111-1307, USA. *
22 * *
23 * GNAT was originally developed by the GNAT team at New York University. *
24 * Extensive contributions were provided by Ada Core Technologies Inc. *
25 * *
26 ****************************************************************************/
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "real.h"
34 #include "flags.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 "ada.h"
43 #include "types.h"
44 #include "atree.h"
45 #include "elists.h"
46 #include "namet.h"
47 #include "nlists.h"
48 #include "snames.h"
49 #include "stringt.h"
50 #include "uintp.h"
51 #include "urealp.h"
52 #include "fe.h"
53 #include "sinfo.h"
54 #include "einfo.h"
55 #include "ada-tree.h"
56 #include "gigi.h"
57
58 int max_gnat_nodes;
59 int number_names;
60 struct Node *Nodes_Ptr;
61 Node_Id *Next_Node_Ptr;
62 Node_Id *Prev_Node_Ptr;
63 struct Elist_Header *Elists_Ptr;
64 struct Elmt_Item *Elmts_Ptr;
65 struct String_Entry *Strings_Ptr;
66 Char_Code *String_Chars_Ptr;
67 struct List_Header *List_Headers_Ptr;
68
69 /* Current filename without path. */
70 const char *ref_filename;
71
72 /* Flag indicating whether file names are discarded in exception messages */
73 int discard_file_names;
74
75 /* If true, then gigi is being called on an analyzed but unexpanded
76 tree, and the only purpose of the call is to properly annotate
77 types with representation information. */
78 int type_annotate_only;
79
80 /* List of TREE_LIST nodes representing a block stack. TREE_VALUE
81 of each gives the variable used for the setjmp buffer in the current
82 block, if any. TREE_PURPOSE gives the bottom condition for a loop,
83 if this block is for a loop. The latter is only used to save the tree
84 over GC. */
85 tree gnu_block_stack;
86
87 /* List of TREE_LIST nodes representing a stack of exception pointer
88 variables. TREE_VALUE is the VAR_DECL that stores the address of
89 the raised exception. Nonzero means we are in an exception
90 handler. Not used in the zero-cost case. */
91 static GTY(()) tree gnu_except_ptr_stack;
92
93 /* List of TREE_LIST nodes containing pending elaborations lists.
94 used to prevent the elaborations being reclaimed by GC. */
95 static GTY(()) tree gnu_pending_elaboration_lists;
96
97 /* Map GNAT tree codes to GCC tree codes for simple expressions. */
98 static enum tree_code gnu_codes[Number_Node_Kinds];
99
100 /* Current node being treated, in case gigi_abort called. */
101 Node_Id error_gnat_node;
102
103 /* Variable that stores a list of labels to be used as a goto target instead of
104 a return in some functions. See processing for N_Subprogram_Body. */
105 static GTY(()) tree gnu_return_label_stack;
106
107 static tree tree_transform PARAMS((Node_Id));
108 static void elaborate_all_entities PARAMS((Node_Id));
109 static void process_freeze_entity PARAMS((Node_Id));
110 static void process_inlined_subprograms PARAMS((Node_Id));
111 static void process_decls PARAMS((List_Id, List_Id, Node_Id,
112 int, int));
113 static tree emit_access_check PARAMS((tree));
114 static tree emit_discriminant_check PARAMS((tree, Node_Id));
115 static tree emit_range_check PARAMS((tree, Node_Id));
116 static tree emit_index_check PARAMS((tree, tree, tree, tree));
117 static tree emit_check PARAMS((tree, tree, int));
118 static tree convert_with_check PARAMS((Entity_Id, tree,
119 int, int, int));
120 static int addressable_p PARAMS((tree));
121 static tree assoc_to_constructor PARAMS((Node_Id, tree));
122 static tree extract_values PARAMS((tree, tree));
123 static tree pos_to_constructor PARAMS((Node_Id, tree, Entity_Id));
124 static tree maybe_implicit_deref PARAMS((tree));
125 static tree gnat_stabilize_reference_1 PARAMS((tree, int));
126 static int build_unit_elab PARAMS((Entity_Id, int, tree));
127
128 /* Constants for +0.5 and -0.5 for float-to-integer rounding. */
129 static REAL_VALUE_TYPE dconstp5;
130 static REAL_VALUE_TYPE dconstmp5;
131 \f
132 /* This is the main program of the back-end. It sets up all the table
133 structures and then generates code. */
134
135 void
136 gigi (gnat_root, max_gnat_node, number_name, nodes_ptr, next_node_ptr,
137 prev_node_ptr, elists_ptr, elmts_ptr, strings_ptr, string_chars_ptr,
138 list_headers_ptr, number_units, file_info_ptr, standard_integer,
139 standard_long_long_float, standard_exception_type, gigi_operating_mode)
140 Node_Id gnat_root;
141 int max_gnat_node;
142 int number_name;
143 struct Node *nodes_ptr;
144 Node_Id *next_node_ptr;
145 Node_Id *prev_node_ptr;
146 struct Elist_Header *elists_ptr;
147 struct Elmt_Item *elmts_ptr;
148 struct String_Entry *strings_ptr;
149 Char_Code *string_chars_ptr;
150 struct List_Header *list_headers_ptr;
151 Int number_units ATTRIBUTE_UNUSED;
152 char *file_info_ptr ATTRIBUTE_UNUSED;
153 Entity_Id standard_integer;
154 Entity_Id standard_long_long_float;
155 Entity_Id standard_exception_type;
156 Int gigi_operating_mode;
157 {
158 tree gnu_standard_long_long_float;
159 tree gnu_standard_exception_type;
160
161 max_gnat_nodes = max_gnat_node;
162 number_names = number_name;
163 Nodes_Ptr = nodes_ptr;
164 Next_Node_Ptr = next_node_ptr;
165 Prev_Node_Ptr = prev_node_ptr;
166 Elists_Ptr = elists_ptr;
167 Elmts_Ptr = elmts_ptr;
168 Strings_Ptr = strings_ptr;
169 String_Chars_Ptr = string_chars_ptr;
170 List_Headers_Ptr = list_headers_ptr;
171
172 type_annotate_only = (gigi_operating_mode == 1);
173
174 /* See if we should discard file names in exception messages. */
175 discard_file_names = (Global_Discard_Names || Debug_Flag_NN);
176
177 if (Nkind (gnat_root) != N_Compilation_Unit)
178 gigi_abort (301);
179
180 set_lineno (gnat_root, 0);
181
182 /* Initialize ourselves. */
183 init_gnat_to_gnu ();
184 init_dummy_type ();
185 init_code_table ();
186
187 /* Enable GNAT stack checking method if needed */
188 if (!Stack_Check_Probes_On_Target)
189 set_stack_check_libfunc (gen_rtx (SYMBOL_REF, Pmode, "_gnat_stack_check"));
190
191 /* Save the type we made for integer as the type for Standard.Integer.
192 Then make the rest of the standard types. Note that some of these
193 may be subtypes. */
194 save_gnu_tree (Base_Type (standard_integer),
195 TYPE_NAME (integer_type_node), 0);
196
197 gnu_except_ptr_stack = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
198
199 REAL_ARITHMETIC (dconstp5, RDIV_EXPR, dconst1, dconst2);
200 REAL_ARITHMETIC (dconstmp5, RDIV_EXPR, dconstm1, dconst2);
201
202 gnu_standard_long_long_float
203 = gnat_to_gnu_entity (Base_Type (standard_long_long_float), NULL_TREE, 0);
204 gnu_standard_exception_type
205 = gnat_to_gnu_entity (Base_Type (standard_exception_type), NULL_TREE, 0);
206
207 init_gigi_decls (gnu_standard_long_long_float, gnu_standard_exception_type);
208
209 /* Process any Pragma Ident for the main unit. */
210 #ifdef ASM_OUTPUT_IDENT
211 if (Present (Ident_String (Main_Unit)))
212 ASM_OUTPUT_IDENT
213 (asm_out_file,
214 TREE_STRING_POINTER (gnat_to_gnu (Ident_String (Main_Unit))));
215 #endif
216
217 /* If we are using the GCC exception mechanism, let GCC know. */
218 if (Exception_Mechanism == GCC_ZCX)
219 gnat_init_gcc_eh ();
220
221 gnat_to_code (gnat_root);
222 }
223
224 \f
225 /* This function is the driver of the GNAT to GCC tree transformation process.
226 GNAT_NODE is the root of some gnat tree. It generates code for that
227 part of the tree. */
228
229 void
230 gnat_to_code (gnat_node)
231 Node_Id gnat_node;
232 {
233 tree gnu_root;
234
235 /* Save node number in case error */
236 error_gnat_node = gnat_node;
237
238 gnu_root = tree_transform (gnat_node);
239
240 /* This should just generate code, not return a value. If it returns
241 a value, something is wrong. */
242 if (gnu_root != error_mark_node)
243 gigi_abort (302);
244 }
245
246 /* GNAT_NODE is the root of some GNAT tree. Return the root of the GCC
247 tree corresponding to that GNAT tree. Normally, no code is generated.
248 We just return an equivalent tree which is used elsewhere to generate
249 code. */
250
251 tree
252 gnat_to_gnu (gnat_node)
253 Node_Id gnat_node;
254 {
255 tree gnu_root;
256
257 /* Save node number in case error */
258 error_gnat_node = gnat_node;
259
260 gnu_root = tree_transform (gnat_node);
261
262 /* If we got no code as a result, something is wrong. */
263 if (gnu_root == error_mark_node && ! type_annotate_only)
264 gigi_abort (303);
265
266 return gnu_root;
267 }
268 \f
269 /* This function is the driver of the GNAT to GCC tree transformation process.
270 It is the entry point of the tree transformer. GNAT_NODE is the root of
271 some GNAT tree. Return the root of the corresponding GCC tree or
272 error_mark_node to signal that there is no GCC tree to return.
273
274 The latter is the case if only code generation actions have to be performed
275 like in the case of if statements, loops, etc. This routine is wrapped
276 in the above two routines for most purposes. */
277
278 static tree
279 tree_transform (gnat_node)
280 Node_Id gnat_node;
281 {
282 tree gnu_result = error_mark_node; /* Default to no value. */
283 tree gnu_result_type = void_type_node;
284 tree gnu_expr;
285 tree gnu_lhs, gnu_rhs;
286 Node_Id gnat_temp;
287 Entity_Id gnat_temp_type;
288
289 /* Set input_file_name and lineno from the Sloc in the GNAT tree. */
290 set_lineno (gnat_node, 0);
291
292 /* If this is a Statement and we are at top level, we add the statement
293 as an elaboration for a null tree. That will cause it to be placed
294 in the elaboration procedure. */
295 if (global_bindings_p ()
296 && ((IN (Nkind (gnat_node), N_Statement_Other_Than_Procedure_Call)
297 && Nkind (gnat_node) != N_Null_Statement)
298 || Nkind (gnat_node) == N_Procedure_Call_Statement
299 || Nkind (gnat_node) == N_Label
300 || (Nkind (gnat_node) == N_Handled_Sequence_Of_Statements
301 && (Present (Exception_Handlers (gnat_node))
302 || Present (At_End_Proc (gnat_node))))
303 || ((Nkind (gnat_node) == N_Raise_Constraint_Error
304 || Nkind (gnat_node) == N_Raise_Storage_Error
305 || Nkind (gnat_node) == N_Raise_Program_Error)
306 && (Ekind (Etype (gnat_node)) == E_Void))))
307 {
308 add_pending_elaborations (NULL_TREE, make_transform_expr (gnat_node));
309
310 return error_mark_node;
311 }
312
313 /* If this node is a non-static subexpression and we are only
314 annotating types, make this into a NULL_EXPR for non-VOID types
315 and error_mark_node for void return types. But allow
316 N_Identifier since we use it for lots of things, including
317 getting trees for discriminants. */
318
319 if (type_annotate_only
320 && IN (Nkind (gnat_node), N_Subexpr)
321 && Nkind (gnat_node) != N_Identifier
322 && ! Compile_Time_Known_Value (gnat_node))
323 {
324 gnu_result_type = get_unpadded_type (Etype (gnat_node));
325
326 if (TREE_CODE (gnu_result_type) == VOID_TYPE)
327 return error_mark_node;
328 else
329 return build1 (NULL_EXPR, gnu_result_type,
330 build_call_raise (CE_Range_Check_Failed));
331 }
332
333 switch (Nkind (gnat_node))
334 {
335 /********************************/
336 /* Chapter 2: Lexical Elements: */
337 /********************************/
338
339 case N_Identifier:
340 case N_Expanded_Name:
341 case N_Operator_Symbol:
342 case N_Defining_Identifier:
343
344 /* If the Etype of this node does not equal the Etype of the
345 Entity, something is wrong with the entity map, probably in
346 generic instantiation. However, this does not apply to
347 types. Since we sometime have strange Ekind's, just do
348 this test for objects. Also, if the Etype of the Entity
349 is private, the Etype of the N_Identifier is allowed to be the
350 full type and also we consider a packed array type to be the
351 same as the original type. Finally, if the types are Itypes,
352 one may be a copy of the other, which is also legal. */
353
354 gnat_temp = (Nkind (gnat_node) == N_Defining_Identifier
355 ? gnat_node : Entity (gnat_node));
356 gnat_temp_type = Etype (gnat_temp);
357
358 if (Etype (gnat_node) != gnat_temp_type
359 && ! (Is_Packed (gnat_temp_type)
360 && Etype (gnat_node) == Packed_Array_Type (gnat_temp_type))
361 && ! (IN (Ekind (gnat_temp_type), Private_Kind)
362 && Present (Full_View (gnat_temp_type))
363 && ((Etype (gnat_node) == Full_View (gnat_temp_type))
364 || (Is_Packed (Full_View (gnat_temp_type))
365 && Etype (gnat_node) ==
366 Packed_Array_Type (Full_View (gnat_temp_type)))))
367 && (!Is_Itype (Etype (gnat_node)) || !Is_Itype (gnat_temp_type))
368 && (Ekind (gnat_temp) == E_Variable
369 || Ekind (gnat_temp) == E_Component
370 || Ekind (gnat_temp) == E_Constant
371 || Ekind (gnat_temp) == E_Loop_Parameter
372 || IN (Ekind (gnat_temp), Formal_Kind)))
373 gigi_abort (304);
374
375 /* If this is a reference to a deferred constant whose partial view
376 is an unconstrained private type, the proper type is on the full
377 view of the constant, not on the full view of the type, which may
378 be unconstrained.
379
380 This may be a reference to a type, for example in the prefix of the
381 attribute Position, generated for dispatching code (see Make_DT in
382 exp_disp,adb). In that case we need the type itself, not is parent,
383 in particular if it is a derived type */
384
385 if (Is_Private_Type (gnat_temp_type)
386 && Has_Unknown_Discriminants (gnat_temp_type)
387 && Present (Full_View (gnat_temp))
388 && ! Is_Type (gnat_temp))
389 {
390 gnat_temp = Full_View (gnat_temp);
391 gnat_temp_type = Etype (gnat_temp);
392 gnu_result_type = get_unpadded_type (gnat_temp_type);
393 }
394 else
395 {
396 /* Expand the type of this identitier first, in case it is
397 an enumeral literal, which only get made when the type
398 is expanded. There is no order-of-elaboration issue here.
399 We want to use the Actual_Subtype if it has already been
400 elaborated, otherwise the Etype. Avoid using Actual_Subtype
401 for packed arrays to simplify things. */
402 if ((Ekind (gnat_temp) == E_Constant
403 || Ekind (gnat_temp) == E_Variable || Is_Formal (gnat_temp))
404 && ! (Is_Array_Type (Etype (gnat_temp))
405 && Present (Packed_Array_Type (Etype (gnat_temp))))
406 && Present (Actual_Subtype (gnat_temp))
407 && present_gnu_tree (Actual_Subtype (gnat_temp)))
408 gnat_temp_type = Actual_Subtype (gnat_temp);
409 else
410 gnat_temp_type = Etype (gnat_node);
411
412 gnu_result_type = get_unpadded_type (gnat_temp_type);
413 }
414
415 gnu_result = gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0);
416
417 /* If we are in an exception handler, force this variable into memory
418 to ensure optimization does not remove stores that appear
419 redundant but are actually needed in case an exception occurs.
420
421 ??? Note that we need not do this if the variable is declared within
422 the handler, only if it is referenced in the handler and declared
423 in an enclosing block, but we have no way of testing that
424 right now. */
425 if (TREE_VALUE (gnu_except_ptr_stack) != 0)
426 {
427 gnat_mark_addressable (gnu_result);
428 flush_addressof (gnu_result);
429 }
430
431 /* Some objects (such as parameters passed by reference, globals of
432 variable size, and renamed objects) actually represent the address
433 of the object. In that case, we must do the dereference. Likewise,
434 deal with parameters to foreign convention subprograms. Call fold
435 here since GNU_RESULT may be a CONST_DECL. */
436 if (DECL_P (gnu_result)
437 && (DECL_BY_REF_P (gnu_result)
438 || (TREE_CODE (gnu_result) == PARM_DECL
439 && DECL_BY_COMPONENT_PTR_P (gnu_result))))
440 {
441 int ro = DECL_POINTS_TO_READONLY_P (gnu_result);
442
443 if (TREE_CODE (gnu_result) == PARM_DECL
444 && DECL_BY_COMPONENT_PTR_P (gnu_result))
445 gnu_result = convert (build_pointer_type (gnu_result_type),
446 gnu_result);
447
448 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE,
449 fold (gnu_result));
450 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result) = ro;
451 }
452
453 /* The GNAT tree has the type of a function as the type of its result.
454 Also use the type of the result if the Etype is a subtype which
455 is nominally unconstrained. But remove any padding from the
456 resulting type. */
457 if (TREE_CODE (TREE_TYPE (gnu_result)) == FUNCTION_TYPE
458 || Is_Constr_Subt_For_UN_Aliased (gnat_temp_type))
459 {
460 gnu_result_type = TREE_TYPE (gnu_result);
461 if (TREE_CODE (gnu_result_type) == RECORD_TYPE
462 && TYPE_IS_PADDING_P (gnu_result_type))
463 gnu_result_type = TREE_TYPE (TYPE_FIELDS (gnu_result_type));
464 }
465
466 /* We always want to return the underlying INTEGER_CST for an
467 enumeration literal to avoid the need to call fold in lots
468 of places. But don't do this is the parent will be taking
469 the address of this object. */
470 if (TREE_CODE (gnu_result) == CONST_DECL)
471 {
472 gnat_temp = Parent (gnat_node);
473 if (DECL_CONST_CORRESPONDING_VAR (gnu_result) == 0
474 || (Nkind (gnat_temp) != N_Reference
475 && ! (Nkind (gnat_temp) == N_Attribute_Reference
476 && ((Get_Attribute_Id (Attribute_Name (gnat_temp))
477 == Attr_Address)
478 || (Get_Attribute_Id (Attribute_Name (gnat_temp))
479 == Attr_Access)
480 || (Get_Attribute_Id (Attribute_Name (gnat_temp))
481 == Attr_Unchecked_Access)
482 || (Get_Attribute_Id (Attribute_Name (gnat_temp))
483 == Attr_Unrestricted_Access)))))
484 gnu_result = DECL_INITIAL (gnu_result);
485 }
486 break;
487
488 case N_Integer_Literal:
489 {
490 tree gnu_type;
491
492 /* Get the type of the result, looking inside any padding and
493 left-justified modular types. Then get the value in that type. */
494 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
495
496 if (TREE_CODE (gnu_type) == RECORD_TYPE
497 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_type))
498 gnu_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
499
500 gnu_result = UI_To_gnu (Intval (gnat_node), gnu_type);
501
502 /* If the result overflows (meaning it doesn't fit in its base type),
503 abort. We would like to check that the value is within the range
504 of the subtype, but that causes problems with subtypes whose usage
505 will raise Constraint_Error and with biased representation, so
506 we don't. */
507 if (TREE_CONSTANT_OVERFLOW (gnu_result))
508 gigi_abort (305);
509 }
510 break;
511
512 case N_Character_Literal:
513 /* If a Entity is present, it means that this was one of the
514 literals in a user-defined character type. In that case,
515 just return the value in the CONST_DECL. Otherwise, use the
516 character code. In that case, the base type should be an
517 INTEGER_TYPE, but we won't bother checking for that. */
518 gnu_result_type = get_unpadded_type (Etype (gnat_node));
519 if (Present (Entity (gnat_node)))
520 gnu_result = DECL_INITIAL (get_gnu_tree (Entity (gnat_node)));
521 else
522 gnu_result = convert (gnu_result_type,
523 build_int_2 (Char_Literal_Value (gnat_node), 0));
524 break;
525
526 case N_Real_Literal:
527 /* If this is of a fixed-point type, the value we want is the
528 value of the corresponding integer. */
529 if (IN (Ekind (Underlying_Type (Etype (gnat_node))), Fixed_Point_Kind))
530 {
531 gnu_result_type = get_unpadded_type (Etype (gnat_node));
532 gnu_result = UI_To_gnu (Corresponding_Integer_Value (gnat_node),
533 gnu_result_type);
534 if (TREE_CONSTANT_OVERFLOW (gnu_result)
535 )
536 gigi_abort (305);
537 }
538 /* We should never see a Vax_Float type literal, since the front end
539 is supposed to transform these using appropriate conversions */
540 else if (Vax_Float (Underlying_Type (Etype (gnat_node))))
541 gigi_abort (334);
542
543 else
544 {
545 Ureal ur_realval = Realval (gnat_node);
546
547 gnu_result_type = get_unpadded_type (Etype (gnat_node));
548
549 /* If the real value is zero, so is the result. Otherwise,
550 convert it to a machine number if it isn't already. That
551 forces BASE to 0 or 2 and simplifies the rest of our logic. */
552 if (UR_Is_Zero (ur_realval))
553 gnu_result = convert (gnu_result_type, integer_zero_node);
554 else
555 {
556 if (! Is_Machine_Number (gnat_node))
557 ur_realval
558 = Machine (Base_Type (Underlying_Type (Etype (gnat_node))),
559 ur_realval, Round_Even);
560
561 gnu_result
562 = UI_To_gnu (Numerator (ur_realval), gnu_result_type);
563
564 /* If we have a base of zero, divide by the denominator.
565 Otherwise, the base must be 2 and we scale the value, which
566 we know can fit in the mantissa of the type (hence the use
567 of that type above). */
568 if (Rbase (ur_realval) == 0)
569 gnu_result
570 = build_binary_op (RDIV_EXPR,
571 get_base_type (gnu_result_type),
572 gnu_result,
573 UI_To_gnu (Denominator (ur_realval),
574 gnu_result_type));
575 else if (Rbase (ur_realval) != 2)
576 gigi_abort (336);
577
578 else
579 {
580 REAL_VALUE_TYPE tmp;
581
582 real_ldexp (&tmp, &TREE_REAL_CST (gnu_result),
583 - UI_To_Int (Denominator (ur_realval)));
584 gnu_result = build_real (gnu_result_type, tmp);
585 }
586 }
587
588 /* Now see if we need to negate the result. Do it this way to
589 properly handle -0. */
590 if (UR_Is_Negative (Realval (gnat_node)))
591 gnu_result
592 = build_unary_op (NEGATE_EXPR, get_base_type (gnu_result_type),
593 gnu_result);
594 }
595
596 break;
597
598 case N_String_Literal:
599 gnu_result_type = get_unpadded_type (Etype (gnat_node));
600 if (TYPE_PRECISION (TREE_TYPE (gnu_result_type)) == HOST_BITS_PER_CHAR)
601 {
602 /* We assume here that all strings are of type standard.string.
603 "Weird" types of string have been converted to an aggregate
604 by the expander. */
605 String_Id gnat_string = Strval (gnat_node);
606 int length = String_Length (gnat_string);
607 char *string = (char *) alloca (length + 1);
608 int i;
609
610 /* Build the string with the characters in the literal. Note
611 that Ada strings are 1-origin. */
612 for (i = 0; i < length; i++)
613 string[i] = Get_String_Char (gnat_string, i + 1);
614
615 /* Put a null at the end of the string in case it's in a context
616 where GCC will want to treat it as a C string. */
617 string[i] = 0;
618
619 gnu_result = build_string (length, string);
620
621 /* Strings in GCC don't normally have types, but we want
622 this to not be converted to the array type. */
623 TREE_TYPE (gnu_result) = gnu_result_type;
624 }
625 else
626 {
627 /* Build a list consisting of each character, then make
628 the aggregate. */
629 String_Id gnat_string = Strval (gnat_node);
630 int length = String_Length (gnat_string);
631 int i;
632 tree gnu_list = NULL_TREE;
633
634 for (i = 0; i < length; i++)
635 gnu_list
636 = tree_cons (NULL_TREE,
637 convert (TREE_TYPE (gnu_result_type),
638 build_int_2 (Get_String_Char (gnat_string,
639 i + 1),
640 0)),
641 gnu_list);
642
643 gnu_result
644 = gnat_build_constructor (gnu_result_type, nreverse (gnu_list));
645 }
646 break;
647
648 case N_Pragma:
649 if (type_annotate_only)
650 break;
651
652 /* Check for (and ignore) unrecognized pragma */
653 if (! Is_Pragma_Name (Chars (gnat_node)))
654 break;
655
656 switch (Get_Pragma_Id (Chars (gnat_node)))
657 {
658 case Pragma_Inspection_Point:
659 /* Do nothing at top level: all such variables are already
660 viewable. */
661 if (global_bindings_p ())
662 break;
663
664 set_lineno (gnat_node, 1);
665 for (gnat_temp = First (Pragma_Argument_Associations (gnat_node));
666 Present (gnat_temp);
667 gnat_temp = Next (gnat_temp))
668 {
669 gnu_expr = gnat_to_gnu (Expression (gnat_temp));
670 if (TREE_CODE (gnu_expr) == UNCONSTRAINED_ARRAY_REF)
671 gnu_expr = TREE_OPERAND (gnu_expr, 0);
672
673 gnu_expr = build1 (USE_EXPR, void_type_node, gnu_expr);
674 TREE_SIDE_EFFECTS (gnu_expr) = 1;
675 expand_expr_stmt (gnu_expr);
676 }
677 break;
678
679 case Pragma_Optimize:
680 switch (Chars (Expression
681 (First (Pragma_Argument_Associations (gnat_node)))))
682 {
683 case Name_Time: case Name_Space:
684 if (optimize == 0)
685 post_error ("insufficient -O value?", gnat_node);
686 break;
687
688 case Name_Off:
689 if (optimize != 0)
690 post_error ("must specify -O0?", gnat_node);
691 break;
692
693 default:
694 gigi_abort (331);
695 break;
696 }
697 break;
698
699 case Pragma_Reviewable:
700 if (write_symbols == NO_DEBUG)
701 post_error ("must specify -g?", gnat_node);
702 break;
703 }
704 break;
705
706 /**************************************/
707 /* Chapter 3: Declarations and Types: */
708 /**************************************/
709
710 case N_Subtype_Declaration:
711 case N_Full_Type_Declaration:
712 case N_Incomplete_Type_Declaration:
713 case N_Private_Type_Declaration:
714 case N_Private_Extension_Declaration:
715 case N_Task_Type_Declaration:
716 process_type (Defining_Entity (gnat_node));
717 break;
718
719 case N_Object_Declaration:
720 case N_Exception_Declaration:
721 gnat_temp = Defining_Entity (gnat_node);
722
723 /* If we are just annotating types and this object has an unconstrained
724 or task type, don't elaborate it. */
725 if (type_annotate_only
726 && (((Is_Array_Type (Etype (gnat_temp))
727 || Is_Record_Type (Etype (gnat_temp)))
728 && ! Is_Constrained (Etype (gnat_temp)))
729 || Is_Concurrent_Type (Etype (gnat_temp))))
730 break;
731
732 if (Present (Expression (gnat_node))
733 && ! (Nkind (gnat_node) == N_Object_Declaration
734 && No_Initialization (gnat_node))
735 && (! type_annotate_only
736 || Compile_Time_Known_Value (Expression (gnat_node))))
737 {
738 gnu_expr = gnat_to_gnu (Expression (gnat_node));
739 if (Do_Range_Check (Expression (gnat_node)))
740 gnu_expr = emit_range_check (gnu_expr, Etype (gnat_temp));
741
742 /* If this object has its elaboration delayed, we must force
743 evaluation of GNU_EXPR right now and save it for when the object
744 is frozen. */
745 if (Present (Freeze_Node (gnat_temp)))
746 {
747 if ((Is_Public (gnat_temp) || global_bindings_p ())
748 && ! TREE_CONSTANT (gnu_expr))
749 gnu_expr
750 = create_var_decl (create_concat_name (gnat_temp, "init"),
751 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
752 0, Is_Public (gnat_temp), 0, 0, 0);
753 else
754 gnu_expr = maybe_variable (gnu_expr, Expression (gnat_node));
755
756 save_gnu_tree (gnat_node, gnu_expr, 1);
757 }
758 }
759 else
760 gnu_expr = 0;
761
762 if (type_annotate_only && gnu_expr != 0
763 && TREE_CODE (gnu_expr) == ERROR_MARK)
764 gnu_expr = 0;
765
766 if (No (Freeze_Node (gnat_temp)))
767 gnat_to_gnu_entity (gnat_temp, gnu_expr, 1);
768 break;
769
770 case N_Object_Renaming_Declaration:
771
772 gnat_temp = Defining_Entity (gnat_node);
773
774 /* Don't do anything if this renaming is handled by the front end.
775 or if we are just annotating types and this object has a
776 composite or task type, don't elaborate it. */
777 if (! Is_Renaming_Of_Object (gnat_temp)
778 && ! (type_annotate_only
779 && (Is_Array_Type (Etype (gnat_temp))
780 || Is_Record_Type (Etype (gnat_temp))
781 || Is_Concurrent_Type (Etype (gnat_temp)))))
782 {
783 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_temp));
784 gnat_to_gnu_entity (gnat_temp, gnu_expr, 1);
785 }
786 break;
787
788 case N_Implicit_Label_Declaration:
789 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
790 break;
791
792 case N_Subprogram_Renaming_Declaration:
793 case N_Package_Renaming_Declaration:
794 case N_Exception_Renaming_Declaration:
795 case N_Number_Declaration:
796 /* These are fully handled in the front end. */
797 break;
798
799 /*************************************/
800 /* Chapter 4: Names and Expressions: */
801 /*************************************/
802
803 case N_Explicit_Dereference:
804 gnu_result = gnat_to_gnu (Prefix (gnat_node));
805 gnu_result_type = get_unpadded_type (Etype (gnat_node));
806
807 /* Emit access check if necessary */
808 if (Do_Access_Check (gnat_node))
809 gnu_result = emit_access_check (gnu_result);
810
811 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
812 break;
813
814 case N_Indexed_Component:
815 {
816 tree gnu_array_object = gnat_to_gnu (Prefix (gnat_node));
817 tree gnu_type;
818 int ndim;
819 int i;
820 Node_Id *gnat_expr_array;
821
822 /* Emit access check if necessary */
823 if (Do_Access_Check (gnat_node))
824 gnu_array_object = emit_access_check (gnu_array_object);
825
826 gnu_array_object = maybe_implicit_deref (gnu_array_object);
827 gnu_array_object = maybe_unconstrained_array (gnu_array_object);
828
829 /* If we got a padded type, remove it too. */
830 if (TREE_CODE (TREE_TYPE (gnu_array_object)) == RECORD_TYPE
831 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_array_object)))
832 gnu_array_object
833 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_array_object))),
834 gnu_array_object);
835
836 gnu_result = gnu_array_object;
837
838 /* First compute the number of dimensions of the array, then
839 fill the expression array, the order depending on whether
840 this is a Convention_Fortran array or not. */
841 for (ndim = 1, gnu_type = TREE_TYPE (gnu_array_object);
842 TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
843 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type));
844 ndim++, gnu_type = TREE_TYPE (gnu_type))
845 ;
846
847 gnat_expr_array = (Node_Id *) alloca (ndim * sizeof (Node_Id));
848
849 if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object)))
850 for (i = ndim - 1, gnat_temp = First (Expressions (gnat_node));
851 i >= 0;
852 i--, gnat_temp = Next (gnat_temp))
853 gnat_expr_array[i] = gnat_temp;
854 else
855 for (i = 0, gnat_temp = First (Expressions (gnat_node));
856 i < ndim;
857 i++, gnat_temp = Next (gnat_temp))
858 gnat_expr_array[i] = gnat_temp;
859
860 for (i = 0, gnu_type = TREE_TYPE (gnu_array_object);
861 i < ndim; i++, gnu_type = TREE_TYPE (gnu_type))
862 {
863 if (TREE_CODE (gnu_type) != ARRAY_TYPE)
864 gigi_abort (307);
865
866 gnat_temp = gnat_expr_array[i];
867 gnu_expr = gnat_to_gnu (gnat_temp);
868
869 if (Do_Range_Check (gnat_temp))
870 gnu_expr
871 = emit_index_check
872 (gnu_array_object, gnu_expr,
873 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
874 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))));
875
876 gnu_result = build_binary_op (ARRAY_REF, NULL_TREE,
877 gnu_result, gnu_expr);
878 }
879 }
880
881 gnu_result_type = get_unpadded_type (Etype (gnat_node));
882 break;
883
884 case N_Slice:
885 {
886 tree gnu_type;
887 Node_Id gnat_range_node = Discrete_Range (gnat_node);
888
889 gnu_result = gnat_to_gnu (Prefix (gnat_node));
890 gnu_result_type = get_unpadded_type (Etype (gnat_node));
891
892 /* Emit access check if necessary */
893 if (Do_Access_Check (gnat_node))
894 gnu_result = emit_access_check (gnu_result);
895
896 /* Do any implicit dereferences of the prefix and do any needed
897 range check. */
898 gnu_result = maybe_implicit_deref (gnu_result);
899 gnu_result = maybe_unconstrained_array (gnu_result);
900 gnu_type = TREE_TYPE (gnu_result);
901 if (Do_Range_Check (gnat_range_node))
902 {
903 /* Get the bounds of the slice. */
904 tree gnu_index_type
905 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_result_type));
906 tree gnu_min_expr = TYPE_MIN_VALUE (gnu_index_type);
907 tree gnu_max_expr = TYPE_MAX_VALUE (gnu_index_type);
908 tree gnu_expr_l, gnu_expr_h, gnu_expr_type;
909
910 /* Check to see that the minimum slice value is in range */
911 gnu_expr_l
912 = emit_index_check
913 (gnu_result, gnu_min_expr,
914 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
915 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))));
916
917 /* Check to see that the maximum slice value is in range */
918 gnu_expr_h
919 = emit_index_check
920 (gnu_result, gnu_max_expr,
921 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
922 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))));
923
924 /* Derive a good type to convert everything too */
925 gnu_expr_type = get_base_type (TREE_TYPE (gnu_expr_l));
926
927 /* Build a compound expression that does the range checks */
928 gnu_expr
929 = build_binary_op (COMPOUND_EXPR, gnu_expr_type,
930 convert (gnu_expr_type, gnu_expr_h),
931 convert (gnu_expr_type, gnu_expr_l));
932
933 /* Build a conditional expression that returns the range checks
934 expression if the slice range is not null (max >= min) or
935 returns the min if the slice range is null */
936 gnu_expr
937 = fold (build (COND_EXPR, gnu_expr_type,
938 build_binary_op (GE_EXPR, gnu_expr_type,
939 convert (gnu_expr_type,
940 gnu_max_expr),
941 convert (gnu_expr_type,
942 gnu_min_expr)),
943 gnu_expr, gnu_min_expr));
944 }
945 else
946 gnu_expr = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
947
948 gnu_result = build_binary_op (ARRAY_RANGE_REF, gnu_result_type,
949 gnu_result, gnu_expr);
950 }
951 break;
952
953 case N_Selected_Component:
954 {
955 tree gnu_prefix = gnat_to_gnu (Prefix (gnat_node));
956 Entity_Id gnat_field = Entity (Selector_Name (gnat_node));
957 Entity_Id gnat_pref_type = Etype (Prefix (gnat_node));
958 tree gnu_field;
959
960 while (IN (Ekind (gnat_pref_type), Incomplete_Or_Private_Kind)
961 || IN (Ekind (gnat_pref_type), Access_Kind))
962 {
963 if (IN (Ekind (gnat_pref_type), Incomplete_Or_Private_Kind))
964 gnat_pref_type = Underlying_Type (gnat_pref_type);
965 else if (IN (Ekind (gnat_pref_type), Access_Kind))
966 gnat_pref_type = Designated_Type (gnat_pref_type);
967 }
968
969 if (Do_Access_Check (gnat_node))
970 gnu_prefix = emit_access_check (gnu_prefix);
971
972 gnu_prefix = maybe_implicit_deref (gnu_prefix);
973
974 /* For discriminant references in tagged types always substitute the
975 corresponding discriminant as the actual selected component. */
976
977 if (Is_Tagged_Type (gnat_pref_type))
978 while (Present (Corresponding_Discriminant (gnat_field)))
979 gnat_field = Corresponding_Discriminant (gnat_field);
980
981 /* For discriminant references of untagged types always substitute the
982 corresponding girder discriminant. */
983
984 else if (Present (Corresponding_Discriminant (gnat_field)))
985 gnat_field = Original_Record_Component (gnat_field);
986
987 /* Handle extracting the real or imaginary part of a complex.
988 The real part is the first field and the imaginary the last. */
989
990 if (TREE_CODE (TREE_TYPE (gnu_prefix)) == COMPLEX_TYPE)
991 gnu_result = build_unary_op (Present (Next_Entity (gnat_field))
992 ? REALPART_EXPR : IMAGPART_EXPR,
993 NULL_TREE, gnu_prefix);
994 else
995 {
996 gnu_field = gnat_to_gnu_entity (gnat_field, NULL_TREE, 0);
997
998 /* If there are discriminants, the prefix might be
999 evaluated more than once, which is a problem if it has
1000 side-effects. */
1001 if (Has_Discriminants (Is_Access_Type (Etype (Prefix (gnat_node)))
1002 ? Designated_Type (Etype
1003 (Prefix (gnat_node)))
1004 : Etype (Prefix (gnat_node))))
1005 gnu_prefix = gnat_stabilize_reference (gnu_prefix, 0);
1006
1007 /* Emit discriminant check if necessary. */
1008 if (Do_Discriminant_Check (gnat_node))
1009 gnu_prefix = emit_discriminant_check (gnu_prefix, gnat_node);
1010 gnu_result
1011 = build_component_ref (gnu_prefix, NULL_TREE, gnu_field);
1012 }
1013
1014 if (gnu_result == 0)
1015 gigi_abort (308);
1016
1017 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1018 }
1019 break;
1020
1021 case N_Attribute_Reference:
1022 {
1023 /* The attribute designator (like an enumeration value). */
1024 int attribute = Get_Attribute_Id (Attribute_Name (gnat_node));
1025 int prefix_unused = 0;
1026 tree gnu_prefix;
1027 tree gnu_type;
1028
1029 /* The Elab_Spec and Elab_Body attributes are special in that
1030 Prefix is a unit, not an object with a GCC equivalent. Similarly
1031 for Elaborated, since that variable isn't otherwise known. */
1032 if (attribute == Attr_Elab_Body || attribute == Attr_Elab_Spec)
1033 {
1034 gnu_prefix
1035 = create_subprog_decl
1036 (create_concat_name (Entity (Prefix (gnat_node)),
1037 attribute == Attr_Elab_Body
1038 ? "elabb" : "elabs"),
1039 NULL_TREE, void_ftype, NULL_TREE, 0, 1, 1, 0);
1040 return gnu_prefix;
1041 }
1042
1043 gnu_prefix = gnat_to_gnu (Prefix (gnat_node));
1044 gnu_type = TREE_TYPE (gnu_prefix);
1045
1046 /* If the input is a NULL_EXPR, make a new one. */
1047 if (TREE_CODE (gnu_prefix) == NULL_EXPR)
1048 {
1049 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1050 gnu_result = build1 (NULL_EXPR, gnu_result_type,
1051 TREE_OPERAND (gnu_prefix, 0));
1052 break;
1053 }
1054
1055 switch (attribute)
1056 {
1057 case Attr_Pos:
1058 case Attr_Val:
1059 /* These are just conversions until since representation
1060 clauses for enumerations are handled in the front end. */
1061 {
1062 int check_p = Do_Range_Check (First (Expressions (gnat_node)));
1063
1064 gnu_result = gnat_to_gnu (First (Expressions (gnat_node)));
1065 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1066 gnu_result = convert_with_check (Etype (gnat_node), gnu_result,
1067 check_p, check_p, 1);
1068 }
1069 break;
1070
1071 case Attr_Pred:
1072 case Attr_Succ:
1073 /* These just add or subject the constant 1. Representation
1074 clauses for enumerations are handled in the front-end. */
1075 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
1076 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1077
1078 if (Do_Range_Check (First (Expressions (gnat_node))))
1079 {
1080 gnu_expr = protect_multiple_eval (gnu_expr);
1081 gnu_expr
1082 = emit_check
1083 (build_binary_op (EQ_EXPR, integer_type_node,
1084 gnu_expr,
1085 attribute == Attr_Pred
1086 ? TYPE_MIN_VALUE (gnu_result_type)
1087 : TYPE_MAX_VALUE (gnu_result_type)),
1088 gnu_expr, CE_Range_Check_Failed);
1089 }
1090
1091 gnu_result
1092 = build_binary_op (attribute == Attr_Pred
1093 ? MINUS_EXPR : PLUS_EXPR,
1094 gnu_result_type, gnu_expr,
1095 convert (gnu_result_type, integer_one_node));
1096 break;
1097
1098 case Attr_Address:
1099 case Attr_Unrestricted_Access:
1100
1101 /* Conversions don't change something's address but can cause
1102 us to miss the COMPONENT_REF case below, so strip them off. */
1103 gnu_prefix
1104 = remove_conversions (gnu_prefix,
1105 ! Must_Be_Byte_Aligned (gnat_node));
1106
1107 /* If we are taking 'Address of an unconstrained object,
1108 this is the pointer to the underlying array. */
1109 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
1110
1111 /* ... fall through ... */
1112
1113 case Attr_Access:
1114 case Attr_Unchecked_Access:
1115 case Attr_Code_Address:
1116
1117 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1118 gnu_result
1119 = build_unary_op (((attribute == Attr_Address
1120 || attribute == Attr_Unrestricted_Access)
1121 && ! Must_Be_Byte_Aligned (gnat_node))
1122 ? ATTR_ADDR_EXPR : ADDR_EXPR,
1123 gnu_result_type, gnu_prefix);
1124
1125 /* For 'Code_Address, find an inner ADDR_EXPR and mark it
1126 so that we don't try to build a trampoline. */
1127 if (attribute == Attr_Code_Address)
1128 {
1129 for (gnu_expr = gnu_result;
1130 TREE_CODE (gnu_expr) == NOP_EXPR
1131 || TREE_CODE (gnu_expr) == CONVERT_EXPR;
1132 gnu_expr = TREE_OPERAND (gnu_expr, 0))
1133 TREE_CONSTANT (gnu_expr) = 1;
1134 ;
1135
1136 if (TREE_CODE (gnu_expr) == ADDR_EXPR)
1137 TREE_STATIC (gnu_expr) = TREE_CONSTANT (gnu_expr) = 1;
1138 }
1139
1140 break;
1141
1142 case Attr_Size:
1143 case Attr_Object_Size:
1144 case Attr_Value_Size:
1145 case Attr_Max_Size_In_Storage_Elements:
1146
1147 gnu_expr = gnu_prefix;
1148
1149 /* Remove NOPS from gnu_expr and conversions from gnu_prefix.
1150 We only use GNU_EXPR to see if a COMPONENT_REF was involved. */
1151 while (TREE_CODE (gnu_expr) == NOP_EXPR)
1152 gnu_expr = TREE_OPERAND (gnu_expr, 0);
1153
1154 gnu_prefix = remove_conversions (gnu_prefix, 1);
1155 prefix_unused = 1;
1156 gnu_type = TREE_TYPE (gnu_prefix);
1157
1158 /* Replace an unconstrained array type with the type of the
1159 underlying array. We can't do this with a call to
1160 maybe_unconstrained_array since we may have a TYPE_DECL.
1161 For 'Max_Size_In_Storage_Elements, use the record type
1162 that will be used to allocate the object and its template. */
1163
1164 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
1165 {
1166 gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type);
1167 if (attribute != Attr_Max_Size_In_Storage_Elements)
1168 gnu_type = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)));
1169 }
1170
1171 /* If we are looking for the size of a field, return the
1172 field size. Otherwise, if the prefix is an object,
1173 or if 'Object_Size or 'Max_Size_In_Storage_Elements has
1174 been specified, the result is the GCC size of the type.
1175 Otherwise, the result is the RM_Size of the type. */
1176 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
1177 gnu_result = DECL_SIZE (TREE_OPERAND (gnu_prefix, 1));
1178 else if (TREE_CODE (gnu_prefix) != TYPE_DECL
1179 || attribute == Attr_Object_Size
1180 || attribute == Attr_Max_Size_In_Storage_Elements)
1181 {
1182 /* If this is a padded type, the GCC size isn't relevant
1183 to the programmer. Normally, what we want is the RM_Size,
1184 which was set from the specified size, but if it was not
1185 set, we want the size of the relevant field. Using the MAX
1186 of those two produces the right result in all case. Don't
1187 use the size of the field if it's a self-referential type,
1188 since that's never what's wanted. */
1189 if (TREE_CODE (gnu_type) == RECORD_TYPE
1190 && TYPE_IS_PADDING_P (gnu_type)
1191 && TREE_CODE (gnu_expr) == COMPONENT_REF)
1192 {
1193 gnu_result = rm_size (gnu_type);
1194 if (! (contains_placeholder_p
1195 (DECL_SIZE (TREE_OPERAND (gnu_expr, 1)))))
1196 gnu_result
1197 = size_binop (MAX_EXPR, gnu_result,
1198 DECL_SIZE (TREE_OPERAND (gnu_expr, 1)));
1199 }
1200 else
1201 gnu_result = TYPE_SIZE (gnu_type);
1202 }
1203 else
1204 gnu_result = rm_size (gnu_type);
1205
1206 if (gnu_result == 0)
1207 gigi_abort (325);
1208
1209 /* Deal with a self-referential size by returning the maximum
1210 size for a type and by qualifying the size with
1211 the object for 'Size of an object. */
1212
1213 if (TREE_CODE (gnu_result) != INTEGER_CST
1214 && contains_placeholder_p (gnu_result))
1215 {
1216 if (TREE_CODE (gnu_prefix) != TYPE_DECL)
1217 gnu_result = build (WITH_RECORD_EXPR, TREE_TYPE (gnu_result),
1218 gnu_result, gnu_prefix);
1219 else
1220 gnu_result = max_size (gnu_result, 1);
1221 }
1222
1223 /* If the type contains a template, subtract the size of the
1224 template. */
1225 if (TREE_CODE (gnu_type) == RECORD_TYPE
1226 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1227 gnu_result = size_binop (MINUS_EXPR, gnu_result,
1228 DECL_SIZE (TYPE_FIELDS (gnu_type)));
1229
1230 /* If the type contains a template, subtract the size of the
1231 template. */
1232 if (TREE_CODE (gnu_type) == RECORD_TYPE
1233 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1234 gnu_result = size_binop (MINUS_EXPR, gnu_result,
1235 DECL_SIZE (TYPE_FIELDS (gnu_type)));
1236
1237 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1238
1239 /* Always perform division using unsigned arithmetic as the
1240 size cannot be negative, but may be an overflowed positive
1241 value. This provides correct results for sizes up to 512 MB.
1242 ??? Size should be calculated in storage elements directly. */
1243
1244 if (attribute == Attr_Max_Size_In_Storage_Elements)
1245 gnu_result = convert (sizetype,
1246 fold (build (CEIL_DIV_EXPR, bitsizetype,
1247 gnu_result,
1248 bitsize_unit_node)));
1249 break;
1250
1251 case Attr_Alignment:
1252 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
1253 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))
1254 == RECORD_TYPE)
1255 && (TYPE_IS_PADDING_P
1256 (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))))
1257 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
1258
1259 gnu_type = TREE_TYPE (gnu_prefix);
1260 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1261 prefix_unused = 1;
1262
1263 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
1264 gnu_result
1265 = size_int (DECL_ALIGN (TREE_OPERAND (gnu_prefix, 1)));
1266 else
1267 gnu_result = size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT);
1268 break;
1269
1270 case Attr_First:
1271 case Attr_Last:
1272 case Attr_Range_Length:
1273 prefix_unused = 1;
1274
1275 if (INTEGRAL_TYPE_P (gnu_type)
1276 || TREE_CODE (gnu_type) == REAL_TYPE)
1277 {
1278 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1279
1280 if (attribute == Attr_First)
1281 gnu_result = TYPE_MIN_VALUE (gnu_type);
1282 else if (attribute == Attr_Last)
1283 gnu_result = TYPE_MAX_VALUE (gnu_type);
1284 else
1285 gnu_result
1286 = build_binary_op
1287 (MAX_EXPR, get_base_type (gnu_result_type),
1288 build_binary_op
1289 (PLUS_EXPR, get_base_type (gnu_result_type),
1290 build_binary_op (MINUS_EXPR,
1291 get_base_type (gnu_result_type),
1292 convert (gnu_result_type,
1293 TYPE_MAX_VALUE (gnu_type)),
1294 convert (gnu_result_type,
1295 TYPE_MIN_VALUE (gnu_type))),
1296 convert (gnu_result_type, integer_one_node)),
1297 convert (gnu_result_type, integer_zero_node));
1298
1299 break;
1300 }
1301 /* ... fall through ... */
1302 case Attr_Length:
1303 {
1304 int Dimension
1305 = (Present (Expressions (gnat_node))
1306 ? UI_To_Int (Intval (First (Expressions (gnat_node))))
1307 : 1);
1308
1309 /* Emit access check if necessary */
1310 if (Do_Access_Check (gnat_node))
1311 gnu_prefix = emit_access_check (gnu_prefix);
1312
1313 /* Make sure any implicit dereference gets done. */
1314 gnu_prefix = maybe_implicit_deref (gnu_prefix);
1315 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
1316 gnu_type = TREE_TYPE (gnu_prefix);
1317 prefix_unused = 1;
1318 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1319
1320 if (TYPE_CONVENTION_FORTRAN_P (gnu_type))
1321 {
1322 int ndim;
1323 tree gnu_type_temp;
1324
1325 for (ndim = 1, gnu_type_temp = gnu_type;
1326 TREE_CODE (TREE_TYPE (gnu_type_temp)) == ARRAY_TYPE
1327 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type_temp));
1328 ndim++, gnu_type_temp = TREE_TYPE (gnu_type_temp))
1329 ;
1330
1331 Dimension = ndim + 1 - Dimension;
1332 }
1333
1334 for (; Dimension > 1; Dimension--)
1335 gnu_type = TREE_TYPE (gnu_type);
1336
1337 if (TREE_CODE (gnu_type) != ARRAY_TYPE)
1338 gigi_abort (309);
1339
1340 if (attribute == Attr_First)
1341 gnu_result
1342 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
1343 else if (attribute == Attr_Last)
1344 gnu_result
1345 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
1346 else
1347 /* 'Length or 'Range_Length. */
1348 {
1349 tree gnu_compute_type
1350 = gnat_signed_or_unsigned_type
1351 (0, get_base_type (gnu_result_type));
1352
1353 gnu_result
1354 = build_binary_op
1355 (MAX_EXPR, gnu_compute_type,
1356 build_binary_op
1357 (PLUS_EXPR, gnu_compute_type,
1358 build_binary_op
1359 (MINUS_EXPR, gnu_compute_type,
1360 convert (gnu_compute_type,
1361 TYPE_MAX_VALUE
1362 (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)))),
1363 convert (gnu_compute_type,
1364 TYPE_MIN_VALUE
1365 (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))))),
1366 convert (gnu_compute_type, integer_one_node)),
1367 convert (gnu_compute_type, integer_zero_node));
1368 }
1369
1370 /* If this has a PLACEHOLDER_EXPR, qualify it by the object
1371 we are handling. Note that these attributes could not
1372 have been used on an unconstrained array type. */
1373 if (TREE_CODE (gnu_result) != INTEGER_CST
1374 && contains_placeholder_p (gnu_result))
1375 gnu_result = build (WITH_RECORD_EXPR, TREE_TYPE (gnu_result),
1376 gnu_result, gnu_prefix);
1377
1378 break;
1379 }
1380
1381 case Attr_Bit_Position:
1382 case Attr_Position:
1383 case Attr_First_Bit:
1384 case Attr_Last_Bit:
1385 case Attr_Bit:
1386 {
1387 HOST_WIDE_INT bitsize;
1388 HOST_WIDE_INT bitpos;
1389 tree gnu_offset;
1390 tree gnu_field_bitpos;
1391 tree gnu_field_offset;
1392 tree gnu_inner;
1393 enum machine_mode mode;
1394 int unsignedp, volatilep;
1395
1396 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1397 gnu_prefix = remove_conversions (gnu_prefix, 1);
1398 prefix_unused = 1;
1399
1400 /* We can have 'Bit on any object, but if it isn't a
1401 COMPONENT_REF, the result is zero. Do not allow
1402 'Bit on a bare component, though. */
1403 if (attribute == Attr_Bit
1404 && TREE_CODE (gnu_prefix) != COMPONENT_REF
1405 && TREE_CODE (gnu_prefix) != FIELD_DECL)
1406 {
1407 gnu_result = integer_zero_node;
1408 break;
1409 }
1410
1411 else if (TREE_CODE (gnu_prefix) != COMPONENT_REF
1412 && ! (attribute == Attr_Bit_Position
1413 && TREE_CODE (gnu_prefix) == FIELD_DECL))
1414 gigi_abort (310);
1415
1416 get_inner_reference (gnu_prefix, &bitsize, &bitpos, &gnu_offset,
1417 &mode, &unsignedp, &volatilep);
1418
1419 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
1420 {
1421 gnu_field_bitpos
1422 = bit_position (TREE_OPERAND (gnu_prefix, 1));
1423 gnu_field_offset
1424 = byte_position (TREE_OPERAND (gnu_prefix, 1));
1425
1426 for (gnu_inner = TREE_OPERAND (gnu_prefix, 0);
1427 TREE_CODE (gnu_inner) == COMPONENT_REF
1428 && DECL_INTERNAL_P (TREE_OPERAND (gnu_inner, 1));
1429 gnu_inner = TREE_OPERAND (gnu_inner, 0))
1430 {
1431 gnu_field_bitpos
1432 = size_binop (PLUS_EXPR, gnu_field_bitpos,
1433 bit_position (TREE_OPERAND (gnu_inner,
1434 1)));
1435 gnu_field_offset
1436 = size_binop (PLUS_EXPR, gnu_field_offset,
1437 byte_position (TREE_OPERAND (gnu_inner,
1438 1)));
1439 }
1440 }
1441 else if (TREE_CODE (gnu_prefix) == FIELD_DECL)
1442 {
1443 gnu_field_bitpos = bit_position (gnu_prefix);
1444 gnu_field_offset = byte_position (gnu_prefix);
1445 }
1446 else
1447 {
1448 gnu_field_bitpos = bitsize_zero_node;
1449 gnu_field_offset = size_zero_node;
1450 }
1451
1452 switch (attribute)
1453 {
1454 case Attr_Position:
1455 gnu_result = gnu_field_offset;
1456 break;
1457
1458 case Attr_First_Bit:
1459 case Attr_Bit:
1460 gnu_result = size_int (bitpos % BITS_PER_UNIT);
1461 break;
1462
1463 case Attr_Last_Bit:
1464 gnu_result = bitsize_int (bitpos % BITS_PER_UNIT);
1465 gnu_result
1466 = size_binop (PLUS_EXPR, gnu_result,
1467 TYPE_SIZE (TREE_TYPE (gnu_prefix)));
1468 gnu_result = size_binop (MINUS_EXPR, gnu_result,
1469 bitsize_one_node);
1470 break;
1471
1472 case Attr_Bit_Position:
1473 gnu_result = gnu_field_bitpos;
1474 break;
1475 }
1476
1477 /* If this has a PLACEHOLDER_EXPR, qualify it by the object
1478 we are handling. */
1479 if (TREE_CODE (gnu_result) != INTEGER_CST
1480 && contains_placeholder_p (gnu_result))
1481 gnu_result = build (WITH_RECORD_EXPR, TREE_TYPE (gnu_result),
1482 gnu_result, gnu_prefix);
1483
1484 break;
1485 }
1486
1487 case Attr_Min:
1488 case Attr_Max:
1489 gnu_lhs = gnat_to_gnu (First (Expressions (gnat_node)));
1490 gnu_rhs = gnat_to_gnu (Next (First (Expressions (gnat_node))));
1491
1492 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1493 gnu_result = build_binary_op (attribute == Attr_Min
1494 ? MIN_EXPR : MAX_EXPR,
1495 gnu_result_type, gnu_lhs, gnu_rhs);
1496 break;
1497
1498 case Attr_Passed_By_Reference:
1499 gnu_result = size_int (default_pass_by_ref (gnu_type)
1500 || must_pass_by_ref (gnu_type));
1501 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1502 break;
1503
1504 case Attr_Component_Size:
1505 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
1506 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))
1507 == RECORD_TYPE)
1508 && (TYPE_IS_PADDING_P
1509 (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))))
1510 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
1511
1512 gnu_prefix = maybe_implicit_deref (gnu_prefix);
1513 gnu_type = TREE_TYPE (gnu_prefix);
1514
1515 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
1516 gnu_type
1517 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type))));
1518
1519 while (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
1520 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
1521 gnu_type = TREE_TYPE (gnu_type);
1522
1523 if (TREE_CODE (gnu_type) != ARRAY_TYPE)
1524 gigi_abort (330);
1525
1526 /* Note this size cannot be self-referential. */
1527 gnu_result = TYPE_SIZE (TREE_TYPE (gnu_type));
1528 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1529 prefix_unused = 1;
1530 break;
1531
1532 case Attr_Null_Parameter:
1533 /* This is just a zero cast to the pointer type for
1534 our prefix and dereferenced. */
1535 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1536 gnu_result
1537 = build_unary_op (INDIRECT_REF, NULL_TREE,
1538 convert (build_pointer_type (gnu_result_type),
1539 integer_zero_node));
1540 TREE_PRIVATE (gnu_result) = 1;
1541 break;
1542
1543 case Attr_Mechanism_Code:
1544 {
1545 int code;
1546 Entity_Id gnat_obj = Entity (Prefix (gnat_node));
1547
1548 prefix_unused = 1;
1549 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1550 if (Present (Expressions (gnat_node)))
1551 {
1552 int i = UI_To_Int (Intval (First (Expressions (gnat_node))));
1553
1554 for (gnat_obj = First_Formal (gnat_obj); i > 1;
1555 i--, gnat_obj = Next_Formal (gnat_obj))
1556 ;
1557 }
1558
1559 code = Mechanism (gnat_obj);
1560 if (code == Default)
1561 code = ((present_gnu_tree (gnat_obj)
1562 && (DECL_BY_REF_P (get_gnu_tree (gnat_obj))
1563 || (DECL_BY_COMPONENT_PTR_P
1564 (get_gnu_tree (gnat_obj)))))
1565 ? By_Reference : By_Copy);
1566 gnu_result = convert (gnu_result_type, size_int (- code));
1567 }
1568 break;
1569
1570 default:
1571 /* Say we have an unimplemented attribute. Then set the
1572 value to be returned to be a zero and hope that's something
1573 we can convert to the type of this attribute. */
1574
1575 post_error ("unimplemented attribute", gnat_node);
1576 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1577 gnu_result = integer_zero_node;
1578 break;
1579 }
1580
1581 /* If this is an attribute where the prefix was unused,
1582 force a use of it if it has a side-effect. But don't do it if
1583 the prefix is just an entity name. However, if an access check
1584 is needed, we must do it. See second example in AARM 11.6(5.e). */
1585 if (prefix_unused && TREE_SIDE_EFFECTS (gnu_prefix)
1586 && (! Is_Entity_Name (Prefix (gnat_node))
1587 || Do_Access_Check (gnat_node)))
1588 gnu_result = fold (build (COMPOUND_EXPR, TREE_TYPE (gnu_result),
1589 gnu_prefix, gnu_result));
1590 }
1591 break;
1592
1593 case N_Reference:
1594 /* Like 'Access as far as we are concerned. */
1595 gnu_result = gnat_to_gnu (Prefix (gnat_node));
1596 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
1597 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1598 break;
1599
1600 case N_Aggregate:
1601 case N_Extension_Aggregate:
1602 {
1603 tree gnu_aggr_type;
1604
1605 /* ??? It is wrong to evaluate the type now, but there doesn't
1606 seem to be any other practical way of doing it. */
1607
1608 gnu_aggr_type = gnu_result_type
1609 = get_unpadded_type (Etype (gnat_node));
1610
1611 if (TREE_CODE (gnu_result_type) == RECORD_TYPE
1612 && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type))
1613 gnu_aggr_type
1614 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_result_type)));
1615
1616 if (Null_Record_Present (gnat_node))
1617 gnu_result = gnat_build_constructor (gnu_aggr_type, NULL_TREE);
1618
1619 else if (TREE_CODE (gnu_aggr_type) == RECORD_TYPE)
1620 gnu_result
1621 = assoc_to_constructor (First (Component_Associations (gnat_node)),
1622 gnu_aggr_type);
1623 else if (TREE_CODE (gnu_aggr_type) == UNION_TYPE)
1624 {
1625 /* The first element is the discrimant, which we ignore. The
1626 next is the field we're building. Convert the expression
1627 to the type of the field and then to the union type. */
1628 Node_Id gnat_assoc
1629 = Next (First (Component_Associations (gnat_node)));
1630 Entity_Id gnat_field = Entity (First (Choices (gnat_assoc)));
1631 tree gnu_field_type
1632 = TREE_TYPE (gnat_to_gnu_entity (gnat_field, NULL_TREE, 0));
1633
1634 gnu_result = convert (gnu_field_type,
1635 gnat_to_gnu (Expression (gnat_assoc)));
1636 }
1637 else if (TREE_CODE (gnu_aggr_type) == ARRAY_TYPE)
1638 gnu_result = pos_to_constructor (First (Expressions (gnat_node)),
1639 gnu_aggr_type,
1640 Component_Type (Etype (gnat_node)));
1641 else if (TREE_CODE (gnu_aggr_type) == COMPLEX_TYPE)
1642 gnu_result
1643 = build_binary_op
1644 (COMPLEX_EXPR, gnu_aggr_type,
1645 gnat_to_gnu (Expression (First
1646 (Component_Associations (gnat_node)))),
1647 gnat_to_gnu (Expression
1648 (Next
1649 (First (Component_Associations (gnat_node))))));
1650 else
1651 gigi_abort (312);
1652
1653 gnu_result = convert (gnu_result_type, gnu_result);
1654 }
1655 break;
1656
1657 case N_Null:
1658 gnu_result = null_pointer_node;
1659 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1660 break;
1661
1662 case N_Type_Conversion:
1663 case N_Qualified_Expression:
1664 /* Get the operand expression. */
1665 gnu_result = gnat_to_gnu (Expression (gnat_node));
1666 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1667
1668 gnu_result
1669 = convert_with_check (Etype (gnat_node), gnu_result,
1670 Do_Overflow_Check (gnat_node),
1671 Do_Range_Check (Expression (gnat_node)),
1672 Nkind (gnat_node) == N_Type_Conversion
1673 && Float_Truncate (gnat_node));
1674 break;
1675
1676 case N_Unchecked_Type_Conversion:
1677 gnu_result = gnat_to_gnu (Expression (gnat_node));
1678 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1679
1680 /* If the result is a pointer type, see if we are improperly
1681 converting to a stricter alignment. */
1682
1683 if (STRICT_ALIGNMENT && POINTER_TYPE_P (gnu_result_type)
1684 && IN (Ekind (Etype (gnat_node)), Access_Kind))
1685 {
1686 unsigned int align = known_alignment (gnu_result);
1687 tree gnu_obj_type = TREE_TYPE (gnu_result_type);
1688 unsigned int oalign
1689 = TREE_CODE (gnu_obj_type) == FUNCTION_TYPE
1690 ? FUNCTION_BOUNDARY : TYPE_ALIGN (gnu_obj_type);
1691
1692 if (align != 0 && align < oalign && ! TYPE_ALIGN_OK (gnu_obj_type))
1693 post_error_ne_tree_2
1694 ("?source alignment (^) < alignment of & (^)",
1695 gnat_node, Designated_Type (Etype (gnat_node)),
1696 size_int (align / BITS_PER_UNIT), oalign / BITS_PER_UNIT);
1697 }
1698
1699 gnu_result = unchecked_convert (gnu_result_type, gnu_result);
1700 break;
1701
1702 case N_In:
1703 case N_Not_In:
1704 {
1705 tree gnu_object = gnat_to_gnu (Left_Opnd (gnat_node));
1706 Node_Id gnat_range = Right_Opnd (gnat_node);
1707 tree gnu_low;
1708 tree gnu_high;
1709
1710 /* GNAT_RANGE is either an N_Range node or an identifier
1711 denoting a subtype. */
1712 if (Nkind (gnat_range) == N_Range)
1713 {
1714 gnu_low = gnat_to_gnu (Low_Bound (gnat_range));
1715 gnu_high = gnat_to_gnu (High_Bound (gnat_range));
1716 }
1717 else if (Nkind (gnat_range) == N_Identifier
1718 || Nkind (gnat_range) == N_Expanded_Name)
1719 {
1720 tree gnu_range_type = get_unpadded_type (Entity (gnat_range));
1721
1722 gnu_low = TYPE_MIN_VALUE (gnu_range_type);
1723 gnu_high = TYPE_MAX_VALUE (gnu_range_type);
1724 }
1725 else
1726 gigi_abort (313);
1727
1728 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1729
1730 /* If LOW and HIGH are identical, perform an equality test.
1731 Otherwise, ensure that GNU_OBJECT is only evaluated once
1732 and perform a full range test. */
1733 if (operand_equal_p (gnu_low, gnu_high, 0))
1734 gnu_result = build_binary_op (EQ_EXPR, gnu_result_type,
1735 gnu_object, gnu_low);
1736 else
1737 {
1738 gnu_object = protect_multiple_eval (gnu_object);
1739 gnu_result
1740 = build_binary_op (TRUTH_ANDIF_EXPR, gnu_result_type,
1741 build_binary_op (GE_EXPR, gnu_result_type,
1742 gnu_object, gnu_low),
1743 build_binary_op (LE_EXPR, gnu_result_type,
1744 gnu_object, gnu_high));
1745 }
1746
1747 if (Nkind (gnat_node) == N_Not_In)
1748 gnu_result = invert_truthvalue (gnu_result);
1749 }
1750 break;
1751
1752 case N_Op_Divide:
1753 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
1754 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
1755 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1756 gnu_result = build_binary_op (FLOAT_TYPE_P (gnu_result_type)
1757 ? RDIV_EXPR
1758 : (Rounded_Result (gnat_node)
1759 ? ROUND_DIV_EXPR : TRUNC_DIV_EXPR),
1760 gnu_result_type, gnu_lhs, gnu_rhs);
1761 break;
1762
1763 case N_And_Then: case N_Or_Else:
1764 {
1765 enum tree_code code = gnu_codes[Nkind (gnat_node)];
1766 tree gnu_rhs_side;
1767
1768 /* The elaboration of the RHS may generate code. If so,
1769 we need to make sure it gets executed after the LHS. */
1770 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
1771 clear_last_expr ();
1772 gnu_rhs_side = expand_start_stmt_expr (/*has_scope=*/1);
1773 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
1774 expand_end_stmt_expr (gnu_rhs_side);
1775 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1776
1777 if (RTL_EXPR_SEQUENCE (gnu_rhs_side) != 0)
1778 gnu_rhs = build (COMPOUND_EXPR, gnu_result_type, gnu_rhs_side,
1779 gnu_rhs);
1780
1781 gnu_result = build_binary_op (code, gnu_result_type, gnu_lhs, gnu_rhs);
1782 }
1783 break;
1784
1785 case N_Op_Or: case N_Op_And: case N_Op_Xor:
1786 /* These can either be operations on booleans or on modular types.
1787 Fall through for boolean types since that's the way GNU_CODES is
1788 set up. */
1789 if (IN (Ekind (Underlying_Type (Etype (gnat_node))),
1790 Modular_Integer_Kind))
1791 {
1792 enum tree_code code
1793 = (Nkind (gnat_node) == N_Op_Or ? BIT_IOR_EXPR
1794 : Nkind (gnat_node) == N_Op_And ? BIT_AND_EXPR
1795 : BIT_XOR_EXPR);
1796
1797 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
1798 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
1799 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1800 gnu_result = build_binary_op (code, gnu_result_type,
1801 gnu_lhs, gnu_rhs);
1802 break;
1803 }
1804
1805 /* ... fall through ... */
1806
1807 case N_Op_Eq: case N_Op_Ne: case N_Op_Lt:
1808 case N_Op_Le: case N_Op_Gt: case N_Op_Ge:
1809 case N_Op_Add: case N_Op_Subtract: case N_Op_Multiply:
1810 case N_Op_Mod: case N_Op_Rem:
1811 case N_Op_Rotate_Left:
1812 case N_Op_Rotate_Right:
1813 case N_Op_Shift_Left:
1814 case N_Op_Shift_Right:
1815 case N_Op_Shift_Right_Arithmetic:
1816 {
1817 enum tree_code code = gnu_codes[Nkind (gnat_node)];
1818 tree gnu_type;
1819
1820 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
1821 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
1822 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
1823
1824 /* If this is a comparison operator, convert any references to
1825 an unconstrained array value into a reference to the
1826 actual array. */
1827 if (TREE_CODE_CLASS (code) == '<')
1828 {
1829 gnu_lhs = maybe_unconstrained_array (gnu_lhs);
1830 gnu_rhs = maybe_unconstrained_array (gnu_rhs);
1831 }
1832
1833 /* If the result type is a private type, its full view may be a
1834 numeric subtype. The representation we need is that of its base
1835 type, given that it is the result of an arithmetic operation. */
1836 else if (Is_Private_Type (Etype (gnat_node)))
1837 gnu_type = gnu_result_type
1838 = get_unpadded_type (Base_Type (Full_View (Etype (gnat_node))));
1839
1840 /* If this is a shift whose count is not guaranteed to be correct,
1841 we need to adjust the shift count. */
1842 if (IN (Nkind (gnat_node), N_Op_Shift)
1843 && ! Shift_Count_OK (gnat_node))
1844 {
1845 tree gnu_count_type = get_base_type (TREE_TYPE (gnu_rhs));
1846 tree gnu_max_shift
1847 = convert (gnu_count_type, TYPE_SIZE (gnu_type));
1848
1849 if (Nkind (gnat_node) == N_Op_Rotate_Left
1850 || Nkind (gnat_node) == N_Op_Rotate_Right)
1851 gnu_rhs = build_binary_op (TRUNC_MOD_EXPR, gnu_count_type,
1852 gnu_rhs, gnu_max_shift);
1853 else if (Nkind (gnat_node) == N_Op_Shift_Right_Arithmetic)
1854 gnu_rhs
1855 = build_binary_op
1856 (MIN_EXPR, gnu_count_type,
1857 build_binary_op (MINUS_EXPR,
1858 gnu_count_type,
1859 gnu_max_shift,
1860 convert (gnu_count_type,
1861 integer_one_node)),
1862 gnu_rhs);
1863 }
1864
1865 /* For right shifts, the type says what kind of shift to do,
1866 so we may need to choose a different type. */
1867 if (Nkind (gnat_node) == N_Op_Shift_Right
1868 && ! TREE_UNSIGNED (gnu_type))
1869 gnu_type = gnat_unsigned_type (gnu_type);
1870 else if (Nkind (gnat_node) == N_Op_Shift_Right_Arithmetic
1871 && TREE_UNSIGNED (gnu_type))
1872 gnu_type = gnat_signed_type (gnu_type);
1873
1874 if (gnu_type != gnu_result_type)
1875 {
1876 gnu_lhs = convert (gnu_type, gnu_lhs);
1877 gnu_rhs = convert (gnu_type, gnu_rhs);
1878 }
1879
1880 gnu_result = build_binary_op (code, gnu_type, gnu_lhs, gnu_rhs);
1881
1882 /* If this is a logical shift with the shift count not verified,
1883 we must return zero if it is too large. We cannot compensate
1884 above in this case. */
1885 if ((Nkind (gnat_node) == N_Op_Shift_Left
1886 || Nkind (gnat_node) == N_Op_Shift_Right)
1887 && ! Shift_Count_OK (gnat_node))
1888 gnu_result
1889 = build_cond_expr
1890 (gnu_type,
1891 build_binary_op (GE_EXPR, integer_type_node,
1892 gnu_rhs,
1893 convert (TREE_TYPE (gnu_rhs),
1894 TYPE_SIZE (gnu_type))),
1895 convert (gnu_type, integer_zero_node),
1896 gnu_result);
1897 }
1898 break;
1899
1900 case N_Conditional_Expression:
1901 {
1902 tree gnu_cond = gnat_to_gnu (First (Expressions (gnat_node)));
1903 tree gnu_true = gnat_to_gnu (Next (First (Expressions (gnat_node))));
1904 tree gnu_false
1905 = gnat_to_gnu (Next (Next (First (Expressions (gnat_node)))));
1906
1907 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1908 gnu_result = build_cond_expr (gnu_result_type,
1909 gnat_truthvalue_conversion (gnu_cond),
1910 gnu_true, gnu_false);
1911 }
1912 break;
1913
1914 case N_Op_Plus:
1915 gnu_result = gnat_to_gnu (Right_Opnd (gnat_node));
1916 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1917 break;
1918
1919 case N_Op_Not:
1920 /* This case can apply to a boolean or a modular type.
1921 Fall through for a boolean operand since GNU_CODES is set
1922 up to handle this. */
1923 if (IN (Ekind (Etype (gnat_node)), Modular_Integer_Kind))
1924 {
1925 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
1926 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1927 gnu_result = build_unary_op (BIT_NOT_EXPR, gnu_result_type,
1928 gnu_expr);
1929 break;
1930 }
1931
1932 /* ... fall through ... */
1933
1934 case N_Op_Minus: case N_Op_Abs:
1935 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
1936
1937 if (Ekind (Etype (gnat_node)) != E_Private_Type)
1938 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1939 else
1940 gnu_result_type = get_unpadded_type (Base_Type
1941 (Full_View (Etype (gnat_node))));
1942
1943 gnu_result = build_unary_op (gnu_codes[Nkind (gnat_node)],
1944 gnu_result_type, gnu_expr);
1945 break;
1946
1947 case N_Allocator:
1948 {
1949 tree gnu_init = 0;
1950 tree gnu_type;
1951
1952 gnat_temp = Expression (gnat_node);
1953
1954 /* The Expression operand can either be an N_Identifier or
1955 Expanded_Name, which must represent a type, or a
1956 N_Qualified_Expression, which contains both the object type and an
1957 initial value for the object. */
1958 if (Nkind (gnat_temp) == N_Identifier
1959 || Nkind (gnat_temp) == N_Expanded_Name)
1960 gnu_type = gnat_to_gnu_type (Entity (gnat_temp));
1961 else if (Nkind (gnat_temp) == N_Qualified_Expression)
1962 {
1963 Entity_Id gnat_desig_type
1964 = Designated_Type (Underlying_Type (Etype (gnat_node)));
1965
1966 gnu_init = gnat_to_gnu (Expression (gnat_temp));
1967
1968 gnu_init = maybe_unconstrained_array (gnu_init);
1969 if (Do_Range_Check (Expression (gnat_temp)))
1970 gnu_init = emit_range_check (gnu_init, gnat_desig_type);
1971
1972 if (Is_Elementary_Type (gnat_desig_type)
1973 || Is_Constrained (gnat_desig_type))
1974 {
1975 gnu_type = gnat_to_gnu_type (gnat_desig_type);
1976 gnu_init = convert (gnu_type, gnu_init);
1977 }
1978 else
1979 {
1980 gnu_type = gnat_to_gnu_type (Etype (Expression (gnat_temp)));
1981 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
1982 gnu_type = TREE_TYPE (gnu_init);
1983
1984 gnu_init = convert (gnu_type, gnu_init);
1985 }
1986 }
1987 else
1988 gigi_abort (315);
1989
1990 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1991 return build_allocator (gnu_type, gnu_init, gnu_result_type,
1992 Procedure_To_Call (gnat_node),
1993 Storage_Pool (gnat_node));
1994 }
1995 break;
1996
1997 /***************************/
1998 /* Chapter 5: Statements: */
1999 /***************************/
2000
2001 case N_Label:
2002 if (! type_annotate_only)
2003 {
2004 tree gnu_label = gnat_to_gnu (Identifier (gnat_node));
2005 Node_Id gnat_parent = Parent (gnat_node);
2006
2007 expand_label (gnu_label);
2008
2009 /* If this is the first label of an exception handler, we must
2010 mark that any CALL_INSN can jump to it. */
2011 if (Present (gnat_parent)
2012 && Nkind (gnat_parent) == N_Exception_Handler
2013 && First (Statements (gnat_parent)) == gnat_node)
2014 nonlocal_goto_handler_labels
2015 = gen_rtx_EXPR_LIST (VOIDmode, label_rtx (gnu_label),
2016 nonlocal_goto_handler_labels);
2017 }
2018 break;
2019
2020 case N_Null_Statement:
2021 break;
2022
2023 case N_Assignment_Statement:
2024 if (type_annotate_only)
2025 break;
2026
2027 /* Get the LHS and RHS of the statement and convert any reference to an
2028 unconstrained array into a reference to the underlying array. */
2029 gnu_lhs = maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node)));
2030 gnu_rhs
2031 = maybe_unconstrained_array (gnat_to_gnu (Expression (gnat_node)));
2032
2033 set_lineno (gnat_node, 1);
2034
2035 /* If range check is needed, emit code to generate it */
2036 if (Do_Range_Check (Expression (gnat_node)))
2037 gnu_rhs = emit_range_check (gnu_rhs, Etype (Name (gnat_node)));
2038
2039 /* If either side's type has a size that overflows, convert this
2040 into raise of Storage_Error: execution shouldn't have gotten
2041 here anyway. */
2042 if ((TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_lhs))) == INTEGER_CST
2043 && TREE_OVERFLOW (TYPE_SIZE (TREE_TYPE (gnu_lhs))))
2044 || (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_rhs))) == INTEGER_CST
2045 && TREE_OVERFLOW (TYPE_SIZE (TREE_TYPE (gnu_rhs)))))
2046 expand_expr_stmt (build_call_raise (SE_Object_Too_Large));
2047 else
2048 expand_expr_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE,
2049 gnu_lhs, gnu_rhs));
2050 break;
2051
2052 case N_If_Statement:
2053 /* Start an IF statement giving the condition. */
2054 gnu_expr = gnat_to_gnu (Condition (gnat_node));
2055 set_lineno (gnat_node, 1);
2056 expand_start_cond (gnu_expr, 0);
2057
2058 /* Generate code for the statements to be executed if the condition
2059 is true. */
2060
2061 for (gnat_temp = First (Then_Statements (gnat_node));
2062 Present (gnat_temp);
2063 gnat_temp = Next (gnat_temp))
2064 gnat_to_code (gnat_temp);
2065
2066 /* Generate each of the "else if" parts. */
2067 if (Present (Elsif_Parts (gnat_node)))
2068 {
2069 for (gnat_temp = First (Elsif_Parts (gnat_node));
2070 Present (gnat_temp);
2071 gnat_temp = Next (gnat_temp))
2072 {
2073 Node_Id gnat_statement;
2074
2075 expand_start_else ();
2076
2077 /* Set up the line numbers for each condition we test. */
2078 set_lineno (Condition (gnat_temp), 1);
2079 expand_elseif (gnat_to_gnu (Condition (gnat_temp)));
2080
2081 for (gnat_statement = First (Then_Statements (gnat_temp));
2082 Present (gnat_statement);
2083 gnat_statement = Next (gnat_statement))
2084 gnat_to_code (gnat_statement);
2085 }
2086 }
2087
2088 /* Finally, handle any statements in the "else" part. */
2089 if (Present (Else_Statements (gnat_node)))
2090 {
2091 expand_start_else ();
2092
2093 for (gnat_temp = First (Else_Statements (gnat_node));
2094 Present (gnat_temp);
2095 gnat_temp = Next (gnat_temp))
2096 gnat_to_code (gnat_temp);
2097 }
2098
2099 expand_end_cond ();
2100 break;
2101
2102 case N_Case_Statement:
2103 {
2104 Node_Id gnat_when;
2105 Node_Id gnat_choice;
2106 tree gnu_label;
2107 Node_Id gnat_statement;
2108
2109 gnu_expr = gnat_to_gnu (Expression (gnat_node));
2110 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
2111
2112 set_lineno (gnat_node, 1);
2113 expand_start_case (1, gnu_expr, TREE_TYPE (gnu_expr), "case");
2114
2115 for (gnat_when = First_Non_Pragma (Alternatives (gnat_node));
2116 Present (gnat_when);
2117 gnat_when = Next_Non_Pragma (gnat_when))
2118 {
2119 /* First compile all the different case choices for the current
2120 WHEN alternative. */
2121
2122 for (gnat_choice = First (Discrete_Choices (gnat_when));
2123 Present (gnat_choice); gnat_choice = Next (gnat_choice))
2124 {
2125 int error_code;
2126
2127 gnu_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
2128
2129 set_lineno (gnat_choice, 1);
2130 switch (Nkind (gnat_choice))
2131 {
2132 case N_Range:
2133 /* Abort on all errors except range empty, which
2134 means we ignore this alternative. */
2135 error_code
2136 = pushcase_range (gnat_to_gnu (Low_Bound (gnat_choice)),
2137 gnat_to_gnu (High_Bound (gnat_choice)),
2138 convert, gnu_label, 0);
2139
2140 if (error_code != 0 && error_code != 4)
2141 gigi_abort (332);
2142 break;
2143
2144 case N_Subtype_Indication:
2145 error_code
2146 = pushcase_range
2147 (gnat_to_gnu (Low_Bound (Range_Expression
2148 (Constraint (gnat_choice)))),
2149 gnat_to_gnu (High_Bound (Range_Expression
2150 (Constraint (gnat_choice)))),
2151 convert, gnu_label, 0);
2152
2153 if (error_code != 0 && error_code != 4)
2154 gigi_abort (332);
2155 break;
2156
2157 case N_Identifier:
2158 case N_Expanded_Name:
2159 /* This represents either a subtype range or a static value
2160 of some kind; Ekind says which. If a static value,
2161 fall through to the next case. */
2162 if (IN (Ekind (Entity (gnat_choice)), Type_Kind))
2163 {
2164 tree type = get_unpadded_type (Entity (gnat_choice));
2165
2166 error_code
2167 = pushcase_range (fold (TYPE_MIN_VALUE (type)),
2168 fold (TYPE_MAX_VALUE (type)),
2169 convert, gnu_label, 0);
2170
2171 if (error_code != 0 && error_code != 4)
2172 gigi_abort (332);
2173 break;
2174 }
2175 /* ... fall through ... */
2176 case N_Character_Literal:
2177 case N_Integer_Literal:
2178 if (pushcase (gnat_to_gnu (gnat_choice), convert,
2179 gnu_label, 0))
2180 gigi_abort (332);
2181 break;
2182
2183 case N_Others_Choice:
2184 if (pushcase (NULL_TREE, convert, gnu_label, 0))
2185 gigi_abort (332);
2186 break;
2187
2188 default:
2189 gigi_abort (316);
2190 }
2191 }
2192
2193 /* After compiling the choices attached to the WHEN compile the
2194 body of statements that have to be executed, should the
2195 "WHEN ... =>" be taken. Push a binding level here in case
2196 variables are declared since we want them to be local to this
2197 set of statements instead of the block containing the Case
2198 statement. */
2199 pushlevel (0);
2200 expand_start_bindings (0);
2201 for (gnat_statement = First (Statements (gnat_when));
2202 Present (gnat_statement);
2203 gnat_statement = Next (gnat_statement))
2204 gnat_to_code (gnat_statement);
2205
2206 /* Communicate to GCC that we are done with the current WHEN,
2207 i.e. insert a "break" statement. */
2208 expand_exit_something ();
2209 expand_end_bindings (getdecls (), kept_level_p (), 0);
2210 poplevel (kept_level_p (), 1, 0);
2211 }
2212
2213 expand_end_case (gnu_expr);
2214 }
2215 break;
2216
2217 case N_Loop_Statement:
2218 {
2219 /* The loop variable in GCC form, if any. */
2220 tree gnu_loop_var = NULL_TREE;
2221 /* PREINCREMENT_EXPR or PREDECREMENT_EXPR. */
2222 enum tree_code gnu_update = ERROR_MARK;
2223 /* Used if this is a named loop for so EXIT can work. */
2224 struct nesting *loop_id;
2225 /* Condition to continue loop tested at top of loop. */
2226 tree gnu_top_condition = integer_one_node;
2227 /* Similar, but tested at bottom of loop. */
2228 tree gnu_bottom_condition = integer_one_node;
2229 Node_Id gnat_statement;
2230 Node_Id gnat_iter_scheme = Iteration_Scheme (gnat_node);
2231 Node_Id gnat_top_condition = Empty;
2232 int enclosing_if_p = 0;
2233
2234 /* Set the condition that under which the loop should continue.
2235 For "LOOP .... END LOOP;" the condition is always true. */
2236 if (No (gnat_iter_scheme))
2237 ;
2238 /* The case "WHILE condition LOOP ..... END LOOP;" */
2239 else if (Present (Condition (gnat_iter_scheme)))
2240 gnat_top_condition = Condition (gnat_iter_scheme);
2241 else
2242 {
2243 /* We have an iteration scheme. */
2244 Node_Id gnat_loop_spec
2245 = Loop_Parameter_Specification (gnat_iter_scheme);
2246 Entity_Id gnat_loop_var = Defining_Entity (gnat_loop_spec);
2247 Entity_Id gnat_type = Etype (gnat_loop_var);
2248 tree gnu_type = get_unpadded_type (gnat_type);
2249 tree gnu_low = TYPE_MIN_VALUE (gnu_type);
2250 tree gnu_high = TYPE_MAX_VALUE (gnu_type);
2251 int reversep = Reverse_Present (gnat_loop_spec);
2252 tree gnu_first = reversep ? gnu_high : gnu_low;
2253 tree gnu_last = reversep ? gnu_low : gnu_high;
2254 enum tree_code end_code = reversep ? GE_EXPR : LE_EXPR;
2255 tree gnu_base_type = get_base_type (gnu_type);
2256 tree gnu_limit
2257 = (reversep ? TYPE_MIN_VALUE (gnu_base_type)
2258 : TYPE_MAX_VALUE (gnu_base_type));
2259
2260 /* We know the loop variable will not overflow if GNU_LAST is
2261 a constant and is not equal to GNU_LIMIT. If it might
2262 overflow, we have to move the limit test to the end of
2263 the loop. In that case, we have to test for an
2264 empty loop outside the loop. */
2265 if (TREE_CODE (gnu_last) != INTEGER_CST
2266 || TREE_CODE (gnu_limit) != INTEGER_CST
2267 || tree_int_cst_equal (gnu_last, gnu_limit))
2268 {
2269 gnu_expr = build_binary_op (LE_EXPR, integer_type_node,
2270 gnu_low, gnu_high);
2271 set_lineno (gnat_loop_spec, 1);
2272 expand_start_cond (gnu_expr, 0);
2273 enclosing_if_p = 1;
2274 }
2275
2276 /* Open a new nesting level that will surround the loop to declare
2277 the loop index variable. */
2278 pushlevel (0);
2279 expand_start_bindings (0);
2280
2281 /* Declare the loop index and set it to its initial value. */
2282 gnu_loop_var = gnat_to_gnu_entity (gnat_loop_var, gnu_first, 1);
2283 if (DECL_BY_REF_P (gnu_loop_var))
2284 gnu_loop_var = build_unary_op (INDIRECT_REF, NULL_TREE,
2285 gnu_loop_var);
2286
2287 /* The loop variable might be a padded type, so use `convert' to
2288 get a reference to the inner variable if so. */
2289 gnu_loop_var = convert (get_base_type (gnu_type), gnu_loop_var);
2290
2291 /* Set either the top or bottom exit condition as
2292 appropriate depending on whether we know an overflow
2293 cannot occur or not. */
2294 if (enclosing_if_p)
2295 gnu_bottom_condition
2296 = build_binary_op (NE_EXPR, integer_type_node,
2297 gnu_loop_var, gnu_last);
2298 else
2299 gnu_top_condition
2300 = build_binary_op (end_code, integer_type_node,
2301 gnu_loop_var, gnu_last);
2302
2303 gnu_update = reversep ? PREDECREMENT_EXPR : PREINCREMENT_EXPR;
2304 }
2305
2306 set_lineno (gnat_node, 1);
2307 if (gnu_loop_var)
2308 loop_id = expand_start_loop_continue_elsewhere (1);
2309 else
2310 loop_id = expand_start_loop (1);
2311
2312 /* If the loop was named, have the name point to this loop. In this
2313 case, the association is not a ..._DECL node; in fact, it isn't
2314 a GCC tree node at all. Since this name is referenced inside
2315 the loop, do it before we process the statements of the loop. */
2316 if (Present (Identifier (gnat_node)))
2317 {
2318 tree gnu_loop_id = make_node (GNAT_LOOP_ID);
2319
2320 TREE_LOOP_ID (gnu_loop_id) = loop_id;
2321 save_gnu_tree (Entity (Identifier (gnat_node)), gnu_loop_id, 1);
2322 }
2323
2324 set_lineno (gnat_node, 1);
2325
2326 /* We must evaluate the condition after we've entered the
2327 loop so that any expression actions get done in the right
2328 place. */
2329 if (Present (gnat_top_condition))
2330 gnu_top_condition = gnat_to_gnu (gnat_top_condition);
2331
2332 expand_exit_loop_top_cond (0, gnu_top_condition);
2333
2334 /* Make the loop body into its own block, so any allocated
2335 storage will be released every iteration. This is needed
2336 for stack allocation. */
2337
2338 pushlevel (0);
2339 gnu_block_stack
2340 = tree_cons (gnu_bottom_condition, NULL_TREE, gnu_block_stack);
2341 expand_start_bindings (0);
2342
2343 for (gnat_statement = First (Statements (gnat_node));
2344 Present (gnat_statement);
2345 gnat_statement = Next (gnat_statement))
2346 gnat_to_code (gnat_statement);
2347
2348 expand_end_bindings (getdecls (), kept_level_p (), 0);
2349 poplevel (kept_level_p (), 1, 0);
2350 gnu_block_stack = TREE_CHAIN (gnu_block_stack);
2351
2352 set_lineno (gnat_node, 1);
2353 expand_exit_loop_if_false (0, gnu_bottom_condition);
2354
2355 if (gnu_loop_var)
2356 {
2357 expand_loop_continue_here ();
2358 gnu_expr = build_binary_op (gnu_update, TREE_TYPE (gnu_loop_var),
2359 gnu_loop_var,
2360 convert (TREE_TYPE (gnu_loop_var),
2361 integer_one_node));
2362 set_lineno (gnat_iter_scheme, 1);
2363 expand_expr_stmt (gnu_expr);
2364 }
2365
2366 set_lineno (gnat_node, 1);
2367 expand_end_loop ();
2368
2369 if (gnu_loop_var)
2370 {
2371 /* Close the nesting level that sourround the loop that was used to
2372 declare the loop index variable. */
2373 set_lineno (gnat_node, 1);
2374 expand_end_bindings (getdecls (), 1, 0);
2375 poplevel (1, 1, 0);
2376 }
2377
2378 if (enclosing_if_p)
2379 {
2380 set_lineno (gnat_node, 1);
2381 expand_end_cond ();
2382 }
2383 }
2384 break;
2385
2386 case N_Block_Statement:
2387 pushlevel (0);
2388 gnu_block_stack = tree_cons (NULL_TREE, NULL_TREE, gnu_block_stack);
2389 expand_start_bindings (0);
2390 process_decls (Declarations (gnat_node), Empty, Empty, 1, 1);
2391 gnat_to_code (Handled_Statement_Sequence (gnat_node));
2392 expand_end_bindings (getdecls (), kept_level_p (), 0);
2393 poplevel (kept_level_p (), 1, 0);
2394 gnu_block_stack = TREE_CHAIN (gnu_block_stack);
2395 if (Present (Identifier (gnat_node)))
2396 mark_out_of_scope (Entity (Identifier (gnat_node)));
2397 break;
2398
2399 case N_Exit_Statement:
2400 {
2401 /* Which loop to exit, NULL if the current loop. */
2402 struct nesting *loop_id = 0;
2403 /* The GCC version of the optional GNAT condition node attached to the
2404 exit statement. Exit the loop if this is false. */
2405 tree gnu_cond = integer_zero_node;
2406
2407 if (Present (Name (gnat_node)))
2408 loop_id
2409 = TREE_LOOP_ID (get_gnu_tree (Entity (Name (gnat_node))));
2410
2411 if (Present (Condition (gnat_node)))
2412 gnu_cond = invert_truthvalue (gnat_truthvalue_conversion
2413 (gnat_to_gnu (Condition (gnat_node))));
2414
2415 set_lineno (gnat_node, 1);
2416 expand_exit_loop_if_false (loop_id, gnu_cond);
2417 }
2418 break;
2419
2420 case N_Return_Statement:
2421 if (type_annotate_only)
2422 break;
2423
2424 {
2425 /* The gnu function type of the subprogram currently processed. */
2426 tree gnu_subprog_type = TREE_TYPE (current_function_decl);
2427 /* The return value from the subprogram. */
2428 tree gnu_ret_val = 0;
2429
2430 /* If we are dealing with a "return;" from an Ada procedure with
2431 parameters passed by copy in copy out, we need to return a record
2432 containing the final values of these parameters. If the list
2433 contains only one entry, return just that entry.
2434
2435 For a full description of the copy in copy out parameter mechanism,
2436 see the part of the gnat_to_gnu_entity routine dealing with the
2437 translation of subprograms.
2438
2439 But if we have a return label defined, convert this into
2440 a branch to that label. */
2441
2442 if (TREE_VALUE (gnu_return_label_stack) != 0)
2443 expand_goto (TREE_VALUE (gnu_return_label_stack));
2444
2445 else if (TYPE_CI_CO_LIST (gnu_subprog_type) != NULL_TREE)
2446 {
2447 if (list_length (TYPE_CI_CO_LIST (gnu_subprog_type)) == 1)
2448 gnu_ret_val = TREE_VALUE (TYPE_CI_CO_LIST (gnu_subprog_type));
2449 else
2450 gnu_ret_val
2451 = gnat_build_constructor (TREE_TYPE (gnu_subprog_type),
2452 TYPE_CI_CO_LIST (gnu_subprog_type));
2453 }
2454
2455 /* If the Ada subprogram is a function, we just need to return the
2456 expression. If the subprogram returns an unconstrained
2457 array, we have to allocate a new version of the result and
2458 return it. If we return by reference, return a pointer. */
2459
2460 else if (Present (Expression (gnat_node)))
2461 {
2462 gnu_ret_val = gnat_to_gnu (Expression (gnat_node));
2463
2464 /* Do not remove the padding from GNU_RET_VAL if the inner
2465 type is self-referential since we want to allocate the fixed
2466 size in that case. */
2467 if (TREE_CODE (gnu_ret_val) == COMPONENT_REF
2468 && (TYPE_IS_PADDING_P
2469 (TREE_TYPE (TREE_OPERAND (gnu_ret_val, 0))))
2470 && contains_placeholder_p
2471 (TYPE_SIZE (TREE_TYPE (gnu_ret_val))))
2472 gnu_ret_val = TREE_OPERAND (gnu_ret_val, 0);
2473
2474 if (TYPE_RETURNS_BY_REF_P (gnu_subprog_type)
2475 || By_Ref (gnat_node))
2476 gnu_ret_val = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_ret_val);
2477
2478 else if (TYPE_RETURNS_UNCONSTRAINED_P (gnu_subprog_type))
2479 {
2480 gnu_ret_val = maybe_unconstrained_array (gnu_ret_val);
2481
2482 /* We have two cases: either the function returns with
2483 depressed stack or not. If not, we allocate on the
2484 secondary stack. If so, we allocate in the stack frame.
2485 if no copy is needed, the front end will set By_Ref,
2486 which we handle in the case above. */
2487 if (TYPE_RETURNS_STACK_DEPRESSED (gnu_subprog_type))
2488 gnu_ret_val
2489 = build_allocator (TREE_TYPE (gnu_ret_val), gnu_ret_val,
2490 TREE_TYPE (gnu_subprog_type), 0, -1);
2491 else
2492 gnu_ret_val
2493 = build_allocator (TREE_TYPE (gnu_ret_val), gnu_ret_val,
2494 TREE_TYPE (gnu_subprog_type),
2495 Procedure_To_Call (gnat_node),
2496 Storage_Pool (gnat_node));
2497 }
2498 }
2499
2500 set_lineno (gnat_node, 1);
2501 if (gnu_ret_val)
2502 expand_return (build_binary_op (MODIFY_EXPR, NULL_TREE,
2503 DECL_RESULT (current_function_decl),
2504 gnu_ret_val));
2505 else
2506 expand_null_return ();
2507
2508 }
2509 break;
2510
2511 case N_Goto_Statement:
2512 if (type_annotate_only)
2513 break;
2514
2515 gnu_expr = gnat_to_gnu (Name (gnat_node));
2516 TREE_USED (gnu_expr) = 1;
2517 set_lineno (gnat_node, 1);
2518 expand_goto (gnu_expr);
2519 break;
2520
2521 /****************************/
2522 /* Chapter 6: Subprograms: */
2523 /****************************/
2524
2525 case N_Subprogram_Declaration:
2526 /* Unless there is a freeze node, declare the subprogram. We consider
2527 this a "definition" even though we're not generating code for
2528 the subprogram because we will be making the corresponding GCC
2529 node here. */
2530
2531 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
2532 gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node)),
2533 NULL_TREE, 1);
2534
2535 break;
2536
2537 case N_Abstract_Subprogram_Declaration:
2538 /* This subprogram doesn't exist for code generation purposes, but we
2539 have to elaborate the types of any parameters, unless they are
2540 imported types (nothing to generate in this case). */
2541 for (gnat_temp
2542 = First_Formal (Defining_Entity (Specification (gnat_node)));
2543 Present (gnat_temp);
2544 gnat_temp = Next_Formal_With_Extras (gnat_temp))
2545 if (Is_Itype (Etype (gnat_temp))
2546 && !From_With_Type (Etype (gnat_temp)))
2547 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2548
2549 break;
2550
2551 case N_Defining_Program_Unit_Name:
2552 /* For a child unit identifier go up a level to get the
2553 specificaton. We get this when we try to find the spec of
2554 a child unit package that is the compilation unit being compiled. */
2555 gnat_to_code (Parent (gnat_node));
2556 break;
2557
2558 case N_Subprogram_Body:
2559 {
2560 /* Save debug output mode in case it is reset. */
2561 enum debug_info_type save_write_symbols = write_symbols;
2562 const struct gcc_debug_hooks *const save_debug_hooks = debug_hooks;
2563 /* Definining identifier of a parameter to the subprogram. */
2564 Entity_Id gnat_param;
2565 /* The defining identifier for the subprogram body. Note that if a
2566 specification has appeared before for this body, then the identifier
2567 occurring in that specification will also be a defining identifier
2568 and all the calls to this subprogram will point to that
2569 specification. */
2570 Entity_Id gnat_subprog_id
2571 = (Present (Corresponding_Spec (gnat_node))
2572 ? Corresponding_Spec (gnat_node) : Defining_Entity (gnat_node));
2573
2574 /* The FUNCTION_DECL node corresponding to the subprogram spec. */
2575 tree gnu_subprog_decl;
2576 /* The FUNCTION_TYPE node corresponding to the subprogram spec. */
2577 tree gnu_subprog_type;
2578 tree gnu_cico_list;
2579
2580 /* If this is a generic object or if it has been eliminated,
2581 ignore it. */
2582
2583 if (Ekind (gnat_subprog_id) == E_Generic_Procedure
2584 || Ekind (gnat_subprog_id) == E_Generic_Function
2585 || Is_Eliminated (gnat_subprog_id))
2586 break;
2587
2588 /* If debug information is suppressed for the subprogram,
2589 turn debug mode off for the duration of processing. */
2590 if (Debug_Info_Off (gnat_subprog_id))
2591 {
2592 write_symbols = NO_DEBUG;
2593 debug_hooks = &do_nothing_debug_hooks;
2594 }
2595
2596 /* If this subprogram acts as its own spec, define it. Otherwise,
2597 just get the already-elaborated tree node. However, if this
2598 subprogram had its elaboration deferred, we will already have
2599 made a tree node for it. So treat it as not being defined in
2600 that case. Such a subprogram cannot have an address clause or
2601 a freeze node, so this test is safe, though it does disable
2602 some otherwise-useful error checking. */
2603 gnu_subprog_decl
2604 = gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE,
2605 Acts_As_Spec (gnat_node)
2606 && ! present_gnu_tree (gnat_subprog_id));
2607
2608 gnu_subprog_type = TREE_TYPE (gnu_subprog_decl);
2609
2610 /* Set the line number in the decl to correspond to that of
2611 the body so that the line number notes are written
2612 correctly. */
2613 set_lineno (gnat_node, 0);
2614 DECL_SOURCE_LOCATION (gnu_subprog_decl) = input_location;
2615
2616 begin_subprog_body (gnu_subprog_decl);
2617 set_lineno (gnat_node, 1);
2618
2619 pushlevel (0);
2620 gnu_block_stack = tree_cons (NULL_TREE, NULL_TREE, gnu_block_stack);
2621 expand_start_bindings (0);
2622
2623 gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
2624
2625 /* If there are OUT parameters, we need to ensure that the
2626 return statement properly copies them out. We do this by
2627 making a new block and converting any inner return into a goto
2628 to a label at the end of the block. */
2629
2630 if (gnu_cico_list != 0)
2631 {
2632 gnu_return_label_stack
2633 = tree_cons (NULL_TREE,
2634 build_decl (LABEL_DECL, NULL_TREE, NULL_TREE),
2635 gnu_return_label_stack);
2636 pushlevel (0);
2637 expand_start_bindings (0);
2638 }
2639 else
2640 gnu_return_label_stack
2641 = tree_cons (NULL_TREE, NULL_TREE, gnu_return_label_stack);
2642
2643 /* See if there are any parameters for which we don't yet have
2644 GCC entities. These must be for OUT parameters for which we
2645 will be making VAR_DECL nodes here. Fill them in to
2646 TYPE_CI_CO_LIST, which must contain the empty entry as well.
2647 We can match up the entries because TYPE_CI_CO_LIST is in the
2648 order of the parameters. */
2649
2650 for (gnat_param = First_Formal (gnat_subprog_id);
2651 Present (gnat_param);
2652 gnat_param = Next_Formal_With_Extras (gnat_param))
2653 if (present_gnu_tree (gnat_param))
2654 adjust_decl_rtl (get_gnu_tree (gnat_param));
2655 else
2656 {
2657 /* Skip any entries that have been already filled in; they
2658 must correspond to IN OUT parameters. */
2659 for (; gnu_cico_list != 0 && TREE_VALUE (gnu_cico_list) != 0;
2660 gnu_cico_list = TREE_CHAIN (gnu_cico_list))
2661 ;
2662
2663 /* Do any needed references for padded types. */
2664 TREE_VALUE (gnu_cico_list)
2665 = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_list)),
2666 gnat_to_gnu_entity (gnat_param, NULL_TREE, 1));
2667 }
2668
2669 process_decls (Declarations (gnat_node), Empty, Empty, 1, 1);
2670
2671 /* Generate the code of the subprogram itself. A return statement
2672 will be present and any OUT parameters will be handled there. */
2673 gnat_to_code (Handled_Statement_Sequence (gnat_node));
2674
2675 expand_end_bindings (getdecls (), kept_level_p (), 0);
2676 poplevel (kept_level_p (), 1, 0);
2677 gnu_block_stack = TREE_CHAIN (gnu_block_stack);
2678
2679 if (TREE_VALUE (gnu_return_label_stack) != 0)
2680 {
2681 tree gnu_retval;
2682
2683 expand_end_bindings (NULL_TREE, kept_level_p (), 0);
2684 poplevel (kept_level_p (), 1, 0);
2685 expand_label (TREE_VALUE (gnu_return_label_stack));
2686
2687 gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
2688 set_lineno (gnat_node, 1);
2689 if (list_length (gnu_cico_list) == 1)
2690 gnu_retval = TREE_VALUE (gnu_cico_list);
2691 else
2692 gnu_retval = gnat_build_constructor (TREE_TYPE (gnu_subprog_type),
2693 gnu_cico_list);
2694
2695 if (DECL_P (gnu_retval) && DECL_BY_REF_P (gnu_retval))
2696 gnu_retval
2697 = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_retval);
2698
2699 expand_return
2700 (build_binary_op (MODIFY_EXPR, NULL_TREE,
2701 DECL_RESULT (current_function_decl),
2702 gnu_retval));
2703
2704 }
2705
2706 gnu_return_label_stack = TREE_CHAIN (gnu_return_label_stack);
2707
2708 /* Disconnect the trees for parameters that we made variables for
2709 from the GNAT entities since these will become unusable after
2710 we end the function. */
2711 for (gnat_param = First_Formal (gnat_subprog_id);
2712 Present (gnat_param);
2713 gnat_param = Next_Formal_With_Extras (gnat_param))
2714 if (TREE_CODE (get_gnu_tree (gnat_param)) == VAR_DECL)
2715 save_gnu_tree (gnat_param, NULL_TREE, 0);
2716
2717 end_subprog_body ();
2718 mark_out_of_scope (Defining_Unit_Name (Specification (gnat_node)));
2719 write_symbols = save_write_symbols;
2720 debug_hooks = save_debug_hooks;
2721 }
2722 break;
2723
2724 case N_Function_Call:
2725 case N_Procedure_Call_Statement:
2726
2727 if (type_annotate_only)
2728 break;
2729
2730 {
2731 /* The GCC node corresponding to the GNAT subprogram name. This can
2732 either be a FUNCTION_DECL node if we are dealing with a standard
2733 subprogram call, or an indirect reference expression (an
2734 INDIRECT_REF node) pointing to a subprogram. */
2735 tree gnu_subprog_node = gnat_to_gnu (Name (gnat_node));
2736 /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */
2737 tree gnu_subprog_type = TREE_TYPE (gnu_subprog_node);
2738 tree gnu_subprog_addr
2739 = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_subprog_node);
2740 Entity_Id gnat_formal;
2741 Node_Id gnat_actual;
2742 tree gnu_actual_list = NULL_TREE;
2743 tree gnu_name_list = NULL_TREE;
2744 tree gnu_after_list = NULL_TREE;
2745 tree gnu_subprog_call;
2746
2747 switch (Nkind (Name (gnat_node)))
2748 {
2749 case N_Identifier:
2750 case N_Operator_Symbol:
2751 case N_Expanded_Name:
2752 case N_Attribute_Reference:
2753 if (Is_Eliminated (Entity (Name (gnat_node))))
2754 post_error_ne ("cannot call eliminated subprogram &!",
2755 gnat_node, Entity (Name (gnat_node)));
2756 }
2757
2758 if (TREE_CODE (gnu_subprog_type) != FUNCTION_TYPE)
2759 gigi_abort (317);
2760
2761 /* If we are calling a stubbed function, make this into a
2762 raise of Program_Error. Elaborate all our args first. */
2763
2764 if (TREE_CODE (gnu_subprog_node) == FUNCTION_DECL
2765 && DECL_STUBBED_P (gnu_subprog_node))
2766 {
2767 for (gnat_actual = First_Actual (gnat_node);
2768 Present (gnat_actual);
2769 gnat_actual = Next_Actual (gnat_actual))
2770 expand_expr_stmt (gnat_to_gnu (gnat_actual));
2771
2772 if (Nkind (gnat_node) == N_Function_Call)
2773 {
2774 gnu_result_type = TREE_TYPE (gnu_subprog_type);
2775 gnu_result
2776 = build1 (NULL_EXPR, gnu_result_type,
2777 build_call_raise (PE_Stubbed_Subprogram_Called));
2778 }
2779 else
2780 expand_expr_stmt
2781 (build_call_raise (PE_Stubbed_Subprogram_Called));
2782 break;
2783 }
2784
2785 /* The only way we can be making a call via an access type is
2786 if Name is an explicit dereference. In that case, get the
2787 list of formal args from the type the access type is pointing
2788 to. Otherwise, get the formals from entity being called. */
2789 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
2790 gnat_formal = First_Formal (Etype (Name (gnat_node)));
2791 else if (Nkind (Name (gnat_node)) == N_Attribute_Reference)
2792 /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */
2793 gnat_formal = 0;
2794 else
2795 gnat_formal = First_Formal (Entity (Name (gnat_node)));
2796
2797 /* Create the list of the actual parameters as GCC expects it, namely
2798 a chain of TREE_LIST nodes in which the TREE_VALUE field of each
2799 node is a parameter-expression and the TREE_PURPOSE field is
2800 null. Skip OUT parameters that are not passed by reference. */
2801
2802 for (gnat_actual = First_Actual (gnat_node);
2803 Present (gnat_actual);
2804 gnat_formal = Next_Formal_With_Extras (gnat_formal),
2805 gnat_actual = Next_Actual (gnat_actual))
2806 {
2807 tree gnu_formal_type = gnat_to_gnu_type (Etype (gnat_formal));
2808 Node_Id gnat_name
2809 = ((Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
2810 ? Expression (gnat_actual) : gnat_actual);
2811 tree gnu_name = gnat_to_gnu (gnat_name);
2812 tree gnu_name_type = gnat_to_gnu_type (Etype (gnat_name));
2813 tree gnu_actual;
2814
2815 /* If it's possible we may need to use this expression twice,
2816 make sure than any side-effects are handled via SAVE_EXPRs.
2817 Likewise if we need to force side-effects before the call.
2818 ??? This is more conservative than we need since we don't
2819 need to do this for pass-by-ref with no conversion.
2820 If we are passing a non-addressable Out or In Out parameter by
2821 reference, pass the address of a copy and set up to copy back
2822 out after the call. */
2823
2824 if (Ekind (gnat_formal) != E_In_Parameter)
2825 {
2826 gnu_name = gnat_stabilize_reference (gnu_name, 1);
2827 if (! addressable_p (gnu_name)
2828 && present_gnu_tree (gnat_formal)
2829 && (DECL_BY_REF_P (get_gnu_tree (gnat_formal))
2830 || DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal))
2831 || DECL_BY_DESCRIPTOR_P (get_gnu_tree (gnat_formal))))
2832 {
2833 tree gnu_copy = gnu_name;
2834
2835 /* Remove any unpadding on the actual and make a copy.
2836 But if the actual is a left-justified modular type,
2837 first convert to it. */
2838 if (TREE_CODE (gnu_name) == COMPONENT_REF
2839 && (TYPE_IS_PADDING_P
2840 (TREE_TYPE (TREE_OPERAND (gnu_name, 0)))))
2841 gnu_name = gnu_copy = TREE_OPERAND (gnu_name, 0);
2842 else if (TREE_CODE (gnu_name_type) == RECORD_TYPE
2843 && (TYPE_LEFT_JUSTIFIED_MODULAR_P
2844 (gnu_name_type)))
2845 gnu_name = convert (gnu_name_type, gnu_name);
2846
2847 gnu_actual = save_expr (gnu_name);
2848
2849 /* Set up to move the copy back to the original. */
2850 gnu_after_list = tree_cons (gnu_copy, gnu_actual,
2851 gnu_after_list);
2852
2853 gnu_name = gnu_actual;
2854 }
2855 }
2856
2857 /* If this was a procedure call, we may not have removed any
2858 padding. So do it here for the part we will use as an
2859 input, if any. */
2860 gnu_actual = gnu_name;
2861 if (Ekind (gnat_formal) != E_Out_Parameter
2862 && TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
2863 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
2864 gnu_actual = convert (get_unpadded_type (Etype (gnat_actual)),
2865 gnu_actual);
2866
2867 if (Ekind (gnat_formal) != E_Out_Parameter
2868 && Nkind (gnat_actual) != N_Unchecked_Type_Conversion
2869 && Do_Range_Check (gnat_actual))
2870 gnu_actual = emit_range_check (gnu_actual, Etype (gnat_formal));
2871
2872 /* Do any needed conversions. We need only check for
2873 unchecked conversion since normal conversions will be handled
2874 by just converting to the formal type. */
2875 if (Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
2876 {
2877 gnu_actual
2878 = unchecked_convert (gnat_to_gnu_type (Etype (gnat_actual)),
2879 gnu_actual);
2880
2881 /* One we've done the unchecked conversion, we still
2882 must ensure that the object is in range of the formal's
2883 type. */
2884 if (Ekind (gnat_formal) != E_Out_Parameter
2885 && Do_Range_Check (gnat_actual))
2886 gnu_actual = emit_range_check (gnu_actual,
2887 Etype (gnat_formal));
2888 }
2889 else
2890 /* We may have suppressed a conversion to the Etype of the
2891 actual since the parent is a procedure call. So add the
2892 conversion here. */
2893 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
2894 gnu_actual);
2895
2896 gnu_actual = convert (gnu_formal_type, gnu_actual);
2897
2898 /* If we have not saved a GCC object for the formal, it means
2899 it is an OUT parameter not passed by reference. Otherwise,
2900 look at the PARM_DECL to see if it is passed by reference. */
2901 if (present_gnu_tree (gnat_formal)
2902 && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
2903 && DECL_BY_REF_P (get_gnu_tree (gnat_formal)))
2904 {
2905 if (Ekind (gnat_formal) != E_In_Parameter)
2906 {
2907 gnu_actual = gnu_name;
2908
2909 /* If we have a padded type, be sure we've removed the
2910 padding. */
2911 if (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
2912 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
2913 gnu_actual
2914 = convert (get_unpadded_type (Etype (gnat_actual)),
2915 gnu_actual);
2916 }
2917
2918 /* The symmetry of the paths to the type of an entity is
2919 broken here since arguments don't know that they will
2920 be passed by ref. */
2921 gnu_formal_type = TREE_TYPE (get_gnu_tree (gnat_formal));
2922 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type,
2923 gnu_actual);
2924 }
2925 else if (present_gnu_tree (gnat_formal)
2926 && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
2927 && DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal)))
2928 {
2929 gnu_formal_type = TREE_TYPE (get_gnu_tree (gnat_formal));
2930 gnu_actual = maybe_implicit_deref (gnu_actual);
2931 gnu_actual = maybe_unconstrained_array (gnu_actual);
2932
2933 if (TREE_CODE (gnu_formal_type) == RECORD_TYPE
2934 && TYPE_IS_PADDING_P (gnu_formal_type))
2935 {
2936 gnu_formal_type
2937 = TREE_TYPE (TYPE_FIELDS (gnu_formal_type));
2938 gnu_actual = convert (gnu_formal_type, gnu_actual);
2939 }
2940
2941 /* Take the address of the object and convert to the
2942 proper pointer type. We'd like to actually compute
2943 the address of the beginning of the array using
2944 an ADDR_EXPR of an ARRAY_REF, but there's a possibility
2945 that the ARRAY_REF might return a constant and we'd
2946 be getting the wrong address. Neither approach is
2947 exactly correct, but this is the most likely to work
2948 in all cases. */
2949 gnu_actual = convert (gnu_formal_type,
2950 build_unary_op (ADDR_EXPR, NULL_TREE,
2951 gnu_actual));
2952 }
2953 else if (present_gnu_tree (gnat_formal)
2954 && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
2955 && DECL_BY_DESCRIPTOR_P (get_gnu_tree (gnat_formal)))
2956 {
2957 /* If arg is 'Null_Parameter, pass zero descriptor. */
2958 if ((TREE_CODE (gnu_actual) == INDIRECT_REF
2959 || TREE_CODE (gnu_actual) == UNCONSTRAINED_ARRAY_REF)
2960 && TREE_PRIVATE (gnu_actual))
2961 gnu_actual
2962 = convert (DECL_ARG_TYPE (get_gnu_tree (gnat_formal)),
2963 integer_zero_node);
2964 else
2965 gnu_actual
2966 = build_unary_op (ADDR_EXPR, NULL_TREE,
2967 fill_vms_descriptor (gnu_actual,
2968 gnat_formal));
2969 }
2970 else
2971 {
2972 tree gnu_actual_size = TYPE_SIZE (TREE_TYPE (gnu_actual));
2973
2974 if (Ekind (gnat_formal) != E_In_Parameter)
2975 gnu_name_list
2976 = chainon (gnu_name_list,
2977 build_tree_list (NULL_TREE, gnu_name));
2978
2979 if (! present_gnu_tree (gnat_formal)
2980 || TREE_CODE (get_gnu_tree (gnat_formal)) != PARM_DECL)
2981 continue;
2982
2983 /* If this is 'Null_Parameter, pass a zero even though we are
2984 dereferencing it. */
2985 else if (TREE_CODE (gnu_actual) == INDIRECT_REF
2986 && TREE_PRIVATE (gnu_actual)
2987 && host_integerp (gnu_actual_size, 1)
2988 && 0 >= compare_tree_int (gnu_actual_size,
2989 BITS_PER_WORD))
2990 gnu_actual
2991 = unchecked_convert
2992 (DECL_ARG_TYPE (get_gnu_tree (gnat_formal)),
2993 convert (gnat_type_for_size
2994 (tree_low_cst (gnu_actual_size, 1), 1),
2995 integer_zero_node));
2996 else
2997 gnu_actual
2998 = convert (TYPE_MAIN_VARIANT
2999 (DECL_ARG_TYPE (get_gnu_tree (gnat_formal))),
3000 gnu_actual);
3001 }
3002
3003 gnu_actual_list
3004 = chainon (gnu_actual_list,
3005 build_tree_list (NULL_TREE, gnu_actual));
3006 }
3007
3008 gnu_subprog_call = build (CALL_EXPR, TREE_TYPE (gnu_subprog_type),
3009 gnu_subprog_addr, gnu_actual_list,
3010 NULL_TREE);
3011 TREE_SIDE_EFFECTS (gnu_subprog_call) = 1;
3012
3013 /* If it is a function call, the result is the call expression. */
3014 if (Nkind (gnat_node) == N_Function_Call)
3015 {
3016 gnu_result = gnu_subprog_call;
3017
3018 /* If the function returns an unconstrained array or by reference,
3019 we have to de-dereference the pointer. */
3020 if (TYPE_RETURNS_UNCONSTRAINED_P (gnu_subprog_type)
3021 || TYPE_RETURNS_BY_REF_P (gnu_subprog_type))
3022 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE,
3023 gnu_result);
3024
3025 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3026 }
3027
3028 /* If this is the case where the GNAT tree contains a procedure call
3029 but the Ada procedure has copy in copy out parameters, the special
3030 parameter passing mechanism must be used. */
3031 else if (TYPE_CI_CO_LIST (gnu_subprog_type) != NULL_TREE)
3032 {
3033 /* List of FIELD_DECLs associated with the PARM_DECLs of the copy
3034 in copy out parameters. */
3035 tree scalar_return_list = TYPE_CI_CO_LIST (gnu_subprog_type);
3036 int length = list_length (scalar_return_list);
3037
3038 if (length > 1)
3039 {
3040 tree gnu_name;
3041
3042 gnu_subprog_call = protect_multiple_eval (gnu_subprog_call);
3043
3044 /* If any of the names had side-effects, ensure they are
3045 all evaluated before the call. */
3046 for (gnu_name = gnu_name_list; gnu_name;
3047 gnu_name = TREE_CHAIN (gnu_name))
3048 if (TREE_SIDE_EFFECTS (TREE_VALUE (gnu_name)))
3049 gnu_subprog_call
3050 = build (COMPOUND_EXPR, TREE_TYPE (gnu_subprog_call),
3051 TREE_VALUE (gnu_name), gnu_subprog_call);
3052 }
3053
3054 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
3055 gnat_formal = First_Formal (Etype (Name (gnat_node)));
3056 else
3057 gnat_formal = First_Formal (Entity (Name (gnat_node)));
3058
3059 for (gnat_actual = First_Actual (gnat_node);
3060 Present (gnat_actual);
3061 gnat_formal = Next_Formal_With_Extras (gnat_formal),
3062 gnat_actual = Next_Actual (gnat_actual))
3063 /* If we are dealing with a copy in copy out parameter, we must
3064 retrieve its value from the record returned in the function
3065 call. */
3066 if (! (present_gnu_tree (gnat_formal)
3067 && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
3068 && (DECL_BY_REF_P (get_gnu_tree (gnat_formal))
3069 || (DECL_BY_COMPONENT_PTR_P
3070 (get_gnu_tree (gnat_formal)))
3071 || DECL_BY_DESCRIPTOR_P (get_gnu_tree (gnat_formal))))
3072 && Ekind (gnat_formal) != E_In_Parameter)
3073 {
3074 /* Get the value to assign to this OUT or IN OUT
3075 parameter. It is either the result of the function if
3076 there is only a single such parameter or the appropriate
3077 field from the record returned. */
3078 tree gnu_result
3079 = length == 1 ? gnu_subprog_call
3080 : build_component_ref
3081 (gnu_subprog_call, NULL_TREE,
3082 TREE_PURPOSE (scalar_return_list));
3083 int unchecked_conversion
3084 = Nkind (gnat_actual) == N_Unchecked_Type_Conversion;
3085 /* If the actual is a conversion, get the inner expression,
3086 which will be the real destination, and convert the
3087 result to the type of the actual parameter. */
3088 tree gnu_actual
3089 = maybe_unconstrained_array (TREE_VALUE (gnu_name_list));
3090
3091 /* If the result is a padded type, remove the padding. */
3092 if (TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE
3093 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
3094 gnu_result
3095 = convert (TREE_TYPE (TYPE_FIELDS
3096 (TREE_TYPE (gnu_result))),
3097 gnu_result);
3098
3099 /* If the result is a type conversion, do it. */
3100 if (Nkind (gnat_actual) == N_Type_Conversion)
3101 gnu_result
3102 = convert_with_check
3103 (Etype (Expression (gnat_actual)), gnu_result,
3104 Do_Overflow_Check (gnat_actual),
3105 Do_Range_Check (Expression (gnat_actual)),
3106 Float_Truncate (gnat_actual));
3107
3108 else if (unchecked_conversion)
3109 gnu_result
3110 = unchecked_convert (TREE_TYPE (gnu_actual), gnu_result);
3111 else
3112 {
3113 if (Do_Range_Check (gnat_actual))
3114 gnu_result = emit_range_check (gnu_result,
3115 Etype (gnat_actual));
3116
3117 if (! (! TREE_CONSTANT (TYPE_SIZE
3118 (TREE_TYPE (gnu_actual)))
3119 && TREE_CONSTANT (TYPE_SIZE
3120 (TREE_TYPE (gnu_result)))))
3121 gnu_result = convert (TREE_TYPE (gnu_actual),
3122 gnu_result);
3123 }
3124
3125 set_lineno (gnat_node, 1);
3126 expand_expr_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE,
3127 gnu_actual, gnu_result));
3128 scalar_return_list = TREE_CHAIN (scalar_return_list);
3129 gnu_name_list = TREE_CHAIN (gnu_name_list);
3130 }
3131 }
3132 else
3133 {
3134 set_lineno (gnat_node, 1);
3135 expand_expr_stmt (gnu_subprog_call);
3136 }
3137
3138 /* Handle anything we need to assign back. */
3139 for (gnu_expr = gnu_after_list;
3140 gnu_expr;
3141 gnu_expr = TREE_CHAIN (gnu_expr))
3142 expand_expr_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE,
3143 TREE_PURPOSE (gnu_expr),
3144 TREE_VALUE (gnu_expr)));
3145 }
3146 break;
3147
3148 /*************************/
3149 /* Chapter 7: Packages: */
3150 /*************************/
3151
3152 case N_Package_Declaration:
3153 gnat_to_code (Specification (gnat_node));
3154 break;
3155
3156 case N_Package_Specification:
3157
3158 process_decls (Visible_Declarations (gnat_node),
3159 Private_Declarations (gnat_node), Empty, 1, 1);
3160 break;
3161
3162 case N_Package_Body:
3163
3164 /* If this is the body of a generic package - do nothing */
3165 if (Ekind (Corresponding_Spec (gnat_node)) == E_Generic_Package)
3166 break;
3167
3168 process_decls (Declarations (gnat_node), Empty, Empty, 1, 1);
3169
3170 if (Present (Handled_Statement_Sequence (gnat_node)))
3171 {
3172 gnu_block_stack = tree_cons (NULL_TREE, NULL_TREE, gnu_block_stack);
3173 gnat_to_code (Handled_Statement_Sequence (gnat_node));
3174 gnu_block_stack = TREE_CHAIN (gnu_block_stack);
3175 }
3176 break;
3177
3178 /*********************************/
3179 /* Chapter 8: Visibility Rules: */
3180 /*********************************/
3181
3182 case N_Use_Package_Clause:
3183 case N_Use_Type_Clause:
3184 /* Nothing to do here - but these may appear in list of declarations */
3185 break;
3186
3187 /***********************/
3188 /* Chapter 9: Tasks: */
3189 /***********************/
3190
3191 case N_Protected_Type_Declaration:
3192 break;
3193
3194 case N_Single_Task_Declaration:
3195 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
3196 break;
3197
3198 /***********************************************************/
3199 /* Chapter 10: Program Structure and Compilation Issues: */
3200 /***********************************************************/
3201
3202 case N_Compilation_Unit:
3203
3204 /* For a body, first process the spec if there is one. */
3205 if (Nkind (Unit (gnat_node)) == N_Package_Body
3206 || (Nkind (Unit (gnat_node)) == N_Subprogram_Body
3207 && ! Acts_As_Spec (gnat_node)))
3208 gnat_to_code (Library_Unit (gnat_node));
3209
3210 process_inlined_subprograms (gnat_node);
3211
3212 if (type_annotate_only && gnat_node == Cunit (Main_Unit))
3213 {
3214 elaborate_all_entities (gnat_node);
3215
3216 if (Nkind (Unit (gnat_node)) == N_Subprogram_Declaration
3217 || Nkind (Unit (gnat_node)) == N_Generic_Package_Declaration
3218 || Nkind (Unit (gnat_node)) == N_Generic_Subprogram_Declaration)
3219 break;
3220 };
3221
3222 process_decls (Declarations (Aux_Decls_Node (gnat_node)),
3223 Empty, Empty, 1, 1);
3224
3225 gnat_to_code (Unit (gnat_node));
3226
3227 /* Process any pragmas following the unit. */
3228 if (Present (Pragmas_After (Aux_Decls_Node (gnat_node))))
3229 for (gnat_temp = First (Pragmas_After (Aux_Decls_Node (gnat_node)));
3230 gnat_temp; gnat_temp = Next (gnat_temp))
3231 gnat_to_code (gnat_temp);
3232
3233 /* Put all the Actions into the elaboration routine if we already had
3234 elaborations. This will happen anyway if they are statements, but we
3235 want to force declarations there too due to order-of-elaboration
3236 issues. Most should have Is_Statically_Allocated set. If we
3237 have had no elaborations, we have no order-of-elaboration issue and
3238 don't want to create elaborations here. */
3239 if (Is_Non_Empty_List (Actions (Aux_Decls_Node (gnat_node))))
3240 for (gnat_temp = First (Actions (Aux_Decls_Node (gnat_node)));
3241 Present (gnat_temp); gnat_temp = Next (gnat_temp))
3242 {
3243 if (pending_elaborations_p ())
3244 add_pending_elaborations (NULL_TREE,
3245 make_transform_expr (gnat_temp));
3246 else
3247 gnat_to_code (gnat_temp);
3248 }
3249
3250 /* Generate elaboration code for this unit, if necessary, and
3251 say whether we did or not. */
3252 Set_Has_No_Elaboration_Code
3253 (gnat_node,
3254 build_unit_elab
3255 (Defining_Entity (Unit (gnat_node)),
3256 Nkind (Unit (gnat_node)) == N_Package_Body
3257 || Nkind (Unit (gnat_node)) == N_Subprogram_Body,
3258 get_pending_elaborations ()));
3259
3260 break;
3261
3262 case N_Subprogram_Body_Stub:
3263 case N_Package_Body_Stub:
3264 case N_Protected_Body_Stub:
3265 case N_Task_Body_Stub:
3266 /* Simply process whatever unit is being inserted. */
3267 gnat_to_code (Unit (Library_Unit (gnat_node)));
3268 break;
3269
3270 case N_Subunit:
3271 gnat_to_code (Proper_Body (gnat_node));
3272 break;
3273
3274 /***************************/
3275 /* Chapter 11: Exceptions: */
3276 /***************************/
3277
3278 case N_Handled_Sequence_Of_Statements:
3279
3280 /* The GCC exception handling mechanism can handle both ZCX and SJLJ
3281 schemes and we have our own SJLJ mechanism. To call the GCC
3282 mechanism, we first call expand_eh_region_start if there is at least
3283 one handler associated with the region. We then generate code for
3284 the region and call expand_start_all_catch to announce that the
3285 associated handlers are going to be generated.
3286
3287 For each handler we call expand_start_catch, generate code for the
3288 handler, and then call expand_end_catch.
3289
3290 After all the handlers, we call expand_end_all_catch.
3291
3292 Here we deal with the region level calls and the
3293 N_Exception_Handler branch deals with the handler level calls
3294 (start_catch/end_catch).
3295
3296 ??? The region level calls down there have been specifically put in
3297 place for a ZCX context and currently the order in which things are
3298 emitted (region/handlers) is different from the SJLJ case. Instead of
3299 putting other calls with different conditions at other places for the
3300 SJLJ case, it seems cleaner to reorder things for the SJLJ case and
3301 generalize the condition to make it not ZCX specific. */
3302
3303 /* Tell the back-end we are starting a new exception region if
3304 necessary. */
3305 if (! type_annotate_only
3306 && Exception_Mechanism == GCC_ZCX
3307 && Present (Exception_Handlers (gnat_node)))
3308 expand_eh_region_start ();
3309
3310 /* If there are exception handlers, start a new binding level that
3311 we can exit (since each exception handler will do so). Then
3312 declare a variable to save the old __gnat_jmpbuf value and a
3313 variable for our jmpbuf. Call setjmp and handle each of the
3314 possible exceptions if it returns one. */
3315
3316 if (! type_annotate_only && Present (Exception_Handlers (gnat_node)))
3317 {
3318 tree gnu_jmpsave_decl = 0;
3319 tree gnu_jmpbuf_decl = 0;
3320 tree gnu_cleanup_call = 0;
3321 tree gnu_cleanup_decl;
3322
3323 pushlevel (0);
3324 expand_start_bindings (1);
3325
3326 if (Exception_Mechanism == Setjmp_Longjmp)
3327 {
3328 gnu_jmpsave_decl
3329 = create_var_decl (get_identifier ("JMPBUF_SAVE"), NULL_TREE,
3330 jmpbuf_ptr_type,
3331 build_call_0_expr (get_jmpbuf_decl),
3332 0, 0, 0, 0, 0);
3333
3334 gnu_jmpbuf_decl = create_var_decl (get_identifier ("JMP_BUF"),
3335 NULL_TREE, jmpbuf_type,
3336 NULL_TREE, 0, 0, 0, 0,
3337 0);
3338 TREE_VALUE (gnu_block_stack) = gnu_jmpbuf_decl;
3339 }
3340
3341 /* See if we are to call a function when exiting this block. */
3342 if (Present (At_End_Proc (gnat_node)))
3343 {
3344 gnu_cleanup_call
3345 = build_call_0_expr (gnat_to_gnu (At_End_Proc (gnat_node)));
3346
3347 gnu_cleanup_decl
3348 = create_var_decl (get_identifier ("CLEANUP"), NULL_TREE,
3349 integer_type_node, NULL_TREE, 0, 0, 0, 0,
3350 0);
3351
3352 expand_decl_cleanup (gnu_cleanup_decl, gnu_cleanup_call);
3353 }
3354
3355 if (Exception_Mechanism == Setjmp_Longjmp)
3356 {
3357 /* When we exit this block, restore the saved value. */
3358 expand_decl_cleanup (gnu_jmpsave_decl,
3359 build_call_1_expr (set_jmpbuf_decl,
3360 gnu_jmpsave_decl));
3361
3362 /* Call setjmp and handle exceptions if it returns one. */
3363 set_lineno (gnat_node, 1);
3364 expand_start_cond
3365 (build_call_1_expr (setjmp_decl,
3366 build_unary_op (ADDR_EXPR, NULL_TREE,
3367 gnu_jmpbuf_decl)),
3368 0);
3369
3370 /* Restore our incoming longjmp value before we do anything. */
3371 expand_expr_stmt (build_call_1_expr (set_jmpbuf_decl,
3372 gnu_jmpsave_decl));
3373
3374 pushlevel (0);
3375 expand_start_bindings (0);
3376
3377 gnu_except_ptr_stack
3378 = tree_cons (NULL_TREE,
3379 create_var_decl
3380 (get_identifier ("EXCEPT_PTR"), NULL_TREE,
3381 build_pointer_type (except_type_node),
3382 build_call_0_expr (get_excptr_decl),
3383 0, 0, 0, 0, 0),
3384 gnu_except_ptr_stack);
3385
3386 /* Generate code for each exception handler. The code at
3387 N_Exception_Handler below does the real work. Note that
3388 we ignore the dummy exception handler for the identifier
3389 case, this is used only by the front end */
3390 if (Present (Exception_Handlers (gnat_node)))
3391 for (gnat_temp
3392 = First_Non_Pragma (Exception_Handlers (gnat_node));
3393 Present (gnat_temp);
3394 gnat_temp = Next_Non_Pragma (gnat_temp))
3395 gnat_to_code (gnat_temp);
3396
3397 /* If none of the exception handlers did anything, re-raise
3398 but do not defer abortion. */
3399 set_lineno (gnat_node, 1);
3400 expand_expr_stmt
3401 (build_call_1_expr (raise_nodefer_decl,
3402 TREE_VALUE (gnu_except_ptr_stack)));
3403
3404 gnu_except_ptr_stack = TREE_CHAIN (gnu_except_ptr_stack);
3405 expand_end_bindings (getdecls (), kept_level_p (), 0);
3406 poplevel (kept_level_p (), 1, 0);
3407
3408 /* End the "if" on setjmp. Note that we have arranged things so
3409 control never returns here. */
3410 expand_end_cond ();
3411
3412 /* This is now immediately before the body proper. Set
3413 our jmp_buf as the current buffer. */
3414 expand_expr_stmt
3415 (build_call_1_expr (set_jmpbuf_decl,
3416 build_unary_op (ADDR_EXPR, NULL_TREE,
3417 gnu_jmpbuf_decl)));
3418 }
3419 }
3420
3421 /* If there are no exception handlers, we must not have an at end
3422 cleanup identifier, since the cleanup identifier should always
3423 generate a corresponding exception handler, except in the case
3424 of the No_Exception_Handlers restriction, where the front-end
3425 does not generate exception handlers. */
3426 else if (! type_annotate_only && Present (At_End_Proc (gnat_node)))
3427 {
3428 if (No_Exception_Handlers_Set ())
3429 {
3430 tree gnu_cleanup_call = 0;
3431 tree gnu_cleanup_decl;
3432
3433 gnu_cleanup_call
3434 = build_call_0_expr (gnat_to_gnu (At_End_Proc (gnat_node)));
3435
3436 gnu_cleanup_decl
3437 = create_var_decl (get_identifier ("CLEANUP"), NULL_TREE,
3438 integer_type_node, NULL_TREE, 0, 0, 0, 0,
3439 0);
3440
3441 expand_decl_cleanup (gnu_cleanup_decl, gnu_cleanup_call);
3442 }
3443 else
3444 gigi_abort (335);
3445 }
3446
3447 /* Generate code and declarations for the prefix of this block,
3448 if any. */
3449 if (Present (First_Real_Statement (gnat_node)))
3450 process_decls (Statements (gnat_node), Empty,
3451 First_Real_Statement (gnat_node), 1, 1);
3452
3453 /* Generate code for each statement in the block. */
3454 for (gnat_temp = (Present (First_Real_Statement (gnat_node))
3455 ? First_Real_Statement (gnat_node)
3456 : First (Statements (gnat_node)));
3457 Present (gnat_temp); gnat_temp = Next (gnat_temp))
3458 gnat_to_code (gnat_temp);
3459
3460 /* Tell the back-end we are ending the new exception region and
3461 starting the associated handlers. */
3462 if (! type_annotate_only
3463 && Exception_Mechanism == GCC_ZCX
3464 && Present (Exception_Handlers (gnat_node)))
3465 expand_start_all_catch ();
3466
3467 /* For zero-cost exceptions, exit the block and then compile
3468 the handlers. */
3469 if (! type_annotate_only
3470 && Exception_Mechanism == GCC_ZCX
3471 && Present (Exception_Handlers (gnat_node)))
3472 {
3473 expand_exit_something ();
3474 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
3475 Present (gnat_temp);
3476 gnat_temp = Next_Non_Pragma (gnat_temp))
3477 gnat_to_code (gnat_temp);
3478 }
3479
3480 /* We don't support Front_End_ZCX in GNAT 5.0, but we don't want to
3481 crash if -gnatdX is specified. */
3482 if (! type_annotate_only
3483 && Exception_Mechanism == Front_End_ZCX
3484 && Present (Exception_Handlers (gnat_node)))
3485 {
3486 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
3487 Present (gnat_temp);
3488 gnat_temp = Next_Non_Pragma (gnat_temp))
3489 gnat_to_code (gnat_temp);
3490 }
3491
3492 /* Tell the backend when we are done with the handlers. */
3493 if (! type_annotate_only
3494 && Exception_Mechanism == GCC_ZCX
3495 && Present (Exception_Handlers (gnat_node)))
3496 expand_end_all_catch ();
3497
3498 /* If we have handlers, close the block we made. */
3499 if (! type_annotate_only && Present (Exception_Handlers (gnat_node)))
3500 {
3501 expand_end_bindings (getdecls (), kept_level_p (), 0);
3502 poplevel (kept_level_p (), 1, 0);
3503 }
3504
3505 break;
3506
3507 case N_Exception_Handler:
3508 if (Exception_Mechanism == Setjmp_Longjmp)
3509 {
3510 /* Unless this is "Others" or the special "Non-Ada" exception
3511 for Ada, make an "if" statement to select the proper
3512 exceptions. For "Others", exclude exceptions where
3513 Handled_By_Others is nonzero unless the All_Others flag is set.
3514 For "Non-ada", accept an exception if "Lang" is 'V'. */
3515 tree gnu_choice = integer_zero_node;
3516
3517 for (gnat_temp = First (Exception_Choices (gnat_node));
3518 gnat_temp; gnat_temp = Next (gnat_temp))
3519 {
3520 tree this_choice;
3521
3522 if (Nkind (gnat_temp) == N_Others_Choice)
3523 {
3524 if (All_Others (gnat_temp))
3525 this_choice = integer_one_node;
3526 else
3527 this_choice
3528 = build_binary_op
3529 (EQ_EXPR, integer_type_node,
3530 convert
3531 (integer_type_node,
3532 build_component_ref
3533 (build_unary_op
3534 (INDIRECT_REF, NULL_TREE,
3535 TREE_VALUE (gnu_except_ptr_stack)),
3536 get_identifier ("not_handled_by_others"), NULL_TREE)),
3537 integer_zero_node);
3538 }
3539
3540 else if (Nkind (gnat_temp) == N_Identifier
3541 || Nkind (gnat_temp) == N_Expanded_Name)
3542 {
3543 /* ??? Note that we have to use gnat_to_gnu_entity here
3544 since the type of the exception will be wrong in the
3545 VMS case and that's exactly what this test is for. */
3546 gnu_expr
3547 = gnat_to_gnu_entity (Entity (gnat_temp), NULL_TREE, 0);
3548
3549 /* If this was a VMS exception, check import_code
3550 against the value of the exception. */
3551 if (TREE_CODE (TREE_TYPE (gnu_expr)) == INTEGER_TYPE)
3552 this_choice
3553 = build_binary_op
3554 (EQ_EXPR, integer_type_node,
3555 build_component_ref
3556 (build_unary_op
3557 (INDIRECT_REF, NULL_TREE,
3558 TREE_VALUE (gnu_except_ptr_stack)),
3559 get_identifier ("import_code"), NULL_TREE),
3560 gnu_expr);
3561 else
3562 this_choice
3563 = build_binary_op
3564 (EQ_EXPR, integer_type_node,
3565 TREE_VALUE (gnu_except_ptr_stack),
3566 convert
3567 (TREE_TYPE (TREE_VALUE (gnu_except_ptr_stack)),
3568 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr)));
3569
3570 /* If this is the distinguished exception "Non_Ada_Error"
3571 (and we are in VMS mode), also allow a non-Ada
3572 exception (a VMS condition) to match. */
3573 if (Is_Non_Ada_Error (Entity (gnat_temp)))
3574 {
3575 tree gnu_comp
3576 = build_component_ref
3577 (build_unary_op
3578 (INDIRECT_REF, NULL_TREE,
3579 TREE_VALUE (gnu_except_ptr_stack)),
3580 get_identifier ("lang"), NULL_TREE);
3581
3582 this_choice
3583 = build_binary_op
3584 (TRUTH_ORIF_EXPR, integer_type_node,
3585 build_binary_op
3586 (EQ_EXPR, integer_type_node, gnu_comp,
3587 convert (TREE_TYPE (gnu_comp),
3588 build_int_2 ('V', 0))),
3589 this_choice);
3590 }
3591 }
3592 else
3593 gigi_abort (318);
3594
3595 gnu_choice = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
3596 gnu_choice, this_choice);
3597 }
3598
3599 set_lineno (gnat_node, 1);
3600
3601 expand_start_cond (gnu_choice, 0);
3602 }
3603
3604 /* Tell the back end that we start an exception handler if necessary. */
3605 if (Exception_Mechanism == GCC_ZCX)
3606 {
3607 /* We build a TREE_LIST of nodes representing what exception
3608 types this handler is able to catch, with special cases
3609 for others and all others cases.
3610
3611 Each exception type is actually identified by a pointer to the
3612 exception id, with special value zero for "others" and one for
3613 "all others". Beware that these special values are known and used
3614 by the personality routine to identify the corresponding specific
3615 kinds of handlers.
3616
3617 ??? For initial time frame reasons, the others and all_others
3618 cases have been handled using specific type trees, but this
3619 somehow hides information to the back-end, which expects NULL to
3620 be passed for catch all and end_cleanup to be used for cleanups.
3621
3622 Care should be taken to ensure that the control flow impact of
3623 such clauses is rendered in some way. lang_eh_type_covers is
3624 doing the trick currently.
3625
3626 ??? Should investigate the possible usage of the end_cleanup
3627 interface in this context. */
3628
3629 tree gnu_expr, gnu_etype;
3630 tree gnu_etypes_list = NULL_TREE;
3631
3632 for (gnat_temp = First (Exception_Choices (gnat_node));
3633 gnat_temp; gnat_temp = Next (gnat_temp))
3634 {
3635 if (Nkind (gnat_temp) == N_Others_Choice)
3636 gnu_etype
3637 = All_Others (gnat_temp) ? integer_one_node
3638 : integer_zero_node;
3639 else if (Nkind (gnat_temp) == N_Identifier
3640 || Nkind (gnat_temp) == N_Expanded_Name)
3641 {
3642 gnu_expr = gnat_to_gnu_entity (Entity (gnat_temp),
3643 NULL_TREE, 0);
3644 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
3645 }
3646 else
3647 gigi_abort (337);
3648
3649 gnu_etypes_list
3650 = tree_cons (NULL_TREE, gnu_etype, gnu_etypes_list);
3651
3652 /* The GCC interface expects NULL to be passed for catch all
3653 handlers, so the approach below is quite tempting :
3654
3655 if (gnu_etype == integer_zero_node)
3656 gnu_etypes_list = NULL;
3657
3658 It would not work, however, because GCC's notion
3659 of "catch all" is stronger than our notion of "others".
3660
3661 Until we correctly use the cleanup interface as well, the
3662 two lines above will prevent the "all others" handlers from
3663 beeing seen, because nothing can be caught beyond a catch
3664 all from GCC's point of view. */
3665 }
3666
3667 expand_start_catch (gnu_etypes_list);
3668 }
3669
3670 for (gnat_temp = First (Statements (gnat_node));
3671 gnat_temp; gnat_temp = Next (gnat_temp))
3672 gnat_to_code (gnat_temp);
3673
3674 /* At the end of the handler, exit the block. We made this block
3675 in N_Handled_Sequence_Of_Statements. */
3676 expand_exit_something ();
3677
3678 /* Tell the back end that we're done with the current handler. */
3679 if (Exception_Mechanism == GCC_ZCX)
3680 expand_end_catch ();
3681 else if (Exception_Mechanism == Setjmp_Longjmp)
3682 expand_end_cond ();
3683
3684 break;
3685
3686 /*******************************/
3687 /* Chapter 12: Generic Units: */
3688 /*******************************/
3689
3690 case N_Generic_Function_Renaming_Declaration:
3691 case N_Generic_Package_Renaming_Declaration:
3692 case N_Generic_Procedure_Renaming_Declaration:
3693 case N_Generic_Package_Declaration:
3694 case N_Generic_Subprogram_Declaration:
3695 case N_Package_Instantiation:
3696 case N_Procedure_Instantiation:
3697 case N_Function_Instantiation:
3698 /* These nodes can appear on a declaration list but there is nothing to
3699 to be done with them. */
3700 break;
3701
3702 /***************************************************/
3703 /* Chapter 13: Representation Clauses and */
3704 /* Implementation-Dependent Features: */
3705 /***************************************************/
3706
3707 case N_Attribute_Definition_Clause:
3708
3709 /* The only one we need deal with is for 'Address. For the others, SEM
3710 puts the information elsewhere. We need only deal with 'Address
3711 if the object has a Freeze_Node (which it never will currently). */
3712 if (Get_Attribute_Id (Chars (gnat_node)) != Attr_Address
3713 || No (Freeze_Node (Entity (Name (gnat_node)))))
3714 break;
3715
3716 /* Get the value to use as the address and save it as the
3717 equivalent for GNAT_TEMP. When the object is frozen,
3718 gnat_to_gnu_entity will do the right thing. */
3719 gnu_expr = gnat_to_gnu (Expression (gnat_node));
3720 save_gnu_tree (Entity (Name (gnat_node)), gnu_expr, 1);
3721 break;
3722
3723 case N_Enumeration_Representation_Clause:
3724 case N_Record_Representation_Clause:
3725 case N_At_Clause:
3726 /* We do nothing with these. SEM puts the information elsewhere. */
3727 break;
3728
3729 case N_Code_Statement:
3730 if (! type_annotate_only)
3731 {
3732 tree gnu_template = gnat_to_gnu (Asm_Template (gnat_node));
3733 tree gnu_input_list = 0, gnu_output_list = 0, gnu_orig_out_list = 0;
3734 tree gnu_clobber_list = 0;
3735 char *clobber;
3736
3737 /* First process inputs, then outputs, then clobbers. */
3738 Setup_Asm_Inputs (gnat_node);
3739 while (Present (gnat_temp = Asm_Input_Value ()))
3740 {
3741 tree gnu_value = gnat_to_gnu (gnat_temp);
3742 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
3743 (Asm_Input_Constraint ()));
3744
3745 gnu_input_list
3746 = tree_cons (gnu_constr, gnu_value, gnu_input_list);
3747 Next_Asm_Input ();
3748 }
3749
3750 Setup_Asm_Outputs (gnat_node);
3751 while (Present (gnat_temp = Asm_Output_Variable ()))
3752 {
3753 tree gnu_value = gnat_to_gnu (gnat_temp);
3754 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
3755 (Asm_Output_Constraint ()));
3756
3757 gnu_orig_out_list
3758 = tree_cons (gnu_constr, gnu_value, gnu_orig_out_list);
3759 gnu_output_list
3760 = tree_cons (gnu_constr, gnu_value, gnu_output_list);
3761 Next_Asm_Output ();
3762 }
3763
3764 Clobber_Setup (gnat_node);
3765 while ((clobber = Clobber_Get_Next ()) != 0)
3766 gnu_clobber_list
3767 = tree_cons (NULL_TREE,
3768 build_string (strlen (clobber) + 1, clobber),
3769 gnu_clobber_list);
3770
3771 gnu_input_list = nreverse (gnu_input_list);
3772 gnu_output_list = nreverse (gnu_output_list);
3773 gnu_orig_out_list = nreverse (gnu_orig_out_list);
3774 expand_asm_operands (gnu_template, gnu_output_list, gnu_input_list,
3775 gnu_clobber_list, Is_Asm_Volatile (gnat_node),
3776 input_location);
3777
3778 /* Copy all the intermediate outputs into the specified outputs. */
3779 for (; gnu_output_list;
3780 (gnu_output_list = TREE_CHAIN (gnu_output_list),
3781 gnu_orig_out_list = TREE_CHAIN (gnu_orig_out_list)))
3782 if (TREE_VALUE (gnu_orig_out_list) != TREE_VALUE (gnu_output_list))
3783 {
3784 expand_expr_stmt
3785 (build_binary_op (MODIFY_EXPR, NULL_TREE,
3786 TREE_VALUE (gnu_orig_out_list),
3787 TREE_VALUE (gnu_output_list)));
3788 free_temp_slots ();
3789 }
3790 }
3791 break;
3792
3793 /***************************************************/
3794 /* Added Nodes */
3795 /***************************************************/
3796
3797 case N_Freeze_Entity:
3798 process_freeze_entity (gnat_node);
3799 process_decls (Actions (gnat_node), Empty, Empty, 1, 1);
3800 break;
3801
3802 case N_Itype_Reference:
3803 if (! present_gnu_tree (Itype (gnat_node)))
3804 process_type (Itype (gnat_node));
3805 break;
3806
3807 case N_Free_Statement:
3808 if (! type_annotate_only)
3809 {
3810 tree gnu_ptr = gnat_to_gnu (Expression (gnat_node));
3811 tree gnu_obj_type;
3812 tree gnu_obj_size;
3813 int align;
3814
3815 /* If this is an unconstrained array, we know the object must
3816 have been allocated with the template in front of the object.
3817 So pass the template address, but get the total size. Do this
3818 by converting to a thin pointer. */
3819 if (TYPE_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
3820 gnu_ptr
3821 = convert (build_pointer_type
3822 (TYPE_OBJECT_RECORD_TYPE
3823 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
3824 gnu_ptr);
3825
3826 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
3827 gnu_obj_size = TYPE_SIZE_UNIT (gnu_obj_type);
3828 align = TYPE_ALIGN (gnu_obj_type);
3829
3830 if (TREE_CODE (gnu_obj_type) == RECORD_TYPE
3831 && TYPE_CONTAINS_TEMPLATE_P (gnu_obj_type))
3832 {
3833 tree gnu_char_ptr_type = build_pointer_type (char_type_node);
3834 tree gnu_pos = byte_position (TYPE_FIELDS (gnu_obj_type));
3835 tree gnu_byte_offset
3836 = convert (gnu_char_ptr_type,
3837 size_diffop (size_zero_node, gnu_pos));
3838
3839 gnu_ptr = convert (gnu_char_ptr_type, gnu_ptr);
3840 gnu_ptr = build_binary_op (MINUS_EXPR, gnu_char_ptr_type,
3841 gnu_ptr, gnu_byte_offset);
3842 }
3843
3844 set_lineno (gnat_node, 1);
3845 expand_expr_stmt
3846 (build_call_alloc_dealloc (gnu_ptr, gnu_obj_size, align,
3847 Procedure_To_Call (gnat_node),
3848 Storage_Pool (gnat_node)));
3849 }
3850 break;
3851
3852 case N_Raise_Constraint_Error:
3853 case N_Raise_Program_Error:
3854 case N_Raise_Storage_Error:
3855
3856 if (type_annotate_only)
3857 break;
3858
3859 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3860 gnu_result = build_call_raise (UI_To_Int (Reason (gnat_node)));
3861
3862 /* If the type is VOID, this is a statement, so we need to
3863 generate the code for the call. Handle a Condition, if there
3864 is one. */
3865 if (TREE_CODE (gnu_result_type) == VOID_TYPE)
3866 {
3867 set_lineno (gnat_node, 1);
3868
3869 if (Present (Condition (gnat_node)))
3870 expand_start_cond (gnat_to_gnu (Condition (gnat_node)), 0);
3871
3872 expand_expr_stmt (gnu_result);
3873 if (Present (Condition (gnat_node)))
3874 expand_end_cond ();
3875 gnu_result = error_mark_node;
3876 }
3877 else
3878 gnu_result = build1 (NULL_EXPR, gnu_result_type, gnu_result);
3879 break;
3880
3881 /* Nothing to do, since front end does all validation using the
3882 values that Gigi back-annotates. */
3883 case N_Validate_Unchecked_Conversion:
3884 break;
3885
3886 case N_Raise_Statement:
3887 case N_Function_Specification:
3888 case N_Procedure_Specification:
3889 case N_Op_Concat:
3890 case N_Component_Association:
3891 case N_Task_Body:
3892 default:
3893 if (! type_annotate_only)
3894 gigi_abort (321);
3895 }
3896
3897 /* If the result is a constant that overflows, raise constraint error. */
3898 if (TREE_CODE (gnu_result) == INTEGER_CST
3899 && TREE_CONSTANT_OVERFLOW (gnu_result))
3900 {
3901 post_error ("Constraint_Error will be raised at run-time?", gnat_node);
3902
3903 gnu_result
3904 = build1 (NULL_EXPR, gnu_result_type,
3905 build_call_raise (CE_Overflow_Check_Failed));
3906 }
3907
3908 /* If our result has side-effects and is of an unconstrained type,
3909 make a SAVE_EXPR so that we can be sure it will only be referenced
3910 once. Note we must do this before any conversions. */
3911 if (TREE_SIDE_EFFECTS (gnu_result)
3912 && (TREE_CODE (gnu_result_type) == UNCONSTRAINED_ARRAY_TYPE
3913 || (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
3914 && contains_placeholder_p (TYPE_SIZE (gnu_result_type)))))
3915 gnu_result = gnat_stabilize_reference (gnu_result, 0);
3916
3917 /* Now convert the result to the proper type. If the type is void or if
3918 we have no result, return error_mark_node to show we have no result.
3919 If the type of the result is correct or if we have a label (which doesn't
3920 have any well-defined type), return our result. Also don't do the
3921 conversion if the "desired" type involves a PLACEHOLDER_EXPR in its size
3922 since those are the cases where the front end may have the type wrong due
3923 to "instantiating" the unconstrained record with discriminant values
3924 or if this is a FIELD_DECL. If this is the Name of an assignment
3925 statement or a parameter of a procedure call, return what we have since
3926 the RHS has to be converted to our type there in that case, unless
3927 GNU_RESULT_TYPE has a simpler size. Similarly, if the two types are
3928 record types with the same name, the expression type has integral mode,
3929 and GNU_RESULT_TYPE BLKmode, don't convert. This will be the case when
3930 we are converting from a packable type to its actual type and we need
3931 those conversions to be NOPs in order for assignments into these types to
3932 work properly if the inner object is a bitfield and hence can't have
3933 its address taken. Finally, don't convert integral types that are the
3934 operand of an unchecked conversion since we need to ignore those
3935 conversions (for 'Valid). Otherwise, convert the result to the proper
3936 type. */
3937
3938 if (Present (Parent (gnat_node))
3939 && ((Nkind (Parent (gnat_node)) == N_Assignment_Statement
3940 && Name (Parent (gnat_node)) == gnat_node)
3941 || (Nkind (Parent (gnat_node)) == N_Procedure_Call_Statement
3942 && Name (Parent (gnat_node)) != gnat_node)
3943 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
3944 && ! AGGREGATE_TYPE_P (gnu_result_type)
3945 && ! AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)))
3946 || Nkind (Parent (gnat_node)) == N_Parameter_Association)
3947 && ! (TYPE_SIZE (gnu_result_type) != 0
3948 && TYPE_SIZE (TREE_TYPE (gnu_result)) != 0
3949 && (AGGREGATE_TYPE_P (gnu_result_type)
3950 == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)))
3951 && ((TREE_CODE (TYPE_SIZE (gnu_result_type)) == INTEGER_CST
3952 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result)))
3953 != INTEGER_CST))
3954 || (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
3955 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result)))
3956 != INTEGER_CST)
3957 && ! (contains_placeholder_p (TYPE_SIZE (gnu_result_type)))
3958 && (contains_placeholder_p
3959 (TYPE_SIZE (TREE_TYPE (gnu_result))))))
3960 && ! (TREE_CODE (gnu_result_type) == RECORD_TYPE
3961 && TYPE_LEFT_JUSTIFIED_MODULAR_P (gnu_result_type))))
3962 {
3963 /* In this case remove padding only if the inner object is of
3964 self-referential size: in that case it must be an object of
3965 unconstrained type with a default discriminant. In other cases,
3966 we want to avoid copying too much data. */
3967 if (TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE
3968 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result))
3969 && contains_placeholder_p (TYPE_SIZE
3970 (TREE_TYPE (TYPE_FIELDS
3971 (TREE_TYPE (gnu_result))))))
3972 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
3973 gnu_result);
3974 }
3975
3976 else if (TREE_CODE (gnu_result) == LABEL_DECL
3977 || TREE_CODE (gnu_result) == FIELD_DECL
3978 || TREE_CODE (gnu_result) == ERROR_MARK
3979 || (TYPE_SIZE (gnu_result_type) != 0
3980 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
3981 && TREE_CODE (gnu_result) != INDIRECT_REF
3982 && contains_placeholder_p (TYPE_SIZE (gnu_result_type)))
3983 || ((TYPE_NAME (gnu_result_type)
3984 == TYPE_NAME (TREE_TYPE (gnu_result)))
3985 && TREE_CODE (gnu_result_type) == RECORD_TYPE
3986 && TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE
3987 && TYPE_MODE (gnu_result_type) == BLKmode
3988 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (gnu_result)))
3989 == MODE_INT)))
3990 {
3991 /* Remove any padding record, but do nothing more in this case. */
3992 if (TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE
3993 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
3994 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
3995 gnu_result);
3996 }
3997
3998 else if (gnu_result == error_mark_node
3999 || gnu_result_type == void_type_node)
4000 gnu_result = error_mark_node;
4001 else if (gnu_result_type != TREE_TYPE (gnu_result))
4002 gnu_result = convert (gnu_result_type, gnu_result);
4003
4004 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on GNU_RESULT. */
4005 while ((TREE_CODE (gnu_result) == NOP_EXPR
4006 || TREE_CODE (gnu_result) == NON_LVALUE_EXPR)
4007 && TREE_TYPE (TREE_OPERAND (gnu_result, 0)) == TREE_TYPE (gnu_result))
4008 gnu_result = TREE_OPERAND (gnu_result, 0);
4009
4010 return gnu_result;
4011 }
4012 \f
4013 /* Force references to each of the entities in packages GNAT_NODE with's
4014 so that the debugging information for all of them are identical
4015 in all clients. Operate recursively on anything it with's, but check
4016 that we aren't elaborating something more than once. */
4017
4018 /* The reason for this routine's existence is two-fold.
4019 First, with some debugging formats, notably MDEBUG on SGI
4020 IRIX, the linker will remove duplicate debugging information if two
4021 clients have identical debugguing information. With the normal scheme
4022 of elaboration, this does not usually occur, since entities in with'ed
4023 packages are elaborated on demand, and if clients have different usage
4024 patterns, the normal case, then the order and selection of entities
4025 will differ. In most cases however, it seems that linkers do not know
4026 how to eliminate duplicate debugging information, even if it is
4027 identical, so the use of this routine would increase the total amount
4028 of debugging information in the final executable.
4029
4030 Second, this routine is called in type_annotate mode, to compute DDA
4031 information for types in withed units, for ASIS use */
4032
4033 static void
4034 elaborate_all_entities (gnat_node)
4035 Node_Id gnat_node;
4036 {
4037 Entity_Id gnat_with_clause, gnat_entity;
4038
4039 save_gnu_tree (gnat_node, integer_zero_node, 1);
4040
4041 /* Save entities in all context units. A body may have an implicit_with
4042 on its own spec, if the context includes a child unit, so don't save
4043 the spec twice. */
4044
4045 for (gnat_with_clause = First (Context_Items (gnat_node));
4046 Present (gnat_with_clause);
4047 gnat_with_clause = Next (gnat_with_clause))
4048 if (Nkind (gnat_with_clause) == N_With_Clause
4049 && ! present_gnu_tree (Library_Unit (gnat_with_clause))
4050 && Library_Unit (gnat_with_clause) != Library_Unit (Cunit (Main_Unit)))
4051 {
4052 elaborate_all_entities (Library_Unit (gnat_with_clause));
4053
4054 if (Ekind (Entity (Name (gnat_with_clause))) == E_Package)
4055 for (gnat_entity = First_Entity (Entity (Name (gnat_with_clause)));
4056 Present (gnat_entity);
4057 gnat_entity = Next_Entity (gnat_entity))
4058 if (Is_Public (gnat_entity)
4059 && Convention (gnat_entity) != Convention_Intrinsic
4060 && Ekind (gnat_entity) != E_Package
4061 && Ekind (gnat_entity) != E_Package_Body
4062 && Ekind (gnat_entity) != E_Operator
4063 && ! (IN (Ekind (gnat_entity), Type_Kind)
4064 && ! Is_Frozen (gnat_entity))
4065 && ! ((Ekind (gnat_entity) == E_Procedure
4066 || Ekind (gnat_entity) == E_Function)
4067 && Is_Intrinsic_Subprogram (gnat_entity))
4068 && ! IN (Ekind (gnat_entity), Named_Kind)
4069 && ! IN (Ekind (gnat_entity), Generic_Unit_Kind))
4070 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4071 }
4072
4073 if (Nkind (Unit (gnat_node)) == N_Package_Body && type_annotate_only)
4074 elaborate_all_entities (Library_Unit (gnat_node));
4075 }
4076 \f
4077 /* Do the processing of N_Freeze_Entity, GNAT_NODE. */
4078
4079 static void
4080 process_freeze_entity (gnat_node)
4081 Node_Id gnat_node;
4082 {
4083 Entity_Id gnat_entity = Entity (gnat_node);
4084 tree gnu_old;
4085 tree gnu_new;
4086 tree gnu_init
4087 = (Nkind (Declaration_Node (gnat_entity)) == N_Object_Declaration
4088 && present_gnu_tree (Declaration_Node (gnat_entity)))
4089 ? get_gnu_tree (Declaration_Node (gnat_entity)) : NULL_TREE;
4090
4091 /* If this is a package, need to generate code for the package. */
4092 if (Ekind (gnat_entity) == E_Package)
4093 {
4094 insert_code_for
4095 (Parent (Corresponding_Body
4096 (Parent (Declaration_Node (gnat_entity)))));
4097 return;
4098 }
4099
4100 /* Check for old definition after the above call. This Freeze_Node
4101 might be for one its Itypes. */
4102 gnu_old
4103 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : 0;
4104
4105 /* If this entity has an Address representation clause, GNU_OLD is the
4106 address, so discard it here. */
4107 if (Present (Address_Clause (gnat_entity)))
4108 gnu_old = 0;
4109
4110 /* Don't do anything for class-wide types they are always
4111 transformed into their root type. */
4112 if (Ekind (gnat_entity) == E_Class_Wide_Type
4113 || (Ekind (gnat_entity) == E_Class_Wide_Subtype
4114 && Present (Equivalent_Type (gnat_entity))))
4115 return;
4116
4117 /* Don't do anything for subprograms that may have been elaborated before
4118 their freeze nodes. This can happen, for example because of an inner call
4119 in an instance body. */
4120 if (gnu_old != 0
4121 && TREE_CODE (gnu_old) == FUNCTION_DECL
4122 && (Ekind (gnat_entity) == E_Function
4123 || Ekind (gnat_entity) == E_Procedure))
4124 return;
4125
4126 /* If we have a non-dummy type old tree, we have nothing to do. Unless
4127 this is the public view of a private type whose full view was not
4128 delayed, this node was never delayed as it should have been.
4129 Also allow this to happen for concurrent types since we may have
4130 frozen both the Corresponding_Record_Type and this type. */
4131 if (gnu_old != 0
4132 && ! (TREE_CODE (gnu_old) == TYPE_DECL
4133 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old))))
4134 {
4135 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
4136 && Present (Full_View (gnat_entity))
4137 && No (Freeze_Node (Full_View (gnat_entity))))
4138 return;
4139 else if (Is_Concurrent_Type (gnat_entity))
4140 return;
4141 else
4142 gigi_abort (320);
4143 }
4144
4145 /* Reset the saved tree, if any, and elaborate the object or type for real.
4146 If there is a full declaration, elaborate it and copy the type to
4147 GNAT_ENTITY. Likewise if this is the record subtype corresponding to
4148 a class wide type or subtype. */
4149 if (gnu_old != 0)
4150 {
4151 save_gnu_tree (gnat_entity, NULL_TREE, 0);
4152 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
4153 && Present (Full_View (gnat_entity))
4154 && present_gnu_tree (Full_View (gnat_entity)))
4155 save_gnu_tree (Full_View (gnat_entity), NULL_TREE, 0);
4156 if (Present (Class_Wide_Type (gnat_entity))
4157 && Class_Wide_Type (gnat_entity) != gnat_entity)
4158 save_gnu_tree (Class_Wide_Type (gnat_entity), NULL_TREE, 0);
4159 }
4160
4161 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
4162 && Present (Full_View (gnat_entity)))
4163 {
4164 gnu_new = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 1);
4165
4166 /* The above call may have defined this entity (the simplest example
4167 of this is when we have a private enumeral type since the bounds
4168 will have the public view. */
4169 if (! present_gnu_tree (gnat_entity))
4170 save_gnu_tree (gnat_entity, gnu_new, 0);
4171 if (Present (Class_Wide_Type (gnat_entity))
4172 && Class_Wide_Type (gnat_entity) != gnat_entity)
4173 save_gnu_tree (Class_Wide_Type (gnat_entity), gnu_new, 0);
4174 }
4175 else
4176 gnu_new = gnat_to_gnu_entity (gnat_entity, gnu_init, 1);
4177
4178 /* If we've made any pointers to the old version of this type, we
4179 have to update them. */
4180 if (gnu_old != 0)
4181 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
4182 TREE_TYPE (gnu_new));
4183 }
4184 \f
4185 /* Process the list of inlined subprograms of GNAT_NODE, which is an
4186 N_Compilation_Unit. */
4187
4188 static void
4189 process_inlined_subprograms (gnat_node)
4190 Node_Id gnat_node;
4191 {
4192 Entity_Id gnat_entity;
4193 Node_Id gnat_body;
4194
4195 /* If we can inline, generate RTL for all the inlined subprograms.
4196 Define the entity first so we set DECL_EXTERNAL. */
4197 if (optimize > 0 && ! flag_no_inline)
4198 for (gnat_entity = First_Inlined_Subprogram (gnat_node);
4199 Present (gnat_entity);
4200 gnat_entity = Next_Inlined_Subprogram (gnat_entity))
4201 {
4202 gnat_body = Parent (Declaration_Node (gnat_entity));
4203
4204 if (Nkind (gnat_body) != N_Subprogram_Body)
4205 {
4206 /* ??? This really should always be Present. */
4207 if (No (Corresponding_Body (gnat_body)))
4208 continue;
4209
4210 gnat_body
4211 = Parent (Declaration_Node (Corresponding_Body (gnat_body)));
4212 }
4213
4214 if (Present (gnat_body))
4215 {
4216 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4217 gnat_to_code (gnat_body);
4218 }
4219 }
4220 }
4221 \f
4222 /* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present.
4223 We make two passes, one to elaborate anything other than bodies (but
4224 we declare a function if there was no spec). The second pass
4225 elaborates the bodies.
4226
4227 GNAT_END_LIST gives the element in the list past the end. Normally,
4228 this is Empty, but can be First_Real_Statement for a
4229 Handled_Sequence_Of_Statements.
4230
4231 We make a complete pass through both lists if PASS1P is true, then make
4232 the second pass over both lists if PASS2P is true. The lists usually
4233 correspond to the public and private parts of a package. */
4234
4235 static void
4236 process_decls (gnat_decls, gnat_decls2, gnat_end_list, pass1p, pass2p)
4237 List_Id gnat_decls, gnat_decls2;
4238 Node_Id gnat_end_list;
4239 int pass1p, pass2p;
4240 {
4241 List_Id gnat_decl_array[2];
4242 Node_Id gnat_decl;
4243 int i;
4244
4245 gnat_decl_array[0] = gnat_decls, gnat_decl_array[1] = gnat_decls2;
4246
4247 if (pass1p)
4248 for (i = 0; i <= 1; i++)
4249 if (Present (gnat_decl_array[i]))
4250 for (gnat_decl = First (gnat_decl_array[i]);
4251 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
4252 {
4253 set_lineno (gnat_decl, 0);
4254
4255 /* For package specs, we recurse inside the declarations,
4256 thus taking the two pass approach inside the boundary. */
4257 if (Nkind (gnat_decl) == N_Package_Declaration
4258 && (Nkind (Specification (gnat_decl)
4259 == N_Package_Specification)))
4260 process_decls (Visible_Declarations (Specification (gnat_decl)),
4261 Private_Declarations (Specification (gnat_decl)),
4262 Empty, 1, 0);
4263
4264 /* Similarly for any declarations in the actions of a
4265 freeze node. */
4266 else if (Nkind (gnat_decl) == N_Freeze_Entity)
4267 {
4268 process_freeze_entity (gnat_decl);
4269 process_decls (Actions (gnat_decl), Empty, Empty, 1, 0);
4270 }
4271
4272 /* Package bodies with freeze nodes get their elaboration deferred
4273 until the freeze node, but the code must be placed in the right
4274 place, so record the code position now. */
4275 else if (Nkind (gnat_decl) == N_Package_Body
4276 && Present (Freeze_Node (Corresponding_Spec (gnat_decl))))
4277 record_code_position (gnat_decl);
4278
4279 else if (Nkind (gnat_decl) == N_Package_Body_Stub
4280 && Present (Library_Unit (gnat_decl))
4281 && Present (Freeze_Node
4282 (Corresponding_Spec
4283 (Proper_Body (Unit
4284 (Library_Unit (gnat_decl)))))))
4285 record_code_position
4286 (Proper_Body (Unit (Library_Unit (gnat_decl))));
4287
4288 /* We defer most subprogram bodies to the second pass.
4289 However, Init_Proc subprograms cannot be defered, but luckily
4290 don't need to be. */
4291 else if ((Nkind (gnat_decl) == N_Subprogram_Body
4292 && (Chars (Defining_Entity (gnat_decl))
4293 != Name_uInit_Proc)))
4294 {
4295 if (Acts_As_Spec (gnat_decl))
4296 {
4297 Node_Id gnat_subprog_id = Defining_Entity (gnat_decl);
4298
4299 if (Ekind (gnat_subprog_id) != E_Generic_Procedure
4300 && Ekind (gnat_subprog_id) != E_Generic_Function)
4301 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
4302 }
4303 }
4304 /* For bodies and stubs that act as their own specs, the entity
4305 itself must be elaborated in the first pass, because it may
4306 be used in other declarations. */
4307 else if (Nkind (gnat_decl) == N_Subprogram_Body_Stub)
4308 {
4309 Node_Id gnat_subprog_id =
4310 Defining_Entity (Specification (gnat_decl));
4311
4312 if (Ekind (gnat_subprog_id) != E_Subprogram_Body
4313 && Ekind (gnat_subprog_id) != E_Generic_Procedure
4314 && Ekind (gnat_subprog_id) != E_Generic_Function)
4315 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
4316 }
4317
4318 /* Concurrent stubs stand for the corresponding subprogram bodies,
4319 which are deferred like other bodies. */
4320 else if (Nkind (gnat_decl) == N_Task_Body_Stub
4321 || Nkind (gnat_decl) == N_Protected_Body_Stub)
4322 ;
4323
4324 else
4325 gnat_to_code (gnat_decl);
4326 }
4327
4328 /* Here we elaborate everything we deferred above except for package bodies,
4329 which are elaborated at their freeze nodes. Note that we must also
4330 go inside things (package specs and freeze nodes) the first pass did. */
4331 if (pass2p)
4332 for (i = 0; i <= 1; i++)
4333 if (Present (gnat_decl_array[i]))
4334 for (gnat_decl = First (gnat_decl_array[i]);
4335 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
4336 {
4337 if ((Nkind (gnat_decl) == N_Subprogram_Body
4338 && (Chars (Defining_Entity (gnat_decl))
4339 != Name_uInit_Proc))
4340 || Nkind (gnat_decl) == N_Subprogram_Body_Stub
4341 || Nkind (gnat_decl) == N_Task_Body_Stub
4342 || Nkind (gnat_decl) == N_Protected_Body_Stub)
4343 gnat_to_code (gnat_decl);
4344
4345 else if (Nkind (gnat_decl) == N_Package_Declaration
4346 && (Nkind (Specification (gnat_decl)
4347 == N_Package_Specification)))
4348 process_decls (Visible_Declarations (Specification (gnat_decl)),
4349 Private_Declarations (Specification (gnat_decl)),
4350 Empty, 0, 1);
4351
4352 else if (Nkind (gnat_decl) == N_Freeze_Entity)
4353 process_decls (Actions (gnat_decl), Empty, Empty, 0, 1);
4354 }
4355 }
4356 \f
4357 /* Emits an access check. GNU_EXPR is the expression that needs to be
4358 checked against the NULL pointer. */
4359
4360 static tree
4361 emit_access_check (gnu_expr)
4362 tree gnu_expr;
4363 {
4364 tree gnu_check_expr;
4365
4366 /* Checked expressions must be evaluated only once. */
4367 gnu_check_expr = gnu_expr = protect_multiple_eval (gnu_expr);
4368
4369 /* Technically, we check a fat pointer against two words of zero. However,
4370 that's wasteful and really doesn't protect against null accesses. It
4371 makes more sense to check oly the array pointer. */
4372 if (TYPE_FAT_POINTER_P (TREE_TYPE (gnu_expr)))
4373 gnu_check_expr
4374 = build_component_ref (gnu_expr, get_identifier ("P_ARRAY"), NULL_TREE);
4375
4376 if (! POINTER_TYPE_P (TREE_TYPE (gnu_check_expr)))
4377 gigi_abort (322);
4378
4379 return emit_check (build_binary_op (EQ_EXPR, integer_type_node,
4380 gnu_check_expr,
4381 convert (TREE_TYPE (gnu_check_expr),
4382 integer_zero_node)),
4383 gnu_expr,
4384 CE_Access_Check_Failed);
4385 }
4386
4387 /* Emits a discriminant check. GNU_EXPR is the expression to be checked and
4388 GNAT_NODE a N_Selected_Component node. */
4389
4390 static tree
4391 emit_discriminant_check (gnu_expr, gnat_node)
4392 tree gnu_expr;
4393 Node_Id gnat_node;
4394 {
4395 Entity_Id orig_comp
4396 = Original_Record_Component (Entity (Selector_Name (gnat_node)));
4397 Entity_Id gnat_discr_fct = Discriminant_Checking_Func (orig_comp);
4398 tree gnu_discr_fct;
4399 Entity_Id gnat_discr;
4400 tree gnu_actual_list = NULL_TREE;
4401 tree gnu_cond;
4402 Entity_Id gnat_pref_type;
4403 tree gnu_pref_type;
4404
4405 if (Is_Tagged_Type (Scope (orig_comp)))
4406 gnat_pref_type = Scope (orig_comp);
4407 else
4408 {
4409 gnat_pref_type = Etype (Prefix (gnat_node));
4410
4411 /* For an untagged derived type, use the discriminants of the parent,
4412 which have been renamed in the derivation, possibly by a one-to-many
4413 constraint. */
4414 if (Is_Derived_Type (gnat_pref_type)
4415 && (Number_Discriminants (gnat_pref_type)
4416 != Number_Discriminants (Etype (Base_Type (gnat_pref_type)))))
4417 gnat_pref_type = Etype (Base_Type (gnat_pref_type));
4418 }
4419
4420 if (! Present (gnat_discr_fct))
4421 return gnu_expr;
4422
4423 gnu_discr_fct = gnat_to_gnu (gnat_discr_fct);
4424
4425 /* Checked expressions must be evaluated only once. */
4426 gnu_expr = protect_multiple_eval (gnu_expr);
4427
4428 /* Create the list of the actual parameters as GCC expects it.
4429 This list is the list of the discriminant fields of the
4430 record expression to be discriminant checked. For documentation
4431 on what is the GCC format for this list see under the
4432 N_Function_Call case */
4433
4434 while (IN (Ekind (gnat_pref_type), Incomplete_Or_Private_Kind)
4435 || IN (Ekind (gnat_pref_type), Access_Kind))
4436 {
4437 if (IN (Ekind (gnat_pref_type), Incomplete_Or_Private_Kind))
4438 gnat_pref_type = Underlying_Type (gnat_pref_type);
4439 else if (IN (Ekind (gnat_pref_type), Access_Kind))
4440 gnat_pref_type = Designated_Type (gnat_pref_type);
4441 }
4442
4443 gnu_pref_type
4444 = TREE_TYPE (gnat_to_gnu_entity (gnat_pref_type, NULL_TREE, 0));
4445
4446 for (gnat_discr = First_Discriminant (gnat_pref_type);
4447 Present (gnat_discr); gnat_discr = Next_Discriminant (gnat_discr))
4448 {
4449 Entity_Id gnat_real_discr
4450 = ((Present (Corresponding_Discriminant (gnat_discr))
4451 && Present (Parent_Subtype (gnat_pref_type)))
4452 ? Corresponding_Discriminant (gnat_discr) : gnat_discr);
4453 tree gnu_discr = gnat_to_gnu_entity (gnat_real_discr, NULL_TREE, 0);
4454
4455 gnu_actual_list
4456 = chainon (gnu_actual_list,
4457 build_tree_list (NULL_TREE,
4458 build_component_ref
4459 (convert (gnu_pref_type, gnu_expr),
4460 NULL_TREE, gnu_discr)));
4461 }
4462
4463 gnu_cond = build (CALL_EXPR,
4464 TREE_TYPE (TREE_TYPE (gnu_discr_fct)),
4465 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_discr_fct),
4466 gnu_actual_list,
4467 NULL_TREE);
4468 TREE_SIDE_EFFECTS (gnu_cond) = 1;
4469
4470 return
4471 build_unary_op
4472 (INDIRECT_REF, NULL_TREE,
4473 emit_check (gnu_cond,
4474 build_unary_op (ADDR_EXPR,
4475 build_reference_type (TREE_TYPE (gnu_expr)),
4476 gnu_expr),
4477 CE_Discriminant_Check_Failed));
4478 }
4479 \f
4480 /* Emit code for a range check. GNU_EXPR is the expression to be checked,
4481 GNAT_RANGE_TYPE the gnat type or subtype containing the bounds against
4482 which we have to check. */
4483
4484 static tree
4485 emit_range_check (gnu_expr, gnat_range_type)
4486 tree gnu_expr;
4487 Entity_Id gnat_range_type;
4488 {
4489 tree gnu_range_type = get_unpadded_type (gnat_range_type);
4490 tree gnu_low = TYPE_MIN_VALUE (gnu_range_type);
4491 tree gnu_high = TYPE_MAX_VALUE (gnu_range_type);
4492 tree gnu_compare_type = get_base_type (TREE_TYPE (gnu_expr));
4493
4494 /* If GNU_EXPR has an integral type that is narrower than GNU_RANGE_TYPE,
4495 we can't do anything since we might be truncating the bounds. No
4496 check is needed in this case. */
4497 if (INTEGRAL_TYPE_P (TREE_TYPE (gnu_expr))
4498 && (TYPE_PRECISION (gnu_compare_type)
4499 < TYPE_PRECISION (get_base_type (gnu_range_type))))
4500 return gnu_expr;
4501
4502 /* Checked expressions must be evaluated only once. */
4503 gnu_expr = protect_multiple_eval (gnu_expr);
4504
4505 /* There's no good type to use here, so we might as well use
4506 integer_type_node. Note that the form of the check is
4507 (not (expr >= lo)) or (not (expr >= hi))
4508 the reason for this slightly convoluted form is that NaN's
4509 are not considered to be in range in the float case. */
4510 return emit_check
4511 (build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
4512 invert_truthvalue
4513 (build_binary_op (GE_EXPR, integer_type_node,
4514 convert (gnu_compare_type, gnu_expr),
4515 convert (gnu_compare_type, gnu_low))),
4516 invert_truthvalue
4517 (build_binary_op (LE_EXPR, integer_type_node,
4518 convert (gnu_compare_type, gnu_expr),
4519 convert (gnu_compare_type,
4520 gnu_high)))),
4521 gnu_expr, CE_Range_Check_Failed);
4522 }
4523 \f
4524 /* Emit code for an index check. GNU_ARRAY_OBJECT is the array object
4525 which we are about to index, GNU_EXPR is the index expression to be
4526 checked, GNU_LOW and GNU_HIGH are the lower and upper bounds
4527 against which GNU_EXPR has to be checked. Note that for index
4528 checking we cannot use the emit_range_check function (although very
4529 similar code needs to be generated in both cases) since for index
4530 checking the array type against which we are checking the indeces
4531 may be unconstrained and consequently we need to retrieve the
4532 actual index bounds from the array object itself
4533 (GNU_ARRAY_OBJECT). The place where we need to do that is in
4534 subprograms having unconstrained array formal parameters */
4535
4536 static tree
4537 emit_index_check (gnu_array_object, gnu_expr, gnu_low, gnu_high)
4538 tree gnu_array_object;
4539 tree gnu_expr;
4540 tree gnu_low;
4541 tree gnu_high;
4542 {
4543 tree gnu_expr_check;
4544
4545 /* Checked expressions must be evaluated only once. */
4546 gnu_expr = protect_multiple_eval (gnu_expr);
4547
4548 /* Must do this computation in the base type in case the expression's
4549 type is an unsigned subtypes. */
4550 gnu_expr_check = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
4551
4552 /* If GNU_LOW or GNU_HIGH are a PLACEHOLDER_EXPR, qualify them by
4553 the object we are handling. */
4554 if (TREE_CODE (gnu_low) != INTEGER_CST && contains_placeholder_p (gnu_low))
4555 gnu_low = build (WITH_RECORD_EXPR, TREE_TYPE (gnu_low),
4556 gnu_low, gnu_array_object);
4557
4558 if (TREE_CODE (gnu_high) != INTEGER_CST && contains_placeholder_p (gnu_high))
4559 gnu_high = build (WITH_RECORD_EXPR, TREE_TYPE (gnu_high),
4560 gnu_high, gnu_array_object);
4561
4562 /* There's no good type to use here, so we might as well use
4563 integer_type_node. */
4564 return emit_check
4565 (build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
4566 build_binary_op (LT_EXPR, integer_type_node,
4567 gnu_expr_check,
4568 convert (TREE_TYPE (gnu_expr_check),
4569 gnu_low)),
4570 build_binary_op (GT_EXPR, integer_type_node,
4571 gnu_expr_check,
4572 convert (TREE_TYPE (gnu_expr_check),
4573 gnu_high))),
4574 gnu_expr, CE_Index_Check_Failed);
4575 }
4576 \f
4577 /* Given GNU_COND which contains the condition corresponding to an access,
4578 discriminant or range check, of value GNU_EXPR, build a COND_EXPR
4579 that returns GNU_EXPR if GNU_COND is false and raises a
4580 CONSTRAINT_ERROR if GNU_COND is true. REASON is the code that says
4581 why the exception was raised. */
4582
4583 static tree
4584 emit_check (gnu_cond, gnu_expr, reason)
4585 tree gnu_cond;
4586 tree gnu_expr;
4587 int reason;
4588 {
4589 tree gnu_call;
4590 tree gnu_result;
4591
4592 gnu_call = build_call_raise (reason);
4593
4594 /* Use an outer COMPOUND_EXPR to make sure that GNU_EXPR will get evaluated
4595 in front of the comparison in case it ends up being a SAVE_EXPR. Put the
4596 whole thing inside its own SAVE_EXPR so the inner SAVE_EXPR doesn't leak
4597 out. */
4598 gnu_result = fold (build (COND_EXPR, TREE_TYPE (gnu_expr), gnu_cond,
4599 build (COMPOUND_EXPR, TREE_TYPE (gnu_expr),
4600 gnu_call, gnu_expr),
4601 gnu_expr));
4602
4603 /* If GNU_EXPR has side effects, make the outer COMPOUND_EXPR and
4604 protect it. Otherwise, show GNU_RESULT has no side effects: we
4605 don't need to evaluate it just for the check. */
4606 if (TREE_SIDE_EFFECTS (gnu_expr))
4607 gnu_result
4608 = build (COMPOUND_EXPR, TREE_TYPE (gnu_expr), gnu_expr, gnu_result);
4609 else
4610 TREE_SIDE_EFFECTS (gnu_result) = 0;
4611
4612 /* ??? Unfortunately, if we don't put a SAVE_EXPR around this whole thing,
4613 we will repeatedly do the test. It would be nice if GCC was able
4614 to optimize this and only do it once. */
4615 return save_expr (gnu_result);
4616 }
4617 \f
4618 /* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing
4619 overflow checks if OVERFLOW_P is nonzero and range checks if
4620 RANGE_P is nonzero. GNAT_TYPE is known to be an integral type.
4621 If TRUNCATE_P is nonzero, do a float to integer conversion with
4622 truncation; otherwise round. */
4623
4624 static tree
4625 convert_with_check (gnat_type, gnu_expr, overflow_p, range_p, truncate_p)
4626 Entity_Id gnat_type;
4627 tree gnu_expr;
4628 int overflow_p;
4629 int range_p;
4630 int truncate_p;
4631 {
4632 tree gnu_type = get_unpadded_type (gnat_type);
4633 tree gnu_in_type = TREE_TYPE (gnu_expr);
4634 tree gnu_in_basetype = get_base_type (gnu_in_type);
4635 tree gnu_base_type = get_base_type (gnu_type);
4636 tree gnu_ada_base_type = get_ada_base_type (gnu_type);
4637 tree gnu_in_lb = TYPE_MIN_VALUE (gnu_in_basetype);
4638 tree gnu_in_ub = TYPE_MAX_VALUE (gnu_in_basetype);
4639 tree gnu_out_lb = TYPE_MIN_VALUE (gnu_base_type);
4640 tree gnu_out_ub = TYPE_MAX_VALUE (gnu_base_type);
4641 tree gnu_result = gnu_expr;
4642
4643 /* If we are not doing any checks, the output is an integral type, and
4644 the input is not a floating type, just do the conversion. This
4645 shortcut is required to avoid problems with packed array types
4646 and simplifies code in all cases anyway. */
4647 if (! range_p && ! overflow_p && INTEGRAL_TYPE_P (gnu_base_type)
4648 && ! FLOAT_TYPE_P (gnu_in_type))
4649 return convert (gnu_type, gnu_expr);
4650
4651 /* First convert the expression to its base type. This
4652 will never generate code, but makes the tests below much simpler.
4653 But don't do this if converting from an integer type to an unconstrained
4654 array type since then we need to get the bounds from the original
4655 (unpacked) type. */
4656 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
4657 gnu_result = convert (gnu_in_basetype, gnu_result);
4658
4659 /* If overflow checks are requested, we need to be sure the result will
4660 fit in the output base type. But don't do this if the input
4661 is integer and the output floating-point. */
4662 if (overflow_p
4663 && ! (FLOAT_TYPE_P (gnu_base_type) && INTEGRAL_TYPE_P (gnu_in_basetype)))
4664 {
4665 /* Ensure GNU_EXPR only gets evaluated once. */
4666 tree gnu_input = protect_multiple_eval (gnu_result);
4667 tree gnu_cond = integer_zero_node;
4668
4669 /* Convert the lower bounds to signed types, so we're sure we're
4670 comparing them properly. Likewise, convert the upper bounds
4671 to unsigned types. */
4672 if (INTEGRAL_TYPE_P (gnu_in_basetype) && TREE_UNSIGNED (gnu_in_basetype))
4673 gnu_in_lb = convert (gnat_signed_type (gnu_in_basetype), gnu_in_lb);
4674
4675 if (INTEGRAL_TYPE_P (gnu_in_basetype)
4676 && ! TREE_UNSIGNED (gnu_in_basetype))
4677 gnu_in_ub = convert (gnat_unsigned_type (gnu_in_basetype), gnu_in_ub);
4678
4679 if (INTEGRAL_TYPE_P (gnu_base_type) && TREE_UNSIGNED (gnu_base_type))
4680 gnu_out_lb = convert (gnat_signed_type (gnu_base_type), gnu_out_lb);
4681
4682 if (INTEGRAL_TYPE_P (gnu_base_type) && ! TREE_UNSIGNED (gnu_base_type))
4683 gnu_out_ub = convert (gnat_unsigned_type (gnu_base_type), gnu_out_ub);
4684
4685 /* Check each bound separately and only if the result bound
4686 is tighter than the bound on the input type. Note that all the
4687 types are base types, so the bounds must be constant. Also,
4688 the comparison is done in the base type of the input, which
4689 always has the proper signedness. First check for input
4690 integer (which means output integer), output float (which means
4691 both float), or mixed, in which case we always compare.
4692 Note that we have to do the comparison which would *fail* in the
4693 case of an error since if it's an FP comparison and one of the
4694 values is a NaN or Inf, the comparison will fail. */
4695 if (INTEGRAL_TYPE_P (gnu_in_basetype)
4696 ? tree_int_cst_lt (gnu_in_lb, gnu_out_lb)
4697 : (FLOAT_TYPE_P (gnu_base_type)
4698 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_in_lb),
4699 TREE_REAL_CST (gnu_out_lb))
4700 : 1))
4701 gnu_cond
4702 = invert_truthvalue
4703 (build_binary_op (GE_EXPR, integer_type_node,
4704 gnu_input, convert (gnu_in_basetype,
4705 gnu_out_lb)));
4706
4707 if (INTEGRAL_TYPE_P (gnu_in_basetype)
4708 ? tree_int_cst_lt (gnu_out_ub, gnu_in_ub)
4709 : (FLOAT_TYPE_P (gnu_base_type)
4710 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_out_ub),
4711 TREE_REAL_CST (gnu_in_lb))
4712 : 1))
4713 gnu_cond
4714 = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node, gnu_cond,
4715 invert_truthvalue
4716 (build_binary_op (LE_EXPR, integer_type_node,
4717 gnu_input,
4718 convert (gnu_in_basetype,
4719 gnu_out_ub))));
4720
4721 if (! integer_zerop (gnu_cond))
4722 gnu_result = emit_check (gnu_cond, gnu_input,
4723 CE_Overflow_Check_Failed);
4724 }
4725
4726 /* Now convert to the result base type. If this is a non-truncating
4727 float-to-integer conversion, round. */
4728 if (INTEGRAL_TYPE_P (gnu_ada_base_type) && FLOAT_TYPE_P (gnu_in_basetype)
4729 && ! truncate_p)
4730 {
4731 tree gnu_point_5 = build_real (gnu_in_basetype, dconstp5);
4732 tree gnu_minus_point_5 = build_real (gnu_in_basetype, dconstmp5);
4733 tree gnu_zero = convert (gnu_in_basetype, integer_zero_node);
4734 tree gnu_saved_result = save_expr (gnu_result);
4735 tree gnu_comp = build (GE_EXPR, integer_type_node,
4736 gnu_saved_result, gnu_zero);
4737 tree gnu_adjust = build (COND_EXPR, gnu_in_basetype, gnu_comp,
4738 gnu_point_5, gnu_minus_point_5);
4739
4740 gnu_result
4741 = build (PLUS_EXPR, gnu_in_basetype, gnu_saved_result, gnu_adjust);
4742 }
4743
4744 if (TREE_CODE (gnu_ada_base_type) == INTEGER_TYPE
4745 && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_ada_base_type)
4746 && TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
4747 gnu_result = unchecked_convert (gnu_ada_base_type, gnu_result);
4748 else
4749 gnu_result = convert (gnu_ada_base_type, gnu_result);
4750
4751 /* Finally, do the range check if requested. Note that if the
4752 result type is a modular type, the range check is actually
4753 an overflow check. */
4754
4755 if (range_p
4756 || (TREE_CODE (gnu_base_type) == INTEGER_TYPE
4757 && TYPE_MODULAR_P (gnu_base_type) && overflow_p))
4758 gnu_result = emit_range_check (gnu_result, gnat_type);
4759
4760 return convert (gnu_type, gnu_result);
4761 }
4762 \f
4763 /* Return 1 if GNU_EXPR can be directly addressed. This is the case
4764 unless it is an expression involving computation or if it involves
4765 a bitfield reference. This returns the same as
4766 gnat_mark_addressable in most cases. */
4767
4768 static int
4769 addressable_p (gnu_expr)
4770 tree gnu_expr;
4771 {
4772 switch (TREE_CODE (gnu_expr))
4773 {
4774 case UNCONSTRAINED_ARRAY_REF:
4775 case INDIRECT_REF:
4776 case VAR_DECL:
4777 case PARM_DECL:
4778 case FUNCTION_DECL:
4779 case RESULT_DECL:
4780 case CONSTRUCTOR:
4781 case NULL_EXPR:
4782 return 1;
4783
4784 case COMPONENT_REF:
4785 return (! DECL_BIT_FIELD (TREE_OPERAND (gnu_expr, 1))
4786 && addressable_p (TREE_OPERAND (gnu_expr, 0)));
4787
4788 case ARRAY_REF: case ARRAY_RANGE_REF:
4789 case REALPART_EXPR: case IMAGPART_EXPR:
4790 case NOP_EXPR:
4791 return addressable_p (TREE_OPERAND (gnu_expr, 0));
4792
4793 case CONVERT_EXPR:
4794 return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr))
4795 && addressable_p (TREE_OPERAND (gnu_expr, 0)));
4796
4797 case VIEW_CONVERT_EXPR:
4798 {
4799 /* This is addressable if we can avoid a copy. */
4800 tree type = TREE_TYPE (gnu_expr);
4801 tree inner_type = TREE_TYPE (TREE_OPERAND (gnu_expr, 0));
4802
4803 return (((TYPE_MODE (type) == TYPE_MODE (inner_type)
4804 && (TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
4805 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT))
4806 || ((TYPE_MODE (type) == BLKmode
4807 || TYPE_MODE (inner_type) == BLKmode)
4808 && (TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
4809 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT
4810 || TYPE_ALIGN_OK (type)
4811 || TYPE_ALIGN_OK (inner_type))))
4812 && addressable_p (TREE_OPERAND (gnu_expr, 0)));
4813 }
4814
4815 default:
4816 return 0;
4817 }
4818 }
4819 \f
4820 /* Do the processing for the declaration of a GNAT_ENTITY, a type. If
4821 a separate Freeze node exists, delay the bulk of the processing. Otherwise
4822 make a GCC type for GNAT_ENTITY and set up the correspondance. */
4823
4824 void
4825 process_type (gnat_entity)
4826 Entity_Id gnat_entity;
4827 {
4828 tree gnu_old
4829 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : 0;
4830 tree gnu_new;
4831
4832 /* If we are to delay elaboration of this type, just do any
4833 elaborations needed for expressions within the declaration and
4834 make a dummy type entry for this node and its Full_View (if
4835 any) in case something points to it. Don't do this if it
4836 has already been done (the only way that can happen is if
4837 the private completion is also delayed). */
4838 if (Present (Freeze_Node (gnat_entity))
4839 || (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
4840 && Present (Full_View (gnat_entity))
4841 && Freeze_Node (Full_View (gnat_entity))
4842 && ! present_gnu_tree (Full_View (gnat_entity))))
4843 {
4844 elaborate_entity (gnat_entity);
4845
4846 if (gnu_old == 0)
4847 {
4848 tree gnu_decl = create_type_decl (get_entity_name (gnat_entity),
4849 make_dummy_type (gnat_entity),
4850 0, 0, 0);
4851
4852 save_gnu_tree (gnat_entity, gnu_decl, 0);
4853 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
4854 && Present (Full_View (gnat_entity)))
4855 save_gnu_tree (Full_View (gnat_entity), gnu_decl, 0);
4856 }
4857
4858 return;
4859 }
4860
4861 /* If we saved away a dummy type for this node it means that this
4862 made the type that corresponds to the full type of an incomplete
4863 type. Clear that type for now and then update the type in the
4864 pointers. */
4865 if (gnu_old != 0)
4866 {
4867 if (TREE_CODE (gnu_old) != TYPE_DECL
4868 || ! TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old)))
4869 {
4870 /* If this was a withed access type, this is not an error
4871 and merely indicates we've already elaborated the type
4872 already. */
4873 if (Is_Type (gnat_entity) && From_With_Type (gnat_entity))
4874 return;
4875
4876 gigi_abort (323);
4877 }
4878
4879 save_gnu_tree (gnat_entity, NULL_TREE, 0);
4880 }
4881
4882 /* Now fully elaborate the type. */
4883 gnu_new = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 1);
4884 if (TREE_CODE (gnu_new) != TYPE_DECL)
4885 gigi_abort (324);
4886
4887 /* If we have an old type and we've made pointers to this type,
4888 update those pointers. */
4889 if (gnu_old != 0)
4890 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
4891 TREE_TYPE (gnu_new));
4892
4893 /* If this is a record type corresponding to a task or protected type
4894 that is a completion of an incomplete type, perform a similar update
4895 on the type. */
4896 /* ??? Including protected types here is a guess. */
4897
4898 if (IN (Ekind (gnat_entity), Record_Kind)
4899 && Is_Concurrent_Record_Type (gnat_entity)
4900 && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity)))
4901 {
4902 tree gnu_task_old
4903 = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity));
4904
4905 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
4906 NULL_TREE, 0);
4907 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
4908 gnu_new, 0);
4909
4910 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old)),
4911 TREE_TYPE (gnu_new));
4912 }
4913 }
4914 \f
4915 /* GNAT_ASSOC is the front of the Component_Associations of an N_Aggregate.
4916 GNU_TYPE is the GCC type of the corresponding record.
4917
4918 Return a CONSTRUCTOR to build the record. */
4919
4920 static tree
4921 assoc_to_constructor (gnat_assoc, gnu_type)
4922 Node_Id gnat_assoc;
4923 tree gnu_type;
4924 {
4925 tree gnu_field, gnu_list, gnu_result;
4926
4927 /* We test for GNU_FIELD being empty in the case where a variant
4928 was the last thing since we don't take things off GNAT_ASSOC in
4929 that case. We check GNAT_ASSOC in case we have a variant, but it
4930 has no fields. */
4931
4932 for (gnu_list = NULL_TREE; Present (gnat_assoc);
4933 gnat_assoc = Next (gnat_assoc))
4934 {
4935 Node_Id gnat_field = First (Choices (gnat_assoc));
4936 tree gnu_field = gnat_to_gnu_entity (Entity (gnat_field), NULL_TREE, 0);
4937 tree gnu_expr = gnat_to_gnu (Expression (gnat_assoc));
4938
4939 /* The expander is supposed to put a single component selector name
4940 in every record component association */
4941 if (Next (gnat_field))
4942 gigi_abort (328);
4943
4944 /* Before assigning a value in an aggregate make sure range checks
4945 are done if required. Then convert to the type of the field. */
4946 if (Do_Range_Check (Expression (gnat_assoc)))
4947 gnu_expr = emit_range_check (gnu_expr, Etype (gnat_field));
4948
4949 gnu_expr = convert (TREE_TYPE (gnu_field), gnu_expr);
4950
4951 /* Add the field and expression to the list. */
4952 gnu_list = tree_cons (gnu_field, gnu_expr, gnu_list);
4953 }
4954
4955 gnu_result = extract_values (gnu_list, gnu_type);
4956
4957 /* Verify every enty in GNU_LIST was used. */
4958 for (gnu_field = gnu_list; gnu_field; gnu_field = TREE_CHAIN (gnu_field))
4959 if (! TREE_ADDRESSABLE (gnu_field))
4960 gigi_abort (311);
4961
4962 return gnu_result;
4963 }
4964
4965 /* Builds a possibly nested constructor for array aggregates. GNAT_EXPR
4966 is the first element of an array aggregate. It may itself be an
4967 aggregate (an array or record aggregate). GNU_ARRAY_TYPE is the gnu type
4968 corresponding to the array aggregate. GNAT_COMPONENT_TYPE is the type
4969 of the array component. It is needed for range checking. */
4970
4971 static tree
4972 pos_to_constructor (gnat_expr, gnu_array_type, gnat_component_type)
4973 Node_Id gnat_expr;
4974 tree gnu_array_type;
4975 Entity_Id gnat_component_type;
4976 {
4977 tree gnu_expr;
4978 tree gnu_expr_list = NULL_TREE;
4979
4980 for ( ; Present (gnat_expr); gnat_expr = Next (gnat_expr))
4981 {
4982 /* If the expression is itself an array aggregate then first build the
4983 innermost constructor if it is part of our array (multi-dimensional
4984 case). */
4985
4986 if (Nkind (gnat_expr) == N_Aggregate
4987 && TREE_CODE (TREE_TYPE (gnu_array_type)) == ARRAY_TYPE
4988 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type)))
4989 gnu_expr = pos_to_constructor (First (Expressions (gnat_expr)),
4990 TREE_TYPE (gnu_array_type),
4991 gnat_component_type);
4992 else
4993 {
4994 gnu_expr = gnat_to_gnu (gnat_expr);
4995
4996 /* before assigning the element to the array make sure it is
4997 in range */
4998 if (Do_Range_Check (gnat_expr))
4999 gnu_expr = emit_range_check (gnu_expr, gnat_component_type);
5000 }
5001
5002 gnu_expr_list
5003 = tree_cons (NULL_TREE, convert (TREE_TYPE (gnu_array_type), gnu_expr),
5004 gnu_expr_list);
5005 }
5006
5007 return gnat_build_constructor (gnu_array_type, nreverse (gnu_expr_list));
5008 }
5009 \f
5010 /* Subroutine of assoc_to_constructor: VALUES is a list of field associations,
5011 some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting
5012 of the associations that are from RECORD_TYPE. If we see an internal
5013 record, make a recursive call to fill it in as well. */
5014
5015 static tree
5016 extract_values (values, record_type)
5017 tree values;
5018 tree record_type;
5019 {
5020 tree result = NULL_TREE;
5021 tree field, tem;
5022
5023 for (field = TYPE_FIELDS (record_type); field; field = TREE_CHAIN (field))
5024 {
5025 tree value = 0;
5026
5027 /* _Parent is an internal field, but may have values in the aggregate,
5028 so check for values first. */
5029 if ((tem = purpose_member (field, values)) != 0)
5030 {
5031 value = TREE_VALUE (tem);
5032 TREE_ADDRESSABLE (tem) = 1;
5033 }
5034
5035 else if (DECL_INTERNAL_P (field))
5036 {
5037 value = extract_values (values, TREE_TYPE (field));
5038 if (TREE_CODE (value) == CONSTRUCTOR
5039 && CONSTRUCTOR_ELTS (value) == 0)
5040 value = 0;
5041 }
5042 else
5043 /* If we have a record subtype, the names will match, but not the
5044 actual FIELD_DECLs. */
5045 for (tem = values; tem; tem = TREE_CHAIN (tem))
5046 if (DECL_NAME (TREE_PURPOSE (tem)) == DECL_NAME (field))
5047 {
5048 value = convert (TREE_TYPE (field), TREE_VALUE (tem));
5049 TREE_ADDRESSABLE (tem) = 1;
5050 }
5051
5052 if (value == 0)
5053 continue;
5054
5055 result = tree_cons (field, value, result);
5056 }
5057
5058 return gnat_build_constructor (record_type, nreverse (result));
5059 }
5060 \f
5061 /* EXP is to be treated as an array or record. Handle the cases when it is
5062 an access object and perform the required dereferences. */
5063
5064 static tree
5065 maybe_implicit_deref (exp)
5066 tree exp;
5067 {
5068 /* If the type is a pointer, dereference it. */
5069
5070 if (POINTER_TYPE_P (TREE_TYPE (exp)) || TYPE_FAT_POINTER_P (TREE_TYPE (exp)))
5071 exp = build_unary_op (INDIRECT_REF, NULL_TREE, exp);
5072
5073 /* If we got a padded type, remove it too. */
5074 if (TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
5075 && TYPE_IS_PADDING_P (TREE_TYPE (exp)))
5076 exp = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp))), exp);
5077
5078 return exp;
5079 }
5080 \f
5081 /* Protect EXP from multiple evaluation. This may make a SAVE_EXPR. */
5082
5083 tree
5084 protect_multiple_eval (exp)
5085 tree exp;
5086 {
5087 tree type = TREE_TYPE (exp);
5088
5089 /* If this has no side effects, we don't need to do anything. */
5090 if (! TREE_SIDE_EFFECTS (exp))
5091 return exp;
5092
5093 /* If it is a conversion, protect what's inside the conversion.
5094 Similarly, if we're indirectly referencing something, we only
5095 actually need to protect the address since the data itself can't
5096 change in these situations. */
5097 else if (TREE_CODE (exp) == NON_LVALUE_EXPR
5098 || TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
5099 || TREE_CODE (exp) == VIEW_CONVERT_EXPR
5100 || TREE_CODE (exp) == INDIRECT_REF
5101 || TREE_CODE (exp) == UNCONSTRAINED_ARRAY_REF)
5102 return build1 (TREE_CODE (exp), type,
5103 protect_multiple_eval (TREE_OPERAND (exp, 0)));
5104
5105 /* If EXP is a fat pointer or something that can be placed into a register,
5106 just make a SAVE_EXPR. */
5107 if (TYPE_FAT_POINTER_P (type) || TYPE_MODE (type) != BLKmode)
5108 return save_expr (exp);
5109
5110 /* Otherwise, dereference, protect the address, and re-reference. */
5111 else
5112 return
5113 build_unary_op (INDIRECT_REF, type,
5114 save_expr (build_unary_op (ADDR_EXPR,
5115 build_reference_type (type),
5116 exp)));
5117 }
5118 \f
5119 /* This is equivalent to stabilize_reference in GCC's tree.c, but we know
5120 how to handle our new nodes and we take an extra argument that says
5121 whether to force evaluation of everything. */
5122
5123 tree
5124 gnat_stabilize_reference (ref, force)
5125 tree ref;
5126 int force;
5127 {
5128 register tree type = TREE_TYPE (ref);
5129 register enum tree_code code = TREE_CODE (ref);
5130 register tree result;
5131
5132 switch (code)
5133 {
5134 case VAR_DECL:
5135 case PARM_DECL:
5136 case RESULT_DECL:
5137 /* No action is needed in this case. */
5138 return ref;
5139
5140 case NOP_EXPR:
5141 case CONVERT_EXPR:
5142 case FLOAT_EXPR:
5143 case FIX_TRUNC_EXPR:
5144 case FIX_FLOOR_EXPR:
5145 case FIX_ROUND_EXPR:
5146 case FIX_CEIL_EXPR:
5147 case VIEW_CONVERT_EXPR:
5148 case ADDR_EXPR:
5149 result
5150 = build1 (code, type,
5151 gnat_stabilize_reference (TREE_OPERAND (ref, 0), force));
5152 break;
5153
5154 case INDIRECT_REF:
5155 case UNCONSTRAINED_ARRAY_REF:
5156 result = build1 (code, type,
5157 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 0),
5158 force));
5159 break;
5160
5161 case COMPONENT_REF:
5162 result = build (COMPONENT_REF, type,
5163 gnat_stabilize_reference (TREE_OPERAND (ref, 0),
5164 force),
5165 TREE_OPERAND (ref, 1));
5166 break;
5167
5168 case BIT_FIELD_REF:
5169 result = build (BIT_FIELD_REF, type,
5170 gnat_stabilize_reference (TREE_OPERAND (ref, 0), force),
5171 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 1),
5172 force),
5173 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 2),
5174 force));
5175 break;
5176
5177 case ARRAY_REF:
5178 result = build (ARRAY_REF, type,
5179 gnat_stabilize_reference (TREE_OPERAND (ref, 0), force),
5180 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 1),
5181 force));
5182 break;
5183
5184 case ARRAY_RANGE_REF:
5185 result = build (ARRAY_RANGE_REF, type,
5186 gnat_stabilize_reference (TREE_OPERAND (ref, 0), force),
5187 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 1),
5188 force));
5189 break;
5190
5191 case COMPOUND_EXPR:
5192 result = build (COMPOUND_EXPR, type,
5193 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 0),
5194 force),
5195 gnat_stabilize_reference (TREE_OPERAND (ref, 1),
5196 force));
5197 break;
5198
5199 case RTL_EXPR:
5200 result = build1 (INDIRECT_REF, type,
5201 save_expr (build1 (ADDR_EXPR,
5202 build_reference_type (type), ref)));
5203 break;
5204
5205 /* If arg isn't a kind of lvalue we recognize, make no change.
5206 Caller should recognize the error for an invalid lvalue. */
5207 default:
5208 return ref;
5209
5210 case ERROR_MARK:
5211 return error_mark_node;
5212 }
5213
5214 TREE_READONLY (result) = TREE_READONLY (ref);
5215 return result;
5216 }
5217
5218 /* Similar to stabilize_reference_1 in tree.c, but supports an extra
5219 arg to force a SAVE_EXPR for everything. */
5220
5221 static tree
5222 gnat_stabilize_reference_1 (e, force)
5223 tree e;
5224 int force;
5225 {
5226 register enum tree_code code = TREE_CODE (e);
5227 register tree type = TREE_TYPE (e);
5228 register tree result;
5229
5230 /* We cannot ignore const expressions because it might be a reference
5231 to a const array but whose index contains side-effects. But we can
5232 ignore things that are actual constant or that already have been
5233 handled by this function. */
5234
5235 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
5236 return e;
5237
5238 switch (TREE_CODE_CLASS (code))
5239 {
5240 case 'x':
5241 case 't':
5242 case 'd':
5243 case 'b':
5244 case '<':
5245 case 's':
5246 case 'e':
5247 case 'r':
5248 if (TREE_SIDE_EFFECTS (e) || force)
5249 return save_expr (e);
5250 return e;
5251
5252 case 'c':
5253 /* Constants need no processing. In fact, we should never reach
5254 here. */
5255 return e;
5256
5257 case '2':
5258 /* Recursively stabilize each operand. */
5259 result = build (code, type,
5260 gnat_stabilize_reference_1 (TREE_OPERAND (e, 0), force),
5261 gnat_stabilize_reference_1 (TREE_OPERAND (e, 1), force));
5262 break;
5263
5264 case '1':
5265 /* Recursively stabilize each operand. */
5266 result = build1 (code, type,
5267 gnat_stabilize_reference_1 (TREE_OPERAND (e, 0),
5268 force));
5269 break;
5270
5271 default:
5272 abort ();
5273 }
5274
5275 TREE_READONLY (result) = TREE_READONLY (e);
5276 return result;
5277 }
5278 \f
5279 /* GNAT_UNIT is the Defining_Identifier for some package or subprogram,
5280 either a spec or a body, BODY_P says which. If needed, make a function
5281 to be the elaboration routine for that object and perform the elaborations
5282 in GNU_ELAB_LIST.
5283
5284 Return 1 if we didn't need an elaboration function, zero otherwise. */
5285
5286 static int
5287 build_unit_elab (gnat_unit, body_p, gnu_elab_list)
5288 Entity_Id gnat_unit;
5289 int body_p;
5290 tree gnu_elab_list;
5291 {
5292 tree gnu_decl;
5293 rtx insn;
5294 int result = 1;
5295
5296 /* If we have nothing to do, return. */
5297 if (gnu_elab_list == 0)
5298 return 1;
5299
5300 /* Prevent the elaboration list from being reclaimed by the GC. */
5301 gnu_pending_elaboration_lists = chainon (gnu_pending_elaboration_lists,
5302 gnu_elab_list);
5303
5304 /* Set our file and line number to that of the object and set up the
5305 elaboration routine. */
5306 gnu_decl = create_subprog_decl (create_concat_name (gnat_unit,
5307 body_p ?
5308 "elabb" : "elabs"),
5309 NULL_TREE, void_ftype, NULL_TREE, 0, 1, 0,
5310 0);
5311 DECL_ELABORATION_PROC_P (gnu_decl) = 1;
5312
5313 begin_subprog_body (gnu_decl);
5314 set_lineno (gnat_unit, 1);
5315 pushlevel (0);
5316 gnu_block_stack = tree_cons (NULL_TREE, NULL_TREE, gnu_block_stack);
5317 expand_start_bindings (0);
5318
5319 /* Emit the assignments for the elaborations we have to do. If there
5320 is no destination, this is just a call to execute some statement
5321 that was placed within the declarative region. But first save a
5322 pointer so we can see if any insns were generated. */
5323
5324 insn = get_last_insn ();
5325
5326 for (; gnu_elab_list; gnu_elab_list = TREE_CHAIN (gnu_elab_list))
5327 if (TREE_PURPOSE (gnu_elab_list) == NULL_TREE)
5328 {
5329 if (TREE_VALUE (gnu_elab_list) != 0)
5330 expand_expr_stmt (TREE_VALUE (gnu_elab_list));
5331 }
5332 else
5333 {
5334 tree lhs = TREE_PURPOSE (gnu_elab_list);
5335
5336 input_location = DECL_SOURCE_LOCATION (lhs);
5337
5338 /* If LHS has a padded type, convert it to the unpadded type
5339 so the assignment is done properly. */
5340 if (TREE_CODE (TREE_TYPE (lhs)) == RECORD_TYPE
5341 && TYPE_IS_PADDING_P (TREE_TYPE (lhs)))
5342 lhs = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (lhs))), lhs);
5343
5344 emit_line_note (input_location);
5345 expand_expr_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE,
5346 TREE_PURPOSE (gnu_elab_list),
5347 TREE_VALUE (gnu_elab_list)));
5348 }
5349
5350 /* See if any non-NOTE insns were generated. */
5351 for (insn = NEXT_INSN (insn); insn; insn = NEXT_INSN (insn))
5352 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
5353 {
5354 result = 0;
5355 break;
5356 }
5357
5358 expand_end_bindings (getdecls (), kept_level_p (), 0);
5359 poplevel (kept_level_p (), 1, 0);
5360 gnu_block_stack = TREE_CHAIN (gnu_block_stack);
5361 end_subprog_body ();
5362
5363 /* We are finished with the elaboration list it can now be discarded. */
5364 gnu_pending_elaboration_lists = TREE_CHAIN (gnu_pending_elaboration_lists);
5365
5366 /* If there were no insns, we don't need an elab routine. It would
5367 be nice to not output this one, but there's no good way to do that. */
5368 return result;
5369 }
5370 \f
5371 extern char *__gnat_to_canonical_file_spec PARAMS ((char *));
5372
5373 /* Determine the input_filename and the input_line from the source location
5374 (Sloc) of GNAT_NODE node. Set the global variable input_filename and
5375 input_line. If WRITE_NOTE_P is true, emit a line number note. */
5376
5377 void
5378 set_lineno (gnat_node, write_note_p)
5379 Node_Id gnat_node;
5380 int write_note_p;
5381 {
5382 Source_Ptr source_location = Sloc (gnat_node);
5383
5384 /* If node not from source code, ignore. */
5385 if (source_location < 0)
5386 return;
5387
5388 /* Use the identifier table to make a hashed, permanent copy of the filename,
5389 since the name table gets reallocated after Gigi returns but before all
5390 the debugging information is output. The call to
5391 __gnat_to_canonical_file_spec translates filenames from pragmas
5392 Source_Reference that contain host style syntax not understood by gdb. */
5393 input_filename
5394 = IDENTIFIER_POINTER
5395 (get_identifier
5396 (__gnat_to_canonical_file_spec
5397 (Get_Name_String
5398 (Debug_Source_Name (Get_Source_File_Index (source_location))))));
5399
5400 /* ref_filename is the reference file name as given by sinput (i.e no
5401 directory) */
5402 ref_filename
5403 = IDENTIFIER_POINTER
5404 (get_identifier
5405 (Get_Name_String
5406 (Reference_Name (Get_Source_File_Index (source_location)))));;
5407 input_line = Get_Logical_Line_Number (source_location);
5408
5409 if (write_note_p)
5410 emit_line_note (input_location);
5411 }
5412 \f
5413 /* Post an error message. MSG is the error message, properly annotated.
5414 NODE is the node at which to post the error and the node to use for the
5415 "&" substitution. */
5416
5417 void
5418 post_error (msg, node)
5419 const char *msg;
5420 Node_Id node;
5421 {
5422 String_Template temp;
5423 Fat_Pointer fp;
5424
5425 temp.Low_Bound = 1, temp.High_Bound = strlen (msg);
5426 fp.Array = msg, fp.Bounds = &temp;
5427 if (Present (node))
5428 Error_Msg_N (fp, node);
5429 }
5430
5431 /* Similar, but NODE is the node at which to post the error and ENT
5432 is the node to use for the "&" substitution. */
5433
5434 void
5435 post_error_ne (msg, node, ent)
5436 const char *msg;
5437 Node_Id node;
5438 Entity_Id ent;
5439 {
5440 String_Template temp;
5441 Fat_Pointer fp;
5442
5443 temp.Low_Bound = 1, temp.High_Bound = strlen (msg);
5444 fp.Array = msg, fp.Bounds = &temp;
5445 if (Present (node))
5446 Error_Msg_NE (fp, node, ent);
5447 }
5448
5449 /* Similar, but NODE is the node at which to post the error, ENT is the node
5450 to use for the "&" substitution, and N is the number to use for the ^. */
5451
5452 void
5453 post_error_ne_num (msg, node, ent, n)
5454 const char *msg;
5455 Node_Id node;
5456 Entity_Id ent;
5457 int n;
5458 {
5459 String_Template temp;
5460 Fat_Pointer fp;
5461
5462 temp.Low_Bound = 1, temp.High_Bound = strlen (msg);
5463 fp.Array = msg, fp.Bounds = &temp;
5464 Error_Msg_Uint_1 = UI_From_Int (n);
5465
5466 if (Present (node))
5467 Error_Msg_NE (fp, node, ent);
5468 }
5469 \f
5470 /* Similar to post_error_ne_num, but T is a GCC tree representing the
5471 number to write. If the tree represents a constant that fits within
5472 a host integer, the text inside curly brackets in MSG will be output
5473 (presumably including a '^'). Otherwise that text will not be output
5474 and the text inside square brackets will be output instead. */
5475
5476 void
5477 post_error_ne_tree (msg, node, ent, t)
5478 const char *msg;
5479 Node_Id node;
5480 Entity_Id ent;
5481 tree t;
5482 {
5483 char *newmsg = alloca (strlen (msg) + 1);
5484 String_Template temp = {1, 0};
5485 Fat_Pointer fp;
5486 char start_yes, end_yes, start_no, end_no;
5487 const char *p;
5488 char *q;
5489
5490 fp.Array = newmsg, fp.Bounds = &temp;
5491
5492 if (host_integerp (t, 1)
5493 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_INT
5494 && compare_tree_int (t, 1 << (HOST_BITS_PER_INT - 2)) < 0
5495 #endif
5496 )
5497 {
5498 Error_Msg_Uint_1 = UI_From_Int (tree_low_cst (t, 1));
5499 start_yes = '{', end_yes = '}', start_no = '[', end_no = ']';
5500 }
5501 else
5502 start_yes = '[', end_yes = ']', start_no = '{', end_no = '}';
5503
5504 for (p = msg, q = newmsg; *p != 0; p++)
5505 {
5506 if (*p == start_yes)
5507 for (p++; *p != end_yes; p++)
5508 *q++ = *p;
5509 else if (*p == start_no)
5510 for (p++; *p != end_no; p++)
5511 ;
5512 else
5513 *q++ = *p;
5514 }
5515
5516 *q = 0;
5517
5518 temp.High_Bound = strlen (newmsg);
5519 if (Present (node))
5520 Error_Msg_NE (fp, node, ent);
5521 }
5522
5523 /* Similar to post_error_ne_tree, except that NUM is a second
5524 integer to write in the message. */
5525
5526 void
5527 post_error_ne_tree_2 (msg, node, ent, t, num)
5528 const char *msg;
5529 Node_Id node;
5530 Entity_Id ent;
5531 tree t;
5532 int num;
5533 {
5534 Error_Msg_Uint_2 = UI_From_Int (num);
5535 post_error_ne_tree (msg, node, ent, t);
5536 }
5537
5538 /* Set the node for a second '&' in the error message. */
5539
5540 void
5541 set_second_error_entity (e)
5542 Entity_Id e;
5543 {
5544 Error_Msg_Node_2 = e;
5545 }
5546 \f
5547 /* Signal abort, with "Gigi abort" as the error label, and error_gnat_node
5548 as the relevant node that provides the location info for the error */
5549
5550 void
5551 gigi_abort (code)
5552 int code;
5553 {
5554 String_Template temp = {1, 10};
5555 Fat_Pointer fp;
5556
5557 fp.Array = "Gigi abort", fp.Bounds = &temp;
5558
5559 Current_Error_Node = error_gnat_node;
5560 Compiler_Abort (fp, code);
5561 }
5562 \f
5563 /* Initialize the table that maps GNAT codes to GCC codes for simple
5564 binary and unary operations. */
5565
5566 void
5567 init_code_table ()
5568 {
5569 gnu_codes[N_And_Then] = TRUTH_ANDIF_EXPR;
5570 gnu_codes[N_Or_Else] = TRUTH_ORIF_EXPR;
5571
5572 gnu_codes[N_Op_And] = TRUTH_AND_EXPR;
5573 gnu_codes[N_Op_Or] = TRUTH_OR_EXPR;
5574 gnu_codes[N_Op_Xor] = TRUTH_XOR_EXPR;
5575 gnu_codes[N_Op_Eq] = EQ_EXPR;
5576 gnu_codes[N_Op_Ne] = NE_EXPR;
5577 gnu_codes[N_Op_Lt] = LT_EXPR;
5578 gnu_codes[N_Op_Le] = LE_EXPR;
5579 gnu_codes[N_Op_Gt] = GT_EXPR;
5580 gnu_codes[N_Op_Ge] = GE_EXPR;
5581 gnu_codes[N_Op_Add] = PLUS_EXPR;
5582 gnu_codes[N_Op_Subtract] = MINUS_EXPR;
5583 gnu_codes[N_Op_Multiply] = MULT_EXPR;
5584 gnu_codes[N_Op_Mod] = FLOOR_MOD_EXPR;
5585 gnu_codes[N_Op_Rem] = TRUNC_MOD_EXPR;
5586 gnu_codes[N_Op_Minus] = NEGATE_EXPR;
5587 gnu_codes[N_Op_Abs] = ABS_EXPR;
5588 gnu_codes[N_Op_Not] = TRUTH_NOT_EXPR;
5589 gnu_codes[N_Op_Rotate_Left] = LROTATE_EXPR;
5590 gnu_codes[N_Op_Rotate_Right] = RROTATE_EXPR;
5591 gnu_codes[N_Op_Shift_Left] = LSHIFT_EXPR;
5592 gnu_codes[N_Op_Shift_Right] = RSHIFT_EXPR;
5593 gnu_codes[N_Op_Shift_Right_Arithmetic] = RSHIFT_EXPR;
5594 }
5595
5596 #include "gt-ada-trans.h"