OSDN Git Service

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