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 */
31 #ifdef MULTIBYTE_CHARS
35 #ifndef CHAR_TYPE_SIZE
36 #define CHAR_TYPE_SIZE BITS_PER_UNIT
40 #define INT_TYPE_SIZE BITS_PER_WORD
43 #ifndef LONG_TYPE_SIZE
44 #define LONG_TYPE_SIZE BITS_PER_WORD
47 #ifndef WCHAR_TYPE_SIZE
48 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
51 #ifndef MAX_CHAR_TYPE_SIZE
52 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
55 #ifndef MAX_INT_TYPE_SIZE
56 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
59 #ifndef MAX_LONG_TYPE_SIZE
60 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
63 #ifndef MAX_WCHAR_TYPE_SIZE
64 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
67 #define MAX_CHAR_TYPE_MASK (MAX_CHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
68 ? (~(~(HOST_WIDEST_INT) 0 << MAX_CHAR_TYPE_SIZE)) \
69 : ~ (HOST_WIDEST_INT) 0)
71 #define MAX_WCHAR_TYPE_MASK (MAX_WCHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
72 ? ~(~(HOST_WIDEST_INT) 0 << MAX_WCHAR_TYPE_SIZE) \
73 : ~ (HOST_WIDEST_INT) 0)
75 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
76 number with SUM's sign, where A, B, and SUM are all C integers. */
77 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
79 static void integer_overflow PARAMS ((cpp_reader *));
80 static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
82 unsigned HOST_WIDEST_INT));
83 static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
85 unsigned HOST_WIDEST_INT));
86 static struct operation parse_number PARAMS ((cpp_reader *, U_CHAR *,
88 static struct operation parse_charconst PARAMS ((cpp_reader *, U_CHAR *,
90 static struct operation parse_defined PARAMS ((cpp_reader *));
91 static HOST_WIDEST_INT parse_escape PARAMS ((cpp_reader *, U_CHAR **,
93 static struct operation lex PARAMS ((cpp_reader *, int));
109 #define LEFT_OPERAND_REQUIRED 1
110 #define RIGHT_OPERAND_REQUIRED 2
112 /* SKIP_OPERAND is set for '&&' '||' '?' and ':' when the
113 following operand should be short-circuited instead of evaluated. */
114 #define SKIP_OPERAND 8
119 U_CHAR rprio; /* Priority of op (relative to it right operand). */
121 U_CHAR unsignedp; /* true if value should be treated as unsigned */
122 HOST_WIDEST_INT value; /* The value logically "right" of op. */
125 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
126 with or without size suffixes. */
128 static struct operation
129 parse_number (pfile, start, end)
137 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
140 int digit, largest_digit = 0;
147 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
159 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
160 MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
161 / ((unsigned HOST_WIDEST_INT) base));
167 if (c >= '0' && c <= '9')
169 else if (base == 16 && c >= 'a' && c <= 'f') /* FIXME: assumes ASCII */
170 digit = c - 'a' + 10;
171 else if (base == 16 && c >= 'A' && c <= 'F')
172 digit = c - 'A' + 10;
175 /* It's a float since it contains a point. */
177 "floating point numbers are not allowed in #if expressions");
182 /* `l' means long, and `u' means unsigned. */
185 if (c == 'l' || c == 'L')
187 else if (c == 'u' || c == 'U')
191 /* Decrement p here so that the error for an invalid number
192 will be generated below in the case where this is the
193 last character in the buffer. */
201 /* Don't look for any more digits after the suffixes. */
205 if (largest_digit < digit)
206 largest_digit = digit;
207 nd = n * base + digit;
208 overflow |= MAX_over_base < n || nd < n;
214 cpp_error (pfile, "invalid number in #if expression");
217 else if (spec_long > (CPP_OPTIONS (pfile)->c89 ? 1 : 2))
219 cpp_error (pfile, "too many `l' suffixes in integer constant");
222 else if (op.unsignedp > 1)
224 cpp_error (pfile, "too many `u' suffixes in integer constant");
228 if (base <= largest_digit)
229 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
232 cpp_pedwarn (pfile, "integer constant out of range");
234 /* If too big to be signed, consider it unsigned. */
235 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
239 "integer constant is so large that it is unsigned");
252 /* Parse and convert a character constant for #if. Understands backslash
253 escapes (\n, \031) and multibyte characters (if so configured). */
254 static struct operation
255 parse_charconst (pfile, start, end)
261 HOST_WIDEST_INT result = 0;
264 unsigned int width = MAX_CHAR_TYPE_SIZE, mask = MAX_CHAR_TYPE_MASK;
273 width = MAX_WCHAR_TYPE_SIZE, mask = MAX_WCHAR_TYPE_MASK;
275 max_chars = MAX_LONG_TYPE_SIZE / width;
277 ++ptr; /* skip initial quote */
282 if (c == '\'' || c == '\0')
286 c = parse_escape (pfile, &ptr, mask);
287 if (width < HOST_BITS_PER_INT
288 && (unsigned int) c >= (unsigned int)(1 << width))
289 cpp_pedwarn (pfile, "escape sequence out of range for character");
292 /* Merge character into result; ignore excess chars. */
293 if (++num_chars <= max_chars)
295 if (width < HOST_BITS_PER_INT)
296 result = (result << width) | (c & ((1 << width) - 1));
304 cpp_error (pfile, "empty character constant");
309 /* cpp_get_token has already emitted an error if !traditional. */
310 if (! CPP_TRADITIONAL (pfile))
311 cpp_error (pfile, "malformatted character constant");
314 else if (num_chars > max_chars)
316 cpp_error (pfile, "character constant too long");
319 else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
320 cpp_warning (pfile, "multi-character character constant");
322 /* If char type is signed, sign-extend the constant. */
323 num_bits = num_chars * width;
325 if (cpp_defined (pfile, (const U_CHAR *)"__CHAR_UNSIGNED__",
326 sizeof ("__CHAR_UNSIGNED__")-1)
327 || ((result >> (num_bits - 1)) & 1) == 0)
328 op.value = result & ((unsigned HOST_WIDEST_INT) ~0
329 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
331 op.value = result | ~((unsigned HOST_WIDEST_INT) ~0
332 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
334 /* This is always a signed type. */
344 static struct operation
345 parse_defined (pfile)
350 enum cpp_token token;
352 long old_written = CPP_WRITTEN (pfile);
357 pfile->no_macro_expand++;
358 token = get_directive_token (pfile);
359 if (token == CPP_LPAREN)
362 CPP_SET_WRITTEN (pfile, old_written);
363 token = get_directive_token (pfile);
366 if (token != CPP_NAME)
369 tok = pfile->token_buffer + old_written;
370 len = CPP_PWRITTEN (pfile) - tok;
371 op.value = cpp_defined (pfile, tok, len);
375 if (get_directive_token (pfile) != CPP_RPAREN)
378 CPP_SET_WRITTEN (pfile, old_written);
379 pfile->no_macro_expand--;
383 CPP_SET_WRITTEN (pfile, old_written);
384 pfile->no_macro_expand--;
385 cpp_error (pfile, "`defined' without an identifier");
393 const char *operator;
397 static const struct token tokentab2[] = {
411 /* Read one token. */
413 static struct operation
414 lex (pfile, skip_evaluation)
418 const struct token *toktab;
419 enum cpp_token token;
421 U_CHAR *tok_start, *tok_end;
424 old_written = CPP_WRITTEN (pfile);
425 token = get_directive_token (pfile);
427 tok_start = pfile->token_buffer + old_written;
428 tok_end = CPP_PWRITTEN (pfile);
429 CPP_SET_WRITTEN (pfile, old_written);
432 case CPP_EOF: /* Should not happen ... */
437 return parse_number (pfile, tok_start, tok_end);
440 cpp_error (pfile, "string constants are not allowed in #if expressions");
446 return parse_charconst (pfile, tok_start, tok_end);
449 if (!strcmp (tok_start, "defined"))
450 return parse_defined (pfile);
456 if (CPP_WARN_UNDEF (pfile) && !skip_evaluation)
457 cpp_warning (pfile, "`%.*s' is not defined",
458 (int) (tok_end - tok_start), tok_start);
464 op.value = cpp_defined (pfile, tok_start, tok_end - tok_start);
468 /* See if it is a special token of length 2. */
469 if (tok_start + 2 == tok_end)
471 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
472 if (tok_start[0] == toktab->operator[0]
473 && tok_start[1] == toktab->operator[1])
475 if (toktab->token == ERROR)
476 cpp_error (pfile, "`%s' not allowed in operand of `#if'",
478 op.op = toktab->token;
489 /* Parse a C escape sequence. STRING_PTR points to a variable
490 containing a pointer to the string to parse. That pointer
491 is updated past the characters we use. The value of the
492 escape sequence is returned.
494 A negative value means the sequence \ newline was seen,
495 which is supposed to be equivalent to nothing at all.
497 If \ is followed by a null character, we return a negative
498 value and leave the string pointer pointing at the null character.
500 If \ is followed by 000, we return 0 and leave the string pointer
501 after the zeros. A value of 0 does not mean end of string. */
503 static HOST_WIDEST_INT
504 parse_escape (pfile, string_ptr, result_mask)
507 HOST_WIDEST_INT result_mask;
509 register int c = *(*string_ptr)++;
518 if (CPP_OPTIONS (pfile)->pedantic)
519 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
524 return TARGET_NEWLINE;
546 register HOST_WIDEST_INT i = c - '0';
547 register int count = 0;
550 c = *(*string_ptr)++;
551 if (c >= '0' && c <= '7')
552 i = (i << 3) + c - '0';
559 if (i != (i & result_mask))
562 cpp_pedwarn (pfile, "octal escape sequence out of range");
568 register unsigned HOST_WIDEST_INT i = 0, overflow = 0;
569 register int digits_found = 0, digit;
572 c = *(*string_ptr)++;
573 if (c >= '0' && c <= '9')
575 else if (c >= 'a' && c <= 'f')
576 digit = c - 'a' + 10;
577 else if (c >= 'A' && c <= 'F')
578 digit = c - 'A' + 10;
584 overflow |= i ^ (i << 4 >> 4);
585 i = (i << 4) + digit;
589 cpp_error (pfile, "\\x used with no following hex digits");
590 if (overflow | (i != (i & result_mask)))
593 cpp_pedwarn (pfile, "hex escape sequence out of range");
603 integer_overflow (pfile)
606 if (CPP_PEDANTIC (pfile))
607 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
610 static HOST_WIDEST_INT
611 left_shift (pfile, a, unsignedp, b)
614 unsigned int unsignedp;
615 unsigned HOST_WIDEST_INT b;
617 if (b >= HOST_BITS_PER_WIDEST_INT)
619 if (! unsignedp && a != 0)
620 integer_overflow (pfile);
624 return (unsigned HOST_WIDEST_INT) a << b;
627 HOST_WIDEST_INT l = a << b;
629 integer_overflow (pfile);
634 static HOST_WIDEST_INT
635 right_shift (pfile, a, unsignedp, b)
636 cpp_reader *pfile ATTRIBUTE_UNUSED;
638 unsigned int unsignedp;
639 unsigned HOST_WIDEST_INT b;
641 if (b >= HOST_BITS_PER_WIDEST_INT)
642 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
644 return (unsigned HOST_WIDEST_INT) a >> b;
649 /* These priorities are all even, so we can handle associatively. */
650 #define PAREN_INNER_PRIO 0
652 #define COND_PRIO (COMMA_PRIO+2)
653 #define OROR_PRIO (COND_PRIO+2)
654 #define ANDAND_PRIO (OROR_PRIO+2)
655 #define OR_PRIO (ANDAND_PRIO+2)
656 #define XOR_PRIO (OR_PRIO+2)
657 #define AND_PRIO (XOR_PRIO+2)
658 #define EQUAL_PRIO (AND_PRIO+2)
659 #define LESS_PRIO (EQUAL_PRIO+2)
660 #define SHIFT_PRIO (LESS_PRIO+2)
661 #define PLUS_PRIO (SHIFT_PRIO+2)
662 #define MUL_PRIO (PLUS_PRIO+2)
663 #define UNARY_PRIO (MUL_PRIO+2)
664 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
666 #define COMPARE(OP) \
668 top->value = (unsigned1 || unsigned2) \
669 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 : (v1 OP v2)
671 /* Parse and evaluate a C expression, reading from PFILE.
672 Returns the value of the expression. */
675 _cpp_parse_expr (pfile)
678 /* The implementation is an operator precedence parser,
679 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
681 The stack base is 'stack', and the current stack pointer is 'top'.
682 There is a stack element for each operator (only),
683 and the most recently pushed operator is 'top->op'.
684 An operand (value) is stored in the 'value' field of the stack
685 element of the operator that precedes it.
686 In that case the 'flags' field has the HAVE_VALUE flag set. */
688 #define INIT_STACK_SIZE 20
689 struct operation init_stack[INIT_STACK_SIZE];
690 struct operation *stack = init_stack;
691 struct operation *limit = stack + INIT_STACK_SIZE;
692 register struct operation *top = stack;
693 unsigned int lprio, rprio = 0;
694 int skip_evaluation = 0;
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 if (stack != init_stack)
1011 /* Check for and handle stack overflow. */
1014 struct operation *new_stack;
1015 int old_size = (char *) limit - (char *) stack;
1016 int new_size = 2 * old_size;
1017 if (stack != init_stack)
1018 new_stack = (struct operation *) xrealloc (stack, new_size);
1021 new_stack = (struct operation *) xmalloc (new_size);
1022 memcpy (new_stack, stack, old_size);
1025 top = (struct operation *) ((char *) new_stack + old_size);
1026 limit = (struct operation *) ((char *) new_stack + new_size);
1032 if ((op.op == OROR && top[-1].value)
1033 || (op.op == ANDAND && !top[-1].value)
1034 || (op.op == '?' && !top[-1].value))
1038 else if (op.op == ':')
1040 if (top[-2].value) /* Was condition true? */
1047 if (stack != init_stack)