3b62bb1a844d8f38b72a64dc6bb8d03595e5923a
[platform/upstream/gcc.git] / gcc / ada / gcc-interface / misc.c
1 /****************************************************************************
2  *                                                                          *
3  *                         GNAT COMPILER COMPONENTS                         *
4  *                                                                          *
5  *                                 M I S C                                  *
6  *                                                                          *
7  *                           C Implementation File                          *
8  *                                                                          *
9  *          Copyright (C) 1992-2017, Free Software Foundation, Inc.         *
10  *                                                                          *
11  * GNAT is free software;  you can  redistribute it  and/or modify it under *
12  * terms of the  GNU General Public License as published  by the Free Soft- *
13  * ware  Foundation;  either version 3,  or (at your option) any later ver- *
14  * sion.  GNAT is distributed in the hope that it will be useful, but WITH- *
15  * OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY *
16  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License *
17  * for  more details.  You should have  received  a copy of the GNU General *
18  * Public License  distributed  with GNAT;  see file  COPYING3.  If not see *
19  * <http://www.gnu.org/licenses/>.                                          *
20  *                                                                          *
21  * GNAT was originally developed  by the GNAT team at  New York University. *
22  * Extensive contributions were provided by Ada Core Technologies Inc.      *
23  *                                                                          *
24  ****************************************************************************/
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "tree.h"
31 #include "diagnostic.h"
32 #include "opts.h"
33 #include "alias.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "print-tree.h"
37 #include "toplev.h"
38 #include "langhooks.h"
39 #include "langhooks-def.h"
40 #include "plugin.h"
41 #include "calls.h"      /* For pass_by_reference.  */
42 #include "dwarf2out.h"
43
44 #include "ada.h"
45 #include "adadecode.h"
46 #include "types.h"
47 #include "atree.h"
48 #include "namet.h"
49 #include "nlists.h"
50 #include "uintp.h"
51 #include "fe.h"
52 #include "sinfo.h"
53 #include "einfo.h"
54 #include "ada-tree.h"
55 #include "gigi.h"
56
57 /* This symbol needs to be defined for the front-end.  */
58 void *callgraph_info_file = NULL;
59
60 /* Command-line argc and argv.  These variables are global since they are
61    imported in back_end.adb.  */
62 unsigned int save_argc;
63 const char **save_argv;
64
65 /* GNAT argc and argv generated by the binder for all Ada programs.  */
66 extern int gnat_argc;
67 extern const char **gnat_argv;
68
69 /* Ada code requires variables for these settings rather than elements
70    of the global_options structure because they are imported.  */
71 #undef gnat_encodings
72 enum dwarf_gnat_encodings gnat_encodings = DWARF_GNAT_ENCODINGS_DEFAULT;
73
74 #undef optimize
75 int optimize;
76
77 #undef optimize_size
78 int optimize_size;
79
80 #undef flag_compare_debug
81 int flag_compare_debug;
82
83 #undef flag_short_enums
84 int flag_short_enums;
85
86 #undef flag_stack_check
87 enum stack_check_type flag_stack_check = NO_STACK_CHECK;
88
89 #ifdef __cplusplus
90 extern "C" {
91 #endif
92
93 /* Declare functions we use as part of startup.  */
94 extern void __gnat_initialize (void *);
95 extern void __gnat_install_SEH_handler (void *);
96 extern void adainit (void);
97 extern void _ada_gnat1drv (void);
98
99 #ifdef __cplusplus
100 }
101 #endif
102
103 /* The parser for the language.  For us, we process the GNAT tree.  */
104
105 static void
106 gnat_parse_file (void)
107 {
108   int seh[2];
109
110   /* Call the target specific initializations.  */
111   __gnat_initialize (NULL);
112
113   /* ??? Call the SEH initialization routine.  This is to workaround
114   a bootstrap path problem.  The call below should be removed at some
115   point and the SEH pointer passed to __gnat_initialize above.  */
116   __gnat_install_SEH_handler ((void *)seh);
117
118   /* Call the front-end elaboration procedures.  */
119   adainit ();
120
121   /* Call the front end.  */
122   _ada_gnat1drv ();
123
124   /* Write the global declarations.  */
125   gnat_write_global_declarations ();
126 }
127
128 /* Return language mask for option processing.  */
129
130 static unsigned int
131 gnat_option_lang_mask (void)
132 {
133   return CL_Ada;
134 }
135
136 /* Decode all the language specific options that cannot be decoded by GCC.
137    The option decoding phase of GCC calls this routine on the flags that
138    are marked as Ada-specific.  Return true on success or false on failure.  */
139
140 static bool
141 gnat_handle_option (size_t scode, const char *arg, int value, int kind,
142                     location_t loc, const struct cl_option_handlers *handlers)
143 {
144   enum opt_code code = (enum opt_code) scode;
145
146   switch (code)
147     {
148     case OPT_Wall:
149       handle_generated_option (&global_options, &global_options_set,
150                                OPT_Wunused, NULL, value,
151                                gnat_option_lang_mask (), kind, loc,
152                                handlers, true, global_dc);
153       warn_uninitialized = value;
154       warn_maybe_uninitialized = value;
155       break;
156
157     case OPT_gant:
158       warning (0, "%<-gnat%> misspelled as %<-gant%>");
159
160       /* ... fall through ... */
161
162     case OPT_gnat:
163     case OPT_gnatO:
164     case OPT_fRTS_:
165     case OPT_I:
166     case OPT_nostdinc:
167     case OPT_nostdlib:
168       /* These are handled by the front-end.  */
169       break;
170
171     case OPT_fshort_enums:
172     case OPT_fsigned_char:
173       /* These are handled by the middle-end.  */
174       break;
175
176     case OPT_fbuiltin_printf:
177       /* This is ignored in Ada but needs to be accepted so it can be
178          defaulted.  */
179       break;
180
181     default:
182       gcc_unreachable ();
183     }
184
185   Ada_handle_option_auto (&global_options, &global_options_set,
186                           scode, arg, value,
187                           gnat_option_lang_mask (), kind, loc,
188                           handlers, global_dc);
189   return true;
190 }
191
192 /* Initialize options structure OPTS.  */
193
194 static void
195 gnat_init_options_struct (struct gcc_options *opts)
196 {
197   /* Uninitialized really means uninitialized in Ada.  */
198   opts->x_flag_zero_initialized_in_bss = 0;
199
200   /* We don't care about errno in Ada and it causes __builtin_sqrt to
201      call the libm function rather than do it inline.  */
202   opts->x_flag_errno_math = 0;
203   opts->frontend_set_flag_errno_math = true;
204 }
205
206 /* Initialize for option processing.  */
207
208 static void
209 gnat_init_options (unsigned int decoded_options_count,
210                    struct cl_decoded_option *decoded_options)
211 {
212   /* Reconstruct an argv array for use of back_end.adb.
213
214      ??? back_end.adb should not rely on this; instead, it should work with
215      decoded options without such reparsing, to ensure consistency in how
216      options are decoded.  */
217   save_argv = XNEWVEC (const char *, 2 * decoded_options_count + 1);
218   save_argc = 0;
219   for (unsigned int i = 0; i < decoded_options_count; i++)
220     {
221       size_t num_elements = decoded_options[i].canonical_option_num_elements;
222
223       if (decoded_options[i].errors
224           || decoded_options[i].opt_index == OPT_SPECIAL_unknown
225           || num_elements == 0)
226         continue;
227
228       /* Deal with -I- specially since it must be a single switch.  */
229       if (decoded_options[i].opt_index == OPT_I
230           && num_elements == 2
231           && decoded_options[i].canonical_option[1][0] == '-'
232           && decoded_options[i].canonical_option[1][1] == '\0')
233         save_argv[save_argc++] = "-I-";
234       else
235         {
236           gcc_assert (num_elements >= 1 && num_elements <= 2);
237           save_argv[save_argc++] = decoded_options[i].canonical_option[0];
238           if (num_elements >= 2)
239             save_argv[save_argc++] = decoded_options[i].canonical_option[1];
240         }
241     }
242   save_argv[save_argc] = NULL;
243
244   /* Pass just the name of the command through the regular channel.  */
245   gnat_argv = (const char **) xmalloc (sizeof (char *));
246   gnat_argv[0] = xstrdup (save_argv[0]);
247   gnat_argc = 1;
248 }
249
250 /* Settings adjustments after switches processing by the back-end.
251    Note that the front-end switches processing (Scan_Compiler_Arguments)
252    has not been done yet at this point!  */
253
254 static bool
255 gnat_post_options (const char **pfilename ATTRIBUTE_UNUSED)
256 {
257   /* Excess precision other than "fast" requires front-end support.  */
258   if (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD)
259     sorry ("-fexcess-precision=standard for Ada");
260   flag_excess_precision_cmdline = EXCESS_PRECISION_FAST;
261
262   /* No psABI change warnings for Ada.  */
263   warn_psabi = 0;
264
265   /* No return type warnings for Ada.  */
266   warn_return_type = 0;
267
268   /* No caret by default for Ada.  */
269   if (!global_options_set.x_flag_diagnostics_show_caret)
270     global_dc->show_caret = false;
271
272   /* Warn only if STABS is not the default: we don't want to emit a warning if
273      the user did not use a -gstabs option.  */
274   if (PREFERRED_DEBUGGING_TYPE != DBX_DEBUG && write_symbols == DBX_DEBUG)
275     warning (0, "STABS debugging information for Ada is obsolete and not "
276                 "supported anymore");
277
278   /* Copy global settings to local versions.  */
279   gnat_encodings = global_options.x_gnat_encodings;
280   optimize = global_options.x_optimize;
281   optimize_size = global_options.x_optimize_size;
282   flag_compare_debug = global_options.x_flag_compare_debug;
283   flag_stack_check = global_options.x_flag_stack_check;
284   flag_short_enums = global_options.x_flag_short_enums;
285
286   /* Unfortunately the post_options hook is called before the value of
287      flag_short_enums is autodetected, if need be.  Mimic the process
288      for our private flag_short_enums.  */
289   if (flag_short_enums == 2)
290     flag_short_enums = targetm.default_short_enums ();
291
292   return false;
293 }
294
295 /* Here is the function to handle the compiler error processing in GCC.  */
296
297 static void
298 internal_error_function (diagnostic_context *context, const char *msgid,
299                          va_list *ap)
300 {
301   text_info tinfo;
302   char *buffer, *p, *loc;
303   String_Template temp, temp_loc;
304   String_Pointer sp, sp_loc;
305   expanded_location xloc;
306
307   /* Warn if plugins present.  */
308   warn_if_plugins ();
309
310   /* Reset the pretty-printer.  */
311   pp_clear_output_area (context->printer);
312
313   /* Format the message into the pretty-printer.  */
314   tinfo.format_spec = msgid;
315   tinfo.args_ptr = ap;
316   tinfo.err_no = errno;
317   pp_format_verbatim (context->printer, &tinfo);
318
319   /* Extract a (writable) pointer to the formatted text.  */
320   buffer = xstrdup (pp_formatted_text (context->printer));
321
322   /* Go up to the first newline.  */
323   for (p = buffer; *p; p++)
324     if (*p == '\n')
325       {
326         *p = '\0';
327         break;
328       }
329
330   temp.Low_Bound = 1;
331   temp.High_Bound = p - buffer;
332   sp.Bounds = &temp;
333   sp.Array = buffer;
334
335   xloc = expand_location (input_location);
336   if (context->show_column && xloc.column != 0)
337     loc = xasprintf ("%s:%d:%d", xloc.file, xloc.line, xloc.column);
338   else
339     loc = xasprintf ("%s:%d", xloc.file, xloc.line);
340   temp_loc.Low_Bound = 1;
341   temp_loc.High_Bound = strlen (loc);
342   sp_loc.Bounds = &temp_loc;
343   sp_loc.Array = loc;
344
345   Current_Error_Node = error_gnat_node;
346   Compiler_Abort (sp, sp_loc, true);
347 }
348
349 /* Perform all the initialization steps that are language-specific.  */
350
351 static bool
352 gnat_init (void)
353 {
354   /* Do little here, most of the standard declarations are set up after the
355      front-end has been run.  Use the same `char' as C for Interfaces.C.  */
356   build_common_tree_nodes (flag_signed_char);
357
358   /* In Ada, we use an unsigned 8-bit type for the default boolean type.  */
359   boolean_type_node = make_unsigned_type (8);
360   TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
361   SET_TYPE_RM_MAX_VALUE (boolean_type_node,
362                          build_int_cst (boolean_type_node, 1));
363   SET_TYPE_RM_SIZE (boolean_type_node, bitsize_int (1));
364   boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
365   boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
366
367   sbitsize_one_node = sbitsize_int (1);
368   sbitsize_unit_node = sbitsize_int (BITS_PER_UNIT);
369
370   /* Register our internal error function.  */
371   global_dc->internal_error = &internal_error_function;
372
373   return true;
374 }
375
376 /* Initialize the GCC support for exception handling.  */
377
378 void
379 gnat_init_gcc_eh (void)
380 {
381   /* We shouldn't do anything if the No_Exceptions_Handler pragma is set,
382      though. This could for instance lead to the emission of tables with
383      references to symbols (such as the Ada eh personality routine) within
384      libraries we won't link against.  */
385   if (No_Exception_Handlers_Set ())
386     return;
387
388   /* Tell GCC we are handling cleanup actions through exception propagation.
389      This opens possibilities that we don't take advantage of yet, but is
390      nonetheless necessary to ensure that fixup code gets assigned to the
391      right exception regions.  */
392   using_eh_for_cleanups ();
393
394   /* Turn on -fexceptions, -fnon-call-exceptions and -fdelete-dead-exceptions.
395      The first one triggers the generation of the necessary exception tables.
396      The second one is useful for two reasons: 1/ we map some asynchronous
397      signals like SEGV to exceptions, so we need to ensure that the insns
398      which can lead to such signals are correctly attached to the exception
399      region they pertain to, 2/ some calls to pure subprograms are handled as
400      libcall blocks and then marked as "cannot trap" if the flag is not set
401      (see emit_libcall_block).  We should not let this be since it is possible
402      for such calls to actually raise in Ada.
403      The third one is an optimization that makes it possible to delete dead
404      instructions that may throw exceptions, most notably loads and stores,
405      as permitted in Ada.  */
406   flag_exceptions = 1;
407   flag_non_call_exceptions = 1;
408   flag_delete_dead_exceptions = 1;
409
410   init_eh ();
411 }
412
413 /* Initialize the GCC support for floating-point operations.  */
414
415 void
416 gnat_init_gcc_fp (void)
417 {
418   /* Disable FP optimizations that ignore the signedness of zero if
419      S'Signed_Zeros is true, but don't override the user if not.  */
420   if (Signed_Zeros_On_Target)
421     flag_signed_zeros = 1;
422   else if (!global_options_set.x_flag_signed_zeros)
423     flag_signed_zeros = 0;
424
425   /* Assume that FP operations can trap if S'Machine_Overflow is true,
426      but don't override the user if not.  */
427   if (Machine_Overflows_On_Target)
428     flag_trapping_math = 1;
429   else if (!global_options_set.x_flag_trapping_math)
430     flag_trapping_math = 0;
431 }
432
433 /* Print language-specific items in declaration NODE.  */
434
435 static void
436 gnat_print_decl (FILE *file, tree node, int indent)
437 {
438   switch (TREE_CODE (node))
439     {
440     case CONST_DECL:
441       print_node (file, "corresponding var",
442                   DECL_CONST_CORRESPONDING_VAR (node), indent + 4);
443       break;
444
445     case FIELD_DECL:
446       print_node (file, "original field", DECL_ORIGINAL_FIELD (node),
447                   indent + 4);
448       break;
449
450     case VAR_DECL:
451       if (DECL_LOOP_PARM_P (node))
452         print_node (file, "induction var", DECL_INDUCTION_VAR (node),
453                     indent + 4);
454       else
455         print_node (file, "renamed object", DECL_RENAMED_OBJECT (node),
456                     indent + 4);
457       break;
458
459     default:
460       break;
461     }
462 }
463
464 /* Print language-specific items in type NODE.  */
465
466 static void
467 gnat_print_type (FILE *file, tree node, int indent)
468 {
469   switch (TREE_CODE (node))
470     {
471     case FUNCTION_TYPE:
472       print_node (file, "ci/co list", TYPE_CI_CO_LIST (node), indent + 4);
473       break;
474
475     case INTEGER_TYPE:
476       if (TYPE_MODULAR_P (node))
477         print_node_brief (file, "modulus", TYPE_MODULUS (node), indent + 4);
478       else if (TYPE_FIXED_POINT_P (node))
479         print_node (file, "scale factor", TYPE_SCALE_FACTOR (node),
480                     indent + 4);
481       else if (TYPE_HAS_ACTUAL_BOUNDS_P (node))
482         print_node (file, "actual bounds", TYPE_ACTUAL_BOUNDS (node),
483                     indent + 4);
484       else
485         print_node (file, "index type", TYPE_INDEX_TYPE (node), indent + 4);
486
487       /* ... fall through ... */
488
489     case ENUMERAL_TYPE:
490     case BOOLEAN_TYPE:
491       print_node_brief (file, "RM size", TYPE_RM_SIZE (node), indent + 4);
492
493       /* ... fall through ... */
494
495     case REAL_TYPE:
496       print_node_brief (file, "RM min", TYPE_RM_MIN_VALUE (node), indent + 4);
497       print_node_brief (file, "RM max", TYPE_RM_MAX_VALUE (node), indent + 4);
498       break;
499
500     case ARRAY_TYPE:
501       print_node (file,"actual bounds", TYPE_ACTUAL_BOUNDS (node), indent + 4);
502       break;
503
504     case VECTOR_TYPE:
505       print_node (file,"representative array",
506                   TYPE_REPRESENTATIVE_ARRAY (node), indent + 4);
507       break;
508
509     case RECORD_TYPE:
510       if (TYPE_FAT_POINTER_P (node) || TYPE_CONTAINS_TEMPLATE_P (node))
511         print_node (file, "unconstrained array",
512                     TYPE_UNCONSTRAINED_ARRAY (node), indent + 4);
513       else
514         print_node (file, "Ada size", TYPE_ADA_SIZE (node), indent + 4);
515       break;
516
517     case UNION_TYPE:
518     case QUAL_UNION_TYPE:
519       print_node (file, "Ada size", TYPE_ADA_SIZE (node), indent + 4);
520       break;
521
522     default:
523       break;
524     }
525
526   if (TYPE_CAN_HAVE_DEBUG_TYPE_P (node) && TYPE_DEBUG_TYPE (node))
527     print_node_brief (file, "debug type", TYPE_DEBUG_TYPE (node), indent + 4);
528
529   if (TYPE_IMPL_PACKED_ARRAY_P (node) && TYPE_ORIGINAL_PACKED_ARRAY (node))
530     print_node_brief (file, "original packed array",
531                       TYPE_ORIGINAL_PACKED_ARRAY (node), indent + 4);
532 }
533
534 /* Return the name to be printed for DECL.  */
535
536 static const char *
537 gnat_printable_name (tree decl, int verbosity)
538 {
539   const char *coded_name = IDENTIFIER_POINTER (DECL_NAME (decl));
540   char *ada_name = (char *) ggc_alloc_atomic (strlen (coded_name) * 2 + 60);
541
542   __gnat_decode (coded_name, ada_name, 0);
543
544   if (verbosity == 2 && !DECL_IS_BUILTIN (decl))
545     {
546       Set_Identifier_Casing (ada_name, DECL_SOURCE_FILE (decl));
547       return ggc_strdup (Name_Buffer);
548     }
549
550   return ada_name;
551 }
552
553 /* Return the name to be used in DWARF debug info for DECL.  */
554
555 static const char *
556 gnat_dwarf_name (tree decl, int verbosity ATTRIBUTE_UNUSED)
557 {
558   gcc_assert (DECL_P (decl));
559   return (const char *) IDENTIFIER_POINTER (DECL_NAME (decl));
560 }
561
562 /* Return the descriptive type associated with TYPE, if any.  */
563
564 static tree
565 gnat_descriptive_type (const_tree type)
566 {
567   if (TYPE_STUB_DECL (type))
568     return DECL_PARALLEL_TYPE (TYPE_STUB_DECL (type));
569   else
570     return NULL_TREE;
571 }
572
573 /* Return the underlying base type of an enumeration type.  */
574
575 static tree
576 gnat_enum_underlying_base_type (const_tree)
577 {
578   /* Enumeration types are base types in Ada.  */
579   return void_type_node;
580 }
581
582 /* Return the type to be used for debugging information instead of TYPE or
583    NULL_TREE if TYPE is fine.  */
584
585 static tree
586 gnat_get_debug_type (const_tree type)
587 {
588   if (TYPE_CAN_HAVE_DEBUG_TYPE_P (type) && TYPE_DEBUG_TYPE (type))
589     {
590       type = TYPE_DEBUG_TYPE (type);
591
592       /* ??? The get_debug_type language hook is processed after the array
593          descriptor language hook, so if there is an array behind this type,
594          the latter is supposed to handle it.  Still, we can get here with
595          a type we are not supposed to handle (e.g. when the DWARF back-end
596          processes the type of a variable), so keep this guard.  */
597       if (type && TYPE_CAN_HAVE_DEBUG_TYPE_P (type))
598         return const_cast<tree> (type);
599     }
600
601   return NULL_TREE;
602 }
603
604 /* Provide information in INFO for debugging output about the TYPE fixed-point
605    type.  Return whether TYPE is handled.  */
606
607 static bool
608 gnat_get_fixed_point_type_info (const_tree type,
609                                 struct fixed_point_type_info *info)
610 {
611   tree scale_factor;
612
613   /* GDB cannot handle fixed-point types yet, so rely on GNAT encodings
614      instead for it.  */
615   if (!TYPE_IS_FIXED_POINT_P (type)
616       || gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
617     return false;
618
619   scale_factor = TYPE_SCALE_FACTOR (type);
620
621   /* We expect here only a finite set of pattern.  See fixed-point types
622      handling in gnat_to_gnu_entity.  */
623
624   /* Put invalid values when compiler internals cannot represent the scale
625      factor.  */
626   if (scale_factor == integer_zero_node)
627     {
628       info->scale_factor_kind = fixed_point_scale_factor_arbitrary;
629       info->scale_factor.arbitrary.numerator = 0;
630       info->scale_factor.arbitrary.denominator = 0;
631       return true;
632     }
633
634   if (TREE_CODE (scale_factor) == RDIV_EXPR)
635     {
636       const tree num = TREE_OPERAND (scale_factor, 0);
637       const tree den = TREE_OPERAND (scale_factor, 1);
638
639       /* See if we have a binary or decimal scale.  */
640       if (TREE_CODE (den) == POWER_EXPR)
641         {
642           const tree base = TREE_OPERAND (den, 0);
643           const tree exponent = TREE_OPERAND (den, 1);
644
645           /* We expect the scale factor to be 1 / 2 ** N or 1 / 10 ** N.  */
646           gcc_assert (num == integer_one_node
647                       && TREE_CODE (base) == INTEGER_CST
648                       && TREE_CODE (exponent) == INTEGER_CST);
649
650           switch (tree_to_shwi (base))
651             {
652             case 2:
653               info->scale_factor_kind = fixed_point_scale_factor_binary;
654               info->scale_factor.binary = -tree_to_shwi (exponent);
655               return true;
656
657             case 10:
658               info->scale_factor_kind = fixed_point_scale_factor_decimal;
659               info->scale_factor.decimal = -tree_to_shwi (exponent);
660               return true;
661
662             default:
663               gcc_unreachable ();
664             }
665         }
666
667       /* If we reach this point, we are handling an arbitrary scale factor.  We
668          expect N / D with constant operands.  */
669       gcc_assert (TREE_CODE (num) == INTEGER_CST
670                   && TREE_CODE (den) == INTEGER_CST);
671
672       info->scale_factor_kind = fixed_point_scale_factor_arbitrary;
673       info->scale_factor.arbitrary.numerator = tree_to_uhwi (num);
674       info->scale_factor.arbitrary.denominator = tree_to_shwi (den);
675       return true;
676     }
677
678   gcc_unreachable ();
679 }
680
681 /* Return true if types T1 and T2 are identical for type hashing purposes.
682    Called only after doing all language independent checks.  At present,
683    this function is only called when both types are FUNCTION_TYPE.  */
684
685 static bool
686 gnat_type_hash_eq (const_tree t1, const_tree t2)
687 {
688   gcc_assert (TREE_CODE (t1) == FUNCTION_TYPE);
689   return fntype_same_flags_p (t1, TYPE_CI_CO_LIST (t2),
690                               TYPE_RETURN_UNCONSTRAINED_P (t2),
691                               TYPE_RETURN_BY_DIRECT_REF_P (t2),
692                               TREE_ADDRESSABLE (t2));
693 }
694
695 /* Do nothing (return the tree node passed).  */
696
697 static tree
698 gnat_return_tree (tree t)
699 {
700   return t;
701 }
702
703 /* Get the alias set corresponding to a type or expression.  */
704
705 static alias_set_type
706 gnat_get_alias_set (tree type)
707 {
708   /* If this is a padding type, use the type of the first field.  */
709   if (TYPE_IS_PADDING_P (type))
710     return get_alias_set (TREE_TYPE (TYPE_FIELDS (type)));
711
712   /* If the type is an unconstrained array, use the type of the
713      self-referential array we make.  */
714   else if (TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
715     return
716       get_alias_set (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (type)))));
717
718   /* If the type can alias any other types, return the alias set 0.  */
719   else if (TYPE_P (type)
720            && !TYPE_IS_DUMMY_P (type)
721            && TYPE_UNIVERSAL_ALIASING_P (type))
722     return 0;
723
724   return -1;
725 }
726
727 /* GNU_TYPE is a type.  Return its maximum size in bytes, if known,
728    as a constant when possible.  */
729
730 static tree
731 gnat_type_max_size (const_tree gnu_type)
732 {
733   /* First see what we can get from TYPE_SIZE_UNIT, which might not
734      be constant even for simple expressions if it has already been
735      elaborated and possibly replaced by a VAR_DECL.  */
736   tree max_unitsize = max_size (TYPE_SIZE_UNIT (gnu_type), true);
737
738   /* If we don't have a constant, try to look at attributes which should have
739      stayed untouched.  */
740   if (!tree_fits_uhwi_p (max_unitsize))
741     {
742       /* For record types, see what we can get from TYPE_ADA_SIZE.  */
743       if (RECORD_OR_UNION_TYPE_P (gnu_type)
744           && !TYPE_FAT_POINTER_P (gnu_type)
745           && TYPE_ADA_SIZE (gnu_type))
746         {
747           tree max_adasize = max_size (TYPE_ADA_SIZE (gnu_type), true);
748
749           /* If we have succeeded in finding a constant, round it up to the
750              type's alignment and return the result in units.  */
751           if (tree_fits_uhwi_p (max_adasize))
752             max_unitsize
753               = size_binop (CEIL_DIV_EXPR,
754                             round_up (max_adasize, TYPE_ALIGN (gnu_type)),
755                             bitsize_unit_node);
756         }
757
758       /* For array types, see what we can get from TYPE_INDEX_TYPE.  */
759       else if (TREE_CODE (gnu_type) == ARRAY_TYPE
760                && TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))
761                && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (gnu_type))))
762         {
763           tree lb = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
764           tree hb = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
765           if (TREE_CODE (lb) != INTEGER_CST
766               && TYPE_RM_SIZE (TREE_TYPE (lb))
767               && compare_tree_int (TYPE_RM_SIZE (TREE_TYPE (lb)), 16) <= 0)
768             lb = TYPE_MIN_VALUE (TREE_TYPE (lb));
769           if (TREE_CODE (hb) != INTEGER_CST
770               && TYPE_RM_SIZE (TREE_TYPE (hb))
771               && compare_tree_int (TYPE_RM_SIZE (TREE_TYPE (hb)), 16) <= 0)
772             hb = TYPE_MAX_VALUE (TREE_TYPE (hb));
773           if (TREE_CODE (lb) == INTEGER_CST && TREE_CODE (hb) == INTEGER_CST)
774             {
775               tree ctype = get_base_type (TREE_TYPE (lb));
776               lb = fold_convert (ctype, lb);
777               hb = fold_convert (ctype, hb);
778               if (tree_int_cst_le (lb, hb))
779                 {
780                   tree length
781                     = fold_build2 (PLUS_EXPR, ctype,
782                                    fold_build2 (MINUS_EXPR, ctype, hb, lb),
783                                    build_int_cst (ctype, 1));
784                   max_unitsize
785                     = fold_build2 (MULT_EXPR, sizetype,
786                                    fold_convert (sizetype, length),
787                                    TYPE_SIZE_UNIT (TREE_TYPE (gnu_type)));
788                 }
789             }
790         }
791     }
792
793   return max_unitsize;
794 }
795
796 static tree get_array_bit_stride (tree);
797
798 /* Provide information in INFO for debug output about the TYPE array type.
799    Return whether TYPE is handled.  */
800
801 static bool
802 gnat_get_array_descr_info (const_tree const_type,
803                            struct array_descr_info *info)
804 {
805   bool convention_fortran_p;
806   bool is_array = false;
807   bool is_fat_ptr = false;
808   bool is_packed_array = false;
809   tree type = const_cast<tree> (const_type);
810   const_tree first_dimen = NULL_TREE;
811   const_tree last_dimen = NULL_TREE;
812   const_tree dimen;
813   int i;
814
815   /* Temporaries created in the first pass and used in the second one for thin
816      pointers.  The first one is an expression that yields the template record
817      from the base address (i.e. the PLACEHOLDER_EXPR).  The second one is just
818      a cursor through this record's fields.  */
819   tree thinptr_template_expr = NULL_TREE;
820   tree thinptr_bound_field = NULL_TREE;
821
822   /* ??? See gnat_get_debug_type.  */
823   type = maybe_debug_type (type);
824
825   /* If we have an implementation type for a packed array, get the orignial
826      array type.  */
827   if (TYPE_IMPL_PACKED_ARRAY_P (type) && TYPE_ORIGINAL_PACKED_ARRAY (type))
828     {
829       type = TYPE_ORIGINAL_PACKED_ARRAY (type);
830       is_packed_array = true;
831     }
832
833   /* First pass: gather all information about this array except everything
834      related to dimensions.  */
835
836   /* Only handle ARRAY_TYPE nodes that come from GNAT.  */
837   if (TREE_CODE (type) == ARRAY_TYPE
838       && TYPE_DOMAIN (type)
839       && TYPE_INDEX_TYPE (TYPE_DOMAIN (type)))
840     {
841       is_array = true;
842       first_dimen = type;
843       info->data_location = NULL_TREE;
844     }
845
846   else if (TYPE_IS_FAT_POINTER_P (type)
847            && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
848     {
849       const tree ua_type = TYPE_UNCONSTRAINED_ARRAY (type);
850
851       /* This will be our base object address.  */
852       const tree placeholder_expr = build0 (PLACEHOLDER_EXPR, type);
853
854       /* We assume below that maybe_unconstrained_array returns an INDIRECT_REF
855          node.  */
856       const tree ua_val
857         = maybe_unconstrained_array (build_unary_op (INDIRECT_REF,
858                                                      ua_type,
859                                                      placeholder_expr));
860
861       is_fat_ptr = true;
862       first_dimen = TREE_TYPE (ua_val);
863
864       /* Get the *address* of the array, not the array itself.  */
865       info->data_location = TREE_OPERAND (ua_val, 0);
866     }
867
868   /* Unlike fat pointers (which appear for unconstrained arrays passed in
869      argument), thin pointers are used only for array access types, so we want
870      them to appear in the debug info as pointers to an array type.  That's why
871      we match only the RECORD_TYPE here instead of the POINTER_TYPE with the
872      TYPE_IS_THIN_POINTER_P predicate.  */
873   else if (TREE_CODE (type) == RECORD_TYPE
874            && TYPE_CONTAINS_TEMPLATE_P (type)
875            && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
876     {
877       /* This will be our base object address.  Note that we assume that
878          pointers to these will actually point to the array field (thin
879          pointers are shifted).  */
880       const tree placeholder_expr = build0 (PLACEHOLDER_EXPR, type);
881       const tree placeholder_addr
882         = build_unary_op (ADDR_EXPR, NULL_TREE, placeholder_expr);
883
884       const tree bounds_field = TYPE_FIELDS (type);
885       const tree bounds_type = TREE_TYPE (bounds_field);
886       const tree array_field = DECL_CHAIN (bounds_field);
887       const tree array_type = TREE_TYPE (array_field);
888
889       /* Shift the thin pointer address to get the address of the template.  */
890       const tree shift_amount
891         = fold_build1 (NEGATE_EXPR, sizetype, byte_position (array_field));
892       tree template_addr
893         = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (placeholder_addr),
894                            placeholder_addr, shift_amount);
895       template_addr
896         = fold_convert (TYPE_POINTER_TO (bounds_type), template_addr);
897
898       first_dimen = array_type;
899
900       /* The thin pointer is already the pointer to the array data, so there's
901          no need for a specific "data location" expression.  */
902       info->data_location = NULL_TREE;
903
904       thinptr_template_expr = build_unary_op (INDIRECT_REF,
905                                               bounds_type,
906                                               template_addr);
907       thinptr_bound_field = TYPE_FIELDS (bounds_type);
908     }
909   else
910     return false;
911
912   /* Second pass: compute the remaining information: dimensions and
913      corresponding bounds.  */
914
915   if (TYPE_PACKED (first_dimen))
916     is_packed_array = true;
917   /* If this array has fortran convention, it's arranged in column-major
918      order, so our view here has reversed dimensions.  */
919   convention_fortran_p = TYPE_CONVENTION_FORTRAN_P (first_dimen);
920   /* ??? For row major ordering, we probably want to emit nothing and
921      instead specify it as the default in Dw_TAG_compile_unit.  */
922   info->ordering = (convention_fortran_p
923                     ? array_descr_ordering_column_major
924                     : array_descr_ordering_row_major);
925
926   /* Count how many dimensions this array has.  */
927   for (i = 0, dimen = first_dimen; ; ++i, dimen = TREE_TYPE (dimen))
928     {
929       if (i > 0
930           && (TREE_CODE (dimen) != ARRAY_TYPE
931               || !TYPE_MULTI_ARRAY_P (dimen)))
932         break;
933       last_dimen = dimen;
934     }
935
936   info->ndimensions = i;
937   info->rank = NULL_TREE;
938
939   /* Too many dimensions?  Give up generating proper description: yield instead
940      nested arrays.  Note that in this case, this hook is invoked once on each
941      intermediate array type: be consistent and output nested arrays for all
942      dimensions.  */
943   if (info->ndimensions > DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN
944       || TYPE_MULTI_ARRAY_P (first_dimen))
945     {
946       info->ndimensions = 1;
947       last_dimen = first_dimen;
948     }
949
950   info->element_type = TREE_TYPE (last_dimen);
951
952   /* Now iterate over all dimensions in source-order and fill the info
953      structure.  */
954   for (i = (convention_fortran_p ? info->ndimensions - 1 : 0),
955        dimen = first_dimen;
956        IN_RANGE (i, 0, info->ndimensions - 1);
957        i += (convention_fortran_p ? -1 : 1),
958        dimen = TREE_TYPE (dimen))
959     {
960       /* We are interested in the stored bounds for the debug info.  */
961       tree index_type = TYPE_INDEX_TYPE (TYPE_DOMAIN (dimen));
962
963       if (is_array || is_fat_ptr)
964         {
965           /* GDB does not handle very well the self-referencial bound
966              expressions we are able to generate here for XUA types (they are
967              used only by XUP encodings) so avoid them in this case.  Note that
968              there are two cases where we generate self-referencial bound
969              expressions:  arrays that are constrained by record discriminants
970              and XUA types.  */
971           if (TYPE_CONTEXT (first_dimen)
972               && TREE_CODE (TYPE_CONTEXT (first_dimen)) != RECORD_TYPE
973               && CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (index_type))
974               && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
975             {
976               info->dimen[i].lower_bound = NULL_TREE;
977               info->dimen[i].upper_bound = NULL_TREE;
978             }
979           else
980             {
981               info->dimen[i].lower_bound
982                 = maybe_character_value (TYPE_MIN_VALUE (index_type));
983               info->dimen[i].upper_bound
984                 = maybe_character_value (TYPE_MAX_VALUE (index_type));
985             }
986         }
987
988       /* This is a thin pointer.  */
989       else
990         {
991           info->dimen[i].lower_bound
992             = build_component_ref (thinptr_template_expr, thinptr_bound_field,
993                                    false);
994           thinptr_bound_field = DECL_CHAIN (thinptr_bound_field);
995
996           info->dimen[i].upper_bound
997             = build_component_ref (thinptr_template_expr, thinptr_bound_field,
998                                    false);
999           thinptr_bound_field = DECL_CHAIN (thinptr_bound_field);
1000         }
1001
1002       /* The DWARF back-end will output BOUNDS_TYPE as the base type of
1003          the array index, so get to the base type of INDEX_TYPE.  */
1004       while (TREE_TYPE (index_type))
1005         index_type = TREE_TYPE (index_type);
1006
1007       info->dimen[i].bounds_type = maybe_debug_type (index_type);
1008       info->dimen[i].stride = NULL_TREE;
1009     }
1010
1011   /* These are Fortran-specific fields.  They make no sense here.  */
1012   info->allocated = NULL_TREE;
1013   info->associated = NULL_TREE;
1014
1015   if (gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1016     {
1017       /* When arrays contain dynamically-sized elements, we usually wrap them
1018          in padding types, or we create constrained types for them.  Then, if
1019          such types are stripped in the debugging information output, the
1020          debugger needs a way to know the size that is reserved for each
1021          element.  This is why we emit a stride in such situations.  */
1022       tree source_element_type = info->element_type;
1023
1024       while (true)
1025         {
1026           if (TYPE_DEBUG_TYPE (source_element_type))
1027             source_element_type = TYPE_DEBUG_TYPE (source_element_type);
1028           else if (TYPE_IS_PADDING_P (source_element_type))
1029             source_element_type
1030               = TREE_TYPE (TYPE_FIELDS (source_element_type));
1031           else
1032             break;
1033         }
1034
1035       if (TREE_CODE (TYPE_SIZE_UNIT (source_element_type)) != INTEGER_CST)
1036         {
1037           info->stride = TYPE_SIZE_UNIT (info->element_type);
1038           info->stride_in_bits = false;
1039         }
1040
1041       /* We need to specify a bit stride when it does not correspond to the
1042          natural size of the contained elements.  ??? Note that we do not
1043          support packed records and nested packed arrays.  */
1044       else if (is_packed_array)
1045         {
1046           info->stride = get_array_bit_stride (info->element_type);
1047           info->stride_in_bits = true;
1048         }
1049     }
1050
1051   return true;
1052 }
1053
1054 /* Given the component type COMP_TYPE of a packed array, return an expression
1055    that computes the bit stride of this packed array.  Return NULL_TREE when
1056    unsuccessful.  */
1057
1058 static tree
1059 get_array_bit_stride (tree comp_type)
1060 {
1061   struct array_descr_info info;
1062   tree stride;
1063
1064   /* Simple case: the array contains an integral type: return its RM size.  */
1065   if (INTEGRAL_TYPE_P (comp_type))
1066     return TYPE_RM_SIZE (comp_type);
1067
1068   /* Otherwise, see if this is an array we can analyze; if it's not, punt.  */
1069   memset (&info, 0, sizeof (info));
1070   if (!gnat_get_array_descr_info (comp_type, &info) || !info.stride)
1071     return NULL_TREE;
1072
1073   /* Otherwise, the array stride is the inner array's stride multiplied by the
1074      number of elements it contains.  Note that if the inner array is not
1075      packed, then the stride is "natural" and thus does not deserve an
1076      attribute.  */
1077   stride = info.stride;
1078   if (!info.stride_in_bits)
1079     {
1080       stride = fold_convert (bitsizetype, stride);
1081       stride = build_binary_op (MULT_EXPR, bitsizetype,
1082                                 stride, build_int_cst (bitsizetype, 8));
1083     }
1084
1085   for (int i = 0; i < info.ndimensions; ++i)
1086     {
1087       tree count;
1088
1089       if (!info.dimen[i].lower_bound || !info.dimen[i].upper_bound)
1090         return NULL_TREE;
1091
1092       /* Put in count an expression that computes the length of this
1093          dimension.  */
1094       count = build_binary_op (MINUS_EXPR, sbitsizetype,
1095                                fold_convert (sbitsizetype,
1096                                              info.dimen[i].upper_bound),
1097                                fold_convert (sbitsizetype,
1098                                              info.dimen[i].lower_bound)),
1099       count = build_binary_op (PLUS_EXPR, sbitsizetype,
1100                                count, build_int_cst (sbitsizetype, 1));
1101       count = build_binary_op (MAX_EXPR, sbitsizetype,
1102                                count,
1103                                build_int_cst (sbitsizetype, 0));
1104       count = fold_convert (bitsizetype, count);
1105       stride = build_binary_op (MULT_EXPR, bitsizetype, stride, count);
1106     }
1107
1108   return stride;
1109 }
1110
1111 /* GNU_TYPE is a subtype of an integral type.  Set LOWVAL to the low bound
1112    and HIGHVAL to the high bound, respectively.  */
1113
1114 static void
1115 gnat_get_subrange_bounds (const_tree gnu_type, tree *lowval, tree *highval)
1116 {
1117   *lowval = TYPE_MIN_VALUE (gnu_type);
1118   *highval = TYPE_MAX_VALUE (gnu_type);
1119 }
1120
1121 /* Return the bias of GNU_TYPE, if any.  */
1122
1123 static tree
1124 gnat_get_type_bias (const_tree gnu_type)
1125 {
1126   if (TREE_CODE (gnu_type) == INTEGER_TYPE
1127       && TYPE_BIASED_REPRESENTATION_P (gnu_type)
1128       && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
1129     return TYPE_RM_MIN_VALUE (gnu_type);
1130
1131   return NULL_TREE;
1132 }
1133
1134 /* GNU_TYPE is the type of a subprogram parameter.  Determine if it should be
1135    passed by reference by default.  */
1136
1137 bool
1138 default_pass_by_ref (tree gnu_type)
1139 {
1140   /* We pass aggregates by reference if they are sufficiently large for
1141      their alignment.  The ratio is somewhat arbitrary.  We also pass by
1142      reference if the target machine would either pass or return by
1143      reference.  Strictly speaking, we need only check the return if this
1144      is an In Out parameter, but it's probably best to err on the side of
1145      passing more things by reference.  */
1146
1147   if (AGGREGATE_TYPE_P (gnu_type)
1148       && (!valid_constant_size_p (TYPE_SIZE_UNIT (gnu_type))
1149           || compare_tree_int (TYPE_SIZE_UNIT (gnu_type),
1150                                TYPE_ALIGN (gnu_type)) > 0))
1151     return true;
1152
1153   if (pass_by_reference (NULL, TYPE_MODE (gnu_type), gnu_type, true))
1154     return true;
1155
1156   if (targetm.calls.return_in_memory (gnu_type, NULL_TREE))
1157     return true;
1158
1159   return false;
1160 }
1161
1162 /* GNU_TYPE is the type of a subprogram parameter.  Determine if it must be
1163    passed by reference.  */
1164
1165 bool
1166 must_pass_by_ref (tree gnu_type)
1167 {
1168   /* We pass only unconstrained objects, those required by the language
1169      to be passed by reference, and objects of variable size.  The latter
1170      is more efficient, avoids problems with variable size temporaries,
1171      and does not produce compatibility problems with C, since C does
1172      not have such objects.  */
1173   return (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
1174           || TYPE_IS_BY_REFERENCE_P (gnu_type)
1175           || (TYPE_SIZE_UNIT (gnu_type)
1176               && TREE_CODE (TYPE_SIZE_UNIT (gnu_type)) != INTEGER_CST));
1177 }
1178
1179 /* This function is called by the front-end to enumerate all the supported
1180    modes for the machine, as well as some predefined C types.  F is a function
1181    which is called back with the parameters as listed below, first a string,
1182    then seven ints.  The name is any arbitrary null-terminated string and has
1183    no particular significance, except for the case of predefined C types, where
1184    it should be the name of the C type.  For integer types, only signed types
1185    should be listed, unsigned versions are assumed.  The order of types should
1186    be in order of preference, with the smallest/cheapest types first.
1187
1188    In particular, C predefined types should be listed before other types,
1189    binary floating point types before decimal ones, and narrower/cheaper
1190    type versions before more expensive ones.  In type selection the first
1191    matching variant will be used.
1192
1193    NAME         pointer to first char of type name
1194    DIGS         number of decimal digits for floating-point modes, else 0
1195    COMPLEX_P    nonzero is this represents a complex mode
1196    COUNT        count of number of items, nonzero for vector mode
1197    FLOAT_REP    Float_Rep_Kind for FP, otherwise undefined
1198    PRECISION    number of bits used to store data
1199    SIZE         number of bits occupied by the mode
1200    ALIGN        number of bits to which mode is aligned.  */
1201
1202 void
1203 enumerate_modes (void (*f) (const char *, int, int, int, int, int, int, int))
1204 {
1205   const tree c_types[]
1206     = { float_type_node, double_type_node, long_double_type_node };
1207   const char *const c_names[]
1208     = { "float", "double", "long double" };
1209   int iloop;
1210
1211   /* We are going to compute it below.  */
1212   fp_arith_may_widen = false;
1213
1214   for (iloop = 0; iloop < NUM_MACHINE_MODES; iloop++)
1215     {
1216       machine_mode i = (machine_mode) iloop;
1217       machine_mode inner_mode = i;
1218       bool float_p = false;
1219       bool complex_p = false;
1220       bool vector_p = false;
1221       bool skip_p = false;
1222       int digs = 0;
1223       unsigned int nameloop;
1224       Float_Rep_Kind float_rep = IEEE_Binary; /* Until proven otherwise */
1225
1226       switch (GET_MODE_CLASS (i))
1227         {
1228         case MODE_INT:
1229           break;
1230         case MODE_FLOAT:
1231           float_p = true;
1232           break;
1233         case MODE_COMPLEX_INT:
1234           complex_p = true;
1235           inner_mode = GET_MODE_INNER (i);
1236           break;
1237         case MODE_COMPLEX_FLOAT:
1238           float_p = true;
1239           complex_p = true;
1240           inner_mode = GET_MODE_INNER (i);
1241           break;
1242         case MODE_VECTOR_INT:
1243           vector_p = true;
1244           inner_mode = GET_MODE_INNER (i);
1245           break;
1246         case MODE_VECTOR_FLOAT:
1247           float_p = true;
1248           vector_p = true;
1249           inner_mode = GET_MODE_INNER (i);
1250           break;
1251         default:
1252           skip_p = true;
1253         }
1254
1255       if (float_p)
1256         {
1257           const struct real_format *fmt = REAL_MODE_FORMAT (inner_mode);
1258
1259           /* ??? Cope with the ghost XFmode of the ARM port.  */
1260           if (!fmt)
1261             continue;
1262
1263           /* Be conservative and consider that floating-point arithmetics may
1264              use wider intermediate results as soon as there is an extended
1265              Motorola or Intel mode supported by the machine.  */
1266           if (fmt == &ieee_extended_motorola_format
1267               || fmt == &ieee_extended_intel_96_format
1268               || fmt == &ieee_extended_intel_96_round_53_format
1269               || fmt == &ieee_extended_intel_128_format)
1270             {
1271 #ifdef TARGET_FPMATH_DEFAULT
1272               if (TARGET_FPMATH_DEFAULT == FPMATH_387)
1273 #endif
1274                 fp_arith_may_widen = true;
1275             }
1276
1277           if (fmt->b == 2)
1278             digs = (fmt->p - 1) * 1233 / 4096; /* scale by log (2) */
1279
1280           else if (fmt->b == 10)
1281             digs = fmt->p;
1282
1283           else
1284             gcc_unreachable ();
1285         }
1286
1287       /* First register any C types for this mode that the front end
1288          may need to know about, unless the mode should be skipped.  */
1289       if (!skip_p && !vector_p)
1290         for (nameloop = 0; nameloop < ARRAY_SIZE (c_types); nameloop++)
1291           {
1292             tree type = c_types[nameloop];
1293             const char *name = c_names[nameloop];
1294
1295             if (TYPE_MODE (type) == i)
1296               {
1297                 f (name, digs, complex_p, 0, float_rep, TYPE_PRECISION (type),
1298                    TREE_INT_CST_LOW (TYPE_SIZE (type)), TYPE_ALIGN (type));
1299                 skip_p = true;
1300               }
1301           }
1302
1303       /* If no predefined C types were found, register the mode itself.  */
1304       int nunits, precision, bitsize;
1305       if (!skip_p
1306           && GET_MODE_NUNITS (i).is_constant (&nunits)
1307           && GET_MODE_PRECISION (i).is_constant (&precision)
1308           && GET_MODE_BITSIZE (i).is_constant (&bitsize))
1309         f (GET_MODE_NAME (i), digs, complex_p,
1310            vector_p ? nunits : 0, float_rep,
1311            precision, bitsize, GET_MODE_ALIGNMENT (i));
1312     }
1313 }
1314
1315 /* Return the size of the FP mode with precision PREC.  */
1316
1317 int
1318 fp_prec_to_size (int prec)
1319 {
1320   opt_scalar_float_mode opt_mode;
1321
1322   FOR_EACH_MODE_IN_CLASS (opt_mode, MODE_FLOAT)
1323     {
1324       scalar_float_mode mode = opt_mode.require ();
1325       if (GET_MODE_PRECISION (mode) == prec)
1326         return GET_MODE_BITSIZE (mode);
1327     }
1328
1329   gcc_unreachable ();
1330 }
1331
1332 /* Return the precision of the FP mode with size SIZE.  */
1333
1334 int
1335 fp_size_to_prec (int size)
1336 {
1337   opt_scalar_float_mode opt_mode;
1338
1339   FOR_EACH_MODE_IN_CLASS (opt_mode, MODE_FLOAT)
1340     {
1341       scalar_mode mode = opt_mode.require ();
1342       if (GET_MODE_BITSIZE (mode) == size)
1343         return GET_MODE_PRECISION (mode);
1344     }
1345
1346   gcc_unreachable ();
1347 }
1348
1349 static GTY(()) tree gnat_eh_personality_decl;
1350
1351 /* Return the GNAT personality function decl.  */
1352
1353 static tree
1354 gnat_eh_personality (void)
1355 {
1356   if (!gnat_eh_personality_decl)
1357     gnat_eh_personality_decl = build_personality_function ("gnat");
1358   return gnat_eh_personality_decl;
1359 }
1360
1361 /* Initialize language-specific bits of tree_contains_struct.  */
1362
1363 static void
1364 gnat_init_ts (void)
1365 {
1366   MARK_TS_COMMON (UNCONSTRAINED_ARRAY_TYPE);
1367
1368   MARK_TS_TYPED (UNCONSTRAINED_ARRAY_REF);
1369   MARK_TS_TYPED (NULL_EXPR);
1370   MARK_TS_TYPED (PLUS_NOMOD_EXPR);
1371   MARK_TS_TYPED (MINUS_NOMOD_EXPR);
1372   MARK_TS_TYPED (POWER_EXPR);
1373   MARK_TS_TYPED (ATTR_ADDR_EXPR);
1374   MARK_TS_TYPED (STMT_STMT);
1375   MARK_TS_TYPED (LOOP_STMT);
1376   MARK_TS_TYPED (EXIT_STMT);
1377 }
1378
1379 /* Return the size of a tree with CODE, which is a language-specific tree code
1380    in category tcc_constant, tcc_exceptional or tcc_type.  The default expects
1381    never to be called.  */
1382
1383 static size_t
1384 gnat_tree_size (enum tree_code code)
1385 {
1386   gcc_checking_assert (code >= NUM_TREE_CODES);
1387   switch (code)
1388     {
1389     case UNCONSTRAINED_ARRAY_TYPE:
1390       return sizeof (tree_type_non_common);
1391     default:
1392       gcc_unreachable ();
1393     }
1394 }
1395
1396 /* Return the lang specific structure attached to NODE.  Allocate it (cleared)
1397    if needed.  */
1398
1399 struct lang_type *
1400 get_lang_specific (tree node)
1401 {
1402   if (!TYPE_LANG_SPECIFIC (node))
1403     TYPE_LANG_SPECIFIC (node) = ggc_cleared_alloc<struct lang_type> ();
1404   return TYPE_LANG_SPECIFIC (node);
1405 }
1406
1407 /* Definitions for our language-specific hooks.  */
1408
1409 #undef  LANG_HOOKS_NAME
1410 #define LANG_HOOKS_NAME                 "GNU Ada"
1411 #undef  LANG_HOOKS_IDENTIFIER_SIZE
1412 #define LANG_HOOKS_IDENTIFIER_SIZE      sizeof (struct tree_identifier)
1413 #undef  LANG_HOOKS_TREE_SIZE
1414 #define LANG_HOOKS_TREE_SIZE            gnat_tree_size
1415 #undef  LANG_HOOKS_INIT
1416 #define LANG_HOOKS_INIT                 gnat_init
1417 #undef  LANG_HOOKS_OPTION_LANG_MASK
1418 #define LANG_HOOKS_OPTION_LANG_MASK     gnat_option_lang_mask
1419 #undef  LANG_HOOKS_INIT_OPTIONS_STRUCT
1420 #define LANG_HOOKS_INIT_OPTIONS_STRUCT  gnat_init_options_struct
1421 #undef  LANG_HOOKS_INIT_OPTIONS
1422 #define LANG_HOOKS_INIT_OPTIONS         gnat_init_options
1423 #undef  LANG_HOOKS_HANDLE_OPTION
1424 #define LANG_HOOKS_HANDLE_OPTION        gnat_handle_option
1425 #undef  LANG_HOOKS_POST_OPTIONS
1426 #define LANG_HOOKS_POST_OPTIONS         gnat_post_options
1427 #undef  LANG_HOOKS_PARSE_FILE
1428 #define LANG_HOOKS_PARSE_FILE           gnat_parse_file
1429 #undef  LANG_HOOKS_TYPE_HASH_EQ
1430 #define LANG_HOOKS_TYPE_HASH_EQ         gnat_type_hash_eq
1431 #undef  LANG_HOOKS_GETDECLS
1432 #define LANG_HOOKS_GETDECLS             hook_tree_void_null
1433 #undef  LANG_HOOKS_PUSHDECL
1434 #define LANG_HOOKS_PUSHDECL             gnat_return_tree
1435 #undef  LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL
1436 #define LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL hook_bool_const_tree_false
1437 #undef  LANG_HOOKS_GET_ALIAS_SET
1438 #define LANG_HOOKS_GET_ALIAS_SET        gnat_get_alias_set
1439 #undef  LANG_HOOKS_PRINT_DECL
1440 #define LANG_HOOKS_PRINT_DECL           gnat_print_decl
1441 #undef  LANG_HOOKS_PRINT_TYPE
1442 #define LANG_HOOKS_PRINT_TYPE           gnat_print_type
1443 #undef  LANG_HOOKS_TYPE_MAX_SIZE
1444 #define LANG_HOOKS_TYPE_MAX_SIZE        gnat_type_max_size
1445 #undef  LANG_HOOKS_DECL_PRINTABLE_NAME
1446 #define LANG_HOOKS_DECL_PRINTABLE_NAME  gnat_printable_name
1447 #undef  LANG_HOOKS_DWARF_NAME
1448 #define LANG_HOOKS_DWARF_NAME           gnat_dwarf_name
1449 #undef  LANG_HOOKS_GIMPLIFY_EXPR
1450 #define LANG_HOOKS_GIMPLIFY_EXPR        gnat_gimplify_expr
1451 #undef  LANG_HOOKS_TYPE_FOR_MODE
1452 #define LANG_HOOKS_TYPE_FOR_MODE        gnat_type_for_mode
1453 #undef  LANG_HOOKS_TYPE_FOR_SIZE
1454 #define LANG_HOOKS_TYPE_FOR_SIZE        gnat_type_for_size
1455 #undef  LANG_HOOKS_TYPES_COMPATIBLE_P
1456 #define LANG_HOOKS_TYPES_COMPATIBLE_P   gnat_types_compatible_p
1457 #undef  LANG_HOOKS_GET_ARRAY_DESCR_INFO
1458 #define LANG_HOOKS_GET_ARRAY_DESCR_INFO gnat_get_array_descr_info
1459 #undef  LANG_HOOKS_GET_SUBRANGE_BOUNDS
1460 #define LANG_HOOKS_GET_SUBRANGE_BOUNDS  gnat_get_subrange_bounds
1461 #undef  LANG_HOOKS_GET_TYPE_BIAS
1462 #define LANG_HOOKS_GET_TYPE_BIAS        gnat_get_type_bias
1463 #undef  LANG_HOOKS_DESCRIPTIVE_TYPE
1464 #define LANG_HOOKS_DESCRIPTIVE_TYPE     gnat_descriptive_type
1465 #undef  LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE
1466 #define LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE gnat_enum_underlying_base_type
1467 #undef  LANG_HOOKS_GET_DEBUG_TYPE
1468 #define LANG_HOOKS_GET_DEBUG_TYPE       gnat_get_debug_type
1469 #undef  LANG_HOOKS_GET_FIXED_POINT_TYPE_INFO
1470 #define LANG_HOOKS_GET_FIXED_POINT_TYPE_INFO gnat_get_fixed_point_type_info
1471 #undef  LANG_HOOKS_ATTRIBUTE_TABLE
1472 #define LANG_HOOKS_ATTRIBUTE_TABLE      gnat_internal_attribute_table
1473 #undef  LANG_HOOKS_BUILTIN_FUNCTION
1474 #define LANG_HOOKS_BUILTIN_FUNCTION     gnat_builtin_function
1475 #undef  LANG_HOOKS_INIT_TS
1476 #define LANG_HOOKS_INIT_TS              gnat_init_ts
1477 #undef  LANG_HOOKS_EH_PERSONALITY
1478 #define LANG_HOOKS_EH_PERSONALITY       gnat_eh_personality
1479 #undef  LANG_HOOKS_DEEP_UNSHARING
1480 #define LANG_HOOKS_DEEP_UNSHARING       true
1481 #undef  LANG_HOOKS_CUSTOM_FUNCTION_DESCRIPTORS
1482 #define LANG_HOOKS_CUSTOM_FUNCTION_DESCRIPTORS true
1483
1484 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
1485
1486 #include "gt-ada-misc.h"