]>
Commit | Line | Data |
---|---|---|
0abc6a6a | 1 | /* Some code common to C and ObjC front ends. |
99dee823 | 2 | Copyright (C) 2001-2021 Free Software Foundation, Inc. |
0abc6a6a NB |
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 | |
9dcd6f09 | 8 | Software Foundation; either version 3, or (at your option) any later |
0abc6a6a NB |
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 | |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
0abc6a6a NB |
19 | |
20 | #include "config.h" | |
21 | #include "system.h" | |
4977bab6 | 22 | #include "coretypes.h" |
0abc6a6a | 23 | #include "c-tree.h" |
b02cec6e | 24 | #include "intl.h" |
39dabefd | 25 | #include "c-family/c-pretty-print.h" |
cf835838 | 26 | #include "tree-pretty-print.h" |
da67acb9 | 27 | #include "gimple-pretty-print.h" |
7afff7cf | 28 | #include "langhooks.h" |
9a4d6480 | 29 | #include "c-objc-common.h" |
96e6ae57 | 30 | #include "gcc-rich-location.h" |
56898e43 RS |
31 | #include "stringpool.h" |
32 | #include "attribs.h" | |
0abc6a6a | 33 | |
39ce81c9 | 34 | static bool c_tree_printer (pretty_printer *, text_info *, const char *, |
ce95abc4 | 35 | int, bool, bool, bool, bool *, const char **); |
3b27886e | 36 | |
6de9cd9a | 37 | bool |
2f6e4e97 | 38 | c_missing_noreturn_ok_p (tree decl) |
0abc6a6a NB |
39 | { |
40 | /* A missing noreturn is not ok for freestanding implementations and | |
41 | ok for the `main' function in hosted implementations. */ | |
42 | return flag_hosted && MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl)); | |
43 | } | |
44 | ||
d7438551 | 45 | /* Called from check_global_declaration. */ |
ef4f94ac RH |
46 | |
47 | bool | |
ac7d7749 | 48 | c_warn_unused_global_decl (const_tree decl) |
ef4f94ac RH |
49 | { |
50 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)) | |
51 | return false; | |
52 | if (DECL_IN_SYSTEM_HEADER (decl)) | |
53 | return false; | |
54 | ||
55 | return true; | |
56 | } | |
57 | ||
0abc6a6a | 58 | /* Initialization common to C and Objective-C front ends. */ |
4bfec483 | 59 | bool |
2f6e4e97 | 60 | c_objc_common_init (void) |
0abc6a6a NB |
61 | { |
62 | c_init_decl_processing (); | |
63 | ||
d723bb7c | 64 | return c_common_init (); |
0abc6a6a NB |
65 | } |
66 | ||
56898e43 RS |
67 | /* Decide whether it's worth saying that TYPE is also known as some other |
68 | type. Return the other type if so, otherwise return TYPE. */ | |
55879815 | 69 | |
56898e43 RS |
70 | static tree |
71 | get_aka_type (tree type) | |
55879815 | 72 | { |
56898e43 RS |
73 | if (type == error_mark_node) |
74 | return type; | |
55879815 | 75 | |
56898e43 RS |
76 | tree result; |
77 | if (typedef_variant_p (type)) | |
55879815 RS |
78 | { |
79 | /* Saying that "foo" is also known as "struct foo" or | |
80 | "struct <anonymous>" is unlikely to be useful, since users of | |
81 | structure-like types would already know that they're structures. | |
82 | The same applies to unions and enums; in general, printing the | |
83 | tag is only useful if it has a different name. */ | |
56898e43 RS |
84 | tree orig_type = DECL_ORIGINAL_TYPE (TYPE_NAME (type)); |
85 | tree_code code = TREE_CODE (orig_type); | |
86 | tree orig_id = TYPE_IDENTIFIER (orig_type); | |
55879815 | 87 | if ((code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE) |
56898e43 RS |
88 | && (!orig_id || TYPE_IDENTIFIER (type) == orig_id)) |
89 | return type; | |
55879815 | 90 | |
56898e43 RS |
91 | if (!user_facing_original_type_p (type)) |
92 | return type; | |
93 | ||
94 | result = get_aka_type (orig_type); | |
55879815 RS |
95 | } |
96 | else | |
97 | { | |
56898e43 RS |
98 | tree canonical = TYPE_CANONICAL (type); |
99 | if (canonical && TREE_CODE (type) != TREE_CODE (canonical)) | |
100 | return canonical; | |
101 | ||
102 | /* Recursive calls might choose a middle ground between TYPE | |
103 | (which has no typedefs stripped) and CANONICAL (which has | |
104 | all typedefs stripped). So try to reuse TYPE or CANONICAL if | |
105 | convenient, but be prepared to create a new type if necessary. */ | |
106 | switch (TREE_CODE (type)) | |
55879815 RS |
107 | { |
108 | case POINTER_TYPE: | |
109 | case REFERENCE_TYPE: | |
56898e43 RS |
110 | { |
111 | tree target_type = get_aka_type (TREE_TYPE (type)); | |
112 | ||
113 | if (target_type == TREE_TYPE (type)) | |
114 | return type; | |
115 | ||
116 | if (canonical && target_type == TREE_TYPE (canonical)) | |
117 | return canonical; | |
118 | ||
119 | result = (TREE_CODE (type) == POINTER_TYPE | |
120 | ? build_pointer_type (target_type) | |
121 | : build_reference_type (target_type)); | |
122 | break; | |
123 | } | |
55879815 RS |
124 | |
125 | case ARRAY_TYPE: | |
56898e43 RS |
126 | { |
127 | tree element_type = get_aka_type (TREE_TYPE (type)); | |
128 | tree index_type = (TYPE_DOMAIN (type) | |
129 | ? get_aka_type (TYPE_DOMAIN (type)) | |
130 | : NULL_TREE); | |
131 | ||
132 | if (element_type == TREE_TYPE (type) | |
133 | && index_type == TYPE_DOMAIN (type)) | |
134 | return type; | |
135 | ||
136 | if (canonical | |
137 | && element_type == TREE_TYPE (canonical) | |
138 | && index_type == TYPE_DOMAIN (canonical)) | |
139 | return canonical; | |
140 | ||
141 | result = build_array_type (element_type, index_type, | |
142 | TYPE_TYPELESS_STORAGE (type)); | |
143 | break; | |
144 | } | |
55879815 RS |
145 | |
146 | case FUNCTION_TYPE: | |
147 | { | |
56898e43 RS |
148 | tree return_type = get_aka_type (TREE_TYPE (type)); |
149 | ||
150 | tree args = TYPE_ARG_TYPES (type); | |
151 | if (args == error_mark_node) | |
152 | return type; | |
153 | ||
154 | auto_vec<tree, 32> arg_types; | |
155 | bool type_ok_p = true; | |
156 | while (args && args != void_list_node) | |
55879815 | 157 | { |
56898e43 RS |
158 | tree arg_type = get_aka_type (TREE_VALUE (args)); |
159 | arg_types.safe_push (arg_type); | |
160 | type_ok_p &= (arg_type == TREE_VALUE (args)); | |
161 | args = TREE_CHAIN (args); | |
55879815 | 162 | } |
56898e43 RS |
163 | |
164 | if (type_ok_p && return_type == TREE_TYPE (type)) | |
165 | return type; | |
166 | ||
167 | unsigned int i; | |
168 | tree arg_type; | |
169 | FOR_EACH_VEC_ELT_REVERSE (arg_types, i, arg_type) | |
170 | args = tree_cons (NULL_TREE, arg_type, args); | |
171 | result = build_function_type (return_type, args); | |
172 | break; | |
55879815 RS |
173 | } |
174 | ||
175 | default: | |
56898e43 | 176 | return canonical ? canonical : type; |
55879815 RS |
177 | } |
178 | } | |
56898e43 RS |
179 | return build_type_attribute_qual_variant (result, TYPE_ATTRIBUTES (type), |
180 | TYPE_QUALS (type)); | |
55879815 RS |
181 | } |
182 | ||
96e6ae57 DM |
183 | /* Print T to CPP. */ |
184 | ||
185 | static void | |
186 | print_type (c_pretty_printer *cpp, tree t, bool *quoted) | |
187 | { | |
188 | gcc_assert (TYPE_P (t)); | |
189 | struct obstack *ob = pp_buffer (cpp)->obstack; | |
190 | char *p = (char *) obstack_base (ob); | |
191 | /* Remember the end of the initial dump. */ | |
192 | int len = obstack_object_size (ob); | |
193 | ||
194 | tree name = TYPE_NAME (t); | |
195 | if (name && TREE_CODE (name) == TYPE_DECL && DECL_NAME (name)) | |
196 | pp_identifier (cpp, lang_hooks.decl_printable_name (name, 2)); | |
197 | else | |
198 | cpp->type_id (t); | |
199 | ||
200 | /* If we're printing a type that involves typedefs, also print the | |
201 | stripped version. But sometimes the stripped version looks | |
202 | exactly the same, so we don't want it after all. To avoid | |
203 | printing it in that case, we play ugly obstack games. */ | |
56898e43 RS |
204 | tree aka_type = get_aka_type (t); |
205 | if (aka_type != t) | |
96e6ae57 DM |
206 | { |
207 | c_pretty_printer cpp2; | |
208 | /* Print the stripped version into a temporary printer. */ | |
56898e43 | 209 | cpp2.type_id (aka_type); |
96e6ae57 DM |
210 | struct obstack *ob2 = cpp2.buffer->obstack; |
211 | /* Get the stripped version from the temporary printer. */ | |
212 | const char *aka = (char *) obstack_base (ob2); | |
213 | int aka_len = obstack_object_size (ob2); | |
214 | int type1_len = obstack_object_size (ob) - len; | |
215 | ||
216 | /* If they are identical, bail out. */ | |
217 | if (aka_len == type1_len && memcmp (p + len, aka, aka_len) == 0) | |
218 | return; | |
219 | ||
220 | /* They're not, print the stripped version now. */ | |
221 | if (*quoted) | |
222 | pp_end_quote (cpp, pp_show_color (cpp)); | |
223 | pp_c_whitespace (cpp); | |
224 | pp_left_brace (cpp); | |
225 | pp_c_ws_string (cpp, _("aka")); | |
226 | pp_c_whitespace (cpp); | |
227 | if (*quoted) | |
228 | pp_begin_quote (cpp, pp_show_color (cpp)); | |
56898e43 | 229 | cpp->type_id (aka_type); |
96e6ae57 DM |
230 | if (*quoted) |
231 | pp_end_quote (cpp, pp_show_color (cpp)); | |
232 | pp_right_brace (cpp); | |
233 | /* No further closing quotes are needed. */ | |
234 | *quoted = false; | |
235 | } | |
236 | } | |
237 | ||
0abc6a6a NB |
238 | /* Called during diagnostic message formatting process to print a |
239 | source-level entity onto BUFFER. The meaning of the format specifiers | |
240 | is as follows: | |
241 | %D: a general decl, | |
7848dfca | 242 | %E: an identifier or expression, |
0abc6a6a | 243 | %F: a function declaration, |
8a45b051 | 244 | %G: a Gimple statement, |
da67acb9 | 245 | %K: a CALL_EXPR, |
0abc6a6a | 246 | %T: a type. |
49706e39 MLI |
247 | %V: a list of type qualifiers from a tree. |
248 | %v: an explicit list of type qualifiers | |
249 | %#v: an explicit list of type qualifiers of a function type. | |
0abc6a6a | 250 | |
0abc6a6a NB |
251 | Please notice when called, the `%' part was already skipped by the |
252 | diagnostic machinery. */ | |
47b69537 | 253 | static bool |
39ce81c9 | 254 | c_tree_printer (pretty_printer *pp, text_info *text, const char *spec, |
f012c8ef | 255 | int precision, bool wide, bool set_locus, bool hash, |
ce95abc4 | 256 | bool *quoted, const char **) |
0abc6a6a | 257 | { |
5d7eb7e2 | 258 | tree t = NULL_TREE; |
025311c4 | 259 | // FIXME: the next cast should be a dynamic_cast, when it is permitted. |
ede1a387 JM |
260 | c_pretty_printer *cpp = (c_pretty_printer *) pp; |
261 | pp->padding = pp_none; | |
0abc6a6a | 262 | |
49706e39 | 263 | if (precision != 0 || wide) |
39ce81c9 ZW |
264 | return false; |
265 | ||
da67acb9 MS |
266 | if (*spec == 'G') |
267 | { | |
268 | percent_G_format (text); | |
269 | return true; | |
270 | } | |
271 | ||
cf835838 JM |
272 | if (*spec == 'K') |
273 | { | |
da67acb9 | 274 | t = va_arg (*text->args_ptr, tree); |
8a45b051 | 275 | percent_K_format (text, EXPR_LOCATION (t), TREE_BLOCK (t)); |
cf835838 JM |
276 | return true; |
277 | } | |
278 | ||
49706e39 MLI |
279 | if (*spec != 'v') |
280 | { | |
281 | t = va_arg (*text->args_ptr, tree); | |
2a2703a2 | 282 | if (set_locus) |
85204e23 DM |
283 | text->set_location (0, DECL_SOURCE_LOCATION (t), |
284 | SHOW_RANGE_WITH_CARET); | |
49706e39 | 285 | } |
dee15844 | 286 | |
39ce81c9 | 287 | switch (*spec) |
0abc6a6a NB |
288 | { |
289 | case 'D': | |
0ae9bd27 | 290 | if (VAR_P (t) && DECL_HAS_DEBUG_EXPR_P (t)) |
dad2a933 RH |
291 | { |
292 | t = DECL_DEBUG_EXPR (t); | |
293 | if (!DECL_P (t)) | |
294 | { | |
20059c8b | 295 | cpp->expression (t); |
dad2a933 RH |
296 | return true; |
297 | } | |
298 | } | |
299 | /* FALLTHRU */ | |
300 | ||
0abc6a6a | 301 | case 'F': |
c157f85c | 302 | if (DECL_NAME (t)) |
b02cec6e JM |
303 | { |
304 | pp_identifier (cpp, lang_hooks.decl_printable_name (t, 2)); | |
305 | return true; | |
306 | } | |
c157f85c RH |
307 | break; |
308 | ||
0abc6a6a | 309 | case 'T': |
96e6ae57 DM |
310 | print_type (cpp, t, quoted); |
311 | return true; | |
0abc6a6a | 312 | |
eb8221ea | 313 | case 'E': |
c157f85c | 314 | if (TREE_CODE (t) == IDENTIFIER_NODE) |
b02cec6e | 315 | pp_identifier (cpp, IDENTIFIER_POINTER (t)); |
c157f85c | 316 | else |
20059c8b | 317 | cpp->expression (t); |
b02cec6e | 318 | return true; |
eb8221ea | 319 | |
49706e39 MLI |
320 | case 'V': |
321 | pp_c_type_qualifier_list (cpp, t); | |
322 | return true; | |
323 | ||
324 | case 'v': | |
325 | pp_c_cv_qualifiers (cpp, va_arg (*text->args_ptr, int), hash); | |
326 | return true; | |
327 | ||
0abc6a6a | 328 | default: |
47b69537 | 329 | return false; |
0abc6a6a | 330 | } |
c157f85c | 331 | |
b02cec6e | 332 | pp_string (cpp, _("({anonymous})")); |
c157f85c | 333 | return true; |
0abc6a6a | 334 | } |
e57e265b | 335 | |
96e6ae57 DM |
336 | /* C-specific implementation of range_label::get_text () vfunc for |
337 | range_label_for_type_mismatch. */ | |
338 | ||
339 | label_text | |
9c4a4b3c | 340 | range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const |
96e6ae57 DM |
341 | { |
342 | if (m_labelled_type == NULL_TREE) | |
d68f5d45 | 343 | return label_text::borrow (NULL); |
96e6ae57 DM |
344 | |
345 | c_pretty_printer cpp; | |
346 | bool quoted = false; | |
347 | print_type (&cpp, m_labelled_type, "ed); | |
d68f5d45 | 348 | return label_text::take (xstrdup (pp_formatted_text (&cpp))); |
96e6ae57 DM |
349 | } |
350 | ||
351 | ||
84b8b0e0 ZW |
352 | /* In C and ObjC, all decls have "C" linkage. */ |
353 | bool | |
58f9752a | 354 | has_c_linkage (const_tree decl ATTRIBUTE_UNUSED) |
84b8b0e0 ZW |
355 | { |
356 | return true; | |
357 | } | |
ede1a387 JM |
358 | |
359 | void | |
360 | c_initialize_diagnostics (diagnostic_context *context) | |
361 | { | |
da6ca2b5 GDR |
362 | pretty_printer *base = context->printer; |
363 | c_pretty_printer *pp = XNEW (c_pretty_printer); | |
364 | context->printer = new (pp) c_pretty_printer (); | |
ede1a387 | 365 | |
5d038c4c | 366 | /* It is safe to free this object because it was previously XNEW()'d. */ |
025311c4 | 367 | base->~pretty_printer (); |
5d038c4c | 368 | XDELETE (base); |
d723bb7c MLI |
369 | |
370 | c_common_diagnostics_set_defaults (context); | |
371 | diagnostic_format_decoder (context) = &c_tree_printer; | |
ede1a387 | 372 | } |
65958285 ZL |
373 | |
374 | int | |
375 | c_types_compatible_p (tree x, tree y) | |
376 | { | |
d9ad7862 | 377 | return comptypes (TYPE_MAIN_VARIANT (x), TYPE_MAIN_VARIANT (y)); |
65958285 | 378 | } |
52ffd86e MS |
379 | |
380 | /* Determine if the type is a vla type for the backend. */ | |
381 | ||
382 | bool | |
383 | c_vla_unspec_p (tree x, tree fn ATTRIBUTE_UNUSED) | |
384 | { | |
385 | return c_vla_type_p (x); | |
386 | } | |
2a82beaa RB |
387 | |
388 | /* Special routine to get the alias set of T for C. */ | |
389 | ||
390 | alias_set_type | |
391 | c_get_alias_set (tree t) | |
392 | { | |
393 | /* Allow aliasing between enumeral types and the underlying | |
394 | integer type. This is required since those are compatible types. */ | |
395 | if (TREE_CODE (t) == ENUMERAL_TYPE) | |
396 | { | |
397 | tree t1 = c_common_type_for_size (tree_to_uhwi (TYPE_SIZE (t)), | |
398 | /* short-cut commoning to signed | |
399 | type. */ | |
400 | false); | |
401 | return get_alias_set (t1); | |
402 | } | |
403 | ||
404 | return c_common_get_alias_set (t); | |
405 | } |