1/* Default language-specific hooks.
2   Copyright (C) 2001-2015 Free Software Foundation, Inc.
3   Contributed by Alexandre Oliva  <aoliva@redhat.com>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3.  If not see
19<http://www.gnu.org/licenses/>.  */
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "intl.h"
25#include "tm.h"
26#include "toplev.h"
27#include "hash-set.h"
28#include "machmode.h"
29#include "vec.h"
30#include "double-int.h"
31#include "input.h"
32#include "alias.h"
33#include "symtab.h"
34#include "wide-int.h"
35#include "inchash.h"
36#include "tree.h"
37#include "stringpool.h"
38#include "attribs.h"
39#include "tree-inline.h"
40#include "gimplify.h"
41#include "rtl.h"
42#include "insn-config.h"
43#include "flags.h"
44#include "langhooks.h"
45#include "target.h"
46#include "langhooks-def.h"
47#include "diagnostic.h"
48#include "tree-diagnostic.h"
49#include "hash-map.h"
50#include "is-a.h"
51#include "plugin-api.h"
52#include "hard-reg-set.h"
53#include "input.h"
54#include "function.h"
55#include "ipa-ref.h"
56#include "cgraph.h"
57#include "timevar.h"
58#include "output.h"
59
60/* Do nothing; in many cases the default hook.  */
61
62void
63lhd_do_nothing (void)
64{
65}
66
67/* Do nothing (tree).  */
68
69void
70lhd_do_nothing_t (tree ARG_UNUSED (t))
71{
72}
73
74/* Pass through (tree).  */
75tree
76lhd_pass_through_t (tree t)
77{
78  return t;
79}
80
81/* Do nothing (int, int, int).  Return NULL_TREE.  */
82
83tree
84lhd_do_nothing_iii_return_null_tree (int ARG_UNUSED (i),
85				     int ARG_UNUSED (j),
86				     int ARG_UNUSED (k))
87{
88  return NULL_TREE;
89}
90
91/* Do nothing (function).  */
92
93void
94lhd_do_nothing_f (struct function * ARG_UNUSED (f))
95{
96}
97
98/* Do nothing (return NULL_TREE).  */
99
100tree
101lhd_return_null_tree_v (void)
102{
103  return NULL_TREE;
104}
105
106/* Do nothing (return NULL_TREE).  */
107
108tree
109lhd_return_null_tree (tree ARG_UNUSED (t))
110{
111  return NULL_TREE;
112}
113
114/* Do nothing (return NULL_TREE).  */
115
116tree
117lhd_return_null_const_tree (const_tree ARG_UNUSED (t))
118{
119  return NULL_TREE;
120}
121
122/* The default post options hook.  */
123
124bool
125lhd_post_options (const char ** ARG_UNUSED (pfilename))
126{
127  /* Excess precision other than "fast" requires front-end
128     support.  */
129  flag_excess_precision_cmdline = EXCESS_PRECISION_FAST;
130  return false;
131}
132
133/* Called from by print-tree.c.  */
134
135void
136lhd_print_tree_nothing (FILE * ARG_UNUSED (file),
137			tree ARG_UNUSED (node),
138			int ARG_UNUSED (indent))
139{
140}
141
142/* Called from check_global_declarations.  */
143
144bool
145lhd_warn_unused_global_decl (const_tree decl)
146{
147  /* This is what used to exist in check_global_declarations.  Probably
148     not many of these actually apply to non-C languages.  */
149
150  if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
151    return false;
152  if (TREE_CODE (decl) == VAR_DECL && TREE_READONLY (decl))
153    return false;
154  if (DECL_IN_SYSTEM_HEADER (decl))
155    return false;
156
157  return true;
158}
159
160/* Set the DECL_ASSEMBLER_NAME for DECL.  */
161void
162lhd_set_decl_assembler_name (tree decl)
163{
164  tree id;
165
166  /* set_decl_assembler_name may be called on TYPE_DECL to record ODR
167     name for C++ types.  By default types have no ODR names.  */
168  if (TREE_CODE (decl) == TYPE_DECL)
169    return;
170
171  /* The language-independent code should never use the
172     DECL_ASSEMBLER_NAME for lots of DECLs.  Only FUNCTION_DECLs and
173     VAR_DECLs for variables with static storage duration need a real
174     DECL_ASSEMBLER_NAME.  */
175  gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
176	      || (TREE_CODE (decl) == VAR_DECL
177		  && (TREE_STATIC (decl)
178		      || DECL_EXTERNAL (decl)
179		      || TREE_PUBLIC (decl))));
180
181  /* By default, assume the name to use in assembly code is the same
182     as that used in the source language.  (That's correct for C, and
183     GCC used to set DECL_ASSEMBLER_NAME to the same value as
184     DECL_NAME in build_decl, so this choice provides backwards
185     compatibility with existing front-ends.  This assumption is wrapped
186     in a target hook, to allow for target-specific modification of the
187     identifier.
188
189     Can't use just the variable's own name for a variable whose scope
190     is less than the whole compilation.  Concatenate a distinguishing
191     number - we use the DECL_UID.  */
192
193  if (TREE_PUBLIC (decl) || DECL_FILE_SCOPE_P (decl))
194    id = targetm.mangle_decl_assembler_name (decl, DECL_NAME (decl));
195  else
196    {
197      const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
198      char *label;
199
200      ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
201      id = get_identifier (label);
202    }
203  SET_DECL_ASSEMBLER_NAME (decl, id);
204
205}
206
207/* Type promotion for variable arguments.  */
208tree
209lhd_type_promotes_to (tree ARG_UNUSED (type))
210{
211  gcc_unreachable ();
212}
213
214/* Registration of machine- or os-specific builtin types.  */
215void
216lhd_register_builtin_type (tree ARG_UNUSED (type),
217			   const char * ARG_UNUSED (name))
218{
219}
220
221/* Invalid use of an incomplete type.  */
222void
223lhd_incomplete_type_error (const_tree ARG_UNUSED (value), const_tree type)
224{
225  gcc_assert (TREE_CODE (type) == ERROR_MARK);
226  return;
227}
228
229/* Provide a default routine for alias sets that always returns -1.  This
230   is used by languages that don't need to do anything special.  */
231
232alias_set_type
233lhd_get_alias_set (tree ARG_UNUSED (t))
234{
235  return -1;
236}
237
238/* This is the default decl_printable_name function.  */
239
240const char *
241lhd_decl_printable_name (tree decl, int ARG_UNUSED (verbosity))
242{
243  gcc_assert (decl && DECL_NAME (decl));
244  return IDENTIFIER_POINTER (DECL_NAME (decl));
245}
246
247/* This is the default dwarf_name function.  */
248
249const char *
250lhd_dwarf_name (tree t, int verbosity)
251{
252  gcc_assert (DECL_P (t));
253
254  return lang_hooks.decl_printable_name (t, verbosity);
255}
256
257/* This compares two types for equivalence ("compatible" in C-based languages).
258   This routine should only return 1 if it is sure.  It should not be used
259   in contexts where erroneously returning 0 causes problems.  */
260
261int
262lhd_types_compatible_p (tree x, tree y)
263{
264  return TYPE_MAIN_VARIANT (x) == TYPE_MAIN_VARIANT (y);
265}
266
267/* lang_hooks.tree_dump.dump_tree:  Dump language-specific parts of tree
268   nodes.  Returns nonzero if it does not want the usual dumping of the
269   second argument.  */
270
271bool
272lhd_tree_dump_dump_tree (void *di ATTRIBUTE_UNUSED, tree t ATTRIBUTE_UNUSED)
273{
274  return false;
275}
276
277/* lang_hooks.tree_dump.type_qual:  Determine type qualifiers in a
278   language-specific way.  */
279
280int
281lhd_tree_dump_type_quals (const_tree t)
282{
283  return TYPE_QUALS (t);
284}
285
286/* lang_hooks.gimplify_expr re-writes *EXPR_P into GIMPLE form.  */
287
288int
289lhd_gimplify_expr (tree *expr_p ATTRIBUTE_UNUSED,
290		   gimple_seq *pre_p ATTRIBUTE_UNUSED,
291		   gimple_seq *post_p ATTRIBUTE_UNUSED)
292{
293  return GS_UNHANDLED;
294}
295
296/* lang_hooks.tree_size: Determine the size of a tree with code C,
297   which is a language-specific tree code in category tcc_constant or
298   tcc_exceptional.  The default expects never to be called.  */
299size_t
300lhd_tree_size (enum tree_code c ATTRIBUTE_UNUSED)
301{
302  gcc_unreachable ();
303}
304
305/* Return true if decl, which is a function decl, may be called by a
306   sibcall.  */
307
308bool
309lhd_decl_ok_for_sibcall (const_tree decl ATTRIBUTE_UNUSED)
310{
311  return true;
312}
313
314/* lang_hooks.decls.final_write_globals: perform final processing on
315   global variables.  */
316void
317write_global_declarations (void)
318{
319  tree globals, decl, *vec;
320  int len, i;
321
322  timevar_start (TV_PHASE_DEFERRED);
323  /* Really define vars that have had only a tentative definition.
324     Really output inline functions that must actually be callable
325     and have not been output so far.  */
326
327  globals = lang_hooks.decls.getdecls ();
328  len = list_length (globals);
329  vec = XNEWVEC (tree, len);
330
331  /* Process the decls in reverse order--earliest first.
332     Put them into VEC from back to front, then take out from front.  */
333
334  for (i = 0, decl = globals; i < len; i++, decl = DECL_CHAIN (decl))
335    vec[len - i - 1] = decl;
336
337  wrapup_global_declarations (vec, len);
338  check_global_declarations (vec, len);
339  timevar_stop (TV_PHASE_DEFERRED);
340
341  timevar_start (TV_PHASE_OPT_GEN);
342  /* This lang hook is dual-purposed, and also finalizes the
343     compilation unit.  */
344  symtab->finalize_compilation_unit ();
345  timevar_stop (TV_PHASE_OPT_GEN);
346
347  timevar_start (TV_PHASE_DBGINFO);
348  emit_debug_global_declarations (vec, len);
349  timevar_stop (TV_PHASE_DBGINFO);
350
351  /* Clean up.  */
352  free (vec);
353}
354
355/* Called to perform language-specific initialization of CTX.  */
356void
357lhd_initialize_diagnostics (diagnostic_context *ctx ATTRIBUTE_UNUSED)
358{
359}
360
361/* Called to perform language-specific options initialization.  */
362void
363lhd_init_options (unsigned int decoded_options_count ATTRIBUTE_UNUSED,
364		  struct cl_decoded_option *decoded_options ATTRIBUTE_UNUSED)
365{
366}
367
368/* By default, always complain about options for the wrong language.  */
369bool
370lhd_complain_wrong_lang_p (const struct cl_option *option ATTRIBUTE_UNUSED)
371{
372  return true;
373}
374
375/* By default, no language-specific options are valid.  */
376bool
377lhd_handle_option (size_t code ATTRIBUTE_UNUSED,
378		   const char *arg ATTRIBUTE_UNUSED,
379		   int value ATTRIBUTE_UNUSED, int kind ATTRIBUTE_UNUSED,
380		   location_t loc ATTRIBUTE_UNUSED,
381		   const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
382{
383  return false;
384}
385
386/* The default function to print out name of current function that caused
387   an error.  */
388void
389lhd_print_error_function (diagnostic_context *context, const char *file,
390			  diagnostic_info *diagnostic)
391{
392  if (diagnostic_last_function_changed (context, diagnostic))
393    {
394      const char *old_prefix = context->printer->prefix;
395      tree abstract_origin = diagnostic_abstract_origin (diagnostic);
396      char *new_prefix = (file && abstract_origin == NULL)
397			 ? file_name_as_prefix (context, file) : NULL;
398
399      pp_set_prefix (context->printer, new_prefix);
400
401      if (current_function_decl == NULL)
402	pp_printf (context->printer, _("At top level:"));
403      else
404	{
405	  tree fndecl, ao;
406
407	  if (abstract_origin)
408	    {
409	      ao = BLOCK_ABSTRACT_ORIGIN (abstract_origin);
410	      while (TREE_CODE (ao) == BLOCK
411		     && BLOCK_ABSTRACT_ORIGIN (ao)
412		     && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
413		ao = BLOCK_ABSTRACT_ORIGIN (ao);
414	      gcc_assert (TREE_CODE (ao) == FUNCTION_DECL);
415	      fndecl = ao;
416	    }
417	  else
418	    fndecl = current_function_decl;
419
420	  if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
421	    pp_printf
422	      (context->printer, _("In member function %qs"),
423	       identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2)));
424	  else
425	    pp_printf
426	      (context->printer, _("In function %qs"),
427	       identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2)));
428
429	  while (abstract_origin)
430	    {
431	      location_t *locus;
432	      tree block = abstract_origin;
433
434	      locus = &BLOCK_SOURCE_LOCATION (block);
435	      fndecl = NULL;
436	      block = BLOCK_SUPERCONTEXT (block);
437	      while (block && TREE_CODE (block) == BLOCK
438		     && BLOCK_ABSTRACT_ORIGIN (block))
439		{
440		  ao = BLOCK_ABSTRACT_ORIGIN (block);
441
442		  while (TREE_CODE (ao) == BLOCK
443			 && BLOCK_ABSTRACT_ORIGIN (ao)
444			 && BLOCK_ABSTRACT_ORIGIN (ao) != ao)
445		    ao = BLOCK_ABSTRACT_ORIGIN (ao);
446
447		  if (TREE_CODE (ao) == FUNCTION_DECL)
448		    {
449		      fndecl = ao;
450		      break;
451		    }
452		  else if (TREE_CODE (ao) != BLOCK)
453		    break;
454
455		  block = BLOCK_SUPERCONTEXT (block);
456		}
457	      if (fndecl)
458		abstract_origin = block;
459	      else
460		{
461		  while (block && TREE_CODE (block) == BLOCK)
462		    block = BLOCK_SUPERCONTEXT (block);
463
464		  if (block && TREE_CODE (block) == FUNCTION_DECL)
465		    fndecl = block;
466		  abstract_origin = NULL;
467		}
468	      if (fndecl)
469		{
470		  expanded_location s = expand_location (*locus);
471		  pp_comma (context->printer);
472		  pp_newline (context->printer);
473		  if (s.file != NULL)
474		    {
475		      if (context->show_column)
476			pp_printf (context->printer,
477				   _("    inlined from %qs at %r%s:%d:%d%R"),
478				   identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2)),
479				   "locus", s.file, s.line, s.column);
480		      else
481			pp_printf (context->printer,
482				   _("    inlined from %qs at %r%s:%d%R"),
483				   identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2)),
484				   "locus", s.file, s.line);
485
486		    }
487		  else
488		    pp_printf (context->printer, _("    inlined from %qs"),
489			       identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 2)));
490		}
491	    }
492	  pp_colon (context->printer);
493	}
494
495      diagnostic_set_last_function (context, diagnostic);
496      pp_newline_and_flush (context->printer);
497      context->printer->prefix = old_prefix;
498      free ((char*) new_prefix);
499    }
500}
501
502tree
503lhd_make_node (enum tree_code code)
504{
505  return make_node (code);
506}
507
508HOST_WIDE_INT
509lhd_to_target_charset (HOST_WIDE_INT c)
510{
511  return c;
512}
513
514tree
515lhd_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se ATTRIBUTE_UNUSED)
516{
517  return expr;
518}
519
520/* Return sharing kind if OpenMP sharing attribute of DECL is
521   predetermined, OMP_CLAUSE_DEFAULT_UNSPECIFIED otherwise.  */
522
523enum omp_clause_default_kind
524lhd_omp_predetermined_sharing (tree decl ATTRIBUTE_UNUSED)
525{
526  if (DECL_ARTIFICIAL (decl))
527    return OMP_CLAUSE_DEFAULT_SHARED;
528  return OMP_CLAUSE_DEFAULT_UNSPECIFIED;
529}
530
531/* Generate code to copy SRC to DST.  */
532
533tree
534lhd_omp_assignment (tree clause ATTRIBUTE_UNUSED, tree dst, tree src)
535{
536  return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
537}
538
539/* Finalize clause C.  */
540
541void
542lhd_omp_finish_clause (tree, gimple_seq *)
543{
544}
545
546/* Register language specific type size variables as potentially OpenMP
547   firstprivate variables.  */
548
549void
550lhd_omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *c ATTRIBUTE_UNUSED,
551				   tree t ATTRIBUTE_UNUSED)
552{
553}
554
555/* Return true if TYPE is an OpenMP mappable type.  */
556
557bool
558lhd_omp_mappable_type (tree type)
559{
560  /* Mappable type has to be complete.  */
561  if (type == error_mark_node || !COMPLETE_TYPE_P (type))
562    return false;
563  return true;
564}
565
566/* Common function for add_builtin_function and
567   add_builtin_function_ext_scope.  */
568static tree
569add_builtin_function_common (const char *name,
570			     tree type,
571			     int function_code,
572			     enum built_in_class cl,
573			     const char *library_name,
574			     tree attrs,
575			     tree (*hook) (tree))
576{
577  tree   id = get_identifier (name);
578  tree decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, id, type);
579
580  TREE_PUBLIC (decl)         = 1;
581  DECL_EXTERNAL (decl)       = 1;
582  DECL_BUILT_IN_CLASS (decl) = cl;
583
584  DECL_FUNCTION_CODE (decl)  = (enum built_in_function) function_code;
585
586  /* DECL_FUNCTION_CODE is a bitfield; verify that the value fits.  */
587  gcc_assert (DECL_FUNCTION_CODE (decl) == function_code);
588
589  if (library_name)
590    {
591      tree libname = get_identifier (library_name);
592      SET_DECL_ASSEMBLER_NAME (decl, libname);
593    }
594
595  /* Possibly apply some default attributes to this built-in function.  */
596  if (attrs)
597    decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
598  else
599    decl_attributes (&decl, NULL_TREE, 0);
600
601  return hook (decl);
602
603}
604
605/* Create a builtin function.  */
606
607tree
608add_builtin_function (const char *name,
609		      tree type,
610		      int function_code,
611		      enum built_in_class cl,
612		      const char *library_name,
613		      tree attrs)
614{
615  return add_builtin_function_common (name, type, function_code, cl,
616				      library_name, attrs,
617				      lang_hooks.builtin_function);
618}
619
620/* Like add_builtin_function, but make sure the scope is the external scope.
621   This is used to delay putting in back end builtin functions until the ISA
622   that defines the builtin is declared via function specific target options,
623   which can save memory for machines like the x86_64 that have multiple ISAs.
624   If this points to the same function as builtin_function, the backend must
625   add all of the builtins at program initialization time.  */
626
627tree
628add_builtin_function_ext_scope (const char *name,
629				tree type,
630				int function_code,
631				enum built_in_class cl,
632				const char *library_name,
633				tree attrs)
634{
635  return add_builtin_function_common (name, type, function_code, cl,
636				      library_name, attrs,
637				      lang_hooks.builtin_function_ext_scope);
638}
639
640tree
641lhd_builtin_function (tree decl)
642{
643  lang_hooks.decls.pushdecl (decl);
644  return decl;
645}
646
647/* Create a builtin type.  */
648
649tree
650add_builtin_type (const char *name, tree type)
651{
652  tree   id = get_identifier (name);
653  tree decl = build_decl (BUILTINS_LOCATION, TYPE_DECL, id, type);
654  return lang_hooks.decls.pushdecl (decl);
655}
656
657/* LTO hooks.  */
658
659/* Used to save and restore any previously active section.  */
660static section *saved_section;
661
662
663/* Begin a new LTO output section named NAME.  This default implementation
664   saves the old section and emits assembly code to switch to the new
665   section.  */
666
667void
668lhd_begin_section (const char *name)
669{
670  section *section;
671
672  /* Save the old section so we can restore it in lto_end_asm_section.  */
673  gcc_assert (!saved_section);
674  saved_section = in_section;
675  if (!saved_section)
676    saved_section = text_section;
677
678  /* Create a new section and switch to it.  */
679  section = get_section (name, SECTION_DEBUG | SECTION_EXCLUDE, NULL);
680  switch_to_section (section);
681}
682
683
684/* Write DATA of length LEN to the current LTO output section.  This default
685   implementation just calls assemble_string.  */
686
687void
688lhd_append_data (const void *data, size_t len, void *)
689{
690  if (data)
691    assemble_string ((const char *)data, len);
692}
693
694
695/* Finish the current LTO output section.  This default implementation emits
696   assembly code to switch to any section previously saved by
697   lhd_begin_section.  */
698
699void
700lhd_end_section (void)
701{
702  if (saved_section)
703    {
704      switch_to_section (saved_section);
705      saved_section = NULL;
706    }
707}
708
709/* Default implementation of enum_underlying_base_type using type_for_size.  */
710
711tree
712lhd_enum_underlying_base_type (const_tree enum_type)
713{
714  return lang_hooks.types.type_for_size (TYPE_PRECISION (enum_type),
715					 TYPE_UNSIGNED (enum_type));
716}
717
718/* Returns true if the current lang_hooks represents the GNU C frontend.  */
719
720bool
721lang_GNU_C (void)
722{
723  return (strncmp (lang_hooks.name, "GNU C", 5) == 0
724	  && (lang_hooks.name[5] == '\0' || ISDIGIT (lang_hooks.name[5])));
725}
726
727/* Returns true if the current lang_hooks represents the GNU C++ frontend.  */
728
729bool
730lang_GNU_CXX (void)
731{
732  return strncmp (lang_hooks.name, "GNU C++", 7) == 0;
733}
734
735/* Returns true if the current lang_hooks represents the GNU Fortran frontend.  */
736
737bool
738lang_GNU_Fortran (void)
739{
740  return strncmp (lang_hooks.name, "GNU Fortran", 11) == 0;
741}
742