home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 7
/
FreshFishVol7.bin
/
bbs
/
gnu
/
gcc-2.3.3-src.lha
/
GNU
/
src
/
amiga
/
gcc-2.3.3
/
cp-spew.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-02-06
|
28KB
|
1,061 lines
/* Type Analyzer for GNU C++.
Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
Hacked... nay, bludgeoned... by Mark Eichin (eichin@cygnus.com)
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
/* This file is the type analyzer for GNU C++. To debug it, define SPEW_DEBUG
when compiling cp-parse.c and cp-spew.c. */
#include "config.h"
#include <stdio.h>
#include "input.h"
#include "tree.h"
#include "cp-lex.h"
#include "cp-parse.h"
#include "cp-tree.h"
#include "flags.h"
#include "obstack.h"
/* This takes a token stream that hasn't decided much about types and
tries to figure out as much as it can, with excessive lookahead and
backtracking. */
/* fifo of tokens recognized and available to parser. */
struct token {
/* The values for YYCHAR will fit in a short. */
short yychar;
short end_of_file;
YYSTYPE yylval;
};
static int do_aggr ();
static struct token frob_identifier ();
static struct token hack_scope ();
static tree hack_ptype ();
static tree hack_more_ids ();
/* From cp-lex.c: */
/* the declaration found for the last IDENTIFIER token read in.
yylex must look this up to detect typedefs, which get token type TYPENAME,
so it is left around in case the identifier is not a typedef but is
used in a context which makes it a reference to a variable. */
extern tree lastiddecl; /* let our brains leak out here too */
extern int yychar; /* the lookahead symbol */
extern YYSTYPE yylval; /* the semantic value of the */
/* lookahead symbol */
extern int end_of_file;
struct obstack token_obstack;
int first_token;
#ifdef SPEW_DEBUG
int spew_debug = 0;
static unsigned int yylex_ctr = 0;
static int debug_yychar ();
#endif
static char follows_typename[END_OF_SAVED_INPUT+1];
static char follows_identifier[END_OF_SAVED_INPUT+1];
/* This is a hack!!! TEMPLATE_TYPE_SEEN_BEFORE_SCOPE consists of the name
* of the last template_type parsed in cp-parse.y if it is followed by a
* scope operator. It will be reset inside the next invocation of yylex().
* This is used for recognizing nested types inside templates.
* - niklas@appli.se */
tree template_type_seen_before_scope;
/* Initialize token_obstack. Called once, from init_lex. */
void
init_spew ()
{
static char *chars_following_identifier = ".+-|/%^!?:";
short *ps;
static short toks_follow_ids[] =
{ POINTSAT_LEFT_RIGHT, ASSIGN, RANGE, OROR, ANDAND, MIN_MAX, EQCOMPARE,
ARITHCOMPARE, LSHIFT, RSHIFT, UNARY, PLUSPLUS, MINUSMINUS, POINTSAT,
POINTSAT_STAR, DOT_STAR, CONSTANT, STRING, SIZEOF, ENUM, IF,
ELSE, WHILE, DO, FOR, SWITCH, CASE, DEFAULT, BREAK, CONTINUE,
RETURN, GOTO, ASM_KEYWORD, TYPEOF, ALIGNOF, HEADOF, CLASSOF, ATTRIBUTE,
AGGR, VISSPEC, DELETE, RAISE, RERAISE, TRY, EXCEPT, CATCH,
THROW, ANSI_TRY, ANSI_THROW, EXTERN_LANG_STRING, ALL,
END_OF_SAVED_INPUT, -1 };
static short toks_follow_types[] =
{ IDENTIFIER, TYPENAME, SCOPED_TYPENAME, SCSPEC, TYPESPEC, TYPE_QUAL,
ELLIPSIS, THIS, OPERATOR, DYNAMIC, TEMPLATE, SCOPE, START_DECLARATOR,
TYPENAME_COLON, PAREN_STAR_PAREN, TYPENAME_ELLIPSIS, PTYPENAME,
PRE_PARSED_FUNCTION_DECL, PRE_PARSED_CLASS_DECL, -1 };
gcc_obstack_init(&token_obstack);
/* Initialize the arrays saying what tokens are definitely
(or possibly) valid following typenames and identifiers. */
while (*chars_following_identifier)
follows_identifier[*chars_following_identifier++] = 1;
for (ps = toks_follow_ids; *ps != -1; ps++)
follows_identifier[*ps] = 1;
for (ps = toks_follow_types; *ps != -1; ps++)
follows_typename[*ps] = 1;
}
#ifdef SPEW_DEBUG
/* Use functions for debugging... */
/* Return the number of tokens available on the fifo. */
static int
num_tokens ()
{
return (obstack_object_size(&token_obstack)/sizeof(struct token))
- first_token;
}
/* Fetch the token N down the line from the head of the fifo. */
static struct token*
nth_token (n)
int n;
{
/* could just have this do slurp_ implicitly, but this way is easier
* to debug... */
my_friendly_assert (n < num_tokens(), 298);
return ((struct token*)obstack_base(&token_obstack))+n+first_token;
}
/* Add a token to the token fifo. */
static void
add_token (t)
struct token* t;
{
obstack_grow(&token_obstack,t,sizeof (struct token));
}
/* Consume the next token out of the fifo. */
static void
consume_token()
{
if (num_tokens() == 1)
{
obstack_free(&token_obstack, obstack_base (&token_obstack));
first_token = 0;
}
else
first_token++;
}
#else
/* ...otherwise use macros. */
#define num_tokens() \
((obstack_object_size(&token_obstack)/sizeof(struct token)) - first_token)
#define nth_token(N) \
(((struct token*)obstack_base(&token_obstack))+(N)+first_token)
#define add_token(T) obstack_grow(&token_obstack, (T), sizeof (struct token))
#define consume_token() \
(num_tokens() == 1 \
? (obstack_free (&token_obstack, obstack_base (&token_obstack)), \
(first_token = 0)) \
: first_token++)
#endif
/* Pull in enough tokens from real_yylex that the queue is N long. */
static void
scan_tokens (n)
int n;
{
int i;
struct token *tmp;
/* We cannot read past certain tokens, so make sure we don't. */
i = num_tokens ();
if (i > n)
return;
while (i-- > 0)
{
tmp = nth_token (i);
/* Never read past these characters: they might separate
the current input stream from one we save away later. */
if (tmp->yychar == '{' || tmp->yychar == ':')
goto pad_tokens;
}
while (num_tokens() <= n)
{
obstack_blank(&token_obstack,sizeof (struct token));
tmp = ((struct token *)obstack_next_free (&token_obstack))-1;
tmp->yychar = real_yylex();
tmp->end_of_file = end_of_file;
tmp->yylval = yylval;
end_of_file = 0;
if (tmp->yychar == '{'
|| tmp->yychar == ':'
|| tmp->yychar == ';')
{
pad_tokens:
while (num_tokens () <= n)
{
obstack_blank(&token_obstack,sizeof (struct token));
tmp = ((struct token *)obstack_next_free (&token_obstack))-1;
tmp->yychar = EMPTY;
tmp->end_of_file = 0;
}
}
}
}
/* Create room for N tokens at the front of the fifo. This is used
to insert new tokens into the stream ahead of the current token. */
static void
shift_tokens (n)
int n;
{
if (first_token >= n)
first_token -= n;
else
{
int old_token_count = num_tokens ();
char *tmp;
obstack_blank (&token_obstack, (n-first_token) * sizeof (struct token));
if (old_token_count)
{
tmp = (char *)alloca ((num_tokens () + (n-first_token))
* sizeof (struct token));
/* This move does not rely on the system being able to handle
overlapping moves. */
bcopy (nth_token (0), tmp, old_token_count * sizeof (struct token));
bcopy (tmp, nth_token (n), old_token_count * sizeof (struct token));
}
first_token = 0;
}
}
int
probe_obstack (h, obj, nlevels)
struct obstack *h;
tree obj;
unsigned int nlevels;
{
register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
register struct _obstack_chunk* plp; /* point to previous chunk if any */
lp = (h)->chunk;
/* We use >= rather than > since the object cannot be exactly at
the beginning of the chunk but might be an empty object exactly
at the end of an adjacent chunk. */
for (; nlevels > 0 && lp != 0 && ((tree)lp >= obj || (tree)lp->limit < obj);
nlevels -= 1)
{
plp = lp->prev;
lp = plp;