]>
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; |
522df488 DN |
75 | /* The value associated with this token, if any. */ |
76 | tree value; | |
82a98427 NS |
77 | /* The location at which this token was found. */ |
78 | location_t location; | |
a723baf1 MM |
79 | } cp_token; |
80 | ||
522df488 DN |
81 | /* The number of tokens in a single token block. |
82 | Computed so that cp_token_block fits in a 512B allocation unit. */ | |
a723baf1 | 83 | |
522df488 | 84 | #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token)) |
a723baf1 MM |
85 | |
86 | /* A group of tokens. These groups are chained together to store | |
87 | large numbers of tokens. (For example, a token block is created | |
88 | when the body of an inline member function is first encountered; | |
89 | the tokens are processed later after the class definition is | |
90 | complete.) | |
91 | ||
92 | This somewhat ungainly data structure (as opposed to, say, a | |
34cd5ae7 | 93 | variable-length array), is used due to constraints imposed by the |
a723baf1 MM |
94 | current garbage-collection methodology. If it is made more |
95 | flexible, we could perhaps simplify the data structures involved. */ | |
96 | ||
97 | typedef struct cp_token_block GTY (()) | |
98 | { | |
99 | /* The tokens. */ | |
100 | cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS]; | |
101 | /* The number of tokens in this block. */ | |
102 | size_t num_tokens; | |
103 | /* The next token block in the chain. */ | |
104 | struct cp_token_block *next; | |
105 | /* The previous block in the chain. */ | |
106 | struct cp_token_block *prev; | |
107 | } cp_token_block; | |
108 | ||
109 | typedef struct cp_token_cache GTY (()) | |
110 | { | |
111 | /* The first block in the cache. NULL if there are no tokens in the | |
112 | cache. */ | |
113 | cp_token_block *first; | |
114 | /* The last block in the cache. NULL If there are no tokens in the | |
115 | cache. */ | |
116 | cp_token_block *last; | |
117 | } cp_token_cache; | |
118 | ||
9bcb9aae | 119 | /* Prototypes. */ |
a723baf1 MM |
120 | |
121 | static cp_token_cache *cp_token_cache_new | |
122 | (void); | |
123 | static void cp_token_cache_push_token | |
124 | (cp_token_cache *, cp_token *); | |
125 | ||
126 | /* Create a new cp_token_cache. */ | |
127 | ||
128 | static cp_token_cache * | |
bf9d3c27 | 129 | cp_token_cache_new (void) |
a723baf1 | 130 | { |
c68b0a84 | 131 | return ggc_alloc_cleared (sizeof (cp_token_cache)); |
a723baf1 MM |
132 | } |
133 | ||
134 | /* Add *TOKEN to *CACHE. */ | |
135 | ||
136 | static void | |
137 | cp_token_cache_push_token (cp_token_cache *cache, | |
138 | cp_token *token) | |
139 | { | |
140 | cp_token_block *b = cache->last; | |
141 | ||
142 | /* See if we need to allocate a new token block. */ | |
143 | if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS) | |
144 | { | |
c68b0a84 | 145 | b = ggc_alloc_cleared (sizeof (cp_token_block)); |
a723baf1 MM |
146 | b->prev = cache->last; |
147 | if (cache->last) | |
148 | { | |
149 | cache->last->next = b; | |
150 | cache->last = b; | |
151 | } | |
152 | else | |
153 | cache->first = cache->last = b; | |
154 | } | |
155 | /* Add this token to the current token block. */ | |
156 | b->tokens[b->num_tokens++] = *token; | |
157 | } | |
158 | ||
159 | /* The cp_lexer structure represents the C++ lexer. It is responsible | |
160 | for managing the token stream from the preprocessor and supplying | |
161 | it to the parser. */ | |
162 | ||
163 | typedef struct cp_lexer GTY (()) | |
164 | { | |
165 | /* The memory allocated for the buffer. Never NULL. */ | |
166 | cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer; | |
167 | /* A pointer just past the end of the memory allocated for the buffer. */ | |
168 | cp_token * GTY ((skip (""))) buffer_end; | |
169 | /* The first valid token in the buffer, or NULL if none. */ | |
170 | cp_token * GTY ((skip (""))) first_token; | |
171 | /* The next available token. If NEXT_TOKEN is NULL, then there are | |
172 | no more available tokens. */ | |
173 | cp_token * GTY ((skip (""))) next_token; | |
174 | /* A pointer just past the last available token. If FIRST_TOKEN is | |
175 | NULL, however, there are no available tokens, and then this | |
176 | location is simply the place in which the next token read will be | |
177 | placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full. | |
178 | When the LAST_TOKEN == BUFFER, then the last token is at the | |
179 | highest memory address in the BUFFER. */ | |
180 | cp_token * GTY ((skip (""))) last_token; | |
181 | ||
182 | /* A stack indicating positions at which cp_lexer_save_tokens was | |
183 | called. The top entry is the most recent position at which we | |
184 | began saving tokens. The entries are differences in token | |
185 | position between FIRST_TOKEN and the first saved token. | |
186 | ||
187 | If the stack is non-empty, we are saving tokens. When a token is | |
188 | consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN | |
189 | pointer will not. The token stream will be preserved so that it | |
190 | can be reexamined later. | |
191 | ||
192 | If the stack is empty, then we are not saving tokens. Whenever a | |
193 | token is consumed, the FIRST_TOKEN pointer will be moved, and the | |
194 | consumed token will be gone forever. */ | |
195 | varray_type saved_tokens; | |
196 | ||
197 | /* The STRING_CST tokens encountered while processing the current | |
198 | string literal. */ | |
199 | varray_type string_tokens; | |
200 | ||
201 | /* True if we should obtain more tokens from the preprocessor; false | |
202 | if we are processing a saved token cache. */ | |
203 | bool main_lexer_p; | |
204 | ||
205 | /* True if we should output debugging information. */ | |
206 | bool debugging_p; | |
207 | ||
208 | /* The next lexer in a linked list of lexers. */ | |
209 | struct cp_lexer *next; | |
210 | } cp_lexer; | |
211 | ||
212 | /* Prototypes. */ | |
213 | ||
17211ab5 | 214 | static cp_lexer *cp_lexer_new_main |
94edc4ab | 215 | (void); |
a723baf1 | 216 | static cp_lexer *cp_lexer_new_from_tokens |
94edc4ab | 217 | (struct cp_token_cache *); |
a723baf1 | 218 | static int cp_lexer_saving_tokens |
94edc4ab | 219 | (const cp_lexer *); |
a723baf1 | 220 | static cp_token *cp_lexer_next_token |
94edc4ab | 221 | (cp_lexer *, cp_token *); |
a668c6ad MM |
222 | static cp_token *cp_lexer_prev_token |
223 | (cp_lexer *, cp_token *); | |
94edc4ab NN |
224 | static ptrdiff_t cp_lexer_token_difference |
225 | (cp_lexer *, cp_token *, cp_token *); | |
a723baf1 | 226 | static cp_token *cp_lexer_read_token |
94edc4ab | 227 | (cp_lexer *); |
a723baf1 | 228 | static void cp_lexer_maybe_grow_buffer |
94edc4ab | 229 | (cp_lexer *); |
a723baf1 | 230 | static void cp_lexer_get_preprocessor_token |
94edc4ab | 231 | (cp_lexer *, cp_token *); |
a723baf1 | 232 | static cp_token *cp_lexer_peek_token |
94edc4ab | 233 | (cp_lexer *); |
a723baf1 | 234 | static cp_token *cp_lexer_peek_nth_token |
94edc4ab | 235 | (cp_lexer *, size_t); |
f7b5ecd9 | 236 | static inline bool cp_lexer_next_token_is |
94edc4ab | 237 | (cp_lexer *, enum cpp_ttype); |
a723baf1 | 238 | static bool cp_lexer_next_token_is_not |
94edc4ab | 239 | (cp_lexer *, enum cpp_ttype); |
a723baf1 | 240 | static bool cp_lexer_next_token_is_keyword |
94edc4ab NN |
241 | (cp_lexer *, enum rid); |
242 | static cp_token *cp_lexer_consume_token | |
243 | (cp_lexer *); | |
a723baf1 MM |
244 | static void cp_lexer_purge_token |
245 | (cp_lexer *); | |
246 | static void cp_lexer_purge_tokens_after | |
247 | (cp_lexer *, cp_token *); | |
248 | static void cp_lexer_save_tokens | |
94edc4ab | 249 | (cp_lexer *); |
a723baf1 | 250 | static void cp_lexer_commit_tokens |
94edc4ab | 251 | (cp_lexer *); |
a723baf1 | 252 | static void cp_lexer_rollback_tokens |
94edc4ab | 253 | (cp_lexer *); |
f7b5ecd9 | 254 | static inline void cp_lexer_set_source_position_from_token |
94edc4ab | 255 | (cp_lexer *, const cp_token *); |
a723baf1 | 256 | static void cp_lexer_print_token |
94edc4ab | 257 | (FILE *, cp_token *); |
f7b5ecd9 | 258 | static inline bool cp_lexer_debugging_p |
94edc4ab | 259 | (cp_lexer *); |
a723baf1 | 260 | static void cp_lexer_start_debugging |
94edc4ab | 261 | (cp_lexer *) ATTRIBUTE_UNUSED; |
a723baf1 | 262 | static void cp_lexer_stop_debugging |
94edc4ab | 263 | (cp_lexer *) ATTRIBUTE_UNUSED; |
a723baf1 MM |
264 | |
265 | /* Manifest constants. */ | |
266 | ||
267 | #define CP_TOKEN_BUFFER_SIZE 5 | |
268 | #define CP_SAVED_TOKENS_SIZE 5 | |
269 | ||
270 | /* A token type for keywords, as opposed to ordinary identifiers. */ | |
271 | #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1)) | |
272 | ||
273 | /* A token type for template-ids. If a template-id is processed while | |
274 | parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token; | |
275 | the value of the CPP_TEMPLATE_ID is whatever was returned by | |
276 | cp_parser_template_id. */ | |
277 | #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1)) | |
278 | ||
279 | /* A token type for nested-name-specifiers. If a | |
280 | nested-name-specifier is processed while parsing tentatively, it is | |
281 | replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the | |
282 | CPP_NESTED_NAME_SPECIFIER is whatever was returned by | |
283 | cp_parser_nested_name_specifier_opt. */ | |
284 | #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1)) | |
285 | ||
286 | /* A token type for tokens that are not tokens at all; these are used | |
287 | to mark the end of a token block. */ | |
288 | #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1) | |
289 | ||
290 | /* Variables. */ | |
291 | ||
292 | /* The stream to which debugging output should be written. */ | |
293 | static FILE *cp_lexer_debug_stream; | |
294 | ||
17211ab5 GK |
295 | /* Create a new main C++ lexer, the lexer that gets tokens from the |
296 | preprocessor. */ | |
a723baf1 MM |
297 | |
298 | static cp_lexer * | |
17211ab5 | 299 | cp_lexer_new_main (void) |
a723baf1 MM |
300 | { |
301 | cp_lexer *lexer; | |
17211ab5 GK |
302 | cp_token first_token; |
303 | ||
304 | /* It's possible that lexing the first token will load a PCH file, | |
305 | which is a GC collection point. So we have to grab the first | |
306 | token before allocating any memory. */ | |
307 | cp_lexer_get_preprocessor_token (NULL, &first_token); | |
18c81520 | 308 | c_common_no_more_pch (); |
a723baf1 MM |
309 | |
310 | /* Allocate the memory. */ | |
c68b0a84 | 311 | lexer = ggc_alloc_cleared (sizeof (cp_lexer)); |
a723baf1 MM |
312 | |
313 | /* Create the circular buffer. */ | |
c68b0a84 | 314 | lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token)); |
a723baf1 MM |
315 | lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE; |
316 | ||
17211ab5 GK |
317 | /* There is one token in the buffer. */ |
318 | lexer->last_token = lexer->buffer + 1; | |
319 | lexer->first_token = lexer->buffer; | |
320 | lexer->next_token = lexer->buffer; | |
321 | memcpy (lexer->buffer, &first_token, sizeof (cp_token)); | |
a723baf1 MM |
322 | |
323 | /* This lexer obtains more tokens by calling c_lex. */ | |
17211ab5 | 324 | lexer->main_lexer_p = true; |
a723baf1 MM |
325 | |
326 | /* Create the SAVED_TOKENS stack. */ | |
327 | VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens"); | |
328 | ||
329 | /* Create the STRINGS array. */ | |
330 | VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings"); | |
331 | ||
332 | /* Assume we are not debugging. */ | |
333 | lexer->debugging_p = false; | |
334 | ||
335 | return lexer; | |
336 | } | |
337 | ||
338 | /* Create a new lexer whose token stream is primed with the TOKENS. | |
339 | When these tokens are exhausted, no new tokens will be read. */ | |
340 | ||
341 | static cp_lexer * | |
342 | cp_lexer_new_from_tokens (cp_token_cache *tokens) | |
343 | { | |
344 | cp_lexer *lexer; | |
345 | cp_token *token; | |
346 | cp_token_block *block; | |
347 | ptrdiff_t num_tokens; | |
348 | ||
17211ab5 | 349 | /* Allocate the memory. */ |
c68b0a84 | 350 | lexer = ggc_alloc_cleared (sizeof (cp_lexer)); |
a723baf1 MM |
351 | |
352 | /* Create a new buffer, appropriately sized. */ | |
353 | num_tokens = 0; | |
354 | for (block = tokens->first; block != NULL; block = block->next) | |
355 | num_tokens += block->num_tokens; | |
c68b0a84 | 356 | lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token)); |
a723baf1 MM |
357 | lexer->buffer_end = lexer->buffer + num_tokens; |
358 | ||
359 | /* Install the tokens. */ | |
360 | token = lexer->buffer; | |
361 | for (block = tokens->first; block != NULL; block = block->next) | |
362 | { | |
363 | memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token)); | |
364 | token += block->num_tokens; | |
365 | } | |
366 | ||
367 | /* The FIRST_TOKEN is the beginning of the buffer. */ | |
368 | lexer->first_token = lexer->buffer; | |
369 | /* The next available token is also at the beginning of the buffer. */ | |
370 | lexer->next_token = lexer->buffer; | |
371 | /* The buffer is full. */ | |
372 | lexer->last_token = lexer->first_token; | |
373 | ||
17211ab5 GK |
374 | /* This lexer doesn't obtain more tokens. */ |
375 | lexer->main_lexer_p = false; | |
376 | ||
377 | /* Create the SAVED_TOKENS stack. */ | |
378 | VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens"); | |
379 | ||
380 | /* Create the STRINGS array. */ | |
381 | VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings"); | |
382 | ||
383 | /* Assume we are not debugging. */ | |
384 | lexer->debugging_p = false; | |
385 | ||
a723baf1 MM |
386 | return lexer; |
387 | } | |
388 | ||
4de8668e | 389 | /* Returns nonzero if debugging information should be output. */ |
a723baf1 | 390 | |
f7b5ecd9 MM |
391 | static inline bool |
392 | cp_lexer_debugging_p (cp_lexer *lexer) | |
a723baf1 | 393 | { |
f7b5ecd9 MM |
394 | return lexer->debugging_p; |
395 | } | |
396 | ||
397 | /* Set the current source position from the information stored in | |
398 | TOKEN. */ | |
399 | ||
400 | static inline void | |
94edc4ab NN |
401 | cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED , |
402 | const cp_token *token) | |
f7b5ecd9 MM |
403 | { |
404 | /* Ideally, the source position information would not be a global | |
405 | variable, but it is. */ | |
406 | ||
407 | /* Update the line number. */ | |
408 | if (token->type != CPP_EOF) | |
82a98427 | 409 | input_location = token->location; |
a723baf1 MM |
410 | } |
411 | ||
412 | /* TOKEN points into the circular token buffer. Return a pointer to | |
413 | the next token in the buffer. */ | |
414 | ||
f7b5ecd9 | 415 | static inline cp_token * |
94edc4ab | 416 | cp_lexer_next_token (cp_lexer* lexer, cp_token* token) |
a723baf1 MM |
417 | { |
418 | token++; | |
419 | if (token == lexer->buffer_end) | |
420 | token = lexer->buffer; | |
421 | return token; | |
422 | } | |
423 | ||
a668c6ad MM |
424 | /* TOKEN points into the circular token buffer. Return a pointer to |
425 | the previous token in the buffer. */ | |
426 | ||
427 | static inline cp_token * | |
428 | cp_lexer_prev_token (cp_lexer* lexer, cp_token* token) | |
429 | { | |
430 | if (token == lexer->buffer) | |
431 | token = lexer->buffer_end; | |
432 | return token - 1; | |
433 | } | |
434 | ||
4de8668e | 435 | /* nonzero if we are presently saving tokens. */ |
f7b5ecd9 MM |
436 | |
437 | static int | |
94edc4ab | 438 | cp_lexer_saving_tokens (const cp_lexer* lexer) |
f7b5ecd9 MM |
439 | { |
440 | return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0; | |
441 | } | |
442 | ||
a723baf1 MM |
443 | /* Return a pointer to the token that is N tokens beyond TOKEN in the |
444 | buffer. */ | |
445 | ||
446 | static cp_token * | |
447 | cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n) | |
448 | { | |
449 | token += n; | |
450 | if (token >= lexer->buffer_end) | |
451 | token = lexer->buffer + (token - lexer->buffer_end); | |
452 | return token; | |
453 | } | |
454 | ||
455 | /* Returns the number of times that START would have to be incremented | |
456 | to reach FINISH. If START and FINISH are the same, returns zero. */ | |
457 | ||
458 | static ptrdiff_t | |
94edc4ab | 459 | cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish) |
a723baf1 MM |
460 | { |
461 | if (finish >= start) | |
462 | return finish - start; | |
463 | else | |
464 | return ((lexer->buffer_end - lexer->buffer) | |
465 | - (start - finish)); | |
466 | } | |
467 | ||
468 | /* Obtain another token from the C preprocessor and add it to the | |
469 | token buffer. Returns the newly read token. */ | |
470 | ||
471 | static cp_token * | |
94edc4ab | 472 | cp_lexer_read_token (cp_lexer* lexer) |
a723baf1 MM |
473 | { |
474 | cp_token *token; | |
475 | ||
476 | /* Make sure there is room in the buffer. */ | |
477 | cp_lexer_maybe_grow_buffer (lexer); | |
478 | ||
479 | /* If there weren't any tokens, then this one will be the first. */ | |
480 | if (!lexer->first_token) | |
481 | lexer->first_token = lexer->last_token; | |
482 | /* Similarly, if there were no available tokens, there is one now. */ | |
483 | if (!lexer->next_token) | |
484 | lexer->next_token = lexer->last_token; | |
485 | ||
486 | /* Figure out where we're going to store the new token. */ | |
487 | token = lexer->last_token; | |
488 | ||
489 | /* Get a new token from the preprocessor. */ | |
490 | cp_lexer_get_preprocessor_token (lexer, token); | |
491 | ||
492 | /* Increment LAST_TOKEN. */ | |
493 | lexer->last_token = cp_lexer_next_token (lexer, token); | |
494 | ||
e6cc3a24 ZW |
495 | /* Strings should have type `const char []'. Right now, we will |
496 | have an ARRAY_TYPE that is constant rather than an array of | |
497 | constant elements. | |
498 | FIXME: Make fix_string_type get this right in the first place. */ | |
499 | if ((token->type == CPP_STRING || token->type == CPP_WSTRING) | |
500 | && flag_const_strings) | |
a723baf1 | 501 | { |
e6cc3a24 ZW |
502 | tree type; |
503 | ||
504 | /* Get the current type. It will be an ARRAY_TYPE. */ | |
505 | type = TREE_TYPE (token->value); | |
506 | /* Use build_cplus_array_type to rebuild the array, thereby | |
507 | getting the right type. */ | |
508 | type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type)); | |
509 | /* Reset the type of the token. */ | |
510 | TREE_TYPE (token->value) = type; | |
a723baf1 MM |
511 | } |
512 | ||
513 | return token; | |
514 | } | |
515 | ||
516 | /* If the circular buffer is full, make it bigger. */ | |
517 | ||
518 | static void | |
94edc4ab | 519 | cp_lexer_maybe_grow_buffer (cp_lexer* lexer) |
a723baf1 MM |
520 | { |
521 | /* If the buffer is full, enlarge it. */ | |
522 | if (lexer->last_token == lexer->first_token) | |
523 | { | |
524 | cp_token *new_buffer; | |
525 | cp_token *old_buffer; | |
526 | cp_token *new_first_token; | |
527 | ptrdiff_t buffer_length; | |
528 | size_t num_tokens_to_copy; | |
529 | ||
530 | /* Remember the current buffer pointer. It will become invalid, | |
531 | but we will need to do pointer arithmetic involving this | |
532 | value. */ | |
533 | old_buffer = lexer->buffer; | |
534 | /* Compute the current buffer size. */ | |
535 | buffer_length = lexer->buffer_end - lexer->buffer; | |
536 | /* Allocate a buffer twice as big. */ | |
c68b0a84 KG |
537 | new_buffer = ggc_realloc (lexer->buffer, |
538 | 2 * buffer_length * sizeof (cp_token)); | |
a723baf1 MM |
539 | |
540 | /* Because the buffer is circular, logically consecutive tokens | |
541 | are not necessarily placed consecutively in memory. | |
542 | Therefore, we must keep move the tokens that were before | |
543 | FIRST_TOKEN to the second half of the newly allocated | |
544 | buffer. */ | |
545 | num_tokens_to_copy = (lexer->first_token - old_buffer); | |
546 | memcpy (new_buffer + buffer_length, | |
547 | new_buffer, | |
548 | num_tokens_to_copy * sizeof (cp_token)); | |
549 | /* Clear the rest of the buffer. We never look at this storage, | |
550 | but the garbage collector may. */ | |
551 | memset (new_buffer + buffer_length + num_tokens_to_copy, 0, | |
552 | (buffer_length - num_tokens_to_copy) * sizeof (cp_token)); | |
553 | ||
554 | /* Now recompute all of the buffer pointers. */ | |
555 | new_first_token | |
556 | = new_buffer + (lexer->first_token - old_buffer); | |
557 | if (lexer->next_token != NULL) | |
558 | { | |
559 | ptrdiff_t next_token_delta; | |
560 | ||
561 | if (lexer->next_token > lexer->first_token) | |
562 | next_token_delta = lexer->next_token - lexer->first_token; | |
563 | else | |
564 | next_token_delta = | |
565 | buffer_length - (lexer->first_token - lexer->next_token); | |
566 | lexer->next_token = new_first_token + next_token_delta; | |
567 | } | |
568 | lexer->last_token = new_first_token + buffer_length; | |
569 | lexer->buffer = new_buffer; | |
570 | lexer->buffer_end = new_buffer + buffer_length * 2; | |
571 | lexer->first_token = new_first_token; | |
572 | } | |
573 | } | |
574 | ||
575 | /* Store the next token from the preprocessor in *TOKEN. */ | |
576 | ||
577 | static void | |
94edc4ab NN |
578 | cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED , |
579 | cp_token *token) | |
a723baf1 MM |
580 | { |
581 | bool done; | |
582 | ||
583 | /* If this not the main lexer, return a terminating CPP_EOF token. */ | |
17211ab5 | 584 | if (lexer != NULL && !lexer->main_lexer_p) |
a723baf1 MM |
585 | { |
586 | token->type = CPP_EOF; | |
82a98427 NS |
587 | token->location.line = 0; |
588 | token->location.file = NULL; | |
a723baf1 MM |
589 | token->value = NULL_TREE; |
590 | token->keyword = RID_MAX; | |
591 | ||
592 | return; | |
593 | } | |
594 | ||
595 | done = false; | |
596 | /* Keep going until we get a token we like. */ | |
597 | while (!done) | |
598 | { | |
599 | /* Get a new token from the preprocessor. */ | |
600 | token->type = c_lex (&token->value); | |
601 | /* Issue messages about tokens we cannot process. */ | |
602 | switch (token->type) | |
603 | { | |
604 | case CPP_ATSIGN: | |
605 | case CPP_HASH: | |
606 | case CPP_PASTE: | |
607 | error ("invalid token"); | |
608 | break; | |
609 | ||
a723baf1 MM |
610 | default: |
611 | /* This is a good token, so we exit the loop. */ | |
612 | done = true; | |
613 | break; | |
614 | } | |
615 | } | |
616 | /* Now we've got our token. */ | |
82a98427 | 617 | token->location = input_location; |
a723baf1 MM |
618 | |
619 | /* Check to see if this token is a keyword. */ | |
620 | if (token->type == CPP_NAME | |
621 | && C_IS_RESERVED_WORD (token->value)) | |
622 | { | |
623 | /* Mark this token as a keyword. */ | |
624 | token->type = CPP_KEYWORD; | |
625 | /* Record which keyword. */ | |
626 | token->keyword = C_RID_CODE (token->value); | |
627 | /* Update the value. Some keywords are mapped to particular | |
628 | entities, rather than simply having the value of the | |
629 | corresponding IDENTIFIER_NODE. For example, `__const' is | |
630 | mapped to `const'. */ | |
631 | token->value = ridpointers[token->keyword]; | |
632 | } | |
633 | else | |
634 | token->keyword = RID_MAX; | |
635 | } | |
636 | ||
637 | /* Return a pointer to the next token in the token stream, but do not | |
638 | consume it. */ | |
639 | ||
640 | static cp_token * | |
94edc4ab | 641 | cp_lexer_peek_token (cp_lexer* lexer) |
a723baf1 MM |
642 | { |
643 | cp_token *token; | |
644 | ||
645 | /* If there are no tokens, read one now. */ | |
646 | if (!lexer->next_token) | |
647 | cp_lexer_read_token (lexer); | |
648 | ||
649 | /* Provide debugging output. */ | |
650 | if (cp_lexer_debugging_p (lexer)) | |
651 | { | |
652 | fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: "); | |
653 | cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token); | |
654 | fprintf (cp_lexer_debug_stream, "\n"); | |
655 | } | |
656 | ||
657 | token = lexer->next_token; | |
658 | cp_lexer_set_source_position_from_token (lexer, token); | |
659 | return token; | |
660 | } | |
661 | ||
662 | /* Return true if the next token has the indicated TYPE. */ | |
663 | ||
664 | static bool | |
94edc4ab | 665 | cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type) |
a723baf1 MM |
666 | { |
667 | cp_token *token; | |
668 | ||
669 | /* Peek at the next token. */ | |
670 | token = cp_lexer_peek_token (lexer); | |
671 | /* Check to see if it has the indicated TYPE. */ | |
672 | return token->type == type; | |
673 | } | |
674 | ||
675 | /* Return true if the next token does not have the indicated TYPE. */ | |
676 | ||
677 | static bool | |
94edc4ab | 678 | cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type) |
a723baf1 MM |
679 | { |
680 | return !cp_lexer_next_token_is (lexer, type); | |
681 | } | |
682 | ||
683 | /* Return true if the next token is the indicated KEYWORD. */ | |
684 | ||
685 | static bool | |
94edc4ab | 686 | cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword) |
a723baf1 MM |
687 | { |
688 | cp_token *token; | |
689 | ||
690 | /* Peek at the next token. */ | |
691 | token = cp_lexer_peek_token (lexer); | |
692 | /* Check to see if it is the indicated keyword. */ | |
693 | return token->keyword == keyword; | |
694 | } | |
695 | ||
696 | /* Return a pointer to the Nth token in the token stream. If N is 1, | |
697 | then this is precisely equivalent to cp_lexer_peek_token. */ | |
698 | ||
699 | static cp_token * | |
94edc4ab | 700 | cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n) |
a723baf1 MM |
701 | { |
702 | cp_token *token; | |
703 | ||
704 | /* N is 1-based, not zero-based. */ | |
705 | my_friendly_assert (n > 0, 20000224); | |
706 | ||
707 | /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */ | |
708 | token = lexer->next_token; | |
709 | /* If there are no tokens in the buffer, get one now. */ | |
710 | if (!token) | |
711 | { | |
712 | cp_lexer_read_token (lexer); | |
713 | token = lexer->next_token; | |
714 | } | |
715 | ||
716 | /* Now, read tokens until we have enough. */ | |
717 | while (--n > 0) | |
718 | { | |
719 | /* Advance to the next token. */ | |
720 | token = cp_lexer_next_token (lexer, token); | |
721 | /* If that's all the tokens we have, read a new one. */ | |
722 | if (token == lexer->last_token) | |
723 | token = cp_lexer_read_token (lexer); | |
724 | } | |
725 | ||
726 | return token; | |
727 | } | |
728 | ||
729 | /* Consume the next token. The pointer returned is valid only until | |
730 | another token is read. Callers should preserve copy the token | |
731 | explicitly if they will need its value for a longer period of | |
732 | time. */ | |
733 | ||
734 | static cp_token * | |
94edc4ab | 735 | cp_lexer_consume_token (cp_lexer* lexer) |
a723baf1 MM |
736 | { |
737 | cp_token *token; | |
738 | ||
739 | /* If there are no tokens, read one now. */ | |
740 | if (!lexer->next_token) | |
741 | cp_lexer_read_token (lexer); | |
742 | ||
743 | /* Remember the token we'll be returning. */ | |
744 | token = lexer->next_token; | |
745 | ||
746 | /* Increment NEXT_TOKEN. */ | |
747 | lexer->next_token = cp_lexer_next_token (lexer, | |
748 | lexer->next_token); | |
749 | /* Check to see if we're all out of tokens. */ | |
750 | if (lexer->next_token == lexer->last_token) | |
751 | lexer->next_token = NULL; | |
752 | ||
753 | /* If we're not saving tokens, then move FIRST_TOKEN too. */ | |
754 | if (!cp_lexer_saving_tokens (lexer)) | |
755 | { | |
756 | /* If there are no tokens available, set FIRST_TOKEN to NULL. */ | |
757 | if (!lexer->next_token) | |
758 | lexer->first_token = NULL; | |
759 | else | |
760 | lexer->first_token = lexer->next_token; | |
761 | } | |
762 | ||
763 | /* Provide debugging output. */ | |
764 | if (cp_lexer_debugging_p (lexer)) | |
765 | { | |
766 | fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: "); | |
767 | cp_lexer_print_token (cp_lexer_debug_stream, token); | |
768 | fprintf (cp_lexer_debug_stream, "\n"); | |
769 | } | |
770 | ||
771 | return token; | |
772 | } | |
773 | ||
774 | /* Permanently remove the next token from the token stream. There | |
775 | must be a valid next token already; this token never reads | |
776 | additional tokens from the preprocessor. */ | |
777 | ||
778 | static void | |
779 | cp_lexer_purge_token (cp_lexer *lexer) | |
780 | { | |
781 | cp_token *token; | |
782 | cp_token *next_token; | |
783 | ||
784 | token = lexer->next_token; | |
785 | while (true) | |
786 | { | |
787 | next_token = cp_lexer_next_token (lexer, token); | |
788 | if (next_token == lexer->last_token) | |
789 | break; | |
790 | *token = *next_token; | |
791 | token = next_token; | |
792 | } | |
793 | ||
794 | lexer->last_token = token; | |
795 | /* The token purged may have been the only token remaining; if so, | |
796 | clear NEXT_TOKEN. */ | |
797 | if (lexer->next_token == token) | |
798 | lexer->next_token = NULL; | |
799 | } | |
800 | ||
801 | /* Permanently remove all tokens after TOKEN, up to, but not | |
802 | including, the token that will be returned next by | |
803 | cp_lexer_peek_token. */ | |
804 | ||
805 | static void | |
806 | cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token) | |
807 | { | |
808 | cp_token *peek; | |
809 | cp_token *t1; | |
810 | cp_token *t2; | |
811 | ||
812 | if (lexer->next_token) | |
813 | { | |
814 | /* Copy the tokens that have not yet been read to the location | |
815 | immediately following TOKEN. */ | |
816 | t1 = cp_lexer_next_token (lexer, token); | |
817 | t2 = peek = cp_lexer_peek_token (lexer); | |
818 | /* Move tokens into the vacant area between TOKEN and PEEK. */ | |
819 | while (t2 != lexer->last_token) | |
820 | { | |
821 | *t1 = *t2; | |
822 | t1 = cp_lexer_next_token (lexer, t1); | |
823 | t2 = cp_lexer_next_token (lexer, t2); | |
824 | } | |
825 | /* Now, the next available token is right after TOKEN. */ | |
826 | lexer->next_token = cp_lexer_next_token (lexer, token); | |
827 | /* And the last token is wherever we ended up. */ | |
828 | lexer->last_token = t1; | |
829 | } | |
830 | else | |
831 | { | |
832 | /* There are no tokens in the buffer, so there is nothing to | |
833 | copy. The last token in the buffer is TOKEN itself. */ | |
834 | lexer->last_token = cp_lexer_next_token (lexer, token); | |
835 | } | |
836 | } | |
837 | ||
838 | /* Begin saving tokens. All tokens consumed after this point will be | |
839 | preserved. */ | |
840 | ||
841 | static void | |
94edc4ab | 842 | cp_lexer_save_tokens (cp_lexer* lexer) |
a723baf1 MM |
843 | { |
844 | /* Provide debugging output. */ | |
845 | if (cp_lexer_debugging_p (lexer)) | |
846 | fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n"); | |
847 | ||
848 | /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can | |
849 | restore the tokens if required. */ | |
850 | if (!lexer->next_token) | |
851 | cp_lexer_read_token (lexer); | |
852 | ||
853 | VARRAY_PUSH_INT (lexer->saved_tokens, | |
854 | cp_lexer_token_difference (lexer, | |
855 | lexer->first_token, | |
856 | lexer->next_token)); | |
857 | } | |
858 | ||
859 | /* Commit to the portion of the token stream most recently saved. */ | |
860 | ||
861 | static void | |
94edc4ab | 862 | cp_lexer_commit_tokens (cp_lexer* lexer) |
a723baf1 MM |
863 | { |
864 | /* Provide debugging output. */ | |
865 | if (cp_lexer_debugging_p (lexer)) | |
866 | fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n"); | |
867 | ||
868 | VARRAY_POP (lexer->saved_tokens); | |
869 | } | |
870 | ||
871 | /* Return all tokens saved since the last call to cp_lexer_save_tokens | |
872 | to the token stream. Stop saving tokens. */ | |
873 | ||
874 | static void | |
94edc4ab | 875 | cp_lexer_rollback_tokens (cp_lexer* lexer) |
a723baf1 MM |
876 | { |
877 | size_t delta; | |
878 | ||
879 | /* Provide debugging output. */ | |
880 | if (cp_lexer_debugging_p (lexer)) | |
881 | fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n"); | |
882 | ||
883 | /* Find the token that was the NEXT_TOKEN when we started saving | |
884 | tokens. */ | |
885 | delta = VARRAY_TOP_INT(lexer->saved_tokens); | |
886 | /* Make it the next token again now. */ | |
887 | lexer->next_token = cp_lexer_advance_token (lexer, | |
888 | lexer->first_token, | |
889 | delta); | |
15d2cb19 | 890 | /* It might be the case that there were no tokens when we started |
a723baf1 MM |
891 | saving tokens, but that there are some tokens now. */ |
892 | if (!lexer->next_token && lexer->first_token) | |
893 | lexer->next_token = lexer->first_token; | |
894 | ||
895 | /* Stop saving tokens. */ | |
896 | VARRAY_POP (lexer->saved_tokens); | |
897 | } | |
898 | ||
a723baf1 MM |
899 | /* Print a representation of the TOKEN on the STREAM. */ |
900 | ||
901 | static void | |
94edc4ab | 902 | cp_lexer_print_token (FILE * stream, cp_token* token) |
a723baf1 MM |
903 | { |
904 | const char *token_type = NULL; | |
905 | ||
906 | /* Figure out what kind of token this is. */ | |
907 | switch (token->type) | |
908 | { | |
909 | case CPP_EQ: | |
910 | token_type = "EQ"; | |
911 | break; | |
912 | ||
913 | case CPP_COMMA: | |
914 | token_type = "COMMA"; | |
915 | break; | |
916 | ||
917 | case CPP_OPEN_PAREN: | |
918 | token_type = "OPEN_PAREN"; | |
919 | break; | |
920 | ||
921 | case CPP_CLOSE_PAREN: | |
922 | token_type = "CLOSE_PAREN"; | |
923 | break; | |
924 | ||
925 | case CPP_OPEN_BRACE: | |
926 | token_type = "OPEN_BRACE"; | |
927 | break; | |
928 | ||
929 | case CPP_CLOSE_BRACE: | |
930 | token_type = "CLOSE_BRACE"; | |
931 | break; | |
932 | ||
933 | case CPP_SEMICOLON: | |
934 | token_type = "SEMICOLON"; | |
935 | break; | |
936 | ||
937 | case CPP_NAME: | |
938 | token_type = "NAME"; | |
939 | break; | |
940 | ||
941 | case CPP_EOF: | |
942 | token_type = "EOF"; | |
943 | break; | |
944 | ||
945 | case CPP_KEYWORD: | |
946 | token_type = "keyword"; | |
947 | break; | |
948 | ||
949 | /* This is not a token that we know how to handle yet. */ | |
950 | default: | |
951 | break; | |
952 | } | |
953 | ||
954 | /* If we have a name for the token, print it out. Otherwise, we | |
955 | simply give the numeric code. */ | |
956 | if (token_type) | |
957 | fprintf (stream, "%s", token_type); | |
958 | else | |
959 | fprintf (stream, "%d", token->type); | |
960 | /* And, for an identifier, print the identifier name. */ | |
961 | if (token->type == CPP_NAME | |
962 | /* Some keywords have a value that is not an IDENTIFIER_NODE. | |
963 | For example, `struct' is mapped to an INTEGER_CST. */ | |
964 | || (token->type == CPP_KEYWORD | |
965 | && TREE_CODE (token->value) == IDENTIFIER_NODE)) | |
966 | fprintf (stream, " %s", IDENTIFIER_POINTER (token->value)); | |
967 | } | |
968 | ||
a723baf1 MM |
969 | /* Start emitting debugging information. */ |
970 | ||
971 | static void | |
94edc4ab | 972 | cp_lexer_start_debugging (cp_lexer* lexer) |
a723baf1 MM |
973 | { |
974 | ++lexer->debugging_p; | |
975 | } | |
976 | ||
977 | /* Stop emitting debugging information. */ | |
978 | ||
979 | static void | |
94edc4ab | 980 | cp_lexer_stop_debugging (cp_lexer* lexer) |
a723baf1 MM |
981 | { |
982 | --lexer->debugging_p; | |
983 | } | |
984 | ||
985 | \f | |
986 | /* The parser. */ | |
987 | ||
988 | /* Overview | |
989 | -------- | |
990 | ||
991 | A cp_parser parses the token stream as specified by the C++ | |
992 | grammar. Its job is purely parsing, not semantic analysis. For | |
993 | example, the parser breaks the token stream into declarators, | |
994 | expressions, statements, and other similar syntactic constructs. | |
995 | It does not check that the types of the expressions on either side | |
996 | of an assignment-statement are compatible, or that a function is | |
997 | not declared with a parameter of type `void'. | |
998 | ||
999 | The parser invokes routines elsewhere in the compiler to perform | |
1000 | semantic analysis and to build up the abstract syntax tree for the | |
1001 | code processed. | |
1002 | ||
1003 | The parser (and the template instantiation code, which is, in a | |
1004 | way, a close relative of parsing) are the only parts of the | |
1005 | compiler that should be calling push_scope and pop_scope, or | |
1006 | related functions. The parser (and template instantiation code) | |
1007 | keeps track of what scope is presently active; everything else | |
1008 | should simply honor that. (The code that generates static | |
1009 | initializers may also need to set the scope, in order to check | |
1010 | access control correctly when emitting the initializers.) | |
1011 | ||
1012 | Methodology | |
1013 | ----------- | |
1014 | ||
1015 | The parser is of the standard recursive-descent variety. Upcoming | |
1016 | tokens in the token stream are examined in order to determine which | |
1017 | production to use when parsing a non-terminal. Some C++ constructs | |
1018 | require arbitrary look ahead to disambiguate. For example, it is | |
1019 | impossible, in the general case, to tell whether a statement is an | |
1020 | expression or declaration without scanning the entire statement. | |
1021 | Therefore, the parser is capable of "parsing tentatively." When the | |
1022 | parser is not sure what construct comes next, it enters this mode. | |
1023 | Then, while we attempt to parse the construct, the parser queues up | |
1024 | error messages, rather than issuing them immediately, and saves the | |
1025 | tokens it consumes. If the construct is parsed successfully, the | |
1026 | parser "commits", i.e., it issues any queued error messages and | |
1027 | the tokens that were being preserved are permanently discarded. | |
1028 | If, however, the construct is not parsed successfully, the parser | |
1029 | rolls back its state completely so that it can resume parsing using | |
1030 | a different alternative. | |
1031 | ||
1032 | Future Improvements | |
1033 | ------------------- | |
1034 | ||
1035 | The performance of the parser could probably be improved | |
1036 | substantially. Some possible improvements include: | |
1037 | ||
1038 | - The expression parser recurses through the various levels of | |
1039 | precedence as specified in the grammar, rather than using an | |
1040 | operator-precedence technique. Therefore, parsing a simple | |
1041 | identifier requires multiple recursive calls. | |
1042 | ||
1043 | - We could often eliminate the need to parse tentatively by | |
1044 | looking ahead a little bit. In some places, this approach | |
1045 | might not entirely eliminate the need to parse tentatively, but | |
1046 | it might still speed up the average case. */ | |
1047 | ||
1048 | /* Flags that are passed to some parsing functions. These values can | |
1049 | be bitwise-ored together. */ | |
1050 | ||
1051 | typedef enum cp_parser_flags | |
1052 | { | |
1053 | /* No flags. */ | |
1054 | CP_PARSER_FLAGS_NONE = 0x0, | |
1055 | /* The construct is optional. If it is not present, then no error | |
1056 | should be issued. */ | |
1057 | CP_PARSER_FLAGS_OPTIONAL = 0x1, | |
1058 | /* When parsing a type-specifier, do not allow user-defined types. */ | |
1059 | CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2 | |
1060 | } cp_parser_flags; | |
1061 | ||
62b8a44e NS |
1062 | /* The different kinds of declarators we want to parse. */ |
1063 | ||
1064 | typedef enum cp_parser_declarator_kind | |
1065 | { | |
9bcb9aae | 1066 | /* We want an abstract declartor. */ |
62b8a44e NS |
1067 | CP_PARSER_DECLARATOR_ABSTRACT, |
1068 | /* We want a named declarator. */ | |
1069 | CP_PARSER_DECLARATOR_NAMED, | |
04c06002 | 1070 | /* We don't mind, but the name must be an unqualified-id. */ |
62b8a44e NS |
1071 | CP_PARSER_DECLARATOR_EITHER |
1072 | } cp_parser_declarator_kind; | |
1073 | ||
a723baf1 MM |
1074 | /* A mapping from a token type to a corresponding tree node type. */ |
1075 | ||
1076 | typedef struct cp_parser_token_tree_map_node | |
1077 | { | |
1078 | /* The token type. */ | |
df2b750f | 1079 | ENUM_BITFIELD (cpp_ttype) token_type : 8; |
a723baf1 | 1080 | /* The corresponding tree code. */ |
df2b750f | 1081 | ENUM_BITFIELD (tree_code) tree_type : 8; |
a723baf1 MM |
1082 | } cp_parser_token_tree_map_node; |
1083 | ||
1084 | /* A complete map consists of several ordinary entries, followed by a | |
1085 | terminator. The terminating entry has a token_type of CPP_EOF. */ | |
1086 | ||
1087 | typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[]; | |
1088 | ||
1089 | /* The status of a tentative parse. */ | |
1090 | ||
1091 | typedef enum cp_parser_status_kind | |
1092 | { | |
1093 | /* No errors have occurred. */ | |
1094 | CP_PARSER_STATUS_KIND_NO_ERROR, | |
1095 | /* An error has occurred. */ | |
1096 | CP_PARSER_STATUS_KIND_ERROR, | |
1097 | /* We are committed to this tentative parse, whether or not an error | |
1098 | has occurred. */ | |
1099 | CP_PARSER_STATUS_KIND_COMMITTED | |
1100 | } cp_parser_status_kind; | |
1101 | ||
1102 | /* Context that is saved and restored when parsing tentatively. */ | |
1103 | ||
1104 | typedef struct cp_parser_context GTY (()) | |
1105 | { | |
1106 | /* If this is a tentative parsing context, the status of the | |
1107 | tentative parse. */ | |
1108 | enum cp_parser_status_kind status; | |
1109 | /* If non-NULL, we have just seen a `x->' or `x.' expression. Names | |
1110 | that are looked up in this context must be looked up both in the | |
1111 | scope given by OBJECT_TYPE (the type of `x' or `*x') and also in | |
1112 | the context of the containing expression. */ | |
1113 | tree object_type; | |
a723baf1 MM |
1114 | /* The next parsing context in the stack. */ |
1115 | struct cp_parser_context *next; | |
1116 | } cp_parser_context; | |
1117 | ||
1118 | /* Prototypes. */ | |
1119 | ||
1120 | /* Constructors and destructors. */ | |
1121 | ||
1122 | static cp_parser_context *cp_parser_context_new | |
94edc4ab | 1123 | (cp_parser_context *); |
a723baf1 | 1124 | |
e5976695 MM |
1125 | /* Class variables. */ |
1126 | ||
92bc1323 | 1127 | static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list; |
e5976695 | 1128 | |
a723baf1 MM |
1129 | /* Constructors and destructors. */ |
1130 | ||
1131 | /* Construct a new context. The context below this one on the stack | |
1132 | is given by NEXT. */ | |
1133 | ||
1134 | static cp_parser_context * | |
94edc4ab | 1135 | cp_parser_context_new (cp_parser_context* next) |
a723baf1 MM |
1136 | { |
1137 | cp_parser_context *context; | |
1138 | ||
1139 | /* Allocate the storage. */ | |
e5976695 MM |
1140 | if (cp_parser_context_free_list != NULL) |
1141 | { | |
1142 | /* Pull the first entry from the free list. */ | |
1143 | context = cp_parser_context_free_list; | |
1144 | cp_parser_context_free_list = context->next; | |
c68b0a84 | 1145 | memset (context, 0, sizeof (*context)); |
e5976695 MM |
1146 | } |
1147 | else | |
c68b0a84 | 1148 | context = ggc_alloc_cleared (sizeof (cp_parser_context)); |
a723baf1 MM |
1149 | /* No errors have occurred yet in this context. */ |
1150 | context->status = CP_PARSER_STATUS_KIND_NO_ERROR; | |
1151 | /* If this is not the bottomost context, copy information that we | |
1152 | need from the previous context. */ | |
1153 | if (next) | |
1154 | { | |
1155 | /* If, in the NEXT context, we are parsing an `x->' or `x.' | |
1156 | expression, then we are parsing one in this context, too. */ | |
1157 | context->object_type = next->object_type; | |
a723baf1 MM |
1158 | /* Thread the stack. */ |
1159 | context->next = next; | |
1160 | } | |
1161 | ||
1162 | return context; | |
1163 | } | |
1164 | ||
1165 | /* The cp_parser structure represents the C++ parser. */ | |
1166 | ||
1167 | typedef struct cp_parser GTY(()) | |
1168 | { | |
1169 | /* The lexer from which we are obtaining tokens. */ | |
1170 | cp_lexer *lexer; | |
1171 | ||
1172 | /* The scope in which names should be looked up. If NULL_TREE, then | |
1173 | we look up names in the scope that is currently open in the | |
1174 | source program. If non-NULL, this is either a TYPE or | |
1175 | NAMESPACE_DECL for the scope in which we should look. | |
1176 | ||
1177 | This value is not cleared automatically after a name is looked | |
1178 | up, so we must be careful to clear it before starting a new look | |
1179 | up sequence. (If it is not cleared, then `X::Y' followed by `Z' | |
1180 | will look up `Z' in the scope of `X', rather than the current | |
1181 | scope.) Unfortunately, it is difficult to tell when name lookup | |
1182 | is complete, because we sometimes peek at a token, look it up, | |
1183 | and then decide not to consume it. */ | |
1184 | tree scope; | |
1185 | ||
1186 | /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the | |
1187 | last lookup took place. OBJECT_SCOPE is used if an expression | |
1188 | like "x->y" or "x.y" was used; it gives the type of "*x" or "x", | |
1189 | respectively. QUALIFYING_SCOPE is used for an expression of the | |
1190 | form "X::Y"; it refers to X. */ | |
1191 | tree object_scope; | |
1192 | tree qualifying_scope; | |
1193 | ||
1194 | /* A stack of parsing contexts. All but the bottom entry on the | |
1195 | stack will be tentative contexts. | |
1196 | ||
1197 | We parse tentatively in order to determine which construct is in | |
1198 | use in some situations. For example, in order to determine | |
1199 | whether a statement is an expression-statement or a | |
1200 | declaration-statement we parse it tentatively as a | |
1201 | declaration-statement. If that fails, we then reparse the same | |
1202 | token stream as an expression-statement. */ | |
1203 | cp_parser_context *context; | |
1204 | ||
1205 | /* True if we are parsing GNU C++. If this flag is not set, then | |
1206 | GNU extensions are not recognized. */ | |
1207 | bool allow_gnu_extensions_p; | |
1208 | ||
1209 | /* TRUE if the `>' token should be interpreted as the greater-than | |
1210 | operator. FALSE if it is the end of a template-id or | |
1211 | template-parameter-list. */ | |
1212 | bool greater_than_is_operator_p; | |
1213 | ||
1214 | /* TRUE if default arguments are allowed within a parameter list | |
1215 | that starts at this point. FALSE if only a gnu extension makes | |
cd0be382 | 1216 | them permissible. */ |
a723baf1 MM |
1217 | bool default_arg_ok_p; |
1218 | ||
1219 | /* TRUE if we are parsing an integral constant-expression. See | |
1220 | [expr.const] for a precise definition. */ | |
67c03833 | 1221 | bool integral_constant_expression_p; |
a723baf1 | 1222 | |
14d22dd6 MM |
1223 | /* TRUE if we are parsing an integral constant-expression -- but a |
1224 | non-constant expression should be permitted as well. This flag | |
1225 | is used when parsing an array bound so that GNU variable-length | |
1226 | arrays are tolerated. */ | |
67c03833 | 1227 | bool allow_non_integral_constant_expression_p; |
14d22dd6 MM |
1228 | |
1229 | /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has | |
1230 | been seen that makes the expression non-constant. */ | |
67c03833 | 1231 | bool non_integral_constant_expression_p; |
14d22dd6 | 1232 | |
263ee052 MM |
1233 | /* TRUE if we are parsing the argument to "__offsetof__". */ |
1234 | bool in_offsetof_p; | |
1235 | ||
a723baf1 MM |
1236 | /* TRUE if local variable names and `this' are forbidden in the |
1237 | current context. */ | |
1238 | bool local_variables_forbidden_p; | |
1239 | ||
1240 | /* TRUE if the declaration we are parsing is part of a | |
1241 | linkage-specification of the form `extern string-literal | |
1242 | declaration'. */ | |
1243 | bool in_unbraced_linkage_specification_p; | |
1244 | ||
1245 | /* TRUE if we are presently parsing a declarator, after the | |
1246 | direct-declarator. */ | |
1247 | bool in_declarator_p; | |
1248 | ||
4bb8ca28 MM |
1249 | /* TRUE if we are presently parsing a template-argument-list. */ |
1250 | bool in_template_argument_list_p; | |
1251 | ||
0e59b3fb MM |
1252 | /* TRUE if we are presently parsing the body of an |
1253 | iteration-statement. */ | |
1254 | bool in_iteration_statement_p; | |
1255 | ||
1256 | /* TRUE if we are presently parsing the body of a switch | |
1257 | statement. */ | |
1258 | bool in_switch_statement_p; | |
1259 | ||
a723baf1 MM |
1260 | /* If non-NULL, then we are parsing a construct where new type |
1261 | definitions are not permitted. The string stored here will be | |
1262 | issued as an error message if a type is defined. */ | |
1263 | const char *type_definition_forbidden_message; | |
1264 | ||
8db1028e NS |
1265 | /* A list of lists. The outer list is a stack, used for member |
1266 | functions of local classes. At each level there are two sub-list, | |
1267 | one on TREE_VALUE and one on TREE_PURPOSE. Each of those | |
1268 | sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their | |
1269 | TREE_VALUE's. The functions are chained in reverse declaration | |
1270 | order. | |
1271 | ||
1272 | The TREE_PURPOSE sublist contains those functions with default | |
1273 | arguments that need post processing, and the TREE_VALUE sublist | |
1274 | contains those functions with definitions that need post | |
1275 | processing. | |
1276 | ||
1277 | These lists can only be processed once the outermost class being | |
9bcb9aae | 1278 | defined is complete. */ |
a723baf1 MM |
1279 | tree unparsed_functions_queues; |
1280 | ||
1281 | /* The number of classes whose definitions are currently in | |
1282 | progress. */ | |
1283 | unsigned num_classes_being_defined; | |
1284 | ||
1285 | /* The number of template parameter lists that apply directly to the | |
1286 | current declaration. */ | |
1287 | unsigned num_template_parameter_lists; | |
1288 | } cp_parser; | |
1289 | ||
04c06002 | 1290 | /* The type of a function that parses some kind of expression. */ |
94edc4ab | 1291 | typedef tree (*cp_parser_expression_fn) (cp_parser *); |
a723baf1 MM |
1292 | |
1293 | /* Prototypes. */ | |
1294 | ||
1295 | /* Constructors and destructors. */ | |
1296 | ||
1297 | static cp_parser *cp_parser_new | |
94edc4ab | 1298 | (void); |
a723baf1 MM |
1299 | |
1300 | /* Routines to parse various constructs. | |
1301 | ||
1302 | Those that return `tree' will return the error_mark_node (rather | |
1303 | than NULL_TREE) if a parse error occurs, unless otherwise noted. | |
1304 | Sometimes, they will return an ordinary node if error-recovery was | |
34cd5ae7 | 1305 | attempted, even though a parse error occurred. So, to check |
a723baf1 MM |
1306 | whether or not a parse error occurred, you should always use |
1307 | cp_parser_error_occurred. If the construct is optional (indicated | |
1308 | either by an `_opt' in the name of the function that does the | |
1309 | parsing or via a FLAGS parameter), then NULL_TREE is returned if | |
1310 | the construct is not present. */ | |
1311 | ||
1312 | /* Lexical conventions [gram.lex] */ | |
1313 | ||
1314 | static tree cp_parser_identifier | |
94edc4ab | 1315 | (cp_parser *); |
a723baf1 MM |
1316 | |
1317 | /* Basic concepts [gram.basic] */ | |
1318 | ||
1319 | static bool cp_parser_translation_unit | |
94edc4ab | 1320 | (cp_parser *); |
a723baf1 MM |
1321 | |
1322 | /* Expressions [gram.expr] */ | |
1323 | ||
1324 | static tree cp_parser_primary_expression | |
b3445994 | 1325 | (cp_parser *, cp_id_kind *, tree *); |
a723baf1 | 1326 | static tree cp_parser_id_expression |
f3c2dfc6 | 1327 | (cp_parser *, bool, bool, bool *, bool); |
a723baf1 | 1328 | static tree cp_parser_unqualified_id |
f3c2dfc6 | 1329 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1330 | static tree cp_parser_nested_name_specifier_opt |
a668c6ad | 1331 | (cp_parser *, bool, bool, bool, bool); |
a723baf1 | 1332 | static tree cp_parser_nested_name_specifier |
a723baf1 | 1333 | (cp_parser *, bool, bool, bool, bool); |
a668c6ad MM |
1334 | static tree cp_parser_class_or_namespace_name |
1335 | (cp_parser *, bool, bool, bool, bool, bool); | |
a723baf1 MM |
1336 | static tree cp_parser_postfix_expression |
1337 | (cp_parser *, bool); | |
7efa3e22 | 1338 | static tree cp_parser_parenthesized_expression_list |
39703eb9 | 1339 | (cp_parser *, bool, bool *); |
a723baf1 | 1340 | static void cp_parser_pseudo_destructor_name |
94edc4ab | 1341 | (cp_parser *, tree *, tree *); |
a723baf1 MM |
1342 | static tree cp_parser_unary_expression |
1343 | (cp_parser *, bool); | |
1344 | static enum tree_code cp_parser_unary_operator | |
94edc4ab | 1345 | (cp_token *); |
a723baf1 | 1346 | static tree cp_parser_new_expression |
94edc4ab | 1347 | (cp_parser *); |
a723baf1 | 1348 | static tree cp_parser_new_placement |
94edc4ab | 1349 | (cp_parser *); |
a723baf1 | 1350 | static tree cp_parser_new_type_id |
94edc4ab | 1351 | (cp_parser *); |
a723baf1 | 1352 | static tree cp_parser_new_declarator_opt |
94edc4ab | 1353 | (cp_parser *); |
a723baf1 | 1354 | static tree cp_parser_direct_new_declarator |
94edc4ab | 1355 | (cp_parser *); |
a723baf1 | 1356 | static tree cp_parser_new_initializer |
94edc4ab | 1357 | (cp_parser *); |
a723baf1 | 1358 | static tree cp_parser_delete_expression |
94edc4ab | 1359 | (cp_parser *); |
a723baf1 MM |
1360 | static tree cp_parser_cast_expression |
1361 | (cp_parser *, bool); | |
1362 | static tree cp_parser_pm_expression | |
94edc4ab | 1363 | (cp_parser *); |
a723baf1 | 1364 | static tree cp_parser_multiplicative_expression |
94edc4ab | 1365 | (cp_parser *); |
a723baf1 | 1366 | static tree cp_parser_additive_expression |
94edc4ab | 1367 | (cp_parser *); |
a723baf1 | 1368 | static tree cp_parser_shift_expression |
94edc4ab | 1369 | (cp_parser *); |
a723baf1 | 1370 | static tree cp_parser_relational_expression |
94edc4ab | 1371 | (cp_parser *); |
a723baf1 | 1372 | static tree cp_parser_equality_expression |
94edc4ab | 1373 | (cp_parser *); |
a723baf1 | 1374 | static tree cp_parser_and_expression |
94edc4ab | 1375 | (cp_parser *); |
a723baf1 | 1376 | static tree cp_parser_exclusive_or_expression |
94edc4ab | 1377 | (cp_parser *); |
a723baf1 | 1378 | static tree cp_parser_inclusive_or_expression |
94edc4ab | 1379 | (cp_parser *); |
a723baf1 | 1380 | static tree cp_parser_logical_and_expression |
94edc4ab | 1381 | (cp_parser *); |
a723baf1 | 1382 | static tree cp_parser_logical_or_expression |
94edc4ab | 1383 | (cp_parser *); |
a723baf1 | 1384 | static tree cp_parser_question_colon_clause |
94edc4ab | 1385 | (cp_parser *, tree); |
a723baf1 | 1386 | static tree cp_parser_assignment_expression |
94edc4ab | 1387 | (cp_parser *); |
a723baf1 | 1388 | static enum tree_code cp_parser_assignment_operator_opt |
94edc4ab | 1389 | (cp_parser *); |
a723baf1 | 1390 | static tree cp_parser_expression |
94edc4ab | 1391 | (cp_parser *); |
a723baf1 | 1392 | static tree cp_parser_constant_expression |
14d22dd6 | 1393 | (cp_parser *, bool, bool *); |
a723baf1 MM |
1394 | |
1395 | /* Statements [gram.stmt.stmt] */ | |
1396 | ||
1397 | static void cp_parser_statement | |
a5bcc582 | 1398 | (cp_parser *, bool); |
a723baf1 | 1399 | static tree cp_parser_labeled_statement |
a5bcc582 | 1400 | (cp_parser *, bool); |
a723baf1 | 1401 | static tree cp_parser_expression_statement |
a5bcc582 | 1402 | (cp_parser *, bool); |
a723baf1 | 1403 | static tree cp_parser_compound_statement |
a5bcc582 | 1404 | (cp_parser *, bool); |
a723baf1 | 1405 | static void cp_parser_statement_seq_opt |
a5bcc582 | 1406 | (cp_parser *, bool); |
a723baf1 | 1407 | static tree cp_parser_selection_statement |
94edc4ab | 1408 | (cp_parser *); |
a723baf1 | 1409 | static tree cp_parser_condition |
94edc4ab | 1410 | (cp_parser *); |
a723baf1 | 1411 | static tree cp_parser_iteration_statement |
94edc4ab | 1412 | (cp_parser *); |
a723baf1 | 1413 | static void cp_parser_for_init_statement |
94edc4ab | 1414 | (cp_parser *); |
a723baf1 | 1415 | static tree cp_parser_jump_statement |
94edc4ab | 1416 | (cp_parser *); |
a723baf1 | 1417 | static void cp_parser_declaration_statement |
94edc4ab | 1418 | (cp_parser *); |
a723baf1 MM |
1419 | |
1420 | static tree cp_parser_implicitly_scoped_statement | |
94edc4ab | 1421 | (cp_parser *); |
a723baf1 | 1422 | static void cp_parser_already_scoped_statement |
94edc4ab | 1423 | (cp_parser *); |
a723baf1 MM |
1424 | |
1425 | /* Declarations [gram.dcl.dcl] */ | |
1426 | ||
1427 | static void cp_parser_declaration_seq_opt | |
94edc4ab | 1428 | (cp_parser *); |
a723baf1 | 1429 | static void cp_parser_declaration |
94edc4ab | 1430 | (cp_parser *); |
a723baf1 | 1431 | static void cp_parser_block_declaration |
94edc4ab | 1432 | (cp_parser *, bool); |
a723baf1 | 1433 | static void cp_parser_simple_declaration |
94edc4ab | 1434 | (cp_parser *, bool); |
a723baf1 | 1435 | static tree cp_parser_decl_specifier_seq |
560ad596 | 1436 | (cp_parser *, cp_parser_flags, tree *, int *); |
a723baf1 | 1437 | static tree cp_parser_storage_class_specifier_opt |
94edc4ab | 1438 | (cp_parser *); |
a723baf1 | 1439 | static tree cp_parser_function_specifier_opt |
94edc4ab | 1440 | (cp_parser *); |
a723baf1 | 1441 | static tree cp_parser_type_specifier |
560ad596 | 1442 | (cp_parser *, cp_parser_flags, bool, bool, int *, bool *); |
a723baf1 | 1443 | static tree cp_parser_simple_type_specifier |
4b0d3cbe | 1444 | (cp_parser *, cp_parser_flags, bool); |
a723baf1 | 1445 | static tree cp_parser_type_name |
94edc4ab | 1446 | (cp_parser *); |
a723baf1 | 1447 | static tree cp_parser_elaborated_type_specifier |
94edc4ab | 1448 | (cp_parser *, bool, bool); |
a723baf1 | 1449 | static tree cp_parser_enum_specifier |
94edc4ab | 1450 | (cp_parser *); |
a723baf1 | 1451 | static void cp_parser_enumerator_list |
94edc4ab | 1452 | (cp_parser *, tree); |
a723baf1 | 1453 | static void cp_parser_enumerator_definition |
94edc4ab | 1454 | (cp_parser *, tree); |
a723baf1 | 1455 | static tree cp_parser_namespace_name |
94edc4ab | 1456 | (cp_parser *); |
a723baf1 | 1457 | static void cp_parser_namespace_definition |
94edc4ab | 1458 | (cp_parser *); |
a723baf1 | 1459 | static void cp_parser_namespace_body |
94edc4ab | 1460 | (cp_parser *); |
a723baf1 | 1461 | static tree cp_parser_qualified_namespace_specifier |
94edc4ab | 1462 | (cp_parser *); |
a723baf1 | 1463 | static void cp_parser_namespace_alias_definition |
94edc4ab | 1464 | (cp_parser *); |
a723baf1 | 1465 | static void cp_parser_using_declaration |
94edc4ab | 1466 | (cp_parser *); |
a723baf1 | 1467 | static void cp_parser_using_directive |
94edc4ab | 1468 | (cp_parser *); |
a723baf1 | 1469 | static void cp_parser_asm_definition |
94edc4ab | 1470 | (cp_parser *); |
a723baf1 | 1471 | static void cp_parser_linkage_specification |
94edc4ab | 1472 | (cp_parser *); |
a723baf1 MM |
1473 | |
1474 | /* Declarators [gram.dcl.decl] */ | |
1475 | ||
1476 | static tree cp_parser_init_declarator | |
560ad596 | 1477 | (cp_parser *, tree, tree, bool, bool, int, bool *); |
a723baf1 | 1478 | static tree cp_parser_declarator |
4bb8ca28 | 1479 | (cp_parser *, cp_parser_declarator_kind, int *, bool *); |
a723baf1 | 1480 | static tree cp_parser_direct_declarator |
7efa3e22 | 1481 | (cp_parser *, cp_parser_declarator_kind, int *); |
a723baf1 | 1482 | static enum tree_code cp_parser_ptr_operator |
94edc4ab | 1483 | (cp_parser *, tree *, tree *); |
a723baf1 | 1484 | static tree cp_parser_cv_qualifier_seq_opt |
94edc4ab | 1485 | (cp_parser *); |
a723baf1 | 1486 | static tree cp_parser_cv_qualifier_opt |
94edc4ab | 1487 | (cp_parser *); |
a723baf1 | 1488 | static tree cp_parser_declarator_id |
94edc4ab | 1489 | (cp_parser *); |
a723baf1 | 1490 | static tree cp_parser_type_id |
94edc4ab | 1491 | (cp_parser *); |
a723baf1 | 1492 | static tree cp_parser_type_specifier_seq |
94edc4ab | 1493 | (cp_parser *); |
a723baf1 | 1494 | static tree cp_parser_parameter_declaration_clause |
94edc4ab | 1495 | (cp_parser *); |
a723baf1 | 1496 | static tree cp_parser_parameter_declaration_list |
94edc4ab | 1497 | (cp_parser *); |
a723baf1 | 1498 | static tree cp_parser_parameter_declaration |
4bb8ca28 | 1499 | (cp_parser *, bool, bool *); |
a723baf1 MM |
1500 | static void cp_parser_function_body |
1501 | (cp_parser *); | |
1502 | static tree cp_parser_initializer | |
39703eb9 | 1503 | (cp_parser *, bool *, bool *); |
a723baf1 | 1504 | static tree cp_parser_initializer_clause |
39703eb9 | 1505 | (cp_parser *, bool *); |
a723baf1 | 1506 | static tree cp_parser_initializer_list |
39703eb9 | 1507 | (cp_parser *, bool *); |
a723baf1 MM |
1508 | |
1509 | static bool cp_parser_ctor_initializer_opt_and_function_body | |
1510 | (cp_parser *); | |
1511 | ||
1512 | /* Classes [gram.class] */ | |
1513 | ||
1514 | static tree cp_parser_class_name | |
a668c6ad | 1515 | (cp_parser *, bool, bool, bool, bool, bool, bool); |
a723baf1 | 1516 | static tree cp_parser_class_specifier |
94edc4ab | 1517 | (cp_parser *); |
a723baf1 | 1518 | static tree cp_parser_class_head |
94edc4ab | 1519 | (cp_parser *, bool *); |
a723baf1 | 1520 | static enum tag_types cp_parser_class_key |
94edc4ab | 1521 | (cp_parser *); |
a723baf1 | 1522 | static void cp_parser_member_specification_opt |
94edc4ab | 1523 | (cp_parser *); |
a723baf1 | 1524 | static void cp_parser_member_declaration |
94edc4ab | 1525 | (cp_parser *); |
a723baf1 | 1526 | static tree cp_parser_pure_specifier |
94edc4ab | 1527 | (cp_parser *); |
a723baf1 | 1528 | static tree cp_parser_constant_initializer |
94edc4ab | 1529 | (cp_parser *); |
a723baf1 MM |
1530 | |
1531 | /* Derived classes [gram.class.derived] */ | |
1532 | ||
1533 | static tree cp_parser_base_clause | |
94edc4ab | 1534 | (cp_parser *); |
a723baf1 | 1535 | static tree cp_parser_base_specifier |
94edc4ab | 1536 | (cp_parser *); |
a723baf1 MM |
1537 | |
1538 | /* Special member functions [gram.special] */ | |
1539 | ||
1540 | static tree cp_parser_conversion_function_id | |
94edc4ab | 1541 | (cp_parser *); |
a723baf1 | 1542 | static tree cp_parser_conversion_type_id |
94edc4ab | 1543 | (cp_parser *); |
a723baf1 | 1544 | static tree cp_parser_conversion_declarator_opt |
94edc4ab | 1545 | (cp_parser *); |
a723baf1 | 1546 | static bool cp_parser_ctor_initializer_opt |
94edc4ab | 1547 | (cp_parser *); |
a723baf1 | 1548 | static void cp_parser_mem_initializer_list |
94edc4ab | 1549 | (cp_parser *); |
a723baf1 | 1550 | static tree cp_parser_mem_initializer |
94edc4ab | 1551 | (cp_parser *); |
a723baf1 | 1552 | static tree cp_parser_mem_initializer_id |
94edc4ab | 1553 | (cp_parser *); |
a723baf1 MM |
1554 | |
1555 | /* Overloading [gram.over] */ | |
1556 | ||
1557 | static tree cp_parser_operator_function_id | |
94edc4ab | 1558 | (cp_parser *); |
a723baf1 | 1559 | static tree cp_parser_operator |
94edc4ab | 1560 | (cp_parser *); |
a723baf1 MM |
1561 | |
1562 | /* Templates [gram.temp] */ | |
1563 | ||
1564 | static void cp_parser_template_declaration | |
94edc4ab | 1565 | (cp_parser *, bool); |
a723baf1 | 1566 | static tree cp_parser_template_parameter_list |
94edc4ab | 1567 | (cp_parser *); |
a723baf1 | 1568 | static tree cp_parser_template_parameter |
94edc4ab | 1569 | (cp_parser *); |
a723baf1 | 1570 | static tree cp_parser_type_parameter |
94edc4ab | 1571 | (cp_parser *); |
a723baf1 | 1572 | static tree cp_parser_template_id |
a668c6ad | 1573 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1574 | static tree cp_parser_template_name |
a668c6ad | 1575 | (cp_parser *, bool, bool, bool, bool *); |
a723baf1 | 1576 | static tree cp_parser_template_argument_list |
94edc4ab | 1577 | (cp_parser *); |
a723baf1 | 1578 | static tree cp_parser_template_argument |
94edc4ab | 1579 | (cp_parser *); |
a723baf1 | 1580 | static void cp_parser_explicit_instantiation |
94edc4ab | 1581 | (cp_parser *); |
a723baf1 | 1582 | static void cp_parser_explicit_specialization |
94edc4ab | 1583 | (cp_parser *); |
a723baf1 MM |
1584 | |
1585 | /* Exception handling [gram.exception] */ | |
1586 | ||
1587 | static tree cp_parser_try_block | |
94edc4ab | 1588 | (cp_parser *); |
a723baf1 | 1589 | static bool cp_parser_function_try_block |
94edc4ab | 1590 | (cp_parser *); |
a723baf1 | 1591 | static void cp_parser_handler_seq |
94edc4ab | 1592 | (cp_parser *); |
a723baf1 | 1593 | static void cp_parser_handler |
94edc4ab | 1594 | (cp_parser *); |
a723baf1 | 1595 | static tree cp_parser_exception_declaration |
94edc4ab | 1596 | (cp_parser *); |
a723baf1 | 1597 | static tree cp_parser_throw_expression |
94edc4ab | 1598 | (cp_parser *); |
a723baf1 | 1599 | static tree cp_parser_exception_specification_opt |
94edc4ab | 1600 | (cp_parser *); |
a723baf1 | 1601 | static tree cp_parser_type_id_list |
94edc4ab | 1602 | (cp_parser *); |
a723baf1 MM |
1603 | |
1604 | /* GNU Extensions */ | |
1605 | ||
1606 | static tree cp_parser_asm_specification_opt | |
94edc4ab | 1607 | (cp_parser *); |
a723baf1 | 1608 | static tree cp_parser_asm_operand_list |
94edc4ab | 1609 | (cp_parser *); |
a723baf1 | 1610 | static tree cp_parser_asm_clobber_list |
94edc4ab | 1611 | (cp_parser *); |
a723baf1 | 1612 | static tree cp_parser_attributes_opt |
94edc4ab | 1613 | (cp_parser *); |
a723baf1 | 1614 | static tree cp_parser_attribute_list |
94edc4ab | 1615 | (cp_parser *); |
a723baf1 | 1616 | static bool cp_parser_extension_opt |
94edc4ab | 1617 | (cp_parser *, int *); |
a723baf1 | 1618 | static void cp_parser_label_declaration |
94edc4ab | 1619 | (cp_parser *); |
a723baf1 MM |
1620 | |
1621 | /* Utility Routines */ | |
1622 | ||
1623 | static tree cp_parser_lookup_name | |
b0bc6e8e | 1624 | (cp_parser *, tree, bool, bool, bool, bool); |
a723baf1 | 1625 | static tree cp_parser_lookup_name_simple |
94edc4ab | 1626 | (cp_parser *, tree); |
a723baf1 MM |
1627 | static tree cp_parser_maybe_treat_template_as_class |
1628 | (tree, bool); | |
1629 | static bool cp_parser_check_declarator_template_parameters | |
94edc4ab | 1630 | (cp_parser *, tree); |
a723baf1 | 1631 | static bool cp_parser_check_template_parameters |
94edc4ab | 1632 | (cp_parser *, unsigned); |
d6b4ea85 MM |
1633 | static tree cp_parser_simple_cast_expression |
1634 | (cp_parser *); | |
a723baf1 | 1635 | static tree cp_parser_binary_expression |
94edc4ab | 1636 | (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn); |
a723baf1 | 1637 | static tree cp_parser_global_scope_opt |
94edc4ab | 1638 | (cp_parser *, bool); |
a723baf1 MM |
1639 | static bool cp_parser_constructor_declarator_p |
1640 | (cp_parser *, bool); | |
1641 | static tree cp_parser_function_definition_from_specifiers_and_declarator | |
94edc4ab | 1642 | (cp_parser *, tree, tree, tree); |
a723baf1 | 1643 | static tree cp_parser_function_definition_after_declarator |
94edc4ab | 1644 | (cp_parser *, bool); |
a723baf1 | 1645 | static void cp_parser_template_declaration_after_export |
94edc4ab | 1646 | (cp_parser *, bool); |
a723baf1 | 1647 | static tree cp_parser_single_declaration |
94edc4ab | 1648 | (cp_parser *, bool, bool *); |
a723baf1 | 1649 | static tree cp_parser_functional_cast |
94edc4ab | 1650 | (cp_parser *, tree); |
4bb8ca28 MM |
1651 | static tree cp_parser_save_member_function_body |
1652 | (cp_parser *, tree, tree, tree); | |
ec75414f MM |
1653 | static tree cp_parser_enclosed_template_argument_list |
1654 | (cp_parser *); | |
8db1028e NS |
1655 | static void cp_parser_save_default_args |
1656 | (cp_parser *, tree); | |
a723baf1 | 1657 | static void cp_parser_late_parsing_for_member |
94edc4ab | 1658 | (cp_parser *, tree); |
a723baf1 | 1659 | static void cp_parser_late_parsing_default_args |
8218bd34 | 1660 | (cp_parser *, tree); |
a723baf1 | 1661 | static tree cp_parser_sizeof_operand |
94edc4ab | 1662 | (cp_parser *, enum rid); |
a723baf1 | 1663 | static bool cp_parser_declares_only_class_p |
94edc4ab | 1664 | (cp_parser *); |
d17811fd MM |
1665 | static tree cp_parser_fold_non_dependent_expr |
1666 | (tree); | |
a723baf1 | 1667 | static bool cp_parser_friend_p |
94edc4ab | 1668 | (tree); |
a723baf1 | 1669 | static cp_token *cp_parser_require |
94edc4ab | 1670 | (cp_parser *, enum cpp_ttype, const char *); |
a723baf1 | 1671 | static cp_token *cp_parser_require_keyword |
94edc4ab | 1672 | (cp_parser *, enum rid, const char *); |
a723baf1 | 1673 | static bool cp_parser_token_starts_function_definition_p |
94edc4ab | 1674 | (cp_token *); |
a723baf1 MM |
1675 | static bool cp_parser_next_token_starts_class_definition_p |
1676 | (cp_parser *); | |
d17811fd MM |
1677 | static bool cp_parser_next_token_ends_template_argument_p |
1678 | (cp_parser *); | |
a723baf1 | 1679 | static enum tag_types cp_parser_token_is_class_key |
94edc4ab | 1680 | (cp_token *); |
a723baf1 MM |
1681 | static void cp_parser_check_class_key |
1682 | (enum tag_types, tree type); | |
37d407a1 KL |
1683 | static void cp_parser_check_access_in_redeclaration |
1684 | (tree type); | |
a723baf1 MM |
1685 | static bool cp_parser_optional_template_keyword |
1686 | (cp_parser *); | |
2050a1bb MM |
1687 | static void cp_parser_pre_parsed_nested_name_specifier |
1688 | (cp_parser *); | |
a723baf1 MM |
1689 | static void cp_parser_cache_group |
1690 | (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned); | |
1691 | static void cp_parser_parse_tentatively | |
94edc4ab | 1692 | (cp_parser *); |
a723baf1 | 1693 | static void cp_parser_commit_to_tentative_parse |
94edc4ab | 1694 | (cp_parser *); |
a723baf1 | 1695 | static void cp_parser_abort_tentative_parse |
94edc4ab | 1696 | (cp_parser *); |
a723baf1 | 1697 | static bool cp_parser_parse_definitely |
94edc4ab | 1698 | (cp_parser *); |
f7b5ecd9 | 1699 | static inline bool cp_parser_parsing_tentatively |
94edc4ab | 1700 | (cp_parser *); |
a723baf1 | 1701 | static bool cp_parser_committed_to_tentative_parse |
94edc4ab | 1702 | (cp_parser *); |
a723baf1 | 1703 | static void cp_parser_error |
94edc4ab | 1704 | (cp_parser *, const char *); |
4bb8ca28 MM |
1705 | static void cp_parser_name_lookup_error |
1706 | (cp_parser *, tree, tree, const char *); | |
e5976695 | 1707 | static bool cp_parser_simulate_error |
94edc4ab | 1708 | (cp_parser *); |
a723baf1 | 1709 | static void cp_parser_check_type_definition |
94edc4ab | 1710 | (cp_parser *); |
560ad596 MM |
1711 | static void cp_parser_check_for_definition_in_return_type |
1712 | (tree, int); | |
ee43dab5 MM |
1713 | static void cp_parser_check_for_invalid_template_id |
1714 | (cp_parser *, tree); | |
67c03833 | 1715 | static tree cp_parser_non_integral_constant_expression |
14d22dd6 | 1716 | (const char *); |
8fbc5ae7 MM |
1717 | static bool cp_parser_diagnose_invalid_type_name |
1718 | (cp_parser *); | |
7efa3e22 | 1719 | static int cp_parser_skip_to_closing_parenthesis |
a668c6ad | 1720 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1721 | static void cp_parser_skip_to_end_of_statement |
94edc4ab | 1722 | (cp_parser *); |
e0860732 MM |
1723 | static void cp_parser_consume_semicolon_at_end_of_statement |
1724 | (cp_parser *); | |
a723baf1 | 1725 | static void cp_parser_skip_to_end_of_block_or_statement |
94edc4ab | 1726 | (cp_parser *); |
a723baf1 MM |
1727 | static void cp_parser_skip_to_closing_brace |
1728 | (cp_parser *); | |
1729 | static void cp_parser_skip_until_found | |
94edc4ab | 1730 | (cp_parser *, enum cpp_ttype, const char *); |
a723baf1 | 1731 | static bool cp_parser_error_occurred |
94edc4ab | 1732 | (cp_parser *); |
a723baf1 | 1733 | static bool cp_parser_allow_gnu_extensions_p |
94edc4ab | 1734 | (cp_parser *); |
a723baf1 | 1735 | static bool cp_parser_is_string_literal |
94edc4ab | 1736 | (cp_token *); |
a723baf1 | 1737 | static bool cp_parser_is_keyword |
94edc4ab | 1738 | (cp_token *, enum rid); |
a723baf1 | 1739 | |
4de8668e | 1740 | /* Returns nonzero if we are parsing tentatively. */ |
f7b5ecd9 MM |
1741 | |
1742 | static inline bool | |
94edc4ab | 1743 | cp_parser_parsing_tentatively (cp_parser* parser) |
f7b5ecd9 MM |
1744 | { |
1745 | return parser->context->next != NULL; | |
1746 | } | |
1747 | ||
4de8668e | 1748 | /* Returns nonzero if TOKEN is a string literal. */ |
a723baf1 MM |
1749 | |
1750 | static bool | |
94edc4ab | 1751 | cp_parser_is_string_literal (cp_token* token) |
a723baf1 MM |
1752 | { |
1753 | return (token->type == CPP_STRING || token->type == CPP_WSTRING); | |
1754 | } | |
1755 | ||
4de8668e | 1756 | /* Returns nonzero if TOKEN is the indicated KEYWORD. */ |
a723baf1 MM |
1757 | |
1758 | static bool | |
94edc4ab | 1759 | cp_parser_is_keyword (cp_token* token, enum rid keyword) |
a723baf1 MM |
1760 | { |
1761 | return token->keyword == keyword; | |
1762 | } | |
1763 | ||
a723baf1 MM |
1764 | /* Issue the indicated error MESSAGE. */ |
1765 | ||
1766 | static void | |
94edc4ab | 1767 | cp_parser_error (cp_parser* parser, const char* message) |
a723baf1 | 1768 | { |
a723baf1 | 1769 | /* Output the MESSAGE -- unless we're parsing tentatively. */ |
e5976695 | 1770 | if (!cp_parser_simulate_error (parser)) |
4bb8ca28 MM |
1771 | { |
1772 | cp_token *token; | |
1773 | token = cp_lexer_peek_token (parser->lexer); | |
5c832178 MM |
1774 | c_parse_error (message, |
1775 | /* Because c_parser_error does not understand | |
1776 | CPP_KEYWORD, keywords are treated like | |
1777 | identifiers. */ | |
1778 | (token->type == CPP_KEYWORD ? CPP_NAME : token->type), | |
1779 | token->value); | |
4bb8ca28 MM |
1780 | } |
1781 | } | |
1782 | ||
1783 | /* Issue an error about name-lookup failing. NAME is the | |
1784 | IDENTIFIER_NODE DECL is the result of | |
1785 | the lookup (as returned from cp_parser_lookup_name). DESIRED is | |
1786 | the thing that we hoped to find. */ | |
1787 | ||
1788 | static void | |
1789 | cp_parser_name_lookup_error (cp_parser* parser, | |
1790 | tree name, | |
1791 | tree decl, | |
1792 | const char* desired) | |
1793 | { | |
1794 | /* If name lookup completely failed, tell the user that NAME was not | |
1795 | declared. */ | |
1796 | if (decl == error_mark_node) | |
1797 | { | |
1798 | if (parser->scope && parser->scope != global_namespace) | |
1799 | error ("`%D::%D' has not been declared", | |
1800 | parser->scope, name); | |
1801 | else if (parser->scope == global_namespace) | |
1802 | error ("`::%D' has not been declared", name); | |
1803 | else | |
1804 | error ("`%D' has not been declared", name); | |
1805 | } | |
1806 | else if (parser->scope && parser->scope != global_namespace) | |
1807 | error ("`%D::%D' %s", parser->scope, name, desired); | |
1808 | else if (parser->scope == global_namespace) | |
1809 | error ("`::%D' %s", name, desired); | |
1810 | else | |
1811 | error ("`%D' %s", name, desired); | |
a723baf1 MM |
1812 | } |
1813 | ||
1814 | /* If we are parsing tentatively, remember that an error has occurred | |
e5976695 MM |
1815 | during this tentative parse. Returns true if the error was |
1816 | simulated; false if a messgae should be issued by the caller. */ | |
a723baf1 | 1817 | |
e5976695 | 1818 | static bool |
94edc4ab | 1819 | cp_parser_simulate_error (cp_parser* parser) |
a723baf1 MM |
1820 | { |
1821 | if (cp_parser_parsing_tentatively (parser) | |
1822 | && !cp_parser_committed_to_tentative_parse (parser)) | |
e5976695 MM |
1823 | { |
1824 | parser->context->status = CP_PARSER_STATUS_KIND_ERROR; | |
1825 | return true; | |
1826 | } | |
1827 | return false; | |
a723baf1 MM |
1828 | } |
1829 | ||
1830 | /* This function is called when a type is defined. If type | |
1831 | definitions are forbidden at this point, an error message is | |
1832 | issued. */ | |
1833 | ||
1834 | static void | |
94edc4ab | 1835 | cp_parser_check_type_definition (cp_parser* parser) |
a723baf1 MM |
1836 | { |
1837 | /* If types are forbidden here, issue a message. */ | |
1838 | if (parser->type_definition_forbidden_message) | |
1839 | /* Use `%s' to print the string in case there are any escape | |
1840 | characters in the message. */ | |
1841 | error ("%s", parser->type_definition_forbidden_message); | |
1842 | } | |
1843 | ||
560ad596 MM |
1844 | /* This function is called when a declaration is parsed. If |
1845 | DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM | |
1846 | indicates that a type was defined in the decl-specifiers for DECL, | |
1847 | then an error is issued. */ | |
1848 | ||
1849 | static void | |
1850 | cp_parser_check_for_definition_in_return_type (tree declarator, | |
1851 | int declares_class_or_enum) | |
1852 | { | |
1853 | /* [dcl.fct] forbids type definitions in return types. | |
1854 | Unfortunately, it's not easy to know whether or not we are | |
1855 | processing a return type until after the fact. */ | |
1856 | while (declarator | |
1857 | && (TREE_CODE (declarator) == INDIRECT_REF | |
1858 | || TREE_CODE (declarator) == ADDR_EXPR)) | |
1859 | declarator = TREE_OPERAND (declarator, 0); | |
1860 | if (declarator | |
1861 | && TREE_CODE (declarator) == CALL_EXPR | |
1862 | && declares_class_or_enum & 2) | |
1863 | error ("new types may not be defined in a return type"); | |
1864 | } | |
1865 | ||
ee43dab5 MM |
1866 | /* A type-specifier (TYPE) has been parsed which cannot be followed by |
1867 | "<" in any valid C++ program. If the next token is indeed "<", | |
1868 | issue a message warning the user about what appears to be an | |
1869 | invalid attempt to form a template-id. */ | |
1870 | ||
1871 | static void | |
1872 | cp_parser_check_for_invalid_template_id (cp_parser* parser, | |
1873 | tree type) | |
1874 | { | |
1875 | ptrdiff_t start; | |
1876 | cp_token *token; | |
1877 | ||
1878 | if (cp_lexer_next_token_is (parser->lexer, CPP_LESS)) | |
1879 | { | |
1880 | if (TYPE_P (type)) | |
1881 | error ("`%T' is not a template", type); | |
1882 | else if (TREE_CODE (type) == IDENTIFIER_NODE) | |
1883 | error ("`%s' is not a template", IDENTIFIER_POINTER (type)); | |
1884 | else | |
1885 | error ("invalid template-id"); | |
1886 | /* Remember the location of the invalid "<". */ | |
1887 | if (cp_parser_parsing_tentatively (parser) | |
1888 | && !cp_parser_committed_to_tentative_parse (parser)) | |
1889 | { | |
1890 | token = cp_lexer_peek_token (parser->lexer); | |
1891 | token = cp_lexer_prev_token (parser->lexer, token); | |
1892 | start = cp_lexer_token_difference (parser->lexer, | |
1893 | parser->lexer->first_token, | |
1894 | token); | |
1895 | } | |
1896 | else | |
1897 | start = -1; | |
1898 | /* Consume the "<". */ | |
1899 | cp_lexer_consume_token (parser->lexer); | |
1900 | /* Parse the template arguments. */ | |
1901 | cp_parser_enclosed_template_argument_list (parser); | |
da1d7781 | 1902 | /* Permanently remove the invalid template arguments so that |
ee43dab5 MM |
1903 | this error message is not issued again. */ |
1904 | if (start >= 0) | |
1905 | { | |
1906 | token = cp_lexer_advance_token (parser->lexer, | |
1907 | parser->lexer->first_token, | |
1908 | start); | |
1909 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
1910 | } | |
1911 | } | |
1912 | } | |
1913 | ||
cd0be382 | 1914 | /* Issue an error message about the fact that THING appeared in a |
14d22dd6 MM |
1915 | constant-expression. Returns ERROR_MARK_NODE. */ |
1916 | ||
1917 | static tree | |
67c03833 | 1918 | cp_parser_non_integral_constant_expression (const char *thing) |
14d22dd6 MM |
1919 | { |
1920 | error ("%s cannot appear in a constant-expression", thing); | |
1921 | return error_mark_node; | |
1922 | } | |
1923 | ||
8fbc5ae7 MM |
1924 | /* Check for a common situation where a type-name should be present, |
1925 | but is not, and issue a sensible error message. Returns true if an | |
1926 | invalid type-name was detected. */ | |
1927 | ||
1928 | static bool | |
1929 | cp_parser_diagnose_invalid_type_name (cp_parser *parser) | |
1930 | { | |
1931 | /* If the next two tokens are both identifiers, the code is | |
1932 | erroneous. The usual cause of this situation is code like: | |
1933 | ||
1934 | T t; | |
1935 | ||
1936 | where "T" should name a type -- but does not. */ | |
1937 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
1938 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME) | |
1939 | { | |
1940 | tree name; | |
1941 | ||
8d241e0b | 1942 | /* If parsing tentatively, we should commit; we really are |
8fbc5ae7 MM |
1943 | looking at a declaration. */ |
1944 | /* Consume the first identifier. */ | |
1945 | name = cp_lexer_consume_token (parser->lexer)->value; | |
1946 | /* Issue an error message. */ | |
1947 | error ("`%s' does not name a type", IDENTIFIER_POINTER (name)); | |
1948 | /* If we're in a template class, it's possible that the user was | |
1949 | referring to a type from a base class. For example: | |
1950 | ||
1951 | template <typename T> struct A { typedef T X; }; | |
1952 | template <typename T> struct B : public A<T> { X x; }; | |
1953 | ||
1954 | The user should have said "typename A<T>::X". */ | |
1955 | if (processing_template_decl && current_class_type) | |
1956 | { | |
1957 | tree b; | |
1958 | ||
1959 | for (b = TREE_CHAIN (TYPE_BINFO (current_class_type)); | |
1960 | b; | |
1961 | b = TREE_CHAIN (b)) | |
1962 | { | |
1963 | tree base_type = BINFO_TYPE (b); | |
1964 | if (CLASS_TYPE_P (base_type) | |
1fb3244a | 1965 | && dependent_type_p (base_type)) |
8fbc5ae7 MM |
1966 | { |
1967 | tree field; | |
1968 | /* Go from a particular instantiation of the | |
1969 | template (which will have an empty TYPE_FIELDs), | |
1970 | to the main version. */ | |
353b4fc0 | 1971 | base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type); |
8fbc5ae7 MM |
1972 | for (field = TYPE_FIELDS (base_type); |
1973 | field; | |
1974 | field = TREE_CHAIN (field)) | |
1975 | if (TREE_CODE (field) == TYPE_DECL | |
1976 | && DECL_NAME (field) == name) | |
1977 | { | |
1978 | error ("(perhaps `typename %T::%s' was intended)", | |
1979 | BINFO_TYPE (b), IDENTIFIER_POINTER (name)); | |
1980 | break; | |
1981 | } | |
1982 | if (field) | |
1983 | break; | |
1984 | } | |
1985 | } | |
1986 | } | |
1987 | /* Skip to the end of the declaration; there's no point in | |
1988 | trying to process it. */ | |
1989 | cp_parser_skip_to_end_of_statement (parser); | |
1990 | ||
1991 | return true; | |
1992 | } | |
1993 | ||
1994 | return false; | |
1995 | } | |
1996 | ||
a723baf1 | 1997 | /* Consume tokens up to, and including, the next non-nested closing `)'. |
7efa3e22 NS |
1998 | Returns 1 iff we found a closing `)'. RECOVERING is true, if we |
1999 | are doing error recovery. Returns -1 if OR_COMMA is true and we | |
2000 | found an unnested comma. */ | |
a723baf1 | 2001 | |
7efa3e22 NS |
2002 | static int |
2003 | cp_parser_skip_to_closing_parenthesis (cp_parser *parser, | |
a668c6ad MM |
2004 | bool recovering, |
2005 | bool or_comma, | |
2006 | bool consume_paren) | |
a723baf1 | 2007 | { |
7efa3e22 NS |
2008 | unsigned paren_depth = 0; |
2009 | unsigned brace_depth = 0; | |
a723baf1 | 2010 | |
7efa3e22 NS |
2011 | if (recovering && !or_comma && cp_parser_parsing_tentatively (parser) |
2012 | && !cp_parser_committed_to_tentative_parse (parser)) | |
2013 | return 0; | |
2014 | ||
a723baf1 MM |
2015 | while (true) |
2016 | { | |
2017 | cp_token *token; | |
7efa3e22 | 2018 | |
a723baf1 MM |
2019 | /* If we've run out of tokens, then there is no closing `)'. */ |
2020 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
7efa3e22 | 2021 | return 0; |
a723baf1 | 2022 | |
a668c6ad MM |
2023 | token = cp_lexer_peek_token (parser->lexer); |
2024 | ||
f4f206f4 | 2025 | /* This matches the processing in skip_to_end_of_statement. */ |
a668c6ad MM |
2026 | if (token->type == CPP_SEMICOLON && !brace_depth) |
2027 | return 0; | |
2028 | if (token->type == CPP_OPEN_BRACE) | |
2029 | ++brace_depth; | |
2030 | if (token->type == CPP_CLOSE_BRACE) | |
7efa3e22 | 2031 | { |
a668c6ad | 2032 | if (!brace_depth--) |
7efa3e22 | 2033 | return 0; |
7efa3e22 | 2034 | } |
a668c6ad MM |
2035 | if (recovering && or_comma && token->type == CPP_COMMA |
2036 | && !brace_depth && !paren_depth) | |
2037 | return -1; | |
7efa3e22 | 2038 | |
7efa3e22 NS |
2039 | if (!brace_depth) |
2040 | { | |
2041 | /* If it is an `(', we have entered another level of nesting. */ | |
2042 | if (token->type == CPP_OPEN_PAREN) | |
2043 | ++paren_depth; | |
2044 | /* If it is a `)', then we might be done. */ | |
2045 | else if (token->type == CPP_CLOSE_PAREN && !paren_depth--) | |
a668c6ad MM |
2046 | { |
2047 | if (consume_paren) | |
2048 | cp_lexer_consume_token (parser->lexer); | |
2049 | return 1; | |
2050 | } | |
7efa3e22 | 2051 | } |
a668c6ad MM |
2052 | |
2053 | /* Consume the token. */ | |
2054 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 MM |
2055 | } |
2056 | } | |
2057 | ||
2058 | /* Consume tokens until we reach the end of the current statement. | |
2059 | Normally, that will be just before consuming a `;'. However, if a | |
2060 | non-nested `}' comes first, then we stop before consuming that. */ | |
2061 | ||
2062 | static void | |
94edc4ab | 2063 | cp_parser_skip_to_end_of_statement (cp_parser* parser) |
a723baf1 MM |
2064 | { |
2065 | unsigned nesting_depth = 0; | |
2066 | ||
2067 | while (true) | |
2068 | { | |
2069 | cp_token *token; | |
2070 | ||
2071 | /* Peek at the next token. */ | |
2072 | token = cp_lexer_peek_token (parser->lexer); | |
2073 | /* If we've run out of tokens, stop. */ | |
2074 | if (token->type == CPP_EOF) | |
2075 | break; | |
2076 | /* If the next token is a `;', we have reached the end of the | |
2077 | statement. */ | |
2078 | if (token->type == CPP_SEMICOLON && !nesting_depth) | |
2079 | break; | |
2080 | /* If the next token is a non-nested `}', then we have reached | |
2081 | the end of the current block. */ | |
2082 | if (token->type == CPP_CLOSE_BRACE) | |
2083 | { | |
2084 | /* If this is a non-nested `}', stop before consuming it. | |
2085 | That way, when confronted with something like: | |
2086 | ||
2087 | { 3 + } | |
2088 | ||
2089 | we stop before consuming the closing `}', even though we | |
2090 | have not yet reached a `;'. */ | |
2091 | if (nesting_depth == 0) | |
2092 | break; | |
2093 | /* If it is the closing `}' for a block that we have | |
2094 | scanned, stop -- but only after consuming the token. | |
2095 | That way given: | |
2096 | ||
2097 | void f g () { ... } | |
2098 | typedef int I; | |
2099 | ||
2100 | we will stop after the body of the erroneously declared | |
2101 | function, but before consuming the following `typedef' | |
2102 | declaration. */ | |
2103 | if (--nesting_depth == 0) | |
2104 | { | |
2105 | cp_lexer_consume_token (parser->lexer); | |
2106 | break; | |
2107 | } | |
2108 | } | |
2109 | /* If it the next token is a `{', then we are entering a new | |
2110 | block. Consume the entire block. */ | |
2111 | else if (token->type == CPP_OPEN_BRACE) | |
2112 | ++nesting_depth; | |
2113 | /* Consume the token. */ | |
2114 | cp_lexer_consume_token (parser->lexer); | |
2115 | } | |
2116 | } | |
2117 | ||
e0860732 MM |
2118 | /* This function is called at the end of a statement or declaration. |
2119 | If the next token is a semicolon, it is consumed; otherwise, error | |
2120 | recovery is attempted. */ | |
2121 | ||
2122 | static void | |
2123 | cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser) | |
2124 | { | |
2125 | /* Look for the trailing `;'. */ | |
2126 | if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")) | |
2127 | { | |
2128 | /* If there is additional (erroneous) input, skip to the end of | |
2129 | the statement. */ | |
2130 | cp_parser_skip_to_end_of_statement (parser); | |
2131 | /* If the next token is now a `;', consume it. */ | |
2132 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
2133 | cp_lexer_consume_token (parser->lexer); | |
2134 | } | |
2135 | } | |
2136 | ||
a723baf1 MM |
2137 | /* Skip tokens until we have consumed an entire block, or until we |
2138 | have consumed a non-nested `;'. */ | |
2139 | ||
2140 | static void | |
94edc4ab | 2141 | cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser) |
a723baf1 MM |
2142 | { |
2143 | unsigned nesting_depth = 0; | |
2144 | ||
2145 | while (true) | |
2146 | { | |
2147 | cp_token *token; | |
2148 | ||
2149 | /* Peek at the next token. */ | |
2150 | token = cp_lexer_peek_token (parser->lexer); | |
2151 | /* If we've run out of tokens, stop. */ | |
2152 | if (token->type == CPP_EOF) | |
2153 | break; | |
2154 | /* If the next token is a `;', we have reached the end of the | |
2155 | statement. */ | |
2156 | if (token->type == CPP_SEMICOLON && !nesting_depth) | |
2157 | { | |
2158 | /* Consume the `;'. */ | |
2159 | cp_lexer_consume_token (parser->lexer); | |
2160 | break; | |
2161 | } | |
2162 | /* Consume the token. */ | |
2163 | token = cp_lexer_consume_token (parser->lexer); | |
2164 | /* If the next token is a non-nested `}', then we have reached | |
2165 | the end of the current block. */ | |
2166 | if (token->type == CPP_CLOSE_BRACE | |
2167 | && (nesting_depth == 0 || --nesting_depth == 0)) | |
2168 | break; | |
2169 | /* If it the next token is a `{', then we are entering a new | |
2170 | block. Consume the entire block. */ | |
2171 | if (token->type == CPP_OPEN_BRACE) | |
2172 | ++nesting_depth; | |
2173 | } | |
2174 | } | |
2175 | ||
2176 | /* Skip tokens until a non-nested closing curly brace is the next | |
2177 | token. */ | |
2178 | ||
2179 | static void | |
2180 | cp_parser_skip_to_closing_brace (cp_parser *parser) | |
2181 | { | |
2182 | unsigned nesting_depth = 0; | |
2183 | ||
2184 | while (true) | |
2185 | { | |
2186 | cp_token *token; | |
2187 | ||
2188 | /* Peek at the next token. */ | |
2189 | token = cp_lexer_peek_token (parser->lexer); | |
2190 | /* If we've run out of tokens, stop. */ | |
2191 | if (token->type == CPP_EOF) | |
2192 | break; | |
2193 | /* If the next token is a non-nested `}', then we have reached | |
2194 | the end of the current block. */ | |
2195 | if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0) | |
2196 | break; | |
2197 | /* If it the next token is a `{', then we are entering a new | |
2198 | block. Consume the entire block. */ | |
2199 | else if (token->type == CPP_OPEN_BRACE) | |
2200 | ++nesting_depth; | |
2201 | /* Consume the token. */ | |
2202 | cp_lexer_consume_token (parser->lexer); | |
2203 | } | |
2204 | } | |
2205 | ||
2206 | /* Create a new C++ parser. */ | |
2207 | ||
2208 | static cp_parser * | |
94edc4ab | 2209 | cp_parser_new (void) |
a723baf1 MM |
2210 | { |
2211 | cp_parser *parser; | |
17211ab5 GK |
2212 | cp_lexer *lexer; |
2213 | ||
2214 | /* cp_lexer_new_main is called before calling ggc_alloc because | |
2215 | cp_lexer_new_main might load a PCH file. */ | |
2216 | lexer = cp_lexer_new_main (); | |
a723baf1 | 2217 | |
c68b0a84 | 2218 | parser = ggc_alloc_cleared (sizeof (cp_parser)); |
17211ab5 | 2219 | parser->lexer = lexer; |
a723baf1 MM |
2220 | parser->context = cp_parser_context_new (NULL); |
2221 | ||
2222 | /* For now, we always accept GNU extensions. */ | |
2223 | parser->allow_gnu_extensions_p = 1; | |
2224 | ||
2225 | /* The `>' token is a greater-than operator, not the end of a | |
2226 | template-id. */ | |
2227 | parser->greater_than_is_operator_p = true; | |
2228 | ||
2229 | parser->default_arg_ok_p = true; | |
2230 | ||
2231 | /* We are not parsing a constant-expression. */ | |
67c03833 JM |
2232 | parser->integral_constant_expression_p = false; |
2233 | parser->allow_non_integral_constant_expression_p = false; | |
2234 | parser->non_integral_constant_expression_p = false; | |
a723baf1 | 2235 | |
263ee052 MM |
2236 | /* We are not parsing offsetof. */ |
2237 | parser->in_offsetof_p = false; | |
2238 | ||
a723baf1 MM |
2239 | /* Local variable names are not forbidden. */ |
2240 | parser->local_variables_forbidden_p = false; | |
2241 | ||
34cd5ae7 | 2242 | /* We are not processing an `extern "C"' declaration. */ |
a723baf1 MM |
2243 | parser->in_unbraced_linkage_specification_p = false; |
2244 | ||
2245 | /* We are not processing a declarator. */ | |
2246 | parser->in_declarator_p = false; | |
2247 | ||
4bb8ca28 MM |
2248 | /* We are not processing a template-argument-list. */ |
2249 | parser->in_template_argument_list_p = false; | |
2250 | ||
0e59b3fb MM |
2251 | /* We are not in an iteration statement. */ |
2252 | parser->in_iteration_statement_p = false; | |
2253 | ||
2254 | /* We are not in a switch statement. */ | |
2255 | parser->in_switch_statement_p = false; | |
2256 | ||
a723baf1 MM |
2257 | /* The unparsed function queue is empty. */ |
2258 | parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE); | |
2259 | ||
2260 | /* There are no classes being defined. */ | |
2261 | parser->num_classes_being_defined = 0; | |
2262 | ||
2263 | /* No template parameters apply. */ | |
2264 | parser->num_template_parameter_lists = 0; | |
2265 | ||
2266 | return parser; | |
2267 | } | |
2268 | ||
2269 | /* Lexical conventions [gram.lex] */ | |
2270 | ||
2271 | /* Parse an identifier. Returns an IDENTIFIER_NODE representing the | |
2272 | identifier. */ | |
2273 | ||
2274 | static tree | |
94edc4ab | 2275 | cp_parser_identifier (cp_parser* parser) |
a723baf1 MM |
2276 | { |
2277 | cp_token *token; | |
2278 | ||
2279 | /* Look for the identifier. */ | |
2280 | token = cp_parser_require (parser, CPP_NAME, "identifier"); | |
2281 | /* Return the value. */ | |
2282 | return token ? token->value : error_mark_node; | |
2283 | } | |
2284 | ||
2285 | /* Basic concepts [gram.basic] */ | |
2286 | ||
2287 | /* Parse a translation-unit. | |
2288 | ||
2289 | translation-unit: | |
2290 | declaration-seq [opt] | |
2291 | ||
2292 | Returns TRUE if all went well. */ | |
2293 | ||
2294 | static bool | |
94edc4ab | 2295 | cp_parser_translation_unit (cp_parser* parser) |
a723baf1 MM |
2296 | { |
2297 | while (true) | |
2298 | { | |
2299 | cp_parser_declaration_seq_opt (parser); | |
2300 | ||
2301 | /* If there are no tokens left then all went well. */ | |
2302 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
2303 | break; | |
2304 | ||
2305 | /* Otherwise, issue an error message. */ | |
2306 | cp_parser_error (parser, "expected declaration"); | |
2307 | return false; | |
2308 | } | |
2309 | ||
2310 | /* Consume the EOF token. */ | |
2311 | cp_parser_require (parser, CPP_EOF, "end-of-file"); | |
2312 | ||
2313 | /* Finish up. */ | |
2314 | finish_translation_unit (); | |
2315 | ||
2316 | /* All went well. */ | |
2317 | return true; | |
2318 | } | |
2319 | ||
2320 | /* Expressions [gram.expr] */ | |
2321 | ||
2322 | /* Parse a primary-expression. | |
2323 | ||
2324 | primary-expression: | |
2325 | literal | |
2326 | this | |
2327 | ( expression ) | |
2328 | id-expression | |
2329 | ||
2330 | GNU Extensions: | |
2331 | ||
2332 | primary-expression: | |
2333 | ( compound-statement ) | |
2334 | __builtin_va_arg ( assignment-expression , type-id ) | |
2335 | ||
2336 | literal: | |
2337 | __null | |
2338 | ||
2339 | Returns a representation of the expression. | |
2340 | ||
2341 | *IDK indicates what kind of id-expression (if any) was present. | |
2342 | ||
2343 | *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be | |
2344 | used as the operand of a pointer-to-member. In that case, | |
2345 | *QUALIFYING_CLASS gives the class that is used as the qualifying | |
2346 | class in the pointer-to-member. */ | |
2347 | ||
2348 | static tree | |
2349 | cp_parser_primary_expression (cp_parser *parser, | |
b3445994 | 2350 | cp_id_kind *idk, |
a723baf1 MM |
2351 | tree *qualifying_class) |
2352 | { | |
2353 | cp_token *token; | |
2354 | ||
2355 | /* Assume the primary expression is not an id-expression. */ | |
b3445994 | 2356 | *idk = CP_ID_KIND_NONE; |
a723baf1 MM |
2357 | /* And that it cannot be used as pointer-to-member. */ |
2358 | *qualifying_class = NULL_TREE; | |
2359 | ||
2360 | /* Peek at the next token. */ | |
2361 | token = cp_lexer_peek_token (parser->lexer); | |
2362 | switch (token->type) | |
2363 | { | |
2364 | /* literal: | |
2365 | integer-literal | |
2366 | character-literal | |
2367 | floating-literal | |
2368 | string-literal | |
2369 | boolean-literal */ | |
2370 | case CPP_CHAR: | |
2371 | case CPP_WCHAR: | |
2372 | case CPP_STRING: | |
2373 | case CPP_WSTRING: | |
2374 | case CPP_NUMBER: | |
2375 | token = cp_lexer_consume_token (parser->lexer); | |
2376 | return token->value; | |
2377 | ||
2378 | case CPP_OPEN_PAREN: | |
2379 | { | |
2380 | tree expr; | |
2381 | bool saved_greater_than_is_operator_p; | |
2382 | ||
2383 | /* Consume the `('. */ | |
2384 | cp_lexer_consume_token (parser->lexer); | |
2385 | /* Within a parenthesized expression, a `>' token is always | |
2386 | the greater-than operator. */ | |
2387 | saved_greater_than_is_operator_p | |
2388 | = parser->greater_than_is_operator_p; | |
2389 | parser->greater_than_is_operator_p = true; | |
2390 | /* If we see `( { ' then we are looking at the beginning of | |
2391 | a GNU statement-expression. */ | |
2392 | if (cp_parser_allow_gnu_extensions_p (parser) | |
2393 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
2394 | { | |
2395 | /* Statement-expressions are not allowed by the standard. */ | |
2396 | if (pedantic) | |
2397 | pedwarn ("ISO C++ forbids braced-groups within expressions"); | |
2398 | ||
2399 | /* And they're not allowed outside of a function-body; you | |
2400 | cannot, for example, write: | |
2401 | ||
2402 | int i = ({ int j = 3; j + 1; }); | |
2403 | ||
2404 | at class or namespace scope. */ | |
2405 | if (!at_function_scope_p ()) | |
2406 | error ("statement-expressions are allowed only inside functions"); | |
2407 | /* Start the statement-expression. */ | |
2408 | expr = begin_stmt_expr (); | |
2409 | /* Parse the compound-statement. */ | |
a5bcc582 | 2410 | cp_parser_compound_statement (parser, true); |
a723baf1 | 2411 | /* Finish up. */ |
303b7406 | 2412 | expr = finish_stmt_expr (expr, false); |
a723baf1 MM |
2413 | } |
2414 | else | |
2415 | { | |
2416 | /* Parse the parenthesized expression. */ | |
2417 | expr = cp_parser_expression (parser); | |
2418 | /* Let the front end know that this expression was | |
2419 | enclosed in parentheses. This matters in case, for | |
2420 | example, the expression is of the form `A::B', since | |
2421 | `&A::B' might be a pointer-to-member, but `&(A::B)' is | |
2422 | not. */ | |
2423 | finish_parenthesized_expr (expr); | |
2424 | } | |
2425 | /* The `>' token might be the end of a template-id or | |
2426 | template-parameter-list now. */ | |
2427 | parser->greater_than_is_operator_p | |
2428 | = saved_greater_than_is_operator_p; | |
2429 | /* Consume the `)'. */ | |
2430 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
2431 | cp_parser_skip_to_end_of_statement (parser); | |
2432 | ||
2433 | return expr; | |
2434 | } | |
2435 | ||
2436 | case CPP_KEYWORD: | |
2437 | switch (token->keyword) | |
2438 | { | |
2439 | /* These two are the boolean literals. */ | |
2440 | case RID_TRUE: | |
2441 | cp_lexer_consume_token (parser->lexer); | |
2442 | return boolean_true_node; | |
2443 | case RID_FALSE: | |
2444 | cp_lexer_consume_token (parser->lexer); | |
2445 | return boolean_false_node; | |
2446 | ||
2447 | /* The `__null' literal. */ | |
2448 | case RID_NULL: | |
2449 | cp_lexer_consume_token (parser->lexer); | |
2450 | return null_node; | |
2451 | ||
2452 | /* Recognize the `this' keyword. */ | |
2453 | case RID_THIS: | |
2454 | cp_lexer_consume_token (parser->lexer); | |
2455 | if (parser->local_variables_forbidden_p) | |
2456 | { | |
2457 | error ("`this' may not be used in this context"); | |
2458 | return error_mark_node; | |
2459 | } | |
14d22dd6 | 2460 | /* Pointers cannot appear in constant-expressions. */ |
67c03833 | 2461 | if (parser->integral_constant_expression_p) |
14d22dd6 | 2462 | { |
67c03833 JM |
2463 | if (!parser->allow_non_integral_constant_expression_p) |
2464 | return cp_parser_non_integral_constant_expression ("`this'"); | |
2465 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 2466 | } |
a723baf1 MM |
2467 | return finish_this_expr (); |
2468 | ||
2469 | /* The `operator' keyword can be the beginning of an | |
2470 | id-expression. */ | |
2471 | case RID_OPERATOR: | |
2472 | goto id_expression; | |
2473 | ||
2474 | case RID_FUNCTION_NAME: | |
2475 | case RID_PRETTY_FUNCTION_NAME: | |
2476 | case RID_C99_FUNCTION_NAME: | |
2477 | /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and | |
2478 | __func__ are the names of variables -- but they are | |
2479 | treated specially. Therefore, they are handled here, | |
2480 | rather than relying on the generic id-expression logic | |
34cd5ae7 | 2481 | below. Grammatically, these names are id-expressions. |
a723baf1 MM |
2482 | |
2483 | Consume the token. */ | |
2484 | token = cp_lexer_consume_token (parser->lexer); | |
2485 | /* Look up the name. */ | |
2486 | return finish_fname (token->value); | |
2487 | ||
2488 | case RID_VA_ARG: | |
2489 | { | |
2490 | tree expression; | |
2491 | tree type; | |
2492 | ||
2493 | /* The `__builtin_va_arg' construct is used to handle | |
2494 | `va_arg'. Consume the `__builtin_va_arg' token. */ | |
2495 | cp_lexer_consume_token (parser->lexer); | |
2496 | /* Look for the opening `('. */ | |
2497 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
2498 | /* Now, parse the assignment-expression. */ | |
2499 | expression = cp_parser_assignment_expression (parser); | |
2500 | /* Look for the `,'. */ | |
2501 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
2502 | /* Parse the type-id. */ | |
2503 | type = cp_parser_type_id (parser); | |
2504 | /* Look for the closing `)'. */ | |
2505 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
14d22dd6 MM |
2506 | /* Using `va_arg' in a constant-expression is not |
2507 | allowed. */ | |
67c03833 | 2508 | if (parser->integral_constant_expression_p) |
14d22dd6 | 2509 | { |
67c03833 JM |
2510 | if (!parser->allow_non_integral_constant_expression_p) |
2511 | return cp_parser_non_integral_constant_expression ("`va_arg'"); | |
2512 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 2513 | } |
a723baf1 MM |
2514 | return build_x_va_arg (expression, type); |
2515 | } | |
2516 | ||
263ee052 MM |
2517 | case RID_OFFSETOF: |
2518 | { | |
2519 | tree expression; | |
2520 | bool saved_in_offsetof_p; | |
2521 | ||
2522 | /* Consume the "__offsetof__" token. */ | |
2523 | cp_lexer_consume_token (parser->lexer); | |
2524 | /* Consume the opening `('. */ | |
2525 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
2526 | /* Parse the parenthesized (almost) constant-expression. */ | |
2527 | saved_in_offsetof_p = parser->in_offsetof_p; | |
2528 | parser->in_offsetof_p = true; | |
2529 | expression | |
2530 | = cp_parser_constant_expression (parser, | |
2531 | /*allow_non_constant_p=*/false, | |
2532 | /*non_constant_p=*/NULL); | |
2533 | parser->in_offsetof_p = saved_in_offsetof_p; | |
2534 | /* Consume the closing ')'. */ | |
2535 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
2536 | ||
2537 | return expression; | |
2538 | } | |
2539 | ||
a723baf1 MM |
2540 | default: |
2541 | cp_parser_error (parser, "expected primary-expression"); | |
2542 | return error_mark_node; | |
2543 | } | |
a723baf1 MM |
2544 | |
2545 | /* An id-expression can start with either an identifier, a | |
2546 | `::' as the beginning of a qualified-id, or the "operator" | |
2547 | keyword. */ | |
2548 | case CPP_NAME: | |
2549 | case CPP_SCOPE: | |
2550 | case CPP_TEMPLATE_ID: | |
2551 | case CPP_NESTED_NAME_SPECIFIER: | |
2552 | { | |
2553 | tree id_expression; | |
2554 | tree decl; | |
b3445994 | 2555 | const char *error_msg; |
a723baf1 MM |
2556 | |
2557 | id_expression: | |
2558 | /* Parse the id-expression. */ | |
2559 | id_expression | |
2560 | = cp_parser_id_expression (parser, | |
2561 | /*template_keyword_p=*/false, | |
2562 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
2563 | /*template_p=*/NULL, |
2564 | /*declarator_p=*/false); | |
a723baf1 MM |
2565 | if (id_expression == error_mark_node) |
2566 | return error_mark_node; | |
2567 | /* If we have a template-id, then no further lookup is | |
2568 | required. If the template-id was for a template-class, we | |
2569 | will sometimes have a TYPE_DECL at this point. */ | |
2570 | else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR | |
2571 | || TREE_CODE (id_expression) == TYPE_DECL) | |
2572 | decl = id_expression; | |
2573 | /* Look up the name. */ | |
2574 | else | |
2575 | { | |
2576 | decl = cp_parser_lookup_name_simple (parser, id_expression); | |
2577 | /* If name lookup gives us a SCOPE_REF, then the | |
2578 | qualifying scope was dependent. Just propagate the | |
2579 | name. */ | |
2580 | if (TREE_CODE (decl) == SCOPE_REF) | |
2581 | { | |
2582 | if (TYPE_P (TREE_OPERAND (decl, 0))) | |
2583 | *qualifying_class = TREE_OPERAND (decl, 0); | |
2584 | return decl; | |
2585 | } | |
2586 | /* Check to see if DECL is a local variable in a context | |
2587 | where that is forbidden. */ | |
2588 | if (parser->local_variables_forbidden_p | |
2589 | && local_variable_p (decl)) | |
2590 | { | |
2591 | /* It might be that we only found DECL because we are | |
2592 | trying to be generous with pre-ISO scoping rules. | |
2593 | For example, consider: | |
2594 | ||
2595 | int i; | |
2596 | void g() { | |
2597 | for (int i = 0; i < 10; ++i) {} | |
2598 | extern void f(int j = i); | |
2599 | } | |
2600 | ||
2601 | Here, name look up will originally find the out | |
2602 | of scope `i'. We need to issue a warning message, | |
2603 | but then use the global `i'. */ | |
2604 | decl = check_for_out_of_scope_variable (decl); | |
2605 | if (local_variable_p (decl)) | |
2606 | { | |
2607 | error ("local variable `%D' may not appear in this context", | |
2608 | decl); | |
2609 | return error_mark_node; | |
2610 | } | |
2611 | } | |
c006d942 | 2612 | } |
b3445994 MM |
2613 | |
2614 | decl = finish_id_expression (id_expression, decl, parser->scope, | |
2615 | idk, qualifying_class, | |
67c03833 JM |
2616 | parser->integral_constant_expression_p, |
2617 | parser->allow_non_integral_constant_expression_p, | |
2618 | &parser->non_integral_constant_expression_p, | |
b3445994 MM |
2619 | &error_msg); |
2620 | if (error_msg) | |
2621 | cp_parser_error (parser, error_msg); | |
a723baf1 MM |
2622 | return decl; |
2623 | } | |
2624 | ||
2625 | /* Anything else is an error. */ | |
2626 | default: | |
2627 | cp_parser_error (parser, "expected primary-expression"); | |
2628 | return error_mark_node; | |
2629 | } | |
2630 | } | |
2631 | ||
2632 | /* Parse an id-expression. | |
2633 | ||
2634 | id-expression: | |
2635 | unqualified-id | |
2636 | qualified-id | |
2637 | ||
2638 | qualified-id: | |
2639 | :: [opt] nested-name-specifier template [opt] unqualified-id | |
2640 | :: identifier | |
2641 | :: operator-function-id | |
2642 | :: template-id | |
2643 | ||
2644 | Return a representation of the unqualified portion of the | |
2645 | identifier. Sets PARSER->SCOPE to the qualifying scope if there is | |
2646 | a `::' or nested-name-specifier. | |
2647 | ||
2648 | Often, if the id-expression was a qualified-id, the caller will | |
2649 | want to make a SCOPE_REF to represent the qualified-id. This | |
2650 | function does not do this in order to avoid wastefully creating | |
2651 | SCOPE_REFs when they are not required. | |
2652 | ||
a723baf1 MM |
2653 | If TEMPLATE_KEYWORD_P is true, then we have just seen the |
2654 | `template' keyword. | |
2655 | ||
2656 | If CHECK_DEPENDENCY_P is false, then names are looked up inside | |
2657 | uninstantiated templates. | |
2658 | ||
15d2cb19 | 2659 | If *TEMPLATE_P is non-NULL, it is set to true iff the |
a723baf1 | 2660 | `template' keyword is used to explicitly indicate that the entity |
f3c2dfc6 MM |
2661 | named is a template. |
2662 | ||
2663 | If DECLARATOR_P is true, the id-expression is appearing as part of | |
cd0be382 | 2664 | a declarator, rather than as part of an expression. */ |
a723baf1 MM |
2665 | |
2666 | static tree | |
2667 | cp_parser_id_expression (cp_parser *parser, | |
2668 | bool template_keyword_p, | |
2669 | bool check_dependency_p, | |
f3c2dfc6 MM |
2670 | bool *template_p, |
2671 | bool declarator_p) | |
a723baf1 MM |
2672 | { |
2673 | bool global_scope_p; | |
2674 | bool nested_name_specifier_p; | |
2675 | ||
2676 | /* Assume the `template' keyword was not used. */ | |
2677 | if (template_p) | |
2678 | *template_p = false; | |
2679 | ||
2680 | /* Look for the optional `::' operator. */ | |
2681 | global_scope_p | |
2682 | = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) | |
2683 | != NULL_TREE); | |
2684 | /* Look for the optional nested-name-specifier. */ | |
2685 | nested_name_specifier_p | |
2686 | = (cp_parser_nested_name_specifier_opt (parser, | |
2687 | /*typename_keyword_p=*/false, | |
2688 | check_dependency_p, | |
a668c6ad MM |
2689 | /*type_p=*/false, |
2690 | /*is_declarator=*/false) | |
a723baf1 MM |
2691 | != NULL_TREE); |
2692 | /* If there is a nested-name-specifier, then we are looking at | |
2693 | the first qualified-id production. */ | |
2694 | if (nested_name_specifier_p) | |
2695 | { | |
2696 | tree saved_scope; | |
2697 | tree saved_object_scope; | |
2698 | tree saved_qualifying_scope; | |
2699 | tree unqualified_id; | |
2700 | bool is_template; | |
2701 | ||
2702 | /* See if the next token is the `template' keyword. */ | |
2703 | if (!template_p) | |
2704 | template_p = &is_template; | |
2705 | *template_p = cp_parser_optional_template_keyword (parser); | |
2706 | /* Name lookup we do during the processing of the | |
2707 | unqualified-id might obliterate SCOPE. */ | |
2708 | saved_scope = parser->scope; | |
2709 | saved_object_scope = parser->object_scope; | |
2710 | saved_qualifying_scope = parser->qualifying_scope; | |
2711 | /* Process the final unqualified-id. */ | |
2712 | unqualified_id = cp_parser_unqualified_id (parser, *template_p, | |
f3c2dfc6 MM |
2713 | check_dependency_p, |
2714 | declarator_p); | |
a723baf1 MM |
2715 | /* Restore the SAVED_SCOPE for our caller. */ |
2716 | parser->scope = saved_scope; | |
2717 | parser->object_scope = saved_object_scope; | |
2718 | parser->qualifying_scope = saved_qualifying_scope; | |
2719 | ||
2720 | return unqualified_id; | |
2721 | } | |
2722 | /* Otherwise, if we are in global scope, then we are looking at one | |
2723 | of the other qualified-id productions. */ | |
2724 | else if (global_scope_p) | |
2725 | { | |
2726 | cp_token *token; | |
2727 | tree id; | |
2728 | ||
e5976695 MM |
2729 | /* Peek at the next token. */ |
2730 | token = cp_lexer_peek_token (parser->lexer); | |
2731 | ||
2732 | /* If it's an identifier, and the next token is not a "<", then | |
2733 | we can avoid the template-id case. This is an optimization | |
2734 | for this common case. */ | |
2735 | if (token->type == CPP_NAME | |
2736 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS) | |
2737 | return cp_parser_identifier (parser); | |
2738 | ||
a723baf1 MM |
2739 | cp_parser_parse_tentatively (parser); |
2740 | /* Try a template-id. */ | |
2741 | id = cp_parser_template_id (parser, | |
2742 | /*template_keyword_p=*/false, | |
a668c6ad MM |
2743 | /*check_dependency_p=*/true, |
2744 | declarator_p); | |
a723baf1 MM |
2745 | /* If that worked, we're done. */ |
2746 | if (cp_parser_parse_definitely (parser)) | |
2747 | return id; | |
2748 | ||
e5976695 MM |
2749 | /* Peek at the next token. (Changes in the token buffer may |
2750 | have invalidated the pointer obtained above.) */ | |
a723baf1 MM |
2751 | token = cp_lexer_peek_token (parser->lexer); |
2752 | ||
2753 | switch (token->type) | |
2754 | { | |
2755 | case CPP_NAME: | |
2756 | return cp_parser_identifier (parser); | |
2757 | ||
2758 | case CPP_KEYWORD: | |
2759 | if (token->keyword == RID_OPERATOR) | |
2760 | return cp_parser_operator_function_id (parser); | |
2761 | /* Fall through. */ | |
2762 | ||
2763 | default: | |
2764 | cp_parser_error (parser, "expected id-expression"); | |
2765 | return error_mark_node; | |
2766 | } | |
2767 | } | |
2768 | else | |
2769 | return cp_parser_unqualified_id (parser, template_keyword_p, | |
f3c2dfc6 MM |
2770 | /*check_dependency_p=*/true, |
2771 | declarator_p); | |
a723baf1 MM |
2772 | } |
2773 | ||
2774 | /* Parse an unqualified-id. | |
2775 | ||
2776 | unqualified-id: | |
2777 | identifier | |
2778 | operator-function-id | |
2779 | conversion-function-id | |
2780 | ~ class-name | |
2781 | template-id | |
2782 | ||
2783 | If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template' | |
2784 | keyword, in a construct like `A::template ...'. | |
2785 | ||
2786 | Returns a representation of unqualified-id. For the `identifier' | |
2787 | production, an IDENTIFIER_NODE is returned. For the `~ class-name' | |
2788 | production a BIT_NOT_EXPR is returned; the operand of the | |
2789 | BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the | |
2790 | other productions, see the documentation accompanying the | |
2791 | corresponding parsing functions. If CHECK_DEPENDENCY_P is false, | |
f3c2dfc6 MM |
2792 | names are looked up in uninstantiated templates. If DECLARATOR_P |
2793 | is true, the unqualified-id is appearing as part of a declarator, | |
2794 | rather than as part of an expression. */ | |
a723baf1 MM |
2795 | |
2796 | static tree | |
94edc4ab NN |
2797 | cp_parser_unqualified_id (cp_parser* parser, |
2798 | bool template_keyword_p, | |
f3c2dfc6 MM |
2799 | bool check_dependency_p, |
2800 | bool declarator_p) | |
a723baf1 MM |
2801 | { |
2802 | cp_token *token; | |
2803 | ||
2804 | /* Peek at the next token. */ | |
2805 | token = cp_lexer_peek_token (parser->lexer); | |
2806 | ||
2807 | switch (token->type) | |
2808 | { | |
2809 | case CPP_NAME: | |
2810 | { | |
2811 | tree id; | |
2812 | ||
2813 | /* We don't know yet whether or not this will be a | |
2814 | template-id. */ | |
2815 | cp_parser_parse_tentatively (parser); | |
2816 | /* Try a template-id. */ | |
2817 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
2818 | check_dependency_p, |
2819 | declarator_p); | |
a723baf1 MM |
2820 | /* If it worked, we're done. */ |
2821 | if (cp_parser_parse_definitely (parser)) | |
2822 | return id; | |
2823 | /* Otherwise, it's an ordinary identifier. */ | |
2824 | return cp_parser_identifier (parser); | |
2825 | } | |
2826 | ||
2827 | case CPP_TEMPLATE_ID: | |
2828 | return cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
2829 | check_dependency_p, |
2830 | declarator_p); | |
a723baf1 MM |
2831 | |
2832 | case CPP_COMPL: | |
2833 | { | |
2834 | tree type_decl; | |
2835 | tree qualifying_scope; | |
2836 | tree object_scope; | |
2837 | tree scope; | |
2838 | ||
2839 | /* Consume the `~' token. */ | |
2840 | cp_lexer_consume_token (parser->lexer); | |
2841 | /* Parse the class-name. The standard, as written, seems to | |
2842 | say that: | |
2843 | ||
2844 | template <typename T> struct S { ~S (); }; | |
2845 | template <typename T> S<T>::~S() {} | |
2846 | ||
2847 | is invalid, since `~' must be followed by a class-name, but | |
2848 | `S<T>' is dependent, and so not known to be a class. | |
2849 | That's not right; we need to look in uninstantiated | |
2850 | templates. A further complication arises from: | |
2851 | ||
2852 | template <typename T> void f(T t) { | |
2853 | t.T::~T(); | |
2854 | } | |
2855 | ||
2856 | Here, it is not possible to look up `T' in the scope of `T' | |
2857 | itself. We must look in both the current scope, and the | |
2858 | scope of the containing complete expression. | |
2859 | ||
2860 | Yet another issue is: | |
2861 | ||
2862 | struct S { | |
2863 | int S; | |
2864 | ~S(); | |
2865 | }; | |
2866 | ||
2867 | S::~S() {} | |
2868 | ||
2869 | The standard does not seem to say that the `S' in `~S' | |
2870 | should refer to the type `S' and not the data member | |
2871 | `S::S'. */ | |
2872 | ||
2873 | /* DR 244 says that we look up the name after the "~" in the | |
2874 | same scope as we looked up the qualifying name. That idea | |
2875 | isn't fully worked out; it's more complicated than that. */ | |
2876 | scope = parser->scope; | |
2877 | object_scope = parser->object_scope; | |
2878 | qualifying_scope = parser->qualifying_scope; | |
2879 | ||
2880 | /* If the name is of the form "X::~X" it's OK. */ | |
2881 | if (scope && TYPE_P (scope) | |
2882 | && cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
2883 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
2884 | == CPP_OPEN_PAREN) | |
2885 | && (cp_lexer_peek_token (parser->lexer)->value | |
2886 | == TYPE_IDENTIFIER (scope))) | |
2887 | { | |
2888 | cp_lexer_consume_token (parser->lexer); | |
2889 | return build_nt (BIT_NOT_EXPR, scope); | |
2890 | } | |
2891 | ||
2892 | /* If there was an explicit qualification (S::~T), first look | |
2893 | in the scope given by the qualification (i.e., S). */ | |
2894 | if (scope) | |
2895 | { | |
2896 | cp_parser_parse_tentatively (parser); | |
2897 | type_decl = cp_parser_class_name (parser, | |
2898 | /*typename_keyword_p=*/false, | |
2899 | /*template_keyword_p=*/false, | |
2900 | /*type_p=*/false, | |
a723baf1 | 2901 | /*check_dependency=*/false, |
a668c6ad MM |
2902 | /*class_head_p=*/false, |
2903 | declarator_p); | |
a723baf1 MM |
2904 | if (cp_parser_parse_definitely (parser)) |
2905 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); | |
2906 | } | |
2907 | /* In "N::S::~S", look in "N" as well. */ | |
2908 | if (scope && qualifying_scope) | |
2909 | { | |
2910 | cp_parser_parse_tentatively (parser); | |
2911 | parser->scope = qualifying_scope; | |
2912 | parser->object_scope = NULL_TREE; | |
2913 | parser->qualifying_scope = NULL_TREE; | |
2914 | type_decl | |
2915 | = cp_parser_class_name (parser, | |
2916 | /*typename_keyword_p=*/false, | |
2917 | /*template_keyword_p=*/false, | |
2918 | /*type_p=*/false, | |
a723baf1 | 2919 | /*check_dependency=*/false, |
a668c6ad MM |
2920 | /*class_head_p=*/false, |
2921 | declarator_p); | |
a723baf1 MM |
2922 | if (cp_parser_parse_definitely (parser)) |
2923 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); | |
2924 | } | |
2925 | /* In "p->S::~T", look in the scope given by "*p" as well. */ | |
2926 | else if (object_scope) | |
2927 | { | |
2928 | cp_parser_parse_tentatively (parser); | |
2929 | parser->scope = object_scope; | |
2930 | parser->object_scope = NULL_TREE; | |
2931 | parser->qualifying_scope = NULL_TREE; | |
2932 | type_decl | |
2933 | = cp_parser_class_name (parser, | |
2934 | /*typename_keyword_p=*/false, | |
2935 | /*template_keyword_p=*/false, | |
2936 | /*type_p=*/false, | |
a723baf1 | 2937 | /*check_dependency=*/false, |
a668c6ad MM |
2938 | /*class_head_p=*/false, |
2939 | declarator_p); | |
a723baf1 MM |
2940 | if (cp_parser_parse_definitely (parser)) |
2941 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); | |
2942 | } | |
2943 | /* Look in the surrounding context. */ | |
2944 | parser->scope = NULL_TREE; | |
2945 | parser->object_scope = NULL_TREE; | |
2946 | parser->qualifying_scope = NULL_TREE; | |
2947 | type_decl | |
2948 | = cp_parser_class_name (parser, | |
2949 | /*typename_keyword_p=*/false, | |
2950 | /*template_keyword_p=*/false, | |
2951 | /*type_p=*/false, | |
a723baf1 | 2952 | /*check_dependency=*/false, |
a668c6ad MM |
2953 | /*class_head_p=*/false, |
2954 | declarator_p); | |
a723baf1 MM |
2955 | /* If an error occurred, assume that the name of the |
2956 | destructor is the same as the name of the qualifying | |
2957 | class. That allows us to keep parsing after running | |
2958 | into ill-formed destructor names. */ | |
2959 | if (type_decl == error_mark_node && scope && TYPE_P (scope)) | |
2960 | return build_nt (BIT_NOT_EXPR, scope); | |
2961 | else if (type_decl == error_mark_node) | |
2962 | return error_mark_node; | |
2963 | ||
f3c2dfc6 MM |
2964 | /* [class.dtor] |
2965 | ||
2966 | A typedef-name that names a class shall not be used as the | |
2967 | identifier in the declarator for a destructor declaration. */ | |
2968 | if (declarator_p | |
2969 | && !DECL_IMPLICIT_TYPEDEF_P (type_decl) | |
2970 | && !DECL_SELF_REFERENCE_P (type_decl)) | |
2971 | error ("typedef-name `%D' used as destructor declarator", | |
2972 | type_decl); | |
2973 | ||
a723baf1 MM |
2974 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); |
2975 | } | |
2976 | ||
2977 | case CPP_KEYWORD: | |
2978 | if (token->keyword == RID_OPERATOR) | |
2979 | { | |
2980 | tree id; | |
2981 | ||
2982 | /* This could be a template-id, so we try that first. */ | |
2983 | cp_parser_parse_tentatively (parser); | |
2984 | /* Try a template-id. */ | |
2985 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
2986 | /*check_dependency_p=*/true, |
2987 | declarator_p); | |
a723baf1 MM |
2988 | /* If that worked, we're done. */ |
2989 | if (cp_parser_parse_definitely (parser)) | |
2990 | return id; | |
2991 | /* We still don't know whether we're looking at an | |
2992 | operator-function-id or a conversion-function-id. */ | |
2993 | cp_parser_parse_tentatively (parser); | |
2994 | /* Try an operator-function-id. */ | |
2995 | id = cp_parser_operator_function_id (parser); | |
2996 | /* If that didn't work, try a conversion-function-id. */ | |
2997 | if (!cp_parser_parse_definitely (parser)) | |
2998 | id = cp_parser_conversion_function_id (parser); | |
2999 | ||
3000 | return id; | |
3001 | } | |
3002 | /* Fall through. */ | |
3003 | ||
3004 | default: | |
3005 | cp_parser_error (parser, "expected unqualified-id"); | |
3006 | return error_mark_node; | |
3007 | } | |
3008 | } | |
3009 | ||
3010 | /* Parse an (optional) nested-name-specifier. | |
3011 | ||
3012 | nested-name-specifier: | |
3013 | class-or-namespace-name :: nested-name-specifier [opt] | |
3014 | class-or-namespace-name :: template nested-name-specifier [opt] | |
3015 | ||
3016 | PARSER->SCOPE should be set appropriately before this function is | |
3017 | called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in | |
3018 | effect. TYPE_P is TRUE if we non-type bindings should be ignored | |
3019 | in name lookups. | |
3020 | ||
3021 | Sets PARSER->SCOPE to the class (TYPE) or namespace | |
3022 | (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves | |
3023 | it unchanged if there is no nested-name-specifier. Returns the new | |
a668c6ad MM |
3024 | scope iff there is a nested-name-specifier, or NULL_TREE otherwise. |
3025 | ||
3026 | If IS_DECLARATION is TRUE, the nested-name-specifier is known to be | |
3027 | part of a declaration and/or decl-specifier. */ | |
a723baf1 MM |
3028 | |
3029 | static tree | |
3030 | cp_parser_nested_name_specifier_opt (cp_parser *parser, | |
3031 | bool typename_keyword_p, | |
3032 | bool check_dependency_p, | |
a668c6ad MM |
3033 | bool type_p, |
3034 | bool is_declaration) | |
a723baf1 MM |
3035 | { |
3036 | bool success = false; | |
3037 | tree access_check = NULL_TREE; | |
3038 | ptrdiff_t start; | |
2050a1bb | 3039 | cp_token* token; |
a723baf1 MM |
3040 | |
3041 | /* If the next token corresponds to a nested name specifier, there | |
2050a1bb MM |
3042 | is no need to reparse it. However, if CHECK_DEPENDENCY_P is |
3043 | false, it may have been true before, in which case something | |
3044 | like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier | |
3045 | of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when | |
3046 | CHECK_DEPENDENCY_P is false, we have to fall through into the | |
3047 | main loop. */ | |
3048 | if (check_dependency_p | |
3049 | && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER)) | |
3050 | { | |
3051 | cp_parser_pre_parsed_nested_name_specifier (parser); | |
a723baf1 MM |
3052 | return parser->scope; |
3053 | } | |
3054 | ||
3055 | /* Remember where the nested-name-specifier starts. */ | |
3056 | if (cp_parser_parsing_tentatively (parser) | |
3057 | && !cp_parser_committed_to_tentative_parse (parser)) | |
3058 | { | |
2050a1bb | 3059 | token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
3060 | start = cp_lexer_token_difference (parser->lexer, |
3061 | parser->lexer->first_token, | |
2050a1bb | 3062 | token); |
a723baf1 MM |
3063 | } |
3064 | else | |
3065 | start = -1; | |
3066 | ||
8d241e0b | 3067 | push_deferring_access_checks (dk_deferred); |
cf22909c | 3068 | |
a723baf1 MM |
3069 | while (true) |
3070 | { | |
3071 | tree new_scope; | |
3072 | tree old_scope; | |
3073 | tree saved_qualifying_scope; | |
a723baf1 MM |
3074 | bool template_keyword_p; |
3075 | ||
2050a1bb MM |
3076 | /* Spot cases that cannot be the beginning of a |
3077 | nested-name-specifier. */ | |
3078 | token = cp_lexer_peek_token (parser->lexer); | |
3079 | ||
3080 | /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process | |
3081 | the already parsed nested-name-specifier. */ | |
3082 | if (token->type == CPP_NESTED_NAME_SPECIFIER) | |
3083 | { | |
3084 | /* Grab the nested-name-specifier and continue the loop. */ | |
3085 | cp_parser_pre_parsed_nested_name_specifier (parser); | |
3086 | success = true; | |
3087 | continue; | |
3088 | } | |
3089 | ||
a723baf1 MM |
3090 | /* Spot cases that cannot be the beginning of a |
3091 | nested-name-specifier. On the second and subsequent times | |
3092 | through the loop, we look for the `template' keyword. */ | |
f7b5ecd9 | 3093 | if (success && token->keyword == RID_TEMPLATE) |
a723baf1 MM |
3094 | ; |
3095 | /* A template-id can start a nested-name-specifier. */ | |
f7b5ecd9 | 3096 | else if (token->type == CPP_TEMPLATE_ID) |
a723baf1 MM |
3097 | ; |
3098 | else | |
3099 | { | |
3100 | /* If the next token is not an identifier, then it is | |
3101 | definitely not a class-or-namespace-name. */ | |
f7b5ecd9 | 3102 | if (token->type != CPP_NAME) |
a723baf1 MM |
3103 | break; |
3104 | /* If the following token is neither a `<' (to begin a | |
3105 | template-id), nor a `::', then we are not looking at a | |
3106 | nested-name-specifier. */ | |
3107 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
3108 | if (token->type != CPP_LESS && token->type != CPP_SCOPE) | |
3109 | break; | |
3110 | } | |
3111 | ||
3112 | /* The nested-name-specifier is optional, so we parse | |
3113 | tentatively. */ | |
3114 | cp_parser_parse_tentatively (parser); | |
3115 | ||
3116 | /* Look for the optional `template' keyword, if this isn't the | |
3117 | first time through the loop. */ | |
3118 | if (success) | |
3119 | template_keyword_p = cp_parser_optional_template_keyword (parser); | |
3120 | else | |
3121 | template_keyword_p = false; | |
3122 | ||
3123 | /* Save the old scope since the name lookup we are about to do | |
3124 | might destroy it. */ | |
3125 | old_scope = parser->scope; | |
3126 | saved_qualifying_scope = parser->qualifying_scope; | |
3127 | /* Parse the qualifying entity. */ | |
3128 | new_scope | |
3129 | = cp_parser_class_or_namespace_name (parser, | |
3130 | typename_keyword_p, | |
3131 | template_keyword_p, | |
3132 | check_dependency_p, | |
a668c6ad MM |
3133 | type_p, |
3134 | is_declaration); | |
a723baf1 MM |
3135 | /* Look for the `::' token. */ |
3136 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
3137 | ||
3138 | /* If we found what we wanted, we keep going; otherwise, we're | |
3139 | done. */ | |
3140 | if (!cp_parser_parse_definitely (parser)) | |
3141 | { | |
3142 | bool error_p = false; | |
3143 | ||
3144 | /* Restore the OLD_SCOPE since it was valid before the | |
3145 | failed attempt at finding the last | |
3146 | class-or-namespace-name. */ | |
3147 | parser->scope = old_scope; | |
3148 | parser->qualifying_scope = saved_qualifying_scope; | |
3149 | /* If the next token is an identifier, and the one after | |
3150 | that is a `::', then any valid interpretation would have | |
3151 | found a class-or-namespace-name. */ | |
3152 | while (cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
3153 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
3154 | == CPP_SCOPE) | |
3155 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type | |
3156 | != CPP_COMPL)) | |
3157 | { | |
3158 | token = cp_lexer_consume_token (parser->lexer); | |
3159 | if (!error_p) | |
3160 | { | |
3161 | tree decl; | |
3162 | ||
3163 | decl = cp_parser_lookup_name_simple (parser, token->value); | |
3164 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
3165 | error ("`%D' used without template parameters", | |
3166 | decl); | |
a723baf1 | 3167 | else |
4bb8ca28 MM |
3168 | cp_parser_name_lookup_error |
3169 | (parser, token->value, decl, | |
3170 | "is not a class or namespace"); | |
a723baf1 MM |
3171 | parser->scope = NULL_TREE; |
3172 | error_p = true; | |
eea9800f MM |
3173 | /* Treat this as a successful nested-name-specifier |
3174 | due to: | |
3175 | ||
3176 | [basic.lookup.qual] | |
3177 | ||
3178 | If the name found is not a class-name (clause | |
3179 | _class_) or namespace-name (_namespace.def_), the | |
3180 | program is ill-formed. */ | |
3181 | success = true; | |
a723baf1 MM |
3182 | } |
3183 | cp_lexer_consume_token (parser->lexer); | |
3184 | } | |
3185 | break; | |
3186 | } | |
3187 | ||
3188 | /* We've found one valid nested-name-specifier. */ | |
3189 | success = true; | |
3190 | /* Make sure we look in the right scope the next time through | |
3191 | the loop. */ | |
3192 | parser->scope = (TREE_CODE (new_scope) == TYPE_DECL | |
3193 | ? TREE_TYPE (new_scope) | |
3194 | : new_scope); | |
3195 | /* If it is a class scope, try to complete it; we are about to | |
3196 | be looking up names inside the class. */ | |
8fbc5ae7 MM |
3197 | if (TYPE_P (parser->scope) |
3198 | /* Since checking types for dependency can be expensive, | |
3199 | avoid doing it if the type is already complete. */ | |
3200 | && !COMPLETE_TYPE_P (parser->scope) | |
3201 | /* Do not try to complete dependent types. */ | |
1fb3244a | 3202 | && !dependent_type_p (parser->scope)) |
a723baf1 MM |
3203 | complete_type (parser->scope); |
3204 | } | |
3205 | ||
cf22909c KL |
3206 | /* Retrieve any deferred checks. Do not pop this access checks yet |
3207 | so the memory will not be reclaimed during token replacing below. */ | |
3208 | access_check = get_deferred_access_checks (); | |
3209 | ||
a723baf1 MM |
3210 | /* If parsing tentatively, replace the sequence of tokens that makes |
3211 | up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER | |
3212 | token. That way, should we re-parse the token stream, we will | |
3213 | not have to repeat the effort required to do the parse, nor will | |
3214 | we issue duplicate error messages. */ | |
3215 | if (success && start >= 0) | |
3216 | { | |
a723baf1 MM |
3217 | /* Find the token that corresponds to the start of the |
3218 | template-id. */ | |
3219 | token = cp_lexer_advance_token (parser->lexer, | |
3220 | parser->lexer->first_token, | |
3221 | start); | |
3222 | ||
a723baf1 MM |
3223 | /* Reset the contents of the START token. */ |
3224 | token->type = CPP_NESTED_NAME_SPECIFIER; | |
3225 | token->value = build_tree_list (access_check, parser->scope); | |
3226 | TREE_TYPE (token->value) = parser->qualifying_scope; | |
3227 | token->keyword = RID_MAX; | |
3228 | /* Purge all subsequent tokens. */ | |
3229 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
3230 | } | |
3231 | ||
cf22909c | 3232 | pop_deferring_access_checks (); |
a723baf1 MM |
3233 | return success ? parser->scope : NULL_TREE; |
3234 | } | |
3235 | ||
3236 | /* Parse a nested-name-specifier. See | |
3237 | cp_parser_nested_name_specifier_opt for details. This function | |
3238 | behaves identically, except that it will an issue an error if no | |
3239 | nested-name-specifier is present, and it will return | |
3240 | ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier | |
3241 | is present. */ | |
3242 | ||
3243 | static tree | |
3244 | cp_parser_nested_name_specifier (cp_parser *parser, | |
3245 | bool typename_keyword_p, | |
3246 | bool check_dependency_p, | |
a668c6ad MM |
3247 | bool type_p, |
3248 | bool is_declaration) | |
a723baf1 MM |
3249 | { |
3250 | tree scope; | |
3251 | ||
3252 | /* Look for the nested-name-specifier. */ | |
3253 | scope = cp_parser_nested_name_specifier_opt (parser, | |
3254 | typename_keyword_p, | |
3255 | check_dependency_p, | |
a668c6ad MM |
3256 | type_p, |
3257 | is_declaration); | |
a723baf1 MM |
3258 | /* If it was not present, issue an error message. */ |
3259 | if (!scope) | |
3260 | { | |
3261 | cp_parser_error (parser, "expected nested-name-specifier"); | |
eb5abb39 | 3262 | parser->scope = NULL_TREE; |
a723baf1 MM |
3263 | return error_mark_node; |
3264 | } | |
3265 | ||
3266 | return scope; | |
3267 | } | |
3268 | ||
3269 | /* Parse a class-or-namespace-name. | |
3270 | ||
3271 | class-or-namespace-name: | |
3272 | class-name | |
3273 | namespace-name | |
3274 | ||
3275 | TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect. | |
3276 | TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect. | |
3277 | CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up. | |
3278 | TYPE_P is TRUE iff the next name should be taken as a class-name, | |
3279 | even the same name is declared to be another entity in the same | |
3280 | scope. | |
3281 | ||
3282 | Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL) | |
eea9800f MM |
3283 | specified by the class-or-namespace-name. If neither is found the |
3284 | ERROR_MARK_NODE is returned. */ | |
a723baf1 MM |
3285 | |
3286 | static tree | |
3287 | cp_parser_class_or_namespace_name (cp_parser *parser, | |
3288 | bool typename_keyword_p, | |
3289 | bool template_keyword_p, | |
3290 | bool check_dependency_p, | |
a668c6ad MM |
3291 | bool type_p, |
3292 | bool is_declaration) | |
a723baf1 MM |
3293 | { |
3294 | tree saved_scope; | |
3295 | tree saved_qualifying_scope; | |
3296 | tree saved_object_scope; | |
3297 | tree scope; | |
eea9800f | 3298 | bool only_class_p; |
a723baf1 | 3299 | |
a723baf1 MM |
3300 | /* Before we try to parse the class-name, we must save away the |
3301 | current PARSER->SCOPE since cp_parser_class_name will destroy | |
3302 | it. */ | |
3303 | saved_scope = parser->scope; | |
3304 | saved_qualifying_scope = parser->qualifying_scope; | |
3305 | saved_object_scope = parser->object_scope; | |
eea9800f MM |
3306 | /* Try for a class-name first. If the SAVED_SCOPE is a type, then |
3307 | there is no need to look for a namespace-name. */ | |
bbaab916 | 3308 | only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope)); |
eea9800f MM |
3309 | if (!only_class_p) |
3310 | cp_parser_parse_tentatively (parser); | |
a723baf1 MM |
3311 | scope = cp_parser_class_name (parser, |
3312 | typename_keyword_p, | |
3313 | template_keyword_p, | |
3314 | type_p, | |
a723baf1 | 3315 | check_dependency_p, |
a668c6ad MM |
3316 | /*class_head_p=*/false, |
3317 | is_declaration); | |
a723baf1 | 3318 | /* If that didn't work, try for a namespace-name. */ |
eea9800f | 3319 | if (!only_class_p && !cp_parser_parse_definitely (parser)) |
a723baf1 MM |
3320 | { |
3321 | /* Restore the saved scope. */ | |
3322 | parser->scope = saved_scope; | |
3323 | parser->qualifying_scope = saved_qualifying_scope; | |
3324 | parser->object_scope = saved_object_scope; | |
eea9800f MM |
3325 | /* If we are not looking at an identifier followed by the scope |
3326 | resolution operator, then this is not part of a | |
3327 | nested-name-specifier. (Note that this function is only used | |
3328 | to parse the components of a nested-name-specifier.) */ | |
3329 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME) | |
3330 | || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE) | |
3331 | return error_mark_node; | |
a723baf1 MM |
3332 | scope = cp_parser_namespace_name (parser); |
3333 | } | |
3334 | ||
3335 | return scope; | |
3336 | } | |
3337 | ||
3338 | /* Parse a postfix-expression. | |
3339 | ||
3340 | postfix-expression: | |
3341 | primary-expression | |
3342 | postfix-expression [ expression ] | |
3343 | postfix-expression ( expression-list [opt] ) | |
3344 | simple-type-specifier ( expression-list [opt] ) | |
3345 | typename :: [opt] nested-name-specifier identifier | |
3346 | ( expression-list [opt] ) | |
3347 | typename :: [opt] nested-name-specifier template [opt] template-id | |
3348 | ( expression-list [opt] ) | |
3349 | postfix-expression . template [opt] id-expression | |
3350 | postfix-expression -> template [opt] id-expression | |
3351 | postfix-expression . pseudo-destructor-name | |
3352 | postfix-expression -> pseudo-destructor-name | |
3353 | postfix-expression ++ | |
3354 | postfix-expression -- | |
3355 | dynamic_cast < type-id > ( expression ) | |
3356 | static_cast < type-id > ( expression ) | |
3357 | reinterpret_cast < type-id > ( expression ) | |
3358 | const_cast < type-id > ( expression ) | |
3359 | typeid ( expression ) | |
3360 | typeid ( type-id ) | |
3361 | ||
3362 | GNU Extension: | |
3363 | ||
3364 | postfix-expression: | |
3365 | ( type-id ) { initializer-list , [opt] } | |
3366 | ||
3367 | This extension is a GNU version of the C99 compound-literal | |
3368 | construct. (The C99 grammar uses `type-name' instead of `type-id', | |
3369 | but they are essentially the same concept.) | |
3370 | ||
3371 | If ADDRESS_P is true, the postfix expression is the operand of the | |
3372 | `&' operator. | |
3373 | ||
3374 | Returns a representation of the expression. */ | |
3375 | ||
3376 | static tree | |
3377 | cp_parser_postfix_expression (cp_parser *parser, bool address_p) | |
3378 | { | |
3379 | cp_token *token; | |
3380 | enum rid keyword; | |
b3445994 | 3381 | cp_id_kind idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3382 | tree postfix_expression = NULL_TREE; |
3383 | /* Non-NULL only if the current postfix-expression can be used to | |
3384 | form a pointer-to-member. In that case, QUALIFYING_CLASS is the | |
3385 | class used to qualify the member. */ | |
3386 | tree qualifying_class = NULL_TREE; | |
a723baf1 MM |
3387 | |
3388 | /* Peek at the next token. */ | |
3389 | token = cp_lexer_peek_token (parser->lexer); | |
3390 | /* Some of the productions are determined by keywords. */ | |
3391 | keyword = token->keyword; | |
3392 | switch (keyword) | |
3393 | { | |
3394 | case RID_DYNCAST: | |
3395 | case RID_STATCAST: | |
3396 | case RID_REINTCAST: | |
3397 | case RID_CONSTCAST: | |
3398 | { | |
3399 | tree type; | |
3400 | tree expression; | |
3401 | const char *saved_message; | |
3402 | ||
3403 | /* All of these can be handled in the same way from the point | |
3404 | of view of parsing. Begin by consuming the token | |
3405 | identifying the cast. */ | |
3406 | cp_lexer_consume_token (parser->lexer); | |
3407 | ||
3408 | /* New types cannot be defined in the cast. */ | |
3409 | saved_message = parser->type_definition_forbidden_message; | |
3410 | parser->type_definition_forbidden_message | |
3411 | = "types may not be defined in casts"; | |
3412 | ||
3413 | /* Look for the opening `<'. */ | |
3414 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
3415 | /* Parse the type to which we are casting. */ | |
3416 | type = cp_parser_type_id (parser); | |
3417 | /* Look for the closing `>'. */ | |
3418 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
3419 | /* Restore the old message. */ | |
3420 | parser->type_definition_forbidden_message = saved_message; | |
3421 | ||
3422 | /* And the expression which is being cast. */ | |
3423 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
3424 | expression = cp_parser_expression (parser); | |
3425 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3426 | ||
14d22dd6 MM |
3427 | /* Only type conversions to integral or enumeration types |
3428 | can be used in constant-expressions. */ | |
67c03833 | 3429 | if (parser->integral_constant_expression_p |
14d22dd6 | 3430 | && !dependent_type_p (type) |
263ee052 MM |
3431 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
3432 | /* A cast to pointer or reference type is allowed in the | |
3433 | implementation of "offsetof". */ | |
3434 | && !(parser->in_offsetof_p && POINTER_TYPE_P (type))) | |
14d22dd6 | 3435 | { |
67c03833 JM |
3436 | if (!parser->allow_non_integral_constant_expression_p) |
3437 | return (cp_parser_non_integral_constant_expression | |
14d22dd6 MM |
3438 | ("a cast to a type other than an integral or " |
3439 | "enumeration type")); | |
67c03833 | 3440 | parser->non_integral_constant_expression_p = true; |
14d22dd6 MM |
3441 | } |
3442 | ||
a723baf1 MM |
3443 | switch (keyword) |
3444 | { | |
3445 | case RID_DYNCAST: | |
3446 | postfix_expression | |
3447 | = build_dynamic_cast (type, expression); | |
3448 | break; | |
3449 | case RID_STATCAST: | |
3450 | postfix_expression | |
3451 | = build_static_cast (type, expression); | |
3452 | break; | |
3453 | case RID_REINTCAST: | |
3454 | postfix_expression | |
3455 | = build_reinterpret_cast (type, expression); | |
3456 | break; | |
3457 | case RID_CONSTCAST: | |
3458 | postfix_expression | |
3459 | = build_const_cast (type, expression); | |
3460 | break; | |
3461 | default: | |
3462 | abort (); | |
3463 | } | |
3464 | } | |
3465 | break; | |
3466 | ||
3467 | case RID_TYPEID: | |
3468 | { | |
3469 | tree type; | |
3470 | const char *saved_message; | |
3471 | ||
3472 | /* Consume the `typeid' token. */ | |
3473 | cp_lexer_consume_token (parser->lexer); | |
3474 | /* Look for the `(' token. */ | |
3475 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
3476 | /* Types cannot be defined in a `typeid' expression. */ | |
3477 | saved_message = parser->type_definition_forbidden_message; | |
3478 | parser->type_definition_forbidden_message | |
3479 | = "types may not be defined in a `typeid\' expression"; | |
3480 | /* We can't be sure yet whether we're looking at a type-id or an | |
3481 | expression. */ | |
3482 | cp_parser_parse_tentatively (parser); | |
3483 | /* Try a type-id first. */ | |
3484 | type = cp_parser_type_id (parser); | |
3485 | /* Look for the `)' token. Otherwise, we can't be sure that | |
3486 | we're not looking at an expression: consider `typeid (int | |
3487 | (3))', for example. */ | |
3488 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3489 | /* If all went well, simply lookup the type-id. */ | |
3490 | if (cp_parser_parse_definitely (parser)) | |
3491 | postfix_expression = get_typeid (type); | |
3492 | /* Otherwise, fall back to the expression variant. */ | |
3493 | else | |
3494 | { | |
3495 | tree expression; | |
3496 | ||
3497 | /* Look for an expression. */ | |
3498 | expression = cp_parser_expression (parser); | |
3499 | /* Compute its typeid. */ | |
3500 | postfix_expression = build_typeid (expression); | |
3501 | /* Look for the `)' token. */ | |
3502 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3503 | } | |
3504 | ||
3505 | /* Restore the saved message. */ | |
3506 | parser->type_definition_forbidden_message = saved_message; | |
3507 | } | |
3508 | break; | |
3509 | ||
3510 | case RID_TYPENAME: | |
3511 | { | |
3512 | bool template_p = false; | |
3513 | tree id; | |
3514 | tree type; | |
3515 | ||
3516 | /* Consume the `typename' token. */ | |
3517 | cp_lexer_consume_token (parser->lexer); | |
3518 | /* Look for the optional `::' operator. */ | |
3519 | cp_parser_global_scope_opt (parser, | |
3520 | /*current_scope_valid_p=*/false); | |
3521 | /* Look for the nested-name-specifier. */ | |
3522 | cp_parser_nested_name_specifier (parser, | |
3523 | /*typename_keyword_p=*/true, | |
3524 | /*check_dependency_p=*/true, | |
a668c6ad MM |
3525 | /*type_p=*/true, |
3526 | /*is_declaration=*/true); | |
a723baf1 MM |
3527 | /* Look for the optional `template' keyword. */ |
3528 | template_p = cp_parser_optional_template_keyword (parser); | |
3529 | /* We don't know whether we're looking at a template-id or an | |
3530 | identifier. */ | |
3531 | cp_parser_parse_tentatively (parser); | |
3532 | /* Try a template-id. */ | |
3533 | id = cp_parser_template_id (parser, template_p, | |
a668c6ad MM |
3534 | /*check_dependency_p=*/true, |
3535 | /*is_declaration=*/true); | |
a723baf1 MM |
3536 | /* If that didn't work, try an identifier. */ |
3537 | if (!cp_parser_parse_definitely (parser)) | |
3538 | id = cp_parser_identifier (parser); | |
3539 | /* Create a TYPENAME_TYPE to represent the type to which the | |
3540 | functional cast is being performed. */ | |
3541 | type = make_typename_type (parser->scope, id, | |
3542 | /*complain=*/1); | |
3543 | ||
3544 | postfix_expression = cp_parser_functional_cast (parser, type); | |
3545 | } | |
3546 | break; | |
3547 | ||
3548 | default: | |
3549 | { | |
3550 | tree type; | |
3551 | ||
3552 | /* If the next thing is a simple-type-specifier, we may be | |
3553 | looking at a functional cast. We could also be looking at | |
3554 | an id-expression. So, we try the functional cast, and if | |
3555 | that doesn't work we fall back to the primary-expression. */ | |
3556 | cp_parser_parse_tentatively (parser); | |
3557 | /* Look for the simple-type-specifier. */ | |
3558 | type = cp_parser_simple_type_specifier (parser, | |
4b0d3cbe MM |
3559 | CP_PARSER_FLAGS_NONE, |
3560 | /*identifier_p=*/false); | |
a723baf1 MM |
3561 | /* Parse the cast itself. */ |
3562 | if (!cp_parser_error_occurred (parser)) | |
3563 | postfix_expression | |
3564 | = cp_parser_functional_cast (parser, type); | |
3565 | /* If that worked, we're done. */ | |
3566 | if (cp_parser_parse_definitely (parser)) | |
3567 | break; | |
3568 | ||
3569 | /* If the functional-cast didn't work out, try a | |
3570 | compound-literal. */ | |
14d22dd6 MM |
3571 | if (cp_parser_allow_gnu_extensions_p (parser) |
3572 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
a723baf1 MM |
3573 | { |
3574 | tree initializer_list = NULL_TREE; | |
3575 | ||
3576 | cp_parser_parse_tentatively (parser); | |
14d22dd6 MM |
3577 | /* Consume the `('. */ |
3578 | cp_lexer_consume_token (parser->lexer); | |
3579 | /* Parse the type. */ | |
3580 | type = cp_parser_type_id (parser); | |
3581 | /* Look for the `)'. */ | |
3582 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3583 | /* Look for the `{'. */ | |
3584 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
3585 | /* If things aren't going well, there's no need to | |
3586 | keep going. */ | |
3587 | if (!cp_parser_error_occurred (parser)) | |
a723baf1 | 3588 | { |
39703eb9 | 3589 | bool non_constant_p; |
14d22dd6 MM |
3590 | /* Parse the initializer-list. */ |
3591 | initializer_list | |
39703eb9 | 3592 | = cp_parser_initializer_list (parser, &non_constant_p); |
14d22dd6 MM |
3593 | /* Allow a trailing `,'. */ |
3594 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
3595 | cp_lexer_consume_token (parser->lexer); | |
3596 | /* Look for the final `}'. */ | |
3597 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
a723baf1 MM |
3598 | } |
3599 | /* If that worked, we're definitely looking at a | |
3600 | compound-literal expression. */ | |
3601 | if (cp_parser_parse_definitely (parser)) | |
3602 | { | |
3603 | /* Warn the user that a compound literal is not | |
3604 | allowed in standard C++. */ | |
3605 | if (pedantic) | |
3606 | pedwarn ("ISO C++ forbids compound-literals"); | |
3607 | /* Form the representation of the compound-literal. */ | |
3608 | postfix_expression | |
3609 | = finish_compound_literal (type, initializer_list); | |
3610 | break; | |
3611 | } | |
3612 | } | |
3613 | ||
3614 | /* It must be a primary-expression. */ | |
3615 | postfix_expression = cp_parser_primary_expression (parser, | |
3616 | &idk, | |
3617 | &qualifying_class); | |
3618 | } | |
3619 | break; | |
3620 | } | |
3621 | ||
ee76b931 MM |
3622 | /* If we were avoiding committing to the processing of a |
3623 | qualified-id until we knew whether or not we had a | |
3624 | pointer-to-member, we now know. */ | |
089d6ea7 | 3625 | if (qualifying_class) |
a723baf1 | 3626 | { |
ee76b931 | 3627 | bool done; |
a723baf1 | 3628 | |
ee76b931 MM |
3629 | /* Peek at the next token. */ |
3630 | token = cp_lexer_peek_token (parser->lexer); | |
3631 | done = (token->type != CPP_OPEN_SQUARE | |
3632 | && token->type != CPP_OPEN_PAREN | |
3633 | && token->type != CPP_DOT | |
3634 | && token->type != CPP_DEREF | |
3635 | && token->type != CPP_PLUS_PLUS | |
3636 | && token->type != CPP_MINUS_MINUS); | |
3637 | ||
3638 | postfix_expression = finish_qualified_id_expr (qualifying_class, | |
3639 | postfix_expression, | |
3640 | done, | |
3641 | address_p); | |
3642 | if (done) | |
3643 | return postfix_expression; | |
a723baf1 MM |
3644 | } |
3645 | ||
a723baf1 MM |
3646 | /* Keep looping until the postfix-expression is complete. */ |
3647 | while (true) | |
3648 | { | |
10b1d5e7 MM |
3649 | if (idk == CP_ID_KIND_UNQUALIFIED |
3650 | && TREE_CODE (postfix_expression) == IDENTIFIER_NODE | |
a723baf1 | 3651 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) |
b3445994 MM |
3652 | /* It is not a Koenig lookup function call. */ |
3653 | postfix_expression | |
3654 | = unqualified_name_lookup_error (postfix_expression); | |
a723baf1 MM |
3655 | |
3656 | /* Peek at the next token. */ | |
3657 | token = cp_lexer_peek_token (parser->lexer); | |
3658 | ||
3659 | switch (token->type) | |
3660 | { | |
3661 | case CPP_OPEN_SQUARE: | |
3662 | /* postfix-expression [ expression ] */ | |
3663 | { | |
3664 | tree index; | |
3665 | ||
3666 | /* Consume the `[' token. */ | |
3667 | cp_lexer_consume_token (parser->lexer); | |
3668 | /* Parse the index expression. */ | |
3669 | index = cp_parser_expression (parser); | |
3670 | /* Look for the closing `]'. */ | |
3671 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
3672 | ||
3673 | /* Build the ARRAY_REF. */ | |
3674 | postfix_expression | |
3675 | = grok_array_decl (postfix_expression, index); | |
b3445994 | 3676 | idk = CP_ID_KIND_NONE; |
a5ac3982 MM |
3677 | /* Array references are not permitted in |
3678 | constant-expressions. */ | |
67c03833 | 3679 | if (parser->integral_constant_expression_p) |
a5ac3982 | 3680 | { |
67c03833 | 3681 | if (!parser->allow_non_integral_constant_expression_p) |
a5ac3982 | 3682 | postfix_expression |
67c03833 JM |
3683 | = cp_parser_non_integral_constant_expression ("an array reference"); |
3684 | parser->non_integral_constant_expression_p = true; | |
a5ac3982 | 3685 | } |
a723baf1 MM |
3686 | } |
3687 | break; | |
3688 | ||
3689 | case CPP_OPEN_PAREN: | |
3690 | /* postfix-expression ( expression-list [opt] ) */ | |
3691 | { | |
6d80c4b9 | 3692 | bool koenig_p; |
39703eb9 MM |
3693 | tree args = (cp_parser_parenthesized_expression_list |
3694 | (parser, false, /*non_constant_p=*/NULL)); | |
a723baf1 | 3695 | |
7efa3e22 NS |
3696 | if (args == error_mark_node) |
3697 | { | |
3698 | postfix_expression = error_mark_node; | |
3699 | break; | |
3700 | } | |
3701 | ||
14d22dd6 MM |
3702 | /* Function calls are not permitted in |
3703 | constant-expressions. */ | |
67c03833 | 3704 | if (parser->integral_constant_expression_p) |
14d22dd6 | 3705 | { |
67c03833 | 3706 | if (!parser->allow_non_integral_constant_expression_p) |
a5ac3982 MM |
3707 | { |
3708 | postfix_expression | |
67c03833 | 3709 | = cp_parser_non_integral_constant_expression ("a function call"); |
a5ac3982 MM |
3710 | break; |
3711 | } | |
67c03833 | 3712 | parser->non_integral_constant_expression_p = true; |
14d22dd6 | 3713 | } |
a723baf1 | 3714 | |
6d80c4b9 | 3715 | koenig_p = false; |
399dedb9 NS |
3716 | if (idk == CP_ID_KIND_UNQUALIFIED) |
3717 | { | |
3718 | if (args | |
3719 | && (is_overloaded_fn (postfix_expression) | |
3720 | || DECL_P (postfix_expression) | |
3721 | || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)) | |
6d80c4b9 MM |
3722 | { |
3723 | koenig_p = true; | |
3724 | postfix_expression | |
3725 | = perform_koenig_lookup (postfix_expression, args); | |
3726 | } | |
399dedb9 NS |
3727 | else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE) |
3728 | postfix_expression | |
3729 | = unqualified_fn_lookup_error (postfix_expression); | |
3730 | } | |
3731 | ||
d17811fd | 3732 | if (TREE_CODE (postfix_expression) == COMPONENT_REF) |
a723baf1 | 3733 | { |
d17811fd MM |
3734 | tree instance = TREE_OPERAND (postfix_expression, 0); |
3735 | tree fn = TREE_OPERAND (postfix_expression, 1); | |
3736 | ||
3737 | if (processing_template_decl | |
3738 | && (type_dependent_expression_p (instance) | |
3739 | || (!BASELINK_P (fn) | |
3740 | && TREE_CODE (fn) != FIELD_DECL) | |
584672ee | 3741 | || type_dependent_expression_p (fn) |
d17811fd MM |
3742 | || any_type_dependent_arguments_p (args))) |
3743 | { | |
3744 | postfix_expression | |
3745 | = build_min_nt (CALL_EXPR, postfix_expression, args); | |
3746 | break; | |
3747 | } | |
3748 | ||
3749 | postfix_expression | |
3750 | = (build_new_method_call | |
3751 | (instance, fn, args, NULL_TREE, | |
b3445994 | 3752 | (idk == CP_ID_KIND_QUALIFIED |
d17811fd | 3753 | ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL))); |
a723baf1 | 3754 | } |
d17811fd MM |
3755 | else if (TREE_CODE (postfix_expression) == OFFSET_REF |
3756 | || TREE_CODE (postfix_expression) == MEMBER_REF | |
3757 | || TREE_CODE (postfix_expression) == DOTSTAR_EXPR) | |
a723baf1 MM |
3758 | postfix_expression = (build_offset_ref_call_from_tree |
3759 | (postfix_expression, args)); | |
b3445994 | 3760 | else if (idk == CP_ID_KIND_QUALIFIED) |
2050a1bb MM |
3761 | /* A call to a static class member, or a namespace-scope |
3762 | function. */ | |
3763 | postfix_expression | |
3764 | = finish_call_expr (postfix_expression, args, | |
6d80c4b9 MM |
3765 | /*disallow_virtual=*/true, |
3766 | koenig_p); | |
a723baf1 | 3767 | else |
2050a1bb MM |
3768 | /* All other function calls. */ |
3769 | postfix_expression | |
3770 | = finish_call_expr (postfix_expression, args, | |
6d80c4b9 MM |
3771 | /*disallow_virtual=*/false, |
3772 | koenig_p); | |
a723baf1 MM |
3773 | |
3774 | /* The POSTFIX_EXPRESSION is certainly no longer an id. */ | |
b3445994 | 3775 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3776 | } |
3777 | break; | |
3778 | ||
3779 | case CPP_DOT: | |
3780 | case CPP_DEREF: | |
3781 | /* postfix-expression . template [opt] id-expression | |
3782 | postfix-expression . pseudo-destructor-name | |
3783 | postfix-expression -> template [opt] id-expression | |
3784 | postfix-expression -> pseudo-destructor-name */ | |
3785 | { | |
3786 | tree name; | |
3787 | bool dependent_p; | |
3788 | bool template_p; | |
3789 | tree scope = NULL_TREE; | |
a5ac3982 | 3790 | enum cpp_ttype token_type = token->type; |
a723baf1 MM |
3791 | |
3792 | /* If this is a `->' operator, dereference the pointer. */ | |
3793 | if (token->type == CPP_DEREF) | |
3794 | postfix_expression = build_x_arrow (postfix_expression); | |
3795 | /* Check to see whether or not the expression is | |
3796 | type-dependent. */ | |
bbaab916 | 3797 | dependent_p = type_dependent_expression_p (postfix_expression); |
a723baf1 MM |
3798 | /* The identifier following the `->' or `.' is not |
3799 | qualified. */ | |
3800 | parser->scope = NULL_TREE; | |
3801 | parser->qualifying_scope = NULL_TREE; | |
3802 | parser->object_scope = NULL_TREE; | |
b3445994 | 3803 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3804 | /* Enter the scope corresponding to the type of the object |
3805 | given by the POSTFIX_EXPRESSION. */ | |
3806 | if (!dependent_p | |
3807 | && TREE_TYPE (postfix_expression) != NULL_TREE) | |
3808 | { | |
3809 | scope = TREE_TYPE (postfix_expression); | |
3810 | /* According to the standard, no expression should | |
3811 | ever have reference type. Unfortunately, we do not | |
3812 | currently match the standard in this respect in | |
3813 | that our internal representation of an expression | |
3814 | may have reference type even when the standard says | |
3815 | it does not. Therefore, we have to manually obtain | |
3816 | the underlying type here. */ | |
ee76b931 | 3817 | scope = non_reference (scope); |
a723baf1 MM |
3818 | /* The type of the POSTFIX_EXPRESSION must be |
3819 | complete. */ | |
3820 | scope = complete_type_or_else (scope, NULL_TREE); | |
3821 | /* Let the name lookup machinery know that we are | |
3822 | processing a class member access expression. */ | |
3823 | parser->context->object_type = scope; | |
3824 | /* If something went wrong, we want to be able to | |
3825 | discern that case, as opposed to the case where | |
3826 | there was no SCOPE due to the type of expression | |
3827 | being dependent. */ | |
3828 | if (!scope) | |
3829 | scope = error_mark_node; | |
3830 | } | |
3831 | ||
3832 | /* Consume the `.' or `->' operator. */ | |
3833 | cp_lexer_consume_token (parser->lexer); | |
3834 | /* If the SCOPE is not a scalar type, we are looking at an | |
3835 | ordinary class member access expression, rather than a | |
3836 | pseudo-destructor-name. */ | |
3837 | if (!scope || !SCALAR_TYPE_P (scope)) | |
3838 | { | |
3839 | template_p = cp_parser_optional_template_keyword (parser); | |
3840 | /* Parse the id-expression. */ | |
3841 | name = cp_parser_id_expression (parser, | |
3842 | template_p, | |
3843 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
3844 | /*template_p=*/NULL, |
3845 | /*declarator_p=*/false); | |
a723baf1 MM |
3846 | /* In general, build a SCOPE_REF if the member name is |
3847 | qualified. However, if the name was not dependent | |
3848 | and has already been resolved; there is no need to | |
3849 | build the SCOPE_REF. For example; | |
3850 | ||
3851 | struct X { void f(); }; | |
3852 | template <typename T> void f(T* t) { t->X::f(); } | |
3853 | ||
d17811fd MM |
3854 | Even though "t" is dependent, "X::f" is not and has |
3855 | been resolved to a BASELINK; there is no need to | |
a723baf1 | 3856 | include scope information. */ |
a6bd211d JM |
3857 | |
3858 | /* But we do need to remember that there was an explicit | |
3859 | scope for virtual function calls. */ | |
3860 | if (parser->scope) | |
b3445994 | 3861 | idk = CP_ID_KIND_QUALIFIED; |
a6bd211d | 3862 | |
a723baf1 MM |
3863 | if (name != error_mark_node |
3864 | && !BASELINK_P (name) | |
3865 | && parser->scope) | |
3866 | { | |
3867 | name = build_nt (SCOPE_REF, parser->scope, name); | |
3868 | parser->scope = NULL_TREE; | |
3869 | parser->qualifying_scope = NULL_TREE; | |
3870 | parser->object_scope = NULL_TREE; | |
3871 | } | |
3872 | postfix_expression | |
3873 | = finish_class_member_access_expr (postfix_expression, name); | |
3874 | } | |
3875 | /* Otherwise, try the pseudo-destructor-name production. */ | |
3876 | else | |
3877 | { | |
90808894 | 3878 | tree s = NULL_TREE; |
a723baf1 MM |
3879 | tree type; |
3880 | ||
3881 | /* Parse the pseudo-destructor-name. */ | |
3882 | cp_parser_pseudo_destructor_name (parser, &s, &type); | |
3883 | /* Form the call. */ | |
3884 | postfix_expression | |
3885 | = finish_pseudo_destructor_expr (postfix_expression, | |
3886 | s, TREE_TYPE (type)); | |
3887 | } | |
3888 | ||
3889 | /* We no longer need to look up names in the scope of the | |
3890 | object on the left-hand side of the `.' or `->' | |
3891 | operator. */ | |
3892 | parser->context->object_type = NULL_TREE; | |
a5ac3982 | 3893 | /* These operators may not appear in constant-expressions. */ |
67c03833 | 3894 | if (parser->integral_constant_expression_p |
263ee052 MM |
3895 | /* The "->" operator is allowed in the implementation |
3896 | of "offsetof". */ | |
3897 | && !(parser->in_offsetof_p && token_type == CPP_DEREF)) | |
a5ac3982 | 3898 | { |
67c03833 | 3899 | if (!parser->allow_non_integral_constant_expression_p) |
a5ac3982 | 3900 | postfix_expression |
67c03833 | 3901 | = (cp_parser_non_integral_constant_expression |
a5ac3982 | 3902 | (token_type == CPP_DEREF ? "'->'" : "`.'")); |
67c03833 | 3903 | parser->non_integral_constant_expression_p = true; |
a5ac3982 | 3904 | } |
a723baf1 MM |
3905 | } |
3906 | break; | |
3907 | ||
3908 | case CPP_PLUS_PLUS: | |
3909 | /* postfix-expression ++ */ | |
3910 | /* Consume the `++' token. */ | |
3911 | cp_lexer_consume_token (parser->lexer); | |
a5ac3982 MM |
3912 | /* Generate a representation for the complete expression. */ |
3913 | postfix_expression | |
3914 | = finish_increment_expr (postfix_expression, | |
3915 | POSTINCREMENT_EXPR); | |
14d22dd6 | 3916 | /* Increments may not appear in constant-expressions. */ |
67c03833 | 3917 | if (parser->integral_constant_expression_p) |
14d22dd6 | 3918 | { |
67c03833 | 3919 | if (!parser->allow_non_integral_constant_expression_p) |
a5ac3982 | 3920 | postfix_expression |
67c03833 JM |
3921 | = cp_parser_non_integral_constant_expression ("an increment"); |
3922 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 3923 | } |
b3445994 | 3924 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3925 | break; |
3926 | ||
3927 | case CPP_MINUS_MINUS: | |
3928 | /* postfix-expression -- */ | |
3929 | /* Consume the `--' token. */ | |
3930 | cp_lexer_consume_token (parser->lexer); | |
a5ac3982 MM |
3931 | /* Generate a representation for the complete expression. */ |
3932 | postfix_expression | |
3933 | = finish_increment_expr (postfix_expression, | |
3934 | POSTDECREMENT_EXPR); | |
14d22dd6 | 3935 | /* Decrements may not appear in constant-expressions. */ |
67c03833 | 3936 | if (parser->integral_constant_expression_p) |
14d22dd6 | 3937 | { |
67c03833 | 3938 | if (!parser->allow_non_integral_constant_expression_p) |
a5ac3982 | 3939 | postfix_expression |
67c03833 JM |
3940 | = cp_parser_non_integral_constant_expression ("a decrement"); |
3941 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 3942 | } |
b3445994 | 3943 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3944 | break; |
3945 | ||
3946 | default: | |
3947 | return postfix_expression; | |
3948 | } | |
3949 | } | |
3950 | ||
3951 | /* We should never get here. */ | |
3952 | abort (); | |
3953 | return error_mark_node; | |
3954 | } | |
3955 | ||
7efa3e22 | 3956 | /* Parse a parenthesized expression-list. |
a723baf1 MM |
3957 | |
3958 | expression-list: | |
3959 | assignment-expression | |
3960 | expression-list, assignment-expression | |
3961 | ||
7efa3e22 NS |
3962 | attribute-list: |
3963 | expression-list | |
3964 | identifier | |
3965 | identifier, expression-list | |
3966 | ||
a723baf1 MM |
3967 | Returns a TREE_LIST. The TREE_VALUE of each node is a |
3968 | representation of an assignment-expression. Note that a TREE_LIST | |
7efa3e22 NS |
3969 | is returned even if there is only a single expression in the list. |
3970 | error_mark_node is returned if the ( and or ) are | |
3971 | missing. NULL_TREE is returned on no expressions. The parentheses | |
3972 | are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute | |
39703eb9 MM |
3973 | list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P |
3974 | indicates whether or not all of the expressions in the list were | |
3975 | constant. */ | |
a723baf1 MM |
3976 | |
3977 | static tree | |
39703eb9 MM |
3978 | cp_parser_parenthesized_expression_list (cp_parser* parser, |
3979 | bool is_attribute_list, | |
3980 | bool *non_constant_p) | |
a723baf1 MM |
3981 | { |
3982 | tree expression_list = NULL_TREE; | |
7efa3e22 | 3983 | tree identifier = NULL_TREE; |
39703eb9 MM |
3984 | |
3985 | /* Assume all the expressions will be constant. */ | |
3986 | if (non_constant_p) | |
3987 | *non_constant_p = false; | |
3988 | ||
7efa3e22 NS |
3989 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) |
3990 | return error_mark_node; | |
3991 | ||
a723baf1 | 3992 | /* Consume expressions until there are no more. */ |
7efa3e22 NS |
3993 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) |
3994 | while (true) | |
3995 | { | |
3996 | tree expr; | |
3997 | ||
3998 | /* At the beginning of attribute lists, check to see if the | |
3999 | next token is an identifier. */ | |
4000 | if (is_attribute_list | |
4001 | && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME) | |
4002 | { | |
4003 | cp_token *token; | |
4004 | ||
4005 | /* Consume the identifier. */ | |
4006 | token = cp_lexer_consume_token (parser->lexer); | |
4007 | /* Save the identifier. */ | |
4008 | identifier = token->value; | |
4009 | } | |
4010 | else | |
4011 | { | |
4012 | /* Parse the next assignment-expression. */ | |
39703eb9 MM |
4013 | if (non_constant_p) |
4014 | { | |
4015 | bool expr_non_constant_p; | |
4016 | expr = (cp_parser_constant_expression | |
4017 | (parser, /*allow_non_constant_p=*/true, | |
4018 | &expr_non_constant_p)); | |
4019 | if (expr_non_constant_p) | |
4020 | *non_constant_p = true; | |
4021 | } | |
4022 | else | |
4023 | expr = cp_parser_assignment_expression (parser); | |
a723baf1 | 4024 | |
7efa3e22 NS |
4025 | /* Add it to the list. We add error_mark_node |
4026 | expressions to the list, so that we can still tell if | |
4027 | the correct form for a parenthesized expression-list | |
4028 | is found. That gives better errors. */ | |
4029 | expression_list = tree_cons (NULL_TREE, expr, expression_list); | |
a723baf1 | 4030 | |
7efa3e22 NS |
4031 | if (expr == error_mark_node) |
4032 | goto skip_comma; | |
4033 | } | |
a723baf1 | 4034 | |
7efa3e22 NS |
4035 | /* After the first item, attribute lists look the same as |
4036 | expression lists. */ | |
4037 | is_attribute_list = false; | |
4038 | ||
4039 | get_comma:; | |
4040 | /* If the next token isn't a `,', then we are done. */ | |
4041 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
4042 | break; | |
4043 | ||
4044 | /* Otherwise, consume the `,' and keep going. */ | |
4045 | cp_lexer_consume_token (parser->lexer); | |
4046 | } | |
4047 | ||
4048 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
4049 | { | |
4050 | int ending; | |
4051 | ||
4052 | skip_comma:; | |
4053 | /* We try and resync to an unnested comma, as that will give the | |
4054 | user better diagnostics. */ | |
4bb8ca28 MM |
4055 | ending = cp_parser_skip_to_closing_parenthesis (parser, |
4056 | /*recovering=*/true, | |
4057 | /*or_comma=*/true, | |
a668c6ad | 4058 | /*consume_paren=*/true); |
7efa3e22 NS |
4059 | if (ending < 0) |
4060 | goto get_comma; | |
4061 | if (!ending) | |
4062 | return error_mark_node; | |
a723baf1 MM |
4063 | } |
4064 | ||
4065 | /* We built up the list in reverse order so we must reverse it now. */ | |
7efa3e22 NS |
4066 | expression_list = nreverse (expression_list); |
4067 | if (identifier) | |
4068 | expression_list = tree_cons (NULL_TREE, identifier, expression_list); | |
4069 | ||
4070 | return expression_list; | |
a723baf1 MM |
4071 | } |
4072 | ||
4073 | /* Parse a pseudo-destructor-name. | |
4074 | ||
4075 | pseudo-destructor-name: | |
4076 | :: [opt] nested-name-specifier [opt] type-name :: ~ type-name | |
4077 | :: [opt] nested-name-specifier template template-id :: ~ type-name | |
4078 | :: [opt] nested-name-specifier [opt] ~ type-name | |
4079 | ||
4080 | If either of the first two productions is used, sets *SCOPE to the | |
4081 | TYPE specified before the final `::'. Otherwise, *SCOPE is set to | |
4082 | NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name, | |
4083 | or ERROR_MARK_NODE if no type-name is present. */ | |
4084 | ||
4085 | static void | |
94edc4ab NN |
4086 | cp_parser_pseudo_destructor_name (cp_parser* parser, |
4087 | tree* scope, | |
4088 | tree* type) | |
a723baf1 MM |
4089 | { |
4090 | bool nested_name_specifier_p; | |
4091 | ||
4092 | /* Look for the optional `::' operator. */ | |
4093 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true); | |
4094 | /* Look for the optional nested-name-specifier. */ | |
4095 | nested_name_specifier_p | |
4096 | = (cp_parser_nested_name_specifier_opt (parser, | |
4097 | /*typename_keyword_p=*/false, | |
4098 | /*check_dependency_p=*/true, | |
a668c6ad MM |
4099 | /*type_p=*/false, |
4100 | /*is_declaration=*/true) | |
a723baf1 MM |
4101 | != NULL_TREE); |
4102 | /* Now, if we saw a nested-name-specifier, we might be doing the | |
4103 | second production. */ | |
4104 | if (nested_name_specifier_p | |
4105 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
4106 | { | |
4107 | /* Consume the `template' keyword. */ | |
4108 | cp_lexer_consume_token (parser->lexer); | |
4109 | /* Parse the template-id. */ | |
4110 | cp_parser_template_id (parser, | |
4111 | /*template_keyword_p=*/true, | |
a668c6ad MM |
4112 | /*check_dependency_p=*/false, |
4113 | /*is_declaration=*/true); | |
a723baf1 MM |
4114 | /* Look for the `::' token. */ |
4115 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
4116 | } | |
4117 | /* If the next token is not a `~', then there might be some | |
9bcb9aae | 4118 | additional qualification. */ |
a723baf1 MM |
4119 | else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL)) |
4120 | { | |
4121 | /* Look for the type-name. */ | |
4122 | *scope = TREE_TYPE (cp_parser_type_name (parser)); | |
4123 | /* Look for the `::' token. */ | |
4124 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
4125 | } | |
4126 | else | |
4127 | *scope = NULL_TREE; | |
4128 | ||
4129 | /* Look for the `~'. */ | |
4130 | cp_parser_require (parser, CPP_COMPL, "`~'"); | |
4131 | /* Look for the type-name again. We are not responsible for | |
4132 | checking that it matches the first type-name. */ | |
4133 | *type = cp_parser_type_name (parser); | |
4134 | } | |
4135 | ||
4136 | /* Parse a unary-expression. | |
4137 | ||
4138 | unary-expression: | |
4139 | postfix-expression | |
4140 | ++ cast-expression | |
4141 | -- cast-expression | |
4142 | unary-operator cast-expression | |
4143 | sizeof unary-expression | |
4144 | sizeof ( type-id ) | |
4145 | new-expression | |
4146 | delete-expression | |
4147 | ||
4148 | GNU Extensions: | |
4149 | ||
4150 | unary-expression: | |
4151 | __extension__ cast-expression | |
4152 | __alignof__ unary-expression | |
4153 | __alignof__ ( type-id ) | |
4154 | __real__ cast-expression | |
4155 | __imag__ cast-expression | |
4156 | && identifier | |
4157 | ||
4158 | ADDRESS_P is true iff the unary-expression is appearing as the | |
4159 | operand of the `&' operator. | |
4160 | ||
34cd5ae7 | 4161 | Returns a representation of the expression. */ |
a723baf1 MM |
4162 | |
4163 | static tree | |
4164 | cp_parser_unary_expression (cp_parser *parser, bool address_p) | |
4165 | { | |
4166 | cp_token *token; | |
4167 | enum tree_code unary_operator; | |
4168 | ||
4169 | /* Peek at the next token. */ | |
4170 | token = cp_lexer_peek_token (parser->lexer); | |
4171 | /* Some keywords give away the kind of expression. */ | |
4172 | if (token->type == CPP_KEYWORD) | |
4173 | { | |
4174 | enum rid keyword = token->keyword; | |
4175 | ||
4176 | switch (keyword) | |
4177 | { | |
4178 | case RID_ALIGNOF: | |
a723baf1 MM |
4179 | case RID_SIZEOF: |
4180 | { | |
4181 | tree operand; | |
7a18b933 | 4182 | enum tree_code op; |
a723baf1 | 4183 | |
7a18b933 NS |
4184 | op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR; |
4185 | /* Consume the token. */ | |
a723baf1 MM |
4186 | cp_lexer_consume_token (parser->lexer); |
4187 | /* Parse the operand. */ | |
4188 | operand = cp_parser_sizeof_operand (parser, keyword); | |
4189 | ||
7a18b933 NS |
4190 | if (TYPE_P (operand)) |
4191 | return cxx_sizeof_or_alignof_type (operand, op, true); | |
a723baf1 | 4192 | else |
7a18b933 | 4193 | return cxx_sizeof_or_alignof_expr (operand, op); |
a723baf1 MM |
4194 | } |
4195 | ||
4196 | case RID_NEW: | |
4197 | return cp_parser_new_expression (parser); | |
4198 | ||
4199 | case RID_DELETE: | |
4200 | return cp_parser_delete_expression (parser); | |
4201 | ||
4202 | case RID_EXTENSION: | |
4203 | { | |
4204 | /* The saved value of the PEDANTIC flag. */ | |
4205 | int saved_pedantic; | |
4206 | tree expr; | |
4207 | ||
4208 | /* Save away the PEDANTIC flag. */ | |
4209 | cp_parser_extension_opt (parser, &saved_pedantic); | |
4210 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4211 | expr = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4212 | /* Restore the PEDANTIC flag. */ |
4213 | pedantic = saved_pedantic; | |
4214 | ||
4215 | return expr; | |
4216 | } | |
4217 | ||
4218 | case RID_REALPART: | |
4219 | case RID_IMAGPART: | |
4220 | { | |
4221 | tree expression; | |
4222 | ||
4223 | /* Consume the `__real__' or `__imag__' token. */ | |
4224 | cp_lexer_consume_token (parser->lexer); | |
4225 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4226 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4227 | /* Create the complete representation. */ |
4228 | return build_x_unary_op ((keyword == RID_REALPART | |
4229 | ? REALPART_EXPR : IMAGPART_EXPR), | |
4230 | expression); | |
4231 | } | |
4232 | break; | |
4233 | ||
4234 | default: | |
4235 | break; | |
4236 | } | |
4237 | } | |
4238 | ||
4239 | /* Look for the `:: new' and `:: delete', which also signal the | |
4240 | beginning of a new-expression, or delete-expression, | |
4241 | respectively. If the next token is `::', then it might be one of | |
4242 | these. */ | |
4243 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
4244 | { | |
4245 | enum rid keyword; | |
4246 | ||
4247 | /* See if the token after the `::' is one of the keywords in | |
4248 | which we're interested. */ | |
4249 | keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword; | |
4250 | /* If it's `new', we have a new-expression. */ | |
4251 | if (keyword == RID_NEW) | |
4252 | return cp_parser_new_expression (parser); | |
4253 | /* Similarly, for `delete'. */ | |
4254 | else if (keyword == RID_DELETE) | |
4255 | return cp_parser_delete_expression (parser); | |
4256 | } | |
4257 | ||
4258 | /* Look for a unary operator. */ | |
4259 | unary_operator = cp_parser_unary_operator (token); | |
4260 | /* The `++' and `--' operators can be handled similarly, even though | |
4261 | they are not technically unary-operators in the grammar. */ | |
4262 | if (unary_operator == ERROR_MARK) | |
4263 | { | |
4264 | if (token->type == CPP_PLUS_PLUS) | |
4265 | unary_operator = PREINCREMENT_EXPR; | |
4266 | else if (token->type == CPP_MINUS_MINUS) | |
4267 | unary_operator = PREDECREMENT_EXPR; | |
4268 | /* Handle the GNU address-of-label extension. */ | |
4269 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
4270 | && token->type == CPP_AND_AND) | |
4271 | { | |
4272 | tree identifier; | |
4273 | ||
4274 | /* Consume the '&&' token. */ | |
4275 | cp_lexer_consume_token (parser->lexer); | |
4276 | /* Look for the identifier. */ | |
4277 | identifier = cp_parser_identifier (parser); | |
4278 | /* Create an expression representing the address. */ | |
4279 | return finish_label_address_expr (identifier); | |
4280 | } | |
4281 | } | |
4282 | if (unary_operator != ERROR_MARK) | |
4283 | { | |
4284 | tree cast_expression; | |
a5ac3982 MM |
4285 | tree expression = error_mark_node; |
4286 | const char *non_constant_p = NULL; | |
a723baf1 MM |
4287 | |
4288 | /* Consume the operator token. */ | |
4289 | token = cp_lexer_consume_token (parser->lexer); | |
4290 | /* Parse the cast-expression. */ | |
4291 | cast_expression | |
4292 | = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR); | |
4293 | /* Now, build an appropriate representation. */ | |
4294 | switch (unary_operator) | |
4295 | { | |
4296 | case INDIRECT_REF: | |
a5ac3982 MM |
4297 | non_constant_p = "`*'"; |
4298 | expression = build_x_indirect_ref (cast_expression, "unary *"); | |
4299 | break; | |
4300 | ||
a723baf1 | 4301 | case ADDR_EXPR: |
263ee052 MM |
4302 | /* The "&" operator is allowed in the implementation of |
4303 | "offsetof". */ | |
4304 | if (!parser->in_offsetof_p) | |
4305 | non_constant_p = "`&'"; | |
a5ac3982 | 4306 | /* Fall through. */ |
d17811fd | 4307 | case BIT_NOT_EXPR: |
a5ac3982 MM |
4308 | expression = build_x_unary_op (unary_operator, cast_expression); |
4309 | break; | |
4310 | ||
14d22dd6 MM |
4311 | case PREINCREMENT_EXPR: |
4312 | case PREDECREMENT_EXPR: | |
a5ac3982 MM |
4313 | non_constant_p = (unary_operator == PREINCREMENT_EXPR |
4314 | ? "`++'" : "`--'"); | |
14d22dd6 | 4315 | /* Fall through. */ |
a723baf1 MM |
4316 | case CONVERT_EXPR: |
4317 | case NEGATE_EXPR: | |
4318 | case TRUTH_NOT_EXPR: | |
a5ac3982 MM |
4319 | expression = finish_unary_op_expr (unary_operator, cast_expression); |
4320 | break; | |
a723baf1 | 4321 | |
a723baf1 MM |
4322 | default: |
4323 | abort (); | |
a723baf1 | 4324 | } |
a5ac3982 | 4325 | |
67c03833 | 4326 | if (non_constant_p && parser->integral_constant_expression_p) |
a5ac3982 | 4327 | { |
67c03833 JM |
4328 | if (!parser->allow_non_integral_constant_expression_p) |
4329 | return cp_parser_non_integral_constant_expression (non_constant_p); | |
4330 | parser->non_integral_constant_expression_p = true; | |
a5ac3982 MM |
4331 | } |
4332 | ||
4333 | return expression; | |
a723baf1 MM |
4334 | } |
4335 | ||
4336 | return cp_parser_postfix_expression (parser, address_p); | |
4337 | } | |
4338 | ||
4339 | /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a | |
4340 | unary-operator, the corresponding tree code is returned. */ | |
4341 | ||
4342 | static enum tree_code | |
94edc4ab | 4343 | cp_parser_unary_operator (cp_token* token) |
a723baf1 MM |
4344 | { |
4345 | switch (token->type) | |
4346 | { | |
4347 | case CPP_MULT: | |
4348 | return INDIRECT_REF; | |
4349 | ||
4350 | case CPP_AND: | |
4351 | return ADDR_EXPR; | |
4352 | ||
4353 | case CPP_PLUS: | |
4354 | return CONVERT_EXPR; | |
4355 | ||
4356 | case CPP_MINUS: | |
4357 | return NEGATE_EXPR; | |
4358 | ||
4359 | case CPP_NOT: | |
4360 | return TRUTH_NOT_EXPR; | |
4361 | ||
4362 | case CPP_COMPL: | |
4363 | return BIT_NOT_EXPR; | |
4364 | ||
4365 | default: | |
4366 | return ERROR_MARK; | |
4367 | } | |
4368 | } | |
4369 | ||
4370 | /* Parse a new-expression. | |
4371 | ||
ca099ac8 | 4372 | new-expression: |
a723baf1 MM |
4373 | :: [opt] new new-placement [opt] new-type-id new-initializer [opt] |
4374 | :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt] | |
4375 | ||
4376 | Returns a representation of the expression. */ | |
4377 | ||
4378 | static tree | |
94edc4ab | 4379 | cp_parser_new_expression (cp_parser* parser) |
a723baf1 MM |
4380 | { |
4381 | bool global_scope_p; | |
4382 | tree placement; | |
4383 | tree type; | |
4384 | tree initializer; | |
4385 | ||
4386 | /* Look for the optional `::' operator. */ | |
4387 | global_scope_p | |
4388 | = (cp_parser_global_scope_opt (parser, | |
4389 | /*current_scope_valid_p=*/false) | |
4390 | != NULL_TREE); | |
4391 | /* Look for the `new' operator. */ | |
4392 | cp_parser_require_keyword (parser, RID_NEW, "`new'"); | |
4393 | /* There's no easy way to tell a new-placement from the | |
4394 | `( type-id )' construct. */ | |
4395 | cp_parser_parse_tentatively (parser); | |
4396 | /* Look for a new-placement. */ | |
4397 | placement = cp_parser_new_placement (parser); | |
4398 | /* If that didn't work out, there's no new-placement. */ | |
4399 | if (!cp_parser_parse_definitely (parser)) | |
4400 | placement = NULL_TREE; | |
4401 | ||
4402 | /* If the next token is a `(', then we have a parenthesized | |
4403 | type-id. */ | |
4404 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4405 | { | |
4406 | /* Consume the `('. */ | |
4407 | cp_lexer_consume_token (parser->lexer); | |
4408 | /* Parse the type-id. */ | |
4409 | type = cp_parser_type_id (parser); | |
4410 | /* Look for the closing `)'. */ | |
4411 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
4412 | } | |
4413 | /* Otherwise, there must be a new-type-id. */ | |
4414 | else | |
4415 | type = cp_parser_new_type_id (parser); | |
4416 | ||
4417 | /* If the next token is a `(', then we have a new-initializer. */ | |
4418 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4419 | initializer = cp_parser_new_initializer (parser); | |
4420 | else | |
4421 | initializer = NULL_TREE; | |
4422 | ||
4423 | /* Create a representation of the new-expression. */ | |
4424 | return build_new (placement, type, initializer, global_scope_p); | |
4425 | } | |
4426 | ||
4427 | /* Parse a new-placement. | |
4428 | ||
4429 | new-placement: | |
4430 | ( expression-list ) | |
4431 | ||
4432 | Returns the same representation as for an expression-list. */ | |
4433 | ||
4434 | static tree | |
94edc4ab | 4435 | cp_parser_new_placement (cp_parser* parser) |
a723baf1 MM |
4436 | { |
4437 | tree expression_list; | |
4438 | ||
a723baf1 | 4439 | /* Parse the expression-list. */ |
39703eb9 MM |
4440 | expression_list = (cp_parser_parenthesized_expression_list |
4441 | (parser, false, /*non_constant_p=*/NULL)); | |
a723baf1 MM |
4442 | |
4443 | return expression_list; | |
4444 | } | |
4445 | ||
4446 | /* Parse a new-type-id. | |
4447 | ||
4448 | new-type-id: | |
4449 | type-specifier-seq new-declarator [opt] | |
4450 | ||
4451 | Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq, | |
4452 | and whose TREE_VALUE is the new-declarator. */ | |
4453 | ||
4454 | static tree | |
94edc4ab | 4455 | cp_parser_new_type_id (cp_parser* parser) |
a723baf1 MM |
4456 | { |
4457 | tree type_specifier_seq; | |
4458 | tree declarator; | |
4459 | const char *saved_message; | |
4460 | ||
4461 | /* The type-specifier sequence must not contain type definitions. | |
4462 | (It cannot contain declarations of new types either, but if they | |
4463 | are not definitions we will catch that because they are not | |
4464 | complete.) */ | |
4465 | saved_message = parser->type_definition_forbidden_message; | |
4466 | parser->type_definition_forbidden_message | |
4467 | = "types may not be defined in a new-type-id"; | |
4468 | /* Parse the type-specifier-seq. */ | |
4469 | type_specifier_seq = cp_parser_type_specifier_seq (parser); | |
4470 | /* Restore the old message. */ | |
4471 | parser->type_definition_forbidden_message = saved_message; | |
4472 | /* Parse the new-declarator. */ | |
4473 | declarator = cp_parser_new_declarator_opt (parser); | |
4474 | ||
4475 | return build_tree_list (type_specifier_seq, declarator); | |
4476 | } | |
4477 | ||
4478 | /* Parse an (optional) new-declarator. | |
4479 | ||
4480 | new-declarator: | |
4481 | ptr-operator new-declarator [opt] | |
4482 | direct-new-declarator | |
4483 | ||
4484 | Returns a representation of the declarator. See | |
4485 | cp_parser_declarator for the representations used. */ | |
4486 | ||
4487 | static tree | |
94edc4ab | 4488 | cp_parser_new_declarator_opt (cp_parser* parser) |
a723baf1 MM |
4489 | { |
4490 | enum tree_code code; | |
4491 | tree type; | |
4492 | tree cv_qualifier_seq; | |
4493 | ||
4494 | /* We don't know if there's a ptr-operator next, or not. */ | |
4495 | cp_parser_parse_tentatively (parser); | |
4496 | /* Look for a ptr-operator. */ | |
4497 | code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq); | |
4498 | /* If that worked, look for more new-declarators. */ | |
4499 | if (cp_parser_parse_definitely (parser)) | |
4500 | { | |
4501 | tree declarator; | |
4502 | ||
4503 | /* Parse another optional declarator. */ | |
4504 | declarator = cp_parser_new_declarator_opt (parser); | |
4505 | ||
4506 | /* Create the representation of the declarator. */ | |
4507 | if (code == INDIRECT_REF) | |
4508 | declarator = make_pointer_declarator (cv_qualifier_seq, | |
4509 | declarator); | |
4510 | else | |
4511 | declarator = make_reference_declarator (cv_qualifier_seq, | |
4512 | declarator); | |
4513 | ||
4514 | /* Handle the pointer-to-member case. */ | |
4515 | if (type) | |
4516 | declarator = build_nt (SCOPE_REF, type, declarator); | |
4517 | ||
4518 | return declarator; | |
4519 | } | |
4520 | ||
4521 | /* If the next token is a `[', there is a direct-new-declarator. */ | |
4522 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
4523 | return cp_parser_direct_new_declarator (parser); | |
4524 | ||
4525 | return NULL_TREE; | |
4526 | } | |
4527 | ||
4528 | /* Parse a direct-new-declarator. | |
4529 | ||
4530 | direct-new-declarator: | |
4531 | [ expression ] | |
4532 | direct-new-declarator [constant-expression] | |
4533 | ||
4534 | Returns an ARRAY_REF, following the same conventions as are | |
4535 | documented for cp_parser_direct_declarator. */ | |
4536 | ||
4537 | static tree | |
94edc4ab | 4538 | cp_parser_direct_new_declarator (cp_parser* parser) |
a723baf1 MM |
4539 | { |
4540 | tree declarator = NULL_TREE; | |
4541 | ||
4542 | while (true) | |
4543 | { | |
4544 | tree expression; | |
4545 | ||
4546 | /* Look for the opening `['. */ | |
4547 | cp_parser_require (parser, CPP_OPEN_SQUARE, "`['"); | |
4548 | /* The first expression is not required to be constant. */ | |
4549 | if (!declarator) | |
4550 | { | |
4551 | expression = cp_parser_expression (parser); | |
4552 | /* The standard requires that the expression have integral | |
4553 | type. DR 74 adds enumeration types. We believe that the | |
4554 | real intent is that these expressions be handled like the | |
4555 | expression in a `switch' condition, which also allows | |
4556 | classes with a single conversion to integral or | |
4557 | enumeration type. */ | |
4558 | if (!processing_template_decl) | |
4559 | { | |
4560 | expression | |
4561 | = build_expr_type_conversion (WANT_INT | WANT_ENUM, | |
4562 | expression, | |
b746c5dc | 4563 | /*complain=*/true); |
a723baf1 MM |
4564 | if (!expression) |
4565 | { | |
4566 | error ("expression in new-declarator must have integral or enumeration type"); | |
4567 | expression = error_mark_node; | |
4568 | } | |
4569 | } | |
4570 | } | |
4571 | /* But all the other expressions must be. */ | |
4572 | else | |
14d22dd6 MM |
4573 | expression |
4574 | = cp_parser_constant_expression (parser, | |
4575 | /*allow_non_constant=*/false, | |
4576 | NULL); | |
a723baf1 MM |
4577 | /* Look for the closing `]'. */ |
4578 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
4579 | ||
4580 | /* Add this bound to the declarator. */ | |
4581 | declarator = build_nt (ARRAY_REF, declarator, expression); | |
4582 | ||
4583 | /* If the next token is not a `[', then there are no more | |
4584 | bounds. */ | |
4585 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) | |
4586 | break; | |
4587 | } | |
4588 | ||
4589 | return declarator; | |
4590 | } | |
4591 | ||
4592 | /* Parse a new-initializer. | |
4593 | ||
4594 | new-initializer: | |
4595 | ( expression-list [opt] ) | |
4596 | ||
34cd5ae7 | 4597 | Returns a representation of the expression-list. If there is no |
a723baf1 MM |
4598 | expression-list, VOID_ZERO_NODE is returned. */ |
4599 | ||
4600 | static tree | |
94edc4ab | 4601 | cp_parser_new_initializer (cp_parser* parser) |
a723baf1 MM |
4602 | { |
4603 | tree expression_list; | |
4604 | ||
39703eb9 MM |
4605 | expression_list = (cp_parser_parenthesized_expression_list |
4606 | (parser, false, /*non_constant_p=*/NULL)); | |
7efa3e22 | 4607 | if (!expression_list) |
a723baf1 | 4608 | expression_list = void_zero_node; |
a723baf1 MM |
4609 | |
4610 | return expression_list; | |
4611 | } | |
4612 | ||
4613 | /* Parse a delete-expression. | |
4614 | ||
4615 | delete-expression: | |
4616 | :: [opt] delete cast-expression | |
4617 | :: [opt] delete [ ] cast-expression | |
4618 | ||
4619 | Returns a representation of the expression. */ | |
4620 | ||
4621 | static tree | |
94edc4ab | 4622 | cp_parser_delete_expression (cp_parser* parser) |
a723baf1 MM |
4623 | { |
4624 | bool global_scope_p; | |
4625 | bool array_p; | |
4626 | tree expression; | |
4627 | ||
4628 | /* Look for the optional `::' operator. */ | |
4629 | global_scope_p | |
4630 | = (cp_parser_global_scope_opt (parser, | |
4631 | /*current_scope_valid_p=*/false) | |
4632 | != NULL_TREE); | |
4633 | /* Look for the `delete' keyword. */ | |
4634 | cp_parser_require_keyword (parser, RID_DELETE, "`delete'"); | |
4635 | /* See if the array syntax is in use. */ | |
4636 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
4637 | { | |
4638 | /* Consume the `[' token. */ | |
4639 | cp_lexer_consume_token (parser->lexer); | |
4640 | /* Look for the `]' token. */ | |
4641 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
4642 | /* Remember that this is the `[]' construct. */ | |
4643 | array_p = true; | |
4644 | } | |
4645 | else | |
4646 | array_p = false; | |
4647 | ||
4648 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4649 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4650 | |
4651 | return delete_sanity (expression, NULL_TREE, array_p, global_scope_p); | |
4652 | } | |
4653 | ||
4654 | /* Parse a cast-expression. | |
4655 | ||
4656 | cast-expression: | |
4657 | unary-expression | |
4658 | ( type-id ) cast-expression | |
4659 | ||
4660 | Returns a representation of the expression. */ | |
4661 | ||
4662 | static tree | |
4663 | cp_parser_cast_expression (cp_parser *parser, bool address_p) | |
4664 | { | |
4665 | /* If it's a `(', then we might be looking at a cast. */ | |
4666 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4667 | { | |
4668 | tree type = NULL_TREE; | |
4669 | tree expr = NULL_TREE; | |
4670 | bool compound_literal_p; | |
4671 | const char *saved_message; | |
4672 | ||
4673 | /* There's no way to know yet whether or not this is a cast. | |
4674 | For example, `(int (3))' is a unary-expression, while `(int) | |
4675 | 3' is a cast. So, we resort to parsing tentatively. */ | |
4676 | cp_parser_parse_tentatively (parser); | |
4677 | /* Types may not be defined in a cast. */ | |
4678 | saved_message = parser->type_definition_forbidden_message; | |
4679 | parser->type_definition_forbidden_message | |
4680 | = "types may not be defined in casts"; | |
4681 | /* Consume the `('. */ | |
4682 | cp_lexer_consume_token (parser->lexer); | |
4683 | /* A very tricky bit is that `(struct S) { 3 }' is a | |
4684 | compound-literal (which we permit in C++ as an extension). | |
4685 | But, that construct is not a cast-expression -- it is a | |
4686 | postfix-expression. (The reason is that `(struct S) { 3 }.i' | |
4687 | is legal; if the compound-literal were a cast-expression, | |
4688 | you'd need an extra set of parentheses.) But, if we parse | |
4689 | the type-id, and it happens to be a class-specifier, then we | |
4690 | will commit to the parse at that point, because we cannot | |
4691 | undo the action that is done when creating a new class. So, | |
4692 | then we cannot back up and do a postfix-expression. | |
4693 | ||
4694 | Therefore, we scan ahead to the closing `)', and check to see | |
4695 | if the token after the `)' is a `{'. If so, we are not | |
4696 | looking at a cast-expression. | |
4697 | ||
4698 | Save tokens so that we can put them back. */ | |
4699 | cp_lexer_save_tokens (parser->lexer); | |
4700 | /* Skip tokens until the next token is a closing parenthesis. | |
4701 | If we find the closing `)', and the next token is a `{', then | |
4702 | we are looking at a compound-literal. */ | |
4703 | compound_literal_p | |
a668c6ad MM |
4704 | = (cp_parser_skip_to_closing_parenthesis (parser, false, false, |
4705 | /*consume_paren=*/true) | |
a723baf1 MM |
4706 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)); |
4707 | /* Roll back the tokens we skipped. */ | |
4708 | cp_lexer_rollback_tokens (parser->lexer); | |
4709 | /* If we were looking at a compound-literal, simulate an error | |
4710 | so that the call to cp_parser_parse_definitely below will | |
4711 | fail. */ | |
4712 | if (compound_literal_p) | |
4713 | cp_parser_simulate_error (parser); | |
4714 | else | |
4715 | { | |
4716 | /* Look for the type-id. */ | |
4717 | type = cp_parser_type_id (parser); | |
4718 | /* Look for the closing `)'. */ | |
4719 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
4720 | } | |
4721 | ||
4722 | /* Restore the saved message. */ | |
4723 | parser->type_definition_forbidden_message = saved_message; | |
4724 | ||
bbaab916 NS |
4725 | /* If ok so far, parse the dependent expression. We cannot be |
4726 | sure it is a cast. Consider `(T ())'. It is a parenthesized | |
4727 | ctor of T, but looks like a cast to function returning T | |
4728 | without a dependent expression. */ | |
4729 | if (!cp_parser_error_occurred (parser)) | |
d6b4ea85 | 4730 | expr = cp_parser_simple_cast_expression (parser); |
bbaab916 | 4731 | |
a723baf1 MM |
4732 | if (cp_parser_parse_definitely (parser)) |
4733 | { | |
a723baf1 MM |
4734 | /* Warn about old-style casts, if so requested. */ |
4735 | if (warn_old_style_cast | |
4736 | && !in_system_header | |
4737 | && !VOID_TYPE_P (type) | |
4738 | && current_lang_name != lang_name_c) | |
4739 | warning ("use of old-style cast"); | |
14d22dd6 MM |
4740 | |
4741 | /* Only type conversions to integral or enumeration types | |
4742 | can be used in constant-expressions. */ | |
67c03833 | 4743 | if (parser->integral_constant_expression_p |
14d22dd6 MM |
4744 | && !dependent_type_p (type) |
4745 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)) | |
4746 | { | |
67c03833 JM |
4747 | if (!parser->allow_non_integral_constant_expression_p) |
4748 | return (cp_parser_non_integral_constant_expression | |
14d22dd6 MM |
4749 | ("a casts to a type other than an integral or " |
4750 | "enumeration type")); | |
67c03833 | 4751 | parser->non_integral_constant_expression_p = true; |
14d22dd6 | 4752 | } |
a723baf1 MM |
4753 | /* Perform the cast. */ |
4754 | expr = build_c_cast (type, expr); | |
bbaab916 | 4755 | return expr; |
a723baf1 | 4756 | } |
a723baf1 MM |
4757 | } |
4758 | ||
4759 | /* If we get here, then it's not a cast, so it must be a | |
4760 | unary-expression. */ | |
4761 | return cp_parser_unary_expression (parser, address_p); | |
4762 | } | |
4763 | ||
4764 | /* Parse a pm-expression. | |
4765 | ||
4766 | pm-expression: | |
4767 | cast-expression | |
4768 | pm-expression .* cast-expression | |
4769 | pm-expression ->* cast-expression | |
4770 | ||
4771 | Returns a representation of the expression. */ | |
4772 | ||
4773 | static tree | |
94edc4ab | 4774 | cp_parser_pm_expression (cp_parser* parser) |
a723baf1 | 4775 | { |
d6b4ea85 MM |
4776 | static const cp_parser_token_tree_map map = { |
4777 | { CPP_DEREF_STAR, MEMBER_REF }, | |
4778 | { CPP_DOT_STAR, DOTSTAR_EXPR }, | |
4779 | { CPP_EOF, ERROR_MARK } | |
4780 | }; | |
a723baf1 | 4781 | |
d6b4ea85 MM |
4782 | return cp_parser_binary_expression (parser, map, |
4783 | cp_parser_simple_cast_expression); | |
a723baf1 MM |
4784 | } |
4785 | ||
4786 | /* Parse a multiplicative-expression. | |
4787 | ||
4788 | mulitplicative-expression: | |
4789 | pm-expression | |
4790 | multiplicative-expression * pm-expression | |
4791 | multiplicative-expression / pm-expression | |
4792 | multiplicative-expression % pm-expression | |
4793 | ||
4794 | Returns a representation of the expression. */ | |
4795 | ||
4796 | static tree | |
94edc4ab | 4797 | cp_parser_multiplicative_expression (cp_parser* parser) |
a723baf1 | 4798 | { |
39b1af70 | 4799 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4800 | { CPP_MULT, MULT_EXPR }, |
4801 | { CPP_DIV, TRUNC_DIV_EXPR }, | |
4802 | { CPP_MOD, TRUNC_MOD_EXPR }, | |
4803 | { CPP_EOF, ERROR_MARK } | |
4804 | }; | |
4805 | ||
4806 | return cp_parser_binary_expression (parser, | |
4807 | map, | |
4808 | cp_parser_pm_expression); | |
4809 | } | |
4810 | ||
4811 | /* Parse an additive-expression. | |
4812 | ||
4813 | additive-expression: | |
4814 | multiplicative-expression | |
4815 | additive-expression + multiplicative-expression | |
4816 | additive-expression - multiplicative-expression | |
4817 | ||
4818 | Returns a representation of the expression. */ | |
4819 | ||
4820 | static tree | |
94edc4ab | 4821 | cp_parser_additive_expression (cp_parser* parser) |
a723baf1 | 4822 | { |
39b1af70 | 4823 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4824 | { CPP_PLUS, PLUS_EXPR }, |
4825 | { CPP_MINUS, MINUS_EXPR }, | |
4826 | { CPP_EOF, ERROR_MARK } | |
4827 | }; | |
4828 | ||
4829 | return cp_parser_binary_expression (parser, | |
4830 | map, | |
4831 | cp_parser_multiplicative_expression); | |
4832 | } | |
4833 | ||
4834 | /* Parse a shift-expression. | |
4835 | ||
4836 | shift-expression: | |
4837 | additive-expression | |
4838 | shift-expression << additive-expression | |
4839 | shift-expression >> additive-expression | |
4840 | ||
4841 | Returns a representation of the expression. */ | |
4842 | ||
4843 | static tree | |
94edc4ab | 4844 | cp_parser_shift_expression (cp_parser* parser) |
a723baf1 | 4845 | { |
39b1af70 | 4846 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4847 | { CPP_LSHIFT, LSHIFT_EXPR }, |
4848 | { CPP_RSHIFT, RSHIFT_EXPR }, | |
4849 | { CPP_EOF, ERROR_MARK } | |
4850 | }; | |
4851 | ||
4852 | return cp_parser_binary_expression (parser, | |
4853 | map, | |
4854 | cp_parser_additive_expression); | |
4855 | } | |
4856 | ||
4857 | /* Parse a relational-expression. | |
4858 | ||
4859 | relational-expression: | |
4860 | shift-expression | |
4861 | relational-expression < shift-expression | |
4862 | relational-expression > shift-expression | |
4863 | relational-expression <= shift-expression | |
4864 | relational-expression >= shift-expression | |
4865 | ||
4866 | GNU Extension: | |
4867 | ||
4868 | relational-expression: | |
4869 | relational-expression <? shift-expression | |
4870 | relational-expression >? shift-expression | |
4871 | ||
4872 | Returns a representation of the expression. */ | |
4873 | ||
4874 | static tree | |
94edc4ab | 4875 | cp_parser_relational_expression (cp_parser* parser) |
a723baf1 | 4876 | { |
39b1af70 | 4877 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4878 | { CPP_LESS, LT_EXPR }, |
4879 | { CPP_GREATER, GT_EXPR }, | |
4880 | { CPP_LESS_EQ, LE_EXPR }, | |
4881 | { CPP_GREATER_EQ, GE_EXPR }, | |
4882 | { CPP_MIN, MIN_EXPR }, | |
4883 | { CPP_MAX, MAX_EXPR }, | |
4884 | { CPP_EOF, ERROR_MARK } | |
4885 | }; | |
4886 | ||
4887 | return cp_parser_binary_expression (parser, | |
4888 | map, | |
4889 | cp_parser_shift_expression); | |
4890 | } | |
4891 | ||
4892 | /* Parse an equality-expression. | |
4893 | ||
4894 | equality-expression: | |
4895 | relational-expression | |
4896 | equality-expression == relational-expression | |
4897 | equality-expression != relational-expression | |
4898 | ||
4899 | Returns a representation of the expression. */ | |
4900 | ||
4901 | static tree | |
94edc4ab | 4902 | cp_parser_equality_expression (cp_parser* parser) |
a723baf1 | 4903 | { |
39b1af70 | 4904 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4905 | { CPP_EQ_EQ, EQ_EXPR }, |
4906 | { CPP_NOT_EQ, NE_EXPR }, | |
4907 | { CPP_EOF, ERROR_MARK } | |
4908 | }; | |
4909 | ||
4910 | return cp_parser_binary_expression (parser, | |
4911 | map, | |
4912 | cp_parser_relational_expression); | |
4913 | } | |
4914 | ||
4915 | /* Parse an and-expression. | |
4916 | ||
4917 | and-expression: | |
4918 | equality-expression | |
4919 | and-expression & equality-expression | |
4920 | ||
4921 | Returns a representation of the expression. */ | |
4922 | ||
4923 | static tree | |
94edc4ab | 4924 | cp_parser_and_expression (cp_parser* parser) |
a723baf1 | 4925 | { |
39b1af70 | 4926 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4927 | { CPP_AND, BIT_AND_EXPR }, |
4928 | { CPP_EOF, ERROR_MARK } | |
4929 | }; | |
4930 | ||
4931 | return cp_parser_binary_expression (parser, | |
4932 | map, | |
4933 | cp_parser_equality_expression); | |
4934 | } | |
4935 | ||
4936 | /* Parse an exclusive-or-expression. | |
4937 | ||
4938 | exclusive-or-expression: | |
4939 | and-expression | |
4940 | exclusive-or-expression ^ and-expression | |
4941 | ||
4942 | Returns a representation of the expression. */ | |
4943 | ||
4944 | static tree | |
94edc4ab | 4945 | cp_parser_exclusive_or_expression (cp_parser* parser) |
a723baf1 | 4946 | { |
39b1af70 | 4947 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4948 | { CPP_XOR, BIT_XOR_EXPR }, |
4949 | { CPP_EOF, ERROR_MARK } | |
4950 | }; | |
4951 | ||
4952 | return cp_parser_binary_expression (parser, | |
4953 | map, | |
4954 | cp_parser_and_expression); | |
4955 | } | |
4956 | ||
4957 | ||
4958 | /* Parse an inclusive-or-expression. | |
4959 | ||
4960 | inclusive-or-expression: | |
4961 | exclusive-or-expression | |
4962 | inclusive-or-expression | exclusive-or-expression | |
4963 | ||
4964 | Returns a representation of the expression. */ | |
4965 | ||
4966 | static tree | |
94edc4ab | 4967 | cp_parser_inclusive_or_expression (cp_parser* parser) |
a723baf1 | 4968 | { |
39b1af70 | 4969 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4970 | { CPP_OR, BIT_IOR_EXPR }, |
4971 | { CPP_EOF, ERROR_MARK } | |
4972 | }; | |
4973 | ||
4974 | return cp_parser_binary_expression (parser, | |
4975 | map, | |
4976 | cp_parser_exclusive_or_expression); | |
4977 | } | |
4978 | ||
4979 | /* Parse a logical-and-expression. | |
4980 | ||
4981 | logical-and-expression: | |
4982 | inclusive-or-expression | |
4983 | logical-and-expression && inclusive-or-expression | |
4984 | ||
4985 | Returns a representation of the expression. */ | |
4986 | ||
4987 | static tree | |
94edc4ab | 4988 | cp_parser_logical_and_expression (cp_parser* parser) |
a723baf1 | 4989 | { |
39b1af70 | 4990 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4991 | { CPP_AND_AND, TRUTH_ANDIF_EXPR }, |
4992 | { CPP_EOF, ERROR_MARK } | |
4993 | }; | |
4994 | ||
4995 | return cp_parser_binary_expression (parser, | |
4996 | map, | |
4997 | cp_parser_inclusive_or_expression); | |
4998 | } | |
4999 | ||
5000 | /* Parse a logical-or-expression. | |
5001 | ||
5002 | logical-or-expression: | |
34cd5ae7 | 5003 | logical-and-expression |
a723baf1 MM |
5004 | logical-or-expression || logical-and-expression |
5005 | ||
5006 | Returns a representation of the expression. */ | |
5007 | ||
5008 | static tree | |
94edc4ab | 5009 | cp_parser_logical_or_expression (cp_parser* parser) |
a723baf1 | 5010 | { |
39b1af70 | 5011 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
5012 | { CPP_OR_OR, TRUTH_ORIF_EXPR }, |
5013 | { CPP_EOF, ERROR_MARK } | |
5014 | }; | |
5015 | ||
5016 | return cp_parser_binary_expression (parser, | |
5017 | map, | |
5018 | cp_parser_logical_and_expression); | |
5019 | } | |
5020 | ||
a723baf1 MM |
5021 | /* Parse the `? expression : assignment-expression' part of a |
5022 | conditional-expression. The LOGICAL_OR_EXPR is the | |
5023 | logical-or-expression that started the conditional-expression. | |
5024 | Returns a representation of the entire conditional-expression. | |
5025 | ||
39703eb9 | 5026 | This routine is used by cp_parser_assignment_expression. |
a723baf1 MM |
5027 | |
5028 | ? expression : assignment-expression | |
5029 | ||
5030 | GNU Extensions: | |
5031 | ||
5032 | ? : assignment-expression */ | |
5033 | ||
5034 | static tree | |
94edc4ab | 5035 | cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr) |
a723baf1 MM |
5036 | { |
5037 | tree expr; | |
5038 | tree assignment_expr; | |
5039 | ||
5040 | /* Consume the `?' token. */ | |
5041 | cp_lexer_consume_token (parser->lexer); | |
5042 | if (cp_parser_allow_gnu_extensions_p (parser) | |
5043 | && cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
5044 | /* Implicit true clause. */ | |
5045 | expr = NULL_TREE; | |
5046 | else | |
5047 | /* Parse the expression. */ | |
5048 | expr = cp_parser_expression (parser); | |
5049 | ||
5050 | /* The next token should be a `:'. */ | |
5051 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
5052 | /* Parse the assignment-expression. */ | |
5053 | assignment_expr = cp_parser_assignment_expression (parser); | |
5054 | ||
5055 | /* Build the conditional-expression. */ | |
5056 | return build_x_conditional_expr (logical_or_expr, | |
5057 | expr, | |
5058 | assignment_expr); | |
5059 | } | |
5060 | ||
5061 | /* Parse an assignment-expression. | |
5062 | ||
5063 | assignment-expression: | |
5064 | conditional-expression | |
5065 | logical-or-expression assignment-operator assignment_expression | |
5066 | throw-expression | |
5067 | ||
5068 | Returns a representation for the expression. */ | |
5069 | ||
5070 | static tree | |
94edc4ab | 5071 | cp_parser_assignment_expression (cp_parser* parser) |
a723baf1 MM |
5072 | { |
5073 | tree expr; | |
5074 | ||
5075 | /* If the next token is the `throw' keyword, then we're looking at | |
5076 | a throw-expression. */ | |
5077 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW)) | |
5078 | expr = cp_parser_throw_expression (parser); | |
5079 | /* Otherwise, it must be that we are looking at a | |
5080 | logical-or-expression. */ | |
5081 | else | |
5082 | { | |
5083 | /* Parse the logical-or-expression. */ | |
5084 | expr = cp_parser_logical_or_expression (parser); | |
5085 | /* If the next token is a `?' then we're actually looking at a | |
5086 | conditional-expression. */ | |
5087 | if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY)) | |
5088 | return cp_parser_question_colon_clause (parser, expr); | |
5089 | else | |
5090 | { | |
5091 | enum tree_code assignment_operator; | |
5092 | ||
5093 | /* If it's an assignment-operator, we're using the second | |
5094 | production. */ | |
5095 | assignment_operator | |
5096 | = cp_parser_assignment_operator_opt (parser); | |
5097 | if (assignment_operator != ERROR_MARK) | |
5098 | { | |
5099 | tree rhs; | |
5100 | ||
5101 | /* Parse the right-hand side of the assignment. */ | |
5102 | rhs = cp_parser_assignment_expression (parser); | |
14d22dd6 MM |
5103 | /* An assignment may not appear in a |
5104 | constant-expression. */ | |
67c03833 | 5105 | if (parser->integral_constant_expression_p) |
14d22dd6 | 5106 | { |
67c03833 JM |
5107 | if (!parser->allow_non_integral_constant_expression_p) |
5108 | return cp_parser_non_integral_constant_expression ("an assignment"); | |
5109 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 5110 | } |
34cd5ae7 | 5111 | /* Build the assignment expression. */ |
a723baf1 MM |
5112 | expr = build_x_modify_expr (expr, |
5113 | assignment_operator, | |
5114 | rhs); | |
5115 | } | |
5116 | } | |
5117 | } | |
5118 | ||
5119 | return expr; | |
5120 | } | |
5121 | ||
5122 | /* Parse an (optional) assignment-operator. | |
5123 | ||
5124 | assignment-operator: one of | |
5125 | = *= /= %= += -= >>= <<= &= ^= |= | |
5126 | ||
5127 | GNU Extension: | |
5128 | ||
5129 | assignment-operator: one of | |
5130 | <?= >?= | |
5131 | ||
5132 | If the next token is an assignment operator, the corresponding tree | |
5133 | code is returned, and the token is consumed. For example, for | |
5134 | `+=', PLUS_EXPR is returned. For `=' itself, the code returned is | |
5135 | NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%', | |
5136 | TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment | |
5137 | operator, ERROR_MARK is returned. */ | |
5138 | ||
5139 | static enum tree_code | |
94edc4ab | 5140 | cp_parser_assignment_operator_opt (cp_parser* parser) |
a723baf1 MM |
5141 | { |
5142 | enum tree_code op; | |
5143 | cp_token *token; | |
5144 | ||
5145 | /* Peek at the next toen. */ | |
5146 | token = cp_lexer_peek_token (parser->lexer); | |
5147 | ||
5148 | switch (token->type) | |
5149 | { | |
5150 | case CPP_EQ: | |
5151 | op = NOP_EXPR; | |
5152 | break; | |
5153 | ||
5154 | case CPP_MULT_EQ: | |
5155 | op = MULT_EXPR; | |
5156 | break; | |
5157 | ||
5158 | case CPP_DIV_EQ: | |
5159 | op = TRUNC_DIV_EXPR; | |
5160 | break; | |
5161 | ||
5162 | case CPP_MOD_EQ: | |
5163 | op = TRUNC_MOD_EXPR; | |
5164 | break; | |
5165 | ||
5166 | case CPP_PLUS_EQ: | |
5167 | op = PLUS_EXPR; | |
5168 | break; | |
5169 | ||
5170 | case CPP_MINUS_EQ: | |
5171 | op = MINUS_EXPR; | |
5172 | break; | |
5173 | ||
5174 | case CPP_RSHIFT_EQ: | |
5175 | op = RSHIFT_EXPR; | |
5176 | break; | |
5177 | ||
5178 | case CPP_LSHIFT_EQ: | |
5179 | op = LSHIFT_EXPR; | |
5180 | break; | |
5181 | ||
5182 | case CPP_AND_EQ: | |
5183 | op = BIT_AND_EXPR; | |
5184 | break; | |
5185 | ||
5186 | case CPP_XOR_EQ: | |
5187 | op = BIT_XOR_EXPR; | |
5188 | break; | |
5189 | ||
5190 | case CPP_OR_EQ: | |
5191 | op = BIT_IOR_EXPR; | |
5192 | break; | |
5193 | ||
5194 | case CPP_MIN_EQ: | |
5195 | op = MIN_EXPR; | |
5196 | break; | |
5197 | ||
5198 | case CPP_MAX_EQ: | |
5199 | op = MAX_EXPR; | |
5200 | break; | |
5201 | ||
5202 | default: | |
5203 | /* Nothing else is an assignment operator. */ | |
5204 | op = ERROR_MARK; | |
5205 | } | |
5206 | ||
5207 | /* If it was an assignment operator, consume it. */ | |
5208 | if (op != ERROR_MARK) | |
5209 | cp_lexer_consume_token (parser->lexer); | |
5210 | ||
5211 | return op; | |
5212 | } | |
5213 | ||
5214 | /* Parse an expression. | |
5215 | ||
5216 | expression: | |
5217 | assignment-expression | |
5218 | expression , assignment-expression | |
5219 | ||
5220 | Returns a representation of the expression. */ | |
5221 | ||
5222 | static tree | |
94edc4ab | 5223 | cp_parser_expression (cp_parser* parser) |
a723baf1 MM |
5224 | { |
5225 | tree expression = NULL_TREE; | |
a723baf1 MM |
5226 | |
5227 | while (true) | |
5228 | { | |
5229 | tree assignment_expression; | |
5230 | ||
5231 | /* Parse the next assignment-expression. */ | |
5232 | assignment_expression | |
5233 | = cp_parser_assignment_expression (parser); | |
5234 | /* If this is the first assignment-expression, we can just | |
5235 | save it away. */ | |
5236 | if (!expression) | |
5237 | expression = assignment_expression; | |
a723baf1 | 5238 | else |
d17811fd MM |
5239 | expression = build_x_compound_expr (expression, |
5240 | assignment_expression); | |
a723baf1 MM |
5241 | /* If the next token is not a comma, then we are done with the |
5242 | expression. */ | |
5243 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
5244 | break; | |
5245 | /* Consume the `,'. */ | |
5246 | cp_lexer_consume_token (parser->lexer); | |
14d22dd6 | 5247 | /* A comma operator cannot appear in a constant-expression. */ |
67c03833 | 5248 | if (parser->integral_constant_expression_p) |
14d22dd6 | 5249 | { |
67c03833 | 5250 | if (!parser->allow_non_integral_constant_expression_p) |
d17811fd | 5251 | expression |
67c03833 JM |
5252 | = cp_parser_non_integral_constant_expression ("a comma operator"); |
5253 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 5254 | } |
14d22dd6 | 5255 | } |
a723baf1 MM |
5256 | |
5257 | return expression; | |
5258 | } | |
5259 | ||
5260 | /* Parse a constant-expression. | |
5261 | ||
5262 | constant-expression: | |
14d22dd6 MM |
5263 | conditional-expression |
5264 | ||
5265 | If ALLOW_NON_CONSTANT_P a non-constant expression is silently | |
d17811fd MM |
5266 | accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not |
5267 | constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P | |
5268 | is false, NON_CONSTANT_P should be NULL. */ | |
a723baf1 MM |
5269 | |
5270 | static tree | |
14d22dd6 MM |
5271 | cp_parser_constant_expression (cp_parser* parser, |
5272 | bool allow_non_constant_p, | |
5273 | bool *non_constant_p) | |
a723baf1 | 5274 | { |
67c03833 JM |
5275 | bool saved_integral_constant_expression_p; |
5276 | bool saved_allow_non_integral_constant_expression_p; | |
5277 | bool saved_non_integral_constant_expression_p; | |
a723baf1 MM |
5278 | tree expression; |
5279 | ||
5280 | /* It might seem that we could simply parse the | |
5281 | conditional-expression, and then check to see if it were | |
5282 | TREE_CONSTANT. However, an expression that is TREE_CONSTANT is | |
5283 | one that the compiler can figure out is constant, possibly after | |
5284 | doing some simplifications or optimizations. The standard has a | |
5285 | precise definition of constant-expression, and we must honor | |
5286 | that, even though it is somewhat more restrictive. | |
5287 | ||
5288 | For example: | |
5289 | ||
5290 | int i[(2, 3)]; | |
5291 | ||
5292 | is not a legal declaration, because `(2, 3)' is not a | |
5293 | constant-expression. The `,' operator is forbidden in a | |
5294 | constant-expression. However, GCC's constant-folding machinery | |
5295 | will fold this operation to an INTEGER_CST for `3'. */ | |
5296 | ||
14d22dd6 | 5297 | /* Save the old settings. */ |
67c03833 JM |
5298 | saved_integral_constant_expression_p = parser->integral_constant_expression_p; |
5299 | saved_allow_non_integral_constant_expression_p | |
5300 | = parser->allow_non_integral_constant_expression_p; | |
5301 | saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p; | |
a723baf1 | 5302 | /* We are now parsing a constant-expression. */ |
67c03833 JM |
5303 | parser->integral_constant_expression_p = true; |
5304 | parser->allow_non_integral_constant_expression_p = allow_non_constant_p; | |
5305 | parser->non_integral_constant_expression_p = false; | |
39703eb9 MM |
5306 | /* Although the grammar says "conditional-expression", we parse an |
5307 | "assignment-expression", which also permits "throw-expression" | |
5308 | and the use of assignment operators. In the case that | |
5309 | ALLOW_NON_CONSTANT_P is false, we get better errors than we would | |
5310 | otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is | |
5311 | actually essential that we look for an assignment-expression. | |
5312 | For example, cp_parser_initializer_clauses uses this function to | |
5313 | determine whether a particular assignment-expression is in fact | |
5314 | constant. */ | |
5315 | expression = cp_parser_assignment_expression (parser); | |
14d22dd6 | 5316 | /* Restore the old settings. */ |
67c03833 JM |
5317 | parser->integral_constant_expression_p = saved_integral_constant_expression_p; |
5318 | parser->allow_non_integral_constant_expression_p | |
5319 | = saved_allow_non_integral_constant_expression_p; | |
14d22dd6 | 5320 | if (allow_non_constant_p) |
67c03833 JM |
5321 | *non_constant_p = parser->non_integral_constant_expression_p; |
5322 | parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p; | |
a723baf1 MM |
5323 | |
5324 | return expression; | |
5325 | } | |
5326 | ||
5327 | /* Statements [gram.stmt.stmt] */ | |
5328 | ||
5329 | /* Parse a statement. | |
5330 | ||
5331 | statement: | |
5332 | labeled-statement | |
5333 | expression-statement | |
5334 | compound-statement | |
5335 | selection-statement | |
5336 | iteration-statement | |
5337 | jump-statement | |
5338 | declaration-statement | |
5339 | try-block */ | |
5340 | ||
5341 | static void | |
a5bcc582 | 5342 | cp_parser_statement (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 MM |
5343 | { |
5344 | tree statement; | |
5345 | cp_token *token; | |
5346 | int statement_line_number; | |
5347 | ||
5348 | /* There is no statement yet. */ | |
5349 | statement = NULL_TREE; | |
5350 | /* Peek at the next token. */ | |
5351 | token = cp_lexer_peek_token (parser->lexer); | |
5352 | /* Remember the line number of the first token in the statement. */ | |
82a98427 | 5353 | statement_line_number = token->location.line; |
a723baf1 MM |
5354 | /* If this is a keyword, then that will often determine what kind of |
5355 | statement we have. */ | |
5356 | if (token->type == CPP_KEYWORD) | |
5357 | { | |
5358 | enum rid keyword = token->keyword; | |
5359 | ||
5360 | switch (keyword) | |
5361 | { | |
5362 | case RID_CASE: | |
5363 | case RID_DEFAULT: | |
a5bcc582 NS |
5364 | statement = cp_parser_labeled_statement (parser, |
5365 | in_statement_expr_p); | |
a723baf1 MM |
5366 | break; |
5367 | ||
5368 | case RID_IF: | |
5369 | case RID_SWITCH: | |
5370 | statement = cp_parser_selection_statement (parser); | |
5371 | break; | |
5372 | ||
5373 | case RID_WHILE: | |
5374 | case RID_DO: | |
5375 | case RID_FOR: | |
5376 | statement = cp_parser_iteration_statement (parser); | |
5377 | break; | |
5378 | ||
5379 | case RID_BREAK: | |
5380 | case RID_CONTINUE: | |
5381 | case RID_RETURN: | |
5382 | case RID_GOTO: | |
5383 | statement = cp_parser_jump_statement (parser); | |
5384 | break; | |
5385 | ||
5386 | case RID_TRY: | |
5387 | statement = cp_parser_try_block (parser); | |
5388 | break; | |
5389 | ||
5390 | default: | |
5391 | /* It might be a keyword like `int' that can start a | |
5392 | declaration-statement. */ | |
5393 | break; | |
5394 | } | |
5395 | } | |
5396 | else if (token->type == CPP_NAME) | |
5397 | { | |
5398 | /* If the next token is a `:', then we are looking at a | |
5399 | labeled-statement. */ | |
5400 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
5401 | if (token->type == CPP_COLON) | |
a5bcc582 | 5402 | statement = cp_parser_labeled_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5403 | } |
5404 | /* Anything that starts with a `{' must be a compound-statement. */ | |
5405 | else if (token->type == CPP_OPEN_BRACE) | |
a5bcc582 | 5406 | statement = cp_parser_compound_statement (parser, false); |
a723baf1 MM |
5407 | |
5408 | /* Everything else must be a declaration-statement or an | |
5409 | expression-statement. Try for the declaration-statement | |
5410 | first, unless we are looking at a `;', in which case we know that | |
5411 | we have an expression-statement. */ | |
5412 | if (!statement) | |
5413 | { | |
5414 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5415 | { | |
5416 | cp_parser_parse_tentatively (parser); | |
5417 | /* Try to parse the declaration-statement. */ | |
5418 | cp_parser_declaration_statement (parser); | |
5419 | /* If that worked, we're done. */ | |
5420 | if (cp_parser_parse_definitely (parser)) | |
5421 | return; | |
5422 | } | |
5423 | /* Look for an expression-statement instead. */ | |
a5bcc582 | 5424 | statement = cp_parser_expression_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5425 | } |
5426 | ||
5427 | /* Set the line number for the statement. */ | |
009ed910 | 5428 | if (statement && STATEMENT_CODE_P (TREE_CODE (statement))) |
a723baf1 MM |
5429 | STMT_LINENO (statement) = statement_line_number; |
5430 | } | |
5431 | ||
5432 | /* Parse a labeled-statement. | |
5433 | ||
5434 | labeled-statement: | |
5435 | identifier : statement | |
5436 | case constant-expression : statement | |
5437 | default : statement | |
5438 | ||
5439 | Returns the new CASE_LABEL, for a `case' or `default' label. For | |
5440 | an ordinary label, returns a LABEL_STMT. */ | |
5441 | ||
5442 | static tree | |
a5bcc582 | 5443 | cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 MM |
5444 | { |
5445 | cp_token *token; | |
0e59b3fb | 5446 | tree statement = error_mark_node; |
a723baf1 MM |
5447 | |
5448 | /* The next token should be an identifier. */ | |
5449 | token = cp_lexer_peek_token (parser->lexer); | |
5450 | if (token->type != CPP_NAME | |
5451 | && token->type != CPP_KEYWORD) | |
5452 | { | |
5453 | cp_parser_error (parser, "expected labeled-statement"); | |
5454 | return error_mark_node; | |
5455 | } | |
5456 | ||
5457 | switch (token->keyword) | |
5458 | { | |
5459 | case RID_CASE: | |
5460 | { | |
5461 | tree expr; | |
5462 | ||
5463 | /* Consume the `case' token. */ | |
5464 | cp_lexer_consume_token (parser->lexer); | |
5465 | /* Parse the constant-expression. */ | |
14d22dd6 | 5466 | expr = cp_parser_constant_expression (parser, |
d17811fd | 5467 | /*allow_non_constant_p=*/false, |
14d22dd6 | 5468 | NULL); |
0e59b3fb MM |
5469 | if (!parser->in_switch_statement_p) |
5470 | error ("case label `%E' not within a switch statement", expr); | |
5471 | else | |
5472 | statement = finish_case_label (expr, NULL_TREE); | |
a723baf1 MM |
5473 | } |
5474 | break; | |
5475 | ||
5476 | case RID_DEFAULT: | |
5477 | /* Consume the `default' token. */ | |
5478 | cp_lexer_consume_token (parser->lexer); | |
0e59b3fb MM |
5479 | if (!parser->in_switch_statement_p) |
5480 | error ("case label not within a switch statement"); | |
5481 | else | |
5482 | statement = finish_case_label (NULL_TREE, NULL_TREE); | |
a723baf1 MM |
5483 | break; |
5484 | ||
5485 | default: | |
5486 | /* Anything else must be an ordinary label. */ | |
5487 | statement = finish_label_stmt (cp_parser_identifier (parser)); | |
5488 | break; | |
5489 | } | |
5490 | ||
5491 | /* Require the `:' token. */ | |
5492 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
5493 | /* Parse the labeled statement. */ | |
a5bcc582 | 5494 | cp_parser_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5495 | |
5496 | /* Return the label, in the case of a `case' or `default' label. */ | |
5497 | return statement; | |
5498 | } | |
5499 | ||
5500 | /* Parse an expression-statement. | |
5501 | ||
5502 | expression-statement: | |
5503 | expression [opt] ; | |
5504 | ||
5505 | Returns the new EXPR_STMT -- or NULL_TREE if the expression | |
a5bcc582 NS |
5506 | statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P |
5507 | indicates whether this expression-statement is part of an | |
5508 | expression statement. */ | |
a723baf1 MM |
5509 | |
5510 | static tree | |
a5bcc582 | 5511 | cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 | 5512 | { |
a5bcc582 | 5513 | tree statement = NULL_TREE; |
a723baf1 | 5514 | |
a5bcc582 | 5515 | /* If the next token is a ';', then there is no expression |
04c06002 | 5516 | statement. */ |
a723baf1 | 5517 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) |
a5bcc582 NS |
5518 | statement = cp_parser_expression (parser); |
5519 | ||
a723baf1 | 5520 | /* Consume the final `;'. */ |
e0860732 | 5521 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 | 5522 | |
a5bcc582 NS |
5523 | if (in_statement_expr_p |
5524 | && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
5525 | { | |
5526 | /* This is the final expression statement of a statement | |
5527 | expression. */ | |
5528 | statement = finish_stmt_expr_expr (statement); | |
5529 | } | |
5530 | else if (statement) | |
5531 | statement = finish_expr_stmt (statement); | |
5532 | else | |
5533 | finish_stmt (); | |
5534 | ||
a723baf1 MM |
5535 | return statement; |
5536 | } | |
5537 | ||
5538 | /* Parse a compound-statement. | |
5539 | ||
5540 | compound-statement: | |
5541 | { statement-seq [opt] } | |
5542 | ||
5543 | Returns a COMPOUND_STMT representing the statement. */ | |
5544 | ||
5545 | static tree | |
a5bcc582 | 5546 | cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p) |
a723baf1 MM |
5547 | { |
5548 | tree compound_stmt; | |
5549 | ||
5550 | /* Consume the `{'. */ | |
5551 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
5552 | return error_mark_node; | |
5553 | /* Begin the compound-statement. */ | |
7a3397c7 | 5554 | compound_stmt = begin_compound_stmt (/*has_no_scope=*/false); |
a723baf1 | 5555 | /* Parse an (optional) statement-seq. */ |
a5bcc582 | 5556 | cp_parser_statement_seq_opt (parser, in_statement_expr_p); |
a723baf1 | 5557 | /* Finish the compound-statement. */ |
7a3397c7 | 5558 | finish_compound_stmt (compound_stmt); |
a723baf1 MM |
5559 | /* Consume the `}'. */ |
5560 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
5561 | ||
5562 | return compound_stmt; | |
5563 | } | |
5564 | ||
5565 | /* Parse an (optional) statement-seq. | |
5566 | ||
5567 | statement-seq: | |
5568 | statement | |
5569 | statement-seq [opt] statement */ | |
5570 | ||
5571 | static void | |
a5bcc582 | 5572 | cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 MM |
5573 | { |
5574 | /* Scan statements until there aren't any more. */ | |
5575 | while (true) | |
5576 | { | |
5577 | /* If we're looking at a `}', then we've run out of statements. */ | |
5578 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE) | |
5579 | || cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
5580 | break; | |
5581 | ||
5582 | /* Parse the statement. */ | |
a5bcc582 | 5583 | cp_parser_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5584 | } |
5585 | } | |
5586 | ||
5587 | /* Parse a selection-statement. | |
5588 | ||
5589 | selection-statement: | |
5590 | if ( condition ) statement | |
5591 | if ( condition ) statement else statement | |
5592 | switch ( condition ) statement | |
5593 | ||
5594 | Returns the new IF_STMT or SWITCH_STMT. */ | |
5595 | ||
5596 | static tree | |
94edc4ab | 5597 | cp_parser_selection_statement (cp_parser* parser) |
a723baf1 MM |
5598 | { |
5599 | cp_token *token; | |
5600 | enum rid keyword; | |
5601 | ||
5602 | /* Peek at the next token. */ | |
5603 | token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement"); | |
5604 | ||
5605 | /* See what kind of keyword it is. */ | |
5606 | keyword = token->keyword; | |
5607 | switch (keyword) | |
5608 | { | |
5609 | case RID_IF: | |
5610 | case RID_SWITCH: | |
5611 | { | |
5612 | tree statement; | |
5613 | tree condition; | |
5614 | ||
5615 | /* Look for the `('. */ | |
5616 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) | |
5617 | { | |
5618 | cp_parser_skip_to_end_of_statement (parser); | |
5619 | return error_mark_node; | |
5620 | } | |
5621 | ||
5622 | /* Begin the selection-statement. */ | |
5623 | if (keyword == RID_IF) | |
5624 | statement = begin_if_stmt (); | |
5625 | else | |
5626 | statement = begin_switch_stmt (); | |
5627 | ||
5628 | /* Parse the condition. */ | |
5629 | condition = cp_parser_condition (parser); | |
5630 | /* Look for the `)'. */ | |
5631 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
a668c6ad MM |
5632 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
5633 | /*consume_paren=*/true); | |
a723baf1 MM |
5634 | |
5635 | if (keyword == RID_IF) | |
5636 | { | |
5637 | tree then_stmt; | |
5638 | ||
5639 | /* Add the condition. */ | |
5640 | finish_if_stmt_cond (condition, statement); | |
5641 | ||
5642 | /* Parse the then-clause. */ | |
5643 | then_stmt = cp_parser_implicitly_scoped_statement (parser); | |
5644 | finish_then_clause (statement); | |
5645 | ||
5646 | /* If the next token is `else', parse the else-clause. */ | |
5647 | if (cp_lexer_next_token_is_keyword (parser->lexer, | |
5648 | RID_ELSE)) | |
5649 | { | |
5650 | tree else_stmt; | |
5651 | ||
5652 | /* Consume the `else' keyword. */ | |
5653 | cp_lexer_consume_token (parser->lexer); | |
5654 | /* Parse the else-clause. */ | |
5655 | else_stmt | |
5656 | = cp_parser_implicitly_scoped_statement (parser); | |
5657 | finish_else_clause (statement); | |
5658 | } | |
5659 | ||
5660 | /* Now we're all done with the if-statement. */ | |
5661 | finish_if_stmt (); | |
5662 | } | |
5663 | else | |
5664 | { | |
5665 | tree body; | |
0e59b3fb | 5666 | bool in_switch_statement_p; |
a723baf1 MM |
5667 | |
5668 | /* Add the condition. */ | |
5669 | finish_switch_cond (condition, statement); | |
5670 | ||
5671 | /* Parse the body of the switch-statement. */ | |
0e59b3fb MM |
5672 | in_switch_statement_p = parser->in_switch_statement_p; |
5673 | parser->in_switch_statement_p = true; | |
a723baf1 | 5674 | body = cp_parser_implicitly_scoped_statement (parser); |
0e59b3fb | 5675 | parser->in_switch_statement_p = in_switch_statement_p; |
a723baf1 MM |
5676 | |
5677 | /* Now we're all done with the switch-statement. */ | |
5678 | finish_switch_stmt (statement); | |
5679 | } | |
5680 | ||
5681 | return statement; | |
5682 | } | |
5683 | break; | |
5684 | ||
5685 | default: | |
5686 | cp_parser_error (parser, "expected selection-statement"); | |
5687 | return error_mark_node; | |
5688 | } | |
5689 | } | |
5690 | ||
5691 | /* Parse a condition. | |
5692 | ||
5693 | condition: | |
5694 | expression | |
5695 | type-specifier-seq declarator = assignment-expression | |
5696 | ||
5697 | GNU Extension: | |
5698 | ||
5699 | condition: | |
5700 | type-specifier-seq declarator asm-specification [opt] | |
5701 | attributes [opt] = assignment-expression | |
5702 | ||
5703 | Returns the expression that should be tested. */ | |
5704 | ||
5705 | static tree | |
94edc4ab | 5706 | cp_parser_condition (cp_parser* parser) |
a723baf1 MM |
5707 | { |
5708 | tree type_specifiers; | |
5709 | const char *saved_message; | |
5710 | ||
5711 | /* Try the declaration first. */ | |
5712 | cp_parser_parse_tentatively (parser); | |
5713 | /* New types are not allowed in the type-specifier-seq for a | |
5714 | condition. */ | |
5715 | saved_message = parser->type_definition_forbidden_message; | |
5716 | parser->type_definition_forbidden_message | |
5717 | = "types may not be defined in conditions"; | |
5718 | /* Parse the type-specifier-seq. */ | |
5719 | type_specifiers = cp_parser_type_specifier_seq (parser); | |
5720 | /* Restore the saved message. */ | |
5721 | parser->type_definition_forbidden_message = saved_message; | |
5722 | /* If all is well, we might be looking at a declaration. */ | |
5723 | if (!cp_parser_error_occurred (parser)) | |
5724 | { | |
5725 | tree decl; | |
5726 | tree asm_specification; | |
5727 | tree attributes; | |
5728 | tree declarator; | |
5729 | tree initializer = NULL_TREE; | |
5730 | ||
5731 | /* Parse the declarator. */ | |
62b8a44e | 5732 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
5733 | /*ctor_dtor_or_conv_p=*/NULL, |
5734 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
5735 | /* Parse the attributes. */ |
5736 | attributes = cp_parser_attributes_opt (parser); | |
5737 | /* Parse the asm-specification. */ | |
5738 | asm_specification = cp_parser_asm_specification_opt (parser); | |
5739 | /* If the next token is not an `=', then we might still be | |
5740 | looking at an expression. For example: | |
5741 | ||
5742 | if (A(a).x) | |
5743 | ||
5744 | looks like a decl-specifier-seq and a declarator -- but then | |
5745 | there is no `=', so this is an expression. */ | |
5746 | cp_parser_require (parser, CPP_EQ, "`='"); | |
5747 | /* If we did see an `=', then we are looking at a declaration | |
5748 | for sure. */ | |
5749 | if (cp_parser_parse_definitely (parser)) | |
5750 | { | |
5751 | /* Create the declaration. */ | |
5752 | decl = start_decl (declarator, type_specifiers, | |
5753 | /*initialized_p=*/true, | |
5754 | attributes, /*prefix_attributes=*/NULL_TREE); | |
5755 | /* Parse the assignment-expression. */ | |
5756 | initializer = cp_parser_assignment_expression (parser); | |
5757 | ||
5758 | /* Process the initializer. */ | |
5759 | cp_finish_decl (decl, | |
5760 | initializer, | |
5761 | asm_specification, | |
5762 | LOOKUP_ONLYCONVERTING); | |
5763 | ||
5764 | return convert_from_reference (decl); | |
5765 | } | |
5766 | } | |
5767 | /* If we didn't even get past the declarator successfully, we are | |
5768 | definitely not looking at a declaration. */ | |
5769 | else | |
5770 | cp_parser_abort_tentative_parse (parser); | |
5771 | ||
5772 | /* Otherwise, we are looking at an expression. */ | |
5773 | return cp_parser_expression (parser); | |
5774 | } | |
5775 | ||
5776 | /* Parse an iteration-statement. | |
5777 | ||
5778 | iteration-statement: | |
5779 | while ( condition ) statement | |
5780 | do statement while ( expression ) ; | |
5781 | for ( for-init-statement condition [opt] ; expression [opt] ) | |
5782 | statement | |
5783 | ||
5784 | Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */ | |
5785 | ||
5786 | static tree | |
94edc4ab | 5787 | cp_parser_iteration_statement (cp_parser* parser) |
a723baf1 MM |
5788 | { |
5789 | cp_token *token; | |
5790 | enum rid keyword; | |
5791 | tree statement; | |
0e59b3fb MM |
5792 | bool in_iteration_statement_p; |
5793 | ||
a723baf1 MM |
5794 | |
5795 | /* Peek at the next token. */ | |
5796 | token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement"); | |
5797 | if (!token) | |
5798 | return error_mark_node; | |
5799 | ||
0e59b3fb MM |
5800 | /* Remember whether or not we are already within an iteration |
5801 | statement. */ | |
5802 | in_iteration_statement_p = parser->in_iteration_statement_p; | |
5803 | ||
a723baf1 MM |
5804 | /* See what kind of keyword it is. */ |
5805 | keyword = token->keyword; | |
5806 | switch (keyword) | |
5807 | { | |
5808 | case RID_WHILE: | |
5809 | { | |
5810 | tree condition; | |
5811 | ||
5812 | /* Begin the while-statement. */ | |
5813 | statement = begin_while_stmt (); | |
5814 | /* Look for the `('. */ | |
5815 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
5816 | /* Parse the condition. */ | |
5817 | condition = cp_parser_condition (parser); | |
5818 | finish_while_stmt_cond (condition, statement); | |
5819 | /* Look for the `)'. */ | |
5820 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
5821 | /* Parse the dependent statement. */ | |
0e59b3fb | 5822 | parser->in_iteration_statement_p = true; |
a723baf1 | 5823 | cp_parser_already_scoped_statement (parser); |
0e59b3fb | 5824 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
5825 | /* We're done with the while-statement. */ |
5826 | finish_while_stmt (statement); | |
5827 | } | |
5828 | break; | |
5829 | ||
5830 | case RID_DO: | |
5831 | { | |
5832 | tree expression; | |
5833 | ||
5834 | /* Begin the do-statement. */ | |
5835 | statement = begin_do_stmt (); | |
5836 | /* Parse the body of the do-statement. */ | |
0e59b3fb | 5837 | parser->in_iteration_statement_p = true; |
a723baf1 | 5838 | cp_parser_implicitly_scoped_statement (parser); |
0e59b3fb | 5839 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
5840 | finish_do_body (statement); |
5841 | /* Look for the `while' keyword. */ | |
5842 | cp_parser_require_keyword (parser, RID_WHILE, "`while'"); | |
5843 | /* Look for the `('. */ | |
5844 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
5845 | /* Parse the expression. */ | |
5846 | expression = cp_parser_expression (parser); | |
5847 | /* We're done with the do-statement. */ | |
5848 | finish_do_stmt (expression, statement); | |
5849 | /* Look for the `)'. */ | |
5850 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
5851 | /* Look for the `;'. */ | |
5852 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
5853 | } | |
5854 | break; | |
5855 | ||
5856 | case RID_FOR: | |
5857 | { | |
5858 | tree condition = NULL_TREE; | |
5859 | tree expression = NULL_TREE; | |
5860 | ||
5861 | /* Begin the for-statement. */ | |
5862 | statement = begin_for_stmt (); | |
5863 | /* Look for the `('. */ | |
5864 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
5865 | /* Parse the initialization. */ | |
5866 | cp_parser_for_init_statement (parser); | |
5867 | finish_for_init_stmt (statement); | |
5868 | ||
5869 | /* If there's a condition, process it. */ | |
5870 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5871 | condition = cp_parser_condition (parser); | |
5872 | finish_for_cond (condition, statement); | |
5873 | /* Look for the `;'. */ | |
5874 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
5875 | ||
5876 | /* If there's an expression, process it. */ | |
5877 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) | |
5878 | expression = cp_parser_expression (parser); | |
5879 | finish_for_expr (expression, statement); | |
5880 | /* Look for the `)'. */ | |
5881 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'"); | |
5882 | ||
5883 | /* Parse the body of the for-statement. */ | |
0e59b3fb | 5884 | parser->in_iteration_statement_p = true; |
a723baf1 | 5885 | cp_parser_already_scoped_statement (parser); |
0e59b3fb | 5886 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
5887 | |
5888 | /* We're done with the for-statement. */ | |
5889 | finish_for_stmt (statement); | |
5890 | } | |
5891 | break; | |
5892 | ||
5893 | default: | |
5894 | cp_parser_error (parser, "expected iteration-statement"); | |
5895 | statement = error_mark_node; | |
5896 | break; | |
5897 | } | |
5898 | ||
5899 | return statement; | |
5900 | } | |
5901 | ||
5902 | /* Parse a for-init-statement. | |
5903 | ||
5904 | for-init-statement: | |
5905 | expression-statement | |
5906 | simple-declaration */ | |
5907 | ||
5908 | static void | |
94edc4ab | 5909 | cp_parser_for_init_statement (cp_parser* parser) |
a723baf1 MM |
5910 | { |
5911 | /* If the next token is a `;', then we have an empty | |
34cd5ae7 | 5912 | expression-statement. Grammatically, this is also a |
a723baf1 MM |
5913 | simple-declaration, but an invalid one, because it does not |
5914 | declare anything. Therefore, if we did not handle this case | |
5915 | specially, we would issue an error message about an invalid | |
5916 | declaration. */ | |
5917 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5918 | { | |
5919 | /* We're going to speculatively look for a declaration, falling back | |
5920 | to an expression, if necessary. */ | |
5921 | cp_parser_parse_tentatively (parser); | |
5922 | /* Parse the declaration. */ | |
5923 | cp_parser_simple_declaration (parser, | |
5924 | /*function_definition_allowed_p=*/false); | |
5925 | /* If the tentative parse failed, then we shall need to look for an | |
5926 | expression-statement. */ | |
5927 | if (cp_parser_parse_definitely (parser)) | |
5928 | return; | |
5929 | } | |
5930 | ||
a5bcc582 | 5931 | cp_parser_expression_statement (parser, false); |
a723baf1 MM |
5932 | } |
5933 | ||
5934 | /* Parse a jump-statement. | |
5935 | ||
5936 | jump-statement: | |
5937 | break ; | |
5938 | continue ; | |
5939 | return expression [opt] ; | |
5940 | goto identifier ; | |
5941 | ||
5942 | GNU extension: | |
5943 | ||
5944 | jump-statement: | |
5945 | goto * expression ; | |
5946 | ||
5947 | Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or | |
5948 | GOTO_STMT. */ | |
5949 | ||
5950 | static tree | |
94edc4ab | 5951 | cp_parser_jump_statement (cp_parser* parser) |
a723baf1 MM |
5952 | { |
5953 | tree statement = error_mark_node; | |
5954 | cp_token *token; | |
5955 | enum rid keyword; | |
5956 | ||
5957 | /* Peek at the next token. */ | |
5958 | token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement"); | |
5959 | if (!token) | |
5960 | return error_mark_node; | |
5961 | ||
5962 | /* See what kind of keyword it is. */ | |
5963 | keyword = token->keyword; | |
5964 | switch (keyword) | |
5965 | { | |
5966 | case RID_BREAK: | |
0e59b3fb MM |
5967 | if (!parser->in_switch_statement_p |
5968 | && !parser->in_iteration_statement_p) | |
5969 | { | |
5970 | error ("break statement not within loop or switch"); | |
5971 | statement = error_mark_node; | |
5972 | } | |
5973 | else | |
5974 | statement = finish_break_stmt (); | |
a723baf1 MM |
5975 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
5976 | break; | |
5977 | ||
5978 | case RID_CONTINUE: | |
0e59b3fb MM |
5979 | if (!parser->in_iteration_statement_p) |
5980 | { | |
5981 | error ("continue statement not within a loop"); | |
5982 | statement = error_mark_node; | |
5983 | } | |
5984 | else | |
5985 | statement = finish_continue_stmt (); | |
a723baf1 MM |
5986 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
5987 | break; | |
5988 | ||
5989 | case RID_RETURN: | |
5990 | { | |
5991 | tree expr; | |
5992 | ||
5993 | /* If the next token is a `;', then there is no | |
5994 | expression. */ | |
5995 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5996 | expr = cp_parser_expression (parser); | |
5997 | else | |
5998 | expr = NULL_TREE; | |
5999 | /* Build the return-statement. */ | |
6000 | statement = finish_return_stmt (expr); | |
6001 | /* Look for the final `;'. */ | |
6002 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6003 | } | |
6004 | break; | |
6005 | ||
6006 | case RID_GOTO: | |
6007 | /* Create the goto-statement. */ | |
6008 | if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)) | |
6009 | { | |
6010 | /* Issue a warning about this use of a GNU extension. */ | |
6011 | if (pedantic) | |
6012 | pedwarn ("ISO C++ forbids computed gotos"); | |
6013 | /* Consume the '*' token. */ | |
6014 | cp_lexer_consume_token (parser->lexer); | |
6015 | /* Parse the dependent expression. */ | |
6016 | finish_goto_stmt (cp_parser_expression (parser)); | |
6017 | } | |
6018 | else | |
6019 | finish_goto_stmt (cp_parser_identifier (parser)); | |
6020 | /* Look for the final `;'. */ | |
6021 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6022 | break; | |
6023 | ||
6024 | default: | |
6025 | cp_parser_error (parser, "expected jump-statement"); | |
6026 | break; | |
6027 | } | |
6028 | ||
6029 | return statement; | |
6030 | } | |
6031 | ||
6032 | /* Parse a declaration-statement. | |
6033 | ||
6034 | declaration-statement: | |
6035 | block-declaration */ | |
6036 | ||
6037 | static void | |
94edc4ab | 6038 | cp_parser_declaration_statement (cp_parser* parser) |
a723baf1 MM |
6039 | { |
6040 | /* Parse the block-declaration. */ | |
6041 | cp_parser_block_declaration (parser, /*statement_p=*/true); | |
6042 | ||
6043 | /* Finish off the statement. */ | |
6044 | finish_stmt (); | |
6045 | } | |
6046 | ||
6047 | /* Some dependent statements (like `if (cond) statement'), are | |
6048 | implicitly in their own scope. In other words, if the statement is | |
6049 | a single statement (as opposed to a compound-statement), it is | |
6050 | none-the-less treated as if it were enclosed in braces. Any | |
6051 | declarations appearing in the dependent statement are out of scope | |
6052 | after control passes that point. This function parses a statement, | |
6053 | but ensures that is in its own scope, even if it is not a | |
6054 | compound-statement. | |
6055 | ||
6056 | Returns the new statement. */ | |
6057 | ||
6058 | static tree | |
94edc4ab | 6059 | cp_parser_implicitly_scoped_statement (cp_parser* parser) |
a723baf1 MM |
6060 | { |
6061 | tree statement; | |
6062 | ||
6063 | /* If the token is not a `{', then we must take special action. */ | |
6064 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
6065 | { | |
6066 | /* Create a compound-statement. */ | |
7a3397c7 | 6067 | statement = begin_compound_stmt (/*has_no_scope=*/false); |
a723baf1 | 6068 | /* Parse the dependent-statement. */ |
a5bcc582 | 6069 | cp_parser_statement (parser, false); |
a723baf1 | 6070 | /* Finish the dummy compound-statement. */ |
7a3397c7 | 6071 | finish_compound_stmt (statement); |
a723baf1 MM |
6072 | } |
6073 | /* Otherwise, we simply parse the statement directly. */ | |
6074 | else | |
a5bcc582 | 6075 | statement = cp_parser_compound_statement (parser, false); |
a723baf1 MM |
6076 | |
6077 | /* Return the statement. */ | |
6078 | return statement; | |
6079 | } | |
6080 | ||
6081 | /* For some dependent statements (like `while (cond) statement'), we | |
6082 | have already created a scope. Therefore, even if the dependent | |
6083 | statement is a compound-statement, we do not want to create another | |
6084 | scope. */ | |
6085 | ||
6086 | static void | |
94edc4ab | 6087 | cp_parser_already_scoped_statement (cp_parser* parser) |
a723baf1 MM |
6088 | { |
6089 | /* If the token is not a `{', then we must take special action. */ | |
6090 | if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE)) | |
6091 | { | |
6092 | tree statement; | |
6093 | ||
6094 | /* Create a compound-statement. */ | |
7a3397c7 | 6095 | statement = begin_compound_stmt (/*has_no_scope=*/true); |
a723baf1 | 6096 | /* Parse the dependent-statement. */ |
a5bcc582 | 6097 | cp_parser_statement (parser, false); |
a723baf1 | 6098 | /* Finish the dummy compound-statement. */ |
7a3397c7 | 6099 | finish_compound_stmt (statement); |
a723baf1 MM |
6100 | } |
6101 | /* Otherwise, we simply parse the statement directly. */ | |
6102 | else | |
a5bcc582 | 6103 | cp_parser_statement (parser, false); |
a723baf1 MM |
6104 | } |
6105 | ||
6106 | /* Declarations [gram.dcl.dcl] */ | |
6107 | ||
6108 | /* Parse an optional declaration-sequence. | |
6109 | ||
6110 | declaration-seq: | |
6111 | declaration | |
6112 | declaration-seq declaration */ | |
6113 | ||
6114 | static void | |
94edc4ab | 6115 | cp_parser_declaration_seq_opt (cp_parser* parser) |
a723baf1 MM |
6116 | { |
6117 | while (true) | |
6118 | { | |
6119 | cp_token *token; | |
6120 | ||
6121 | token = cp_lexer_peek_token (parser->lexer); | |
6122 | ||
6123 | if (token->type == CPP_CLOSE_BRACE | |
6124 | || token->type == CPP_EOF) | |
6125 | break; | |
6126 | ||
6127 | if (token->type == CPP_SEMICOLON) | |
6128 | { | |
6129 | /* A declaration consisting of a single semicolon is | |
6130 | invalid. Allow it unless we're being pedantic. */ | |
499b568f | 6131 | if (pedantic && !in_system_header) |
a723baf1 MM |
6132 | pedwarn ("extra `;'"); |
6133 | cp_lexer_consume_token (parser->lexer); | |
6134 | continue; | |
6135 | } | |
6136 | ||
c838d82f | 6137 | /* The C lexer modifies PENDING_LANG_CHANGE when it wants the |
34cd5ae7 | 6138 | parser to enter or exit implicit `extern "C"' blocks. */ |
c838d82f MM |
6139 | while (pending_lang_change > 0) |
6140 | { | |
6141 | push_lang_context (lang_name_c); | |
6142 | --pending_lang_change; | |
6143 | } | |
6144 | while (pending_lang_change < 0) | |
6145 | { | |
6146 | pop_lang_context (); | |
6147 | ++pending_lang_change; | |
6148 | } | |
6149 | ||
6150 | /* Parse the declaration itself. */ | |
a723baf1 MM |
6151 | cp_parser_declaration (parser); |
6152 | } | |
6153 | } | |
6154 | ||
6155 | /* Parse a declaration. | |
6156 | ||
6157 | declaration: | |
6158 | block-declaration | |
6159 | function-definition | |
6160 | template-declaration | |
6161 | explicit-instantiation | |
6162 | explicit-specialization | |
6163 | linkage-specification | |
1092805d MM |
6164 | namespace-definition |
6165 | ||
6166 | GNU extension: | |
6167 | ||
6168 | declaration: | |
6169 | __extension__ declaration */ | |
a723baf1 MM |
6170 | |
6171 | static void | |
94edc4ab | 6172 | cp_parser_declaration (cp_parser* parser) |
a723baf1 MM |
6173 | { |
6174 | cp_token token1; | |
6175 | cp_token token2; | |
1092805d MM |
6176 | int saved_pedantic; |
6177 | ||
6178 | /* Check for the `__extension__' keyword. */ | |
6179 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
6180 | { | |
6181 | /* Parse the qualified declaration. */ | |
6182 | cp_parser_declaration (parser); | |
6183 | /* Restore the PEDANTIC flag. */ | |
6184 | pedantic = saved_pedantic; | |
6185 | ||
6186 | return; | |
6187 | } | |
a723baf1 MM |
6188 | |
6189 | /* Try to figure out what kind of declaration is present. */ | |
6190 | token1 = *cp_lexer_peek_token (parser->lexer); | |
6191 | if (token1.type != CPP_EOF) | |
6192 | token2 = *cp_lexer_peek_nth_token (parser->lexer, 2); | |
6193 | ||
6194 | /* If the next token is `extern' and the following token is a string | |
6195 | literal, then we have a linkage specification. */ | |
6196 | if (token1.keyword == RID_EXTERN | |
6197 | && cp_parser_is_string_literal (&token2)) | |
6198 | cp_parser_linkage_specification (parser); | |
6199 | /* If the next token is `template', then we have either a template | |
6200 | declaration, an explicit instantiation, or an explicit | |
6201 | specialization. */ | |
6202 | else if (token1.keyword == RID_TEMPLATE) | |
6203 | { | |
6204 | /* `template <>' indicates a template specialization. */ | |
6205 | if (token2.type == CPP_LESS | |
6206 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER) | |
6207 | cp_parser_explicit_specialization (parser); | |
6208 | /* `template <' indicates a template declaration. */ | |
6209 | else if (token2.type == CPP_LESS) | |
6210 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
6211 | /* Anything else must be an explicit instantiation. */ | |
6212 | else | |
6213 | cp_parser_explicit_instantiation (parser); | |
6214 | } | |
6215 | /* If the next token is `export', then we have a template | |
6216 | declaration. */ | |
6217 | else if (token1.keyword == RID_EXPORT) | |
6218 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
6219 | /* If the next token is `extern', 'static' or 'inline' and the one | |
6220 | after that is `template', we have a GNU extended explicit | |
6221 | instantiation directive. */ | |
6222 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
6223 | && (token1.keyword == RID_EXTERN | |
6224 | || token1.keyword == RID_STATIC | |
6225 | || token1.keyword == RID_INLINE) | |
6226 | && token2.keyword == RID_TEMPLATE) | |
6227 | cp_parser_explicit_instantiation (parser); | |
6228 | /* If the next token is `namespace', check for a named or unnamed | |
6229 | namespace definition. */ | |
6230 | else if (token1.keyword == RID_NAMESPACE | |
6231 | && (/* A named namespace definition. */ | |
6232 | (token2.type == CPP_NAME | |
6233 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type | |
6234 | == CPP_OPEN_BRACE)) | |
6235 | /* An unnamed namespace definition. */ | |
6236 | || token2.type == CPP_OPEN_BRACE)) | |
6237 | cp_parser_namespace_definition (parser); | |
6238 | /* We must have either a block declaration or a function | |
6239 | definition. */ | |
6240 | else | |
6241 | /* Try to parse a block-declaration, or a function-definition. */ | |
6242 | cp_parser_block_declaration (parser, /*statement_p=*/false); | |
6243 | } | |
6244 | ||
6245 | /* Parse a block-declaration. | |
6246 | ||
6247 | block-declaration: | |
6248 | simple-declaration | |
6249 | asm-definition | |
6250 | namespace-alias-definition | |
6251 | using-declaration | |
6252 | using-directive | |
6253 | ||
6254 | GNU Extension: | |
6255 | ||
6256 | block-declaration: | |
6257 | __extension__ block-declaration | |
6258 | label-declaration | |
6259 | ||
34cd5ae7 | 6260 | If STATEMENT_P is TRUE, then this block-declaration is occurring as |
a723baf1 MM |
6261 | part of a declaration-statement. */ |
6262 | ||
6263 | static void | |
6264 | cp_parser_block_declaration (cp_parser *parser, | |
6265 | bool statement_p) | |
6266 | { | |
6267 | cp_token *token1; | |
6268 | int saved_pedantic; | |
6269 | ||
6270 | /* Check for the `__extension__' keyword. */ | |
6271 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
6272 | { | |
6273 | /* Parse the qualified declaration. */ | |
6274 | cp_parser_block_declaration (parser, statement_p); | |
6275 | /* Restore the PEDANTIC flag. */ | |
6276 | pedantic = saved_pedantic; | |
6277 | ||
6278 | return; | |
6279 | } | |
6280 | ||
6281 | /* Peek at the next token to figure out which kind of declaration is | |
6282 | present. */ | |
6283 | token1 = cp_lexer_peek_token (parser->lexer); | |
6284 | ||
6285 | /* If the next keyword is `asm', we have an asm-definition. */ | |
6286 | if (token1->keyword == RID_ASM) | |
6287 | { | |
6288 | if (statement_p) | |
6289 | cp_parser_commit_to_tentative_parse (parser); | |
6290 | cp_parser_asm_definition (parser); | |
6291 | } | |
6292 | /* If the next keyword is `namespace', we have a | |
6293 | namespace-alias-definition. */ | |
6294 | else if (token1->keyword == RID_NAMESPACE) | |
6295 | cp_parser_namespace_alias_definition (parser); | |
6296 | /* If the next keyword is `using', we have either a | |
6297 | using-declaration or a using-directive. */ | |
6298 | else if (token1->keyword == RID_USING) | |
6299 | { | |
6300 | cp_token *token2; | |
6301 | ||
6302 | if (statement_p) | |
6303 | cp_parser_commit_to_tentative_parse (parser); | |
6304 | /* If the token after `using' is `namespace', then we have a | |
6305 | using-directive. */ | |
6306 | token2 = cp_lexer_peek_nth_token (parser->lexer, 2); | |
6307 | if (token2->keyword == RID_NAMESPACE) | |
6308 | cp_parser_using_directive (parser); | |
6309 | /* Otherwise, it's a using-declaration. */ | |
6310 | else | |
6311 | cp_parser_using_declaration (parser); | |
6312 | } | |
6313 | /* If the next keyword is `__label__' we have a label declaration. */ | |
6314 | else if (token1->keyword == RID_LABEL) | |
6315 | { | |
6316 | if (statement_p) | |
6317 | cp_parser_commit_to_tentative_parse (parser); | |
6318 | cp_parser_label_declaration (parser); | |
6319 | } | |
6320 | /* Anything else must be a simple-declaration. */ | |
6321 | else | |
6322 | cp_parser_simple_declaration (parser, !statement_p); | |
6323 | } | |
6324 | ||
6325 | /* Parse a simple-declaration. | |
6326 | ||
6327 | simple-declaration: | |
6328 | decl-specifier-seq [opt] init-declarator-list [opt] ; | |
6329 | ||
6330 | init-declarator-list: | |
6331 | init-declarator | |
6332 | init-declarator-list , init-declarator | |
6333 | ||
34cd5ae7 | 6334 | If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a |
9bcb9aae | 6335 | function-definition as a simple-declaration. */ |
a723baf1 MM |
6336 | |
6337 | static void | |
94edc4ab NN |
6338 | cp_parser_simple_declaration (cp_parser* parser, |
6339 | bool function_definition_allowed_p) | |
a723baf1 MM |
6340 | { |
6341 | tree decl_specifiers; | |
6342 | tree attributes; | |
560ad596 | 6343 | int declares_class_or_enum; |
a723baf1 MM |
6344 | bool saw_declarator; |
6345 | ||
6346 | /* Defer access checks until we know what is being declared; the | |
6347 | checks for names appearing in the decl-specifier-seq should be | |
6348 | done as if we were in the scope of the thing being declared. */ | |
8d241e0b | 6349 | push_deferring_access_checks (dk_deferred); |
cf22909c | 6350 | |
a723baf1 MM |
6351 | /* Parse the decl-specifier-seq. We have to keep track of whether |
6352 | or not the decl-specifier-seq declares a named class or | |
6353 | enumeration type, since that is the only case in which the | |
6354 | init-declarator-list is allowed to be empty. | |
6355 | ||
6356 | [dcl.dcl] | |
6357 | ||
6358 | In a simple-declaration, the optional init-declarator-list can be | |
6359 | omitted only when declaring a class or enumeration, that is when | |
6360 | the decl-specifier-seq contains either a class-specifier, an | |
6361 | elaborated-type-specifier, or an enum-specifier. */ | |
6362 | decl_specifiers | |
6363 | = cp_parser_decl_specifier_seq (parser, | |
6364 | CP_PARSER_FLAGS_OPTIONAL, | |
6365 | &attributes, | |
6366 | &declares_class_or_enum); | |
6367 | /* We no longer need to defer access checks. */ | |
cf22909c | 6368 | stop_deferring_access_checks (); |
24c0ef37 | 6369 | |
39703eb9 MM |
6370 | /* In a block scope, a valid declaration must always have a |
6371 | decl-specifier-seq. By not trying to parse declarators, we can | |
6372 | resolve the declaration/expression ambiguity more quickly. */ | |
6373 | if (!function_definition_allowed_p && !decl_specifiers) | |
6374 | { | |
6375 | cp_parser_error (parser, "expected declaration"); | |
6376 | goto done; | |
6377 | } | |
6378 | ||
8fbc5ae7 MM |
6379 | /* If the next two tokens are both identifiers, the code is |
6380 | erroneous. The usual cause of this situation is code like: | |
6381 | ||
6382 | T t; | |
6383 | ||
6384 | where "T" should name a type -- but does not. */ | |
6385 | if (cp_parser_diagnose_invalid_type_name (parser)) | |
6386 | { | |
8d241e0b | 6387 | /* If parsing tentatively, we should commit; we really are |
8fbc5ae7 MM |
6388 | looking at a declaration. */ |
6389 | cp_parser_commit_to_tentative_parse (parser); | |
6390 | /* Give up. */ | |
39703eb9 | 6391 | goto done; |
8fbc5ae7 MM |
6392 | } |
6393 | ||
a723baf1 MM |
6394 | /* Keep going until we hit the `;' at the end of the simple |
6395 | declaration. */ | |
6396 | saw_declarator = false; | |
6397 | while (cp_lexer_next_token_is_not (parser->lexer, | |
6398 | CPP_SEMICOLON)) | |
6399 | { | |
6400 | cp_token *token; | |
6401 | bool function_definition_p; | |
560ad596 | 6402 | tree decl; |
a723baf1 MM |
6403 | |
6404 | saw_declarator = true; | |
6405 | /* Parse the init-declarator. */ | |
560ad596 MM |
6406 | decl = cp_parser_init_declarator (parser, decl_specifiers, attributes, |
6407 | function_definition_allowed_p, | |
6408 | /*member_p=*/false, | |
6409 | declares_class_or_enum, | |
6410 | &function_definition_p); | |
1fb3244a MM |
6411 | /* If an error occurred while parsing tentatively, exit quickly. |
6412 | (That usually happens when in the body of a function; each | |
6413 | statement is treated as a declaration-statement until proven | |
6414 | otherwise.) */ | |
6415 | if (cp_parser_error_occurred (parser)) | |
39703eb9 | 6416 | goto done; |
a723baf1 MM |
6417 | /* Handle function definitions specially. */ |
6418 | if (function_definition_p) | |
6419 | { | |
6420 | /* If the next token is a `,', then we are probably | |
6421 | processing something like: | |
6422 | ||
6423 | void f() {}, *p; | |
6424 | ||
6425 | which is erroneous. */ | |
6426 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
6427 | error ("mixing declarations and function-definitions is forbidden"); | |
6428 | /* Otherwise, we're done with the list of declarators. */ | |
6429 | else | |
24c0ef37 | 6430 | { |
cf22909c | 6431 | pop_deferring_access_checks (); |
24c0ef37 GS |
6432 | return; |
6433 | } | |
a723baf1 MM |
6434 | } |
6435 | /* The next token should be either a `,' or a `;'. */ | |
6436 | token = cp_lexer_peek_token (parser->lexer); | |
6437 | /* If it's a `,', there are more declarators to come. */ | |
6438 | if (token->type == CPP_COMMA) | |
6439 | cp_lexer_consume_token (parser->lexer); | |
6440 | /* If it's a `;', we are done. */ | |
6441 | else if (token->type == CPP_SEMICOLON) | |
6442 | break; | |
6443 | /* Anything else is an error. */ | |
6444 | else | |
6445 | { | |
6446 | cp_parser_error (parser, "expected `,' or `;'"); | |
6447 | /* Skip tokens until we reach the end of the statement. */ | |
6448 | cp_parser_skip_to_end_of_statement (parser); | |
39703eb9 | 6449 | goto done; |
a723baf1 MM |
6450 | } |
6451 | /* After the first time around, a function-definition is not | |
6452 | allowed -- even if it was OK at first. For example: | |
6453 | ||
6454 | int i, f() {} | |
6455 | ||
6456 | is not valid. */ | |
6457 | function_definition_allowed_p = false; | |
6458 | } | |
6459 | ||
6460 | /* Issue an error message if no declarators are present, and the | |
6461 | decl-specifier-seq does not itself declare a class or | |
6462 | enumeration. */ | |
6463 | if (!saw_declarator) | |
6464 | { | |
6465 | if (cp_parser_declares_only_class_p (parser)) | |
6466 | shadow_tag (decl_specifiers); | |
6467 | /* Perform any deferred access checks. */ | |
cf22909c | 6468 | perform_deferred_access_checks (); |
a723baf1 MM |
6469 | } |
6470 | ||
6471 | /* Consume the `;'. */ | |
6472 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6473 | ||
39703eb9 MM |
6474 | done: |
6475 | pop_deferring_access_checks (); | |
a723baf1 MM |
6476 | } |
6477 | ||
6478 | /* Parse a decl-specifier-seq. | |
6479 | ||
6480 | decl-specifier-seq: | |
6481 | decl-specifier-seq [opt] decl-specifier | |
6482 | ||
6483 | decl-specifier: | |
6484 | storage-class-specifier | |
6485 | type-specifier | |
6486 | function-specifier | |
6487 | friend | |
6488 | typedef | |
6489 | ||
6490 | GNU Extension: | |
6491 | ||
6492 | decl-specifier-seq: | |
6493 | decl-specifier-seq [opt] attributes | |
6494 | ||
6495 | Returns a TREE_LIST, giving the decl-specifiers in the order they | |
6496 | appear in the source code. The TREE_VALUE of each node is the | |
6497 | decl-specifier. For a keyword (such as `auto' or `friend'), the | |
34cd5ae7 | 6498 | TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the |
a723baf1 MM |
6499 | representation of a type-specifier, see cp_parser_type_specifier. |
6500 | ||
6501 | If there are attributes, they will be stored in *ATTRIBUTES, | |
6502 | represented as described above cp_parser_attributes. | |
6503 | ||
6504 | If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier | |
6505 | appears, and the entity that will be a friend is not going to be a | |
6506 | class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that | |
6507 | even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which | |
560ad596 MM |
6508 | friendship is granted might not be a class. |
6509 | ||
6510 | *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following | |
6511 | *flags: | |
6512 | ||
6513 | 1: one of the decl-specifiers is an elaborated-type-specifier | |
6514 | 2: one of the decl-specifiers is an enum-specifier or a | |
6515 | class-specifier | |
6516 | ||
6517 | */ | |
a723baf1 MM |
6518 | |
6519 | static tree | |
94edc4ab NN |
6520 | cp_parser_decl_specifier_seq (cp_parser* parser, |
6521 | cp_parser_flags flags, | |
6522 | tree* attributes, | |
560ad596 | 6523 | int* declares_class_or_enum) |
a723baf1 MM |
6524 | { |
6525 | tree decl_specs = NULL_TREE; | |
6526 | bool friend_p = false; | |
f2ce60b8 NS |
6527 | bool constructor_possible_p = !parser->in_declarator_p; |
6528 | ||
a723baf1 | 6529 | /* Assume no class or enumeration type is declared. */ |
560ad596 | 6530 | *declares_class_or_enum = 0; |
a723baf1 MM |
6531 | |
6532 | /* Assume there are no attributes. */ | |
6533 | *attributes = NULL_TREE; | |
6534 | ||
6535 | /* Keep reading specifiers until there are no more to read. */ | |
6536 | while (true) | |
6537 | { | |
6538 | tree decl_spec = NULL_TREE; | |
6539 | bool constructor_p; | |
6540 | cp_token *token; | |
6541 | ||
6542 | /* Peek at the next token. */ | |
6543 | token = cp_lexer_peek_token (parser->lexer); | |
6544 | /* Handle attributes. */ | |
6545 | if (token->keyword == RID_ATTRIBUTE) | |
6546 | { | |
6547 | /* Parse the attributes. */ | |
6548 | decl_spec = cp_parser_attributes_opt (parser); | |
6549 | /* Add them to the list. */ | |
6550 | *attributes = chainon (*attributes, decl_spec); | |
6551 | continue; | |
6552 | } | |
6553 | /* If the next token is an appropriate keyword, we can simply | |
6554 | add it to the list. */ | |
6555 | switch (token->keyword) | |
6556 | { | |
6557 | case RID_FRIEND: | |
6558 | /* decl-specifier: | |
6559 | friend */ | |
1918facf SB |
6560 | if (friend_p) |
6561 | error ("duplicate `friend'"); | |
6562 | else | |
6563 | friend_p = true; | |
a723baf1 MM |
6564 | /* The representation of the specifier is simply the |
6565 | appropriate TREE_IDENTIFIER node. */ | |
6566 | decl_spec = token->value; | |
6567 | /* Consume the token. */ | |
6568 | cp_lexer_consume_token (parser->lexer); | |
6569 | break; | |
6570 | ||
6571 | /* function-specifier: | |
6572 | inline | |
6573 | virtual | |
6574 | explicit */ | |
6575 | case RID_INLINE: | |
6576 | case RID_VIRTUAL: | |
6577 | case RID_EXPLICIT: | |
6578 | decl_spec = cp_parser_function_specifier_opt (parser); | |
6579 | break; | |
6580 | ||
6581 | /* decl-specifier: | |
6582 | typedef */ | |
6583 | case RID_TYPEDEF: | |
6584 | /* The representation of the specifier is simply the | |
6585 | appropriate TREE_IDENTIFIER node. */ | |
6586 | decl_spec = token->value; | |
6587 | /* Consume the token. */ | |
6588 | cp_lexer_consume_token (parser->lexer); | |
2050a1bb MM |
6589 | /* A constructor declarator cannot appear in a typedef. */ |
6590 | constructor_possible_p = false; | |
c006d942 MM |
6591 | /* The "typedef" keyword can only occur in a declaration; we |
6592 | may as well commit at this point. */ | |
6593 | cp_parser_commit_to_tentative_parse (parser); | |
a723baf1 MM |
6594 | break; |
6595 | ||
6596 | /* storage-class-specifier: | |
6597 | auto | |
6598 | register | |
6599 | static | |
6600 | extern | |
6601 | mutable | |
6602 | ||
6603 | GNU Extension: | |
6604 | thread */ | |
6605 | case RID_AUTO: | |
6606 | case RID_REGISTER: | |
6607 | case RID_STATIC: | |
6608 | case RID_EXTERN: | |
6609 | case RID_MUTABLE: | |
6610 | case RID_THREAD: | |
6611 | decl_spec = cp_parser_storage_class_specifier_opt (parser); | |
6612 | break; | |
6613 | ||
6614 | default: | |
6615 | break; | |
6616 | } | |
6617 | ||
6618 | /* Constructors are a special case. The `S' in `S()' is not a | |
6619 | decl-specifier; it is the beginning of the declarator. */ | |
6620 | constructor_p = (!decl_spec | |
2050a1bb | 6621 | && constructor_possible_p |
a723baf1 MM |
6622 | && cp_parser_constructor_declarator_p (parser, |
6623 | friend_p)); | |
6624 | ||
6625 | /* If we don't have a DECL_SPEC yet, then we must be looking at | |
6626 | a type-specifier. */ | |
6627 | if (!decl_spec && !constructor_p) | |
6628 | { | |
560ad596 | 6629 | int decl_spec_declares_class_or_enum; |
a723baf1 MM |
6630 | bool is_cv_qualifier; |
6631 | ||
6632 | decl_spec | |
6633 | = cp_parser_type_specifier (parser, flags, | |
6634 | friend_p, | |
6635 | /*is_declaration=*/true, | |
6636 | &decl_spec_declares_class_or_enum, | |
6637 | &is_cv_qualifier); | |
6638 | ||
6639 | *declares_class_or_enum |= decl_spec_declares_class_or_enum; | |
6640 | ||
6641 | /* If this type-specifier referenced a user-defined type | |
6642 | (a typedef, class-name, etc.), then we can't allow any | |
6643 | more such type-specifiers henceforth. | |
6644 | ||
6645 | [dcl.spec] | |
6646 | ||
6647 | The longest sequence of decl-specifiers that could | |
6648 | possibly be a type name is taken as the | |
6649 | decl-specifier-seq of a declaration. The sequence shall | |
6650 | be self-consistent as described below. | |
6651 | ||
6652 | [dcl.type] | |
6653 | ||
6654 | As a general rule, at most one type-specifier is allowed | |
6655 | in the complete decl-specifier-seq of a declaration. The | |
6656 | only exceptions are the following: | |
6657 | ||
6658 | -- const or volatile can be combined with any other | |
6659 | type-specifier. | |
6660 | ||
6661 | -- signed or unsigned can be combined with char, long, | |
6662 | short, or int. | |
6663 | ||
6664 | -- .. | |
6665 | ||
6666 | Example: | |
6667 | ||
6668 | typedef char* Pc; | |
6669 | void g (const int Pc); | |
6670 | ||
6671 | Here, Pc is *not* part of the decl-specifier seq; it's | |
6672 | the declarator. Therefore, once we see a type-specifier | |
6673 | (other than a cv-qualifier), we forbid any additional | |
6674 | user-defined types. We *do* still allow things like `int | |
6675 | int' to be considered a decl-specifier-seq, and issue the | |
6676 | error message later. */ | |
6677 | if (decl_spec && !is_cv_qualifier) | |
6678 | flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; | |
2050a1bb MM |
6679 | /* A constructor declarator cannot follow a type-specifier. */ |
6680 | if (decl_spec) | |
6681 | constructor_possible_p = false; | |
a723baf1 MM |
6682 | } |
6683 | ||
6684 | /* If we still do not have a DECL_SPEC, then there are no more | |
6685 | decl-specifiers. */ | |
6686 | if (!decl_spec) | |
6687 | { | |
6688 | /* Issue an error message, unless the entire construct was | |
6689 | optional. */ | |
6690 | if (!(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
6691 | { | |
6692 | cp_parser_error (parser, "expected decl specifier"); | |
6693 | return error_mark_node; | |
6694 | } | |
6695 | ||
6696 | break; | |
6697 | } | |
6698 | ||
6699 | /* Add the DECL_SPEC to the list of specifiers. */ | |
6700 | decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs); | |
6701 | ||
6702 | /* After we see one decl-specifier, further decl-specifiers are | |
6703 | always optional. */ | |
6704 | flags |= CP_PARSER_FLAGS_OPTIONAL; | |
6705 | } | |
6706 | ||
6707 | /* We have built up the DECL_SPECS in reverse order. Return them in | |
6708 | the correct order. */ | |
6709 | return nreverse (decl_specs); | |
6710 | } | |
6711 | ||
6712 | /* Parse an (optional) storage-class-specifier. | |
6713 | ||
6714 | storage-class-specifier: | |
6715 | auto | |
6716 | register | |
6717 | static | |
6718 | extern | |
6719 | mutable | |
6720 | ||
6721 | GNU Extension: | |
6722 | ||
6723 | storage-class-specifier: | |
6724 | thread | |
6725 | ||
6726 | Returns an IDENTIFIER_NODE corresponding to the keyword used. */ | |
6727 | ||
6728 | static tree | |
94edc4ab | 6729 | cp_parser_storage_class_specifier_opt (cp_parser* parser) |
a723baf1 MM |
6730 | { |
6731 | switch (cp_lexer_peek_token (parser->lexer)->keyword) | |
6732 | { | |
6733 | case RID_AUTO: | |
6734 | case RID_REGISTER: | |
6735 | case RID_STATIC: | |
6736 | case RID_EXTERN: | |
6737 | case RID_MUTABLE: | |
6738 | case RID_THREAD: | |
6739 | /* Consume the token. */ | |
6740 | return cp_lexer_consume_token (parser->lexer)->value; | |
6741 | ||
6742 | default: | |
6743 | return NULL_TREE; | |
6744 | } | |
6745 | } | |
6746 | ||
6747 | /* Parse an (optional) function-specifier. | |
6748 | ||
6749 | function-specifier: | |
6750 | inline | |
6751 | virtual | |
6752 | explicit | |
6753 | ||
6754 | Returns an IDENTIFIER_NODE corresponding to the keyword used. */ | |
6755 | ||
6756 | static tree | |
94edc4ab | 6757 | cp_parser_function_specifier_opt (cp_parser* parser) |
a723baf1 MM |
6758 | { |
6759 | switch (cp_lexer_peek_token (parser->lexer)->keyword) | |
6760 | { | |
6761 | case RID_INLINE: | |
6762 | case RID_VIRTUAL: | |
6763 | case RID_EXPLICIT: | |
6764 | /* Consume the token. */ | |
6765 | return cp_lexer_consume_token (parser->lexer)->value; | |
6766 | ||
6767 | default: | |
6768 | return NULL_TREE; | |
6769 | } | |
6770 | } | |
6771 | ||
6772 | /* Parse a linkage-specification. | |
6773 | ||
6774 | linkage-specification: | |
6775 | extern string-literal { declaration-seq [opt] } | |
6776 | extern string-literal declaration */ | |
6777 | ||
6778 | static void | |
94edc4ab | 6779 | cp_parser_linkage_specification (cp_parser* parser) |
a723baf1 MM |
6780 | { |
6781 | cp_token *token; | |
6782 | tree linkage; | |
6783 | ||
6784 | /* Look for the `extern' keyword. */ | |
6785 | cp_parser_require_keyword (parser, RID_EXTERN, "`extern'"); | |
6786 | ||
6787 | /* Peek at the next token. */ | |
6788 | token = cp_lexer_peek_token (parser->lexer); | |
6789 | /* If it's not a string-literal, then there's a problem. */ | |
6790 | if (!cp_parser_is_string_literal (token)) | |
6791 | { | |
6792 | cp_parser_error (parser, "expected language-name"); | |
6793 | return; | |
6794 | } | |
6795 | /* Consume the token. */ | |
6796 | cp_lexer_consume_token (parser->lexer); | |
6797 | ||
6798 | /* Transform the literal into an identifier. If the literal is a | |
6799 | wide-character string, or contains embedded NULs, then we can't | |
6800 | handle it as the user wants. */ | |
6801 | if (token->type == CPP_WSTRING | |
6802 | || (strlen (TREE_STRING_POINTER (token->value)) | |
6803 | != (size_t) (TREE_STRING_LENGTH (token->value) - 1))) | |
6804 | { | |
6805 | cp_parser_error (parser, "invalid linkage-specification"); | |
6806 | /* Assume C++ linkage. */ | |
6807 | linkage = get_identifier ("c++"); | |
6808 | } | |
6809 | /* If it's a simple string constant, things are easier. */ | |
6810 | else | |
6811 | linkage = get_identifier (TREE_STRING_POINTER (token->value)); | |
6812 | ||
6813 | /* We're now using the new linkage. */ | |
6814 | push_lang_context (linkage); | |
6815 | ||
6816 | /* If the next token is a `{', then we're using the first | |
6817 | production. */ | |
6818 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
6819 | { | |
6820 | /* Consume the `{' token. */ | |
6821 | cp_lexer_consume_token (parser->lexer); | |
6822 | /* Parse the declarations. */ | |
6823 | cp_parser_declaration_seq_opt (parser); | |
6824 | /* Look for the closing `}'. */ | |
6825 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
6826 | } | |
6827 | /* Otherwise, there's just one declaration. */ | |
6828 | else | |
6829 | { | |
6830 | bool saved_in_unbraced_linkage_specification_p; | |
6831 | ||
6832 | saved_in_unbraced_linkage_specification_p | |
6833 | = parser->in_unbraced_linkage_specification_p; | |
6834 | parser->in_unbraced_linkage_specification_p = true; | |
6835 | have_extern_spec = true; | |
6836 | cp_parser_declaration (parser); | |
6837 | have_extern_spec = false; | |
6838 | parser->in_unbraced_linkage_specification_p | |
6839 | = saved_in_unbraced_linkage_specification_p; | |
6840 | } | |
6841 | ||
6842 | /* We're done with the linkage-specification. */ | |
6843 | pop_lang_context (); | |
6844 | } | |
6845 | ||
6846 | /* Special member functions [gram.special] */ | |
6847 | ||
6848 | /* Parse a conversion-function-id. | |
6849 | ||
6850 | conversion-function-id: | |
6851 | operator conversion-type-id | |
6852 | ||
6853 | Returns an IDENTIFIER_NODE representing the operator. */ | |
6854 | ||
6855 | static tree | |
94edc4ab | 6856 | cp_parser_conversion_function_id (cp_parser* parser) |
a723baf1 MM |
6857 | { |
6858 | tree type; | |
6859 | tree saved_scope; | |
6860 | tree saved_qualifying_scope; | |
6861 | tree saved_object_scope; | |
6862 | ||
6863 | /* Look for the `operator' token. */ | |
6864 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'")) | |
6865 | return error_mark_node; | |
6866 | /* When we parse the conversion-type-id, the current scope will be | |
6867 | reset. However, we need that information in able to look up the | |
6868 | conversion function later, so we save it here. */ | |
6869 | saved_scope = parser->scope; | |
6870 | saved_qualifying_scope = parser->qualifying_scope; | |
6871 | saved_object_scope = parser->object_scope; | |
6872 | /* We must enter the scope of the class so that the names of | |
6873 | entities declared within the class are available in the | |
6874 | conversion-type-id. For example, consider: | |
6875 | ||
6876 | struct S { | |
6877 | typedef int I; | |
6878 | operator I(); | |
6879 | }; | |
6880 | ||
6881 | S::operator I() { ... } | |
6882 | ||
6883 | In order to see that `I' is a type-name in the definition, we | |
6884 | must be in the scope of `S'. */ | |
6885 | if (saved_scope) | |
6886 | push_scope (saved_scope); | |
6887 | /* Parse the conversion-type-id. */ | |
6888 | type = cp_parser_conversion_type_id (parser); | |
6889 | /* Leave the scope of the class, if any. */ | |
6890 | if (saved_scope) | |
6891 | pop_scope (saved_scope); | |
6892 | /* Restore the saved scope. */ | |
6893 | parser->scope = saved_scope; | |
6894 | parser->qualifying_scope = saved_qualifying_scope; | |
6895 | parser->object_scope = saved_object_scope; | |
6896 | /* If the TYPE is invalid, indicate failure. */ | |
6897 | if (type == error_mark_node) | |
6898 | return error_mark_node; | |
6899 | return mangle_conv_op_name_for_type (type); | |
6900 | } | |
6901 | ||
6902 | /* Parse a conversion-type-id: | |
6903 | ||
6904 | conversion-type-id: | |
6905 | type-specifier-seq conversion-declarator [opt] | |
6906 | ||
6907 | Returns the TYPE specified. */ | |
6908 | ||
6909 | static tree | |
94edc4ab | 6910 | cp_parser_conversion_type_id (cp_parser* parser) |
a723baf1 MM |
6911 | { |
6912 | tree attributes; | |
6913 | tree type_specifiers; | |
6914 | tree declarator; | |
6915 | ||
6916 | /* Parse the attributes. */ | |
6917 | attributes = cp_parser_attributes_opt (parser); | |
6918 | /* Parse the type-specifiers. */ | |
6919 | type_specifiers = cp_parser_type_specifier_seq (parser); | |
6920 | /* If that didn't work, stop. */ | |
6921 | if (type_specifiers == error_mark_node) | |
6922 | return error_mark_node; | |
6923 | /* Parse the conversion-declarator. */ | |
6924 | declarator = cp_parser_conversion_declarator_opt (parser); | |
6925 | ||
6926 | return grokdeclarator (declarator, type_specifiers, TYPENAME, | |
6927 | /*initialized=*/0, &attributes); | |
6928 | } | |
6929 | ||
6930 | /* Parse an (optional) conversion-declarator. | |
6931 | ||
6932 | conversion-declarator: | |
6933 | ptr-operator conversion-declarator [opt] | |
6934 | ||
6935 | Returns a representation of the declarator. See | |
6936 | cp_parser_declarator for details. */ | |
6937 | ||
6938 | static tree | |
94edc4ab | 6939 | cp_parser_conversion_declarator_opt (cp_parser* parser) |
a723baf1 MM |
6940 | { |
6941 | enum tree_code code; | |
6942 | tree class_type; | |
6943 | tree cv_qualifier_seq; | |
6944 | ||
6945 | /* We don't know if there's a ptr-operator next, or not. */ | |
6946 | cp_parser_parse_tentatively (parser); | |
6947 | /* Try the ptr-operator. */ | |
6948 | code = cp_parser_ptr_operator (parser, &class_type, | |
6949 | &cv_qualifier_seq); | |
6950 | /* If it worked, look for more conversion-declarators. */ | |
6951 | if (cp_parser_parse_definitely (parser)) | |
6952 | { | |
6953 | tree declarator; | |
6954 | ||
6955 | /* Parse another optional declarator. */ | |
6956 | declarator = cp_parser_conversion_declarator_opt (parser); | |
6957 | ||
6958 | /* Create the representation of the declarator. */ | |
6959 | if (code == INDIRECT_REF) | |
6960 | declarator = make_pointer_declarator (cv_qualifier_seq, | |
6961 | declarator); | |
6962 | else | |
6963 | declarator = make_reference_declarator (cv_qualifier_seq, | |
6964 | declarator); | |
6965 | ||
6966 | /* Handle the pointer-to-member case. */ | |
6967 | if (class_type) | |
6968 | declarator = build_nt (SCOPE_REF, class_type, declarator); | |
6969 | ||
6970 | return declarator; | |
6971 | } | |
6972 | ||
6973 | return NULL_TREE; | |
6974 | } | |
6975 | ||
6976 | /* Parse an (optional) ctor-initializer. | |
6977 | ||
6978 | ctor-initializer: | |
6979 | : mem-initializer-list | |
6980 | ||
6981 | Returns TRUE iff the ctor-initializer was actually present. */ | |
6982 | ||
6983 | static bool | |
94edc4ab | 6984 | cp_parser_ctor_initializer_opt (cp_parser* parser) |
a723baf1 MM |
6985 | { |
6986 | /* If the next token is not a `:', then there is no | |
6987 | ctor-initializer. */ | |
6988 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) | |
6989 | { | |
6990 | /* Do default initialization of any bases and members. */ | |
6991 | if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
6992 | finish_mem_initializers (NULL_TREE); | |
6993 | ||
6994 | return false; | |
6995 | } | |
6996 | ||
6997 | /* Consume the `:' token. */ | |
6998 | cp_lexer_consume_token (parser->lexer); | |
6999 | /* And the mem-initializer-list. */ | |
7000 | cp_parser_mem_initializer_list (parser); | |
7001 | ||
7002 | return true; | |
7003 | } | |
7004 | ||
7005 | /* Parse a mem-initializer-list. | |
7006 | ||
7007 | mem-initializer-list: | |
7008 | mem-initializer | |
7009 | mem-initializer , mem-initializer-list */ | |
7010 | ||
7011 | static void | |
94edc4ab | 7012 | cp_parser_mem_initializer_list (cp_parser* parser) |
a723baf1 MM |
7013 | { |
7014 | tree mem_initializer_list = NULL_TREE; | |
7015 | ||
7016 | /* Let the semantic analysis code know that we are starting the | |
7017 | mem-initializer-list. */ | |
0e136342 MM |
7018 | if (!DECL_CONSTRUCTOR_P (current_function_decl)) |
7019 | error ("only constructors take base initializers"); | |
a723baf1 MM |
7020 | |
7021 | /* Loop through the list. */ | |
7022 | while (true) | |
7023 | { | |
7024 | tree mem_initializer; | |
7025 | ||
7026 | /* Parse the mem-initializer. */ | |
7027 | mem_initializer = cp_parser_mem_initializer (parser); | |
7028 | /* Add it to the list, unless it was erroneous. */ | |
7029 | if (mem_initializer) | |
7030 | { | |
7031 | TREE_CHAIN (mem_initializer) = mem_initializer_list; | |
7032 | mem_initializer_list = mem_initializer; | |
7033 | } | |
7034 | /* If the next token is not a `,', we're done. */ | |
7035 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
7036 | break; | |
7037 | /* Consume the `,' token. */ | |
7038 | cp_lexer_consume_token (parser->lexer); | |
7039 | } | |
7040 | ||
7041 | /* Perform semantic analysis. */ | |
0e136342 MM |
7042 | if (DECL_CONSTRUCTOR_P (current_function_decl)) |
7043 | finish_mem_initializers (mem_initializer_list); | |
a723baf1 MM |
7044 | } |
7045 | ||
7046 | /* Parse a mem-initializer. | |
7047 | ||
7048 | mem-initializer: | |
7049 | mem-initializer-id ( expression-list [opt] ) | |
7050 | ||
7051 | GNU extension: | |
7052 | ||
7053 | mem-initializer: | |
34cd5ae7 | 7054 | ( expression-list [opt] ) |
a723baf1 MM |
7055 | |
7056 | Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base | |
7057 | class) or FIELD_DECL (for a non-static data member) to initialize; | |
7058 | the TREE_VALUE is the expression-list. */ | |
7059 | ||
7060 | static tree | |
94edc4ab | 7061 | cp_parser_mem_initializer (cp_parser* parser) |
a723baf1 MM |
7062 | { |
7063 | tree mem_initializer_id; | |
7064 | tree expression_list; | |
1f5a253a NS |
7065 | tree member; |
7066 | ||
a723baf1 MM |
7067 | /* Find out what is being initialized. */ |
7068 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
7069 | { | |
7070 | pedwarn ("anachronistic old-style base class initializer"); | |
7071 | mem_initializer_id = NULL_TREE; | |
7072 | } | |
7073 | else | |
7074 | mem_initializer_id = cp_parser_mem_initializer_id (parser); | |
1f5a253a NS |
7075 | member = expand_member_init (mem_initializer_id); |
7076 | if (member && !DECL_P (member)) | |
7077 | in_base_initializer = 1; | |
7efa3e22 | 7078 | |
39703eb9 MM |
7079 | expression_list |
7080 | = cp_parser_parenthesized_expression_list (parser, false, | |
7081 | /*non_constant_p=*/NULL); | |
7efa3e22 | 7082 | if (!expression_list) |
a723baf1 | 7083 | expression_list = void_type_node; |
a723baf1 | 7084 | |
1f5a253a NS |
7085 | in_base_initializer = 0; |
7086 | ||
7087 | return member ? build_tree_list (member, expression_list) : NULL_TREE; | |
a723baf1 MM |
7088 | } |
7089 | ||
7090 | /* Parse a mem-initializer-id. | |
7091 | ||
7092 | mem-initializer-id: | |
7093 | :: [opt] nested-name-specifier [opt] class-name | |
7094 | identifier | |
7095 | ||
7096 | Returns a TYPE indicating the class to be initializer for the first | |
7097 | production. Returns an IDENTIFIER_NODE indicating the data member | |
7098 | to be initialized for the second production. */ | |
7099 | ||
7100 | static tree | |
94edc4ab | 7101 | cp_parser_mem_initializer_id (cp_parser* parser) |
a723baf1 MM |
7102 | { |
7103 | bool global_scope_p; | |
7104 | bool nested_name_specifier_p; | |
7105 | tree id; | |
7106 | ||
7107 | /* Look for the optional `::' operator. */ | |
7108 | global_scope_p | |
7109 | = (cp_parser_global_scope_opt (parser, | |
7110 | /*current_scope_valid_p=*/false) | |
7111 | != NULL_TREE); | |
7112 | /* Look for the optional nested-name-specifier. The simplest way to | |
7113 | implement: | |
7114 | ||
7115 | [temp.res] | |
7116 | ||
7117 | The keyword `typename' is not permitted in a base-specifier or | |
7118 | mem-initializer; in these contexts a qualified name that | |
7119 | depends on a template-parameter is implicitly assumed to be a | |
7120 | type name. | |
7121 | ||
7122 | is to assume that we have seen the `typename' keyword at this | |
7123 | point. */ | |
7124 | nested_name_specifier_p | |
7125 | = (cp_parser_nested_name_specifier_opt (parser, | |
7126 | /*typename_keyword_p=*/true, | |
7127 | /*check_dependency_p=*/true, | |
a668c6ad MM |
7128 | /*type_p=*/true, |
7129 | /*is_declaration=*/true) | |
a723baf1 MM |
7130 | != NULL_TREE); |
7131 | /* If there is a `::' operator or a nested-name-specifier, then we | |
7132 | are definitely looking for a class-name. */ | |
7133 | if (global_scope_p || nested_name_specifier_p) | |
7134 | return cp_parser_class_name (parser, | |
7135 | /*typename_keyword_p=*/true, | |
7136 | /*template_keyword_p=*/false, | |
7137 | /*type_p=*/false, | |
a723baf1 | 7138 | /*check_dependency_p=*/true, |
a668c6ad MM |
7139 | /*class_head_p=*/false, |
7140 | /*is_declaration=*/true); | |
a723baf1 MM |
7141 | /* Otherwise, we could also be looking for an ordinary identifier. */ |
7142 | cp_parser_parse_tentatively (parser); | |
7143 | /* Try a class-name. */ | |
7144 | id = cp_parser_class_name (parser, | |
7145 | /*typename_keyword_p=*/true, | |
7146 | /*template_keyword_p=*/false, | |
7147 | /*type_p=*/false, | |
a723baf1 | 7148 | /*check_dependency_p=*/true, |
a668c6ad MM |
7149 | /*class_head_p=*/false, |
7150 | /*is_declaration=*/true); | |
a723baf1 MM |
7151 | /* If we found one, we're done. */ |
7152 | if (cp_parser_parse_definitely (parser)) | |
7153 | return id; | |
7154 | /* Otherwise, look for an ordinary identifier. */ | |
7155 | return cp_parser_identifier (parser); | |
7156 | } | |
7157 | ||
7158 | /* Overloading [gram.over] */ | |
7159 | ||
7160 | /* Parse an operator-function-id. | |
7161 | ||
7162 | operator-function-id: | |
7163 | operator operator | |
7164 | ||
7165 | Returns an IDENTIFIER_NODE for the operator which is a | |
7166 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
7167 | ||
7168 | static tree | |
94edc4ab | 7169 | cp_parser_operator_function_id (cp_parser* parser) |
a723baf1 MM |
7170 | { |
7171 | /* Look for the `operator' keyword. */ | |
7172 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'")) | |
7173 | return error_mark_node; | |
7174 | /* And then the name of the operator itself. */ | |
7175 | return cp_parser_operator (parser); | |
7176 | } | |
7177 | ||
7178 | /* Parse an operator. | |
7179 | ||
7180 | operator: | |
7181 | new delete new[] delete[] + - * / % ^ & | ~ ! = < > | |
7182 | += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && | |
7183 | || ++ -- , ->* -> () [] | |
7184 | ||
7185 | GNU Extensions: | |
7186 | ||
7187 | operator: | |
7188 | <? >? <?= >?= | |
7189 | ||
7190 | Returns an IDENTIFIER_NODE for the operator which is a | |
7191 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
7192 | ||
7193 | static tree | |
94edc4ab | 7194 | cp_parser_operator (cp_parser* parser) |
a723baf1 MM |
7195 | { |
7196 | tree id = NULL_TREE; | |
7197 | cp_token *token; | |
7198 | ||
7199 | /* Peek at the next token. */ | |
7200 | token = cp_lexer_peek_token (parser->lexer); | |
7201 | /* Figure out which operator we have. */ | |
7202 | switch (token->type) | |
7203 | { | |
7204 | case CPP_KEYWORD: | |
7205 | { | |
7206 | enum tree_code op; | |
7207 | ||
7208 | /* The keyword should be either `new' or `delete'. */ | |
7209 | if (token->keyword == RID_NEW) | |
7210 | op = NEW_EXPR; | |
7211 | else if (token->keyword == RID_DELETE) | |
7212 | op = DELETE_EXPR; | |
7213 | else | |
7214 | break; | |
7215 | ||
7216 | /* Consume the `new' or `delete' token. */ | |
7217 | cp_lexer_consume_token (parser->lexer); | |
7218 | ||
7219 | /* Peek at the next token. */ | |
7220 | token = cp_lexer_peek_token (parser->lexer); | |
7221 | /* If it's a `[' token then this is the array variant of the | |
7222 | operator. */ | |
7223 | if (token->type == CPP_OPEN_SQUARE) | |
7224 | { | |
7225 | /* Consume the `[' token. */ | |
7226 | cp_lexer_consume_token (parser->lexer); | |
7227 | /* Look for the `]' token. */ | |
7228 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
7229 | id = ansi_opname (op == NEW_EXPR | |
7230 | ? VEC_NEW_EXPR : VEC_DELETE_EXPR); | |
7231 | } | |
7232 | /* Otherwise, we have the non-array variant. */ | |
7233 | else | |
7234 | id = ansi_opname (op); | |
7235 | ||
7236 | return id; | |
7237 | } | |
7238 | ||
7239 | case CPP_PLUS: | |
7240 | id = ansi_opname (PLUS_EXPR); | |
7241 | break; | |
7242 | ||
7243 | case CPP_MINUS: | |
7244 | id = ansi_opname (MINUS_EXPR); | |
7245 | break; | |
7246 | ||
7247 | case CPP_MULT: | |
7248 | id = ansi_opname (MULT_EXPR); | |
7249 | break; | |
7250 | ||
7251 | case CPP_DIV: | |
7252 | id = ansi_opname (TRUNC_DIV_EXPR); | |
7253 | break; | |
7254 | ||
7255 | case CPP_MOD: | |
7256 | id = ansi_opname (TRUNC_MOD_EXPR); | |
7257 | break; | |
7258 | ||
7259 | case CPP_XOR: | |
7260 | id = ansi_opname (BIT_XOR_EXPR); | |
7261 | break; | |
7262 | ||
7263 | case CPP_AND: | |
7264 | id = ansi_opname (BIT_AND_EXPR); | |
7265 | break; | |
7266 | ||
7267 | case CPP_OR: | |
7268 | id = ansi_opname (BIT_IOR_EXPR); | |
7269 | break; | |
7270 | ||
7271 | case CPP_COMPL: | |
7272 | id = ansi_opname (BIT_NOT_EXPR); | |
7273 | break; | |
7274 | ||
7275 | case CPP_NOT: | |
7276 | id = ansi_opname (TRUTH_NOT_EXPR); | |
7277 | break; | |
7278 | ||
7279 | case CPP_EQ: | |
7280 | id = ansi_assopname (NOP_EXPR); | |
7281 | break; | |
7282 | ||
7283 | case CPP_LESS: | |
7284 | id = ansi_opname (LT_EXPR); | |
7285 | break; | |
7286 | ||
7287 | case CPP_GREATER: | |
7288 | id = ansi_opname (GT_EXPR); | |
7289 | break; | |
7290 | ||
7291 | case CPP_PLUS_EQ: | |
7292 | id = ansi_assopname (PLUS_EXPR); | |
7293 | break; | |
7294 | ||
7295 | case CPP_MINUS_EQ: | |
7296 | id = ansi_assopname (MINUS_EXPR); | |
7297 | break; | |
7298 | ||
7299 | case CPP_MULT_EQ: | |
7300 | id = ansi_assopname (MULT_EXPR); | |
7301 | break; | |
7302 | ||
7303 | case CPP_DIV_EQ: | |
7304 | id = ansi_assopname (TRUNC_DIV_EXPR); | |
7305 | break; | |
7306 | ||
7307 | case CPP_MOD_EQ: | |
7308 | id = ansi_assopname (TRUNC_MOD_EXPR); | |
7309 | break; | |
7310 | ||
7311 | case CPP_XOR_EQ: | |
7312 | id = ansi_assopname (BIT_XOR_EXPR); | |
7313 | break; | |
7314 | ||
7315 | case CPP_AND_EQ: | |
7316 | id = ansi_assopname (BIT_AND_EXPR); | |
7317 | break; | |
7318 | ||
7319 | case CPP_OR_EQ: | |
7320 | id = ansi_assopname (BIT_IOR_EXPR); | |
7321 | break; | |
7322 | ||
7323 | case CPP_LSHIFT: | |
7324 | id = ansi_opname (LSHIFT_EXPR); | |
7325 | break; | |
7326 | ||
7327 | case CPP_RSHIFT: | |
7328 | id = ansi_opname (RSHIFT_EXPR); | |
7329 | break; | |
7330 | ||
7331 | case CPP_LSHIFT_EQ: | |
7332 | id = ansi_assopname (LSHIFT_EXPR); | |
7333 | break; | |
7334 | ||
7335 | case CPP_RSHIFT_EQ: | |
7336 | id = ansi_assopname (RSHIFT_EXPR); | |
7337 | break; | |
7338 | ||
7339 | case CPP_EQ_EQ: | |
7340 | id = ansi_opname (EQ_EXPR); | |
7341 | break; | |
7342 | ||
7343 | case CPP_NOT_EQ: | |
7344 | id = ansi_opname (NE_EXPR); | |
7345 | break; | |
7346 | ||
7347 | case CPP_LESS_EQ: | |
7348 | id = ansi_opname (LE_EXPR); | |
7349 | break; | |
7350 | ||
7351 | case CPP_GREATER_EQ: | |
7352 | id = ansi_opname (GE_EXPR); | |
7353 | break; | |
7354 | ||
7355 | case CPP_AND_AND: | |
7356 | id = ansi_opname (TRUTH_ANDIF_EXPR); | |
7357 | break; | |
7358 | ||
7359 | case CPP_OR_OR: | |
7360 | id = ansi_opname (TRUTH_ORIF_EXPR); | |
7361 | break; | |
7362 | ||
7363 | case CPP_PLUS_PLUS: | |
7364 | id = ansi_opname (POSTINCREMENT_EXPR); | |
7365 | break; | |
7366 | ||
7367 | case CPP_MINUS_MINUS: | |
7368 | id = ansi_opname (PREDECREMENT_EXPR); | |
7369 | break; | |
7370 | ||
7371 | case CPP_COMMA: | |
7372 | id = ansi_opname (COMPOUND_EXPR); | |
7373 | break; | |
7374 | ||
7375 | case CPP_DEREF_STAR: | |
7376 | id = ansi_opname (MEMBER_REF); | |
7377 | break; | |
7378 | ||
7379 | case CPP_DEREF: | |
7380 | id = ansi_opname (COMPONENT_REF); | |
7381 | break; | |
7382 | ||
7383 | case CPP_OPEN_PAREN: | |
7384 | /* Consume the `('. */ | |
7385 | cp_lexer_consume_token (parser->lexer); | |
7386 | /* Look for the matching `)'. */ | |
7387 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
7388 | return ansi_opname (CALL_EXPR); | |
7389 | ||
7390 | case CPP_OPEN_SQUARE: | |
7391 | /* Consume the `['. */ | |
7392 | cp_lexer_consume_token (parser->lexer); | |
7393 | /* Look for the matching `]'. */ | |
7394 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
7395 | return ansi_opname (ARRAY_REF); | |
7396 | ||
7397 | /* Extensions. */ | |
7398 | case CPP_MIN: | |
7399 | id = ansi_opname (MIN_EXPR); | |
7400 | break; | |
7401 | ||
7402 | case CPP_MAX: | |
7403 | id = ansi_opname (MAX_EXPR); | |
7404 | break; | |
7405 | ||
7406 | case CPP_MIN_EQ: | |
7407 | id = ansi_assopname (MIN_EXPR); | |
7408 | break; | |
7409 | ||
7410 | case CPP_MAX_EQ: | |
7411 | id = ansi_assopname (MAX_EXPR); | |
7412 | break; | |
7413 | ||
7414 | default: | |
7415 | /* Anything else is an error. */ | |
7416 | break; | |
7417 | } | |
7418 | ||
7419 | /* If we have selected an identifier, we need to consume the | |
7420 | operator token. */ | |
7421 | if (id) | |
7422 | cp_lexer_consume_token (parser->lexer); | |
7423 | /* Otherwise, no valid operator name was present. */ | |
7424 | else | |
7425 | { | |
7426 | cp_parser_error (parser, "expected operator"); | |
7427 | id = error_mark_node; | |
7428 | } | |
7429 | ||
7430 | return id; | |
7431 | } | |
7432 | ||
7433 | /* Parse a template-declaration. | |
7434 | ||
7435 | template-declaration: | |
7436 | export [opt] template < template-parameter-list > declaration | |
7437 | ||
7438 | If MEMBER_P is TRUE, this template-declaration occurs within a | |
7439 | class-specifier. | |
7440 | ||
7441 | The grammar rule given by the standard isn't correct. What | |
7442 | is really meant is: | |
7443 | ||
7444 | template-declaration: | |
7445 | export [opt] template-parameter-list-seq | |
7446 | decl-specifier-seq [opt] init-declarator [opt] ; | |
7447 | export [opt] template-parameter-list-seq | |
7448 | function-definition | |
7449 | ||
7450 | template-parameter-list-seq: | |
7451 | template-parameter-list-seq [opt] | |
7452 | template < template-parameter-list > */ | |
7453 | ||
7454 | static void | |
94edc4ab | 7455 | cp_parser_template_declaration (cp_parser* parser, bool member_p) |
a723baf1 MM |
7456 | { |
7457 | /* Check for `export'. */ | |
7458 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT)) | |
7459 | { | |
7460 | /* Consume the `export' token. */ | |
7461 | cp_lexer_consume_token (parser->lexer); | |
7462 | /* Warn that we do not support `export'. */ | |
7463 | warning ("keyword `export' not implemented, and will be ignored"); | |
7464 | } | |
7465 | ||
7466 | cp_parser_template_declaration_after_export (parser, member_p); | |
7467 | } | |
7468 | ||
7469 | /* Parse a template-parameter-list. | |
7470 | ||
7471 | template-parameter-list: | |
7472 | template-parameter | |
7473 | template-parameter-list , template-parameter | |
7474 | ||
7475 | Returns a TREE_LIST. Each node represents a template parameter. | |
7476 | The nodes are connected via their TREE_CHAINs. */ | |
7477 | ||
7478 | static tree | |
94edc4ab | 7479 | cp_parser_template_parameter_list (cp_parser* parser) |
a723baf1 MM |
7480 | { |
7481 | tree parameter_list = NULL_TREE; | |
7482 | ||
7483 | while (true) | |
7484 | { | |
7485 | tree parameter; | |
7486 | cp_token *token; | |
7487 | ||
7488 | /* Parse the template-parameter. */ | |
7489 | parameter = cp_parser_template_parameter (parser); | |
7490 | /* Add it to the list. */ | |
7491 | parameter_list = process_template_parm (parameter_list, | |
7492 | parameter); | |
7493 | ||
7494 | /* Peek at the next token. */ | |
7495 | token = cp_lexer_peek_token (parser->lexer); | |
7496 | /* If it's not a `,', we're done. */ | |
7497 | if (token->type != CPP_COMMA) | |
7498 | break; | |
7499 | /* Otherwise, consume the `,' token. */ | |
7500 | cp_lexer_consume_token (parser->lexer); | |
7501 | } | |
7502 | ||
7503 | return parameter_list; | |
7504 | } | |
7505 | ||
7506 | /* Parse a template-parameter. | |
7507 | ||
7508 | template-parameter: | |
7509 | type-parameter | |
7510 | parameter-declaration | |
7511 | ||
7512 | Returns a TREE_LIST. The TREE_VALUE represents the parameter. The | |
7513 | TREE_PURPOSE is the default value, if any. */ | |
7514 | ||
7515 | static tree | |
94edc4ab | 7516 | cp_parser_template_parameter (cp_parser* parser) |
a723baf1 MM |
7517 | { |
7518 | cp_token *token; | |
7519 | ||
7520 | /* Peek at the next token. */ | |
7521 | token = cp_lexer_peek_token (parser->lexer); | |
7522 | /* If it is `class' or `template', we have a type-parameter. */ | |
7523 | if (token->keyword == RID_TEMPLATE) | |
7524 | return cp_parser_type_parameter (parser); | |
7525 | /* If it is `class' or `typename' we do not know yet whether it is a | |
7526 | type parameter or a non-type parameter. Consider: | |
7527 | ||
7528 | template <typename T, typename T::X X> ... | |
7529 | ||
7530 | or: | |
7531 | ||
7532 | template <class C, class D*> ... | |
7533 | ||
7534 | Here, the first parameter is a type parameter, and the second is | |
7535 | a non-type parameter. We can tell by looking at the token after | |
7536 | the identifier -- if it is a `,', `=', or `>' then we have a type | |
7537 | parameter. */ | |
7538 | if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS) | |
7539 | { | |
7540 | /* Peek at the token after `class' or `typename'. */ | |
7541 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
7542 | /* If it's an identifier, skip it. */ | |
7543 | if (token->type == CPP_NAME) | |
7544 | token = cp_lexer_peek_nth_token (parser->lexer, 3); | |
7545 | /* Now, see if the token looks like the end of a template | |
7546 | parameter. */ | |
7547 | if (token->type == CPP_COMMA | |
7548 | || token->type == CPP_EQ | |
7549 | || token->type == CPP_GREATER) | |
7550 | return cp_parser_type_parameter (parser); | |
7551 | } | |
7552 | ||
7553 | /* Otherwise, it is a non-type parameter. | |
7554 | ||
7555 | [temp.param] | |
7556 | ||
7557 | When parsing a default template-argument for a non-type | |
7558 | template-parameter, the first non-nested `>' is taken as the end | |
7559 | of the template parameter-list rather than a greater-than | |
7560 | operator. */ | |
7561 | return | |
4bb8ca28 MM |
7562 | cp_parser_parameter_declaration (parser, /*template_parm_p=*/true, |
7563 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
7564 | } |
7565 | ||
7566 | /* Parse a type-parameter. | |
7567 | ||
7568 | type-parameter: | |
7569 | class identifier [opt] | |
7570 | class identifier [opt] = type-id | |
7571 | typename identifier [opt] | |
7572 | typename identifier [opt] = type-id | |
7573 | template < template-parameter-list > class identifier [opt] | |
7574 | template < template-parameter-list > class identifier [opt] | |
7575 | = id-expression | |
7576 | ||
7577 | Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The | |
7578 | TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is | |
7579 | the declaration of the parameter. */ | |
7580 | ||
7581 | static tree | |
94edc4ab | 7582 | cp_parser_type_parameter (cp_parser* parser) |
a723baf1 MM |
7583 | { |
7584 | cp_token *token; | |
7585 | tree parameter; | |
7586 | ||
7587 | /* Look for a keyword to tell us what kind of parameter this is. */ | |
7588 | token = cp_parser_require (parser, CPP_KEYWORD, | |
8a6393df | 7589 | "`class', `typename', or `template'"); |
a723baf1 MM |
7590 | if (!token) |
7591 | return error_mark_node; | |
7592 | ||
7593 | switch (token->keyword) | |
7594 | { | |
7595 | case RID_CLASS: | |
7596 | case RID_TYPENAME: | |
7597 | { | |
7598 | tree identifier; | |
7599 | tree default_argument; | |
7600 | ||
7601 | /* If the next token is an identifier, then it names the | |
7602 | parameter. */ | |
7603 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
7604 | identifier = cp_parser_identifier (parser); | |
7605 | else | |
7606 | identifier = NULL_TREE; | |
7607 | ||
7608 | /* Create the parameter. */ | |
7609 | parameter = finish_template_type_parm (class_type_node, identifier); | |
7610 | ||
7611 | /* If the next token is an `=', we have a default argument. */ | |
7612 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
7613 | { | |
7614 | /* Consume the `=' token. */ | |
7615 | cp_lexer_consume_token (parser->lexer); | |
34cd5ae7 | 7616 | /* Parse the default-argument. */ |
a723baf1 MM |
7617 | default_argument = cp_parser_type_id (parser); |
7618 | } | |
7619 | else | |
7620 | default_argument = NULL_TREE; | |
7621 | ||
7622 | /* Create the combined representation of the parameter and the | |
7623 | default argument. */ | |
c67d36d0 | 7624 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
7625 | } |
7626 | break; | |
7627 | ||
7628 | case RID_TEMPLATE: | |
7629 | { | |
7630 | tree parameter_list; | |
7631 | tree identifier; | |
7632 | tree default_argument; | |
7633 | ||
7634 | /* Look for the `<'. */ | |
7635 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
7636 | /* Parse the template-parameter-list. */ | |
7637 | begin_template_parm_list (); | |
7638 | parameter_list | |
7639 | = cp_parser_template_parameter_list (parser); | |
7640 | parameter_list = end_template_parm_list (parameter_list); | |
7641 | /* Look for the `>'. */ | |
7642 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
7643 | /* Look for the `class' keyword. */ | |
7644 | cp_parser_require_keyword (parser, RID_CLASS, "`class'"); | |
7645 | /* If the next token is an `=', then there is a | |
7646 | default-argument. If the next token is a `>', we are at | |
7647 | the end of the parameter-list. If the next token is a `,', | |
7648 | then we are at the end of this parameter. */ | |
7649 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ) | |
7650 | && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER) | |
7651 | && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
7652 | identifier = cp_parser_identifier (parser); | |
7653 | else | |
7654 | identifier = NULL_TREE; | |
7655 | /* Create the template parameter. */ | |
7656 | parameter = finish_template_template_parm (class_type_node, | |
7657 | identifier); | |
7658 | ||
7659 | /* If the next token is an `=', then there is a | |
7660 | default-argument. */ | |
7661 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
7662 | { | |
b0bc6e8e KL |
7663 | bool is_template; |
7664 | ||
a723baf1 MM |
7665 | /* Consume the `='. */ |
7666 | cp_lexer_consume_token (parser->lexer); | |
7667 | /* Parse the id-expression. */ | |
7668 | default_argument | |
7669 | = cp_parser_id_expression (parser, | |
7670 | /*template_keyword_p=*/false, | |
7671 | /*check_dependency_p=*/true, | |
b0bc6e8e | 7672 | /*template_p=*/&is_template, |
f3c2dfc6 | 7673 | /*declarator_p=*/false); |
a723baf1 MM |
7674 | /* Look up the name. */ |
7675 | default_argument | |
b0bc6e8e KL |
7676 | = cp_parser_lookup_name (parser, default_argument, |
7677 | /*is_type=*/false, | |
7678 | /*is_template=*/is_template, | |
7679 | /*is_namespace=*/false, | |
7680 | /*check_dependency=*/true); | |
a723baf1 MM |
7681 | /* See if the default argument is valid. */ |
7682 | default_argument | |
7683 | = check_template_template_default_arg (default_argument); | |
7684 | } | |
7685 | else | |
7686 | default_argument = NULL_TREE; | |
7687 | ||
7688 | /* Create the combined representation of the parameter and the | |
7689 | default argument. */ | |
c67d36d0 | 7690 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
7691 | } |
7692 | break; | |
7693 | ||
7694 | default: | |
7695 | /* Anything else is an error. */ | |
7696 | cp_parser_error (parser, | |
7697 | "expected `class', `typename', or `template'"); | |
7698 | parameter = error_mark_node; | |
7699 | } | |
7700 | ||
7701 | return parameter; | |
7702 | } | |
7703 | ||
7704 | /* Parse a template-id. | |
7705 | ||
7706 | template-id: | |
7707 | template-name < template-argument-list [opt] > | |
7708 | ||
7709 | If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the | |
7710 | `template' keyword. In this case, a TEMPLATE_ID_EXPR will be | |
7711 | returned. Otherwise, if the template-name names a function, or set | |
7712 | of functions, returns a TEMPLATE_ID_EXPR. If the template-name | |
7713 | names a class, returns a TYPE_DECL for the specialization. | |
7714 | ||
7715 | If CHECK_DEPENDENCY_P is FALSE, names are looked up in | |
7716 | uninstantiated templates. */ | |
7717 | ||
7718 | static tree | |
7719 | cp_parser_template_id (cp_parser *parser, | |
7720 | bool template_keyword_p, | |
a668c6ad MM |
7721 | bool check_dependency_p, |
7722 | bool is_declaration) | |
a723baf1 MM |
7723 | { |
7724 | tree template; | |
7725 | tree arguments; | |
a723baf1 | 7726 | tree template_id; |
a723baf1 MM |
7727 | ptrdiff_t start_of_id; |
7728 | tree access_check = NULL_TREE; | |
2050a1bb | 7729 | cp_token *next_token; |
a668c6ad | 7730 | bool is_identifier; |
a723baf1 MM |
7731 | |
7732 | /* If the next token corresponds to a template-id, there is no need | |
7733 | to reparse it. */ | |
2050a1bb MM |
7734 | next_token = cp_lexer_peek_token (parser->lexer); |
7735 | if (next_token->type == CPP_TEMPLATE_ID) | |
a723baf1 MM |
7736 | { |
7737 | tree value; | |
7738 | tree check; | |
7739 | ||
7740 | /* Get the stored value. */ | |
7741 | value = cp_lexer_consume_token (parser->lexer)->value; | |
7742 | /* Perform any access checks that were deferred. */ | |
7743 | for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check)) | |
cf22909c KL |
7744 | perform_or_defer_access_check (TREE_PURPOSE (check), |
7745 | TREE_VALUE (check)); | |
a723baf1 MM |
7746 | /* Return the stored value. */ |
7747 | return TREE_VALUE (value); | |
7748 | } | |
7749 | ||
2050a1bb MM |
7750 | /* Avoid performing name lookup if there is no possibility of |
7751 | finding a template-id. */ | |
7752 | if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR) | |
7753 | || (next_token->type == CPP_NAME | |
7754 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)) | |
7755 | { | |
7756 | cp_parser_error (parser, "expected template-id"); | |
7757 | return error_mark_node; | |
7758 | } | |
7759 | ||
a723baf1 MM |
7760 | /* Remember where the template-id starts. */ |
7761 | if (cp_parser_parsing_tentatively (parser) | |
7762 | && !cp_parser_committed_to_tentative_parse (parser)) | |
7763 | { | |
2050a1bb | 7764 | next_token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
7765 | start_of_id = cp_lexer_token_difference (parser->lexer, |
7766 | parser->lexer->first_token, | |
7767 | next_token); | |
a723baf1 MM |
7768 | } |
7769 | else | |
7770 | start_of_id = -1; | |
7771 | ||
8d241e0b | 7772 | push_deferring_access_checks (dk_deferred); |
cf22909c | 7773 | |
a723baf1 | 7774 | /* Parse the template-name. */ |
a668c6ad | 7775 | is_identifier = false; |
a723baf1 | 7776 | template = cp_parser_template_name (parser, template_keyword_p, |
a668c6ad MM |
7777 | check_dependency_p, |
7778 | is_declaration, | |
7779 | &is_identifier); | |
7780 | if (template == error_mark_node || is_identifier) | |
cf22909c KL |
7781 | { |
7782 | pop_deferring_access_checks (); | |
a668c6ad | 7783 | return template; |
cf22909c | 7784 | } |
a723baf1 MM |
7785 | |
7786 | /* Look for the `<' that starts the template-argument-list. */ | |
7787 | if (!cp_parser_require (parser, CPP_LESS, "`<'")) | |
cf22909c KL |
7788 | { |
7789 | pop_deferring_access_checks (); | |
7790 | return error_mark_node; | |
7791 | } | |
a723baf1 | 7792 | |
ec75414f MM |
7793 | /* Parse the arguments. */ |
7794 | arguments = cp_parser_enclosed_template_argument_list (parser); | |
a723baf1 MM |
7795 | |
7796 | /* Build a representation of the specialization. */ | |
7797 | if (TREE_CODE (template) == IDENTIFIER_NODE) | |
7798 | template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments); | |
7799 | else if (DECL_CLASS_TEMPLATE_P (template) | |
7800 | || DECL_TEMPLATE_TEMPLATE_PARM_P (template)) | |
7801 | template_id | |
7802 | = finish_template_type (template, arguments, | |
7803 | cp_lexer_next_token_is (parser->lexer, | |
7804 | CPP_SCOPE)); | |
7805 | else | |
7806 | { | |
7807 | /* If it's not a class-template or a template-template, it should be | |
7808 | a function-template. */ | |
7809 | my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template) | |
7810 | || TREE_CODE (template) == OVERLOAD | |
7811 | || BASELINK_P (template)), | |
7812 | 20010716); | |
7813 | ||
7814 | template_id = lookup_template_function (template, arguments); | |
7815 | } | |
7816 | ||
cf22909c KL |
7817 | /* Retrieve any deferred checks. Do not pop this access checks yet |
7818 | so the memory will not be reclaimed during token replacing below. */ | |
7819 | access_check = get_deferred_access_checks (); | |
7820 | ||
a723baf1 MM |
7821 | /* If parsing tentatively, replace the sequence of tokens that makes |
7822 | up the template-id with a CPP_TEMPLATE_ID token. That way, | |
7823 | should we re-parse the token stream, we will not have to repeat | |
7824 | the effort required to do the parse, nor will we issue duplicate | |
7825 | error messages about problems during instantiation of the | |
7826 | template. */ | |
7827 | if (start_of_id >= 0) | |
7828 | { | |
7829 | cp_token *token; | |
a723baf1 MM |
7830 | |
7831 | /* Find the token that corresponds to the start of the | |
7832 | template-id. */ | |
7833 | token = cp_lexer_advance_token (parser->lexer, | |
7834 | parser->lexer->first_token, | |
7835 | start_of_id); | |
7836 | ||
a723baf1 MM |
7837 | /* Reset the contents of the START_OF_ID token. */ |
7838 | token->type = CPP_TEMPLATE_ID; | |
7839 | token->value = build_tree_list (access_check, template_id); | |
7840 | token->keyword = RID_MAX; | |
7841 | /* Purge all subsequent tokens. */ | |
7842 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
7843 | } | |
7844 | ||
cf22909c | 7845 | pop_deferring_access_checks (); |
a723baf1 MM |
7846 | return template_id; |
7847 | } | |
7848 | ||
7849 | /* Parse a template-name. | |
7850 | ||
7851 | template-name: | |
7852 | identifier | |
7853 | ||
7854 | The standard should actually say: | |
7855 | ||
7856 | template-name: | |
7857 | identifier | |
7858 | operator-function-id | |
7859 | conversion-function-id | |
7860 | ||
7861 | A defect report has been filed about this issue. | |
7862 | ||
7863 | If TEMPLATE_KEYWORD_P is true, then we have just seen the | |
7864 | `template' keyword, in a construction like: | |
7865 | ||
7866 | T::template f<3>() | |
7867 | ||
7868 | In that case `f' is taken to be a template-name, even though there | |
7869 | is no way of knowing for sure. | |
7870 | ||
7871 | Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the | |
7872 | name refers to a set of overloaded functions, at least one of which | |
7873 | is a template, or an IDENTIFIER_NODE with the name of the template, | |
7874 | if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE, | |
7875 | names are looked up inside uninstantiated templates. */ | |
7876 | ||
7877 | static tree | |
94edc4ab NN |
7878 | cp_parser_template_name (cp_parser* parser, |
7879 | bool template_keyword_p, | |
a668c6ad MM |
7880 | bool check_dependency_p, |
7881 | bool is_declaration, | |
7882 | bool *is_identifier) | |
a723baf1 MM |
7883 | { |
7884 | tree identifier; | |
7885 | tree decl; | |
7886 | tree fns; | |
7887 | ||
7888 | /* If the next token is `operator', then we have either an | |
7889 | operator-function-id or a conversion-function-id. */ | |
7890 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR)) | |
7891 | { | |
7892 | /* We don't know whether we're looking at an | |
7893 | operator-function-id or a conversion-function-id. */ | |
7894 | cp_parser_parse_tentatively (parser); | |
7895 | /* Try an operator-function-id. */ | |
7896 | identifier = cp_parser_operator_function_id (parser); | |
7897 | /* If that didn't work, try a conversion-function-id. */ | |
7898 | if (!cp_parser_parse_definitely (parser)) | |
7899 | identifier = cp_parser_conversion_function_id (parser); | |
7900 | } | |
7901 | /* Look for the identifier. */ | |
7902 | else | |
7903 | identifier = cp_parser_identifier (parser); | |
7904 | ||
7905 | /* If we didn't find an identifier, we don't have a template-id. */ | |
7906 | if (identifier == error_mark_node) | |
7907 | return error_mark_node; | |
7908 | ||
7909 | /* If the name immediately followed the `template' keyword, then it | |
7910 | is a template-name. However, if the next token is not `<', then | |
7911 | we do not treat it as a template-name, since it is not being used | |
7912 | as part of a template-id. This enables us to handle constructs | |
7913 | like: | |
7914 | ||
7915 | template <typename T> struct S { S(); }; | |
7916 | template <typename T> S<T>::S(); | |
7917 | ||
7918 | correctly. We would treat `S' as a template -- if it were `S<T>' | |
7919 | -- but we do not if there is no `<'. */ | |
a668c6ad MM |
7920 | |
7921 | if (processing_template_decl | |
a723baf1 | 7922 | && cp_lexer_next_token_is (parser->lexer, CPP_LESS)) |
a668c6ad MM |
7923 | { |
7924 | /* In a declaration, in a dependent context, we pretend that the | |
7925 | "template" keyword was present in order to improve error | |
7926 | recovery. For example, given: | |
7927 | ||
7928 | template <typename T> void f(T::X<int>); | |
7929 | ||
7930 | we want to treat "X<int>" as a template-id. */ | |
7931 | if (is_declaration | |
7932 | && !template_keyword_p | |
7933 | && parser->scope && TYPE_P (parser->scope) | |
7934 | && dependent_type_p (parser->scope)) | |
7935 | { | |
7936 | ptrdiff_t start; | |
7937 | cp_token* token; | |
7938 | /* Explain what went wrong. */ | |
7939 | error ("non-template `%D' used as template", identifier); | |
7940 | error ("(use `%T::template %D' to indicate that it is a template)", | |
7941 | parser->scope, identifier); | |
7942 | /* If parsing tentatively, find the location of the "<" | |
7943 | token. */ | |
7944 | if (cp_parser_parsing_tentatively (parser) | |
7945 | && !cp_parser_committed_to_tentative_parse (parser)) | |
7946 | { | |
7947 | cp_parser_simulate_error (parser); | |
7948 | token = cp_lexer_peek_token (parser->lexer); | |
7949 | token = cp_lexer_prev_token (parser->lexer, token); | |
7950 | start = cp_lexer_token_difference (parser->lexer, | |
7951 | parser->lexer->first_token, | |
7952 | token); | |
7953 | } | |
7954 | else | |
7955 | start = -1; | |
7956 | /* Parse the template arguments so that we can issue error | |
7957 | messages about them. */ | |
7958 | cp_lexer_consume_token (parser->lexer); | |
7959 | cp_parser_enclosed_template_argument_list (parser); | |
7960 | /* Skip tokens until we find a good place from which to | |
7961 | continue parsing. */ | |
7962 | cp_parser_skip_to_closing_parenthesis (parser, | |
7963 | /*recovering=*/true, | |
7964 | /*or_comma=*/true, | |
7965 | /*consume_paren=*/false); | |
7966 | /* If parsing tentatively, permanently remove the | |
7967 | template argument list. That will prevent duplicate | |
7968 | error messages from being issued about the missing | |
7969 | "template" keyword. */ | |
7970 | if (start >= 0) | |
7971 | { | |
7972 | token = cp_lexer_advance_token (parser->lexer, | |
7973 | parser->lexer->first_token, | |
7974 | start); | |
7975 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
7976 | } | |
7977 | if (is_identifier) | |
7978 | *is_identifier = true; | |
7979 | return identifier; | |
7980 | } | |
7981 | if (template_keyword_p) | |
7982 | return identifier; | |
7983 | } | |
a723baf1 MM |
7984 | |
7985 | /* Look up the name. */ | |
7986 | decl = cp_parser_lookup_name (parser, identifier, | |
a723baf1 | 7987 | /*is_type=*/false, |
b0bc6e8e | 7988 | /*is_template=*/false, |
eea9800f | 7989 | /*is_namespace=*/false, |
a723baf1 MM |
7990 | check_dependency_p); |
7991 | decl = maybe_get_template_decl_from_type_decl (decl); | |
7992 | ||
7993 | /* If DECL is a template, then the name was a template-name. */ | |
7994 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
7995 | ; | |
7996 | else | |
7997 | { | |
7998 | /* The standard does not explicitly indicate whether a name that | |
7999 | names a set of overloaded declarations, some of which are | |
8000 | templates, is a template-name. However, such a name should | |
8001 | be a template-name; otherwise, there is no way to form a | |
8002 | template-id for the overloaded templates. */ | |
8003 | fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl; | |
8004 | if (TREE_CODE (fns) == OVERLOAD) | |
8005 | { | |
8006 | tree fn; | |
8007 | ||
8008 | for (fn = fns; fn; fn = OVL_NEXT (fn)) | |
8009 | if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL) | |
8010 | break; | |
8011 | } | |
8012 | else | |
8013 | { | |
8014 | /* Otherwise, the name does not name a template. */ | |
8015 | cp_parser_error (parser, "expected template-name"); | |
8016 | return error_mark_node; | |
8017 | } | |
8018 | } | |
8019 | ||
8020 | /* If DECL is dependent, and refers to a function, then just return | |
8021 | its name; we will look it up again during template instantiation. */ | |
8022 | if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl)) | |
8023 | { | |
8024 | tree scope = CP_DECL_CONTEXT (get_first_fn (decl)); | |
1fb3244a | 8025 | if (TYPE_P (scope) && dependent_type_p (scope)) |
a723baf1 MM |
8026 | return identifier; |
8027 | } | |
8028 | ||
8029 | return decl; | |
8030 | } | |
8031 | ||
8032 | /* Parse a template-argument-list. | |
8033 | ||
8034 | template-argument-list: | |
8035 | template-argument | |
8036 | template-argument-list , template-argument | |
8037 | ||
04c06002 | 8038 | Returns a TREE_VEC containing the arguments. */ |
a723baf1 MM |
8039 | |
8040 | static tree | |
94edc4ab | 8041 | cp_parser_template_argument_list (cp_parser* parser) |
a723baf1 | 8042 | { |
bf12d54d NS |
8043 | tree fixed_args[10]; |
8044 | unsigned n_args = 0; | |
8045 | unsigned alloced = 10; | |
8046 | tree *arg_ary = fixed_args; | |
8047 | tree vec; | |
4bb8ca28 | 8048 | bool saved_in_template_argument_list_p; |
a723baf1 | 8049 | |
4bb8ca28 MM |
8050 | saved_in_template_argument_list_p = parser->in_template_argument_list_p; |
8051 | parser->in_template_argument_list_p = true; | |
bf12d54d | 8052 | do |
a723baf1 MM |
8053 | { |
8054 | tree argument; | |
8055 | ||
bf12d54d | 8056 | if (n_args) |
04c06002 | 8057 | /* Consume the comma. */ |
bf12d54d NS |
8058 | cp_lexer_consume_token (parser->lexer); |
8059 | ||
a723baf1 MM |
8060 | /* Parse the template-argument. */ |
8061 | argument = cp_parser_template_argument (parser); | |
bf12d54d NS |
8062 | if (n_args == alloced) |
8063 | { | |
8064 | alloced *= 2; | |
8065 | ||
8066 | if (arg_ary == fixed_args) | |
8067 | { | |
8068 | arg_ary = xmalloc (sizeof (tree) * alloced); | |
8069 | memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); | |
8070 | } | |
8071 | else | |
8072 | arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced); | |
8073 | } | |
8074 | arg_ary[n_args++] = argument; | |
a723baf1 | 8075 | } |
bf12d54d NS |
8076 | while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); |
8077 | ||
8078 | vec = make_tree_vec (n_args); | |
a723baf1 | 8079 | |
bf12d54d NS |
8080 | while (n_args--) |
8081 | TREE_VEC_ELT (vec, n_args) = arg_ary[n_args]; | |
8082 | ||
8083 | if (arg_ary != fixed_args) | |
8084 | free (arg_ary); | |
4bb8ca28 | 8085 | parser->in_template_argument_list_p = saved_in_template_argument_list_p; |
bf12d54d | 8086 | return vec; |
a723baf1 MM |
8087 | } |
8088 | ||
8089 | /* Parse a template-argument. | |
8090 | ||
8091 | template-argument: | |
8092 | assignment-expression | |
8093 | type-id | |
8094 | id-expression | |
8095 | ||
8096 | The representation is that of an assignment-expression, type-id, or | |
8097 | id-expression -- except that the qualified id-expression is | |
8098 | evaluated, so that the value returned is either a DECL or an | |
d17811fd MM |
8099 | OVERLOAD. |
8100 | ||
8101 | Although the standard says "assignment-expression", it forbids | |
8102 | throw-expressions or assignments in the template argument. | |
8103 | Therefore, we use "conditional-expression" instead. */ | |
a723baf1 MM |
8104 | |
8105 | static tree | |
94edc4ab | 8106 | cp_parser_template_argument (cp_parser* parser) |
a723baf1 MM |
8107 | { |
8108 | tree argument; | |
8109 | bool template_p; | |
d17811fd | 8110 | bool address_p; |
4d5297fa | 8111 | bool maybe_type_id = false; |
d17811fd | 8112 | cp_token *token; |
b3445994 | 8113 | cp_id_kind idk; |
d17811fd | 8114 | tree qualifying_class; |
a723baf1 MM |
8115 | |
8116 | /* There's really no way to know what we're looking at, so we just | |
8117 | try each alternative in order. | |
8118 | ||
8119 | [temp.arg] | |
8120 | ||
8121 | In a template-argument, an ambiguity between a type-id and an | |
8122 | expression is resolved to a type-id, regardless of the form of | |
8123 | the corresponding template-parameter. | |
8124 | ||
8125 | Therefore, we try a type-id first. */ | |
8126 | cp_parser_parse_tentatively (parser); | |
a723baf1 | 8127 | argument = cp_parser_type_id (parser); |
4d5297fa GB |
8128 | /* If there was no error parsing the type-id but the next token is a '>>', |
8129 | we probably found a typo for '> >'. But there are type-id which are | |
8130 | also valid expressions. For instance: | |
8131 | ||
8132 | struct X { int operator >> (int); }; | |
8133 | template <int V> struct Foo {}; | |
8134 | Foo<X () >> 5> r; | |
8135 | ||
8136 | Here 'X()' is a valid type-id of a function type, but the user just | |
8137 | wanted to write the expression "X() >> 5". Thus, we remember that we | |
8138 | found a valid type-id, but we still try to parse the argument as an | |
8139 | expression to see what happens. */ | |
8140 | if (!cp_parser_error_occurred (parser) | |
8141 | && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) | |
8142 | { | |
8143 | maybe_type_id = true; | |
8144 | cp_parser_abort_tentative_parse (parser); | |
8145 | } | |
8146 | else | |
8147 | { | |
8148 | /* If the next token isn't a `,' or a `>', then this argument wasn't | |
8149 | really finished. This means that the argument is not a valid | |
8150 | type-id. */ | |
8151 | if (!cp_parser_next_token_ends_template_argument_p (parser)) | |
8152 | cp_parser_error (parser, "expected template-argument"); | |
8153 | /* If that worked, we're done. */ | |
8154 | if (cp_parser_parse_definitely (parser)) | |
8155 | return argument; | |
8156 | } | |
a723baf1 MM |
8157 | /* We're still not sure what the argument will be. */ |
8158 | cp_parser_parse_tentatively (parser); | |
8159 | /* Try a template. */ | |
8160 | argument = cp_parser_id_expression (parser, | |
8161 | /*template_keyword_p=*/false, | |
8162 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
8163 | &template_p, |
8164 | /*declarator_p=*/false); | |
a723baf1 MM |
8165 | /* If the next token isn't a `,' or a `>', then this argument wasn't |
8166 | really finished. */ | |
d17811fd | 8167 | if (!cp_parser_next_token_ends_template_argument_p (parser)) |
a723baf1 MM |
8168 | cp_parser_error (parser, "expected template-argument"); |
8169 | if (!cp_parser_error_occurred (parser)) | |
8170 | { | |
8171 | /* Figure out what is being referred to. */ | |
5b4acce1 KL |
8172 | argument = cp_parser_lookup_name (parser, argument, |
8173 | /*is_type=*/false, | |
8174 | /*is_template=*/template_p, | |
8175 | /*is_namespace=*/false, | |
8176 | /*check_dependency=*/true); | |
8177 | if (TREE_CODE (argument) != TEMPLATE_DECL | |
8178 | && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE) | |
a723baf1 MM |
8179 | cp_parser_error (parser, "expected template-name"); |
8180 | } | |
8181 | if (cp_parser_parse_definitely (parser)) | |
8182 | return argument; | |
d17811fd MM |
8183 | /* It must be a non-type argument. There permitted cases are given |
8184 | in [temp.arg.nontype]: | |
8185 | ||
8186 | -- an integral constant-expression of integral or enumeration | |
8187 | type; or | |
8188 | ||
8189 | -- the name of a non-type template-parameter; or | |
8190 | ||
8191 | -- the name of an object or function with external linkage... | |
8192 | ||
8193 | -- the address of an object or function with external linkage... | |
8194 | ||
04c06002 | 8195 | -- a pointer to member... */ |
d17811fd MM |
8196 | /* Look for a non-type template parameter. */ |
8197 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
8198 | { | |
8199 | cp_parser_parse_tentatively (parser); | |
8200 | argument = cp_parser_primary_expression (parser, | |
8201 | &idk, | |
8202 | &qualifying_class); | |
8203 | if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX | |
8204 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
8205 | cp_parser_simulate_error (parser); | |
8206 | if (cp_parser_parse_definitely (parser)) | |
8207 | return argument; | |
8208 | } | |
8209 | /* If the next token is "&", the argument must be the address of an | |
8210 | object or function with external linkage. */ | |
8211 | address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND); | |
8212 | if (address_p) | |
8213 | cp_lexer_consume_token (parser->lexer); | |
8214 | /* See if we might have an id-expression. */ | |
8215 | token = cp_lexer_peek_token (parser->lexer); | |
8216 | if (token->type == CPP_NAME | |
8217 | || token->keyword == RID_OPERATOR | |
8218 | || token->type == CPP_SCOPE | |
8219 | || token->type == CPP_TEMPLATE_ID | |
8220 | || token->type == CPP_NESTED_NAME_SPECIFIER) | |
8221 | { | |
8222 | cp_parser_parse_tentatively (parser); | |
8223 | argument = cp_parser_primary_expression (parser, | |
8224 | &idk, | |
8225 | &qualifying_class); | |
8226 | if (cp_parser_error_occurred (parser) | |
8227 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
8228 | cp_parser_abort_tentative_parse (parser); | |
8229 | else | |
8230 | { | |
8231 | if (qualifying_class) | |
8232 | argument = finish_qualified_id_expr (qualifying_class, | |
8233 | argument, | |
8234 | /*done=*/true, | |
8235 | address_p); | |
8236 | if (TREE_CODE (argument) == VAR_DECL) | |
8237 | { | |
8238 | /* A variable without external linkage might still be a | |
8239 | valid constant-expression, so no error is issued here | |
8240 | if the external-linkage check fails. */ | |
8241 | if (!DECL_EXTERNAL_LINKAGE_P (argument)) | |
8242 | cp_parser_simulate_error (parser); | |
8243 | } | |
8244 | else if (is_overloaded_fn (argument)) | |
8245 | /* All overloaded functions are allowed; if the external | |
8246 | linkage test does not pass, an error will be issued | |
8247 | later. */ | |
8248 | ; | |
8249 | else if (address_p | |
8250 | && (TREE_CODE (argument) == OFFSET_REF | |
8251 | || TREE_CODE (argument) == SCOPE_REF)) | |
8252 | /* A pointer-to-member. */ | |
8253 | ; | |
8254 | else | |
8255 | cp_parser_simulate_error (parser); | |
8256 | ||
8257 | if (cp_parser_parse_definitely (parser)) | |
8258 | { | |
8259 | if (address_p) | |
8260 | argument = build_x_unary_op (ADDR_EXPR, argument); | |
8261 | return argument; | |
8262 | } | |
8263 | } | |
8264 | } | |
8265 | /* If the argument started with "&", there are no other valid | |
8266 | alternatives at this point. */ | |
8267 | if (address_p) | |
8268 | { | |
8269 | cp_parser_error (parser, "invalid non-type template argument"); | |
8270 | return error_mark_node; | |
8271 | } | |
4d5297fa GB |
8272 | /* If the argument wasn't successfully parsed as a type-id followed |
8273 | by '>>', the argument can only be a constant expression now. | |
8274 | Otherwise, we try parsing the constant-expression tentatively, | |
8275 | because the argument could really be a type-id. */ | |
8276 | if (maybe_type_id) | |
8277 | cp_parser_parse_tentatively (parser); | |
d17811fd MM |
8278 | argument = cp_parser_constant_expression (parser, |
8279 | /*allow_non_constant_p=*/false, | |
8280 | /*non_constant_p=*/NULL); | |
4d5297fa GB |
8281 | argument = cp_parser_fold_non_dependent_expr (argument); |
8282 | if (!maybe_type_id) | |
8283 | return argument; | |
8284 | if (!cp_parser_next_token_ends_template_argument_p (parser)) | |
8285 | cp_parser_error (parser, "expected template-argument"); | |
8286 | if (cp_parser_parse_definitely (parser)) | |
8287 | return argument; | |
8288 | /* We did our best to parse the argument as a non type-id, but that | |
8289 | was the only alternative that matched (albeit with a '>' after | |
8290 | it). We can assume it's just a typo from the user, and a | |
8291 | diagnostic will then be issued. */ | |
8292 | return cp_parser_type_id (parser); | |
a723baf1 MM |
8293 | } |
8294 | ||
8295 | /* Parse an explicit-instantiation. | |
8296 | ||
8297 | explicit-instantiation: | |
8298 | template declaration | |
8299 | ||
8300 | Although the standard says `declaration', what it really means is: | |
8301 | ||
8302 | explicit-instantiation: | |
8303 | template decl-specifier-seq [opt] declarator [opt] ; | |
8304 | ||
8305 | Things like `template int S<int>::i = 5, int S<double>::j;' are not | |
8306 | supposed to be allowed. A defect report has been filed about this | |
8307 | issue. | |
8308 | ||
8309 | GNU Extension: | |
8310 | ||
8311 | explicit-instantiation: | |
8312 | storage-class-specifier template | |
8313 | decl-specifier-seq [opt] declarator [opt] ; | |
8314 | function-specifier template | |
8315 | decl-specifier-seq [opt] declarator [opt] ; */ | |
8316 | ||
8317 | static void | |
94edc4ab | 8318 | cp_parser_explicit_instantiation (cp_parser* parser) |
a723baf1 | 8319 | { |
560ad596 | 8320 | int declares_class_or_enum; |
a723baf1 MM |
8321 | tree decl_specifiers; |
8322 | tree attributes; | |
8323 | tree extension_specifier = NULL_TREE; | |
8324 | ||
8325 | /* Look for an (optional) storage-class-specifier or | |
8326 | function-specifier. */ | |
8327 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
8328 | { | |
8329 | extension_specifier | |
8330 | = cp_parser_storage_class_specifier_opt (parser); | |
8331 | if (!extension_specifier) | |
8332 | extension_specifier = cp_parser_function_specifier_opt (parser); | |
8333 | } | |
8334 | ||
8335 | /* Look for the `template' keyword. */ | |
8336 | cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"); | |
8337 | /* Let the front end know that we are processing an explicit | |
8338 | instantiation. */ | |
8339 | begin_explicit_instantiation (); | |
8340 | /* [temp.explicit] says that we are supposed to ignore access | |
8341 | control while processing explicit instantiation directives. */ | |
78757caa | 8342 | push_deferring_access_checks (dk_no_check); |
a723baf1 MM |
8343 | /* Parse a decl-specifier-seq. */ |
8344 | decl_specifiers | |
8345 | = cp_parser_decl_specifier_seq (parser, | |
8346 | CP_PARSER_FLAGS_OPTIONAL, | |
8347 | &attributes, | |
8348 | &declares_class_or_enum); | |
8349 | /* If there was exactly one decl-specifier, and it declared a class, | |
8350 | and there's no declarator, then we have an explicit type | |
8351 | instantiation. */ | |
8352 | if (declares_class_or_enum && cp_parser_declares_only_class_p (parser)) | |
8353 | { | |
8354 | tree type; | |
8355 | ||
8356 | type = check_tag_decl (decl_specifiers); | |
b7fc8b57 KL |
8357 | /* Turn access control back on for names used during |
8358 | template instantiation. */ | |
8359 | pop_deferring_access_checks (); | |
a723baf1 MM |
8360 | if (type) |
8361 | do_type_instantiation (type, extension_specifier, /*complain=*/1); | |
8362 | } | |
8363 | else | |
8364 | { | |
8365 | tree declarator; | |
8366 | tree decl; | |
8367 | ||
8368 | /* Parse the declarator. */ | |
8369 | declarator | |
62b8a44e | 8370 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
8371 | /*ctor_dtor_or_conv_p=*/NULL, |
8372 | /*parenthesized_p=*/NULL); | |
560ad596 MM |
8373 | cp_parser_check_for_definition_in_return_type (declarator, |
8374 | declares_class_or_enum); | |
a723baf1 MM |
8375 | decl = grokdeclarator (declarator, decl_specifiers, |
8376 | NORMAL, 0, NULL); | |
b7fc8b57 KL |
8377 | /* Turn access control back on for names used during |
8378 | template instantiation. */ | |
8379 | pop_deferring_access_checks (); | |
a723baf1 MM |
8380 | /* Do the explicit instantiation. */ |
8381 | do_decl_instantiation (decl, extension_specifier); | |
8382 | } | |
8383 | /* We're done with the instantiation. */ | |
8384 | end_explicit_instantiation (); | |
a723baf1 | 8385 | |
e0860732 | 8386 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 MM |
8387 | } |
8388 | ||
8389 | /* Parse an explicit-specialization. | |
8390 | ||
8391 | explicit-specialization: | |
8392 | template < > declaration | |
8393 | ||
8394 | Although the standard says `declaration', what it really means is: | |
8395 | ||
8396 | explicit-specialization: | |
8397 | template <> decl-specifier [opt] init-declarator [opt] ; | |
8398 | template <> function-definition | |
8399 | template <> explicit-specialization | |
8400 | template <> template-declaration */ | |
8401 | ||
8402 | static void | |
94edc4ab | 8403 | cp_parser_explicit_specialization (cp_parser* parser) |
a723baf1 MM |
8404 | { |
8405 | /* Look for the `template' keyword. */ | |
8406 | cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"); | |
8407 | /* Look for the `<'. */ | |
8408 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
8409 | /* Look for the `>'. */ | |
8410 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
8411 | /* We have processed another parameter list. */ | |
8412 | ++parser->num_template_parameter_lists; | |
8413 | /* Let the front end know that we are beginning a specialization. */ | |
8414 | begin_specialization (); | |
8415 | ||
8416 | /* If the next keyword is `template', we need to figure out whether | |
8417 | or not we're looking a template-declaration. */ | |
8418 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
8419 | { | |
8420 | if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS | |
8421 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER) | |
8422 | cp_parser_template_declaration_after_export (parser, | |
8423 | /*member_p=*/false); | |
8424 | else | |
8425 | cp_parser_explicit_specialization (parser); | |
8426 | } | |
8427 | else | |
8428 | /* Parse the dependent declaration. */ | |
8429 | cp_parser_single_declaration (parser, | |
8430 | /*member_p=*/false, | |
8431 | /*friend_p=*/NULL); | |
8432 | ||
8433 | /* We're done with the specialization. */ | |
8434 | end_specialization (); | |
8435 | /* We're done with this parameter list. */ | |
8436 | --parser->num_template_parameter_lists; | |
8437 | } | |
8438 | ||
8439 | /* Parse a type-specifier. | |
8440 | ||
8441 | type-specifier: | |
8442 | simple-type-specifier | |
8443 | class-specifier | |
8444 | enum-specifier | |
8445 | elaborated-type-specifier | |
8446 | cv-qualifier | |
8447 | ||
8448 | GNU Extension: | |
8449 | ||
8450 | type-specifier: | |
8451 | __complex__ | |
8452 | ||
8453 | Returns a representation of the type-specifier. If the | |
8454 | type-specifier is a keyword (like `int' or `const', or | |
34cd5ae7 | 8455 | `__complex__') then the corresponding IDENTIFIER_NODE is returned. |
a723baf1 MM |
8456 | For a class-specifier, enum-specifier, or elaborated-type-specifier |
8457 | a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned. | |
8458 | ||
8459 | If IS_FRIEND is TRUE then this type-specifier is being declared a | |
8460 | `friend'. If IS_DECLARATION is TRUE, then this type-specifier is | |
8461 | appearing in a decl-specifier-seq. | |
8462 | ||
8463 | If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a | |
8464 | class-specifier, enum-specifier, or elaborated-type-specifier, then | |
83a00410 | 8465 | *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1 |
560ad596 MM |
8466 | if a type is declared; 2 if it is defined. Otherwise, it is set to |
8467 | zero. | |
a723baf1 MM |
8468 | |
8469 | If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a | |
8470 | cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it | |
8471 | is set to FALSE. */ | |
8472 | ||
8473 | static tree | |
94edc4ab NN |
8474 | cp_parser_type_specifier (cp_parser* parser, |
8475 | cp_parser_flags flags, | |
8476 | bool is_friend, | |
8477 | bool is_declaration, | |
560ad596 | 8478 | int* declares_class_or_enum, |
94edc4ab | 8479 | bool* is_cv_qualifier) |
a723baf1 MM |
8480 | { |
8481 | tree type_spec = NULL_TREE; | |
8482 | cp_token *token; | |
8483 | enum rid keyword; | |
8484 | ||
8485 | /* Assume this type-specifier does not declare a new type. */ | |
8486 | if (declares_class_or_enum) | |
8487 | *declares_class_or_enum = false; | |
8488 | /* And that it does not specify a cv-qualifier. */ | |
8489 | if (is_cv_qualifier) | |
8490 | *is_cv_qualifier = false; | |
8491 | /* Peek at the next token. */ | |
8492 | token = cp_lexer_peek_token (parser->lexer); | |
8493 | ||
8494 | /* If we're looking at a keyword, we can use that to guide the | |
8495 | production we choose. */ | |
8496 | keyword = token->keyword; | |
8497 | switch (keyword) | |
8498 | { | |
8499 | /* Any of these indicate either a class-specifier, or an | |
8500 | elaborated-type-specifier. */ | |
8501 | case RID_CLASS: | |
8502 | case RID_STRUCT: | |
8503 | case RID_UNION: | |
8504 | case RID_ENUM: | |
8505 | /* Parse tentatively so that we can back up if we don't find a | |
8506 | class-specifier or enum-specifier. */ | |
8507 | cp_parser_parse_tentatively (parser); | |
8508 | /* Look for the class-specifier or enum-specifier. */ | |
8509 | if (keyword == RID_ENUM) | |
8510 | type_spec = cp_parser_enum_specifier (parser); | |
8511 | else | |
8512 | type_spec = cp_parser_class_specifier (parser); | |
8513 | ||
8514 | /* If that worked, we're done. */ | |
8515 | if (cp_parser_parse_definitely (parser)) | |
8516 | { | |
8517 | if (declares_class_or_enum) | |
560ad596 | 8518 | *declares_class_or_enum = 2; |
a723baf1 MM |
8519 | return type_spec; |
8520 | } | |
8521 | ||
8522 | /* Fall through. */ | |
8523 | ||
8524 | case RID_TYPENAME: | |
8525 | /* Look for an elaborated-type-specifier. */ | |
8526 | type_spec = cp_parser_elaborated_type_specifier (parser, | |
8527 | is_friend, | |
8528 | is_declaration); | |
8529 | /* We're declaring a class or enum -- unless we're using | |
8530 | `typename'. */ | |
8531 | if (declares_class_or_enum && keyword != RID_TYPENAME) | |
560ad596 | 8532 | *declares_class_or_enum = 1; |
a723baf1 MM |
8533 | return type_spec; |
8534 | ||
8535 | case RID_CONST: | |
8536 | case RID_VOLATILE: | |
8537 | case RID_RESTRICT: | |
8538 | type_spec = cp_parser_cv_qualifier_opt (parser); | |
8539 | /* Even though we call a routine that looks for an optional | |
8540 | qualifier, we know that there should be one. */ | |
8541 | my_friendly_assert (type_spec != NULL, 20000328); | |
8542 | /* This type-specifier was a cv-qualified. */ | |
8543 | if (is_cv_qualifier) | |
8544 | *is_cv_qualifier = true; | |
8545 | ||
8546 | return type_spec; | |
8547 | ||
8548 | case RID_COMPLEX: | |
8549 | /* The `__complex__' keyword is a GNU extension. */ | |
8550 | return cp_lexer_consume_token (parser->lexer)->value; | |
8551 | ||
8552 | default: | |
8553 | break; | |
8554 | } | |
8555 | ||
8556 | /* If we do not already have a type-specifier, assume we are looking | |
8557 | at a simple-type-specifier. */ | |
4b0d3cbe MM |
8558 | type_spec = cp_parser_simple_type_specifier (parser, flags, |
8559 | /*identifier_p=*/true); | |
a723baf1 MM |
8560 | |
8561 | /* If we didn't find a type-specifier, and a type-specifier was not | |
8562 | optional in this context, issue an error message. */ | |
8563 | if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
8564 | { | |
8565 | cp_parser_error (parser, "expected type specifier"); | |
8566 | return error_mark_node; | |
8567 | } | |
8568 | ||
8569 | return type_spec; | |
8570 | } | |
8571 | ||
8572 | /* Parse a simple-type-specifier. | |
8573 | ||
8574 | simple-type-specifier: | |
8575 | :: [opt] nested-name-specifier [opt] type-name | |
8576 | :: [opt] nested-name-specifier template template-id | |
8577 | char | |
8578 | wchar_t | |
8579 | bool | |
8580 | short | |
8581 | int | |
8582 | long | |
8583 | signed | |
8584 | unsigned | |
8585 | float | |
8586 | double | |
8587 | void | |
8588 | ||
8589 | GNU Extension: | |
8590 | ||
8591 | simple-type-specifier: | |
8592 | __typeof__ unary-expression | |
8593 | __typeof__ ( type-id ) | |
8594 | ||
8595 | For the various keywords, the value returned is simply the | |
4b0d3cbe MM |
8596 | TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true. |
8597 | For the first two productions, and if IDENTIFIER_P is false, the | |
8598 | value returned is the indicated TYPE_DECL. */ | |
a723baf1 MM |
8599 | |
8600 | static tree | |
4b0d3cbe MM |
8601 | cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags, |
8602 | bool identifier_p) | |
a723baf1 MM |
8603 | { |
8604 | tree type = NULL_TREE; | |
8605 | cp_token *token; | |
8606 | ||
8607 | /* Peek at the next token. */ | |
8608 | token = cp_lexer_peek_token (parser->lexer); | |
8609 | ||
8610 | /* If we're looking at a keyword, things are easy. */ | |
8611 | switch (token->keyword) | |
8612 | { | |
8613 | case RID_CHAR: | |
4b0d3cbe MM |
8614 | type = char_type_node; |
8615 | break; | |
a723baf1 | 8616 | case RID_WCHAR: |
4b0d3cbe MM |
8617 | type = wchar_type_node; |
8618 | break; | |
a723baf1 | 8619 | case RID_BOOL: |
4b0d3cbe MM |
8620 | type = boolean_type_node; |
8621 | break; | |
a723baf1 | 8622 | case RID_SHORT: |
4b0d3cbe MM |
8623 | type = short_integer_type_node; |
8624 | break; | |
a723baf1 | 8625 | case RID_INT: |
4b0d3cbe MM |
8626 | type = integer_type_node; |
8627 | break; | |
a723baf1 | 8628 | case RID_LONG: |
4b0d3cbe MM |
8629 | type = long_integer_type_node; |
8630 | break; | |
a723baf1 | 8631 | case RID_SIGNED: |
4b0d3cbe MM |
8632 | type = integer_type_node; |
8633 | break; | |
a723baf1 | 8634 | case RID_UNSIGNED: |
4b0d3cbe MM |
8635 | type = unsigned_type_node; |
8636 | break; | |
a723baf1 | 8637 | case RID_FLOAT: |
4b0d3cbe MM |
8638 | type = float_type_node; |
8639 | break; | |
a723baf1 | 8640 | case RID_DOUBLE: |
4b0d3cbe MM |
8641 | type = double_type_node; |
8642 | break; | |
a723baf1 | 8643 | case RID_VOID: |
4b0d3cbe MM |
8644 | type = void_type_node; |
8645 | break; | |
a723baf1 MM |
8646 | |
8647 | case RID_TYPEOF: | |
8648 | { | |
8649 | tree operand; | |
8650 | ||
8651 | /* Consume the `typeof' token. */ | |
8652 | cp_lexer_consume_token (parser->lexer); | |
04c06002 | 8653 | /* Parse the operand to `typeof'. */ |
a723baf1 MM |
8654 | operand = cp_parser_sizeof_operand (parser, RID_TYPEOF); |
8655 | /* If it is not already a TYPE, take its type. */ | |
8656 | if (!TYPE_P (operand)) | |
8657 | operand = finish_typeof (operand); | |
8658 | ||
8659 | return operand; | |
8660 | } | |
8661 | ||
8662 | default: | |
8663 | break; | |
8664 | } | |
8665 | ||
4b0d3cbe MM |
8666 | /* If the type-specifier was for a built-in type, we're done. */ |
8667 | if (type) | |
8668 | { | |
8669 | tree id; | |
8670 | ||
8671 | /* Consume the token. */ | |
8672 | id = cp_lexer_consume_token (parser->lexer)->value; | |
8673 | return identifier_p ? id : TYPE_NAME (type); | |
8674 | } | |
8675 | ||
a723baf1 MM |
8676 | /* The type-specifier must be a user-defined type. */ |
8677 | if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) | |
8678 | { | |
8679 | /* Don't gobble tokens or issue error messages if this is an | |
8680 | optional type-specifier. */ | |
8681 | if (flags & CP_PARSER_FLAGS_OPTIONAL) | |
8682 | cp_parser_parse_tentatively (parser); | |
8683 | ||
8684 | /* Look for the optional `::' operator. */ | |
8685 | cp_parser_global_scope_opt (parser, | |
8686 | /*current_scope_valid_p=*/false); | |
8687 | /* Look for the nested-name specifier. */ | |
8688 | cp_parser_nested_name_specifier_opt (parser, | |
8689 | /*typename_keyword_p=*/false, | |
8690 | /*check_dependency_p=*/true, | |
a668c6ad MM |
8691 | /*type_p=*/false, |
8692 | /*is_declaration=*/false); | |
a723baf1 MM |
8693 | /* If we have seen a nested-name-specifier, and the next token |
8694 | is `template', then we are using the template-id production. */ | |
8695 | if (parser->scope | |
8696 | && cp_parser_optional_template_keyword (parser)) | |
8697 | { | |
8698 | /* Look for the template-id. */ | |
8699 | type = cp_parser_template_id (parser, | |
8700 | /*template_keyword_p=*/true, | |
a668c6ad MM |
8701 | /*check_dependency_p=*/true, |
8702 | /*is_declaration=*/false); | |
a723baf1 MM |
8703 | /* If the template-id did not name a type, we are out of |
8704 | luck. */ | |
8705 | if (TREE_CODE (type) != TYPE_DECL) | |
8706 | { | |
8707 | cp_parser_error (parser, "expected template-id for type"); | |
8708 | type = NULL_TREE; | |
8709 | } | |
8710 | } | |
8711 | /* Otherwise, look for a type-name. */ | |
8712 | else | |
4bb8ca28 | 8713 | type = cp_parser_type_name (parser); |
a723baf1 MM |
8714 | /* If it didn't work out, we don't have a TYPE. */ |
8715 | if ((flags & CP_PARSER_FLAGS_OPTIONAL) | |
8716 | && !cp_parser_parse_definitely (parser)) | |
8717 | type = NULL_TREE; | |
8718 | } | |
8719 | ||
8720 | /* If we didn't get a type-name, issue an error message. */ | |
8721 | if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
8722 | { | |
8723 | cp_parser_error (parser, "expected type-name"); | |
8724 | return error_mark_node; | |
8725 | } | |
8726 | ||
a668c6ad MM |
8727 | /* There is no valid C++ program where a non-template type is |
8728 | followed by a "<". That usually indicates that the user thought | |
8729 | that the type was a template. */ | |
4bb8ca28 | 8730 | if (type && type != error_mark_node) |
ee43dab5 | 8731 | cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type)); |
ec75414f | 8732 | |
a723baf1 MM |
8733 | return type; |
8734 | } | |
8735 | ||
8736 | /* Parse a type-name. | |
8737 | ||
8738 | type-name: | |
8739 | class-name | |
8740 | enum-name | |
8741 | typedef-name | |
8742 | ||
8743 | enum-name: | |
8744 | identifier | |
8745 | ||
8746 | typedef-name: | |
8747 | identifier | |
8748 | ||
8749 | Returns a TYPE_DECL for the the type. */ | |
8750 | ||
8751 | static tree | |
94edc4ab | 8752 | cp_parser_type_name (cp_parser* parser) |
a723baf1 MM |
8753 | { |
8754 | tree type_decl; | |
8755 | tree identifier; | |
8756 | ||
8757 | /* We can't know yet whether it is a class-name or not. */ | |
8758 | cp_parser_parse_tentatively (parser); | |
8759 | /* Try a class-name. */ | |
8760 | type_decl = cp_parser_class_name (parser, | |
8761 | /*typename_keyword_p=*/false, | |
8762 | /*template_keyword_p=*/false, | |
8763 | /*type_p=*/false, | |
a723baf1 | 8764 | /*check_dependency_p=*/true, |
a668c6ad MM |
8765 | /*class_head_p=*/false, |
8766 | /*is_declaration=*/false); | |
a723baf1 MM |
8767 | /* If it's not a class-name, keep looking. */ |
8768 | if (!cp_parser_parse_definitely (parser)) | |
8769 | { | |
8770 | /* It must be a typedef-name or an enum-name. */ | |
8771 | identifier = cp_parser_identifier (parser); | |
8772 | if (identifier == error_mark_node) | |
8773 | return error_mark_node; | |
8774 | ||
8775 | /* Look up the type-name. */ | |
8776 | type_decl = cp_parser_lookup_name_simple (parser, identifier); | |
8777 | /* Issue an error if we did not find a type-name. */ | |
8778 | if (TREE_CODE (type_decl) != TYPE_DECL) | |
8779 | { | |
4bb8ca28 MM |
8780 | if (!cp_parser_simulate_error (parser)) |
8781 | cp_parser_name_lookup_error (parser, identifier, type_decl, | |
8782 | "is not a type"); | |
a723baf1 MM |
8783 | type_decl = error_mark_node; |
8784 | } | |
8785 | /* Remember that the name was used in the definition of the | |
8786 | current class so that we can check later to see if the | |
8787 | meaning would have been different after the class was | |
8788 | entirely defined. */ | |
8789 | else if (type_decl != error_mark_node | |
8790 | && !parser->scope) | |
8791 | maybe_note_name_used_in_class (identifier, type_decl); | |
8792 | } | |
8793 | ||
8794 | return type_decl; | |
8795 | } | |
8796 | ||
8797 | ||
8798 | /* Parse an elaborated-type-specifier. Note that the grammar given | |
8799 | here incorporates the resolution to DR68. | |
8800 | ||
8801 | elaborated-type-specifier: | |
8802 | class-key :: [opt] nested-name-specifier [opt] identifier | |
8803 | class-key :: [opt] nested-name-specifier [opt] template [opt] template-id | |
8804 | enum :: [opt] nested-name-specifier [opt] identifier | |
8805 | typename :: [opt] nested-name-specifier identifier | |
8806 | typename :: [opt] nested-name-specifier template [opt] | |
8807 | template-id | |
8808 | ||
360d1b99 MM |
8809 | GNU extension: |
8810 | ||
8811 | elaborated-type-specifier: | |
8812 | class-key attributes :: [opt] nested-name-specifier [opt] identifier | |
8813 | class-key attributes :: [opt] nested-name-specifier [opt] | |
8814 | template [opt] template-id | |
8815 | enum attributes :: [opt] nested-name-specifier [opt] identifier | |
8816 | ||
a723baf1 MM |
8817 | If IS_FRIEND is TRUE, then this elaborated-type-specifier is being |
8818 | declared `friend'. If IS_DECLARATION is TRUE, then this | |
8819 | elaborated-type-specifier appears in a decl-specifiers-seq, i.e., | |
8820 | something is being declared. | |
8821 | ||
8822 | Returns the TYPE specified. */ | |
8823 | ||
8824 | static tree | |
94edc4ab NN |
8825 | cp_parser_elaborated_type_specifier (cp_parser* parser, |
8826 | bool is_friend, | |
8827 | bool is_declaration) | |
a723baf1 MM |
8828 | { |
8829 | enum tag_types tag_type; | |
8830 | tree identifier; | |
8831 | tree type = NULL_TREE; | |
360d1b99 | 8832 | tree attributes = NULL_TREE; |
a723baf1 MM |
8833 | |
8834 | /* See if we're looking at the `enum' keyword. */ | |
8835 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM)) | |
8836 | { | |
8837 | /* Consume the `enum' token. */ | |
8838 | cp_lexer_consume_token (parser->lexer); | |
8839 | /* Remember that it's an enumeration type. */ | |
8840 | tag_type = enum_type; | |
360d1b99 MM |
8841 | /* Parse the attributes. */ |
8842 | attributes = cp_parser_attributes_opt (parser); | |
a723baf1 MM |
8843 | } |
8844 | /* Or, it might be `typename'. */ | |
8845 | else if (cp_lexer_next_token_is_keyword (parser->lexer, | |
8846 | RID_TYPENAME)) | |
8847 | { | |
8848 | /* Consume the `typename' token. */ | |
8849 | cp_lexer_consume_token (parser->lexer); | |
8850 | /* Remember that it's a `typename' type. */ | |
8851 | tag_type = typename_type; | |
8852 | /* The `typename' keyword is only allowed in templates. */ | |
8853 | if (!processing_template_decl) | |
8854 | pedwarn ("using `typename' outside of template"); | |
8855 | } | |
8856 | /* Otherwise it must be a class-key. */ | |
8857 | else | |
8858 | { | |
8859 | tag_type = cp_parser_class_key (parser); | |
8860 | if (tag_type == none_type) | |
8861 | return error_mark_node; | |
360d1b99 MM |
8862 | /* Parse the attributes. */ |
8863 | attributes = cp_parser_attributes_opt (parser); | |
a723baf1 MM |
8864 | } |
8865 | ||
8866 | /* Look for the `::' operator. */ | |
8867 | cp_parser_global_scope_opt (parser, | |
8868 | /*current_scope_valid_p=*/false); | |
8869 | /* Look for the nested-name-specifier. */ | |
8870 | if (tag_type == typename_type) | |
8fa1ad0e MM |
8871 | { |
8872 | if (cp_parser_nested_name_specifier (parser, | |
8873 | /*typename_keyword_p=*/true, | |
8874 | /*check_dependency_p=*/true, | |
a668c6ad MM |
8875 | /*type_p=*/true, |
8876 | is_declaration) | |
8fa1ad0e MM |
8877 | == error_mark_node) |
8878 | return error_mark_node; | |
8879 | } | |
a723baf1 MM |
8880 | else |
8881 | /* Even though `typename' is not present, the proposed resolution | |
8882 | to Core Issue 180 says that in `class A<T>::B', `B' should be | |
8883 | considered a type-name, even if `A<T>' is dependent. */ | |
8884 | cp_parser_nested_name_specifier_opt (parser, | |
8885 | /*typename_keyword_p=*/true, | |
8886 | /*check_dependency_p=*/true, | |
a668c6ad MM |
8887 | /*type_p=*/true, |
8888 | is_declaration); | |
a723baf1 MM |
8889 | /* For everything but enumeration types, consider a template-id. */ |
8890 | if (tag_type != enum_type) | |
8891 | { | |
8892 | bool template_p = false; | |
8893 | tree decl; | |
8894 | ||
8895 | /* Allow the `template' keyword. */ | |
8896 | template_p = cp_parser_optional_template_keyword (parser); | |
8897 | /* If we didn't see `template', we don't know if there's a | |
8898 | template-id or not. */ | |
8899 | if (!template_p) | |
8900 | cp_parser_parse_tentatively (parser); | |
8901 | /* Parse the template-id. */ | |
8902 | decl = cp_parser_template_id (parser, template_p, | |
a668c6ad MM |
8903 | /*check_dependency_p=*/true, |
8904 | is_declaration); | |
a723baf1 MM |
8905 | /* If we didn't find a template-id, look for an ordinary |
8906 | identifier. */ | |
8907 | if (!template_p && !cp_parser_parse_definitely (parser)) | |
8908 | ; | |
8909 | /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is | |
8910 | in effect, then we must assume that, upon instantiation, the | |
8911 | template will correspond to a class. */ | |
8912 | else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR | |
8913 | && tag_type == typename_type) | |
8914 | type = make_typename_type (parser->scope, decl, | |
8915 | /*complain=*/1); | |
8916 | else | |
8917 | type = TREE_TYPE (decl); | |
8918 | } | |
8919 | ||
8920 | /* For an enumeration type, consider only a plain identifier. */ | |
8921 | if (!type) | |
8922 | { | |
8923 | identifier = cp_parser_identifier (parser); | |
8924 | ||
8925 | if (identifier == error_mark_node) | |
eb5abb39 NS |
8926 | { |
8927 | parser->scope = NULL_TREE; | |
8928 | return error_mark_node; | |
8929 | } | |
a723baf1 MM |
8930 | |
8931 | /* For a `typename', we needn't call xref_tag. */ | |
8932 | if (tag_type == typename_type) | |
8933 | return make_typename_type (parser->scope, identifier, | |
8934 | /*complain=*/1); | |
8935 | /* Look up a qualified name in the usual way. */ | |
8936 | if (parser->scope) | |
8937 | { | |
8938 | tree decl; | |
8939 | ||
8940 | /* In an elaborated-type-specifier, names are assumed to name | |
8941 | types, so we set IS_TYPE to TRUE when calling | |
8942 | cp_parser_lookup_name. */ | |
8943 | decl = cp_parser_lookup_name (parser, identifier, | |
a723baf1 | 8944 | /*is_type=*/true, |
b0bc6e8e | 8945 | /*is_template=*/false, |
eea9800f | 8946 | /*is_namespace=*/false, |
a723baf1 | 8947 | /*check_dependency=*/true); |
710b73e6 KL |
8948 | |
8949 | /* If we are parsing friend declaration, DECL may be a | |
8950 | TEMPLATE_DECL tree node here. However, we need to check | |
8951 | whether this TEMPLATE_DECL results in valid code. Consider | |
8952 | the following example: | |
8953 | ||
8954 | namespace N { | |
8955 | template <class T> class C {}; | |
8956 | } | |
8957 | class X { | |
8958 | template <class T> friend class N::C; // #1, valid code | |
8959 | }; | |
8960 | template <class T> class Y { | |
8961 | friend class N::C; // #2, invalid code | |
8962 | }; | |
8963 | ||
8964 | For both case #1 and #2, we arrive at a TEMPLATE_DECL after | |
8965 | name lookup of `N::C'. We see that friend declaration must | |
8966 | be template for the code to be valid. Note that | |
8967 | processing_template_decl does not work here since it is | |
8968 | always 1 for the above two cases. */ | |
8969 | ||
a723baf1 | 8970 | decl = (cp_parser_maybe_treat_template_as_class |
710b73e6 KL |
8971 | (decl, /*tag_name_p=*/is_friend |
8972 | && parser->num_template_parameter_lists)); | |
a723baf1 MM |
8973 | |
8974 | if (TREE_CODE (decl) != TYPE_DECL) | |
8975 | { | |
8976 | error ("expected type-name"); | |
8977 | return error_mark_node; | |
8978 | } | |
560ad596 MM |
8979 | |
8980 | if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE) | |
8981 | check_elaborated_type_specifier | |
4b0d3cbe | 8982 | (tag_type, decl, |
560ad596 MM |
8983 | (parser->num_template_parameter_lists |
8984 | || DECL_SELF_REFERENCE_P (decl))); | |
a723baf1 MM |
8985 | |
8986 | type = TREE_TYPE (decl); | |
8987 | } | |
8988 | else | |
8989 | { | |
8990 | /* An elaborated-type-specifier sometimes introduces a new type and | |
8991 | sometimes names an existing type. Normally, the rule is that it | |
8992 | introduces a new type only if there is not an existing type of | |
8993 | the same name already in scope. For example, given: | |
8994 | ||
8995 | struct S {}; | |
8996 | void f() { struct S s; } | |
8997 | ||
8998 | the `struct S' in the body of `f' is the same `struct S' as in | |
8999 | the global scope; the existing definition is used. However, if | |
9000 | there were no global declaration, this would introduce a new | |
9001 | local class named `S'. | |
9002 | ||
9003 | An exception to this rule applies to the following code: | |
9004 | ||
9005 | namespace N { struct S; } | |
9006 | ||
9007 | Here, the elaborated-type-specifier names a new type | |
9008 | unconditionally; even if there is already an `S' in the | |
9009 | containing scope this declaration names a new type. | |
9010 | This exception only applies if the elaborated-type-specifier | |
9011 | forms the complete declaration: | |
9012 | ||
9013 | [class.name] | |
9014 | ||
9015 | A declaration consisting solely of `class-key identifier ;' is | |
9016 | either a redeclaration of the name in the current scope or a | |
9017 | forward declaration of the identifier as a class name. It | |
9018 | introduces the name into the current scope. | |
9019 | ||
9020 | We are in this situation precisely when the next token is a `;'. | |
9021 | ||
9022 | An exception to the exception is that a `friend' declaration does | |
9023 | *not* name a new type; i.e., given: | |
9024 | ||
9025 | struct S { friend struct T; }; | |
9026 | ||
9027 | `T' is not a new type in the scope of `S'. | |
9028 | ||
9029 | Also, `new struct S' or `sizeof (struct S)' never results in the | |
9030 | definition of a new type; a new type can only be declared in a | |
9bcb9aae | 9031 | declaration context. */ |
a723baf1 MM |
9032 | |
9033 | type = xref_tag (tag_type, identifier, | |
360d1b99 | 9034 | attributes, |
a723baf1 MM |
9035 | (is_friend |
9036 | || !is_declaration | |
9037 | || cp_lexer_next_token_is_not (parser->lexer, | |
cbd63935 KL |
9038 | CPP_SEMICOLON)), |
9039 | parser->num_template_parameter_lists); | |
a723baf1 MM |
9040 | } |
9041 | } | |
9042 | if (tag_type != enum_type) | |
9043 | cp_parser_check_class_key (tag_type, type); | |
ee43dab5 MM |
9044 | |
9045 | /* A "<" cannot follow an elaborated type specifier. If that | |
9046 | happens, the user was probably trying to form a template-id. */ | |
9047 | cp_parser_check_for_invalid_template_id (parser, type); | |
9048 | ||
a723baf1 MM |
9049 | return type; |
9050 | } | |
9051 | ||
9052 | /* Parse an enum-specifier. | |
9053 | ||
9054 | enum-specifier: | |
9055 | enum identifier [opt] { enumerator-list [opt] } | |
9056 | ||
9057 | Returns an ENUM_TYPE representing the enumeration. */ | |
9058 | ||
9059 | static tree | |
94edc4ab | 9060 | cp_parser_enum_specifier (cp_parser* parser) |
a723baf1 MM |
9061 | { |
9062 | cp_token *token; | |
9063 | tree identifier = NULL_TREE; | |
9064 | tree type; | |
9065 | ||
9066 | /* Look for the `enum' keyword. */ | |
9067 | if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'")) | |
9068 | return error_mark_node; | |
9069 | /* Peek at the next token. */ | |
9070 | token = cp_lexer_peek_token (parser->lexer); | |
9071 | ||
9072 | /* See if it is an identifier. */ | |
9073 | if (token->type == CPP_NAME) | |
9074 | identifier = cp_parser_identifier (parser); | |
9075 | ||
9076 | /* Look for the `{'. */ | |
9077 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
9078 | return error_mark_node; | |
9079 | ||
9080 | /* At this point, we're going ahead with the enum-specifier, even | |
9081 | if some other problem occurs. */ | |
9082 | cp_parser_commit_to_tentative_parse (parser); | |
9083 | ||
9084 | /* Issue an error message if type-definitions are forbidden here. */ | |
9085 | cp_parser_check_type_definition (parser); | |
9086 | ||
9087 | /* Create the new type. */ | |
9088 | type = start_enum (identifier ? identifier : make_anon_name ()); | |
9089 | ||
9090 | /* Peek at the next token. */ | |
9091 | token = cp_lexer_peek_token (parser->lexer); | |
9092 | /* If it's not a `}', then there are some enumerators. */ | |
9093 | if (token->type != CPP_CLOSE_BRACE) | |
9094 | cp_parser_enumerator_list (parser, type); | |
9095 | /* Look for the `}'. */ | |
9096 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
9097 | ||
9098 | /* Finish up the enumeration. */ | |
9099 | finish_enum (type); | |
9100 | ||
9101 | return type; | |
9102 | } | |
9103 | ||
9104 | /* Parse an enumerator-list. The enumerators all have the indicated | |
9105 | TYPE. | |
9106 | ||
9107 | enumerator-list: | |
9108 | enumerator-definition | |
9109 | enumerator-list , enumerator-definition */ | |
9110 | ||
9111 | static void | |
94edc4ab | 9112 | cp_parser_enumerator_list (cp_parser* parser, tree type) |
a723baf1 MM |
9113 | { |
9114 | while (true) | |
9115 | { | |
9116 | cp_token *token; | |
9117 | ||
9118 | /* Parse an enumerator-definition. */ | |
9119 | cp_parser_enumerator_definition (parser, type); | |
9120 | /* Peek at the next token. */ | |
9121 | token = cp_lexer_peek_token (parser->lexer); | |
9122 | /* If it's not a `,', then we've reached the end of the | |
9123 | list. */ | |
9124 | if (token->type != CPP_COMMA) | |
9125 | break; | |
9126 | /* Otherwise, consume the `,' and keep going. */ | |
9127 | cp_lexer_consume_token (parser->lexer); | |
9128 | /* If the next token is a `}', there is a trailing comma. */ | |
9129 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
9130 | { | |
9131 | if (pedantic && !in_system_header) | |
9132 | pedwarn ("comma at end of enumerator list"); | |
9133 | break; | |
9134 | } | |
9135 | } | |
9136 | } | |
9137 | ||
9138 | /* Parse an enumerator-definition. The enumerator has the indicated | |
9139 | TYPE. | |
9140 | ||
9141 | enumerator-definition: | |
9142 | enumerator | |
9143 | enumerator = constant-expression | |
9144 | ||
9145 | enumerator: | |
9146 | identifier */ | |
9147 | ||
9148 | static void | |
94edc4ab | 9149 | cp_parser_enumerator_definition (cp_parser* parser, tree type) |
a723baf1 MM |
9150 | { |
9151 | cp_token *token; | |
9152 | tree identifier; | |
9153 | tree value; | |
9154 | ||
9155 | /* Look for the identifier. */ | |
9156 | identifier = cp_parser_identifier (parser); | |
9157 | if (identifier == error_mark_node) | |
9158 | return; | |
9159 | ||
9160 | /* Peek at the next token. */ | |
9161 | token = cp_lexer_peek_token (parser->lexer); | |
9162 | /* If it's an `=', then there's an explicit value. */ | |
9163 | if (token->type == CPP_EQ) | |
9164 | { | |
9165 | /* Consume the `=' token. */ | |
9166 | cp_lexer_consume_token (parser->lexer); | |
9167 | /* Parse the value. */ | |
14d22dd6 | 9168 | value = cp_parser_constant_expression (parser, |
d17811fd | 9169 | /*allow_non_constant_p=*/false, |
14d22dd6 | 9170 | NULL); |
a723baf1 MM |
9171 | } |
9172 | else | |
9173 | value = NULL_TREE; | |
9174 | ||
9175 | /* Create the enumerator. */ | |
9176 | build_enumerator (identifier, value, type); | |
9177 | } | |
9178 | ||
9179 | /* Parse a namespace-name. | |
9180 | ||
9181 | namespace-name: | |
9182 | original-namespace-name | |
9183 | namespace-alias | |
9184 | ||
9185 | Returns the NAMESPACE_DECL for the namespace. */ | |
9186 | ||
9187 | static tree | |
94edc4ab | 9188 | cp_parser_namespace_name (cp_parser* parser) |
a723baf1 MM |
9189 | { |
9190 | tree identifier; | |
9191 | tree namespace_decl; | |
9192 | ||
9193 | /* Get the name of the namespace. */ | |
9194 | identifier = cp_parser_identifier (parser); | |
9195 | if (identifier == error_mark_node) | |
9196 | return error_mark_node; | |
9197 | ||
eea9800f MM |
9198 | /* Look up the identifier in the currently active scope. Look only |
9199 | for namespaces, due to: | |
9200 | ||
9201 | [basic.lookup.udir] | |
9202 | ||
9203 | When looking up a namespace-name in a using-directive or alias | |
9204 | definition, only namespace names are considered. | |
9205 | ||
9206 | And: | |
9207 | ||
9208 | [basic.lookup.qual] | |
9209 | ||
9210 | During the lookup of a name preceding the :: scope resolution | |
9211 | operator, object, function, and enumerator names are ignored. | |
9212 | ||
9213 | (Note that cp_parser_class_or_namespace_name only calls this | |
9214 | function if the token after the name is the scope resolution | |
9215 | operator.) */ | |
9216 | namespace_decl = cp_parser_lookup_name (parser, identifier, | |
eea9800f | 9217 | /*is_type=*/false, |
b0bc6e8e | 9218 | /*is_template=*/false, |
eea9800f MM |
9219 | /*is_namespace=*/true, |
9220 | /*check_dependency=*/true); | |
a723baf1 MM |
9221 | /* If it's not a namespace, issue an error. */ |
9222 | if (namespace_decl == error_mark_node | |
9223 | || TREE_CODE (namespace_decl) != NAMESPACE_DECL) | |
9224 | { | |
9225 | cp_parser_error (parser, "expected namespace-name"); | |
9226 | namespace_decl = error_mark_node; | |
9227 | } | |
9228 | ||
9229 | return namespace_decl; | |
9230 | } | |
9231 | ||
9232 | /* Parse a namespace-definition. | |
9233 | ||
9234 | namespace-definition: | |
9235 | named-namespace-definition | |
9236 | unnamed-namespace-definition | |
9237 | ||
9238 | named-namespace-definition: | |
9239 | original-namespace-definition | |
9240 | extension-namespace-definition | |
9241 | ||
9242 | original-namespace-definition: | |
9243 | namespace identifier { namespace-body } | |
9244 | ||
9245 | extension-namespace-definition: | |
9246 | namespace original-namespace-name { namespace-body } | |
9247 | ||
9248 | unnamed-namespace-definition: | |
9249 | namespace { namespace-body } */ | |
9250 | ||
9251 | static void | |
94edc4ab | 9252 | cp_parser_namespace_definition (cp_parser* parser) |
a723baf1 MM |
9253 | { |
9254 | tree identifier; | |
9255 | ||
9256 | /* Look for the `namespace' keyword. */ | |
9257 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
9258 | ||
9259 | /* Get the name of the namespace. We do not attempt to distinguish | |
9260 | between an original-namespace-definition and an | |
9261 | extension-namespace-definition at this point. The semantic | |
9262 | analysis routines are responsible for that. */ | |
9263 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
9264 | identifier = cp_parser_identifier (parser); | |
9265 | else | |
9266 | identifier = NULL_TREE; | |
9267 | ||
9268 | /* Look for the `{' to start the namespace. */ | |
9269 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
9270 | /* Start the namespace. */ | |
9271 | push_namespace (identifier); | |
9272 | /* Parse the body of the namespace. */ | |
9273 | cp_parser_namespace_body (parser); | |
9274 | /* Finish the namespace. */ | |
9275 | pop_namespace (); | |
9276 | /* Look for the final `}'. */ | |
9277 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
9278 | } | |
9279 | ||
9280 | /* Parse a namespace-body. | |
9281 | ||
9282 | namespace-body: | |
9283 | declaration-seq [opt] */ | |
9284 | ||
9285 | static void | |
94edc4ab | 9286 | cp_parser_namespace_body (cp_parser* parser) |
a723baf1 MM |
9287 | { |
9288 | cp_parser_declaration_seq_opt (parser); | |
9289 | } | |
9290 | ||
9291 | /* Parse a namespace-alias-definition. | |
9292 | ||
9293 | namespace-alias-definition: | |
9294 | namespace identifier = qualified-namespace-specifier ; */ | |
9295 | ||
9296 | static void | |
94edc4ab | 9297 | cp_parser_namespace_alias_definition (cp_parser* parser) |
a723baf1 MM |
9298 | { |
9299 | tree identifier; | |
9300 | tree namespace_specifier; | |
9301 | ||
9302 | /* Look for the `namespace' keyword. */ | |
9303 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
9304 | /* Look for the identifier. */ | |
9305 | identifier = cp_parser_identifier (parser); | |
9306 | if (identifier == error_mark_node) | |
9307 | return; | |
9308 | /* Look for the `=' token. */ | |
9309 | cp_parser_require (parser, CPP_EQ, "`='"); | |
9310 | /* Look for the qualified-namespace-specifier. */ | |
9311 | namespace_specifier | |
9312 | = cp_parser_qualified_namespace_specifier (parser); | |
9313 | /* Look for the `;' token. */ | |
9314 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
9315 | ||
9316 | /* Register the alias in the symbol table. */ | |
9317 | do_namespace_alias (identifier, namespace_specifier); | |
9318 | } | |
9319 | ||
9320 | /* Parse a qualified-namespace-specifier. | |
9321 | ||
9322 | qualified-namespace-specifier: | |
9323 | :: [opt] nested-name-specifier [opt] namespace-name | |
9324 | ||
9325 | Returns a NAMESPACE_DECL corresponding to the specified | |
9326 | namespace. */ | |
9327 | ||
9328 | static tree | |
94edc4ab | 9329 | cp_parser_qualified_namespace_specifier (cp_parser* parser) |
a723baf1 MM |
9330 | { |
9331 | /* Look for the optional `::'. */ | |
9332 | cp_parser_global_scope_opt (parser, | |
9333 | /*current_scope_valid_p=*/false); | |
9334 | ||
9335 | /* Look for the optional nested-name-specifier. */ | |
9336 | cp_parser_nested_name_specifier_opt (parser, | |
9337 | /*typename_keyword_p=*/false, | |
9338 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9339 | /*type_p=*/false, |
9340 | /*is_declaration=*/true); | |
a723baf1 MM |
9341 | |
9342 | return cp_parser_namespace_name (parser); | |
9343 | } | |
9344 | ||
9345 | /* Parse a using-declaration. | |
9346 | ||
9347 | using-declaration: | |
9348 | using typename [opt] :: [opt] nested-name-specifier unqualified-id ; | |
9349 | using :: unqualified-id ; */ | |
9350 | ||
9351 | static void | |
94edc4ab | 9352 | cp_parser_using_declaration (cp_parser* parser) |
a723baf1 MM |
9353 | { |
9354 | cp_token *token; | |
9355 | bool typename_p = false; | |
9356 | bool global_scope_p; | |
9357 | tree decl; | |
9358 | tree identifier; | |
9359 | tree scope; | |
9360 | ||
9361 | /* Look for the `using' keyword. */ | |
9362 | cp_parser_require_keyword (parser, RID_USING, "`using'"); | |
9363 | ||
9364 | /* Peek at the next token. */ | |
9365 | token = cp_lexer_peek_token (parser->lexer); | |
9366 | /* See if it's `typename'. */ | |
9367 | if (token->keyword == RID_TYPENAME) | |
9368 | { | |
9369 | /* Remember that we've seen it. */ | |
9370 | typename_p = true; | |
9371 | /* Consume the `typename' token. */ | |
9372 | cp_lexer_consume_token (parser->lexer); | |
9373 | } | |
9374 | ||
9375 | /* Look for the optional global scope qualification. */ | |
9376 | global_scope_p | |
9377 | = (cp_parser_global_scope_opt (parser, | |
9378 | /*current_scope_valid_p=*/false) | |
9379 | != NULL_TREE); | |
9380 | ||
9381 | /* If we saw `typename', or didn't see `::', then there must be a | |
9382 | nested-name-specifier present. */ | |
9383 | if (typename_p || !global_scope_p) | |
9384 | cp_parser_nested_name_specifier (parser, typename_p, | |
9385 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9386 | /*type_p=*/false, |
9387 | /*is_declaration=*/true); | |
a723baf1 MM |
9388 | /* Otherwise, we could be in either of the two productions. In that |
9389 | case, treat the nested-name-specifier as optional. */ | |
9390 | else | |
9391 | cp_parser_nested_name_specifier_opt (parser, | |
9392 | /*typename_keyword_p=*/false, | |
9393 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9394 | /*type_p=*/false, |
9395 | /*is_declaration=*/true); | |
a723baf1 MM |
9396 | |
9397 | /* Parse the unqualified-id. */ | |
9398 | identifier = cp_parser_unqualified_id (parser, | |
9399 | /*template_keyword_p=*/false, | |
f3c2dfc6 MM |
9400 | /*check_dependency_p=*/true, |
9401 | /*declarator_p=*/true); | |
a723baf1 MM |
9402 | |
9403 | /* The function we call to handle a using-declaration is different | |
9404 | depending on what scope we are in. */ | |
f3c2dfc6 MM |
9405 | if (identifier == error_mark_node) |
9406 | ; | |
9407 | else if (TREE_CODE (identifier) != IDENTIFIER_NODE | |
9408 | && TREE_CODE (identifier) != BIT_NOT_EXPR) | |
9409 | /* [namespace.udecl] | |
9410 | ||
9411 | A using declaration shall not name a template-id. */ | |
9412 | error ("a template-id may not appear in a using-declaration"); | |
a723baf1 MM |
9413 | else |
9414 | { | |
f3c2dfc6 MM |
9415 | scope = current_scope (); |
9416 | if (scope && TYPE_P (scope)) | |
4eb6d609 | 9417 | { |
f3c2dfc6 MM |
9418 | /* Create the USING_DECL. */ |
9419 | decl = do_class_using_decl (build_nt (SCOPE_REF, | |
9420 | parser->scope, | |
9421 | identifier)); | |
9422 | /* Add it to the list of members in this class. */ | |
9423 | finish_member_declaration (decl); | |
4eb6d609 | 9424 | } |
a723baf1 | 9425 | else |
f3c2dfc6 MM |
9426 | { |
9427 | decl = cp_parser_lookup_name_simple (parser, identifier); | |
9428 | if (decl == error_mark_node) | |
4bb8ca28 | 9429 | cp_parser_name_lookup_error (parser, identifier, decl, NULL); |
f3c2dfc6 MM |
9430 | else if (scope) |
9431 | do_local_using_decl (decl); | |
9432 | else | |
9433 | do_toplevel_using_decl (decl); | |
9434 | } | |
a723baf1 MM |
9435 | } |
9436 | ||
9437 | /* Look for the final `;'. */ | |
9438 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
9439 | } | |
9440 | ||
9441 | /* Parse a using-directive. | |
9442 | ||
9443 | using-directive: | |
9444 | using namespace :: [opt] nested-name-specifier [opt] | |
9445 | namespace-name ; */ | |
9446 | ||
9447 | static void | |
94edc4ab | 9448 | cp_parser_using_directive (cp_parser* parser) |
a723baf1 MM |
9449 | { |
9450 | tree namespace_decl; | |
86098eb8 | 9451 | tree attribs; |
a723baf1 MM |
9452 | |
9453 | /* Look for the `using' keyword. */ | |
9454 | cp_parser_require_keyword (parser, RID_USING, "`using'"); | |
9455 | /* And the `namespace' keyword. */ | |
9456 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
9457 | /* Look for the optional `::' operator. */ | |
9458 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); | |
34cd5ae7 | 9459 | /* And the optional nested-name-specifier. */ |
a723baf1 MM |
9460 | cp_parser_nested_name_specifier_opt (parser, |
9461 | /*typename_keyword_p=*/false, | |
9462 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9463 | /*type_p=*/false, |
9464 | /*is_declaration=*/true); | |
a723baf1 MM |
9465 | /* Get the namespace being used. */ |
9466 | namespace_decl = cp_parser_namespace_name (parser); | |
86098eb8 JM |
9467 | /* And any specified attributes. */ |
9468 | attribs = cp_parser_attributes_opt (parser); | |
a723baf1 | 9469 | /* Update the symbol table. */ |
86098eb8 | 9470 | parse_using_directive (namespace_decl, attribs); |
a723baf1 MM |
9471 | /* Look for the final `;'. */ |
9472 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
9473 | } | |
9474 | ||
9475 | /* Parse an asm-definition. | |
9476 | ||
9477 | asm-definition: | |
9478 | asm ( string-literal ) ; | |
9479 | ||
9480 | GNU Extension: | |
9481 | ||
9482 | asm-definition: | |
9483 | asm volatile [opt] ( string-literal ) ; | |
9484 | asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ; | |
9485 | asm volatile [opt] ( string-literal : asm-operand-list [opt] | |
9486 | : asm-operand-list [opt] ) ; | |
9487 | asm volatile [opt] ( string-literal : asm-operand-list [opt] | |
9488 | : asm-operand-list [opt] | |
9489 | : asm-operand-list [opt] ) ; */ | |
9490 | ||
9491 | static void | |
94edc4ab | 9492 | cp_parser_asm_definition (cp_parser* parser) |
a723baf1 MM |
9493 | { |
9494 | cp_token *token; | |
9495 | tree string; | |
9496 | tree outputs = NULL_TREE; | |
9497 | tree inputs = NULL_TREE; | |
9498 | tree clobbers = NULL_TREE; | |
9499 | tree asm_stmt; | |
9500 | bool volatile_p = false; | |
9501 | bool extended_p = false; | |
9502 | ||
9503 | /* Look for the `asm' keyword. */ | |
9504 | cp_parser_require_keyword (parser, RID_ASM, "`asm'"); | |
9505 | /* See if the next token is `volatile'. */ | |
9506 | if (cp_parser_allow_gnu_extensions_p (parser) | |
9507 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE)) | |
9508 | { | |
9509 | /* Remember that we saw the `volatile' keyword. */ | |
9510 | volatile_p = true; | |
9511 | /* Consume the token. */ | |
9512 | cp_lexer_consume_token (parser->lexer); | |
9513 | } | |
9514 | /* Look for the opening `('. */ | |
9515 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
9516 | /* Look for the string. */ | |
9517 | token = cp_parser_require (parser, CPP_STRING, "asm body"); | |
9518 | if (!token) | |
9519 | return; | |
9520 | string = token->value; | |
9521 | /* If we're allowing GNU extensions, check for the extended assembly | |
9522 | syntax. Unfortunately, the `:' tokens need not be separated by | |
9523 | a space in C, and so, for compatibility, we tolerate that here | |
9524 | too. Doing that means that we have to treat the `::' operator as | |
9525 | two `:' tokens. */ | |
9526 | if (cp_parser_allow_gnu_extensions_p (parser) | |
9527 | && at_function_scope_p () | |
9528 | && (cp_lexer_next_token_is (parser->lexer, CPP_COLON) | |
9529 | || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))) | |
9530 | { | |
9531 | bool inputs_p = false; | |
9532 | bool clobbers_p = false; | |
9533 | ||
9534 | /* The extended syntax was used. */ | |
9535 | extended_p = true; | |
9536 | ||
9537 | /* Look for outputs. */ | |
9538 | if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
9539 | { | |
9540 | /* Consume the `:'. */ | |
9541 | cp_lexer_consume_token (parser->lexer); | |
9542 | /* Parse the output-operands. */ | |
9543 | if (cp_lexer_next_token_is_not (parser->lexer, | |
9544 | CPP_COLON) | |
9545 | && cp_lexer_next_token_is_not (parser->lexer, | |
8caf4c38 MM |
9546 | CPP_SCOPE) |
9547 | && cp_lexer_next_token_is_not (parser->lexer, | |
9548 | CPP_CLOSE_PAREN)) | |
a723baf1 MM |
9549 | outputs = cp_parser_asm_operand_list (parser); |
9550 | } | |
9551 | /* If the next token is `::', there are no outputs, and the | |
9552 | next token is the beginning of the inputs. */ | |
9553 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
9554 | { | |
9555 | /* Consume the `::' token. */ | |
9556 | cp_lexer_consume_token (parser->lexer); | |
9557 | /* The inputs are coming next. */ | |
9558 | inputs_p = true; | |
9559 | } | |
9560 | ||
9561 | /* Look for inputs. */ | |
9562 | if (inputs_p | |
9563 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
9564 | { | |
9565 | if (!inputs_p) | |
9566 | /* Consume the `:'. */ | |
9567 | cp_lexer_consume_token (parser->lexer); | |
9568 | /* Parse the output-operands. */ | |
9569 | if (cp_lexer_next_token_is_not (parser->lexer, | |
9570 | CPP_COLON) | |
9571 | && cp_lexer_next_token_is_not (parser->lexer, | |
8caf4c38 MM |
9572 | CPP_SCOPE) |
9573 | && cp_lexer_next_token_is_not (parser->lexer, | |
9574 | CPP_CLOSE_PAREN)) | |
a723baf1 MM |
9575 | inputs = cp_parser_asm_operand_list (parser); |
9576 | } | |
9577 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
9578 | /* The clobbers are coming next. */ | |
9579 | clobbers_p = true; | |
9580 | ||
9581 | /* Look for clobbers. */ | |
9582 | if (clobbers_p | |
9583 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
9584 | { | |
9585 | if (!clobbers_p) | |
9586 | /* Consume the `:'. */ | |
9587 | cp_lexer_consume_token (parser->lexer); | |
9588 | /* Parse the clobbers. */ | |
8caf4c38 MM |
9589 | if (cp_lexer_next_token_is_not (parser->lexer, |
9590 | CPP_CLOSE_PAREN)) | |
9591 | clobbers = cp_parser_asm_clobber_list (parser); | |
a723baf1 MM |
9592 | } |
9593 | } | |
9594 | /* Look for the closing `)'. */ | |
9595 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
a668c6ad MM |
9596 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
9597 | /*consume_paren=*/true); | |
a723baf1 MM |
9598 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
9599 | ||
9600 | /* Create the ASM_STMT. */ | |
9601 | if (at_function_scope_p ()) | |
9602 | { | |
9603 | asm_stmt = | |
9604 | finish_asm_stmt (volatile_p | |
9605 | ? ridpointers[(int) RID_VOLATILE] : NULL_TREE, | |
9606 | string, outputs, inputs, clobbers); | |
9607 | /* If the extended syntax was not used, mark the ASM_STMT. */ | |
9608 | if (!extended_p) | |
9609 | ASM_INPUT_P (asm_stmt) = 1; | |
9610 | } | |
9611 | else | |
9612 | assemble_asm (string); | |
9613 | } | |
9614 | ||
9615 | /* Declarators [gram.dcl.decl] */ | |
9616 | ||
9617 | /* Parse an init-declarator. | |
9618 | ||
9619 | init-declarator: | |
9620 | declarator initializer [opt] | |
9621 | ||
9622 | GNU Extension: | |
9623 | ||
9624 | init-declarator: | |
9625 | declarator asm-specification [opt] attributes [opt] initializer [opt] | |
9626 | ||
4bb8ca28 MM |
9627 | function-definition: |
9628 | decl-specifier-seq [opt] declarator ctor-initializer [opt] | |
9629 | function-body | |
9630 | decl-specifier-seq [opt] declarator function-try-block | |
9631 | ||
9632 | GNU Extension: | |
9633 | ||
9634 | function-definition: | |
9635 | __extension__ function-definition | |
9636 | ||
a723baf1 | 9637 | The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator. |
c8e4f0e9 | 9638 | Returns a representation of the entity declared. If MEMBER_P is TRUE, |
cf22909c KL |
9639 | then this declarator appears in a class scope. The new DECL created |
9640 | by this declarator is returned. | |
a723baf1 MM |
9641 | |
9642 | If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and | |
9643 | for a function-definition here as well. If the declarator is a | |
9644 | declarator for a function-definition, *FUNCTION_DEFINITION_P will | |
9645 | be TRUE upon return. By that point, the function-definition will | |
9646 | have been completely parsed. | |
9647 | ||
9648 | FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P | |
9649 | is FALSE. */ | |
9650 | ||
9651 | static tree | |
94edc4ab NN |
9652 | cp_parser_init_declarator (cp_parser* parser, |
9653 | tree decl_specifiers, | |
9654 | tree prefix_attributes, | |
9655 | bool function_definition_allowed_p, | |
9656 | bool member_p, | |
560ad596 | 9657 | int declares_class_or_enum, |
94edc4ab | 9658 | bool* function_definition_p) |
a723baf1 MM |
9659 | { |
9660 | cp_token *token; | |
9661 | tree declarator; | |
9662 | tree attributes; | |
9663 | tree asm_specification; | |
9664 | tree initializer; | |
9665 | tree decl = NULL_TREE; | |
9666 | tree scope; | |
a723baf1 MM |
9667 | bool is_initialized; |
9668 | bool is_parenthesized_init; | |
39703eb9 | 9669 | bool is_non_constant_init; |
7efa3e22 | 9670 | int ctor_dtor_or_conv_p; |
a723baf1 MM |
9671 | bool friend_p; |
9672 | ||
9673 | /* Assume that this is not the declarator for a function | |
9674 | definition. */ | |
9675 | if (function_definition_p) | |
9676 | *function_definition_p = false; | |
9677 | ||
9678 | /* Defer access checks while parsing the declarator; we cannot know | |
9679 | what names are accessible until we know what is being | |
9680 | declared. */ | |
cf22909c KL |
9681 | resume_deferring_access_checks (); |
9682 | ||
a723baf1 MM |
9683 | /* Parse the declarator. */ |
9684 | declarator | |
62b8a44e | 9685 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
9686 | &ctor_dtor_or_conv_p, |
9687 | /*parenthesized_p=*/NULL); | |
a723baf1 | 9688 | /* Gather up the deferred checks. */ |
cf22909c | 9689 | stop_deferring_access_checks (); |
24c0ef37 | 9690 | |
a723baf1 MM |
9691 | /* If the DECLARATOR was erroneous, there's no need to go |
9692 | further. */ | |
9693 | if (declarator == error_mark_node) | |
cf22909c | 9694 | return error_mark_node; |
a723baf1 | 9695 | |
560ad596 MM |
9696 | cp_parser_check_for_definition_in_return_type (declarator, |
9697 | declares_class_or_enum); | |
9698 | ||
a723baf1 MM |
9699 | /* Figure out what scope the entity declared by the DECLARATOR is |
9700 | located in. `grokdeclarator' sometimes changes the scope, so | |
9701 | we compute it now. */ | |
9702 | scope = get_scope_of_declarator (declarator); | |
9703 | ||
9704 | /* If we're allowing GNU extensions, look for an asm-specification | |
9705 | and attributes. */ | |
9706 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
9707 | { | |
9708 | /* Look for an asm-specification. */ | |
9709 | asm_specification = cp_parser_asm_specification_opt (parser); | |
9710 | /* And attributes. */ | |
9711 | attributes = cp_parser_attributes_opt (parser); | |
9712 | } | |
9713 | else | |
9714 | { | |
9715 | asm_specification = NULL_TREE; | |
9716 | attributes = NULL_TREE; | |
9717 | } | |
9718 | ||
9719 | /* Peek at the next token. */ | |
9720 | token = cp_lexer_peek_token (parser->lexer); | |
9721 | /* Check to see if the token indicates the start of a | |
9722 | function-definition. */ | |
9723 | if (cp_parser_token_starts_function_definition_p (token)) | |
9724 | { | |
9725 | if (!function_definition_allowed_p) | |
9726 | { | |
9727 | /* If a function-definition should not appear here, issue an | |
9728 | error message. */ | |
9729 | cp_parser_error (parser, | |
9730 | "a function-definition is not allowed here"); | |
9731 | return error_mark_node; | |
9732 | } | |
9733 | else | |
9734 | { | |
a723baf1 MM |
9735 | /* Neither attributes nor an asm-specification are allowed |
9736 | on a function-definition. */ | |
9737 | if (asm_specification) | |
9738 | error ("an asm-specification is not allowed on a function-definition"); | |
9739 | if (attributes) | |
9740 | error ("attributes are not allowed on a function-definition"); | |
9741 | /* This is a function-definition. */ | |
9742 | *function_definition_p = true; | |
9743 | ||
a723baf1 | 9744 | /* Parse the function definition. */ |
4bb8ca28 MM |
9745 | if (member_p) |
9746 | decl = cp_parser_save_member_function_body (parser, | |
9747 | decl_specifiers, | |
9748 | declarator, | |
9749 | prefix_attributes); | |
9750 | else | |
9751 | decl | |
9752 | = (cp_parser_function_definition_from_specifiers_and_declarator | |
9753 | (parser, decl_specifiers, prefix_attributes, declarator)); | |
24c0ef37 | 9754 | |
a723baf1 MM |
9755 | return decl; |
9756 | } | |
9757 | } | |
9758 | ||
9759 | /* [dcl.dcl] | |
9760 | ||
9761 | Only in function declarations for constructors, destructors, and | |
9762 | type conversions can the decl-specifier-seq be omitted. | |
9763 | ||
9764 | We explicitly postpone this check past the point where we handle | |
9765 | function-definitions because we tolerate function-definitions | |
9766 | that are missing their return types in some modes. */ | |
7efa3e22 | 9767 | if (!decl_specifiers && ctor_dtor_or_conv_p <= 0) |
a723baf1 MM |
9768 | { |
9769 | cp_parser_error (parser, | |
9770 | "expected constructor, destructor, or type conversion"); | |
9771 | return error_mark_node; | |
9772 | } | |
9773 | ||
9774 | /* An `=' or an `(' indicates an initializer. */ | |
9775 | is_initialized = (token->type == CPP_EQ | |
9776 | || token->type == CPP_OPEN_PAREN); | |
9777 | /* If the init-declarator isn't initialized and isn't followed by a | |
9778 | `,' or `;', it's not a valid init-declarator. */ | |
9779 | if (!is_initialized | |
9780 | && token->type != CPP_COMMA | |
9781 | && token->type != CPP_SEMICOLON) | |
9782 | { | |
9783 | cp_parser_error (parser, "expected init-declarator"); | |
9784 | return error_mark_node; | |
9785 | } | |
9786 | ||
9787 | /* Because start_decl has side-effects, we should only call it if we | |
9788 | know we're going ahead. By this point, we know that we cannot | |
9789 | possibly be looking at any other construct. */ | |
9790 | cp_parser_commit_to_tentative_parse (parser); | |
9791 | ||
9792 | /* Check to see whether or not this declaration is a friend. */ | |
9793 | friend_p = cp_parser_friend_p (decl_specifiers); | |
9794 | ||
9795 | /* Check that the number of template-parameter-lists is OK. */ | |
ee3071ef | 9796 | if (!cp_parser_check_declarator_template_parameters (parser, declarator)) |
cf22909c | 9797 | return error_mark_node; |
a723baf1 MM |
9798 | |
9799 | /* Enter the newly declared entry in the symbol table. If we're | |
9800 | processing a declaration in a class-specifier, we wait until | |
9801 | after processing the initializer. */ | |
9802 | if (!member_p) | |
9803 | { | |
9804 | if (parser->in_unbraced_linkage_specification_p) | |
9805 | { | |
9806 | decl_specifiers = tree_cons (error_mark_node, | |
9807 | get_identifier ("extern"), | |
9808 | decl_specifiers); | |
9809 | have_extern_spec = false; | |
9810 | } | |
ee3071ef NS |
9811 | decl = start_decl (declarator, decl_specifiers, |
9812 | is_initialized, attributes, prefix_attributes); | |
a723baf1 MM |
9813 | } |
9814 | ||
9815 | /* Enter the SCOPE. That way unqualified names appearing in the | |
9816 | initializer will be looked up in SCOPE. */ | |
9817 | if (scope) | |
9818 | push_scope (scope); | |
9819 | ||
9820 | /* Perform deferred access control checks, now that we know in which | |
9821 | SCOPE the declared entity resides. */ | |
9822 | if (!member_p && decl) | |
9823 | { | |
9824 | tree saved_current_function_decl = NULL_TREE; | |
9825 | ||
9826 | /* If the entity being declared is a function, pretend that we | |
9827 | are in its scope. If it is a `friend', it may have access to | |
9bcb9aae | 9828 | things that would not otherwise be accessible. */ |
a723baf1 MM |
9829 | if (TREE_CODE (decl) == FUNCTION_DECL) |
9830 | { | |
9831 | saved_current_function_decl = current_function_decl; | |
9832 | current_function_decl = decl; | |
9833 | } | |
9834 | ||
cf22909c KL |
9835 | /* Perform the access control checks for the declarator and the |
9836 | the decl-specifiers. */ | |
9837 | perform_deferred_access_checks (); | |
a723baf1 MM |
9838 | |
9839 | /* Restore the saved value. */ | |
9840 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
9841 | current_function_decl = saved_current_function_decl; | |
9842 | } | |
9843 | ||
9844 | /* Parse the initializer. */ | |
9845 | if (is_initialized) | |
39703eb9 MM |
9846 | initializer = cp_parser_initializer (parser, |
9847 | &is_parenthesized_init, | |
9848 | &is_non_constant_init); | |
a723baf1 MM |
9849 | else |
9850 | { | |
9851 | initializer = NULL_TREE; | |
9852 | is_parenthesized_init = false; | |
39703eb9 | 9853 | is_non_constant_init = true; |
a723baf1 MM |
9854 | } |
9855 | ||
9856 | /* The old parser allows attributes to appear after a parenthesized | |
9857 | initializer. Mark Mitchell proposed removing this functionality | |
9858 | on the GCC mailing lists on 2002-08-13. This parser accepts the | |
9859 | attributes -- but ignores them. */ | |
9860 | if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init) | |
9861 | if (cp_parser_attributes_opt (parser)) | |
9862 | warning ("attributes after parenthesized initializer ignored"); | |
9863 | ||
9864 | /* Leave the SCOPE, now that we have processed the initializer. It | |
9865 | is important to do this before calling cp_finish_decl because it | |
9866 | makes decisions about whether to create DECL_STMTs or not based | |
9867 | on the current scope. */ | |
9868 | if (scope) | |
9869 | pop_scope (scope); | |
9870 | ||
9871 | /* For an in-class declaration, use `grokfield' to create the | |
9872 | declaration. */ | |
9873 | if (member_p) | |
8db1028e NS |
9874 | { |
9875 | decl = grokfield (declarator, decl_specifiers, | |
9876 | initializer, /*asmspec=*/NULL_TREE, | |
a723baf1 | 9877 | /*attributes=*/NULL_TREE); |
8db1028e NS |
9878 | if (decl && TREE_CODE (decl) == FUNCTION_DECL) |
9879 | cp_parser_save_default_args (parser, decl); | |
9880 | } | |
9881 | ||
a723baf1 MM |
9882 | /* Finish processing the declaration. But, skip friend |
9883 | declarations. */ | |
9884 | if (!friend_p && decl) | |
9885 | cp_finish_decl (decl, | |
9886 | initializer, | |
9887 | asm_specification, | |
9888 | /* If the initializer is in parentheses, then this is | |
9889 | a direct-initialization, which means that an | |
9890 | `explicit' constructor is OK. Otherwise, an | |
9891 | `explicit' constructor cannot be used. */ | |
9892 | ((is_parenthesized_init || !is_initialized) | |
9893 | ? 0 : LOOKUP_ONLYCONVERTING)); | |
9894 | ||
39703eb9 MM |
9895 | /* Remember whether or not variables were initialized by |
9896 | constant-expressions. */ | |
9897 | if (decl && TREE_CODE (decl) == VAR_DECL | |
9898 | && is_initialized && !is_non_constant_init) | |
9899 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; | |
9900 | ||
a723baf1 MM |
9901 | return decl; |
9902 | } | |
9903 | ||
9904 | /* Parse a declarator. | |
9905 | ||
9906 | declarator: | |
9907 | direct-declarator | |
9908 | ptr-operator declarator | |
9909 | ||
9910 | abstract-declarator: | |
9911 | ptr-operator abstract-declarator [opt] | |
9912 | direct-abstract-declarator | |
9913 | ||
9914 | GNU Extensions: | |
9915 | ||
9916 | declarator: | |
9917 | attributes [opt] direct-declarator | |
9918 | attributes [opt] ptr-operator declarator | |
9919 | ||
9920 | abstract-declarator: | |
9921 | attributes [opt] ptr-operator abstract-declarator [opt] | |
9922 | attributes [opt] direct-abstract-declarator | |
9923 | ||
9924 | Returns a representation of the declarator. If the declarator has | |
9925 | the form `* declarator', then an INDIRECT_REF is returned, whose | |
34cd5ae7 | 9926 | only operand is the sub-declarator. Analogously, `& declarator' is |
a723baf1 MM |
9927 | represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is |
9928 | used. The first operand is the TYPE for `X'. The second operand | |
9929 | is an INDIRECT_REF whose operand is the sub-declarator. | |
9930 | ||
34cd5ae7 | 9931 | Otherwise, the representation is as for a direct-declarator. |
a723baf1 MM |
9932 | |
9933 | (It would be better to define a structure type to represent | |
9934 | declarators, rather than abusing `tree' nodes to represent | |
9935 | declarators. That would be much clearer and save some memory. | |
9936 | There is no reason for declarators to be garbage-collected, for | |
9937 | example; they are created during parser and no longer needed after | |
9938 | `grokdeclarator' has been called.) | |
9939 | ||
9940 | For a ptr-operator that has the optional cv-qualifier-seq, | |
9941 | cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF | |
9942 | node. | |
9943 | ||
7efa3e22 NS |
9944 | If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to |
9945 | detect constructor, destructor or conversion operators. It is set | |
9946 | to -1 if the declarator is a name, and +1 if it is a | |
9947 | function. Otherwise it is set to zero. Usually you just want to | |
9948 | test for >0, but internally the negative value is used. | |
9949 | ||
a723baf1 MM |
9950 | (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have |
9951 | a decl-specifier-seq unless it declares a constructor, destructor, | |
9952 | or conversion. It might seem that we could check this condition in | |
9953 | semantic analysis, rather than parsing, but that makes it difficult | |
9954 | to handle something like `f()'. We want to notice that there are | |
9955 | no decl-specifiers, and therefore realize that this is an | |
4bb8ca28 MM |
9956 | expression, not a declaration.) |
9957 | ||
9958 | If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff | |
9959 | the declarator is a direct-declarator of the form "(...)". */ | |
a723baf1 MM |
9960 | |
9961 | static tree | |
94edc4ab NN |
9962 | cp_parser_declarator (cp_parser* parser, |
9963 | cp_parser_declarator_kind dcl_kind, | |
4bb8ca28 MM |
9964 | int* ctor_dtor_or_conv_p, |
9965 | bool* parenthesized_p) | |
a723baf1 MM |
9966 | { |
9967 | cp_token *token; | |
9968 | tree declarator; | |
9969 | enum tree_code code; | |
9970 | tree cv_qualifier_seq; | |
9971 | tree class_type; | |
9972 | tree attributes = NULL_TREE; | |
9973 | ||
9974 | /* Assume this is not a constructor, destructor, or type-conversion | |
9975 | operator. */ | |
9976 | if (ctor_dtor_or_conv_p) | |
7efa3e22 | 9977 | *ctor_dtor_or_conv_p = 0; |
a723baf1 MM |
9978 | |
9979 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
9980 | attributes = cp_parser_attributes_opt (parser); | |
9981 | ||
9982 | /* Peek at the next token. */ | |
9983 | token = cp_lexer_peek_token (parser->lexer); | |
9984 | ||
9985 | /* Check for the ptr-operator production. */ | |
9986 | cp_parser_parse_tentatively (parser); | |
9987 | /* Parse the ptr-operator. */ | |
9988 | code = cp_parser_ptr_operator (parser, | |
9989 | &class_type, | |
9990 | &cv_qualifier_seq); | |
9991 | /* If that worked, then we have a ptr-operator. */ | |
9992 | if (cp_parser_parse_definitely (parser)) | |
9993 | { | |
4bb8ca28 MM |
9994 | /* If a ptr-operator was found, then this declarator was not |
9995 | parenthesized. */ | |
9996 | if (parenthesized_p) | |
9997 | *parenthesized_p = true; | |
a723baf1 MM |
9998 | /* The dependent declarator is optional if we are parsing an |
9999 | abstract-declarator. */ | |
62b8a44e | 10000 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
a723baf1 MM |
10001 | cp_parser_parse_tentatively (parser); |
10002 | ||
10003 | /* Parse the dependent declarator. */ | |
62b8a44e | 10004 | declarator = cp_parser_declarator (parser, dcl_kind, |
4bb8ca28 MM |
10005 | /*ctor_dtor_or_conv_p=*/NULL, |
10006 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
10007 | |
10008 | /* If we are parsing an abstract-declarator, we must handle the | |
10009 | case where the dependent declarator is absent. */ | |
62b8a44e NS |
10010 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED |
10011 | && !cp_parser_parse_definitely (parser)) | |
a723baf1 MM |
10012 | declarator = NULL_TREE; |
10013 | ||
10014 | /* Build the representation of the ptr-operator. */ | |
10015 | if (code == INDIRECT_REF) | |
10016 | declarator = make_pointer_declarator (cv_qualifier_seq, | |
10017 | declarator); | |
10018 | else | |
10019 | declarator = make_reference_declarator (cv_qualifier_seq, | |
10020 | declarator); | |
10021 | /* Handle the pointer-to-member case. */ | |
10022 | if (class_type) | |
10023 | declarator = build_nt (SCOPE_REF, class_type, declarator); | |
10024 | } | |
10025 | /* Everything else is a direct-declarator. */ | |
10026 | else | |
4bb8ca28 MM |
10027 | { |
10028 | if (parenthesized_p) | |
10029 | *parenthesized_p = cp_lexer_next_token_is (parser->lexer, | |
10030 | CPP_OPEN_PAREN); | |
10031 | declarator = cp_parser_direct_declarator (parser, dcl_kind, | |
10032 | ctor_dtor_or_conv_p); | |
10033 | } | |
a723baf1 MM |
10034 | |
10035 | if (attributes && declarator != error_mark_node) | |
10036 | declarator = tree_cons (attributes, declarator, NULL_TREE); | |
10037 | ||
10038 | return declarator; | |
10039 | } | |
10040 | ||
10041 | /* Parse a direct-declarator or direct-abstract-declarator. | |
10042 | ||
10043 | direct-declarator: | |
10044 | declarator-id | |
10045 | direct-declarator ( parameter-declaration-clause ) | |
10046 | cv-qualifier-seq [opt] | |
10047 | exception-specification [opt] | |
10048 | direct-declarator [ constant-expression [opt] ] | |
10049 | ( declarator ) | |
10050 | ||
10051 | direct-abstract-declarator: | |
10052 | direct-abstract-declarator [opt] | |
10053 | ( parameter-declaration-clause ) | |
10054 | cv-qualifier-seq [opt] | |
10055 | exception-specification [opt] | |
10056 | direct-abstract-declarator [opt] [ constant-expression [opt] ] | |
10057 | ( abstract-declarator ) | |
10058 | ||
62b8a44e NS |
10059 | Returns a representation of the declarator. DCL_KIND is |
10060 | CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a | |
10061 | direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if | |
10062 | we are parsing a direct-declarator. It is | |
10063 | CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case | |
10064 | of ambiguity we prefer an abstract declarator, as per | |
10065 | [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for | |
a723baf1 MM |
10066 | cp_parser_declarator. |
10067 | ||
10068 | For the declarator-id production, the representation is as for an | |
10069 | id-expression, except that a qualified name is represented as a | |
10070 | SCOPE_REF. A function-declarator is represented as a CALL_EXPR; | |
10071 | see the documentation of the FUNCTION_DECLARATOR_* macros for | |
10072 | information about how to find the various declarator components. | |
10073 | An array-declarator is represented as an ARRAY_REF. The | |
10074 | direct-declarator is the first operand; the constant-expression | |
10075 | indicating the size of the array is the second operand. */ | |
10076 | ||
10077 | static tree | |
94edc4ab NN |
10078 | cp_parser_direct_declarator (cp_parser* parser, |
10079 | cp_parser_declarator_kind dcl_kind, | |
7efa3e22 | 10080 | int* ctor_dtor_or_conv_p) |
a723baf1 MM |
10081 | { |
10082 | cp_token *token; | |
62b8a44e | 10083 | tree declarator = NULL_TREE; |
a723baf1 MM |
10084 | tree scope = NULL_TREE; |
10085 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
10086 | bool saved_in_declarator_p = parser->in_declarator_p; | |
62b8a44e NS |
10087 | bool first = true; |
10088 | ||
10089 | while (true) | |
a723baf1 | 10090 | { |
62b8a44e NS |
10091 | /* Peek at the next token. */ |
10092 | token = cp_lexer_peek_token (parser->lexer); | |
10093 | if (token->type == CPP_OPEN_PAREN) | |
a723baf1 | 10094 | { |
62b8a44e NS |
10095 | /* This is either a parameter-declaration-clause, or a |
10096 | parenthesized declarator. When we know we are parsing a | |
34cd5ae7 | 10097 | named declarator, it must be a parenthesized declarator |
62b8a44e NS |
10098 | if FIRST is true. For instance, `(int)' is a |
10099 | parameter-declaration-clause, with an omitted | |
10100 | direct-abstract-declarator. But `((*))', is a | |
10101 | parenthesized abstract declarator. Finally, when T is a | |
10102 | template parameter `(T)' is a | |
34cd5ae7 | 10103 | parameter-declaration-clause, and not a parenthesized |
62b8a44e | 10104 | named declarator. |
a723baf1 | 10105 | |
62b8a44e NS |
10106 | We first try and parse a parameter-declaration-clause, |
10107 | and then try a nested declarator (if FIRST is true). | |
a723baf1 | 10108 | |
62b8a44e NS |
10109 | It is not an error for it not to be a |
10110 | parameter-declaration-clause, even when FIRST is | |
10111 | false. Consider, | |
10112 | ||
10113 | int i (int); | |
10114 | int i (3); | |
10115 | ||
10116 | The first is the declaration of a function while the | |
10117 | second is a the definition of a variable, including its | |
10118 | initializer. | |
10119 | ||
10120 | Having seen only the parenthesis, we cannot know which of | |
10121 | these two alternatives should be selected. Even more | |
10122 | complex are examples like: | |
10123 | ||
10124 | int i (int (a)); | |
10125 | int i (int (3)); | |
10126 | ||
10127 | The former is a function-declaration; the latter is a | |
10128 | variable initialization. | |
10129 | ||
34cd5ae7 | 10130 | Thus again, we try a parameter-declaration-clause, and if |
62b8a44e NS |
10131 | that fails, we back out and return. */ |
10132 | ||
10133 | if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) | |
a723baf1 | 10134 | { |
62b8a44e | 10135 | tree params; |
4047b164 | 10136 | unsigned saved_num_template_parameter_lists; |
62b8a44e NS |
10137 | |
10138 | cp_parser_parse_tentatively (parser); | |
a723baf1 | 10139 | |
62b8a44e NS |
10140 | /* Consume the `('. */ |
10141 | cp_lexer_consume_token (parser->lexer); | |
10142 | if (first) | |
10143 | { | |
10144 | /* If this is going to be an abstract declarator, we're | |
10145 | in a declarator and we can't have default args. */ | |
10146 | parser->default_arg_ok_p = false; | |
10147 | parser->in_declarator_p = true; | |
10148 | } | |
10149 | ||
4047b164 KL |
10150 | /* Inside the function parameter list, surrounding |
10151 | template-parameter-lists do not apply. */ | |
10152 | saved_num_template_parameter_lists | |
10153 | = parser->num_template_parameter_lists; | |
10154 | parser->num_template_parameter_lists = 0; | |
10155 | ||
62b8a44e NS |
10156 | /* Parse the parameter-declaration-clause. */ |
10157 | params = cp_parser_parameter_declaration_clause (parser); | |
10158 | ||
4047b164 KL |
10159 | parser->num_template_parameter_lists |
10160 | = saved_num_template_parameter_lists; | |
10161 | ||
62b8a44e | 10162 | /* If all went well, parse the cv-qualifier-seq and the |
34cd5ae7 | 10163 | exception-specification. */ |
62b8a44e NS |
10164 | if (cp_parser_parse_definitely (parser)) |
10165 | { | |
10166 | tree cv_qualifiers; | |
10167 | tree exception_specification; | |
7efa3e22 NS |
10168 | |
10169 | if (ctor_dtor_or_conv_p) | |
10170 | *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0; | |
62b8a44e NS |
10171 | first = false; |
10172 | /* Consume the `)'. */ | |
10173 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
10174 | ||
10175 | /* Parse the cv-qualifier-seq. */ | |
10176 | cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser); | |
10177 | /* And the exception-specification. */ | |
10178 | exception_specification | |
10179 | = cp_parser_exception_specification_opt (parser); | |
10180 | ||
10181 | /* Create the function-declarator. */ | |
10182 | declarator = make_call_declarator (declarator, | |
10183 | params, | |
10184 | cv_qualifiers, | |
10185 | exception_specification); | |
10186 | /* Any subsequent parameter lists are to do with | |
10187 | return type, so are not those of the declared | |
10188 | function. */ | |
10189 | parser->default_arg_ok_p = false; | |
10190 | ||
10191 | /* Repeat the main loop. */ | |
10192 | continue; | |
10193 | } | |
10194 | } | |
10195 | ||
10196 | /* If this is the first, we can try a parenthesized | |
10197 | declarator. */ | |
10198 | if (first) | |
a723baf1 | 10199 | { |
a723baf1 | 10200 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
62b8a44e NS |
10201 | parser->in_declarator_p = saved_in_declarator_p; |
10202 | ||
10203 | /* Consume the `('. */ | |
10204 | cp_lexer_consume_token (parser->lexer); | |
10205 | /* Parse the nested declarator. */ | |
10206 | declarator | |
4bb8ca28 MM |
10207 | = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p, |
10208 | /*parenthesized_p=*/NULL); | |
62b8a44e NS |
10209 | first = false; |
10210 | /* Expect a `)'. */ | |
10211 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
10212 | declarator = error_mark_node; | |
10213 | if (declarator == error_mark_node) | |
10214 | break; | |
10215 | ||
10216 | goto handle_declarator; | |
a723baf1 | 10217 | } |
9bcb9aae | 10218 | /* Otherwise, we must be done. */ |
62b8a44e NS |
10219 | else |
10220 | break; | |
a723baf1 | 10221 | } |
62b8a44e NS |
10222 | else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
10223 | && token->type == CPP_OPEN_SQUARE) | |
a723baf1 | 10224 | { |
62b8a44e | 10225 | /* Parse an array-declarator. */ |
a723baf1 MM |
10226 | tree bounds; |
10227 | ||
7efa3e22 NS |
10228 | if (ctor_dtor_or_conv_p) |
10229 | *ctor_dtor_or_conv_p = 0; | |
10230 | ||
62b8a44e NS |
10231 | first = false; |
10232 | parser->default_arg_ok_p = false; | |
10233 | parser->in_declarator_p = true; | |
a723baf1 MM |
10234 | /* Consume the `['. */ |
10235 | cp_lexer_consume_token (parser->lexer); | |
10236 | /* Peek at the next token. */ | |
10237 | token = cp_lexer_peek_token (parser->lexer); | |
10238 | /* If the next token is `]', then there is no | |
10239 | constant-expression. */ | |
10240 | if (token->type != CPP_CLOSE_SQUARE) | |
14d22dd6 MM |
10241 | { |
10242 | bool non_constant_p; | |
10243 | ||
10244 | bounds | |
10245 | = cp_parser_constant_expression (parser, | |
10246 | /*allow_non_constant=*/true, | |
10247 | &non_constant_p); | |
d17811fd MM |
10248 | if (!non_constant_p) |
10249 | bounds = cp_parser_fold_non_dependent_expr (bounds); | |
14d22dd6 | 10250 | } |
a723baf1 MM |
10251 | else |
10252 | bounds = NULL_TREE; | |
10253 | /* Look for the closing `]'. */ | |
62b8a44e NS |
10254 | if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'")) |
10255 | { | |
10256 | declarator = error_mark_node; | |
10257 | break; | |
10258 | } | |
a723baf1 MM |
10259 | |
10260 | declarator = build_nt (ARRAY_REF, declarator, bounds); | |
10261 | } | |
62b8a44e | 10262 | else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT) |
a723baf1 | 10263 | { |
a668c6ad | 10264 | /* Parse a declarator-id */ |
62b8a44e NS |
10265 | if (dcl_kind == CP_PARSER_DECLARATOR_EITHER) |
10266 | cp_parser_parse_tentatively (parser); | |
10267 | declarator = cp_parser_declarator_id (parser); | |
712becab NS |
10268 | if (dcl_kind == CP_PARSER_DECLARATOR_EITHER) |
10269 | { | |
10270 | if (!cp_parser_parse_definitely (parser)) | |
10271 | declarator = error_mark_node; | |
10272 | else if (TREE_CODE (declarator) != IDENTIFIER_NODE) | |
10273 | { | |
10274 | cp_parser_error (parser, "expected unqualified-id"); | |
10275 | declarator = error_mark_node; | |
10276 | } | |
10277 | } | |
10278 | ||
62b8a44e NS |
10279 | if (declarator == error_mark_node) |
10280 | break; | |
a723baf1 | 10281 | |
d9a50301 KL |
10282 | if (TREE_CODE (declarator) == SCOPE_REF |
10283 | && !current_scope ()) | |
62b8a44e NS |
10284 | { |
10285 | tree scope = TREE_OPERAND (declarator, 0); | |
712becab | 10286 | |
62b8a44e NS |
10287 | /* In the declaration of a member of a template class |
10288 | outside of the class itself, the SCOPE will sometimes | |
10289 | be a TYPENAME_TYPE. For example, given: | |
10290 | ||
10291 | template <typename T> | |
10292 | int S<T>::R::i = 3; | |
10293 | ||
10294 | the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In | |
10295 | this context, we must resolve S<T>::R to an ordinary | |
10296 | type, rather than a typename type. | |
10297 | ||
10298 | The reason we normally avoid resolving TYPENAME_TYPEs | |
10299 | is that a specialization of `S' might render | |
10300 | `S<T>::R' not a type. However, if `S' is | |
10301 | specialized, then this `i' will not be used, so there | |
10302 | is no harm in resolving the types here. */ | |
10303 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
10304 | { | |
14d22dd6 MM |
10305 | tree type; |
10306 | ||
62b8a44e | 10307 | /* Resolve the TYPENAME_TYPE. */ |
14d22dd6 MM |
10308 | type = resolve_typename_type (scope, |
10309 | /*only_current_p=*/false); | |
62b8a44e | 10310 | /* If that failed, the declarator is invalid. */ |
14d22dd6 MM |
10311 | if (type != error_mark_node) |
10312 | scope = type; | |
62b8a44e NS |
10313 | /* Build a new DECLARATOR. */ |
10314 | declarator = build_nt (SCOPE_REF, | |
10315 | scope, | |
10316 | TREE_OPERAND (declarator, 1)); | |
10317 | } | |
10318 | } | |
10319 | ||
10320 | /* Check to see whether the declarator-id names a constructor, | |
10321 | destructor, or conversion. */ | |
10322 | if (declarator && ctor_dtor_or_conv_p | |
10323 | && ((TREE_CODE (declarator) == SCOPE_REF | |
10324 | && CLASS_TYPE_P (TREE_OPERAND (declarator, 0))) | |
10325 | || (TREE_CODE (declarator) != SCOPE_REF | |
10326 | && at_class_scope_p ()))) | |
a723baf1 | 10327 | { |
62b8a44e NS |
10328 | tree unqualified_name; |
10329 | tree class_type; | |
10330 | ||
10331 | /* Get the unqualified part of the name. */ | |
10332 | if (TREE_CODE (declarator) == SCOPE_REF) | |
10333 | { | |
10334 | class_type = TREE_OPERAND (declarator, 0); | |
10335 | unqualified_name = TREE_OPERAND (declarator, 1); | |
10336 | } | |
10337 | else | |
10338 | { | |
10339 | class_type = current_class_type; | |
10340 | unqualified_name = declarator; | |
10341 | } | |
10342 | ||
10343 | /* See if it names ctor, dtor or conv. */ | |
10344 | if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR | |
10345 | || IDENTIFIER_TYPENAME_P (unqualified_name) | |
10346 | || constructor_name_p (unqualified_name, class_type)) | |
7efa3e22 | 10347 | *ctor_dtor_or_conv_p = -1; |
a723baf1 | 10348 | } |
62b8a44e NS |
10349 | |
10350 | handle_declarator:; | |
10351 | scope = get_scope_of_declarator (declarator); | |
10352 | if (scope) | |
10353 | /* Any names that appear after the declarator-id for a member | |
10354 | are looked up in the containing scope. */ | |
10355 | push_scope (scope); | |
10356 | parser->in_declarator_p = true; | |
10357 | if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p) | |
10358 | || (declarator | |
10359 | && (TREE_CODE (declarator) == SCOPE_REF | |
10360 | || TREE_CODE (declarator) == IDENTIFIER_NODE))) | |
10361 | /* Default args are only allowed on function | |
10362 | declarations. */ | |
10363 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
a723baf1 | 10364 | else |
62b8a44e NS |
10365 | parser->default_arg_ok_p = false; |
10366 | ||
10367 | first = false; | |
a723baf1 | 10368 | } |
62b8a44e | 10369 | /* We're done. */ |
a723baf1 MM |
10370 | else |
10371 | break; | |
a723baf1 MM |
10372 | } |
10373 | ||
10374 | /* For an abstract declarator, we might wind up with nothing at this | |
10375 | point. That's an error; the declarator is not optional. */ | |
10376 | if (!declarator) | |
10377 | cp_parser_error (parser, "expected declarator"); | |
10378 | ||
10379 | /* If we entered a scope, we must exit it now. */ | |
10380 | if (scope) | |
10381 | pop_scope (scope); | |
10382 | ||
10383 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
10384 | parser->in_declarator_p = saved_in_declarator_p; | |
10385 | ||
10386 | return declarator; | |
10387 | } | |
10388 | ||
10389 | /* Parse a ptr-operator. | |
10390 | ||
10391 | ptr-operator: | |
10392 | * cv-qualifier-seq [opt] | |
10393 | & | |
10394 | :: [opt] nested-name-specifier * cv-qualifier-seq [opt] | |
10395 | ||
10396 | GNU Extension: | |
10397 | ||
10398 | ptr-operator: | |
10399 | & cv-qualifier-seq [opt] | |
10400 | ||
10401 | Returns INDIRECT_REF if a pointer, or pointer-to-member, was | |
10402 | used. Returns ADDR_EXPR if a reference was used. In the | |
10403 | case of a pointer-to-member, *TYPE is filled in with the | |
10404 | TYPE containing the member. *CV_QUALIFIER_SEQ is filled in | |
10405 | with the cv-qualifier-seq, or NULL_TREE, if there are no | |
10406 | cv-qualifiers. Returns ERROR_MARK if an error occurred. */ | |
10407 | ||
10408 | static enum tree_code | |
94edc4ab NN |
10409 | cp_parser_ptr_operator (cp_parser* parser, |
10410 | tree* type, | |
10411 | tree* cv_qualifier_seq) | |
a723baf1 MM |
10412 | { |
10413 | enum tree_code code = ERROR_MARK; | |
10414 | cp_token *token; | |
10415 | ||
10416 | /* Assume that it's not a pointer-to-member. */ | |
10417 | *type = NULL_TREE; | |
10418 | /* And that there are no cv-qualifiers. */ | |
10419 | *cv_qualifier_seq = NULL_TREE; | |
10420 | ||
10421 | /* Peek at the next token. */ | |
10422 | token = cp_lexer_peek_token (parser->lexer); | |
10423 | /* If it's a `*' or `&' we have a pointer or reference. */ | |
10424 | if (token->type == CPP_MULT || token->type == CPP_AND) | |
10425 | { | |
10426 | /* Remember which ptr-operator we were processing. */ | |
10427 | code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF); | |
10428 | ||
10429 | /* Consume the `*' or `&'. */ | |
10430 | cp_lexer_consume_token (parser->lexer); | |
10431 | ||
10432 | /* A `*' can be followed by a cv-qualifier-seq, and so can a | |
10433 | `&', if we are allowing GNU extensions. (The only qualifier | |
10434 | that can legally appear after `&' is `restrict', but that is | |
10435 | enforced during semantic analysis. */ | |
10436 | if (code == INDIRECT_REF | |
10437 | || cp_parser_allow_gnu_extensions_p (parser)) | |
10438 | *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser); | |
10439 | } | |
10440 | else | |
10441 | { | |
10442 | /* Try the pointer-to-member case. */ | |
10443 | cp_parser_parse_tentatively (parser); | |
10444 | /* Look for the optional `::' operator. */ | |
10445 | cp_parser_global_scope_opt (parser, | |
10446 | /*current_scope_valid_p=*/false); | |
10447 | /* Look for the nested-name specifier. */ | |
10448 | cp_parser_nested_name_specifier (parser, | |
10449 | /*typename_keyword_p=*/false, | |
10450 | /*check_dependency_p=*/true, | |
a668c6ad MM |
10451 | /*type_p=*/false, |
10452 | /*is_declaration=*/false); | |
a723baf1 MM |
10453 | /* If we found it, and the next token is a `*', then we are |
10454 | indeed looking at a pointer-to-member operator. */ | |
10455 | if (!cp_parser_error_occurred (parser) | |
10456 | && cp_parser_require (parser, CPP_MULT, "`*'")) | |
10457 | { | |
10458 | /* The type of which the member is a member is given by the | |
10459 | current SCOPE. */ | |
10460 | *type = parser->scope; | |
10461 | /* The next name will not be qualified. */ | |
10462 | parser->scope = NULL_TREE; | |
10463 | parser->qualifying_scope = NULL_TREE; | |
10464 | parser->object_scope = NULL_TREE; | |
10465 | /* Indicate that the `*' operator was used. */ | |
10466 | code = INDIRECT_REF; | |
10467 | /* Look for the optional cv-qualifier-seq. */ | |
10468 | *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser); | |
10469 | } | |
10470 | /* If that didn't work we don't have a ptr-operator. */ | |
10471 | if (!cp_parser_parse_definitely (parser)) | |
10472 | cp_parser_error (parser, "expected ptr-operator"); | |
10473 | } | |
10474 | ||
10475 | return code; | |
10476 | } | |
10477 | ||
10478 | /* Parse an (optional) cv-qualifier-seq. | |
10479 | ||
10480 | cv-qualifier-seq: | |
10481 | cv-qualifier cv-qualifier-seq [opt] | |
10482 | ||
10483 | Returns a TREE_LIST. The TREE_VALUE of each node is the | |
10484 | representation of a cv-qualifier. */ | |
10485 | ||
10486 | static tree | |
94edc4ab | 10487 | cp_parser_cv_qualifier_seq_opt (cp_parser* parser) |
a723baf1 MM |
10488 | { |
10489 | tree cv_qualifiers = NULL_TREE; | |
10490 | ||
10491 | while (true) | |
10492 | { | |
10493 | tree cv_qualifier; | |
10494 | ||
10495 | /* Look for the next cv-qualifier. */ | |
10496 | cv_qualifier = cp_parser_cv_qualifier_opt (parser); | |
10497 | /* If we didn't find one, we're done. */ | |
10498 | if (!cv_qualifier) | |
10499 | break; | |
10500 | ||
10501 | /* Add this cv-qualifier to the list. */ | |
10502 | cv_qualifiers | |
10503 | = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers); | |
10504 | } | |
10505 | ||
10506 | /* We built up the list in reverse order. */ | |
10507 | return nreverse (cv_qualifiers); | |
10508 | } | |
10509 | ||
10510 | /* Parse an (optional) cv-qualifier. | |
10511 | ||
10512 | cv-qualifier: | |
10513 | const | |
10514 | volatile | |
10515 | ||
10516 | GNU Extension: | |
10517 | ||
10518 | cv-qualifier: | |
10519 | __restrict__ */ | |
10520 | ||
10521 | static tree | |
94edc4ab | 10522 | cp_parser_cv_qualifier_opt (cp_parser* parser) |
a723baf1 MM |
10523 | { |
10524 | cp_token *token; | |
10525 | tree cv_qualifier = NULL_TREE; | |
10526 | ||
10527 | /* Peek at the next token. */ | |
10528 | token = cp_lexer_peek_token (parser->lexer); | |
10529 | /* See if it's a cv-qualifier. */ | |
10530 | switch (token->keyword) | |
10531 | { | |
10532 | case RID_CONST: | |
10533 | case RID_VOLATILE: | |
10534 | case RID_RESTRICT: | |
10535 | /* Save the value of the token. */ | |
10536 | cv_qualifier = token->value; | |
10537 | /* Consume the token. */ | |
10538 | cp_lexer_consume_token (parser->lexer); | |
10539 | break; | |
10540 | ||
10541 | default: | |
10542 | break; | |
10543 | } | |
10544 | ||
10545 | return cv_qualifier; | |
10546 | } | |
10547 | ||
10548 | /* Parse a declarator-id. | |
10549 | ||
10550 | declarator-id: | |
10551 | id-expression | |
10552 | :: [opt] nested-name-specifier [opt] type-name | |
10553 | ||
10554 | In the `id-expression' case, the value returned is as for | |
10555 | cp_parser_id_expression if the id-expression was an unqualified-id. | |
10556 | If the id-expression was a qualified-id, then a SCOPE_REF is | |
10557 | returned. The first operand is the scope (either a NAMESPACE_DECL | |
10558 | or TREE_TYPE), but the second is still just a representation of an | |
10559 | unqualified-id. */ | |
10560 | ||
10561 | static tree | |
94edc4ab | 10562 | cp_parser_declarator_id (cp_parser* parser) |
a723baf1 MM |
10563 | { |
10564 | tree id_expression; | |
10565 | ||
10566 | /* The expression must be an id-expression. Assume that qualified | |
10567 | names are the names of types so that: | |
10568 | ||
10569 | template <class T> | |
10570 | int S<T>::R::i = 3; | |
10571 | ||
10572 | will work; we must treat `S<T>::R' as the name of a type. | |
10573 | Similarly, assume that qualified names are templates, where | |
10574 | required, so that: | |
10575 | ||
10576 | template <class T> | |
10577 | int S<T>::R<T>::i = 3; | |
10578 | ||
10579 | will work, too. */ | |
10580 | id_expression = cp_parser_id_expression (parser, | |
10581 | /*template_keyword_p=*/false, | |
10582 | /*check_dependency_p=*/false, | |
f3c2dfc6 MM |
10583 | /*template_p=*/NULL, |
10584 | /*declarator_p=*/true); | |
a723baf1 MM |
10585 | /* If the name was qualified, create a SCOPE_REF to represent |
10586 | that. */ | |
10587 | if (parser->scope) | |
ec20aa6c MM |
10588 | { |
10589 | id_expression = build_nt (SCOPE_REF, parser->scope, id_expression); | |
10590 | parser->scope = NULL_TREE; | |
10591 | } | |
a723baf1 MM |
10592 | |
10593 | return id_expression; | |
10594 | } | |
10595 | ||
10596 | /* Parse a type-id. | |
10597 | ||
10598 | type-id: | |
10599 | type-specifier-seq abstract-declarator [opt] | |
10600 | ||
10601 | Returns the TYPE specified. */ | |
10602 | ||
10603 | static tree | |
94edc4ab | 10604 | cp_parser_type_id (cp_parser* parser) |
a723baf1 MM |
10605 | { |
10606 | tree type_specifier_seq; | |
10607 | tree abstract_declarator; | |
10608 | ||
10609 | /* Parse the type-specifier-seq. */ | |
10610 | type_specifier_seq | |
10611 | = cp_parser_type_specifier_seq (parser); | |
10612 | if (type_specifier_seq == error_mark_node) | |
10613 | return error_mark_node; | |
10614 | ||
10615 | /* There might or might not be an abstract declarator. */ | |
10616 | cp_parser_parse_tentatively (parser); | |
10617 | /* Look for the declarator. */ | |
10618 | abstract_declarator | |
4bb8ca28 MM |
10619 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL, |
10620 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
10621 | /* Check to see if there really was a declarator. */ |
10622 | if (!cp_parser_parse_definitely (parser)) | |
10623 | abstract_declarator = NULL_TREE; | |
10624 | ||
10625 | return groktypename (build_tree_list (type_specifier_seq, | |
10626 | abstract_declarator)); | |
10627 | } | |
10628 | ||
10629 | /* Parse a type-specifier-seq. | |
10630 | ||
10631 | type-specifier-seq: | |
10632 | type-specifier type-specifier-seq [opt] | |
10633 | ||
10634 | GNU extension: | |
10635 | ||
10636 | type-specifier-seq: | |
10637 | attributes type-specifier-seq [opt] | |
10638 | ||
10639 | Returns a TREE_LIST. Either the TREE_VALUE of each node is a | |
10640 | type-specifier, or the TREE_PURPOSE is a list of attributes. */ | |
10641 | ||
10642 | static tree | |
94edc4ab | 10643 | cp_parser_type_specifier_seq (cp_parser* parser) |
a723baf1 MM |
10644 | { |
10645 | bool seen_type_specifier = false; | |
10646 | tree type_specifier_seq = NULL_TREE; | |
10647 | ||
10648 | /* Parse the type-specifiers and attributes. */ | |
10649 | while (true) | |
10650 | { | |
10651 | tree type_specifier; | |
10652 | ||
10653 | /* Check for attributes first. */ | |
10654 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) | |
10655 | { | |
10656 | type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser), | |
10657 | NULL_TREE, | |
10658 | type_specifier_seq); | |
10659 | continue; | |
10660 | } | |
10661 | ||
10662 | /* After the first type-specifier, others are optional. */ | |
10663 | if (seen_type_specifier) | |
10664 | cp_parser_parse_tentatively (parser); | |
10665 | /* Look for the type-specifier. */ | |
10666 | type_specifier = cp_parser_type_specifier (parser, | |
10667 | CP_PARSER_FLAGS_NONE, | |
10668 | /*is_friend=*/false, | |
10669 | /*is_declaration=*/false, | |
10670 | NULL, | |
10671 | NULL); | |
10672 | /* If the first type-specifier could not be found, this is not a | |
10673 | type-specifier-seq at all. */ | |
10674 | if (!seen_type_specifier && type_specifier == error_mark_node) | |
10675 | return error_mark_node; | |
10676 | /* If subsequent type-specifiers could not be found, the | |
10677 | type-specifier-seq is complete. */ | |
10678 | else if (seen_type_specifier && !cp_parser_parse_definitely (parser)) | |
10679 | break; | |
10680 | ||
10681 | /* Add the new type-specifier to the list. */ | |
10682 | type_specifier_seq | |
10683 | = tree_cons (NULL_TREE, type_specifier, type_specifier_seq); | |
10684 | seen_type_specifier = true; | |
10685 | } | |
10686 | ||
10687 | /* We built up the list in reverse order. */ | |
10688 | return nreverse (type_specifier_seq); | |
10689 | } | |
10690 | ||
10691 | /* Parse a parameter-declaration-clause. | |
10692 | ||
10693 | parameter-declaration-clause: | |
10694 | parameter-declaration-list [opt] ... [opt] | |
10695 | parameter-declaration-list , ... | |
10696 | ||
10697 | Returns a representation for the parameter declarations. Each node | |
10698 | is a TREE_LIST. (See cp_parser_parameter_declaration for the exact | |
10699 | representation.) If the parameter-declaration-clause ends with an | |
10700 | ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the | |
10701 | list. A return value of NULL_TREE indicates a | |
10702 | parameter-declaration-clause consisting only of an ellipsis. */ | |
10703 | ||
10704 | static tree | |
94edc4ab | 10705 | cp_parser_parameter_declaration_clause (cp_parser* parser) |
a723baf1 MM |
10706 | { |
10707 | tree parameters; | |
10708 | cp_token *token; | |
10709 | bool ellipsis_p; | |
10710 | ||
10711 | /* Peek at the next token. */ | |
10712 | token = cp_lexer_peek_token (parser->lexer); | |
10713 | /* Check for trivial parameter-declaration-clauses. */ | |
10714 | if (token->type == CPP_ELLIPSIS) | |
10715 | { | |
10716 | /* Consume the `...' token. */ | |
10717 | cp_lexer_consume_token (parser->lexer); | |
10718 | return NULL_TREE; | |
10719 | } | |
10720 | else if (token->type == CPP_CLOSE_PAREN) | |
10721 | /* There are no parameters. */ | |
c73aecdf DE |
10722 | { |
10723 | #ifndef NO_IMPLICIT_EXTERN_C | |
10724 | if (in_system_header && current_class_type == NULL | |
10725 | && current_lang_name == lang_name_c) | |
10726 | return NULL_TREE; | |
10727 | else | |
10728 | #endif | |
10729 | return void_list_node; | |
10730 | } | |
a723baf1 MM |
10731 | /* Check for `(void)', too, which is a special case. */ |
10732 | else if (token->keyword == RID_VOID | |
10733 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
10734 | == CPP_CLOSE_PAREN)) | |
10735 | { | |
10736 | /* Consume the `void' token. */ | |
10737 | cp_lexer_consume_token (parser->lexer); | |
10738 | /* There are no parameters. */ | |
10739 | return void_list_node; | |
10740 | } | |
10741 | ||
10742 | /* Parse the parameter-declaration-list. */ | |
10743 | parameters = cp_parser_parameter_declaration_list (parser); | |
10744 | /* If a parse error occurred while parsing the | |
10745 | parameter-declaration-list, then the entire | |
10746 | parameter-declaration-clause is erroneous. */ | |
10747 | if (parameters == error_mark_node) | |
10748 | return error_mark_node; | |
10749 | ||
10750 | /* Peek at the next token. */ | |
10751 | token = cp_lexer_peek_token (parser->lexer); | |
10752 | /* If it's a `,', the clause should terminate with an ellipsis. */ | |
10753 | if (token->type == CPP_COMMA) | |
10754 | { | |
10755 | /* Consume the `,'. */ | |
10756 | cp_lexer_consume_token (parser->lexer); | |
10757 | /* Expect an ellipsis. */ | |
10758 | ellipsis_p | |
10759 | = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL); | |
10760 | } | |
10761 | /* It might also be `...' if the optional trailing `,' was | |
10762 | omitted. */ | |
10763 | else if (token->type == CPP_ELLIPSIS) | |
10764 | { | |
10765 | /* Consume the `...' token. */ | |
10766 | cp_lexer_consume_token (parser->lexer); | |
10767 | /* And remember that we saw it. */ | |
10768 | ellipsis_p = true; | |
10769 | } | |
10770 | else | |
10771 | ellipsis_p = false; | |
10772 | ||
10773 | /* Finish the parameter list. */ | |
10774 | return finish_parmlist (parameters, ellipsis_p); | |
10775 | } | |
10776 | ||
10777 | /* Parse a parameter-declaration-list. | |
10778 | ||
10779 | parameter-declaration-list: | |
10780 | parameter-declaration | |
10781 | parameter-declaration-list , parameter-declaration | |
10782 | ||
10783 | Returns a representation of the parameter-declaration-list, as for | |
10784 | cp_parser_parameter_declaration_clause. However, the | |
10785 | `void_list_node' is never appended to the list. */ | |
10786 | ||
10787 | static tree | |
94edc4ab | 10788 | cp_parser_parameter_declaration_list (cp_parser* parser) |
a723baf1 MM |
10789 | { |
10790 | tree parameters = NULL_TREE; | |
10791 | ||
10792 | /* Look for more parameters. */ | |
10793 | while (true) | |
10794 | { | |
10795 | tree parameter; | |
4bb8ca28 | 10796 | bool parenthesized_p; |
a723baf1 MM |
10797 | /* Parse the parameter. */ |
10798 | parameter | |
4bb8ca28 MM |
10799 | = cp_parser_parameter_declaration (parser, |
10800 | /*template_parm_p=*/false, | |
10801 | &parenthesized_p); | |
ec194454 | 10802 | |
34cd5ae7 | 10803 | /* If a parse error occurred parsing the parameter declaration, |
a723baf1 MM |
10804 | then the entire parameter-declaration-list is erroneous. */ |
10805 | if (parameter == error_mark_node) | |
10806 | { | |
10807 | parameters = error_mark_node; | |
10808 | break; | |
10809 | } | |
10810 | /* Add the new parameter to the list. */ | |
10811 | TREE_CHAIN (parameter) = parameters; | |
10812 | parameters = parameter; | |
10813 | ||
10814 | /* Peek at the next token. */ | |
10815 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN) | |
10816 | || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
10817 | /* The parameter-declaration-list is complete. */ | |
10818 | break; | |
10819 | else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
10820 | { | |
10821 | cp_token *token; | |
10822 | ||
10823 | /* Peek at the next token. */ | |
10824 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
10825 | /* If it's an ellipsis, then the list is complete. */ | |
10826 | if (token->type == CPP_ELLIPSIS) | |
10827 | break; | |
10828 | /* Otherwise, there must be more parameters. Consume the | |
10829 | `,'. */ | |
10830 | cp_lexer_consume_token (parser->lexer); | |
4bb8ca28 MM |
10831 | /* When parsing something like: |
10832 | ||
10833 | int i(float f, double d) | |
10834 | ||
10835 | we can tell after seeing the declaration for "f" that we | |
10836 | are not looking at an initialization of a variable "i", | |
10837 | but rather at the declaration of a function "i". | |
10838 | ||
10839 | Due to the fact that the parsing of template arguments | |
10840 | (as specified to a template-id) requires backtracking we | |
10841 | cannot use this technique when inside a template argument | |
10842 | list. */ | |
10843 | if (!parser->in_template_argument_list_p | |
10844 | && cp_parser_parsing_tentatively (parser) | |
10845 | && !cp_parser_committed_to_tentative_parse (parser) | |
10846 | /* However, a parameter-declaration of the form | |
10847 | "foat(f)" (which is a valid declaration of a | |
10848 | parameter "f") can also be interpreted as an | |
10849 | expression (the conversion of "f" to "float"). */ | |
10850 | && !parenthesized_p) | |
10851 | cp_parser_commit_to_tentative_parse (parser); | |
a723baf1 MM |
10852 | } |
10853 | else | |
10854 | { | |
10855 | cp_parser_error (parser, "expected `,' or `...'"); | |
4bb8ca28 MM |
10856 | if (!cp_parser_parsing_tentatively (parser) |
10857 | || cp_parser_committed_to_tentative_parse (parser)) | |
10858 | cp_parser_skip_to_closing_parenthesis (parser, | |
10859 | /*recovering=*/true, | |
5c832178 | 10860 | /*or_comma=*/false, |
4bb8ca28 | 10861 | /*consume_paren=*/false); |
a723baf1 MM |
10862 | break; |
10863 | } | |
10864 | } | |
10865 | ||
10866 | /* We built up the list in reverse order; straighten it out now. */ | |
10867 | return nreverse (parameters); | |
10868 | } | |
10869 | ||
10870 | /* Parse a parameter declaration. | |
10871 | ||
10872 | parameter-declaration: | |
10873 | decl-specifier-seq declarator | |
10874 | decl-specifier-seq declarator = assignment-expression | |
10875 | decl-specifier-seq abstract-declarator [opt] | |
10876 | decl-specifier-seq abstract-declarator [opt] = assignment-expression | |
10877 | ||
ec194454 MM |
10878 | If TEMPLATE_PARM_P is TRUE, then this parameter-declaration |
10879 | declares a template parameter. (In that case, a non-nested `>' | |
10880 | token encountered during the parsing of the assignment-expression | |
10881 | is not interpreted as a greater-than operator.) | |
a723baf1 MM |
10882 | |
10883 | Returns a TREE_LIST representing the parameter-declaration. The | |
4bb8ca28 MM |
10884 | TREE_PURPOSE is the default argument expression, or NULL_TREE if |
10885 | there is no default argument. The TREE_VALUE is a representation | |
10886 | of the decl-specifier-seq and declarator. In particular, the | |
10887 | TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the | |
10888 | decl-specifier-seq and whose TREE_VALUE represents the declarator. | |
10889 | If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff | |
10890 | the declarator is of the form "(p)". */ | |
a723baf1 MM |
10891 | |
10892 | static tree | |
ec194454 | 10893 | cp_parser_parameter_declaration (cp_parser *parser, |
4bb8ca28 MM |
10894 | bool template_parm_p, |
10895 | bool *parenthesized_p) | |
a723baf1 | 10896 | { |
560ad596 | 10897 | int declares_class_or_enum; |
ec194454 | 10898 | bool greater_than_is_operator_p; |
a723baf1 MM |
10899 | tree decl_specifiers; |
10900 | tree attributes; | |
10901 | tree declarator; | |
10902 | tree default_argument; | |
10903 | tree parameter; | |
10904 | cp_token *token; | |
10905 | const char *saved_message; | |
10906 | ||
ec194454 MM |
10907 | /* In a template parameter, `>' is not an operator. |
10908 | ||
10909 | [temp.param] | |
10910 | ||
10911 | When parsing a default template-argument for a non-type | |
10912 | template-parameter, the first non-nested `>' is taken as the end | |
10913 | of the template parameter-list rather than a greater-than | |
10914 | operator. */ | |
10915 | greater_than_is_operator_p = !template_parm_p; | |
10916 | ||
a723baf1 MM |
10917 | /* Type definitions may not appear in parameter types. */ |
10918 | saved_message = parser->type_definition_forbidden_message; | |
10919 | parser->type_definition_forbidden_message | |
10920 | = "types may not be defined in parameter types"; | |
10921 | ||
10922 | /* Parse the declaration-specifiers. */ | |
10923 | decl_specifiers | |
10924 | = cp_parser_decl_specifier_seq (parser, | |
10925 | CP_PARSER_FLAGS_NONE, | |
10926 | &attributes, | |
10927 | &declares_class_or_enum); | |
10928 | /* If an error occurred, there's no reason to attempt to parse the | |
10929 | rest of the declaration. */ | |
10930 | if (cp_parser_error_occurred (parser)) | |
10931 | { | |
10932 | parser->type_definition_forbidden_message = saved_message; | |
10933 | return error_mark_node; | |
10934 | } | |
10935 | ||
10936 | /* Peek at the next token. */ | |
10937 | token = cp_lexer_peek_token (parser->lexer); | |
10938 | /* If the next token is a `)', `,', `=', `>', or `...', then there | |
10939 | is no declarator. */ | |
10940 | if (token->type == CPP_CLOSE_PAREN | |
10941 | || token->type == CPP_COMMA | |
10942 | || token->type == CPP_EQ | |
10943 | || token->type == CPP_ELLIPSIS | |
10944 | || token->type == CPP_GREATER) | |
4bb8ca28 MM |
10945 | { |
10946 | declarator = NULL_TREE; | |
10947 | if (parenthesized_p) | |
10948 | *parenthesized_p = false; | |
10949 | } | |
a723baf1 MM |
10950 | /* Otherwise, there should be a declarator. */ |
10951 | else | |
10952 | { | |
10953 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
10954 | parser->default_arg_ok_p = false; | |
10955 | ||
5c832178 MM |
10956 | /* After seeing a decl-specifier-seq, if the next token is not a |
10957 | "(", there is no possibility that the code is a valid | |
10958 | expression initializer. Therefore, if parsing tentatively, | |
10959 | we commit at this point. */ | |
10960 | if (!parser->in_template_argument_list_p | |
10961 | && cp_parser_parsing_tentatively (parser) | |
10962 | && !cp_parser_committed_to_tentative_parse (parser) | |
10963 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) | |
10964 | cp_parser_commit_to_tentative_parse (parser); | |
10965 | /* Parse the declarator. */ | |
a723baf1 | 10966 | declarator = cp_parser_declarator (parser, |
62b8a44e | 10967 | CP_PARSER_DECLARATOR_EITHER, |
4bb8ca28 MM |
10968 | /*ctor_dtor_or_conv_p=*/NULL, |
10969 | parenthesized_p); | |
a723baf1 | 10970 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
4971227d MM |
10971 | /* After the declarator, allow more attributes. */ |
10972 | attributes = chainon (attributes, cp_parser_attributes_opt (parser)); | |
a723baf1 MM |
10973 | } |
10974 | ||
62b8a44e | 10975 | /* The restriction on defining new types applies only to the type |
a723baf1 MM |
10976 | of the parameter, not to the default argument. */ |
10977 | parser->type_definition_forbidden_message = saved_message; | |
10978 | ||
10979 | /* If the next token is `=', then process a default argument. */ | |
10980 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
10981 | { | |
10982 | bool saved_greater_than_is_operator_p; | |
10983 | /* Consume the `='. */ | |
10984 | cp_lexer_consume_token (parser->lexer); | |
10985 | ||
10986 | /* If we are defining a class, then the tokens that make up the | |
10987 | default argument must be saved and processed later. */ | |
ec194454 MM |
10988 | if (!template_parm_p && at_class_scope_p () |
10989 | && TYPE_BEING_DEFINED (current_class_type)) | |
a723baf1 MM |
10990 | { |
10991 | unsigned depth = 0; | |
10992 | ||
10993 | /* Create a DEFAULT_ARG to represented the unparsed default | |
10994 | argument. */ | |
10995 | default_argument = make_node (DEFAULT_ARG); | |
10996 | DEFARG_TOKENS (default_argument) = cp_token_cache_new (); | |
10997 | ||
10998 | /* Add tokens until we have processed the entire default | |
10999 | argument. */ | |
11000 | while (true) | |
11001 | { | |
11002 | bool done = false; | |
11003 | cp_token *token; | |
11004 | ||
11005 | /* Peek at the next token. */ | |
11006 | token = cp_lexer_peek_token (parser->lexer); | |
11007 | /* What we do depends on what token we have. */ | |
11008 | switch (token->type) | |
11009 | { | |
11010 | /* In valid code, a default argument must be | |
11011 | immediately followed by a `,' `)', or `...'. */ | |
11012 | case CPP_COMMA: | |
11013 | case CPP_CLOSE_PAREN: | |
11014 | case CPP_ELLIPSIS: | |
11015 | /* If we run into a non-nested `;', `}', or `]', | |
11016 | then the code is invalid -- but the default | |
11017 | argument is certainly over. */ | |
11018 | case CPP_SEMICOLON: | |
11019 | case CPP_CLOSE_BRACE: | |
11020 | case CPP_CLOSE_SQUARE: | |
11021 | if (depth == 0) | |
11022 | done = true; | |
11023 | /* Update DEPTH, if necessary. */ | |
11024 | else if (token->type == CPP_CLOSE_PAREN | |
11025 | || token->type == CPP_CLOSE_BRACE | |
11026 | || token->type == CPP_CLOSE_SQUARE) | |
11027 | --depth; | |
11028 | break; | |
11029 | ||
11030 | case CPP_OPEN_PAREN: | |
11031 | case CPP_OPEN_SQUARE: | |
11032 | case CPP_OPEN_BRACE: | |
11033 | ++depth; | |
11034 | break; | |
11035 | ||
11036 | case CPP_GREATER: | |
11037 | /* If we see a non-nested `>', and `>' is not an | |
11038 | operator, then it marks the end of the default | |
11039 | argument. */ | |
11040 | if (!depth && !greater_than_is_operator_p) | |
11041 | done = true; | |
11042 | break; | |
11043 | ||
11044 | /* If we run out of tokens, issue an error message. */ | |
11045 | case CPP_EOF: | |
11046 | error ("file ends in default argument"); | |
11047 | done = true; | |
11048 | break; | |
11049 | ||
11050 | case CPP_NAME: | |
11051 | case CPP_SCOPE: | |
11052 | /* In these cases, we should look for template-ids. | |
11053 | For example, if the default argument is | |
11054 | `X<int, double>()', we need to do name lookup to | |
11055 | figure out whether or not `X' is a template; if | |
34cd5ae7 | 11056 | so, the `,' does not end the default argument. |
a723baf1 MM |
11057 | |
11058 | That is not yet done. */ | |
11059 | break; | |
11060 | ||
11061 | default: | |
11062 | break; | |
11063 | } | |
11064 | ||
11065 | /* If we've reached the end, stop. */ | |
11066 | if (done) | |
11067 | break; | |
11068 | ||
11069 | /* Add the token to the token block. */ | |
11070 | token = cp_lexer_consume_token (parser->lexer); | |
11071 | cp_token_cache_push_token (DEFARG_TOKENS (default_argument), | |
11072 | token); | |
11073 | } | |
11074 | } | |
11075 | /* Outside of a class definition, we can just parse the | |
11076 | assignment-expression. */ | |
11077 | else | |
11078 | { | |
11079 | bool saved_local_variables_forbidden_p; | |
11080 | ||
11081 | /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is | |
11082 | set correctly. */ | |
11083 | saved_greater_than_is_operator_p | |
11084 | = parser->greater_than_is_operator_p; | |
11085 | parser->greater_than_is_operator_p = greater_than_is_operator_p; | |
11086 | /* Local variable names (and the `this' keyword) may not | |
11087 | appear in a default argument. */ | |
11088 | saved_local_variables_forbidden_p | |
11089 | = parser->local_variables_forbidden_p; | |
11090 | parser->local_variables_forbidden_p = true; | |
11091 | /* Parse the assignment-expression. */ | |
11092 | default_argument = cp_parser_assignment_expression (parser); | |
11093 | /* Restore saved state. */ | |
11094 | parser->greater_than_is_operator_p | |
11095 | = saved_greater_than_is_operator_p; | |
11096 | parser->local_variables_forbidden_p | |
11097 | = saved_local_variables_forbidden_p; | |
11098 | } | |
11099 | if (!parser->default_arg_ok_p) | |
11100 | { | |
c67d36d0 NS |
11101 | if (!flag_pedantic_errors) |
11102 | warning ("deprecated use of default argument for parameter of non-function"); | |
11103 | else | |
11104 | { | |
11105 | error ("default arguments are only permitted for function parameters"); | |
11106 | default_argument = NULL_TREE; | |
11107 | } | |
a723baf1 MM |
11108 | } |
11109 | } | |
11110 | else | |
11111 | default_argument = NULL_TREE; | |
11112 | ||
11113 | /* Create the representation of the parameter. */ | |
11114 | if (attributes) | |
11115 | decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers); | |
11116 | parameter = build_tree_list (default_argument, | |
11117 | build_tree_list (decl_specifiers, | |
11118 | declarator)); | |
11119 | ||
11120 | return parameter; | |
11121 | } | |
11122 | ||
a723baf1 MM |
11123 | /* Parse a function-body. |
11124 | ||
11125 | function-body: | |
11126 | compound_statement */ | |
11127 | ||
11128 | static void | |
11129 | cp_parser_function_body (cp_parser *parser) | |
11130 | { | |
a5bcc582 | 11131 | cp_parser_compound_statement (parser, false); |
a723baf1 MM |
11132 | } |
11133 | ||
11134 | /* Parse a ctor-initializer-opt followed by a function-body. Return | |
11135 | true if a ctor-initializer was present. */ | |
11136 | ||
11137 | static bool | |
11138 | cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser) | |
11139 | { | |
11140 | tree body; | |
11141 | bool ctor_initializer_p; | |
11142 | ||
11143 | /* Begin the function body. */ | |
11144 | body = begin_function_body (); | |
11145 | /* Parse the optional ctor-initializer. */ | |
11146 | ctor_initializer_p = cp_parser_ctor_initializer_opt (parser); | |
11147 | /* Parse the function-body. */ | |
11148 | cp_parser_function_body (parser); | |
11149 | /* Finish the function body. */ | |
11150 | finish_function_body (body); | |
11151 | ||
11152 | return ctor_initializer_p; | |
11153 | } | |
11154 | ||
11155 | /* Parse an initializer. | |
11156 | ||
11157 | initializer: | |
11158 | = initializer-clause | |
11159 | ( expression-list ) | |
11160 | ||
11161 | Returns a expression representing the initializer. If no | |
11162 | initializer is present, NULL_TREE is returned. | |
11163 | ||
11164 | *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )' | |
11165 | production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is | |
39703eb9 MM |
11166 | set to FALSE if there is no initializer present. If there is an |
11167 | initializer, and it is not a constant-expression, *NON_CONSTANT_P | |
11168 | is set to true; otherwise it is set to false. */ | |
a723baf1 MM |
11169 | |
11170 | static tree | |
39703eb9 MM |
11171 | cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init, |
11172 | bool* non_constant_p) | |
a723baf1 MM |
11173 | { |
11174 | cp_token *token; | |
11175 | tree init; | |
11176 | ||
11177 | /* Peek at the next token. */ | |
11178 | token = cp_lexer_peek_token (parser->lexer); | |
11179 | ||
11180 | /* Let our caller know whether or not this initializer was | |
11181 | parenthesized. */ | |
11182 | *is_parenthesized_init = (token->type == CPP_OPEN_PAREN); | |
39703eb9 MM |
11183 | /* Assume that the initializer is constant. */ |
11184 | *non_constant_p = false; | |
a723baf1 MM |
11185 | |
11186 | if (token->type == CPP_EQ) | |
11187 | { | |
11188 | /* Consume the `='. */ | |
11189 | cp_lexer_consume_token (parser->lexer); | |
11190 | /* Parse the initializer-clause. */ | |
39703eb9 | 11191 | init = cp_parser_initializer_clause (parser, non_constant_p); |
a723baf1 MM |
11192 | } |
11193 | else if (token->type == CPP_OPEN_PAREN) | |
39703eb9 MM |
11194 | init = cp_parser_parenthesized_expression_list (parser, false, |
11195 | non_constant_p); | |
a723baf1 MM |
11196 | else |
11197 | { | |
11198 | /* Anything else is an error. */ | |
11199 | cp_parser_error (parser, "expected initializer"); | |
11200 | init = error_mark_node; | |
11201 | } | |
11202 | ||
11203 | return init; | |
11204 | } | |
11205 | ||
11206 | /* Parse an initializer-clause. | |
11207 | ||
11208 | initializer-clause: | |
11209 | assignment-expression | |
11210 | { initializer-list , [opt] } | |
11211 | { } | |
11212 | ||
11213 | Returns an expression representing the initializer. | |
11214 | ||
11215 | If the `assignment-expression' production is used the value | |
34cd5ae7 | 11216 | returned is simply a representation for the expression. |
a723baf1 MM |
11217 | |
11218 | Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be | |
11219 | the elements of the initializer-list (or NULL_TREE, if the last | |
11220 | production is used). The TREE_TYPE for the CONSTRUCTOR will be | |
11221 | NULL_TREE. There is no way to detect whether or not the optional | |
39703eb9 MM |
11222 | trailing `,' was provided. NON_CONSTANT_P is as for |
11223 | cp_parser_initializer. */ | |
a723baf1 MM |
11224 | |
11225 | static tree | |
39703eb9 | 11226 | cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p) |
a723baf1 MM |
11227 | { |
11228 | tree initializer; | |
11229 | ||
11230 | /* If it is not a `{', then we are looking at an | |
11231 | assignment-expression. */ | |
11232 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
39703eb9 MM |
11233 | initializer |
11234 | = cp_parser_constant_expression (parser, | |
11235 | /*allow_non_constant_p=*/true, | |
11236 | non_constant_p); | |
a723baf1 MM |
11237 | else |
11238 | { | |
11239 | /* Consume the `{' token. */ | |
11240 | cp_lexer_consume_token (parser->lexer); | |
11241 | /* Create a CONSTRUCTOR to represent the braced-initializer. */ | |
11242 | initializer = make_node (CONSTRUCTOR); | |
11243 | /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be | |
11244 | necessary, but check_initializer depends upon it, for | |
11245 | now. */ | |
11246 | TREE_HAS_CONSTRUCTOR (initializer) = 1; | |
11247 | /* If it's not a `}', then there is a non-trivial initializer. */ | |
11248 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)) | |
11249 | { | |
11250 | /* Parse the initializer list. */ | |
11251 | CONSTRUCTOR_ELTS (initializer) | |
39703eb9 | 11252 | = cp_parser_initializer_list (parser, non_constant_p); |
a723baf1 MM |
11253 | /* A trailing `,' token is allowed. */ |
11254 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
11255 | cp_lexer_consume_token (parser->lexer); | |
11256 | } | |
a723baf1 MM |
11257 | /* Now, there should be a trailing `}'. */ |
11258 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
11259 | } | |
11260 | ||
11261 | return initializer; | |
11262 | } | |
11263 | ||
11264 | /* Parse an initializer-list. | |
11265 | ||
11266 | initializer-list: | |
11267 | initializer-clause | |
11268 | initializer-list , initializer-clause | |
11269 | ||
11270 | GNU Extension: | |
11271 | ||
11272 | initializer-list: | |
11273 | identifier : initializer-clause | |
11274 | initializer-list, identifier : initializer-clause | |
11275 | ||
11276 | Returns a TREE_LIST. The TREE_VALUE of each node is an expression | |
11277 | for the initializer. If the TREE_PURPOSE is non-NULL, it is the | |
39703eb9 MM |
11278 | IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is |
11279 | as for cp_parser_initializer. */ | |
a723baf1 MM |
11280 | |
11281 | static tree | |
39703eb9 | 11282 | cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p) |
a723baf1 MM |
11283 | { |
11284 | tree initializers = NULL_TREE; | |
11285 | ||
39703eb9 MM |
11286 | /* Assume all of the expressions are constant. */ |
11287 | *non_constant_p = false; | |
11288 | ||
a723baf1 MM |
11289 | /* Parse the rest of the list. */ |
11290 | while (true) | |
11291 | { | |
11292 | cp_token *token; | |
11293 | tree identifier; | |
11294 | tree initializer; | |
39703eb9 MM |
11295 | bool clause_non_constant_p; |
11296 | ||
a723baf1 MM |
11297 | /* If the next token is an identifier and the following one is a |
11298 | colon, we are looking at the GNU designated-initializer | |
11299 | syntax. */ | |
11300 | if (cp_parser_allow_gnu_extensions_p (parser) | |
11301 | && cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
11302 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON) | |
11303 | { | |
11304 | /* Consume the identifier. */ | |
11305 | identifier = cp_lexer_consume_token (parser->lexer)->value; | |
11306 | /* Consume the `:'. */ | |
11307 | cp_lexer_consume_token (parser->lexer); | |
11308 | } | |
11309 | else | |
11310 | identifier = NULL_TREE; | |
11311 | ||
11312 | /* Parse the initializer. */ | |
39703eb9 MM |
11313 | initializer = cp_parser_initializer_clause (parser, |
11314 | &clause_non_constant_p); | |
11315 | /* If any clause is non-constant, so is the entire initializer. */ | |
11316 | if (clause_non_constant_p) | |
11317 | *non_constant_p = true; | |
a723baf1 MM |
11318 | /* Add it to the list. */ |
11319 | initializers = tree_cons (identifier, initializer, initializers); | |
11320 | ||
11321 | /* If the next token is not a comma, we have reached the end of | |
11322 | the list. */ | |
11323 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
11324 | break; | |
11325 | ||
11326 | /* Peek at the next token. */ | |
11327 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
11328 | /* If the next token is a `}', then we're still done. An | |
11329 | initializer-clause can have a trailing `,' after the | |
11330 | initializer-list and before the closing `}'. */ | |
11331 | if (token->type == CPP_CLOSE_BRACE) | |
11332 | break; | |
11333 | ||
11334 | /* Consume the `,' token. */ | |
11335 | cp_lexer_consume_token (parser->lexer); | |
11336 | } | |
11337 | ||
11338 | /* The initializers were built up in reverse order, so we need to | |
11339 | reverse them now. */ | |
11340 | return nreverse (initializers); | |
11341 | } | |
11342 | ||
11343 | /* Classes [gram.class] */ | |
11344 | ||
11345 | /* Parse a class-name. | |
11346 | ||
11347 | class-name: | |
11348 | identifier | |
11349 | template-id | |
11350 | ||
11351 | TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used | |
11352 | to indicate that names looked up in dependent types should be | |
11353 | assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template' | |
11354 | keyword has been used to indicate that the name that appears next | |
11355 | is a template. TYPE_P is true iff the next name should be treated | |
11356 | as class-name, even if it is declared to be some other kind of name | |
8d241e0b KL |
11357 | as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in |
11358 | dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class | |
11359 | being defined in a class-head. | |
a723baf1 MM |
11360 | |
11361 | Returns the TYPE_DECL representing the class. */ | |
11362 | ||
11363 | static tree | |
11364 | cp_parser_class_name (cp_parser *parser, | |
11365 | bool typename_keyword_p, | |
11366 | bool template_keyword_p, | |
11367 | bool type_p, | |
a723baf1 | 11368 | bool check_dependency_p, |
a668c6ad MM |
11369 | bool class_head_p, |
11370 | bool is_declaration) | |
a723baf1 MM |
11371 | { |
11372 | tree decl; | |
11373 | tree scope; | |
11374 | bool typename_p; | |
e5976695 MM |
11375 | cp_token *token; |
11376 | ||
11377 | /* All class-names start with an identifier. */ | |
11378 | token = cp_lexer_peek_token (parser->lexer); | |
11379 | if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID) | |
11380 | { | |
11381 | cp_parser_error (parser, "expected class-name"); | |
11382 | return error_mark_node; | |
11383 | } | |
11384 | ||
a723baf1 MM |
11385 | /* PARSER->SCOPE can be cleared when parsing the template-arguments |
11386 | to a template-id, so we save it here. */ | |
11387 | scope = parser->scope; | |
3adee96c KL |
11388 | if (scope == error_mark_node) |
11389 | return error_mark_node; | |
11390 | ||
a723baf1 MM |
11391 | /* Any name names a type if we're following the `typename' keyword |
11392 | in a qualified name where the enclosing scope is type-dependent. */ | |
11393 | typename_p = (typename_keyword_p && scope && TYPE_P (scope) | |
1fb3244a | 11394 | && dependent_type_p (scope)); |
e5976695 MM |
11395 | /* Handle the common case (an identifier, but not a template-id) |
11396 | efficiently. */ | |
11397 | if (token->type == CPP_NAME | |
11398 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS) | |
a723baf1 | 11399 | { |
a723baf1 MM |
11400 | tree identifier; |
11401 | ||
11402 | /* Look for the identifier. */ | |
11403 | identifier = cp_parser_identifier (parser); | |
11404 | /* If the next token isn't an identifier, we are certainly not | |
11405 | looking at a class-name. */ | |
11406 | if (identifier == error_mark_node) | |
11407 | decl = error_mark_node; | |
11408 | /* If we know this is a type-name, there's no need to look it | |
11409 | up. */ | |
11410 | else if (typename_p) | |
11411 | decl = identifier; | |
11412 | else | |
11413 | { | |
11414 | /* If the next token is a `::', then the name must be a type | |
11415 | name. | |
11416 | ||
11417 | [basic.lookup.qual] | |
11418 | ||
11419 | During the lookup for a name preceding the :: scope | |
11420 | resolution operator, object, function, and enumerator | |
11421 | names are ignored. */ | |
11422 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
11423 | type_p = true; | |
11424 | /* Look up the name. */ | |
11425 | decl = cp_parser_lookup_name (parser, identifier, | |
a723baf1 | 11426 | type_p, |
b0bc6e8e | 11427 | /*is_template=*/false, |
eea9800f | 11428 | /*is_namespace=*/false, |
a723baf1 MM |
11429 | check_dependency_p); |
11430 | } | |
11431 | } | |
e5976695 MM |
11432 | else |
11433 | { | |
11434 | /* Try a template-id. */ | |
11435 | decl = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
11436 | check_dependency_p, |
11437 | is_declaration); | |
e5976695 MM |
11438 | if (decl == error_mark_node) |
11439 | return error_mark_node; | |
11440 | } | |
a723baf1 MM |
11441 | |
11442 | decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p); | |
11443 | ||
11444 | /* If this is a typename, create a TYPENAME_TYPE. */ | |
11445 | if (typename_p && decl != error_mark_node) | |
11446 | decl = TYPE_NAME (make_typename_type (scope, decl, | |
11447 | /*complain=*/1)); | |
11448 | ||
11449 | /* Check to see that it is really the name of a class. */ | |
11450 | if (TREE_CODE (decl) == TEMPLATE_ID_EXPR | |
11451 | && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE | |
11452 | && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
11453 | /* Situations like this: | |
11454 | ||
11455 | template <typename T> struct A { | |
11456 | typename T::template X<int>::I i; | |
11457 | }; | |
11458 | ||
11459 | are problematic. Is `T::template X<int>' a class-name? The | |
11460 | standard does not seem to be definitive, but there is no other | |
11461 | valid interpretation of the following `::'. Therefore, those | |
11462 | names are considered class-names. */ | |
78757caa | 11463 | decl = TYPE_NAME (make_typename_type (scope, decl, tf_error)); |
a723baf1 MM |
11464 | else if (decl == error_mark_node |
11465 | || TREE_CODE (decl) != TYPE_DECL | |
11466 | || !IS_AGGR_TYPE (TREE_TYPE (decl))) | |
11467 | { | |
11468 | cp_parser_error (parser, "expected class-name"); | |
11469 | return error_mark_node; | |
11470 | } | |
11471 | ||
11472 | return decl; | |
11473 | } | |
11474 | ||
11475 | /* Parse a class-specifier. | |
11476 | ||
11477 | class-specifier: | |
11478 | class-head { member-specification [opt] } | |
11479 | ||
11480 | Returns the TREE_TYPE representing the class. */ | |
11481 | ||
11482 | static tree | |
94edc4ab | 11483 | cp_parser_class_specifier (cp_parser* parser) |
a723baf1 MM |
11484 | { |
11485 | cp_token *token; | |
11486 | tree type; | |
11487 | tree attributes = NULL_TREE; | |
11488 | int has_trailing_semicolon; | |
11489 | bool nested_name_specifier_p; | |
a723baf1 MM |
11490 | unsigned saved_num_template_parameter_lists; |
11491 | ||
8d241e0b | 11492 | push_deferring_access_checks (dk_no_deferred); |
cf22909c | 11493 | |
a723baf1 MM |
11494 | /* Parse the class-head. */ |
11495 | type = cp_parser_class_head (parser, | |
cf22909c | 11496 | &nested_name_specifier_p); |
a723baf1 MM |
11497 | /* If the class-head was a semantic disaster, skip the entire body |
11498 | of the class. */ | |
11499 | if (!type) | |
11500 | { | |
11501 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
cf22909c | 11502 | pop_deferring_access_checks (); |
a723baf1 MM |
11503 | return error_mark_node; |
11504 | } | |
cf22909c | 11505 | |
a723baf1 MM |
11506 | /* Look for the `{'. */ |
11507 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
cf22909c KL |
11508 | { |
11509 | pop_deferring_access_checks (); | |
11510 | return error_mark_node; | |
11511 | } | |
11512 | ||
a723baf1 MM |
11513 | /* Issue an error message if type-definitions are forbidden here. */ |
11514 | cp_parser_check_type_definition (parser); | |
11515 | /* Remember that we are defining one more class. */ | |
11516 | ++parser->num_classes_being_defined; | |
11517 | /* Inside the class, surrounding template-parameter-lists do not | |
11518 | apply. */ | |
11519 | saved_num_template_parameter_lists | |
11520 | = parser->num_template_parameter_lists; | |
11521 | parser->num_template_parameter_lists = 0; | |
78757caa | 11522 | |
a723baf1 | 11523 | /* Start the class. */ |
eeb23c11 MM |
11524 | if (nested_name_specifier_p) |
11525 | push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type))); | |
a723baf1 MM |
11526 | type = begin_class_definition (type); |
11527 | if (type == error_mark_node) | |
9bcb9aae | 11528 | /* If the type is erroneous, skip the entire body of the class. */ |
a723baf1 MM |
11529 | cp_parser_skip_to_closing_brace (parser); |
11530 | else | |
11531 | /* Parse the member-specification. */ | |
11532 | cp_parser_member_specification_opt (parser); | |
11533 | /* Look for the trailing `}'. */ | |
11534 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
11535 | /* We get better error messages by noticing a common problem: a | |
11536 | missing trailing `;'. */ | |
11537 | token = cp_lexer_peek_token (parser->lexer); | |
11538 | has_trailing_semicolon = (token->type == CPP_SEMICOLON); | |
11539 | /* Look for attributes to apply to this class. */ | |
11540 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
11541 | attributes = cp_parser_attributes_opt (parser); | |
560ad596 MM |
11542 | /* If we got any attributes in class_head, xref_tag will stick them in |
11543 | TREE_TYPE of the type. Grab them now. */ | |
11544 | if (type != error_mark_node) | |
11545 | { | |
11546 | attributes = chainon (TYPE_ATTRIBUTES (type), attributes); | |
11547 | TYPE_ATTRIBUTES (type) = NULL_TREE; | |
11548 | type = finish_struct (type, attributes); | |
11549 | } | |
11550 | if (nested_name_specifier_p) | |
11551 | pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type))); | |
a723baf1 MM |
11552 | /* If this class is not itself within the scope of another class, |
11553 | then we need to parse the bodies of all of the queued function | |
11554 | definitions. Note that the queued functions defined in a class | |
11555 | are not always processed immediately following the | |
11556 | class-specifier for that class. Consider: | |
11557 | ||
11558 | struct A { | |
11559 | struct B { void f() { sizeof (A); } }; | |
11560 | }; | |
11561 | ||
11562 | If `f' were processed before the processing of `A' were | |
11563 | completed, there would be no way to compute the size of `A'. | |
11564 | Note that the nesting we are interested in here is lexical -- | |
11565 | not the semantic nesting given by TYPE_CONTEXT. In particular, | |
11566 | for: | |
11567 | ||
11568 | struct A { struct B; }; | |
11569 | struct A::B { void f() { } }; | |
11570 | ||
11571 | there is no need to delay the parsing of `A::B::f'. */ | |
11572 | if (--parser->num_classes_being_defined == 0) | |
11573 | { | |
8218bd34 MM |
11574 | tree queue_entry; |
11575 | tree fn; | |
a723baf1 | 11576 | |
8218bd34 MM |
11577 | /* In a first pass, parse default arguments to the functions. |
11578 | Then, in a second pass, parse the bodies of the functions. | |
11579 | This two-phased approach handles cases like: | |
11580 | ||
11581 | struct S { | |
11582 | void f() { g(); } | |
11583 | void g(int i = 3); | |
11584 | }; | |
11585 | ||
11586 | */ | |
8db1028e NS |
11587 | for (TREE_PURPOSE (parser->unparsed_functions_queues) |
11588 | = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues)); | |
11589 | (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues)); | |
11590 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
11591 | = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues))) | |
8218bd34 MM |
11592 | { |
11593 | fn = TREE_VALUE (queue_entry); | |
8218bd34 MM |
11594 | /* Make sure that any template parameters are in scope. */ |
11595 | maybe_begin_member_template_processing (fn); | |
11596 | /* If there are default arguments that have not yet been processed, | |
11597 | take care of them now. */ | |
11598 | cp_parser_late_parsing_default_args (parser, fn); | |
11599 | /* Remove any template parameters from the symbol table. */ | |
11600 | maybe_end_member_template_processing (); | |
11601 | } | |
11602 | /* Now parse the body of the functions. */ | |
8db1028e NS |
11603 | for (TREE_VALUE (parser->unparsed_functions_queues) |
11604 | = nreverse (TREE_VALUE (parser->unparsed_functions_queues)); | |
11605 | (queue_entry = TREE_VALUE (parser->unparsed_functions_queues)); | |
11606 | TREE_VALUE (parser->unparsed_functions_queues) | |
11607 | = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues))) | |
a723baf1 | 11608 | { |
a723baf1 | 11609 | /* Figure out which function we need to process. */ |
a723baf1 MM |
11610 | fn = TREE_VALUE (queue_entry); |
11611 | ||
11612 | /* Parse the function. */ | |
11613 | cp_parser_late_parsing_for_member (parser, fn); | |
a723baf1 MM |
11614 | } |
11615 | ||
a723baf1 MM |
11616 | } |
11617 | ||
11618 | /* Put back any saved access checks. */ | |
cf22909c | 11619 | pop_deferring_access_checks (); |
a723baf1 MM |
11620 | |
11621 | /* Restore the count of active template-parameter-lists. */ | |
11622 | parser->num_template_parameter_lists | |
11623 | = saved_num_template_parameter_lists; | |
11624 | ||
11625 | return type; | |
11626 | } | |
11627 | ||
11628 | /* Parse a class-head. | |
11629 | ||
11630 | class-head: | |
11631 | class-key identifier [opt] base-clause [opt] | |
11632 | class-key nested-name-specifier identifier base-clause [opt] | |
11633 | class-key nested-name-specifier [opt] template-id | |
11634 | base-clause [opt] | |
11635 | ||
11636 | GNU Extensions: | |
11637 | class-key attributes identifier [opt] base-clause [opt] | |
11638 | class-key attributes nested-name-specifier identifier base-clause [opt] | |
11639 | class-key attributes nested-name-specifier [opt] template-id | |
11640 | base-clause [opt] | |
11641 | ||
11642 | Returns the TYPE of the indicated class. Sets | |
11643 | *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions | |
11644 | involving a nested-name-specifier was used, and FALSE otherwise. | |
a723baf1 MM |
11645 | |
11646 | Returns NULL_TREE if the class-head is syntactically valid, but | |
11647 | semantically invalid in a way that means we should skip the entire | |
11648 | body of the class. */ | |
11649 | ||
11650 | static tree | |
94edc4ab NN |
11651 | cp_parser_class_head (cp_parser* parser, |
11652 | bool* nested_name_specifier_p) | |
a723baf1 MM |
11653 | { |
11654 | cp_token *token; | |
11655 | tree nested_name_specifier; | |
11656 | enum tag_types class_key; | |
11657 | tree id = NULL_TREE; | |
11658 | tree type = NULL_TREE; | |
11659 | tree attributes; | |
11660 | bool template_id_p = false; | |
11661 | bool qualified_p = false; | |
11662 | bool invalid_nested_name_p = false; | |
afb0918a | 11663 | bool invalid_explicit_specialization_p = false; |
a723baf1 MM |
11664 | unsigned num_templates; |
11665 | ||
11666 | /* Assume no nested-name-specifier will be present. */ | |
11667 | *nested_name_specifier_p = false; | |
11668 | /* Assume no template parameter lists will be used in defining the | |
11669 | type. */ | |
11670 | num_templates = 0; | |
11671 | ||
11672 | /* Look for the class-key. */ | |
11673 | class_key = cp_parser_class_key (parser); | |
11674 | if (class_key == none_type) | |
11675 | return error_mark_node; | |
11676 | ||
11677 | /* Parse the attributes. */ | |
11678 | attributes = cp_parser_attributes_opt (parser); | |
11679 | ||
11680 | /* If the next token is `::', that is invalid -- but sometimes | |
11681 | people do try to write: | |
11682 | ||
11683 | struct ::S {}; | |
11684 | ||
11685 | Handle this gracefully by accepting the extra qualifier, and then | |
11686 | issuing an error about it later if this really is a | |
2050a1bb | 11687 | class-head. If it turns out just to be an elaborated type |
a723baf1 MM |
11688 | specifier, remain silent. */ |
11689 | if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)) | |
11690 | qualified_p = true; | |
11691 | ||
8d241e0b KL |
11692 | push_deferring_access_checks (dk_no_check); |
11693 | ||
a723baf1 MM |
11694 | /* Determine the name of the class. Begin by looking for an |
11695 | optional nested-name-specifier. */ | |
11696 | nested_name_specifier | |
11697 | = cp_parser_nested_name_specifier_opt (parser, | |
11698 | /*typename_keyword_p=*/false, | |
66d418e6 | 11699 | /*check_dependency_p=*/false, |
a668c6ad MM |
11700 | /*type_p=*/false, |
11701 | /*is_declaration=*/false); | |
a723baf1 MM |
11702 | /* If there was a nested-name-specifier, then there *must* be an |
11703 | identifier. */ | |
11704 | if (nested_name_specifier) | |
11705 | { | |
11706 | /* Although the grammar says `identifier', it really means | |
11707 | `class-name' or `template-name'. You are only allowed to | |
11708 | define a class that has already been declared with this | |
11709 | syntax. | |
11710 | ||
11711 | The proposed resolution for Core Issue 180 says that whever | |
11712 | you see `class T::X' you should treat `X' as a type-name. | |
11713 | ||
11714 | It is OK to define an inaccessible class; for example: | |
11715 | ||
11716 | class A { class B; }; | |
11717 | class A::B {}; | |
11718 | ||
a723baf1 MM |
11719 | We do not know if we will see a class-name, or a |
11720 | template-name. We look for a class-name first, in case the | |
11721 | class-name is a template-id; if we looked for the | |
11722 | template-name first we would stop after the template-name. */ | |
11723 | cp_parser_parse_tentatively (parser); | |
11724 | type = cp_parser_class_name (parser, | |
11725 | /*typename_keyword_p=*/false, | |
11726 | /*template_keyword_p=*/false, | |
11727 | /*type_p=*/true, | |
a723baf1 | 11728 | /*check_dependency_p=*/false, |
a668c6ad MM |
11729 | /*class_head_p=*/true, |
11730 | /*is_declaration=*/false); | |
a723baf1 MM |
11731 | /* If that didn't work, ignore the nested-name-specifier. */ |
11732 | if (!cp_parser_parse_definitely (parser)) | |
11733 | { | |
11734 | invalid_nested_name_p = true; | |
11735 | id = cp_parser_identifier (parser); | |
11736 | if (id == error_mark_node) | |
11737 | id = NULL_TREE; | |
11738 | } | |
11739 | /* If we could not find a corresponding TYPE, treat this | |
11740 | declaration like an unqualified declaration. */ | |
11741 | if (type == error_mark_node) | |
11742 | nested_name_specifier = NULL_TREE; | |
11743 | /* Otherwise, count the number of templates used in TYPE and its | |
11744 | containing scopes. */ | |
11745 | else | |
11746 | { | |
11747 | tree scope; | |
11748 | ||
11749 | for (scope = TREE_TYPE (type); | |
11750 | scope && TREE_CODE (scope) != NAMESPACE_DECL; | |
11751 | scope = (TYPE_P (scope) | |
11752 | ? TYPE_CONTEXT (scope) | |
11753 | : DECL_CONTEXT (scope))) | |
11754 | if (TYPE_P (scope) | |
11755 | && CLASS_TYPE_P (scope) | |
11756 | && CLASSTYPE_TEMPLATE_INFO (scope) | |
2050a1bb MM |
11757 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)) |
11758 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)) | |
a723baf1 MM |
11759 | ++num_templates; |
11760 | } | |
11761 | } | |
11762 | /* Otherwise, the identifier is optional. */ | |
11763 | else | |
11764 | { | |
11765 | /* We don't know whether what comes next is a template-id, | |
11766 | an identifier, or nothing at all. */ | |
11767 | cp_parser_parse_tentatively (parser); | |
11768 | /* Check for a template-id. */ | |
11769 | id = cp_parser_template_id (parser, | |
11770 | /*template_keyword_p=*/false, | |
a668c6ad MM |
11771 | /*check_dependency_p=*/true, |
11772 | /*is_declaration=*/true); | |
a723baf1 MM |
11773 | /* If that didn't work, it could still be an identifier. */ |
11774 | if (!cp_parser_parse_definitely (parser)) | |
11775 | { | |
11776 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
11777 | id = cp_parser_identifier (parser); | |
11778 | else | |
11779 | id = NULL_TREE; | |
11780 | } | |
11781 | else | |
11782 | { | |
11783 | template_id_p = true; | |
11784 | ++num_templates; | |
11785 | } | |
11786 | } | |
11787 | ||
8d241e0b KL |
11788 | pop_deferring_access_checks (); |
11789 | ||
ee43dab5 MM |
11790 | cp_parser_check_for_invalid_template_id (parser, id); |
11791 | ||
a723baf1 MM |
11792 | /* If it's not a `:' or a `{' then we can't really be looking at a |
11793 | class-head, since a class-head only appears as part of a | |
11794 | class-specifier. We have to detect this situation before calling | |
11795 | xref_tag, since that has irreversible side-effects. */ | |
11796 | if (!cp_parser_next_token_starts_class_definition_p (parser)) | |
11797 | { | |
11798 | cp_parser_error (parser, "expected `{' or `:'"); | |
11799 | return error_mark_node; | |
11800 | } | |
11801 | ||
11802 | /* At this point, we're going ahead with the class-specifier, even | |
11803 | if some other problem occurs. */ | |
11804 | cp_parser_commit_to_tentative_parse (parser); | |
11805 | /* Issue the error about the overly-qualified name now. */ | |
11806 | if (qualified_p) | |
11807 | cp_parser_error (parser, | |
11808 | "global qualification of class name is invalid"); | |
11809 | else if (invalid_nested_name_p) | |
11810 | cp_parser_error (parser, | |
11811 | "qualified name does not name a class"); | |
afb0918a MM |
11812 | /* An explicit-specialization must be preceded by "template <>". If |
11813 | it is not, try to recover gracefully. */ | |
11814 | if (at_namespace_scope_p () | |
11815 | && parser->num_template_parameter_lists == 0 | |
eeb23c11 | 11816 | && template_id_p) |
afb0918a MM |
11817 | { |
11818 | error ("an explicit specialization must be preceded by 'template <>'"); | |
11819 | invalid_explicit_specialization_p = true; | |
11820 | /* Take the same action that would have been taken by | |
11821 | cp_parser_explicit_specialization. */ | |
11822 | ++parser->num_template_parameter_lists; | |
11823 | begin_specialization (); | |
11824 | } | |
11825 | /* There must be no "return" statements between this point and the | |
11826 | end of this function; set "type "to the correct return value and | |
11827 | use "goto done;" to return. */ | |
a723baf1 MM |
11828 | /* Make sure that the right number of template parameters were |
11829 | present. */ | |
11830 | if (!cp_parser_check_template_parameters (parser, num_templates)) | |
afb0918a MM |
11831 | { |
11832 | /* If something went wrong, there is no point in even trying to | |
11833 | process the class-definition. */ | |
11834 | type = NULL_TREE; | |
11835 | goto done; | |
11836 | } | |
a723baf1 | 11837 | |
a723baf1 MM |
11838 | /* Look up the type. */ |
11839 | if (template_id_p) | |
11840 | { | |
11841 | type = TREE_TYPE (id); | |
11842 | maybe_process_partial_specialization (type); | |
11843 | } | |
11844 | else if (!nested_name_specifier) | |
11845 | { | |
11846 | /* If the class was unnamed, create a dummy name. */ | |
11847 | if (!id) | |
11848 | id = make_anon_name (); | |
cbd63935 KL |
11849 | type = xref_tag (class_key, id, attributes, /*globalize=*/false, |
11850 | parser->num_template_parameter_lists); | |
a723baf1 MM |
11851 | } |
11852 | else | |
11853 | { | |
a723baf1 | 11854 | tree class_type; |
089d6ea7 | 11855 | tree scope; |
a723baf1 MM |
11856 | |
11857 | /* Given: | |
11858 | ||
11859 | template <typename T> struct S { struct T }; | |
14d22dd6 | 11860 | template <typename T> struct S<T>::T { }; |
a723baf1 MM |
11861 | |
11862 | we will get a TYPENAME_TYPE when processing the definition of | |
11863 | `S::T'. We need to resolve it to the actual type before we | |
11864 | try to define it. */ | |
11865 | if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE) | |
11866 | { | |
14d22dd6 MM |
11867 | class_type = resolve_typename_type (TREE_TYPE (type), |
11868 | /*only_current_p=*/false); | |
11869 | if (class_type != error_mark_node) | |
11870 | type = TYPE_NAME (class_type); | |
11871 | else | |
11872 | { | |
11873 | cp_parser_error (parser, "could not resolve typename type"); | |
11874 | type = error_mark_node; | |
11875 | } | |
a723baf1 MM |
11876 | } |
11877 | ||
089d6ea7 MM |
11878 | /* Figure out in what scope the declaration is being placed. */ |
11879 | scope = current_scope (); | |
11880 | if (!scope) | |
11881 | scope = current_namespace; | |
11882 | /* If that scope does not contain the scope in which the | |
11883 | class was originally declared, the program is invalid. */ | |
11884 | if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type))) | |
11885 | { | |
0e136342 | 11886 | error ("declaration of `%D' in `%D' which does not " |
089d6ea7 | 11887 | "enclose `%D'", type, scope, nested_name_specifier); |
afb0918a MM |
11888 | type = NULL_TREE; |
11889 | goto done; | |
089d6ea7 | 11890 | } |
560ad596 | 11891 | /* [dcl.meaning] |
089d6ea7 | 11892 | |
560ad596 MM |
11893 | A declarator-id shall not be qualified exception of the |
11894 | definition of a ... nested class outside of its class | |
11895 | ... [or] a the definition or explicit instantiation of a | |
11896 | class member of a namespace outside of its namespace. */ | |
11897 | if (scope == CP_DECL_CONTEXT (type)) | |
a723baf1 | 11898 | { |
560ad596 MM |
11899 | pedwarn ("extra qualification ignored"); |
11900 | nested_name_specifier = NULL_TREE; | |
a723baf1 | 11901 | } |
560ad596 MM |
11902 | |
11903 | maybe_process_partial_specialization (TREE_TYPE (type)); | |
11904 | class_type = current_class_type; | |
11905 | /* Enter the scope indicated by the nested-name-specifier. */ | |
11906 | if (nested_name_specifier) | |
11907 | push_scope (nested_name_specifier); | |
11908 | /* Get the canonical version of this type. */ | |
11909 | type = TYPE_MAIN_DECL (TREE_TYPE (type)); | |
11910 | if (PROCESSING_REAL_TEMPLATE_DECL_P () | |
11911 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type))) | |
11912 | type = push_template_decl (type); | |
11913 | type = TREE_TYPE (type); | |
11914 | if (nested_name_specifier) | |
eeb23c11 MM |
11915 | { |
11916 | *nested_name_specifier_p = true; | |
11917 | pop_scope (nested_name_specifier); | |
11918 | } | |
a723baf1 MM |
11919 | } |
11920 | /* Indicate whether this class was declared as a `class' or as a | |
11921 | `struct'. */ | |
11922 | if (TREE_CODE (type) == RECORD_TYPE) | |
11923 | CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type); | |
11924 | cp_parser_check_class_key (class_key, type); | |
11925 | ||
11926 | /* Enter the scope containing the class; the names of base classes | |
11927 | should be looked up in that context. For example, given: | |
11928 | ||
11929 | struct A { struct B {}; struct C; }; | |
11930 | struct A::C : B {}; | |
11931 | ||
11932 | is valid. */ | |
11933 | if (nested_name_specifier) | |
11934 | push_scope (nested_name_specifier); | |
11935 | /* Now, look for the base-clause. */ | |
11936 | token = cp_lexer_peek_token (parser->lexer); | |
11937 | if (token->type == CPP_COLON) | |
11938 | { | |
11939 | tree bases; | |
11940 | ||
11941 | /* Get the list of base-classes. */ | |
11942 | bases = cp_parser_base_clause (parser); | |
11943 | /* Process them. */ | |
11944 | xref_basetypes (type, bases); | |
11945 | } | |
11946 | /* Leave the scope given by the nested-name-specifier. We will | |
11947 | enter the class scope itself while processing the members. */ | |
11948 | if (nested_name_specifier) | |
11949 | pop_scope (nested_name_specifier); | |
11950 | ||
afb0918a MM |
11951 | done: |
11952 | if (invalid_explicit_specialization_p) | |
11953 | { | |
11954 | end_specialization (); | |
11955 | --parser->num_template_parameter_lists; | |
11956 | } | |
a723baf1 MM |
11957 | return type; |
11958 | } | |
11959 | ||
11960 | /* Parse a class-key. | |
11961 | ||
11962 | class-key: | |
11963 | class | |
11964 | struct | |
11965 | union | |
11966 | ||
11967 | Returns the kind of class-key specified, or none_type to indicate | |
11968 | error. */ | |
11969 | ||
11970 | static enum tag_types | |
94edc4ab | 11971 | cp_parser_class_key (cp_parser* parser) |
a723baf1 MM |
11972 | { |
11973 | cp_token *token; | |
11974 | enum tag_types tag_type; | |
11975 | ||
11976 | /* Look for the class-key. */ | |
11977 | token = cp_parser_require (parser, CPP_KEYWORD, "class-key"); | |
11978 | if (!token) | |
11979 | return none_type; | |
11980 | ||
11981 | /* Check to see if the TOKEN is a class-key. */ | |
11982 | tag_type = cp_parser_token_is_class_key (token); | |
11983 | if (!tag_type) | |
11984 | cp_parser_error (parser, "expected class-key"); | |
11985 | return tag_type; | |
11986 | } | |
11987 | ||
11988 | /* Parse an (optional) member-specification. | |
11989 | ||
11990 | member-specification: | |
11991 | member-declaration member-specification [opt] | |
11992 | access-specifier : member-specification [opt] */ | |
11993 | ||
11994 | static void | |
94edc4ab | 11995 | cp_parser_member_specification_opt (cp_parser* parser) |
a723baf1 MM |
11996 | { |
11997 | while (true) | |
11998 | { | |
11999 | cp_token *token; | |
12000 | enum rid keyword; | |
12001 | ||
12002 | /* Peek at the next token. */ | |
12003 | token = cp_lexer_peek_token (parser->lexer); | |
12004 | /* If it's a `}', or EOF then we've seen all the members. */ | |
12005 | if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF) | |
12006 | break; | |
12007 | ||
12008 | /* See if this token is a keyword. */ | |
12009 | keyword = token->keyword; | |
12010 | switch (keyword) | |
12011 | { | |
12012 | case RID_PUBLIC: | |
12013 | case RID_PROTECTED: | |
12014 | case RID_PRIVATE: | |
12015 | /* Consume the access-specifier. */ | |
12016 | cp_lexer_consume_token (parser->lexer); | |
12017 | /* Remember which access-specifier is active. */ | |
12018 | current_access_specifier = token->value; | |
12019 | /* Look for the `:'. */ | |
12020 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
12021 | break; | |
12022 | ||
12023 | default: | |
12024 | /* Otherwise, the next construction must be a | |
12025 | member-declaration. */ | |
12026 | cp_parser_member_declaration (parser); | |
a723baf1 MM |
12027 | } |
12028 | } | |
12029 | } | |
12030 | ||
12031 | /* Parse a member-declaration. | |
12032 | ||
12033 | member-declaration: | |
12034 | decl-specifier-seq [opt] member-declarator-list [opt] ; | |
12035 | function-definition ; [opt] | |
12036 | :: [opt] nested-name-specifier template [opt] unqualified-id ; | |
12037 | using-declaration | |
12038 | template-declaration | |
12039 | ||
12040 | member-declarator-list: | |
12041 | member-declarator | |
12042 | member-declarator-list , member-declarator | |
12043 | ||
12044 | member-declarator: | |
12045 | declarator pure-specifier [opt] | |
12046 | declarator constant-initializer [opt] | |
12047 | identifier [opt] : constant-expression | |
12048 | ||
12049 | GNU Extensions: | |
12050 | ||
12051 | member-declaration: | |
12052 | __extension__ member-declaration | |
12053 | ||
12054 | member-declarator: | |
12055 | declarator attributes [opt] pure-specifier [opt] | |
12056 | declarator attributes [opt] constant-initializer [opt] | |
12057 | identifier [opt] attributes [opt] : constant-expression */ | |
12058 | ||
12059 | static void | |
94edc4ab | 12060 | cp_parser_member_declaration (cp_parser* parser) |
a723baf1 MM |
12061 | { |
12062 | tree decl_specifiers; | |
12063 | tree prefix_attributes; | |
12064 | tree decl; | |
560ad596 | 12065 | int declares_class_or_enum; |
a723baf1 MM |
12066 | bool friend_p; |
12067 | cp_token *token; | |
12068 | int saved_pedantic; | |
12069 | ||
12070 | /* Check for the `__extension__' keyword. */ | |
12071 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
12072 | { | |
12073 | /* Recurse. */ | |
12074 | cp_parser_member_declaration (parser); | |
12075 | /* Restore the old value of the PEDANTIC flag. */ | |
12076 | pedantic = saved_pedantic; | |
12077 | ||
12078 | return; | |
12079 | } | |
12080 | ||
12081 | /* Check for a template-declaration. */ | |
12082 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
12083 | { | |
12084 | /* Parse the template-declaration. */ | |
12085 | cp_parser_template_declaration (parser, /*member_p=*/true); | |
12086 | ||
12087 | return; | |
12088 | } | |
12089 | ||
12090 | /* Check for a using-declaration. */ | |
12091 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING)) | |
12092 | { | |
12093 | /* Parse the using-declaration. */ | |
12094 | cp_parser_using_declaration (parser); | |
12095 | ||
12096 | return; | |
12097 | } | |
12098 | ||
a723baf1 MM |
12099 | /* Parse the decl-specifier-seq. */ |
12100 | decl_specifiers | |
12101 | = cp_parser_decl_specifier_seq (parser, | |
12102 | CP_PARSER_FLAGS_OPTIONAL, | |
12103 | &prefix_attributes, | |
12104 | &declares_class_or_enum); | |
8fbc5ae7 MM |
12105 | /* Check for an invalid type-name. */ |
12106 | if (cp_parser_diagnose_invalid_type_name (parser)) | |
12107 | return; | |
a723baf1 MM |
12108 | /* If there is no declarator, then the decl-specifier-seq should |
12109 | specify a type. */ | |
12110 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
12111 | { | |
12112 | /* If there was no decl-specifier-seq, and the next token is a | |
12113 | `;', then we have something like: | |
12114 | ||
12115 | struct S { ; }; | |
12116 | ||
12117 | [class.mem] | |
12118 | ||
12119 | Each member-declaration shall declare at least one member | |
12120 | name of the class. */ | |
12121 | if (!decl_specifiers) | |
12122 | { | |
12123 | if (pedantic) | |
12124 | pedwarn ("extra semicolon"); | |
12125 | } | |
12126 | else | |
12127 | { | |
12128 | tree type; | |
12129 | ||
12130 | /* See if this declaration is a friend. */ | |
12131 | friend_p = cp_parser_friend_p (decl_specifiers); | |
12132 | /* If there were decl-specifiers, check to see if there was | |
12133 | a class-declaration. */ | |
12134 | type = check_tag_decl (decl_specifiers); | |
12135 | /* Nested classes have already been added to the class, but | |
12136 | a `friend' needs to be explicitly registered. */ | |
12137 | if (friend_p) | |
12138 | { | |
12139 | /* If the `friend' keyword was present, the friend must | |
12140 | be introduced with a class-key. */ | |
12141 | if (!declares_class_or_enum) | |
12142 | error ("a class-key must be used when declaring a friend"); | |
12143 | /* In this case: | |
12144 | ||
12145 | template <typename T> struct A { | |
12146 | friend struct A<T>::B; | |
12147 | }; | |
12148 | ||
12149 | A<T>::B will be represented by a TYPENAME_TYPE, and | |
12150 | therefore not recognized by check_tag_decl. */ | |
12151 | if (!type) | |
12152 | { | |
12153 | tree specifier; | |
12154 | ||
12155 | for (specifier = decl_specifiers; | |
12156 | specifier; | |
12157 | specifier = TREE_CHAIN (specifier)) | |
12158 | { | |
12159 | tree s = TREE_VALUE (specifier); | |
12160 | ||
c003e212 GDR |
12161 | if (TREE_CODE (s) == IDENTIFIER_NODE) |
12162 | get_global_value_if_present (s, &type); | |
a723baf1 MM |
12163 | if (TREE_CODE (s) == TYPE_DECL) |
12164 | s = TREE_TYPE (s); | |
12165 | if (TYPE_P (s)) | |
12166 | { | |
12167 | type = s; | |
12168 | break; | |
12169 | } | |
12170 | } | |
12171 | } | |
12172 | if (!type) | |
12173 | error ("friend declaration does not name a class or " | |
12174 | "function"); | |
12175 | else | |
19db77ce KL |
12176 | make_friend_class (current_class_type, type, |
12177 | /*complain=*/true); | |
a723baf1 MM |
12178 | } |
12179 | /* If there is no TYPE, an error message will already have | |
12180 | been issued. */ | |
12181 | else if (!type) | |
12182 | ; | |
12183 | /* An anonymous aggregate has to be handled specially; such | |
12184 | a declaration really declares a data member (with a | |
12185 | particular type), as opposed to a nested class. */ | |
12186 | else if (ANON_AGGR_TYPE_P (type)) | |
12187 | { | |
12188 | /* Remove constructors and such from TYPE, now that we | |
34cd5ae7 | 12189 | know it is an anonymous aggregate. */ |
a723baf1 MM |
12190 | fixup_anonymous_aggr (type); |
12191 | /* And make the corresponding data member. */ | |
12192 | decl = build_decl (FIELD_DECL, NULL_TREE, type); | |
12193 | /* Add it to the class. */ | |
12194 | finish_member_declaration (decl); | |
12195 | } | |
37d407a1 KL |
12196 | else |
12197 | cp_parser_check_access_in_redeclaration (TYPE_NAME (type)); | |
a723baf1 MM |
12198 | } |
12199 | } | |
12200 | else | |
12201 | { | |
12202 | /* See if these declarations will be friends. */ | |
12203 | friend_p = cp_parser_friend_p (decl_specifiers); | |
12204 | ||
12205 | /* Keep going until we hit the `;' at the end of the | |
12206 | declaration. */ | |
12207 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
12208 | { | |
12209 | tree attributes = NULL_TREE; | |
12210 | tree first_attribute; | |
12211 | ||
12212 | /* Peek at the next token. */ | |
12213 | token = cp_lexer_peek_token (parser->lexer); | |
12214 | ||
12215 | /* Check for a bitfield declaration. */ | |
12216 | if (token->type == CPP_COLON | |
12217 | || (token->type == CPP_NAME | |
12218 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
12219 | == CPP_COLON)) | |
12220 | { | |
12221 | tree identifier; | |
12222 | tree width; | |
12223 | ||
12224 | /* Get the name of the bitfield. Note that we cannot just | |
12225 | check TOKEN here because it may have been invalidated by | |
12226 | the call to cp_lexer_peek_nth_token above. */ | |
12227 | if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON) | |
12228 | identifier = cp_parser_identifier (parser); | |
12229 | else | |
12230 | identifier = NULL_TREE; | |
12231 | ||
12232 | /* Consume the `:' token. */ | |
12233 | cp_lexer_consume_token (parser->lexer); | |
12234 | /* Get the width of the bitfield. */ | |
14d22dd6 MM |
12235 | width |
12236 | = cp_parser_constant_expression (parser, | |
12237 | /*allow_non_constant=*/false, | |
12238 | NULL); | |
a723baf1 MM |
12239 | |
12240 | /* Look for attributes that apply to the bitfield. */ | |
12241 | attributes = cp_parser_attributes_opt (parser); | |
12242 | /* Remember which attributes are prefix attributes and | |
12243 | which are not. */ | |
12244 | first_attribute = attributes; | |
12245 | /* Combine the attributes. */ | |
12246 | attributes = chainon (prefix_attributes, attributes); | |
12247 | ||
12248 | /* Create the bitfield declaration. */ | |
12249 | decl = grokbitfield (identifier, | |
12250 | decl_specifiers, | |
12251 | width); | |
12252 | /* Apply the attributes. */ | |
12253 | cplus_decl_attributes (&decl, attributes, /*flags=*/0); | |
12254 | } | |
12255 | else | |
12256 | { | |
12257 | tree declarator; | |
12258 | tree initializer; | |
12259 | tree asm_specification; | |
7efa3e22 | 12260 | int ctor_dtor_or_conv_p; |
a723baf1 MM |
12261 | |
12262 | /* Parse the declarator. */ | |
12263 | declarator | |
62b8a44e | 12264 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
12265 | &ctor_dtor_or_conv_p, |
12266 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
12267 | |
12268 | /* If something went wrong parsing the declarator, make sure | |
12269 | that we at least consume some tokens. */ | |
12270 | if (declarator == error_mark_node) | |
12271 | { | |
12272 | /* Skip to the end of the statement. */ | |
12273 | cp_parser_skip_to_end_of_statement (parser); | |
4bb8ca28 MM |
12274 | /* If the next token is not a semicolon, that is |
12275 | probably because we just skipped over the body of | |
12276 | a function. So, we consume a semicolon if | |
12277 | present, but do not issue an error message if it | |
12278 | is not present. */ | |
12279 | if (cp_lexer_next_token_is (parser->lexer, | |
12280 | CPP_SEMICOLON)) | |
12281 | cp_lexer_consume_token (parser->lexer); | |
12282 | return; | |
a723baf1 MM |
12283 | } |
12284 | ||
560ad596 MM |
12285 | cp_parser_check_for_definition_in_return_type |
12286 | (declarator, declares_class_or_enum); | |
12287 | ||
a723baf1 MM |
12288 | /* Look for an asm-specification. */ |
12289 | asm_specification = cp_parser_asm_specification_opt (parser); | |
12290 | /* Look for attributes that apply to the declaration. */ | |
12291 | attributes = cp_parser_attributes_opt (parser); | |
12292 | /* Remember which attributes are prefix attributes and | |
12293 | which are not. */ | |
12294 | first_attribute = attributes; | |
12295 | /* Combine the attributes. */ | |
12296 | attributes = chainon (prefix_attributes, attributes); | |
12297 | ||
12298 | /* If it's an `=', then we have a constant-initializer or a | |
12299 | pure-specifier. It is not correct to parse the | |
12300 | initializer before registering the member declaration | |
12301 | since the member declaration should be in scope while | |
12302 | its initializer is processed. However, the rest of the | |
12303 | front end does not yet provide an interface that allows | |
12304 | us to handle this correctly. */ | |
12305 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
12306 | { | |
12307 | /* In [class.mem]: | |
12308 | ||
12309 | A pure-specifier shall be used only in the declaration of | |
12310 | a virtual function. | |
12311 | ||
12312 | A member-declarator can contain a constant-initializer | |
12313 | only if it declares a static member of integral or | |
12314 | enumeration type. | |
12315 | ||
12316 | Therefore, if the DECLARATOR is for a function, we look | |
12317 | for a pure-specifier; otherwise, we look for a | |
12318 | constant-initializer. When we call `grokfield', it will | |
12319 | perform more stringent semantics checks. */ | |
12320 | if (TREE_CODE (declarator) == CALL_EXPR) | |
12321 | initializer = cp_parser_pure_specifier (parser); | |
12322 | else | |
4bb8ca28 MM |
12323 | /* Parse the initializer. */ |
12324 | initializer = cp_parser_constant_initializer (parser); | |
a723baf1 MM |
12325 | } |
12326 | /* Otherwise, there is no initializer. */ | |
12327 | else | |
12328 | initializer = NULL_TREE; | |
12329 | ||
12330 | /* See if we are probably looking at a function | |
12331 | definition. We are certainly not looking at at a | |
12332 | member-declarator. Calling `grokfield' has | |
12333 | side-effects, so we must not do it unless we are sure | |
12334 | that we are looking at a member-declarator. */ | |
12335 | if (cp_parser_token_starts_function_definition_p | |
12336 | (cp_lexer_peek_token (parser->lexer))) | |
4bb8ca28 MM |
12337 | { |
12338 | /* The grammar does not allow a pure-specifier to be | |
12339 | used when a member function is defined. (It is | |
12340 | possible that this fact is an oversight in the | |
12341 | standard, since a pure function may be defined | |
12342 | outside of the class-specifier. */ | |
12343 | if (initializer) | |
12344 | error ("pure-specifier on function-definition"); | |
12345 | decl = cp_parser_save_member_function_body (parser, | |
12346 | decl_specifiers, | |
12347 | declarator, | |
12348 | attributes); | |
12349 | /* If the member was not a friend, declare it here. */ | |
12350 | if (!friend_p) | |
12351 | finish_member_declaration (decl); | |
12352 | /* Peek at the next token. */ | |
12353 | token = cp_lexer_peek_token (parser->lexer); | |
12354 | /* If the next token is a semicolon, consume it. */ | |
12355 | if (token->type == CPP_SEMICOLON) | |
12356 | cp_lexer_consume_token (parser->lexer); | |
12357 | return; | |
12358 | } | |
a723baf1 | 12359 | else |
39703eb9 MM |
12360 | { |
12361 | /* Create the declaration. */ | |
ee3071ef NS |
12362 | decl = grokfield (declarator, decl_specifiers, |
12363 | initializer, asm_specification, | |
39703eb9 MM |
12364 | attributes); |
12365 | /* Any initialization must have been from a | |
12366 | constant-expression. */ | |
12367 | if (decl && TREE_CODE (decl) == VAR_DECL && initializer) | |
12368 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; | |
12369 | } | |
a723baf1 MM |
12370 | } |
12371 | ||
12372 | /* Reset PREFIX_ATTRIBUTES. */ | |
12373 | while (attributes && TREE_CHAIN (attributes) != first_attribute) | |
12374 | attributes = TREE_CHAIN (attributes); | |
12375 | if (attributes) | |
12376 | TREE_CHAIN (attributes) = NULL_TREE; | |
12377 | ||
12378 | /* If there is any qualification still in effect, clear it | |
12379 | now; we will be starting fresh with the next declarator. */ | |
12380 | parser->scope = NULL_TREE; | |
12381 | parser->qualifying_scope = NULL_TREE; | |
12382 | parser->object_scope = NULL_TREE; | |
12383 | /* If it's a `,', then there are more declarators. */ | |
12384 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
12385 | cp_lexer_consume_token (parser->lexer); | |
12386 | /* If the next token isn't a `;', then we have a parse error. */ | |
12387 | else if (cp_lexer_next_token_is_not (parser->lexer, | |
12388 | CPP_SEMICOLON)) | |
12389 | { | |
12390 | cp_parser_error (parser, "expected `;'"); | |
04c06002 | 12391 | /* Skip tokens until we find a `;'. */ |
a723baf1 MM |
12392 | cp_parser_skip_to_end_of_statement (parser); |
12393 | ||
12394 | break; | |
12395 | } | |
12396 | ||
12397 | if (decl) | |
12398 | { | |
12399 | /* Add DECL to the list of members. */ | |
12400 | if (!friend_p) | |
12401 | finish_member_declaration (decl); | |
12402 | ||
a723baf1 | 12403 | if (TREE_CODE (decl) == FUNCTION_DECL) |
8db1028e | 12404 | cp_parser_save_default_args (parser, decl); |
a723baf1 MM |
12405 | } |
12406 | } | |
12407 | } | |
12408 | ||
4bb8ca28 | 12409 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
a723baf1 MM |
12410 | } |
12411 | ||
12412 | /* Parse a pure-specifier. | |
12413 | ||
12414 | pure-specifier: | |
12415 | = 0 | |
12416 | ||
12417 | Returns INTEGER_ZERO_NODE if a pure specifier is found. | |
cd0be382 | 12418 | Otherwise, ERROR_MARK_NODE is returned. */ |
a723baf1 MM |
12419 | |
12420 | static tree | |
94edc4ab | 12421 | cp_parser_pure_specifier (cp_parser* parser) |
a723baf1 MM |
12422 | { |
12423 | cp_token *token; | |
12424 | ||
12425 | /* Look for the `=' token. */ | |
12426 | if (!cp_parser_require (parser, CPP_EQ, "`='")) | |
12427 | return error_mark_node; | |
12428 | /* Look for the `0' token. */ | |
12429 | token = cp_parser_require (parser, CPP_NUMBER, "`0'"); | |
12430 | /* Unfortunately, this will accept `0L' and `0x00' as well. We need | |
12431 | to get information from the lexer about how the number was | |
12432 | spelled in order to fix this problem. */ | |
12433 | if (!token || !integer_zerop (token->value)) | |
12434 | return error_mark_node; | |
12435 | ||
12436 | return integer_zero_node; | |
12437 | } | |
12438 | ||
12439 | /* Parse a constant-initializer. | |
12440 | ||
12441 | constant-initializer: | |
12442 | = constant-expression | |
12443 | ||
12444 | Returns a representation of the constant-expression. */ | |
12445 | ||
12446 | static tree | |
94edc4ab | 12447 | cp_parser_constant_initializer (cp_parser* parser) |
a723baf1 MM |
12448 | { |
12449 | /* Look for the `=' token. */ | |
12450 | if (!cp_parser_require (parser, CPP_EQ, "`='")) | |
12451 | return error_mark_node; | |
12452 | ||
12453 | /* It is invalid to write: | |
12454 | ||
12455 | struct S { static const int i = { 7 }; }; | |
12456 | ||
12457 | */ | |
12458 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
12459 | { | |
12460 | cp_parser_error (parser, | |
12461 | "a brace-enclosed initializer is not allowed here"); | |
12462 | /* Consume the opening brace. */ | |
12463 | cp_lexer_consume_token (parser->lexer); | |
12464 | /* Skip the initializer. */ | |
12465 | cp_parser_skip_to_closing_brace (parser); | |
12466 | /* Look for the trailing `}'. */ | |
12467 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
12468 | ||
12469 | return error_mark_node; | |
12470 | } | |
12471 | ||
14d22dd6 MM |
12472 | return cp_parser_constant_expression (parser, |
12473 | /*allow_non_constant=*/false, | |
12474 | NULL); | |
a723baf1 MM |
12475 | } |
12476 | ||
12477 | /* Derived classes [gram.class.derived] */ | |
12478 | ||
12479 | /* Parse a base-clause. | |
12480 | ||
12481 | base-clause: | |
12482 | : base-specifier-list | |
12483 | ||
12484 | base-specifier-list: | |
12485 | base-specifier | |
12486 | base-specifier-list , base-specifier | |
12487 | ||
12488 | Returns a TREE_LIST representing the base-classes, in the order in | |
12489 | which they were declared. The representation of each node is as | |
12490 | described by cp_parser_base_specifier. | |
12491 | ||
12492 | In the case that no bases are specified, this function will return | |
12493 | NULL_TREE, not ERROR_MARK_NODE. */ | |
12494 | ||
12495 | static tree | |
94edc4ab | 12496 | cp_parser_base_clause (cp_parser* parser) |
a723baf1 MM |
12497 | { |
12498 | tree bases = NULL_TREE; | |
12499 | ||
12500 | /* Look for the `:' that begins the list. */ | |
12501 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
12502 | ||
12503 | /* Scan the base-specifier-list. */ | |
12504 | while (true) | |
12505 | { | |
12506 | cp_token *token; | |
12507 | tree base; | |
12508 | ||
12509 | /* Look for the base-specifier. */ | |
12510 | base = cp_parser_base_specifier (parser); | |
12511 | /* Add BASE to the front of the list. */ | |
12512 | if (base != error_mark_node) | |
12513 | { | |
12514 | TREE_CHAIN (base) = bases; | |
12515 | bases = base; | |
12516 | } | |
12517 | /* Peek at the next token. */ | |
12518 | token = cp_lexer_peek_token (parser->lexer); | |
12519 | /* If it's not a comma, then the list is complete. */ | |
12520 | if (token->type != CPP_COMMA) | |
12521 | break; | |
12522 | /* Consume the `,'. */ | |
12523 | cp_lexer_consume_token (parser->lexer); | |
12524 | } | |
12525 | ||
12526 | /* PARSER->SCOPE may still be non-NULL at this point, if the last | |
12527 | base class had a qualified name. However, the next name that | |
12528 | appears is certainly not qualified. */ | |
12529 | parser->scope = NULL_TREE; | |
12530 | parser->qualifying_scope = NULL_TREE; | |
12531 | parser->object_scope = NULL_TREE; | |
12532 | ||
12533 | return nreverse (bases); | |
12534 | } | |
12535 | ||
12536 | /* Parse a base-specifier. | |
12537 | ||
12538 | base-specifier: | |
12539 | :: [opt] nested-name-specifier [opt] class-name | |
12540 | virtual access-specifier [opt] :: [opt] nested-name-specifier | |
12541 | [opt] class-name | |
12542 | access-specifier virtual [opt] :: [opt] nested-name-specifier | |
12543 | [opt] class-name | |
12544 | ||
12545 | Returns a TREE_LIST. The TREE_PURPOSE will be one of | |
12546 | ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to | |
12547 | indicate the specifiers provided. The TREE_VALUE will be a TYPE | |
12548 | (or the ERROR_MARK_NODE) indicating the type that was specified. */ | |
12549 | ||
12550 | static tree | |
94edc4ab | 12551 | cp_parser_base_specifier (cp_parser* parser) |
a723baf1 MM |
12552 | { |
12553 | cp_token *token; | |
12554 | bool done = false; | |
12555 | bool virtual_p = false; | |
12556 | bool duplicate_virtual_error_issued_p = false; | |
12557 | bool duplicate_access_error_issued_p = false; | |
bbaab916 | 12558 | bool class_scope_p, template_p; |
dbbf88d1 | 12559 | tree access = access_default_node; |
a723baf1 MM |
12560 | tree type; |
12561 | ||
12562 | /* Process the optional `virtual' and `access-specifier'. */ | |
12563 | while (!done) | |
12564 | { | |
12565 | /* Peek at the next token. */ | |
12566 | token = cp_lexer_peek_token (parser->lexer); | |
12567 | /* Process `virtual'. */ | |
12568 | switch (token->keyword) | |
12569 | { | |
12570 | case RID_VIRTUAL: | |
12571 | /* If `virtual' appears more than once, issue an error. */ | |
12572 | if (virtual_p && !duplicate_virtual_error_issued_p) | |
12573 | { | |
12574 | cp_parser_error (parser, | |
12575 | "`virtual' specified more than once in base-specified"); | |
12576 | duplicate_virtual_error_issued_p = true; | |
12577 | } | |
12578 | ||
12579 | virtual_p = true; | |
12580 | ||
12581 | /* Consume the `virtual' token. */ | |
12582 | cp_lexer_consume_token (parser->lexer); | |
12583 | ||
12584 | break; | |
12585 | ||
12586 | case RID_PUBLIC: | |
12587 | case RID_PROTECTED: | |
12588 | case RID_PRIVATE: | |
12589 | /* If more than one access specifier appears, issue an | |
12590 | error. */ | |
dbbf88d1 NS |
12591 | if (access != access_default_node |
12592 | && !duplicate_access_error_issued_p) | |
a723baf1 MM |
12593 | { |
12594 | cp_parser_error (parser, | |
12595 | "more than one access specifier in base-specified"); | |
12596 | duplicate_access_error_issued_p = true; | |
12597 | } | |
12598 | ||
dbbf88d1 | 12599 | access = ridpointers[(int) token->keyword]; |
a723baf1 MM |
12600 | |
12601 | /* Consume the access-specifier. */ | |
12602 | cp_lexer_consume_token (parser->lexer); | |
12603 | ||
12604 | break; | |
12605 | ||
12606 | default: | |
12607 | done = true; | |
12608 | break; | |
12609 | } | |
12610 | } | |
12611 | ||
a723baf1 MM |
12612 | /* Look for the optional `::' operator. */ |
12613 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); | |
12614 | /* Look for the nested-name-specifier. The simplest way to | |
12615 | implement: | |
12616 | ||
12617 | [temp.res] | |
12618 | ||
12619 | The keyword `typename' is not permitted in a base-specifier or | |
12620 | mem-initializer; in these contexts a qualified name that | |
12621 | depends on a template-parameter is implicitly assumed to be a | |
12622 | type name. | |
12623 | ||
12624 | is to pretend that we have seen the `typename' keyword at this | |
12625 | point. */ | |
12626 | cp_parser_nested_name_specifier_opt (parser, | |
12627 | /*typename_keyword_p=*/true, | |
12628 | /*check_dependency_p=*/true, | |
a668c6ad MM |
12629 | /*type_p=*/true, |
12630 | /*is_declaration=*/true); | |
a723baf1 MM |
12631 | /* If the base class is given by a qualified name, assume that names |
12632 | we see are type names or templates, as appropriate. */ | |
12633 | class_scope_p = (parser->scope && TYPE_P (parser->scope)); | |
bbaab916 NS |
12634 | template_p = class_scope_p && cp_parser_optional_template_keyword (parser); |
12635 | ||
a723baf1 MM |
12636 | /* Finally, look for the class-name. */ |
12637 | type = cp_parser_class_name (parser, | |
12638 | class_scope_p, | |
bbaab916 | 12639 | template_p, |
a723baf1 | 12640 | /*type_p=*/true, |
a723baf1 | 12641 | /*check_dependency_p=*/true, |
a668c6ad MM |
12642 | /*class_head_p=*/false, |
12643 | /*is_declaration=*/true); | |
a723baf1 MM |
12644 | |
12645 | if (type == error_mark_node) | |
12646 | return error_mark_node; | |
12647 | ||
dbbf88d1 | 12648 | return finish_base_specifier (TREE_TYPE (type), access, virtual_p); |
a723baf1 MM |
12649 | } |
12650 | ||
12651 | /* Exception handling [gram.exception] */ | |
12652 | ||
12653 | /* Parse an (optional) exception-specification. | |
12654 | ||
12655 | exception-specification: | |
12656 | throw ( type-id-list [opt] ) | |
12657 | ||
12658 | Returns a TREE_LIST representing the exception-specification. The | |
12659 | TREE_VALUE of each node is a type. */ | |
12660 | ||
12661 | static tree | |
94edc4ab | 12662 | cp_parser_exception_specification_opt (cp_parser* parser) |
a723baf1 MM |
12663 | { |
12664 | cp_token *token; | |
12665 | tree type_id_list; | |
12666 | ||
12667 | /* Peek at the next token. */ | |
12668 | token = cp_lexer_peek_token (parser->lexer); | |
12669 | /* If it's not `throw', then there's no exception-specification. */ | |
12670 | if (!cp_parser_is_keyword (token, RID_THROW)) | |
12671 | return NULL_TREE; | |
12672 | ||
12673 | /* Consume the `throw'. */ | |
12674 | cp_lexer_consume_token (parser->lexer); | |
12675 | ||
12676 | /* Look for the `('. */ | |
12677 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
12678 | ||
12679 | /* Peek at the next token. */ | |
12680 | token = cp_lexer_peek_token (parser->lexer); | |
12681 | /* If it's not a `)', then there is a type-id-list. */ | |
12682 | if (token->type != CPP_CLOSE_PAREN) | |
12683 | { | |
12684 | const char *saved_message; | |
12685 | ||
12686 | /* Types may not be defined in an exception-specification. */ | |
12687 | saved_message = parser->type_definition_forbidden_message; | |
12688 | parser->type_definition_forbidden_message | |
12689 | = "types may not be defined in an exception-specification"; | |
12690 | /* Parse the type-id-list. */ | |
12691 | type_id_list = cp_parser_type_id_list (parser); | |
12692 | /* Restore the saved message. */ | |
12693 | parser->type_definition_forbidden_message = saved_message; | |
12694 | } | |
12695 | else | |
12696 | type_id_list = empty_except_spec; | |
12697 | ||
12698 | /* Look for the `)'. */ | |
12699 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
12700 | ||
12701 | return type_id_list; | |
12702 | } | |
12703 | ||
12704 | /* Parse an (optional) type-id-list. | |
12705 | ||
12706 | type-id-list: | |
12707 | type-id | |
12708 | type-id-list , type-id | |
12709 | ||
12710 | Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE, | |
12711 | in the order that the types were presented. */ | |
12712 | ||
12713 | static tree | |
94edc4ab | 12714 | cp_parser_type_id_list (cp_parser* parser) |
a723baf1 MM |
12715 | { |
12716 | tree types = NULL_TREE; | |
12717 | ||
12718 | while (true) | |
12719 | { | |
12720 | cp_token *token; | |
12721 | tree type; | |
12722 | ||
12723 | /* Get the next type-id. */ | |
12724 | type = cp_parser_type_id (parser); | |
12725 | /* Add it to the list. */ | |
12726 | types = add_exception_specifier (types, type, /*complain=*/1); | |
12727 | /* Peek at the next token. */ | |
12728 | token = cp_lexer_peek_token (parser->lexer); | |
12729 | /* If it is not a `,', we are done. */ | |
12730 | if (token->type != CPP_COMMA) | |
12731 | break; | |
12732 | /* Consume the `,'. */ | |
12733 | cp_lexer_consume_token (parser->lexer); | |
12734 | } | |
12735 | ||
12736 | return nreverse (types); | |
12737 | } | |
12738 | ||
12739 | /* Parse a try-block. | |
12740 | ||
12741 | try-block: | |
12742 | try compound-statement handler-seq */ | |
12743 | ||
12744 | static tree | |
94edc4ab | 12745 | cp_parser_try_block (cp_parser* parser) |
a723baf1 MM |
12746 | { |
12747 | tree try_block; | |
12748 | ||
12749 | cp_parser_require_keyword (parser, RID_TRY, "`try'"); | |
12750 | try_block = begin_try_block (); | |
a5bcc582 | 12751 | cp_parser_compound_statement (parser, false); |
a723baf1 MM |
12752 | finish_try_block (try_block); |
12753 | cp_parser_handler_seq (parser); | |
12754 | finish_handler_sequence (try_block); | |
12755 | ||
12756 | return try_block; | |
12757 | } | |
12758 | ||
12759 | /* Parse a function-try-block. | |
12760 | ||
12761 | function-try-block: | |
12762 | try ctor-initializer [opt] function-body handler-seq */ | |
12763 | ||
12764 | static bool | |
94edc4ab | 12765 | cp_parser_function_try_block (cp_parser* parser) |
a723baf1 MM |
12766 | { |
12767 | tree try_block; | |
12768 | bool ctor_initializer_p; | |
12769 | ||
12770 | /* Look for the `try' keyword. */ | |
12771 | if (!cp_parser_require_keyword (parser, RID_TRY, "`try'")) | |
12772 | return false; | |
12773 | /* Let the rest of the front-end know where we are. */ | |
12774 | try_block = begin_function_try_block (); | |
12775 | /* Parse the function-body. */ | |
12776 | ctor_initializer_p | |
12777 | = cp_parser_ctor_initializer_opt_and_function_body (parser); | |
12778 | /* We're done with the `try' part. */ | |
12779 | finish_function_try_block (try_block); | |
12780 | /* Parse the handlers. */ | |
12781 | cp_parser_handler_seq (parser); | |
12782 | /* We're done with the handlers. */ | |
12783 | finish_function_handler_sequence (try_block); | |
12784 | ||
12785 | return ctor_initializer_p; | |
12786 | } | |
12787 | ||
12788 | /* Parse a handler-seq. | |
12789 | ||
12790 | handler-seq: | |
12791 | handler handler-seq [opt] */ | |
12792 | ||
12793 | static void | |
94edc4ab | 12794 | cp_parser_handler_seq (cp_parser* parser) |
a723baf1 MM |
12795 | { |
12796 | while (true) | |
12797 | { | |
12798 | cp_token *token; | |
12799 | ||
12800 | /* Parse the handler. */ | |
12801 | cp_parser_handler (parser); | |
12802 | /* Peek at the next token. */ | |
12803 | token = cp_lexer_peek_token (parser->lexer); | |
12804 | /* If it's not `catch' then there are no more handlers. */ | |
12805 | if (!cp_parser_is_keyword (token, RID_CATCH)) | |
12806 | break; | |
12807 | } | |
12808 | } | |
12809 | ||
12810 | /* Parse a handler. | |
12811 | ||
12812 | handler: | |
12813 | catch ( exception-declaration ) compound-statement */ | |
12814 | ||
12815 | static void | |
94edc4ab | 12816 | cp_parser_handler (cp_parser* parser) |
a723baf1 MM |
12817 | { |
12818 | tree handler; | |
12819 | tree declaration; | |
12820 | ||
12821 | cp_parser_require_keyword (parser, RID_CATCH, "`catch'"); | |
12822 | handler = begin_handler (); | |
12823 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
12824 | declaration = cp_parser_exception_declaration (parser); | |
12825 | finish_handler_parms (declaration, handler); | |
12826 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
a5bcc582 | 12827 | cp_parser_compound_statement (parser, false); |
a723baf1 MM |
12828 | finish_handler (handler); |
12829 | } | |
12830 | ||
12831 | /* Parse an exception-declaration. | |
12832 | ||
12833 | exception-declaration: | |
12834 | type-specifier-seq declarator | |
12835 | type-specifier-seq abstract-declarator | |
12836 | type-specifier-seq | |
12837 | ... | |
12838 | ||
12839 | Returns a VAR_DECL for the declaration, or NULL_TREE if the | |
12840 | ellipsis variant is used. */ | |
12841 | ||
12842 | static tree | |
94edc4ab | 12843 | cp_parser_exception_declaration (cp_parser* parser) |
a723baf1 MM |
12844 | { |
12845 | tree type_specifiers; | |
12846 | tree declarator; | |
12847 | const char *saved_message; | |
12848 | ||
12849 | /* If it's an ellipsis, it's easy to handle. */ | |
12850 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
12851 | { | |
12852 | /* Consume the `...' token. */ | |
12853 | cp_lexer_consume_token (parser->lexer); | |
12854 | return NULL_TREE; | |
12855 | } | |
12856 | ||
12857 | /* Types may not be defined in exception-declarations. */ | |
12858 | saved_message = parser->type_definition_forbidden_message; | |
12859 | parser->type_definition_forbidden_message | |
12860 | = "types may not be defined in exception-declarations"; | |
12861 | ||
12862 | /* Parse the type-specifier-seq. */ | |
12863 | type_specifiers = cp_parser_type_specifier_seq (parser); | |
12864 | /* If it's a `)', then there is no declarator. */ | |
12865 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)) | |
12866 | declarator = NULL_TREE; | |
12867 | else | |
62b8a44e | 12868 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER, |
4bb8ca28 MM |
12869 | /*ctor_dtor_or_conv_p=*/NULL, |
12870 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
12871 | |
12872 | /* Restore the saved message. */ | |
12873 | parser->type_definition_forbidden_message = saved_message; | |
12874 | ||
12875 | return start_handler_parms (type_specifiers, declarator); | |
12876 | } | |
12877 | ||
12878 | /* Parse a throw-expression. | |
12879 | ||
12880 | throw-expression: | |
34cd5ae7 | 12881 | throw assignment-expression [opt] |
a723baf1 MM |
12882 | |
12883 | Returns a THROW_EXPR representing the throw-expression. */ | |
12884 | ||
12885 | static tree | |
94edc4ab | 12886 | cp_parser_throw_expression (cp_parser* parser) |
a723baf1 MM |
12887 | { |
12888 | tree expression; | |
89f1a6ec | 12889 | cp_token* token; |
a723baf1 MM |
12890 | |
12891 | cp_parser_require_keyword (parser, RID_THROW, "`throw'"); | |
89f1a6ec MM |
12892 | token = cp_lexer_peek_token (parser->lexer); |
12893 | /* Figure out whether or not there is an assignment-expression | |
12894 | following the "throw" keyword. */ | |
12895 | if (token->type == CPP_COMMA | |
12896 | || token->type == CPP_SEMICOLON | |
12897 | || token->type == CPP_CLOSE_PAREN | |
12898 | || token->type == CPP_CLOSE_SQUARE | |
12899 | || token->type == CPP_CLOSE_BRACE | |
12900 | || token->type == CPP_COLON) | |
a723baf1 | 12901 | expression = NULL_TREE; |
89f1a6ec MM |
12902 | else |
12903 | expression = cp_parser_assignment_expression (parser); | |
a723baf1 MM |
12904 | |
12905 | return build_throw (expression); | |
12906 | } | |
12907 | ||
12908 | /* GNU Extensions */ | |
12909 | ||
12910 | /* Parse an (optional) asm-specification. | |
12911 | ||
12912 | asm-specification: | |
12913 | asm ( string-literal ) | |
12914 | ||
12915 | If the asm-specification is present, returns a STRING_CST | |
12916 | corresponding to the string-literal. Otherwise, returns | |
12917 | NULL_TREE. */ | |
12918 | ||
12919 | static tree | |
94edc4ab | 12920 | cp_parser_asm_specification_opt (cp_parser* parser) |
a723baf1 MM |
12921 | { |
12922 | cp_token *token; | |
12923 | tree asm_specification; | |
12924 | ||
12925 | /* Peek at the next token. */ | |
12926 | token = cp_lexer_peek_token (parser->lexer); | |
12927 | /* If the next token isn't the `asm' keyword, then there's no | |
12928 | asm-specification. */ | |
12929 | if (!cp_parser_is_keyword (token, RID_ASM)) | |
12930 | return NULL_TREE; | |
12931 | ||
12932 | /* Consume the `asm' token. */ | |
12933 | cp_lexer_consume_token (parser->lexer); | |
12934 | /* Look for the `('. */ | |
12935 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
12936 | ||
12937 | /* Look for the string-literal. */ | |
12938 | token = cp_parser_require (parser, CPP_STRING, "string-literal"); | |
12939 | if (token) | |
12940 | asm_specification = token->value; | |
12941 | else | |
12942 | asm_specification = NULL_TREE; | |
12943 | ||
12944 | /* Look for the `)'. */ | |
12945 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`('"); | |
12946 | ||
12947 | return asm_specification; | |
12948 | } | |
12949 | ||
12950 | /* Parse an asm-operand-list. | |
12951 | ||
12952 | asm-operand-list: | |
12953 | asm-operand | |
12954 | asm-operand-list , asm-operand | |
12955 | ||
12956 | asm-operand: | |
12957 | string-literal ( expression ) | |
12958 | [ string-literal ] string-literal ( expression ) | |
12959 | ||
12960 | Returns a TREE_LIST representing the operands. The TREE_VALUE of | |
12961 | each node is the expression. The TREE_PURPOSE is itself a | |
12962 | TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed | |
12963 | string-literal (or NULL_TREE if not present) and whose TREE_VALUE | |
12964 | is a STRING_CST for the string literal before the parenthesis. */ | |
12965 | ||
12966 | static tree | |
94edc4ab | 12967 | cp_parser_asm_operand_list (cp_parser* parser) |
a723baf1 MM |
12968 | { |
12969 | tree asm_operands = NULL_TREE; | |
12970 | ||
12971 | while (true) | |
12972 | { | |
12973 | tree string_literal; | |
12974 | tree expression; | |
12975 | tree name; | |
12976 | cp_token *token; | |
12977 | ||
12978 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
12979 | { | |
12980 | /* Consume the `[' token. */ | |
12981 | cp_lexer_consume_token (parser->lexer); | |
12982 | /* Read the operand name. */ | |
12983 | name = cp_parser_identifier (parser); | |
12984 | if (name != error_mark_node) | |
12985 | name = build_string (IDENTIFIER_LENGTH (name), | |
12986 | IDENTIFIER_POINTER (name)); | |
12987 | /* Look for the closing `]'. */ | |
12988 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
12989 | } | |
12990 | else | |
12991 | name = NULL_TREE; | |
12992 | /* Look for the string-literal. */ | |
12993 | token = cp_parser_require (parser, CPP_STRING, "string-literal"); | |
12994 | string_literal = token ? token->value : error_mark_node; | |
12995 | /* Look for the `('. */ | |
12996 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
12997 | /* Parse the expression. */ | |
12998 | expression = cp_parser_expression (parser); | |
12999 | /* Look for the `)'. */ | |
13000 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
13001 | /* Add this operand to the list. */ | |
13002 | asm_operands = tree_cons (build_tree_list (name, string_literal), | |
13003 | expression, | |
13004 | asm_operands); | |
13005 | /* If the next token is not a `,', there are no more | |
13006 | operands. */ | |
13007 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
13008 | break; | |
13009 | /* Consume the `,'. */ | |
13010 | cp_lexer_consume_token (parser->lexer); | |
13011 | } | |
13012 | ||
13013 | return nreverse (asm_operands); | |
13014 | } | |
13015 | ||
13016 | /* Parse an asm-clobber-list. | |
13017 | ||
13018 | asm-clobber-list: | |
13019 | string-literal | |
13020 | asm-clobber-list , string-literal | |
13021 | ||
13022 | Returns a TREE_LIST, indicating the clobbers in the order that they | |
13023 | appeared. The TREE_VALUE of each node is a STRING_CST. */ | |
13024 | ||
13025 | static tree | |
94edc4ab | 13026 | cp_parser_asm_clobber_list (cp_parser* parser) |
a723baf1 MM |
13027 | { |
13028 | tree clobbers = NULL_TREE; | |
13029 | ||
13030 | while (true) | |
13031 | { | |
13032 | cp_token *token; | |
13033 | tree string_literal; | |
13034 | ||
13035 | /* Look for the string literal. */ | |
13036 | token = cp_parser_require (parser, CPP_STRING, "string-literal"); | |
13037 | string_literal = token ? token->value : error_mark_node; | |
13038 | /* Add it to the list. */ | |
13039 | clobbers = tree_cons (NULL_TREE, string_literal, clobbers); | |
13040 | /* If the next token is not a `,', then the list is | |
13041 | complete. */ | |
13042 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
13043 | break; | |
13044 | /* Consume the `,' token. */ | |
13045 | cp_lexer_consume_token (parser->lexer); | |
13046 | } | |
13047 | ||
13048 | return clobbers; | |
13049 | } | |
13050 | ||
13051 | /* Parse an (optional) series of attributes. | |
13052 | ||
13053 | attributes: | |
13054 | attributes attribute | |
13055 | ||
13056 | attribute: | |
13057 | __attribute__ (( attribute-list [opt] )) | |
13058 | ||
13059 | The return value is as for cp_parser_attribute_list. */ | |
13060 | ||
13061 | static tree | |
94edc4ab | 13062 | cp_parser_attributes_opt (cp_parser* parser) |
a723baf1 MM |
13063 | { |
13064 | tree attributes = NULL_TREE; | |
13065 | ||
13066 | while (true) | |
13067 | { | |
13068 | cp_token *token; | |
13069 | tree attribute_list; | |
13070 | ||
13071 | /* Peek at the next token. */ | |
13072 | token = cp_lexer_peek_token (parser->lexer); | |
13073 | /* If it's not `__attribute__', then we're done. */ | |
13074 | if (token->keyword != RID_ATTRIBUTE) | |
13075 | break; | |
13076 | ||
13077 | /* Consume the `__attribute__' keyword. */ | |
13078 | cp_lexer_consume_token (parser->lexer); | |
13079 | /* Look for the two `(' tokens. */ | |
13080 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13081 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13082 | ||
13083 | /* Peek at the next token. */ | |
13084 | token = cp_lexer_peek_token (parser->lexer); | |
13085 | if (token->type != CPP_CLOSE_PAREN) | |
13086 | /* Parse the attribute-list. */ | |
13087 | attribute_list = cp_parser_attribute_list (parser); | |
13088 | else | |
13089 | /* If the next token is a `)', then there is no attribute | |
13090 | list. */ | |
13091 | attribute_list = NULL; | |
13092 | ||
13093 | /* Look for the two `)' tokens. */ | |
13094 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
13095 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
13096 | ||
13097 | /* Add these new attributes to the list. */ | |
13098 | attributes = chainon (attributes, attribute_list); | |
13099 | } | |
13100 | ||
13101 | return attributes; | |
13102 | } | |
13103 | ||
13104 | /* Parse an attribute-list. | |
13105 | ||
13106 | attribute-list: | |
13107 | attribute | |
13108 | attribute-list , attribute | |
13109 | ||
13110 | attribute: | |
13111 | identifier | |
13112 | identifier ( identifier ) | |
13113 | identifier ( identifier , expression-list ) | |
13114 | identifier ( expression-list ) | |
13115 | ||
13116 | Returns a TREE_LIST. Each node corresponds to an attribute. THe | |
13117 | TREE_PURPOSE of each node is the identifier indicating which | |
13118 | attribute is in use. The TREE_VALUE represents the arguments, if | |
13119 | any. */ | |
13120 | ||
13121 | static tree | |
94edc4ab | 13122 | cp_parser_attribute_list (cp_parser* parser) |
a723baf1 MM |
13123 | { |
13124 | tree attribute_list = NULL_TREE; | |
13125 | ||
13126 | while (true) | |
13127 | { | |
13128 | cp_token *token; | |
13129 | tree identifier; | |
13130 | tree attribute; | |
13131 | ||
13132 | /* Look for the identifier. We also allow keywords here; for | |
13133 | example `__attribute__ ((const))' is legal. */ | |
13134 | token = cp_lexer_peek_token (parser->lexer); | |
13135 | if (token->type != CPP_NAME | |
13136 | && token->type != CPP_KEYWORD) | |
13137 | return error_mark_node; | |
13138 | /* Consume the token. */ | |
13139 | token = cp_lexer_consume_token (parser->lexer); | |
13140 | ||
13141 | /* Save away the identifier that indicates which attribute this is. */ | |
13142 | identifier = token->value; | |
13143 | attribute = build_tree_list (identifier, NULL_TREE); | |
13144 | ||
13145 | /* Peek at the next token. */ | |
13146 | token = cp_lexer_peek_token (parser->lexer); | |
13147 | /* If it's an `(', then parse the attribute arguments. */ | |
13148 | if (token->type == CPP_OPEN_PAREN) | |
13149 | { | |
13150 | tree arguments; | |
a723baf1 | 13151 | |
39703eb9 MM |
13152 | arguments = (cp_parser_parenthesized_expression_list |
13153 | (parser, true, /*non_constant_p=*/NULL)); | |
a723baf1 MM |
13154 | /* Save the identifier and arguments away. */ |
13155 | TREE_VALUE (attribute) = arguments; | |
a723baf1 MM |
13156 | } |
13157 | ||
13158 | /* Add this attribute to the list. */ | |
13159 | TREE_CHAIN (attribute) = attribute_list; | |
13160 | attribute_list = attribute; | |
13161 | ||
13162 | /* Now, look for more attributes. */ | |
13163 | token = cp_lexer_peek_token (parser->lexer); | |
13164 | /* If the next token isn't a `,', we're done. */ | |
13165 | if (token->type != CPP_COMMA) | |
13166 | break; | |
13167 | ||
cd0be382 | 13168 | /* Consume the comma and keep going. */ |
a723baf1 MM |
13169 | cp_lexer_consume_token (parser->lexer); |
13170 | } | |
13171 | ||
13172 | /* We built up the list in reverse order. */ | |
13173 | return nreverse (attribute_list); | |
13174 | } | |
13175 | ||
13176 | /* Parse an optional `__extension__' keyword. Returns TRUE if it is | |
13177 | present, and FALSE otherwise. *SAVED_PEDANTIC is set to the | |
13178 | current value of the PEDANTIC flag, regardless of whether or not | |
13179 | the `__extension__' keyword is present. The caller is responsible | |
13180 | for restoring the value of the PEDANTIC flag. */ | |
13181 | ||
13182 | static bool | |
94edc4ab | 13183 | cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic) |
a723baf1 MM |
13184 | { |
13185 | /* Save the old value of the PEDANTIC flag. */ | |
13186 | *saved_pedantic = pedantic; | |
13187 | ||
13188 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION)) | |
13189 | { | |
13190 | /* Consume the `__extension__' token. */ | |
13191 | cp_lexer_consume_token (parser->lexer); | |
13192 | /* We're not being pedantic while the `__extension__' keyword is | |
13193 | in effect. */ | |
13194 | pedantic = 0; | |
13195 | ||
13196 | return true; | |
13197 | } | |
13198 | ||
13199 | return false; | |
13200 | } | |
13201 | ||
13202 | /* Parse a label declaration. | |
13203 | ||
13204 | label-declaration: | |
13205 | __label__ label-declarator-seq ; | |
13206 | ||
13207 | label-declarator-seq: | |
13208 | identifier , label-declarator-seq | |
13209 | identifier */ | |
13210 | ||
13211 | static void | |
94edc4ab | 13212 | cp_parser_label_declaration (cp_parser* parser) |
a723baf1 MM |
13213 | { |
13214 | /* Look for the `__label__' keyword. */ | |
13215 | cp_parser_require_keyword (parser, RID_LABEL, "`__label__'"); | |
13216 | ||
13217 | while (true) | |
13218 | { | |
13219 | tree identifier; | |
13220 | ||
13221 | /* Look for an identifier. */ | |
13222 | identifier = cp_parser_identifier (parser); | |
13223 | /* Declare it as a lobel. */ | |
13224 | finish_label_decl (identifier); | |
13225 | /* If the next token is a `;', stop. */ | |
13226 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
13227 | break; | |
13228 | /* Look for the `,' separating the label declarations. */ | |
13229 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
13230 | } | |
13231 | ||
13232 | /* Look for the final `;'. */ | |
13233 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
13234 | } | |
13235 | ||
13236 | /* Support Functions */ | |
13237 | ||
13238 | /* Looks up NAME in the current scope, as given by PARSER->SCOPE. | |
13239 | NAME should have one of the representations used for an | |
13240 | id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE | |
13241 | is returned. If PARSER->SCOPE is a dependent type, then a | |
13242 | SCOPE_REF is returned. | |
13243 | ||
13244 | If NAME is a TEMPLATE_ID_EXPR, then it will be immediately | |
13245 | returned; the name was already resolved when the TEMPLATE_ID_EXPR | |
13246 | was formed. Abstractly, such entities should not be passed to this | |
13247 | function, because they do not need to be looked up, but it is | |
13248 | simpler to check for this special case here, rather than at the | |
13249 | call-sites. | |
13250 | ||
13251 | In cases not explicitly covered above, this function returns a | |
13252 | DECL, OVERLOAD, or baselink representing the result of the lookup. | |
13253 | If there was no entity with the indicated NAME, the ERROR_MARK_NODE | |
13254 | is returned. | |
13255 | ||
a723baf1 MM |
13256 | If IS_TYPE is TRUE, bindings that do not refer to types are |
13257 | ignored. | |
13258 | ||
b0bc6e8e KL |
13259 | If IS_TEMPLATE is TRUE, bindings that do not refer to templates are |
13260 | ignored. | |
13261 | ||
eea9800f MM |
13262 | If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces |
13263 | are ignored. | |
13264 | ||
a723baf1 MM |
13265 | If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent |
13266 | types. */ | |
13267 | ||
13268 | static tree | |
8d241e0b | 13269 | cp_parser_lookup_name (cp_parser *parser, tree name, |
b0bc6e8e KL |
13270 | bool is_type, bool is_template, bool is_namespace, |
13271 | bool check_dependency) | |
a723baf1 MM |
13272 | { |
13273 | tree decl; | |
13274 | tree object_type = parser->context->object_type; | |
13275 | ||
13276 | /* Now that we have looked up the name, the OBJECT_TYPE (if any) is | |
13277 | no longer valid. Note that if we are parsing tentatively, and | |
13278 | the parse fails, OBJECT_TYPE will be automatically restored. */ | |
13279 | parser->context->object_type = NULL_TREE; | |
13280 | ||
13281 | if (name == error_mark_node) | |
13282 | return error_mark_node; | |
13283 | ||
13284 | /* A template-id has already been resolved; there is no lookup to | |
13285 | do. */ | |
13286 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
13287 | return name; | |
13288 | if (BASELINK_P (name)) | |
13289 | { | |
13290 | my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name)) | |
13291 | == TEMPLATE_ID_EXPR), | |
13292 | 20020909); | |
13293 | return name; | |
13294 | } | |
13295 | ||
13296 | /* A BIT_NOT_EXPR is used to represent a destructor. By this point, | |
13297 | it should already have been checked to make sure that the name | |
13298 | used matches the type being destroyed. */ | |
13299 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
13300 | { | |
13301 | tree type; | |
13302 | ||
13303 | /* Figure out to which type this destructor applies. */ | |
13304 | if (parser->scope) | |
13305 | type = parser->scope; | |
13306 | else if (object_type) | |
13307 | type = object_type; | |
13308 | else | |
13309 | type = current_class_type; | |
13310 | /* If that's not a class type, there is no destructor. */ | |
13311 | if (!type || !CLASS_TYPE_P (type)) | |
13312 | return error_mark_node; | |
13313 | /* If it was a class type, return the destructor. */ | |
13314 | return CLASSTYPE_DESTRUCTORS (type); | |
13315 | } | |
13316 | ||
13317 | /* By this point, the NAME should be an ordinary identifier. If | |
13318 | the id-expression was a qualified name, the qualifying scope is | |
13319 | stored in PARSER->SCOPE at this point. */ | |
13320 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, | |
13321 | 20000619); | |
13322 | ||
13323 | /* Perform the lookup. */ | |
13324 | if (parser->scope) | |
13325 | { | |
1fb3244a | 13326 | bool dependent_p; |
a723baf1 MM |
13327 | |
13328 | if (parser->scope == error_mark_node) | |
13329 | return error_mark_node; | |
13330 | ||
13331 | /* If the SCOPE is dependent, the lookup must be deferred until | |
13332 | the template is instantiated -- unless we are explicitly | |
13333 | looking up names in uninstantiated templates. Even then, we | |
13334 | cannot look up the name if the scope is not a class type; it | |
13335 | might, for example, be a template type parameter. */ | |
1fb3244a MM |
13336 | dependent_p = (TYPE_P (parser->scope) |
13337 | && !(parser->in_declarator_p | |
13338 | && currently_open_class (parser->scope)) | |
13339 | && dependent_type_p (parser->scope)); | |
a723baf1 | 13340 | if ((check_dependency || !CLASS_TYPE_P (parser->scope)) |
1fb3244a | 13341 | && dependent_p) |
a723baf1 | 13342 | { |
b0bc6e8e | 13343 | if (is_type) |
a723baf1 MM |
13344 | /* The resolution to Core Issue 180 says that `struct A::B' |
13345 | should be considered a type-name, even if `A' is | |
13346 | dependent. */ | |
13347 | decl = TYPE_NAME (make_typename_type (parser->scope, | |
13348 | name, | |
13349 | /*complain=*/1)); | |
b0bc6e8e | 13350 | else if (is_template) |
5b4acce1 KL |
13351 | decl = make_unbound_class_template (parser->scope, |
13352 | name, | |
13353 | /*complain=*/1); | |
b0bc6e8e KL |
13354 | else |
13355 | decl = build_nt (SCOPE_REF, parser->scope, name); | |
a723baf1 MM |
13356 | } |
13357 | else | |
13358 | { | |
13359 | /* If PARSER->SCOPE is a dependent type, then it must be a | |
13360 | class type, and we must not be checking dependencies; | |
13361 | otherwise, we would have processed this lookup above. So | |
13362 | that PARSER->SCOPE is not considered a dependent base by | |
13363 | lookup_member, we must enter the scope here. */ | |
1fb3244a | 13364 | if (dependent_p) |
a723baf1 MM |
13365 | push_scope (parser->scope); |
13366 | /* If the PARSER->SCOPE is a a template specialization, it | |
13367 | may be instantiated during name lookup. In that case, | |
13368 | errors may be issued. Even if we rollback the current | |
13369 | tentative parse, those errors are valid. */ | |
5e08432e MM |
13370 | decl = lookup_qualified_name (parser->scope, name, is_type, |
13371 | /*complain=*/true); | |
1fb3244a | 13372 | if (dependent_p) |
a723baf1 MM |
13373 | pop_scope (parser->scope); |
13374 | } | |
13375 | parser->qualifying_scope = parser->scope; | |
13376 | parser->object_scope = NULL_TREE; | |
13377 | } | |
13378 | else if (object_type) | |
13379 | { | |
13380 | tree object_decl = NULL_TREE; | |
13381 | /* Look up the name in the scope of the OBJECT_TYPE, unless the | |
13382 | OBJECT_TYPE is not a class. */ | |
13383 | if (CLASS_TYPE_P (object_type)) | |
13384 | /* If the OBJECT_TYPE is a template specialization, it may | |
13385 | be instantiated during name lookup. In that case, errors | |
13386 | may be issued. Even if we rollback the current tentative | |
13387 | parse, those errors are valid. */ | |
13388 | object_decl = lookup_member (object_type, | |
13389 | name, | |
13390 | /*protect=*/0, is_type); | |
13391 | /* Look it up in the enclosing context, too. */ | |
13392 | decl = lookup_name_real (name, is_type, /*nonclass=*/0, | |
eea9800f | 13393 | is_namespace, |
a723baf1 MM |
13394 | /*flags=*/0); |
13395 | parser->object_scope = object_type; | |
13396 | parser->qualifying_scope = NULL_TREE; | |
13397 | if (object_decl) | |
13398 | decl = object_decl; | |
13399 | } | |
13400 | else | |
13401 | { | |
13402 | decl = lookup_name_real (name, is_type, /*nonclass=*/0, | |
eea9800f | 13403 | is_namespace, |
a723baf1 MM |
13404 | /*flags=*/0); |
13405 | parser->qualifying_scope = NULL_TREE; | |
13406 | parser->object_scope = NULL_TREE; | |
13407 | } | |
13408 | ||
13409 | /* If the lookup failed, let our caller know. */ | |
13410 | if (!decl | |
13411 | || decl == error_mark_node | |
13412 | || (TREE_CODE (decl) == FUNCTION_DECL | |
13413 | && DECL_ANTICIPATED (decl))) | |
13414 | return error_mark_node; | |
13415 | ||
13416 | /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ | |
13417 | if (TREE_CODE (decl) == TREE_LIST) | |
13418 | { | |
13419 | /* The error message we have to print is too complicated for | |
13420 | cp_parser_error, so we incorporate its actions directly. */ | |
e5976695 | 13421 | if (!cp_parser_simulate_error (parser)) |
a723baf1 MM |
13422 | { |
13423 | error ("reference to `%D' is ambiguous", name); | |
13424 | print_candidates (decl); | |
13425 | } | |
13426 | return error_mark_node; | |
13427 | } | |
13428 | ||
13429 | my_friendly_assert (DECL_P (decl) | |
13430 | || TREE_CODE (decl) == OVERLOAD | |
13431 | || TREE_CODE (decl) == SCOPE_REF | |
5b4acce1 | 13432 | || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE |
a723baf1 MM |
13433 | || BASELINK_P (decl), |
13434 | 20000619); | |
13435 | ||
13436 | /* If we have resolved the name of a member declaration, check to | |
13437 | see if the declaration is accessible. When the name resolves to | |
34cd5ae7 | 13438 | set of overloaded functions, accessibility is checked when |
a723baf1 MM |
13439 | overload resolution is done. |
13440 | ||
13441 | During an explicit instantiation, access is not checked at all, | |
13442 | as per [temp.explicit]. */ | |
8d241e0b | 13443 | if (DECL_P (decl)) |
ee76b931 | 13444 | check_accessibility_of_qualified_id (decl, object_type, parser->scope); |
a723baf1 MM |
13445 | |
13446 | return decl; | |
13447 | } | |
13448 | ||
13449 | /* Like cp_parser_lookup_name, but for use in the typical case where | |
b0bc6e8e KL |
13450 | CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE, |
13451 | IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */ | |
a723baf1 MM |
13452 | |
13453 | static tree | |
94edc4ab | 13454 | cp_parser_lookup_name_simple (cp_parser* parser, tree name) |
a723baf1 MM |
13455 | { |
13456 | return cp_parser_lookup_name (parser, name, | |
eea9800f | 13457 | /*is_type=*/false, |
b0bc6e8e | 13458 | /*is_template=*/false, |
eea9800f | 13459 | /*is_namespace=*/false, |
a723baf1 MM |
13460 | /*check_dependency=*/true); |
13461 | } | |
13462 | ||
a723baf1 MM |
13463 | /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in |
13464 | the current context, return the TYPE_DECL. If TAG_NAME_P is | |
13465 | true, the DECL indicates the class being defined in a class-head, | |
13466 | or declared in an elaborated-type-specifier. | |
13467 | ||
13468 | Otherwise, return DECL. */ | |
13469 | ||
13470 | static tree | |
13471 | cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p) | |
13472 | { | |
710b73e6 KL |
13473 | /* If the TEMPLATE_DECL is being declared as part of a class-head, |
13474 | the translation from TEMPLATE_DECL to TYPE_DECL occurs: | |
a723baf1 MM |
13475 | |
13476 | struct A { | |
13477 | template <typename T> struct B; | |
13478 | }; | |
13479 | ||
13480 | template <typename T> struct A::B {}; | |
13481 | ||
13482 | Similarly, in a elaborated-type-specifier: | |
13483 | ||
13484 | namespace N { struct X{}; } | |
13485 | ||
13486 | struct A { | |
13487 | template <typename T> friend struct N::X; | |
13488 | }; | |
13489 | ||
710b73e6 KL |
13490 | However, if the DECL refers to a class type, and we are in |
13491 | the scope of the class, then the name lookup automatically | |
13492 | finds the TYPE_DECL created by build_self_reference rather | |
13493 | than a TEMPLATE_DECL. For example, in: | |
13494 | ||
13495 | template <class T> struct S { | |
13496 | S s; | |
13497 | }; | |
13498 | ||
13499 | there is no need to handle such case. */ | |
13500 | ||
13501 | if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p) | |
a723baf1 MM |
13502 | return DECL_TEMPLATE_RESULT (decl); |
13503 | ||
13504 | return decl; | |
13505 | } | |
13506 | ||
13507 | /* If too many, or too few, template-parameter lists apply to the | |
13508 | declarator, issue an error message. Returns TRUE if all went well, | |
13509 | and FALSE otherwise. */ | |
13510 | ||
13511 | static bool | |
94edc4ab NN |
13512 | cp_parser_check_declarator_template_parameters (cp_parser* parser, |
13513 | tree declarator) | |
a723baf1 MM |
13514 | { |
13515 | unsigned num_templates; | |
13516 | ||
13517 | /* We haven't seen any classes that involve template parameters yet. */ | |
13518 | num_templates = 0; | |
13519 | ||
13520 | switch (TREE_CODE (declarator)) | |
13521 | { | |
13522 | case CALL_EXPR: | |
13523 | case ARRAY_REF: | |
13524 | case INDIRECT_REF: | |
13525 | case ADDR_EXPR: | |
13526 | { | |
13527 | tree main_declarator = TREE_OPERAND (declarator, 0); | |
13528 | return | |
13529 | cp_parser_check_declarator_template_parameters (parser, | |
13530 | main_declarator); | |
13531 | } | |
13532 | ||
13533 | case SCOPE_REF: | |
13534 | { | |
13535 | tree scope; | |
13536 | tree member; | |
13537 | ||
13538 | scope = TREE_OPERAND (declarator, 0); | |
13539 | member = TREE_OPERAND (declarator, 1); | |
13540 | ||
13541 | /* If this is a pointer-to-member, then we are not interested | |
13542 | in the SCOPE, because it does not qualify the thing that is | |
13543 | being declared. */ | |
13544 | if (TREE_CODE (member) == INDIRECT_REF) | |
13545 | return (cp_parser_check_declarator_template_parameters | |
13546 | (parser, member)); | |
13547 | ||
13548 | while (scope && CLASS_TYPE_P (scope)) | |
13549 | { | |
13550 | /* You're supposed to have one `template <...>' | |
13551 | for every template class, but you don't need one | |
13552 | for a full specialization. For example: | |
13553 | ||
13554 | template <class T> struct S{}; | |
13555 | template <> struct S<int> { void f(); }; | |
13556 | void S<int>::f () {} | |
13557 | ||
13558 | is correct; there shouldn't be a `template <>' for | |
13559 | the definition of `S<int>::f'. */ | |
13560 | if (CLASSTYPE_TEMPLATE_INFO (scope) | |
13561 | && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope) | |
13562 | || uses_template_parms (CLASSTYPE_TI_ARGS (scope))) | |
13563 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))) | |
13564 | ++num_templates; | |
13565 | ||
13566 | scope = TYPE_CONTEXT (scope); | |
13567 | } | |
13568 | } | |
13569 | ||
13570 | /* Fall through. */ | |
13571 | ||
13572 | default: | |
13573 | /* If the DECLARATOR has the form `X<y>' then it uses one | |
13574 | additional level of template parameters. */ | |
13575 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
13576 | ++num_templates; | |
13577 | ||
13578 | return cp_parser_check_template_parameters (parser, | |
13579 | num_templates); | |
13580 | } | |
13581 | } | |
13582 | ||
13583 | /* NUM_TEMPLATES were used in the current declaration. If that is | |
13584 | invalid, return FALSE and issue an error messages. Otherwise, | |
13585 | return TRUE. */ | |
13586 | ||
13587 | static bool | |
94edc4ab NN |
13588 | cp_parser_check_template_parameters (cp_parser* parser, |
13589 | unsigned num_templates) | |
a723baf1 MM |
13590 | { |
13591 | /* If there are more template classes than parameter lists, we have | |
13592 | something like: | |
13593 | ||
13594 | template <class T> void S<T>::R<T>::f (); */ | |
13595 | if (parser->num_template_parameter_lists < num_templates) | |
13596 | { | |
13597 | error ("too few template-parameter-lists"); | |
13598 | return false; | |
13599 | } | |
13600 | /* If there are the same number of template classes and parameter | |
13601 | lists, that's OK. */ | |
13602 | if (parser->num_template_parameter_lists == num_templates) | |
13603 | return true; | |
13604 | /* If there are more, but only one more, then we are referring to a | |
13605 | member template. That's OK too. */ | |
13606 | if (parser->num_template_parameter_lists == num_templates + 1) | |
13607 | return true; | |
13608 | /* Otherwise, there are too many template parameter lists. We have | |
13609 | something like: | |
13610 | ||
13611 | template <class T> template <class U> void S::f(); */ | |
13612 | error ("too many template-parameter-lists"); | |
13613 | return false; | |
13614 | } | |
13615 | ||
13616 | /* Parse a binary-expression of the general form: | |
13617 | ||
13618 | binary-expression: | |
13619 | <expr> | |
13620 | binary-expression <token> <expr> | |
13621 | ||
13622 | The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used | |
13623 | to parser the <expr>s. If the first production is used, then the | |
13624 | value returned by FN is returned directly. Otherwise, a node with | |
13625 | the indicated EXPR_TYPE is returned, with operands corresponding to | |
13626 | the two sub-expressions. */ | |
13627 | ||
13628 | static tree | |
94edc4ab NN |
13629 | cp_parser_binary_expression (cp_parser* parser, |
13630 | const cp_parser_token_tree_map token_tree_map, | |
13631 | cp_parser_expression_fn fn) | |
a723baf1 MM |
13632 | { |
13633 | tree lhs; | |
13634 | ||
13635 | /* Parse the first expression. */ | |
13636 | lhs = (*fn) (parser); | |
13637 | /* Now, look for more expressions. */ | |
13638 | while (true) | |
13639 | { | |
13640 | cp_token *token; | |
39b1af70 | 13641 | const cp_parser_token_tree_map_node *map_node; |
a723baf1 MM |
13642 | tree rhs; |
13643 | ||
13644 | /* Peek at the next token. */ | |
13645 | token = cp_lexer_peek_token (parser->lexer); | |
13646 | /* If the token is `>', and that's not an operator at the | |
13647 | moment, then we're done. */ | |
13648 | if (token->type == CPP_GREATER | |
13649 | && !parser->greater_than_is_operator_p) | |
13650 | break; | |
34cd5ae7 | 13651 | /* If we find one of the tokens we want, build the corresponding |
a723baf1 MM |
13652 | tree representation. */ |
13653 | for (map_node = token_tree_map; | |
13654 | map_node->token_type != CPP_EOF; | |
13655 | ++map_node) | |
13656 | if (map_node->token_type == token->type) | |
13657 | { | |
13658 | /* Consume the operator token. */ | |
13659 | cp_lexer_consume_token (parser->lexer); | |
13660 | /* Parse the right-hand side of the expression. */ | |
13661 | rhs = (*fn) (parser); | |
13662 | /* Build the binary tree node. */ | |
13663 | lhs = build_x_binary_op (map_node->tree_type, lhs, rhs); | |
13664 | break; | |
13665 | } | |
13666 | ||
13667 | /* If the token wasn't one of the ones we want, we're done. */ | |
13668 | if (map_node->token_type == CPP_EOF) | |
13669 | break; | |
13670 | } | |
13671 | ||
13672 | return lhs; | |
13673 | } | |
13674 | ||
13675 | /* Parse an optional `::' token indicating that the following name is | |
13676 | from the global namespace. If so, PARSER->SCOPE is set to the | |
13677 | GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE, | |
13678 | unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone. | |
13679 | Returns the new value of PARSER->SCOPE, if the `::' token is | |
13680 | present, and NULL_TREE otherwise. */ | |
13681 | ||
13682 | static tree | |
94edc4ab | 13683 | cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p) |
a723baf1 MM |
13684 | { |
13685 | cp_token *token; | |
13686 | ||
13687 | /* Peek at the next token. */ | |
13688 | token = cp_lexer_peek_token (parser->lexer); | |
13689 | /* If we're looking at a `::' token then we're starting from the | |
13690 | global namespace, not our current location. */ | |
13691 | if (token->type == CPP_SCOPE) | |
13692 | { | |
13693 | /* Consume the `::' token. */ | |
13694 | cp_lexer_consume_token (parser->lexer); | |
13695 | /* Set the SCOPE so that we know where to start the lookup. */ | |
13696 | parser->scope = global_namespace; | |
13697 | parser->qualifying_scope = global_namespace; | |
13698 | parser->object_scope = NULL_TREE; | |
13699 | ||
13700 | return parser->scope; | |
13701 | } | |
13702 | else if (!current_scope_valid_p) | |
13703 | { | |
13704 | parser->scope = NULL_TREE; | |
13705 | parser->qualifying_scope = NULL_TREE; | |
13706 | parser->object_scope = NULL_TREE; | |
13707 | } | |
13708 | ||
13709 | return NULL_TREE; | |
13710 | } | |
13711 | ||
13712 | /* Returns TRUE if the upcoming token sequence is the start of a | |
13713 | constructor declarator. If FRIEND_P is true, the declarator is | |
13714 | preceded by the `friend' specifier. */ | |
13715 | ||
13716 | static bool | |
13717 | cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p) | |
13718 | { | |
13719 | bool constructor_p; | |
13720 | tree type_decl = NULL_TREE; | |
13721 | bool nested_name_p; | |
2050a1bb MM |
13722 | cp_token *next_token; |
13723 | ||
13724 | /* The common case is that this is not a constructor declarator, so | |
8fbc5ae7 MM |
13725 | try to avoid doing lots of work if at all possible. It's not |
13726 | valid declare a constructor at function scope. */ | |
13727 | if (at_function_scope_p ()) | |
13728 | return false; | |
13729 | /* And only certain tokens can begin a constructor declarator. */ | |
2050a1bb MM |
13730 | next_token = cp_lexer_peek_token (parser->lexer); |
13731 | if (next_token->type != CPP_NAME | |
13732 | && next_token->type != CPP_SCOPE | |
13733 | && next_token->type != CPP_NESTED_NAME_SPECIFIER | |
13734 | && next_token->type != CPP_TEMPLATE_ID) | |
13735 | return false; | |
a723baf1 MM |
13736 | |
13737 | /* Parse tentatively; we are going to roll back all of the tokens | |
13738 | consumed here. */ | |
13739 | cp_parser_parse_tentatively (parser); | |
13740 | /* Assume that we are looking at a constructor declarator. */ | |
13741 | constructor_p = true; | |
8d241e0b | 13742 | |
a723baf1 MM |
13743 | /* Look for the optional `::' operator. */ |
13744 | cp_parser_global_scope_opt (parser, | |
13745 | /*current_scope_valid_p=*/false); | |
13746 | /* Look for the nested-name-specifier. */ | |
13747 | nested_name_p | |
13748 | = (cp_parser_nested_name_specifier_opt (parser, | |
13749 | /*typename_keyword_p=*/false, | |
13750 | /*check_dependency_p=*/false, | |
a668c6ad MM |
13751 | /*type_p=*/false, |
13752 | /*is_declaration=*/false) | |
a723baf1 MM |
13753 | != NULL_TREE); |
13754 | /* Outside of a class-specifier, there must be a | |
13755 | nested-name-specifier. */ | |
13756 | if (!nested_name_p && | |
13757 | (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type) | |
13758 | || friend_p)) | |
13759 | constructor_p = false; | |
13760 | /* If we still think that this might be a constructor-declarator, | |
13761 | look for a class-name. */ | |
13762 | if (constructor_p) | |
13763 | { | |
13764 | /* If we have: | |
13765 | ||
8fbc5ae7 | 13766 | template <typename T> struct S { S(); }; |
a723baf1 MM |
13767 | template <typename T> S<T>::S (); |
13768 | ||
13769 | we must recognize that the nested `S' names a class. | |
13770 | Similarly, for: | |
13771 | ||
13772 | template <typename T> S<T>::S<T> (); | |
13773 | ||
13774 | we must recognize that the nested `S' names a template. */ | |
13775 | type_decl = cp_parser_class_name (parser, | |
13776 | /*typename_keyword_p=*/false, | |
13777 | /*template_keyword_p=*/false, | |
13778 | /*type_p=*/false, | |
a723baf1 | 13779 | /*check_dependency_p=*/false, |
a668c6ad MM |
13780 | /*class_head_p=*/false, |
13781 | /*is_declaration=*/false); | |
a723baf1 MM |
13782 | /* If there was no class-name, then this is not a constructor. */ |
13783 | constructor_p = !cp_parser_error_occurred (parser); | |
13784 | } | |
8d241e0b | 13785 | |
a723baf1 MM |
13786 | /* If we're still considering a constructor, we have to see a `(', |
13787 | to begin the parameter-declaration-clause, followed by either a | |
13788 | `)', an `...', or a decl-specifier. We need to check for a | |
13789 | type-specifier to avoid being fooled into thinking that: | |
13790 | ||
13791 | S::S (f) (int); | |
13792 | ||
13793 | is a constructor. (It is actually a function named `f' that | |
13794 | takes one parameter (of type `int') and returns a value of type | |
13795 | `S::S'. */ | |
13796 | if (constructor_p | |
13797 | && cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) | |
13798 | { | |
13799 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN) | |
13800 | && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS) | |
13801 | && !cp_parser_storage_class_specifier_opt (parser)) | |
13802 | { | |
5dae1114 | 13803 | tree type; |
4047b164 | 13804 | unsigned saved_num_template_parameter_lists; |
5dae1114 MM |
13805 | |
13806 | /* Names appearing in the type-specifier should be looked up | |
13807 | in the scope of the class. */ | |
13808 | if (current_class_type) | |
13809 | type = NULL_TREE; | |
a723baf1 MM |
13810 | else |
13811 | { | |
5dae1114 MM |
13812 | type = TREE_TYPE (type_decl); |
13813 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
14d22dd6 MM |
13814 | { |
13815 | type = resolve_typename_type (type, | |
13816 | /*only_current_p=*/false); | |
13817 | if (type == error_mark_node) | |
13818 | { | |
13819 | cp_parser_abort_tentative_parse (parser); | |
13820 | return false; | |
13821 | } | |
13822 | } | |
5dae1114 | 13823 | push_scope (type); |
a723baf1 | 13824 | } |
4047b164 KL |
13825 | |
13826 | /* Inside the constructor parameter list, surrounding | |
13827 | template-parameter-lists do not apply. */ | |
13828 | saved_num_template_parameter_lists | |
13829 | = parser->num_template_parameter_lists; | |
13830 | parser->num_template_parameter_lists = 0; | |
13831 | ||
5dae1114 MM |
13832 | /* Look for the type-specifier. */ |
13833 | cp_parser_type_specifier (parser, | |
13834 | CP_PARSER_FLAGS_NONE, | |
13835 | /*is_friend=*/false, | |
13836 | /*is_declarator=*/true, | |
13837 | /*declares_class_or_enum=*/NULL, | |
13838 | /*is_cv_qualifier=*/NULL); | |
4047b164 KL |
13839 | |
13840 | parser->num_template_parameter_lists | |
13841 | = saved_num_template_parameter_lists; | |
13842 | ||
5dae1114 MM |
13843 | /* Leave the scope of the class. */ |
13844 | if (type) | |
13845 | pop_scope (type); | |
13846 | ||
13847 | constructor_p = !cp_parser_error_occurred (parser); | |
a723baf1 MM |
13848 | } |
13849 | } | |
13850 | else | |
13851 | constructor_p = false; | |
13852 | /* We did not really want to consume any tokens. */ | |
13853 | cp_parser_abort_tentative_parse (parser); | |
13854 | ||
13855 | return constructor_p; | |
13856 | } | |
13857 | ||
13858 | /* Parse the definition of the function given by the DECL_SPECIFIERS, | |
cf22909c | 13859 | ATTRIBUTES, and DECLARATOR. The access checks have been deferred; |
a723baf1 MM |
13860 | they must be performed once we are in the scope of the function. |
13861 | ||
13862 | Returns the function defined. */ | |
13863 | ||
13864 | static tree | |
13865 | cp_parser_function_definition_from_specifiers_and_declarator | |
94edc4ab NN |
13866 | (cp_parser* parser, |
13867 | tree decl_specifiers, | |
13868 | tree attributes, | |
13869 | tree declarator) | |
a723baf1 MM |
13870 | { |
13871 | tree fn; | |
13872 | bool success_p; | |
13873 | ||
13874 | /* Begin the function-definition. */ | |
13875 | success_p = begin_function_definition (decl_specifiers, | |
13876 | attributes, | |
13877 | declarator); | |
13878 | ||
13879 | /* If there were names looked up in the decl-specifier-seq that we | |
13880 | did not check, check them now. We must wait until we are in the | |
13881 | scope of the function to perform the checks, since the function | |
13882 | might be a friend. */ | |
cf22909c | 13883 | perform_deferred_access_checks (); |
a723baf1 MM |
13884 | |
13885 | if (!success_p) | |
13886 | { | |
13887 | /* If begin_function_definition didn't like the definition, skip | |
13888 | the entire function. */ | |
13889 | error ("invalid function declaration"); | |
13890 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
13891 | fn = error_mark_node; | |
13892 | } | |
13893 | else | |
13894 | fn = cp_parser_function_definition_after_declarator (parser, | |
13895 | /*inline_p=*/false); | |
13896 | ||
13897 | return fn; | |
13898 | } | |
13899 | ||
13900 | /* Parse the part of a function-definition that follows the | |
13901 | declarator. INLINE_P is TRUE iff this function is an inline | |
13902 | function defined with a class-specifier. | |
13903 | ||
13904 | Returns the function defined. */ | |
13905 | ||
13906 | static tree | |
94edc4ab NN |
13907 | cp_parser_function_definition_after_declarator (cp_parser* parser, |
13908 | bool inline_p) | |
a723baf1 MM |
13909 | { |
13910 | tree fn; | |
13911 | bool ctor_initializer_p = false; | |
13912 | bool saved_in_unbraced_linkage_specification_p; | |
13913 | unsigned saved_num_template_parameter_lists; | |
13914 | ||
13915 | /* If the next token is `return', then the code may be trying to | |
13916 | make use of the "named return value" extension that G++ used to | |
13917 | support. */ | |
13918 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN)) | |
13919 | { | |
13920 | /* Consume the `return' keyword. */ | |
13921 | cp_lexer_consume_token (parser->lexer); | |
13922 | /* Look for the identifier that indicates what value is to be | |
13923 | returned. */ | |
13924 | cp_parser_identifier (parser); | |
13925 | /* Issue an error message. */ | |
13926 | error ("named return values are no longer supported"); | |
13927 | /* Skip tokens until we reach the start of the function body. */ | |
21eb631b MM |
13928 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE) |
13929 | && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF)) | |
a723baf1 MM |
13930 | cp_lexer_consume_token (parser->lexer); |
13931 | } | |
13932 | /* The `extern' in `extern "C" void f () { ... }' does not apply to | |
13933 | anything declared inside `f'. */ | |
13934 | saved_in_unbraced_linkage_specification_p | |
13935 | = parser->in_unbraced_linkage_specification_p; | |
13936 | parser->in_unbraced_linkage_specification_p = false; | |
13937 | /* Inside the function, surrounding template-parameter-lists do not | |
13938 | apply. */ | |
13939 | saved_num_template_parameter_lists | |
13940 | = parser->num_template_parameter_lists; | |
13941 | parser->num_template_parameter_lists = 0; | |
13942 | /* If the next token is `try', then we are looking at a | |
13943 | function-try-block. */ | |
13944 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY)) | |
13945 | ctor_initializer_p = cp_parser_function_try_block (parser); | |
13946 | /* A function-try-block includes the function-body, so we only do | |
13947 | this next part if we're not processing a function-try-block. */ | |
13948 | else | |
13949 | ctor_initializer_p | |
13950 | = cp_parser_ctor_initializer_opt_and_function_body (parser); | |
13951 | ||
13952 | /* Finish the function. */ | |
13953 | fn = finish_function ((ctor_initializer_p ? 1 : 0) | | |
13954 | (inline_p ? 2 : 0)); | |
13955 | /* Generate code for it, if necessary. */ | |
8cd2462c | 13956 | expand_or_defer_fn (fn); |
a723baf1 MM |
13957 | /* Restore the saved values. */ |
13958 | parser->in_unbraced_linkage_specification_p | |
13959 | = saved_in_unbraced_linkage_specification_p; | |
13960 | parser->num_template_parameter_lists | |
13961 | = saved_num_template_parameter_lists; | |
13962 | ||
13963 | return fn; | |
13964 | } | |
13965 | ||
13966 | /* Parse a template-declaration, assuming that the `export' (and | |
13967 | `extern') keywords, if present, has already been scanned. MEMBER_P | |
13968 | is as for cp_parser_template_declaration. */ | |
13969 | ||
13970 | static void | |
94edc4ab | 13971 | cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p) |
a723baf1 MM |
13972 | { |
13973 | tree decl = NULL_TREE; | |
13974 | tree parameter_list; | |
13975 | bool friend_p = false; | |
13976 | ||
13977 | /* Look for the `template' keyword. */ | |
13978 | if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'")) | |
13979 | return; | |
13980 | ||
13981 | /* And the `<'. */ | |
13982 | if (!cp_parser_require (parser, CPP_LESS, "`<'")) | |
13983 | return; | |
13984 | ||
a723baf1 MM |
13985 | /* If the next token is `>', then we have an invalid |
13986 | specialization. Rather than complain about an invalid template | |
13987 | parameter, issue an error message here. */ | |
13988 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) | |
13989 | { | |
13990 | cp_parser_error (parser, "invalid explicit specialization"); | |
2f9afd51 | 13991 | begin_specialization (); |
a723baf1 MM |
13992 | parameter_list = NULL_TREE; |
13993 | } | |
13994 | else | |
2f9afd51 KL |
13995 | { |
13996 | /* Parse the template parameters. */ | |
13997 | begin_template_parm_list (); | |
13998 | parameter_list = cp_parser_template_parameter_list (parser); | |
13999 | parameter_list = end_template_parm_list (parameter_list); | |
14000 | } | |
14001 | ||
a723baf1 MM |
14002 | /* Look for the `>'. */ |
14003 | cp_parser_skip_until_found (parser, CPP_GREATER, "`>'"); | |
14004 | /* We just processed one more parameter list. */ | |
14005 | ++parser->num_template_parameter_lists; | |
14006 | /* If the next token is `template', there are more template | |
14007 | parameters. */ | |
14008 | if (cp_lexer_next_token_is_keyword (parser->lexer, | |
14009 | RID_TEMPLATE)) | |
14010 | cp_parser_template_declaration_after_export (parser, member_p); | |
14011 | else | |
14012 | { | |
14013 | decl = cp_parser_single_declaration (parser, | |
14014 | member_p, | |
14015 | &friend_p); | |
14016 | ||
14017 | /* If this is a member template declaration, let the front | |
14018 | end know. */ | |
14019 | if (member_p && !friend_p && decl) | |
37d407a1 KL |
14020 | { |
14021 | if (TREE_CODE (decl) == TYPE_DECL) | |
14022 | cp_parser_check_access_in_redeclaration (decl); | |
14023 | ||
14024 | decl = finish_member_template_decl (decl); | |
14025 | } | |
a723baf1 | 14026 | else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL) |
19db77ce KL |
14027 | make_friend_class (current_class_type, TREE_TYPE (decl), |
14028 | /*complain=*/true); | |
a723baf1 MM |
14029 | } |
14030 | /* We are done with the current parameter list. */ | |
14031 | --parser->num_template_parameter_lists; | |
14032 | ||
14033 | /* Finish up. */ | |
14034 | finish_template_decl (parameter_list); | |
14035 | ||
14036 | /* Register member declarations. */ | |
14037 | if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl)) | |
14038 | finish_member_declaration (decl); | |
14039 | ||
14040 | /* If DECL is a function template, we must return to parse it later. | |
14041 | (Even though there is no definition, there might be default | |
14042 | arguments that need handling.) */ | |
14043 | if (member_p && decl | |
14044 | && (TREE_CODE (decl) == FUNCTION_DECL | |
14045 | || DECL_FUNCTION_TEMPLATE_P (decl))) | |
14046 | TREE_VALUE (parser->unparsed_functions_queues) | |
8218bd34 | 14047 | = tree_cons (NULL_TREE, decl, |
a723baf1 MM |
14048 | TREE_VALUE (parser->unparsed_functions_queues)); |
14049 | } | |
14050 | ||
14051 | /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or | |
14052 | `function-definition' sequence. MEMBER_P is true, this declaration | |
14053 | appears in a class scope. | |
14054 | ||
14055 | Returns the DECL for the declared entity. If FRIEND_P is non-NULL, | |
14056 | *FRIEND_P is set to TRUE iff the declaration is a friend. */ | |
14057 | ||
14058 | static tree | |
94edc4ab NN |
14059 | cp_parser_single_declaration (cp_parser* parser, |
14060 | bool member_p, | |
14061 | bool* friend_p) | |
a723baf1 | 14062 | { |
560ad596 | 14063 | int declares_class_or_enum; |
a723baf1 MM |
14064 | tree decl = NULL_TREE; |
14065 | tree decl_specifiers; | |
14066 | tree attributes; | |
4bb8ca28 | 14067 | bool function_definition_p = false; |
a723baf1 | 14068 | |
a723baf1 | 14069 | /* Defer access checks until we know what is being declared. */ |
8d241e0b | 14070 | push_deferring_access_checks (dk_deferred); |
cf22909c | 14071 | |
a723baf1 MM |
14072 | /* Try the `decl-specifier-seq [opt] init-declarator [opt]' |
14073 | alternative. */ | |
14074 | decl_specifiers | |
14075 | = cp_parser_decl_specifier_seq (parser, | |
14076 | CP_PARSER_FLAGS_OPTIONAL, | |
14077 | &attributes, | |
14078 | &declares_class_or_enum); | |
4bb8ca28 MM |
14079 | if (friend_p) |
14080 | *friend_p = cp_parser_friend_p (decl_specifiers); | |
a723baf1 MM |
14081 | /* Gather up the access checks that occurred the |
14082 | decl-specifier-seq. */ | |
cf22909c KL |
14083 | stop_deferring_access_checks (); |
14084 | ||
a723baf1 MM |
14085 | /* Check for the declaration of a template class. */ |
14086 | if (declares_class_or_enum) | |
14087 | { | |
14088 | if (cp_parser_declares_only_class_p (parser)) | |
14089 | { | |
14090 | decl = shadow_tag (decl_specifiers); | |
14091 | if (decl) | |
14092 | decl = TYPE_NAME (decl); | |
14093 | else | |
14094 | decl = error_mark_node; | |
14095 | } | |
14096 | } | |
14097 | else | |
14098 | decl = NULL_TREE; | |
14099 | /* If it's not a template class, try for a template function. If | |
14100 | the next token is a `;', then this declaration does not declare | |
14101 | anything. But, if there were errors in the decl-specifiers, then | |
14102 | the error might well have come from an attempted class-specifier. | |
14103 | In that case, there's no need to warn about a missing declarator. */ | |
14104 | if (!decl | |
14105 | && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON) | |
14106 | || !value_member (error_mark_node, decl_specifiers))) | |
14107 | decl = cp_parser_init_declarator (parser, | |
14108 | decl_specifiers, | |
14109 | attributes, | |
4bb8ca28 | 14110 | /*function_definition_allowed_p=*/true, |
a723baf1 | 14111 | member_p, |
560ad596 | 14112 | declares_class_or_enum, |
4bb8ca28 | 14113 | &function_definition_p); |
cf22909c KL |
14114 | |
14115 | pop_deferring_access_checks (); | |
14116 | ||
a723baf1 MM |
14117 | /* Clear any current qualification; whatever comes next is the start |
14118 | of something new. */ | |
14119 | parser->scope = NULL_TREE; | |
14120 | parser->qualifying_scope = NULL_TREE; | |
14121 | parser->object_scope = NULL_TREE; | |
14122 | /* Look for a trailing `;' after the declaration. */ | |
4bb8ca28 MM |
14123 | if (!function_definition_p |
14124 | && !cp_parser_require (parser, CPP_SEMICOLON, "`;'")) | |
a723baf1 | 14125 | cp_parser_skip_to_end_of_block_or_statement (parser); |
a723baf1 MM |
14126 | |
14127 | return decl; | |
14128 | } | |
14129 | ||
d6b4ea85 MM |
14130 | /* Parse a cast-expression that is not the operand of a unary "&". */ |
14131 | ||
14132 | static tree | |
14133 | cp_parser_simple_cast_expression (cp_parser *parser) | |
14134 | { | |
14135 | return cp_parser_cast_expression (parser, /*address_p=*/false); | |
14136 | } | |
14137 | ||
a723baf1 MM |
14138 | /* Parse a functional cast to TYPE. Returns an expression |
14139 | representing the cast. */ | |
14140 | ||
14141 | static tree | |
94edc4ab | 14142 | cp_parser_functional_cast (cp_parser* parser, tree type) |
a723baf1 MM |
14143 | { |
14144 | tree expression_list; | |
14145 | ||
39703eb9 MM |
14146 | expression_list |
14147 | = cp_parser_parenthesized_expression_list (parser, false, | |
14148 | /*non_constant_p=*/NULL); | |
a723baf1 MM |
14149 | |
14150 | return build_functional_cast (type, expression_list); | |
14151 | } | |
14152 | ||
4bb8ca28 MM |
14153 | /* Save the tokens that make up the body of a member function defined |
14154 | in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have | |
14155 | already been parsed. The ATTRIBUTES are any GNU "__attribute__" | |
14156 | specifiers applied to the declaration. Returns the FUNCTION_DECL | |
14157 | for the member function. */ | |
14158 | ||
14159 | tree | |
14160 | cp_parser_save_member_function_body (cp_parser* parser, | |
14161 | tree decl_specifiers, | |
14162 | tree declarator, | |
14163 | tree attributes) | |
14164 | { | |
14165 | cp_token_cache *cache; | |
14166 | tree fn; | |
14167 | ||
14168 | /* Create the function-declaration. */ | |
14169 | fn = start_method (decl_specifiers, declarator, attributes); | |
14170 | /* If something went badly wrong, bail out now. */ | |
14171 | if (fn == error_mark_node) | |
14172 | { | |
14173 | /* If there's a function-body, skip it. */ | |
14174 | if (cp_parser_token_starts_function_definition_p | |
14175 | (cp_lexer_peek_token (parser->lexer))) | |
14176 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
14177 | return error_mark_node; | |
14178 | } | |
14179 | ||
14180 | /* Remember it, if there default args to post process. */ | |
14181 | cp_parser_save_default_args (parser, fn); | |
14182 | ||
14183 | /* Create a token cache. */ | |
14184 | cache = cp_token_cache_new (); | |
14185 | /* Save away the tokens that make up the body of the | |
14186 | function. */ | |
14187 | cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0); | |
14188 | /* Handle function try blocks. */ | |
14189 | while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH)) | |
14190 | cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0); | |
14191 | ||
14192 | /* Save away the inline definition; we will process it when the | |
14193 | class is complete. */ | |
14194 | DECL_PENDING_INLINE_INFO (fn) = cache; | |
14195 | DECL_PENDING_INLINE_P (fn) = 1; | |
14196 | ||
14197 | /* We need to know that this was defined in the class, so that | |
14198 | friend templates are handled correctly. */ | |
14199 | DECL_INITIALIZED_IN_CLASS_P (fn) = 1; | |
14200 | ||
14201 | /* We're done with the inline definition. */ | |
14202 | finish_method (fn); | |
14203 | ||
14204 | /* Add FN to the queue of functions to be parsed later. */ | |
14205 | TREE_VALUE (parser->unparsed_functions_queues) | |
14206 | = tree_cons (NULL_TREE, fn, | |
14207 | TREE_VALUE (parser->unparsed_functions_queues)); | |
14208 | ||
14209 | return fn; | |
14210 | } | |
14211 | ||
ec75414f MM |
14212 | /* Parse a template-argument-list, as well as the trailing ">" (but |
14213 | not the opening ">"). See cp_parser_template_argument_list for the | |
14214 | return value. */ | |
14215 | ||
14216 | static tree | |
14217 | cp_parser_enclosed_template_argument_list (cp_parser* parser) | |
14218 | { | |
14219 | tree arguments; | |
14220 | tree saved_scope; | |
14221 | tree saved_qualifying_scope; | |
14222 | tree saved_object_scope; | |
14223 | bool saved_greater_than_is_operator_p; | |
14224 | ||
14225 | /* [temp.names] | |
14226 | ||
14227 | When parsing a template-id, the first non-nested `>' is taken as | |
14228 | the end of the template-argument-list rather than a greater-than | |
14229 | operator. */ | |
14230 | saved_greater_than_is_operator_p | |
14231 | = parser->greater_than_is_operator_p; | |
14232 | parser->greater_than_is_operator_p = false; | |
14233 | /* Parsing the argument list may modify SCOPE, so we save it | |
14234 | here. */ | |
14235 | saved_scope = parser->scope; | |
14236 | saved_qualifying_scope = parser->qualifying_scope; | |
14237 | saved_object_scope = parser->object_scope; | |
14238 | /* Parse the template-argument-list itself. */ | |
14239 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) | |
14240 | arguments = NULL_TREE; | |
14241 | else | |
14242 | arguments = cp_parser_template_argument_list (parser); | |
4d5297fa GB |
14243 | /* Look for the `>' that ends the template-argument-list. If we find |
14244 | a '>>' instead, it's probably just a typo. */ | |
14245 | if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) | |
14246 | { | |
14247 | if (!saved_greater_than_is_operator_p) | |
14248 | { | |
14249 | /* If we're in a nested template argument list, the '>>' has to be | |
14250 | a typo for '> >'. We emit the error message, but we continue | |
14251 | parsing and we push a '>' as next token, so that the argument | |
14252 | list will be parsed correctly.. */ | |
14253 | cp_token* token; | |
14254 | error ("`>>' should be `> >' within a nested template argument list"); | |
14255 | token = cp_lexer_peek_token (parser->lexer); | |
14256 | token->type = CPP_GREATER; | |
14257 | } | |
14258 | else | |
14259 | { | |
14260 | /* If this is not a nested template argument list, the '>>' is | |
14261 | a typo for '>'. Emit an error message and continue. */ | |
14262 | error ("spurious `>>', use `>' to terminate a template argument list"); | |
14263 | cp_lexer_consume_token (parser->lexer); | |
14264 | } | |
14265 | } | |
14266 | else | |
14267 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
ec75414f MM |
14268 | /* The `>' token might be a greater-than operator again now. */ |
14269 | parser->greater_than_is_operator_p | |
14270 | = saved_greater_than_is_operator_p; | |
14271 | /* Restore the SAVED_SCOPE. */ | |
14272 | parser->scope = saved_scope; | |
14273 | parser->qualifying_scope = saved_qualifying_scope; | |
14274 | parser->object_scope = saved_object_scope; | |
14275 | ||
14276 | return arguments; | |
14277 | } | |
14278 | ||
a723baf1 MM |
14279 | /* MEMBER_FUNCTION is a member function, or a friend. If default |
14280 | arguments, or the body of the function have not yet been parsed, | |
14281 | parse them now. */ | |
14282 | ||
14283 | static void | |
94edc4ab | 14284 | cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function) |
a723baf1 MM |
14285 | { |
14286 | cp_lexer *saved_lexer; | |
14287 | ||
14288 | /* If this member is a template, get the underlying | |
14289 | FUNCTION_DECL. */ | |
14290 | if (DECL_FUNCTION_TEMPLATE_P (member_function)) | |
14291 | member_function = DECL_TEMPLATE_RESULT (member_function); | |
14292 | ||
14293 | /* There should not be any class definitions in progress at this | |
14294 | point; the bodies of members are only parsed outside of all class | |
14295 | definitions. */ | |
14296 | my_friendly_assert (parser->num_classes_being_defined == 0, 20010816); | |
14297 | /* While we're parsing the member functions we might encounter more | |
14298 | classes. We want to handle them right away, but we don't want | |
14299 | them getting mixed up with functions that are currently in the | |
14300 | queue. */ | |
14301 | parser->unparsed_functions_queues | |
14302 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
14303 | ||
14304 | /* Make sure that any template parameters are in scope. */ | |
14305 | maybe_begin_member_template_processing (member_function); | |
14306 | ||
a723baf1 MM |
14307 | /* If the body of the function has not yet been parsed, parse it |
14308 | now. */ | |
14309 | if (DECL_PENDING_INLINE_P (member_function)) | |
14310 | { | |
14311 | tree function_scope; | |
14312 | cp_token_cache *tokens; | |
14313 | ||
14314 | /* The function is no longer pending; we are processing it. */ | |
14315 | tokens = DECL_PENDING_INLINE_INFO (member_function); | |
14316 | DECL_PENDING_INLINE_INFO (member_function) = NULL; | |
14317 | DECL_PENDING_INLINE_P (member_function) = 0; | |
14318 | /* If this was an inline function in a local class, enter the scope | |
14319 | of the containing function. */ | |
14320 | function_scope = decl_function_context (member_function); | |
14321 | if (function_scope) | |
14322 | push_function_context_to (function_scope); | |
14323 | ||
14324 | /* Save away the current lexer. */ | |
14325 | saved_lexer = parser->lexer; | |
14326 | /* Make a new lexer to feed us the tokens saved for this function. */ | |
14327 | parser->lexer = cp_lexer_new_from_tokens (tokens); | |
14328 | parser->lexer->next = saved_lexer; | |
14329 | ||
14330 | /* Set the current source position to be the location of the first | |
14331 | token in the saved inline body. */ | |
3466b292 | 14332 | cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
14333 | |
14334 | /* Let the front end know that we going to be defining this | |
14335 | function. */ | |
14336 | start_function (NULL_TREE, member_function, NULL_TREE, | |
14337 | SF_PRE_PARSED | SF_INCLASS_INLINE); | |
14338 | ||
14339 | /* Now, parse the body of the function. */ | |
14340 | cp_parser_function_definition_after_declarator (parser, | |
14341 | /*inline_p=*/true); | |
14342 | ||
14343 | /* Leave the scope of the containing function. */ | |
14344 | if (function_scope) | |
14345 | pop_function_context_from (function_scope); | |
14346 | /* Restore the lexer. */ | |
14347 | parser->lexer = saved_lexer; | |
14348 | } | |
14349 | ||
14350 | /* Remove any template parameters from the symbol table. */ | |
14351 | maybe_end_member_template_processing (); | |
14352 | ||
14353 | /* Restore the queue. */ | |
14354 | parser->unparsed_functions_queues | |
14355 | = TREE_CHAIN (parser->unparsed_functions_queues); | |
14356 | } | |
14357 | ||
cd0be382 | 14358 | /* If DECL contains any default args, remember it on the unparsed |
8db1028e NS |
14359 | functions queue. */ |
14360 | ||
14361 | static void | |
14362 | cp_parser_save_default_args (cp_parser* parser, tree decl) | |
14363 | { | |
14364 | tree probe; | |
14365 | ||
14366 | for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
14367 | probe; | |
14368 | probe = TREE_CHAIN (probe)) | |
14369 | if (TREE_PURPOSE (probe)) | |
14370 | { | |
14371 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
14372 | = tree_cons (NULL_TREE, decl, | |
14373 | TREE_PURPOSE (parser->unparsed_functions_queues)); | |
14374 | break; | |
14375 | } | |
14376 | return; | |
14377 | } | |
14378 | ||
8218bd34 MM |
14379 | /* FN is a FUNCTION_DECL which may contains a parameter with an |
14380 | unparsed DEFAULT_ARG. Parse the default args now. */ | |
a723baf1 MM |
14381 | |
14382 | static void | |
8218bd34 | 14383 | cp_parser_late_parsing_default_args (cp_parser *parser, tree fn) |
a723baf1 MM |
14384 | { |
14385 | cp_lexer *saved_lexer; | |
14386 | cp_token_cache *tokens; | |
14387 | bool saved_local_variables_forbidden_p; | |
14388 | tree parameters; | |
8218bd34 | 14389 | |
b92bc2a0 NS |
14390 | /* While we're parsing the default args, we might (due to the |
14391 | statement expression extension) encounter more classes. We want | |
14392 | to handle them right away, but we don't want them getting mixed | |
14393 | up with default args that are currently in the queue. */ | |
14394 | parser->unparsed_functions_queues | |
14395 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
14396 | ||
8218bd34 | 14397 | for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
a723baf1 MM |
14398 | parameters; |
14399 | parameters = TREE_CHAIN (parameters)) | |
14400 | { | |
14401 | if (!TREE_PURPOSE (parameters) | |
14402 | || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG) | |
14403 | continue; | |
14404 | ||
14405 | /* Save away the current lexer. */ | |
14406 | saved_lexer = parser->lexer; | |
14407 | /* Create a new one, using the tokens we have saved. */ | |
14408 | tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters)); | |
14409 | parser->lexer = cp_lexer_new_from_tokens (tokens); | |
14410 | ||
14411 | /* Set the current source position to be the location of the | |
14412 | first token in the default argument. */ | |
3466b292 | 14413 | cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
14414 | |
14415 | /* Local variable names (and the `this' keyword) may not appear | |
14416 | in a default argument. */ | |
14417 | saved_local_variables_forbidden_p = parser->local_variables_forbidden_p; | |
14418 | parser->local_variables_forbidden_p = true; | |
14419 | /* Parse the assignment-expression. */ | |
f128e1f3 | 14420 | if (DECL_CLASS_SCOPE_P (fn)) |
14d22dd6 | 14421 | push_nested_class (DECL_CONTEXT (fn)); |
a723baf1 | 14422 | TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser); |
f128e1f3 | 14423 | if (DECL_CLASS_SCOPE_P (fn)) |
e5976695 | 14424 | pop_nested_class (); |
a723baf1 MM |
14425 | |
14426 | /* Restore saved state. */ | |
14427 | parser->lexer = saved_lexer; | |
14428 | parser->local_variables_forbidden_p = saved_local_variables_forbidden_p; | |
14429 | } | |
b92bc2a0 NS |
14430 | |
14431 | /* Restore the queue. */ | |
14432 | parser->unparsed_functions_queues | |
14433 | = TREE_CHAIN (parser->unparsed_functions_queues); | |
a723baf1 MM |
14434 | } |
14435 | ||
14436 | /* Parse the operand of `sizeof' (or a similar operator). Returns | |
14437 | either a TYPE or an expression, depending on the form of the | |
14438 | input. The KEYWORD indicates which kind of expression we have | |
14439 | encountered. */ | |
14440 | ||
14441 | static tree | |
94edc4ab | 14442 | cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword) |
a723baf1 MM |
14443 | { |
14444 | static const char *format; | |
14445 | tree expr = NULL_TREE; | |
14446 | const char *saved_message; | |
67c03833 | 14447 | bool saved_integral_constant_expression_p; |
a723baf1 MM |
14448 | |
14449 | /* Initialize FORMAT the first time we get here. */ | |
14450 | if (!format) | |
14451 | format = "types may not be defined in `%s' expressions"; | |
14452 | ||
14453 | /* Types cannot be defined in a `sizeof' expression. Save away the | |
14454 | old message. */ | |
14455 | saved_message = parser->type_definition_forbidden_message; | |
14456 | /* And create the new one. */ | |
14457 | parser->type_definition_forbidden_message | |
c68b0a84 KG |
14458 | = xmalloc (strlen (format) |
14459 | + strlen (IDENTIFIER_POINTER (ridpointers[keyword])) | |
14460 | + 1 /* `\0' */); | |
a723baf1 MM |
14461 | sprintf ((char *) parser->type_definition_forbidden_message, |
14462 | format, IDENTIFIER_POINTER (ridpointers[keyword])); | |
14463 | ||
14464 | /* The restrictions on constant-expressions do not apply inside | |
14465 | sizeof expressions. */ | |
67c03833 JM |
14466 | saved_integral_constant_expression_p = parser->integral_constant_expression_p; |
14467 | parser->integral_constant_expression_p = false; | |
a723baf1 | 14468 | |
3beb3abf MM |
14469 | /* Do not actually evaluate the expression. */ |
14470 | ++skip_evaluation; | |
a723baf1 MM |
14471 | /* If it's a `(', then we might be looking at the type-id |
14472 | construction. */ | |
14473 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
14474 | { | |
14475 | tree type; | |
14476 | ||
14477 | /* We can't be sure yet whether we're looking at a type-id or an | |
14478 | expression. */ | |
14479 | cp_parser_parse_tentatively (parser); | |
14480 | /* Consume the `('. */ | |
14481 | cp_lexer_consume_token (parser->lexer); | |
14482 | /* Parse the type-id. */ | |
14483 | type = cp_parser_type_id (parser); | |
14484 | /* Now, look for the trailing `)'. */ | |
14485 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
14486 | /* If all went well, then we're done. */ | |
14487 | if (cp_parser_parse_definitely (parser)) | |
14488 | { | |
14489 | /* Build a list of decl-specifiers; right now, we have only | |
14490 | a single type-specifier. */ | |
14491 | type = build_tree_list (NULL_TREE, | |
14492 | type); | |
14493 | ||
14494 | /* Call grokdeclarator to figure out what type this is. */ | |
14495 | expr = grokdeclarator (NULL_TREE, | |
14496 | type, | |
14497 | TYPENAME, | |
14498 | /*initialized=*/0, | |
14499 | /*attrlist=*/NULL); | |
14500 | } | |
14501 | } | |
14502 | ||
14503 | /* If the type-id production did not work out, then we must be | |
14504 | looking at the unary-expression production. */ | |
14505 | if (!expr) | |
14506 | expr = cp_parser_unary_expression (parser, /*address_p=*/false); | |
3beb3abf MM |
14507 | /* Go back to evaluating expressions. */ |
14508 | --skip_evaluation; | |
a723baf1 MM |
14509 | |
14510 | /* Free the message we created. */ | |
14511 | free ((char *) parser->type_definition_forbidden_message); | |
14512 | /* And restore the old one. */ | |
14513 | parser->type_definition_forbidden_message = saved_message; | |
67c03833 | 14514 | parser->integral_constant_expression_p = saved_integral_constant_expression_p; |
a723baf1 MM |
14515 | |
14516 | return expr; | |
14517 | } | |
14518 | ||
14519 | /* If the current declaration has no declarator, return true. */ | |
14520 | ||
14521 | static bool | |
14522 | cp_parser_declares_only_class_p (cp_parser *parser) | |
14523 | { | |
14524 | /* If the next token is a `;' or a `,' then there is no | |
14525 | declarator. */ | |
14526 | return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) | |
14527 | || cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); | |
14528 | } | |
14529 | ||
d17811fd MM |
14530 | /* Simplify EXPR if it is a non-dependent expression. Returns the |
14531 | (possibly simplified) expression. */ | |
14532 | ||
14533 | static tree | |
14534 | cp_parser_fold_non_dependent_expr (tree expr) | |
14535 | { | |
14536 | /* If we're in a template, but EXPR isn't value dependent, simplify | |
14537 | it. We're supposed to treat: | |
14538 | ||
14539 | template <typename T> void f(T[1 + 1]); | |
14540 | template <typename T> void f(T[2]); | |
14541 | ||
14542 | as two declarations of the same function, for example. */ | |
14543 | if (processing_template_decl | |
14544 | && !type_dependent_expression_p (expr) | |
14545 | && !value_dependent_expression_p (expr)) | |
14546 | { | |
14547 | HOST_WIDE_INT saved_processing_template_decl; | |
14548 | ||
14549 | saved_processing_template_decl = processing_template_decl; | |
14550 | processing_template_decl = 0; | |
14551 | expr = tsubst_copy_and_build (expr, | |
14552 | /*args=*/NULL_TREE, | |
14553 | tf_error, | |
b3445994 MM |
14554 | /*in_decl=*/NULL_TREE, |
14555 | /*function_p=*/false); | |
d17811fd MM |
14556 | processing_template_decl = saved_processing_template_decl; |
14557 | } | |
14558 | return expr; | |
14559 | } | |
14560 | ||
a723baf1 MM |
14561 | /* DECL_SPECIFIERS is the representation of a decl-specifier-seq. |
14562 | Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */ | |
14563 | ||
14564 | static bool | |
94edc4ab | 14565 | cp_parser_friend_p (tree decl_specifiers) |
a723baf1 MM |
14566 | { |
14567 | while (decl_specifiers) | |
14568 | { | |
14569 | /* See if this decl-specifier is `friend'. */ | |
14570 | if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE | |
14571 | && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND) | |
14572 | return true; | |
14573 | ||
14574 | /* Go on to the next decl-specifier. */ | |
14575 | decl_specifiers = TREE_CHAIN (decl_specifiers); | |
14576 | } | |
14577 | ||
14578 | return false; | |
14579 | } | |
14580 | ||
14581 | /* If the next token is of the indicated TYPE, consume it. Otherwise, | |
14582 | issue an error message indicating that TOKEN_DESC was expected. | |
14583 | ||
14584 | Returns the token consumed, if the token had the appropriate type. | |
14585 | Otherwise, returns NULL. */ | |
14586 | ||
14587 | static cp_token * | |
94edc4ab NN |
14588 | cp_parser_require (cp_parser* parser, |
14589 | enum cpp_ttype type, | |
14590 | const char* token_desc) | |
a723baf1 MM |
14591 | { |
14592 | if (cp_lexer_next_token_is (parser->lexer, type)) | |
14593 | return cp_lexer_consume_token (parser->lexer); | |
14594 | else | |
14595 | { | |
e5976695 MM |
14596 | /* Output the MESSAGE -- unless we're parsing tentatively. */ |
14597 | if (!cp_parser_simulate_error (parser)) | |
14598 | error ("expected %s", token_desc); | |
a723baf1 MM |
14599 | return NULL; |
14600 | } | |
14601 | } | |
14602 | ||
14603 | /* Like cp_parser_require, except that tokens will be skipped until | |
14604 | the desired token is found. An error message is still produced if | |
14605 | the next token is not as expected. */ | |
14606 | ||
14607 | static void | |
94edc4ab NN |
14608 | cp_parser_skip_until_found (cp_parser* parser, |
14609 | enum cpp_ttype type, | |
14610 | const char* token_desc) | |
a723baf1 MM |
14611 | { |
14612 | cp_token *token; | |
14613 | unsigned nesting_depth = 0; | |
14614 | ||
14615 | if (cp_parser_require (parser, type, token_desc)) | |
14616 | return; | |
14617 | ||
14618 | /* Skip tokens until the desired token is found. */ | |
14619 | while (true) | |
14620 | { | |
14621 | /* Peek at the next token. */ | |
14622 | token = cp_lexer_peek_token (parser->lexer); | |
14623 | /* If we've reached the token we want, consume it and | |
14624 | stop. */ | |
14625 | if (token->type == type && !nesting_depth) | |
14626 | { | |
14627 | cp_lexer_consume_token (parser->lexer); | |
14628 | return; | |
14629 | } | |
14630 | /* If we've run out of tokens, stop. */ | |
14631 | if (token->type == CPP_EOF) | |
14632 | return; | |
14633 | if (token->type == CPP_OPEN_BRACE | |
14634 | || token->type == CPP_OPEN_PAREN | |
14635 | || token->type == CPP_OPEN_SQUARE) | |
14636 | ++nesting_depth; | |
14637 | else if (token->type == CPP_CLOSE_BRACE | |
14638 | || token->type == CPP_CLOSE_PAREN | |
14639 | || token->type == CPP_CLOSE_SQUARE) | |
14640 | { | |
14641 | if (nesting_depth-- == 0) | |
14642 | return; | |
14643 | } | |
14644 | /* Consume this token. */ | |
14645 | cp_lexer_consume_token (parser->lexer); | |
14646 | } | |
14647 | } | |
14648 | ||
14649 | /* If the next token is the indicated keyword, consume it. Otherwise, | |
14650 | issue an error message indicating that TOKEN_DESC was expected. | |
14651 | ||
14652 | Returns the token consumed, if the token had the appropriate type. | |
14653 | Otherwise, returns NULL. */ | |
14654 | ||
14655 | static cp_token * | |
94edc4ab NN |
14656 | cp_parser_require_keyword (cp_parser* parser, |
14657 | enum rid keyword, | |
14658 | const char* token_desc) | |
a723baf1 MM |
14659 | { |
14660 | cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc); | |
14661 | ||
14662 | if (token && token->keyword != keyword) | |
14663 | { | |
14664 | dyn_string_t error_msg; | |
14665 | ||
14666 | /* Format the error message. */ | |
14667 | error_msg = dyn_string_new (0); | |
14668 | dyn_string_append_cstr (error_msg, "expected "); | |
14669 | dyn_string_append_cstr (error_msg, token_desc); | |
14670 | cp_parser_error (parser, error_msg->s); | |
14671 | dyn_string_delete (error_msg); | |
14672 | return NULL; | |
14673 | } | |
14674 | ||
14675 | return token; | |
14676 | } | |
14677 | ||
14678 | /* Returns TRUE iff TOKEN is a token that can begin the body of a | |
14679 | function-definition. */ | |
14680 | ||
14681 | static bool | |
94edc4ab | 14682 | cp_parser_token_starts_function_definition_p (cp_token* token) |
a723baf1 MM |
14683 | { |
14684 | return (/* An ordinary function-body begins with an `{'. */ | |
14685 | token->type == CPP_OPEN_BRACE | |
14686 | /* A ctor-initializer begins with a `:'. */ | |
14687 | || token->type == CPP_COLON | |
14688 | /* A function-try-block begins with `try'. */ | |
14689 | || token->keyword == RID_TRY | |
14690 | /* The named return value extension begins with `return'. */ | |
14691 | || token->keyword == RID_RETURN); | |
14692 | } | |
14693 | ||
14694 | /* Returns TRUE iff the next token is the ":" or "{" beginning a class | |
14695 | definition. */ | |
14696 | ||
14697 | static bool | |
14698 | cp_parser_next_token_starts_class_definition_p (cp_parser *parser) | |
14699 | { | |
14700 | cp_token *token; | |
14701 | ||
14702 | token = cp_lexer_peek_token (parser->lexer); | |
14703 | return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON); | |
14704 | } | |
14705 | ||
d17811fd | 14706 | /* Returns TRUE iff the next token is the "," or ">" ending a |
4d5297fa GB |
14707 | template-argument. ">>" is also accepted (after the full |
14708 | argument was parsed) because it's probably a typo for "> >", | |
14709 | and there is a specific diagnostic for this. */ | |
d17811fd MM |
14710 | |
14711 | static bool | |
14712 | cp_parser_next_token_ends_template_argument_p (cp_parser *parser) | |
14713 | { | |
14714 | cp_token *token; | |
14715 | ||
14716 | token = cp_lexer_peek_token (parser->lexer); | |
4d5297fa GB |
14717 | return (token->type == CPP_COMMA || token->type == CPP_GREATER |
14718 | || token->type == CPP_RSHIFT); | |
d17811fd MM |
14719 | } |
14720 | ||
a723baf1 MM |
14721 | /* Returns the kind of tag indicated by TOKEN, if it is a class-key, |
14722 | or none_type otherwise. */ | |
14723 | ||
14724 | static enum tag_types | |
94edc4ab | 14725 | cp_parser_token_is_class_key (cp_token* token) |
a723baf1 MM |
14726 | { |
14727 | switch (token->keyword) | |
14728 | { | |
14729 | case RID_CLASS: | |
14730 | return class_type; | |
14731 | case RID_STRUCT: | |
14732 | return record_type; | |
14733 | case RID_UNION: | |
14734 | return union_type; | |
14735 | ||
14736 | default: | |
14737 | return none_type; | |
14738 | } | |
14739 | } | |
14740 | ||
14741 | /* Issue an error message if the CLASS_KEY does not match the TYPE. */ | |
14742 | ||
14743 | static void | |
14744 | cp_parser_check_class_key (enum tag_types class_key, tree type) | |
14745 | { | |
14746 | if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type)) | |
14747 | pedwarn ("`%s' tag used in naming `%#T'", | |
14748 | class_key == union_type ? "union" | |
14749 | : class_key == record_type ? "struct" : "class", | |
14750 | type); | |
14751 | } | |
14752 | ||
cd0be382 | 14753 | /* Issue an error message if DECL is redeclared with different |
37d407a1 KL |
14754 | access than its original declaration [class.access.spec/3]. |
14755 | This applies to nested classes and nested class templates. | |
14756 | [class.mem/1]. */ | |
14757 | ||
14758 | static void cp_parser_check_access_in_redeclaration (tree decl) | |
14759 | { | |
14760 | if (!CLASS_TYPE_P (TREE_TYPE (decl))) | |
14761 | return; | |
14762 | ||
14763 | if ((TREE_PRIVATE (decl) | |
14764 | != (current_access_specifier == access_private_node)) | |
14765 | || (TREE_PROTECTED (decl) | |
14766 | != (current_access_specifier == access_protected_node))) | |
14767 | error ("%D redeclared with different access", decl); | |
14768 | } | |
14769 | ||
a723baf1 MM |
14770 | /* Look for the `template' keyword, as a syntactic disambiguator. |
14771 | Return TRUE iff it is present, in which case it will be | |
14772 | consumed. */ | |
14773 | ||
14774 | static bool | |
14775 | cp_parser_optional_template_keyword (cp_parser *parser) | |
14776 | { | |
14777 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
14778 | { | |
14779 | /* The `template' keyword can only be used within templates; | |
14780 | outside templates the parser can always figure out what is a | |
14781 | template and what is not. */ | |
14782 | if (!processing_template_decl) | |
14783 | { | |
14784 | error ("`template' (as a disambiguator) is only allowed " | |
14785 | "within templates"); | |
14786 | /* If this part of the token stream is rescanned, the same | |
14787 | error message would be generated. So, we purge the token | |
14788 | from the stream. */ | |
14789 | cp_lexer_purge_token (parser->lexer); | |
14790 | return false; | |
14791 | } | |
14792 | else | |
14793 | { | |
14794 | /* Consume the `template' keyword. */ | |
14795 | cp_lexer_consume_token (parser->lexer); | |
14796 | return true; | |
14797 | } | |
14798 | } | |
14799 | ||
14800 | return false; | |
14801 | } | |
14802 | ||
2050a1bb MM |
14803 | /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token, |
14804 | set PARSER->SCOPE, and perform other related actions. */ | |
14805 | ||
14806 | static void | |
14807 | cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser) | |
14808 | { | |
14809 | tree value; | |
14810 | tree check; | |
14811 | ||
14812 | /* Get the stored value. */ | |
14813 | value = cp_lexer_consume_token (parser->lexer)->value; | |
14814 | /* Perform any access checks that were deferred. */ | |
14815 | for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check)) | |
cf22909c | 14816 | perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check)); |
2050a1bb MM |
14817 | /* Set the scope from the stored value. */ |
14818 | parser->scope = TREE_VALUE (value); | |
14819 | parser->qualifying_scope = TREE_TYPE (value); | |
14820 | parser->object_scope = NULL_TREE; | |
14821 | } | |
14822 | ||
a723baf1 MM |
14823 | /* Add tokens to CACHE until an non-nested END token appears. */ |
14824 | ||
14825 | static void | |
14826 | cp_parser_cache_group (cp_parser *parser, | |
14827 | cp_token_cache *cache, | |
14828 | enum cpp_ttype end, | |
14829 | unsigned depth) | |
14830 | { | |
14831 | while (true) | |
14832 | { | |
14833 | cp_token *token; | |
14834 | ||
14835 | /* Abort a parenthesized expression if we encounter a brace. */ | |
14836 | if ((end == CPP_CLOSE_PAREN || depth == 0) | |
14837 | && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
14838 | return; | |
14839 | /* Consume the next token. */ | |
14840 | token = cp_lexer_consume_token (parser->lexer); | |
14841 | /* If we've reached the end of the file, stop. */ | |
14842 | if (token->type == CPP_EOF) | |
14843 | return; | |
14844 | /* Add this token to the tokens we are saving. */ | |
14845 | cp_token_cache_push_token (cache, token); | |
14846 | /* See if it starts a new group. */ | |
14847 | if (token->type == CPP_OPEN_BRACE) | |
14848 | { | |
14849 | cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1); | |
14850 | if (depth == 0) | |
14851 | return; | |
14852 | } | |
14853 | else if (token->type == CPP_OPEN_PAREN) | |
14854 | cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1); | |
14855 | else if (token->type == end) | |
14856 | return; | |
14857 | } | |
14858 | } | |
14859 | ||
14860 | /* Begin parsing tentatively. We always save tokens while parsing | |
14861 | tentatively so that if the tentative parsing fails we can restore the | |
14862 | tokens. */ | |
14863 | ||
14864 | static void | |
94edc4ab | 14865 | cp_parser_parse_tentatively (cp_parser* parser) |
a723baf1 MM |
14866 | { |
14867 | /* Enter a new parsing context. */ | |
14868 | parser->context = cp_parser_context_new (parser->context); | |
14869 | /* Begin saving tokens. */ | |
14870 | cp_lexer_save_tokens (parser->lexer); | |
14871 | /* In order to avoid repetitive access control error messages, | |
14872 | access checks are queued up until we are no longer parsing | |
14873 | tentatively. */ | |
8d241e0b | 14874 | push_deferring_access_checks (dk_deferred); |
a723baf1 MM |
14875 | } |
14876 | ||
14877 | /* Commit to the currently active tentative parse. */ | |
14878 | ||
14879 | static void | |
94edc4ab | 14880 | cp_parser_commit_to_tentative_parse (cp_parser* parser) |
a723baf1 MM |
14881 | { |
14882 | cp_parser_context *context; | |
14883 | cp_lexer *lexer; | |
14884 | ||
14885 | /* Mark all of the levels as committed. */ | |
14886 | lexer = parser->lexer; | |
14887 | for (context = parser->context; context->next; context = context->next) | |
14888 | { | |
14889 | if (context->status == CP_PARSER_STATUS_KIND_COMMITTED) | |
14890 | break; | |
14891 | context->status = CP_PARSER_STATUS_KIND_COMMITTED; | |
14892 | while (!cp_lexer_saving_tokens (lexer)) | |
14893 | lexer = lexer->next; | |
14894 | cp_lexer_commit_tokens (lexer); | |
14895 | } | |
14896 | } | |
14897 | ||
14898 | /* Abort the currently active tentative parse. All consumed tokens | |
14899 | will be rolled back, and no diagnostics will be issued. */ | |
14900 | ||
14901 | static void | |
94edc4ab | 14902 | cp_parser_abort_tentative_parse (cp_parser* parser) |
a723baf1 MM |
14903 | { |
14904 | cp_parser_simulate_error (parser); | |
14905 | /* Now, pretend that we want to see if the construct was | |
14906 | successfully parsed. */ | |
14907 | cp_parser_parse_definitely (parser); | |
14908 | } | |
14909 | ||
34cd5ae7 | 14910 | /* Stop parsing tentatively. If a parse error has occurred, restore the |
a723baf1 MM |
14911 | token stream. Otherwise, commit to the tokens we have consumed. |
14912 | Returns true if no error occurred; false otherwise. */ | |
14913 | ||
14914 | static bool | |
94edc4ab | 14915 | cp_parser_parse_definitely (cp_parser* parser) |
a723baf1 MM |
14916 | { |
14917 | bool error_occurred; | |
14918 | cp_parser_context *context; | |
14919 | ||
34cd5ae7 | 14920 | /* Remember whether or not an error occurred, since we are about to |
a723baf1 MM |
14921 | destroy that information. */ |
14922 | error_occurred = cp_parser_error_occurred (parser); | |
14923 | /* Remove the topmost context from the stack. */ | |
14924 | context = parser->context; | |
14925 | parser->context = context->next; | |
14926 | /* If no parse errors occurred, commit to the tentative parse. */ | |
14927 | if (!error_occurred) | |
14928 | { | |
14929 | /* Commit to the tokens read tentatively, unless that was | |
14930 | already done. */ | |
14931 | if (context->status != CP_PARSER_STATUS_KIND_COMMITTED) | |
14932 | cp_lexer_commit_tokens (parser->lexer); | |
cf22909c KL |
14933 | |
14934 | pop_to_parent_deferring_access_checks (); | |
a723baf1 MM |
14935 | } |
14936 | /* Otherwise, if errors occurred, roll back our state so that things | |
14937 | are just as they were before we began the tentative parse. */ | |
14938 | else | |
cf22909c KL |
14939 | { |
14940 | cp_lexer_rollback_tokens (parser->lexer); | |
14941 | pop_deferring_access_checks (); | |
14942 | } | |
e5976695 MM |
14943 | /* Add the context to the front of the free list. */ |
14944 | context->next = cp_parser_context_free_list; | |
14945 | cp_parser_context_free_list = context; | |
14946 | ||
14947 | return !error_occurred; | |
a723baf1 MM |
14948 | } |
14949 | ||
a723baf1 MM |
14950 | /* Returns true if we are parsing tentatively -- but have decided that |
14951 | we will stick with this tentative parse, even if errors occur. */ | |
14952 | ||
14953 | static bool | |
94edc4ab | 14954 | cp_parser_committed_to_tentative_parse (cp_parser* parser) |
a723baf1 MM |
14955 | { |
14956 | return (cp_parser_parsing_tentatively (parser) | |
14957 | && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED); | |
14958 | } | |
14959 | ||
4de8668e | 14960 | /* Returns nonzero iff an error has occurred during the most recent |
a723baf1 MM |
14961 | tentative parse. */ |
14962 | ||
14963 | static bool | |
94edc4ab | 14964 | cp_parser_error_occurred (cp_parser* parser) |
a723baf1 MM |
14965 | { |
14966 | return (cp_parser_parsing_tentatively (parser) | |
14967 | && parser->context->status == CP_PARSER_STATUS_KIND_ERROR); | |
14968 | } | |
14969 | ||
4de8668e | 14970 | /* Returns nonzero if GNU extensions are allowed. */ |
a723baf1 MM |
14971 | |
14972 | static bool | |
94edc4ab | 14973 | cp_parser_allow_gnu_extensions_p (cp_parser* parser) |
a723baf1 MM |
14974 | { |
14975 | return parser->allow_gnu_extensions_p; | |
14976 | } | |
14977 | ||
14978 | \f | |
14979 | ||
14980 | /* The parser. */ | |
14981 | ||
14982 | static GTY (()) cp_parser *the_parser; | |
14983 | ||
14984 | /* External interface. */ | |
14985 | ||
d1bd0ded | 14986 | /* Parse one entire translation unit. */ |
a723baf1 | 14987 | |
d1bd0ded GK |
14988 | void |
14989 | c_parse_file (void) | |
a723baf1 MM |
14990 | { |
14991 | bool error_occurred; | |
14992 | ||
14993 | the_parser = cp_parser_new (); | |
78757caa KL |
14994 | push_deferring_access_checks (flag_access_control |
14995 | ? dk_no_deferred : dk_no_check); | |
a723baf1 MM |
14996 | error_occurred = cp_parser_translation_unit (the_parser); |
14997 | the_parser = NULL; | |
a723baf1 MM |
14998 | } |
14999 | ||
15000 | /* Clean up after parsing the entire translation unit. */ | |
15001 | ||
15002 | void | |
94edc4ab | 15003 | free_parser_stacks (void) |
a723baf1 MM |
15004 | { |
15005 | /* Nothing to do. */ | |
15006 | } | |
15007 | ||
15008 | /* This variable must be provided by every front end. */ | |
15009 | ||
15010 | int yydebug; | |
15011 | ||
15012 | #include "gt-cp-parser.h" |