1/* Mainly the interface between cpplib and the C front ends.
2 Copyright (C) 1987-2024 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along 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. */
37static int header_time, body_time;
38static splay_tree file_info_tree;
39
40int pending_lang_change; /* If we need to switch languages - C++ only */
41int c_header_level; /* depth in C headers - C++ only */
42
43static tree interpret_integer (const cpp_token *, unsigned int,
44 enum overflow_type *);
45static tree interpret_float (const cpp_token *, unsigned int, const char *,
46 enum overflow_type *);
47static tree interpret_fixed (const cpp_token *, unsigned int);
48static enum integer_type_kind narrowest_unsigned_type
49 (const widest_int &, unsigned int);
50static enum integer_type_kind narrowest_signed_type
51 (const widest_int &, unsigned int);
52static enum cpp_ttype lex_string (const cpp_token *, tree *, bool, bool);
53static tree lex_charconst (const cpp_token *);
54static void update_header_times (const char *);
55static int dump_one_header (splay_tree_node, void *);
56static void cb_line_change (cpp_reader *, const cpp_token *, int);
57static void cb_ident (cpp_reader *, unsigned int, const cpp_string *);
58static void cb_def_pragma (cpp_reader *, unsigned int);
59static void cb_define (cpp_reader *, unsigned int, cpp_hashnode *);
60static void cb_undef (cpp_reader *, unsigned int, cpp_hashnode *);
61
62void
63init_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
101struct c_fileinfo *
102get_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
125static void
126update_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
140static int
141dump_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
148void
149dump_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, format: "\n******\n");
156 print_time ("header files (total)", header_time);
157 print_time ("main file (total)", this_time - body_time);
158 fprintf (stderr, format: "ratio = %g : 1\n",
159 (double) header_time / (double) (this_time - body_time));
160 fprintf (stderr, format: "\n******\n");
161
162 splay_tree_foreach (file_info_tree, dump_one_header, 0);
163}
164
165static void
166cb_ident (cpp_reader * ARG_UNUSED (pfile),
167 unsigned int 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 = { .len: 0, .text: 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. */
184static void
185cb_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
192void
193fe_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 (ord_map: new_map))
203 {
204 location_t included_at = linemap_included_from (ord_map: new_map);
205 int line = 0;
206 if (included_at > BUILTINS_LOCATION)
207 line = SOURCE_LINE (ord_map: new_map - 1, loc: included_at);
208
209 input_location = new_map->start_location;
210 (*debug_hooks->start_source_file) (line, LINEMAP_FILE (ord_map: 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 (ord_map: new_map));
235 }
236
237 update_header_times (name: LINEMAP_FILE (ord_map: new_map));
238 input_location = new_map->start_location;
239}
240
241static void
242cb_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 (loc: 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. */
273static void
274cb_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 (ord_map: linemap_check_ordinary (map), loc),
278 (const char *) cpp_macro_definition (pfile, node));
279}
280
281/* #undef callback for DWARF and DWARF2 debug info. */
282static void
283cb_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 (ord_map: 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
298static const cpp_token *
299get_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: *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
319static const cpp_token *
320get_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. */
334int
335c_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 msgid: "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_name: attr_id);
392 /* OpenMP attributes need special handling. */
393 if ((flag_openmp || flag_openmp_simd)
394 && is_attribute_p (attr_name: "omp", ident: attr_ns)
395 && (is_attribute_p (attr_name: "directive", ident: attr_id)
396 || is_attribute_p (attr_name: "sequence", ident: attr_id)
397 || is_attribute_p (attr_name: "decl", ident: 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 msgid: "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 (attr_name: "noreturn", ident: attr_name))
417 result = 200809;
418 else if (is_attribute_p (attr_name: "deprecated", ident: attr_name))
419 result = 201309;
420 else if (is_attribute_p (attr_name: "maybe_unused", ident: attr_name)
421 || is_attribute_p (attr_name: "fallthrough", ident: attr_name))
422 result = 201603;
423 else if (is_attribute_p (attr_name: "no_unique_address", ident: attr_name)
424 || is_attribute_p (attr_name: "likely", ident: attr_name)
425 || is_attribute_p (attr_name: "unlikely", ident: attr_name))
426 result = 201803;
427 else if (is_attribute_p (attr_name: "nodiscard", ident: attr_name))
428 result = 201907;
429 else if (is_attribute_p (attr_name: "assume", ident: attr_name))
430 result = 202207;
431 else if (is_attribute_p (attr_name: "init_priority", ident: 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 (attr_name: "deprecated", ident: attr_name)
444 || is_attribute_p (attr_name: "fallthrough", ident: attr_name)
445 || is_attribute_p (attr_name: "maybe_unused", ident: attr_name)
446 || is_attribute_p (attr_name: "nodiscard", ident: attr_name)
447 || is_attribute_p (attr_name: "noreturn", ident: attr_name)
448 || is_attribute_p (attr_name: "_Noreturn", ident: attr_name))
449 result = 202311;
450 }
451 if (result)
452 attr_name = NULL_TREE;
453 }
454 if (attr_name && (have_scope || !std_syntax))
455 {
456 init_attributes ();
457 const struct attribute_spec *attr = lookup_attribute_spec (attr_name);
458 if (attr)
459 result = 1;
460 }
461 }
462 else
463 {
464 cpp_error (pfile, CPP_DL_ERROR,
465 msgid: "macro \"__has_attribute\" requires an identifier");
466 return 0;
467 }
468
469 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
470 cpp_error (pfile, CPP_DL_ERROR,
471 msgid: "missing ')' after \"__has_attribute\"");
472
473 return result;
474}
475
476/* Helper for __has_{builtin,feature,extension}. */
477
478static const char *
479c_common_lex_availability_macro (cpp_reader *pfile, const char *builtin)
480{
481 const cpp_token *token = get_token_no_padding (pfile);
482 if (token->type != CPP_OPEN_PAREN)
483 {
484 cpp_error (pfile, CPP_DL_ERROR,
485 msgid: "missing '(' after \"__has_%s\"", builtin);
486 return 0;
487 }
488
489 const char *name = "";
490 token = get_token_no_padding (pfile);
491 if (token->type == CPP_NAME)
492 {
493 name = (const char *) cpp_token_as_text (pfile, token);
494 token = get_token_no_padding (pfile);
495 if (token->type != CPP_CLOSE_PAREN)
496 {
497 cpp_error (pfile, CPP_DL_ERROR,
498 msgid: "expected ')' after \"%s\"", name);
499 name = "";
500 }
501 }
502 else
503 {
504 cpp_error (pfile, CPP_DL_ERROR,
505 msgid: "macro \"__has_%s\" requires an identifier", builtin);
506 if (token->type == CPP_CLOSE_PAREN)
507 return 0;
508 }
509
510 /* Consume tokens up to the closing parenthesis, including any nested
511 pairs of parentheses, to avoid confusing redundant errors. */
512 for (unsigned nparen = 1; ; token = get_token_no_padding (pfile))
513 {
514 if (token->type == CPP_OPEN_PAREN)
515 ++nparen;
516 else if (token->type == CPP_CLOSE_PAREN)
517 --nparen;
518 else if (token->type == CPP_EOF)
519 break;
520 if (!nparen)
521 break;
522 }
523
524 return name;
525}
526
527/* Callback for has_builtin. */
528
529int
530c_common_has_builtin (cpp_reader *pfile)
531{
532 const char *name = c_common_lex_availability_macro (pfile, builtin: "builtin");
533 if (!name)
534 return 0;
535
536 return names_builtin_p (name);
537}
538
539/* Callback for has_feature. STRICT_P is true for has_feature and false
540 for has_extension. */
541
542int
543c_common_has_feature (cpp_reader *pfile, bool strict_p)
544{
545 const char *builtin = strict_p ? "feature" : "extension";
546 const char *name = c_common_lex_availability_macro (pfile, builtin);
547 if (!name)
548 return 0;
549
550 /* If -pedantic-errors is given, __has_extension is equivalent to
551 __has_feature. */
552 strict_p |= flag_pedantic_errors;
553 return has_feature_p (name, strict_p);
554}
555
556
557/* Read a token and return its type. Fill *VALUE with its value, if
558 applicable. Fill *CPP_FLAGS with the token's flags, if it is
559 non-NULL. */
560
561enum cpp_ttype
562c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
563 int lex_flags)
564{
565 const cpp_token *tok;
566 enum cpp_ttype type;
567 unsigned char add_flags = 0;
568 enum overflow_type overflow = OT_NONE;
569
570 timevar_push (tv: TV_CPP);
571 retry:
572 tok = get_token (pfile: parse_in, loc);
573 type = tok->type;
574
575 retry_after_at:
576 switch (type)
577 {
578 case CPP_PADDING:
579 goto retry;
580
581 case CPP_NAME:
582 *value = HT_IDENT_TO_GCC_IDENT (HT_NODE (tok->val.node.node));
583 break;
584
585 case CPP_NUMBER:
586 {
587 const char *suffix = NULL;
588 unsigned int flags = cpp_classify_number (parse_in, tok, &suffix, *loc);
589
590 switch (flags & CPP_N_CATEGORY)
591 {
592 case CPP_N_INVALID:
593 /* cpplib has issued an error. */
594 *value = error_mark_node;
595 break;
596
597 case CPP_N_INTEGER:
598 /* C++ uses '0' to mark virtual functions as pure.
599 Set PURE_ZERO to pass this information to the C++ parser. */
600 if (tok->val.str.len == 1 && *tok->val.str.text == '0')
601 add_flags = PURE_ZERO | DECIMAL_INT;
602 else if ((flags & CPP_N_INTEGER) && (flags & CPP_N_DECIMAL))
603 /* -Wxor-used-as-pow is only active for LHS of ^ expressed
604 as a decimal integer. */
605 add_flags = DECIMAL_INT;
606 *value = interpret_integer (tok, flags, &overflow);
607 break;
608
609 case CPP_N_FLOATING:
610 *value = interpret_float (tok, flags, suffix, &overflow);
611 break;
612
613 default:
614 gcc_unreachable ();
615 }
616
617 if (flags & CPP_N_USERDEF)
618 {
619 char *str;
620 tree literal;
621 tree suffix_id = get_identifier (suffix);
622 int len = tok->val.str.len - strlen (s: suffix);
623 /* If this is going to be used as a C string to pass to a
624 raw literal operator, we need to add a trailing NUL. */
625 tree num_string = build_string (len + 1,
626 (const char *) tok->val.str.text);
627 TREE_TYPE (num_string) = char_array_type_node;
628 num_string = fix_string_type (num_string);
629 str = CONST_CAST (char *, TREE_STRING_POINTER (num_string));
630 str[len] = '\0';
631 literal = build_userdef_literal (suffix_id, value: *value, overflow,
632 num_string);
633 *value = literal;
634 }
635 }
636 break;
637
638 case CPP_ATSIGN:
639 /* An @ may give the next token special significance in Objective-C. */
640 if (c_dialect_objc ())
641 {
642 location_t atloc = *loc;
643 location_t newloc;
644
645 retry_at:
646 tok = get_token (pfile: parse_in, loc: &newloc);
647 type = tok->type;
648 switch (type)
649 {
650 case CPP_PADDING:
651 goto retry_at;
652
653 case CPP_STRING:
654 case CPP_WSTRING:
655 case CPP_STRING16:
656 case CPP_STRING32:
657 case CPP_UTF8STRING:
658 type = lex_string (tok, value, true, true);
659 break;
660
661 case CPP_NAME:
662 *value = HT_IDENT_TO_GCC_IDENT (HT_NODE (tok->val.node.node));
663 if (OBJC_IS_AT_KEYWORD (C_RID_CODE (*value))
664 || OBJC_IS_CXX_KEYWORD (C_RID_CODE (*value)))
665 {
666 type = CPP_AT_NAME;
667 /* Note the complication: if we found an OBJC_CXX
668 keyword, for example, 'class', we will be
669 returning a token of type CPP_AT_NAME and rid
670 code RID_CLASS (not RID_AT_CLASS). The language
671 parser needs to convert that to RID_AT_CLASS.
672 However, we've now spliced the '@' together with the
673 keyword that follows; Adjust the location so that we
674 get a source range covering the composite.
675 */
676 *loc = make_location (caret: atloc, start: atloc, finish: newloc);
677 break;
678 }
679 /* FALLTHROUGH */
680
681 default:
682 /* ... or not. */
683 error_at (atloc, "stray %<@%> in program");
684 *loc = newloc;
685 goto retry_after_at;
686 }
687 break;
688 }
689
690 /* FALLTHROUGH */
691 case CPP_HASH:
692 case CPP_PASTE:
693 {
694 unsigned char name[8];
695
696 *cpp_spell_token (parse_in, tok, name, true) = 0;
697
698 error_at (*loc, "stray %qs in program", name);
699 }
700
701 goto retry;
702
703 case CPP_OTHER:
704 {
705 cppchar_t c = tok->val.str.text[0];
706
707 if (c == '"' || c == '\'')
708 error_at (*loc, "missing terminating %c character", (int) c);
709 else if (ISGRAPH (c))
710 error_at (*loc, "stray %qc in program", (int) c);
711 else
712 {
713 rich_location rich_loc (line_table, *loc);
714 rich_loc.set_escape_on_output (true);
715 error_at (&rich_loc, "stray %<\\%o%> in program", (int) c);
716 }
717 }
718 goto retry;
719
720 case CPP_CHAR_USERDEF:
721 case CPP_WCHAR_USERDEF:
722 case CPP_CHAR16_USERDEF:
723 case CPP_CHAR32_USERDEF:
724 case CPP_UTF8CHAR_USERDEF:
725 {
726 tree literal;
727 cpp_token temp_tok = *tok;
728 const char *suffix = cpp_get_userdef_suffix (tok);
729 temp_tok.val.str.len -= strlen (s: suffix);
730 temp_tok.type = cpp_userdef_char_remove_type (type);
731 literal = build_userdef_literal (get_identifier (suffix),
732 value: lex_charconst (&temp_tok),
733 overflow: OT_NONE, NULL_TREE);
734 *value = literal;
735 }
736 break;
737
738 case CPP_CHAR:
739 case CPP_WCHAR:
740 case CPP_CHAR16:
741 case CPP_CHAR32:
742 case CPP_UTF8CHAR:
743 *value = lex_charconst (tok);
744 break;
745
746 case CPP_STRING_USERDEF:
747 case CPP_WSTRING_USERDEF:
748 case CPP_STRING16_USERDEF:
749 case CPP_STRING32_USERDEF:
750 case CPP_UTF8STRING_USERDEF:
751 {
752 tree literal, string;
753 const char *suffix = cpp_get_userdef_suffix (tok);
754 string = build_string (tok->val.str.len - strlen (s: suffix),
755 (const char *) tok->val.str.text);
756 literal = build_userdef_literal (get_identifier (suffix),
757 value: string, overflow: OT_NONE, NULL_TREE);
758 *value = literal;
759 }
760 break;
761
762 case CPP_STRING:
763 case CPP_WSTRING:
764 case CPP_STRING16:
765 case CPP_STRING32:
766 case CPP_UTF8STRING:
767 if ((lex_flags & C_LEX_STRING_NO_JOIN) == 0)
768 {
769 type = lex_string (tok, value, false,
770 (lex_flags & C_LEX_STRING_NO_TRANSLATE) == 0);
771 break;
772 }
773 *value = build_string (tok->val.str.len, (const char *) tok->val.str.text);
774 break;
775
776 case CPP_PRAGMA:
777 *value = build_int_cst (integer_type_node, tok->val.pragma);
778 break;
779
780 case CPP_HEADER_NAME:
781 *value = build_string (tok->val.str.len, (const char *)tok->val.str.text);
782 break;
783
784 /* This token should not be visible outside cpplib. */
785 case CPP_MACRO_ARG:
786 gcc_unreachable ();
787
788 /* CPP_COMMENT will appear when compiling with -C. Ignore, except
789 when it is a FALLTHROUGH comment, in that case set
790 PREV_FALLTHROUGH flag on the next non-comment token. */
791 case CPP_COMMENT:
792 if (tok->flags & PREV_FALLTHROUGH)
793 {
794 do
795 {
796 tok = get_token (pfile: parse_in, loc);
797 type = tok->type;
798 }
799 while (type == CPP_PADDING || type == CPP_COMMENT);
800 add_flags |= PREV_FALLTHROUGH;
801 goto retry_after_at;
802 }
803 goto retry;
804
805 default:
806 *value = NULL_TREE;
807 break;
808 }
809
810 if (cpp_flags)
811 *cpp_flags = tok->flags | add_flags;
812
813 timevar_pop (tv: TV_CPP);
814
815 return type;
816}
817
818/* Returns the narrowest C-visible unsigned type, starting with the
819 minimum specified by FLAGS, that can fit HIGH:LOW, or itk_none if
820 there isn't one. */
821
822static enum integer_type_kind
823narrowest_unsigned_type (const widest_int &val, unsigned int flags)
824{
825 int itk;
826
827 if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
828 itk = itk_unsigned_int;
829 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
830 itk = itk_unsigned_long;
831 else
832 itk = itk_unsigned_long_long;
833
834 for (; itk < itk_none; itk += 2 /* skip unsigned types */)
835 {
836 tree upper;
837
838 if (integer_types[itk] == NULL_TREE)
839 continue;
840 upper = TYPE_MAX_VALUE (integer_types[itk]);
841
842 if (wi::geu_p (x: wi::to_widest (t: upper), y: val))
843 return (enum integer_type_kind) itk;
844 }
845
846 return itk_none;
847}
848
849/* Ditto, but narrowest signed type. */
850static enum integer_type_kind
851narrowest_signed_type (const widest_int &val, unsigned int flags)
852{
853 int itk;
854
855 if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
856 itk = itk_int;
857 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
858 itk = itk_long;
859 else
860 itk = itk_long_long;
861
862 for (; itk < itk_none; itk += 2 /* skip signed types */)
863 {
864 tree upper;
865
866 if (integer_types[itk] == NULL_TREE)
867 continue;
868 upper = TYPE_MAX_VALUE (integer_types[itk]);
869
870 if (wi::geu_p (x: wi::to_widest (t: upper), y: val))
871 return (enum integer_type_kind) itk;
872 }
873
874 return itk_none;
875}
876
877/* Interpret TOKEN, an integer with FLAGS as classified by cpplib. */
878static tree
879interpret_integer (const cpp_token *token, unsigned int flags,
880 enum overflow_type *overflow)
881{
882 tree value, type;
883 enum integer_type_kind itk;
884 cpp_num integer;
885 HOST_WIDE_INT ival[3];
886
887 *overflow = OT_NONE;
888
889 if (UNLIKELY (flags & CPP_N_BITINT))
890 {
891 unsigned int suffix_len = 2 + ((flags & CPP_N_UNSIGNED) ? 1 : 0);
892 int max_bits_per_digit = 4; // ceil (log2 (10))
893 unsigned int prefix_len = 0;
894 bool hex = false;
895 const int bitint_maxwidth = WIDE_INT_MAX_PRECISION - 1;
896 if ((flags & CPP_N_RADIX) == CPP_N_OCTAL)
897 {
898 max_bits_per_digit = 3;
899 prefix_len = 1;
900 }
901 else if ((flags & CPP_N_RADIX) == CPP_N_HEX)
902 {
903 max_bits_per_digit = 4;
904 prefix_len = 2;
905 hex = true;
906 }
907 else if ((flags & CPP_N_RADIX) == CPP_N_BINARY)
908 {
909 max_bits_per_digit = 1;
910 prefix_len = 2;
911 }
912 int max_digits
913 = TYPE_PRECISION (intmax_type_node) >> max_bits_per_digit;
914 const int max_buf = 128;
915 if (max_digits > max_buf)
916 max_digits = max_buf;
917
918 widest_int wval;
919 unsigned int prec;
920 gcc_checking_assert (token->val.str.len > prefix_len + suffix_len
921 || token->val.str.len == 1 + suffix_len);
922 if (token->val.str.len - (prefix_len + suffix_len)
923 <= (unsigned) max_digits)
924 {
925 integer = cpp_interpret_integer (parse_in, token,
926 (flags & CPP_N_RADIX)
927 | CPP_N_UNSIGNED);
928 ival[0] = integer.low;
929 ival[1] = integer.high;
930 ival[2] = 0;
931 wval = widest_int::from_array (val: ival, len: 3);
932 }
933 else
934 {
935 unsigned char buf[3 + max_buf];
936 memcpy (dest: buf, src: token->val.str.text, n: prefix_len);
937 wval = 0U;
938 const unsigned char *p = token->val.str.text + prefix_len;
939 cpp_token tok = *token;
940 tok.val.str.text = buf;
941 if (!prefix_len)
942 max_digits = 19;
943 do
944 {
945 unsigned char *q = buf + prefix_len;
946 do
947 {
948 unsigned char c = *p++;
949 if (ISDIGIT (c) || (hex && ISXDIGIT (c)))
950 {
951 *q++ = c;
952 if (q == buf + prefix_len + max_digits)
953 break;
954 }
955 else if (c != '\'')
956 {
957 --p;
958 break;
959 }
960 }
961 while (1);
962 if (q == buf + prefix_len)
963 break;
964 else
965 {
966 wi::overflow_type wioverflow;
967 *q = '\0';
968 tok.val.str.len = q - buf;
969 if (wval == 0)
970 ;
971 else if (prefix_len)
972 {
973 prec = wi::min_precision (x: wval, sgn: UNSIGNED);
974 unsigned HOST_WIDE_INT shift
975 = (tok.val.str.len - prefix_len) * max_bits_per_digit;
976 if (prec + shift > bitint_maxwidth)
977 goto bitint_overflow;
978 wval = wi::lshift (x: wval, y: shift);
979 }
980 else
981 {
982 static unsigned HOST_WIDE_INT tens[]
983 = { 1U, 10U, 100U, 1000U,
984 HOST_WIDE_INT_UC (10000),
985 HOST_WIDE_INT_UC (100000),
986 HOST_WIDE_INT_UC (1000000),
987 HOST_WIDE_INT_UC (10000000),
988 HOST_WIDE_INT_UC (100000000),
989 HOST_WIDE_INT_UC (1000000000),
990 HOST_WIDE_INT_UC (10000000000),
991 HOST_WIDE_INT_UC (100000000000),
992 HOST_WIDE_INT_UC (1000000000000),
993 HOST_WIDE_INT_UC (10000000000000),
994 HOST_WIDE_INT_UC (100000000000000),
995 HOST_WIDE_INT_UC (1000000000000000),
996 HOST_WIDE_INT_UC (10000000000000000),
997 HOST_WIDE_INT_UC (100000000000000000),
998 HOST_WIDE_INT_UC (1000000000000000000),
999 HOST_WIDE_INT_UC (10000000000000000000) };
1000 widest_int ten = tens[q - buf];
1001 wval = wi::umul (x: wval, y: ten, overflow: &wioverflow);
1002 if (wioverflow)
1003 goto bitint_overflow;
1004 }
1005 integer = cpp_interpret_integer (parse_in, &tok,
1006 (flags & CPP_N_RADIX)
1007 | CPP_N_UNSIGNED);
1008 ival[0] = integer.low;
1009 ival[1] = integer.high;
1010 ival[2] = 0;
1011 if (prefix_len)
1012 wval = wval + widest_int::from_array (val: ival, len: 3);
1013 else
1014 {
1015 widest_int addend = widest_int::from_array (val: ival, len: 3);
1016 wval = wi::add (x: wval, y: addend, sgn: UNSIGNED, overflow: &wioverflow);
1017 if (wioverflow)
1018 goto bitint_overflow;
1019 }
1020 }
1021 }
1022 while (1);
1023 }
1024
1025 prec = wi::min_precision (x: wval, sgn: UNSIGNED);
1026 if (prec == 0)
1027 prec = 1;
1028 if ((flags & CPP_N_UNSIGNED) == 0)
1029 ++prec;
1030 if (prec > bitint_maxwidth)
1031 {
1032 bitint_overflow:
1033 if ((flags & CPP_N_UNSIGNED) != 0)
1034 error ("integer constant is too large for "
1035 "%<unsigned _BitInt(%d)%> type", bitint_maxwidth);
1036 else
1037 error ("integer constant is too large for "
1038 "%<_BitInt(%d)%> type", bitint_maxwidth);
1039 return integer_zero_node;
1040 }
1041
1042 struct bitint_info info;
1043 if (!targetm.c.bitint_type_info (prec, &info))
1044 {
1045 sorry ("%<_BitInt(%d)%> is not supported on this target", prec);
1046 return integer_zero_node;
1047 }
1048
1049 type = build_bitint_type (prec, (flags & CPP_N_UNSIGNED) != 0);
1050 return wide_int_to_tree (type, cst: wval);
1051 }
1052
1053 integer = cpp_interpret_integer (parse_in, token, flags);
1054 if (integer.overflow)
1055 *overflow = OT_OVERFLOW;
1056
1057 ival[0] = integer.low;
1058 ival[1] = integer.high;
1059 ival[2] = 0;
1060 widest_int wval = widest_int::from_array (val: ival, len: 3);
1061
1062 /* The type of a constant with a U suffix is straightforward. */
1063 if (flags & CPP_N_UNSIGNED)
1064 itk = narrowest_unsigned_type (val: wval, flags);
1065 else
1066 {
1067 /* The type of a potentially-signed integer constant varies
1068 depending on the base it's in, the standard in use, and the
1069 length suffixes. */
1070 enum integer_type_kind itk_u
1071 = narrowest_unsigned_type (val: wval, flags);
1072 enum integer_type_kind itk_s
1073 = narrowest_signed_type (val: wval, flags);
1074
1075 /* In both C89 and C99, octal and hex constants may be signed or
1076 unsigned, whichever fits tighter. We do not warn about this
1077 choice differing from the traditional choice, as the constant
1078 is probably a bit pattern and either way will work. */
1079 if ((flags & CPP_N_RADIX) != CPP_N_DECIMAL)
1080 itk = MIN (itk_u, itk_s);
1081 else
1082 {
1083 /* In C99, decimal constants are always signed.
1084 In C89, decimal constants that don't fit in long have
1085 undefined behavior; we try to make them unsigned long.
1086 In GCC's extended C89, that last is true of decimal
1087 constants that don't fit in long long, too. */
1088
1089 itk = itk_s;
1090 if (itk_s > itk_u && itk_s > itk_long)
1091 {
1092 if (!flag_isoc99)
1093 {
1094 if (itk_u < itk_unsigned_long)
1095 itk_u = itk_unsigned_long;
1096 itk = itk_u;
1097 warning (0, "this decimal constant is unsigned only in ISO C90");
1098 }
1099 else
1100 warning (OPT_Wtraditional,
1101 "this decimal constant would be unsigned in ISO C90");
1102 }
1103 }
1104 }
1105
1106 if (itk == itk_none)
1107 /* cpplib has already issued a warning for overflow. */
1108 type = ((flags & CPP_N_UNSIGNED)
1109 ? widest_unsigned_literal_type_node
1110 : widest_integer_literal_type_node);
1111 else if (flags & CPP_N_SIZE_T)
1112 {
1113 /* itk refers to fundamental types not aliased size types. */
1114 if (flags & CPP_N_UNSIGNED)
1115 type = size_type_node;
1116 else
1117 type = signed_size_type_node;
1118 }
1119 else
1120 {
1121 type = integer_types[itk];
1122 if (itk > itk_unsigned_long
1123 && (flags & CPP_N_WIDTH) != CPP_N_LARGE)
1124 emit_diagnostic
1125 ((c_dialect_cxx () ? cxx_dialect == cxx98 : !flag_isoc99)
1126 ? DK_PEDWARN : DK_WARNING,
1127 input_location, OPT_Wlong_long,
1128 (flags & CPP_N_UNSIGNED)
1129 ? "integer constant is too large for %<unsigned long%> type"
1130 : "integer constant is too large for %<long%> type");
1131 }
1132
1133 value = wide_int_to_tree (type, cst: wval);
1134
1135 /* Convert imaginary to a complex type. */
1136 if (flags & CPP_N_IMAGINARY)
1137 value = build_complex (NULL_TREE, build_int_cst (type, 0), value);
1138
1139 return value;
1140}
1141
1142/* Interpret TOKEN, a floating point number with FLAGS as classified
1143 by cpplib. For C++11 SUFFIX may contain a user-defined literal suffix. */
1144static tree
1145interpret_float (const cpp_token *token, unsigned int flags,
1146 const char *suffix, enum overflow_type *overflow)
1147{
1148 tree type;
1149 tree const_type;
1150 tree value;
1151 REAL_VALUE_TYPE real;
1152 REAL_VALUE_TYPE real_trunc;
1153 char *copy;
1154 size_t copylen;
1155
1156 *overflow = OT_NONE;
1157
1158 /* Default (no suffix) depends on whether the FLOAT_CONST_DECIMAL64
1159 pragma has been used and is either double or _Decimal64. Types
1160 that are not allowed with decimal float default to double. */
1161 if (flags & CPP_N_DEFAULT)
1162 {
1163 flags ^= CPP_N_DEFAULT;
1164 flags |= CPP_N_MEDIUM;
1165
1166 if (((flags & CPP_N_HEX) == 0) && ((flags & CPP_N_IMAGINARY) == 0))
1167 {
1168 warning (OPT_Wunsuffixed_float_constants,
1169 "unsuffixed floating constant");
1170 if (float_const_decimal64_p ())
1171 flags |= CPP_N_DFLOAT;
1172 }
1173 }
1174
1175 /* Decode _Fract and _Accum. */
1176 if (flags & CPP_N_FRACT || flags & CPP_N_ACCUM)
1177 return interpret_fixed (token, flags);
1178
1179 /* Decode type based on width and properties. */
1180 if (flags & CPP_N_DFLOAT)
1181 if (!targetm.decimal_float_supported_p ())
1182 {
1183 error ("decimal floating-point not supported for this target");
1184 return error_mark_node;
1185 }
1186 else if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1187 type = dfloat128_type_node;
1188 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1189 type = dfloat32_type_node;
1190 else
1191 type = dfloat64_type_node;
1192 else
1193 if (flags & CPP_N_WIDTH_MD)
1194 {
1195 char suffix;
1196 machine_mode mode;
1197
1198 if ((flags & CPP_N_WIDTH_MD) == CPP_N_MD_W)
1199 suffix = 'w';
1200 else
1201 suffix = 'q';
1202
1203 mode = targetm.c.mode_for_suffix (suffix);
1204 if (mode == VOIDmode)
1205 {
1206 error ("unsupported non-standard suffix on floating constant");
1207
1208 return error_mark_node;
1209 }
1210 else
1211 pedwarn (input_location, OPT_Wpedantic, "non-standard suffix on floating constant");
1212
1213 type = c_common_type_for_mode (mode, 0);
1214 /* For Q suffix, prefer float128t_type_node (__float128) type
1215 over float128_type_node (_Float128) type if they are distinct. */
1216 if (type == float128_type_node && float128t_type_node)
1217 type = float128t_type_node;
1218 gcc_assert (type);
1219 }
1220 else if ((flags & (CPP_N_FLOATN | CPP_N_FLOATNX)) != 0)
1221 {
1222 unsigned int n = (flags & CPP_N_WIDTH_FLOATN_NX) >> CPP_FLOATN_SHIFT;
1223 bool extended = (flags & CPP_N_FLOATNX) != 0;
1224 type = NULL_TREE;
1225 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
1226 if (floatn_nx_types[i].n == (int) n
1227 && floatn_nx_types[i].extended == extended)
1228 {
1229 type = FLOATN_NX_TYPE_NODE (i);
1230 break;
1231 }
1232 if (type == NULL_TREE)
1233 {
1234 error ("unsupported non-standard suffix on floating constant");
1235 return error_mark_node;
1236 }
1237 else if (!c_dialect_cxx ())
1238 {
1239 if (warn_c11_c23_compat > 0)
1240 {
1241 if (pedantic && !flag_isoc23)
1242 pedwarn (input_location, OPT_Wc11_c23_compat,
1243 "non-standard suffix on floating constant "
1244 "before C23");
1245 else
1246 warning (OPT_Wc11_c23_compat,
1247 "non-standard suffix on floating constant "
1248 "before C23");
1249 }
1250 else if (warn_c11_c23_compat != 0 && pedantic && !flag_isoc23)
1251 pedwarn (input_location, OPT_Wpedantic,
1252 "non-standard suffix on floating constant "
1253 "before C23");
1254 }
1255 else if (!extended)
1256 {
1257 if (cxx_dialect < cxx23)
1258 pedwarn (input_location, OPT_Wpedantic,
1259 "%<f%d%> or %<F%d%> suffix on floating constant only "
1260 "available with %<-std=c++2b%> or %<-std=gnu++2b%>",
1261 n, n);
1262 }
1263 else
1264 pedwarn (input_location, OPT_Wpedantic,
1265 "non-standard suffix on floating constant");
1266 }
1267 else if ((flags & CPP_N_BFLOAT16) != 0)
1268 {
1269 type = bfloat16_type_node;
1270 if (type == NULL_TREE)
1271 {
1272 error ("unsupported non-standard suffix on floating constant");
1273 return error_mark_node;
1274 }
1275 if (!c_dialect_cxx ())
1276 pedwarn (input_location, OPT_Wpedantic,
1277 "non-standard suffix on floating constant");
1278 else if (cxx_dialect < cxx23)
1279 pedwarn (input_location, OPT_Wpedantic,
1280 "%<bf16%> or %<BF16%> suffix on floating constant only "
1281 "available with %<-std=c++2b%> or %<-std=gnu++2b%>");
1282 }
1283 else if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1284 type = long_double_type_node;
1285 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL
1286 || flag_single_precision_constant)
1287 type = float_type_node;
1288 else
1289 type = double_type_node;
1290
1291 const_type = excess_precision_type (type);
1292 if (!const_type)
1293 const_type = type;
1294
1295 /* Copy the constant to a nul-terminated buffer. If the constant
1296 has any suffixes, cut them off; REAL_VALUE_ATOF/ REAL_VALUE_HTOF
1297 can't handle them. */
1298 copylen = token->val.str.len;
1299 if (flags & CPP_N_USERDEF)
1300 copylen -= strlen (s: suffix);
1301 else if (flags & CPP_N_DFLOAT)
1302 copylen -= 2;
1303 else
1304 {
1305 if ((flags & CPP_N_WIDTH) != CPP_N_MEDIUM)
1306 /* Must be an F or L or machine defined suffix. */
1307 copylen--;
1308 if (flags & CPP_N_IMAGINARY)
1309 /* I or J suffix. */
1310 copylen--;
1311 if (flags & CPP_N_FLOATNX)
1312 copylen--;
1313 if (flags & (CPP_N_FLOATN | CPP_N_FLOATNX))
1314 {
1315 unsigned int n = (flags & CPP_N_WIDTH_FLOATN_NX) >> CPP_FLOATN_SHIFT;
1316 while (n > 0)
1317 {
1318 copylen--;
1319 n /= 10;
1320 }
1321 }
1322 }
1323
1324 copy = (char *) alloca (copylen + 1);
1325 if (c_dialect_cxx () ? cxx_dialect > cxx11 : flag_isoc23)
1326 {
1327 size_t maxlen = 0;
1328 for (size_t i = 0; i < copylen; ++i)
1329 if (token->val.str.text[i] != '\'')
1330 copy[maxlen++] = token->val.str.text[i];
1331 copy[maxlen] = '\0';
1332 }
1333 else
1334 {
1335 memcpy (dest: copy, src: token->val.str.text, n: copylen);
1336 copy[copylen] = '\0';
1337 }
1338
1339 real_from_string3 (&real, copy, TYPE_MODE (const_type));
1340 if (const_type != type)
1341 /* Diagnosing if the result of converting the value with excess
1342 precision to the semantic type would overflow (with associated
1343 double rounding) is more appropriate than diagnosing if the
1344 result of converting the string directly to the semantic type
1345 would overflow. */
1346 real_convert (&real_trunc, TYPE_MODE (type), &real);
1347
1348 /* Both C and C++ require a diagnostic for a floating constant
1349 outside the range of representable values of its type. Since we
1350 have __builtin_inf* to produce an infinity, this is now a
1351 mandatory pedwarn if the target does not support infinities. */
1352 if (REAL_VALUE_ISINF (real)
1353 || (const_type != type && REAL_VALUE_ISINF (real_trunc)))
1354 {
1355 *overflow = OT_OVERFLOW;
1356 if (!(flags & CPP_N_USERDEF))
1357 {
1358 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)))
1359 pedwarn (input_location, 0,
1360 "floating constant exceeds range of %qT", type);
1361 else
1362 warning (OPT_Woverflow,
1363 "floating constant exceeds range of %qT", type);
1364 }
1365 }
1366 /* We also give a warning if the value underflows. */
1367 else if (real_equal (&real, &dconst0)
1368 || (const_type != type
1369 && real_equal (&real_trunc, &dconst0)))
1370 {
1371 REAL_VALUE_TYPE realvoidmode;
1372 int oflow = real_from_string (&realvoidmode, copy);
1373 *overflow = (oflow == 0 ? OT_NONE
1374 : (oflow < 0 ? OT_UNDERFLOW : OT_OVERFLOW));
1375 if (!(flags & CPP_N_USERDEF))
1376 {
1377 if (oflow < 0 || !real_equal (&realvoidmode, &dconst0))
1378 warning (OPT_Woverflow, "floating constant truncated to zero");
1379 }
1380 }
1381
1382 /* Create a node with determined type and value. */
1383 value = build_real (const_type, real);
1384 if (flags & CPP_N_IMAGINARY)
1385 {
1386 value = build_complex (NULL_TREE,
1387 fold_convert (const_type,
1388 integer_zero_node), value);
1389 if (type != const_type)
1390 {
1391 const_type = TREE_TYPE (value);
1392 type = build_complex_type (type);
1393 }
1394 }
1395
1396 if (type != const_type)
1397 value = build1_loc (loc: token->src_loc, code: EXCESS_PRECISION_EXPR, type, arg1: value);
1398
1399 return value;
1400}
1401
1402/* Interpret TOKEN, a fixed-point number with FLAGS as classified
1403 by cpplib. */
1404
1405static tree
1406interpret_fixed (const cpp_token *token, unsigned int flags)
1407{
1408 tree type;
1409 tree value;
1410 FIXED_VALUE_TYPE fixed;
1411 char *copy;
1412 size_t copylen;
1413
1414 copylen = token->val.str.len;
1415
1416 if (flags & CPP_N_FRACT) /* _Fract. */
1417 {
1418 if (flags & CPP_N_UNSIGNED) /* Unsigned _Fract. */
1419 {
1420 if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1421 {
1422 type = unsigned_long_long_fract_type_node;
1423 copylen -= 4;
1424 }
1425 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
1426 {
1427 type = unsigned_long_fract_type_node;
1428 copylen -= 3;
1429 }
1430 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1431 {
1432 type = unsigned_short_fract_type_node;
1433 copylen -= 3;
1434 }
1435 else
1436 {
1437 type = unsigned_fract_type_node;
1438 copylen -= 2;
1439 }
1440 }
1441 else /* Signed _Fract. */
1442 {
1443 if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1444 {
1445 type = long_long_fract_type_node;
1446 copylen -= 3;
1447 }
1448 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
1449 {
1450 type = long_fract_type_node;
1451 copylen -= 2;
1452 }
1453 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1454 {
1455 type = short_fract_type_node;
1456 copylen -= 2;
1457 }
1458 else
1459 {
1460 type = fract_type_node;
1461 copylen --;
1462 }
1463 }
1464 }
1465 else /* _Accum. */
1466 {
1467 if (flags & CPP_N_UNSIGNED) /* Unsigned _Accum. */
1468 {
1469 if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1470 {
1471 type = unsigned_long_long_accum_type_node;
1472 copylen -= 4;
1473 }
1474 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
1475 {
1476 type = unsigned_long_accum_type_node;
1477 copylen -= 3;
1478 }
1479 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1480 {
1481 type = unsigned_short_accum_type_node;
1482 copylen -= 3;
1483 }
1484 else
1485 {
1486 type = unsigned_accum_type_node;
1487 copylen -= 2;
1488 }
1489 }
1490 else /* Signed _Accum. */
1491 {
1492 if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1493 {
1494 type = long_long_accum_type_node;
1495 copylen -= 3;
1496 }
1497 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
1498 {
1499 type = long_accum_type_node;
1500 copylen -= 2;
1501 }
1502 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1503 {
1504 type = short_accum_type_node;
1505 copylen -= 2;
1506 }
1507 else
1508 {
1509 type = accum_type_node;
1510 copylen --;
1511 }
1512 }
1513 }
1514
1515 copy = (char *) alloca (copylen + 1);
1516 memcpy (dest: copy, src: token->val.str.text, n: copylen);
1517 copy[copylen] = '\0';
1518
1519 fixed_from_string (&fixed, copy, SCALAR_TYPE_MODE (type));
1520
1521 /* Create a node with determined type and value. */
1522 value = build_fixed (type, fixed);
1523
1524 return value;
1525}
1526
1527/* Convert a series of STRING, WSTRING, STRING16, STRING32 and/or
1528 UTF8STRING tokens into a tree, performing string constant
1529 concatenation. TOK is the first of these. VALP is the location to
1530 write the string into. OBJC_STRING indicates whether an '@' token
1531 preceded the incoming token (in that case, the strings can either
1532 be ObjC strings, preceded by a single '@', or normal strings, not
1533 preceded by '@'. The result will be a CPP_OBJC_STRING). Returns
1534 the CPP token type of the result (CPP_STRING, CPP_WSTRING,
1535 CPP_STRING32, CPP_STRING16, CPP_UTF8STRING, or CPP_OBJC_STRING).
1536
1537 This is unfortunately more work than it should be. If any of the
1538 strings in the series has an L prefix, the result is a wide string
1539 (6.4.5p4). Whether or not the result is a wide string affects the
1540 meaning of octal and hexadecimal escapes (6.4.4.4p6,9). But escape
1541 sequences do not continue across the boundary between two strings in
1542 a series (6.4.5p7), so we must not lose the boundaries. Therefore
1543 cpp_interpret_string takes a vector of cpp_string structures, which
1544 we must arrange to provide. */
1545
1546static enum cpp_ttype
1547lex_string (const cpp_token *tok, tree *valp, bool objc_string, bool translate)
1548{
1549 tree value;
1550 size_t concats = 0;
1551 struct obstack str_ob;
1552 struct obstack loc_ob;
1553 cpp_string istr;
1554 enum cpp_ttype type = tok->type;
1555
1556 /* Try to avoid the overhead of creating and destroying an obstack
1557 for the common case of just one string. */
1558 cpp_string str = tok->val.str;
1559 location_t init_loc = tok->src_loc;
1560 cpp_string *strs = &str;
1561 location_t *locs = NULL;
1562
1563 /* objc_at_sign_was_seen is only used when doing Objective-C string
1564 concatenation. It is 'true' if we have seen an '@' before the
1565 current string, and 'false' if not. We must see exactly one or
1566 zero '@' before each string. */
1567 bool objc_at_sign_was_seen = false;
1568
1569 retry:
1570 tok = get_token (pfile: parse_in);
1571 switch (tok->type)
1572 {
1573 case CPP_PADDING:
1574 goto retry;
1575 case CPP_ATSIGN:
1576 if (objc_string)
1577 {
1578 if (objc_at_sign_was_seen)
1579 error ("repeated %<@%> before Objective-C string");
1580
1581 objc_at_sign_was_seen = true;
1582 goto retry;
1583 }
1584 /* FALLTHROUGH */
1585
1586 default:
1587 break;
1588
1589 case CPP_WSTRING:
1590 case CPP_STRING16:
1591 case CPP_STRING32:
1592 case CPP_UTF8STRING:
1593 if (type != tok->type)
1594 {
1595 if (type == CPP_STRING)
1596 type = tok->type;
1597 else
1598 error ("unsupported non-standard concatenation of string literals");
1599 }
1600 /* FALLTHROUGH */
1601
1602 case CPP_STRING:
1603 if (!concats)
1604 {
1605 gcc_obstack_init (&str_ob);
1606 gcc_obstack_init (&loc_ob);
1607 obstack_grow (&str_ob, &str, sizeof (cpp_string));
1608 obstack_grow (&loc_ob, &init_loc, sizeof (location_t));
1609 }
1610
1611 concats++;
1612 obstack_grow (&str_ob, &tok->val.str, sizeof (cpp_string));
1613 obstack_grow (&loc_ob, &tok->src_loc, sizeof (location_t));
1614
1615 if (objc_string)
1616 objc_at_sign_was_seen = false;
1617 goto retry;
1618 }
1619
1620 /* It is an error if we saw a '@' with no following string. */
1621 if (objc_at_sign_was_seen)
1622 error ("stray %<@%> in program");
1623
1624 /* We have read one more token than we want. */
1625 _cpp_backup_tokens (parse_in, 1);
1626 if (concats)
1627 {
1628 strs = XOBFINISH (&str_ob, cpp_string *);
1629 locs = XOBFINISH (&loc_ob, location_t *);
1630 }
1631
1632 if (concats && !objc_string && !in_system_header_at (loc: input_location))
1633 warning (OPT_Wtraditional,
1634 "traditional C rejects string constant concatenation");
1635
1636 if ((translate
1637 ? cpp_interpret_string : cpp_interpret_string_notranslate)
1638 (parse_in, strs, concats + 1, &istr, type))
1639 {
1640 value = build_string (istr.len, (const char *) istr.text);
1641 free (CONST_CAST (unsigned char *, istr.text));
1642 if (concats)
1643 {
1644 gcc_assert (locs);
1645 gcc_assert (g_string_concat_db);
1646 g_string_concat_db->record_string_concatenation (num: concats + 1, locs);
1647 }
1648 }
1649 else
1650 {
1651 /* Callers cannot generally handle error_mark_node in this context,
1652 so return the empty string instead. cpp_interpret_string has
1653 issued an error. */
1654 switch (type)
1655 {
1656 default:
1657 case CPP_STRING:
1658 case CPP_UTF8STRING:
1659 if (type == CPP_UTF8STRING && flag_char8_t)
1660 {
1661 value = build_string (TYPE_PRECISION (char8_type_node)
1662 / TYPE_PRECISION (char_type_node),
1663 ""); /* char8_t is 8 bits */
1664 }
1665 else
1666 value = build_string (1, "");
1667 break;
1668 case CPP_STRING16:
1669 value = build_string (TYPE_PRECISION (char16_type_node)
1670 / TYPE_PRECISION (char_type_node),
1671 "\0"); /* char16_t is 16 bits */
1672 break;
1673 case CPP_STRING32:
1674 value = build_string (TYPE_PRECISION (char32_type_node)
1675 / TYPE_PRECISION (char_type_node),
1676 "\0\0\0"); /* char32_t is 32 bits */
1677 break;
1678 case CPP_WSTRING:
1679 value = build_string (TYPE_PRECISION (wchar_type_node)
1680 / TYPE_PRECISION (char_type_node),
1681 "\0\0\0"); /* widest supported wchar_t
1682 is 32 bits */
1683 break;
1684 }
1685 }
1686
1687 switch (type)
1688 {
1689 default:
1690 case CPP_STRING:
1691 TREE_TYPE (value) = char_array_type_node;
1692 break;
1693 case CPP_UTF8STRING:
1694 if (flag_char8_t)
1695 TREE_TYPE (value) = char8_array_type_node;
1696 else
1697 TREE_TYPE (value) = char_array_type_node;
1698 break;
1699 case CPP_STRING16:
1700 TREE_TYPE (value) = char16_array_type_node;
1701 break;
1702 case CPP_STRING32:
1703 TREE_TYPE (value) = char32_array_type_node;
1704 break;
1705 case CPP_WSTRING:
1706 TREE_TYPE (value) = wchar_array_type_node;
1707 }
1708 *valp = fix_string_type (value);
1709
1710 if (concats)
1711 {
1712 obstack_free (&str_ob, 0);
1713 obstack_free (&loc_ob, 0);
1714 }
1715
1716 return objc_string ? CPP_OBJC_STRING : type;
1717}
1718
1719/* Converts a (possibly wide) character constant token into a tree. */
1720static tree
1721lex_charconst (const cpp_token *token)
1722{
1723 cppchar_t result;
1724 tree type, value;
1725 unsigned int chars_seen;
1726 int unsignedp = 0;
1727
1728 result = cpp_interpret_charconst (parse_in, token,
1729 &chars_seen, &unsignedp);
1730
1731 if (token->type == CPP_WCHAR)
1732 type = wchar_type_node;
1733 else if (token->type == CPP_CHAR32)
1734 type = char32_type_node;
1735 else if (token->type == CPP_CHAR16)
1736 type = char16_type_node;
1737 else if (token->type == CPP_UTF8CHAR)
1738 {
1739 if (flag_char8_t)
1740 type = char8_type_node;
1741 else
1742 type = char_type_node;
1743 }
1744 /* In C, a character constant has type 'int'.
1745 In C++ 'char', but multi-char charconsts have type 'int'. */
1746 else if (!c_dialect_cxx () || chars_seen > 1)
1747 type = integer_type_node;
1748 else
1749 type = char_type_node;
1750
1751 /* Cast to cppchar_signed_t to get correct sign-extension of RESULT
1752 before possibly widening to HOST_WIDE_INT for build_int_cst. */
1753 if (unsignedp || (cppchar_signed_t) result >= 0)
1754 value = build_int_cst (type, result);
1755 else
1756 value = build_int_cst (type, (cppchar_signed_t) result);
1757
1758 return value;
1759}
1760
1761/* Helper function for c_parser_peek_conflict_marker
1762 and cp_lexer_peek_conflict_marker.
1763 Given a possible conflict marker token of kind TOK1_KIND
1764 consisting of a pair of characters, get the token kind for the
1765 standalone final character. */
1766
1767enum cpp_ttype
1768conflict_marker_get_final_tok_kind (enum cpp_ttype tok1_kind)
1769{
1770 switch (tok1_kind)
1771 {
1772 default: gcc_unreachable ();
1773 case CPP_LSHIFT:
1774 /* "<<" and '<' */
1775 return CPP_LESS;
1776
1777 case CPP_EQ_EQ:
1778 /* "==" and '=' */
1779 return CPP_EQ;
1780
1781 case CPP_RSHIFT:
1782 /* ">>" and '>' */
1783 return CPP_GREATER;
1784 }
1785}
1786

source code of gcc/c-family/c-lex.cc