1/* Some code common to C and ObjC front ends.
2 Copyright (C) 2001-2023 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
23#include "c-tree.h"
24#include "intl.h"
25#include "c-family/c-pretty-print.h"
26#include "tree-pretty-print.h"
27#include "gimple-pretty-print.h"
28#include "langhooks.h"
29#include "c-objc-common.h"
30#include "gcc-rich-location.h"
31#include "stringpool.h"
32#include "attribs.h"
33
34static bool c_tree_printer (pretty_printer *, text_info *, const char *,
35 int, bool, bool, bool, bool *, const char **);
36
37bool
38c_missing_noreturn_ok_p (tree decl)
39{
40 /* A missing noreturn is ok for the `main' function. */
41 if (!MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl)))
42 return false;
43
44 return flag_hosted
45 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl))) == integer_type_node;
46}
47
48/* Called from check_global_declaration. */
49
50bool
51c_warn_unused_global_decl (const_tree decl)
52{
53 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
54 return false;
55 if (DECL_IN_SYSTEM_HEADER (decl))
56 return false;
57
58 return true;
59}
60
61/* Initialization common to C and Objective-C front ends. */
62bool
63c_objc_common_init (void)
64{
65 c_init_decl_processing ();
66
67 return c_common_init ();
68}
69
70/* Decide whether it's worth saying that TYPE is also known as some other
71 type. Return the other type if so, otherwise return TYPE. */
72
73static tree
74get_aka_type (tree type)
75{
76 if (type == error_mark_node)
77 return type;
78
79 tree result;
80 if (typedef_variant_p (type))
81 {
82 /* Saying that "foo" is also known as "struct foo" or
83 "struct <anonymous>" is unlikely to be useful, since users of
84 structure-like types would already know that they're structures.
85 The same applies to unions and enums; in general, printing the
86 tag is only useful if it has a different name. */
87 tree orig_type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
88 tree_code code = TREE_CODE (orig_type);
89 tree orig_id = TYPE_IDENTIFIER (orig_type);
90 if ((code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
91 && (!orig_id || TYPE_IDENTIFIER (type) == orig_id))
92 return type;
93
94 if (!user_facing_original_type_p (type))
95 return type;
96
97 result = get_aka_type (type: orig_type);
98 }
99 else
100 {
101 tree canonical = TYPE_CANONICAL (type);
102 if (canonical && TREE_CODE (type) != TREE_CODE (canonical))
103 return canonical;
104
105 /* Recursive calls might choose a middle ground between TYPE
106 (which has no typedefs stripped) and CANONICAL (which has
107 all typedefs stripped). So try to reuse TYPE or CANONICAL if
108 convenient, but be prepared to create a new type if necessary. */
109 switch (TREE_CODE (type))
110 {
111 case POINTER_TYPE:
112 case REFERENCE_TYPE:
113 {
114 tree target_type = get_aka_type (TREE_TYPE (type));
115
116 if (target_type == TREE_TYPE (type))
117 return type;
118
119 if (canonical && target_type == TREE_TYPE (canonical))
120 return canonical;
121
122 result = (TREE_CODE (type) == POINTER_TYPE
123 ? build_pointer_type (target_type)
124 : build_reference_type (target_type));
125 break;
126 }
127
128 case ARRAY_TYPE:
129 {
130 tree element_type = get_aka_type (TREE_TYPE (type));
131 tree index_type = (TYPE_DOMAIN (type)
132 ? get_aka_type (TYPE_DOMAIN (type))
133 : NULL_TREE);
134
135 if (element_type == TREE_TYPE (type)
136 && index_type == TYPE_DOMAIN (type))
137 return type;
138
139 if (canonical
140 && element_type == TREE_TYPE (canonical)
141 && index_type == TYPE_DOMAIN (canonical))
142 return canonical;
143
144 result = build_array_type (element_type, index_type,
145 TYPE_TYPELESS_STORAGE (type));
146 break;
147 }
148
149 case FUNCTION_TYPE:
150 {
151 tree return_type = get_aka_type (TREE_TYPE (type));
152
153 tree args = TYPE_ARG_TYPES (type);
154 if (args == error_mark_node)
155 return type;
156
157 auto_vec<tree, 32> arg_types;
158 bool type_ok_p = true;
159 while (args && args != void_list_node)
160 {
161 tree arg_type = get_aka_type (TREE_VALUE (args));
162 arg_types.safe_push (obj: arg_type);
163 type_ok_p &= (arg_type == TREE_VALUE (args));
164 args = TREE_CHAIN (args);
165 }
166
167 if (type_ok_p && return_type == TREE_TYPE (type))
168 return type;
169
170 unsigned int i;
171 tree arg_type;
172 FOR_EACH_VEC_ELT_REVERSE (arg_types, i, arg_type)
173 args = tree_cons (NULL_TREE, arg_type, args);
174 result = build_function_type (return_type, args);
175 break;
176 }
177
178 default:
179 return canonical ? canonical : type;
180 }
181 }
182 return build_type_attribute_qual_variant (result, TYPE_ATTRIBUTES (type),
183 TYPE_QUALS (type));
184}
185
186/* Print T to CPP. */
187
188static void
189print_type (c_pretty_printer *cpp, tree t, bool *quoted)
190{
191 if (t == error_mark_node)
192 {
193 pp_string (cpp, _("{erroneous}"));
194 return;
195 }
196
197 gcc_assert (TYPE_P (t));
198 struct obstack *ob = pp_buffer (cpp)->obstack;
199 char *p = (char *) obstack_base (ob);
200 /* Remember the end of the initial dump. */
201 int len = obstack_object_size (ob);
202
203 tree name = TYPE_NAME (t);
204 if (name && TREE_CODE (name) == TYPE_DECL && DECL_NAME (name))
205 pp_identifier (cpp, lang_hooks.decl_printable_name (name, 2));
206 else
207 cpp->type_id (t);
208
209 /* If we're printing a type that involves typedefs, also print the
210 stripped version. But sometimes the stripped version looks
211 exactly the same, so we don't want it after all. To avoid
212 printing it in that case, we play ugly obstack games. */
213 tree aka_type = get_aka_type (type: t);
214 if (aka_type != t)
215 {
216 c_pretty_printer cpp2;
217 /* Print the stripped version into a temporary printer. */
218 cpp2.type_id (aka_type);
219 struct obstack *ob2 = cpp2.buffer->obstack;
220 /* Get the stripped version from the temporary printer. */
221 const char *aka = (char *) obstack_base (ob2);
222 int aka_len = obstack_object_size (ob2);
223 int type1_len = obstack_object_size (ob) - len;
224
225 /* If they are identical, bail out. */
226 if (aka_len == type1_len && memcmp (s1: p + len, s2: aka, n: aka_len) == 0)
227 return;
228
229 /* They're not, print the stripped version now. */
230 if (*quoted)
231 pp_end_quote (cpp, pp_show_color (cpp));
232 pp_c_whitespace (cpp);
233 pp_left_brace (cpp);
234 pp_c_ws_string (cpp, _("aka"));
235 pp_c_whitespace (cpp);
236 if (*quoted)
237 pp_begin_quote (cpp, pp_show_color (cpp));
238 cpp->type_id (aka_type);
239 if (*quoted)
240 pp_end_quote (cpp, pp_show_color (cpp));
241 pp_right_brace (cpp);
242 /* No further closing quotes are needed. */
243 *quoted = false;
244 }
245}
246
247/* Called during diagnostic message formatting process to print a
248 source-level entity onto BUFFER. The meaning of the format specifiers
249 is as follows:
250 %D: a general decl,
251 %E: an identifier or expression,
252 %F: a function declaration,
253 %T: a type.
254 %V: a list of type qualifiers from a tree.
255 %v: an explicit list of type qualifiers
256 %#v: an explicit list of type qualifiers of a function type.
257
258 Please notice when called, the `%' part was already skipped by the
259 diagnostic machinery. */
260static bool
261c_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
262 int precision, bool wide, bool set_locus, bool hash,
263 bool *quoted, const char **)
264{
265 tree t = NULL_TREE;
266 // FIXME: the next cast should be a dynamic_cast, when it is permitted.
267 c_pretty_printer *cpp = (c_pretty_printer *) pp;
268 pp->padding = pp_none;
269
270 if (precision != 0 || wide)
271 return false;
272
273 if (*spec != 'v')
274 {
275 t = va_arg (*text->m_args_ptr, tree);
276 if (set_locus)
277 text->set_location (idx: 0, DECL_SOURCE_LOCATION (t),
278 range_display_kind: SHOW_RANGE_WITH_CARET);
279 }
280
281 switch (*spec)
282 {
283 case 'D':
284 if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t))
285 {
286 t = DECL_DEBUG_EXPR (t);
287 if (!DECL_P (t))
288 {
289 cpp->expression (t);
290 return true;
291 }
292 }
293 /* FALLTHRU */
294
295 case 'F':
296 if (DECL_NAME (t))
297 {
298 pp_identifier (cpp, lang_hooks.decl_printable_name (t, 2));
299 return true;
300 }
301 break;
302
303 case 'T':
304 print_type (cpp, t, quoted);
305 return true;
306
307 case 'E':
308 if (TREE_CODE (t) == IDENTIFIER_NODE)
309 pp_identifier (cpp, IDENTIFIER_POINTER (t));
310 else
311 cpp->expression (t);
312 return true;
313
314 case 'V':
315 pp_c_type_qualifier_list (cpp, t);
316 return true;
317
318 case 'v':
319 pp_c_cv_qualifiers (pp: cpp, va_arg (*text->m_args_ptr, int), func_type: hash);
320 return true;
321
322 default:
323 return false;
324 }
325
326 pp_string (cpp, _("({anonymous})"));
327 return true;
328}
329
330/* C-specific implementation of range_label::get_text () vfunc for
331 range_label_for_type_mismatch. */
332
333label_text
334range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
335{
336 if (m_labelled_type == NULL_TREE)
337 return label_text::borrow (NULL);
338
339 c_pretty_printer cpp;
340 bool quoted = false;
341 print_type (cpp: &cpp, t: m_labelled_type, quoted: &quoted);
342 return label_text::take (buffer: xstrdup (pp_formatted_text (&cpp)));
343}
344
345
346/* In C and ObjC, all decls have "C" linkage. */
347bool
348has_c_linkage (const_tree decl ATTRIBUTE_UNUSED)
349{
350 return true;
351}
352
353void
354c_initialize_diagnostics (diagnostic_context *context)
355{
356 pretty_printer *base = context->printer;
357 c_pretty_printer *pp = XNEW (c_pretty_printer);
358 context->printer = new (pp) c_pretty_printer ();
359
360 /* It is safe to free this object because it was previously XNEW()'d. */
361 base->~pretty_printer ();
362 XDELETE (base);
363
364 c_common_diagnostics_set_defaults (context);
365 diagnostic_format_decoder (context) = &c_tree_printer;
366}
367
368int
369c_types_compatible_p (tree x, tree y)
370{
371 return comptypes (TYPE_MAIN_VARIANT (x), TYPE_MAIN_VARIANT (y));
372}
373
374/* Determine if the type is a variably modified type for the backend. */
375
376bool
377c_var_mod_p (tree x, tree fn ATTRIBUTE_UNUSED)
378{
379 return C_TYPE_VARIABLY_MODIFIED (x);
380}
381
382/* Special routine to get the alias set of T for C. */
383
384alias_set_type
385c_get_alias_set (tree t)
386{
387 /* Allow aliasing between enumeral types and the underlying
388 integer type. This is required since those are compatible types. */
389 if (TREE_CODE (t) == ENUMERAL_TYPE)
390 return get_alias_set (ENUM_UNDERLYING_TYPE (t));
391
392 return c_common_get_alias_set (t);
393}
394
395/* In C there are no invisible parameters like in C++ (this, in-charge, VTT,
396 etc.). */
397
398int
399maybe_adjust_arg_pos_for_attribute (const_tree)
400{
401 return 0;
402}
403
404/* In C, no expression is dependent. */
405
406bool
407instantiation_dependent_expression_p (tree)
408{
409 return false;
410}
411

source code of gcc/c/c-objc-common.cc