Commit 1920de47 by Zack Weinberg Committed by Zack Weinberg

cpplib.h: Add accessor macros for token lists.

	* cpplib.h: Add accessor macros for token lists.
	* cpplib.c, cpphash.c, cpplex.c: Use them.

From-SVN: r33630
parent 32919a0d
2000-05-03 Zack Weinberg <zack@wolery.cumb.org>
* cpplib.h: Add accessor macros for token lists.
* cpplib.c, cpphash.c, cpplex.c: Use them.
Wed May 3 09:29:17 2000 Richard Kenner <kenner@vlsi1.ultra.nyu.edu> Wed May 3 09:29:17 2000 Richard Kenner <kenner@vlsi1.ultra.nyu.edu>
* expr.c (expand_expr, case COMPONENT_REF): Don't check for checking * expr.c (expand_expr, case COMPONENT_REF): Don't check for checking
......
...@@ -467,7 +467,7 @@ collect_objlike_expansion (pfile, list) ...@@ -467,7 +467,7 @@ collect_objlike_expansion (pfile, list)
for (i = 1; i < list->tokens_used; i++) for (i = 1; i < list->tokens_used; i++)
{ {
switch (list->tokens[i].type) switch (TOK_TYPE (list, i))
{ {
case CPP_EOF: case CPP_EOF:
cpp_ice (pfile, "EOF in collect_expansion"); cpp_ice (pfile, "EOF in collect_expansion");
...@@ -489,13 +489,10 @@ collect_objlike_expansion (pfile, list) ...@@ -489,13 +489,10 @@ collect_objlike_expansion (pfile, list)
default:; default:;
} }
if (i > 1 && !last_was_paste if (i > 1 && !last_was_paste && TOK_PREV_WHITE (list, i))
&& (list->tokens[i].flags & PREV_WHITESPACE))
CPP_PUTC (pfile, ' '); CPP_PUTC (pfile, ' ');
CPP_PUTS (pfile, CPP_PUTS (pfile, TOK_NAME (list, i), TOK_LEN (list, i));
list->tokens[i].val.name.offset + list->namebuf,
list->tokens[i].val.name.len);
last_was_paste = 0; last_was_paste = 0;
} }
done: done:
...@@ -554,9 +551,9 @@ collect_funlike_expansion (pfile, list, arglist, replacement) ...@@ -554,9 +551,9 @@ collect_funlike_expansion (pfile, list, arglist, replacement)
for (i = replacement; i < list->tokens_used; i++) for (i = replacement; i < list->tokens_used; i++)
{ {
token = list->tokens[i].type; token = TOK_TYPE (list, i);
tok = list->tokens[i].val.name.offset + list->namebuf; tok = TOK_NAME (list, i);
len = list->tokens[i].val.name.len; len = TOK_LEN (list, i);
switch (token) switch (token)
{ {
case CPP_EOF: case CPP_EOF:
...@@ -597,10 +594,10 @@ collect_funlike_expansion (pfile, list, arglist, replacement) ...@@ -597,10 +594,10 @@ collect_funlike_expansion (pfile, list, arglist, replacement)
} }
if (last_token != PASTE && last_token != START if (last_token != PASTE && last_token != START
&& (list->tokens[i].flags & PREV_WHITESPACE)) && TOK_PREV_WHITE (list, i))
CPP_PUTC (pfile, ' '); CPP_PUTC (pfile, ' ');
if (last_token == ARG && CPP_TRADITIONAL (pfile) if (last_token == ARG && CPP_TRADITIONAL (pfile)
&& !(list->tokens[i].flags & PREV_WHITESPACE)) && !TOK_PREV_WHITE (list, i))
endpat->raw_after = 1; endpat->raw_after = 1;
switch (token) switch (token)
...@@ -642,7 +639,7 @@ collect_funlike_expansion (pfile, list, arglist, replacement) ...@@ -642,7 +639,7 @@ collect_funlike_expansion (pfile, list, arglist, replacement)
{ {
int raw_before = (last_token == PASTE int raw_before = (last_token == PASTE
|| (CPP_TRADITIONAL (pfile) || (CPP_TRADITIONAL (pfile)
&& !(list->tokens[i].flags & PREV_WHITESPACE))); && ! TOK_PREV_WHITE (list, j)));
add_pat (&pat, &endpat, add_pat (&pat, &endpat,
CPP_WRITTEN (pfile) - last /* nchars */, j /* argno */, CPP_WRITTEN (pfile) - last /* nchars */, j /* argno */,
...@@ -711,10 +708,10 @@ collect_params (pfile, list, arglist) ...@@ -711,10 +708,10 @@ collect_params (pfile, list, arglist)
unsigned int argc, a, i, j; unsigned int argc, a, i, j;
/* The formal parameters list starts at token 1. */ /* The formal parameters list starts at token 1. */
if (list->tokens[1].type != CPP_OPEN_PAREN) if (TOK_TYPE (list, 1) != CPP_OPEN_PAREN)
{ {
cpp_ice (pfile, "first token = %d not %d in collect_formal_parameters", cpp_ice (pfile, "first token = %d not %d in collect_formal_parameters",
list->tokens[1].type, CPP_OPEN_PAREN); TOK_TYPE (list, 1), CPP_OPEN_PAREN);
return 0; return 0;
} }
...@@ -723,10 +720,10 @@ collect_params (pfile, list, arglist) ...@@ -723,10 +720,10 @@ collect_params (pfile, list, arglist)
argc = 0; argc = 0;
argslen = 0; argslen = 0;
for (i = 2; i < list->tokens_used; i++) for (i = 2; i < list->tokens_used; i++)
switch (list->tokens[i].type) switch (TOK_TYPE (list, i))
{ {
case CPP_NAME: case CPP_NAME:
argslen += list->tokens[i].val.name.len + 1; argslen += TOK_LEN (list, i) + 1;
argc++; argc++;
break; break;
case CPP_COMMA: case CPP_COMMA:
...@@ -734,25 +731,25 @@ collect_params (pfile, list, arglist) ...@@ -734,25 +731,25 @@ collect_params (pfile, list, arglist)
case CPP_CLOSE_PAREN: case CPP_CLOSE_PAREN:
goto scanned; goto scanned;
case CPP_VSPACE: case CPP_VSPACE:
cpp_error_with_line (pfile, list->line, list->tokens[i].col, cpp_error_with_line (pfile, list->line, TOK_COL (list, i),
"missing right paren in macro argument list"); "missing right paren in macro argument list");
return 0; return 0;
default: default:
cpp_error_with_line (pfile, list->line, list->tokens[i].col, cpp_error_with_line (pfile, list->line, TOK_COL (list, i),
"syntax error in #define"); "illegal token in macro argument list");
return 0; return 0;
case CPP_ELLIPSIS: case CPP_ELLIPSIS:
if (list->tokens[i-1].type != CPP_NAME) if (TOK_TYPE (list, i-1) != CPP_NAME)
{ {
argslen += sizeof "__VA_ARGS__"; argslen += sizeof "__VA_ARGS__";
argc++; argc++;
} }
i++; i++;
if (list->tokens[i].type != CPP_CLOSE_PAREN) if (TOK_TYPE (list, i) != CPP_CLOSE_PAREN)
{ {
cpp_error_with_line (pfile, list->line, list->tokens[i].col, cpp_error_with_line (pfile, list->line, TOK_COL (list, i),
"another parameter follows \"...\""); "another parameter follows \"...\"");
return 0; return 0;
} }
...@@ -760,7 +757,7 @@ collect_params (pfile, list, arglist) ...@@ -760,7 +757,7 @@ collect_params (pfile, list, arglist)
} }
cpp_ice (pfile, "collect_params: unreachable - i=%d, ntokens=%d, type=%d", cpp_ice (pfile, "collect_params: unreachable - i=%d, ntokens=%d, type=%d",
i, list->tokens_used, list->tokens[i-1].type); i, list->tokens_used, TOK_TYPE (list, i-1));
return 0; return 0;
scanned: scanned:
...@@ -783,11 +780,11 @@ collect_params (pfile, list, arglist) ...@@ -783,11 +780,11 @@ collect_params (pfile, list, arglist)
p = namebuf; p = namebuf;
a = 0; a = 0;
for (j = 2; j < i; j++) for (j = 2; j < i; j++)
switch (list->tokens[j].type) switch (TOK_TYPE (list, j))
{ {
case CPP_NAME: case CPP_NAME:
tok = list->tokens[j].val.name.offset + list->namebuf; tok = TOK_NAME (list, j);
len = list->tokens[j].val.name.len; len = TOK_LEN (list, j);
memcpy (p, tok, len); memcpy (p, tok, len);
p[len] = '\0'; p[len] = '\0';
if (duplicate_arg_p (namebuf, p)) if (duplicate_arg_p (namebuf, p))
...@@ -812,7 +809,7 @@ collect_params (pfile, list, arglist) ...@@ -812,7 +809,7 @@ collect_params (pfile, list, arglist)
break; break;
case CPP_ELLIPSIS: case CPP_ELLIPSIS:
if (list->tokens[j-1].type != CPP_NAME) if (TOK_TYPE (list, j-1) != CPP_NAME)
{ {
if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)) if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99))
cpp_pedwarn (pfile, "C89 does not permit varargs macros"); cpp_pedwarn (pfile, "C89 does not permit varargs macros");
...@@ -833,7 +830,7 @@ collect_params (pfile, list, arglist) ...@@ -833,7 +830,7 @@ collect_params (pfile, list, arglist)
default: default:
cpp_ice (pfile, "collect_params: impossible token type %d", cpp_ice (pfile, "collect_params: impossible token type %d",
list->tokens[j].type); TOK_TYPE (list, j));
} }
arglist->argc = argc; arglist->argc = argc;
...@@ -869,17 +866,16 @@ _cpp_create_definition (pfile, list, hp) ...@@ -869,17 +866,16 @@ _cpp_create_definition (pfile, list, hp)
if (list->tokens_used == 2) if (list->tokens_used == 2)
ntype = T_EMPTY; /* Empty definition of object-like macro. */ ntype = T_EMPTY; /* Empty definition of object-like macro. */
else if (list->tokens_used == 3 && list->tokens[1].type == CPP_NAME else if (list->tokens_used == 3 && TOK_TYPE (list, 1) == CPP_NAME
&& list->tokens[0].val.name.len == list->tokens[1].val.name.len && TOK_LEN (list, 0) == TOK_LEN (list, 1)
&& !strncmp (list->tokens[0].val.name.offset + list->namebuf, && !strncmp (TOK_NAME (list, 0), TOK_NAME (list, 1),
list->tokens[1].val.name.offset + list->namebuf, TOK_LEN (list, 0)))
list->tokens[0].val.name.len))
ntype = T_IDENTITY; /* Object like macro defined to itself. */ ntype = T_IDENTITY; /* Object like macro defined to itself. */
/* The macro is function-like only if the next character, /* The macro is function-like only if the next character,
with no intervening whitespace, is '('. */ with no intervening whitespace, is '('. */
else if (list->tokens[1].type == CPP_OPEN_PAREN else if (TOK_TYPE (list, 1) == CPP_OPEN_PAREN
&& ! (list->tokens[1].flags & PREV_WHITESPACE)) && ! TOK_PREV_WHITE (list, 1))
{ {
struct arglist args; struct arglist args;
int replacement; int replacement;
...@@ -898,7 +894,7 @@ _cpp_create_definition (pfile, list, hp) ...@@ -898,7 +894,7 @@ _cpp_create_definition (pfile, list, hp)
whitespace after the name (6.10.3 para 3). */ whitespace after the name (6.10.3 para 3). */
else else
{ {
if (! (list->tokens[1].flags & PREV_WHITESPACE)) if (! TOK_PREV_WHITE (list, 1))
cpp_pedwarn (pfile, cpp_pedwarn (pfile,
"The C standard requires whitespace after #define %s", "The C standard requires whitespace after #define %s",
hp->name); hp->name);
...@@ -981,14 +977,14 @@ _cpp_create_definition (pfile, list, hp) ...@@ -981,14 +977,14 @@ _cpp_create_definition (pfile, list, hp)
{ {
fdefn->file = CPP_BUFFER (pfile)->nominal_fname; fdefn->file = CPP_BUFFER (pfile)->nominal_fname;
fdefn->line = list->line; fdefn->line = list->line;
fdefn->col = list->tokens[0].col; fdefn->col = TOK_COL (list, 0);
hp->value.fdefn = fdefn; hp->value.fdefn = fdefn;
} }
else else
{ {
odefn->file = CPP_BUFFER (pfile)->nominal_fname; odefn->file = CPP_BUFFER (pfile)->nominal_fname;
odefn->line = list->line; odefn->line = list->line;
odefn->col = list->tokens[0].col; odefn->col = TOK_COL (list, 0);
hp->value.odefn = odefn; hp->value.odefn = odefn;
} }
return 1; return 1;
......
...@@ -307,23 +307,22 @@ cpp_output_list (pfile, print, list) ...@@ -307,23 +307,22 @@ cpp_output_list (pfile, print, list)
for (i = 0; i < list->tokens_used; i++) for (i = 0; i < list->tokens_used; i++)
{ {
if (list->tokens[i].type == CPP_VSPACE) if (TOK_TYPE (list, i) == CPP_VSPACE)
{ {
output_line_command (pfile, print, list->tokens[i].aux); output_line_command (pfile, print, list->tokens[i].aux);
continue; continue;
} }
if (curcol < list->tokens[i].col) if (curcol < TOK_COL (list, i))
{ {
/* Insert space to bring the column to what it should be. */ /* Insert space to bring the column to what it should be. */
bump_column (print, curcol - 1, list->tokens[i].col); bump_column (print, curcol - 1, TOK_COL (list, i));
curcol = list->tokens[i].col; curcol = TOK_COL (list, i);
} }
/* XXX We may have to insert space to prevent an accidental /* XXX We may have to insert space to prevent an accidental
token paste. */ token paste. */
safe_fwrite (pfile, list->namebuf + list->tokens[i].val.name.offset, safe_fwrite (pfile, TOK_NAME (list, i), TOK_LEN (list, i), print->outf);
list->tokens[i].val.name.len, print->outf); curcol += TOK_LEN (list, i);
curcol += list->tokens[i].val.name.len;
} }
} }
...@@ -534,21 +533,21 @@ _cpp_scan_line (pfile, list) ...@@ -534,21 +533,21 @@ _cpp_scan_line (pfile, list)
type = CPP_NAME; type = CPP_NAME;
list->tokens_used++; list->tokens_used++;
list->tokens[i].type = type; TOK_TYPE (list, i) = type;
list->tokens[i].col = col; TOK_COL (list, i) = col;
list->tokens[i].flags = space_before ? PREV_WHITESPACE : 0; TOK_FLAGS (list, i) = space_before ? PREV_WHITESPACE : 0;
if (type == CPP_VSPACE) if (type == CPP_VSPACE)
break; break;
list->tokens[i].val.name.len = len; TOK_LEN (list, i) = len;
list->tokens[i].val.name.offset = list->name_used; TOK_OFFSET (list, i) = list->name_used;
memcpy (list->namebuf + list->name_used, CPP_PWRITTEN (pfile), len); memcpy (TOK_NAME (list, i), CPP_PWRITTEN (pfile), len);
list->name_used += len; list->name_used += len;
i++; i++;
space_before = 0; space_before = 0;
} }
list->tokens[i].aux = CPP_BUFFER (pfile)->lineno + 1; TOK_AUX (list, i) = CPP_BUFFER (pfile)->lineno + 1;
/* XXX Temporary kluge: put back the newline. */ /* XXX Temporary kluge: put back the newline. */
FORWARD(-1); FORWARD(-1);
...@@ -2194,7 +2193,7 @@ typedef unsigned int (* speller) PARAMS ((unsigned char *, cpp_toklist *, ...@@ -2194,7 +2193,7 @@ typedef unsigned int (* speller) PARAMS ((unsigned char *, cpp_toklist *,
#define INIT_NAME(list, name) \ #define INIT_NAME(list, name) \
do {(name).len = 0; (name).offset = (list)->name_used;} while (0) do {(name).len = 0; (name).offset = (list)->name_used;} while (0)
#define IS_DIRECTIVE(list) (list->tokens[0].type == CPP_HASH) #define IS_DIRECTIVE(list) (TOK_TYPE (list, 0) == CPP_HASH)
#define COLUMN(cur) ((cur) - buffer->line_base) #define COLUMN(cur) ((cur) - buffer->line_base)
/* Maybe put these in the ISTABLE eventually. */ /* Maybe put these in the ISTABLE eventually. */
...@@ -2893,7 +2892,7 @@ _cpp_lex_line (pfile, list) ...@@ -2893,7 +2892,7 @@ _cpp_lex_line (pfile, list)
/* Do we have a wide string? */ /* Do we have a wide string? */
if (cur_token[-1].type == CPP_NAME && IMMED_TOKEN () if (cur_token[-1].type == CPP_NAME && IMMED_TOKEN ()
&& cur_token[-1].val.name.len == 1 && cur_token[-1].val.name.len == 1
&& TOK_NAME (list, cur_token - 1)[0] == 'L' && *(list->namebuf + cur_token[-1].val.name.offset) == 'L'
&& !CPP_TRADITIONAL (pfile)) && !CPP_TRADITIONAL (pfile))
{ {
/* No need for 'L' any more. */ /* No need for 'L' any more. */
...@@ -3286,7 +3285,7 @@ spell_string (buffer, list, token) ...@@ -3286,7 +3285,7 @@ spell_string (buffer, list, token)
*buffer++ = c; *buffer++ = c;
len = token->val.name.len; len = token->val.name.len;
memcpy (buffer, TOK_NAME (list, token), len); memcpy (buffer, list->namebuf + token->val.name.offset, len);
buffer += len; buffer += len;
*buffer++ = c; *buffer++ = c;
return buffer - orig_buff; return buffer - orig_buff;
...@@ -3318,7 +3317,7 @@ spell_comment (buffer, list, token) ...@@ -3318,7 +3317,7 @@ spell_comment (buffer, list, token)
} }
len = token->val.name.len; len = token->val.name.len;
memcpy (buffer, TOK_NAME (list, token), len); memcpy (buffer, list->namebuf + token->val.name.offset, len);
return len + 2; return len + 2;
} }
...@@ -3333,7 +3332,7 @@ spell_name (buffer, list, token) ...@@ -3333,7 +3332,7 @@ spell_name (buffer, list, token)
size_t len; size_t len;
len = token->val.name.len; len = token->val.name.len;
memcpy (buffer, TOK_NAME (list, token), len); memcpy (buffer, list->namebuf + token->val.name.offset, len);
buffer += len; buffer += len;
return len; return len;
......
...@@ -349,21 +349,21 @@ do_define (pfile) ...@@ -349,21 +349,21 @@ do_define (pfile)
/* First token on the line must be a NAME. There must be at least /* First token on the line must be a NAME. There must be at least
one token (the VSPACE at the end). */ one token (the VSPACE at the end). */
if (list->tokens[0].type != CPP_NAME) if (TOK_TYPE (list, 0) != CPP_NAME)
{ {
cpp_error_with_line (pfile, list->line, list->tokens[0].col, cpp_error_with_line (pfile, list->line, TOK_COL (list, 0),
"#define must be followed by an identifier"); "#define must be followed by an identifier");
goto out; goto out;
} }
sym = list->namebuf + list->tokens[0].val.name.offset; sym = TOK_NAME (list, 0);
len = list->tokens[0].val.name.len; len = TOK_LEN (list, 0);
/* That NAME is not allowed to be "defined". (Not clear if the /* That NAME is not allowed to be "defined". (Not clear if the
standard requires this.) */ standard requires this.) */
if (len == 7 && !strncmp (sym, "defined", 7)) if (len == 7 && !strncmp (sym, "defined", 7))
{ {
cpp_error_with_line (pfile, list->line, list->tokens[0].col, cpp_error_with_line (pfile, list->line, TOK_COL (list, 0),
"\"defined\" is not a legal macro name"); "\"defined\" is not a legal macro name");
goto out; goto out;
} }
......
...@@ -157,24 +157,31 @@ struct cpp_name ...@@ -157,24 +157,31 @@ struct cpp_name
unsigned int offset; /* from list->namebuf */ unsigned int offset; /* from list->namebuf */
}; };
#define TOK_NAME(list, token) ((list)->namebuf + (token)->val.name.offset) /* Accessor macros for token lists - all expect you have a
list and an index. */
#define TOK_TYPE(l_, i_) ((l_)->tokens[i_].type)
#define TOK_FLAGS(l_, i_) ((l_)->tokens[i_].flags)
#define TOK_AUX(l_, i_) ((l_)->tokens[i_].aux)
#define TOK_COL(l_, i_) ((l_)->tokens[i_].col)
#define TOK_INT(l_, i_) ((l_)->tokens[i_].val.integer)
#define TOK_OFFSET(l_, i_) ((l_)->tokens[i_].val.name.offset)
#define TOK_NAME(l_, i_) ((l_)->tokens[i_].val.name.offset + (l_)->namebuf)
#define TOK_LEN(l_, i_) ((l_)->tokens[i_].val.name.len)
#define TOK_PREV_WHITE(l_, i_) (TOK_FLAGS(l_, i_) & PREV_WHITESPACE)
/* Flags for the cpp_token structure. */ /* Flags for the cpp_token structure. */
#define PREV_WHITESPACE 1 /* If whitespace before this token. */ #define PREV_WHITESPACE 1 /* If whitespace before this token. */
#define DIGRAPH 2 /* If it was a digraph. */ #define DIGRAPH 2 /* If it was a digraph. */
#define UNSIGNED_INT 4 /* If int preprocessing token unsigned. */ #define UNSIGNED_INT 4 /* If int preprocessing token unsigned. */
/* A preprocessing token. /* A preprocessing token. This has been carefully packed and should
This has been carefully packed and should occupy 16 bytes on occupy 16 bytes on both 32- and 64-bit hosts. */
both 32- and 64-bit hosts. */
struct cpp_token struct cpp_token
{ {
unsigned short col; /* starting column of this token */ unsigned short col; /* starting column of this token */
#ifdef ENUM_BITFIELDS_ARE_UNSIGNED ENUM_BITFIELD(cpp_ttype) type : CHAR_BIT; /* node type */
enum cpp_ttype type : CHAR_BIT; /* node type */
#else
unsigned char type;
#endif
unsigned char flags; /* flags - see above */ unsigned char flags; /* flags - see above */
unsigned int aux; /* CPP_OTHER character. Hash of a unsigned int aux; /* CPP_OTHER character. Hash of a
NAME, or something - see uses NAME, or something - see uses
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment