1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 92, 94, 95, 97, 98, 1999, 2000 Free Software Foundation.
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
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.
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.
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!
23 Written by Per Bothner 1994. */
25 /* Parse a C expression from text in a string */
32 #ifndef CHAR_TYPE_SIZE
33 #define CHAR_TYPE_SIZE BITS_PER_UNIT
37 #define INT_TYPE_SIZE BITS_PER_WORD
40 #ifndef LONG_TYPE_SIZE
41 #define LONG_TYPE_SIZE BITS_PER_WORD
44 #ifndef WCHAR_TYPE_SIZE
45 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
48 #ifndef MAX_CHAR_TYPE_SIZE
49 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
52 #ifndef MAX_INT_TYPE_SIZE
53 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
56 #ifndef MAX_LONG_TYPE_SIZE
57 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
60 #ifndef MAX_WCHAR_TYPE_SIZE
61 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
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)
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)
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)
76 static void integer_overflow PARAMS ((cpp_reader *));
77 static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
79 unsigned HOST_WIDEST_INT));
80 static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
82 unsigned HOST_WIDEST_INT));
83 static struct operation parse_number PARAMS ((cpp_reader *, U_CHAR *,
85 static struct operation parse_charconst PARAMS ((cpp_reader *, U_CHAR *,
87 static struct operation parse_defined PARAMS ((cpp_reader *));
88 static HOST_WIDEST_INT parse_escape PARAMS ((cpp_reader *, U_CHAR **,
90 static struct operation lex PARAMS ((cpp_reader *, int));
106 #define LEFT_OPERAND_REQUIRED 1
107 #define RIGHT_OPERAND_REQUIRED 2
109 /* SKIP_OPERAND is set for '&&' '||' '?' and ':' when the
110 following operand should be short-circuited instead of evaluated. */
111 #define SKIP_OPERAND 8
116 U_CHAR rprio; /* Priority of op (relative to it right operand). */
118 U_CHAR unsignedp; /* true if value should be treated as unsigned */
119 HOST_WIDEST_INT value; /* The value logically "right" of op. */
122 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
123 with or without size suffixes. */
125 static struct operation
126 parse_number (pfile, start, end)
134 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
137 int digit, largest_digit = 0;
144 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
156 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
157 MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
158 / ((unsigned HOST_WIDEST_INT) base));
164 if (c >= '0' && c <= '9')
166 else if (base == 16 && c >= 'a' && c <= 'f') /* FIXME: assumes ASCII */
167 digit = c - 'a' + 10;
168 else if (base == 16 && c >= 'A' && c <= 'F')
169 digit = c - 'A' + 10;
172 /* It's a float since it contains a point. */
174 "floating point numbers are not allowed in #if expressions");
179 /* `l' means long, and `u' means unsigned. */
182 if (c == 'l' || c == 'L')
184 else if (c == 'u' || c == 'U')
188 /* Decrement p here so that the error for an invalid number
189 will be generated below in the case where this is the
190 last character in the buffer. */
198 /* Don't look for any more digits after the suffixes. */
202 if (largest_digit < digit)
203 largest_digit = digit;
204 nd = n * base + digit;
205 overflow |= MAX_over_base < n || nd < n;
211 cpp_error (pfile, "invalid number in #if expression");
214 else if (spec_long > (CPP_OPTIONS (pfile)->c89 ? 1 : 2))
216 cpp_error (pfile, "too many `l' suffixes in integer constant");
219 else if (op.unsignedp > 1)
221 cpp_error (pfile, "too many `u' suffixes in integer constant");
225 if (base <= largest_digit)
226 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
229 cpp_pedwarn (pfile, "integer constant out of range");
231 /* If too big to be signed, consider it unsigned. */
232 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
236 "integer constant is so large that it is unsigned");
249 /* Parse and convert a character constant for #if. Understands backslash
250 escapes (\n, \031) and multibyte characters (if so configured). */
251 static struct operation
252 parse_charconst (pfile, start, end)
258 HOST_WIDEST_INT result = 0;
261 unsigned int width = MAX_CHAR_TYPE_SIZE, mask = MAX_CHAR_TYPE_MASK;
270 width = MAX_WCHAR_TYPE_SIZE, mask = MAX_WCHAR_TYPE_MASK;
272 max_chars = MAX_LONG_TYPE_SIZE / width;
274 ++ptr; /* skip initial quote */
279 if (c == '\'' || c == '\0')
283 c = parse_escape (pfile, &ptr, mask);
284 if (width < HOST_BITS_PER_INT
285 && (unsigned int) c >= (unsigned int)(1 << width))
286 cpp_pedwarn (pfile, "escape sequence out of range for character");
289 /* Merge character into result; ignore excess chars. */
290 if (++num_chars <= max_chars)
292 if (width < HOST_BITS_PER_INT)
293 result = (result << width) | (c & ((1 << width) - 1));
301 cpp_error (pfile, "empty character constant");
306 /* cpp_get_token has already emitted an error if !traditional. */
307 if (! CPP_TRADITIONAL (pfile))
308 cpp_error (pfile, "malformatted character constant");
311 else if (num_chars > max_chars)
313 cpp_error (pfile, "character constant too long");
316 else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
317 cpp_warning (pfile, "multi-character character constant");
319 /* If char type is signed, sign-extend the constant. */
320 num_bits = num_chars * width;
322 if (cpp_defined (pfile, (const U_CHAR *)"__CHAR_UNSIGNED__",
323 sizeof ("__CHAR_UNSIGNED__")-1)
324 || ((result >> (num_bits - 1)) & 1) == 0)
325 op.value = result & ((unsigned HOST_WIDEST_INT) ~0
326 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
328 op.value = result | ~((unsigned HOST_WIDEST_INT) ~0
329 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
331 /* This is always a signed type. */
341 static struct operation
342 parse_defined (pfile)
347 enum cpp_token token;
349 long old_written = CPP_WRITTEN (pfile);
354 pfile->no_macro_expand++;
355 token = _cpp_get_directive_token (pfile);
356 if (token == CPP_LPAREN)
359 CPP_SET_WRITTEN (pfile, old_written);
360 token = _cpp_get_directive_token (pfile);
363 if (token != CPP_NAME)
366 tok = pfile->token_buffer + old_written;
367 len = CPP_PWRITTEN (pfile) - tok;
368 op.value = cpp_defined (pfile, tok, len);
372 if (_cpp_get_directive_token (pfile) != CPP_RPAREN)
375 CPP_SET_WRITTEN (pfile, old_written);
376 pfile->no_macro_expand--;
380 CPP_SET_WRITTEN (pfile, old_written);
381 pfile->no_macro_expand--;
382 cpp_error (pfile, "`defined' without an identifier");
390 const char *operator;
394 static const struct token tokentab2[] = {
408 /* Read one token. */
410 static struct operation
411 lex (pfile, skip_evaluation)
415 const struct token *toktab;
416 enum cpp_token token;
418 U_CHAR *tok_start, *tok_end;
421 old_written = CPP_WRITTEN (pfile);
422 token = _cpp_get_directive_token (pfile);
424 tok_start = pfile->token_buffer + old_written;
425 tok_end = CPP_PWRITTEN (pfile);
426 CPP_SET_WRITTEN (pfile, old_written);
429 case CPP_EOF: /* Should not happen ... */
434 return parse_number (pfile, tok_start, tok_end);
437 cpp_error (pfile, "string constants are not allowed in #if expressions");
443 return parse_charconst (pfile, tok_start, tok_end);
446 if (!strcmp (tok_start, "defined"))
447 return parse_defined (pfile);
453 if (CPP_OPTIONS (pfile)->warn_undef && !skip_evaluation)
454 cpp_warning (pfile, "`%.*s' is not defined",
455 (int) (tok_end - tok_start), tok_start);
461 op.value = cpp_defined (pfile, tok_start, tok_end - tok_start);
465 /* See if it is a special token of length 2. */
466 if (tok_start + 2 == tok_end)
468 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
469 if (tok_start[0] == toktab->operator[0]
470 && tok_start[1] == toktab->operator[1])
472 if (toktab->token == ERROR)
473 cpp_error (pfile, "`%s' not allowed in operand of `#if'",
475 op.op = toktab->token;
486 /* Parse a C escape sequence. STRING_PTR points to a variable
487 containing a pointer to the string to parse. That pointer
488 is updated past the characters we use. The value of the
489 escape sequence is returned.
491 A negative value means the sequence \ newline was seen,
492 which is supposed to be equivalent to nothing at all.
494 If \ is followed by a null character, we return a negative
495 value and leave the string pointer pointing at the null character.
497 If \ is followed by 000, we return 0 and leave the string pointer
498 after the zeros. A value of 0 does not mean end of string. */
500 static HOST_WIDEST_INT
501 parse_escape (pfile, string_ptr, result_mask)
504 HOST_WIDEST_INT result_mask;
506 register int c = *(*string_ptr)++;
515 if (CPP_OPTIONS (pfile)->pedantic)
516 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
521 return TARGET_NEWLINE;
543 register HOST_WIDEST_INT i = c - '0';
544 register int count = 0;
547 c = *(*string_ptr)++;
548 if (c >= '0' && c <= '7')
549 i = (i << 3) + c - '0';
556 if (i != (i & result_mask))
559 cpp_pedwarn (pfile, "octal escape sequence out of range");
565 register unsigned HOST_WIDEST_INT i = 0, overflow = 0;
566 register int digits_found = 0, digit;
569 c = *(*string_ptr)++;
570 if (c >= '0' && c <= '9')
572 else if (c >= 'a' && c <= 'f')
573 digit = c - 'a' + 10;
574 else if (c >= 'A' && c <= 'F')
575 digit = c - 'A' + 10;
581 overflow |= i ^ (i << 4 >> 4);
582 i = (i << 4) + digit;
586 cpp_error (pfile, "\\x used with no following hex digits");
587 if (overflow | (i != (i & result_mask)))
590 cpp_pedwarn (pfile, "hex escape sequence out of range");
600 integer_overflow (pfile)
603 if (CPP_PEDANTIC (pfile))
604 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
607 static HOST_WIDEST_INT
608 left_shift (pfile, a, unsignedp, b)
611 unsigned int unsignedp;
612 unsigned HOST_WIDEST_INT b;
614 if (b >= HOST_BITS_PER_WIDEST_INT)
616 if (! unsignedp && a != 0)
617 integer_overflow (pfile);
621 return (unsigned HOST_WIDEST_INT) a << b;
624 HOST_WIDEST_INT l = a << b;
626 integer_overflow (pfile);
631 static HOST_WIDEST_INT
632 right_shift (pfile, a, unsignedp, b)
633 cpp_reader *pfile ATTRIBUTE_UNUSED;
635 unsigned int unsignedp;
636 unsigned HOST_WIDEST_INT b;
638 if (b >= HOST_BITS_PER_WIDEST_INT)
639 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
641 return (unsigned HOST_WIDEST_INT) a >> b;
646 /* These priorities are all even, so we can handle associatively. */
647 #define PAREN_INNER_PRIO 0
649 #define COND_PRIO (COMMA_PRIO+2)
650 #define OROR_PRIO (COND_PRIO+2)
651 #define ANDAND_PRIO (OROR_PRIO+2)
652 #define OR_PRIO (ANDAND_PRIO+2)
653 #define XOR_PRIO (OR_PRIO+2)
654 #define AND_PRIO (XOR_PRIO+2)
655 #define EQUAL_PRIO (AND_PRIO+2)
656 #define LESS_PRIO (EQUAL_PRIO+2)
657 #define SHIFT_PRIO (LESS_PRIO+2)
658 #define PLUS_PRIO (SHIFT_PRIO+2)
659 #define MUL_PRIO (PLUS_PRIO+2)
660 #define UNARY_PRIO (MUL_PRIO+2)
661 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
663 #define COMPARE(OP) \
665 top->value = (unsigned1 || unsigned2) \
666 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 : (v1 OP v2)
668 /* Parse and evaluate a C expression, reading from PFILE.
669 Returns the truth value of the expression. */
672 _cpp_parse_expr (pfile)
675 /* The implementation is an operator precedence parser,
676 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
678 The stack base is 'stack', and the current stack pointer is 'top'.
679 There is a stack element for each operator (only),
680 and the most recently pushed operator is 'top->op'.
681 An operand (value) is stored in the 'value' field of the stack
682 element of the operator that precedes it.
683 In that case the 'flags' field has the HAVE_VALUE flag set. */
685 #define INIT_STACK_SIZE 20
686 struct operation init_stack[INIT_STACK_SIZE];
687 struct operation *stack = init_stack;
688 struct operation *limit = stack + INIT_STACK_SIZE;
689 register struct operation *top = stack;
690 unsigned int lprio, rprio = 0;
691 int skip_evaluation = 0;
692 long old_written = CPP_WRITTEN (pfile);
695 pfile->parsing_if_directive++;
704 op = lex (pfile, skip_evaluation);
706 /* See if the token is an operand, in which case go to set_value.
707 If the token is an operator, figure out its left and right
708 priorities, and then goto maybe_reduce. */
713 cpp_ice (pfile, "lex returns a NAME");
718 lprio = 0; goto maybe_reduce;
720 if (top->flags & HAVE_VALUE)
725 /* else fall through */
727 flags = RIGHT_OPERAND_REQUIRED;
728 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
729 case '*': case '/': case '%':
730 lprio = MUL_PRIO; goto binop;
731 case '<': case '>': case LEQ: case GEQ:
732 lprio = LESS_PRIO; goto binop;
733 case EQUAL: case NOTEQUAL:
734 lprio = EQUAL_PRIO; goto binop;
736 lprio = SHIFT_PRIO; goto binop;
737 case '&': lprio = AND_PRIO; goto binop;
738 case '^': lprio = XOR_PRIO; goto binop;
739 case '|': lprio = OR_PRIO; goto binop;
740 case ANDAND: lprio = ANDAND_PRIO; goto binop;
741 case OROR: lprio = OROR_PRIO; goto binop;
743 lprio = COMMA_PRIO; goto binop;
745 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
748 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
751 lprio = COND_PRIO; rprio = COND_PRIO;
754 lprio = COND_PRIO + 1; rprio = COND_PRIO;
759 cpp_error (pfile, "invalid character in #if");
764 /* Push a value onto the stack. */
765 if (top->flags & HAVE_VALUE)
767 cpp_error (pfile, "syntax error in #if");
770 top->value = op.value;
771 top->unsignedp = op.unsignedp;
772 top->flags |= HAVE_VALUE;
776 flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
780 /* Push an operator, and check if we can reduce now. */
781 while (top->rprio > lprio)
783 HOST_WIDEST_INT v1 = top[-1].value, v2 = top[0].value;
784 unsigned int unsigned1 = top[-1].unsignedp;
785 unsigned int unsigned2 = top[0].unsignedp;
787 if ((top[1].flags & LEFT_OPERAND_REQUIRED)
788 && ! (top[0].flags & HAVE_VALUE))
790 cpp_error (pfile, "syntax error - missing left operand");
793 if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
794 && ! (top[1].flags & HAVE_VALUE))
796 cpp_error (pfile, "syntax error - missing right operand");
799 /* top[0].value = (top[1].op)(v1, v2);*/
803 if (!(top->flags & HAVE_VALUE))
806 top->unsignedp = unsigned2;
807 top->flags |= HAVE_VALUE;
811 top->value = v1 + v2;
812 top->unsignedp = unsigned1 || unsigned2;
813 if (! top->unsignedp && ! skip_evaluation
814 && ! possible_sum_sign (v1, v2, top->value))
815 integer_overflow (pfile);
819 if (!(top->flags & HAVE_VALUE))
822 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
823 integer_overflow (pfile);
824 top->unsignedp = unsigned2;
825 top->flags |= HAVE_VALUE;
829 top->value = v1 - v2;
830 top->unsignedp = unsigned1 || unsigned2;
831 if (! top->unsignedp && ! skip_evaluation
832 && ! possible_sum_sign (top->value, v2, v1))
833 integer_overflow (pfile);
837 top->unsignedp = unsigned1 || unsigned2;
839 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
840 else if (!skip_evaluation)
842 top->value = v1 * v2;
844 && (top->value / v1 != v2
845 || (top->value & v1 & v2) < 0))
846 integer_overflow (pfile);
854 cpp_error (pfile, "division by zero in #if");
857 top->unsignedp = unsigned1 || unsigned2;
859 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
862 top->value = v1 / v2;
863 if ((top->value & v1 & v2) < 0)
864 integer_overflow (pfile);
872 cpp_error (pfile, "division by zero in #if");
875 top->unsignedp = unsigned1 || unsigned2;
877 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
879 top->value = v1 % v2;
882 if (top->flags & HAVE_VALUE)
884 cpp_error (pfile, "syntax error");
889 top->flags |= HAVE_VALUE;
892 if (top->flags & HAVE_VALUE)
894 cpp_error (pfile, "syntax error");
898 top->unsignedp = unsigned2;
899 top->flags |= HAVE_VALUE;
901 case '<': COMPARE(<); break;
902 case '>': COMPARE(>); break;
903 case LEQ: COMPARE(<=); break;
904 case GEQ: COMPARE(>=); break;
906 top->value = (v1 == v2);
910 top->value = (v1 != v2);
916 top->unsignedp = unsigned1;
917 if (v2 < 0 && ! unsigned2)
918 top->value = right_shift (pfile, v1, unsigned1, -v2);
920 top->value = left_shift (pfile, v1, unsigned1, v2);
925 top->unsignedp = unsigned1;
926 if (v2 < 0 && ! unsigned2)
927 top->value = left_shift (pfile, v1, unsigned1, -v2);
929 top->value = right_shift (pfile, v1, unsigned1, v2);
931 #define LOGICAL(OP) \
932 top->value = v1 OP v2;\
933 top->unsignedp = unsigned1 || unsigned2;
934 case '&': LOGICAL(&); break;
935 case '^': LOGICAL(^); break;
936 case '|': LOGICAL(|); break;
938 top->value = v1 && v2; top->unsignedp = 0;
939 if (!v1) skip_evaluation--;
942 top->value = v1 || v2; top->unsignedp = 0;
943 if (v1) skip_evaluation--;
946 if (CPP_PEDANTIC (pfile))
947 cpp_pedwarn (pfile, "comma operator in operand of `#if'");
949 top->unsignedp = unsigned2;
952 cpp_error (pfile, "syntax error in #if");
955 if (top[0].op != '?')
958 "syntax error ':' without preceding '?'");
961 else if (! (top[1].flags & HAVE_VALUE)
962 || !(top[-1].flags & HAVE_VALUE)
963 || !(top[0].flags & HAVE_VALUE))
965 cpp_error (pfile, "bad syntax for ?: operator");
971 if (top->value) skip_evaluation--;
972 top->value = top->value ? v1 : v2;
973 top->unsignedp = unsigned1 || unsigned2;
977 if ((top[1].flags & HAVE_VALUE)
978 || ! (top[0].flags & HAVE_VALUE)
980 || (top[-1].flags & HAVE_VALUE))
982 cpp_error (pfile, "mismatched parentheses in #if");
989 top->unsignedp = unsigned1;
990 top->flags |= HAVE_VALUE;
994 if (ISGRAPH (top[1].op))
995 cpp_error (pfile, "unimplemented operator '%c'\n", top[1].op);
997 cpp_error (pfile, "unimplemented operator '\\%03o'\n",
1004 cpp_ice (pfile, "unbalanced stack in #if expression");
1005 result = (top->value != 0);
1010 /* Check for and handle stack overflow. */
1013 struct operation *new_stack;
1014 int old_size = (char *) limit - (char *) stack;
1015 int new_size = 2 * old_size;
1016 if (stack != init_stack)
1017 new_stack = (struct operation *) xrealloc (stack, new_size);
1020 new_stack = (struct operation *) xmalloc (new_size);
1021 memcpy (new_stack, stack, old_size);
1024 top = (struct operation *) ((char *) new_stack + old_size);
1025 limit = (struct operation *) ((char *) new_stack + new_size);
1031 if ((op.op == OROR && top[-1].value)
1032 || (op.op == ANDAND && !top[-1].value)
1033 || (op.op == '?' && !top[-1].value))
1037 else if (op.op == ':')
1039 if (top[-2].value) /* Was condition true? */
1046 _cpp_skip_rest_of_line (pfile);
1049 pfile->parsing_if_directive--;
1050 CPP_SET_WRITTEN (pfile, old_written);
1051 if (stack != init_stack)