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