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