]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cppexp.c
237b8e686a0ab9fa6dae7048f03f96b36b582771
[thirdparty/gcc.git] / gcc / cppexp.c
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 92, 94, 95, 97, 98, 1999, 2000 Free Software Foundation.
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding!
22
23 Written by Per Bothner 1994. */
24
25 /* Parse a C expression from text in a string */
26
27 #include "config.h"
28 #include "system.h"
29 #include "cpplib.h"
30 #include "cpphash.h"
31
32 #ifndef CHAR_TYPE_SIZE
33 #define CHAR_TYPE_SIZE BITS_PER_UNIT
34 #endif
35
36 #ifndef INT_TYPE_SIZE
37 #define INT_TYPE_SIZE BITS_PER_WORD
38 #endif
39
40 #ifndef LONG_TYPE_SIZE
41 #define LONG_TYPE_SIZE BITS_PER_WORD
42 #endif
43
44 #ifndef WCHAR_TYPE_SIZE
45 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
46 #endif
47
48 #ifndef MAX_CHAR_TYPE_SIZE
49 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
50 #endif
51
52 #ifndef MAX_INT_TYPE_SIZE
53 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
54 #endif
55
56 #ifndef MAX_LONG_TYPE_SIZE
57 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
58 #endif
59
60 #ifndef MAX_WCHAR_TYPE_SIZE
61 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
62 #endif
63
64 #define MAX_CHAR_TYPE_MASK (MAX_CHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
65 ? (~(~(HOST_WIDEST_INT) 0 << MAX_CHAR_TYPE_SIZE)) \
66 : ~ (HOST_WIDEST_INT) 0)
67
68 #define MAX_WCHAR_TYPE_MASK (MAX_WCHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
69 ? ~(~(HOST_WIDEST_INT) 0 << MAX_WCHAR_TYPE_SIZE) \
70 : ~ (HOST_WIDEST_INT) 0)
71
72 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
73 number with SUM's sign, where A, B, and SUM are all C integers. */
74 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
75
76 typedef short op_t;
77
78 static void integer_overflow PARAMS ((cpp_reader *));
79 static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
80 unsigned int,
81 unsigned HOST_WIDEST_INT));
82 static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
83 unsigned int,
84 unsigned HOST_WIDEST_INT));
85 static struct operation parse_number PARAMS ((cpp_reader *, U_CHAR *,
86 U_CHAR *));
87 static struct operation parse_charconst PARAMS ((cpp_reader *, U_CHAR *,
88 U_CHAR *));
89 static struct operation parse_defined PARAMS ((cpp_reader *));
90 static HOST_WIDEST_INT parse_escape PARAMS ((cpp_reader *, U_CHAR **,
91 HOST_WIDEST_INT));
92 static struct operation lex PARAMS ((cpp_reader *, int));
93 static const char * op_to_str PARAMS ((op_t, char *));
94
95 #define ERROR 299
96 #define OROR 300
97 #define ANDAND 301
98 #define EQUAL 302
99 #define NOTEQUAL 303
100 #define LEQ 304
101 #define GEQ 305
102 #define LSH 306
103 #define RSH 307
104 #define NAME 308
105 #define INT 309
106 #define CHAR 310
107 #define FINISHED 311
108
109 struct operation
110 {
111 op_t op;
112 U_CHAR prio; /* Priority of op. */
113 U_CHAR flags;
114 U_CHAR unsignedp; /* True if value should be treated as unsigned. */
115 HOST_WIDEST_INT value; /* The value logically "right" of op. */
116 };
117
118 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
119 with or without size suffixes. */
120
121 static struct operation
122 parse_number (pfile, start, end)
123 cpp_reader *pfile;
124 U_CHAR *start;
125 U_CHAR *end;
126 {
127 struct operation op;
128 U_CHAR *p = start;
129 int c;
130 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
131 int base = 10;
132 int overflow = 0;
133 int digit, largest_digit = 0;
134 int spec_long = 0;
135
136 op.unsignedp = 0;
137
138 if (p[0] == '0')
139 {
140 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
141 {
142 p += 2;
143 base = 16;
144 }
145 else
146 {
147 p += 1;
148 base = 8;
149 }
150 }
151
152 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
153 MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
154 / ((unsigned HOST_WIDEST_INT) base));
155
156 while (p < end)
157 {
158 c = *p++;
159
160 if (c >= '0' && c <= '9')
161 digit = c - '0';
162 /* FIXME: assumes ASCII */
163 else if (base == 16 && c >= 'a' && c <= 'f')
164 digit = c - 'a' + 10;
165 else if (base == 16 && c >= 'A' && c <= 'F')
166 digit = c - 'A' + 10;
167 else if (c == '.')
168 {
169 /* It's a float since it contains a point. */
170 cpp_error (pfile,
171 "floating point numbers are not allowed in #if expressions");
172 goto error;
173 }
174 else
175 {
176 /* `l' means long, and `u' means unsigned. */
177 for (;;)
178 {
179 if (c == 'l' || c == 'L')
180 spec_long++;
181 else if (c == 'u' || c == 'U')
182 op.unsignedp++;
183 else
184 {
185 /* Decrement p here so that the error for an invalid
186 number will be generated below in the case where
187 this is the last character in the buffer. */
188 p--;
189 break;
190 }
191 if (p == end)
192 break;
193 c = *p++;
194 }
195 /* Don't look for any more digits after the suffixes. */
196 break;
197 }
198
199 if (largest_digit < digit)
200 largest_digit = digit;
201 nd = n * base + digit;
202 overflow |= MAX_over_base < n || nd < n;
203 n = nd;
204 }
205
206 if (p != end)
207 {
208 cpp_error (pfile, "invalid number in #if expression");
209 goto error;
210 }
211 else if (spec_long > (CPP_OPTION (pfile, c89) ? 1 : 2))
212 {
213 cpp_error (pfile, "too many 'l' suffixes in integer constant");
214 goto error;
215 }
216 else if (op.unsignedp > 1)
217 {
218 cpp_error (pfile, "too many 'u' suffixes in integer constant");
219 goto error;
220 }
221
222 if (base <= largest_digit)
223 cpp_pedwarn (pfile,
224 "integer constant contains digits beyond the radix");
225
226 if (overflow)
227 cpp_pedwarn (pfile, "integer constant out of range");
228
229 /* If too big to be signed, consider it unsigned. */
230 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
231 {
232 if (base == 10)
233 cpp_warning (pfile,
234 "integer constant is so large that it is unsigned");
235 op.unsignedp = 1;
236 }
237
238 op.value = n;
239 op.op = INT;
240 return op;
241
242 error:
243 op.op = ERROR;
244 return op;
245 }
246
247 /* Parse and convert a character constant for #if. Understands backslash
248 escapes (\n, \031) and multibyte characters (if so configured). */
249 static struct operation
250 parse_charconst (pfile, start, end)
251 cpp_reader *pfile;
252 U_CHAR *start;
253 U_CHAR *end;
254 {
255 struct operation op;
256 HOST_WIDEST_INT result = 0;
257 int num_chars = 0;
258 int num_bits;
259 unsigned int width = MAX_CHAR_TYPE_SIZE, mask = MAX_CHAR_TYPE_MASK;
260 int max_chars;
261 U_CHAR *ptr = start;
262
263 int c = -1;
264
265 if (*ptr == 'L')
266 {
267 ++ptr;
268 width = MAX_WCHAR_TYPE_SIZE, mask = MAX_WCHAR_TYPE_MASK;
269 }
270 max_chars = MAX_LONG_TYPE_SIZE / width;
271
272 ++ptr; /* skip initial quote */
273
274 while (ptr < end)
275 {
276 c = *ptr++;
277 if (c == '\'')
278 break;
279 else if (c == '\\')
280 {
281 c = parse_escape (pfile, &ptr, mask);
282 if (width < HOST_BITS_PER_INT
283 && (unsigned int) c >= (unsigned int)(1 << width))
284 cpp_pedwarn (pfile,
285 "escape sequence out of range for character");
286 }
287
288 /* Merge character into result; ignore excess chars. */
289 if (++num_chars <= max_chars)
290 {
291 if (width < HOST_BITS_PER_INT)
292 result = (result << width) | (c & ((1 << width) - 1));
293 else
294 result = c;
295 }
296 }
297
298 if (num_chars == 0)
299 {
300 cpp_error (pfile, "empty character constant");
301 goto error;
302 }
303 else if (c != '\'')
304 {
305 /* cpp_get_token has already emitted an error if !traditional. */
306 if (! CPP_TRADITIONAL (pfile))
307 cpp_error (pfile, "malformatted character constant");
308 goto error;
309 }
310 else if (num_chars > max_chars)
311 {
312 cpp_error (pfile, "character constant too long");
313 goto error;
314 }
315 else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
316 cpp_warning (pfile, "multi-character character constant");
317
318 /* If char type is signed, sign-extend the constant. */
319 num_bits = num_chars * width;
320
321 if (cpp_defined (pfile, (const U_CHAR *)"__CHAR_UNSIGNED__",
322 sizeof ("__CHAR_UNSIGNED__")-1)
323 || ((result >> (num_bits - 1)) & 1) == 0)
324 op.value = result & ((unsigned HOST_WIDEST_INT) ~0
325 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
326 else
327 op.value = result | ~((unsigned HOST_WIDEST_INT) ~0
328 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
329
330 /* This is always a signed type. */
331 op.unsignedp = 0;
332 op.op = CHAR;
333 return op;
334
335 error:
336 op.op = ERROR;
337 return op;
338 }
339
340 static struct operation
341 parse_defined (pfile)
342 cpp_reader *pfile;
343 {
344 int paren = 0, len;
345 U_CHAR *tok;
346 enum cpp_ttype token;
347 struct operation op;
348 long old_written = CPP_WRITTEN (pfile);
349
350 op.unsignedp = 0;
351 op.op = INT;
352
353 pfile->no_macro_expand++;
354 token = _cpp_get_directive_token (pfile);
355 if (token == CPP_LPAREN)
356 {
357 paren++;
358 CPP_SET_WRITTEN (pfile, old_written);
359 token = _cpp_get_directive_token (pfile);
360 }
361
362 if (token != CPP_NAME)
363 goto oops;
364
365 tok = pfile->token_buffer + old_written;
366 len = CPP_PWRITTEN (pfile) - tok;
367 op.value = cpp_defined (pfile, tok, len);
368
369 if (paren)
370 {
371 if (_cpp_get_directive_token (pfile) != CPP_RPAREN)
372 goto oops;
373 }
374 CPP_SET_WRITTEN (pfile, old_written);
375 pfile->no_macro_expand--;
376 return op;
377
378 oops:
379 CPP_SET_WRITTEN (pfile, old_written);
380 pfile->no_macro_expand--;
381 cpp_error (pfile, "'defined' without an identifier");
382
383 op.op = ERROR;
384 return op;
385 }
386
387 struct token
388 {
389 const char *operator;
390 op_t token;
391 };
392
393 static const struct token tokentab2[] =
394 {
395 {"&&", ANDAND},
396 {"||", OROR},
397 {"<<", LSH},
398 {">>", RSH},
399 {"==", EQUAL},
400 {"!=", NOTEQUAL},
401 {"<=", LEQ},
402 {">=", GEQ},
403 {"++", ERROR},
404 {"--", ERROR},
405 {NULL, ERROR}
406 };
407
408 /* Read one token. */
409
410 static struct operation
411 lex (pfile, skip_evaluation)
412 cpp_reader *pfile;
413 int skip_evaluation;
414 {
415 const struct token *toktab;
416 enum cpp_ttype token;
417 struct operation op;
418 U_CHAR *tok_start, *tok_end;
419 long old_written;
420
421 old_written = CPP_WRITTEN (pfile);
422 token = _cpp_get_directive_token (pfile);
423
424 tok_start = pfile->token_buffer + old_written;
425 tok_end = CPP_PWRITTEN (pfile);
426 CPP_SET_WRITTEN (pfile, old_written);
427 switch (token)
428 {
429 case CPP_EOF: /* Should not happen ... */
430 case CPP_VSPACE:
431 op.op = 0;
432 return op;
433 case CPP_NUMBER:
434 return parse_number (pfile, tok_start, tok_end);
435 case CPP_STRING:
436 case CPP_WSTRING:
437 cpp_error (pfile,
438 "string constants are not allowed in #if expressions");
439 op.op = ERROR;
440 return op;
441
442 case CPP_CHAR:
443 case CPP_WCHAR:
444 return parse_charconst (pfile, tok_start, tok_end);
445
446 case CPP_NAME:
447 if (!strncmp (tok_start, "defined", 7))
448 return parse_defined (pfile);
449
450 op.op = INT;
451 op.unsignedp = 0;
452 op.value = 0;
453
454 if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
455 cpp_warning (pfile, "'%.*s' is not defined",
456 (int) (tok_end - tok_start), tok_start);
457 return op;
458
459 case CPP_ASSERTION:
460 op.op = INT;
461 op.unsignedp = 0;
462 op.value = cpp_defined (pfile, tok_start, tok_end - tok_start);
463 return op;
464
465 case CPP_OTHER:
466 /* See if it is a special token of length 2. */
467 if (tok_start + 2 == tok_end)
468 {
469 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
470 if (tok_start[0] == toktab->operator[0]
471 && tok_start[1] == toktab->operator[1])
472 break;
473 if (toktab->token == ERROR)
474 cpp_error (pfile, "'%s' not allowed in operand of #if",
475 tok_start);
476 op.op = toktab->token;
477 return op;
478 }
479 /* fall through */
480 default:
481 op.op = *tok_start;
482 return op;
483 }
484 }
485
486 /* Convert an operator ID to a string. BUFF is a buffer at least 5
487 characters long which might be used to store the string. */
488 /* XXX FIXME: Remove BUFF when new lexer is implemented. */
489 static const char *
490 op_to_str (op, buff)
491 op_t op;
492 char *buff;
493 {
494 const struct token *toktab;
495
496 /* See if it is a special token of length 2. */
497 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
498 if (op == toktab->token)
499 return toktab->operator;
500
501 if (ISGRAPH (op))
502 sprintf (buff, "%c", (int) op);
503 else
504 sprintf (buff, "\\%03o", (int) op);
505 return buff;
506 }
507
508 /* Parse a C escape sequence. STRING_PTR points to a variable
509 containing a pointer to the string to parse. That pointer
510 is updated past the characters we use. The value of the
511 escape sequence is returned.
512
513 A negative value means the sequence \ newline was seen,
514 which is supposed to be equivalent to nothing at all.
515
516 If \ is followed by a null character, we return a negative
517 value and leave the string pointer pointing at the null character.
518
519 If \ is followed by 000, we return 0 and leave the string pointer
520 after the zeros. A value of 0 does not mean end of string. */
521
522 static HOST_WIDEST_INT
523 parse_escape (pfile, string_ptr, result_mask)
524 cpp_reader *pfile;
525 U_CHAR **string_ptr;
526 HOST_WIDEST_INT result_mask;
527 {
528 register int c = *(*string_ptr)++;
529 switch (c)
530 {
531 case 'a':
532 return TARGET_BELL;
533 case 'b':
534 return TARGET_BS;
535 case 'e':
536 case 'E':
537 if (CPP_PEDANTIC (pfile))
538 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, '\\%c'", c);
539 return TARGET_ESC;
540 case 'f':
541 return TARGET_FF;
542 case 'n':
543 return TARGET_NEWLINE;
544 case 'r':
545 return TARGET_CR;
546 case 't':
547 return TARGET_TAB;
548 case 'v':
549 return TARGET_VT;
550 case '\n':
551 return -2;
552 case 0:
553 (*string_ptr)--;
554 return 0;
555
556 case '0':
557 case '1':
558 case '2':
559 case '3':
560 case '4':
561 case '5':
562 case '6':
563 case '7':
564 {
565 register HOST_WIDEST_INT i = c - '0';
566 register int count = 0;
567 while (++count < 3)
568 {
569 c = *(*string_ptr)++;
570 if (c >= '0' && c <= '7')
571 i = (i << 3) + c - '0';
572 else
573 {
574 (*string_ptr)--;
575 break;
576 }
577 }
578 if (i != (i & result_mask))
579 {
580 i &= result_mask;
581 cpp_pedwarn (pfile, "octal escape sequence out of range");
582 }
583 return i;
584 }
585 case 'x':
586 {
587 register unsigned HOST_WIDEST_INT i = 0, overflow = 0;
588 register int digits_found = 0, digit;
589 for (;;)
590 {
591 c = *(*string_ptr)++;
592 if (c >= '0' && c <= '9')
593 digit = c - '0';
594 else if (c >= 'a' && c <= 'f')
595 digit = c - 'a' + 10;
596 else if (c >= 'A' && c <= 'F')
597 digit = c - 'A' + 10;
598 else
599 {
600 (*string_ptr)--;
601 break;
602 }
603 overflow |= i ^ (i << 4 >> 4);
604 i = (i << 4) + digit;
605 digits_found = 1;
606 }
607 if (!digits_found)
608 cpp_error (pfile, "\\x used with no following hex digits");
609 if (overflow | (i != (i & result_mask)))
610 {
611 i &= result_mask;
612 cpp_pedwarn (pfile, "hex escape sequence out of range");
613 }
614 return i;
615 }
616 default:
617 return c;
618 }
619 }
620
621 static void
622 integer_overflow (pfile)
623 cpp_reader *pfile;
624 {
625 if (CPP_PEDANTIC (pfile))
626 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
627 }
628
629 static HOST_WIDEST_INT
630 left_shift (pfile, a, unsignedp, b)
631 cpp_reader *pfile;
632 HOST_WIDEST_INT a;
633 unsigned int unsignedp;
634 unsigned HOST_WIDEST_INT b;
635 {
636 if (b >= HOST_BITS_PER_WIDEST_INT)
637 {
638 if (! unsignedp && a != 0)
639 integer_overflow (pfile);
640 return 0;
641 }
642 else if (unsignedp)
643 return (unsigned HOST_WIDEST_INT) a << b;
644 else
645 {
646 HOST_WIDEST_INT l = a << b;
647 if (l >> b != a)
648 integer_overflow (pfile);
649 return l;
650 }
651 }
652
653 static HOST_WIDEST_INT
654 right_shift (pfile, a, unsignedp, b)
655 cpp_reader *pfile ATTRIBUTE_UNUSED;
656 HOST_WIDEST_INT a;
657 unsigned int unsignedp;
658 unsigned HOST_WIDEST_INT b;
659 {
660 if (b >= HOST_BITS_PER_WIDEST_INT)
661 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
662 else if (unsignedp)
663 return (unsigned HOST_WIDEST_INT) a >> b;
664 else
665 return a >> b;
666 }
667 \f
668 /* Operator precedence and flags table.
669
670 After an operator is returned from the lexer, if it has priority less
671 than or equal to the operator on the top of the stack, we reduce the
672 stack by one operator and repeat the test. Since equal priorities
673 reduce, this is naturally left-associative.
674
675 We handle right-associative operators by clearing the lower bit of all
676 left-associative operators, and setting it for right-associative ones.
677 After the reduction phase of a new operator, just before it is pushed
678 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
679 during the reduction phase, the current right-associative operator has
680 a priority one greater than any other operator of otherwise equal
681 precedence that has been pushed on the top of the stack. This avoids
682 a reduction pass, and effectively makes the logic right-associative.
683
684 The remaining cases are '(' and ')'. We handle '(' by skipping the
685 reduction phase completely. ')' is given lower priority than
686 everything else, including '(', effectively forcing a reduction of the
687 parenthesised expression. If there is no matching '(', the stack will
688 be reduced all the way to the beginning, exiting the parser in the
689 same way as the ultra-low priority end-of-expression dummy operator.
690 The exit code checks to see if the operator that caused it is ')', and
691 if so outputs an appropriate error message.
692
693 The parser assumes all shifted operators require a right operand
694 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
695 These semantics are automatically checked, any extra semantics need to
696 be handled with operator-specific code. */
697
698 #define FLAG_BITS 8
699 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
700 #define PRIO_SHIFT (FLAG_BITS + 1)
701 #define EXTRACT_PRIO(cnst) (cnst >> FLAG_BITS)
702 #define EXTRACT_FLAGS(cnst) (cnst & FLAG_MASK)
703
704 /* Flags. */
705 #define HAVE_VALUE (1 << 0)
706 #define NO_L_OPERAND (1 << 1)
707 #define NO_R_OPERAND (1 << 2)
708 #define SHORT_CIRCUIT (1 << 3)
709
710 /* Priority and flag combinations. */
711 #define RIGHT_ASSOC (1 << FLAG_BITS)
712 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
713 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
714 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
715 #define COMMA_PRIO (3 << PRIO_SHIFT)
716 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
717 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
718 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
719 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
720 #define OR_PRIO (8 << PRIO_SHIFT)
721 #define XOR_PRIO (9 << PRIO_SHIFT)
722 #define AND_PRIO (10 << PRIO_SHIFT)
723 #define EQUAL_PRIO (11 << PRIO_SHIFT)
724 #define LESS_PRIO (12 << PRIO_SHIFT)
725 #define SHIFT_PRIO (13 << PRIO_SHIFT)
726 #define PLUS_PRIO (14 << PRIO_SHIFT)
727 #define MUL_PRIO (15 << PRIO_SHIFT)
728 #define UNARY_PRIO ((16 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
729
730 #define COMPARE(OP) \
731 top->unsignedp = 0; \
732 top->value = (unsigned1 | unsigned2) \
733 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
734 : (v1 OP v2)
735 #define EQUALITY(OP) \
736 top->value = v1 OP v2; \
737 top->unsignedp = 0;
738 #define LOGICAL(OP) \
739 top->value = v1 OP v2; \
740 top->unsignedp = unsigned1 | unsigned2;
741
742 /* With -O2, gcc appears to produce nice code, moving the error
743 message load and subsequent jump completely out of the main path. */
744 #define CPP_ICE(msgid) \
745 do { cpp_ice (pfile, msgid); goto syntax_error; } while(0)
746 #define SYNTAX_ERROR(msgid) \
747 do { cpp_error (pfile, msgid); goto syntax_error; } while(0)
748 #define SYNTAX_ERROR2(msgid, arg) \
749 do { cpp_error (pfile, msgid, arg); goto syntax_error; } while(0)
750
751 /* Parse and evaluate a C expression, reading from PFILE.
752 Returns the truth value of the expression. */
753
754 int
755 _cpp_parse_expr (pfile)
756 cpp_reader *pfile;
757 {
758 /* The implementation is an operator precedence parser, i.e. a
759 bottom-up parser, using a stack for not-yet-reduced tokens.
760
761 The stack base is 'stack', and the current stack pointer is 'top'.
762 There is a stack element for each operator (only),
763 and the most recently pushed operator is 'top->op'.
764 An operand (value) is stored in the 'value' field of the stack
765 element of the operator that precedes it.
766 In that case the 'flags' field has the HAVE_VALUE flag set. */
767
768 #define INIT_STACK_SIZE 20
769 struct operation init_stack[INIT_STACK_SIZE];
770 struct operation *stack = init_stack;
771 struct operation *limit = stack + INIT_STACK_SIZE;
772 register struct operation *top = stack + 1;
773 long old_written = CPP_WRITTEN (pfile);
774 int skip_evaluation = 0;
775 int result;
776 char buff[5];
777
778 pfile->parsing_if_directive++;
779 /* We've finished when we try to reduce this. */
780 top->op = FINISHED;
781 /* Nifty way to catch missing '('. */
782 top->prio = EXTRACT_PRIO(CLOSE_PAREN_PRIO);
783 /* Avoid missing right operand checks. */
784 top->flags = NO_R_OPERAND;
785
786 for (;;)
787 {
788 unsigned int prio;
789 unsigned int flags;
790 struct operation op;
791
792 /* Read a token */
793 op = lex (pfile, skip_evaluation);
794
795 /* If the token is an operand, push its value and get next
796 token. If it is an operator, get its priority and flags, and
797 try to reduce the expression on the stack. */
798 switch (op.op)
799 {
800 case NAME:
801 CPP_ICE ("lex returns a NAME");
802 case ERROR:
803 goto syntax_error;
804 case '#':
805 /* We get '#' when get_directive_token hits a syntactically
806 invalid assertion predicate. _cpp_parse_assertion has
807 already issued an error. */
808 goto syntax_error;
809 default:
810 SYNTAX_ERROR ("invalid character in #if");
811
812 push_immediate:
813 case INT:
814 case CHAR:
815 /* Push a value onto the stack. */
816 if (top->flags & HAVE_VALUE)
817 SYNTAX_ERROR ("missing binary operator");
818 top->value = op.value;
819 top->unsignedp = op.unsignedp;
820 top->flags |= HAVE_VALUE;
821 continue;
822
823 case '+':
824 case '-': prio = PLUS_PRIO; if (top->flags & HAVE_VALUE) break;
825 /* else unary; fall through */
826 case '!':
827 case '~': prio = UNARY_PRIO; break;
828
829 case '*':
830 case '/':
831 case '%': prio = MUL_PRIO; break;
832 case '<':
833 case '>':
834 case LEQ:
835 case GEQ: prio = LESS_PRIO; break;
836 case NOTEQUAL:
837 case EQUAL: prio = EQUAL_PRIO; break;
838 case LSH:
839 case RSH: prio = SHIFT_PRIO; break;
840 case '&': prio = AND_PRIO; break;
841 case '^': prio = XOR_PRIO; break;
842 case '|': prio = OR_PRIO; break;
843 case ANDAND: prio = ANDAND_PRIO; break;
844 case OROR: prio = OROR_PRIO; break;
845 case ',': prio = COMMA_PRIO; break;
846 case '(': prio = OPEN_PAREN_PRIO; break;
847 case ')': prio = CLOSE_PAREN_PRIO; break;
848 case ':': prio = COLON_PRIO; break;
849 case '?': prio = COND_PRIO; break;
850 case 0: prio = FORCE_REDUCE_PRIO; break;
851 }
852
853 /* Separate the operator's code into priority and flags. */
854 flags = EXTRACT_FLAGS(prio);
855 prio = EXTRACT_PRIO(prio);
856 if (op.op == '(')
857 goto skip_reduction;
858
859 /* Check for reductions. Then push the operator. */
860 while (prio <= top->prio)
861 {
862 HOST_WIDEST_INT v1, v2;
863 unsigned int unsigned1, unsigned2;
864
865 /* Most operators that can appear on the stack require a
866 right operand. Check this before trying to reduce. */
867 if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0)
868 {
869 if (top->op == '(')
870 SYNTAX_ERROR ("void expression between '(' and ')'");
871 else
872 SYNTAX_ERROR2 ("operator '%s' has no right operand",
873 op_to_str (top->op, buff));
874 }
875
876 unsigned2 = top->unsignedp, v2 = top->value;
877 top--;
878 unsigned1 = top->unsignedp, v1 = top->value;
879
880 /* Now set top->value = (top[1].op)(v1, v2); */
881 switch (top[1].op)
882 {
883 case '+':
884 if (!(top->flags & HAVE_VALUE))
885 { /* Unary '+' */
886 top->value = v2;
887 top->unsignedp = unsigned2;
888 top->flags |= HAVE_VALUE;
889 }
890 else
891 {
892 top->value = v1 + v2;
893 top->unsignedp = unsigned1 | unsigned2;
894 if (! top->unsignedp && ! skip_evaluation
895 && ! possible_sum_sign (v1, v2, top->value))
896 integer_overflow (pfile);
897 }
898 break;
899 case '-':
900 if (!(top->flags & HAVE_VALUE))
901 { /* Unary '-' */
902 top->value = - v2;
903 if (!skip_evaluation && (top->value & v2) < 0
904 && !unsigned2)
905 integer_overflow (pfile);
906 top->unsignedp = unsigned2;
907 top->flags |= HAVE_VALUE;
908 }
909 else
910 { /* Binary '-' */
911 top->value = v1 - v2;
912 top->unsignedp = unsigned1 | unsigned2;
913 if (! top->unsignedp && ! skip_evaluation
914 && ! possible_sum_sign (top->value, v2, v1))
915 integer_overflow (pfile);
916 }
917 break;
918 case '*':
919 top->unsignedp = unsigned1 | unsigned2;
920 if (top->unsignedp)
921 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
922 else if (!skip_evaluation)
923 {
924 top->value = v1 * v2;
925 if (v1 && (top->value / v1 != v2
926 || (top->value & v1 & v2) < 0))
927 integer_overflow (pfile);
928 }
929 break;
930 case '/':
931 case '%':
932 if (skip_evaluation)
933 break;
934 if (v2 == 0)
935 SYNTAX_ERROR ("division by zero in #if");
936 top->unsignedp = unsigned1 | unsigned2;
937 if (top[1].op == '/')
938 {
939 if (top->unsignedp)
940 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
941 else
942 {
943 top->value = v1 / v2;
944 if ((top->value & v1 & v2) < 0)
945 integer_overflow (pfile);
946 }
947 }
948 else
949 {
950 if (top->unsignedp)
951 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
952 else
953 top->value = v1 % v2;
954 }
955 break;
956 case '!':
957 top->value = ! v2;
958 top->unsignedp = 0;
959 top->flags |= HAVE_VALUE;
960 break;
961 case '~':
962 top->value = ~ v2;
963 top->unsignedp = unsigned2;
964 top->flags |= HAVE_VALUE;
965 break;
966 case '<': COMPARE(<); break;
967 case '>': COMPARE(>); break;
968 case LEQ: COMPARE(<=); break;
969 case GEQ: COMPARE(>=); break;
970 case EQUAL: EQUALITY(==); break;
971 case NOTEQUAL: EQUALITY(!=); break;
972 case LSH:
973 if (skip_evaluation)
974 break;
975 top->unsignedp = unsigned1;
976 if (v2 < 0 && ! unsigned2)
977 top->value = right_shift (pfile, v1, unsigned1, -v2);
978 else
979 top->value = left_shift (pfile, v1, unsigned1, v2);
980 break;
981 case RSH:
982 if (skip_evaluation)
983 break;
984 top->unsignedp = unsigned1;
985 if (v2 < 0 && ! unsigned2)
986 top->value = left_shift (pfile, v1, unsigned1, -v2);
987 else
988 top->value = right_shift (pfile, v1, unsigned1, v2);
989 break;
990 case '&': LOGICAL(&); break;
991 case '^': LOGICAL(^); break;
992 case '|': LOGICAL(|); break;
993 case ANDAND:
994 top->value = v1 && v2; top->unsignedp = 0;
995 if (!v1) skip_evaluation--;
996 break;
997 case OROR:
998 top->value = v1 || v2; top->unsignedp = 0;
999 if (v1) skip_evaluation--;
1000 break;
1001 case ',':
1002 if (CPP_PEDANTIC (pfile))
1003 cpp_pedwarn (pfile, "comma operator in operand of #if");
1004 top->value = v2;
1005 top->unsignedp = unsigned2;
1006 break;
1007 case '?':
1008 SYNTAX_ERROR ("syntax error '?' without following ':'");
1009 case ':':
1010 if (top[0].op != '?')
1011 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
1012 top--;
1013 if (top->value) skip_evaluation--;
1014 top->value = top->value ? v1 : v2;
1015 top->unsignedp = unsigned1 | unsigned2;
1016 break;
1017 case '(':
1018 if (op.op != ')')
1019 SYNTAX_ERROR ("missing ')' in expression");
1020 op.value = v2;
1021 op.unsignedp = unsigned2;
1022 goto push_immediate;
1023 default:
1024 SYNTAX_ERROR2 ("unimplemented operator '%s'",
1025 op_to_str (top[1].op, buff));
1026 case FINISHED:
1027 /* Reducing this dummy operator indicates we've finished. */
1028 if (op.op == ')')
1029 SYNTAX_ERROR ("missing '(' in expression");
1030 goto done;
1031 }
1032 }
1033
1034 /* Handle short-circuit evaluations. */
1035 if (flags & SHORT_CIRCUIT)
1036 switch (op.op)
1037 {
1038 case OROR: if (top->value) skip_evaluation++; break;
1039 case ANDAND:
1040 case '?': if (!top->value) skip_evaluation++; break;
1041 case ':':
1042 if (top[-1].value) /* Was '?' condition true? */
1043 skip_evaluation++;
1044 else
1045 skip_evaluation--;
1046 }
1047
1048 skip_reduction:
1049 /* Check we have a left operand iff we need one. */
1050 if (flags & NO_L_OPERAND)
1051 {
1052 if (top->flags & HAVE_VALUE)
1053 SYNTAX_ERROR2 ("missing binary operator before '%s'",
1054 op_to_str (op.op, buff));
1055 }
1056 else
1057 {
1058 if (!(top->flags & HAVE_VALUE))
1059 SYNTAX_ERROR2 ("operator '%s' has no left operand",
1060 op_to_str (op.op, buff));
1061 }
1062
1063 /* Check for and handle stack overflow. */
1064 top++;
1065 if (top == limit)
1066 {
1067 struct operation *new_stack;
1068 int old_size = (char *) limit - (char *) stack;
1069 int new_size = 2 * old_size;
1070 if (stack != init_stack)
1071 new_stack = (struct operation *) xrealloc (stack, new_size);
1072 else
1073 {
1074 new_stack = (struct operation *) xmalloc (new_size);
1075 memcpy (new_stack, stack, old_size);
1076 }
1077 stack = new_stack;
1078 top = (struct operation *) ((char *) new_stack + old_size);
1079 limit = (struct operation *) ((char *) new_stack + new_size);
1080 }
1081
1082 top->flags = flags;
1083 top->prio = prio & ~EXTRACT_PRIO(RIGHT_ASSOC);
1084 top->op = op.op;
1085 }
1086
1087 done:
1088 result = (top[1].value != 0);
1089 if (top != stack)
1090 CPP_ICE ("unbalanced stack in #if expression");
1091 else if (!(top[1].flags & HAVE_VALUE))
1092 {
1093 SYNTAX_ERROR ("#if with no expression");
1094 syntax_error:
1095 _cpp_skip_rest_of_line (pfile);
1096 result = 0; /* Return 0 on syntax error. */
1097 }
1098
1099 /* Free dynamic stack if we allocated one. */
1100 if (stack != init_stack)
1101 free (stack);
1102 pfile->parsing_if_directive--;
1103 CPP_SET_WRITTEN (pfile, old_written);
1104 return result;
1105 }