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-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 : 24595 : c_register_features ()
64 : : {
65 : 196760 : for (unsigned i = 0; i < ARRAY_SIZE (c_feature_table); i++)
66 : : {
67 : 172165 : const c_feature_info *info = c_feature_table + i;
68 : 172165 : const bool feat_p = !info->enable_flag || *info->enable_flag;
69 : 172165 : c_common_register_feature (info->ident, feat_p);
70 : : }
71 : 24595 : }
72 : :
73 : : bool
74 : 12535 : c_missing_noreturn_ok_p (tree decl)
75 : : {
76 : : /* A missing noreturn is ok for the `main' function. */
77 : 12535 : if (!MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl)))
78 : : return false;
79 : :
80 : 6795 : return flag_hosted
81 : 6795 : || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl))) == integer_type_node;
82 : : }
83 : :
84 : : /* Called from check_global_declaration. */
85 : :
86 : : bool
87 : 14406 : c_warn_unused_global_decl (const_tree decl)
88 : : {
89 : 14406 : if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
90 : : return false;
91 : 519 : if (DECL_IN_SYSTEM_HEADER (decl))
92 : : return false;
93 : :
94 : : return true;
95 : : }
96 : :
97 : : /* Initialization common to C and Objective-C front ends. */
98 : : bool
99 : 113067 : c_objc_common_init (void)
100 : : {
101 : 113067 : c_init_decl_processing ();
102 : :
103 : 113067 : 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 : 84464 : get_aka_type (tree type)
111 : : {
112 : 84464 : if (type == error_mark_node)
113 : : return type;
114 : :
115 : 84449 : tree result;
116 : 84449 : 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 : 6999 : tree orig_type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
124 : 6999 : tree_code code = TREE_CODE (orig_type);
125 : 6999 : tree orig_id = TYPE_IDENTIFIER (orig_type);
126 : 6999 : if ((code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
127 : 6999 : && (!orig_id || TYPE_IDENTIFIER (type) == orig_id))
128 : : return type;
129 : :
130 : 6581 : if (!user_facing_original_type_p (type))
131 : : return type;
132 : :
133 : 6468 : result = get_aka_type (orig_type);
134 : : }
135 : 77450 : else if (TREE_CODE (type) == ENUMERAL_TYPE)
136 : : return type;
137 : : else
138 : : {
139 : 77149 : tree canonical = TYPE_CANONICAL (type);
140 : 77149 : 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 : 77149 : switch (TREE_CODE (type))
148 : : {
149 : 6256 : case POINTER_TYPE:
150 : 6256 : case REFERENCE_TYPE:
151 : 6256 : {
152 : 6256 : tree target_type = get_aka_type (TREE_TYPE (type));
153 : :
154 : 6256 : 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 : 22692 : case ARRAY_TYPE:
167 : 22692 : {
168 : 22692 : tree element_type = get_aka_type (TREE_TYPE (type));
169 : 22692 : tree index_type = (TYPE_DOMAIN (type)
170 : 22692 : ? get_aka_type (TYPE_DOMAIN (type))
171 : : : NULL_TREE);
172 : :
173 : 22692 : if (element_type == TREE_TYPE (type)
174 : 38733 : && index_type == TYPE_DOMAIN (type))
175 : : return type;
176 : :
177 : 6651 : if (canonical
178 : 6524 : && element_type == TREE_TYPE (canonical)
179 : 13164 : && index_type == TYPE_DOMAIN (canonical))
180 : : return canonical;
181 : :
182 : 276 : result = build_array_type (element_type, index_type,
183 : 138 : TYPE_TYPELESS_STORAGE (type));
184 : 138 : 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 : 3779 : while (args && args != void_list_node)
198 : : {
199 : 1784 : tree arg_type = get_aka_type (TREE_VALUE (args));
200 : 1784 : arg_types.safe_push (arg_type);
201 : 1784 : type_ok_p &= (arg_type == TREE_VALUE (args));
202 : 1784 : 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 : 46206 : default:
217 : 55705 : 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 : 7041 : if (RECORD_OR_UNION_TYPE_P (type) || TREE_CODE (type) == ENUMERAL_TYPE)
224 : 315 : return build_qualified_type (result, TYPE_QUALS (type));
225 : 6726 : return build_type_attribute_qual_variant (result, TYPE_ATTRIBUTES (type),
226 : 13452 : TYPE_QUALS (type));
227 : : }
228 : :
229 : : /* Print T to CPP. */
230 : :
231 : : static void
232 : 23579 : print_type (c_pretty_printer *cpp, tree t, bool *quoted,
233 : : const char *highlight_color = nullptr)
234 : : {
235 : 23579 : if (t == error_mark_node)
236 : : {
237 : 0 : pp_string (cpp, _("{erroneous}"));
238 : 0 : return;
239 : : }
240 : :
241 : 23579 : if (!pp_show_highlight_colors (cpp))
242 : 447 : highlight_color = nullptr;
243 : :
244 : 23579 : gcc_assert (TYPE_P (t));
245 : 23579 : struct obstack *ob = pp_buffer (cpp)->m_obstack;
246 : 23579 : char *p = (char *) obstack_base (ob);
247 : : /* Remember the end of the initial dump. */
248 : 23579 : int len = obstack_object_size (ob);
249 : :
250 : 23579 : tree name = TYPE_NAME (t);
251 : 23579 : if (name && TREE_CODE (name) == TYPE_DECL && DECL_NAME (name))
252 : 7103 : pp_identifier (cpp, lang_hooks.decl_printable_name (name, 2));
253 : : else
254 : 16476 : 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 : 23579 : tree aka_type = get_aka_type (t);
261 : 23579 : if (aka_type != t)
262 : : {
263 : 6045 : const bool show_color = pp_show_color (cpp);
264 : 6045 : c_pretty_printer cpp2;
265 : : /* Print the stripped version into a temporary printer. */
266 : 6045 : cpp2.type_id (aka_type);
267 : 6045 : struct obstack *ob2 = pp_buffer (&cpp2)->m_obstack;
268 : : /* Get the stripped version from the temporary printer. */
269 : 6045 : const char *aka = (char *) obstack_base (ob2);
270 : 6045 : int aka_len = obstack_object_size (ob2);
271 : 6045 : int type1_len = obstack_object_size (ob) - len;
272 : :
273 : : /* If they are identical, bail out. */
274 : 6045 : 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 : 5863 : if (*quoted)
279 : 5647 : pp_end_quote (cpp, show_color);
280 : 5863 : pp_c_whitespace (cpp);
281 : 5863 : pp_left_brace (cpp);
282 : 5863 : pp_c_ws_string (cpp, _("aka"));
283 : 5863 : pp_c_whitespace (cpp);
284 : 5863 : pp_string (cpp, colorize_stop (show_color));
285 : 5863 : if (*quoted)
286 : 5647 : pp_begin_quote (cpp, show_color);
287 : 5863 : if (highlight_color)
288 : 361 : pp_string (cpp, colorize_start (show_color, highlight_color));
289 : 5863 : cpp->type_id (aka_type);
290 : 5863 : if (*quoted)
291 : 5647 : pp_end_quote (cpp, show_color);
292 : 5863 : pp_right_brace (cpp);
293 : : /* No further closing quotes are needed. */
294 : 5863 : *quoted = false;
295 : 6045 : }
296 : : }
297 : :
298 : : /* Implementation of pp_markup::element_quoted_type::print_type
299 : : for C/ObjC. */
300 : :
301 : : void
302 : 5443 : pp_markup::element_quoted_type::print_type (pp_markup::context &ctxt)
303 : : {
304 : 5443 : auto pp = ctxt.m_pp.clone ();
305 : 5443 : c_pretty_printer *cpp = (c_pretty_printer *)pp.get ();
306 : 5443 : cpp->set_padding (pp_none);
307 : 5443 : ::print_type (cpp, m_type, &ctxt.m_quoted, m_highlight_color);
308 : 5443 : pp_string (&ctxt.m_pp, pp_formatted_text (cpp));
309 : 5443 : }
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 : 65514 : 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 : 65514 : tree t = NULL_TREE;
330 : : // FIXME: the next cast should be a dynamic_cast, when it is permitted.
331 : 65514 : c_pretty_printer *cpp = (c_pretty_printer *) pp;
332 : 65514 : pp->set_padding (pp_none);
333 : :
334 : 65514 : if (precision != 0 || wide)
335 : : return false;
336 : :
337 : 65514 : if (*spec != 'v')
338 : : {
339 : 65309 : t = va_arg (*text->m_args_ptr, tree);
340 : 65309 : if (set_locus)
341 : 1611 : text->set_location (0, DECL_SOURCE_LOCATION (t),
342 : : SHOW_RANGE_WITH_CARET);
343 : : }
344 : :
345 : 65514 : switch (*spec)
346 : : {
347 : 15305 : case 'D':
348 : 15305 : if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t))
349 : : {
350 : 40 : t = DECL_DEBUG_EXPR (t);
351 : 40 : if (!DECL_P (t))
352 : : {
353 : 40 : cpp->expression (t);
354 : 40 : return true;
355 : : }
356 : : }
357 : : /* FALLTHRU */
358 : :
359 : 15340 : case 'F':
360 : 15340 : if (DECL_NAME (t))
361 : : {
362 : 14825 : pp_identifier (cpp, lang_hooks.decl_printable_name (t, 2));
363 : 14825 : if (TREE_CODE (t) == FUNCTION_DECL)
364 : : {
365 : 8039 : pp_c_function_target_version (cpp, t);
366 : 8039 : pp_c_function_target_clones (cpp, t);
367 : : }
368 : 14825 : return true;
369 : : }
370 : 515 : break;
371 : :
372 : 17691 : case 'T':
373 : 17691 : print_type (cpp, t, quoted);
374 : 17691 : return true;
375 : :
376 : 32238 : case 'E':
377 : 32238 : if (TREE_CODE (t) == IDENTIFIER_NODE)
378 : 5594 : pp_identifier (cpp, IDENTIFIER_POINTER (t));
379 : : else
380 : 26644 : 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 : 205 : pp_c_cv_qualifiers (cpp, va_arg (*text->m_args_ptr, int), hash);
389 : 205 : return true;
390 : :
391 : : default:
392 : : return false;
393 : : }
394 : :
395 : 515 : pp_string (cpp, _("({anonymous})"));
396 : 515 : return true;
397 : : }
398 : :
399 : : /* C-specific implementation of range_label::get_text () vfunc for
400 : : range_label_for_type_mismatch. */
401 : :
402 : : label_text
403 : 445 : range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
404 : : {
405 : 445 : if (m_labelled_type == NULL_TREE)
406 : 0 : return label_text::borrow (NULL);
407 : :
408 : 445 : c_pretty_printer cpp;
409 : 445 : bool quoted = false;
410 : 445 : print_type (&cpp, m_labelled_type, "ed);
411 : 445 : return label_text::take (xstrdup (pp_formatted_text (&cpp)));
412 : 445 : }
413 : :
414 : :
415 : : /* In C and ObjC, all decls have "C" linkage. */
416 : : bool
417 : 15607986 : has_c_linkage (const_tree decl ATTRIBUTE_UNUSED)
418 : : {
419 : 15607986 : return true;
420 : : }
421 : :
422 : : void
423 : 113291 : c_initialize_diagnostics (diagnostics::context *context)
424 : : {
425 : 113291 : context->set_pretty_printer (std::make_unique<c_pretty_printer> ());
426 : 113291 : c_common_diagnostics_set_defaults (context);
427 : 113291 : context->set_format_decoder (&c_tree_printer);
428 : 113291 : }
429 : :
430 : : int
431 : 15250516 : c_types_compatible_p (tree x, tree y)
432 : : {
433 : 15250516 : return comptypes (TYPE_MAIN_VARIANT (x), TYPE_MAIN_VARIANT (y));
434 : : }
435 : :
436 : : /* Determine if the type is a variably modified type for the backend. */
437 : :
438 : : bool
439 : 11029047 : c_var_mod_p (tree x, tree fn ATTRIBUTE_UNUSED)
440 : : {
441 : 11029047 : return C_TYPE_VARIABLY_MODIFIED (x);
442 : : }
443 : :
444 : : /* Special routine to get the alias set of T for C. */
445 : :
446 : : alias_set_type
447 : 216711161 : c_get_alias_set (tree t)
448 : : {
449 : 216711161 : return c_common_get_alias_set (t);
450 : : }
451 : :
452 : : /* In C there are no invisible parameters like in C++ (this, in-charge, VTT,
453 : : etc.). */
454 : :
455 : : int
456 : 42352930 : maybe_adjust_arg_pos_for_attribute (const_tree)
457 : : {
458 : 42352930 : return 0;
459 : : }
460 : :
461 : : /* In C, no expression is dependent. */
462 : :
463 : : bool
464 : 35 : instantiation_dependent_expression_p (tree)
465 : : {
466 : 35 : return false;
467 : : }
468 : :
469 : : /* Return -1 if dwarf ATTR shouldn't be added for TYPE, or the attribute
470 : : value otherwise. */
471 : : int
472 : 121121 : c_type_dwarf_attribute (const_tree type, int attr)
473 : : {
474 : 121121 : if (type == NULL_TREE)
475 : : return -1;
476 : :
477 : 121121 : switch (attr)
478 : : {
479 : 6069 : case DW_AT_export_symbols:
480 : 6069 : if (RECORD_OR_UNION_TYPE_P (type) && TYPE_NAME (type) == NULL_TREE)
481 : 6069 : return 1;
482 : : break;
483 : :
484 : : default:
485 : : break;
486 : : }
487 : :
488 : : return -1;
489 : : }
|