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