| 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 |
#include "make-unique.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 |
c_register_features () |
| 64 |
{ |
| 65 |
for (unsigned i = 0; i < ARRAY_SIZE (c_feature_table); i++) |
| 66 |
{ |
| 67 |
const c_feature_info *info = c_feature_table + i; |
| 68 |
const bool feat_p = !info->enable_flag || *info->enable_flag; |
| 69 |
c_common_register_feature (info->ident, feat_p); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
bool |
| 74 |
c_missing_noreturn_ok_p (tree decl) |
| 75 |
{ |
| 76 |
/* A missing noreturn is ok for the `main' function. */ |
| 77 |
if (!MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl))) |
| 78 |
return false; |
| 79 |
|
| 80 |
return flag_hosted |
| 81 |
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl))) == integer_type_node; |
| 82 |
} |
| 83 |
|
| 84 |
/* Called from check_global_declaration. */ |
| 85 |
|
| 86 |
bool |
| 87 |
c_warn_unused_global_decl (const_tree decl) |
| 88 |
{ |
| 89 |
if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)) |
| 90 |
return false; |
| 91 |
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 |
c_objc_common_init (void) |
| 100 |
{ |
| 101 |
c_init_decl_processing (); |
| 102 |
|
| 103 |
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 |
get_aka_type (tree type) |
| 111 |
{ |
| 112 |
if (type == error_mark_node) |
| 113 |
return type; |
| 114 |
|
| 115 |
tree result; |
| 116 |
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 |
tree orig_type = DECL_ORIGINAL_TYPE (TYPE_NAME (type)); |
| 124 |
tree_code code = TREE_CODE (orig_type); |
| 125 |
tree orig_id = TYPE_IDENTIFIER (orig_type); |
| 126 |
if ((code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE) |
| 127 |
&& (!orig_id || TYPE_IDENTIFIER (type) == orig_id)) |
| 128 |
return type; |
| 129 |
|
| 130 |
if (!user_facing_original_type_p (type)) |
| 131 |
return type; |
| 132 |
|
| 133 |
result = get_aka_type (orig_type); |
| 134 |
} |
| 135 |
else if (TREE_CODE (type) == ENUMERAL_TYPE) |
| 136 |
return type; |
| 137 |
else |
| 138 |
{ |
| 139 |
tree canonical = TYPE_CANONICAL (type); |
| 140 |
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 |
switch (TREE_CODE (type)) |
| 148 |
{ |
| 149 |
case POINTER_TYPE: |
| 150 |
case REFERENCE_TYPE: |
| 151 |
{ |
| 152 |
tree target_type = get_aka_type (TREE_TYPE (type)); |
| 153 |
|
| 154 |
if (target_type == TREE_TYPE (type)) |
| 155 |
return type; |
| 156 |
|
| 157 |
if (canonical && target_type == TREE_TYPE (canonical)) |
| 158 |
return canonical; |
| 159 |
|
| 160 |
result = (TREE_CODE (type) == POINTER_TYPE |
| 161 |
? build_pointer_type (target_type) |
| 162 |
: build_reference_type (target_type)); |
| 163 |
break; |
| 164 |
} |
| 165 |
|
| 166 |
case ARRAY_TYPE: |
| 167 |
{ |
| 168 |
tree element_type = get_aka_type (TREE_TYPE (type)); |
| 169 |
tree index_type = (TYPE_DOMAIN (type) |
| 170 |
? get_aka_type (TYPE_DOMAIN (type)) |
| 171 |
: NULL_TREE); |
| 172 |
|
| 173 |
if (element_type == TREE_TYPE (type) |
| 174 |
&& index_type == TYPE_DOMAIN (type)) |
| 175 |
return type; |
| 176 |
|
| 177 |
if (canonical |
| 178 |
&& element_type == TREE_TYPE (canonical) |
| 179 |
&& index_type == TYPE_DOMAIN (canonical)) |
| 180 |
return canonical; |
| 181 |
|
| 182 |
result = build_array_type (element_type, index_type, |
| 183 |
TYPE_TYPELESS_STORAGE (type)); |
| 184 |
break; |
| 185 |
} |
| 186 |
|
| 187 |
case FUNCTION_TYPE: |
| 188 |
{ |
| 189 |
tree return_type = get_aka_type (TREE_TYPE (type)); |
| 190 |
|
| 191 |
tree args = TYPE_ARG_TYPES (type); |
| 192 |
if (args == error_mark_node) |
| 193 |
return type; |
| 194 |
|
| 195 |
auto_vec<tree, 32> arg_types; |
| 196 |
bool type_ok_p = true; |
| 197 |
while (args && args != void_list_node) |
| 198 |
{ |
| 199 |
tree arg_type = get_aka_type (TREE_VALUE (args)); |
| 200 |
arg_types.safe_push (arg_type); |
| 201 |
type_ok_p &= (arg_type == TREE_VALUE (args)); |
| 202 |
args = TREE_CHAIN (args); |
| 203 |
} |
| 204 |
|
| 205 |
if (type_ok_p && return_type == TREE_TYPE (type)) |
| 206 |
return type; |
| 207 |
|
| 208 |
unsigned int i; |
| 209 |
tree arg_type; |
| 210 |
FOR_EACH_VEC_ELT_REVERSE (arg_types, i, arg_type) |
| 211 |
args = tree_cons (NULL_TREE, arg_type, args); |
| 212 |
result = build_function_type (return_type, args); |
| 213 |
break; |
| 214 |
} |
| 215 |
|
| 216 |
default: |
| 217 |
return canonical ? canonical : type; |
| 218 |
} |
| 219 |
} |
| 220 |
return build_type_attribute_qual_variant (result, TYPE_ATTRIBUTES (type), |
| 221 |
TYPE_QUALS (type)); |
| 222 |
} |
| 223 |
|
| 224 |
/* Print T to CPP. */ |
| 225 |
|
| 226 |
static void |
| 227 |
print_type (c_pretty_printer *cpp, tree t, bool *quoted, |
| 228 |
const char *highlight_color = nullptr) |
| 229 |
{ |
| 230 |
if (t == error_mark_node) |
| 231 |
{ |
| 232 |
pp_string (cpp, _("{erroneous}")); |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
if (!pp_show_highlight_colors (cpp)) |
| 237 |
highlight_color = nullptr; |
| 238 |
|
| 239 |
gcc_assert (TYPE_P (t)); |
| 240 |
struct obstack *ob = pp_buffer (cpp)->m_obstack; |
| 241 |
char *p = (char *) obstack_base (ob); |
| 242 |
/* Remember the end of the initial dump. */ |
| 243 |
int len = obstack_object_size (ob); |
| 244 |
|
| 245 |
tree name = TYPE_NAME (t); |
| 246 |
if (name && TREE_CODE (name) == TYPE_DECL && DECL_NAME (name)) |
| 247 |
pp_identifier (cpp, lang_hooks.decl_printable_name (name, 2)); |
| 248 |
else |
| 249 |
cpp->type_id (t); |
| 250 |
|
| 251 |
/* If we're printing a type that involves typedefs, also print the |
| 252 |
stripped version. But sometimes the stripped version looks |
| 253 |
exactly the same, so we don't want it after all. To avoid |
| 254 |
printing it in that case, we play ugly obstack games. */ |
| 255 |
tree aka_type = get_aka_type (t); |
| 256 |
if (aka_type != t) |
| 257 |
{ |
| 258 |
const bool show_color = pp_show_color (cpp); |
| 259 |
c_pretty_printer cpp2; |
| 260 |
/* Print the stripped version into a temporary printer. */ |
| 261 |
cpp2.type_id (aka_type); |
| 262 |
struct obstack *ob2 = pp_buffer (&cpp2)->m_obstack; |
| 263 |
/* Get the stripped version from the temporary printer. */ |
| 264 |
const char *aka = (char *) obstack_base (ob2); |
| 265 |
int aka_len = obstack_object_size (ob2); |
| 266 |
int type1_len = obstack_object_size (ob) - len; |
| 267 |
|
| 268 |
/* If they are identical, bail out. */ |
| 269 |
if (aka_len == type1_len && memcmp (p + len, aka, aka_len) == 0) |
| 270 |
return; |
| 271 |
|
| 272 |
/* They're not, print the stripped version now. */ |
| 273 |
if (*quoted) |
| 274 |
pp_end_quote (cpp, show_color); |
| 275 |
pp_c_whitespace (cpp); |
| 276 |
pp_left_brace (cpp); |
| 277 |
pp_c_ws_string (cpp, _("aka")); |
| 278 |
pp_c_whitespace (cpp); |
| 279 |
pp_string (cpp, colorize_stop (show_color)); |
| 280 |
if (*quoted) |
| 281 |
pp_begin_quote (cpp, show_color); |
| 282 |
if (highlight_color) |
| 283 |
pp_string (cpp, colorize_start (show_color, highlight_color)); |
| 284 |
cpp->type_id (aka_type); |
| 285 |
if (*quoted) |
| 286 |
pp_end_quote (cpp, show_color); |
| 287 |
pp_right_brace (cpp); |
| 288 |
/* No further closing quotes are needed. */ |
| 289 |
*quoted = false; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
/* Implementation of pp_markup::element_quoted_type::print_type |
| 294 |
for C/ObjC. */ |
| 295 |
|
| 296 |
void |
| 297 |
pp_markup::element_quoted_type::print_type (pp_markup::context &ctxt) |
| 298 |
{ |
| 299 |
auto pp = ctxt.m_pp.clone (); |
| 300 |
c_pretty_printer *cpp = (c_pretty_printer *)pp.get (); |
| 301 |
cpp->set_padding (pp_none); |
| 302 |
::print_type (cpp, m_type, &ctxt.m_quoted, m_highlight_color); |
| 303 |
pp_string (&ctxt.m_pp, pp_formatted_text (cpp)); |
| 304 |
} |
| 305 |
|
| 306 |
/* Called during diagnostic message formatting process to print a |
| 307 |
source-level entity onto BUFFER. The meaning of the format specifiers |
| 308 |
is as follows: |
| 309 |
%D: a general decl, |
| 310 |
%E: an identifier or expression, |
| 311 |
%F: a function declaration, |
| 312 |
%T: a type. |
| 313 |
%V: a list of type qualifiers from a tree. |
| 314 |
%v: an explicit list of type qualifiers |
| 315 |
%#v: an explicit list of type qualifiers of a function type. |
| 316 |
|
| 317 |
Please notice when called, the `%' part was already skipped by the |
| 318 |
diagnostic machinery. */ |
| 319 |
static bool |
| 320 |
c_tree_printer (pretty_printer *pp, text_info *text, const char *spec, |
| 321 |
int precision, bool wide, bool set_locus, bool hash, |
| 322 |
bool *quoted, pp_token_list &) |
| 323 |
{ |
| 324 |
tree t = NULL_TREE; |
| 325 |
// FIXME: the next cast should be a dynamic_cast, when it is permitted. |
| 326 |
c_pretty_printer *cpp = (c_pretty_printer *) pp; |
| 327 |
pp->set_padding (pp_none); |
| 328 |
|
| 329 |
if (precision != 0 || wide) |
| 330 |
return false; |
| 331 |
|
| 332 |
if (*spec != 'v') |
| 333 |
{ |
| 334 |
t = va_arg (*text->m_args_ptr, tree); |
| 335 |
if (set_locus) |
| 336 |
text->set_location (0, DECL_SOURCE_LOCATION (t), |
| 337 |
SHOW_RANGE_WITH_CARET); |
| 338 |
} |
| 339 |
|
| 340 |
switch (*spec) |
| 341 |
{ |
| 342 |
case 'D': |
| 343 |
if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t)) |
| 344 |
{ |
| 345 |
t = DECL_DEBUG_EXPR (t); |
| 346 |
if (!DECL_P (t)) |
| 347 |
{ |
| 348 |
cpp->expression (t); |
| 349 |
return true; |
| 350 |
} |
| 351 |
} |
| 352 |
/* FALLTHRU */ |
| 353 |
|
| 354 |
case 'F': |
| 355 |
if (DECL_NAME (t)) |
| 356 |
{ |
| 357 |
pp_identifier (cpp, lang_hooks.decl_printable_name (t, 2)); |
| 358 |
return true; |
| 359 |
} |
| 360 |
break; |
| 361 |
|
| 362 |
case 'T': |
| 363 |
print_type (cpp, t, quoted); |
| 364 |
return true; |
| 365 |
|
| 366 |
case 'E': |
| 367 |
if (TREE_CODE (t) == IDENTIFIER_NODE) |
| 368 |
pp_identifier (cpp, IDENTIFIER_POINTER (t)); |
| 369 |
else |
| 370 |
cpp->expression (t); |
| 371 |
return true; |
| 372 |
|
| 373 |
case 'V': |
| 374 |
pp_c_type_qualifier_list (cpp, t); |
| 375 |
return true; |
| 376 |
|
| 377 |
case 'v': |
| 378 |
pp_c_cv_qualifiers (cpp, va_arg (*text->m_args_ptr, int), hash); |
| 379 |
return true; |
| 380 |
|
| 381 |
default: |
| 382 |
return false; |
| 383 |
} |
| 384 |
|
| 385 |
pp_string (cpp, _("({anonymous})")); |
| 386 |
return true; |
| 387 |
} |
| 388 |
|
| 389 |
/* C-specific implementation of range_label::get_text () vfunc for |
| 390 |
range_label_for_type_mismatch. */ |
| 391 |
|
| 392 |
label_text |
| 393 |
range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const |
| 394 |
{ |
| 395 |
if (m_labelled_type == NULL_TREE) |
| 396 |
return label_text::borrow (NULL); |
| 397 |
|
| 398 |
c_pretty_printer cpp; |
| 399 |
bool quoted = false; |
| 400 |
print_type (&cpp, m_labelled_type, "ed); |
| 401 |
return label_text::take (xstrdup (pp_formatted_text (&cpp))); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
/* In C and ObjC, all decls have "C" linkage. */ |
| 406 |
bool |
| 407 |
has_c_linkage (const_tree decl ATTRIBUTE_UNUSED) |
| 408 |
{ |
| 409 |
return true; |
| 410 |
} |
| 411 |
|
| 412 |
void |
| 413 |
c_initialize_diagnostics (diagnostic_context *context) |
| 414 |
{ |
| 415 |
context->set_pretty_printer (::make_unique<c_pretty_printer> ()); |
| 416 |
c_common_diagnostics_set_defaults (context); |
| 417 |
context->set_format_decoder (&c_tree_printer); |
| 418 |
} |
| 419 |
|
| 420 |
int |
| 421 |
c_types_compatible_p (tree x, tree y) |
| 422 |
{ |
| 423 |
return comptypes (TYPE_MAIN_VARIANT (x), TYPE_MAIN_VARIANT (y)); |
| 424 |
} |
| 425 |
|
| 426 |
/* Determine if the type is a variably modified type for the backend. */ |
| 427 |
|
| 428 |
bool |
| 429 |
c_var_mod_p (tree x, tree fn ATTRIBUTE_UNUSED) |
| 430 |
{ |
| 431 |
return C_TYPE_VARIABLY_MODIFIED (x); |
| 432 |
} |
| 433 |
|
| 434 |
/* Special routine to get the alias set of T for C. */ |
| 435 |
|
| 436 |
alias_set_type |
| 437 |
c_get_alias_set (tree t) |
| 438 |
{ |
| 439 |
return c_common_get_alias_set (t); |
| 440 |
} |
| 441 |
|
| 442 |
/* In C there are no invisible parameters like in C++ (this, in-charge, VTT, |
| 443 |
etc.). */ |
| 444 |
|
| 445 |
int |
| 446 |
maybe_adjust_arg_pos_for_attribute (const_tree) |
| 447 |
{ |
| 448 |
return 0; |
| 449 |
} |
| 450 |
|
| 451 |
/* In C, no expression is dependent. */ |
| 452 |
|
| 453 |
bool |
| 454 |
instantiation_dependent_expression_p (tree) |
| 455 |
{ |
| 456 |
return false; |
| 457 |
} |
| 458 |
|
| 459 |
/* Return -1 if dwarf ATTR shouldn't be added for TYPE, or the attribute |
| 460 |
value otherwise. */ |
| 461 |
int |
| 462 |
c_type_dwarf_attribute (const_tree type, int attr) |
| 463 |
{ |
| 464 |
if (type == NULL_TREE) |
| 465 |
return -1; |
| 466 |
|
| 467 |
switch (attr) |
| 468 |
{ |
| 469 |
case DW_AT_export_symbols: |
| 470 |
if (RECORD_OR_UNION_TYPE_P (type) && TYPE_NAME (type) == NULL_TREE) |
| 471 |
return 1; |
| 472 |
break; |
| 473 |
|
| 474 |
default: |
| 475 |
break; |
| 476 |
} |
| 477 |
|
| 478 |
return -1; |
| 479 |
} |