Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 1 | /* Parse C expressions for cpplib. |
Richard Sandiford | 35c3d61 | 2014-01-02 22:24:45 +0000 | [diff] [blame] | 2 | Copyright (C) 1987-2014 Free Software Foundation, Inc. |
Richard Kenner | e38992e | 2000-04-18 20:42:00 +0000 | [diff] [blame] | 3 | Contributed by Per Bothner, 1994. |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 4 | |
| 5 | This program is free software; you can redistribute it and/or modify it |
| 6 | under the terms of the GNU General Public License as published by the |
Jakub Jelinek | 748086b | 2009-04-09 17:00:19 +0200 | [diff] [blame] | 7 | Free Software Foundation; either version 3, or (at your option) any |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 8 | later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
Jakub Jelinek | 748086b | 2009-04-09 17:00:19 +0200 | [diff] [blame] | 16 | along with this program; see the file COPYING3. If not see |
| 17 | <http://www.gnu.org/licenses/>. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 18 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 19 | #include "config.h" |
Kaveh R. Ghazi | b04cd507 | 1998-03-30 12:05:54 +0000 | [diff] [blame] | 20 | #include "system.h" |
Kaveh R. Ghazi | 487a6e0 | 1998-05-19 08:42:48 +0000 | [diff] [blame] | 21 | #include "cpplib.h" |
Paolo Bonzini | 4f4e53dd | 2004-05-24 10:50:45 +0000 | [diff] [blame] | 22 | #include "internal.h" |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 23 | |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 24 | #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT) |
| 25 | #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2)) |
| 26 | #define LOW_PART(num_part) (num_part & HALF_MASK) |
| 27 | #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2)) |
| 28 | |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 29 | struct op |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 30 | { |
Neil Booth | 68e65275 | 2002-07-20 13:31:56 +0000 | [diff] [blame] | 31 | const cpp_token *token; /* The token forming op (for diagnostics). */ |
Neil Booth | ad28cff | 2002-07-18 22:08:35 +0000 | [diff] [blame] | 32 | cpp_num value; /* The value logically "right" of op. */ |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 33 | source_location loc; /* The location of this value. */ |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 34 | enum cpp_ttype op; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 35 | }; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 36 | |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 37 | /* Some simple utility routines on double integers. */ |
| 38 | #define num_zerop(num) ((num.low | num.high) == 0) |
| 39 | #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high) |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 40 | static bool num_positive (cpp_num, size_t); |
| 41 | static bool num_greater_eq (cpp_num, cpp_num, size_t); |
| 42 | static cpp_num num_trim (cpp_num, size_t); |
| 43 | static cpp_num num_part_mul (cpp_num_part, cpp_num_part); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 44 | |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 45 | static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype); |
| 46 | static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype); |
| 47 | static cpp_num num_negate (cpp_num, size_t); |
| 48 | static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype); |
| 49 | static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num, |
| 50 | enum cpp_ttype); |
| 51 | static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num, |
| 52 | enum cpp_ttype); |
| 53 | static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num); |
Manuel López-Ibáñez | b506a5a | 2009-06-18 15:10:23 +0000 | [diff] [blame] | 54 | static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype, |
| 55 | source_location); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 56 | static cpp_num num_lshift (cpp_num, size_t, size_t); |
| 57 | static cpp_num num_rshift (cpp_num, size_t, size_t); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 58 | |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 59 | static cpp_num append_digit (cpp_num, int, int, size_t); |
| 60 | static cpp_num parse_defined (cpp_reader *); |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 61 | static cpp_num eval_token (cpp_reader *, const cpp_token *, source_location); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 62 | static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype); |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 63 | static unsigned int interpret_float_suffix (cpp_reader *, const uchar *, size_t); |
| 64 | static unsigned int interpret_int_suffix (cpp_reader *, const uchar *, size_t); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 65 | static void check_promotion (cpp_reader *, const struct op *); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 66 | |
Edward Smith-Rowland | a15f7cb | 2014-10-01 11:49:23 +0000 | [diff] [blame] | 67 | static cpp_num parse_has_include (cpp_reader *, enum include_type); |
| 68 | |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 69 | /* Token type abuse to create unary plus and minus operators. */ |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 70 | #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1)) |
| 71 | #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2)) |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 72 | |
Zack Weinberg | 15dad1d | 2000-05-18 15:55:46 +0000 | [diff] [blame] | 73 | /* With -O2, gcc appears to produce nice code, moving the error |
| 74 | message load and subsequent jump completely out of the main path. */ |
Zack Weinberg | 15dad1d | 2000-05-18 15:55:46 +0000 | [diff] [blame] | 75 | #define SYNTAX_ERROR(msgid) \ |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 76 | do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0) |
Zack Weinberg | 15dad1d | 2000-05-18 15:55:46 +0000 | [diff] [blame] | 77 | #define SYNTAX_ERROR2(msgid, arg) \ |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 78 | do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \ |
| 79 | while(0) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 80 | #define SYNTAX_ERROR_AT(loc, msgid) \ |
| 81 | do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \ |
| 82 | while(0) |
| 83 | #define SYNTAX_ERROR2_AT(loc, msgid, arg) \ |
| 84 | do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \ |
| 85 | while(0) |
Zack Weinberg | 15dad1d | 2000-05-18 15:55:46 +0000 | [diff] [blame] | 86 | |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 87 | /* Subroutine of cpp_classify_number. S points to a float suffix of |
| 88 | length LEN, possibly zero. Returns 0 for an invalid suffix, or a |
| 89 | flag vector describing the suffix. */ |
| 90 | static unsigned int |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 91 | interpret_float_suffix (cpp_reader *pfile, const uchar *s, size_t len) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 92 | { |
Janis Johnson | eec4911 | 2009-04-01 17:04:42 +0000 | [diff] [blame] | 93 | size_t flags; |
Janis Johnson | 839a3b8 | 2009-04-01 17:31:26 +0000 | [diff] [blame] | 94 | size_t f, d, l, w, q, i; |
Uros Bizjak | c77cd3d | 2007-07-03 07:53:58 +0200 | [diff] [blame] | 95 | |
Janis Johnson | eec4911 | 2009-04-01 17:04:42 +0000 | [diff] [blame] | 96 | flags = 0; |
Janis Johnson | 839a3b8 | 2009-04-01 17:31:26 +0000 | [diff] [blame] | 97 | f = d = l = w = q = i = 0; |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 98 | |
Janis Johnson | eec4911 | 2009-04-01 17:04:42 +0000 | [diff] [blame] | 99 | /* Process decimal float suffixes, which are two letters starting |
| 100 | with d or D. Order and case are significant. */ |
| 101 | if (len == 2 && (*s == 'd' || *s == 'D')) |
| 102 | { |
| 103 | bool uppercase = (*s == 'D'); |
| 104 | switch (s[1]) |
| 105 | { |
| 106 | case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break; |
| 107 | case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break; |
| 108 | case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break; |
| 109 | case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break; |
| 110 | case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break; |
| 111 | case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break; |
| 112 | default: |
Janis Johnson | 839a3b8 | 2009-04-01 17:31:26 +0000 | [diff] [blame] | 113 | /* Additional two-character suffixes beginning with D are not |
| 114 | for decimal float constants. */ |
| 115 | break; |
Janis Johnson | eec4911 | 2009-04-01 17:04:42 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 119 | if (CPP_OPTION (pfile, ext_numeric_literals)) |
Janis Johnson | eec4911 | 2009-04-01 17:04:42 +0000 | [diff] [blame] | 120 | { |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 121 | /* Recognize a fixed-point suffix. */ |
| 122 | if (len != 0) |
| 123 | switch (s[len-1]) |
| 124 | { |
| 125 | case 'k': case 'K': flags = CPP_N_ACCUM; break; |
| 126 | case 'r': case 'R': flags = CPP_N_FRACT; break; |
| 127 | default: break; |
| 128 | } |
Janis Johnson | eec4911 | 2009-04-01 17:04:42 +0000 | [diff] [blame] | 129 | |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 130 | /* Continue processing a fixed-point suffix. The suffix is case |
| 131 | insensitive except for ll or LL. Order is significant. */ |
| 132 | if (flags) |
Janis Johnson | eec4911 | 2009-04-01 17:04:42 +0000 | [diff] [blame] | 133 | { |
Janis Johnson | eec4911 | 2009-04-01 17:04:42 +0000 | [diff] [blame] | 134 | if (len == 1) |
| 135 | return flags; |
| 136 | len--; |
Janis Johnson | eec4911 | 2009-04-01 17:04:42 +0000 | [diff] [blame] | 137 | |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 138 | if (*s == 'u' || *s == 'U') |
| 139 | { |
| 140 | flags |= CPP_N_UNSIGNED; |
| 141 | if (len == 1) |
| 142 | return flags; |
| 143 | len--; |
| 144 | s++; |
| 145 | } |
| 146 | |
| 147 | switch (*s) |
| 148 | { |
| 149 | case 'h': case 'H': |
| 150 | if (len == 1) |
| 151 | return flags |= CPP_N_SMALL; |
| 152 | break; |
| 153 | case 'l': |
| 154 | if (len == 1) |
| 155 | return flags |= CPP_N_MEDIUM; |
| 156 | if (len == 2 && s[1] == 'l') |
| 157 | return flags |= CPP_N_LARGE; |
| 158 | break; |
| 159 | case 'L': |
| 160 | if (len == 1) |
| 161 | return flags |= CPP_N_MEDIUM; |
| 162 | if (len == 2 && s[1] == 'L') |
| 163 | return flags |= CPP_N_LARGE; |
| 164 | break; |
| 165 | default: |
| 166 | break; |
| 167 | } |
| 168 | /* Anything left at this point is invalid. */ |
| 169 | return 0; |
| 170 | } |
Janis Johnson | eec4911 | 2009-04-01 17:04:42 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* In any remaining valid suffix, the case and order don't matter. */ |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 174 | while (len--) |
| 175 | switch (s[len]) |
| 176 | { |
Janis Johnson | eec4911 | 2009-04-01 17:04:42 +0000 | [diff] [blame] | 177 | case 'f': case 'F': f++; break; |
Janis Johnson | 839a3b8 | 2009-04-01 17:31:26 +0000 | [diff] [blame] | 178 | case 'd': case 'D': d++; break; |
Janis Johnson | eec4911 | 2009-04-01 17:04:42 +0000 | [diff] [blame] | 179 | case 'l': case 'L': l++; break; |
| 180 | case 'w': case 'W': w++; break; |
| 181 | case 'q': case 'Q': q++; break; |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 182 | case 'i': case 'I': |
| 183 | case 'j': case 'J': i++; break; |
| 184 | default: |
| 185 | return 0; |
| 186 | } |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 187 | |
Janis Johnson | 839a3b8 | 2009-04-01 17:31:26 +0000 | [diff] [blame] | 188 | if (f + d + l + w + q > 1 || i > 1) |
Jon Grimm | ad6ed77 | 2005-12-06 23:13:15 +0000 | [diff] [blame] | 189 | return 0; |
| 190 | |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 191 | if (i && !CPP_OPTION (pfile, ext_numeric_literals)) |
| 192 | return 0; |
| 193 | |
| 194 | if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals)) |
| 195 | return 0; |
| 196 | |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 197 | return ((i ? CPP_N_IMAGINARY : 0) |
| 198 | | (f ? CPP_N_SMALL : |
Janis Johnson | 839a3b8 | 2009-04-01 17:31:26 +0000 | [diff] [blame] | 199 | d ? CPP_N_MEDIUM : |
Uros Bizjak | c77cd3d | 2007-07-03 07:53:58 +0200 | [diff] [blame] | 200 | l ? CPP_N_LARGE : |
| 201 | w ? CPP_N_MD_W : |
Janis Johnson | 839a3b8 | 2009-04-01 17:31:26 +0000 | [diff] [blame] | 202 | q ? CPP_N_MD_Q : CPP_N_DEFAULT)); |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 205 | /* Return the classification flags for a float suffix. */ |
| 206 | unsigned int |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 207 | cpp_interpret_float_suffix (cpp_reader *pfile, const char *s, size_t len) |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 208 | { |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 209 | return interpret_float_suffix (pfile, (const unsigned char *)s, len); |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 212 | /* Subroutine of cpp_classify_number. S points to an integer suffix |
| 213 | of length LEN, possibly zero. Returns 0 for an invalid suffix, or a |
| 214 | flag vector describing the suffix. */ |
| 215 | static unsigned int |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 216 | interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len) |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 217 | { |
| 218 | size_t u, l, i; |
| 219 | |
| 220 | u = l = i = 0; |
| 221 | |
| 222 | while (len--) |
| 223 | switch (s[len]) |
| 224 | { |
| 225 | case 'u': case 'U': u++; break; |
| 226 | case 'i': case 'I': |
| 227 | case 'j': case 'J': i++; break; |
| 228 | case 'l': case 'L': l++; |
| 229 | /* If there are two Ls, they must be adjacent and the same case. */ |
| 230 | if (l == 2 && s[len] != s[len + 1]) |
| 231 | return 0; |
| 232 | break; |
| 233 | default: |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | if (l > 2 || u > 1 || i > 1) |
| 238 | return 0; |
| 239 | |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 240 | if (i && !CPP_OPTION (pfile, ext_numeric_literals)) |
| 241 | return 0; |
| 242 | |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 243 | return ((i ? CPP_N_IMAGINARY : 0) |
| 244 | | (u ? CPP_N_UNSIGNED : 0) |
| 245 | | ((l == 0) ? CPP_N_SMALL |
| 246 | : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE)); |
| 247 | } |
| 248 | |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 249 | /* Return the classification flags for an int suffix. */ |
| 250 | unsigned int |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 251 | cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len) |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 252 | { |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 253 | return interpret_int_suffix (pfile, (const unsigned char *)s, len); |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* Return the string type corresponding to the the input user-defined string |
| 257 | literal type. If the input type is not a user-defined string literal |
| 258 | type return the input type. */ |
| 259 | enum cpp_ttype |
| 260 | cpp_userdef_string_remove_type (enum cpp_ttype type) |
| 261 | { |
| 262 | if (type == CPP_STRING_USERDEF) |
| 263 | return CPP_STRING; |
| 264 | else if (type == CPP_WSTRING_USERDEF) |
| 265 | return CPP_WSTRING; |
| 266 | else if (type == CPP_STRING16_USERDEF) |
| 267 | return CPP_STRING16; |
| 268 | else if (type == CPP_STRING32_USERDEF) |
| 269 | return CPP_STRING32; |
| 270 | else if (type == CPP_UTF8STRING_USERDEF) |
| 271 | return CPP_UTF8STRING; |
| 272 | else |
| 273 | return type; |
| 274 | } |
| 275 | |
| 276 | /* Return the user-defined string literal type corresponding to the input |
| 277 | string type. If the input type is not a string type return the input |
| 278 | type. */ |
| 279 | enum cpp_ttype |
| 280 | cpp_userdef_string_add_type (enum cpp_ttype type) |
| 281 | { |
| 282 | if (type == CPP_STRING) |
| 283 | return CPP_STRING_USERDEF; |
| 284 | else if (type == CPP_WSTRING) |
| 285 | return CPP_WSTRING_USERDEF; |
| 286 | else if (type == CPP_STRING16) |
| 287 | return CPP_STRING16_USERDEF; |
| 288 | else if (type == CPP_STRING32) |
| 289 | return CPP_STRING32_USERDEF; |
| 290 | else if (type == CPP_UTF8STRING) |
| 291 | return CPP_UTF8STRING_USERDEF; |
| 292 | else |
| 293 | return type; |
| 294 | } |
| 295 | |
| 296 | /* Return the char type corresponding to the the input user-defined char |
| 297 | literal type. If the input type is not a user-defined char literal |
| 298 | type return the input type. */ |
| 299 | enum cpp_ttype |
| 300 | cpp_userdef_char_remove_type (enum cpp_ttype type) |
| 301 | { |
| 302 | if (type == CPP_CHAR_USERDEF) |
| 303 | return CPP_CHAR; |
| 304 | else if (type == CPP_WCHAR_USERDEF) |
| 305 | return CPP_WCHAR; |
| 306 | else if (type == CPP_CHAR16_USERDEF) |
Ed Smith-Rowland | 7e74ce3 | 2011-11-21 19:27:30 +0000 | [diff] [blame] | 307 | return CPP_CHAR16; |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 308 | else if (type == CPP_CHAR32_USERDEF) |
Ed Smith-Rowland | 7e74ce3 | 2011-11-21 19:27:30 +0000 | [diff] [blame] | 309 | return CPP_CHAR32; |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 310 | else |
| 311 | return type; |
| 312 | } |
| 313 | |
| 314 | /* Return the user-defined char literal type corresponding to the input |
| 315 | char type. If the input type is not a char type return the input |
| 316 | type. */ |
| 317 | enum cpp_ttype |
| 318 | cpp_userdef_char_add_type (enum cpp_ttype type) |
| 319 | { |
| 320 | if (type == CPP_CHAR) |
| 321 | return CPP_CHAR_USERDEF; |
| 322 | else if (type == CPP_WCHAR) |
| 323 | return CPP_WCHAR_USERDEF; |
| 324 | else if (type == CPP_CHAR16) |
| 325 | return CPP_CHAR16_USERDEF; |
| 326 | else if (type == CPP_CHAR32) |
| 327 | return CPP_CHAR32_USERDEF; |
| 328 | else |
| 329 | return type; |
| 330 | } |
| 331 | |
| 332 | /* Return true if the token type is a user-defined string literal. */ |
| 333 | bool |
| 334 | cpp_userdef_string_p (enum cpp_ttype type) |
| 335 | { |
| 336 | if (type == CPP_STRING_USERDEF |
| 337 | || type == CPP_WSTRING_USERDEF |
| 338 | || type == CPP_STRING16_USERDEF |
| 339 | || type == CPP_STRING32_USERDEF |
| 340 | || type == CPP_UTF8STRING_USERDEF) |
| 341 | return true; |
| 342 | else |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | /* Return true if the token type is a user-defined char literal. */ |
| 347 | bool |
| 348 | cpp_userdef_char_p (enum cpp_ttype type) |
| 349 | { |
| 350 | if (type == CPP_CHAR_USERDEF |
| 351 | || type == CPP_WCHAR_USERDEF |
| 352 | || type == CPP_CHAR16_USERDEF |
| 353 | || type == CPP_CHAR32_USERDEF) |
| 354 | return true; |
| 355 | else |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | /* Extract the suffix from a user-defined literal string or char. */ |
| 360 | const char * |
| 361 | cpp_get_userdef_suffix (const cpp_token *tok) |
| 362 | { |
| 363 | unsigned int len = tok->val.str.len; |
| 364 | const char *text = (const char *)tok->val.str.text; |
| 365 | char delim; |
| 366 | unsigned int i; |
| 367 | for (i = 0; i < len; ++i) |
| 368 | if (text[i] == '\'' || text[i] == '"') |
| 369 | break; |
| 370 | if (i == len) |
| 371 | return text + len; |
| 372 | delim = text[i]; |
| 373 | for (i = len; i > 0; --i) |
| 374 | if (text[i - 1] == delim) |
| 375 | break; |
| 376 | return text + i; |
| 377 | } |
| 378 | |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 379 | /* Categorize numeric constants according to their field (integer, |
| 380 | floating point, or invalid), radix (decimal, octal, hexadecimal), |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 381 | and type suffixes. |
| 382 | |
| 383 | TOKEN is the token that represents the numeric constant to |
| 384 | classify. |
| 385 | |
| 386 | In C++0X if UD_SUFFIX is non null it will be assigned |
| 387 | any unrecognized suffix for a user-defined literal. |
| 388 | |
| 389 | VIRTUAL_LOCATION is the virtual location for TOKEN. */ |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 390 | unsigned int |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 391 | cpp_classify_number (cpp_reader *pfile, const cpp_token *token, |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 392 | const char **ud_suffix, source_location virtual_location) |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 393 | { |
| 394 | const uchar *str = token->val.str.text; |
| 395 | const uchar *limit; |
| 396 | unsigned int max_digit, result, radix; |
| 397 | enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag; |
Joseph Myers | dadab4f | 2010-01-01 18:08:17 +0000 | [diff] [blame] | 398 | bool seen_digit; |
Edward Smith-Rowland | 7057e64 | 2013-10-31 14:01:23 +0000 | [diff] [blame] | 399 | bool seen_digit_sep; |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 400 | |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 401 | if (ud_suffix) |
| 402 | *ud_suffix = NULL; |
| 403 | |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 404 | /* If the lexer has done its job, length one can only be a single |
| 405 | digit. Fast-path this very common case. */ |
| 406 | if (token->val.str.len == 1) |
| 407 | return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL; |
| 408 | |
| 409 | limit = str + token->val.str.len; |
| 410 | float_flag = NOT_FLOAT; |
| 411 | max_digit = 0; |
| 412 | radix = 10; |
Joseph Myers | dadab4f | 2010-01-01 18:08:17 +0000 | [diff] [blame] | 413 | seen_digit = false; |
Edward Smith-Rowland | 7057e64 | 2013-10-31 14:01:23 +0000 | [diff] [blame] | 414 | seen_digit_sep = false; |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 415 | |
| 416 | /* First, interpret the radix. */ |
| 417 | if (*str == '0') |
| 418 | { |
| 419 | radix = 8; |
| 420 | str++; |
| 421 | |
| 422 | /* Require at least one hex digit to classify it as hex. */ |
Edward Smith-Rowland | 7057e64 | 2013-10-31 14:01:23 +0000 | [diff] [blame] | 423 | if (*str == 'x' || *str == 'X') |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 424 | { |
Edward Smith-Rowland | 7057e64 | 2013-10-31 14:01:23 +0000 | [diff] [blame] | 425 | if (str[1] == '.' || ISXDIGIT (str[1])) |
| 426 | { |
| 427 | radix = 16; |
| 428 | str++; |
| 429 | } |
| 430 | else if (DIGIT_SEP (str[1])) |
| 431 | SYNTAX_ERROR_AT (virtual_location, |
| 432 | "digit separator after base indicator"); |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 433 | } |
Edward Smith-Rowland | 7057e64 | 2013-10-31 14:01:23 +0000 | [diff] [blame] | 434 | else if (*str == 'b' || *str == 'B') |
Joerg Wunsch | f7fd775 | 2007-06-05 22:25:27 +0000 | [diff] [blame] | 435 | { |
Edward Smith-Rowland | 7057e64 | 2013-10-31 14:01:23 +0000 | [diff] [blame] | 436 | if (str[1] == '0' || str[1] == '1') |
| 437 | { |
| 438 | radix = 2; |
| 439 | str++; |
| 440 | } |
| 441 | else if (DIGIT_SEP (str[1])) |
| 442 | SYNTAX_ERROR_AT (virtual_location, |
| 443 | "digit separator after base indicator"); |
Joerg Wunsch | f7fd775 | 2007-06-05 22:25:27 +0000 | [diff] [blame] | 444 | } |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* Now scan for a well-formed integer or float. */ |
| 448 | for (;;) |
| 449 | { |
| 450 | unsigned int c = *str++; |
| 451 | |
| 452 | if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16)) |
| 453 | { |
Edward Smith-Rowland | 7057e64 | 2013-10-31 14:01:23 +0000 | [diff] [blame] | 454 | seen_digit_sep = false; |
Joseph Myers | dadab4f | 2010-01-01 18:08:17 +0000 | [diff] [blame] | 455 | seen_digit = true; |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 456 | c = hex_value (c); |
| 457 | if (c > max_digit) |
| 458 | max_digit = c; |
| 459 | } |
Edward Smith-Rowland | 7057e64 | 2013-10-31 14:01:23 +0000 | [diff] [blame] | 460 | else if (DIGIT_SEP (c)) |
| 461 | { |
| 462 | if (seen_digit_sep) |
| 463 | SYNTAX_ERROR_AT (virtual_location, "adjacent digit separators"); |
| 464 | seen_digit_sep = true; |
| 465 | } |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 466 | else if (c == '.') |
| 467 | { |
Edward Smith-Rowland | 7057e64 | 2013-10-31 14:01:23 +0000 | [diff] [blame] | 468 | if (seen_digit_sep || DIGIT_SEP (*str)) |
| 469 | SYNTAX_ERROR_AT (virtual_location, |
| 470 | "digit separator adjacent to decimal point"); |
| 471 | seen_digit_sep = false; |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 472 | if (float_flag == NOT_FLOAT) |
| 473 | float_flag = AFTER_POINT; |
| 474 | else |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 475 | SYNTAX_ERROR_AT (virtual_location, |
| 476 | "too many decimal points in number"); |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 477 | } |
| 478 | else if ((radix <= 10 && (c == 'e' || c == 'E')) |
| 479 | || (radix == 16 && (c == 'p' || c == 'P'))) |
| 480 | { |
Edward Smith-Rowland | 7057e64 | 2013-10-31 14:01:23 +0000 | [diff] [blame] | 481 | if (seen_digit_sep || DIGIT_SEP (*str)) |
| 482 | SYNTAX_ERROR_AT (virtual_location, |
| 483 | "digit separator adjacent to exponent"); |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 484 | float_flag = AFTER_EXPON; |
| 485 | break; |
| 486 | } |
| 487 | else |
| 488 | { |
| 489 | /* Start of suffix. */ |
| 490 | str--; |
| 491 | break; |
| 492 | } |
| 493 | } |
| 494 | |
Edward Smith-Rowland | 7057e64 | 2013-10-31 14:01:23 +0000 | [diff] [blame] | 495 | if (seen_digit_sep && float_flag != AFTER_EXPON) |
| 496 | SYNTAX_ERROR_AT (virtual_location, |
| 497 | "digit separator outside digit sequence"); |
| 498 | |
Chao-ying Fu | ac6b1c6 | 2007-08-30 23:05:17 +0000 | [diff] [blame] | 499 | /* The suffix may be for decimal fixed-point constants without exponent. */ |
| 500 | if (radix != 16 && float_flag == NOT_FLOAT) |
| 501 | { |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 502 | result = interpret_float_suffix (pfile, str, limit - str); |
Chao-ying Fu | ac6b1c6 | 2007-08-30 23:05:17 +0000 | [diff] [blame] | 503 | if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM)) |
| 504 | { |
| 505 | result |= CPP_N_FLOATING; |
| 506 | /* We need to restore the radix to 10, if the radix is 8. */ |
| 507 | if (radix == 8) |
| 508 | radix = 10; |
| 509 | |
| 510 | if (CPP_PEDANTIC (pfile)) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 511 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, |
| 512 | "fixed-point constants are a GCC extension"); |
Chao-ying Fu | ac6b1c6 | 2007-08-30 23:05:17 +0000 | [diff] [blame] | 513 | goto syntax_ok; |
| 514 | } |
| 515 | else |
| 516 | result = 0; |
| 517 | } |
| 518 | |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 519 | if (float_flag != NOT_FLOAT && radix == 8) |
| 520 | radix = 10; |
| 521 | |
| 522 | if (max_digit >= radix) |
Joerg Wunsch | f7fd775 | 2007-06-05 22:25:27 +0000 | [diff] [blame] | 523 | { |
| 524 | if (radix == 2) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 525 | SYNTAX_ERROR2_AT (virtual_location, |
| 526 | "invalid digit \"%c\" in binary constant", '0' + max_digit); |
Joerg Wunsch | f7fd775 | 2007-06-05 22:25:27 +0000 | [diff] [blame] | 527 | else |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 528 | SYNTAX_ERROR2_AT (virtual_location, |
| 529 | "invalid digit \"%c\" in octal constant", '0' + max_digit); |
Joerg Wunsch | f7fd775 | 2007-06-05 22:25:27 +0000 | [diff] [blame] | 530 | } |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 531 | |
| 532 | if (float_flag != NOT_FLOAT) |
| 533 | { |
Joerg Wunsch | f7fd775 | 2007-06-05 22:25:27 +0000 | [diff] [blame] | 534 | if (radix == 2) |
| 535 | { |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 536 | cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, |
| 537 | "invalid prefix \"0b\" for floating constant"); |
Joerg Wunsch | f7fd775 | 2007-06-05 22:25:27 +0000 | [diff] [blame] | 538 | return CPP_N_INVALID; |
| 539 | } |
| 540 | |
Joseph Myers | dadab4f | 2010-01-01 18:08:17 +0000 | [diff] [blame] | 541 | if (radix == 16 && !seen_digit) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 542 | SYNTAX_ERROR_AT (virtual_location, |
| 543 | "no digits in hexadecimal floating constant"); |
Joseph Myers | dadab4f | 2010-01-01 18:08:17 +0000 | [diff] [blame] | 544 | |
Edward Smith-Rowland | 7c05e50 | 2014-08-28 02:38:24 +0000 | [diff] [blame] | 545 | if (radix == 16 && CPP_PEDANTIC (pfile) |
| 546 | && !CPP_OPTION (pfile, extended_numbers)) |
| 547 | { |
| 548 | if (CPP_OPTION (pfile, cplusplus)) |
| 549 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, |
| 550 | "use of C++11 hexadecimal floating constant"); |
| 551 | else |
| 552 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, |
| 553 | "use of C99 hexadecimal floating constant"); |
| 554 | } |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 555 | |
| 556 | if (float_flag == AFTER_EXPON) |
| 557 | { |
| 558 | if (*str == '+' || *str == '-') |
| 559 | str++; |
| 560 | |
| 561 | /* Exponent is decimal, even if string is a hex float. */ |
| 562 | if (!ISDIGIT (*str)) |
Edward Smith-Rowland | 7057e64 | 2013-10-31 14:01:23 +0000 | [diff] [blame] | 563 | { |
| 564 | if (DIGIT_SEP (*str)) |
| 565 | SYNTAX_ERROR_AT (virtual_location, |
| 566 | "digit separator adjacent to exponent"); |
| 567 | else |
| 568 | SYNTAX_ERROR_AT (virtual_location, "exponent has no digits"); |
| 569 | } |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 570 | do |
Edward Smith-Rowland | 7057e64 | 2013-10-31 14:01:23 +0000 | [diff] [blame] | 571 | { |
| 572 | seen_digit_sep = DIGIT_SEP (*str); |
| 573 | str++; |
| 574 | } |
| 575 | while (ISDIGIT (*str) || DIGIT_SEP (*str)); |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 576 | } |
| 577 | else if (radix == 16) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 578 | SYNTAX_ERROR_AT (virtual_location, |
| 579 | "hexadecimal floating constants require an exponent"); |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 580 | |
Edward Smith-Rowland | 7057e64 | 2013-10-31 14:01:23 +0000 | [diff] [blame] | 581 | if (seen_digit_sep) |
| 582 | SYNTAX_ERROR_AT (virtual_location, |
| 583 | "digit separator outside digit sequence"); |
| 584 | |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 585 | result = interpret_float_suffix (pfile, str, limit - str); |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 586 | if (result == 0) |
| 587 | { |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 588 | if (CPP_OPTION (pfile, user_literals)) |
| 589 | { |
| 590 | if (ud_suffix) |
| 591 | *ud_suffix = (const char *) str; |
| 592 | result = CPP_N_LARGE | CPP_N_USERDEF; |
| 593 | } |
| 594 | else |
| 595 | { |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 596 | cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, |
| 597 | "invalid suffix \"%.*s\" on floating constant", |
| 598 | (int) (limit - str), str); |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 599 | return CPP_N_INVALID; |
| 600 | } |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* Traditional C didn't accept any floating suffixes. */ |
| 604 | if (limit != str |
| 605 | && CPP_WTRADITIONAL (pfile) |
| 606 | && ! cpp_sys_macro_p (pfile)) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 607 | cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0, |
| 608 | "traditional C rejects the \"%.*s\" suffix", |
| 609 | (int) (limit - str), str); |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 610 | |
Janis Johnson | 839a3b8 | 2009-04-01 17:31:26 +0000 | [diff] [blame] | 611 | /* A suffix for double is a GCC extension via decimal float support. |
| 612 | If the suffix also specifies an imaginary value we'll catch that |
| 613 | later. */ |
| 614 | if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile)) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 615 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, |
| 616 | "suffix for double constant is a GCC extension"); |
Janis Johnson | 839a3b8 | 2009-04-01 17:31:26 +0000 | [diff] [blame] | 617 | |
Jon Grimm | ad6ed77 | 2005-12-06 23:13:15 +0000 | [diff] [blame] | 618 | /* Radix must be 10 for decimal floats. */ |
| 619 | if ((result & CPP_N_DFLOAT) && radix != 10) |
| 620 | { |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 621 | cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, |
| 622 | "invalid suffix \"%.*s\" with hexadecimal floating constant", |
| 623 | (int) (limit - str), str); |
Jon Grimm | ad6ed77 | 2005-12-06 23:13:15 +0000 | [diff] [blame] | 624 | return CPP_N_INVALID; |
| 625 | } |
| 626 | |
Chao-ying Fu | ac6b1c6 | 2007-08-30 23:05:17 +0000 | [diff] [blame] | 627 | if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile)) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 628 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, |
| 629 | "fixed-point constants are a GCC extension"); |
Chao-ying Fu | ac6b1c6 | 2007-08-30 23:05:17 +0000 | [diff] [blame] | 630 | |
Janis Johnson | 5a6bb57 | 2007-05-14 23:45:40 +0000 | [diff] [blame] | 631 | if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile)) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 632 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, |
| 633 | "decimal float constants are a GCC extension"); |
Janis Johnson | 5a6bb57 | 2007-05-14 23:45:40 +0000 | [diff] [blame] | 634 | |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 635 | result |= CPP_N_FLOATING; |
| 636 | } |
| 637 | else |
| 638 | { |
Ed Smith-Rowland | a4a0016 | 2012-11-10 00:08:49 +0000 | [diff] [blame] | 639 | result = interpret_int_suffix (pfile, str, limit - str); |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 640 | if (result == 0) |
| 641 | { |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 642 | if (CPP_OPTION (pfile, user_literals)) |
| 643 | { |
| 644 | if (ud_suffix) |
| 645 | *ud_suffix = (const char *) str; |
| 646 | result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF; |
| 647 | } |
| 648 | else |
| 649 | { |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 650 | cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, |
| 651 | "invalid suffix \"%.*s\" on integer constant", |
| 652 | (int) (limit - str), str); |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 653 | return CPP_N_INVALID; |
| 654 | } |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Zack Weinberg | 56da720 | 2002-08-02 04:18:16 +0000 | [diff] [blame] | 657 | /* Traditional C only accepted the 'L' suffix. |
| 658 | Suppress warning about 'LL' with -Wno-long-long. */ |
| 659 | if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile)) |
| 660 | { |
| 661 | int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY)); |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 662 | int large = (result & CPP_N_WIDTH) == CPP_N_LARGE |
Joseph Myers | e3339d0 | 2010-09-29 15:49:14 +0100 | [diff] [blame] | 663 | && CPP_OPTION (pfile, cpp_warn_long_long); |
Zack Weinberg | 56da720 | 2002-08-02 04:18:16 +0000 | [diff] [blame] | 664 | |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 665 | if (u_or_i || large) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 666 | cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL, |
| 667 | virtual_location, 0, |
| 668 | "traditional C rejects the \"%.*s\" suffix", |
| 669 | (int) (limit - str), str); |
Zack Weinberg | 56da720 | 2002-08-02 04:18:16 +0000 | [diff] [blame] | 670 | } |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 671 | |
| 672 | if ((result & CPP_N_WIDTH) == CPP_N_LARGE |
Joseph Myers | e3339d0 | 2010-09-29 15:49:14 +0100 | [diff] [blame] | 673 | && CPP_OPTION (pfile, cpp_warn_long_long)) |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 674 | { |
| 675 | const char *message = CPP_OPTION (pfile, cplusplus) |
Jakub Jelinek | 01187df | 2013-04-28 23:36:57 +0200 | [diff] [blame] | 676 | ? N_("use of C++11 long long integer constant") |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 677 | : N_("use of C99 long long integer constant"); |
| 678 | |
| 679 | if (CPP_OPTION (pfile, c99)) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 680 | cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location, |
| 681 | 0, message); |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 682 | else |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 683 | cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG, |
| 684 | virtual_location, 0, message); |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 685 | } |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 686 | |
| 687 | result |= CPP_N_INTEGER; |
| 688 | } |
| 689 | |
Chao-ying Fu | ac6b1c6 | 2007-08-30 23:05:17 +0000 | [diff] [blame] | 690 | syntax_ok: |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 691 | if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile)) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 692 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, |
| 693 | "imaginary constants are a GCC extension"); |
Jakub Jelinek | 01187df | 2013-04-28 23:36:57 +0200 | [diff] [blame] | 694 | if (radix == 2 |
| 695 | && !CPP_OPTION (pfile, binary_constants) |
| 696 | && CPP_PEDANTIC (pfile)) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 697 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, |
Jakub Jelinek | 01187df | 2013-04-28 23:36:57 +0200 | [diff] [blame] | 698 | CPP_OPTION (pfile, cplusplus) |
Edward Smith-Rowland | e4276ba | 2014-08-23 16:50:22 +0000 | [diff] [blame] | 699 | ? "binary constants are a C++14 feature " |
Jakub Jelinek | 01187df | 2013-04-28 23:36:57 +0200 | [diff] [blame] | 700 | "or GCC extension" |
| 701 | : "binary constants are a GCC extension"); |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 702 | |
| 703 | if (radix == 10) |
| 704 | result |= CPP_N_DECIMAL; |
| 705 | else if (radix == 16) |
| 706 | result |= CPP_N_HEX; |
Joerg Wunsch | f7fd775 | 2007-06-05 22:25:27 +0000 | [diff] [blame] | 707 | else if (radix == 2) |
| 708 | result |= CPP_N_BINARY; |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 709 | else |
| 710 | result |= CPP_N_OCTAL; |
| 711 | |
| 712 | return result; |
| 713 | |
| 714 | syntax_error: |
| 715 | return CPP_N_INVALID; |
| 716 | } |
| 717 | |
| 718 | /* cpp_interpret_integer converts an integer constant into a cpp_num, |
| 719 | of precision options->precision. |
| 720 | |
| 721 | We do not provide any interface for decimal->float conversion, |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 722 | because the preprocessor doesn't need it and we don't want to |
| 723 | drag in GCC's floating point emulator. */ |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 724 | cpp_num |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 725 | cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token, |
| 726 | unsigned int type) |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 727 | { |
| 728 | const uchar *p, *end; |
| 729 | cpp_num result; |
| 730 | |
| 731 | result.low = 0; |
| 732 | result.high = 0; |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 733 | result.unsignedp = !!(type & CPP_N_UNSIGNED); |
| 734 | result.overflow = false; |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 735 | |
| 736 | p = token->val.str.text; |
| 737 | end = p + token->val.str.len; |
| 738 | |
| 739 | /* Common case of a single digit. */ |
| 740 | if (token->val.str.len == 1) |
| 741 | result.low = p[0] - '0'; |
| 742 | else |
| 743 | { |
| 744 | cpp_num_part max; |
| 745 | size_t precision = CPP_OPTION (pfile, precision); |
| 746 | unsigned int base = 10, c = 0; |
| 747 | bool overflow = false; |
| 748 | |
| 749 | if ((type & CPP_N_RADIX) == CPP_N_OCTAL) |
| 750 | { |
| 751 | base = 8; |
| 752 | p++; |
| 753 | } |
| 754 | else if ((type & CPP_N_RADIX) == CPP_N_HEX) |
| 755 | { |
| 756 | base = 16; |
| 757 | p += 2; |
| 758 | } |
Joerg Wunsch | f7fd775 | 2007-06-05 22:25:27 +0000 | [diff] [blame] | 759 | else if ((type & CPP_N_RADIX) == CPP_N_BINARY) |
| 760 | { |
| 761 | base = 2; |
| 762 | p += 2; |
| 763 | } |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 764 | |
| 765 | /* We can add a digit to numbers strictly less than this without |
| 766 | needing the precision and slowness of double integers. */ |
| 767 | max = ~(cpp_num_part) 0; |
| 768 | if (precision < PART_PRECISION) |
| 769 | max >>= PART_PRECISION - precision; |
| 770 | max = (max - base + 1) / base + 1; |
| 771 | |
| 772 | for (; p < end; p++) |
| 773 | { |
| 774 | c = *p; |
| 775 | |
| 776 | if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c))) |
| 777 | c = hex_value (c); |
Edward Smith-Rowland | 7057e64 | 2013-10-31 14:01:23 +0000 | [diff] [blame] | 778 | else if (DIGIT_SEP (c)) |
| 779 | continue; |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 780 | else |
| 781 | break; |
| 782 | |
| 783 | /* Strict inequality for when max is set to zero. */ |
| 784 | if (result.low < max) |
| 785 | result.low = result.low * base + c; |
| 786 | else |
| 787 | { |
| 788 | result = append_digit (result, c, base, precision); |
| 789 | overflow |= result.overflow; |
| 790 | max = 0; |
| 791 | } |
| 792 | } |
| 793 | |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 794 | if (overflow && !(type & CPP_N_USERDEF)) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 795 | cpp_error (pfile, CPP_DL_PEDWARN, |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 796 | "integer constant is too large for its type"); |
Neil Booth | 017acb4 | 2002-06-20 20:34:19 +0000 | [diff] [blame] | 797 | /* If too big to be signed, consider it unsigned. Only warn for |
| 798 | decimal numbers. Traditional numbers were always signed (but |
Kazu Hirata | 8d9afc4e | 2002-09-16 11:42:00 +0000 | [diff] [blame] | 799 | we still honor an explicit U suffix); but we only have |
Neil Booth | cd98faa | 2002-07-09 22:21:37 +0000 | [diff] [blame] | 800 | traditional semantics in directives. */ |
Neil Booth | 017acb4 | 2002-06-20 20:34:19 +0000 | [diff] [blame] | 801 | else if (!result.unsignedp |
Neil Booth | cd98faa | 2002-07-09 22:21:37 +0000 | [diff] [blame] | 802 | && !(CPP_OPTION (pfile, traditional) |
| 803 | && pfile->state.in_directive) |
Neil Booth | 017acb4 | 2002-06-20 20:34:19 +0000 | [diff] [blame] | 804 | && !num_positive (result, precision)) |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 805 | { |
Joseph Myers | f88d077 | 2009-04-25 19:46:03 +0100 | [diff] [blame] | 806 | /* This is for constants within the range of uintmax_t but |
Joseph Myers | 813b9e7 | 2009-04-25 19:59:20 +0100 | [diff] [blame] | 807 | not that of intmax_t. For such decimal constants, a |
Joseph Myers | f88d077 | 2009-04-25 19:46:03 +0100 | [diff] [blame] | 808 | diagnostic is required for C99 as the selected type must |
| 809 | be signed and not having a type is a constraint violation |
| 810 | (DR#298, TC3), so this must be a pedwarn. For C90, |
| 811 | unsigned long is specified to be used for a constant that |
| 812 | does not fit in signed long; if uintmax_t has the same |
| 813 | range as unsigned long this means only a warning is |
| 814 | appropriate here. C90 permits the preprocessor to use a |
| 815 | wider range than unsigned long in the compiler, so if |
| 816 | uintmax_t is wider than unsigned long no diagnostic is |
| 817 | required for such constants in preprocessor #if |
| 818 | expressions and the compiler will pedwarn for such |
| 819 | constants outside the range of unsigned long that reach |
| 820 | the compiler so a diagnostic is not required there |
| 821 | either; thus, pedwarn for C99 but use a plain warning for |
| 822 | C90. */ |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 823 | if (base == 10) |
Joseph Myers | f88d077 | 2009-04-25 19:46:03 +0100 | [diff] [blame] | 824 | cpp_error (pfile, (CPP_OPTION (pfile, c99) |
| 825 | ? CPP_DL_PEDWARN |
| 826 | : CPP_DL_WARNING), |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 827 | "integer constant is so large that it is unsigned"); |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 828 | result.unsignedp = true; |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
| 832 | return result; |
| 833 | } |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 834 | |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 835 | /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */ |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 836 | static cpp_num |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 837 | append_digit (cpp_num num, int digit, int base, size_t precision) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 838 | { |
| 839 | cpp_num result; |
Joerg Wunsch | f7fd775 | 2007-06-05 22:25:27 +0000 | [diff] [blame] | 840 | unsigned int shift; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 841 | bool overflow; |
| 842 | cpp_num_part add_high, add_low; |
| 843 | |
Joerg Wunsch | f7fd775 | 2007-06-05 22:25:27 +0000 | [diff] [blame] | 844 | /* Multiply by 2, 8 or 16. Catching this overflow here means we don't |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 845 | need to worry about add_high overflowing. */ |
Joerg Wunsch | f7fd775 | 2007-06-05 22:25:27 +0000 | [diff] [blame] | 846 | switch (base) |
| 847 | { |
| 848 | case 2: |
| 849 | shift = 1; |
| 850 | break; |
| 851 | |
| 852 | case 16: |
| 853 | shift = 4; |
| 854 | break; |
| 855 | |
| 856 | default: |
| 857 | shift = 3; |
| 858 | } |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 859 | overflow = !!(num.high >> (PART_PRECISION - shift)); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 860 | result.high = num.high << shift; |
| 861 | result.low = num.low << shift; |
| 862 | result.high |= num.low >> (PART_PRECISION - shift); |
Diego Novillo | 6de9cd9 | 2004-05-13 02:41:07 -0400 | [diff] [blame] | 863 | result.unsignedp = num.unsignedp; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 864 | |
| 865 | if (base == 10) |
| 866 | { |
| 867 | add_low = num.low << 1; |
| 868 | add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1)); |
| 869 | } |
| 870 | else |
| 871 | add_high = add_low = 0; |
| 872 | |
| 873 | if (add_low + digit < add_low) |
| 874 | add_high++; |
| 875 | add_low += digit; |
Eric Christopher | 22a8a52 | 2007-05-02 21:57:50 +0000 | [diff] [blame] | 876 | |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 877 | if (result.low + add_low < result.low) |
| 878 | add_high++; |
| 879 | if (result.high + add_high < result.high) |
| 880 | overflow = true; |
| 881 | |
| 882 | result.low += add_low; |
| 883 | result.high += add_high; |
Diego Novillo | 6de9cd9 | 2004-05-13 02:41:07 -0400 | [diff] [blame] | 884 | result.overflow = overflow; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 885 | |
| 886 | /* The above code catches overflow of a cpp_num type. This catches |
| 887 | overflow of the (possibly shorter) target precision. */ |
| 888 | num.low = result.low; |
| 889 | num.high = result.high; |
| 890 | result = num_trim (result, precision); |
| 891 | if (!num_eq (result, num)) |
Diego Novillo | 6de9cd9 | 2004-05-13 02:41:07 -0400 | [diff] [blame] | 892 | result.overflow = true; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 893 | |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 894 | return result; |
| 895 | } |
| 896 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 897 | /* Handle meeting "defined" in a preprocessor expression. */ |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 898 | static cpp_num |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 899 | parse_defined (cpp_reader *pfile) |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 900 | { |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 901 | cpp_num result; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 902 | int paren = 0; |
| 903 | cpp_hashnode *node = 0; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 904 | const cpp_token *token; |
Neil Booth | 63d7500 | 2001-11-05 22:26:13 +0000 | [diff] [blame] | 905 | cpp_context *initial_context = pfile->context; |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 906 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 907 | /* Don't expand macros. */ |
| 908 | pfile->state.prevent_expansion++; |
| 909 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 910 | token = cpp_get_token (pfile); |
| 911 | if (token->type == CPP_OPEN_PAREN) |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 912 | { |
| 913 | paren = 1; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 914 | token = cpp_get_token (pfile); |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 917 | if (token->type == CPP_NAME) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 918 | { |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 919 | node = token->val.node.node; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 920 | if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 921 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 922 | cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\""); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 923 | node = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 924 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 925 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 926 | else |
Neil Booth | 3c8465d | 2001-02-06 20:07:07 +0000 | [diff] [blame] | 927 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 928 | cpp_error (pfile, CPP_DL_ERROR, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 929 | "operator \"defined\" requires an identifier"); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 930 | if (token->flags & NAMED_OP) |
Neil Booth | 3c8465d | 2001-02-06 20:07:07 +0000 | [diff] [blame] | 931 | { |
| 932 | cpp_token op; |
| 933 | |
| 934 | op.flags = 0; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 935 | op.type = token->type; |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 936 | cpp_error (pfile, CPP_DL_ERROR, |
Neil Booth | 3c8465d | 2001-02-06 20:07:07 +0000 | [diff] [blame] | 937 | "(\"%s\" is an alternative token for \"%s\" in C++)", |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 938 | cpp_token_as_text (pfile, token), |
Neil Booth | 3c8465d | 2001-02-06 20:07:07 +0000 | [diff] [blame] | 939 | cpp_token_as_text (pfile, &op)); |
| 940 | } |
| 941 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 942 | |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 943 | if (node) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 944 | { |
Neil Booth | 335d03e | 2003-08-03 12:23:46 +0000 | [diff] [blame] | 945 | if (pfile->context != initial_context && CPP_PEDANTIC (pfile)) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 946 | cpp_error (pfile, CPP_DL_WARNING, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 947 | "this use of \"defined\" may not be portable"); |
Neil Booth | 63d7500 | 2001-11-05 22:26:13 +0000 | [diff] [blame] | 948 | |
Neil Booth | a69cbaa | 2002-07-23 22:57:49 +0000 | [diff] [blame] | 949 | _cpp_mark_macro_used (node); |
Joseph Myers | 93d45d9 | 2008-04-02 20:42:53 +0100 | [diff] [blame] | 950 | if (!(node->flags & NODE_USED)) |
| 951 | { |
| 952 | node->flags |= NODE_USED; |
| 953 | if (node->type == NT_MACRO) |
| 954 | { |
Jakub Jelinek | a69d252 | 2010-09-01 00:47:25 +0200 | [diff] [blame] | 955 | if ((node->flags & NODE_BUILTIN) |
| 956 | && pfile->cb.user_builtin_macro) |
| 957 | pfile->cb.user_builtin_macro (pfile, node); |
Joseph Myers | 93d45d9 | 2008-04-02 20:42:53 +0100 | [diff] [blame] | 958 | if (pfile->cb.used_define) |
| 959 | pfile->cb.used_define (pfile, pfile->directive_line, node); |
| 960 | } |
| 961 | else |
| 962 | { |
| 963 | if (pfile->cb.used_undef) |
| 964 | pfile->cb.used_undef (pfile, pfile->directive_line, node); |
| 965 | } |
| 966 | } |
Neil Booth | a69cbaa | 2002-07-23 22:57:49 +0000 | [diff] [blame] | 967 | |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 968 | /* A possible controlling macro of the form #if !defined (). |
| 969 | _cpp_parse_expr checks there was no other junk on the line. */ |
| 970 | pfile->mi_ind_cmacro = node; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | pfile->state.prevent_expansion--; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 974 | |
Michael Meissner | f3c33d9 | 2011-03-21 16:21:30 +0000 | [diff] [blame] | 975 | /* Do not treat conditional macros as being defined. This is due to the |
| 976 | powerpc and spu ports using conditional macros for 'vector', 'bool', and |
| 977 | 'pixel' to act as conditional keywords. This messes up tests like #ifndef |
| 978 | bool. */ |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 979 | result.unsignedp = false; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 980 | result.high = 0; |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 981 | result.overflow = false; |
Michael Meissner | f3c33d9 | 2011-03-21 16:21:30 +0000 | [diff] [blame] | 982 | result.low = (node && node->type == NT_MACRO |
| 983 | && (node->flags & NODE_CONDITIONAL) == 0); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 984 | return result; |
Zack Weinberg | 15dad1d | 2000-05-18 15:55:46 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Neil Booth | 60284a5 | 2002-04-28 23:14:56 +0000 | [diff] [blame] | 987 | /* Convert a token into a CPP_NUMBER (an interpreted preprocessing |
| 988 | number or character constant, or the result of the "defined" or "#" |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 989 | operators). */ |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 990 | static cpp_num |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 991 | eval_token (cpp_reader *pfile, const cpp_token *token, |
| 992 | source_location virtual_location) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 993 | { |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 994 | cpp_num result; |
Neil Booth | 60284a5 | 2002-04-28 23:14:56 +0000 | [diff] [blame] | 995 | unsigned int temp; |
Neil Booth | 4268e8b | 2002-05-04 07:30:32 +0000 | [diff] [blame] | 996 | int unsignedp = 0; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 997 | |
Diego Novillo | 6de9cd9 | 2004-05-13 02:41:07 -0400 | [diff] [blame] | 998 | result.unsignedp = false; |
| 999 | result.overflow = false; |
| 1000 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1001 | switch (token->type) |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 1002 | { |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1003 | case CPP_NUMBER: |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1004 | temp = cpp_classify_number (pfile, token, NULL, virtual_location); |
Ed Smith-Rowland | 3ce4f9e | 2011-10-26 19:30:59 +0000 | [diff] [blame] | 1005 | if (temp & CPP_N_USERDEF) |
| 1006 | cpp_error (pfile, CPP_DL_ERROR, |
| 1007 | "user-defined literal in preprocessor expression"); |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 1008 | switch (temp & CPP_N_CATEGORY) |
| 1009 | { |
| 1010 | case CPP_N_FLOATING: |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1011 | cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, |
| 1012 | "floating constant in preprocessor expression"); |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 1013 | break; |
| 1014 | case CPP_N_INTEGER: |
| 1015 | if (!(temp & CPP_N_IMAGINARY)) |
| 1016 | return cpp_interpret_integer (pfile, token, temp); |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1017 | cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, |
| 1018 | "imaginary number in preprocessor expression"); |
Neil Booth | cd7ab83 | 2002-05-29 17:15:42 +0000 | [diff] [blame] | 1019 | break; |
| 1020 | |
| 1021 | case CPP_N_INVALID: |
| 1022 | /* Error already issued. */ |
| 1023 | break; |
| 1024 | } |
| 1025 | result.high = result.low = 0; |
| 1026 | break; |
Neil Booth | 7f2f1a6 | 2000-11-14 18:32:06 +0000 | [diff] [blame] | 1027 | |
Alexandre Oliva | 525bc95 | 2000-02-23 19:21:07 +0000 | [diff] [blame] | 1028 | case CPP_WCHAR: |
Neil Booth | 4268e8b | 2002-05-04 07:30:32 +0000 | [diff] [blame] | 1029 | case CPP_CHAR: |
Kris Van Hees | b6baa67 | 2008-04-18 13:58:08 +0000 | [diff] [blame] | 1030 | case CPP_CHAR16: |
| 1031 | case CPP_CHAR32: |
Neil Booth | a5a4944 | 2002-05-06 22:53:10 +0000 | [diff] [blame] | 1032 | { |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1033 | cppchar_t cc = cpp_interpret_charconst (pfile, token, |
| 1034 | &temp, &unsignedp); |
| 1035 | |
| 1036 | result.high = 0; |
| 1037 | result.low = cc; |
Neil Booth | a5a4944 | 2002-05-06 22:53:10 +0000 | [diff] [blame] | 1038 | /* Sign-extend the result if necessary. */ |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1039 | if (!unsignedp && (cppchar_signed_t) cc < 0) |
| 1040 | { |
| 1041 | if (PART_PRECISION > BITS_PER_CPPCHAR_T) |
| 1042 | result.low |= ~(~(cpp_num_part) 0 |
| 1043 | >> (PART_PRECISION - BITS_PER_CPPCHAR_T)); |
| 1044 | result.high = ~(cpp_num_part) 0; |
| 1045 | result = num_trim (result, CPP_OPTION (pfile, precision)); |
| 1046 | } |
Neil Booth | a5a4944 | 2002-05-06 22:53:10 +0000 | [diff] [blame] | 1047 | } |
Neil Booth | 60284a5 | 2002-04-28 23:14:56 +0000 | [diff] [blame] | 1048 | break; |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 1049 | |
Zack Weinberg | 92936ec | 2000-07-19 20:18:08 +0000 | [diff] [blame] | 1050 | case CPP_NAME: |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 1051 | if (token->val.node.node == pfile->spec_nodes.n_defined) |
Neil Booth | 63d7500 | 2001-11-05 22:26:13 +0000 | [diff] [blame] | 1052 | return parse_defined (pfile); |
Edward Smith-Rowland | a15f7cb | 2014-10-01 11:49:23 +0000 | [diff] [blame] | 1053 | else if (token->val.node.node == pfile->spec_nodes.n__has_include__) |
| 1054 | return parse_has_include (pfile, IT_INCLUDE); |
| 1055 | else if (token->val.node.node == pfile->spec_nodes.n__has_include_next__) |
| 1056 | return parse_has_include (pfile, IT_INCLUDE_NEXT); |
Zack Weinberg | 7d4918a | 2001-02-07 18:32:42 +0000 | [diff] [blame] | 1057 | else if (CPP_OPTION (pfile, cplusplus) |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 1058 | && (token->val.node.node == pfile->spec_nodes.n_true |
| 1059 | || token->val.node.node == pfile->spec_nodes.n_false)) |
Zack Weinberg | 7d4918a | 2001-02-07 18:32:42 +0000 | [diff] [blame] | 1060 | { |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1061 | result.high = 0; |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 1062 | result.low = (token->val.node.node == pfile->spec_nodes.n_true); |
Zack Weinberg | 7d4918a | 2001-02-07 18:32:42 +0000 | [diff] [blame] | 1063 | } |
| 1064 | else |
| 1065 | { |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1066 | result.high = 0; |
| 1067 | result.low = 0; |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1068 | if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1069 | cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0, |
| 1070 | "\"%s\" is not defined", |
| 1071 | NODE_NAME (token->val.node.node)); |
Zack Weinberg | 7d4918a | 2001-02-07 18:32:42 +0000 | [diff] [blame] | 1072 | } |
Neil Booth | 60284a5 | 2002-04-28 23:14:56 +0000 | [diff] [blame] | 1073 | break; |
Zack Weinberg | 5dfa4da | 1999-02-08 20:27:27 +0000 | [diff] [blame] | 1074 | |
Tom Tromey | 899015a | 2008-05-13 14:50:27 +0000 | [diff] [blame] | 1075 | case CPP_HASH: |
| 1076 | if (!pfile->state.skipping) |
| 1077 | { |
| 1078 | /* A pedantic warning takes precedence over a deprecated |
| 1079 | warning here. */ |
| 1080 | if (CPP_PEDANTIC (pfile)) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1081 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, |
| 1082 | virtual_location, 0, |
| 1083 | "assertions are a GCC extension"); |
Joseph Myers | e3339d0 | 2010-09-29 15:49:14 +0100 | [diff] [blame] | 1084 | else if (CPP_OPTION (pfile, cpp_warn_deprecated)) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1085 | cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0, |
| 1086 | "assertions are a deprecated extension"); |
Tom Tromey | 899015a | 2008-05-13 14:50:27 +0000 | [diff] [blame] | 1087 | } |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1088 | _cpp_test_assertion (pfile, &temp); |
| 1089 | result.high = 0; |
| 1090 | result.low = temp; |
Tom Tromey | 899015a | 2008-05-13 14:50:27 +0000 | [diff] [blame] | 1091 | break; |
| 1092 | |
| 1093 | default: |
| 1094 | abort (); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1095 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1096 | |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 1097 | result.unsignedp = !!unsignedp; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1098 | return result; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
Neil Booth | 4063b94 | 2000-04-02 08:27:23 +0000 | [diff] [blame] | 1101 | /* Operator precedence and flags table. |
Neil Booth | dbac4af | 2000-04-01 07:48:59 +0000 | [diff] [blame] | 1102 | |
| 1103 | After an operator is returned from the lexer, if it has priority less |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1104 | than the operator on the top of the stack, we reduce the stack by one |
| 1105 | operator and repeat the test. Since equal priorities do not reduce, |
| 1106 | this is naturally right-associative. |
Neil Booth | dbac4af | 2000-04-01 07:48:59 +0000 | [diff] [blame] | 1107 | |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1108 | We handle left-associative operators by decrementing the priority of |
| 1109 | just-lexed operators by one, but retaining the priority of operators |
| 1110 | already on the stack. |
Neil Booth | dbac4af | 2000-04-01 07:48:59 +0000 | [diff] [blame] | 1111 | |
| 1112 | The remaining cases are '(' and ')'. We handle '(' by skipping the |
| 1113 | reduction phase completely. ')' is given lower priority than |
| 1114 | everything else, including '(', effectively forcing a reduction of the |
Kazu Hirata | 272d0be | 2002-12-19 05:18:13 +0000 | [diff] [blame] | 1115 | parenthesized expression. If there is a matching '(', the routine |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1116 | reduce() exits immediately. If the normal exit route sees a ')', then |
| 1117 | there cannot have been a matching '(' and an error message is output. |
Neil Booth | dbac4af | 2000-04-01 07:48:59 +0000 | [diff] [blame] | 1118 | |
Neil Booth | f8b954f | 2002-04-26 06:32:50 +0000 | [diff] [blame] | 1119 | The parser assumes all shifted operators require a left operand unless |
| 1120 | the flag NO_L_OPERAND is set. These semantics are automatic; any |
| 1121 | extra semantics need to be handled with operator-specific code. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1122 | |
Neil Booth | 68e65275 | 2002-07-20 13:31:56 +0000 | [diff] [blame] | 1123 | /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an |
| 1124 | operand changes because of integer promotions. */ |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1125 | #define NO_L_OPERAND (1 << 0) |
| 1126 | #define LEFT_ASSOC (1 << 1) |
Neil Booth | 68e65275 | 2002-07-20 13:31:56 +0000 | [diff] [blame] | 1127 | #define CHECK_PROMOTION (1 << 2) |
Neil Booth | eba3052 | 2000-03-31 22:23:59 +0000 | [diff] [blame] | 1128 | |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 1129 | /* Operator to priority map. Must be in the same order as the first |
| 1130 | N entries of enum cpp_ttype. */ |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1131 | static const struct cpp_operator |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 1132 | { |
Neil Booth | 60284a5 | 2002-04-28 23:14:56 +0000 | [diff] [blame] | 1133 | uchar prio; |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1134 | uchar flags; |
| 1135 | } optab[] = |
| 1136 | { |
Neil Booth | ad28cff | 2002-07-18 22:08:35 +0000 | [diff] [blame] | 1137 | /* EQ */ {0, 0}, /* Shouldn't happen. */ |
| 1138 | /* NOT */ {16, NO_L_OPERAND}, |
Neil Booth | 68e65275 | 2002-07-20 13:31:56 +0000 | [diff] [blame] | 1139 | /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION}, |
| 1140 | /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION}, |
| 1141 | /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION}, |
| 1142 | /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION}, |
| 1143 | /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION}, |
| 1144 | /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION}, |
| 1145 | /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION}, |
| 1146 | /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION}, |
| 1147 | /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION}, |
| 1148 | /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION}, |
Neil Booth | ad28cff | 2002-07-18 22:08:35 +0000 | [diff] [blame] | 1149 | /* RSHIFT */ {13, LEFT_ASSOC}, |
| 1150 | /* LSHIFT */ {13, LEFT_ASSOC}, |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 1151 | |
Neil Booth | ad28cff | 2002-07-18 22:08:35 +0000 | [diff] [blame] | 1152 | /* COMPL */ {16, NO_L_OPERAND}, |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1153 | /* AND_AND */ {6, LEFT_ASSOC}, |
| 1154 | /* OR_OR */ {5, LEFT_ASSOC}, |
Tom Tromey | 71c1003 | 2008-05-06 17:15:07 +0000 | [diff] [blame] | 1155 | /* Note that QUERY, COLON, and COMMA must have the same precedence. |
| 1156 | However, there are some special cases for these in reduce(). */ |
| 1157 | /* QUERY */ {4, 0}, |
Neil Booth | 68e65275 | 2002-07-20 13:31:56 +0000 | [diff] [blame] | 1158 | /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION}, |
Tom Tromey | 71c1003 | 2008-05-06 17:15:07 +0000 | [diff] [blame] | 1159 | /* COMMA */ {4, LEFT_ASSOC}, |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1160 | /* OPEN_PAREN */ {1, NO_L_OPERAND}, |
Neil Booth | ad28cff | 2002-07-18 22:08:35 +0000 | [diff] [blame] | 1161 | /* CLOSE_PAREN */ {0, 0}, |
| 1162 | /* EOF */ {0, 0}, |
| 1163 | /* EQ_EQ */ {11, LEFT_ASSOC}, |
| 1164 | /* NOT_EQ */ {11, LEFT_ASSOC}, |
Neil Booth | 68e65275 | 2002-07-20 13:31:56 +0000 | [diff] [blame] | 1165 | /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION}, |
| 1166 | /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION}, |
Neil Booth | ad28cff | 2002-07-18 22:08:35 +0000 | [diff] [blame] | 1167 | /* UPLUS */ {16, NO_L_OPERAND}, |
| 1168 | /* UMINUS */ {16, NO_L_OPERAND} |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 1169 | }; |
| 1170 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1171 | /* Parse and evaluate a C expression, reading from PFILE. |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 1172 | Returns the truth value of the expression. |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1173 | |
| 1174 | The implementation is an operator precedence parser, i.e. a |
| 1175 | bottom-up parser, using a stack for not-yet-reduced tokens. |
| 1176 | |
| 1177 | The stack base is op_stack, and the current stack pointer is 'top'. |
| 1178 | There is a stack element for each operator (only), and the most |
| 1179 | recently pushed operator is 'top->op'. An operand (value) is |
| 1180 | stored in the 'value' field of the stack element of the operator |
| 1181 | that precedes it. */ |
| 1182 | bool |
Tom Tromey | d750887 | 2008-05-30 14:25:09 +0000 | [diff] [blame] | 1183 | _cpp_parse_expr (cpp_reader *pfile, bool is_if) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1184 | { |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1185 | struct op *top = pfile->op_stack; |
| 1186 | unsigned int lex_count; |
| 1187 | bool saw_leading_not, want_value = true; |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1188 | source_location virtual_location = 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1189 | |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1190 | pfile->state.skip_eval = 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1191 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1192 | /* Set up detection of #if ! defined(). */ |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1193 | pfile->mi_ind_cmacro = 0; |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1194 | saw_leading_not = false; |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1195 | lex_count = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1196 | |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1197 | /* Lowest priority operator prevents further reductions. */ |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 1198 | top->op = CPP_EOF; |
Neil Booth | 4063b94 | 2000-04-02 08:27:23 +0000 | [diff] [blame] | 1199 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1200 | for (;;) |
| 1201 | { |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 1202 | struct op op; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1203 | |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1204 | lex_count++; |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1205 | op.token = cpp_get_token_with_location (pfile, &virtual_location); |
Neil Booth | 68e65275 | 2002-07-20 13:31:56 +0000 | [diff] [blame] | 1206 | op.op = op.token->type; |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1207 | op.loc = virtual_location; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1208 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1209 | switch (op.op) |
| 1210 | { |
Neil Booth | 60284a5 | 2002-04-28 23:14:56 +0000 | [diff] [blame] | 1211 | /* These tokens convert into values. */ |
Neil Booth | c60e94a | 2001-07-19 06:12:50 +0000 | [diff] [blame] | 1212 | case CPP_NUMBER: |
Neil Booth | 60284a5 | 2002-04-28 23:14:56 +0000 | [diff] [blame] | 1213 | case CPP_CHAR: |
| 1214 | case CPP_WCHAR: |
Kris Van Hees | b6baa67 | 2008-04-18 13:58:08 +0000 | [diff] [blame] | 1215 | case CPP_CHAR16: |
| 1216 | case CPP_CHAR32: |
Neil Booth | 60284a5 | 2002-04-28 23:14:56 +0000 | [diff] [blame] | 1217 | case CPP_NAME: |
| 1218 | case CPP_HASH: |
Neil Booth | f8b954f | 2002-04-26 06:32:50 +0000 | [diff] [blame] | 1219 | if (!want_value) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1220 | SYNTAX_ERROR2_AT (op.loc, |
| 1221 | "missing binary operator before token \"%s\"", |
| 1222 | cpp_token_as_text (pfile, op.token)); |
Neil Booth | f8b954f | 2002-04-26 06:32:50 +0000 | [diff] [blame] | 1223 | want_value = false; |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1224 | top->value = eval_token (pfile, op.token, op.loc); |
Neil Booth | 9ee70313 | 2000-04-01 07:42:37 +0000 | [diff] [blame] | 1225 | continue; |
| 1226 | |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1227 | case CPP_NOT: |
| 1228 | saw_leading_not = lex_count == 1; |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1229 | break; |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 1230 | case CPP_PLUS: |
Neil Booth | f8b954f | 2002-04-26 06:32:50 +0000 | [diff] [blame] | 1231 | if (want_value) |
| 1232 | op.op = CPP_UPLUS; |
| 1233 | break; |
| 1234 | case CPP_MINUS: |
| 1235 | if (want_value) |
| 1236 | op.op = CPP_UMINUS; |
| 1237 | break; |
Neil Booth | 60284a5 | 2002-04-28 23:14:56 +0000 | [diff] [blame] | 1238 | |
Neil Booth | f8b954f | 2002-04-26 06:32:50 +0000 | [diff] [blame] | 1239 | default: |
Neil Booth | 60284a5 | 2002-04-28 23:14:56 +0000 | [diff] [blame] | 1240 | if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1241 | SYNTAX_ERROR2_AT (op.loc, |
| 1242 | "token \"%s\" is not valid in preprocessor expressions", |
| 1243 | cpp_token_as_text (pfile, op.token)); |
Neil Booth | f8b954f | 2002-04-26 06:32:50 +0000 | [diff] [blame] | 1244 | break; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1245 | } |
| 1246 | |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1247 | /* Check we have a value or operator as appropriate. */ |
| 1248 | if (optab[op.op].flags & NO_L_OPERAND) |
Neil Booth | 4063b94 | 2000-04-02 08:27:23 +0000 | [diff] [blame] | 1249 | { |
Neil Booth | f8b954f | 2002-04-26 06:32:50 +0000 | [diff] [blame] | 1250 | if (!want_value) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1251 | SYNTAX_ERROR2_AT (op.loc, |
| 1252 | "missing binary operator before token \"%s\"", |
| 1253 | cpp_token_as_text (pfile, op.token)); |
Neil Booth | b22ef13 | 2000-04-03 22:33:12 +0000 | [diff] [blame] | 1254 | } |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1255 | else if (want_value) |
Neil Booth | b22ef13 | 2000-04-03 22:33:12 +0000 | [diff] [blame] | 1256 | { |
Neil Booth | a09d474 | 2004-07-04 12:57:50 +0000 | [diff] [blame] | 1257 | /* We want a number (or expression) and haven't got one. |
| 1258 | Try to emit a specific diagnostic. */ |
| 1259 | if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1260 | SYNTAX_ERROR_AT (op.loc, |
| 1261 | "missing expression between '(' and ')'"); |
Neil Booth | a09d474 | 2004-07-04 12:57:50 +0000 | [diff] [blame] | 1262 | |
| 1263 | if (op.op == CPP_EOF && top->op == CPP_EOF) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1264 | SYNTAX_ERROR2_AT (op.loc, |
| 1265 | "%s with no expression", is_if ? "#if" : "#elif"); |
Neil Booth | a09d474 | 2004-07-04 12:57:50 +0000 | [diff] [blame] | 1266 | |
| 1267 | if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1268 | SYNTAX_ERROR2_AT (op.loc, |
| 1269 | "operator '%s' has no right operand", |
| 1270 | cpp_token_as_text (pfile, top->token)); |
Neil Booth | a09d474 | 2004-07-04 12:57:50 +0000 | [diff] [blame] | 1271 | else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF) |
| 1272 | /* Complain about missing paren during reduction. */; |
| 1273 | else |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1274 | SYNTAX_ERROR2_AT (op.loc, |
| 1275 | "operator '%s' has no left operand", |
| 1276 | cpp_token_as_text (pfile, op.token)); |
Neil Booth | 4063b94 | 2000-04-02 08:27:23 +0000 | [diff] [blame] | 1277 | } |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1278 | |
| 1279 | top = reduce (pfile, top, op.op); |
| 1280 | if (!top) |
| 1281 | goto syntax_error; |
| 1282 | |
Neil Booth | 60284a5 | 2002-04-28 23:14:56 +0000 | [diff] [blame] | 1283 | if (op.op == CPP_EOF) |
| 1284 | break; |
| 1285 | |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1286 | switch (op.op) |
| 1287 | { |
| 1288 | case CPP_CLOSE_PAREN: |
| 1289 | continue; |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1290 | case CPP_OR_OR: |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1291 | if (!num_zerop (top->value)) |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1292 | pfile->state.skip_eval++; |
| 1293 | break; |
| 1294 | case CPP_AND_AND: |
| 1295 | case CPP_QUERY: |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1296 | if (num_zerop (top->value)) |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1297 | pfile->state.skip_eval++; |
| 1298 | break; |
| 1299 | case CPP_COLON: |
Neil Booth | 60284a5 | 2002-04-28 23:14:56 +0000 | [diff] [blame] | 1300 | if (top->op != CPP_QUERY) |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1301 | SYNTAX_ERROR_AT (op.loc, |
| 1302 | " ':' without preceding '?'"); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1303 | if (!num_zerop (top[-1].value)) /* Was '?' condition true? */ |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1304 | pfile->state.skip_eval++; |
| 1305 | else |
| 1306 | pfile->state.skip_eval--; |
| 1307 | default: |
| 1308 | break; |
| 1309 | } |
| 1310 | |
Neil Booth | f8b954f | 2002-04-26 06:32:50 +0000 | [diff] [blame] | 1311 | want_value = true; |
Neil Booth | 4063b94 | 2000-04-02 08:27:23 +0000 | [diff] [blame] | 1312 | |
Mike Stump | 0f41302 | 1996-07-03 22:07:53 +0000 | [diff] [blame] | 1313 | /* Check for and handle stack overflow. */ |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1314 | if (++top == pfile->op_limit) |
| 1315 | top = _cpp_expand_op_stack (pfile); |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 1316 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1317 | top->op = op.op; |
Neil Booth | 68e65275 | 2002-07-20 13:31:56 +0000 | [diff] [blame] | 1318 | top->token = op.token; |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1319 | top->loc = op.loc; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1320 | } |
Neil Booth | 9ee70313 | 2000-04-01 07:42:37 +0000 | [diff] [blame] | 1321 | |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1322 | /* The controlling macro expression is only valid if we called lex 3 |
| 1323 | times: <!> <defined expression> and <EOF>. push_conditional () |
| 1324 | checks that we are at top-of-file. */ |
| 1325 | if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3)) |
| 1326 | pfile->mi_ind_cmacro = 0; |
| 1327 | |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1328 | if (top != pfile->op_stack) |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1329 | { |
Dodji Seketeli | 0b2c4be | 2012-05-16 10:51:15 +0000 | [diff] [blame] | 1330 | cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0, |
| 1331 | "unbalanced stack in %s", |
| 1332 | is_if ? "#if" : "#elif"); |
Neil Booth | 4063b94 | 2000-04-02 08:27:23 +0000 | [diff] [blame] | 1333 | syntax_error: |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1334 | return false; /* Return false on syntax error. */ |
Neil Booth | 4063b94 | 2000-04-02 08:27:23 +0000 | [diff] [blame] | 1335 | } |
Neil Booth | 9ee70313 | 2000-04-01 07:42:37 +0000 | [diff] [blame] | 1336 | |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1337 | return !num_zerop (top->value); |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | /* Reduce the operator / value stack if possible, in preparation for |
| 1341 | pushing operator OP. Returns NULL on error, otherwise the top of |
| 1342 | the stack. */ |
| 1343 | static struct op * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1344 | reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op) |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1345 | { |
| 1346 | unsigned int prio; |
| 1347 | |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1348 | if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2) |
| 1349 | { |
| 1350 | bad_op: |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1351 | cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1352 | return 0; |
| 1353 | } |
| 1354 | |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1355 | if (op == CPP_OPEN_PAREN) |
| 1356 | return top; |
| 1357 | |
| 1358 | /* Decrement the priority of left-associative operators to force a |
| 1359 | reduction with operators of otherwise equal priority. */ |
| 1360 | prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0); |
| 1361 | while (prio < optab[top->op].prio) |
| 1362 | { |
Neil Booth | 68e65275 | 2002-07-20 13:31:56 +0000 | [diff] [blame] | 1363 | if (CPP_OPTION (pfile, warn_num_sign_change) |
| 1364 | && optab[top->op].flags & CHECK_PROMOTION) |
| 1365 | check_promotion (pfile, top); |
| 1366 | |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1367 | switch (top->op) |
| 1368 | { |
| 1369 | case CPP_UPLUS: |
| 1370 | case CPP_UMINUS: |
| 1371 | case CPP_NOT: |
| 1372 | case CPP_COMPL: |
| 1373 | top[-1].value = num_unary_op (pfile, top->value, top->op); |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 1374 | top[-1].loc = top->loc; |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1375 | break; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1376 | |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1377 | case CPP_PLUS: |
| 1378 | case CPP_MINUS: |
| 1379 | case CPP_RSHIFT: |
| 1380 | case CPP_LSHIFT: |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1381 | case CPP_COMMA: |
| 1382 | top[-1].value = num_binary_op (pfile, top[-1].value, |
| 1383 | top->value, top->op); |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 1384 | top[-1].loc = top->loc; |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1385 | break; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1386 | |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1387 | case CPP_GREATER: |
| 1388 | case CPP_LESS: |
| 1389 | case CPP_GREATER_EQ: |
| 1390 | case CPP_LESS_EQ: |
| 1391 | top[-1].value |
| 1392 | = num_inequality_op (pfile, top[-1].value, top->value, top->op); |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 1393 | top[-1].loc = top->loc; |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1394 | break; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1395 | |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1396 | case CPP_EQ_EQ: |
| 1397 | case CPP_NOT_EQ: |
| 1398 | top[-1].value |
| 1399 | = num_equality_op (pfile, top[-1].value, top->value, top->op); |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 1400 | top[-1].loc = top->loc; |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1401 | break; |
Neil Booth | ad28cff | 2002-07-18 22:08:35 +0000 | [diff] [blame] | 1402 | |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1403 | case CPP_AND: |
| 1404 | case CPP_OR: |
| 1405 | case CPP_XOR: |
| 1406 | top[-1].value |
| 1407 | = num_bitwise_op (pfile, top[-1].value, top->value, top->op); |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 1408 | top[-1].loc = top->loc; |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1409 | break; |
Neil Booth | ad28cff | 2002-07-18 22:08:35 +0000 | [diff] [blame] | 1410 | |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1411 | case CPP_MULT: |
| 1412 | top[-1].value = num_mul (pfile, top[-1].value, top->value); |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 1413 | top[-1].loc = top->loc; |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1414 | break; |
Neil Booth | ad28cff | 2002-07-18 22:08:35 +0000 | [diff] [blame] | 1415 | |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1416 | case CPP_DIV: |
| 1417 | case CPP_MOD: |
| 1418 | top[-1].value = num_div_op (pfile, top[-1].value, |
Manuel López-Ibáñez | b506a5a | 2009-06-18 15:10:23 +0000 | [diff] [blame] | 1419 | top->value, top->op, top->loc); |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 1420 | top[-1].loc = top->loc; |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1421 | break; |
Neil Booth | ad28cff | 2002-07-18 22:08:35 +0000 | [diff] [blame] | 1422 | |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1423 | case CPP_OR_OR: |
| 1424 | top--; |
| 1425 | if (!num_zerop (top->value)) |
| 1426 | pfile->state.skip_eval--; |
| 1427 | top->value.low = (!num_zerop (top->value) |
| 1428 | || !num_zerop (top[1].value)); |
| 1429 | top->value.high = 0; |
| 1430 | top->value.unsignedp = false; |
| 1431 | top->value.overflow = false; |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 1432 | top->loc = top[1].loc; |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1433 | continue; |
| 1434 | |
| 1435 | case CPP_AND_AND: |
| 1436 | top--; |
| 1437 | if (num_zerop (top->value)) |
| 1438 | pfile->state.skip_eval--; |
| 1439 | top->value.low = (!num_zerop (top->value) |
| 1440 | && !num_zerop (top[1].value)); |
| 1441 | top->value.high = 0; |
| 1442 | top->value.unsignedp = false; |
| 1443 | top->value.overflow = false; |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 1444 | top->loc = top[1].loc; |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1445 | continue; |
| 1446 | |
| 1447 | case CPP_OPEN_PAREN: |
| 1448 | if (op != CPP_CLOSE_PAREN) |
| 1449 | { |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 1450 | cpp_error_with_line (pfile, CPP_DL_ERROR, |
| 1451 | top->token->src_loc, |
| 1452 | 0, "missing ')' in expression"); |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |
| 1455 | top--; |
| 1456 | top->value = top[1].value; |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 1457 | top->loc = top[1].loc; |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1458 | return top; |
| 1459 | |
| 1460 | case CPP_COLON: |
| 1461 | top -= 2; |
| 1462 | if (!num_zerop (top->value)) |
| 1463 | { |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1464 | pfile->state.skip_eval--; |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1465 | top->value = top[1].value; |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 1466 | top->loc = top[1].loc; |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1467 | } |
| 1468 | else |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 1469 | { |
| 1470 | top->value = top[2].value; |
| 1471 | top->loc = top[2].loc; |
| 1472 | } |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1473 | top->value.unsignedp = (top[1].value.unsignedp |
| 1474 | || top[2].value.unsignedp); |
| 1475 | continue; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1476 | |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1477 | case CPP_QUERY: |
Tom Tromey | 71c1003 | 2008-05-06 17:15:07 +0000 | [diff] [blame] | 1478 | /* COMMA and COLON should not reduce a QUERY operator. */ |
| 1479 | if (op == CPP_COMMA || op == CPP_COLON) |
| 1480 | return top; |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1481 | cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'"); |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1482 | return 0; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1483 | |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1484 | default: |
| 1485 | goto bad_op; |
| 1486 | } |
Neil Booth | ad28cff | 2002-07-18 22:08:35 +0000 | [diff] [blame] | 1487 | |
| 1488 | top--; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1489 | if (top->value.overflow && !pfile->state.skip_eval) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1490 | cpp_error (pfile, CPP_DL_PEDWARN, |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1491 | "integer overflow in preprocessor expression"); |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | if (op == CPP_CLOSE_PAREN) |
| 1495 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1496 | cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression"); |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | return top; |
| 1501 | } |
| 1502 | |
| 1503 | /* Returns the position of the old top of stack after expansion. */ |
| 1504 | struct op * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1505 | _cpp_expand_op_stack (cpp_reader *pfile) |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1506 | { |
Neil Booth | 32fa456 | 2002-05-09 22:27:31 +0000 | [diff] [blame] | 1507 | size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack); |
| 1508 | size_t new_size = old_size * 2 + 20; |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1509 | |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1510 | pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size); |
Neil Booth | 32fa456 | 2002-05-09 22:27:31 +0000 | [diff] [blame] | 1511 | pfile->op_limit = pfile->op_stack + new_size; |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1512 | |
Neil Booth | 32fa456 | 2002-05-09 22:27:31 +0000 | [diff] [blame] | 1513 | return pfile->op_stack + old_size; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1514 | } |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1515 | |
Neil Booth | 68e65275 | 2002-07-20 13:31:56 +0000 | [diff] [blame] | 1516 | /* Emits a warning if the effective sign of either operand of OP |
| 1517 | changes because of integer promotions. */ |
| 1518 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1519 | check_promotion (cpp_reader *pfile, const struct op *op) |
Neil Booth | 68e65275 | 2002-07-20 13:31:56 +0000 | [diff] [blame] | 1520 | { |
| 1521 | if (op->value.unsignedp == op[-1].value.unsignedp) |
| 1522 | return; |
| 1523 | |
| 1524 | if (op->value.unsignedp) |
| 1525 | { |
| 1526 | if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision))) |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 1527 | cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0, |
| 1528 | "the left operand of \"%s\" changes sign when promoted", |
| 1529 | cpp_token_as_text (pfile, op->token)); |
Neil Booth | 68e65275 | 2002-07-20 13:31:56 +0000 | [diff] [blame] | 1530 | } |
| 1531 | else if (!num_positive (op->value, CPP_OPTION (pfile, precision))) |
Manuel López-Ibáñez | 47960aa | 2008-10-31 22:00:37 +0000 | [diff] [blame] | 1532 | cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0, |
Neil Booth | 68e65275 | 2002-07-20 13:31:56 +0000 | [diff] [blame] | 1533 | "the right operand of \"%s\" changes sign when promoted", |
| 1534 | cpp_token_as_text (pfile, op->token)); |
| 1535 | } |
| 1536 | |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1537 | /* Clears the unused high order bits of the number pointed to by PNUM. */ |
| 1538 | static cpp_num |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1539 | num_trim (cpp_num num, size_t precision) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1540 | { |
| 1541 | if (precision > PART_PRECISION) |
| 1542 | { |
| 1543 | precision -= PART_PRECISION; |
| 1544 | if (precision < PART_PRECISION) |
Neil Booth | 359b0be | 2002-05-28 05:44:06 +0000 | [diff] [blame] | 1545 | num.high &= ((cpp_num_part) 1 << precision) - 1; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1546 | } |
| 1547 | else |
| 1548 | { |
| 1549 | if (precision < PART_PRECISION) |
Neil Booth | 359b0be | 2002-05-28 05:44:06 +0000 | [diff] [blame] | 1550 | num.low &= ((cpp_num_part) 1 << precision) - 1; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1551 | num.high = 0; |
| 1552 | } |
| 1553 | |
| 1554 | return num; |
| 1555 | } |
| 1556 | |
| 1557 | /* True iff A (presumed signed) >= 0. */ |
| 1558 | static bool |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1559 | num_positive (cpp_num num, size_t precision) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1560 | { |
| 1561 | if (precision > PART_PRECISION) |
| 1562 | { |
| 1563 | precision -= PART_PRECISION; |
Neil Booth | 359b0be | 2002-05-28 05:44:06 +0000 | [diff] [blame] | 1564 | return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Neil Booth | 359b0be | 2002-05-28 05:44:06 +0000 | [diff] [blame] | 1567 | return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
Neil Booth | ceeedfc | 2002-06-02 19:37:34 +0000 | [diff] [blame] | 1570 | /* Sign extend a number, with PRECISION significant bits and all |
| 1571 | others assumed clear, to fill out a cpp_num structure. */ |
| 1572 | cpp_num |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1573 | cpp_num_sign_extend (cpp_num num, size_t precision) |
Neil Booth | ceeedfc | 2002-06-02 19:37:34 +0000 | [diff] [blame] | 1574 | { |
| 1575 | if (!num.unsignedp) |
| 1576 | { |
| 1577 | if (precision > PART_PRECISION) |
| 1578 | { |
| 1579 | precision -= PART_PRECISION; |
| 1580 | if (precision < PART_PRECISION |
| 1581 | && (num.high & (cpp_num_part) 1 << (precision - 1))) |
| 1582 | num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision)); |
| 1583 | } |
| 1584 | else if (num.low & (cpp_num_part) 1 << (precision - 1)) |
| 1585 | { |
| 1586 | if (precision < PART_PRECISION) |
| 1587 | num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision)); |
| 1588 | num.high = ~(cpp_num_part) 0; |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | return num; |
| 1593 | } |
| 1594 | |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1595 | /* Returns the negative of NUM. */ |
| 1596 | static cpp_num |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1597 | num_negate (cpp_num num, size_t precision) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1598 | { |
| 1599 | cpp_num copy; |
| 1600 | |
| 1601 | copy = num; |
| 1602 | num.high = ~num.high; |
| 1603 | num.low = ~num.low; |
| 1604 | if (++num.low == 0) |
| 1605 | num.high++; |
| 1606 | num = num_trim (num, precision); |
| 1607 | num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num)); |
| 1608 | |
| 1609 | return num; |
| 1610 | } |
| 1611 | |
| 1612 | /* Returns true if A >= B. */ |
| 1613 | static bool |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1614 | num_greater_eq (cpp_num pa, cpp_num pb, size_t precision) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1615 | { |
| 1616 | bool unsignedp; |
| 1617 | |
| 1618 | unsignedp = pa.unsignedp || pb.unsignedp; |
| 1619 | |
| 1620 | if (!unsignedp) |
| 1621 | { |
| 1622 | /* Both numbers have signed type. If they are of different |
| 1623 | sign, the answer is the sign of A. */ |
| 1624 | unsignedp = num_positive (pa, precision); |
| 1625 | |
| 1626 | if (unsignedp != num_positive (pb, precision)) |
| 1627 | return unsignedp; |
| 1628 | |
| 1629 | /* Otherwise we can do an unsigned comparison. */ |
| 1630 | } |
| 1631 | |
| 1632 | return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low); |
| 1633 | } |
| 1634 | |
| 1635 | /* Returns LHS OP RHS, where OP is a bit-wise operation. */ |
| 1636 | static cpp_num |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1637 | num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED, |
| 1638 | cpp_num lhs, cpp_num rhs, enum cpp_ttype op) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1639 | { |
| 1640 | lhs.overflow = false; |
| 1641 | lhs.unsignedp = lhs.unsignedp || rhs.unsignedp; |
| 1642 | |
| 1643 | /* As excess precision is zeroed, there is no need to num_trim () as |
| 1644 | these operations cannot introduce a set bit there. */ |
| 1645 | if (op == CPP_AND) |
| 1646 | { |
| 1647 | lhs.low &= rhs.low; |
| 1648 | lhs.high &= rhs.high; |
| 1649 | } |
| 1650 | else if (op == CPP_OR) |
| 1651 | { |
| 1652 | lhs.low |= rhs.low; |
| 1653 | lhs.high |= rhs.high; |
| 1654 | } |
| 1655 | else |
| 1656 | { |
| 1657 | lhs.low ^= rhs.low; |
| 1658 | lhs.high ^= rhs.high; |
| 1659 | } |
| 1660 | |
| 1661 | return lhs; |
| 1662 | } |
| 1663 | |
| 1664 | /* Returns LHS OP RHS, where OP is an inequality. */ |
| 1665 | static cpp_num |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1666 | num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, |
| 1667 | enum cpp_ttype op) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1668 | { |
| 1669 | bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision)); |
| 1670 | |
| 1671 | if (op == CPP_GREATER_EQ) |
| 1672 | lhs.low = gte; |
| 1673 | else if (op == CPP_LESS) |
| 1674 | lhs.low = !gte; |
| 1675 | else if (op == CPP_GREATER) |
| 1676 | lhs.low = gte && !num_eq (lhs, rhs); |
| 1677 | else /* CPP_LESS_EQ. */ |
| 1678 | lhs.low = !gte || num_eq (lhs, rhs); |
| 1679 | |
| 1680 | lhs.high = 0; |
| 1681 | lhs.overflow = false; |
| 1682 | lhs.unsignedp = false; |
| 1683 | return lhs; |
| 1684 | } |
| 1685 | |
| 1686 | /* Returns LHS OP RHS, where OP is == or !=. */ |
| 1687 | static cpp_num |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1688 | num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED, |
| 1689 | cpp_num lhs, cpp_num rhs, enum cpp_ttype op) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1690 | { |
Jason Merrill | 9745979 | 2002-06-07 09:29:17 -0400 | [diff] [blame] | 1691 | /* Work around a 3.0.4 bug; see PR 6950. */ |
| 1692 | bool eq = num_eq (lhs, rhs); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1693 | if (op == CPP_NOT_EQ) |
Jason Merrill | 9745979 | 2002-06-07 09:29:17 -0400 | [diff] [blame] | 1694 | eq = !eq; |
| 1695 | lhs.low = eq; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1696 | lhs.high = 0; |
| 1697 | lhs.overflow = false; |
| 1698 | lhs.unsignedp = false; |
| 1699 | return lhs; |
| 1700 | } |
| 1701 | |
| 1702 | /* Shift NUM, of width PRECISION, right by N bits. */ |
| 1703 | static cpp_num |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1704 | num_rshift (cpp_num num, size_t precision, size_t n) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1705 | { |
| 1706 | cpp_num_part sign_mask; |
Diego Novillo | 6de9cd9 | 2004-05-13 02:41:07 -0400 | [diff] [blame] | 1707 | bool x = num_positive (num, precision); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1708 | |
Diego Novillo | 6de9cd9 | 2004-05-13 02:41:07 -0400 | [diff] [blame] | 1709 | if (num.unsignedp || x) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1710 | sign_mask = 0; |
| 1711 | else |
| 1712 | sign_mask = ~(cpp_num_part) 0; |
| 1713 | |
| 1714 | if (n >= precision) |
| 1715 | num.high = num.low = sign_mask; |
| 1716 | else |
| 1717 | { |
| 1718 | /* Sign-extend. */ |
| 1719 | if (precision < PART_PRECISION) |
| 1720 | num.high = sign_mask, num.low |= sign_mask << precision; |
| 1721 | else if (precision < 2 * PART_PRECISION) |
| 1722 | num.high |= sign_mask << (precision - PART_PRECISION); |
| 1723 | |
| 1724 | if (n >= PART_PRECISION) |
| 1725 | { |
| 1726 | n -= PART_PRECISION; |
| 1727 | num.low = num.high; |
| 1728 | num.high = sign_mask; |
| 1729 | } |
| 1730 | |
| 1731 | if (n) |
| 1732 | { |
| 1733 | num.low = (num.low >> n) | (num.high << (PART_PRECISION - n)); |
| 1734 | num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n)); |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | num = num_trim (num, precision); |
| 1739 | num.overflow = false; |
| 1740 | return num; |
| 1741 | } |
| 1742 | |
| 1743 | /* Shift NUM, of width PRECISION, left by N bits. */ |
| 1744 | static cpp_num |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1745 | num_lshift (cpp_num num, size_t precision, size_t n) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1746 | { |
| 1747 | if (n >= precision) |
| 1748 | { |
| 1749 | num.overflow = !num.unsignedp && !num_zerop (num); |
| 1750 | num.high = num.low = 0; |
| 1751 | } |
| 1752 | else |
| 1753 | { |
| 1754 | cpp_num orig, maybe_orig; |
| 1755 | size_t m = n; |
| 1756 | |
| 1757 | orig = num; |
| 1758 | if (m >= PART_PRECISION) |
| 1759 | { |
| 1760 | m -= PART_PRECISION; |
| 1761 | num.high = num.low; |
| 1762 | num.low = 0; |
| 1763 | } |
| 1764 | if (m) |
| 1765 | { |
| 1766 | num.high = (num.high << m) | (num.low >> (PART_PRECISION - m)); |
| 1767 | num.low <<= m; |
| 1768 | } |
| 1769 | num = num_trim (num, precision); |
| 1770 | |
| 1771 | if (num.unsignedp) |
| 1772 | num.overflow = false; |
| 1773 | else |
| 1774 | { |
| 1775 | maybe_orig = num_rshift (num, precision, n); |
| 1776 | num.overflow = !num_eq (orig, maybe_orig); |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | return num; |
| 1781 | } |
| 1782 | |
| 1783 | /* The four unary operators: +, -, ! and ~. */ |
| 1784 | static cpp_num |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1785 | num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1786 | { |
| 1787 | switch (op) |
| 1788 | { |
| 1789 | case CPP_UPLUS: |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 1790 | if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval) |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 1791 | cpp_warning (pfile, CPP_W_TRADITIONAL, |
| 1792 | "traditional C rejects the unary plus operator"); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1793 | num.overflow = false; |
| 1794 | break; |
| 1795 | |
| 1796 | case CPP_UMINUS: |
| 1797 | num = num_negate (num, CPP_OPTION (pfile, precision)); |
| 1798 | break; |
| 1799 | |
| 1800 | case CPP_COMPL: |
| 1801 | num.high = ~num.high; |
| 1802 | num.low = ~num.low; |
| 1803 | num = num_trim (num, CPP_OPTION (pfile, precision)); |
| 1804 | num.overflow = false; |
| 1805 | break; |
| 1806 | |
| 1807 | default: /* case CPP_NOT: */ |
| 1808 | num.low = num_zerop (num); |
| 1809 | num.high = 0; |
| 1810 | num.overflow = false; |
| 1811 | num.unsignedp = false; |
| 1812 | break; |
| 1813 | } |
| 1814 | |
| 1815 | return num; |
| 1816 | } |
| 1817 | |
| 1818 | /* The various binary operators. */ |
| 1819 | static cpp_num |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1820 | num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1821 | { |
| 1822 | cpp_num result; |
| 1823 | size_t precision = CPP_OPTION (pfile, precision); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1824 | size_t n; |
| 1825 | |
| 1826 | switch (op) |
| 1827 | { |
| 1828 | /* Shifts. */ |
| 1829 | case CPP_LSHIFT: |
| 1830 | case CPP_RSHIFT: |
| 1831 | if (!rhs.unsignedp && !num_positive (rhs, precision)) |
| 1832 | { |
| 1833 | /* A negative shift is a positive shift the other way. */ |
| 1834 | if (op == CPP_LSHIFT) |
| 1835 | op = CPP_RSHIFT; |
| 1836 | else |
| 1837 | op = CPP_LSHIFT; |
| 1838 | rhs = num_negate (rhs, precision); |
| 1839 | } |
| 1840 | if (rhs.high) |
| 1841 | n = ~0; /* Maximal. */ |
| 1842 | else |
| 1843 | n = rhs.low; |
| 1844 | if (op == CPP_LSHIFT) |
| 1845 | lhs = num_lshift (lhs, precision, n); |
| 1846 | else |
| 1847 | lhs = num_rshift (lhs, precision, n); |
| 1848 | break; |
| 1849 | |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1850 | /* Arithmetic. */ |
| 1851 | case CPP_MINUS: |
Joseph Myers | 3a4efce | 2013-12-10 01:23:37 +0000 | [diff] [blame] | 1852 | result.low = lhs.low - rhs.low; |
| 1853 | result.high = lhs.high - rhs.high; |
| 1854 | if (result.low > lhs.low) |
| 1855 | result.high--; |
| 1856 | result.unsignedp = lhs.unsignedp || rhs.unsignedp; |
| 1857 | result.overflow = false; |
| 1858 | |
| 1859 | result = num_trim (result, precision); |
| 1860 | if (!result.unsignedp) |
| 1861 | { |
| 1862 | bool lhsp = num_positive (lhs, precision); |
| 1863 | result.overflow = (lhsp != num_positive (rhs, precision) |
| 1864 | && lhsp != num_positive (result, precision)); |
| 1865 | } |
| 1866 | return result; |
| 1867 | |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1868 | case CPP_PLUS: |
| 1869 | result.low = lhs.low + rhs.low; |
| 1870 | result.high = lhs.high + rhs.high; |
| 1871 | if (result.low < lhs.low) |
| 1872 | result.high++; |
Diego Novillo | 6de9cd9 | 2004-05-13 02:41:07 -0400 | [diff] [blame] | 1873 | result.unsignedp = lhs.unsignedp || rhs.unsignedp; |
| 1874 | result.overflow = false; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1875 | |
| 1876 | result = num_trim (result, precision); |
Diego Novillo | 6de9cd9 | 2004-05-13 02:41:07 -0400 | [diff] [blame] | 1877 | if (!result.unsignedp) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1878 | { |
| 1879 | bool lhsp = num_positive (lhs, precision); |
| 1880 | result.overflow = (lhsp == num_positive (rhs, precision) |
| 1881 | && lhsp != num_positive (result, precision)); |
| 1882 | } |
| 1883 | return result; |
| 1884 | |
| 1885 | /* Comma. */ |
| 1886 | default: /* case CPP_COMMA: */ |
Joseph Myers | 32e8aa9 | 2004-02-11 23:50:45 +0000 | [diff] [blame] | 1887 | if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99) |
| 1888 | || !pfile->state.skip_eval)) |
Manuel López-Ibáñez | 2b71f4a | 2014-09-04 15:13:40 +0000 | [diff] [blame] | 1889 | cpp_pedwarning (pfile, CPP_W_PEDANTIC, |
| 1890 | "comma operator in operand of #if"); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1891 | lhs = rhs; |
| 1892 | break; |
| 1893 | } |
| 1894 | |
| 1895 | return lhs; |
| 1896 | } |
| 1897 | |
| 1898 | /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This |
| 1899 | cannot overflow. */ |
| 1900 | static cpp_num |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1901 | num_part_mul (cpp_num_part lhs, cpp_num_part rhs) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1902 | { |
| 1903 | cpp_num result; |
| 1904 | cpp_num_part middle[2], temp; |
| 1905 | |
| 1906 | result.low = LOW_PART (lhs) * LOW_PART (rhs); |
| 1907 | result.high = HIGH_PART (lhs) * HIGH_PART (rhs); |
| 1908 | |
| 1909 | middle[0] = LOW_PART (lhs) * HIGH_PART (rhs); |
| 1910 | middle[1] = HIGH_PART (lhs) * LOW_PART (rhs); |
| 1911 | |
| 1912 | temp = result.low; |
| 1913 | result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2); |
| 1914 | if (result.low < temp) |
| 1915 | result.high++; |
| 1916 | |
| 1917 | temp = result.low; |
| 1918 | result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2); |
| 1919 | if (result.low < temp) |
| 1920 | result.high++; |
| 1921 | |
| 1922 | result.high += HIGH_PART (middle[0]); |
| 1923 | result.high += HIGH_PART (middle[1]); |
Diego Novillo | 6de9cd9 | 2004-05-13 02:41:07 -0400 | [diff] [blame] | 1924 | result.unsignedp = true; |
| 1925 | result.overflow = false; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1926 | |
| 1927 | return result; |
| 1928 | } |
| 1929 | |
| 1930 | /* Multiply two preprocessing numbers. */ |
| 1931 | static cpp_num |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1932 | num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1933 | { |
| 1934 | cpp_num result, temp; |
| 1935 | bool unsignedp = lhs.unsignedp || rhs.unsignedp; |
| 1936 | bool overflow, negate = false; |
| 1937 | size_t precision = CPP_OPTION (pfile, precision); |
| 1938 | |
| 1939 | /* Prepare for unsigned multiplication. */ |
| 1940 | if (!unsignedp) |
| 1941 | { |
| 1942 | if (!num_positive (lhs, precision)) |
| 1943 | negate = !negate, lhs = num_negate (lhs, precision); |
| 1944 | if (!num_positive (rhs, precision)) |
| 1945 | negate = !negate, rhs = num_negate (rhs, precision); |
| 1946 | } |
| 1947 | |
| 1948 | overflow = lhs.high && rhs.high; |
| 1949 | result = num_part_mul (lhs.low, rhs.low); |
| 1950 | |
| 1951 | temp = num_part_mul (lhs.high, rhs.low); |
| 1952 | result.high += temp.low; |
| 1953 | if (temp.high) |
| 1954 | overflow = true; |
| 1955 | |
| 1956 | temp = num_part_mul (lhs.low, rhs.high); |
| 1957 | result.high += temp.low; |
| 1958 | if (temp.high) |
| 1959 | overflow = true; |
| 1960 | |
| 1961 | temp.low = result.low, temp.high = result.high; |
| 1962 | result = num_trim (result, precision); |
| 1963 | if (!num_eq (result, temp)) |
| 1964 | overflow = true; |
| 1965 | |
| 1966 | if (negate) |
| 1967 | result = num_negate (result, precision); |
| 1968 | |
| 1969 | if (unsignedp) |
| 1970 | result.overflow = false; |
| 1971 | else |
| 1972 | result.overflow = overflow || (num_positive (result, precision) ^ !negate |
| 1973 | && !num_zerop (result)); |
| 1974 | result.unsignedp = unsignedp; |
| 1975 | |
| 1976 | return result; |
| 1977 | } |
| 1978 | |
Manuel López-Ibáñez | b506a5a | 2009-06-18 15:10:23 +0000 | [diff] [blame] | 1979 | /* Divide two preprocessing numbers, LHS and RHS, returning the answer |
| 1980 | or the remainder depending upon OP. LOCATION is the source location |
| 1981 | of this operator (for diagnostics). */ |
| 1982 | |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1983 | static cpp_num |
Manuel López-Ibáñez | b506a5a | 2009-06-18 15:10:23 +0000 | [diff] [blame] | 1984 | num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op, |
| 1985 | source_location location) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1986 | { |
| 1987 | cpp_num result, sub; |
| 1988 | cpp_num_part mask; |
| 1989 | bool unsignedp = lhs.unsignedp || rhs.unsignedp; |
| 1990 | bool negate = false, lhs_neg = false; |
| 1991 | size_t i, precision = CPP_OPTION (pfile, precision); |
| 1992 | |
| 1993 | /* Prepare for unsigned division. */ |
| 1994 | if (!unsignedp) |
| 1995 | { |
| 1996 | if (!num_positive (lhs, precision)) |
| 1997 | negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision); |
| 1998 | if (!num_positive (rhs, precision)) |
| 1999 | negate = !negate, rhs = num_negate (rhs, precision); |
| 2000 | } |
| 2001 | |
| 2002 | /* Find the high bit. */ |
| 2003 | if (rhs.high) |
| 2004 | { |
| 2005 | i = precision - 1; |
Neil Booth | 359b0be | 2002-05-28 05:44:06 +0000 | [diff] [blame] | 2006 | mask = (cpp_num_part) 1 << (i - PART_PRECISION); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 2007 | for (; ; i--, mask >>= 1) |
| 2008 | if (rhs.high & mask) |
| 2009 | break; |
| 2010 | } |
| 2011 | else if (rhs.low) |
| 2012 | { |
| 2013 | if (precision > PART_PRECISION) |
| 2014 | i = precision - PART_PRECISION - 1; |
| 2015 | else |
| 2016 | i = precision - 1; |
Neil Booth | 359b0be | 2002-05-28 05:44:06 +0000 | [diff] [blame] | 2017 | mask = (cpp_num_part) 1 << i; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 2018 | for (; ; i--, mask >>= 1) |
| 2019 | if (rhs.low & mask) |
| 2020 | break; |
| 2021 | } |
| 2022 | else |
| 2023 | { |
Neil Booth | 75aef48 | 2002-07-19 19:24:43 +0000 | [diff] [blame] | 2024 | if (!pfile->state.skip_eval) |
Manuel López-Ibáñez | b506a5a | 2009-06-18 15:10:23 +0000 | [diff] [blame] | 2025 | cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, |
| 2026 | "division by zero in #if"); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 2027 | return lhs; |
| 2028 | } |
| 2029 | |
Kazu Hirata | da7d830 | 2002-09-22 02:03:17 +0000 | [diff] [blame] | 2030 | /* First nonzero bit of RHS is bit I. Do naive division by |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 2031 | shifting the RHS fully left, and subtracting from LHS if LHS is |
| 2032 | at least as big, and then repeating but with one less shift. |
| 2033 | This is not very efficient, but is easy to understand. */ |
| 2034 | |
| 2035 | rhs.unsignedp = true; |
| 2036 | lhs.unsignedp = true; |
| 2037 | i = precision - i - 1; |
| 2038 | sub = num_lshift (rhs, precision, i); |
| 2039 | |
| 2040 | result.high = result.low = 0; |
| 2041 | for (;;) |
| 2042 | { |
| 2043 | if (num_greater_eq (lhs, sub, precision)) |
| 2044 | { |
| 2045 | lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS); |
| 2046 | if (i >= PART_PRECISION) |
Neil Booth | 359b0be | 2002-05-28 05:44:06 +0000 | [diff] [blame] | 2047 | result.high |= (cpp_num_part) 1 << (i - PART_PRECISION); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 2048 | else |
Neil Booth | 359b0be | 2002-05-28 05:44:06 +0000 | [diff] [blame] | 2049 | result.low |= (cpp_num_part) 1 << i; |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 2050 | } |
| 2051 | if (i-- == 0) |
| 2052 | break; |
| 2053 | sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1)); |
| 2054 | sub.high >>= 1; |
| 2055 | } |
| 2056 | |
| 2057 | /* We divide so that the remainder has the sign of the LHS. */ |
| 2058 | if (op == CPP_DIV) |
| 2059 | { |
| 2060 | result.unsignedp = unsignedp; |
Diego Novillo | 6de9cd9 | 2004-05-13 02:41:07 -0400 | [diff] [blame] | 2061 | result.overflow = false; |
| 2062 | if (!unsignedp) |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 2063 | { |
| 2064 | if (negate) |
| 2065 | result = num_negate (result, precision); |
Eric Christopher | 22a8a52 | 2007-05-02 21:57:50 +0000 | [diff] [blame] | 2066 | result.overflow = (num_positive (result, precision) ^ !negate |
| 2067 | && !num_zerop (result)); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | return result; |
| 2071 | } |
| 2072 | |
| 2073 | /* CPP_MOD. */ |
| 2074 | lhs.unsignedp = unsignedp; |
| 2075 | lhs.overflow = false; |
| 2076 | if (lhs_neg) |
| 2077 | lhs = num_negate (lhs, precision); |
| 2078 | |
| 2079 | return lhs; |
| 2080 | } |
Edward Smith-Rowland | a15f7cb | 2014-10-01 11:49:23 +0000 | [diff] [blame] | 2081 | |
| 2082 | /* Handle meeting "__has_include__" in a preprocessor expression. */ |
| 2083 | static cpp_num |
| 2084 | parse_has_include (cpp_reader *pfile, enum include_type type) |
| 2085 | { |
| 2086 | cpp_num result; |
| 2087 | bool paren = false; |
| 2088 | cpp_hashnode *node = 0; |
| 2089 | const cpp_token *token; |
| 2090 | bool bracket = false; |
| 2091 | char *fname = 0; |
| 2092 | |
| 2093 | result.unsignedp = false; |
| 2094 | result.high = 0; |
| 2095 | result.overflow = false; |
| 2096 | result.low = 0; |
| 2097 | |
| 2098 | pfile->state.in__has_include__++; |
| 2099 | |
| 2100 | token = cpp_get_token (pfile); |
| 2101 | if (token->type == CPP_OPEN_PAREN) |
| 2102 | { |
| 2103 | paren = true; |
| 2104 | token = cpp_get_token (pfile); |
| 2105 | } |
| 2106 | |
| 2107 | if (token->type == CPP_STRING || token->type == CPP_HEADER_NAME) |
| 2108 | { |
| 2109 | if (token->type == CPP_HEADER_NAME) |
| 2110 | bracket = true; |
| 2111 | fname = XNEWVEC (char, token->val.str.len - 1); |
| 2112 | memcpy (fname, token->val.str.text + 1, token->val.str.len - 2); |
| 2113 | fname[token->val.str.len - 2] = '\0'; |
| 2114 | node = token->val.node.node; |
| 2115 | } |
| 2116 | else if (token->type == CPP_LESS) |
| 2117 | { |
| 2118 | bracket = true; |
| 2119 | fname = _cpp_bracket_include (pfile); |
| 2120 | } |
| 2121 | else |
| 2122 | cpp_error (pfile, CPP_DL_ERROR, |
| 2123 | "operator \"__has_include__\" requires a header string"); |
| 2124 | |
| 2125 | if (fname) |
| 2126 | { |
| 2127 | int angle_brackets = (bracket ? 1 : 0); |
| 2128 | |
| 2129 | if (_cpp_has_header (pfile, fname, angle_brackets, type)) |
| 2130 | result.low = 1; |
| 2131 | else |
| 2132 | result.low = 0; |
| 2133 | |
| 2134 | XDELETEVEC (fname); |
| 2135 | } |
| 2136 | |
| 2137 | if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN) |
| 2138 | cpp_error (pfile, CPP_DL_ERROR, |
| 2139 | "missing ')' after \"__has_include__\""); |
| 2140 | |
| 2141 | /* A possible controlling macro of the form #if !__has_include__ (). |
| 2142 | _cpp_parse_expr checks there was no other junk on the line. */ |
| 2143 | if (node) |
| 2144 | pfile->mi_ind_cmacro = node; |
| 2145 | |
| 2146 | pfile->state.in__has_include__--; |
| 2147 | |
| 2148 | return result; |
| 2149 | } |