re PR inline-asm/61692 (ICE in extract_insn in recog.c for asm with many parameters)
[official-gcc.git] / gcc / c-family / c-lex.c
blob357d1370c9e7f20b63e9e9393324a1158f755542
1 /* Mainly the interface between cpplib and the C front ends.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
25 #include "tree.h"
26 #include "stringpool.h"
27 #include "stor-layout.h"
28 #include "input.h"
29 #include "c-common.h"
30 #include "flags.h"
31 #include "timevar.h"
32 #include "cpplib.h"
33 #include "c-pragma.h"
34 #include "intl.h"
35 #include "splay-tree.h"
36 #include "debug.h"
37 #include "target.h"
38 #include "wide-int.h"
40 #include "attribs.h"
42 /* We may keep statistics about how long which files took to compile. */
43 static int header_time, body_time;
44 static splay_tree file_info_tree;
46 int pending_lang_change; /* If we need to switch languages - C++ only */
47 int c_header_level; /* depth in C headers - C++ only */
49 static tree interpret_integer (const cpp_token *, unsigned int,
50 enum overflow_type *);
51 static tree interpret_float (const cpp_token *, unsigned int, const char *,
52 enum overflow_type *);
53 static tree interpret_fixed (const cpp_token *, unsigned int);
54 static enum integer_type_kind narrowest_unsigned_type
55 (const widest_int &, unsigned int);
56 static enum integer_type_kind narrowest_signed_type
57 (const widest_int &, unsigned int);
58 static enum cpp_ttype lex_string (const cpp_token *, tree *, bool, bool);
59 static tree lex_charconst (const cpp_token *);
60 static void update_header_times (const char *);
61 static int dump_one_header (splay_tree_node, void *);
62 static void cb_line_change (cpp_reader *, const cpp_token *, int);
63 static void cb_ident (cpp_reader *, unsigned int, const cpp_string *);
64 static void cb_def_pragma (cpp_reader *, unsigned int);
65 static void cb_define (cpp_reader *, unsigned int, cpp_hashnode *);
66 static void cb_undef (cpp_reader *, unsigned int, cpp_hashnode *);
67 static int cb_has_attribute (cpp_reader *);
69 void
70 init_c_lex (void)
72 struct cpp_callbacks *cb;
73 struct c_fileinfo *toplevel;
75 /* The get_fileinfo data structure must be initialized before
76 cpp_read_main_file is called. */
77 toplevel = get_fileinfo ("<top level>");
78 if (flag_detailed_statistics)
80 header_time = 0;
81 body_time = get_run_time ();
82 toplevel->time = body_time;
85 cb = cpp_get_callbacks (parse_in);
87 cb->line_change = cb_line_change;
88 cb->ident = cb_ident;
89 cb->def_pragma = cb_def_pragma;
90 cb->valid_pch = c_common_valid_pch;
91 cb->read_pch = c_common_read_pch;
92 cb->has_attribute = cb_has_attribute;
94 /* Set the debug callbacks if we can use them. */
95 if ((debug_info_level == DINFO_LEVEL_VERBOSE
96 && (write_symbols == DWARF2_DEBUG
97 || write_symbols == VMS_AND_DWARF2_DEBUG))
98 || flag_dump_go_spec != NULL)
100 cb->define = cb_define;
101 cb->undef = cb_undef;
105 struct c_fileinfo *
106 get_fileinfo (const char *name)
108 splay_tree_node n;
109 struct c_fileinfo *fi;
111 if (!file_info_tree)
112 file_info_tree = splay_tree_new ((splay_tree_compare_fn) strcmp,
114 (splay_tree_delete_value_fn) free);
116 n = splay_tree_lookup (file_info_tree, (splay_tree_key) name);
117 if (n)
118 return (struct c_fileinfo *) n->value;
120 fi = XNEW (struct c_fileinfo);
121 fi->time = 0;
122 fi->interface_only = 0;
123 fi->interface_unknown = 1;
124 splay_tree_insert (file_info_tree, (splay_tree_key) name,
125 (splay_tree_value) fi);
126 return fi;
129 static void
130 update_header_times (const char *name)
132 /* Changing files again. This means currently collected time
133 is charged against header time, and body time starts back at 0. */
134 if (flag_detailed_statistics)
136 int this_time = get_run_time ();
137 struct c_fileinfo *file = get_fileinfo (name);
138 header_time += this_time - body_time;
139 file->time += this_time - body_time;
140 body_time = this_time;
144 static int
145 dump_one_header (splay_tree_node n, void * ARG_UNUSED (dummy))
147 print_time ((const char *) n->key,
148 ((struct c_fileinfo *) n->value)->time);
149 return 0;
152 void
153 dump_time_statistics (void)
155 struct c_fileinfo *file = get_fileinfo (LOCATION_FILE (input_location));
156 int this_time = get_run_time ();
157 file->time += this_time - body_time;
159 fprintf (stderr, "\n******\n");
160 print_time ("header files (total)", header_time);
161 print_time ("main file (total)", this_time - body_time);
162 fprintf (stderr, "ratio = %g : 1\n",
163 (double) header_time / (double) (this_time - body_time));
164 fprintf (stderr, "\n******\n");
166 splay_tree_foreach (file_info_tree, dump_one_header, 0);
169 static void
170 cb_ident (cpp_reader * ARG_UNUSED (pfile),
171 unsigned int ARG_UNUSED (line),
172 const cpp_string * ARG_UNUSED (str))
174 if (!flag_no_ident)
176 /* Convert escapes in the string. */
177 cpp_string cstr = { 0, 0 };
178 if (cpp_interpret_string (pfile, str, 1, &cstr, CPP_STRING))
180 targetm.asm_out.output_ident ((const char *) cstr.text);
181 free (CONST_CAST (unsigned char *, cstr.text));
186 /* Called at the start of every non-empty line. TOKEN is the first
187 lexed token on the line. Used for diagnostic line numbers. */
188 static void
189 cb_line_change (cpp_reader * ARG_UNUSED (pfile), const cpp_token *token,
190 int parsing_args)
192 if (token->type != CPP_EOF && !parsing_args)
193 input_location = token->src_loc;
196 void
197 fe_file_change (const struct line_map *new_map)
199 if (new_map == NULL)
200 return;
202 if (new_map->reason == LC_ENTER)
204 /* Don't stack the main buffer on the input stack;
205 we already did in compile_file. */
206 if (!MAIN_FILE_P (new_map))
208 unsigned int included_at = LAST_SOURCE_LINE_LOCATION (new_map - 1);
209 int line = 0;
210 if (included_at > BUILTINS_LOCATION)
211 line = SOURCE_LINE (new_map - 1, included_at);
213 input_location = new_map->start_location;
214 (*debug_hooks->start_source_file) (line, LINEMAP_FILE (new_map));
215 #ifndef NO_IMPLICIT_EXTERN_C
216 if (c_header_level)
217 ++c_header_level;
218 else if (LINEMAP_SYSP (new_map) == 2)
220 c_header_level = 1;
221 ++pending_lang_change;
223 #endif
226 else if (new_map->reason == LC_LEAVE)
228 #ifndef NO_IMPLICIT_EXTERN_C
229 if (c_header_level && --c_header_level == 0)
231 if (LINEMAP_SYSP (new_map) == 2)
232 warning (0, "badly nested C headers from preprocessor");
233 --pending_lang_change;
235 #endif
236 input_location = new_map->start_location;
238 (*debug_hooks->end_source_file) (LINEMAP_LINE (new_map));
241 update_header_times (LINEMAP_FILE (new_map));
242 input_location = new_map->start_location;
245 static void
246 cb_def_pragma (cpp_reader *pfile, source_location loc)
248 /* Issue a warning message if we have been asked to do so. Ignore
249 unknown pragmas in system headers unless an explicit
250 -Wunknown-pragmas has been given. */
251 if (warn_unknown_pragmas > in_system_header_at (input_location))
253 const unsigned char *space, *name;
254 const cpp_token *s;
255 location_t fe_loc = loc;
257 space = name = (const unsigned char *) "";
258 s = cpp_get_token (pfile);
259 if (s->type != CPP_EOF)
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);
267 warning_at (fe_loc, OPT_Wunknown_pragmas, "ignoring #pragma %s %s",
268 space, name);
272 /* #define callback for DWARF and DWARF2 debug info. */
273 static void
274 cb_define (cpp_reader *pfile, source_location loc, cpp_hashnode *node)
276 const struct line_map *map = linemap_lookup (line_table, loc);
277 (*debug_hooks->define) (SOURCE_LINE (map, loc),
278 (const char *) cpp_macro_definition (pfile, node));
281 /* #undef callback for DWARF and DWARF2 debug info. */
282 static void
283 cb_undef (cpp_reader * ARG_UNUSED (pfile), source_location loc,
284 cpp_hashnode *node)
286 const struct line_map *map = linemap_lookup (line_table, loc);
287 (*debug_hooks->undef) (SOURCE_LINE (map, loc),
288 (const char *) NODE_NAME (node));
291 /* Callback for has_attribute. */
292 static int
293 cb_has_attribute (cpp_reader *pfile)
295 int result = 0;
296 bool paren = false;
297 tree attr_ns = NULL_TREE, attr_id = NULL_TREE, attr_name = NULL_TREE;
298 const cpp_token *token;
300 token = cpp_get_token (pfile);
301 if (token->type == CPP_OPEN_PAREN)
303 paren = true;
304 token = cpp_get_token (pfile);
307 if (token->type == CPP_NAME)
309 //node = token->val.node.node;
310 const cpp_token *nxt_token = cpp_peek_token (pfile, 0);
311 if (c_dialect_cxx() && nxt_token->type == CPP_SCOPE)
313 nxt_token = cpp_get_token (pfile); // Eat scope.
314 nxt_token = cpp_get_token (pfile);
315 if (nxt_token->type == CPP_NAME)
317 attr_ns = get_identifier (
318 (const char *) cpp_token_as_text (pfile, token));
319 attr_id = get_identifier (
320 (const char *) cpp_token_as_text (pfile, nxt_token));
321 attr_name = build_tree_list (attr_ns, attr_id);
323 else
324 cpp_error (pfile, CPP_DL_ERROR,
325 "attribute identifier required after scope");
327 else
329 attr_ns = get_identifier ("gnu");
330 attr_id = get_identifier (
331 (const char *) cpp_token_as_text (pfile, token));
332 attr_name = build_tree_list (attr_ns, attr_id);
334 if (attr_name)
336 const struct attribute_spec *attr = lookup_attribute_spec (attr_name);
337 if (attr)
339 if (is_attribute_p ("noreturn", TREE_VALUE (attr_name)))
340 result = 200809;
341 else if (is_attribute_p ("deprecated", TREE_VALUE (attr_name)))
342 result = 201309;
343 else
344 result = 1;
348 else
349 cpp_error (pfile, CPP_DL_ERROR,
350 "operator \"__has_attribute__\" requires an identifier");
352 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
353 cpp_error (pfile, CPP_DL_ERROR,
354 "missing ')' after \"__has_attribute__\"");
356 return result;
360 /* Read a token and return its type. Fill *VALUE with its value, if
361 applicable. Fill *CPP_FLAGS with the token's flags, if it is
362 non-NULL. */
364 enum cpp_ttype
365 c_lex_with_flags (tree *value, location_t *loc, unsigned char *cpp_flags,
366 int lex_flags)
368 static bool no_more_pch;
369 const cpp_token *tok;
370 enum cpp_ttype type;
371 unsigned char add_flags = 0;
372 enum overflow_type overflow = OT_NONE;
374 timevar_push (TV_CPP);
375 retry:
376 tok = cpp_get_token_with_location (parse_in, loc);
377 type = tok->type;
379 retry_after_at:
380 switch (type)
382 case CPP_PADDING:
383 goto retry;
385 case CPP_NAME:
386 *value = HT_IDENT_TO_GCC_IDENT (HT_NODE (tok->val.node.node));
387 break;
389 case CPP_NUMBER:
391 const char *suffix = NULL;
392 unsigned int flags = cpp_classify_number (parse_in, tok, &suffix, *loc);
394 switch (flags & CPP_N_CATEGORY)
396 case CPP_N_INVALID:
397 /* cpplib has issued an error. */
398 *value = error_mark_node;
399 break;
401 case CPP_N_INTEGER:
402 /* C++ uses '0' to mark virtual functions as pure.
403 Set PURE_ZERO to pass this information to the C++ parser. */
404 if (tok->val.str.len == 1 && *tok->val.str.text == '0')
405 add_flags = PURE_ZERO;
406 *value = interpret_integer (tok, flags, &overflow);
407 break;
409 case CPP_N_FLOATING:
410 *value = interpret_float (tok, flags, suffix, &overflow);
411 break;
413 default:
414 gcc_unreachable ();
417 if (flags & CPP_N_USERDEF)
419 char *str;
420 tree literal;
421 tree suffix_id = get_identifier (suffix);
422 int len = tok->val.str.len - strlen (suffix);
423 /* If this is going to be used as a C string to pass to a
424 raw literal operator, we need to add a trailing NUL. */
425 tree num_string = build_string (len + 1,
426 (const char *) tok->val.str.text);
427 TREE_TYPE (num_string) = char_array_type_node;
428 num_string = fix_string_type (num_string);
429 str = CONST_CAST (char *, TREE_STRING_POINTER (num_string));
430 str[len] = '\0';
431 literal = build_userdef_literal (suffix_id, *value, overflow,
432 num_string);
433 *value = literal;
436 break;
438 case CPP_ATSIGN:
439 /* An @ may give the next token special significance in Objective-C. */
440 if (c_dialect_objc ())
442 location_t atloc = *loc;
443 location_t newloc;
445 retry_at:
446 tok = cpp_get_token_with_location (parse_in, &newloc);
447 type = tok->type;
448 switch (type)
450 case CPP_PADDING:
451 goto retry_at;
453 case CPP_STRING:
454 case CPP_WSTRING:
455 case CPP_STRING16:
456 case CPP_STRING32:
457 case CPP_UTF8STRING:
458 type = lex_string (tok, value, true, true);
459 break;
461 case CPP_NAME:
462 *value = HT_IDENT_TO_GCC_IDENT (HT_NODE (tok->val.node.node));
463 if (OBJC_IS_AT_KEYWORD (C_RID_CODE (*value))
464 || OBJC_IS_CXX_KEYWORD (C_RID_CODE (*value)))
466 type = CPP_AT_NAME;
467 /* Note the complication: if we found an OBJC_CXX
468 keyword, for example, 'class', we will be
469 returning a token of type CPP_AT_NAME and rid
470 code RID_CLASS (not RID_AT_CLASS). The language
471 parser needs to convert that to RID_AT_CLASS.
473 break;
475 /* FALLTHROUGH */
477 default:
478 /* ... or not. */
479 error_at (atloc, "stray %<@%> in program");
480 *loc = newloc;
481 goto retry_after_at;
483 break;
486 /* FALLTHROUGH */
487 case CPP_HASH:
488 case CPP_PASTE:
490 unsigned char name[8];
492 *cpp_spell_token (parse_in, tok, name, true) = 0;
494 error_at (*loc, "stray %qs in program", name);
497 goto retry;
499 case CPP_OTHER:
501 cppchar_t c = tok->val.str.text[0];
503 if (c == '"' || c == '\'')
504 error ("missing terminating %c character", (int) c);
505 else if (ISGRAPH (c))
506 error ("stray %qc in program", (int) c);
507 else
508 error ("stray %<\\%o%> in program", (int) c);
510 goto retry;
512 case CPP_CHAR_USERDEF:
513 case CPP_WCHAR_USERDEF:
514 case CPP_CHAR16_USERDEF:
515 case CPP_CHAR32_USERDEF:
517 tree literal;
518 cpp_token temp_tok = *tok;
519 const char *suffix = cpp_get_userdef_suffix (tok);
520 temp_tok.val.str.len -= strlen (suffix);
521 temp_tok.type = cpp_userdef_char_remove_type (type);
522 literal = build_userdef_literal (get_identifier (suffix),
523 lex_charconst (&temp_tok),
524 OT_NONE, NULL_TREE);
525 *value = literal;
527 break;
529 case CPP_CHAR:
530 case CPP_WCHAR:
531 case CPP_CHAR16:
532 case CPP_CHAR32:
533 *value = lex_charconst (tok);
534 break;
536 case CPP_STRING_USERDEF:
537 case CPP_WSTRING_USERDEF:
538 case CPP_STRING16_USERDEF:
539 case CPP_STRING32_USERDEF:
540 case CPP_UTF8STRING_USERDEF:
542 tree literal, string;
543 const char *suffix = cpp_get_userdef_suffix (tok);
544 string = build_string (tok->val.str.len - strlen (suffix),
545 (const char *) tok->val.str.text);
546 literal = build_userdef_literal (get_identifier (suffix),
547 string, OT_NONE, NULL_TREE);
548 *value = literal;
550 break;
552 case CPP_STRING:
553 case CPP_WSTRING:
554 case CPP_STRING16:
555 case CPP_STRING32:
556 case CPP_UTF8STRING:
557 if ((lex_flags & C_LEX_STRING_NO_JOIN) == 0)
559 type = lex_string (tok, value, false,
560 (lex_flags & C_LEX_STRING_NO_TRANSLATE) == 0);
561 break;
563 *value = build_string (tok->val.str.len, (const char *) tok->val.str.text);
564 break;
566 case CPP_PRAGMA:
567 *value = build_int_cst (integer_type_node, tok->val.pragma);
568 break;
570 /* These tokens should not be visible outside cpplib. */
571 case CPP_HEADER_NAME:
572 case CPP_MACRO_ARG:
573 gcc_unreachable ();
575 /* CPP_COMMENT will appear when compiling with -C and should be
576 ignored. */
577 case CPP_COMMENT:
578 goto retry;
580 default:
581 *value = NULL_TREE;
582 break;
585 if (cpp_flags)
586 *cpp_flags = tok->flags | add_flags;
588 if (!no_more_pch)
590 no_more_pch = true;
591 c_common_no_more_pch ();
594 timevar_pop (TV_CPP);
596 return type;
599 /* Returns the narrowest C-visible unsigned type, starting with the
600 minimum specified by FLAGS, that can fit HIGH:LOW, or itk_none if
601 there isn't one. */
603 static enum integer_type_kind
604 narrowest_unsigned_type (const widest_int &val, unsigned int flags)
606 int itk;
608 if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
609 itk = itk_unsigned_int;
610 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
611 itk = itk_unsigned_long;
612 else
613 itk = itk_unsigned_long_long;
615 for (; itk < itk_none; itk += 2 /* skip unsigned types */)
617 tree upper;
619 if (integer_types[itk] == NULL_TREE)
620 continue;
621 upper = TYPE_MAX_VALUE (integer_types[itk]);
623 if (wi::geu_p (wi::to_widest (upper), val))
624 return (enum integer_type_kind) itk;
627 return itk_none;
630 /* Ditto, but narrowest signed type. */
631 static enum integer_type_kind
632 narrowest_signed_type (const widest_int &val, unsigned int flags)
634 int itk;
636 if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
637 itk = itk_int;
638 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
639 itk = itk_long;
640 else
641 itk = itk_long_long;
643 for (; itk < itk_none; itk += 2 /* skip signed types */)
645 tree upper;
647 if (integer_types[itk] == NULL_TREE)
648 continue;
649 upper = TYPE_MAX_VALUE (integer_types[itk]);
651 if (wi::geu_p (wi::to_widest (upper), val))
652 return (enum integer_type_kind) itk;
655 return itk_none;
658 /* Interpret TOKEN, an integer with FLAGS as classified by cpplib. */
659 static tree
660 interpret_integer (const cpp_token *token, unsigned int flags,
661 enum overflow_type *overflow)
663 tree value, type;
664 enum integer_type_kind itk;
665 cpp_num integer;
666 HOST_WIDE_INT ival[3];
668 *overflow = OT_NONE;
670 integer = cpp_interpret_integer (parse_in, token, flags);
671 if (integer.overflow)
672 *overflow = OT_OVERFLOW;
674 ival[0] = integer.low;
675 ival[1] = integer.high;
676 ival[2] = 0;
677 widest_int wval = widest_int::from_array (ival, 3);
679 /* The type of a constant with a U suffix is straightforward. */
680 if (flags & CPP_N_UNSIGNED)
681 itk = narrowest_unsigned_type (wval, flags);
682 else
684 /* The type of a potentially-signed integer constant varies
685 depending on the base it's in, the standard in use, and the
686 length suffixes. */
687 enum integer_type_kind itk_u
688 = narrowest_unsigned_type (wval, flags);
689 enum integer_type_kind itk_s
690 = narrowest_signed_type (wval, flags);
692 /* In both C89 and C99, octal and hex constants may be signed or
693 unsigned, whichever fits tighter. We do not warn about this
694 choice differing from the traditional choice, as the constant
695 is probably a bit pattern and either way will work. */
696 if ((flags & CPP_N_RADIX) != CPP_N_DECIMAL)
697 itk = MIN (itk_u, itk_s);
698 else
700 /* In C99, decimal constants are always signed.
701 In C89, decimal constants that don't fit in long have
702 undefined behavior; we try to make them unsigned long.
703 In GCC's extended C89, that last is true of decimal
704 constants that don't fit in long long, too. */
706 itk = itk_s;
707 if (itk_s > itk_u && itk_s > itk_long)
709 if (!flag_isoc99)
711 if (itk_u < itk_unsigned_long)
712 itk_u = itk_unsigned_long;
713 itk = itk_u;
714 warning (0, "this decimal constant is unsigned only in ISO C90");
716 else
717 warning (OPT_Wtraditional,
718 "this decimal constant would be unsigned in ISO C90");
723 if (itk == itk_none)
724 /* cpplib has already issued a warning for overflow. */
725 type = ((flags & CPP_N_UNSIGNED)
726 ? widest_unsigned_literal_type_node
727 : widest_integer_literal_type_node);
728 else
730 type = integer_types[itk];
731 if (itk > itk_unsigned_long
732 && (flags & CPP_N_WIDTH) != CPP_N_LARGE)
733 emit_diagnostic
734 ((c_dialect_cxx () ? cxx_dialect == cxx98 : !flag_isoc99)
735 ? DK_PEDWARN : DK_WARNING,
736 input_location, OPT_Wlong_long,
737 (flags & CPP_N_UNSIGNED)
738 ? "integer constant is too large for %<unsigned long%> type"
739 : "integer constant is too large for %<long%> type");
742 value = wide_int_to_tree (type, wval);
744 /* Convert imaginary to a complex type. */
745 if (flags & CPP_N_IMAGINARY)
746 value = build_complex (NULL_TREE, build_int_cst (type, 0), value);
748 return value;
751 /* Interpret TOKEN, a floating point number with FLAGS as classified
752 by cpplib. For C++0X SUFFIX may contain a user-defined literal suffix. */
753 static tree
754 interpret_float (const cpp_token *token, unsigned int flags,
755 const char *suffix, enum overflow_type *overflow)
757 tree type;
758 tree const_type;
759 tree value;
760 REAL_VALUE_TYPE real;
761 REAL_VALUE_TYPE real_trunc;
762 char *copy;
763 size_t copylen;
765 *overflow = OT_NONE;
767 /* Default (no suffix) depends on whether the FLOAT_CONST_DECIMAL64
768 pragma has been used and is either double or _Decimal64. Types
769 that are not allowed with decimal float default to double. */
770 if (flags & CPP_N_DEFAULT)
772 flags ^= CPP_N_DEFAULT;
773 flags |= CPP_N_MEDIUM;
775 if (((flags & CPP_N_HEX) == 0) && ((flags & CPP_N_IMAGINARY) == 0))
777 warning (OPT_Wunsuffixed_float_constants,
778 "unsuffixed float constant");
779 if (float_const_decimal64_p ())
780 flags |= CPP_N_DFLOAT;
784 /* Decode _Fract and _Accum. */
785 if (flags & CPP_N_FRACT || flags & CPP_N_ACCUM)
786 return interpret_fixed (token, flags);
788 /* Decode type based on width and properties. */
789 if (flags & CPP_N_DFLOAT)
790 if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
791 type = dfloat128_type_node;
792 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
793 type = dfloat32_type_node;
794 else
795 type = dfloat64_type_node;
796 else
797 if (flags & CPP_N_WIDTH_MD)
799 char suffix;
800 machine_mode mode;
802 if ((flags & CPP_N_WIDTH_MD) == CPP_N_MD_W)
803 suffix = 'w';
804 else
805 suffix = 'q';
807 mode = targetm.c.mode_for_suffix (suffix);
808 if (mode == VOIDmode)
810 error ("unsupported non-standard suffix on floating constant");
812 return error_mark_node;
814 else
815 pedwarn (input_location, OPT_Wpedantic, "non-standard suffix on floating constant");
817 type = c_common_type_for_mode (mode, 0);
818 gcc_assert (type);
820 else if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
821 type = long_double_type_node;
822 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL
823 || flag_single_precision_constant)
824 type = float_type_node;
825 else
826 type = double_type_node;
828 const_type = excess_precision_type (type);
829 if (!const_type)
830 const_type = type;
832 /* Copy the constant to a nul-terminated buffer. If the constant
833 has any suffixes, cut them off; REAL_VALUE_ATOF/ REAL_VALUE_HTOF
834 can't handle them. */
835 copylen = token->val.str.len;
836 if (flags & CPP_N_USERDEF)
837 copylen -= strlen (suffix);
838 else if (flags & CPP_N_DFLOAT)
839 copylen -= 2;
840 else
842 if ((flags & CPP_N_WIDTH) != CPP_N_MEDIUM)
843 /* Must be an F or L or machine defined suffix. */
844 copylen--;
845 if (flags & CPP_N_IMAGINARY)
846 /* I or J suffix. */
847 copylen--;
850 copy = (char *) alloca (copylen + 1);
851 if (cxx_dialect > cxx11)
853 size_t maxlen = 0;
854 for (size_t i = 0; i < copylen; ++i)
855 if (token->val.str.text[i] != '\'')
856 copy[maxlen++] = token->val.str.text[i];
857 copy[maxlen] = '\0';
859 else
861 memcpy (copy, token->val.str.text, copylen);
862 copy[copylen] = '\0';
865 real_from_string3 (&real, copy, TYPE_MODE (const_type));
866 if (const_type != type)
867 /* Diagnosing if the result of converting the value with excess
868 precision to the semantic type would overflow (with associated
869 double rounding) is more appropriate than diagnosing if the
870 result of converting the string directly to the semantic type
871 would overflow. */
872 real_convert (&real_trunc, TYPE_MODE (type), &real);
874 /* Both C and C++ require a diagnostic for a floating constant
875 outside the range of representable values of its type. Since we
876 have __builtin_inf* to produce an infinity, this is now a
877 mandatory pedwarn if the target does not support infinities. */
878 if (REAL_VALUE_ISINF (real)
879 || (const_type != type && REAL_VALUE_ISINF (real_trunc)))
881 *overflow = OT_OVERFLOW;
882 if (!(flags & CPP_N_USERDEF))
884 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)))
885 pedwarn (input_location, 0,
886 "floating constant exceeds range of %qT", type);
887 else
888 warning (OPT_Woverflow,
889 "floating constant exceeds range of %qT", type);
892 /* We also give a warning if the value underflows. */
893 else if (REAL_VALUES_EQUAL (real, dconst0)
894 || (const_type != type
895 && REAL_VALUES_EQUAL (real_trunc, dconst0)))
897 REAL_VALUE_TYPE realvoidmode;
898 int oflow = real_from_string (&realvoidmode, copy);
899 *overflow = (oflow == 0 ? OT_NONE
900 : (oflow < 0 ? OT_UNDERFLOW : OT_OVERFLOW));
901 if (!(flags & CPP_N_USERDEF))
903 if (oflow < 0 || !REAL_VALUES_EQUAL (realvoidmode, dconst0))
904 warning (OPT_Woverflow, "floating constant truncated to zero");
908 /* Create a node with determined type and value. */
909 value = build_real (const_type, real);
910 if (flags & CPP_N_IMAGINARY)
912 value = build_complex (NULL_TREE, convert (const_type,
913 integer_zero_node), value);
914 if (type != const_type)
916 const_type = TREE_TYPE (value);
917 type = build_complex_type (type);
921 if (type != const_type)
922 value = build1 (EXCESS_PRECISION_EXPR, type, value);
924 return value;
927 /* Interpret TOKEN, a fixed-point number with FLAGS as classified
928 by cpplib. */
930 static tree
931 interpret_fixed (const cpp_token *token, unsigned int flags)
933 tree type;
934 tree value;
935 FIXED_VALUE_TYPE fixed;
936 char *copy;
937 size_t copylen;
939 copylen = token->val.str.len;
941 if (flags & CPP_N_FRACT) /* _Fract. */
943 if (flags & CPP_N_UNSIGNED) /* Unsigned _Fract. */
945 if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
947 type = unsigned_long_long_fract_type_node;
948 copylen -= 4;
950 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
952 type = unsigned_long_fract_type_node;
953 copylen -= 3;
955 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
957 type = unsigned_short_fract_type_node;
958 copylen -= 3;
960 else
962 type = unsigned_fract_type_node;
963 copylen -= 2;
966 else /* Signed _Fract. */
968 if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
970 type = long_long_fract_type_node;
971 copylen -= 3;
973 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
975 type = long_fract_type_node;
976 copylen -= 2;
978 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
980 type = short_fract_type_node;
981 copylen -= 2;
983 else
985 type = fract_type_node;
986 copylen --;
990 else /* _Accum. */
992 if (flags & CPP_N_UNSIGNED) /* Unsigned _Accum. */
994 if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
996 type = unsigned_long_long_accum_type_node;
997 copylen -= 4;
999 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
1001 type = unsigned_long_accum_type_node;
1002 copylen -= 3;
1004 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1006 type = unsigned_short_accum_type_node;
1007 copylen -= 3;
1009 else
1011 type = unsigned_accum_type_node;
1012 copylen -= 2;
1015 else /* Signed _Accum. */
1017 if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
1019 type = long_long_accum_type_node;
1020 copylen -= 3;
1022 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
1024 type = long_accum_type_node;
1025 copylen -= 2;
1027 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
1029 type = short_accum_type_node;
1030 copylen -= 2;
1032 else
1034 type = accum_type_node;
1035 copylen --;
1040 copy = (char *) alloca (copylen + 1);
1041 memcpy (copy, token->val.str.text, copylen);
1042 copy[copylen] = '\0';
1044 fixed_from_string (&fixed, copy, TYPE_MODE (type));
1046 /* Create a node with determined type and value. */
1047 value = build_fixed (type, fixed);
1049 return value;
1052 /* Convert a series of STRING, WSTRING, STRING16, STRING32 and/or
1053 UTF8STRING tokens into a tree, performing string constant
1054 concatenation. TOK is the first of these. VALP is the location to
1055 write the string into. OBJC_STRING indicates whether an '@' token
1056 preceded the incoming token (in that case, the strings can either
1057 be ObjC strings, preceded by a single '@', or normal strings, not
1058 preceded by '@'. The result will be a CPP_OBJC_STRING). Returns
1059 the CPP token type of the result (CPP_STRING, CPP_WSTRING,
1060 CPP_STRING32, CPP_STRING16, CPP_UTF8STRING, or CPP_OBJC_STRING).
1062 This is unfortunately more work than it should be. If any of the
1063 strings in the series has an L prefix, the result is a wide string
1064 (6.4.5p4). Whether or not the result is a wide string affects the
1065 meaning of octal and hexadecimal escapes (6.4.4.4p6,9). But escape
1066 sequences do not continue across the boundary between two strings in
1067 a series (6.4.5p7), so we must not lose the boundaries. Therefore
1068 cpp_interpret_string takes a vector of cpp_string structures, which
1069 we must arrange to provide. */
1071 static enum cpp_ttype
1072 lex_string (const cpp_token *tok, tree *valp, bool objc_string, bool translate)
1074 tree value;
1075 size_t concats = 0;
1076 struct obstack str_ob;
1077 cpp_string istr;
1078 enum cpp_ttype type = tok->type;
1080 /* Try to avoid the overhead of creating and destroying an obstack
1081 for the common case of just one string. */
1082 cpp_string str = tok->val.str;
1083 cpp_string *strs = &str;
1085 /* objc_at_sign_was_seen is only used when doing Objective-C string
1086 concatenation. It is 'true' if we have seen an '@' before the
1087 current string, and 'false' if not. We must see exactly one or
1088 zero '@' before each string. */
1089 bool objc_at_sign_was_seen = false;
1091 retry:
1092 tok = cpp_get_token (parse_in);
1093 switch (tok->type)
1095 case CPP_PADDING:
1096 goto retry;
1097 case CPP_ATSIGN:
1098 if (objc_string)
1100 if (objc_at_sign_was_seen)
1101 error ("repeated %<@%> before Objective-C string");
1103 objc_at_sign_was_seen = true;
1104 goto retry;
1106 /* FALLTHROUGH */
1108 default:
1109 break;
1111 case CPP_WSTRING:
1112 case CPP_STRING16:
1113 case CPP_STRING32:
1114 case CPP_UTF8STRING:
1115 if (type != tok->type)
1117 if (type == CPP_STRING)
1118 type = tok->type;
1119 else
1120 error ("unsupported non-standard concatenation of string literals");
1123 case CPP_STRING:
1124 if (!concats)
1126 gcc_obstack_init (&str_ob);
1127 obstack_grow (&str_ob, &str, sizeof (cpp_string));
1130 concats++;
1131 obstack_grow (&str_ob, &tok->val.str, sizeof (cpp_string));
1132 if (objc_string)
1133 objc_at_sign_was_seen = false;
1134 goto retry;
1137 /* It is an error if we saw a '@' with no following string. */
1138 if (objc_at_sign_was_seen)
1139 error ("stray %<@%> in program");
1141 /* We have read one more token than we want. */
1142 _cpp_backup_tokens (parse_in, 1);
1143 if (concats)
1144 strs = XOBFINISH (&str_ob, cpp_string *);
1146 if (concats && !objc_string && !in_system_header_at (input_location))
1147 warning (OPT_Wtraditional,
1148 "traditional C rejects string constant concatenation");
1150 if ((translate
1151 ? cpp_interpret_string : cpp_interpret_string_notranslate)
1152 (parse_in, strs, concats + 1, &istr, type))
1154 value = build_string (istr.len, (const char *) istr.text);
1155 free (CONST_CAST (unsigned char *, istr.text));
1157 else
1159 /* Callers cannot generally handle error_mark_node in this context,
1160 so return the empty string instead. cpp_interpret_string has
1161 issued an error. */
1162 switch (type)
1164 default:
1165 case CPP_STRING:
1166 case CPP_UTF8STRING:
1167 value = build_string (1, "");
1168 break;
1169 case CPP_STRING16:
1170 value = build_string (TYPE_PRECISION (char16_type_node)
1171 / TYPE_PRECISION (char_type_node),
1172 "\0"); /* char16_t is 16 bits */
1173 break;
1174 case CPP_STRING32:
1175 value = build_string (TYPE_PRECISION (char32_type_node)
1176 / TYPE_PRECISION (char_type_node),
1177 "\0\0\0"); /* char32_t is 32 bits */
1178 break;
1179 case CPP_WSTRING:
1180 value = build_string (TYPE_PRECISION (wchar_type_node)
1181 / TYPE_PRECISION (char_type_node),
1182 "\0\0\0"); /* widest supported wchar_t
1183 is 32 bits */
1184 break;
1188 switch (type)
1190 default:
1191 case CPP_STRING:
1192 case CPP_UTF8STRING:
1193 TREE_TYPE (value) = char_array_type_node;
1194 break;
1195 case CPP_STRING16:
1196 TREE_TYPE (value) = char16_array_type_node;
1197 break;
1198 case CPP_STRING32:
1199 TREE_TYPE (value) = char32_array_type_node;
1200 break;
1201 case CPP_WSTRING:
1202 TREE_TYPE (value) = wchar_array_type_node;
1204 *valp = fix_string_type (value);
1206 if (concats)
1207 obstack_free (&str_ob, 0);
1209 return objc_string ? CPP_OBJC_STRING : type;
1212 /* Converts a (possibly wide) character constant token into a tree. */
1213 static tree
1214 lex_charconst (const cpp_token *token)
1216 cppchar_t result;
1217 tree type, value;
1218 unsigned int chars_seen;
1219 int unsignedp = 0;
1221 result = cpp_interpret_charconst (parse_in, token,
1222 &chars_seen, &unsignedp);
1224 if (token->type == CPP_WCHAR)
1225 type = wchar_type_node;
1226 else if (token->type == CPP_CHAR32)
1227 type = char32_type_node;
1228 else if (token->type == CPP_CHAR16)
1229 type = char16_type_node;
1230 /* In C, a character constant has type 'int'.
1231 In C++ 'char', but multi-char charconsts have type 'int'. */
1232 else if (!c_dialect_cxx () || chars_seen > 1)
1233 type = integer_type_node;
1234 else
1235 type = char_type_node;
1237 /* Cast to cppchar_signed_t to get correct sign-extension of RESULT
1238 before possibly widening to HOST_WIDE_INT for build_int_cst. */
1239 if (unsignedp || (cppchar_signed_t) result >= 0)
1240 value = build_int_cst (type, result);
1241 else
1242 value = build_int_cst (type, (cppchar_signed_t) result);
1244 return value;