1 /* Some code common to C and ObjC front ends.
2 Copyright (C) 2001-2022 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
22 #include "coretypes.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"
34 static bool c_tree_printer (pretty_printer
*, text_info
*, const char *,
35 int, bool, bool, bool, bool *, const char **);
38 c_missing_noreturn_ok_p (tree decl
)
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
));
45 /* Called from check_global_declaration. */
48 c_warn_unused_global_decl (const_tree decl
)
50 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (decl
))
52 if (DECL_IN_SYSTEM_HEADER (decl
))
58 /* Initialization common to C and Objective-C front ends. */
60 c_objc_common_init (void)
62 c_init_decl_processing ();
64 return c_common_init ();
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. */
71 get_aka_type (tree type
)
73 if (type
== error_mark_node
)
77 if (typedef_variant_p (type
))
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. */
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
);
87 if ((code
== RECORD_TYPE
|| code
== UNION_TYPE
|| code
== ENUMERAL_TYPE
)
88 && (!orig_id
|| TYPE_IDENTIFIER (type
) == orig_id
))
91 if (!user_facing_original_type_p (type
))
94 result
= get_aka_type (orig_type
);
98 tree canonical
= TYPE_CANONICAL (type
);
99 if (canonical
&& TREE_CODE (type
) != TREE_CODE (canonical
))
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
))
111 tree target_type
= get_aka_type (TREE_TYPE (type
));
113 if (target_type
== TREE_TYPE (type
))
116 if (canonical
&& target_type
== TREE_TYPE (canonical
))
119 result
= (TREE_CODE (type
) == POINTER_TYPE
120 ? build_pointer_type (target_type
)
121 : build_reference_type (target_type
));
127 tree element_type
= get_aka_type (TREE_TYPE (type
));
128 tree index_type
= (TYPE_DOMAIN (type
)
129 ? get_aka_type (TYPE_DOMAIN (type
))
132 if (element_type
== TREE_TYPE (type
)
133 && index_type
== TYPE_DOMAIN (type
))
137 && element_type
== TREE_TYPE (canonical
)
138 && index_type
== TYPE_DOMAIN (canonical
))
141 result
= build_array_type (element_type
, index_type
,
142 TYPE_TYPELESS_STORAGE (type
));
148 tree return_type
= get_aka_type (TREE_TYPE (type
));
150 tree args
= TYPE_ARG_TYPES (type
);
151 if (args
== error_mark_node
)
154 auto_vec
<tree
, 32> arg_types
;
155 bool type_ok_p
= true;
156 while (args
&& args
!= void_list_node
)
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
);
164 if (type_ok_p
&& return_type
== TREE_TYPE (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
);
176 return canonical
? canonical
: type
;
179 return build_type_attribute_qual_variant (result
, TYPE_ATTRIBUTES (type
),
183 /* Print T to CPP. */
186 print_type (c_pretty_printer
*cpp
, tree t
, bool *quoted
)
188 if (t
== error_mark_node
)
190 pp_string (cpp
, _("{erroneous}"));
194 gcc_assert (TYPE_P (t
));
195 struct obstack
*ob
= pp_buffer (cpp
)->obstack
;
196 char *p
= (char *) obstack_base (ob
);
197 /* Remember the end of the initial dump. */
198 int len
= obstack_object_size (ob
);
200 tree name
= TYPE_NAME (t
);
201 if (name
&& TREE_CODE (name
) == TYPE_DECL
&& DECL_NAME (name
))
202 pp_identifier (cpp
, lang_hooks
.decl_printable_name (name
, 2));
206 /* If we're printing a type that involves typedefs, also print the
207 stripped version. But sometimes the stripped version looks
208 exactly the same, so we don't want it after all. To avoid
209 printing it in that case, we play ugly obstack games. */
210 tree aka_type
= get_aka_type (t
);
213 c_pretty_printer cpp2
;
214 /* Print the stripped version into a temporary printer. */
215 cpp2
.type_id (aka_type
);
216 struct obstack
*ob2
= cpp2
.buffer
->obstack
;
217 /* Get the stripped version from the temporary printer. */
218 const char *aka
= (char *) obstack_base (ob2
);
219 int aka_len
= obstack_object_size (ob2
);
220 int type1_len
= obstack_object_size (ob
) - len
;
222 /* If they are identical, bail out. */
223 if (aka_len
== type1_len
&& memcmp (p
+ len
, aka
, aka_len
) == 0)
226 /* They're not, print the stripped version now. */
228 pp_end_quote (cpp
, pp_show_color (cpp
));
229 pp_c_whitespace (cpp
);
231 pp_c_ws_string (cpp
, _("aka"));
232 pp_c_whitespace (cpp
);
234 pp_begin_quote (cpp
, pp_show_color (cpp
));
235 cpp
->type_id (aka_type
);
237 pp_end_quote (cpp
, pp_show_color (cpp
));
238 pp_right_brace (cpp
);
239 /* No further closing quotes are needed. */
244 /* Called during diagnostic message formatting process to print a
245 source-level entity onto BUFFER. The meaning of the format specifiers
248 %E: an identifier or expression,
249 %F: a function declaration,
251 %V: a list of type qualifiers from a tree.
252 %v: an explicit list of type qualifiers
253 %#v: an explicit list of type qualifiers of a function type.
255 Please notice when called, the `%' part was already skipped by the
256 diagnostic machinery. */
258 c_tree_printer (pretty_printer
*pp
, text_info
*text
, const char *spec
,
259 int precision
, bool wide
, bool set_locus
, bool hash
,
260 bool *quoted
, const char **)
263 // FIXME: the next cast should be a dynamic_cast, when it is permitted.
264 c_pretty_printer
*cpp
= (c_pretty_printer
*) pp
;
265 pp
->padding
= pp_none
;
267 if (precision
!= 0 || wide
)
272 t
= va_arg (*text
->args_ptr
, tree
);
274 text
->set_location (0, DECL_SOURCE_LOCATION (t
),
275 SHOW_RANGE_WITH_CARET
);
281 if (VAR_P (t
) && DECL_HAS_DEBUG_EXPR_P (t
))
283 t
= DECL_DEBUG_EXPR (t
);
295 pp_identifier (cpp
, lang_hooks
.decl_printable_name (t
, 2));
301 print_type (cpp
, t
, quoted
);
305 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
306 pp_identifier (cpp
, IDENTIFIER_POINTER (t
));
312 pp_c_type_qualifier_list (cpp
, t
);
316 pp_c_cv_qualifiers (cpp
, va_arg (*text
->args_ptr
, int), hash
);
323 pp_string (cpp
, _("({anonymous})"));
327 /* C-specific implementation of range_label::get_text () vfunc for
328 range_label_for_type_mismatch. */
331 range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
333 if (m_labelled_type
== NULL_TREE
)
334 return label_text::borrow (NULL
);
336 c_pretty_printer cpp
;
338 print_type (&cpp
, m_labelled_type
, "ed
);
339 return label_text::take (xstrdup (pp_formatted_text (&cpp
)));
343 /* In C and ObjC, all decls have "C" linkage. */
345 has_c_linkage (const_tree decl ATTRIBUTE_UNUSED
)
351 c_initialize_diagnostics (diagnostic_context
*context
)
353 pretty_printer
*base
= context
->printer
;
354 c_pretty_printer
*pp
= XNEW (c_pretty_printer
);
355 context
->printer
= new (pp
) c_pretty_printer ();
357 /* It is safe to free this object because it was previously XNEW()'d. */
358 base
->~pretty_printer ();
361 c_common_diagnostics_set_defaults (context
);
362 diagnostic_format_decoder (context
) = &c_tree_printer
;
366 c_types_compatible_p (tree x
, tree y
)
368 return comptypes (TYPE_MAIN_VARIANT (x
), TYPE_MAIN_VARIANT (y
));
371 /* Determine if the type is a vla type for the backend. */
374 c_vla_unspec_p (tree x
, tree fn ATTRIBUTE_UNUSED
)
376 return c_vla_type_p (x
);
379 /* Special routine to get the alias set of T for C. */
382 c_get_alias_set (tree t
)
384 /* Allow aliasing between enumeral types and the underlying
385 integer type. This is required since those are compatible types. */
386 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
388 tree t1
= c_common_type_for_size (tree_to_uhwi (TYPE_SIZE (t
)),
389 /* short-cut commoning to signed
392 return get_alias_set (t1
);
395 return c_common_get_alias_set (t
);
398 /* In C there are no invisible parameters like in C++ (this, in-charge, VTT,
402 maybe_adjust_arg_pos_for_attribute (const_tree
)