]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/gcc-interface/trans.c
trans.c (call_to_gnu): When creating the copy for a non-addressable parameter passed...
[thirdparty/gcc.git] / gcc / ada / gcc-interface / trans.c
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * T R A N S *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2009, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License distributed with GNAT; see file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
20 * *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
23 * *
24 ****************************************************************************/
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "real.h"
32 #include "flags.h"
33 #include "toplev.h"
34 #include "rtl.h"
35 #include "expr.h"
36 #include "ggc.h"
37 #include "cgraph.h"
38 #include "function.h"
39 #include "except.h"
40 #include "debug.h"
41 #include "output.h"
42 #include "tree-iterator.h"
43 #include "gimple.h"
44 #include "ada.h"
45 #include "types.h"
46 #include "atree.h"
47 #include "elists.h"
48 #include "namet.h"
49 #include "nlists.h"
50 #include "snames.h"
51 #include "stringt.h"
52 #include "uintp.h"
53 #include "urealp.h"
54 #include "fe.h"
55 #include "sinfo.h"
56 #include "einfo.h"
57 #include "ada-tree.h"
58 #include "gigi.h"
59 #include "adadecode.h"
60 #include "dwarf2.h"
61 #include "dwarf2out.h"
62
63 /* We should avoid allocating more than ALLOCA_THRESHOLD bytes via alloca,
64 for fear of running out of stack space. If we need more, we use xmalloc
65 instead. */
66 #define ALLOCA_THRESHOLD 1000
67
68 /* Let code below know whether we are targetting VMS without need of
69 intrusive preprocessor directives. */
70 #ifndef TARGET_ABI_OPEN_VMS
71 #define TARGET_ABI_OPEN_VMS 0
72 #endif
73
74 /* For efficient float-to-int rounding, it is necessary to know whether
75 floating-point arithmetic may use wider intermediate results. When
76 FP_ARITH_MAY_WIDEN is not defined, be conservative and only assume
77 that arithmetic does not widen if double precision is emulated. */
78 #ifndef FP_ARITH_MAY_WIDEN
79 #if defined(HAVE_extendsfdf2)
80 #define FP_ARITH_MAY_WIDEN HAVE_extendsfdf2
81 #else
82 #define FP_ARITH_MAY_WIDEN 0
83 #endif
84 #endif
85
86 extern char *__gnat_to_canonical_file_spec (char *);
87
88 int max_gnat_nodes;
89 int number_names;
90 int number_files;
91 struct Node *Nodes_Ptr;
92 Node_Id *Next_Node_Ptr;
93 Node_Id *Prev_Node_Ptr;
94 struct Elist_Header *Elists_Ptr;
95 struct Elmt_Item *Elmts_Ptr;
96 struct String_Entry *Strings_Ptr;
97 Char_Code *String_Chars_Ptr;
98 struct List_Header *List_Headers_Ptr;
99
100 /* Current filename without path. */
101 const char *ref_filename;
102
103 /* True when gigi is being called on an analyzed but unexpanded
104 tree, and the only purpose of the call is to properly annotate
105 types with representation information. */
106 bool type_annotate_only;
107
108 /* When not optimizing, we cache the 'First, 'Last and 'Length attributes
109 of unconstrained array IN parameters to avoid emitting a great deal of
110 redundant instructions to recompute them each time. */
111 struct parm_attr GTY (())
112 {
113 int id; /* GTY doesn't like Entity_Id. */
114 int dim;
115 tree first;
116 tree last;
117 tree length;
118 };
119
120 typedef struct parm_attr *parm_attr;
121
122 DEF_VEC_P(parm_attr);
123 DEF_VEC_ALLOC_P(parm_attr,gc);
124
125 struct language_function GTY(())
126 {
127 VEC(parm_attr,gc) *parm_attr_cache;
128 };
129
130 #define f_parm_attr_cache \
131 DECL_STRUCT_FUNCTION (current_function_decl)->language->parm_attr_cache
132
133 /* A structure used to gather together information about a statement group.
134 We use this to gather related statements, for example the "then" part
135 of a IF. In the case where it represents a lexical scope, we may also
136 have a BLOCK node corresponding to it and/or cleanups. */
137
138 struct stmt_group GTY((chain_next ("%h.previous"))) {
139 struct stmt_group *previous; /* Previous code group. */
140 tree stmt_list; /* List of statements for this code group. */
141 tree block; /* BLOCK for this code group, if any. */
142 tree cleanups; /* Cleanups for this code group, if any. */
143 };
144
145 static GTY(()) struct stmt_group *current_stmt_group;
146
147 /* List of unused struct stmt_group nodes. */
148 static GTY((deletable)) struct stmt_group *stmt_group_free_list;
149
150 /* A structure used to record information on elaboration procedures
151 we've made and need to process.
152
153 ??? gnat_node should be Node_Id, but gengtype gets confused. */
154
155 struct elab_info GTY((chain_next ("%h.next"))) {
156 struct elab_info *next; /* Pointer to next in chain. */
157 tree elab_proc; /* Elaboration procedure. */
158 int gnat_node; /* The N_Compilation_Unit. */
159 };
160
161 static GTY(()) struct elab_info *elab_info_list;
162
163 /* Free list of TREE_LIST nodes used for stacks. */
164 static GTY((deletable)) tree gnu_stack_free_list;
165
166 /* List of TREE_LIST nodes representing a stack of exception pointer
167 variables. TREE_VALUE is the VAR_DECL that stores the address of
168 the raised exception. Nonzero means we are in an exception
169 handler. Not used in the zero-cost case. */
170 static GTY(()) tree gnu_except_ptr_stack;
171
172 /* List of TREE_LIST nodes used to store the current elaboration procedure
173 decl. TREE_VALUE is the decl. */
174 static GTY(()) tree gnu_elab_proc_stack;
175
176 /* Variable that stores a list of labels to be used as a goto target instead of
177 a return in some functions. See processing for N_Subprogram_Body. */
178 static GTY(()) tree gnu_return_label_stack;
179
180 /* List of TREE_LIST nodes representing a stack of LOOP_STMT nodes.
181 TREE_VALUE of each entry is the label of the corresponding LOOP_STMT. */
182 static GTY(()) tree gnu_loop_label_stack;
183
184 /* List of TREE_LIST nodes representing labels for switch statements.
185 TREE_VALUE of each entry is the label at the end of the switch. */
186 static GTY(()) tree gnu_switch_label_stack;
187
188 /* List of TREE_LIST nodes containing the stacks for N_{Push,Pop}_*_Label. */
189 static GTY(()) tree gnu_constraint_error_label_stack;
190 static GTY(()) tree gnu_storage_error_label_stack;
191 static GTY(()) tree gnu_program_error_label_stack;
192
193 /* Map GNAT tree codes to GCC tree codes for simple expressions. */
194 static enum tree_code gnu_codes[Number_Node_Kinds];
195
196 /* Current node being treated, in case abort called. */
197 Node_Id error_gnat_node;
198
199 static void init_code_table (void);
200 static void Compilation_Unit_to_gnu (Node_Id);
201 static void record_code_position (Node_Id);
202 static void insert_code_for (Node_Id);
203 static void add_cleanup (tree, Node_Id);
204 static tree unshare_save_expr (tree *, int *, void *);
205 static void add_stmt_list (List_Id);
206 static void push_exception_label_stack (tree *, Entity_Id);
207 static tree build_stmt_group (List_Id, bool);
208 static void push_stack (tree *, tree, tree);
209 static void pop_stack (tree *);
210 static enum gimplify_status gnat_gimplify_stmt (tree *);
211 static void elaborate_all_entities (Node_Id);
212 static void process_freeze_entity (Node_Id);
213 static void process_inlined_subprograms (Node_Id);
214 static void process_decls (List_Id, List_Id, Node_Id, bool, bool);
215 static tree emit_range_check (tree, Node_Id, Node_Id);
216 static tree emit_index_check (tree, tree, tree, tree, Node_Id);
217 static tree emit_check (tree, tree, int, Node_Id);
218 static tree build_unary_op_trapv (enum tree_code, tree, tree, Node_Id);
219 static tree build_binary_op_trapv (enum tree_code, tree, tree, tree, Node_Id);
220 static tree convert_with_check (Entity_Id, tree, bool, bool, bool, Node_Id);
221 static bool smaller_packable_type_p (tree, tree);
222 static bool addressable_p (tree, tree);
223 static tree assoc_to_constructor (Entity_Id, Node_Id, tree);
224 static tree extract_values (tree, tree);
225 static tree pos_to_constructor (Node_Id, tree, Entity_Id);
226 static tree maybe_implicit_deref (tree);
227 static tree gnat_stabilize_reference (tree, bool);
228 static tree gnat_stabilize_reference_1 (tree, bool);
229 static void set_expr_location_from_node (tree, Node_Id);
230 static int lvalue_required_p (Node_Id, tree, int);
231
232 /* Hooks for debug info back-ends, only supported and used in a restricted set
233 of configurations. */
234 static const char *extract_encoding (const char *) ATTRIBUTE_UNUSED;
235 static const char *decode_name (const char *) ATTRIBUTE_UNUSED;
236 \f
237 /* This is the main program of the back-end. It sets up all the table
238 structures and then generates code. */
239
240 void
241 gigi (Node_Id gnat_root, int max_gnat_node, int number_name,
242 struct Node *nodes_ptr, Node_Id *next_node_ptr, Node_Id *prev_node_ptr,
243 struct Elist_Header *elists_ptr, struct Elmt_Item *elmts_ptr,
244 struct String_Entry *strings_ptr, Char_Code *string_chars_ptr,
245 struct List_Header *list_headers_ptr, Nat number_file,
246 struct File_Info_Type *file_info_ptr, Entity_Id standard_boolean,
247 Entity_Id standard_integer, Entity_Id standard_long_long_float,
248 Entity_Id standard_exception_type, Int gigi_operating_mode)
249 {
250 Entity_Id gnat_literal;
251 tree long_long_float_type, exception_type, t;
252 tree int64_type = gnat_type_for_size (64, 0);
253 struct elab_info *info;
254 int i;
255
256 max_gnat_nodes = max_gnat_node;
257 number_names = number_name;
258 number_files = number_file;
259 Nodes_Ptr = nodes_ptr;
260 Next_Node_Ptr = next_node_ptr;
261 Prev_Node_Ptr = prev_node_ptr;
262 Elists_Ptr = elists_ptr;
263 Elmts_Ptr = elmts_ptr;
264 Strings_Ptr = strings_ptr;
265 String_Chars_Ptr = string_chars_ptr;
266 List_Headers_Ptr = list_headers_ptr;
267
268 type_annotate_only = (gigi_operating_mode == 1);
269
270 for (i = 0; i < number_files; i++)
271 {
272 /* Use the identifier table to make a permanent copy of the filename as
273 the name table gets reallocated after Gigi returns but before all the
274 debugging information is output. The __gnat_to_canonical_file_spec
275 call translates filenames from pragmas Source_Reference that contain
276 host style syntax not understood by gdb. */
277 const char *filename
278 = IDENTIFIER_POINTER
279 (get_identifier
280 (__gnat_to_canonical_file_spec
281 (Get_Name_String (file_info_ptr[i].File_Name))));
282
283 /* We rely on the order isomorphism between files and line maps. */
284 gcc_assert ((int) line_table->used == i);
285
286 /* We create the line map for a source file at once, with a fixed number
287 of columns chosen to avoid jumping over the next power of 2. */
288 linemap_add (line_table, LC_ENTER, 0, filename, 1);
289 linemap_line_start (line_table, file_info_ptr[i].Num_Source_Lines, 252);
290 linemap_position_for_column (line_table, 252 - 1);
291 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
292 }
293
294 /* Initialize ourselves. */
295 init_code_table ();
296 init_gnat_to_gnu ();
297 gnat_compute_largest_alignment ();
298 init_dummy_type ();
299
300 /* If we are just annotating types, give VOID_TYPE zero sizes to avoid
301 errors. */
302 if (type_annotate_only)
303 {
304 TYPE_SIZE (void_type_node) = bitsize_zero_node;
305 TYPE_SIZE_UNIT (void_type_node) = size_zero_node;
306 }
307
308 /* If the GNU type extensions to DWARF are available, setup the hooks. */
309 #if defined (DWARF2_DEBUGGING_INFO) && defined (DWARF2_GNU_TYPE_EXTENSIONS)
310 /* We condition the name demangling and the generation of type encoding
311 strings on -gdwarf+ and always set descriptive types on. */
312 if (use_gnu_debug_info_extensions)
313 {
314 dwarf2out_set_type_encoding_func (extract_encoding);
315 dwarf2out_set_demangle_name_func (decode_name);
316 }
317 dwarf2out_set_descriptive_type_func (get_parallel_type);
318 #endif
319
320 /* Enable GNAT stack checking method if needed */
321 if (!Stack_Check_Probes_On_Target)
322 set_stack_check_libfunc (gen_rtx_SYMBOL_REF (Pmode, "_gnat_stack_check"));
323
324 /* Record the builtin types. Define `integer' and `unsigned char' first so
325 that dbx will output them first. */
326 record_builtin_type ("integer", integer_type_node);
327 record_builtin_type ("unsigned char", char_type_node);
328 record_builtin_type ("long integer", long_integer_type_node);
329 unsigned_type_node = gnat_type_for_size (INT_TYPE_SIZE, 1);
330 record_builtin_type ("unsigned int", unsigned_type_node);
331 record_builtin_type (SIZE_TYPE, sizetype);
332 record_builtin_type ("boolean", boolean_type_node);
333 record_builtin_type ("void", void_type_node);
334
335 /* Save the type we made for integer as the type for Standard.Integer. */
336 save_gnu_tree (Base_Type (standard_integer), TYPE_NAME (integer_type_node),
337 false);
338
339 /* Save the type we made for boolean as the type for Standard.Boolean. */
340 save_gnu_tree (Base_Type (standard_boolean), TYPE_NAME (boolean_type_node),
341 false);
342 gnat_literal = First_Literal (Base_Type (standard_boolean));
343 t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node);
344 gcc_assert (t == boolean_false_node);
345 t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
346 boolean_type_node, t, true, false, false, false,
347 NULL, gnat_literal);
348 DECL_IGNORED_P (t) = 1;
349 save_gnu_tree (gnat_literal, t, false);
350 gnat_literal = Next_Literal (gnat_literal);
351 t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node);
352 gcc_assert (t == boolean_true_node);
353 t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
354 boolean_type_node, t, true, false, false, false,
355 NULL, gnat_literal);
356 DECL_IGNORED_P (t) = 1;
357 save_gnu_tree (gnat_literal, t, false);
358
359 void_ftype = build_function_type (void_type_node, NULL_TREE);
360 ptr_void_ftype = build_pointer_type (void_ftype);
361
362 /* Now declare runtime functions. */
363 t = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
364
365 /* malloc is a function declaration tree for a function to allocate
366 memory. */
367 malloc_decl
368 = create_subprog_decl (get_identifier ("__gnat_malloc"), NULL_TREE,
369 build_function_type (ptr_void_type_node,
370 tree_cons (NULL_TREE,
371 sizetype, t)),
372 NULL_TREE, false, true, true, NULL, Empty);
373 DECL_IS_MALLOC (malloc_decl) = 1;
374
375 /* malloc32 is a function declaration tree for a function to allocate
376 32-bit memory on a 64-bit system. Needed only on 64-bit VMS. */
377 malloc32_decl
378 = create_subprog_decl (get_identifier ("__gnat_malloc32"), NULL_TREE,
379 build_function_type (ptr_void_type_node,
380 tree_cons (NULL_TREE,
381 sizetype, t)),
382 NULL_TREE, false, true, true, NULL, Empty);
383 DECL_IS_MALLOC (malloc32_decl) = 1;
384
385 /* free is a function declaration tree for a function to free memory. */
386 free_decl
387 = create_subprog_decl (get_identifier ("__gnat_free"), NULL_TREE,
388 build_function_type (void_type_node,
389 tree_cons (NULL_TREE,
390 ptr_void_type_node,
391 t)),
392 NULL_TREE, false, true, true, NULL, Empty);
393
394 /* This is used for 64-bit multiplication with overflow checking. */
395 mulv64_decl
396 = create_subprog_decl (get_identifier ("__gnat_mulv64"), NULL_TREE,
397 build_function_type_list (int64_type, int64_type,
398 int64_type, NULL_TREE),
399 NULL_TREE, false, true, true, NULL, Empty);
400
401 /* Make the types and functions used for exception processing. */
402 jmpbuf_type
403 = build_array_type (gnat_type_for_mode (Pmode, 0),
404 build_index_type (build_int_cst (NULL_TREE, 5)));
405 record_builtin_type ("JMPBUF_T", jmpbuf_type);
406 jmpbuf_ptr_type = build_pointer_type (jmpbuf_type);
407
408 /* Functions to get and set the jumpbuf pointer for the current thread. */
409 get_jmpbuf_decl
410 = create_subprog_decl
411 (get_identifier ("system__soft_links__get_jmpbuf_address_soft"),
412 NULL_TREE, build_function_type (jmpbuf_ptr_type, NULL_TREE),
413 NULL_TREE, false, true, true, NULL, Empty);
414 /* Avoid creating superfluous edges to __builtin_setjmp receivers. */
415 DECL_PURE_P (get_jmpbuf_decl) = 1;
416
417 set_jmpbuf_decl
418 = create_subprog_decl
419 (get_identifier ("system__soft_links__set_jmpbuf_address_soft"),
420 NULL_TREE,
421 build_function_type (void_type_node,
422 tree_cons (NULL_TREE, jmpbuf_ptr_type, t)),
423 NULL_TREE, false, true, true, NULL, Empty);
424
425 /* setjmp returns an integer and has one operand, which is a pointer to
426 a jmpbuf. */
427 setjmp_decl
428 = create_subprog_decl
429 (get_identifier ("__builtin_setjmp"), NULL_TREE,
430 build_function_type (integer_type_node,
431 tree_cons (NULL_TREE, jmpbuf_ptr_type, t)),
432 NULL_TREE, false, true, true, NULL, Empty);
433
434 DECL_BUILT_IN_CLASS (setjmp_decl) = BUILT_IN_NORMAL;
435 DECL_FUNCTION_CODE (setjmp_decl) = BUILT_IN_SETJMP;
436
437 /* update_setjmp_buf updates a setjmp buffer from the current stack pointer
438 address. */
439 update_setjmp_buf_decl
440 = create_subprog_decl
441 (get_identifier ("__builtin_update_setjmp_buf"), NULL_TREE,
442 build_function_type (void_type_node,
443 tree_cons (NULL_TREE, jmpbuf_ptr_type, t)),
444 NULL_TREE, false, true, true, NULL, Empty);
445
446 DECL_BUILT_IN_CLASS (update_setjmp_buf_decl) = BUILT_IN_NORMAL;
447 DECL_FUNCTION_CODE (update_setjmp_buf_decl) = BUILT_IN_UPDATE_SETJMP_BUF;
448
449 /* Hooks to call when entering/leaving an exception handler. */
450 begin_handler_decl
451 = create_subprog_decl (get_identifier ("__gnat_begin_handler"), NULL_TREE,
452 build_function_type (void_type_node,
453 tree_cons (NULL_TREE,
454 ptr_void_type_node,
455 t)),
456 NULL_TREE, false, true, true, NULL, Empty);
457
458 end_handler_decl
459 = create_subprog_decl (get_identifier ("__gnat_end_handler"), NULL_TREE,
460 build_function_type (void_type_node,
461 tree_cons (NULL_TREE,
462 ptr_void_type_node,
463 t)),
464 NULL_TREE, false, true, true, NULL, Empty);
465
466 /* If in no exception handlers mode, all raise statements are redirected to
467 __gnat_last_chance_handler. No need to redefine raise_nodefer_decl since
468 this procedure will never be called in this mode. */
469 if (No_Exception_Handlers_Set ())
470 {
471 tree decl
472 = create_subprog_decl
473 (get_identifier ("__gnat_last_chance_handler"), NULL_TREE,
474 build_function_type (void_type_node,
475 tree_cons (NULL_TREE,
476 build_pointer_type (char_type_node),
477 tree_cons (NULL_TREE,
478 integer_type_node,
479 t))),
480 NULL_TREE, false, true, true, NULL, Empty);
481
482 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++)
483 gnat_raise_decls[i] = decl;
484 }
485 else
486 /* Otherwise, make one decl for each exception reason. */
487 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++)
488 {
489 char name[17];
490
491 sprintf (name, "__gnat_rcheck_%.2d", i);
492 gnat_raise_decls[i]
493 = create_subprog_decl
494 (get_identifier (name), NULL_TREE,
495 build_function_type (void_type_node,
496 tree_cons (NULL_TREE,
497 build_pointer_type
498 (char_type_node),
499 tree_cons (NULL_TREE,
500 integer_type_node,
501 t))),
502 NULL_TREE, false, true, true, NULL, Empty);
503 }
504
505 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++)
506 {
507 TREE_THIS_VOLATILE (gnat_raise_decls[i]) = 1;
508 TREE_SIDE_EFFECTS (gnat_raise_decls[i]) = 1;
509 TREE_TYPE (gnat_raise_decls[i])
510 = build_qualified_type (TREE_TYPE (gnat_raise_decls[i]),
511 TYPE_QUAL_VOLATILE);
512 }
513
514 /* Set the types that GCC and Gigi use from the front end. We would
515 like to do this for char_type_node, but it needs to correspond to
516 the C char type. */
517 exception_type
518 = gnat_to_gnu_entity (Base_Type (standard_exception_type), NULL_TREE, 0);
519 except_type_node = TREE_TYPE (exception_type);
520
521 /* Make other functions used for exception processing. */
522 get_excptr_decl
523 = create_subprog_decl
524 (get_identifier ("system__soft_links__get_gnat_exception"),
525 NULL_TREE,
526 build_function_type (build_pointer_type (except_type_node), NULL_TREE),
527 NULL_TREE, false, true, true, NULL, Empty);
528 /* Avoid creating superfluous edges to __builtin_setjmp receivers. */
529 DECL_PURE_P (get_excptr_decl) = 1;
530
531 raise_nodefer_decl
532 = create_subprog_decl
533 (get_identifier ("__gnat_raise_nodefer_with_msg"), NULL_TREE,
534 build_function_type (void_type_node,
535 tree_cons (NULL_TREE,
536 build_pointer_type (except_type_node),
537 t)),
538 NULL_TREE, false, true, true, NULL, Empty);
539
540 /* Indicate that these never return. */
541 TREE_THIS_VOLATILE (raise_nodefer_decl) = 1;
542 TREE_SIDE_EFFECTS (raise_nodefer_decl) = 1;
543 TREE_TYPE (raise_nodefer_decl)
544 = build_qualified_type (TREE_TYPE (raise_nodefer_decl),
545 TYPE_QUAL_VOLATILE);
546
547 /* Build the special descriptor type and its null node if needed. */
548 if (TARGET_VTABLE_USES_DESCRIPTORS)
549 {
550 tree null_node = fold_convert (ptr_void_ftype, null_pointer_node);
551 tree field_list = NULL_TREE, null_list = NULL_TREE;
552 int j;
553
554 fdesc_type_node = make_node (RECORD_TYPE);
555
556 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; j++)
557 {
558 tree field = create_field_decl (NULL_TREE, ptr_void_ftype,
559 fdesc_type_node, 0, 0, 0, 1);
560 TREE_CHAIN (field) = field_list;
561 field_list = field;
562 null_list = tree_cons (field, null_node, null_list);
563 }
564
565 finish_record_type (fdesc_type_node, nreverse (field_list), 0, true);
566 record_builtin_type ("descriptor", fdesc_type_node);
567 null_fdesc_node = gnat_build_constructor (fdesc_type_node, null_list);
568 }
569
570 long_long_float_type
571 = gnat_to_gnu_entity (Base_Type (standard_long_long_float), NULL_TREE, 0);
572
573 if (TREE_CODE (TREE_TYPE (long_long_float_type)) == INTEGER_TYPE)
574 {
575 /* In this case, the builtin floating point types are VAX float,
576 so make up a type for use. */
577 longest_float_type_node = make_node (REAL_TYPE);
578 TYPE_PRECISION (longest_float_type_node) = LONG_DOUBLE_TYPE_SIZE;
579 layout_type (longest_float_type_node);
580 record_builtin_type ("longest float type", longest_float_type_node);
581 }
582 else
583 longest_float_type_node = TREE_TYPE (long_long_float_type);
584
585 /* Dummy objects to materialize "others" and "all others" in the exception
586 tables. These are exported by a-exexpr.adb, so see this unit for the
587 types to use. */
588 others_decl
589 = create_var_decl (get_identifier ("OTHERS"),
590 get_identifier ("__gnat_others_value"),
591 integer_type_node, 0, 1, 0, 1, 1, 0, Empty);
592
593 all_others_decl
594 = create_var_decl (get_identifier ("ALL_OTHERS"),
595 get_identifier ("__gnat_all_others_value"),
596 integer_type_node, 0, 1, 0, 1, 1, 0, Empty);
597
598 main_identifier_node = get_identifier ("main");
599
600 /* Install the builtins we might need, either internally or as
601 user available facilities for Intrinsic imports. */
602 gnat_install_builtins ();
603
604 gnu_except_ptr_stack = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
605 gnu_constraint_error_label_stack
606 = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
607 gnu_storage_error_label_stack = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
608 gnu_program_error_label_stack = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
609
610 /* Process any Pragma Ident for the main unit. */
611 #ifdef ASM_OUTPUT_IDENT
612 if (Present (Ident_String (Main_Unit)))
613 ASM_OUTPUT_IDENT
614 (asm_out_file,
615 TREE_STRING_POINTER (gnat_to_gnu (Ident_String (Main_Unit))));
616 #endif
617
618 /* If we are using the GCC exception mechanism, let GCC know. */
619 if (Exception_Mechanism == Back_End_Exceptions)
620 gnat_init_gcc_eh ();
621
622 gcc_assert (Nkind (gnat_root) == N_Compilation_Unit);
623
624 /* Declare the name of the compilation unit as the first global
625 name in order to make the middle-end fully deterministic. */
626 t = create_concat_name (Defining_Entity (Unit (gnat_root)), NULL);
627 first_global_object_name = ggc_strdup (IDENTIFIER_POINTER (t));
628
629 /* Now translate the compilation unit proper. */
630 start_stmt_group ();
631 Compilation_Unit_to_gnu (gnat_root);
632
633 /* Finally see if we have any elaboration procedures to deal with. */
634 for (info = elab_info_list; info; info = info->next)
635 {
636 tree gnu_body = DECL_SAVED_TREE (info->elab_proc);
637
638 /* Unshare SAVE_EXPRs between subprograms. These are not unshared by
639 the gimplifier for obvious reasons, but it turns out that we need to
640 unshare them for the global level because of SAVE_EXPRs made around
641 checks for global objects and around allocators for global objects
642 of variable size, in order to prevent node sharing in the underlying
643 expression. Note that this implicitly assumes that the SAVE_EXPR
644 nodes themselves are not shared between subprograms, which would be
645 an upstream bug for which we would not change the outcome. */
646 walk_tree_without_duplicates (&gnu_body, unshare_save_expr, NULL);
647
648 /* Process the function as others, but for indicating this is an
649 elab proc, to be discarded if empty, then propagate the status
650 up to the GNAT tree node. */
651 begin_subprog_body (info->elab_proc);
652 end_subprog_body (gnu_body, true);
653
654 if (empty_body_p (gimple_body (info->elab_proc)))
655 Set_Has_No_Elaboration_Code (info->gnat_node, 1);
656 }
657
658 /* We cannot track the location of errors past this point. */
659 error_gnat_node = Empty;
660 }
661 \f
662 /* Return a positive value if an lvalue is required for GNAT_NODE.
663 GNU_TYPE is the type that will be used for GNAT_NODE in the
664 translated GNU tree. ALIASED indicates whether the underlying
665 object represented by GNAT_NODE is aliased in the Ada sense.
666
667 The function climbs up the GNAT tree starting from the node and
668 returns 1 upon encountering a node that effectively requires an
669 lvalue downstream. It returns int instead of bool to facilitate
670 usage in non purely binary logic contexts. */
671
672 static int
673 lvalue_required_p (Node_Id gnat_node, tree gnu_type, int aliased)
674 {
675 Node_Id gnat_parent = Parent (gnat_node), gnat_temp;
676
677 switch (Nkind (gnat_parent))
678 {
679 case N_Reference:
680 return 1;
681
682 case N_Attribute_Reference:
683 {
684 unsigned char id = Get_Attribute_Id (Attribute_Name (gnat_parent));
685 return id == Attr_Address
686 || id == Attr_Access
687 || id == Attr_Unchecked_Access
688 || id == Attr_Unrestricted_Access;
689 }
690
691 case N_Parameter_Association:
692 case N_Function_Call:
693 case N_Procedure_Call_Statement:
694 return (must_pass_by_ref (gnu_type) || default_pass_by_ref (gnu_type));
695
696 case N_Indexed_Component:
697 /* Only the array expression can require an lvalue. */
698 if (Prefix (gnat_parent) != gnat_node)
699 return 0;
700
701 /* ??? Consider that referencing an indexed component with a
702 non-constant index forces the whole aggregate to memory.
703 Note that N_Integer_Literal is conservative, any static
704 expression in the RM sense could probably be accepted. */
705 for (gnat_temp = First (Expressions (gnat_parent));
706 Present (gnat_temp);
707 gnat_temp = Next (gnat_temp))
708 if (Nkind (gnat_temp) != N_Integer_Literal)
709 return 1;
710
711 /* ... fall through ... */
712
713 case N_Slice:
714 /* Only the array expression can require an lvalue. */
715 if (Prefix (gnat_parent) != gnat_node)
716 return 0;
717
718 aliased |= Has_Aliased_Components (Etype (gnat_node));
719 return lvalue_required_p (gnat_parent, gnu_type, aliased);
720
721 case N_Selected_Component:
722 aliased |= Is_Aliased (Entity (Selector_Name (gnat_parent)));
723 return lvalue_required_p (gnat_parent, gnu_type, aliased);
724
725 case N_Object_Renaming_Declaration:
726 /* We need to make a real renaming only if the constant object is
727 aliased or if we may use a renaming pointer; otherwise we can
728 optimize and return the rvalue. We make an exception if the object
729 is an identifier since in this case the rvalue can be propagated
730 attached to the CONST_DECL. */
731 return (aliased != 0
732 /* This should match the constant case of the renaming code. */
733 || Is_Composite_Type
734 (Underlying_Type (Etype (Name (gnat_parent))))
735 || Nkind (Name (gnat_parent)) == N_Identifier);
736
737 default:
738 return 0;
739 }
740
741 gcc_unreachable ();
742 }
743
744 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Identifier,
745 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer
746 to where we should place the result type. */
747
748 static tree
749 Identifier_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
750 {
751 Node_Id gnat_temp, gnat_temp_type;
752 tree gnu_result, gnu_result_type;
753
754 /* Whether we should require an lvalue for GNAT_NODE. Needed in
755 specific circumstances only, so evaluated lazily. < 0 means
756 unknown, > 0 means known true, 0 means known false. */
757 int require_lvalue = -1;
758
759 /* If GNAT_NODE is a constant, whether we should use the initialization
760 value instead of the constant entity, typically for scalars with an
761 address clause when the parent doesn't require an lvalue. */
762 bool use_constant_initializer = false;
763
764 /* If the Etype of this node does not equal the Etype of the Entity,
765 something is wrong with the entity map, probably in generic
766 instantiation. However, this does not apply to types. Since we sometime
767 have strange Ekind's, just do this test for objects. Also, if the Etype of
768 the Entity is private, the Etype of the N_Identifier is allowed to be the
769 full type and also we consider a packed array type to be the same as the
770 original type. Similarly, a class-wide type is equivalent to a subtype of
771 itself. Finally, if the types are Itypes, one may be a copy of the other,
772 which is also legal. */
773 gnat_temp = (Nkind (gnat_node) == N_Defining_Identifier
774 ? gnat_node : Entity (gnat_node));
775 gnat_temp_type = Etype (gnat_temp);
776
777 gcc_assert (Etype (gnat_node) == gnat_temp_type
778 || (Is_Packed (gnat_temp_type)
779 && Etype (gnat_node) == Packed_Array_Type (gnat_temp_type))
780 || (Is_Class_Wide_Type (Etype (gnat_node)))
781 || (IN (Ekind (gnat_temp_type), Private_Kind)
782 && Present (Full_View (gnat_temp_type))
783 && ((Etype (gnat_node) == Full_View (gnat_temp_type))
784 || (Is_Packed (Full_View (gnat_temp_type))
785 && (Etype (gnat_node)
786 == Packed_Array_Type (Full_View
787 (gnat_temp_type))))))
788 || (Is_Itype (Etype (gnat_node)) && Is_Itype (gnat_temp_type))
789 || !(Ekind (gnat_temp) == E_Variable
790 || Ekind (gnat_temp) == E_Component
791 || Ekind (gnat_temp) == E_Constant
792 || Ekind (gnat_temp) == E_Loop_Parameter
793 || IN (Ekind (gnat_temp), Formal_Kind)));
794
795 /* If this is a reference to a deferred constant whose partial view is an
796 unconstrained private type, the proper type is on the full view of the
797 constant, not on the full view of the type, which may be unconstrained.
798
799 This may be a reference to a type, for example in the prefix of the
800 attribute Position, generated for dispatching code (see Make_DT in
801 exp_disp,adb). In that case we need the type itself, not is parent,
802 in particular if it is a derived type */
803 if (Is_Private_Type (gnat_temp_type)
804 && Has_Unknown_Discriminants (gnat_temp_type)
805 && Ekind (gnat_temp) == E_Constant
806 && Present (Full_View (gnat_temp)))
807 {
808 gnat_temp = Full_View (gnat_temp);
809 gnat_temp_type = Etype (gnat_temp);
810 }
811 else
812 {
813 /* We want to use the Actual_Subtype if it has already been elaborated,
814 otherwise the Etype. Avoid using Actual_Subtype for packed arrays to
815 simplify things. */
816 if ((Ekind (gnat_temp) == E_Constant
817 || Ekind (gnat_temp) == E_Variable || Is_Formal (gnat_temp))
818 && !(Is_Array_Type (Etype (gnat_temp))
819 && Present (Packed_Array_Type (Etype (gnat_temp))))
820 && Present (Actual_Subtype (gnat_temp))
821 && present_gnu_tree (Actual_Subtype (gnat_temp)))
822 gnat_temp_type = Actual_Subtype (gnat_temp);
823 else
824 gnat_temp_type = Etype (gnat_node);
825 }
826
827 /* Expand the type of this identifier first, in case it is an enumeral
828 literal, which only get made when the type is expanded. There is no
829 order-of-elaboration issue here. */
830 gnu_result_type = get_unpadded_type (gnat_temp_type);
831
832 /* If this is a non-imported scalar constant with an address clause,
833 retrieve the value instead of a pointer to be dereferenced unless
834 an lvalue is required. This is generally more efficient and actually
835 required if this is a static expression because it might be used
836 in a context where a dereference is inappropriate, such as a case
837 statement alternative or a record discriminant. There is no possible
838 volatile-ness short-circuit here since Volatile constants must bei
839 imported per C.6. */
840 if (Ekind (gnat_temp) == E_Constant && Is_Scalar_Type (gnat_temp_type)
841 && !Is_Imported (gnat_temp)
842 && Present (Address_Clause (gnat_temp)))
843 {
844 require_lvalue = lvalue_required_p (gnat_node, gnu_result_type,
845 Is_Aliased (gnat_temp));
846 use_constant_initializer = !require_lvalue;
847 }
848
849 if (use_constant_initializer)
850 {
851 /* If this is a deferred constant, the initializer is attached to
852 the full view. */
853 if (Present (Full_View (gnat_temp)))
854 gnat_temp = Full_View (gnat_temp);
855
856 gnu_result = gnat_to_gnu (Expression (Declaration_Node (gnat_temp)));
857 }
858 else
859 gnu_result = gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0);
860
861 /* If we are in an exception handler, force this variable into memory to
862 ensure optimization does not remove stores that appear redundant but are
863 actually needed in case an exception occurs.
864
865 ??? Note that we need not do this if the variable is declared within the
866 handler, only if it is referenced in the handler and declared in an
867 enclosing block, but we have no way of testing that right now.
868
869 ??? We used to essentially set the TREE_ADDRESSABLE flag on the variable
870 here, but it can now be removed by the Tree aliasing machinery if the
871 address of the variable is never taken. All we can do is to make the
872 variable volatile, which might incur the generation of temporaries just
873 to access the memory in some circumstances. This can be avoided for
874 variables of non-constant size because they are automatically allocated
875 to memory. There might be no way of allocating a proper temporary for
876 them in any case. We only do this for SJLJ though. */
877 if (TREE_VALUE (gnu_except_ptr_stack)
878 && TREE_CODE (gnu_result) == VAR_DECL
879 && TREE_CODE (DECL_SIZE_UNIT (gnu_result)) == INTEGER_CST)
880 TREE_THIS_VOLATILE (gnu_result) = TREE_SIDE_EFFECTS (gnu_result) = 1;
881
882 /* Some objects (such as parameters passed by reference, globals of
883 variable size, and renamed objects) actually represent the address
884 of the object. In that case, we must do the dereference. Likewise,
885 deal with parameters to foreign convention subprograms. */
886 if (DECL_P (gnu_result)
887 && (DECL_BY_REF_P (gnu_result)
888 || (TREE_CODE (gnu_result) == PARM_DECL
889 && DECL_BY_COMPONENT_PTR_P (gnu_result))))
890 {
891 bool ro = DECL_POINTS_TO_READONLY_P (gnu_result);
892 tree renamed_obj;
893
894 if (TREE_CODE (gnu_result) == PARM_DECL
895 && DECL_BY_COMPONENT_PTR_P (gnu_result))
896 gnu_result
897 = build_unary_op (INDIRECT_REF, NULL_TREE,
898 convert (build_pointer_type (gnu_result_type),
899 gnu_result));
900
901 /* If it's a renaming pointer and we are at the right binding level,
902 we can reference the renamed object directly, since the renamed
903 expression has been protected against multiple evaluations. */
904 else if (TREE_CODE (gnu_result) == VAR_DECL
905 && (renamed_obj = DECL_RENAMED_OBJECT (gnu_result)) != 0
906 && (! DECL_RENAMING_GLOBAL_P (gnu_result)
907 || global_bindings_p ()))
908 gnu_result = renamed_obj;
909
910 /* Return the underlying CST for a CONST_DECL like a few lines below,
911 after dereferencing in this case. */
912 else if (TREE_CODE (gnu_result) == CONST_DECL)
913 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE,
914 DECL_INITIAL (gnu_result));
915
916 else
917 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
918
919 TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result) = ro;
920 }
921
922 /* The GNAT tree has the type of a function as the type of its result. Also
923 use the type of the result if the Etype is a subtype which is nominally
924 unconstrained. But remove any padding from the resulting type. */
925 if (TREE_CODE (TREE_TYPE (gnu_result)) == FUNCTION_TYPE
926 || Is_Constr_Subt_For_UN_Aliased (gnat_temp_type))
927 {
928 gnu_result_type = TREE_TYPE (gnu_result);
929 if (TREE_CODE (gnu_result_type) == RECORD_TYPE
930 && TYPE_IS_PADDING_P (gnu_result_type))
931 gnu_result_type = TREE_TYPE (TYPE_FIELDS (gnu_result_type));
932 }
933
934 /* If we have a constant declaration and its initializer at hand,
935 try to return the latter to avoid the need to call fold in lots
936 of places and the need of elaboration code if this Id is used as
937 an initializer itself. */
938 if (TREE_CONSTANT (gnu_result)
939 && DECL_P (gnu_result)
940 && DECL_INITIAL (gnu_result))
941 {
942 tree object
943 = (TREE_CODE (gnu_result) == CONST_DECL
944 ? DECL_CONST_CORRESPONDING_VAR (gnu_result) : gnu_result);
945
946 /* If there is a corresponding variable, we only want to return
947 the CST value if an lvalue is not required. Evaluate this
948 now if we have not already done so. */
949 if (object && require_lvalue < 0)
950 require_lvalue = lvalue_required_p (gnat_node, gnu_result_type,
951 Is_Aliased (gnat_temp));
952
953 if (!object || !require_lvalue)
954 gnu_result = unshare_expr (DECL_INITIAL (gnu_result));
955 }
956
957 *gnu_result_type_p = gnu_result_type;
958 return gnu_result;
959 }
960 \f
961 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Pragma. Return
962 any statements we generate. */
963
964 static tree
965 Pragma_to_gnu (Node_Id gnat_node)
966 {
967 Node_Id gnat_temp;
968 tree gnu_result = alloc_stmt_list ();
969
970 /* Check for (and ignore) unrecognized pragma and do nothing if we are just
971 annotating types. */
972 if (type_annotate_only
973 || !Is_Pragma_Name (Chars (Pragma_Identifier (gnat_node))))
974 return gnu_result;
975
976 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node))))
977 {
978 case Pragma_Inspection_Point:
979 /* Do nothing at top level: all such variables are already viewable. */
980 if (global_bindings_p ())
981 break;
982
983 for (gnat_temp = First (Pragma_Argument_Associations (gnat_node));
984 Present (gnat_temp);
985 gnat_temp = Next (gnat_temp))
986 {
987 Node_Id gnat_expr = Expression (gnat_temp);
988 tree gnu_expr = gnat_to_gnu (gnat_expr);
989 int use_address;
990 enum machine_mode mode;
991 tree asm_constraint = NULL_TREE;
992 #ifdef ASM_COMMENT_START
993 char *comment;
994 #endif
995
996 if (TREE_CODE (gnu_expr) == UNCONSTRAINED_ARRAY_REF)
997 gnu_expr = TREE_OPERAND (gnu_expr, 0);
998
999 /* Use the value only if it fits into a normal register,
1000 otherwise use the address. */
1001 mode = TYPE_MODE (TREE_TYPE (gnu_expr));
1002 use_address = ((GET_MODE_CLASS (mode) != MODE_INT
1003 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1004 || GET_MODE_SIZE (mode) > UNITS_PER_WORD);
1005
1006 if (use_address)
1007 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
1008
1009 #ifdef ASM_COMMENT_START
1010 comment = concat (ASM_COMMENT_START,
1011 " inspection point: ",
1012 Get_Name_String (Chars (gnat_expr)),
1013 use_address ? " address" : "",
1014 " is in %0",
1015 NULL);
1016 asm_constraint = build_string (strlen (comment), comment);
1017 free (comment);
1018 #endif
1019 gnu_expr = build4 (ASM_EXPR, void_type_node,
1020 asm_constraint,
1021 NULL_TREE,
1022 tree_cons
1023 (build_tree_list (NULL_TREE,
1024 build_string (1, "g")),
1025 gnu_expr, NULL_TREE),
1026 NULL_TREE);
1027 ASM_VOLATILE_P (gnu_expr) = 1;
1028 set_expr_location_from_node (gnu_expr, gnat_node);
1029 append_to_statement_list (gnu_expr, &gnu_result);
1030 }
1031 break;
1032
1033 case Pragma_Optimize:
1034 switch (Chars (Expression
1035 (First (Pragma_Argument_Associations (gnat_node)))))
1036 {
1037 case Name_Time: case Name_Space:
1038 if (!optimize)
1039 post_error ("insufficient -O value?", gnat_node);
1040 break;
1041
1042 case Name_Off:
1043 if (optimize)
1044 post_error ("must specify -O0?", gnat_node);
1045 break;
1046
1047 default:
1048 gcc_unreachable ();
1049 }
1050 break;
1051
1052 case Pragma_Reviewable:
1053 if (write_symbols == NO_DEBUG)
1054 post_error ("must specify -g?", gnat_node);
1055 break;
1056 }
1057
1058 return gnu_result;
1059 }
1060 \f
1061 /* Issue an error message if GNAT_NODE references an eliminated entity. */
1062
1063 static void
1064 check_for_eliminated_entity (Node_Id gnat_node)
1065 {
1066 switch (Nkind (gnat_node))
1067 {
1068 case N_Identifier:
1069 case N_Operator_Symbol:
1070 case N_Expanded_Name:
1071 case N_Attribute_Reference:
1072 if (Is_Eliminated (Entity (gnat_node)))
1073 Eliminate_Error_Msg (gnat_node, Entity (gnat_node));
1074 break;
1075 }
1076 }
1077
1078 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Attribute,
1079 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to
1080 where we should place the result type. ATTRIBUTE is the attribute ID. */
1081
1082 static tree
1083 Attribute_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, int attribute)
1084 {
1085 tree gnu_result = error_mark_node;
1086 tree gnu_result_type;
1087 tree gnu_expr;
1088 bool prefix_unused = false;
1089 tree gnu_prefix = gnat_to_gnu (Prefix (gnat_node));
1090 tree gnu_type = TREE_TYPE (gnu_prefix);
1091
1092 /* If the input is a NULL_EXPR, make a new one. */
1093 if (TREE_CODE (gnu_prefix) == NULL_EXPR)
1094 {
1095 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
1096 return build1 (NULL_EXPR, *gnu_result_type_p,
1097 TREE_OPERAND (gnu_prefix, 0));
1098 }
1099
1100 switch (attribute)
1101 {
1102 case Attr_Pos:
1103 case Attr_Val:
1104 /* These are just conversions until since representation clauses for
1105 enumerations are handled in the front end. */
1106 {
1107 bool checkp = Do_Range_Check (First (Expressions (gnat_node)));
1108
1109 gnu_result = gnat_to_gnu (First (Expressions (gnat_node)));
1110 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1111 gnu_result = convert_with_check (Etype (gnat_node), gnu_result,
1112 checkp, checkp, true, gnat_node);
1113 }
1114 break;
1115
1116 case Attr_Pred:
1117 case Attr_Succ:
1118 /* These just add or subject the constant 1. Representation clauses for
1119 enumerations are handled in the front-end. */
1120 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
1121 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1122
1123 if (Do_Range_Check (First (Expressions (gnat_node))))
1124 {
1125 gnu_expr = protect_multiple_eval (gnu_expr);
1126 gnu_expr
1127 = emit_check
1128 (build_binary_op (EQ_EXPR, integer_type_node,
1129 gnu_expr,
1130 attribute == Attr_Pred
1131 ? TYPE_MIN_VALUE (gnu_result_type)
1132 : TYPE_MAX_VALUE (gnu_result_type)),
1133 gnu_expr, CE_Range_Check_Failed, gnat_node);
1134 }
1135
1136 gnu_result
1137 = build_binary_op (attribute == Attr_Pred
1138 ? MINUS_EXPR : PLUS_EXPR,
1139 gnu_result_type, gnu_expr,
1140 convert (gnu_result_type, integer_one_node));
1141 break;
1142
1143 case Attr_Address:
1144 case Attr_Unrestricted_Access:
1145 /* Conversions don't change something's address but can cause us to miss
1146 the COMPONENT_REF case below, so strip them off. */
1147 gnu_prefix = remove_conversions (gnu_prefix,
1148 !Must_Be_Byte_Aligned (gnat_node));
1149
1150 /* If we are taking 'Address of an unconstrained object, this is the
1151 pointer to the underlying array. */
1152 if (attribute == Attr_Address)
1153 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
1154
1155 /* If we are building a static dispatch table, we have to honor
1156 TARGET_VTABLE_USES_DESCRIPTORS if we want to be compatible
1157 with the C++ ABI. We do it in the non-static case as well,
1158 see gnat_to_gnu_entity, case E_Access_Subprogram_Type. */
1159 else if (TARGET_VTABLE_USES_DESCRIPTORS
1160 && Is_Dispatch_Table_Entity (Etype (gnat_node)))
1161 {
1162 tree gnu_field, gnu_list = NULL_TREE, t;
1163 /* Descriptors can only be built here for top-level functions. */
1164 bool build_descriptor = (global_bindings_p () != 0);
1165 int i;
1166
1167 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1168
1169 /* If we're not going to build the descriptor, we have to retrieve
1170 the one which will be built by the linker (or by the compiler
1171 later if a static chain is requested). */
1172 if (!build_descriptor)
1173 {
1174 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_prefix);
1175 gnu_result = fold_convert (build_pointer_type (gnu_result_type),
1176 gnu_result);
1177 gnu_result = build1 (INDIRECT_REF, gnu_result_type, gnu_result);
1178 }
1179
1180 for (gnu_field = TYPE_FIELDS (gnu_result_type), i = 0;
1181 i < TARGET_VTABLE_USES_DESCRIPTORS;
1182 gnu_field = TREE_CHAIN (gnu_field), i++)
1183 {
1184 if (build_descriptor)
1185 {
1186 t = build2 (FDESC_EXPR, TREE_TYPE (gnu_field), gnu_prefix,
1187 build_int_cst (NULL_TREE, i));
1188 TREE_CONSTANT (t) = 1;
1189 }
1190 else
1191 t = build3 (COMPONENT_REF, ptr_void_ftype, gnu_result,
1192 gnu_field, NULL_TREE);
1193
1194 gnu_list = tree_cons (gnu_field, t, gnu_list);
1195 }
1196
1197 gnu_result = gnat_build_constructor (gnu_result_type, gnu_list);
1198 break;
1199 }
1200
1201 /* ... fall through ... */
1202
1203 case Attr_Access:
1204 case Attr_Unchecked_Access:
1205 case Attr_Code_Address:
1206 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1207 gnu_result
1208 = build_unary_op (((attribute == Attr_Address
1209 || attribute == Attr_Unrestricted_Access)
1210 && !Must_Be_Byte_Aligned (gnat_node))
1211 ? ATTR_ADDR_EXPR : ADDR_EXPR,
1212 gnu_result_type, gnu_prefix);
1213
1214 /* For 'Code_Address, find an inner ADDR_EXPR and mark it so that we
1215 don't try to build a trampoline. */
1216 if (attribute == Attr_Code_Address)
1217 {
1218 check_for_eliminated_entity (Prefix (gnat_node));
1219
1220 for (gnu_expr = gnu_result;
1221 CONVERT_EXPR_P (gnu_expr);
1222 gnu_expr = TREE_OPERAND (gnu_expr, 0))
1223 TREE_CONSTANT (gnu_expr) = 1;
1224
1225 if (TREE_CODE (gnu_expr) == ADDR_EXPR)
1226 TREE_NO_TRAMPOLINE (gnu_expr) = TREE_CONSTANT (gnu_expr) = 1;
1227 }
1228
1229 /* For other address attributes applied to a nested function,
1230 find an inner ADDR_EXPR and annotate it so that we can issue
1231 a useful warning with -Wtrampolines. */
1232 else if (TREE_CODE (TREE_TYPE (gnu_prefix)) == FUNCTION_TYPE)
1233 {
1234 check_for_eliminated_entity (Prefix (gnat_node));
1235
1236 for (gnu_expr = gnu_result;
1237 CONVERT_EXPR_P (gnu_expr);
1238 gnu_expr = TREE_OPERAND (gnu_expr, 0))
1239 ;
1240
1241 if (TREE_CODE (gnu_expr) == ADDR_EXPR
1242 && decl_function_context (TREE_OPERAND (gnu_expr, 0)))
1243 {
1244 set_expr_location_from_node (gnu_expr, gnat_node);
1245
1246 /* Check that we're not violating the No_Implicit_Dynamic_Code
1247 restriction. Be conservative if we don't know anything
1248 about the trampoline strategy for the target. */
1249 Check_Implicit_Dynamic_Code_Allowed (gnat_node);
1250 }
1251 }
1252 break;
1253
1254 case Attr_Pool_Address:
1255 {
1256 tree gnu_obj_type;
1257 tree gnu_ptr = gnu_prefix;
1258
1259 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1260
1261 /* If this is an unconstrained array, we know the object must have been
1262 allocated with the template in front of the object. So compute the
1263 template address.*/
1264 if (TYPE_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
1265 gnu_ptr
1266 = convert (build_pointer_type
1267 (TYPE_OBJECT_RECORD_TYPE
1268 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
1269 gnu_ptr);
1270
1271 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
1272 if (TREE_CODE (gnu_obj_type) == RECORD_TYPE
1273 && TYPE_CONTAINS_TEMPLATE_P (gnu_obj_type))
1274 {
1275 tree gnu_char_ptr_type = build_pointer_type (char_type_node);
1276 tree gnu_pos = byte_position (TYPE_FIELDS (gnu_obj_type));
1277 tree gnu_byte_offset
1278 = convert (sizetype,
1279 size_diffop (size_zero_node, gnu_pos));
1280 gnu_byte_offset = fold_build1 (NEGATE_EXPR, sizetype, gnu_byte_offset);
1281
1282 gnu_ptr = convert (gnu_char_ptr_type, gnu_ptr);
1283 gnu_ptr = build_binary_op (POINTER_PLUS_EXPR, gnu_char_ptr_type,
1284 gnu_ptr, gnu_byte_offset);
1285 }
1286
1287 gnu_result = convert (gnu_result_type, gnu_ptr);
1288 }
1289 break;
1290
1291 case Attr_Size:
1292 case Attr_Object_Size:
1293 case Attr_Value_Size:
1294 case Attr_Max_Size_In_Storage_Elements:
1295 gnu_expr = gnu_prefix;
1296
1297 /* Remove NOPS from gnu_expr and conversions from gnu_prefix.
1298 We only use GNU_EXPR to see if a COMPONENT_REF was involved. */
1299 while (TREE_CODE (gnu_expr) == NOP_EXPR)
1300 gnu_expr = TREE_OPERAND (gnu_expr, 0);
1301
1302 gnu_prefix = remove_conversions (gnu_prefix, true);
1303 prefix_unused = true;
1304 gnu_type = TREE_TYPE (gnu_prefix);
1305
1306 /* Replace an unconstrained array type with the type of the underlying
1307 array. We can't do this with a call to maybe_unconstrained_array
1308 since we may have a TYPE_DECL. For 'Max_Size_In_Storage_Elements,
1309 use the record type that will be used to allocate the object and its
1310 template. */
1311 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
1312 {
1313 gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type);
1314 if (attribute != Attr_Max_Size_In_Storage_Elements)
1315 gnu_type = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)));
1316 }
1317
1318 /* If we're looking for the size of a field, return the field size.
1319 Otherwise, if the prefix is an object, or if 'Object_Size or
1320 'Max_Size_In_Storage_Elements has been specified, the result is the
1321 GCC size of the type. Otherwise, the result is the RM_Size of the
1322 type. */
1323 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
1324 gnu_result = DECL_SIZE (TREE_OPERAND (gnu_prefix, 1));
1325 else if (TREE_CODE (gnu_prefix) != TYPE_DECL
1326 || attribute == Attr_Object_Size
1327 || attribute == Attr_Max_Size_In_Storage_Elements)
1328 {
1329 /* If this is a padded type, the GCC size isn't relevant to the
1330 programmer. Normally, what we want is the RM_Size, which was set
1331 from the specified size, but if it was not set, we want the size
1332 of the relevant field. Using the MAX of those two produces the
1333 right result in all case. Don't use the size of the field if it's
1334 a self-referential type, since that's never what's wanted. */
1335 if (TREE_CODE (gnu_type) == RECORD_TYPE
1336 && TYPE_IS_PADDING_P (gnu_type)
1337 && TREE_CODE (gnu_expr) == COMPONENT_REF)
1338 {
1339 gnu_result = rm_size (gnu_type);
1340 if (!(CONTAINS_PLACEHOLDER_P
1341 (DECL_SIZE (TREE_OPERAND (gnu_expr, 1)))))
1342 gnu_result
1343 = size_binop (MAX_EXPR, gnu_result,
1344 DECL_SIZE (TREE_OPERAND (gnu_expr, 1)));
1345 }
1346 else if (Nkind (Prefix (gnat_node)) == N_Explicit_Dereference)
1347 {
1348 Node_Id gnat_deref = Prefix (gnat_node);
1349 Node_Id gnat_actual_subtype
1350 = Actual_Designated_Subtype (gnat_deref);
1351 tree gnu_ptr_type
1352 = TREE_TYPE (gnat_to_gnu (Prefix (gnat_deref)));
1353
1354 if (TYPE_FAT_OR_THIN_POINTER_P (gnu_ptr_type)
1355 && Present (gnat_actual_subtype))
1356 {
1357 tree gnu_actual_obj_type
1358 = gnat_to_gnu_type (gnat_actual_subtype);
1359 gnu_type
1360 = build_unc_object_type_from_ptr (gnu_ptr_type,
1361 gnu_actual_obj_type,
1362 get_identifier ("SIZE"));
1363 }
1364
1365 gnu_result = TYPE_SIZE (gnu_type);
1366 }
1367 else
1368 gnu_result = TYPE_SIZE (gnu_type);
1369 }
1370 else
1371 gnu_result = rm_size (gnu_type);
1372
1373 gcc_assert (gnu_result);
1374
1375 /* Deal with a self-referential size by returning the maximum size for a
1376 type and by qualifying the size with the object for 'Size of an
1377 object. */
1378 if (CONTAINS_PLACEHOLDER_P (gnu_result))
1379 {
1380 if (TREE_CODE (gnu_prefix) != TYPE_DECL)
1381 gnu_result = substitute_placeholder_in_expr (gnu_result, gnu_expr);
1382 else
1383 gnu_result = max_size (gnu_result, true);
1384 }
1385
1386 /* If the type contains a template, subtract its size. */
1387 if (TREE_CODE (gnu_type) == RECORD_TYPE
1388 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1389 gnu_result = size_binop (MINUS_EXPR, gnu_result,
1390 DECL_SIZE (TYPE_FIELDS (gnu_type)));
1391
1392 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1393
1394 if (attribute == Attr_Max_Size_In_Storage_Elements)
1395 gnu_result = fold_build2 (CEIL_DIV_EXPR, bitsizetype,
1396 gnu_result, bitsize_unit_node);
1397 break;
1398
1399 case Attr_Alignment:
1400 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
1401 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))
1402 == RECORD_TYPE)
1403 && (TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))))
1404 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
1405
1406 gnu_type = TREE_TYPE (gnu_prefix);
1407 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1408 prefix_unused = true;
1409
1410 gnu_result = size_int ((TREE_CODE (gnu_prefix) == COMPONENT_REF
1411 ? DECL_ALIGN (TREE_OPERAND (gnu_prefix, 1))
1412 : TYPE_ALIGN (gnu_type)) / BITS_PER_UNIT);
1413 break;
1414
1415 case Attr_First:
1416 case Attr_Last:
1417 case Attr_Range_Length:
1418 prefix_unused = true;
1419
1420 if (INTEGRAL_TYPE_P (gnu_type) || TREE_CODE (gnu_type) == REAL_TYPE)
1421 {
1422 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1423
1424 if (attribute == Attr_First)
1425 gnu_result = TYPE_MIN_VALUE (gnu_type);
1426 else if (attribute == Attr_Last)
1427 gnu_result = TYPE_MAX_VALUE (gnu_type);
1428 else
1429 gnu_result
1430 = build_binary_op
1431 (MAX_EXPR, get_base_type (gnu_result_type),
1432 build_binary_op
1433 (PLUS_EXPR, get_base_type (gnu_result_type),
1434 build_binary_op (MINUS_EXPR,
1435 get_base_type (gnu_result_type),
1436 convert (gnu_result_type,
1437 TYPE_MAX_VALUE (gnu_type)),
1438 convert (gnu_result_type,
1439 TYPE_MIN_VALUE (gnu_type))),
1440 convert (gnu_result_type, integer_one_node)),
1441 convert (gnu_result_type, integer_zero_node));
1442
1443 break;
1444 }
1445
1446 /* ... fall through ... */
1447
1448 case Attr_Length:
1449 {
1450 int Dimension = (Present (Expressions (gnat_node))
1451 ? UI_To_Int (Intval (First (Expressions (gnat_node))))
1452 : 1), i;
1453 struct parm_attr *pa = NULL;
1454 Entity_Id gnat_param = Empty;
1455
1456 /* Make sure any implicit dereference gets done. */
1457 gnu_prefix = maybe_implicit_deref (gnu_prefix);
1458 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
1459 /* We treat unconstrained array In parameters specially. */
1460 if (Nkind (Prefix (gnat_node)) == N_Identifier
1461 && !Is_Constrained (Etype (Prefix (gnat_node)))
1462 && Ekind (Entity (Prefix (gnat_node))) == E_In_Parameter)
1463 gnat_param = Entity (Prefix (gnat_node));
1464 gnu_type = TREE_TYPE (gnu_prefix);
1465 prefix_unused = true;
1466 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1467
1468 if (TYPE_CONVENTION_FORTRAN_P (gnu_type))
1469 {
1470 int ndim;
1471 tree gnu_type_temp;
1472
1473 for (ndim = 1, gnu_type_temp = gnu_type;
1474 TREE_CODE (TREE_TYPE (gnu_type_temp)) == ARRAY_TYPE
1475 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type_temp));
1476 ndim++, gnu_type_temp = TREE_TYPE (gnu_type_temp))
1477 ;
1478
1479 Dimension = ndim + 1 - Dimension;
1480 }
1481
1482 for (i = 1; i < Dimension; i++)
1483 gnu_type = TREE_TYPE (gnu_type);
1484
1485 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
1486
1487 /* When not optimizing, look up the slot associated with the parameter
1488 and the dimension in the cache and create a new one on failure. */
1489 if (!optimize && Present (gnat_param))
1490 {
1491 for (i = 0; VEC_iterate (parm_attr, f_parm_attr_cache, i, pa); i++)
1492 if (pa->id == gnat_param && pa->dim == Dimension)
1493 break;
1494
1495 if (!pa)
1496 {
1497 pa = GGC_CNEW (struct parm_attr);
1498 pa->id = gnat_param;
1499 pa->dim = Dimension;
1500 VEC_safe_push (parm_attr, gc, f_parm_attr_cache, pa);
1501 }
1502 }
1503
1504 /* Return the cached expression or build a new one. */
1505 if (attribute == Attr_First)
1506 {
1507 if (pa && pa->first)
1508 {
1509 gnu_result = pa->first;
1510 break;
1511 }
1512
1513 gnu_result
1514 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
1515 }
1516
1517 else if (attribute == Attr_Last)
1518 {
1519 if (pa && pa->last)
1520 {
1521 gnu_result = pa->last;
1522 break;
1523 }
1524
1525 gnu_result
1526 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
1527 }
1528
1529 else /* attribute == Attr_Range_Length || attribute == Attr_Length */
1530 {
1531 if (pa && pa->length)
1532 {
1533 gnu_result = pa->length;
1534 break;
1535 }
1536 else
1537 {
1538 /* We used to compute the length as max (hb - lb + 1, 0),
1539 which could overflow for some cases of empty arrays, e.g.
1540 when lb == index_type'first. We now compute the length as
1541 (hb < lb) ? 0 : hb - lb + 1, which would only overflow in
1542 much rarer cases, for extremely large arrays we expect
1543 never to encounter in practice. In addition, the former
1544 computation required the use of potentially constraining
1545 signed arithmetic while the latter doesn't. Note that the
1546 comparison must be done in the original index base type,
1547 otherwise the conversion of either bound to gnu_compute_type
1548 may overflow. */
1549
1550 tree gnu_compute_type = get_base_type (gnu_result_type);
1551
1552 tree index_type
1553 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type));
1554 tree lb
1555 = convert (gnu_compute_type, TYPE_MIN_VALUE (index_type));
1556 tree hb
1557 = convert (gnu_compute_type, TYPE_MAX_VALUE (index_type));
1558
1559 gnu_result
1560 = build3
1561 (COND_EXPR, gnu_compute_type,
1562 build_binary_op (LT_EXPR, get_base_type (index_type),
1563 TYPE_MAX_VALUE (index_type),
1564 TYPE_MIN_VALUE (index_type)),
1565 convert (gnu_compute_type, integer_zero_node),
1566 build_binary_op
1567 (PLUS_EXPR, gnu_compute_type,
1568 build_binary_op (MINUS_EXPR, gnu_compute_type, hb, lb),
1569 convert (gnu_compute_type, integer_one_node)));
1570 }
1571 }
1572
1573 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
1574 handling. Note that these attributes could not have been used on
1575 an unconstrained array type. */
1576 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result,
1577 gnu_prefix);
1578
1579 /* Cache the expression we have just computed. Since we want to do it
1580 at runtime, we force the use of a SAVE_EXPR and let the gimplifier
1581 create the temporary. */
1582 if (pa)
1583 {
1584 gnu_result
1585 = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result);
1586 TREE_SIDE_EFFECTS (gnu_result) = 1;
1587 if (attribute == Attr_First)
1588 pa->first = gnu_result;
1589 else if (attribute == Attr_Last)
1590 pa->last = gnu_result;
1591 else
1592 pa->length = gnu_result;
1593 }
1594 break;
1595 }
1596
1597 case Attr_Bit_Position:
1598 case Attr_Position:
1599 case Attr_First_Bit:
1600 case Attr_Last_Bit:
1601 case Attr_Bit:
1602 {
1603 HOST_WIDE_INT bitsize;
1604 HOST_WIDE_INT bitpos;
1605 tree gnu_offset;
1606 tree gnu_field_bitpos;
1607 tree gnu_field_offset;
1608 tree gnu_inner;
1609 enum machine_mode mode;
1610 int unsignedp, volatilep;
1611
1612 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1613 gnu_prefix = remove_conversions (gnu_prefix, true);
1614 prefix_unused = true;
1615
1616 /* We can have 'Bit on any object, but if it isn't a COMPONENT_REF,
1617 the result is 0. Don't allow 'Bit on a bare component, though. */
1618 if (attribute == Attr_Bit
1619 && TREE_CODE (gnu_prefix) != COMPONENT_REF
1620 && TREE_CODE (gnu_prefix) != FIELD_DECL)
1621 {
1622 gnu_result = integer_zero_node;
1623 break;
1624 }
1625
1626 else
1627 gcc_assert (TREE_CODE (gnu_prefix) == COMPONENT_REF
1628 || (attribute == Attr_Bit_Position
1629 && TREE_CODE (gnu_prefix) == FIELD_DECL));
1630
1631 get_inner_reference (gnu_prefix, &bitsize, &bitpos, &gnu_offset,
1632 &mode, &unsignedp, &volatilep, false);
1633
1634 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
1635 {
1636 gnu_field_bitpos = bit_position (TREE_OPERAND (gnu_prefix, 1));
1637 gnu_field_offset = byte_position (TREE_OPERAND (gnu_prefix, 1));
1638
1639 for (gnu_inner = TREE_OPERAND (gnu_prefix, 0);
1640 TREE_CODE (gnu_inner) == COMPONENT_REF
1641 && DECL_INTERNAL_P (TREE_OPERAND (gnu_inner, 1));
1642 gnu_inner = TREE_OPERAND (gnu_inner, 0))
1643 {
1644 gnu_field_bitpos
1645 = size_binop (PLUS_EXPR, gnu_field_bitpos,
1646 bit_position (TREE_OPERAND (gnu_inner, 1)));
1647 gnu_field_offset
1648 = size_binop (PLUS_EXPR, gnu_field_offset,
1649 byte_position (TREE_OPERAND (gnu_inner, 1)));
1650 }
1651 }
1652 else if (TREE_CODE (gnu_prefix) == FIELD_DECL)
1653 {
1654 gnu_field_bitpos = bit_position (gnu_prefix);
1655 gnu_field_offset = byte_position (gnu_prefix);
1656 }
1657 else
1658 {
1659 gnu_field_bitpos = bitsize_zero_node;
1660 gnu_field_offset = size_zero_node;
1661 }
1662
1663 switch (attribute)
1664 {
1665 case Attr_Position:
1666 gnu_result = gnu_field_offset;
1667 break;
1668
1669 case Attr_First_Bit:
1670 case Attr_Bit:
1671 gnu_result = size_int (bitpos % BITS_PER_UNIT);
1672 break;
1673
1674 case Attr_Last_Bit:
1675 gnu_result = bitsize_int (bitpos % BITS_PER_UNIT);
1676 gnu_result = size_binop (PLUS_EXPR, gnu_result,
1677 TYPE_SIZE (TREE_TYPE (gnu_prefix)));
1678 gnu_result = size_binop (MINUS_EXPR, gnu_result,
1679 bitsize_one_node);
1680 break;
1681
1682 case Attr_Bit_Position:
1683 gnu_result = gnu_field_bitpos;
1684 break;
1685 }
1686
1687 /* If this has a PLACEHOLDER_EXPR, qualify it by the object
1688 we are handling. */
1689 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
1690 break;
1691 }
1692
1693 case Attr_Min:
1694 case Attr_Max:
1695 {
1696 tree gnu_lhs = gnat_to_gnu (First (Expressions (gnat_node)));
1697 tree gnu_rhs = gnat_to_gnu (Next (First (Expressions (gnat_node))));
1698
1699 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1700 gnu_result = build_binary_op (attribute == Attr_Min
1701 ? MIN_EXPR : MAX_EXPR,
1702 gnu_result_type, gnu_lhs, gnu_rhs);
1703 }
1704 break;
1705
1706 case Attr_Passed_By_Reference:
1707 gnu_result = size_int (default_pass_by_ref (gnu_type)
1708 || must_pass_by_ref (gnu_type));
1709 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1710 break;
1711
1712 case Attr_Component_Size:
1713 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
1714 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))
1715 == RECORD_TYPE)
1716 && (TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))))
1717 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
1718
1719 gnu_prefix = maybe_implicit_deref (gnu_prefix);
1720 gnu_type = TREE_TYPE (gnu_prefix);
1721
1722 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
1723 gnu_type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type))));
1724
1725 while (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
1726 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
1727 gnu_type = TREE_TYPE (gnu_type);
1728
1729 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
1730
1731 /* Note this size cannot be self-referential. */
1732 gnu_result = TYPE_SIZE (TREE_TYPE (gnu_type));
1733 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1734 prefix_unused = true;
1735 break;
1736
1737 case Attr_Null_Parameter:
1738 /* This is just a zero cast to the pointer type for
1739 our prefix and dereferenced. */
1740 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1741 gnu_result
1742 = build_unary_op (INDIRECT_REF, NULL_TREE,
1743 convert (build_pointer_type (gnu_result_type),
1744 integer_zero_node));
1745 TREE_PRIVATE (gnu_result) = 1;
1746 break;
1747
1748 case Attr_Mechanism_Code:
1749 {
1750 int code;
1751 Entity_Id gnat_obj = Entity (Prefix (gnat_node));
1752
1753 prefix_unused = true;
1754 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1755 if (Present (Expressions (gnat_node)))
1756 {
1757 int i = UI_To_Int (Intval (First (Expressions (gnat_node))));
1758
1759 for (gnat_obj = First_Formal (gnat_obj); i > 1;
1760 i--, gnat_obj = Next_Formal (gnat_obj))
1761 ;
1762 }
1763
1764 code = Mechanism (gnat_obj);
1765 if (code == Default)
1766 code = ((present_gnu_tree (gnat_obj)
1767 && (DECL_BY_REF_P (get_gnu_tree (gnat_obj))
1768 || ((TREE_CODE (get_gnu_tree (gnat_obj))
1769 == PARM_DECL)
1770 && (DECL_BY_COMPONENT_PTR_P
1771 (get_gnu_tree (gnat_obj))))))
1772 ? By_Reference : By_Copy);
1773 gnu_result = convert (gnu_result_type, size_int (- code));
1774 }
1775 break;
1776
1777 default:
1778 /* Say we have an unimplemented attribute. Then set the value to be
1779 returned to be a zero and hope that's something we can convert to the
1780 type of this attribute. */
1781 post_error ("unimplemented attribute", gnat_node);
1782 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1783 gnu_result = integer_zero_node;
1784 break;
1785 }
1786
1787 /* If this is an attribute where the prefix was unused, force a use of it if
1788 it has a side-effect. But don't do it if the prefix is just an entity
1789 name. However, if an access check is needed, we must do it. See second
1790 example in AARM 11.6(5.e). */
1791 if (prefix_unused && TREE_SIDE_EFFECTS (gnu_prefix)
1792 && !Is_Entity_Name (Prefix (gnat_node)))
1793 gnu_result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (gnu_result),
1794 gnu_prefix, gnu_result);
1795
1796 *gnu_result_type_p = gnu_result_type;
1797 return gnu_result;
1798 }
1799 \f
1800 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Case_Statement,
1801 to a GCC tree, which is returned. */
1802
1803 static tree
1804 Case_Statement_to_gnu (Node_Id gnat_node)
1805 {
1806 tree gnu_result;
1807 tree gnu_expr;
1808 Node_Id gnat_when;
1809
1810 gnu_expr = gnat_to_gnu (Expression (gnat_node));
1811 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
1812
1813 /* The range of values in a case statement is determined by the rules in
1814 RM 5.4(7-9). In almost all cases, this range is represented by the Etype
1815 of the expression. One exception arises in the case of a simple name that
1816 is parenthesized. This still has the Etype of the name, but since it is
1817 not a name, para 7 does not apply, and we need to go to the base type.
1818 This is the only case where parenthesization affects the dynamic
1819 semantics (i.e. the range of possible values at runtime that is covered
1820 by the others alternative.
1821
1822 Another exception is if the subtype of the expression is non-static. In
1823 that case, we also have to use the base type. */
1824 if (Paren_Count (Expression (gnat_node)) != 0
1825 || !Is_OK_Static_Subtype (Underlying_Type
1826 (Etype (Expression (gnat_node)))))
1827 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
1828
1829 /* We build a SWITCH_EXPR that contains the code with interspersed
1830 CASE_LABEL_EXPRs for each label. */
1831
1832 push_stack (&gnu_switch_label_stack, NULL_TREE, create_artificial_label ());
1833 start_stmt_group ();
1834 for (gnat_when = First_Non_Pragma (Alternatives (gnat_node));
1835 Present (gnat_when);
1836 gnat_when = Next_Non_Pragma (gnat_when))
1837 {
1838 Node_Id gnat_choice;
1839 int choices_added = 0;
1840
1841 /* First compile all the different case choices for the current WHEN
1842 alternative. */
1843 for (gnat_choice = First (Discrete_Choices (gnat_when));
1844 Present (gnat_choice); gnat_choice = Next (gnat_choice))
1845 {
1846 tree gnu_low = NULL_TREE, gnu_high = NULL_TREE;
1847
1848 switch (Nkind (gnat_choice))
1849 {
1850 case N_Range:
1851 gnu_low = gnat_to_gnu (Low_Bound (gnat_choice));
1852 gnu_high = gnat_to_gnu (High_Bound (gnat_choice));
1853 break;
1854
1855 case N_Subtype_Indication:
1856 gnu_low = gnat_to_gnu (Low_Bound (Range_Expression
1857 (Constraint (gnat_choice))));
1858 gnu_high = gnat_to_gnu (High_Bound (Range_Expression
1859 (Constraint (gnat_choice))));
1860 break;
1861
1862 case N_Identifier:
1863 case N_Expanded_Name:
1864 /* This represents either a subtype range or a static value of
1865 some kind; Ekind says which. */
1866 if (IN (Ekind (Entity (gnat_choice)), Type_Kind))
1867 {
1868 tree gnu_type = get_unpadded_type (Entity (gnat_choice));
1869
1870 gnu_low = fold (TYPE_MIN_VALUE (gnu_type));
1871 gnu_high = fold (TYPE_MAX_VALUE (gnu_type));
1872 break;
1873 }
1874
1875 /* ... fall through ... */
1876
1877 case N_Character_Literal:
1878 case N_Integer_Literal:
1879 gnu_low = gnat_to_gnu (gnat_choice);
1880 break;
1881
1882 case N_Others_Choice:
1883 break;
1884
1885 default:
1886 gcc_unreachable ();
1887 }
1888
1889 /* If the case value is a subtype that raises Constraint_Error at
1890 run-time because of a wrong bound, then gnu_low or gnu_high is
1891 not translated into an INTEGER_CST. In such a case, we need
1892 to ensure that the when statement is not added in the tree,
1893 otherwise it will crash the gimplifier. */
1894 if ((!gnu_low || TREE_CODE (gnu_low) == INTEGER_CST)
1895 && (!gnu_high || TREE_CODE (gnu_high) == INTEGER_CST))
1896 {
1897 add_stmt_with_node (build3 (CASE_LABEL_EXPR, void_type_node,
1898 gnu_low, gnu_high,
1899 create_artificial_label ()),
1900 gnat_choice);
1901 choices_added++;
1902 }
1903 }
1904
1905 /* Push a binding level here in case variables are declared as we want
1906 them to be local to this set of statements instead of to the block
1907 containing the Case statement. */
1908 if (choices_added > 0)
1909 {
1910 add_stmt (build_stmt_group (Statements (gnat_when), true));
1911 add_stmt (build1 (GOTO_EXPR, void_type_node,
1912 TREE_VALUE (gnu_switch_label_stack)));
1913 }
1914 }
1915
1916 /* Now emit a definition of the label all the cases branched to. */
1917 add_stmt (build1 (LABEL_EXPR, void_type_node,
1918 TREE_VALUE (gnu_switch_label_stack)));
1919 gnu_result = build3 (SWITCH_EXPR, TREE_TYPE (gnu_expr), gnu_expr,
1920 end_stmt_group (), NULL_TREE);
1921 pop_stack (&gnu_switch_label_stack);
1922
1923 return gnu_result;
1924 }
1925 \f
1926 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Loop_Statement,
1927 to a GCC tree, which is returned. */
1928
1929 static tree
1930 Loop_Statement_to_gnu (Node_Id gnat_node)
1931 {
1932 /* ??? It would be nice to use "build" here, but there's no build5. */
1933 tree gnu_loop_stmt = build_nt (LOOP_STMT, NULL_TREE, NULL_TREE,
1934 NULL_TREE, NULL_TREE, NULL_TREE);
1935 tree gnu_loop_var = NULL_TREE;
1936 Node_Id gnat_iter_scheme = Iteration_Scheme (gnat_node);
1937 tree gnu_cond_expr = NULL_TREE;
1938 tree gnu_result;
1939
1940 TREE_TYPE (gnu_loop_stmt) = void_type_node;
1941 TREE_SIDE_EFFECTS (gnu_loop_stmt) = 1;
1942 LOOP_STMT_LABEL (gnu_loop_stmt) = create_artificial_label ();
1943 set_expr_location_from_node (gnu_loop_stmt, gnat_node);
1944 Sloc_to_locus (Sloc (End_Label (gnat_node)),
1945 &DECL_SOURCE_LOCATION (LOOP_STMT_LABEL (gnu_loop_stmt)));
1946
1947 /* Save the end label of this LOOP_STMT in a stack so that the corresponding
1948 N_Exit_Statement can find it. */
1949 push_stack (&gnu_loop_label_stack, NULL_TREE,
1950 LOOP_STMT_LABEL (gnu_loop_stmt));
1951
1952 /* Set the condition under which the loop must keep going.
1953 For the case "LOOP .... END LOOP;" the condition is always true. */
1954 if (No (gnat_iter_scheme))
1955 ;
1956
1957 /* For the case "WHILE condition LOOP ..... END LOOP;" it's immediate. */
1958 else if (Present (Condition (gnat_iter_scheme)))
1959 LOOP_STMT_TOP_COND (gnu_loop_stmt)
1960 = gnat_to_gnu (Condition (gnat_iter_scheme));
1961
1962 /* Otherwise we have an iteration scheme and the condition is given by
1963 the bounds of the subtype of the iteration variable. */
1964 else
1965 {
1966 Node_Id gnat_loop_spec = Loop_Parameter_Specification (gnat_iter_scheme);
1967 Entity_Id gnat_loop_var = Defining_Entity (gnat_loop_spec);
1968 Entity_Id gnat_type = Etype (gnat_loop_var);
1969 tree gnu_type = get_unpadded_type (gnat_type);
1970 tree gnu_low = TYPE_MIN_VALUE (gnu_type);
1971 tree gnu_high = TYPE_MAX_VALUE (gnu_type);
1972 tree gnu_first, gnu_last, gnu_limit;
1973 enum tree_code update_code, end_code;
1974 tree gnu_base_type = get_base_type (gnu_type);
1975
1976 /* We must disable modulo reduction for the loop variable, if any,
1977 in order for the loop comparison to be effective. */
1978 if (Reverse_Present (gnat_loop_spec))
1979 {
1980 gnu_first = gnu_high;
1981 gnu_last = gnu_low;
1982 update_code = MINUS_NOMOD_EXPR;
1983 end_code = GE_EXPR;
1984 gnu_limit = TYPE_MIN_VALUE (gnu_base_type);
1985 }
1986 else
1987 {
1988 gnu_first = gnu_low;
1989 gnu_last = gnu_high;
1990 update_code = PLUS_NOMOD_EXPR;
1991 end_code = LE_EXPR;
1992 gnu_limit = TYPE_MAX_VALUE (gnu_base_type);
1993 }
1994
1995 /* We know the loop variable will not overflow if GNU_LAST is a constant
1996 and is not equal to GNU_LIMIT. If it might overflow, we have to move
1997 the limit test to the end of the loop. In that case, we have to test
1998 for an empty loop outside the loop. */
1999 if (TREE_CODE (gnu_last) != INTEGER_CST
2000 || TREE_CODE (gnu_limit) != INTEGER_CST
2001 || tree_int_cst_equal (gnu_last, gnu_limit))
2002 {
2003 gnu_cond_expr
2004 = build3 (COND_EXPR, void_type_node,
2005 build_binary_op (LE_EXPR, integer_type_node,
2006 gnu_low, gnu_high),
2007 NULL_TREE, alloc_stmt_list ());
2008 set_expr_location_from_node (gnu_cond_expr, gnat_loop_spec);
2009 }
2010
2011 /* Open a new nesting level that will surround the loop to declare the
2012 loop index variable. */
2013 start_stmt_group ();
2014 gnat_pushlevel ();
2015
2016 /* Declare the loop index and set it to its initial value. */
2017 gnu_loop_var = gnat_to_gnu_entity (gnat_loop_var, gnu_first, 1);
2018 if (DECL_BY_REF_P (gnu_loop_var))
2019 gnu_loop_var = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_loop_var);
2020
2021 /* The loop variable might be a padded type, so use `convert' to get a
2022 reference to the inner variable if so. */
2023 gnu_loop_var = convert (get_base_type (gnu_type), gnu_loop_var);
2024
2025 /* Set either the top or bottom exit condition as appropriate depending
2026 on whether or not we know an overflow cannot occur. */
2027 if (gnu_cond_expr)
2028 LOOP_STMT_BOT_COND (gnu_loop_stmt)
2029 = build_binary_op (NE_EXPR, integer_type_node,
2030 gnu_loop_var, gnu_last);
2031 else
2032 LOOP_STMT_TOP_COND (gnu_loop_stmt)
2033 = build_binary_op (end_code, integer_type_node,
2034 gnu_loop_var, gnu_last);
2035
2036 LOOP_STMT_UPDATE (gnu_loop_stmt)
2037 = build_binary_op (MODIFY_EXPR, NULL_TREE,
2038 gnu_loop_var,
2039 build_binary_op (update_code,
2040 TREE_TYPE (gnu_loop_var),
2041 gnu_loop_var,
2042 convert (TREE_TYPE (gnu_loop_var),
2043 integer_one_node)));
2044 set_expr_location_from_node (LOOP_STMT_UPDATE (gnu_loop_stmt),
2045 gnat_iter_scheme);
2046 }
2047
2048 /* If the loop was named, have the name point to this loop. In this case,
2049 the association is not a ..._DECL node, but the end label from this
2050 LOOP_STMT. */
2051 if (Present (Identifier (gnat_node)))
2052 save_gnu_tree (Entity (Identifier (gnat_node)),
2053 LOOP_STMT_LABEL (gnu_loop_stmt), true);
2054
2055 /* Make the loop body into its own block, so any allocated storage will be
2056 released every iteration. This is needed for stack allocation. */
2057 LOOP_STMT_BODY (gnu_loop_stmt)
2058 = build_stmt_group (Statements (gnat_node), true);
2059
2060 /* If we declared a variable, then we are in a statement group for that
2061 declaration. Add the LOOP_STMT to it and make that the "loop". */
2062 if (gnu_loop_var)
2063 {
2064 add_stmt (gnu_loop_stmt);
2065 gnat_poplevel ();
2066 gnu_loop_stmt = end_stmt_group ();
2067 }
2068
2069 /* If we have an outer COND_EXPR, that's our result and this loop is its
2070 "true" statement. Otherwise, the result is the LOOP_STMT. */
2071 if (gnu_cond_expr)
2072 {
2073 COND_EXPR_THEN (gnu_cond_expr) = gnu_loop_stmt;
2074 gnu_result = gnu_cond_expr;
2075 recalculate_side_effects (gnu_cond_expr);
2076 }
2077 else
2078 gnu_result = gnu_loop_stmt;
2079
2080 pop_stack (&gnu_loop_label_stack);
2081
2082 return gnu_result;
2083 }
2084 \f
2085 /* Emit statements to establish __gnat_handle_vms_condition as a VMS condition
2086 handler for the current function. */
2087
2088 /* This is implemented by issuing a call to the appropriate VMS specific
2089 builtin. To avoid having VMS specific sections in the global gigi decls
2090 array, we maintain the decls of interest here. We can't declare them
2091 inside the function because we must mark them never to be GC'd, which we
2092 can only do at the global level. */
2093
2094 static GTY(()) tree vms_builtin_establish_handler_decl = NULL_TREE;
2095 static GTY(()) tree gnat_vms_condition_handler_decl = NULL_TREE;
2096
2097 static void
2098 establish_gnat_vms_condition_handler (void)
2099 {
2100 tree establish_stmt;
2101
2102 /* Elaborate the required decls on the first call. Check on the decl for
2103 the gnat condition handler to decide, as this is one we create so we are
2104 sure that it will be non null on subsequent calls. The builtin decl is
2105 looked up so remains null on targets where it is not implemented yet. */
2106 if (gnat_vms_condition_handler_decl == NULL_TREE)
2107 {
2108 vms_builtin_establish_handler_decl
2109 = builtin_decl_for
2110 (get_identifier ("__builtin_establish_vms_condition_handler"));
2111
2112 gnat_vms_condition_handler_decl
2113 = create_subprog_decl (get_identifier ("__gnat_handle_vms_condition"),
2114 NULL_TREE,
2115 build_function_type_list (integer_type_node,
2116 ptr_void_type_node,
2117 ptr_void_type_node,
2118 NULL_TREE),
2119 NULL_TREE, 0, 1, 1, 0, Empty);
2120
2121 /* ??? DECL_CONTEXT shouldn't have been set because of DECL_EXTERNAL. */
2122 DECL_CONTEXT (gnat_vms_condition_handler_decl) = NULL_TREE;
2123 }
2124
2125 /* Do nothing if the establish builtin is not available, which might happen
2126 on targets where the facility is not implemented. */
2127 if (vms_builtin_establish_handler_decl == NULL_TREE)
2128 return;
2129
2130 establish_stmt
2131 = build_call_1_expr (vms_builtin_establish_handler_decl,
2132 build_unary_op
2133 (ADDR_EXPR, NULL_TREE,
2134 gnat_vms_condition_handler_decl));
2135
2136 add_stmt (establish_stmt);
2137 }
2138 \f
2139 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Subprogram_Body. We
2140 don't return anything. */
2141
2142 static void
2143 Subprogram_Body_to_gnu (Node_Id gnat_node)
2144 {
2145 /* Defining identifier of a parameter to the subprogram. */
2146 Entity_Id gnat_param;
2147 /* The defining identifier for the subprogram body. Note that if a
2148 specification has appeared before for this body, then the identifier
2149 occurring in that specification will also be a defining identifier and all
2150 the calls to this subprogram will point to that specification. */
2151 Entity_Id gnat_subprog_id
2152 = (Present (Corresponding_Spec (gnat_node))
2153 ? Corresponding_Spec (gnat_node) : Defining_Entity (gnat_node));
2154 /* The FUNCTION_DECL node corresponding to the subprogram spec. */
2155 tree gnu_subprog_decl;
2156 /* The FUNCTION_TYPE node corresponding to the subprogram spec. */
2157 tree gnu_subprog_type;
2158 tree gnu_cico_list;
2159 tree gnu_result;
2160 VEC(parm_attr,gc) *cache;
2161
2162 /* If this is a generic object or if it has been eliminated,
2163 ignore it. */
2164 if (Ekind (gnat_subprog_id) == E_Generic_Procedure
2165 || Ekind (gnat_subprog_id) == E_Generic_Function
2166 || Is_Eliminated (gnat_subprog_id))
2167 return;
2168
2169 /* If this subprogram acts as its own spec, define it. Otherwise, just get
2170 the already-elaborated tree node. However, if this subprogram had its
2171 elaboration deferred, we will already have made a tree node for it. So
2172 treat it as not being defined in that case. Such a subprogram cannot
2173 have an address clause or a freeze node, so this test is safe, though it
2174 does disable some otherwise-useful error checking. */
2175 gnu_subprog_decl
2176 = gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE,
2177 Acts_As_Spec (gnat_node)
2178 && !present_gnu_tree (gnat_subprog_id));
2179
2180 gnu_subprog_type = TREE_TYPE (gnu_subprog_decl);
2181
2182 /* Propagate the debug mode. */
2183 if (!Needs_Debug_Info (gnat_subprog_id))
2184 DECL_IGNORED_P (gnu_subprog_decl) = 1;
2185
2186 /* Set the line number in the decl to correspond to that of the body so that
2187 the line number notes are written correctly. */
2188 Sloc_to_locus (Sloc (gnat_node), &DECL_SOURCE_LOCATION (gnu_subprog_decl));
2189
2190 /* Initialize the information structure for the function. */
2191 allocate_struct_function (gnu_subprog_decl, false);
2192 DECL_STRUCT_FUNCTION (gnu_subprog_decl)->language
2193 = GGC_CNEW (struct language_function);
2194
2195 begin_subprog_body (gnu_subprog_decl);
2196 gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
2197
2198 /* If there are Out parameters, we need to ensure that the return statement
2199 properly copies them out. We do this by making a new block and converting
2200 any inner return into a goto to a label at the end of the block. */
2201 push_stack (&gnu_return_label_stack, NULL_TREE,
2202 gnu_cico_list ? create_artificial_label () : NULL_TREE);
2203
2204 /* Get a tree corresponding to the code for the subprogram. */
2205 start_stmt_group ();
2206 gnat_pushlevel ();
2207
2208 /* See if there are any parameters for which we don't yet have GCC entities.
2209 These must be for Out parameters for which we will be making VAR_DECL
2210 nodes here. Fill them in to TYPE_CI_CO_LIST, which must contain the empty
2211 entry as well. We can match up the entries because TYPE_CI_CO_LIST is in
2212 the order of the parameters. */
2213 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
2214 Present (gnat_param);
2215 gnat_param = Next_Formal_With_Extras (gnat_param))
2216 if (!present_gnu_tree (gnat_param))
2217 {
2218 /* Skip any entries that have been already filled in; they must
2219 correspond to In Out parameters. */
2220 for (; gnu_cico_list && TREE_VALUE (gnu_cico_list);
2221 gnu_cico_list = TREE_CHAIN (gnu_cico_list))
2222 ;
2223
2224 /* Do any needed references for padded types. */
2225 TREE_VALUE (gnu_cico_list)
2226 = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_list)),
2227 gnat_to_gnu_entity (gnat_param, NULL_TREE, 1));
2228 }
2229
2230 /* On VMS, establish our condition handler to possibly turn a condition into
2231 the corresponding exception if the subprogram has a foreign convention or
2232 is exported.
2233
2234 To ensure proper execution of local finalizations on condition instances,
2235 we must turn a condition into the corresponding exception even if there
2236 is no applicable Ada handler, and need at least one condition handler per
2237 possible call chain involving GNAT code. OTOH, establishing the handler
2238 has a cost so we want to minimize the number of subprograms into which
2239 this happens. The foreign or exported condition is expected to satisfy
2240 all the constraints. */
2241 if (TARGET_ABI_OPEN_VMS
2242 && (Has_Foreign_Convention (gnat_subprog_id)
2243 || Is_Exported (gnat_subprog_id)))
2244 establish_gnat_vms_condition_handler ();
2245
2246 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
2247
2248 /* Generate the code of the subprogram itself. A return statement will be
2249 present and any Out parameters will be handled there. */
2250 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
2251 gnat_poplevel ();
2252 gnu_result = end_stmt_group ();
2253
2254 /* If we populated the parameter attributes cache, we need to make sure
2255 that the cached expressions are evaluated on all possible paths. */
2256 cache = DECL_STRUCT_FUNCTION (gnu_subprog_decl)->language->parm_attr_cache;
2257 if (cache)
2258 {
2259 struct parm_attr *pa;
2260 int i;
2261
2262 start_stmt_group ();
2263
2264 for (i = 0; VEC_iterate (parm_attr, cache, i, pa); i++)
2265 {
2266 if (pa->first)
2267 add_stmt_with_node (pa->first, gnat_node);
2268 if (pa->last)
2269 add_stmt_with_node (pa->last, gnat_node);
2270 if (pa->length)
2271 add_stmt_with_node (pa->length, gnat_node);
2272 }
2273
2274 add_stmt (gnu_result);
2275 gnu_result = end_stmt_group ();
2276 }
2277
2278 /* If we made a special return label, we need to make a block that contains
2279 the definition of that label and the copying to the return value. That
2280 block first contains the function, then the label and copy statement. */
2281 if (TREE_VALUE (gnu_return_label_stack))
2282 {
2283 tree gnu_retval;
2284
2285 start_stmt_group ();
2286 gnat_pushlevel ();
2287 add_stmt (gnu_result);
2288 add_stmt (build1 (LABEL_EXPR, void_type_node,
2289 TREE_VALUE (gnu_return_label_stack)));
2290
2291 gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
2292 if (list_length (gnu_cico_list) == 1)
2293 gnu_retval = TREE_VALUE (gnu_cico_list);
2294 else
2295 gnu_retval = gnat_build_constructor (TREE_TYPE (gnu_subprog_type),
2296 gnu_cico_list);
2297
2298 if (DECL_P (gnu_retval) && DECL_BY_REF_P (gnu_retval))
2299 gnu_retval = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_retval);
2300
2301 add_stmt_with_node
2302 (build_return_expr (DECL_RESULT (gnu_subprog_decl), gnu_retval),
2303 End_Label (Handled_Statement_Sequence (gnat_node)));
2304 gnat_poplevel ();
2305 gnu_result = end_stmt_group ();
2306 }
2307
2308 pop_stack (&gnu_return_label_stack);
2309
2310 /* Set the end location. */
2311 Sloc_to_locus
2312 ((Present (End_Label (Handled_Statement_Sequence (gnat_node)))
2313 ? Sloc (End_Label (Handled_Statement_Sequence (gnat_node)))
2314 : Sloc (gnat_node)),
2315 &DECL_STRUCT_FUNCTION (gnu_subprog_decl)->function_end_locus);
2316
2317 end_subprog_body (gnu_result, false);
2318
2319 /* Disconnect the trees for parameters that we made variables for from the
2320 GNAT entities since these are unusable after we end the function. */
2321 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
2322 Present (gnat_param);
2323 gnat_param = Next_Formal_With_Extras (gnat_param))
2324 if (TREE_CODE (get_gnu_tree (gnat_param)) == VAR_DECL)
2325 save_gnu_tree (gnat_param, NULL_TREE, false);
2326
2327 if (DECL_FUNCTION_STUB (gnu_subprog_decl))
2328 build_function_stub (gnu_subprog_decl, gnat_subprog_id);
2329
2330 mark_out_of_scope (Defining_Unit_Name (Specification (gnat_node)));
2331 }
2332 \f
2333 /* Subroutine of gnat_to_gnu to translate gnat_node, either an N_Function_Call
2334 or an N_Procedure_Call_Statement, to a GCC tree, which is returned.
2335 GNU_RESULT_TYPE_P is a pointer to where we should place the result type.
2336 If GNU_TARGET is non-null, this must be a function call and the result
2337 of the call is to be placed into that object. */
2338
2339 static tree
2340 call_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, tree gnu_target)
2341 {
2342 tree gnu_result;
2343 /* The GCC node corresponding to the GNAT subprogram name. This can either
2344 be a FUNCTION_DECL node if we are dealing with a standard subprogram call,
2345 or an indirect reference expression (an INDIRECT_REF node) pointing to a
2346 subprogram. */
2347 tree gnu_subprog_node = gnat_to_gnu (Name (gnat_node));
2348 /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */
2349 tree gnu_subprog_type = TREE_TYPE (gnu_subprog_node);
2350 tree gnu_subprog_addr = build_unary_op (ADDR_EXPR, NULL_TREE,
2351 gnu_subprog_node);
2352 Entity_Id gnat_formal;
2353 Node_Id gnat_actual;
2354 tree gnu_actual_list = NULL_TREE;
2355 tree gnu_name_list = NULL_TREE;
2356 tree gnu_before_list = NULL_TREE;
2357 tree gnu_after_list = NULL_TREE;
2358 tree gnu_subprog_call;
2359
2360 check_for_eliminated_entity (Name (gnat_node));
2361
2362 gcc_assert (TREE_CODE (gnu_subprog_type) == FUNCTION_TYPE);
2363
2364 /* If we are calling a stubbed function, make this into a raise of
2365 Program_Error. Elaborate all our args first. */
2366 if (TREE_CODE (gnu_subprog_node) == FUNCTION_DECL
2367 && DECL_STUBBED_P (gnu_subprog_node))
2368 {
2369 for (gnat_actual = First_Actual (gnat_node);
2370 Present (gnat_actual);
2371 gnat_actual = Next_Actual (gnat_actual))
2372 add_stmt (gnat_to_gnu (gnat_actual));
2373
2374 {
2375 tree call_expr
2376 = build_call_raise (PE_Stubbed_Subprogram_Called, gnat_node,
2377 N_Raise_Program_Error);
2378
2379 if (Nkind (gnat_node) == N_Function_Call && !gnu_target)
2380 {
2381 *gnu_result_type_p = TREE_TYPE (gnu_subprog_type);
2382 return build1 (NULL_EXPR, *gnu_result_type_p, call_expr);
2383 }
2384 else
2385 return call_expr;
2386 }
2387 }
2388
2389 /* If we are calling by supplying a pointer to a target, set up that
2390 pointer as the first argument. Use GNU_TARGET if one was passed;
2391 otherwise, make a target by building a variable of the maximum size
2392 of the type. */
2393 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type))
2394 {
2395 tree gnu_real_ret_type
2396 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (gnu_subprog_type)));
2397
2398 if (!gnu_target)
2399 {
2400 tree gnu_obj_type
2401 = maybe_pad_type (gnu_real_ret_type,
2402 max_size (TYPE_SIZE (gnu_real_ret_type), true),
2403 0, Etype (Name (gnat_node)), "PAD", false,
2404 false, false);
2405
2406 /* ??? We may be about to create a static temporary if we happen to
2407 be at the global binding level. That's a regression from what
2408 the 3.x back-end would generate in the same situation, but we
2409 don't have a mechanism in Gigi for creating automatic variables
2410 in the elaboration routines. */
2411 gnu_target
2412 = create_var_decl (create_tmp_var_name ("LR"), NULL, gnu_obj_type,
2413 NULL, false, false, false, false, NULL,
2414 gnat_node);
2415 }
2416
2417 gnu_actual_list
2418 = tree_cons (NULL_TREE,
2419 build_unary_op (ADDR_EXPR, NULL_TREE,
2420 unchecked_convert (gnu_real_ret_type,
2421 gnu_target,
2422 false)),
2423 NULL_TREE);
2424
2425 }
2426
2427 /* The only way we can be making a call via an access type is if Name is an
2428 explicit dereference. In that case, get the list of formal args from the
2429 type the access type is pointing to. Otherwise, get the formals from
2430 entity being called. */
2431 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
2432 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
2433 else if (Nkind (Name (gnat_node)) == N_Attribute_Reference)
2434 /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */
2435 gnat_formal = 0;
2436 else
2437 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
2438
2439 /* Create the list of the actual parameters as GCC expects it, namely a chain
2440 of TREE_LIST nodes in which the TREE_VALUE field of each node is a
2441 parameter-expression and the TREE_PURPOSE field is null. Skip Out
2442 parameters not passed by reference and don't need to be copied in. */
2443 for (gnat_actual = First_Actual (gnat_node);
2444 Present (gnat_actual);
2445 gnat_formal = Next_Formal_With_Extras (gnat_formal),
2446 gnat_actual = Next_Actual (gnat_actual))
2447 {
2448 tree gnu_formal
2449 = (present_gnu_tree (gnat_formal)
2450 ? get_gnu_tree (gnat_formal) : NULL_TREE);
2451 tree gnu_formal_type = gnat_to_gnu_type (Etype (gnat_formal));
2452 /* We must suppress conversions that can cause the creation of a
2453 temporary in the Out or In Out case because we need the real
2454 object in this case, either to pass its address if it's passed
2455 by reference or as target of the back copy done after the call
2456 if it uses the copy-in copy-out mechanism. We do it in the In
2457 case too, except for an unchecked conversion because it alone
2458 can cause the actual to be misaligned and the addressability
2459 test is applied to the real object. */
2460 bool suppress_type_conversion
2461 = ((Nkind (gnat_actual) == N_Unchecked_Type_Conversion
2462 && Ekind (gnat_formal) != E_In_Parameter)
2463 || (Nkind (gnat_actual) == N_Type_Conversion
2464 && Is_Composite_Type (Underlying_Type (Etype (gnat_formal)))));
2465 Node_Id gnat_name = (suppress_type_conversion
2466 ? Expression (gnat_actual) : gnat_actual);
2467 tree gnu_name = gnat_to_gnu (gnat_name), gnu_name_type;
2468 tree gnu_actual;
2469
2470 /* If it's possible we may need to use this expression twice, make sure
2471 that any side-effects are handled via SAVE_EXPRs. Likewise if we need
2472 to force side-effects before the call.
2473 ??? This is more conservative than we need since we don't need to do
2474 this for pass-by-ref with no conversion. */
2475 if (Ekind (gnat_formal) != E_In_Parameter)
2476 gnu_name = gnat_stabilize_reference (gnu_name, true);
2477
2478 /* If we are passing a non-addressable parameter by reference, pass the
2479 address of a copy. In the Out or In Out case, set up to copy back
2480 out after the call. */
2481 if (gnu_formal
2482 && (DECL_BY_REF_P (gnu_formal)
2483 || (TREE_CODE (gnu_formal) == PARM_DECL
2484 && (DECL_BY_COMPONENT_PTR_P (gnu_formal)
2485 || (DECL_BY_DESCRIPTOR_P (gnu_formal)))))
2486 && (gnu_name_type = gnat_to_gnu_type (Etype (gnat_name)))
2487 && !addressable_p (gnu_name, gnu_name_type))
2488 {
2489 tree gnu_copy = gnu_name, gnu_temp;
2490
2491 /* If the type is by_reference, a copy is not allowed. */
2492 if (Is_By_Reference_Type (Etype (gnat_formal)))
2493 post_error
2494 ("misaligned actual cannot be passed by reference", gnat_actual);
2495
2496 /* For users of Starlet we issue a warning because the
2497 interface apparently assumes that by-ref parameters
2498 outlive the procedure invocation. The code still
2499 will not work as intended, but we cannot do much
2500 better since other low-level parts of the back-end
2501 would allocate temporaries at will because of the
2502 misalignment if we did not do so here. */
2503 else if (Is_Valued_Procedure (Entity (Name (gnat_node))))
2504 {
2505 post_error
2506 ("?possible violation of implicit assumption", gnat_actual);
2507 post_error_ne
2508 ("?made by pragma Import_Valued_Procedure on &", gnat_actual,
2509 Entity (Name (gnat_node)));
2510 post_error_ne ("?because of misalignment of &", gnat_actual,
2511 gnat_formal);
2512 }
2513
2514 /* If the actual type of the object is already the nominal type,
2515 we have nothing to do, except if the size is self-referential
2516 in which case we'll remove the unpadding below. */
2517 if (TREE_TYPE (gnu_name) == gnu_name_type
2518 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_name_type)))
2519 ;
2520
2521 /* Otherwise remove unpadding from the object and reset the copy. */
2522 else if (TREE_CODE (gnu_name) == COMPONENT_REF
2523 && ((TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_name, 0)))
2524 == RECORD_TYPE)
2525 && (TYPE_IS_PADDING_P
2526 (TREE_TYPE (TREE_OPERAND (gnu_name, 0))))))
2527 gnu_name = gnu_copy = TREE_OPERAND (gnu_name, 0);
2528
2529 /* Otherwise convert to the nominal type of the object if it's
2530 a record type. There are several cases in which we need to
2531 make the temporary using this type instead of the actual type
2532 of the object if they are distinct, because the expectations
2533 of the callee would otherwise not be met:
2534 - if it's a justified modular type,
2535 - if the actual type is a smaller packable version of it. */
2536 else if (TREE_CODE (gnu_name_type) == RECORD_TYPE
2537 && (TYPE_JUSTIFIED_MODULAR_P (gnu_name_type)
2538 || smaller_packable_type_p (TREE_TYPE (gnu_name),
2539 gnu_name_type)))
2540 gnu_name = convert (gnu_name_type, gnu_name);
2541
2542 /* Make a SAVE_EXPR to both properly account for potential side
2543 effects and handle the creation of a temporary copy. Special
2544 code in gnat_gimplify_expr ensures that the same temporary is
2545 used as the object and copied back after the call if needed. */
2546 gnu_name = build1 (SAVE_EXPR, TREE_TYPE (gnu_name), gnu_name);
2547 TREE_SIDE_EFFECTS (gnu_name) = 1;
2548
2549 /* Set up to move the copy back to the original. */
2550 if (Ekind (gnat_formal) != E_In_Parameter)
2551 {
2552 gnu_temp = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_copy,
2553 gnu_name);
2554 set_expr_location_from_node (gnu_temp, gnat_node);
2555 append_to_statement_list (gnu_temp, &gnu_after_list);
2556 }
2557 }
2558
2559 /* Start from the real object and build the actual. */
2560 gnu_actual = gnu_name;
2561
2562 /* If this was a procedure call, we may not have removed any padding.
2563 So do it here for the part we will use as an input, if any. */
2564 if (Ekind (gnat_formal) != E_Out_Parameter
2565 && TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
2566 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
2567 gnu_actual = convert (get_unpadded_type (Etype (gnat_actual)),
2568 gnu_actual);
2569
2570 /* Do any needed conversions for the actual and make sure that it is
2571 in range of the formal's type. */
2572 if (suppress_type_conversion)
2573 {
2574 /* Put back the conversion we suppressed above in the computation
2575 of the real object. Note that we treat a conversion between
2576 aggregate types as if it is an unchecked conversion here. */
2577 gnu_actual
2578 = unchecked_convert (gnat_to_gnu_type (Etype (gnat_actual)),
2579 gnu_actual,
2580 (Nkind (gnat_actual)
2581 == N_Unchecked_Type_Conversion)
2582 && No_Truncation (gnat_actual));
2583
2584 if (Ekind (gnat_formal) != E_Out_Parameter
2585 && Do_Range_Check (gnat_actual))
2586 gnu_actual = emit_range_check (gnu_actual, Etype (gnat_formal),
2587 gnat_actual);
2588 }
2589 else
2590 {
2591 if (Ekind (gnat_formal) != E_Out_Parameter
2592 && Do_Range_Check (gnat_actual))
2593 gnu_actual = emit_range_check (gnu_actual, Etype (gnat_formal),
2594 gnat_actual);
2595
2596 /* We may have suppressed a conversion to the Etype of the actual
2597 since the parent is a procedure call. So put it back here.
2598 ??? We use the reverse order compared to the case above because
2599 of an awkward interaction with the check and actually don't put
2600 back the conversion at all if a check is emitted. This is also
2601 done for the conversion to the formal's type just below. */
2602 if (TREE_CODE (gnu_actual) != SAVE_EXPR)
2603 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
2604 gnu_actual);
2605 }
2606
2607 if (TREE_CODE (gnu_actual) != SAVE_EXPR)
2608 gnu_actual = convert (gnu_formal_type, gnu_actual);
2609
2610 /* Unless this is an In parameter, we must remove any justified modular
2611 building from GNU_NAME to get an lvalue. */
2612 if (Ekind (gnat_formal) != E_In_Parameter
2613 && TREE_CODE (gnu_name) == CONSTRUCTOR
2614 && TREE_CODE (TREE_TYPE (gnu_name)) == RECORD_TYPE
2615 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (gnu_name)))
2616 gnu_name = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_name))),
2617 gnu_name);
2618
2619 /* If we have not saved a GCC object for the formal, it means it is an
2620 Out parameter not passed by reference and that does not need to be
2621 copied in. Otherwise, look at the PARM_DECL to see if it is passed by
2622 reference. */
2623 if (gnu_formal
2624 && TREE_CODE (gnu_formal) == PARM_DECL
2625 && DECL_BY_REF_P (gnu_formal))
2626 {
2627 if (Ekind (gnat_formal) != E_In_Parameter)
2628 {
2629 /* In Out or Out parameters passed by reference don't use the
2630 copy-in copy-out mechanism so the address of the real object
2631 must be passed to the function. */
2632 gnu_actual = gnu_name;
2633
2634 /* If we have a padded type, be sure we've removed padding. */
2635 if (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
2636 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual))
2637 && TREE_CODE (gnu_actual) != SAVE_EXPR)
2638 gnu_actual = convert (get_unpadded_type (Etype (gnat_actual)),
2639 gnu_actual);
2640
2641 /* If we have the constructed subtype of an aliased object
2642 with an unconstrained nominal subtype, the type of the
2643 actual includes the template, although it is formally
2644 constrained. So we need to convert it back to the real
2645 constructed subtype to retrieve the constrained part
2646 and takes its address. */
2647 if (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
2648 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual))
2649 && TREE_CODE (gnu_actual) != SAVE_EXPR
2650 && Is_Constr_Subt_For_UN_Aliased (Etype (gnat_actual))
2651 && Is_Array_Type (Etype (gnat_actual)))
2652 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
2653 gnu_actual);
2654 }
2655
2656 /* The symmetry of the paths to the type of an entity is broken here
2657 since arguments don't know that they will be passed by ref. */
2658 gnu_formal_type = TREE_TYPE (get_gnu_tree (gnat_formal));
2659 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
2660 }
2661 else if (gnu_formal
2662 && TREE_CODE (gnu_formal) == PARM_DECL
2663 && DECL_BY_COMPONENT_PTR_P (gnu_formal))
2664 {
2665 gnu_formal_type = TREE_TYPE (get_gnu_tree (gnat_formal));
2666 gnu_actual = maybe_implicit_deref (gnu_actual);
2667 gnu_actual = maybe_unconstrained_array (gnu_actual);
2668
2669 if (TREE_CODE (gnu_formal_type) == RECORD_TYPE
2670 && TYPE_IS_PADDING_P (gnu_formal_type))
2671 {
2672 gnu_formal_type = TREE_TYPE (TYPE_FIELDS (gnu_formal_type));
2673 gnu_actual = convert (gnu_formal_type, gnu_actual);
2674 }
2675
2676 /* Take the address of the object and convert to the proper pointer
2677 type. We'd like to actually compute the address of the beginning
2678 of the array using an ADDR_EXPR of an ARRAY_REF, but there's a
2679 possibility that the ARRAY_REF might return a constant and we'd be
2680 getting the wrong address. Neither approach is exactly correct,
2681 but this is the most likely to work in all cases. */
2682 gnu_actual = convert (gnu_formal_type,
2683 build_unary_op (ADDR_EXPR, NULL_TREE,
2684 gnu_actual));
2685 }
2686 else if (gnu_formal
2687 && TREE_CODE (gnu_formal) == PARM_DECL
2688 && DECL_BY_DESCRIPTOR_P (gnu_formal))
2689 {
2690 /* If arg is 'Null_Parameter, pass zero descriptor. */
2691 if ((TREE_CODE (gnu_actual) == INDIRECT_REF
2692 || TREE_CODE (gnu_actual) == UNCONSTRAINED_ARRAY_REF)
2693 && TREE_PRIVATE (gnu_actual))
2694 gnu_actual = convert (DECL_ARG_TYPE (get_gnu_tree (gnat_formal)),
2695 integer_zero_node);
2696 else
2697 gnu_actual = build_unary_op (ADDR_EXPR, NULL_TREE,
2698 fill_vms_descriptor (gnu_actual,
2699 gnat_formal,
2700 gnat_actual));
2701 }
2702 else
2703 {
2704 tree gnu_actual_size = TYPE_SIZE (TREE_TYPE (gnu_actual));
2705
2706 if (Ekind (gnat_formal) != E_In_Parameter)
2707 gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list);
2708
2709 if (!gnu_formal || TREE_CODE (gnu_formal) != PARM_DECL)
2710 continue;
2711
2712 /* If this is 'Null_Parameter, pass a zero even though we are
2713 dereferencing it. */
2714 else if (TREE_CODE (gnu_actual) == INDIRECT_REF
2715 && TREE_PRIVATE (gnu_actual)
2716 && host_integerp (gnu_actual_size, 1)
2717 && 0 >= compare_tree_int (gnu_actual_size,
2718 BITS_PER_WORD))
2719 gnu_actual
2720 = unchecked_convert (DECL_ARG_TYPE (gnu_formal),
2721 convert (gnat_type_for_size
2722 (tree_low_cst (gnu_actual_size, 1),
2723 1),
2724 integer_zero_node),
2725 false);
2726 else
2727 gnu_actual = convert (DECL_ARG_TYPE (gnu_formal), gnu_actual);
2728 }
2729
2730 gnu_actual_list = tree_cons (NULL_TREE, gnu_actual, gnu_actual_list);
2731 }
2732
2733 gnu_subprog_call = build_call_list (TREE_TYPE (gnu_subprog_type),
2734 gnu_subprog_addr,
2735 nreverse (gnu_actual_list));
2736 set_expr_location_from_node (gnu_subprog_call, gnat_node);
2737
2738 /* If we return by passing a target, the result is the target after the
2739 call. We must not emit the call directly here because this might be
2740 evaluated as part of an expression with conditions to control whether
2741 the call should be emitted or not. */
2742 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type))
2743 {
2744 /* Conceptually, what we need is a COMPOUND_EXPR with the call followed
2745 by the target object converted to the proper type. Doing so would
2746 potentially be very inefficient, however, as this expression might
2747 end up wrapped into an outer SAVE_EXPR later on, which would incur a
2748 pointless temporary copy of the whole object.
2749
2750 What we do instead is build a COMPOUND_EXPR returning the address of
2751 the target, and then dereference. Wrapping the COMPOUND_EXPR into a
2752 SAVE_EXPR later on then only incurs a pointer copy. */
2753
2754 tree gnu_result_type
2755 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (gnu_subprog_type)));
2756
2757 /* Build and return
2758 (result_type) *[gnu_subprog_call (&gnu_target, ...), &gnu_target] */
2759
2760 tree gnu_target_address
2761 = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_target);
2762 set_expr_location_from_node (gnu_target_address, gnat_node);
2763
2764 gnu_result
2765 = build2 (COMPOUND_EXPR, TREE_TYPE (gnu_target_address),
2766 gnu_subprog_call, gnu_target_address);
2767
2768 gnu_result
2769 = unchecked_convert (gnu_result_type,
2770 build_unary_op (INDIRECT_REF, NULL_TREE,
2771 gnu_result),
2772 false);
2773
2774 *gnu_result_type_p = gnu_result_type;
2775 return gnu_result;
2776 }
2777
2778 /* If it is a function call, the result is the call expression unless
2779 a target is specified, in which case we copy the result into the target
2780 and return the assignment statement. */
2781 else if (Nkind (gnat_node) == N_Function_Call)
2782 {
2783 gnu_result = gnu_subprog_call;
2784
2785 /* If the function returns an unconstrained array or by reference,
2786 we have to de-dereference the pointer. */
2787 if (TYPE_RETURNS_UNCONSTRAINED_P (gnu_subprog_type)
2788 || TYPE_RETURNS_BY_REF_P (gnu_subprog_type))
2789 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
2790
2791 if (gnu_target)
2792 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
2793 gnu_target, gnu_result);
2794 else
2795 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
2796
2797 return gnu_result;
2798 }
2799
2800 /* If this is the case where the GNAT tree contains a procedure call
2801 but the Ada procedure has copy in copy out parameters, the special
2802 parameter passing mechanism must be used. */
2803 else if (TYPE_CI_CO_LIST (gnu_subprog_type) != NULL_TREE)
2804 {
2805 /* List of FIELD_DECLs associated with the PARM_DECLs of the copy
2806 in copy out parameters. */
2807 tree scalar_return_list = TYPE_CI_CO_LIST (gnu_subprog_type);
2808 int length = list_length (scalar_return_list);
2809
2810 if (length > 1)
2811 {
2812 tree gnu_name;
2813
2814 gnu_subprog_call = save_expr (gnu_subprog_call);
2815 gnu_name_list = nreverse (gnu_name_list);
2816
2817 /* If any of the names had side-effects, ensure they are all
2818 evaluated before the call. */
2819 for (gnu_name = gnu_name_list; gnu_name;
2820 gnu_name = TREE_CHAIN (gnu_name))
2821 if (TREE_SIDE_EFFECTS (TREE_VALUE (gnu_name)))
2822 append_to_statement_list (TREE_VALUE (gnu_name),
2823 &gnu_before_list);
2824 }
2825
2826 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
2827 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
2828 else
2829 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
2830
2831 for (gnat_actual = First_Actual (gnat_node);
2832 Present (gnat_actual);
2833 gnat_formal = Next_Formal_With_Extras (gnat_formal),
2834 gnat_actual = Next_Actual (gnat_actual))
2835 /* If we are dealing with a copy in copy out parameter, we must
2836 retrieve its value from the record returned in the call. */
2837 if (!(present_gnu_tree (gnat_formal)
2838 && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
2839 && (DECL_BY_REF_P (get_gnu_tree (gnat_formal))
2840 || (TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
2841 && ((DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal))
2842 || (DECL_BY_DESCRIPTOR_P
2843 (get_gnu_tree (gnat_formal))))))))
2844 && Ekind (gnat_formal) != E_In_Parameter)
2845 {
2846 /* Get the value to assign to this Out or In Out parameter. It is
2847 either the result of the function if there is only a single such
2848 parameter or the appropriate field from the record returned. */
2849 tree gnu_result
2850 = length == 1 ? gnu_subprog_call
2851 : build_component_ref (gnu_subprog_call, NULL_TREE,
2852 TREE_PURPOSE (scalar_return_list),
2853 false);
2854
2855 /* If the actual is a conversion, get the inner expression, which
2856 will be the real destination, and convert the result to the
2857 type of the actual parameter. */
2858 tree gnu_actual
2859 = maybe_unconstrained_array (TREE_VALUE (gnu_name_list));
2860
2861 /* If the result is a padded type, remove the padding. */
2862 if (TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE
2863 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
2864 gnu_result = convert (TREE_TYPE (TYPE_FIELDS
2865 (TREE_TYPE (gnu_result))),
2866 gnu_result);
2867
2868 /* If the actual is a type conversion, the real target object is
2869 denoted by the inner Expression and we need to convert the
2870 result to the associated type.
2871 We also need to convert our gnu assignment target to this type
2872 if the corresponding GNU_NAME was constructed from the GNAT
2873 conversion node and not from the inner Expression. */
2874 if (Nkind (gnat_actual) == N_Type_Conversion)
2875 {
2876 gnu_result
2877 = convert_with_check
2878 (Etype (Expression (gnat_actual)), gnu_result,
2879 Do_Overflow_Check (gnat_actual),
2880 Do_Range_Check (Expression (gnat_actual)),
2881 Float_Truncate (gnat_actual), gnat_actual);
2882
2883 if (!Is_Composite_Type (Underlying_Type (Etype (gnat_formal))))
2884 gnu_actual = convert (TREE_TYPE (gnu_result), gnu_actual);
2885 }
2886
2887 /* Unchecked conversions as actuals for Out parameters are not
2888 allowed in user code because they are not variables, but do
2889 occur in front-end expansions. The associated GNU_NAME is
2890 always obtained from the inner expression in such cases. */
2891 else if (Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
2892 gnu_result = unchecked_convert (TREE_TYPE (gnu_actual),
2893 gnu_result,
2894 No_Truncation (gnat_actual));
2895 else
2896 {
2897 if (Do_Range_Check (gnat_actual))
2898 gnu_result
2899 = emit_range_check (gnu_result, Etype (gnat_actual),
2900 gnat_actual);
2901
2902 if (!(!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual)))
2903 && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_result)))))
2904 gnu_result = convert (TREE_TYPE (gnu_actual), gnu_result);
2905 }
2906
2907 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
2908 gnu_actual, gnu_result);
2909 set_expr_location_from_node (gnu_result, gnat_node);
2910 append_to_statement_list (gnu_result, &gnu_before_list);
2911 scalar_return_list = TREE_CHAIN (scalar_return_list);
2912 gnu_name_list = TREE_CHAIN (gnu_name_list);
2913 }
2914 }
2915 else
2916 append_to_statement_list (gnu_subprog_call, &gnu_before_list);
2917
2918 append_to_statement_list (gnu_after_list, &gnu_before_list);
2919 return gnu_before_list;
2920 }
2921 \f
2922 /* Subroutine of gnat_to_gnu to translate gnat_node, an
2923 N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */
2924
2925 static tree
2926 Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node)
2927 {
2928 tree gnu_jmpsave_decl = NULL_TREE;
2929 tree gnu_jmpbuf_decl = NULL_TREE;
2930 /* If just annotating, ignore all EH and cleanups. */
2931 bool gcc_zcx = (!type_annotate_only
2932 && Present (Exception_Handlers (gnat_node))
2933 && Exception_Mechanism == Back_End_Exceptions);
2934 bool setjmp_longjmp
2935 = (!type_annotate_only && Present (Exception_Handlers (gnat_node))
2936 && Exception_Mechanism == Setjmp_Longjmp);
2937 bool at_end = !type_annotate_only && Present (At_End_Proc (gnat_node));
2938 bool binding_for_block = (at_end || gcc_zcx || setjmp_longjmp);
2939 tree gnu_inner_block; /* The statement(s) for the block itself. */
2940 tree gnu_result;
2941 tree gnu_expr;
2942 Node_Id gnat_temp;
2943
2944 /* The GCC exception handling mechanism can handle both ZCX and SJLJ schemes
2945 and we have our own SJLJ mechanism. To call the GCC mechanism, we call
2946 add_cleanup, and when we leave the binding, end_stmt_group will create
2947 the TRY_FINALLY_EXPR.
2948
2949 ??? The region level calls down there have been specifically put in place
2950 for a ZCX context and currently the order in which things are emitted
2951 (region/handlers) is different from the SJLJ case. Instead of putting
2952 other calls with different conditions at other places for the SJLJ case,
2953 it seems cleaner to reorder things for the SJLJ case and generalize the
2954 condition to make it not ZCX specific.
2955
2956 If there are any exceptions or cleanup processing involved, we need an
2957 outer statement group (for Setjmp_Longjmp) and binding level. */
2958 if (binding_for_block)
2959 {
2960 start_stmt_group ();
2961 gnat_pushlevel ();
2962 }
2963
2964 /* If using setjmp_longjmp, make the variables for the setjmp buffer and save
2965 area for address of previous buffer. Do this first since we need to have
2966 the setjmp buf known for any decls in this block. */
2967 if (setjmp_longjmp)
2968 {
2969 gnu_jmpsave_decl = create_var_decl (get_identifier ("JMPBUF_SAVE"),
2970 NULL_TREE, jmpbuf_ptr_type,
2971 build_call_0_expr (get_jmpbuf_decl),
2972 false, false, false, false, NULL,
2973 gnat_node);
2974 DECL_ARTIFICIAL (gnu_jmpsave_decl) = 1;
2975
2976 /* The __builtin_setjmp receivers will immediately reinstall it. Now
2977 because of the unstructured form of EH used by setjmp_longjmp, there
2978 might be forward edges going to __builtin_setjmp receivers on which
2979 it is uninitialized, although they will never be actually taken. */
2980 TREE_NO_WARNING (gnu_jmpsave_decl) = 1;
2981 gnu_jmpbuf_decl = create_var_decl (get_identifier ("JMP_BUF"),
2982 NULL_TREE, jmpbuf_type,
2983 NULL_TREE, false, false, false, false,
2984 NULL, gnat_node);
2985 DECL_ARTIFICIAL (gnu_jmpbuf_decl) = 1;
2986
2987 set_block_jmpbuf_decl (gnu_jmpbuf_decl);
2988
2989 /* When we exit this block, restore the saved value. */
2990 add_cleanup (build_call_1_expr (set_jmpbuf_decl, gnu_jmpsave_decl),
2991 End_Label (gnat_node));
2992 }
2993
2994 /* If we are to call a function when exiting this block, add a cleanup
2995 to the binding level we made above. Note that add_cleanup is FIFO
2996 so we must register this cleanup after the EH cleanup just above. */
2997 if (at_end)
2998 add_cleanup (build_call_0_expr (gnat_to_gnu (At_End_Proc (gnat_node))),
2999 End_Label (gnat_node));
3000
3001 /* Now build the tree for the declarations and statements inside this block.
3002 If this is SJLJ, set our jmp_buf as the current buffer. */
3003 start_stmt_group ();
3004
3005 if (setjmp_longjmp)
3006 add_stmt (build_call_1_expr (set_jmpbuf_decl,
3007 build_unary_op (ADDR_EXPR, NULL_TREE,
3008 gnu_jmpbuf_decl)));
3009
3010 if (Present (First_Real_Statement (gnat_node)))
3011 process_decls (Statements (gnat_node), Empty,
3012 First_Real_Statement (gnat_node), true, true);
3013
3014 /* Generate code for each statement in the block. */
3015 for (gnat_temp = (Present (First_Real_Statement (gnat_node))
3016 ? First_Real_Statement (gnat_node)
3017 : First (Statements (gnat_node)));
3018 Present (gnat_temp); gnat_temp = Next (gnat_temp))
3019 add_stmt (gnat_to_gnu (gnat_temp));
3020 gnu_inner_block = end_stmt_group ();
3021
3022 /* Now generate code for the two exception models, if either is relevant for
3023 this block. */
3024 if (setjmp_longjmp)
3025 {
3026 tree *gnu_else_ptr = 0;
3027 tree gnu_handler;
3028
3029 /* Make a binding level for the exception handling declarations and code
3030 and set up gnu_except_ptr_stack for the handlers to use. */
3031 start_stmt_group ();
3032 gnat_pushlevel ();
3033
3034 push_stack (&gnu_except_ptr_stack, NULL_TREE,
3035 create_var_decl (get_identifier ("EXCEPT_PTR"),
3036 NULL_TREE,
3037 build_pointer_type (except_type_node),
3038 build_call_0_expr (get_excptr_decl), false,
3039 false, false, false, NULL, gnat_node));
3040
3041 /* Generate code for each handler. The N_Exception_Handler case does the
3042 real work and returns a COND_EXPR for each handler, which we chain
3043 together here. */
3044 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
3045 Present (gnat_temp); gnat_temp = Next_Non_Pragma (gnat_temp))
3046 {
3047 gnu_expr = gnat_to_gnu (gnat_temp);
3048
3049 /* If this is the first one, set it as the outer one. Otherwise,
3050 point the "else" part of the previous handler to us. Then point
3051 to our "else" part. */
3052 if (!gnu_else_ptr)
3053 add_stmt (gnu_expr);
3054 else
3055 *gnu_else_ptr = gnu_expr;
3056
3057 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
3058 }
3059
3060 /* If none of the exception handlers did anything, re-raise but do not
3061 defer abortion. */
3062 gnu_expr = build_call_1_expr (raise_nodefer_decl,
3063 TREE_VALUE (gnu_except_ptr_stack));
3064 set_expr_location_from_node (gnu_expr, gnat_node);
3065
3066 if (gnu_else_ptr)
3067 *gnu_else_ptr = gnu_expr;
3068 else
3069 add_stmt (gnu_expr);
3070
3071 /* End the binding level dedicated to the exception handlers and get the
3072 whole statement group. */
3073 pop_stack (&gnu_except_ptr_stack);
3074 gnat_poplevel ();
3075 gnu_handler = end_stmt_group ();
3076
3077 /* If the setjmp returns 1, we restore our incoming longjmp value and
3078 then check the handlers. */
3079 start_stmt_group ();
3080 add_stmt_with_node (build_call_1_expr (set_jmpbuf_decl,
3081 gnu_jmpsave_decl),
3082 gnat_node);
3083 add_stmt (gnu_handler);
3084 gnu_handler = end_stmt_group ();
3085
3086 /* This block is now "if (setjmp) ... <handlers> else <block>". */
3087 gnu_result = build3 (COND_EXPR, void_type_node,
3088 (build_call_1_expr
3089 (setjmp_decl,
3090 build_unary_op (ADDR_EXPR, NULL_TREE,
3091 gnu_jmpbuf_decl))),
3092 gnu_handler, gnu_inner_block);
3093 }
3094 else if (gcc_zcx)
3095 {
3096 tree gnu_handlers;
3097
3098 /* First make a block containing the handlers. */
3099 start_stmt_group ();
3100 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
3101 Present (gnat_temp);
3102 gnat_temp = Next_Non_Pragma (gnat_temp))
3103 add_stmt (gnat_to_gnu (gnat_temp));
3104 gnu_handlers = end_stmt_group ();
3105
3106 /* Now make the TRY_CATCH_EXPR for the block. */
3107 gnu_result = build2 (TRY_CATCH_EXPR, void_type_node,
3108 gnu_inner_block, gnu_handlers);
3109 }
3110 else
3111 gnu_result = gnu_inner_block;
3112
3113 /* Now close our outer block, if we had to make one. */
3114 if (binding_for_block)
3115 {
3116 add_stmt (gnu_result);
3117 gnat_poplevel ();
3118 gnu_result = end_stmt_group ();
3119 }
3120
3121 return gnu_result;
3122 }
3123 \f
3124 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
3125 to a GCC tree, which is returned. This is the variant for Setjmp_Longjmp
3126 exception handling. */
3127
3128 static tree
3129 Exception_Handler_to_gnu_sjlj (Node_Id gnat_node)
3130 {
3131 /* Unless this is "Others" or the special "Non-Ada" exception for Ada, make
3132 an "if" statement to select the proper exceptions. For "Others", exclude
3133 exceptions where Handled_By_Others is nonzero unless the All_Others flag
3134 is set. For "Non-ada", accept an exception if "Lang" is 'V'. */
3135 tree gnu_choice = integer_zero_node;
3136 tree gnu_body = build_stmt_group (Statements (gnat_node), false);
3137 Node_Id gnat_temp;
3138
3139 for (gnat_temp = First (Exception_Choices (gnat_node));
3140 gnat_temp; gnat_temp = Next (gnat_temp))
3141 {
3142 tree this_choice;
3143
3144 if (Nkind (gnat_temp) == N_Others_Choice)
3145 {
3146 if (All_Others (gnat_temp))
3147 this_choice = integer_one_node;
3148 else
3149 this_choice
3150 = build_binary_op
3151 (EQ_EXPR, integer_type_node,
3152 convert
3153 (integer_type_node,
3154 build_component_ref
3155 (build_unary_op
3156 (INDIRECT_REF, NULL_TREE,
3157 TREE_VALUE (gnu_except_ptr_stack)),
3158 get_identifier ("not_handled_by_others"), NULL_TREE,
3159 false)),
3160 integer_zero_node);
3161 }
3162
3163 else if (Nkind (gnat_temp) == N_Identifier
3164 || Nkind (gnat_temp) == N_Expanded_Name)
3165 {
3166 Entity_Id gnat_ex_id = Entity (gnat_temp);
3167 tree gnu_expr;
3168
3169 /* Exception may be a renaming. Recover original exception which is
3170 the one elaborated and registered. */
3171 if (Present (Renamed_Object (gnat_ex_id)))
3172 gnat_ex_id = Renamed_Object (gnat_ex_id);
3173
3174 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, 0);
3175
3176 this_choice
3177 = build_binary_op
3178 (EQ_EXPR, integer_type_node, TREE_VALUE (gnu_except_ptr_stack),
3179 convert (TREE_TYPE (TREE_VALUE (gnu_except_ptr_stack)),
3180 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr)));
3181
3182 /* If this is the distinguished exception "Non_Ada_Error" (and we are
3183 in VMS mode), also allow a non-Ada exception (a VMS condition) t
3184 match. */
3185 if (Is_Non_Ada_Error (Entity (gnat_temp)))
3186 {
3187 tree gnu_comp
3188 = build_component_ref
3189 (build_unary_op (INDIRECT_REF, NULL_TREE,
3190 TREE_VALUE (gnu_except_ptr_stack)),
3191 get_identifier ("lang"), NULL_TREE, false);
3192
3193 this_choice
3194 = build_binary_op
3195 (TRUTH_ORIF_EXPR, integer_type_node,
3196 build_binary_op (EQ_EXPR, integer_type_node, gnu_comp,
3197 build_int_cst (TREE_TYPE (gnu_comp), 'V')),
3198 this_choice);
3199 }
3200 }
3201 else
3202 gcc_unreachable ();
3203
3204 gnu_choice = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
3205 gnu_choice, this_choice);
3206 }
3207
3208 return build3 (COND_EXPR, void_type_node, gnu_choice, gnu_body, NULL_TREE);
3209 }
3210 \f
3211 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
3212 to a GCC tree, which is returned. This is the variant for ZCX. */
3213
3214 static tree
3215 Exception_Handler_to_gnu_zcx (Node_Id gnat_node)
3216 {
3217 tree gnu_etypes_list = NULL_TREE;
3218 tree gnu_expr;
3219 tree gnu_etype;
3220 tree gnu_current_exc_ptr;
3221 tree gnu_incoming_exc_ptr;
3222 Node_Id gnat_temp;
3223
3224 /* We build a TREE_LIST of nodes representing what exception types this
3225 handler can catch, with special cases for others and all others cases.
3226
3227 Each exception type is actually identified by a pointer to the exception
3228 id, or to a dummy object for "others" and "all others".
3229
3230 Care should be taken to ensure that the control flow impact of "others"
3231 and "all others" is known to GCC. lang_eh_type_covers is doing the trick
3232 currently. */
3233 for (gnat_temp = First (Exception_Choices (gnat_node));
3234 gnat_temp; gnat_temp = Next (gnat_temp))
3235 {
3236 if (Nkind (gnat_temp) == N_Others_Choice)
3237 {
3238 tree gnu_expr
3239 = All_Others (gnat_temp) ? all_others_decl : others_decl;
3240
3241 gnu_etype
3242 = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
3243 }
3244 else if (Nkind (gnat_temp) == N_Identifier
3245 || Nkind (gnat_temp) == N_Expanded_Name)
3246 {
3247 Entity_Id gnat_ex_id = Entity (gnat_temp);
3248
3249 /* Exception may be a renaming. Recover original exception which is
3250 the one elaborated and registered. */
3251 if (Present (Renamed_Object (gnat_ex_id)))
3252 gnat_ex_id = Renamed_Object (gnat_ex_id);
3253
3254 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, 0);
3255 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
3256
3257 /* The Non_Ada_Error case for VMS exceptions is handled
3258 by the personality routine. */
3259 }
3260 else
3261 gcc_unreachable ();
3262
3263 /* The GCC interface expects NULL to be passed for catch all handlers, so
3264 it would be quite tempting to set gnu_etypes_list to NULL if gnu_etype
3265 is integer_zero_node. It would not work, however, because GCC's
3266 notion of "catch all" is stronger than our notion of "others". Until
3267 we correctly use the cleanup interface as well, doing that would
3268 prevent the "all others" handlers from being seen, because nothing
3269 can be caught beyond a catch all from GCC's point of view. */
3270 gnu_etypes_list = tree_cons (NULL_TREE, gnu_etype, gnu_etypes_list);
3271 }
3272
3273 start_stmt_group ();
3274 gnat_pushlevel ();
3275
3276 /* Expand a call to the begin_handler hook at the beginning of the handler,
3277 and arrange for a call to the end_handler hook to occur on every possible
3278 exit path.
3279
3280 The hooks expect a pointer to the low level occurrence. This is required
3281 for our stack management scheme because a raise inside the handler pushes
3282 a new occurrence on top of the stack, which means that this top does not
3283 necessarily match the occurrence this handler was dealing with.
3284
3285 The EXC_PTR_EXPR object references the exception occurrence being
3286 propagated. Upon handler entry, this is the exception for which the
3287 handler is triggered. This might not be the case upon handler exit,
3288 however, as we might have a new occurrence propagated by the handler's
3289 body, and the end_handler hook called as a cleanup in this context.
3290
3291 We use a local variable to retrieve the incoming value at handler entry
3292 time, and reuse it to feed the end_handler hook's argument at exit. */
3293 gnu_current_exc_ptr = build0 (EXC_PTR_EXPR, ptr_type_node);
3294 gnu_incoming_exc_ptr = create_var_decl (get_identifier ("EXPTR"), NULL_TREE,
3295 ptr_type_node, gnu_current_exc_ptr,
3296 false, false, false, false, NULL,
3297 gnat_node);
3298
3299 add_stmt_with_node (build_call_1_expr (begin_handler_decl,
3300 gnu_incoming_exc_ptr),
3301 gnat_node);
3302 /* ??? We don't seem to have an End_Label at hand to set the location. */
3303 add_cleanup (build_call_1_expr (end_handler_decl, gnu_incoming_exc_ptr),
3304 Empty);
3305 add_stmt_list (Statements (gnat_node));
3306 gnat_poplevel ();
3307
3308 return build2 (CATCH_EXPR, void_type_node, gnu_etypes_list,
3309 end_stmt_group ());
3310 }
3311 \f
3312 /* Subroutine of gnat_to_gnu to generate code for an N_Compilation unit. */
3313
3314 static void
3315 Compilation_Unit_to_gnu (Node_Id gnat_node)
3316 {
3317 /* Make the decl for the elaboration procedure. */
3318 bool body_p = (Defining_Entity (Unit (gnat_node)),
3319 Nkind (Unit (gnat_node)) == N_Package_Body
3320 || Nkind (Unit (gnat_node)) == N_Subprogram_Body);
3321 Entity_Id gnat_unit_entity = Defining_Entity (Unit (gnat_node));
3322 tree gnu_elab_proc_decl
3323 = create_subprog_decl
3324 (create_concat_name (gnat_unit_entity,
3325 body_p ? "elabb" : "elabs"),
3326 NULL_TREE, void_ftype, NULL_TREE, false, true, false, NULL,
3327 gnat_unit_entity);
3328 struct elab_info *info;
3329
3330 push_stack (&gnu_elab_proc_stack, NULL_TREE, gnu_elab_proc_decl);
3331
3332 DECL_ELABORATION_PROC_P (gnu_elab_proc_decl) = 1;
3333 allocate_struct_function (gnu_elab_proc_decl, false);
3334 Sloc_to_locus (Sloc (gnat_unit_entity), &cfun->function_end_locus);
3335 set_cfun (NULL);
3336
3337 /* For a body, first process the spec if there is one. */
3338 if (Nkind (Unit (gnat_node)) == N_Package_Body
3339 || (Nkind (Unit (gnat_node)) == N_Subprogram_Body
3340 && !Acts_As_Spec (gnat_node)))
3341 {
3342 add_stmt (gnat_to_gnu (Library_Unit (gnat_node)));
3343 finalize_from_with_types ();
3344 }
3345
3346 process_inlined_subprograms (gnat_node);
3347
3348 if (type_annotate_only && gnat_node == Cunit (Main_Unit))
3349 {
3350 elaborate_all_entities (gnat_node);
3351
3352 if (Nkind (Unit (gnat_node)) == N_Subprogram_Declaration
3353 || Nkind (Unit (gnat_node)) == N_Generic_Package_Declaration
3354 || Nkind (Unit (gnat_node)) == N_Generic_Subprogram_Declaration)
3355 return;
3356 }
3357
3358 process_decls (Declarations (Aux_Decls_Node (gnat_node)), Empty, Empty,
3359 true, true);
3360 add_stmt (gnat_to_gnu (Unit (gnat_node)));
3361
3362 /* Process any pragmas and actions following the unit. */
3363 add_stmt_list (Pragmas_After (Aux_Decls_Node (gnat_node)));
3364 add_stmt_list (Actions (Aux_Decls_Node (gnat_node)));
3365 finalize_from_with_types ();
3366
3367 /* Save away what we've made so far and record this potential elaboration
3368 procedure. */
3369 info = (struct elab_info *) ggc_alloc (sizeof (struct elab_info));
3370 set_current_block_context (gnu_elab_proc_decl);
3371 gnat_poplevel ();
3372 DECL_SAVED_TREE (gnu_elab_proc_decl) = end_stmt_group ();
3373 info->next = elab_info_list;
3374 info->elab_proc = gnu_elab_proc_decl;
3375 info->gnat_node = gnat_node;
3376 elab_info_list = info;
3377
3378 /* Generate elaboration code for this unit, if necessary, and say whether
3379 we did or not. */
3380 pop_stack (&gnu_elab_proc_stack);
3381
3382 /* Invalidate the global renaming pointers. This is necessary because
3383 stabilization of the renamed entities may create SAVE_EXPRs which
3384 have been tied to a specific elaboration routine just above. */
3385 invalidate_global_renaming_pointers ();
3386 }
3387 \f
3388 /* This function is the driver of the GNAT to GCC tree transformation
3389 process. It is the entry point of the tree transformer. GNAT_NODE is the
3390 root of some GNAT tree. Return the root of the corresponding GCC tree.
3391 If this is an expression, return the GCC equivalent of the expression. If
3392 it is a statement, return the statement. In the case when called for a
3393 statement, it may also add statements to the current statement group, in
3394 which case anything it returns is to be interpreted as occurring after
3395 anything `it already added. */
3396
3397 tree
3398 gnat_to_gnu (Node_Id gnat_node)
3399 {
3400 bool went_into_elab_proc = false;
3401 tree gnu_result = error_mark_node; /* Default to no value. */
3402 tree gnu_result_type = void_type_node;
3403 tree gnu_expr;
3404 tree gnu_lhs, gnu_rhs;
3405 Node_Id gnat_temp;
3406
3407 /* Save node number for error message and set location information. */
3408 error_gnat_node = gnat_node;
3409 Sloc_to_locus (Sloc (gnat_node), &input_location);
3410
3411 if (type_annotate_only
3412 && IN (Nkind (gnat_node), N_Statement_Other_Than_Procedure_Call))
3413 return alloc_stmt_list ();
3414
3415 /* If this node is a non-static subexpression and we are only
3416 annotating types, make this into a NULL_EXPR. */
3417 if (type_annotate_only
3418 && IN (Nkind (gnat_node), N_Subexpr)
3419 && Nkind (gnat_node) != N_Identifier
3420 && !Compile_Time_Known_Value (gnat_node))
3421 return build1 (NULL_EXPR, get_unpadded_type (Etype (gnat_node)),
3422 build_call_raise (CE_Range_Check_Failed, gnat_node,
3423 N_Raise_Constraint_Error));
3424
3425 /* If this is a Statement and we are at top level, it must be part of the
3426 elaboration procedure, so mark us as being in that procedure and push our
3427 context.
3428
3429 If we are in the elaboration procedure, check if we are violating a
3430 No_Elaboration_Code restriction by having a statement there. */
3431 if ((IN (Nkind (gnat_node), N_Statement_Other_Than_Procedure_Call)
3432 && Nkind (gnat_node) != N_Null_Statement)
3433 || Nkind (gnat_node) == N_Procedure_Call_Statement
3434 || Nkind (gnat_node) == N_Label
3435 || Nkind (gnat_node) == N_Implicit_Label_Declaration
3436 || Nkind (gnat_node) == N_Handled_Sequence_Of_Statements
3437 || ((Nkind (gnat_node) == N_Raise_Constraint_Error
3438 || Nkind (gnat_node) == N_Raise_Storage_Error
3439 || Nkind (gnat_node) == N_Raise_Program_Error)
3440 && (Ekind (Etype (gnat_node)) == E_Void)))
3441 {
3442 if (!current_function_decl)
3443 {
3444 current_function_decl = TREE_VALUE (gnu_elab_proc_stack);
3445 start_stmt_group ();
3446 gnat_pushlevel ();
3447 went_into_elab_proc = true;
3448 }
3449
3450 /* Don't check for a possible No_Elaboration_Code restriction violation
3451 on N_Handled_Sequence_Of_Statements, as we want to signal an error on
3452 every nested real statement instead. This also avoids triggering
3453 spurious errors on dummy (empty) sequences created by the front-end
3454 for package bodies in some cases. */
3455
3456 if (current_function_decl == TREE_VALUE (gnu_elab_proc_stack)
3457 && Nkind (gnat_node) != N_Handled_Sequence_Of_Statements)
3458 Check_Elaboration_Code_Allowed (gnat_node);
3459 }
3460
3461 switch (Nkind (gnat_node))
3462 {
3463 /********************************/
3464 /* Chapter 2: Lexical Elements */
3465 /********************************/
3466
3467 case N_Identifier:
3468 case N_Expanded_Name:
3469 case N_Operator_Symbol:
3470 case N_Defining_Identifier:
3471 gnu_result = Identifier_to_gnu (gnat_node, &gnu_result_type);
3472 break;
3473
3474 case N_Integer_Literal:
3475 {
3476 tree gnu_type;
3477
3478 /* Get the type of the result, looking inside any padding and
3479 justified modular types. Then get the value in that type. */
3480 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
3481
3482 if (TREE_CODE (gnu_type) == RECORD_TYPE
3483 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
3484 gnu_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
3485
3486 gnu_result = UI_To_gnu (Intval (gnat_node), gnu_type);
3487
3488 /* If the result overflows (meaning it doesn't fit in its base type),
3489 abort. We would like to check that the value is within the range
3490 of the subtype, but that causes problems with subtypes whose usage
3491 will raise Constraint_Error and with biased representation, so
3492 we don't. */
3493 gcc_assert (!TREE_OVERFLOW (gnu_result));
3494 }
3495 break;
3496
3497 case N_Character_Literal:
3498 /* If a Entity is present, it means that this was one of the
3499 literals in a user-defined character type. In that case,
3500 just return the value in the CONST_DECL. Otherwise, use the
3501 character code. In that case, the base type should be an
3502 INTEGER_TYPE, but we won't bother checking for that. */
3503 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3504 if (Present (Entity (gnat_node)))
3505 gnu_result = DECL_INITIAL (get_gnu_tree (Entity (gnat_node)));
3506 else
3507 gnu_result
3508 = build_int_cst_type
3509 (gnu_result_type, UI_To_CC (Char_Literal_Value (gnat_node)));
3510 break;
3511
3512 case N_Real_Literal:
3513 /* If this is of a fixed-point type, the value we want is the
3514 value of the corresponding integer. */
3515 if (IN (Ekind (Underlying_Type (Etype (gnat_node))), Fixed_Point_Kind))
3516 {
3517 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3518 gnu_result = UI_To_gnu (Corresponding_Integer_Value (gnat_node),
3519 gnu_result_type);
3520 gcc_assert (!TREE_OVERFLOW (gnu_result));
3521 }
3522
3523 /* We should never see a Vax_Float type literal, since the front end
3524 is supposed to transform these using appropriate conversions. */
3525 else if (Vax_Float (Underlying_Type (Etype (gnat_node))))
3526 gcc_unreachable ();
3527
3528 else
3529 {
3530 Ureal ur_realval = Realval (gnat_node);
3531
3532 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3533
3534 /* If the real value is zero, so is the result. Otherwise,
3535 convert it to a machine number if it isn't already. That
3536 forces BASE to 0 or 2 and simplifies the rest of our logic. */
3537 if (UR_Is_Zero (ur_realval))
3538 gnu_result = convert (gnu_result_type, integer_zero_node);
3539 else
3540 {
3541 if (!Is_Machine_Number (gnat_node))
3542 ur_realval
3543 = Machine (Base_Type (Underlying_Type (Etype (gnat_node))),
3544 ur_realval, Round_Even, gnat_node);
3545
3546 gnu_result
3547 = UI_To_gnu (Numerator (ur_realval), gnu_result_type);
3548
3549 /* If we have a base of zero, divide by the denominator.
3550 Otherwise, the base must be 2 and we scale the value, which
3551 we know can fit in the mantissa of the type (hence the use
3552 of that type above). */
3553 if (No (Rbase (ur_realval)))
3554 gnu_result
3555 = build_binary_op (RDIV_EXPR,
3556 get_base_type (gnu_result_type),
3557 gnu_result,
3558 UI_To_gnu (Denominator (ur_realval),
3559 gnu_result_type));
3560 else
3561 {
3562 REAL_VALUE_TYPE tmp;
3563
3564 gcc_assert (Rbase (ur_realval) == 2);
3565 real_ldexp (&tmp, &TREE_REAL_CST (gnu_result),
3566 - UI_To_Int (Denominator (ur_realval)));
3567 gnu_result = build_real (gnu_result_type, tmp);
3568 }
3569 }
3570
3571 /* Now see if we need to negate the result. Do it this way to
3572 properly handle -0. */
3573 if (UR_Is_Negative (Realval (gnat_node)))
3574 gnu_result
3575 = build_unary_op (NEGATE_EXPR, get_base_type (gnu_result_type),
3576 gnu_result);
3577 }
3578
3579 break;
3580
3581 case N_String_Literal:
3582 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3583 if (TYPE_PRECISION (TREE_TYPE (gnu_result_type)) == HOST_BITS_PER_CHAR)
3584 {
3585 String_Id gnat_string = Strval (gnat_node);
3586 int length = String_Length (gnat_string);
3587 int i;
3588 char *string;
3589 if (length >= ALLOCA_THRESHOLD)
3590 string = XNEWVEC (char, length + 1);
3591 else
3592 string = (char *) alloca (length + 1);
3593
3594 /* Build the string with the characters in the literal. Note
3595 that Ada strings are 1-origin. */
3596 for (i = 0; i < length; i++)
3597 string[i] = Get_String_Char (gnat_string, i + 1);
3598
3599 /* Put a null at the end of the string in case it's in a context
3600 where GCC will want to treat it as a C string. */
3601 string[i] = 0;
3602
3603 gnu_result = build_string (length, string);
3604
3605 /* Strings in GCC don't normally have types, but we want
3606 this to not be converted to the array type. */
3607 TREE_TYPE (gnu_result) = gnu_result_type;
3608
3609 if (length >= ALLOCA_THRESHOLD)
3610 free (string);
3611 }
3612 else
3613 {
3614 /* Build a list consisting of each character, then make
3615 the aggregate. */
3616 String_Id gnat_string = Strval (gnat_node);
3617 int length = String_Length (gnat_string);
3618 int i;
3619 tree gnu_list = NULL_TREE;
3620 tree gnu_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
3621
3622 for (i = 0; i < length; i++)
3623 {
3624 gnu_list
3625 = tree_cons (gnu_idx,
3626 build_int_cst (TREE_TYPE (gnu_result_type),
3627 Get_String_Char (gnat_string,
3628 i + 1)),
3629 gnu_list);
3630
3631 gnu_idx = int_const_binop (PLUS_EXPR, gnu_idx, integer_one_node,
3632 0);
3633 }
3634
3635 gnu_result
3636 = gnat_build_constructor (gnu_result_type, nreverse (gnu_list));
3637 }
3638 break;
3639
3640 case N_Pragma:
3641 gnu_result = Pragma_to_gnu (gnat_node);
3642 break;
3643
3644 /**************************************/
3645 /* Chapter 3: Declarations and Types */
3646 /**************************************/
3647
3648 case N_Subtype_Declaration:
3649 case N_Full_Type_Declaration:
3650 case N_Incomplete_Type_Declaration:
3651 case N_Private_Type_Declaration:
3652 case N_Private_Extension_Declaration:
3653 case N_Task_Type_Declaration:
3654 process_type (Defining_Entity (gnat_node));
3655 gnu_result = alloc_stmt_list ();
3656 break;
3657
3658 case N_Object_Declaration:
3659 case N_Exception_Declaration:
3660 gnat_temp = Defining_Entity (gnat_node);
3661 gnu_result = alloc_stmt_list ();
3662
3663 /* If we are just annotating types and this object has an unconstrained
3664 or task type, don't elaborate it. */
3665 if (type_annotate_only
3666 && (((Is_Array_Type (Etype (gnat_temp))
3667 || Is_Record_Type (Etype (gnat_temp)))
3668 && !Is_Constrained (Etype (gnat_temp)))
3669 || Is_Concurrent_Type (Etype (gnat_temp))))
3670 break;
3671
3672 if (Present (Expression (gnat_node))
3673 && !(Nkind (gnat_node) == N_Object_Declaration
3674 && No_Initialization (gnat_node))
3675 && (!type_annotate_only
3676 || Compile_Time_Known_Value (Expression (gnat_node))))
3677 {
3678 gnu_expr = gnat_to_gnu (Expression (gnat_node));
3679 if (Do_Range_Check (Expression (gnat_node)))
3680 gnu_expr
3681 = emit_range_check (gnu_expr, Etype (gnat_temp), gnat_node);
3682
3683 /* If this object has its elaboration delayed, we must force
3684 evaluation of GNU_EXPR right now and save it for when the object
3685 is frozen. */
3686 if (Present (Freeze_Node (gnat_temp)))
3687 {
3688 if ((Is_Public (gnat_temp) || global_bindings_p ())
3689 && !TREE_CONSTANT (gnu_expr))
3690 gnu_expr
3691 = create_var_decl (create_concat_name (gnat_temp, "init"),
3692 NULL_TREE, TREE_TYPE (gnu_expr),
3693 gnu_expr, false, Is_Public (gnat_temp),
3694 false, false, NULL, gnat_temp);
3695 else
3696 gnu_expr = maybe_variable (gnu_expr);
3697
3698 save_gnu_tree (gnat_node, gnu_expr, true);
3699 }
3700 }
3701 else
3702 gnu_expr = NULL_TREE;
3703
3704 if (type_annotate_only && gnu_expr && TREE_CODE (gnu_expr) == ERROR_MARK)
3705 gnu_expr = NULL_TREE;
3706
3707 /* If this is a deferred constant with an address clause, we ignore the
3708 full view since the clause is on the partial view and we cannot have
3709 2 different GCC trees for the object. The only bits of the full view
3710 we will use is the initializer, but it will be directly fetched. */
3711 if (Ekind(gnat_temp) == E_Constant
3712 && Present (Address_Clause (gnat_temp))
3713 && Present (Full_View (gnat_temp)))
3714 save_gnu_tree (Full_View (gnat_temp), error_mark_node, true);
3715
3716 if (No (Freeze_Node (gnat_temp)))
3717 gnat_to_gnu_entity (gnat_temp, gnu_expr, 1);
3718 break;
3719
3720 case N_Object_Renaming_Declaration:
3721 gnat_temp = Defining_Entity (gnat_node);
3722
3723 /* Don't do anything if this renaming is handled by the front end or if
3724 we are just annotating types and this object has a composite or task
3725 type, don't elaborate it. We return the result in case it has any
3726 SAVE_EXPRs in it that need to be evaluated here. */
3727 if (!Is_Renaming_Of_Object (gnat_temp)
3728 && ! (type_annotate_only
3729 && (Is_Array_Type (Etype (gnat_temp))
3730 || Is_Record_Type (Etype (gnat_temp))
3731 || Is_Concurrent_Type (Etype (gnat_temp)))))
3732 gnu_result
3733 = gnat_to_gnu_entity (gnat_temp,
3734 gnat_to_gnu (Renamed_Object (gnat_temp)), 1);
3735 else
3736 gnu_result = alloc_stmt_list ();
3737 break;
3738
3739 case N_Implicit_Label_Declaration:
3740 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
3741 gnu_result = alloc_stmt_list ();
3742 break;
3743
3744 case N_Exception_Renaming_Declaration:
3745 case N_Number_Declaration:
3746 case N_Package_Renaming_Declaration:
3747 case N_Subprogram_Renaming_Declaration:
3748 /* These are fully handled in the front end. */
3749 gnu_result = alloc_stmt_list ();
3750 break;
3751
3752 /*************************************/
3753 /* Chapter 4: Names and Expressions */
3754 /*************************************/
3755
3756 case N_Explicit_Dereference:
3757 gnu_result = gnat_to_gnu (Prefix (gnat_node));
3758 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3759 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
3760 break;
3761
3762 case N_Indexed_Component:
3763 {
3764 tree gnu_array_object = gnat_to_gnu (Prefix (gnat_node));
3765 tree gnu_type;
3766 int ndim;
3767 int i;
3768 Node_Id *gnat_expr_array;
3769
3770 gnu_array_object = maybe_implicit_deref (gnu_array_object);
3771 gnu_array_object = maybe_unconstrained_array (gnu_array_object);
3772
3773 /* If we got a padded type, remove it too. */
3774 if (TREE_CODE (TREE_TYPE (gnu_array_object)) == RECORD_TYPE
3775 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_array_object)))
3776 gnu_array_object
3777 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_array_object))),
3778 gnu_array_object);
3779
3780 gnu_result = gnu_array_object;
3781
3782 /* First compute the number of dimensions of the array, then
3783 fill the expression array, the order depending on whether
3784 this is a Convention_Fortran array or not. */
3785 for (ndim = 1, gnu_type = TREE_TYPE (gnu_array_object);
3786 TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
3787 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type));
3788 ndim++, gnu_type = TREE_TYPE (gnu_type))
3789 ;
3790
3791 gnat_expr_array = (Node_Id *) alloca (ndim * sizeof (Node_Id));
3792
3793 if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object)))
3794 for (i = ndim - 1, gnat_temp = First (Expressions (gnat_node));
3795 i >= 0;
3796 i--, gnat_temp = Next (gnat_temp))
3797 gnat_expr_array[i] = gnat_temp;
3798 else
3799 for (i = 0, gnat_temp = First (Expressions (gnat_node));
3800 i < ndim;
3801 i++, gnat_temp = Next (gnat_temp))
3802 gnat_expr_array[i] = gnat_temp;
3803
3804 for (i = 0, gnu_type = TREE_TYPE (gnu_array_object);
3805 i < ndim; i++, gnu_type = TREE_TYPE (gnu_type))
3806 {
3807 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
3808 gnat_temp = gnat_expr_array[i];
3809 gnu_expr = gnat_to_gnu (gnat_temp);
3810
3811 if (Do_Range_Check (gnat_temp))
3812 gnu_expr
3813 = emit_index_check
3814 (gnu_array_object, gnu_expr,
3815 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
3816 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
3817 gnat_temp);
3818
3819 gnu_result = build_binary_op (ARRAY_REF, NULL_TREE,
3820 gnu_result, gnu_expr);
3821 }
3822 }
3823
3824 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3825 break;
3826
3827 case N_Slice:
3828 {
3829 tree gnu_type;
3830 Node_Id gnat_range_node = Discrete_Range (gnat_node);
3831
3832 gnu_result = gnat_to_gnu (Prefix (gnat_node));
3833 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3834
3835 /* Do any implicit dereferences of the prefix and do any needed
3836 range check. */
3837 gnu_result = maybe_implicit_deref (gnu_result);
3838 gnu_result = maybe_unconstrained_array (gnu_result);
3839 gnu_type = TREE_TYPE (gnu_result);
3840 if (Do_Range_Check (gnat_range_node))
3841 {
3842 /* Get the bounds of the slice. */
3843 tree gnu_index_type
3844 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_result_type));
3845 tree gnu_min_expr = TYPE_MIN_VALUE (gnu_index_type);
3846 tree gnu_max_expr = TYPE_MAX_VALUE (gnu_index_type);
3847 /* Get the permitted bounds. */
3848 tree gnu_base_index_type
3849 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type));
3850 tree gnu_base_min_expr = SUBSTITUTE_PLACEHOLDER_IN_EXPR
3851 (TYPE_MIN_VALUE (gnu_base_index_type), gnu_result);
3852 tree gnu_base_max_expr = SUBSTITUTE_PLACEHOLDER_IN_EXPR
3853 (TYPE_MAX_VALUE (gnu_base_index_type), gnu_result);
3854 tree gnu_expr_l, gnu_expr_h, gnu_expr_type;
3855
3856 gnu_min_expr = protect_multiple_eval (gnu_min_expr);
3857 gnu_max_expr = protect_multiple_eval (gnu_max_expr);
3858
3859 /* Derive a good type to convert everything to. */
3860 gnu_expr_type = get_base_type (gnu_index_type);
3861
3862 /* Test whether the minimum slice value is too small. */
3863 gnu_expr_l = build_binary_op (LT_EXPR, integer_type_node,
3864 convert (gnu_expr_type,
3865 gnu_min_expr),
3866 convert (gnu_expr_type,
3867 gnu_base_min_expr));
3868
3869 /* Test whether the maximum slice value is too large. */
3870 gnu_expr_h = build_binary_op (GT_EXPR, integer_type_node,
3871 convert (gnu_expr_type,
3872 gnu_max_expr),
3873 convert (gnu_expr_type,
3874 gnu_base_max_expr));
3875
3876 /* Build a slice index check that returns the low bound,
3877 assuming the slice is not empty. */
3878 gnu_expr = emit_check
3879 (build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
3880 gnu_expr_l, gnu_expr_h),
3881 gnu_min_expr, CE_Index_Check_Failed, gnat_node);
3882
3883 /* Build a conditional expression that does the index checks and
3884 returns the low bound if the slice is not empty (max >= min),
3885 and returns the naked low bound otherwise (max < min), unless
3886 it is non-constant and the high bound is; this prevents VRP
3887 from inferring bogus ranges on the unlikely path. */
3888 gnu_expr = fold_build3 (COND_EXPR, gnu_expr_type,
3889 build_binary_op (GE_EXPR, gnu_expr_type,
3890 convert (gnu_expr_type,
3891 gnu_max_expr),
3892 convert (gnu_expr_type,
3893 gnu_min_expr)),
3894 gnu_expr,
3895 TREE_CODE (gnu_min_expr) != INTEGER_CST
3896 && TREE_CODE (gnu_max_expr) == INTEGER_CST
3897 ? gnu_max_expr : gnu_min_expr);
3898 }
3899 else
3900 /* Simply return the naked low bound. */
3901 gnu_expr = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
3902
3903 gnu_result = build_binary_op (ARRAY_RANGE_REF, gnu_result_type,
3904 gnu_result, gnu_expr);
3905 }
3906 break;
3907
3908 case N_Selected_Component:
3909 {
3910 tree gnu_prefix = gnat_to_gnu (Prefix (gnat_node));
3911 Entity_Id gnat_field = Entity (Selector_Name (gnat_node));
3912 Entity_Id gnat_pref_type = Etype (Prefix (gnat_node));
3913 tree gnu_field;
3914
3915 while (IN (Ekind (gnat_pref_type), Incomplete_Or_Private_Kind)
3916 || IN (Ekind (gnat_pref_type), Access_Kind))
3917 {
3918 if (IN (Ekind (gnat_pref_type), Incomplete_Or_Private_Kind))
3919 gnat_pref_type = Underlying_Type (gnat_pref_type);
3920 else if (IN (Ekind (gnat_pref_type), Access_Kind))
3921 gnat_pref_type = Designated_Type (gnat_pref_type);
3922 }
3923
3924 gnu_prefix = maybe_implicit_deref (gnu_prefix);
3925
3926 /* For discriminant references in tagged types always substitute the
3927 corresponding discriminant as the actual selected component. */
3928 if (Is_Tagged_Type (gnat_pref_type))
3929 while (Present (Corresponding_Discriminant (gnat_field)))
3930 gnat_field = Corresponding_Discriminant (gnat_field);
3931
3932 /* For discriminant references of untagged types always substitute the
3933 corresponding stored discriminant. */
3934 else if (Present (Corresponding_Discriminant (gnat_field)))
3935 gnat_field = Original_Record_Component (gnat_field);
3936
3937 /* Handle extracting the real or imaginary part of a complex.
3938 The real part is the first field and the imaginary the last. */
3939 if (TREE_CODE (TREE_TYPE (gnu_prefix)) == COMPLEX_TYPE)
3940 gnu_result = build_unary_op (Present (Next_Entity (gnat_field))
3941 ? REALPART_EXPR : IMAGPART_EXPR,
3942 NULL_TREE, gnu_prefix);
3943 else
3944 {
3945 gnu_field = gnat_to_gnu_field_decl (gnat_field);
3946
3947 /* If there are discriminants, the prefix might be evaluated more
3948 than once, which is a problem if it has side-effects. */
3949 if (Has_Discriminants (Is_Access_Type (Etype (Prefix (gnat_node)))
3950 ? Designated_Type (Etype
3951 (Prefix (gnat_node)))
3952 : Etype (Prefix (gnat_node))))
3953 gnu_prefix = gnat_stabilize_reference (gnu_prefix, false);
3954
3955 gnu_result
3956 = build_component_ref (gnu_prefix, NULL_TREE, gnu_field,
3957 (Nkind (Parent (gnat_node))
3958 == N_Attribute_Reference));
3959 }
3960
3961 gcc_assert (gnu_result);
3962 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3963 }
3964 break;
3965
3966 case N_Attribute_Reference:
3967 {
3968 /* The attribute designator (like an enumeration value). */
3969 int attribute = Get_Attribute_Id (Attribute_Name (gnat_node));
3970
3971 /* The Elab_Spec and Elab_Body attributes are special in that
3972 Prefix is a unit, not an object with a GCC equivalent. Similarly
3973 for Elaborated, since that variable isn't otherwise known. */
3974 if (attribute == Attr_Elab_Body || attribute == Attr_Elab_Spec)
3975 return (create_subprog_decl
3976 (create_concat_name (Entity (Prefix (gnat_node)),
3977 attribute == Attr_Elab_Body
3978 ? "elabb" : "elabs"),
3979 NULL_TREE, void_ftype, NULL_TREE, false, true, true, NULL,
3980 gnat_node));
3981
3982 gnu_result = Attribute_to_gnu (gnat_node, &gnu_result_type, attribute);
3983 }
3984 break;
3985
3986 case N_Reference:
3987 /* Like 'Access as far as we are concerned. */
3988 gnu_result = gnat_to_gnu (Prefix (gnat_node));
3989 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
3990 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3991 break;
3992
3993 case N_Aggregate:
3994 case N_Extension_Aggregate:
3995 {
3996 tree gnu_aggr_type;
3997
3998 /* ??? It is wrong to evaluate the type now, but there doesn't
3999 seem to be any other practical way of doing it. */
4000
4001 gcc_assert (!Expansion_Delayed (gnat_node));
4002
4003 gnu_aggr_type = gnu_result_type
4004 = get_unpadded_type (Etype (gnat_node));
4005
4006 if (TREE_CODE (gnu_result_type) == RECORD_TYPE
4007 && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type))
4008 gnu_aggr_type
4009 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_result_type)));
4010
4011 if (Null_Record_Present (gnat_node))
4012 gnu_result = gnat_build_constructor (gnu_aggr_type, NULL_TREE);
4013
4014 else if (TREE_CODE (gnu_aggr_type) == RECORD_TYPE
4015 || TREE_CODE (gnu_aggr_type) == UNION_TYPE)
4016 gnu_result
4017 = assoc_to_constructor (Etype (gnat_node),
4018 First (Component_Associations (gnat_node)),
4019 gnu_aggr_type);
4020 else if (TREE_CODE (gnu_aggr_type) == ARRAY_TYPE)
4021 gnu_result = pos_to_constructor (First (Expressions (gnat_node)),
4022 gnu_aggr_type,
4023 Component_Type (Etype (gnat_node)));
4024 else if (TREE_CODE (gnu_aggr_type) == COMPLEX_TYPE)
4025 gnu_result
4026 = build_binary_op
4027 (COMPLEX_EXPR, gnu_aggr_type,
4028 gnat_to_gnu (Expression (First
4029 (Component_Associations (gnat_node)))),
4030 gnat_to_gnu (Expression
4031 (Next
4032 (First (Component_Associations (gnat_node))))));
4033 else
4034 gcc_unreachable ();
4035
4036 gnu_result = convert (gnu_result_type, gnu_result);
4037 }
4038 break;
4039
4040 case N_Null:
4041 if (TARGET_VTABLE_USES_DESCRIPTORS
4042 && Ekind (Etype (gnat_node)) == E_Access_Subprogram_Type
4043 && Is_Dispatch_Table_Entity (Etype (gnat_node)))
4044 gnu_result = null_fdesc_node;
4045 else
4046 gnu_result = null_pointer_node;
4047 gnu_result_type = get_unpadded_type (Etype (gnat_node));
4048 break;
4049
4050 case N_Type_Conversion:
4051 case N_Qualified_Expression:
4052 /* Get the operand expression. */
4053 gnu_result = gnat_to_gnu (Expression (gnat_node));
4054 gnu_result_type = get_unpadded_type (Etype (gnat_node));
4055
4056 gnu_result
4057 = convert_with_check (Etype (gnat_node), gnu_result,
4058 Do_Overflow_Check (gnat_node),
4059 Do_Range_Check (Expression (gnat_node)),
4060 Nkind (gnat_node) == N_Type_Conversion
4061 && Float_Truncate (gnat_node), gnat_node);
4062 break;
4063
4064 case N_Unchecked_Type_Conversion:
4065 gnu_result = gnat_to_gnu (Expression (gnat_node));
4066 gnu_result_type = get_unpadded_type (Etype (gnat_node));
4067
4068 /* If the result is a pointer type, see if we are improperly
4069 converting to a stricter alignment. */
4070 if (STRICT_ALIGNMENT && POINTER_TYPE_P (gnu_result_type)
4071 && IN (Ekind (Etype (gnat_node)), Access_Kind))
4072 {
4073 unsigned int align = known_alignment (gnu_result);
4074 tree gnu_obj_type = TREE_TYPE (gnu_result_type);
4075 unsigned int oalign = TYPE_ALIGN (gnu_obj_type);
4076
4077 if (align != 0 && align < oalign && !TYPE_ALIGN_OK (gnu_obj_type))
4078 post_error_ne_tree_2
4079 ("?source alignment (^) '< alignment of & (^)",
4080 gnat_node, Designated_Type (Etype (gnat_node)),
4081 size_int (align / BITS_PER_UNIT), oalign / BITS_PER_UNIT);
4082 }
4083
4084 /* If we are converting a descriptor to a function pointer, first
4085 build the pointer. */
4086 if (TARGET_VTABLE_USES_DESCRIPTORS
4087 && TREE_TYPE (gnu_result) == fdesc_type_node
4088 && POINTER_TYPE_P (gnu_result_type))
4089 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
4090
4091 gnu_result = unchecked_convert (gnu_result_type, gnu_result,
4092 No_Truncation (gnat_node));
4093 break;
4094
4095 case N_In:
4096 case N_Not_In:
4097 {
4098 tree gnu_object = gnat_to_gnu (Left_Opnd (gnat_node));
4099 Node_Id gnat_range = Right_Opnd (gnat_node);
4100 tree gnu_low;
4101 tree gnu_high;
4102
4103 /* GNAT_RANGE is either an N_Range node or an identifier
4104 denoting a subtype. */
4105 if (Nkind (gnat_range) == N_Range)
4106 {
4107 gnu_low = gnat_to_gnu (Low_Bound (gnat_range));
4108 gnu_high = gnat_to_gnu (High_Bound (gnat_range));
4109 }
4110 else if (Nkind (gnat_range) == N_Identifier
4111 || Nkind (gnat_range) == N_Expanded_Name)
4112 {
4113 tree gnu_range_type = get_unpadded_type (Entity (gnat_range));
4114
4115 gnu_low = TYPE_MIN_VALUE (gnu_range_type);
4116 gnu_high = TYPE_MAX_VALUE (gnu_range_type);
4117 }
4118 else
4119 gcc_unreachable ();
4120
4121 gnu_result_type = get_unpadded_type (Etype (gnat_node));
4122
4123 /* If LOW and HIGH are identical, perform an equality test.
4124 Otherwise, ensure that GNU_OBJECT is only evaluated once
4125 and perform a full range test. */
4126 if (operand_equal_p (gnu_low, gnu_high, 0))
4127 gnu_result = build_binary_op (EQ_EXPR, gnu_result_type,
4128 gnu_object, gnu_low);
4129 else
4130 {
4131 gnu_object = protect_multiple_eval (gnu_object);
4132 gnu_result
4133 = build_binary_op (TRUTH_ANDIF_EXPR, gnu_result_type,
4134 build_binary_op (GE_EXPR, gnu_result_type,
4135 gnu_object, gnu_low),
4136 build_binary_op (LE_EXPR, gnu_result_type,
4137 gnu_object, gnu_high));
4138 }
4139
4140 if (Nkind (gnat_node) == N_Not_In)
4141 gnu_result = invert_truthvalue (gnu_result);
4142 }
4143 break;
4144
4145 case N_Op_Divide:
4146 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
4147 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
4148 gnu_result_type = get_unpadded_type (Etype (gnat_node));
4149 gnu_result = build_binary_op (FLOAT_TYPE_P (gnu_result_type)
4150 ? RDIV_EXPR
4151 : (Rounded_Result (gnat_node)
4152 ? ROUND_DIV_EXPR : TRUNC_DIV_EXPR),
4153 gnu_result_type, gnu_lhs, gnu_rhs);
4154 break;
4155
4156 case N_Op_Or: case N_Op_And: case N_Op_Xor:
4157 /* These can either be operations on booleans or on modular types.
4158 Fall through for boolean types since that's the way GNU_CODES is
4159 set up. */
4160 if (IN (Ekind (Underlying_Type (Etype (gnat_node))),
4161 Modular_Integer_Kind))
4162 {
4163 enum tree_code code
4164 = (Nkind (gnat_node) == N_Op_Or ? BIT_IOR_EXPR
4165 : Nkind (gnat_node) == N_Op_And ? BIT_AND_EXPR
4166 : BIT_XOR_EXPR);
4167
4168 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
4169 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
4170 gnu_result_type = get_unpadded_type (Etype (gnat_node));
4171 gnu_result = build_binary_op (code, gnu_result_type,
4172 gnu_lhs, gnu_rhs);
4173 break;
4174 }
4175
4176 /* ... fall through ... */
4177
4178 case N_Op_Eq: case N_Op_Ne: case N_Op_Lt:
4179 case N_Op_Le: case N_Op_Gt: case N_Op_Ge:
4180 case N_Op_Add: case N_Op_Subtract: case N_Op_Multiply:
4181 case N_Op_Mod: case N_Op_Rem:
4182 case N_Op_Rotate_Left:
4183 case N_Op_Rotate_Right:
4184 case N_Op_Shift_Left:
4185 case N_Op_Shift_Right:
4186 case N_Op_Shift_Right_Arithmetic:
4187 case N_And_Then: case N_Or_Else:
4188 {
4189 enum tree_code code = gnu_codes[Nkind (gnat_node)];
4190 bool ignore_lhs_overflow = false;
4191 tree gnu_type;
4192
4193 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
4194 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
4195 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
4196
4197 /* If this is a comparison operator, convert any references to
4198 an unconstrained array value into a reference to the
4199 actual array. */
4200 if (TREE_CODE_CLASS (code) == tcc_comparison)
4201 {
4202 gnu_lhs = maybe_unconstrained_array (gnu_lhs);
4203 gnu_rhs = maybe_unconstrained_array (gnu_rhs);
4204 }
4205
4206 /* If the result type is a private type, its full view may be a
4207 numeric subtype. The representation we need is that of its base
4208 type, given that it is the result of an arithmetic operation. */
4209 else if (Is_Private_Type (Etype (gnat_node)))
4210 gnu_type = gnu_result_type
4211 = get_unpadded_type (Base_Type (Full_View (Etype (gnat_node))));
4212
4213 /* If this is a shift whose count is not guaranteed to be correct,
4214 we need to adjust the shift count. */
4215 if (IN (Nkind (gnat_node), N_Op_Shift)
4216 && !Shift_Count_OK (gnat_node))
4217 {
4218 tree gnu_count_type = get_base_type (TREE_TYPE (gnu_rhs));
4219 tree gnu_max_shift
4220 = convert (gnu_count_type, TYPE_SIZE (gnu_type));
4221
4222 if (Nkind (gnat_node) == N_Op_Rotate_Left
4223 || Nkind (gnat_node) == N_Op_Rotate_Right)
4224 gnu_rhs = build_binary_op (TRUNC_MOD_EXPR, gnu_count_type,
4225 gnu_rhs, gnu_max_shift);
4226 else if (Nkind (gnat_node) == N_Op_Shift_Right_Arithmetic)
4227 gnu_rhs
4228 = build_binary_op
4229 (MIN_EXPR, gnu_count_type,
4230 build_binary_op (MINUS_EXPR,
4231 gnu_count_type,
4232 gnu_max_shift,
4233 convert (gnu_count_type,
4234 integer_one_node)),
4235 gnu_rhs);
4236 }
4237
4238 /* For right shifts, the type says what kind of shift to do,
4239 so we may need to choose a different type. In this case,
4240 we have to ignore integer overflow lest it propagates all
4241 the way down and causes a CE to be explicitly raised. */
4242 if (Nkind (gnat_node) == N_Op_Shift_Right
4243 && !TYPE_UNSIGNED (gnu_type))
4244 {
4245 gnu_type = gnat_unsigned_type (gnu_type);
4246 ignore_lhs_overflow = true;
4247 }
4248 else if (Nkind (gnat_node) == N_Op_Shift_Right_Arithmetic
4249 && TYPE_UNSIGNED (gnu_type))
4250 {
4251 gnu_type = gnat_signed_type (gnu_type);
4252 ignore_lhs_overflow = true;
4253 }
4254
4255 if (gnu_type != gnu_result_type)
4256 {
4257 tree gnu_old_lhs = gnu_lhs;
4258 gnu_lhs = convert (gnu_type, gnu_lhs);
4259 if (TREE_CODE (gnu_lhs) == INTEGER_CST && ignore_lhs_overflow)
4260 TREE_OVERFLOW (gnu_lhs) = TREE_OVERFLOW (gnu_old_lhs);
4261 gnu_rhs = convert (gnu_type, gnu_rhs);
4262 }
4263
4264 /* Instead of expanding overflow checks for addition, subtraction
4265 and multiplication itself, the front end will leave this to
4266 the back end when Backend_Overflow_Checks_On_Target is set.
4267 As the GCC back end itself does not know yet how to properly
4268 do overflow checking, do it here. The goal is to push
4269 the expansions further into the back end over time. */
4270 if (Do_Overflow_Check (gnat_node) && Backend_Overflow_Checks_On_Target
4271 && (Nkind (gnat_node) == N_Op_Add
4272 || Nkind (gnat_node) == N_Op_Subtract
4273 || Nkind (gnat_node) == N_Op_Multiply)
4274 && !TYPE_UNSIGNED (gnu_type)
4275 && !FLOAT_TYPE_P (gnu_type))
4276 gnu_result = build_binary_op_trapv (code, gnu_type,
4277 gnu_lhs, gnu_rhs, gnat_node);
4278 else
4279 gnu_result = build_binary_op (code, gnu_type, gnu_lhs, gnu_rhs);
4280
4281 /* If this is a logical shift with the shift count not verified,
4282 we must return zero if it is too large. We cannot compensate
4283 above in this case. */
4284 if ((Nkind (gnat_node) == N_Op_Shift_Left
4285 || Nkind (gnat_node) == N_Op_Shift_Right)
4286 && !Shift_Count_OK (gnat_node))
4287 gnu_result
4288 = build_cond_expr
4289 (gnu_type,
4290 build_binary_op (GE_EXPR, integer_type_node,
4291 gnu_rhs,
4292 convert (TREE_TYPE (gnu_rhs),
4293 TYPE_SIZE (gnu_type))),
4294 convert (gnu_type, integer_zero_node),
4295 gnu_result);
4296 }
4297 break;
4298
4299 case N_Conditional_Expression:
4300 {
4301 tree gnu_cond = gnat_to_gnu (First (Expressions (gnat_node)));
4302 tree gnu_true = gnat_to_gnu (Next (First (Expressions (gnat_node))));
4303 tree gnu_false
4304 = gnat_to_gnu (Next (Next (First (Expressions (gnat_node)))));
4305
4306 gnu_result_type = get_unpadded_type (Etype (gnat_node));
4307 gnu_result = build_cond_expr (gnu_result_type,
4308 gnat_truthvalue_conversion (gnu_cond),
4309 gnu_true, gnu_false);
4310 }
4311 break;
4312
4313 case N_Op_Plus:
4314 gnu_result = gnat_to_gnu (Right_Opnd (gnat_node));
4315 gnu_result_type = get_unpadded_type (Etype (gnat_node));
4316 break;
4317
4318 case N_Op_Not:
4319 /* This case can apply to a boolean or a modular type.
4320 Fall through for a boolean operand since GNU_CODES is set
4321 up to handle this. */
4322 if (Is_Modular_Integer_Type (Etype (gnat_node))
4323 || (Ekind (Etype (gnat_node)) == E_Private_Type
4324 && Is_Modular_Integer_Type (Full_View (Etype (gnat_node)))))
4325 {
4326 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
4327 gnu_result_type = get_unpadded_type (Etype (gnat_node));
4328 gnu_result = build_unary_op (BIT_NOT_EXPR, gnu_result_type,
4329 gnu_expr);
4330 break;
4331 }
4332
4333 /* ... fall through ... */
4334
4335 case N_Op_Minus: case N_Op_Abs:
4336 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
4337
4338 if (Ekind (Etype (gnat_node)) != E_Private_Type)
4339 gnu_result_type = get_unpadded_type (Etype (gnat_node));
4340 else
4341 gnu_result_type = get_unpadded_type (Base_Type
4342 (Full_View (Etype (gnat_node))));
4343
4344 if (Do_Overflow_Check (gnat_node)
4345 && !TYPE_UNSIGNED (gnu_result_type)
4346 && !FLOAT_TYPE_P (gnu_result_type))
4347 gnu_result
4348 = build_unary_op_trapv (gnu_codes[Nkind (gnat_node)],
4349 gnu_result_type, gnu_expr, gnat_node);
4350 else
4351 gnu_result = build_unary_op (gnu_codes[Nkind (gnat_node)],
4352 gnu_result_type, gnu_expr);
4353 break;
4354
4355 case N_Allocator:
4356 {
4357 tree gnu_init = 0;
4358 tree gnu_type;
4359 bool ignore_init_type = false;
4360
4361 gnat_temp = Expression (gnat_node);
4362
4363 /* The Expression operand can either be an N_Identifier or
4364 Expanded_Name, which must represent a type, or a
4365 N_Qualified_Expression, which contains both the object type and an
4366 initial value for the object. */
4367 if (Nkind (gnat_temp) == N_Identifier
4368 || Nkind (gnat_temp) == N_Expanded_Name)
4369 gnu_type = gnat_to_gnu_type (Entity (gnat_temp));
4370 else if (Nkind (gnat_temp) == N_Qualified_Expression)
4371 {
4372 Entity_Id gnat_desig_type
4373 = Designated_Type (Underlying_Type (Etype (gnat_node)));
4374
4375 ignore_init_type = Has_Constrained_Partial_View (gnat_desig_type);
4376 gnu_init = gnat_to_gnu (Expression (gnat_temp));
4377
4378 gnu_init = maybe_unconstrained_array (gnu_init);
4379 if (Do_Range_Check (Expression (gnat_temp)))
4380 gnu_init
4381 = emit_range_check (gnu_init, gnat_desig_type, gnat_temp);
4382
4383 if (Is_Elementary_Type (gnat_desig_type)
4384 || Is_Constrained (gnat_desig_type))
4385 {
4386 gnu_type = gnat_to_gnu_type (gnat_desig_type);
4387 gnu_init = convert (gnu_type, gnu_init);
4388 }
4389 else
4390 {
4391 gnu_type = gnat_to_gnu_type (Etype (Expression (gnat_temp)));
4392 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4393 gnu_type = TREE_TYPE (gnu_init);
4394
4395 gnu_init = convert (gnu_type, gnu_init);
4396 }
4397 }
4398 else
4399 gcc_unreachable ();
4400
4401 gnu_result_type = get_unpadded_type (Etype (gnat_node));
4402 return build_allocator (gnu_type, gnu_init, gnu_result_type,
4403 Procedure_To_Call (gnat_node),
4404 Storage_Pool (gnat_node), gnat_node,
4405 ignore_init_type);
4406 }
4407 break;
4408
4409 /**************************/
4410 /* Chapter 5: Statements */
4411 /**************************/
4412
4413 case N_Label:
4414 gnu_result = build1 (LABEL_EXPR, void_type_node,
4415 gnat_to_gnu (Identifier (gnat_node)));
4416 break;
4417
4418 case N_Null_Statement:
4419 gnu_result = alloc_stmt_list ();
4420 break;
4421
4422 case N_Assignment_Statement:
4423 /* Get the LHS and RHS of the statement and convert any reference to an
4424 unconstrained array into a reference to the underlying array.
4425 If we are not to do range checking and the RHS is an N_Function_Call,
4426 pass the LHS to the call function. */
4427 gnu_lhs = maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node)));
4428
4429 /* If the type has a size that overflows, convert this into raise of
4430 Storage_Error: execution shouldn't have gotten here anyway. */
4431 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))) == INTEGER_CST
4432 && TREE_OVERFLOW (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))))
4433 gnu_result = build_call_raise (SE_Object_Too_Large, gnat_node,
4434 N_Raise_Storage_Error);
4435 else if (Nkind (Expression (gnat_node)) == N_Function_Call
4436 && !Do_Range_Check (Expression (gnat_node)))
4437 gnu_result = call_to_gnu (Expression (gnat_node),
4438 &gnu_result_type, gnu_lhs);
4439 else
4440 {
4441 gnu_rhs
4442 = maybe_unconstrained_array (gnat_to_gnu (Expression (gnat_node)));
4443
4444 /* If range check is needed, emit code to generate it. */
4445 if (Do_Range_Check (Expression (gnat_node)))
4446 gnu_rhs = emit_range_check (gnu_rhs, Etype (Name (gnat_node)),
4447 gnat_node);
4448
4449 gnu_result
4450 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_lhs, gnu_rhs);
4451
4452 /* If the type being assigned is an array type and the two sides
4453 are not completely disjoint, play safe and use memmove. */
4454 if (TREE_CODE (gnu_result) == MODIFY_EXPR
4455 && Is_Array_Type (Etype (Name (gnat_node)))
4456 && !(Forwards_OK (gnat_node) && Backwards_OK (gnat_node)))
4457 {
4458 tree to, from, size, to_ptr, from_ptr, t;
4459
4460 to = TREE_OPERAND (gnu_result, 0);
4461 from = TREE_OPERAND (gnu_result, 1);
4462
4463 size = TYPE_SIZE_UNIT (TREE_TYPE (from));
4464 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, from);
4465
4466 to_ptr = build_fold_addr_expr (to);
4467 from_ptr = build_fold_addr_expr (from);
4468
4469 t = implicit_built_in_decls[BUILT_IN_MEMMOVE];
4470 gnu_result = build_call_expr (t, 3, to_ptr, from_ptr, size);
4471 }
4472 }
4473 break;
4474
4475 case N_If_Statement:
4476 {
4477 tree *gnu_else_ptr; /* Point to put next "else if" or "else". */
4478
4479 /* Make the outer COND_EXPR. Avoid non-determinism. */
4480 gnu_result = build3 (COND_EXPR, void_type_node,
4481 gnat_to_gnu (Condition (gnat_node)),
4482 NULL_TREE, NULL_TREE);
4483 COND_EXPR_THEN (gnu_result)
4484 = build_stmt_group (Then_Statements (gnat_node), false);
4485 TREE_SIDE_EFFECTS (gnu_result) = 1;
4486 gnu_else_ptr = &COND_EXPR_ELSE (gnu_result);
4487
4488 /* Now make a COND_EXPR for each of the "else if" parts. Put each
4489 into the previous "else" part and point to where to put any
4490 outer "else". Also avoid non-determinism. */
4491 if (Present (Elsif_Parts (gnat_node)))
4492 for (gnat_temp = First (Elsif_Parts (gnat_node));
4493 Present (gnat_temp); gnat_temp = Next (gnat_temp))
4494 {
4495 gnu_expr = build3 (COND_EXPR, void_type_node,
4496 gnat_to_gnu (Condition (gnat_temp)),
4497 NULL_TREE, NULL_TREE);
4498 COND_EXPR_THEN (gnu_expr)
4499 = build_stmt_group (Then_Statements (gnat_temp), false);
4500 TREE_SIDE_EFFECTS (gnu_expr) = 1;
4501 set_expr_location_from_node (gnu_expr, gnat_temp);
4502 *gnu_else_ptr = gnu_expr;
4503 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
4504 }
4505
4506 *gnu_else_ptr = build_stmt_group (Else_Statements (gnat_node), false);
4507 }
4508 break;
4509
4510 case N_Case_Statement:
4511 gnu_result = Case_Statement_to_gnu (gnat_node);
4512 break;
4513
4514 case N_Loop_Statement:
4515 gnu_result = Loop_Statement_to_gnu (gnat_node);
4516 break;
4517
4518 case N_Block_Statement:
4519 start_stmt_group ();
4520 gnat_pushlevel ();
4521 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
4522 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
4523 gnat_poplevel ();
4524 gnu_result = end_stmt_group ();
4525
4526 if (Present (Identifier (gnat_node)))
4527 mark_out_of_scope (Entity (Identifier (gnat_node)));
4528 break;
4529
4530 case N_Exit_Statement:
4531 gnu_result
4532 = build2 (EXIT_STMT, void_type_node,
4533 (Present (Condition (gnat_node))
4534 ? gnat_to_gnu (Condition (gnat_node)) : NULL_TREE),
4535 (Present (Name (gnat_node))
4536 ? get_gnu_tree (Entity (Name (gnat_node)))
4537 : TREE_VALUE (gnu_loop_label_stack)));
4538 break;
4539
4540 case N_Return_Statement:
4541 {
4542 /* The gnu function type of the subprogram currently processed. */
4543 tree gnu_subprog_type = TREE_TYPE (current_function_decl);
4544 /* The return value from the subprogram. */
4545 tree gnu_ret_val = NULL_TREE;
4546 /* The place to put the return value. */
4547 tree gnu_lhs;
4548
4549 /* If we are dealing with a "return;" from an Ada procedure with
4550 parameters passed by copy in copy out, we need to return a record
4551 containing the final values of these parameters. If the list
4552 contains only one entry, return just that entry.
4553
4554 For a full description of the copy in copy out parameter mechanism,
4555 see the part of the gnat_to_gnu_entity routine dealing with the
4556 translation of subprograms.
4557
4558 But if we have a return label defined, convert this into
4559 a branch to that label. */
4560
4561 if (TREE_VALUE (gnu_return_label_stack))
4562 {
4563 gnu_result = build1 (GOTO_EXPR, void_type_node,
4564 TREE_VALUE (gnu_return_label_stack));
4565 break;
4566 }
4567
4568 else if (TYPE_CI_CO_LIST (gnu_subprog_type))
4569 {
4570 gnu_lhs = DECL_RESULT (current_function_decl);
4571 if (list_length (TYPE_CI_CO_LIST (gnu_subprog_type)) == 1)
4572 gnu_ret_val = TREE_VALUE (TYPE_CI_CO_LIST (gnu_subprog_type));
4573 else
4574 gnu_ret_val
4575 = gnat_build_constructor (TREE_TYPE (gnu_subprog_type),
4576 TYPE_CI_CO_LIST (gnu_subprog_type));
4577 }
4578
4579 /* If the Ada subprogram is a function, we just need to return the
4580 expression. If the subprogram returns an unconstrained
4581 array, we have to allocate a new version of the result and
4582 return it. If we return by reference, return a pointer. */
4583
4584 else if (Present (Expression (gnat_node)))
4585 {
4586 /* If the current function returns by target pointer and we
4587 are doing a call, pass that target to the call. */
4588 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type)
4589 && Nkind (Expression (gnat_node)) == N_Function_Call)
4590 {
4591 gnu_lhs
4592 = build_unary_op (INDIRECT_REF, NULL_TREE,
4593 DECL_ARGUMENTS (current_function_decl));
4594 gnu_result = call_to_gnu (Expression (gnat_node),
4595 &gnu_result_type, gnu_lhs);
4596 }
4597 else
4598 {
4599 gnu_ret_val = gnat_to_gnu (Expression (gnat_node));
4600
4601 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type))
4602 /* The original return type was unconstrained so dereference
4603 the TARGET pointer in the actual return value's type. */
4604 gnu_lhs
4605 = build_unary_op (INDIRECT_REF, TREE_TYPE (gnu_ret_val),
4606 DECL_ARGUMENTS (current_function_decl));
4607 else
4608 gnu_lhs = DECL_RESULT (current_function_decl);
4609
4610 /* Do not remove the padding from GNU_RET_VAL if the inner
4611 type is self-referential since we want to allocate the fixed
4612 size in that case. */
4613 if (TREE_CODE (gnu_ret_val) == COMPONENT_REF
4614 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_ret_val, 0)))
4615 == RECORD_TYPE)
4616 && (TYPE_IS_PADDING_P
4617 (TREE_TYPE (TREE_OPERAND (gnu_ret_val, 0))))
4618 && (CONTAINS_PLACEHOLDER_P
4619 (TYPE_SIZE (TREE_TYPE (gnu_ret_val)))))
4620 gnu_ret_val = TREE_OPERAND (gnu_ret_val, 0);
4621
4622 if (TYPE_RETURNS_BY_REF_P (gnu_subprog_type)
4623 || By_Ref (gnat_node))
4624 gnu_ret_val
4625 = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_ret_val);
4626
4627 else if (TYPE_RETURNS_UNCONSTRAINED_P (gnu_subprog_type))
4628 {
4629 gnu_ret_val = maybe_unconstrained_array (gnu_ret_val);
4630 gnu_ret_val
4631 = build_allocator (TREE_TYPE (gnu_ret_val),
4632 gnu_ret_val,
4633 TREE_TYPE (gnu_subprog_type),
4634 Procedure_To_Call (gnat_node),
4635 Storage_Pool (gnat_node),
4636 gnat_node, false);
4637 }
4638 }
4639 }
4640 else
4641 /* If the Ada subprogram is a regular procedure, just return. */
4642 gnu_lhs = NULL_TREE;
4643
4644 if (TYPE_RETURNS_BY_TARGET_PTR_P (gnu_subprog_type))
4645 {
4646 if (gnu_ret_val)
4647 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
4648 gnu_lhs, gnu_ret_val);
4649 add_stmt_with_node (gnu_result, gnat_node);
4650 gnu_lhs = NULL_TREE;
4651 }
4652
4653 gnu_result = build_return_expr (gnu_lhs, gnu_ret_val);
4654 }
4655 break;
4656
4657 case N_Goto_Statement:
4658 gnu_result = build1 (GOTO_EXPR, void_type_node,
4659 gnat_to_gnu (Name (gnat_node)));
4660 break;
4661
4662 /***************************/
4663 /* Chapter 6: Subprograms */
4664 /***************************/
4665
4666 case N_Subprogram_Declaration:
4667 /* Unless there is a freeze node, declare the subprogram. We consider
4668 this a "definition" even though we're not generating code for
4669 the subprogram because we will be making the corresponding GCC
4670 node here. */
4671
4672 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
4673 gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node)),
4674 NULL_TREE, 1);
4675 gnu_result = alloc_stmt_list ();
4676 break;
4677
4678 case N_Abstract_Subprogram_Declaration:
4679 /* This subprogram doesn't exist for code generation purposes, but we
4680 have to elaborate the types of any parameters and result, unless
4681 they are imported types (nothing to generate in this case). */
4682
4683 /* Process the parameter types first. */
4684
4685 for (gnat_temp
4686 = First_Formal_With_Extras
4687 (Defining_Entity (Specification (gnat_node)));
4688 Present (gnat_temp);
4689 gnat_temp = Next_Formal_With_Extras (gnat_temp))
4690 if (Is_Itype (Etype (gnat_temp))
4691 && !From_With_Type (Etype (gnat_temp)))
4692 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
4693
4694
4695 /* Then the result type, set to Standard_Void_Type for procedures. */
4696
4697 {
4698 Entity_Id gnat_temp_type
4699 = Etype (Defining_Entity (Specification (gnat_node)));
4700
4701 if (Is_Itype (gnat_temp_type) && !From_With_Type (gnat_temp_type))
4702 gnat_to_gnu_entity (Etype (gnat_temp_type), NULL_TREE, 0);
4703 }
4704
4705 gnu_result = alloc_stmt_list ();
4706 break;
4707
4708 case N_Defining_Program_Unit_Name:
4709 /* For a child unit identifier go up a level to get the specification.
4710 We get this when we try to find the spec of a child unit package
4711 that is the compilation unit being compiled. */
4712 gnu_result = gnat_to_gnu (Parent (gnat_node));
4713 break;
4714
4715 case N_Subprogram_Body:
4716 Subprogram_Body_to_gnu (gnat_node);
4717 gnu_result = alloc_stmt_list ();
4718 break;
4719
4720 case N_Function_Call:
4721 case N_Procedure_Call_Statement:
4722 gnu_result = call_to_gnu (gnat_node, &gnu_result_type, NULL_TREE);
4723 break;
4724
4725 /************************/
4726 /* Chapter 7: Packages */
4727 /************************/
4728
4729 case N_Package_Declaration:
4730 gnu_result = gnat_to_gnu (Specification (gnat_node));
4731 break;
4732
4733 case N_Package_Specification:
4734
4735 start_stmt_group ();
4736 process_decls (Visible_Declarations (gnat_node),
4737 Private_Declarations (gnat_node), Empty, true, true);
4738 gnu_result = end_stmt_group ();
4739 break;
4740
4741 case N_Package_Body:
4742
4743 /* If this is the body of a generic package - do nothing. */
4744 if (Ekind (Corresponding_Spec (gnat_node)) == E_Generic_Package)
4745 {
4746 gnu_result = alloc_stmt_list ();
4747 break;
4748 }
4749
4750 start_stmt_group ();
4751 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
4752
4753 if (Present (Handled_Statement_Sequence (gnat_node)))
4754 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
4755
4756 gnu_result = end_stmt_group ();
4757 break;
4758
4759 /********************************/
4760 /* Chapter 8: Visibility Rules */
4761 /********************************/
4762
4763 case N_Use_Package_Clause:
4764 case N_Use_Type_Clause:
4765 /* Nothing to do here - but these may appear in list of declarations. */
4766 gnu_result = alloc_stmt_list ();
4767 break;
4768
4769 /*********************/
4770 /* Chapter 9: Tasks */
4771 /*********************/
4772
4773 case N_Protected_Type_Declaration:
4774 gnu_result = alloc_stmt_list ();
4775 break;
4776
4777 case N_Single_Task_Declaration:
4778 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
4779 gnu_result = alloc_stmt_list ();
4780 break;
4781
4782 /*********************************************************/
4783 /* Chapter 10: Program Structure and Compilation Issues */
4784 /*********************************************************/
4785
4786 case N_Compilation_Unit:
4787
4788 /* This is not called for the main unit, which is handled in function
4789 gigi above. */
4790 start_stmt_group ();
4791 gnat_pushlevel ();
4792
4793 Compilation_Unit_to_gnu (gnat_node);
4794 gnu_result = alloc_stmt_list ();
4795 break;
4796
4797 case N_Subprogram_Body_Stub:
4798 case N_Package_Body_Stub:
4799 case N_Protected_Body_Stub:
4800 case N_Task_Body_Stub:
4801 /* Simply process whatever unit is being inserted. */
4802 gnu_result = gnat_to_gnu (Unit (Library_Unit (gnat_node)));
4803 break;
4804
4805 case N_Subunit:
4806 gnu_result = gnat_to_gnu (Proper_Body (gnat_node));
4807 break;
4808
4809 /***************************/
4810 /* Chapter 11: Exceptions */
4811 /***************************/
4812
4813 case N_Handled_Sequence_Of_Statements:
4814 /* If there is an At_End procedure attached to this node, and the EH
4815 mechanism is SJLJ, we must have at least a corresponding At_End
4816 handler, unless the No_Exception_Handlers restriction is set. */
4817 gcc_assert (type_annotate_only
4818 || Exception_Mechanism != Setjmp_Longjmp
4819 || No (At_End_Proc (gnat_node))
4820 || Present (Exception_Handlers (gnat_node))
4821 || No_Exception_Handlers_Set ());
4822
4823 gnu_result = Handled_Sequence_Of_Statements_to_gnu (gnat_node);
4824 break;
4825
4826 case N_Exception_Handler:
4827 if (Exception_Mechanism == Setjmp_Longjmp)
4828 gnu_result = Exception_Handler_to_gnu_sjlj (gnat_node);
4829 else if (Exception_Mechanism == Back_End_Exceptions)
4830 gnu_result = Exception_Handler_to_gnu_zcx (gnat_node);
4831 else
4832 gcc_unreachable ();
4833
4834 break;
4835
4836 case N_Push_Constraint_Error_Label:
4837 push_exception_label_stack (&gnu_constraint_error_label_stack,
4838 Exception_Label (gnat_node));
4839 break;
4840
4841 case N_Push_Storage_Error_Label:
4842 push_exception_label_stack (&gnu_storage_error_label_stack,
4843 Exception_Label (gnat_node));
4844 break;
4845
4846 case N_Push_Program_Error_Label:
4847 push_exception_label_stack (&gnu_program_error_label_stack,
4848 Exception_Label (gnat_node));
4849 break;
4850
4851 case N_Pop_Constraint_Error_Label:
4852 gnu_constraint_error_label_stack
4853 = TREE_CHAIN (gnu_constraint_error_label_stack);
4854 break;
4855
4856 case N_Pop_Storage_Error_Label:
4857 gnu_storage_error_label_stack
4858 = TREE_CHAIN (gnu_storage_error_label_stack);
4859 break;
4860
4861 case N_Pop_Program_Error_Label:
4862 gnu_program_error_label_stack
4863 = TREE_CHAIN (gnu_program_error_label_stack);
4864 break;
4865
4866 /******************************/
4867 /* Chapter 12: Generic Units */
4868 /******************************/
4869
4870 case N_Generic_Function_Renaming_Declaration:
4871 case N_Generic_Package_Renaming_Declaration:
4872 case N_Generic_Procedure_Renaming_Declaration:
4873 case N_Generic_Package_Declaration:
4874 case N_Generic_Subprogram_Declaration:
4875 case N_Package_Instantiation:
4876 case N_Procedure_Instantiation:
4877 case N_Function_Instantiation:
4878 /* These nodes can appear on a declaration list but there is nothing to
4879 to be done with them. */
4880 gnu_result = alloc_stmt_list ();
4881 break;
4882
4883 /**************************************************/
4884 /* Chapter 13: Representation Clauses and */
4885 /* Implementation-Dependent Features */
4886 /**************************************************/
4887
4888 case N_Attribute_Definition_Clause:
4889 gnu_result = alloc_stmt_list ();
4890
4891 /* The only one we need to deal with is 'Address since, for the others,
4892 the front-end puts the information elsewhere. */
4893 if (Get_Attribute_Id (Chars (gnat_node)) != Attr_Address)
4894 break;
4895
4896 /* And we only deal with 'Address if the object has a Freeze node. */
4897 gnat_temp = Entity (Name (gnat_node));
4898 if (No (Freeze_Node (gnat_temp)))
4899 break;
4900
4901 /* Get the value to use as the address and save it as the equivalent
4902 for the object. When it is frozen, gnat_to_gnu_entity will do the
4903 right thing. */
4904 save_gnu_tree (gnat_temp, gnat_to_gnu (Expression (gnat_node)), true);
4905 break;
4906
4907 case N_Enumeration_Representation_Clause:
4908 case N_Record_Representation_Clause:
4909 case N_At_Clause:
4910 /* We do nothing with these. SEM puts the information elsewhere. */
4911 gnu_result = alloc_stmt_list ();
4912 break;
4913
4914 case N_Code_Statement:
4915 if (!type_annotate_only)
4916 {
4917 tree gnu_template = gnat_to_gnu (Asm_Template (gnat_node));
4918 tree gnu_inputs = NULL_TREE, gnu_outputs = NULL_TREE;
4919 tree gnu_clobbers = NULL_TREE, tail;
4920 bool allows_mem, allows_reg, fake;
4921 int ninputs, noutputs, i;
4922 const char **oconstraints;
4923 const char *constraint;
4924 char *clobber;
4925
4926 /* First retrieve the 3 operand lists built by the front-end. */
4927 Setup_Asm_Outputs (gnat_node);
4928 while (Present (gnat_temp = Asm_Output_Variable ()))
4929 {
4930 tree gnu_value = gnat_to_gnu (gnat_temp);
4931 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
4932 (Asm_Output_Constraint ()));
4933
4934 gnu_outputs = tree_cons (gnu_constr, gnu_value, gnu_outputs);
4935 Next_Asm_Output ();
4936 }
4937
4938 Setup_Asm_Inputs (gnat_node);
4939 while (Present (gnat_temp = Asm_Input_Value ()))
4940 {
4941 tree gnu_value = gnat_to_gnu (gnat_temp);
4942 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
4943 (Asm_Input_Constraint ()));
4944
4945 gnu_inputs = tree_cons (gnu_constr, gnu_value, gnu_inputs);
4946 Next_Asm_Input ();
4947 }
4948
4949 Clobber_Setup (gnat_node);
4950 while ((clobber = Clobber_Get_Next ()))
4951 gnu_clobbers
4952 = tree_cons (NULL_TREE,
4953 build_string (strlen (clobber) + 1, clobber),
4954 gnu_clobbers);
4955
4956 /* Then perform some standard checking and processing on the
4957 operands. In particular, mark them addressable if needed. */
4958 gnu_outputs = nreverse (gnu_outputs);
4959 noutputs = list_length (gnu_outputs);
4960 gnu_inputs = nreverse (gnu_inputs);
4961 ninputs = list_length (gnu_inputs);
4962 oconstraints
4963 = (const char **) alloca (noutputs * sizeof (const char *));
4964
4965 for (i = 0, tail = gnu_outputs; tail; ++i, tail = TREE_CHAIN (tail))
4966 {
4967 tree output = TREE_VALUE (tail);
4968 constraint
4969 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
4970 oconstraints[i] = constraint;
4971
4972 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
4973 &allows_mem, &allows_reg, &fake))
4974 {
4975 /* If the operand is going to end up in memory,
4976 mark it addressable. Note that we don't test
4977 allows_mem like in the input case below; this
4978 is modelled on the C front-end. */
4979 if (!allows_reg
4980 && !gnat_mark_addressable (output))
4981 output = error_mark_node;
4982 }
4983 else
4984 output = error_mark_node;
4985
4986 TREE_VALUE (tail) = output;
4987 }
4988
4989 for (i = 0, tail = gnu_inputs; tail; ++i, tail = TREE_CHAIN (tail))
4990 {
4991 tree input = TREE_VALUE (tail);
4992 constraint
4993 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
4994
4995 if (parse_input_constraint (&constraint, i, ninputs, noutputs,
4996 0, oconstraints,
4997 &allows_mem, &allows_reg))
4998 {
4999 /* If the operand is going to end up in memory,
5000 mark it addressable. */
5001 if (!allows_reg && allows_mem
5002 && !gnat_mark_addressable (input))
5003 input = error_mark_node;
5004 }
5005 else
5006 input = error_mark_node;
5007
5008 TREE_VALUE (tail) = input;
5009 }
5010
5011 gnu_result = build4 (ASM_EXPR, void_type_node,
5012 gnu_template, gnu_outputs,
5013 gnu_inputs, gnu_clobbers);
5014 ASM_VOLATILE_P (gnu_result) = Is_Asm_Volatile (gnat_node);
5015 }
5016 else
5017 gnu_result = alloc_stmt_list ();
5018
5019 break;
5020
5021 /****************/
5022 /* Added Nodes */
5023 /****************/
5024
5025 case N_Freeze_Entity:
5026 start_stmt_group ();
5027 process_freeze_entity (gnat_node);
5028 process_decls (Actions (gnat_node), Empty, Empty, true, true);
5029 gnu_result = end_stmt_group ();
5030 break;
5031
5032 case N_Itype_Reference:
5033 if (!present_gnu_tree (Itype (gnat_node)))
5034 process_type (Itype (gnat_node));
5035
5036 gnu_result = alloc_stmt_list ();
5037 break;
5038
5039 case N_Free_Statement:
5040 if (!type_annotate_only)
5041 {
5042 tree gnu_ptr = gnat_to_gnu (Expression (gnat_node));
5043 tree gnu_ptr_type = TREE_TYPE (gnu_ptr);
5044 tree gnu_obj_type;
5045 tree gnu_actual_obj_type = 0;
5046 tree gnu_obj_size;
5047 unsigned int align;
5048 unsigned int default_allocator_alignment
5049 = get_target_default_allocator_alignment () * BITS_PER_UNIT;
5050
5051 /* If this is a thin pointer, we must dereference it to create
5052 a fat pointer, then go back below to a thin pointer. The
5053 reason for this is that we need a fat pointer someplace in
5054 order to properly compute the size. */
5055 if (TYPE_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
5056 gnu_ptr = build_unary_op (ADDR_EXPR, NULL_TREE,
5057 build_unary_op (INDIRECT_REF, NULL_TREE,
5058 gnu_ptr));
5059
5060 /* If this is an unconstrained array, we know the object must
5061 have been allocated with the template in front of the object.
5062 So pass the template address, but get the total size. Do this
5063 by converting to a thin pointer. */
5064 if (TYPE_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
5065 gnu_ptr
5066 = convert (build_pointer_type
5067 (TYPE_OBJECT_RECORD_TYPE
5068 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
5069 gnu_ptr);
5070
5071 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
5072
5073 if (Present (Actual_Designated_Subtype (gnat_node)))
5074 {
5075 gnu_actual_obj_type
5076 = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_node));
5077
5078 if (TYPE_FAT_OR_THIN_POINTER_P (gnu_ptr_type))
5079 gnu_actual_obj_type
5080 = build_unc_object_type_from_ptr (gnu_ptr_type,
5081 gnu_actual_obj_type,
5082 get_identifier ("DEALLOC"));
5083 }
5084 else
5085 gnu_actual_obj_type = gnu_obj_type;
5086
5087 gnu_obj_size = TYPE_SIZE_UNIT (gnu_actual_obj_type);
5088 align = TYPE_ALIGN (gnu_obj_type);
5089
5090 if (TREE_CODE (gnu_obj_type) == RECORD_TYPE
5091 && TYPE_CONTAINS_TEMPLATE_P (gnu_obj_type))
5092 {
5093 tree gnu_char_ptr_type = build_pointer_type (char_type_node);
5094 tree gnu_pos = byte_position (TYPE_FIELDS (gnu_obj_type));
5095 tree gnu_byte_offset
5096 = convert (sizetype,
5097 size_diffop (size_zero_node, gnu_pos));
5098 gnu_byte_offset = fold_build1 (NEGATE_EXPR, sizetype, gnu_byte_offset);
5099
5100 gnu_ptr = convert (gnu_char_ptr_type, gnu_ptr);
5101 gnu_ptr = build_binary_op (POINTER_PLUS_EXPR, gnu_char_ptr_type,
5102 gnu_ptr, gnu_byte_offset);
5103 }
5104
5105 /* If the object was allocated from the default storage pool, the
5106 alignment was greater than what the allocator provides, and this
5107 is not a fat or thin pointer, what we have in gnu_ptr here is an
5108 address dynamically adjusted to match the alignment requirement
5109 (see build_allocator). What we need to pass to free is the
5110 initial allocator's return value, which has been stored just in
5111 front of the block we have. */
5112
5113 if (No (Procedure_To_Call (gnat_node))
5114 && align > default_allocator_alignment
5115 && ! TYPE_FAT_OR_THIN_POINTER_P (gnu_ptr_type))
5116 {
5117 /* We set GNU_PTR
5118 as * (void **)((void *)GNU_PTR - (void *)sizeof(void *))
5119 in two steps: */
5120
5121 /* GNU_PTR (void *)
5122 = (void *)GNU_PTR - (void *)sizeof (void *)) */
5123 gnu_ptr
5124 = build_binary_op
5125 (POINTER_PLUS_EXPR, ptr_void_type_node,
5126 convert (ptr_void_type_node, gnu_ptr),
5127 size_int (-POINTER_SIZE/BITS_PER_UNIT));
5128
5129 /* GNU_PTR (void *) = *(void **)GNU_PTR */
5130 gnu_ptr
5131 = build_unary_op
5132 (INDIRECT_REF, NULL_TREE,
5133 convert (build_pointer_type (ptr_void_type_node),
5134 gnu_ptr));
5135 }
5136
5137 gnu_result = build_call_alloc_dealloc (gnu_ptr, gnu_obj_size, align,
5138 Procedure_To_Call (gnat_node),
5139 Storage_Pool (gnat_node),
5140 gnat_node);
5141 }
5142 break;
5143
5144 case N_Raise_Constraint_Error:
5145 case N_Raise_Program_Error:
5146 case N_Raise_Storage_Error:
5147 if (type_annotate_only)
5148 {
5149 gnu_result = alloc_stmt_list ();
5150 break;
5151 }
5152
5153 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5154 gnu_result
5155 = build_call_raise (UI_To_Int (Reason (gnat_node)), gnat_node,
5156 Nkind (gnat_node));
5157
5158 /* If the type is VOID, this is a statement, so we need to
5159 generate the code for the call. Handle a Condition, if there
5160 is one. */
5161 if (TREE_CODE (gnu_result_type) == VOID_TYPE)
5162 {
5163 set_expr_location_from_node (gnu_result, gnat_node);
5164
5165 if (Present (Condition (gnat_node)))
5166 gnu_result = build3 (COND_EXPR, void_type_node,
5167 gnat_to_gnu (Condition (gnat_node)),
5168 gnu_result, alloc_stmt_list ());
5169 }
5170 else
5171 gnu_result = build1 (NULL_EXPR, gnu_result_type, gnu_result);
5172 break;
5173
5174 case N_Validate_Unchecked_Conversion:
5175 {
5176 Entity_Id gnat_target_type = Target_Type (gnat_node);
5177 tree gnu_source_type = gnat_to_gnu_type (Source_Type (gnat_node));
5178 tree gnu_target_type = gnat_to_gnu_type (gnat_target_type);
5179
5180 /* No need for any warning in this case. */
5181 if (!flag_strict_aliasing)
5182 ;
5183
5184 /* If the result is a pointer type, see if we are either converting
5185 from a non-pointer or from a pointer to a type with a different
5186 alias set and warn if so. If the result is defined in the same
5187 unit as this unchecked conversion, we can allow this because we
5188 can know to make the pointer type behave properly. */
5189 else if (POINTER_TYPE_P (gnu_target_type)
5190 && !In_Same_Source_Unit (gnat_target_type, gnat_node)
5191 && !No_Strict_Aliasing (Underlying_Type (gnat_target_type)))
5192 {
5193 tree gnu_source_desig_type = POINTER_TYPE_P (gnu_source_type)
5194 ? TREE_TYPE (gnu_source_type)
5195 : NULL_TREE;
5196 tree gnu_target_desig_type = TREE_TYPE (gnu_target_type);
5197
5198 if ((TYPE_DUMMY_P (gnu_target_desig_type)
5199 || get_alias_set (gnu_target_desig_type) != 0)
5200 && (!POINTER_TYPE_P (gnu_source_type)
5201 || (TYPE_DUMMY_P (gnu_source_desig_type)
5202 != TYPE_DUMMY_P (gnu_target_desig_type))
5203 || (TYPE_DUMMY_P (gnu_source_desig_type)
5204 && gnu_source_desig_type != gnu_target_desig_type)
5205 || !alias_sets_conflict_p
5206 (get_alias_set (gnu_source_desig_type),
5207 get_alias_set (gnu_target_desig_type))))
5208 {
5209 post_error_ne
5210 ("?possible aliasing problem for type&",
5211 gnat_node, Target_Type (gnat_node));
5212 post_error
5213 ("\\?use -fno-strict-aliasing switch for references",
5214 gnat_node);
5215 post_error_ne
5216 ("\\?or use `pragma No_Strict_Aliasing (&);`",
5217 gnat_node, Target_Type (gnat_node));
5218 }
5219 }
5220
5221 /* But if the result is a fat pointer type, we have no mechanism to
5222 do that, so we unconditionally warn in problematic cases. */
5223 else if (TYPE_FAT_POINTER_P (gnu_target_type))
5224 {
5225 tree gnu_source_array_type
5226 = TYPE_FAT_POINTER_P (gnu_source_type)
5227 ? TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_source_type)))
5228 : NULL_TREE;
5229 tree gnu_target_array_type
5230 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_target_type)));
5231
5232 if ((TYPE_DUMMY_P (gnu_target_array_type)
5233 || get_alias_set (gnu_target_array_type) != 0)
5234 && (!TYPE_FAT_POINTER_P (gnu_source_type)
5235 || (TYPE_DUMMY_P (gnu_source_array_type)
5236 != TYPE_DUMMY_P (gnu_target_array_type))
5237 || (TYPE_DUMMY_P (gnu_source_array_type)
5238 && gnu_source_array_type != gnu_target_array_type)
5239 || !alias_sets_conflict_p
5240 (get_alias_set (gnu_source_array_type),
5241 get_alias_set (gnu_target_array_type))))
5242 {
5243 post_error_ne
5244 ("?possible aliasing problem for type&",
5245 gnat_node, Target_Type (gnat_node));
5246 post_error
5247 ("\\?use -fno-strict-aliasing switch for references",
5248 gnat_node);
5249 }
5250 }
5251 }
5252 gnu_result = alloc_stmt_list ();
5253 break;
5254
5255 case N_Raise_Statement:
5256 case N_Function_Specification:
5257 case N_Procedure_Specification:
5258 case N_Op_Concat:
5259 case N_Component_Association:
5260 case N_Task_Body:
5261 default:
5262 gcc_assert (type_annotate_only);
5263 gnu_result = alloc_stmt_list ();
5264 }
5265
5266 /* If we pushed our level as part of processing the elaboration routine,
5267 pop it back now. */
5268 if (went_into_elab_proc)
5269 {
5270 add_stmt (gnu_result);
5271 gnat_poplevel ();
5272 gnu_result = end_stmt_group ();
5273 current_function_decl = NULL_TREE;
5274 }
5275
5276 /* Set the location information on the result if it is a real expression.
5277 References can be reused for multiple GNAT nodes and they would get
5278 the location information of their last use. Note that we may have
5279 no result if we tried to build a CALL_EXPR node to a procedure with
5280 no side-effects and optimization is enabled. */
5281 if (gnu_result
5282 && EXPR_P (gnu_result)
5283 && TREE_CODE (gnu_result) != NOP_EXPR
5284 && !REFERENCE_CLASS_P (gnu_result))
5285 set_expr_location_from_node (gnu_result, gnat_node);
5286
5287 /* If we're supposed to return something of void_type, it means we have
5288 something we're elaborating for effect, so just return. */
5289 if (TREE_CODE (gnu_result_type) == VOID_TYPE)
5290 return gnu_result;
5291
5292 /* If the result is a constant that overflows, raise constraint error. */
5293 else if (TREE_CODE (gnu_result) == INTEGER_CST
5294 && TREE_OVERFLOW (gnu_result))
5295 {
5296 post_error ("Constraint_Error will be raised at run-time?", gnat_node);
5297
5298 gnu_result
5299 = build1 (NULL_EXPR, gnu_result_type,
5300 build_call_raise (CE_Overflow_Check_Failed, gnat_node,
5301 N_Raise_Constraint_Error));
5302 }
5303
5304 /* If our result has side-effects and is of an unconstrained type,
5305 make a SAVE_EXPR so that we can be sure it will only be referenced
5306 once. Note we must do this before any conversions. */
5307 if (TREE_SIDE_EFFECTS (gnu_result)
5308 && (TREE_CODE (gnu_result_type) == UNCONSTRAINED_ARRAY_TYPE
5309 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))))
5310 gnu_result = gnat_stabilize_reference (gnu_result, false);
5311
5312 /* Now convert the result to the result type, unless we are in one of the
5313 following cases:
5314
5315 1. If this is the Name of an assignment statement or a parameter of
5316 a procedure call, return the result almost unmodified since the
5317 RHS will have to be converted to our type in that case, unless
5318 the result type has a simpler size. Similarly, don't convert
5319 integral types that are the operands of an unchecked conversion
5320 since we need to ignore those conversions (for 'Valid).
5321
5322 2. If we have a label (which doesn't have any well-defined type), a
5323 field or an error, return the result almost unmodified. Also don't
5324 do the conversion if the result type involves a PLACEHOLDER_EXPR in
5325 its size since those are the cases where the front end may have the
5326 type wrong due to "instantiating" the unconstrained record with
5327 discriminant values. Similarly, if the two types are record types
5328 with the same name don't convert. This will be the case when we are
5329 converting from a packable version of a type to its original type and
5330 we need those conversions to be NOPs in order for assignments into
5331 these types to work properly.
5332
5333 3. If the type is void or if we have no result, return error_mark_node
5334 to show we have no result.
5335
5336 4. Finally, if the type of the result is already correct. */
5337
5338 if (Present (Parent (gnat_node))
5339 && ((Nkind (Parent (gnat_node)) == N_Assignment_Statement
5340 && Name (Parent (gnat_node)) == gnat_node)
5341 || (Nkind (Parent (gnat_node)) == N_Procedure_Call_Statement
5342 && Name (Parent (gnat_node)) != gnat_node)
5343 || Nkind (Parent (gnat_node)) == N_Parameter_Association
5344 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
5345 && !AGGREGATE_TYPE_P (gnu_result_type)
5346 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_result))))
5347 && !(TYPE_SIZE (gnu_result_type)
5348 && TYPE_SIZE (TREE_TYPE (gnu_result))
5349 && (AGGREGATE_TYPE_P (gnu_result_type)
5350 == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)))
5351 && ((TREE_CODE (TYPE_SIZE (gnu_result_type)) == INTEGER_CST
5352 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result)))
5353 != INTEGER_CST))
5354 || (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
5355 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))
5356 && (CONTAINS_PLACEHOLDER_P
5357 (TYPE_SIZE (TREE_TYPE (gnu_result))))))
5358 && !(TREE_CODE (gnu_result_type) == RECORD_TYPE
5359 && TYPE_JUSTIFIED_MODULAR_P (gnu_result_type))))
5360 {
5361 /* Remove padding only if the inner object is of self-referential
5362 size: in that case it must be an object of unconstrained type
5363 with a default discriminant and we want to avoid copying too
5364 much data. */
5365 if (TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE
5366 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result))
5367 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS
5368 (TREE_TYPE (gnu_result))))))
5369 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
5370 gnu_result);
5371 }
5372
5373 else if (TREE_CODE (gnu_result) == LABEL_DECL
5374 || TREE_CODE (gnu_result) == FIELD_DECL
5375 || TREE_CODE (gnu_result) == ERROR_MARK
5376 || (TYPE_SIZE (gnu_result_type)
5377 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
5378 && TREE_CODE (gnu_result) != INDIRECT_REF
5379 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type)))
5380 || ((TYPE_NAME (gnu_result_type)
5381 == TYPE_NAME (TREE_TYPE (gnu_result)))
5382 && TREE_CODE (gnu_result_type) == RECORD_TYPE
5383 && TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE))
5384 {
5385 /* Remove any padding. */
5386 if (TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE
5387 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
5388 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
5389 gnu_result);
5390 }
5391
5392 else if (gnu_result == error_mark_node || gnu_result_type == void_type_node)
5393 gnu_result = error_mark_node;
5394
5395 else if (gnu_result_type != TREE_TYPE (gnu_result))
5396 gnu_result = convert (gnu_result_type, gnu_result);
5397
5398 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on the result. */
5399 while ((TREE_CODE (gnu_result) == NOP_EXPR
5400 || TREE_CODE (gnu_result) == NON_LVALUE_EXPR)
5401 && TREE_TYPE (TREE_OPERAND (gnu_result, 0)) == TREE_TYPE (gnu_result))
5402 gnu_result = TREE_OPERAND (gnu_result, 0);
5403
5404 return gnu_result;
5405 }
5406 \f
5407 /* Subroutine of above to push the exception label stack. GNU_STACK is
5408 a pointer to the stack to update and GNAT_LABEL, if present, is the
5409 label to push onto the stack. */
5410
5411 static void
5412 push_exception_label_stack (tree *gnu_stack, Entity_Id gnat_label)
5413 {
5414 tree gnu_label = (Present (gnat_label)
5415 ? gnat_to_gnu_entity (gnat_label, NULL_TREE, 0)
5416 : NULL_TREE);
5417
5418 *gnu_stack = tree_cons (NULL_TREE, gnu_label, *gnu_stack);
5419 }
5420 \f
5421 /* Record the current code position in GNAT_NODE. */
5422
5423 static void
5424 record_code_position (Node_Id gnat_node)
5425 {
5426 tree stmt_stmt = build1 (STMT_STMT, void_type_node, NULL_TREE);
5427
5428 add_stmt_with_node (stmt_stmt, gnat_node);
5429 save_gnu_tree (gnat_node, stmt_stmt, true);
5430 }
5431
5432 /* Insert the code for GNAT_NODE at the position saved for that node. */
5433
5434 static void
5435 insert_code_for (Node_Id gnat_node)
5436 {
5437 STMT_STMT_STMT (get_gnu_tree (gnat_node)) = gnat_to_gnu (gnat_node);
5438 save_gnu_tree (gnat_node, NULL_TREE, true);
5439 }
5440 \f
5441 /* Start a new statement group chained to the previous group. */
5442
5443 void
5444 start_stmt_group (void)
5445 {
5446 struct stmt_group *group = stmt_group_free_list;
5447
5448 /* First see if we can get one from the free list. */
5449 if (group)
5450 stmt_group_free_list = group->previous;
5451 else
5452 group = (struct stmt_group *) ggc_alloc (sizeof (struct stmt_group));
5453
5454 group->previous = current_stmt_group;
5455 group->stmt_list = group->block = group->cleanups = NULL_TREE;
5456 current_stmt_group = group;
5457 }
5458
5459 /* Add GNU_STMT to the current statement group. */
5460
5461 void
5462 add_stmt (tree gnu_stmt)
5463 {
5464 append_to_statement_list (gnu_stmt, &current_stmt_group->stmt_list);
5465 }
5466
5467 /* Similar, but set the location of GNU_STMT to that of GNAT_NODE. */
5468
5469 void
5470 add_stmt_with_node (tree gnu_stmt, Node_Id gnat_node)
5471 {
5472 if (Present (gnat_node))
5473 set_expr_location_from_node (gnu_stmt, gnat_node);
5474 add_stmt (gnu_stmt);
5475 }
5476
5477 /* Add a declaration statement for GNU_DECL to the current statement group.
5478 Get SLOC from Entity_Id. */
5479
5480 void
5481 add_decl_expr (tree gnu_decl, Entity_Id gnat_entity)
5482 {
5483 tree type = TREE_TYPE (gnu_decl);
5484 tree gnu_stmt, gnu_init, t;
5485
5486 /* If this is a variable that Gigi is to ignore, we may have been given
5487 an ERROR_MARK. So test for it. We also might have been given a
5488 reference for a renaming. So only do something for a decl. Also
5489 ignore a TYPE_DECL for an UNCONSTRAINED_ARRAY_TYPE. */
5490 if (!DECL_P (gnu_decl)
5491 || (TREE_CODE (gnu_decl) == TYPE_DECL
5492 && TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE))
5493 return;
5494
5495 gnu_stmt = build1 (DECL_EXPR, void_type_node, gnu_decl);
5496
5497 /* If we are global, we don't want to actually output the DECL_EXPR for
5498 this decl since we already have evaluated the expressions in the
5499 sizes and positions as globals and doing it again would be wrong. */
5500 if (global_bindings_p ())
5501 {
5502 /* Mark everything as used to prevent node sharing with subprograms.
5503 Note that walk_tree knows how to deal with TYPE_DECL, but neither
5504 VAR_DECL nor CONST_DECL. This appears to be somewhat arbitrary. */
5505 mark_visited (&gnu_stmt);
5506 if (TREE_CODE (gnu_decl) == VAR_DECL
5507 || TREE_CODE (gnu_decl) == CONST_DECL)
5508 {
5509 mark_visited (&DECL_SIZE (gnu_decl));
5510 mark_visited (&DECL_SIZE_UNIT (gnu_decl));
5511 mark_visited (&DECL_INITIAL (gnu_decl));
5512 }
5513 /* In any case, we have to deal with our own TYPE_ADA_SIZE field. */
5514 if (TREE_CODE (gnu_decl) == TYPE_DECL
5515 && (TREE_CODE (type) == RECORD_TYPE
5516 || TREE_CODE (type) == UNION_TYPE
5517 || TREE_CODE (type) == QUAL_UNION_TYPE)
5518 && (t = TYPE_ADA_SIZE (type)))
5519 mark_visited (&t);
5520 }
5521 else
5522 add_stmt_with_node (gnu_stmt, gnat_entity);
5523
5524 /* If this is a variable and an initializer is attached to it, it must be
5525 valid for the context. Similar to init_const in create_var_decl_1. */
5526 if (TREE_CODE (gnu_decl) == VAR_DECL
5527 && (gnu_init = DECL_INITIAL (gnu_decl)) != NULL_TREE
5528 && (!gnat_types_compatible_p (type, TREE_TYPE (gnu_init))
5529 || (TREE_STATIC (gnu_decl)
5530 && !initializer_constant_valid_p (gnu_init,
5531 TREE_TYPE (gnu_init)))))
5532 {
5533 /* If GNU_DECL has a padded type, convert it to the unpadded
5534 type so the assignment is done properly. */
5535 if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5536 t = convert (TREE_TYPE (TYPE_FIELDS (type)), gnu_decl);
5537 else
5538 t = gnu_decl;
5539
5540 gnu_stmt = build_binary_op (MODIFY_EXPR, NULL_TREE, t, gnu_init);
5541
5542 DECL_INITIAL (gnu_decl) = NULL_TREE;
5543 if (TREE_READONLY (gnu_decl))
5544 {
5545 TREE_READONLY (gnu_decl) = 0;
5546 DECL_READONLY_ONCE_ELAB (gnu_decl) = 1;
5547 }
5548
5549 add_stmt_with_node (gnu_stmt, gnat_entity);
5550 }
5551 }
5552
5553 /* Callback for walk_tree to mark the visited trees rooted at *TP. */
5554
5555 static tree
5556 mark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
5557 {
5558 if (TREE_VISITED (*tp))
5559 *walk_subtrees = 0;
5560
5561 /* Don't mark a dummy type as visited because we want to mark its sizes
5562 and fields once it's filled in. */
5563 else if (!TYPE_IS_DUMMY_P (*tp))
5564 TREE_VISITED (*tp) = 1;
5565
5566 if (TYPE_P (*tp))
5567 TYPE_SIZES_GIMPLIFIED (*tp) = 1;
5568
5569 return NULL_TREE;
5570 }
5571
5572 /* Utility function to unshare expressions wrapped up in a SAVE_EXPR. */
5573
5574 static tree
5575 unshare_save_expr (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
5576 void *data ATTRIBUTE_UNUSED)
5577 {
5578 tree t = *tp;
5579
5580 if (TREE_CODE (t) == SAVE_EXPR)
5581 TREE_OPERAND (t, 0) = unshare_expr (TREE_OPERAND (t, 0));
5582
5583 return NULL_TREE;
5584 }
5585
5586 /* Mark nodes rooted at *TP with TREE_VISITED and types as having their
5587 sized gimplified. We use this to indicate all variable sizes and
5588 positions in global types may not be shared by any subprogram. */
5589
5590 void
5591 mark_visited (tree *tp)
5592 {
5593 walk_tree (tp, mark_visited_r, NULL, NULL);
5594 }
5595
5596 /* Add GNU_CLEANUP, a cleanup action, to the current code group and
5597 set its location to that of GNAT_NODE if present. */
5598
5599 static void
5600 add_cleanup (tree gnu_cleanup, Node_Id gnat_node)
5601 {
5602 if (Present (gnat_node))
5603 set_expr_location_from_node (gnu_cleanup, gnat_node);
5604 append_to_statement_list (gnu_cleanup, &current_stmt_group->cleanups);
5605 }
5606
5607 /* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */
5608
5609 void
5610 set_block_for_group (tree gnu_block)
5611 {
5612 gcc_assert (!current_stmt_group->block);
5613 current_stmt_group->block = gnu_block;
5614 }
5615
5616 /* Return code corresponding to the current code group. It is normally
5617 a STATEMENT_LIST, but may also be a BIND_EXPR or TRY_FINALLY_EXPR if
5618 BLOCK or cleanups were set. */
5619
5620 tree
5621 end_stmt_group (void)
5622 {
5623 struct stmt_group *group = current_stmt_group;
5624 tree gnu_retval = group->stmt_list;
5625
5626 /* If this is a null list, allocate a new STATEMENT_LIST. Then, if there
5627 are cleanups, make a TRY_FINALLY_EXPR. Last, if there is a BLOCK,
5628 make a BIND_EXPR. Note that we nest in that because the cleanup may
5629 reference variables in the block. */
5630 if (gnu_retval == NULL_TREE)
5631 gnu_retval = alloc_stmt_list ();
5632
5633 if (group->cleanups)
5634 gnu_retval = build2 (TRY_FINALLY_EXPR, void_type_node, gnu_retval,
5635 group->cleanups);
5636
5637 if (current_stmt_group->block)
5638 gnu_retval = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (group->block),
5639 gnu_retval, group->block);
5640
5641 /* Remove this group from the stack and add it to the free list. */
5642 current_stmt_group = group->previous;
5643 group->previous = stmt_group_free_list;
5644 stmt_group_free_list = group;
5645
5646 return gnu_retval;
5647 }
5648
5649 /* Add a list of statements from GNAT_LIST, a possibly-empty list of
5650 statements.*/
5651
5652 static void
5653 add_stmt_list (List_Id gnat_list)
5654 {
5655 Node_Id gnat_node;
5656
5657 if (Present (gnat_list))
5658 for (gnat_node = First (gnat_list); Present (gnat_node);
5659 gnat_node = Next (gnat_node))
5660 add_stmt (gnat_to_gnu (gnat_node));
5661 }
5662
5663 /* Build a tree from GNAT_LIST, a possibly-empty list of statements.
5664 If BINDING_P is true, push and pop a binding level around the list. */
5665
5666 static tree
5667 build_stmt_group (List_Id gnat_list, bool binding_p)
5668 {
5669 start_stmt_group ();
5670 if (binding_p)
5671 gnat_pushlevel ();
5672
5673 add_stmt_list (gnat_list);
5674 if (binding_p)
5675 gnat_poplevel ();
5676
5677 return end_stmt_group ();
5678 }
5679 \f
5680 /* Push and pop routines for stacks. We keep a free list around so we
5681 don't waste tree nodes. */
5682
5683 static void
5684 push_stack (tree *gnu_stack_ptr, tree gnu_purpose, tree gnu_value)
5685 {
5686 tree gnu_node = gnu_stack_free_list;
5687
5688 if (gnu_node)
5689 {
5690 gnu_stack_free_list = TREE_CHAIN (gnu_node);
5691 TREE_CHAIN (gnu_node) = *gnu_stack_ptr;
5692 TREE_PURPOSE (gnu_node) = gnu_purpose;
5693 TREE_VALUE (gnu_node) = gnu_value;
5694 }
5695 else
5696 gnu_node = tree_cons (gnu_purpose, gnu_value, *gnu_stack_ptr);
5697
5698 *gnu_stack_ptr = gnu_node;
5699 }
5700
5701 static void
5702 pop_stack (tree *gnu_stack_ptr)
5703 {
5704 tree gnu_node = *gnu_stack_ptr;
5705
5706 *gnu_stack_ptr = TREE_CHAIN (gnu_node);
5707 TREE_CHAIN (gnu_node) = gnu_stack_free_list;
5708 gnu_stack_free_list = gnu_node;
5709 }
5710 \f
5711 /* Generate GIMPLE in place for the expression at *EXPR_P. */
5712
5713 int
5714 gnat_gimplify_expr (tree *expr_p, gimple_seq *pre_p,
5715 gimple_seq *post_p ATTRIBUTE_UNUSED)
5716 {
5717 tree expr = *expr_p;
5718 tree op;
5719
5720 if (IS_ADA_STMT (expr))
5721 return gnat_gimplify_stmt (expr_p);
5722
5723 switch (TREE_CODE (expr))
5724 {
5725 case NULL_EXPR:
5726 /* If this is for a scalar, just make a VAR_DECL for it. If for
5727 an aggregate, get a null pointer of the appropriate type and
5728 dereference it. */
5729 if (AGGREGATE_TYPE_P (TREE_TYPE (expr)))
5730 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (expr),
5731 convert (build_pointer_type (TREE_TYPE (expr)),
5732 integer_zero_node));
5733 else
5734 {
5735 *expr_p = create_tmp_var (TREE_TYPE (expr), NULL);
5736 TREE_NO_WARNING (*expr_p) = 1;
5737 }
5738
5739 gimplify_and_add (TREE_OPERAND (expr, 0), pre_p);
5740 return GS_OK;
5741
5742 case UNCONSTRAINED_ARRAY_REF:
5743 /* We should only do this if we are just elaborating for side-effects,
5744 but we can't know that yet. */
5745 *expr_p = TREE_OPERAND (*expr_p, 0);
5746 return GS_OK;
5747
5748 case ADDR_EXPR:
5749 op = TREE_OPERAND (expr, 0);
5750
5751 /* If we're taking the address of a constant CONSTRUCTOR, force it to
5752 be put into static memory. We know it's going to be readonly given
5753 the semantics we have and it's required to be static memory in
5754 the case when the reference is in an elaboration procedure. */
5755 if (TREE_CODE (op) == CONSTRUCTOR && TREE_CONSTANT (op))
5756 {
5757 tree new_var = create_tmp_var (TREE_TYPE (op), "C");
5758
5759 TREE_READONLY (new_var) = 1;
5760 TREE_STATIC (new_var) = 1;
5761 TREE_ADDRESSABLE (new_var) = 1;
5762 DECL_INITIAL (new_var) = op;
5763
5764 TREE_OPERAND (expr, 0) = new_var;
5765 recompute_tree_invariant_for_addr_expr (expr);
5766 return GS_ALL_DONE;
5767 }
5768
5769 /* If we are taking the address of a SAVE_EXPR, we are typically
5770 processing a misaligned argument to be passed by reference in a
5771 procedure call. We just mark the operand as addressable + not
5772 readonly here and let the common gimplifier code perform the
5773 temporary creation, initialization, and "instantiation" in place of
5774 the SAVE_EXPR in further operands, in particular in the copy back
5775 code inserted after the call. */
5776 else if (TREE_CODE (op) == SAVE_EXPR)
5777 {
5778 TREE_ADDRESSABLE (op) = 1;
5779 TREE_READONLY (op) = 0;
5780 }
5781
5782 /* We let the gimplifier process &COND_EXPR and expect it to yield the
5783 address of the selected operand when it is addressable. Besides, we
5784 also expect addressable_p to only let COND_EXPRs where both arms are
5785 addressable reach here. */
5786 else if (TREE_CODE (op) == COND_EXPR)
5787 ;
5788
5789 /* Otherwise, if we are taking the address of something that is neither
5790 reference, declaration, or constant, make a variable for the operand
5791 here and then take its address. If we don't do it this way, we may
5792 confuse the gimplifier because it needs to know the variable is
5793 addressable at this point. This duplicates code in
5794 internal_get_tmp_var, which is unfortunate. */
5795 else if (TREE_CODE_CLASS (TREE_CODE (op)) != tcc_reference
5796 && TREE_CODE_CLASS (TREE_CODE (op)) != tcc_declaration
5797 && TREE_CODE_CLASS (TREE_CODE (op)) != tcc_constant)
5798 {
5799 tree new_var = create_tmp_var (TREE_TYPE (op), "A");
5800 gimple stmt;
5801
5802 TREE_ADDRESSABLE (new_var) = 1;
5803
5804 stmt = gimplify_assign (new_var, op, pre_p);
5805 if (EXPR_HAS_LOCATION (op))
5806 gimple_set_location (stmt, *EXPR_LOCUS (op));
5807
5808 TREE_OPERAND (expr, 0) = new_var;
5809 recompute_tree_invariant_for_addr_expr (expr);
5810 return GS_ALL_DONE;
5811 }
5812
5813 /* ... fall through ... */
5814
5815 default:
5816 return GS_UNHANDLED;
5817 }
5818 }
5819
5820 /* Generate GIMPLE in place for the statement at *STMT_P. */
5821
5822 static enum gimplify_status
5823 gnat_gimplify_stmt (tree *stmt_p)
5824 {
5825 tree stmt = *stmt_p;
5826
5827 switch (TREE_CODE (stmt))
5828 {
5829 case STMT_STMT:
5830 *stmt_p = STMT_STMT_STMT (stmt);
5831 return GS_OK;
5832
5833 case LOOP_STMT:
5834 {
5835 tree gnu_start_label = create_artificial_label ();
5836 tree gnu_end_label = LOOP_STMT_LABEL (stmt);
5837 tree t;
5838
5839 /* Set to emit the statements of the loop. */
5840 *stmt_p = NULL_TREE;
5841
5842 /* We first emit the start label and then a conditional jump to
5843 the end label if there's a top condition, then the body of the
5844 loop, then a conditional branch to the end label, then the update,
5845 if any, and finally a jump to the start label and the definition
5846 of the end label. */
5847 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
5848 gnu_start_label),
5849 stmt_p);
5850
5851 if (LOOP_STMT_TOP_COND (stmt))
5852 append_to_statement_list (build3 (COND_EXPR, void_type_node,
5853 LOOP_STMT_TOP_COND (stmt),
5854 alloc_stmt_list (),
5855 build1 (GOTO_EXPR,
5856 void_type_node,
5857 gnu_end_label)),
5858 stmt_p);
5859
5860 append_to_statement_list (LOOP_STMT_BODY (stmt), stmt_p);
5861
5862 if (LOOP_STMT_BOT_COND (stmt))
5863 append_to_statement_list (build3 (COND_EXPR, void_type_node,
5864 LOOP_STMT_BOT_COND (stmt),
5865 alloc_stmt_list (),
5866 build1 (GOTO_EXPR,
5867 void_type_node,
5868 gnu_end_label)),
5869 stmt_p);
5870
5871 if (LOOP_STMT_UPDATE (stmt))
5872 append_to_statement_list (LOOP_STMT_UPDATE (stmt), stmt_p);
5873
5874 t = build1 (GOTO_EXPR, void_type_node, gnu_start_label);
5875 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (gnu_end_label));
5876 append_to_statement_list (t, stmt_p);
5877
5878 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
5879 gnu_end_label),
5880 stmt_p);
5881 return GS_OK;
5882 }
5883
5884 case EXIT_STMT:
5885 /* Build a statement to jump to the corresponding end label, then
5886 see if it needs to be conditional. */
5887 *stmt_p = build1 (GOTO_EXPR, void_type_node, EXIT_STMT_LABEL (stmt));
5888 if (EXIT_STMT_COND (stmt))
5889 *stmt_p = build3 (COND_EXPR, void_type_node,
5890 EXIT_STMT_COND (stmt), *stmt_p, alloc_stmt_list ());
5891 return GS_OK;
5892
5893 default:
5894 gcc_unreachable ();
5895 }
5896 }
5897 \f
5898 /* Force references to each of the entities in packages withed by GNAT_NODE.
5899 Operate recursively but check that we aren't elaborating something more
5900 than once.
5901
5902 This routine is exclusively called in type_annotate mode, to compute DDA
5903 information for types in withed units, for ASIS use. */
5904
5905 static void
5906 elaborate_all_entities (Node_Id gnat_node)
5907 {
5908 Entity_Id gnat_with_clause, gnat_entity;
5909
5910 /* Process each unit only once. As we trace the context of all relevant
5911 units transitively, including generic bodies, we may encounter the
5912 same generic unit repeatedly. */
5913 if (!present_gnu_tree (gnat_node))
5914 save_gnu_tree (gnat_node, integer_zero_node, true);
5915
5916 /* Save entities in all context units. A body may have an implicit_with
5917 on its own spec, if the context includes a child unit, so don't save
5918 the spec twice. */
5919 for (gnat_with_clause = First (Context_Items (gnat_node));
5920 Present (gnat_with_clause);
5921 gnat_with_clause = Next (gnat_with_clause))
5922 if (Nkind (gnat_with_clause) == N_With_Clause
5923 && !present_gnu_tree (Library_Unit (gnat_with_clause))
5924 && Library_Unit (gnat_with_clause) != Library_Unit (Cunit (Main_Unit)))
5925 {
5926 elaborate_all_entities (Library_Unit (gnat_with_clause));
5927
5928 if (Ekind (Entity (Name (gnat_with_clause))) == E_Package)
5929 {
5930 for (gnat_entity = First_Entity (Entity (Name (gnat_with_clause)));
5931 Present (gnat_entity);
5932 gnat_entity = Next_Entity (gnat_entity))
5933 if (Is_Public (gnat_entity)
5934 && Convention (gnat_entity) != Convention_Intrinsic
5935 && Ekind (gnat_entity) != E_Package
5936 && Ekind (gnat_entity) != E_Package_Body
5937 && Ekind (gnat_entity) != E_Operator
5938 && !(IN (Ekind (gnat_entity), Type_Kind)
5939 && !Is_Frozen (gnat_entity))
5940 && !((Ekind (gnat_entity) == E_Procedure
5941 || Ekind (gnat_entity) == E_Function)
5942 && Is_Intrinsic_Subprogram (gnat_entity))
5943 && !IN (Ekind (gnat_entity), Named_Kind)
5944 && !IN (Ekind (gnat_entity), Generic_Unit_Kind))
5945 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5946 }
5947 else if (Ekind (Entity (Name (gnat_with_clause))) == E_Generic_Package)
5948 {
5949 Node_Id gnat_body
5950 = Corresponding_Body (Unit (Library_Unit (gnat_with_clause)));
5951
5952 /* Retrieve compilation unit node of generic body. */
5953 while (Present (gnat_body)
5954 && Nkind (gnat_body) != N_Compilation_Unit)
5955 gnat_body = Parent (gnat_body);
5956
5957 /* If body is available, elaborate its context. */
5958 if (Present (gnat_body))
5959 elaborate_all_entities (gnat_body);
5960 }
5961 }
5962
5963 if (Nkind (Unit (gnat_node)) == N_Package_Body)
5964 elaborate_all_entities (Library_Unit (gnat_node));
5965 }
5966 \f
5967 /* Do the processing of N_Freeze_Entity, GNAT_NODE. */
5968
5969 static void
5970 process_freeze_entity (Node_Id gnat_node)
5971 {
5972 Entity_Id gnat_entity = Entity (gnat_node);
5973 tree gnu_old;
5974 tree gnu_new;
5975 tree gnu_init
5976 = (Nkind (Declaration_Node (gnat_entity)) == N_Object_Declaration
5977 && present_gnu_tree (Declaration_Node (gnat_entity)))
5978 ? get_gnu_tree (Declaration_Node (gnat_entity)) : NULL_TREE;
5979
5980 /* If this is a package, need to generate code for the package. */
5981 if (Ekind (gnat_entity) == E_Package)
5982 {
5983 insert_code_for
5984 (Parent (Corresponding_Body
5985 (Parent (Declaration_Node (gnat_entity)))));
5986 return;
5987 }
5988
5989 /* Check for old definition after the above call. This Freeze_Node
5990 might be for one its Itypes. */
5991 gnu_old
5992 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : 0;
5993
5994 /* If this entity has an Address representation clause, GNU_OLD is the
5995 address, so discard it here. */
5996 if (Present (Address_Clause (gnat_entity)))
5997 gnu_old = 0;
5998
5999 /* Don't do anything for class-wide types they are always
6000 transformed into their root type. */
6001 if (Ekind (gnat_entity) == E_Class_Wide_Type
6002 || (Ekind (gnat_entity) == E_Class_Wide_Subtype
6003 && Present (Equivalent_Type (gnat_entity))))
6004 return;
6005
6006 /* Don't do anything for subprograms that may have been elaborated before
6007 their freeze nodes. This can happen, for example because of an inner call
6008 in an instance body, or a previous compilation of a spec for inlining
6009 purposes. */
6010 if (gnu_old
6011 && ((TREE_CODE (gnu_old) == FUNCTION_DECL
6012 && (Ekind (gnat_entity) == E_Function
6013 || Ekind (gnat_entity) == E_Procedure))
6014 || (gnu_old
6015 && TREE_CODE (TREE_TYPE (gnu_old)) == FUNCTION_TYPE
6016 && Ekind (gnat_entity) == E_Subprogram_Type)))
6017 return;
6018
6019 /* If we have a non-dummy type old tree, we have nothing to do, except
6020 aborting if this is the public view of a private type whose full view was
6021 not delayed, as this node was never delayed as it should have been. We
6022 let this happen for concurrent types and their Corresponding_Record_Type,
6023 however, because each might legitimately be elaborated before it's own
6024 freeze node, e.g. while processing the other. */
6025 if (gnu_old
6026 && !(TREE_CODE (gnu_old) == TYPE_DECL
6027 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old))))
6028 {
6029 gcc_assert ((IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
6030 && Present (Full_View (gnat_entity))
6031 && No (Freeze_Node (Full_View (gnat_entity))))
6032 || Is_Concurrent_Type (gnat_entity)
6033 || (IN (Ekind (gnat_entity), Record_Kind)
6034 && Is_Concurrent_Record_Type (gnat_entity)));
6035 return;
6036 }
6037
6038 /* Reset the saved tree, if any, and elaborate the object or type for real.
6039 If there is a full declaration, elaborate it and copy the type to
6040 GNAT_ENTITY. Likewise if this is the record subtype corresponding to
6041 a class wide type or subtype. */
6042 if (gnu_old)
6043 {
6044 save_gnu_tree (gnat_entity, NULL_TREE, false);
6045 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
6046 && Present (Full_View (gnat_entity))
6047 && present_gnu_tree (Full_View (gnat_entity)))
6048 save_gnu_tree (Full_View (gnat_entity), NULL_TREE, false);
6049 if (Present (Class_Wide_Type (gnat_entity))
6050 && Class_Wide_Type (gnat_entity) != gnat_entity)
6051 save_gnu_tree (Class_Wide_Type (gnat_entity), NULL_TREE, false);
6052 }
6053
6054 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
6055 && Present (Full_View (gnat_entity)))
6056 {
6057 gnu_new = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 1);
6058
6059 /* Propagate back-annotations from full view to partial view. */
6060 if (Unknown_Alignment (gnat_entity))
6061 Set_Alignment (gnat_entity, Alignment (Full_View (gnat_entity)));
6062
6063 if (Unknown_Esize (gnat_entity))
6064 Set_Esize (gnat_entity, Esize (Full_View (gnat_entity)));
6065
6066 if (Unknown_RM_Size (gnat_entity))
6067 Set_RM_Size (gnat_entity, RM_Size (Full_View (gnat_entity)));
6068
6069 /* The above call may have defined this entity (the simplest example
6070 of this is when we have a private enumeral type since the bounds
6071 will have the public view. */
6072 if (!present_gnu_tree (gnat_entity))
6073 save_gnu_tree (gnat_entity, gnu_new, false);
6074 if (Present (Class_Wide_Type (gnat_entity))
6075 && Class_Wide_Type (gnat_entity) != gnat_entity)
6076 save_gnu_tree (Class_Wide_Type (gnat_entity), gnu_new, false);
6077 }
6078 else
6079 gnu_new = gnat_to_gnu_entity (gnat_entity, gnu_init, 1);
6080
6081 /* If we've made any pointers to the old version of this type, we
6082 have to update them. */
6083 if (gnu_old)
6084 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
6085 TREE_TYPE (gnu_new));
6086 }
6087 \f
6088 /* Process the list of inlined subprograms of GNAT_NODE, which is an
6089 N_Compilation_Unit. */
6090
6091 static void
6092 process_inlined_subprograms (Node_Id gnat_node)
6093 {
6094 Entity_Id gnat_entity;
6095 Node_Id gnat_body;
6096
6097 /* If we can inline, generate Gimple for all the inlined subprograms.
6098 Define the entity first so we set DECL_EXTERNAL. */
6099 if (optimize > 0)
6100 for (gnat_entity = First_Inlined_Subprogram (gnat_node);
6101 Present (gnat_entity);
6102 gnat_entity = Next_Inlined_Subprogram (gnat_entity))
6103 {
6104 gnat_body = Parent (Declaration_Node (gnat_entity));
6105
6106 if (Nkind (gnat_body) != N_Subprogram_Body)
6107 {
6108 /* ??? This really should always be Present. */
6109 if (No (Corresponding_Body (gnat_body)))
6110 continue;
6111
6112 gnat_body
6113 = Parent (Declaration_Node (Corresponding_Body (gnat_body)));
6114 }
6115
6116 if (Present (gnat_body))
6117 {
6118 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
6119 add_stmt (gnat_to_gnu (gnat_body));
6120 }
6121 }
6122 }
6123 \f
6124 /* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present.
6125 We make two passes, one to elaborate anything other than bodies (but
6126 we declare a function if there was no spec). The second pass
6127 elaborates the bodies.
6128
6129 GNAT_END_LIST gives the element in the list past the end. Normally,
6130 this is Empty, but can be First_Real_Statement for a
6131 Handled_Sequence_Of_Statements.
6132
6133 We make a complete pass through both lists if PASS1P is true, then make
6134 the second pass over both lists if PASS2P is true. The lists usually
6135 correspond to the public and private parts of a package. */
6136
6137 static void
6138 process_decls (List_Id gnat_decls, List_Id gnat_decls2,
6139 Node_Id gnat_end_list, bool pass1p, bool pass2p)
6140 {
6141 List_Id gnat_decl_array[2];
6142 Node_Id gnat_decl;
6143 int i;
6144
6145 gnat_decl_array[0] = gnat_decls, gnat_decl_array[1] = gnat_decls2;
6146
6147 if (pass1p)
6148 for (i = 0; i <= 1; i++)
6149 if (Present (gnat_decl_array[i]))
6150 for (gnat_decl = First (gnat_decl_array[i]);
6151 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
6152 {
6153 /* For package specs, we recurse inside the declarations,
6154 thus taking the two pass approach inside the boundary. */
6155 if (Nkind (gnat_decl) == N_Package_Declaration
6156 && (Nkind (Specification (gnat_decl)
6157 == N_Package_Specification)))
6158 process_decls (Visible_Declarations (Specification (gnat_decl)),
6159 Private_Declarations (Specification (gnat_decl)),
6160 Empty, true, false);
6161
6162 /* Similarly for any declarations in the actions of a
6163 freeze node. */
6164 else if (Nkind (gnat_decl) == N_Freeze_Entity)
6165 {
6166 process_freeze_entity (gnat_decl);
6167 process_decls (Actions (gnat_decl), Empty, Empty, true, false);
6168 }
6169
6170 /* Package bodies with freeze nodes get their elaboration deferred
6171 until the freeze node, but the code must be placed in the right
6172 place, so record the code position now. */
6173 else if (Nkind (gnat_decl) == N_Package_Body
6174 && Present (Freeze_Node (Corresponding_Spec (gnat_decl))))
6175 record_code_position (gnat_decl);
6176
6177 else if (Nkind (gnat_decl) == N_Package_Body_Stub
6178 && Present (Library_Unit (gnat_decl))
6179 && Present (Freeze_Node
6180 (Corresponding_Spec
6181 (Proper_Body (Unit
6182 (Library_Unit (gnat_decl)))))))
6183 record_code_position
6184 (Proper_Body (Unit (Library_Unit (gnat_decl))));
6185
6186 /* We defer most subprogram bodies to the second pass. */
6187 else if (Nkind (gnat_decl) == N_Subprogram_Body)
6188 {
6189 if (Acts_As_Spec (gnat_decl))
6190 {
6191 Node_Id gnat_subprog_id = Defining_Entity (gnat_decl);
6192
6193 if (Ekind (gnat_subprog_id) != E_Generic_Procedure
6194 && Ekind (gnat_subprog_id) != E_Generic_Function)
6195 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
6196 }
6197 }
6198
6199 /* For bodies and stubs that act as their own specs, the entity
6200 itself must be elaborated in the first pass, because it may
6201 be used in other declarations. */
6202 else if (Nkind (gnat_decl) == N_Subprogram_Body_Stub)
6203 {
6204 Node_Id gnat_subprog_id
6205 = Defining_Entity (Specification (gnat_decl));
6206
6207 if (Ekind (gnat_subprog_id) != E_Subprogram_Body
6208 && Ekind (gnat_subprog_id) != E_Generic_Procedure
6209 && Ekind (gnat_subprog_id) != E_Generic_Function)
6210 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
6211 }
6212
6213 /* Concurrent stubs stand for the corresponding subprogram bodies,
6214 which are deferred like other bodies. */
6215 else if (Nkind (gnat_decl) == N_Task_Body_Stub
6216 || Nkind (gnat_decl) == N_Protected_Body_Stub)
6217 ;
6218
6219 else
6220 add_stmt (gnat_to_gnu (gnat_decl));
6221 }
6222
6223 /* Here we elaborate everything we deferred above except for package bodies,
6224 which are elaborated at their freeze nodes. Note that we must also
6225 go inside things (package specs and freeze nodes) the first pass did. */
6226 if (pass2p)
6227 for (i = 0; i <= 1; i++)
6228 if (Present (gnat_decl_array[i]))
6229 for (gnat_decl = First (gnat_decl_array[i]);
6230 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
6231 {
6232 if (Nkind (gnat_decl) == N_Subprogram_Body
6233 || Nkind (gnat_decl) == N_Subprogram_Body_Stub
6234 || Nkind (gnat_decl) == N_Task_Body_Stub
6235 || Nkind (gnat_decl) == N_Protected_Body_Stub)
6236 add_stmt (gnat_to_gnu (gnat_decl));
6237
6238 else if (Nkind (gnat_decl) == N_Package_Declaration
6239 && (Nkind (Specification (gnat_decl)
6240 == N_Package_Specification)))
6241 process_decls (Visible_Declarations (Specification (gnat_decl)),
6242 Private_Declarations (Specification (gnat_decl)),
6243 Empty, false, true);
6244
6245 else if (Nkind (gnat_decl) == N_Freeze_Entity)
6246 process_decls (Actions (gnat_decl), Empty, Empty, false, true);
6247 }
6248 }
6249 \f
6250 /* Make a unary operation of kind CODE using build_unary_op, but guard
6251 the operation by an overflow check. CODE can be one of NEGATE_EXPR
6252 or ABS_EXPR. GNU_TYPE is the type desired for the result. Usually
6253 the operation is to be performed in that type. GNAT_NODE is the gnat
6254 node conveying the source location for which the error should be
6255 signaled. */
6256
6257 static tree
6258 build_unary_op_trapv (enum tree_code code, tree gnu_type, tree operand,
6259 Node_Id gnat_node)
6260 {
6261 gcc_assert (code == NEGATE_EXPR || code == ABS_EXPR);
6262
6263 operand = protect_multiple_eval (operand);
6264
6265 return emit_check (build_binary_op (EQ_EXPR, integer_type_node,
6266 operand, TYPE_MIN_VALUE (gnu_type)),
6267 build_unary_op (code, gnu_type, operand),
6268 CE_Overflow_Check_Failed, gnat_node);
6269 }
6270
6271 /* Make a binary operation of kind CODE using build_binary_op, but guard
6272 the operation by an overflow check. CODE can be one of PLUS_EXPR,
6273 MINUS_EXPR or MULT_EXPR. GNU_TYPE is the type desired for the result.
6274 Usually the operation is to be performed in that type. GNAT_NODE is
6275 the GNAT node conveying the source location for which the error should
6276 be signaled. */
6277
6278 static tree
6279 build_binary_op_trapv (enum tree_code code, tree gnu_type, tree left,
6280 tree right, Node_Id gnat_node)
6281 {
6282 tree lhs = protect_multiple_eval (left);
6283 tree rhs = protect_multiple_eval (right);
6284 tree type_max = TYPE_MAX_VALUE (gnu_type);
6285 tree type_min = TYPE_MIN_VALUE (gnu_type);
6286 tree gnu_expr;
6287 tree tmp1, tmp2;
6288 tree zero = convert (gnu_type, integer_zero_node);
6289 tree rhs_lt_zero;
6290 tree check_pos;
6291 tree check_neg;
6292 tree check;
6293 int precision = TYPE_PRECISION (gnu_type);
6294
6295 gcc_assert (!(precision & (precision - 1))); /* ensure power of 2 */
6296
6297 /* Prefer a constant or known-positive rhs to simplify checks. */
6298 if (!TREE_CONSTANT (rhs)
6299 && commutative_tree_code (code)
6300 && (TREE_CONSTANT (lhs) || (!tree_expr_nonnegative_p (rhs)
6301 && tree_expr_nonnegative_p (lhs))))
6302 {
6303 tree tmp = lhs;
6304 lhs = rhs;
6305 rhs = tmp;
6306 }
6307
6308 rhs_lt_zero = tree_expr_nonnegative_p (rhs)
6309 ? integer_zero_node
6310 : build_binary_op (LT_EXPR, integer_type_node, rhs, zero);
6311
6312 /* ??? Should use more efficient check for operand_equal_p (lhs, rhs, 0) */
6313
6314 /* Try a few strategies that may be cheaper than the general
6315 code at the end of the function, if the rhs is not known.
6316 The strategies are:
6317 - Call library function for 64-bit multiplication (complex)
6318 - Widen, if input arguments are sufficiently small
6319 - Determine overflow using wrapped result for addition/subtraction. */
6320
6321 if (!TREE_CONSTANT (rhs))
6322 {
6323 /* Even for add/subtract double size to get another base type. */
6324 int needed_precision = precision * 2;
6325
6326 if (code == MULT_EXPR && precision == 64)
6327 {
6328 tree int_64 = gnat_type_for_size (64, 0);
6329
6330 return convert (gnu_type, build_call_2_expr (mulv64_decl,
6331 convert (int_64, lhs),
6332 convert (int_64, rhs)));
6333 }
6334
6335 else if (needed_precision <= BITS_PER_WORD
6336 || (code == MULT_EXPR
6337 && needed_precision <= LONG_LONG_TYPE_SIZE))
6338 {
6339 tree wide_type = gnat_type_for_size (needed_precision, 0);
6340
6341 tree wide_result = build_binary_op (code, wide_type,
6342 convert (wide_type, lhs),
6343 convert (wide_type, rhs));
6344
6345 tree check = build_binary_op
6346 (TRUTH_ORIF_EXPR, integer_type_node,
6347 build_binary_op (LT_EXPR, integer_type_node, wide_result,
6348 convert (wide_type, type_min)),
6349 build_binary_op (GT_EXPR, integer_type_node, wide_result,
6350 convert (wide_type, type_max)));
6351
6352 tree result = convert (gnu_type, wide_result);
6353
6354 return
6355 emit_check (check, result, CE_Overflow_Check_Failed, gnat_node);
6356 }
6357
6358 else if (code == PLUS_EXPR || code == MINUS_EXPR)
6359 {
6360 tree unsigned_type = gnat_type_for_size (precision, 1);
6361 tree wrapped_expr = convert
6362 (gnu_type, build_binary_op (code, unsigned_type,
6363 convert (unsigned_type, lhs),
6364 convert (unsigned_type, rhs)));
6365
6366 tree result = convert
6367 (gnu_type, build_binary_op (code, gnu_type, lhs, rhs));
6368
6369 /* Overflow when (rhs < 0) ^ (wrapped_expr < lhs)), for addition
6370 or when (rhs < 0) ^ (wrapped_expr > lhs) for subtraction. */
6371 tree check = build_binary_op
6372 (TRUTH_XOR_EXPR, integer_type_node, rhs_lt_zero,
6373 build_binary_op (code == PLUS_EXPR ? LT_EXPR : GT_EXPR,
6374 integer_type_node, wrapped_expr, lhs));
6375
6376 return
6377 emit_check (check, result, CE_Overflow_Check_Failed, gnat_node);
6378 }
6379 }
6380
6381 switch (code)
6382 {
6383 case PLUS_EXPR:
6384 /* When rhs >= 0, overflow when lhs > type_max - rhs. */
6385 check_pos = build_binary_op (GT_EXPR, integer_type_node, lhs,
6386 build_binary_op (MINUS_EXPR, gnu_type,
6387 type_max, rhs)),
6388
6389 /* When rhs < 0, overflow when lhs < type_min - rhs. */
6390 check_neg = build_binary_op (LT_EXPR, integer_type_node, lhs,
6391 build_binary_op (MINUS_EXPR, gnu_type,
6392 type_min, rhs));
6393 break;
6394
6395 case MINUS_EXPR:
6396 /* When rhs >= 0, overflow when lhs < type_min + rhs. */
6397 check_pos = build_binary_op (LT_EXPR, integer_type_node, lhs,
6398 build_binary_op (PLUS_EXPR, gnu_type,
6399 type_min, rhs)),
6400
6401 /* When rhs < 0, overflow when lhs > type_max + rhs. */
6402 check_neg = build_binary_op (GT_EXPR, integer_type_node, lhs,
6403 build_binary_op (PLUS_EXPR, gnu_type,
6404 type_max, rhs));
6405 break;
6406
6407 case MULT_EXPR:
6408 /* The check here is designed to be efficient if the rhs is constant,
6409 but it will work for any rhs by using integer division.
6410 Four different check expressions determine wether X * C overflows,
6411 depending on C.
6412 C == 0 => false
6413 C > 0 => X > type_max / C || X < type_min / C
6414 C == -1 => X == type_min
6415 C < -1 => X > type_min / C || X < type_max / C */
6416
6417 tmp1 = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs);
6418 tmp2 = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs);
6419
6420 check_pos = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
6421 build_binary_op (NE_EXPR, integer_type_node, zero, rhs),
6422 build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
6423 build_binary_op (GT_EXPR, integer_type_node, lhs, tmp1),
6424 build_binary_op (LT_EXPR, integer_type_node, lhs, tmp2)));
6425
6426 check_neg = fold_build3 (COND_EXPR, integer_type_node,
6427 build_binary_op (EQ_EXPR, integer_type_node, rhs,
6428 build_int_cst (gnu_type, -1)),
6429 build_binary_op (EQ_EXPR, integer_type_node, lhs, type_min),
6430 build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
6431 build_binary_op (GT_EXPR, integer_type_node, lhs, tmp2),
6432 build_binary_op (LT_EXPR, integer_type_node, lhs, tmp1)));
6433 break;
6434
6435 default:
6436 gcc_unreachable();
6437 }
6438
6439 gnu_expr = build_binary_op (code, gnu_type, lhs, rhs);
6440
6441 /* If we can fold the expression to a constant, just return it.
6442 The caller will deal with overflow, no need to generate a check. */
6443 if (TREE_CONSTANT (gnu_expr))
6444 return gnu_expr;
6445
6446 check = fold_build3 (COND_EXPR, integer_type_node,
6447 rhs_lt_zero, check_neg, check_pos);
6448
6449 return emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
6450 }
6451
6452 /* Emit code for a range check. GNU_EXPR is the expression to be checked,
6453 GNAT_RANGE_TYPE the gnat type or subtype containing the bounds against
6454 which we have to check. GNAT_NODE is the GNAT node conveying the source
6455 location for which the error should be signaled. */
6456
6457 static tree
6458 emit_range_check (tree gnu_expr, Entity_Id gnat_range_type, Node_Id gnat_node)
6459 {
6460 tree gnu_range_type = get_unpadded_type (gnat_range_type);
6461 tree gnu_low = TYPE_MIN_VALUE (gnu_range_type);
6462 tree gnu_high = TYPE_MAX_VALUE (gnu_range_type);
6463 tree gnu_compare_type = get_base_type (TREE_TYPE (gnu_expr));
6464
6465 /* If GNU_EXPR has GNAT_RANGE_TYPE as its base type, no check is needed.
6466 This can for example happen when translating 'Val or 'Value. */
6467 if (gnu_compare_type == gnu_range_type)
6468 return gnu_expr;
6469
6470 /* If GNU_EXPR has an integral type that is narrower than GNU_RANGE_TYPE,
6471 we can't do anything since we might be truncating the bounds. No
6472 check is needed in this case. */
6473 if (INTEGRAL_TYPE_P (TREE_TYPE (gnu_expr))
6474 && (TYPE_PRECISION (gnu_compare_type)
6475 < TYPE_PRECISION (get_base_type (gnu_range_type))))
6476 return gnu_expr;
6477
6478 /* Checked expressions must be evaluated only once. */
6479 gnu_expr = protect_multiple_eval (gnu_expr);
6480
6481 /* There's no good type to use here, so we might as well use
6482 integer_type_node. Note that the form of the check is
6483 (not (expr >= lo)) or (not (expr <= hi))
6484 the reason for this slightly convoluted form is that NaNs
6485 are not considered to be in range in the float case. */
6486 return emit_check
6487 (build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
6488 invert_truthvalue
6489 (build_binary_op (GE_EXPR, integer_type_node,
6490 convert (gnu_compare_type, gnu_expr),
6491 convert (gnu_compare_type, gnu_low))),
6492 invert_truthvalue
6493 (build_binary_op (LE_EXPR, integer_type_node,
6494 convert (gnu_compare_type, gnu_expr),
6495 convert (gnu_compare_type,
6496 gnu_high)))),
6497 gnu_expr, CE_Range_Check_Failed, gnat_node);
6498 }
6499 \f
6500 /* Emit code for an index check. GNU_ARRAY_OBJECT is the array object which
6501 we are about to index, GNU_EXPR is the index expression to be checked,
6502 GNU_LOW and GNU_HIGH are the lower and upper bounds against which GNU_EXPR
6503 has to be checked. Note that for index checking we cannot simply use the
6504 emit_range_check function (although very similar code needs to be generated
6505 in both cases) since for index checking the array type against which we are
6506 checking the indices may be unconstrained and consequently we need to get
6507 the actual index bounds from the array object itself (GNU_ARRAY_OBJECT).
6508 The place where we need to do that is in subprograms having unconstrained
6509 array formal parameters. GNAT_NODE is the GNAT node conveying the source
6510 location for which the error should be signaled. */
6511
6512 static tree
6513 emit_index_check (tree gnu_array_object, tree gnu_expr, tree gnu_low,
6514 tree gnu_high, Node_Id gnat_node)
6515 {
6516 tree gnu_expr_check;
6517
6518 /* Checked expressions must be evaluated only once. */
6519 gnu_expr = protect_multiple_eval (gnu_expr);
6520
6521 /* Must do this computation in the base type in case the expression's
6522 type is an unsigned subtypes. */
6523 gnu_expr_check = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
6524
6525 /* If GNU_LOW or GNU_HIGH are a PLACEHOLDER_EXPR, qualify them by
6526 the object we are handling. */
6527 gnu_low = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_low, gnu_array_object);
6528 gnu_high = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_high, gnu_array_object);
6529
6530 /* There's no good type to use here, so we might as well use
6531 integer_type_node. */
6532 return emit_check
6533 (build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
6534 build_binary_op (LT_EXPR, integer_type_node,
6535 gnu_expr_check,
6536 convert (TREE_TYPE (gnu_expr_check),
6537 gnu_low)),
6538 build_binary_op (GT_EXPR, integer_type_node,
6539 gnu_expr_check,
6540 convert (TREE_TYPE (gnu_expr_check),
6541 gnu_high))),
6542 gnu_expr, CE_Index_Check_Failed, gnat_node);
6543 }
6544 \f
6545 /* GNU_COND contains the condition corresponding to an access, discriminant or
6546 range check of value GNU_EXPR. Build a COND_EXPR that returns GNU_EXPR if
6547 GNU_COND is false and raises a CONSTRAINT_ERROR if GNU_COND is true.
6548 REASON is the code that says why the exception was raised. GNAT_NODE is
6549 the GNAT node conveying the source location for which the error should be
6550 signaled. */
6551
6552 static tree
6553 emit_check (tree gnu_cond, tree gnu_expr, int reason, Node_Id gnat_node)
6554 {
6555 tree gnu_call
6556 = build_call_raise (reason, gnat_node, N_Raise_Constraint_Error);
6557 tree gnu_result
6558 = fold_build3 (COND_EXPR, TREE_TYPE (gnu_expr), gnu_cond,
6559 build2 (COMPOUND_EXPR, TREE_TYPE (gnu_expr), gnu_call,
6560 convert (TREE_TYPE (gnu_expr), integer_zero_node)),
6561 gnu_expr);
6562
6563 /* GNU_RESULT has side effects if and only if GNU_EXPR has:
6564 we don't need to evaluate it just for the check. */
6565 TREE_SIDE_EFFECTS (gnu_result) = TREE_SIDE_EFFECTS (gnu_expr);
6566
6567 /* ??? Unfortunately, if we don't put a SAVE_EXPR around this whole thing,
6568 we will repeatedly do the test and, at compile time, we will repeatedly
6569 visit it during unsharing, which leads to an exponential explosion. */
6570 return save_expr (gnu_result);
6571 }
6572 \f
6573 /* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing overflow
6574 checks if OVERFLOW_P is true and range checks if RANGE_P is true.
6575 GNAT_TYPE is known to be an integral type. If TRUNCATE_P true, do a
6576 float to integer conversion with truncation; otherwise round.
6577 GNAT_NODE is the GNAT node conveying the source location for which the
6578 error should be signaled. */
6579
6580 static tree
6581 convert_with_check (Entity_Id gnat_type, tree gnu_expr, bool overflowp,
6582 bool rangep, bool truncatep, Node_Id gnat_node)
6583 {
6584 tree gnu_type = get_unpadded_type (gnat_type);
6585 tree gnu_in_type = TREE_TYPE (gnu_expr);
6586 tree gnu_in_basetype = get_base_type (gnu_in_type);
6587 tree gnu_base_type = get_base_type (gnu_type);
6588 tree gnu_result = gnu_expr;
6589
6590 /* If we are not doing any checks, the output is an integral type, and
6591 the input is not a floating type, just do the conversion. This
6592 shortcut is required to avoid problems with packed array types
6593 and simplifies code in all cases anyway. */
6594 if (!rangep && !overflowp && INTEGRAL_TYPE_P (gnu_base_type)
6595 && !FLOAT_TYPE_P (gnu_in_type))
6596 return convert (gnu_type, gnu_expr);
6597
6598 /* First convert the expression to its base type. This
6599 will never generate code, but makes the tests below much simpler.
6600 But don't do this if converting from an integer type to an unconstrained
6601 array type since then we need to get the bounds from the original
6602 (unpacked) type. */
6603 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
6604 gnu_result = convert (gnu_in_basetype, gnu_result);
6605
6606 /* If overflow checks are requested, we need to be sure the result will
6607 fit in the output base type. But don't do this if the input
6608 is integer and the output floating-point. */
6609 if (overflowp
6610 && !(FLOAT_TYPE_P (gnu_base_type) && INTEGRAL_TYPE_P (gnu_in_basetype)))
6611 {
6612 /* Ensure GNU_EXPR only gets evaluated once. */
6613 tree gnu_input = protect_multiple_eval (gnu_result);
6614 tree gnu_cond = integer_zero_node;
6615 tree gnu_in_lb = TYPE_MIN_VALUE (gnu_in_basetype);
6616 tree gnu_in_ub = TYPE_MAX_VALUE (gnu_in_basetype);
6617 tree gnu_out_lb = TYPE_MIN_VALUE (gnu_base_type);
6618 tree gnu_out_ub = TYPE_MAX_VALUE (gnu_base_type);
6619
6620 /* Convert the lower bounds to signed types, so we're sure we're
6621 comparing them properly. Likewise, convert the upper bounds
6622 to unsigned types. */
6623 if (INTEGRAL_TYPE_P (gnu_in_basetype) && TYPE_UNSIGNED (gnu_in_basetype))
6624 gnu_in_lb = convert (gnat_signed_type (gnu_in_basetype), gnu_in_lb);
6625
6626 if (INTEGRAL_TYPE_P (gnu_in_basetype)
6627 && !TYPE_UNSIGNED (gnu_in_basetype))
6628 gnu_in_ub = convert (gnat_unsigned_type (gnu_in_basetype), gnu_in_ub);
6629
6630 if (INTEGRAL_TYPE_P (gnu_base_type) && TYPE_UNSIGNED (gnu_base_type))
6631 gnu_out_lb = convert (gnat_signed_type (gnu_base_type), gnu_out_lb);
6632
6633 if (INTEGRAL_TYPE_P (gnu_base_type) && !TYPE_UNSIGNED (gnu_base_type))
6634 gnu_out_ub = convert (gnat_unsigned_type (gnu_base_type), gnu_out_ub);
6635
6636 /* Check each bound separately and only if the result bound
6637 is tighter than the bound on the input type. Note that all the
6638 types are base types, so the bounds must be constant. Also,
6639 the comparison is done in the base type of the input, which
6640 always has the proper signedness. First check for input
6641 integer (which means output integer), output float (which means
6642 both float), or mixed, in which case we always compare.
6643 Note that we have to do the comparison which would *fail* in the
6644 case of an error since if it's an FP comparison and one of the
6645 values is a NaN or Inf, the comparison will fail. */
6646 if (INTEGRAL_TYPE_P (gnu_in_basetype)
6647 ? tree_int_cst_lt (gnu_in_lb, gnu_out_lb)
6648 : (FLOAT_TYPE_P (gnu_base_type)
6649 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_in_lb),
6650 TREE_REAL_CST (gnu_out_lb))
6651 : 1))
6652 gnu_cond
6653 = invert_truthvalue
6654 (build_binary_op (GE_EXPR, integer_type_node,
6655 gnu_input, convert (gnu_in_basetype,
6656 gnu_out_lb)));
6657
6658 if (INTEGRAL_TYPE_P (gnu_in_basetype)
6659 ? tree_int_cst_lt (gnu_out_ub, gnu_in_ub)
6660 : (FLOAT_TYPE_P (gnu_base_type)
6661 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_out_ub),
6662 TREE_REAL_CST (gnu_in_lb))
6663 : 1))
6664 gnu_cond
6665 = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node, gnu_cond,
6666 invert_truthvalue
6667 (build_binary_op (LE_EXPR, integer_type_node,
6668 gnu_input,
6669 convert (gnu_in_basetype,
6670 gnu_out_ub))));
6671
6672 if (!integer_zerop (gnu_cond))
6673 gnu_result = emit_check (gnu_cond, gnu_input,
6674 CE_Overflow_Check_Failed, gnat_node);
6675 }
6676
6677 /* Now convert to the result base type. If this is a non-truncating
6678 float-to-integer conversion, round. */
6679 if (INTEGRAL_TYPE_P (gnu_base_type) && FLOAT_TYPE_P (gnu_in_basetype)
6680 && !truncatep)
6681 {
6682 REAL_VALUE_TYPE half_minus_pred_half, pred_half;
6683 tree gnu_conv, gnu_zero, gnu_comp, gnu_saved_result, calc_type;
6684 tree gnu_pred_half, gnu_add_pred_half, gnu_subtract_pred_half;
6685 const struct real_format *fmt;
6686
6687 /* The following calculations depend on proper rounding to even
6688 of each arithmetic operation. In order to prevent excess
6689 precision from spoiling this property, use the widest hardware
6690 floating-point type if FP_ARITH_MAY_WIDEN is true. */
6691 calc_type
6692 = FP_ARITH_MAY_WIDEN ? longest_float_type_node : gnu_in_basetype;
6693
6694 /* FIXME: Should not have padding in the first place. */
6695 if (TREE_CODE (calc_type) == RECORD_TYPE
6696 && TYPE_IS_PADDING_P (calc_type))
6697 calc_type = TREE_TYPE (TYPE_FIELDS (calc_type));
6698
6699 /* Compute the exact value calc_type'Pred (0.5) at compile time. */
6700 fmt = REAL_MODE_FORMAT (TYPE_MODE (calc_type));
6701 real_2expN (&half_minus_pred_half, -(fmt->p) - 1, TYPE_MODE (calc_type));
6702 REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf,
6703 half_minus_pred_half);
6704 gnu_pred_half = build_real (calc_type, pred_half);
6705
6706 /* If the input is strictly negative, subtract this value
6707 and otherwise add it from the input. For 0.5, the result
6708 is exactly between 1.0 and the machine number preceding 1.0
6709 (for calc_type). Since the last bit of 1.0 is even, this 0.5
6710 will round to 1.0, while all other number with an absolute
6711 value less than 0.5 round to 0.0. For larger numbers exactly
6712 halfway between integers, rounding will always be correct as
6713 the true mathematical result will be closer to the higher
6714 integer compared to the lower one. So, this constant works
6715 for all floating-point numbers.
6716
6717 The reason to use the same constant with subtract/add instead
6718 of a positive and negative constant is to allow the comparison
6719 to be scheduled in parallel with retrieval of the constant and
6720 conversion of the input to the calc_type (if necessary). */
6721
6722 gnu_zero = convert (gnu_in_basetype, integer_zero_node);
6723 gnu_saved_result = save_expr (gnu_result);
6724 gnu_conv = convert (calc_type, gnu_saved_result);
6725 gnu_comp = build2 (GE_EXPR, integer_type_node,
6726 gnu_saved_result, gnu_zero);
6727 gnu_add_pred_half
6728 = build2 (PLUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
6729 gnu_subtract_pred_half
6730 = build2 (MINUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
6731 gnu_result = build3 (COND_EXPR, calc_type, gnu_comp,
6732 gnu_add_pred_half, gnu_subtract_pred_half);
6733 }
6734
6735 if (TREE_CODE (gnu_base_type) == INTEGER_TYPE
6736 && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_base_type)
6737 && TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
6738 gnu_result = unchecked_convert (gnu_base_type, gnu_result, false);
6739 else
6740 gnu_result = convert (gnu_base_type, gnu_result);
6741
6742 /* Finally, do the range check if requested. Note that if the
6743 result type is a modular type, the range check is actually
6744 an overflow check. */
6745
6746 if (rangep
6747 || (TREE_CODE (gnu_base_type) == INTEGER_TYPE
6748 && TYPE_MODULAR_P (gnu_base_type) && overflowp))
6749 gnu_result = emit_range_check (gnu_result, gnat_type, gnat_node);
6750
6751 return convert (gnu_type, gnu_result);
6752 }
6753 \f
6754 /* Return true if TYPE is a smaller packable version of RECORD_TYPE. */
6755
6756 static bool
6757 smaller_packable_type_p (tree type, tree record_type)
6758 {
6759 tree size, rsize;
6760
6761 /* We're not interested in variants here. */
6762 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (record_type))
6763 return false;
6764
6765 /* Like a variant, a packable version keeps the original TYPE_NAME. */
6766 if (TYPE_NAME (type) != TYPE_NAME (record_type))
6767 return false;
6768
6769 size = TYPE_SIZE (type);
6770 rsize = TYPE_SIZE (record_type);
6771
6772 if (!(TREE_CODE (size) == INTEGER_CST && TREE_CODE (rsize) == INTEGER_CST))
6773 return false;
6774
6775 return tree_int_cst_lt (size, rsize) != 0;
6776 }
6777
6778 /* Return true if GNU_EXPR can be directly addressed. This is the case
6779 unless it is an expression involving computation or if it involves a
6780 reference to a bitfield or to an object not sufficiently aligned for
6781 its type. If GNU_TYPE is non-null, return true only if GNU_EXPR can
6782 be directly addressed as an object of this type.
6783
6784 *** Notes on addressability issues in the Ada compiler ***
6785
6786 This predicate is necessary in order to bridge the gap between Gigi
6787 and the middle-end about addressability of GENERIC trees. A tree
6788 is said to be addressable if it can be directly addressed, i.e. if
6789 its address can be taken, is a multiple of the type's alignment on
6790 strict-alignment architectures and returns the first storage unit
6791 assigned to the object represented by the tree.
6792
6793 In the C family of languages, everything is in practice addressable
6794 at the language level, except for bit-fields. This means that these
6795 compilers will take the address of any tree that doesn't represent
6796 a bit-field reference and expect the result to be the first storage
6797 unit assigned to the object. Even in cases where this will result
6798 in unaligned accesses at run time, nothing is supposed to be done
6799 and the program is considered as erroneous instead (see PR c/18287).
6800
6801 The implicit assumptions made in the middle-end are in keeping with
6802 the C viewpoint described above:
6803 - the address of a bit-field reference is supposed to be never
6804 taken; the compiler (generally) will stop on such a construct,
6805 - any other tree is addressable if it is formally addressable,
6806 i.e. if it is formally allowed to be the operand of ADDR_EXPR.
6807
6808 In Ada, the viewpoint is the opposite one: nothing is addressable
6809 at the language level unless explicitly declared so. This means
6810 that the compiler will both make sure that the trees representing
6811 references to addressable ("aliased" in Ada parlance) objects are
6812 addressable and make no real attempts at ensuring that the trees
6813 representing references to non-addressable objects are addressable.
6814
6815 In the first case, Ada is effectively equivalent to C and handing
6816 down the direct result of applying ADDR_EXPR to these trees to the
6817 middle-end works flawlessly. In the second case, Ada cannot afford
6818 to consider the program as erroneous if the address of trees that
6819 are not addressable is requested for technical reasons, unlike C;
6820 as a consequence, the Ada compiler must arrange for either making
6821 sure that this address is not requested in the middle-end or for
6822 compensating by inserting temporaries if it is requested in Gigi.
6823
6824 The first goal can be achieved because the middle-end should not
6825 request the address of non-addressable trees on its own; the only
6826 exception is for the invocation of low-level block operations like
6827 memcpy, for which the addressability requirements are lower since
6828 the type's alignment can be disregarded. In practice, this means
6829 that Gigi must make sure that such operations cannot be applied to
6830 non-BLKmode bit-fields.
6831
6832 The second goal is achieved by means of the addressable_p predicate
6833 and by inserting SAVE_EXPRs around trees deemed non-addressable.
6834 They will be turned during gimplification into proper temporaries
6835 whose address will be used in lieu of that of the original tree. */
6836
6837 static bool
6838 addressable_p (tree gnu_expr, tree gnu_type)
6839 {
6840 /* The size of the real type of the object must not be smaller than
6841 that of the expected type, otherwise an indirect access in the
6842 latter type would be larger than the object. Only records need
6843 to be considered in practice. */
6844 if (gnu_type
6845 && TREE_CODE (gnu_type) == RECORD_TYPE
6846 && smaller_packable_type_p (TREE_TYPE (gnu_expr), gnu_type))
6847 return false;
6848
6849 switch (TREE_CODE (gnu_expr))
6850 {
6851 case VAR_DECL:
6852 case PARM_DECL:
6853 case FUNCTION_DECL:
6854 case RESULT_DECL:
6855 /* All DECLs are addressable: if they are in a register, we can force
6856 them to memory. */
6857 return true;
6858
6859 case UNCONSTRAINED_ARRAY_REF:
6860 case INDIRECT_REF:
6861 case CONSTRUCTOR:
6862 case STRING_CST:
6863 case INTEGER_CST:
6864 case NULL_EXPR:
6865 case SAVE_EXPR:
6866 case CALL_EXPR:
6867 return true;
6868
6869 case COND_EXPR:
6870 /* We accept &COND_EXPR as soon as both operands are addressable and
6871 expect the outcome to be the address of the selected operand. */
6872 return (addressable_p (TREE_OPERAND (gnu_expr, 1), NULL_TREE)
6873 && addressable_p (TREE_OPERAND (gnu_expr, 2), NULL_TREE));
6874
6875 case COMPONENT_REF:
6876 return (((!DECL_BIT_FIELD (TREE_OPERAND (gnu_expr, 1))
6877 /* Even with DECL_BIT_FIELD cleared, we have to ensure that
6878 the field is sufficiently aligned, in case it is subject
6879 to a pragma Component_Alignment. But we don't need to
6880 check the alignment of the containing record, as it is
6881 guaranteed to be not smaller than that of its most
6882 aligned field that is not a bit-field. */
6883 && (!STRICT_ALIGNMENT
6884 || DECL_ALIGN (TREE_OPERAND (gnu_expr, 1))
6885 >= TYPE_ALIGN (TREE_TYPE (gnu_expr))))
6886 /* The field of a padding record is always addressable. */
6887 || TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
6888 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
6889
6890 case ARRAY_REF: case ARRAY_RANGE_REF:
6891 case REALPART_EXPR: case IMAGPART_EXPR:
6892 case NOP_EXPR:
6893 return addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE);
6894
6895 case CONVERT_EXPR:
6896 return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr))
6897 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
6898
6899 case VIEW_CONVERT_EXPR:
6900 {
6901 /* This is addressable if we can avoid a copy. */
6902 tree type = TREE_TYPE (gnu_expr);
6903 tree inner_type = TREE_TYPE (TREE_OPERAND (gnu_expr, 0));
6904 return (((TYPE_MODE (type) == TYPE_MODE (inner_type)
6905 && (!STRICT_ALIGNMENT
6906 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
6907 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT))
6908 || ((TYPE_MODE (type) == BLKmode
6909 || TYPE_MODE (inner_type) == BLKmode)
6910 && (!STRICT_ALIGNMENT
6911 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
6912 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT
6913 || TYPE_ALIGN_OK (type)
6914 || TYPE_ALIGN_OK (inner_type))))
6915 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
6916 }
6917
6918 default:
6919 return false;
6920 }
6921 }
6922 \f
6923 /* Do the processing for the declaration of a GNAT_ENTITY, a type. If
6924 a separate Freeze node exists, delay the bulk of the processing. Otherwise
6925 make a GCC type for GNAT_ENTITY and set up the correspondence. */
6926
6927 void
6928 process_type (Entity_Id gnat_entity)
6929 {
6930 tree gnu_old
6931 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : 0;
6932 tree gnu_new;
6933
6934 /* If we are to delay elaboration of this type, just do any
6935 elaborations needed for expressions within the declaration and
6936 make a dummy type entry for this node and its Full_View (if
6937 any) in case something points to it. Don't do this if it
6938 has already been done (the only way that can happen is if
6939 the private completion is also delayed). */
6940 if (Present (Freeze_Node (gnat_entity))
6941 || (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
6942 && Present (Full_View (gnat_entity))
6943 && Freeze_Node (Full_View (gnat_entity))
6944 && !present_gnu_tree (Full_View (gnat_entity))))
6945 {
6946 elaborate_entity (gnat_entity);
6947
6948 if (!gnu_old)
6949 {
6950 tree gnu_decl = TYPE_STUB_DECL (make_dummy_type (gnat_entity));
6951 save_gnu_tree (gnat_entity, gnu_decl, false);
6952 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
6953 && Present (Full_View (gnat_entity)))
6954 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
6955 }
6956
6957 return;
6958 }
6959
6960 /* If we saved away a dummy type for this node it means that this
6961 made the type that corresponds to the full type of an incomplete
6962 type. Clear that type for now and then update the type in the
6963 pointers. */
6964 if (gnu_old)
6965 {
6966 gcc_assert (TREE_CODE (gnu_old) == TYPE_DECL
6967 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old)));
6968
6969 save_gnu_tree (gnat_entity, NULL_TREE, false);
6970 }
6971
6972 /* Now fully elaborate the type. */
6973 gnu_new = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 1);
6974 gcc_assert (TREE_CODE (gnu_new) == TYPE_DECL);
6975
6976 /* If we have an old type and we've made pointers to this type,
6977 update those pointers. */
6978 if (gnu_old)
6979 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
6980 TREE_TYPE (gnu_new));
6981
6982 /* If this is a record type corresponding to a task or protected type
6983 that is a completion of an incomplete type, perform a similar update
6984 on the type. ??? Including protected types here is a guess. */
6985 if (IN (Ekind (gnat_entity), Record_Kind)
6986 && Is_Concurrent_Record_Type (gnat_entity)
6987 && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity)))
6988 {
6989 tree gnu_task_old
6990 = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity));
6991
6992 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
6993 NULL_TREE, false);
6994 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
6995 gnu_new, false);
6996
6997 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old)),
6998 TREE_TYPE (gnu_new));
6999 }
7000 }
7001 \f
7002 /* GNAT_ENTITY is the type of the resulting constructors,
7003 GNAT_ASSOC is the front of the Component_Associations of an N_Aggregate,
7004 and GNU_TYPE is the GCC type of the corresponding record.
7005
7006 Return a CONSTRUCTOR to build the record. */
7007
7008 static tree
7009 assoc_to_constructor (Entity_Id gnat_entity, Node_Id gnat_assoc, tree gnu_type)
7010 {
7011 tree gnu_list, gnu_result;
7012
7013 /* We test for GNU_FIELD being empty in the case where a variant
7014 was the last thing since we don't take things off GNAT_ASSOC in
7015 that case. We check GNAT_ASSOC in case we have a variant, but it
7016 has no fields. */
7017
7018 for (gnu_list = NULL_TREE; Present (gnat_assoc);
7019 gnat_assoc = Next (gnat_assoc))
7020 {
7021 Node_Id gnat_field = First (Choices (gnat_assoc));
7022 tree gnu_field = gnat_to_gnu_field_decl (Entity (gnat_field));
7023 tree gnu_expr = gnat_to_gnu (Expression (gnat_assoc));
7024
7025 /* The expander is supposed to put a single component selector name
7026 in every record component association. */
7027 gcc_assert (No (Next (gnat_field)));
7028
7029 /* Ignore fields that have Corresponding_Discriminants since we'll
7030 be setting that field in the parent. */
7031 if (Present (Corresponding_Discriminant (Entity (gnat_field)))
7032 && Is_Tagged_Type (Scope (Entity (gnat_field))))
7033 continue;
7034
7035 /* Also ignore discriminants of Unchecked_Unions. */
7036 else if (Is_Unchecked_Union (gnat_entity)
7037 && Ekind (Entity (gnat_field)) == E_Discriminant)
7038 continue;
7039
7040 /* Before assigning a value in an aggregate make sure range checks
7041 are done if required. Then convert to the type of the field. */
7042 if (Do_Range_Check (Expression (gnat_assoc)))
7043 gnu_expr = emit_range_check (gnu_expr, Etype (gnat_field), Empty);
7044
7045 gnu_expr = convert (TREE_TYPE (gnu_field), gnu_expr);
7046
7047 /* Add the field and expression to the list. */
7048 gnu_list = tree_cons (gnu_field, gnu_expr, gnu_list);
7049 }
7050
7051 gnu_result = extract_values (gnu_list, gnu_type);
7052
7053 #ifdef ENABLE_CHECKING
7054 {
7055 tree gnu_field;
7056
7057 /* Verify every entry in GNU_LIST was used. */
7058 for (gnu_field = gnu_list; gnu_field; gnu_field = TREE_CHAIN (gnu_field))
7059 gcc_assert (TREE_ADDRESSABLE (gnu_field));
7060 }
7061 #endif
7062
7063 return gnu_result;
7064 }
7065
7066 /* Build a possibly nested constructor for array aggregates. GNAT_EXPR is
7067 the first element of an array aggregate. It may itself be an aggregate.
7068 GNU_ARRAY_TYPE is the GCC type corresponding to the array aggregate.
7069 GNAT_COMPONENT_TYPE is the type of the array component; it is needed
7070 for range checking. */
7071
7072 static tree
7073 pos_to_constructor (Node_Id gnat_expr, tree gnu_array_type,
7074 Entity_Id gnat_component_type)
7075 {
7076 tree gnu_expr_list = NULL_TREE;
7077 tree gnu_index = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type));
7078 tree gnu_expr;
7079
7080 for ( ; Present (gnat_expr); gnat_expr = Next (gnat_expr))
7081 {
7082 /* If the expression is itself an array aggregate then first build the
7083 innermost constructor if it is part of our array (multi-dimensional
7084 case). */
7085 if (Nkind (gnat_expr) == N_Aggregate
7086 && TREE_CODE (TREE_TYPE (gnu_array_type)) == ARRAY_TYPE
7087 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type)))
7088 gnu_expr = pos_to_constructor (First (Expressions (gnat_expr)),
7089 TREE_TYPE (gnu_array_type),
7090 gnat_component_type);
7091 else
7092 {
7093 gnu_expr = gnat_to_gnu (gnat_expr);
7094
7095 /* Before assigning the element to the array, make sure it is
7096 in range. */
7097 if (Do_Range_Check (gnat_expr))
7098 gnu_expr = emit_range_check (gnu_expr, gnat_component_type, Empty);
7099 }
7100
7101 gnu_expr_list
7102 = tree_cons (gnu_index, convert (TREE_TYPE (gnu_array_type), gnu_expr),
7103 gnu_expr_list);
7104
7105 gnu_index = int_const_binop (PLUS_EXPR, gnu_index, integer_one_node, 0);
7106 }
7107
7108 return gnat_build_constructor (gnu_array_type, nreverse (gnu_expr_list));
7109 }
7110 \f
7111 /* Subroutine of assoc_to_constructor: VALUES is a list of field associations,
7112 some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting
7113 of the associations that are from RECORD_TYPE. If we see an internal
7114 record, make a recursive call to fill it in as well. */
7115
7116 static tree
7117 extract_values (tree values, tree record_type)
7118 {
7119 tree result = NULL_TREE;
7120 tree field, tem;
7121
7122 for (field = TYPE_FIELDS (record_type); field; field = TREE_CHAIN (field))
7123 {
7124 tree value = 0;
7125
7126 /* _Parent is an internal field, but may have values in the aggregate,
7127 so check for values first. */
7128 if ((tem = purpose_member (field, values)))
7129 {
7130 value = TREE_VALUE (tem);
7131 TREE_ADDRESSABLE (tem) = 1;
7132 }
7133
7134 else if (DECL_INTERNAL_P (field))
7135 {
7136 value = extract_values (values, TREE_TYPE (field));
7137 if (TREE_CODE (value) == CONSTRUCTOR
7138 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (value)))
7139 value = 0;
7140 }
7141 else
7142 /* If we have a record subtype, the names will match, but not the
7143 actual FIELD_DECLs. */
7144 for (tem = values; tem; tem = TREE_CHAIN (tem))
7145 if (DECL_NAME (TREE_PURPOSE (tem)) == DECL_NAME (field))
7146 {
7147 value = convert (TREE_TYPE (field), TREE_VALUE (tem));
7148 TREE_ADDRESSABLE (tem) = 1;
7149 }
7150
7151 if (!value)
7152 continue;
7153
7154 result = tree_cons (field, value, result);
7155 }
7156
7157 return gnat_build_constructor (record_type, nreverse (result));
7158 }
7159 \f
7160 /* EXP is to be treated as an array or record. Handle the cases when it is
7161 an access object and perform the required dereferences. */
7162
7163 static tree
7164 maybe_implicit_deref (tree exp)
7165 {
7166 /* If the type is a pointer, dereference it. */
7167
7168 if (POINTER_TYPE_P (TREE_TYPE (exp)) || TYPE_FAT_POINTER_P (TREE_TYPE (exp)))
7169 exp = build_unary_op (INDIRECT_REF, NULL_TREE, exp);
7170
7171 /* If we got a padded type, remove it too. */
7172 if (TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
7173 && TYPE_IS_PADDING_P (TREE_TYPE (exp)))
7174 exp = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp))), exp);
7175
7176 return exp;
7177 }
7178 \f
7179 /* Protect EXP from multiple evaluation. This may make a SAVE_EXPR. */
7180
7181 tree
7182 protect_multiple_eval (tree exp)
7183 {
7184 tree type = TREE_TYPE (exp);
7185
7186 /* If this has no side effects, we don't need to do anything. */
7187 if (!TREE_SIDE_EFFECTS (exp))
7188 return exp;
7189
7190 /* If it is a conversion, protect what's inside the conversion.
7191 Similarly, if we're indirectly referencing something, we only
7192 actually need to protect the address since the data itself can't
7193 change in these situations. */
7194 else if (TREE_CODE (exp) == NON_LVALUE_EXPR
7195 || CONVERT_EXPR_P (exp)
7196 || TREE_CODE (exp) == VIEW_CONVERT_EXPR
7197 || TREE_CODE (exp) == INDIRECT_REF
7198 || TREE_CODE (exp) == UNCONSTRAINED_ARRAY_REF)
7199 return build1 (TREE_CODE (exp), type,
7200 protect_multiple_eval (TREE_OPERAND (exp, 0)));
7201
7202 /* If EXP is a fat pointer or something that can be placed into a register,
7203 just make a SAVE_EXPR. */
7204 if (TYPE_FAT_POINTER_P (type) || TYPE_MODE (type) != BLKmode)
7205 return save_expr (exp);
7206
7207 /* Otherwise, dereference, protect the address, and re-reference. */
7208 else
7209 return
7210 build_unary_op (INDIRECT_REF, type,
7211 save_expr (build_unary_op (ADDR_EXPR,
7212 build_reference_type (type),
7213 exp)));
7214 }
7215 \f
7216 /* This is equivalent to stabilize_reference in tree.c, but we know how to
7217 handle our own nodes and we take extra arguments. FORCE says whether to
7218 force evaluation of everything. We set SUCCESS to true unless we walk
7219 through something we don't know how to stabilize. */
7220
7221 tree
7222 maybe_stabilize_reference (tree ref, bool force, bool *success)
7223 {
7224 tree type = TREE_TYPE (ref);
7225 enum tree_code code = TREE_CODE (ref);
7226 tree result;
7227
7228 /* Assume we'll success unless proven otherwise. */
7229 *success = true;
7230
7231 switch (code)
7232 {
7233 case CONST_DECL:
7234 case VAR_DECL:
7235 case PARM_DECL:
7236 case RESULT_DECL:
7237 /* No action is needed in this case. */
7238 return ref;
7239
7240 case ADDR_EXPR:
7241 CASE_CONVERT:
7242 case FLOAT_EXPR:
7243 case FIX_TRUNC_EXPR:
7244 case VIEW_CONVERT_EXPR:
7245 result
7246 = build1 (code, type,
7247 maybe_stabilize_reference (TREE_OPERAND (ref, 0), force,
7248 success));
7249 break;
7250
7251 case INDIRECT_REF:
7252 case UNCONSTRAINED_ARRAY_REF:
7253 result = build1 (code, type,
7254 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 0),
7255 force));
7256 break;
7257
7258 case COMPONENT_REF:
7259 result = build3 (COMPONENT_REF, type,
7260 maybe_stabilize_reference (TREE_OPERAND (ref, 0), force,
7261 success),
7262 TREE_OPERAND (ref, 1), NULL_TREE);
7263 break;
7264
7265 case BIT_FIELD_REF:
7266 result = build3 (BIT_FIELD_REF, type,
7267 maybe_stabilize_reference (TREE_OPERAND (ref, 0), force,
7268 success),
7269 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 1),
7270 force),
7271 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 2),
7272 force));
7273 break;
7274
7275 case ARRAY_REF:
7276 case ARRAY_RANGE_REF:
7277 result = build4 (code, type,
7278 maybe_stabilize_reference (TREE_OPERAND (ref, 0), force,
7279 success),
7280 gnat_stabilize_reference_1 (TREE_OPERAND (ref, 1),
7281 force),
7282 NULL_TREE, NULL_TREE);
7283 break;
7284
7285 case COMPOUND_EXPR:
7286 result = gnat_stabilize_reference_1 (ref, force);
7287 break;
7288
7289 case CALL_EXPR:
7290 /* This generates better code than the scheme in protect_multiple_eval
7291 because large objects will be returned via invisible reference in
7292 most ABIs so the temporary will directly be filled by the callee. */
7293 result = gnat_stabilize_reference_1 (ref, force);
7294 break;
7295
7296 case CONSTRUCTOR:
7297 /* Constructors with 1 element are used extensively to formally
7298 convert objects to special wrapping types. */
7299 if (TREE_CODE (type) == RECORD_TYPE
7300 && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (ref)) == 1)
7301 {
7302 tree index
7303 = VEC_index (constructor_elt, CONSTRUCTOR_ELTS (ref), 0)->index;
7304 tree value
7305 = VEC_index (constructor_elt, CONSTRUCTOR_ELTS (ref), 0)->value;
7306 result
7307 = build_constructor_single (type, index,
7308 gnat_stabilize_reference_1 (value,
7309 force));
7310 }
7311 else
7312 {
7313 *success = false;
7314 return ref;
7315 }
7316 break;
7317
7318 case ERROR_MARK:
7319 ref = error_mark_node;
7320
7321 /* ... fall through to failure ... */
7322
7323 /* If arg isn't a kind of lvalue we recognize, make no change.
7324 Caller should recognize the error for an invalid lvalue. */
7325 default:
7326 *success = false;
7327 return ref;
7328 }
7329
7330 TREE_READONLY (result) = TREE_READONLY (ref);
7331
7332 /* TREE_THIS_VOLATILE and TREE_SIDE_EFFECTS attached to the initial
7333 expression may not be sustained across some paths, such as the way via
7334 build1 for INDIRECT_REF. We re-populate those flags here for the general
7335 case, which is consistent with the GCC version of this routine.
7336
7337 Special care should be taken regarding TREE_SIDE_EFFECTS, because some
7338 paths introduce side effects where there was none initially (e.g. calls
7339 to save_expr), and we also want to keep track of that. */
7340
7341 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
7342 TREE_SIDE_EFFECTS (result) |= TREE_SIDE_EFFECTS (ref);
7343
7344 return result;
7345 }
7346
7347 /* Wrapper around maybe_stabilize_reference, for common uses without
7348 lvalue restrictions and without need to examine the success
7349 indication. */
7350
7351 static tree
7352 gnat_stabilize_reference (tree ref, bool force)
7353 {
7354 bool dummy;
7355 return maybe_stabilize_reference (ref, force, &dummy);
7356 }
7357
7358 /* Similar to stabilize_reference_1 in tree.c, but supports an extra
7359 arg to force a SAVE_EXPR for everything. */
7360
7361 static tree
7362 gnat_stabilize_reference_1 (tree e, bool force)
7363 {
7364 enum tree_code code = TREE_CODE (e);
7365 tree type = TREE_TYPE (e);
7366 tree result;
7367
7368 /* We cannot ignore const expressions because it might be a reference
7369 to a const array but whose index contains side-effects. But we can
7370 ignore things that are actual constant or that already have been
7371 handled by this function. */
7372
7373 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
7374 return e;
7375
7376 switch (TREE_CODE_CLASS (code))
7377 {
7378 case tcc_exceptional:
7379 case tcc_type:
7380 case tcc_declaration:
7381 case tcc_comparison:
7382 case tcc_statement:
7383 case tcc_expression:
7384 case tcc_reference:
7385 case tcc_vl_exp:
7386 /* If this is a COMPONENT_REF of a fat pointer, save the entire
7387 fat pointer. This may be more efficient, but will also allow
7388 us to more easily find the match for the PLACEHOLDER_EXPR. */
7389 if (code == COMPONENT_REF
7390 && TYPE_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (e, 0))))
7391 result = build3 (COMPONENT_REF, type,
7392 gnat_stabilize_reference_1 (TREE_OPERAND (e, 0),
7393 force),
7394 TREE_OPERAND (e, 1), TREE_OPERAND (e, 2));
7395 else if (TREE_SIDE_EFFECTS (e) || force)
7396 return save_expr (e);
7397 else
7398 return e;
7399 break;
7400
7401 case tcc_constant:
7402 /* Constants need no processing. In fact, we should never reach
7403 here. */
7404 return e;
7405
7406 case tcc_binary:
7407 /* Recursively stabilize each operand. */
7408 result = build2 (code, type,
7409 gnat_stabilize_reference_1 (TREE_OPERAND (e, 0), force),
7410 gnat_stabilize_reference_1 (TREE_OPERAND (e, 1),
7411 force));
7412 break;
7413
7414 case tcc_unary:
7415 /* Recursively stabilize each operand. */
7416 result = build1 (code, type,
7417 gnat_stabilize_reference_1 (TREE_OPERAND (e, 0),
7418 force));
7419 break;
7420
7421 default:
7422 gcc_unreachable ();
7423 }
7424
7425 TREE_READONLY (result) = TREE_READONLY (e);
7426
7427 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
7428 TREE_SIDE_EFFECTS (result) |= TREE_SIDE_EFFECTS (e);
7429 return result;
7430 }
7431 \f
7432 /* Convert SLOC into LOCUS. Return true if SLOC corresponds to a source code
7433 location and false if it doesn't. In the former case, set the Gigi global
7434 variable REF_FILENAME to the simple debug file name as given by sinput. */
7435
7436 bool
7437 Sloc_to_locus (Source_Ptr Sloc, location_t *locus)
7438 {
7439 if (Sloc == No_Location)
7440 return false;
7441
7442 if (Sloc <= Standard_Location)
7443 {
7444 *locus = BUILTINS_LOCATION;
7445 return false;
7446 }
7447 else
7448 {
7449 Source_File_Index file = Get_Source_File_Index (Sloc);
7450 Logical_Line_Number line = Get_Logical_Line_Number (Sloc);
7451 Column_Number column = Get_Column_Number (Sloc);
7452 struct line_map *map = &line_table->maps[file - 1];
7453
7454 /* Translate the location according to the line-map.h formula. */
7455 *locus = map->start_location
7456 + ((line - map->to_line) << map->column_bits)
7457 + (column & ((1 << map->column_bits) - 1));
7458 }
7459
7460 ref_filename
7461 = IDENTIFIER_POINTER
7462 (get_identifier
7463 (Get_Name_String (Debug_Source_Name (Get_Source_File_Index (Sloc)))));;
7464
7465 return true;
7466 }
7467
7468 /* Similar to set_expr_location, but start with the Sloc of GNAT_NODE and
7469 don't do anything if it doesn't correspond to a source location. */
7470
7471 static void
7472 set_expr_location_from_node (tree node, Node_Id gnat_node)
7473 {
7474 location_t locus;
7475
7476 if (!Sloc_to_locus (Sloc (gnat_node), &locus))
7477 return;
7478
7479 SET_EXPR_LOCATION (node, locus);
7480 }
7481 \f
7482 /* Return a colon-separated list of encodings contained in encoded Ada
7483 name. */
7484
7485 static const char *
7486 extract_encoding (const char *name)
7487 {
7488 char *encoding = GGC_NEWVEC (char, strlen (name));
7489 get_encoding (name, encoding);
7490 return encoding;
7491 }
7492
7493 /* Extract the Ada name from an encoded name. */
7494
7495 static const char *
7496 decode_name (const char *name)
7497 {
7498 char *decoded = GGC_NEWVEC (char, strlen (name) * 2 + 60);
7499 __gnat_decode (name, decoded, 0);
7500 return decoded;
7501 }
7502 \f
7503 /* Post an error message. MSG is the error message, properly annotated.
7504 NODE is the node at which to post the error and the node to use for the
7505 "&" substitution. */
7506
7507 void
7508 post_error (const char *msg, Node_Id node)
7509 {
7510 String_Template temp;
7511 Fat_Pointer fp;
7512
7513 temp.Low_Bound = 1, temp.High_Bound = strlen (msg);
7514 fp.Array = msg, fp.Bounds = &temp;
7515 if (Present (node))
7516 Error_Msg_N (fp, node);
7517 }
7518
7519 /* Similar, but NODE is the node at which to post the error and ENT
7520 is the node to use for the "&" substitution. */
7521
7522 void
7523 post_error_ne (const char *msg, Node_Id node, Entity_Id ent)
7524 {
7525 String_Template temp;
7526 Fat_Pointer fp;
7527
7528 temp.Low_Bound = 1, temp.High_Bound = strlen (msg);
7529 fp.Array = msg, fp.Bounds = &temp;
7530 if (Present (node))
7531 Error_Msg_NE (fp, node, ent);
7532 }
7533
7534 /* Similar, but NODE is the node at which to post the error, ENT is the node
7535 to use for the "&" substitution, and N is the number to use for the ^. */
7536
7537 void
7538 post_error_ne_num (const char *msg, Node_Id node, Entity_Id ent, int n)
7539 {
7540 String_Template temp;
7541 Fat_Pointer fp;
7542
7543 temp.Low_Bound = 1, temp.High_Bound = strlen (msg);
7544 fp.Array = msg, fp.Bounds = &temp;
7545 Error_Msg_Uint_1 = UI_From_Int (n);
7546
7547 if (Present (node))
7548 Error_Msg_NE (fp, node, ent);
7549 }
7550 \f
7551 /* Similar to post_error_ne_num, but T is a GCC tree representing the
7552 number to write. If the tree represents a constant that fits within
7553 a host integer, the text inside curly brackets in MSG will be output
7554 (presumably including a '^'). Otherwise that text will not be output
7555 and the text inside square brackets will be output instead. */
7556
7557 void
7558 post_error_ne_tree (const char *msg, Node_Id node, Entity_Id ent, tree t)
7559 {
7560 char *newmsg = XALLOCAVEC (char, strlen (msg) + 1);
7561 String_Template temp = {1, 0};
7562 Fat_Pointer fp;
7563 char start_yes, end_yes, start_no, end_no;
7564 const char *p;
7565 char *q;
7566
7567 fp.Array = newmsg, fp.Bounds = &temp;
7568
7569 if (host_integerp (t, 1)
7570 #if HOST_BITS_PER_WIDE_INT > HOST_BITS_PER_INT
7571 &&
7572 compare_tree_int
7573 (t, (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_INT - 1)) - 1)) < 0
7574 #endif
7575 )
7576 {
7577 Error_Msg_Uint_1 = UI_From_Int (tree_low_cst (t, 1));
7578 start_yes = '{', end_yes = '}', start_no = '[', end_no = ']';
7579 }
7580 else
7581 start_yes = '[', end_yes = ']', start_no = '{', end_no = '}';
7582
7583 for (p = msg, q = newmsg; *p; p++)
7584 {
7585 if (*p == start_yes)
7586 for (p++; *p != end_yes; p++)
7587 *q++ = *p;
7588 else if (*p == start_no)
7589 for (p++; *p != end_no; p++)
7590 ;
7591 else
7592 *q++ = *p;
7593 }
7594
7595 *q = 0;
7596
7597 temp.High_Bound = strlen (newmsg);
7598 if (Present (node))
7599 Error_Msg_NE (fp, node, ent);
7600 }
7601
7602 /* Similar to post_error_ne_tree, except that NUM is a second
7603 integer to write in the message. */
7604
7605 void
7606 post_error_ne_tree_2 (const char *msg, Node_Id node, Entity_Id ent, tree t,
7607 int num)
7608 {
7609 Error_Msg_Uint_2 = UI_From_Int (num);
7610 post_error_ne_tree (msg, node, ent, t);
7611 }
7612 \f
7613 /* Initialize the table that maps GNAT codes to GCC codes for simple
7614 binary and unary operations. */
7615
7616 static void
7617 init_code_table (void)
7618 {
7619 gnu_codes[N_And_Then] = TRUTH_ANDIF_EXPR;
7620 gnu_codes[N_Or_Else] = TRUTH_ORIF_EXPR;
7621
7622 gnu_codes[N_Op_And] = TRUTH_AND_EXPR;
7623 gnu_codes[N_Op_Or] = TRUTH_OR_EXPR;
7624 gnu_codes[N_Op_Xor] = TRUTH_XOR_EXPR;
7625 gnu_codes[N_Op_Eq] = EQ_EXPR;
7626 gnu_codes[N_Op_Ne] = NE_EXPR;
7627 gnu_codes[N_Op_Lt] = LT_EXPR;
7628 gnu_codes[N_Op_Le] = LE_EXPR;
7629 gnu_codes[N_Op_Gt] = GT_EXPR;
7630 gnu_codes[N_Op_Ge] = GE_EXPR;
7631 gnu_codes[N_Op_Add] = PLUS_EXPR;
7632 gnu_codes[N_Op_Subtract] = MINUS_EXPR;
7633 gnu_codes[N_Op_Multiply] = MULT_EXPR;
7634 gnu_codes[N_Op_Mod] = FLOOR_MOD_EXPR;
7635 gnu_codes[N_Op_Rem] = TRUNC_MOD_EXPR;
7636 gnu_codes[N_Op_Minus] = NEGATE_EXPR;
7637 gnu_codes[N_Op_Abs] = ABS_EXPR;
7638 gnu_codes[N_Op_Not] = TRUTH_NOT_EXPR;
7639 gnu_codes[N_Op_Rotate_Left] = LROTATE_EXPR;
7640 gnu_codes[N_Op_Rotate_Right] = RROTATE_EXPR;
7641 gnu_codes[N_Op_Shift_Left] = LSHIFT_EXPR;
7642 gnu_codes[N_Op_Shift_Right] = RSHIFT_EXPR;
7643 gnu_codes[N_Op_Shift_Right_Arithmetic] = RSHIFT_EXPR;
7644 }
7645
7646 /* Return a label to branch to for the exception type in KIND or NULL_TREE
7647 if none. */
7648
7649 tree
7650 get_exception_label (char kind)
7651 {
7652 if (kind == N_Raise_Constraint_Error)
7653 return TREE_VALUE (gnu_constraint_error_label_stack);
7654 else if (kind == N_Raise_Storage_Error)
7655 return TREE_VALUE (gnu_storage_error_label_stack);
7656 else if (kind == N_Raise_Program_Error)
7657 return TREE_VALUE (gnu_program_error_label_stack);
7658 else
7659 return NULL_TREE;
7660 }
7661
7662 #include "gt-ada-trans.h"