1 /* Type Analyzer for GNU C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Hacked... nay, bludgeoned... by Mark Eichin (eichin@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* This file is the type analyzer for GNU C++. To debug it, define SPEW_DEBUG
24 when compiling parse.c and spew.c. */
45 #define SPEW_INLINE inline
48 /* This takes a token stream that hasn't decided much about types and
49 tries to figure out as much as it can, with excessive lookahead and
52 /* fifo of tokens recognized and available to parser. */
55 /* The values for YYCHAR will fit in a short. */
58 YYSTYPE
GTY ((desc ("%1.yychar"))) yylval
;
61 /* Since inline methods can refer to text which has not yet been seen,
62 we store the text of the method in a structure which is placed in the
63 DECL_PENDING_INLINE_INFO field of the FUNCTION_DECL.
64 After parsing the body of the class definition, the FUNCTION_DECL's are
65 scanned to see which ones have this field set. Those are then digested
68 This function's FUNCTION_DECL will have a bit set in its common so
69 that we know to watch out for it. */
71 #define TOKEN_CHUNK_SIZE 20
72 struct token_chunk
GTY(())
74 struct token_chunk
*next
;
75 struct token toks
[TOKEN_CHUNK_SIZE
];
78 struct unparsed_text
GTY(())
80 struct unparsed_text
*next
; /* process this one next */
81 tree decl
; /* associated declaration */
82 location_t locus
; /* location we got the text from */
83 int interface
; /* remembering interface_unknown and interface_only */
85 struct token_chunk
* tokens
; /* Start of the token list. */
87 struct token_chunk
*last_chunk
; /* End of the token list. */
88 short last_pos
; /* Number of tokens used in the last chunk of
91 short cur_pos
; /* Current token in 'cur_chunk', when rescanning. */
92 struct token_chunk
*cur_chunk
; /* Current chunk, when rescanning. */
95 /* Stack of state saved off when we return to an inline method or
96 default argument that has been stored for later parsing. */
99 struct unparsed_text
*input
;
102 YYSTYPE
GTY ((desc ("%1.yychar"))) yylval
;
104 struct obstack
GTY ((skip (""))) token_obstack
;
108 static GTY(()) struct feed
*feed
;
110 static SPEW_INLINE
void do_aggr
PARAMS ((void));
111 static SPEW_INLINE
int identifier_type
PARAMS ((tree
));
112 static void scan_tokens
PARAMS ((int));
113 static void feed_defarg
PARAMS ((tree
));
114 static void finish_defarg
PARAMS ((void));
115 static void yylexstring
PARAMS ((struct token
*));
116 static int read_token
PARAMS ((struct token
*));
118 static SPEW_INLINE
int num_tokens
PARAMS ((void));
119 static SPEW_INLINE
struct token
*nth_token
PARAMS ((int));
120 static SPEW_INLINE
int next_token
PARAMS ((struct token
*));
121 static SPEW_INLINE
int shift_token
PARAMS ((void));
122 static SPEW_INLINE
void push_token
PARAMS ((struct token
*));
123 static SPEW_INLINE
void consume_token
PARAMS ((void));
124 static SPEW_INLINE
int read_process_identifier
PARAMS ((YYSTYPE
*));
126 static SPEW_INLINE
void feed_input
PARAMS ((struct unparsed_text
*));
127 static SPEW_INLINE
struct token
* space_for_token
128 PARAMS ((struct unparsed_text
*t
));
129 static SPEW_INLINE
struct token
* remove_last_token
130 PARAMS ((struct unparsed_text
*t
));
131 static struct unparsed_text
* alloc_unparsed_text
132 PARAMS ((const location_t
*, tree decl
, int interface
));
134 static void snarf_block
PARAMS ((struct unparsed_text
*t
));
135 static tree snarf_defarg
PARAMS ((void));
136 static int frob_id
PARAMS ((int, int, tree
*));
138 /* The list of inline functions being held off until we reach the end of
139 the current class declaration. */
140 static GTY(()) struct unparsed_text
*pending_inlines
;
141 static GTY(()) struct unparsed_text
*pending_inlines_tail
;
143 /* The list of previously-deferred inline functions currently being parsed.
144 This exists solely to be a GC root. */
145 static GTY(()) struct unparsed_text
*processing_these_inlines
;
147 static void begin_parsing_inclass_inline
PARAMS ((struct unparsed_text
*));
151 static unsigned int yylex_ctr
= 0;
153 static void debug_yychar
PARAMS ((int));
156 extern char *debug_yytranslate
PARAMS ((int));
158 static enum cpp_ttype last_token
;
159 static tree last_token_id
;
162 /* the declaration found for the last IDENTIFIER token read in. yylex
163 must look this up to detect typedefs, which get token type
164 tTYPENAME, so it is left around in case the identifier is not a
165 typedef but is used in a context which makes it a reference to a
167 extern tree lastiddecl
; /* let our brains leak out here too */
168 extern int yychar
; /* the lookahead symbol */
169 extern YYSTYPE yylval
; /* the semantic value of the */
170 /* lookahead symbol */
171 /* The token fifo lives in this obstack. */
172 static struct obstack token_obstack
;
173 static int first_token
;
175 /* When we see a default argument in a method declaration, we snarf it as
176 text using snarf_defarg. When we get up to namespace scope, we then go
177 through and parse all of them using do_pending_defargs. Since yacc
178 parsers are not reentrant, we retain defargs state in these two
179 variables so that subsequent calls to do_pending_defargs can resume
180 where the previous call left off. DEFARG_FNS is a tree_list where
181 the TREE_TYPE is the current_class_type, TREE_VALUE is the FUNCTION_DECL,
182 and TREE_PURPOSE is the list unprocessed dependent functions. */
184 /* list of functions with unprocessed defargs */
185 static GTY(()) tree defarg_fns
;
186 /* current default parameter */
187 static GTY(()) tree defarg_parm
;
188 /* list of unprocessed fns met during current fn. */
189 static GTY(()) tree defarg_depfns
;
190 /* list of fns with circular defargs */
191 static GTY(()) tree defarg_fnsdone
;
193 /* Initialize obstacks. Called once, from cxx_init. */
198 gcc_obstack_init (&token_obstack
);
201 /* Subroutine of read_token. */
202 static SPEW_INLINE
int
203 read_process_identifier (pyylval
)
206 tree id
= pyylval
->ttype
;
208 if (C_IS_RESERVED_WORD (id
))
210 /* Possibly replace the IDENTIFIER_NODE with a magic cookie.
211 Can't put yylval.code numbers in ridpointers[]. Bleah. */
213 switch (C_RID_CODE (id
))
215 case RID_BITAND
: pyylval
->code
= BIT_AND_EXPR
; return '&';
216 case RID_AND_EQ
: pyylval
->code
= BIT_AND_EXPR
; return ASSIGN
;
217 case RID_BITOR
: pyylval
->code
= BIT_IOR_EXPR
; return '|';
218 case RID_OR_EQ
: pyylval
->code
= BIT_IOR_EXPR
; return ASSIGN
;
219 case RID_XOR
: pyylval
->code
= BIT_XOR_EXPR
; return '^';
220 case RID_XOR_EQ
: pyylval
->code
= BIT_XOR_EXPR
; return ASSIGN
;
221 case RID_NOT_EQ
: pyylval
->code
= NE_EXPR
; return EQCOMPARE
;
224 pyylval
->ttype
= ridpointers
[C_RID_CODE (id
)];
225 return C_RID_YYCODE (id
);
229 /* Make sure that user does not collide with our internal naming
230 scheme. This is not necessary if '.' is used to remove them from
231 the user's namespace, but is if '$' or double underscores are. */
233 #if !defined(JOINER) || JOINER == '$'
235 || VTABLE_NAME_P (id
)
237 || ANON_AGGRNAME_P (id
))
239 "identifier name `%s' conflicts with GNU C++ internal naming strategy",
240 IDENTIFIER_POINTER (id
));
245 /* Concatenate strings before returning them to the parser. This isn't quite
246 as good as having it done in the lexer, but it's better than nothing. */
252 enum cpp_ttype next_type
;
255 next_type
= c_lex (&next
);
256 if (next_type
== CPP_STRING
|| next_type
== CPP_WSTRING
)
260 VARRAY_TREE_INIT (strings
, 32, "strings");
261 VARRAY_PUSH_TREE (strings
, t
->yylval
.ttype
);
265 VARRAY_PUSH_TREE (strings
, next
);
266 next_type
= c_lex (&next
);
268 while (next_type
== CPP_STRING
|| next_type
== CPP_WSTRING
);
270 t
->yylval
.ttype
= combine_strings (strings
);
271 last_token_id
= t
->yylval
.ttype
;
274 /* We will have always read one token too many. */
275 _cpp_backup_tokens (parse_in
, 1);
280 /* Read the next token from the input file. The token is written into
281 T, and its type number is returned. */
288 last_token
= c_lex (&last_token_id
);
289 t
->yylval
.ttype
= last_token_id
;
293 #define YYCHAR(YY) t->yychar = (YY); break;
294 #define YYCODE(C) t->yylval.code = (C);
296 case CPP_EQ
: YYCHAR('=');
297 case CPP_NOT
: YYCHAR('!');
298 case CPP_GREATER
: YYCODE(GT_EXPR
); YYCHAR('>');
299 case CPP_LESS
: YYCODE(LT_EXPR
); YYCHAR('<');
300 case CPP_PLUS
: YYCODE(PLUS_EXPR
); YYCHAR('+');
301 case CPP_MINUS
: YYCODE(MINUS_EXPR
); YYCHAR('-');
302 case CPP_MULT
: YYCODE(MULT_EXPR
); YYCHAR('*');
303 case CPP_DIV
: YYCODE(TRUNC_DIV_EXPR
); YYCHAR('/');
304 case CPP_MOD
: YYCODE(TRUNC_MOD_EXPR
); YYCHAR('%');
305 case CPP_AND
: YYCODE(BIT_AND_EXPR
); YYCHAR('&');
306 case CPP_OR
: YYCODE(BIT_IOR_EXPR
); YYCHAR('|');
307 case CPP_XOR
: YYCODE(BIT_XOR_EXPR
); YYCHAR('^');
308 case CPP_RSHIFT
: YYCODE(RSHIFT_EXPR
); YYCHAR(RSHIFT
);
309 case CPP_LSHIFT
: YYCODE(LSHIFT_EXPR
); YYCHAR(LSHIFT
);
311 case CPP_COMPL
: YYCHAR('~');
312 case CPP_AND_AND
: YYCHAR(ANDAND
);
313 case CPP_OR_OR
: YYCHAR(OROR
);
314 case CPP_QUERY
: YYCHAR('?');
315 case CPP_COLON
: YYCHAR(':');
316 case CPP_COMMA
: YYCHAR(',');
317 case CPP_OPEN_PAREN
: YYCHAR('(');
318 case CPP_CLOSE_PAREN
: YYCHAR(')');
319 case CPP_EQ_EQ
: YYCODE(EQ_EXPR
); YYCHAR(EQCOMPARE
);
320 case CPP_NOT_EQ
: YYCODE(NE_EXPR
); YYCHAR(EQCOMPARE
);
321 case CPP_GREATER_EQ
:YYCODE(GE_EXPR
); YYCHAR(ARITHCOMPARE
);
322 case CPP_LESS_EQ
: YYCODE(LE_EXPR
); YYCHAR(ARITHCOMPARE
);
324 case CPP_PLUS_EQ
: YYCODE(PLUS_EXPR
); YYCHAR(ASSIGN
);
325 case CPP_MINUS_EQ
: YYCODE(MINUS_EXPR
); YYCHAR(ASSIGN
);
326 case CPP_MULT_EQ
: YYCODE(MULT_EXPR
); YYCHAR(ASSIGN
);
327 case CPP_DIV_EQ
: YYCODE(TRUNC_DIV_EXPR
); YYCHAR(ASSIGN
);
328 case CPP_MOD_EQ
: YYCODE(TRUNC_MOD_EXPR
); YYCHAR(ASSIGN
);
329 case CPP_AND_EQ
: YYCODE(BIT_AND_EXPR
); YYCHAR(ASSIGN
);
330 case CPP_OR_EQ
: YYCODE(BIT_IOR_EXPR
); YYCHAR(ASSIGN
);
331 case CPP_XOR_EQ
: YYCODE(BIT_XOR_EXPR
); YYCHAR(ASSIGN
);
332 case CPP_RSHIFT_EQ
: YYCODE(RSHIFT_EXPR
); YYCHAR(ASSIGN
);
333 case CPP_LSHIFT_EQ
: YYCODE(LSHIFT_EXPR
); YYCHAR(ASSIGN
);
335 case CPP_OPEN_SQUARE
: YYCHAR('[');
336 case CPP_CLOSE_SQUARE
: YYCHAR(']');
337 case CPP_OPEN_BRACE
: YYCHAR('{');
338 case CPP_CLOSE_BRACE
: YYCHAR('}');
339 case CPP_SEMICOLON
: YYCHAR(';');
340 case CPP_ELLIPSIS
: YYCHAR(ELLIPSIS
);
342 case CPP_PLUS_PLUS
: YYCHAR(PLUSPLUS
);
343 case CPP_MINUS_MINUS
: YYCHAR(MINUSMINUS
);
344 case CPP_DEREF
: YYCHAR(POINTSAT
);
345 case CPP_DOT
: YYCHAR('.');
347 /* These tokens are C++ specific. */
348 case CPP_SCOPE
: YYCHAR(SCOPE
);
349 case CPP_DEREF_STAR
: YYCHAR(POINTSAT_STAR
);
350 case CPP_DOT_STAR
: YYCHAR(DOT_STAR
);
351 case CPP_MIN_EQ
: YYCODE(MIN_EXPR
); YYCHAR(ASSIGN
);
352 case CPP_MAX_EQ
: YYCODE(MAX_EXPR
); YYCHAR(ASSIGN
);
353 case CPP_MIN
: YYCODE(MIN_EXPR
); YYCHAR(MIN_MAX
);
354 case CPP_MAX
: YYCODE(MAX_EXPR
); YYCHAR(MIN_MAX
);
363 t
->yychar
= read_process_identifier (&t
->yylval
);
369 t
->yychar
= CONSTANT
;
378 yyerror ("parse error");
388 struct unparsed_text
*input
;
396 f
= ggc_alloc (sizeof (struct feed
));
398 input
->cur_chunk
= input
->tokens
;
403 fprintf (stderr
, "\tfeeding %s:%d [%d tokens]\n",
404 input
->locus
.file
, input
->locus
.line
, input
->limit
- input
->pos
);
408 f
->locus
.file
= input_filename
;
409 f
->locus
.line
= lineno
;
412 f
->first_token
= first_token
;
413 f
->token_obstack
= token_obstack
;
416 input_filename
= input
->locus
.file
;
417 lineno
= input
->locus
.line
;
419 yylval
.ttype
= NULL_TREE
;
421 gcc_obstack_init (&token_obstack
);
428 struct feed
*f
= feed
;
430 input_filename
= f
->locus
.file
;
431 lineno
= f
->locus
.line
;
434 first_token
= f
->first_token
;
435 obstack_free (&token_obstack
, 0);
436 token_obstack
= f
->token_obstack
;
441 fprintf (stderr
, "\treturning to %s:%d\n", input_filename
, lineno
);
445 /* Token queue management. */
447 /* Return the number of tokens available on the fifo. */
448 static SPEW_INLINE
int
451 return (obstack_object_size (&token_obstack
) / sizeof (struct token
))
455 /* Fetch the token N down the line from the head of the fifo. */
457 static SPEW_INLINE
struct token
*
461 #ifdef ENABLE_CHECKING
462 /* could just have this do slurp_ implicitly, but this way is easier
464 my_friendly_assert (n
>= 0 && n
< num_tokens (), 298);
466 return ((struct token
*)obstack_base (&token_obstack
)) + n
+ first_token
;
469 static const struct token Teosi
= { END_OF_SAVED_INPUT
, 0 UNION_INIT_ZERO
};
470 static const struct token Tpad
= { EMPTY
, 0 UNION_INIT_ZERO
};
472 /* Copy the next token into T and return its value. */
473 static SPEW_INLINE
int
478 return read_token (t
);
480 if (feed
->input
->cur_chunk
!= feed
->input
->last_chunk
481 || feed
->input
->cur_pos
!= feed
->input
->last_pos
)
483 if (feed
->input
->cur_pos
== TOKEN_CHUNK_SIZE
)
485 feed
->input
->cur_chunk
= feed
->input
->cur_chunk
->next
;
486 feed
->input
->cur_pos
= 0;
488 memcpy (t
, feed
->input
->cur_chunk
->toks
+ feed
->input
->cur_pos
,
489 sizeof (struct token
));
490 feed
->input
->cur_pos
++;
494 memcpy (t
, &Teosi
, sizeof (struct token
));
495 return END_OF_SAVED_INPUT
;
498 /* Shift the next token onto the fifo. */
499 static SPEW_INLINE
int
502 size_t point
= obstack_object_size (&token_obstack
);
503 obstack_blank (&token_obstack
, sizeof (struct token
));
504 return next_token ((struct token
*) (obstack_base (&token_obstack
) + point
));
507 /* Consume the next token out of the fifo. */
509 static SPEW_INLINE
void
512 if (num_tokens () == 1)
514 obstack_free (&token_obstack
, obstack_base (&token_obstack
));
521 /* Push a token at the head of the queue; it will be the next token read. */
522 static SPEW_INLINE
void
526 if (first_token
== 0) /* We hope this doesn't happen often. */
528 size_t active
= obstack_object_size (&token_obstack
);
529 obstack_blank (&token_obstack
, sizeof (struct token
));
531 memmove (obstack_base (&token_obstack
) + sizeof (struct token
),
532 obstack_base (&token_obstack
), active
);
536 memcpy (nth_token (0), t
, sizeof (struct token
));
540 /* Pull in enough tokens that the queue is N long beyond the current
548 int num
= num_tokens ();
551 /* First, prune any empty tokens at the end. */
553 while (i
> 0 && nth_token (i
- 1)->yychar
== EMPTY
)
557 obstack_blank (&token_obstack
, -((num
- i
) * sizeof (struct token
)));
561 /* Now, if we already have enough tokens, return. */
565 /* Never read past these characters: they might separate
566 the current input stream from one we save away later. */
567 for (i
= 0; i
< num
; i
++)
569 yychar
= nth_token (i
)->yychar
;
570 if (yychar
== '{' || yychar
== ':' || yychar
== ';')
574 while (num_tokens () <= n
)
576 yychar
= shift_token ();
577 if (yychar
== '{' || yychar
== ':' || yychar
== ';')
583 while (num_tokens () <= n
)
584 obstack_grow (&token_obstack
, &Tpad
, sizeof (struct token
));
587 int looking_for_typename
;
588 int looking_for_template
;
590 static int after_friend
;
591 static int after_new
;
592 static int do_snarf_defarg
;
597 static SPEW_INLINE
int
598 identifier_type (decl
)
603 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
605 if (TREE_CODE (DECL_TEMPLATE_RESULT (decl
)) == TYPE_DECL
)
607 else if (looking_for_template
)
610 if (looking_for_template
&& really_overloaded_fn (decl
))
612 /* See through a baselink. */
613 if (TREE_CODE (decl
) == TREE_LIST
)
614 decl
= TREE_VALUE (decl
);
616 for (t
= decl
; t
!= NULL_TREE
; t
= OVL_CHAIN (t
))
617 if (DECL_FUNCTION_TEMPLATE_P (OVL_FUNCTION (t
)))
620 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
622 if (TREE_CODE (decl
) != TYPE_DECL
)
624 if (DECL_ARTIFICIAL (decl
) && TREE_TYPE (decl
) == current_class_type
)
627 /* A constructor declarator for a template type will get here as an
628 implicit typename, a TYPENAME_TYPE with a type. */
630 if (t
&& TREE_CODE (t
) == TYPENAME_TYPE
)
632 decl
= TREE_TYPE (decl
);
633 if (TREE_CODE (decl
) == TYPENAME_TYPE
)
634 decl
= TREE_TYPE (decl
);
641 /* token[0] == AGGR (struct/union/enum)
642 Thus, token[1] is either a tTYPENAME or a TYPENAME_DEFN.
643 If token[2] == '{' or ':' then it's TYPENAME_DEFN.
644 It's also a definition if it's a forward declaration (as in 'struct Foo;')
645 which we can tell if token[2] == ';' *and* token[-1] != FRIEND or NEW. */
647 static SPEW_INLINE
void
653 yc1
= nth_token (1)->yychar
;
654 if (yc1
!= tTYPENAME
&& yc1
!= IDENTIFIER
&& yc1
!= PTYPENAME
)
656 yc2
= nth_token (2)->yychar
;
659 /* It's a forward declaration iff we were not preceded by
660 'friend' or `new'. */
661 if (after_friend
|| after_new
)
664 else if (yc2
!= '{' && yc2
!= ':')
670 nth_token (1)->yychar
= TYPENAME_DEFN
;
673 nth_token (1)->yychar
= PTYPENAME_DEFN
;
676 nth_token (1)->yychar
= IDENTIFIER_DEFN
;
686 /* Only types expected, not even namespaces. */
687 looking_for_typename
= 2;
689 if ((yychar
= yylex ()) < 0) yychar
= 0;
690 looking_for_typename
= 0;
691 if (yychar
== IDENTIFIER
)
693 lastiddecl
= lookup_name (yylval
.ttype
, -2);
695 yychar
= identifier_type (lastiddecl
);
703 int old_looking_for_typename
= 0;
704 int just_saw_new
= 0;
705 int just_saw_friend
= 0;
707 timevar_push (TV_LEX
);
714 fprintf (stderr
, "\t\t## %d @%d ", yylex_ctr
, lineno
);
721 yylval
.ttype
= snarf_defarg ();
723 got_object
= NULL_TREE
;
724 timevar_pop (TV_LEX
);
728 /* if we've got tokens, send them */
729 else if (num_tokens ())
730 yychr
= nth_token (0)->yychar
;
732 yychr
= shift_token ();
734 /* many tokens just need to be returned. At first glance, all we
735 have to do is send them back up, but some of them are needed to
736 figure out local context. */
740 /* This is a lexical no-op. */
743 debug_yychar (yychr
);
750 if (nth_token (1)->yychar
== ')')
762 peek
= nth_token (1)->yychar
;
763 yychr
= frob_id (yychr
, peek
, &nth_token (0)->yylval
.ttype
);
766 case IDENTIFIER_DEFN
:
771 /* If we see a SCOPE next, restore the old value.
772 Otherwise, we got what we want. */
773 looking_for_typename
= old_looking_for_typename
;
774 looking_for_template
= 0;
778 if (nth_token (0)->yylval
.ttype
== ridpointers
[RID_EXTERN
])
781 if (nth_token (1)->yychar
== STRING
)
783 yychr
= EXTERN_LANG_STRING
;
784 nth_token (1)->yylval
.ttype
= get_identifier
785 (TREE_STRING_POINTER (nth_token (1)->yylval
.ttype
));
789 /* do_aggr needs to know if the previous token was `friend'. */
790 else if (nth_token (0)->yylval
.ttype
== ridpointers
[RID_FRIEND
])
796 /* do_aggr needs to know if the previous token was `new'. */
804 /* If this provides a type for us, then revert lexical
805 state to standard state. */
806 looking_for_typename
= 0;
814 /* Set this again, in case we are rescanning. */
815 looking_for_typename
= 2;
822 after_friend
= just_saw_friend
;
823 after_new
= just_saw_new
;
825 /* class member lookup only applies to the first token after the object
826 expression, except for explicit destructor calls. */
828 got_object
= NULL_TREE
;
832 struct token
*tok
= nth_token (0);
834 yylval
= tok
->yylval
;
836 lineno
= tok
->lineno
;
841 debug_yychar (yychr
);
845 timevar_pop (TV_LEX
);
849 /* Unget character CH from the input stream.
850 If RESCAN is non-zero, then we want to `see' this
851 character as the next input token. */
854 yyungetc (ch
, rescan
)
858 /* Unget a character from the input stream. */
859 if (yychar
== YYEMPTY
|| rescan
== 0)
864 fake
.yylval
.ttype
= 0;
865 fake
.lineno
= lineno
;
875 /* Lexer hackery to determine what *IDP really is. */
878 frob_id (yyc
, peek
, idp
)
884 int old_looking_for_typename
= 0;
888 /* Don't interfere with the setting from an 'aggr' prefix. */
889 old_looking_for_typename
= looking_for_typename
;
890 looking_for_typename
= 1;
892 else if (peek
== '<')
893 looking_for_template
= 1;
894 trrr
= lookup_name (*idp
, -2);
897 yyc
= identifier_type (trrr
);
904 /* If this got special lookup, remember it. In these
905 cases, we know it can't be a declarator-id. */
906 if (got_scope
|| got_object
)
918 lastiddecl
= NULL_TREE
;
919 got_scope
= NULL_TREE
;
920 looking_for_typename
= old_looking_for_typename
;
921 looking_for_template
= 0;
925 /* ID is an operator name. Duplicate the hackery in yylex to determine what
928 tree
frob_opname (id
)
932 frob_id (0, nth_token (0)->yychar
, &id
);
933 got_object
= NULL_TREE
;
937 /* Set up the state required to correctly handle the definition of the
938 inline function whose preparsed state has been saved in PI. */
941 begin_parsing_inclass_inline (pi
)
942 struct unparsed_text
*pi
;
946 /* Record that we are processing the chain of inlines starting at
949 cp_function_chain
->unparsed_inlines
= pi
;
951 processing_these_inlines
= pi
;
955 /* If this is an inline function in a local class, we must make sure
956 that we save all pertinent information about the function
957 surrounding the local class. */
958 context
= decl_function_context (pi
->decl
);
960 push_function_context_to (context
);
963 interface_unknown
= pi
->interface
== 1;
964 interface_only
= pi
->interface
== 0;
965 DECL_PENDING_INLINE_P (pi
->decl
) = 0;
966 DECL_PENDING_INLINE_INFO (pi
->decl
) = 0;
968 /* Pass back a handle to the rest of the inline functions, so that they
969 can be processed later. */
970 yychar
= PRE_PARSED_FUNCTION_DECL
;
973 start_function (NULL_TREE
, pi
->decl
, NULL_TREE
,
974 (SF_DEFAULT
| SF_PRE_PARSED
| SF_INCLASS_INLINE
));
977 /* Called from the top level: if there are any pending inlines to
978 do, set up to process them now. This function sets up the first function
979 to be parsed; after it has been, the rule for fndef in parse.y will
980 call process_next_inline to start working on the next one. */
983 do_pending_inlines ()
985 /* Oops, we're still dealing with the last batch. */
986 if (yychar
== PRE_PARSED_FUNCTION_DECL
)
991 /* Clear the chain, so that any inlines nested inside the batch
992 we're to process now don't refer to this batch. See e.g.
993 g++.other/lookup6.C. */
994 struct unparsed_text
*first
= pending_inlines
;
995 pending_inlines
= pending_inlines_tail
= 0;
997 begin_parsing_inclass_inline (first
);
1001 /* Called from the fndecl rule in the parser when the function just parsed
1002 was declared using a PRE_PARSED_FUNCTION_DECL (i.e. came from
1003 do_pending_inlines). */
1006 process_next_inline (i
)
1007 struct unparsed_text
*i
;
1009 tree decl
= i
->decl
;
1010 tree context
= decl_function_context (decl
);
1013 pop_function_context_from (context
);
1014 if (yychar
== YYEMPTY
)
1016 if (yychar
!= END_OF_SAVED_INPUT
)
1017 error ("parse error at end of saved function text");
1022 begin_parsing_inclass_inline (i
);
1026 cp_function_chain
->unparsed_inlines
= 0;
1028 processing_these_inlines
= 0;
1029 extract_interface_info ();
1033 /* Create a new token at the end of the token list in T. */
1034 static SPEW_INLINE
struct token
*
1036 struct unparsed_text
*t
;
1038 if (t
->last_pos
!= TOKEN_CHUNK_SIZE
)
1039 return t
->last_chunk
->toks
+ (t
->last_pos
++);
1041 t
->last_chunk
->next
= ggc_alloc (sizeof (*t
->last_chunk
->next
));
1042 t
->last_chunk
= t
->last_chunk
->next
;
1043 t
->last_chunk
->next
= NULL
;
1046 return t
->last_chunk
->toks
;
1049 /* Shrink the token list in T by one token. */
1050 static SPEW_INLINE
struct token
*
1051 remove_last_token (t
)
1052 struct unparsed_text
*t
;
1054 struct token
*result
= t
->last_chunk
->toks
+ t
->last_pos
- 1;
1055 if (t
->last_pos
== 0)
1058 if (t
->last_pos
== 0 && t
->last_chunk
!= t
->tokens
)
1060 struct token_chunk
**tc
;
1061 for (tc
= &t
->tokens
; (*tc
)->next
!= NULL
; tc
= &(*tc
)->next
)
1064 t
->last_pos
= ARRAY_SIZE ((*tc
)->toks
);
1069 /* Allocate an 'unparsed_text' structure, ready to use space_for_token. */
1070 static struct unparsed_text
*
1071 alloc_unparsed_text (locus
, decl
, interface
)
1072 const location_t
*locus
;
1076 struct unparsed_text
*r
;
1077 r
= ggc_alloc_cleared (sizeof (*r
));
1080 r
->interface
= interface
;
1081 r
->tokens
= r
->last_chunk
= ggc_alloc_cleared (sizeof (*r
->tokens
));
1085 /* Subroutine of snarf_method, deals with actual absorption of the block. */
1089 struct unparsed_text
*t
;
1092 int look_for_semicolon
= 0;
1093 int look_for_lbrac
= 0;
1094 int look_for_catch
= 0;
1096 struct token
*current
;
1100 else if (yychar
== '=')
1101 look_for_semicolon
= 1;
1102 else if (yychar
== ':' || yychar
== RETURN_KEYWORD
|| yychar
== TRY
)
1110 yyerror ("parse error in method specification");
1112 /* The current token is the first one to be recorded. */
1113 current
= space_for_token (t
);
1114 current
->yychar
= yychar
;
1115 current
->yylval
= yylval
;
1116 current
->lineno
= lineno
;
1120 yyc
= next_token (space_for_token (t
));
1127 else if (yyc
== '}')
1130 if (blev
== 0 && !look_for_semicolon
)
1132 if (!look_for_catch
)
1135 if (next_token (space_for_token (t
)) != CATCH
)
1137 push_token (remove_last_token (t
));
1144 else if (yyc
== ';')
1150 error ("function body for constructor missing");
1151 /* fake a { } to avoid further errors */
1152 fake
= space_for_token (t
);
1153 fake
->yylval
.ttype
= 0;
1155 fake
= space_for_token (t
);
1156 fake
->yylval
.ttype
= 0;
1160 else if (look_for_semicolon
&& blev
== 0)
1165 error ("%Hend of file read inside definition", &t
->locus
);
1171 /* This function stores away the text for an inline function that should
1172 be processed later (by do_pending_inlines). */
1177 struct unparsed_text
*meth
;
1178 location_t starting
;
1179 starting
.file
= input_filename
;
1180 starting
.line
= lineno
;
1182 meth
= alloc_unparsed_text (&starting
, decl
, (interface_unknown
? 1
1183 : (interface_only
? 0 : 2)));
1187 /* Happens when we get two declarations of the same function in the
1189 if (decl
== void_type_node
1190 || (current_class_type
&& TYPE_REDEFINED (current_class_type
)))
1195 fprintf (stderr
, "\tsaved method of %d tokens from %s:%d\n",
1196 meth
->limit
, starting
.file
, starting
.line
);
1199 DECL_PENDING_INLINE_INFO (decl
) = meth
;
1200 DECL_PENDING_INLINE_P (decl
) = 1;
1202 if (pending_inlines_tail
)
1203 pending_inlines_tail
->next
= meth
;
1205 pending_inlines
= meth
;
1206 pending_inlines_tail
= meth
;
1209 /* Consume a no-commas expression - a default argument - and return
1210 a DEFAULT_ARG tree node. */
1217 struct unparsed_text
*buf
;
1219 location_t starting
;
1220 starting
.file
= input_filename
;
1221 starting
.line
= lineno
;
1223 buf
= alloc_unparsed_text (&starting
, 0, 0);
1227 yyc
= next_token (space_for_token (buf
));
1229 if (plev
<= 0 && (yyc
== ')' || yyc
== ','))
1231 else if (yyc
== '(' || yyc
== '[')
1233 else if (yyc
== ']' || yyc
== ')')
1237 error ("%Hend of file read inside default argument", &starting
);
1242 /* Unget the last token. */
1243 push_token (remove_last_token (buf
));
1248 fprintf (stderr
, "\tsaved defarg of %d tokens from %s:%d\n",
1249 buf
->limit
, starting
.file
, starting
.line
);
1252 arg
= make_node (DEFAULT_ARG
);
1253 DEFARG_POINTER (arg
) = (char *)buf
;
1258 /* Decide whether the default argument we are about to see should be
1259 gobbled up as text for later parsing. */
1262 maybe_snarf_defarg ()
1264 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
1265 do_snarf_defarg
= 1;
1268 /* Called from grokfndecl to note a function decl with unparsed default
1269 arguments for later processing. Also called from grokdeclarator
1270 for function types with unparsed defargs; the call from grokfndecl
1271 will always come second, so we can overwrite the entry from the type. */
1274 add_defarg_fn (decl
)
1277 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1278 TREE_VALUE (defarg_fns
) = decl
;
1281 defarg_fns
= tree_cons (NULL_TREE
, decl
, defarg_fns
);
1282 TREE_TYPE (defarg_fns
) = current_class_type
;
1286 /* Helper for do_pending_defargs. Starts the parsing of a default arg. */
1292 tree d
= TREE_PURPOSE (p
);
1294 feed_input ((struct unparsed_text
*)DEFARG_POINTER (d
));
1295 yychar
= DEFARG_MARKER
;
1299 /* Helper for do_pending_defargs. Ends the parsing of a default arg. */
1304 if (yychar
== YYEMPTY
)
1306 if (yychar
!= END_OF_SAVED_INPUT
)
1307 error ("parse error at end of saved function text");
1312 /* Main function for deferred parsing of default arguments. Called from
1316 do_pending_defargs ()
1323 tree current
= defarg_fns
;
1325 tree defarg_fn
= TREE_VALUE (defarg_fns
);
1326 if (defarg_parm
== NULL_TREE
)
1328 push_nested_class (TREE_TYPE (defarg_fns
), 1);
1330 if (TREE_CODE (defarg_fn
) == FUNCTION_DECL
)
1331 maybe_begin_member_template_processing (defarg_fn
);
1333 if (TREE_CODE (defarg_fn
) == FUNCTION_DECL
)
1334 defarg_parm
= TYPE_ARG_TYPES (TREE_TYPE (defarg_fn
));
1336 defarg_parm
= TYPE_ARG_TYPES (defarg_fn
);
1339 defarg_parm
= TREE_CHAIN (defarg_parm
);
1341 for (; defarg_parm
; defarg_parm
= TREE_CHAIN (defarg_parm
))
1342 if (!TREE_PURPOSE (defarg_parm
)
1343 || TREE_CODE (TREE_PURPOSE (defarg_parm
)) != DEFAULT_ARG
)
1345 else if (TREE_PURPOSE (current
) == error_mark_node
)
1346 DEFARG_POINTER (TREE_PURPOSE (defarg_parm
)) = NULL
;
1349 feed_defarg (defarg_parm
);
1351 /* Return to the parser, which will process this defarg
1352 and call us again. */
1356 if (TREE_CODE (defarg_fn
) == FUNCTION_DECL
)
1358 maybe_end_member_template_processing ();
1359 check_default_args (defarg_fn
);
1363 pop_nested_class ();
1365 defarg_fns
= TREE_CHAIN (defarg_fns
);
1368 /* This function's default args depend on unprocessed default args
1369 of defarg_fns. We will need to reprocess this function, and
1370 check for circular dependencies. */
1373 for (a
= defarg_depfns
, b
= TREE_PURPOSE (current
); a
&& b
;
1374 a
= TREE_CHAIN (a
), b
= TREE_CHAIN (b
))
1375 if (TREE_VALUE (a
) != TREE_VALUE (b
))
1380 TREE_CHAIN (current
) = NULL_TREE
;
1381 defarg_fns
= chainon (defarg_fns
, current
);
1382 TREE_PURPOSE (current
) = defarg_depfns
;
1386 cp_warning_at ("circular dependency in default args of `%#D'", defarg_fn
);
1387 /* No need to say what else is dependent, as they will be
1388 picked up in another pass. */
1390 /* Immediately repeat, but marked so that we break the loop. */
1391 defarg_fns
= current
;
1392 TREE_PURPOSE (current
) = error_mark_node
;
1394 defarg_depfns
= NULL_TREE
;
1396 else if (TREE_PURPOSE (current
) == error_mark_node
)
1397 defarg_fnsdone
= tree_cons (NULL_TREE
, defarg_fn
, defarg_fnsdone
);
1401 /* After parsing all the default arguments, we must clear any that remain,
1402 which will be part of a circular dependency. */
1404 done_pending_defargs ()
1406 for (; defarg_fnsdone
; defarg_fnsdone
= TREE_CHAIN (defarg_fnsdone
))
1408 tree fn
= TREE_VALUE (defarg_fnsdone
);
1411 if (TREE_CODE (fn
) == FUNCTION_DECL
)
1412 parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1414 parms
= TYPE_ARG_TYPES (fn
);
1415 for (; parms
; parms
= TREE_CHAIN (parms
))
1416 if (TREE_PURPOSE (parms
)
1417 && TREE_CODE (TREE_PURPOSE (parms
)) == DEFAULT_ARG
)
1419 my_friendly_assert (!DEFARG_POINTER (TREE_PURPOSE (parms
)), 20010107);
1420 TREE_PURPOSE (parms
) = NULL_TREE
;
1425 /* In processing the current default arg, we called FN, but that call
1426 required a default argument of FN, and that had not yet been processed.
1430 unprocessed_defarg_fn (fn
)
1433 defarg_depfns
= tree_cons (NULL_TREE
, fn
, defarg_depfns
);
1436 /* Called from the parser to update an element of TYPE_ARG_TYPES for some
1437 FUNCTION_TYPE with the newly parsed version of its default argument, which
1438 was previously digested as text. */
1441 replace_defarg (arg
, init
)
1444 if (init
== error_mark_node
)
1445 TREE_PURPOSE (arg
) = error_mark_node
;
1448 if (! processing_template_decl
1449 && ! can_convert_arg (TREE_VALUE (arg
), TREE_TYPE (init
), init
))
1450 pedwarn ("invalid type `%T' for default argument to `%T'",
1451 TREE_TYPE (init
), TREE_VALUE (arg
));
1453 TREE_PURPOSE (arg
) = init
;
1458 /* debug_yychar takes a yychar (token number) value and prints its name. */
1465 fprintf (stderr
, "->%d < %c >\n", lineno
, yy
);
1466 else if (yy
== IDENTIFIER
|| yy
== tTYPENAME
)
1469 if (TREE_CODE (yylval
.ttype
) == IDENTIFIER_NODE
)
1470 id
= IDENTIFIER_POINTER (yylval
.ttype
);
1471 else if (TREE_CODE_CLASS (TREE_CODE (yylval
.ttype
)) == 'd')
1472 id
= IDENTIFIER_POINTER (DECL_NAME (yylval
.ttype
));
1475 fprintf (stderr
, "->%d <%s `%s'>\n", lineno
, debug_yytranslate (yy
), id
);
1478 fprintf (stderr
, "->%d <%s>\n", lineno
, debug_yytranslate (yy
));
1483 #define NAME(TYPE) cpp_type2name (TYPE)
1489 const char *string
= _(msgid
);
1491 if (last_token
== CPP_EOF
)
1492 error ("%s at end of input", string
);
1493 else if (last_token
== CPP_CHAR
|| last_token
== CPP_WCHAR
)
1495 unsigned int val
= TREE_INT_CST_LOW (yylval
.ttype
);
1496 const char *const ell
= (last_token
== CPP_CHAR
) ? "" : "L";
1497 if (val
<= UCHAR_MAX
&& ISGRAPH (val
))
1498 error ("%s before %s'%c'", string
, ell
, val
);
1500 error ("%s before %s'\\x%x'", string
, ell
, val
);
1502 else if (last_token
== CPP_STRING
1503 || last_token
== CPP_WSTRING
)
1504 error ("%s before string constant", string
);
1505 else if (last_token
== CPP_NUMBER
)
1506 error ("%s before numeric constant", string
);
1507 else if (last_token
== CPP_NAME
)
1509 if (TREE_CODE (last_token_id
) == IDENTIFIER_NODE
)
1510 error ("%s before `%s'", string
, IDENTIFIER_POINTER (last_token_id
));
1511 else if (ISGRAPH (yychar
))
1512 error ("%s before `%c'", string
, yychar
);
1514 error ("%s before `\%o'", string
, yychar
);
1517 error ("%s before `%s' token", string
, NAME (last_token
));
1520 #include "gt-cp-spew.h"