]>
Commit | Line | Data |
---|---|---|
b9305c66 | 1 | /* Mainly the interface between cpplib and the C front ends. |
7adcbafe | 2 | Copyright (C) 1987-2022 Free Software Foundation, Inc. |
e8bbfc4e | 3 | |
1322177d | 4 | This file is part of GCC. |
e8bbfc4e | 5 | |
1322177d LB |
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 |
1322177d | 9 | version. |
e8bbfc4e | 10 | |
1322177d LB |
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. | |
e8bbfc4e RK |
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/>. */ | |
e8bbfc4e | 19 | |
e9a25f70 | 20 | #include "config.h" |
670ee920 | 21 | #include "system.h" |
4977bab6 | 22 | #include "coretypes.h" |
2adfab87 | 23 | #include "target.h" |
2adfab87 AM |
24 | #include "c-common.h" |
25 | #include "timevar.h" | |
d8a2d370 DN |
26 | #include "stringpool.h" |
27 | #include "stor-layout.h" | |
3d6f7931 | 28 | #include "c-pragma.h" |
7f905405 | 29 | #include "debug.h" |
66168f96 | 30 | #include "flags.h" |
7365279f | 31 | #include "file-prefix-map.h" /* remap_macro_filename() */ |
8a97aed0 | 32 | #include "langhooks.h" |
42fd12b1 ESR |
33 | #include "attribs.h" |
34 | ||
0e5921e8 ZW |
35 | /* We may keep statistics about how long which files took to compile. */ |
36 | static int header_time, body_time; | |
37 | static splay_tree file_info_tree; | |
3ab6dd7c | 38 | |
0e5921e8 ZW |
39 | int pending_lang_change; /* If we need to switch languages - C++ only */ |
40 | int c_header_level; /* depth in C headers - C++ only */ | |
0173bb6f | 41 | |
2d7aa578 ESR |
42 | static tree interpret_integer (const cpp_token *, unsigned int, |
43 | enum overflow_type *); | |
44 | static tree interpret_float (const cpp_token *, unsigned int, const char *, | |
45 | enum overflow_type *); | |
14c931f1 | 46 | static tree interpret_fixed (const cpp_token *, unsigned int); |
5e9754af | 47 | static enum integer_type_kind narrowest_unsigned_type |
807e902e | 48 | (const widest_int &, unsigned int); |
5e9754af | 49 | static enum integer_type_kind narrowest_signed_type |
807e902e | 50 | (const widest_int &, unsigned int); |
46c2514e | 51 | static enum cpp_ttype lex_string (const cpp_token *, tree *, bool, bool); |
2f6e4e97 AJ |
52 | static tree lex_charconst (const cpp_token *); |
53 | static void update_header_times (const char *); | |
54 | static int dump_one_header (splay_tree_node, void *); | |
55 | static void cb_line_change (cpp_reader *, const cpp_token *, int); | |
56 | static void cb_ident (cpp_reader *, unsigned int, const cpp_string *); | |
57 | static void cb_def_pragma (cpp_reader *, unsigned int); | |
58 | static void cb_define (cpp_reader *, unsigned int, cpp_hashnode *); | |
59 | static void cb_undef (cpp_reader *, unsigned int, cpp_hashnode *); | |
e31c7eec | 60 | \f |
63973df3 | 61 | void |
2f6e4e97 | 62 | init_c_lex (void) |
e3d1fd32 | 63 | { |
0e5921e8 ZW |
64 | struct c_fileinfo *toplevel; |
65 | ||
5d709b00 ZW |
66 | /* The get_fileinfo data structure must be initialized before |
67 | cpp_read_main_file is called. */ | |
a8a05998 | 68 | toplevel = get_fileinfo ("<top level>"); |
0e5921e8 ZW |
69 | if (flag_detailed_statistics) |
70 | { | |
71 | header_time = 0; | |
72 | body_time = get_run_time (); | |
73 | toplevel->time = body_time; | |
74 | } | |
2f6e4e97 | 75 | |
c22027a0 | 76 | struct cpp_callbacks *cb = cpp_get_callbacks (parse_in); |
b61c5ed0 | 77 | |
97293897 | 78 | cb->line_change = cb_line_change; |
b61c5ed0 | 79 | cb->ident = cb_ident; |
b61c5ed0 | 80 | cb->def_pragma = cb_def_pragma; |
17211ab5 GK |
81 | cb->valid_pch = c_common_valid_pch; |
82 | cb->read_pch = c_common_read_pch; | |
1f8d3e84 | 83 | cb->has_attribute = c_common_has_attribute; |
ad1539d5 | 84 | cb->has_builtin = c_common_has_builtin; |
15c98b2e | 85 | cb->get_source_date_epoch = cb_get_source_date_epoch; |
cb18fd07 | 86 | cb->get_suggestion = cb_get_suggestion; |
7365279f | 87 | cb->remap_filename = remap_macro_filename; |
0e5921e8 | 88 | |
65289a3a | 89 | /* Set the debug callbacks if we can use them. */ |
c6a13190 | 90 | if ((debug_info_level == DINFO_LEVEL_VERBOSE |
66168f96 | 91 | && dwarf_debuginfo_p ()) |
c6a13190 | 92 | || flag_dump_go_spec != NULL) |
65289a3a | 93 | { |
b61c5ed0 NB |
94 | cb->define = cb_define; |
95 | cb->undef = cb_undef; | |
65289a3a | 96 | } |
e3d1fd32 PB |
97 | } |
98 | ||
0e5921e8 | 99 | struct c_fileinfo * |
2f6e4e97 | 100 | get_fileinfo (const char *name) |
e3d1fd32 | 101 | { |
0e5921e8 ZW |
102 | splay_tree_node n; |
103 | struct c_fileinfo *fi; | |
104 | ||
5d709b00 | 105 | if (!file_info_tree) |
b0c31bc6 | 106 | file_info_tree = splay_tree_new (splay_tree_compare_strings, |
5d709b00 | 107 | 0, |
b0c31bc6 | 108 | splay_tree_delete_pointers); |
5d709b00 | 109 | |
0e5921e8 ZW |
110 | n = splay_tree_lookup (file_info_tree, (splay_tree_key) name); |
111 | if (n) | |
112 | return (struct c_fileinfo *) n->value; | |
113 | ||
5d038c4c | 114 | fi = XNEW (struct c_fileinfo); |
0e5921e8 ZW |
115 | fi->time = 0; |
116 | fi->interface_only = 0; | |
117 | fi->interface_unknown = 1; | |
118 | splay_tree_insert (file_info_tree, (splay_tree_key) name, | |
119 | (splay_tree_value) fi); | |
120 | return fi; | |
e56e519d | 121 | } |
e3d1fd32 | 122 | |
0e5921e8 | 123 | static void |
2f6e4e97 | 124 | update_header_times (const char *name) |
e8bbfc4e | 125 | { |
0e5921e8 ZW |
126 | /* Changing files again. This means currently collected time |
127 | is charged against header time, and body time starts back at 0. */ | |
128 | if (flag_detailed_statistics) | |
e8bbfc4e | 129 | { |
0e5921e8 ZW |
130 | int this_time = get_run_time (); |
131 | struct c_fileinfo *file = get_fileinfo (name); | |
132 | header_time += this_time - body_time; | |
133 | file->time += this_time - body_time; | |
134 | body_time = this_time; | |
e8bbfc4e RK |
135 | } |
136 | } | |
137 | ||
0e5921e8 | 138 | static int |
e18476eb | 139 | dump_one_header (splay_tree_node n, void * ARG_UNUSED (dummy)) |
e8bbfc4e | 140 | { |
0e5921e8 ZW |
141 | print_time ((const char *) n->key, |
142 | ((struct c_fileinfo *) n->value)->time); | |
143 | return 0; | |
e8bbfc4e | 144 | } |
e8bbfc4e RK |
145 | |
146 | void | |
2f6e4e97 | 147 | dump_time_statistics (void) |
e8bbfc4e | 148 | { |
8400e75e | 149 | struct c_fileinfo *file = get_fileinfo (LOCATION_FILE (input_location)); |
0e5921e8 ZW |
150 | int this_time = get_run_time (); |
151 | file->time += this_time - body_time; | |
152 | ||
153 | fprintf (stderr, "\n******\n"); | |
154 | print_time ("header files (total)", header_time); | |
155 | print_time ("main file (total)", this_time - body_time); | |
156 | fprintf (stderr, "ratio = %g : 1\n", | |
3f75a254 | 157 | (double) header_time / (double) (this_time - body_time)); |
0e5921e8 ZW |
158 | fprintf (stderr, "\n******\n"); |
159 | ||
160 | splay_tree_foreach (file_info_tree, dump_one_header, 0); | |
e8bbfc4e | 161 | } |
a6124a42 | 162 | |
0e5921e8 | 163 | static void |
e18476eb BI |
164 | cb_ident (cpp_reader * ARG_UNUSED (pfile), |
165 | unsigned int ARG_UNUSED (line), | |
166 | const cpp_string * ARG_UNUSED (str)) | |
0e5921e8 | 167 | { |
3f75a254 | 168 | if (!flag_no_ident) |
0e5921e8 | 169 | { |
27e2564a | 170 | /* Convert escapes in the string. */ |
e6cc3a24 | 171 | cpp_string cstr = { 0, 0 }; |
b6baa67d | 172 | if (cpp_interpret_string (pfile, str, 1, &cstr, CPP_STRING)) |
e6cc3a24 | 173 | { |
a8781821 | 174 | targetm.asm_out.output_ident ((const char *) cstr.text); |
b1d5455a | 175 | free (CONST_CAST (unsigned char *, cstr.text)); |
e6cc3a24 | 176 | } |
0e5921e8 | 177 | } |
27e2564a NB |
178 | } |
179 | ||
97293897 NB |
180 | /* Called at the start of every non-empty line. TOKEN is the first |
181 | lexed token on the line. Used for diagnostic line numbers. */ | |
182 | static void | |
e18476eb | 183 | cb_line_change (cpp_reader * ARG_UNUSED (pfile), const cpp_token *token, |
7b9a5a66 | 184 | int parsing_args) |
97293897 | 185 | { |
12f9df4e | 186 | if (token->type != CPP_EOF && !parsing_args) |
3c20847b | 187 | input_location = token->src_loc; |
97293897 NB |
188 | } |
189 | ||
23345bbb | 190 | void |
0e50b624 | 191 | fe_file_change (const line_map_ordinary *new_map) |
27e2564a | 192 | { |
f4b2bde7 | 193 | if (new_map == NULL) |
12f9df4e | 194 | return; |
f4b2bde7 | 195 | |
47d89cf3 | 196 | if (new_map->reason == LC_ENTER) |
fbb18613 | 197 | { |
5bea1ccf JM |
198 | /* Don't stack the main buffer on the input stack; |
199 | we already did in compile_file. */ | |
3f75a254 | 200 | if (!MAIN_FILE_P (new_map)) |
0e5921e8 | 201 | { |
f10a9135 | 202 | location_t included_at = linemap_included_from (new_map); |
711b2510 TT |
203 | int line = 0; |
204 | if (included_at > BUILTINS_LOCATION) | |
205 | line = SOURCE_LINE (new_map - 1, included_at); | |
3c20847b | 206 | |
966e8f4d | 207 | input_location = new_map->start_location; |
46427374 | 208 | (*debug_hooks->start_source_file) (line, LINEMAP_FILE (new_map)); |
09cff37b | 209 | #ifdef SYSTEM_IMPLICIT_EXTERN_C |
27e2564a NB |
210 | if (c_header_level) |
211 | ++c_header_level; | |
d17687f6 | 212 | else if (LINEMAP_SYSP (new_map) == 2) |
27e2564a NB |
213 | { |
214 | c_header_level = 1; | |
215 | ++pending_lang_change; | |
216 | } | |
0e5921e8 | 217 | #endif |
27e2564a | 218 | } |
fbb18613 | 219 | } |
47d89cf3 | 220 | else if (new_map->reason == LC_LEAVE) |
fbb18613 | 221 | { |
09cff37b | 222 | #ifdef SYSTEM_IMPLICIT_EXTERN_C |
47d89cf3 NB |
223 | if (c_header_level && --c_header_level == 0) |
224 | { | |
d17687f6 | 225 | if (LINEMAP_SYSP (new_map) == 2) |
d4ee4d25 | 226 | warning (0, "badly nested C headers from preprocessor"); |
47d89cf3 NB |
227 | --pending_lang_change; |
228 | } | |
47d89cf3 | 229 | #endif |
966e8f4d | 230 | input_location = new_map->start_location; |
2f6e4e97 | 231 | |
46427374 | 232 | (*debug_hooks->end_source_file) (LINEMAP_LINE (new_map)); |
e8bbfc4e | 233 | } |
fbb18613 | 234 | |
46427374 | 235 | update_header_times (LINEMAP_FILE (new_map)); |
3c20847b | 236 | input_location = new_map->start_location; |
0e5921e8 | 237 | } |
8b97c5f8 ZW |
238 | |
239 | static void | |
620e594b | 240 | cb_def_pragma (cpp_reader *pfile, location_t loc) |
8b97c5f8 ZW |
241 | { |
242 | /* Issue a warning message if we have been asked to do so. Ignore | |
243 | unknown pragmas in system headers unless an explicit | |
ec5c56db | 244 | -Wunknown-pragmas has been given. */ |
8400e75e | 245 | if (warn_unknown_pragmas > in_system_header_at (input_location)) |
8b97c5f8 | 246 | { |
b6ff777c ZW |
247 | const unsigned char *space, *name; |
248 | const cpp_token *s; | |
b6ff777c | 249 | location_t fe_loc = loc; |
23356f93 | 250 | |
06470238 | 251 | space = name = (const unsigned char *) ""; |
4ed5bcfb | 252 | s = cpp_get_token (pfile); |
06470238 NB |
253 | if (s->type != CPP_EOF) |
254 | { | |
255 | space = cpp_token_as_text (pfile, s); | |
256 | s = cpp_get_token (pfile); | |
257 | if (s->type == CPP_NAME) | |
258 | name = cpp_token_as_text (pfile, s); | |
259 | } | |
8b97c5f8 | 260 | |
a9c697b8 | 261 | warning_at (fe_loc, OPT_Wunknown_pragmas, "ignoring %<#pragma %s %s%>", |
fab922b1 | 262 | space, name); |
8b97c5f8 ZW |
263 | } |
264 | } | |
0e5921e8 | 265 | |
65289a3a NB |
266 | /* #define callback for DWARF and DWARF2 debug info. */ |
267 | static void | |
620e594b | 268 | cb_define (cpp_reader *pfile, location_t loc, cpp_hashnode *node) |
65289a3a | 269 | { |
5ffeb913 | 270 | const struct line_map *map = linemap_lookup (line_table, loc); |
0e50b624 | 271 | (*debug_hooks->define) (SOURCE_LINE (linemap_check_ordinary (map), loc), |
7f905405 | 272 | (const char *) cpp_macro_definition (pfile, node)); |
65289a3a NB |
273 | } |
274 | ||
275 | /* #undef callback for DWARF and DWARF2 debug info. */ | |
276 | static void | |
8a97aed0 | 277 | cb_undef (cpp_reader *pfile, location_t loc, cpp_hashnode *node) |
65289a3a | 278 | { |
8a97aed0 NS |
279 | if (lang_hooks.preprocess_undef) |
280 | lang_hooks.preprocess_undef (pfile, loc, node); | |
281 | ||
5ffeb913 | 282 | const struct line_map *map = linemap_lookup (line_table, loc); |
0e50b624 | 283 | (*debug_hooks->undef) (SOURCE_LINE (linemap_check_ordinary (map), loc), |
7f905405 | 284 | (const char *) NODE_NAME (node)); |
65289a3a | 285 | } |
42fd12b1 | 286 | |
1f8d3e84 JJ |
287 | /* Wrapper around cpp_get_token to skip CPP_PADDING tokens |
288 | and not consume CPP_EOF. */ | |
289 | static const cpp_token * | |
290 | get_token_no_padding (cpp_reader *pfile) | |
291 | { | |
292 | for (;;) | |
293 | { | |
294 | const cpp_token *ret = cpp_peek_token (pfile, 0); | |
295 | if (ret->type == CPP_EOF) | |
296 | return ret; | |
297 | ret = cpp_get_token (pfile); | |
298 | if (ret->type != CPP_PADDING) | |
299 | return ret; | |
300 | } | |
301 | } | |
302 | ||
42fd12b1 | 303 | /* Callback for has_attribute. */ |
1f8d3e84 | 304 | int |
1d00f8c8 | 305 | c_common_has_attribute (cpp_reader *pfile, bool std_syntax) |
42fd12b1 ESR |
306 | { |
307 | int result = 0; | |
1f8d3e84 | 308 | tree attr_name = NULL_TREE; |
42fd12b1 ESR |
309 | const cpp_token *token; |
310 | ||
1f8d3e84 JJ |
311 | token = get_token_no_padding (pfile); |
312 | if (token->type != CPP_OPEN_PAREN) | |
42fd12b1 | 313 | { |
1f8d3e84 JJ |
314 | cpp_error (pfile, CPP_DL_ERROR, |
315 | "missing '(' after \"__has_attribute\""); | |
316 | return 0; | |
42fd12b1 | 317 | } |
1f8d3e84 | 318 | token = get_token_no_padding (pfile); |
42fd12b1 ESR |
319 | if (token->type == CPP_NAME) |
320 | { | |
1f8d3e84 JJ |
321 | attr_name = get_identifier ((const char *) |
322 | cpp_token_as_text (pfile, token)); | |
577eec56 | 323 | attr_name = canonicalize_attr_name (attr_name); |
1d00f8c8 JM |
324 | bool have_scope = false; |
325 | int idx = 0; | |
326 | const cpp_token *nxt_token; | |
327 | do | |
328 | nxt_token = cpp_peek_token (pfile, idx++); | |
329 | while (nxt_token->type == CPP_PADDING); | |
330 | if (nxt_token->type == CPP_SCOPE) | |
42fd12b1 | 331 | { |
1d00f8c8 JM |
332 | have_scope = true; |
333 | get_token_no_padding (pfile); // Eat scope. | |
334 | nxt_token = get_token_no_padding (pfile); | |
335 | if (nxt_token->type == CPP_NAME) | |
42fd12b1 | 336 | { |
1d00f8c8 JM |
337 | tree attr_ns = attr_name; |
338 | tree attr_id | |
339 | = get_identifier ((const char *) | |
340 | cpp_token_as_text (pfile, nxt_token)); | |
7f736410 JJ |
341 | attr_id = canonicalize_attr_name (attr_id); |
342 | if (c_dialect_cxx ()) | |
343 | { | |
344 | /* OpenMP attributes need special handling. */ | |
345 | if ((flag_openmp || flag_openmp_simd) | |
346 | && is_attribute_p ("omp", attr_ns) | |
347 | && (is_attribute_p ("directive", attr_id) | |
348 | || is_attribute_p ("sequence", attr_id))) | |
349 | result = 1; | |
350 | } | |
351 | if (result) | |
352 | attr_name = NULL_TREE; | |
353 | else | |
354 | attr_name = build_tree_list (attr_ns, attr_id); | |
42fd12b1 | 355 | } |
d067e05f | 356 | else |
42fd12b1 | 357 | { |
1d00f8c8 JM |
358 | cpp_error (pfile, CPP_DL_ERROR, |
359 | "attribute identifier required after scope"); | |
360 | attr_name = NULL_TREE; | |
361 | } | |
362 | } | |
363 | else | |
364 | { | |
365 | /* Some standard attributes need special handling. */ | |
366 | if (c_dialect_cxx ()) | |
367 | { | |
1f8d3e84 | 368 | if (is_attribute_p ("noreturn", attr_name)) |
42fd12b1 | 369 | result = 200809; |
1f8d3e84 | 370 | else if (is_attribute_p ("deprecated", attr_name)) |
42fd12b1 | 371 | result = 201309; |
b632761d | 372 | else if (is_attribute_p ("maybe_unused", attr_name) |
c6147dc4 | 373 | || is_attribute_p ("fallthrough", attr_name)) |
d067e05f | 374 | result = 201603; |
2674fa47 JM |
375 | else if (is_attribute_p ("no_unique_address", attr_name) |
376 | || is_attribute_p ("likely", attr_name) | |
377 | || is_attribute_p ("unlikely", attr_name)) | |
46c62690 | 378 | result = 201803; |
8ad0c477 JM |
379 | else if (is_attribute_p ("nodiscard", attr_name)) |
380 | result = 201907; | |
42fd12b1 | 381 | } |
1d00f8c8 JM |
382 | else |
383 | { | |
2eca4ff4 | 384 | if (is_attribute_p ("deprecated", attr_name)) |
1d00f8c8 | 385 | result = 201904; |
2eca4ff4 JM |
386 | else if (is_attribute_p ("fallthrough", attr_name)) |
387 | result = 201910; | |
1d00f8c8 JM |
388 | else if (is_attribute_p ("nodiscard", attr_name)) |
389 | result = 202003; | |
2eca4ff4 JM |
390 | else if (is_attribute_p ("maybe_unused", attr_name)) |
391 | result = 202106; | |
1d00f8c8 JM |
392 | } |
393 | if (result) | |
394 | attr_name = NULL_TREE; | |
42fd12b1 | 395 | } |
1d00f8c8 | 396 | if (attr_name && (have_scope || !std_syntax)) |
d067e05f JM |
397 | { |
398 | init_attributes (); | |
399 | const struct attribute_spec *attr = lookup_attribute_spec (attr_name); | |
400 | if (attr) | |
401 | result = 1; | |
402 | } | |
42fd12b1 ESR |
403 | } |
404 | else | |
1f8d3e84 JJ |
405 | { |
406 | cpp_error (pfile, CPP_DL_ERROR, | |
407 | "macro \"__has_attribute\" requires an identifier"); | |
408 | return 0; | |
409 | } | |
42fd12b1 | 410 | |
1f8d3e84 | 411 | if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN) |
42fd12b1 | 412 | cpp_error (pfile, CPP_DL_ERROR, |
1f8d3e84 | 413 | "missing ')' after \"__has_attribute\""); |
42fd12b1 ESR |
414 | |
415 | return result; | |
416 | } | |
ad1539d5 MS |
417 | |
418 | /* Callback for has_builtin. */ | |
419 | ||
420 | int | |
421 | c_common_has_builtin (cpp_reader *pfile) | |
422 | { | |
423 | const cpp_token *token = get_token_no_padding (pfile); | |
424 | if (token->type != CPP_OPEN_PAREN) | |
425 | { | |
426 | cpp_error (pfile, CPP_DL_ERROR, | |
427 | "missing '(' after \"__has_builtin\""); | |
428 | return 0; | |
429 | } | |
430 | ||
431 | const char *name = ""; | |
432 | token = get_token_no_padding (pfile); | |
433 | if (token->type == CPP_NAME) | |
434 | { | |
435 | name = (const char *) cpp_token_as_text (pfile, token); | |
436 | token = get_token_no_padding (pfile); | |
437 | if (token->type != CPP_CLOSE_PAREN) | |
438 | { | |
439 | cpp_error (pfile, CPP_DL_ERROR, | |
440 | "expected ')' after \"%s\"", name); | |
441 | name = ""; | |
442 | } | |
443 | } | |
444 | else | |
445 | { | |
446 | cpp_error (pfile, CPP_DL_ERROR, | |
447 | "macro \"__has_builtin\" requires an identifier"); | |
448 | if (token->type == CPP_CLOSE_PAREN) | |
449 | return 0; | |
450 | } | |
451 | ||
452 | /* Consume tokens up to the closing parenthesis, including any nested | |
453 | pairs of parentheses, to avoid confusing redundant errors. */ | |
454 | for (unsigned nparen = 1; ; token = get_token_no_padding (pfile)) | |
455 | { | |
456 | if (token->type == CPP_OPEN_PAREN) | |
457 | ++nparen; | |
458 | else if (token->type == CPP_CLOSE_PAREN) | |
459 | --nparen; | |
460 | else if (token->type == CPP_EOF) | |
461 | break; | |
462 | if (!nparen) | |
463 | break; | |
464 | } | |
465 | ||
466 | return names_builtin_p (name); | |
467 | } | |
468 | ||
e8bbfc4e | 469 | \f |
51e63e60 NS |
470 | /* Read a token and return its type. Fill *VALUE with its value, if |
471 | applicable. Fill *CPP_FLAGS with the token's flags, if it is | |
472 | non-NULL. */ | |
e6cc3a24 | 473 | |
f4086145 | 474 | enum cpp_ttype |
46c2514e TT |
475 | c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags, |
476 | int lex_flags) | |
e6cc3a24 | 477 | { |
51e63e60 NS |
478 | const cpp_token *tok; |
479 | enum cpp_ttype type; | |
ab84748a | 480 | unsigned char add_flags = 0; |
2d7aa578 | 481 | enum overflow_type overflow = OT_NONE; |
e6cc3a24 | 482 | |
51e63e60 | 483 | timevar_push (TV_CPP); |
e6cc3a24 | 484 | retry: |
5ffeb913 | 485 | tok = cpp_get_token_with_location (parse_in, loc); |
5ffeb913 TT |
486 | type = tok->type; |
487 | ||
488 | retry_after_at: | |
51e63e60 | 489 | switch (type) |
0e5921e8 | 490 | { |
51e63e60 NS |
491 | case CPP_PADDING: |
492 | goto retry; | |
c22cacf3 | 493 | |
0e5921e8 | 494 | case CPP_NAME: |
9a0c6187 | 495 | *value = HT_IDENT_TO_GCC_IDENT (HT_NODE (tok->val.node.node)); |
0e5921e8 | 496 | break; |
fbb18613 | 497 | |
0e5921e8 | 498 | case CPP_NUMBER: |
ceeedfc1 | 499 | { |
3ce4f9e4 | 500 | const char *suffix = NULL; |
0b2c4be5 | 501 | unsigned int flags = cpp_classify_number (parse_in, tok, &suffix, *loc); |
ceeedfc1 NB |
502 | |
503 | switch (flags & CPP_N_CATEGORY) | |
504 | { | |
505 | case CPP_N_INVALID: | |
506 | /* cpplib has issued an error. */ | |
e8f2b18d | 507 | *value = error_mark_node; |
ceeedfc1 NB |
508 | break; |
509 | ||
510 | case CPP_N_INTEGER: | |
ab84748a VR |
511 | /* C++ uses '0' to mark virtual functions as pure. |
512 | Set PURE_ZERO to pass this information to the C++ parser. */ | |
513 | if (tok->val.str.len == 1 && *tok->val.str.text == '0') | |
bedfca64 DM |
514 | add_flags = PURE_ZERO | DECIMAL_INT; |
515 | else if ((flags & CPP_N_INTEGER) && (flags & CPP_N_DECIMAL)) | |
516 | /* -Wxor-used-as-pow is only active for LHS of ^ expressed | |
517 | as a decimal integer. */ | |
518 | add_flags = DECIMAL_INT; | |
2d7aa578 | 519 | *value = interpret_integer (tok, flags, &overflow); |
ceeedfc1 NB |
520 | break; |
521 | ||
522 | case CPP_N_FLOATING: | |
2d7aa578 | 523 | *value = interpret_float (tok, flags, suffix, &overflow); |
ceeedfc1 NB |
524 | break; |
525 | ||
526 | default: | |
366de0ce | 527 | gcc_unreachable (); |
ceeedfc1 | 528 | } |
3ce4f9e4 ESR |
529 | |
530 | if (flags & CPP_N_USERDEF) | |
531 | { | |
573ac65e WB |
532 | char *str; |
533 | tree literal; | |
3ce4f9e4 ESR |
534 | tree suffix_id = get_identifier (suffix); |
535 | int len = tok->val.str.len - strlen (suffix); | |
1ca3916f JM |
536 | /* If this is going to be used as a C string to pass to a |
537 | raw literal operator, we need to add a trailing NUL. */ | |
3ce4f9e4 ESR |
538 | tree num_string = build_string (len + 1, |
539 | (const char *) tok->val.str.text); | |
540 | TREE_TYPE (num_string) = char_array_type_node; | |
541 | num_string = fix_string_type (num_string); | |
573ac65e | 542 | str = CONST_CAST (char *, TREE_STRING_POINTER (num_string)); |
3ce4f9e4 | 543 | str[len] = '\0'; |
2d7aa578 ESR |
544 | literal = build_userdef_literal (suffix_id, *value, overflow, |
545 | num_string); | |
3ce4f9e4 ESR |
546 | *value = literal; |
547 | } | |
ceeedfc1 | 548 | } |
0e5921e8 | 549 | break; |
93868d11 | 550 | |
e6cc3a24 ZW |
551 | case CPP_ATSIGN: |
552 | /* An @ may give the next token special significance in Objective-C. */ | |
e6cc3a24 ZW |
553 | if (c_dialect_objc ()) |
554 | { | |
5ffeb913 TT |
555 | location_t atloc = *loc; |
556 | location_t newloc; | |
c22cacf3 | 557 | |
51e63e60 | 558 | retry_at: |
5ffeb913 | 559 | tok = cpp_get_token_with_location (parse_in, &newloc); |
51e63e60 NS |
560 | type = tok->type; |
561 | switch (type) | |
e6cc3a24 | 562 | { |
51e63e60 NS |
563 | case CPP_PADDING: |
564 | goto retry_at; | |
c22cacf3 | 565 | |
51e63e60 NS |
566 | case CPP_STRING: |
567 | case CPP_WSTRING: | |
b6baa67d KVH |
568 | case CPP_STRING16: |
569 | case CPP_STRING32: | |
2c6e3f55 | 570 | case CPP_UTF8STRING: |
46c2514e | 571 | type = lex_string (tok, value, true, true); |
51e63e60 NS |
572 | break; |
573 | ||
e6cc3a24 | 574 | case CPP_NAME: |
9a0c6187 | 575 | *value = HT_IDENT_TO_GCC_IDENT (HT_NODE (tok->val.node.node)); |
1973201f NP |
576 | if (OBJC_IS_AT_KEYWORD (C_RID_CODE (*value)) |
577 | || OBJC_IS_CXX_KEYWORD (C_RID_CODE (*value))) | |
e6cc3a24 | 578 | { |
51e63e60 | 579 | type = CPP_AT_NAME; |
49b91f05 NP |
580 | /* Note the complication: if we found an OBJC_CXX |
581 | keyword, for example, 'class', we will be | |
582 | returning a token of type CPP_AT_NAME and rid | |
583 | code RID_CLASS (not RID_AT_CLASS). The language | |
584 | parser needs to convert that to RID_AT_CLASS. | |
7a2cc1e8 IS |
585 | However, we've now spliced the '@' together with the |
586 | keyword that follows; Adjust the location so that we | |
587 | get a source range covering the composite. | |
49b91f05 | 588 | */ |
7a2cc1e8 | 589 | *loc = make_location (atloc, atloc, newloc); |
51e63e60 | 590 | break; |
e6cc3a24 | 591 | } |
51e63e60 | 592 | /* FALLTHROUGH */ |
e6cc3a24 | 593 | |
51e63e60 NS |
594 | default: |
595 | /* ... or not. */ | |
fab922b1 | 596 | error_at (atloc, "stray %<@%> in program"); |
5ffeb913 | 597 | *loc = newloc; |
51e63e60 | 598 | goto retry_after_at; |
e6cc3a24 | 599 | } |
51e63e60 | 600 | break; |
e6cc3a24 ZW |
601 | } |
602 | ||
51e63e60 NS |
603 | /* FALLTHROUGH */ |
604 | case CPP_HASH: | |
605 | case CPP_PASTE: | |
606 | { | |
065312cf | 607 | unsigned char name[8]; |
c22cacf3 | 608 | |
47e20491 | 609 | *cpp_spell_token (parse_in, tok, name, true) = 0; |
c22cacf3 | 610 | |
0b2c4be5 | 611 | error_at (*loc, "stray %qs in program", name); |
51e63e60 | 612 | } |
c22cacf3 | 613 | |
51e63e60 | 614 | goto retry; |
e6cc3a24 | 615 | |
6338b358 NB |
616 | case CPP_OTHER: |
617 | { | |
618 | cppchar_t c = tok->val.str.text[0]; | |
619 | ||
620 | if (c == '"' || c == '\'') | |
584a7c46 | 621 | error_at (*loc, "missing terminating %c character", (int) c); |
6338b358 | 622 | else if (ISGRAPH (c)) |
584a7c46 | 623 | error_at (*loc, "stray %qc in program", (int) c); |
6338b358 | 624 | else |
bd5e882c DM |
625 | { |
626 | rich_location rich_loc (line_table, *loc); | |
627 | rich_loc.set_escape_on_output (true); | |
628 | error_at (&rich_loc, "stray %<\\%o%> in program", (int) c); | |
629 | } | |
6338b358 NB |
630 | } |
631 | goto retry; | |
632 | ||
3ce4f9e4 ESR |
633 | case CPP_CHAR_USERDEF: |
634 | case CPP_WCHAR_USERDEF: | |
635 | case CPP_CHAR16_USERDEF: | |
636 | case CPP_CHAR32_USERDEF: | |
fe95b036 | 637 | case CPP_UTF8CHAR_USERDEF: |
3ce4f9e4 ESR |
638 | { |
639 | tree literal; | |
640 | cpp_token temp_tok = *tok; | |
641 | const char *suffix = cpp_get_userdef_suffix (tok); | |
642 | temp_tok.val.str.len -= strlen (suffix); | |
643 | temp_tok.type = cpp_userdef_char_remove_type (type); | |
644 | literal = build_userdef_literal (get_identifier (suffix), | |
2d7aa578 ESR |
645 | lex_charconst (&temp_tok), |
646 | OT_NONE, NULL_TREE); | |
3ce4f9e4 ESR |
647 | *value = literal; |
648 | } | |
649 | break; | |
650 | ||
0e5921e8 ZW |
651 | case CPP_CHAR: |
652 | case CPP_WCHAR: | |
b6baa67d KVH |
653 | case CPP_CHAR16: |
654 | case CPP_CHAR32: | |
fe95b036 | 655 | case CPP_UTF8CHAR: |
4ed5bcfb | 656 | *value = lex_charconst (tok); |
0e5921e8 | 657 | break; |
fbb18613 | 658 | |
3ce4f9e4 ESR |
659 | case CPP_STRING_USERDEF: |
660 | case CPP_WSTRING_USERDEF: | |
661 | case CPP_STRING16_USERDEF: | |
662 | case CPP_STRING32_USERDEF: | |
663 | case CPP_UTF8STRING_USERDEF: | |
664 | { | |
665 | tree literal, string; | |
666 | const char *suffix = cpp_get_userdef_suffix (tok); | |
667 | string = build_string (tok->val.str.len - strlen (suffix), | |
668 | (const char *) tok->val.str.text); | |
669 | literal = build_userdef_literal (get_identifier (suffix), | |
2d7aa578 | 670 | string, OT_NONE, NULL_TREE); |
3ce4f9e4 ESR |
671 | *value = literal; |
672 | } | |
673 | break; | |
674 | ||
0e5921e8 ZW |
675 | case CPP_STRING: |
676 | case CPP_WSTRING: | |
b6baa67d KVH |
677 | case CPP_STRING16: |
678 | case CPP_STRING32: | |
2c6e3f55 JJ |
679 | case CPP_UTF8STRING: |
680 | if ((lex_flags & C_LEX_STRING_NO_JOIN) == 0) | |
51e63e60 | 681 | { |
46c2514e TT |
682 | type = lex_string (tok, value, false, |
683 | (lex_flags & C_LEX_STRING_NO_TRANSLATE) == 0); | |
51e63e60 NS |
684 | break; |
685 | } | |
5f754896 | 686 | *value = build_string (tok->val.str.len, (const char *) tok->val.str.text); |
bc4071dd | 687 | break; |
b8698a0f | 688 | |
c162c75e | 689 | case CPP_PRAGMA: |
c62c040f | 690 | *value = build_int_cst (integer_type_node, tok->val.pragma); |
0e5921e8 | 691 | break; |
fbb18613 | 692 | |
0e5921e8 | 693 | case CPP_HEADER_NAME: |
c9c3d5f2 NS |
694 | *value = build_string (tok->val.str.len, (const char *)tok->val.str.text); |
695 | break; | |
696 | ||
697 | /* This token should not be visible outside cpplib. */ | |
0e5921e8 | 698 | case CPP_MACRO_ARG: |
366de0ce | 699 | gcc_unreachable (); |
0e5921e8 | 700 | |
7bad794a JJ |
701 | /* CPP_COMMENT will appear when compiling with -C. Ignore, except |
702 | when it is a FALLTHROUGH comment, in that case set | |
703 | PREV_FALLTHROUGH flag on the next non-comment token. */ | |
704 | case CPP_COMMENT: | |
705 | if (tok->flags & PREV_FALLTHROUGH) | |
706 | { | |
707 | do | |
708 | { | |
709 | tok = cpp_get_token_with_location (parse_in, loc); | |
710 | type = tok->type; | |
711 | } | |
712 | while (type == CPP_PADDING || type == CPP_COMMENT); | |
713 | add_flags |= PREV_FALLTHROUGH; | |
714 | goto retry_after_at; | |
715 | } | |
9cc54940 AC |
716 | goto retry; |
717 | ||
e6cc3a24 ZW |
718 | default: |
719 | *value = NULL_TREE; | |
720 | break; | |
0e5921e8 ZW |
721 | } |
722 | ||
51e63e60 | 723 | if (cpp_flags) |
ab84748a | 724 | *cpp_flags = tok->flags | add_flags; |
51e63e60 | 725 | |
51e63e60 | 726 | timevar_pop (TV_CPP); |
c22cacf3 | 727 | |
51e63e60 | 728 | return type; |
0e5921e8 | 729 | } |
8d9bfdc5 | 730 | |
ceeedfc1 | 731 | /* Returns the narrowest C-visible unsigned type, starting with the |
5e9754af | 732 | minimum specified by FLAGS, that can fit HIGH:LOW, or itk_none if |
ceeedfc1 | 733 | there isn't one. */ |
5e9754af | 734 | |
ceeedfc1 | 735 | static enum integer_type_kind |
807e902e | 736 | narrowest_unsigned_type (const widest_int &val, unsigned int flags) |
0e5921e8 | 737 | { |
d75d71e0 | 738 | int itk; |
56f48ce9 | 739 | |
ceeedfc1 NB |
740 | if ((flags & CPP_N_WIDTH) == CPP_N_SMALL) |
741 | itk = itk_unsigned_int; | |
742 | else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM) | |
743 | itk = itk_unsigned_long; | |
744 | else | |
745 | itk = itk_unsigned_long_long; | |
e8bbfc4e | 746 | |
ceeedfc1 | 747 | for (; itk < itk_none; itk += 2 /* skip unsigned types */) |
5e9754af | 748 | { |
64c31785 KT |
749 | tree upper; |
750 | ||
751 | if (integer_types[itk] == NULL_TREE) | |
752 | continue; | |
753 | upper = TYPE_MAX_VALUE (integer_types[itk]); | |
5e9754af | 754 | |
807e902e | 755 | if (wi::geu_p (wi::to_widest (upper), val)) |
d75d71e0 | 756 | return (enum integer_type_kind) itk; |
5e9754af | 757 | } |
56f48ce9 | 758 | |
ceeedfc1 NB |
759 | return itk_none; |
760 | } | |
e8bbfc4e | 761 | |
ceeedfc1 NB |
762 | /* Ditto, but narrowest signed type. */ |
763 | static enum integer_type_kind | |
807e902e | 764 | narrowest_signed_type (const widest_int &val, unsigned int flags) |
ceeedfc1 | 765 | { |
d75d71e0 | 766 | int itk; |
e8bbfc4e | 767 | |
ceeedfc1 NB |
768 | if ((flags & CPP_N_WIDTH) == CPP_N_SMALL) |
769 | itk = itk_int; | |
770 | else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM) | |
771 | itk = itk_long; | |
772 | else | |
773 | itk = itk_long_long; | |
e8bbfc4e | 774 | |
ceeedfc1 | 775 | for (; itk < itk_none; itk += 2 /* skip signed types */) |
5e9754af | 776 | { |
64c31785 KT |
777 | tree upper; |
778 | ||
779 | if (integer_types[itk] == NULL_TREE) | |
780 | continue; | |
781 | upper = TYPE_MAX_VALUE (integer_types[itk]); | |
c22cacf3 | 782 | |
807e902e | 783 | if (wi::geu_p (wi::to_widest (upper), val)) |
d75d71e0 | 784 | return (enum integer_type_kind) itk; |
5e9754af | 785 | } |
15e5ad76 | 786 | |
ceeedfc1 NB |
787 | return itk_none; |
788 | } | |
15e5ad76 | 789 | |
ceeedfc1 NB |
790 | /* Interpret TOKEN, an integer with FLAGS as classified by cpplib. */ |
791 | static tree | |
2d7aa578 ESR |
792 | interpret_integer (const cpp_token *token, unsigned int flags, |
793 | enum overflow_type *overflow) | |
ceeedfc1 NB |
794 | { |
795 | tree value, type; | |
796 | enum integer_type_kind itk; | |
797 | cpp_num integer; | |
807e902e | 798 | HOST_WIDE_INT ival[3]; |
ceeedfc1 | 799 | |
2d7aa578 ESR |
800 | *overflow = OT_NONE; |
801 | ||
ceeedfc1 | 802 | integer = cpp_interpret_integer (parse_in, token, flags); |
2d7aa578 ESR |
803 | if (integer.overflow) |
804 | *overflow = OT_OVERFLOW; | |
ceeedfc1 | 805 | |
807e902e KZ |
806 | ival[0] = integer.low; |
807 | ival[1] = integer.high; | |
808 | ival[2] = 0; | |
809 | widest_int wval = widest_int::from_array (ival, 3); | |
810 | ||
ceeedfc1 NB |
811 | /* The type of a constant with a U suffix is straightforward. */ |
812 | if (flags & CPP_N_UNSIGNED) | |
807e902e | 813 | itk = narrowest_unsigned_type (wval, flags); |
0e5921e8 ZW |
814 | else |
815 | { | |
ceeedfc1 NB |
816 | /* The type of a potentially-signed integer constant varies |
817 | depending on the base it's in, the standard in use, and the | |
818 | length suffixes. */ | |
5e9754af | 819 | enum integer_type_kind itk_u |
807e902e | 820 | = narrowest_unsigned_type (wval, flags); |
5e9754af | 821 | enum integer_type_kind itk_s |
807e902e | 822 | = narrowest_signed_type (wval, flags); |
ceeedfc1 NB |
823 | |
824 | /* In both C89 and C99, octal and hex constants may be signed or | |
825 | unsigned, whichever fits tighter. We do not warn about this | |
826 | choice differing from the traditional choice, as the constant | |
827 | is probably a bit pattern and either way will work. */ | |
828 | if ((flags & CPP_N_RADIX) != CPP_N_DECIMAL) | |
829 | itk = MIN (itk_u, itk_s); | |
830 | else | |
0e5921e8 | 831 | { |
ceeedfc1 NB |
832 | /* In C99, decimal constants are always signed. |
833 | In C89, decimal constants that don't fit in long have | |
8d9afc4e | 834 | undefined behavior; we try to make them unsigned long. |
ceeedfc1 NB |
835 | In GCC's extended C89, that last is true of decimal |
836 | constants that don't fit in long long, too. */ | |
837 | ||
838 | itk = itk_s; | |
839 | if (itk_s > itk_u && itk_s > itk_long) | |
0e5921e8 | 840 | { |
ceeedfc1 | 841 | if (!flag_isoc99) |
0e5921e8 | 842 | { |
ceeedfc1 NB |
843 | if (itk_u < itk_unsigned_long) |
844 | itk_u = itk_unsigned_long; | |
845 | itk = itk_u; | |
d4ee4d25 | 846 | warning (0, "this decimal constant is unsigned only in ISO C90"); |
0e5921e8 | 847 | } |
44c21c7f DD |
848 | else |
849 | warning (OPT_Wtraditional, | |
850 | "this decimal constant would be unsigned in ISO C90"); | |
0e5921e8 ZW |
851 | } |
852 | } | |
ceeedfc1 | 853 | } |
56f48ce9 | 854 | |
ceeedfc1 NB |
855 | if (itk == itk_none) |
856 | /* cpplib has already issued a warning for overflow. */ | |
857 | type = ((flags & CPP_N_UNSIGNED) | |
858 | ? widest_unsigned_literal_type_node | |
859 | : widest_integer_literal_type_node); | |
1f69e63c ESR |
860 | else if (flags & CPP_N_SIZE_T) |
861 | { | |
862 | /* itk refers to fundamental types not aliased size types. */ | |
863 | if (flags & CPP_N_UNSIGNED) | |
864 | type = size_type_node; | |
865 | else | |
866 | type = signed_size_type_node; | |
867 | } | |
ceeedfc1 | 868 | else |
9c650d90 MLI |
869 | { |
870 | type = integer_types[itk]; | |
871 | if (itk > itk_unsigned_long | |
872 | && (flags & CPP_N_WIDTH) != CPP_N_LARGE) | |
b8698a0f | 873 | emit_diagnostic |
9c650d90 MLI |
874 | ((c_dialect_cxx () ? cxx_dialect == cxx98 : !flag_isoc99) |
875 | ? DK_PEDWARN : DK_WARNING, | |
876 | input_location, OPT_Wlong_long, | |
b8698a0f | 877 | (flags & CPP_N_UNSIGNED) |
9c650d90 MLI |
878 | ? "integer constant is too large for %<unsigned long%> type" |
879 | : "integer constant is too large for %<long%> type"); | |
880 | } | |
0468bc75 | 881 | |
807e902e | 882 | value = wide_int_to_tree (type, wval); |
e8bbfc4e | 883 | |
ceeedfc1 NB |
884 | /* Convert imaginary to a complex type. */ |
885 | if (flags & CPP_N_IMAGINARY) | |
7d60be94 | 886 | value = build_complex (NULL_TREE, build_int_cst (type, 0), value); |
e8bbfc4e | 887 | |
ceeedfc1 NB |
888 | return value; |
889 | } | |
e8bbfc4e | 890 | |
ceeedfc1 | 891 | /* Interpret TOKEN, a floating point number with FLAGS as classified |
36a85135 | 892 | by cpplib. For C++11 SUFFIX may contain a user-defined literal suffix. */ |
ceeedfc1 | 893 | static tree |
3ce4f9e4 | 894 | interpret_float (const cpp_token *token, unsigned int flags, |
2d7aa578 | 895 | const char *suffix, enum overflow_type *overflow) |
ceeedfc1 NB |
896 | { |
897 | tree type; | |
8ce94e44 | 898 | tree const_type; |
ceeedfc1 NB |
899 | tree value; |
900 | REAL_VALUE_TYPE real; | |
8ce94e44 | 901 | REAL_VALUE_TYPE real_trunc; |
ceeedfc1 NB |
902 | char *copy; |
903 | size_t copylen; | |
e8bbfc4e | 904 | |
2d7aa578 ESR |
905 | *overflow = OT_NONE; |
906 | ||
6ec637a4 JJ |
907 | /* Default (no suffix) depends on whether the FLOAT_CONST_DECIMAL64 |
908 | pragma has been used and is either double or _Decimal64. Types | |
909 | that are not allowed with decimal float default to double. */ | |
839a3b8a JJ |
910 | if (flags & CPP_N_DEFAULT) |
911 | { | |
912 | flags ^= CPP_N_DEFAULT; | |
913 | flags |= CPP_N_MEDIUM; | |
6ec637a4 JJ |
914 | |
915 | if (((flags & CPP_N_HEX) == 0) && ((flags & CPP_N_IMAGINARY) == 0)) | |
916 | { | |
917 | warning (OPT_Wunsuffixed_float_constants, | |
a9c697b8 | 918 | "unsuffixed floating constant"); |
6ec637a4 JJ |
919 | if (float_const_decimal64_p ()) |
920 | flags |= CPP_N_DFLOAT; | |
921 | } | |
839a3b8a JJ |
922 | } |
923 | ||
14c931f1 CF |
924 | /* Decode _Fract and _Accum. */ |
925 | if (flags & CPP_N_FRACT || flags & CPP_N_ACCUM) | |
926 | return interpret_fixed (token, flags); | |
927 | ||
9a8ce21f JG |
928 | /* Decode type based on width and properties. */ |
929 | if (flags & CPP_N_DFLOAT) | |
5b8d9367 JM |
930 | if (!targetm.decimal_float_supported_p ()) |
931 | { | |
932 | error ("decimal floating-point not supported for this target"); | |
933 | return error_mark_node; | |
934 | } | |
935 | else if ((flags & CPP_N_WIDTH) == CPP_N_LARGE) | |
9a8ce21f JG |
936 | type = dfloat128_type_node; |
937 | else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL) | |
938 | type = dfloat32_type_node; | |
939 | else | |
940 | type = dfloat64_type_node; | |
941 | else | |
c77cd3d1 UB |
942 | if (flags & CPP_N_WIDTH_MD) |
943 | { | |
944 | char suffix; | |
ef4bddc2 | 945 | machine_mode mode; |
c77cd3d1 UB |
946 | |
947 | if ((flags & CPP_N_WIDTH_MD) == CPP_N_MD_W) | |
948 | suffix = 'w'; | |
949 | else | |
950 | suffix = 'q'; | |
951 | ||
952 | mode = targetm.c.mode_for_suffix (suffix); | |
953 | if (mode == VOIDmode) | |
954 | { | |
955 | error ("unsupported non-standard suffix on floating constant"); | |
c77cd3d1 UB |
956 | |
957 | return error_mark_node; | |
958 | } | |
fcf73884 | 959 | else |
c1771a20 | 960 | pedwarn (input_location, OPT_Wpedantic, "non-standard suffix on floating constant"); |
c77cd3d1 UB |
961 | |
962 | type = c_common_type_for_mode (mode, 0); | |
b0420889 JJ |
963 | /* For Q suffix, prefer float128t_type_node (__float128) type |
964 | over float128_type_node (_Float128) type if they are distinct. */ | |
965 | if (type == float128_type_node && float128t_type_node) | |
966 | type = float128t_type_node; | |
c77cd3d1 UB |
967 | gcc_assert (type); |
968 | } | |
c65699ef JM |
969 | else if ((flags & (CPP_N_FLOATN | CPP_N_FLOATNX)) != 0) |
970 | { | |
971 | unsigned int n = (flags & CPP_N_WIDTH_FLOATN_NX) >> CPP_FLOATN_SHIFT; | |
972 | bool extended = (flags & CPP_N_FLOATNX) != 0; | |
973 | type = NULL_TREE; | |
974 | for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++) | |
975 | if (floatn_nx_types[i].n == (int) n | |
976 | && floatn_nx_types[i].extended == extended) | |
977 | { | |
978 | type = FLOATN_NX_TYPE_NODE (i); | |
979 | break; | |
980 | } | |
981 | if (type == NULL_TREE) | |
982 | { | |
983 | error ("unsupported non-standard suffix on floating constant"); | |
984 | return error_mark_node; | |
985 | } | |
b0420889 JJ |
986 | else if (c_dialect_cxx () && !extended) |
987 | { | |
988 | if (cxx_dialect < cxx23) | |
989 | pedwarn (input_location, OPT_Wpedantic, | |
990 | "%<f%d%> or %<F%d%> suffix on floating constant only " | |
991 | "available with %<-std=c++2b%> or %<-std=gnu++2b%>", | |
992 | n, n); | |
993 | } | |
c65699ef | 994 | else |
b0420889 JJ |
995 | pedwarn (input_location, OPT_Wpedantic, |
996 | "non-standard suffix on floating constant"); | |
c65699ef | 997 | } |
c77cd3d1 | 998 | else if ((flags & CPP_N_WIDTH) == CPP_N_LARGE) |
ceeedfc1 | 999 | type = long_double_type_node; |
9a8ce21f JG |
1000 | else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL |
1001 | || flag_single_precision_constant) | |
ceeedfc1 | 1002 | type = float_type_node; |
9a8ce21f | 1003 | else |
ceeedfc1 | 1004 | type = double_type_node; |
e8bbfc4e | 1005 | |
b0420889 JJ |
1006 | if (c_dialect_cxx ()) |
1007 | const_type = NULL_TREE; | |
1008 | else | |
1009 | const_type = excess_precision_type (type); | |
8ce94e44 JM |
1010 | if (!const_type) |
1011 | const_type = type; | |
1012 | ||
ceeedfc1 NB |
1013 | /* Copy the constant to a nul-terminated buffer. If the constant |
1014 | has any suffixes, cut them off; REAL_VALUE_ATOF/ REAL_VALUE_HTOF | |
1015 | can't handle them. */ | |
1016 | copylen = token->val.str.len; | |
3ce4f9e4 ESR |
1017 | if (flags & CPP_N_USERDEF) |
1018 | copylen -= strlen (suffix); | |
1019 | else if (flags & CPP_N_DFLOAT) | |
9a8ce21f | 1020 | copylen -= 2; |
b8698a0f | 1021 | else |
9a8ce21f JG |
1022 | { |
1023 | if ((flags & CPP_N_WIDTH) != CPP_N_MEDIUM) | |
c77cd3d1 | 1024 | /* Must be an F or L or machine defined suffix. */ |
9a8ce21f JG |
1025 | copylen--; |
1026 | if (flags & CPP_N_IMAGINARY) | |
1027 | /* I or J suffix. */ | |
1028 | copylen--; | |
c65699ef JM |
1029 | if (flags & CPP_N_FLOATNX) |
1030 | copylen--; | |
1031 | if (flags & (CPP_N_FLOATN | CPP_N_FLOATNX)) | |
1032 | { | |
1033 | unsigned int n = (flags & CPP_N_WIDTH_FLOATN_NX) >> CPP_FLOATN_SHIFT; | |
1034 | while (n > 0) | |
1035 | { | |
1036 | copylen--; | |
1037 | n /= 10; | |
1038 | } | |
1039 | } | |
9a8ce21f | 1040 | } |
ceeedfc1 | 1041 | |
28dab132 | 1042 | copy = (char *) alloca (copylen + 1); |
5ea40269 | 1043 | if (c_dialect_cxx () ? cxx_dialect > cxx11 : flag_isoc2x) |
7057e645 ESR |
1044 | { |
1045 | size_t maxlen = 0; | |
1046 | for (size_t i = 0; i < copylen; ++i) | |
1047 | if (token->val.str.text[i] != '\'') | |
1048 | copy[maxlen++] = token->val.str.text[i]; | |
1049 | copy[maxlen] = '\0'; | |
1050 | } | |
1051 | else | |
1052 | { | |
1053 | memcpy (copy, token->val.str.text, copylen); | |
1054 | copy[copylen] = '\0'; | |
1055 | } | |
ceeedfc1 | 1056 | |
8ce94e44 JM |
1057 | real_from_string3 (&real, copy, TYPE_MODE (const_type)); |
1058 | if (const_type != type) | |
1059 | /* Diagnosing if the result of converting the value with excess | |
1060 | precision to the semantic type would overflow (with associated | |
1061 | double rounding) is more appropriate than diagnosing if the | |
1062 | result of converting the string directly to the semantic type | |
1063 | would overflow. */ | |
1064 | real_convert (&real_trunc, TYPE_MODE (type), &real); | |
fbb18613 | 1065 | |
6d84156b JM |
1066 | /* Both C and C++ require a diagnostic for a floating constant |
1067 | outside the range of representable values of its type. Since we | |
92ef5cf9 MLI |
1068 | have __builtin_inf* to produce an infinity, this is now a |
1069 | mandatory pedwarn if the target does not support infinities. */ | |
8ce94e44 JM |
1070 | if (REAL_VALUE_ISINF (real) |
1071 | || (const_type != type && REAL_VALUE_ISINF (real_trunc))) | |
92ef5cf9 | 1072 | { |
2d7aa578 ESR |
1073 | *overflow = OT_OVERFLOW; |
1074 | if (!(flags & CPP_N_USERDEF)) | |
1075 | { | |
1076 | if (!MODE_HAS_INFINITIES (TYPE_MODE (type))) | |
1077 | pedwarn (input_location, 0, | |
1078 | "floating constant exceeds range of %qT", type); | |
1079 | else | |
1080 | warning (OPT_Woverflow, | |
1081 | "floating constant exceeds range of %qT", type); | |
1082 | } | |
92ef5cf9 MLI |
1083 | } |
1084 | /* We also give a warning if the value underflows. */ | |
624d31fe | 1085 | else if (real_equal (&real, &dconst0) |
2d7aa578 | 1086 | || (const_type != type |
624d31fe | 1087 | && real_equal (&real_trunc, &dconst0))) |
92ef5cf9 MLI |
1088 | { |
1089 | REAL_VALUE_TYPE realvoidmode; | |
2d7aa578 ESR |
1090 | int oflow = real_from_string (&realvoidmode, copy); |
1091 | *overflow = (oflow == 0 ? OT_NONE | |
1092 | : (oflow < 0 ? OT_UNDERFLOW : OT_OVERFLOW)); | |
1093 | if (!(flags & CPP_N_USERDEF)) | |
1094 | { | |
624d31fe | 1095 | if (oflow < 0 || !real_equal (&realvoidmode, &dconst0)) |
2d7aa578 ESR |
1096 | warning (OPT_Woverflow, "floating constant truncated to zero"); |
1097 | } | |
92ef5cf9 | 1098 | } |
fbb18613 | 1099 | |
ceeedfc1 | 1100 | /* Create a node with determined type and value. */ |
8ce94e44 | 1101 | value = build_real (const_type, real); |
ceeedfc1 | 1102 | if (flags & CPP_N_IMAGINARY) |
7273813a | 1103 | { |
269e63b7 KT |
1104 | value = build_complex (NULL_TREE, |
1105 | fold_convert (const_type, | |
1106 | integer_zero_node), value); | |
7273813a JJ |
1107 | if (type != const_type) |
1108 | { | |
1109 | const_type = TREE_TYPE (value); | |
1110 | type = build_complex_type (type); | |
1111 | } | |
1112 | } | |
8ce94e44 JM |
1113 | |
1114 | if (type != const_type) | |
14e18d71 | 1115 | value = build1_loc (token->src_loc, EXCESS_PRECISION_EXPR, type, value); |
e8bbfc4e | 1116 | |
0e5921e8 | 1117 | return value; |
0e5921e8 | 1118 | } |
e8bbfc4e | 1119 | |
14c931f1 CF |
1120 | /* Interpret TOKEN, a fixed-point number with FLAGS as classified |
1121 | by cpplib. */ | |
1122 | ||
1123 | static tree | |
1124 | interpret_fixed (const cpp_token *token, unsigned int flags) | |
1125 | { | |
1126 | tree type; | |
1127 | tree value; | |
1128 | FIXED_VALUE_TYPE fixed; | |
1129 | char *copy; | |
1130 | size_t copylen; | |
1131 | ||
1132 | copylen = token->val.str.len; | |
1133 | ||
1134 | if (flags & CPP_N_FRACT) /* _Fract. */ | |
1135 | { | |
1136 | if (flags & CPP_N_UNSIGNED) /* Unsigned _Fract. */ | |
1137 | { | |
1138 | if ((flags & CPP_N_WIDTH) == CPP_N_LARGE) | |
1139 | { | |
1140 | type = unsigned_long_long_fract_type_node; | |
1141 | copylen -= 4; | |
1142 | } | |
1143 | else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM) | |
1144 | { | |
1145 | type = unsigned_long_fract_type_node; | |
1146 | copylen -= 3; | |
1147 | } | |
1148 | else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL) | |
1149 | { | |
1150 | type = unsigned_short_fract_type_node; | |
1151 | copylen -= 3; | |
1152 | } | |
1153 | else | |
1154 | { | |
1155 | type = unsigned_fract_type_node; | |
1156 | copylen -= 2; | |
1157 | } | |
1158 | } | |
1159 | else /* Signed _Fract. */ | |
1160 | { | |
1161 | if ((flags & CPP_N_WIDTH) == CPP_N_LARGE) | |
1162 | { | |
1163 | type = long_long_fract_type_node; | |
1164 | copylen -= 3; | |
1165 | } | |
1166 | else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM) | |
1167 | { | |
1168 | type = long_fract_type_node; | |
1169 | copylen -= 2; | |
1170 | } | |
1171 | else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL) | |
1172 | { | |
1173 | type = short_fract_type_node; | |
1174 | copylen -= 2; | |
1175 | } | |
1176 | else | |
1177 | { | |
1178 | type = fract_type_node; | |
1179 | copylen --; | |
1180 | } | |
1181 | } | |
1182 | } | |
1183 | else /* _Accum. */ | |
1184 | { | |
1185 | if (flags & CPP_N_UNSIGNED) /* Unsigned _Accum. */ | |
1186 | { | |
1187 | if ((flags & CPP_N_WIDTH) == CPP_N_LARGE) | |
1188 | { | |
1189 | type = unsigned_long_long_accum_type_node; | |
1190 | copylen -= 4; | |
1191 | } | |
1192 | else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM) | |
1193 | { | |
1194 | type = unsigned_long_accum_type_node; | |
1195 | copylen -= 3; | |
1196 | } | |
1197 | else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL) | |
1198 | { | |
1199 | type = unsigned_short_accum_type_node; | |
1200 | copylen -= 3; | |
1201 | } | |
1202 | else | |
1203 | { | |
1204 | type = unsigned_accum_type_node; | |
1205 | copylen -= 2; | |
1206 | } | |
1207 | } | |
1208 | else /* Signed _Accum. */ | |
1209 | { | |
1210 | if ((flags & CPP_N_WIDTH) == CPP_N_LARGE) | |
1211 | { | |
1212 | type = long_long_accum_type_node; | |
1213 | copylen -= 3; | |
1214 | } | |
1215 | else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM) | |
1216 | { | |
1217 | type = long_accum_type_node; | |
1218 | copylen -= 2; | |
1219 | } | |
1220 | else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL) | |
1221 | { | |
1222 | type = short_accum_type_node; | |
1223 | copylen -= 2; | |
1224 | } | |
1225 | else | |
1226 | { | |
1227 | type = accum_type_node; | |
1228 | copylen --; | |
1229 | } | |
1230 | } | |
1231 | } | |
1232 | ||
1233 | copy = (char *) alloca (copylen + 1); | |
1234 | memcpy (copy, token->val.str.text, copylen); | |
1235 | copy[copylen] = '\0'; | |
1236 | ||
b397965c | 1237 | fixed_from_string (&fixed, copy, SCALAR_TYPE_MODE (type)); |
14c931f1 CF |
1238 | |
1239 | /* Create a node with determined type and value. */ | |
1240 | value = build_fixed (type, fixed); | |
1241 | ||
1242 | return value; | |
1243 | } | |
1244 | ||
2c6e3f55 JJ |
1245 | /* Convert a series of STRING, WSTRING, STRING16, STRING32 and/or |
1246 | UTF8STRING tokens into a tree, performing string constant | |
a9546771 NP |
1247 | concatenation. TOK is the first of these. VALP is the location to |
1248 | write the string into. OBJC_STRING indicates whether an '@' token | |
1249 | preceded the incoming token (in that case, the strings can either | |
1250 | be ObjC strings, preceded by a single '@', or normal strings, not | |
1251 | preceded by '@'. The result will be a CPP_OBJC_STRING). Returns | |
1252 | the CPP token type of the result (CPP_STRING, CPP_WSTRING, | |
2c6e3f55 | 1253 | CPP_STRING32, CPP_STRING16, CPP_UTF8STRING, or CPP_OBJC_STRING). |
e6cc3a24 ZW |
1254 | |
1255 | This is unfortunately more work than it should be. If any of the | |
1256 | strings in the series has an L prefix, the result is a wide string | |
1257 | (6.4.5p4). Whether or not the result is a wide string affects the | |
1258 | meaning of octal and hexadecimal escapes (6.4.4.4p6,9). But escape | |
1259 | sequences do not continue across the boundary between two strings in | |
1260 | a series (6.4.5p7), so we must not lose the boundaries. Therefore | |
1261 | cpp_interpret_string takes a vector of cpp_string structures, which | |
1262 | we must arrange to provide. */ | |
1263 | ||
1264 | static enum cpp_ttype | |
46c2514e | 1265 | lex_string (const cpp_token *tok, tree *valp, bool objc_string, bool translate) |
0e5921e8 ZW |
1266 | { |
1267 | tree value; | |
51e63e60 | 1268 | size_t concats = 0; |
e6cc3a24 | 1269 | struct obstack str_ob; |
88fa5555 | 1270 | struct obstack loc_ob; |
e6cc3a24 | 1271 | cpp_string istr; |
b6baa67d | 1272 | enum cpp_ttype type = tok->type; |
2f6e4e97 | 1273 | |
e6cc3a24 ZW |
1274 | /* Try to avoid the overhead of creating and destroying an obstack |
1275 | for the common case of just one string. */ | |
1276 | cpp_string str = tok->val.str; | |
88fa5555 | 1277 | location_t init_loc = tok->src_loc; |
e6cc3a24 | 1278 | cpp_string *strs = &str; |
88fa5555 | 1279 | location_t *locs = NULL; |
e9a25f70 | 1280 | |
a9546771 NP |
1281 | /* objc_at_sign_was_seen is only used when doing Objective-C string |
1282 | concatenation. It is 'true' if we have seen an '@' before the | |
1283 | current string, and 'false' if not. We must see exactly one or | |
1284 | zero '@' before each string. */ | |
1285 | bool objc_at_sign_was_seen = false; | |
1286 | ||
51e63e60 NS |
1287 | retry: |
1288 | tok = cpp_get_token (parse_in); | |
1289 | switch (tok->type) | |
e6cc3a24 | 1290 | { |
51e63e60 NS |
1291 | case CPP_PADDING: |
1292 | goto retry; | |
1293 | case CPP_ATSIGN: | |
a9546771 | 1294 | if (objc_string) |
0e5921e8 | 1295 | { |
a9546771 NP |
1296 | if (objc_at_sign_was_seen) |
1297 | error ("repeated %<@%> before Objective-C string"); | |
1298 | ||
1299 | objc_at_sign_was_seen = true; | |
51e63e60 NS |
1300 | goto retry; |
1301 | } | |
1302 | /* FALLTHROUGH */ | |
c22cacf3 | 1303 | |
51e63e60 NS |
1304 | default: |
1305 | break; | |
c22cacf3 | 1306 | |
51e63e60 | 1307 | case CPP_WSTRING: |
b6baa67d KVH |
1308 | case CPP_STRING16: |
1309 | case CPP_STRING32: | |
2c6e3f55 | 1310 | case CPP_UTF8STRING: |
b6baa67d KVH |
1311 | if (type != tok->type) |
1312 | { | |
1313 | if (type == CPP_STRING) | |
1314 | type = tok->type; | |
1315 | else | |
1316 | error ("unsupported non-standard concatenation of string literals"); | |
1317 | } | |
88fa5555 | 1318 | /* FALLTHROUGH */ |
c22cacf3 | 1319 | |
51e63e60 NS |
1320 | case CPP_STRING: |
1321 | if (!concats) | |
1322 | { | |
1323 | gcc_obstack_init (&str_ob); | |
88fa5555 | 1324 | gcc_obstack_init (&loc_ob); |
51e63e60 | 1325 | obstack_grow (&str_ob, &str, sizeof (cpp_string)); |
88fa5555 | 1326 | obstack_grow (&loc_ob, &init_loc, sizeof (location_t)); |
0e5921e8 | 1327 | } |
c22cacf3 | 1328 | |
51e63e60 NS |
1329 | concats++; |
1330 | obstack_grow (&str_ob, &tok->val.str, sizeof (cpp_string)); | |
88fa5555 DM |
1331 | obstack_grow (&loc_ob, &tok->src_loc, sizeof (location_t)); |
1332 | ||
a9546771 NP |
1333 | if (objc_string) |
1334 | objc_at_sign_was_seen = false; | |
51e63e60 | 1335 | goto retry; |
e8bbfc4e RK |
1336 | } |
1337 | ||
a9546771 NP |
1338 | /* It is an error if we saw a '@' with no following string. */ |
1339 | if (objc_at_sign_was_seen) | |
1340 | error ("stray %<@%> in program"); | |
1341 | ||
e6cc3a24 ZW |
1342 | /* We have read one more token than we want. */ |
1343 | _cpp_backup_tokens (parse_in, 1); | |
51e63e60 | 1344 | if (concats) |
88fa5555 DM |
1345 | { |
1346 | strs = XOBFINISH (&str_ob, cpp_string *); | |
1347 | locs = XOBFINISH (&loc_ob, location_t *); | |
1348 | } | |
e6cc3a24 | 1349 | |
8400e75e | 1350 | if (concats && !objc_string && !in_system_header_at (input_location)) |
44c21c7f DD |
1351 | warning (OPT_Wtraditional, |
1352 | "traditional C rejects string constant concatenation"); | |
e8bbfc4e | 1353 | |
46c2514e | 1354 | if ((translate |
21526606 | 1355 | ? cpp_interpret_string : cpp_interpret_string_notranslate) |
b6baa67d | 1356 | (parse_in, strs, concats + 1, &istr, type)) |
0e5921e8 | 1357 | { |
5f754896 | 1358 | value = build_string (istr.len, (const char *) istr.text); |
b1d5455a | 1359 | free (CONST_CAST (unsigned char *, istr.text)); |
88fa5555 DM |
1360 | if (concats) |
1361 | { | |
1362 | gcc_assert (locs); | |
1363 | gcc_assert (g_string_concat_db); | |
1364 | g_string_concat_db->record_string_concatenation (concats + 1, locs); | |
1365 | } | |
0e5921e8 ZW |
1366 | } |
1367 | else | |
1368 | { | |
e6cc3a24 ZW |
1369 | /* Callers cannot generally handle error_mark_node in this context, |
1370 | so return the empty string instead. cpp_interpret_string has | |
1371 | issued an error. */ | |
b6baa67d KVH |
1372 | switch (type) |
1373 | { | |
1374 | default: | |
1375 | case CPP_STRING: | |
2c6e3f55 | 1376 | case CPP_UTF8STRING: |
703837b2 TH |
1377 | if (type == CPP_UTF8STRING && flag_char8_t) |
1378 | { | |
1379 | value = build_string (TYPE_PRECISION (char8_type_node) | |
1380 | / TYPE_PRECISION (char_type_node), | |
1381 | ""); /* char8_t is 8 bits */ | |
1382 | } | |
1383 | else | |
1384 | value = build_string (1, ""); | |
b6baa67d KVH |
1385 | break; |
1386 | case CPP_STRING16: | |
1387 | value = build_string (TYPE_PRECISION (char16_type_node) | |
1388 | / TYPE_PRECISION (char_type_node), | |
1389 | "\0"); /* char16_t is 16 bits */ | |
1390 | break; | |
1391 | case CPP_STRING32: | |
1392 | value = build_string (TYPE_PRECISION (char32_type_node) | |
1393 | / TYPE_PRECISION (char_type_node), | |
1394 | "\0\0\0"); /* char32_t is 32 bits */ | |
1395 | break; | |
1396 | case CPP_WSTRING: | |
1397 | value = build_string (TYPE_PRECISION (wchar_type_node) | |
1398 | / TYPE_PRECISION (char_type_node), | |
1399 | "\0\0\0"); /* widest supported wchar_t | |
1400 | is 32 bits */ | |
1401 | break; | |
1402 | } | |
0e5921e8 ZW |
1403 | } |
1404 | ||
b6baa67d KVH |
1405 | switch (type) |
1406 | { | |
1407 | default: | |
1408 | case CPP_STRING: | |
1409 | TREE_TYPE (value) = char_array_type_node; | |
1410 | break; | |
2d91f79d TH |
1411 | case CPP_UTF8STRING: |
1412 | if (flag_char8_t) | |
1413 | TREE_TYPE (value) = char8_array_type_node; | |
1414 | else | |
1415 | TREE_TYPE (value) = char_array_type_node; | |
1416 | break; | |
b6baa67d KVH |
1417 | case CPP_STRING16: |
1418 | TREE_TYPE (value) = char16_array_type_node; | |
1419 | break; | |
1420 | case CPP_STRING32: | |
1421 | TREE_TYPE (value) = char32_array_type_node; | |
1422 | break; | |
1423 | case CPP_WSTRING: | |
1424 | TREE_TYPE (value) = wchar_array_type_node; | |
1425 | } | |
e6cc3a24 | 1426 | *valp = fix_string_type (value); |
0e5921e8 | 1427 | |
51e63e60 | 1428 | if (concats) |
88fa5555 DM |
1429 | { |
1430 | obstack_free (&str_ob, 0); | |
1431 | obstack_free (&loc_ob, 0); | |
1432 | } | |
e6cc3a24 | 1433 | |
b6baa67d | 1434 | return objc_string ? CPP_OBJC_STRING : type; |
e8bbfc4e RK |
1435 | } |
1436 | ||
c8a96070 | 1437 | /* Converts a (possibly wide) character constant token into a tree. */ |
0e5921e8 | 1438 | static tree |
2f6e4e97 | 1439 | lex_charconst (const cpp_token *token) |
e8bbfc4e | 1440 | { |
4268e8bb | 1441 | cppchar_t result; |
9340544b | 1442 | tree type, value; |
c8a96070 | 1443 | unsigned int chars_seen; |
098b9c46 | 1444 | int unsignedp = 0; |
ceeedfc1 | 1445 | |
a5a49440 | 1446 | result = cpp_interpret_charconst (parse_in, token, |
2f6e4e97 | 1447 | &chars_seen, &unsignedp); |
9340544b | 1448 | |
4268e8bb NB |
1449 | if (token->type == CPP_WCHAR) |
1450 | type = wchar_type_node; | |
b6baa67d KVH |
1451 | else if (token->type == CPP_CHAR32) |
1452 | type = char32_type_node; | |
1453 | else if (token->type == CPP_CHAR16) | |
1454 | type = char16_type_node; | |
fe95b036 | 1455 | else if (token->type == CPP_UTF8CHAR) |
2d91f79d | 1456 | { |
703837b2 | 1457 | if (flag_char8_t) |
2d91f79d TH |
1458 | type = char8_type_node; |
1459 | else | |
1460 | type = char_type_node; | |
1461 | } | |
4268e8bb NB |
1462 | /* In C, a character constant has type 'int'. |
1463 | In C++ 'char', but multi-char charconsts have type 'int'. */ | |
37fa72e9 | 1464 | else if (!c_dialect_cxx () || chars_seen > 1) |
4268e8bb NB |
1465 | type = integer_type_node; |
1466 | else | |
1467 | type = char_type_node; | |
9340544b | 1468 | |
4a90aeeb NS |
1469 | /* Cast to cppchar_signed_t to get correct sign-extension of RESULT |
1470 | before possibly widening to HOST_WIDE_INT for build_int_cst. */ | |
1471 | if (unsignedp || (cppchar_signed_t) result >= 0) | |
807e902e | 1472 | value = build_int_cst (type, result); |
4a90aeeb | 1473 | else |
807e902e | 1474 | value = build_int_cst (type, (cppchar_signed_t) result); |
4a90aeeb | 1475 | |
0e5921e8 | 1476 | return value; |
e8bbfc4e | 1477 | } |
de67c4c3 DM |
1478 | |
1479 | /* Helper function for c_parser_peek_conflict_marker | |
1480 | and cp_lexer_peek_conflict_marker. | |
1481 | Given a possible conflict marker token of kind TOK1_KIND | |
1482 | consisting of a pair of characters, get the token kind for the | |
1483 | standalone final character. */ | |
1484 | ||
1485 | enum cpp_ttype | |
1486 | conflict_marker_get_final_tok_kind (enum cpp_ttype tok1_kind) | |
1487 | { | |
1488 | switch (tok1_kind) | |
1489 | { | |
1490 | default: gcc_unreachable (); | |
1491 | case CPP_LSHIFT: | |
1492 | /* "<<" and '<' */ | |
1493 | return CPP_LESS; | |
1494 | ||
1495 | case CPP_EQ_EQ: | |
1496 | /* "==" and '=' */ | |
1497 | return CPP_EQ; | |
1498 | ||
1499 | case CPP_RSHIFT: | |
1500 | /* ">>" and '>' */ | |
1501 | return CPP_GREATER; | |
1502 | } | |
1503 | } |