Debian Codegraph @

gcc/c-family/c-lex.cc

raw
1 /* Mainly the interface between cpplib and the C front ends.
2 Copyright (C) 1987-2025 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 "target.h"
24 #include "c-common.h"
25 #include "timevar.h"
26 #include "stringpool.h"
27 #include "stor-layout.h"
28 #include "c-pragma.h"
29 #include "debug.h"
30 #include "flags.h"
31 #include "file-prefix-map.h" /* remap_macro_filename() */
32 #include "langhooks.h"
33 #include "attribs.h"
34 #include "rich-location.h"
35
36 /* We may keep statistics about how long which files took to compile. */
37 static int header_time, body_time;
38 static splay_tree file_info_tree;
39
40 int pending_lang_change; /* If we need to switch languages - C++ only */
41 int c_header_level; /* depth in C headers - C++ only */
42
43 static tree interpret_integer (const cpp_token *, unsigned int,
44 enum overflow_type *);
45 static tree interpret_float (const cpp_token *, unsigned int, const char *,
46 enum overflow_type *);
47 static tree interpret_fixed (const cpp_token *, unsigned int);
48 static enum integer_type_kind narrowest_unsigned_type
49 (const widest_int &, unsigned int);
50 static enum integer_type_kind narrowest_signed_type
51 (const widest_int &, unsigned int);
52 static enum cpp_ttype lex_string (const cpp_token *, tree *, bool, bool);
53 static tree lex_charconst (const cpp_token *);
54 static void update_header_times (const char *);
55 static int dump_one_header (splay_tree_node, void *);
56 static void cb_line_change (cpp_reader *, const cpp_token *, int);
57 static void cb_ident (cpp_reader *, location_t, const cpp_string *);
58 static void cb_def_pragma (cpp_reader *, location_t);
59 static void cb_define (cpp_reader *, location_t, cpp_hashnode *);
60 static void cb_undef (cpp_reader *, location_t, cpp_hashnode *);
61
62 void
63 init_c_lex (void)
64 {
65 struct c_fileinfo *toplevel;
66
67 /* The get_fileinfo data structure must be initialized before
68 cpp_read_main_file is called. */
69 toplevel = get_fileinfo ("<top level>");
70 if (flag_detailed_statistics)
71 {
72 header_time = 0;
73 body_time = get_run_time ();
74 toplevel->time = body_time;
75 }
76
77 struct cpp_callbacks *cb = cpp_get_callbacks (parse_in);
78
79 cb->line_change = cb_line_change;
80 cb->ident = cb_ident;
81 cb->def_pragma = cb_def_pragma;
82 cb->valid_pch = c_common_valid_pch;
83 cb->read_pch = c_common_read_pch;
84 cb->has_attribute = c_common_has_attribute;
85 cb->has_builtin = c_common_has_builtin;
86 cb->has_feature = c_common_has_feature;
87 cb->get_source_date_epoch = cb_get_source_date_epoch;
88 cb->get_suggestion = cb_get_suggestion;
89 cb->remap_filename = remap_macro_filename;
90
91 /* Set the debug callbacks if we can use them. */
92 if ((debug_info_level == DINFO_LEVEL_VERBOSE
93 && dwarf_debuginfo_p ())
94 || flag_dump_go_spec != NULL)
95 {
96 cb->define = cb_define;
97 cb->undef = cb_undef;
98 }
99 }
100
101 struct c_fileinfo *
102 get_fileinfo (const char *name)
103 {
104 splay_tree_node n;
105 struct c_fileinfo *fi;
106
107 if (!file_info_tree)
108 file_info_tree = splay_tree_new (splay_tree_compare_strings,
109 0,
110 splay_tree_delete_pointers);
111
112 n = splay_tree_lookup (file_info_tree, (splay_tree_key) name);
113 if (n)
114 return (struct c_fileinfo *) n->value;
115
116 fi = XNEW (struct c_fileinfo);
117 fi->time = 0;
118 fi->interface_only = 0;
119 fi->interface_unknown = 1;
120 splay_tree_insert (file_info_tree, (splay_tree_key) name,
121 (splay_tree_value) fi);
122 return fi;
123 }
124
125 static void
126 update_header_times (const char *name)
127 {
128 /* Changing files again. This means currently collected time
129 is charged against header time, and body time starts back at 0. */
130 if (flag_detailed_statistics)
131 {
132 int this_time = get_run_time ();
133 struct c_fileinfo *file = get_fileinfo (name);
134 header_time += this_time - body_time;
135 file->time += this_time - body_time;
136 body_time = this_time;
137 }
138 }
139
140 static int
141 dump_one_header (splay_tree_node n, void * ARG_UNUSED (dummy))
142 {
143 print_time ((const char *) n->key,
144 ((struct c_fileinfo *) n->value)->time);
145 return 0;
146 }
147
148 void
149 dump_time_statistics (void)
150 {
151 struct c_fileinfo *file = get_fileinfo (LOCATION_FILE (input_location));
152 int this_time = get_run_time ();
153 file->time += this_time - body_time;
154
155 fprintf (stderr, "\n******\n");
156 print_time ("header files (total)", header_time);
157 print_time ("main file (total)", this_time - body_time);
158 fprintf (stderr, "ratio = %g : 1\n",
159 (double) header_time / (double) (this_time - body_time));
160 fprintf (stderr, "\n******\n");
161
162 splay_tree_foreach (file_info_tree, dump_one_header, 0);
163 }
164
165 static void
166 cb_ident (cpp_reader * ARG_UNUSED (pfile),
167 location_t ARG_UNUSED (line),
168 const cpp_string * ARG_UNUSED (str))
169 {
170 if (!flag_no_ident)
171 {
172 /* Convert escapes in the string. */
173 cpp_string cstr = { 0, 0 };
174 if (cpp_interpret_string (pfile, str, 1, &cstr, CPP_STRING))
175 {
176 targetm.asm_out.output_ident ((const char *) cstr.text);
177 free (CONST_CAST (unsigned char *, cstr.text));
178 }
179 }
180 }
181
182 /* Called at the start of every non-empty line. TOKEN is the first
183 lexed token on the line. Used for diagnostic line numbers. */
184 static void
185 cb_line_change (cpp_reader * ARG_UNUSED (pfile), const cpp_token *token,
186 int parsing_args)
187 {
188 if (token->type != CPP_EOF && !parsing_args)
189 input_location = token->src_loc;
190 }
191
192 void
193 fe_file_change (const line_map_ordinary *new_map)
194 {
195 if (new_map == NULL)
196 return;
197
198 if (new_map->reason == LC_ENTER)
199 {
200 /* Don't stack the main buffer on the input stack;
201 we already did in compile_file. */
202 if (!MAIN_FILE_P (new_map))
203 {
204 location_t included_at = linemap_included_from (new_map);
205 int line = 0;
206 if (included_at > BUILTINS_LOCATION)
207 line = SOURCE_LINE (new_map - 1, included_at);
208
209 input_location = new_map->start_location;
210 (*debug_hooks->start_source_file) (line, LINEMAP_FILE (new_map));
211 #ifdef SYSTEM_IMPLICIT_EXTERN_C
212 if (c_header_level)
213 ++c_header_level;
214 else if (LINEMAP_SYSP (new_map) == 2)
215 {
216 c_header_level = 1;
217 ++pending_lang_change;
218 }
219 #endif
220 }
221 }
222 else if (new_map->reason == LC_LEAVE)
223 {
224 #ifdef SYSTEM_IMPLICIT_EXTERN_C
225 if (c_header_level && --c_header_level == 0)
226 {
227 if (LINEMAP_SYSP (new_map) == 2)
228 warning (0, "badly nested C headers from preprocessor");
229 --pending_lang_change;
230 }
231 #endif
232 input_location = new_map->start_location;
233
234 (*debug_hooks->end_source_file) (LINEMAP_LINE (new_map));
235 }
236
237 update_header_times (LINEMAP_FILE (new_map));
238 input_location = new_map->start_location;
239 }
240
241 static void
242 cb_def_pragma (cpp_reader *pfile, location_t loc)
243 {
244 /* Issue a warning message if we have been asked to do so. Ignore
245 unknown pragmas in system headers unless an explicit
246 -Wunknown-pragmas has been given. */
247 if (warn_unknown_pragmas > in_system_header_at (input_location))
248 {
249 const unsigned char *space, *name;
250 const cpp_token *s;
251 location_t fe_loc = loc;
252
253 space = name = (const unsigned char *) "";
254
255 /* N.B. It's fine to call cpp_get_token () directly here (rather than our
256 local wrapper get_token ()), because this callback is not used with
257 flag_preprocess_only==true. */
258 s = cpp_get_token (pfile);
259 if (s->type != CPP_EOF)
260 {
261 space = cpp_token_as_text (pfile, s);
262 s = cpp_get_token (pfile);
263 if (s->type == CPP_NAME)
264 name = cpp_token_as_text (pfile, s);
265 }
266
267 warning_at (fe_loc, OPT_Wunknown_pragmas, "ignoring %<#pragma %s %s%>",
268 space, name);
269 }
270 }
271
272 /* #define callback for DWARF and DWARF2 debug info. */
273 static void
274 cb_define (cpp_reader *pfile, location_t loc, cpp_hashnode *node)
275 {
276 const struct line_map *map = linemap_lookup (line_table, loc);
277 (*debug_hooks->define) (SOURCE_LINE (linemap_check_ordinary (map), loc),
278 (const char *) cpp_macro_definition (pfile, node));
279 }
280
281 /* #undef callback for DWARF and DWARF2 debug info. */
282 static void
283 cb_undef (cpp_reader *pfile, location_t loc, cpp_hashnode *node)
284 {
285 if (lang_hooks.preprocess_undef)
286 lang_hooks.preprocess_undef (pfile, loc, node);
287
288 const struct line_map *map = linemap_lookup (line_table, loc);
289 (*debug_hooks->undef) (SOURCE_LINE (linemap_check_ordinary (map), loc),
290 (const char *) NODE_NAME (node));
291 }
292
293 /* Wrapper around cpp_get_token_with_location to stream the token to the
294 preprocessor so it can output it. This is necessary with
295 flag_preprocess_only if we are obtaining tokens here instead of from the loop
296 in c-ppoutput.cc, such as while processing a #pragma. */
297
298 static const cpp_token *
299 get_token (cpp_reader *pfile, location_t *loc = nullptr)
300 {
301 if (flag_preprocess_only)
302 {
303 location_t x;
304 if (!loc)
305 loc = &x;
306 const auto tok = cpp_get_token_with_location (pfile, loc);
307 c_pp_stream_token (pfile, tok, *loc);
308 return tok;
309 }
310 else
311 return cpp_get_token_with_location (pfile, loc);
312 }
313
314 /* Wrapper around cpp_get_token to skip CPP_PADDING tokens
315 and not consume CPP_EOF. This does not perform the optional
316 streaming in preprocess_only mode, so is suitable to be used
317 when processing builtin expansions such as c_common_has_attribute. */
318
319 static const cpp_token *
320 get_token_no_padding (cpp_reader *pfile)
321 {
322 for (;;)
323 {
324 const cpp_token *ret = cpp_peek_token (pfile, 0);
325 if (ret->type == CPP_EOF)
326 return ret;
327 ret = cpp_get_token (pfile);
328 if (ret->type != CPP_PADDING)
329 return ret;
330 }
331 }
332
333 /* Callback for has_attribute. */
334 int
335 c_common_has_attribute (cpp_reader *pfile, bool std_syntax)
336 {
337 int result = 0;
338 tree attr_name = NULL_TREE;
339 const cpp_token *token;
340
341 token = get_token_no_padding (pfile);
342 if (token->type != CPP_OPEN_PAREN)
343 {
344 cpp_error (pfile, CPP_DL_ERROR,
345 "missing %<(%> after %<__has_attribute%>");
346 return 0;
347 }
348 token = get_token_no_padding (pfile);
349 if (token->type == CPP_NAME)
350 {
351 attr_name = get_identifier ((const char *)
352 cpp_token_as_text (pfile, token));
353 attr_name = canonicalize_attr_name (attr_name);
354 bool have_scope = false;
355 int idx = 0;
356 const cpp_token *nxt_token;
357 do
358 nxt_token = cpp_peek_token (pfile, idx++);
359 while (nxt_token->type == CPP_PADDING);
360 if (!c_dialect_cxx ()
361 && nxt_token->type == CPP_COLON
362 && (nxt_token->flags & COLON_SCOPE) != 0)
363 {
364 const cpp_token *prev_token = nxt_token;
365 do
366 nxt_token = cpp_peek_token (pfile, idx++);
367 while (nxt_token->type == CPP_PADDING);
368 if (nxt_token->type == CPP_COLON)
369 {
370 /* __has_attribute (vendor::attr) in -std=c17 etc. modes.
371 :: isn't CPP_SCOPE but 2 CPP_COLON tokens, where the
372 first one should have COLON_SCOPE flag to distinguish
373 it from : :. */
374 have_scope = true;
375 get_token_no_padding (pfile); // Eat first colon.
376 }
377 else
378 nxt_token = prev_token;
379 }
380 if (nxt_token->type == CPP_SCOPE || have_scope)
381 {
382 have_scope = true;
383 get_token_no_padding (pfile); // Eat scope.
384 nxt_token = get_token_no_padding (pfile);
385 if (nxt_token->type == CPP_NAME)
386 {
387 tree attr_ns = attr_name;
388 tree attr_id
389 = get_identifier ((const char *)
390 cpp_token_as_text (pfile, nxt_token));
391 attr_id = canonicalize_attr_name (attr_id);
392 /* OpenMP attributes need special handling. */
393 if ((flag_openmp || flag_openmp_simd)
394 && is_attribute_p ("omp", attr_ns)
395 && (is_attribute_p ("directive", attr_id)
396 || is_attribute_p ("sequence", attr_id)
397 || is_attribute_p ("decl", attr_id)))
398 result = 1;
399 if (result)
400 attr_name = NULL_TREE;
401 else
402 attr_name = build_tree_list (attr_ns, attr_id);
403 }
404 else
405 {
406 cpp_error (pfile, CPP_DL_ERROR,
407 "attribute identifier required after scope");
408 attr_name = NULL_TREE;
409 }
410 }
411 else
412 {
413 /* Some standard attributes need special handling. */
414 if (c_dialect_cxx ())
415 {
416 if (is_attribute_p ("noreturn", attr_name))
417 result = 200809;
418 else if (is_attribute_p ("deprecated", attr_name))
419 result = 201309;
420 else if (is_attribute_p ("maybe_unused", attr_name)
421 || is_attribute_p ("fallthrough", attr_name))
422 result = 201603;
423 else if (is_attribute_p ("no_unique_address", attr_name)
424 || is_attribute_p ("likely", attr_name)
425 || is_attribute_p ("unlikely", attr_name))
426 result = 201803;
427 else if (is_attribute_p ("nodiscard", attr_name))
428 result = 201907;
429 else if (is_attribute_p ("assume", attr_name))
430 result = 202207;
431 else if (is_attribute_p ("init_priority", attr_name))
432 {
433 /* The (non-standard) init_priority attribute is always
434 included in the attribute table, but we don't want to
435 advertise the attribute unless the target actually
436 supports init priorities. */
437 result = SUPPORTS_INIT_PRIORITY ? 1 : 0;
438 attr_name = NULL_TREE;
439 }
440 }
441 else
442 {
443 if (is_attribute_p ("deprecated", attr_name)
444 || is_attribute_p ("fallthrough", attr_name)
445 || is_attribute_p ("maybe_unused", attr_name)
446 || is_attribute_p ("nodiscard", attr_name)
447 || is_attribute_p ("noreturn", attr_name)
448 || is_attribute_p ("_Noreturn", attr_name)
449 || is_attribute_p ("reproducible", attr_name)
450 || is_attribute_p ("unsequenced", attr_name))
451 result = 202311;
452 }
453 if (result)
454 attr_name = NULL_TREE;
455 }
456 if (attr_name && (have_scope || !std_syntax))
457 {
458 init_attributes ();
459 const struct attribute_spec *attr = lookup_attribute_spec (attr_name);
460 if (attr)
461 result = 1;
462 }
463 }
464 else
465 {
466 cpp_error (pfile, CPP_DL_ERROR,
467 "macro %<__has_attribute%> requires an identifier");
468 return 0;
469 }
470
471 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
472 cpp_error (pfile, CPP_DL_ERROR,
473 "missing %<)%> after %<__has_attribute%>");
474
475 return result;
476 }
477
478 /* Helper for __has_{builtin,feature,extension}. */
479
480 static const char *
481 c_common_lex_availability_macro (cpp_reader *pfile, const char *builtin)
482 {
483 const cpp_token *token = get_token_no_padding (pfile);
484 if (token->type != CPP_OPEN_PAREN)
485 {
486 cpp_error (pfile, CPP_DL_ERROR,
487 "missing %<(%> after %<__has_%s%>", builtin);
488 return 0;
489 }
490
491 const char *name = "";
492 token = get_token_no_padding (pfile);
493 if (token->type == CPP_NAME)
494 {
495 name = (const char *) cpp_token_as_text (pfile, token);
496 token = get_token_no_padding (pfile);
497 if (token->type != CPP_CLOSE_PAREN)
498 {
499 cpp_error (pfile, CPP_DL_ERROR,
500 "expected %<)%> after %qs", name);
501 name = "";
502 }
503 }
504 else
505 {
506 cpp_error (pfile, CPP_DL_ERROR,
507 "macro %<__has_%s%> requires an identifier", builtin);
508 if (token->type == CPP_CLOSE_PAREN)
509 return 0;
510 }
511
512 /* Consume tokens up to the closing parenthesis, including any nested
513 pairs of parentheses, to avoid confusing redundant errors. */
514 for (unsigned nparen = 1; ; token = get_token_no_padding (pfile))
515 {
516 if (token->type == CPP_OPEN_PAREN)
517 ++nparen;
518 else if (token->type == CPP_CLOSE_PAREN)
519 --nparen;
520 else if (token->type == CPP_EOF)
521 break;
522 if (!nparen)
523 break;
524 }
525
526 return name;
527 }
528
529 /* Callback for has_builtin. */
530
531 int
532 c_common_has_builtin (cpp_reader *pfile)
533 {
534 const char *name = c_common_lex_availability_macro (pfile, "builtin");
535 if (!name)
536 return 0;
537
538 return names_builtin_p (name);
539 }
540
541 /* Callback for has_feature. STRICT_P is true for has_feature and false
542 for has_extension. */
543
544 int
545 c_common_has_feature (cpp_reader *pfile, bool strict_p)
546 {
547 const char *builtin = strict_p ? "feature" : "extension";
548 const char *name = c_common_lex_availability_macro (pfile, builtin);
549 if (!name)
550 return 0;
551
552 /* If -pedantic-errors is given, __has_extension is equivalent to
553 __has_feature. */
554 strict_p |= flag_pedantic_errors;
555 return has_feature_p (name, strict_p);
556 }
557
558
559 /* Read a token and return its type. Fill *VALUE with its value, if
560 applicable. Fill *CPP_FLAGS with the token's flags, if it is
561 non-NULL. */
562
563 enum cpp_ttype
564 c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
565 int lex_flags)
566 {
567 const cpp_token *tok;
568 enum cpp_ttype type;
569 unsigned char add_flags = 0;
570 enum overflow_type overflow = OT_NONE;
571
572 timevar_push (TV_CPP);
573 retry:
574 tok = get_token (parse_in, loc);
575 type = tok->type;
576
577 retry_after_at:
578 switch (type)
579 {
580 case CPP_PADDING:
581 goto retry;
582
583 case CPP_NAME:
584 *value = HT_IDENT_TO_GCC_IDENT (HT_NODE (tok->val.node.node));
585 break;
586
587 case CPP_NUMBER:
588 {
589 const char *suffix = NULL;
590 unsigned int flags = cpp_classify_number (parse_in, tok, &suffix, *loc);
591
592 switch (flags & CPP_N_CATEGORY)
593 {
594 case CPP_N_INVALID:
595 /* cpplib has issued an error. */
596 *value = error_mark_node;
597 break;
598
599 case CPP_N_INTEGER:
600 /* C++ uses '0' to mark virtual functions as pure.
601 Set PURE_ZERO to pass this information to the C++ parser. */
602 if (tok->val.str.len == 1 && *tok->val.str.text == '0')
603 add_flags = PURE_ZERO | DECIMAL_INT;
604 else if ((flags & CPP_N_INTEGER) && (flags & CPP_N_DECIMAL))
605 /* -Wxor-used-as-pow is only active for LHS of ^ expressed
606 as a decimal integer. */
607 add_flags = DECIMAL_INT;
608 *value = interpret_integer (tok, flags, &overflow);
609 break;
610
611 case CPP_N_FLOATING:
612 *value = interpret_float (tok, flags, suffix, &overflow);
613 break;
614
615 default:
616 gcc_unreachable ();
617 }
618
619 if (flags & CPP_N_USERDEF)
620 {
621 char *str;
622 tree literal;
623 tree suffix_id = get_identifier (suffix);
624 int len = tok->val.str.len - strlen (suffix);
625 /* If this is going to be used as a C string to pass to a
626 raw literal operator, we need to add a trailing NUL. */
627 tree num_string = build_string (len + 1,
628 (const char *) tok->val.str.text);
629 TREE_TYPE (num_string) = char_array_type_node;
630 num_string = fix_string_type (num_string);
631 str = CONST_CAST (char *, TREE_STRING_POINTER (num_string));
632 str[len] = '\0';
633 literal = build_userdef_literal (suffix_id, *value, overflow,
634 num_string);
635 *value = literal;
636 }
637 }
638 break;
639
640 case CPP_ATSIGN:
641 /* An @ may give the next token special significance in Objective-C. */
642 if (c_dialect_objc ())
643 {
644 location_t atloc = *loc;
645 location_t newloc;
646
647 retry_at:
648 tok = get_token (parse_in, &newloc);
649 type = tok->type;
650 switch (type)
651 {
652 case CPP_PADDING:
653 goto retry_at;
654
655 case CPP_STRING:
656 case CPP_WSTRING:
657 case CPP_STRING16:
658 case CPP_STRING32:
659 case CPP_UTF8STRING:
660 type = lex_string (tok, value, true, true);
661 break;
662
663 case CPP_NAME:
664 *value = HT_IDENT_TO_GCC_IDENT (HT_NODE (tok->val.node.node));
665 if (OBJC_IS_AT_KEYWORD (C_RID_CODE (*value))
666 || OBJC_IS_CXX_KEYWORD (C_RID_CODE (*value)))
667 {
668 type = CPP_AT_NAME;
669 /* Note the complication: if we found an OBJC_CXX
670 keyword, for example, 'class', we will be
671 returning a token of type CPP_AT_NAME and rid
672 code RID_CLASS (not RID_AT_CLASS). The language
673 parser needs to convert that to RID_AT_CLASS.
674 However, we've now spliced the '@' together with the
675 keyword that follows; Adjust the location so that we
676 get a source range covering the composite.
677 */
678 *loc = make_location (atloc, atloc, newloc);
679 break;
680 }
681 /* FALLTHROUGH */
682
683 default:
684 /* ... or not. */
685 error_at (atloc, "stray %<@%> in program");
686 *loc = newloc;
687 goto retry_after_at;
688 }
689 break;
690 }
691
692 /* FALLTHROUGH */
693 case CPP_HASH:
694 case CPP_PASTE:
695 {
696 unsigned char name[8];
697
698 *cpp_spell_token (parse_in, tok, name, true) = 0;
699
700 error_at (*loc, "stray %qs in program", name);
701 }
702
703 goto retry;
704
705 case CPP_OTHER:
706 {
707 cppchar_t c = tok->val.str.text[0];
708
709 if (c == '"' || c == '\'')
710 error_at (*loc, "missing terminating %c character", (int) c);
711 else if (ISGRAPH (c))
712 error_at (*loc, "stray %qc in program", (int) c);
713 else
714 {
715 rich_location rich_loc (line_table, *loc);
716 rich_loc.set_escape_on_output (true);
717 error_at (&rich_loc, "stray %<\\%o%> in program", (int) c);
718 }
719 }
720 goto retry;
721
722 case CPP_CHAR_USERDEF:
723 case CPP_WCHAR_USERDEF:
724 case CPP_CHAR16_USERDEF:
725 case CPP_CHAR32_USERDEF:
726 case CPP_UTF8CHAR_USERDEF:
727 {
728 tree literal;
729 cpp_token temp_tok = *tok;
730 const char *suffix = cpp_get_userdef_suffix (tok);
731 temp_tok.val.str.len -= strlen (suffix);
732 temp_tok.type = cpp_userdef_char_remove_type (type);
733 literal = build_userdef_literal (get_identifier (suffix),
734 lex_charconst (&temp_tok),
735 OT_NONE, NULL_TREE);
736 *value = literal;
737 }
738 break;
739
740 case CPP_CHAR:
741 case CPP_WCHAR:
742 case CPP_CHAR16:
743 case CPP_CHAR32:
744 case CPP_UTF8CHAR:
745 *value = lex_charconst (tok);
746 break;
747
748 case CPP_STRING_USERDEF:
749 case CPP_WSTRING_USERDEF:
750 case CPP_STRING16_USERDEF:
751 case CPP_STRING32_USERDEF:
752 case CPP_UTF8STRING_USERDEF:
753 {
754 tree literal, string;
755 const char *suffix = cpp_get_userdef_suffix (tok);
756 string = build_string (tok->val.str.len - strlen (suffix),
757 (const char *) tok->val.str.text);
758 literal = build_userdef_literal (get_identifier (suffix),
759 string, OT_NONE, NULL_TREE);
760 *value = literal;
761 }
762 break;
763
764 case CPP_STRING:
765 case CPP_WSTRING:
766 case CPP_STRING16:
767 case CPP_STRING32:
768 case CPP_UTF8STRING:
769 if ((lex_flags & C_LEX_STRING_NO_JOIN) == 0)
770 {
771 type = lex_string (tok, value, false,
772 (lex_flags & C_LEX_STRING_NO_TRANSLATE) == 0);
773 break;
774 }
775 *value = build_string (tok->val.str.len, (const char *) tok->val.str.text);
776 break;
777
778 case CPP_PRAGMA:
779 *value = build_int_cst (integer_type_node, tok->val.pragma);
780 break;
781
782 case CPP_HEADER_NAME:
783 *value = build_string (tok->val.str.len, (const char *)tok->val.str.text);
784 break;
785
786 case CPP_EMBED:
787 *value = make_node (RAW_DATA_CST);
788 TREE_TYPE (*value) = integer_type_node;
789 RAW_DATA_LENGTH (*value) = tok->val.str.len;
790 if (pch_file)
791 {
792 /* When writing PCH headers, copy the data over, such that
793 the owner is a STRING_CST. */
794 int off = 0;
795 if (tok->val.str.len <= INT_MAX - 2)
796 /* See below. */
797 off = 1;
798 tree owner = build_string (tok->val.str.len + 2 * off,
799 (const char *) tok->val.str.text - off);
800 TREE_TYPE (owner) = build_array_type_nelts (unsigned_char_type_node,
801 tok->val.str.len);
802 RAW_DATA_OWNER (*value) = owner;
803 RAW_DATA_POINTER (*value) = TREE_STRING_POINTER (owner) + off;
804 }
805 else
806 {
807 /* Otherwise add another dummy RAW_DATA_CST as owner which
808 indicates the data is owned by libcpp. */
809 RAW_DATA_POINTER (*value) = (const char *) tok->val.str.text;
810 tree owner = make_node (RAW_DATA_CST);
811 TREE_TYPE (owner) = integer_type_node;
812 RAW_DATA_LENGTH (owner) = tok->val.str.len;
813 RAW_DATA_POINTER (owner) = (const char *) tok->val.str.text;
814 if (tok->val.str.len <= INT_MAX - 2)
815 {
816 /* The preprocessor surrounds at least smaller CPP_EMBEDs
817 in between CPP_NUMBER CPP_COMMA before and
818 CPP_COMMA CPP_NUMBER after, so the actual libcpp buffer
819 holds those 2 extra bytes around it. Don't do that if
820 CPP_EMBED is at the maximum ~ 2GB size. */
821 RAW_DATA_LENGTH (owner) += 2;
822 RAW_DATA_POINTER (owner)--;
823 }
824 RAW_DATA_OWNER (*value) = owner;
825 }
826 break;
827
828 /* This token should not be visible outside cpplib. */
829 case CPP_MACRO_ARG:
830 gcc_unreachable ();
831
832 /* CPP_COMMENT will appear when compiling with -C. Ignore, except
833 when it is a FALLTHROUGH comment, in that case set
834 PREV_FALLTHROUGH flag on the next non-comment token. */
835 case CPP_COMMENT:
836 if (tok->flags & PREV_FALLTHROUGH)
837 {
838 do
839 {
840 tok = get_token (parse_in, loc);
841 type = tok->type;
842 }
843 while (type == CPP_PADDING || type == CPP_COMMENT);
844 add_flags |= PREV_FALLTHROUGH;
845 goto retry_after_at;
846 }
847 goto retry;
848
849 default:
850 *value = NULL_TREE;
851 break;
852 }
853
854 if (cpp_flags)
855 *cpp_flags = tok->flags | add_flags;
856
857 timevar_pop (TV_CPP);
858
859 return type;
860 }
861
862 /* Returns the narrowest C-visible unsigned type, starting with the
863 minimum specified by FLAGS, that can fit HIGH:LOW, or itk_none if
864 there isn't one. */
865
866 static enum integer_type_kind
867 narrowest_unsigned_type (const widest_int &val, unsigned int flags)
868 {
869 int itk;
870
871 if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
872 itk = itk_unsigned_int;
873 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
874 itk = itk_unsigned_long;
875 else
876 itk = itk_unsigned_long_long;
877
878 for (; itk < itk_none; itk += 2 /* skip unsigned types */)
879 {
880 tree upper;
881
882 if (integer_types[itk] == NULL_TREE)
883 continue;
884 upper = TYPE_MAX_VALUE (integer_types[itk]);
885
886 if (wi::geu_p (wi::to_widest (upper), val))
887 return (enum integer_type_kind) itk;
888 }
889
890 return itk_none;
891 }
892
893 /* Ditto, but narrowest signed type. */
894 static enum integer_type_kind
895 narrowest_signed_type (const widest_int &val, unsigned int flags)
896 {
897 int itk;
898
899 if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
900 itk = itk_int;
901 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
902 itk = itk_long;
903 else
904 itk = itk_long_long;
905
906 for (; itk < itk_none; itk += 2 /* skip signed types */)
907 {
908 tree upper;
909
910 if (integer_types[itk] == NULL_TREE)
911 continue;
912 upper = TYPE_MAX_VALUE (integer_types[itk]);
913
914 if (wi::geu_p (wi::to_widest (upper), val))
915 return (enum integer_type_kind) itk;
916 }
917
918 return itk_none;
919 }
920
921 /* Interpret TOKEN, an integer with FLAGS as classified by cpplib. */
922 static tree
923 interpret_integer (const cpp_token *token, unsigned int flags,
924 enum overflow_type *overflow)
925 {
926 tree value, type;
927 enum integer_type_kind itk;
928 cpp_num integer;
929 HOST_WIDE_INT ival[3];
930
931 *overflow = OT_NONE;
932
933 if (UNLIKELY (flags & CPP_N_BITINT))
934 {
935 unsigned int suffix_len = 2 + ((flags & CPP_N_UNSIGNED) ? 1 : 0);
936 int max_bits_per_digit = 4; // ceil (log2 (10))
937 unsigned int prefix_len = 0;
938 bool hex = false;
939 const int bitint_maxwidth = WIDE_INT_MAX_PRECISION - 1;
940 if ((flags & CPP_N_RADIX) == CPP_N_OCTAL)
941 {
942 max_bits_per_digit = 3;
943 prefix_len = 1;
944 if (token->val.str.len > 2
945 && (token->val.str.text[1] == 'o'
946 || token->val.str.text[1] == 'O'))
947 prefix_len = 2;
948 }
949 else if ((flags & CPP_N_RADIX) == CPP_N_HEX)
950 {
951 max_bits_per_digit = 4;
952 prefix_len = 2;
953 hex = true;
954 }
955 else if ((flags & CPP_N_RADIX) == CPP_N_BINARY)
956 {
957 max_bits_per_digit = 1;
958 prefix_len = 2;
959 }
960 int max_digits
961 = TYPE_PRECISION (intmax_type_node) >> max_bits_per_digit;
962 const int max_buf = 128;
963 if (max_digits > max_buf)
964 max_digits = max_buf;
965
966 widest_int wval;
967 unsigned int prec;
968 gcc_checking_assert (token->val.str.len > prefix_len + suffix_len
969 || token->val.str.len == 1 + suffix_len);
970 if (token->val.str.len - (prefix_len + suffix_len)
971 <= (unsigned) max_digits)
972 {
973 integer = cpp_interpret_integer (parse_in, token,
974 (flags & CPP_N_RADIX)
975 | CPP_N_UNSIGNED);
976 ival[0] = integer.low;
977 ival[1] = integer.high;
978 ival[2] = 0;
979 wval = widest_int::from_array (ival, 3);
980 }
981 else
982 {
983 unsigned char buf[3 + max_buf];
984 memcpy (buf, token->val.str.text, prefix_len);
985 wval = 0U;
986 const unsigned char *p = token->val.str.text + prefix_len;
987 cpp_token tok = *token;
988 tok.val.str.text = buf;
989 if (!prefix_len)
990 max_digits = 19;
991 do
992 {
993 unsigned char *q = buf + prefix_len;
994 do
995 {
996 unsigned char c = *p++;
997 if (ISDIGIT (c) || (hex && ISXDIGIT (c)))
998 {
999 *q++ = c;
1000 if (q == buf + prefix_len + max_digits)
1001 break;
1002 }
1003 else if (c != '\'')
1004 {
1005 --p;
1006 break;
1007 }
1008 }
1009 while (1);
1010 if (q == buf + prefix_len)
1011 break;
1012 else
1013 {
1014 wi::overflow_type wioverflow;
1015 *q = '\0';
1016 tok.val.str.len = q - buf;
1017 if (wval == 0)
1018 ;
1019 else if (prefix_len)
1020 {
1021 prec = wi::min_precision (wval, UNSIGNED);
1022 unsigned HOST_WIDE_INT shift
1023 = (tok.val.str.len - prefix_len) * max_bits_per_digit;
1024 if (prec + shift > bitint_maxwidth)
1025 goto bitint_overflow;
1026 wval = wi::lshift (wval, shift);
1027 }
1028 else
1029 {
1030 static unsigned HOST_WIDE_INT tens[]
1031 = { 1U, 10U, 100U, 1000U,
1032 HOST_WIDE_INT_UC (10000),
1033 HOST_WIDE_INT_UC (100000),
1034 HOST_WIDE_INT_UC (1000000),
1035 HOST_WIDE_INT_UC (10000000),
1036 HOST_WIDE_INT_UC (100000000),
1037 HOST_WIDE_INT_UC (1000000000),
1038 HOST_WIDE_INT_UC (10000000000),
1039 HOST_WIDE_INT_UC (100000000000),
1040 HOST_WIDE_INT_UC (1000000000000),
1041 HOST_WIDE_INT_UC (10000000000000),
1042 HOST_WIDE_INT_UC (100000000000000),
1043 HOST_WIDE_INT_UC (1000000000000000),
1044 HOST_WIDE_INT_UC (10000000000000000),
1045 HOST_WIDE_INT_UC (100000000000000000),
1046 HOST_WIDE_INT_UC (1000000000000000000),
1047 HOST_WIDE_INT_UC (10000000000000000000) };
1048 widest_int ten = tens[q - buf];
1049 wval = wi::umul (wval, ten, &wioverflow);
1050 if (wioverflow)
1051 goto bitint_overflow;
1052 }
1053 integer = cpp_interpret_integer (parse_in, &tok,
1054 (flags & CPP_N_RADIX)
1055 | CPP_N_UNSIGNED);
1056 ival[0] = integer.low;
1057 ival[1] = integer.high;
1058 ival[2] = 0;
1059 if (prefix_len)
1060 wval = wval + widest_int::from_array (ival, 3);
1061 else
1062 {
1063 widest_int addend = widest_int::from_array (ival, 3);
1064 wval = wi::add (wval, addend, UNSIGNED, &wioverflow);
1065 if (wioverflow)
1066 goto bitint_overflow;
1067 }
1068 }
1069 }
1070 while (1);
1071 }
1072
1073 prec = wi::min_precision (wval, UNSIGNED);
1074 if (prec == 0)
1075 prec = 1;
1076 if ((flags & CPP_N_UNSIGNED) == 0)
1077 ++prec;
1078 if (prec > bitint_maxwidth)
1079 {
1080 bitint_overflow:
1081 if ((flags & CPP_N_UNSIGNED) != 0)
1082 error ("integer constant is too large for "
1083 "%<unsigned _BitInt(%d)%> type", bitint_maxwidth);
1084 else
1085 error ("integer constant is too large for "
1086 "%<_BitInt(%d)%> type", bitint_maxwidth);
1087 return integer_zero_node;
1088 }
1089
1090 struct bitint_info info;
1091 if (!targetm.c.bitint_type_info (prec, &info))
1092 {
1093 sorry ("%<_BitInt(%d)%> is not supported on this target", prec);
1094 return integer_zero_node;
1095 }
1096
1097 type = build_bitint_type (prec, (flags & CPP_N_UNSIGNED) != 0);
1098 return wide_int_to_tree (type, wval);
1099 }
1100
1101 integer = cpp_interpret_integer (parse_in, token, flags);
1102 if (integer.overflow)
1103 *overflow = OT_OVERFLOW;
1104
1105 ival[0] = integer.low;
1106 ival[1] = integer.high;
1107 ival[2] = 0;
1108 widest_int wval = widest_int::from_array (ival, 3);
1109
1110 /* The type of a constant with a U suffix is straightforward. */
1111 if (flags & CPP_N_UNSIGNED)
1112 itk = narrowest_unsigned_type (wval, flags);
1113 else
1114 {
1115 /* The type of a potentially-signed integer constant varies
1116 depending on the base it's in, the standard in use, and the
1117 length suffixes. */
1118 enum integer_type_kind itk_u
1119 = narrowest_unsigned_type (wval, flags);
1120 enum integer_type_kind itk_s
1121 = narrowest_signed_type (wval, flags);
1122
1123 /* In both C89 and C99, octal and hex constants may be signed or
1124 unsigned, whichever fits tighter. We do not warn about this
1125 choice differing from the traditional choice, as the constant
1126 is probably a bit pattern and either way will work. */
1127 if ((flags & CPP_N_RADIX) != CPP_N_DECIMAL)
1128 itk = MIN (itk_u, itk_s);
1129 else
1130 {
1131 /* In C99, decimal constants are always signed.
1132 In C89, decimal constants that don't fit in long have
1133 undefined behavior; we try to make them unsigned long.
1134 In GCC's extended C89, that last is true of decimal
1135 constants that don't fit in long long, too. */
1136
1137 itk = itk_s;
1138 if (itk_s > itk_u && itk_s > itk_long)
1139 {
1140 if (!flag_isoc99)
1141 {
1142 if (itk_u < itk_unsigned_long)
1143 itk_u = itk_unsigned_long;
1144 itk = itk_u;
1145 warning (0, "this decimal constant is unsigned only in ISO C90");
1146 }
1147 else
1148 warning (OPT_Wtraditional,
1149 "this decimal constant would be unsigned in ISO C90");
1150 }
1151 }
1152 }
1153
1154 if (itk == itk_none)
1155 /* cpplib has already issued a warning for overflow. */
1156 type = ((flags & CPP_N_UNSIGNED)
1157 ? widest_unsigned_literal_type_node
1158 : widest_integer_literal_type_node);
1159 else if (flags & CPP_N_SIZE_T)
1160 {
1161 /* itk refers to fundamental types not aliased size types. */
1162 if (flags & CPP_N_UNSIGNED)
1163 type = size_type_node;
1164 else
1165 type = signed_size_type_node;
1166 }
1167 else
1168 {
1169 type = integer_types[itk];
1170 if (itk > itk_unsigned_long
1171 && (flags & CPP_N_WIDTH) != CPP_N_LARGE)
1172 emit_diagnostic
1173 ((c_dialect_cxx () ? cxx_dialect == cxx98 : !flag_isoc99)
1174 ? DK_PEDWARN : DK_WARNING,
1175 input_location, OPT_Wlong_long,
1176 (flags & CPP_N_UNSIGNED)
1177 ? "integer constant is too large for %<unsigned long%> type"
1178 : "integer constant is too large for %<long%> type");
1179 }
1180
1181 value = wide_int_to_tree (type, wval);
1182
1183 /* Convert imaginary to a complex type. */
1184 if (flags & CPP_N_IMAGINARY)
1185 value = build_complex (NULL_TREE, build_int_cst (type, 0), value);
1186
1187 return value;
1188 }
1189
1190 /* Interpret TOKEN, a floating point number with FLAGS as classified
1191 by cpplib. For C++11 SUFFIX may contain a user-defined literal suffix. */
1192 static tree
1193 interpret_float (const cpp_token *token, unsigned int flags,
1194 const char *suffix, enum overflow_type *overflow)
1195 {
1196 tree type;
1197 tree const_type;
1198 tree value;
1199 REAL_VALUE_TYPE real;
1200 REAL_VALUE_TYPE real_trunc;
1201 char *copy;
1202 size_t copylen;
1203
1204 *overflow = OT_NONE;
1205
1206 /* Default (no suffix) depends on whether the FLOAT_CONST_DECIMAL64
1207 pragma has been used and is either double or _Decimal64. Types
1208 that are not allowed with decimal float default to double. */
1209 if (flags & CPP_N_DEFAULT)
1210 {
1211 flags ^= CPP_N_DEFAULT;
1212 flags |= CPP_N_MEDIUM;
1213
1214 if (((flags & CPP_N_HEX) == 0) && ((flags & CPP_N_IMAGINARY) == 0))
1215 {
1216 warning (OPT_Wunsuffixed_float_constants,
1217 "unsuffixed floating constant");
1218 if (float_const_decimal64_p ())
1219 flags |= CPP_N_DFLOAT;
1220 }
1221 }
1222
1223 /* Decode _Fract and _Accum. */
1224 if (flags & CPP_N_FRACT || flags & CPP_N_ACCUM)
1225 return interpret_fixed (token, flags);
1226
1227 /* Decode type based on width and properties. */
1228 if (flags & CPP_N_DFLOAT)
1229 if (!targetm.decimal_float_supported_p ())
1230 {
1231 error ("decimal floating-point not supported for this target");
1232 return error_mark_node;
1233 }
1234 else if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1235 type = dfloat128_type_node;
1236 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1237 type = dfloat32_type_node;
1238 else if ((flags & CPP_N_FLOATNX) != 0)
1239 type = dfloat64x_type_node;
1240 else
1241 type = dfloat64_type_node;
1242 else
1243 if (flags & CPP_N_WIDTH_MD)
1244 {
1245 char suffix;
1246 machine_mode mode;
1247
1248 if ((flags & CPP_N_WIDTH_MD) == CPP_N_MD_W)
1249 suffix = 'w';
1250 else
1251 suffix = 'q';
1252
1253 mode = targetm.c.mode_for_suffix (suffix);
1254 if (mode == VOIDmode)
1255 {
1256 error ("unsupported non-standard suffix on floating constant");
1257
1258 return error_mark_node;
1259 }
1260 else
1261 pedwarn (input_location, OPT_Wpedantic, "non-standard suffix on floating constant");
1262
1263 type = c_common_type_for_mode (mode, 0);
1264 /* For Q suffix, prefer float128t_type_node (__float128) type
1265 over float128_type_node (_Float128) type if they are distinct. */
1266 if (type == float128_type_node && float128t_type_node)
1267 type = float128t_type_node;
1268 gcc_assert (type);
1269 }
1270 else if ((flags & (CPP_N_FLOATN | CPP_N_FLOATNX)) != 0)
1271 {
1272 unsigned int n = (flags & CPP_N_WIDTH_FLOATN_NX) >> CPP_FLOATN_SHIFT;
1273 bool extended = (flags & CPP_N_FLOATNX) != 0;
1274 type = NULL_TREE;
1275 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
1276 if (floatn_nx_types[i].n == (int) n
1277 && floatn_nx_types[i].extended == extended)
1278 {
1279 type = FLOATN_NX_TYPE_NODE (i);
1280 break;
1281 }
1282 if (type == NULL_TREE)
1283 {
1284 error ("unsupported non-standard suffix on floating constant");
1285 return error_mark_node;
1286 }
1287 else if (!c_dialect_cxx ())
1288 {
1289 if (warn_c11_c23_compat > 0)
1290 {
1291 if (pedantic && !flag_isoc23)
1292 pedwarn (input_location, OPT_Wc11_c23_compat,
1293 "non-standard suffix on floating constant "
1294 "before C23");
1295 else
1296 warning (OPT_Wc11_c23_compat,
1297 "non-standard suffix on floating constant "
1298 "before C23");
1299 }
1300 else if (warn_c11_c23_compat != 0 && pedantic && !flag_isoc23)
1301 pedwarn (input_location, OPT_Wpedantic,
1302 "non-standard suffix on floating constant "
1303 "before C23");
1304 }
1305 else if (!extended)
1306 {
1307 if (cxx_dialect < cxx23 && pedantic)
1308 pedwarn (input_location, OPT_Wc__23_extensions,
1309 "%<f%d%> or %<F%d%> suffix on floating constant only "
1310 "available with %<-std=c++23%> or %<-std=gnu++23%>",
1311 n, n);
1312 }
1313 else
1314 pedwarn (input_location, OPT_Wpedantic,
1315 "non-standard suffix on floating constant");
1316 }
1317 else if ((flags & CPP_N_BFLOAT16) != 0)
1318 {
1319 type = bfloat16_type_node;
1320 if (type == NULL_TREE)
1321 {
1322 error ("unsupported non-standard suffix on floating constant");
1323 return error_mark_node;
1324 }
1325 if (!c_dialect_cxx ())
1326 pedwarn (input_location, OPT_Wpedantic,
1327 "non-standard suffix on floating constant");
1328 else if (cxx_dialect < cxx23 && pedantic)
1329 pedwarn (input_location, OPT_Wc__23_extensions,
1330 "%<bf16%> or %<BF16%> suffix on floating constant only "
1331 "available with %<-std=c++23%> or %<-std=gnu++23%>");
1332 }
1333 else if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1334 type = long_double_type_node;
1335 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL
1336 || flag_single_precision_constant)
1337 type = float_type_node;
1338 else
1339 type = double_type_node;
1340
1341 const_type = excess_precision_type (type);
1342 if (!const_type)
1343 const_type = type;
1344
1345 /* Copy the constant to a nul-terminated buffer. If the constant
1346 has any suffixes, cut them off; REAL_VALUE_ATOF/ REAL_VALUE_HTOF
1347 can't handle them. */
1348 copylen = token->val.str.len;
1349 if (flags & CPP_N_USERDEF)
1350 copylen -= strlen (suffix);
1351 else if (flags & CPP_N_DFLOAT)
1352 {
1353 if (ISDIGIT (token->val.str.text[copylen - 1]))
1354 copylen -= (flags & CPP_N_LARGE) ? 4 : 3;
1355 else if ((flags & CPP_N_FLOATNX) != 0)
1356 copylen -= 4;
1357 else
1358 copylen -= 2;
1359 }
1360 else
1361 {
1362 if ((flags & CPP_N_WIDTH) != CPP_N_MEDIUM)
1363 /* Must be an F or L or machine defined suffix. */
1364 copylen--;
1365 if (flags & CPP_N_IMAGINARY)
1366 /* I or J suffix. */
1367 copylen--;
1368 if (flags & CPP_N_FLOATNX)
1369 copylen--;
1370 if (flags & (CPP_N_FLOATN | CPP_N_FLOATNX))
1371 {
1372 unsigned int n = (flags & CPP_N_WIDTH_FLOATN_NX) >> CPP_FLOATN_SHIFT;
1373 while (n > 0)
1374 {
1375 copylen--;
1376 n /= 10;
1377 }
1378 }
1379 }
1380
1381 copy = (char *) alloca (copylen + 1);
1382 if (c_dialect_cxx () ? cxx_dialect > cxx11 : flag_isoc23)
1383 {
1384 size_t maxlen = 0;
1385 for (size_t i = 0; i < copylen; ++i)
1386 if (token->val.str.text[i] != '\'')
1387 copy[maxlen++] = token->val.str.text[i];
1388 copy[maxlen] = '\0';
1389 }
1390 else
1391 {
1392 memcpy (copy, token->val.str.text, copylen);
1393 copy[copylen] = '\0';
1394 }
1395
1396 real_from_string3 (&real, copy, TYPE_MODE (const_type));
1397 if (const_type != type)
1398 /* Diagnosing if the result of converting the value with excess
1399 precision to the semantic type would overflow (with associated
1400 double rounding) is more appropriate than diagnosing if the
1401 result of converting the string directly to the semantic type
1402 would overflow. */
1403 real_convert (&real_trunc, TYPE_MODE (type), &real);
1404
1405 /* Both C and C++ require a diagnostic for a floating constant
1406 outside the range of representable values of its type. Since we
1407 have __builtin_inf* to produce an infinity, this is now a
1408 mandatory pedwarn if the target does not support infinities. */
1409 if (REAL_VALUE_ISINF (real)
1410 || (const_type != type && REAL_VALUE_ISINF (real_trunc)))
1411 {
1412 *overflow = OT_OVERFLOW;
1413 if (!(flags & CPP_N_USERDEF))
1414 {
1415 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)))
1416 pedwarn (input_location, 0,
1417 "floating constant exceeds range of %qT", type);
1418 else
1419 warning (OPT_Woverflow,
1420 "floating constant exceeds range of %qT", type);
1421 }
1422 }
1423 /* We also give a warning if the value underflows. */
1424 else if (real_equal (&real, &dconst0)
1425 || (const_type != type
1426 && real_equal (&real_trunc, &dconst0)))
1427 {
1428 REAL_VALUE_TYPE realvoidmode;
1429 int oflow = real_from_string (&realvoidmode, copy);
1430 *overflow = (oflow == 0 ? OT_NONE
1431 : (oflow < 0 ? OT_UNDERFLOW : OT_OVERFLOW));
1432 if (!(flags & CPP_N_USERDEF))
1433 {
1434 if (oflow < 0 || !real_equal (&realvoidmode, &dconst0))
1435 warning (OPT_Woverflow, "floating constant truncated to zero");
1436 }
1437 }
1438
1439 /* Create a node with determined type and value. */
1440 value = build_real (const_type, real);
1441 if (flags & CPP_N_IMAGINARY)
1442 {
1443 value = build_complex (NULL_TREE,
1444 fold_convert (const_type,
1445 integer_zero_node), value);
1446 if (type != const_type)
1447 {
1448 const_type = TREE_TYPE (value);
1449 type = build_complex_type (type);
1450 }
1451 }
1452
1453 if (type != const_type)
1454 value = build1_loc (token->src_loc, EXCESS_PRECISION_EXPR, type, value);
1455
1456 return value;
1457 }
1458
1459 /* Interpret TOKEN, a fixed-point number with FLAGS as classified
1460 by cpplib. */
1461
1462 static tree
1463 interpret_fixed (const cpp_token *token, unsigned int flags)
1464 {
1465 tree type;
1466 tree value;
1467 FIXED_VALUE_TYPE fixed;
1468 char *copy;
1469 size_t copylen;
1470
1471 copylen = token->val.str.len;
1472
1473 if (flags & CPP_N_FRACT) /* _Fract. */
1474 {
1475 if (flags & CPP_N_UNSIGNED) /* Unsigned _Fract. */
1476 {
1477 if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1478 {
1479 type = unsigned_long_long_fract_type_node;
1480 copylen -= 4;
1481 }
1482 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
1483 {
1484 type = unsigned_long_fract_type_node;
1485 copylen -= 3;
1486 }
1487 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1488 {
1489 type = unsigned_short_fract_type_node;
1490 copylen -= 3;
1491 }
1492 else
1493 {
1494 type = unsigned_fract_type_node;
1495 copylen -= 2;
1496 }
1497 }
1498 else /* Signed _Fract. */
1499 {
1500 if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1501 {
1502 type = long_long_fract_type_node;
1503 copylen -= 3;
1504 }
1505 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
1506 {
1507 type = long_fract_type_node;
1508 copylen -= 2;
1509 }
1510 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1511 {
1512 type = short_fract_type_node;
1513 copylen -= 2;
1514 }
1515 else
1516 {
1517 type = fract_type_node;
1518 copylen --;
1519 }
1520 }
1521 }
1522 else /* _Accum. */
1523 {
1524 if (flags & CPP_N_UNSIGNED) /* Unsigned _Accum. */
1525 {
1526 if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1527 {
1528 type = unsigned_long_long_accum_type_node;
1529 copylen -= 4;
1530 }
1531 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
1532 {
1533 type = unsigned_long_accum_type_node;
1534 copylen -= 3;
1535 }
1536 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1537 {
1538 type = unsigned_short_accum_type_node;
1539 copylen -= 3;
1540 }
1541 else
1542 {
1543 type = unsigned_accum_type_node;
1544 copylen -= 2;
1545 }
1546 }
1547 else /* Signed _Accum. */
1548 {
1549 if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1550 {
1551 type = long_long_accum_type_node;
1552 copylen -= 3;
1553 }
1554 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
1555 {
1556 type = long_accum_type_node;
1557 copylen -= 2;
1558 }
1559 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1560 {
1561 type = short_accum_type_node;
1562 copylen -= 2;
1563 }
1564 else
1565 {
1566 type = accum_type_node;
1567 copylen --;
1568 }
1569 }
1570 }
1571
1572 copy = (char *) alloca (copylen + 1);
1573 memcpy (copy, token->val.str.text, copylen);
1574 copy[copylen] = '\0';
1575
1576 fixed_from_string (&fixed, copy, SCALAR_TYPE_MODE (type));
1577
1578 /* Create a node with determined type and value. */
1579 value = build_fixed (type, fixed);
1580
1581 return value;
1582 }
1583
1584 /* Convert a series of STRING, WSTRING, STRING16, STRING32 and/or
1585 UTF8STRING tokens into a tree, performing string constant
1586 concatenation. TOK is the first of these. VALP is the location to
1587 write the string into. OBJC_STRING indicates whether an '@' token
1588 preceded the incoming token (in that case, the strings can either
1589 be ObjC strings, preceded by a single '@', or normal strings, not
1590 preceded by '@'. The result will be a CPP_OBJC_STRING). Returns
1591 the CPP token type of the result (CPP_STRING, CPP_WSTRING,
1592 CPP_STRING32, CPP_STRING16, CPP_UTF8STRING, or CPP_OBJC_STRING).
1593
1594 This is unfortunately more work than it should be. If any of the
1595 strings in the series has an L prefix, the result is a wide string
1596 (6.4.5p4). Whether or not the result is a wide string affects the
1597 meaning of octal and hexadecimal escapes (6.4.4.4p6,9). But escape
1598 sequences do not continue across the boundary between two strings in
1599 a series (6.4.5p7), so we must not lose the boundaries. Therefore
1600 cpp_interpret_string takes a vector of cpp_string structures, which
1601 we must arrange to provide. */
1602
1603 static enum cpp_ttype
1604 lex_string (const cpp_token *tok, tree *valp, bool objc_string, bool translate)
1605 {
1606 tree value;
1607 size_t concats = 0;
1608 struct obstack str_ob;
1609 struct obstack loc_ob;
1610 cpp_string istr;
1611 enum cpp_ttype type = tok->type;
1612
1613 /* Try to avoid the overhead of creating and destroying an obstack
1614 for the common case of just one string. */
1615 cpp_string str = tok->val.str;
1616 location_t init_loc = tok->src_loc;
1617 cpp_string *strs = &str;
1618 location_t *locs = NULL;
1619
1620 /* objc_at_sign_was_seen is only used when doing Objective-C string
1621 concatenation. It is 'true' if we have seen an '@' before the
1622 current string, and 'false' if not. We must see exactly one or
1623 zero '@' before each string. */
1624 bool objc_at_sign_was_seen = false;
1625
1626 retry:
1627 tok = get_token (parse_in);
1628 switch (tok->type)
1629 {
1630 case CPP_PADDING:
1631 goto retry;
1632 case CPP_ATSIGN:
1633 if (objc_string)
1634 {
1635 if (objc_at_sign_was_seen)
1636 error ("repeated %<@%> before Objective-C string");
1637
1638 objc_at_sign_was_seen = true;
1639 goto retry;
1640 }
1641 /* FALLTHROUGH */
1642
1643 default:
1644 break;
1645
1646 case CPP_WSTRING:
1647 case CPP_STRING16:
1648 case CPP_STRING32:
1649 case CPP_UTF8STRING:
1650 if (type != tok->type)
1651 {
1652 if (type == CPP_STRING)
1653 type = tok->type;
1654 else
1655 error ("unsupported non-standard concatenation of string literals");
1656 }
1657 /* FALLTHROUGH */
1658
1659 case CPP_STRING:
1660 if (!concats)
1661 {
1662 gcc_obstack_init (&str_ob);
1663 gcc_obstack_init (&loc_ob);
1664 obstack_grow (&str_ob, &str, sizeof (cpp_string));
1665 obstack_grow (&loc_ob, &init_loc, sizeof (location_t));
1666 }
1667
1668 concats++;
1669 obstack_grow (&str_ob, &tok->val.str, sizeof (cpp_string));
1670 obstack_grow (&loc_ob, &tok->src_loc, sizeof (location_t));
1671
1672 if (objc_string)
1673 objc_at_sign_was_seen = false;
1674 goto retry;
1675 }
1676
1677 /* It is an error if we saw a '@' with no following string. */
1678 if (objc_at_sign_was_seen)
1679 error ("stray %<@%> in program");
1680
1681 /* We have read one more token than we want. */
1682 _cpp_backup_tokens (parse_in, 1);
1683 if (concats)
1684 {
1685 strs = XOBFINISH (&str_ob, cpp_string *);
1686 locs = XOBFINISH (&loc_ob, location_t *);
1687 }
1688
1689 if (concats && !objc_string && !in_system_header_at (input_location))
1690 warning (OPT_Wtraditional,
1691 "traditional C rejects string constant concatenation");
1692
1693 if ((translate
1694 ? cpp_interpret_string : cpp_interpret_string_notranslate)
1695 (parse_in, strs, concats + 1, &istr, type))
1696 {
1697 value = build_string (istr.len, (const char *) istr.text);
1698 free (CONST_CAST (unsigned char *, istr.text));
1699 if (concats)
1700 {
1701 gcc_assert (locs);
1702 gcc_assert (g_string_concat_db);
1703 g_string_concat_db->record_string_concatenation (concats + 1, locs);
1704 }
1705 }
1706 else
1707 {
1708 /* Callers cannot generally handle error_mark_node in this context,
1709 so return the empty string instead. cpp_interpret_string has
1710 issued an error. */
1711 switch (type)
1712 {
1713 default:
1714 case CPP_STRING:
1715 case CPP_UTF8STRING:
1716 if (type == CPP_UTF8STRING && flag_char8_t)
1717 {
1718 value = build_string (TYPE_PRECISION (char8_type_node)
1719 / TYPE_PRECISION (char_type_node),
1720 ""); /* char8_t is 8 bits */
1721 }
1722 else
1723 value = build_string (1, "");
1724 break;
1725 case CPP_STRING16:
1726 value = build_string (TYPE_PRECISION (char16_type_node)
1727 / TYPE_PRECISION (char_type_node),
1728 "\0"); /* char16_t is 16 bits */
1729 break;
1730 case CPP_STRING32:
1731 value = build_string (TYPE_PRECISION (char32_type_node)
1732 / TYPE_PRECISION (char_type_node),
1733 "\0\0\0"); /* char32_t is 32 bits */
1734 break;
1735 case CPP_WSTRING:
1736 value = build_string (TYPE_PRECISION (wchar_type_node)
1737 / TYPE_PRECISION (char_type_node),
1738 "\0\0\0"); /* widest supported wchar_t
1739 is 32 bits */
1740 break;
1741 }
1742 }
1743
1744 switch (type)
1745 {
1746 default:
1747 case CPP_STRING:
1748 TREE_TYPE (value) = char_array_type_node;
1749 break;
1750 case CPP_UTF8STRING:
1751 if (flag_char8_t)
1752 TREE_TYPE (value) = char8_array_type_node;
1753 else
1754 TREE_TYPE (value) = char_array_type_node;
1755 break;
1756 case CPP_STRING16:
1757 TREE_TYPE (value) = char16_array_type_node;
1758 break;
1759 case CPP_STRING32:
1760 TREE_TYPE (value) = char32_array_type_node;
1761 break;
1762 case CPP_WSTRING:
1763 TREE_TYPE (value) = wchar_array_type_node;
1764 }
1765 *valp = fix_string_type (value);
1766
1767 if (concats)
1768 {
1769 obstack_free (&str_ob, 0);
1770 obstack_free (&loc_ob, 0);
1771 }
1772
1773 return objc_string ? CPP_OBJC_STRING : type;
1774 }
1775
1776 /* Converts a (possibly wide) character constant token into a tree. */
1777 static tree
1778 lex_charconst (const cpp_token *token)
1779 {
1780 cppchar_t result;
1781 tree type, value;
1782 unsigned int chars_seen;
1783 int unsignedp = 0;
1784
1785 result = cpp_interpret_charconst (parse_in, token,
1786 &chars_seen, &unsignedp);
1787
1788 if (token->type == CPP_WCHAR)
1789 type = wchar_type_node;
1790 else if (token->type == CPP_CHAR32)
1791 type = char32_type_node;
1792 else if (token->type == CPP_CHAR16)
1793 type = char16_type_node;
1794 else if (token->type == CPP_UTF8CHAR)
1795 {
1796 if (flag_char8_t)
1797 type = char8_type_node;
1798 else
1799 type = char_type_node;
1800 }
1801 /* In C, a character constant has type 'int'.
1802 In C++ 'char', but multi-char charconsts have type 'int'. */
1803 else if (!c_dialect_cxx () || chars_seen > 1)
1804 type = integer_type_node;
1805 else
1806 type = char_type_node;
1807
1808 /* Cast to cppchar_signed_t to get correct sign-extension of RESULT
1809 before possibly widening to HOST_WIDE_INT for build_int_cst. */
1810 if (unsignedp || (cppchar_signed_t) result >= 0)
1811 value = build_int_cst (type, result);
1812 else
1813 value = build_int_cst (type, (cppchar_signed_t) result);
1814
1815 return value;
1816 }
1817
1818 /* Helper function for c_parser_peek_conflict_marker
1819 and cp_lexer_peek_conflict_marker.
1820 Given a possible conflict marker token of kind TOK1_KIND
1821 consisting of a pair of characters, get the token kind for the
1822 standalone final character. */
1823
1824 enum cpp_ttype
1825 conflict_marker_get_final_tok_kind (enum cpp_ttype tok1_kind)
1826 {
1827 switch (tok1_kind)
1828 {
1829 default: gcc_unreachable ();
1830 case CPP_LSHIFT:
1831 /* "<<" and '<' */
1832 return CPP_LESS;
1833
1834 case CPP_EQ_EQ:
1835 /* "==" and '=' */
1836 return CPP_EQ;
1837
1838 case CPP_RSHIFT:
1839 /* ">>" and '>' */
1840 return CPP_GREATER;
1841 }
1842 }