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
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
26 #include "stringpool.h"
27 #include "stor-layout.h"
35 #include "splay-tree.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
*);
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
)
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
;
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
;
106 get_fileinfo (const char *name
)
109 struct c_fileinfo
*fi
;
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
);
118 return (struct c_fileinfo
*) n
->value
;
120 fi
= XNEW (struct c_fileinfo
);
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
);
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
;
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
);
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);
170 cb_ident (cpp_reader
* ARG_UNUSED (pfile
),
171 unsigned int ARG_UNUSED (line
),
172 const cpp_string
* ARG_UNUSED (str
))
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. */
189 cb_line_change (cpp_reader
* ARG_UNUSED (pfile
), const cpp_token
*token
,
192 if (token
->type
!= CPP_EOF
&& !parsing_args
)
193 input_location
= token
->src_loc
;
197 fe_file_change (const struct line_map
*new_map
)
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);
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
218 else if (LINEMAP_SYSP (new_map
) == 2)
221 ++pending_lang_change
;
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
;
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
;
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
;
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",
272 /* #define callback for DWARF and DWARF2 debug info. */
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. */
283 cb_undef (cpp_reader
* ARG_UNUSED (pfile
), source_location loc
,
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. */
293 cb_has_attribute (cpp_reader
*pfile
)
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
)
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
);
324 cpp_error (pfile
, CPP_DL_ERROR
,
325 "attribute identifier required after scope");
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
);
336 const struct attribute_spec
*attr
= lookup_attribute_spec (attr_name
);
339 if (is_attribute_p ("noreturn", TREE_VALUE (attr_name
)))
341 else if (is_attribute_p ("deprecated", TREE_VALUE (attr_name
)))
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__\"");
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
365 c_lex_with_flags (tree
*value
, location_t
*loc
, unsigned char *cpp_flags
,
368 static bool no_more_pch
;
369 const cpp_token
*tok
;
371 unsigned char add_flags
= 0;
372 enum overflow_type overflow
= OT_NONE
;
374 timevar_push (TV_CPP
);
376 tok
= cpp_get_token_with_location (parse_in
, loc
);
386 *value
= HT_IDENT_TO_GCC_IDENT (HT_NODE (tok
->val
.node
.node
));
391 const char *suffix
= NULL
;
392 unsigned int flags
= cpp_classify_number (parse_in
, tok
, &suffix
, *loc
);
394 switch (flags
& CPP_N_CATEGORY
)
397 /* cpplib has issued an error. */
398 *value
= error_mark_node
;
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
);
410 *value
= interpret_float (tok
, flags
, suffix
, &overflow
);
417 if (flags
& CPP_N_USERDEF
)
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
));
431 literal
= build_userdef_literal (suffix_id
, *value
, overflow
,
439 /* An @ may give the next token special significance in Objective-C. */
440 if (c_dialect_objc ())
442 location_t atloc
= *loc
;
446 tok
= cpp_get_token_with_location (parse_in
, &newloc
);
458 type
= lex_string (tok
, value
, true, true);
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
)))
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.
479 error_at (atloc
, "stray %<@%> in program");
490 unsigned char name
[8];
492 *cpp_spell_token (parse_in
, tok
, name
, true) = 0;
494 error_at (*loc
, "stray %qs in program", name
);
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
);
508 error ("stray %<\\%o%> in program", (int) c
);
512 case CPP_CHAR_USERDEF
:
513 case CPP_WCHAR_USERDEF
:
514 case CPP_CHAR16_USERDEF
:
515 case CPP_CHAR32_USERDEF
:
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
),
533 *value
= lex_charconst (tok
);
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
);
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);
563 *value
= build_string (tok
->val
.str
.len
, (const char *) tok
->val
.str
.text
);
567 *value
= build_int_cst (integer_type_node
, tok
->val
.pragma
);
570 /* These tokens should not be visible outside cpplib. */
571 case CPP_HEADER_NAME
:
575 /* CPP_COMMENT will appear when compiling with -C and should be
586 *cpp_flags
= tok
->flags
| add_flags
;
591 c_common_no_more_pch ();
594 timevar_pop (TV_CPP
);
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
603 static enum integer_type_kind
604 narrowest_unsigned_type (const widest_int
&val
, unsigned int flags
)
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
;
613 itk
= itk_unsigned_long_long
;
615 for (; itk
< itk_none
; itk
+= 2 /* skip unsigned types */)
619 if (integer_types
[itk
] == NULL_TREE
)
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
;
630 /* Ditto, but narrowest signed type. */
631 static enum integer_type_kind
632 narrowest_signed_type (const widest_int
&val
, unsigned int flags
)
636 if ((flags
& CPP_N_WIDTH
) == CPP_N_SMALL
)
638 else if ((flags
& CPP_N_WIDTH
) == CPP_N_MEDIUM
)
643 for (; itk
< itk_none
; itk
+= 2 /* skip signed types */)
647 if (integer_types
[itk
] == NULL_TREE
)
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
;
658 /* Interpret TOKEN, an integer with FLAGS as classified by cpplib. */
660 interpret_integer (const cpp_token
*token
, unsigned int flags
,
661 enum overflow_type
*overflow
)
664 enum integer_type_kind itk
;
666 HOST_WIDE_INT ival
[3];
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
;
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
);
684 /* The type of a potentially-signed integer constant varies
685 depending on the base it's in, the standard in use, and the
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
);
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. */
707 if (itk_s
> itk_u
&& itk_s
> itk_long
)
711 if (itk_u
< itk_unsigned_long
)
712 itk_u
= itk_unsigned_long
;
714 warning (0, "this decimal constant is unsigned only in ISO C90");
717 warning (OPT_Wtraditional
,
718 "this decimal constant would be unsigned in ISO C90");
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
);
730 type
= integer_types
[itk
];
731 if (itk
> itk_unsigned_long
732 && (flags
& CPP_N_WIDTH
) != CPP_N_LARGE
)
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
);
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. */
754 interpret_float (const cpp_token
*token
, unsigned int flags
,
755 const char *suffix
, enum overflow_type
*overflow
)
760 REAL_VALUE_TYPE real
;
761 REAL_VALUE_TYPE real_trunc
;
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
;
795 type
= dfloat64_type_node
;
797 if (flags
& CPP_N_WIDTH_MD
)
802 if ((flags
& CPP_N_WIDTH_MD
) == CPP_N_MD_W
)
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
;
815 pedwarn (input_location
, OPT_Wpedantic
, "non-standard suffix on floating constant");
817 type
= c_common_type_for_mode (mode
, 0);
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
;
826 type
= double_type_node
;
828 const_type
= excess_precision_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
)
842 if ((flags
& CPP_N_WIDTH
) != CPP_N_MEDIUM
)
843 /* Must be an F or L or machine defined suffix. */
845 if (flags
& CPP_N_IMAGINARY
)
850 copy
= (char *) alloca (copylen
+ 1);
851 if (cxx_dialect
> cxx11
)
854 for (size_t i
= 0; i
< copylen
; ++i
)
855 if (token
->val
.str
.text
[i
] != '\'')
856 copy
[maxlen
++] = token
->val
.str
.text
[i
];
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
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
);
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
);
927 /* Interpret TOKEN, a fixed-point number with FLAGS as classified
931 interpret_fixed (const cpp_token
*token
, unsigned int flags
)
935 FIXED_VALUE_TYPE fixed
;
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
;
950 else if ((flags
& CPP_N_WIDTH
) == CPP_N_MEDIUM
)
952 type
= unsigned_long_fract_type_node
;
955 else if ((flags
& CPP_N_WIDTH
) == CPP_N_SMALL
)
957 type
= unsigned_short_fract_type_node
;
962 type
= unsigned_fract_type_node
;
966 else /* Signed _Fract. */
968 if ((flags
& CPP_N_WIDTH
) == CPP_N_LARGE
)
970 type
= long_long_fract_type_node
;
973 else if ((flags
& CPP_N_WIDTH
) == CPP_N_MEDIUM
)
975 type
= long_fract_type_node
;
978 else if ((flags
& CPP_N_WIDTH
) == CPP_N_SMALL
)
980 type
= short_fract_type_node
;
985 type
= fract_type_node
;
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
;
999 else if ((flags
& CPP_N_WIDTH
) == CPP_N_MEDIUM
)
1001 type
= unsigned_long_accum_type_node
;
1004 else if ((flags
& CPP_N_WIDTH
) == CPP_N_SMALL
)
1006 type
= unsigned_short_accum_type_node
;
1011 type
= unsigned_accum_type_node
;
1015 else /* Signed _Accum. */
1017 if ((flags
& CPP_N_WIDTH
) == CPP_N_LARGE
)
1019 type
= long_long_accum_type_node
;
1022 else if ((flags
& CPP_N_WIDTH
) == CPP_N_MEDIUM
)
1024 type
= long_accum_type_node
;
1027 else if ((flags
& CPP_N_WIDTH
) == CPP_N_SMALL
)
1029 type
= short_accum_type_node
;
1034 type
= accum_type_node
;
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
);
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
)
1076 struct obstack str_ob
;
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;
1092 tok
= cpp_get_token (parse_in
);
1100 if (objc_at_sign_was_seen
)
1101 error ("repeated %<@%> before Objective-C string");
1103 objc_at_sign_was_seen
= true;
1114 case CPP_UTF8STRING
:
1115 if (type
!= tok
->type
)
1117 if (type
== CPP_STRING
)
1120 error ("unsupported non-standard concatenation of string literals");
1126 gcc_obstack_init (&str_ob
);
1127 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
1131 obstack_grow (&str_ob
, &tok
->val
.str
, sizeof (cpp_string
));
1133 objc_at_sign_was_seen
= false;
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);
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");
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
));
1159 /* Callers cannot generally handle error_mark_node in this context,
1160 so return the empty string instead. cpp_interpret_string has
1166 case CPP_UTF8STRING
:
1167 value
= build_string (1, "");
1170 value
= build_string (TYPE_PRECISION (char16_type_node
)
1171 / TYPE_PRECISION (char_type_node
),
1172 "\0"); /* char16_t is 16 bits */
1175 value
= build_string (TYPE_PRECISION (char32_type_node
)
1176 / TYPE_PRECISION (char_type_node
),
1177 "\0\0\0"); /* char32_t is 32 bits */
1180 value
= build_string (TYPE_PRECISION (wchar_type_node
)
1181 / TYPE_PRECISION (char_type_node
),
1182 "\0\0\0"); /* widest supported wchar_t
1192 case CPP_UTF8STRING
:
1193 TREE_TYPE (value
) = char_array_type_node
;
1196 TREE_TYPE (value
) = char16_array_type_node
;
1199 TREE_TYPE (value
) = char32_array_type_node
;
1202 TREE_TYPE (value
) = wchar_array_type_node
;
1204 *valp
= fix_string_type (value
);
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. */
1214 lex_charconst (const cpp_token
*token
)
1218 unsigned int chars_seen
;
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
;
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
);
1242 value
= build_int_cst (type
, (cppchar_signed_t
) result
);