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