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