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