Branch data Line data Source code
1 : : /* Some code common to C and ObjC front ends.
2 : : Copyright (C) 2001-2024 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 "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 : : #include "dwarf2.h"
34 : :
35 : : static bool c_tree_printer (pretty_printer *, text_info *, const char *,
36 : : int, bool, bool, bool, bool *, const char **);
37 : :
38 : : /* Info for C language features which can be queried through
39 : : __has_{feature,extension}. */
40 : :
41 : : struct c_feature_info
42 : : {
43 : : const char *ident;
44 : : const int *enable_flag;
45 : : };
46 : :
47 : : static const c_feature_info c_feature_table[] =
48 : : {
49 : : { "c_alignas", &flag_isoc11 },
50 : : { "c_alignof", &flag_isoc11 },
51 : : { "c_atomic", &flag_isoc11 },
52 : : { "c_generic_selections", &flag_isoc11 },
53 : : { "c_static_assert", &flag_isoc11 },
54 : : { "c_thread_local", &flag_isoc11 },
55 : : { "cxx_binary_literals", &flag_isoc23 }
56 : : };
57 : :
58 : : /* Register features specific to the C language. */
59 : :
60 : : void
61 : 23397 : c_register_features ()
62 : : {
63 : 187176 : for (unsigned i = 0; i < ARRAY_SIZE (c_feature_table); i++)
64 : : {
65 : 163779 : const c_feature_info *info = c_feature_table + i;
66 : 163779 : const bool feat_p = !info->enable_flag || *info->enable_flag;
67 : 163779 : c_common_register_feature (info->ident, feat_p);
68 : : }
69 : 23397 : }
70 : :
71 : : bool
72 : 12134 : c_missing_noreturn_ok_p (tree decl)
73 : : {
74 : : /* A missing noreturn is ok for the `main' function. */
75 : 12134 : if (!MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl)))
76 : : return false;
77 : :
78 : 6703 : return flag_hosted
79 : 6703 : || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl))) == integer_type_node;
80 : : }
81 : :
82 : : /* Called from check_global_declaration. */
83 : :
84 : : bool
85 : 14405 : c_warn_unused_global_decl (const_tree decl)
86 : : {
87 : 14405 : if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
88 : : return false;
89 : 518 : if (DECL_IN_SYSTEM_HEADER (decl))
90 : : return false;
91 : :
92 : : return true;
93 : : }
94 : :
95 : : /* Initialization common to C and Objective-C front ends. */
96 : : bool
97 : 104748 : c_objc_common_init (void)
98 : : {
99 : 104748 : c_init_decl_processing ();
100 : :
101 : 104748 : return c_common_init ();
102 : : }
103 : :
104 : : /* Decide whether it's worth saying that TYPE is also known as some other
105 : : type. Return the other type if so, otherwise return TYPE. */
106 : :
107 : : static tree
108 : 83036 : get_aka_type (tree type)
109 : : {
110 : 83036 : if (type == error_mark_node)
111 : : return type;
112 : :
113 : 83021 : tree result;
114 : 83021 : if (typedef_variant_p (type))
115 : : {
116 : : /* Saying that "foo" is also known as "struct foo" or
117 : : "struct <anonymous>" is unlikely to be useful, since users of
118 : : structure-like types would already know that they're structures.
119 : : The same applies to unions and enums; in general, printing the
120 : : tag is only useful if it has a different name. */
121 : 6861 : tree orig_type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
122 : 6861 : tree_code code = TREE_CODE (orig_type);
123 : 6861 : tree orig_id = TYPE_IDENTIFIER (orig_type);
124 : 6861 : if ((code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
125 : 6861 : && (!orig_id || TYPE_IDENTIFIER (type) == orig_id))
126 : : return type;
127 : :
128 : 6446 : if (!user_facing_original_type_p (type))
129 : : return type;
130 : :
131 : 6333 : result = get_aka_type (orig_type);
132 : : }
133 : : else
134 : : {
135 : 76160 : tree canonical = TYPE_CANONICAL (type);
136 : 76160 : if (canonical && TREE_CODE (type) != TREE_CODE (canonical))
137 : : return canonical;
138 : :
139 : : /* Recursive calls might choose a middle ground between TYPE
140 : : (which has no typedefs stripped) and CANONICAL (which has
141 : : all typedefs stripped). So try to reuse TYPE or CANONICAL if
142 : : convenient, but be prepared to create a new type if necessary. */
143 : 76160 : switch (TREE_CODE (type))
144 : : {
145 : 6060 : case POINTER_TYPE:
146 : 6060 : case REFERENCE_TYPE:
147 : 6060 : {
148 : 6060 : tree target_type = get_aka_type (TREE_TYPE (type));
149 : :
150 : 6060 : if (target_type == TREE_TYPE (type))
151 : : return type;
152 : :
153 : 650 : if (canonical && target_type == TREE_TYPE (canonical))
154 : : return canonical;
155 : :
156 : 156 : result = (TREE_CODE (type) == POINTER_TYPE
157 : 156 : ? build_pointer_type (target_type)
158 : 0 : : build_reference_type (target_type));
159 : : break;
160 : : }
161 : :
162 : 22577 : case ARRAY_TYPE:
163 : 22577 : {
164 : 22577 : tree element_type = get_aka_type (TREE_TYPE (type));
165 : 22577 : tree index_type = (TYPE_DOMAIN (type)
166 : 22577 : ? get_aka_type (TYPE_DOMAIN (type))
167 : 22577 : : NULL_TREE);
168 : :
169 : 22577 : if (element_type == TREE_TYPE (type)
170 : 38504 : && index_type == TYPE_DOMAIN (type))
171 : : return type;
172 : :
173 : 6650 : if (canonical
174 : 6523 : && element_type == TREE_TYPE (canonical)
175 : 13164 : && index_type == TYPE_DOMAIN (canonical))
176 : : return canonical;
177 : :
178 : 272 : result = build_array_type (element_type, index_type,
179 : 136 : TYPE_TYPELESS_STORAGE (type));
180 : 136 : break;
181 : : }
182 : :
183 : 1920 : case FUNCTION_TYPE:
184 : 1920 : {
185 : 1920 : tree return_type = get_aka_type (TREE_TYPE (type));
186 : :
187 : 1920 : tree args = TYPE_ARG_TYPES (type);
188 : 1920 : if (args == error_mark_node)
189 : 1685 : return type;
190 : :
191 : 1920 : auto_vec<tree, 32> arg_types;
192 : 1920 : bool type_ok_p = true;
193 : 3617 : while (args && args != void_list_node)
194 : : {
195 : 1697 : tree arg_type = get_aka_type (TREE_VALUE (args));
196 : 1697 : arg_types.safe_push (arg_type);
197 : 1697 : type_ok_p &= (arg_type == TREE_VALUE (args));
198 : 1697 : args = TREE_CHAIN (args);
199 : : }
200 : :
201 : 1920 : if (type_ok_p && return_type == TREE_TYPE (type))
202 : 1685 : return type;
203 : :
204 : 235 : unsigned int i;
205 : 235 : tree arg_type;
206 : 880 : FOR_EACH_VEC_ELT_REVERSE (arg_types, i, arg_type)
207 : 410 : args = tree_cons (NULL_TREE, arg_type, args);
208 : 235 : result = build_function_type (return_type, args);
209 : 235 : break;
210 : 1920 : }
211 : :
212 : 45603 : default:
213 : 54877 : return canonical ? canonical : type;
214 : : }
215 : : }
216 : 6860 : return build_type_attribute_qual_variant (result, TYPE_ATTRIBUTES (type),
217 : 13720 : TYPE_QUALS (type));
218 : : }
219 : :
220 : : /* Print T to CPP. */
221 : :
222 : : static void
223 : 22803 : print_type (c_pretty_printer *cpp, tree t, bool *quoted)
224 : : {
225 : 22803 : if (t == error_mark_node)
226 : : {
227 : 0 : pp_string (cpp, _("{erroneous}"));
228 : 0 : return;
229 : : }
230 : :
231 : 22803 : gcc_assert (TYPE_P (t));
232 : 22803 : struct obstack *ob = pp_buffer (cpp)->obstack;
233 : 22803 : char *p = (char *) obstack_base (ob);
234 : : /* Remember the end of the initial dump. */
235 : 22803 : int len = obstack_object_size (ob);
236 : :
237 : 22803 : tree name = TYPE_NAME (t);
238 : 22803 : if (name && TREE_CODE (name) == TYPE_DECL && DECL_NAME (name))
239 : 6641 : pp_identifier (cpp, lang_hooks.decl_printable_name (name, 2));
240 : : else
241 : 16162 : cpp->type_id (t);
242 : :
243 : : /* If we're printing a type that involves typedefs, also print the
244 : : stripped version. But sometimes the stripped version looks
245 : : exactly the same, so we don't want it after all. To avoid
246 : : printing it in that case, we play ugly obstack games. */
247 : 22803 : tree aka_type = get_aka_type (t);
248 : 22803 : if (aka_type != t)
249 : : {
250 : 5951 : c_pretty_printer cpp2;
251 : : /* Print the stripped version into a temporary printer. */
252 : 5951 : cpp2.type_id (aka_type);
253 : 5951 : struct obstack *ob2 = cpp2.buffer->obstack;
254 : : /* Get the stripped version from the temporary printer. */
255 : 5951 : const char *aka = (char *) obstack_base (ob2);
256 : 5951 : int aka_len = obstack_object_size (ob2);
257 : 5951 : int type1_len = obstack_object_size (ob) - len;
258 : :
259 : : /* If they are identical, bail out. */
260 : 5951 : if (aka_len == type1_len && memcmp (p + len, aka, aka_len) == 0)
261 : 176 : return;
262 : :
263 : : /* They're not, print the stripped version now. */
264 : 5775 : if (*quoted)
265 : 5590 : pp_end_quote (cpp, pp_show_color (cpp));
266 : 5775 : pp_c_whitespace (cpp);
267 : 5775 : pp_left_brace (cpp);
268 : 5775 : pp_c_ws_string (cpp, _("aka"));
269 : 5775 : pp_c_whitespace (cpp);
270 : 5775 : if (*quoted)
271 : 5590 : pp_begin_quote (cpp, pp_show_color (cpp));
272 : 5775 : cpp->type_id (aka_type);
273 : 5775 : if (*quoted)
274 : 5590 : pp_end_quote (cpp, pp_show_color (cpp));
275 : 5775 : pp_right_brace (cpp);
276 : : /* No further closing quotes are needed. */
277 : 5775 : *quoted = false;
278 : 5951 : }
279 : : }
280 : :
281 : : /* Called during diagnostic message formatting process to print a
282 : : source-level entity onto BUFFER. The meaning of the format specifiers
283 : : is as follows:
284 : : %D: a general decl,
285 : : %E: an identifier or expression,
286 : : %F: a function declaration,
287 : : %T: a type.
288 : : %V: a list of type qualifiers from a tree.
289 : : %v: an explicit list of type qualifiers
290 : : %#v: an explicit list of type qualifiers of a function type.
291 : :
292 : : Please notice when called, the `%' part was already skipped by the
293 : : diagnostic machinery. */
294 : : static bool
295 : 68025 : c_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
296 : : int precision, bool wide, bool set_locus, bool hash,
297 : : bool *quoted, const char **)
298 : : {
299 : 68025 : tree t = NULL_TREE;
300 : : // FIXME: the next cast should be a dynamic_cast, when it is permitted.
301 : 68025 : c_pretty_printer *cpp = (c_pretty_printer *) pp;
302 : 68025 : pp->padding = pp_none;
303 : :
304 : 68025 : if (precision != 0 || wide)
305 : : return false;
306 : :
307 : 68025 : if (*spec != 'v')
308 : : {
309 : 67820 : t = va_arg (*text->m_args_ptr, tree);
310 : 67820 : if (set_locus)
311 : 1491 : text->set_location (0, DECL_SOURCE_LOCATION (t),
312 : : SHOW_RANGE_WITH_CARET);
313 : : }
314 : :
315 : 68025 : switch (*spec)
316 : : {
317 : 14415 : case 'D':
318 : 14415 : if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t))
319 : : {
320 : 40 : t = DECL_DEBUG_EXPR (t);
321 : 40 : if (!DECL_P (t))
322 : : {
323 : 40 : cpp->expression (t);
324 : 40 : return true;
325 : : }
326 : : }
327 : : /* FALLTHRU */
328 : :
329 : 14437 : case 'F':
330 : 14437 : if (DECL_NAME (t))
331 : : {
332 : 13923 : pp_identifier (cpp, lang_hooks.decl_printable_name (t, 2));
333 : 13923 : return true;
334 : : }
335 : 514 : break;
336 : :
337 : 22488 : case 'T':
338 : 22488 : print_type (cpp, t, quoted);
339 : 22488 : return true;
340 : :
341 : 30855 : case 'E':
342 : 30855 : if (TREE_CODE (t) == IDENTIFIER_NODE)
343 : 4919 : pp_identifier (cpp, IDENTIFIER_POINTER (t));
344 : : else
345 : 25936 : cpp->expression (t);
346 : : return true;
347 : :
348 : 0 : case 'V':
349 : 0 : pp_c_type_qualifier_list (cpp, t);
350 : 0 : return true;
351 : :
352 : 205 : case 'v':
353 : 205 : pp_c_cv_qualifiers (cpp, va_arg (*text->m_args_ptr, int), hash);
354 : 205 : return true;
355 : :
356 : : default:
357 : : return false;
358 : : }
359 : :
360 : 514 : pp_string (cpp, _("({anonymous})"));
361 : 514 : return true;
362 : : }
363 : :
364 : : /* C-specific implementation of range_label::get_text () vfunc for
365 : : range_label_for_type_mismatch. */
366 : :
367 : : label_text
368 : 315 : range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
369 : : {
370 : 315 : if (m_labelled_type == NULL_TREE)
371 : 0 : return label_text::borrow (NULL);
372 : :
373 : 315 : c_pretty_printer cpp;
374 : 315 : bool quoted = false;
375 : 315 : print_type (&cpp, m_labelled_type, "ed);
376 : 315 : return label_text::take (xstrdup (pp_formatted_text (&cpp)));
377 : 315 : }
378 : :
379 : :
380 : : /* In C and ObjC, all decls have "C" linkage. */
381 : : bool
382 : 13535400 : has_c_linkage (const_tree decl ATTRIBUTE_UNUSED)
383 : : {
384 : 13535400 : return true;
385 : : }
386 : :
387 : : void
388 : 104930 : c_initialize_diagnostics (diagnostic_context *context)
389 : : {
390 : 104930 : pretty_printer *base = context->printer;
391 : 104930 : c_pretty_printer *pp = XNEW (c_pretty_printer);
392 : 104930 : context->printer = new (pp) c_pretty_printer ();
393 : :
394 : : /* It is safe to free this object because it was previously XNEW()'d. */
395 : 104930 : base->~pretty_printer ();
396 : 104930 : XDELETE (base);
397 : :
398 : 104930 : c_common_diagnostics_set_defaults (context);
399 : 104930 : diagnostic_format_decoder (context) = &c_tree_printer;
400 : 104930 : }
401 : :
402 : : int
403 : 13242953 : c_types_compatible_p (tree x, tree y)
404 : : {
405 : 13242953 : return comptypes (TYPE_MAIN_VARIANT (x), TYPE_MAIN_VARIANT (y));
406 : : }
407 : :
408 : : /* Determine if the type is a variably modified type for the backend. */
409 : :
410 : : bool
411 : 10299544 : c_var_mod_p (tree x, tree fn ATTRIBUTE_UNUSED)
412 : : {
413 : 10299544 : return C_TYPE_VARIABLY_MODIFIED (x);
414 : : }
415 : :
416 : : /* Special routine to get the alias set of T for C. */
417 : :
418 : : alias_set_type
419 : 190529531 : c_get_alias_set (tree t)
420 : : {
421 : : /* Allow aliasing between enumeral types and the underlying
422 : : integer type. This is required since those are compatible types. */
423 : 190529531 : if (TREE_CODE (t) == ENUMERAL_TYPE)
424 : 406346 : return get_alias_set (ENUM_UNDERLYING_TYPE (t));
425 : :
426 : : /* Structs with variable size can alias different incompatible
427 : : structs. Let them alias anything. */
428 : 190123185 : if (RECORD_OR_UNION_TYPE_P (t) && C_TYPE_VARIABLE_SIZE (t))
429 : : return 0;
430 : :
431 : 190115125 : return c_common_get_alias_set (t);
432 : : }
433 : :
434 : : /* In C there are no invisible parameters like in C++ (this, in-charge, VTT,
435 : : etc.). */
436 : :
437 : : int
438 : 21591871 : maybe_adjust_arg_pos_for_attribute (const_tree)
439 : : {
440 : 21591871 : return 0;
441 : : }
442 : :
443 : : /* In C, no expression is dependent. */
444 : :
445 : : bool
446 : 35 : instantiation_dependent_expression_p (tree)
447 : : {
448 : 35 : return false;
449 : : }
450 : :
451 : : /* Return -1 if dwarf ATTR shouldn't be added for TYPE, or the attribute
452 : : value otherwise. */
453 : : int
454 : 101351 : c_type_dwarf_attribute (const_tree type, int attr)
455 : : {
456 : 101351 : if (type == NULL_TREE)
457 : : return -1;
458 : :
459 : 101351 : switch (attr)
460 : : {
461 : 5857 : case DW_AT_export_symbols:
462 : 5857 : if (RECORD_OR_UNION_TYPE_P (type) && TYPE_NAME (type) == NULL_TREE)
463 : 5857 : return 1;
464 : : break;
465 : :
466 : : default:
467 : : break;
468 : : }
469 : :
470 : : return -1;
471 : : }
|