1*38fd1498Szrj /* Some code common to C and ObjC front ends.
2*38fd1498Szrj    Copyright (C) 2001-2018 Free Software Foundation, Inc.
3*38fd1498Szrj 
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj 
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj 
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj 
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
19*38fd1498Szrj 
20*38fd1498Szrj #include "config.h"
21*38fd1498Szrj #include "system.h"
22*38fd1498Szrj #include "coretypes.h"
23*38fd1498Szrj #include "c-tree.h"
24*38fd1498Szrj #include "intl.h"
25*38fd1498Szrj #include "c-family/c-pretty-print.h"
26*38fd1498Szrj #include "tree-pretty-print.h"
27*38fd1498Szrj #include "gimple-pretty-print.h"
28*38fd1498Szrj #include "langhooks.h"
29*38fd1498Szrj #include "c-objc-common.h"
30*38fd1498Szrj 
31*38fd1498Szrj static bool c_tree_printer (pretty_printer *, text_info *, const char *,
32*38fd1498Szrj 			    int, bool, bool, bool, bool *, const char **);
33*38fd1498Szrj 
34*38fd1498Szrj bool
c_missing_noreturn_ok_p(tree decl)35*38fd1498Szrj c_missing_noreturn_ok_p (tree decl)
36*38fd1498Szrj {
37*38fd1498Szrj   /* A missing noreturn is not ok for freestanding implementations and
38*38fd1498Szrj      ok for the `main' function in hosted implementations.  */
39*38fd1498Szrj   return flag_hosted && MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl));
40*38fd1498Szrj }
41*38fd1498Szrj 
42*38fd1498Szrj /* Called from check_global_declaration.  */
43*38fd1498Szrj 
44*38fd1498Szrj bool
c_warn_unused_global_decl(const_tree decl)45*38fd1498Szrj c_warn_unused_global_decl (const_tree decl)
46*38fd1498Szrj {
47*38fd1498Szrj   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
48*38fd1498Szrj     return false;
49*38fd1498Szrj   if (DECL_IN_SYSTEM_HEADER (decl))
50*38fd1498Szrj     return false;
51*38fd1498Szrj 
52*38fd1498Szrj   return true;
53*38fd1498Szrj }
54*38fd1498Szrj 
55*38fd1498Szrj /* Initialization common to C and Objective-C front ends.  */
56*38fd1498Szrj bool
c_objc_common_init(void)57*38fd1498Szrj c_objc_common_init (void)
58*38fd1498Szrj {
59*38fd1498Szrj   c_init_decl_processing ();
60*38fd1498Szrj 
61*38fd1498Szrj   return c_common_init ();
62*38fd1498Szrj }
63*38fd1498Szrj 
64*38fd1498Szrj /* Called during diagnostic message formatting process to print a
65*38fd1498Szrj    source-level entity onto BUFFER.  The meaning of the format specifiers
66*38fd1498Szrj    is as follows:
67*38fd1498Szrj    %D: a general decl,
68*38fd1498Szrj    %E: an identifier or expression,
69*38fd1498Szrj    %F: a function declaration,
70*38fd1498Szrj    %G: a Gimple call statement,
71*38fd1498Szrj    %K: a CALL_EXPR,
72*38fd1498Szrj    %T: a type.
73*38fd1498Szrj    %V: a list of type qualifiers from a tree.
74*38fd1498Szrj    %v: an explicit list of type qualifiers
75*38fd1498Szrj    %#v: an explicit list of type qualifiers of a function type.
76*38fd1498Szrj 
77*38fd1498Szrj    Please notice when called, the `%' part was already skipped by the
78*38fd1498Szrj    diagnostic machinery.  */
79*38fd1498Szrj static bool
c_tree_printer(pretty_printer * pp,text_info * text,const char * spec,int precision,bool wide,bool set_locus,bool hash,bool * quoted,const char **)80*38fd1498Szrj c_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
81*38fd1498Szrj 		int precision, bool wide, bool set_locus, bool hash,
82*38fd1498Szrj 		bool *quoted, const char **)
83*38fd1498Szrj {
84*38fd1498Szrj   tree t = NULL_TREE;
85*38fd1498Szrj   tree name;
86*38fd1498Szrj   // FIXME: the next cast should be a dynamic_cast, when it is permitted.
87*38fd1498Szrj   c_pretty_printer *cpp = (c_pretty_printer *) pp;
88*38fd1498Szrj   pp->padding = pp_none;
89*38fd1498Szrj 
90*38fd1498Szrj   if (precision != 0 || wide)
91*38fd1498Szrj     return false;
92*38fd1498Szrj 
93*38fd1498Szrj   if (*spec == 'G')
94*38fd1498Szrj     {
95*38fd1498Szrj       percent_G_format (text);
96*38fd1498Szrj       return true;
97*38fd1498Szrj     }
98*38fd1498Szrj 
99*38fd1498Szrj   if (*spec == 'K')
100*38fd1498Szrj     {
101*38fd1498Szrj       t = va_arg (*text->args_ptr, tree);
102*38fd1498Szrj       percent_K_format (text, t);
103*38fd1498Szrj       return true;
104*38fd1498Szrj     }
105*38fd1498Szrj 
106*38fd1498Szrj   if (*spec != 'v')
107*38fd1498Szrj     {
108*38fd1498Szrj       t = va_arg (*text->args_ptr, tree);
109*38fd1498Szrj       if (set_locus)
110*38fd1498Szrj 	text->set_location (0, DECL_SOURCE_LOCATION (t), true);
111*38fd1498Szrj     }
112*38fd1498Szrj 
113*38fd1498Szrj   switch (*spec)
114*38fd1498Szrj     {
115*38fd1498Szrj     case 'D':
116*38fd1498Szrj       if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t))
117*38fd1498Szrj 	{
118*38fd1498Szrj 	  t = DECL_DEBUG_EXPR (t);
119*38fd1498Szrj 	  if (!DECL_P (t))
120*38fd1498Szrj 	    {
121*38fd1498Szrj 	      cpp->expression (t);
122*38fd1498Szrj 	      return true;
123*38fd1498Szrj 	    }
124*38fd1498Szrj 	}
125*38fd1498Szrj       /* FALLTHRU */
126*38fd1498Szrj 
127*38fd1498Szrj     case 'F':
128*38fd1498Szrj       if (DECL_NAME (t))
129*38fd1498Szrj 	{
130*38fd1498Szrj 	  pp_identifier (cpp, lang_hooks.decl_printable_name (t, 2));
131*38fd1498Szrj 	  return true;
132*38fd1498Szrj 	}
133*38fd1498Szrj       break;
134*38fd1498Szrj 
135*38fd1498Szrj     case 'T':
136*38fd1498Szrj       {
137*38fd1498Szrj 	gcc_assert (TYPE_P (t));
138*38fd1498Szrj 	struct obstack *ob = pp_buffer (cpp)->obstack;
139*38fd1498Szrj 	char *p = (char *) obstack_base (ob);
140*38fd1498Szrj 	/* Remember the end of the initial dump.  */
141*38fd1498Szrj 	int len = obstack_object_size (ob);
142*38fd1498Szrj 
143*38fd1498Szrj 	name = TYPE_NAME (t);
144*38fd1498Szrj 	if (name && TREE_CODE (name) == TYPE_DECL && DECL_NAME (name))
145*38fd1498Szrj 	  pp_identifier (cpp, lang_hooks.decl_printable_name (name, 2));
146*38fd1498Szrj 	else
147*38fd1498Szrj 	  cpp->type_id (t);
148*38fd1498Szrj 
149*38fd1498Szrj 	/* If we're printing a type that involves typedefs, also print the
150*38fd1498Szrj 	   stripped version.  But sometimes the stripped version looks
151*38fd1498Szrj 	   exactly the same, so we don't want it after all.  To avoid
152*38fd1498Szrj 	   printing it in that case, we play ugly obstack games.  */
153*38fd1498Szrj 	if (TYPE_CANONICAL (t) && t != TYPE_CANONICAL (t))
154*38fd1498Szrj 	  {
155*38fd1498Szrj 	    c_pretty_printer cpp2;
156*38fd1498Szrj 	    /* Print the stripped version into a temporary printer.  */
157*38fd1498Szrj 	    cpp2.type_id (TYPE_CANONICAL (t));
158*38fd1498Szrj 	    struct obstack *ob2 = cpp2.buffer->obstack;
159*38fd1498Szrj 	    /* Get the stripped version from the temporary printer.  */
160*38fd1498Szrj 	    const char *aka = (char *) obstack_base (ob2);
161*38fd1498Szrj 	    int aka_len = obstack_object_size (ob2);
162*38fd1498Szrj 	    int type1_len = obstack_object_size (ob) - len;
163*38fd1498Szrj 
164*38fd1498Szrj 	    /* If they are identical, bail out.  */
165*38fd1498Szrj 	    if (aka_len == type1_len && memcmp (p + len, aka, aka_len) == 0)
166*38fd1498Szrj 	      return true;
167*38fd1498Szrj 
168*38fd1498Szrj 	    /* They're not, print the stripped version now.  */
169*38fd1498Szrj 	    if (*quoted)
170*38fd1498Szrj 	      pp_end_quote (pp, pp_show_color (pp));
171*38fd1498Szrj 	    pp_c_whitespace (cpp);
172*38fd1498Szrj 	    pp_left_brace (cpp);
173*38fd1498Szrj 	    pp_c_ws_string (cpp, _("aka"));
174*38fd1498Szrj 	    pp_c_whitespace (cpp);
175*38fd1498Szrj 	    if (*quoted)
176*38fd1498Szrj 	      pp_begin_quote (pp, pp_show_color (pp));
177*38fd1498Szrj 	    cpp->type_id (TYPE_CANONICAL (t));
178*38fd1498Szrj 	    if (*quoted)
179*38fd1498Szrj 	      pp_end_quote (pp, pp_show_color (pp));
180*38fd1498Szrj 	    pp_right_brace (cpp);
181*38fd1498Szrj 	    /* No further closing quotes are needed.  */
182*38fd1498Szrj 	    *quoted = false;
183*38fd1498Szrj 	  }
184*38fd1498Szrj 	return true;
185*38fd1498Szrj       }
186*38fd1498Szrj 
187*38fd1498Szrj     case 'E':
188*38fd1498Szrj       if (TREE_CODE (t) == IDENTIFIER_NODE)
189*38fd1498Szrj 	pp_identifier (cpp, IDENTIFIER_POINTER (t));
190*38fd1498Szrj       else
191*38fd1498Szrj 	cpp->expression (t);
192*38fd1498Szrj       return true;
193*38fd1498Szrj 
194*38fd1498Szrj     case 'V':
195*38fd1498Szrj       pp_c_type_qualifier_list (cpp, t);
196*38fd1498Szrj       return true;
197*38fd1498Szrj 
198*38fd1498Szrj     case 'v':
199*38fd1498Szrj       pp_c_cv_qualifiers (cpp, va_arg (*text->args_ptr, int), hash);
200*38fd1498Szrj       return true;
201*38fd1498Szrj 
202*38fd1498Szrj     default:
203*38fd1498Szrj       return false;
204*38fd1498Szrj     }
205*38fd1498Szrj 
206*38fd1498Szrj   pp_string (cpp, _("({anonymous})"));
207*38fd1498Szrj   return true;
208*38fd1498Szrj }
209*38fd1498Szrj 
210*38fd1498Szrj /* In C and ObjC, all decls have "C" linkage.  */
211*38fd1498Szrj bool
has_c_linkage(const_tree decl ATTRIBUTE_UNUSED)212*38fd1498Szrj has_c_linkage (const_tree decl ATTRIBUTE_UNUSED)
213*38fd1498Szrj {
214*38fd1498Szrj   return true;
215*38fd1498Szrj }
216*38fd1498Szrj 
217*38fd1498Szrj void
c_initialize_diagnostics(diagnostic_context * context)218*38fd1498Szrj c_initialize_diagnostics (diagnostic_context *context)
219*38fd1498Szrj {
220*38fd1498Szrj   pretty_printer *base = context->printer;
221*38fd1498Szrj   c_pretty_printer *pp = XNEW (c_pretty_printer);
222*38fd1498Szrj   context->printer = new (pp) c_pretty_printer ();
223*38fd1498Szrj 
224*38fd1498Szrj   /* It is safe to free this object because it was previously XNEW()'d.  */
225*38fd1498Szrj   base->~pretty_printer ();
226*38fd1498Szrj   XDELETE (base);
227*38fd1498Szrj 
228*38fd1498Szrj   c_common_diagnostics_set_defaults (context);
229*38fd1498Szrj   diagnostic_format_decoder (context) = &c_tree_printer;
230*38fd1498Szrj }
231*38fd1498Szrj 
232*38fd1498Szrj int
c_types_compatible_p(tree x,tree y)233*38fd1498Szrj c_types_compatible_p (tree x, tree y)
234*38fd1498Szrj {
235*38fd1498Szrj   return comptypes (TYPE_MAIN_VARIANT (x), TYPE_MAIN_VARIANT (y));
236*38fd1498Szrj }
237*38fd1498Szrj 
238*38fd1498Szrj /* Determine if the type is a vla type for the backend.  */
239*38fd1498Szrj 
240*38fd1498Szrj bool
c_vla_unspec_p(tree x,tree fn ATTRIBUTE_UNUSED)241*38fd1498Szrj c_vla_unspec_p (tree x, tree fn ATTRIBUTE_UNUSED)
242*38fd1498Szrj {
243*38fd1498Szrj   return c_vla_type_p (x);
244*38fd1498Szrj }
245