]>
Commit | Line | Data |
---|---|---|
a723baf1 | 1 | /* C++ Parser. |
b0bc6e8e | 2 | Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. |
a723baf1 MM |
3 | Written by Mark Mitchell <mark@codesourcery.com>. |
4 | ||
f5adbb8d | 5 | This file is part of GCC. |
a723baf1 | 6 | |
f5adbb8d | 7 | GCC is free software; you can redistribute it and/or modify it |
a723baf1 MM |
8 | under the terms of the GNU General Public License as published by |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
f5adbb8d | 12 | GCC is distributed in the hope that it will be useful, but |
a723baf1 MM |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
f5adbb8d | 18 | along with GCC; see the file COPYING. If not, write to the Free |
a723baf1 MM |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
20 | 02111-1307, USA. */ | |
21 | ||
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
25 | #include "tm.h" | |
26 | #include "dyn-string.h" | |
27 | #include "varray.h" | |
28 | #include "cpplib.h" | |
29 | #include "tree.h" | |
30 | #include "cp-tree.h" | |
31 | #include "c-pragma.h" | |
32 | #include "decl.h" | |
33 | #include "flags.h" | |
34 | #include "diagnostic.h" | |
a723baf1 MM |
35 | #include "toplev.h" |
36 | #include "output.h" | |
37 | ||
38 | \f | |
39 | /* The lexer. */ | |
40 | ||
41 | /* Overview | |
42 | -------- | |
43 | ||
44 | A cp_lexer represents a stream of cp_tokens. It allows arbitrary | |
45 | look-ahead. | |
46 | ||
47 | Methodology | |
48 | ----------- | |
49 | ||
50 | We use a circular buffer to store incoming tokens. | |
51 | ||
52 | Some artifacts of the C++ language (such as the | |
53 | expression/declaration ambiguity) require arbitrary look-ahead. | |
54 | The strategy we adopt for dealing with these problems is to attempt | |
55 | to parse one construct (e.g., the declaration) and fall back to the | |
56 | other (e.g., the expression) if that attempt does not succeed. | |
57 | Therefore, we must sometimes store an arbitrary number of tokens. | |
58 | ||
59 | The parser routinely peeks at the next token, and then consumes it | |
60 | later. That also requires a buffer in which to store the tokens. | |
61 | ||
62 | In order to easily permit adding tokens to the end of the buffer, | |
63 | while removing them from the beginning of the buffer, we use a | |
64 | circular buffer. */ | |
65 | ||
66 | /* A C++ token. */ | |
67 | ||
68 | typedef struct cp_token GTY (()) | |
69 | { | |
70 | /* The kind of token. */ | |
df2b750f | 71 | ENUM_BITFIELD (cpp_ttype) type : 8; |
a723baf1 MM |
72 | /* If this token is a keyword, this value indicates which keyword. |
73 | Otherwise, this value is RID_MAX. */ | |
df2b750f | 74 | ENUM_BITFIELD (rid) keyword : 8; |
f4abade9 GB |
75 | /* Token flags. */ |
76 | unsigned char flags; | |
522df488 DN |
77 | /* The value associated with this token, if any. */ |
78 | tree value; | |
82a98427 NS |
79 | /* The location at which this token was found. */ |
80 | location_t location; | |
a723baf1 MM |
81 | } cp_token; |
82 | ||
522df488 DN |
83 | /* The number of tokens in a single token block. |
84 | Computed so that cp_token_block fits in a 512B allocation unit. */ | |
a723baf1 | 85 | |
522df488 | 86 | #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token)) |
a723baf1 MM |
87 | |
88 | /* A group of tokens. These groups are chained together to store | |
89 | large numbers of tokens. (For example, a token block is created | |
90 | when the body of an inline member function is first encountered; | |
91 | the tokens are processed later after the class definition is | |
92 | complete.) | |
93 | ||
94 | This somewhat ungainly data structure (as opposed to, say, a | |
34cd5ae7 | 95 | variable-length array), is used due to constraints imposed by the |
a723baf1 MM |
96 | current garbage-collection methodology. If it is made more |
97 | flexible, we could perhaps simplify the data structures involved. */ | |
98 | ||
99 | typedef struct cp_token_block GTY (()) | |
100 | { | |
101 | /* The tokens. */ | |
102 | cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS]; | |
103 | /* The number of tokens in this block. */ | |
104 | size_t num_tokens; | |
105 | /* The next token block in the chain. */ | |
106 | struct cp_token_block *next; | |
107 | /* The previous block in the chain. */ | |
108 | struct cp_token_block *prev; | |
109 | } cp_token_block; | |
110 | ||
111 | typedef struct cp_token_cache GTY (()) | |
112 | { | |
113 | /* The first block in the cache. NULL if there are no tokens in the | |
114 | cache. */ | |
115 | cp_token_block *first; | |
116 | /* The last block in the cache. NULL If there are no tokens in the | |
117 | cache. */ | |
118 | cp_token_block *last; | |
119 | } cp_token_cache; | |
120 | ||
9bcb9aae | 121 | /* Prototypes. */ |
a723baf1 MM |
122 | |
123 | static cp_token_cache *cp_token_cache_new | |
124 | (void); | |
125 | static void cp_token_cache_push_token | |
126 | (cp_token_cache *, cp_token *); | |
127 | ||
128 | /* Create a new cp_token_cache. */ | |
129 | ||
130 | static cp_token_cache * | |
bf9d3c27 | 131 | cp_token_cache_new (void) |
a723baf1 | 132 | { |
c68b0a84 | 133 | return ggc_alloc_cleared (sizeof (cp_token_cache)); |
a723baf1 MM |
134 | } |
135 | ||
136 | /* Add *TOKEN to *CACHE. */ | |
137 | ||
138 | static void | |
139 | cp_token_cache_push_token (cp_token_cache *cache, | |
140 | cp_token *token) | |
141 | { | |
142 | cp_token_block *b = cache->last; | |
143 | ||
144 | /* See if we need to allocate a new token block. */ | |
145 | if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS) | |
146 | { | |
c68b0a84 | 147 | b = ggc_alloc_cleared (sizeof (cp_token_block)); |
a723baf1 MM |
148 | b->prev = cache->last; |
149 | if (cache->last) | |
150 | { | |
151 | cache->last->next = b; | |
152 | cache->last = b; | |
153 | } | |
154 | else | |
155 | cache->first = cache->last = b; | |
156 | } | |
157 | /* Add this token to the current token block. */ | |
158 | b->tokens[b->num_tokens++] = *token; | |
159 | } | |
160 | ||
161 | /* The cp_lexer structure represents the C++ lexer. It is responsible | |
162 | for managing the token stream from the preprocessor and supplying | |
163 | it to the parser. */ | |
164 | ||
165 | typedef struct cp_lexer GTY (()) | |
166 | { | |
167 | /* The memory allocated for the buffer. Never NULL. */ | |
168 | cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer; | |
169 | /* A pointer just past the end of the memory allocated for the buffer. */ | |
170 | cp_token * GTY ((skip (""))) buffer_end; | |
171 | /* The first valid token in the buffer, or NULL if none. */ | |
172 | cp_token * GTY ((skip (""))) first_token; | |
173 | /* The next available token. If NEXT_TOKEN is NULL, then there are | |
174 | no more available tokens. */ | |
175 | cp_token * GTY ((skip (""))) next_token; | |
176 | /* A pointer just past the last available token. If FIRST_TOKEN is | |
177 | NULL, however, there are no available tokens, and then this | |
178 | location is simply the place in which the next token read will be | |
179 | placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full. | |
180 | When the LAST_TOKEN == BUFFER, then the last token is at the | |
181 | highest memory address in the BUFFER. */ | |
182 | cp_token * GTY ((skip (""))) last_token; | |
183 | ||
184 | /* A stack indicating positions at which cp_lexer_save_tokens was | |
185 | called. The top entry is the most recent position at which we | |
186 | began saving tokens. The entries are differences in token | |
187 | position between FIRST_TOKEN and the first saved token. | |
188 | ||
189 | If the stack is non-empty, we are saving tokens. When a token is | |
190 | consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN | |
191 | pointer will not. The token stream will be preserved so that it | |
192 | can be reexamined later. | |
193 | ||
194 | If the stack is empty, then we are not saving tokens. Whenever a | |
195 | token is consumed, the FIRST_TOKEN pointer will be moved, and the | |
196 | consumed token will be gone forever. */ | |
197 | varray_type saved_tokens; | |
198 | ||
199 | /* The STRING_CST tokens encountered while processing the current | |
200 | string literal. */ | |
201 | varray_type string_tokens; | |
202 | ||
203 | /* True if we should obtain more tokens from the preprocessor; false | |
204 | if we are processing a saved token cache. */ | |
205 | bool main_lexer_p; | |
206 | ||
207 | /* True if we should output debugging information. */ | |
208 | bool debugging_p; | |
209 | ||
210 | /* The next lexer in a linked list of lexers. */ | |
211 | struct cp_lexer *next; | |
212 | } cp_lexer; | |
213 | ||
214 | /* Prototypes. */ | |
215 | ||
17211ab5 | 216 | static cp_lexer *cp_lexer_new_main |
94edc4ab | 217 | (void); |
a723baf1 | 218 | static cp_lexer *cp_lexer_new_from_tokens |
94edc4ab | 219 | (struct cp_token_cache *); |
a723baf1 | 220 | static int cp_lexer_saving_tokens |
94edc4ab | 221 | (const cp_lexer *); |
a723baf1 | 222 | static cp_token *cp_lexer_next_token |
94edc4ab | 223 | (cp_lexer *, cp_token *); |
a668c6ad MM |
224 | static cp_token *cp_lexer_prev_token |
225 | (cp_lexer *, cp_token *); | |
94edc4ab NN |
226 | static ptrdiff_t cp_lexer_token_difference |
227 | (cp_lexer *, cp_token *, cp_token *); | |
a723baf1 | 228 | static cp_token *cp_lexer_read_token |
94edc4ab | 229 | (cp_lexer *); |
a723baf1 | 230 | static void cp_lexer_maybe_grow_buffer |
94edc4ab | 231 | (cp_lexer *); |
a723baf1 | 232 | static void cp_lexer_get_preprocessor_token |
94edc4ab | 233 | (cp_lexer *, cp_token *); |
a723baf1 | 234 | static cp_token *cp_lexer_peek_token |
94edc4ab | 235 | (cp_lexer *); |
a723baf1 | 236 | static cp_token *cp_lexer_peek_nth_token |
94edc4ab | 237 | (cp_lexer *, size_t); |
f7b5ecd9 | 238 | static inline bool cp_lexer_next_token_is |
94edc4ab | 239 | (cp_lexer *, enum cpp_ttype); |
a723baf1 | 240 | static bool cp_lexer_next_token_is_not |
94edc4ab | 241 | (cp_lexer *, enum cpp_ttype); |
a723baf1 | 242 | static bool cp_lexer_next_token_is_keyword |
94edc4ab NN |
243 | (cp_lexer *, enum rid); |
244 | static cp_token *cp_lexer_consume_token | |
245 | (cp_lexer *); | |
a723baf1 MM |
246 | static void cp_lexer_purge_token |
247 | (cp_lexer *); | |
248 | static void cp_lexer_purge_tokens_after | |
249 | (cp_lexer *, cp_token *); | |
250 | static void cp_lexer_save_tokens | |
94edc4ab | 251 | (cp_lexer *); |
a723baf1 | 252 | static void cp_lexer_commit_tokens |
94edc4ab | 253 | (cp_lexer *); |
a723baf1 | 254 | static void cp_lexer_rollback_tokens |
94edc4ab | 255 | (cp_lexer *); |
f7b5ecd9 | 256 | static inline void cp_lexer_set_source_position_from_token |
94edc4ab | 257 | (cp_lexer *, const cp_token *); |
a723baf1 | 258 | static void cp_lexer_print_token |
94edc4ab | 259 | (FILE *, cp_token *); |
f7b5ecd9 | 260 | static inline bool cp_lexer_debugging_p |
94edc4ab | 261 | (cp_lexer *); |
a723baf1 | 262 | static void cp_lexer_start_debugging |
94edc4ab | 263 | (cp_lexer *) ATTRIBUTE_UNUSED; |
a723baf1 | 264 | static void cp_lexer_stop_debugging |
94edc4ab | 265 | (cp_lexer *) ATTRIBUTE_UNUSED; |
a723baf1 MM |
266 | |
267 | /* Manifest constants. */ | |
268 | ||
269 | #define CP_TOKEN_BUFFER_SIZE 5 | |
270 | #define CP_SAVED_TOKENS_SIZE 5 | |
271 | ||
272 | /* A token type for keywords, as opposed to ordinary identifiers. */ | |
273 | #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1)) | |
274 | ||
275 | /* A token type for template-ids. If a template-id is processed while | |
276 | parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token; | |
277 | the value of the CPP_TEMPLATE_ID is whatever was returned by | |
278 | cp_parser_template_id. */ | |
279 | #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1)) | |
280 | ||
281 | /* A token type for nested-name-specifiers. If a | |
282 | nested-name-specifier is processed while parsing tentatively, it is | |
283 | replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the | |
284 | CPP_NESTED_NAME_SPECIFIER is whatever was returned by | |
285 | cp_parser_nested_name_specifier_opt. */ | |
286 | #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1)) | |
287 | ||
288 | /* A token type for tokens that are not tokens at all; these are used | |
289 | to mark the end of a token block. */ | |
290 | #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1) | |
291 | ||
292 | /* Variables. */ | |
293 | ||
294 | /* The stream to which debugging output should be written. */ | |
295 | static FILE *cp_lexer_debug_stream; | |
296 | ||
17211ab5 GK |
297 | /* Create a new main C++ lexer, the lexer that gets tokens from the |
298 | preprocessor. */ | |
a723baf1 MM |
299 | |
300 | static cp_lexer * | |
17211ab5 | 301 | cp_lexer_new_main (void) |
a723baf1 MM |
302 | { |
303 | cp_lexer *lexer; | |
17211ab5 GK |
304 | cp_token first_token; |
305 | ||
306 | /* It's possible that lexing the first token will load a PCH file, | |
307 | which is a GC collection point. So we have to grab the first | |
308 | token before allocating any memory. */ | |
309 | cp_lexer_get_preprocessor_token (NULL, &first_token); | |
18c81520 | 310 | c_common_no_more_pch (); |
a723baf1 MM |
311 | |
312 | /* Allocate the memory. */ | |
c68b0a84 | 313 | lexer = ggc_alloc_cleared (sizeof (cp_lexer)); |
a723baf1 MM |
314 | |
315 | /* Create the circular buffer. */ | |
c68b0a84 | 316 | lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token)); |
a723baf1 MM |
317 | lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE; |
318 | ||
17211ab5 GK |
319 | /* There is one token in the buffer. */ |
320 | lexer->last_token = lexer->buffer + 1; | |
321 | lexer->first_token = lexer->buffer; | |
322 | lexer->next_token = lexer->buffer; | |
323 | memcpy (lexer->buffer, &first_token, sizeof (cp_token)); | |
a723baf1 MM |
324 | |
325 | /* This lexer obtains more tokens by calling c_lex. */ | |
17211ab5 | 326 | lexer->main_lexer_p = true; |
a723baf1 MM |
327 | |
328 | /* Create the SAVED_TOKENS stack. */ | |
329 | VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens"); | |
330 | ||
331 | /* Create the STRINGS array. */ | |
332 | VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings"); | |
333 | ||
334 | /* Assume we are not debugging. */ | |
335 | lexer->debugging_p = false; | |
336 | ||
337 | return lexer; | |
338 | } | |
339 | ||
340 | /* Create a new lexer whose token stream is primed with the TOKENS. | |
341 | When these tokens are exhausted, no new tokens will be read. */ | |
342 | ||
343 | static cp_lexer * | |
344 | cp_lexer_new_from_tokens (cp_token_cache *tokens) | |
345 | { | |
346 | cp_lexer *lexer; | |
347 | cp_token *token; | |
348 | cp_token_block *block; | |
349 | ptrdiff_t num_tokens; | |
350 | ||
17211ab5 | 351 | /* Allocate the memory. */ |
c68b0a84 | 352 | lexer = ggc_alloc_cleared (sizeof (cp_lexer)); |
a723baf1 MM |
353 | |
354 | /* Create a new buffer, appropriately sized. */ | |
355 | num_tokens = 0; | |
356 | for (block = tokens->first; block != NULL; block = block->next) | |
357 | num_tokens += block->num_tokens; | |
c68b0a84 | 358 | lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token)); |
a723baf1 MM |
359 | lexer->buffer_end = lexer->buffer + num_tokens; |
360 | ||
361 | /* Install the tokens. */ | |
362 | token = lexer->buffer; | |
363 | for (block = tokens->first; block != NULL; block = block->next) | |
364 | { | |
365 | memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token)); | |
366 | token += block->num_tokens; | |
367 | } | |
368 | ||
369 | /* The FIRST_TOKEN is the beginning of the buffer. */ | |
370 | lexer->first_token = lexer->buffer; | |
371 | /* The next available token is also at the beginning of the buffer. */ | |
372 | lexer->next_token = lexer->buffer; | |
373 | /* The buffer is full. */ | |
374 | lexer->last_token = lexer->first_token; | |
375 | ||
17211ab5 GK |
376 | /* This lexer doesn't obtain more tokens. */ |
377 | lexer->main_lexer_p = false; | |
378 | ||
379 | /* Create the SAVED_TOKENS stack. */ | |
380 | VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens"); | |
381 | ||
382 | /* Create the STRINGS array. */ | |
383 | VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings"); | |
384 | ||
385 | /* Assume we are not debugging. */ | |
386 | lexer->debugging_p = false; | |
387 | ||
a723baf1 MM |
388 | return lexer; |
389 | } | |
390 | ||
4de8668e | 391 | /* Returns nonzero if debugging information should be output. */ |
a723baf1 | 392 | |
f7b5ecd9 MM |
393 | static inline bool |
394 | cp_lexer_debugging_p (cp_lexer *lexer) | |
a723baf1 | 395 | { |
f7b5ecd9 MM |
396 | return lexer->debugging_p; |
397 | } | |
398 | ||
399 | /* Set the current source position from the information stored in | |
400 | TOKEN. */ | |
401 | ||
402 | static inline void | |
94edc4ab NN |
403 | cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED , |
404 | const cp_token *token) | |
f7b5ecd9 MM |
405 | { |
406 | /* Ideally, the source position information would not be a global | |
407 | variable, but it is. */ | |
408 | ||
409 | /* Update the line number. */ | |
410 | if (token->type != CPP_EOF) | |
82a98427 | 411 | input_location = token->location; |
a723baf1 MM |
412 | } |
413 | ||
414 | /* TOKEN points into the circular token buffer. Return a pointer to | |
415 | the next token in the buffer. */ | |
416 | ||
f7b5ecd9 | 417 | static inline cp_token * |
94edc4ab | 418 | cp_lexer_next_token (cp_lexer* lexer, cp_token* token) |
a723baf1 MM |
419 | { |
420 | token++; | |
421 | if (token == lexer->buffer_end) | |
422 | token = lexer->buffer; | |
423 | return token; | |
424 | } | |
425 | ||
a668c6ad MM |
426 | /* TOKEN points into the circular token buffer. Return a pointer to |
427 | the previous token in the buffer. */ | |
428 | ||
429 | static inline cp_token * | |
430 | cp_lexer_prev_token (cp_lexer* lexer, cp_token* token) | |
431 | { | |
432 | if (token == lexer->buffer) | |
433 | token = lexer->buffer_end; | |
434 | return token - 1; | |
435 | } | |
436 | ||
4de8668e | 437 | /* nonzero if we are presently saving tokens. */ |
f7b5ecd9 MM |
438 | |
439 | static int | |
94edc4ab | 440 | cp_lexer_saving_tokens (const cp_lexer* lexer) |
f7b5ecd9 MM |
441 | { |
442 | return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0; | |
443 | } | |
444 | ||
a723baf1 MM |
445 | /* Return a pointer to the token that is N tokens beyond TOKEN in the |
446 | buffer. */ | |
447 | ||
448 | static cp_token * | |
449 | cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n) | |
450 | { | |
451 | token += n; | |
452 | if (token >= lexer->buffer_end) | |
453 | token = lexer->buffer + (token - lexer->buffer_end); | |
454 | return token; | |
455 | } | |
456 | ||
457 | /* Returns the number of times that START would have to be incremented | |
458 | to reach FINISH. If START and FINISH are the same, returns zero. */ | |
459 | ||
460 | static ptrdiff_t | |
94edc4ab | 461 | cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish) |
a723baf1 MM |
462 | { |
463 | if (finish >= start) | |
464 | return finish - start; | |
465 | else | |
466 | return ((lexer->buffer_end - lexer->buffer) | |
467 | - (start - finish)); | |
468 | } | |
469 | ||
470 | /* Obtain another token from the C preprocessor and add it to the | |
471 | token buffer. Returns the newly read token. */ | |
472 | ||
473 | static cp_token * | |
94edc4ab | 474 | cp_lexer_read_token (cp_lexer* lexer) |
a723baf1 MM |
475 | { |
476 | cp_token *token; | |
477 | ||
478 | /* Make sure there is room in the buffer. */ | |
479 | cp_lexer_maybe_grow_buffer (lexer); | |
480 | ||
481 | /* If there weren't any tokens, then this one will be the first. */ | |
482 | if (!lexer->first_token) | |
483 | lexer->first_token = lexer->last_token; | |
484 | /* Similarly, if there were no available tokens, there is one now. */ | |
485 | if (!lexer->next_token) | |
486 | lexer->next_token = lexer->last_token; | |
487 | ||
488 | /* Figure out where we're going to store the new token. */ | |
489 | token = lexer->last_token; | |
490 | ||
491 | /* Get a new token from the preprocessor. */ | |
492 | cp_lexer_get_preprocessor_token (lexer, token); | |
493 | ||
494 | /* Increment LAST_TOKEN. */ | |
495 | lexer->last_token = cp_lexer_next_token (lexer, token); | |
496 | ||
e6cc3a24 ZW |
497 | /* Strings should have type `const char []'. Right now, we will |
498 | have an ARRAY_TYPE that is constant rather than an array of | |
499 | constant elements. | |
500 | FIXME: Make fix_string_type get this right in the first place. */ | |
501 | if ((token->type == CPP_STRING || token->type == CPP_WSTRING) | |
502 | && flag_const_strings) | |
a723baf1 | 503 | { |
e6cc3a24 ZW |
504 | tree type; |
505 | ||
506 | /* Get the current type. It will be an ARRAY_TYPE. */ | |
507 | type = TREE_TYPE (token->value); | |
508 | /* Use build_cplus_array_type to rebuild the array, thereby | |
509 | getting the right type. */ | |
510 | type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type)); | |
511 | /* Reset the type of the token. */ | |
512 | TREE_TYPE (token->value) = type; | |
a723baf1 MM |
513 | } |
514 | ||
515 | return token; | |
516 | } | |
517 | ||
518 | /* If the circular buffer is full, make it bigger. */ | |
519 | ||
520 | static void | |
94edc4ab | 521 | cp_lexer_maybe_grow_buffer (cp_lexer* lexer) |
a723baf1 MM |
522 | { |
523 | /* If the buffer is full, enlarge it. */ | |
524 | if (lexer->last_token == lexer->first_token) | |
525 | { | |
526 | cp_token *new_buffer; | |
527 | cp_token *old_buffer; | |
528 | cp_token *new_first_token; | |
529 | ptrdiff_t buffer_length; | |
530 | size_t num_tokens_to_copy; | |
531 | ||
532 | /* Remember the current buffer pointer. It will become invalid, | |
533 | but we will need to do pointer arithmetic involving this | |
534 | value. */ | |
535 | old_buffer = lexer->buffer; | |
536 | /* Compute the current buffer size. */ | |
537 | buffer_length = lexer->buffer_end - lexer->buffer; | |
538 | /* Allocate a buffer twice as big. */ | |
c68b0a84 KG |
539 | new_buffer = ggc_realloc (lexer->buffer, |
540 | 2 * buffer_length * sizeof (cp_token)); | |
a723baf1 MM |
541 | |
542 | /* Because the buffer is circular, logically consecutive tokens | |
543 | are not necessarily placed consecutively in memory. | |
544 | Therefore, we must keep move the tokens that were before | |
545 | FIRST_TOKEN to the second half of the newly allocated | |
546 | buffer. */ | |
547 | num_tokens_to_copy = (lexer->first_token - old_buffer); | |
548 | memcpy (new_buffer + buffer_length, | |
549 | new_buffer, | |
550 | num_tokens_to_copy * sizeof (cp_token)); | |
551 | /* Clear the rest of the buffer. We never look at this storage, | |
552 | but the garbage collector may. */ | |
553 | memset (new_buffer + buffer_length + num_tokens_to_copy, 0, | |
554 | (buffer_length - num_tokens_to_copy) * sizeof (cp_token)); | |
555 | ||
556 | /* Now recompute all of the buffer pointers. */ | |
557 | new_first_token | |
558 | = new_buffer + (lexer->first_token - old_buffer); | |
559 | if (lexer->next_token != NULL) | |
560 | { | |
561 | ptrdiff_t next_token_delta; | |
562 | ||
563 | if (lexer->next_token > lexer->first_token) | |
564 | next_token_delta = lexer->next_token - lexer->first_token; | |
565 | else | |
566 | next_token_delta = | |
567 | buffer_length - (lexer->first_token - lexer->next_token); | |
568 | lexer->next_token = new_first_token + next_token_delta; | |
569 | } | |
570 | lexer->last_token = new_first_token + buffer_length; | |
571 | lexer->buffer = new_buffer; | |
572 | lexer->buffer_end = new_buffer + buffer_length * 2; | |
573 | lexer->first_token = new_first_token; | |
574 | } | |
575 | } | |
576 | ||
577 | /* Store the next token from the preprocessor in *TOKEN. */ | |
578 | ||
579 | static void | |
94edc4ab NN |
580 | cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED , |
581 | cp_token *token) | |
a723baf1 MM |
582 | { |
583 | bool done; | |
584 | ||
585 | /* If this not the main lexer, return a terminating CPP_EOF token. */ | |
17211ab5 | 586 | if (lexer != NULL && !lexer->main_lexer_p) |
a723baf1 MM |
587 | { |
588 | token->type = CPP_EOF; | |
82a98427 NS |
589 | token->location.line = 0; |
590 | token->location.file = NULL; | |
a723baf1 MM |
591 | token->value = NULL_TREE; |
592 | token->keyword = RID_MAX; | |
593 | ||
594 | return; | |
595 | } | |
596 | ||
597 | done = false; | |
598 | /* Keep going until we get a token we like. */ | |
599 | while (!done) | |
600 | { | |
601 | /* Get a new token from the preprocessor. */ | |
f4abade9 | 602 | token->type = c_lex_with_flags (&token->value, &token->flags); |
a723baf1 MM |
603 | /* Issue messages about tokens we cannot process. */ |
604 | switch (token->type) | |
605 | { | |
606 | case CPP_ATSIGN: | |
607 | case CPP_HASH: | |
608 | case CPP_PASTE: | |
609 | error ("invalid token"); | |
610 | break; | |
611 | ||
a723baf1 MM |
612 | default: |
613 | /* This is a good token, so we exit the loop. */ | |
614 | done = true; | |
615 | break; | |
616 | } | |
617 | } | |
618 | /* Now we've got our token. */ | |
82a98427 | 619 | token->location = input_location; |
a723baf1 MM |
620 | |
621 | /* Check to see if this token is a keyword. */ | |
622 | if (token->type == CPP_NAME | |
623 | && C_IS_RESERVED_WORD (token->value)) | |
624 | { | |
625 | /* Mark this token as a keyword. */ | |
626 | token->type = CPP_KEYWORD; | |
627 | /* Record which keyword. */ | |
628 | token->keyword = C_RID_CODE (token->value); | |
629 | /* Update the value. Some keywords are mapped to particular | |
630 | entities, rather than simply having the value of the | |
631 | corresponding IDENTIFIER_NODE. For example, `__const' is | |
632 | mapped to `const'. */ | |
633 | token->value = ridpointers[token->keyword]; | |
634 | } | |
635 | else | |
636 | token->keyword = RID_MAX; | |
637 | } | |
638 | ||
639 | /* Return a pointer to the next token in the token stream, but do not | |
640 | consume it. */ | |
641 | ||
642 | static cp_token * | |
94edc4ab | 643 | cp_lexer_peek_token (cp_lexer* lexer) |
a723baf1 MM |
644 | { |
645 | cp_token *token; | |
646 | ||
647 | /* If there are no tokens, read one now. */ | |
648 | if (!lexer->next_token) | |
649 | cp_lexer_read_token (lexer); | |
650 | ||
651 | /* Provide debugging output. */ | |
652 | if (cp_lexer_debugging_p (lexer)) | |
653 | { | |
654 | fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: "); | |
655 | cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token); | |
656 | fprintf (cp_lexer_debug_stream, "\n"); | |
657 | } | |
658 | ||
659 | token = lexer->next_token; | |
660 | cp_lexer_set_source_position_from_token (lexer, token); | |
661 | return token; | |
662 | } | |
663 | ||
664 | /* Return true if the next token has the indicated TYPE. */ | |
665 | ||
666 | static bool | |
94edc4ab | 667 | cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type) |
a723baf1 MM |
668 | { |
669 | cp_token *token; | |
670 | ||
671 | /* Peek at the next token. */ | |
672 | token = cp_lexer_peek_token (lexer); | |
673 | /* Check to see if it has the indicated TYPE. */ | |
674 | return token->type == type; | |
675 | } | |
676 | ||
677 | /* Return true if the next token does not have the indicated TYPE. */ | |
678 | ||
679 | static bool | |
94edc4ab | 680 | cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type) |
a723baf1 MM |
681 | { |
682 | return !cp_lexer_next_token_is (lexer, type); | |
683 | } | |
684 | ||
685 | /* Return true if the next token is the indicated KEYWORD. */ | |
686 | ||
687 | static bool | |
94edc4ab | 688 | cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword) |
a723baf1 MM |
689 | { |
690 | cp_token *token; | |
691 | ||
692 | /* Peek at the next token. */ | |
693 | token = cp_lexer_peek_token (lexer); | |
694 | /* Check to see if it is the indicated keyword. */ | |
695 | return token->keyword == keyword; | |
696 | } | |
697 | ||
698 | /* Return a pointer to the Nth token in the token stream. If N is 1, | |
699 | then this is precisely equivalent to cp_lexer_peek_token. */ | |
700 | ||
701 | static cp_token * | |
94edc4ab | 702 | cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n) |
a723baf1 MM |
703 | { |
704 | cp_token *token; | |
705 | ||
706 | /* N is 1-based, not zero-based. */ | |
707 | my_friendly_assert (n > 0, 20000224); | |
708 | ||
709 | /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */ | |
710 | token = lexer->next_token; | |
711 | /* If there are no tokens in the buffer, get one now. */ | |
712 | if (!token) | |
713 | { | |
714 | cp_lexer_read_token (lexer); | |
715 | token = lexer->next_token; | |
716 | } | |
717 | ||
718 | /* Now, read tokens until we have enough. */ | |
719 | while (--n > 0) | |
720 | { | |
721 | /* Advance to the next token. */ | |
722 | token = cp_lexer_next_token (lexer, token); | |
723 | /* If that's all the tokens we have, read a new one. */ | |
724 | if (token == lexer->last_token) | |
725 | token = cp_lexer_read_token (lexer); | |
726 | } | |
727 | ||
728 | return token; | |
729 | } | |
730 | ||
731 | /* Consume the next token. The pointer returned is valid only until | |
732 | another token is read. Callers should preserve copy the token | |
733 | explicitly if they will need its value for a longer period of | |
734 | time. */ | |
735 | ||
736 | static cp_token * | |
94edc4ab | 737 | cp_lexer_consume_token (cp_lexer* lexer) |
a723baf1 MM |
738 | { |
739 | cp_token *token; | |
740 | ||
741 | /* If there are no tokens, read one now. */ | |
742 | if (!lexer->next_token) | |
743 | cp_lexer_read_token (lexer); | |
744 | ||
745 | /* Remember the token we'll be returning. */ | |
746 | token = lexer->next_token; | |
747 | ||
748 | /* Increment NEXT_TOKEN. */ | |
749 | lexer->next_token = cp_lexer_next_token (lexer, | |
750 | lexer->next_token); | |
751 | /* Check to see if we're all out of tokens. */ | |
752 | if (lexer->next_token == lexer->last_token) | |
753 | lexer->next_token = NULL; | |
754 | ||
755 | /* If we're not saving tokens, then move FIRST_TOKEN too. */ | |
756 | if (!cp_lexer_saving_tokens (lexer)) | |
757 | { | |
758 | /* If there are no tokens available, set FIRST_TOKEN to NULL. */ | |
759 | if (!lexer->next_token) | |
760 | lexer->first_token = NULL; | |
761 | else | |
762 | lexer->first_token = lexer->next_token; | |
763 | } | |
764 | ||
765 | /* Provide debugging output. */ | |
766 | if (cp_lexer_debugging_p (lexer)) | |
767 | { | |
768 | fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: "); | |
769 | cp_lexer_print_token (cp_lexer_debug_stream, token); | |
770 | fprintf (cp_lexer_debug_stream, "\n"); | |
771 | } | |
772 | ||
773 | return token; | |
774 | } | |
775 | ||
776 | /* Permanently remove the next token from the token stream. There | |
777 | must be a valid next token already; this token never reads | |
778 | additional tokens from the preprocessor. */ | |
779 | ||
780 | static void | |
781 | cp_lexer_purge_token (cp_lexer *lexer) | |
782 | { | |
783 | cp_token *token; | |
784 | cp_token *next_token; | |
785 | ||
786 | token = lexer->next_token; | |
787 | while (true) | |
788 | { | |
789 | next_token = cp_lexer_next_token (lexer, token); | |
790 | if (next_token == lexer->last_token) | |
791 | break; | |
792 | *token = *next_token; | |
793 | token = next_token; | |
794 | } | |
795 | ||
796 | lexer->last_token = token; | |
797 | /* The token purged may have been the only token remaining; if so, | |
798 | clear NEXT_TOKEN. */ | |
799 | if (lexer->next_token == token) | |
800 | lexer->next_token = NULL; | |
801 | } | |
802 | ||
803 | /* Permanently remove all tokens after TOKEN, up to, but not | |
804 | including, the token that will be returned next by | |
805 | cp_lexer_peek_token. */ | |
806 | ||
807 | static void | |
808 | cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token) | |
809 | { | |
810 | cp_token *peek; | |
811 | cp_token *t1; | |
812 | cp_token *t2; | |
813 | ||
814 | if (lexer->next_token) | |
815 | { | |
816 | /* Copy the tokens that have not yet been read to the location | |
817 | immediately following TOKEN. */ | |
818 | t1 = cp_lexer_next_token (lexer, token); | |
819 | t2 = peek = cp_lexer_peek_token (lexer); | |
820 | /* Move tokens into the vacant area between TOKEN and PEEK. */ | |
821 | while (t2 != lexer->last_token) | |
822 | { | |
823 | *t1 = *t2; | |
824 | t1 = cp_lexer_next_token (lexer, t1); | |
825 | t2 = cp_lexer_next_token (lexer, t2); | |
826 | } | |
827 | /* Now, the next available token is right after TOKEN. */ | |
828 | lexer->next_token = cp_lexer_next_token (lexer, token); | |
829 | /* And the last token is wherever we ended up. */ | |
830 | lexer->last_token = t1; | |
831 | } | |
832 | else | |
833 | { | |
834 | /* There are no tokens in the buffer, so there is nothing to | |
835 | copy. The last token in the buffer is TOKEN itself. */ | |
836 | lexer->last_token = cp_lexer_next_token (lexer, token); | |
837 | } | |
838 | } | |
839 | ||
840 | /* Begin saving tokens. All tokens consumed after this point will be | |
841 | preserved. */ | |
842 | ||
843 | static void | |
94edc4ab | 844 | cp_lexer_save_tokens (cp_lexer* lexer) |
a723baf1 MM |
845 | { |
846 | /* Provide debugging output. */ | |
847 | if (cp_lexer_debugging_p (lexer)) | |
848 | fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n"); | |
849 | ||
850 | /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can | |
851 | restore the tokens if required. */ | |
852 | if (!lexer->next_token) | |
853 | cp_lexer_read_token (lexer); | |
854 | ||
855 | VARRAY_PUSH_INT (lexer->saved_tokens, | |
856 | cp_lexer_token_difference (lexer, | |
857 | lexer->first_token, | |
858 | lexer->next_token)); | |
859 | } | |
860 | ||
861 | /* Commit to the portion of the token stream most recently saved. */ | |
862 | ||
863 | static void | |
94edc4ab | 864 | cp_lexer_commit_tokens (cp_lexer* lexer) |
a723baf1 MM |
865 | { |
866 | /* Provide debugging output. */ | |
867 | if (cp_lexer_debugging_p (lexer)) | |
868 | fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n"); | |
869 | ||
870 | VARRAY_POP (lexer->saved_tokens); | |
871 | } | |
872 | ||
873 | /* Return all tokens saved since the last call to cp_lexer_save_tokens | |
874 | to the token stream. Stop saving tokens. */ | |
875 | ||
876 | static void | |
94edc4ab | 877 | cp_lexer_rollback_tokens (cp_lexer* lexer) |
a723baf1 MM |
878 | { |
879 | size_t delta; | |
880 | ||
881 | /* Provide debugging output. */ | |
882 | if (cp_lexer_debugging_p (lexer)) | |
883 | fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n"); | |
884 | ||
885 | /* Find the token that was the NEXT_TOKEN when we started saving | |
886 | tokens. */ | |
887 | delta = VARRAY_TOP_INT(lexer->saved_tokens); | |
888 | /* Make it the next token again now. */ | |
889 | lexer->next_token = cp_lexer_advance_token (lexer, | |
890 | lexer->first_token, | |
891 | delta); | |
15d2cb19 | 892 | /* It might be the case that there were no tokens when we started |
a723baf1 MM |
893 | saving tokens, but that there are some tokens now. */ |
894 | if (!lexer->next_token && lexer->first_token) | |
895 | lexer->next_token = lexer->first_token; | |
896 | ||
897 | /* Stop saving tokens. */ | |
898 | VARRAY_POP (lexer->saved_tokens); | |
899 | } | |
900 | ||
a723baf1 MM |
901 | /* Print a representation of the TOKEN on the STREAM. */ |
902 | ||
903 | static void | |
94edc4ab | 904 | cp_lexer_print_token (FILE * stream, cp_token* token) |
a723baf1 MM |
905 | { |
906 | const char *token_type = NULL; | |
907 | ||
908 | /* Figure out what kind of token this is. */ | |
909 | switch (token->type) | |
910 | { | |
911 | case CPP_EQ: | |
912 | token_type = "EQ"; | |
913 | break; | |
914 | ||
915 | case CPP_COMMA: | |
916 | token_type = "COMMA"; | |
917 | break; | |
918 | ||
919 | case CPP_OPEN_PAREN: | |
920 | token_type = "OPEN_PAREN"; | |
921 | break; | |
922 | ||
923 | case CPP_CLOSE_PAREN: | |
924 | token_type = "CLOSE_PAREN"; | |
925 | break; | |
926 | ||
927 | case CPP_OPEN_BRACE: | |
928 | token_type = "OPEN_BRACE"; | |
929 | break; | |
930 | ||
931 | case CPP_CLOSE_BRACE: | |
932 | token_type = "CLOSE_BRACE"; | |
933 | break; | |
934 | ||
935 | case CPP_SEMICOLON: | |
936 | token_type = "SEMICOLON"; | |
937 | break; | |
938 | ||
939 | case CPP_NAME: | |
940 | token_type = "NAME"; | |
941 | break; | |
942 | ||
943 | case CPP_EOF: | |
944 | token_type = "EOF"; | |
945 | break; | |
946 | ||
947 | case CPP_KEYWORD: | |
948 | token_type = "keyword"; | |
949 | break; | |
950 | ||
951 | /* This is not a token that we know how to handle yet. */ | |
952 | default: | |
953 | break; | |
954 | } | |
955 | ||
956 | /* If we have a name for the token, print it out. Otherwise, we | |
957 | simply give the numeric code. */ | |
958 | if (token_type) | |
959 | fprintf (stream, "%s", token_type); | |
960 | else | |
961 | fprintf (stream, "%d", token->type); | |
962 | /* And, for an identifier, print the identifier name. */ | |
963 | if (token->type == CPP_NAME | |
964 | /* Some keywords have a value that is not an IDENTIFIER_NODE. | |
965 | For example, `struct' is mapped to an INTEGER_CST. */ | |
966 | || (token->type == CPP_KEYWORD | |
967 | && TREE_CODE (token->value) == IDENTIFIER_NODE)) | |
968 | fprintf (stream, " %s", IDENTIFIER_POINTER (token->value)); | |
969 | } | |
970 | ||
a723baf1 MM |
971 | /* Start emitting debugging information. */ |
972 | ||
973 | static void | |
94edc4ab | 974 | cp_lexer_start_debugging (cp_lexer* lexer) |
a723baf1 MM |
975 | { |
976 | ++lexer->debugging_p; | |
977 | } | |
978 | ||
979 | /* Stop emitting debugging information. */ | |
980 | ||
981 | static void | |
94edc4ab | 982 | cp_lexer_stop_debugging (cp_lexer* lexer) |
a723baf1 MM |
983 | { |
984 | --lexer->debugging_p; | |
985 | } | |
986 | ||
987 | \f | |
988 | /* The parser. */ | |
989 | ||
990 | /* Overview | |
991 | -------- | |
992 | ||
993 | A cp_parser parses the token stream as specified by the C++ | |
994 | grammar. Its job is purely parsing, not semantic analysis. For | |
995 | example, the parser breaks the token stream into declarators, | |
996 | expressions, statements, and other similar syntactic constructs. | |
997 | It does not check that the types of the expressions on either side | |
998 | of an assignment-statement are compatible, or that a function is | |
999 | not declared with a parameter of type `void'. | |
1000 | ||
1001 | The parser invokes routines elsewhere in the compiler to perform | |
1002 | semantic analysis and to build up the abstract syntax tree for the | |
1003 | code processed. | |
1004 | ||
1005 | The parser (and the template instantiation code, which is, in a | |
1006 | way, a close relative of parsing) are the only parts of the | |
1007 | compiler that should be calling push_scope and pop_scope, or | |
1008 | related functions. The parser (and template instantiation code) | |
1009 | keeps track of what scope is presently active; everything else | |
1010 | should simply honor that. (The code that generates static | |
1011 | initializers may also need to set the scope, in order to check | |
1012 | access control correctly when emitting the initializers.) | |
1013 | ||
1014 | Methodology | |
1015 | ----------- | |
1016 | ||
1017 | The parser is of the standard recursive-descent variety. Upcoming | |
1018 | tokens in the token stream are examined in order to determine which | |
1019 | production to use when parsing a non-terminal. Some C++ constructs | |
1020 | require arbitrary look ahead to disambiguate. For example, it is | |
1021 | impossible, in the general case, to tell whether a statement is an | |
1022 | expression or declaration without scanning the entire statement. | |
1023 | Therefore, the parser is capable of "parsing tentatively." When the | |
1024 | parser is not sure what construct comes next, it enters this mode. | |
1025 | Then, while we attempt to parse the construct, the parser queues up | |
1026 | error messages, rather than issuing them immediately, and saves the | |
1027 | tokens it consumes. If the construct is parsed successfully, the | |
1028 | parser "commits", i.e., it issues any queued error messages and | |
1029 | the tokens that were being preserved are permanently discarded. | |
1030 | If, however, the construct is not parsed successfully, the parser | |
1031 | rolls back its state completely so that it can resume parsing using | |
1032 | a different alternative. | |
1033 | ||
1034 | Future Improvements | |
1035 | ------------------- | |
1036 | ||
1037 | The performance of the parser could probably be improved | |
1038 | substantially. Some possible improvements include: | |
1039 | ||
1040 | - The expression parser recurses through the various levels of | |
1041 | precedence as specified in the grammar, rather than using an | |
1042 | operator-precedence technique. Therefore, parsing a simple | |
1043 | identifier requires multiple recursive calls. | |
1044 | ||
1045 | - We could often eliminate the need to parse tentatively by | |
1046 | looking ahead a little bit. In some places, this approach | |
1047 | might not entirely eliminate the need to parse tentatively, but | |
1048 | it might still speed up the average case. */ | |
1049 | ||
1050 | /* Flags that are passed to some parsing functions. These values can | |
1051 | be bitwise-ored together. */ | |
1052 | ||
1053 | typedef enum cp_parser_flags | |
1054 | { | |
1055 | /* No flags. */ | |
1056 | CP_PARSER_FLAGS_NONE = 0x0, | |
1057 | /* The construct is optional. If it is not present, then no error | |
1058 | should be issued. */ | |
1059 | CP_PARSER_FLAGS_OPTIONAL = 0x1, | |
1060 | /* When parsing a type-specifier, do not allow user-defined types. */ | |
1061 | CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2 | |
1062 | } cp_parser_flags; | |
1063 | ||
62b8a44e NS |
1064 | /* The different kinds of declarators we want to parse. */ |
1065 | ||
1066 | typedef enum cp_parser_declarator_kind | |
1067 | { | |
852dcbdd | 1068 | /* We want an abstract declarator. */ |
62b8a44e NS |
1069 | CP_PARSER_DECLARATOR_ABSTRACT, |
1070 | /* We want a named declarator. */ | |
1071 | CP_PARSER_DECLARATOR_NAMED, | |
04c06002 | 1072 | /* We don't mind, but the name must be an unqualified-id. */ |
62b8a44e NS |
1073 | CP_PARSER_DECLARATOR_EITHER |
1074 | } cp_parser_declarator_kind; | |
1075 | ||
a723baf1 MM |
1076 | /* A mapping from a token type to a corresponding tree node type. */ |
1077 | ||
1078 | typedef struct cp_parser_token_tree_map_node | |
1079 | { | |
1080 | /* The token type. */ | |
df2b750f | 1081 | ENUM_BITFIELD (cpp_ttype) token_type : 8; |
a723baf1 | 1082 | /* The corresponding tree code. */ |
df2b750f | 1083 | ENUM_BITFIELD (tree_code) tree_type : 8; |
a723baf1 MM |
1084 | } cp_parser_token_tree_map_node; |
1085 | ||
1086 | /* A complete map consists of several ordinary entries, followed by a | |
1087 | terminator. The terminating entry has a token_type of CPP_EOF. */ | |
1088 | ||
1089 | typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[]; | |
1090 | ||
1091 | /* The status of a tentative parse. */ | |
1092 | ||
1093 | typedef enum cp_parser_status_kind | |
1094 | { | |
1095 | /* No errors have occurred. */ | |
1096 | CP_PARSER_STATUS_KIND_NO_ERROR, | |
1097 | /* An error has occurred. */ | |
1098 | CP_PARSER_STATUS_KIND_ERROR, | |
1099 | /* We are committed to this tentative parse, whether or not an error | |
1100 | has occurred. */ | |
1101 | CP_PARSER_STATUS_KIND_COMMITTED | |
1102 | } cp_parser_status_kind; | |
1103 | ||
1104 | /* Context that is saved and restored when parsing tentatively. */ | |
1105 | ||
1106 | typedef struct cp_parser_context GTY (()) | |
1107 | { | |
1108 | /* If this is a tentative parsing context, the status of the | |
1109 | tentative parse. */ | |
1110 | enum cp_parser_status_kind status; | |
1111 | /* If non-NULL, we have just seen a `x->' or `x.' expression. Names | |
1112 | that are looked up in this context must be looked up both in the | |
1113 | scope given by OBJECT_TYPE (the type of `x' or `*x') and also in | |
1114 | the context of the containing expression. */ | |
1115 | tree object_type; | |
a723baf1 MM |
1116 | /* The next parsing context in the stack. */ |
1117 | struct cp_parser_context *next; | |
1118 | } cp_parser_context; | |
1119 | ||
1120 | /* Prototypes. */ | |
1121 | ||
1122 | /* Constructors and destructors. */ | |
1123 | ||
1124 | static cp_parser_context *cp_parser_context_new | |
94edc4ab | 1125 | (cp_parser_context *); |
a723baf1 | 1126 | |
e5976695 MM |
1127 | /* Class variables. */ |
1128 | ||
92bc1323 | 1129 | static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list; |
e5976695 | 1130 | |
a723baf1 MM |
1131 | /* Constructors and destructors. */ |
1132 | ||
1133 | /* Construct a new context. The context below this one on the stack | |
1134 | is given by NEXT. */ | |
1135 | ||
1136 | static cp_parser_context * | |
94edc4ab | 1137 | cp_parser_context_new (cp_parser_context* next) |
a723baf1 MM |
1138 | { |
1139 | cp_parser_context *context; | |
1140 | ||
1141 | /* Allocate the storage. */ | |
e5976695 MM |
1142 | if (cp_parser_context_free_list != NULL) |
1143 | { | |
1144 | /* Pull the first entry from the free list. */ | |
1145 | context = cp_parser_context_free_list; | |
1146 | cp_parser_context_free_list = context->next; | |
c68b0a84 | 1147 | memset (context, 0, sizeof (*context)); |
e5976695 MM |
1148 | } |
1149 | else | |
c68b0a84 | 1150 | context = ggc_alloc_cleared (sizeof (cp_parser_context)); |
a723baf1 MM |
1151 | /* No errors have occurred yet in this context. */ |
1152 | context->status = CP_PARSER_STATUS_KIND_NO_ERROR; | |
1153 | /* If this is not the bottomost context, copy information that we | |
1154 | need from the previous context. */ | |
1155 | if (next) | |
1156 | { | |
1157 | /* If, in the NEXT context, we are parsing an `x->' or `x.' | |
1158 | expression, then we are parsing one in this context, too. */ | |
1159 | context->object_type = next->object_type; | |
a723baf1 MM |
1160 | /* Thread the stack. */ |
1161 | context->next = next; | |
1162 | } | |
1163 | ||
1164 | return context; | |
1165 | } | |
1166 | ||
1167 | /* The cp_parser structure represents the C++ parser. */ | |
1168 | ||
1169 | typedef struct cp_parser GTY(()) | |
1170 | { | |
1171 | /* The lexer from which we are obtaining tokens. */ | |
1172 | cp_lexer *lexer; | |
1173 | ||
1174 | /* The scope in which names should be looked up. If NULL_TREE, then | |
1175 | we look up names in the scope that is currently open in the | |
1176 | source program. If non-NULL, this is either a TYPE or | |
1177 | NAMESPACE_DECL for the scope in which we should look. | |
1178 | ||
1179 | This value is not cleared automatically after a name is looked | |
1180 | up, so we must be careful to clear it before starting a new look | |
1181 | up sequence. (If it is not cleared, then `X::Y' followed by `Z' | |
1182 | will look up `Z' in the scope of `X', rather than the current | |
1183 | scope.) Unfortunately, it is difficult to tell when name lookup | |
1184 | is complete, because we sometimes peek at a token, look it up, | |
1185 | and then decide not to consume it. */ | |
1186 | tree scope; | |
1187 | ||
1188 | /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the | |
1189 | last lookup took place. OBJECT_SCOPE is used if an expression | |
1190 | like "x->y" or "x.y" was used; it gives the type of "*x" or "x", | |
1191 | respectively. QUALIFYING_SCOPE is used for an expression of the | |
1192 | form "X::Y"; it refers to X. */ | |
1193 | tree object_scope; | |
1194 | tree qualifying_scope; | |
1195 | ||
1196 | /* A stack of parsing contexts. All but the bottom entry on the | |
1197 | stack will be tentative contexts. | |
1198 | ||
1199 | We parse tentatively in order to determine which construct is in | |
1200 | use in some situations. For example, in order to determine | |
1201 | whether a statement is an expression-statement or a | |
1202 | declaration-statement we parse it tentatively as a | |
1203 | declaration-statement. If that fails, we then reparse the same | |
1204 | token stream as an expression-statement. */ | |
1205 | cp_parser_context *context; | |
1206 | ||
1207 | /* True if we are parsing GNU C++. If this flag is not set, then | |
1208 | GNU extensions are not recognized. */ | |
1209 | bool allow_gnu_extensions_p; | |
1210 | ||
1211 | /* TRUE if the `>' token should be interpreted as the greater-than | |
1212 | operator. FALSE if it is the end of a template-id or | |
1213 | template-parameter-list. */ | |
1214 | bool greater_than_is_operator_p; | |
1215 | ||
1216 | /* TRUE if default arguments are allowed within a parameter list | |
1217 | that starts at this point. FALSE if only a gnu extension makes | |
cd0be382 | 1218 | them permissible. */ |
a723baf1 MM |
1219 | bool default_arg_ok_p; |
1220 | ||
1221 | /* TRUE if we are parsing an integral constant-expression. See | |
1222 | [expr.const] for a precise definition. */ | |
67c03833 | 1223 | bool integral_constant_expression_p; |
a723baf1 | 1224 | |
14d22dd6 MM |
1225 | /* TRUE if we are parsing an integral constant-expression -- but a |
1226 | non-constant expression should be permitted as well. This flag | |
1227 | is used when parsing an array bound so that GNU variable-length | |
1228 | arrays are tolerated. */ | |
67c03833 | 1229 | bool allow_non_integral_constant_expression_p; |
14d22dd6 MM |
1230 | |
1231 | /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has | |
1232 | been seen that makes the expression non-constant. */ | |
67c03833 | 1233 | bool non_integral_constant_expression_p; |
14d22dd6 | 1234 | |
263ee052 MM |
1235 | /* TRUE if we are parsing the argument to "__offsetof__". */ |
1236 | bool in_offsetof_p; | |
1237 | ||
a723baf1 MM |
1238 | /* TRUE if local variable names and `this' are forbidden in the |
1239 | current context. */ | |
1240 | bool local_variables_forbidden_p; | |
1241 | ||
1242 | /* TRUE if the declaration we are parsing is part of a | |
1243 | linkage-specification of the form `extern string-literal | |
1244 | declaration'. */ | |
1245 | bool in_unbraced_linkage_specification_p; | |
1246 | ||
1247 | /* TRUE if we are presently parsing a declarator, after the | |
1248 | direct-declarator. */ | |
1249 | bool in_declarator_p; | |
1250 | ||
4bb8ca28 MM |
1251 | /* TRUE if we are presently parsing a template-argument-list. */ |
1252 | bool in_template_argument_list_p; | |
1253 | ||
0e59b3fb MM |
1254 | /* TRUE if we are presently parsing the body of an |
1255 | iteration-statement. */ | |
1256 | bool in_iteration_statement_p; | |
1257 | ||
1258 | /* TRUE if we are presently parsing the body of a switch | |
1259 | statement. */ | |
1260 | bool in_switch_statement_p; | |
1261 | ||
4f8163b1 MM |
1262 | /* TRUE if we are parsing a type-id in an expression context. In |
1263 | such a situation, both "type (expr)" and "type (type)" are valid | |
1264 | alternatives. */ | |
1265 | bool in_type_id_in_expr_p; | |
1266 | ||
a723baf1 MM |
1267 | /* If non-NULL, then we are parsing a construct where new type |
1268 | definitions are not permitted. The string stored here will be | |
1269 | issued as an error message if a type is defined. */ | |
1270 | const char *type_definition_forbidden_message; | |
1271 | ||
8db1028e NS |
1272 | /* A list of lists. The outer list is a stack, used for member |
1273 | functions of local classes. At each level there are two sub-list, | |
1274 | one on TREE_VALUE and one on TREE_PURPOSE. Each of those | |
1275 | sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their | |
1276 | TREE_VALUE's. The functions are chained in reverse declaration | |
1277 | order. | |
1278 | ||
1279 | The TREE_PURPOSE sublist contains those functions with default | |
1280 | arguments that need post processing, and the TREE_VALUE sublist | |
1281 | contains those functions with definitions that need post | |
1282 | processing. | |
1283 | ||
1284 | These lists can only be processed once the outermost class being | |
9bcb9aae | 1285 | defined is complete. */ |
a723baf1 MM |
1286 | tree unparsed_functions_queues; |
1287 | ||
1288 | /* The number of classes whose definitions are currently in | |
1289 | progress. */ | |
1290 | unsigned num_classes_being_defined; | |
1291 | ||
1292 | /* The number of template parameter lists that apply directly to the | |
1293 | current declaration. */ | |
1294 | unsigned num_template_parameter_lists; | |
1295 | } cp_parser; | |
1296 | ||
04c06002 | 1297 | /* The type of a function that parses some kind of expression. */ |
94edc4ab | 1298 | typedef tree (*cp_parser_expression_fn) (cp_parser *); |
a723baf1 MM |
1299 | |
1300 | /* Prototypes. */ | |
1301 | ||
1302 | /* Constructors and destructors. */ | |
1303 | ||
1304 | static cp_parser *cp_parser_new | |
94edc4ab | 1305 | (void); |
a723baf1 MM |
1306 | |
1307 | /* Routines to parse various constructs. | |
1308 | ||
1309 | Those that return `tree' will return the error_mark_node (rather | |
1310 | than NULL_TREE) if a parse error occurs, unless otherwise noted. | |
1311 | Sometimes, they will return an ordinary node if error-recovery was | |
34cd5ae7 | 1312 | attempted, even though a parse error occurred. So, to check |
a723baf1 MM |
1313 | whether or not a parse error occurred, you should always use |
1314 | cp_parser_error_occurred. If the construct is optional (indicated | |
1315 | either by an `_opt' in the name of the function that does the | |
1316 | parsing or via a FLAGS parameter), then NULL_TREE is returned if | |
1317 | the construct is not present. */ | |
1318 | ||
1319 | /* Lexical conventions [gram.lex] */ | |
1320 | ||
1321 | static tree cp_parser_identifier | |
94edc4ab | 1322 | (cp_parser *); |
a723baf1 MM |
1323 | |
1324 | /* Basic concepts [gram.basic] */ | |
1325 | ||
1326 | static bool cp_parser_translation_unit | |
94edc4ab | 1327 | (cp_parser *); |
a723baf1 MM |
1328 | |
1329 | /* Expressions [gram.expr] */ | |
1330 | ||
1331 | static tree cp_parser_primary_expression | |
b3445994 | 1332 | (cp_parser *, cp_id_kind *, tree *); |
a723baf1 | 1333 | static tree cp_parser_id_expression |
f3c2dfc6 | 1334 | (cp_parser *, bool, bool, bool *, bool); |
a723baf1 | 1335 | static tree cp_parser_unqualified_id |
f3c2dfc6 | 1336 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1337 | static tree cp_parser_nested_name_specifier_opt |
a668c6ad | 1338 | (cp_parser *, bool, bool, bool, bool); |
a723baf1 | 1339 | static tree cp_parser_nested_name_specifier |
a723baf1 | 1340 | (cp_parser *, bool, bool, bool, bool); |
a668c6ad MM |
1341 | static tree cp_parser_class_or_namespace_name |
1342 | (cp_parser *, bool, bool, bool, bool, bool); | |
a723baf1 MM |
1343 | static tree cp_parser_postfix_expression |
1344 | (cp_parser *, bool); | |
7efa3e22 | 1345 | static tree cp_parser_parenthesized_expression_list |
39703eb9 | 1346 | (cp_parser *, bool, bool *); |
a723baf1 | 1347 | static void cp_parser_pseudo_destructor_name |
94edc4ab | 1348 | (cp_parser *, tree *, tree *); |
a723baf1 MM |
1349 | static tree cp_parser_unary_expression |
1350 | (cp_parser *, bool); | |
1351 | static enum tree_code cp_parser_unary_operator | |
94edc4ab | 1352 | (cp_token *); |
a723baf1 | 1353 | static tree cp_parser_new_expression |
94edc4ab | 1354 | (cp_parser *); |
a723baf1 | 1355 | static tree cp_parser_new_placement |
94edc4ab | 1356 | (cp_parser *); |
a723baf1 | 1357 | static tree cp_parser_new_type_id |
94edc4ab | 1358 | (cp_parser *); |
a723baf1 | 1359 | static tree cp_parser_new_declarator_opt |
94edc4ab | 1360 | (cp_parser *); |
a723baf1 | 1361 | static tree cp_parser_direct_new_declarator |
94edc4ab | 1362 | (cp_parser *); |
a723baf1 | 1363 | static tree cp_parser_new_initializer |
94edc4ab | 1364 | (cp_parser *); |
a723baf1 | 1365 | static tree cp_parser_delete_expression |
94edc4ab | 1366 | (cp_parser *); |
a723baf1 MM |
1367 | static tree cp_parser_cast_expression |
1368 | (cp_parser *, bool); | |
1369 | static tree cp_parser_pm_expression | |
94edc4ab | 1370 | (cp_parser *); |
a723baf1 | 1371 | static tree cp_parser_multiplicative_expression |
94edc4ab | 1372 | (cp_parser *); |
a723baf1 | 1373 | static tree cp_parser_additive_expression |
94edc4ab | 1374 | (cp_parser *); |
a723baf1 | 1375 | static tree cp_parser_shift_expression |
94edc4ab | 1376 | (cp_parser *); |
a723baf1 | 1377 | static tree cp_parser_relational_expression |
94edc4ab | 1378 | (cp_parser *); |
a723baf1 | 1379 | static tree cp_parser_equality_expression |
94edc4ab | 1380 | (cp_parser *); |
a723baf1 | 1381 | static tree cp_parser_and_expression |
94edc4ab | 1382 | (cp_parser *); |
a723baf1 | 1383 | static tree cp_parser_exclusive_or_expression |
94edc4ab | 1384 | (cp_parser *); |
a723baf1 | 1385 | static tree cp_parser_inclusive_or_expression |
94edc4ab | 1386 | (cp_parser *); |
a723baf1 | 1387 | static tree cp_parser_logical_and_expression |
94edc4ab | 1388 | (cp_parser *); |
a723baf1 | 1389 | static tree cp_parser_logical_or_expression |
94edc4ab | 1390 | (cp_parser *); |
a723baf1 | 1391 | static tree cp_parser_question_colon_clause |
94edc4ab | 1392 | (cp_parser *, tree); |
a723baf1 | 1393 | static tree cp_parser_assignment_expression |
94edc4ab | 1394 | (cp_parser *); |
a723baf1 | 1395 | static enum tree_code cp_parser_assignment_operator_opt |
94edc4ab | 1396 | (cp_parser *); |
a723baf1 | 1397 | static tree cp_parser_expression |
94edc4ab | 1398 | (cp_parser *); |
a723baf1 | 1399 | static tree cp_parser_constant_expression |
14d22dd6 | 1400 | (cp_parser *, bool, bool *); |
a723baf1 MM |
1401 | |
1402 | /* Statements [gram.stmt.stmt] */ | |
1403 | ||
1404 | static void cp_parser_statement | |
a5bcc582 | 1405 | (cp_parser *, bool); |
a723baf1 | 1406 | static tree cp_parser_labeled_statement |
a5bcc582 | 1407 | (cp_parser *, bool); |
a723baf1 | 1408 | static tree cp_parser_expression_statement |
a5bcc582 | 1409 | (cp_parser *, bool); |
a723baf1 | 1410 | static tree cp_parser_compound_statement |
a5bcc582 | 1411 | (cp_parser *, bool); |
a723baf1 | 1412 | static void cp_parser_statement_seq_opt |
a5bcc582 | 1413 | (cp_parser *, bool); |
a723baf1 | 1414 | static tree cp_parser_selection_statement |
94edc4ab | 1415 | (cp_parser *); |
a723baf1 | 1416 | static tree cp_parser_condition |
94edc4ab | 1417 | (cp_parser *); |
a723baf1 | 1418 | static tree cp_parser_iteration_statement |
94edc4ab | 1419 | (cp_parser *); |
a723baf1 | 1420 | static void cp_parser_for_init_statement |
94edc4ab | 1421 | (cp_parser *); |
a723baf1 | 1422 | static tree cp_parser_jump_statement |
94edc4ab | 1423 | (cp_parser *); |
a723baf1 | 1424 | static void cp_parser_declaration_statement |
94edc4ab | 1425 | (cp_parser *); |
a723baf1 MM |
1426 | |
1427 | static tree cp_parser_implicitly_scoped_statement | |
94edc4ab | 1428 | (cp_parser *); |
a723baf1 | 1429 | static void cp_parser_already_scoped_statement |
94edc4ab | 1430 | (cp_parser *); |
a723baf1 MM |
1431 | |
1432 | /* Declarations [gram.dcl.dcl] */ | |
1433 | ||
1434 | static void cp_parser_declaration_seq_opt | |
94edc4ab | 1435 | (cp_parser *); |
a723baf1 | 1436 | static void cp_parser_declaration |
94edc4ab | 1437 | (cp_parser *); |
a723baf1 | 1438 | static void cp_parser_block_declaration |
94edc4ab | 1439 | (cp_parser *, bool); |
a723baf1 | 1440 | static void cp_parser_simple_declaration |
94edc4ab | 1441 | (cp_parser *, bool); |
a723baf1 | 1442 | static tree cp_parser_decl_specifier_seq |
560ad596 | 1443 | (cp_parser *, cp_parser_flags, tree *, int *); |
a723baf1 | 1444 | static tree cp_parser_storage_class_specifier_opt |
94edc4ab | 1445 | (cp_parser *); |
a723baf1 | 1446 | static tree cp_parser_function_specifier_opt |
94edc4ab | 1447 | (cp_parser *); |
a723baf1 | 1448 | static tree cp_parser_type_specifier |
560ad596 | 1449 | (cp_parser *, cp_parser_flags, bool, bool, int *, bool *); |
a723baf1 | 1450 | static tree cp_parser_simple_type_specifier |
4b0d3cbe | 1451 | (cp_parser *, cp_parser_flags, bool); |
a723baf1 | 1452 | static tree cp_parser_type_name |
94edc4ab | 1453 | (cp_parser *); |
a723baf1 | 1454 | static tree cp_parser_elaborated_type_specifier |
94edc4ab | 1455 | (cp_parser *, bool, bool); |
a723baf1 | 1456 | static tree cp_parser_enum_specifier |
94edc4ab | 1457 | (cp_parser *); |
a723baf1 | 1458 | static void cp_parser_enumerator_list |
94edc4ab | 1459 | (cp_parser *, tree); |
a723baf1 | 1460 | static void cp_parser_enumerator_definition |
94edc4ab | 1461 | (cp_parser *, tree); |
a723baf1 | 1462 | static tree cp_parser_namespace_name |
94edc4ab | 1463 | (cp_parser *); |
a723baf1 | 1464 | static void cp_parser_namespace_definition |
94edc4ab | 1465 | (cp_parser *); |
a723baf1 | 1466 | static void cp_parser_namespace_body |
94edc4ab | 1467 | (cp_parser *); |
a723baf1 | 1468 | static tree cp_parser_qualified_namespace_specifier |
94edc4ab | 1469 | (cp_parser *); |
a723baf1 | 1470 | static void cp_parser_namespace_alias_definition |
94edc4ab | 1471 | (cp_parser *); |
a723baf1 | 1472 | static void cp_parser_using_declaration |
94edc4ab | 1473 | (cp_parser *); |
a723baf1 | 1474 | static void cp_parser_using_directive |
94edc4ab | 1475 | (cp_parser *); |
a723baf1 | 1476 | static void cp_parser_asm_definition |
94edc4ab | 1477 | (cp_parser *); |
a723baf1 | 1478 | static void cp_parser_linkage_specification |
94edc4ab | 1479 | (cp_parser *); |
a723baf1 MM |
1480 | |
1481 | /* Declarators [gram.dcl.decl] */ | |
1482 | ||
1483 | static tree cp_parser_init_declarator | |
560ad596 | 1484 | (cp_parser *, tree, tree, bool, bool, int, bool *); |
a723baf1 | 1485 | static tree cp_parser_declarator |
4bb8ca28 | 1486 | (cp_parser *, cp_parser_declarator_kind, int *, bool *); |
a723baf1 | 1487 | static tree cp_parser_direct_declarator |
7efa3e22 | 1488 | (cp_parser *, cp_parser_declarator_kind, int *); |
a723baf1 | 1489 | static enum tree_code cp_parser_ptr_operator |
94edc4ab | 1490 | (cp_parser *, tree *, tree *); |
a723baf1 | 1491 | static tree cp_parser_cv_qualifier_seq_opt |
94edc4ab | 1492 | (cp_parser *); |
a723baf1 | 1493 | static tree cp_parser_cv_qualifier_opt |
94edc4ab | 1494 | (cp_parser *); |
a723baf1 | 1495 | static tree cp_parser_declarator_id |
94edc4ab | 1496 | (cp_parser *); |
a723baf1 | 1497 | static tree cp_parser_type_id |
94edc4ab | 1498 | (cp_parser *); |
a723baf1 | 1499 | static tree cp_parser_type_specifier_seq |
94edc4ab | 1500 | (cp_parser *); |
a723baf1 | 1501 | static tree cp_parser_parameter_declaration_clause |
94edc4ab | 1502 | (cp_parser *); |
a723baf1 | 1503 | static tree cp_parser_parameter_declaration_list |
94edc4ab | 1504 | (cp_parser *); |
a723baf1 | 1505 | static tree cp_parser_parameter_declaration |
4bb8ca28 | 1506 | (cp_parser *, bool, bool *); |
a723baf1 MM |
1507 | static void cp_parser_function_body |
1508 | (cp_parser *); | |
1509 | static tree cp_parser_initializer | |
39703eb9 | 1510 | (cp_parser *, bool *, bool *); |
a723baf1 | 1511 | static tree cp_parser_initializer_clause |
39703eb9 | 1512 | (cp_parser *, bool *); |
a723baf1 | 1513 | static tree cp_parser_initializer_list |
39703eb9 | 1514 | (cp_parser *, bool *); |
a723baf1 MM |
1515 | |
1516 | static bool cp_parser_ctor_initializer_opt_and_function_body | |
1517 | (cp_parser *); | |
1518 | ||
1519 | /* Classes [gram.class] */ | |
1520 | ||
1521 | static tree cp_parser_class_name | |
a668c6ad | 1522 | (cp_parser *, bool, bool, bool, bool, bool, bool); |
a723baf1 | 1523 | static tree cp_parser_class_specifier |
94edc4ab | 1524 | (cp_parser *); |
a723baf1 | 1525 | static tree cp_parser_class_head |
94edc4ab | 1526 | (cp_parser *, bool *); |
a723baf1 | 1527 | static enum tag_types cp_parser_class_key |
94edc4ab | 1528 | (cp_parser *); |
a723baf1 | 1529 | static void cp_parser_member_specification_opt |
94edc4ab | 1530 | (cp_parser *); |
a723baf1 | 1531 | static void cp_parser_member_declaration |
94edc4ab | 1532 | (cp_parser *); |
a723baf1 | 1533 | static tree cp_parser_pure_specifier |
94edc4ab | 1534 | (cp_parser *); |
a723baf1 | 1535 | static tree cp_parser_constant_initializer |
94edc4ab | 1536 | (cp_parser *); |
a723baf1 MM |
1537 | |
1538 | /* Derived classes [gram.class.derived] */ | |
1539 | ||
1540 | static tree cp_parser_base_clause | |
94edc4ab | 1541 | (cp_parser *); |
a723baf1 | 1542 | static tree cp_parser_base_specifier |
94edc4ab | 1543 | (cp_parser *); |
a723baf1 MM |
1544 | |
1545 | /* Special member functions [gram.special] */ | |
1546 | ||
1547 | static tree cp_parser_conversion_function_id | |
94edc4ab | 1548 | (cp_parser *); |
a723baf1 | 1549 | static tree cp_parser_conversion_type_id |
94edc4ab | 1550 | (cp_parser *); |
a723baf1 | 1551 | static tree cp_parser_conversion_declarator_opt |
94edc4ab | 1552 | (cp_parser *); |
a723baf1 | 1553 | static bool cp_parser_ctor_initializer_opt |
94edc4ab | 1554 | (cp_parser *); |
a723baf1 | 1555 | static void cp_parser_mem_initializer_list |
94edc4ab | 1556 | (cp_parser *); |
a723baf1 | 1557 | static tree cp_parser_mem_initializer |
94edc4ab | 1558 | (cp_parser *); |
a723baf1 | 1559 | static tree cp_parser_mem_initializer_id |
94edc4ab | 1560 | (cp_parser *); |
a723baf1 MM |
1561 | |
1562 | /* Overloading [gram.over] */ | |
1563 | ||
1564 | static tree cp_parser_operator_function_id | |
94edc4ab | 1565 | (cp_parser *); |
a723baf1 | 1566 | static tree cp_parser_operator |
94edc4ab | 1567 | (cp_parser *); |
a723baf1 MM |
1568 | |
1569 | /* Templates [gram.temp] */ | |
1570 | ||
1571 | static void cp_parser_template_declaration | |
94edc4ab | 1572 | (cp_parser *, bool); |
a723baf1 | 1573 | static tree cp_parser_template_parameter_list |
94edc4ab | 1574 | (cp_parser *); |
a723baf1 | 1575 | static tree cp_parser_template_parameter |
94edc4ab | 1576 | (cp_parser *); |
a723baf1 | 1577 | static tree cp_parser_type_parameter |
94edc4ab | 1578 | (cp_parser *); |
a723baf1 | 1579 | static tree cp_parser_template_id |
a668c6ad | 1580 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1581 | static tree cp_parser_template_name |
a668c6ad | 1582 | (cp_parser *, bool, bool, bool, bool *); |
a723baf1 | 1583 | static tree cp_parser_template_argument_list |
94edc4ab | 1584 | (cp_parser *); |
a723baf1 | 1585 | static tree cp_parser_template_argument |
94edc4ab | 1586 | (cp_parser *); |
a723baf1 | 1587 | static void cp_parser_explicit_instantiation |
94edc4ab | 1588 | (cp_parser *); |
a723baf1 | 1589 | static void cp_parser_explicit_specialization |
94edc4ab | 1590 | (cp_parser *); |
a723baf1 MM |
1591 | |
1592 | /* Exception handling [gram.exception] */ | |
1593 | ||
1594 | static tree cp_parser_try_block | |
94edc4ab | 1595 | (cp_parser *); |
a723baf1 | 1596 | static bool cp_parser_function_try_block |
94edc4ab | 1597 | (cp_parser *); |
a723baf1 | 1598 | static void cp_parser_handler_seq |
94edc4ab | 1599 | (cp_parser *); |
a723baf1 | 1600 | static void cp_parser_handler |
94edc4ab | 1601 | (cp_parser *); |
a723baf1 | 1602 | static tree cp_parser_exception_declaration |
94edc4ab | 1603 | (cp_parser *); |
a723baf1 | 1604 | static tree cp_parser_throw_expression |
94edc4ab | 1605 | (cp_parser *); |
a723baf1 | 1606 | static tree cp_parser_exception_specification_opt |
94edc4ab | 1607 | (cp_parser *); |
a723baf1 | 1608 | static tree cp_parser_type_id_list |
94edc4ab | 1609 | (cp_parser *); |
a723baf1 MM |
1610 | |
1611 | /* GNU Extensions */ | |
1612 | ||
1613 | static tree cp_parser_asm_specification_opt | |
94edc4ab | 1614 | (cp_parser *); |
a723baf1 | 1615 | static tree cp_parser_asm_operand_list |
94edc4ab | 1616 | (cp_parser *); |
a723baf1 | 1617 | static tree cp_parser_asm_clobber_list |
94edc4ab | 1618 | (cp_parser *); |
a723baf1 | 1619 | static tree cp_parser_attributes_opt |
94edc4ab | 1620 | (cp_parser *); |
a723baf1 | 1621 | static tree cp_parser_attribute_list |
94edc4ab | 1622 | (cp_parser *); |
a723baf1 | 1623 | static bool cp_parser_extension_opt |
94edc4ab | 1624 | (cp_parser *, int *); |
a723baf1 | 1625 | static void cp_parser_label_declaration |
94edc4ab | 1626 | (cp_parser *); |
a723baf1 MM |
1627 | |
1628 | /* Utility Routines */ | |
1629 | ||
1630 | static tree cp_parser_lookup_name | |
b0bc6e8e | 1631 | (cp_parser *, tree, bool, bool, bool, bool); |
a723baf1 | 1632 | static tree cp_parser_lookup_name_simple |
94edc4ab | 1633 | (cp_parser *, tree); |
a723baf1 MM |
1634 | static tree cp_parser_maybe_treat_template_as_class |
1635 | (tree, bool); | |
1636 | static bool cp_parser_check_declarator_template_parameters | |
94edc4ab | 1637 | (cp_parser *, tree); |
a723baf1 | 1638 | static bool cp_parser_check_template_parameters |
94edc4ab | 1639 | (cp_parser *, unsigned); |
d6b4ea85 MM |
1640 | static tree cp_parser_simple_cast_expression |
1641 | (cp_parser *); | |
a723baf1 | 1642 | static tree cp_parser_binary_expression |
94edc4ab | 1643 | (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn); |
a723baf1 | 1644 | static tree cp_parser_global_scope_opt |
94edc4ab | 1645 | (cp_parser *, bool); |
a723baf1 MM |
1646 | static bool cp_parser_constructor_declarator_p |
1647 | (cp_parser *, bool); | |
1648 | static tree cp_parser_function_definition_from_specifiers_and_declarator | |
94edc4ab | 1649 | (cp_parser *, tree, tree, tree); |
a723baf1 | 1650 | static tree cp_parser_function_definition_after_declarator |
94edc4ab | 1651 | (cp_parser *, bool); |
a723baf1 | 1652 | static void cp_parser_template_declaration_after_export |
94edc4ab | 1653 | (cp_parser *, bool); |
a723baf1 | 1654 | static tree cp_parser_single_declaration |
94edc4ab | 1655 | (cp_parser *, bool, bool *); |
a723baf1 | 1656 | static tree cp_parser_functional_cast |
94edc4ab | 1657 | (cp_parser *, tree); |
4bb8ca28 MM |
1658 | static tree cp_parser_save_member_function_body |
1659 | (cp_parser *, tree, tree, tree); | |
ec75414f MM |
1660 | static tree cp_parser_enclosed_template_argument_list |
1661 | (cp_parser *); | |
8db1028e NS |
1662 | static void cp_parser_save_default_args |
1663 | (cp_parser *, tree); | |
a723baf1 | 1664 | static void cp_parser_late_parsing_for_member |
94edc4ab | 1665 | (cp_parser *, tree); |
a723baf1 | 1666 | static void cp_parser_late_parsing_default_args |
8218bd34 | 1667 | (cp_parser *, tree); |
a723baf1 | 1668 | static tree cp_parser_sizeof_operand |
94edc4ab | 1669 | (cp_parser *, enum rid); |
a723baf1 | 1670 | static bool cp_parser_declares_only_class_p |
94edc4ab | 1671 | (cp_parser *); |
d17811fd MM |
1672 | static tree cp_parser_fold_non_dependent_expr |
1673 | (tree); | |
a723baf1 | 1674 | static bool cp_parser_friend_p |
94edc4ab | 1675 | (tree); |
a723baf1 | 1676 | static cp_token *cp_parser_require |
94edc4ab | 1677 | (cp_parser *, enum cpp_ttype, const char *); |
a723baf1 | 1678 | static cp_token *cp_parser_require_keyword |
94edc4ab | 1679 | (cp_parser *, enum rid, const char *); |
a723baf1 | 1680 | static bool cp_parser_token_starts_function_definition_p |
94edc4ab | 1681 | (cp_token *); |
a723baf1 MM |
1682 | static bool cp_parser_next_token_starts_class_definition_p |
1683 | (cp_parser *); | |
d17811fd MM |
1684 | static bool cp_parser_next_token_ends_template_argument_p |
1685 | (cp_parser *); | |
f4abade9 GB |
1686 | static bool cp_parser_nth_token_starts_template_argument_list_p |
1687 | (cp_parser *, size_t); | |
a723baf1 | 1688 | static enum tag_types cp_parser_token_is_class_key |
94edc4ab | 1689 | (cp_token *); |
a723baf1 MM |
1690 | static void cp_parser_check_class_key |
1691 | (enum tag_types, tree type); | |
37d407a1 KL |
1692 | static void cp_parser_check_access_in_redeclaration |
1693 | (tree type); | |
a723baf1 MM |
1694 | static bool cp_parser_optional_template_keyword |
1695 | (cp_parser *); | |
2050a1bb MM |
1696 | static void cp_parser_pre_parsed_nested_name_specifier |
1697 | (cp_parser *); | |
a723baf1 MM |
1698 | static void cp_parser_cache_group |
1699 | (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned); | |
1700 | static void cp_parser_parse_tentatively | |
94edc4ab | 1701 | (cp_parser *); |
a723baf1 | 1702 | static void cp_parser_commit_to_tentative_parse |
94edc4ab | 1703 | (cp_parser *); |
a723baf1 | 1704 | static void cp_parser_abort_tentative_parse |
94edc4ab | 1705 | (cp_parser *); |
a723baf1 | 1706 | static bool cp_parser_parse_definitely |
94edc4ab | 1707 | (cp_parser *); |
f7b5ecd9 | 1708 | static inline bool cp_parser_parsing_tentatively |
94edc4ab | 1709 | (cp_parser *); |
a723baf1 | 1710 | static bool cp_parser_committed_to_tentative_parse |
94edc4ab | 1711 | (cp_parser *); |
a723baf1 | 1712 | static void cp_parser_error |
94edc4ab | 1713 | (cp_parser *, const char *); |
4bb8ca28 MM |
1714 | static void cp_parser_name_lookup_error |
1715 | (cp_parser *, tree, tree, const char *); | |
e5976695 | 1716 | static bool cp_parser_simulate_error |
94edc4ab | 1717 | (cp_parser *); |
a723baf1 | 1718 | static void cp_parser_check_type_definition |
94edc4ab | 1719 | (cp_parser *); |
560ad596 MM |
1720 | static void cp_parser_check_for_definition_in_return_type |
1721 | (tree, int); | |
ee43dab5 MM |
1722 | static void cp_parser_check_for_invalid_template_id |
1723 | (cp_parser *, tree); | |
67c03833 | 1724 | static tree cp_parser_non_integral_constant_expression |
14d22dd6 | 1725 | (const char *); |
8fbc5ae7 MM |
1726 | static bool cp_parser_diagnose_invalid_type_name |
1727 | (cp_parser *); | |
7efa3e22 | 1728 | static int cp_parser_skip_to_closing_parenthesis |
a668c6ad | 1729 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1730 | static void cp_parser_skip_to_end_of_statement |
94edc4ab | 1731 | (cp_parser *); |
e0860732 MM |
1732 | static void cp_parser_consume_semicolon_at_end_of_statement |
1733 | (cp_parser *); | |
a723baf1 | 1734 | static void cp_parser_skip_to_end_of_block_or_statement |
94edc4ab | 1735 | (cp_parser *); |
a723baf1 MM |
1736 | static void cp_parser_skip_to_closing_brace |
1737 | (cp_parser *); | |
1738 | static void cp_parser_skip_until_found | |
94edc4ab | 1739 | (cp_parser *, enum cpp_ttype, const char *); |
a723baf1 | 1740 | static bool cp_parser_error_occurred |
94edc4ab | 1741 | (cp_parser *); |
a723baf1 | 1742 | static bool cp_parser_allow_gnu_extensions_p |
94edc4ab | 1743 | (cp_parser *); |
a723baf1 | 1744 | static bool cp_parser_is_string_literal |
94edc4ab | 1745 | (cp_token *); |
a723baf1 | 1746 | static bool cp_parser_is_keyword |
94edc4ab | 1747 | (cp_token *, enum rid); |
a723baf1 | 1748 | |
4de8668e | 1749 | /* Returns nonzero if we are parsing tentatively. */ |
f7b5ecd9 MM |
1750 | |
1751 | static inline bool | |
94edc4ab | 1752 | cp_parser_parsing_tentatively (cp_parser* parser) |
f7b5ecd9 MM |
1753 | { |
1754 | return parser->context->next != NULL; | |
1755 | } | |
1756 | ||
4de8668e | 1757 | /* Returns nonzero if TOKEN is a string literal. */ |
a723baf1 MM |
1758 | |
1759 | static bool | |
94edc4ab | 1760 | cp_parser_is_string_literal (cp_token* token) |
a723baf1 MM |
1761 | { |
1762 | return (token->type == CPP_STRING || token->type == CPP_WSTRING); | |
1763 | } | |
1764 | ||
4de8668e | 1765 | /* Returns nonzero if TOKEN is the indicated KEYWORD. */ |
a723baf1 MM |
1766 | |
1767 | static bool | |
94edc4ab | 1768 | cp_parser_is_keyword (cp_token* token, enum rid keyword) |
a723baf1 MM |
1769 | { |
1770 | return token->keyword == keyword; | |
1771 | } | |
1772 | ||
a723baf1 MM |
1773 | /* Issue the indicated error MESSAGE. */ |
1774 | ||
1775 | static void | |
94edc4ab | 1776 | cp_parser_error (cp_parser* parser, const char* message) |
a723baf1 | 1777 | { |
a723baf1 | 1778 | /* Output the MESSAGE -- unless we're parsing tentatively. */ |
e5976695 | 1779 | if (!cp_parser_simulate_error (parser)) |
4bb8ca28 MM |
1780 | { |
1781 | cp_token *token; | |
1782 | token = cp_lexer_peek_token (parser->lexer); | |
5c832178 MM |
1783 | c_parse_error (message, |
1784 | /* Because c_parser_error does not understand | |
1785 | CPP_KEYWORD, keywords are treated like | |
1786 | identifiers. */ | |
1787 | (token->type == CPP_KEYWORD ? CPP_NAME : token->type), | |
1788 | token->value); | |
4bb8ca28 MM |
1789 | } |
1790 | } | |
1791 | ||
1792 | /* Issue an error about name-lookup failing. NAME is the | |
1793 | IDENTIFIER_NODE DECL is the result of | |
1794 | the lookup (as returned from cp_parser_lookup_name). DESIRED is | |
1795 | the thing that we hoped to find. */ | |
1796 | ||
1797 | static void | |
1798 | cp_parser_name_lookup_error (cp_parser* parser, | |
1799 | tree name, | |
1800 | tree decl, | |
1801 | const char* desired) | |
1802 | { | |
1803 | /* If name lookup completely failed, tell the user that NAME was not | |
1804 | declared. */ | |
1805 | if (decl == error_mark_node) | |
1806 | { | |
1807 | if (parser->scope && parser->scope != global_namespace) | |
1808 | error ("`%D::%D' has not been declared", | |
1809 | parser->scope, name); | |
1810 | else if (parser->scope == global_namespace) | |
1811 | error ("`::%D' has not been declared", name); | |
1812 | else | |
1813 | error ("`%D' has not been declared", name); | |
1814 | } | |
1815 | else if (parser->scope && parser->scope != global_namespace) | |
1816 | error ("`%D::%D' %s", parser->scope, name, desired); | |
1817 | else if (parser->scope == global_namespace) | |
1818 | error ("`::%D' %s", name, desired); | |
1819 | else | |
1820 | error ("`%D' %s", name, desired); | |
a723baf1 MM |
1821 | } |
1822 | ||
1823 | /* If we are parsing tentatively, remember that an error has occurred | |
e5976695 MM |
1824 | during this tentative parse. Returns true if the error was |
1825 | simulated; false if a messgae should be issued by the caller. */ | |
a723baf1 | 1826 | |
e5976695 | 1827 | static bool |
94edc4ab | 1828 | cp_parser_simulate_error (cp_parser* parser) |
a723baf1 MM |
1829 | { |
1830 | if (cp_parser_parsing_tentatively (parser) | |
1831 | && !cp_parser_committed_to_tentative_parse (parser)) | |
e5976695 MM |
1832 | { |
1833 | parser->context->status = CP_PARSER_STATUS_KIND_ERROR; | |
1834 | return true; | |
1835 | } | |
1836 | return false; | |
a723baf1 MM |
1837 | } |
1838 | ||
1839 | /* This function is called when a type is defined. If type | |
1840 | definitions are forbidden at this point, an error message is | |
1841 | issued. */ | |
1842 | ||
1843 | static void | |
94edc4ab | 1844 | cp_parser_check_type_definition (cp_parser* parser) |
a723baf1 MM |
1845 | { |
1846 | /* If types are forbidden here, issue a message. */ | |
1847 | if (parser->type_definition_forbidden_message) | |
1848 | /* Use `%s' to print the string in case there are any escape | |
1849 | characters in the message. */ | |
1850 | error ("%s", parser->type_definition_forbidden_message); | |
1851 | } | |
1852 | ||
560ad596 MM |
1853 | /* This function is called when a declaration is parsed. If |
1854 | DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM | |
1855 | indicates that a type was defined in the decl-specifiers for DECL, | |
1856 | then an error is issued. */ | |
1857 | ||
1858 | static void | |
1859 | cp_parser_check_for_definition_in_return_type (tree declarator, | |
1860 | int declares_class_or_enum) | |
1861 | { | |
1862 | /* [dcl.fct] forbids type definitions in return types. | |
1863 | Unfortunately, it's not easy to know whether or not we are | |
1864 | processing a return type until after the fact. */ | |
1865 | while (declarator | |
1866 | && (TREE_CODE (declarator) == INDIRECT_REF | |
1867 | || TREE_CODE (declarator) == ADDR_EXPR)) | |
1868 | declarator = TREE_OPERAND (declarator, 0); | |
1869 | if (declarator | |
1870 | && TREE_CODE (declarator) == CALL_EXPR | |
1871 | && declares_class_or_enum & 2) | |
1872 | error ("new types may not be defined in a return type"); | |
1873 | } | |
1874 | ||
ee43dab5 MM |
1875 | /* A type-specifier (TYPE) has been parsed which cannot be followed by |
1876 | "<" in any valid C++ program. If the next token is indeed "<", | |
1877 | issue a message warning the user about what appears to be an | |
1878 | invalid attempt to form a template-id. */ | |
1879 | ||
1880 | static void | |
1881 | cp_parser_check_for_invalid_template_id (cp_parser* parser, | |
1882 | tree type) | |
1883 | { | |
1884 | ptrdiff_t start; | |
1885 | cp_token *token; | |
1886 | ||
1887 | if (cp_lexer_next_token_is (parser->lexer, CPP_LESS)) | |
1888 | { | |
1889 | if (TYPE_P (type)) | |
1890 | error ("`%T' is not a template", type); | |
1891 | else if (TREE_CODE (type) == IDENTIFIER_NODE) | |
1892 | error ("`%s' is not a template", IDENTIFIER_POINTER (type)); | |
1893 | else | |
1894 | error ("invalid template-id"); | |
1895 | /* Remember the location of the invalid "<". */ | |
1896 | if (cp_parser_parsing_tentatively (parser) | |
1897 | && !cp_parser_committed_to_tentative_parse (parser)) | |
1898 | { | |
1899 | token = cp_lexer_peek_token (parser->lexer); | |
1900 | token = cp_lexer_prev_token (parser->lexer, token); | |
1901 | start = cp_lexer_token_difference (parser->lexer, | |
1902 | parser->lexer->first_token, | |
1903 | token); | |
1904 | } | |
1905 | else | |
1906 | start = -1; | |
1907 | /* Consume the "<". */ | |
1908 | cp_lexer_consume_token (parser->lexer); | |
1909 | /* Parse the template arguments. */ | |
1910 | cp_parser_enclosed_template_argument_list (parser); | |
da1d7781 | 1911 | /* Permanently remove the invalid template arguments so that |
ee43dab5 MM |
1912 | this error message is not issued again. */ |
1913 | if (start >= 0) | |
1914 | { | |
1915 | token = cp_lexer_advance_token (parser->lexer, | |
1916 | parser->lexer->first_token, | |
1917 | start); | |
1918 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
1919 | } | |
1920 | } | |
1921 | } | |
1922 | ||
cd0be382 | 1923 | /* Issue an error message about the fact that THING appeared in a |
14d22dd6 MM |
1924 | constant-expression. Returns ERROR_MARK_NODE. */ |
1925 | ||
1926 | static tree | |
67c03833 | 1927 | cp_parser_non_integral_constant_expression (const char *thing) |
14d22dd6 MM |
1928 | { |
1929 | error ("%s cannot appear in a constant-expression", thing); | |
1930 | return error_mark_node; | |
1931 | } | |
1932 | ||
8fbc5ae7 MM |
1933 | /* Check for a common situation where a type-name should be present, |
1934 | but is not, and issue a sensible error message. Returns true if an | |
1935 | invalid type-name was detected. */ | |
1936 | ||
1937 | static bool | |
1938 | cp_parser_diagnose_invalid_type_name (cp_parser *parser) | |
1939 | { | |
1940 | /* If the next two tokens are both identifiers, the code is | |
1941 | erroneous. The usual cause of this situation is code like: | |
1942 | ||
1943 | T t; | |
1944 | ||
1945 | where "T" should name a type -- but does not. */ | |
1946 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
1947 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME) | |
1948 | { | |
1949 | tree name; | |
1950 | ||
8d241e0b | 1951 | /* If parsing tentatively, we should commit; we really are |
8fbc5ae7 MM |
1952 | looking at a declaration. */ |
1953 | /* Consume the first identifier. */ | |
1954 | name = cp_lexer_consume_token (parser->lexer)->value; | |
1955 | /* Issue an error message. */ | |
1956 | error ("`%s' does not name a type", IDENTIFIER_POINTER (name)); | |
1957 | /* If we're in a template class, it's possible that the user was | |
1958 | referring to a type from a base class. For example: | |
1959 | ||
1960 | template <typename T> struct A { typedef T X; }; | |
1961 | template <typename T> struct B : public A<T> { X x; }; | |
1962 | ||
1963 | The user should have said "typename A<T>::X". */ | |
1964 | if (processing_template_decl && current_class_type) | |
1965 | { | |
1966 | tree b; | |
1967 | ||
1968 | for (b = TREE_CHAIN (TYPE_BINFO (current_class_type)); | |
1969 | b; | |
1970 | b = TREE_CHAIN (b)) | |
1971 | { | |
1972 | tree base_type = BINFO_TYPE (b); | |
1973 | if (CLASS_TYPE_P (base_type) | |
1fb3244a | 1974 | && dependent_type_p (base_type)) |
8fbc5ae7 MM |
1975 | { |
1976 | tree field; | |
1977 | /* Go from a particular instantiation of the | |
1978 | template (which will have an empty TYPE_FIELDs), | |
1979 | to the main version. */ | |
353b4fc0 | 1980 | base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type); |
8fbc5ae7 MM |
1981 | for (field = TYPE_FIELDS (base_type); |
1982 | field; | |
1983 | field = TREE_CHAIN (field)) | |
1984 | if (TREE_CODE (field) == TYPE_DECL | |
1985 | && DECL_NAME (field) == name) | |
1986 | { | |
1987 | error ("(perhaps `typename %T::%s' was intended)", | |
1988 | BINFO_TYPE (b), IDENTIFIER_POINTER (name)); | |
1989 | break; | |
1990 | } | |
1991 | if (field) | |
1992 | break; | |
1993 | } | |
1994 | } | |
1995 | } | |
1996 | /* Skip to the end of the declaration; there's no point in | |
1997 | trying to process it. */ | |
1998 | cp_parser_skip_to_end_of_statement (parser); | |
1999 | ||
2000 | return true; | |
2001 | } | |
2002 | ||
2003 | return false; | |
2004 | } | |
2005 | ||
a723baf1 | 2006 | /* Consume tokens up to, and including, the next non-nested closing `)'. |
7efa3e22 NS |
2007 | Returns 1 iff we found a closing `)'. RECOVERING is true, if we |
2008 | are doing error recovery. Returns -1 if OR_COMMA is true and we | |
2009 | found an unnested comma. */ | |
a723baf1 | 2010 | |
7efa3e22 NS |
2011 | static int |
2012 | cp_parser_skip_to_closing_parenthesis (cp_parser *parser, | |
a668c6ad MM |
2013 | bool recovering, |
2014 | bool or_comma, | |
2015 | bool consume_paren) | |
a723baf1 | 2016 | { |
7efa3e22 NS |
2017 | unsigned paren_depth = 0; |
2018 | unsigned brace_depth = 0; | |
a723baf1 | 2019 | |
7efa3e22 NS |
2020 | if (recovering && !or_comma && cp_parser_parsing_tentatively (parser) |
2021 | && !cp_parser_committed_to_tentative_parse (parser)) | |
2022 | return 0; | |
2023 | ||
a723baf1 MM |
2024 | while (true) |
2025 | { | |
2026 | cp_token *token; | |
7efa3e22 | 2027 | |
a723baf1 MM |
2028 | /* If we've run out of tokens, then there is no closing `)'. */ |
2029 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
7efa3e22 | 2030 | return 0; |
a723baf1 | 2031 | |
a668c6ad MM |
2032 | token = cp_lexer_peek_token (parser->lexer); |
2033 | ||
f4f206f4 | 2034 | /* This matches the processing in skip_to_end_of_statement. */ |
a668c6ad MM |
2035 | if (token->type == CPP_SEMICOLON && !brace_depth) |
2036 | return 0; | |
2037 | if (token->type == CPP_OPEN_BRACE) | |
2038 | ++brace_depth; | |
2039 | if (token->type == CPP_CLOSE_BRACE) | |
7efa3e22 | 2040 | { |
a668c6ad | 2041 | if (!brace_depth--) |
7efa3e22 | 2042 | return 0; |
7efa3e22 | 2043 | } |
a668c6ad MM |
2044 | if (recovering && or_comma && token->type == CPP_COMMA |
2045 | && !brace_depth && !paren_depth) | |
2046 | return -1; | |
7efa3e22 | 2047 | |
7efa3e22 NS |
2048 | if (!brace_depth) |
2049 | { | |
2050 | /* If it is an `(', we have entered another level of nesting. */ | |
2051 | if (token->type == CPP_OPEN_PAREN) | |
2052 | ++paren_depth; | |
2053 | /* If it is a `)', then we might be done. */ | |
2054 | else if (token->type == CPP_CLOSE_PAREN && !paren_depth--) | |
a668c6ad MM |
2055 | { |
2056 | if (consume_paren) | |
2057 | cp_lexer_consume_token (parser->lexer); | |
2058 | return 1; | |
2059 | } | |
7efa3e22 | 2060 | } |
a668c6ad MM |
2061 | |
2062 | /* Consume the token. */ | |
2063 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 MM |
2064 | } |
2065 | } | |
2066 | ||
2067 | /* Consume tokens until we reach the end of the current statement. | |
2068 | Normally, that will be just before consuming a `;'. However, if a | |
2069 | non-nested `}' comes first, then we stop before consuming that. */ | |
2070 | ||
2071 | static void | |
94edc4ab | 2072 | cp_parser_skip_to_end_of_statement (cp_parser* parser) |
a723baf1 MM |
2073 | { |
2074 | unsigned nesting_depth = 0; | |
2075 | ||
2076 | while (true) | |
2077 | { | |
2078 | cp_token *token; | |
2079 | ||
2080 | /* Peek at the next token. */ | |
2081 | token = cp_lexer_peek_token (parser->lexer); | |
2082 | /* If we've run out of tokens, stop. */ | |
2083 | if (token->type == CPP_EOF) | |
2084 | break; | |
2085 | /* If the next token is a `;', we have reached the end of the | |
2086 | statement. */ | |
2087 | if (token->type == CPP_SEMICOLON && !nesting_depth) | |
2088 | break; | |
2089 | /* If the next token is a non-nested `}', then we have reached | |
2090 | the end of the current block. */ | |
2091 | if (token->type == CPP_CLOSE_BRACE) | |
2092 | { | |
2093 | /* If this is a non-nested `}', stop before consuming it. | |
2094 | That way, when confronted with something like: | |
2095 | ||
2096 | { 3 + } | |
2097 | ||
2098 | we stop before consuming the closing `}', even though we | |
2099 | have not yet reached a `;'. */ | |
2100 | if (nesting_depth == 0) | |
2101 | break; | |
2102 | /* If it is the closing `}' for a block that we have | |
2103 | scanned, stop -- but only after consuming the token. | |
2104 | That way given: | |
2105 | ||
2106 | void f g () { ... } | |
2107 | typedef int I; | |
2108 | ||
2109 | we will stop after the body of the erroneously declared | |
2110 | function, but before consuming the following `typedef' | |
2111 | declaration. */ | |
2112 | if (--nesting_depth == 0) | |
2113 | { | |
2114 | cp_lexer_consume_token (parser->lexer); | |
2115 | break; | |
2116 | } | |
2117 | } | |
2118 | /* If it the next token is a `{', then we are entering a new | |
2119 | block. Consume the entire block. */ | |
2120 | else if (token->type == CPP_OPEN_BRACE) | |
2121 | ++nesting_depth; | |
2122 | /* Consume the token. */ | |
2123 | cp_lexer_consume_token (parser->lexer); | |
2124 | } | |
2125 | } | |
2126 | ||
e0860732 MM |
2127 | /* This function is called at the end of a statement or declaration. |
2128 | If the next token is a semicolon, it is consumed; otherwise, error | |
2129 | recovery is attempted. */ | |
2130 | ||
2131 | static void | |
2132 | cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser) | |
2133 | { | |
2134 | /* Look for the trailing `;'. */ | |
2135 | if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")) | |
2136 | { | |
2137 | /* If there is additional (erroneous) input, skip to the end of | |
2138 | the statement. */ | |
2139 | cp_parser_skip_to_end_of_statement (parser); | |
2140 | /* If the next token is now a `;', consume it. */ | |
2141 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
2142 | cp_lexer_consume_token (parser->lexer); | |
2143 | } | |
2144 | } | |
2145 | ||
a723baf1 MM |
2146 | /* Skip tokens until we have consumed an entire block, or until we |
2147 | have consumed a non-nested `;'. */ | |
2148 | ||
2149 | static void | |
94edc4ab | 2150 | cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser) |
a723baf1 MM |
2151 | { |
2152 | unsigned nesting_depth = 0; | |
2153 | ||
2154 | while (true) | |
2155 | { | |
2156 | cp_token *token; | |
2157 | ||
2158 | /* Peek at the next token. */ | |
2159 | token = cp_lexer_peek_token (parser->lexer); | |
2160 | /* If we've run out of tokens, stop. */ | |
2161 | if (token->type == CPP_EOF) | |
2162 | break; | |
2163 | /* If the next token is a `;', we have reached the end of the | |
2164 | statement. */ | |
2165 | if (token->type == CPP_SEMICOLON && !nesting_depth) | |
2166 | { | |
2167 | /* Consume the `;'. */ | |
2168 | cp_lexer_consume_token (parser->lexer); | |
2169 | break; | |
2170 | } | |
2171 | /* Consume the token. */ | |
2172 | token = cp_lexer_consume_token (parser->lexer); | |
2173 | /* If the next token is a non-nested `}', then we have reached | |
2174 | the end of the current block. */ | |
2175 | if (token->type == CPP_CLOSE_BRACE | |
2176 | && (nesting_depth == 0 || --nesting_depth == 0)) | |
2177 | break; | |
2178 | /* If it the next token is a `{', then we are entering a new | |
2179 | block. Consume the entire block. */ | |
2180 | if (token->type == CPP_OPEN_BRACE) | |
2181 | ++nesting_depth; | |
2182 | } | |
2183 | } | |
2184 | ||
2185 | /* Skip tokens until a non-nested closing curly brace is the next | |
2186 | token. */ | |
2187 | ||
2188 | static void | |
2189 | cp_parser_skip_to_closing_brace (cp_parser *parser) | |
2190 | { | |
2191 | unsigned nesting_depth = 0; | |
2192 | ||
2193 | while (true) | |
2194 | { | |
2195 | cp_token *token; | |
2196 | ||
2197 | /* Peek at the next token. */ | |
2198 | token = cp_lexer_peek_token (parser->lexer); | |
2199 | /* If we've run out of tokens, stop. */ | |
2200 | if (token->type == CPP_EOF) | |
2201 | break; | |
2202 | /* If the next token is a non-nested `}', then we have reached | |
2203 | the end of the current block. */ | |
2204 | if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0) | |
2205 | break; | |
2206 | /* If it the next token is a `{', then we are entering a new | |
2207 | block. Consume the entire block. */ | |
2208 | else if (token->type == CPP_OPEN_BRACE) | |
2209 | ++nesting_depth; | |
2210 | /* Consume the token. */ | |
2211 | cp_lexer_consume_token (parser->lexer); | |
2212 | } | |
2213 | } | |
2214 | ||
2215 | /* Create a new C++ parser. */ | |
2216 | ||
2217 | static cp_parser * | |
94edc4ab | 2218 | cp_parser_new (void) |
a723baf1 MM |
2219 | { |
2220 | cp_parser *parser; | |
17211ab5 GK |
2221 | cp_lexer *lexer; |
2222 | ||
2223 | /* cp_lexer_new_main is called before calling ggc_alloc because | |
2224 | cp_lexer_new_main might load a PCH file. */ | |
2225 | lexer = cp_lexer_new_main (); | |
a723baf1 | 2226 | |
c68b0a84 | 2227 | parser = ggc_alloc_cleared (sizeof (cp_parser)); |
17211ab5 | 2228 | parser->lexer = lexer; |
a723baf1 MM |
2229 | parser->context = cp_parser_context_new (NULL); |
2230 | ||
2231 | /* For now, we always accept GNU extensions. */ | |
2232 | parser->allow_gnu_extensions_p = 1; | |
2233 | ||
2234 | /* The `>' token is a greater-than operator, not the end of a | |
2235 | template-id. */ | |
2236 | parser->greater_than_is_operator_p = true; | |
2237 | ||
2238 | parser->default_arg_ok_p = true; | |
2239 | ||
2240 | /* We are not parsing a constant-expression. */ | |
67c03833 JM |
2241 | parser->integral_constant_expression_p = false; |
2242 | parser->allow_non_integral_constant_expression_p = false; | |
2243 | parser->non_integral_constant_expression_p = false; | |
a723baf1 | 2244 | |
263ee052 MM |
2245 | /* We are not parsing offsetof. */ |
2246 | parser->in_offsetof_p = false; | |
2247 | ||
a723baf1 MM |
2248 | /* Local variable names are not forbidden. */ |
2249 | parser->local_variables_forbidden_p = false; | |
2250 | ||
34cd5ae7 | 2251 | /* We are not processing an `extern "C"' declaration. */ |
a723baf1 MM |
2252 | parser->in_unbraced_linkage_specification_p = false; |
2253 | ||
2254 | /* We are not processing a declarator. */ | |
2255 | parser->in_declarator_p = false; | |
2256 | ||
4bb8ca28 MM |
2257 | /* We are not processing a template-argument-list. */ |
2258 | parser->in_template_argument_list_p = false; | |
2259 | ||
0e59b3fb MM |
2260 | /* We are not in an iteration statement. */ |
2261 | parser->in_iteration_statement_p = false; | |
2262 | ||
2263 | /* We are not in a switch statement. */ | |
2264 | parser->in_switch_statement_p = false; | |
2265 | ||
4f8163b1 MM |
2266 | /* We are not parsing a type-id inside an expression. */ |
2267 | parser->in_type_id_in_expr_p = false; | |
2268 | ||
a723baf1 MM |
2269 | /* The unparsed function queue is empty. */ |
2270 | parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE); | |
2271 | ||
2272 | /* There are no classes being defined. */ | |
2273 | parser->num_classes_being_defined = 0; | |
2274 | ||
2275 | /* No template parameters apply. */ | |
2276 | parser->num_template_parameter_lists = 0; | |
2277 | ||
2278 | return parser; | |
2279 | } | |
2280 | ||
2281 | /* Lexical conventions [gram.lex] */ | |
2282 | ||
2283 | /* Parse an identifier. Returns an IDENTIFIER_NODE representing the | |
2284 | identifier. */ | |
2285 | ||
2286 | static tree | |
94edc4ab | 2287 | cp_parser_identifier (cp_parser* parser) |
a723baf1 MM |
2288 | { |
2289 | cp_token *token; | |
2290 | ||
2291 | /* Look for the identifier. */ | |
2292 | token = cp_parser_require (parser, CPP_NAME, "identifier"); | |
2293 | /* Return the value. */ | |
2294 | return token ? token->value : error_mark_node; | |
2295 | } | |
2296 | ||
2297 | /* Basic concepts [gram.basic] */ | |
2298 | ||
2299 | /* Parse a translation-unit. | |
2300 | ||
2301 | translation-unit: | |
2302 | declaration-seq [opt] | |
2303 | ||
2304 | Returns TRUE if all went well. */ | |
2305 | ||
2306 | static bool | |
94edc4ab | 2307 | cp_parser_translation_unit (cp_parser* parser) |
a723baf1 MM |
2308 | { |
2309 | while (true) | |
2310 | { | |
2311 | cp_parser_declaration_seq_opt (parser); | |
2312 | ||
2313 | /* If there are no tokens left then all went well. */ | |
2314 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
2315 | break; | |
2316 | ||
2317 | /* Otherwise, issue an error message. */ | |
2318 | cp_parser_error (parser, "expected declaration"); | |
2319 | return false; | |
2320 | } | |
2321 | ||
2322 | /* Consume the EOF token. */ | |
2323 | cp_parser_require (parser, CPP_EOF, "end-of-file"); | |
2324 | ||
2325 | /* Finish up. */ | |
2326 | finish_translation_unit (); | |
2327 | ||
2328 | /* All went well. */ | |
2329 | return true; | |
2330 | } | |
2331 | ||
2332 | /* Expressions [gram.expr] */ | |
2333 | ||
2334 | /* Parse a primary-expression. | |
2335 | ||
2336 | primary-expression: | |
2337 | literal | |
2338 | this | |
2339 | ( expression ) | |
2340 | id-expression | |
2341 | ||
2342 | GNU Extensions: | |
2343 | ||
2344 | primary-expression: | |
2345 | ( compound-statement ) | |
2346 | __builtin_va_arg ( assignment-expression , type-id ) | |
2347 | ||
2348 | literal: | |
2349 | __null | |
2350 | ||
2351 | Returns a representation of the expression. | |
2352 | ||
2353 | *IDK indicates what kind of id-expression (if any) was present. | |
2354 | ||
2355 | *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be | |
2356 | used as the operand of a pointer-to-member. In that case, | |
2357 | *QUALIFYING_CLASS gives the class that is used as the qualifying | |
2358 | class in the pointer-to-member. */ | |
2359 | ||
2360 | static tree | |
2361 | cp_parser_primary_expression (cp_parser *parser, | |
b3445994 | 2362 | cp_id_kind *idk, |
a723baf1 MM |
2363 | tree *qualifying_class) |
2364 | { | |
2365 | cp_token *token; | |
2366 | ||
2367 | /* Assume the primary expression is not an id-expression. */ | |
b3445994 | 2368 | *idk = CP_ID_KIND_NONE; |
a723baf1 MM |
2369 | /* And that it cannot be used as pointer-to-member. */ |
2370 | *qualifying_class = NULL_TREE; | |
2371 | ||
2372 | /* Peek at the next token. */ | |
2373 | token = cp_lexer_peek_token (parser->lexer); | |
2374 | switch (token->type) | |
2375 | { | |
2376 | /* literal: | |
2377 | integer-literal | |
2378 | character-literal | |
2379 | floating-literal | |
2380 | string-literal | |
2381 | boolean-literal */ | |
2382 | case CPP_CHAR: | |
2383 | case CPP_WCHAR: | |
2384 | case CPP_STRING: | |
2385 | case CPP_WSTRING: | |
2386 | case CPP_NUMBER: | |
2387 | token = cp_lexer_consume_token (parser->lexer); | |
2388 | return token->value; | |
2389 | ||
2390 | case CPP_OPEN_PAREN: | |
2391 | { | |
2392 | tree expr; | |
2393 | bool saved_greater_than_is_operator_p; | |
2394 | ||
2395 | /* Consume the `('. */ | |
2396 | cp_lexer_consume_token (parser->lexer); | |
2397 | /* Within a parenthesized expression, a `>' token is always | |
2398 | the greater-than operator. */ | |
2399 | saved_greater_than_is_operator_p | |
2400 | = parser->greater_than_is_operator_p; | |
2401 | parser->greater_than_is_operator_p = true; | |
2402 | /* If we see `( { ' then we are looking at the beginning of | |
2403 | a GNU statement-expression. */ | |
2404 | if (cp_parser_allow_gnu_extensions_p (parser) | |
2405 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
2406 | { | |
2407 | /* Statement-expressions are not allowed by the standard. */ | |
2408 | if (pedantic) | |
2409 | pedwarn ("ISO C++ forbids braced-groups within expressions"); | |
2410 | ||
2411 | /* And they're not allowed outside of a function-body; you | |
2412 | cannot, for example, write: | |
2413 | ||
2414 | int i = ({ int j = 3; j + 1; }); | |
2415 | ||
2416 | at class or namespace scope. */ | |
2417 | if (!at_function_scope_p ()) | |
2418 | error ("statement-expressions are allowed only inside functions"); | |
2419 | /* Start the statement-expression. */ | |
2420 | expr = begin_stmt_expr (); | |
2421 | /* Parse the compound-statement. */ | |
a5bcc582 | 2422 | cp_parser_compound_statement (parser, true); |
a723baf1 | 2423 | /* Finish up. */ |
303b7406 | 2424 | expr = finish_stmt_expr (expr, false); |
a723baf1 MM |
2425 | } |
2426 | else | |
2427 | { | |
2428 | /* Parse the parenthesized expression. */ | |
2429 | expr = cp_parser_expression (parser); | |
2430 | /* Let the front end know that this expression was | |
2431 | enclosed in parentheses. This matters in case, for | |
2432 | example, the expression is of the form `A::B', since | |
2433 | `&A::B' might be a pointer-to-member, but `&(A::B)' is | |
2434 | not. */ | |
2435 | finish_parenthesized_expr (expr); | |
2436 | } | |
2437 | /* The `>' token might be the end of a template-id or | |
2438 | template-parameter-list now. */ | |
2439 | parser->greater_than_is_operator_p | |
2440 | = saved_greater_than_is_operator_p; | |
2441 | /* Consume the `)'. */ | |
2442 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
2443 | cp_parser_skip_to_end_of_statement (parser); | |
2444 | ||
2445 | return expr; | |
2446 | } | |
2447 | ||
2448 | case CPP_KEYWORD: | |
2449 | switch (token->keyword) | |
2450 | { | |
2451 | /* These two are the boolean literals. */ | |
2452 | case RID_TRUE: | |
2453 | cp_lexer_consume_token (parser->lexer); | |
2454 | return boolean_true_node; | |
2455 | case RID_FALSE: | |
2456 | cp_lexer_consume_token (parser->lexer); | |
2457 | return boolean_false_node; | |
2458 | ||
2459 | /* The `__null' literal. */ | |
2460 | case RID_NULL: | |
2461 | cp_lexer_consume_token (parser->lexer); | |
2462 | return null_node; | |
2463 | ||
2464 | /* Recognize the `this' keyword. */ | |
2465 | case RID_THIS: | |
2466 | cp_lexer_consume_token (parser->lexer); | |
2467 | if (parser->local_variables_forbidden_p) | |
2468 | { | |
2469 | error ("`this' may not be used in this context"); | |
2470 | return error_mark_node; | |
2471 | } | |
14d22dd6 | 2472 | /* Pointers cannot appear in constant-expressions. */ |
67c03833 | 2473 | if (parser->integral_constant_expression_p) |
14d22dd6 | 2474 | { |
67c03833 JM |
2475 | if (!parser->allow_non_integral_constant_expression_p) |
2476 | return cp_parser_non_integral_constant_expression ("`this'"); | |
2477 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 2478 | } |
a723baf1 MM |
2479 | return finish_this_expr (); |
2480 | ||
2481 | /* The `operator' keyword can be the beginning of an | |
2482 | id-expression. */ | |
2483 | case RID_OPERATOR: | |
2484 | goto id_expression; | |
2485 | ||
2486 | case RID_FUNCTION_NAME: | |
2487 | case RID_PRETTY_FUNCTION_NAME: | |
2488 | case RID_C99_FUNCTION_NAME: | |
2489 | /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and | |
2490 | __func__ are the names of variables -- but they are | |
2491 | treated specially. Therefore, they are handled here, | |
2492 | rather than relying on the generic id-expression logic | |
34cd5ae7 | 2493 | below. Grammatically, these names are id-expressions. |
a723baf1 MM |
2494 | |
2495 | Consume the token. */ | |
2496 | token = cp_lexer_consume_token (parser->lexer); | |
2497 | /* Look up the name. */ | |
2498 | return finish_fname (token->value); | |
2499 | ||
2500 | case RID_VA_ARG: | |
2501 | { | |
2502 | tree expression; | |
2503 | tree type; | |
2504 | ||
2505 | /* The `__builtin_va_arg' construct is used to handle | |
2506 | `va_arg'. Consume the `__builtin_va_arg' token. */ | |
2507 | cp_lexer_consume_token (parser->lexer); | |
2508 | /* Look for the opening `('. */ | |
2509 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
2510 | /* Now, parse the assignment-expression. */ | |
2511 | expression = cp_parser_assignment_expression (parser); | |
2512 | /* Look for the `,'. */ | |
2513 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
2514 | /* Parse the type-id. */ | |
2515 | type = cp_parser_type_id (parser); | |
2516 | /* Look for the closing `)'. */ | |
2517 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
14d22dd6 MM |
2518 | /* Using `va_arg' in a constant-expression is not |
2519 | allowed. */ | |
67c03833 | 2520 | if (parser->integral_constant_expression_p) |
14d22dd6 | 2521 | { |
67c03833 JM |
2522 | if (!parser->allow_non_integral_constant_expression_p) |
2523 | return cp_parser_non_integral_constant_expression ("`va_arg'"); | |
2524 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 2525 | } |
a723baf1 MM |
2526 | return build_x_va_arg (expression, type); |
2527 | } | |
2528 | ||
263ee052 MM |
2529 | case RID_OFFSETOF: |
2530 | { | |
2531 | tree expression; | |
2532 | bool saved_in_offsetof_p; | |
2533 | ||
2534 | /* Consume the "__offsetof__" token. */ | |
2535 | cp_lexer_consume_token (parser->lexer); | |
2536 | /* Consume the opening `('. */ | |
2537 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
2538 | /* Parse the parenthesized (almost) constant-expression. */ | |
2539 | saved_in_offsetof_p = parser->in_offsetof_p; | |
2540 | parser->in_offsetof_p = true; | |
2541 | expression | |
2542 | = cp_parser_constant_expression (parser, | |
2543 | /*allow_non_constant_p=*/false, | |
2544 | /*non_constant_p=*/NULL); | |
2545 | parser->in_offsetof_p = saved_in_offsetof_p; | |
2546 | /* Consume the closing ')'. */ | |
2547 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
2548 | ||
2549 | return expression; | |
2550 | } | |
2551 | ||
a723baf1 MM |
2552 | default: |
2553 | cp_parser_error (parser, "expected primary-expression"); | |
2554 | return error_mark_node; | |
2555 | } | |
a723baf1 MM |
2556 | |
2557 | /* An id-expression can start with either an identifier, a | |
2558 | `::' as the beginning of a qualified-id, or the "operator" | |
2559 | keyword. */ | |
2560 | case CPP_NAME: | |
2561 | case CPP_SCOPE: | |
2562 | case CPP_TEMPLATE_ID: | |
2563 | case CPP_NESTED_NAME_SPECIFIER: | |
2564 | { | |
2565 | tree id_expression; | |
2566 | tree decl; | |
b3445994 | 2567 | const char *error_msg; |
a723baf1 MM |
2568 | |
2569 | id_expression: | |
2570 | /* Parse the id-expression. */ | |
2571 | id_expression | |
2572 | = cp_parser_id_expression (parser, | |
2573 | /*template_keyword_p=*/false, | |
2574 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
2575 | /*template_p=*/NULL, |
2576 | /*declarator_p=*/false); | |
a723baf1 MM |
2577 | if (id_expression == error_mark_node) |
2578 | return error_mark_node; | |
2579 | /* If we have a template-id, then no further lookup is | |
2580 | required. If the template-id was for a template-class, we | |
2581 | will sometimes have a TYPE_DECL at this point. */ | |
2582 | else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR | |
2583 | || TREE_CODE (id_expression) == TYPE_DECL) | |
2584 | decl = id_expression; | |
2585 | /* Look up the name. */ | |
2586 | else | |
2587 | { | |
2588 | decl = cp_parser_lookup_name_simple (parser, id_expression); | |
2589 | /* If name lookup gives us a SCOPE_REF, then the | |
2590 | qualifying scope was dependent. Just propagate the | |
2591 | name. */ | |
2592 | if (TREE_CODE (decl) == SCOPE_REF) | |
2593 | { | |
2594 | if (TYPE_P (TREE_OPERAND (decl, 0))) | |
2595 | *qualifying_class = TREE_OPERAND (decl, 0); | |
2596 | return decl; | |
2597 | } | |
2598 | /* Check to see if DECL is a local variable in a context | |
2599 | where that is forbidden. */ | |
2600 | if (parser->local_variables_forbidden_p | |
2601 | && local_variable_p (decl)) | |
2602 | { | |
2603 | /* It might be that we only found DECL because we are | |
2604 | trying to be generous with pre-ISO scoping rules. | |
2605 | For example, consider: | |
2606 | ||
2607 | int i; | |
2608 | void g() { | |
2609 | for (int i = 0; i < 10; ++i) {} | |
2610 | extern void f(int j = i); | |
2611 | } | |
2612 | ||
2613 | Here, name look up will originally find the out | |
2614 | of scope `i'. We need to issue a warning message, | |
2615 | but then use the global `i'. */ | |
2616 | decl = check_for_out_of_scope_variable (decl); | |
2617 | if (local_variable_p (decl)) | |
2618 | { | |
2619 | error ("local variable `%D' may not appear in this context", | |
2620 | decl); | |
2621 | return error_mark_node; | |
2622 | } | |
2623 | } | |
c006d942 | 2624 | } |
b3445994 MM |
2625 | |
2626 | decl = finish_id_expression (id_expression, decl, parser->scope, | |
2627 | idk, qualifying_class, | |
67c03833 JM |
2628 | parser->integral_constant_expression_p, |
2629 | parser->allow_non_integral_constant_expression_p, | |
2630 | &parser->non_integral_constant_expression_p, | |
b3445994 MM |
2631 | &error_msg); |
2632 | if (error_msg) | |
2633 | cp_parser_error (parser, error_msg); | |
a723baf1 MM |
2634 | return decl; |
2635 | } | |
2636 | ||
2637 | /* Anything else is an error. */ | |
2638 | default: | |
2639 | cp_parser_error (parser, "expected primary-expression"); | |
2640 | return error_mark_node; | |
2641 | } | |
2642 | } | |
2643 | ||
2644 | /* Parse an id-expression. | |
2645 | ||
2646 | id-expression: | |
2647 | unqualified-id | |
2648 | qualified-id | |
2649 | ||
2650 | qualified-id: | |
2651 | :: [opt] nested-name-specifier template [opt] unqualified-id | |
2652 | :: identifier | |
2653 | :: operator-function-id | |
2654 | :: template-id | |
2655 | ||
2656 | Return a representation of the unqualified portion of the | |
2657 | identifier. Sets PARSER->SCOPE to the qualifying scope if there is | |
2658 | a `::' or nested-name-specifier. | |
2659 | ||
2660 | Often, if the id-expression was a qualified-id, the caller will | |
2661 | want to make a SCOPE_REF to represent the qualified-id. This | |
2662 | function does not do this in order to avoid wastefully creating | |
2663 | SCOPE_REFs when they are not required. | |
2664 | ||
a723baf1 MM |
2665 | If TEMPLATE_KEYWORD_P is true, then we have just seen the |
2666 | `template' keyword. | |
2667 | ||
2668 | If CHECK_DEPENDENCY_P is false, then names are looked up inside | |
2669 | uninstantiated templates. | |
2670 | ||
15d2cb19 | 2671 | If *TEMPLATE_P is non-NULL, it is set to true iff the |
a723baf1 | 2672 | `template' keyword is used to explicitly indicate that the entity |
f3c2dfc6 MM |
2673 | named is a template. |
2674 | ||
2675 | If DECLARATOR_P is true, the id-expression is appearing as part of | |
cd0be382 | 2676 | a declarator, rather than as part of an expression. */ |
a723baf1 MM |
2677 | |
2678 | static tree | |
2679 | cp_parser_id_expression (cp_parser *parser, | |
2680 | bool template_keyword_p, | |
2681 | bool check_dependency_p, | |
f3c2dfc6 MM |
2682 | bool *template_p, |
2683 | bool declarator_p) | |
a723baf1 MM |
2684 | { |
2685 | bool global_scope_p; | |
2686 | bool nested_name_specifier_p; | |
2687 | ||
2688 | /* Assume the `template' keyword was not used. */ | |
2689 | if (template_p) | |
2690 | *template_p = false; | |
2691 | ||
2692 | /* Look for the optional `::' operator. */ | |
2693 | global_scope_p | |
2694 | = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) | |
2695 | != NULL_TREE); | |
2696 | /* Look for the optional nested-name-specifier. */ | |
2697 | nested_name_specifier_p | |
2698 | = (cp_parser_nested_name_specifier_opt (parser, | |
2699 | /*typename_keyword_p=*/false, | |
2700 | check_dependency_p, | |
a668c6ad MM |
2701 | /*type_p=*/false, |
2702 | /*is_declarator=*/false) | |
a723baf1 MM |
2703 | != NULL_TREE); |
2704 | /* If there is a nested-name-specifier, then we are looking at | |
2705 | the first qualified-id production. */ | |
2706 | if (nested_name_specifier_p) | |
2707 | { | |
2708 | tree saved_scope; | |
2709 | tree saved_object_scope; | |
2710 | tree saved_qualifying_scope; | |
2711 | tree unqualified_id; | |
2712 | bool is_template; | |
2713 | ||
2714 | /* See if the next token is the `template' keyword. */ | |
2715 | if (!template_p) | |
2716 | template_p = &is_template; | |
2717 | *template_p = cp_parser_optional_template_keyword (parser); | |
2718 | /* Name lookup we do during the processing of the | |
2719 | unqualified-id might obliterate SCOPE. */ | |
2720 | saved_scope = parser->scope; | |
2721 | saved_object_scope = parser->object_scope; | |
2722 | saved_qualifying_scope = parser->qualifying_scope; | |
2723 | /* Process the final unqualified-id. */ | |
2724 | unqualified_id = cp_parser_unqualified_id (parser, *template_p, | |
f3c2dfc6 MM |
2725 | check_dependency_p, |
2726 | declarator_p); | |
a723baf1 MM |
2727 | /* Restore the SAVED_SCOPE for our caller. */ |
2728 | parser->scope = saved_scope; | |
2729 | parser->object_scope = saved_object_scope; | |
2730 | parser->qualifying_scope = saved_qualifying_scope; | |
2731 | ||
2732 | return unqualified_id; | |
2733 | } | |
2734 | /* Otherwise, if we are in global scope, then we are looking at one | |
2735 | of the other qualified-id productions. */ | |
2736 | else if (global_scope_p) | |
2737 | { | |
2738 | cp_token *token; | |
2739 | tree id; | |
2740 | ||
e5976695 MM |
2741 | /* Peek at the next token. */ |
2742 | token = cp_lexer_peek_token (parser->lexer); | |
2743 | ||
2744 | /* If it's an identifier, and the next token is not a "<", then | |
2745 | we can avoid the template-id case. This is an optimization | |
2746 | for this common case. */ | |
2747 | if (token->type == CPP_NAME | |
f4abade9 GB |
2748 | && !cp_parser_nth_token_starts_template_argument_list_p |
2749 | (parser, 2)) | |
e5976695 MM |
2750 | return cp_parser_identifier (parser); |
2751 | ||
a723baf1 MM |
2752 | cp_parser_parse_tentatively (parser); |
2753 | /* Try a template-id. */ | |
2754 | id = cp_parser_template_id (parser, | |
2755 | /*template_keyword_p=*/false, | |
a668c6ad MM |
2756 | /*check_dependency_p=*/true, |
2757 | declarator_p); | |
a723baf1 MM |
2758 | /* If that worked, we're done. */ |
2759 | if (cp_parser_parse_definitely (parser)) | |
2760 | return id; | |
2761 | ||
e5976695 MM |
2762 | /* Peek at the next token. (Changes in the token buffer may |
2763 | have invalidated the pointer obtained above.) */ | |
a723baf1 MM |
2764 | token = cp_lexer_peek_token (parser->lexer); |
2765 | ||
2766 | switch (token->type) | |
2767 | { | |
2768 | case CPP_NAME: | |
2769 | return cp_parser_identifier (parser); | |
2770 | ||
2771 | case CPP_KEYWORD: | |
2772 | if (token->keyword == RID_OPERATOR) | |
2773 | return cp_parser_operator_function_id (parser); | |
2774 | /* Fall through. */ | |
2775 | ||
2776 | default: | |
2777 | cp_parser_error (parser, "expected id-expression"); | |
2778 | return error_mark_node; | |
2779 | } | |
2780 | } | |
2781 | else | |
2782 | return cp_parser_unqualified_id (parser, template_keyword_p, | |
f3c2dfc6 MM |
2783 | /*check_dependency_p=*/true, |
2784 | declarator_p); | |
a723baf1 MM |
2785 | } |
2786 | ||
2787 | /* Parse an unqualified-id. | |
2788 | ||
2789 | unqualified-id: | |
2790 | identifier | |
2791 | operator-function-id | |
2792 | conversion-function-id | |
2793 | ~ class-name | |
2794 | template-id | |
2795 | ||
2796 | If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template' | |
2797 | keyword, in a construct like `A::template ...'. | |
2798 | ||
2799 | Returns a representation of unqualified-id. For the `identifier' | |
2800 | production, an IDENTIFIER_NODE is returned. For the `~ class-name' | |
2801 | production a BIT_NOT_EXPR is returned; the operand of the | |
2802 | BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the | |
2803 | other productions, see the documentation accompanying the | |
2804 | corresponding parsing functions. If CHECK_DEPENDENCY_P is false, | |
f3c2dfc6 MM |
2805 | names are looked up in uninstantiated templates. If DECLARATOR_P |
2806 | is true, the unqualified-id is appearing as part of a declarator, | |
2807 | rather than as part of an expression. */ | |
a723baf1 MM |
2808 | |
2809 | static tree | |
94edc4ab NN |
2810 | cp_parser_unqualified_id (cp_parser* parser, |
2811 | bool template_keyword_p, | |
f3c2dfc6 MM |
2812 | bool check_dependency_p, |
2813 | bool declarator_p) | |
a723baf1 MM |
2814 | { |
2815 | cp_token *token; | |
2816 | ||
2817 | /* Peek at the next token. */ | |
2818 | token = cp_lexer_peek_token (parser->lexer); | |
2819 | ||
2820 | switch (token->type) | |
2821 | { | |
2822 | case CPP_NAME: | |
2823 | { | |
2824 | tree id; | |
2825 | ||
2826 | /* We don't know yet whether or not this will be a | |
2827 | template-id. */ | |
2828 | cp_parser_parse_tentatively (parser); | |
2829 | /* Try a template-id. */ | |
2830 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
2831 | check_dependency_p, |
2832 | declarator_p); | |
a723baf1 MM |
2833 | /* If it worked, we're done. */ |
2834 | if (cp_parser_parse_definitely (parser)) | |
2835 | return id; | |
2836 | /* Otherwise, it's an ordinary identifier. */ | |
2837 | return cp_parser_identifier (parser); | |
2838 | } | |
2839 | ||
2840 | case CPP_TEMPLATE_ID: | |
2841 | return cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
2842 | check_dependency_p, |
2843 | declarator_p); | |
a723baf1 MM |
2844 | |
2845 | case CPP_COMPL: | |
2846 | { | |
2847 | tree type_decl; | |
2848 | tree qualifying_scope; | |
2849 | tree object_scope; | |
2850 | tree scope; | |
2851 | ||
2852 | /* Consume the `~' token. */ | |
2853 | cp_lexer_consume_token (parser->lexer); | |
2854 | /* Parse the class-name. The standard, as written, seems to | |
2855 | say that: | |
2856 | ||
2857 | template <typename T> struct S { ~S (); }; | |
2858 | template <typename T> S<T>::~S() {} | |
2859 | ||
2860 | is invalid, since `~' must be followed by a class-name, but | |
2861 | `S<T>' is dependent, and so not known to be a class. | |
2862 | That's not right; we need to look in uninstantiated | |
2863 | templates. A further complication arises from: | |
2864 | ||
2865 | template <typename T> void f(T t) { | |
2866 | t.T::~T(); | |
2867 | } | |
2868 | ||
2869 | Here, it is not possible to look up `T' in the scope of `T' | |
2870 | itself. We must look in both the current scope, and the | |
2871 | scope of the containing complete expression. | |
2872 | ||
2873 | Yet another issue is: | |
2874 | ||
2875 | struct S { | |
2876 | int S; | |
2877 | ~S(); | |
2878 | }; | |
2879 | ||
2880 | S::~S() {} | |
2881 | ||
2882 | The standard does not seem to say that the `S' in `~S' | |
2883 | should refer to the type `S' and not the data member | |
2884 | `S::S'. */ | |
2885 | ||
2886 | /* DR 244 says that we look up the name after the "~" in the | |
2887 | same scope as we looked up the qualifying name. That idea | |
2888 | isn't fully worked out; it's more complicated than that. */ | |
2889 | scope = parser->scope; | |
2890 | object_scope = parser->object_scope; | |
2891 | qualifying_scope = parser->qualifying_scope; | |
2892 | ||
2893 | /* If the name is of the form "X::~X" it's OK. */ | |
2894 | if (scope && TYPE_P (scope) | |
2895 | && cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
2896 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
2897 | == CPP_OPEN_PAREN) | |
2898 | && (cp_lexer_peek_token (parser->lexer)->value | |
2899 | == TYPE_IDENTIFIER (scope))) | |
2900 | { | |
2901 | cp_lexer_consume_token (parser->lexer); | |
2902 | return build_nt (BIT_NOT_EXPR, scope); | |
2903 | } | |
2904 | ||
2905 | /* If there was an explicit qualification (S::~T), first look | |
2906 | in the scope given by the qualification (i.e., S). */ | |
2907 | if (scope) | |
2908 | { | |
2909 | cp_parser_parse_tentatively (parser); | |
2910 | type_decl = cp_parser_class_name (parser, | |
2911 | /*typename_keyword_p=*/false, | |
2912 | /*template_keyword_p=*/false, | |
2913 | /*type_p=*/false, | |
a723baf1 | 2914 | /*check_dependency=*/false, |
a668c6ad MM |
2915 | /*class_head_p=*/false, |
2916 | declarator_p); | |
a723baf1 MM |
2917 | if (cp_parser_parse_definitely (parser)) |
2918 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); | |
2919 | } | |
2920 | /* In "N::S::~S", look in "N" as well. */ | |
2921 | if (scope && qualifying_scope) | |
2922 | { | |
2923 | cp_parser_parse_tentatively (parser); | |
2924 | parser->scope = qualifying_scope; | |
2925 | parser->object_scope = NULL_TREE; | |
2926 | parser->qualifying_scope = NULL_TREE; | |
2927 | type_decl | |
2928 | = cp_parser_class_name (parser, | |
2929 | /*typename_keyword_p=*/false, | |
2930 | /*template_keyword_p=*/false, | |
2931 | /*type_p=*/false, | |
a723baf1 | 2932 | /*check_dependency=*/false, |
a668c6ad MM |
2933 | /*class_head_p=*/false, |
2934 | declarator_p); | |
a723baf1 MM |
2935 | if (cp_parser_parse_definitely (parser)) |
2936 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); | |
2937 | } | |
2938 | /* In "p->S::~T", look in the scope given by "*p" as well. */ | |
2939 | else if (object_scope) | |
2940 | { | |
2941 | cp_parser_parse_tentatively (parser); | |
2942 | parser->scope = object_scope; | |
2943 | parser->object_scope = NULL_TREE; | |
2944 | parser->qualifying_scope = NULL_TREE; | |
2945 | type_decl | |
2946 | = cp_parser_class_name (parser, | |
2947 | /*typename_keyword_p=*/false, | |
2948 | /*template_keyword_p=*/false, | |
2949 | /*type_p=*/false, | |
a723baf1 | 2950 | /*check_dependency=*/false, |
a668c6ad MM |
2951 | /*class_head_p=*/false, |
2952 | declarator_p); | |
a723baf1 MM |
2953 | if (cp_parser_parse_definitely (parser)) |
2954 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); | |
2955 | } | |
2956 | /* Look in the surrounding context. */ | |
2957 | parser->scope = NULL_TREE; | |
2958 | parser->object_scope = NULL_TREE; | |
2959 | parser->qualifying_scope = NULL_TREE; | |
2960 | type_decl | |
2961 | = cp_parser_class_name (parser, | |
2962 | /*typename_keyword_p=*/false, | |
2963 | /*template_keyword_p=*/false, | |
2964 | /*type_p=*/false, | |
a723baf1 | 2965 | /*check_dependency=*/false, |
a668c6ad MM |
2966 | /*class_head_p=*/false, |
2967 | declarator_p); | |
a723baf1 MM |
2968 | /* If an error occurred, assume that the name of the |
2969 | destructor is the same as the name of the qualifying | |
2970 | class. That allows us to keep parsing after running | |
2971 | into ill-formed destructor names. */ | |
2972 | if (type_decl == error_mark_node && scope && TYPE_P (scope)) | |
2973 | return build_nt (BIT_NOT_EXPR, scope); | |
2974 | else if (type_decl == error_mark_node) | |
2975 | return error_mark_node; | |
2976 | ||
f3c2dfc6 MM |
2977 | /* [class.dtor] |
2978 | ||
2979 | A typedef-name that names a class shall not be used as the | |
2980 | identifier in the declarator for a destructor declaration. */ | |
2981 | if (declarator_p | |
2982 | && !DECL_IMPLICIT_TYPEDEF_P (type_decl) | |
2983 | && !DECL_SELF_REFERENCE_P (type_decl)) | |
2984 | error ("typedef-name `%D' used as destructor declarator", | |
2985 | type_decl); | |
2986 | ||
a723baf1 MM |
2987 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); |
2988 | } | |
2989 | ||
2990 | case CPP_KEYWORD: | |
2991 | if (token->keyword == RID_OPERATOR) | |
2992 | { | |
2993 | tree id; | |
2994 | ||
2995 | /* This could be a template-id, so we try that first. */ | |
2996 | cp_parser_parse_tentatively (parser); | |
2997 | /* Try a template-id. */ | |
2998 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
2999 | /*check_dependency_p=*/true, |
3000 | declarator_p); | |
a723baf1 MM |
3001 | /* If that worked, we're done. */ |
3002 | if (cp_parser_parse_definitely (parser)) | |
3003 | return id; | |
3004 | /* We still don't know whether we're looking at an | |
3005 | operator-function-id or a conversion-function-id. */ | |
3006 | cp_parser_parse_tentatively (parser); | |
3007 | /* Try an operator-function-id. */ | |
3008 | id = cp_parser_operator_function_id (parser); | |
3009 | /* If that didn't work, try a conversion-function-id. */ | |
3010 | if (!cp_parser_parse_definitely (parser)) | |
3011 | id = cp_parser_conversion_function_id (parser); | |
3012 | ||
3013 | return id; | |
3014 | } | |
3015 | /* Fall through. */ | |
3016 | ||
3017 | default: | |
3018 | cp_parser_error (parser, "expected unqualified-id"); | |
3019 | return error_mark_node; | |
3020 | } | |
3021 | } | |
3022 | ||
3023 | /* Parse an (optional) nested-name-specifier. | |
3024 | ||
3025 | nested-name-specifier: | |
3026 | class-or-namespace-name :: nested-name-specifier [opt] | |
3027 | class-or-namespace-name :: template nested-name-specifier [opt] | |
3028 | ||
3029 | PARSER->SCOPE should be set appropriately before this function is | |
3030 | called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in | |
3031 | effect. TYPE_P is TRUE if we non-type bindings should be ignored | |
3032 | in name lookups. | |
3033 | ||
3034 | Sets PARSER->SCOPE to the class (TYPE) or namespace | |
3035 | (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves | |
3036 | it unchanged if there is no nested-name-specifier. Returns the new | |
a668c6ad MM |
3037 | scope iff there is a nested-name-specifier, or NULL_TREE otherwise. |
3038 | ||
3039 | If IS_DECLARATION is TRUE, the nested-name-specifier is known to be | |
3040 | part of a declaration and/or decl-specifier. */ | |
a723baf1 MM |
3041 | |
3042 | static tree | |
3043 | cp_parser_nested_name_specifier_opt (cp_parser *parser, | |
3044 | bool typename_keyword_p, | |
3045 | bool check_dependency_p, | |
a668c6ad MM |
3046 | bool type_p, |
3047 | bool is_declaration) | |
a723baf1 MM |
3048 | { |
3049 | bool success = false; | |
3050 | tree access_check = NULL_TREE; | |
3051 | ptrdiff_t start; | |
2050a1bb | 3052 | cp_token* token; |
a723baf1 MM |
3053 | |
3054 | /* If the next token corresponds to a nested name specifier, there | |
2050a1bb MM |
3055 | is no need to reparse it. However, if CHECK_DEPENDENCY_P is |
3056 | false, it may have been true before, in which case something | |
3057 | like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier | |
3058 | of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when | |
3059 | CHECK_DEPENDENCY_P is false, we have to fall through into the | |
3060 | main loop. */ | |
3061 | if (check_dependency_p | |
3062 | && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER)) | |
3063 | { | |
3064 | cp_parser_pre_parsed_nested_name_specifier (parser); | |
a723baf1 MM |
3065 | return parser->scope; |
3066 | } | |
3067 | ||
3068 | /* Remember where the nested-name-specifier starts. */ | |
3069 | if (cp_parser_parsing_tentatively (parser) | |
3070 | && !cp_parser_committed_to_tentative_parse (parser)) | |
3071 | { | |
2050a1bb | 3072 | token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
3073 | start = cp_lexer_token_difference (parser->lexer, |
3074 | parser->lexer->first_token, | |
2050a1bb | 3075 | token); |
a723baf1 MM |
3076 | } |
3077 | else | |
3078 | start = -1; | |
3079 | ||
8d241e0b | 3080 | push_deferring_access_checks (dk_deferred); |
cf22909c | 3081 | |
a723baf1 MM |
3082 | while (true) |
3083 | { | |
3084 | tree new_scope; | |
3085 | tree old_scope; | |
3086 | tree saved_qualifying_scope; | |
a723baf1 MM |
3087 | bool template_keyword_p; |
3088 | ||
2050a1bb MM |
3089 | /* Spot cases that cannot be the beginning of a |
3090 | nested-name-specifier. */ | |
3091 | token = cp_lexer_peek_token (parser->lexer); | |
3092 | ||
3093 | /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process | |
3094 | the already parsed nested-name-specifier. */ | |
3095 | if (token->type == CPP_NESTED_NAME_SPECIFIER) | |
3096 | { | |
3097 | /* Grab the nested-name-specifier and continue the loop. */ | |
3098 | cp_parser_pre_parsed_nested_name_specifier (parser); | |
3099 | success = true; | |
3100 | continue; | |
3101 | } | |
3102 | ||
a723baf1 MM |
3103 | /* Spot cases that cannot be the beginning of a |
3104 | nested-name-specifier. On the second and subsequent times | |
3105 | through the loop, we look for the `template' keyword. */ | |
f7b5ecd9 | 3106 | if (success && token->keyword == RID_TEMPLATE) |
a723baf1 MM |
3107 | ; |
3108 | /* A template-id can start a nested-name-specifier. */ | |
f7b5ecd9 | 3109 | else if (token->type == CPP_TEMPLATE_ID) |
a723baf1 MM |
3110 | ; |
3111 | else | |
3112 | { | |
3113 | /* If the next token is not an identifier, then it is | |
3114 | definitely not a class-or-namespace-name. */ | |
f7b5ecd9 | 3115 | if (token->type != CPP_NAME) |
a723baf1 MM |
3116 | break; |
3117 | /* If the following token is neither a `<' (to begin a | |
3118 | template-id), nor a `::', then we are not looking at a | |
3119 | nested-name-specifier. */ | |
3120 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
f4abade9 GB |
3121 | if (token->type != CPP_SCOPE |
3122 | && !cp_parser_nth_token_starts_template_argument_list_p | |
3123 | (parser, 2)) | |
a723baf1 MM |
3124 | break; |
3125 | } | |
3126 | ||
3127 | /* The nested-name-specifier is optional, so we parse | |
3128 | tentatively. */ | |
3129 | cp_parser_parse_tentatively (parser); | |
3130 | ||
3131 | /* Look for the optional `template' keyword, if this isn't the | |
3132 | first time through the loop. */ | |
3133 | if (success) | |
3134 | template_keyword_p = cp_parser_optional_template_keyword (parser); | |
3135 | else | |
3136 | template_keyword_p = false; | |
3137 | ||
3138 | /* Save the old scope since the name lookup we are about to do | |
3139 | might destroy it. */ | |
3140 | old_scope = parser->scope; | |
3141 | saved_qualifying_scope = parser->qualifying_scope; | |
3142 | /* Parse the qualifying entity. */ | |
3143 | new_scope | |
3144 | = cp_parser_class_or_namespace_name (parser, | |
3145 | typename_keyword_p, | |
3146 | template_keyword_p, | |
3147 | check_dependency_p, | |
a668c6ad MM |
3148 | type_p, |
3149 | is_declaration); | |
a723baf1 MM |
3150 | /* Look for the `::' token. */ |
3151 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
3152 | ||
3153 | /* If we found what we wanted, we keep going; otherwise, we're | |
3154 | done. */ | |
3155 | if (!cp_parser_parse_definitely (parser)) | |
3156 | { | |
3157 | bool error_p = false; | |
3158 | ||
3159 | /* Restore the OLD_SCOPE since it was valid before the | |
3160 | failed attempt at finding the last | |
3161 | class-or-namespace-name. */ | |
3162 | parser->scope = old_scope; | |
3163 | parser->qualifying_scope = saved_qualifying_scope; | |
3164 | /* If the next token is an identifier, and the one after | |
3165 | that is a `::', then any valid interpretation would have | |
3166 | found a class-or-namespace-name. */ | |
3167 | while (cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
3168 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
3169 | == CPP_SCOPE) | |
3170 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type | |
3171 | != CPP_COMPL)) | |
3172 | { | |
3173 | token = cp_lexer_consume_token (parser->lexer); | |
3174 | if (!error_p) | |
3175 | { | |
3176 | tree decl; | |
3177 | ||
3178 | decl = cp_parser_lookup_name_simple (parser, token->value); | |
3179 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
3180 | error ("`%D' used without template parameters", | |
3181 | decl); | |
a723baf1 | 3182 | else |
4bb8ca28 MM |
3183 | cp_parser_name_lookup_error |
3184 | (parser, token->value, decl, | |
3185 | "is not a class or namespace"); | |
a723baf1 MM |
3186 | parser->scope = NULL_TREE; |
3187 | error_p = true; | |
eea9800f MM |
3188 | /* Treat this as a successful nested-name-specifier |
3189 | due to: | |
3190 | ||
3191 | [basic.lookup.qual] | |
3192 | ||
3193 | If the name found is not a class-name (clause | |
3194 | _class_) or namespace-name (_namespace.def_), the | |
3195 | program is ill-formed. */ | |
3196 | success = true; | |
a723baf1 MM |
3197 | } |
3198 | cp_lexer_consume_token (parser->lexer); | |
3199 | } | |
3200 | break; | |
3201 | } | |
3202 | ||
3203 | /* We've found one valid nested-name-specifier. */ | |
3204 | success = true; | |
3205 | /* Make sure we look in the right scope the next time through | |
3206 | the loop. */ | |
3207 | parser->scope = (TREE_CODE (new_scope) == TYPE_DECL | |
3208 | ? TREE_TYPE (new_scope) | |
3209 | : new_scope); | |
3210 | /* If it is a class scope, try to complete it; we are about to | |
3211 | be looking up names inside the class. */ | |
8fbc5ae7 MM |
3212 | if (TYPE_P (parser->scope) |
3213 | /* Since checking types for dependency can be expensive, | |
3214 | avoid doing it if the type is already complete. */ | |
3215 | && !COMPLETE_TYPE_P (parser->scope) | |
3216 | /* Do not try to complete dependent types. */ | |
1fb3244a | 3217 | && !dependent_type_p (parser->scope)) |
a723baf1 MM |
3218 | complete_type (parser->scope); |
3219 | } | |
3220 | ||
cf22909c KL |
3221 | /* Retrieve any deferred checks. Do not pop this access checks yet |
3222 | so the memory will not be reclaimed during token replacing below. */ | |
3223 | access_check = get_deferred_access_checks (); | |
3224 | ||
a723baf1 MM |
3225 | /* If parsing tentatively, replace the sequence of tokens that makes |
3226 | up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER | |
3227 | token. That way, should we re-parse the token stream, we will | |
3228 | not have to repeat the effort required to do the parse, nor will | |
3229 | we issue duplicate error messages. */ | |
3230 | if (success && start >= 0) | |
3231 | { | |
a723baf1 MM |
3232 | /* Find the token that corresponds to the start of the |
3233 | template-id. */ | |
3234 | token = cp_lexer_advance_token (parser->lexer, | |
3235 | parser->lexer->first_token, | |
3236 | start); | |
3237 | ||
a723baf1 MM |
3238 | /* Reset the contents of the START token. */ |
3239 | token->type = CPP_NESTED_NAME_SPECIFIER; | |
3240 | token->value = build_tree_list (access_check, parser->scope); | |
3241 | TREE_TYPE (token->value) = parser->qualifying_scope; | |
3242 | token->keyword = RID_MAX; | |
3243 | /* Purge all subsequent tokens. */ | |
3244 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
3245 | } | |
3246 | ||
cf22909c | 3247 | pop_deferring_access_checks (); |
a723baf1 MM |
3248 | return success ? parser->scope : NULL_TREE; |
3249 | } | |
3250 | ||
3251 | /* Parse a nested-name-specifier. See | |
3252 | cp_parser_nested_name_specifier_opt for details. This function | |
3253 | behaves identically, except that it will an issue an error if no | |
3254 | nested-name-specifier is present, and it will return | |
3255 | ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier | |
3256 | is present. */ | |
3257 | ||
3258 | static tree | |
3259 | cp_parser_nested_name_specifier (cp_parser *parser, | |
3260 | bool typename_keyword_p, | |
3261 | bool check_dependency_p, | |
a668c6ad MM |
3262 | bool type_p, |
3263 | bool is_declaration) | |
a723baf1 MM |
3264 | { |
3265 | tree scope; | |
3266 | ||
3267 | /* Look for the nested-name-specifier. */ | |
3268 | scope = cp_parser_nested_name_specifier_opt (parser, | |
3269 | typename_keyword_p, | |
3270 | check_dependency_p, | |
a668c6ad MM |
3271 | type_p, |
3272 | is_declaration); | |
a723baf1 MM |
3273 | /* If it was not present, issue an error message. */ |
3274 | if (!scope) | |
3275 | { | |
3276 | cp_parser_error (parser, "expected nested-name-specifier"); | |
eb5abb39 | 3277 | parser->scope = NULL_TREE; |
a723baf1 MM |
3278 | return error_mark_node; |
3279 | } | |
3280 | ||
3281 | return scope; | |
3282 | } | |
3283 | ||
3284 | /* Parse a class-or-namespace-name. | |
3285 | ||
3286 | class-or-namespace-name: | |
3287 | class-name | |
3288 | namespace-name | |
3289 | ||
3290 | TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect. | |
3291 | TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect. | |
3292 | CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up. | |
3293 | TYPE_P is TRUE iff the next name should be taken as a class-name, | |
3294 | even the same name is declared to be another entity in the same | |
3295 | scope. | |
3296 | ||
3297 | Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL) | |
eea9800f MM |
3298 | specified by the class-or-namespace-name. If neither is found the |
3299 | ERROR_MARK_NODE is returned. */ | |
a723baf1 MM |
3300 | |
3301 | static tree | |
3302 | cp_parser_class_or_namespace_name (cp_parser *parser, | |
3303 | bool typename_keyword_p, | |
3304 | bool template_keyword_p, | |
3305 | bool check_dependency_p, | |
a668c6ad MM |
3306 | bool type_p, |
3307 | bool is_declaration) | |
a723baf1 MM |
3308 | { |
3309 | tree saved_scope; | |
3310 | tree saved_qualifying_scope; | |
3311 | tree saved_object_scope; | |
3312 | tree scope; | |
eea9800f | 3313 | bool only_class_p; |
a723baf1 | 3314 | |
a723baf1 MM |
3315 | /* Before we try to parse the class-name, we must save away the |
3316 | current PARSER->SCOPE since cp_parser_class_name will destroy | |
3317 | it. */ | |
3318 | saved_scope = parser->scope; | |
3319 | saved_qualifying_scope = parser->qualifying_scope; | |
3320 | saved_object_scope = parser->object_scope; | |
eea9800f MM |
3321 | /* Try for a class-name first. If the SAVED_SCOPE is a type, then |
3322 | there is no need to look for a namespace-name. */ | |
bbaab916 | 3323 | only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope)); |
eea9800f MM |
3324 | if (!only_class_p) |
3325 | cp_parser_parse_tentatively (parser); | |
a723baf1 MM |
3326 | scope = cp_parser_class_name (parser, |
3327 | typename_keyword_p, | |
3328 | template_keyword_p, | |
3329 | type_p, | |
a723baf1 | 3330 | check_dependency_p, |
a668c6ad MM |
3331 | /*class_head_p=*/false, |
3332 | is_declaration); | |
a723baf1 | 3333 | /* If that didn't work, try for a namespace-name. */ |
eea9800f | 3334 | if (!only_class_p && !cp_parser_parse_definitely (parser)) |
a723baf1 MM |
3335 | { |
3336 | /* Restore the saved scope. */ | |
3337 | parser->scope = saved_scope; | |
3338 | parser->qualifying_scope = saved_qualifying_scope; | |
3339 | parser->object_scope = saved_object_scope; | |
eea9800f MM |
3340 | /* If we are not looking at an identifier followed by the scope |
3341 | resolution operator, then this is not part of a | |
3342 | nested-name-specifier. (Note that this function is only used | |
3343 | to parse the components of a nested-name-specifier.) */ | |
3344 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME) | |
3345 | || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE) | |
3346 | return error_mark_node; | |
a723baf1 MM |
3347 | scope = cp_parser_namespace_name (parser); |
3348 | } | |
3349 | ||
3350 | return scope; | |
3351 | } | |
3352 | ||
3353 | /* Parse a postfix-expression. | |
3354 | ||
3355 | postfix-expression: | |
3356 | primary-expression | |
3357 | postfix-expression [ expression ] | |
3358 | postfix-expression ( expression-list [opt] ) | |
3359 | simple-type-specifier ( expression-list [opt] ) | |
3360 | typename :: [opt] nested-name-specifier identifier | |
3361 | ( expression-list [opt] ) | |
3362 | typename :: [opt] nested-name-specifier template [opt] template-id | |
3363 | ( expression-list [opt] ) | |
3364 | postfix-expression . template [opt] id-expression | |
3365 | postfix-expression -> template [opt] id-expression | |
3366 | postfix-expression . pseudo-destructor-name | |
3367 | postfix-expression -> pseudo-destructor-name | |
3368 | postfix-expression ++ | |
3369 | postfix-expression -- | |
3370 | dynamic_cast < type-id > ( expression ) | |
3371 | static_cast < type-id > ( expression ) | |
3372 | reinterpret_cast < type-id > ( expression ) | |
3373 | const_cast < type-id > ( expression ) | |
3374 | typeid ( expression ) | |
3375 | typeid ( type-id ) | |
3376 | ||
3377 | GNU Extension: | |
3378 | ||
3379 | postfix-expression: | |
3380 | ( type-id ) { initializer-list , [opt] } | |
3381 | ||
3382 | This extension is a GNU version of the C99 compound-literal | |
3383 | construct. (The C99 grammar uses `type-name' instead of `type-id', | |
3384 | but they are essentially the same concept.) | |
3385 | ||
3386 | If ADDRESS_P is true, the postfix expression is the operand of the | |
3387 | `&' operator. | |
3388 | ||
3389 | Returns a representation of the expression. */ | |
3390 | ||
3391 | static tree | |
3392 | cp_parser_postfix_expression (cp_parser *parser, bool address_p) | |
3393 | { | |
3394 | cp_token *token; | |
3395 | enum rid keyword; | |
b3445994 | 3396 | cp_id_kind idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3397 | tree postfix_expression = NULL_TREE; |
3398 | /* Non-NULL only if the current postfix-expression can be used to | |
3399 | form a pointer-to-member. In that case, QUALIFYING_CLASS is the | |
3400 | class used to qualify the member. */ | |
3401 | tree qualifying_class = NULL_TREE; | |
a723baf1 MM |
3402 | |
3403 | /* Peek at the next token. */ | |
3404 | token = cp_lexer_peek_token (parser->lexer); | |
3405 | /* Some of the productions are determined by keywords. */ | |
3406 | keyword = token->keyword; | |
3407 | switch (keyword) | |
3408 | { | |
3409 | case RID_DYNCAST: | |
3410 | case RID_STATCAST: | |
3411 | case RID_REINTCAST: | |
3412 | case RID_CONSTCAST: | |
3413 | { | |
3414 | tree type; | |
3415 | tree expression; | |
3416 | const char *saved_message; | |
3417 | ||
3418 | /* All of these can be handled in the same way from the point | |
3419 | of view of parsing. Begin by consuming the token | |
3420 | identifying the cast. */ | |
3421 | cp_lexer_consume_token (parser->lexer); | |
3422 | ||
3423 | /* New types cannot be defined in the cast. */ | |
3424 | saved_message = parser->type_definition_forbidden_message; | |
3425 | parser->type_definition_forbidden_message | |
3426 | = "types may not be defined in casts"; | |
3427 | ||
3428 | /* Look for the opening `<'. */ | |
3429 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
3430 | /* Parse the type to which we are casting. */ | |
3431 | type = cp_parser_type_id (parser); | |
3432 | /* Look for the closing `>'. */ | |
3433 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
3434 | /* Restore the old message. */ | |
3435 | parser->type_definition_forbidden_message = saved_message; | |
3436 | ||
3437 | /* And the expression which is being cast. */ | |
3438 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
3439 | expression = cp_parser_expression (parser); | |
3440 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3441 | ||
14d22dd6 MM |
3442 | /* Only type conversions to integral or enumeration types |
3443 | can be used in constant-expressions. */ | |
67c03833 | 3444 | if (parser->integral_constant_expression_p |
14d22dd6 | 3445 | && !dependent_type_p (type) |
263ee052 MM |
3446 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
3447 | /* A cast to pointer or reference type is allowed in the | |
3448 | implementation of "offsetof". */ | |
3449 | && !(parser->in_offsetof_p && POINTER_TYPE_P (type))) | |
14d22dd6 | 3450 | { |
67c03833 JM |
3451 | if (!parser->allow_non_integral_constant_expression_p) |
3452 | return (cp_parser_non_integral_constant_expression | |
14d22dd6 MM |
3453 | ("a cast to a type other than an integral or " |
3454 | "enumeration type")); | |
67c03833 | 3455 | parser->non_integral_constant_expression_p = true; |
14d22dd6 MM |
3456 | } |
3457 | ||
a723baf1 MM |
3458 | switch (keyword) |
3459 | { | |
3460 | case RID_DYNCAST: | |
3461 | postfix_expression | |
3462 | = build_dynamic_cast (type, expression); | |
3463 | break; | |
3464 | case RID_STATCAST: | |
3465 | postfix_expression | |
3466 | = build_static_cast (type, expression); | |
3467 | break; | |
3468 | case RID_REINTCAST: | |
3469 | postfix_expression | |
3470 | = build_reinterpret_cast (type, expression); | |
3471 | break; | |
3472 | case RID_CONSTCAST: | |
3473 | postfix_expression | |
3474 | = build_const_cast (type, expression); | |
3475 | break; | |
3476 | default: | |
3477 | abort (); | |
3478 | } | |
3479 | } | |
3480 | break; | |
3481 | ||
3482 | case RID_TYPEID: | |
3483 | { | |
3484 | tree type; | |
3485 | const char *saved_message; | |
4f8163b1 | 3486 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
3487 | |
3488 | /* Consume the `typeid' token. */ | |
3489 | cp_lexer_consume_token (parser->lexer); | |
3490 | /* Look for the `(' token. */ | |
3491 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
3492 | /* Types cannot be defined in a `typeid' expression. */ | |
3493 | saved_message = parser->type_definition_forbidden_message; | |
3494 | parser->type_definition_forbidden_message | |
3495 | = "types may not be defined in a `typeid\' expression"; | |
3496 | /* We can't be sure yet whether we're looking at a type-id or an | |
3497 | expression. */ | |
3498 | cp_parser_parse_tentatively (parser); | |
3499 | /* Try a type-id first. */ | |
4f8163b1 MM |
3500 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
3501 | parser->in_type_id_in_expr_p = true; | |
a723baf1 | 3502 | type = cp_parser_type_id (parser); |
4f8163b1 | 3503 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 MM |
3504 | /* Look for the `)' token. Otherwise, we can't be sure that |
3505 | we're not looking at an expression: consider `typeid (int | |
3506 | (3))', for example. */ | |
3507 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3508 | /* If all went well, simply lookup the type-id. */ | |
3509 | if (cp_parser_parse_definitely (parser)) | |
3510 | postfix_expression = get_typeid (type); | |
3511 | /* Otherwise, fall back to the expression variant. */ | |
3512 | else | |
3513 | { | |
3514 | tree expression; | |
3515 | ||
3516 | /* Look for an expression. */ | |
3517 | expression = cp_parser_expression (parser); | |
3518 | /* Compute its typeid. */ | |
3519 | postfix_expression = build_typeid (expression); | |
3520 | /* Look for the `)' token. */ | |
3521 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3522 | } | |
3523 | ||
3524 | /* Restore the saved message. */ | |
3525 | parser->type_definition_forbidden_message = saved_message; | |
3526 | } | |
3527 | break; | |
3528 | ||
3529 | case RID_TYPENAME: | |
3530 | { | |
3531 | bool template_p = false; | |
3532 | tree id; | |
3533 | tree type; | |
3534 | ||
3535 | /* Consume the `typename' token. */ | |
3536 | cp_lexer_consume_token (parser->lexer); | |
3537 | /* Look for the optional `::' operator. */ | |
3538 | cp_parser_global_scope_opt (parser, | |
3539 | /*current_scope_valid_p=*/false); | |
3540 | /* Look for the nested-name-specifier. */ | |
3541 | cp_parser_nested_name_specifier (parser, | |
3542 | /*typename_keyword_p=*/true, | |
3543 | /*check_dependency_p=*/true, | |
a668c6ad MM |
3544 | /*type_p=*/true, |
3545 | /*is_declaration=*/true); | |
a723baf1 MM |
3546 | /* Look for the optional `template' keyword. */ |
3547 | template_p = cp_parser_optional_template_keyword (parser); | |
3548 | /* We don't know whether we're looking at a template-id or an | |
3549 | identifier. */ | |
3550 | cp_parser_parse_tentatively (parser); | |
3551 | /* Try a template-id. */ | |
3552 | id = cp_parser_template_id (parser, template_p, | |
a668c6ad MM |
3553 | /*check_dependency_p=*/true, |
3554 | /*is_declaration=*/true); | |
a723baf1 MM |
3555 | /* If that didn't work, try an identifier. */ |
3556 | if (!cp_parser_parse_definitely (parser)) | |
3557 | id = cp_parser_identifier (parser); | |
3558 | /* Create a TYPENAME_TYPE to represent the type to which the | |
3559 | functional cast is being performed. */ | |
3560 | type = make_typename_type (parser->scope, id, | |
3561 | /*complain=*/1); | |
3562 | ||
3563 | postfix_expression = cp_parser_functional_cast (parser, type); | |
3564 | } | |
3565 | break; | |
3566 | ||
3567 | default: | |
3568 | { | |
3569 | tree type; | |
3570 | ||
3571 | /* If the next thing is a simple-type-specifier, we may be | |
3572 | looking at a functional cast. We could also be looking at | |
3573 | an id-expression. So, we try the functional cast, and if | |
3574 | that doesn't work we fall back to the primary-expression. */ | |
3575 | cp_parser_parse_tentatively (parser); | |
3576 | /* Look for the simple-type-specifier. */ | |
3577 | type = cp_parser_simple_type_specifier (parser, | |
4b0d3cbe MM |
3578 | CP_PARSER_FLAGS_NONE, |
3579 | /*identifier_p=*/false); | |
a723baf1 MM |
3580 | /* Parse the cast itself. */ |
3581 | if (!cp_parser_error_occurred (parser)) | |
3582 | postfix_expression | |
3583 | = cp_parser_functional_cast (parser, type); | |
3584 | /* If that worked, we're done. */ | |
3585 | if (cp_parser_parse_definitely (parser)) | |
3586 | break; | |
3587 | ||
3588 | /* If the functional-cast didn't work out, try a | |
3589 | compound-literal. */ | |
14d22dd6 MM |
3590 | if (cp_parser_allow_gnu_extensions_p (parser) |
3591 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
a723baf1 MM |
3592 | { |
3593 | tree initializer_list = NULL_TREE; | |
4f8163b1 | 3594 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
3595 | |
3596 | cp_parser_parse_tentatively (parser); | |
14d22dd6 MM |
3597 | /* Consume the `('. */ |
3598 | cp_lexer_consume_token (parser->lexer); | |
3599 | /* Parse the type. */ | |
4f8163b1 MM |
3600 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
3601 | parser->in_type_id_in_expr_p = true; | |
14d22dd6 | 3602 | type = cp_parser_type_id (parser); |
4f8163b1 | 3603 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
14d22dd6 MM |
3604 | /* Look for the `)'. */ |
3605 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3606 | /* Look for the `{'. */ | |
3607 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
3608 | /* If things aren't going well, there's no need to | |
3609 | keep going. */ | |
3610 | if (!cp_parser_error_occurred (parser)) | |
a723baf1 | 3611 | { |
39703eb9 | 3612 | bool non_constant_p; |
14d22dd6 MM |
3613 | /* Parse the initializer-list. */ |
3614 | initializer_list | |
39703eb9 | 3615 | = cp_parser_initializer_list (parser, &non_constant_p); |
14d22dd6 MM |
3616 | /* Allow a trailing `,'. */ |
3617 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
3618 | cp_lexer_consume_token (parser->lexer); | |
3619 | /* Look for the final `}'. */ | |
3620 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
a723baf1 MM |
3621 | } |
3622 | /* If that worked, we're definitely looking at a | |
3623 | compound-literal expression. */ | |
3624 | if (cp_parser_parse_definitely (parser)) | |
3625 | { | |
3626 | /* Warn the user that a compound literal is not | |
3627 | allowed in standard C++. */ | |
3628 | if (pedantic) | |
3629 | pedwarn ("ISO C++ forbids compound-literals"); | |
3630 | /* Form the representation of the compound-literal. */ | |
3631 | postfix_expression | |
3632 | = finish_compound_literal (type, initializer_list); | |
3633 | break; | |
3634 | } | |
3635 | } | |
3636 | ||
3637 | /* It must be a primary-expression. */ | |
3638 | postfix_expression = cp_parser_primary_expression (parser, | |
3639 | &idk, | |
3640 | &qualifying_class); | |
3641 | } | |
3642 | break; | |
3643 | } | |
3644 | ||
ee76b931 MM |
3645 | /* If we were avoiding committing to the processing of a |
3646 | qualified-id until we knew whether or not we had a | |
3647 | pointer-to-member, we now know. */ | |
089d6ea7 | 3648 | if (qualifying_class) |
a723baf1 | 3649 | { |
ee76b931 | 3650 | bool done; |
a723baf1 | 3651 | |
ee76b931 MM |
3652 | /* Peek at the next token. */ |
3653 | token = cp_lexer_peek_token (parser->lexer); | |
3654 | done = (token->type != CPP_OPEN_SQUARE | |
3655 | && token->type != CPP_OPEN_PAREN | |
3656 | && token->type != CPP_DOT | |
3657 | && token->type != CPP_DEREF | |
3658 | && token->type != CPP_PLUS_PLUS | |
3659 | && token->type != CPP_MINUS_MINUS); | |
3660 | ||
3661 | postfix_expression = finish_qualified_id_expr (qualifying_class, | |
3662 | postfix_expression, | |
3663 | done, | |
3664 | address_p); | |
3665 | if (done) | |
3666 | return postfix_expression; | |
a723baf1 MM |
3667 | } |
3668 | ||
a723baf1 MM |
3669 | /* Keep looping until the postfix-expression is complete. */ |
3670 | while (true) | |
3671 | { | |
10b1d5e7 MM |
3672 | if (idk == CP_ID_KIND_UNQUALIFIED |
3673 | && TREE_CODE (postfix_expression) == IDENTIFIER_NODE | |
a723baf1 | 3674 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) |
b3445994 MM |
3675 | /* It is not a Koenig lookup function call. */ |
3676 | postfix_expression | |
3677 | = unqualified_name_lookup_error (postfix_expression); | |
a723baf1 MM |
3678 | |
3679 | /* Peek at the next token. */ | |
3680 | token = cp_lexer_peek_token (parser->lexer); | |
3681 | ||
3682 | switch (token->type) | |
3683 | { | |
3684 | case CPP_OPEN_SQUARE: | |
3685 | /* postfix-expression [ expression ] */ | |
3686 | { | |
3687 | tree index; | |
3688 | ||
3689 | /* Consume the `[' token. */ | |
3690 | cp_lexer_consume_token (parser->lexer); | |
3691 | /* Parse the index expression. */ | |
3692 | index = cp_parser_expression (parser); | |
3693 | /* Look for the closing `]'. */ | |
3694 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
3695 | ||
3696 | /* Build the ARRAY_REF. */ | |
3697 | postfix_expression | |
3698 | = grok_array_decl (postfix_expression, index); | |
b3445994 | 3699 | idk = CP_ID_KIND_NONE; |
a5ac3982 MM |
3700 | /* Array references are not permitted in |
3701 | constant-expressions. */ | |
67c03833 | 3702 | if (parser->integral_constant_expression_p) |
a5ac3982 | 3703 | { |
67c03833 | 3704 | if (!parser->allow_non_integral_constant_expression_p) |
a5ac3982 | 3705 | postfix_expression |
67c03833 JM |
3706 | = cp_parser_non_integral_constant_expression ("an array reference"); |
3707 | parser->non_integral_constant_expression_p = true; | |
a5ac3982 | 3708 | } |
a723baf1 MM |
3709 | } |
3710 | break; | |
3711 | ||
3712 | case CPP_OPEN_PAREN: | |
3713 | /* postfix-expression ( expression-list [opt] ) */ | |
3714 | { | |
6d80c4b9 | 3715 | bool koenig_p; |
39703eb9 MM |
3716 | tree args = (cp_parser_parenthesized_expression_list |
3717 | (parser, false, /*non_constant_p=*/NULL)); | |
a723baf1 | 3718 | |
7efa3e22 NS |
3719 | if (args == error_mark_node) |
3720 | { | |
3721 | postfix_expression = error_mark_node; | |
3722 | break; | |
3723 | } | |
3724 | ||
14d22dd6 MM |
3725 | /* Function calls are not permitted in |
3726 | constant-expressions. */ | |
67c03833 | 3727 | if (parser->integral_constant_expression_p) |
14d22dd6 | 3728 | { |
67c03833 | 3729 | if (!parser->allow_non_integral_constant_expression_p) |
a5ac3982 MM |
3730 | { |
3731 | postfix_expression | |
67c03833 | 3732 | = cp_parser_non_integral_constant_expression ("a function call"); |
a5ac3982 MM |
3733 | break; |
3734 | } | |
67c03833 | 3735 | parser->non_integral_constant_expression_p = true; |
14d22dd6 | 3736 | } |
a723baf1 | 3737 | |
6d80c4b9 | 3738 | koenig_p = false; |
399dedb9 NS |
3739 | if (idk == CP_ID_KIND_UNQUALIFIED) |
3740 | { | |
3741 | if (args | |
3742 | && (is_overloaded_fn (postfix_expression) | |
3743 | || DECL_P (postfix_expression) | |
3744 | || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)) | |
6d80c4b9 MM |
3745 | { |
3746 | koenig_p = true; | |
3747 | postfix_expression | |
3748 | = perform_koenig_lookup (postfix_expression, args); | |
3749 | } | |
399dedb9 NS |
3750 | else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE) |
3751 | postfix_expression | |
3752 | = unqualified_fn_lookup_error (postfix_expression); | |
3753 | } | |
3754 | ||
d17811fd | 3755 | if (TREE_CODE (postfix_expression) == COMPONENT_REF) |
a723baf1 | 3756 | { |
d17811fd MM |
3757 | tree instance = TREE_OPERAND (postfix_expression, 0); |
3758 | tree fn = TREE_OPERAND (postfix_expression, 1); | |
3759 | ||
3760 | if (processing_template_decl | |
3761 | && (type_dependent_expression_p (instance) | |
3762 | || (!BASELINK_P (fn) | |
3763 | && TREE_CODE (fn) != FIELD_DECL) | |
584672ee | 3764 | || type_dependent_expression_p (fn) |
d17811fd MM |
3765 | || any_type_dependent_arguments_p (args))) |
3766 | { | |
3767 | postfix_expression | |
3768 | = build_min_nt (CALL_EXPR, postfix_expression, args); | |
3769 | break; | |
3770 | } | |
9f880ef9 MM |
3771 | |
3772 | if (BASELINK_P (fn)) | |
3773 | postfix_expression | |
3774 | = (build_new_method_call | |
3775 | (instance, fn, args, NULL_TREE, | |
3776 | (idk == CP_ID_KIND_QUALIFIED | |
3777 | ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL))); | |
3778 | else | |
3779 | postfix_expression | |
3780 | = finish_call_expr (postfix_expression, args, | |
3781 | /*disallow_virtual=*/false, | |
3782 | /*koenig_p=*/false); | |
a723baf1 | 3783 | } |
d17811fd MM |
3784 | else if (TREE_CODE (postfix_expression) == OFFSET_REF |
3785 | || TREE_CODE (postfix_expression) == MEMBER_REF | |
3786 | || TREE_CODE (postfix_expression) == DOTSTAR_EXPR) | |
a723baf1 MM |
3787 | postfix_expression = (build_offset_ref_call_from_tree |
3788 | (postfix_expression, args)); | |
b3445994 | 3789 | else if (idk == CP_ID_KIND_QUALIFIED) |
2050a1bb MM |
3790 | /* A call to a static class member, or a namespace-scope |
3791 | function. */ | |
3792 | postfix_expression | |
3793 | = finish_call_expr (postfix_expression, args, | |
6d80c4b9 MM |
3794 | /*disallow_virtual=*/true, |
3795 | koenig_p); | |
a723baf1 | 3796 | else |
2050a1bb MM |
3797 | /* All other function calls. */ |
3798 | postfix_expression | |
3799 | = finish_call_expr (postfix_expression, args, | |
6d80c4b9 MM |
3800 | /*disallow_virtual=*/false, |
3801 | koenig_p); | |
a723baf1 MM |
3802 | |
3803 | /* The POSTFIX_EXPRESSION is certainly no longer an id. */ | |
b3445994 | 3804 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3805 | } |
3806 | break; | |
3807 | ||
3808 | case CPP_DOT: | |
3809 | case CPP_DEREF: | |
3810 | /* postfix-expression . template [opt] id-expression | |
3811 | postfix-expression . pseudo-destructor-name | |
3812 | postfix-expression -> template [opt] id-expression | |
3813 | postfix-expression -> pseudo-destructor-name */ | |
3814 | { | |
3815 | tree name; | |
3816 | bool dependent_p; | |
3817 | bool template_p; | |
3818 | tree scope = NULL_TREE; | |
a5ac3982 | 3819 | enum cpp_ttype token_type = token->type; |
a723baf1 MM |
3820 | |
3821 | /* If this is a `->' operator, dereference the pointer. */ | |
3822 | if (token->type == CPP_DEREF) | |
3823 | postfix_expression = build_x_arrow (postfix_expression); | |
3824 | /* Check to see whether or not the expression is | |
3825 | type-dependent. */ | |
bbaab916 | 3826 | dependent_p = type_dependent_expression_p (postfix_expression); |
a723baf1 MM |
3827 | /* The identifier following the `->' or `.' is not |
3828 | qualified. */ | |
3829 | parser->scope = NULL_TREE; | |
3830 | parser->qualifying_scope = NULL_TREE; | |
3831 | parser->object_scope = NULL_TREE; | |
b3445994 | 3832 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3833 | /* Enter the scope corresponding to the type of the object |
3834 | given by the POSTFIX_EXPRESSION. */ | |
3835 | if (!dependent_p | |
3836 | && TREE_TYPE (postfix_expression) != NULL_TREE) | |
3837 | { | |
3838 | scope = TREE_TYPE (postfix_expression); | |
3839 | /* According to the standard, no expression should | |
3840 | ever have reference type. Unfortunately, we do not | |
3841 | currently match the standard in this respect in | |
3842 | that our internal representation of an expression | |
3843 | may have reference type even when the standard says | |
3844 | it does not. Therefore, we have to manually obtain | |
3845 | the underlying type here. */ | |
ee76b931 | 3846 | scope = non_reference (scope); |
a723baf1 MM |
3847 | /* The type of the POSTFIX_EXPRESSION must be |
3848 | complete. */ | |
3849 | scope = complete_type_or_else (scope, NULL_TREE); | |
3850 | /* Let the name lookup machinery know that we are | |
3851 | processing a class member access expression. */ | |
3852 | parser->context->object_type = scope; | |
3853 | /* If something went wrong, we want to be able to | |
3854 | discern that case, as opposed to the case where | |
3855 | there was no SCOPE due to the type of expression | |
3856 | being dependent. */ | |
3857 | if (!scope) | |
3858 | scope = error_mark_node; | |
be799b1e MM |
3859 | /* If the SCOPE was erroneous, make the various |
3860 | semantic analysis functions exit quickly -- and | |
3861 | without issuing additional error messages. */ | |
3862 | if (scope == error_mark_node) | |
3863 | postfix_expression = error_mark_node; | |
a723baf1 MM |
3864 | } |
3865 | ||
3866 | /* Consume the `.' or `->' operator. */ | |
3867 | cp_lexer_consume_token (parser->lexer); | |
3868 | /* If the SCOPE is not a scalar type, we are looking at an | |
3869 | ordinary class member access expression, rather than a | |
3870 | pseudo-destructor-name. */ | |
3871 | if (!scope || !SCALAR_TYPE_P (scope)) | |
3872 | { | |
3873 | template_p = cp_parser_optional_template_keyword (parser); | |
3874 | /* Parse the id-expression. */ | |
3875 | name = cp_parser_id_expression (parser, | |
3876 | template_p, | |
3877 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
3878 | /*template_p=*/NULL, |
3879 | /*declarator_p=*/false); | |
a723baf1 MM |
3880 | /* In general, build a SCOPE_REF if the member name is |
3881 | qualified. However, if the name was not dependent | |
3882 | and has already been resolved; there is no need to | |
3883 | build the SCOPE_REF. For example; | |
3884 | ||
3885 | struct X { void f(); }; | |
3886 | template <typename T> void f(T* t) { t->X::f(); } | |
3887 | ||
d17811fd MM |
3888 | Even though "t" is dependent, "X::f" is not and has |
3889 | been resolved to a BASELINK; there is no need to | |
a723baf1 | 3890 | include scope information. */ |
a6bd211d JM |
3891 | |
3892 | /* But we do need to remember that there was an explicit | |
3893 | scope for virtual function calls. */ | |
3894 | if (parser->scope) | |
b3445994 | 3895 | idk = CP_ID_KIND_QUALIFIED; |
a6bd211d | 3896 | |
a723baf1 MM |
3897 | if (name != error_mark_node |
3898 | && !BASELINK_P (name) | |
3899 | && parser->scope) | |
3900 | { | |
3901 | name = build_nt (SCOPE_REF, parser->scope, name); | |
3902 | parser->scope = NULL_TREE; | |
3903 | parser->qualifying_scope = NULL_TREE; | |
3904 | parser->object_scope = NULL_TREE; | |
3905 | } | |
3906 | postfix_expression | |
3907 | = finish_class_member_access_expr (postfix_expression, name); | |
3908 | } | |
3909 | /* Otherwise, try the pseudo-destructor-name production. */ | |
3910 | else | |
3911 | { | |
90808894 | 3912 | tree s = NULL_TREE; |
a723baf1 MM |
3913 | tree type; |
3914 | ||
3915 | /* Parse the pseudo-destructor-name. */ | |
3916 | cp_parser_pseudo_destructor_name (parser, &s, &type); | |
3917 | /* Form the call. */ | |
3918 | postfix_expression | |
3919 | = finish_pseudo_destructor_expr (postfix_expression, | |
3920 | s, TREE_TYPE (type)); | |
3921 | } | |
3922 | ||
3923 | /* We no longer need to look up names in the scope of the | |
3924 | object on the left-hand side of the `.' or `->' | |
3925 | operator. */ | |
3926 | parser->context->object_type = NULL_TREE; | |
a5ac3982 | 3927 | /* These operators may not appear in constant-expressions. */ |
67c03833 | 3928 | if (parser->integral_constant_expression_p |
263ee052 | 3929 | /* The "->" operator is allowed in the implementation |
643aee72 MM |
3930 | of "offsetof". The "." operator may appear in the |
3931 | name of the member. */ | |
3932 | && !parser->in_offsetof_p) | |
a5ac3982 | 3933 | { |
67c03833 | 3934 | if (!parser->allow_non_integral_constant_expression_p) |
a5ac3982 | 3935 | postfix_expression |
67c03833 | 3936 | = (cp_parser_non_integral_constant_expression |
a5ac3982 | 3937 | (token_type == CPP_DEREF ? "'->'" : "`.'")); |
67c03833 | 3938 | parser->non_integral_constant_expression_p = true; |
a5ac3982 | 3939 | } |
a723baf1 MM |
3940 | } |
3941 | break; | |
3942 | ||
3943 | case CPP_PLUS_PLUS: | |
3944 | /* postfix-expression ++ */ | |
3945 | /* Consume the `++' token. */ | |
3946 | cp_lexer_consume_token (parser->lexer); | |
a5ac3982 MM |
3947 | /* Generate a representation for the complete expression. */ |
3948 | postfix_expression | |
3949 | = finish_increment_expr (postfix_expression, | |
3950 | POSTINCREMENT_EXPR); | |
14d22dd6 | 3951 | /* Increments may not appear in constant-expressions. */ |
67c03833 | 3952 | if (parser->integral_constant_expression_p) |
14d22dd6 | 3953 | { |
67c03833 | 3954 | if (!parser->allow_non_integral_constant_expression_p) |
a5ac3982 | 3955 | postfix_expression |
67c03833 JM |
3956 | = cp_parser_non_integral_constant_expression ("an increment"); |
3957 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 3958 | } |
b3445994 | 3959 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3960 | break; |
3961 | ||
3962 | case CPP_MINUS_MINUS: | |
3963 | /* postfix-expression -- */ | |
3964 | /* Consume the `--' token. */ | |
3965 | cp_lexer_consume_token (parser->lexer); | |
a5ac3982 MM |
3966 | /* Generate a representation for the complete expression. */ |
3967 | postfix_expression | |
3968 | = finish_increment_expr (postfix_expression, | |
3969 | POSTDECREMENT_EXPR); | |
14d22dd6 | 3970 | /* Decrements may not appear in constant-expressions. */ |
67c03833 | 3971 | if (parser->integral_constant_expression_p) |
14d22dd6 | 3972 | { |
67c03833 | 3973 | if (!parser->allow_non_integral_constant_expression_p) |
a5ac3982 | 3974 | postfix_expression |
67c03833 JM |
3975 | = cp_parser_non_integral_constant_expression ("a decrement"); |
3976 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 3977 | } |
b3445994 | 3978 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3979 | break; |
3980 | ||
3981 | default: | |
3982 | return postfix_expression; | |
3983 | } | |
3984 | } | |
3985 | ||
3986 | /* We should never get here. */ | |
3987 | abort (); | |
3988 | return error_mark_node; | |
3989 | } | |
3990 | ||
7efa3e22 | 3991 | /* Parse a parenthesized expression-list. |
a723baf1 MM |
3992 | |
3993 | expression-list: | |
3994 | assignment-expression | |
3995 | expression-list, assignment-expression | |
3996 | ||
7efa3e22 NS |
3997 | attribute-list: |
3998 | expression-list | |
3999 | identifier | |
4000 | identifier, expression-list | |
4001 | ||
a723baf1 MM |
4002 | Returns a TREE_LIST. The TREE_VALUE of each node is a |
4003 | representation of an assignment-expression. Note that a TREE_LIST | |
7efa3e22 NS |
4004 | is returned even if there is only a single expression in the list. |
4005 | error_mark_node is returned if the ( and or ) are | |
4006 | missing. NULL_TREE is returned on no expressions. The parentheses | |
4007 | are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute | |
39703eb9 MM |
4008 | list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P |
4009 | indicates whether or not all of the expressions in the list were | |
4010 | constant. */ | |
a723baf1 MM |
4011 | |
4012 | static tree | |
39703eb9 MM |
4013 | cp_parser_parenthesized_expression_list (cp_parser* parser, |
4014 | bool is_attribute_list, | |
4015 | bool *non_constant_p) | |
a723baf1 MM |
4016 | { |
4017 | tree expression_list = NULL_TREE; | |
7efa3e22 | 4018 | tree identifier = NULL_TREE; |
39703eb9 MM |
4019 | |
4020 | /* Assume all the expressions will be constant. */ | |
4021 | if (non_constant_p) | |
4022 | *non_constant_p = false; | |
4023 | ||
7efa3e22 NS |
4024 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) |
4025 | return error_mark_node; | |
4026 | ||
a723baf1 | 4027 | /* Consume expressions until there are no more. */ |
7efa3e22 NS |
4028 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) |
4029 | while (true) | |
4030 | { | |
4031 | tree expr; | |
4032 | ||
4033 | /* At the beginning of attribute lists, check to see if the | |
4034 | next token is an identifier. */ | |
4035 | if (is_attribute_list | |
4036 | && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME) | |
4037 | { | |
4038 | cp_token *token; | |
4039 | ||
4040 | /* Consume the identifier. */ | |
4041 | token = cp_lexer_consume_token (parser->lexer); | |
4042 | /* Save the identifier. */ | |
4043 | identifier = token->value; | |
4044 | } | |
4045 | else | |
4046 | { | |
4047 | /* Parse the next assignment-expression. */ | |
39703eb9 MM |
4048 | if (non_constant_p) |
4049 | { | |
4050 | bool expr_non_constant_p; | |
4051 | expr = (cp_parser_constant_expression | |
4052 | (parser, /*allow_non_constant_p=*/true, | |
4053 | &expr_non_constant_p)); | |
4054 | if (expr_non_constant_p) | |
4055 | *non_constant_p = true; | |
4056 | } | |
4057 | else | |
4058 | expr = cp_parser_assignment_expression (parser); | |
a723baf1 | 4059 | |
7efa3e22 NS |
4060 | /* Add it to the list. We add error_mark_node |
4061 | expressions to the list, so that we can still tell if | |
4062 | the correct form for a parenthesized expression-list | |
4063 | is found. That gives better errors. */ | |
4064 | expression_list = tree_cons (NULL_TREE, expr, expression_list); | |
a723baf1 | 4065 | |
7efa3e22 NS |
4066 | if (expr == error_mark_node) |
4067 | goto skip_comma; | |
4068 | } | |
a723baf1 | 4069 | |
7efa3e22 NS |
4070 | /* After the first item, attribute lists look the same as |
4071 | expression lists. */ | |
4072 | is_attribute_list = false; | |
4073 | ||
4074 | get_comma:; | |
4075 | /* If the next token isn't a `,', then we are done. */ | |
4076 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
4077 | break; | |
4078 | ||
4079 | /* Otherwise, consume the `,' and keep going. */ | |
4080 | cp_lexer_consume_token (parser->lexer); | |
4081 | } | |
4082 | ||
4083 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
4084 | { | |
4085 | int ending; | |
4086 | ||
4087 | skip_comma:; | |
4088 | /* We try and resync to an unnested comma, as that will give the | |
4089 | user better diagnostics. */ | |
4bb8ca28 MM |
4090 | ending = cp_parser_skip_to_closing_parenthesis (parser, |
4091 | /*recovering=*/true, | |
4092 | /*or_comma=*/true, | |
a668c6ad | 4093 | /*consume_paren=*/true); |
7efa3e22 NS |
4094 | if (ending < 0) |
4095 | goto get_comma; | |
4096 | if (!ending) | |
4097 | return error_mark_node; | |
a723baf1 MM |
4098 | } |
4099 | ||
4100 | /* We built up the list in reverse order so we must reverse it now. */ | |
7efa3e22 NS |
4101 | expression_list = nreverse (expression_list); |
4102 | if (identifier) | |
4103 | expression_list = tree_cons (NULL_TREE, identifier, expression_list); | |
4104 | ||
4105 | return expression_list; | |
a723baf1 MM |
4106 | } |
4107 | ||
4108 | /* Parse a pseudo-destructor-name. | |
4109 | ||
4110 | pseudo-destructor-name: | |
4111 | :: [opt] nested-name-specifier [opt] type-name :: ~ type-name | |
4112 | :: [opt] nested-name-specifier template template-id :: ~ type-name | |
4113 | :: [opt] nested-name-specifier [opt] ~ type-name | |
4114 | ||
4115 | If either of the first two productions is used, sets *SCOPE to the | |
4116 | TYPE specified before the final `::'. Otherwise, *SCOPE is set to | |
4117 | NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name, | |
4118 | or ERROR_MARK_NODE if no type-name is present. */ | |
4119 | ||
4120 | static void | |
94edc4ab NN |
4121 | cp_parser_pseudo_destructor_name (cp_parser* parser, |
4122 | tree* scope, | |
4123 | tree* type) | |
a723baf1 MM |
4124 | { |
4125 | bool nested_name_specifier_p; | |
4126 | ||
4127 | /* Look for the optional `::' operator. */ | |
4128 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true); | |
4129 | /* Look for the optional nested-name-specifier. */ | |
4130 | nested_name_specifier_p | |
4131 | = (cp_parser_nested_name_specifier_opt (parser, | |
4132 | /*typename_keyword_p=*/false, | |
4133 | /*check_dependency_p=*/true, | |
a668c6ad MM |
4134 | /*type_p=*/false, |
4135 | /*is_declaration=*/true) | |
a723baf1 MM |
4136 | != NULL_TREE); |
4137 | /* Now, if we saw a nested-name-specifier, we might be doing the | |
4138 | second production. */ | |
4139 | if (nested_name_specifier_p | |
4140 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
4141 | { | |
4142 | /* Consume the `template' keyword. */ | |
4143 | cp_lexer_consume_token (parser->lexer); | |
4144 | /* Parse the template-id. */ | |
4145 | cp_parser_template_id (parser, | |
4146 | /*template_keyword_p=*/true, | |
a668c6ad MM |
4147 | /*check_dependency_p=*/false, |
4148 | /*is_declaration=*/true); | |
a723baf1 MM |
4149 | /* Look for the `::' token. */ |
4150 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
4151 | } | |
4152 | /* If the next token is not a `~', then there might be some | |
9bcb9aae | 4153 | additional qualification. */ |
a723baf1 MM |
4154 | else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL)) |
4155 | { | |
4156 | /* Look for the type-name. */ | |
4157 | *scope = TREE_TYPE (cp_parser_type_name (parser)); | |
4158 | /* Look for the `::' token. */ | |
4159 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
4160 | } | |
4161 | else | |
4162 | *scope = NULL_TREE; | |
4163 | ||
4164 | /* Look for the `~'. */ | |
4165 | cp_parser_require (parser, CPP_COMPL, "`~'"); | |
4166 | /* Look for the type-name again. We are not responsible for | |
4167 | checking that it matches the first type-name. */ | |
4168 | *type = cp_parser_type_name (parser); | |
4169 | } | |
4170 | ||
4171 | /* Parse a unary-expression. | |
4172 | ||
4173 | unary-expression: | |
4174 | postfix-expression | |
4175 | ++ cast-expression | |
4176 | -- cast-expression | |
4177 | unary-operator cast-expression | |
4178 | sizeof unary-expression | |
4179 | sizeof ( type-id ) | |
4180 | new-expression | |
4181 | delete-expression | |
4182 | ||
4183 | GNU Extensions: | |
4184 | ||
4185 | unary-expression: | |
4186 | __extension__ cast-expression | |
4187 | __alignof__ unary-expression | |
4188 | __alignof__ ( type-id ) | |
4189 | __real__ cast-expression | |
4190 | __imag__ cast-expression | |
4191 | && identifier | |
4192 | ||
4193 | ADDRESS_P is true iff the unary-expression is appearing as the | |
4194 | operand of the `&' operator. | |
4195 | ||
34cd5ae7 | 4196 | Returns a representation of the expression. */ |
a723baf1 MM |
4197 | |
4198 | static tree | |
4199 | cp_parser_unary_expression (cp_parser *parser, bool address_p) | |
4200 | { | |
4201 | cp_token *token; | |
4202 | enum tree_code unary_operator; | |
4203 | ||
4204 | /* Peek at the next token. */ | |
4205 | token = cp_lexer_peek_token (parser->lexer); | |
4206 | /* Some keywords give away the kind of expression. */ | |
4207 | if (token->type == CPP_KEYWORD) | |
4208 | { | |
4209 | enum rid keyword = token->keyword; | |
4210 | ||
4211 | switch (keyword) | |
4212 | { | |
4213 | case RID_ALIGNOF: | |
a723baf1 MM |
4214 | case RID_SIZEOF: |
4215 | { | |
4216 | tree operand; | |
7a18b933 | 4217 | enum tree_code op; |
a723baf1 | 4218 | |
7a18b933 NS |
4219 | op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR; |
4220 | /* Consume the token. */ | |
a723baf1 MM |
4221 | cp_lexer_consume_token (parser->lexer); |
4222 | /* Parse the operand. */ | |
4223 | operand = cp_parser_sizeof_operand (parser, keyword); | |
4224 | ||
7a18b933 NS |
4225 | if (TYPE_P (operand)) |
4226 | return cxx_sizeof_or_alignof_type (operand, op, true); | |
a723baf1 | 4227 | else |
7a18b933 | 4228 | return cxx_sizeof_or_alignof_expr (operand, op); |
a723baf1 MM |
4229 | } |
4230 | ||
4231 | case RID_NEW: | |
4232 | return cp_parser_new_expression (parser); | |
4233 | ||
4234 | case RID_DELETE: | |
4235 | return cp_parser_delete_expression (parser); | |
4236 | ||
4237 | case RID_EXTENSION: | |
4238 | { | |
4239 | /* The saved value of the PEDANTIC flag. */ | |
4240 | int saved_pedantic; | |
4241 | tree expr; | |
4242 | ||
4243 | /* Save away the PEDANTIC flag. */ | |
4244 | cp_parser_extension_opt (parser, &saved_pedantic); | |
4245 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4246 | expr = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4247 | /* Restore the PEDANTIC flag. */ |
4248 | pedantic = saved_pedantic; | |
4249 | ||
4250 | return expr; | |
4251 | } | |
4252 | ||
4253 | case RID_REALPART: | |
4254 | case RID_IMAGPART: | |
4255 | { | |
4256 | tree expression; | |
4257 | ||
4258 | /* Consume the `__real__' or `__imag__' token. */ | |
4259 | cp_lexer_consume_token (parser->lexer); | |
4260 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4261 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4262 | /* Create the complete representation. */ |
4263 | return build_x_unary_op ((keyword == RID_REALPART | |
4264 | ? REALPART_EXPR : IMAGPART_EXPR), | |
4265 | expression); | |
4266 | } | |
4267 | break; | |
4268 | ||
4269 | default: | |
4270 | break; | |
4271 | } | |
4272 | } | |
4273 | ||
4274 | /* Look for the `:: new' and `:: delete', which also signal the | |
4275 | beginning of a new-expression, or delete-expression, | |
4276 | respectively. If the next token is `::', then it might be one of | |
4277 | these. */ | |
4278 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
4279 | { | |
4280 | enum rid keyword; | |
4281 | ||
4282 | /* See if the token after the `::' is one of the keywords in | |
4283 | which we're interested. */ | |
4284 | keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword; | |
4285 | /* If it's `new', we have a new-expression. */ | |
4286 | if (keyword == RID_NEW) | |
4287 | return cp_parser_new_expression (parser); | |
4288 | /* Similarly, for `delete'. */ | |
4289 | else if (keyword == RID_DELETE) | |
4290 | return cp_parser_delete_expression (parser); | |
4291 | } | |
4292 | ||
4293 | /* Look for a unary operator. */ | |
4294 | unary_operator = cp_parser_unary_operator (token); | |
4295 | /* The `++' and `--' operators can be handled similarly, even though | |
4296 | they are not technically unary-operators in the grammar. */ | |
4297 | if (unary_operator == ERROR_MARK) | |
4298 | { | |
4299 | if (token->type == CPP_PLUS_PLUS) | |
4300 | unary_operator = PREINCREMENT_EXPR; | |
4301 | else if (token->type == CPP_MINUS_MINUS) | |
4302 | unary_operator = PREDECREMENT_EXPR; | |
4303 | /* Handle the GNU address-of-label extension. */ | |
4304 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
4305 | && token->type == CPP_AND_AND) | |
4306 | { | |
4307 | tree identifier; | |
4308 | ||
4309 | /* Consume the '&&' token. */ | |
4310 | cp_lexer_consume_token (parser->lexer); | |
4311 | /* Look for the identifier. */ | |
4312 | identifier = cp_parser_identifier (parser); | |
4313 | /* Create an expression representing the address. */ | |
4314 | return finish_label_address_expr (identifier); | |
4315 | } | |
4316 | } | |
4317 | if (unary_operator != ERROR_MARK) | |
4318 | { | |
4319 | tree cast_expression; | |
a5ac3982 MM |
4320 | tree expression = error_mark_node; |
4321 | const char *non_constant_p = NULL; | |
a723baf1 MM |
4322 | |
4323 | /* Consume the operator token. */ | |
4324 | token = cp_lexer_consume_token (parser->lexer); | |
4325 | /* Parse the cast-expression. */ | |
4326 | cast_expression | |
4327 | = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR); | |
4328 | /* Now, build an appropriate representation. */ | |
4329 | switch (unary_operator) | |
4330 | { | |
4331 | case INDIRECT_REF: | |
a5ac3982 MM |
4332 | non_constant_p = "`*'"; |
4333 | expression = build_x_indirect_ref (cast_expression, "unary *"); | |
4334 | break; | |
4335 | ||
a723baf1 | 4336 | case ADDR_EXPR: |
263ee052 MM |
4337 | /* The "&" operator is allowed in the implementation of |
4338 | "offsetof". */ | |
4339 | if (!parser->in_offsetof_p) | |
4340 | non_constant_p = "`&'"; | |
a5ac3982 | 4341 | /* Fall through. */ |
d17811fd | 4342 | case BIT_NOT_EXPR: |
a5ac3982 MM |
4343 | expression = build_x_unary_op (unary_operator, cast_expression); |
4344 | break; | |
4345 | ||
14d22dd6 MM |
4346 | case PREINCREMENT_EXPR: |
4347 | case PREDECREMENT_EXPR: | |
a5ac3982 MM |
4348 | non_constant_p = (unary_operator == PREINCREMENT_EXPR |
4349 | ? "`++'" : "`--'"); | |
14d22dd6 | 4350 | /* Fall through. */ |
a723baf1 MM |
4351 | case CONVERT_EXPR: |
4352 | case NEGATE_EXPR: | |
4353 | case TRUTH_NOT_EXPR: | |
a5ac3982 MM |
4354 | expression = finish_unary_op_expr (unary_operator, cast_expression); |
4355 | break; | |
a723baf1 | 4356 | |
a723baf1 MM |
4357 | default: |
4358 | abort (); | |
a723baf1 | 4359 | } |
a5ac3982 | 4360 | |
67c03833 | 4361 | if (non_constant_p && parser->integral_constant_expression_p) |
a5ac3982 | 4362 | { |
67c03833 JM |
4363 | if (!parser->allow_non_integral_constant_expression_p) |
4364 | return cp_parser_non_integral_constant_expression (non_constant_p); | |
4365 | parser->non_integral_constant_expression_p = true; | |
a5ac3982 MM |
4366 | } |
4367 | ||
4368 | return expression; | |
a723baf1 MM |
4369 | } |
4370 | ||
4371 | return cp_parser_postfix_expression (parser, address_p); | |
4372 | } | |
4373 | ||
4374 | /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a | |
4375 | unary-operator, the corresponding tree code is returned. */ | |
4376 | ||
4377 | static enum tree_code | |
94edc4ab | 4378 | cp_parser_unary_operator (cp_token* token) |
a723baf1 MM |
4379 | { |
4380 | switch (token->type) | |
4381 | { | |
4382 | case CPP_MULT: | |
4383 | return INDIRECT_REF; | |
4384 | ||
4385 | case CPP_AND: | |
4386 | return ADDR_EXPR; | |
4387 | ||
4388 | case CPP_PLUS: | |
4389 | return CONVERT_EXPR; | |
4390 | ||
4391 | case CPP_MINUS: | |
4392 | return NEGATE_EXPR; | |
4393 | ||
4394 | case CPP_NOT: | |
4395 | return TRUTH_NOT_EXPR; | |
4396 | ||
4397 | case CPP_COMPL: | |
4398 | return BIT_NOT_EXPR; | |
4399 | ||
4400 | default: | |
4401 | return ERROR_MARK; | |
4402 | } | |
4403 | } | |
4404 | ||
4405 | /* Parse a new-expression. | |
4406 | ||
ca099ac8 | 4407 | new-expression: |
a723baf1 MM |
4408 | :: [opt] new new-placement [opt] new-type-id new-initializer [opt] |
4409 | :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt] | |
4410 | ||
4411 | Returns a representation of the expression. */ | |
4412 | ||
4413 | static tree | |
94edc4ab | 4414 | cp_parser_new_expression (cp_parser* parser) |
a723baf1 MM |
4415 | { |
4416 | bool global_scope_p; | |
4417 | tree placement; | |
4418 | tree type; | |
4419 | tree initializer; | |
4420 | ||
4421 | /* Look for the optional `::' operator. */ | |
4422 | global_scope_p | |
4423 | = (cp_parser_global_scope_opt (parser, | |
4424 | /*current_scope_valid_p=*/false) | |
4425 | != NULL_TREE); | |
4426 | /* Look for the `new' operator. */ | |
4427 | cp_parser_require_keyword (parser, RID_NEW, "`new'"); | |
4428 | /* There's no easy way to tell a new-placement from the | |
4429 | `( type-id )' construct. */ | |
4430 | cp_parser_parse_tentatively (parser); | |
4431 | /* Look for a new-placement. */ | |
4432 | placement = cp_parser_new_placement (parser); | |
4433 | /* If that didn't work out, there's no new-placement. */ | |
4434 | if (!cp_parser_parse_definitely (parser)) | |
4435 | placement = NULL_TREE; | |
4436 | ||
4437 | /* If the next token is a `(', then we have a parenthesized | |
4438 | type-id. */ | |
4439 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4440 | { | |
4441 | /* Consume the `('. */ | |
4442 | cp_lexer_consume_token (parser->lexer); | |
4443 | /* Parse the type-id. */ | |
4444 | type = cp_parser_type_id (parser); | |
4445 | /* Look for the closing `)'. */ | |
4446 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
4447 | } | |
4448 | /* Otherwise, there must be a new-type-id. */ | |
4449 | else | |
4450 | type = cp_parser_new_type_id (parser); | |
4451 | ||
4452 | /* If the next token is a `(', then we have a new-initializer. */ | |
4453 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4454 | initializer = cp_parser_new_initializer (parser); | |
4455 | else | |
4456 | initializer = NULL_TREE; | |
4457 | ||
4458 | /* Create a representation of the new-expression. */ | |
4459 | return build_new (placement, type, initializer, global_scope_p); | |
4460 | } | |
4461 | ||
4462 | /* Parse a new-placement. | |
4463 | ||
4464 | new-placement: | |
4465 | ( expression-list ) | |
4466 | ||
4467 | Returns the same representation as for an expression-list. */ | |
4468 | ||
4469 | static tree | |
94edc4ab | 4470 | cp_parser_new_placement (cp_parser* parser) |
a723baf1 MM |
4471 | { |
4472 | tree expression_list; | |
4473 | ||
a723baf1 | 4474 | /* Parse the expression-list. */ |
39703eb9 MM |
4475 | expression_list = (cp_parser_parenthesized_expression_list |
4476 | (parser, false, /*non_constant_p=*/NULL)); | |
a723baf1 MM |
4477 | |
4478 | return expression_list; | |
4479 | } | |
4480 | ||
4481 | /* Parse a new-type-id. | |
4482 | ||
4483 | new-type-id: | |
4484 | type-specifier-seq new-declarator [opt] | |
4485 | ||
4486 | Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq, | |
4487 | and whose TREE_VALUE is the new-declarator. */ | |
4488 | ||
4489 | static tree | |
94edc4ab | 4490 | cp_parser_new_type_id (cp_parser* parser) |
a723baf1 MM |
4491 | { |
4492 | tree type_specifier_seq; | |
4493 | tree declarator; | |
4494 | const char *saved_message; | |
4495 | ||
4496 | /* The type-specifier sequence must not contain type definitions. | |
4497 | (It cannot contain declarations of new types either, but if they | |
4498 | are not definitions we will catch that because they are not | |
4499 | complete.) */ | |
4500 | saved_message = parser->type_definition_forbidden_message; | |
4501 | parser->type_definition_forbidden_message | |
4502 | = "types may not be defined in a new-type-id"; | |
4503 | /* Parse the type-specifier-seq. */ | |
4504 | type_specifier_seq = cp_parser_type_specifier_seq (parser); | |
4505 | /* Restore the old message. */ | |
4506 | parser->type_definition_forbidden_message = saved_message; | |
4507 | /* Parse the new-declarator. */ | |
4508 | declarator = cp_parser_new_declarator_opt (parser); | |
4509 | ||
4510 | return build_tree_list (type_specifier_seq, declarator); | |
4511 | } | |
4512 | ||
4513 | /* Parse an (optional) new-declarator. | |
4514 | ||
4515 | new-declarator: | |
4516 | ptr-operator new-declarator [opt] | |
4517 | direct-new-declarator | |
4518 | ||
4519 | Returns a representation of the declarator. See | |
4520 | cp_parser_declarator for the representations used. */ | |
4521 | ||
4522 | static tree | |
94edc4ab | 4523 | cp_parser_new_declarator_opt (cp_parser* parser) |
a723baf1 MM |
4524 | { |
4525 | enum tree_code code; | |
4526 | tree type; | |
4527 | tree cv_qualifier_seq; | |
4528 | ||
4529 | /* We don't know if there's a ptr-operator next, or not. */ | |
4530 | cp_parser_parse_tentatively (parser); | |
4531 | /* Look for a ptr-operator. */ | |
4532 | code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq); | |
4533 | /* If that worked, look for more new-declarators. */ | |
4534 | if (cp_parser_parse_definitely (parser)) | |
4535 | { | |
4536 | tree declarator; | |
4537 | ||
4538 | /* Parse another optional declarator. */ | |
4539 | declarator = cp_parser_new_declarator_opt (parser); | |
4540 | ||
4541 | /* Create the representation of the declarator. */ | |
4542 | if (code == INDIRECT_REF) | |
4543 | declarator = make_pointer_declarator (cv_qualifier_seq, | |
4544 | declarator); | |
4545 | else | |
4546 | declarator = make_reference_declarator (cv_qualifier_seq, | |
4547 | declarator); | |
4548 | ||
4549 | /* Handle the pointer-to-member case. */ | |
4550 | if (type) | |
4551 | declarator = build_nt (SCOPE_REF, type, declarator); | |
4552 | ||
4553 | return declarator; | |
4554 | } | |
4555 | ||
4556 | /* If the next token is a `[', there is a direct-new-declarator. */ | |
4557 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
4558 | return cp_parser_direct_new_declarator (parser); | |
4559 | ||
4560 | return NULL_TREE; | |
4561 | } | |
4562 | ||
4563 | /* Parse a direct-new-declarator. | |
4564 | ||
4565 | direct-new-declarator: | |
4566 | [ expression ] | |
4567 | direct-new-declarator [constant-expression] | |
4568 | ||
4569 | Returns an ARRAY_REF, following the same conventions as are | |
4570 | documented for cp_parser_direct_declarator. */ | |
4571 | ||
4572 | static tree | |
94edc4ab | 4573 | cp_parser_direct_new_declarator (cp_parser* parser) |
a723baf1 MM |
4574 | { |
4575 | tree declarator = NULL_TREE; | |
4576 | ||
4577 | while (true) | |
4578 | { | |
4579 | tree expression; | |
4580 | ||
4581 | /* Look for the opening `['. */ | |
4582 | cp_parser_require (parser, CPP_OPEN_SQUARE, "`['"); | |
4583 | /* The first expression is not required to be constant. */ | |
4584 | if (!declarator) | |
4585 | { | |
4586 | expression = cp_parser_expression (parser); | |
4587 | /* The standard requires that the expression have integral | |
4588 | type. DR 74 adds enumeration types. We believe that the | |
4589 | real intent is that these expressions be handled like the | |
4590 | expression in a `switch' condition, which also allows | |
4591 | classes with a single conversion to integral or | |
4592 | enumeration type. */ | |
4593 | if (!processing_template_decl) | |
4594 | { | |
4595 | expression | |
4596 | = build_expr_type_conversion (WANT_INT | WANT_ENUM, | |
4597 | expression, | |
b746c5dc | 4598 | /*complain=*/true); |
a723baf1 MM |
4599 | if (!expression) |
4600 | { | |
4601 | error ("expression in new-declarator must have integral or enumeration type"); | |
4602 | expression = error_mark_node; | |
4603 | } | |
4604 | } | |
4605 | } | |
4606 | /* But all the other expressions must be. */ | |
4607 | else | |
14d22dd6 MM |
4608 | expression |
4609 | = cp_parser_constant_expression (parser, | |
4610 | /*allow_non_constant=*/false, | |
4611 | NULL); | |
a723baf1 MM |
4612 | /* Look for the closing `]'. */ |
4613 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
4614 | ||
4615 | /* Add this bound to the declarator. */ | |
4616 | declarator = build_nt (ARRAY_REF, declarator, expression); | |
4617 | ||
4618 | /* If the next token is not a `[', then there are no more | |
4619 | bounds. */ | |
4620 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) | |
4621 | break; | |
4622 | } | |
4623 | ||
4624 | return declarator; | |
4625 | } | |
4626 | ||
4627 | /* Parse a new-initializer. | |
4628 | ||
4629 | new-initializer: | |
4630 | ( expression-list [opt] ) | |
4631 | ||
34cd5ae7 | 4632 | Returns a representation of the expression-list. If there is no |
a723baf1 MM |
4633 | expression-list, VOID_ZERO_NODE is returned. */ |
4634 | ||
4635 | static tree | |
94edc4ab | 4636 | cp_parser_new_initializer (cp_parser* parser) |
a723baf1 MM |
4637 | { |
4638 | tree expression_list; | |
4639 | ||
39703eb9 MM |
4640 | expression_list = (cp_parser_parenthesized_expression_list |
4641 | (parser, false, /*non_constant_p=*/NULL)); | |
7efa3e22 | 4642 | if (!expression_list) |
a723baf1 | 4643 | expression_list = void_zero_node; |
a723baf1 MM |
4644 | |
4645 | return expression_list; | |
4646 | } | |
4647 | ||
4648 | /* Parse a delete-expression. | |
4649 | ||
4650 | delete-expression: | |
4651 | :: [opt] delete cast-expression | |
4652 | :: [opt] delete [ ] cast-expression | |
4653 | ||
4654 | Returns a representation of the expression. */ | |
4655 | ||
4656 | static tree | |
94edc4ab | 4657 | cp_parser_delete_expression (cp_parser* parser) |
a723baf1 MM |
4658 | { |
4659 | bool global_scope_p; | |
4660 | bool array_p; | |
4661 | tree expression; | |
4662 | ||
4663 | /* Look for the optional `::' operator. */ | |
4664 | global_scope_p | |
4665 | = (cp_parser_global_scope_opt (parser, | |
4666 | /*current_scope_valid_p=*/false) | |
4667 | != NULL_TREE); | |
4668 | /* Look for the `delete' keyword. */ | |
4669 | cp_parser_require_keyword (parser, RID_DELETE, "`delete'"); | |
4670 | /* See if the array syntax is in use. */ | |
4671 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
4672 | { | |
4673 | /* Consume the `[' token. */ | |
4674 | cp_lexer_consume_token (parser->lexer); | |
4675 | /* Look for the `]' token. */ | |
4676 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
4677 | /* Remember that this is the `[]' construct. */ | |
4678 | array_p = true; | |
4679 | } | |
4680 | else | |
4681 | array_p = false; | |
4682 | ||
4683 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4684 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4685 | |
4686 | return delete_sanity (expression, NULL_TREE, array_p, global_scope_p); | |
4687 | } | |
4688 | ||
4689 | /* Parse a cast-expression. | |
4690 | ||
4691 | cast-expression: | |
4692 | unary-expression | |
4693 | ( type-id ) cast-expression | |
4694 | ||
4695 | Returns a representation of the expression. */ | |
4696 | ||
4697 | static tree | |
4698 | cp_parser_cast_expression (cp_parser *parser, bool address_p) | |
4699 | { | |
4700 | /* If it's a `(', then we might be looking at a cast. */ | |
4701 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4702 | { | |
4703 | tree type = NULL_TREE; | |
4704 | tree expr = NULL_TREE; | |
4705 | bool compound_literal_p; | |
4706 | const char *saved_message; | |
4707 | ||
4708 | /* There's no way to know yet whether or not this is a cast. | |
4709 | For example, `(int (3))' is a unary-expression, while `(int) | |
4710 | 3' is a cast. So, we resort to parsing tentatively. */ | |
4711 | cp_parser_parse_tentatively (parser); | |
4712 | /* Types may not be defined in a cast. */ | |
4713 | saved_message = parser->type_definition_forbidden_message; | |
4714 | parser->type_definition_forbidden_message | |
4715 | = "types may not be defined in casts"; | |
4716 | /* Consume the `('. */ | |
4717 | cp_lexer_consume_token (parser->lexer); | |
4718 | /* A very tricky bit is that `(struct S) { 3 }' is a | |
4719 | compound-literal (which we permit in C++ as an extension). | |
4720 | But, that construct is not a cast-expression -- it is a | |
4721 | postfix-expression. (The reason is that `(struct S) { 3 }.i' | |
4722 | is legal; if the compound-literal were a cast-expression, | |
4723 | you'd need an extra set of parentheses.) But, if we parse | |
4724 | the type-id, and it happens to be a class-specifier, then we | |
4725 | will commit to the parse at that point, because we cannot | |
4726 | undo the action that is done when creating a new class. So, | |
4727 | then we cannot back up and do a postfix-expression. | |
4728 | ||
4729 | Therefore, we scan ahead to the closing `)', and check to see | |
4730 | if the token after the `)' is a `{'. If so, we are not | |
4731 | looking at a cast-expression. | |
4732 | ||
4733 | Save tokens so that we can put them back. */ | |
4734 | cp_lexer_save_tokens (parser->lexer); | |
4735 | /* Skip tokens until the next token is a closing parenthesis. | |
4736 | If we find the closing `)', and the next token is a `{', then | |
4737 | we are looking at a compound-literal. */ | |
4738 | compound_literal_p | |
a668c6ad MM |
4739 | = (cp_parser_skip_to_closing_parenthesis (parser, false, false, |
4740 | /*consume_paren=*/true) | |
a723baf1 MM |
4741 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)); |
4742 | /* Roll back the tokens we skipped. */ | |
4743 | cp_lexer_rollback_tokens (parser->lexer); | |
4744 | /* If we were looking at a compound-literal, simulate an error | |
4745 | so that the call to cp_parser_parse_definitely below will | |
4746 | fail. */ | |
4747 | if (compound_literal_p) | |
4748 | cp_parser_simulate_error (parser); | |
4749 | else | |
4750 | { | |
4f8163b1 MM |
4751 | bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
4752 | parser->in_type_id_in_expr_p = true; | |
a723baf1 MM |
4753 | /* Look for the type-id. */ |
4754 | type = cp_parser_type_id (parser); | |
4755 | /* Look for the closing `)'. */ | |
4756 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
4f8163b1 | 4757 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 MM |
4758 | } |
4759 | ||
4760 | /* Restore the saved message. */ | |
4761 | parser->type_definition_forbidden_message = saved_message; | |
4762 | ||
bbaab916 NS |
4763 | /* If ok so far, parse the dependent expression. We cannot be |
4764 | sure it is a cast. Consider `(T ())'. It is a parenthesized | |
4765 | ctor of T, but looks like a cast to function returning T | |
4766 | without a dependent expression. */ | |
4767 | if (!cp_parser_error_occurred (parser)) | |
d6b4ea85 | 4768 | expr = cp_parser_simple_cast_expression (parser); |
bbaab916 | 4769 | |
a723baf1 MM |
4770 | if (cp_parser_parse_definitely (parser)) |
4771 | { | |
a723baf1 MM |
4772 | /* Warn about old-style casts, if so requested. */ |
4773 | if (warn_old_style_cast | |
4774 | && !in_system_header | |
4775 | && !VOID_TYPE_P (type) | |
4776 | && current_lang_name != lang_name_c) | |
4777 | warning ("use of old-style cast"); | |
14d22dd6 MM |
4778 | |
4779 | /* Only type conversions to integral or enumeration types | |
4780 | can be used in constant-expressions. */ | |
67c03833 | 4781 | if (parser->integral_constant_expression_p |
14d22dd6 MM |
4782 | && !dependent_type_p (type) |
4783 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)) | |
4784 | { | |
67c03833 JM |
4785 | if (!parser->allow_non_integral_constant_expression_p) |
4786 | return (cp_parser_non_integral_constant_expression | |
14d22dd6 MM |
4787 | ("a casts to a type other than an integral or " |
4788 | "enumeration type")); | |
67c03833 | 4789 | parser->non_integral_constant_expression_p = true; |
14d22dd6 | 4790 | } |
a723baf1 MM |
4791 | /* Perform the cast. */ |
4792 | expr = build_c_cast (type, expr); | |
bbaab916 | 4793 | return expr; |
a723baf1 | 4794 | } |
a723baf1 MM |
4795 | } |
4796 | ||
4797 | /* If we get here, then it's not a cast, so it must be a | |
4798 | unary-expression. */ | |
4799 | return cp_parser_unary_expression (parser, address_p); | |
4800 | } | |
4801 | ||
4802 | /* Parse a pm-expression. | |
4803 | ||
4804 | pm-expression: | |
4805 | cast-expression | |
4806 | pm-expression .* cast-expression | |
4807 | pm-expression ->* cast-expression | |
4808 | ||
4809 | Returns a representation of the expression. */ | |
4810 | ||
4811 | static tree | |
94edc4ab | 4812 | cp_parser_pm_expression (cp_parser* parser) |
a723baf1 | 4813 | { |
d6b4ea85 MM |
4814 | static const cp_parser_token_tree_map map = { |
4815 | { CPP_DEREF_STAR, MEMBER_REF }, | |
4816 | { CPP_DOT_STAR, DOTSTAR_EXPR }, | |
4817 | { CPP_EOF, ERROR_MARK } | |
4818 | }; | |
a723baf1 | 4819 | |
d6b4ea85 MM |
4820 | return cp_parser_binary_expression (parser, map, |
4821 | cp_parser_simple_cast_expression); | |
a723baf1 MM |
4822 | } |
4823 | ||
4824 | /* Parse a multiplicative-expression. | |
4825 | ||
4826 | mulitplicative-expression: | |
4827 | pm-expression | |
4828 | multiplicative-expression * pm-expression | |
4829 | multiplicative-expression / pm-expression | |
4830 | multiplicative-expression % pm-expression | |
4831 | ||
4832 | Returns a representation of the expression. */ | |
4833 | ||
4834 | static tree | |
94edc4ab | 4835 | cp_parser_multiplicative_expression (cp_parser* parser) |
a723baf1 | 4836 | { |
39b1af70 | 4837 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4838 | { CPP_MULT, MULT_EXPR }, |
4839 | { CPP_DIV, TRUNC_DIV_EXPR }, | |
4840 | { CPP_MOD, TRUNC_MOD_EXPR }, | |
4841 | { CPP_EOF, ERROR_MARK } | |
4842 | }; | |
4843 | ||
4844 | return cp_parser_binary_expression (parser, | |
4845 | map, | |
4846 | cp_parser_pm_expression); | |
4847 | } | |
4848 | ||
4849 | /* Parse an additive-expression. | |
4850 | ||
4851 | additive-expression: | |
4852 | multiplicative-expression | |
4853 | additive-expression + multiplicative-expression | |
4854 | additive-expression - multiplicative-expression | |
4855 | ||
4856 | Returns a representation of the expression. */ | |
4857 | ||
4858 | static tree | |
94edc4ab | 4859 | cp_parser_additive_expression (cp_parser* parser) |
a723baf1 | 4860 | { |
39b1af70 | 4861 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4862 | { CPP_PLUS, PLUS_EXPR }, |
4863 | { CPP_MINUS, MINUS_EXPR }, | |
4864 | { CPP_EOF, ERROR_MARK } | |
4865 | }; | |
4866 | ||
4867 | return cp_parser_binary_expression (parser, | |
4868 | map, | |
4869 | cp_parser_multiplicative_expression); | |
4870 | } | |
4871 | ||
4872 | /* Parse a shift-expression. | |
4873 | ||
4874 | shift-expression: | |
4875 | additive-expression | |
4876 | shift-expression << additive-expression | |
4877 | shift-expression >> additive-expression | |
4878 | ||
4879 | Returns a representation of the expression. */ | |
4880 | ||
4881 | static tree | |
94edc4ab | 4882 | cp_parser_shift_expression (cp_parser* parser) |
a723baf1 | 4883 | { |
39b1af70 | 4884 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4885 | { CPP_LSHIFT, LSHIFT_EXPR }, |
4886 | { CPP_RSHIFT, RSHIFT_EXPR }, | |
4887 | { CPP_EOF, ERROR_MARK } | |
4888 | }; | |
4889 | ||
4890 | return cp_parser_binary_expression (parser, | |
4891 | map, | |
4892 | cp_parser_additive_expression); | |
4893 | } | |
4894 | ||
4895 | /* Parse a relational-expression. | |
4896 | ||
4897 | relational-expression: | |
4898 | shift-expression | |
4899 | relational-expression < shift-expression | |
4900 | relational-expression > shift-expression | |
4901 | relational-expression <= shift-expression | |
4902 | relational-expression >= shift-expression | |
4903 | ||
4904 | GNU Extension: | |
4905 | ||
4906 | relational-expression: | |
4907 | relational-expression <? shift-expression | |
4908 | relational-expression >? shift-expression | |
4909 | ||
4910 | Returns a representation of the expression. */ | |
4911 | ||
4912 | static tree | |
94edc4ab | 4913 | cp_parser_relational_expression (cp_parser* parser) |
a723baf1 | 4914 | { |
39b1af70 | 4915 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4916 | { CPP_LESS, LT_EXPR }, |
4917 | { CPP_GREATER, GT_EXPR }, | |
4918 | { CPP_LESS_EQ, LE_EXPR }, | |
4919 | { CPP_GREATER_EQ, GE_EXPR }, | |
4920 | { CPP_MIN, MIN_EXPR }, | |
4921 | { CPP_MAX, MAX_EXPR }, | |
4922 | { CPP_EOF, ERROR_MARK } | |
4923 | }; | |
4924 | ||
4925 | return cp_parser_binary_expression (parser, | |
4926 | map, | |
4927 | cp_parser_shift_expression); | |
4928 | } | |
4929 | ||
4930 | /* Parse an equality-expression. | |
4931 | ||
4932 | equality-expression: | |
4933 | relational-expression | |
4934 | equality-expression == relational-expression | |
4935 | equality-expression != relational-expression | |
4936 | ||
4937 | Returns a representation of the expression. */ | |
4938 | ||
4939 | static tree | |
94edc4ab | 4940 | cp_parser_equality_expression (cp_parser* parser) |
a723baf1 | 4941 | { |
39b1af70 | 4942 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4943 | { CPP_EQ_EQ, EQ_EXPR }, |
4944 | { CPP_NOT_EQ, NE_EXPR }, | |
4945 | { CPP_EOF, ERROR_MARK } | |
4946 | }; | |
4947 | ||
4948 | return cp_parser_binary_expression (parser, | |
4949 | map, | |
4950 | cp_parser_relational_expression); | |
4951 | } | |
4952 | ||
4953 | /* Parse an and-expression. | |
4954 | ||
4955 | and-expression: | |
4956 | equality-expression | |
4957 | and-expression & equality-expression | |
4958 | ||
4959 | Returns a representation of the expression. */ | |
4960 | ||
4961 | static tree | |
94edc4ab | 4962 | cp_parser_and_expression (cp_parser* parser) |
a723baf1 | 4963 | { |
39b1af70 | 4964 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4965 | { CPP_AND, BIT_AND_EXPR }, |
4966 | { CPP_EOF, ERROR_MARK } | |
4967 | }; | |
4968 | ||
4969 | return cp_parser_binary_expression (parser, | |
4970 | map, | |
4971 | cp_parser_equality_expression); | |
4972 | } | |
4973 | ||
4974 | /* Parse an exclusive-or-expression. | |
4975 | ||
4976 | exclusive-or-expression: | |
4977 | and-expression | |
4978 | exclusive-or-expression ^ and-expression | |
4979 | ||
4980 | Returns a representation of the expression. */ | |
4981 | ||
4982 | static tree | |
94edc4ab | 4983 | cp_parser_exclusive_or_expression (cp_parser* parser) |
a723baf1 | 4984 | { |
39b1af70 | 4985 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4986 | { CPP_XOR, BIT_XOR_EXPR }, |
4987 | { CPP_EOF, ERROR_MARK } | |
4988 | }; | |
4989 | ||
4990 | return cp_parser_binary_expression (parser, | |
4991 | map, | |
4992 | cp_parser_and_expression); | |
4993 | } | |
4994 | ||
4995 | ||
4996 | /* Parse an inclusive-or-expression. | |
4997 | ||
4998 | inclusive-or-expression: | |
4999 | exclusive-or-expression | |
5000 | inclusive-or-expression | exclusive-or-expression | |
5001 | ||
5002 | Returns a representation of the expression. */ | |
5003 | ||
5004 | static tree | |
94edc4ab | 5005 | cp_parser_inclusive_or_expression (cp_parser* parser) |
a723baf1 | 5006 | { |
39b1af70 | 5007 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
5008 | { CPP_OR, BIT_IOR_EXPR }, |
5009 | { CPP_EOF, ERROR_MARK } | |
5010 | }; | |
5011 | ||
5012 | return cp_parser_binary_expression (parser, | |
5013 | map, | |
5014 | cp_parser_exclusive_or_expression); | |
5015 | } | |
5016 | ||
5017 | /* Parse a logical-and-expression. | |
5018 | ||
5019 | logical-and-expression: | |
5020 | inclusive-or-expression | |
5021 | logical-and-expression && inclusive-or-expression | |
5022 | ||
5023 | Returns a representation of the expression. */ | |
5024 | ||
5025 | static tree | |
94edc4ab | 5026 | cp_parser_logical_and_expression (cp_parser* parser) |
a723baf1 | 5027 | { |
39b1af70 | 5028 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
5029 | { CPP_AND_AND, TRUTH_ANDIF_EXPR }, |
5030 | { CPP_EOF, ERROR_MARK } | |
5031 | }; | |
5032 | ||
5033 | return cp_parser_binary_expression (parser, | |
5034 | map, | |
5035 | cp_parser_inclusive_or_expression); | |
5036 | } | |
5037 | ||
5038 | /* Parse a logical-or-expression. | |
5039 | ||
5040 | logical-or-expression: | |
34cd5ae7 | 5041 | logical-and-expression |
a723baf1 MM |
5042 | logical-or-expression || logical-and-expression |
5043 | ||
5044 | Returns a representation of the expression. */ | |
5045 | ||
5046 | static tree | |
94edc4ab | 5047 | cp_parser_logical_or_expression (cp_parser* parser) |
a723baf1 | 5048 | { |
39b1af70 | 5049 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
5050 | { CPP_OR_OR, TRUTH_ORIF_EXPR }, |
5051 | { CPP_EOF, ERROR_MARK } | |
5052 | }; | |
5053 | ||
5054 | return cp_parser_binary_expression (parser, | |
5055 | map, | |
5056 | cp_parser_logical_and_expression); | |
5057 | } | |
5058 | ||
a723baf1 MM |
5059 | /* Parse the `? expression : assignment-expression' part of a |
5060 | conditional-expression. The LOGICAL_OR_EXPR is the | |
5061 | logical-or-expression that started the conditional-expression. | |
5062 | Returns a representation of the entire conditional-expression. | |
5063 | ||
39703eb9 | 5064 | This routine is used by cp_parser_assignment_expression. |
a723baf1 MM |
5065 | |
5066 | ? expression : assignment-expression | |
5067 | ||
5068 | GNU Extensions: | |
5069 | ||
5070 | ? : assignment-expression */ | |
5071 | ||
5072 | static tree | |
94edc4ab | 5073 | cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr) |
a723baf1 MM |
5074 | { |
5075 | tree expr; | |
5076 | tree assignment_expr; | |
5077 | ||
5078 | /* Consume the `?' token. */ | |
5079 | cp_lexer_consume_token (parser->lexer); | |
5080 | if (cp_parser_allow_gnu_extensions_p (parser) | |
5081 | && cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
5082 | /* Implicit true clause. */ | |
5083 | expr = NULL_TREE; | |
5084 | else | |
5085 | /* Parse the expression. */ | |
5086 | expr = cp_parser_expression (parser); | |
5087 | ||
5088 | /* The next token should be a `:'. */ | |
5089 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
5090 | /* Parse the assignment-expression. */ | |
5091 | assignment_expr = cp_parser_assignment_expression (parser); | |
5092 | ||
5093 | /* Build the conditional-expression. */ | |
5094 | return build_x_conditional_expr (logical_or_expr, | |
5095 | expr, | |
5096 | assignment_expr); | |
5097 | } | |
5098 | ||
5099 | /* Parse an assignment-expression. | |
5100 | ||
5101 | assignment-expression: | |
5102 | conditional-expression | |
5103 | logical-or-expression assignment-operator assignment_expression | |
5104 | throw-expression | |
5105 | ||
5106 | Returns a representation for the expression. */ | |
5107 | ||
5108 | static tree | |
94edc4ab | 5109 | cp_parser_assignment_expression (cp_parser* parser) |
a723baf1 MM |
5110 | { |
5111 | tree expr; | |
5112 | ||
5113 | /* If the next token is the `throw' keyword, then we're looking at | |
5114 | a throw-expression. */ | |
5115 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW)) | |
5116 | expr = cp_parser_throw_expression (parser); | |
5117 | /* Otherwise, it must be that we are looking at a | |
5118 | logical-or-expression. */ | |
5119 | else | |
5120 | { | |
5121 | /* Parse the logical-or-expression. */ | |
5122 | expr = cp_parser_logical_or_expression (parser); | |
5123 | /* If the next token is a `?' then we're actually looking at a | |
5124 | conditional-expression. */ | |
5125 | if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY)) | |
5126 | return cp_parser_question_colon_clause (parser, expr); | |
5127 | else | |
5128 | { | |
5129 | enum tree_code assignment_operator; | |
5130 | ||
5131 | /* If it's an assignment-operator, we're using the second | |
5132 | production. */ | |
5133 | assignment_operator | |
5134 | = cp_parser_assignment_operator_opt (parser); | |
5135 | if (assignment_operator != ERROR_MARK) | |
5136 | { | |
5137 | tree rhs; | |
5138 | ||
5139 | /* Parse the right-hand side of the assignment. */ | |
5140 | rhs = cp_parser_assignment_expression (parser); | |
14d22dd6 MM |
5141 | /* An assignment may not appear in a |
5142 | constant-expression. */ | |
67c03833 | 5143 | if (parser->integral_constant_expression_p) |
14d22dd6 | 5144 | { |
67c03833 JM |
5145 | if (!parser->allow_non_integral_constant_expression_p) |
5146 | return cp_parser_non_integral_constant_expression ("an assignment"); | |
5147 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 5148 | } |
34cd5ae7 | 5149 | /* Build the assignment expression. */ |
a723baf1 MM |
5150 | expr = build_x_modify_expr (expr, |
5151 | assignment_operator, | |
5152 | rhs); | |
5153 | } | |
5154 | } | |
5155 | } | |
5156 | ||
5157 | return expr; | |
5158 | } | |
5159 | ||
5160 | /* Parse an (optional) assignment-operator. | |
5161 | ||
5162 | assignment-operator: one of | |
5163 | = *= /= %= += -= >>= <<= &= ^= |= | |
5164 | ||
5165 | GNU Extension: | |
5166 | ||
5167 | assignment-operator: one of | |
5168 | <?= >?= | |
5169 | ||
5170 | If the next token is an assignment operator, the corresponding tree | |
5171 | code is returned, and the token is consumed. For example, for | |
5172 | `+=', PLUS_EXPR is returned. For `=' itself, the code returned is | |
5173 | NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%', | |
5174 | TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment | |
5175 | operator, ERROR_MARK is returned. */ | |
5176 | ||
5177 | static enum tree_code | |
94edc4ab | 5178 | cp_parser_assignment_operator_opt (cp_parser* parser) |
a723baf1 MM |
5179 | { |
5180 | enum tree_code op; | |
5181 | cp_token *token; | |
5182 | ||
5183 | /* Peek at the next toen. */ | |
5184 | token = cp_lexer_peek_token (parser->lexer); | |
5185 | ||
5186 | switch (token->type) | |
5187 | { | |
5188 | case CPP_EQ: | |
5189 | op = NOP_EXPR; | |
5190 | break; | |
5191 | ||
5192 | case CPP_MULT_EQ: | |
5193 | op = MULT_EXPR; | |
5194 | break; | |
5195 | ||
5196 | case CPP_DIV_EQ: | |
5197 | op = TRUNC_DIV_EXPR; | |
5198 | break; | |
5199 | ||
5200 | case CPP_MOD_EQ: | |
5201 | op = TRUNC_MOD_EXPR; | |
5202 | break; | |
5203 | ||
5204 | case CPP_PLUS_EQ: | |
5205 | op = PLUS_EXPR; | |
5206 | break; | |
5207 | ||
5208 | case CPP_MINUS_EQ: | |
5209 | op = MINUS_EXPR; | |
5210 | break; | |
5211 | ||
5212 | case CPP_RSHIFT_EQ: | |
5213 | op = RSHIFT_EXPR; | |
5214 | break; | |
5215 | ||
5216 | case CPP_LSHIFT_EQ: | |
5217 | op = LSHIFT_EXPR; | |
5218 | break; | |
5219 | ||
5220 | case CPP_AND_EQ: | |
5221 | op = BIT_AND_EXPR; | |
5222 | break; | |
5223 | ||
5224 | case CPP_XOR_EQ: | |
5225 | op = BIT_XOR_EXPR; | |
5226 | break; | |
5227 | ||
5228 | case CPP_OR_EQ: | |
5229 | op = BIT_IOR_EXPR; | |
5230 | break; | |
5231 | ||
5232 | case CPP_MIN_EQ: | |
5233 | op = MIN_EXPR; | |
5234 | break; | |
5235 | ||
5236 | case CPP_MAX_EQ: | |
5237 | op = MAX_EXPR; | |
5238 | break; | |
5239 | ||
5240 | default: | |
5241 | /* Nothing else is an assignment operator. */ | |
5242 | op = ERROR_MARK; | |
5243 | } | |
5244 | ||
5245 | /* If it was an assignment operator, consume it. */ | |
5246 | if (op != ERROR_MARK) | |
5247 | cp_lexer_consume_token (parser->lexer); | |
5248 | ||
5249 | return op; | |
5250 | } | |
5251 | ||
5252 | /* Parse an expression. | |
5253 | ||
5254 | expression: | |
5255 | assignment-expression | |
5256 | expression , assignment-expression | |
5257 | ||
5258 | Returns a representation of the expression. */ | |
5259 | ||
5260 | static tree | |
94edc4ab | 5261 | cp_parser_expression (cp_parser* parser) |
a723baf1 MM |
5262 | { |
5263 | tree expression = NULL_TREE; | |
a723baf1 MM |
5264 | |
5265 | while (true) | |
5266 | { | |
5267 | tree assignment_expression; | |
5268 | ||
5269 | /* Parse the next assignment-expression. */ | |
5270 | assignment_expression | |
5271 | = cp_parser_assignment_expression (parser); | |
5272 | /* If this is the first assignment-expression, we can just | |
5273 | save it away. */ | |
5274 | if (!expression) | |
5275 | expression = assignment_expression; | |
a723baf1 | 5276 | else |
d17811fd MM |
5277 | expression = build_x_compound_expr (expression, |
5278 | assignment_expression); | |
a723baf1 MM |
5279 | /* If the next token is not a comma, then we are done with the |
5280 | expression. */ | |
5281 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
5282 | break; | |
5283 | /* Consume the `,'. */ | |
5284 | cp_lexer_consume_token (parser->lexer); | |
14d22dd6 | 5285 | /* A comma operator cannot appear in a constant-expression. */ |
67c03833 | 5286 | if (parser->integral_constant_expression_p) |
14d22dd6 | 5287 | { |
67c03833 | 5288 | if (!parser->allow_non_integral_constant_expression_p) |
d17811fd | 5289 | expression |
67c03833 JM |
5290 | = cp_parser_non_integral_constant_expression ("a comma operator"); |
5291 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 5292 | } |
14d22dd6 | 5293 | } |
a723baf1 MM |
5294 | |
5295 | return expression; | |
5296 | } | |
5297 | ||
5298 | /* Parse a constant-expression. | |
5299 | ||
5300 | constant-expression: | |
14d22dd6 MM |
5301 | conditional-expression |
5302 | ||
5303 | If ALLOW_NON_CONSTANT_P a non-constant expression is silently | |
d17811fd MM |
5304 | accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not |
5305 | constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P | |
5306 | is false, NON_CONSTANT_P should be NULL. */ | |
a723baf1 MM |
5307 | |
5308 | static tree | |
14d22dd6 MM |
5309 | cp_parser_constant_expression (cp_parser* parser, |
5310 | bool allow_non_constant_p, | |
5311 | bool *non_constant_p) | |
a723baf1 | 5312 | { |
67c03833 JM |
5313 | bool saved_integral_constant_expression_p; |
5314 | bool saved_allow_non_integral_constant_expression_p; | |
5315 | bool saved_non_integral_constant_expression_p; | |
a723baf1 MM |
5316 | tree expression; |
5317 | ||
5318 | /* It might seem that we could simply parse the | |
5319 | conditional-expression, and then check to see if it were | |
5320 | TREE_CONSTANT. However, an expression that is TREE_CONSTANT is | |
5321 | one that the compiler can figure out is constant, possibly after | |
5322 | doing some simplifications or optimizations. The standard has a | |
5323 | precise definition of constant-expression, and we must honor | |
5324 | that, even though it is somewhat more restrictive. | |
5325 | ||
5326 | For example: | |
5327 | ||
5328 | int i[(2, 3)]; | |
5329 | ||
5330 | is not a legal declaration, because `(2, 3)' is not a | |
5331 | constant-expression. The `,' operator is forbidden in a | |
5332 | constant-expression. However, GCC's constant-folding machinery | |
5333 | will fold this operation to an INTEGER_CST for `3'. */ | |
5334 | ||
14d22dd6 | 5335 | /* Save the old settings. */ |
67c03833 JM |
5336 | saved_integral_constant_expression_p = parser->integral_constant_expression_p; |
5337 | saved_allow_non_integral_constant_expression_p | |
5338 | = parser->allow_non_integral_constant_expression_p; | |
5339 | saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p; | |
a723baf1 | 5340 | /* We are now parsing a constant-expression. */ |
67c03833 JM |
5341 | parser->integral_constant_expression_p = true; |
5342 | parser->allow_non_integral_constant_expression_p = allow_non_constant_p; | |
5343 | parser->non_integral_constant_expression_p = false; | |
39703eb9 MM |
5344 | /* Although the grammar says "conditional-expression", we parse an |
5345 | "assignment-expression", which also permits "throw-expression" | |
5346 | and the use of assignment operators. In the case that | |
5347 | ALLOW_NON_CONSTANT_P is false, we get better errors than we would | |
5348 | otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is | |
5349 | actually essential that we look for an assignment-expression. | |
5350 | For example, cp_parser_initializer_clauses uses this function to | |
5351 | determine whether a particular assignment-expression is in fact | |
5352 | constant. */ | |
5353 | expression = cp_parser_assignment_expression (parser); | |
14d22dd6 | 5354 | /* Restore the old settings. */ |
67c03833 JM |
5355 | parser->integral_constant_expression_p = saved_integral_constant_expression_p; |
5356 | parser->allow_non_integral_constant_expression_p | |
5357 | = saved_allow_non_integral_constant_expression_p; | |
14d22dd6 | 5358 | if (allow_non_constant_p) |
67c03833 JM |
5359 | *non_constant_p = parser->non_integral_constant_expression_p; |
5360 | parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p; | |
a723baf1 MM |
5361 | |
5362 | return expression; | |
5363 | } | |
5364 | ||
5365 | /* Statements [gram.stmt.stmt] */ | |
5366 | ||
5367 | /* Parse a statement. | |
5368 | ||
5369 | statement: | |
5370 | labeled-statement | |
5371 | expression-statement | |
5372 | compound-statement | |
5373 | selection-statement | |
5374 | iteration-statement | |
5375 | jump-statement | |
5376 | declaration-statement | |
5377 | try-block */ | |
5378 | ||
5379 | static void | |
a5bcc582 | 5380 | cp_parser_statement (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 MM |
5381 | { |
5382 | tree statement; | |
5383 | cp_token *token; | |
5384 | int statement_line_number; | |
5385 | ||
5386 | /* There is no statement yet. */ | |
5387 | statement = NULL_TREE; | |
5388 | /* Peek at the next token. */ | |
5389 | token = cp_lexer_peek_token (parser->lexer); | |
5390 | /* Remember the line number of the first token in the statement. */ | |
82a98427 | 5391 | statement_line_number = token->location.line; |
a723baf1 MM |
5392 | /* If this is a keyword, then that will often determine what kind of |
5393 | statement we have. */ | |
5394 | if (token->type == CPP_KEYWORD) | |
5395 | { | |
5396 | enum rid keyword = token->keyword; | |
5397 | ||
5398 | switch (keyword) | |
5399 | { | |
5400 | case RID_CASE: | |
5401 | case RID_DEFAULT: | |
a5bcc582 NS |
5402 | statement = cp_parser_labeled_statement (parser, |
5403 | in_statement_expr_p); | |
a723baf1 MM |
5404 | break; |
5405 | ||
5406 | case RID_IF: | |
5407 | case RID_SWITCH: | |
5408 | statement = cp_parser_selection_statement (parser); | |
5409 | break; | |
5410 | ||
5411 | case RID_WHILE: | |
5412 | case RID_DO: | |
5413 | case RID_FOR: | |
5414 | statement = cp_parser_iteration_statement (parser); | |
5415 | break; | |
5416 | ||
5417 | case RID_BREAK: | |
5418 | case RID_CONTINUE: | |
5419 | case RID_RETURN: | |
5420 | case RID_GOTO: | |
5421 | statement = cp_parser_jump_statement (parser); | |
5422 | break; | |
5423 | ||
5424 | case RID_TRY: | |
5425 | statement = cp_parser_try_block (parser); | |
5426 | break; | |
5427 | ||
5428 | default: | |
5429 | /* It might be a keyword like `int' that can start a | |
5430 | declaration-statement. */ | |
5431 | break; | |
5432 | } | |
5433 | } | |
5434 | else if (token->type == CPP_NAME) | |
5435 | { | |
5436 | /* If the next token is a `:', then we are looking at a | |
5437 | labeled-statement. */ | |
5438 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
5439 | if (token->type == CPP_COLON) | |
a5bcc582 | 5440 | statement = cp_parser_labeled_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5441 | } |
5442 | /* Anything that starts with a `{' must be a compound-statement. */ | |
5443 | else if (token->type == CPP_OPEN_BRACE) | |
a5bcc582 | 5444 | statement = cp_parser_compound_statement (parser, false); |
a723baf1 MM |
5445 | |
5446 | /* Everything else must be a declaration-statement or an | |
5447 | expression-statement. Try for the declaration-statement | |
5448 | first, unless we are looking at a `;', in which case we know that | |
5449 | we have an expression-statement. */ | |
5450 | if (!statement) | |
5451 | { | |
5452 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5453 | { | |
5454 | cp_parser_parse_tentatively (parser); | |
5455 | /* Try to parse the declaration-statement. */ | |
5456 | cp_parser_declaration_statement (parser); | |
5457 | /* If that worked, we're done. */ | |
5458 | if (cp_parser_parse_definitely (parser)) | |
5459 | return; | |
5460 | } | |
5461 | /* Look for an expression-statement instead. */ | |
a5bcc582 | 5462 | statement = cp_parser_expression_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5463 | } |
5464 | ||
5465 | /* Set the line number for the statement. */ | |
009ed910 | 5466 | if (statement && STATEMENT_CODE_P (TREE_CODE (statement))) |
a723baf1 MM |
5467 | STMT_LINENO (statement) = statement_line_number; |
5468 | } | |
5469 | ||
5470 | /* Parse a labeled-statement. | |
5471 | ||
5472 | labeled-statement: | |
5473 | identifier : statement | |
5474 | case constant-expression : statement | |
98ce043b MM |
5475 | default : statement |
5476 | ||
5477 | GNU Extension: | |
5478 | ||
5479 | labeled-statement: | |
5480 | case constant-expression ... constant-expression : statement | |
a723baf1 MM |
5481 | |
5482 | Returns the new CASE_LABEL, for a `case' or `default' label. For | |
5483 | an ordinary label, returns a LABEL_STMT. */ | |
5484 | ||
5485 | static tree | |
a5bcc582 | 5486 | cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 MM |
5487 | { |
5488 | cp_token *token; | |
0e59b3fb | 5489 | tree statement = error_mark_node; |
a723baf1 MM |
5490 | |
5491 | /* The next token should be an identifier. */ | |
5492 | token = cp_lexer_peek_token (parser->lexer); | |
5493 | if (token->type != CPP_NAME | |
5494 | && token->type != CPP_KEYWORD) | |
5495 | { | |
5496 | cp_parser_error (parser, "expected labeled-statement"); | |
5497 | return error_mark_node; | |
5498 | } | |
5499 | ||
5500 | switch (token->keyword) | |
5501 | { | |
5502 | case RID_CASE: | |
5503 | { | |
98ce043b MM |
5504 | tree expr, expr_hi; |
5505 | cp_token *ellipsis; | |
a723baf1 MM |
5506 | |
5507 | /* Consume the `case' token. */ | |
5508 | cp_lexer_consume_token (parser->lexer); | |
5509 | /* Parse the constant-expression. */ | |
14d22dd6 | 5510 | expr = cp_parser_constant_expression (parser, |
d17811fd | 5511 | /*allow_non_constant_p=*/false, |
14d22dd6 | 5512 | NULL); |
98ce043b MM |
5513 | |
5514 | ellipsis = cp_lexer_peek_token (parser->lexer); | |
5515 | if (ellipsis->type == CPP_ELLIPSIS) | |
5516 | { | |
5517 | /* Consume the `...' token. */ | |
5518 | cp_lexer_consume_token (parser->lexer); | |
5519 | expr_hi = | |
5520 | cp_parser_constant_expression (parser, | |
5521 | /*allow_non_constant_p=*/false, | |
5522 | NULL); | |
5523 | /* We don't need to emit warnings here, as the common code | |
5524 | will do this for us. */ | |
5525 | } | |
5526 | else | |
5527 | expr_hi = NULL_TREE; | |
5528 | ||
0e59b3fb MM |
5529 | if (!parser->in_switch_statement_p) |
5530 | error ("case label `%E' not within a switch statement", expr); | |
5531 | else | |
98ce043b | 5532 | statement = finish_case_label (expr, expr_hi); |
a723baf1 MM |
5533 | } |
5534 | break; | |
5535 | ||
5536 | case RID_DEFAULT: | |
5537 | /* Consume the `default' token. */ | |
5538 | cp_lexer_consume_token (parser->lexer); | |
0e59b3fb MM |
5539 | if (!parser->in_switch_statement_p) |
5540 | error ("case label not within a switch statement"); | |
5541 | else | |
5542 | statement = finish_case_label (NULL_TREE, NULL_TREE); | |
a723baf1 MM |
5543 | break; |
5544 | ||
5545 | default: | |
5546 | /* Anything else must be an ordinary label. */ | |
5547 | statement = finish_label_stmt (cp_parser_identifier (parser)); | |
5548 | break; | |
5549 | } | |
5550 | ||
5551 | /* Require the `:' token. */ | |
5552 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
5553 | /* Parse the labeled statement. */ | |
a5bcc582 | 5554 | cp_parser_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5555 | |
5556 | /* Return the label, in the case of a `case' or `default' label. */ | |
5557 | return statement; | |
5558 | } | |
5559 | ||
5560 | /* Parse an expression-statement. | |
5561 | ||
5562 | expression-statement: | |
5563 | expression [opt] ; | |
5564 | ||
5565 | Returns the new EXPR_STMT -- or NULL_TREE if the expression | |
a5bcc582 NS |
5566 | statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P |
5567 | indicates whether this expression-statement is part of an | |
5568 | expression statement. */ | |
a723baf1 MM |
5569 | |
5570 | static tree | |
a5bcc582 | 5571 | cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 | 5572 | { |
a5bcc582 | 5573 | tree statement = NULL_TREE; |
a723baf1 | 5574 | |
a5bcc582 | 5575 | /* If the next token is a ';', then there is no expression |
04c06002 | 5576 | statement. */ |
a723baf1 | 5577 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) |
a5bcc582 NS |
5578 | statement = cp_parser_expression (parser); |
5579 | ||
a723baf1 | 5580 | /* Consume the final `;'. */ |
e0860732 | 5581 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 | 5582 | |
a5bcc582 NS |
5583 | if (in_statement_expr_p |
5584 | && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
5585 | { | |
5586 | /* This is the final expression statement of a statement | |
5587 | expression. */ | |
5588 | statement = finish_stmt_expr_expr (statement); | |
5589 | } | |
5590 | else if (statement) | |
5591 | statement = finish_expr_stmt (statement); | |
5592 | else | |
5593 | finish_stmt (); | |
5594 | ||
a723baf1 MM |
5595 | return statement; |
5596 | } | |
5597 | ||
5598 | /* Parse a compound-statement. | |
5599 | ||
5600 | compound-statement: | |
5601 | { statement-seq [opt] } | |
5602 | ||
5603 | Returns a COMPOUND_STMT representing the statement. */ | |
5604 | ||
5605 | static tree | |
a5bcc582 | 5606 | cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p) |
a723baf1 MM |
5607 | { |
5608 | tree compound_stmt; | |
5609 | ||
5610 | /* Consume the `{'. */ | |
5611 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
5612 | return error_mark_node; | |
5613 | /* Begin the compound-statement. */ | |
7a3397c7 | 5614 | compound_stmt = begin_compound_stmt (/*has_no_scope=*/false); |
a723baf1 | 5615 | /* Parse an (optional) statement-seq. */ |
a5bcc582 | 5616 | cp_parser_statement_seq_opt (parser, in_statement_expr_p); |
a723baf1 | 5617 | /* Finish the compound-statement. */ |
7a3397c7 | 5618 | finish_compound_stmt (compound_stmt); |
a723baf1 MM |
5619 | /* Consume the `}'. */ |
5620 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
5621 | ||
5622 | return compound_stmt; | |
5623 | } | |
5624 | ||
5625 | /* Parse an (optional) statement-seq. | |
5626 | ||
5627 | statement-seq: | |
5628 | statement | |
5629 | statement-seq [opt] statement */ | |
5630 | ||
5631 | static void | |
a5bcc582 | 5632 | cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 MM |
5633 | { |
5634 | /* Scan statements until there aren't any more. */ | |
5635 | while (true) | |
5636 | { | |
5637 | /* If we're looking at a `}', then we've run out of statements. */ | |
5638 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE) | |
5639 | || cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
5640 | break; | |
5641 | ||
5642 | /* Parse the statement. */ | |
a5bcc582 | 5643 | cp_parser_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5644 | } |
5645 | } | |
5646 | ||
5647 | /* Parse a selection-statement. | |
5648 | ||
5649 | selection-statement: | |
5650 | if ( condition ) statement | |
5651 | if ( condition ) statement else statement | |
5652 | switch ( condition ) statement | |
5653 | ||
5654 | Returns the new IF_STMT or SWITCH_STMT. */ | |
5655 | ||
5656 | static tree | |
94edc4ab | 5657 | cp_parser_selection_statement (cp_parser* parser) |
a723baf1 MM |
5658 | { |
5659 | cp_token *token; | |
5660 | enum rid keyword; | |
5661 | ||
5662 | /* Peek at the next token. */ | |
5663 | token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement"); | |
5664 | ||
5665 | /* See what kind of keyword it is. */ | |
5666 | keyword = token->keyword; | |
5667 | switch (keyword) | |
5668 | { | |
5669 | case RID_IF: | |
5670 | case RID_SWITCH: | |
5671 | { | |
5672 | tree statement; | |
5673 | tree condition; | |
5674 | ||
5675 | /* Look for the `('. */ | |
5676 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) | |
5677 | { | |
5678 | cp_parser_skip_to_end_of_statement (parser); | |
5679 | return error_mark_node; | |
5680 | } | |
5681 | ||
5682 | /* Begin the selection-statement. */ | |
5683 | if (keyword == RID_IF) | |
5684 | statement = begin_if_stmt (); | |
5685 | else | |
5686 | statement = begin_switch_stmt (); | |
5687 | ||
5688 | /* Parse the condition. */ | |
5689 | condition = cp_parser_condition (parser); | |
5690 | /* Look for the `)'. */ | |
5691 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
a668c6ad MM |
5692 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
5693 | /*consume_paren=*/true); | |
a723baf1 MM |
5694 | |
5695 | if (keyword == RID_IF) | |
5696 | { | |
5697 | tree then_stmt; | |
5698 | ||
5699 | /* Add the condition. */ | |
5700 | finish_if_stmt_cond (condition, statement); | |
5701 | ||
5702 | /* Parse the then-clause. */ | |
5703 | then_stmt = cp_parser_implicitly_scoped_statement (parser); | |
5704 | finish_then_clause (statement); | |
5705 | ||
5706 | /* If the next token is `else', parse the else-clause. */ | |
5707 | if (cp_lexer_next_token_is_keyword (parser->lexer, | |
5708 | RID_ELSE)) | |
5709 | { | |
5710 | tree else_stmt; | |
5711 | ||
5712 | /* Consume the `else' keyword. */ | |
5713 | cp_lexer_consume_token (parser->lexer); | |
5714 | /* Parse the else-clause. */ | |
5715 | else_stmt | |
5716 | = cp_parser_implicitly_scoped_statement (parser); | |
5717 | finish_else_clause (statement); | |
5718 | } | |
5719 | ||
5720 | /* Now we're all done with the if-statement. */ | |
5721 | finish_if_stmt (); | |
5722 | } | |
5723 | else | |
5724 | { | |
5725 | tree body; | |
0e59b3fb | 5726 | bool in_switch_statement_p; |
a723baf1 MM |
5727 | |
5728 | /* Add the condition. */ | |
5729 | finish_switch_cond (condition, statement); | |
5730 | ||
5731 | /* Parse the body of the switch-statement. */ | |
0e59b3fb MM |
5732 | in_switch_statement_p = parser->in_switch_statement_p; |
5733 | parser->in_switch_statement_p = true; | |
a723baf1 | 5734 | body = cp_parser_implicitly_scoped_statement (parser); |
0e59b3fb | 5735 | parser->in_switch_statement_p = in_switch_statement_p; |
a723baf1 MM |
5736 | |
5737 | /* Now we're all done with the switch-statement. */ | |
5738 | finish_switch_stmt (statement); | |
5739 | } | |
5740 | ||
5741 | return statement; | |
5742 | } | |
5743 | break; | |
5744 | ||
5745 | default: | |
5746 | cp_parser_error (parser, "expected selection-statement"); | |
5747 | return error_mark_node; | |
5748 | } | |
5749 | } | |
5750 | ||
5751 | /* Parse a condition. | |
5752 | ||
5753 | condition: | |
5754 | expression | |
5755 | type-specifier-seq declarator = assignment-expression | |
5756 | ||
5757 | GNU Extension: | |
5758 | ||
5759 | condition: | |
5760 | type-specifier-seq declarator asm-specification [opt] | |
5761 | attributes [opt] = assignment-expression | |
5762 | ||
5763 | Returns the expression that should be tested. */ | |
5764 | ||
5765 | static tree | |
94edc4ab | 5766 | cp_parser_condition (cp_parser* parser) |
a723baf1 MM |
5767 | { |
5768 | tree type_specifiers; | |
5769 | const char *saved_message; | |
5770 | ||
5771 | /* Try the declaration first. */ | |
5772 | cp_parser_parse_tentatively (parser); | |
5773 | /* New types are not allowed in the type-specifier-seq for a | |
5774 | condition. */ | |
5775 | saved_message = parser->type_definition_forbidden_message; | |
5776 | parser->type_definition_forbidden_message | |
5777 | = "types may not be defined in conditions"; | |
5778 | /* Parse the type-specifier-seq. */ | |
5779 | type_specifiers = cp_parser_type_specifier_seq (parser); | |
5780 | /* Restore the saved message. */ | |
5781 | parser->type_definition_forbidden_message = saved_message; | |
5782 | /* If all is well, we might be looking at a declaration. */ | |
5783 | if (!cp_parser_error_occurred (parser)) | |
5784 | { | |
5785 | tree decl; | |
5786 | tree asm_specification; | |
5787 | tree attributes; | |
5788 | tree declarator; | |
5789 | tree initializer = NULL_TREE; | |
5790 | ||
5791 | /* Parse the declarator. */ | |
62b8a44e | 5792 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
5793 | /*ctor_dtor_or_conv_p=*/NULL, |
5794 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
5795 | /* Parse the attributes. */ |
5796 | attributes = cp_parser_attributes_opt (parser); | |
5797 | /* Parse the asm-specification. */ | |
5798 | asm_specification = cp_parser_asm_specification_opt (parser); | |
5799 | /* If the next token is not an `=', then we might still be | |
5800 | looking at an expression. For example: | |
5801 | ||
5802 | if (A(a).x) | |
5803 | ||
5804 | looks like a decl-specifier-seq and a declarator -- but then | |
5805 | there is no `=', so this is an expression. */ | |
5806 | cp_parser_require (parser, CPP_EQ, "`='"); | |
5807 | /* If we did see an `=', then we are looking at a declaration | |
5808 | for sure. */ | |
5809 | if (cp_parser_parse_definitely (parser)) | |
5810 | { | |
5811 | /* Create the declaration. */ | |
5812 | decl = start_decl (declarator, type_specifiers, | |
5813 | /*initialized_p=*/true, | |
5814 | attributes, /*prefix_attributes=*/NULL_TREE); | |
5815 | /* Parse the assignment-expression. */ | |
5816 | initializer = cp_parser_assignment_expression (parser); | |
5817 | ||
5818 | /* Process the initializer. */ | |
5819 | cp_finish_decl (decl, | |
5820 | initializer, | |
5821 | asm_specification, | |
5822 | LOOKUP_ONLYCONVERTING); | |
5823 | ||
5824 | return convert_from_reference (decl); | |
5825 | } | |
5826 | } | |
5827 | /* If we didn't even get past the declarator successfully, we are | |
5828 | definitely not looking at a declaration. */ | |
5829 | else | |
5830 | cp_parser_abort_tentative_parse (parser); | |
5831 | ||
5832 | /* Otherwise, we are looking at an expression. */ | |
5833 | return cp_parser_expression (parser); | |
5834 | } | |
5835 | ||
5836 | /* Parse an iteration-statement. | |
5837 | ||
5838 | iteration-statement: | |
5839 | while ( condition ) statement | |
5840 | do statement while ( expression ) ; | |
5841 | for ( for-init-statement condition [opt] ; expression [opt] ) | |
5842 | statement | |
5843 | ||
5844 | Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */ | |
5845 | ||
5846 | static tree | |
94edc4ab | 5847 | cp_parser_iteration_statement (cp_parser* parser) |
a723baf1 MM |
5848 | { |
5849 | cp_token *token; | |
5850 | enum rid keyword; | |
5851 | tree statement; | |
0e59b3fb MM |
5852 | bool in_iteration_statement_p; |
5853 | ||
a723baf1 MM |
5854 | |
5855 | /* Peek at the next token. */ | |
5856 | token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement"); | |
5857 | if (!token) | |
5858 | return error_mark_node; | |
5859 | ||
0e59b3fb MM |
5860 | /* Remember whether or not we are already within an iteration |
5861 | statement. */ | |
5862 | in_iteration_statement_p = parser->in_iteration_statement_p; | |
5863 | ||
a723baf1 MM |
5864 | /* See what kind of keyword it is. */ |
5865 | keyword = token->keyword; | |
5866 | switch (keyword) | |
5867 | { | |
5868 | case RID_WHILE: | |
5869 | { | |
5870 | tree condition; | |
5871 | ||
5872 | /* Begin the while-statement. */ | |
5873 | statement = begin_while_stmt (); | |
5874 | /* Look for the `('. */ | |
5875 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
5876 | /* Parse the condition. */ | |
5877 | condition = cp_parser_condition (parser); | |
5878 | finish_while_stmt_cond (condition, statement); | |
5879 | /* Look for the `)'. */ | |
5880 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
5881 | /* Parse the dependent statement. */ | |
0e59b3fb | 5882 | parser->in_iteration_statement_p = true; |
a723baf1 | 5883 | cp_parser_already_scoped_statement (parser); |
0e59b3fb | 5884 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
5885 | /* We're done with the while-statement. */ |
5886 | finish_while_stmt (statement); | |
5887 | } | |
5888 | break; | |
5889 | ||
5890 | case RID_DO: | |
5891 | { | |
5892 | tree expression; | |
5893 | ||
5894 | /* Begin the do-statement. */ | |
5895 | statement = begin_do_stmt (); | |
5896 | /* Parse the body of the do-statement. */ | |
0e59b3fb | 5897 | parser->in_iteration_statement_p = true; |
a723baf1 | 5898 | cp_parser_implicitly_scoped_statement (parser); |
0e59b3fb | 5899 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
5900 | finish_do_body (statement); |
5901 | /* Look for the `while' keyword. */ | |
5902 | cp_parser_require_keyword (parser, RID_WHILE, "`while'"); | |
5903 | /* Look for the `('. */ | |
5904 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
5905 | /* Parse the expression. */ | |
5906 | expression = cp_parser_expression (parser); | |
5907 | /* We're done with the do-statement. */ | |
5908 | finish_do_stmt (expression, statement); | |
5909 | /* Look for the `)'. */ | |
5910 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
5911 | /* Look for the `;'. */ | |
5912 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
5913 | } | |
5914 | break; | |
5915 | ||
5916 | case RID_FOR: | |
5917 | { | |
5918 | tree condition = NULL_TREE; | |
5919 | tree expression = NULL_TREE; | |
5920 | ||
5921 | /* Begin the for-statement. */ | |
5922 | statement = begin_for_stmt (); | |
5923 | /* Look for the `('. */ | |
5924 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
5925 | /* Parse the initialization. */ | |
5926 | cp_parser_for_init_statement (parser); | |
5927 | finish_for_init_stmt (statement); | |
5928 | ||
5929 | /* If there's a condition, process it. */ | |
5930 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5931 | condition = cp_parser_condition (parser); | |
5932 | finish_for_cond (condition, statement); | |
5933 | /* Look for the `;'. */ | |
5934 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
5935 | ||
5936 | /* If there's an expression, process it. */ | |
5937 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) | |
5938 | expression = cp_parser_expression (parser); | |
5939 | finish_for_expr (expression, statement); | |
5940 | /* Look for the `)'. */ | |
5941 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'"); | |
5942 | ||
5943 | /* Parse the body of the for-statement. */ | |
0e59b3fb | 5944 | parser->in_iteration_statement_p = true; |
a723baf1 | 5945 | cp_parser_already_scoped_statement (parser); |
0e59b3fb | 5946 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
5947 | |
5948 | /* We're done with the for-statement. */ | |
5949 | finish_for_stmt (statement); | |
5950 | } | |
5951 | break; | |
5952 | ||
5953 | default: | |
5954 | cp_parser_error (parser, "expected iteration-statement"); | |
5955 | statement = error_mark_node; | |
5956 | break; | |
5957 | } | |
5958 | ||
5959 | return statement; | |
5960 | } | |
5961 | ||
5962 | /* Parse a for-init-statement. | |
5963 | ||
5964 | for-init-statement: | |
5965 | expression-statement | |
5966 | simple-declaration */ | |
5967 | ||
5968 | static void | |
94edc4ab | 5969 | cp_parser_for_init_statement (cp_parser* parser) |
a723baf1 MM |
5970 | { |
5971 | /* If the next token is a `;', then we have an empty | |
34cd5ae7 | 5972 | expression-statement. Grammatically, this is also a |
a723baf1 MM |
5973 | simple-declaration, but an invalid one, because it does not |
5974 | declare anything. Therefore, if we did not handle this case | |
5975 | specially, we would issue an error message about an invalid | |
5976 | declaration. */ | |
5977 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5978 | { | |
5979 | /* We're going to speculatively look for a declaration, falling back | |
5980 | to an expression, if necessary. */ | |
5981 | cp_parser_parse_tentatively (parser); | |
5982 | /* Parse the declaration. */ | |
5983 | cp_parser_simple_declaration (parser, | |
5984 | /*function_definition_allowed_p=*/false); | |
5985 | /* If the tentative parse failed, then we shall need to look for an | |
5986 | expression-statement. */ | |
5987 | if (cp_parser_parse_definitely (parser)) | |
5988 | return; | |
5989 | } | |
5990 | ||
a5bcc582 | 5991 | cp_parser_expression_statement (parser, false); |
a723baf1 MM |
5992 | } |
5993 | ||
5994 | /* Parse a jump-statement. | |
5995 | ||
5996 | jump-statement: | |
5997 | break ; | |
5998 | continue ; | |
5999 | return expression [opt] ; | |
6000 | goto identifier ; | |
6001 | ||
6002 | GNU extension: | |
6003 | ||
6004 | jump-statement: | |
6005 | goto * expression ; | |
6006 | ||
6007 | Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or | |
6008 | GOTO_STMT. */ | |
6009 | ||
6010 | static tree | |
94edc4ab | 6011 | cp_parser_jump_statement (cp_parser* parser) |
a723baf1 MM |
6012 | { |
6013 | tree statement = error_mark_node; | |
6014 | cp_token *token; | |
6015 | enum rid keyword; | |
6016 | ||
6017 | /* Peek at the next token. */ | |
6018 | token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement"); | |
6019 | if (!token) | |
6020 | return error_mark_node; | |
6021 | ||
6022 | /* See what kind of keyword it is. */ | |
6023 | keyword = token->keyword; | |
6024 | switch (keyword) | |
6025 | { | |
6026 | case RID_BREAK: | |
0e59b3fb MM |
6027 | if (!parser->in_switch_statement_p |
6028 | && !parser->in_iteration_statement_p) | |
6029 | { | |
6030 | error ("break statement not within loop or switch"); | |
6031 | statement = error_mark_node; | |
6032 | } | |
6033 | else | |
6034 | statement = finish_break_stmt (); | |
a723baf1 MM |
6035 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
6036 | break; | |
6037 | ||
6038 | case RID_CONTINUE: | |
0e59b3fb MM |
6039 | if (!parser->in_iteration_statement_p) |
6040 | { | |
6041 | error ("continue statement not within a loop"); | |
6042 | statement = error_mark_node; | |
6043 | } | |
6044 | else | |
6045 | statement = finish_continue_stmt (); | |
a723baf1 MM |
6046 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
6047 | break; | |
6048 | ||
6049 | case RID_RETURN: | |
6050 | { | |
6051 | tree expr; | |
6052 | ||
6053 | /* If the next token is a `;', then there is no | |
6054 | expression. */ | |
6055 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
6056 | expr = cp_parser_expression (parser); | |
6057 | else | |
6058 | expr = NULL_TREE; | |
6059 | /* Build the return-statement. */ | |
6060 | statement = finish_return_stmt (expr); | |
6061 | /* Look for the final `;'. */ | |
6062 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6063 | } | |
6064 | break; | |
6065 | ||
6066 | case RID_GOTO: | |
6067 | /* Create the goto-statement. */ | |
6068 | if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)) | |
6069 | { | |
6070 | /* Issue a warning about this use of a GNU extension. */ | |
6071 | if (pedantic) | |
6072 | pedwarn ("ISO C++ forbids computed gotos"); | |
6073 | /* Consume the '*' token. */ | |
6074 | cp_lexer_consume_token (parser->lexer); | |
6075 | /* Parse the dependent expression. */ | |
6076 | finish_goto_stmt (cp_parser_expression (parser)); | |
6077 | } | |
6078 | else | |
6079 | finish_goto_stmt (cp_parser_identifier (parser)); | |
6080 | /* Look for the final `;'. */ | |
6081 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6082 | break; | |
6083 | ||
6084 | default: | |
6085 | cp_parser_error (parser, "expected jump-statement"); | |
6086 | break; | |
6087 | } | |
6088 | ||
6089 | return statement; | |
6090 | } | |
6091 | ||
6092 | /* Parse a declaration-statement. | |
6093 | ||
6094 | declaration-statement: | |
6095 | block-declaration */ | |
6096 | ||
6097 | static void | |
94edc4ab | 6098 | cp_parser_declaration_statement (cp_parser* parser) |
a723baf1 MM |
6099 | { |
6100 | /* Parse the block-declaration. */ | |
6101 | cp_parser_block_declaration (parser, /*statement_p=*/true); | |
6102 | ||
6103 | /* Finish off the statement. */ | |
6104 | finish_stmt (); | |
6105 | } | |
6106 | ||
6107 | /* Some dependent statements (like `if (cond) statement'), are | |
6108 | implicitly in their own scope. In other words, if the statement is | |
6109 | a single statement (as opposed to a compound-statement), it is | |
6110 | none-the-less treated as if it were enclosed in braces. Any | |
6111 | declarations appearing in the dependent statement are out of scope | |
6112 | after control passes that point. This function parses a statement, | |
6113 | but ensures that is in its own scope, even if it is not a | |
6114 | compound-statement. | |
6115 | ||
6116 | Returns the new statement. */ | |
6117 | ||
6118 | static tree | |
94edc4ab | 6119 | cp_parser_implicitly_scoped_statement (cp_parser* parser) |
a723baf1 MM |
6120 | { |
6121 | tree statement; | |
6122 | ||
6123 | /* If the token is not a `{', then we must take special action. */ | |
6124 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
6125 | { | |
6126 | /* Create a compound-statement. */ | |
7a3397c7 | 6127 | statement = begin_compound_stmt (/*has_no_scope=*/false); |
a723baf1 | 6128 | /* Parse the dependent-statement. */ |
a5bcc582 | 6129 | cp_parser_statement (parser, false); |
a723baf1 | 6130 | /* Finish the dummy compound-statement. */ |
7a3397c7 | 6131 | finish_compound_stmt (statement); |
a723baf1 MM |
6132 | } |
6133 | /* Otherwise, we simply parse the statement directly. */ | |
6134 | else | |
a5bcc582 | 6135 | statement = cp_parser_compound_statement (parser, false); |
a723baf1 MM |
6136 | |
6137 | /* Return the statement. */ | |
6138 | return statement; | |
6139 | } | |
6140 | ||
6141 | /* For some dependent statements (like `while (cond) statement'), we | |
6142 | have already created a scope. Therefore, even if the dependent | |
6143 | statement is a compound-statement, we do not want to create another | |
6144 | scope. */ | |
6145 | ||
6146 | static void | |
94edc4ab | 6147 | cp_parser_already_scoped_statement (cp_parser* parser) |
a723baf1 MM |
6148 | { |
6149 | /* If the token is not a `{', then we must take special action. */ | |
6150 | if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE)) | |
6151 | { | |
6152 | tree statement; | |
6153 | ||
6154 | /* Create a compound-statement. */ | |
7a3397c7 | 6155 | statement = begin_compound_stmt (/*has_no_scope=*/true); |
a723baf1 | 6156 | /* Parse the dependent-statement. */ |
a5bcc582 | 6157 | cp_parser_statement (parser, false); |
a723baf1 | 6158 | /* Finish the dummy compound-statement. */ |
7a3397c7 | 6159 | finish_compound_stmt (statement); |
a723baf1 MM |
6160 | } |
6161 | /* Otherwise, we simply parse the statement directly. */ | |
6162 | else | |
a5bcc582 | 6163 | cp_parser_statement (parser, false); |
a723baf1 MM |
6164 | } |
6165 | ||
6166 | /* Declarations [gram.dcl.dcl] */ | |
6167 | ||
6168 | /* Parse an optional declaration-sequence. | |
6169 | ||
6170 | declaration-seq: | |
6171 | declaration | |
6172 | declaration-seq declaration */ | |
6173 | ||
6174 | static void | |
94edc4ab | 6175 | cp_parser_declaration_seq_opt (cp_parser* parser) |
a723baf1 MM |
6176 | { |
6177 | while (true) | |
6178 | { | |
6179 | cp_token *token; | |
6180 | ||
6181 | token = cp_lexer_peek_token (parser->lexer); | |
6182 | ||
6183 | if (token->type == CPP_CLOSE_BRACE | |
6184 | || token->type == CPP_EOF) | |
6185 | break; | |
6186 | ||
6187 | if (token->type == CPP_SEMICOLON) | |
6188 | { | |
6189 | /* A declaration consisting of a single semicolon is | |
6190 | invalid. Allow it unless we're being pedantic. */ | |
499b568f | 6191 | if (pedantic && !in_system_header) |
a723baf1 MM |
6192 | pedwarn ("extra `;'"); |
6193 | cp_lexer_consume_token (parser->lexer); | |
6194 | continue; | |
6195 | } | |
6196 | ||
c838d82f | 6197 | /* The C lexer modifies PENDING_LANG_CHANGE when it wants the |
34cd5ae7 | 6198 | parser to enter or exit implicit `extern "C"' blocks. */ |
c838d82f MM |
6199 | while (pending_lang_change > 0) |
6200 | { | |
6201 | push_lang_context (lang_name_c); | |
6202 | --pending_lang_change; | |
6203 | } | |
6204 | while (pending_lang_change < 0) | |
6205 | { | |
6206 | pop_lang_context (); | |
6207 | ++pending_lang_change; | |
6208 | } | |
6209 | ||
6210 | /* Parse the declaration itself. */ | |
a723baf1 MM |
6211 | cp_parser_declaration (parser); |
6212 | } | |
6213 | } | |
6214 | ||
6215 | /* Parse a declaration. | |
6216 | ||
6217 | declaration: | |
6218 | block-declaration | |
6219 | function-definition | |
6220 | template-declaration | |
6221 | explicit-instantiation | |
6222 | explicit-specialization | |
6223 | linkage-specification | |
1092805d MM |
6224 | namespace-definition |
6225 | ||
6226 | GNU extension: | |
6227 | ||
6228 | declaration: | |
6229 | __extension__ declaration */ | |
a723baf1 MM |
6230 | |
6231 | static void | |
94edc4ab | 6232 | cp_parser_declaration (cp_parser* parser) |
a723baf1 MM |
6233 | { |
6234 | cp_token token1; | |
6235 | cp_token token2; | |
1092805d MM |
6236 | int saved_pedantic; |
6237 | ||
6238 | /* Check for the `__extension__' keyword. */ | |
6239 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
6240 | { | |
6241 | /* Parse the qualified declaration. */ | |
6242 | cp_parser_declaration (parser); | |
6243 | /* Restore the PEDANTIC flag. */ | |
6244 | pedantic = saved_pedantic; | |
6245 | ||
6246 | return; | |
6247 | } | |
a723baf1 MM |
6248 | |
6249 | /* Try to figure out what kind of declaration is present. */ | |
6250 | token1 = *cp_lexer_peek_token (parser->lexer); | |
6251 | if (token1.type != CPP_EOF) | |
6252 | token2 = *cp_lexer_peek_nth_token (parser->lexer, 2); | |
6253 | ||
6254 | /* If the next token is `extern' and the following token is a string | |
6255 | literal, then we have a linkage specification. */ | |
6256 | if (token1.keyword == RID_EXTERN | |
6257 | && cp_parser_is_string_literal (&token2)) | |
6258 | cp_parser_linkage_specification (parser); | |
6259 | /* If the next token is `template', then we have either a template | |
6260 | declaration, an explicit instantiation, or an explicit | |
6261 | specialization. */ | |
6262 | else if (token1.keyword == RID_TEMPLATE) | |
6263 | { | |
6264 | /* `template <>' indicates a template specialization. */ | |
6265 | if (token2.type == CPP_LESS | |
6266 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER) | |
6267 | cp_parser_explicit_specialization (parser); | |
6268 | /* `template <' indicates a template declaration. */ | |
6269 | else if (token2.type == CPP_LESS) | |
6270 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
6271 | /* Anything else must be an explicit instantiation. */ | |
6272 | else | |
6273 | cp_parser_explicit_instantiation (parser); | |
6274 | } | |
6275 | /* If the next token is `export', then we have a template | |
6276 | declaration. */ | |
6277 | else if (token1.keyword == RID_EXPORT) | |
6278 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
6279 | /* If the next token is `extern', 'static' or 'inline' and the one | |
6280 | after that is `template', we have a GNU extended explicit | |
6281 | instantiation directive. */ | |
6282 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
6283 | && (token1.keyword == RID_EXTERN | |
6284 | || token1.keyword == RID_STATIC | |
6285 | || token1.keyword == RID_INLINE) | |
6286 | && token2.keyword == RID_TEMPLATE) | |
6287 | cp_parser_explicit_instantiation (parser); | |
6288 | /* If the next token is `namespace', check for a named or unnamed | |
6289 | namespace definition. */ | |
6290 | else if (token1.keyword == RID_NAMESPACE | |
6291 | && (/* A named namespace definition. */ | |
6292 | (token2.type == CPP_NAME | |
6293 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type | |
6294 | == CPP_OPEN_BRACE)) | |
6295 | /* An unnamed namespace definition. */ | |
6296 | || token2.type == CPP_OPEN_BRACE)) | |
6297 | cp_parser_namespace_definition (parser); | |
6298 | /* We must have either a block declaration or a function | |
6299 | definition. */ | |
6300 | else | |
6301 | /* Try to parse a block-declaration, or a function-definition. */ | |
6302 | cp_parser_block_declaration (parser, /*statement_p=*/false); | |
6303 | } | |
6304 | ||
6305 | /* Parse a block-declaration. | |
6306 | ||
6307 | block-declaration: | |
6308 | simple-declaration | |
6309 | asm-definition | |
6310 | namespace-alias-definition | |
6311 | using-declaration | |
6312 | using-directive | |
6313 | ||
6314 | GNU Extension: | |
6315 | ||
6316 | block-declaration: | |
6317 | __extension__ block-declaration | |
6318 | label-declaration | |
6319 | ||
34cd5ae7 | 6320 | If STATEMENT_P is TRUE, then this block-declaration is occurring as |
a723baf1 MM |
6321 | part of a declaration-statement. */ |
6322 | ||
6323 | static void | |
6324 | cp_parser_block_declaration (cp_parser *parser, | |
6325 | bool statement_p) | |
6326 | { | |
6327 | cp_token *token1; | |
6328 | int saved_pedantic; | |
6329 | ||
6330 | /* Check for the `__extension__' keyword. */ | |
6331 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
6332 | { | |
6333 | /* Parse the qualified declaration. */ | |
6334 | cp_parser_block_declaration (parser, statement_p); | |
6335 | /* Restore the PEDANTIC flag. */ | |
6336 | pedantic = saved_pedantic; | |
6337 | ||
6338 | return; | |
6339 | } | |
6340 | ||
6341 | /* Peek at the next token to figure out which kind of declaration is | |
6342 | present. */ | |
6343 | token1 = cp_lexer_peek_token (parser->lexer); | |
6344 | ||
6345 | /* If the next keyword is `asm', we have an asm-definition. */ | |
6346 | if (token1->keyword == RID_ASM) | |
6347 | { | |
6348 | if (statement_p) | |
6349 | cp_parser_commit_to_tentative_parse (parser); | |
6350 | cp_parser_asm_definition (parser); | |
6351 | } | |
6352 | /* If the next keyword is `namespace', we have a | |
6353 | namespace-alias-definition. */ | |
6354 | else if (token1->keyword == RID_NAMESPACE) | |
6355 | cp_parser_namespace_alias_definition (parser); | |
6356 | /* If the next keyword is `using', we have either a | |
6357 | using-declaration or a using-directive. */ | |
6358 | else if (token1->keyword == RID_USING) | |
6359 | { | |
6360 | cp_token *token2; | |
6361 | ||
6362 | if (statement_p) | |
6363 | cp_parser_commit_to_tentative_parse (parser); | |
6364 | /* If the token after `using' is `namespace', then we have a | |
6365 | using-directive. */ | |
6366 | token2 = cp_lexer_peek_nth_token (parser->lexer, 2); | |
6367 | if (token2->keyword == RID_NAMESPACE) | |
6368 | cp_parser_using_directive (parser); | |
6369 | /* Otherwise, it's a using-declaration. */ | |
6370 | else | |
6371 | cp_parser_using_declaration (parser); | |
6372 | } | |
6373 | /* If the next keyword is `__label__' we have a label declaration. */ | |
6374 | else if (token1->keyword == RID_LABEL) | |
6375 | { | |
6376 | if (statement_p) | |
6377 | cp_parser_commit_to_tentative_parse (parser); | |
6378 | cp_parser_label_declaration (parser); | |
6379 | } | |
6380 | /* Anything else must be a simple-declaration. */ | |
6381 | else | |
6382 | cp_parser_simple_declaration (parser, !statement_p); | |
6383 | } | |
6384 | ||
6385 | /* Parse a simple-declaration. | |
6386 | ||
6387 | simple-declaration: | |
6388 | decl-specifier-seq [opt] init-declarator-list [opt] ; | |
6389 | ||
6390 | init-declarator-list: | |
6391 | init-declarator | |
6392 | init-declarator-list , init-declarator | |
6393 | ||
34cd5ae7 | 6394 | If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a |
9bcb9aae | 6395 | function-definition as a simple-declaration. */ |
a723baf1 MM |
6396 | |
6397 | static void | |
94edc4ab NN |
6398 | cp_parser_simple_declaration (cp_parser* parser, |
6399 | bool function_definition_allowed_p) | |
a723baf1 MM |
6400 | { |
6401 | tree decl_specifiers; | |
6402 | tree attributes; | |
560ad596 | 6403 | int declares_class_or_enum; |
a723baf1 MM |
6404 | bool saw_declarator; |
6405 | ||
6406 | /* Defer access checks until we know what is being declared; the | |
6407 | checks for names appearing in the decl-specifier-seq should be | |
6408 | done as if we were in the scope of the thing being declared. */ | |
8d241e0b | 6409 | push_deferring_access_checks (dk_deferred); |
cf22909c | 6410 | |
a723baf1 MM |
6411 | /* Parse the decl-specifier-seq. We have to keep track of whether |
6412 | or not the decl-specifier-seq declares a named class or | |
6413 | enumeration type, since that is the only case in which the | |
6414 | init-declarator-list is allowed to be empty. | |
6415 | ||
6416 | [dcl.dcl] | |
6417 | ||
6418 | In a simple-declaration, the optional init-declarator-list can be | |
6419 | omitted only when declaring a class or enumeration, that is when | |
6420 | the decl-specifier-seq contains either a class-specifier, an | |
6421 | elaborated-type-specifier, or an enum-specifier. */ | |
6422 | decl_specifiers | |
6423 | = cp_parser_decl_specifier_seq (parser, | |
6424 | CP_PARSER_FLAGS_OPTIONAL, | |
6425 | &attributes, | |
6426 | &declares_class_or_enum); | |
6427 | /* We no longer need to defer access checks. */ | |
cf22909c | 6428 | stop_deferring_access_checks (); |
24c0ef37 | 6429 | |
39703eb9 MM |
6430 | /* In a block scope, a valid declaration must always have a |
6431 | decl-specifier-seq. By not trying to parse declarators, we can | |
6432 | resolve the declaration/expression ambiguity more quickly. */ | |
6433 | if (!function_definition_allowed_p && !decl_specifiers) | |
6434 | { | |
6435 | cp_parser_error (parser, "expected declaration"); | |
6436 | goto done; | |
6437 | } | |
6438 | ||
8fbc5ae7 MM |
6439 | /* If the next two tokens are both identifiers, the code is |
6440 | erroneous. The usual cause of this situation is code like: | |
6441 | ||
6442 | T t; | |
6443 | ||
6444 | where "T" should name a type -- but does not. */ | |
6445 | if (cp_parser_diagnose_invalid_type_name (parser)) | |
6446 | { | |
8d241e0b | 6447 | /* If parsing tentatively, we should commit; we really are |
8fbc5ae7 MM |
6448 | looking at a declaration. */ |
6449 | cp_parser_commit_to_tentative_parse (parser); | |
6450 | /* Give up. */ | |
39703eb9 | 6451 | goto done; |
8fbc5ae7 MM |
6452 | } |
6453 | ||
a723baf1 MM |
6454 | /* Keep going until we hit the `;' at the end of the simple |
6455 | declaration. */ | |
6456 | saw_declarator = false; | |
6457 | while (cp_lexer_next_token_is_not (parser->lexer, | |
6458 | CPP_SEMICOLON)) | |
6459 | { | |
6460 | cp_token *token; | |
6461 | bool function_definition_p; | |
560ad596 | 6462 | tree decl; |
a723baf1 MM |
6463 | |
6464 | saw_declarator = true; | |
6465 | /* Parse the init-declarator. */ | |
560ad596 MM |
6466 | decl = cp_parser_init_declarator (parser, decl_specifiers, attributes, |
6467 | function_definition_allowed_p, | |
6468 | /*member_p=*/false, | |
6469 | declares_class_or_enum, | |
6470 | &function_definition_p); | |
1fb3244a MM |
6471 | /* If an error occurred while parsing tentatively, exit quickly. |
6472 | (That usually happens when in the body of a function; each | |
6473 | statement is treated as a declaration-statement until proven | |
6474 | otherwise.) */ | |
6475 | if (cp_parser_error_occurred (parser)) | |
39703eb9 | 6476 | goto done; |
a723baf1 MM |
6477 | /* Handle function definitions specially. */ |
6478 | if (function_definition_p) | |
6479 | { | |
6480 | /* If the next token is a `,', then we are probably | |
6481 | processing something like: | |
6482 | ||
6483 | void f() {}, *p; | |
6484 | ||
6485 | which is erroneous. */ | |
6486 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
6487 | error ("mixing declarations and function-definitions is forbidden"); | |
6488 | /* Otherwise, we're done with the list of declarators. */ | |
6489 | else | |
24c0ef37 | 6490 | { |
cf22909c | 6491 | pop_deferring_access_checks (); |
24c0ef37 GS |
6492 | return; |
6493 | } | |
a723baf1 MM |
6494 | } |
6495 | /* The next token should be either a `,' or a `;'. */ | |
6496 | token = cp_lexer_peek_token (parser->lexer); | |
6497 | /* If it's a `,', there are more declarators to come. */ | |
6498 | if (token->type == CPP_COMMA) | |
6499 | cp_lexer_consume_token (parser->lexer); | |
6500 | /* If it's a `;', we are done. */ | |
6501 | else if (token->type == CPP_SEMICOLON) | |
6502 | break; | |
6503 | /* Anything else is an error. */ | |
6504 | else | |
6505 | { | |
6506 | cp_parser_error (parser, "expected `,' or `;'"); | |
6507 | /* Skip tokens until we reach the end of the statement. */ | |
6508 | cp_parser_skip_to_end_of_statement (parser); | |
5a98fa7b MM |
6509 | /* If the next token is now a `;', consume it. */ |
6510 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
6511 | cp_lexer_consume_token (parser->lexer); | |
39703eb9 | 6512 | goto done; |
a723baf1 MM |
6513 | } |
6514 | /* After the first time around, a function-definition is not | |
6515 | allowed -- even if it was OK at first. For example: | |
6516 | ||
6517 | int i, f() {} | |
6518 | ||
6519 | is not valid. */ | |
6520 | function_definition_allowed_p = false; | |
6521 | } | |
6522 | ||
6523 | /* Issue an error message if no declarators are present, and the | |
6524 | decl-specifier-seq does not itself declare a class or | |
6525 | enumeration. */ | |
6526 | if (!saw_declarator) | |
6527 | { | |
6528 | if (cp_parser_declares_only_class_p (parser)) | |
6529 | shadow_tag (decl_specifiers); | |
6530 | /* Perform any deferred access checks. */ | |
cf22909c | 6531 | perform_deferred_access_checks (); |
a723baf1 MM |
6532 | } |
6533 | ||
6534 | /* Consume the `;'. */ | |
6535 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6536 | ||
39703eb9 MM |
6537 | done: |
6538 | pop_deferring_access_checks (); | |
a723baf1 MM |
6539 | } |
6540 | ||
6541 | /* Parse a decl-specifier-seq. | |
6542 | ||
6543 | decl-specifier-seq: | |
6544 | decl-specifier-seq [opt] decl-specifier | |
6545 | ||
6546 | decl-specifier: | |
6547 | storage-class-specifier | |
6548 | type-specifier | |
6549 | function-specifier | |
6550 | friend | |
6551 | typedef | |
6552 | ||
6553 | GNU Extension: | |
6554 | ||
6555 | decl-specifier-seq: | |
6556 | decl-specifier-seq [opt] attributes | |
6557 | ||
6558 | Returns a TREE_LIST, giving the decl-specifiers in the order they | |
6559 | appear in the source code. The TREE_VALUE of each node is the | |
6560 | decl-specifier. For a keyword (such as `auto' or `friend'), the | |
34cd5ae7 | 6561 | TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the |
a723baf1 MM |
6562 | representation of a type-specifier, see cp_parser_type_specifier. |
6563 | ||
6564 | If there are attributes, they will be stored in *ATTRIBUTES, | |
6565 | represented as described above cp_parser_attributes. | |
6566 | ||
6567 | If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier | |
6568 | appears, and the entity that will be a friend is not going to be a | |
6569 | class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that | |
6570 | even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which | |
560ad596 MM |
6571 | friendship is granted might not be a class. |
6572 | ||
6573 | *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following | |
543ca912 | 6574 | flags: |
560ad596 MM |
6575 | |
6576 | 1: one of the decl-specifiers is an elaborated-type-specifier | |
543ca912 | 6577 | (i.e., a type declaration) |
560ad596 | 6578 | 2: one of the decl-specifiers is an enum-specifier or a |
543ca912 | 6579 | class-specifier (i.e., a type definition) |
560ad596 MM |
6580 | |
6581 | */ | |
a723baf1 MM |
6582 | |
6583 | static tree | |
94edc4ab NN |
6584 | cp_parser_decl_specifier_seq (cp_parser* parser, |
6585 | cp_parser_flags flags, | |
6586 | tree* attributes, | |
560ad596 | 6587 | int* declares_class_or_enum) |
a723baf1 MM |
6588 | { |
6589 | tree decl_specs = NULL_TREE; | |
6590 | bool friend_p = false; | |
f2ce60b8 NS |
6591 | bool constructor_possible_p = !parser->in_declarator_p; |
6592 | ||
a723baf1 | 6593 | /* Assume no class or enumeration type is declared. */ |
560ad596 | 6594 | *declares_class_or_enum = 0; |
a723baf1 MM |
6595 | |
6596 | /* Assume there are no attributes. */ | |
6597 | *attributes = NULL_TREE; | |
6598 | ||
6599 | /* Keep reading specifiers until there are no more to read. */ | |
6600 | while (true) | |
6601 | { | |
6602 | tree decl_spec = NULL_TREE; | |
6603 | bool constructor_p; | |
6604 | cp_token *token; | |
6605 | ||
6606 | /* Peek at the next token. */ | |
6607 | token = cp_lexer_peek_token (parser->lexer); | |
6608 | /* Handle attributes. */ | |
6609 | if (token->keyword == RID_ATTRIBUTE) | |
6610 | { | |
6611 | /* Parse the attributes. */ | |
6612 | decl_spec = cp_parser_attributes_opt (parser); | |
6613 | /* Add them to the list. */ | |
6614 | *attributes = chainon (*attributes, decl_spec); | |
6615 | continue; | |
6616 | } | |
6617 | /* If the next token is an appropriate keyword, we can simply | |
6618 | add it to the list. */ | |
6619 | switch (token->keyword) | |
6620 | { | |
6621 | case RID_FRIEND: | |
6622 | /* decl-specifier: | |
6623 | friend */ | |
1918facf SB |
6624 | if (friend_p) |
6625 | error ("duplicate `friend'"); | |
6626 | else | |
6627 | friend_p = true; | |
a723baf1 MM |
6628 | /* The representation of the specifier is simply the |
6629 | appropriate TREE_IDENTIFIER node. */ | |
6630 | decl_spec = token->value; | |
6631 | /* Consume the token. */ | |
6632 | cp_lexer_consume_token (parser->lexer); | |
6633 | break; | |
6634 | ||
6635 | /* function-specifier: | |
6636 | inline | |
6637 | virtual | |
6638 | explicit */ | |
6639 | case RID_INLINE: | |
6640 | case RID_VIRTUAL: | |
6641 | case RID_EXPLICIT: | |
6642 | decl_spec = cp_parser_function_specifier_opt (parser); | |
6643 | break; | |
6644 | ||
6645 | /* decl-specifier: | |
6646 | typedef */ | |
6647 | case RID_TYPEDEF: | |
6648 | /* The representation of the specifier is simply the | |
6649 | appropriate TREE_IDENTIFIER node. */ | |
6650 | decl_spec = token->value; | |
6651 | /* Consume the token. */ | |
6652 | cp_lexer_consume_token (parser->lexer); | |
2050a1bb MM |
6653 | /* A constructor declarator cannot appear in a typedef. */ |
6654 | constructor_possible_p = false; | |
c006d942 MM |
6655 | /* The "typedef" keyword can only occur in a declaration; we |
6656 | may as well commit at this point. */ | |
6657 | cp_parser_commit_to_tentative_parse (parser); | |
a723baf1 MM |
6658 | break; |
6659 | ||
6660 | /* storage-class-specifier: | |
6661 | auto | |
6662 | register | |
6663 | static | |
6664 | extern | |
6665 | mutable | |
6666 | ||
6667 | GNU Extension: | |
6668 | thread */ | |
6669 | case RID_AUTO: | |
6670 | case RID_REGISTER: | |
6671 | case RID_STATIC: | |
6672 | case RID_EXTERN: | |
6673 | case RID_MUTABLE: | |
6674 | case RID_THREAD: | |
6675 | decl_spec = cp_parser_storage_class_specifier_opt (parser); | |
6676 | break; | |
6677 | ||
6678 | default: | |
6679 | break; | |
6680 | } | |
6681 | ||
6682 | /* Constructors are a special case. The `S' in `S()' is not a | |
6683 | decl-specifier; it is the beginning of the declarator. */ | |
6684 | constructor_p = (!decl_spec | |
2050a1bb | 6685 | && constructor_possible_p |
a723baf1 MM |
6686 | && cp_parser_constructor_declarator_p (parser, |
6687 | friend_p)); | |
6688 | ||
6689 | /* If we don't have a DECL_SPEC yet, then we must be looking at | |
6690 | a type-specifier. */ | |
6691 | if (!decl_spec && !constructor_p) | |
6692 | { | |
560ad596 | 6693 | int decl_spec_declares_class_or_enum; |
a723baf1 MM |
6694 | bool is_cv_qualifier; |
6695 | ||
6696 | decl_spec | |
6697 | = cp_parser_type_specifier (parser, flags, | |
6698 | friend_p, | |
6699 | /*is_declaration=*/true, | |
6700 | &decl_spec_declares_class_or_enum, | |
6701 | &is_cv_qualifier); | |
6702 | ||
6703 | *declares_class_or_enum |= decl_spec_declares_class_or_enum; | |
6704 | ||
6705 | /* If this type-specifier referenced a user-defined type | |
6706 | (a typedef, class-name, etc.), then we can't allow any | |
6707 | more such type-specifiers henceforth. | |
6708 | ||
6709 | [dcl.spec] | |
6710 | ||
6711 | The longest sequence of decl-specifiers that could | |
6712 | possibly be a type name is taken as the | |
6713 | decl-specifier-seq of a declaration. The sequence shall | |
6714 | be self-consistent as described below. | |
6715 | ||
6716 | [dcl.type] | |
6717 | ||
6718 | As a general rule, at most one type-specifier is allowed | |
6719 | in the complete decl-specifier-seq of a declaration. The | |
6720 | only exceptions are the following: | |
6721 | ||
6722 | -- const or volatile can be combined with any other | |
6723 | type-specifier. | |
6724 | ||
6725 | -- signed or unsigned can be combined with char, long, | |
6726 | short, or int. | |
6727 | ||
6728 | -- .. | |
6729 | ||
6730 | Example: | |
6731 | ||
6732 | typedef char* Pc; | |
6733 | void g (const int Pc); | |
6734 | ||
6735 | Here, Pc is *not* part of the decl-specifier seq; it's | |
6736 | the declarator. Therefore, once we see a type-specifier | |
6737 | (other than a cv-qualifier), we forbid any additional | |
6738 | user-defined types. We *do* still allow things like `int | |
6739 | int' to be considered a decl-specifier-seq, and issue the | |
6740 | error message later. */ | |
6741 | if (decl_spec && !is_cv_qualifier) | |
6742 | flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; | |
2050a1bb MM |
6743 | /* A constructor declarator cannot follow a type-specifier. */ |
6744 | if (decl_spec) | |
6745 | constructor_possible_p = false; | |
a723baf1 MM |
6746 | } |
6747 | ||
6748 | /* If we still do not have a DECL_SPEC, then there are no more | |
6749 | decl-specifiers. */ | |
6750 | if (!decl_spec) | |
6751 | { | |
6752 | /* Issue an error message, unless the entire construct was | |
6753 | optional. */ | |
6754 | if (!(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
6755 | { | |
6756 | cp_parser_error (parser, "expected decl specifier"); | |
6757 | return error_mark_node; | |
6758 | } | |
6759 | ||
6760 | break; | |
6761 | } | |
6762 | ||
6763 | /* Add the DECL_SPEC to the list of specifiers. */ | |
e90c7b84 ILT |
6764 | if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node) |
6765 | decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs); | |
a723baf1 MM |
6766 | |
6767 | /* After we see one decl-specifier, further decl-specifiers are | |
6768 | always optional. */ | |
6769 | flags |= CP_PARSER_FLAGS_OPTIONAL; | |
6770 | } | |
6771 | ||
0426c4ca SB |
6772 | /* Don't allow a friend specifier with a class definition. */ |
6773 | if (friend_p && (*declares_class_or_enum & 2)) | |
6774 | error ("class definition may not be declared a friend"); | |
6775 | ||
a723baf1 MM |
6776 | /* We have built up the DECL_SPECS in reverse order. Return them in |
6777 | the correct order. */ | |
6778 | return nreverse (decl_specs); | |
6779 | } | |
6780 | ||
6781 | /* Parse an (optional) storage-class-specifier. | |
6782 | ||
6783 | storage-class-specifier: | |
6784 | auto | |
6785 | register | |
6786 | static | |
6787 | extern | |
6788 | mutable | |
6789 | ||
6790 | GNU Extension: | |
6791 | ||
6792 | storage-class-specifier: | |
6793 | thread | |
6794 | ||
6795 | Returns an IDENTIFIER_NODE corresponding to the keyword used. */ | |
6796 | ||
6797 | static tree | |
94edc4ab | 6798 | cp_parser_storage_class_specifier_opt (cp_parser* parser) |
a723baf1 MM |
6799 | { |
6800 | switch (cp_lexer_peek_token (parser->lexer)->keyword) | |
6801 | { | |
6802 | case RID_AUTO: | |
6803 | case RID_REGISTER: | |
6804 | case RID_STATIC: | |
6805 | case RID_EXTERN: | |
6806 | case RID_MUTABLE: | |
6807 | case RID_THREAD: | |
6808 | /* Consume the token. */ | |
6809 | return cp_lexer_consume_token (parser->lexer)->value; | |
6810 | ||
6811 | default: | |
6812 | return NULL_TREE; | |
6813 | } | |
6814 | } | |
6815 | ||
6816 | /* Parse an (optional) function-specifier. | |
6817 | ||
6818 | function-specifier: | |
6819 | inline | |
6820 | virtual | |
6821 | explicit | |
6822 | ||
6823 | Returns an IDENTIFIER_NODE corresponding to the keyword used. */ | |
6824 | ||
6825 | static tree | |
94edc4ab | 6826 | cp_parser_function_specifier_opt (cp_parser* parser) |
a723baf1 MM |
6827 | { |
6828 | switch (cp_lexer_peek_token (parser->lexer)->keyword) | |
6829 | { | |
6830 | case RID_INLINE: | |
6831 | case RID_VIRTUAL: | |
6832 | case RID_EXPLICIT: | |
6833 | /* Consume the token. */ | |
6834 | return cp_lexer_consume_token (parser->lexer)->value; | |
6835 | ||
6836 | default: | |
6837 | return NULL_TREE; | |
6838 | } | |
6839 | } | |
6840 | ||
6841 | /* Parse a linkage-specification. | |
6842 | ||
6843 | linkage-specification: | |
6844 | extern string-literal { declaration-seq [opt] } | |
6845 | extern string-literal declaration */ | |
6846 | ||
6847 | static void | |
94edc4ab | 6848 | cp_parser_linkage_specification (cp_parser* parser) |
a723baf1 MM |
6849 | { |
6850 | cp_token *token; | |
6851 | tree linkage; | |
6852 | ||
6853 | /* Look for the `extern' keyword. */ | |
6854 | cp_parser_require_keyword (parser, RID_EXTERN, "`extern'"); | |
6855 | ||
6856 | /* Peek at the next token. */ | |
6857 | token = cp_lexer_peek_token (parser->lexer); | |
6858 | /* If it's not a string-literal, then there's a problem. */ | |
6859 | if (!cp_parser_is_string_literal (token)) | |
6860 | { | |
6861 | cp_parser_error (parser, "expected language-name"); | |
6862 | return; | |
6863 | } | |
6864 | /* Consume the token. */ | |
6865 | cp_lexer_consume_token (parser->lexer); | |
6866 | ||
6867 | /* Transform the literal into an identifier. If the literal is a | |
6868 | wide-character string, or contains embedded NULs, then we can't | |
6869 | handle it as the user wants. */ | |
6870 | if (token->type == CPP_WSTRING | |
6871 | || (strlen (TREE_STRING_POINTER (token->value)) | |
6872 | != (size_t) (TREE_STRING_LENGTH (token->value) - 1))) | |
6873 | { | |
6874 | cp_parser_error (parser, "invalid linkage-specification"); | |
6875 | /* Assume C++ linkage. */ | |
6876 | linkage = get_identifier ("c++"); | |
6877 | } | |
6878 | /* If it's a simple string constant, things are easier. */ | |
6879 | else | |
6880 | linkage = get_identifier (TREE_STRING_POINTER (token->value)); | |
6881 | ||
6882 | /* We're now using the new linkage. */ | |
6883 | push_lang_context (linkage); | |
6884 | ||
6885 | /* If the next token is a `{', then we're using the first | |
6886 | production. */ | |
6887 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
6888 | { | |
6889 | /* Consume the `{' token. */ | |
6890 | cp_lexer_consume_token (parser->lexer); | |
6891 | /* Parse the declarations. */ | |
6892 | cp_parser_declaration_seq_opt (parser); | |
6893 | /* Look for the closing `}'. */ | |
6894 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
6895 | } | |
6896 | /* Otherwise, there's just one declaration. */ | |
6897 | else | |
6898 | { | |
6899 | bool saved_in_unbraced_linkage_specification_p; | |
6900 | ||
6901 | saved_in_unbraced_linkage_specification_p | |
6902 | = parser->in_unbraced_linkage_specification_p; | |
6903 | parser->in_unbraced_linkage_specification_p = true; | |
6904 | have_extern_spec = true; | |
6905 | cp_parser_declaration (parser); | |
6906 | have_extern_spec = false; | |
6907 | parser->in_unbraced_linkage_specification_p | |
6908 | = saved_in_unbraced_linkage_specification_p; | |
6909 | } | |
6910 | ||
6911 | /* We're done with the linkage-specification. */ | |
6912 | pop_lang_context (); | |
6913 | } | |
6914 | ||
6915 | /* Special member functions [gram.special] */ | |
6916 | ||
6917 | /* Parse a conversion-function-id. | |
6918 | ||
6919 | conversion-function-id: | |
6920 | operator conversion-type-id | |
6921 | ||
6922 | Returns an IDENTIFIER_NODE representing the operator. */ | |
6923 | ||
6924 | static tree | |
94edc4ab | 6925 | cp_parser_conversion_function_id (cp_parser* parser) |
a723baf1 MM |
6926 | { |
6927 | tree type; | |
6928 | tree saved_scope; | |
6929 | tree saved_qualifying_scope; | |
6930 | tree saved_object_scope; | |
6931 | ||
6932 | /* Look for the `operator' token. */ | |
6933 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'")) | |
6934 | return error_mark_node; | |
6935 | /* When we parse the conversion-type-id, the current scope will be | |
6936 | reset. However, we need that information in able to look up the | |
6937 | conversion function later, so we save it here. */ | |
6938 | saved_scope = parser->scope; | |
6939 | saved_qualifying_scope = parser->qualifying_scope; | |
6940 | saved_object_scope = parser->object_scope; | |
6941 | /* We must enter the scope of the class so that the names of | |
6942 | entities declared within the class are available in the | |
6943 | conversion-type-id. For example, consider: | |
6944 | ||
6945 | struct S { | |
6946 | typedef int I; | |
6947 | operator I(); | |
6948 | }; | |
6949 | ||
6950 | S::operator I() { ... } | |
6951 | ||
6952 | In order to see that `I' is a type-name in the definition, we | |
6953 | must be in the scope of `S'. */ | |
6954 | if (saved_scope) | |
6955 | push_scope (saved_scope); | |
6956 | /* Parse the conversion-type-id. */ | |
6957 | type = cp_parser_conversion_type_id (parser); | |
6958 | /* Leave the scope of the class, if any. */ | |
6959 | if (saved_scope) | |
6960 | pop_scope (saved_scope); | |
6961 | /* Restore the saved scope. */ | |
6962 | parser->scope = saved_scope; | |
6963 | parser->qualifying_scope = saved_qualifying_scope; | |
6964 | parser->object_scope = saved_object_scope; | |
6965 | /* If the TYPE is invalid, indicate failure. */ | |
6966 | if (type == error_mark_node) | |
6967 | return error_mark_node; | |
6968 | return mangle_conv_op_name_for_type (type); | |
6969 | } | |
6970 | ||
6971 | /* Parse a conversion-type-id: | |
6972 | ||
6973 | conversion-type-id: | |
6974 | type-specifier-seq conversion-declarator [opt] | |
6975 | ||
6976 | Returns the TYPE specified. */ | |
6977 | ||
6978 | static tree | |
94edc4ab | 6979 | cp_parser_conversion_type_id (cp_parser* parser) |
a723baf1 MM |
6980 | { |
6981 | tree attributes; | |
6982 | tree type_specifiers; | |
6983 | tree declarator; | |
6984 | ||
6985 | /* Parse the attributes. */ | |
6986 | attributes = cp_parser_attributes_opt (parser); | |
6987 | /* Parse the type-specifiers. */ | |
6988 | type_specifiers = cp_parser_type_specifier_seq (parser); | |
6989 | /* If that didn't work, stop. */ | |
6990 | if (type_specifiers == error_mark_node) | |
6991 | return error_mark_node; | |
6992 | /* Parse the conversion-declarator. */ | |
6993 | declarator = cp_parser_conversion_declarator_opt (parser); | |
6994 | ||
6995 | return grokdeclarator (declarator, type_specifiers, TYPENAME, | |
6996 | /*initialized=*/0, &attributes); | |
6997 | } | |
6998 | ||
6999 | /* Parse an (optional) conversion-declarator. | |
7000 | ||
7001 | conversion-declarator: | |
7002 | ptr-operator conversion-declarator [opt] | |
7003 | ||
7004 | Returns a representation of the declarator. See | |
7005 | cp_parser_declarator for details. */ | |
7006 | ||
7007 | static tree | |
94edc4ab | 7008 | cp_parser_conversion_declarator_opt (cp_parser* parser) |
a723baf1 MM |
7009 | { |
7010 | enum tree_code code; | |
7011 | tree class_type; | |
7012 | tree cv_qualifier_seq; | |
7013 | ||
7014 | /* We don't know if there's a ptr-operator next, or not. */ | |
7015 | cp_parser_parse_tentatively (parser); | |
7016 | /* Try the ptr-operator. */ | |
7017 | code = cp_parser_ptr_operator (parser, &class_type, | |
7018 | &cv_qualifier_seq); | |
7019 | /* If it worked, look for more conversion-declarators. */ | |
7020 | if (cp_parser_parse_definitely (parser)) | |
7021 | { | |
7022 | tree declarator; | |
7023 | ||
7024 | /* Parse another optional declarator. */ | |
7025 | declarator = cp_parser_conversion_declarator_opt (parser); | |
7026 | ||
7027 | /* Create the representation of the declarator. */ | |
7028 | if (code == INDIRECT_REF) | |
7029 | declarator = make_pointer_declarator (cv_qualifier_seq, | |
7030 | declarator); | |
7031 | else | |
7032 | declarator = make_reference_declarator (cv_qualifier_seq, | |
7033 | declarator); | |
7034 | ||
7035 | /* Handle the pointer-to-member case. */ | |
7036 | if (class_type) | |
7037 | declarator = build_nt (SCOPE_REF, class_type, declarator); | |
7038 | ||
7039 | return declarator; | |
7040 | } | |
7041 | ||
7042 | return NULL_TREE; | |
7043 | } | |
7044 | ||
7045 | /* Parse an (optional) ctor-initializer. | |
7046 | ||
7047 | ctor-initializer: | |
7048 | : mem-initializer-list | |
7049 | ||
7050 | Returns TRUE iff the ctor-initializer was actually present. */ | |
7051 | ||
7052 | static bool | |
94edc4ab | 7053 | cp_parser_ctor_initializer_opt (cp_parser* parser) |
a723baf1 MM |
7054 | { |
7055 | /* If the next token is not a `:', then there is no | |
7056 | ctor-initializer. */ | |
7057 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) | |
7058 | { | |
7059 | /* Do default initialization of any bases and members. */ | |
7060 | if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
7061 | finish_mem_initializers (NULL_TREE); | |
7062 | ||
7063 | return false; | |
7064 | } | |
7065 | ||
7066 | /* Consume the `:' token. */ | |
7067 | cp_lexer_consume_token (parser->lexer); | |
7068 | /* And the mem-initializer-list. */ | |
7069 | cp_parser_mem_initializer_list (parser); | |
7070 | ||
7071 | return true; | |
7072 | } | |
7073 | ||
7074 | /* Parse a mem-initializer-list. | |
7075 | ||
7076 | mem-initializer-list: | |
7077 | mem-initializer | |
7078 | mem-initializer , mem-initializer-list */ | |
7079 | ||
7080 | static void | |
94edc4ab | 7081 | cp_parser_mem_initializer_list (cp_parser* parser) |
a723baf1 MM |
7082 | { |
7083 | tree mem_initializer_list = NULL_TREE; | |
7084 | ||
7085 | /* Let the semantic analysis code know that we are starting the | |
7086 | mem-initializer-list. */ | |
0e136342 MM |
7087 | if (!DECL_CONSTRUCTOR_P (current_function_decl)) |
7088 | error ("only constructors take base initializers"); | |
a723baf1 MM |
7089 | |
7090 | /* Loop through the list. */ | |
7091 | while (true) | |
7092 | { | |
7093 | tree mem_initializer; | |
7094 | ||
7095 | /* Parse the mem-initializer. */ | |
7096 | mem_initializer = cp_parser_mem_initializer (parser); | |
7097 | /* Add it to the list, unless it was erroneous. */ | |
7098 | if (mem_initializer) | |
7099 | { | |
7100 | TREE_CHAIN (mem_initializer) = mem_initializer_list; | |
7101 | mem_initializer_list = mem_initializer; | |
7102 | } | |
7103 | /* If the next token is not a `,', we're done. */ | |
7104 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
7105 | break; | |
7106 | /* Consume the `,' token. */ | |
7107 | cp_lexer_consume_token (parser->lexer); | |
7108 | } | |
7109 | ||
7110 | /* Perform semantic analysis. */ | |
0e136342 MM |
7111 | if (DECL_CONSTRUCTOR_P (current_function_decl)) |
7112 | finish_mem_initializers (mem_initializer_list); | |
a723baf1 MM |
7113 | } |
7114 | ||
7115 | /* Parse a mem-initializer. | |
7116 | ||
7117 | mem-initializer: | |
7118 | mem-initializer-id ( expression-list [opt] ) | |
7119 | ||
7120 | GNU extension: | |
7121 | ||
7122 | mem-initializer: | |
34cd5ae7 | 7123 | ( expression-list [opt] ) |
a723baf1 MM |
7124 | |
7125 | Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base | |
7126 | class) or FIELD_DECL (for a non-static data member) to initialize; | |
7127 | the TREE_VALUE is the expression-list. */ | |
7128 | ||
7129 | static tree | |
94edc4ab | 7130 | cp_parser_mem_initializer (cp_parser* parser) |
a723baf1 MM |
7131 | { |
7132 | tree mem_initializer_id; | |
7133 | tree expression_list; | |
1f5a253a NS |
7134 | tree member; |
7135 | ||
a723baf1 MM |
7136 | /* Find out what is being initialized. */ |
7137 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
7138 | { | |
7139 | pedwarn ("anachronistic old-style base class initializer"); | |
7140 | mem_initializer_id = NULL_TREE; | |
7141 | } | |
7142 | else | |
7143 | mem_initializer_id = cp_parser_mem_initializer_id (parser); | |
1f5a253a NS |
7144 | member = expand_member_init (mem_initializer_id); |
7145 | if (member && !DECL_P (member)) | |
7146 | in_base_initializer = 1; | |
7efa3e22 | 7147 | |
39703eb9 MM |
7148 | expression_list |
7149 | = cp_parser_parenthesized_expression_list (parser, false, | |
7150 | /*non_constant_p=*/NULL); | |
7efa3e22 | 7151 | if (!expression_list) |
a723baf1 | 7152 | expression_list = void_type_node; |
a723baf1 | 7153 | |
1f5a253a NS |
7154 | in_base_initializer = 0; |
7155 | ||
7156 | return member ? build_tree_list (member, expression_list) : NULL_TREE; | |
a723baf1 MM |
7157 | } |
7158 | ||
7159 | /* Parse a mem-initializer-id. | |
7160 | ||
7161 | mem-initializer-id: | |
7162 | :: [opt] nested-name-specifier [opt] class-name | |
7163 | identifier | |
7164 | ||
7165 | Returns a TYPE indicating the class to be initializer for the first | |
7166 | production. Returns an IDENTIFIER_NODE indicating the data member | |
7167 | to be initialized for the second production. */ | |
7168 | ||
7169 | static tree | |
94edc4ab | 7170 | cp_parser_mem_initializer_id (cp_parser* parser) |
a723baf1 MM |
7171 | { |
7172 | bool global_scope_p; | |
7173 | bool nested_name_specifier_p; | |
7174 | tree id; | |
7175 | ||
7176 | /* Look for the optional `::' operator. */ | |
7177 | global_scope_p | |
7178 | = (cp_parser_global_scope_opt (parser, | |
7179 | /*current_scope_valid_p=*/false) | |
7180 | != NULL_TREE); | |
7181 | /* Look for the optional nested-name-specifier. The simplest way to | |
7182 | implement: | |
7183 | ||
7184 | [temp.res] | |
7185 | ||
7186 | The keyword `typename' is not permitted in a base-specifier or | |
7187 | mem-initializer; in these contexts a qualified name that | |
7188 | depends on a template-parameter is implicitly assumed to be a | |
7189 | type name. | |
7190 | ||
7191 | is to assume that we have seen the `typename' keyword at this | |
7192 | point. */ | |
7193 | nested_name_specifier_p | |
7194 | = (cp_parser_nested_name_specifier_opt (parser, | |
7195 | /*typename_keyword_p=*/true, | |
7196 | /*check_dependency_p=*/true, | |
a668c6ad MM |
7197 | /*type_p=*/true, |
7198 | /*is_declaration=*/true) | |
a723baf1 MM |
7199 | != NULL_TREE); |
7200 | /* If there is a `::' operator or a nested-name-specifier, then we | |
7201 | are definitely looking for a class-name. */ | |
7202 | if (global_scope_p || nested_name_specifier_p) | |
7203 | return cp_parser_class_name (parser, | |
7204 | /*typename_keyword_p=*/true, | |
7205 | /*template_keyword_p=*/false, | |
7206 | /*type_p=*/false, | |
a723baf1 | 7207 | /*check_dependency_p=*/true, |
a668c6ad MM |
7208 | /*class_head_p=*/false, |
7209 | /*is_declaration=*/true); | |
a723baf1 MM |
7210 | /* Otherwise, we could also be looking for an ordinary identifier. */ |
7211 | cp_parser_parse_tentatively (parser); | |
7212 | /* Try a class-name. */ | |
7213 | id = cp_parser_class_name (parser, | |
7214 | /*typename_keyword_p=*/true, | |
7215 | /*template_keyword_p=*/false, | |
7216 | /*type_p=*/false, | |
a723baf1 | 7217 | /*check_dependency_p=*/true, |
a668c6ad MM |
7218 | /*class_head_p=*/false, |
7219 | /*is_declaration=*/true); | |
a723baf1 MM |
7220 | /* If we found one, we're done. */ |
7221 | if (cp_parser_parse_definitely (parser)) | |
7222 | return id; | |
7223 | /* Otherwise, look for an ordinary identifier. */ | |
7224 | return cp_parser_identifier (parser); | |
7225 | } | |
7226 | ||
7227 | /* Overloading [gram.over] */ | |
7228 | ||
7229 | /* Parse an operator-function-id. | |
7230 | ||
7231 | operator-function-id: | |
7232 | operator operator | |
7233 | ||
7234 | Returns an IDENTIFIER_NODE for the operator which is a | |
7235 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
7236 | ||
7237 | static tree | |
94edc4ab | 7238 | cp_parser_operator_function_id (cp_parser* parser) |
a723baf1 MM |
7239 | { |
7240 | /* Look for the `operator' keyword. */ | |
7241 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'")) | |
7242 | return error_mark_node; | |
7243 | /* And then the name of the operator itself. */ | |
7244 | return cp_parser_operator (parser); | |
7245 | } | |
7246 | ||
7247 | /* Parse an operator. | |
7248 | ||
7249 | operator: | |
7250 | new delete new[] delete[] + - * / % ^ & | ~ ! = < > | |
7251 | += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && | |
7252 | || ++ -- , ->* -> () [] | |
7253 | ||
7254 | GNU Extensions: | |
7255 | ||
7256 | operator: | |
7257 | <? >? <?= >?= | |
7258 | ||
7259 | Returns an IDENTIFIER_NODE for the operator which is a | |
7260 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
7261 | ||
7262 | static tree | |
94edc4ab | 7263 | cp_parser_operator (cp_parser* parser) |
a723baf1 MM |
7264 | { |
7265 | tree id = NULL_TREE; | |
7266 | cp_token *token; | |
7267 | ||
7268 | /* Peek at the next token. */ | |
7269 | token = cp_lexer_peek_token (parser->lexer); | |
7270 | /* Figure out which operator we have. */ | |
7271 | switch (token->type) | |
7272 | { | |
7273 | case CPP_KEYWORD: | |
7274 | { | |
7275 | enum tree_code op; | |
7276 | ||
7277 | /* The keyword should be either `new' or `delete'. */ | |
7278 | if (token->keyword == RID_NEW) | |
7279 | op = NEW_EXPR; | |
7280 | else if (token->keyword == RID_DELETE) | |
7281 | op = DELETE_EXPR; | |
7282 | else | |
7283 | break; | |
7284 | ||
7285 | /* Consume the `new' or `delete' token. */ | |
7286 | cp_lexer_consume_token (parser->lexer); | |
7287 | ||
7288 | /* Peek at the next token. */ | |
7289 | token = cp_lexer_peek_token (parser->lexer); | |
7290 | /* If it's a `[' token then this is the array variant of the | |
7291 | operator. */ | |
7292 | if (token->type == CPP_OPEN_SQUARE) | |
7293 | { | |
7294 | /* Consume the `[' token. */ | |
7295 | cp_lexer_consume_token (parser->lexer); | |
7296 | /* Look for the `]' token. */ | |
7297 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
7298 | id = ansi_opname (op == NEW_EXPR | |
7299 | ? VEC_NEW_EXPR : VEC_DELETE_EXPR); | |
7300 | } | |
7301 | /* Otherwise, we have the non-array variant. */ | |
7302 | else | |
7303 | id = ansi_opname (op); | |
7304 | ||
7305 | return id; | |
7306 | } | |
7307 | ||
7308 | case CPP_PLUS: | |
7309 | id = ansi_opname (PLUS_EXPR); | |
7310 | break; | |
7311 | ||
7312 | case CPP_MINUS: | |
7313 | id = ansi_opname (MINUS_EXPR); | |
7314 | break; | |
7315 | ||
7316 | case CPP_MULT: | |
7317 | id = ansi_opname (MULT_EXPR); | |
7318 | break; | |
7319 | ||
7320 | case CPP_DIV: | |
7321 | id = ansi_opname (TRUNC_DIV_EXPR); | |
7322 | break; | |
7323 | ||
7324 | case CPP_MOD: | |
7325 | id = ansi_opname (TRUNC_MOD_EXPR); | |
7326 | break; | |
7327 | ||
7328 | case CPP_XOR: | |
7329 | id = ansi_opname (BIT_XOR_EXPR); | |
7330 | break; | |
7331 | ||
7332 | case CPP_AND: | |
7333 | id = ansi_opname (BIT_AND_EXPR); | |
7334 | break; | |
7335 | ||
7336 | case CPP_OR: | |
7337 | id = ansi_opname (BIT_IOR_EXPR); | |
7338 | break; | |
7339 | ||
7340 | case CPP_COMPL: | |
7341 | id = ansi_opname (BIT_NOT_EXPR); | |
7342 | break; | |
7343 | ||
7344 | case CPP_NOT: | |
7345 | id = ansi_opname (TRUTH_NOT_EXPR); | |
7346 | break; | |
7347 | ||
7348 | case CPP_EQ: | |
7349 | id = ansi_assopname (NOP_EXPR); | |
7350 | break; | |
7351 | ||
7352 | case CPP_LESS: | |
7353 | id = ansi_opname (LT_EXPR); | |
7354 | break; | |
7355 | ||
7356 | case CPP_GREATER: | |
7357 | id = ansi_opname (GT_EXPR); | |
7358 | break; | |
7359 | ||
7360 | case CPP_PLUS_EQ: | |
7361 | id = ansi_assopname (PLUS_EXPR); | |
7362 | break; | |
7363 | ||
7364 | case CPP_MINUS_EQ: | |
7365 | id = ansi_assopname (MINUS_EXPR); | |
7366 | break; | |
7367 | ||
7368 | case CPP_MULT_EQ: | |
7369 | id = ansi_assopname (MULT_EXPR); | |
7370 | break; | |
7371 | ||
7372 | case CPP_DIV_EQ: | |
7373 | id = ansi_assopname (TRUNC_DIV_EXPR); | |
7374 | break; | |
7375 | ||
7376 | case CPP_MOD_EQ: | |
7377 | id = ansi_assopname (TRUNC_MOD_EXPR); | |
7378 | break; | |
7379 | ||
7380 | case CPP_XOR_EQ: | |
7381 | id = ansi_assopname (BIT_XOR_EXPR); | |
7382 | break; | |
7383 | ||
7384 | case CPP_AND_EQ: | |
7385 | id = ansi_assopname (BIT_AND_EXPR); | |
7386 | break; | |
7387 | ||
7388 | case CPP_OR_EQ: | |
7389 | id = ansi_assopname (BIT_IOR_EXPR); | |
7390 | break; | |
7391 | ||
7392 | case CPP_LSHIFT: | |
7393 | id = ansi_opname (LSHIFT_EXPR); | |
7394 | break; | |
7395 | ||
7396 | case CPP_RSHIFT: | |
7397 | id = ansi_opname (RSHIFT_EXPR); | |
7398 | break; | |
7399 | ||
7400 | case CPP_LSHIFT_EQ: | |
7401 | id = ansi_assopname (LSHIFT_EXPR); | |
7402 | break; | |
7403 | ||
7404 | case CPP_RSHIFT_EQ: | |
7405 | id = ansi_assopname (RSHIFT_EXPR); | |
7406 | break; | |
7407 | ||
7408 | case CPP_EQ_EQ: | |
7409 | id = ansi_opname (EQ_EXPR); | |
7410 | break; | |
7411 | ||
7412 | case CPP_NOT_EQ: | |
7413 | id = ansi_opname (NE_EXPR); | |
7414 | break; | |
7415 | ||
7416 | case CPP_LESS_EQ: | |
7417 | id = ansi_opname (LE_EXPR); | |
7418 | break; | |
7419 | ||
7420 | case CPP_GREATER_EQ: | |
7421 | id = ansi_opname (GE_EXPR); | |
7422 | break; | |
7423 | ||
7424 | case CPP_AND_AND: | |
7425 | id = ansi_opname (TRUTH_ANDIF_EXPR); | |
7426 | break; | |
7427 | ||
7428 | case CPP_OR_OR: | |
7429 | id = ansi_opname (TRUTH_ORIF_EXPR); | |
7430 | break; | |
7431 | ||
7432 | case CPP_PLUS_PLUS: | |
7433 | id = ansi_opname (POSTINCREMENT_EXPR); | |
7434 | break; | |
7435 | ||
7436 | case CPP_MINUS_MINUS: | |
7437 | id = ansi_opname (PREDECREMENT_EXPR); | |
7438 | break; | |
7439 | ||
7440 | case CPP_COMMA: | |
7441 | id = ansi_opname (COMPOUND_EXPR); | |
7442 | break; | |
7443 | ||
7444 | case CPP_DEREF_STAR: | |
7445 | id = ansi_opname (MEMBER_REF); | |
7446 | break; | |
7447 | ||
7448 | case CPP_DEREF: | |
7449 | id = ansi_opname (COMPONENT_REF); | |
7450 | break; | |
7451 | ||
7452 | case CPP_OPEN_PAREN: | |
7453 | /* Consume the `('. */ | |
7454 | cp_lexer_consume_token (parser->lexer); | |
7455 | /* Look for the matching `)'. */ | |
7456 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
7457 | return ansi_opname (CALL_EXPR); | |
7458 | ||
7459 | case CPP_OPEN_SQUARE: | |
7460 | /* Consume the `['. */ | |
7461 | cp_lexer_consume_token (parser->lexer); | |
7462 | /* Look for the matching `]'. */ | |
7463 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
7464 | return ansi_opname (ARRAY_REF); | |
7465 | ||
7466 | /* Extensions. */ | |
7467 | case CPP_MIN: | |
7468 | id = ansi_opname (MIN_EXPR); | |
7469 | break; | |
7470 | ||
7471 | case CPP_MAX: | |
7472 | id = ansi_opname (MAX_EXPR); | |
7473 | break; | |
7474 | ||
7475 | case CPP_MIN_EQ: | |
7476 | id = ansi_assopname (MIN_EXPR); | |
7477 | break; | |
7478 | ||
7479 | case CPP_MAX_EQ: | |
7480 | id = ansi_assopname (MAX_EXPR); | |
7481 | break; | |
7482 | ||
7483 | default: | |
7484 | /* Anything else is an error. */ | |
7485 | break; | |
7486 | } | |
7487 | ||
7488 | /* If we have selected an identifier, we need to consume the | |
7489 | operator token. */ | |
7490 | if (id) | |
7491 | cp_lexer_consume_token (parser->lexer); | |
7492 | /* Otherwise, no valid operator name was present. */ | |
7493 | else | |
7494 | { | |
7495 | cp_parser_error (parser, "expected operator"); | |
7496 | id = error_mark_node; | |
7497 | } | |
7498 | ||
7499 | return id; | |
7500 | } | |
7501 | ||
7502 | /* Parse a template-declaration. | |
7503 | ||
7504 | template-declaration: | |
7505 | export [opt] template < template-parameter-list > declaration | |
7506 | ||
7507 | If MEMBER_P is TRUE, this template-declaration occurs within a | |
7508 | class-specifier. | |
7509 | ||
7510 | The grammar rule given by the standard isn't correct. What | |
7511 | is really meant is: | |
7512 | ||
7513 | template-declaration: | |
7514 | export [opt] template-parameter-list-seq | |
7515 | decl-specifier-seq [opt] init-declarator [opt] ; | |
7516 | export [opt] template-parameter-list-seq | |
7517 | function-definition | |
7518 | ||
7519 | template-parameter-list-seq: | |
7520 | template-parameter-list-seq [opt] | |
7521 | template < template-parameter-list > */ | |
7522 | ||
7523 | static void | |
94edc4ab | 7524 | cp_parser_template_declaration (cp_parser* parser, bool member_p) |
a723baf1 MM |
7525 | { |
7526 | /* Check for `export'. */ | |
7527 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT)) | |
7528 | { | |
7529 | /* Consume the `export' token. */ | |
7530 | cp_lexer_consume_token (parser->lexer); | |
7531 | /* Warn that we do not support `export'. */ | |
7532 | warning ("keyword `export' not implemented, and will be ignored"); | |
7533 | } | |
7534 | ||
7535 | cp_parser_template_declaration_after_export (parser, member_p); | |
7536 | } | |
7537 | ||
7538 | /* Parse a template-parameter-list. | |
7539 | ||
7540 | template-parameter-list: | |
7541 | template-parameter | |
7542 | template-parameter-list , template-parameter | |
7543 | ||
7544 | Returns a TREE_LIST. Each node represents a template parameter. | |
7545 | The nodes are connected via their TREE_CHAINs. */ | |
7546 | ||
7547 | static tree | |
94edc4ab | 7548 | cp_parser_template_parameter_list (cp_parser* parser) |
a723baf1 MM |
7549 | { |
7550 | tree parameter_list = NULL_TREE; | |
7551 | ||
7552 | while (true) | |
7553 | { | |
7554 | tree parameter; | |
7555 | cp_token *token; | |
7556 | ||
7557 | /* Parse the template-parameter. */ | |
7558 | parameter = cp_parser_template_parameter (parser); | |
7559 | /* Add it to the list. */ | |
7560 | parameter_list = process_template_parm (parameter_list, | |
7561 | parameter); | |
7562 | ||
7563 | /* Peek at the next token. */ | |
7564 | token = cp_lexer_peek_token (parser->lexer); | |
7565 | /* If it's not a `,', we're done. */ | |
7566 | if (token->type != CPP_COMMA) | |
7567 | break; | |
7568 | /* Otherwise, consume the `,' token. */ | |
7569 | cp_lexer_consume_token (parser->lexer); | |
7570 | } | |
7571 | ||
7572 | return parameter_list; | |
7573 | } | |
7574 | ||
7575 | /* Parse a template-parameter. | |
7576 | ||
7577 | template-parameter: | |
7578 | type-parameter | |
7579 | parameter-declaration | |
7580 | ||
7581 | Returns a TREE_LIST. The TREE_VALUE represents the parameter. The | |
7582 | TREE_PURPOSE is the default value, if any. */ | |
7583 | ||
7584 | static tree | |
94edc4ab | 7585 | cp_parser_template_parameter (cp_parser* parser) |
a723baf1 MM |
7586 | { |
7587 | cp_token *token; | |
7588 | ||
7589 | /* Peek at the next token. */ | |
7590 | token = cp_lexer_peek_token (parser->lexer); | |
7591 | /* If it is `class' or `template', we have a type-parameter. */ | |
7592 | if (token->keyword == RID_TEMPLATE) | |
7593 | return cp_parser_type_parameter (parser); | |
7594 | /* If it is `class' or `typename' we do not know yet whether it is a | |
7595 | type parameter or a non-type parameter. Consider: | |
7596 | ||
7597 | template <typename T, typename T::X X> ... | |
7598 | ||
7599 | or: | |
7600 | ||
7601 | template <class C, class D*> ... | |
7602 | ||
7603 | Here, the first parameter is a type parameter, and the second is | |
7604 | a non-type parameter. We can tell by looking at the token after | |
7605 | the identifier -- if it is a `,', `=', or `>' then we have a type | |
7606 | parameter. */ | |
7607 | if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS) | |
7608 | { | |
7609 | /* Peek at the token after `class' or `typename'. */ | |
7610 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
7611 | /* If it's an identifier, skip it. */ | |
7612 | if (token->type == CPP_NAME) | |
7613 | token = cp_lexer_peek_nth_token (parser->lexer, 3); | |
7614 | /* Now, see if the token looks like the end of a template | |
7615 | parameter. */ | |
7616 | if (token->type == CPP_COMMA | |
7617 | || token->type == CPP_EQ | |
7618 | || token->type == CPP_GREATER) | |
7619 | return cp_parser_type_parameter (parser); | |
7620 | } | |
7621 | ||
7622 | /* Otherwise, it is a non-type parameter. | |
7623 | ||
7624 | [temp.param] | |
7625 | ||
7626 | When parsing a default template-argument for a non-type | |
7627 | template-parameter, the first non-nested `>' is taken as the end | |
7628 | of the template parameter-list rather than a greater-than | |
7629 | operator. */ | |
7630 | return | |
4bb8ca28 MM |
7631 | cp_parser_parameter_declaration (parser, /*template_parm_p=*/true, |
7632 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
7633 | } |
7634 | ||
7635 | /* Parse a type-parameter. | |
7636 | ||
7637 | type-parameter: | |
7638 | class identifier [opt] | |
7639 | class identifier [opt] = type-id | |
7640 | typename identifier [opt] | |
7641 | typename identifier [opt] = type-id | |
7642 | template < template-parameter-list > class identifier [opt] | |
7643 | template < template-parameter-list > class identifier [opt] | |
7644 | = id-expression | |
7645 | ||
7646 | Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The | |
7647 | TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is | |
7648 | the declaration of the parameter. */ | |
7649 | ||
7650 | static tree | |
94edc4ab | 7651 | cp_parser_type_parameter (cp_parser* parser) |
a723baf1 MM |
7652 | { |
7653 | cp_token *token; | |
7654 | tree parameter; | |
7655 | ||
7656 | /* Look for a keyword to tell us what kind of parameter this is. */ | |
7657 | token = cp_parser_require (parser, CPP_KEYWORD, | |
8a6393df | 7658 | "`class', `typename', or `template'"); |
a723baf1 MM |
7659 | if (!token) |
7660 | return error_mark_node; | |
7661 | ||
7662 | switch (token->keyword) | |
7663 | { | |
7664 | case RID_CLASS: | |
7665 | case RID_TYPENAME: | |
7666 | { | |
7667 | tree identifier; | |
7668 | tree default_argument; | |
7669 | ||
7670 | /* If the next token is an identifier, then it names the | |
7671 | parameter. */ | |
7672 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
7673 | identifier = cp_parser_identifier (parser); | |
7674 | else | |
7675 | identifier = NULL_TREE; | |
7676 | ||
7677 | /* Create the parameter. */ | |
7678 | parameter = finish_template_type_parm (class_type_node, identifier); | |
7679 | ||
7680 | /* If the next token is an `=', we have a default argument. */ | |
7681 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
7682 | { | |
7683 | /* Consume the `=' token. */ | |
7684 | cp_lexer_consume_token (parser->lexer); | |
34cd5ae7 | 7685 | /* Parse the default-argument. */ |
a723baf1 MM |
7686 | default_argument = cp_parser_type_id (parser); |
7687 | } | |
7688 | else | |
7689 | default_argument = NULL_TREE; | |
7690 | ||
7691 | /* Create the combined representation of the parameter and the | |
7692 | default argument. */ | |
c67d36d0 | 7693 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
7694 | } |
7695 | break; | |
7696 | ||
7697 | case RID_TEMPLATE: | |
7698 | { | |
7699 | tree parameter_list; | |
7700 | tree identifier; | |
7701 | tree default_argument; | |
7702 | ||
7703 | /* Look for the `<'. */ | |
7704 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
7705 | /* Parse the template-parameter-list. */ | |
7706 | begin_template_parm_list (); | |
7707 | parameter_list | |
7708 | = cp_parser_template_parameter_list (parser); | |
7709 | parameter_list = end_template_parm_list (parameter_list); | |
7710 | /* Look for the `>'. */ | |
7711 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
7712 | /* Look for the `class' keyword. */ | |
7713 | cp_parser_require_keyword (parser, RID_CLASS, "`class'"); | |
7714 | /* If the next token is an `=', then there is a | |
7715 | default-argument. If the next token is a `>', we are at | |
7716 | the end of the parameter-list. If the next token is a `,', | |
7717 | then we are at the end of this parameter. */ | |
7718 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ) | |
7719 | && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER) | |
7720 | && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
7721 | identifier = cp_parser_identifier (parser); | |
7722 | else | |
7723 | identifier = NULL_TREE; | |
7724 | /* Create the template parameter. */ | |
7725 | parameter = finish_template_template_parm (class_type_node, | |
7726 | identifier); | |
7727 | ||
7728 | /* If the next token is an `=', then there is a | |
7729 | default-argument. */ | |
7730 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
7731 | { | |
b0bc6e8e KL |
7732 | bool is_template; |
7733 | ||
a723baf1 MM |
7734 | /* Consume the `='. */ |
7735 | cp_lexer_consume_token (parser->lexer); | |
7736 | /* Parse the id-expression. */ | |
7737 | default_argument | |
7738 | = cp_parser_id_expression (parser, | |
7739 | /*template_keyword_p=*/false, | |
7740 | /*check_dependency_p=*/true, | |
b0bc6e8e | 7741 | /*template_p=*/&is_template, |
f3c2dfc6 | 7742 | /*declarator_p=*/false); |
a3a503a5 GB |
7743 | if (TREE_CODE (default_argument) == TYPE_DECL) |
7744 | /* If the id-expression was a template-id that refers to | |
7745 | a template-class, we already have the declaration here, | |
7746 | so no further lookup is needed. */ | |
7747 | ; | |
7748 | else | |
7749 | /* Look up the name. */ | |
7750 | default_argument | |
7751 | = cp_parser_lookup_name (parser, default_argument, | |
7752 | /*is_type=*/false, | |
7753 | /*is_template=*/is_template, | |
7754 | /*is_namespace=*/false, | |
7755 | /*check_dependency=*/true); | |
a723baf1 MM |
7756 | /* See if the default argument is valid. */ |
7757 | default_argument | |
7758 | = check_template_template_default_arg (default_argument); | |
7759 | } | |
7760 | else | |
7761 | default_argument = NULL_TREE; | |
7762 | ||
7763 | /* Create the combined representation of the parameter and the | |
7764 | default argument. */ | |
c67d36d0 | 7765 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
7766 | } |
7767 | break; | |
7768 | ||
7769 | default: | |
7770 | /* Anything else is an error. */ | |
7771 | cp_parser_error (parser, | |
7772 | "expected `class', `typename', or `template'"); | |
7773 | parameter = error_mark_node; | |
7774 | } | |
7775 | ||
7776 | return parameter; | |
7777 | } | |
7778 | ||
7779 | /* Parse a template-id. | |
7780 | ||
7781 | template-id: | |
7782 | template-name < template-argument-list [opt] > | |
7783 | ||
7784 | If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the | |
7785 | `template' keyword. In this case, a TEMPLATE_ID_EXPR will be | |
7786 | returned. Otherwise, if the template-name names a function, or set | |
7787 | of functions, returns a TEMPLATE_ID_EXPR. If the template-name | |
7788 | names a class, returns a TYPE_DECL for the specialization. | |
7789 | ||
7790 | If CHECK_DEPENDENCY_P is FALSE, names are looked up in | |
7791 | uninstantiated templates. */ | |
7792 | ||
7793 | static tree | |
7794 | cp_parser_template_id (cp_parser *parser, | |
7795 | bool template_keyword_p, | |
a668c6ad MM |
7796 | bool check_dependency_p, |
7797 | bool is_declaration) | |
a723baf1 MM |
7798 | { |
7799 | tree template; | |
7800 | tree arguments; | |
a723baf1 | 7801 | tree template_id; |
a723baf1 MM |
7802 | ptrdiff_t start_of_id; |
7803 | tree access_check = NULL_TREE; | |
f4abade9 | 7804 | cp_token *next_token, *next_token_2; |
a668c6ad | 7805 | bool is_identifier; |
a723baf1 MM |
7806 | |
7807 | /* If the next token corresponds to a template-id, there is no need | |
7808 | to reparse it. */ | |
2050a1bb MM |
7809 | next_token = cp_lexer_peek_token (parser->lexer); |
7810 | if (next_token->type == CPP_TEMPLATE_ID) | |
a723baf1 MM |
7811 | { |
7812 | tree value; | |
7813 | tree check; | |
7814 | ||
7815 | /* Get the stored value. */ | |
7816 | value = cp_lexer_consume_token (parser->lexer)->value; | |
7817 | /* Perform any access checks that were deferred. */ | |
7818 | for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check)) | |
cf22909c KL |
7819 | perform_or_defer_access_check (TREE_PURPOSE (check), |
7820 | TREE_VALUE (check)); | |
a723baf1 MM |
7821 | /* Return the stored value. */ |
7822 | return TREE_VALUE (value); | |
7823 | } | |
7824 | ||
2050a1bb MM |
7825 | /* Avoid performing name lookup if there is no possibility of |
7826 | finding a template-id. */ | |
7827 | if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR) | |
7828 | || (next_token->type == CPP_NAME | |
f4abade9 GB |
7829 | && !cp_parser_nth_token_starts_template_argument_list_p |
7830 | (parser, 2))) | |
2050a1bb MM |
7831 | { |
7832 | cp_parser_error (parser, "expected template-id"); | |
7833 | return error_mark_node; | |
7834 | } | |
7835 | ||
a723baf1 MM |
7836 | /* Remember where the template-id starts. */ |
7837 | if (cp_parser_parsing_tentatively (parser) | |
7838 | && !cp_parser_committed_to_tentative_parse (parser)) | |
7839 | { | |
2050a1bb | 7840 | next_token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
7841 | start_of_id = cp_lexer_token_difference (parser->lexer, |
7842 | parser->lexer->first_token, | |
7843 | next_token); | |
a723baf1 MM |
7844 | } |
7845 | else | |
7846 | start_of_id = -1; | |
7847 | ||
8d241e0b | 7848 | push_deferring_access_checks (dk_deferred); |
cf22909c | 7849 | |
a723baf1 | 7850 | /* Parse the template-name. */ |
a668c6ad | 7851 | is_identifier = false; |
a723baf1 | 7852 | template = cp_parser_template_name (parser, template_keyword_p, |
a668c6ad MM |
7853 | check_dependency_p, |
7854 | is_declaration, | |
7855 | &is_identifier); | |
7856 | if (template == error_mark_node || is_identifier) | |
cf22909c KL |
7857 | { |
7858 | pop_deferring_access_checks (); | |
a668c6ad | 7859 | return template; |
cf22909c | 7860 | } |
a723baf1 | 7861 | |
f4abade9 GB |
7862 | /* If we find the sequence `[:' after a template-name, it's probably |
7863 | a digraph-typo for `< ::'. Substitute the tokens and check if we can | |
7864 | parse correctly the argument list. */ | |
7865 | next_token = cp_lexer_peek_nth_token (parser->lexer, 1); | |
7866 | next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2); | |
7867 | if (next_token->type == CPP_OPEN_SQUARE | |
7868 | && next_token->flags & DIGRAPH | |
7869 | && next_token_2->type == CPP_COLON | |
7870 | && !(next_token_2->flags & PREV_WHITE)) | |
cf22909c | 7871 | { |
f4abade9 GB |
7872 | cp_parser_parse_tentatively (parser); |
7873 | /* Change `:' into `::'. */ | |
7874 | next_token_2->type = CPP_SCOPE; | |
7875 | /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is | |
7876 | CPP_LESS. */ | |
7877 | cp_lexer_consume_token (parser->lexer); | |
7878 | /* Parse the arguments. */ | |
7879 | arguments = cp_parser_enclosed_template_argument_list (parser); | |
7880 | if (!cp_parser_parse_definitely (parser)) | |
7881 | { | |
7882 | /* If we couldn't parse an argument list, then we revert our changes | |
7883 | and return simply an error. Maybe this is not a template-id | |
7884 | after all. */ | |
7885 | next_token_2->type = CPP_COLON; | |
7886 | cp_parser_error (parser, "expected `<'"); | |
7887 | pop_deferring_access_checks (); | |
7888 | return error_mark_node; | |
7889 | } | |
7890 | /* Otherwise, emit an error about the invalid digraph, but continue | |
7891 | parsing because we got our argument list. */ | |
7892 | pedwarn ("`<::' cannot begin a template-argument list"); | |
7893 | inform ("`<:' is an alternate spelling for `['. Insert whitespace " | |
7894 | "between `<' and `::'"); | |
7895 | if (!flag_permissive) | |
7896 | { | |
7897 | static bool hint; | |
7898 | if (!hint) | |
7899 | { | |
7900 | inform ("(if you use `-fpermissive' G++ will accept your code)"); | |
7901 | hint = true; | |
7902 | } | |
7903 | } | |
7904 | } | |
7905 | else | |
7906 | { | |
7907 | /* Look for the `<' that starts the template-argument-list. */ | |
7908 | if (!cp_parser_require (parser, CPP_LESS, "`<'")) | |
7909 | { | |
7910 | pop_deferring_access_checks (); | |
7911 | return error_mark_node; | |
7912 | } | |
7913 | /* Parse the arguments. */ | |
7914 | arguments = cp_parser_enclosed_template_argument_list (parser); | |
cf22909c | 7915 | } |
a723baf1 MM |
7916 | |
7917 | /* Build a representation of the specialization. */ | |
7918 | if (TREE_CODE (template) == IDENTIFIER_NODE) | |
7919 | template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments); | |
7920 | else if (DECL_CLASS_TEMPLATE_P (template) | |
7921 | || DECL_TEMPLATE_TEMPLATE_PARM_P (template)) | |
7922 | template_id | |
7923 | = finish_template_type (template, arguments, | |
7924 | cp_lexer_next_token_is (parser->lexer, | |
7925 | CPP_SCOPE)); | |
7926 | else | |
7927 | { | |
7928 | /* If it's not a class-template or a template-template, it should be | |
7929 | a function-template. */ | |
7930 | my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template) | |
7931 | || TREE_CODE (template) == OVERLOAD | |
7932 | || BASELINK_P (template)), | |
7933 | 20010716); | |
7934 | ||
7935 | template_id = lookup_template_function (template, arguments); | |
7936 | } | |
7937 | ||
cf22909c KL |
7938 | /* Retrieve any deferred checks. Do not pop this access checks yet |
7939 | so the memory will not be reclaimed during token replacing below. */ | |
7940 | access_check = get_deferred_access_checks (); | |
7941 | ||
a723baf1 MM |
7942 | /* If parsing tentatively, replace the sequence of tokens that makes |
7943 | up the template-id with a CPP_TEMPLATE_ID token. That way, | |
7944 | should we re-parse the token stream, we will not have to repeat | |
7945 | the effort required to do the parse, nor will we issue duplicate | |
7946 | error messages about problems during instantiation of the | |
7947 | template. */ | |
7948 | if (start_of_id >= 0) | |
7949 | { | |
7950 | cp_token *token; | |
a723baf1 MM |
7951 | |
7952 | /* Find the token that corresponds to the start of the | |
7953 | template-id. */ | |
7954 | token = cp_lexer_advance_token (parser->lexer, | |
7955 | parser->lexer->first_token, | |
7956 | start_of_id); | |
7957 | ||
a723baf1 MM |
7958 | /* Reset the contents of the START_OF_ID token. */ |
7959 | token->type = CPP_TEMPLATE_ID; | |
7960 | token->value = build_tree_list (access_check, template_id); | |
7961 | token->keyword = RID_MAX; | |
7962 | /* Purge all subsequent tokens. */ | |
7963 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
7964 | } | |
7965 | ||
cf22909c | 7966 | pop_deferring_access_checks (); |
a723baf1 MM |
7967 | return template_id; |
7968 | } | |
7969 | ||
7970 | /* Parse a template-name. | |
7971 | ||
7972 | template-name: | |
7973 | identifier | |
7974 | ||
7975 | The standard should actually say: | |
7976 | ||
7977 | template-name: | |
7978 | identifier | |
7979 | operator-function-id | |
a723baf1 MM |
7980 | |
7981 | A defect report has been filed about this issue. | |
7982 | ||
0d956474 GB |
7983 | A conversion-function-id cannot be a template name because they cannot |
7984 | be part of a template-id. In fact, looking at this code: | |
7985 | ||
7986 | a.operator K<int>() | |
7987 | ||
7988 | the conversion-function-id is "operator K<int>", and K<int> is a type-id. | |
7989 | It is impossible to call a templated conversion-function-id with an | |
7990 | explicit argument list, since the only allowed template parameter is | |
7991 | the type to which it is converting. | |
7992 | ||
a723baf1 MM |
7993 | If TEMPLATE_KEYWORD_P is true, then we have just seen the |
7994 | `template' keyword, in a construction like: | |
7995 | ||
7996 | T::template f<3>() | |
7997 | ||
7998 | In that case `f' is taken to be a template-name, even though there | |
7999 | is no way of knowing for sure. | |
8000 | ||
8001 | Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the | |
8002 | name refers to a set of overloaded functions, at least one of which | |
8003 | is a template, or an IDENTIFIER_NODE with the name of the template, | |
8004 | if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE, | |
8005 | names are looked up inside uninstantiated templates. */ | |
8006 | ||
8007 | static tree | |
94edc4ab NN |
8008 | cp_parser_template_name (cp_parser* parser, |
8009 | bool template_keyword_p, | |
a668c6ad MM |
8010 | bool check_dependency_p, |
8011 | bool is_declaration, | |
8012 | bool *is_identifier) | |
a723baf1 MM |
8013 | { |
8014 | tree identifier; | |
8015 | tree decl; | |
8016 | tree fns; | |
8017 | ||
8018 | /* If the next token is `operator', then we have either an | |
8019 | operator-function-id or a conversion-function-id. */ | |
8020 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR)) | |
8021 | { | |
8022 | /* We don't know whether we're looking at an | |
8023 | operator-function-id or a conversion-function-id. */ | |
8024 | cp_parser_parse_tentatively (parser); | |
8025 | /* Try an operator-function-id. */ | |
8026 | identifier = cp_parser_operator_function_id (parser); | |
8027 | /* If that didn't work, try a conversion-function-id. */ | |
8028 | if (!cp_parser_parse_definitely (parser)) | |
0d956474 GB |
8029 | { |
8030 | cp_parser_error (parser, "expected template-name"); | |
8031 | return error_mark_node; | |
8032 | } | |
a723baf1 MM |
8033 | } |
8034 | /* Look for the identifier. */ | |
8035 | else | |
8036 | identifier = cp_parser_identifier (parser); | |
8037 | ||
8038 | /* If we didn't find an identifier, we don't have a template-id. */ | |
8039 | if (identifier == error_mark_node) | |
8040 | return error_mark_node; | |
8041 | ||
8042 | /* If the name immediately followed the `template' keyword, then it | |
8043 | is a template-name. However, if the next token is not `<', then | |
8044 | we do not treat it as a template-name, since it is not being used | |
8045 | as part of a template-id. This enables us to handle constructs | |
8046 | like: | |
8047 | ||
8048 | template <typename T> struct S { S(); }; | |
8049 | template <typename T> S<T>::S(); | |
8050 | ||
8051 | correctly. We would treat `S' as a template -- if it were `S<T>' | |
8052 | -- but we do not if there is no `<'. */ | |
a668c6ad MM |
8053 | |
8054 | if (processing_template_decl | |
f4abade9 | 8055 | && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)) |
a668c6ad MM |
8056 | { |
8057 | /* In a declaration, in a dependent context, we pretend that the | |
8058 | "template" keyword was present in order to improve error | |
8059 | recovery. For example, given: | |
8060 | ||
8061 | template <typename T> void f(T::X<int>); | |
8062 | ||
8063 | we want to treat "X<int>" as a template-id. */ | |
8064 | if (is_declaration | |
8065 | && !template_keyword_p | |
8066 | && parser->scope && TYPE_P (parser->scope) | |
8067 | && dependent_type_p (parser->scope)) | |
8068 | { | |
8069 | ptrdiff_t start; | |
8070 | cp_token* token; | |
8071 | /* Explain what went wrong. */ | |
8072 | error ("non-template `%D' used as template", identifier); | |
8073 | error ("(use `%T::template %D' to indicate that it is a template)", | |
8074 | parser->scope, identifier); | |
8075 | /* If parsing tentatively, find the location of the "<" | |
8076 | token. */ | |
8077 | if (cp_parser_parsing_tentatively (parser) | |
8078 | && !cp_parser_committed_to_tentative_parse (parser)) | |
8079 | { | |
8080 | cp_parser_simulate_error (parser); | |
8081 | token = cp_lexer_peek_token (parser->lexer); | |
8082 | token = cp_lexer_prev_token (parser->lexer, token); | |
8083 | start = cp_lexer_token_difference (parser->lexer, | |
8084 | parser->lexer->first_token, | |
8085 | token); | |
8086 | } | |
8087 | else | |
8088 | start = -1; | |
8089 | /* Parse the template arguments so that we can issue error | |
8090 | messages about them. */ | |
8091 | cp_lexer_consume_token (parser->lexer); | |
8092 | cp_parser_enclosed_template_argument_list (parser); | |
8093 | /* Skip tokens until we find a good place from which to | |
8094 | continue parsing. */ | |
8095 | cp_parser_skip_to_closing_parenthesis (parser, | |
8096 | /*recovering=*/true, | |
8097 | /*or_comma=*/true, | |
8098 | /*consume_paren=*/false); | |
8099 | /* If parsing tentatively, permanently remove the | |
8100 | template argument list. That will prevent duplicate | |
8101 | error messages from being issued about the missing | |
8102 | "template" keyword. */ | |
8103 | if (start >= 0) | |
8104 | { | |
8105 | token = cp_lexer_advance_token (parser->lexer, | |
8106 | parser->lexer->first_token, | |
8107 | start); | |
8108 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
8109 | } | |
8110 | if (is_identifier) | |
8111 | *is_identifier = true; | |
8112 | return identifier; | |
8113 | } | |
8114 | if (template_keyword_p) | |
8115 | return identifier; | |
8116 | } | |
a723baf1 MM |
8117 | |
8118 | /* Look up the name. */ | |
8119 | decl = cp_parser_lookup_name (parser, identifier, | |
a723baf1 | 8120 | /*is_type=*/false, |
b0bc6e8e | 8121 | /*is_template=*/false, |
eea9800f | 8122 | /*is_namespace=*/false, |
a723baf1 MM |
8123 | check_dependency_p); |
8124 | decl = maybe_get_template_decl_from_type_decl (decl); | |
8125 | ||
8126 | /* If DECL is a template, then the name was a template-name. */ | |
8127 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
8128 | ; | |
8129 | else | |
8130 | { | |
8131 | /* The standard does not explicitly indicate whether a name that | |
8132 | names a set of overloaded declarations, some of which are | |
8133 | templates, is a template-name. However, such a name should | |
8134 | be a template-name; otherwise, there is no way to form a | |
8135 | template-id for the overloaded templates. */ | |
8136 | fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl; | |
8137 | if (TREE_CODE (fns) == OVERLOAD) | |
8138 | { | |
8139 | tree fn; | |
8140 | ||
8141 | for (fn = fns; fn; fn = OVL_NEXT (fn)) | |
8142 | if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL) | |
8143 | break; | |
8144 | } | |
8145 | else | |
8146 | { | |
8147 | /* Otherwise, the name does not name a template. */ | |
8148 | cp_parser_error (parser, "expected template-name"); | |
8149 | return error_mark_node; | |
8150 | } | |
8151 | } | |
8152 | ||
8153 | /* If DECL is dependent, and refers to a function, then just return | |
8154 | its name; we will look it up again during template instantiation. */ | |
8155 | if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl)) | |
8156 | { | |
8157 | tree scope = CP_DECL_CONTEXT (get_first_fn (decl)); | |
1fb3244a | 8158 | if (TYPE_P (scope) && dependent_type_p (scope)) |
a723baf1 MM |
8159 | return identifier; |
8160 | } | |
8161 | ||
8162 | return decl; | |
8163 | } | |
8164 | ||
8165 | /* Parse a template-argument-list. | |
8166 | ||
8167 | template-argument-list: | |
8168 | template-argument | |
8169 | template-argument-list , template-argument | |
8170 | ||
04c06002 | 8171 | Returns a TREE_VEC containing the arguments. */ |
a723baf1 MM |
8172 | |
8173 | static tree | |
94edc4ab | 8174 | cp_parser_template_argument_list (cp_parser* parser) |
a723baf1 | 8175 | { |
bf12d54d NS |
8176 | tree fixed_args[10]; |
8177 | unsigned n_args = 0; | |
8178 | unsigned alloced = 10; | |
8179 | tree *arg_ary = fixed_args; | |
8180 | tree vec; | |
4bb8ca28 | 8181 | bool saved_in_template_argument_list_p; |
a723baf1 | 8182 | |
4bb8ca28 MM |
8183 | saved_in_template_argument_list_p = parser->in_template_argument_list_p; |
8184 | parser->in_template_argument_list_p = true; | |
bf12d54d | 8185 | do |
a723baf1 MM |
8186 | { |
8187 | tree argument; | |
8188 | ||
bf12d54d | 8189 | if (n_args) |
04c06002 | 8190 | /* Consume the comma. */ |
bf12d54d NS |
8191 | cp_lexer_consume_token (parser->lexer); |
8192 | ||
a723baf1 MM |
8193 | /* Parse the template-argument. */ |
8194 | argument = cp_parser_template_argument (parser); | |
bf12d54d NS |
8195 | if (n_args == alloced) |
8196 | { | |
8197 | alloced *= 2; | |
8198 | ||
8199 | if (arg_ary == fixed_args) | |
8200 | { | |
8201 | arg_ary = xmalloc (sizeof (tree) * alloced); | |
8202 | memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); | |
8203 | } | |
8204 | else | |
8205 | arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced); | |
8206 | } | |
8207 | arg_ary[n_args++] = argument; | |
a723baf1 | 8208 | } |
bf12d54d NS |
8209 | while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); |
8210 | ||
8211 | vec = make_tree_vec (n_args); | |
a723baf1 | 8212 | |
bf12d54d NS |
8213 | while (n_args--) |
8214 | TREE_VEC_ELT (vec, n_args) = arg_ary[n_args]; | |
8215 | ||
8216 | if (arg_ary != fixed_args) | |
8217 | free (arg_ary); | |
4bb8ca28 | 8218 | parser->in_template_argument_list_p = saved_in_template_argument_list_p; |
bf12d54d | 8219 | return vec; |
a723baf1 MM |
8220 | } |
8221 | ||
8222 | /* Parse a template-argument. | |
8223 | ||
8224 | template-argument: | |
8225 | assignment-expression | |
8226 | type-id | |
8227 | id-expression | |
8228 | ||
8229 | The representation is that of an assignment-expression, type-id, or | |
8230 | id-expression -- except that the qualified id-expression is | |
8231 | evaluated, so that the value returned is either a DECL or an | |
d17811fd MM |
8232 | OVERLOAD. |
8233 | ||
8234 | Although the standard says "assignment-expression", it forbids | |
8235 | throw-expressions or assignments in the template argument. | |
8236 | Therefore, we use "conditional-expression" instead. */ | |
a723baf1 MM |
8237 | |
8238 | static tree | |
94edc4ab | 8239 | cp_parser_template_argument (cp_parser* parser) |
a723baf1 MM |
8240 | { |
8241 | tree argument; | |
8242 | bool template_p; | |
d17811fd | 8243 | bool address_p; |
4d5297fa | 8244 | bool maybe_type_id = false; |
d17811fd | 8245 | cp_token *token; |
b3445994 | 8246 | cp_id_kind idk; |
d17811fd | 8247 | tree qualifying_class; |
a723baf1 MM |
8248 | |
8249 | /* There's really no way to know what we're looking at, so we just | |
8250 | try each alternative in order. | |
8251 | ||
8252 | [temp.arg] | |
8253 | ||
8254 | In a template-argument, an ambiguity between a type-id and an | |
8255 | expression is resolved to a type-id, regardless of the form of | |
8256 | the corresponding template-parameter. | |
8257 | ||
8258 | Therefore, we try a type-id first. */ | |
8259 | cp_parser_parse_tentatively (parser); | |
a723baf1 | 8260 | argument = cp_parser_type_id (parser); |
4d5297fa GB |
8261 | /* If there was no error parsing the type-id but the next token is a '>>', |
8262 | we probably found a typo for '> >'. But there are type-id which are | |
8263 | also valid expressions. For instance: | |
8264 | ||
8265 | struct X { int operator >> (int); }; | |
8266 | template <int V> struct Foo {}; | |
8267 | Foo<X () >> 5> r; | |
8268 | ||
8269 | Here 'X()' is a valid type-id of a function type, but the user just | |
8270 | wanted to write the expression "X() >> 5". Thus, we remember that we | |
8271 | found a valid type-id, but we still try to parse the argument as an | |
8272 | expression to see what happens. */ | |
8273 | if (!cp_parser_error_occurred (parser) | |
8274 | && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) | |
8275 | { | |
8276 | maybe_type_id = true; | |
8277 | cp_parser_abort_tentative_parse (parser); | |
8278 | } | |
8279 | else | |
8280 | { | |
8281 | /* If the next token isn't a `,' or a `>', then this argument wasn't | |
8282 | really finished. This means that the argument is not a valid | |
8283 | type-id. */ | |
8284 | if (!cp_parser_next_token_ends_template_argument_p (parser)) | |
8285 | cp_parser_error (parser, "expected template-argument"); | |
8286 | /* If that worked, we're done. */ | |
8287 | if (cp_parser_parse_definitely (parser)) | |
8288 | return argument; | |
8289 | } | |
a723baf1 MM |
8290 | /* We're still not sure what the argument will be. */ |
8291 | cp_parser_parse_tentatively (parser); | |
8292 | /* Try a template. */ | |
8293 | argument = cp_parser_id_expression (parser, | |
8294 | /*template_keyword_p=*/false, | |
8295 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
8296 | &template_p, |
8297 | /*declarator_p=*/false); | |
a723baf1 MM |
8298 | /* If the next token isn't a `,' or a `>', then this argument wasn't |
8299 | really finished. */ | |
d17811fd | 8300 | if (!cp_parser_next_token_ends_template_argument_p (parser)) |
a723baf1 MM |
8301 | cp_parser_error (parser, "expected template-argument"); |
8302 | if (!cp_parser_error_occurred (parser)) | |
8303 | { | |
8304 | /* Figure out what is being referred to. */ | |
5b4acce1 KL |
8305 | argument = cp_parser_lookup_name (parser, argument, |
8306 | /*is_type=*/false, | |
8307 | /*is_template=*/template_p, | |
8308 | /*is_namespace=*/false, | |
8309 | /*check_dependency=*/true); | |
8310 | if (TREE_CODE (argument) != TEMPLATE_DECL | |
8311 | && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE) | |
a723baf1 MM |
8312 | cp_parser_error (parser, "expected template-name"); |
8313 | } | |
8314 | if (cp_parser_parse_definitely (parser)) | |
8315 | return argument; | |
d17811fd MM |
8316 | /* It must be a non-type argument. There permitted cases are given |
8317 | in [temp.arg.nontype]: | |
8318 | ||
8319 | -- an integral constant-expression of integral or enumeration | |
8320 | type; or | |
8321 | ||
8322 | -- the name of a non-type template-parameter; or | |
8323 | ||
8324 | -- the name of an object or function with external linkage... | |
8325 | ||
8326 | -- the address of an object or function with external linkage... | |
8327 | ||
04c06002 | 8328 | -- a pointer to member... */ |
d17811fd MM |
8329 | /* Look for a non-type template parameter. */ |
8330 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
8331 | { | |
8332 | cp_parser_parse_tentatively (parser); | |
8333 | argument = cp_parser_primary_expression (parser, | |
8334 | &idk, | |
8335 | &qualifying_class); | |
8336 | if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX | |
8337 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
8338 | cp_parser_simulate_error (parser); | |
8339 | if (cp_parser_parse_definitely (parser)) | |
8340 | return argument; | |
8341 | } | |
8342 | /* If the next token is "&", the argument must be the address of an | |
8343 | object or function with external linkage. */ | |
8344 | address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND); | |
8345 | if (address_p) | |
8346 | cp_lexer_consume_token (parser->lexer); | |
8347 | /* See if we might have an id-expression. */ | |
8348 | token = cp_lexer_peek_token (parser->lexer); | |
8349 | if (token->type == CPP_NAME | |
8350 | || token->keyword == RID_OPERATOR | |
8351 | || token->type == CPP_SCOPE | |
8352 | || token->type == CPP_TEMPLATE_ID | |
8353 | || token->type == CPP_NESTED_NAME_SPECIFIER) | |
8354 | { | |
8355 | cp_parser_parse_tentatively (parser); | |
8356 | argument = cp_parser_primary_expression (parser, | |
8357 | &idk, | |
8358 | &qualifying_class); | |
8359 | if (cp_parser_error_occurred (parser) | |
8360 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
8361 | cp_parser_abort_tentative_parse (parser); | |
8362 | else | |
8363 | { | |
8364 | if (qualifying_class) | |
8365 | argument = finish_qualified_id_expr (qualifying_class, | |
8366 | argument, | |
8367 | /*done=*/true, | |
8368 | address_p); | |
8369 | if (TREE_CODE (argument) == VAR_DECL) | |
8370 | { | |
8371 | /* A variable without external linkage might still be a | |
8372 | valid constant-expression, so no error is issued here | |
8373 | if the external-linkage check fails. */ | |
8374 | if (!DECL_EXTERNAL_LINKAGE_P (argument)) | |
8375 | cp_parser_simulate_error (parser); | |
8376 | } | |
8377 | else if (is_overloaded_fn (argument)) | |
8378 | /* All overloaded functions are allowed; if the external | |
8379 | linkage test does not pass, an error will be issued | |
8380 | later. */ | |
8381 | ; | |
8382 | else if (address_p | |
8383 | && (TREE_CODE (argument) == OFFSET_REF | |
8384 | || TREE_CODE (argument) == SCOPE_REF)) | |
8385 | /* A pointer-to-member. */ | |
8386 | ; | |
8387 | else | |
8388 | cp_parser_simulate_error (parser); | |
8389 | ||
8390 | if (cp_parser_parse_definitely (parser)) | |
8391 | { | |
8392 | if (address_p) | |
8393 | argument = build_x_unary_op (ADDR_EXPR, argument); | |
8394 | return argument; | |
8395 | } | |
8396 | } | |
8397 | } | |
8398 | /* If the argument started with "&", there are no other valid | |
8399 | alternatives at this point. */ | |
8400 | if (address_p) | |
8401 | { | |
8402 | cp_parser_error (parser, "invalid non-type template argument"); | |
8403 | return error_mark_node; | |
8404 | } | |
4d5297fa GB |
8405 | /* If the argument wasn't successfully parsed as a type-id followed |
8406 | by '>>', the argument can only be a constant expression now. | |
8407 | Otherwise, we try parsing the constant-expression tentatively, | |
8408 | because the argument could really be a type-id. */ | |
8409 | if (maybe_type_id) | |
8410 | cp_parser_parse_tentatively (parser); | |
d17811fd MM |
8411 | argument = cp_parser_constant_expression (parser, |
8412 | /*allow_non_constant_p=*/false, | |
8413 | /*non_constant_p=*/NULL); | |
4d5297fa GB |
8414 | argument = cp_parser_fold_non_dependent_expr (argument); |
8415 | if (!maybe_type_id) | |
8416 | return argument; | |
8417 | if (!cp_parser_next_token_ends_template_argument_p (parser)) | |
8418 | cp_parser_error (parser, "expected template-argument"); | |
8419 | if (cp_parser_parse_definitely (parser)) | |
8420 | return argument; | |
8421 | /* We did our best to parse the argument as a non type-id, but that | |
8422 | was the only alternative that matched (albeit with a '>' after | |
8423 | it). We can assume it's just a typo from the user, and a | |
8424 | diagnostic will then be issued. */ | |
8425 | return cp_parser_type_id (parser); | |
a723baf1 MM |
8426 | } |
8427 | ||
8428 | /* Parse an explicit-instantiation. | |
8429 | ||
8430 | explicit-instantiation: | |
8431 | template declaration | |
8432 | ||
8433 | Although the standard says `declaration', what it really means is: | |
8434 | ||
8435 | explicit-instantiation: | |
8436 | template decl-specifier-seq [opt] declarator [opt] ; | |
8437 | ||
8438 | Things like `template int S<int>::i = 5, int S<double>::j;' are not | |
8439 | supposed to be allowed. A defect report has been filed about this | |
8440 | issue. | |
8441 | ||
8442 | GNU Extension: | |
8443 | ||
8444 | explicit-instantiation: | |
8445 | storage-class-specifier template | |
8446 | decl-specifier-seq [opt] declarator [opt] ; | |
8447 | function-specifier template | |
8448 | decl-specifier-seq [opt] declarator [opt] ; */ | |
8449 | ||
8450 | static void | |
94edc4ab | 8451 | cp_parser_explicit_instantiation (cp_parser* parser) |
a723baf1 | 8452 | { |
560ad596 | 8453 | int declares_class_or_enum; |
a723baf1 MM |
8454 | tree decl_specifiers; |
8455 | tree attributes; | |
8456 | tree extension_specifier = NULL_TREE; | |
8457 | ||
8458 | /* Look for an (optional) storage-class-specifier or | |
8459 | function-specifier. */ | |
8460 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
8461 | { | |
8462 | extension_specifier | |
8463 | = cp_parser_storage_class_specifier_opt (parser); | |
8464 | if (!extension_specifier) | |
8465 | extension_specifier = cp_parser_function_specifier_opt (parser); | |
8466 | } | |
8467 | ||
8468 | /* Look for the `template' keyword. */ | |
8469 | cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"); | |
8470 | /* Let the front end know that we are processing an explicit | |
8471 | instantiation. */ | |
8472 | begin_explicit_instantiation (); | |
8473 | /* [temp.explicit] says that we are supposed to ignore access | |
8474 | control while processing explicit instantiation directives. */ | |
78757caa | 8475 | push_deferring_access_checks (dk_no_check); |
a723baf1 MM |
8476 | /* Parse a decl-specifier-seq. */ |
8477 | decl_specifiers | |
8478 | = cp_parser_decl_specifier_seq (parser, | |
8479 | CP_PARSER_FLAGS_OPTIONAL, | |
8480 | &attributes, | |
8481 | &declares_class_or_enum); | |
8482 | /* If there was exactly one decl-specifier, and it declared a class, | |
8483 | and there's no declarator, then we have an explicit type | |
8484 | instantiation. */ | |
8485 | if (declares_class_or_enum && cp_parser_declares_only_class_p (parser)) | |
8486 | { | |
8487 | tree type; | |
8488 | ||
8489 | type = check_tag_decl (decl_specifiers); | |
b7fc8b57 KL |
8490 | /* Turn access control back on for names used during |
8491 | template instantiation. */ | |
8492 | pop_deferring_access_checks (); | |
a723baf1 MM |
8493 | if (type) |
8494 | do_type_instantiation (type, extension_specifier, /*complain=*/1); | |
8495 | } | |
8496 | else | |
8497 | { | |
8498 | tree declarator; | |
8499 | tree decl; | |
8500 | ||
8501 | /* Parse the declarator. */ | |
8502 | declarator | |
62b8a44e | 8503 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
8504 | /*ctor_dtor_or_conv_p=*/NULL, |
8505 | /*parenthesized_p=*/NULL); | |
560ad596 MM |
8506 | cp_parser_check_for_definition_in_return_type (declarator, |
8507 | declares_class_or_enum); | |
216bb6e1 MM |
8508 | if (declarator != error_mark_node) |
8509 | { | |
8510 | decl = grokdeclarator (declarator, decl_specifiers, | |
8511 | NORMAL, 0, NULL); | |
8512 | /* Turn access control back on for names used during | |
8513 | template instantiation. */ | |
8514 | pop_deferring_access_checks (); | |
8515 | /* Do the explicit instantiation. */ | |
8516 | do_decl_instantiation (decl, extension_specifier); | |
8517 | } | |
8518 | else | |
8519 | { | |
8520 | pop_deferring_access_checks (); | |
8521 | /* Skip the body of the explicit instantiation. */ | |
8522 | cp_parser_skip_to_end_of_statement (parser); | |
8523 | } | |
a723baf1 MM |
8524 | } |
8525 | /* We're done with the instantiation. */ | |
8526 | end_explicit_instantiation (); | |
a723baf1 | 8527 | |
e0860732 | 8528 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 MM |
8529 | } |
8530 | ||
8531 | /* Parse an explicit-specialization. | |
8532 | ||
8533 | explicit-specialization: | |
8534 | template < > declaration | |
8535 | ||
8536 | Although the standard says `declaration', what it really means is: | |
8537 | ||
8538 | explicit-specialization: | |
8539 | template <> decl-specifier [opt] init-declarator [opt] ; | |
8540 | template <> function-definition | |
8541 | template <> explicit-specialization | |
8542 | template <> template-declaration */ | |
8543 | ||
8544 | static void | |
94edc4ab | 8545 | cp_parser_explicit_specialization (cp_parser* parser) |
a723baf1 MM |
8546 | { |
8547 | /* Look for the `template' keyword. */ | |
8548 | cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"); | |
8549 | /* Look for the `<'. */ | |
8550 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
8551 | /* Look for the `>'. */ | |
8552 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
8553 | /* We have processed another parameter list. */ | |
8554 | ++parser->num_template_parameter_lists; | |
8555 | /* Let the front end know that we are beginning a specialization. */ | |
8556 | begin_specialization (); | |
8557 | ||
8558 | /* If the next keyword is `template', we need to figure out whether | |
8559 | or not we're looking a template-declaration. */ | |
8560 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
8561 | { | |
8562 | if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS | |
8563 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER) | |
8564 | cp_parser_template_declaration_after_export (parser, | |
8565 | /*member_p=*/false); | |
8566 | else | |
8567 | cp_parser_explicit_specialization (parser); | |
8568 | } | |
8569 | else | |
8570 | /* Parse the dependent declaration. */ | |
8571 | cp_parser_single_declaration (parser, | |
8572 | /*member_p=*/false, | |
8573 | /*friend_p=*/NULL); | |
8574 | ||
8575 | /* We're done with the specialization. */ | |
8576 | end_specialization (); | |
8577 | /* We're done with this parameter list. */ | |
8578 | --parser->num_template_parameter_lists; | |
8579 | } | |
8580 | ||
8581 | /* Parse a type-specifier. | |
8582 | ||
8583 | type-specifier: | |
8584 | simple-type-specifier | |
8585 | class-specifier | |
8586 | enum-specifier | |
8587 | elaborated-type-specifier | |
8588 | cv-qualifier | |
8589 | ||
8590 | GNU Extension: | |
8591 | ||
8592 | type-specifier: | |
8593 | __complex__ | |
8594 | ||
8595 | Returns a representation of the type-specifier. If the | |
8596 | type-specifier is a keyword (like `int' or `const', or | |
34cd5ae7 | 8597 | `__complex__') then the corresponding IDENTIFIER_NODE is returned. |
a723baf1 MM |
8598 | For a class-specifier, enum-specifier, or elaborated-type-specifier |
8599 | a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned. | |
8600 | ||
8601 | If IS_FRIEND is TRUE then this type-specifier is being declared a | |
8602 | `friend'. If IS_DECLARATION is TRUE, then this type-specifier is | |
8603 | appearing in a decl-specifier-seq. | |
8604 | ||
8605 | If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a | |
8606 | class-specifier, enum-specifier, or elaborated-type-specifier, then | |
83a00410 | 8607 | *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1 |
560ad596 MM |
8608 | if a type is declared; 2 if it is defined. Otherwise, it is set to |
8609 | zero. | |
a723baf1 MM |
8610 | |
8611 | If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a | |
8612 | cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it | |
8613 | is set to FALSE. */ | |
8614 | ||
8615 | static tree | |
94edc4ab NN |
8616 | cp_parser_type_specifier (cp_parser* parser, |
8617 | cp_parser_flags flags, | |
8618 | bool is_friend, | |
8619 | bool is_declaration, | |
560ad596 | 8620 | int* declares_class_or_enum, |
94edc4ab | 8621 | bool* is_cv_qualifier) |
a723baf1 MM |
8622 | { |
8623 | tree type_spec = NULL_TREE; | |
8624 | cp_token *token; | |
8625 | enum rid keyword; | |
8626 | ||
8627 | /* Assume this type-specifier does not declare a new type. */ | |
8628 | if (declares_class_or_enum) | |
543ca912 | 8629 | *declares_class_or_enum = 0; |
a723baf1 MM |
8630 | /* And that it does not specify a cv-qualifier. */ |
8631 | if (is_cv_qualifier) | |
8632 | *is_cv_qualifier = false; | |
8633 | /* Peek at the next token. */ | |
8634 | token = cp_lexer_peek_token (parser->lexer); | |
8635 | ||
8636 | /* If we're looking at a keyword, we can use that to guide the | |
8637 | production we choose. */ | |
8638 | keyword = token->keyword; | |
8639 | switch (keyword) | |
8640 | { | |
8641 | /* Any of these indicate either a class-specifier, or an | |
8642 | elaborated-type-specifier. */ | |
8643 | case RID_CLASS: | |
8644 | case RID_STRUCT: | |
8645 | case RID_UNION: | |
8646 | case RID_ENUM: | |
8647 | /* Parse tentatively so that we can back up if we don't find a | |
8648 | class-specifier or enum-specifier. */ | |
8649 | cp_parser_parse_tentatively (parser); | |
8650 | /* Look for the class-specifier or enum-specifier. */ | |
8651 | if (keyword == RID_ENUM) | |
8652 | type_spec = cp_parser_enum_specifier (parser); | |
8653 | else | |
8654 | type_spec = cp_parser_class_specifier (parser); | |
8655 | ||
8656 | /* If that worked, we're done. */ | |
8657 | if (cp_parser_parse_definitely (parser)) | |
8658 | { | |
8659 | if (declares_class_or_enum) | |
560ad596 | 8660 | *declares_class_or_enum = 2; |
a723baf1 MM |
8661 | return type_spec; |
8662 | } | |
8663 | ||
8664 | /* Fall through. */ | |
8665 | ||
8666 | case RID_TYPENAME: | |
8667 | /* Look for an elaborated-type-specifier. */ | |
8668 | type_spec = cp_parser_elaborated_type_specifier (parser, | |
8669 | is_friend, | |
8670 | is_declaration); | |
8671 | /* We're declaring a class or enum -- unless we're using | |
8672 | `typename'. */ | |
8673 | if (declares_class_or_enum && keyword != RID_TYPENAME) | |
560ad596 | 8674 | *declares_class_or_enum = 1; |
a723baf1 MM |
8675 | return type_spec; |
8676 | ||
8677 | case RID_CONST: | |
8678 | case RID_VOLATILE: | |
8679 | case RID_RESTRICT: | |
8680 | type_spec = cp_parser_cv_qualifier_opt (parser); | |
8681 | /* Even though we call a routine that looks for an optional | |
8682 | qualifier, we know that there should be one. */ | |
8683 | my_friendly_assert (type_spec != NULL, 20000328); | |
8684 | /* This type-specifier was a cv-qualified. */ | |
8685 | if (is_cv_qualifier) | |
8686 | *is_cv_qualifier = true; | |
8687 | ||
8688 | return type_spec; | |
8689 | ||
8690 | case RID_COMPLEX: | |
8691 | /* The `__complex__' keyword is a GNU extension. */ | |
8692 | return cp_lexer_consume_token (parser->lexer)->value; | |
8693 | ||
8694 | default: | |
8695 | break; | |
8696 | } | |
8697 | ||
8698 | /* If we do not already have a type-specifier, assume we are looking | |
8699 | at a simple-type-specifier. */ | |
4b0d3cbe MM |
8700 | type_spec = cp_parser_simple_type_specifier (parser, flags, |
8701 | /*identifier_p=*/true); | |
a723baf1 MM |
8702 | |
8703 | /* If we didn't find a type-specifier, and a type-specifier was not | |
8704 | optional in this context, issue an error message. */ | |
8705 | if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
8706 | { | |
8707 | cp_parser_error (parser, "expected type specifier"); | |
8708 | return error_mark_node; | |
8709 | } | |
8710 | ||
8711 | return type_spec; | |
8712 | } | |
8713 | ||
8714 | /* Parse a simple-type-specifier. | |
8715 | ||
8716 | simple-type-specifier: | |
8717 | :: [opt] nested-name-specifier [opt] type-name | |
8718 | :: [opt] nested-name-specifier template template-id | |
8719 | char | |
8720 | wchar_t | |
8721 | bool | |
8722 | short | |
8723 | int | |
8724 | long | |
8725 | signed | |
8726 | unsigned | |
8727 | float | |
8728 | double | |
8729 | void | |
8730 | ||
8731 | GNU Extension: | |
8732 | ||
8733 | simple-type-specifier: | |
8734 | __typeof__ unary-expression | |
8735 | __typeof__ ( type-id ) | |
8736 | ||
8737 | For the various keywords, the value returned is simply the | |
4b0d3cbe MM |
8738 | TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true. |
8739 | For the first two productions, and if IDENTIFIER_P is false, the | |
8740 | value returned is the indicated TYPE_DECL. */ | |
a723baf1 MM |
8741 | |
8742 | static tree | |
4b0d3cbe MM |
8743 | cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags, |
8744 | bool identifier_p) | |
a723baf1 MM |
8745 | { |
8746 | tree type = NULL_TREE; | |
8747 | cp_token *token; | |
8748 | ||
8749 | /* Peek at the next token. */ | |
8750 | token = cp_lexer_peek_token (parser->lexer); | |
8751 | ||
8752 | /* If we're looking at a keyword, things are easy. */ | |
8753 | switch (token->keyword) | |
8754 | { | |
8755 | case RID_CHAR: | |
4b0d3cbe MM |
8756 | type = char_type_node; |
8757 | break; | |
a723baf1 | 8758 | case RID_WCHAR: |
4b0d3cbe MM |
8759 | type = wchar_type_node; |
8760 | break; | |
a723baf1 | 8761 | case RID_BOOL: |
4b0d3cbe MM |
8762 | type = boolean_type_node; |
8763 | break; | |
a723baf1 | 8764 | case RID_SHORT: |
4b0d3cbe MM |
8765 | type = short_integer_type_node; |
8766 | break; | |
a723baf1 | 8767 | case RID_INT: |
4b0d3cbe MM |
8768 | type = integer_type_node; |
8769 | break; | |
a723baf1 | 8770 | case RID_LONG: |
4b0d3cbe MM |
8771 | type = long_integer_type_node; |
8772 | break; | |
a723baf1 | 8773 | case RID_SIGNED: |
4b0d3cbe MM |
8774 | type = integer_type_node; |
8775 | break; | |
a723baf1 | 8776 | case RID_UNSIGNED: |
4b0d3cbe MM |
8777 | type = unsigned_type_node; |
8778 | break; | |
a723baf1 | 8779 | case RID_FLOAT: |
4b0d3cbe MM |
8780 | type = float_type_node; |
8781 | break; | |
a723baf1 | 8782 | case RID_DOUBLE: |
4b0d3cbe MM |
8783 | type = double_type_node; |
8784 | break; | |
a723baf1 | 8785 | case RID_VOID: |
4b0d3cbe MM |
8786 | type = void_type_node; |
8787 | break; | |
a723baf1 MM |
8788 | |
8789 | case RID_TYPEOF: | |
8790 | { | |
8791 | tree operand; | |
8792 | ||
8793 | /* Consume the `typeof' token. */ | |
8794 | cp_lexer_consume_token (parser->lexer); | |
04c06002 | 8795 | /* Parse the operand to `typeof'. */ |
a723baf1 MM |
8796 | operand = cp_parser_sizeof_operand (parser, RID_TYPEOF); |
8797 | /* If it is not already a TYPE, take its type. */ | |
8798 | if (!TYPE_P (operand)) | |
8799 | operand = finish_typeof (operand); | |
8800 | ||
8801 | return operand; | |
8802 | } | |
8803 | ||
8804 | default: | |
8805 | break; | |
8806 | } | |
8807 | ||
4b0d3cbe MM |
8808 | /* If the type-specifier was for a built-in type, we're done. */ |
8809 | if (type) | |
8810 | { | |
8811 | tree id; | |
8812 | ||
8813 | /* Consume the token. */ | |
8814 | id = cp_lexer_consume_token (parser->lexer)->value; | |
0d956474 GB |
8815 | |
8816 | /* There is no valid C++ program where a non-template type is | |
8817 | followed by a "<". That usually indicates that the user thought | |
8818 | that the type was a template. */ | |
8819 | cp_parser_check_for_invalid_template_id (parser, type); | |
8820 | ||
4b0d3cbe MM |
8821 | return identifier_p ? id : TYPE_NAME (type); |
8822 | } | |
8823 | ||
a723baf1 MM |
8824 | /* The type-specifier must be a user-defined type. */ |
8825 | if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) | |
8826 | { | |
8827 | /* Don't gobble tokens or issue error messages if this is an | |
8828 | optional type-specifier. */ | |
8829 | if (flags & CP_PARSER_FLAGS_OPTIONAL) | |
8830 | cp_parser_parse_tentatively (parser); | |
8831 | ||
8832 | /* Look for the optional `::' operator. */ | |
8833 | cp_parser_global_scope_opt (parser, | |
8834 | /*current_scope_valid_p=*/false); | |
8835 | /* Look for the nested-name specifier. */ | |
8836 | cp_parser_nested_name_specifier_opt (parser, | |
8837 | /*typename_keyword_p=*/false, | |
8838 | /*check_dependency_p=*/true, | |
a668c6ad MM |
8839 | /*type_p=*/false, |
8840 | /*is_declaration=*/false); | |
a723baf1 MM |
8841 | /* If we have seen a nested-name-specifier, and the next token |
8842 | is `template', then we are using the template-id production. */ | |
8843 | if (parser->scope | |
8844 | && cp_parser_optional_template_keyword (parser)) | |
8845 | { | |
8846 | /* Look for the template-id. */ | |
8847 | type = cp_parser_template_id (parser, | |
8848 | /*template_keyword_p=*/true, | |
a668c6ad MM |
8849 | /*check_dependency_p=*/true, |
8850 | /*is_declaration=*/false); | |
a723baf1 MM |
8851 | /* If the template-id did not name a type, we are out of |
8852 | luck. */ | |
8853 | if (TREE_CODE (type) != TYPE_DECL) | |
8854 | { | |
8855 | cp_parser_error (parser, "expected template-id for type"); | |
8856 | type = NULL_TREE; | |
8857 | } | |
8858 | } | |
8859 | /* Otherwise, look for a type-name. */ | |
8860 | else | |
4bb8ca28 | 8861 | type = cp_parser_type_name (parser); |
a723baf1 MM |
8862 | /* If it didn't work out, we don't have a TYPE. */ |
8863 | if ((flags & CP_PARSER_FLAGS_OPTIONAL) | |
8864 | && !cp_parser_parse_definitely (parser)) | |
8865 | type = NULL_TREE; | |
8866 | } | |
8867 | ||
8868 | /* If we didn't get a type-name, issue an error message. */ | |
8869 | if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
8870 | { | |
8871 | cp_parser_error (parser, "expected type-name"); | |
8872 | return error_mark_node; | |
8873 | } | |
8874 | ||
a668c6ad MM |
8875 | /* There is no valid C++ program where a non-template type is |
8876 | followed by a "<". That usually indicates that the user thought | |
8877 | that the type was a template. */ | |
4bb8ca28 | 8878 | if (type && type != error_mark_node) |
ee43dab5 | 8879 | cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type)); |
ec75414f | 8880 | |
a723baf1 MM |
8881 | return type; |
8882 | } | |
8883 | ||
8884 | /* Parse a type-name. | |
8885 | ||
8886 | type-name: | |
8887 | class-name | |
8888 | enum-name | |
8889 | typedef-name | |
8890 | ||
8891 | enum-name: | |
8892 | identifier | |
8893 | ||
8894 | typedef-name: | |
8895 | identifier | |
8896 | ||
8897 | Returns a TYPE_DECL for the the type. */ | |
8898 | ||
8899 | static tree | |
94edc4ab | 8900 | cp_parser_type_name (cp_parser* parser) |
a723baf1 MM |
8901 | { |
8902 | tree type_decl; | |
8903 | tree identifier; | |
8904 | ||
8905 | /* We can't know yet whether it is a class-name or not. */ | |
8906 | cp_parser_parse_tentatively (parser); | |
8907 | /* Try a class-name. */ | |
8908 | type_decl = cp_parser_class_name (parser, | |
8909 | /*typename_keyword_p=*/false, | |
8910 | /*template_keyword_p=*/false, | |
8911 | /*type_p=*/false, | |
a723baf1 | 8912 | /*check_dependency_p=*/true, |
a668c6ad MM |
8913 | /*class_head_p=*/false, |
8914 | /*is_declaration=*/false); | |
a723baf1 MM |
8915 | /* If it's not a class-name, keep looking. */ |
8916 | if (!cp_parser_parse_definitely (parser)) | |
8917 | { | |
8918 | /* It must be a typedef-name or an enum-name. */ | |
8919 | identifier = cp_parser_identifier (parser); | |
8920 | if (identifier == error_mark_node) | |
8921 | return error_mark_node; | |
8922 | ||
8923 | /* Look up the type-name. */ | |
8924 | type_decl = cp_parser_lookup_name_simple (parser, identifier); | |
8925 | /* Issue an error if we did not find a type-name. */ | |
8926 | if (TREE_CODE (type_decl) != TYPE_DECL) | |
8927 | { | |
4bb8ca28 MM |
8928 | if (!cp_parser_simulate_error (parser)) |
8929 | cp_parser_name_lookup_error (parser, identifier, type_decl, | |
8930 | "is not a type"); | |
a723baf1 MM |
8931 | type_decl = error_mark_node; |
8932 | } | |
8933 | /* Remember that the name was used in the definition of the | |
8934 | current class so that we can check later to see if the | |
8935 | meaning would have been different after the class was | |
8936 | entirely defined. */ | |
8937 | else if (type_decl != error_mark_node | |
8938 | && !parser->scope) | |
8939 | maybe_note_name_used_in_class (identifier, type_decl); | |
8940 | } | |
8941 | ||
8942 | return type_decl; | |
8943 | } | |
8944 | ||
8945 | ||
8946 | /* Parse an elaborated-type-specifier. Note that the grammar given | |
8947 | here incorporates the resolution to DR68. | |
8948 | ||
8949 | elaborated-type-specifier: | |
8950 | class-key :: [opt] nested-name-specifier [opt] identifier | |
8951 | class-key :: [opt] nested-name-specifier [opt] template [opt] template-id | |
8952 | enum :: [opt] nested-name-specifier [opt] identifier | |
8953 | typename :: [opt] nested-name-specifier identifier | |
8954 | typename :: [opt] nested-name-specifier template [opt] | |
8955 | template-id | |
8956 | ||
360d1b99 MM |
8957 | GNU extension: |
8958 | ||
8959 | elaborated-type-specifier: | |
8960 | class-key attributes :: [opt] nested-name-specifier [opt] identifier | |
8961 | class-key attributes :: [opt] nested-name-specifier [opt] | |
8962 | template [opt] template-id | |
8963 | enum attributes :: [opt] nested-name-specifier [opt] identifier | |
8964 | ||
a723baf1 MM |
8965 | If IS_FRIEND is TRUE, then this elaborated-type-specifier is being |
8966 | declared `friend'. If IS_DECLARATION is TRUE, then this | |
8967 | elaborated-type-specifier appears in a decl-specifiers-seq, i.e., | |
8968 | something is being declared. | |
8969 | ||
8970 | Returns the TYPE specified. */ | |
8971 | ||
8972 | static tree | |
94edc4ab NN |
8973 | cp_parser_elaborated_type_specifier (cp_parser* parser, |
8974 | bool is_friend, | |
8975 | bool is_declaration) | |
a723baf1 MM |
8976 | { |
8977 | enum tag_types tag_type; | |
8978 | tree identifier; | |
8979 | tree type = NULL_TREE; | |
360d1b99 | 8980 | tree attributes = NULL_TREE; |
a723baf1 MM |
8981 | |
8982 | /* See if we're looking at the `enum' keyword. */ | |
8983 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM)) | |
8984 | { | |
8985 | /* Consume the `enum' token. */ | |
8986 | cp_lexer_consume_token (parser->lexer); | |
8987 | /* Remember that it's an enumeration type. */ | |
8988 | tag_type = enum_type; | |
360d1b99 MM |
8989 | /* Parse the attributes. */ |
8990 | attributes = cp_parser_attributes_opt (parser); | |
a723baf1 MM |
8991 | } |
8992 | /* Or, it might be `typename'. */ | |
8993 | else if (cp_lexer_next_token_is_keyword (parser->lexer, | |
8994 | RID_TYPENAME)) | |
8995 | { | |
8996 | /* Consume the `typename' token. */ | |
8997 | cp_lexer_consume_token (parser->lexer); | |
8998 | /* Remember that it's a `typename' type. */ | |
8999 | tag_type = typename_type; | |
9000 | /* The `typename' keyword is only allowed in templates. */ | |
9001 | if (!processing_template_decl) | |
9002 | pedwarn ("using `typename' outside of template"); | |
9003 | } | |
9004 | /* Otherwise it must be a class-key. */ | |
9005 | else | |
9006 | { | |
9007 | tag_type = cp_parser_class_key (parser); | |
9008 | if (tag_type == none_type) | |
9009 | return error_mark_node; | |
360d1b99 MM |
9010 | /* Parse the attributes. */ |
9011 | attributes = cp_parser_attributes_opt (parser); | |
a723baf1 MM |
9012 | } |
9013 | ||
9014 | /* Look for the `::' operator. */ | |
9015 | cp_parser_global_scope_opt (parser, | |
9016 | /*current_scope_valid_p=*/false); | |
9017 | /* Look for the nested-name-specifier. */ | |
9018 | if (tag_type == typename_type) | |
8fa1ad0e MM |
9019 | { |
9020 | if (cp_parser_nested_name_specifier (parser, | |
9021 | /*typename_keyword_p=*/true, | |
9022 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9023 | /*type_p=*/true, |
9024 | is_declaration) | |
8fa1ad0e MM |
9025 | == error_mark_node) |
9026 | return error_mark_node; | |
9027 | } | |
a723baf1 MM |
9028 | else |
9029 | /* Even though `typename' is not present, the proposed resolution | |
9030 | to Core Issue 180 says that in `class A<T>::B', `B' should be | |
9031 | considered a type-name, even if `A<T>' is dependent. */ | |
9032 | cp_parser_nested_name_specifier_opt (parser, | |
9033 | /*typename_keyword_p=*/true, | |
9034 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9035 | /*type_p=*/true, |
9036 | is_declaration); | |
a723baf1 MM |
9037 | /* For everything but enumeration types, consider a template-id. */ |
9038 | if (tag_type != enum_type) | |
9039 | { | |
9040 | bool template_p = false; | |
9041 | tree decl; | |
9042 | ||
9043 | /* Allow the `template' keyword. */ | |
9044 | template_p = cp_parser_optional_template_keyword (parser); | |
9045 | /* If we didn't see `template', we don't know if there's a | |
9046 | template-id or not. */ | |
9047 | if (!template_p) | |
9048 | cp_parser_parse_tentatively (parser); | |
9049 | /* Parse the template-id. */ | |
9050 | decl = cp_parser_template_id (parser, template_p, | |
a668c6ad MM |
9051 | /*check_dependency_p=*/true, |
9052 | is_declaration); | |
a723baf1 MM |
9053 | /* If we didn't find a template-id, look for an ordinary |
9054 | identifier. */ | |
9055 | if (!template_p && !cp_parser_parse_definitely (parser)) | |
9056 | ; | |
9057 | /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is | |
9058 | in effect, then we must assume that, upon instantiation, the | |
9059 | template will correspond to a class. */ | |
9060 | else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR | |
9061 | && tag_type == typename_type) | |
9062 | type = make_typename_type (parser->scope, decl, | |
9063 | /*complain=*/1); | |
9064 | else | |
9065 | type = TREE_TYPE (decl); | |
9066 | } | |
9067 | ||
9068 | /* For an enumeration type, consider only a plain identifier. */ | |
9069 | if (!type) | |
9070 | { | |
9071 | identifier = cp_parser_identifier (parser); | |
9072 | ||
9073 | if (identifier == error_mark_node) | |
eb5abb39 NS |
9074 | { |
9075 | parser->scope = NULL_TREE; | |
9076 | return error_mark_node; | |
9077 | } | |
a723baf1 MM |
9078 | |
9079 | /* For a `typename', we needn't call xref_tag. */ | |
9080 | if (tag_type == typename_type) | |
9081 | return make_typename_type (parser->scope, identifier, | |
9082 | /*complain=*/1); | |
9083 | /* Look up a qualified name in the usual way. */ | |
9084 | if (parser->scope) | |
9085 | { | |
9086 | tree decl; | |
9087 | ||
9088 | /* In an elaborated-type-specifier, names are assumed to name | |
9089 | types, so we set IS_TYPE to TRUE when calling | |
9090 | cp_parser_lookup_name. */ | |
9091 | decl = cp_parser_lookup_name (parser, identifier, | |
a723baf1 | 9092 | /*is_type=*/true, |
b0bc6e8e | 9093 | /*is_template=*/false, |
eea9800f | 9094 | /*is_namespace=*/false, |
a723baf1 | 9095 | /*check_dependency=*/true); |
710b73e6 KL |
9096 | |
9097 | /* If we are parsing friend declaration, DECL may be a | |
9098 | TEMPLATE_DECL tree node here. However, we need to check | |
9099 | whether this TEMPLATE_DECL results in valid code. Consider | |
9100 | the following example: | |
9101 | ||
9102 | namespace N { | |
9103 | template <class T> class C {}; | |
9104 | } | |
9105 | class X { | |
9106 | template <class T> friend class N::C; // #1, valid code | |
9107 | }; | |
9108 | template <class T> class Y { | |
9109 | friend class N::C; // #2, invalid code | |
9110 | }; | |
9111 | ||
9112 | For both case #1 and #2, we arrive at a TEMPLATE_DECL after | |
9113 | name lookup of `N::C'. We see that friend declaration must | |
9114 | be template for the code to be valid. Note that | |
9115 | processing_template_decl does not work here since it is | |
9116 | always 1 for the above two cases. */ | |
9117 | ||
a723baf1 | 9118 | decl = (cp_parser_maybe_treat_template_as_class |
710b73e6 KL |
9119 | (decl, /*tag_name_p=*/is_friend |
9120 | && parser->num_template_parameter_lists)); | |
a723baf1 MM |
9121 | |
9122 | if (TREE_CODE (decl) != TYPE_DECL) | |
9123 | { | |
9124 | error ("expected type-name"); | |
9125 | return error_mark_node; | |
9126 | } | |
560ad596 MM |
9127 | |
9128 | if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE) | |
9129 | check_elaborated_type_specifier | |
4b0d3cbe | 9130 | (tag_type, decl, |
560ad596 MM |
9131 | (parser->num_template_parameter_lists |
9132 | || DECL_SELF_REFERENCE_P (decl))); | |
a723baf1 MM |
9133 | |
9134 | type = TREE_TYPE (decl); | |
9135 | } | |
9136 | else | |
9137 | { | |
9138 | /* An elaborated-type-specifier sometimes introduces a new type and | |
9139 | sometimes names an existing type. Normally, the rule is that it | |
9140 | introduces a new type only if there is not an existing type of | |
9141 | the same name already in scope. For example, given: | |
9142 | ||
9143 | struct S {}; | |
9144 | void f() { struct S s; } | |
9145 | ||
9146 | the `struct S' in the body of `f' is the same `struct S' as in | |
9147 | the global scope; the existing definition is used. However, if | |
9148 | there were no global declaration, this would introduce a new | |
9149 | local class named `S'. | |
9150 | ||
9151 | An exception to this rule applies to the following code: | |
9152 | ||
9153 | namespace N { struct S; } | |
9154 | ||
9155 | Here, the elaborated-type-specifier names a new type | |
9156 | unconditionally; even if there is already an `S' in the | |
9157 | containing scope this declaration names a new type. | |
9158 | This exception only applies if the elaborated-type-specifier | |
9159 | forms the complete declaration: | |
9160 | ||
9161 | [class.name] | |
9162 | ||
9163 | A declaration consisting solely of `class-key identifier ;' is | |
9164 | either a redeclaration of the name in the current scope or a | |
9165 | forward declaration of the identifier as a class name. It | |
9166 | introduces the name into the current scope. | |
9167 | ||
9168 | We are in this situation precisely when the next token is a `;'. | |
9169 | ||
9170 | An exception to the exception is that a `friend' declaration does | |
9171 | *not* name a new type; i.e., given: | |
9172 | ||
9173 | struct S { friend struct T; }; | |
9174 | ||
9175 | `T' is not a new type in the scope of `S'. | |
9176 | ||
9177 | Also, `new struct S' or `sizeof (struct S)' never results in the | |
9178 | definition of a new type; a new type can only be declared in a | |
9bcb9aae | 9179 | declaration context. */ |
a723baf1 | 9180 | |
e0fed25b DS |
9181 | /* Warn about attributes. They are ignored. */ |
9182 | if (attributes) | |
9183 | warning ("type attributes are honored only at type definition"); | |
9184 | ||
a723baf1 | 9185 | type = xref_tag (tag_type, identifier, |
e0fed25b | 9186 | /*attributes=*/NULL_TREE, |
a723baf1 MM |
9187 | (is_friend |
9188 | || !is_declaration | |
9189 | || cp_lexer_next_token_is_not (parser->lexer, | |
cbd63935 KL |
9190 | CPP_SEMICOLON)), |
9191 | parser->num_template_parameter_lists); | |
a723baf1 MM |
9192 | } |
9193 | } | |
9194 | if (tag_type != enum_type) | |
9195 | cp_parser_check_class_key (tag_type, type); | |
ee43dab5 MM |
9196 | |
9197 | /* A "<" cannot follow an elaborated type specifier. If that | |
9198 | happens, the user was probably trying to form a template-id. */ | |
9199 | cp_parser_check_for_invalid_template_id (parser, type); | |
9200 | ||
a723baf1 MM |
9201 | return type; |
9202 | } | |
9203 | ||
9204 | /* Parse an enum-specifier. | |
9205 | ||
9206 | enum-specifier: | |
9207 | enum identifier [opt] { enumerator-list [opt] } | |
9208 | ||
9209 | Returns an ENUM_TYPE representing the enumeration. */ | |
9210 | ||
9211 | static tree | |
94edc4ab | 9212 | cp_parser_enum_specifier (cp_parser* parser) |
a723baf1 MM |
9213 | { |
9214 | cp_token *token; | |
9215 | tree identifier = NULL_TREE; | |
9216 | tree type; | |
9217 | ||
9218 | /* Look for the `enum' keyword. */ | |
9219 | if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'")) | |
9220 | return error_mark_node; | |
9221 | /* Peek at the next token. */ | |
9222 | token = cp_lexer_peek_token (parser->lexer); | |
9223 | ||
9224 | /* See if it is an identifier. */ | |
9225 | if (token->type == CPP_NAME) | |
9226 | identifier = cp_parser_identifier (parser); | |
9227 | ||
9228 | /* Look for the `{'. */ | |
9229 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
9230 | return error_mark_node; | |
9231 | ||
9232 | /* At this point, we're going ahead with the enum-specifier, even | |
9233 | if some other problem occurs. */ | |
9234 | cp_parser_commit_to_tentative_parse (parser); | |
9235 | ||
9236 | /* Issue an error message if type-definitions are forbidden here. */ | |
9237 | cp_parser_check_type_definition (parser); | |
9238 | ||
9239 | /* Create the new type. */ | |
9240 | type = start_enum (identifier ? identifier : make_anon_name ()); | |
9241 | ||
9242 | /* Peek at the next token. */ | |
9243 | token = cp_lexer_peek_token (parser->lexer); | |
9244 | /* If it's not a `}', then there are some enumerators. */ | |
9245 | if (token->type != CPP_CLOSE_BRACE) | |
9246 | cp_parser_enumerator_list (parser, type); | |
9247 | /* Look for the `}'. */ | |
9248 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
9249 | ||
9250 | /* Finish up the enumeration. */ | |
9251 | finish_enum (type); | |
9252 | ||
9253 | return type; | |
9254 | } | |
9255 | ||
9256 | /* Parse an enumerator-list. The enumerators all have the indicated | |
9257 | TYPE. | |
9258 | ||
9259 | enumerator-list: | |
9260 | enumerator-definition | |
9261 | enumerator-list , enumerator-definition */ | |
9262 | ||
9263 | static void | |
94edc4ab | 9264 | cp_parser_enumerator_list (cp_parser* parser, tree type) |
a723baf1 MM |
9265 | { |
9266 | while (true) | |
9267 | { | |
9268 | cp_token *token; | |
9269 | ||
9270 | /* Parse an enumerator-definition. */ | |
9271 | cp_parser_enumerator_definition (parser, type); | |
9272 | /* Peek at the next token. */ | |
9273 | token = cp_lexer_peek_token (parser->lexer); | |
9274 | /* If it's not a `,', then we've reached the end of the | |
9275 | list. */ | |
9276 | if (token->type != CPP_COMMA) | |
9277 | break; | |
9278 | /* Otherwise, consume the `,' and keep going. */ | |
9279 | cp_lexer_consume_token (parser->lexer); | |
9280 | /* If the next token is a `}', there is a trailing comma. */ | |
9281 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
9282 | { | |
9283 | if (pedantic && !in_system_header) | |
9284 | pedwarn ("comma at end of enumerator list"); | |
9285 | break; | |
9286 | } | |
9287 | } | |
9288 | } | |
9289 | ||
9290 | /* Parse an enumerator-definition. The enumerator has the indicated | |
9291 | TYPE. | |
9292 | ||
9293 | enumerator-definition: | |
9294 | enumerator | |
9295 | enumerator = constant-expression | |
9296 | ||
9297 | enumerator: | |
9298 | identifier */ | |
9299 | ||
9300 | static void | |
94edc4ab | 9301 | cp_parser_enumerator_definition (cp_parser* parser, tree type) |
a723baf1 MM |
9302 | { |
9303 | cp_token *token; | |
9304 | tree identifier; | |
9305 | tree value; | |
9306 | ||
9307 | /* Look for the identifier. */ | |
9308 | identifier = cp_parser_identifier (parser); | |
9309 | if (identifier == error_mark_node) | |
9310 | return; | |
9311 | ||
9312 | /* Peek at the next token. */ | |
9313 | token = cp_lexer_peek_token (parser->lexer); | |
9314 | /* If it's an `=', then there's an explicit value. */ | |
9315 | if (token->type == CPP_EQ) | |
9316 | { | |
9317 | /* Consume the `=' token. */ | |
9318 | cp_lexer_consume_token (parser->lexer); | |
9319 | /* Parse the value. */ | |
14d22dd6 | 9320 | value = cp_parser_constant_expression (parser, |
d17811fd | 9321 | /*allow_non_constant_p=*/false, |
14d22dd6 | 9322 | NULL); |
a723baf1 MM |
9323 | } |
9324 | else | |
9325 | value = NULL_TREE; | |
9326 | ||
9327 | /* Create the enumerator. */ | |
9328 | build_enumerator (identifier, value, type); | |
9329 | } | |
9330 | ||
9331 | /* Parse a namespace-name. | |
9332 | ||
9333 | namespace-name: | |
9334 | original-namespace-name | |
9335 | namespace-alias | |
9336 | ||
9337 | Returns the NAMESPACE_DECL for the namespace. */ | |
9338 | ||
9339 | static tree | |
94edc4ab | 9340 | cp_parser_namespace_name (cp_parser* parser) |
a723baf1 MM |
9341 | { |
9342 | tree identifier; | |
9343 | tree namespace_decl; | |
9344 | ||
9345 | /* Get the name of the namespace. */ | |
9346 | identifier = cp_parser_identifier (parser); | |
9347 | if (identifier == error_mark_node) | |
9348 | return error_mark_node; | |
9349 | ||
eea9800f MM |
9350 | /* Look up the identifier in the currently active scope. Look only |
9351 | for namespaces, due to: | |
9352 | ||
9353 | [basic.lookup.udir] | |
9354 | ||
9355 | When looking up a namespace-name in a using-directive or alias | |
9356 | definition, only namespace names are considered. | |
9357 | ||
9358 | And: | |
9359 | ||
9360 | [basic.lookup.qual] | |
9361 | ||
9362 | During the lookup of a name preceding the :: scope resolution | |
9363 | operator, object, function, and enumerator names are ignored. | |
9364 | ||
9365 | (Note that cp_parser_class_or_namespace_name only calls this | |
9366 | function if the token after the name is the scope resolution | |
9367 | operator.) */ | |
9368 | namespace_decl = cp_parser_lookup_name (parser, identifier, | |
eea9800f | 9369 | /*is_type=*/false, |
b0bc6e8e | 9370 | /*is_template=*/false, |
eea9800f MM |
9371 | /*is_namespace=*/true, |
9372 | /*check_dependency=*/true); | |
a723baf1 MM |
9373 | /* If it's not a namespace, issue an error. */ |
9374 | if (namespace_decl == error_mark_node | |
9375 | || TREE_CODE (namespace_decl) != NAMESPACE_DECL) | |
9376 | { | |
9377 | cp_parser_error (parser, "expected namespace-name"); | |
9378 | namespace_decl = error_mark_node; | |
9379 | } | |
9380 | ||
9381 | return namespace_decl; | |
9382 | } | |
9383 | ||
9384 | /* Parse a namespace-definition. | |
9385 | ||
9386 | namespace-definition: | |
9387 | named-namespace-definition | |
9388 | unnamed-namespace-definition | |
9389 | ||
9390 | named-namespace-definition: | |
9391 | original-namespace-definition | |
9392 | extension-namespace-definition | |
9393 | ||
9394 | original-namespace-definition: | |
9395 | namespace identifier { namespace-body } | |
9396 | ||
9397 | extension-namespace-definition: | |
9398 | namespace original-namespace-name { namespace-body } | |
9399 | ||
9400 | unnamed-namespace-definition: | |
9401 | namespace { namespace-body } */ | |
9402 | ||
9403 | static void | |
94edc4ab | 9404 | cp_parser_namespace_definition (cp_parser* parser) |
a723baf1 MM |
9405 | { |
9406 | tree identifier; | |
9407 | ||
9408 | /* Look for the `namespace' keyword. */ | |
9409 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
9410 | ||
9411 | /* Get the name of the namespace. We do not attempt to distinguish | |
9412 | between an original-namespace-definition and an | |
9413 | extension-namespace-definition at this point. The semantic | |
9414 | analysis routines are responsible for that. */ | |
9415 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
9416 | identifier = cp_parser_identifier (parser); | |
9417 | else | |
9418 | identifier = NULL_TREE; | |
9419 | ||
9420 | /* Look for the `{' to start the namespace. */ | |
9421 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
9422 | /* Start the namespace. */ | |
9423 | push_namespace (identifier); | |
9424 | /* Parse the body of the namespace. */ | |
9425 | cp_parser_namespace_body (parser); | |
9426 | /* Finish the namespace. */ | |
9427 | pop_namespace (); | |
9428 | /* Look for the final `}'. */ | |
9429 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
9430 | } | |
9431 | ||
9432 | /* Parse a namespace-body. | |
9433 | ||
9434 | namespace-body: | |
9435 | declaration-seq [opt] */ | |
9436 | ||
9437 | static void | |
94edc4ab | 9438 | cp_parser_namespace_body (cp_parser* parser) |
a723baf1 MM |
9439 | { |
9440 | cp_parser_declaration_seq_opt (parser); | |
9441 | } | |
9442 | ||
9443 | /* Parse a namespace-alias-definition. | |
9444 | ||
9445 | namespace-alias-definition: | |
9446 | namespace identifier = qualified-namespace-specifier ; */ | |
9447 | ||
9448 | static void | |
94edc4ab | 9449 | cp_parser_namespace_alias_definition (cp_parser* parser) |
a723baf1 MM |
9450 | { |
9451 | tree identifier; | |
9452 | tree namespace_specifier; | |
9453 | ||
9454 | /* Look for the `namespace' keyword. */ | |
9455 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
9456 | /* Look for the identifier. */ | |
9457 | identifier = cp_parser_identifier (parser); | |
9458 | if (identifier == error_mark_node) | |
9459 | return; | |
9460 | /* Look for the `=' token. */ | |
9461 | cp_parser_require (parser, CPP_EQ, "`='"); | |
9462 | /* Look for the qualified-namespace-specifier. */ | |
9463 | namespace_specifier | |
9464 | = cp_parser_qualified_namespace_specifier (parser); | |
9465 | /* Look for the `;' token. */ | |
9466 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
9467 | ||
9468 | /* Register the alias in the symbol table. */ | |
9469 | do_namespace_alias (identifier, namespace_specifier); | |
9470 | } | |
9471 | ||
9472 | /* Parse a qualified-namespace-specifier. | |
9473 | ||
9474 | qualified-namespace-specifier: | |
9475 | :: [opt] nested-name-specifier [opt] namespace-name | |
9476 | ||
9477 | Returns a NAMESPACE_DECL corresponding to the specified | |
9478 | namespace. */ | |
9479 | ||
9480 | static tree | |
94edc4ab | 9481 | cp_parser_qualified_namespace_specifier (cp_parser* parser) |
a723baf1 MM |
9482 | { |
9483 | /* Look for the optional `::'. */ | |
9484 | cp_parser_global_scope_opt (parser, | |
9485 | /*current_scope_valid_p=*/false); | |
9486 | ||
9487 | /* Look for the optional nested-name-specifier. */ | |
9488 | cp_parser_nested_name_specifier_opt (parser, | |
9489 | /*typename_keyword_p=*/false, | |
9490 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9491 | /*type_p=*/false, |
9492 | /*is_declaration=*/true); | |
a723baf1 MM |
9493 | |
9494 | return cp_parser_namespace_name (parser); | |
9495 | } | |
9496 | ||
9497 | /* Parse a using-declaration. | |
9498 | ||
9499 | using-declaration: | |
9500 | using typename [opt] :: [opt] nested-name-specifier unqualified-id ; | |
9501 | using :: unqualified-id ; */ | |
9502 | ||
9503 | static void | |
94edc4ab | 9504 | cp_parser_using_declaration (cp_parser* parser) |
a723baf1 MM |
9505 | { |
9506 | cp_token *token; | |
9507 | bool typename_p = false; | |
9508 | bool global_scope_p; | |
9509 | tree decl; | |
9510 | tree identifier; | |
9511 | tree scope; | |
ed5f054f | 9512 | tree qscope; |
a723baf1 MM |
9513 | |
9514 | /* Look for the `using' keyword. */ | |
9515 | cp_parser_require_keyword (parser, RID_USING, "`using'"); | |
9516 | ||
9517 | /* Peek at the next token. */ | |
9518 | token = cp_lexer_peek_token (parser->lexer); | |
9519 | /* See if it's `typename'. */ | |
9520 | if (token->keyword == RID_TYPENAME) | |
9521 | { | |
9522 | /* Remember that we've seen it. */ | |
9523 | typename_p = true; | |
9524 | /* Consume the `typename' token. */ | |
9525 | cp_lexer_consume_token (parser->lexer); | |
9526 | } | |
9527 | ||
9528 | /* Look for the optional global scope qualification. */ | |
9529 | global_scope_p | |
9530 | = (cp_parser_global_scope_opt (parser, | |
9531 | /*current_scope_valid_p=*/false) | |
9532 | != NULL_TREE); | |
9533 | ||
9534 | /* If we saw `typename', or didn't see `::', then there must be a | |
9535 | nested-name-specifier present. */ | |
9536 | if (typename_p || !global_scope_p) | |
ed5f054f AO |
9537 | qscope = cp_parser_nested_name_specifier (parser, typename_p, |
9538 | /*check_dependency_p=*/true, | |
9539 | /*type_p=*/false, | |
9540 | /*is_declaration=*/true); | |
a723baf1 MM |
9541 | /* Otherwise, we could be in either of the two productions. In that |
9542 | case, treat the nested-name-specifier as optional. */ | |
9543 | else | |
ed5f054f AO |
9544 | qscope = cp_parser_nested_name_specifier_opt (parser, |
9545 | /*typename_keyword_p=*/false, | |
9546 | /*check_dependency_p=*/true, | |
9547 | /*type_p=*/false, | |
9548 | /*is_declaration=*/true); | |
9549 | if (!qscope) | |
9550 | qscope = global_namespace; | |
a723baf1 MM |
9551 | |
9552 | /* Parse the unqualified-id. */ | |
9553 | identifier = cp_parser_unqualified_id (parser, | |
9554 | /*template_keyword_p=*/false, | |
f3c2dfc6 MM |
9555 | /*check_dependency_p=*/true, |
9556 | /*declarator_p=*/true); | |
a723baf1 MM |
9557 | |
9558 | /* The function we call to handle a using-declaration is different | |
9559 | depending on what scope we are in. */ | |
f3c2dfc6 MM |
9560 | if (identifier == error_mark_node) |
9561 | ; | |
9562 | else if (TREE_CODE (identifier) != IDENTIFIER_NODE | |
9563 | && TREE_CODE (identifier) != BIT_NOT_EXPR) | |
9564 | /* [namespace.udecl] | |
9565 | ||
9566 | A using declaration shall not name a template-id. */ | |
9567 | error ("a template-id may not appear in a using-declaration"); | |
a723baf1 MM |
9568 | else |
9569 | { | |
f3c2dfc6 MM |
9570 | scope = current_scope (); |
9571 | if (scope && TYPE_P (scope)) | |
4eb6d609 | 9572 | { |
f3c2dfc6 MM |
9573 | /* Create the USING_DECL. */ |
9574 | decl = do_class_using_decl (build_nt (SCOPE_REF, | |
9575 | parser->scope, | |
9576 | identifier)); | |
9577 | /* Add it to the list of members in this class. */ | |
9578 | finish_member_declaration (decl); | |
4eb6d609 | 9579 | } |
a723baf1 | 9580 | else |
f3c2dfc6 MM |
9581 | { |
9582 | decl = cp_parser_lookup_name_simple (parser, identifier); | |
9583 | if (decl == error_mark_node) | |
4bb8ca28 | 9584 | cp_parser_name_lookup_error (parser, identifier, decl, NULL); |
f3c2dfc6 | 9585 | else if (scope) |
ed5f054f | 9586 | do_local_using_decl (decl, qscope, identifier); |
f3c2dfc6 | 9587 | else |
ed5f054f | 9588 | do_toplevel_using_decl (decl, qscope, identifier); |
f3c2dfc6 | 9589 | } |
a723baf1 MM |
9590 | } |
9591 | ||
9592 | /* Look for the final `;'. */ | |
9593 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
9594 | } | |
9595 | ||
9596 | /* Parse a using-directive. | |
9597 | ||
9598 | using-directive: | |
9599 | using namespace :: [opt] nested-name-specifier [opt] | |
9600 | namespace-name ; */ | |
9601 | ||
9602 | static void | |
94edc4ab | 9603 | cp_parser_using_directive (cp_parser* parser) |
a723baf1 MM |
9604 | { |
9605 | tree namespace_decl; | |
86098eb8 | 9606 | tree attribs; |
a723baf1 MM |
9607 | |
9608 | /* Look for the `using' keyword. */ | |
9609 | cp_parser_require_keyword (parser, RID_USING, "`using'"); | |
9610 | /* And the `namespace' keyword. */ | |
9611 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
9612 | /* Look for the optional `::' operator. */ | |
9613 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); | |
34cd5ae7 | 9614 | /* And the optional nested-name-specifier. */ |
a723baf1 MM |
9615 | cp_parser_nested_name_specifier_opt (parser, |
9616 | /*typename_keyword_p=*/false, | |
9617 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9618 | /*type_p=*/false, |
9619 | /*is_declaration=*/true); | |
a723baf1 MM |
9620 | /* Get the namespace being used. */ |
9621 | namespace_decl = cp_parser_namespace_name (parser); | |
86098eb8 JM |
9622 | /* And any specified attributes. */ |
9623 | attribs = cp_parser_attributes_opt (parser); | |
a723baf1 | 9624 | /* Update the symbol table. */ |
86098eb8 | 9625 | parse_using_directive (namespace_decl, attribs); |
a723baf1 MM |
9626 | /* Look for the final `;'. */ |
9627 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
9628 | } | |
9629 | ||
9630 | /* Parse an asm-definition. | |
9631 | ||
9632 | asm-definition: | |
9633 | asm ( string-literal ) ; | |
9634 | ||
9635 | GNU Extension: | |
9636 | ||
9637 | asm-definition: | |
9638 | asm volatile [opt] ( string-literal ) ; | |
9639 | asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ; | |
9640 | asm volatile [opt] ( string-literal : asm-operand-list [opt] | |
9641 | : asm-operand-list [opt] ) ; | |
9642 | asm volatile [opt] ( string-literal : asm-operand-list [opt] | |
9643 | : asm-operand-list [opt] | |
9644 | : asm-operand-list [opt] ) ; */ | |
9645 | ||
9646 | static void | |
94edc4ab | 9647 | cp_parser_asm_definition (cp_parser* parser) |
a723baf1 MM |
9648 | { |
9649 | cp_token *token; | |
9650 | tree string; | |
9651 | tree outputs = NULL_TREE; | |
9652 | tree inputs = NULL_TREE; | |
9653 | tree clobbers = NULL_TREE; | |
9654 | tree asm_stmt; | |
9655 | bool volatile_p = false; | |
9656 | bool extended_p = false; | |
9657 | ||
9658 | /* Look for the `asm' keyword. */ | |
9659 | cp_parser_require_keyword (parser, RID_ASM, "`asm'"); | |
9660 | /* See if the next token is `volatile'. */ | |
9661 | if (cp_parser_allow_gnu_extensions_p (parser) | |
9662 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE)) | |
9663 | { | |
9664 | /* Remember that we saw the `volatile' keyword. */ | |
9665 | volatile_p = true; | |
9666 | /* Consume the token. */ | |
9667 | cp_lexer_consume_token (parser->lexer); | |
9668 | } | |
9669 | /* Look for the opening `('. */ | |
9670 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
9671 | /* Look for the string. */ | |
9672 | token = cp_parser_require (parser, CPP_STRING, "asm body"); | |
9673 | if (!token) | |
9674 | return; | |
9675 | string = token->value; | |
9676 | /* If we're allowing GNU extensions, check for the extended assembly | |
9677 | syntax. Unfortunately, the `:' tokens need not be separated by | |
9678 | a space in C, and so, for compatibility, we tolerate that here | |
9679 | too. Doing that means that we have to treat the `::' operator as | |
9680 | two `:' tokens. */ | |
9681 | if (cp_parser_allow_gnu_extensions_p (parser) | |
9682 | && at_function_scope_p () | |
9683 | && (cp_lexer_next_token_is (parser->lexer, CPP_COLON) | |
9684 | || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))) | |
9685 | { | |
9686 | bool inputs_p = false; | |
9687 | bool clobbers_p = false; | |
9688 | ||
9689 | /* The extended syntax was used. */ | |
9690 | extended_p = true; | |
9691 | ||
9692 | /* Look for outputs. */ | |
9693 | if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
9694 | { | |
9695 | /* Consume the `:'. */ | |
9696 | cp_lexer_consume_token (parser->lexer); | |
9697 | /* Parse the output-operands. */ | |
9698 | if (cp_lexer_next_token_is_not (parser->lexer, | |
9699 | CPP_COLON) | |
9700 | && cp_lexer_next_token_is_not (parser->lexer, | |
8caf4c38 MM |
9701 | CPP_SCOPE) |
9702 | && cp_lexer_next_token_is_not (parser->lexer, | |
9703 | CPP_CLOSE_PAREN)) | |
a723baf1 MM |
9704 | outputs = cp_parser_asm_operand_list (parser); |
9705 | } | |
9706 | /* If the next token is `::', there are no outputs, and the | |
9707 | next token is the beginning of the inputs. */ | |
9708 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
9709 | { | |
9710 | /* Consume the `::' token. */ | |
9711 | cp_lexer_consume_token (parser->lexer); | |
9712 | /* The inputs are coming next. */ | |
9713 | inputs_p = true; | |
9714 | } | |
9715 | ||
9716 | /* Look for inputs. */ | |
9717 | if (inputs_p | |
9718 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
9719 | { | |
9720 | if (!inputs_p) | |
9721 | /* Consume the `:'. */ | |
9722 | cp_lexer_consume_token (parser->lexer); | |
9723 | /* Parse the output-operands. */ | |
9724 | if (cp_lexer_next_token_is_not (parser->lexer, | |
9725 | CPP_COLON) | |
9726 | && cp_lexer_next_token_is_not (parser->lexer, | |
8caf4c38 MM |
9727 | CPP_SCOPE) |
9728 | && cp_lexer_next_token_is_not (parser->lexer, | |
9729 | CPP_CLOSE_PAREN)) | |
a723baf1 MM |
9730 | inputs = cp_parser_asm_operand_list (parser); |
9731 | } | |
9732 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
9733 | /* The clobbers are coming next. */ | |
9734 | clobbers_p = true; | |
9735 | ||
9736 | /* Look for clobbers. */ | |
9737 | if (clobbers_p | |
9738 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
9739 | { | |
9740 | if (!clobbers_p) | |
9741 | /* Consume the `:'. */ | |
9742 | cp_lexer_consume_token (parser->lexer); | |
9743 | /* Parse the clobbers. */ | |
8caf4c38 MM |
9744 | if (cp_lexer_next_token_is_not (parser->lexer, |
9745 | CPP_CLOSE_PAREN)) | |
9746 | clobbers = cp_parser_asm_clobber_list (parser); | |
a723baf1 MM |
9747 | } |
9748 | } | |
9749 | /* Look for the closing `)'. */ | |
9750 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
a668c6ad MM |
9751 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
9752 | /*consume_paren=*/true); | |
a723baf1 MM |
9753 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
9754 | ||
9755 | /* Create the ASM_STMT. */ | |
9756 | if (at_function_scope_p ()) | |
9757 | { | |
9758 | asm_stmt = | |
9759 | finish_asm_stmt (volatile_p | |
9760 | ? ridpointers[(int) RID_VOLATILE] : NULL_TREE, | |
9761 | string, outputs, inputs, clobbers); | |
9762 | /* If the extended syntax was not used, mark the ASM_STMT. */ | |
9763 | if (!extended_p) | |
9764 | ASM_INPUT_P (asm_stmt) = 1; | |
9765 | } | |
9766 | else | |
9767 | assemble_asm (string); | |
9768 | } | |
9769 | ||
9770 | /* Declarators [gram.dcl.decl] */ | |
9771 | ||
9772 | /* Parse an init-declarator. | |
9773 | ||
9774 | init-declarator: | |
9775 | declarator initializer [opt] | |
9776 | ||
9777 | GNU Extension: | |
9778 | ||
9779 | init-declarator: | |
9780 | declarator asm-specification [opt] attributes [opt] initializer [opt] | |
9781 | ||
4bb8ca28 MM |
9782 | function-definition: |
9783 | decl-specifier-seq [opt] declarator ctor-initializer [opt] | |
9784 | function-body | |
9785 | decl-specifier-seq [opt] declarator function-try-block | |
9786 | ||
9787 | GNU Extension: | |
9788 | ||
9789 | function-definition: | |
9790 | __extension__ function-definition | |
9791 | ||
a723baf1 | 9792 | The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator. |
c8e4f0e9 | 9793 | Returns a representation of the entity declared. If MEMBER_P is TRUE, |
cf22909c KL |
9794 | then this declarator appears in a class scope. The new DECL created |
9795 | by this declarator is returned. | |
a723baf1 MM |
9796 | |
9797 | If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and | |
9798 | for a function-definition here as well. If the declarator is a | |
9799 | declarator for a function-definition, *FUNCTION_DEFINITION_P will | |
9800 | be TRUE upon return. By that point, the function-definition will | |
9801 | have been completely parsed. | |
9802 | ||
9803 | FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P | |
9804 | is FALSE. */ | |
9805 | ||
9806 | static tree | |
94edc4ab NN |
9807 | cp_parser_init_declarator (cp_parser* parser, |
9808 | tree decl_specifiers, | |
9809 | tree prefix_attributes, | |
9810 | bool function_definition_allowed_p, | |
9811 | bool member_p, | |
560ad596 | 9812 | int declares_class_or_enum, |
94edc4ab | 9813 | bool* function_definition_p) |
a723baf1 MM |
9814 | { |
9815 | cp_token *token; | |
9816 | tree declarator; | |
9817 | tree attributes; | |
9818 | tree asm_specification; | |
9819 | tree initializer; | |
9820 | tree decl = NULL_TREE; | |
9821 | tree scope; | |
a723baf1 MM |
9822 | bool is_initialized; |
9823 | bool is_parenthesized_init; | |
39703eb9 | 9824 | bool is_non_constant_init; |
7efa3e22 | 9825 | int ctor_dtor_or_conv_p; |
a723baf1 MM |
9826 | bool friend_p; |
9827 | ||
9828 | /* Assume that this is not the declarator for a function | |
9829 | definition. */ | |
9830 | if (function_definition_p) | |
9831 | *function_definition_p = false; | |
9832 | ||
9833 | /* Defer access checks while parsing the declarator; we cannot know | |
9834 | what names are accessible until we know what is being | |
9835 | declared. */ | |
cf22909c KL |
9836 | resume_deferring_access_checks (); |
9837 | ||
a723baf1 MM |
9838 | /* Parse the declarator. */ |
9839 | declarator | |
62b8a44e | 9840 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
9841 | &ctor_dtor_or_conv_p, |
9842 | /*parenthesized_p=*/NULL); | |
a723baf1 | 9843 | /* Gather up the deferred checks. */ |
cf22909c | 9844 | stop_deferring_access_checks (); |
24c0ef37 | 9845 | |
a723baf1 MM |
9846 | /* If the DECLARATOR was erroneous, there's no need to go |
9847 | further. */ | |
9848 | if (declarator == error_mark_node) | |
cf22909c | 9849 | return error_mark_node; |
a723baf1 | 9850 | |
560ad596 MM |
9851 | cp_parser_check_for_definition_in_return_type (declarator, |
9852 | declares_class_or_enum); | |
9853 | ||
a723baf1 MM |
9854 | /* Figure out what scope the entity declared by the DECLARATOR is |
9855 | located in. `grokdeclarator' sometimes changes the scope, so | |
9856 | we compute it now. */ | |
9857 | scope = get_scope_of_declarator (declarator); | |
9858 | ||
9859 | /* If we're allowing GNU extensions, look for an asm-specification | |
9860 | and attributes. */ | |
9861 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
9862 | { | |
9863 | /* Look for an asm-specification. */ | |
9864 | asm_specification = cp_parser_asm_specification_opt (parser); | |
9865 | /* And attributes. */ | |
9866 | attributes = cp_parser_attributes_opt (parser); | |
9867 | } | |
9868 | else | |
9869 | { | |
9870 | asm_specification = NULL_TREE; | |
9871 | attributes = NULL_TREE; | |
9872 | } | |
9873 | ||
9874 | /* Peek at the next token. */ | |
9875 | token = cp_lexer_peek_token (parser->lexer); | |
9876 | /* Check to see if the token indicates the start of a | |
9877 | function-definition. */ | |
9878 | if (cp_parser_token_starts_function_definition_p (token)) | |
9879 | { | |
9880 | if (!function_definition_allowed_p) | |
9881 | { | |
9882 | /* If a function-definition should not appear here, issue an | |
9883 | error message. */ | |
9884 | cp_parser_error (parser, | |
9885 | "a function-definition is not allowed here"); | |
9886 | return error_mark_node; | |
9887 | } | |
9888 | else | |
9889 | { | |
a723baf1 MM |
9890 | /* Neither attributes nor an asm-specification are allowed |
9891 | on a function-definition. */ | |
9892 | if (asm_specification) | |
9893 | error ("an asm-specification is not allowed on a function-definition"); | |
9894 | if (attributes) | |
9895 | error ("attributes are not allowed on a function-definition"); | |
9896 | /* This is a function-definition. */ | |
9897 | *function_definition_p = true; | |
9898 | ||
a723baf1 | 9899 | /* Parse the function definition. */ |
4bb8ca28 MM |
9900 | if (member_p) |
9901 | decl = cp_parser_save_member_function_body (parser, | |
9902 | decl_specifiers, | |
9903 | declarator, | |
9904 | prefix_attributes); | |
9905 | else | |
9906 | decl | |
9907 | = (cp_parser_function_definition_from_specifiers_and_declarator | |
9908 | (parser, decl_specifiers, prefix_attributes, declarator)); | |
24c0ef37 | 9909 | |
a723baf1 MM |
9910 | return decl; |
9911 | } | |
9912 | } | |
9913 | ||
9914 | /* [dcl.dcl] | |
9915 | ||
9916 | Only in function declarations for constructors, destructors, and | |
9917 | type conversions can the decl-specifier-seq be omitted. | |
9918 | ||
9919 | We explicitly postpone this check past the point where we handle | |
9920 | function-definitions because we tolerate function-definitions | |
9921 | that are missing their return types in some modes. */ | |
7efa3e22 | 9922 | if (!decl_specifiers && ctor_dtor_or_conv_p <= 0) |
a723baf1 MM |
9923 | { |
9924 | cp_parser_error (parser, | |
9925 | "expected constructor, destructor, or type conversion"); | |
9926 | return error_mark_node; | |
9927 | } | |
9928 | ||
9929 | /* An `=' or an `(' indicates an initializer. */ | |
9930 | is_initialized = (token->type == CPP_EQ | |
9931 | || token->type == CPP_OPEN_PAREN); | |
9932 | /* If the init-declarator isn't initialized and isn't followed by a | |
9933 | `,' or `;', it's not a valid init-declarator. */ | |
9934 | if (!is_initialized | |
9935 | && token->type != CPP_COMMA | |
9936 | && token->type != CPP_SEMICOLON) | |
9937 | { | |
9938 | cp_parser_error (parser, "expected init-declarator"); | |
9939 | return error_mark_node; | |
9940 | } | |
9941 | ||
9942 | /* Because start_decl has side-effects, we should only call it if we | |
9943 | know we're going ahead. By this point, we know that we cannot | |
9944 | possibly be looking at any other construct. */ | |
9945 | cp_parser_commit_to_tentative_parse (parser); | |
9946 | ||
e90c7b84 ILT |
9947 | /* If the decl specifiers were bad, issue an error now that we're |
9948 | sure this was intended to be a declarator. Then continue | |
9949 | declaring the variable(s), as int, to try to cut down on further | |
9950 | errors. */ | |
9951 | if (decl_specifiers != NULL | |
9952 | && TREE_VALUE (decl_specifiers) == error_mark_node) | |
9953 | { | |
9954 | cp_parser_error (parser, "invalid type in declaration"); | |
9955 | TREE_VALUE (decl_specifiers) = integer_type_node; | |
9956 | } | |
9957 | ||
a723baf1 MM |
9958 | /* Check to see whether or not this declaration is a friend. */ |
9959 | friend_p = cp_parser_friend_p (decl_specifiers); | |
9960 | ||
9961 | /* Check that the number of template-parameter-lists is OK. */ | |
ee3071ef | 9962 | if (!cp_parser_check_declarator_template_parameters (parser, declarator)) |
cf22909c | 9963 | return error_mark_node; |
a723baf1 MM |
9964 | |
9965 | /* Enter the newly declared entry in the symbol table. If we're | |
9966 | processing a declaration in a class-specifier, we wait until | |
9967 | after processing the initializer. */ | |
9968 | if (!member_p) | |
9969 | { | |
9970 | if (parser->in_unbraced_linkage_specification_p) | |
9971 | { | |
9972 | decl_specifiers = tree_cons (error_mark_node, | |
9973 | get_identifier ("extern"), | |
9974 | decl_specifiers); | |
9975 | have_extern_spec = false; | |
9976 | } | |
ee3071ef NS |
9977 | decl = start_decl (declarator, decl_specifiers, |
9978 | is_initialized, attributes, prefix_attributes); | |
a723baf1 MM |
9979 | } |
9980 | ||
9981 | /* Enter the SCOPE. That way unqualified names appearing in the | |
9982 | initializer will be looked up in SCOPE. */ | |
9983 | if (scope) | |
9984 | push_scope (scope); | |
9985 | ||
9986 | /* Perform deferred access control checks, now that we know in which | |
9987 | SCOPE the declared entity resides. */ | |
9988 | if (!member_p && decl) | |
9989 | { | |
9990 | tree saved_current_function_decl = NULL_TREE; | |
9991 | ||
9992 | /* If the entity being declared is a function, pretend that we | |
9993 | are in its scope. If it is a `friend', it may have access to | |
9bcb9aae | 9994 | things that would not otherwise be accessible. */ |
a723baf1 MM |
9995 | if (TREE_CODE (decl) == FUNCTION_DECL) |
9996 | { | |
9997 | saved_current_function_decl = current_function_decl; | |
9998 | current_function_decl = decl; | |
9999 | } | |
10000 | ||
cf22909c KL |
10001 | /* Perform the access control checks for the declarator and the |
10002 | the decl-specifiers. */ | |
10003 | perform_deferred_access_checks (); | |
a723baf1 MM |
10004 | |
10005 | /* Restore the saved value. */ | |
10006 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
10007 | current_function_decl = saved_current_function_decl; | |
10008 | } | |
10009 | ||
10010 | /* Parse the initializer. */ | |
10011 | if (is_initialized) | |
39703eb9 MM |
10012 | initializer = cp_parser_initializer (parser, |
10013 | &is_parenthesized_init, | |
10014 | &is_non_constant_init); | |
a723baf1 MM |
10015 | else |
10016 | { | |
10017 | initializer = NULL_TREE; | |
10018 | is_parenthesized_init = false; | |
39703eb9 | 10019 | is_non_constant_init = true; |
a723baf1 MM |
10020 | } |
10021 | ||
10022 | /* The old parser allows attributes to appear after a parenthesized | |
10023 | initializer. Mark Mitchell proposed removing this functionality | |
10024 | on the GCC mailing lists on 2002-08-13. This parser accepts the | |
10025 | attributes -- but ignores them. */ | |
10026 | if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init) | |
10027 | if (cp_parser_attributes_opt (parser)) | |
10028 | warning ("attributes after parenthesized initializer ignored"); | |
10029 | ||
10030 | /* Leave the SCOPE, now that we have processed the initializer. It | |
10031 | is important to do this before calling cp_finish_decl because it | |
10032 | makes decisions about whether to create DECL_STMTs or not based | |
10033 | on the current scope. */ | |
10034 | if (scope) | |
10035 | pop_scope (scope); | |
10036 | ||
10037 | /* For an in-class declaration, use `grokfield' to create the | |
10038 | declaration. */ | |
10039 | if (member_p) | |
8db1028e NS |
10040 | { |
10041 | decl = grokfield (declarator, decl_specifiers, | |
10042 | initializer, /*asmspec=*/NULL_TREE, | |
a723baf1 | 10043 | /*attributes=*/NULL_TREE); |
8db1028e NS |
10044 | if (decl && TREE_CODE (decl) == FUNCTION_DECL) |
10045 | cp_parser_save_default_args (parser, decl); | |
10046 | } | |
10047 | ||
a723baf1 MM |
10048 | /* Finish processing the declaration. But, skip friend |
10049 | declarations. */ | |
10050 | if (!friend_p && decl) | |
10051 | cp_finish_decl (decl, | |
10052 | initializer, | |
10053 | asm_specification, | |
10054 | /* If the initializer is in parentheses, then this is | |
10055 | a direct-initialization, which means that an | |
10056 | `explicit' constructor is OK. Otherwise, an | |
10057 | `explicit' constructor cannot be used. */ | |
10058 | ((is_parenthesized_init || !is_initialized) | |
10059 | ? 0 : LOOKUP_ONLYCONVERTING)); | |
10060 | ||
39703eb9 MM |
10061 | /* Remember whether or not variables were initialized by |
10062 | constant-expressions. */ | |
10063 | if (decl && TREE_CODE (decl) == VAR_DECL | |
10064 | && is_initialized && !is_non_constant_init) | |
10065 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; | |
10066 | ||
a723baf1 MM |
10067 | return decl; |
10068 | } | |
10069 | ||
10070 | /* Parse a declarator. | |
10071 | ||
10072 | declarator: | |
10073 | direct-declarator | |
10074 | ptr-operator declarator | |
10075 | ||
10076 | abstract-declarator: | |
10077 | ptr-operator abstract-declarator [opt] | |
10078 | direct-abstract-declarator | |
10079 | ||
10080 | GNU Extensions: | |
10081 | ||
10082 | declarator: | |
10083 | attributes [opt] direct-declarator | |
10084 | attributes [opt] ptr-operator declarator | |
10085 | ||
10086 | abstract-declarator: | |
10087 | attributes [opt] ptr-operator abstract-declarator [opt] | |
10088 | attributes [opt] direct-abstract-declarator | |
10089 | ||
10090 | Returns a representation of the declarator. If the declarator has | |
10091 | the form `* declarator', then an INDIRECT_REF is returned, whose | |
34cd5ae7 | 10092 | only operand is the sub-declarator. Analogously, `& declarator' is |
a723baf1 MM |
10093 | represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is |
10094 | used. The first operand is the TYPE for `X'. The second operand | |
10095 | is an INDIRECT_REF whose operand is the sub-declarator. | |
10096 | ||
34cd5ae7 | 10097 | Otherwise, the representation is as for a direct-declarator. |
a723baf1 MM |
10098 | |
10099 | (It would be better to define a structure type to represent | |
10100 | declarators, rather than abusing `tree' nodes to represent | |
10101 | declarators. That would be much clearer and save some memory. | |
10102 | There is no reason for declarators to be garbage-collected, for | |
10103 | example; they are created during parser and no longer needed after | |
10104 | `grokdeclarator' has been called.) | |
10105 | ||
10106 | For a ptr-operator that has the optional cv-qualifier-seq, | |
10107 | cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF | |
10108 | node. | |
10109 | ||
7efa3e22 NS |
10110 | If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to |
10111 | detect constructor, destructor or conversion operators. It is set | |
10112 | to -1 if the declarator is a name, and +1 if it is a | |
10113 | function. Otherwise it is set to zero. Usually you just want to | |
10114 | test for >0, but internally the negative value is used. | |
10115 | ||
a723baf1 MM |
10116 | (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have |
10117 | a decl-specifier-seq unless it declares a constructor, destructor, | |
10118 | or conversion. It might seem that we could check this condition in | |
10119 | semantic analysis, rather than parsing, but that makes it difficult | |
10120 | to handle something like `f()'. We want to notice that there are | |
10121 | no decl-specifiers, and therefore realize that this is an | |
4bb8ca28 MM |
10122 | expression, not a declaration.) |
10123 | ||
10124 | If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff | |
10125 | the declarator is a direct-declarator of the form "(...)". */ | |
a723baf1 MM |
10126 | |
10127 | static tree | |
94edc4ab NN |
10128 | cp_parser_declarator (cp_parser* parser, |
10129 | cp_parser_declarator_kind dcl_kind, | |
4bb8ca28 MM |
10130 | int* ctor_dtor_or_conv_p, |
10131 | bool* parenthesized_p) | |
a723baf1 MM |
10132 | { |
10133 | cp_token *token; | |
10134 | tree declarator; | |
10135 | enum tree_code code; | |
10136 | tree cv_qualifier_seq; | |
10137 | tree class_type; | |
10138 | tree attributes = NULL_TREE; | |
10139 | ||
10140 | /* Assume this is not a constructor, destructor, or type-conversion | |
10141 | operator. */ | |
10142 | if (ctor_dtor_or_conv_p) | |
7efa3e22 | 10143 | *ctor_dtor_or_conv_p = 0; |
a723baf1 MM |
10144 | |
10145 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
10146 | attributes = cp_parser_attributes_opt (parser); | |
10147 | ||
10148 | /* Peek at the next token. */ | |
10149 | token = cp_lexer_peek_token (parser->lexer); | |
10150 | ||
10151 | /* Check for the ptr-operator production. */ | |
10152 | cp_parser_parse_tentatively (parser); | |
10153 | /* Parse the ptr-operator. */ | |
10154 | code = cp_parser_ptr_operator (parser, | |
10155 | &class_type, | |
10156 | &cv_qualifier_seq); | |
10157 | /* If that worked, then we have a ptr-operator. */ | |
10158 | if (cp_parser_parse_definitely (parser)) | |
10159 | { | |
4bb8ca28 MM |
10160 | /* If a ptr-operator was found, then this declarator was not |
10161 | parenthesized. */ | |
10162 | if (parenthesized_p) | |
10163 | *parenthesized_p = true; | |
a723baf1 MM |
10164 | /* The dependent declarator is optional if we are parsing an |
10165 | abstract-declarator. */ | |
62b8a44e | 10166 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
a723baf1 MM |
10167 | cp_parser_parse_tentatively (parser); |
10168 | ||
10169 | /* Parse the dependent declarator. */ | |
62b8a44e | 10170 | declarator = cp_parser_declarator (parser, dcl_kind, |
4bb8ca28 MM |
10171 | /*ctor_dtor_or_conv_p=*/NULL, |
10172 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
10173 | |
10174 | /* If we are parsing an abstract-declarator, we must handle the | |
10175 | case where the dependent declarator is absent. */ | |
62b8a44e NS |
10176 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED |
10177 | && !cp_parser_parse_definitely (parser)) | |
a723baf1 MM |
10178 | declarator = NULL_TREE; |
10179 | ||
10180 | /* Build the representation of the ptr-operator. */ | |
10181 | if (code == INDIRECT_REF) | |
10182 | declarator = make_pointer_declarator (cv_qualifier_seq, | |
10183 | declarator); | |
10184 | else | |
10185 | declarator = make_reference_declarator (cv_qualifier_seq, | |
10186 | declarator); | |
10187 | /* Handle the pointer-to-member case. */ | |
10188 | if (class_type) | |
10189 | declarator = build_nt (SCOPE_REF, class_type, declarator); | |
10190 | } | |
10191 | /* Everything else is a direct-declarator. */ | |
10192 | else | |
4bb8ca28 MM |
10193 | { |
10194 | if (parenthesized_p) | |
10195 | *parenthesized_p = cp_lexer_next_token_is (parser->lexer, | |
10196 | CPP_OPEN_PAREN); | |
10197 | declarator = cp_parser_direct_declarator (parser, dcl_kind, | |
10198 | ctor_dtor_or_conv_p); | |
10199 | } | |
a723baf1 MM |
10200 | |
10201 | if (attributes && declarator != error_mark_node) | |
10202 | declarator = tree_cons (attributes, declarator, NULL_TREE); | |
10203 | ||
10204 | return declarator; | |
10205 | } | |
10206 | ||
10207 | /* Parse a direct-declarator or direct-abstract-declarator. | |
10208 | ||
10209 | direct-declarator: | |
10210 | declarator-id | |
10211 | direct-declarator ( parameter-declaration-clause ) | |
10212 | cv-qualifier-seq [opt] | |
10213 | exception-specification [opt] | |
10214 | direct-declarator [ constant-expression [opt] ] | |
10215 | ( declarator ) | |
10216 | ||
10217 | direct-abstract-declarator: | |
10218 | direct-abstract-declarator [opt] | |
10219 | ( parameter-declaration-clause ) | |
10220 | cv-qualifier-seq [opt] | |
10221 | exception-specification [opt] | |
10222 | direct-abstract-declarator [opt] [ constant-expression [opt] ] | |
10223 | ( abstract-declarator ) | |
10224 | ||
62b8a44e NS |
10225 | Returns a representation of the declarator. DCL_KIND is |
10226 | CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a | |
10227 | direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if | |
10228 | we are parsing a direct-declarator. It is | |
10229 | CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case | |
10230 | of ambiguity we prefer an abstract declarator, as per | |
10231 | [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for | |
a723baf1 MM |
10232 | cp_parser_declarator. |
10233 | ||
10234 | For the declarator-id production, the representation is as for an | |
10235 | id-expression, except that a qualified name is represented as a | |
10236 | SCOPE_REF. A function-declarator is represented as a CALL_EXPR; | |
10237 | see the documentation of the FUNCTION_DECLARATOR_* macros for | |
10238 | information about how to find the various declarator components. | |
10239 | An array-declarator is represented as an ARRAY_REF. The | |
10240 | direct-declarator is the first operand; the constant-expression | |
10241 | indicating the size of the array is the second operand. */ | |
10242 | ||
10243 | static tree | |
94edc4ab NN |
10244 | cp_parser_direct_declarator (cp_parser* parser, |
10245 | cp_parser_declarator_kind dcl_kind, | |
7efa3e22 | 10246 | int* ctor_dtor_or_conv_p) |
a723baf1 MM |
10247 | { |
10248 | cp_token *token; | |
62b8a44e | 10249 | tree declarator = NULL_TREE; |
a723baf1 MM |
10250 | tree scope = NULL_TREE; |
10251 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
10252 | bool saved_in_declarator_p = parser->in_declarator_p; | |
62b8a44e NS |
10253 | bool first = true; |
10254 | ||
10255 | while (true) | |
a723baf1 | 10256 | { |
62b8a44e NS |
10257 | /* Peek at the next token. */ |
10258 | token = cp_lexer_peek_token (parser->lexer); | |
10259 | if (token->type == CPP_OPEN_PAREN) | |
a723baf1 | 10260 | { |
62b8a44e NS |
10261 | /* This is either a parameter-declaration-clause, or a |
10262 | parenthesized declarator. When we know we are parsing a | |
34cd5ae7 | 10263 | named declarator, it must be a parenthesized declarator |
62b8a44e NS |
10264 | if FIRST is true. For instance, `(int)' is a |
10265 | parameter-declaration-clause, with an omitted | |
10266 | direct-abstract-declarator. But `((*))', is a | |
10267 | parenthesized abstract declarator. Finally, when T is a | |
10268 | template parameter `(T)' is a | |
34cd5ae7 | 10269 | parameter-declaration-clause, and not a parenthesized |
62b8a44e | 10270 | named declarator. |
a723baf1 | 10271 | |
62b8a44e NS |
10272 | We first try and parse a parameter-declaration-clause, |
10273 | and then try a nested declarator (if FIRST is true). | |
a723baf1 | 10274 | |
62b8a44e NS |
10275 | It is not an error for it not to be a |
10276 | parameter-declaration-clause, even when FIRST is | |
10277 | false. Consider, | |
10278 | ||
10279 | int i (int); | |
10280 | int i (3); | |
10281 | ||
10282 | The first is the declaration of a function while the | |
10283 | second is a the definition of a variable, including its | |
10284 | initializer. | |
10285 | ||
10286 | Having seen only the parenthesis, we cannot know which of | |
10287 | these two alternatives should be selected. Even more | |
10288 | complex are examples like: | |
10289 | ||
10290 | int i (int (a)); | |
10291 | int i (int (3)); | |
10292 | ||
10293 | The former is a function-declaration; the latter is a | |
10294 | variable initialization. | |
10295 | ||
34cd5ae7 | 10296 | Thus again, we try a parameter-declaration-clause, and if |
62b8a44e NS |
10297 | that fails, we back out and return. */ |
10298 | ||
10299 | if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) | |
a723baf1 | 10300 | { |
62b8a44e | 10301 | tree params; |
4047b164 | 10302 | unsigned saved_num_template_parameter_lists; |
62b8a44e NS |
10303 | |
10304 | cp_parser_parse_tentatively (parser); | |
a723baf1 | 10305 | |
62b8a44e NS |
10306 | /* Consume the `('. */ |
10307 | cp_lexer_consume_token (parser->lexer); | |
10308 | if (first) | |
10309 | { | |
10310 | /* If this is going to be an abstract declarator, we're | |
10311 | in a declarator and we can't have default args. */ | |
10312 | parser->default_arg_ok_p = false; | |
10313 | parser->in_declarator_p = true; | |
10314 | } | |
10315 | ||
4047b164 KL |
10316 | /* Inside the function parameter list, surrounding |
10317 | template-parameter-lists do not apply. */ | |
10318 | saved_num_template_parameter_lists | |
10319 | = parser->num_template_parameter_lists; | |
10320 | parser->num_template_parameter_lists = 0; | |
10321 | ||
62b8a44e NS |
10322 | /* Parse the parameter-declaration-clause. */ |
10323 | params = cp_parser_parameter_declaration_clause (parser); | |
10324 | ||
4047b164 KL |
10325 | parser->num_template_parameter_lists |
10326 | = saved_num_template_parameter_lists; | |
10327 | ||
62b8a44e | 10328 | /* If all went well, parse the cv-qualifier-seq and the |
34cd5ae7 | 10329 | exception-specification. */ |
62b8a44e NS |
10330 | if (cp_parser_parse_definitely (parser)) |
10331 | { | |
10332 | tree cv_qualifiers; | |
10333 | tree exception_specification; | |
7efa3e22 NS |
10334 | |
10335 | if (ctor_dtor_or_conv_p) | |
10336 | *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0; | |
62b8a44e NS |
10337 | first = false; |
10338 | /* Consume the `)'. */ | |
10339 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
10340 | ||
10341 | /* Parse the cv-qualifier-seq. */ | |
10342 | cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser); | |
10343 | /* And the exception-specification. */ | |
10344 | exception_specification | |
10345 | = cp_parser_exception_specification_opt (parser); | |
10346 | ||
10347 | /* Create the function-declarator. */ | |
10348 | declarator = make_call_declarator (declarator, | |
10349 | params, | |
10350 | cv_qualifiers, | |
10351 | exception_specification); | |
10352 | /* Any subsequent parameter lists are to do with | |
10353 | return type, so are not those of the declared | |
10354 | function. */ | |
10355 | parser->default_arg_ok_p = false; | |
10356 | ||
10357 | /* Repeat the main loop. */ | |
10358 | continue; | |
10359 | } | |
10360 | } | |
10361 | ||
10362 | /* If this is the first, we can try a parenthesized | |
10363 | declarator. */ | |
10364 | if (first) | |
a723baf1 | 10365 | { |
a7324e75 MM |
10366 | bool saved_in_type_id_in_expr_p; |
10367 | ||
a723baf1 | 10368 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
62b8a44e NS |
10369 | parser->in_declarator_p = saved_in_declarator_p; |
10370 | ||
10371 | /* Consume the `('. */ | |
10372 | cp_lexer_consume_token (parser->lexer); | |
10373 | /* Parse the nested declarator. */ | |
a7324e75 MM |
10374 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
10375 | parser->in_type_id_in_expr_p = true; | |
62b8a44e | 10376 | declarator |
4bb8ca28 MM |
10377 | = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p, |
10378 | /*parenthesized_p=*/NULL); | |
a7324e75 | 10379 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
62b8a44e NS |
10380 | first = false; |
10381 | /* Expect a `)'. */ | |
10382 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
10383 | declarator = error_mark_node; | |
10384 | if (declarator == error_mark_node) | |
10385 | break; | |
10386 | ||
10387 | goto handle_declarator; | |
a723baf1 | 10388 | } |
9bcb9aae | 10389 | /* Otherwise, we must be done. */ |
62b8a44e NS |
10390 | else |
10391 | break; | |
a723baf1 | 10392 | } |
62b8a44e NS |
10393 | else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
10394 | && token->type == CPP_OPEN_SQUARE) | |
a723baf1 | 10395 | { |
62b8a44e | 10396 | /* Parse an array-declarator. */ |
a723baf1 MM |
10397 | tree bounds; |
10398 | ||
7efa3e22 NS |
10399 | if (ctor_dtor_or_conv_p) |
10400 | *ctor_dtor_or_conv_p = 0; | |
10401 | ||
62b8a44e NS |
10402 | first = false; |
10403 | parser->default_arg_ok_p = false; | |
10404 | parser->in_declarator_p = true; | |
a723baf1 MM |
10405 | /* Consume the `['. */ |
10406 | cp_lexer_consume_token (parser->lexer); | |
10407 | /* Peek at the next token. */ | |
10408 | token = cp_lexer_peek_token (parser->lexer); | |
10409 | /* If the next token is `]', then there is no | |
10410 | constant-expression. */ | |
10411 | if (token->type != CPP_CLOSE_SQUARE) | |
14d22dd6 MM |
10412 | { |
10413 | bool non_constant_p; | |
10414 | ||
10415 | bounds | |
10416 | = cp_parser_constant_expression (parser, | |
10417 | /*allow_non_constant=*/true, | |
10418 | &non_constant_p); | |
d17811fd MM |
10419 | if (!non_constant_p) |
10420 | bounds = cp_parser_fold_non_dependent_expr (bounds); | |
14d22dd6 | 10421 | } |
a723baf1 MM |
10422 | else |
10423 | bounds = NULL_TREE; | |
10424 | /* Look for the closing `]'. */ | |
62b8a44e NS |
10425 | if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'")) |
10426 | { | |
10427 | declarator = error_mark_node; | |
10428 | break; | |
10429 | } | |
a723baf1 MM |
10430 | |
10431 | declarator = build_nt (ARRAY_REF, declarator, bounds); | |
10432 | } | |
62b8a44e | 10433 | else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT) |
a723baf1 | 10434 | { |
a668c6ad | 10435 | /* Parse a declarator-id */ |
62b8a44e NS |
10436 | if (dcl_kind == CP_PARSER_DECLARATOR_EITHER) |
10437 | cp_parser_parse_tentatively (parser); | |
10438 | declarator = cp_parser_declarator_id (parser); | |
712becab NS |
10439 | if (dcl_kind == CP_PARSER_DECLARATOR_EITHER) |
10440 | { | |
10441 | if (!cp_parser_parse_definitely (parser)) | |
10442 | declarator = error_mark_node; | |
10443 | else if (TREE_CODE (declarator) != IDENTIFIER_NODE) | |
10444 | { | |
10445 | cp_parser_error (parser, "expected unqualified-id"); | |
10446 | declarator = error_mark_node; | |
10447 | } | |
10448 | } | |
10449 | ||
62b8a44e NS |
10450 | if (declarator == error_mark_node) |
10451 | break; | |
a723baf1 | 10452 | |
d9a50301 KL |
10453 | if (TREE_CODE (declarator) == SCOPE_REF |
10454 | && !current_scope ()) | |
62b8a44e NS |
10455 | { |
10456 | tree scope = TREE_OPERAND (declarator, 0); | |
712becab | 10457 | |
62b8a44e NS |
10458 | /* In the declaration of a member of a template class |
10459 | outside of the class itself, the SCOPE will sometimes | |
10460 | be a TYPENAME_TYPE. For example, given: | |
10461 | ||
10462 | template <typename T> | |
10463 | int S<T>::R::i = 3; | |
10464 | ||
10465 | the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In | |
10466 | this context, we must resolve S<T>::R to an ordinary | |
10467 | type, rather than a typename type. | |
10468 | ||
10469 | The reason we normally avoid resolving TYPENAME_TYPEs | |
10470 | is that a specialization of `S' might render | |
10471 | `S<T>::R' not a type. However, if `S' is | |
10472 | specialized, then this `i' will not be used, so there | |
10473 | is no harm in resolving the types here. */ | |
10474 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
10475 | { | |
14d22dd6 MM |
10476 | tree type; |
10477 | ||
62b8a44e | 10478 | /* Resolve the TYPENAME_TYPE. */ |
14d22dd6 MM |
10479 | type = resolve_typename_type (scope, |
10480 | /*only_current_p=*/false); | |
62b8a44e | 10481 | /* If that failed, the declarator is invalid. */ |
14d22dd6 MM |
10482 | if (type != error_mark_node) |
10483 | scope = type; | |
62b8a44e NS |
10484 | /* Build a new DECLARATOR. */ |
10485 | declarator = build_nt (SCOPE_REF, | |
10486 | scope, | |
10487 | TREE_OPERAND (declarator, 1)); | |
10488 | } | |
10489 | } | |
10490 | ||
10491 | /* Check to see whether the declarator-id names a constructor, | |
10492 | destructor, or conversion. */ | |
10493 | if (declarator && ctor_dtor_or_conv_p | |
10494 | && ((TREE_CODE (declarator) == SCOPE_REF | |
10495 | && CLASS_TYPE_P (TREE_OPERAND (declarator, 0))) | |
10496 | || (TREE_CODE (declarator) != SCOPE_REF | |
10497 | && at_class_scope_p ()))) | |
a723baf1 | 10498 | { |
62b8a44e NS |
10499 | tree unqualified_name; |
10500 | tree class_type; | |
10501 | ||
10502 | /* Get the unqualified part of the name. */ | |
10503 | if (TREE_CODE (declarator) == SCOPE_REF) | |
10504 | { | |
10505 | class_type = TREE_OPERAND (declarator, 0); | |
10506 | unqualified_name = TREE_OPERAND (declarator, 1); | |
10507 | } | |
10508 | else | |
10509 | { | |
10510 | class_type = current_class_type; | |
10511 | unqualified_name = declarator; | |
10512 | } | |
10513 | ||
10514 | /* See if it names ctor, dtor or conv. */ | |
10515 | if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR | |
10516 | || IDENTIFIER_TYPENAME_P (unqualified_name) | |
10517 | || constructor_name_p (unqualified_name, class_type)) | |
7efa3e22 | 10518 | *ctor_dtor_or_conv_p = -1; |
a723baf1 | 10519 | } |
62b8a44e NS |
10520 | |
10521 | handle_declarator:; | |
10522 | scope = get_scope_of_declarator (declarator); | |
10523 | if (scope) | |
10524 | /* Any names that appear after the declarator-id for a member | |
10525 | are looked up in the containing scope. */ | |
10526 | push_scope (scope); | |
10527 | parser->in_declarator_p = true; | |
10528 | if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p) | |
10529 | || (declarator | |
10530 | && (TREE_CODE (declarator) == SCOPE_REF | |
10531 | || TREE_CODE (declarator) == IDENTIFIER_NODE))) | |
10532 | /* Default args are only allowed on function | |
10533 | declarations. */ | |
10534 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
a723baf1 | 10535 | else |
62b8a44e NS |
10536 | parser->default_arg_ok_p = false; |
10537 | ||
10538 | first = false; | |
a723baf1 | 10539 | } |
62b8a44e | 10540 | /* We're done. */ |
a723baf1 MM |
10541 | else |
10542 | break; | |
a723baf1 MM |
10543 | } |
10544 | ||
10545 | /* For an abstract declarator, we might wind up with nothing at this | |
10546 | point. That's an error; the declarator is not optional. */ | |
10547 | if (!declarator) | |
10548 | cp_parser_error (parser, "expected declarator"); | |
10549 | ||
10550 | /* If we entered a scope, we must exit it now. */ | |
10551 | if (scope) | |
10552 | pop_scope (scope); | |
10553 | ||
10554 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
10555 | parser->in_declarator_p = saved_in_declarator_p; | |
10556 | ||
10557 | return declarator; | |
10558 | } | |
10559 | ||
10560 | /* Parse a ptr-operator. | |
10561 | ||
10562 | ptr-operator: | |
10563 | * cv-qualifier-seq [opt] | |
10564 | & | |
10565 | :: [opt] nested-name-specifier * cv-qualifier-seq [opt] | |
10566 | ||
10567 | GNU Extension: | |
10568 | ||
10569 | ptr-operator: | |
10570 | & cv-qualifier-seq [opt] | |
10571 | ||
10572 | Returns INDIRECT_REF if a pointer, or pointer-to-member, was | |
10573 | used. Returns ADDR_EXPR if a reference was used. In the | |
10574 | case of a pointer-to-member, *TYPE is filled in with the | |
10575 | TYPE containing the member. *CV_QUALIFIER_SEQ is filled in | |
10576 | with the cv-qualifier-seq, or NULL_TREE, if there are no | |
10577 | cv-qualifiers. Returns ERROR_MARK if an error occurred. */ | |
10578 | ||
10579 | static enum tree_code | |
94edc4ab NN |
10580 | cp_parser_ptr_operator (cp_parser* parser, |
10581 | tree* type, | |
10582 | tree* cv_qualifier_seq) | |
a723baf1 MM |
10583 | { |
10584 | enum tree_code code = ERROR_MARK; | |
10585 | cp_token *token; | |
10586 | ||
10587 | /* Assume that it's not a pointer-to-member. */ | |
10588 | *type = NULL_TREE; | |
10589 | /* And that there are no cv-qualifiers. */ | |
10590 | *cv_qualifier_seq = NULL_TREE; | |
10591 | ||
10592 | /* Peek at the next token. */ | |
10593 | token = cp_lexer_peek_token (parser->lexer); | |
10594 | /* If it's a `*' or `&' we have a pointer or reference. */ | |
10595 | if (token->type == CPP_MULT || token->type == CPP_AND) | |
10596 | { | |
10597 | /* Remember which ptr-operator we were processing. */ | |
10598 | code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF); | |
10599 | ||
10600 | /* Consume the `*' or `&'. */ | |
10601 | cp_lexer_consume_token (parser->lexer); | |
10602 | ||
10603 | /* A `*' can be followed by a cv-qualifier-seq, and so can a | |
10604 | `&', if we are allowing GNU extensions. (The only qualifier | |
10605 | that can legally appear after `&' is `restrict', but that is | |
10606 | enforced during semantic analysis. */ | |
10607 | if (code == INDIRECT_REF | |
10608 | || cp_parser_allow_gnu_extensions_p (parser)) | |
10609 | *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser); | |
10610 | } | |
10611 | else | |
10612 | { | |
10613 | /* Try the pointer-to-member case. */ | |
10614 | cp_parser_parse_tentatively (parser); | |
10615 | /* Look for the optional `::' operator. */ | |
10616 | cp_parser_global_scope_opt (parser, | |
10617 | /*current_scope_valid_p=*/false); | |
10618 | /* Look for the nested-name specifier. */ | |
10619 | cp_parser_nested_name_specifier (parser, | |
10620 | /*typename_keyword_p=*/false, | |
10621 | /*check_dependency_p=*/true, | |
a668c6ad MM |
10622 | /*type_p=*/false, |
10623 | /*is_declaration=*/false); | |
a723baf1 MM |
10624 | /* If we found it, and the next token is a `*', then we are |
10625 | indeed looking at a pointer-to-member operator. */ | |
10626 | if (!cp_parser_error_occurred (parser) | |
10627 | && cp_parser_require (parser, CPP_MULT, "`*'")) | |
10628 | { | |
10629 | /* The type of which the member is a member is given by the | |
10630 | current SCOPE. */ | |
10631 | *type = parser->scope; | |
10632 | /* The next name will not be qualified. */ | |
10633 | parser->scope = NULL_TREE; | |
10634 | parser->qualifying_scope = NULL_TREE; | |
10635 | parser->object_scope = NULL_TREE; | |
10636 | /* Indicate that the `*' operator was used. */ | |
10637 | code = INDIRECT_REF; | |
10638 | /* Look for the optional cv-qualifier-seq. */ | |
10639 | *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser); | |
10640 | } | |
10641 | /* If that didn't work we don't have a ptr-operator. */ | |
10642 | if (!cp_parser_parse_definitely (parser)) | |
10643 | cp_parser_error (parser, "expected ptr-operator"); | |
10644 | } | |
10645 | ||
10646 | return code; | |
10647 | } | |
10648 | ||
10649 | /* Parse an (optional) cv-qualifier-seq. | |
10650 | ||
10651 | cv-qualifier-seq: | |
10652 | cv-qualifier cv-qualifier-seq [opt] | |
10653 | ||
10654 | Returns a TREE_LIST. The TREE_VALUE of each node is the | |
10655 | representation of a cv-qualifier. */ | |
10656 | ||
10657 | static tree | |
94edc4ab | 10658 | cp_parser_cv_qualifier_seq_opt (cp_parser* parser) |
a723baf1 MM |
10659 | { |
10660 | tree cv_qualifiers = NULL_TREE; | |
10661 | ||
10662 | while (true) | |
10663 | { | |
10664 | tree cv_qualifier; | |
10665 | ||
10666 | /* Look for the next cv-qualifier. */ | |
10667 | cv_qualifier = cp_parser_cv_qualifier_opt (parser); | |
10668 | /* If we didn't find one, we're done. */ | |
10669 | if (!cv_qualifier) | |
10670 | break; | |
10671 | ||
10672 | /* Add this cv-qualifier to the list. */ | |
10673 | cv_qualifiers | |
10674 | = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers); | |
10675 | } | |
10676 | ||
10677 | /* We built up the list in reverse order. */ | |
10678 | return nreverse (cv_qualifiers); | |
10679 | } | |
10680 | ||
10681 | /* Parse an (optional) cv-qualifier. | |
10682 | ||
10683 | cv-qualifier: | |
10684 | const | |
10685 | volatile | |
10686 | ||
10687 | GNU Extension: | |
10688 | ||
10689 | cv-qualifier: | |
10690 | __restrict__ */ | |
10691 | ||
10692 | static tree | |
94edc4ab | 10693 | cp_parser_cv_qualifier_opt (cp_parser* parser) |
a723baf1 MM |
10694 | { |
10695 | cp_token *token; | |
10696 | tree cv_qualifier = NULL_TREE; | |
10697 | ||
10698 | /* Peek at the next token. */ | |
10699 | token = cp_lexer_peek_token (parser->lexer); | |
10700 | /* See if it's a cv-qualifier. */ | |
10701 | switch (token->keyword) | |
10702 | { | |
10703 | case RID_CONST: | |
10704 | case RID_VOLATILE: | |
10705 | case RID_RESTRICT: | |
10706 | /* Save the value of the token. */ | |
10707 | cv_qualifier = token->value; | |
10708 | /* Consume the token. */ | |
10709 | cp_lexer_consume_token (parser->lexer); | |
10710 | break; | |
10711 | ||
10712 | default: | |
10713 | break; | |
10714 | } | |
10715 | ||
10716 | return cv_qualifier; | |
10717 | } | |
10718 | ||
10719 | /* Parse a declarator-id. | |
10720 | ||
10721 | declarator-id: | |
10722 | id-expression | |
10723 | :: [opt] nested-name-specifier [opt] type-name | |
10724 | ||
10725 | In the `id-expression' case, the value returned is as for | |
10726 | cp_parser_id_expression if the id-expression was an unqualified-id. | |
10727 | If the id-expression was a qualified-id, then a SCOPE_REF is | |
10728 | returned. The first operand is the scope (either a NAMESPACE_DECL | |
10729 | or TREE_TYPE), but the second is still just a representation of an | |
10730 | unqualified-id. */ | |
10731 | ||
10732 | static tree | |
94edc4ab | 10733 | cp_parser_declarator_id (cp_parser* parser) |
a723baf1 MM |
10734 | { |
10735 | tree id_expression; | |
10736 | ||
10737 | /* The expression must be an id-expression. Assume that qualified | |
10738 | names are the names of types so that: | |
10739 | ||
10740 | template <class T> | |
10741 | int S<T>::R::i = 3; | |
10742 | ||
10743 | will work; we must treat `S<T>::R' as the name of a type. | |
10744 | Similarly, assume that qualified names are templates, where | |
10745 | required, so that: | |
10746 | ||
10747 | template <class T> | |
10748 | int S<T>::R<T>::i = 3; | |
10749 | ||
10750 | will work, too. */ | |
10751 | id_expression = cp_parser_id_expression (parser, | |
10752 | /*template_keyword_p=*/false, | |
10753 | /*check_dependency_p=*/false, | |
f3c2dfc6 MM |
10754 | /*template_p=*/NULL, |
10755 | /*declarator_p=*/true); | |
a723baf1 MM |
10756 | /* If the name was qualified, create a SCOPE_REF to represent |
10757 | that. */ | |
10758 | if (parser->scope) | |
ec20aa6c MM |
10759 | { |
10760 | id_expression = build_nt (SCOPE_REF, parser->scope, id_expression); | |
10761 | parser->scope = NULL_TREE; | |
10762 | } | |
a723baf1 MM |
10763 | |
10764 | return id_expression; | |
10765 | } | |
10766 | ||
10767 | /* Parse a type-id. | |
10768 | ||
10769 | type-id: | |
10770 | type-specifier-seq abstract-declarator [opt] | |
10771 | ||
10772 | Returns the TYPE specified. */ | |
10773 | ||
10774 | static tree | |
94edc4ab | 10775 | cp_parser_type_id (cp_parser* parser) |
a723baf1 MM |
10776 | { |
10777 | tree type_specifier_seq; | |
10778 | tree abstract_declarator; | |
10779 | ||
10780 | /* Parse the type-specifier-seq. */ | |
10781 | type_specifier_seq | |
10782 | = cp_parser_type_specifier_seq (parser); | |
10783 | if (type_specifier_seq == error_mark_node) | |
10784 | return error_mark_node; | |
10785 | ||
10786 | /* There might or might not be an abstract declarator. */ | |
10787 | cp_parser_parse_tentatively (parser); | |
10788 | /* Look for the declarator. */ | |
10789 | abstract_declarator | |
4bb8ca28 MM |
10790 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL, |
10791 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
10792 | /* Check to see if there really was a declarator. */ |
10793 | if (!cp_parser_parse_definitely (parser)) | |
10794 | abstract_declarator = NULL_TREE; | |
10795 | ||
10796 | return groktypename (build_tree_list (type_specifier_seq, | |
10797 | abstract_declarator)); | |
10798 | } | |
10799 | ||
10800 | /* Parse a type-specifier-seq. | |
10801 | ||
10802 | type-specifier-seq: | |
10803 | type-specifier type-specifier-seq [opt] | |
10804 | ||
10805 | GNU extension: | |
10806 | ||
10807 | type-specifier-seq: | |
10808 | attributes type-specifier-seq [opt] | |
10809 | ||
10810 | Returns a TREE_LIST. Either the TREE_VALUE of each node is a | |
10811 | type-specifier, or the TREE_PURPOSE is a list of attributes. */ | |
10812 | ||
10813 | static tree | |
94edc4ab | 10814 | cp_parser_type_specifier_seq (cp_parser* parser) |
a723baf1 MM |
10815 | { |
10816 | bool seen_type_specifier = false; | |
10817 | tree type_specifier_seq = NULL_TREE; | |
10818 | ||
10819 | /* Parse the type-specifiers and attributes. */ | |
10820 | while (true) | |
10821 | { | |
10822 | tree type_specifier; | |
10823 | ||
10824 | /* Check for attributes first. */ | |
10825 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) | |
10826 | { | |
10827 | type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser), | |
10828 | NULL_TREE, | |
10829 | type_specifier_seq); | |
10830 | continue; | |
10831 | } | |
10832 | ||
10833 | /* After the first type-specifier, others are optional. */ | |
10834 | if (seen_type_specifier) | |
10835 | cp_parser_parse_tentatively (parser); | |
10836 | /* Look for the type-specifier. */ | |
10837 | type_specifier = cp_parser_type_specifier (parser, | |
10838 | CP_PARSER_FLAGS_NONE, | |
10839 | /*is_friend=*/false, | |
10840 | /*is_declaration=*/false, | |
10841 | NULL, | |
10842 | NULL); | |
10843 | /* If the first type-specifier could not be found, this is not a | |
10844 | type-specifier-seq at all. */ | |
10845 | if (!seen_type_specifier && type_specifier == error_mark_node) | |
10846 | return error_mark_node; | |
10847 | /* If subsequent type-specifiers could not be found, the | |
10848 | type-specifier-seq is complete. */ | |
10849 | else if (seen_type_specifier && !cp_parser_parse_definitely (parser)) | |
10850 | break; | |
10851 | ||
10852 | /* Add the new type-specifier to the list. */ | |
10853 | type_specifier_seq | |
10854 | = tree_cons (NULL_TREE, type_specifier, type_specifier_seq); | |
10855 | seen_type_specifier = true; | |
10856 | } | |
10857 | ||
10858 | /* We built up the list in reverse order. */ | |
10859 | return nreverse (type_specifier_seq); | |
10860 | } | |
10861 | ||
10862 | /* Parse a parameter-declaration-clause. | |
10863 | ||
10864 | parameter-declaration-clause: | |
10865 | parameter-declaration-list [opt] ... [opt] | |
10866 | parameter-declaration-list , ... | |
10867 | ||
10868 | Returns a representation for the parameter declarations. Each node | |
10869 | is a TREE_LIST. (See cp_parser_parameter_declaration for the exact | |
10870 | representation.) If the parameter-declaration-clause ends with an | |
10871 | ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the | |
10872 | list. A return value of NULL_TREE indicates a | |
10873 | parameter-declaration-clause consisting only of an ellipsis. */ | |
10874 | ||
10875 | static tree | |
94edc4ab | 10876 | cp_parser_parameter_declaration_clause (cp_parser* parser) |
a723baf1 MM |
10877 | { |
10878 | tree parameters; | |
10879 | cp_token *token; | |
10880 | bool ellipsis_p; | |
10881 | ||
10882 | /* Peek at the next token. */ | |
10883 | token = cp_lexer_peek_token (parser->lexer); | |
10884 | /* Check for trivial parameter-declaration-clauses. */ | |
10885 | if (token->type == CPP_ELLIPSIS) | |
10886 | { | |
10887 | /* Consume the `...' token. */ | |
10888 | cp_lexer_consume_token (parser->lexer); | |
10889 | return NULL_TREE; | |
10890 | } | |
10891 | else if (token->type == CPP_CLOSE_PAREN) | |
10892 | /* There are no parameters. */ | |
c73aecdf DE |
10893 | { |
10894 | #ifndef NO_IMPLICIT_EXTERN_C | |
10895 | if (in_system_header && current_class_type == NULL | |
10896 | && current_lang_name == lang_name_c) | |
10897 | return NULL_TREE; | |
10898 | else | |
10899 | #endif | |
10900 | return void_list_node; | |
10901 | } | |
a723baf1 MM |
10902 | /* Check for `(void)', too, which is a special case. */ |
10903 | else if (token->keyword == RID_VOID | |
10904 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
10905 | == CPP_CLOSE_PAREN)) | |
10906 | { | |
10907 | /* Consume the `void' token. */ | |
10908 | cp_lexer_consume_token (parser->lexer); | |
10909 | /* There are no parameters. */ | |
10910 | return void_list_node; | |
10911 | } | |
10912 | ||
10913 | /* Parse the parameter-declaration-list. */ | |
10914 | parameters = cp_parser_parameter_declaration_list (parser); | |
10915 | /* If a parse error occurred while parsing the | |
10916 | parameter-declaration-list, then the entire | |
10917 | parameter-declaration-clause is erroneous. */ | |
10918 | if (parameters == error_mark_node) | |
10919 | return error_mark_node; | |
10920 | ||
10921 | /* Peek at the next token. */ | |
10922 | token = cp_lexer_peek_token (parser->lexer); | |
10923 | /* If it's a `,', the clause should terminate with an ellipsis. */ | |
10924 | if (token->type == CPP_COMMA) | |
10925 | { | |
10926 | /* Consume the `,'. */ | |
10927 | cp_lexer_consume_token (parser->lexer); | |
10928 | /* Expect an ellipsis. */ | |
10929 | ellipsis_p | |
10930 | = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL); | |
10931 | } | |
10932 | /* It might also be `...' if the optional trailing `,' was | |
10933 | omitted. */ | |
10934 | else if (token->type == CPP_ELLIPSIS) | |
10935 | { | |
10936 | /* Consume the `...' token. */ | |
10937 | cp_lexer_consume_token (parser->lexer); | |
10938 | /* And remember that we saw it. */ | |
10939 | ellipsis_p = true; | |
10940 | } | |
10941 | else | |
10942 | ellipsis_p = false; | |
10943 | ||
10944 | /* Finish the parameter list. */ | |
10945 | return finish_parmlist (parameters, ellipsis_p); | |
10946 | } | |
10947 | ||
10948 | /* Parse a parameter-declaration-list. | |
10949 | ||
10950 | parameter-declaration-list: | |
10951 | parameter-declaration | |
10952 | parameter-declaration-list , parameter-declaration | |
10953 | ||
10954 | Returns a representation of the parameter-declaration-list, as for | |
10955 | cp_parser_parameter_declaration_clause. However, the | |
10956 | `void_list_node' is never appended to the list. */ | |
10957 | ||
10958 | static tree | |
94edc4ab | 10959 | cp_parser_parameter_declaration_list (cp_parser* parser) |
a723baf1 MM |
10960 | { |
10961 | tree parameters = NULL_TREE; | |
10962 | ||
10963 | /* Look for more parameters. */ | |
10964 | while (true) | |
10965 | { | |
10966 | tree parameter; | |
4bb8ca28 | 10967 | bool parenthesized_p; |
a723baf1 MM |
10968 | /* Parse the parameter. */ |
10969 | parameter | |
4bb8ca28 MM |
10970 | = cp_parser_parameter_declaration (parser, |
10971 | /*template_parm_p=*/false, | |
10972 | &parenthesized_p); | |
ec194454 | 10973 | |
34cd5ae7 | 10974 | /* If a parse error occurred parsing the parameter declaration, |
a723baf1 MM |
10975 | then the entire parameter-declaration-list is erroneous. */ |
10976 | if (parameter == error_mark_node) | |
10977 | { | |
10978 | parameters = error_mark_node; | |
10979 | break; | |
10980 | } | |
10981 | /* Add the new parameter to the list. */ | |
10982 | TREE_CHAIN (parameter) = parameters; | |
10983 | parameters = parameter; | |
10984 | ||
10985 | /* Peek at the next token. */ | |
10986 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN) | |
10987 | || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
10988 | /* The parameter-declaration-list is complete. */ | |
10989 | break; | |
10990 | else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
10991 | { | |
10992 | cp_token *token; | |
10993 | ||
10994 | /* Peek at the next token. */ | |
10995 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
10996 | /* If it's an ellipsis, then the list is complete. */ | |
10997 | if (token->type == CPP_ELLIPSIS) | |
10998 | break; | |
10999 | /* Otherwise, there must be more parameters. Consume the | |
11000 | `,'. */ | |
11001 | cp_lexer_consume_token (parser->lexer); | |
4bb8ca28 MM |
11002 | /* When parsing something like: |
11003 | ||
11004 | int i(float f, double d) | |
11005 | ||
11006 | we can tell after seeing the declaration for "f" that we | |
11007 | are not looking at an initialization of a variable "i", | |
11008 | but rather at the declaration of a function "i". | |
11009 | ||
11010 | Due to the fact that the parsing of template arguments | |
11011 | (as specified to a template-id) requires backtracking we | |
11012 | cannot use this technique when inside a template argument | |
11013 | list. */ | |
11014 | if (!parser->in_template_argument_list_p | |
11015 | && cp_parser_parsing_tentatively (parser) | |
11016 | && !cp_parser_committed_to_tentative_parse (parser) | |
11017 | /* However, a parameter-declaration of the form | |
11018 | "foat(f)" (which is a valid declaration of a | |
11019 | parameter "f") can also be interpreted as an | |
11020 | expression (the conversion of "f" to "float"). */ | |
11021 | && !parenthesized_p) | |
11022 | cp_parser_commit_to_tentative_parse (parser); | |
a723baf1 MM |
11023 | } |
11024 | else | |
11025 | { | |
11026 | cp_parser_error (parser, "expected `,' or `...'"); | |
4bb8ca28 MM |
11027 | if (!cp_parser_parsing_tentatively (parser) |
11028 | || cp_parser_committed_to_tentative_parse (parser)) | |
11029 | cp_parser_skip_to_closing_parenthesis (parser, | |
11030 | /*recovering=*/true, | |
5c832178 | 11031 | /*or_comma=*/false, |
4bb8ca28 | 11032 | /*consume_paren=*/false); |
a723baf1 MM |
11033 | break; |
11034 | } | |
11035 | } | |
11036 | ||
11037 | /* We built up the list in reverse order; straighten it out now. */ | |
11038 | return nreverse (parameters); | |
11039 | } | |
11040 | ||
11041 | /* Parse a parameter declaration. | |
11042 | ||
11043 | parameter-declaration: | |
11044 | decl-specifier-seq declarator | |
11045 | decl-specifier-seq declarator = assignment-expression | |
11046 | decl-specifier-seq abstract-declarator [opt] | |
11047 | decl-specifier-seq abstract-declarator [opt] = assignment-expression | |
11048 | ||
ec194454 MM |
11049 | If TEMPLATE_PARM_P is TRUE, then this parameter-declaration |
11050 | declares a template parameter. (In that case, a non-nested `>' | |
11051 | token encountered during the parsing of the assignment-expression | |
11052 | is not interpreted as a greater-than operator.) | |
a723baf1 MM |
11053 | |
11054 | Returns a TREE_LIST representing the parameter-declaration. The | |
4bb8ca28 MM |
11055 | TREE_PURPOSE is the default argument expression, or NULL_TREE if |
11056 | there is no default argument. The TREE_VALUE is a representation | |
11057 | of the decl-specifier-seq and declarator. In particular, the | |
11058 | TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the | |
11059 | decl-specifier-seq and whose TREE_VALUE represents the declarator. | |
11060 | If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff | |
11061 | the declarator is of the form "(p)". */ | |
a723baf1 MM |
11062 | |
11063 | static tree | |
ec194454 | 11064 | cp_parser_parameter_declaration (cp_parser *parser, |
4bb8ca28 MM |
11065 | bool template_parm_p, |
11066 | bool *parenthesized_p) | |
a723baf1 | 11067 | { |
560ad596 | 11068 | int declares_class_or_enum; |
ec194454 | 11069 | bool greater_than_is_operator_p; |
a723baf1 MM |
11070 | tree decl_specifiers; |
11071 | tree attributes; | |
11072 | tree declarator; | |
11073 | tree default_argument; | |
11074 | tree parameter; | |
11075 | cp_token *token; | |
11076 | const char *saved_message; | |
11077 | ||
ec194454 MM |
11078 | /* In a template parameter, `>' is not an operator. |
11079 | ||
11080 | [temp.param] | |
11081 | ||
11082 | When parsing a default template-argument for a non-type | |
11083 | template-parameter, the first non-nested `>' is taken as the end | |
11084 | of the template parameter-list rather than a greater-than | |
11085 | operator. */ | |
11086 | greater_than_is_operator_p = !template_parm_p; | |
11087 | ||
a723baf1 MM |
11088 | /* Type definitions may not appear in parameter types. */ |
11089 | saved_message = parser->type_definition_forbidden_message; | |
11090 | parser->type_definition_forbidden_message | |
11091 | = "types may not be defined in parameter types"; | |
11092 | ||
11093 | /* Parse the declaration-specifiers. */ | |
11094 | decl_specifiers | |
11095 | = cp_parser_decl_specifier_seq (parser, | |
11096 | CP_PARSER_FLAGS_NONE, | |
11097 | &attributes, | |
11098 | &declares_class_or_enum); | |
11099 | /* If an error occurred, there's no reason to attempt to parse the | |
11100 | rest of the declaration. */ | |
11101 | if (cp_parser_error_occurred (parser)) | |
11102 | { | |
11103 | parser->type_definition_forbidden_message = saved_message; | |
11104 | return error_mark_node; | |
11105 | } | |
11106 | ||
11107 | /* Peek at the next token. */ | |
11108 | token = cp_lexer_peek_token (parser->lexer); | |
11109 | /* If the next token is a `)', `,', `=', `>', or `...', then there | |
11110 | is no declarator. */ | |
11111 | if (token->type == CPP_CLOSE_PAREN | |
11112 | || token->type == CPP_COMMA | |
11113 | || token->type == CPP_EQ | |
11114 | || token->type == CPP_ELLIPSIS | |
11115 | || token->type == CPP_GREATER) | |
4bb8ca28 MM |
11116 | { |
11117 | declarator = NULL_TREE; | |
11118 | if (parenthesized_p) | |
11119 | *parenthesized_p = false; | |
11120 | } | |
a723baf1 MM |
11121 | /* Otherwise, there should be a declarator. */ |
11122 | else | |
11123 | { | |
11124 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
11125 | parser->default_arg_ok_p = false; | |
11126 | ||
5c832178 MM |
11127 | /* After seeing a decl-specifier-seq, if the next token is not a |
11128 | "(", there is no possibility that the code is a valid | |
4f8163b1 MM |
11129 | expression. Therefore, if parsing tentatively, we commit at |
11130 | this point. */ | |
5c832178 | 11131 | if (!parser->in_template_argument_list_p |
643aee72 | 11132 | /* In an expression context, having seen: |
4f8163b1 | 11133 | |
a7324e75 | 11134 | (int((char ... |
4f8163b1 MM |
11135 | |
11136 | we cannot be sure whether we are looking at a | |
a7324e75 MM |
11137 | function-type (taking a "char" as a parameter) or a cast |
11138 | of some object of type "char" to "int". */ | |
4f8163b1 | 11139 | && !parser->in_type_id_in_expr_p |
5c832178 MM |
11140 | && cp_parser_parsing_tentatively (parser) |
11141 | && !cp_parser_committed_to_tentative_parse (parser) | |
11142 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) | |
11143 | cp_parser_commit_to_tentative_parse (parser); | |
11144 | /* Parse the declarator. */ | |
a723baf1 | 11145 | declarator = cp_parser_declarator (parser, |
62b8a44e | 11146 | CP_PARSER_DECLARATOR_EITHER, |
4bb8ca28 MM |
11147 | /*ctor_dtor_or_conv_p=*/NULL, |
11148 | parenthesized_p); | |
a723baf1 | 11149 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
4971227d MM |
11150 | /* After the declarator, allow more attributes. */ |
11151 | attributes = chainon (attributes, cp_parser_attributes_opt (parser)); | |
a723baf1 MM |
11152 | } |
11153 | ||
62b8a44e | 11154 | /* The restriction on defining new types applies only to the type |
a723baf1 MM |
11155 | of the parameter, not to the default argument. */ |
11156 | parser->type_definition_forbidden_message = saved_message; | |
11157 | ||
11158 | /* If the next token is `=', then process a default argument. */ | |
11159 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
11160 | { | |
11161 | bool saved_greater_than_is_operator_p; | |
11162 | /* Consume the `='. */ | |
11163 | cp_lexer_consume_token (parser->lexer); | |
11164 | ||
11165 | /* If we are defining a class, then the tokens that make up the | |
11166 | default argument must be saved and processed later. */ | |
ec194454 MM |
11167 | if (!template_parm_p && at_class_scope_p () |
11168 | && TYPE_BEING_DEFINED (current_class_type)) | |
a723baf1 MM |
11169 | { |
11170 | unsigned depth = 0; | |
11171 | ||
11172 | /* Create a DEFAULT_ARG to represented the unparsed default | |
11173 | argument. */ | |
11174 | default_argument = make_node (DEFAULT_ARG); | |
11175 | DEFARG_TOKENS (default_argument) = cp_token_cache_new (); | |
11176 | ||
11177 | /* Add tokens until we have processed the entire default | |
11178 | argument. */ | |
11179 | while (true) | |
11180 | { | |
11181 | bool done = false; | |
11182 | cp_token *token; | |
11183 | ||
11184 | /* Peek at the next token. */ | |
11185 | token = cp_lexer_peek_token (parser->lexer); | |
11186 | /* What we do depends on what token we have. */ | |
11187 | switch (token->type) | |
11188 | { | |
11189 | /* In valid code, a default argument must be | |
11190 | immediately followed by a `,' `)', or `...'. */ | |
11191 | case CPP_COMMA: | |
11192 | case CPP_CLOSE_PAREN: | |
11193 | case CPP_ELLIPSIS: | |
11194 | /* If we run into a non-nested `;', `}', or `]', | |
11195 | then the code is invalid -- but the default | |
11196 | argument is certainly over. */ | |
11197 | case CPP_SEMICOLON: | |
11198 | case CPP_CLOSE_BRACE: | |
11199 | case CPP_CLOSE_SQUARE: | |
11200 | if (depth == 0) | |
11201 | done = true; | |
11202 | /* Update DEPTH, if necessary. */ | |
11203 | else if (token->type == CPP_CLOSE_PAREN | |
11204 | || token->type == CPP_CLOSE_BRACE | |
11205 | || token->type == CPP_CLOSE_SQUARE) | |
11206 | --depth; | |
11207 | break; | |
11208 | ||
11209 | case CPP_OPEN_PAREN: | |
11210 | case CPP_OPEN_SQUARE: | |
11211 | case CPP_OPEN_BRACE: | |
11212 | ++depth; | |
11213 | break; | |
11214 | ||
11215 | case CPP_GREATER: | |
11216 | /* If we see a non-nested `>', and `>' is not an | |
11217 | operator, then it marks the end of the default | |
11218 | argument. */ | |
11219 | if (!depth && !greater_than_is_operator_p) | |
11220 | done = true; | |
11221 | break; | |
11222 | ||
11223 | /* If we run out of tokens, issue an error message. */ | |
11224 | case CPP_EOF: | |
11225 | error ("file ends in default argument"); | |
11226 | done = true; | |
11227 | break; | |
11228 | ||
11229 | case CPP_NAME: | |
11230 | case CPP_SCOPE: | |
11231 | /* In these cases, we should look for template-ids. | |
11232 | For example, if the default argument is | |
11233 | `X<int, double>()', we need to do name lookup to | |
11234 | figure out whether or not `X' is a template; if | |
34cd5ae7 | 11235 | so, the `,' does not end the default argument. |
a723baf1 MM |
11236 | |
11237 | That is not yet done. */ | |
11238 | break; | |
11239 | ||
11240 | default: | |
11241 | break; | |
11242 | } | |
11243 | ||
11244 | /* If we've reached the end, stop. */ | |
11245 | if (done) | |
11246 | break; | |
11247 | ||
11248 | /* Add the token to the token block. */ | |
11249 | token = cp_lexer_consume_token (parser->lexer); | |
11250 | cp_token_cache_push_token (DEFARG_TOKENS (default_argument), | |
11251 | token); | |
11252 | } | |
11253 | } | |
11254 | /* Outside of a class definition, we can just parse the | |
11255 | assignment-expression. */ | |
11256 | else | |
11257 | { | |
11258 | bool saved_local_variables_forbidden_p; | |
11259 | ||
11260 | /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is | |
11261 | set correctly. */ | |
11262 | saved_greater_than_is_operator_p | |
11263 | = parser->greater_than_is_operator_p; | |
11264 | parser->greater_than_is_operator_p = greater_than_is_operator_p; | |
11265 | /* Local variable names (and the `this' keyword) may not | |
11266 | appear in a default argument. */ | |
11267 | saved_local_variables_forbidden_p | |
11268 | = parser->local_variables_forbidden_p; | |
11269 | parser->local_variables_forbidden_p = true; | |
11270 | /* Parse the assignment-expression. */ | |
11271 | default_argument = cp_parser_assignment_expression (parser); | |
11272 | /* Restore saved state. */ | |
11273 | parser->greater_than_is_operator_p | |
11274 | = saved_greater_than_is_operator_p; | |
11275 | parser->local_variables_forbidden_p | |
11276 | = saved_local_variables_forbidden_p; | |
11277 | } | |
11278 | if (!parser->default_arg_ok_p) | |
11279 | { | |
c67d36d0 NS |
11280 | if (!flag_pedantic_errors) |
11281 | warning ("deprecated use of default argument for parameter of non-function"); | |
11282 | else | |
11283 | { | |
11284 | error ("default arguments are only permitted for function parameters"); | |
11285 | default_argument = NULL_TREE; | |
11286 | } | |
a723baf1 MM |
11287 | } |
11288 | } | |
11289 | else | |
11290 | default_argument = NULL_TREE; | |
11291 | ||
11292 | /* Create the representation of the parameter. */ | |
11293 | if (attributes) | |
11294 | decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers); | |
11295 | parameter = build_tree_list (default_argument, | |
11296 | build_tree_list (decl_specifiers, | |
11297 | declarator)); | |
11298 | ||
11299 | return parameter; | |
11300 | } | |
11301 | ||
a723baf1 MM |
11302 | /* Parse a function-body. |
11303 | ||
11304 | function-body: | |
11305 | compound_statement */ | |
11306 | ||
11307 | static void | |
11308 | cp_parser_function_body (cp_parser *parser) | |
11309 | { | |
a5bcc582 | 11310 | cp_parser_compound_statement (parser, false); |
a723baf1 MM |
11311 | } |
11312 | ||
11313 | /* Parse a ctor-initializer-opt followed by a function-body. Return | |
11314 | true if a ctor-initializer was present. */ | |
11315 | ||
11316 | static bool | |
11317 | cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser) | |
11318 | { | |
11319 | tree body; | |
11320 | bool ctor_initializer_p; | |
11321 | ||
11322 | /* Begin the function body. */ | |
11323 | body = begin_function_body (); | |
11324 | /* Parse the optional ctor-initializer. */ | |
11325 | ctor_initializer_p = cp_parser_ctor_initializer_opt (parser); | |
11326 | /* Parse the function-body. */ | |
11327 | cp_parser_function_body (parser); | |
11328 | /* Finish the function body. */ | |
11329 | finish_function_body (body); | |
11330 | ||
11331 | return ctor_initializer_p; | |
11332 | } | |
11333 | ||
11334 | /* Parse an initializer. | |
11335 | ||
11336 | initializer: | |
11337 | = initializer-clause | |
11338 | ( expression-list ) | |
11339 | ||
11340 | Returns a expression representing the initializer. If no | |
11341 | initializer is present, NULL_TREE is returned. | |
11342 | ||
11343 | *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )' | |
11344 | production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is | |
39703eb9 MM |
11345 | set to FALSE if there is no initializer present. If there is an |
11346 | initializer, and it is not a constant-expression, *NON_CONSTANT_P | |
11347 | is set to true; otherwise it is set to false. */ | |
a723baf1 MM |
11348 | |
11349 | static tree | |
39703eb9 MM |
11350 | cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init, |
11351 | bool* non_constant_p) | |
a723baf1 MM |
11352 | { |
11353 | cp_token *token; | |
11354 | tree init; | |
11355 | ||
11356 | /* Peek at the next token. */ | |
11357 | token = cp_lexer_peek_token (parser->lexer); | |
11358 | ||
11359 | /* Let our caller know whether or not this initializer was | |
11360 | parenthesized. */ | |
11361 | *is_parenthesized_init = (token->type == CPP_OPEN_PAREN); | |
39703eb9 MM |
11362 | /* Assume that the initializer is constant. */ |
11363 | *non_constant_p = false; | |
a723baf1 MM |
11364 | |
11365 | if (token->type == CPP_EQ) | |
11366 | { | |
11367 | /* Consume the `='. */ | |
11368 | cp_lexer_consume_token (parser->lexer); | |
11369 | /* Parse the initializer-clause. */ | |
39703eb9 | 11370 | init = cp_parser_initializer_clause (parser, non_constant_p); |
a723baf1 MM |
11371 | } |
11372 | else if (token->type == CPP_OPEN_PAREN) | |
39703eb9 MM |
11373 | init = cp_parser_parenthesized_expression_list (parser, false, |
11374 | non_constant_p); | |
a723baf1 MM |
11375 | else |
11376 | { | |
11377 | /* Anything else is an error. */ | |
11378 | cp_parser_error (parser, "expected initializer"); | |
11379 | init = error_mark_node; | |
11380 | } | |
11381 | ||
11382 | return init; | |
11383 | } | |
11384 | ||
11385 | /* Parse an initializer-clause. | |
11386 | ||
11387 | initializer-clause: | |
11388 | assignment-expression | |
11389 | { initializer-list , [opt] } | |
11390 | { } | |
11391 | ||
11392 | Returns an expression representing the initializer. | |
11393 | ||
11394 | If the `assignment-expression' production is used the value | |
34cd5ae7 | 11395 | returned is simply a representation for the expression. |
a723baf1 MM |
11396 | |
11397 | Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be | |
11398 | the elements of the initializer-list (or NULL_TREE, if the last | |
11399 | production is used). The TREE_TYPE for the CONSTRUCTOR will be | |
11400 | NULL_TREE. There is no way to detect whether or not the optional | |
39703eb9 MM |
11401 | trailing `,' was provided. NON_CONSTANT_P is as for |
11402 | cp_parser_initializer. */ | |
a723baf1 MM |
11403 | |
11404 | static tree | |
39703eb9 | 11405 | cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p) |
a723baf1 MM |
11406 | { |
11407 | tree initializer; | |
11408 | ||
11409 | /* If it is not a `{', then we are looking at an | |
11410 | assignment-expression. */ | |
11411 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
39703eb9 MM |
11412 | initializer |
11413 | = cp_parser_constant_expression (parser, | |
11414 | /*allow_non_constant_p=*/true, | |
11415 | non_constant_p); | |
a723baf1 MM |
11416 | else |
11417 | { | |
11418 | /* Consume the `{' token. */ | |
11419 | cp_lexer_consume_token (parser->lexer); | |
11420 | /* Create a CONSTRUCTOR to represent the braced-initializer. */ | |
11421 | initializer = make_node (CONSTRUCTOR); | |
11422 | /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be | |
11423 | necessary, but check_initializer depends upon it, for | |
11424 | now. */ | |
11425 | TREE_HAS_CONSTRUCTOR (initializer) = 1; | |
11426 | /* If it's not a `}', then there is a non-trivial initializer. */ | |
11427 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)) | |
11428 | { | |
11429 | /* Parse the initializer list. */ | |
11430 | CONSTRUCTOR_ELTS (initializer) | |
39703eb9 | 11431 | = cp_parser_initializer_list (parser, non_constant_p); |
a723baf1 MM |
11432 | /* A trailing `,' token is allowed. */ |
11433 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
11434 | cp_lexer_consume_token (parser->lexer); | |
11435 | } | |
a723baf1 MM |
11436 | /* Now, there should be a trailing `}'. */ |
11437 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
11438 | } | |
11439 | ||
11440 | return initializer; | |
11441 | } | |
11442 | ||
11443 | /* Parse an initializer-list. | |
11444 | ||
11445 | initializer-list: | |
11446 | initializer-clause | |
11447 | initializer-list , initializer-clause | |
11448 | ||
11449 | GNU Extension: | |
11450 | ||
11451 | initializer-list: | |
11452 | identifier : initializer-clause | |
11453 | initializer-list, identifier : initializer-clause | |
11454 | ||
11455 | Returns a TREE_LIST. The TREE_VALUE of each node is an expression | |
11456 | for the initializer. If the TREE_PURPOSE is non-NULL, it is the | |
39703eb9 MM |
11457 | IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is |
11458 | as for cp_parser_initializer. */ | |
a723baf1 MM |
11459 | |
11460 | static tree | |
39703eb9 | 11461 | cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p) |
a723baf1 MM |
11462 | { |
11463 | tree initializers = NULL_TREE; | |
11464 | ||
39703eb9 MM |
11465 | /* Assume all of the expressions are constant. */ |
11466 | *non_constant_p = false; | |
11467 | ||
a723baf1 MM |
11468 | /* Parse the rest of the list. */ |
11469 | while (true) | |
11470 | { | |
11471 | cp_token *token; | |
11472 | tree identifier; | |
11473 | tree initializer; | |
39703eb9 MM |
11474 | bool clause_non_constant_p; |
11475 | ||
a723baf1 MM |
11476 | /* If the next token is an identifier and the following one is a |
11477 | colon, we are looking at the GNU designated-initializer | |
11478 | syntax. */ | |
11479 | if (cp_parser_allow_gnu_extensions_p (parser) | |
11480 | && cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
11481 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON) | |
11482 | { | |
11483 | /* Consume the identifier. */ | |
11484 | identifier = cp_lexer_consume_token (parser->lexer)->value; | |
11485 | /* Consume the `:'. */ | |
11486 | cp_lexer_consume_token (parser->lexer); | |
11487 | } | |
11488 | else | |
11489 | identifier = NULL_TREE; | |
11490 | ||
11491 | /* Parse the initializer. */ | |
39703eb9 MM |
11492 | initializer = cp_parser_initializer_clause (parser, |
11493 | &clause_non_constant_p); | |
11494 | /* If any clause is non-constant, so is the entire initializer. */ | |
11495 | if (clause_non_constant_p) | |
11496 | *non_constant_p = true; | |
a723baf1 MM |
11497 | /* Add it to the list. */ |
11498 | initializers = tree_cons (identifier, initializer, initializers); | |
11499 | ||
11500 | /* If the next token is not a comma, we have reached the end of | |
11501 | the list. */ | |
11502 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
11503 | break; | |
11504 | ||
11505 | /* Peek at the next token. */ | |
11506 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
11507 | /* If the next token is a `}', then we're still done. An | |
11508 | initializer-clause can have a trailing `,' after the | |
11509 | initializer-list and before the closing `}'. */ | |
11510 | if (token->type == CPP_CLOSE_BRACE) | |
11511 | break; | |
11512 | ||
11513 | /* Consume the `,' token. */ | |
11514 | cp_lexer_consume_token (parser->lexer); | |
11515 | } | |
11516 | ||
11517 | /* The initializers were built up in reverse order, so we need to | |
11518 | reverse them now. */ | |
11519 | return nreverse (initializers); | |
11520 | } | |
11521 | ||
11522 | /* Classes [gram.class] */ | |
11523 | ||
11524 | /* Parse a class-name. | |
11525 | ||
11526 | class-name: | |
11527 | identifier | |
11528 | template-id | |
11529 | ||
11530 | TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used | |
11531 | to indicate that names looked up in dependent types should be | |
11532 | assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template' | |
11533 | keyword has been used to indicate that the name that appears next | |
11534 | is a template. TYPE_P is true iff the next name should be treated | |
11535 | as class-name, even if it is declared to be some other kind of name | |
8d241e0b KL |
11536 | as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in |
11537 | dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class | |
11538 | being defined in a class-head. | |
a723baf1 MM |
11539 | |
11540 | Returns the TYPE_DECL representing the class. */ | |
11541 | ||
11542 | static tree | |
11543 | cp_parser_class_name (cp_parser *parser, | |
11544 | bool typename_keyword_p, | |
11545 | bool template_keyword_p, | |
11546 | bool type_p, | |
a723baf1 | 11547 | bool check_dependency_p, |
a668c6ad MM |
11548 | bool class_head_p, |
11549 | bool is_declaration) | |
a723baf1 MM |
11550 | { |
11551 | tree decl; | |
11552 | tree scope; | |
11553 | bool typename_p; | |
e5976695 MM |
11554 | cp_token *token; |
11555 | ||
11556 | /* All class-names start with an identifier. */ | |
11557 | token = cp_lexer_peek_token (parser->lexer); | |
11558 | if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID) | |
11559 | { | |
11560 | cp_parser_error (parser, "expected class-name"); | |
11561 | return error_mark_node; | |
11562 | } | |
11563 | ||
a723baf1 MM |
11564 | /* PARSER->SCOPE can be cleared when parsing the template-arguments |
11565 | to a template-id, so we save it here. */ | |
11566 | scope = parser->scope; | |
3adee96c KL |
11567 | if (scope == error_mark_node) |
11568 | return error_mark_node; | |
11569 | ||
a723baf1 MM |
11570 | /* Any name names a type if we're following the `typename' keyword |
11571 | in a qualified name where the enclosing scope is type-dependent. */ | |
11572 | typename_p = (typename_keyword_p && scope && TYPE_P (scope) | |
1fb3244a | 11573 | && dependent_type_p (scope)); |
e5976695 MM |
11574 | /* Handle the common case (an identifier, but not a template-id) |
11575 | efficiently. */ | |
11576 | if (token->type == CPP_NAME | |
f4abade9 | 11577 | && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2)) |
a723baf1 | 11578 | { |
a723baf1 MM |
11579 | tree identifier; |
11580 | ||
11581 | /* Look for the identifier. */ | |
11582 | identifier = cp_parser_identifier (parser); | |
11583 | /* If the next token isn't an identifier, we are certainly not | |
11584 | looking at a class-name. */ | |
11585 | if (identifier == error_mark_node) | |
11586 | decl = error_mark_node; | |
11587 | /* If we know this is a type-name, there's no need to look it | |
11588 | up. */ | |
11589 | else if (typename_p) | |
11590 | decl = identifier; | |
11591 | else | |
11592 | { | |
11593 | /* If the next token is a `::', then the name must be a type | |
11594 | name. | |
11595 | ||
11596 | [basic.lookup.qual] | |
11597 | ||
11598 | During the lookup for a name preceding the :: scope | |
11599 | resolution operator, object, function, and enumerator | |
11600 | names are ignored. */ | |
11601 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
11602 | type_p = true; | |
11603 | /* Look up the name. */ | |
11604 | decl = cp_parser_lookup_name (parser, identifier, | |
a723baf1 | 11605 | type_p, |
b0bc6e8e | 11606 | /*is_template=*/false, |
eea9800f | 11607 | /*is_namespace=*/false, |
a723baf1 MM |
11608 | check_dependency_p); |
11609 | } | |
11610 | } | |
e5976695 MM |
11611 | else |
11612 | { | |
11613 | /* Try a template-id. */ | |
11614 | decl = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
11615 | check_dependency_p, |
11616 | is_declaration); | |
e5976695 MM |
11617 | if (decl == error_mark_node) |
11618 | return error_mark_node; | |
11619 | } | |
a723baf1 MM |
11620 | |
11621 | decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p); | |
11622 | ||
11623 | /* If this is a typename, create a TYPENAME_TYPE. */ | |
11624 | if (typename_p && decl != error_mark_node) | |
11625 | decl = TYPE_NAME (make_typename_type (scope, decl, | |
11626 | /*complain=*/1)); | |
11627 | ||
11628 | /* Check to see that it is really the name of a class. */ | |
11629 | if (TREE_CODE (decl) == TEMPLATE_ID_EXPR | |
11630 | && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE | |
11631 | && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
11632 | /* Situations like this: | |
11633 | ||
11634 | template <typename T> struct A { | |
11635 | typename T::template X<int>::I i; | |
11636 | }; | |
11637 | ||
11638 | are problematic. Is `T::template X<int>' a class-name? The | |
11639 | standard does not seem to be definitive, but there is no other | |
11640 | valid interpretation of the following `::'. Therefore, those | |
11641 | names are considered class-names. */ | |
78757caa | 11642 | decl = TYPE_NAME (make_typename_type (scope, decl, tf_error)); |
a723baf1 MM |
11643 | else if (decl == error_mark_node |
11644 | || TREE_CODE (decl) != TYPE_DECL | |
11645 | || !IS_AGGR_TYPE (TREE_TYPE (decl))) | |
11646 | { | |
11647 | cp_parser_error (parser, "expected class-name"); | |
11648 | return error_mark_node; | |
11649 | } | |
11650 | ||
11651 | return decl; | |
11652 | } | |
11653 | ||
11654 | /* Parse a class-specifier. | |
11655 | ||
11656 | class-specifier: | |
11657 | class-head { member-specification [opt] } | |
11658 | ||
11659 | Returns the TREE_TYPE representing the class. */ | |
11660 | ||
11661 | static tree | |
94edc4ab | 11662 | cp_parser_class_specifier (cp_parser* parser) |
a723baf1 MM |
11663 | { |
11664 | cp_token *token; | |
11665 | tree type; | |
11666 | tree attributes = NULL_TREE; | |
11667 | int has_trailing_semicolon; | |
11668 | bool nested_name_specifier_p; | |
a723baf1 MM |
11669 | unsigned saved_num_template_parameter_lists; |
11670 | ||
8d241e0b | 11671 | push_deferring_access_checks (dk_no_deferred); |
cf22909c | 11672 | |
a723baf1 MM |
11673 | /* Parse the class-head. */ |
11674 | type = cp_parser_class_head (parser, | |
cf22909c | 11675 | &nested_name_specifier_p); |
a723baf1 MM |
11676 | /* If the class-head was a semantic disaster, skip the entire body |
11677 | of the class. */ | |
11678 | if (!type) | |
11679 | { | |
11680 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
cf22909c | 11681 | pop_deferring_access_checks (); |
a723baf1 MM |
11682 | return error_mark_node; |
11683 | } | |
cf22909c | 11684 | |
a723baf1 MM |
11685 | /* Look for the `{'. */ |
11686 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
cf22909c KL |
11687 | { |
11688 | pop_deferring_access_checks (); | |
11689 | return error_mark_node; | |
11690 | } | |
11691 | ||
a723baf1 MM |
11692 | /* Issue an error message if type-definitions are forbidden here. */ |
11693 | cp_parser_check_type_definition (parser); | |
11694 | /* Remember that we are defining one more class. */ | |
11695 | ++parser->num_classes_being_defined; | |
11696 | /* Inside the class, surrounding template-parameter-lists do not | |
11697 | apply. */ | |
11698 | saved_num_template_parameter_lists | |
11699 | = parser->num_template_parameter_lists; | |
11700 | parser->num_template_parameter_lists = 0; | |
78757caa | 11701 | |
a723baf1 | 11702 | /* Start the class. */ |
eeb23c11 MM |
11703 | if (nested_name_specifier_p) |
11704 | push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type))); | |
a723baf1 MM |
11705 | type = begin_class_definition (type); |
11706 | if (type == error_mark_node) | |
9bcb9aae | 11707 | /* If the type is erroneous, skip the entire body of the class. */ |
a723baf1 MM |
11708 | cp_parser_skip_to_closing_brace (parser); |
11709 | else | |
11710 | /* Parse the member-specification. */ | |
11711 | cp_parser_member_specification_opt (parser); | |
11712 | /* Look for the trailing `}'. */ | |
11713 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
11714 | /* We get better error messages by noticing a common problem: a | |
11715 | missing trailing `;'. */ | |
11716 | token = cp_lexer_peek_token (parser->lexer); | |
11717 | has_trailing_semicolon = (token->type == CPP_SEMICOLON); | |
11718 | /* Look for attributes to apply to this class. */ | |
11719 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
11720 | attributes = cp_parser_attributes_opt (parser); | |
560ad596 MM |
11721 | /* If we got any attributes in class_head, xref_tag will stick them in |
11722 | TREE_TYPE of the type. Grab them now. */ | |
11723 | if (type != error_mark_node) | |
11724 | { | |
11725 | attributes = chainon (TYPE_ATTRIBUTES (type), attributes); | |
11726 | TYPE_ATTRIBUTES (type) = NULL_TREE; | |
11727 | type = finish_struct (type, attributes); | |
11728 | } | |
11729 | if (nested_name_specifier_p) | |
11730 | pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type))); | |
a723baf1 MM |
11731 | /* If this class is not itself within the scope of another class, |
11732 | then we need to parse the bodies of all of the queued function | |
11733 | definitions. Note that the queued functions defined in a class | |
11734 | are not always processed immediately following the | |
11735 | class-specifier for that class. Consider: | |
11736 | ||
11737 | struct A { | |
11738 | struct B { void f() { sizeof (A); } }; | |
11739 | }; | |
11740 | ||
11741 | If `f' were processed before the processing of `A' were | |
11742 | completed, there would be no way to compute the size of `A'. | |
11743 | Note that the nesting we are interested in here is lexical -- | |
11744 | not the semantic nesting given by TYPE_CONTEXT. In particular, | |
11745 | for: | |
11746 | ||
11747 | struct A { struct B; }; | |
11748 | struct A::B { void f() { } }; | |
11749 | ||
11750 | there is no need to delay the parsing of `A::B::f'. */ | |
11751 | if (--parser->num_classes_being_defined == 0) | |
11752 | { | |
8218bd34 MM |
11753 | tree queue_entry; |
11754 | tree fn; | |
a723baf1 | 11755 | |
8218bd34 MM |
11756 | /* In a first pass, parse default arguments to the functions. |
11757 | Then, in a second pass, parse the bodies of the functions. | |
11758 | This two-phased approach handles cases like: | |
11759 | ||
11760 | struct S { | |
11761 | void f() { g(); } | |
11762 | void g(int i = 3); | |
11763 | }; | |
11764 | ||
11765 | */ | |
8db1028e NS |
11766 | for (TREE_PURPOSE (parser->unparsed_functions_queues) |
11767 | = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues)); | |
11768 | (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues)); | |
11769 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
11770 | = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues))) | |
8218bd34 MM |
11771 | { |
11772 | fn = TREE_VALUE (queue_entry); | |
8218bd34 MM |
11773 | /* Make sure that any template parameters are in scope. */ |
11774 | maybe_begin_member_template_processing (fn); | |
11775 | /* If there are default arguments that have not yet been processed, | |
11776 | take care of them now. */ | |
11777 | cp_parser_late_parsing_default_args (parser, fn); | |
11778 | /* Remove any template parameters from the symbol table. */ | |
11779 | maybe_end_member_template_processing (); | |
11780 | } | |
11781 | /* Now parse the body of the functions. */ | |
8db1028e NS |
11782 | for (TREE_VALUE (parser->unparsed_functions_queues) |
11783 | = nreverse (TREE_VALUE (parser->unparsed_functions_queues)); | |
11784 | (queue_entry = TREE_VALUE (parser->unparsed_functions_queues)); | |
11785 | TREE_VALUE (parser->unparsed_functions_queues) | |
11786 | = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues))) | |
a723baf1 | 11787 | { |
a723baf1 | 11788 | /* Figure out which function we need to process. */ |
a723baf1 MM |
11789 | fn = TREE_VALUE (queue_entry); |
11790 | ||
4543ee47 ZD |
11791 | /* A hack to prevent garbage collection. */ |
11792 | function_depth++; | |
11793 | ||
a723baf1 MM |
11794 | /* Parse the function. */ |
11795 | cp_parser_late_parsing_for_member (parser, fn); | |
4543ee47 | 11796 | function_depth--; |
a723baf1 MM |
11797 | } |
11798 | ||
a723baf1 MM |
11799 | } |
11800 | ||
11801 | /* Put back any saved access checks. */ | |
cf22909c | 11802 | pop_deferring_access_checks (); |
a723baf1 MM |
11803 | |
11804 | /* Restore the count of active template-parameter-lists. */ | |
11805 | parser->num_template_parameter_lists | |
11806 | = saved_num_template_parameter_lists; | |
11807 | ||
11808 | return type; | |
11809 | } | |
11810 | ||
11811 | /* Parse a class-head. | |
11812 | ||
11813 | class-head: | |
11814 | class-key identifier [opt] base-clause [opt] | |
11815 | class-key nested-name-specifier identifier base-clause [opt] | |
11816 | class-key nested-name-specifier [opt] template-id | |
11817 | base-clause [opt] | |
11818 | ||
11819 | GNU Extensions: | |
11820 | class-key attributes identifier [opt] base-clause [opt] | |
11821 | class-key attributes nested-name-specifier identifier base-clause [opt] | |
11822 | class-key attributes nested-name-specifier [opt] template-id | |
11823 | base-clause [opt] | |
11824 | ||
11825 | Returns the TYPE of the indicated class. Sets | |
11826 | *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions | |
11827 | involving a nested-name-specifier was used, and FALSE otherwise. | |
a723baf1 MM |
11828 | |
11829 | Returns NULL_TREE if the class-head is syntactically valid, but | |
11830 | semantically invalid in a way that means we should skip the entire | |
11831 | body of the class. */ | |
11832 | ||
11833 | static tree | |
94edc4ab NN |
11834 | cp_parser_class_head (cp_parser* parser, |
11835 | bool* nested_name_specifier_p) | |
a723baf1 MM |
11836 | { |
11837 | cp_token *token; | |
11838 | tree nested_name_specifier; | |
11839 | enum tag_types class_key; | |
11840 | tree id = NULL_TREE; | |
11841 | tree type = NULL_TREE; | |
11842 | tree attributes; | |
11843 | bool template_id_p = false; | |
11844 | bool qualified_p = false; | |
11845 | bool invalid_nested_name_p = false; | |
afb0918a | 11846 | bool invalid_explicit_specialization_p = false; |
a723baf1 MM |
11847 | unsigned num_templates; |
11848 | ||
11849 | /* Assume no nested-name-specifier will be present. */ | |
11850 | *nested_name_specifier_p = false; | |
11851 | /* Assume no template parameter lists will be used in defining the | |
11852 | type. */ | |
11853 | num_templates = 0; | |
11854 | ||
11855 | /* Look for the class-key. */ | |
11856 | class_key = cp_parser_class_key (parser); | |
11857 | if (class_key == none_type) | |
11858 | return error_mark_node; | |
11859 | ||
11860 | /* Parse the attributes. */ | |
11861 | attributes = cp_parser_attributes_opt (parser); | |
11862 | ||
11863 | /* If the next token is `::', that is invalid -- but sometimes | |
11864 | people do try to write: | |
11865 | ||
11866 | struct ::S {}; | |
11867 | ||
11868 | Handle this gracefully by accepting the extra qualifier, and then | |
11869 | issuing an error about it later if this really is a | |
2050a1bb | 11870 | class-head. If it turns out just to be an elaborated type |
a723baf1 MM |
11871 | specifier, remain silent. */ |
11872 | if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)) | |
11873 | qualified_p = true; | |
11874 | ||
8d241e0b KL |
11875 | push_deferring_access_checks (dk_no_check); |
11876 | ||
a723baf1 MM |
11877 | /* Determine the name of the class. Begin by looking for an |
11878 | optional nested-name-specifier. */ | |
11879 | nested_name_specifier | |
11880 | = cp_parser_nested_name_specifier_opt (parser, | |
11881 | /*typename_keyword_p=*/false, | |
66d418e6 | 11882 | /*check_dependency_p=*/false, |
a668c6ad MM |
11883 | /*type_p=*/false, |
11884 | /*is_declaration=*/false); | |
a723baf1 MM |
11885 | /* If there was a nested-name-specifier, then there *must* be an |
11886 | identifier. */ | |
11887 | if (nested_name_specifier) | |
11888 | { | |
11889 | /* Although the grammar says `identifier', it really means | |
11890 | `class-name' or `template-name'. You are only allowed to | |
11891 | define a class that has already been declared with this | |
11892 | syntax. | |
11893 | ||
11894 | The proposed resolution for Core Issue 180 says that whever | |
11895 | you see `class T::X' you should treat `X' as a type-name. | |
11896 | ||
11897 | It is OK to define an inaccessible class; for example: | |
11898 | ||
11899 | class A { class B; }; | |
11900 | class A::B {}; | |
11901 | ||
a723baf1 MM |
11902 | We do not know if we will see a class-name, or a |
11903 | template-name. We look for a class-name first, in case the | |
11904 | class-name is a template-id; if we looked for the | |
11905 | template-name first we would stop after the template-name. */ | |
11906 | cp_parser_parse_tentatively (parser); | |
11907 | type = cp_parser_class_name (parser, | |
11908 | /*typename_keyword_p=*/false, | |
11909 | /*template_keyword_p=*/false, | |
11910 | /*type_p=*/true, | |
a723baf1 | 11911 | /*check_dependency_p=*/false, |
a668c6ad MM |
11912 | /*class_head_p=*/true, |
11913 | /*is_declaration=*/false); | |
a723baf1 MM |
11914 | /* If that didn't work, ignore the nested-name-specifier. */ |
11915 | if (!cp_parser_parse_definitely (parser)) | |
11916 | { | |
11917 | invalid_nested_name_p = true; | |
11918 | id = cp_parser_identifier (parser); | |
11919 | if (id == error_mark_node) | |
11920 | id = NULL_TREE; | |
11921 | } | |
11922 | /* If we could not find a corresponding TYPE, treat this | |
11923 | declaration like an unqualified declaration. */ | |
11924 | if (type == error_mark_node) | |
11925 | nested_name_specifier = NULL_TREE; | |
11926 | /* Otherwise, count the number of templates used in TYPE and its | |
11927 | containing scopes. */ | |
11928 | else | |
11929 | { | |
11930 | tree scope; | |
11931 | ||
11932 | for (scope = TREE_TYPE (type); | |
11933 | scope && TREE_CODE (scope) != NAMESPACE_DECL; | |
11934 | scope = (TYPE_P (scope) | |
11935 | ? TYPE_CONTEXT (scope) | |
11936 | : DECL_CONTEXT (scope))) | |
11937 | if (TYPE_P (scope) | |
11938 | && CLASS_TYPE_P (scope) | |
11939 | && CLASSTYPE_TEMPLATE_INFO (scope) | |
2050a1bb MM |
11940 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)) |
11941 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)) | |
a723baf1 MM |
11942 | ++num_templates; |
11943 | } | |
11944 | } | |
11945 | /* Otherwise, the identifier is optional. */ | |
11946 | else | |
11947 | { | |
11948 | /* We don't know whether what comes next is a template-id, | |
11949 | an identifier, or nothing at all. */ | |
11950 | cp_parser_parse_tentatively (parser); | |
11951 | /* Check for a template-id. */ | |
11952 | id = cp_parser_template_id (parser, | |
11953 | /*template_keyword_p=*/false, | |
a668c6ad MM |
11954 | /*check_dependency_p=*/true, |
11955 | /*is_declaration=*/true); | |
a723baf1 MM |
11956 | /* If that didn't work, it could still be an identifier. */ |
11957 | if (!cp_parser_parse_definitely (parser)) | |
11958 | { | |
11959 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
11960 | id = cp_parser_identifier (parser); | |
11961 | else | |
11962 | id = NULL_TREE; | |
11963 | } | |
11964 | else | |
11965 | { | |
11966 | template_id_p = true; | |
11967 | ++num_templates; | |
11968 | } | |
11969 | } | |
11970 | ||
8d241e0b KL |
11971 | pop_deferring_access_checks (); |
11972 | ||
ee43dab5 MM |
11973 | cp_parser_check_for_invalid_template_id (parser, id); |
11974 | ||
a723baf1 MM |
11975 | /* If it's not a `:' or a `{' then we can't really be looking at a |
11976 | class-head, since a class-head only appears as part of a | |
11977 | class-specifier. We have to detect this situation before calling | |
11978 | xref_tag, since that has irreversible side-effects. */ | |
11979 | if (!cp_parser_next_token_starts_class_definition_p (parser)) | |
11980 | { | |
11981 | cp_parser_error (parser, "expected `{' or `:'"); | |
11982 | return error_mark_node; | |
11983 | } | |
11984 | ||
11985 | /* At this point, we're going ahead with the class-specifier, even | |
11986 | if some other problem occurs. */ | |
11987 | cp_parser_commit_to_tentative_parse (parser); | |
11988 | /* Issue the error about the overly-qualified name now. */ | |
11989 | if (qualified_p) | |
11990 | cp_parser_error (parser, | |
11991 | "global qualification of class name is invalid"); | |
11992 | else if (invalid_nested_name_p) | |
11993 | cp_parser_error (parser, | |
11994 | "qualified name does not name a class"); | |
88081599 MM |
11995 | else if (nested_name_specifier) |
11996 | { | |
11997 | tree scope; | |
11998 | /* Figure out in what scope the declaration is being placed. */ | |
11999 | scope = current_scope (); | |
12000 | if (!scope) | |
12001 | scope = current_namespace; | |
12002 | /* If that scope does not contain the scope in which the | |
12003 | class was originally declared, the program is invalid. */ | |
12004 | if (scope && !is_ancestor (scope, nested_name_specifier)) | |
12005 | { | |
12006 | error ("declaration of `%D' in `%D' which does not " | |
12007 | "enclose `%D'", type, scope, nested_name_specifier); | |
12008 | type = NULL_TREE; | |
12009 | goto done; | |
12010 | } | |
12011 | /* [dcl.meaning] | |
12012 | ||
12013 | A declarator-id shall not be qualified exception of the | |
12014 | definition of a ... nested class outside of its class | |
12015 | ... [or] a the definition or explicit instantiation of a | |
12016 | class member of a namespace outside of its namespace. */ | |
12017 | if (scope == nested_name_specifier) | |
12018 | { | |
12019 | pedwarn ("extra qualification ignored"); | |
12020 | nested_name_specifier = NULL_TREE; | |
12021 | num_templates = 0; | |
12022 | } | |
12023 | } | |
afb0918a MM |
12024 | /* An explicit-specialization must be preceded by "template <>". If |
12025 | it is not, try to recover gracefully. */ | |
12026 | if (at_namespace_scope_p () | |
12027 | && parser->num_template_parameter_lists == 0 | |
eeb23c11 | 12028 | && template_id_p) |
afb0918a MM |
12029 | { |
12030 | error ("an explicit specialization must be preceded by 'template <>'"); | |
12031 | invalid_explicit_specialization_p = true; | |
12032 | /* Take the same action that would have been taken by | |
12033 | cp_parser_explicit_specialization. */ | |
12034 | ++parser->num_template_parameter_lists; | |
12035 | begin_specialization (); | |
12036 | } | |
12037 | /* There must be no "return" statements between this point and the | |
12038 | end of this function; set "type "to the correct return value and | |
12039 | use "goto done;" to return. */ | |
a723baf1 MM |
12040 | /* Make sure that the right number of template parameters were |
12041 | present. */ | |
12042 | if (!cp_parser_check_template_parameters (parser, num_templates)) | |
afb0918a MM |
12043 | { |
12044 | /* If something went wrong, there is no point in even trying to | |
12045 | process the class-definition. */ | |
12046 | type = NULL_TREE; | |
12047 | goto done; | |
12048 | } | |
a723baf1 | 12049 | |
a723baf1 MM |
12050 | /* Look up the type. */ |
12051 | if (template_id_p) | |
12052 | { | |
12053 | type = TREE_TYPE (id); | |
12054 | maybe_process_partial_specialization (type); | |
12055 | } | |
12056 | else if (!nested_name_specifier) | |
12057 | { | |
12058 | /* If the class was unnamed, create a dummy name. */ | |
12059 | if (!id) | |
12060 | id = make_anon_name (); | |
cbd63935 KL |
12061 | type = xref_tag (class_key, id, attributes, /*globalize=*/false, |
12062 | parser->num_template_parameter_lists); | |
a723baf1 MM |
12063 | } |
12064 | else | |
12065 | { | |
a723baf1 MM |
12066 | tree class_type; |
12067 | ||
12068 | /* Given: | |
12069 | ||
12070 | template <typename T> struct S { struct T }; | |
14d22dd6 | 12071 | template <typename T> struct S<T>::T { }; |
a723baf1 MM |
12072 | |
12073 | we will get a TYPENAME_TYPE when processing the definition of | |
12074 | `S::T'. We need to resolve it to the actual type before we | |
12075 | try to define it. */ | |
12076 | if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE) | |
12077 | { | |
14d22dd6 MM |
12078 | class_type = resolve_typename_type (TREE_TYPE (type), |
12079 | /*only_current_p=*/false); | |
12080 | if (class_type != error_mark_node) | |
12081 | type = TYPE_NAME (class_type); | |
12082 | else | |
12083 | { | |
12084 | cp_parser_error (parser, "could not resolve typename type"); | |
12085 | type = error_mark_node; | |
12086 | } | |
a723baf1 MM |
12087 | } |
12088 | ||
560ad596 MM |
12089 | maybe_process_partial_specialization (TREE_TYPE (type)); |
12090 | class_type = current_class_type; | |
12091 | /* Enter the scope indicated by the nested-name-specifier. */ | |
12092 | if (nested_name_specifier) | |
12093 | push_scope (nested_name_specifier); | |
12094 | /* Get the canonical version of this type. */ | |
12095 | type = TYPE_MAIN_DECL (TREE_TYPE (type)); | |
12096 | if (PROCESSING_REAL_TEMPLATE_DECL_P () | |
12097 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type))) | |
12098 | type = push_template_decl (type); | |
12099 | type = TREE_TYPE (type); | |
12100 | if (nested_name_specifier) | |
eeb23c11 MM |
12101 | { |
12102 | *nested_name_specifier_p = true; | |
12103 | pop_scope (nested_name_specifier); | |
12104 | } | |
a723baf1 MM |
12105 | } |
12106 | /* Indicate whether this class was declared as a `class' or as a | |
12107 | `struct'. */ | |
12108 | if (TREE_CODE (type) == RECORD_TYPE) | |
12109 | CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type); | |
12110 | cp_parser_check_class_key (class_key, type); | |
12111 | ||
12112 | /* Enter the scope containing the class; the names of base classes | |
12113 | should be looked up in that context. For example, given: | |
12114 | ||
12115 | struct A { struct B {}; struct C; }; | |
12116 | struct A::C : B {}; | |
12117 | ||
12118 | is valid. */ | |
12119 | if (nested_name_specifier) | |
12120 | push_scope (nested_name_specifier); | |
12121 | /* Now, look for the base-clause. */ | |
12122 | token = cp_lexer_peek_token (parser->lexer); | |
12123 | if (token->type == CPP_COLON) | |
12124 | { | |
12125 | tree bases; | |
12126 | ||
12127 | /* Get the list of base-classes. */ | |
12128 | bases = cp_parser_base_clause (parser); | |
12129 | /* Process them. */ | |
12130 | xref_basetypes (type, bases); | |
12131 | } | |
12132 | /* Leave the scope given by the nested-name-specifier. We will | |
12133 | enter the class scope itself while processing the members. */ | |
12134 | if (nested_name_specifier) | |
12135 | pop_scope (nested_name_specifier); | |
12136 | ||
afb0918a MM |
12137 | done: |
12138 | if (invalid_explicit_specialization_p) | |
12139 | { | |
12140 | end_specialization (); | |
12141 | --parser->num_template_parameter_lists; | |
12142 | } | |
a723baf1 MM |
12143 | return type; |
12144 | } | |
12145 | ||
12146 | /* Parse a class-key. | |
12147 | ||
12148 | class-key: | |
12149 | class | |
12150 | struct | |
12151 | union | |
12152 | ||
12153 | Returns the kind of class-key specified, or none_type to indicate | |
12154 | error. */ | |
12155 | ||
12156 | static enum tag_types | |
94edc4ab | 12157 | cp_parser_class_key (cp_parser* parser) |
a723baf1 MM |
12158 | { |
12159 | cp_token *token; | |
12160 | enum tag_types tag_type; | |
12161 | ||
12162 | /* Look for the class-key. */ | |
12163 | token = cp_parser_require (parser, CPP_KEYWORD, "class-key"); | |
12164 | if (!token) | |
12165 | return none_type; | |
12166 | ||
12167 | /* Check to see if the TOKEN is a class-key. */ | |
12168 | tag_type = cp_parser_token_is_class_key (token); | |
12169 | if (!tag_type) | |
12170 | cp_parser_error (parser, "expected class-key"); | |
12171 | return tag_type; | |
12172 | } | |
12173 | ||
12174 | /* Parse an (optional) member-specification. | |
12175 | ||
12176 | member-specification: | |
12177 | member-declaration member-specification [opt] | |
12178 | access-specifier : member-specification [opt] */ | |
12179 | ||
12180 | static void | |
94edc4ab | 12181 | cp_parser_member_specification_opt (cp_parser* parser) |
a723baf1 MM |
12182 | { |
12183 | while (true) | |
12184 | { | |
12185 | cp_token *token; | |
12186 | enum rid keyword; | |
12187 | ||
12188 | /* Peek at the next token. */ | |
12189 | token = cp_lexer_peek_token (parser->lexer); | |
12190 | /* If it's a `}', or EOF then we've seen all the members. */ | |
12191 | if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF) | |
12192 | break; | |
12193 | ||
12194 | /* See if this token is a keyword. */ | |
12195 | keyword = token->keyword; | |
12196 | switch (keyword) | |
12197 | { | |
12198 | case RID_PUBLIC: | |
12199 | case RID_PROTECTED: | |
12200 | case RID_PRIVATE: | |
12201 | /* Consume the access-specifier. */ | |
12202 | cp_lexer_consume_token (parser->lexer); | |
12203 | /* Remember which access-specifier is active. */ | |
12204 | current_access_specifier = token->value; | |
12205 | /* Look for the `:'. */ | |
12206 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
12207 | break; | |
12208 | ||
12209 | default: | |
12210 | /* Otherwise, the next construction must be a | |
12211 | member-declaration. */ | |
12212 | cp_parser_member_declaration (parser); | |
a723baf1 MM |
12213 | } |
12214 | } | |
12215 | } | |
12216 | ||
12217 | /* Parse a member-declaration. | |
12218 | ||
12219 | member-declaration: | |
12220 | decl-specifier-seq [opt] member-declarator-list [opt] ; | |
12221 | function-definition ; [opt] | |
12222 | :: [opt] nested-name-specifier template [opt] unqualified-id ; | |
12223 | using-declaration | |
12224 | template-declaration | |
12225 | ||
12226 | member-declarator-list: | |
12227 | member-declarator | |
12228 | member-declarator-list , member-declarator | |
12229 | ||
12230 | member-declarator: | |
12231 | declarator pure-specifier [opt] | |
12232 | declarator constant-initializer [opt] | |
12233 | identifier [opt] : constant-expression | |
12234 | ||
12235 | GNU Extensions: | |
12236 | ||
12237 | member-declaration: | |
12238 | __extension__ member-declaration | |
12239 | ||
12240 | member-declarator: | |
12241 | declarator attributes [opt] pure-specifier [opt] | |
12242 | declarator attributes [opt] constant-initializer [opt] | |
12243 | identifier [opt] attributes [opt] : constant-expression */ | |
12244 | ||
12245 | static void | |
94edc4ab | 12246 | cp_parser_member_declaration (cp_parser* parser) |
a723baf1 MM |
12247 | { |
12248 | tree decl_specifiers; | |
12249 | tree prefix_attributes; | |
12250 | tree decl; | |
560ad596 | 12251 | int declares_class_or_enum; |
a723baf1 MM |
12252 | bool friend_p; |
12253 | cp_token *token; | |
12254 | int saved_pedantic; | |
12255 | ||
12256 | /* Check for the `__extension__' keyword. */ | |
12257 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
12258 | { | |
12259 | /* Recurse. */ | |
12260 | cp_parser_member_declaration (parser); | |
12261 | /* Restore the old value of the PEDANTIC flag. */ | |
12262 | pedantic = saved_pedantic; | |
12263 | ||
12264 | return; | |
12265 | } | |
12266 | ||
12267 | /* Check for a template-declaration. */ | |
12268 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
12269 | { | |
12270 | /* Parse the template-declaration. */ | |
12271 | cp_parser_template_declaration (parser, /*member_p=*/true); | |
12272 | ||
12273 | return; | |
12274 | } | |
12275 | ||
12276 | /* Check for a using-declaration. */ | |
12277 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING)) | |
12278 | { | |
12279 | /* Parse the using-declaration. */ | |
12280 | cp_parser_using_declaration (parser); | |
12281 | ||
12282 | return; | |
12283 | } | |
12284 | ||
a723baf1 MM |
12285 | /* Parse the decl-specifier-seq. */ |
12286 | decl_specifiers | |
12287 | = cp_parser_decl_specifier_seq (parser, | |
12288 | CP_PARSER_FLAGS_OPTIONAL, | |
12289 | &prefix_attributes, | |
12290 | &declares_class_or_enum); | |
8fbc5ae7 MM |
12291 | /* Check for an invalid type-name. */ |
12292 | if (cp_parser_diagnose_invalid_type_name (parser)) | |
12293 | return; | |
a723baf1 MM |
12294 | /* If there is no declarator, then the decl-specifier-seq should |
12295 | specify a type. */ | |
12296 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
12297 | { | |
12298 | /* If there was no decl-specifier-seq, and the next token is a | |
12299 | `;', then we have something like: | |
12300 | ||
12301 | struct S { ; }; | |
12302 | ||
12303 | [class.mem] | |
12304 | ||
12305 | Each member-declaration shall declare at least one member | |
12306 | name of the class. */ | |
12307 | if (!decl_specifiers) | |
12308 | { | |
12309 | if (pedantic) | |
12310 | pedwarn ("extra semicolon"); | |
12311 | } | |
12312 | else | |
12313 | { | |
12314 | tree type; | |
12315 | ||
12316 | /* See if this declaration is a friend. */ | |
12317 | friend_p = cp_parser_friend_p (decl_specifiers); | |
12318 | /* If there were decl-specifiers, check to see if there was | |
12319 | a class-declaration. */ | |
12320 | type = check_tag_decl (decl_specifiers); | |
12321 | /* Nested classes have already been added to the class, but | |
12322 | a `friend' needs to be explicitly registered. */ | |
12323 | if (friend_p) | |
12324 | { | |
12325 | /* If the `friend' keyword was present, the friend must | |
12326 | be introduced with a class-key. */ | |
12327 | if (!declares_class_or_enum) | |
12328 | error ("a class-key must be used when declaring a friend"); | |
12329 | /* In this case: | |
12330 | ||
12331 | template <typename T> struct A { | |
12332 | friend struct A<T>::B; | |
12333 | }; | |
12334 | ||
12335 | A<T>::B will be represented by a TYPENAME_TYPE, and | |
12336 | therefore not recognized by check_tag_decl. */ | |
12337 | if (!type) | |
12338 | { | |
12339 | tree specifier; | |
12340 | ||
12341 | for (specifier = decl_specifiers; | |
12342 | specifier; | |
12343 | specifier = TREE_CHAIN (specifier)) | |
12344 | { | |
12345 | tree s = TREE_VALUE (specifier); | |
12346 | ||
c003e212 GDR |
12347 | if (TREE_CODE (s) == IDENTIFIER_NODE) |
12348 | get_global_value_if_present (s, &type); | |
a723baf1 MM |
12349 | if (TREE_CODE (s) == TYPE_DECL) |
12350 | s = TREE_TYPE (s); | |
12351 | if (TYPE_P (s)) | |
12352 | { | |
12353 | type = s; | |
12354 | break; | |
12355 | } | |
12356 | } | |
12357 | } | |
fdd09134 | 12358 | if (!type || !TYPE_P (type)) |
a723baf1 MM |
12359 | error ("friend declaration does not name a class or " |
12360 | "function"); | |
12361 | else | |
19db77ce KL |
12362 | make_friend_class (current_class_type, type, |
12363 | /*complain=*/true); | |
a723baf1 MM |
12364 | } |
12365 | /* If there is no TYPE, an error message will already have | |
12366 | been issued. */ | |
12367 | else if (!type) | |
12368 | ; | |
12369 | /* An anonymous aggregate has to be handled specially; such | |
12370 | a declaration really declares a data member (with a | |
12371 | particular type), as opposed to a nested class. */ | |
12372 | else if (ANON_AGGR_TYPE_P (type)) | |
12373 | { | |
12374 | /* Remove constructors and such from TYPE, now that we | |
34cd5ae7 | 12375 | know it is an anonymous aggregate. */ |
a723baf1 MM |
12376 | fixup_anonymous_aggr (type); |
12377 | /* And make the corresponding data member. */ | |
12378 | decl = build_decl (FIELD_DECL, NULL_TREE, type); | |
12379 | /* Add it to the class. */ | |
12380 | finish_member_declaration (decl); | |
12381 | } | |
37d407a1 KL |
12382 | else |
12383 | cp_parser_check_access_in_redeclaration (TYPE_NAME (type)); | |
a723baf1 MM |
12384 | } |
12385 | } | |
12386 | else | |
12387 | { | |
12388 | /* See if these declarations will be friends. */ | |
12389 | friend_p = cp_parser_friend_p (decl_specifiers); | |
12390 | ||
12391 | /* Keep going until we hit the `;' at the end of the | |
12392 | declaration. */ | |
12393 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
12394 | { | |
12395 | tree attributes = NULL_TREE; | |
12396 | tree first_attribute; | |
12397 | ||
12398 | /* Peek at the next token. */ | |
12399 | token = cp_lexer_peek_token (parser->lexer); | |
12400 | ||
12401 | /* Check for a bitfield declaration. */ | |
12402 | if (token->type == CPP_COLON | |
12403 | || (token->type == CPP_NAME | |
12404 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
12405 | == CPP_COLON)) | |
12406 | { | |
12407 | tree identifier; | |
12408 | tree width; | |
12409 | ||
12410 | /* Get the name of the bitfield. Note that we cannot just | |
12411 | check TOKEN here because it may have been invalidated by | |
12412 | the call to cp_lexer_peek_nth_token above. */ | |
12413 | if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON) | |
12414 | identifier = cp_parser_identifier (parser); | |
12415 | else | |
12416 | identifier = NULL_TREE; | |
12417 | ||
12418 | /* Consume the `:' token. */ | |
12419 | cp_lexer_consume_token (parser->lexer); | |
12420 | /* Get the width of the bitfield. */ | |
14d22dd6 MM |
12421 | width |
12422 | = cp_parser_constant_expression (parser, | |
12423 | /*allow_non_constant=*/false, | |
12424 | NULL); | |
a723baf1 MM |
12425 | |
12426 | /* Look for attributes that apply to the bitfield. */ | |
12427 | attributes = cp_parser_attributes_opt (parser); | |
12428 | /* Remember which attributes are prefix attributes and | |
12429 | which are not. */ | |
12430 | first_attribute = attributes; | |
12431 | /* Combine the attributes. */ | |
12432 | attributes = chainon (prefix_attributes, attributes); | |
12433 | ||
12434 | /* Create the bitfield declaration. */ | |
12435 | decl = grokbitfield (identifier, | |
12436 | decl_specifiers, | |
12437 | width); | |
12438 | /* Apply the attributes. */ | |
12439 | cplus_decl_attributes (&decl, attributes, /*flags=*/0); | |
12440 | } | |
12441 | else | |
12442 | { | |
12443 | tree declarator; | |
12444 | tree initializer; | |
12445 | tree asm_specification; | |
7efa3e22 | 12446 | int ctor_dtor_or_conv_p; |
a723baf1 MM |
12447 | |
12448 | /* Parse the declarator. */ | |
12449 | declarator | |
62b8a44e | 12450 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
12451 | &ctor_dtor_or_conv_p, |
12452 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
12453 | |
12454 | /* If something went wrong parsing the declarator, make sure | |
12455 | that we at least consume some tokens. */ | |
12456 | if (declarator == error_mark_node) | |
12457 | { | |
12458 | /* Skip to the end of the statement. */ | |
12459 | cp_parser_skip_to_end_of_statement (parser); | |
4bb8ca28 MM |
12460 | /* If the next token is not a semicolon, that is |
12461 | probably because we just skipped over the body of | |
12462 | a function. So, we consume a semicolon if | |
12463 | present, but do not issue an error message if it | |
12464 | is not present. */ | |
12465 | if (cp_lexer_next_token_is (parser->lexer, | |
12466 | CPP_SEMICOLON)) | |
12467 | cp_lexer_consume_token (parser->lexer); | |
12468 | return; | |
a723baf1 MM |
12469 | } |
12470 | ||
560ad596 MM |
12471 | cp_parser_check_for_definition_in_return_type |
12472 | (declarator, declares_class_or_enum); | |
12473 | ||
a723baf1 MM |
12474 | /* Look for an asm-specification. */ |
12475 | asm_specification = cp_parser_asm_specification_opt (parser); | |
12476 | /* Look for attributes that apply to the declaration. */ | |
12477 | attributes = cp_parser_attributes_opt (parser); | |
12478 | /* Remember which attributes are prefix attributes and | |
12479 | which are not. */ | |
12480 | first_attribute = attributes; | |
12481 | /* Combine the attributes. */ | |
12482 | attributes = chainon (prefix_attributes, attributes); | |
12483 | ||
12484 | /* If it's an `=', then we have a constant-initializer or a | |
12485 | pure-specifier. It is not correct to parse the | |
12486 | initializer before registering the member declaration | |
12487 | since the member declaration should be in scope while | |
12488 | its initializer is processed. However, the rest of the | |
12489 | front end does not yet provide an interface that allows | |
12490 | us to handle this correctly. */ | |
12491 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
12492 | { | |
12493 | /* In [class.mem]: | |
12494 | ||
12495 | A pure-specifier shall be used only in the declaration of | |
12496 | a virtual function. | |
12497 | ||
12498 | A member-declarator can contain a constant-initializer | |
12499 | only if it declares a static member of integral or | |
12500 | enumeration type. | |
12501 | ||
12502 | Therefore, if the DECLARATOR is for a function, we look | |
12503 | for a pure-specifier; otherwise, we look for a | |
12504 | constant-initializer. When we call `grokfield', it will | |
12505 | perform more stringent semantics checks. */ | |
12506 | if (TREE_CODE (declarator) == CALL_EXPR) | |
12507 | initializer = cp_parser_pure_specifier (parser); | |
12508 | else | |
4bb8ca28 MM |
12509 | /* Parse the initializer. */ |
12510 | initializer = cp_parser_constant_initializer (parser); | |
a723baf1 MM |
12511 | } |
12512 | /* Otherwise, there is no initializer. */ | |
12513 | else | |
12514 | initializer = NULL_TREE; | |
12515 | ||
12516 | /* See if we are probably looking at a function | |
12517 | definition. We are certainly not looking at at a | |
12518 | member-declarator. Calling `grokfield' has | |
12519 | side-effects, so we must not do it unless we are sure | |
12520 | that we are looking at a member-declarator. */ | |
12521 | if (cp_parser_token_starts_function_definition_p | |
12522 | (cp_lexer_peek_token (parser->lexer))) | |
4bb8ca28 MM |
12523 | { |
12524 | /* The grammar does not allow a pure-specifier to be | |
12525 | used when a member function is defined. (It is | |
12526 | possible that this fact is an oversight in the | |
12527 | standard, since a pure function may be defined | |
12528 | outside of the class-specifier. */ | |
12529 | if (initializer) | |
12530 | error ("pure-specifier on function-definition"); | |
12531 | decl = cp_parser_save_member_function_body (parser, | |
12532 | decl_specifiers, | |
12533 | declarator, | |
12534 | attributes); | |
12535 | /* If the member was not a friend, declare it here. */ | |
12536 | if (!friend_p) | |
12537 | finish_member_declaration (decl); | |
12538 | /* Peek at the next token. */ | |
12539 | token = cp_lexer_peek_token (parser->lexer); | |
12540 | /* If the next token is a semicolon, consume it. */ | |
12541 | if (token->type == CPP_SEMICOLON) | |
12542 | cp_lexer_consume_token (parser->lexer); | |
12543 | return; | |
12544 | } | |
a723baf1 | 12545 | else |
39703eb9 MM |
12546 | { |
12547 | /* Create the declaration. */ | |
ee3071ef NS |
12548 | decl = grokfield (declarator, decl_specifiers, |
12549 | initializer, asm_specification, | |
39703eb9 MM |
12550 | attributes); |
12551 | /* Any initialization must have been from a | |
12552 | constant-expression. */ | |
12553 | if (decl && TREE_CODE (decl) == VAR_DECL && initializer) | |
12554 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; | |
12555 | } | |
a723baf1 MM |
12556 | } |
12557 | ||
12558 | /* Reset PREFIX_ATTRIBUTES. */ | |
12559 | while (attributes && TREE_CHAIN (attributes) != first_attribute) | |
12560 | attributes = TREE_CHAIN (attributes); | |
12561 | if (attributes) | |
12562 | TREE_CHAIN (attributes) = NULL_TREE; | |
12563 | ||
12564 | /* If there is any qualification still in effect, clear it | |
12565 | now; we will be starting fresh with the next declarator. */ | |
12566 | parser->scope = NULL_TREE; | |
12567 | parser->qualifying_scope = NULL_TREE; | |
12568 | parser->object_scope = NULL_TREE; | |
12569 | /* If it's a `,', then there are more declarators. */ | |
12570 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
12571 | cp_lexer_consume_token (parser->lexer); | |
12572 | /* If the next token isn't a `;', then we have a parse error. */ | |
12573 | else if (cp_lexer_next_token_is_not (parser->lexer, | |
12574 | CPP_SEMICOLON)) | |
12575 | { | |
12576 | cp_parser_error (parser, "expected `;'"); | |
04c06002 | 12577 | /* Skip tokens until we find a `;'. */ |
a723baf1 MM |
12578 | cp_parser_skip_to_end_of_statement (parser); |
12579 | ||
12580 | break; | |
12581 | } | |
12582 | ||
12583 | if (decl) | |
12584 | { | |
12585 | /* Add DECL to the list of members. */ | |
12586 | if (!friend_p) | |
12587 | finish_member_declaration (decl); | |
12588 | ||
a723baf1 | 12589 | if (TREE_CODE (decl) == FUNCTION_DECL) |
8db1028e | 12590 | cp_parser_save_default_args (parser, decl); |
a723baf1 MM |
12591 | } |
12592 | } | |
12593 | } | |
12594 | ||
4bb8ca28 | 12595 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
a723baf1 MM |
12596 | } |
12597 | ||
12598 | /* Parse a pure-specifier. | |
12599 | ||
12600 | pure-specifier: | |
12601 | = 0 | |
12602 | ||
12603 | Returns INTEGER_ZERO_NODE if a pure specifier is found. | |
cd0be382 | 12604 | Otherwise, ERROR_MARK_NODE is returned. */ |
a723baf1 MM |
12605 | |
12606 | static tree | |
94edc4ab | 12607 | cp_parser_pure_specifier (cp_parser* parser) |
a723baf1 MM |
12608 | { |
12609 | cp_token *token; | |
12610 | ||
12611 | /* Look for the `=' token. */ | |
12612 | if (!cp_parser_require (parser, CPP_EQ, "`='")) | |
12613 | return error_mark_node; | |
12614 | /* Look for the `0' token. */ | |
12615 | token = cp_parser_require (parser, CPP_NUMBER, "`0'"); | |
12616 | /* Unfortunately, this will accept `0L' and `0x00' as well. We need | |
12617 | to get information from the lexer about how the number was | |
12618 | spelled in order to fix this problem. */ | |
12619 | if (!token || !integer_zerop (token->value)) | |
12620 | return error_mark_node; | |
12621 | ||
12622 | return integer_zero_node; | |
12623 | } | |
12624 | ||
12625 | /* Parse a constant-initializer. | |
12626 | ||
12627 | constant-initializer: | |
12628 | = constant-expression | |
12629 | ||
12630 | Returns a representation of the constant-expression. */ | |
12631 | ||
12632 | static tree | |
94edc4ab | 12633 | cp_parser_constant_initializer (cp_parser* parser) |
a723baf1 MM |
12634 | { |
12635 | /* Look for the `=' token. */ | |
12636 | if (!cp_parser_require (parser, CPP_EQ, "`='")) | |
12637 | return error_mark_node; | |
12638 | ||
12639 | /* It is invalid to write: | |
12640 | ||
12641 | struct S { static const int i = { 7 }; }; | |
12642 | ||
12643 | */ | |
12644 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
12645 | { | |
12646 | cp_parser_error (parser, | |
12647 | "a brace-enclosed initializer is not allowed here"); | |
12648 | /* Consume the opening brace. */ | |
12649 | cp_lexer_consume_token (parser->lexer); | |
12650 | /* Skip the initializer. */ | |
12651 | cp_parser_skip_to_closing_brace (parser); | |
12652 | /* Look for the trailing `}'. */ | |
12653 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
12654 | ||
12655 | return error_mark_node; | |
12656 | } | |
12657 | ||
14d22dd6 MM |
12658 | return cp_parser_constant_expression (parser, |
12659 | /*allow_non_constant=*/false, | |
12660 | NULL); | |
a723baf1 MM |
12661 | } |
12662 | ||
12663 | /* Derived classes [gram.class.derived] */ | |
12664 | ||
12665 | /* Parse a base-clause. | |
12666 | ||
12667 | base-clause: | |
12668 | : base-specifier-list | |
12669 | ||
12670 | base-specifier-list: | |
12671 | base-specifier | |
12672 | base-specifier-list , base-specifier | |
12673 | ||
12674 | Returns a TREE_LIST representing the base-classes, in the order in | |
12675 | which they were declared. The representation of each node is as | |
12676 | described by cp_parser_base_specifier. | |
12677 | ||
12678 | In the case that no bases are specified, this function will return | |
12679 | NULL_TREE, not ERROR_MARK_NODE. */ | |
12680 | ||
12681 | static tree | |
94edc4ab | 12682 | cp_parser_base_clause (cp_parser* parser) |
a723baf1 MM |
12683 | { |
12684 | tree bases = NULL_TREE; | |
12685 | ||
12686 | /* Look for the `:' that begins the list. */ | |
12687 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
12688 | ||
12689 | /* Scan the base-specifier-list. */ | |
12690 | while (true) | |
12691 | { | |
12692 | cp_token *token; | |
12693 | tree base; | |
12694 | ||
12695 | /* Look for the base-specifier. */ | |
12696 | base = cp_parser_base_specifier (parser); | |
12697 | /* Add BASE to the front of the list. */ | |
12698 | if (base != error_mark_node) | |
12699 | { | |
12700 | TREE_CHAIN (base) = bases; | |
12701 | bases = base; | |
12702 | } | |
12703 | /* Peek at the next token. */ | |
12704 | token = cp_lexer_peek_token (parser->lexer); | |
12705 | /* If it's not a comma, then the list is complete. */ | |
12706 | if (token->type != CPP_COMMA) | |
12707 | break; | |
12708 | /* Consume the `,'. */ | |
12709 | cp_lexer_consume_token (parser->lexer); | |
12710 | } | |
12711 | ||
12712 | /* PARSER->SCOPE may still be non-NULL at this point, if the last | |
12713 | base class had a qualified name. However, the next name that | |
12714 | appears is certainly not qualified. */ | |
12715 | parser->scope = NULL_TREE; | |
12716 | parser->qualifying_scope = NULL_TREE; | |
12717 | parser->object_scope = NULL_TREE; | |
12718 | ||
12719 | return nreverse (bases); | |
12720 | } | |
12721 | ||
12722 | /* Parse a base-specifier. | |
12723 | ||
12724 | base-specifier: | |
12725 | :: [opt] nested-name-specifier [opt] class-name | |
12726 | virtual access-specifier [opt] :: [opt] nested-name-specifier | |
12727 | [opt] class-name | |
12728 | access-specifier virtual [opt] :: [opt] nested-name-specifier | |
12729 | [opt] class-name | |
12730 | ||
12731 | Returns a TREE_LIST. The TREE_PURPOSE will be one of | |
12732 | ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to | |
12733 | indicate the specifiers provided. The TREE_VALUE will be a TYPE | |
12734 | (or the ERROR_MARK_NODE) indicating the type that was specified. */ | |
12735 | ||
12736 | static tree | |
94edc4ab | 12737 | cp_parser_base_specifier (cp_parser* parser) |
a723baf1 MM |
12738 | { |
12739 | cp_token *token; | |
12740 | bool done = false; | |
12741 | bool virtual_p = false; | |
12742 | bool duplicate_virtual_error_issued_p = false; | |
12743 | bool duplicate_access_error_issued_p = false; | |
bbaab916 | 12744 | bool class_scope_p, template_p; |
dbbf88d1 | 12745 | tree access = access_default_node; |
a723baf1 MM |
12746 | tree type; |
12747 | ||
12748 | /* Process the optional `virtual' and `access-specifier'. */ | |
12749 | while (!done) | |
12750 | { | |
12751 | /* Peek at the next token. */ | |
12752 | token = cp_lexer_peek_token (parser->lexer); | |
12753 | /* Process `virtual'. */ | |
12754 | switch (token->keyword) | |
12755 | { | |
12756 | case RID_VIRTUAL: | |
12757 | /* If `virtual' appears more than once, issue an error. */ | |
12758 | if (virtual_p && !duplicate_virtual_error_issued_p) | |
12759 | { | |
12760 | cp_parser_error (parser, | |
12761 | "`virtual' specified more than once in base-specified"); | |
12762 | duplicate_virtual_error_issued_p = true; | |
12763 | } | |
12764 | ||
12765 | virtual_p = true; | |
12766 | ||
12767 | /* Consume the `virtual' token. */ | |
12768 | cp_lexer_consume_token (parser->lexer); | |
12769 | ||
12770 | break; | |
12771 | ||
12772 | case RID_PUBLIC: | |
12773 | case RID_PROTECTED: | |
12774 | case RID_PRIVATE: | |
12775 | /* If more than one access specifier appears, issue an | |
12776 | error. */ | |
dbbf88d1 NS |
12777 | if (access != access_default_node |
12778 | && !duplicate_access_error_issued_p) | |
a723baf1 MM |
12779 | { |
12780 | cp_parser_error (parser, | |
12781 | "more than one access specifier in base-specified"); | |
12782 | duplicate_access_error_issued_p = true; | |
12783 | } | |
12784 | ||
dbbf88d1 | 12785 | access = ridpointers[(int) token->keyword]; |
a723baf1 MM |
12786 | |
12787 | /* Consume the access-specifier. */ | |
12788 | cp_lexer_consume_token (parser->lexer); | |
12789 | ||
12790 | break; | |
12791 | ||
12792 | default: | |
12793 | done = true; | |
12794 | break; | |
12795 | } | |
12796 | } | |
852dcbdd | 12797 | /* It is not uncommon to see programs mechanically, erroneously, use |
a3a503a5 | 12798 | the 'typename' keyword to denote (dependent) qualified types |
1ed53ef3 GB |
12799 | as base classes. */ |
12800 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME)) | |
12801 | { | |
12802 | if (!processing_template_decl) | |
12803 | error ("keyword `typename' not allowed outside of templates"); | |
12804 | else | |
12805 | error ("keyword `typename' not allowed in this context " | |
12806 | "(the base class is implicitly a type)"); | |
12807 | cp_lexer_consume_token (parser->lexer); | |
12808 | } | |
a723baf1 | 12809 | |
a723baf1 MM |
12810 | /* Look for the optional `::' operator. */ |
12811 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); | |
12812 | /* Look for the nested-name-specifier. The simplest way to | |
12813 | implement: | |
12814 | ||
12815 | [temp.res] | |
12816 | ||
12817 | The keyword `typename' is not permitted in a base-specifier or | |
12818 | mem-initializer; in these contexts a qualified name that | |
12819 | depends on a template-parameter is implicitly assumed to be a | |
12820 | type name. | |
12821 | ||
12822 | is to pretend that we have seen the `typename' keyword at this | |
12823 | point. */ | |
12824 | cp_parser_nested_name_specifier_opt (parser, | |
12825 | /*typename_keyword_p=*/true, | |
12826 | /*check_dependency_p=*/true, | |
a668c6ad MM |
12827 | /*type_p=*/true, |
12828 | /*is_declaration=*/true); | |
a723baf1 MM |
12829 | /* If the base class is given by a qualified name, assume that names |
12830 | we see are type names or templates, as appropriate. */ | |
12831 | class_scope_p = (parser->scope && TYPE_P (parser->scope)); | |
bbaab916 NS |
12832 | template_p = class_scope_p && cp_parser_optional_template_keyword (parser); |
12833 | ||
a723baf1 MM |
12834 | /* Finally, look for the class-name. */ |
12835 | type = cp_parser_class_name (parser, | |
12836 | class_scope_p, | |
bbaab916 | 12837 | template_p, |
a723baf1 | 12838 | /*type_p=*/true, |
a723baf1 | 12839 | /*check_dependency_p=*/true, |
a668c6ad MM |
12840 | /*class_head_p=*/false, |
12841 | /*is_declaration=*/true); | |
a723baf1 MM |
12842 | |
12843 | if (type == error_mark_node) | |
12844 | return error_mark_node; | |
12845 | ||
dbbf88d1 | 12846 | return finish_base_specifier (TREE_TYPE (type), access, virtual_p); |
a723baf1 MM |
12847 | } |
12848 | ||
12849 | /* Exception handling [gram.exception] */ | |
12850 | ||
12851 | /* Parse an (optional) exception-specification. | |
12852 | ||
12853 | exception-specification: | |
12854 | throw ( type-id-list [opt] ) | |
12855 | ||
12856 | Returns a TREE_LIST representing the exception-specification. The | |
12857 | TREE_VALUE of each node is a type. */ | |
12858 | ||
12859 | static tree | |
94edc4ab | 12860 | cp_parser_exception_specification_opt (cp_parser* parser) |
a723baf1 MM |
12861 | { |
12862 | cp_token *token; | |
12863 | tree type_id_list; | |
12864 | ||
12865 | /* Peek at the next token. */ | |
12866 | token = cp_lexer_peek_token (parser->lexer); | |
12867 | /* If it's not `throw', then there's no exception-specification. */ | |
12868 | if (!cp_parser_is_keyword (token, RID_THROW)) | |
12869 | return NULL_TREE; | |
12870 | ||
12871 | /* Consume the `throw'. */ | |
12872 | cp_lexer_consume_token (parser->lexer); | |
12873 | ||
12874 | /* Look for the `('. */ | |
12875 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
12876 | ||
12877 | /* Peek at the next token. */ | |
12878 | token = cp_lexer_peek_token (parser->lexer); | |
12879 | /* If it's not a `)', then there is a type-id-list. */ | |
12880 | if (token->type != CPP_CLOSE_PAREN) | |
12881 | { | |
12882 | const char *saved_message; | |
12883 | ||
12884 | /* Types may not be defined in an exception-specification. */ | |
12885 | saved_message = parser->type_definition_forbidden_message; | |
12886 | parser->type_definition_forbidden_message | |
12887 | = "types may not be defined in an exception-specification"; | |
12888 | /* Parse the type-id-list. */ | |
12889 | type_id_list = cp_parser_type_id_list (parser); | |
12890 | /* Restore the saved message. */ | |
12891 | parser->type_definition_forbidden_message = saved_message; | |
12892 | } | |
12893 | else | |
12894 | type_id_list = empty_except_spec; | |
12895 | ||
12896 | /* Look for the `)'. */ | |
12897 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
12898 | ||
12899 | return type_id_list; | |
12900 | } | |
12901 | ||
12902 | /* Parse an (optional) type-id-list. | |
12903 | ||
12904 | type-id-list: | |
12905 | type-id | |
12906 | type-id-list , type-id | |
12907 | ||
12908 | Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE, | |
12909 | in the order that the types were presented. */ | |
12910 | ||
12911 | static tree | |
94edc4ab | 12912 | cp_parser_type_id_list (cp_parser* parser) |
a723baf1 MM |
12913 | { |
12914 | tree types = NULL_TREE; | |
12915 | ||
12916 | while (true) | |
12917 | { | |
12918 | cp_token *token; | |
12919 | tree type; | |
12920 | ||
12921 | /* Get the next type-id. */ | |
12922 | type = cp_parser_type_id (parser); | |
12923 | /* Add it to the list. */ | |
12924 | types = add_exception_specifier (types, type, /*complain=*/1); | |
12925 | /* Peek at the next token. */ | |
12926 | token = cp_lexer_peek_token (parser->lexer); | |
12927 | /* If it is not a `,', we are done. */ | |
12928 | if (token->type != CPP_COMMA) | |
12929 | break; | |
12930 | /* Consume the `,'. */ | |
12931 | cp_lexer_consume_token (parser->lexer); | |
12932 | } | |
12933 | ||
12934 | return nreverse (types); | |
12935 | } | |
12936 | ||
12937 | /* Parse a try-block. | |
12938 | ||
12939 | try-block: | |
12940 | try compound-statement handler-seq */ | |
12941 | ||
12942 | static tree | |
94edc4ab | 12943 | cp_parser_try_block (cp_parser* parser) |
a723baf1 MM |
12944 | { |
12945 | tree try_block; | |
12946 | ||
12947 | cp_parser_require_keyword (parser, RID_TRY, "`try'"); | |
12948 | try_block = begin_try_block (); | |
a5bcc582 | 12949 | cp_parser_compound_statement (parser, false); |
a723baf1 MM |
12950 | finish_try_block (try_block); |
12951 | cp_parser_handler_seq (parser); | |
12952 | finish_handler_sequence (try_block); | |
12953 | ||
12954 | return try_block; | |
12955 | } | |
12956 | ||
12957 | /* Parse a function-try-block. | |
12958 | ||
12959 | function-try-block: | |
12960 | try ctor-initializer [opt] function-body handler-seq */ | |
12961 | ||
12962 | static bool | |
94edc4ab | 12963 | cp_parser_function_try_block (cp_parser* parser) |
a723baf1 MM |
12964 | { |
12965 | tree try_block; | |
12966 | bool ctor_initializer_p; | |
12967 | ||
12968 | /* Look for the `try' keyword. */ | |
12969 | if (!cp_parser_require_keyword (parser, RID_TRY, "`try'")) | |
12970 | return false; | |
12971 | /* Let the rest of the front-end know where we are. */ | |
12972 | try_block = begin_function_try_block (); | |
12973 | /* Parse the function-body. */ | |
12974 | ctor_initializer_p | |
12975 | = cp_parser_ctor_initializer_opt_and_function_body (parser); | |
12976 | /* We're done with the `try' part. */ | |
12977 | finish_function_try_block (try_block); | |
12978 | /* Parse the handlers. */ | |
12979 | cp_parser_handler_seq (parser); | |
12980 | /* We're done with the handlers. */ | |
12981 | finish_function_handler_sequence (try_block); | |
12982 | ||
12983 | return ctor_initializer_p; | |
12984 | } | |
12985 | ||
12986 | /* Parse a handler-seq. | |
12987 | ||
12988 | handler-seq: | |
12989 | handler handler-seq [opt] */ | |
12990 | ||
12991 | static void | |
94edc4ab | 12992 | cp_parser_handler_seq (cp_parser* parser) |
a723baf1 MM |
12993 | { |
12994 | while (true) | |
12995 | { | |
12996 | cp_token *token; | |
12997 | ||
12998 | /* Parse the handler. */ | |
12999 | cp_parser_handler (parser); | |
13000 | /* Peek at the next token. */ | |
13001 | token = cp_lexer_peek_token (parser->lexer); | |
13002 | /* If it's not `catch' then there are no more handlers. */ | |
13003 | if (!cp_parser_is_keyword (token, RID_CATCH)) | |
13004 | break; | |
13005 | } | |
13006 | } | |
13007 | ||
13008 | /* Parse a handler. | |
13009 | ||
13010 | handler: | |
13011 | catch ( exception-declaration ) compound-statement */ | |
13012 | ||
13013 | static void | |
94edc4ab | 13014 | cp_parser_handler (cp_parser* parser) |
a723baf1 MM |
13015 | { |
13016 | tree handler; | |
13017 | tree declaration; | |
13018 | ||
13019 | cp_parser_require_keyword (parser, RID_CATCH, "`catch'"); | |
13020 | handler = begin_handler (); | |
13021 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13022 | declaration = cp_parser_exception_declaration (parser); | |
13023 | finish_handler_parms (declaration, handler); | |
13024 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
a5bcc582 | 13025 | cp_parser_compound_statement (parser, false); |
a723baf1 MM |
13026 | finish_handler (handler); |
13027 | } | |
13028 | ||
13029 | /* Parse an exception-declaration. | |
13030 | ||
13031 | exception-declaration: | |
13032 | type-specifier-seq declarator | |
13033 | type-specifier-seq abstract-declarator | |
13034 | type-specifier-seq | |
13035 | ... | |
13036 | ||
13037 | Returns a VAR_DECL for the declaration, or NULL_TREE if the | |
13038 | ellipsis variant is used. */ | |
13039 | ||
13040 | static tree | |
94edc4ab | 13041 | cp_parser_exception_declaration (cp_parser* parser) |
a723baf1 MM |
13042 | { |
13043 | tree type_specifiers; | |
13044 | tree declarator; | |
13045 | const char *saved_message; | |
13046 | ||
13047 | /* If it's an ellipsis, it's easy to handle. */ | |
13048 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
13049 | { | |
13050 | /* Consume the `...' token. */ | |
13051 | cp_lexer_consume_token (parser->lexer); | |
13052 | return NULL_TREE; | |
13053 | } | |
13054 | ||
13055 | /* Types may not be defined in exception-declarations. */ | |
13056 | saved_message = parser->type_definition_forbidden_message; | |
13057 | parser->type_definition_forbidden_message | |
13058 | = "types may not be defined in exception-declarations"; | |
13059 | ||
13060 | /* Parse the type-specifier-seq. */ | |
13061 | type_specifiers = cp_parser_type_specifier_seq (parser); | |
13062 | /* If it's a `)', then there is no declarator. */ | |
13063 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)) | |
13064 | declarator = NULL_TREE; | |
13065 | else | |
62b8a44e | 13066 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER, |
4bb8ca28 MM |
13067 | /*ctor_dtor_or_conv_p=*/NULL, |
13068 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
13069 | |
13070 | /* Restore the saved message. */ | |
13071 | parser->type_definition_forbidden_message = saved_message; | |
13072 | ||
13073 | return start_handler_parms (type_specifiers, declarator); | |
13074 | } | |
13075 | ||
13076 | /* Parse a throw-expression. | |
13077 | ||
13078 | throw-expression: | |
34cd5ae7 | 13079 | throw assignment-expression [opt] |
a723baf1 MM |
13080 | |
13081 | Returns a THROW_EXPR representing the throw-expression. */ | |
13082 | ||
13083 | static tree | |
94edc4ab | 13084 | cp_parser_throw_expression (cp_parser* parser) |
a723baf1 MM |
13085 | { |
13086 | tree expression; | |
89f1a6ec | 13087 | cp_token* token; |
a723baf1 MM |
13088 | |
13089 | cp_parser_require_keyword (parser, RID_THROW, "`throw'"); | |
89f1a6ec MM |
13090 | token = cp_lexer_peek_token (parser->lexer); |
13091 | /* Figure out whether or not there is an assignment-expression | |
13092 | following the "throw" keyword. */ | |
13093 | if (token->type == CPP_COMMA | |
13094 | || token->type == CPP_SEMICOLON | |
13095 | || token->type == CPP_CLOSE_PAREN | |
13096 | || token->type == CPP_CLOSE_SQUARE | |
13097 | || token->type == CPP_CLOSE_BRACE | |
13098 | || token->type == CPP_COLON) | |
a723baf1 | 13099 | expression = NULL_TREE; |
89f1a6ec MM |
13100 | else |
13101 | expression = cp_parser_assignment_expression (parser); | |
a723baf1 MM |
13102 | |
13103 | return build_throw (expression); | |
13104 | } | |
13105 | ||
13106 | /* GNU Extensions */ | |
13107 | ||
13108 | /* Parse an (optional) asm-specification. | |
13109 | ||
13110 | asm-specification: | |
13111 | asm ( string-literal ) | |
13112 | ||
13113 | If the asm-specification is present, returns a STRING_CST | |
13114 | corresponding to the string-literal. Otherwise, returns | |
13115 | NULL_TREE. */ | |
13116 | ||
13117 | static tree | |
94edc4ab | 13118 | cp_parser_asm_specification_opt (cp_parser* parser) |
a723baf1 MM |
13119 | { |
13120 | cp_token *token; | |
13121 | tree asm_specification; | |
13122 | ||
13123 | /* Peek at the next token. */ | |
13124 | token = cp_lexer_peek_token (parser->lexer); | |
13125 | /* If the next token isn't the `asm' keyword, then there's no | |
13126 | asm-specification. */ | |
13127 | if (!cp_parser_is_keyword (token, RID_ASM)) | |
13128 | return NULL_TREE; | |
13129 | ||
13130 | /* Consume the `asm' token. */ | |
13131 | cp_lexer_consume_token (parser->lexer); | |
13132 | /* Look for the `('. */ | |
13133 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13134 | ||
13135 | /* Look for the string-literal. */ | |
13136 | token = cp_parser_require (parser, CPP_STRING, "string-literal"); | |
13137 | if (token) | |
13138 | asm_specification = token->value; | |
13139 | else | |
13140 | asm_specification = NULL_TREE; | |
13141 | ||
13142 | /* Look for the `)'. */ | |
13143 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`('"); | |
13144 | ||
13145 | return asm_specification; | |
13146 | } | |
13147 | ||
13148 | /* Parse an asm-operand-list. | |
13149 | ||
13150 | asm-operand-list: | |
13151 | asm-operand | |
13152 | asm-operand-list , asm-operand | |
13153 | ||
13154 | asm-operand: | |
13155 | string-literal ( expression ) | |
13156 | [ string-literal ] string-literal ( expression ) | |
13157 | ||
13158 | Returns a TREE_LIST representing the operands. The TREE_VALUE of | |
13159 | each node is the expression. The TREE_PURPOSE is itself a | |
13160 | TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed | |
13161 | string-literal (or NULL_TREE if not present) and whose TREE_VALUE | |
13162 | is a STRING_CST for the string literal before the parenthesis. */ | |
13163 | ||
13164 | static tree | |
94edc4ab | 13165 | cp_parser_asm_operand_list (cp_parser* parser) |
a723baf1 MM |
13166 | { |
13167 | tree asm_operands = NULL_TREE; | |
13168 | ||
13169 | while (true) | |
13170 | { | |
13171 | tree string_literal; | |
13172 | tree expression; | |
13173 | tree name; | |
13174 | cp_token *token; | |
13175 | ||
13176 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
13177 | { | |
13178 | /* Consume the `[' token. */ | |
13179 | cp_lexer_consume_token (parser->lexer); | |
13180 | /* Read the operand name. */ | |
13181 | name = cp_parser_identifier (parser); | |
13182 | if (name != error_mark_node) | |
13183 | name = build_string (IDENTIFIER_LENGTH (name), | |
13184 | IDENTIFIER_POINTER (name)); | |
13185 | /* Look for the closing `]'. */ | |
13186 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
13187 | } | |
13188 | else | |
13189 | name = NULL_TREE; | |
13190 | /* Look for the string-literal. */ | |
13191 | token = cp_parser_require (parser, CPP_STRING, "string-literal"); | |
13192 | string_literal = token ? token->value : error_mark_node; | |
13193 | /* Look for the `('. */ | |
13194 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13195 | /* Parse the expression. */ | |
13196 | expression = cp_parser_expression (parser); | |
13197 | /* Look for the `)'. */ | |
13198 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
13199 | /* Add this operand to the list. */ | |
13200 | asm_operands = tree_cons (build_tree_list (name, string_literal), | |
13201 | expression, | |
13202 | asm_operands); | |
13203 | /* If the next token is not a `,', there are no more | |
13204 | operands. */ | |
13205 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
13206 | break; | |
13207 | /* Consume the `,'. */ | |
13208 | cp_lexer_consume_token (parser->lexer); | |
13209 | } | |
13210 | ||
13211 | return nreverse (asm_operands); | |
13212 | } | |
13213 | ||
13214 | /* Parse an asm-clobber-list. | |
13215 | ||
13216 | asm-clobber-list: | |
13217 | string-literal | |
13218 | asm-clobber-list , string-literal | |
13219 | ||
13220 | Returns a TREE_LIST, indicating the clobbers in the order that they | |
13221 | appeared. The TREE_VALUE of each node is a STRING_CST. */ | |
13222 | ||
13223 | static tree | |
94edc4ab | 13224 | cp_parser_asm_clobber_list (cp_parser* parser) |
a723baf1 MM |
13225 | { |
13226 | tree clobbers = NULL_TREE; | |
13227 | ||
13228 | while (true) | |
13229 | { | |
13230 | cp_token *token; | |
13231 | tree string_literal; | |
13232 | ||
13233 | /* Look for the string literal. */ | |
13234 | token = cp_parser_require (parser, CPP_STRING, "string-literal"); | |
13235 | string_literal = token ? token->value : error_mark_node; | |
13236 | /* Add it to the list. */ | |
13237 | clobbers = tree_cons (NULL_TREE, string_literal, clobbers); | |
13238 | /* If the next token is not a `,', then the list is | |
13239 | complete. */ | |
13240 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
13241 | break; | |
13242 | /* Consume the `,' token. */ | |
13243 | cp_lexer_consume_token (parser->lexer); | |
13244 | } | |
13245 | ||
13246 | return clobbers; | |
13247 | } | |
13248 | ||
13249 | /* Parse an (optional) series of attributes. | |
13250 | ||
13251 | attributes: | |
13252 | attributes attribute | |
13253 | ||
13254 | attribute: | |
13255 | __attribute__ (( attribute-list [opt] )) | |
13256 | ||
13257 | The return value is as for cp_parser_attribute_list. */ | |
13258 | ||
13259 | static tree | |
94edc4ab | 13260 | cp_parser_attributes_opt (cp_parser* parser) |
a723baf1 MM |
13261 | { |
13262 | tree attributes = NULL_TREE; | |
13263 | ||
13264 | while (true) | |
13265 | { | |
13266 | cp_token *token; | |
13267 | tree attribute_list; | |
13268 | ||
13269 | /* Peek at the next token. */ | |
13270 | token = cp_lexer_peek_token (parser->lexer); | |
13271 | /* If it's not `__attribute__', then we're done. */ | |
13272 | if (token->keyword != RID_ATTRIBUTE) | |
13273 | break; | |
13274 | ||
13275 | /* Consume the `__attribute__' keyword. */ | |
13276 | cp_lexer_consume_token (parser->lexer); | |
13277 | /* Look for the two `(' tokens. */ | |
13278 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13279 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13280 | ||
13281 | /* Peek at the next token. */ | |
13282 | token = cp_lexer_peek_token (parser->lexer); | |
13283 | if (token->type != CPP_CLOSE_PAREN) | |
13284 | /* Parse the attribute-list. */ | |
13285 | attribute_list = cp_parser_attribute_list (parser); | |
13286 | else | |
13287 | /* If the next token is a `)', then there is no attribute | |
13288 | list. */ | |
13289 | attribute_list = NULL; | |
13290 | ||
13291 | /* Look for the two `)' tokens. */ | |
13292 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
13293 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
13294 | ||
13295 | /* Add these new attributes to the list. */ | |
13296 | attributes = chainon (attributes, attribute_list); | |
13297 | } | |
13298 | ||
13299 | return attributes; | |
13300 | } | |
13301 | ||
13302 | /* Parse an attribute-list. | |
13303 | ||
13304 | attribute-list: | |
13305 | attribute | |
13306 | attribute-list , attribute | |
13307 | ||
13308 | attribute: | |
13309 | identifier | |
13310 | identifier ( identifier ) | |
13311 | identifier ( identifier , expression-list ) | |
13312 | identifier ( expression-list ) | |
13313 | ||
13314 | Returns a TREE_LIST. Each node corresponds to an attribute. THe | |
13315 | TREE_PURPOSE of each node is the identifier indicating which | |
13316 | attribute is in use. The TREE_VALUE represents the arguments, if | |
13317 | any. */ | |
13318 | ||
13319 | static tree | |
94edc4ab | 13320 | cp_parser_attribute_list (cp_parser* parser) |
a723baf1 MM |
13321 | { |
13322 | tree attribute_list = NULL_TREE; | |
13323 | ||
13324 | while (true) | |
13325 | { | |
13326 | cp_token *token; | |
13327 | tree identifier; | |
13328 | tree attribute; | |
13329 | ||
13330 | /* Look for the identifier. We also allow keywords here; for | |
13331 | example `__attribute__ ((const))' is legal. */ | |
13332 | token = cp_lexer_peek_token (parser->lexer); | |
13333 | if (token->type != CPP_NAME | |
13334 | && token->type != CPP_KEYWORD) | |
13335 | return error_mark_node; | |
13336 | /* Consume the token. */ | |
13337 | token = cp_lexer_consume_token (parser->lexer); | |
13338 | ||
13339 | /* Save away the identifier that indicates which attribute this is. */ | |
13340 | identifier = token->value; | |
13341 | attribute = build_tree_list (identifier, NULL_TREE); | |
13342 | ||
13343 | /* Peek at the next token. */ | |
13344 | token = cp_lexer_peek_token (parser->lexer); | |
13345 | /* If it's an `(', then parse the attribute arguments. */ | |
13346 | if (token->type == CPP_OPEN_PAREN) | |
13347 | { | |
13348 | tree arguments; | |
a723baf1 | 13349 | |
39703eb9 MM |
13350 | arguments = (cp_parser_parenthesized_expression_list |
13351 | (parser, true, /*non_constant_p=*/NULL)); | |
a723baf1 MM |
13352 | /* Save the identifier and arguments away. */ |
13353 | TREE_VALUE (attribute) = arguments; | |
a723baf1 MM |
13354 | } |
13355 | ||
13356 | /* Add this attribute to the list. */ | |
13357 | TREE_CHAIN (attribute) = attribute_list; | |
13358 | attribute_list = attribute; | |
13359 | ||
13360 | /* Now, look for more attributes. */ | |
13361 | token = cp_lexer_peek_token (parser->lexer); | |
13362 | /* If the next token isn't a `,', we're done. */ | |
13363 | if (token->type != CPP_COMMA) | |
13364 | break; | |
13365 | ||
cd0be382 | 13366 | /* Consume the comma and keep going. */ |
a723baf1 MM |
13367 | cp_lexer_consume_token (parser->lexer); |
13368 | } | |
13369 | ||
13370 | /* We built up the list in reverse order. */ | |
13371 | return nreverse (attribute_list); | |
13372 | } | |
13373 | ||
13374 | /* Parse an optional `__extension__' keyword. Returns TRUE if it is | |
13375 | present, and FALSE otherwise. *SAVED_PEDANTIC is set to the | |
13376 | current value of the PEDANTIC flag, regardless of whether or not | |
13377 | the `__extension__' keyword is present. The caller is responsible | |
13378 | for restoring the value of the PEDANTIC flag. */ | |
13379 | ||
13380 | static bool | |
94edc4ab | 13381 | cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic) |
a723baf1 MM |
13382 | { |
13383 | /* Save the old value of the PEDANTIC flag. */ | |
13384 | *saved_pedantic = pedantic; | |
13385 | ||
13386 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION)) | |
13387 | { | |
13388 | /* Consume the `__extension__' token. */ | |
13389 | cp_lexer_consume_token (parser->lexer); | |
13390 | /* We're not being pedantic while the `__extension__' keyword is | |
13391 | in effect. */ | |
13392 | pedantic = 0; | |
13393 | ||
13394 | return true; | |
13395 | } | |
13396 | ||
13397 | return false; | |
13398 | } | |
13399 | ||
13400 | /* Parse a label declaration. | |
13401 | ||
13402 | label-declaration: | |
13403 | __label__ label-declarator-seq ; | |
13404 | ||
13405 | label-declarator-seq: | |
13406 | identifier , label-declarator-seq | |
13407 | identifier */ | |
13408 | ||
13409 | static void | |
94edc4ab | 13410 | cp_parser_label_declaration (cp_parser* parser) |
a723baf1 MM |
13411 | { |
13412 | /* Look for the `__label__' keyword. */ | |
13413 | cp_parser_require_keyword (parser, RID_LABEL, "`__label__'"); | |
13414 | ||
13415 | while (true) | |
13416 | { | |
13417 | tree identifier; | |
13418 | ||
13419 | /* Look for an identifier. */ | |
13420 | identifier = cp_parser_identifier (parser); | |
13421 | /* Declare it as a lobel. */ | |
13422 | finish_label_decl (identifier); | |
13423 | /* If the next token is a `;', stop. */ | |
13424 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
13425 | break; | |
13426 | /* Look for the `,' separating the label declarations. */ | |
13427 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
13428 | } | |
13429 | ||
13430 | /* Look for the final `;'. */ | |
13431 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
13432 | } | |
13433 | ||
13434 | /* Support Functions */ | |
13435 | ||
13436 | /* Looks up NAME in the current scope, as given by PARSER->SCOPE. | |
13437 | NAME should have one of the representations used for an | |
13438 | id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE | |
13439 | is returned. If PARSER->SCOPE is a dependent type, then a | |
13440 | SCOPE_REF is returned. | |
13441 | ||
13442 | If NAME is a TEMPLATE_ID_EXPR, then it will be immediately | |
13443 | returned; the name was already resolved when the TEMPLATE_ID_EXPR | |
13444 | was formed. Abstractly, such entities should not be passed to this | |
13445 | function, because they do not need to be looked up, but it is | |
13446 | simpler to check for this special case here, rather than at the | |
13447 | call-sites. | |
13448 | ||
13449 | In cases not explicitly covered above, this function returns a | |
13450 | DECL, OVERLOAD, or baselink representing the result of the lookup. | |
13451 | If there was no entity with the indicated NAME, the ERROR_MARK_NODE | |
13452 | is returned. | |
13453 | ||
a723baf1 MM |
13454 | If IS_TYPE is TRUE, bindings that do not refer to types are |
13455 | ignored. | |
13456 | ||
b0bc6e8e KL |
13457 | If IS_TEMPLATE is TRUE, bindings that do not refer to templates are |
13458 | ignored. | |
13459 | ||
eea9800f MM |
13460 | If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces |
13461 | are ignored. | |
13462 | ||
a723baf1 MM |
13463 | If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent |
13464 | types. */ | |
13465 | ||
13466 | static tree | |
8d241e0b | 13467 | cp_parser_lookup_name (cp_parser *parser, tree name, |
b0bc6e8e KL |
13468 | bool is_type, bool is_template, bool is_namespace, |
13469 | bool check_dependency) | |
a723baf1 MM |
13470 | { |
13471 | tree decl; | |
13472 | tree object_type = parser->context->object_type; | |
13473 | ||
13474 | /* Now that we have looked up the name, the OBJECT_TYPE (if any) is | |
13475 | no longer valid. Note that if we are parsing tentatively, and | |
13476 | the parse fails, OBJECT_TYPE will be automatically restored. */ | |
13477 | parser->context->object_type = NULL_TREE; | |
13478 | ||
13479 | if (name == error_mark_node) | |
13480 | return error_mark_node; | |
13481 | ||
13482 | /* A template-id has already been resolved; there is no lookup to | |
13483 | do. */ | |
13484 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
13485 | return name; | |
13486 | if (BASELINK_P (name)) | |
13487 | { | |
13488 | my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name)) | |
13489 | == TEMPLATE_ID_EXPR), | |
13490 | 20020909); | |
13491 | return name; | |
13492 | } | |
13493 | ||
13494 | /* A BIT_NOT_EXPR is used to represent a destructor. By this point, | |
13495 | it should already have been checked to make sure that the name | |
13496 | used matches the type being destroyed. */ | |
13497 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
13498 | { | |
13499 | tree type; | |
13500 | ||
13501 | /* Figure out to which type this destructor applies. */ | |
13502 | if (parser->scope) | |
13503 | type = parser->scope; | |
13504 | else if (object_type) | |
13505 | type = object_type; | |
13506 | else | |
13507 | type = current_class_type; | |
13508 | /* If that's not a class type, there is no destructor. */ | |
13509 | if (!type || !CLASS_TYPE_P (type)) | |
13510 | return error_mark_node; | |
fd6e3cce GB |
13511 | if (!CLASSTYPE_DESTRUCTORS (type)) |
13512 | return error_mark_node; | |
a723baf1 MM |
13513 | /* If it was a class type, return the destructor. */ |
13514 | return CLASSTYPE_DESTRUCTORS (type); | |
13515 | } | |
13516 | ||
13517 | /* By this point, the NAME should be an ordinary identifier. If | |
13518 | the id-expression was a qualified name, the qualifying scope is | |
13519 | stored in PARSER->SCOPE at this point. */ | |
13520 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, | |
13521 | 20000619); | |
13522 | ||
13523 | /* Perform the lookup. */ | |
13524 | if (parser->scope) | |
13525 | { | |
1fb3244a | 13526 | bool dependent_p; |
a723baf1 MM |
13527 | |
13528 | if (parser->scope == error_mark_node) | |
13529 | return error_mark_node; | |
13530 | ||
13531 | /* If the SCOPE is dependent, the lookup must be deferred until | |
13532 | the template is instantiated -- unless we are explicitly | |
13533 | looking up names in uninstantiated templates. Even then, we | |
13534 | cannot look up the name if the scope is not a class type; it | |
13535 | might, for example, be a template type parameter. */ | |
1fb3244a MM |
13536 | dependent_p = (TYPE_P (parser->scope) |
13537 | && !(parser->in_declarator_p | |
13538 | && currently_open_class (parser->scope)) | |
13539 | && dependent_type_p (parser->scope)); | |
a723baf1 | 13540 | if ((check_dependency || !CLASS_TYPE_P (parser->scope)) |
1fb3244a | 13541 | && dependent_p) |
a723baf1 | 13542 | { |
b0bc6e8e | 13543 | if (is_type) |
a723baf1 MM |
13544 | /* The resolution to Core Issue 180 says that `struct A::B' |
13545 | should be considered a type-name, even if `A' is | |
13546 | dependent. */ | |
13547 | decl = TYPE_NAME (make_typename_type (parser->scope, | |
13548 | name, | |
13549 | /*complain=*/1)); | |
b0bc6e8e | 13550 | else if (is_template) |
5b4acce1 KL |
13551 | decl = make_unbound_class_template (parser->scope, |
13552 | name, | |
13553 | /*complain=*/1); | |
b0bc6e8e KL |
13554 | else |
13555 | decl = build_nt (SCOPE_REF, parser->scope, name); | |
a723baf1 MM |
13556 | } |
13557 | else | |
13558 | { | |
13559 | /* If PARSER->SCOPE is a dependent type, then it must be a | |
13560 | class type, and we must not be checking dependencies; | |
13561 | otherwise, we would have processed this lookup above. So | |
13562 | that PARSER->SCOPE is not considered a dependent base by | |
13563 | lookup_member, we must enter the scope here. */ | |
1fb3244a | 13564 | if (dependent_p) |
a723baf1 MM |
13565 | push_scope (parser->scope); |
13566 | /* If the PARSER->SCOPE is a a template specialization, it | |
13567 | may be instantiated during name lookup. In that case, | |
13568 | errors may be issued. Even if we rollback the current | |
13569 | tentative parse, those errors are valid. */ | |
5e08432e MM |
13570 | decl = lookup_qualified_name (parser->scope, name, is_type, |
13571 | /*complain=*/true); | |
1fb3244a | 13572 | if (dependent_p) |
a723baf1 MM |
13573 | pop_scope (parser->scope); |
13574 | } | |
13575 | parser->qualifying_scope = parser->scope; | |
13576 | parser->object_scope = NULL_TREE; | |
13577 | } | |
13578 | else if (object_type) | |
13579 | { | |
13580 | tree object_decl = NULL_TREE; | |
13581 | /* Look up the name in the scope of the OBJECT_TYPE, unless the | |
13582 | OBJECT_TYPE is not a class. */ | |
13583 | if (CLASS_TYPE_P (object_type)) | |
13584 | /* If the OBJECT_TYPE is a template specialization, it may | |
13585 | be instantiated during name lookup. In that case, errors | |
13586 | may be issued. Even if we rollback the current tentative | |
13587 | parse, those errors are valid. */ | |
13588 | object_decl = lookup_member (object_type, | |
13589 | name, | |
13590 | /*protect=*/0, is_type); | |
13591 | /* Look it up in the enclosing context, too. */ | |
13592 | decl = lookup_name_real (name, is_type, /*nonclass=*/0, | |
eea9800f | 13593 | is_namespace, |
a723baf1 MM |
13594 | /*flags=*/0); |
13595 | parser->object_scope = object_type; | |
13596 | parser->qualifying_scope = NULL_TREE; | |
13597 | if (object_decl) | |
13598 | decl = object_decl; | |
13599 | } | |
13600 | else | |
13601 | { | |
13602 | decl = lookup_name_real (name, is_type, /*nonclass=*/0, | |
eea9800f | 13603 | is_namespace, |
a723baf1 MM |
13604 | /*flags=*/0); |
13605 | parser->qualifying_scope = NULL_TREE; | |
13606 | parser->object_scope = NULL_TREE; | |
13607 | } | |
13608 | ||
13609 | /* If the lookup failed, let our caller know. */ | |
13610 | if (!decl | |
13611 | || decl == error_mark_node | |
13612 | || (TREE_CODE (decl) == FUNCTION_DECL | |
13613 | && DECL_ANTICIPATED (decl))) | |
13614 | return error_mark_node; | |
13615 | ||
13616 | /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ | |
13617 | if (TREE_CODE (decl) == TREE_LIST) | |
13618 | { | |
13619 | /* The error message we have to print is too complicated for | |
13620 | cp_parser_error, so we incorporate its actions directly. */ | |
e5976695 | 13621 | if (!cp_parser_simulate_error (parser)) |
a723baf1 MM |
13622 | { |
13623 | error ("reference to `%D' is ambiguous", name); | |
13624 | print_candidates (decl); | |
13625 | } | |
13626 | return error_mark_node; | |
13627 | } | |
13628 | ||
13629 | my_friendly_assert (DECL_P (decl) | |
13630 | || TREE_CODE (decl) == OVERLOAD | |
13631 | || TREE_CODE (decl) == SCOPE_REF | |
5b4acce1 | 13632 | || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE |
a723baf1 MM |
13633 | || BASELINK_P (decl), |
13634 | 20000619); | |
13635 | ||
13636 | /* If we have resolved the name of a member declaration, check to | |
13637 | see if the declaration is accessible. When the name resolves to | |
34cd5ae7 | 13638 | set of overloaded functions, accessibility is checked when |
a723baf1 MM |
13639 | overload resolution is done. |
13640 | ||
13641 | During an explicit instantiation, access is not checked at all, | |
13642 | as per [temp.explicit]. */ | |
8d241e0b | 13643 | if (DECL_P (decl)) |
ee76b931 | 13644 | check_accessibility_of_qualified_id (decl, object_type, parser->scope); |
a723baf1 MM |
13645 | |
13646 | return decl; | |
13647 | } | |
13648 | ||
13649 | /* Like cp_parser_lookup_name, but for use in the typical case where | |
b0bc6e8e KL |
13650 | CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE, |
13651 | IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */ | |
a723baf1 MM |
13652 | |
13653 | static tree | |
94edc4ab | 13654 | cp_parser_lookup_name_simple (cp_parser* parser, tree name) |
a723baf1 MM |
13655 | { |
13656 | return cp_parser_lookup_name (parser, name, | |
eea9800f | 13657 | /*is_type=*/false, |
b0bc6e8e | 13658 | /*is_template=*/false, |
eea9800f | 13659 | /*is_namespace=*/false, |
a723baf1 MM |
13660 | /*check_dependency=*/true); |
13661 | } | |
13662 | ||
a723baf1 MM |
13663 | /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in |
13664 | the current context, return the TYPE_DECL. If TAG_NAME_P is | |
13665 | true, the DECL indicates the class being defined in a class-head, | |
13666 | or declared in an elaborated-type-specifier. | |
13667 | ||
13668 | Otherwise, return DECL. */ | |
13669 | ||
13670 | static tree | |
13671 | cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p) | |
13672 | { | |
710b73e6 KL |
13673 | /* If the TEMPLATE_DECL is being declared as part of a class-head, |
13674 | the translation from TEMPLATE_DECL to TYPE_DECL occurs: | |
a723baf1 MM |
13675 | |
13676 | struct A { | |
13677 | template <typename T> struct B; | |
13678 | }; | |
13679 | ||
13680 | template <typename T> struct A::B {}; | |
13681 | ||
13682 | Similarly, in a elaborated-type-specifier: | |
13683 | ||
13684 | namespace N { struct X{}; } | |
13685 | ||
13686 | struct A { | |
13687 | template <typename T> friend struct N::X; | |
13688 | }; | |
13689 | ||
710b73e6 KL |
13690 | However, if the DECL refers to a class type, and we are in |
13691 | the scope of the class, then the name lookup automatically | |
13692 | finds the TYPE_DECL created by build_self_reference rather | |
13693 | than a TEMPLATE_DECL. For example, in: | |
13694 | ||
13695 | template <class T> struct S { | |
13696 | S s; | |
13697 | }; | |
13698 | ||
13699 | there is no need to handle such case. */ | |
13700 | ||
13701 | if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p) | |
a723baf1 MM |
13702 | return DECL_TEMPLATE_RESULT (decl); |
13703 | ||
13704 | return decl; | |
13705 | } | |
13706 | ||
13707 | /* If too many, or too few, template-parameter lists apply to the | |
13708 | declarator, issue an error message. Returns TRUE if all went well, | |
13709 | and FALSE otherwise. */ | |
13710 | ||
13711 | static bool | |
94edc4ab NN |
13712 | cp_parser_check_declarator_template_parameters (cp_parser* parser, |
13713 | tree declarator) | |
a723baf1 MM |
13714 | { |
13715 | unsigned num_templates; | |
13716 | ||
13717 | /* We haven't seen any classes that involve template parameters yet. */ | |
13718 | num_templates = 0; | |
13719 | ||
13720 | switch (TREE_CODE (declarator)) | |
13721 | { | |
13722 | case CALL_EXPR: | |
13723 | case ARRAY_REF: | |
13724 | case INDIRECT_REF: | |
13725 | case ADDR_EXPR: | |
13726 | { | |
13727 | tree main_declarator = TREE_OPERAND (declarator, 0); | |
13728 | return | |
13729 | cp_parser_check_declarator_template_parameters (parser, | |
13730 | main_declarator); | |
13731 | } | |
13732 | ||
13733 | case SCOPE_REF: | |
13734 | { | |
13735 | tree scope; | |
13736 | tree member; | |
13737 | ||
13738 | scope = TREE_OPERAND (declarator, 0); | |
13739 | member = TREE_OPERAND (declarator, 1); | |
13740 | ||
13741 | /* If this is a pointer-to-member, then we are not interested | |
13742 | in the SCOPE, because it does not qualify the thing that is | |
13743 | being declared. */ | |
13744 | if (TREE_CODE (member) == INDIRECT_REF) | |
13745 | return (cp_parser_check_declarator_template_parameters | |
13746 | (parser, member)); | |
13747 | ||
13748 | while (scope && CLASS_TYPE_P (scope)) | |
13749 | { | |
13750 | /* You're supposed to have one `template <...>' | |
13751 | for every template class, but you don't need one | |
13752 | for a full specialization. For example: | |
13753 | ||
13754 | template <class T> struct S{}; | |
13755 | template <> struct S<int> { void f(); }; | |
13756 | void S<int>::f () {} | |
13757 | ||
13758 | is correct; there shouldn't be a `template <>' for | |
13759 | the definition of `S<int>::f'. */ | |
13760 | if (CLASSTYPE_TEMPLATE_INFO (scope) | |
13761 | && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope) | |
13762 | || uses_template_parms (CLASSTYPE_TI_ARGS (scope))) | |
13763 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))) | |
13764 | ++num_templates; | |
13765 | ||
13766 | scope = TYPE_CONTEXT (scope); | |
13767 | } | |
13768 | } | |
13769 | ||
13770 | /* Fall through. */ | |
13771 | ||
13772 | default: | |
13773 | /* If the DECLARATOR has the form `X<y>' then it uses one | |
13774 | additional level of template parameters. */ | |
13775 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
13776 | ++num_templates; | |
13777 | ||
13778 | return cp_parser_check_template_parameters (parser, | |
13779 | num_templates); | |
13780 | } | |
13781 | } | |
13782 | ||
13783 | /* NUM_TEMPLATES were used in the current declaration. If that is | |
13784 | invalid, return FALSE and issue an error messages. Otherwise, | |
13785 | return TRUE. */ | |
13786 | ||
13787 | static bool | |
94edc4ab NN |
13788 | cp_parser_check_template_parameters (cp_parser* parser, |
13789 | unsigned num_templates) | |
a723baf1 MM |
13790 | { |
13791 | /* If there are more template classes than parameter lists, we have | |
13792 | something like: | |
13793 | ||
13794 | template <class T> void S<T>::R<T>::f (); */ | |
13795 | if (parser->num_template_parameter_lists < num_templates) | |
13796 | { | |
13797 | error ("too few template-parameter-lists"); | |
13798 | return false; | |
13799 | } | |
13800 | /* If there are the same number of template classes and parameter | |
13801 | lists, that's OK. */ | |
13802 | if (parser->num_template_parameter_lists == num_templates) | |
13803 | return true; | |
13804 | /* If there are more, but only one more, then we are referring to a | |
13805 | member template. That's OK too. */ | |
13806 | if (parser->num_template_parameter_lists == num_templates + 1) | |
13807 | return true; | |
13808 | /* Otherwise, there are too many template parameter lists. We have | |
13809 | something like: | |
13810 | ||
13811 | template <class T> template <class U> void S::f(); */ | |
13812 | error ("too many template-parameter-lists"); | |
13813 | return false; | |
13814 | } | |
13815 | ||
13816 | /* Parse a binary-expression of the general form: | |
13817 | ||
13818 | binary-expression: | |
13819 | <expr> | |
13820 | binary-expression <token> <expr> | |
13821 | ||
13822 | The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used | |
13823 | to parser the <expr>s. If the first production is used, then the | |
13824 | value returned by FN is returned directly. Otherwise, a node with | |
13825 | the indicated EXPR_TYPE is returned, with operands corresponding to | |
13826 | the two sub-expressions. */ | |
13827 | ||
13828 | static tree | |
94edc4ab NN |
13829 | cp_parser_binary_expression (cp_parser* parser, |
13830 | const cp_parser_token_tree_map token_tree_map, | |
13831 | cp_parser_expression_fn fn) | |
a723baf1 MM |
13832 | { |
13833 | tree lhs; | |
13834 | ||
13835 | /* Parse the first expression. */ | |
13836 | lhs = (*fn) (parser); | |
13837 | /* Now, look for more expressions. */ | |
13838 | while (true) | |
13839 | { | |
13840 | cp_token *token; | |
39b1af70 | 13841 | const cp_parser_token_tree_map_node *map_node; |
a723baf1 MM |
13842 | tree rhs; |
13843 | ||
13844 | /* Peek at the next token. */ | |
13845 | token = cp_lexer_peek_token (parser->lexer); | |
13846 | /* If the token is `>', and that's not an operator at the | |
13847 | moment, then we're done. */ | |
13848 | if (token->type == CPP_GREATER | |
13849 | && !parser->greater_than_is_operator_p) | |
13850 | break; | |
34cd5ae7 | 13851 | /* If we find one of the tokens we want, build the corresponding |
a723baf1 MM |
13852 | tree representation. */ |
13853 | for (map_node = token_tree_map; | |
13854 | map_node->token_type != CPP_EOF; | |
13855 | ++map_node) | |
13856 | if (map_node->token_type == token->type) | |
13857 | { | |
13858 | /* Consume the operator token. */ | |
13859 | cp_lexer_consume_token (parser->lexer); | |
13860 | /* Parse the right-hand side of the expression. */ | |
13861 | rhs = (*fn) (parser); | |
13862 | /* Build the binary tree node. */ | |
13863 | lhs = build_x_binary_op (map_node->tree_type, lhs, rhs); | |
13864 | break; | |
13865 | } | |
13866 | ||
13867 | /* If the token wasn't one of the ones we want, we're done. */ | |
13868 | if (map_node->token_type == CPP_EOF) | |
13869 | break; | |
13870 | } | |
13871 | ||
13872 | return lhs; | |
13873 | } | |
13874 | ||
13875 | /* Parse an optional `::' token indicating that the following name is | |
13876 | from the global namespace. If so, PARSER->SCOPE is set to the | |
13877 | GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE, | |
13878 | unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone. | |
13879 | Returns the new value of PARSER->SCOPE, if the `::' token is | |
13880 | present, and NULL_TREE otherwise. */ | |
13881 | ||
13882 | static tree | |
94edc4ab | 13883 | cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p) |
a723baf1 MM |
13884 | { |
13885 | cp_token *token; | |
13886 | ||
13887 | /* Peek at the next token. */ | |
13888 | token = cp_lexer_peek_token (parser->lexer); | |
13889 | /* If we're looking at a `::' token then we're starting from the | |
13890 | global namespace, not our current location. */ | |
13891 | if (token->type == CPP_SCOPE) | |
13892 | { | |
13893 | /* Consume the `::' token. */ | |
13894 | cp_lexer_consume_token (parser->lexer); | |
13895 | /* Set the SCOPE so that we know where to start the lookup. */ | |
13896 | parser->scope = global_namespace; | |
13897 | parser->qualifying_scope = global_namespace; | |
13898 | parser->object_scope = NULL_TREE; | |
13899 | ||
13900 | return parser->scope; | |
13901 | } | |
13902 | else if (!current_scope_valid_p) | |
13903 | { | |
13904 | parser->scope = NULL_TREE; | |
13905 | parser->qualifying_scope = NULL_TREE; | |
13906 | parser->object_scope = NULL_TREE; | |
13907 | } | |
13908 | ||
13909 | return NULL_TREE; | |
13910 | } | |
13911 | ||
13912 | /* Returns TRUE if the upcoming token sequence is the start of a | |
13913 | constructor declarator. If FRIEND_P is true, the declarator is | |
13914 | preceded by the `friend' specifier. */ | |
13915 | ||
13916 | static bool | |
13917 | cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p) | |
13918 | { | |
13919 | bool constructor_p; | |
13920 | tree type_decl = NULL_TREE; | |
13921 | bool nested_name_p; | |
2050a1bb MM |
13922 | cp_token *next_token; |
13923 | ||
13924 | /* The common case is that this is not a constructor declarator, so | |
8fbc5ae7 MM |
13925 | try to avoid doing lots of work if at all possible. It's not |
13926 | valid declare a constructor at function scope. */ | |
13927 | if (at_function_scope_p ()) | |
13928 | return false; | |
13929 | /* And only certain tokens can begin a constructor declarator. */ | |
2050a1bb MM |
13930 | next_token = cp_lexer_peek_token (parser->lexer); |
13931 | if (next_token->type != CPP_NAME | |
13932 | && next_token->type != CPP_SCOPE | |
13933 | && next_token->type != CPP_NESTED_NAME_SPECIFIER | |
13934 | && next_token->type != CPP_TEMPLATE_ID) | |
13935 | return false; | |
a723baf1 MM |
13936 | |
13937 | /* Parse tentatively; we are going to roll back all of the tokens | |
13938 | consumed here. */ | |
13939 | cp_parser_parse_tentatively (parser); | |
13940 | /* Assume that we are looking at a constructor declarator. */ | |
13941 | constructor_p = true; | |
8d241e0b | 13942 | |
a723baf1 MM |
13943 | /* Look for the optional `::' operator. */ |
13944 | cp_parser_global_scope_opt (parser, | |
13945 | /*current_scope_valid_p=*/false); | |
13946 | /* Look for the nested-name-specifier. */ | |
13947 | nested_name_p | |
13948 | = (cp_parser_nested_name_specifier_opt (parser, | |
13949 | /*typename_keyword_p=*/false, | |
13950 | /*check_dependency_p=*/false, | |
a668c6ad MM |
13951 | /*type_p=*/false, |
13952 | /*is_declaration=*/false) | |
a723baf1 MM |
13953 | != NULL_TREE); |
13954 | /* Outside of a class-specifier, there must be a | |
13955 | nested-name-specifier. */ | |
13956 | if (!nested_name_p && | |
13957 | (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type) | |
13958 | || friend_p)) | |
13959 | constructor_p = false; | |
13960 | /* If we still think that this might be a constructor-declarator, | |
13961 | look for a class-name. */ | |
13962 | if (constructor_p) | |
13963 | { | |
13964 | /* If we have: | |
13965 | ||
8fbc5ae7 | 13966 | template <typename T> struct S { S(); }; |
a723baf1 MM |
13967 | template <typename T> S<T>::S (); |
13968 | ||
13969 | we must recognize that the nested `S' names a class. | |
13970 | Similarly, for: | |
13971 | ||
13972 | template <typename T> S<T>::S<T> (); | |
13973 | ||
13974 | we must recognize that the nested `S' names a template. */ | |
13975 | type_decl = cp_parser_class_name (parser, | |
13976 | /*typename_keyword_p=*/false, | |
13977 | /*template_keyword_p=*/false, | |
13978 | /*type_p=*/false, | |
a723baf1 | 13979 | /*check_dependency_p=*/false, |
a668c6ad MM |
13980 | /*class_head_p=*/false, |
13981 | /*is_declaration=*/false); | |
a723baf1 MM |
13982 | /* If there was no class-name, then this is not a constructor. */ |
13983 | constructor_p = !cp_parser_error_occurred (parser); | |
13984 | } | |
8d241e0b | 13985 | |
a723baf1 MM |
13986 | /* If we're still considering a constructor, we have to see a `(', |
13987 | to begin the parameter-declaration-clause, followed by either a | |
13988 | `)', an `...', or a decl-specifier. We need to check for a | |
13989 | type-specifier to avoid being fooled into thinking that: | |
13990 | ||
13991 | S::S (f) (int); | |
13992 | ||
13993 | is a constructor. (It is actually a function named `f' that | |
13994 | takes one parameter (of type `int') and returns a value of type | |
13995 | `S::S'. */ | |
13996 | if (constructor_p | |
13997 | && cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) | |
13998 | { | |
13999 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN) | |
14000 | && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS) | |
14001 | && !cp_parser_storage_class_specifier_opt (parser)) | |
14002 | { | |
5dae1114 | 14003 | tree type; |
4047b164 | 14004 | unsigned saved_num_template_parameter_lists; |
5dae1114 MM |
14005 | |
14006 | /* Names appearing in the type-specifier should be looked up | |
14007 | in the scope of the class. */ | |
14008 | if (current_class_type) | |
14009 | type = NULL_TREE; | |
a723baf1 MM |
14010 | else |
14011 | { | |
5dae1114 MM |
14012 | type = TREE_TYPE (type_decl); |
14013 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
14d22dd6 MM |
14014 | { |
14015 | type = resolve_typename_type (type, | |
14016 | /*only_current_p=*/false); | |
14017 | if (type == error_mark_node) | |
14018 | { | |
14019 | cp_parser_abort_tentative_parse (parser); | |
14020 | return false; | |
14021 | } | |
14022 | } | |
5dae1114 | 14023 | push_scope (type); |
a723baf1 | 14024 | } |
4047b164 KL |
14025 | |
14026 | /* Inside the constructor parameter list, surrounding | |
14027 | template-parameter-lists do not apply. */ | |
14028 | saved_num_template_parameter_lists | |
14029 | = parser->num_template_parameter_lists; | |
14030 | parser->num_template_parameter_lists = 0; | |
14031 | ||
5dae1114 MM |
14032 | /* Look for the type-specifier. */ |
14033 | cp_parser_type_specifier (parser, | |
14034 | CP_PARSER_FLAGS_NONE, | |
14035 | /*is_friend=*/false, | |
14036 | /*is_declarator=*/true, | |
14037 | /*declares_class_or_enum=*/NULL, | |
14038 | /*is_cv_qualifier=*/NULL); | |
4047b164 KL |
14039 | |
14040 | parser->num_template_parameter_lists | |
14041 | = saved_num_template_parameter_lists; | |
14042 | ||
5dae1114 MM |
14043 | /* Leave the scope of the class. */ |
14044 | if (type) | |
14045 | pop_scope (type); | |
14046 | ||
14047 | constructor_p = !cp_parser_error_occurred (parser); | |
a723baf1 MM |
14048 | } |
14049 | } | |
14050 | else | |
14051 | constructor_p = false; | |
14052 | /* We did not really want to consume any tokens. */ | |
14053 | cp_parser_abort_tentative_parse (parser); | |
14054 | ||
14055 | return constructor_p; | |
14056 | } | |
14057 | ||
14058 | /* Parse the definition of the function given by the DECL_SPECIFIERS, | |
cf22909c | 14059 | ATTRIBUTES, and DECLARATOR. The access checks have been deferred; |
a723baf1 MM |
14060 | they must be performed once we are in the scope of the function. |
14061 | ||
14062 | Returns the function defined. */ | |
14063 | ||
14064 | static tree | |
14065 | cp_parser_function_definition_from_specifiers_and_declarator | |
94edc4ab NN |
14066 | (cp_parser* parser, |
14067 | tree decl_specifiers, | |
14068 | tree attributes, | |
14069 | tree declarator) | |
a723baf1 MM |
14070 | { |
14071 | tree fn; | |
14072 | bool success_p; | |
14073 | ||
14074 | /* Begin the function-definition. */ | |
14075 | success_p = begin_function_definition (decl_specifiers, | |
14076 | attributes, | |
14077 | declarator); | |
14078 | ||
14079 | /* If there were names looked up in the decl-specifier-seq that we | |
14080 | did not check, check them now. We must wait until we are in the | |
14081 | scope of the function to perform the checks, since the function | |
14082 | might be a friend. */ | |
cf22909c | 14083 | perform_deferred_access_checks (); |
a723baf1 MM |
14084 | |
14085 | if (!success_p) | |
14086 | { | |
14087 | /* If begin_function_definition didn't like the definition, skip | |
14088 | the entire function. */ | |
14089 | error ("invalid function declaration"); | |
14090 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
14091 | fn = error_mark_node; | |
14092 | } | |
14093 | else | |
14094 | fn = cp_parser_function_definition_after_declarator (parser, | |
14095 | /*inline_p=*/false); | |
14096 | ||
14097 | return fn; | |
14098 | } | |
14099 | ||
14100 | /* Parse the part of a function-definition that follows the | |
14101 | declarator. INLINE_P is TRUE iff this function is an inline | |
14102 | function defined with a class-specifier. | |
14103 | ||
14104 | Returns the function defined. */ | |
14105 | ||
14106 | static tree | |
94edc4ab NN |
14107 | cp_parser_function_definition_after_declarator (cp_parser* parser, |
14108 | bool inline_p) | |
a723baf1 MM |
14109 | { |
14110 | tree fn; | |
14111 | bool ctor_initializer_p = false; | |
14112 | bool saved_in_unbraced_linkage_specification_p; | |
14113 | unsigned saved_num_template_parameter_lists; | |
14114 | ||
14115 | /* If the next token is `return', then the code may be trying to | |
14116 | make use of the "named return value" extension that G++ used to | |
14117 | support. */ | |
14118 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN)) | |
14119 | { | |
14120 | /* Consume the `return' keyword. */ | |
14121 | cp_lexer_consume_token (parser->lexer); | |
14122 | /* Look for the identifier that indicates what value is to be | |
14123 | returned. */ | |
14124 | cp_parser_identifier (parser); | |
14125 | /* Issue an error message. */ | |
14126 | error ("named return values are no longer supported"); | |
14127 | /* Skip tokens until we reach the start of the function body. */ | |
21eb631b MM |
14128 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE) |
14129 | && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF)) | |
a723baf1 MM |
14130 | cp_lexer_consume_token (parser->lexer); |
14131 | } | |
14132 | /* The `extern' in `extern "C" void f () { ... }' does not apply to | |
14133 | anything declared inside `f'. */ | |
14134 | saved_in_unbraced_linkage_specification_p | |
14135 | = parser->in_unbraced_linkage_specification_p; | |
14136 | parser->in_unbraced_linkage_specification_p = false; | |
14137 | /* Inside the function, surrounding template-parameter-lists do not | |
14138 | apply. */ | |
14139 | saved_num_template_parameter_lists | |
14140 | = parser->num_template_parameter_lists; | |
14141 | parser->num_template_parameter_lists = 0; | |
14142 | /* If the next token is `try', then we are looking at a | |
14143 | function-try-block. */ | |
14144 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY)) | |
14145 | ctor_initializer_p = cp_parser_function_try_block (parser); | |
14146 | /* A function-try-block includes the function-body, so we only do | |
14147 | this next part if we're not processing a function-try-block. */ | |
14148 | else | |
14149 | ctor_initializer_p | |
14150 | = cp_parser_ctor_initializer_opt_and_function_body (parser); | |
14151 | ||
14152 | /* Finish the function. */ | |
14153 | fn = finish_function ((ctor_initializer_p ? 1 : 0) | | |
14154 | (inline_p ? 2 : 0)); | |
14155 | /* Generate code for it, if necessary. */ | |
8cd2462c | 14156 | expand_or_defer_fn (fn); |
a723baf1 MM |
14157 | /* Restore the saved values. */ |
14158 | parser->in_unbraced_linkage_specification_p | |
14159 | = saved_in_unbraced_linkage_specification_p; | |
14160 | parser->num_template_parameter_lists | |
14161 | = saved_num_template_parameter_lists; | |
14162 | ||
14163 | return fn; | |
14164 | } | |
14165 | ||
14166 | /* Parse a template-declaration, assuming that the `export' (and | |
14167 | `extern') keywords, if present, has already been scanned. MEMBER_P | |
14168 | is as for cp_parser_template_declaration. */ | |
14169 | ||
14170 | static void | |
94edc4ab | 14171 | cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p) |
a723baf1 MM |
14172 | { |
14173 | tree decl = NULL_TREE; | |
14174 | tree parameter_list; | |
14175 | bool friend_p = false; | |
14176 | ||
14177 | /* Look for the `template' keyword. */ | |
14178 | if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'")) | |
14179 | return; | |
14180 | ||
14181 | /* And the `<'. */ | |
14182 | if (!cp_parser_require (parser, CPP_LESS, "`<'")) | |
14183 | return; | |
14184 | ||
a723baf1 MM |
14185 | /* If the next token is `>', then we have an invalid |
14186 | specialization. Rather than complain about an invalid template | |
14187 | parameter, issue an error message here. */ | |
14188 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) | |
14189 | { | |
14190 | cp_parser_error (parser, "invalid explicit specialization"); | |
2f9afd51 | 14191 | begin_specialization (); |
a723baf1 MM |
14192 | parameter_list = NULL_TREE; |
14193 | } | |
14194 | else | |
2f9afd51 KL |
14195 | { |
14196 | /* Parse the template parameters. */ | |
14197 | begin_template_parm_list (); | |
14198 | parameter_list = cp_parser_template_parameter_list (parser); | |
14199 | parameter_list = end_template_parm_list (parameter_list); | |
14200 | } | |
14201 | ||
a723baf1 MM |
14202 | /* Look for the `>'. */ |
14203 | cp_parser_skip_until_found (parser, CPP_GREATER, "`>'"); | |
14204 | /* We just processed one more parameter list. */ | |
14205 | ++parser->num_template_parameter_lists; | |
14206 | /* If the next token is `template', there are more template | |
14207 | parameters. */ | |
14208 | if (cp_lexer_next_token_is_keyword (parser->lexer, | |
14209 | RID_TEMPLATE)) | |
14210 | cp_parser_template_declaration_after_export (parser, member_p); | |
14211 | else | |
14212 | { | |
14213 | decl = cp_parser_single_declaration (parser, | |
14214 | member_p, | |
14215 | &friend_p); | |
14216 | ||
14217 | /* If this is a member template declaration, let the front | |
14218 | end know. */ | |
14219 | if (member_p && !friend_p && decl) | |
37d407a1 KL |
14220 | { |
14221 | if (TREE_CODE (decl) == TYPE_DECL) | |
14222 | cp_parser_check_access_in_redeclaration (decl); | |
14223 | ||
14224 | decl = finish_member_template_decl (decl); | |
14225 | } | |
a723baf1 | 14226 | else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL) |
19db77ce KL |
14227 | make_friend_class (current_class_type, TREE_TYPE (decl), |
14228 | /*complain=*/true); | |
a723baf1 MM |
14229 | } |
14230 | /* We are done with the current parameter list. */ | |
14231 | --parser->num_template_parameter_lists; | |
14232 | ||
14233 | /* Finish up. */ | |
14234 | finish_template_decl (parameter_list); | |
14235 | ||
14236 | /* Register member declarations. */ | |
14237 | if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl)) | |
14238 | finish_member_declaration (decl); | |
14239 | ||
14240 | /* If DECL is a function template, we must return to parse it later. | |
14241 | (Even though there is no definition, there might be default | |
14242 | arguments that need handling.) */ | |
14243 | if (member_p && decl | |
14244 | && (TREE_CODE (decl) == FUNCTION_DECL | |
14245 | || DECL_FUNCTION_TEMPLATE_P (decl))) | |
14246 | TREE_VALUE (parser->unparsed_functions_queues) | |
8218bd34 | 14247 | = tree_cons (NULL_TREE, decl, |
a723baf1 MM |
14248 | TREE_VALUE (parser->unparsed_functions_queues)); |
14249 | } | |
14250 | ||
14251 | /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or | |
14252 | `function-definition' sequence. MEMBER_P is true, this declaration | |
14253 | appears in a class scope. | |
14254 | ||
14255 | Returns the DECL for the declared entity. If FRIEND_P is non-NULL, | |
14256 | *FRIEND_P is set to TRUE iff the declaration is a friend. */ | |
14257 | ||
14258 | static tree | |
94edc4ab NN |
14259 | cp_parser_single_declaration (cp_parser* parser, |
14260 | bool member_p, | |
14261 | bool* friend_p) | |
a723baf1 | 14262 | { |
560ad596 | 14263 | int declares_class_or_enum; |
a723baf1 MM |
14264 | tree decl = NULL_TREE; |
14265 | tree decl_specifiers; | |
14266 | tree attributes; | |
4bb8ca28 | 14267 | bool function_definition_p = false; |
a723baf1 | 14268 | |
a723baf1 | 14269 | /* Defer access checks until we know what is being declared. */ |
8d241e0b | 14270 | push_deferring_access_checks (dk_deferred); |
cf22909c | 14271 | |
a723baf1 MM |
14272 | /* Try the `decl-specifier-seq [opt] init-declarator [opt]' |
14273 | alternative. */ | |
14274 | decl_specifiers | |
14275 | = cp_parser_decl_specifier_seq (parser, | |
14276 | CP_PARSER_FLAGS_OPTIONAL, | |
14277 | &attributes, | |
14278 | &declares_class_or_enum); | |
4bb8ca28 MM |
14279 | if (friend_p) |
14280 | *friend_p = cp_parser_friend_p (decl_specifiers); | |
a723baf1 MM |
14281 | /* Gather up the access checks that occurred the |
14282 | decl-specifier-seq. */ | |
cf22909c KL |
14283 | stop_deferring_access_checks (); |
14284 | ||
a723baf1 MM |
14285 | /* Check for the declaration of a template class. */ |
14286 | if (declares_class_or_enum) | |
14287 | { | |
14288 | if (cp_parser_declares_only_class_p (parser)) | |
14289 | { | |
14290 | decl = shadow_tag (decl_specifiers); | |
14291 | if (decl) | |
14292 | decl = TYPE_NAME (decl); | |
14293 | else | |
14294 | decl = error_mark_node; | |
14295 | } | |
14296 | } | |
14297 | else | |
14298 | decl = NULL_TREE; | |
14299 | /* If it's not a template class, try for a template function. If | |
14300 | the next token is a `;', then this declaration does not declare | |
14301 | anything. But, if there were errors in the decl-specifiers, then | |
14302 | the error might well have come from an attempted class-specifier. | |
14303 | In that case, there's no need to warn about a missing declarator. */ | |
14304 | if (!decl | |
14305 | && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON) | |
14306 | || !value_member (error_mark_node, decl_specifiers))) | |
14307 | decl = cp_parser_init_declarator (parser, | |
14308 | decl_specifiers, | |
14309 | attributes, | |
4bb8ca28 | 14310 | /*function_definition_allowed_p=*/true, |
a723baf1 | 14311 | member_p, |
560ad596 | 14312 | declares_class_or_enum, |
4bb8ca28 | 14313 | &function_definition_p); |
cf22909c KL |
14314 | |
14315 | pop_deferring_access_checks (); | |
14316 | ||
a723baf1 MM |
14317 | /* Clear any current qualification; whatever comes next is the start |
14318 | of something new. */ | |
14319 | parser->scope = NULL_TREE; | |
14320 | parser->qualifying_scope = NULL_TREE; | |
14321 | parser->object_scope = NULL_TREE; | |
14322 | /* Look for a trailing `;' after the declaration. */ | |
4bb8ca28 MM |
14323 | if (!function_definition_p |
14324 | && !cp_parser_require (parser, CPP_SEMICOLON, "`;'")) | |
a723baf1 | 14325 | cp_parser_skip_to_end_of_block_or_statement (parser); |
a723baf1 MM |
14326 | |
14327 | return decl; | |
14328 | } | |
14329 | ||
d6b4ea85 MM |
14330 | /* Parse a cast-expression that is not the operand of a unary "&". */ |
14331 | ||
14332 | static tree | |
14333 | cp_parser_simple_cast_expression (cp_parser *parser) | |
14334 | { | |
14335 | return cp_parser_cast_expression (parser, /*address_p=*/false); | |
14336 | } | |
14337 | ||
a723baf1 MM |
14338 | /* Parse a functional cast to TYPE. Returns an expression |
14339 | representing the cast. */ | |
14340 | ||
14341 | static tree | |
94edc4ab | 14342 | cp_parser_functional_cast (cp_parser* parser, tree type) |
a723baf1 MM |
14343 | { |
14344 | tree expression_list; | |
14345 | ||
39703eb9 MM |
14346 | expression_list |
14347 | = cp_parser_parenthesized_expression_list (parser, false, | |
14348 | /*non_constant_p=*/NULL); | |
a723baf1 MM |
14349 | |
14350 | return build_functional_cast (type, expression_list); | |
14351 | } | |
14352 | ||
4bb8ca28 MM |
14353 | /* Save the tokens that make up the body of a member function defined |
14354 | in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have | |
14355 | already been parsed. The ATTRIBUTES are any GNU "__attribute__" | |
14356 | specifiers applied to the declaration. Returns the FUNCTION_DECL | |
14357 | for the member function. */ | |
14358 | ||
7ce27103 | 14359 | static tree |
4bb8ca28 MM |
14360 | cp_parser_save_member_function_body (cp_parser* parser, |
14361 | tree decl_specifiers, | |
14362 | tree declarator, | |
14363 | tree attributes) | |
14364 | { | |
14365 | cp_token_cache *cache; | |
14366 | tree fn; | |
14367 | ||
14368 | /* Create the function-declaration. */ | |
14369 | fn = start_method (decl_specifiers, declarator, attributes); | |
14370 | /* If something went badly wrong, bail out now. */ | |
14371 | if (fn == error_mark_node) | |
14372 | { | |
14373 | /* If there's a function-body, skip it. */ | |
14374 | if (cp_parser_token_starts_function_definition_p | |
14375 | (cp_lexer_peek_token (parser->lexer))) | |
14376 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
14377 | return error_mark_node; | |
14378 | } | |
14379 | ||
14380 | /* Remember it, if there default args to post process. */ | |
14381 | cp_parser_save_default_args (parser, fn); | |
14382 | ||
14383 | /* Create a token cache. */ | |
14384 | cache = cp_token_cache_new (); | |
14385 | /* Save away the tokens that make up the body of the | |
14386 | function. */ | |
14387 | cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0); | |
14388 | /* Handle function try blocks. */ | |
14389 | while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH)) | |
14390 | cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0); | |
14391 | ||
14392 | /* Save away the inline definition; we will process it when the | |
14393 | class is complete. */ | |
14394 | DECL_PENDING_INLINE_INFO (fn) = cache; | |
14395 | DECL_PENDING_INLINE_P (fn) = 1; | |
14396 | ||
14397 | /* We need to know that this was defined in the class, so that | |
14398 | friend templates are handled correctly. */ | |
14399 | DECL_INITIALIZED_IN_CLASS_P (fn) = 1; | |
14400 | ||
14401 | /* We're done with the inline definition. */ | |
14402 | finish_method (fn); | |
14403 | ||
14404 | /* Add FN to the queue of functions to be parsed later. */ | |
14405 | TREE_VALUE (parser->unparsed_functions_queues) | |
14406 | = tree_cons (NULL_TREE, fn, | |
14407 | TREE_VALUE (parser->unparsed_functions_queues)); | |
14408 | ||
14409 | return fn; | |
14410 | } | |
14411 | ||
ec75414f MM |
14412 | /* Parse a template-argument-list, as well as the trailing ">" (but |
14413 | not the opening ">"). See cp_parser_template_argument_list for the | |
14414 | return value. */ | |
14415 | ||
14416 | static tree | |
14417 | cp_parser_enclosed_template_argument_list (cp_parser* parser) | |
14418 | { | |
14419 | tree arguments; | |
14420 | tree saved_scope; | |
14421 | tree saved_qualifying_scope; | |
14422 | tree saved_object_scope; | |
14423 | bool saved_greater_than_is_operator_p; | |
14424 | ||
14425 | /* [temp.names] | |
14426 | ||
14427 | When parsing a template-id, the first non-nested `>' is taken as | |
14428 | the end of the template-argument-list rather than a greater-than | |
14429 | operator. */ | |
14430 | saved_greater_than_is_operator_p | |
14431 | = parser->greater_than_is_operator_p; | |
14432 | parser->greater_than_is_operator_p = false; | |
14433 | /* Parsing the argument list may modify SCOPE, so we save it | |
14434 | here. */ | |
14435 | saved_scope = parser->scope; | |
14436 | saved_qualifying_scope = parser->qualifying_scope; | |
14437 | saved_object_scope = parser->object_scope; | |
14438 | /* Parse the template-argument-list itself. */ | |
14439 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) | |
14440 | arguments = NULL_TREE; | |
14441 | else | |
14442 | arguments = cp_parser_template_argument_list (parser); | |
4d5297fa GB |
14443 | /* Look for the `>' that ends the template-argument-list. If we find |
14444 | a '>>' instead, it's probably just a typo. */ | |
14445 | if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) | |
14446 | { | |
14447 | if (!saved_greater_than_is_operator_p) | |
14448 | { | |
14449 | /* If we're in a nested template argument list, the '>>' has to be | |
14450 | a typo for '> >'. We emit the error message, but we continue | |
14451 | parsing and we push a '>' as next token, so that the argument | |
14452 | list will be parsed correctly.. */ | |
14453 | cp_token* token; | |
14454 | error ("`>>' should be `> >' within a nested template argument list"); | |
14455 | token = cp_lexer_peek_token (parser->lexer); | |
14456 | token->type = CPP_GREATER; | |
14457 | } | |
14458 | else | |
14459 | { | |
14460 | /* If this is not a nested template argument list, the '>>' is | |
14461 | a typo for '>'. Emit an error message and continue. */ | |
14462 | error ("spurious `>>', use `>' to terminate a template argument list"); | |
14463 | cp_lexer_consume_token (parser->lexer); | |
14464 | } | |
14465 | } | |
14466 | else | |
14467 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
ec75414f MM |
14468 | /* The `>' token might be a greater-than operator again now. */ |
14469 | parser->greater_than_is_operator_p | |
14470 | = saved_greater_than_is_operator_p; | |
14471 | /* Restore the SAVED_SCOPE. */ | |
14472 | parser->scope = saved_scope; | |
14473 | parser->qualifying_scope = saved_qualifying_scope; | |
14474 | parser->object_scope = saved_object_scope; | |
14475 | ||
14476 | return arguments; | |
14477 | } | |
14478 | ||
a723baf1 MM |
14479 | /* MEMBER_FUNCTION is a member function, or a friend. If default |
14480 | arguments, or the body of the function have not yet been parsed, | |
14481 | parse them now. */ | |
14482 | ||
14483 | static void | |
94edc4ab | 14484 | cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function) |
a723baf1 MM |
14485 | { |
14486 | cp_lexer *saved_lexer; | |
14487 | ||
14488 | /* If this member is a template, get the underlying | |
14489 | FUNCTION_DECL. */ | |
14490 | if (DECL_FUNCTION_TEMPLATE_P (member_function)) | |
14491 | member_function = DECL_TEMPLATE_RESULT (member_function); | |
14492 | ||
14493 | /* There should not be any class definitions in progress at this | |
14494 | point; the bodies of members are only parsed outside of all class | |
14495 | definitions. */ | |
14496 | my_friendly_assert (parser->num_classes_being_defined == 0, 20010816); | |
14497 | /* While we're parsing the member functions we might encounter more | |
14498 | classes. We want to handle them right away, but we don't want | |
14499 | them getting mixed up with functions that are currently in the | |
14500 | queue. */ | |
14501 | parser->unparsed_functions_queues | |
14502 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
14503 | ||
14504 | /* Make sure that any template parameters are in scope. */ | |
14505 | maybe_begin_member_template_processing (member_function); | |
14506 | ||
a723baf1 MM |
14507 | /* If the body of the function has not yet been parsed, parse it |
14508 | now. */ | |
14509 | if (DECL_PENDING_INLINE_P (member_function)) | |
14510 | { | |
14511 | tree function_scope; | |
14512 | cp_token_cache *tokens; | |
14513 | ||
14514 | /* The function is no longer pending; we are processing it. */ | |
14515 | tokens = DECL_PENDING_INLINE_INFO (member_function); | |
14516 | DECL_PENDING_INLINE_INFO (member_function) = NULL; | |
14517 | DECL_PENDING_INLINE_P (member_function) = 0; | |
14518 | /* If this was an inline function in a local class, enter the scope | |
14519 | of the containing function. */ | |
14520 | function_scope = decl_function_context (member_function); | |
14521 | if (function_scope) | |
14522 | push_function_context_to (function_scope); | |
14523 | ||
14524 | /* Save away the current lexer. */ | |
14525 | saved_lexer = parser->lexer; | |
14526 | /* Make a new lexer to feed us the tokens saved for this function. */ | |
14527 | parser->lexer = cp_lexer_new_from_tokens (tokens); | |
14528 | parser->lexer->next = saved_lexer; | |
14529 | ||
14530 | /* Set the current source position to be the location of the first | |
14531 | token in the saved inline body. */ | |
3466b292 | 14532 | cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
14533 | |
14534 | /* Let the front end know that we going to be defining this | |
14535 | function. */ | |
14536 | start_function (NULL_TREE, member_function, NULL_TREE, | |
14537 | SF_PRE_PARSED | SF_INCLASS_INLINE); | |
14538 | ||
14539 | /* Now, parse the body of the function. */ | |
14540 | cp_parser_function_definition_after_declarator (parser, | |
14541 | /*inline_p=*/true); | |
14542 | ||
14543 | /* Leave the scope of the containing function. */ | |
14544 | if (function_scope) | |
14545 | pop_function_context_from (function_scope); | |
14546 | /* Restore the lexer. */ | |
14547 | parser->lexer = saved_lexer; | |
14548 | } | |
14549 | ||
14550 | /* Remove any template parameters from the symbol table. */ | |
14551 | maybe_end_member_template_processing (); | |
14552 | ||
14553 | /* Restore the queue. */ | |
14554 | parser->unparsed_functions_queues | |
14555 | = TREE_CHAIN (parser->unparsed_functions_queues); | |
14556 | } | |
14557 | ||
cd0be382 | 14558 | /* If DECL contains any default args, remember it on the unparsed |
8db1028e NS |
14559 | functions queue. */ |
14560 | ||
14561 | static void | |
14562 | cp_parser_save_default_args (cp_parser* parser, tree decl) | |
14563 | { | |
14564 | tree probe; | |
14565 | ||
14566 | for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
14567 | probe; | |
14568 | probe = TREE_CHAIN (probe)) | |
14569 | if (TREE_PURPOSE (probe)) | |
14570 | { | |
14571 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
14572 | = tree_cons (NULL_TREE, decl, | |
14573 | TREE_PURPOSE (parser->unparsed_functions_queues)); | |
14574 | break; | |
14575 | } | |
14576 | return; | |
14577 | } | |
14578 | ||
8218bd34 MM |
14579 | /* FN is a FUNCTION_DECL which may contains a parameter with an |
14580 | unparsed DEFAULT_ARG. Parse the default args now. */ | |
a723baf1 MM |
14581 | |
14582 | static void | |
8218bd34 | 14583 | cp_parser_late_parsing_default_args (cp_parser *parser, tree fn) |
a723baf1 MM |
14584 | { |
14585 | cp_lexer *saved_lexer; | |
14586 | cp_token_cache *tokens; | |
14587 | bool saved_local_variables_forbidden_p; | |
14588 | tree parameters; | |
8218bd34 | 14589 | |
b92bc2a0 NS |
14590 | /* While we're parsing the default args, we might (due to the |
14591 | statement expression extension) encounter more classes. We want | |
14592 | to handle them right away, but we don't want them getting mixed | |
14593 | up with default args that are currently in the queue. */ | |
14594 | parser->unparsed_functions_queues | |
14595 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
14596 | ||
8218bd34 | 14597 | for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
a723baf1 MM |
14598 | parameters; |
14599 | parameters = TREE_CHAIN (parameters)) | |
14600 | { | |
14601 | if (!TREE_PURPOSE (parameters) | |
14602 | || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG) | |
14603 | continue; | |
14604 | ||
14605 | /* Save away the current lexer. */ | |
14606 | saved_lexer = parser->lexer; | |
14607 | /* Create a new one, using the tokens we have saved. */ | |
14608 | tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters)); | |
14609 | parser->lexer = cp_lexer_new_from_tokens (tokens); | |
14610 | ||
14611 | /* Set the current source position to be the location of the | |
14612 | first token in the default argument. */ | |
3466b292 | 14613 | cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
14614 | |
14615 | /* Local variable names (and the `this' keyword) may not appear | |
14616 | in a default argument. */ | |
14617 | saved_local_variables_forbidden_p = parser->local_variables_forbidden_p; | |
14618 | parser->local_variables_forbidden_p = true; | |
14619 | /* Parse the assignment-expression. */ | |
f128e1f3 | 14620 | if (DECL_CLASS_SCOPE_P (fn)) |
14d22dd6 | 14621 | push_nested_class (DECL_CONTEXT (fn)); |
a723baf1 | 14622 | TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser); |
f128e1f3 | 14623 | if (DECL_CLASS_SCOPE_P (fn)) |
e5976695 | 14624 | pop_nested_class (); |
a723baf1 MM |
14625 | |
14626 | /* Restore saved state. */ | |
14627 | parser->lexer = saved_lexer; | |
14628 | parser->local_variables_forbidden_p = saved_local_variables_forbidden_p; | |
14629 | } | |
b92bc2a0 NS |
14630 | |
14631 | /* Restore the queue. */ | |
14632 | parser->unparsed_functions_queues | |
14633 | = TREE_CHAIN (parser->unparsed_functions_queues); | |
a723baf1 MM |
14634 | } |
14635 | ||
14636 | /* Parse the operand of `sizeof' (or a similar operator). Returns | |
14637 | either a TYPE or an expression, depending on the form of the | |
14638 | input. The KEYWORD indicates which kind of expression we have | |
14639 | encountered. */ | |
14640 | ||
14641 | static tree | |
94edc4ab | 14642 | cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword) |
a723baf1 MM |
14643 | { |
14644 | static const char *format; | |
14645 | tree expr = NULL_TREE; | |
14646 | const char *saved_message; | |
67c03833 | 14647 | bool saved_integral_constant_expression_p; |
a723baf1 MM |
14648 | |
14649 | /* Initialize FORMAT the first time we get here. */ | |
14650 | if (!format) | |
14651 | format = "types may not be defined in `%s' expressions"; | |
14652 | ||
14653 | /* Types cannot be defined in a `sizeof' expression. Save away the | |
14654 | old message. */ | |
14655 | saved_message = parser->type_definition_forbidden_message; | |
14656 | /* And create the new one. */ | |
14657 | parser->type_definition_forbidden_message | |
c68b0a84 KG |
14658 | = xmalloc (strlen (format) |
14659 | + strlen (IDENTIFIER_POINTER (ridpointers[keyword])) | |
14660 | + 1 /* `\0' */); | |
a723baf1 MM |
14661 | sprintf ((char *) parser->type_definition_forbidden_message, |
14662 | format, IDENTIFIER_POINTER (ridpointers[keyword])); | |
14663 | ||
14664 | /* The restrictions on constant-expressions do not apply inside | |
14665 | sizeof expressions. */ | |
67c03833 JM |
14666 | saved_integral_constant_expression_p = parser->integral_constant_expression_p; |
14667 | parser->integral_constant_expression_p = false; | |
a723baf1 | 14668 | |
3beb3abf MM |
14669 | /* Do not actually evaluate the expression. */ |
14670 | ++skip_evaluation; | |
a723baf1 MM |
14671 | /* If it's a `(', then we might be looking at the type-id |
14672 | construction. */ | |
14673 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
14674 | { | |
14675 | tree type; | |
4f8163b1 | 14676 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
14677 | |
14678 | /* We can't be sure yet whether we're looking at a type-id or an | |
14679 | expression. */ | |
14680 | cp_parser_parse_tentatively (parser); | |
14681 | /* Consume the `('. */ | |
14682 | cp_lexer_consume_token (parser->lexer); | |
14683 | /* Parse the type-id. */ | |
4f8163b1 MM |
14684 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
14685 | parser->in_type_id_in_expr_p = true; | |
a723baf1 | 14686 | type = cp_parser_type_id (parser); |
4f8163b1 | 14687 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 MM |
14688 | /* Now, look for the trailing `)'. */ |
14689 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
14690 | /* If all went well, then we're done. */ | |
14691 | if (cp_parser_parse_definitely (parser)) | |
14692 | { | |
14693 | /* Build a list of decl-specifiers; right now, we have only | |
14694 | a single type-specifier. */ | |
14695 | type = build_tree_list (NULL_TREE, | |
14696 | type); | |
14697 | ||
14698 | /* Call grokdeclarator to figure out what type this is. */ | |
14699 | expr = grokdeclarator (NULL_TREE, | |
14700 | type, | |
14701 | TYPENAME, | |
14702 | /*initialized=*/0, | |
14703 | /*attrlist=*/NULL); | |
14704 | } | |
14705 | } | |
14706 | ||
14707 | /* If the type-id production did not work out, then we must be | |
14708 | looking at the unary-expression production. */ | |
14709 | if (!expr) | |
14710 | expr = cp_parser_unary_expression (parser, /*address_p=*/false); | |
3beb3abf MM |
14711 | /* Go back to evaluating expressions. */ |
14712 | --skip_evaluation; | |
a723baf1 MM |
14713 | |
14714 | /* Free the message we created. */ | |
14715 | free ((char *) parser->type_definition_forbidden_message); | |
14716 | /* And restore the old one. */ | |
14717 | parser->type_definition_forbidden_message = saved_message; | |
67c03833 | 14718 | parser->integral_constant_expression_p = saved_integral_constant_expression_p; |
a723baf1 MM |
14719 | |
14720 | return expr; | |
14721 | } | |
14722 | ||
14723 | /* If the current declaration has no declarator, return true. */ | |
14724 | ||
14725 | static bool | |
14726 | cp_parser_declares_only_class_p (cp_parser *parser) | |
14727 | { | |
14728 | /* If the next token is a `;' or a `,' then there is no | |
14729 | declarator. */ | |
14730 | return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) | |
14731 | || cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); | |
14732 | } | |
14733 | ||
d17811fd MM |
14734 | /* Simplify EXPR if it is a non-dependent expression. Returns the |
14735 | (possibly simplified) expression. */ | |
14736 | ||
14737 | static tree | |
14738 | cp_parser_fold_non_dependent_expr (tree expr) | |
14739 | { | |
14740 | /* If we're in a template, but EXPR isn't value dependent, simplify | |
14741 | it. We're supposed to treat: | |
14742 | ||
14743 | template <typename T> void f(T[1 + 1]); | |
14744 | template <typename T> void f(T[2]); | |
14745 | ||
14746 | as two declarations of the same function, for example. */ | |
14747 | if (processing_template_decl | |
14748 | && !type_dependent_expression_p (expr) | |
14749 | && !value_dependent_expression_p (expr)) | |
14750 | { | |
14751 | HOST_WIDE_INT saved_processing_template_decl; | |
14752 | ||
14753 | saved_processing_template_decl = processing_template_decl; | |
14754 | processing_template_decl = 0; | |
14755 | expr = tsubst_copy_and_build (expr, | |
14756 | /*args=*/NULL_TREE, | |
14757 | tf_error, | |
b3445994 MM |
14758 | /*in_decl=*/NULL_TREE, |
14759 | /*function_p=*/false); | |
d17811fd MM |
14760 | processing_template_decl = saved_processing_template_decl; |
14761 | } | |
14762 | return expr; | |
14763 | } | |
14764 | ||
a723baf1 MM |
14765 | /* DECL_SPECIFIERS is the representation of a decl-specifier-seq. |
14766 | Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */ | |
14767 | ||
14768 | static bool | |
94edc4ab | 14769 | cp_parser_friend_p (tree decl_specifiers) |
a723baf1 MM |
14770 | { |
14771 | while (decl_specifiers) | |
14772 | { | |
14773 | /* See if this decl-specifier is `friend'. */ | |
14774 | if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE | |
14775 | && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND) | |
14776 | return true; | |
14777 | ||
14778 | /* Go on to the next decl-specifier. */ | |
14779 | decl_specifiers = TREE_CHAIN (decl_specifiers); | |
14780 | } | |
14781 | ||
14782 | return false; | |
14783 | } | |
14784 | ||
14785 | /* If the next token is of the indicated TYPE, consume it. Otherwise, | |
14786 | issue an error message indicating that TOKEN_DESC was expected. | |
14787 | ||
14788 | Returns the token consumed, if the token had the appropriate type. | |
14789 | Otherwise, returns NULL. */ | |
14790 | ||
14791 | static cp_token * | |
94edc4ab NN |
14792 | cp_parser_require (cp_parser* parser, |
14793 | enum cpp_ttype type, | |
14794 | const char* token_desc) | |
a723baf1 MM |
14795 | { |
14796 | if (cp_lexer_next_token_is (parser->lexer, type)) | |
14797 | return cp_lexer_consume_token (parser->lexer); | |
14798 | else | |
14799 | { | |
e5976695 MM |
14800 | /* Output the MESSAGE -- unless we're parsing tentatively. */ |
14801 | if (!cp_parser_simulate_error (parser)) | |
216bb6e1 MM |
14802 | { |
14803 | char *message = concat ("expected ", token_desc, NULL); | |
14804 | cp_parser_error (parser, message); | |
14805 | free (message); | |
14806 | } | |
a723baf1 MM |
14807 | return NULL; |
14808 | } | |
14809 | } | |
14810 | ||
14811 | /* Like cp_parser_require, except that tokens will be skipped until | |
14812 | the desired token is found. An error message is still produced if | |
14813 | the next token is not as expected. */ | |
14814 | ||
14815 | static void | |
94edc4ab NN |
14816 | cp_parser_skip_until_found (cp_parser* parser, |
14817 | enum cpp_ttype type, | |
14818 | const char* token_desc) | |
a723baf1 MM |
14819 | { |
14820 | cp_token *token; | |
14821 | unsigned nesting_depth = 0; | |
14822 | ||
14823 | if (cp_parser_require (parser, type, token_desc)) | |
14824 | return; | |
14825 | ||
14826 | /* Skip tokens until the desired token is found. */ | |
14827 | while (true) | |
14828 | { | |
14829 | /* Peek at the next token. */ | |
14830 | token = cp_lexer_peek_token (parser->lexer); | |
14831 | /* If we've reached the token we want, consume it and | |
14832 | stop. */ | |
14833 | if (token->type == type && !nesting_depth) | |
14834 | { | |
14835 | cp_lexer_consume_token (parser->lexer); | |
14836 | return; | |
14837 | } | |
14838 | /* If we've run out of tokens, stop. */ | |
14839 | if (token->type == CPP_EOF) | |
14840 | return; | |
14841 | if (token->type == CPP_OPEN_BRACE | |
14842 | || token->type == CPP_OPEN_PAREN | |
14843 | || token->type == CPP_OPEN_SQUARE) | |
14844 | ++nesting_depth; | |
14845 | else if (token->type == CPP_CLOSE_BRACE | |
14846 | || token->type == CPP_CLOSE_PAREN | |
14847 | || token->type == CPP_CLOSE_SQUARE) | |
14848 | { | |
14849 | if (nesting_depth-- == 0) | |
14850 | return; | |
14851 | } | |
14852 | /* Consume this token. */ | |
14853 | cp_lexer_consume_token (parser->lexer); | |
14854 | } | |
14855 | } | |
14856 | ||
14857 | /* If the next token is the indicated keyword, consume it. Otherwise, | |
14858 | issue an error message indicating that TOKEN_DESC was expected. | |
14859 | ||
14860 | Returns the token consumed, if the token had the appropriate type. | |
14861 | Otherwise, returns NULL. */ | |
14862 | ||
14863 | static cp_token * | |
94edc4ab NN |
14864 | cp_parser_require_keyword (cp_parser* parser, |
14865 | enum rid keyword, | |
14866 | const char* token_desc) | |
a723baf1 MM |
14867 | { |
14868 | cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc); | |
14869 | ||
14870 | if (token && token->keyword != keyword) | |
14871 | { | |
14872 | dyn_string_t error_msg; | |
14873 | ||
14874 | /* Format the error message. */ | |
14875 | error_msg = dyn_string_new (0); | |
14876 | dyn_string_append_cstr (error_msg, "expected "); | |
14877 | dyn_string_append_cstr (error_msg, token_desc); | |
14878 | cp_parser_error (parser, error_msg->s); | |
14879 | dyn_string_delete (error_msg); | |
14880 | return NULL; | |
14881 | } | |
14882 | ||
14883 | return token; | |
14884 | } | |
14885 | ||
14886 | /* Returns TRUE iff TOKEN is a token that can begin the body of a | |
14887 | function-definition. */ | |
14888 | ||
14889 | static bool | |
94edc4ab | 14890 | cp_parser_token_starts_function_definition_p (cp_token* token) |
a723baf1 MM |
14891 | { |
14892 | return (/* An ordinary function-body begins with an `{'. */ | |
14893 | token->type == CPP_OPEN_BRACE | |
14894 | /* A ctor-initializer begins with a `:'. */ | |
14895 | || token->type == CPP_COLON | |
14896 | /* A function-try-block begins with `try'. */ | |
14897 | || token->keyword == RID_TRY | |
14898 | /* The named return value extension begins with `return'. */ | |
14899 | || token->keyword == RID_RETURN); | |
14900 | } | |
14901 | ||
14902 | /* Returns TRUE iff the next token is the ":" or "{" beginning a class | |
14903 | definition. */ | |
14904 | ||
14905 | static bool | |
14906 | cp_parser_next_token_starts_class_definition_p (cp_parser *parser) | |
14907 | { | |
14908 | cp_token *token; | |
14909 | ||
14910 | token = cp_lexer_peek_token (parser->lexer); | |
14911 | return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON); | |
14912 | } | |
14913 | ||
d17811fd | 14914 | /* Returns TRUE iff the next token is the "," or ">" ending a |
4d5297fa GB |
14915 | template-argument. ">>" is also accepted (after the full |
14916 | argument was parsed) because it's probably a typo for "> >", | |
14917 | and there is a specific diagnostic for this. */ | |
d17811fd MM |
14918 | |
14919 | static bool | |
14920 | cp_parser_next_token_ends_template_argument_p (cp_parser *parser) | |
14921 | { | |
14922 | cp_token *token; | |
14923 | ||
14924 | token = cp_lexer_peek_token (parser->lexer); | |
4d5297fa GB |
14925 | return (token->type == CPP_COMMA || token->type == CPP_GREATER |
14926 | || token->type == CPP_RSHIFT); | |
d17811fd | 14927 | } |
f4abade9 GB |
14928 | |
14929 | /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the | |
14930 | (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */ | |
14931 | ||
14932 | static bool | |
14933 | cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser, | |
14934 | size_t n) | |
14935 | { | |
14936 | cp_token *token; | |
14937 | ||
14938 | token = cp_lexer_peek_nth_token (parser->lexer, n); | |
14939 | if (token->type == CPP_LESS) | |
14940 | return true; | |
14941 | /* Check for the sequence `<::' in the original code. It would be lexed as | |
14942 | `[:', where `[' is a digraph, and there is no whitespace before | |
14943 | `:'. */ | |
14944 | if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH) | |
14945 | { | |
14946 | cp_token *token2; | |
14947 | token2 = cp_lexer_peek_nth_token (parser->lexer, n+1); | |
14948 | if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE)) | |
14949 | return true; | |
14950 | } | |
14951 | return false; | |
14952 | } | |
d17811fd | 14953 | |
a723baf1 MM |
14954 | /* Returns the kind of tag indicated by TOKEN, if it is a class-key, |
14955 | or none_type otherwise. */ | |
14956 | ||
14957 | static enum tag_types | |
94edc4ab | 14958 | cp_parser_token_is_class_key (cp_token* token) |
a723baf1 MM |
14959 | { |
14960 | switch (token->keyword) | |
14961 | { | |
14962 | case RID_CLASS: | |
14963 | return class_type; | |
14964 | case RID_STRUCT: | |
14965 | return record_type; | |
14966 | case RID_UNION: | |
14967 | return union_type; | |
14968 | ||
14969 | default: | |
14970 | return none_type; | |
14971 | } | |
14972 | } | |
14973 | ||
14974 | /* Issue an error message if the CLASS_KEY does not match the TYPE. */ | |
14975 | ||
14976 | static void | |
14977 | cp_parser_check_class_key (enum tag_types class_key, tree type) | |
14978 | { | |
14979 | if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type)) | |
14980 | pedwarn ("`%s' tag used in naming `%#T'", | |
14981 | class_key == union_type ? "union" | |
14982 | : class_key == record_type ? "struct" : "class", | |
14983 | type); | |
14984 | } | |
14985 | ||
cd0be382 | 14986 | /* Issue an error message if DECL is redeclared with different |
37d407a1 KL |
14987 | access than its original declaration [class.access.spec/3]. |
14988 | This applies to nested classes and nested class templates. | |
14989 | [class.mem/1]. */ | |
14990 | ||
14991 | static void cp_parser_check_access_in_redeclaration (tree decl) | |
14992 | { | |
14993 | if (!CLASS_TYPE_P (TREE_TYPE (decl))) | |
14994 | return; | |
14995 | ||
14996 | if ((TREE_PRIVATE (decl) | |
14997 | != (current_access_specifier == access_private_node)) | |
14998 | || (TREE_PROTECTED (decl) | |
14999 | != (current_access_specifier == access_protected_node))) | |
15000 | error ("%D redeclared with different access", decl); | |
15001 | } | |
15002 | ||
a723baf1 MM |
15003 | /* Look for the `template' keyword, as a syntactic disambiguator. |
15004 | Return TRUE iff it is present, in which case it will be | |
15005 | consumed. */ | |
15006 | ||
15007 | static bool | |
15008 | cp_parser_optional_template_keyword (cp_parser *parser) | |
15009 | { | |
15010 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
15011 | { | |
15012 | /* The `template' keyword can only be used within templates; | |
15013 | outside templates the parser can always figure out what is a | |
15014 | template and what is not. */ | |
15015 | if (!processing_template_decl) | |
15016 | { | |
15017 | error ("`template' (as a disambiguator) is only allowed " | |
15018 | "within templates"); | |
15019 | /* If this part of the token stream is rescanned, the same | |
15020 | error message would be generated. So, we purge the token | |
15021 | from the stream. */ | |
15022 | cp_lexer_purge_token (parser->lexer); | |
15023 | return false; | |
15024 | } | |
15025 | else | |
15026 | { | |
15027 | /* Consume the `template' keyword. */ | |
15028 | cp_lexer_consume_token (parser->lexer); | |
15029 | return true; | |
15030 | } | |
15031 | } | |
15032 | ||
15033 | return false; | |
15034 | } | |
15035 | ||
2050a1bb MM |
15036 | /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token, |
15037 | set PARSER->SCOPE, and perform other related actions. */ | |
15038 | ||
15039 | static void | |
15040 | cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser) | |
15041 | { | |
15042 | tree value; | |
15043 | tree check; | |
15044 | ||
15045 | /* Get the stored value. */ | |
15046 | value = cp_lexer_consume_token (parser->lexer)->value; | |
15047 | /* Perform any access checks that were deferred. */ | |
15048 | for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check)) | |
cf22909c | 15049 | perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check)); |
2050a1bb MM |
15050 | /* Set the scope from the stored value. */ |
15051 | parser->scope = TREE_VALUE (value); | |
15052 | parser->qualifying_scope = TREE_TYPE (value); | |
15053 | parser->object_scope = NULL_TREE; | |
15054 | } | |
15055 | ||
852dcbdd | 15056 | /* Add tokens to CACHE until a non-nested END token appears. */ |
a723baf1 MM |
15057 | |
15058 | static void | |
15059 | cp_parser_cache_group (cp_parser *parser, | |
15060 | cp_token_cache *cache, | |
15061 | enum cpp_ttype end, | |
15062 | unsigned depth) | |
15063 | { | |
15064 | while (true) | |
15065 | { | |
15066 | cp_token *token; | |
15067 | ||
15068 | /* Abort a parenthesized expression if we encounter a brace. */ | |
15069 | if ((end == CPP_CLOSE_PAREN || depth == 0) | |
15070 | && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
15071 | return; | |
15072 | /* Consume the next token. */ | |
15073 | token = cp_lexer_consume_token (parser->lexer); | |
15074 | /* If we've reached the end of the file, stop. */ | |
15075 | if (token->type == CPP_EOF) | |
15076 | return; | |
15077 | /* Add this token to the tokens we are saving. */ | |
15078 | cp_token_cache_push_token (cache, token); | |
15079 | /* See if it starts a new group. */ | |
15080 | if (token->type == CPP_OPEN_BRACE) | |
15081 | { | |
15082 | cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1); | |
15083 | if (depth == 0) | |
15084 | return; | |
15085 | } | |
15086 | else if (token->type == CPP_OPEN_PAREN) | |
15087 | cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1); | |
15088 | else if (token->type == end) | |
15089 | return; | |
15090 | } | |
15091 | } | |
15092 | ||
15093 | /* Begin parsing tentatively. We always save tokens while parsing | |
15094 | tentatively so that if the tentative parsing fails we can restore the | |
15095 | tokens. */ | |
15096 | ||
15097 | static void | |
94edc4ab | 15098 | cp_parser_parse_tentatively (cp_parser* parser) |
a723baf1 MM |
15099 | { |
15100 | /* Enter a new parsing context. */ | |
15101 | parser->context = cp_parser_context_new (parser->context); | |
15102 | /* Begin saving tokens. */ | |
15103 | cp_lexer_save_tokens (parser->lexer); | |
15104 | /* In order to avoid repetitive access control error messages, | |
15105 | access checks are queued up until we are no longer parsing | |
15106 | tentatively. */ | |
8d241e0b | 15107 | push_deferring_access_checks (dk_deferred); |
a723baf1 MM |
15108 | } |
15109 | ||
15110 | /* Commit to the currently active tentative parse. */ | |
15111 | ||
15112 | static void | |
94edc4ab | 15113 | cp_parser_commit_to_tentative_parse (cp_parser* parser) |
a723baf1 MM |
15114 | { |
15115 | cp_parser_context *context; | |
15116 | cp_lexer *lexer; | |
15117 | ||
15118 | /* Mark all of the levels as committed. */ | |
15119 | lexer = parser->lexer; | |
15120 | for (context = parser->context; context->next; context = context->next) | |
15121 | { | |
15122 | if (context->status == CP_PARSER_STATUS_KIND_COMMITTED) | |
15123 | break; | |
15124 | context->status = CP_PARSER_STATUS_KIND_COMMITTED; | |
15125 | while (!cp_lexer_saving_tokens (lexer)) | |
15126 | lexer = lexer->next; | |
15127 | cp_lexer_commit_tokens (lexer); | |
15128 | } | |
15129 | } | |
15130 | ||
15131 | /* Abort the currently active tentative parse. All consumed tokens | |
15132 | will be rolled back, and no diagnostics will be issued. */ | |
15133 | ||
15134 | static void | |
94edc4ab | 15135 | cp_parser_abort_tentative_parse (cp_parser* parser) |
a723baf1 MM |
15136 | { |
15137 | cp_parser_simulate_error (parser); | |
15138 | /* Now, pretend that we want to see if the construct was | |
15139 | successfully parsed. */ | |
15140 | cp_parser_parse_definitely (parser); | |
15141 | } | |
15142 | ||
34cd5ae7 | 15143 | /* Stop parsing tentatively. If a parse error has occurred, restore the |
a723baf1 MM |
15144 | token stream. Otherwise, commit to the tokens we have consumed. |
15145 | Returns true if no error occurred; false otherwise. */ | |
15146 | ||
15147 | static bool | |
94edc4ab | 15148 | cp_parser_parse_definitely (cp_parser* parser) |
a723baf1 MM |
15149 | { |
15150 | bool error_occurred; | |
15151 | cp_parser_context *context; | |
15152 | ||
34cd5ae7 | 15153 | /* Remember whether or not an error occurred, since we are about to |
a723baf1 MM |
15154 | destroy that information. */ |
15155 | error_occurred = cp_parser_error_occurred (parser); | |
15156 | /* Remove the topmost context from the stack. */ | |
15157 | context = parser->context; | |
15158 | parser->context = context->next; | |
15159 | /* If no parse errors occurred, commit to the tentative parse. */ | |
15160 | if (!error_occurred) | |
15161 | { | |
15162 | /* Commit to the tokens read tentatively, unless that was | |
15163 | already done. */ | |
15164 | if (context->status != CP_PARSER_STATUS_KIND_COMMITTED) | |
15165 | cp_lexer_commit_tokens (parser->lexer); | |
cf22909c KL |
15166 | |
15167 | pop_to_parent_deferring_access_checks (); | |
a723baf1 MM |
15168 | } |
15169 | /* Otherwise, if errors occurred, roll back our state so that things | |
15170 | are just as they were before we began the tentative parse. */ | |
15171 | else | |
cf22909c KL |
15172 | { |
15173 | cp_lexer_rollback_tokens (parser->lexer); | |
15174 | pop_deferring_access_checks (); | |
15175 | } | |
e5976695 MM |
15176 | /* Add the context to the front of the free list. */ |
15177 | context->next = cp_parser_context_free_list; | |
15178 | cp_parser_context_free_list = context; | |
15179 | ||
15180 | return !error_occurred; | |
a723baf1 MM |
15181 | } |
15182 | ||
a723baf1 MM |
15183 | /* Returns true if we are parsing tentatively -- but have decided that |
15184 | we will stick with this tentative parse, even if errors occur. */ | |
15185 | ||
15186 | static bool | |
94edc4ab | 15187 | cp_parser_committed_to_tentative_parse (cp_parser* parser) |
a723baf1 MM |
15188 | { |
15189 | return (cp_parser_parsing_tentatively (parser) | |
15190 | && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED); | |
15191 | } | |
15192 | ||
4de8668e | 15193 | /* Returns nonzero iff an error has occurred during the most recent |
a723baf1 MM |
15194 | tentative parse. */ |
15195 | ||
15196 | static bool | |
94edc4ab | 15197 | cp_parser_error_occurred (cp_parser* parser) |
a723baf1 MM |
15198 | { |
15199 | return (cp_parser_parsing_tentatively (parser) | |
15200 | && parser->context->status == CP_PARSER_STATUS_KIND_ERROR); | |
15201 | } | |
15202 | ||
4de8668e | 15203 | /* Returns nonzero if GNU extensions are allowed. */ |
a723baf1 MM |
15204 | |
15205 | static bool | |
94edc4ab | 15206 | cp_parser_allow_gnu_extensions_p (cp_parser* parser) |
a723baf1 MM |
15207 | { |
15208 | return parser->allow_gnu_extensions_p; | |
15209 | } | |
15210 | ||
15211 | \f | |
15212 | ||
15213 | /* The parser. */ | |
15214 | ||
15215 | static GTY (()) cp_parser *the_parser; | |
15216 | ||
15217 | /* External interface. */ | |
15218 | ||
d1bd0ded | 15219 | /* Parse one entire translation unit. */ |
a723baf1 | 15220 | |
d1bd0ded GK |
15221 | void |
15222 | c_parse_file (void) | |
a723baf1 MM |
15223 | { |
15224 | bool error_occurred; | |
15225 | ||
15226 | the_parser = cp_parser_new (); | |
78757caa KL |
15227 | push_deferring_access_checks (flag_access_control |
15228 | ? dk_no_deferred : dk_no_check); | |
a723baf1 MM |
15229 | error_occurred = cp_parser_translation_unit (the_parser); |
15230 | the_parser = NULL; | |
a723baf1 MM |
15231 | } |
15232 | ||
15233 | /* Clean up after parsing the entire translation unit. */ | |
15234 | ||
15235 | void | |
94edc4ab | 15236 | free_parser_stacks (void) |
a723baf1 MM |
15237 | { |
15238 | /* Nothing to do. */ | |
15239 | } | |
15240 | ||
15241 | /* This variable must be provided by every front end. */ | |
15242 | ||
15243 | int yydebug; | |
15244 | ||
15245 | #include "gt-cp-parser.h" |