190075Sobrien/* Some code common to C and ObjC front ends.
2169689Skan   Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
390075Sobrien
490075SobrienThis file is part of GCC.
590075Sobrien
690075SobrienGCC is free software; you can redistribute it and/or modify it under
790075Sobrienthe terms of the GNU General Public License as published by the Free
890075SobrienSoftware Foundation; either version 2, or (at your option) any later
990075Sobrienversion.
1090075Sobrien
1190075SobrienGCC is distributed in the hope that it will be useful, but WITHOUT ANY
1290075SobrienWARRANTY; without even the implied warranty of MERCHANTABILITY or
1390075SobrienFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1490075Sobrienfor more details.
1590075Sobrien
1690075SobrienYou should have received a copy of the GNU General Public License
1790075Sobrienalong with GCC; see the file COPYING.  If not, write to the Free
18169689SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19169689Skan02110-1301, USA.  */
2090075Sobrien
2190075Sobrien#include "config.h"
2290075Sobrien#include "system.h"
23132718Skan#include "coretypes.h"
24132718Skan#include "tm.h"
2590075Sobrien#include "tree.h"
2690075Sobrien#include "rtl.h"
2790075Sobrien#include "insn-config.h"
2890075Sobrien#include "integrate.h"
2990075Sobrien#include "c-tree.h"
30169689Skan#include "c-pretty-print.h"
3190075Sobrien#include "function.h"
3290075Sobrien#include "flags.h"
3390075Sobrien#include "toplev.h"
3490075Sobrien#include "diagnostic.h"
3590075Sobrien#include "tree-inline.h"
3690075Sobrien#include "varray.h"
3790075Sobrien#include "ggc.h"
38117395Skan#include "langhooks.h"
39169689Skan#include "tree-mudflap.h"
40117395Skan#include "target.h"
41169689Skan#include "c-objc-common.h"
4290075Sobrien
43169689Skanstatic bool c_tree_printer (pretty_printer *, text_info *, const char *,
44169689Skan			    int, bool, bool, bool);
4590075Sobrien
46169689Skanbool
47132718Skanc_missing_noreturn_ok_p (tree decl)
4890075Sobrien{
4990075Sobrien  /* A missing noreturn is not ok for freestanding implementations and
5090075Sobrien     ok for the `main' function in hosted implementations.  */
5190075Sobrien  return flag_hosted && MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl));
5290075Sobrien}
5390075Sobrien
5490075Sobrien/* We want to inline `extern inline' functions even if this would
5590075Sobrien   violate inlining limits.  Some glibc and linux constructs depend on
5690075Sobrien   such functions always being inlined when optimizing.  */
5790075Sobrien
5890075Sobrienint
59132718Skanc_disregard_inline_limits (tree fn)
6090075Sobrien{
6196263Sobrien  if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)) != NULL)
6296263Sobrien    return 1;
6396263Sobrien
64132718Skan  return (!flag_really_no_inline && DECL_DECLARED_INLINE_P (fn)
65132718Skan	  && DECL_EXTERNAL (fn));
6690075Sobrien}
6790075Sobrien
6890075Sobrienint
69132718Skanc_cannot_inline_tree_fn (tree *fnp)
7090075Sobrien{
7190075Sobrien  tree fn = *fnp;
72132718Skan  bool do_warning = (warn_inline
73132718Skan		     && DECL_INLINE (fn)
74132718Skan		     && DECL_DECLARED_INLINE_P (fn)
75132718Skan		     && !DECL_IN_SYSTEM_HEADER (fn));
7690075Sobrien
7796263Sobrien  if (flag_really_no_inline
7896263Sobrien      && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)) == NULL)
79132718Skan    {
80132718Skan      if (do_warning)
81169689Skan	warning (OPT_Winline, "function %q+F can never be inlined because it "
82169689Skan		 "is suppressed using -fno-inline", fn);
83132718Skan      goto cannot_inline;
84132718Skan    }
8596263Sobrien
86132718Skan  /* Don't auto-inline anything that might not be bound within
87104752Skan     this unit of translation.  */
88169689Skan  if (!DECL_DECLARED_INLINE_P (fn) && !targetm.binds_local_p (fn))
89132718Skan    {
90132718Skan      if (do_warning)
91169689Skan	warning (OPT_Winline, "function %q+F can never be inlined because it "
92169689Skan		 "might not be bound within this unit of translation", fn);
93132718Skan      goto cannot_inline;
94132718Skan    }
95104752Skan
96169689Skan  if (!function_attribute_inlinable_p (fn))
97132718Skan    {
98132718Skan      if (do_warning)
99169689Skan	warning (OPT_Winline, "function %q+F can never be inlined because it "
100169689Skan		 "uses attributes conflicting with inlining", fn);
101132718Skan      goto cannot_inline;
102132718Skan    }
10390075Sobrien
10490075Sobrien  return 0;
105117395Skan
106117395Skan cannot_inline:
107117395Skan  DECL_UNINLINABLE (fn) = 1;
108117395Skan  return 1;
10990075Sobrien}
11090075Sobrien
111117395Skan/* Called from check_global_declarations.  */
112117395Skan
113117395Skanbool
114132718Skanc_warn_unused_global_decl (tree decl)
115117395Skan{
116117395Skan  if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
117117395Skan    return false;
118117395Skan  if (DECL_IN_SYSTEM_HEADER (decl))
119117395Skan    return false;
120117395Skan
121117395Skan  return true;
122117395Skan}
123117395Skan
12490075Sobrien/* Initialization common to C and Objective-C front ends.  */
125132718Skanbool
126132718Skanc_objc_common_init (void)
12790075Sobrien{
12890075Sobrien  c_init_decl_processing ();
12990075Sobrien
130132718Skan  if (c_common_init () == false)
131132718Skan    return false;
13290075Sobrien
13390075Sobrien  /* These were not defined in the Objective-C front end, but I'm
13490075Sobrien     putting them here anyway.  The diagnostic format decoder might
13590075Sobrien     want an enhanced ObjC implementation.  */
13690075Sobrien  diagnostic_format_decoder (global_dc) = &c_tree_printer;
13790075Sobrien
13890075Sobrien  /* If still unspecified, make it match -std=c99
13990075Sobrien     (allowing for -pedantic-errors).  */
14090075Sobrien  if (mesg_implicit_function_declaration < 0)
14190075Sobrien    {
14290075Sobrien      if (flag_isoc99)
14390075Sobrien	mesg_implicit_function_declaration = flag_pedantic_errors ? 2 : 1;
14490075Sobrien      else
14590075Sobrien	mesg_implicit_function_declaration = 0;
14690075Sobrien    }
14790075Sobrien
148132718Skan  return true;
14990075Sobrien}
15090075Sobrien
15190075Sobrien/* Called during diagnostic message formatting process to print a
15290075Sobrien   source-level entity onto BUFFER.  The meaning of the format specifiers
15390075Sobrien   is as follows:
15490075Sobrien   %D: a general decl,
155169689Skan   %E: an identifier or expression,
15690075Sobrien   %F: a function declaration,
15790075Sobrien   %T: a type.
15890075Sobrien
15990075Sobrien   These format specifiers form a subset of the format specifiers set used
16090075Sobrien   by the C++ front-end.
16190075Sobrien   Please notice when called, the `%' part was already skipped by the
16290075Sobrien   diagnostic machinery.  */
163117395Skanstatic bool
164169689Skanc_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
165169689Skan		int precision, bool wide, bool set_locus, bool hash)
16690075Sobrien{
167117395Skan  tree t = va_arg (*text->args_ptr, tree);
168169689Skan  tree name;
169132718Skan  const char *n = "({anonymous})";
170169689Skan  c_pretty_printer *cpp = (c_pretty_printer *) pp;
171169689Skan  pp->padding = pp_none;
17290075Sobrien
173169689Skan  if (precision != 0 || wide || hash)
174169689Skan    return false;
175169689Skan
176169689Skan  if (set_locus && text->locus)
177169689Skan    *text->locus = DECL_SOURCE_LOCATION (t);
178169689Skan
179169689Skan  switch (*spec)
18090075Sobrien    {
18190075Sobrien    case 'D':
182169689Skan      if (DECL_DEBUG_EXPR_IS_FROM (t) && DECL_DEBUG_EXPR (t))
183169689Skan	{
184169689Skan	  t = DECL_DEBUG_EXPR (t);
185169689Skan	  if (!DECL_P (t))
186169689Skan	    {
187169689Skan	      pp_c_expression (cpp, t);
188169689Skan	      return true;
189169689Skan	    }
190169689Skan	}
191169689Skan      /* FALLTHRU */
192169689Skan
19390075Sobrien    case 'F':
194132718Skan      if (DECL_NAME (t))
195169689Skan	n = lang_hooks.decl_printable_name (t, 2);
196132718Skan      break;
197132718Skan
19890075Sobrien    case 'T':
199169689Skan      gcc_assert (TYPE_P (t));
200169689Skan      name = TYPE_NAME (t);
201169689Skan
202169689Skan      if (name && TREE_CODE (name) == TYPE_DECL)
203132718Skan	{
204169689Skan	  if (DECL_NAME (name))
205169689Skan	    pp_string (cpp, lang_hooks.decl_printable_name (name, 2));
206169689Skan	  else
207169689Skan	    pp_type_id (cpp, t);
208169689Skan	  return true;
209132718Skan	}
210132718Skan      else
211132718Skan	{
212169689Skan	  pp_type_id (cpp, t);
213169689Skan	  return true;
214132718Skan	}
215132718Skan      break;
21690075Sobrien
217132718Skan    case 'E':
218132718Skan      if (TREE_CODE (t) == IDENTIFIER_NODE)
219132718Skan	n = IDENTIFIER_POINTER (t);
220132718Skan      else
221169689Skan	{
222169689Skan	  pp_expression (cpp, t);
223169689Skan	  return true;
224169689Skan	}
225132718Skan      break;
226132718Skan
22790075Sobrien    default:
228117395Skan      return false;
22990075Sobrien    }
230132718Skan
231169689Skan  pp_string (cpp, n);
232132718Skan  return true;
23390075Sobrien}
234169689Skan
235169689Skan/* In C and ObjC, all decls have "C" linkage.  */
236169689Skanbool
237169689Skanhas_c_linkage (tree decl ATTRIBUTE_UNUSED)
238169689Skan{
239169689Skan  return true;
240169689Skan}
241169689Skan
242169689Skanvoid
243169689Skanc_initialize_diagnostics (diagnostic_context *context)
244169689Skan{
245169689Skan  pretty_printer *base = context->printer;
246169689Skan  c_pretty_printer *pp = XNEW (c_pretty_printer);
247169689Skan  memcpy (pp_base (pp), base, sizeof (pretty_printer));
248169689Skan  pp_c_pretty_printer_init (pp);
249169689Skan  context->printer = (pretty_printer *) pp;
250169689Skan
251169689Skan  /* It is safe to free this object because it was previously XNEW()'d.  */
252169689Skan  XDELETE (base);
253169689Skan}
254169689Skan
255169689Skanint
256169689Skanc_types_compatible_p (tree x, tree y)
257169689Skan{
258169689Skan    return comptypes (TYPE_MAIN_VARIANT (x), TYPE_MAIN_VARIANT (y));
259169689Skan}
260169689Skan
261169689Skan/* Determine if the type is a vla type for the backend.  */
262169689Skan
263169689Skanbool
264169689Skanc_vla_unspec_p (tree x, tree fn ATTRIBUTE_UNUSED)
265169689Skan{
266169689Skan  return c_vla_type_p (x);
267169689Skan}
268