1/* Some code common to C and ObjC front ends.
2 Copyright (C) 2001-2024 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along 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
35static 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
41struct c_feature_info
42{
43 const char *ident;
44 const int *enable_flag;
45};
46
47static const c_feature_info c_feature_table[] =
48{
49 { .ident: "c_alignas", .enable_flag: &flag_isoc11 },
50 { .ident: "c_alignof", .enable_flag: &flag_isoc11 },
51 { .ident: "c_atomic", .enable_flag: &flag_isoc11 },
52 { .ident: "c_generic_selections", .enable_flag: &flag_isoc11 },
53 { .ident: "c_static_assert", .enable_flag: &flag_isoc11 },
54 { .ident: "c_thread_local", .enable_flag: &flag_isoc11 },
55 { .ident: "cxx_binary_literals", .enable_flag: &flag_isoc23 }
56};
57
58/* Register features specific to the C language. */
59
60void
61c_register_features ()
62{
63 for (unsigned i = 0; i < ARRAY_SIZE (c_feature_table); i++)
64 {
65 const c_feature_info *info = c_feature_table + i;
66 const bool feat_p = !info->enable_flag || *info->enable_flag;
67 c_common_register_feature (info->ident, feat_p);
68 }
69}
70
71bool
72c_missing_noreturn_ok_p (tree decl)
73{
74 /* A missing noreturn is ok for the `main' function. */
75 if (!MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl)))
76 return false;
77
78 return flag_hosted
79 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl))) == integer_type_node;
80}
81
82/* Called from check_global_declaration. */
83
84bool
85c_warn_unused_global_decl (const_tree decl)
86{
87 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
88 return false;
89 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. */
96bool
97c_objc_common_init (void)
98{
99 c_init_decl_processing ();
100
101 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
107static tree
108get_aka_type (tree type)
109{
110 if (type == error_mark_node)
111 return type;
112
113 tree result;
114 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 tree orig_type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
122 tree_code code = TREE_CODE (orig_type);
123 tree orig_id = TYPE_IDENTIFIER (orig_type);
124 if ((code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
125 && (!orig_id || TYPE_IDENTIFIER (type) == orig_id))
126 return type;
127
128 if (!user_facing_original_type_p (type))
129 return type;
130
131 result = get_aka_type (type: orig_type);
132 }
133 else
134 {
135 tree canonical = TYPE_CANONICAL (type);
136 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 switch (TREE_CODE (type))
144 {
145 case POINTER_TYPE:
146 case REFERENCE_TYPE:
147 {
148 tree target_type = get_aka_type (TREE_TYPE (type));
149
150 if (target_type == TREE_TYPE (type))
151 return type;
152
153 if (canonical && target_type == TREE_TYPE (canonical))
154 return canonical;
155
156 result = (TREE_CODE (type) == POINTER_TYPE
157 ? build_pointer_type (target_type)
158 : build_reference_type (target_type));
159 break;
160 }
161
162 case ARRAY_TYPE:
163 {
164 tree element_type = get_aka_type (TREE_TYPE (type));
165 tree index_type = (TYPE_DOMAIN (type)
166 ? get_aka_type (TYPE_DOMAIN (type))
167 : NULL_TREE);
168
169 if (element_type == TREE_TYPE (type)
170 && index_type == TYPE_DOMAIN (type))
171 return type;
172
173 if (canonical
174 && element_type == TREE_TYPE (canonical)
175 && index_type == TYPE_DOMAIN (canonical))
176 return canonical;
177
178 result = build_array_type (element_type, index_type,
179 TYPE_TYPELESS_STORAGE (type));
180 break;
181 }
182
183 case FUNCTION_TYPE:
184 {
185 tree return_type = get_aka_type (TREE_TYPE (type));
186
187 tree args = TYPE_ARG_TYPES (type);
188 if (args == error_mark_node)
189 return type;
190
191 auto_vec<tree, 32> arg_types;
192 bool type_ok_p = true;
193 while (args && args != void_list_node)
194 {
195 tree arg_type = get_aka_type (TREE_VALUE (args));
196 arg_types.safe_push (obj: arg_type);
197 type_ok_p &= (arg_type == TREE_VALUE (args));
198 args = TREE_CHAIN (args);
199 }
200
201 if (type_ok_p && return_type == TREE_TYPE (type))
202 return type;
203
204 unsigned int i;
205 tree arg_type;
206 FOR_EACH_VEC_ELT_REVERSE (arg_types, i, arg_type)
207 args = tree_cons (NULL_TREE, arg_type, args);
208 result = build_function_type (return_type, args);
209 break;
210 }
211
212 default:
213 return canonical ? canonical : type;
214 }
215 }
216 return build_type_attribute_qual_variant (result, TYPE_ATTRIBUTES (type),
217 TYPE_QUALS (type));
218}
219
220/* Print T to CPP. */
221
222static void
223print_type (c_pretty_printer *cpp, tree t, bool *quoted)
224{
225 if (t == error_mark_node)
226 {
227 pp_string (cpp, _("{erroneous}"));
228 return;
229 }
230
231 gcc_assert (TYPE_P (t));
232 struct obstack *ob = pp_buffer (cpp)->obstack;
233 char *p = (char *) obstack_base (ob);
234 /* Remember the end of the initial dump. */
235 int len = obstack_object_size (ob);
236
237 tree name = TYPE_NAME (t);
238 if (name && TREE_CODE (name) == TYPE_DECL && DECL_NAME (name))
239 pp_identifier (cpp, lang_hooks.decl_printable_name (name, 2));
240 else
241 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 tree aka_type = get_aka_type (type: t);
248 if (aka_type != t)
249 {
250 c_pretty_printer cpp2;
251 /* Print the stripped version into a temporary printer. */
252 cpp2.type_id (aka_type);
253 struct obstack *ob2 = cpp2.buffer->obstack;
254 /* Get the stripped version from the temporary printer. */
255 const char *aka = (char *) obstack_base (ob2);
256 int aka_len = obstack_object_size (ob2);
257 int type1_len = obstack_object_size (ob) - len;
258
259 /* If they are identical, bail out. */
260 if (aka_len == type1_len && memcmp (s1: p + len, s2: aka, n: aka_len) == 0)
261 return;
262
263 /* They're not, print the stripped version now. */
264 if (*quoted)
265 pp_end_quote (cpp, pp_show_color (cpp));
266 pp_c_whitespace (cpp);
267 pp_left_brace (cpp);
268 pp_c_ws_string (cpp, _("aka"));
269 pp_c_whitespace (cpp);
270 if (*quoted)
271 pp_begin_quote (cpp, pp_show_color (cpp));
272 cpp->type_id (aka_type);
273 if (*quoted)
274 pp_end_quote (cpp, pp_show_color (cpp));
275 pp_right_brace (cpp);
276 /* No further closing quotes are needed. */
277 *quoted = false;
278 }
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. */
294static bool
295c_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 tree t = NULL_TREE;
300 // FIXME: the next cast should be a dynamic_cast, when it is permitted.
301 c_pretty_printer *cpp = (c_pretty_printer *) pp;
302 pp->padding = pp_none;
303
304 if (precision != 0 || wide)
305 return false;
306
307 if (*spec != 'v')
308 {
309 t = va_arg (*text->m_args_ptr, tree);
310 if (set_locus)
311 text->set_location (idx: 0, DECL_SOURCE_LOCATION (t),
312 range_display_kind: SHOW_RANGE_WITH_CARET);
313 }
314
315 switch (*spec)
316 {
317 case 'D':
318 if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t))
319 {
320 t = DECL_DEBUG_EXPR (t);
321 if (!DECL_P (t))
322 {
323 cpp->expression (t);
324 return true;
325 }
326 }
327 /* FALLTHRU */
328
329 case 'F':
330 if (DECL_NAME (t))
331 {
332 pp_identifier (cpp, lang_hooks.decl_printable_name (t, 2));
333 return true;
334 }
335 break;
336
337 case 'T':
338 print_type (cpp, t, quoted);
339 return true;
340
341 case 'E':
342 if (TREE_CODE (t) == IDENTIFIER_NODE)
343 pp_identifier (cpp, IDENTIFIER_POINTER (t));
344 else
345 cpp->expression (t);
346 return true;
347
348 case 'V':
349 pp_c_type_qualifier_list (cpp, t);
350 return true;
351
352 case 'v':
353 pp_c_cv_qualifiers (pp: cpp, va_arg (*text->m_args_ptr, int), func_type: hash);
354 return true;
355
356 default:
357 return false;
358 }
359
360 pp_string (cpp, _("({anonymous})"));
361 return true;
362}
363
364/* C-specific implementation of range_label::get_text () vfunc for
365 range_label_for_type_mismatch. */
366
367label_text
368range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
369{
370 if (m_labelled_type == NULL_TREE)
371 return label_text::borrow (NULL);
372
373 c_pretty_printer cpp;
374 bool quoted = false;
375 print_type (cpp: &cpp, t: m_labelled_type, quoted: &quoted);
376 return label_text::take (buffer: xstrdup (pp_formatted_text (&cpp)));
377}
378
379
380/* In C and ObjC, all decls have "C" linkage. */
381bool
382has_c_linkage (const_tree decl ATTRIBUTE_UNUSED)
383{
384 return true;
385}
386
387void
388c_initialize_diagnostics (diagnostic_context *context)
389{
390 pretty_printer *base = context->printer;
391 c_pretty_printer *pp = XNEW (c_pretty_printer);
392 context->printer = new (pp) c_pretty_printer ();
393
394 /* It is safe to free this object because it was previously XNEW()'d. */
395 base->~pretty_printer ();
396 XDELETE (base);
397
398 c_common_diagnostics_set_defaults (context);
399 diagnostic_format_decoder (context) = &c_tree_printer;
400}
401
402int
403c_types_compatible_p (tree x, tree y)
404{
405 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
410bool
411c_var_mod_p (tree x, tree fn ATTRIBUTE_UNUSED)
412{
413 return C_TYPE_VARIABLY_MODIFIED (x);
414}
415
416/* Special routine to get the alias set of T for C. */
417
418alias_set_type
419c_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 if (TREE_CODE (t) == ENUMERAL_TYPE)
424 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 if (RECORD_OR_UNION_TYPE_P (t) && C_TYPE_VARIABLE_SIZE (t))
429 return 0;
430
431 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
437int
438maybe_adjust_arg_pos_for_attribute (const_tree)
439{
440 return 0;
441}
442
443/* In C, no expression is dependent. */
444
445bool
446instantiation_dependent_expression_p (tree)
447{
448 return false;
449}
450
451/* Return -1 if dwarf ATTR shouldn't be added for TYPE, or the attribute
452 value otherwise. */
453int
454c_type_dwarf_attribute (const_tree type, int attr)
455{
456 if (type == NULL_TREE)
457 return -1;
458
459 switch (attr)
460 {
461 case DW_AT_export_symbols:
462 if (RECORD_OR_UNION_TYPE_P (type) && TYPE_NAME (type) == NULL_TREE)
463 return 1;
464 break;
465
466 default:
467 break;
468 }
469
470 return -1;
471}
472

source code of gcc/c/c-objc-common.cc