* make.adb:
[platform/upstream/gcc.git] / gcc / ada / utils.c
1 /****************************************************************************
2  *                                                                          *
3  *                         GNAT COMPILER COMPONENTS                         *
4  *                                                                          *
5  *                                U T I L S                                 *
6  *                                                                          *
7  *                          C Implementation File                           *
8  *                                                                          *
9  *                            $Revision: 1.4 $
10  *                                                                          *
11  *          Copyright (C) 1992-2001, Free Software Foundation, Inc.         *
12  *                                                                          *
13  * GNAT is free software;  you can  redistribute it  and/or modify it under *
14  * terms of the  GNU General Public License as published  by the Free Soft- *
15  * ware  Foundation;  either version 2,  or (at your option) any later ver- *
16  * sion.  GNAT is distributed in the hope that it will be useful, but WITH- *
17  * OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY *
18  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License *
19  * for  more details.  You should have  received  a copy of the GNU General *
20  * Public License  distributed with GNAT;  see file COPYING.  If not, write *
21  * to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, *
22  * MA 02111-1307, USA.                                                      *
23  *                                                                          *
24  * GNAT was originally developed  by the GNAT team at  New York University. *
25  * It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). *
26  *                                                                          *
27  ****************************************************************************/
28
29 #include "config.h"
30 #include "system.h"
31 #include "tree.h"
32 #include "flags.h"
33 #include "defaults.h"
34 #include "toplev.h"
35 #include "output.h"
36 #include "ggc.h"
37 #include "convert.h"
38
39 #include "ada.h"
40 #include "types.h"
41 #include "atree.h"
42 #include "elists.h"
43 #include "namet.h"
44 #include "nlists.h"
45 #include "stringt.h"
46 #include "uintp.h"
47 #include "fe.h"
48 #include "sinfo.h"
49 #include "einfo.h"
50 #include "ada-tree.h"
51 #include "gigi.h"
52
53 #ifndef MAX_FIXED_MODE_SIZE
54 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
55 #endif
56
57 #ifndef MAX_BITS_PER_WORD
58 #define MAX_BITS_PER_WORD  BITS_PER_WORD
59 #endif
60
61 /* If nonzero, pretend we are allocating at global level.  */
62 int force_global;
63
64 /* Global Variables for the various types we create.  */ 
65 tree gnat_std_decls[(int) ADT_LAST];
66
67 /* Associates a GNAT tree node to a GCC tree node. It is used in
68    `save_gnu_tree', `get_gnu_tree' and `present_gnu_tree'. See documentation
69    of `save_gnu_tree' for more info.  */
70 static tree *associate_gnat_to_gnu;
71
72 /* This listhead is used to record any global objects that need elaboration.
73    TREE_PURPOSE is the variable to be elaborated and TREE_VALUE is the
74    initial value to assign.  */
75
76 static tree pending_elaborations;
77
78 /* This stack allows us to momentarily switch to generating elaboration
79    lists for an inner context.  */
80
81 static struct e_stack {struct e_stack *next; tree elab_list; } *elist_stack;
82
83 /* This variable keeps a table for types for each precision so that we only 
84    allocate each of them once. Signed and unsigned types are kept separate.
85
86    Note that these types are only used when fold-const requests something
87    special.  Perhaps we should NOT share these types; we'll see how it
88    goes later.  */
89 static tree signed_and_unsigned_types[2 * MAX_BITS_PER_WORD + 1][2];
90
91 /* Likewise for float types, but record these by mode.  */
92 static tree float_types[NUM_MACHINE_MODES];
93
94 /* For each binding contour we allocate a binding_level structure which records
95    the entities defined or declared in that contour. Contours include:
96
97         the global one
98         one for each subprogram definition
99         one for each compound statement (declare block)
100
101    Binding contours are used to create GCC tree BLOCK nodes.  */
102
103 struct binding_level
104 {
105   /* A chain of ..._DECL nodes for all variables, constants, functions,
106      parameters and type declarations.  These ..._DECL nodes are chained
107      through the TREE_CHAIN field. Note that these ..._DECL nodes are stored
108      in the reverse of the order supplied to be compatible with the
109      back-end.  */
110   tree names;
111   /* For each level (except the global one), a chain of BLOCK nodes for all
112      the levels that were entered and exited one level down from this one.  */
113   tree blocks;
114   /* The BLOCK node for this level, if one has been preallocated.
115      If 0, the BLOCK is allocated (if needed) when the level is popped.  */
116   tree this_block;
117   /* The binding level containing this one (the enclosing binding level). */
118   struct binding_level *level_chain;
119 };
120
121 /* The binding level currently in effect.  */
122 static struct binding_level *current_binding_level = NULL;
123
124 /* A chain of binding_level structures awaiting reuse.  */
125 static struct binding_level *free_binding_level = NULL;
126
127 /* The outermost binding level. This binding level is created when the
128    compiler is started and it will exist through the entire compilation.  */
129 static struct binding_level *global_binding_level;
130
131 /* Binding level structures are initialized by copying this one.  */
132 static struct binding_level clear_binding_level = {NULL, NULL, NULL, NULL};
133
134
135 static tree merge_sizes                 PARAMS ((tree, tree, tree, int, int));
136 static tree compute_related_constant    PARAMS ((tree, tree));
137 static tree split_plus                  PARAMS ((tree, tree *));
138 static int value_zerop                  PARAMS ((tree));
139 static tree float_type_for_size         PARAMS ((int, enum machine_mode));
140 static tree convert_to_fat_pointer      PARAMS ((tree, tree));
141 static tree convert_to_thin_pointer     PARAMS ((tree, tree));
142 static tree make_descriptor_field       PARAMS ((const char *,tree, tree,
143                                                  tree));
144 static void mark_binding_level          PARAMS((PTR));
145 static void mark_e_stack                PARAMS((PTR));
146 \f
147 /* Initialize the association of GNAT nodes to GCC trees.  */
148
149 void
150 init_gnat_to_gnu ()
151 {
152   Node_Id gnat_node;
153
154   associate_gnat_to_gnu = (tree *) xmalloc (max_gnat_nodes * sizeof (tree));
155   ggc_add_tree_root (associate_gnat_to_gnu, max_gnat_nodes);
156
157   for (gnat_node = 0; gnat_node < max_gnat_nodes; gnat_node++)
158     associate_gnat_to_gnu [gnat_node] = NULL_TREE;
159
160   associate_gnat_to_gnu -= First_Node_Id;
161
162   pending_elaborations = build_tree_list (NULL_TREE, NULL_TREE);
163   ggc_add_tree_root (&pending_elaborations, 1);
164   ggc_add_root ((PTR) &elist_stack, 1, sizeof (struct e_stack), mark_e_stack);
165   ggc_add_tree_root (&signed_and_unsigned_types[0][0],
166                      (sizeof signed_and_unsigned_types
167                       / sizeof signed_and_unsigned_types[0][0]));
168   ggc_add_tree_root (float_types, sizeof float_types / sizeof float_types[0]);
169
170   ggc_add_root (&current_binding_level, 1, sizeof current_binding_level,
171                 mark_binding_level);
172 }
173
174 /* GNAT_ENTITY is a GNAT tree node for an entity.   GNU_DECL is the GCC tree
175    which is to be associated with GNAT_ENTITY. Such GCC tree node is always
176    a ..._DECL node.  If NO_CHECK is nonzero, the latter check is suppressed.
177
178    If GNU_DECL is zero, a previous association is to be reset.  */
179
180 void
181 save_gnu_tree (gnat_entity, gnu_decl, no_check)
182      Entity_Id gnat_entity;
183      tree gnu_decl;
184      int no_check;
185 {
186   if (gnu_decl
187       && (associate_gnat_to_gnu [gnat_entity]
188           || (! no_check && ! DECL_P (gnu_decl))))
189     gigi_abort (401);
190
191   associate_gnat_to_gnu [gnat_entity] = gnu_decl;
192 }
193
194 /* GNAT_ENTITY is a GNAT tree node for a defining identifier.
195    Return the ..._DECL node that was associated with it.  If there is no tree
196    node associated with GNAT_ENTITY, abort.
197
198    In some cases, such as delayed elaboration or expressions that need to
199    be elaborated only once, GNAT_ENTITY is really not an entity.  */
200
201 tree
202 get_gnu_tree (gnat_entity)
203      Entity_Id gnat_entity;
204 {
205   if (! associate_gnat_to_gnu [gnat_entity])
206     gigi_abort (402);
207
208   return associate_gnat_to_gnu [gnat_entity];
209 }
210
211 /* Return nonzero if a GCC tree has been associated with GNAT_ENTITY.  */
212
213 int
214 present_gnu_tree (gnat_entity)
215      Entity_Id gnat_entity;
216 {
217   return (associate_gnat_to_gnu [gnat_entity] != NULL_TREE);
218 }
219
220 \f
221 /* Return non-zero if we are currently in the global binding level.  */
222
223 int
224 global_bindings_p ()
225 {
226   return (force_global != 0 || current_binding_level == global_binding_level
227           ? -1 : 0);
228 }
229
230 /* Return the list of declarations in the current level. Note that this list
231    is in reverse order (it has to be so for back-end compatibility).  */
232
233 tree
234 getdecls ()
235 {
236   return current_binding_level->names;
237 }
238
239 /* Nonzero if the current level needs to have a BLOCK made.  */
240
241 int
242 kept_level_p ()
243 {
244   return (current_binding_level->names != 0);
245 }
246
247 /* Enter a new binding level. The input parameter is ignored, but has to be
248    specified for back-end compatibility.  */
249
250 void
251 pushlevel (ignore)
252      int ignore ATTRIBUTE_UNUSED;
253 {
254   struct binding_level *newlevel = NULL;
255
256   /* Reuse a struct for this binding level, if there is one.  */
257   if (free_binding_level)
258     {
259       newlevel = free_binding_level;
260       free_binding_level = free_binding_level->level_chain;
261     }
262   else
263     newlevel
264       = (struct binding_level *) xmalloc (sizeof (struct binding_level));
265
266   *newlevel = clear_binding_level;
267
268   /* Add this level to the front of the chain (stack) of levels that are
269      active.  */
270   newlevel->level_chain = current_binding_level;
271   current_binding_level = newlevel;
272 }
273
274 /* Exit a binding level.
275    Pop the level off, and restore the state of the identifier-decl mappings
276    that were in effect when this level was entered.
277
278    If KEEP is nonzero, this level had explicit declarations, so
279    and create a "block" (a BLOCK node) for the level
280    to record its declarations and subblocks for symbol table output.
281
282    If FUNCTIONBODY is nonzero, this level is the body of a function,
283    so create a block as if KEEP were set and also clear out all
284    label names.
285
286    If REVERSE is nonzero, reverse the order of decls before putting
287    them into the BLOCK.  */
288
289 tree
290 poplevel (keep, reverse, functionbody)
291      int keep;
292      int reverse;
293      int functionbody;
294 {
295   /* Points to a GCC BLOCK tree node. This is the BLOCK node construted for the
296      binding level that we are about to exit and which is returned by this
297      routine.  */
298   tree block = NULL_TREE;
299   tree decl_chain;
300   tree decl_node;
301   tree subblock_chain = current_binding_level->blocks;
302   tree subblock_node;
303   int block_previously_created;
304
305   /* Reverse the list of XXXX_DECL nodes if desired.  Note that the ..._DECL
306      nodes chained through the `names' field of current_binding_level are in
307      reverse order except for PARM_DECL node, which are explicitely stored in
308      the right order.  */
309   current_binding_level->names
310     = decl_chain = (reverse) ? nreverse (current_binding_level->names)
311       : current_binding_level->names;
312
313   /* Output any nested inline functions within this block which must be
314      compiled because their address is needed. */
315   for (decl_node = decl_chain; decl_node; decl_node = TREE_CHAIN (decl_node))
316     if (TREE_CODE (decl_node) == FUNCTION_DECL
317         && ! TREE_ASM_WRITTEN (decl_node) && TREE_ADDRESSABLE (decl_node)
318         && DECL_INITIAL (decl_node) != 0)
319       {
320         push_function_context ();
321         output_inline_function (decl_node);
322         pop_function_context ();
323       }
324
325   block = 0;
326   block_previously_created = (current_binding_level->this_block != 0);
327   if (block_previously_created)
328     block = current_binding_level->this_block;
329   else if (keep || functionbody)
330     block = make_node (BLOCK);
331   if (block != 0)
332     {
333       BLOCK_VARS (block) = keep ? decl_chain : 0;
334       BLOCK_SUBBLOCKS (block) = subblock_chain;
335     }
336
337   /* Record the BLOCK node just built as the subblock its enclosing scope.  */
338   for (subblock_node = subblock_chain; subblock_node;
339        subblock_node = TREE_CHAIN (subblock_node))
340     BLOCK_SUPERCONTEXT (subblock_node) = block;
341
342   /* Clear out the meanings of the local variables of this level.  */
343
344   for (subblock_node = decl_chain; subblock_node;
345        subblock_node = TREE_CHAIN (subblock_node))
346     if (DECL_NAME (subblock_node) != 0)
347       /* If the identifier was used or addressed via a local extern decl,  
348          don't forget that fact.   */
349       if (DECL_EXTERNAL (subblock_node))
350         {
351           if (TREE_USED (subblock_node))
352             TREE_USED (DECL_NAME (subblock_node)) = 1;
353           if (TREE_ADDRESSABLE (subblock_node))
354             TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (subblock_node)) = 1;
355         }
356
357   {
358     /* Pop the current level, and free the structure for reuse.  */
359     struct binding_level *level = current_binding_level;
360     current_binding_level = current_binding_level->level_chain;
361     level->level_chain = free_binding_level;
362     free_binding_level = level;
363   }
364
365   if (functionbody)
366     {
367       /* This is the top level block of a function. The ..._DECL chain stored
368          in BLOCK_VARS are the function's parameters (PARM_DECL nodes). Don't
369          leave them in the BLOCK because they are found in the FUNCTION_DECL
370          instead.  */
371       DECL_INITIAL (current_function_decl) = block;
372       BLOCK_VARS (block) = 0;
373     }
374   else if (block)
375     {
376       if (!block_previously_created)
377         current_binding_level->blocks
378           = chainon (current_binding_level->blocks, block);
379     }
380
381   /* If we did not make a block for the level just exited, any blocks made for
382      inner levels (since they cannot be recorded as subblocks in that level)
383      must be carried forward so they will later become subblocks of something
384      else.  */
385   else if (subblock_chain)
386     current_binding_level->blocks
387       = chainon (current_binding_level->blocks, subblock_chain);
388   if (block)
389     TREE_USED (block) = 1;
390
391   return block;
392 }
393 \f
394 /* Insert BLOCK at the end of the list of subblocks of the
395    current binding level.  This is used when a BIND_EXPR is expanded,
396    to handle the BLOCK node inside the BIND_EXPR.  */
397
398 void
399 insert_block (block)
400      tree block;
401 {
402   TREE_USED (block) = 1;
403   current_binding_level->blocks
404     = chainon (current_binding_level->blocks, block);
405 }
406
407 /* Set the BLOCK node for the innermost scope
408    (the one we are currently in).  */
409
410 void
411 set_block (block)
412      tree block;
413 {
414   current_binding_level->this_block = block;
415   current_binding_level->names = chainon (current_binding_level->names,
416                                           BLOCK_VARS (block));
417   current_binding_level->blocks = chainon (current_binding_level->blocks,
418                                            BLOCK_SUBBLOCKS (block));
419 }
420
421 /* Records a ..._DECL node DECL as belonging to the current lexical scope.
422    Returns the ..._DECL node. */
423
424 tree
425 pushdecl (decl)
426      tree decl;
427 {
428   struct binding_level *b;
429
430   /* If at top level, there is no context. But PARM_DECLs always go in the
431      level of its function. */
432   if (global_bindings_p () && TREE_CODE (decl) != PARM_DECL)
433     {
434       b = global_binding_level;
435       DECL_CONTEXT (decl) = 0;
436     }
437   else
438     {
439       b = current_binding_level;
440       DECL_CONTEXT (decl) = current_function_decl;
441     }
442
443   /* Put the declaration on the list.  The list of declarations is in reverse
444      order. The list will be reversed later if necessary.  This needs to be
445      this way for compatibility with the back-end.
446
447      Don't put TYPE_DECLs for UNCONSTRAINED_ARRAY_TYPE into the list.  They
448      will cause trouble with the debugger and aren't needed anyway.  */
449   if (TREE_CODE (decl) != TYPE_DECL
450       || TREE_CODE (TREE_TYPE (decl)) != UNCONSTRAINED_ARRAY_TYPE)
451     {
452       TREE_CHAIN (decl) = b->names;
453       b->names = decl;
454     }
455
456   /* For the declaration of a type, set its name if it either is not already
457      set, was set to an IDENTIFIER_NODE, indicating an internal name,
458      or if the previous type name was not derived from a source name.
459      We'd rather have the type named with a real name and all the pointer
460      types to the same object have the same POINTER_TYPE node.  Code in this
461      function in c-decl.c makes a copy of the type node here, but that may
462      cause us trouble with incomplete types, so let's not try it (at least
463      for now).  */
464
465   if (TREE_CODE (decl) == TYPE_DECL
466       && DECL_NAME (decl) != 0
467       && (TYPE_NAME (TREE_TYPE (decl)) == 0
468           || TREE_CODE (TYPE_NAME (TREE_TYPE (decl))) == IDENTIFIER_NODE
469           || (TREE_CODE (TYPE_NAME (TREE_TYPE (decl))) == TYPE_DECL
470               && DECL_ARTIFICIAL (TYPE_NAME (TREE_TYPE (decl)))
471               && ! DECL_ARTIFICIAL (decl))))
472     TYPE_NAME (TREE_TYPE (decl)) = decl;
473
474   return decl;
475 }
476 \f
477 /* Do little here.  Set up the standard declarations later after the
478    front end has been run.  */
479
480 void
481 init_decl_processing ()
482 {
483   /* The structure `tree_identifier' is the GCC tree data structure that holds
484      IDENTIFIER_NODE nodes. We need to call `set_identifier_size' to tell GCC
485      that we have not added any language specific fields to IDENTIFIER_NODE
486      nodes.  */
487   set_identifier_size (sizeof (struct tree_identifier));
488
489   lineno = 0;
490
491   /* incomplete_decl_finalize_hook is defined in toplev.c. It needs to be set
492      by each front end to the appropriate routine that handles incomplete 
493      VAR_DECL nodes. This routine will be invoked by compile_file when a  
494      VAR_DECL node of DECL_SIZE zero is encountered.  */
495   incomplete_decl_finalize_hook = finish_incomplete_decl;
496
497   /* Make the binding_level structure for global names.  */
498   current_function_decl = 0;
499   current_binding_level = 0;
500   free_binding_level = 0;
501   pushlevel (0);
502   global_binding_level = current_binding_level;
503
504   build_common_tree_nodes (0);
505
506   /* In Ada, we use a signed type for SIZETYPE.  Use the signed type
507      corresponding to the size of ptr_mode.  Make this here since we need
508      this before we can expand the GNAT types.  */
509   set_sizetype (type_for_size (GET_MODE_BITSIZE (ptr_mode), 0));
510   build_common_tree_nodes_2 (0);
511
512   pushdecl (build_decl (TYPE_DECL, get_identifier (SIZE_TYPE), sizetype));
513
514   /* We need to make the integer type before doing anything else.
515      We stitch this in to the appropriate GNAT type later.  */
516   pushdecl (build_decl (TYPE_DECL, get_identifier ("integer"),
517                         integer_type_node));
518   pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned char"),
519                         char_type_node));
520
521   ptr_void_type_node = build_pointer_type (void_type_node);
522
523 }
524
525 /* Create the predefined scalar types such as `integer_type_node' needed 
526    in the gcc back-end and initialize the global binding level.  */
527
528 void
529 init_gigi_decls (long_long_float_type, exception_type)
530      tree long_long_float_type, exception_type;
531 {
532   tree endlink;
533
534   /* Set the types that GCC and Gigi use from the front end.  We would like
535      to do this for char_type_node, but it needs to correspond to the C
536      char type.  */
537   if (TREE_CODE (TREE_TYPE (long_long_float_type)) == INTEGER_TYPE)
538     {
539       /* In this case, the builtin floating point types are VAX float,
540          so make up a type for use.  */
541       longest_float_type_node = make_node (REAL_TYPE);
542       TYPE_PRECISION (longest_float_type_node) = LONG_DOUBLE_TYPE_SIZE;
543       layout_type (longest_float_type_node);
544       pushdecl (build_decl (TYPE_DECL, get_identifier ("longest float type"),
545                             longest_float_type_node));
546     }
547   else
548     longest_float_type_node = TREE_TYPE (long_long_float_type);
549
550   except_type_node = TREE_TYPE (exception_type);
551
552   unsigned_type_node = type_for_size (INT_TYPE_SIZE, 1);
553   pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
554                         unsigned_type_node));
555
556   void_type_decl_node
557     = pushdecl (build_decl (TYPE_DECL, get_identifier ("void"),
558                             void_type_node));
559
560   void_ftype = build_function_type (void_type_node, NULL_TREE);
561   ptr_void_ftype = build_pointer_type (void_ftype);
562
563   /* Now declare runtime functions. */
564   endlink = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
565
566   /* malloc is a function declaration tree for a function to allocate
567      memory.  */
568   malloc_decl = create_subprog_decl (get_identifier ("__gnat_malloc"),
569                                      NULL_TREE,
570                                      build_function_type (ptr_void_type_node,
571                                                           tree_cons (NULL_TREE,
572                                                                      sizetype,
573                                                                      endlink)),
574                                      NULL_TREE, 0, 1, 1, 0);
575
576   /* free is a function declaration tree for a function to free memory.  */
577
578   free_decl
579     = create_subprog_decl (get_identifier ("__gnat_free"), NULL_TREE,
580                            build_function_type (void_type_node,
581                                                 tree_cons (NULL_TREE,
582                                                            ptr_void_type_node,
583                                                            endlink)),
584                            NULL_TREE, 0, 1, 1, 0);
585
586   /* Make the types and functions used for exception processing.    */
587   jmpbuf_type
588     = build_array_type (type_for_mode (Pmode, 0),
589                         build_index_type (build_int_2 (5, 0)));
590   pushdecl (build_decl (TYPE_DECL, get_identifier ("JMPBUF_T"), jmpbuf_type));
591   jmpbuf_ptr_type = build_pointer_type (jmpbuf_type);
592
593   /* Functions to get and set the jumpbuf pointer for the current thread.  */
594   get_jmpbuf_decl
595     = create_subprog_decl
596     (get_identifier ("system__soft_links__get_jmpbuf_address_soft"),
597      NULL_TREE, build_function_type (jmpbuf_ptr_type, NULL_TREE),
598      NULL_TREE, 0, 1, 1, 0);
599
600   set_jmpbuf_decl
601     = create_subprog_decl
602     (get_identifier ("system__soft_links__set_jmpbuf_address_soft"),
603      NULL_TREE,
604      build_function_type (void_type_node, 
605                           tree_cons (NULL_TREE, jmpbuf_ptr_type, endlink)),
606      NULL_TREE, 0, 1, 1, 0);
607
608   /* Function to get the current exception.  */
609   get_excptr_decl
610     = create_subprog_decl
611     (get_identifier ("system__soft_links__get_gnat_exception"),
612      NULL_TREE,
613      build_function_type (build_pointer_type (except_type_node), NULL_TREE),
614      NULL_TREE, 0, 1, 1, 0);
615
616   /* Function that raise exceptions. */
617   raise_nodefer_decl
618     = create_subprog_decl
619       (get_identifier ("__gnat_raise_nodefer_with_msg"), NULL_TREE,
620        build_function_type (void_type_node,
621                             tree_cons (NULL_TREE,
622                                        build_pointer_type (except_type_node),
623                                        endlink)),
624        NULL_TREE, 0, 1, 1, 0);
625
626
627   /* __gnat_raise_constraint_error takes a string, an integer and never
628      returns.  */
629   raise_constraint_error_decl
630     = create_subprog_decl
631       (get_identifier ("__gnat_raise_constraint_error"), NULL_TREE,
632        build_function_type (void_type_node,
633                             tree_cons (NULL_TREE,
634                                        build_pointer_type (char_type_node),
635                                        tree_cons (NULL_TREE,
636                                                   integer_type_node,
637                                                   endlink))),
638        NULL_TREE, 0, 1, 1, 0);
639
640   /* Likewise for __gnat_raise_program_error.  */
641   raise_program_error_decl
642     = create_subprog_decl
643       (get_identifier ("__gnat_raise_program_error"), NULL_TREE,
644        build_function_type (void_type_node,
645                             tree_cons (NULL_TREE,
646                                        build_pointer_type (char_type_node),
647                                        tree_cons (NULL_TREE,
648                                                   integer_type_node,
649                                                   endlink))),
650        NULL_TREE, 0, 1, 1, 0);
651
652   /* Likewise for __gnat_raise_storage_error.  */
653   raise_storage_error_decl
654     = create_subprog_decl
655       (get_identifier ("__gnat_raise_storage_error"), NULL_TREE,
656        build_function_type (void_type_node,
657                             tree_cons (NULL_TREE,
658                                        build_pointer_type (char_type_node),
659                                        tree_cons (NULL_TREE,
660                                                   integer_type_node,
661                                                   endlink))),
662        NULL_TREE, 0, 1, 1, 0);
663
664   /* Indicate that these never return.  */
665
666   TREE_THIS_VOLATILE (raise_nodefer_decl) = 1;
667   TREE_THIS_VOLATILE (raise_constraint_error_decl) = 1;
668   TREE_THIS_VOLATILE (raise_program_error_decl) = 1;
669   TREE_THIS_VOLATILE (raise_storage_error_decl) = 1;
670
671   TREE_SIDE_EFFECTS (raise_nodefer_decl) = 1;
672   TREE_SIDE_EFFECTS (raise_constraint_error_decl) = 1;
673   TREE_SIDE_EFFECTS (raise_program_error_decl) = 1;
674   TREE_SIDE_EFFECTS (raise_storage_error_decl) = 1;
675
676   TREE_TYPE (raise_nodefer_decl)
677     = build_qualified_type (TREE_TYPE (raise_nodefer_decl),
678                             TYPE_QUAL_VOLATILE);
679   TREE_TYPE (raise_constraint_error_decl)
680     = build_qualified_type (TREE_TYPE (raise_constraint_error_decl),
681                             TYPE_QUAL_VOLATILE);
682   TREE_TYPE (raise_program_error_decl)
683     = build_qualified_type (TREE_TYPE (raise_program_error_decl),
684                             TYPE_QUAL_VOLATILE);
685   TREE_TYPE (raise_storage_error_decl)
686     = build_qualified_type (TREE_TYPE (raise_storage_error_decl),
687                             TYPE_QUAL_VOLATILE);
688
689   /* setjmp returns an integer and has one operand, which is a pointer to
690      a jmpbuf.  */
691   setjmp_decl
692     = create_subprog_decl
693       (get_identifier ("setjmp"), NULL_TREE,
694        build_function_type (integer_type_node,
695                             tree_cons (NULL_TREE,  jmpbuf_ptr_type, endlink)),
696        NULL_TREE, 0, 1, 1, 0);
697
698   DECL_BUILT_IN_CLASS (setjmp_decl) = BUILT_IN_NORMAL;
699   DECL_FUNCTION_CODE (setjmp_decl) = BUILT_IN_SETJMP;
700
701   ggc_add_tree_root (gnat_std_decls,
702                      sizeof gnat_std_decls / sizeof gnat_std_decls[0]);
703 }
704 \f
705 /* This routine is called in tree.c to print an error message for invalid use
706    of an incomplete type.  */
707
708 void
709 incomplete_type_error (dont_care_1, dont_care_2)
710      tree dont_care_1 ATTRIBUTE_UNUSED;
711      tree dont_care_2 ATTRIBUTE_UNUSED;
712 {
713   gigi_abort (404);
714 }
715
716 /* This function is called indirectly from toplev.c to handle incomplete 
717    declarations, i.e. VAR_DECL nodes whose DECL_SIZE is zero.  To be precise,
718    compile_file in toplev.c makes an indirect call through the function pointer
719    incomplete_decl_finalize_hook which is initialized to this routine in
720    init_decl_processing.  */
721
722 void
723 finish_incomplete_decl (dont_care)
724      tree dont_care ATTRIBUTE_UNUSED;
725 {
726   gigi_abort (405);
727 }
728 \f
729 /* Given a record type (RECORD_TYPE) and a chain of FIELD_DECL
730    nodes (FIELDLIST), finish constructing the record or union type. 
731    If HAS_REP is nonzero, this record has a rep clause; don't call
732    layout_type but merely set the size and alignment ourselves. 
733    If DEFER_DEBUG is nonzero, do not call the debugging routines
734    on this type; it will be done later. */
735
736 void
737 finish_record_type (record_type, fieldlist, has_rep, defer_debug)
738      tree record_type;
739      tree fieldlist;
740      int has_rep;
741      int defer_debug;
742 {
743   enum tree_code code = TREE_CODE (record_type);
744   tree ada_size = bitsize_zero_node;
745   tree size = bitsize_zero_node;
746   tree size_unit = size_zero_node;
747   tree field;
748
749   TYPE_FIELDS (record_type) = fieldlist;
750
751   if (TYPE_NAME (record_type) != 0
752       && TREE_CODE (TYPE_NAME (record_type)) == TYPE_DECL)
753     TYPE_STUB_DECL (record_type) = TYPE_NAME (record_type);
754   else
755     TYPE_STUB_DECL (record_type)
756       = pushdecl (build_decl (TYPE_DECL, TYPE_NAME (record_type),
757                               record_type));
758
759   /* We don't need both the typedef name and the record name output in
760      the debugging information, since they are the same.  */
761   DECL_ARTIFICIAL (TYPE_STUB_DECL (record_type)) = 1;
762
763   /* Globally initialize the record first.  If this is a rep'ed record,
764      that just means some initializations; otherwise, layout the record.  */
765
766   if (has_rep)
767     {
768       TYPE_ALIGN (record_type) = MAX (BITS_PER_UNIT, TYPE_ALIGN (record_type));
769       TYPE_MODE (record_type) = BLKmode;
770       if (TYPE_SIZE (record_type) == 0)
771         {
772           TYPE_SIZE (record_type) = bitsize_zero_node;
773           TYPE_SIZE_UNIT (record_type) = size_zero_node;
774         }
775     }
776   else
777     {
778       /* Ensure there isn't a size already set.  There can be in an error
779          case where there is a rep clause but all fields have errors and
780          no longer have a position.  */
781       TYPE_SIZE (record_type) = 0;
782       layout_type (record_type);
783     }
784
785   /* At this point, the position and size of each field is known.  It was
786      either set before entry by a rep clause, or by laying out the type
787      above.  We now make a pass through the fields (in reverse order for
788      QUAL_UNION_TYPEs) to compute the Ada size; the GCC size and alignment
789      (for rep'ed records that are not padding types); and the mode (for
790      rep'ed records).  */
791
792   if (code == QUAL_UNION_TYPE)
793     fieldlist = nreverse (fieldlist);
794
795   for (field = fieldlist; field; field = TREE_CHAIN (field))
796     {
797       tree type = TREE_TYPE (field);
798       tree this_size = DECL_SIZE (field);
799       tree this_size_unit = DECL_SIZE_UNIT (field);
800       tree this_ada_size = DECL_SIZE (field);
801
802       if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
803           || TREE_CODE (type) == QUAL_UNION_TYPE)
804           && ! TYPE_IS_FAT_POINTER_P (type)
805           && ! TYPE_CONTAINS_TEMPLATE_P (type)
806           && TYPE_ADA_SIZE (type) != 0)
807         this_ada_size = TYPE_ADA_SIZE (type);
808
809       if (has_rep && ! DECL_BIT_FIELD (field))
810         TYPE_ALIGN (record_type)
811           = MAX (TYPE_ALIGN (record_type), DECL_ALIGN (field));
812
813       switch (code)
814         {
815         case UNION_TYPE:
816           ada_size = size_binop (MAX_EXPR, ada_size, this_ada_size);
817           size = size_binop (MAX_EXPR, size, this_size);
818           size_unit = size_binop (MAX_EXPR, size_unit, this_size_unit);
819           break;
820
821         case QUAL_UNION_TYPE:
822           ada_size
823             = fold (build (COND_EXPR, bitsizetype, DECL_QUALIFIER (field),
824                            this_ada_size, ada_size));
825           size = fold (build (COND_EXPR, bitsizetype, DECL_QUALIFIER (field),
826                               this_size, size));
827           size_unit = fold (build (COND_EXPR, sizetype, DECL_QUALIFIER (field),
828                                    this_size_unit, size_unit));
829           break;
830
831         case RECORD_TYPE:
832           /* Since we know here that all fields are sorted in order of
833              increasing bit position, the size of the record is one
834              higher than the ending bit of the last field processed
835              unless we have a rep clause, since in that case we might
836              have a field outside a QUAL_UNION_TYPE that has a higher ending
837              position.  So use a MAX in that case.  Also, if this field is a
838              QUAL_UNION_TYPE, we need to take into account the previous size in
839              the case of empty variants.  */
840           ada_size
841             = merge_sizes (ada_size, bit_position (field), this_ada_size,
842                            TREE_CODE (type) == QUAL_UNION_TYPE, has_rep);
843           size = merge_sizes (size, bit_position (field), this_size,
844                               TREE_CODE (type) == QUAL_UNION_TYPE, has_rep);
845           size_unit
846             = merge_sizes (size_unit, byte_position (field), this_size_unit,
847                            TREE_CODE (type) == QUAL_UNION_TYPE, has_rep);
848           break;
849
850         default:
851           abort ();
852         }
853     }
854
855   if (code == QUAL_UNION_TYPE)
856     nreverse (fieldlist);
857
858   /* If this is a padding record, we never want to make the size smaller than
859      what was specified in it, if any.  */
860   if (TREE_CODE (record_type) == RECORD_TYPE
861       && TYPE_IS_PADDING_P (record_type) && TYPE_SIZE (record_type) != 0)
862     {
863       size = TYPE_SIZE (record_type);
864       size_unit = TYPE_SIZE_UNIT (record_type);
865     }
866
867   /* Now set any of the values we've just computed that apply.  */
868   if (! TYPE_IS_FAT_POINTER_P (record_type)
869       && ! TYPE_CONTAINS_TEMPLATE_P (record_type))
870     TYPE_ADA_SIZE (record_type) = ada_size;
871
872 #ifdef ROUND_TYPE_SIZE
873   size = ROUND_TYPE_SIZE (record_type, size, TYPE_ALIGN (record_type));
874   size_unit = ROUND_TYPE_SIZE_UNIT (record_size, size_unit,
875                                     TYPE_ALIGN (record_type) / BITS_PER_UNIT);
876 #else
877   size = round_up (size, TYPE_ALIGN (record_type));
878   size_unit = round_up (size_unit, TYPE_ALIGN (record_type) / BITS_PER_UNIT);
879 #endif
880
881   if (has_rep
882       && ! (TREE_CODE (record_type) == RECORD_TYPE
883             && TYPE_IS_PADDING_P (record_type)
884             && TREE_CODE (size) != INTEGER_CST
885             && contains_placeholder_p (size)))
886     {
887       TYPE_SIZE (record_type) = size;
888       TYPE_SIZE_UNIT (record_type) = size_unit;
889     }
890
891   if (has_rep)
892     compute_record_mode (record_type);
893
894   if (! defer_debug)
895     {
896       /* If this record is of variable size, rename it so that the
897          debugger knows it is and make a new, parallel, record
898          that tells the debugger how the record is laid out.  See
899          exp_dbug.ads.  */
900       if (TREE_CODE (TYPE_SIZE (record_type)) != INTEGER_CST)
901         {
902           tree new_record_type
903             = make_node (TREE_CODE (record_type) == QUAL_UNION_TYPE
904                          ? UNION_TYPE : TREE_CODE (record_type));
905           tree orig_id = DECL_NAME (TYPE_STUB_DECL (record_type));
906           tree new_id
907             = concat_id_with_name (orig_id,
908                                    TREE_CODE (record_type) == QUAL_UNION_TYPE
909                                    ? "XVU" : "XVE");
910           tree last_pos = bitsize_zero_node;
911           tree old_field;
912
913           TYPE_NAME (new_record_type) = new_id;
914           TYPE_ALIGN (new_record_type) = BIGGEST_ALIGNMENT;
915           TYPE_STUB_DECL (new_record_type)
916             = pushdecl (build_decl (TYPE_DECL, new_id, new_record_type));
917           DECL_ARTIFICIAL (TYPE_STUB_DECL (new_record_type)) = 1;
918           DECL_IGNORED_P (TYPE_STUB_DECL (new_record_type))
919             = DECL_IGNORED_P (TYPE_STUB_DECL (record_type));
920           TYPE_SIZE (new_record_type) = size_int (TYPE_ALIGN (record_type));
921
922           /* Now scan all the fields, replacing each field with a new
923              field corresponding to the new encoding.  */
924           for (old_field = TYPE_FIELDS (record_type); old_field != 0;
925                old_field = TREE_CHAIN (old_field))
926             {
927               tree field_type = TREE_TYPE (old_field);
928               tree field_name = DECL_NAME (old_field);
929               tree new_field;
930               tree curpos = bit_position (old_field);
931               int var = 0;
932               unsigned int align = 0;
933               tree pos;
934
935               /* See how the position was modified from the last position.
936
937                  There are two basic cases we support: a value was added
938                  to the last position or the last position was rounded to
939                  a boundary and they something was added.  Check for the
940                  first case first.  If not, see if there is any evidence
941                  of rounding.  If so, round the last position and try
942                  again. 
943
944                  If this is a union, the position can be taken as zero. */
945
946               if (TREE_CODE (new_record_type) == UNION_TYPE)
947                 pos = bitsize_zero_node, align = 0;
948               else
949                 pos = compute_related_constant (curpos, last_pos);
950
951               if (pos == 0 && TREE_CODE (curpos) == MULT_EXPR
952                   && TREE_CODE (TREE_OPERAND (curpos, 1)) == INTEGER_CST)
953                 {
954                   align = TREE_INT_CST_LOW (TREE_OPERAND (curpos, 1));
955                   pos = compute_related_constant (curpos,
956                                                   round_up (last_pos, align));
957                 }
958               else if (pos == 0 && TREE_CODE (curpos) == PLUS_EXPR
959                        && TREE_CODE (TREE_OPERAND (curpos, 1)) == INTEGER_CST
960                        && TREE_CODE (TREE_OPERAND (curpos, 0)) == MULT_EXPR
961                        && host_integerp (TREE_OPERAND
962                                          (TREE_OPERAND (curpos, 0), 1),
963                                          1))
964                 {
965                   align
966                     = tree_low_cst
967                       (TREE_OPERAND (TREE_OPERAND (curpos, 0), 1), 1);
968                   pos = compute_related_constant (curpos,
969                                                   round_up (last_pos, align));
970                 }
971
972               /* If we can't compute a position, set it to zero.
973
974                  ??? We really should abort here, but it's too much work
975                  to get this correct for all cases.  */
976
977               if (pos == 0)
978                 pos = bitsize_zero_node;
979
980               /* See if this type is variable-size and make a new type
981                  and indicate the indirection if so.  */
982               if (TREE_CODE (TYPE_SIZE (field_type)) != INTEGER_CST)
983                 {
984                   field_type = build_pointer_type (field_type);
985                   var = 1;
986                 }
987
988               /* Make a new field name, if necessary.  */
989               if (var || align != 0)
990                 {
991                   char suffix[6];
992
993                   if (align != 0)
994                     sprintf (suffix, "XV%c%u", var ? 'L' : 'A',
995                              align / BITS_PER_UNIT);
996                   else
997                     strcpy (suffix, "XVL");
998
999                   field_name = concat_id_with_name (field_name, suffix);
1000                 }
1001
1002               new_field = create_field_decl (field_name, field_type,
1003                                              new_record_type, 0,
1004                                              TYPE_SIZE (field_type), pos, 0);
1005               TREE_CHAIN (new_field) = TYPE_FIELDS (new_record_type);
1006               TYPE_FIELDS (new_record_type) = new_field;
1007
1008               /* If old_field is a QUAL_UNION_TYPE, take its size as being
1009                  zero.  The only time it's not the last field of the record
1010                  is when there are other components at fixed positions after
1011                  it (meaning there was a rep clause for every field) and we
1012                  want to be able to encode them.  */
1013               last_pos = size_binop (PLUS_EXPR, bit_position (old_field),
1014                                      (TREE_CODE (TREE_TYPE (old_field))
1015                                       == QUAL_UNION_TYPE)
1016                                      ? bitsize_zero_node
1017                                      : TYPE_SIZE (TREE_TYPE (old_field)));
1018             }
1019
1020           TYPE_FIELDS (new_record_type)
1021             = nreverse (TYPE_FIELDS (new_record_type));
1022
1023           rest_of_type_compilation (new_record_type, global_bindings_p ());
1024         }
1025
1026       rest_of_type_compilation (record_type, global_bindings_p ());
1027     }
1028 }
1029
1030 /* Utility function of above to merge LAST_SIZE, the previous size of a record
1031    with FIRST_BIT and SIZE that describe a field.  SPECIAL is nonzero
1032    if this represents a QUAL_UNION_TYPE in which case we must look for
1033    COND_EXPRs and replace a value of zero with the old size.  If HAS_REP
1034    is nonzero, we must take the MAX of the end position of this field
1035    with LAST_SIZE.  In all other cases, we use FIRST_BIT plus SIZE.
1036
1037    We return an expression for the size.  */
1038
1039 static tree
1040 merge_sizes (last_size, first_bit, size, special, has_rep)
1041      tree last_size;
1042      tree first_bit, size;
1043      int special;
1044      int has_rep;
1045 {
1046   tree type = TREE_TYPE (last_size);
1047
1048   if (! special || TREE_CODE (size) != COND_EXPR)
1049     {
1050       tree new = size_binop (PLUS_EXPR, first_bit, size);
1051
1052       if (has_rep)
1053         new = size_binop (MAX_EXPR, last_size, new);
1054
1055       return new;
1056     }
1057
1058   return fold (build (COND_EXPR, type, TREE_OPERAND (size, 0),
1059                       integer_zerop (TREE_OPERAND (size, 1))
1060                       ? last_size : merge_sizes (last_size, first_bit,
1061                                                  TREE_OPERAND (size, 1),
1062                                                  1, has_rep),
1063                       integer_zerop (TREE_OPERAND (size, 2))
1064                       ? last_size : merge_sizes (last_size, first_bit,
1065                                                  TREE_OPERAND (size, 2),
1066                                                  1, has_rep)));
1067 }
1068
1069 /* Utility function of above to see if OP0 and OP1, both of SIZETYPE, are
1070    related by the addition of a constant.  Return that constant if so.  */
1071
1072 static tree
1073 compute_related_constant (op0, op1)
1074      tree op0, op1;
1075 {
1076   tree op0_var, op1_var;
1077   tree op0_con = split_plus (op0, &op0_var);
1078   tree op1_con = split_plus (op1, &op1_var);
1079   tree result = size_binop (MINUS_EXPR, op0_con, op1_con);
1080
1081   if (operand_equal_p (op0_var, op1_var, 0))
1082     return result;
1083   else if (operand_equal_p (op0, size_binop (PLUS_EXPR, op1_var, result), 0))
1084     return result;
1085   else
1086     return 0;
1087 }
1088
1089 /* Utility function of above to split a tree OP which may be a sum, into a
1090    constant part, which is returned, and a variable part, which is stored
1091    in *PVAR.  *PVAR may be size_zero_node.  All operations must be of
1092    sizetype.  */
1093
1094 static tree
1095 split_plus (in, pvar)
1096      tree in;
1097      tree *pvar;
1098 {
1099   tree result = bitsize_zero_node;
1100
1101   while (TREE_CODE (in) == NON_LVALUE_EXPR)
1102     in = TREE_OPERAND (in, 0);
1103
1104   *pvar = in;
1105   if (TREE_CODE (in) == INTEGER_CST)
1106     {
1107       *pvar = bitsize_zero_node;
1108       return in;
1109     }
1110   else if (TREE_CODE (in) == PLUS_EXPR || TREE_CODE (in) == MINUS_EXPR)
1111     {
1112       tree lhs_var, rhs_var;
1113       tree lhs_con = split_plus (TREE_OPERAND (in, 0), &lhs_var);
1114       tree rhs_con = split_plus (TREE_OPERAND (in, 1), &rhs_var);
1115
1116       result = size_binop (PLUS_EXPR, result, lhs_con);
1117       result = size_binop (TREE_CODE (in), result, rhs_con);
1118
1119       if (lhs_var == TREE_OPERAND (in, 0)
1120           && rhs_var == TREE_OPERAND (in, 1))
1121         return bitsize_zero_node;
1122
1123       *pvar = size_binop (TREE_CODE (in), lhs_var, rhs_var);
1124       return result;
1125     }
1126   else
1127     return bitsize_zero_node;
1128 }
1129 \f
1130 /* Return a FUNCTION_TYPE node. RETURN_TYPE is the type returned by the
1131    subprogram. If it is void_type_node, then we are dealing with a procedure,
1132    otherwise we are dealing with a function. PARAM_DECL_LIST is a list of
1133    PARM_DECL nodes that are the subprogram arguments.  CICO_LIST is the
1134    copy-in/copy-out list to be stored into TYPE_CICO_LIST.
1135    RETURNS_UNCONSTRAINED is nonzero if the function returns an unconstrained
1136    object.  RETURNS_BY_REF is nonzero if the function returns by reference. 
1137    RETURNS_WITH_DSP is nonzero if the function is to return with a
1138    depressed stack pointer.  */
1139
1140 tree
1141 create_subprog_type (return_type, param_decl_list, cico_list,
1142                      returns_unconstrained, returns_by_ref, returns_with_dsp)
1143      tree return_type;
1144      tree param_decl_list;
1145      tree cico_list;
1146      int returns_unconstrained, returns_by_ref, returns_with_dsp;
1147 {
1148   /* A chain of TREE_LIST nodes whose TREE_VALUEs are the data type nodes of
1149      the subprogram formal parameters. This list is generated by traversing the
1150      input list of PARM_DECL nodes.  */
1151   tree param_type_list = NULL;
1152   tree param_decl;
1153   tree type;
1154
1155   for (param_decl = param_decl_list; param_decl;
1156        param_decl = TREE_CHAIN (param_decl))
1157     param_type_list = tree_cons (NULL_TREE, TREE_TYPE (param_decl),
1158                                           param_type_list);
1159
1160   /* The list of the function parameter types has to be terminated by the void
1161      type to signal to the back-end that we are not dealing with a variable
1162      parameter subprogram, but that the subprogram has a fixed number of
1163      parameters.  */
1164   param_type_list = tree_cons (NULL_TREE, void_type_node, param_type_list);
1165
1166   /* The list of argument types has been created in reverse
1167      so nreverse it.   */
1168   param_type_list = nreverse (param_type_list);
1169
1170   type = build_function_type (return_type, param_type_list);
1171
1172   /* TYPE may have been shared since GCC hashes types.  If it has a CICO_LIST
1173      or the new type should, make a copy of TYPE.  Likewise for
1174      RETURNS_UNCONSTRAINED and RETURNS_BY_REF.  */
1175   if (TYPE_CI_CO_LIST (type) != 0 || cico_list != 0
1176       || TYPE_RETURNS_UNCONSTRAINED_P (type) != returns_unconstrained
1177       || TYPE_RETURNS_BY_REF_P (type) != returns_by_ref)
1178     type = copy_type (type);
1179
1180   TYPE_CI_CO_LIST (type) = cico_list;
1181   TYPE_RETURNS_UNCONSTRAINED_P (type) = returns_unconstrained;
1182   TYPE_RETURNS_STACK_DEPRESSED (type) = returns_with_dsp;
1183   TYPE_RETURNS_BY_REF_P (type) = returns_by_ref;
1184   return type;
1185 }
1186 \f
1187 /* Return a copy of TYPE but safe to modify in any way.  */
1188
1189 tree
1190 copy_type (type)
1191      tree type;
1192 {
1193   tree new = copy_node (type);
1194
1195   /* copy_node clears this field instead of copying it, because it is
1196      aliased with TREE_CHAIN.  */
1197   TYPE_STUB_DECL (new) = TYPE_STUB_DECL (type);
1198
1199   TYPE_POINTER_TO (new) = 0;
1200   TYPE_REFERENCE_TO (new) = 0;
1201   TYPE_MAIN_VARIANT (new) = new;
1202   TYPE_NEXT_VARIANT (new) = 0;
1203
1204   return new;
1205 }
1206 \f
1207 /* Return an INTEGER_TYPE of SIZETYPE with range MIN to MAX and whose
1208    TYPE_INDEX_TYPE is INDEX.  */
1209
1210 tree
1211 create_index_type (min, max, index)
1212      tree min, max;
1213      tree index;
1214 {
1215   /* First build a type for the desired range.  */
1216   tree type = build_index_2_type (min, max);
1217
1218   /* If this type has the TYPE_INDEX_TYPE we want, return it.  Otherwise, if it
1219      doesn't have TYPE_INDEX_TYPE set, set it to INDEX.  If TYPE_INDEX_TYPE
1220      is set, but not to INDEX, make a copy of this type with the requested
1221      index type.  Note that we have no way of sharing these types, but that's
1222      only a small hole.  */
1223   if (TYPE_INDEX_TYPE (type) == index)
1224     return type;
1225   else if (TYPE_INDEX_TYPE (type) != 0)
1226     type = copy_type (type);
1227
1228   TYPE_INDEX_TYPE (type) = index;
1229   return type;
1230 }
1231 \f
1232 /* Return a TYPE_DECL node. TYPE_NAME gives the name of the type (a character
1233    string) and TYPE is a ..._TYPE node giving its data type. 
1234    ARTIFICIAL_P is nonzero if this is a declaration that was generated
1235    by the compiler.  DEBUG_INFO_P is nonzero if we need to write debugging
1236    information about this type.  */
1237
1238 tree
1239 create_type_decl (type_name, type, attr_list, artificial_p, debug_info_p)
1240      tree type_name;
1241      tree type;
1242      struct attrib *attr_list;
1243      int artificial_p;
1244      int debug_info_p;
1245 {
1246   tree type_decl = build_decl (TYPE_DECL, type_name, type);
1247   enum tree_code code = TREE_CODE (type);
1248
1249   DECL_ARTIFICIAL (type_decl) = artificial_p;
1250   pushdecl (type_decl);
1251   process_attributes (type_decl, attr_list);
1252
1253   /* Pass type declaration information to the debugger unless this is an
1254      UNCONSTRAINED_ARRAY_TYPE, which the debugger does not support,
1255      and ENUMERAL_TYPE or RECORD_TYPE which is handled separately,
1256      a dummy type, which will be completed later, or a type for which
1257      debugging information was not requested.  */
1258   if (code == UNCONSTRAINED_ARRAY_TYPE || TYPE_IS_DUMMY_P (type)
1259       || ! debug_info_p)
1260     DECL_IGNORED_P (type_decl) = 1;
1261   else if (code != ENUMERAL_TYPE && code != RECORD_TYPE
1262       && ! ((code == POINTER_TYPE || code == REFERENCE_TYPE)
1263             && TYPE_IS_DUMMY_P (TREE_TYPE (type))))
1264     rest_of_decl_compilation (type_decl, NULL, global_bindings_p (), 0);
1265
1266   return type_decl;
1267 }
1268
1269 /* Returns a GCC VAR_DECL node. VAR_NAME gives the name of the variable.
1270    ASM_NAME is its assembler name (if provided).  TYPE is its data type
1271    (a GCC ..._TYPE node).  VAR_INIT is the GCC tree for an optional initial
1272    expression; NULL_TREE if none.
1273
1274    CONST_FLAG is nonzero if this variable is constant.
1275
1276    PUBLIC_FLAG is nonzero if this definition is to be made visible outside of
1277    the current compilation unit. This flag should be set when processing the
1278    variable definitions in a package specification.  EXTERN_FLAG is nonzero 
1279    when processing an external variable declaration (as opposed to a
1280    definition: no storage is to be allocated for the variable here). 
1281
1282    STATIC_FLAG is only relevant when not at top level.  In that case
1283    it indicates whether to always allocate storage to the variable.   */
1284
1285 tree
1286 create_var_decl (var_name, asm_name, type, var_init, const_flag, public_flag,
1287                  extern_flag, static_flag, attr_list)
1288      tree var_name;
1289      tree asm_name;
1290      tree type;
1291      tree var_init;
1292      int const_flag;
1293      int public_flag;
1294      int extern_flag;
1295      int static_flag;
1296      struct attrib *attr_list;
1297 {
1298   int init_const
1299     = (var_init == 0
1300        ? 0
1301        : (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (var_init))
1302           && (global_bindings_p () || static_flag
1303               ? 0 != initializer_constant_valid_p (var_init,
1304                                                    TREE_TYPE (var_init))
1305               : TREE_CONSTANT (var_init))));
1306   tree var_decl
1307     = build_decl ((const_flag && init_const
1308                    /* Only make a CONST_DECL for sufficiently-small objects.
1309                       We consider complex double "sufficiently-small"  */
1310                    && TYPE_SIZE (type) != 0
1311                    && host_integerp (TYPE_SIZE_UNIT (type), 1)
1312                    && 0 >= compare_tree_int (TYPE_SIZE_UNIT (type),
1313                                              GET_MODE_SIZE (DCmode)))
1314                   ? CONST_DECL : VAR_DECL, var_name, type);
1315   tree assign_init = 0;
1316
1317   /* If this is external, throw away any initializations unless this is a
1318      CONST_DECL (meaning we have a constant); they will be done elsewhere.  If
1319      we are defining a global here, leave a constant initialization and save
1320      any variable elaborations for the elaboration routine.  Otherwise, if
1321      the initializing expression is not the same as TYPE, generate the
1322      initialization with an assignment statement, since it knows how
1323      to do the required adjustents.  */
1324
1325   if (extern_flag && TREE_CODE (var_decl) != CONST_DECL)
1326     var_init = 0;
1327
1328   if (global_bindings_p () && var_init != 0 && ! init_const)
1329     {
1330       add_pending_elaborations (var_decl, var_init);
1331       var_init = 0;
1332     }
1333
1334   else if (var_init != 0
1335            && ((TYPE_MAIN_VARIANT (TREE_TYPE (var_init))
1336                 != TYPE_MAIN_VARIANT (type))
1337                || (static_flag && ! init_const)))
1338     assign_init = var_init, var_init = 0;
1339
1340   DECL_COMMON   (var_decl) = !flag_no_common;
1341   DECL_INITIAL  (var_decl) = var_init;
1342   TREE_READONLY (var_decl) = const_flag;
1343   DECL_EXTERNAL (var_decl) = extern_flag;
1344   TREE_PUBLIC   (var_decl) = public_flag || extern_flag;
1345   TREE_CONSTANT (var_decl) = TREE_CODE (var_decl) == CONST_DECL;
1346   TREE_THIS_VOLATILE (var_decl) = TREE_SIDE_EFFECTS (var_decl)
1347     = TYPE_VOLATILE (type);
1348
1349   /* At the global binding level we need to allocate static storage for the
1350      variable if and only if its not external. If we are not at the top level
1351      we allocate automatic storage unless requested not to.  */
1352   TREE_STATIC (var_decl) = global_bindings_p () ? !extern_flag : static_flag;
1353
1354   if (asm_name != 0)
1355     SET_DECL_ASSEMBLER_NAME (var_decl, asm_name);
1356
1357   process_attributes (var_decl, attr_list);
1358
1359   /* Add this decl to the current binding level and generate any
1360      needed code and RTL. */
1361   var_decl = pushdecl (var_decl);
1362   expand_decl (var_decl);
1363
1364   if (DECL_CONTEXT (var_decl) != 0)
1365     expand_decl_init (var_decl);
1366
1367   /* If this is volatile, force it into memory.  */
1368   if (TREE_SIDE_EFFECTS (var_decl))
1369     mark_addressable (var_decl);
1370
1371   if (TREE_CODE (var_decl) != CONST_DECL)
1372     rest_of_decl_compilation (var_decl, 0, global_bindings_p (), 0);
1373
1374   if (assign_init != 0)
1375     {
1376       /* If VAR_DECL has a padded type, convert it to the unpadded
1377          type so the assignment is done properly.  */
1378       tree lhs = var_decl;
1379
1380       if (TREE_CODE (TREE_TYPE (lhs)) == RECORD_TYPE
1381           && TYPE_IS_PADDING_P (TREE_TYPE (lhs)))
1382         lhs = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (lhs))), lhs);
1383
1384       expand_expr_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE, lhs,
1385                                          assign_init));
1386     }
1387
1388   return var_decl;
1389 }
1390 \f
1391 /* Returns a FIELD_DECL node. FIELD_NAME the field name, FIELD_TYPE is its
1392    type, and RECORD_TYPE is the type of the parent.  PACKED is nonzero if
1393    this field is in a record type with a "pragma pack".  If SIZE is nonzero
1394    it is the specified size for this field.  If POS is nonzero, it is the bit
1395    position.  If ADDRESSABLE is nonzero, it means we are allowed to take
1396    the address of this field for aliasing purposes.  */
1397
1398 tree
1399 create_field_decl (field_name, field_type, record_type, packed, size, pos,
1400                    addressable)
1401      tree field_name;
1402      tree field_type;
1403      tree record_type;
1404      int packed;
1405      tree size, pos;
1406      int addressable;
1407 {
1408   tree field_decl = build_decl (FIELD_DECL, field_name, field_type);
1409
1410   DECL_CONTEXT (field_decl) = record_type;
1411   TREE_READONLY (field_decl) = TREE_READONLY (field_type);
1412
1413   /* If FIELD_TYPE is BLKmode, we must ensure this is aligned to at least a
1414      byte boundary since GCC cannot handle less-aligned BLKmode bitfields.
1415      If it is a padding type where the inner field is of variable size, it
1416      must be at its natural alignment.  Just handle the packed case here; we
1417      will disallow non-aligned rep clauses elsewhere.  */
1418   if (packed && TYPE_MODE (field_type) == BLKmode)
1419     DECL_ALIGN (field_decl)
1420       = ((TREE_CODE (field_type) == RECORD_TYPE
1421           && TYPE_IS_PADDING_P (field_type)
1422           && ! TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (field_type))))
1423          ?  TYPE_ALIGN (field_type) : BITS_PER_UNIT);
1424
1425   /* If a size is specified, use it.  Otherwise, see if we have a size
1426      to use that may differ from the natural size of the object.  */
1427   if (size != 0)
1428     size = convert (bitsizetype, size);
1429   else if (packed)
1430     {
1431       if (packed == 1 && ! operand_equal_p (rm_size (field_type),
1432                                             TYPE_SIZE (field_type), 0))
1433         size = rm_size (field_type);
1434
1435       /* For a constant size larger than MAX_FIXED_MODE_SIZE, round up to
1436          byte.  */
1437       if (size != 0 && TREE_CODE (size) == INTEGER_CST
1438           && compare_tree_int (size, MAX_FIXED_MODE_SIZE) > 0)
1439         size = round_up (size, BITS_PER_UNIT);
1440     }
1441
1442   /* Make a bitfield if a size is specified for two reasons: first if the size
1443      differs from the natural size.  Second, if the alignment is insufficient.
1444      There are a number of ways the latter can be true.  But never make a
1445      bitfield if the type of the field has a nonconstant size.  */
1446
1447   if (size != 0 && TREE_CODE (size) == INTEGER_CST
1448       && TREE_CODE (TYPE_SIZE (field_type)) == INTEGER_CST
1449       && (! operand_equal_p (TYPE_SIZE (field_type), size, 0)
1450           || (pos != 0
1451               && ! value_zerop (size_binop (TRUNC_MOD_EXPR, pos,
1452                                             bitsize_int (TYPE_ALIGN
1453                                                          (field_type)))))
1454           || packed
1455           || (TYPE_ALIGN (record_type) != 0
1456               && TYPE_ALIGN (record_type) < TYPE_ALIGN (field_type))))
1457     {
1458       DECL_BIT_FIELD (field_decl) = 1;
1459       DECL_SIZE (field_decl) = size;
1460       if (! packed && pos == 0)
1461         DECL_ALIGN (field_decl)
1462           = (TYPE_ALIGN (record_type) != 0
1463              ? MIN (TYPE_ALIGN (record_type), TYPE_ALIGN (field_type))
1464              : TYPE_ALIGN (field_type));
1465     }
1466
1467   DECL_PACKED (field_decl) = pos != 0 ? DECL_BIT_FIELD (field_decl) : packed;
1468   DECL_ALIGN (field_decl)
1469     = MAX (DECL_ALIGN (field_decl),
1470            DECL_BIT_FIELD (field_decl) ? 1
1471            : packed && TYPE_MODE (field_type) != BLKmode ? BITS_PER_UNIT
1472            : TYPE_ALIGN (field_type));
1473
1474   if (pos != 0)
1475     {
1476       /* We need to pass in the alignment the DECL is known to have.
1477          This is the lowest-order bit set in POS, but no more than
1478          the alignment of the record, if one is specified.  Note
1479          that an alignment of 0 is taken as infinite.  */
1480       unsigned int known_align;
1481
1482       if (host_integerp (pos, 1))
1483         known_align = tree_low_cst (pos, 1) & - tree_low_cst (pos, 1);
1484       else
1485         known_align = BITS_PER_UNIT;
1486
1487       if (TYPE_ALIGN (record_type)
1488           && (known_align == 0 || known_align > TYPE_ALIGN (record_type)))
1489         known_align = TYPE_ALIGN (record_type);
1490
1491       layout_decl (field_decl, known_align);
1492       SET_DECL_OFFSET_ALIGN (field_decl, BIGGEST_ALIGNMENT);
1493       pos_from_bit (&DECL_FIELD_OFFSET (field_decl),
1494                     &DECL_FIELD_BIT_OFFSET (field_decl),
1495                     BIGGEST_ALIGNMENT, pos);
1496
1497       DECL_HAS_REP_P (field_decl) = 1;
1498     }
1499
1500   /* Mark the decl as nonaddressable if it either is indicated so semantically
1501      or if it is a bit field.  */
1502   DECL_NONADDRESSABLE_P (field_decl)
1503     = ! addressable || DECL_BIT_FIELD (field_decl);
1504
1505   return field_decl;
1506 }
1507
1508 /* Subroutine of previous function: return nonzero if EXP, ignoring any side
1509    effects, has the value of zero.  */
1510
1511 static int
1512 value_zerop (exp)
1513      tree exp;
1514 {
1515   if (TREE_CODE (exp) == COMPOUND_EXPR)
1516     return value_zerop (TREE_OPERAND (exp, 1));
1517
1518   return integer_zerop (exp);
1519 }
1520 \f
1521 /* Returns a PARM_DECL node. PARAM_NAME is the name of the parameter,
1522    PARAM_TYPE is its type.  READONLY is nonzero if the parameter is
1523    readonly (either an IN parameter or an address of a pass-by-ref
1524    parameter). */
1525
1526 tree
1527 create_param_decl (param_name, param_type, readonly)
1528      tree param_name;
1529      tree param_type;
1530      int readonly;
1531 {
1532   tree param_decl = build_decl (PARM_DECL, param_name, param_type);
1533
1534   DECL_ARG_TYPE (param_decl) = param_type;
1535   DECL_ARG_TYPE_AS_WRITTEN (param_decl) = param_type;
1536   TREE_READONLY (param_decl) = readonly;
1537   return param_decl;
1538 }
1539 \f
1540 /* Given a DECL and ATTR_LIST, process the listed attributes.  */
1541
1542 void
1543 process_attributes (decl, attr_list)
1544      tree decl;
1545      struct attrib *attr_list;
1546 {
1547   for (; attr_list; attr_list = attr_list->next)
1548     switch (attr_list->type)
1549       {
1550       case ATTR_MACHINE_ATTRIBUTE:
1551         decl_attributes (&decl, tree_cons (attr_list->name, attr_list->arg,
1552                                            NULL_TREE),
1553                          ATTR_FLAG_TYPE_IN_PLACE);
1554         break;
1555
1556       case ATTR_LINK_ALIAS:
1557         TREE_STATIC (decl) = 1;
1558         assemble_alias (decl, attr_list->name);
1559         break;
1560
1561       case ATTR_WEAK_EXTERNAL:
1562         if (SUPPORTS_WEAK)
1563           declare_weak (decl);
1564         else
1565           post_error ("?weak declarations not supported on this target",
1566                       attr_list->error_point);
1567         break;
1568
1569       case ATTR_LINK_SECTION:
1570 #ifdef ASM_OUTPUT_SECTION_NAME
1571         DECL_SECTION_NAME (decl)
1572           = build_string (IDENTIFIER_LENGTH (attr_list->name),
1573                           IDENTIFIER_POINTER (attr_list->name));
1574         DECL_COMMON (decl) = 0;
1575 #else
1576         post_error ("?section attributes are not supported for this target",
1577                     attr_list->error_point);
1578 #endif
1579         break;
1580       }
1581 }
1582 \f
1583 /* Add some pending elaborations on the list.  */
1584
1585 void 
1586 add_pending_elaborations (var_decl, var_init)
1587      tree var_decl;
1588      tree var_init;
1589 {
1590   if (var_init != 0)
1591     Check_Elaboration_Code_Allowed (error_gnat_node);
1592
1593   pending_elaborations
1594     = chainon (pending_elaborations, build_tree_list (var_decl, var_init));
1595 }
1596
1597 /* Obtain any pending elaborations and clear the old list.  */
1598
1599 tree
1600 get_pending_elaborations ()
1601 {
1602   /* Each thing added to the list went on the end; we want it on the
1603      beginning.  */
1604   tree result = TREE_CHAIN (pending_elaborations);
1605
1606   TREE_CHAIN (pending_elaborations) = 0;
1607   return result;
1608 }
1609
1610 /* Mark the binding level stack.  */
1611
1612 static void
1613 mark_binding_level (arg)
1614      PTR arg;
1615 {
1616   struct binding_level *level = *(struct binding_level **) arg;
1617
1618   for (; level != 0; level = level->level_chain)
1619     {
1620       ggc_mark_tree (level->names);
1621       ggc_mark_tree (level->blocks);
1622       ggc_mark_tree (level->this_block);
1623     }
1624 }
1625
1626 /* Mark the pending elaboration list.  */
1627
1628 static void
1629 mark_e_stack (data)
1630      PTR data;
1631 {
1632   struct e_stack *p = *((struct e_stack **) data);
1633
1634   if (p != 0)
1635     {
1636       ggc_mark_tree (p->elab_list);
1637       mark_e_stack (&p->next);
1638     }
1639 }
1640
1641 /* Return nonzero if there are pending elaborations.  */
1642
1643 int
1644 pending_elaborations_p ()
1645 {
1646   return TREE_CHAIN (pending_elaborations) != 0;
1647 }
1648
1649 /* Save a copy of the current pending elaboration list and make a new
1650    one.  */
1651
1652 void
1653 push_pending_elaborations ()
1654 {
1655   struct e_stack *p = (struct e_stack *) xmalloc (sizeof (struct e_stack));
1656
1657   p->next = elist_stack;
1658   p->elab_list = pending_elaborations;
1659   elist_stack = p;
1660   pending_elaborations = build_tree_list (NULL_TREE, NULL_TREE);
1661 }
1662
1663 /* Pop the stack of pending elaborations.  */
1664
1665 void
1666 pop_pending_elaborations ()
1667 {
1668   struct e_stack *p = elist_stack;
1669
1670   pending_elaborations = p->elab_list;
1671   elist_stack = p->next;
1672   free (p);
1673 }
1674
1675 /* Return the current position in pending_elaborations so we can insert
1676    elaborations after that point.  */
1677
1678 tree
1679 get_elaboration_location ()
1680 {
1681   return tree_last (pending_elaborations);
1682 }
1683
1684 /* Insert the current elaborations after ELAB, which is in some elaboration
1685    list.  */
1686
1687 void
1688 insert_elaboration_list (elab)
1689      tree elab;
1690 {
1691   tree next = TREE_CHAIN (elab);
1692
1693   if (TREE_CHAIN (pending_elaborations))
1694     {
1695       TREE_CHAIN (elab) = TREE_CHAIN (pending_elaborations);
1696       TREE_CHAIN (tree_last (pending_elaborations)) = next;
1697       TREE_CHAIN (pending_elaborations) = 0;
1698     }
1699 }
1700
1701 /* Returns a LABEL_DECL node for LABEL_NAME.  */
1702
1703 tree
1704 create_label_decl (label_name)
1705      tree label_name;
1706 {
1707   tree label_decl = build_decl (LABEL_DECL, label_name, void_type_node);
1708
1709   DECL_CONTEXT (label_decl)     = current_function_decl;
1710   DECL_MODE (label_decl)        = VOIDmode;
1711   DECL_SOURCE_LINE (label_decl) = lineno;
1712   DECL_SOURCE_FILE (label_decl) = input_filename;
1713
1714   return label_decl;
1715 }
1716 \f
1717 /* Returns a FUNCTION_DECL node.  SUBPROG_NAME is the name of the subprogram,
1718    ASM_NAME is its assembler name, SUBPROG_TYPE is its type (a FUNCTION_TYPE
1719    node), PARAM_DECL_LIST is the list of the subprogram arguments (a list of
1720    PARM_DECL nodes chained through the TREE_CHAIN field).
1721
1722    INLINE_FLAG, PUBLIC_FLAG, and EXTERN_FLAG are used to set the appropriate
1723    fields in the FUNCTION_DECL.  */
1724
1725 tree
1726 create_subprog_decl (subprog_name, asm_name, subprog_type, param_decl_list,
1727                      inline_flag, public_flag, extern_flag, attr_list)
1728      tree subprog_name;
1729      tree asm_name;
1730      tree subprog_type;
1731      tree param_decl_list;
1732      int inline_flag;
1733      int public_flag;
1734      int extern_flag;
1735      struct attrib *attr_list;
1736 {
1737   tree return_type  = TREE_TYPE (subprog_type);
1738   tree subprog_decl = build_decl (FUNCTION_DECL, subprog_name, subprog_type);
1739
1740   /* If this is a function nested inside an inlined external function, it
1741      means we aren't going to compile the outer function unless it is
1742      actually inlined, so do the same for us.  */
1743   if (current_function_decl != 0 && DECL_INLINE (current_function_decl)
1744       && DECL_EXTERNAL (current_function_decl))
1745     extern_flag = 1;
1746
1747   DECL_EXTERNAL (subprog_decl)  = extern_flag;
1748   TREE_PUBLIC (subprog_decl)    = public_flag;
1749   DECL_INLINE (subprog_decl)    = inline_flag;
1750   TREE_READONLY (subprog_decl)  = TYPE_READONLY (subprog_type);
1751   TREE_THIS_VOLATILE (subprog_decl) = TYPE_VOLATILE (subprog_type);
1752   TREE_SIDE_EFFECTS (subprog_decl) = TYPE_VOLATILE (subprog_type);
1753   DECL_ARGUMENTS (subprog_decl) = param_decl_list;
1754   DECL_RESULT (subprog_decl)    = build_decl (RESULT_DECL, 0, return_type);
1755
1756   if (asm_name != 0)
1757     DECL_ASSEMBLER_NAME (subprog_decl) = asm_name;
1758
1759   process_attributes (subprog_decl, attr_list);
1760
1761   /* Add this decl to the current binding level.  */
1762   subprog_decl = pushdecl (subprog_decl);
1763
1764   /* Output the assembler code and/or RTL for the declaration.  */
1765   rest_of_decl_compilation (subprog_decl, 0, global_bindings_p (), 0);
1766
1767   return subprog_decl;
1768 }
1769 \f
1770 /* Count how deep we are into nested functions.  This is because
1771    we shouldn't call the backend function context routines unless we
1772    are in a nested function.  */
1773
1774 static int function_nesting_depth;
1775
1776 /* Set up the framework for generating code for SUBPROG_DECL, a subprogram
1777    body. This routine needs to be invoked before processing the declarations
1778    appearing in the subprogram.  */
1779
1780 void
1781 begin_subprog_body (subprog_decl)
1782      tree subprog_decl;
1783 {
1784   tree param_decl_list;
1785   tree param_decl;
1786   tree next_param;
1787
1788   if (function_nesting_depth++ != 0)
1789     push_function_context ();
1790
1791   announce_function (subprog_decl);
1792
1793   /* Make this field nonzero so further routines know that this is not
1794      tentative. error_mark_node is replaced below (in poplevel) with the
1795      adequate BLOCK.  */
1796   DECL_INITIAL (subprog_decl)  = error_mark_node;
1797
1798   /* This function exists in static storage. This does not mean `static' in
1799      the C sense!  */
1800   TREE_STATIC (subprog_decl)   = 1;
1801
1802   /* Enter a new binding level.  */
1803   current_function_decl = subprog_decl;
1804   pushlevel (0);
1805
1806   /* Push all the PARM_DECL nodes onto the current scope (i.e. the scope of the
1807      subprogram body) so that they can be recognized as local variables in the
1808      subprogram. 
1809
1810      The list of PARM_DECL nodes is stored in the right order in
1811      DECL_ARGUMENTS.  Since ..._DECL nodes get stored in the reverse order in
1812      which they are transmitted to `pushdecl' we need to reverse the list of
1813      PARM_DECLs if we want it to be stored in the right order. The reason why
1814      we want to make sure the PARM_DECLs are stored in the correct order is
1815      that this list will be retrieved in a few lines with a call to `getdecl'
1816      to store it back into the DECL_ARGUMENTS field.  */
1817     param_decl_list = nreverse (DECL_ARGUMENTS (subprog_decl));
1818
1819     for (param_decl = param_decl_list; param_decl; param_decl = next_param)
1820       {
1821         next_param = TREE_CHAIN (param_decl);
1822         TREE_CHAIN (param_decl) = NULL;
1823         pushdecl (param_decl);
1824       }
1825
1826   /* Store back the PARM_DECL nodes. They appear in the right order. */
1827   DECL_ARGUMENTS (subprog_decl) = getdecls ();
1828
1829   init_function_start   (subprog_decl, input_filename, lineno);
1830   expand_function_start (subprog_decl, 0);
1831 }
1832
1833
1834 /* Finish the definition of the current subprogram and compile it all the way
1835    to assembler language output.  */
1836
1837 void
1838 end_subprog_body (void)
1839 {
1840   tree decl;
1841   tree cico_list;
1842
1843   poplevel (1, 0, 1);
1844   BLOCK_SUPERCONTEXT (DECL_INITIAL (current_function_decl))
1845     = current_function_decl;
1846
1847   /* Mark the RESULT_DECL as being in this subprogram. */
1848   DECL_CONTEXT (DECL_RESULT (current_function_decl)) = current_function_decl;
1849
1850   expand_function_end (input_filename, lineno, 0);
1851   rest_of_compilation (current_function_decl);
1852
1853 #if 0
1854   /* If we're sure this function is defined in this file then mark it
1855      as such */
1856   if (TREE_ASM_WRITTEN (current_function_decl))
1857     mark_fn_defined_in_this_file (current_function_decl);
1858 #endif
1859
1860   /* Throw away any VAR_DECLs we made for OUT parameters; they must
1861      not be seen when we call this function and will be in
1862      unallocated memory anyway.  */
1863   for (cico_list = TYPE_CI_CO_LIST (TREE_TYPE (current_function_decl));
1864        cico_list != 0; cico_list = TREE_CHAIN (cico_list))
1865     TREE_VALUE (cico_list) = 0;
1866
1867   if (DECL_SAVED_INSNS (current_function_decl) == 0)
1868     {
1869       /* Throw away DECL_RTL in any PARM_DECLs unless this function
1870          was saved for inline, in which case the DECL_RTLs are in
1871          preserved memory.  */
1872       for (decl = DECL_ARGUMENTS (current_function_decl);
1873            decl != 0; decl = TREE_CHAIN (decl))
1874         {
1875           SET_DECL_RTL (decl, 0);
1876           DECL_INCOMING_RTL (decl) = 0;
1877         }
1878
1879       /* Similarly, discard DECL_RTL of the return value.  */
1880       SET_DECL_RTL (DECL_RESULT (current_function_decl), 0);
1881
1882       /* But DECL_INITIAL must remain nonzero so we know this
1883          was an actual function definition unless toplev.c decided not
1884          to inline it.  */
1885       if (DECL_INITIAL (current_function_decl) != 0)
1886         DECL_INITIAL (current_function_decl) = error_mark_node;
1887
1888       DECL_ARGUMENTS (current_function_decl) = 0;
1889     }
1890
1891   /* If we are not at the bottom of the function nesting stack, pop up to
1892      the containing function.  Otherwise show we aren't in any function.  */
1893   if (--function_nesting_depth != 0)
1894     pop_function_context ();
1895   else
1896     current_function_decl = 0;
1897 }
1898 \f
1899 /* Return a definition for a builtin function named NAME and whose data type
1900    is TYPE.  TYPE should be a function type with argument types.
1901    FUNCTION_CODE tells later passes how to compile calls to this function.
1902    See tree.h for its possible values.
1903
1904    If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
1905    the name to be called if we can't opencode the function.  */
1906
1907 tree
1908 builtin_function (name, type, function_code, class, library_name)
1909      const char *name;
1910      tree type;
1911      int function_code;
1912      enum built_in_class class;
1913      const char *library_name;
1914 {
1915   tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
1916
1917   DECL_EXTERNAL (decl) = 1;
1918   TREE_PUBLIC (decl) = 1;
1919   if (library_name)
1920     DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
1921
1922   pushdecl (decl);
1923   DECL_BUILT_IN_CLASS (decl) = class;
1924   DECL_FUNCTION_CODE (decl) = function_code;
1925   return decl;
1926 }
1927
1928 /* Return an integer type with the number of bits of precision given by  
1929    PRECISION.  UNSIGNEDP is nonzero if the type is unsigned; otherwise
1930    it is a signed type.  */
1931
1932 tree
1933 type_for_size (precision, unsignedp)
1934      unsigned precision;
1935      int unsignedp;
1936 {
1937   tree t;
1938   char type_name[20];
1939
1940   if (precision <= 2 * MAX_BITS_PER_WORD
1941       && signed_and_unsigned_types[precision][unsignedp] != 0)
1942     return signed_and_unsigned_types[precision][unsignedp];
1943
1944  if (unsignedp)
1945     t = make_unsigned_type (precision);
1946   else
1947     t = make_signed_type (precision);
1948
1949   if (precision <= 2 * MAX_BITS_PER_WORD)
1950     signed_and_unsigned_types[precision][unsignedp] = t;
1951
1952   if (TYPE_NAME (t) == 0)
1953     {
1954       sprintf (type_name, "%sSIGNED_%d", unsignedp ? "UN" : "", precision);
1955       TYPE_NAME (t) = get_identifier (type_name);
1956     }
1957
1958   return t;
1959 }
1960
1961 /* Likewise for floating-point types.  */
1962
1963 static tree
1964 float_type_for_size (precision, mode)
1965      int precision;
1966      enum machine_mode mode;
1967 {
1968   tree t;
1969   char type_name[20];
1970
1971   if (float_types[(int) mode] != 0)
1972     return float_types[(int) mode];
1973
1974   float_types[(int) mode] = t = make_node (REAL_TYPE);
1975   TYPE_PRECISION (t) = precision;
1976   layout_type (t);
1977
1978   if (TYPE_MODE (t) != mode)
1979     gigi_abort (414);
1980
1981   if (TYPE_NAME (t) == 0)
1982     {
1983       sprintf (type_name, "FLOAT_%d", precision);
1984       TYPE_NAME (t) = get_identifier (type_name);
1985     }
1986
1987   return t;
1988 }
1989
1990 /* Return a data type that has machine mode MODE.  UNSIGNEDP selects
1991    an unsigned type; otherwise a signed type is returned.  */
1992
1993 tree
1994 type_for_mode (mode, unsignedp)
1995      enum machine_mode mode;
1996      int unsignedp;
1997 {
1998   if (GET_MODE_CLASS (mode) == MODE_FLOAT)
1999     return float_type_for_size (GET_MODE_BITSIZE (mode), mode);
2000   else
2001     return type_for_size (GET_MODE_BITSIZE (mode), unsignedp);
2002 }
2003
2004 /* Return the unsigned version of a TYPE_NODE, a scalar type.  */
2005
2006 tree
2007 unsigned_type (type_node)
2008      tree type_node;
2009 {
2010   tree type = type_for_size (TYPE_PRECISION (type_node), 1);
2011
2012   if (TREE_CODE (type_node) == INTEGER_TYPE && TYPE_MODULAR_P (type_node))
2013     {
2014       type = copy_node (type);
2015       TREE_TYPE (type) = type_node;
2016     }
2017   else if (TREE_TYPE (type_node) != 0
2018            && TREE_CODE (TREE_TYPE (type_node)) == INTEGER_TYPE
2019            && TYPE_MODULAR_P (TREE_TYPE (type_node)))
2020     {
2021       type = copy_node (type);
2022       TREE_TYPE (type) = TREE_TYPE (type_node);
2023     }
2024
2025   return type;
2026 }
2027
2028 /* Return the signed version of a TYPE_NODE, a scalar type.  */
2029
2030 tree
2031 signed_type (type_node)
2032      tree type_node;
2033 {
2034   tree type = type_for_size (TYPE_PRECISION (type_node), 0);
2035
2036   if (TREE_CODE (type_node) == INTEGER_TYPE && TYPE_MODULAR_P (type_node))
2037     {
2038       type = copy_node (type);
2039       TREE_TYPE (type) = type_node;
2040     }
2041   else if (TREE_TYPE (type_node) != 0
2042            && TREE_CODE (TREE_TYPE (type_node)) == INTEGER_TYPE
2043            && TYPE_MODULAR_P (TREE_TYPE (type_node)))
2044     {
2045       type = copy_node (type);
2046       TREE_TYPE (type) = TREE_TYPE (type_node);
2047     }
2048
2049   return type;
2050 }
2051
2052 /* Return a type the same as TYPE except unsigned or signed according to
2053    UNSIGNEDP.  */
2054
2055 tree
2056 signed_or_unsigned_type (unsignedp, type)
2057      int unsignedp;
2058      tree type;
2059 {
2060   if (! INTEGRAL_TYPE_P (type) || TREE_UNSIGNED (type) == unsignedp)
2061     return type;
2062   else
2063     return type_for_size (TYPE_PRECISION (type), unsignedp);
2064 }
2065 \f
2066 /* EXP is an expression for the size of an object.  If this size contains
2067    discriminant references, replace them with the maximum (if MAX_P) or
2068    minimum (if ! MAX_P) possible value of the discriminant.  */
2069
2070 tree
2071 max_size (exp, max_p)
2072      tree exp;
2073      int max_p;
2074 {
2075   enum tree_code code = TREE_CODE (exp);
2076   tree type = TREE_TYPE (exp);
2077
2078   switch (TREE_CODE_CLASS (code))
2079     {
2080     case 'd':
2081     case 'c':
2082       return exp;
2083
2084     case 'x':
2085       if (code == TREE_LIST)
2086         return tree_cons (TREE_PURPOSE (exp),
2087                           max_size (TREE_VALUE (exp), max_p),
2088                           TREE_CHAIN (exp) != 0
2089                           ? max_size (TREE_CHAIN (exp), max_p) : 0);
2090       break;
2091
2092     case 'r':
2093       /* If this contains a PLACEHOLDER_EXPR, it is the thing we want to
2094          modify.  Otherwise, we abort since it is something we can't
2095          handle.  */
2096       if (! contains_placeholder_p (exp))
2097         gigi_abort (406);
2098
2099       type = TREE_TYPE (TREE_OPERAND (exp, 1));
2100       return
2101         max_size (max_p ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type), 1);
2102
2103     case '<':
2104       return max_p ? size_one_node : size_zero_node;
2105
2106     case '1':
2107     case '2':
2108     case 'e':
2109       switch (TREE_CODE_LENGTH (code))
2110         {
2111         case 1:
2112           if (code == NON_LVALUE_EXPR)
2113             return max_size (TREE_OPERAND (exp, 0), max_p);
2114           else
2115             return
2116               fold (build1 (code, type,
2117                             max_size (TREE_OPERAND (exp, 0),
2118                                       code == NEGATE_EXPR ? ! max_p : max_p)));
2119
2120         case 2:
2121           if (code == RTL_EXPR)
2122             gigi_abort (407);
2123           else if (code == COMPOUND_EXPR)
2124             return max_size (TREE_OPERAND (exp, 1), max_p);
2125           else if (code == WITH_RECORD_EXPR)
2126             return exp;
2127
2128           {
2129             tree lhs = max_size (TREE_OPERAND (exp, 0), max_p);
2130             tree rhs = max_size (TREE_OPERAND (exp, 1),
2131                                  code == MINUS_EXPR ? ! max_p : max_p);
2132
2133             /* Special-case wanting the maximum value of a MIN_EXPR.
2134                In that case, if one side overflows, return the other.
2135                sizetype is signed, but we know sizes are non-negative.
2136                Likewise, handle a MINUS_EXPR or PLUS_EXPR with the LHS
2137                overflowing or the maximum possible value and the RHS
2138                a variable.  */
2139             if (max_p && code == MIN_EXPR && TREE_OVERFLOW (rhs))
2140               return lhs;
2141             else if (max_p && code == MIN_EXPR && TREE_OVERFLOW (lhs))
2142               return rhs;
2143             else if ((code == MINUS_EXPR || code == PLUS_EXPR)
2144                      && (TREE_OVERFLOW (lhs)
2145                          || operand_equal_p (lhs, TYPE_MAX_VALUE (type), 0))
2146                      && ! TREE_CONSTANT (rhs))
2147               return lhs;
2148             else
2149               return fold (build (code, type, lhs, rhs));
2150           }
2151
2152         case 3:
2153           if (code == SAVE_EXPR)
2154             return exp;
2155           else if (code == COND_EXPR)
2156             return fold (build (MAX_EXPR, type,
2157                                 max_size (TREE_OPERAND (exp, 1), max_p),
2158                                 max_size (TREE_OPERAND (exp, 2), max_p)));
2159           else if (code == CALL_EXPR && TREE_OPERAND (exp, 1) != 0)
2160             return build (CALL_EXPR, type, TREE_OPERAND (exp, 0),
2161                           max_size (TREE_OPERAND (exp, 1), max_p));
2162         }
2163     }
2164
2165   gigi_abort (408);
2166 }
2167 \f
2168 /* Build a template of type TEMPLATE_TYPE from the array bounds of ARRAY_TYPE.
2169    EXPR is an expression that we can use to locate any PLACEHOLDER_EXPRs.
2170    Return a constructor for the template.  */
2171
2172 tree
2173 build_template (template_type, array_type, expr)
2174      tree template_type;
2175      tree array_type;
2176      tree expr;
2177 {
2178   tree template_elts = NULL_TREE;
2179   tree bound_list = NULL_TREE;
2180   tree field;
2181
2182   if (TREE_CODE (array_type) == RECORD_TYPE
2183       && (TYPE_IS_PADDING_P (array_type)
2184           || TYPE_LEFT_JUSTIFIED_MODULAR_P (array_type)))
2185     array_type = TREE_TYPE (TYPE_FIELDS (array_type));
2186
2187   if (TREE_CODE (array_type) == ARRAY_TYPE
2188       || (TREE_CODE (array_type) == INTEGER_TYPE
2189           && TYPE_HAS_ACTUAL_BOUNDS_P (array_type)))
2190     bound_list = TYPE_ACTUAL_BOUNDS (array_type);
2191
2192   /* First make the list for a CONSTRUCTOR for the template.   Go down the
2193      field list of the template instead of the type chain because this
2194      array might be an Ada array of arrays and we can't tell where the
2195      nested arrays stop being the underlying object.  */
2196
2197   for (field = TYPE_FIELDS (template_type); field;
2198        (bound_list != 0
2199         ? (bound_list = TREE_CHAIN (bound_list))
2200         : (array_type = TREE_TYPE (array_type))),
2201        field = TREE_CHAIN (TREE_CHAIN (field)))
2202     {
2203       tree bounds, min, max;
2204
2205       /* If we have a bound list, get the bounds from there.  Likewise
2206          for an ARRAY_TYPE.  Otherwise, if expr is a PARM_DECL with
2207          DECL_BY_COMPONENT_PTR_P, use the bounds of the field in the template.
2208          This will give us a maximum range.  */
2209       if (bound_list != 0)
2210         bounds = TREE_VALUE (bound_list);
2211       else if (TREE_CODE (array_type) == ARRAY_TYPE)
2212         bounds = TYPE_INDEX_TYPE (TYPE_DOMAIN (array_type));
2213       else if (expr != 0 && TREE_CODE (expr) == PARM_DECL
2214                && DECL_BY_COMPONENT_PTR_P (expr))
2215         bounds = TREE_TYPE (field);
2216       else
2217         gigi_abort (411);
2218
2219       min = convert (TREE_TYPE (TREE_CHAIN (field)), TYPE_MIN_VALUE (bounds));
2220       max = convert (TREE_TYPE (field), TYPE_MAX_VALUE (bounds));
2221
2222       /* If either MIN or MAX involve a PLACEHOLDER_EXPR, we must
2223          surround them with a WITH_RECORD_EXPR giving EXPR as the
2224          OBJECT.  */
2225       if (! TREE_CONSTANT (min) && contains_placeholder_p (min))
2226         min = build (WITH_RECORD_EXPR, TREE_TYPE (min), min, expr);
2227       if (! TREE_CONSTANT (max) && contains_placeholder_p (max))
2228         max = build (WITH_RECORD_EXPR, TREE_TYPE (max), max, expr);
2229
2230       template_elts = tree_cons (TREE_CHAIN (field), max,
2231                                  tree_cons (field, min, template_elts));
2232     }
2233
2234   return build_constructor (template_type, nreverse (template_elts));
2235 }
2236 \f
2237 /* Build a VMS descriptor from a Mechanism_Type, which must specify
2238    a descriptor type, and the GCC type of an object.  Each FIELD_DECL
2239    in the type contains in its DECL_INITIAL the expression to use when
2240    a constructor is made for the type.  GNAT_ENTITY is a gnat node used
2241    to print out an error message if the mechanism cannot be applied to
2242    an object of that type and also for the name.  */
2243
2244 tree
2245 build_vms_descriptor (type, mech, gnat_entity)
2246      tree type;
2247      Mechanism_Type mech;
2248      Entity_Id gnat_entity;
2249 {
2250   tree record_type = make_node (RECORD_TYPE);
2251   tree field_list = 0;
2252   int class;
2253   int dtype = 0;
2254   tree inner_type;
2255   int ndim;
2256   int i;
2257   tree *idx_arr;
2258   tree tem;
2259
2260   /* If TYPE is an unconstrained array, use the underlying array type.  */
2261   if (TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
2262     type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (type))));
2263
2264   /* If this is an array, compute the number of dimensions in the array,
2265      get the index types, and point to the inner type.  */
2266   if (TREE_CODE (type) != ARRAY_TYPE)
2267     ndim = 0;
2268   else
2269     for (ndim = 1, inner_type = type;
2270          TREE_CODE (TREE_TYPE (inner_type)) == ARRAY_TYPE
2271          && TYPE_MULTI_ARRAY_P (TREE_TYPE (inner_type));
2272          ndim++, inner_type = TREE_TYPE (inner_type))
2273       ;
2274
2275   idx_arr = (tree *) alloca (ndim * sizeof (tree));
2276
2277   if (mech != By_Descriptor_NCA
2278       && TREE_CODE (type) == ARRAY_TYPE && TYPE_CONVENTION_FORTRAN_P (type))
2279     for (i = ndim - 1, inner_type = type;
2280          i >= 0;
2281          i--, inner_type = TREE_TYPE (inner_type))
2282       idx_arr[i] = TYPE_DOMAIN (inner_type);
2283   else
2284     for (i = 0, inner_type = type;
2285          i < ndim;
2286          i++, inner_type = TREE_TYPE (inner_type))
2287       idx_arr[i] = TYPE_DOMAIN (inner_type);
2288
2289   /* Now get the DTYPE value.  */
2290   switch (TREE_CODE (type))
2291     {
2292     case INTEGER_TYPE:
2293     case ENUMERAL_TYPE:
2294       if (TYPE_VAX_FLOATING_POINT_P (type))
2295         switch ((int) TYPE_DIGITS_VALUE (type))
2296           {
2297           case 6:
2298             dtype = 10;
2299             break;
2300           case 9:
2301             dtype = 11;
2302             break;
2303           case 15:
2304             dtype = 27;
2305             break;
2306           }
2307       else
2308         switch (GET_MODE_BITSIZE (TYPE_MODE (type)))
2309           {
2310           case 8:
2311             dtype = TREE_UNSIGNED (type) ? 2 : 6;
2312             break;
2313           case 16:
2314             dtype = TREE_UNSIGNED (type) ? 3 : 7;
2315             break;
2316           case 32:
2317             dtype = TREE_UNSIGNED (type) ? 4 : 8;
2318             break;
2319           case 64:
2320             dtype = TREE_UNSIGNED (type) ? 5 : 9;
2321             break;
2322           case 128:
2323             dtype = TREE_UNSIGNED (type) ? 25 : 26;
2324             break;
2325           }
2326       break;
2327
2328     case REAL_TYPE:
2329       dtype = GET_MODE_BITSIZE (TYPE_MODE (type)) == 32 ? 52 : 53;
2330       break;
2331
2332     case COMPLEX_TYPE:
2333       if (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
2334           && TYPE_VAX_FLOATING_POINT_P (type))
2335         switch ((int) TYPE_DIGITS_VALUE (type))
2336           {
2337           case 6:
2338             dtype = 12;
2339             break;
2340           case 9:
2341             dtype = 13;
2342             break;
2343           case 15:
2344             dtype = 29;
2345           }
2346       else
2347         dtype = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) == 32 ? 54: 55;
2348       break;
2349
2350     case ARRAY_TYPE:
2351       dtype = 14;
2352       break;
2353
2354     default:
2355       break;
2356     }
2357
2358   /* Get the CLASS value.  */
2359   switch (mech)
2360     {
2361     case By_Descriptor_A:
2362       class = 4;
2363       break;
2364     case By_Descriptor_NCA:
2365       class = 10;
2366       break;
2367     case By_Descriptor_SB:
2368       class = 15;
2369       break;
2370     default:
2371       class = 1;
2372     }
2373
2374   /* Make the type for a descriptor for VMS.  The first four fields
2375      are the same for all types.  */
2376
2377   field_list
2378     = chainon (field_list,
2379                make_descriptor_field
2380                ("LENGTH", type_for_size (16, 1), record_type,
2381                 size_in_bytes (mech == By_Descriptor_A ? inner_type : type)));
2382
2383   field_list = chainon (field_list,
2384                         make_descriptor_field ("DTYPE", type_for_size (8, 1),
2385                                                record_type, size_int (dtype)));
2386   field_list = chainon (field_list,
2387                         make_descriptor_field ("CLASS", type_for_size (8, 1),
2388                                                record_type, size_int (class)));
2389
2390   field_list
2391     = chainon (field_list,
2392                make_descriptor_field ("POINTER",
2393                                       build_pointer_type (type),
2394                                       record_type,
2395                                       build1 (ADDR_EXPR,
2396                                               build_pointer_type (type),
2397                                               build (PLACEHOLDER_EXPR,
2398                                                      type))));
2399
2400   switch (mech)
2401     {
2402     case By_Descriptor:
2403     case By_Descriptor_S:
2404       break;
2405
2406     case By_Descriptor_SB:
2407       field_list
2408         = chainon (field_list,
2409                    make_descriptor_field 
2410                    ("SB_L1", type_for_size (32, 1), record_type,
2411                     TREE_CODE (type) == ARRAY_TYPE
2412                     ? TYPE_MIN_VALUE (TYPE_DOMAIN (type)) : size_zero_node));
2413       field_list
2414         = chainon (field_list,
2415                    make_descriptor_field
2416                    ("SB_L2", type_for_size (32, 1), record_type,
2417                     TREE_CODE (type) == ARRAY_TYPE
2418                     ? TYPE_MAX_VALUE (TYPE_DOMAIN (type)) : size_zero_node));
2419       break;
2420
2421     case By_Descriptor_A:
2422     case By_Descriptor_NCA:
2423       field_list = chainon (field_list,
2424                             make_descriptor_field ("SCALE",
2425                                                    type_for_size (8, 1),
2426                                                    record_type,
2427                                                    size_zero_node));
2428
2429       field_list = chainon (field_list,
2430                             make_descriptor_field ("DIGITS",
2431                                                    type_for_size (8, 1),
2432                                                    record_type,
2433                                                    size_zero_node));
2434
2435       field_list
2436         = chainon (field_list,
2437                    make_descriptor_field
2438                    ("AFLAGS", type_for_size (8, 1), record_type,
2439                     size_int (mech == By_Descriptor_NCA
2440                               ? 0
2441                               /* Set FL_COLUMN, FL_COEFF, and FL_BOUNDS.  */
2442                               : (TREE_CODE (type) == ARRAY_TYPE
2443                                  && TYPE_CONVENTION_FORTRAN_P (type)
2444                                  ? 224 : 192))));
2445
2446       field_list = chainon (field_list,
2447                             make_descriptor_field ("DIMCT",
2448                                                    type_for_size (8, 1),
2449                                                    record_type,
2450                                                    size_int (ndim)));
2451
2452       field_list = chainon (field_list,
2453                             make_descriptor_field ("ARSIZE",
2454                                                    type_for_size (32, 1),
2455                                                    record_type,
2456                                                    size_in_bytes (type)));
2457
2458       /* Now build a pointer to the 0,0,0... element.  */
2459       tem = build (PLACEHOLDER_EXPR, type);
2460       for (i = 0, inner_type = type; i < ndim;
2461            i++, inner_type = TREE_TYPE (inner_type))
2462         tem = build (ARRAY_REF, TREE_TYPE (inner_type), tem,
2463                      convert (TYPE_DOMAIN (inner_type), size_zero_node));
2464
2465       field_list
2466         = chainon (field_list,
2467                    make_descriptor_field
2468                    ("A0", build_pointer_type (inner_type), record_type,
2469                     build1 (ADDR_EXPR, build_pointer_type (inner_type), tem)));
2470
2471       /* Next come the addressing coefficients.  */
2472       tem = size_int (1);
2473       for (i = 0; i < ndim; i++)
2474         {
2475           char fname[3];
2476           tree idx_length
2477             = size_binop (MULT_EXPR, tem,
2478                           size_binop (PLUS_EXPR,
2479                                       size_binop (MINUS_EXPR,
2480                                                   TYPE_MAX_VALUE (idx_arr[i]),
2481                                                   TYPE_MIN_VALUE (idx_arr[i])),
2482                                       size_int (1)));
2483
2484           fname[0] = (mech == By_Descriptor_NCA ? 'S' : 'M');
2485           fname[1] = '0' + i, fname[2] = 0;
2486           field_list = chainon (field_list,
2487                                 make_descriptor_field (fname,
2488                                                        type_for_size (32, 1),
2489                                                        record_type,
2490                                                        idx_length));
2491
2492           if (mech == By_Descriptor_NCA)
2493             tem = idx_length;
2494         }
2495
2496       /* Finally here are the bounds.  */
2497       for (i = 0; i < ndim; i++)
2498         {
2499           char fname[3];
2500
2501           fname[0] = 'L', fname[1] = '0' + i, fname[2] = 0;
2502           field_list
2503             = chainon (field_list,
2504                        make_descriptor_field
2505                        (fname, type_for_size (32, 1), record_type,
2506                         TYPE_MIN_VALUE (idx_arr[i])));
2507
2508           fname[0] = 'U';
2509           field_list
2510             = chainon (field_list,
2511                        make_descriptor_field
2512                        (fname, type_for_size (32, 1), record_type,
2513                         TYPE_MAX_VALUE (idx_arr[i])));
2514         }
2515       break;
2516
2517     default:
2518       post_error ("unsupported descriptor type for &", gnat_entity);
2519     }
2520
2521   finish_record_type (record_type, field_list, 0, 1);
2522   pushdecl (build_decl (TYPE_DECL, create_concat_name (gnat_entity, "DESC"),
2523                         record_type));
2524
2525   return record_type;
2526 }
2527
2528 /* Utility routine for above code to make a field.  */
2529
2530 static tree
2531 make_descriptor_field (name, type, rec_type, initial)
2532      const char *name;
2533      tree type;
2534      tree rec_type;
2535      tree initial;
2536 {
2537   tree field
2538     = create_field_decl (get_identifier (name), type, rec_type, 0, 0, 0, 0);
2539
2540   DECL_INITIAL (field) = initial;
2541   return field;
2542 }
2543 \f
2544 /* Build a type to be used to represent an aliased object whose nominal
2545    type is an unconstrained array.  This consists of a RECORD_TYPE containing
2546    a field of TEMPLATE_TYPE and a field of OBJECT_TYPE, which is an
2547    ARRAY_TYPE.  If ARRAY_TYPE is that of the unconstrained array, this
2548    is used to represent an arbitrary unconstrained object.  Use NAME
2549    as the name of the record.  */
2550
2551 tree
2552 build_unc_object_type (template_type, object_type, name)
2553      tree template_type;
2554      tree object_type;
2555      tree name;
2556 {
2557   tree type = make_node (RECORD_TYPE);
2558   tree template_field = create_field_decl (get_identifier ("BOUNDS"),
2559                                            template_type, type, 0, 0, 0, 1);
2560   tree array_field = create_field_decl (get_identifier ("ARRAY"), object_type,
2561                                         type, 0, 0, 0, 1);
2562
2563   TYPE_NAME (type) = name;
2564   TYPE_CONTAINS_TEMPLATE_P (type) = 1;
2565   finish_record_type (type,
2566                       chainon (chainon (NULL_TREE, template_field),
2567                                array_field),
2568                       0, 0);
2569
2570   return type;
2571 }
2572 \f
2573 /* Update anything previously pointing to OLD_TYPE to point to NEW_TYPE.  In
2574    the normal case this is just two adjustments, but we have more to do
2575    if NEW is an UNCONSTRAINED_ARRAY_TYPE.  */
2576
2577 void
2578 update_pointer_to (old_type, new_type)
2579      tree old_type;
2580      tree new_type;
2581 {
2582   tree ptr = TYPE_POINTER_TO (old_type);
2583   tree ref = TYPE_REFERENCE_TO (old_type);
2584
2585   if ((ptr == 0 && ref == 0) || old_type == new_type)
2586     return;
2587
2588   /* First handle the simple case.  */
2589   if (TREE_CODE (new_type) != UNCONSTRAINED_ARRAY_TYPE)
2590     {
2591       if (ptr != 0)
2592         TREE_TYPE (ptr) = new_type;
2593       TYPE_POINTER_TO (new_type) = ptr;
2594
2595       if (ref != 0)
2596         TREE_TYPE (ref) = new_type;
2597       TYPE_REFERENCE_TO (new_type) = ref;
2598
2599       if (ptr != 0 && TYPE_NAME (ptr) != 0
2600           && TREE_CODE (TYPE_NAME (ptr)) == TYPE_DECL
2601           && TREE_CODE (new_type) != ENUMERAL_TYPE)
2602         rest_of_decl_compilation (TYPE_NAME (ptr), NULL,
2603                                   global_bindings_p (), 0);
2604       if (ref != 0 && TYPE_NAME (ref) != 0
2605           && TREE_CODE (TYPE_NAME (ref)) == TYPE_DECL
2606           && TREE_CODE (new_type) != ENUMERAL_TYPE)
2607         rest_of_decl_compilation (TYPE_NAME (ref), NULL,
2608                                   global_bindings_p (), 0);
2609     }
2610
2611   /* Now deal with the unconstrained array case. In this case the "pointer"
2612      is actually a RECORD_TYPE where the types of both fields are
2613      pointers to void.  In that case, copy the field list from the
2614      old type to the new one and update the fields' context. */
2615   else if (TREE_CODE (ptr) != RECORD_TYPE || ! TYPE_IS_FAT_POINTER_P (ptr))
2616     gigi_abort (412);
2617
2618   else
2619     {
2620       tree new_obj_rec = TYPE_OBJECT_RECORD_TYPE (new_type);
2621       tree ptr_temp_type;
2622       tree new_ref;
2623       tree var;
2624
2625       TYPE_FIELDS (ptr) = TYPE_FIELDS (TYPE_POINTER_TO (new_type));
2626       DECL_CONTEXT (TYPE_FIELDS (ptr)) = ptr;
2627       DECL_CONTEXT (TREE_CHAIN (TYPE_FIELDS (ptr))) = ptr;
2628
2629       /* Rework the PLACEHOLDER_EXPR inside the reference to the
2630          template bounds.
2631
2632          ??? This is now the only use of gnat_substitute_in_type, which
2633          is now a very "heavy" routine to do this, so it should be replaced
2634          at some point.  */
2635       ptr_temp_type = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (ptr)));
2636       new_ref = build (COMPONENT_REF, ptr_temp_type,
2637                        build (PLACEHOLDER_EXPR, ptr),
2638                        TREE_CHAIN (TYPE_FIELDS (ptr)));
2639
2640       update_pointer_to 
2641         (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (ptr))),
2642          gnat_substitute_in_type (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (ptr))),
2643                                   TREE_CHAIN (TYPE_FIELDS (ptr)), new_ref));
2644
2645       for (var = TYPE_MAIN_VARIANT (ptr); var; var = TYPE_NEXT_VARIANT (var))
2646         TYPE_UNCONSTRAINED_ARRAY (var) = new_type;
2647
2648       TYPE_POINTER_TO (new_type) = TYPE_REFERENCE_TO (new_type)
2649         = TREE_TYPE (new_type) = ptr;
2650
2651       /* Now handle updating the allocation record, what the thin pointer
2652          points to.  Update all pointers from the old record into the new
2653          one, update the types of the fields, and recompute the size.  */
2654
2655       update_pointer_to (TYPE_OBJECT_RECORD_TYPE (old_type), new_obj_rec);
2656
2657       TREE_TYPE (TYPE_FIELDS (new_obj_rec)) = TREE_TYPE (ptr_temp_type);
2658       TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (new_obj_rec)))
2659         = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (ptr)));
2660       DECL_SIZE (TREE_CHAIN (TYPE_FIELDS (new_obj_rec)))
2661         = TYPE_SIZE (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (ptr))));
2662       DECL_SIZE_UNIT (TREE_CHAIN (TYPE_FIELDS (new_obj_rec)))
2663         = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (ptr))));
2664
2665       TYPE_SIZE (new_obj_rec)
2666         = size_binop (PLUS_EXPR,
2667                       DECL_SIZE (TYPE_FIELDS (new_obj_rec)),
2668                       DECL_SIZE (TREE_CHAIN (TYPE_FIELDS (new_obj_rec))));
2669       TYPE_SIZE_UNIT (new_obj_rec)
2670         = size_binop (PLUS_EXPR,
2671                       DECL_SIZE_UNIT (TYPE_FIELDS (new_obj_rec)),
2672                       DECL_SIZE_UNIT (TREE_CHAIN (TYPE_FIELDS (new_obj_rec))));
2673       rest_of_type_compilation (ptr, global_bindings_p ());
2674     }
2675 }
2676 \f
2677 /* Convert a pointer to a constrained array into a pointer to a fat
2678    pointer.  This involves making or finding a template.  */
2679
2680 static tree
2681 convert_to_fat_pointer (type, expr)
2682      tree type;
2683      tree expr;
2684 {
2685   tree template_type = TREE_TYPE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (type))));
2686   tree template, template_addr;
2687   tree etype = TREE_TYPE (expr);
2688
2689   /* If EXPR is a constant of zero, we make a fat pointer that has a null
2690      pointer to the template and array.  */
2691   if (integer_zerop (expr))
2692     return
2693       build_constructor
2694         (type,
2695          tree_cons (TYPE_FIELDS (type),
2696                     convert (TREE_TYPE (TYPE_FIELDS (type)), expr),
2697                     tree_cons (TREE_CHAIN (TYPE_FIELDS (type)),
2698                                convert (build_pointer_type (template_type),
2699                                         expr),
2700                                NULL_TREE)));
2701
2702   /* If EXPR is a thin pointer, make the template and data from the record.  */
2703
2704   else if (TYPE_THIN_POINTER_P (etype))
2705     {
2706       tree fields = TYPE_FIELDS (TREE_TYPE (etype));
2707
2708       expr = save_expr (expr);
2709       if (TREE_CODE (expr) == ADDR_EXPR)
2710         expr = TREE_OPERAND (expr, 0);
2711       else
2712         expr = build1 (INDIRECT_REF, TREE_TYPE (etype), expr);
2713
2714       template = build_component_ref (expr, NULL_TREE, fields);
2715       expr = build_unary_op (ADDR_EXPR, NULL_TREE,
2716                              build_component_ref (expr, NULL_TREE,
2717                                                   TREE_CHAIN (fields)));
2718     }
2719   else
2720     /* Otherwise, build the constructor for the template.  */
2721     template = build_template (template_type, TREE_TYPE (etype), expr);
2722
2723   template_addr = build_unary_op (ADDR_EXPR, NULL_TREE, template);
2724
2725   /* The result is a CONSTRUCTOR for the fat pointer.  */
2726   return
2727     build_constructor (type,
2728                        tree_cons (TYPE_FIELDS (type), expr,
2729                                   tree_cons (TREE_CHAIN (TYPE_FIELDS (type)),
2730                                              template_addr, NULL_TREE)));
2731 }
2732 \f
2733 /* Convert to a thin pointer type, TYPE.  The only thing we know how to convert
2734    is something that is a fat pointer, so convert to it first if it EXPR
2735    is not already a fat pointer.  */
2736
2737 static tree
2738 convert_to_thin_pointer (type, expr)
2739      tree type;
2740      tree expr;
2741 {
2742   if (! TYPE_FAT_POINTER_P (TREE_TYPE (expr)))
2743     expr
2744       = convert_to_fat_pointer
2745         (TREE_TYPE (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type))), expr);
2746
2747   /* We get the pointer to the data and use a NOP_EXPR to make it the
2748      proper GCC type.  */
2749   expr = build_component_ref (expr, NULL_TREE, TYPE_FIELDS (TREE_TYPE (expr)));
2750   expr = build1 (NOP_EXPR, type, expr);
2751
2752   return expr;
2753 }
2754 \f
2755 /* Create an expression whose value is that of EXPR,
2756    converted to type TYPE.  The TREE_TYPE of the value
2757    is always TYPE.  This function implements all reasonable
2758    conversions; callers should filter out those that are
2759    not permitted by the language being compiled.  */
2760
2761 tree
2762 convert (type, expr)
2763      tree type, expr;
2764 {
2765   enum tree_code code = TREE_CODE (type);
2766   tree etype = TREE_TYPE (expr);
2767   enum tree_code ecode = TREE_CODE (etype);
2768   tree tem;
2769
2770   /* If EXPR is already the right type, we are done.  */
2771   if (type == etype)
2772     return expr;
2773
2774   /* If EXPR is a WITH_RECORD_EXPR, do the conversion inside and then make a
2775      new one.  */
2776   if (TREE_CODE (expr) == WITH_RECORD_EXPR)
2777     return build (WITH_RECORD_EXPR, type,
2778                   convert (type, TREE_OPERAND (expr, 0)),
2779                   TREE_OPERAND (expr, 1));
2780
2781   /* If the input type has padding, remove it by doing a component reference
2782      to the field.  If the output type has padding, make a constructor
2783      to build the record.  If both input and output have padding and are
2784      of variable size, do this as an unchecked conversion.  */
2785   if (ecode == RECORD_TYPE && code == RECORD_TYPE
2786       && TYPE_IS_PADDING_P (type) && TYPE_IS_PADDING_P (etype)
2787       && (! TREE_CONSTANT (TYPE_SIZE (type))
2788           || ! TREE_CONSTANT (TYPE_SIZE (etype))))
2789     ;
2790   else if (ecode == RECORD_TYPE && TYPE_IS_PADDING_P (etype))
2791     {
2792       /* If we have just converted to this padded type, just get
2793          the inner expression.  */
2794       if (TREE_CODE (expr) == CONSTRUCTOR
2795           && CONSTRUCTOR_ELTS (expr) != 0
2796           && TREE_PURPOSE (CONSTRUCTOR_ELTS (expr)) == TYPE_FIELDS (etype))
2797         return TREE_VALUE (CONSTRUCTOR_ELTS (expr));
2798       else
2799         return convert (type, build_component_ref (expr, NULL_TREE,
2800                                                    TYPE_FIELDS (etype)));
2801     }
2802   else if (code == RECORD_TYPE && TYPE_IS_PADDING_P (type))
2803     {
2804       /* If we previously converted from another type and our type is
2805          of variable size, remove the conversion to avoid the need for
2806          variable-size temporaries.  */
2807       if (TREE_CODE (expr) == UNCHECKED_CONVERT_EXPR
2808           && ! TREE_CONSTANT (TYPE_SIZE (type)))
2809         expr = TREE_OPERAND (expr, 0);
2810
2811       /* If we are just removing the padding from expr, convert the original
2812          object if we have variable size.  That will avoid the need
2813          for some variable-size temporaries.  */
2814       if (TREE_CODE (expr) == COMPONENT_REF
2815           && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE
2816           && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (expr, 0)))
2817           && ! TREE_CONSTANT (TYPE_SIZE (type)))
2818         return convert (type, TREE_OPERAND (expr, 0));
2819
2820       /* If the result type is a padded type with a self-referentially-sized
2821          field and the expression type is a record, do this as an
2822          unchecked converstion.  */
2823       else if (TREE_CODE (DECL_SIZE (TYPE_FIELDS (type))) != INTEGER_CST
2824                && contains_placeholder_p (DECL_SIZE (TYPE_FIELDS (type)))
2825                && TREE_CODE (etype) == RECORD_TYPE)
2826         return unchecked_convert (type, expr);
2827
2828       else
2829         return
2830           build_constructor (type,
2831                              tree_cons (TYPE_FIELDS (type),
2832                                         convert (TREE_TYPE
2833                                                  (TYPE_FIELDS (type)),
2834                                                  expr),
2835                                         NULL_TREE));
2836     }
2837
2838   /* If the input is a biased type, adjust first.  */
2839   if (ecode == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (etype))
2840     return convert (type, fold (build (PLUS_EXPR, TREE_TYPE (etype),
2841                                        fold (build1 (GNAT_NOP_EXPR,
2842                                                      TREE_TYPE (etype), expr)),
2843                                        TYPE_MIN_VALUE (etype))));
2844
2845   /* If the input is a left-justified modular type, we need to extract
2846      the actual object before converting it to any other type with the
2847      exception of an unconstrained array.  */
2848   if (ecode == RECORD_TYPE && TYPE_LEFT_JUSTIFIED_MODULAR_P (etype)
2849       && code != UNCONSTRAINED_ARRAY_TYPE)
2850     return convert (type, build_component_ref (expr, NULL_TREE,
2851                                                TYPE_FIELDS (etype)));
2852
2853   /* If converting a type that does not contain a template into one
2854      that does, convert to the data type and then build the template. */
2855   if (code == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (type)
2856       && ! (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype)))
2857     {
2858       tree obj_type = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (type)));
2859
2860       return
2861         build_constructor
2862           (type,
2863            tree_cons (TYPE_FIELDS (type),
2864                       build_template (TREE_TYPE (TYPE_FIELDS (type)),
2865                                       obj_type, NULL_TREE),
2866                       tree_cons (TREE_CHAIN (TYPE_FIELDS (type)),
2867                                  convert (obj_type, expr), NULL_TREE)));
2868     }
2869
2870   /* There are some special cases of expressions that we process
2871      specially.  */
2872   switch (TREE_CODE (expr))
2873     {
2874     case ERROR_MARK:
2875       return expr;
2876
2877     case TRANSFORM_EXPR:
2878     case NULL_EXPR:
2879       /* Just set its type here.  For TRANSFORM_EXPR, we will do the actual
2880          conversion in gnat_expand_expr.  NULL_EXPR does not represent
2881          and actual value, so no conversion is needed.  */
2882       TREE_TYPE (expr) = type;
2883       return expr;
2884
2885     case STRING_CST:
2886     case CONSTRUCTOR:
2887       /* If we are converting a STRING_CST to another constrained array type,
2888          just make a new one in the proper type.  Likewise for a
2889          CONSTRUCTOR.  But if the mode of the type is different, we must
2890          ensure a new RTL is made for the constant.  */
2891       if (code == ecode && AGGREGATE_TYPE_P (etype)
2892           && ! (TREE_CODE (TYPE_SIZE (etype)) == INTEGER_CST
2893                 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
2894         {
2895           expr = copy_node (expr);
2896           TREE_TYPE (expr) = type;
2897
2898           if (TYPE_MODE (type) != TYPE_MODE (etype))
2899             TREE_CST_RTL (expr) = 0;
2900
2901           return expr;
2902         }
2903       break;
2904
2905     case COMPONENT_REF:
2906       /* If we are converting between two aggregate types of the same
2907          kind, size, mode, and alignment, just make a new COMPONENT_REF.
2908          This avoid unneeded conversions which makes reference computations
2909          more complex.  */
2910       if (code == ecode && TYPE_MODE (type) == TYPE_MODE (etype)
2911           && AGGREGATE_TYPE_P (type) && AGGREGATE_TYPE_P (etype)
2912           && TYPE_ALIGN (type) == TYPE_ALIGN (etype)
2913           && operand_equal_p (TYPE_SIZE (type), TYPE_SIZE (etype), 0))
2914         return build (COMPONENT_REF, type, TREE_OPERAND (expr, 0),
2915                       TREE_OPERAND (expr, 1));
2916
2917       break;
2918
2919     case UNCONSTRAINED_ARRAY_REF:
2920       /* Convert this to the type of the inner array by getting the address of
2921          the array from the template.  */
2922       expr = build_unary_op (INDIRECT_REF, NULL_TREE,
2923                              build_component_ref (TREE_OPERAND (expr, 0),
2924                                                   get_identifier ("P_ARRAY"),
2925                                                   NULL_TREE));
2926       etype = TREE_TYPE (expr);
2927       ecode = TREE_CODE (etype);
2928       break;
2929
2930     case UNCHECKED_CONVERT_EXPR:
2931       if (AGGREGATE_TYPE_P (type) && AGGREGATE_TYPE_P (etype)
2932           && ! TYPE_FAT_POINTER_P (type) && ! TYPE_FAT_POINTER_P (etype))
2933         return convert (type, TREE_OPERAND (expr, 0));
2934       break;
2935
2936     case INDIRECT_REF:
2937       /* If both types are record types, just convert the pointer and
2938          make a new INDIRECT_REF. 
2939
2940          ??? Disable this for now since it causes problems with the
2941          code in build_binary_op for MODIFY_EXPR which wants to
2942          strip off conversions.  But that code really is a mess and
2943          we need to do this a much better way some time.  */
2944       if (0
2945           && (TREE_CODE (type) == RECORD_TYPE
2946               || TREE_CODE (type) == UNION_TYPE)
2947           && (TREE_CODE (etype) == RECORD_TYPE
2948               || TREE_CODE (etype) == UNION_TYPE)
2949           && ! TYPE_FAT_POINTER_P (type) && ! TYPE_FAT_POINTER_P (etype))
2950         return build_unary_op (INDIRECT_REF, NULL_TREE,
2951                                convert (build_pointer_type (type),
2952                                         TREE_OPERAND (expr, 0)));
2953       break;
2954
2955     default:
2956       break;
2957     }
2958
2959   /* Check for converting to a pointer to an unconstrained array.  */
2960   if (TYPE_FAT_POINTER_P (type) && ! TYPE_FAT_POINTER_P (etype))
2961     return convert_to_fat_pointer (type, expr);
2962
2963   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (etype)
2964       || (code == INTEGER_CST && ecode == INTEGER_CST
2965           && (type == TREE_TYPE (etype) || etype == TREE_TYPE (type))))
2966     return fold (build1 (NOP_EXPR, type, expr));
2967
2968   switch (code)
2969     {
2970     case VOID_TYPE:
2971       return build1 (CONVERT_EXPR, type, expr);
2972
2973     case INTEGER_TYPE:
2974       if (TYPE_HAS_ACTUAL_BOUNDS_P (type)
2975           && (ecode == ARRAY_TYPE || ecode == UNCONSTRAINED_ARRAY_TYPE))
2976         return unchecked_convert (type, expr);
2977       else if (TYPE_BIASED_REPRESENTATION_P (type))
2978         return fold (build1 (CONVERT_EXPR, type,
2979                              fold (build (MINUS_EXPR, TREE_TYPE (type),
2980                                           convert (TREE_TYPE (type), expr),
2981                                           TYPE_MIN_VALUE (type)))));
2982
2983       /* ... fall through ... */
2984
2985     case ENUMERAL_TYPE:
2986       return fold (convert_to_integer (type, expr));
2987
2988     case POINTER_TYPE:
2989     case REFERENCE_TYPE:
2990       /* If converting between two pointers to records denoting
2991          both a template and type, adjust if needed to account
2992          for any differing offsets, since one might be negative.  */
2993       if (TYPE_THIN_POINTER_P (etype) && TYPE_THIN_POINTER_P (type))
2994         {
2995           tree bit_diff
2996             = size_diffop (bit_position (TYPE_FIELDS (TREE_TYPE (etype))),
2997                            bit_position (TYPE_FIELDS (TREE_TYPE (type))));
2998           tree byte_diff = size_binop (CEIL_DIV_EXPR, bit_diff,
2999                                        sbitsize_int (BITS_PER_UNIT));
3000
3001           expr = build1 (NOP_EXPR, type, expr);
3002           TREE_CONSTANT (expr) = TREE_CONSTANT (TREE_OPERAND (expr, 0));
3003           if (integer_zerop (byte_diff))
3004             return expr;
3005
3006           return build_binary_op (PLUS_EXPR, type, expr,
3007                                   fold (convert_to_pointer (type, byte_diff)));
3008         }
3009
3010       /* If converting to a thin pointer, handle specially.  */
3011       if (TYPE_THIN_POINTER_P (type)
3012           && TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)) != 0)
3013         return convert_to_thin_pointer (type, expr);
3014
3015       /* If converting fat pointer to normal pointer, get the pointer to the
3016          array and then convert it.  */
3017       else if (TYPE_FAT_POINTER_P (etype))
3018         expr = build_component_ref (expr, get_identifier ("P_ARRAY"),
3019                                     NULL_TREE);
3020
3021       return fold (convert_to_pointer (type, expr));
3022
3023     case REAL_TYPE:
3024       return fold (convert_to_real (type, expr));
3025
3026     case RECORD_TYPE:
3027       if (TYPE_LEFT_JUSTIFIED_MODULAR_P (type) && ! AGGREGATE_TYPE_P (etype))
3028         return
3029           build_constructor
3030             (type, tree_cons (TYPE_FIELDS (type),
3031                               convert (TREE_TYPE (TYPE_FIELDS (type)), expr),
3032                               NULL_TREE));
3033
3034       /* ... fall through ... */
3035
3036     case ARRAY_TYPE:
3037       /* In these cases, assume the front-end has validated the conversion.
3038          If the conversion is valid, it will be a bit-wise conversion, so
3039          it can be viewed as an unchecked conversion.  */
3040       return unchecked_convert (type, expr);
3041
3042     case UNION_TYPE:
3043       /* Just validate that the type is indeed that of a field
3044          of the type.  Then make the simple conversion.  */
3045       for (tem = TYPE_FIELDS (type); tem; tem = TREE_CHAIN (tem))
3046         if (TREE_TYPE (tem) == etype)
3047           return build1 (CONVERT_EXPR, type, expr);
3048
3049       gigi_abort (413);
3050
3051     case UNCONSTRAINED_ARRAY_TYPE:
3052       /* If EXPR is a constrained array, take its address, convert it to a
3053          fat pointer, and then dereference it.  Likewise if EXPR is a
3054          record containing both a template and a constrained array.
3055          Note that a record representing a left justified modular type
3056          always represents a packed constrained array.  */
3057       if (ecode == ARRAY_TYPE
3058           || (ecode == INTEGER_TYPE && TYPE_HAS_ACTUAL_BOUNDS_P (etype))
3059           || (ecode == RECORD_TYPE && TYPE_CONTAINS_TEMPLATE_P (etype))
3060           || (ecode == RECORD_TYPE && TYPE_LEFT_JUSTIFIED_MODULAR_P (etype)))
3061         return
3062           build_unary_op
3063             (INDIRECT_REF, NULL_TREE,
3064              convert_to_fat_pointer (TREE_TYPE (type),
3065                                      build_unary_op (ADDR_EXPR,
3066                                                      NULL_TREE, expr)));
3067
3068       /* Do something very similar for converting one unconstrained
3069          array to another.  */
3070       else if (ecode == UNCONSTRAINED_ARRAY_TYPE)
3071         return
3072           build_unary_op (INDIRECT_REF, NULL_TREE,
3073                           convert (TREE_TYPE (type),
3074                                    build_unary_op (ADDR_EXPR,
3075                                                    NULL_TREE, expr)));
3076       else
3077         gigi_abort (409);
3078
3079     case COMPLEX_TYPE:
3080       return fold (convert_to_complex (type, expr));
3081
3082     default:
3083       gigi_abort (410);
3084     }
3085 }
3086 \f
3087 /* Remove all conversions that are done in EXP.  This includes converting
3088    from a padded type or converting to a left-justified modular type.  */
3089
3090 tree
3091 remove_conversions (exp)
3092      tree exp;
3093 {
3094   switch (TREE_CODE (exp))
3095     {
3096     case CONSTRUCTOR:
3097       if (TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
3098           && TYPE_LEFT_JUSTIFIED_MODULAR_P (TREE_TYPE (exp)))
3099         return remove_conversions (TREE_VALUE (CONSTRUCTOR_ELTS (exp)));
3100       break;
3101
3102     case COMPONENT_REF:
3103       if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == RECORD_TYPE
3104           && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
3105         return remove_conversions (TREE_OPERAND (exp, 0));
3106       break;
3107
3108     case UNCHECKED_CONVERT_EXPR:
3109     case NOP_EXPR:  case CONVERT_EXPR:
3110       return remove_conversions (TREE_OPERAND (exp, 0));
3111
3112     default:
3113       break;
3114     }
3115
3116   return exp;
3117 }
3118 \f
3119 /* If EXP's type is an UNCONSTRAINED_ARRAY_TYPE, return an expression that
3120    refers to the underlying array.  If its type has TYPE_CONTAINS_TEMPLATE_P,
3121    likewise return an expression pointing to the underlying array.  */
3122
3123 tree
3124 maybe_unconstrained_array (exp)
3125      tree exp;
3126 {
3127   enum tree_code code = TREE_CODE (exp);
3128   tree new;
3129
3130   switch (TREE_CODE (TREE_TYPE (exp)))
3131     {
3132     case UNCONSTRAINED_ARRAY_TYPE:
3133       if (code == UNCONSTRAINED_ARRAY_REF)
3134         {
3135           new
3136             = build_unary_op (INDIRECT_REF, NULL_TREE,
3137                               build_component_ref (TREE_OPERAND (exp, 0),
3138                                                    get_identifier ("P_ARRAY"),
3139                                                    NULL_TREE));
3140           TREE_READONLY (new) = TREE_STATIC (new) = TREE_READONLY (exp);
3141           return new;
3142         }
3143
3144       else if (code == NULL_EXPR)
3145         return build1 (NULL_EXPR,
3146                        TREE_TYPE (TREE_TYPE (TYPE_FIELDS
3147                                              (TREE_TYPE (TREE_TYPE (exp))))),
3148                        TREE_OPERAND (exp, 0));
3149
3150       else if (code == WITH_RECORD_EXPR
3151                && (TREE_OPERAND (exp, 0)
3152                    != (new = maybe_unconstrained_array
3153                        (TREE_OPERAND (exp, 0)))))
3154         return build (WITH_RECORD_EXPR, TREE_TYPE (new), new,
3155                       TREE_OPERAND (exp, 1));
3156
3157     case RECORD_TYPE:
3158       if (TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (exp)))
3159         {
3160           new
3161             = build_component_ref (exp, NULL_TREE,
3162                                    TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (exp))));
3163           if (TREE_CODE (TREE_TYPE (new)) == RECORD_TYPE
3164               && TYPE_IS_PADDING_P (TREE_TYPE (new)))
3165             new = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (new))), new);
3166
3167           return new;
3168         }
3169       break;
3170
3171     default:
3172       break;
3173     }
3174
3175   return exp;
3176 }
3177 \f
3178 /* Return an expression that does an unchecked converstion of EXPR to TYPE.  */
3179
3180 tree
3181 unchecked_convert (type, expr)
3182      tree type;
3183      tree expr;
3184 {
3185   tree etype = TREE_TYPE (expr);
3186
3187   /* If the expression is already the right type, we are done.  */
3188   if (etype == type)
3189     return expr;
3190
3191   /* If EXPR is a WITH_RECORD_EXPR, do the conversion inside and then make a
3192      new one.  */
3193   if (TREE_CODE (expr) == WITH_RECORD_EXPR)
3194     return build (WITH_RECORD_EXPR, type,
3195                   unchecked_convert (type, TREE_OPERAND (expr, 0)),
3196                   TREE_OPERAND (expr, 1));
3197
3198   /* If both types types are integral just do a normal conversion.
3199      Likewise for a conversion to an unconstrained array.  */
3200   if ((((INTEGRAL_TYPE_P (type)
3201          && ! (TREE_CODE (type) == INTEGER_TYPE
3202                && TYPE_VAX_FLOATING_POINT_P (type)))
3203         || (POINTER_TYPE_P (type) && ! TYPE_THIN_POINTER_P (type))
3204         || (TREE_CODE (type) == RECORD_TYPE
3205             && TYPE_LEFT_JUSTIFIED_MODULAR_P (type)))
3206        && ((INTEGRAL_TYPE_P (etype)
3207             && ! (TREE_CODE (etype) == INTEGER_TYPE
3208                   && TYPE_VAX_FLOATING_POINT_P (etype)))
3209            || (POINTER_TYPE_P (etype) && ! TYPE_THIN_POINTER_P (etype))
3210            || (TREE_CODE (etype) == RECORD_TYPE
3211                && TYPE_LEFT_JUSTIFIED_MODULAR_P (etype))))
3212       || TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
3213     {
3214       tree rtype = type;
3215
3216       if (TREE_CODE (etype) == INTEGER_TYPE
3217           && TYPE_BIASED_REPRESENTATION_P (etype))
3218         {
3219           tree ntype = copy_type (etype);
3220
3221           TYPE_BIASED_REPRESENTATION_P (ntype) = 0;
3222           TYPE_MAIN_VARIANT (ntype) = ntype;
3223           expr = build1 (GNAT_NOP_EXPR, ntype, expr);
3224         }
3225
3226       if (TREE_CODE (type) == INTEGER_TYPE
3227           && TYPE_BIASED_REPRESENTATION_P (type))
3228         {
3229           rtype = copy_type (type);
3230           TYPE_BIASED_REPRESENTATION_P (rtype) = 0;
3231           TYPE_MAIN_VARIANT (rtype) = rtype;
3232         }
3233
3234       expr = convert (rtype, expr);
3235       if (type != rtype)
3236         expr = build1 (GNAT_NOP_EXPR, type, expr);
3237     }
3238
3239   /* If we are converting TO an integral type whose precision is not the
3240      same as its size, first unchecked convert to a record that contains
3241      an object of the output type.  Then extract the field. */
3242   else if (INTEGRAL_TYPE_P (type) && TYPE_RM_SIZE (type) != 0
3243            && 0 != compare_tree_int (TYPE_RM_SIZE (type),
3244                                      GET_MODE_BITSIZE (TYPE_MODE (type))))
3245     {
3246       tree rec_type = make_node (RECORD_TYPE);
3247       tree field = create_field_decl (get_identifier ("OBJ"), type, 
3248                                       rec_type, 1, 0, 0, 0);
3249
3250       TYPE_FIELDS (rec_type) = field;
3251       layout_type (rec_type);
3252
3253       expr = unchecked_convert (rec_type, expr);
3254       expr = build_component_ref (expr, NULL_TREE, field);
3255     }
3256
3257   /* Similarly for integral input type whose precision is not equal to its
3258      size.  */
3259   else if (INTEGRAL_TYPE_P (etype) && TYPE_RM_SIZE (etype) != 0
3260       && 0 != compare_tree_int (TYPE_RM_SIZE (etype),
3261                                 GET_MODE_BITSIZE (TYPE_MODE (etype))))
3262     {
3263       tree rec_type = make_node (RECORD_TYPE);
3264       tree field
3265         = create_field_decl (get_identifier ("OBJ"), etype, rec_type,
3266                              1, 0, 0, 0);
3267
3268       TYPE_FIELDS (rec_type) = field;
3269       layout_type (rec_type);
3270
3271       expr = build_constructor (rec_type, build_tree_list (field, expr));
3272       expr = unchecked_convert (type, expr);
3273     }
3274
3275   /* We have a special case when we are converting between two
3276      unconstrained array types.  In that case, take the address,
3277      convert the fat pointer types, and dereference.  */
3278   else if (TREE_CODE (etype) == UNCONSTRAINED_ARRAY_TYPE
3279            && TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
3280     expr = build_unary_op (INDIRECT_REF, NULL_TREE,
3281                            build1 (UNCHECKED_CONVERT_EXPR, TREE_TYPE (type),
3282                                    build_unary_op (ADDR_EXPR, NULL_TREE,
3283                                                    expr)));
3284
3285   /* If both types are aggregates with the same mode and alignment (except
3286      if the result is a UNION_TYPE), we can do this as a normal conversion.  */
3287   else if (AGGREGATE_TYPE_P (type) && AGGREGATE_TYPE_P (etype)
3288            && TREE_CODE (type) != UNION_TYPE
3289            && TYPE_ALIGN (type) == TYPE_ALIGN (etype)
3290            && TYPE_MODE (type) == TYPE_MODE (etype))
3291     expr = build1 (CONVERT_EXPR, type, expr);
3292
3293   else
3294     {
3295       expr = maybe_unconstrained_array (expr);
3296       etype = TREE_TYPE (expr);
3297       expr = build1 (UNCHECKED_CONVERT_EXPR, type, expr);
3298     }
3299
3300
3301   /* If the result is an integral type whose size is not equal to
3302      the size of the underlying machine type, sign- or zero-extend
3303      the result.  We need not do this in the case where the input is
3304      an integral type of the same precision and signedness or if the output
3305      is a biased type or if both the input and output are unsigned.  */
3306   if (INTEGRAL_TYPE_P (type) && TYPE_RM_SIZE (type) != 0
3307       && ! (TREE_CODE (type) == INTEGER_TYPE
3308             && TYPE_BIASED_REPRESENTATION_P (type))
3309       && 0 != compare_tree_int (TYPE_RM_SIZE (type),
3310                                 GET_MODE_BITSIZE (TYPE_MODE (type)))
3311       && ! (INTEGRAL_TYPE_P (etype)
3312             && TREE_UNSIGNED (type) == TREE_UNSIGNED (etype)
3313             && operand_equal_p (TYPE_RM_SIZE (type),
3314                                 (TYPE_RM_SIZE (etype) != 0
3315                                  ? TYPE_RM_SIZE (etype) : TYPE_SIZE (etype)),
3316                                 0))
3317       && ! (TREE_UNSIGNED (type) && TREE_UNSIGNED (etype)))
3318     {
3319       tree base_type = type_for_mode (TYPE_MODE (type), TREE_UNSIGNED (type));
3320       tree shift_expr
3321         = convert (base_type,
3322                    size_binop (MINUS_EXPR,
3323                                bitsize_int
3324                                (GET_MODE_BITSIZE (TYPE_MODE (type))),
3325                                TYPE_RM_SIZE (type)));
3326       expr
3327         = convert (type,
3328                    build_binary_op (RSHIFT_EXPR, base_type,
3329                                     build_binary_op (LSHIFT_EXPR, base_type,
3330                                                      convert (base_type, expr),
3331                                                      shift_expr),
3332                                     shift_expr));
3333     }
3334
3335   /* An unchecked conversion should never raise Constraint_Error.  The code
3336      below assumes that GCC's conversion routines overflow the same
3337      way that the underlying hardware does.  This is probably true.  In
3338      the rare case when it isn't, we can rely on the fact that such
3339      conversions are erroneous anyway.  */
3340   if (TREE_CODE (expr) == INTEGER_CST)
3341     TREE_OVERFLOW (expr) = TREE_CONSTANT_OVERFLOW (expr) = 0;
3342
3343   /* If the sizes of the types differ and this is an UNCHECKED_CONVERT_EXPR,
3344      show no longer constant.  */
3345   if (TREE_CODE (expr) == UNCHECKED_CONVERT_EXPR
3346       && ! operand_equal_p (TYPE_SIZE_UNIT (type), TYPE_SIZE_UNIT (etype), 1))
3347     TREE_CONSTANT (expr) = 0;
3348
3349   return expr;
3350 }