Branch data Line data Source code
1 : : /* Some code common to C and ObjC front ends.
2 : : Copyright (C) 2001-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify it under
7 : : the terms of the GNU General Public License as published by the Free
8 : : Software Foundation; either version 3, or (at your option) any later
9 : : version.
10 : :
11 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : : for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along 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 "tree-pretty-print-markup.h"
28 : : #include "gimple-pretty-print.h"
29 : : #include "langhooks.h"
30 : : #include "c-objc-common.h"
31 : : #include "c-family/c-type-mismatch.h"
32 : : #include "stringpool.h"
33 : : #include "attribs.h"
34 : : #include "dwarf2.h"
35 : :
36 : : static bool c_tree_printer (pretty_printer *, text_info *, const char *,
37 : : int, bool, bool, bool, bool *, pp_token_list &);
38 : :
39 : : /* Info for C language features which can be queried through
40 : : __has_{feature,extension}. */
41 : :
42 : : struct c_feature_info
43 : : {
44 : : const char *ident;
45 : : const int *enable_flag;
46 : : };
47 : :
48 : : static const c_feature_info c_feature_table[] =
49 : : {
50 : : { "c_alignas", &flag_isoc11 },
51 : : { "c_alignof", &flag_isoc11 },
52 : : { "c_atomic", &flag_isoc11 },
53 : : { "c_generic_selections", &flag_isoc11 },
54 : : { "c_static_assert", &flag_isoc11 },
55 : : { "c_thread_local", &flag_isoc11 },
56 : : { "cxx_binary_literals", &flag_isoc23 }
57 : : };
58 : :
59 : : /* Register features specific to the C language. */
60 : :
61 : : void
62 : 24387 : c_register_features ()
63 : : {
64 : 195096 : for (unsigned i = 0; i < ARRAY_SIZE (c_feature_table); i++)
65 : : {
66 : 170709 : const c_feature_info *info = c_feature_table + i;
67 : 170709 : const bool feat_p = !info->enable_flag || *info->enable_flag;
68 : 170709 : c_common_register_feature (info->ident, feat_p);
69 : : }
70 : 24387 : }
71 : :
72 : : bool
73 : 12409 : c_missing_noreturn_ok_p (tree decl)
74 : : {
75 : : /* A missing noreturn is ok for the `main' function. */
76 : 12409 : if (!MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl)))
77 : : return false;
78 : :
79 : 6768 : return flag_hosted
80 : 6768 : || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl))) == integer_type_node;
81 : : }
82 : :
83 : : /* Called from check_global_declaration. */
84 : :
85 : : bool
86 : 14406 : c_warn_unused_global_decl (const_tree decl)
87 : : {
88 : 14406 : if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
89 : : return false;
90 : 519 : if (DECL_IN_SYSTEM_HEADER (decl))
91 : : return false;
92 : :
93 : : return true;
94 : : }
95 : :
96 : : /* Initialization common to C and Objective-C front ends. */
97 : : bool
98 : 110990 : c_objc_common_init (void)
99 : : {
100 : 110990 : c_init_decl_processing ();
101 : :
102 : 110990 : return c_common_init ();
103 : : }
104 : :
105 : : /* Decide whether it's worth saying that TYPE is also known as some other
106 : : type. Return the other type if so, otherwise return TYPE. */
107 : :
108 : : static tree
109 : 84166 : get_aka_type (tree type)
110 : : {
111 : 84166 : if (type == error_mark_node)
112 : : return type;
113 : :
114 : 84151 : tree result;
115 : 84151 : if (typedef_variant_p (type))
116 : : {
117 : : /* Saying that "foo" is also known as "struct foo" or
118 : : "struct <anonymous>" is unlikely to be useful, since users of
119 : : structure-like types would already know that they're structures.
120 : : The same applies to unions and enums; in general, printing the
121 : : tag is only useful if it has a different name. */
122 : 6931 : tree orig_type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
123 : 6931 : tree_code code = TREE_CODE (orig_type);
124 : 6931 : tree orig_id = TYPE_IDENTIFIER (orig_type);
125 : 6931 : if ((code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
126 : 6931 : && (!orig_id || TYPE_IDENTIFIER (type) == orig_id))
127 : : return type;
128 : :
129 : 6514 : if (!user_facing_original_type_p (type))
130 : : return type;
131 : :
132 : 6401 : result = get_aka_type (orig_type);
133 : : }
134 : 77220 : else if (TREE_CODE (type) == ENUMERAL_TYPE)
135 : : return type;
136 : : else
137 : : {
138 : 76919 : tree canonical = TYPE_CANONICAL (type);
139 : 76919 : if (canonical && TREE_CODE (type) != TREE_CODE (canonical))
140 : : return canonical;
141 : :
142 : : /* Recursive calls might choose a middle ground between TYPE
143 : : (which has no typedefs stripped) and CANONICAL (which has
144 : : all typedefs stripped). So try to reuse TYPE or CANONICAL if
145 : : convenient, but be prepared to create a new type if necessary. */
146 : 76919 : switch (TREE_CODE (type))
147 : : {
148 : 6236 : case POINTER_TYPE:
149 : 6236 : case REFERENCE_TYPE:
150 : 6236 : {
151 : 6236 : tree target_type = get_aka_type (TREE_TYPE (type));
152 : :
153 : 6236 : if (target_type == TREE_TYPE (type))
154 : : return type;
155 : :
156 : 656 : if (canonical && target_type == TREE_TYPE (canonical))
157 : : return canonical;
158 : :
159 : 200 : result = (TREE_CODE (type) == POINTER_TYPE
160 : 200 : ? build_pointer_type (target_type)
161 : 0 : : build_reference_type (target_type));
162 : : break;
163 : : }
164 : :
165 : 22682 : case ARRAY_TYPE:
166 : 22682 : {
167 : 22682 : tree element_type = get_aka_type (TREE_TYPE (type));
168 : 22682 : tree index_type = (TYPE_DOMAIN (type)
169 : 22682 : ? get_aka_type (TYPE_DOMAIN (type))
170 : : : NULL_TREE);
171 : :
172 : 22682 : if (element_type == TREE_TYPE (type)
173 : 38713 : && index_type == TYPE_DOMAIN (type))
174 : : return type;
175 : :
176 : 6651 : if (canonical
177 : 6524 : && element_type == TREE_TYPE (canonical)
178 : 13164 : && index_type == TYPE_DOMAIN (canonical))
179 : : return canonical;
180 : :
181 : 276 : result = build_array_type (element_type, index_type,
182 : 138 : TYPE_TYPELESS_STORAGE (type));
183 : 138 : break;
184 : : }
185 : :
186 : 1986 : case FUNCTION_TYPE:
187 : 1986 : {
188 : 1986 : tree return_type = get_aka_type (TREE_TYPE (type));
189 : :
190 : 1986 : tree args = TYPE_ARG_TYPES (type);
191 : 1986 : if (args == error_mark_node)
192 : 1751 : return type;
193 : :
194 : 1986 : auto_vec<tree, 32> arg_types;
195 : 1986 : bool type_ok_p = true;
196 : 3766 : while (args && args != void_list_node)
197 : : {
198 : 1780 : tree arg_type = get_aka_type (TREE_VALUE (args));
199 : 1780 : arg_types.safe_push (arg_type);
200 : 1780 : type_ok_p &= (arg_type == TREE_VALUE (args));
201 : 1780 : args = TREE_CHAIN (args);
202 : : }
203 : :
204 : 1986 : if (type_ok_p && return_type == TREE_TYPE (type))
205 : 1751 : return type;
206 : :
207 : 235 : unsigned int i;
208 : 235 : tree arg_type;
209 : 880 : FOR_EACH_VEC_ELT_REVERSE (arg_types, i, arg_type)
210 : 410 : args = tree_cons (NULL_TREE, arg_type, args);
211 : 235 : result = build_function_type (return_type, args);
212 : 235 : break;
213 : 1986 : }
214 : :
215 : 46015 : default:
216 : 55492 : return canonical ? canonical : type;
217 : : }
218 : : }
219 : : /* For tagged types ignore attributes because they will otherwise
220 : : be ignored later causing a warning inside diagnostics which leads
221 : : to an ICE. */
222 : 6974 : if (RECORD_OR_UNION_TYPE_P (type) || TREE_CODE (type) == ENUMERAL_TYPE)
223 : 315 : return build_qualified_type (result, TYPE_QUALS (type));
224 : 6659 : return build_type_attribute_qual_variant (result, TYPE_ATTRIBUTES (type),
225 : 13318 : TYPE_QUALS (type));
226 : : }
227 : :
228 : : /* Print T to CPP. */
229 : :
230 : : static void
231 : 23386 : print_type (c_pretty_printer *cpp, tree t, bool *quoted,
232 : : const char *highlight_color = nullptr)
233 : : {
234 : 23386 : if (t == error_mark_node)
235 : : {
236 : 0 : pp_string (cpp, _("{erroneous}"));
237 : 0 : return;
238 : : }
239 : :
240 : 23386 : if (!pp_show_highlight_colors (cpp))
241 : 415 : highlight_color = nullptr;
242 : :
243 : 23386 : gcc_assert (TYPE_P (t));
244 : 23386 : struct obstack *ob = pp_buffer (cpp)->m_obstack;
245 : 23386 : char *p = (char *) obstack_base (ob);
246 : : /* Remember the end of the initial dump. */
247 : 23386 : int len = obstack_object_size (ob);
248 : :
249 : 23386 : tree name = TYPE_NAME (t);
250 : 23386 : if (name && TREE_CODE (name) == TYPE_DECL && DECL_NAME (name))
251 : 6970 : pp_identifier (cpp, lang_hooks.decl_printable_name (name, 2));
252 : : else
253 : 16416 : cpp->type_id (t);
254 : :
255 : : /* If we're printing a type that involves typedefs, also print the
256 : : stripped version. But sometimes the stripped version looks
257 : : exactly the same, so we don't want it after all. To avoid
258 : : printing it in that case, we play ugly obstack games. */
259 : 23386 : tree aka_type = get_aka_type (t);
260 : 23386 : if (aka_type != t)
261 : : {
262 : 6010 : const bool show_color = pp_show_color (cpp);
263 : 6010 : c_pretty_printer cpp2;
264 : : /* Print the stripped version into a temporary printer. */
265 : 6010 : cpp2.type_id (aka_type);
266 : 6010 : struct obstack *ob2 = pp_buffer (&cpp2)->m_obstack;
267 : : /* Get the stripped version from the temporary printer. */
268 : 6010 : const char *aka = (char *) obstack_base (ob2);
269 : 6010 : int aka_len = obstack_object_size (ob2);
270 : 6010 : int type1_len = obstack_object_size (ob) - len;
271 : :
272 : : /* If they are identical, bail out. */
273 : 6010 : if (aka_len == type1_len && memcmp (p + len, aka, aka_len) == 0)
274 : 181 : return;
275 : :
276 : : /* They're not, print the stripped version now. */
277 : 5829 : if (*quoted)
278 : 5629 : pp_end_quote (cpp, show_color);
279 : 5829 : pp_c_whitespace (cpp);
280 : 5829 : pp_left_brace (cpp);
281 : 5829 : pp_c_ws_string (cpp, _("aka"));
282 : 5829 : pp_c_whitespace (cpp);
283 : 5829 : pp_string (cpp, colorize_stop (show_color));
284 : 5829 : if (*quoted)
285 : 5629 : pp_begin_quote (cpp, show_color);
286 : 5829 : if (highlight_color)
287 : 343 : pp_string (cpp, colorize_start (show_color, highlight_color));
288 : 5829 : cpp->type_id (aka_type);
289 : 5829 : if (*quoted)
290 : 5629 : pp_end_quote (cpp, show_color);
291 : 5829 : pp_right_brace (cpp);
292 : : /* No further closing quotes are needed. */
293 : 5829 : *quoted = false;
294 : 6010 : }
295 : : }
296 : :
297 : : /* Implementation of pp_markup::element_quoted_type::print_type
298 : : for C/ObjC. */
299 : :
300 : : void
301 : 5407 : pp_markup::element_quoted_type::print_type (pp_markup::context &ctxt)
302 : : {
303 : 5407 : auto pp = ctxt.m_pp.clone ();
304 : 5407 : c_pretty_printer *cpp = (c_pretty_printer *)pp.get ();
305 : 5407 : cpp->set_padding (pp_none);
306 : 5407 : ::print_type (cpp, m_type, &ctxt.m_quoted, m_highlight_color);
307 : 5407 : pp_string (&ctxt.m_pp, pp_formatted_text (cpp));
308 : 5407 : }
309 : :
310 : : /* Called during diagnostic message formatting process to print a
311 : : source-level entity onto BUFFER. The meaning of the format specifiers
312 : : is as follows:
313 : : %D: a general decl,
314 : : %E: an identifier or expression,
315 : : %F: a function declaration,
316 : : %T: a type.
317 : : %V: a list of type qualifiers from a tree.
318 : : %v: an explicit list of type qualifiers
319 : : %#v: an explicit list of type qualifiers of a function type.
320 : :
321 : : Please notice when called, the `%' part was already skipped by the
322 : : diagnostic machinery. */
323 : : static bool
324 : 64670 : c_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
325 : : int precision, bool wide, bool set_locus, bool hash,
326 : : bool *quoted, pp_token_list &)
327 : : {
328 : 64670 : tree t = NULL_TREE;
329 : : // FIXME: the next cast should be a dynamic_cast, when it is permitted.
330 : 64670 : c_pretty_printer *cpp = (c_pretty_printer *) pp;
331 : 64670 : pp->set_padding (pp_none);
332 : :
333 : 64670 : if (precision != 0 || wide)
334 : : return false;
335 : :
336 : 64670 : if (*spec != 'v')
337 : : {
338 : 64465 : t = va_arg (*text->m_args_ptr, tree);
339 : 64465 : if (set_locus)
340 : 1542 : text->set_location (0, DECL_SOURCE_LOCATION (t),
341 : : SHOW_RANGE_WITH_CARET);
342 : : }
343 : :
344 : 64670 : switch (*spec)
345 : : {
346 : 15023 : case 'D':
347 : 15023 : if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t))
348 : : {
349 : 40 : t = DECL_DEBUG_EXPR (t);
350 : 40 : if (!DECL_P (t))
351 : : {
352 : 40 : cpp->expression (t);
353 : 40 : return true;
354 : : }
355 : : }
356 : : /* FALLTHRU */
357 : :
358 : 15048 : case 'F':
359 : 15048 : if (DECL_NAME (t))
360 : : {
361 : 14533 : pp_identifier (cpp, lang_hooks.decl_printable_name (t, 2));
362 : 14533 : return true;
363 : : }
364 : 515 : break;
365 : :
366 : 17566 : case 'T':
367 : 17566 : print_type (cpp, t, quoted);
368 : 17566 : return true;
369 : :
370 : 31811 : case 'E':
371 : 31811 : if (TREE_CODE (t) == IDENTIFIER_NODE)
372 : 5496 : pp_identifier (cpp, IDENTIFIER_POINTER (t));
373 : : else
374 : 26315 : cpp->expression (t);
375 : : return true;
376 : :
377 : 0 : case 'V':
378 : 0 : pp_c_type_qualifier_list (cpp, t);
379 : 0 : return true;
380 : :
381 : 205 : case 'v':
382 : 205 : pp_c_cv_qualifiers (cpp, va_arg (*text->m_args_ptr, int), hash);
383 : 205 : return true;
384 : :
385 : : default:
386 : : return false;
387 : : }
388 : :
389 : 515 : pp_string (cpp, _("({anonymous})"));
390 : 515 : return true;
391 : : }
392 : :
393 : : /* C-specific implementation of range_label::get_text () vfunc for
394 : : range_label_for_type_mismatch. */
395 : :
396 : : label_text
397 : 413 : range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
398 : : {
399 : 413 : if (m_labelled_type == NULL_TREE)
400 : 0 : return label_text::borrow (NULL);
401 : :
402 : 413 : c_pretty_printer cpp;
403 : 413 : bool quoted = false;
404 : 413 : print_type (&cpp, m_labelled_type, "ed);
405 : 413 : return label_text::take (xstrdup (pp_formatted_text (&cpp)));
406 : 413 : }
407 : :
408 : :
409 : : /* In C and ObjC, all decls have "C" linkage. */
410 : : bool
411 : 15033991 : has_c_linkage (const_tree decl ATTRIBUTE_UNUSED)
412 : : {
413 : 15033991 : return true;
414 : : }
415 : :
416 : : void
417 : 111196 : c_initialize_diagnostics (diagnostic_context *context)
418 : : {
419 : 111196 : context->set_pretty_printer (std::make_unique<c_pretty_printer> ());
420 : 111196 : c_common_diagnostics_set_defaults (context);
421 : 111196 : context->set_format_decoder (&c_tree_printer);
422 : 111196 : }
423 : :
424 : : int
425 : 15240598 : c_types_compatible_p (tree x, tree y)
426 : : {
427 : 15240598 : return comptypes (TYPE_MAIN_VARIANT (x), TYPE_MAIN_VARIANT (y));
428 : : }
429 : :
430 : : /* Determine if the type is a variably modified type for the backend. */
431 : :
432 : : bool
433 : 11031138 : c_var_mod_p (tree x, tree fn ATTRIBUTE_UNUSED)
434 : : {
435 : 11031138 : return C_TYPE_VARIABLY_MODIFIED (x);
436 : : }
437 : :
438 : : /* Special routine to get the alias set of T for C. */
439 : :
440 : : alias_set_type
441 : 212610877 : c_get_alias_set (tree t)
442 : : {
443 : 212610877 : return c_common_get_alias_set (t);
444 : : }
445 : :
446 : : /* In C there are no invisible parameters like in C++ (this, in-charge, VTT,
447 : : etc.). */
448 : :
449 : : int
450 : 39932416 : maybe_adjust_arg_pos_for_attribute (const_tree)
451 : : {
452 : 39932416 : return 0;
453 : : }
454 : :
455 : : /* In C, no expression is dependent. */
456 : :
457 : : bool
458 : 35 : instantiation_dependent_expression_p (tree)
459 : : {
460 : 35 : return false;
461 : : }
462 : :
463 : : /* Return -1 if dwarf ATTR shouldn't be added for TYPE, or the attribute
464 : : value otherwise. */
465 : : int
466 : 121028 : c_type_dwarf_attribute (const_tree type, int attr)
467 : : {
468 : 121028 : if (type == NULL_TREE)
469 : : return -1;
470 : :
471 : 121028 : switch (attr)
472 : : {
473 : 6062 : case DW_AT_export_symbols:
474 : 6062 : if (RECORD_OR_UNION_TYPE_P (type) && TYPE_NAME (type) == NULL_TREE)
475 : 6062 : return 1;
476 : : break;
477 : :
478 : : default:
479 : : break;
480 : : }
481 : :
482 : : return -1;
483 : : }
|