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