-/* Parse C expressions for CCCP.
+/* Parse C expressions for cpplib.
Copyright (C) 1987, 92, 94, 95, 97, 98, 1999, 2000 Free Software Foundation.
This program is free software; you can redistribute it and/or modify it
#include "config.h"
#include "system.h"
#include "cpplib.h"
-
-#ifdef MULTIBYTE_CHARS
-#include <locale.h>
-#endif
+#include "cpphash.h"
#ifndef CHAR_TYPE_SIZE
#define CHAR_TYPE_SIZE BITS_PER_UNIT
#endif
#define MAX_CHAR_TYPE_MASK (MAX_CHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
- ? (~ (~ (HOST_WIDEST_INT) 0 << MAX_CHAR_TYPE_SIZE)) \
+ ? (~(~(HOST_WIDEST_INT) 0 << MAX_CHAR_TYPE_SIZE)) \
: ~ (HOST_WIDEST_INT) 0)
#define MAX_WCHAR_TYPE_MASK (MAX_WCHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
- ? ~ (~ (HOST_WIDEST_INT) 0 << MAX_WCHAR_TYPE_SIZE) \
+ ? ~(~(HOST_WIDEST_INT) 0 << MAX_WCHAR_TYPE_SIZE) \
: ~ (HOST_WIDEST_INT) 0)
/* Yield nonzero if adding two numbers with A's and B's signs can yield a
#define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
static void integer_overflow PARAMS ((cpp_reader *));
-static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT, int, unsigned HOST_WIDEST_INT));
-static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT, int, unsigned HOST_WIDEST_INT));
-static struct operation parse_number PARAMS ((cpp_reader *, U_CHAR *, U_CHAR *));
-static struct operation parse_charconst PARAMS ((cpp_reader *, U_CHAR *, U_CHAR *));
+static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
+ unsigned int,
+ unsigned HOST_WIDEST_INT));
+static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
+ unsigned int,
+ unsigned HOST_WIDEST_INT));
+static struct operation parse_number PARAMS ((cpp_reader *, U_CHAR *,
+ U_CHAR *));
+static struct operation parse_charconst PARAMS ((cpp_reader *, U_CHAR *,
+ U_CHAR *));
static struct operation parse_defined PARAMS ((cpp_reader *));
-static struct operation cpp_lex PARAMS ((cpp_reader *, int));
-extern HOST_WIDEST_INT cpp_parse_expr PARAMS ((cpp_reader *));
-static HOST_WIDEST_INT cpp_parse_escape PARAMS ((cpp_reader *, U_CHAR **, HOST_WIDEST_INT));
+static HOST_WIDEST_INT parse_escape PARAMS ((cpp_reader *, U_CHAR **,
+ HOST_WIDEST_INT));
+static struct operation lex PARAMS ((cpp_reader *, int));
+
#define ERROR 299
#define OROR 300
/* SKIP_OPERAND is set for '&&' '||' '?' and ':' when the
following operand should be short-circuited instead of evaluated. */
#define SKIP_OPERAND 8
-/*#define UNSIGNEDP 16*/
-
-struct operation {
- short op;
- char rprio; /* Priority of op (relative to it right operand). */
- char flags;
- char unsignedp; /* true if value should be treated as unsigned */
- HOST_WIDEST_INT value; /* The value logically "right" of op. */
+
+struct operation
+{
+ short op;
+ U_CHAR rprio; /* Priority of op (relative to it right operand). */
+ U_CHAR flags;
+ U_CHAR unsignedp; /* true if value should be treated as unsigned */
+ HOST_WIDEST_INT value; /* The value logically "right" of op. */
};
/* Parse and convert an integer for #if. Accepts decimal, hex, or octal
int max_chars;
U_CHAR *ptr = start;
- /* FIXME: Should use reentrant multibyte functions. */
-#ifdef MULTIBYTE_CHARS
- wchar_t c = (wchar_t)-1;
- (void) mbtowc (NULL_PTR, NULL_PTR, 0);
-#else
int c = -1;
-#endif
if (*ptr == 'L')
{
while (ptr < end)
{
-#ifndef MULTIBYTE_CHARS
c = *ptr++;
-#else
- ptr += mbtowc (&c, ptr, end - ptr);
-#endif
if (c == '\'' || c == '\0')
break;
else if (c == '\\')
{
- /* Hopefully valid assumption: if mbtowc returns a backslash,
- we are in initial shift state. No valid escape-sequence
- character can take us out of initial shift state or begin
- an unshifted multibyte char, so cpp_parse_escape doesn't
- need to know about multibyte chars. */
-
- c = cpp_parse_escape (pfile, &ptr, mask);
+ c = parse_escape (pfile, &ptr, mask);
if (width < HOST_BITS_PER_INT
&& (unsigned int) c >= (unsigned int)(1 << width))
cpp_pedwarn (pfile, "escape sequence out of range for character");
op.op = INT;
pfile->no_macro_expand++;
- token = get_directive_token (pfile);
+ token = _cpp_get_directive_token (pfile);
if (token == CPP_LPAREN)
{
paren++;
CPP_SET_WRITTEN (pfile, old_written);
- token = get_directive_token (pfile);
+ token = _cpp_get_directive_token (pfile);
}
if (token != CPP_NAME)
if (paren)
{
- if (get_directive_token (pfile) != CPP_RPAREN)
+ if (_cpp_get_directive_token (pfile) != CPP_RPAREN)
goto oops;
}
CPP_SET_WRITTEN (pfile, old_written);
int token;
};
-static struct token tokentab2[] = {
+static const struct token tokentab2[] = {
{"&&", ANDAND},
{"||", OROR},
{"<<", LSH},
/* Read one token. */
static struct operation
-cpp_lex (pfile, skip_evaluation)
+lex (pfile, skip_evaluation)
cpp_reader *pfile;
int skip_evaluation;
{
- struct token *toktab;
+ const struct token *toktab;
enum cpp_token token;
struct operation op;
U_CHAR *tok_start, *tok_end;
long old_written;
old_written = CPP_WRITTEN (pfile);
- token = get_directive_token (pfile);
+ token = _cpp_get_directive_token (pfile);
tok_start = pfile->token_buffer + old_written;
tok_end = CPP_PWRITTEN (pfile);
op.unsignedp = 0;
op.value = 0;
- if (CPP_WARN_UNDEF (pfile) && !skip_evaluation)
+ if (CPP_OPTIONS (pfile)->warn_undef && !skip_evaluation)
cpp_warning (pfile, "`%.*s' is not defined",
(int) (tok_end - tok_start), tok_start);
return op;
after the zeros. A value of 0 does not mean end of string. */
static HOST_WIDEST_INT
-cpp_parse_escape (pfile, string_ptr, result_mask)
+parse_escape (pfile, string_ptr, result_mask)
cpp_reader *pfile;
U_CHAR **string_ptr;
HOST_WIDEST_INT result_mask;
left_shift (pfile, a, unsignedp, b)
cpp_reader *pfile;
HOST_WIDEST_INT a;
- int unsignedp;
+ unsigned int unsignedp;
unsigned HOST_WIDEST_INT b;
{
if (b >= HOST_BITS_PER_WIDEST_INT)
right_shift (pfile, a, unsignedp, b)
cpp_reader *pfile ATTRIBUTE_UNUSED;
HOST_WIDEST_INT a;
- int unsignedp;
+ unsigned int unsignedp;
unsigned HOST_WIDEST_INT b;
{
if (b >= HOST_BITS_PER_WIDEST_INT)
? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 : (v1 OP v2)
/* Parse and evaluate a C expression, reading from PFILE.
- Returns the value of the expression. */
+ Returns the truth value of the expression. */
-HOST_WIDEST_INT
-cpp_parse_expr (pfile)
+int
+_cpp_parse_expr (pfile)
cpp_reader *pfile;
{
/* The implementation is an operator precedence parser,
struct operation *stack = init_stack;
struct operation *limit = stack + INIT_STACK_SIZE;
register struct operation *top = stack;
- int lprio, rprio = 0;
+ unsigned int lprio, rprio = 0;
int skip_evaluation = 0;
+ long old_written = CPP_WRITTEN (pfile);
+ int result;
+ pfile->parsing_if_directive++;
top->rprio = 0;
top->flags = 0;
for (;;)
{
struct operation op;
- char flags = 0;
+ U_CHAR flags = 0;
/* Read a token */
- op = cpp_lex (pfile, skip_evaluation);
+ op = lex (pfile, skip_evaluation);
/* See if the token is an operand, in which case go to set_value.
If the token is an operator, figure out its left and right
switch (op.op)
{
case NAME:
- cpp_ice (pfile, "cpp_lex returns a NAME");
+ cpp_ice (pfile, "lex returns a NAME");
goto syntax_error;
case INT: case CHAR:
goto set_value;
while (top->rprio > lprio)
{
HOST_WIDEST_INT v1 = top[-1].value, v2 = top[0].value;
- int unsigned1 = top[-1].unsignedp, unsigned2 = top[0].unsignedp;
+ unsigned int unsigned1 = top[-1].unsignedp;
+ unsigned int unsigned2 = top[0].unsignedp;
top--;
if ((top[1].flags & LEFT_OPERAND_REQUIRED)
&& ! (top[0].flags & HAVE_VALUE))
{
if (top != stack)
cpp_ice (pfile, "unbalanced stack in #if expression");
- if (stack != init_stack)
- free (stack);
- return top->value;
+ result = (top->value != 0);
+ goto done;
}
top++;
else
{
new_stack = (struct operation *) xmalloc (new_size);
- bcopy ((char *) stack, (char *) new_stack, old_size);
+ memcpy (new_stack, stack, old_size);
}
stack = new_stack;
top = (struct operation *) ((char *) new_stack + old_size);
}
}
syntax_error:
+ _cpp_skip_rest_of_line (pfile);
+ result = 0;
+ done:
+ pfile->parsing_if_directive--;
+ CPP_SET_WRITTEN (pfile, old_written);
if (stack != init_stack)
free (stack);
- return 0;
+ return result;
}