OSDN Git Service

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