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