]>
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 | ||
4f8163b1 MM |
1260 | /* TRUE if we are parsing a type-id in an expression context. In |
1261 | such a situation, both "type (expr)" and "type (type)" are valid | |
1262 | alternatives. */ | |
1263 | bool in_type_id_in_expr_p; | |
1264 | ||
a723baf1 MM |
1265 | /* If non-NULL, then we are parsing a construct where new type |
1266 | definitions are not permitted. The string stored here will be | |
1267 | issued as an error message if a type is defined. */ | |
1268 | const char *type_definition_forbidden_message; | |
1269 | ||
8db1028e NS |
1270 | /* A list of lists. The outer list is a stack, used for member |
1271 | functions of local classes. At each level there are two sub-list, | |
1272 | one on TREE_VALUE and one on TREE_PURPOSE. Each of those | |
1273 | sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their | |
1274 | TREE_VALUE's. The functions are chained in reverse declaration | |
1275 | order. | |
1276 | ||
1277 | The TREE_PURPOSE sublist contains those functions with default | |
1278 | arguments that need post processing, and the TREE_VALUE sublist | |
1279 | contains those functions with definitions that need post | |
1280 | processing. | |
1281 | ||
1282 | These lists can only be processed once the outermost class being | |
9bcb9aae | 1283 | defined is complete. */ |
a723baf1 MM |
1284 | tree unparsed_functions_queues; |
1285 | ||
1286 | /* The number of classes whose definitions are currently in | |
1287 | progress. */ | |
1288 | unsigned num_classes_being_defined; | |
1289 | ||
1290 | /* The number of template parameter lists that apply directly to the | |
1291 | current declaration. */ | |
1292 | unsigned num_template_parameter_lists; | |
1293 | } cp_parser; | |
1294 | ||
04c06002 | 1295 | /* The type of a function that parses some kind of expression. */ |
94edc4ab | 1296 | typedef tree (*cp_parser_expression_fn) (cp_parser *); |
a723baf1 MM |
1297 | |
1298 | /* Prototypes. */ | |
1299 | ||
1300 | /* Constructors and destructors. */ | |
1301 | ||
1302 | static cp_parser *cp_parser_new | |
94edc4ab | 1303 | (void); |
a723baf1 MM |
1304 | |
1305 | /* Routines to parse various constructs. | |
1306 | ||
1307 | Those that return `tree' will return the error_mark_node (rather | |
1308 | than NULL_TREE) if a parse error occurs, unless otherwise noted. | |
1309 | Sometimes, they will return an ordinary node if error-recovery was | |
34cd5ae7 | 1310 | attempted, even though a parse error occurred. So, to check |
a723baf1 MM |
1311 | whether or not a parse error occurred, you should always use |
1312 | cp_parser_error_occurred. If the construct is optional (indicated | |
1313 | either by an `_opt' in the name of the function that does the | |
1314 | parsing or via a FLAGS parameter), then NULL_TREE is returned if | |
1315 | the construct is not present. */ | |
1316 | ||
1317 | /* Lexical conventions [gram.lex] */ | |
1318 | ||
1319 | static tree cp_parser_identifier | |
94edc4ab | 1320 | (cp_parser *); |
a723baf1 MM |
1321 | |
1322 | /* Basic concepts [gram.basic] */ | |
1323 | ||
1324 | static bool cp_parser_translation_unit | |
94edc4ab | 1325 | (cp_parser *); |
a723baf1 MM |
1326 | |
1327 | /* Expressions [gram.expr] */ | |
1328 | ||
1329 | static tree cp_parser_primary_expression | |
b3445994 | 1330 | (cp_parser *, cp_id_kind *, tree *); |
a723baf1 | 1331 | static tree cp_parser_id_expression |
f3c2dfc6 | 1332 | (cp_parser *, bool, bool, bool *, bool); |
a723baf1 | 1333 | static tree cp_parser_unqualified_id |
f3c2dfc6 | 1334 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1335 | static tree cp_parser_nested_name_specifier_opt |
a668c6ad | 1336 | (cp_parser *, bool, bool, bool, bool); |
a723baf1 | 1337 | static tree cp_parser_nested_name_specifier |
a723baf1 | 1338 | (cp_parser *, bool, bool, bool, bool); |
a668c6ad MM |
1339 | static tree cp_parser_class_or_namespace_name |
1340 | (cp_parser *, bool, bool, bool, bool, bool); | |
a723baf1 MM |
1341 | static tree cp_parser_postfix_expression |
1342 | (cp_parser *, bool); | |
7efa3e22 | 1343 | static tree cp_parser_parenthesized_expression_list |
39703eb9 | 1344 | (cp_parser *, bool, bool *); |
a723baf1 | 1345 | static void cp_parser_pseudo_destructor_name |
94edc4ab | 1346 | (cp_parser *, tree *, tree *); |
a723baf1 MM |
1347 | static tree cp_parser_unary_expression |
1348 | (cp_parser *, bool); | |
1349 | static enum tree_code cp_parser_unary_operator | |
94edc4ab | 1350 | (cp_token *); |
a723baf1 | 1351 | static tree cp_parser_new_expression |
94edc4ab | 1352 | (cp_parser *); |
a723baf1 | 1353 | static tree cp_parser_new_placement |
94edc4ab | 1354 | (cp_parser *); |
a723baf1 | 1355 | static tree cp_parser_new_type_id |
94edc4ab | 1356 | (cp_parser *); |
a723baf1 | 1357 | static tree cp_parser_new_declarator_opt |
94edc4ab | 1358 | (cp_parser *); |
a723baf1 | 1359 | static tree cp_parser_direct_new_declarator |
94edc4ab | 1360 | (cp_parser *); |
a723baf1 | 1361 | static tree cp_parser_new_initializer |
94edc4ab | 1362 | (cp_parser *); |
a723baf1 | 1363 | static tree cp_parser_delete_expression |
94edc4ab | 1364 | (cp_parser *); |
a723baf1 MM |
1365 | static tree cp_parser_cast_expression |
1366 | (cp_parser *, bool); | |
1367 | static tree cp_parser_pm_expression | |
94edc4ab | 1368 | (cp_parser *); |
a723baf1 | 1369 | static tree cp_parser_multiplicative_expression |
94edc4ab | 1370 | (cp_parser *); |
a723baf1 | 1371 | static tree cp_parser_additive_expression |
94edc4ab | 1372 | (cp_parser *); |
a723baf1 | 1373 | static tree cp_parser_shift_expression |
94edc4ab | 1374 | (cp_parser *); |
a723baf1 | 1375 | static tree cp_parser_relational_expression |
94edc4ab | 1376 | (cp_parser *); |
a723baf1 | 1377 | static tree cp_parser_equality_expression |
94edc4ab | 1378 | (cp_parser *); |
a723baf1 | 1379 | static tree cp_parser_and_expression |
94edc4ab | 1380 | (cp_parser *); |
a723baf1 | 1381 | static tree cp_parser_exclusive_or_expression |
94edc4ab | 1382 | (cp_parser *); |
a723baf1 | 1383 | static tree cp_parser_inclusive_or_expression |
94edc4ab | 1384 | (cp_parser *); |
a723baf1 | 1385 | static tree cp_parser_logical_and_expression |
94edc4ab | 1386 | (cp_parser *); |
a723baf1 | 1387 | static tree cp_parser_logical_or_expression |
94edc4ab | 1388 | (cp_parser *); |
a723baf1 | 1389 | static tree cp_parser_question_colon_clause |
94edc4ab | 1390 | (cp_parser *, tree); |
a723baf1 | 1391 | static tree cp_parser_assignment_expression |
94edc4ab | 1392 | (cp_parser *); |
a723baf1 | 1393 | static enum tree_code cp_parser_assignment_operator_opt |
94edc4ab | 1394 | (cp_parser *); |
a723baf1 | 1395 | static tree cp_parser_expression |
94edc4ab | 1396 | (cp_parser *); |
a723baf1 | 1397 | static tree cp_parser_constant_expression |
14d22dd6 | 1398 | (cp_parser *, bool, bool *); |
a723baf1 MM |
1399 | |
1400 | /* Statements [gram.stmt.stmt] */ | |
1401 | ||
1402 | static void cp_parser_statement | |
a5bcc582 | 1403 | (cp_parser *, bool); |
a723baf1 | 1404 | static tree cp_parser_labeled_statement |
a5bcc582 | 1405 | (cp_parser *, bool); |
a723baf1 | 1406 | static tree cp_parser_expression_statement |
a5bcc582 | 1407 | (cp_parser *, bool); |
a723baf1 | 1408 | static tree cp_parser_compound_statement |
a5bcc582 | 1409 | (cp_parser *, bool); |
a723baf1 | 1410 | static void cp_parser_statement_seq_opt |
a5bcc582 | 1411 | (cp_parser *, bool); |
a723baf1 | 1412 | static tree cp_parser_selection_statement |
94edc4ab | 1413 | (cp_parser *); |
a723baf1 | 1414 | static tree cp_parser_condition |
94edc4ab | 1415 | (cp_parser *); |
a723baf1 | 1416 | static tree cp_parser_iteration_statement |
94edc4ab | 1417 | (cp_parser *); |
a723baf1 | 1418 | static void cp_parser_for_init_statement |
94edc4ab | 1419 | (cp_parser *); |
a723baf1 | 1420 | static tree cp_parser_jump_statement |
94edc4ab | 1421 | (cp_parser *); |
a723baf1 | 1422 | static void cp_parser_declaration_statement |
94edc4ab | 1423 | (cp_parser *); |
a723baf1 MM |
1424 | |
1425 | static tree cp_parser_implicitly_scoped_statement | |
94edc4ab | 1426 | (cp_parser *); |
a723baf1 | 1427 | static void cp_parser_already_scoped_statement |
94edc4ab | 1428 | (cp_parser *); |
a723baf1 MM |
1429 | |
1430 | /* Declarations [gram.dcl.dcl] */ | |
1431 | ||
1432 | static void cp_parser_declaration_seq_opt | |
94edc4ab | 1433 | (cp_parser *); |
a723baf1 | 1434 | static void cp_parser_declaration |
94edc4ab | 1435 | (cp_parser *); |
a723baf1 | 1436 | static void cp_parser_block_declaration |
94edc4ab | 1437 | (cp_parser *, bool); |
a723baf1 | 1438 | static void cp_parser_simple_declaration |
94edc4ab | 1439 | (cp_parser *, bool); |
a723baf1 | 1440 | static tree cp_parser_decl_specifier_seq |
560ad596 | 1441 | (cp_parser *, cp_parser_flags, tree *, int *); |
a723baf1 | 1442 | static tree cp_parser_storage_class_specifier_opt |
94edc4ab | 1443 | (cp_parser *); |
a723baf1 | 1444 | static tree cp_parser_function_specifier_opt |
94edc4ab | 1445 | (cp_parser *); |
a723baf1 | 1446 | static tree cp_parser_type_specifier |
560ad596 | 1447 | (cp_parser *, cp_parser_flags, bool, bool, int *, bool *); |
a723baf1 | 1448 | static tree cp_parser_simple_type_specifier |
4b0d3cbe | 1449 | (cp_parser *, cp_parser_flags, bool); |
a723baf1 | 1450 | static tree cp_parser_type_name |
94edc4ab | 1451 | (cp_parser *); |
a723baf1 | 1452 | static tree cp_parser_elaborated_type_specifier |
94edc4ab | 1453 | (cp_parser *, bool, bool); |
a723baf1 | 1454 | static tree cp_parser_enum_specifier |
94edc4ab | 1455 | (cp_parser *); |
a723baf1 | 1456 | static void cp_parser_enumerator_list |
94edc4ab | 1457 | (cp_parser *, tree); |
a723baf1 | 1458 | static void cp_parser_enumerator_definition |
94edc4ab | 1459 | (cp_parser *, tree); |
a723baf1 | 1460 | static tree cp_parser_namespace_name |
94edc4ab | 1461 | (cp_parser *); |
a723baf1 | 1462 | static void cp_parser_namespace_definition |
94edc4ab | 1463 | (cp_parser *); |
a723baf1 | 1464 | static void cp_parser_namespace_body |
94edc4ab | 1465 | (cp_parser *); |
a723baf1 | 1466 | static tree cp_parser_qualified_namespace_specifier |
94edc4ab | 1467 | (cp_parser *); |
a723baf1 | 1468 | static void cp_parser_namespace_alias_definition |
94edc4ab | 1469 | (cp_parser *); |
a723baf1 | 1470 | static void cp_parser_using_declaration |
94edc4ab | 1471 | (cp_parser *); |
a723baf1 | 1472 | static void cp_parser_using_directive |
94edc4ab | 1473 | (cp_parser *); |
a723baf1 | 1474 | static void cp_parser_asm_definition |
94edc4ab | 1475 | (cp_parser *); |
a723baf1 | 1476 | static void cp_parser_linkage_specification |
94edc4ab | 1477 | (cp_parser *); |
a723baf1 MM |
1478 | |
1479 | /* Declarators [gram.dcl.decl] */ | |
1480 | ||
1481 | static tree cp_parser_init_declarator | |
560ad596 | 1482 | (cp_parser *, tree, tree, bool, bool, int, bool *); |
a723baf1 | 1483 | static tree cp_parser_declarator |
4bb8ca28 | 1484 | (cp_parser *, cp_parser_declarator_kind, int *, bool *); |
a723baf1 | 1485 | static tree cp_parser_direct_declarator |
7efa3e22 | 1486 | (cp_parser *, cp_parser_declarator_kind, int *); |
a723baf1 | 1487 | static enum tree_code cp_parser_ptr_operator |
94edc4ab | 1488 | (cp_parser *, tree *, tree *); |
a723baf1 | 1489 | static tree cp_parser_cv_qualifier_seq_opt |
94edc4ab | 1490 | (cp_parser *); |
a723baf1 | 1491 | static tree cp_parser_cv_qualifier_opt |
94edc4ab | 1492 | (cp_parser *); |
a723baf1 | 1493 | static tree cp_parser_declarator_id |
94edc4ab | 1494 | (cp_parser *); |
a723baf1 | 1495 | static tree cp_parser_type_id |
94edc4ab | 1496 | (cp_parser *); |
a723baf1 | 1497 | static tree cp_parser_type_specifier_seq |
94edc4ab | 1498 | (cp_parser *); |
a723baf1 | 1499 | static tree cp_parser_parameter_declaration_clause |
94edc4ab | 1500 | (cp_parser *); |
a723baf1 | 1501 | static tree cp_parser_parameter_declaration_list |
94edc4ab | 1502 | (cp_parser *); |
a723baf1 | 1503 | static tree cp_parser_parameter_declaration |
4bb8ca28 | 1504 | (cp_parser *, bool, bool *); |
a723baf1 MM |
1505 | static void cp_parser_function_body |
1506 | (cp_parser *); | |
1507 | static tree cp_parser_initializer | |
39703eb9 | 1508 | (cp_parser *, bool *, bool *); |
a723baf1 | 1509 | static tree cp_parser_initializer_clause |
39703eb9 | 1510 | (cp_parser *, bool *); |
a723baf1 | 1511 | static tree cp_parser_initializer_list |
39703eb9 | 1512 | (cp_parser *, bool *); |
a723baf1 MM |
1513 | |
1514 | static bool cp_parser_ctor_initializer_opt_and_function_body | |
1515 | (cp_parser *); | |
1516 | ||
1517 | /* Classes [gram.class] */ | |
1518 | ||
1519 | static tree cp_parser_class_name | |
a668c6ad | 1520 | (cp_parser *, bool, bool, bool, bool, bool, bool); |
a723baf1 | 1521 | static tree cp_parser_class_specifier |
94edc4ab | 1522 | (cp_parser *); |
a723baf1 | 1523 | static tree cp_parser_class_head |
94edc4ab | 1524 | (cp_parser *, bool *); |
a723baf1 | 1525 | static enum tag_types cp_parser_class_key |
94edc4ab | 1526 | (cp_parser *); |
a723baf1 | 1527 | static void cp_parser_member_specification_opt |
94edc4ab | 1528 | (cp_parser *); |
a723baf1 | 1529 | static void cp_parser_member_declaration |
94edc4ab | 1530 | (cp_parser *); |
a723baf1 | 1531 | static tree cp_parser_pure_specifier |
94edc4ab | 1532 | (cp_parser *); |
a723baf1 | 1533 | static tree cp_parser_constant_initializer |
94edc4ab | 1534 | (cp_parser *); |
a723baf1 MM |
1535 | |
1536 | /* Derived classes [gram.class.derived] */ | |
1537 | ||
1538 | static tree cp_parser_base_clause | |
94edc4ab | 1539 | (cp_parser *); |
a723baf1 | 1540 | static tree cp_parser_base_specifier |
94edc4ab | 1541 | (cp_parser *); |
a723baf1 MM |
1542 | |
1543 | /* Special member functions [gram.special] */ | |
1544 | ||
1545 | static tree cp_parser_conversion_function_id | |
94edc4ab | 1546 | (cp_parser *); |
a723baf1 | 1547 | static tree cp_parser_conversion_type_id |
94edc4ab | 1548 | (cp_parser *); |
a723baf1 | 1549 | static tree cp_parser_conversion_declarator_opt |
94edc4ab | 1550 | (cp_parser *); |
a723baf1 | 1551 | static bool cp_parser_ctor_initializer_opt |
94edc4ab | 1552 | (cp_parser *); |
a723baf1 | 1553 | static void cp_parser_mem_initializer_list |
94edc4ab | 1554 | (cp_parser *); |
a723baf1 | 1555 | static tree cp_parser_mem_initializer |
94edc4ab | 1556 | (cp_parser *); |
a723baf1 | 1557 | static tree cp_parser_mem_initializer_id |
94edc4ab | 1558 | (cp_parser *); |
a723baf1 MM |
1559 | |
1560 | /* Overloading [gram.over] */ | |
1561 | ||
1562 | static tree cp_parser_operator_function_id | |
94edc4ab | 1563 | (cp_parser *); |
a723baf1 | 1564 | static tree cp_parser_operator |
94edc4ab | 1565 | (cp_parser *); |
a723baf1 MM |
1566 | |
1567 | /* Templates [gram.temp] */ | |
1568 | ||
1569 | static void cp_parser_template_declaration | |
94edc4ab | 1570 | (cp_parser *, bool); |
a723baf1 | 1571 | static tree cp_parser_template_parameter_list |
94edc4ab | 1572 | (cp_parser *); |
a723baf1 | 1573 | static tree cp_parser_template_parameter |
94edc4ab | 1574 | (cp_parser *); |
a723baf1 | 1575 | static tree cp_parser_type_parameter |
94edc4ab | 1576 | (cp_parser *); |
a723baf1 | 1577 | static tree cp_parser_template_id |
a668c6ad | 1578 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1579 | static tree cp_parser_template_name |
a668c6ad | 1580 | (cp_parser *, bool, bool, bool, bool *); |
a723baf1 | 1581 | static tree cp_parser_template_argument_list |
94edc4ab | 1582 | (cp_parser *); |
a723baf1 | 1583 | static tree cp_parser_template_argument |
94edc4ab | 1584 | (cp_parser *); |
a723baf1 | 1585 | static void cp_parser_explicit_instantiation |
94edc4ab | 1586 | (cp_parser *); |
a723baf1 | 1587 | static void cp_parser_explicit_specialization |
94edc4ab | 1588 | (cp_parser *); |
a723baf1 MM |
1589 | |
1590 | /* Exception handling [gram.exception] */ | |
1591 | ||
1592 | static tree cp_parser_try_block | |
94edc4ab | 1593 | (cp_parser *); |
a723baf1 | 1594 | static bool cp_parser_function_try_block |
94edc4ab | 1595 | (cp_parser *); |
a723baf1 | 1596 | static void cp_parser_handler_seq |
94edc4ab | 1597 | (cp_parser *); |
a723baf1 | 1598 | static void cp_parser_handler |
94edc4ab | 1599 | (cp_parser *); |
a723baf1 | 1600 | static tree cp_parser_exception_declaration |
94edc4ab | 1601 | (cp_parser *); |
a723baf1 | 1602 | static tree cp_parser_throw_expression |
94edc4ab | 1603 | (cp_parser *); |
a723baf1 | 1604 | static tree cp_parser_exception_specification_opt |
94edc4ab | 1605 | (cp_parser *); |
a723baf1 | 1606 | static tree cp_parser_type_id_list |
94edc4ab | 1607 | (cp_parser *); |
a723baf1 MM |
1608 | |
1609 | /* GNU Extensions */ | |
1610 | ||
1611 | static tree cp_parser_asm_specification_opt | |
94edc4ab | 1612 | (cp_parser *); |
a723baf1 | 1613 | static tree cp_parser_asm_operand_list |
94edc4ab | 1614 | (cp_parser *); |
a723baf1 | 1615 | static tree cp_parser_asm_clobber_list |
94edc4ab | 1616 | (cp_parser *); |
a723baf1 | 1617 | static tree cp_parser_attributes_opt |
94edc4ab | 1618 | (cp_parser *); |
a723baf1 | 1619 | static tree cp_parser_attribute_list |
94edc4ab | 1620 | (cp_parser *); |
a723baf1 | 1621 | static bool cp_parser_extension_opt |
94edc4ab | 1622 | (cp_parser *, int *); |
a723baf1 | 1623 | static void cp_parser_label_declaration |
94edc4ab | 1624 | (cp_parser *); |
a723baf1 MM |
1625 | |
1626 | /* Utility Routines */ | |
1627 | ||
1628 | static tree cp_parser_lookup_name | |
b0bc6e8e | 1629 | (cp_parser *, tree, bool, bool, bool, bool); |
a723baf1 | 1630 | static tree cp_parser_lookup_name_simple |
94edc4ab | 1631 | (cp_parser *, tree); |
a723baf1 MM |
1632 | static tree cp_parser_maybe_treat_template_as_class |
1633 | (tree, bool); | |
1634 | static bool cp_parser_check_declarator_template_parameters | |
94edc4ab | 1635 | (cp_parser *, tree); |
a723baf1 | 1636 | static bool cp_parser_check_template_parameters |
94edc4ab | 1637 | (cp_parser *, unsigned); |
d6b4ea85 MM |
1638 | static tree cp_parser_simple_cast_expression |
1639 | (cp_parser *); | |
a723baf1 | 1640 | static tree cp_parser_binary_expression |
94edc4ab | 1641 | (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn); |
a723baf1 | 1642 | static tree cp_parser_global_scope_opt |
94edc4ab | 1643 | (cp_parser *, bool); |
a723baf1 MM |
1644 | static bool cp_parser_constructor_declarator_p |
1645 | (cp_parser *, bool); | |
1646 | static tree cp_parser_function_definition_from_specifiers_and_declarator | |
94edc4ab | 1647 | (cp_parser *, tree, tree, tree); |
a723baf1 | 1648 | static tree cp_parser_function_definition_after_declarator |
94edc4ab | 1649 | (cp_parser *, bool); |
a723baf1 | 1650 | static void cp_parser_template_declaration_after_export |
94edc4ab | 1651 | (cp_parser *, bool); |
a723baf1 | 1652 | static tree cp_parser_single_declaration |
94edc4ab | 1653 | (cp_parser *, bool, bool *); |
a723baf1 | 1654 | static tree cp_parser_functional_cast |
94edc4ab | 1655 | (cp_parser *, tree); |
4bb8ca28 MM |
1656 | static tree cp_parser_save_member_function_body |
1657 | (cp_parser *, tree, tree, tree); | |
ec75414f MM |
1658 | static tree cp_parser_enclosed_template_argument_list |
1659 | (cp_parser *); | |
8db1028e NS |
1660 | static void cp_parser_save_default_args |
1661 | (cp_parser *, tree); | |
a723baf1 | 1662 | static void cp_parser_late_parsing_for_member |
94edc4ab | 1663 | (cp_parser *, tree); |
a723baf1 | 1664 | static void cp_parser_late_parsing_default_args |
8218bd34 | 1665 | (cp_parser *, tree); |
a723baf1 | 1666 | static tree cp_parser_sizeof_operand |
94edc4ab | 1667 | (cp_parser *, enum rid); |
a723baf1 | 1668 | static bool cp_parser_declares_only_class_p |
94edc4ab | 1669 | (cp_parser *); |
d17811fd MM |
1670 | static tree cp_parser_fold_non_dependent_expr |
1671 | (tree); | |
a723baf1 | 1672 | static bool cp_parser_friend_p |
94edc4ab | 1673 | (tree); |
a723baf1 | 1674 | static cp_token *cp_parser_require |
94edc4ab | 1675 | (cp_parser *, enum cpp_ttype, const char *); |
a723baf1 | 1676 | static cp_token *cp_parser_require_keyword |
94edc4ab | 1677 | (cp_parser *, enum rid, const char *); |
a723baf1 | 1678 | static bool cp_parser_token_starts_function_definition_p |
94edc4ab | 1679 | (cp_token *); |
a723baf1 MM |
1680 | static bool cp_parser_next_token_starts_class_definition_p |
1681 | (cp_parser *); | |
d17811fd MM |
1682 | static bool cp_parser_next_token_ends_template_argument_p |
1683 | (cp_parser *); | |
a723baf1 | 1684 | static enum tag_types cp_parser_token_is_class_key |
94edc4ab | 1685 | (cp_token *); |
a723baf1 MM |
1686 | static void cp_parser_check_class_key |
1687 | (enum tag_types, tree type); | |
37d407a1 KL |
1688 | static void cp_parser_check_access_in_redeclaration |
1689 | (tree type); | |
a723baf1 MM |
1690 | static bool cp_parser_optional_template_keyword |
1691 | (cp_parser *); | |
2050a1bb MM |
1692 | static void cp_parser_pre_parsed_nested_name_specifier |
1693 | (cp_parser *); | |
a723baf1 MM |
1694 | static void cp_parser_cache_group |
1695 | (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned); | |
1696 | static void cp_parser_parse_tentatively | |
94edc4ab | 1697 | (cp_parser *); |
a723baf1 | 1698 | static void cp_parser_commit_to_tentative_parse |
94edc4ab | 1699 | (cp_parser *); |
a723baf1 | 1700 | static void cp_parser_abort_tentative_parse |
94edc4ab | 1701 | (cp_parser *); |
a723baf1 | 1702 | static bool cp_parser_parse_definitely |
94edc4ab | 1703 | (cp_parser *); |
f7b5ecd9 | 1704 | static inline bool cp_parser_parsing_tentatively |
94edc4ab | 1705 | (cp_parser *); |
a723baf1 | 1706 | static bool cp_parser_committed_to_tentative_parse |
94edc4ab | 1707 | (cp_parser *); |
a723baf1 | 1708 | static void cp_parser_error |
94edc4ab | 1709 | (cp_parser *, const char *); |
4bb8ca28 MM |
1710 | static void cp_parser_name_lookup_error |
1711 | (cp_parser *, tree, tree, const char *); | |
e5976695 | 1712 | static bool cp_parser_simulate_error |
94edc4ab | 1713 | (cp_parser *); |
a723baf1 | 1714 | static void cp_parser_check_type_definition |
94edc4ab | 1715 | (cp_parser *); |
560ad596 MM |
1716 | static void cp_parser_check_for_definition_in_return_type |
1717 | (tree, int); | |
ee43dab5 MM |
1718 | static void cp_parser_check_for_invalid_template_id |
1719 | (cp_parser *, tree); | |
67c03833 | 1720 | static tree cp_parser_non_integral_constant_expression |
14d22dd6 | 1721 | (const char *); |
8fbc5ae7 MM |
1722 | static bool cp_parser_diagnose_invalid_type_name |
1723 | (cp_parser *); | |
7efa3e22 | 1724 | static int cp_parser_skip_to_closing_parenthesis |
a668c6ad | 1725 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1726 | static void cp_parser_skip_to_end_of_statement |
94edc4ab | 1727 | (cp_parser *); |
e0860732 MM |
1728 | static void cp_parser_consume_semicolon_at_end_of_statement |
1729 | (cp_parser *); | |
a723baf1 | 1730 | static void cp_parser_skip_to_end_of_block_or_statement |
94edc4ab | 1731 | (cp_parser *); |
a723baf1 MM |
1732 | static void cp_parser_skip_to_closing_brace |
1733 | (cp_parser *); | |
1734 | static void cp_parser_skip_until_found | |
94edc4ab | 1735 | (cp_parser *, enum cpp_ttype, const char *); |
a723baf1 | 1736 | static bool cp_parser_error_occurred |
94edc4ab | 1737 | (cp_parser *); |
a723baf1 | 1738 | static bool cp_parser_allow_gnu_extensions_p |
94edc4ab | 1739 | (cp_parser *); |
a723baf1 | 1740 | static bool cp_parser_is_string_literal |
94edc4ab | 1741 | (cp_token *); |
a723baf1 | 1742 | static bool cp_parser_is_keyword |
94edc4ab | 1743 | (cp_token *, enum rid); |
a723baf1 | 1744 | |
4de8668e | 1745 | /* Returns nonzero if we are parsing tentatively. */ |
f7b5ecd9 MM |
1746 | |
1747 | static inline bool | |
94edc4ab | 1748 | cp_parser_parsing_tentatively (cp_parser* parser) |
f7b5ecd9 MM |
1749 | { |
1750 | return parser->context->next != NULL; | |
1751 | } | |
1752 | ||
4de8668e | 1753 | /* Returns nonzero if TOKEN is a string literal. */ |
a723baf1 MM |
1754 | |
1755 | static bool | |
94edc4ab | 1756 | cp_parser_is_string_literal (cp_token* token) |
a723baf1 MM |
1757 | { |
1758 | return (token->type == CPP_STRING || token->type == CPP_WSTRING); | |
1759 | } | |
1760 | ||
4de8668e | 1761 | /* Returns nonzero if TOKEN is the indicated KEYWORD. */ |
a723baf1 MM |
1762 | |
1763 | static bool | |
94edc4ab | 1764 | cp_parser_is_keyword (cp_token* token, enum rid keyword) |
a723baf1 MM |
1765 | { |
1766 | return token->keyword == keyword; | |
1767 | } | |
1768 | ||
a723baf1 MM |
1769 | /* Issue the indicated error MESSAGE. */ |
1770 | ||
1771 | static void | |
94edc4ab | 1772 | cp_parser_error (cp_parser* parser, const char* message) |
a723baf1 | 1773 | { |
a723baf1 | 1774 | /* Output the MESSAGE -- unless we're parsing tentatively. */ |
e5976695 | 1775 | if (!cp_parser_simulate_error (parser)) |
4bb8ca28 MM |
1776 | { |
1777 | cp_token *token; | |
1778 | token = cp_lexer_peek_token (parser->lexer); | |
5c832178 MM |
1779 | c_parse_error (message, |
1780 | /* Because c_parser_error does not understand | |
1781 | CPP_KEYWORD, keywords are treated like | |
1782 | identifiers. */ | |
1783 | (token->type == CPP_KEYWORD ? CPP_NAME : token->type), | |
1784 | token->value); | |
4bb8ca28 MM |
1785 | } |
1786 | } | |
1787 | ||
1788 | /* Issue an error about name-lookup failing. NAME is the | |
1789 | IDENTIFIER_NODE DECL is the result of | |
1790 | the lookup (as returned from cp_parser_lookup_name). DESIRED is | |
1791 | the thing that we hoped to find. */ | |
1792 | ||
1793 | static void | |
1794 | cp_parser_name_lookup_error (cp_parser* parser, | |
1795 | tree name, | |
1796 | tree decl, | |
1797 | const char* desired) | |
1798 | { | |
1799 | /* If name lookup completely failed, tell the user that NAME was not | |
1800 | declared. */ | |
1801 | if (decl == error_mark_node) | |
1802 | { | |
1803 | if (parser->scope && parser->scope != global_namespace) | |
1804 | error ("`%D::%D' has not been declared", | |
1805 | parser->scope, name); | |
1806 | else if (parser->scope == global_namespace) | |
1807 | error ("`::%D' has not been declared", name); | |
1808 | else | |
1809 | error ("`%D' has not been declared", name); | |
1810 | } | |
1811 | else if (parser->scope && parser->scope != global_namespace) | |
1812 | error ("`%D::%D' %s", parser->scope, name, desired); | |
1813 | else if (parser->scope == global_namespace) | |
1814 | error ("`::%D' %s", name, desired); | |
1815 | else | |
1816 | error ("`%D' %s", name, desired); | |
a723baf1 MM |
1817 | } |
1818 | ||
1819 | /* If we are parsing tentatively, remember that an error has occurred | |
e5976695 MM |
1820 | during this tentative parse. Returns true if the error was |
1821 | simulated; false if a messgae should be issued by the caller. */ | |
a723baf1 | 1822 | |
e5976695 | 1823 | static bool |
94edc4ab | 1824 | cp_parser_simulate_error (cp_parser* parser) |
a723baf1 MM |
1825 | { |
1826 | if (cp_parser_parsing_tentatively (parser) | |
1827 | && !cp_parser_committed_to_tentative_parse (parser)) | |
e5976695 MM |
1828 | { |
1829 | parser->context->status = CP_PARSER_STATUS_KIND_ERROR; | |
1830 | return true; | |
1831 | } | |
1832 | return false; | |
a723baf1 MM |
1833 | } |
1834 | ||
1835 | /* This function is called when a type is defined. If type | |
1836 | definitions are forbidden at this point, an error message is | |
1837 | issued. */ | |
1838 | ||
1839 | static void | |
94edc4ab | 1840 | cp_parser_check_type_definition (cp_parser* parser) |
a723baf1 MM |
1841 | { |
1842 | /* If types are forbidden here, issue a message. */ | |
1843 | if (parser->type_definition_forbidden_message) | |
1844 | /* Use `%s' to print the string in case there are any escape | |
1845 | characters in the message. */ | |
1846 | error ("%s", parser->type_definition_forbidden_message); | |
1847 | } | |
1848 | ||
560ad596 MM |
1849 | /* This function is called when a declaration is parsed. If |
1850 | DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM | |
1851 | indicates that a type was defined in the decl-specifiers for DECL, | |
1852 | then an error is issued. */ | |
1853 | ||
1854 | static void | |
1855 | cp_parser_check_for_definition_in_return_type (tree declarator, | |
1856 | int declares_class_or_enum) | |
1857 | { | |
1858 | /* [dcl.fct] forbids type definitions in return types. | |
1859 | Unfortunately, it's not easy to know whether or not we are | |
1860 | processing a return type until after the fact. */ | |
1861 | while (declarator | |
1862 | && (TREE_CODE (declarator) == INDIRECT_REF | |
1863 | || TREE_CODE (declarator) == ADDR_EXPR)) | |
1864 | declarator = TREE_OPERAND (declarator, 0); | |
1865 | if (declarator | |
1866 | && TREE_CODE (declarator) == CALL_EXPR | |
1867 | && declares_class_or_enum & 2) | |
1868 | error ("new types may not be defined in a return type"); | |
1869 | } | |
1870 | ||
ee43dab5 MM |
1871 | /* A type-specifier (TYPE) has been parsed which cannot be followed by |
1872 | "<" in any valid C++ program. If the next token is indeed "<", | |
1873 | issue a message warning the user about what appears to be an | |
1874 | invalid attempt to form a template-id. */ | |
1875 | ||
1876 | static void | |
1877 | cp_parser_check_for_invalid_template_id (cp_parser* parser, | |
1878 | tree type) | |
1879 | { | |
1880 | ptrdiff_t start; | |
1881 | cp_token *token; | |
1882 | ||
1883 | if (cp_lexer_next_token_is (parser->lexer, CPP_LESS)) | |
1884 | { | |
1885 | if (TYPE_P (type)) | |
1886 | error ("`%T' is not a template", type); | |
1887 | else if (TREE_CODE (type) == IDENTIFIER_NODE) | |
1888 | error ("`%s' is not a template", IDENTIFIER_POINTER (type)); | |
1889 | else | |
1890 | error ("invalid template-id"); | |
1891 | /* Remember the location of the invalid "<". */ | |
1892 | if (cp_parser_parsing_tentatively (parser) | |
1893 | && !cp_parser_committed_to_tentative_parse (parser)) | |
1894 | { | |
1895 | token = cp_lexer_peek_token (parser->lexer); | |
1896 | token = cp_lexer_prev_token (parser->lexer, token); | |
1897 | start = cp_lexer_token_difference (parser->lexer, | |
1898 | parser->lexer->first_token, | |
1899 | token); | |
1900 | } | |
1901 | else | |
1902 | start = -1; | |
1903 | /* Consume the "<". */ | |
1904 | cp_lexer_consume_token (parser->lexer); | |
1905 | /* Parse the template arguments. */ | |
1906 | cp_parser_enclosed_template_argument_list (parser); | |
da1d7781 | 1907 | /* Permanently remove the invalid template arguments so that |
ee43dab5 MM |
1908 | this error message is not issued again. */ |
1909 | if (start >= 0) | |
1910 | { | |
1911 | token = cp_lexer_advance_token (parser->lexer, | |
1912 | parser->lexer->first_token, | |
1913 | start); | |
1914 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
1915 | } | |
1916 | } | |
1917 | } | |
1918 | ||
cd0be382 | 1919 | /* Issue an error message about the fact that THING appeared in a |
14d22dd6 MM |
1920 | constant-expression. Returns ERROR_MARK_NODE. */ |
1921 | ||
1922 | static tree | |
67c03833 | 1923 | cp_parser_non_integral_constant_expression (const char *thing) |
14d22dd6 MM |
1924 | { |
1925 | error ("%s cannot appear in a constant-expression", thing); | |
1926 | return error_mark_node; | |
1927 | } | |
1928 | ||
8fbc5ae7 MM |
1929 | /* Check for a common situation where a type-name should be present, |
1930 | but is not, and issue a sensible error message. Returns true if an | |
1931 | invalid type-name was detected. */ | |
1932 | ||
1933 | static bool | |
1934 | cp_parser_diagnose_invalid_type_name (cp_parser *parser) | |
1935 | { | |
1936 | /* If the next two tokens are both identifiers, the code is | |
1937 | erroneous. The usual cause of this situation is code like: | |
1938 | ||
1939 | T t; | |
1940 | ||
1941 | where "T" should name a type -- but does not. */ | |
1942 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
1943 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME) | |
1944 | { | |
1945 | tree name; | |
1946 | ||
8d241e0b | 1947 | /* If parsing tentatively, we should commit; we really are |
8fbc5ae7 MM |
1948 | looking at a declaration. */ |
1949 | /* Consume the first identifier. */ | |
1950 | name = cp_lexer_consume_token (parser->lexer)->value; | |
1951 | /* Issue an error message. */ | |
1952 | error ("`%s' does not name a type", IDENTIFIER_POINTER (name)); | |
1953 | /* If we're in a template class, it's possible that the user was | |
1954 | referring to a type from a base class. For example: | |
1955 | ||
1956 | template <typename T> struct A { typedef T X; }; | |
1957 | template <typename T> struct B : public A<T> { X x; }; | |
1958 | ||
1959 | The user should have said "typename A<T>::X". */ | |
1960 | if (processing_template_decl && current_class_type) | |
1961 | { | |
1962 | tree b; | |
1963 | ||
1964 | for (b = TREE_CHAIN (TYPE_BINFO (current_class_type)); | |
1965 | b; | |
1966 | b = TREE_CHAIN (b)) | |
1967 | { | |
1968 | tree base_type = BINFO_TYPE (b); | |
1969 | if (CLASS_TYPE_P (base_type) | |
1fb3244a | 1970 | && dependent_type_p (base_type)) |
8fbc5ae7 MM |
1971 | { |
1972 | tree field; | |
1973 | /* Go from a particular instantiation of the | |
1974 | template (which will have an empty TYPE_FIELDs), | |
1975 | to the main version. */ | |
353b4fc0 | 1976 | base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type); |
8fbc5ae7 MM |
1977 | for (field = TYPE_FIELDS (base_type); |
1978 | field; | |
1979 | field = TREE_CHAIN (field)) | |
1980 | if (TREE_CODE (field) == TYPE_DECL | |
1981 | && DECL_NAME (field) == name) | |
1982 | { | |
1983 | error ("(perhaps `typename %T::%s' was intended)", | |
1984 | BINFO_TYPE (b), IDENTIFIER_POINTER (name)); | |
1985 | break; | |
1986 | } | |
1987 | if (field) | |
1988 | break; | |
1989 | } | |
1990 | } | |
1991 | } | |
1992 | /* Skip to the end of the declaration; there's no point in | |
1993 | trying to process it. */ | |
1994 | cp_parser_skip_to_end_of_statement (parser); | |
1995 | ||
1996 | return true; | |
1997 | } | |
1998 | ||
1999 | return false; | |
2000 | } | |
2001 | ||
a723baf1 | 2002 | /* Consume tokens up to, and including, the next non-nested closing `)'. |
7efa3e22 NS |
2003 | Returns 1 iff we found a closing `)'. RECOVERING is true, if we |
2004 | are doing error recovery. Returns -1 if OR_COMMA is true and we | |
2005 | found an unnested comma. */ | |
a723baf1 | 2006 | |
7efa3e22 NS |
2007 | static int |
2008 | cp_parser_skip_to_closing_parenthesis (cp_parser *parser, | |
a668c6ad MM |
2009 | bool recovering, |
2010 | bool or_comma, | |
2011 | bool consume_paren) | |
a723baf1 | 2012 | { |
7efa3e22 NS |
2013 | unsigned paren_depth = 0; |
2014 | unsigned brace_depth = 0; | |
a723baf1 | 2015 | |
7efa3e22 NS |
2016 | if (recovering && !or_comma && cp_parser_parsing_tentatively (parser) |
2017 | && !cp_parser_committed_to_tentative_parse (parser)) | |
2018 | return 0; | |
2019 | ||
a723baf1 MM |
2020 | while (true) |
2021 | { | |
2022 | cp_token *token; | |
7efa3e22 | 2023 | |
a723baf1 MM |
2024 | /* If we've run out of tokens, then there is no closing `)'. */ |
2025 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
7efa3e22 | 2026 | return 0; |
a723baf1 | 2027 | |
a668c6ad MM |
2028 | token = cp_lexer_peek_token (parser->lexer); |
2029 | ||
f4f206f4 | 2030 | /* This matches the processing in skip_to_end_of_statement. */ |
a668c6ad MM |
2031 | if (token->type == CPP_SEMICOLON && !brace_depth) |
2032 | return 0; | |
2033 | if (token->type == CPP_OPEN_BRACE) | |
2034 | ++brace_depth; | |
2035 | if (token->type == CPP_CLOSE_BRACE) | |
7efa3e22 | 2036 | { |
a668c6ad | 2037 | if (!brace_depth--) |
7efa3e22 | 2038 | return 0; |
7efa3e22 | 2039 | } |
a668c6ad MM |
2040 | if (recovering && or_comma && token->type == CPP_COMMA |
2041 | && !brace_depth && !paren_depth) | |
2042 | return -1; | |
7efa3e22 | 2043 | |
7efa3e22 NS |
2044 | if (!brace_depth) |
2045 | { | |
2046 | /* If it is an `(', we have entered another level of nesting. */ | |
2047 | if (token->type == CPP_OPEN_PAREN) | |
2048 | ++paren_depth; | |
2049 | /* If it is a `)', then we might be done. */ | |
2050 | else if (token->type == CPP_CLOSE_PAREN && !paren_depth--) | |
a668c6ad MM |
2051 | { |
2052 | if (consume_paren) | |
2053 | cp_lexer_consume_token (parser->lexer); | |
2054 | return 1; | |
2055 | } | |
7efa3e22 | 2056 | } |
a668c6ad MM |
2057 | |
2058 | /* Consume the token. */ | |
2059 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 MM |
2060 | } |
2061 | } | |
2062 | ||
2063 | /* Consume tokens until we reach the end of the current statement. | |
2064 | Normally, that will be just before consuming a `;'. However, if a | |
2065 | non-nested `}' comes first, then we stop before consuming that. */ | |
2066 | ||
2067 | static void | |
94edc4ab | 2068 | cp_parser_skip_to_end_of_statement (cp_parser* parser) |
a723baf1 MM |
2069 | { |
2070 | unsigned nesting_depth = 0; | |
2071 | ||
2072 | while (true) | |
2073 | { | |
2074 | cp_token *token; | |
2075 | ||
2076 | /* Peek at the next token. */ | |
2077 | token = cp_lexer_peek_token (parser->lexer); | |
2078 | /* If we've run out of tokens, stop. */ | |
2079 | if (token->type == CPP_EOF) | |
2080 | break; | |
2081 | /* If the next token is a `;', we have reached the end of the | |
2082 | statement. */ | |
2083 | if (token->type == CPP_SEMICOLON && !nesting_depth) | |
2084 | break; | |
2085 | /* If the next token is a non-nested `}', then we have reached | |
2086 | the end of the current block. */ | |
2087 | if (token->type == CPP_CLOSE_BRACE) | |
2088 | { | |
2089 | /* If this is a non-nested `}', stop before consuming it. | |
2090 | That way, when confronted with something like: | |
2091 | ||
2092 | { 3 + } | |
2093 | ||
2094 | we stop before consuming the closing `}', even though we | |
2095 | have not yet reached a `;'. */ | |
2096 | if (nesting_depth == 0) | |
2097 | break; | |
2098 | /* If it is the closing `}' for a block that we have | |
2099 | scanned, stop -- but only after consuming the token. | |
2100 | That way given: | |
2101 | ||
2102 | void f g () { ... } | |
2103 | typedef int I; | |
2104 | ||
2105 | we will stop after the body of the erroneously declared | |
2106 | function, but before consuming the following `typedef' | |
2107 | declaration. */ | |
2108 | if (--nesting_depth == 0) | |
2109 | { | |
2110 | cp_lexer_consume_token (parser->lexer); | |
2111 | break; | |
2112 | } | |
2113 | } | |
2114 | /* If it the next token is a `{', then we are entering a new | |
2115 | block. Consume the entire block. */ | |
2116 | else if (token->type == CPP_OPEN_BRACE) | |
2117 | ++nesting_depth; | |
2118 | /* Consume the token. */ | |
2119 | cp_lexer_consume_token (parser->lexer); | |
2120 | } | |
2121 | } | |
2122 | ||
e0860732 MM |
2123 | /* This function is called at the end of a statement or declaration. |
2124 | If the next token is a semicolon, it is consumed; otherwise, error | |
2125 | recovery is attempted. */ | |
2126 | ||
2127 | static void | |
2128 | cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser) | |
2129 | { | |
2130 | /* Look for the trailing `;'. */ | |
2131 | if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")) | |
2132 | { | |
2133 | /* If there is additional (erroneous) input, skip to the end of | |
2134 | the statement. */ | |
2135 | cp_parser_skip_to_end_of_statement (parser); | |
2136 | /* If the next token is now a `;', consume it. */ | |
2137 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
2138 | cp_lexer_consume_token (parser->lexer); | |
2139 | } | |
2140 | } | |
2141 | ||
a723baf1 MM |
2142 | /* Skip tokens until we have consumed an entire block, or until we |
2143 | have consumed a non-nested `;'. */ | |
2144 | ||
2145 | static void | |
94edc4ab | 2146 | cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser) |
a723baf1 MM |
2147 | { |
2148 | unsigned nesting_depth = 0; | |
2149 | ||
2150 | while (true) | |
2151 | { | |
2152 | cp_token *token; | |
2153 | ||
2154 | /* Peek at the next token. */ | |
2155 | token = cp_lexer_peek_token (parser->lexer); | |
2156 | /* If we've run out of tokens, stop. */ | |
2157 | if (token->type == CPP_EOF) | |
2158 | break; | |
2159 | /* If the next token is a `;', we have reached the end of the | |
2160 | statement. */ | |
2161 | if (token->type == CPP_SEMICOLON && !nesting_depth) | |
2162 | { | |
2163 | /* Consume the `;'. */ | |
2164 | cp_lexer_consume_token (parser->lexer); | |
2165 | break; | |
2166 | } | |
2167 | /* Consume the token. */ | |
2168 | token = cp_lexer_consume_token (parser->lexer); | |
2169 | /* If the next token is a non-nested `}', then we have reached | |
2170 | the end of the current block. */ | |
2171 | if (token->type == CPP_CLOSE_BRACE | |
2172 | && (nesting_depth == 0 || --nesting_depth == 0)) | |
2173 | break; | |
2174 | /* If it the next token is a `{', then we are entering a new | |
2175 | block. Consume the entire block. */ | |
2176 | if (token->type == CPP_OPEN_BRACE) | |
2177 | ++nesting_depth; | |
2178 | } | |
2179 | } | |
2180 | ||
2181 | /* Skip tokens until a non-nested closing curly brace is the next | |
2182 | token. */ | |
2183 | ||
2184 | static void | |
2185 | cp_parser_skip_to_closing_brace (cp_parser *parser) | |
2186 | { | |
2187 | unsigned nesting_depth = 0; | |
2188 | ||
2189 | while (true) | |
2190 | { | |
2191 | cp_token *token; | |
2192 | ||
2193 | /* Peek at the next token. */ | |
2194 | token = cp_lexer_peek_token (parser->lexer); | |
2195 | /* If we've run out of tokens, stop. */ | |
2196 | if (token->type == CPP_EOF) | |
2197 | break; | |
2198 | /* If the next token is a non-nested `}', then we have reached | |
2199 | the end of the current block. */ | |
2200 | if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0) | |
2201 | break; | |
2202 | /* If it the next token is a `{', then we are entering a new | |
2203 | block. Consume the entire block. */ | |
2204 | else if (token->type == CPP_OPEN_BRACE) | |
2205 | ++nesting_depth; | |
2206 | /* Consume the token. */ | |
2207 | cp_lexer_consume_token (parser->lexer); | |
2208 | } | |
2209 | } | |
2210 | ||
2211 | /* Create a new C++ parser. */ | |
2212 | ||
2213 | static cp_parser * | |
94edc4ab | 2214 | cp_parser_new (void) |
a723baf1 MM |
2215 | { |
2216 | cp_parser *parser; | |
17211ab5 GK |
2217 | cp_lexer *lexer; |
2218 | ||
2219 | /* cp_lexer_new_main is called before calling ggc_alloc because | |
2220 | cp_lexer_new_main might load a PCH file. */ | |
2221 | lexer = cp_lexer_new_main (); | |
a723baf1 | 2222 | |
c68b0a84 | 2223 | parser = ggc_alloc_cleared (sizeof (cp_parser)); |
17211ab5 | 2224 | parser->lexer = lexer; |
a723baf1 MM |
2225 | parser->context = cp_parser_context_new (NULL); |
2226 | ||
2227 | /* For now, we always accept GNU extensions. */ | |
2228 | parser->allow_gnu_extensions_p = 1; | |
2229 | ||
2230 | /* The `>' token is a greater-than operator, not the end of a | |
2231 | template-id. */ | |
2232 | parser->greater_than_is_operator_p = true; | |
2233 | ||
2234 | parser->default_arg_ok_p = true; | |
2235 | ||
2236 | /* We are not parsing a constant-expression. */ | |
67c03833 JM |
2237 | parser->integral_constant_expression_p = false; |
2238 | parser->allow_non_integral_constant_expression_p = false; | |
2239 | parser->non_integral_constant_expression_p = false; | |
a723baf1 | 2240 | |
263ee052 MM |
2241 | /* We are not parsing offsetof. */ |
2242 | parser->in_offsetof_p = false; | |
2243 | ||
a723baf1 MM |
2244 | /* Local variable names are not forbidden. */ |
2245 | parser->local_variables_forbidden_p = false; | |
2246 | ||
34cd5ae7 | 2247 | /* We are not processing an `extern "C"' declaration. */ |
a723baf1 MM |
2248 | parser->in_unbraced_linkage_specification_p = false; |
2249 | ||
2250 | /* We are not processing a declarator. */ | |
2251 | parser->in_declarator_p = false; | |
2252 | ||
4bb8ca28 MM |
2253 | /* We are not processing a template-argument-list. */ |
2254 | parser->in_template_argument_list_p = false; | |
2255 | ||
0e59b3fb MM |
2256 | /* We are not in an iteration statement. */ |
2257 | parser->in_iteration_statement_p = false; | |
2258 | ||
2259 | /* We are not in a switch statement. */ | |
2260 | parser->in_switch_statement_p = false; | |
2261 | ||
4f8163b1 MM |
2262 | /* We are not parsing a type-id inside an expression. */ |
2263 | parser->in_type_id_in_expr_p = false; | |
2264 | ||
a723baf1 MM |
2265 | /* The unparsed function queue is empty. */ |
2266 | parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE); | |
2267 | ||
2268 | /* There are no classes being defined. */ | |
2269 | parser->num_classes_being_defined = 0; | |
2270 | ||
2271 | /* No template parameters apply. */ | |
2272 | parser->num_template_parameter_lists = 0; | |
2273 | ||
2274 | return parser; | |
2275 | } | |
2276 | ||
2277 | /* Lexical conventions [gram.lex] */ | |
2278 | ||
2279 | /* Parse an identifier. Returns an IDENTIFIER_NODE representing the | |
2280 | identifier. */ | |
2281 | ||
2282 | static tree | |
94edc4ab | 2283 | cp_parser_identifier (cp_parser* parser) |
a723baf1 MM |
2284 | { |
2285 | cp_token *token; | |
2286 | ||
2287 | /* Look for the identifier. */ | |
2288 | token = cp_parser_require (parser, CPP_NAME, "identifier"); | |
2289 | /* Return the value. */ | |
2290 | return token ? token->value : error_mark_node; | |
2291 | } | |
2292 | ||
2293 | /* Basic concepts [gram.basic] */ | |
2294 | ||
2295 | /* Parse a translation-unit. | |
2296 | ||
2297 | translation-unit: | |
2298 | declaration-seq [opt] | |
2299 | ||
2300 | Returns TRUE if all went well. */ | |
2301 | ||
2302 | static bool | |
94edc4ab | 2303 | cp_parser_translation_unit (cp_parser* parser) |
a723baf1 MM |
2304 | { |
2305 | while (true) | |
2306 | { | |
2307 | cp_parser_declaration_seq_opt (parser); | |
2308 | ||
2309 | /* If there are no tokens left then all went well. */ | |
2310 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
2311 | break; | |
2312 | ||
2313 | /* Otherwise, issue an error message. */ | |
2314 | cp_parser_error (parser, "expected declaration"); | |
2315 | return false; | |
2316 | } | |
2317 | ||
2318 | /* Consume the EOF token. */ | |
2319 | cp_parser_require (parser, CPP_EOF, "end-of-file"); | |
2320 | ||
2321 | /* Finish up. */ | |
2322 | finish_translation_unit (); | |
2323 | ||
2324 | /* All went well. */ | |
2325 | return true; | |
2326 | } | |
2327 | ||
2328 | /* Expressions [gram.expr] */ | |
2329 | ||
2330 | /* Parse a primary-expression. | |
2331 | ||
2332 | primary-expression: | |
2333 | literal | |
2334 | this | |
2335 | ( expression ) | |
2336 | id-expression | |
2337 | ||
2338 | GNU Extensions: | |
2339 | ||
2340 | primary-expression: | |
2341 | ( compound-statement ) | |
2342 | __builtin_va_arg ( assignment-expression , type-id ) | |
2343 | ||
2344 | literal: | |
2345 | __null | |
2346 | ||
2347 | Returns a representation of the expression. | |
2348 | ||
2349 | *IDK indicates what kind of id-expression (if any) was present. | |
2350 | ||
2351 | *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be | |
2352 | used as the operand of a pointer-to-member. In that case, | |
2353 | *QUALIFYING_CLASS gives the class that is used as the qualifying | |
2354 | class in the pointer-to-member. */ | |
2355 | ||
2356 | static tree | |
2357 | cp_parser_primary_expression (cp_parser *parser, | |
b3445994 | 2358 | cp_id_kind *idk, |
a723baf1 MM |
2359 | tree *qualifying_class) |
2360 | { | |
2361 | cp_token *token; | |
2362 | ||
2363 | /* Assume the primary expression is not an id-expression. */ | |
b3445994 | 2364 | *idk = CP_ID_KIND_NONE; |
a723baf1 MM |
2365 | /* And that it cannot be used as pointer-to-member. */ |
2366 | *qualifying_class = NULL_TREE; | |
2367 | ||
2368 | /* Peek at the next token. */ | |
2369 | token = cp_lexer_peek_token (parser->lexer); | |
2370 | switch (token->type) | |
2371 | { | |
2372 | /* literal: | |
2373 | integer-literal | |
2374 | character-literal | |
2375 | floating-literal | |
2376 | string-literal | |
2377 | boolean-literal */ | |
2378 | case CPP_CHAR: | |
2379 | case CPP_WCHAR: | |
2380 | case CPP_STRING: | |
2381 | case CPP_WSTRING: | |
2382 | case CPP_NUMBER: | |
2383 | token = cp_lexer_consume_token (parser->lexer); | |
2384 | return token->value; | |
2385 | ||
2386 | case CPP_OPEN_PAREN: | |
2387 | { | |
2388 | tree expr; | |
2389 | bool saved_greater_than_is_operator_p; | |
2390 | ||
2391 | /* Consume the `('. */ | |
2392 | cp_lexer_consume_token (parser->lexer); | |
2393 | /* Within a parenthesized expression, a `>' token is always | |
2394 | the greater-than operator. */ | |
2395 | saved_greater_than_is_operator_p | |
2396 | = parser->greater_than_is_operator_p; | |
2397 | parser->greater_than_is_operator_p = true; | |
2398 | /* If we see `( { ' then we are looking at the beginning of | |
2399 | a GNU statement-expression. */ | |
2400 | if (cp_parser_allow_gnu_extensions_p (parser) | |
2401 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
2402 | { | |
2403 | /* Statement-expressions are not allowed by the standard. */ | |
2404 | if (pedantic) | |
2405 | pedwarn ("ISO C++ forbids braced-groups within expressions"); | |
2406 | ||
2407 | /* And they're not allowed outside of a function-body; you | |
2408 | cannot, for example, write: | |
2409 | ||
2410 | int i = ({ int j = 3; j + 1; }); | |
2411 | ||
2412 | at class or namespace scope. */ | |
2413 | if (!at_function_scope_p ()) | |
2414 | error ("statement-expressions are allowed only inside functions"); | |
2415 | /* Start the statement-expression. */ | |
2416 | expr = begin_stmt_expr (); | |
2417 | /* Parse the compound-statement. */ | |
a5bcc582 | 2418 | cp_parser_compound_statement (parser, true); |
a723baf1 | 2419 | /* Finish up. */ |
303b7406 | 2420 | expr = finish_stmt_expr (expr, false); |
a723baf1 MM |
2421 | } |
2422 | else | |
2423 | { | |
2424 | /* Parse the parenthesized expression. */ | |
2425 | expr = cp_parser_expression (parser); | |
2426 | /* Let the front end know that this expression was | |
2427 | enclosed in parentheses. This matters in case, for | |
2428 | example, the expression is of the form `A::B', since | |
2429 | `&A::B' might be a pointer-to-member, but `&(A::B)' is | |
2430 | not. */ | |
2431 | finish_parenthesized_expr (expr); | |
2432 | } | |
2433 | /* The `>' token might be the end of a template-id or | |
2434 | template-parameter-list now. */ | |
2435 | parser->greater_than_is_operator_p | |
2436 | = saved_greater_than_is_operator_p; | |
2437 | /* Consume the `)'. */ | |
2438 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
2439 | cp_parser_skip_to_end_of_statement (parser); | |
2440 | ||
2441 | return expr; | |
2442 | } | |
2443 | ||
2444 | case CPP_KEYWORD: | |
2445 | switch (token->keyword) | |
2446 | { | |
2447 | /* These two are the boolean literals. */ | |
2448 | case RID_TRUE: | |
2449 | cp_lexer_consume_token (parser->lexer); | |
2450 | return boolean_true_node; | |
2451 | case RID_FALSE: | |
2452 | cp_lexer_consume_token (parser->lexer); | |
2453 | return boolean_false_node; | |
2454 | ||
2455 | /* The `__null' literal. */ | |
2456 | case RID_NULL: | |
2457 | cp_lexer_consume_token (parser->lexer); | |
2458 | return null_node; | |
2459 | ||
2460 | /* Recognize the `this' keyword. */ | |
2461 | case RID_THIS: | |
2462 | cp_lexer_consume_token (parser->lexer); | |
2463 | if (parser->local_variables_forbidden_p) | |
2464 | { | |
2465 | error ("`this' may not be used in this context"); | |
2466 | return error_mark_node; | |
2467 | } | |
14d22dd6 | 2468 | /* Pointers cannot appear in constant-expressions. */ |
67c03833 | 2469 | if (parser->integral_constant_expression_p) |
14d22dd6 | 2470 | { |
67c03833 JM |
2471 | if (!parser->allow_non_integral_constant_expression_p) |
2472 | return cp_parser_non_integral_constant_expression ("`this'"); | |
2473 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 2474 | } |
a723baf1 MM |
2475 | return finish_this_expr (); |
2476 | ||
2477 | /* The `operator' keyword can be the beginning of an | |
2478 | id-expression. */ | |
2479 | case RID_OPERATOR: | |
2480 | goto id_expression; | |
2481 | ||
2482 | case RID_FUNCTION_NAME: | |
2483 | case RID_PRETTY_FUNCTION_NAME: | |
2484 | case RID_C99_FUNCTION_NAME: | |
2485 | /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and | |
2486 | __func__ are the names of variables -- but they are | |
2487 | treated specially. Therefore, they are handled here, | |
2488 | rather than relying on the generic id-expression logic | |
34cd5ae7 | 2489 | below. Grammatically, these names are id-expressions. |
a723baf1 MM |
2490 | |
2491 | Consume the token. */ | |
2492 | token = cp_lexer_consume_token (parser->lexer); | |
2493 | /* Look up the name. */ | |
2494 | return finish_fname (token->value); | |
2495 | ||
2496 | case RID_VA_ARG: | |
2497 | { | |
2498 | tree expression; | |
2499 | tree type; | |
2500 | ||
2501 | /* The `__builtin_va_arg' construct is used to handle | |
2502 | `va_arg'. Consume the `__builtin_va_arg' token. */ | |
2503 | cp_lexer_consume_token (parser->lexer); | |
2504 | /* Look for the opening `('. */ | |
2505 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
2506 | /* Now, parse the assignment-expression. */ | |
2507 | expression = cp_parser_assignment_expression (parser); | |
2508 | /* Look for the `,'. */ | |
2509 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
2510 | /* Parse the type-id. */ | |
2511 | type = cp_parser_type_id (parser); | |
2512 | /* Look for the closing `)'. */ | |
2513 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
14d22dd6 MM |
2514 | /* Using `va_arg' in a constant-expression is not |
2515 | allowed. */ | |
67c03833 | 2516 | if (parser->integral_constant_expression_p) |
14d22dd6 | 2517 | { |
67c03833 JM |
2518 | if (!parser->allow_non_integral_constant_expression_p) |
2519 | return cp_parser_non_integral_constant_expression ("`va_arg'"); | |
2520 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 2521 | } |
a723baf1 MM |
2522 | return build_x_va_arg (expression, type); |
2523 | } | |
2524 | ||
263ee052 MM |
2525 | case RID_OFFSETOF: |
2526 | { | |
2527 | tree expression; | |
2528 | bool saved_in_offsetof_p; | |
2529 | ||
2530 | /* Consume the "__offsetof__" token. */ | |
2531 | cp_lexer_consume_token (parser->lexer); | |
2532 | /* Consume the opening `('. */ | |
2533 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
2534 | /* Parse the parenthesized (almost) constant-expression. */ | |
2535 | saved_in_offsetof_p = parser->in_offsetof_p; | |
2536 | parser->in_offsetof_p = true; | |
2537 | expression | |
2538 | = cp_parser_constant_expression (parser, | |
2539 | /*allow_non_constant_p=*/false, | |
2540 | /*non_constant_p=*/NULL); | |
2541 | parser->in_offsetof_p = saved_in_offsetof_p; | |
2542 | /* Consume the closing ')'. */ | |
2543 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
2544 | ||
2545 | return expression; | |
2546 | } | |
2547 | ||
a723baf1 MM |
2548 | default: |
2549 | cp_parser_error (parser, "expected primary-expression"); | |
2550 | return error_mark_node; | |
2551 | } | |
a723baf1 MM |
2552 | |
2553 | /* An id-expression can start with either an identifier, a | |
2554 | `::' as the beginning of a qualified-id, or the "operator" | |
2555 | keyword. */ | |
2556 | case CPP_NAME: | |
2557 | case CPP_SCOPE: | |
2558 | case CPP_TEMPLATE_ID: | |
2559 | case CPP_NESTED_NAME_SPECIFIER: | |
2560 | { | |
2561 | tree id_expression; | |
2562 | tree decl; | |
b3445994 | 2563 | const char *error_msg; |
a723baf1 MM |
2564 | |
2565 | id_expression: | |
2566 | /* Parse the id-expression. */ | |
2567 | id_expression | |
2568 | = cp_parser_id_expression (parser, | |
2569 | /*template_keyword_p=*/false, | |
2570 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
2571 | /*template_p=*/NULL, |
2572 | /*declarator_p=*/false); | |
a723baf1 MM |
2573 | if (id_expression == error_mark_node) |
2574 | return error_mark_node; | |
2575 | /* If we have a template-id, then no further lookup is | |
2576 | required. If the template-id was for a template-class, we | |
2577 | will sometimes have a TYPE_DECL at this point. */ | |
2578 | else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR | |
2579 | || TREE_CODE (id_expression) == TYPE_DECL) | |
2580 | decl = id_expression; | |
2581 | /* Look up the name. */ | |
2582 | else | |
2583 | { | |
2584 | decl = cp_parser_lookup_name_simple (parser, id_expression); | |
2585 | /* If name lookup gives us a SCOPE_REF, then the | |
2586 | qualifying scope was dependent. Just propagate the | |
2587 | name. */ | |
2588 | if (TREE_CODE (decl) == SCOPE_REF) | |
2589 | { | |
2590 | if (TYPE_P (TREE_OPERAND (decl, 0))) | |
2591 | *qualifying_class = TREE_OPERAND (decl, 0); | |
2592 | return decl; | |
2593 | } | |
2594 | /* Check to see if DECL is a local variable in a context | |
2595 | where that is forbidden. */ | |
2596 | if (parser->local_variables_forbidden_p | |
2597 | && local_variable_p (decl)) | |
2598 | { | |
2599 | /* It might be that we only found DECL because we are | |
2600 | trying to be generous with pre-ISO scoping rules. | |
2601 | For example, consider: | |
2602 | ||
2603 | int i; | |
2604 | void g() { | |
2605 | for (int i = 0; i < 10; ++i) {} | |
2606 | extern void f(int j = i); | |
2607 | } | |
2608 | ||
2609 | Here, name look up will originally find the out | |
2610 | of scope `i'. We need to issue a warning message, | |
2611 | but then use the global `i'. */ | |
2612 | decl = check_for_out_of_scope_variable (decl); | |
2613 | if (local_variable_p (decl)) | |
2614 | { | |
2615 | error ("local variable `%D' may not appear in this context", | |
2616 | decl); | |
2617 | return error_mark_node; | |
2618 | } | |
2619 | } | |
c006d942 | 2620 | } |
b3445994 MM |
2621 | |
2622 | decl = finish_id_expression (id_expression, decl, parser->scope, | |
2623 | idk, qualifying_class, | |
67c03833 JM |
2624 | parser->integral_constant_expression_p, |
2625 | parser->allow_non_integral_constant_expression_p, | |
2626 | &parser->non_integral_constant_expression_p, | |
b3445994 MM |
2627 | &error_msg); |
2628 | if (error_msg) | |
2629 | cp_parser_error (parser, error_msg); | |
a723baf1 MM |
2630 | return decl; |
2631 | } | |
2632 | ||
2633 | /* Anything else is an error. */ | |
2634 | default: | |
2635 | cp_parser_error (parser, "expected primary-expression"); | |
2636 | return error_mark_node; | |
2637 | } | |
2638 | } | |
2639 | ||
2640 | /* Parse an id-expression. | |
2641 | ||
2642 | id-expression: | |
2643 | unqualified-id | |
2644 | qualified-id | |
2645 | ||
2646 | qualified-id: | |
2647 | :: [opt] nested-name-specifier template [opt] unqualified-id | |
2648 | :: identifier | |
2649 | :: operator-function-id | |
2650 | :: template-id | |
2651 | ||
2652 | Return a representation of the unqualified portion of the | |
2653 | identifier. Sets PARSER->SCOPE to the qualifying scope if there is | |
2654 | a `::' or nested-name-specifier. | |
2655 | ||
2656 | Often, if the id-expression was a qualified-id, the caller will | |
2657 | want to make a SCOPE_REF to represent the qualified-id. This | |
2658 | function does not do this in order to avoid wastefully creating | |
2659 | SCOPE_REFs when they are not required. | |
2660 | ||
a723baf1 MM |
2661 | If TEMPLATE_KEYWORD_P is true, then we have just seen the |
2662 | `template' keyword. | |
2663 | ||
2664 | If CHECK_DEPENDENCY_P is false, then names are looked up inside | |
2665 | uninstantiated templates. | |
2666 | ||
15d2cb19 | 2667 | If *TEMPLATE_P is non-NULL, it is set to true iff the |
a723baf1 | 2668 | `template' keyword is used to explicitly indicate that the entity |
f3c2dfc6 MM |
2669 | named is a template. |
2670 | ||
2671 | If DECLARATOR_P is true, the id-expression is appearing as part of | |
cd0be382 | 2672 | a declarator, rather than as part of an expression. */ |
a723baf1 MM |
2673 | |
2674 | static tree | |
2675 | cp_parser_id_expression (cp_parser *parser, | |
2676 | bool template_keyword_p, | |
2677 | bool check_dependency_p, | |
f3c2dfc6 MM |
2678 | bool *template_p, |
2679 | bool declarator_p) | |
a723baf1 MM |
2680 | { |
2681 | bool global_scope_p; | |
2682 | bool nested_name_specifier_p; | |
2683 | ||
2684 | /* Assume the `template' keyword was not used. */ | |
2685 | if (template_p) | |
2686 | *template_p = false; | |
2687 | ||
2688 | /* Look for the optional `::' operator. */ | |
2689 | global_scope_p | |
2690 | = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) | |
2691 | != NULL_TREE); | |
2692 | /* Look for the optional nested-name-specifier. */ | |
2693 | nested_name_specifier_p | |
2694 | = (cp_parser_nested_name_specifier_opt (parser, | |
2695 | /*typename_keyword_p=*/false, | |
2696 | check_dependency_p, | |
a668c6ad MM |
2697 | /*type_p=*/false, |
2698 | /*is_declarator=*/false) | |
a723baf1 MM |
2699 | != NULL_TREE); |
2700 | /* If there is a nested-name-specifier, then we are looking at | |
2701 | the first qualified-id production. */ | |
2702 | if (nested_name_specifier_p) | |
2703 | { | |
2704 | tree saved_scope; | |
2705 | tree saved_object_scope; | |
2706 | tree saved_qualifying_scope; | |
2707 | tree unqualified_id; | |
2708 | bool is_template; | |
2709 | ||
2710 | /* See if the next token is the `template' keyword. */ | |
2711 | if (!template_p) | |
2712 | template_p = &is_template; | |
2713 | *template_p = cp_parser_optional_template_keyword (parser); | |
2714 | /* Name lookup we do during the processing of the | |
2715 | unqualified-id might obliterate SCOPE. */ | |
2716 | saved_scope = parser->scope; | |
2717 | saved_object_scope = parser->object_scope; | |
2718 | saved_qualifying_scope = parser->qualifying_scope; | |
2719 | /* Process the final unqualified-id. */ | |
2720 | unqualified_id = cp_parser_unqualified_id (parser, *template_p, | |
f3c2dfc6 MM |
2721 | check_dependency_p, |
2722 | declarator_p); | |
a723baf1 MM |
2723 | /* Restore the SAVED_SCOPE for our caller. */ |
2724 | parser->scope = saved_scope; | |
2725 | parser->object_scope = saved_object_scope; | |
2726 | parser->qualifying_scope = saved_qualifying_scope; | |
2727 | ||
2728 | return unqualified_id; | |
2729 | } | |
2730 | /* Otherwise, if we are in global scope, then we are looking at one | |
2731 | of the other qualified-id productions. */ | |
2732 | else if (global_scope_p) | |
2733 | { | |
2734 | cp_token *token; | |
2735 | tree id; | |
2736 | ||
e5976695 MM |
2737 | /* Peek at the next token. */ |
2738 | token = cp_lexer_peek_token (parser->lexer); | |
2739 | ||
2740 | /* If it's an identifier, and the next token is not a "<", then | |
2741 | we can avoid the template-id case. This is an optimization | |
2742 | for this common case. */ | |
2743 | if (token->type == CPP_NAME | |
2744 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS) | |
2745 | return cp_parser_identifier (parser); | |
2746 | ||
a723baf1 MM |
2747 | cp_parser_parse_tentatively (parser); |
2748 | /* Try a template-id. */ | |
2749 | id = cp_parser_template_id (parser, | |
2750 | /*template_keyword_p=*/false, | |
a668c6ad MM |
2751 | /*check_dependency_p=*/true, |
2752 | declarator_p); | |
a723baf1 MM |
2753 | /* If that worked, we're done. */ |
2754 | if (cp_parser_parse_definitely (parser)) | |
2755 | return id; | |
2756 | ||
e5976695 MM |
2757 | /* Peek at the next token. (Changes in the token buffer may |
2758 | have invalidated the pointer obtained above.) */ | |
a723baf1 MM |
2759 | token = cp_lexer_peek_token (parser->lexer); |
2760 | ||
2761 | switch (token->type) | |
2762 | { | |
2763 | case CPP_NAME: | |
2764 | return cp_parser_identifier (parser); | |
2765 | ||
2766 | case CPP_KEYWORD: | |
2767 | if (token->keyword == RID_OPERATOR) | |
2768 | return cp_parser_operator_function_id (parser); | |
2769 | /* Fall through. */ | |
2770 | ||
2771 | default: | |
2772 | cp_parser_error (parser, "expected id-expression"); | |
2773 | return error_mark_node; | |
2774 | } | |
2775 | } | |
2776 | else | |
2777 | return cp_parser_unqualified_id (parser, template_keyword_p, | |
f3c2dfc6 MM |
2778 | /*check_dependency_p=*/true, |
2779 | declarator_p); | |
a723baf1 MM |
2780 | } |
2781 | ||
2782 | /* Parse an unqualified-id. | |
2783 | ||
2784 | unqualified-id: | |
2785 | identifier | |
2786 | operator-function-id | |
2787 | conversion-function-id | |
2788 | ~ class-name | |
2789 | template-id | |
2790 | ||
2791 | If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template' | |
2792 | keyword, in a construct like `A::template ...'. | |
2793 | ||
2794 | Returns a representation of unqualified-id. For the `identifier' | |
2795 | production, an IDENTIFIER_NODE is returned. For the `~ class-name' | |
2796 | production a BIT_NOT_EXPR is returned; the operand of the | |
2797 | BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the | |
2798 | other productions, see the documentation accompanying the | |
2799 | corresponding parsing functions. If CHECK_DEPENDENCY_P is false, | |
f3c2dfc6 MM |
2800 | names are looked up in uninstantiated templates. If DECLARATOR_P |
2801 | is true, the unqualified-id is appearing as part of a declarator, | |
2802 | rather than as part of an expression. */ | |
a723baf1 MM |
2803 | |
2804 | static tree | |
94edc4ab NN |
2805 | cp_parser_unqualified_id (cp_parser* parser, |
2806 | bool template_keyword_p, | |
f3c2dfc6 MM |
2807 | bool check_dependency_p, |
2808 | bool declarator_p) | |
a723baf1 MM |
2809 | { |
2810 | cp_token *token; | |
2811 | ||
2812 | /* Peek at the next token. */ | |
2813 | token = cp_lexer_peek_token (parser->lexer); | |
2814 | ||
2815 | switch (token->type) | |
2816 | { | |
2817 | case CPP_NAME: | |
2818 | { | |
2819 | tree id; | |
2820 | ||
2821 | /* We don't know yet whether or not this will be a | |
2822 | template-id. */ | |
2823 | cp_parser_parse_tentatively (parser); | |
2824 | /* Try a template-id. */ | |
2825 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
2826 | check_dependency_p, |
2827 | declarator_p); | |
a723baf1 MM |
2828 | /* If it worked, we're done. */ |
2829 | if (cp_parser_parse_definitely (parser)) | |
2830 | return id; | |
2831 | /* Otherwise, it's an ordinary identifier. */ | |
2832 | return cp_parser_identifier (parser); | |
2833 | } | |
2834 | ||
2835 | case CPP_TEMPLATE_ID: | |
2836 | return cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
2837 | check_dependency_p, |
2838 | declarator_p); | |
a723baf1 MM |
2839 | |
2840 | case CPP_COMPL: | |
2841 | { | |
2842 | tree type_decl; | |
2843 | tree qualifying_scope; | |
2844 | tree object_scope; | |
2845 | tree scope; | |
2846 | ||
2847 | /* Consume the `~' token. */ | |
2848 | cp_lexer_consume_token (parser->lexer); | |
2849 | /* Parse the class-name. The standard, as written, seems to | |
2850 | say that: | |
2851 | ||
2852 | template <typename T> struct S { ~S (); }; | |
2853 | template <typename T> S<T>::~S() {} | |
2854 | ||
2855 | is invalid, since `~' must be followed by a class-name, but | |
2856 | `S<T>' is dependent, and so not known to be a class. | |
2857 | That's not right; we need to look in uninstantiated | |
2858 | templates. A further complication arises from: | |
2859 | ||
2860 | template <typename T> void f(T t) { | |
2861 | t.T::~T(); | |
2862 | } | |
2863 | ||
2864 | Here, it is not possible to look up `T' in the scope of `T' | |
2865 | itself. We must look in both the current scope, and the | |
2866 | scope of the containing complete expression. | |
2867 | ||
2868 | Yet another issue is: | |
2869 | ||
2870 | struct S { | |
2871 | int S; | |
2872 | ~S(); | |
2873 | }; | |
2874 | ||
2875 | S::~S() {} | |
2876 | ||
2877 | The standard does not seem to say that the `S' in `~S' | |
2878 | should refer to the type `S' and not the data member | |
2879 | `S::S'. */ | |
2880 | ||
2881 | /* DR 244 says that we look up the name after the "~" in the | |
2882 | same scope as we looked up the qualifying name. That idea | |
2883 | isn't fully worked out; it's more complicated than that. */ | |
2884 | scope = parser->scope; | |
2885 | object_scope = parser->object_scope; | |
2886 | qualifying_scope = parser->qualifying_scope; | |
2887 | ||
2888 | /* If the name is of the form "X::~X" it's OK. */ | |
2889 | if (scope && TYPE_P (scope) | |
2890 | && cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
2891 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
2892 | == CPP_OPEN_PAREN) | |
2893 | && (cp_lexer_peek_token (parser->lexer)->value | |
2894 | == TYPE_IDENTIFIER (scope))) | |
2895 | { | |
2896 | cp_lexer_consume_token (parser->lexer); | |
2897 | return build_nt (BIT_NOT_EXPR, scope); | |
2898 | } | |
2899 | ||
2900 | /* If there was an explicit qualification (S::~T), first look | |
2901 | in the scope given by the qualification (i.e., S). */ | |
2902 | if (scope) | |
2903 | { | |
2904 | cp_parser_parse_tentatively (parser); | |
2905 | type_decl = cp_parser_class_name (parser, | |
2906 | /*typename_keyword_p=*/false, | |
2907 | /*template_keyword_p=*/false, | |
2908 | /*type_p=*/false, | |
a723baf1 | 2909 | /*check_dependency=*/false, |
a668c6ad MM |
2910 | /*class_head_p=*/false, |
2911 | declarator_p); | |
a723baf1 MM |
2912 | if (cp_parser_parse_definitely (parser)) |
2913 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); | |
2914 | } | |
2915 | /* In "N::S::~S", look in "N" as well. */ | |
2916 | if (scope && qualifying_scope) | |
2917 | { | |
2918 | cp_parser_parse_tentatively (parser); | |
2919 | parser->scope = qualifying_scope; | |
2920 | parser->object_scope = NULL_TREE; | |
2921 | parser->qualifying_scope = NULL_TREE; | |
2922 | type_decl | |
2923 | = cp_parser_class_name (parser, | |
2924 | /*typename_keyword_p=*/false, | |
2925 | /*template_keyword_p=*/false, | |
2926 | /*type_p=*/false, | |
a723baf1 | 2927 | /*check_dependency=*/false, |
a668c6ad MM |
2928 | /*class_head_p=*/false, |
2929 | declarator_p); | |
a723baf1 MM |
2930 | if (cp_parser_parse_definitely (parser)) |
2931 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); | |
2932 | } | |
2933 | /* In "p->S::~T", look in the scope given by "*p" as well. */ | |
2934 | else if (object_scope) | |
2935 | { | |
2936 | cp_parser_parse_tentatively (parser); | |
2937 | parser->scope = object_scope; | |
2938 | parser->object_scope = NULL_TREE; | |
2939 | parser->qualifying_scope = NULL_TREE; | |
2940 | type_decl | |
2941 | = cp_parser_class_name (parser, | |
2942 | /*typename_keyword_p=*/false, | |
2943 | /*template_keyword_p=*/false, | |
2944 | /*type_p=*/false, | |
a723baf1 | 2945 | /*check_dependency=*/false, |
a668c6ad MM |
2946 | /*class_head_p=*/false, |
2947 | declarator_p); | |
a723baf1 MM |
2948 | if (cp_parser_parse_definitely (parser)) |
2949 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); | |
2950 | } | |
2951 | /* Look in the surrounding context. */ | |
2952 | parser->scope = NULL_TREE; | |
2953 | parser->object_scope = NULL_TREE; | |
2954 | parser->qualifying_scope = NULL_TREE; | |
2955 | type_decl | |
2956 | = cp_parser_class_name (parser, | |
2957 | /*typename_keyword_p=*/false, | |
2958 | /*template_keyword_p=*/false, | |
2959 | /*type_p=*/false, | |
a723baf1 | 2960 | /*check_dependency=*/false, |
a668c6ad MM |
2961 | /*class_head_p=*/false, |
2962 | declarator_p); | |
a723baf1 MM |
2963 | /* If an error occurred, assume that the name of the |
2964 | destructor is the same as the name of the qualifying | |
2965 | class. That allows us to keep parsing after running | |
2966 | into ill-formed destructor names. */ | |
2967 | if (type_decl == error_mark_node && scope && TYPE_P (scope)) | |
2968 | return build_nt (BIT_NOT_EXPR, scope); | |
2969 | else if (type_decl == error_mark_node) | |
2970 | return error_mark_node; | |
2971 | ||
f3c2dfc6 MM |
2972 | /* [class.dtor] |
2973 | ||
2974 | A typedef-name that names a class shall not be used as the | |
2975 | identifier in the declarator for a destructor declaration. */ | |
2976 | if (declarator_p | |
2977 | && !DECL_IMPLICIT_TYPEDEF_P (type_decl) | |
2978 | && !DECL_SELF_REFERENCE_P (type_decl)) | |
2979 | error ("typedef-name `%D' used as destructor declarator", | |
2980 | type_decl); | |
2981 | ||
a723baf1 MM |
2982 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); |
2983 | } | |
2984 | ||
2985 | case CPP_KEYWORD: | |
2986 | if (token->keyword == RID_OPERATOR) | |
2987 | { | |
2988 | tree id; | |
2989 | ||
2990 | /* This could be a template-id, so we try that first. */ | |
2991 | cp_parser_parse_tentatively (parser); | |
2992 | /* Try a template-id. */ | |
2993 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
2994 | /*check_dependency_p=*/true, |
2995 | declarator_p); | |
a723baf1 MM |
2996 | /* If that worked, we're done. */ |
2997 | if (cp_parser_parse_definitely (parser)) | |
2998 | return id; | |
2999 | /* We still don't know whether we're looking at an | |
3000 | operator-function-id or a conversion-function-id. */ | |
3001 | cp_parser_parse_tentatively (parser); | |
3002 | /* Try an operator-function-id. */ | |
3003 | id = cp_parser_operator_function_id (parser); | |
3004 | /* If that didn't work, try a conversion-function-id. */ | |
3005 | if (!cp_parser_parse_definitely (parser)) | |
3006 | id = cp_parser_conversion_function_id (parser); | |
3007 | ||
3008 | return id; | |
3009 | } | |
3010 | /* Fall through. */ | |
3011 | ||
3012 | default: | |
3013 | cp_parser_error (parser, "expected unqualified-id"); | |
3014 | return error_mark_node; | |
3015 | } | |
3016 | } | |
3017 | ||
3018 | /* Parse an (optional) nested-name-specifier. | |
3019 | ||
3020 | nested-name-specifier: | |
3021 | class-or-namespace-name :: nested-name-specifier [opt] | |
3022 | class-or-namespace-name :: template nested-name-specifier [opt] | |
3023 | ||
3024 | PARSER->SCOPE should be set appropriately before this function is | |
3025 | called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in | |
3026 | effect. TYPE_P is TRUE if we non-type bindings should be ignored | |
3027 | in name lookups. | |
3028 | ||
3029 | Sets PARSER->SCOPE to the class (TYPE) or namespace | |
3030 | (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves | |
3031 | it unchanged if there is no nested-name-specifier. Returns the new | |
a668c6ad MM |
3032 | scope iff there is a nested-name-specifier, or NULL_TREE otherwise. |
3033 | ||
3034 | If IS_DECLARATION is TRUE, the nested-name-specifier is known to be | |
3035 | part of a declaration and/or decl-specifier. */ | |
a723baf1 MM |
3036 | |
3037 | static tree | |
3038 | cp_parser_nested_name_specifier_opt (cp_parser *parser, | |
3039 | bool typename_keyword_p, | |
3040 | bool check_dependency_p, | |
a668c6ad MM |
3041 | bool type_p, |
3042 | bool is_declaration) | |
a723baf1 MM |
3043 | { |
3044 | bool success = false; | |
3045 | tree access_check = NULL_TREE; | |
3046 | ptrdiff_t start; | |
2050a1bb | 3047 | cp_token* token; |
a723baf1 MM |
3048 | |
3049 | /* If the next token corresponds to a nested name specifier, there | |
2050a1bb MM |
3050 | is no need to reparse it. However, if CHECK_DEPENDENCY_P is |
3051 | false, it may have been true before, in which case something | |
3052 | like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier | |
3053 | of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when | |
3054 | CHECK_DEPENDENCY_P is false, we have to fall through into the | |
3055 | main loop. */ | |
3056 | if (check_dependency_p | |
3057 | && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER)) | |
3058 | { | |
3059 | cp_parser_pre_parsed_nested_name_specifier (parser); | |
a723baf1 MM |
3060 | return parser->scope; |
3061 | } | |
3062 | ||
3063 | /* Remember where the nested-name-specifier starts. */ | |
3064 | if (cp_parser_parsing_tentatively (parser) | |
3065 | && !cp_parser_committed_to_tentative_parse (parser)) | |
3066 | { | |
2050a1bb | 3067 | token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
3068 | start = cp_lexer_token_difference (parser->lexer, |
3069 | parser->lexer->first_token, | |
2050a1bb | 3070 | token); |
a723baf1 MM |
3071 | } |
3072 | else | |
3073 | start = -1; | |
3074 | ||
8d241e0b | 3075 | push_deferring_access_checks (dk_deferred); |
cf22909c | 3076 | |
a723baf1 MM |
3077 | while (true) |
3078 | { | |
3079 | tree new_scope; | |
3080 | tree old_scope; | |
3081 | tree saved_qualifying_scope; | |
a723baf1 MM |
3082 | bool template_keyword_p; |
3083 | ||
2050a1bb MM |
3084 | /* Spot cases that cannot be the beginning of a |
3085 | nested-name-specifier. */ | |
3086 | token = cp_lexer_peek_token (parser->lexer); | |
3087 | ||
3088 | /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process | |
3089 | the already parsed nested-name-specifier. */ | |
3090 | if (token->type == CPP_NESTED_NAME_SPECIFIER) | |
3091 | { | |
3092 | /* Grab the nested-name-specifier and continue the loop. */ | |
3093 | cp_parser_pre_parsed_nested_name_specifier (parser); | |
3094 | success = true; | |
3095 | continue; | |
3096 | } | |
3097 | ||
a723baf1 MM |
3098 | /* Spot cases that cannot be the beginning of a |
3099 | nested-name-specifier. On the second and subsequent times | |
3100 | through the loop, we look for the `template' keyword. */ | |
f7b5ecd9 | 3101 | if (success && token->keyword == RID_TEMPLATE) |
a723baf1 MM |
3102 | ; |
3103 | /* A template-id can start a nested-name-specifier. */ | |
f7b5ecd9 | 3104 | else if (token->type == CPP_TEMPLATE_ID) |
a723baf1 MM |
3105 | ; |
3106 | else | |
3107 | { | |
3108 | /* If the next token is not an identifier, then it is | |
3109 | definitely not a class-or-namespace-name. */ | |
f7b5ecd9 | 3110 | if (token->type != CPP_NAME) |
a723baf1 MM |
3111 | break; |
3112 | /* If the following token is neither a `<' (to begin a | |
3113 | template-id), nor a `::', then we are not looking at a | |
3114 | nested-name-specifier. */ | |
3115 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
3116 | if (token->type != CPP_LESS && token->type != CPP_SCOPE) | |
3117 | break; | |
3118 | } | |
3119 | ||
3120 | /* The nested-name-specifier is optional, so we parse | |
3121 | tentatively. */ | |
3122 | cp_parser_parse_tentatively (parser); | |
3123 | ||
3124 | /* Look for the optional `template' keyword, if this isn't the | |
3125 | first time through the loop. */ | |
3126 | if (success) | |
3127 | template_keyword_p = cp_parser_optional_template_keyword (parser); | |
3128 | else | |
3129 | template_keyword_p = false; | |
3130 | ||
3131 | /* Save the old scope since the name lookup we are about to do | |
3132 | might destroy it. */ | |
3133 | old_scope = parser->scope; | |
3134 | saved_qualifying_scope = parser->qualifying_scope; | |
3135 | /* Parse the qualifying entity. */ | |
3136 | new_scope | |
3137 | = cp_parser_class_or_namespace_name (parser, | |
3138 | typename_keyword_p, | |
3139 | template_keyword_p, | |
3140 | check_dependency_p, | |
a668c6ad MM |
3141 | type_p, |
3142 | is_declaration); | |
a723baf1 MM |
3143 | /* Look for the `::' token. */ |
3144 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
3145 | ||
3146 | /* If we found what we wanted, we keep going; otherwise, we're | |
3147 | done. */ | |
3148 | if (!cp_parser_parse_definitely (parser)) | |
3149 | { | |
3150 | bool error_p = false; | |
3151 | ||
3152 | /* Restore the OLD_SCOPE since it was valid before the | |
3153 | failed attempt at finding the last | |
3154 | class-or-namespace-name. */ | |
3155 | parser->scope = old_scope; | |
3156 | parser->qualifying_scope = saved_qualifying_scope; | |
3157 | /* If the next token is an identifier, and the one after | |
3158 | that is a `::', then any valid interpretation would have | |
3159 | found a class-or-namespace-name. */ | |
3160 | while (cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
3161 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
3162 | == CPP_SCOPE) | |
3163 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type | |
3164 | != CPP_COMPL)) | |
3165 | { | |
3166 | token = cp_lexer_consume_token (parser->lexer); | |
3167 | if (!error_p) | |
3168 | { | |
3169 | tree decl; | |
3170 | ||
3171 | decl = cp_parser_lookup_name_simple (parser, token->value); | |
3172 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
3173 | error ("`%D' used without template parameters", | |
3174 | decl); | |
a723baf1 | 3175 | else |
4bb8ca28 MM |
3176 | cp_parser_name_lookup_error |
3177 | (parser, token->value, decl, | |
3178 | "is not a class or namespace"); | |
a723baf1 MM |
3179 | parser->scope = NULL_TREE; |
3180 | error_p = true; | |
eea9800f MM |
3181 | /* Treat this as a successful nested-name-specifier |
3182 | due to: | |
3183 | ||
3184 | [basic.lookup.qual] | |
3185 | ||
3186 | If the name found is not a class-name (clause | |
3187 | _class_) or namespace-name (_namespace.def_), the | |
3188 | program is ill-formed. */ | |
3189 | success = true; | |
a723baf1 MM |
3190 | } |
3191 | cp_lexer_consume_token (parser->lexer); | |
3192 | } | |
3193 | break; | |
3194 | } | |
3195 | ||
3196 | /* We've found one valid nested-name-specifier. */ | |
3197 | success = true; | |
3198 | /* Make sure we look in the right scope the next time through | |
3199 | the loop. */ | |
3200 | parser->scope = (TREE_CODE (new_scope) == TYPE_DECL | |
3201 | ? TREE_TYPE (new_scope) | |
3202 | : new_scope); | |
3203 | /* If it is a class scope, try to complete it; we are about to | |
3204 | be looking up names inside the class. */ | |
8fbc5ae7 MM |
3205 | if (TYPE_P (parser->scope) |
3206 | /* Since checking types for dependency can be expensive, | |
3207 | avoid doing it if the type is already complete. */ | |
3208 | && !COMPLETE_TYPE_P (parser->scope) | |
3209 | /* Do not try to complete dependent types. */ | |
1fb3244a | 3210 | && !dependent_type_p (parser->scope)) |
a723baf1 MM |
3211 | complete_type (parser->scope); |
3212 | } | |
3213 | ||
cf22909c KL |
3214 | /* Retrieve any deferred checks. Do not pop this access checks yet |
3215 | so the memory will not be reclaimed during token replacing below. */ | |
3216 | access_check = get_deferred_access_checks (); | |
3217 | ||
a723baf1 MM |
3218 | /* If parsing tentatively, replace the sequence of tokens that makes |
3219 | up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER | |
3220 | token. That way, should we re-parse the token stream, we will | |
3221 | not have to repeat the effort required to do the parse, nor will | |
3222 | we issue duplicate error messages. */ | |
3223 | if (success && start >= 0) | |
3224 | { | |
a723baf1 MM |
3225 | /* Find the token that corresponds to the start of the |
3226 | template-id. */ | |
3227 | token = cp_lexer_advance_token (parser->lexer, | |
3228 | parser->lexer->first_token, | |
3229 | start); | |
3230 | ||
a723baf1 MM |
3231 | /* Reset the contents of the START token. */ |
3232 | token->type = CPP_NESTED_NAME_SPECIFIER; | |
3233 | token->value = build_tree_list (access_check, parser->scope); | |
3234 | TREE_TYPE (token->value) = parser->qualifying_scope; | |
3235 | token->keyword = RID_MAX; | |
3236 | /* Purge all subsequent tokens. */ | |
3237 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
3238 | } | |
3239 | ||
cf22909c | 3240 | pop_deferring_access_checks (); |
a723baf1 MM |
3241 | return success ? parser->scope : NULL_TREE; |
3242 | } | |
3243 | ||
3244 | /* Parse a nested-name-specifier. See | |
3245 | cp_parser_nested_name_specifier_opt for details. This function | |
3246 | behaves identically, except that it will an issue an error if no | |
3247 | nested-name-specifier is present, and it will return | |
3248 | ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier | |
3249 | is present. */ | |
3250 | ||
3251 | static tree | |
3252 | cp_parser_nested_name_specifier (cp_parser *parser, | |
3253 | bool typename_keyword_p, | |
3254 | bool check_dependency_p, | |
a668c6ad MM |
3255 | bool type_p, |
3256 | bool is_declaration) | |
a723baf1 MM |
3257 | { |
3258 | tree scope; | |
3259 | ||
3260 | /* Look for the nested-name-specifier. */ | |
3261 | scope = cp_parser_nested_name_specifier_opt (parser, | |
3262 | typename_keyword_p, | |
3263 | check_dependency_p, | |
a668c6ad MM |
3264 | type_p, |
3265 | is_declaration); | |
a723baf1 MM |
3266 | /* If it was not present, issue an error message. */ |
3267 | if (!scope) | |
3268 | { | |
3269 | cp_parser_error (parser, "expected nested-name-specifier"); | |
eb5abb39 | 3270 | parser->scope = NULL_TREE; |
a723baf1 MM |
3271 | return error_mark_node; |
3272 | } | |
3273 | ||
3274 | return scope; | |
3275 | } | |
3276 | ||
3277 | /* Parse a class-or-namespace-name. | |
3278 | ||
3279 | class-or-namespace-name: | |
3280 | class-name | |
3281 | namespace-name | |
3282 | ||
3283 | TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect. | |
3284 | TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect. | |
3285 | CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up. | |
3286 | TYPE_P is TRUE iff the next name should be taken as a class-name, | |
3287 | even the same name is declared to be another entity in the same | |
3288 | scope. | |
3289 | ||
3290 | Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL) | |
eea9800f MM |
3291 | specified by the class-or-namespace-name. If neither is found the |
3292 | ERROR_MARK_NODE is returned. */ | |
a723baf1 MM |
3293 | |
3294 | static tree | |
3295 | cp_parser_class_or_namespace_name (cp_parser *parser, | |
3296 | bool typename_keyword_p, | |
3297 | bool template_keyword_p, | |
3298 | bool check_dependency_p, | |
a668c6ad MM |
3299 | bool type_p, |
3300 | bool is_declaration) | |
a723baf1 MM |
3301 | { |
3302 | tree saved_scope; | |
3303 | tree saved_qualifying_scope; | |
3304 | tree saved_object_scope; | |
3305 | tree scope; | |
eea9800f | 3306 | bool only_class_p; |
a723baf1 | 3307 | |
a723baf1 MM |
3308 | /* Before we try to parse the class-name, we must save away the |
3309 | current PARSER->SCOPE since cp_parser_class_name will destroy | |
3310 | it. */ | |
3311 | saved_scope = parser->scope; | |
3312 | saved_qualifying_scope = parser->qualifying_scope; | |
3313 | saved_object_scope = parser->object_scope; | |
eea9800f MM |
3314 | /* Try for a class-name first. If the SAVED_SCOPE is a type, then |
3315 | there is no need to look for a namespace-name. */ | |
bbaab916 | 3316 | only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope)); |
eea9800f MM |
3317 | if (!only_class_p) |
3318 | cp_parser_parse_tentatively (parser); | |
a723baf1 MM |
3319 | scope = cp_parser_class_name (parser, |
3320 | typename_keyword_p, | |
3321 | template_keyword_p, | |
3322 | type_p, | |
a723baf1 | 3323 | check_dependency_p, |
a668c6ad MM |
3324 | /*class_head_p=*/false, |
3325 | is_declaration); | |
a723baf1 | 3326 | /* If that didn't work, try for a namespace-name. */ |
eea9800f | 3327 | if (!only_class_p && !cp_parser_parse_definitely (parser)) |
a723baf1 MM |
3328 | { |
3329 | /* Restore the saved scope. */ | |
3330 | parser->scope = saved_scope; | |
3331 | parser->qualifying_scope = saved_qualifying_scope; | |
3332 | parser->object_scope = saved_object_scope; | |
eea9800f MM |
3333 | /* If we are not looking at an identifier followed by the scope |
3334 | resolution operator, then this is not part of a | |
3335 | nested-name-specifier. (Note that this function is only used | |
3336 | to parse the components of a nested-name-specifier.) */ | |
3337 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME) | |
3338 | || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE) | |
3339 | return error_mark_node; | |
a723baf1 MM |
3340 | scope = cp_parser_namespace_name (parser); |
3341 | } | |
3342 | ||
3343 | return scope; | |
3344 | } | |
3345 | ||
3346 | /* Parse a postfix-expression. | |
3347 | ||
3348 | postfix-expression: | |
3349 | primary-expression | |
3350 | postfix-expression [ expression ] | |
3351 | postfix-expression ( expression-list [opt] ) | |
3352 | simple-type-specifier ( expression-list [opt] ) | |
3353 | typename :: [opt] nested-name-specifier identifier | |
3354 | ( expression-list [opt] ) | |
3355 | typename :: [opt] nested-name-specifier template [opt] template-id | |
3356 | ( expression-list [opt] ) | |
3357 | postfix-expression . template [opt] id-expression | |
3358 | postfix-expression -> template [opt] id-expression | |
3359 | postfix-expression . pseudo-destructor-name | |
3360 | postfix-expression -> pseudo-destructor-name | |
3361 | postfix-expression ++ | |
3362 | postfix-expression -- | |
3363 | dynamic_cast < type-id > ( expression ) | |
3364 | static_cast < type-id > ( expression ) | |
3365 | reinterpret_cast < type-id > ( expression ) | |
3366 | const_cast < type-id > ( expression ) | |
3367 | typeid ( expression ) | |
3368 | typeid ( type-id ) | |
3369 | ||
3370 | GNU Extension: | |
3371 | ||
3372 | postfix-expression: | |
3373 | ( type-id ) { initializer-list , [opt] } | |
3374 | ||
3375 | This extension is a GNU version of the C99 compound-literal | |
3376 | construct. (The C99 grammar uses `type-name' instead of `type-id', | |
3377 | but they are essentially the same concept.) | |
3378 | ||
3379 | If ADDRESS_P is true, the postfix expression is the operand of the | |
3380 | `&' operator. | |
3381 | ||
3382 | Returns a representation of the expression. */ | |
3383 | ||
3384 | static tree | |
3385 | cp_parser_postfix_expression (cp_parser *parser, bool address_p) | |
3386 | { | |
3387 | cp_token *token; | |
3388 | enum rid keyword; | |
b3445994 | 3389 | cp_id_kind idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3390 | tree postfix_expression = NULL_TREE; |
3391 | /* Non-NULL only if the current postfix-expression can be used to | |
3392 | form a pointer-to-member. In that case, QUALIFYING_CLASS is the | |
3393 | class used to qualify the member. */ | |
3394 | tree qualifying_class = NULL_TREE; | |
a723baf1 MM |
3395 | |
3396 | /* Peek at the next token. */ | |
3397 | token = cp_lexer_peek_token (parser->lexer); | |
3398 | /* Some of the productions are determined by keywords. */ | |
3399 | keyword = token->keyword; | |
3400 | switch (keyword) | |
3401 | { | |
3402 | case RID_DYNCAST: | |
3403 | case RID_STATCAST: | |
3404 | case RID_REINTCAST: | |
3405 | case RID_CONSTCAST: | |
3406 | { | |
3407 | tree type; | |
3408 | tree expression; | |
3409 | const char *saved_message; | |
3410 | ||
3411 | /* All of these can be handled in the same way from the point | |
3412 | of view of parsing. Begin by consuming the token | |
3413 | identifying the cast. */ | |
3414 | cp_lexer_consume_token (parser->lexer); | |
3415 | ||
3416 | /* New types cannot be defined in the cast. */ | |
3417 | saved_message = parser->type_definition_forbidden_message; | |
3418 | parser->type_definition_forbidden_message | |
3419 | = "types may not be defined in casts"; | |
3420 | ||
3421 | /* Look for the opening `<'. */ | |
3422 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
3423 | /* Parse the type to which we are casting. */ | |
3424 | type = cp_parser_type_id (parser); | |
3425 | /* Look for the closing `>'. */ | |
3426 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
3427 | /* Restore the old message. */ | |
3428 | parser->type_definition_forbidden_message = saved_message; | |
3429 | ||
3430 | /* And the expression which is being cast. */ | |
3431 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
3432 | expression = cp_parser_expression (parser); | |
3433 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3434 | ||
14d22dd6 MM |
3435 | /* Only type conversions to integral or enumeration types |
3436 | can be used in constant-expressions. */ | |
67c03833 | 3437 | if (parser->integral_constant_expression_p |
14d22dd6 | 3438 | && !dependent_type_p (type) |
263ee052 MM |
3439 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
3440 | /* A cast to pointer or reference type is allowed in the | |
3441 | implementation of "offsetof". */ | |
3442 | && !(parser->in_offsetof_p && POINTER_TYPE_P (type))) | |
14d22dd6 | 3443 | { |
67c03833 JM |
3444 | if (!parser->allow_non_integral_constant_expression_p) |
3445 | return (cp_parser_non_integral_constant_expression | |
14d22dd6 MM |
3446 | ("a cast to a type other than an integral or " |
3447 | "enumeration type")); | |
67c03833 | 3448 | parser->non_integral_constant_expression_p = true; |
14d22dd6 MM |
3449 | } |
3450 | ||
a723baf1 MM |
3451 | switch (keyword) |
3452 | { | |
3453 | case RID_DYNCAST: | |
3454 | postfix_expression | |
3455 | = build_dynamic_cast (type, expression); | |
3456 | break; | |
3457 | case RID_STATCAST: | |
3458 | postfix_expression | |
3459 | = build_static_cast (type, expression); | |
3460 | break; | |
3461 | case RID_REINTCAST: | |
3462 | postfix_expression | |
3463 | = build_reinterpret_cast (type, expression); | |
3464 | break; | |
3465 | case RID_CONSTCAST: | |
3466 | postfix_expression | |
3467 | = build_const_cast (type, expression); | |
3468 | break; | |
3469 | default: | |
3470 | abort (); | |
3471 | } | |
3472 | } | |
3473 | break; | |
3474 | ||
3475 | case RID_TYPEID: | |
3476 | { | |
3477 | tree type; | |
3478 | const char *saved_message; | |
4f8163b1 | 3479 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
3480 | |
3481 | /* Consume the `typeid' token. */ | |
3482 | cp_lexer_consume_token (parser->lexer); | |
3483 | /* Look for the `(' token. */ | |
3484 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
3485 | /* Types cannot be defined in a `typeid' expression. */ | |
3486 | saved_message = parser->type_definition_forbidden_message; | |
3487 | parser->type_definition_forbidden_message | |
3488 | = "types may not be defined in a `typeid\' expression"; | |
3489 | /* We can't be sure yet whether we're looking at a type-id or an | |
3490 | expression. */ | |
3491 | cp_parser_parse_tentatively (parser); | |
3492 | /* Try a type-id first. */ | |
4f8163b1 MM |
3493 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
3494 | parser->in_type_id_in_expr_p = true; | |
a723baf1 | 3495 | type = cp_parser_type_id (parser); |
4f8163b1 | 3496 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 MM |
3497 | /* Look for the `)' token. Otherwise, we can't be sure that |
3498 | we're not looking at an expression: consider `typeid (int | |
3499 | (3))', for example. */ | |
3500 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3501 | /* If all went well, simply lookup the type-id. */ | |
3502 | if (cp_parser_parse_definitely (parser)) | |
3503 | postfix_expression = get_typeid (type); | |
3504 | /* Otherwise, fall back to the expression variant. */ | |
3505 | else | |
3506 | { | |
3507 | tree expression; | |
3508 | ||
3509 | /* Look for an expression. */ | |
3510 | expression = cp_parser_expression (parser); | |
3511 | /* Compute its typeid. */ | |
3512 | postfix_expression = build_typeid (expression); | |
3513 | /* Look for the `)' token. */ | |
3514 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3515 | } | |
3516 | ||
3517 | /* Restore the saved message. */ | |
3518 | parser->type_definition_forbidden_message = saved_message; | |
3519 | } | |
3520 | break; | |
3521 | ||
3522 | case RID_TYPENAME: | |
3523 | { | |
3524 | bool template_p = false; | |
3525 | tree id; | |
3526 | tree type; | |
3527 | ||
3528 | /* Consume the `typename' token. */ | |
3529 | cp_lexer_consume_token (parser->lexer); | |
3530 | /* Look for the optional `::' operator. */ | |
3531 | cp_parser_global_scope_opt (parser, | |
3532 | /*current_scope_valid_p=*/false); | |
3533 | /* Look for the nested-name-specifier. */ | |
3534 | cp_parser_nested_name_specifier (parser, | |
3535 | /*typename_keyword_p=*/true, | |
3536 | /*check_dependency_p=*/true, | |
a668c6ad MM |
3537 | /*type_p=*/true, |
3538 | /*is_declaration=*/true); | |
a723baf1 MM |
3539 | /* Look for the optional `template' keyword. */ |
3540 | template_p = cp_parser_optional_template_keyword (parser); | |
3541 | /* We don't know whether we're looking at a template-id or an | |
3542 | identifier. */ | |
3543 | cp_parser_parse_tentatively (parser); | |
3544 | /* Try a template-id. */ | |
3545 | id = cp_parser_template_id (parser, template_p, | |
a668c6ad MM |
3546 | /*check_dependency_p=*/true, |
3547 | /*is_declaration=*/true); | |
a723baf1 MM |
3548 | /* If that didn't work, try an identifier. */ |
3549 | if (!cp_parser_parse_definitely (parser)) | |
3550 | id = cp_parser_identifier (parser); | |
3551 | /* Create a TYPENAME_TYPE to represent the type to which the | |
3552 | functional cast is being performed. */ | |
3553 | type = make_typename_type (parser->scope, id, | |
3554 | /*complain=*/1); | |
3555 | ||
3556 | postfix_expression = cp_parser_functional_cast (parser, type); | |
3557 | } | |
3558 | break; | |
3559 | ||
3560 | default: | |
3561 | { | |
3562 | tree type; | |
3563 | ||
3564 | /* If the next thing is a simple-type-specifier, we may be | |
3565 | looking at a functional cast. We could also be looking at | |
3566 | an id-expression. So, we try the functional cast, and if | |
3567 | that doesn't work we fall back to the primary-expression. */ | |
3568 | cp_parser_parse_tentatively (parser); | |
3569 | /* Look for the simple-type-specifier. */ | |
3570 | type = cp_parser_simple_type_specifier (parser, | |
4b0d3cbe MM |
3571 | CP_PARSER_FLAGS_NONE, |
3572 | /*identifier_p=*/false); | |
a723baf1 MM |
3573 | /* Parse the cast itself. */ |
3574 | if (!cp_parser_error_occurred (parser)) | |
3575 | postfix_expression | |
3576 | = cp_parser_functional_cast (parser, type); | |
3577 | /* If that worked, we're done. */ | |
3578 | if (cp_parser_parse_definitely (parser)) | |
3579 | break; | |
3580 | ||
3581 | /* If the functional-cast didn't work out, try a | |
3582 | compound-literal. */ | |
14d22dd6 MM |
3583 | if (cp_parser_allow_gnu_extensions_p (parser) |
3584 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
a723baf1 MM |
3585 | { |
3586 | tree initializer_list = NULL_TREE; | |
4f8163b1 | 3587 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
3588 | |
3589 | cp_parser_parse_tentatively (parser); | |
14d22dd6 MM |
3590 | /* Consume the `('. */ |
3591 | cp_lexer_consume_token (parser->lexer); | |
3592 | /* Parse the type. */ | |
4f8163b1 MM |
3593 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
3594 | parser->in_type_id_in_expr_p = true; | |
14d22dd6 | 3595 | type = cp_parser_type_id (parser); |
4f8163b1 | 3596 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
14d22dd6 MM |
3597 | /* Look for the `)'. */ |
3598 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3599 | /* Look for the `{'. */ | |
3600 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
3601 | /* If things aren't going well, there's no need to | |
3602 | keep going. */ | |
3603 | if (!cp_parser_error_occurred (parser)) | |
a723baf1 | 3604 | { |
39703eb9 | 3605 | bool non_constant_p; |
14d22dd6 MM |
3606 | /* Parse the initializer-list. */ |
3607 | initializer_list | |
39703eb9 | 3608 | = cp_parser_initializer_list (parser, &non_constant_p); |
14d22dd6 MM |
3609 | /* Allow a trailing `,'. */ |
3610 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
3611 | cp_lexer_consume_token (parser->lexer); | |
3612 | /* Look for the final `}'. */ | |
3613 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
a723baf1 MM |
3614 | } |
3615 | /* If that worked, we're definitely looking at a | |
3616 | compound-literal expression. */ | |
3617 | if (cp_parser_parse_definitely (parser)) | |
3618 | { | |
3619 | /* Warn the user that a compound literal is not | |
3620 | allowed in standard C++. */ | |
3621 | if (pedantic) | |
3622 | pedwarn ("ISO C++ forbids compound-literals"); | |
3623 | /* Form the representation of the compound-literal. */ | |
3624 | postfix_expression | |
3625 | = finish_compound_literal (type, initializer_list); | |
3626 | break; | |
3627 | } | |
3628 | } | |
3629 | ||
3630 | /* It must be a primary-expression. */ | |
3631 | postfix_expression = cp_parser_primary_expression (parser, | |
3632 | &idk, | |
3633 | &qualifying_class); | |
3634 | } | |
3635 | break; | |
3636 | } | |
3637 | ||
ee76b931 MM |
3638 | /* If we were avoiding committing to the processing of a |
3639 | qualified-id until we knew whether or not we had a | |
3640 | pointer-to-member, we now know. */ | |
089d6ea7 | 3641 | if (qualifying_class) |
a723baf1 | 3642 | { |
ee76b931 | 3643 | bool done; |
a723baf1 | 3644 | |
ee76b931 MM |
3645 | /* Peek at the next token. */ |
3646 | token = cp_lexer_peek_token (parser->lexer); | |
3647 | done = (token->type != CPP_OPEN_SQUARE | |
3648 | && token->type != CPP_OPEN_PAREN | |
3649 | && token->type != CPP_DOT | |
3650 | && token->type != CPP_DEREF | |
3651 | && token->type != CPP_PLUS_PLUS | |
3652 | && token->type != CPP_MINUS_MINUS); | |
3653 | ||
3654 | postfix_expression = finish_qualified_id_expr (qualifying_class, | |
3655 | postfix_expression, | |
3656 | done, | |
3657 | address_p); | |
3658 | if (done) | |
3659 | return postfix_expression; | |
a723baf1 MM |
3660 | } |
3661 | ||
a723baf1 MM |
3662 | /* Keep looping until the postfix-expression is complete. */ |
3663 | while (true) | |
3664 | { | |
10b1d5e7 MM |
3665 | if (idk == CP_ID_KIND_UNQUALIFIED |
3666 | && TREE_CODE (postfix_expression) == IDENTIFIER_NODE | |
a723baf1 | 3667 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) |
b3445994 MM |
3668 | /* It is not a Koenig lookup function call. */ |
3669 | postfix_expression | |
3670 | = unqualified_name_lookup_error (postfix_expression); | |
a723baf1 MM |
3671 | |
3672 | /* Peek at the next token. */ | |
3673 | token = cp_lexer_peek_token (parser->lexer); | |
3674 | ||
3675 | switch (token->type) | |
3676 | { | |
3677 | case CPP_OPEN_SQUARE: | |
3678 | /* postfix-expression [ expression ] */ | |
3679 | { | |
3680 | tree index; | |
3681 | ||
3682 | /* Consume the `[' token. */ | |
3683 | cp_lexer_consume_token (parser->lexer); | |
3684 | /* Parse the index expression. */ | |
3685 | index = cp_parser_expression (parser); | |
3686 | /* Look for the closing `]'. */ | |
3687 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
3688 | ||
3689 | /* Build the ARRAY_REF. */ | |
3690 | postfix_expression | |
3691 | = grok_array_decl (postfix_expression, index); | |
b3445994 | 3692 | idk = CP_ID_KIND_NONE; |
a5ac3982 MM |
3693 | /* Array references are not permitted in |
3694 | constant-expressions. */ | |
67c03833 | 3695 | if (parser->integral_constant_expression_p) |
a5ac3982 | 3696 | { |
67c03833 | 3697 | if (!parser->allow_non_integral_constant_expression_p) |
a5ac3982 | 3698 | postfix_expression |
67c03833 JM |
3699 | = cp_parser_non_integral_constant_expression ("an array reference"); |
3700 | parser->non_integral_constant_expression_p = true; | |
a5ac3982 | 3701 | } |
a723baf1 MM |
3702 | } |
3703 | break; | |
3704 | ||
3705 | case CPP_OPEN_PAREN: | |
3706 | /* postfix-expression ( expression-list [opt] ) */ | |
3707 | { | |
6d80c4b9 | 3708 | bool koenig_p; |
39703eb9 MM |
3709 | tree args = (cp_parser_parenthesized_expression_list |
3710 | (parser, false, /*non_constant_p=*/NULL)); | |
a723baf1 | 3711 | |
7efa3e22 NS |
3712 | if (args == error_mark_node) |
3713 | { | |
3714 | postfix_expression = error_mark_node; | |
3715 | break; | |
3716 | } | |
3717 | ||
14d22dd6 MM |
3718 | /* Function calls are not permitted in |
3719 | constant-expressions. */ | |
67c03833 | 3720 | if (parser->integral_constant_expression_p) |
14d22dd6 | 3721 | { |
67c03833 | 3722 | if (!parser->allow_non_integral_constant_expression_p) |
a5ac3982 MM |
3723 | { |
3724 | postfix_expression | |
67c03833 | 3725 | = cp_parser_non_integral_constant_expression ("a function call"); |
a5ac3982 MM |
3726 | break; |
3727 | } | |
67c03833 | 3728 | parser->non_integral_constant_expression_p = true; |
14d22dd6 | 3729 | } |
a723baf1 | 3730 | |
6d80c4b9 | 3731 | koenig_p = false; |
399dedb9 NS |
3732 | if (idk == CP_ID_KIND_UNQUALIFIED) |
3733 | { | |
3734 | if (args | |
3735 | && (is_overloaded_fn (postfix_expression) | |
3736 | || DECL_P (postfix_expression) | |
3737 | || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)) | |
6d80c4b9 MM |
3738 | { |
3739 | koenig_p = true; | |
3740 | postfix_expression | |
3741 | = perform_koenig_lookup (postfix_expression, args); | |
3742 | } | |
399dedb9 NS |
3743 | else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE) |
3744 | postfix_expression | |
3745 | = unqualified_fn_lookup_error (postfix_expression); | |
3746 | } | |
3747 | ||
d17811fd | 3748 | if (TREE_CODE (postfix_expression) == COMPONENT_REF) |
a723baf1 | 3749 | { |
d17811fd MM |
3750 | tree instance = TREE_OPERAND (postfix_expression, 0); |
3751 | tree fn = TREE_OPERAND (postfix_expression, 1); | |
3752 | ||
3753 | if (processing_template_decl | |
3754 | && (type_dependent_expression_p (instance) | |
3755 | || (!BASELINK_P (fn) | |
3756 | && TREE_CODE (fn) != FIELD_DECL) | |
584672ee | 3757 | || type_dependent_expression_p (fn) |
d17811fd MM |
3758 | || any_type_dependent_arguments_p (args))) |
3759 | { | |
3760 | postfix_expression | |
3761 | = build_min_nt (CALL_EXPR, postfix_expression, args); | |
3762 | break; | |
3763 | } | |
3764 | ||
3765 | postfix_expression | |
3766 | = (build_new_method_call | |
3767 | (instance, fn, args, NULL_TREE, | |
b3445994 | 3768 | (idk == CP_ID_KIND_QUALIFIED |
d17811fd | 3769 | ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL))); |
a723baf1 | 3770 | } |
d17811fd MM |
3771 | else if (TREE_CODE (postfix_expression) == OFFSET_REF |
3772 | || TREE_CODE (postfix_expression) == MEMBER_REF | |
3773 | || TREE_CODE (postfix_expression) == DOTSTAR_EXPR) | |
a723baf1 MM |
3774 | postfix_expression = (build_offset_ref_call_from_tree |
3775 | (postfix_expression, args)); | |
b3445994 | 3776 | else if (idk == CP_ID_KIND_QUALIFIED) |
2050a1bb MM |
3777 | /* A call to a static class member, or a namespace-scope |
3778 | function. */ | |
3779 | postfix_expression | |
3780 | = finish_call_expr (postfix_expression, args, | |
6d80c4b9 MM |
3781 | /*disallow_virtual=*/true, |
3782 | koenig_p); | |
a723baf1 | 3783 | else |
2050a1bb MM |
3784 | /* All other function calls. */ |
3785 | postfix_expression | |
3786 | = finish_call_expr (postfix_expression, args, | |
6d80c4b9 MM |
3787 | /*disallow_virtual=*/false, |
3788 | koenig_p); | |
a723baf1 MM |
3789 | |
3790 | /* The POSTFIX_EXPRESSION is certainly no longer an id. */ | |
b3445994 | 3791 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3792 | } |
3793 | break; | |
3794 | ||
3795 | case CPP_DOT: | |
3796 | case CPP_DEREF: | |
3797 | /* postfix-expression . template [opt] id-expression | |
3798 | postfix-expression . pseudo-destructor-name | |
3799 | postfix-expression -> template [opt] id-expression | |
3800 | postfix-expression -> pseudo-destructor-name */ | |
3801 | { | |
3802 | tree name; | |
3803 | bool dependent_p; | |
3804 | bool template_p; | |
3805 | tree scope = NULL_TREE; | |
a5ac3982 | 3806 | enum cpp_ttype token_type = token->type; |
a723baf1 MM |
3807 | |
3808 | /* If this is a `->' operator, dereference the pointer. */ | |
3809 | if (token->type == CPP_DEREF) | |
3810 | postfix_expression = build_x_arrow (postfix_expression); | |
3811 | /* Check to see whether or not the expression is | |
3812 | type-dependent. */ | |
bbaab916 | 3813 | dependent_p = type_dependent_expression_p (postfix_expression); |
a723baf1 MM |
3814 | /* The identifier following the `->' or `.' is not |
3815 | qualified. */ | |
3816 | parser->scope = NULL_TREE; | |
3817 | parser->qualifying_scope = NULL_TREE; | |
3818 | parser->object_scope = NULL_TREE; | |
b3445994 | 3819 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3820 | /* Enter the scope corresponding to the type of the object |
3821 | given by the POSTFIX_EXPRESSION. */ | |
3822 | if (!dependent_p | |
3823 | && TREE_TYPE (postfix_expression) != NULL_TREE) | |
3824 | { | |
3825 | scope = TREE_TYPE (postfix_expression); | |
3826 | /* According to the standard, no expression should | |
3827 | ever have reference type. Unfortunately, we do not | |
3828 | currently match the standard in this respect in | |
3829 | that our internal representation of an expression | |
3830 | may have reference type even when the standard says | |
3831 | it does not. Therefore, we have to manually obtain | |
3832 | the underlying type here. */ | |
ee76b931 | 3833 | scope = non_reference (scope); |
a723baf1 MM |
3834 | /* The type of the POSTFIX_EXPRESSION must be |
3835 | complete. */ | |
3836 | scope = complete_type_or_else (scope, NULL_TREE); | |
3837 | /* Let the name lookup machinery know that we are | |
3838 | processing a class member access expression. */ | |
3839 | parser->context->object_type = scope; | |
3840 | /* If something went wrong, we want to be able to | |
3841 | discern that case, as opposed to the case where | |
3842 | there was no SCOPE due to the type of expression | |
3843 | being dependent. */ | |
3844 | if (!scope) | |
3845 | scope = error_mark_node; | |
3846 | } | |
3847 | ||
3848 | /* Consume the `.' or `->' operator. */ | |
3849 | cp_lexer_consume_token (parser->lexer); | |
3850 | /* If the SCOPE is not a scalar type, we are looking at an | |
3851 | ordinary class member access expression, rather than a | |
3852 | pseudo-destructor-name. */ | |
3853 | if (!scope || !SCALAR_TYPE_P (scope)) | |
3854 | { | |
3855 | template_p = cp_parser_optional_template_keyword (parser); | |
3856 | /* Parse the id-expression. */ | |
3857 | name = cp_parser_id_expression (parser, | |
3858 | template_p, | |
3859 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
3860 | /*template_p=*/NULL, |
3861 | /*declarator_p=*/false); | |
a723baf1 MM |
3862 | /* In general, build a SCOPE_REF if the member name is |
3863 | qualified. However, if the name was not dependent | |
3864 | and has already been resolved; there is no need to | |
3865 | build the SCOPE_REF. For example; | |
3866 | ||
3867 | struct X { void f(); }; | |
3868 | template <typename T> void f(T* t) { t->X::f(); } | |
3869 | ||
d17811fd MM |
3870 | Even though "t" is dependent, "X::f" is not and has |
3871 | been resolved to a BASELINK; there is no need to | |
a723baf1 | 3872 | include scope information. */ |
a6bd211d JM |
3873 | |
3874 | /* But we do need to remember that there was an explicit | |
3875 | scope for virtual function calls. */ | |
3876 | if (parser->scope) | |
b3445994 | 3877 | idk = CP_ID_KIND_QUALIFIED; |
a6bd211d | 3878 | |
a723baf1 MM |
3879 | if (name != error_mark_node |
3880 | && !BASELINK_P (name) | |
3881 | && parser->scope) | |
3882 | { | |
3883 | name = build_nt (SCOPE_REF, parser->scope, name); | |
3884 | parser->scope = NULL_TREE; | |
3885 | parser->qualifying_scope = NULL_TREE; | |
3886 | parser->object_scope = NULL_TREE; | |
3887 | } | |
3888 | postfix_expression | |
3889 | = finish_class_member_access_expr (postfix_expression, name); | |
3890 | } | |
3891 | /* Otherwise, try the pseudo-destructor-name production. */ | |
3892 | else | |
3893 | { | |
90808894 | 3894 | tree s = NULL_TREE; |
a723baf1 MM |
3895 | tree type; |
3896 | ||
3897 | /* Parse the pseudo-destructor-name. */ | |
3898 | cp_parser_pseudo_destructor_name (parser, &s, &type); | |
3899 | /* Form the call. */ | |
3900 | postfix_expression | |
3901 | = finish_pseudo_destructor_expr (postfix_expression, | |
3902 | s, TREE_TYPE (type)); | |
3903 | } | |
3904 | ||
3905 | /* We no longer need to look up names in the scope of the | |
3906 | object on the left-hand side of the `.' or `->' | |
3907 | operator. */ | |
3908 | parser->context->object_type = NULL_TREE; | |
a5ac3982 | 3909 | /* These operators may not appear in constant-expressions. */ |
67c03833 | 3910 | if (parser->integral_constant_expression_p |
263ee052 MM |
3911 | /* The "->" operator is allowed in the implementation |
3912 | of "offsetof". */ | |
3913 | && !(parser->in_offsetof_p && token_type == CPP_DEREF)) | |
a5ac3982 | 3914 | { |
67c03833 | 3915 | if (!parser->allow_non_integral_constant_expression_p) |
a5ac3982 | 3916 | postfix_expression |
67c03833 | 3917 | = (cp_parser_non_integral_constant_expression |
a5ac3982 | 3918 | (token_type == CPP_DEREF ? "'->'" : "`.'")); |
67c03833 | 3919 | parser->non_integral_constant_expression_p = true; |
a5ac3982 | 3920 | } |
a723baf1 MM |
3921 | } |
3922 | break; | |
3923 | ||
3924 | case CPP_PLUS_PLUS: | |
3925 | /* postfix-expression ++ */ | |
3926 | /* Consume the `++' token. */ | |
3927 | cp_lexer_consume_token (parser->lexer); | |
a5ac3982 MM |
3928 | /* Generate a representation for the complete expression. */ |
3929 | postfix_expression | |
3930 | = finish_increment_expr (postfix_expression, | |
3931 | POSTINCREMENT_EXPR); | |
14d22dd6 | 3932 | /* Increments may not appear in constant-expressions. */ |
67c03833 | 3933 | if (parser->integral_constant_expression_p) |
14d22dd6 | 3934 | { |
67c03833 | 3935 | if (!parser->allow_non_integral_constant_expression_p) |
a5ac3982 | 3936 | postfix_expression |
67c03833 JM |
3937 | = cp_parser_non_integral_constant_expression ("an increment"); |
3938 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 3939 | } |
b3445994 | 3940 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3941 | break; |
3942 | ||
3943 | case CPP_MINUS_MINUS: | |
3944 | /* postfix-expression -- */ | |
3945 | /* Consume the `--' token. */ | |
3946 | cp_lexer_consume_token (parser->lexer); | |
a5ac3982 MM |
3947 | /* Generate a representation for the complete expression. */ |
3948 | postfix_expression | |
3949 | = finish_increment_expr (postfix_expression, | |
3950 | POSTDECREMENT_EXPR); | |
14d22dd6 | 3951 | /* Decrements may not appear in constant-expressions. */ |
67c03833 | 3952 | if (parser->integral_constant_expression_p) |
14d22dd6 | 3953 | { |
67c03833 | 3954 | if (!parser->allow_non_integral_constant_expression_p) |
a5ac3982 | 3955 | postfix_expression |
67c03833 JM |
3956 | = cp_parser_non_integral_constant_expression ("a decrement"); |
3957 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 3958 | } |
b3445994 | 3959 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3960 | break; |
3961 | ||
3962 | default: | |
3963 | return postfix_expression; | |
3964 | } | |
3965 | } | |
3966 | ||
3967 | /* We should never get here. */ | |
3968 | abort (); | |
3969 | return error_mark_node; | |
3970 | } | |
3971 | ||
7efa3e22 | 3972 | /* Parse a parenthesized expression-list. |
a723baf1 MM |
3973 | |
3974 | expression-list: | |
3975 | assignment-expression | |
3976 | expression-list, assignment-expression | |
3977 | ||
7efa3e22 NS |
3978 | attribute-list: |
3979 | expression-list | |
3980 | identifier | |
3981 | identifier, expression-list | |
3982 | ||
a723baf1 MM |
3983 | Returns a TREE_LIST. The TREE_VALUE of each node is a |
3984 | representation of an assignment-expression. Note that a TREE_LIST | |
7efa3e22 NS |
3985 | is returned even if there is only a single expression in the list. |
3986 | error_mark_node is returned if the ( and or ) are | |
3987 | missing. NULL_TREE is returned on no expressions. The parentheses | |
3988 | are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute | |
39703eb9 MM |
3989 | list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P |
3990 | indicates whether or not all of the expressions in the list were | |
3991 | constant. */ | |
a723baf1 MM |
3992 | |
3993 | static tree | |
39703eb9 MM |
3994 | cp_parser_parenthesized_expression_list (cp_parser* parser, |
3995 | bool is_attribute_list, | |
3996 | bool *non_constant_p) | |
a723baf1 MM |
3997 | { |
3998 | tree expression_list = NULL_TREE; | |
7efa3e22 | 3999 | tree identifier = NULL_TREE; |
39703eb9 MM |
4000 | |
4001 | /* Assume all the expressions will be constant. */ | |
4002 | if (non_constant_p) | |
4003 | *non_constant_p = false; | |
4004 | ||
7efa3e22 NS |
4005 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) |
4006 | return error_mark_node; | |
4007 | ||
a723baf1 | 4008 | /* Consume expressions until there are no more. */ |
7efa3e22 NS |
4009 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) |
4010 | while (true) | |
4011 | { | |
4012 | tree expr; | |
4013 | ||
4014 | /* At the beginning of attribute lists, check to see if the | |
4015 | next token is an identifier. */ | |
4016 | if (is_attribute_list | |
4017 | && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME) | |
4018 | { | |
4019 | cp_token *token; | |
4020 | ||
4021 | /* Consume the identifier. */ | |
4022 | token = cp_lexer_consume_token (parser->lexer); | |
4023 | /* Save the identifier. */ | |
4024 | identifier = token->value; | |
4025 | } | |
4026 | else | |
4027 | { | |
4028 | /* Parse the next assignment-expression. */ | |
39703eb9 MM |
4029 | if (non_constant_p) |
4030 | { | |
4031 | bool expr_non_constant_p; | |
4032 | expr = (cp_parser_constant_expression | |
4033 | (parser, /*allow_non_constant_p=*/true, | |
4034 | &expr_non_constant_p)); | |
4035 | if (expr_non_constant_p) | |
4036 | *non_constant_p = true; | |
4037 | } | |
4038 | else | |
4039 | expr = cp_parser_assignment_expression (parser); | |
a723baf1 | 4040 | |
7efa3e22 NS |
4041 | /* Add it to the list. We add error_mark_node |
4042 | expressions to the list, so that we can still tell if | |
4043 | the correct form for a parenthesized expression-list | |
4044 | is found. That gives better errors. */ | |
4045 | expression_list = tree_cons (NULL_TREE, expr, expression_list); | |
a723baf1 | 4046 | |
7efa3e22 NS |
4047 | if (expr == error_mark_node) |
4048 | goto skip_comma; | |
4049 | } | |
a723baf1 | 4050 | |
7efa3e22 NS |
4051 | /* After the first item, attribute lists look the same as |
4052 | expression lists. */ | |
4053 | is_attribute_list = false; | |
4054 | ||
4055 | get_comma:; | |
4056 | /* If the next token isn't a `,', then we are done. */ | |
4057 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
4058 | break; | |
4059 | ||
4060 | /* Otherwise, consume the `,' and keep going. */ | |
4061 | cp_lexer_consume_token (parser->lexer); | |
4062 | } | |
4063 | ||
4064 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
4065 | { | |
4066 | int ending; | |
4067 | ||
4068 | skip_comma:; | |
4069 | /* We try and resync to an unnested comma, as that will give the | |
4070 | user better diagnostics. */ | |
4bb8ca28 MM |
4071 | ending = cp_parser_skip_to_closing_parenthesis (parser, |
4072 | /*recovering=*/true, | |
4073 | /*or_comma=*/true, | |
a668c6ad | 4074 | /*consume_paren=*/true); |
7efa3e22 NS |
4075 | if (ending < 0) |
4076 | goto get_comma; | |
4077 | if (!ending) | |
4078 | return error_mark_node; | |
a723baf1 MM |
4079 | } |
4080 | ||
4081 | /* We built up the list in reverse order so we must reverse it now. */ | |
7efa3e22 NS |
4082 | expression_list = nreverse (expression_list); |
4083 | if (identifier) | |
4084 | expression_list = tree_cons (NULL_TREE, identifier, expression_list); | |
4085 | ||
4086 | return expression_list; | |
a723baf1 MM |
4087 | } |
4088 | ||
4089 | /* Parse a pseudo-destructor-name. | |
4090 | ||
4091 | pseudo-destructor-name: | |
4092 | :: [opt] nested-name-specifier [opt] type-name :: ~ type-name | |
4093 | :: [opt] nested-name-specifier template template-id :: ~ type-name | |
4094 | :: [opt] nested-name-specifier [opt] ~ type-name | |
4095 | ||
4096 | If either of the first two productions is used, sets *SCOPE to the | |
4097 | TYPE specified before the final `::'. Otherwise, *SCOPE is set to | |
4098 | NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name, | |
4099 | or ERROR_MARK_NODE if no type-name is present. */ | |
4100 | ||
4101 | static void | |
94edc4ab NN |
4102 | cp_parser_pseudo_destructor_name (cp_parser* parser, |
4103 | tree* scope, | |
4104 | tree* type) | |
a723baf1 MM |
4105 | { |
4106 | bool nested_name_specifier_p; | |
4107 | ||
4108 | /* Look for the optional `::' operator. */ | |
4109 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true); | |
4110 | /* Look for the optional nested-name-specifier. */ | |
4111 | nested_name_specifier_p | |
4112 | = (cp_parser_nested_name_specifier_opt (parser, | |
4113 | /*typename_keyword_p=*/false, | |
4114 | /*check_dependency_p=*/true, | |
a668c6ad MM |
4115 | /*type_p=*/false, |
4116 | /*is_declaration=*/true) | |
a723baf1 MM |
4117 | != NULL_TREE); |
4118 | /* Now, if we saw a nested-name-specifier, we might be doing the | |
4119 | second production. */ | |
4120 | if (nested_name_specifier_p | |
4121 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
4122 | { | |
4123 | /* Consume the `template' keyword. */ | |
4124 | cp_lexer_consume_token (parser->lexer); | |
4125 | /* Parse the template-id. */ | |
4126 | cp_parser_template_id (parser, | |
4127 | /*template_keyword_p=*/true, | |
a668c6ad MM |
4128 | /*check_dependency_p=*/false, |
4129 | /*is_declaration=*/true); | |
a723baf1 MM |
4130 | /* Look for the `::' token. */ |
4131 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
4132 | } | |
4133 | /* If the next token is not a `~', then there might be some | |
9bcb9aae | 4134 | additional qualification. */ |
a723baf1 MM |
4135 | else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL)) |
4136 | { | |
4137 | /* Look for the type-name. */ | |
4138 | *scope = TREE_TYPE (cp_parser_type_name (parser)); | |
4139 | /* Look for the `::' token. */ | |
4140 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
4141 | } | |
4142 | else | |
4143 | *scope = NULL_TREE; | |
4144 | ||
4145 | /* Look for the `~'. */ | |
4146 | cp_parser_require (parser, CPP_COMPL, "`~'"); | |
4147 | /* Look for the type-name again. We are not responsible for | |
4148 | checking that it matches the first type-name. */ | |
4149 | *type = cp_parser_type_name (parser); | |
4150 | } | |
4151 | ||
4152 | /* Parse a unary-expression. | |
4153 | ||
4154 | unary-expression: | |
4155 | postfix-expression | |
4156 | ++ cast-expression | |
4157 | -- cast-expression | |
4158 | unary-operator cast-expression | |
4159 | sizeof unary-expression | |
4160 | sizeof ( type-id ) | |
4161 | new-expression | |
4162 | delete-expression | |
4163 | ||
4164 | GNU Extensions: | |
4165 | ||
4166 | unary-expression: | |
4167 | __extension__ cast-expression | |
4168 | __alignof__ unary-expression | |
4169 | __alignof__ ( type-id ) | |
4170 | __real__ cast-expression | |
4171 | __imag__ cast-expression | |
4172 | && identifier | |
4173 | ||
4174 | ADDRESS_P is true iff the unary-expression is appearing as the | |
4175 | operand of the `&' operator. | |
4176 | ||
34cd5ae7 | 4177 | Returns a representation of the expression. */ |
a723baf1 MM |
4178 | |
4179 | static tree | |
4180 | cp_parser_unary_expression (cp_parser *parser, bool address_p) | |
4181 | { | |
4182 | cp_token *token; | |
4183 | enum tree_code unary_operator; | |
4184 | ||
4185 | /* Peek at the next token. */ | |
4186 | token = cp_lexer_peek_token (parser->lexer); | |
4187 | /* Some keywords give away the kind of expression. */ | |
4188 | if (token->type == CPP_KEYWORD) | |
4189 | { | |
4190 | enum rid keyword = token->keyword; | |
4191 | ||
4192 | switch (keyword) | |
4193 | { | |
4194 | case RID_ALIGNOF: | |
a723baf1 MM |
4195 | case RID_SIZEOF: |
4196 | { | |
4197 | tree operand; | |
7a18b933 | 4198 | enum tree_code op; |
a723baf1 | 4199 | |
7a18b933 NS |
4200 | op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR; |
4201 | /* Consume the token. */ | |
a723baf1 MM |
4202 | cp_lexer_consume_token (parser->lexer); |
4203 | /* Parse the operand. */ | |
4204 | operand = cp_parser_sizeof_operand (parser, keyword); | |
4205 | ||
7a18b933 NS |
4206 | if (TYPE_P (operand)) |
4207 | return cxx_sizeof_or_alignof_type (operand, op, true); | |
a723baf1 | 4208 | else |
7a18b933 | 4209 | return cxx_sizeof_or_alignof_expr (operand, op); |
a723baf1 MM |
4210 | } |
4211 | ||
4212 | case RID_NEW: | |
4213 | return cp_parser_new_expression (parser); | |
4214 | ||
4215 | case RID_DELETE: | |
4216 | return cp_parser_delete_expression (parser); | |
4217 | ||
4218 | case RID_EXTENSION: | |
4219 | { | |
4220 | /* The saved value of the PEDANTIC flag. */ | |
4221 | int saved_pedantic; | |
4222 | tree expr; | |
4223 | ||
4224 | /* Save away the PEDANTIC flag. */ | |
4225 | cp_parser_extension_opt (parser, &saved_pedantic); | |
4226 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4227 | expr = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4228 | /* Restore the PEDANTIC flag. */ |
4229 | pedantic = saved_pedantic; | |
4230 | ||
4231 | return expr; | |
4232 | } | |
4233 | ||
4234 | case RID_REALPART: | |
4235 | case RID_IMAGPART: | |
4236 | { | |
4237 | tree expression; | |
4238 | ||
4239 | /* Consume the `__real__' or `__imag__' token. */ | |
4240 | cp_lexer_consume_token (parser->lexer); | |
4241 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4242 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4243 | /* Create the complete representation. */ |
4244 | return build_x_unary_op ((keyword == RID_REALPART | |
4245 | ? REALPART_EXPR : IMAGPART_EXPR), | |
4246 | expression); | |
4247 | } | |
4248 | break; | |
4249 | ||
4250 | default: | |
4251 | break; | |
4252 | } | |
4253 | } | |
4254 | ||
4255 | /* Look for the `:: new' and `:: delete', which also signal the | |
4256 | beginning of a new-expression, or delete-expression, | |
4257 | respectively. If the next token is `::', then it might be one of | |
4258 | these. */ | |
4259 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
4260 | { | |
4261 | enum rid keyword; | |
4262 | ||
4263 | /* See if the token after the `::' is one of the keywords in | |
4264 | which we're interested. */ | |
4265 | keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword; | |
4266 | /* If it's `new', we have a new-expression. */ | |
4267 | if (keyword == RID_NEW) | |
4268 | return cp_parser_new_expression (parser); | |
4269 | /* Similarly, for `delete'. */ | |
4270 | else if (keyword == RID_DELETE) | |
4271 | return cp_parser_delete_expression (parser); | |
4272 | } | |
4273 | ||
4274 | /* Look for a unary operator. */ | |
4275 | unary_operator = cp_parser_unary_operator (token); | |
4276 | /* The `++' and `--' operators can be handled similarly, even though | |
4277 | they are not technically unary-operators in the grammar. */ | |
4278 | if (unary_operator == ERROR_MARK) | |
4279 | { | |
4280 | if (token->type == CPP_PLUS_PLUS) | |
4281 | unary_operator = PREINCREMENT_EXPR; | |
4282 | else if (token->type == CPP_MINUS_MINUS) | |
4283 | unary_operator = PREDECREMENT_EXPR; | |
4284 | /* Handle the GNU address-of-label extension. */ | |
4285 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
4286 | && token->type == CPP_AND_AND) | |
4287 | { | |
4288 | tree identifier; | |
4289 | ||
4290 | /* Consume the '&&' token. */ | |
4291 | cp_lexer_consume_token (parser->lexer); | |
4292 | /* Look for the identifier. */ | |
4293 | identifier = cp_parser_identifier (parser); | |
4294 | /* Create an expression representing the address. */ | |
4295 | return finish_label_address_expr (identifier); | |
4296 | } | |
4297 | } | |
4298 | if (unary_operator != ERROR_MARK) | |
4299 | { | |
4300 | tree cast_expression; | |
a5ac3982 MM |
4301 | tree expression = error_mark_node; |
4302 | const char *non_constant_p = NULL; | |
a723baf1 MM |
4303 | |
4304 | /* Consume the operator token. */ | |
4305 | token = cp_lexer_consume_token (parser->lexer); | |
4306 | /* Parse the cast-expression. */ | |
4307 | cast_expression | |
4308 | = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR); | |
4309 | /* Now, build an appropriate representation. */ | |
4310 | switch (unary_operator) | |
4311 | { | |
4312 | case INDIRECT_REF: | |
a5ac3982 MM |
4313 | non_constant_p = "`*'"; |
4314 | expression = build_x_indirect_ref (cast_expression, "unary *"); | |
4315 | break; | |
4316 | ||
a723baf1 | 4317 | case ADDR_EXPR: |
263ee052 MM |
4318 | /* The "&" operator is allowed in the implementation of |
4319 | "offsetof". */ | |
4320 | if (!parser->in_offsetof_p) | |
4321 | non_constant_p = "`&'"; | |
a5ac3982 | 4322 | /* Fall through. */ |
d17811fd | 4323 | case BIT_NOT_EXPR: |
a5ac3982 MM |
4324 | expression = build_x_unary_op (unary_operator, cast_expression); |
4325 | break; | |
4326 | ||
14d22dd6 MM |
4327 | case PREINCREMENT_EXPR: |
4328 | case PREDECREMENT_EXPR: | |
a5ac3982 MM |
4329 | non_constant_p = (unary_operator == PREINCREMENT_EXPR |
4330 | ? "`++'" : "`--'"); | |
14d22dd6 | 4331 | /* Fall through. */ |
a723baf1 MM |
4332 | case CONVERT_EXPR: |
4333 | case NEGATE_EXPR: | |
4334 | case TRUTH_NOT_EXPR: | |
a5ac3982 MM |
4335 | expression = finish_unary_op_expr (unary_operator, cast_expression); |
4336 | break; | |
a723baf1 | 4337 | |
a723baf1 MM |
4338 | default: |
4339 | abort (); | |
a723baf1 | 4340 | } |
a5ac3982 | 4341 | |
67c03833 | 4342 | if (non_constant_p && parser->integral_constant_expression_p) |
a5ac3982 | 4343 | { |
67c03833 JM |
4344 | if (!parser->allow_non_integral_constant_expression_p) |
4345 | return cp_parser_non_integral_constant_expression (non_constant_p); | |
4346 | parser->non_integral_constant_expression_p = true; | |
a5ac3982 MM |
4347 | } |
4348 | ||
4349 | return expression; | |
a723baf1 MM |
4350 | } |
4351 | ||
4352 | return cp_parser_postfix_expression (parser, address_p); | |
4353 | } | |
4354 | ||
4355 | /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a | |
4356 | unary-operator, the corresponding tree code is returned. */ | |
4357 | ||
4358 | static enum tree_code | |
94edc4ab | 4359 | cp_parser_unary_operator (cp_token* token) |
a723baf1 MM |
4360 | { |
4361 | switch (token->type) | |
4362 | { | |
4363 | case CPP_MULT: | |
4364 | return INDIRECT_REF; | |
4365 | ||
4366 | case CPP_AND: | |
4367 | return ADDR_EXPR; | |
4368 | ||
4369 | case CPP_PLUS: | |
4370 | return CONVERT_EXPR; | |
4371 | ||
4372 | case CPP_MINUS: | |
4373 | return NEGATE_EXPR; | |
4374 | ||
4375 | case CPP_NOT: | |
4376 | return TRUTH_NOT_EXPR; | |
4377 | ||
4378 | case CPP_COMPL: | |
4379 | return BIT_NOT_EXPR; | |
4380 | ||
4381 | default: | |
4382 | return ERROR_MARK; | |
4383 | } | |
4384 | } | |
4385 | ||
4386 | /* Parse a new-expression. | |
4387 | ||
ca099ac8 | 4388 | new-expression: |
a723baf1 MM |
4389 | :: [opt] new new-placement [opt] new-type-id new-initializer [opt] |
4390 | :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt] | |
4391 | ||
4392 | Returns a representation of the expression. */ | |
4393 | ||
4394 | static tree | |
94edc4ab | 4395 | cp_parser_new_expression (cp_parser* parser) |
a723baf1 MM |
4396 | { |
4397 | bool global_scope_p; | |
4398 | tree placement; | |
4399 | tree type; | |
4400 | tree initializer; | |
4401 | ||
4402 | /* Look for the optional `::' operator. */ | |
4403 | global_scope_p | |
4404 | = (cp_parser_global_scope_opt (parser, | |
4405 | /*current_scope_valid_p=*/false) | |
4406 | != NULL_TREE); | |
4407 | /* Look for the `new' operator. */ | |
4408 | cp_parser_require_keyword (parser, RID_NEW, "`new'"); | |
4409 | /* There's no easy way to tell a new-placement from the | |
4410 | `( type-id )' construct. */ | |
4411 | cp_parser_parse_tentatively (parser); | |
4412 | /* Look for a new-placement. */ | |
4413 | placement = cp_parser_new_placement (parser); | |
4414 | /* If that didn't work out, there's no new-placement. */ | |
4415 | if (!cp_parser_parse_definitely (parser)) | |
4416 | placement = NULL_TREE; | |
4417 | ||
4418 | /* If the next token is a `(', then we have a parenthesized | |
4419 | type-id. */ | |
4420 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4421 | { | |
4422 | /* Consume the `('. */ | |
4423 | cp_lexer_consume_token (parser->lexer); | |
4424 | /* Parse the type-id. */ | |
4425 | type = cp_parser_type_id (parser); | |
4426 | /* Look for the closing `)'. */ | |
4427 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
4428 | } | |
4429 | /* Otherwise, there must be a new-type-id. */ | |
4430 | else | |
4431 | type = cp_parser_new_type_id (parser); | |
4432 | ||
4433 | /* If the next token is a `(', then we have a new-initializer. */ | |
4434 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4435 | initializer = cp_parser_new_initializer (parser); | |
4436 | else | |
4437 | initializer = NULL_TREE; | |
4438 | ||
4439 | /* Create a representation of the new-expression. */ | |
4440 | return build_new (placement, type, initializer, global_scope_p); | |
4441 | } | |
4442 | ||
4443 | /* Parse a new-placement. | |
4444 | ||
4445 | new-placement: | |
4446 | ( expression-list ) | |
4447 | ||
4448 | Returns the same representation as for an expression-list. */ | |
4449 | ||
4450 | static tree | |
94edc4ab | 4451 | cp_parser_new_placement (cp_parser* parser) |
a723baf1 MM |
4452 | { |
4453 | tree expression_list; | |
4454 | ||
a723baf1 | 4455 | /* Parse the expression-list. */ |
39703eb9 MM |
4456 | expression_list = (cp_parser_parenthesized_expression_list |
4457 | (parser, false, /*non_constant_p=*/NULL)); | |
a723baf1 MM |
4458 | |
4459 | return expression_list; | |
4460 | } | |
4461 | ||
4462 | /* Parse a new-type-id. | |
4463 | ||
4464 | new-type-id: | |
4465 | type-specifier-seq new-declarator [opt] | |
4466 | ||
4467 | Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq, | |
4468 | and whose TREE_VALUE is the new-declarator. */ | |
4469 | ||
4470 | static tree | |
94edc4ab | 4471 | cp_parser_new_type_id (cp_parser* parser) |
a723baf1 MM |
4472 | { |
4473 | tree type_specifier_seq; | |
4474 | tree declarator; | |
4475 | const char *saved_message; | |
4476 | ||
4477 | /* The type-specifier sequence must not contain type definitions. | |
4478 | (It cannot contain declarations of new types either, but if they | |
4479 | are not definitions we will catch that because they are not | |
4480 | complete.) */ | |
4481 | saved_message = parser->type_definition_forbidden_message; | |
4482 | parser->type_definition_forbidden_message | |
4483 | = "types may not be defined in a new-type-id"; | |
4484 | /* Parse the type-specifier-seq. */ | |
4485 | type_specifier_seq = cp_parser_type_specifier_seq (parser); | |
4486 | /* Restore the old message. */ | |
4487 | parser->type_definition_forbidden_message = saved_message; | |
4488 | /* Parse the new-declarator. */ | |
4489 | declarator = cp_parser_new_declarator_opt (parser); | |
4490 | ||
4491 | return build_tree_list (type_specifier_seq, declarator); | |
4492 | } | |
4493 | ||
4494 | /* Parse an (optional) new-declarator. | |
4495 | ||
4496 | new-declarator: | |
4497 | ptr-operator new-declarator [opt] | |
4498 | direct-new-declarator | |
4499 | ||
4500 | Returns a representation of the declarator. See | |
4501 | cp_parser_declarator for the representations used. */ | |
4502 | ||
4503 | static tree | |
94edc4ab | 4504 | cp_parser_new_declarator_opt (cp_parser* parser) |
a723baf1 MM |
4505 | { |
4506 | enum tree_code code; | |
4507 | tree type; | |
4508 | tree cv_qualifier_seq; | |
4509 | ||
4510 | /* We don't know if there's a ptr-operator next, or not. */ | |
4511 | cp_parser_parse_tentatively (parser); | |
4512 | /* Look for a ptr-operator. */ | |
4513 | code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq); | |
4514 | /* If that worked, look for more new-declarators. */ | |
4515 | if (cp_parser_parse_definitely (parser)) | |
4516 | { | |
4517 | tree declarator; | |
4518 | ||
4519 | /* Parse another optional declarator. */ | |
4520 | declarator = cp_parser_new_declarator_opt (parser); | |
4521 | ||
4522 | /* Create the representation of the declarator. */ | |
4523 | if (code == INDIRECT_REF) | |
4524 | declarator = make_pointer_declarator (cv_qualifier_seq, | |
4525 | declarator); | |
4526 | else | |
4527 | declarator = make_reference_declarator (cv_qualifier_seq, | |
4528 | declarator); | |
4529 | ||
4530 | /* Handle the pointer-to-member case. */ | |
4531 | if (type) | |
4532 | declarator = build_nt (SCOPE_REF, type, declarator); | |
4533 | ||
4534 | return declarator; | |
4535 | } | |
4536 | ||
4537 | /* If the next token is a `[', there is a direct-new-declarator. */ | |
4538 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
4539 | return cp_parser_direct_new_declarator (parser); | |
4540 | ||
4541 | return NULL_TREE; | |
4542 | } | |
4543 | ||
4544 | /* Parse a direct-new-declarator. | |
4545 | ||
4546 | direct-new-declarator: | |
4547 | [ expression ] | |
4548 | direct-new-declarator [constant-expression] | |
4549 | ||
4550 | Returns an ARRAY_REF, following the same conventions as are | |
4551 | documented for cp_parser_direct_declarator. */ | |
4552 | ||
4553 | static tree | |
94edc4ab | 4554 | cp_parser_direct_new_declarator (cp_parser* parser) |
a723baf1 MM |
4555 | { |
4556 | tree declarator = NULL_TREE; | |
4557 | ||
4558 | while (true) | |
4559 | { | |
4560 | tree expression; | |
4561 | ||
4562 | /* Look for the opening `['. */ | |
4563 | cp_parser_require (parser, CPP_OPEN_SQUARE, "`['"); | |
4564 | /* The first expression is not required to be constant. */ | |
4565 | if (!declarator) | |
4566 | { | |
4567 | expression = cp_parser_expression (parser); | |
4568 | /* The standard requires that the expression have integral | |
4569 | type. DR 74 adds enumeration types. We believe that the | |
4570 | real intent is that these expressions be handled like the | |
4571 | expression in a `switch' condition, which also allows | |
4572 | classes with a single conversion to integral or | |
4573 | enumeration type. */ | |
4574 | if (!processing_template_decl) | |
4575 | { | |
4576 | expression | |
4577 | = build_expr_type_conversion (WANT_INT | WANT_ENUM, | |
4578 | expression, | |
b746c5dc | 4579 | /*complain=*/true); |
a723baf1 MM |
4580 | if (!expression) |
4581 | { | |
4582 | error ("expression in new-declarator must have integral or enumeration type"); | |
4583 | expression = error_mark_node; | |
4584 | } | |
4585 | } | |
4586 | } | |
4587 | /* But all the other expressions must be. */ | |
4588 | else | |
14d22dd6 MM |
4589 | expression |
4590 | = cp_parser_constant_expression (parser, | |
4591 | /*allow_non_constant=*/false, | |
4592 | NULL); | |
a723baf1 MM |
4593 | /* Look for the closing `]'. */ |
4594 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
4595 | ||
4596 | /* Add this bound to the declarator. */ | |
4597 | declarator = build_nt (ARRAY_REF, declarator, expression); | |
4598 | ||
4599 | /* If the next token is not a `[', then there are no more | |
4600 | bounds. */ | |
4601 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) | |
4602 | break; | |
4603 | } | |
4604 | ||
4605 | return declarator; | |
4606 | } | |
4607 | ||
4608 | /* Parse a new-initializer. | |
4609 | ||
4610 | new-initializer: | |
4611 | ( expression-list [opt] ) | |
4612 | ||
34cd5ae7 | 4613 | Returns a representation of the expression-list. If there is no |
a723baf1 MM |
4614 | expression-list, VOID_ZERO_NODE is returned. */ |
4615 | ||
4616 | static tree | |
94edc4ab | 4617 | cp_parser_new_initializer (cp_parser* parser) |
a723baf1 MM |
4618 | { |
4619 | tree expression_list; | |
4620 | ||
39703eb9 MM |
4621 | expression_list = (cp_parser_parenthesized_expression_list |
4622 | (parser, false, /*non_constant_p=*/NULL)); | |
7efa3e22 | 4623 | if (!expression_list) |
a723baf1 | 4624 | expression_list = void_zero_node; |
a723baf1 MM |
4625 | |
4626 | return expression_list; | |
4627 | } | |
4628 | ||
4629 | /* Parse a delete-expression. | |
4630 | ||
4631 | delete-expression: | |
4632 | :: [opt] delete cast-expression | |
4633 | :: [opt] delete [ ] cast-expression | |
4634 | ||
4635 | Returns a representation of the expression. */ | |
4636 | ||
4637 | static tree | |
94edc4ab | 4638 | cp_parser_delete_expression (cp_parser* parser) |
a723baf1 MM |
4639 | { |
4640 | bool global_scope_p; | |
4641 | bool array_p; | |
4642 | tree expression; | |
4643 | ||
4644 | /* Look for the optional `::' operator. */ | |
4645 | global_scope_p | |
4646 | = (cp_parser_global_scope_opt (parser, | |
4647 | /*current_scope_valid_p=*/false) | |
4648 | != NULL_TREE); | |
4649 | /* Look for the `delete' keyword. */ | |
4650 | cp_parser_require_keyword (parser, RID_DELETE, "`delete'"); | |
4651 | /* See if the array syntax is in use. */ | |
4652 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
4653 | { | |
4654 | /* Consume the `[' token. */ | |
4655 | cp_lexer_consume_token (parser->lexer); | |
4656 | /* Look for the `]' token. */ | |
4657 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
4658 | /* Remember that this is the `[]' construct. */ | |
4659 | array_p = true; | |
4660 | } | |
4661 | else | |
4662 | array_p = false; | |
4663 | ||
4664 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4665 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4666 | |
4667 | return delete_sanity (expression, NULL_TREE, array_p, global_scope_p); | |
4668 | } | |
4669 | ||
4670 | /* Parse a cast-expression. | |
4671 | ||
4672 | cast-expression: | |
4673 | unary-expression | |
4674 | ( type-id ) cast-expression | |
4675 | ||
4676 | Returns a representation of the expression. */ | |
4677 | ||
4678 | static tree | |
4679 | cp_parser_cast_expression (cp_parser *parser, bool address_p) | |
4680 | { | |
4681 | /* If it's a `(', then we might be looking at a cast. */ | |
4682 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4683 | { | |
4684 | tree type = NULL_TREE; | |
4685 | tree expr = NULL_TREE; | |
4686 | bool compound_literal_p; | |
4687 | const char *saved_message; | |
4688 | ||
4689 | /* There's no way to know yet whether or not this is a cast. | |
4690 | For example, `(int (3))' is a unary-expression, while `(int) | |
4691 | 3' is a cast. So, we resort to parsing tentatively. */ | |
4692 | cp_parser_parse_tentatively (parser); | |
4693 | /* Types may not be defined in a cast. */ | |
4694 | saved_message = parser->type_definition_forbidden_message; | |
4695 | parser->type_definition_forbidden_message | |
4696 | = "types may not be defined in casts"; | |
4697 | /* Consume the `('. */ | |
4698 | cp_lexer_consume_token (parser->lexer); | |
4699 | /* A very tricky bit is that `(struct S) { 3 }' is a | |
4700 | compound-literal (which we permit in C++ as an extension). | |
4701 | But, that construct is not a cast-expression -- it is a | |
4702 | postfix-expression. (The reason is that `(struct S) { 3 }.i' | |
4703 | is legal; if the compound-literal were a cast-expression, | |
4704 | you'd need an extra set of parentheses.) But, if we parse | |
4705 | the type-id, and it happens to be a class-specifier, then we | |
4706 | will commit to the parse at that point, because we cannot | |
4707 | undo the action that is done when creating a new class. So, | |
4708 | then we cannot back up and do a postfix-expression. | |
4709 | ||
4710 | Therefore, we scan ahead to the closing `)', and check to see | |
4711 | if the token after the `)' is a `{'. If so, we are not | |
4712 | looking at a cast-expression. | |
4713 | ||
4714 | Save tokens so that we can put them back. */ | |
4715 | cp_lexer_save_tokens (parser->lexer); | |
4716 | /* Skip tokens until the next token is a closing parenthesis. | |
4717 | If we find the closing `)', and the next token is a `{', then | |
4718 | we are looking at a compound-literal. */ | |
4719 | compound_literal_p | |
a668c6ad MM |
4720 | = (cp_parser_skip_to_closing_parenthesis (parser, false, false, |
4721 | /*consume_paren=*/true) | |
a723baf1 MM |
4722 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)); |
4723 | /* Roll back the tokens we skipped. */ | |
4724 | cp_lexer_rollback_tokens (parser->lexer); | |
4725 | /* If we were looking at a compound-literal, simulate an error | |
4726 | so that the call to cp_parser_parse_definitely below will | |
4727 | fail. */ | |
4728 | if (compound_literal_p) | |
4729 | cp_parser_simulate_error (parser); | |
4730 | else | |
4731 | { | |
4f8163b1 MM |
4732 | bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
4733 | parser->in_type_id_in_expr_p = true; | |
a723baf1 MM |
4734 | /* Look for the type-id. */ |
4735 | type = cp_parser_type_id (parser); | |
4736 | /* Look for the closing `)'. */ | |
4737 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
4f8163b1 | 4738 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 MM |
4739 | } |
4740 | ||
4741 | /* Restore the saved message. */ | |
4742 | parser->type_definition_forbidden_message = saved_message; | |
4743 | ||
bbaab916 NS |
4744 | /* If ok so far, parse the dependent expression. We cannot be |
4745 | sure it is a cast. Consider `(T ())'. It is a parenthesized | |
4746 | ctor of T, but looks like a cast to function returning T | |
4747 | without a dependent expression. */ | |
4748 | if (!cp_parser_error_occurred (parser)) | |
d6b4ea85 | 4749 | expr = cp_parser_simple_cast_expression (parser); |
bbaab916 | 4750 | |
a723baf1 MM |
4751 | if (cp_parser_parse_definitely (parser)) |
4752 | { | |
a723baf1 MM |
4753 | /* Warn about old-style casts, if so requested. */ |
4754 | if (warn_old_style_cast | |
4755 | && !in_system_header | |
4756 | && !VOID_TYPE_P (type) | |
4757 | && current_lang_name != lang_name_c) | |
4758 | warning ("use of old-style cast"); | |
14d22dd6 MM |
4759 | |
4760 | /* Only type conversions to integral or enumeration types | |
4761 | can be used in constant-expressions. */ | |
67c03833 | 4762 | if (parser->integral_constant_expression_p |
14d22dd6 MM |
4763 | && !dependent_type_p (type) |
4764 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)) | |
4765 | { | |
67c03833 JM |
4766 | if (!parser->allow_non_integral_constant_expression_p) |
4767 | return (cp_parser_non_integral_constant_expression | |
14d22dd6 MM |
4768 | ("a casts to a type other than an integral or " |
4769 | "enumeration type")); | |
67c03833 | 4770 | parser->non_integral_constant_expression_p = true; |
14d22dd6 | 4771 | } |
a723baf1 MM |
4772 | /* Perform the cast. */ |
4773 | expr = build_c_cast (type, expr); | |
bbaab916 | 4774 | return expr; |
a723baf1 | 4775 | } |
a723baf1 MM |
4776 | } |
4777 | ||
4778 | /* If we get here, then it's not a cast, so it must be a | |
4779 | unary-expression. */ | |
4780 | return cp_parser_unary_expression (parser, address_p); | |
4781 | } | |
4782 | ||
4783 | /* Parse a pm-expression. | |
4784 | ||
4785 | pm-expression: | |
4786 | cast-expression | |
4787 | pm-expression .* cast-expression | |
4788 | pm-expression ->* cast-expression | |
4789 | ||
4790 | Returns a representation of the expression. */ | |
4791 | ||
4792 | static tree | |
94edc4ab | 4793 | cp_parser_pm_expression (cp_parser* parser) |
a723baf1 | 4794 | { |
d6b4ea85 MM |
4795 | static const cp_parser_token_tree_map map = { |
4796 | { CPP_DEREF_STAR, MEMBER_REF }, | |
4797 | { CPP_DOT_STAR, DOTSTAR_EXPR }, | |
4798 | { CPP_EOF, ERROR_MARK } | |
4799 | }; | |
a723baf1 | 4800 | |
d6b4ea85 MM |
4801 | return cp_parser_binary_expression (parser, map, |
4802 | cp_parser_simple_cast_expression); | |
a723baf1 MM |
4803 | } |
4804 | ||
4805 | /* Parse a multiplicative-expression. | |
4806 | ||
4807 | mulitplicative-expression: | |
4808 | pm-expression | |
4809 | multiplicative-expression * pm-expression | |
4810 | multiplicative-expression / pm-expression | |
4811 | multiplicative-expression % pm-expression | |
4812 | ||
4813 | Returns a representation of the expression. */ | |
4814 | ||
4815 | static tree | |
94edc4ab | 4816 | cp_parser_multiplicative_expression (cp_parser* parser) |
a723baf1 | 4817 | { |
39b1af70 | 4818 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4819 | { CPP_MULT, MULT_EXPR }, |
4820 | { CPP_DIV, TRUNC_DIV_EXPR }, | |
4821 | { CPP_MOD, TRUNC_MOD_EXPR }, | |
4822 | { CPP_EOF, ERROR_MARK } | |
4823 | }; | |
4824 | ||
4825 | return cp_parser_binary_expression (parser, | |
4826 | map, | |
4827 | cp_parser_pm_expression); | |
4828 | } | |
4829 | ||
4830 | /* Parse an additive-expression. | |
4831 | ||
4832 | additive-expression: | |
4833 | multiplicative-expression | |
4834 | additive-expression + multiplicative-expression | |
4835 | additive-expression - multiplicative-expression | |
4836 | ||
4837 | Returns a representation of the expression. */ | |
4838 | ||
4839 | static tree | |
94edc4ab | 4840 | cp_parser_additive_expression (cp_parser* parser) |
a723baf1 | 4841 | { |
39b1af70 | 4842 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4843 | { CPP_PLUS, PLUS_EXPR }, |
4844 | { CPP_MINUS, MINUS_EXPR }, | |
4845 | { CPP_EOF, ERROR_MARK } | |
4846 | }; | |
4847 | ||
4848 | return cp_parser_binary_expression (parser, | |
4849 | map, | |
4850 | cp_parser_multiplicative_expression); | |
4851 | } | |
4852 | ||
4853 | /* Parse a shift-expression. | |
4854 | ||
4855 | shift-expression: | |
4856 | additive-expression | |
4857 | shift-expression << additive-expression | |
4858 | shift-expression >> additive-expression | |
4859 | ||
4860 | Returns a representation of the expression. */ | |
4861 | ||
4862 | static tree | |
94edc4ab | 4863 | cp_parser_shift_expression (cp_parser* parser) |
a723baf1 | 4864 | { |
39b1af70 | 4865 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4866 | { CPP_LSHIFT, LSHIFT_EXPR }, |
4867 | { CPP_RSHIFT, RSHIFT_EXPR }, | |
4868 | { CPP_EOF, ERROR_MARK } | |
4869 | }; | |
4870 | ||
4871 | return cp_parser_binary_expression (parser, | |
4872 | map, | |
4873 | cp_parser_additive_expression); | |
4874 | } | |
4875 | ||
4876 | /* Parse a relational-expression. | |
4877 | ||
4878 | relational-expression: | |
4879 | shift-expression | |
4880 | relational-expression < shift-expression | |
4881 | relational-expression > shift-expression | |
4882 | relational-expression <= shift-expression | |
4883 | relational-expression >= shift-expression | |
4884 | ||
4885 | GNU Extension: | |
4886 | ||
4887 | relational-expression: | |
4888 | relational-expression <? shift-expression | |
4889 | relational-expression >? shift-expression | |
4890 | ||
4891 | Returns a representation of the expression. */ | |
4892 | ||
4893 | static tree | |
94edc4ab | 4894 | cp_parser_relational_expression (cp_parser* parser) |
a723baf1 | 4895 | { |
39b1af70 | 4896 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4897 | { CPP_LESS, LT_EXPR }, |
4898 | { CPP_GREATER, GT_EXPR }, | |
4899 | { CPP_LESS_EQ, LE_EXPR }, | |
4900 | { CPP_GREATER_EQ, GE_EXPR }, | |
4901 | { CPP_MIN, MIN_EXPR }, | |
4902 | { CPP_MAX, MAX_EXPR }, | |
4903 | { CPP_EOF, ERROR_MARK } | |
4904 | }; | |
4905 | ||
4906 | return cp_parser_binary_expression (parser, | |
4907 | map, | |
4908 | cp_parser_shift_expression); | |
4909 | } | |
4910 | ||
4911 | /* Parse an equality-expression. | |
4912 | ||
4913 | equality-expression: | |
4914 | relational-expression | |
4915 | equality-expression == relational-expression | |
4916 | equality-expression != relational-expression | |
4917 | ||
4918 | Returns a representation of the expression. */ | |
4919 | ||
4920 | static tree | |
94edc4ab | 4921 | cp_parser_equality_expression (cp_parser* parser) |
a723baf1 | 4922 | { |
39b1af70 | 4923 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4924 | { CPP_EQ_EQ, EQ_EXPR }, |
4925 | { CPP_NOT_EQ, NE_EXPR }, | |
4926 | { CPP_EOF, ERROR_MARK } | |
4927 | }; | |
4928 | ||
4929 | return cp_parser_binary_expression (parser, | |
4930 | map, | |
4931 | cp_parser_relational_expression); | |
4932 | } | |
4933 | ||
4934 | /* Parse an and-expression. | |
4935 | ||
4936 | and-expression: | |
4937 | equality-expression | |
4938 | and-expression & equality-expression | |
4939 | ||
4940 | Returns a representation of the expression. */ | |
4941 | ||
4942 | static tree | |
94edc4ab | 4943 | cp_parser_and_expression (cp_parser* parser) |
a723baf1 | 4944 | { |
39b1af70 | 4945 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4946 | { CPP_AND, BIT_AND_EXPR }, |
4947 | { CPP_EOF, ERROR_MARK } | |
4948 | }; | |
4949 | ||
4950 | return cp_parser_binary_expression (parser, | |
4951 | map, | |
4952 | cp_parser_equality_expression); | |
4953 | } | |
4954 | ||
4955 | /* Parse an exclusive-or-expression. | |
4956 | ||
4957 | exclusive-or-expression: | |
4958 | and-expression | |
4959 | exclusive-or-expression ^ and-expression | |
4960 | ||
4961 | Returns a representation of the expression. */ | |
4962 | ||
4963 | static tree | |
94edc4ab | 4964 | cp_parser_exclusive_or_expression (cp_parser* parser) |
a723baf1 | 4965 | { |
39b1af70 | 4966 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4967 | { CPP_XOR, BIT_XOR_EXPR }, |
4968 | { CPP_EOF, ERROR_MARK } | |
4969 | }; | |
4970 | ||
4971 | return cp_parser_binary_expression (parser, | |
4972 | map, | |
4973 | cp_parser_and_expression); | |
4974 | } | |
4975 | ||
4976 | ||
4977 | /* Parse an inclusive-or-expression. | |
4978 | ||
4979 | inclusive-or-expression: | |
4980 | exclusive-or-expression | |
4981 | inclusive-or-expression | exclusive-or-expression | |
4982 | ||
4983 | Returns a representation of the expression. */ | |
4984 | ||
4985 | static tree | |
94edc4ab | 4986 | cp_parser_inclusive_or_expression (cp_parser* parser) |
a723baf1 | 4987 | { |
39b1af70 | 4988 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4989 | { CPP_OR, BIT_IOR_EXPR }, |
4990 | { CPP_EOF, ERROR_MARK } | |
4991 | }; | |
4992 | ||
4993 | return cp_parser_binary_expression (parser, | |
4994 | map, | |
4995 | cp_parser_exclusive_or_expression); | |
4996 | } | |
4997 | ||
4998 | /* Parse a logical-and-expression. | |
4999 | ||
5000 | logical-and-expression: | |
5001 | inclusive-or-expression | |
5002 | logical-and-expression && inclusive-or-expression | |
5003 | ||
5004 | Returns a representation of the expression. */ | |
5005 | ||
5006 | static tree | |
94edc4ab | 5007 | cp_parser_logical_and_expression (cp_parser* parser) |
a723baf1 | 5008 | { |
39b1af70 | 5009 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
5010 | { CPP_AND_AND, TRUTH_ANDIF_EXPR }, |
5011 | { CPP_EOF, ERROR_MARK } | |
5012 | }; | |
5013 | ||
5014 | return cp_parser_binary_expression (parser, | |
5015 | map, | |
5016 | cp_parser_inclusive_or_expression); | |
5017 | } | |
5018 | ||
5019 | /* Parse a logical-or-expression. | |
5020 | ||
5021 | logical-or-expression: | |
34cd5ae7 | 5022 | logical-and-expression |
a723baf1 MM |
5023 | logical-or-expression || logical-and-expression |
5024 | ||
5025 | Returns a representation of the expression. */ | |
5026 | ||
5027 | static tree | |
94edc4ab | 5028 | cp_parser_logical_or_expression (cp_parser* parser) |
a723baf1 | 5029 | { |
39b1af70 | 5030 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
5031 | { CPP_OR_OR, TRUTH_ORIF_EXPR }, |
5032 | { CPP_EOF, ERROR_MARK } | |
5033 | }; | |
5034 | ||
5035 | return cp_parser_binary_expression (parser, | |
5036 | map, | |
5037 | cp_parser_logical_and_expression); | |
5038 | } | |
5039 | ||
a723baf1 MM |
5040 | /* Parse the `? expression : assignment-expression' part of a |
5041 | conditional-expression. The LOGICAL_OR_EXPR is the | |
5042 | logical-or-expression that started the conditional-expression. | |
5043 | Returns a representation of the entire conditional-expression. | |
5044 | ||
39703eb9 | 5045 | This routine is used by cp_parser_assignment_expression. |
a723baf1 MM |
5046 | |
5047 | ? expression : assignment-expression | |
5048 | ||
5049 | GNU Extensions: | |
5050 | ||
5051 | ? : assignment-expression */ | |
5052 | ||
5053 | static tree | |
94edc4ab | 5054 | cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr) |
a723baf1 MM |
5055 | { |
5056 | tree expr; | |
5057 | tree assignment_expr; | |
5058 | ||
5059 | /* Consume the `?' token. */ | |
5060 | cp_lexer_consume_token (parser->lexer); | |
5061 | if (cp_parser_allow_gnu_extensions_p (parser) | |
5062 | && cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
5063 | /* Implicit true clause. */ | |
5064 | expr = NULL_TREE; | |
5065 | else | |
5066 | /* Parse the expression. */ | |
5067 | expr = cp_parser_expression (parser); | |
5068 | ||
5069 | /* The next token should be a `:'. */ | |
5070 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
5071 | /* Parse the assignment-expression. */ | |
5072 | assignment_expr = cp_parser_assignment_expression (parser); | |
5073 | ||
5074 | /* Build the conditional-expression. */ | |
5075 | return build_x_conditional_expr (logical_or_expr, | |
5076 | expr, | |
5077 | assignment_expr); | |
5078 | } | |
5079 | ||
5080 | /* Parse an assignment-expression. | |
5081 | ||
5082 | assignment-expression: | |
5083 | conditional-expression | |
5084 | logical-or-expression assignment-operator assignment_expression | |
5085 | throw-expression | |
5086 | ||
5087 | Returns a representation for the expression. */ | |
5088 | ||
5089 | static tree | |
94edc4ab | 5090 | cp_parser_assignment_expression (cp_parser* parser) |
a723baf1 MM |
5091 | { |
5092 | tree expr; | |
5093 | ||
5094 | /* If the next token is the `throw' keyword, then we're looking at | |
5095 | a throw-expression. */ | |
5096 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW)) | |
5097 | expr = cp_parser_throw_expression (parser); | |
5098 | /* Otherwise, it must be that we are looking at a | |
5099 | logical-or-expression. */ | |
5100 | else | |
5101 | { | |
5102 | /* Parse the logical-or-expression. */ | |
5103 | expr = cp_parser_logical_or_expression (parser); | |
5104 | /* If the next token is a `?' then we're actually looking at a | |
5105 | conditional-expression. */ | |
5106 | if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY)) | |
5107 | return cp_parser_question_colon_clause (parser, expr); | |
5108 | else | |
5109 | { | |
5110 | enum tree_code assignment_operator; | |
5111 | ||
5112 | /* If it's an assignment-operator, we're using the second | |
5113 | production. */ | |
5114 | assignment_operator | |
5115 | = cp_parser_assignment_operator_opt (parser); | |
5116 | if (assignment_operator != ERROR_MARK) | |
5117 | { | |
5118 | tree rhs; | |
5119 | ||
5120 | /* Parse the right-hand side of the assignment. */ | |
5121 | rhs = cp_parser_assignment_expression (parser); | |
14d22dd6 MM |
5122 | /* An assignment may not appear in a |
5123 | constant-expression. */ | |
67c03833 | 5124 | if (parser->integral_constant_expression_p) |
14d22dd6 | 5125 | { |
67c03833 JM |
5126 | if (!parser->allow_non_integral_constant_expression_p) |
5127 | return cp_parser_non_integral_constant_expression ("an assignment"); | |
5128 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 5129 | } |
34cd5ae7 | 5130 | /* Build the assignment expression. */ |
a723baf1 MM |
5131 | expr = build_x_modify_expr (expr, |
5132 | assignment_operator, | |
5133 | rhs); | |
5134 | } | |
5135 | } | |
5136 | } | |
5137 | ||
5138 | return expr; | |
5139 | } | |
5140 | ||
5141 | /* Parse an (optional) assignment-operator. | |
5142 | ||
5143 | assignment-operator: one of | |
5144 | = *= /= %= += -= >>= <<= &= ^= |= | |
5145 | ||
5146 | GNU Extension: | |
5147 | ||
5148 | assignment-operator: one of | |
5149 | <?= >?= | |
5150 | ||
5151 | If the next token is an assignment operator, the corresponding tree | |
5152 | code is returned, and the token is consumed. For example, for | |
5153 | `+=', PLUS_EXPR is returned. For `=' itself, the code returned is | |
5154 | NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%', | |
5155 | TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment | |
5156 | operator, ERROR_MARK is returned. */ | |
5157 | ||
5158 | static enum tree_code | |
94edc4ab | 5159 | cp_parser_assignment_operator_opt (cp_parser* parser) |
a723baf1 MM |
5160 | { |
5161 | enum tree_code op; | |
5162 | cp_token *token; | |
5163 | ||
5164 | /* Peek at the next toen. */ | |
5165 | token = cp_lexer_peek_token (parser->lexer); | |
5166 | ||
5167 | switch (token->type) | |
5168 | { | |
5169 | case CPP_EQ: | |
5170 | op = NOP_EXPR; | |
5171 | break; | |
5172 | ||
5173 | case CPP_MULT_EQ: | |
5174 | op = MULT_EXPR; | |
5175 | break; | |
5176 | ||
5177 | case CPP_DIV_EQ: | |
5178 | op = TRUNC_DIV_EXPR; | |
5179 | break; | |
5180 | ||
5181 | case CPP_MOD_EQ: | |
5182 | op = TRUNC_MOD_EXPR; | |
5183 | break; | |
5184 | ||
5185 | case CPP_PLUS_EQ: | |
5186 | op = PLUS_EXPR; | |
5187 | break; | |
5188 | ||
5189 | case CPP_MINUS_EQ: | |
5190 | op = MINUS_EXPR; | |
5191 | break; | |
5192 | ||
5193 | case CPP_RSHIFT_EQ: | |
5194 | op = RSHIFT_EXPR; | |
5195 | break; | |
5196 | ||
5197 | case CPP_LSHIFT_EQ: | |
5198 | op = LSHIFT_EXPR; | |
5199 | break; | |
5200 | ||
5201 | case CPP_AND_EQ: | |
5202 | op = BIT_AND_EXPR; | |
5203 | break; | |
5204 | ||
5205 | case CPP_XOR_EQ: | |
5206 | op = BIT_XOR_EXPR; | |
5207 | break; | |
5208 | ||
5209 | case CPP_OR_EQ: | |
5210 | op = BIT_IOR_EXPR; | |
5211 | break; | |
5212 | ||
5213 | case CPP_MIN_EQ: | |
5214 | op = MIN_EXPR; | |
5215 | break; | |
5216 | ||
5217 | case CPP_MAX_EQ: | |
5218 | op = MAX_EXPR; | |
5219 | break; | |
5220 | ||
5221 | default: | |
5222 | /* Nothing else is an assignment operator. */ | |
5223 | op = ERROR_MARK; | |
5224 | } | |
5225 | ||
5226 | /* If it was an assignment operator, consume it. */ | |
5227 | if (op != ERROR_MARK) | |
5228 | cp_lexer_consume_token (parser->lexer); | |
5229 | ||
5230 | return op; | |
5231 | } | |
5232 | ||
5233 | /* Parse an expression. | |
5234 | ||
5235 | expression: | |
5236 | assignment-expression | |
5237 | expression , assignment-expression | |
5238 | ||
5239 | Returns a representation of the expression. */ | |
5240 | ||
5241 | static tree | |
94edc4ab | 5242 | cp_parser_expression (cp_parser* parser) |
a723baf1 MM |
5243 | { |
5244 | tree expression = NULL_TREE; | |
a723baf1 MM |
5245 | |
5246 | while (true) | |
5247 | { | |
5248 | tree assignment_expression; | |
5249 | ||
5250 | /* Parse the next assignment-expression. */ | |
5251 | assignment_expression | |
5252 | = cp_parser_assignment_expression (parser); | |
5253 | /* If this is the first assignment-expression, we can just | |
5254 | save it away. */ | |
5255 | if (!expression) | |
5256 | expression = assignment_expression; | |
a723baf1 | 5257 | else |
d17811fd MM |
5258 | expression = build_x_compound_expr (expression, |
5259 | assignment_expression); | |
a723baf1 MM |
5260 | /* If the next token is not a comma, then we are done with the |
5261 | expression. */ | |
5262 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
5263 | break; | |
5264 | /* Consume the `,'. */ | |
5265 | cp_lexer_consume_token (parser->lexer); | |
14d22dd6 | 5266 | /* A comma operator cannot appear in a constant-expression. */ |
67c03833 | 5267 | if (parser->integral_constant_expression_p) |
14d22dd6 | 5268 | { |
67c03833 | 5269 | if (!parser->allow_non_integral_constant_expression_p) |
d17811fd | 5270 | expression |
67c03833 JM |
5271 | = cp_parser_non_integral_constant_expression ("a comma operator"); |
5272 | parser->non_integral_constant_expression_p = true; | |
14d22dd6 | 5273 | } |
14d22dd6 | 5274 | } |
a723baf1 MM |
5275 | |
5276 | return expression; | |
5277 | } | |
5278 | ||
5279 | /* Parse a constant-expression. | |
5280 | ||
5281 | constant-expression: | |
14d22dd6 MM |
5282 | conditional-expression |
5283 | ||
5284 | If ALLOW_NON_CONSTANT_P a non-constant expression is silently | |
d17811fd MM |
5285 | accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not |
5286 | constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P | |
5287 | is false, NON_CONSTANT_P should be NULL. */ | |
a723baf1 MM |
5288 | |
5289 | static tree | |
14d22dd6 MM |
5290 | cp_parser_constant_expression (cp_parser* parser, |
5291 | bool allow_non_constant_p, | |
5292 | bool *non_constant_p) | |
a723baf1 | 5293 | { |
67c03833 JM |
5294 | bool saved_integral_constant_expression_p; |
5295 | bool saved_allow_non_integral_constant_expression_p; | |
5296 | bool saved_non_integral_constant_expression_p; | |
a723baf1 MM |
5297 | tree expression; |
5298 | ||
5299 | /* It might seem that we could simply parse the | |
5300 | conditional-expression, and then check to see if it were | |
5301 | TREE_CONSTANT. However, an expression that is TREE_CONSTANT is | |
5302 | one that the compiler can figure out is constant, possibly after | |
5303 | doing some simplifications or optimizations. The standard has a | |
5304 | precise definition of constant-expression, and we must honor | |
5305 | that, even though it is somewhat more restrictive. | |
5306 | ||
5307 | For example: | |
5308 | ||
5309 | int i[(2, 3)]; | |
5310 | ||
5311 | is not a legal declaration, because `(2, 3)' is not a | |
5312 | constant-expression. The `,' operator is forbidden in a | |
5313 | constant-expression. However, GCC's constant-folding machinery | |
5314 | will fold this operation to an INTEGER_CST for `3'. */ | |
5315 | ||
14d22dd6 | 5316 | /* Save the old settings. */ |
67c03833 JM |
5317 | saved_integral_constant_expression_p = parser->integral_constant_expression_p; |
5318 | saved_allow_non_integral_constant_expression_p | |
5319 | = parser->allow_non_integral_constant_expression_p; | |
5320 | saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p; | |
a723baf1 | 5321 | /* We are now parsing a constant-expression. */ |
67c03833 JM |
5322 | parser->integral_constant_expression_p = true; |
5323 | parser->allow_non_integral_constant_expression_p = allow_non_constant_p; | |
5324 | parser->non_integral_constant_expression_p = false; | |
39703eb9 MM |
5325 | /* Although the grammar says "conditional-expression", we parse an |
5326 | "assignment-expression", which also permits "throw-expression" | |
5327 | and the use of assignment operators. In the case that | |
5328 | ALLOW_NON_CONSTANT_P is false, we get better errors than we would | |
5329 | otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is | |
5330 | actually essential that we look for an assignment-expression. | |
5331 | For example, cp_parser_initializer_clauses uses this function to | |
5332 | determine whether a particular assignment-expression is in fact | |
5333 | constant. */ | |
5334 | expression = cp_parser_assignment_expression (parser); | |
14d22dd6 | 5335 | /* Restore the old settings. */ |
67c03833 JM |
5336 | parser->integral_constant_expression_p = saved_integral_constant_expression_p; |
5337 | parser->allow_non_integral_constant_expression_p | |
5338 | = saved_allow_non_integral_constant_expression_p; | |
14d22dd6 | 5339 | if (allow_non_constant_p) |
67c03833 JM |
5340 | *non_constant_p = parser->non_integral_constant_expression_p; |
5341 | parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p; | |
a723baf1 MM |
5342 | |
5343 | return expression; | |
5344 | } | |
5345 | ||
5346 | /* Statements [gram.stmt.stmt] */ | |
5347 | ||
5348 | /* Parse a statement. | |
5349 | ||
5350 | statement: | |
5351 | labeled-statement | |
5352 | expression-statement | |
5353 | compound-statement | |
5354 | selection-statement | |
5355 | iteration-statement | |
5356 | jump-statement | |
5357 | declaration-statement | |
5358 | try-block */ | |
5359 | ||
5360 | static void | |
a5bcc582 | 5361 | cp_parser_statement (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 MM |
5362 | { |
5363 | tree statement; | |
5364 | cp_token *token; | |
5365 | int statement_line_number; | |
5366 | ||
5367 | /* There is no statement yet. */ | |
5368 | statement = NULL_TREE; | |
5369 | /* Peek at the next token. */ | |
5370 | token = cp_lexer_peek_token (parser->lexer); | |
5371 | /* Remember the line number of the first token in the statement. */ | |
82a98427 | 5372 | statement_line_number = token->location.line; |
a723baf1 MM |
5373 | /* If this is a keyword, then that will often determine what kind of |
5374 | statement we have. */ | |
5375 | if (token->type == CPP_KEYWORD) | |
5376 | { | |
5377 | enum rid keyword = token->keyword; | |
5378 | ||
5379 | switch (keyword) | |
5380 | { | |
5381 | case RID_CASE: | |
5382 | case RID_DEFAULT: | |
a5bcc582 NS |
5383 | statement = cp_parser_labeled_statement (parser, |
5384 | in_statement_expr_p); | |
a723baf1 MM |
5385 | break; |
5386 | ||
5387 | case RID_IF: | |
5388 | case RID_SWITCH: | |
5389 | statement = cp_parser_selection_statement (parser); | |
5390 | break; | |
5391 | ||
5392 | case RID_WHILE: | |
5393 | case RID_DO: | |
5394 | case RID_FOR: | |
5395 | statement = cp_parser_iteration_statement (parser); | |
5396 | break; | |
5397 | ||
5398 | case RID_BREAK: | |
5399 | case RID_CONTINUE: | |
5400 | case RID_RETURN: | |
5401 | case RID_GOTO: | |
5402 | statement = cp_parser_jump_statement (parser); | |
5403 | break; | |
5404 | ||
5405 | case RID_TRY: | |
5406 | statement = cp_parser_try_block (parser); | |
5407 | break; | |
5408 | ||
5409 | default: | |
5410 | /* It might be a keyword like `int' that can start a | |
5411 | declaration-statement. */ | |
5412 | break; | |
5413 | } | |
5414 | } | |
5415 | else if (token->type == CPP_NAME) | |
5416 | { | |
5417 | /* If the next token is a `:', then we are looking at a | |
5418 | labeled-statement. */ | |
5419 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
5420 | if (token->type == CPP_COLON) | |
a5bcc582 | 5421 | statement = cp_parser_labeled_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5422 | } |
5423 | /* Anything that starts with a `{' must be a compound-statement. */ | |
5424 | else if (token->type == CPP_OPEN_BRACE) | |
a5bcc582 | 5425 | statement = cp_parser_compound_statement (parser, false); |
a723baf1 MM |
5426 | |
5427 | /* Everything else must be a declaration-statement or an | |
5428 | expression-statement. Try for the declaration-statement | |
5429 | first, unless we are looking at a `;', in which case we know that | |
5430 | we have an expression-statement. */ | |
5431 | if (!statement) | |
5432 | { | |
5433 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5434 | { | |
5435 | cp_parser_parse_tentatively (parser); | |
5436 | /* Try to parse the declaration-statement. */ | |
5437 | cp_parser_declaration_statement (parser); | |
5438 | /* If that worked, we're done. */ | |
5439 | if (cp_parser_parse_definitely (parser)) | |
5440 | return; | |
5441 | } | |
5442 | /* Look for an expression-statement instead. */ | |
a5bcc582 | 5443 | statement = cp_parser_expression_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5444 | } |
5445 | ||
5446 | /* Set the line number for the statement. */ | |
009ed910 | 5447 | if (statement && STATEMENT_CODE_P (TREE_CODE (statement))) |
a723baf1 MM |
5448 | STMT_LINENO (statement) = statement_line_number; |
5449 | } | |
5450 | ||
5451 | /* Parse a labeled-statement. | |
5452 | ||
5453 | labeled-statement: | |
5454 | identifier : statement | |
5455 | case constant-expression : statement | |
5456 | default : statement | |
5457 | ||
5458 | Returns the new CASE_LABEL, for a `case' or `default' label. For | |
5459 | an ordinary label, returns a LABEL_STMT. */ | |
5460 | ||
5461 | static tree | |
a5bcc582 | 5462 | cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 MM |
5463 | { |
5464 | cp_token *token; | |
0e59b3fb | 5465 | tree statement = error_mark_node; |
a723baf1 MM |
5466 | |
5467 | /* The next token should be an identifier. */ | |
5468 | token = cp_lexer_peek_token (parser->lexer); | |
5469 | if (token->type != CPP_NAME | |
5470 | && token->type != CPP_KEYWORD) | |
5471 | { | |
5472 | cp_parser_error (parser, "expected labeled-statement"); | |
5473 | return error_mark_node; | |
5474 | } | |
5475 | ||
5476 | switch (token->keyword) | |
5477 | { | |
5478 | case RID_CASE: | |
5479 | { | |
5480 | tree expr; | |
5481 | ||
5482 | /* Consume the `case' token. */ | |
5483 | cp_lexer_consume_token (parser->lexer); | |
5484 | /* Parse the constant-expression. */ | |
14d22dd6 | 5485 | expr = cp_parser_constant_expression (parser, |
d17811fd | 5486 | /*allow_non_constant_p=*/false, |
14d22dd6 | 5487 | NULL); |
0e59b3fb MM |
5488 | if (!parser->in_switch_statement_p) |
5489 | error ("case label `%E' not within a switch statement", expr); | |
5490 | else | |
5491 | statement = finish_case_label (expr, NULL_TREE); | |
a723baf1 MM |
5492 | } |
5493 | break; | |
5494 | ||
5495 | case RID_DEFAULT: | |
5496 | /* Consume the `default' token. */ | |
5497 | cp_lexer_consume_token (parser->lexer); | |
0e59b3fb MM |
5498 | if (!parser->in_switch_statement_p) |
5499 | error ("case label not within a switch statement"); | |
5500 | else | |
5501 | statement = finish_case_label (NULL_TREE, NULL_TREE); | |
a723baf1 MM |
5502 | break; |
5503 | ||
5504 | default: | |
5505 | /* Anything else must be an ordinary label. */ | |
5506 | statement = finish_label_stmt (cp_parser_identifier (parser)); | |
5507 | break; | |
5508 | } | |
5509 | ||
5510 | /* Require the `:' token. */ | |
5511 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
5512 | /* Parse the labeled statement. */ | |
a5bcc582 | 5513 | cp_parser_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5514 | |
5515 | /* Return the label, in the case of a `case' or `default' label. */ | |
5516 | return statement; | |
5517 | } | |
5518 | ||
5519 | /* Parse an expression-statement. | |
5520 | ||
5521 | expression-statement: | |
5522 | expression [opt] ; | |
5523 | ||
5524 | Returns the new EXPR_STMT -- or NULL_TREE if the expression | |
a5bcc582 NS |
5525 | statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P |
5526 | indicates whether this expression-statement is part of an | |
5527 | expression statement. */ | |
a723baf1 MM |
5528 | |
5529 | static tree | |
a5bcc582 | 5530 | cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 | 5531 | { |
a5bcc582 | 5532 | tree statement = NULL_TREE; |
a723baf1 | 5533 | |
a5bcc582 | 5534 | /* If the next token is a ';', then there is no expression |
04c06002 | 5535 | statement. */ |
a723baf1 | 5536 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) |
a5bcc582 NS |
5537 | statement = cp_parser_expression (parser); |
5538 | ||
a723baf1 | 5539 | /* Consume the final `;'. */ |
e0860732 | 5540 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 | 5541 | |
a5bcc582 NS |
5542 | if (in_statement_expr_p |
5543 | && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
5544 | { | |
5545 | /* This is the final expression statement of a statement | |
5546 | expression. */ | |
5547 | statement = finish_stmt_expr_expr (statement); | |
5548 | } | |
5549 | else if (statement) | |
5550 | statement = finish_expr_stmt (statement); | |
5551 | else | |
5552 | finish_stmt (); | |
5553 | ||
a723baf1 MM |
5554 | return statement; |
5555 | } | |
5556 | ||
5557 | /* Parse a compound-statement. | |
5558 | ||
5559 | compound-statement: | |
5560 | { statement-seq [opt] } | |
5561 | ||
5562 | Returns a COMPOUND_STMT representing the statement. */ | |
5563 | ||
5564 | static tree | |
a5bcc582 | 5565 | cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p) |
a723baf1 MM |
5566 | { |
5567 | tree compound_stmt; | |
5568 | ||
5569 | /* Consume the `{'. */ | |
5570 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
5571 | return error_mark_node; | |
5572 | /* Begin the compound-statement. */ | |
7a3397c7 | 5573 | compound_stmt = begin_compound_stmt (/*has_no_scope=*/false); |
a723baf1 | 5574 | /* Parse an (optional) statement-seq. */ |
a5bcc582 | 5575 | cp_parser_statement_seq_opt (parser, in_statement_expr_p); |
a723baf1 | 5576 | /* Finish the compound-statement. */ |
7a3397c7 | 5577 | finish_compound_stmt (compound_stmt); |
a723baf1 MM |
5578 | /* Consume the `}'. */ |
5579 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
5580 | ||
5581 | return compound_stmt; | |
5582 | } | |
5583 | ||
5584 | /* Parse an (optional) statement-seq. | |
5585 | ||
5586 | statement-seq: | |
5587 | statement | |
5588 | statement-seq [opt] statement */ | |
5589 | ||
5590 | static void | |
a5bcc582 | 5591 | cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 MM |
5592 | { |
5593 | /* Scan statements until there aren't any more. */ | |
5594 | while (true) | |
5595 | { | |
5596 | /* If we're looking at a `}', then we've run out of statements. */ | |
5597 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE) | |
5598 | || cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
5599 | break; | |
5600 | ||
5601 | /* Parse the statement. */ | |
a5bcc582 | 5602 | cp_parser_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5603 | } |
5604 | } | |
5605 | ||
5606 | /* Parse a selection-statement. | |
5607 | ||
5608 | selection-statement: | |
5609 | if ( condition ) statement | |
5610 | if ( condition ) statement else statement | |
5611 | switch ( condition ) statement | |
5612 | ||
5613 | Returns the new IF_STMT or SWITCH_STMT. */ | |
5614 | ||
5615 | static tree | |
94edc4ab | 5616 | cp_parser_selection_statement (cp_parser* parser) |
a723baf1 MM |
5617 | { |
5618 | cp_token *token; | |
5619 | enum rid keyword; | |
5620 | ||
5621 | /* Peek at the next token. */ | |
5622 | token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement"); | |
5623 | ||
5624 | /* See what kind of keyword it is. */ | |
5625 | keyword = token->keyword; | |
5626 | switch (keyword) | |
5627 | { | |
5628 | case RID_IF: | |
5629 | case RID_SWITCH: | |
5630 | { | |
5631 | tree statement; | |
5632 | tree condition; | |
5633 | ||
5634 | /* Look for the `('. */ | |
5635 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) | |
5636 | { | |
5637 | cp_parser_skip_to_end_of_statement (parser); | |
5638 | return error_mark_node; | |
5639 | } | |
5640 | ||
5641 | /* Begin the selection-statement. */ | |
5642 | if (keyword == RID_IF) | |
5643 | statement = begin_if_stmt (); | |
5644 | else | |
5645 | statement = begin_switch_stmt (); | |
5646 | ||
5647 | /* Parse the condition. */ | |
5648 | condition = cp_parser_condition (parser); | |
5649 | /* Look for the `)'. */ | |
5650 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
a668c6ad MM |
5651 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
5652 | /*consume_paren=*/true); | |
a723baf1 MM |
5653 | |
5654 | if (keyword == RID_IF) | |
5655 | { | |
5656 | tree then_stmt; | |
5657 | ||
5658 | /* Add the condition. */ | |
5659 | finish_if_stmt_cond (condition, statement); | |
5660 | ||
5661 | /* Parse the then-clause. */ | |
5662 | then_stmt = cp_parser_implicitly_scoped_statement (parser); | |
5663 | finish_then_clause (statement); | |
5664 | ||
5665 | /* If the next token is `else', parse the else-clause. */ | |
5666 | if (cp_lexer_next_token_is_keyword (parser->lexer, | |
5667 | RID_ELSE)) | |
5668 | { | |
5669 | tree else_stmt; | |
5670 | ||
5671 | /* Consume the `else' keyword. */ | |
5672 | cp_lexer_consume_token (parser->lexer); | |
5673 | /* Parse the else-clause. */ | |
5674 | else_stmt | |
5675 | = cp_parser_implicitly_scoped_statement (parser); | |
5676 | finish_else_clause (statement); | |
5677 | } | |
5678 | ||
5679 | /* Now we're all done with the if-statement. */ | |
5680 | finish_if_stmt (); | |
5681 | } | |
5682 | else | |
5683 | { | |
5684 | tree body; | |
0e59b3fb | 5685 | bool in_switch_statement_p; |
a723baf1 MM |
5686 | |
5687 | /* Add the condition. */ | |
5688 | finish_switch_cond (condition, statement); | |
5689 | ||
5690 | /* Parse the body of the switch-statement. */ | |
0e59b3fb MM |
5691 | in_switch_statement_p = parser->in_switch_statement_p; |
5692 | parser->in_switch_statement_p = true; | |
a723baf1 | 5693 | body = cp_parser_implicitly_scoped_statement (parser); |
0e59b3fb | 5694 | parser->in_switch_statement_p = in_switch_statement_p; |
a723baf1 MM |
5695 | |
5696 | /* Now we're all done with the switch-statement. */ | |
5697 | finish_switch_stmt (statement); | |
5698 | } | |
5699 | ||
5700 | return statement; | |
5701 | } | |
5702 | break; | |
5703 | ||
5704 | default: | |
5705 | cp_parser_error (parser, "expected selection-statement"); | |
5706 | return error_mark_node; | |
5707 | } | |
5708 | } | |
5709 | ||
5710 | /* Parse a condition. | |
5711 | ||
5712 | condition: | |
5713 | expression | |
5714 | type-specifier-seq declarator = assignment-expression | |
5715 | ||
5716 | GNU Extension: | |
5717 | ||
5718 | condition: | |
5719 | type-specifier-seq declarator asm-specification [opt] | |
5720 | attributes [opt] = assignment-expression | |
5721 | ||
5722 | Returns the expression that should be tested. */ | |
5723 | ||
5724 | static tree | |
94edc4ab | 5725 | cp_parser_condition (cp_parser* parser) |
a723baf1 MM |
5726 | { |
5727 | tree type_specifiers; | |
5728 | const char *saved_message; | |
5729 | ||
5730 | /* Try the declaration first. */ | |
5731 | cp_parser_parse_tentatively (parser); | |
5732 | /* New types are not allowed in the type-specifier-seq for a | |
5733 | condition. */ | |
5734 | saved_message = parser->type_definition_forbidden_message; | |
5735 | parser->type_definition_forbidden_message | |
5736 | = "types may not be defined in conditions"; | |
5737 | /* Parse the type-specifier-seq. */ | |
5738 | type_specifiers = cp_parser_type_specifier_seq (parser); | |
5739 | /* Restore the saved message. */ | |
5740 | parser->type_definition_forbidden_message = saved_message; | |
5741 | /* If all is well, we might be looking at a declaration. */ | |
5742 | if (!cp_parser_error_occurred (parser)) | |
5743 | { | |
5744 | tree decl; | |
5745 | tree asm_specification; | |
5746 | tree attributes; | |
5747 | tree declarator; | |
5748 | tree initializer = NULL_TREE; | |
5749 | ||
5750 | /* Parse the declarator. */ | |
62b8a44e | 5751 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
5752 | /*ctor_dtor_or_conv_p=*/NULL, |
5753 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
5754 | /* Parse the attributes. */ |
5755 | attributes = cp_parser_attributes_opt (parser); | |
5756 | /* Parse the asm-specification. */ | |
5757 | asm_specification = cp_parser_asm_specification_opt (parser); | |
5758 | /* If the next token is not an `=', then we might still be | |
5759 | looking at an expression. For example: | |
5760 | ||
5761 | if (A(a).x) | |
5762 | ||
5763 | looks like a decl-specifier-seq and a declarator -- but then | |
5764 | there is no `=', so this is an expression. */ | |
5765 | cp_parser_require (parser, CPP_EQ, "`='"); | |
5766 | /* If we did see an `=', then we are looking at a declaration | |
5767 | for sure. */ | |
5768 | if (cp_parser_parse_definitely (parser)) | |
5769 | { | |
5770 | /* Create the declaration. */ | |
5771 | decl = start_decl (declarator, type_specifiers, | |
5772 | /*initialized_p=*/true, | |
5773 | attributes, /*prefix_attributes=*/NULL_TREE); | |
5774 | /* Parse the assignment-expression. */ | |
5775 | initializer = cp_parser_assignment_expression (parser); | |
5776 | ||
5777 | /* Process the initializer. */ | |
5778 | cp_finish_decl (decl, | |
5779 | initializer, | |
5780 | asm_specification, | |
5781 | LOOKUP_ONLYCONVERTING); | |
5782 | ||
5783 | return convert_from_reference (decl); | |
5784 | } | |
5785 | } | |
5786 | /* If we didn't even get past the declarator successfully, we are | |
5787 | definitely not looking at a declaration. */ | |
5788 | else | |
5789 | cp_parser_abort_tentative_parse (parser); | |
5790 | ||
5791 | /* Otherwise, we are looking at an expression. */ | |
5792 | return cp_parser_expression (parser); | |
5793 | } | |
5794 | ||
5795 | /* Parse an iteration-statement. | |
5796 | ||
5797 | iteration-statement: | |
5798 | while ( condition ) statement | |
5799 | do statement while ( expression ) ; | |
5800 | for ( for-init-statement condition [opt] ; expression [opt] ) | |
5801 | statement | |
5802 | ||
5803 | Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */ | |
5804 | ||
5805 | static tree | |
94edc4ab | 5806 | cp_parser_iteration_statement (cp_parser* parser) |
a723baf1 MM |
5807 | { |
5808 | cp_token *token; | |
5809 | enum rid keyword; | |
5810 | tree statement; | |
0e59b3fb MM |
5811 | bool in_iteration_statement_p; |
5812 | ||
a723baf1 MM |
5813 | |
5814 | /* Peek at the next token. */ | |
5815 | token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement"); | |
5816 | if (!token) | |
5817 | return error_mark_node; | |
5818 | ||
0e59b3fb MM |
5819 | /* Remember whether or not we are already within an iteration |
5820 | statement. */ | |
5821 | in_iteration_statement_p = parser->in_iteration_statement_p; | |
5822 | ||
a723baf1 MM |
5823 | /* See what kind of keyword it is. */ |
5824 | keyword = token->keyword; | |
5825 | switch (keyword) | |
5826 | { | |
5827 | case RID_WHILE: | |
5828 | { | |
5829 | tree condition; | |
5830 | ||
5831 | /* Begin the while-statement. */ | |
5832 | statement = begin_while_stmt (); | |
5833 | /* Look for the `('. */ | |
5834 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
5835 | /* Parse the condition. */ | |
5836 | condition = cp_parser_condition (parser); | |
5837 | finish_while_stmt_cond (condition, statement); | |
5838 | /* Look for the `)'. */ | |
5839 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
5840 | /* Parse the dependent statement. */ | |
0e59b3fb | 5841 | parser->in_iteration_statement_p = true; |
a723baf1 | 5842 | cp_parser_already_scoped_statement (parser); |
0e59b3fb | 5843 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
5844 | /* We're done with the while-statement. */ |
5845 | finish_while_stmt (statement); | |
5846 | } | |
5847 | break; | |
5848 | ||
5849 | case RID_DO: | |
5850 | { | |
5851 | tree expression; | |
5852 | ||
5853 | /* Begin the do-statement. */ | |
5854 | statement = begin_do_stmt (); | |
5855 | /* Parse the body of the do-statement. */ | |
0e59b3fb | 5856 | parser->in_iteration_statement_p = true; |
a723baf1 | 5857 | cp_parser_implicitly_scoped_statement (parser); |
0e59b3fb | 5858 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
5859 | finish_do_body (statement); |
5860 | /* Look for the `while' keyword. */ | |
5861 | cp_parser_require_keyword (parser, RID_WHILE, "`while'"); | |
5862 | /* Look for the `('. */ | |
5863 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
5864 | /* Parse the expression. */ | |
5865 | expression = cp_parser_expression (parser); | |
5866 | /* We're done with the do-statement. */ | |
5867 | finish_do_stmt (expression, statement); | |
5868 | /* Look for the `)'. */ | |
5869 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
5870 | /* Look for the `;'. */ | |
5871 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
5872 | } | |
5873 | break; | |
5874 | ||
5875 | case RID_FOR: | |
5876 | { | |
5877 | tree condition = NULL_TREE; | |
5878 | tree expression = NULL_TREE; | |
5879 | ||
5880 | /* Begin the for-statement. */ | |
5881 | statement = begin_for_stmt (); | |
5882 | /* Look for the `('. */ | |
5883 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
5884 | /* Parse the initialization. */ | |
5885 | cp_parser_for_init_statement (parser); | |
5886 | finish_for_init_stmt (statement); | |
5887 | ||
5888 | /* If there's a condition, process it. */ | |
5889 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5890 | condition = cp_parser_condition (parser); | |
5891 | finish_for_cond (condition, statement); | |
5892 | /* Look for the `;'. */ | |
5893 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
5894 | ||
5895 | /* If there's an expression, process it. */ | |
5896 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) | |
5897 | expression = cp_parser_expression (parser); | |
5898 | finish_for_expr (expression, statement); | |
5899 | /* Look for the `)'. */ | |
5900 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'"); | |
5901 | ||
5902 | /* Parse the body of the for-statement. */ | |
0e59b3fb | 5903 | parser->in_iteration_statement_p = true; |
a723baf1 | 5904 | cp_parser_already_scoped_statement (parser); |
0e59b3fb | 5905 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
5906 | |
5907 | /* We're done with the for-statement. */ | |
5908 | finish_for_stmt (statement); | |
5909 | } | |
5910 | break; | |
5911 | ||
5912 | default: | |
5913 | cp_parser_error (parser, "expected iteration-statement"); | |
5914 | statement = error_mark_node; | |
5915 | break; | |
5916 | } | |
5917 | ||
5918 | return statement; | |
5919 | } | |
5920 | ||
5921 | /* Parse a for-init-statement. | |
5922 | ||
5923 | for-init-statement: | |
5924 | expression-statement | |
5925 | simple-declaration */ | |
5926 | ||
5927 | static void | |
94edc4ab | 5928 | cp_parser_for_init_statement (cp_parser* parser) |
a723baf1 MM |
5929 | { |
5930 | /* If the next token is a `;', then we have an empty | |
34cd5ae7 | 5931 | expression-statement. Grammatically, this is also a |
a723baf1 MM |
5932 | simple-declaration, but an invalid one, because it does not |
5933 | declare anything. Therefore, if we did not handle this case | |
5934 | specially, we would issue an error message about an invalid | |
5935 | declaration. */ | |
5936 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5937 | { | |
5938 | /* We're going to speculatively look for a declaration, falling back | |
5939 | to an expression, if necessary. */ | |
5940 | cp_parser_parse_tentatively (parser); | |
5941 | /* Parse the declaration. */ | |
5942 | cp_parser_simple_declaration (parser, | |
5943 | /*function_definition_allowed_p=*/false); | |
5944 | /* If the tentative parse failed, then we shall need to look for an | |
5945 | expression-statement. */ | |
5946 | if (cp_parser_parse_definitely (parser)) | |
5947 | return; | |
5948 | } | |
5949 | ||
a5bcc582 | 5950 | cp_parser_expression_statement (parser, false); |
a723baf1 MM |
5951 | } |
5952 | ||
5953 | /* Parse a jump-statement. | |
5954 | ||
5955 | jump-statement: | |
5956 | break ; | |
5957 | continue ; | |
5958 | return expression [opt] ; | |
5959 | goto identifier ; | |
5960 | ||
5961 | GNU extension: | |
5962 | ||
5963 | jump-statement: | |
5964 | goto * expression ; | |
5965 | ||
5966 | Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or | |
5967 | GOTO_STMT. */ | |
5968 | ||
5969 | static tree | |
94edc4ab | 5970 | cp_parser_jump_statement (cp_parser* parser) |
a723baf1 MM |
5971 | { |
5972 | tree statement = error_mark_node; | |
5973 | cp_token *token; | |
5974 | enum rid keyword; | |
5975 | ||
5976 | /* Peek at the next token. */ | |
5977 | token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement"); | |
5978 | if (!token) | |
5979 | return error_mark_node; | |
5980 | ||
5981 | /* See what kind of keyword it is. */ | |
5982 | keyword = token->keyword; | |
5983 | switch (keyword) | |
5984 | { | |
5985 | case RID_BREAK: | |
0e59b3fb MM |
5986 | if (!parser->in_switch_statement_p |
5987 | && !parser->in_iteration_statement_p) | |
5988 | { | |
5989 | error ("break statement not within loop or switch"); | |
5990 | statement = error_mark_node; | |
5991 | } | |
5992 | else | |
5993 | statement = finish_break_stmt (); | |
a723baf1 MM |
5994 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
5995 | break; | |
5996 | ||
5997 | case RID_CONTINUE: | |
0e59b3fb MM |
5998 | if (!parser->in_iteration_statement_p) |
5999 | { | |
6000 | error ("continue statement not within a loop"); | |
6001 | statement = error_mark_node; | |
6002 | } | |
6003 | else | |
6004 | statement = finish_continue_stmt (); | |
a723baf1 MM |
6005 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
6006 | break; | |
6007 | ||
6008 | case RID_RETURN: | |
6009 | { | |
6010 | tree expr; | |
6011 | ||
6012 | /* If the next token is a `;', then there is no | |
6013 | expression. */ | |
6014 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
6015 | expr = cp_parser_expression (parser); | |
6016 | else | |
6017 | expr = NULL_TREE; | |
6018 | /* Build the return-statement. */ | |
6019 | statement = finish_return_stmt (expr); | |
6020 | /* Look for the final `;'. */ | |
6021 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6022 | } | |
6023 | break; | |
6024 | ||
6025 | case RID_GOTO: | |
6026 | /* Create the goto-statement. */ | |
6027 | if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)) | |
6028 | { | |
6029 | /* Issue a warning about this use of a GNU extension. */ | |
6030 | if (pedantic) | |
6031 | pedwarn ("ISO C++ forbids computed gotos"); | |
6032 | /* Consume the '*' token. */ | |
6033 | cp_lexer_consume_token (parser->lexer); | |
6034 | /* Parse the dependent expression. */ | |
6035 | finish_goto_stmt (cp_parser_expression (parser)); | |
6036 | } | |
6037 | else | |
6038 | finish_goto_stmt (cp_parser_identifier (parser)); | |
6039 | /* Look for the final `;'. */ | |
6040 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6041 | break; | |
6042 | ||
6043 | default: | |
6044 | cp_parser_error (parser, "expected jump-statement"); | |
6045 | break; | |
6046 | } | |
6047 | ||
6048 | return statement; | |
6049 | } | |
6050 | ||
6051 | /* Parse a declaration-statement. | |
6052 | ||
6053 | declaration-statement: | |
6054 | block-declaration */ | |
6055 | ||
6056 | static void | |
94edc4ab | 6057 | cp_parser_declaration_statement (cp_parser* parser) |
a723baf1 MM |
6058 | { |
6059 | /* Parse the block-declaration. */ | |
6060 | cp_parser_block_declaration (parser, /*statement_p=*/true); | |
6061 | ||
6062 | /* Finish off the statement. */ | |
6063 | finish_stmt (); | |
6064 | } | |
6065 | ||
6066 | /* Some dependent statements (like `if (cond) statement'), are | |
6067 | implicitly in their own scope. In other words, if the statement is | |
6068 | a single statement (as opposed to a compound-statement), it is | |
6069 | none-the-less treated as if it were enclosed in braces. Any | |
6070 | declarations appearing in the dependent statement are out of scope | |
6071 | after control passes that point. This function parses a statement, | |
6072 | but ensures that is in its own scope, even if it is not a | |
6073 | compound-statement. | |
6074 | ||
6075 | Returns the new statement. */ | |
6076 | ||
6077 | static tree | |
94edc4ab | 6078 | cp_parser_implicitly_scoped_statement (cp_parser* parser) |
a723baf1 MM |
6079 | { |
6080 | tree statement; | |
6081 | ||
6082 | /* If the token is not a `{', then we must take special action. */ | |
6083 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
6084 | { | |
6085 | /* Create a compound-statement. */ | |
7a3397c7 | 6086 | statement = begin_compound_stmt (/*has_no_scope=*/false); |
a723baf1 | 6087 | /* Parse the dependent-statement. */ |
a5bcc582 | 6088 | cp_parser_statement (parser, false); |
a723baf1 | 6089 | /* Finish the dummy compound-statement. */ |
7a3397c7 | 6090 | finish_compound_stmt (statement); |
a723baf1 MM |
6091 | } |
6092 | /* Otherwise, we simply parse the statement directly. */ | |
6093 | else | |
a5bcc582 | 6094 | statement = cp_parser_compound_statement (parser, false); |
a723baf1 MM |
6095 | |
6096 | /* Return the statement. */ | |
6097 | return statement; | |
6098 | } | |
6099 | ||
6100 | /* For some dependent statements (like `while (cond) statement'), we | |
6101 | have already created a scope. Therefore, even if the dependent | |
6102 | statement is a compound-statement, we do not want to create another | |
6103 | scope. */ | |
6104 | ||
6105 | static void | |
94edc4ab | 6106 | cp_parser_already_scoped_statement (cp_parser* parser) |
a723baf1 MM |
6107 | { |
6108 | /* If the token is not a `{', then we must take special action. */ | |
6109 | if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE)) | |
6110 | { | |
6111 | tree statement; | |
6112 | ||
6113 | /* Create a compound-statement. */ | |
7a3397c7 | 6114 | statement = begin_compound_stmt (/*has_no_scope=*/true); |
a723baf1 | 6115 | /* Parse the dependent-statement. */ |
a5bcc582 | 6116 | cp_parser_statement (parser, false); |
a723baf1 | 6117 | /* Finish the dummy compound-statement. */ |
7a3397c7 | 6118 | finish_compound_stmt (statement); |
a723baf1 MM |
6119 | } |
6120 | /* Otherwise, we simply parse the statement directly. */ | |
6121 | else | |
a5bcc582 | 6122 | cp_parser_statement (parser, false); |
a723baf1 MM |
6123 | } |
6124 | ||
6125 | /* Declarations [gram.dcl.dcl] */ | |
6126 | ||
6127 | /* Parse an optional declaration-sequence. | |
6128 | ||
6129 | declaration-seq: | |
6130 | declaration | |
6131 | declaration-seq declaration */ | |
6132 | ||
6133 | static void | |
94edc4ab | 6134 | cp_parser_declaration_seq_opt (cp_parser* parser) |
a723baf1 MM |
6135 | { |
6136 | while (true) | |
6137 | { | |
6138 | cp_token *token; | |
6139 | ||
6140 | token = cp_lexer_peek_token (parser->lexer); | |
6141 | ||
6142 | if (token->type == CPP_CLOSE_BRACE | |
6143 | || token->type == CPP_EOF) | |
6144 | break; | |
6145 | ||
6146 | if (token->type == CPP_SEMICOLON) | |
6147 | { | |
6148 | /* A declaration consisting of a single semicolon is | |
6149 | invalid. Allow it unless we're being pedantic. */ | |
499b568f | 6150 | if (pedantic && !in_system_header) |
a723baf1 MM |
6151 | pedwarn ("extra `;'"); |
6152 | cp_lexer_consume_token (parser->lexer); | |
6153 | continue; | |
6154 | } | |
6155 | ||
c838d82f | 6156 | /* The C lexer modifies PENDING_LANG_CHANGE when it wants the |
34cd5ae7 | 6157 | parser to enter or exit implicit `extern "C"' blocks. */ |
c838d82f MM |
6158 | while (pending_lang_change > 0) |
6159 | { | |
6160 | push_lang_context (lang_name_c); | |
6161 | --pending_lang_change; | |
6162 | } | |
6163 | while (pending_lang_change < 0) | |
6164 | { | |
6165 | pop_lang_context (); | |
6166 | ++pending_lang_change; | |
6167 | } | |
6168 | ||
6169 | /* Parse the declaration itself. */ | |
a723baf1 MM |
6170 | cp_parser_declaration (parser); |
6171 | } | |
6172 | } | |
6173 | ||
6174 | /* Parse a declaration. | |
6175 | ||
6176 | declaration: | |
6177 | block-declaration | |
6178 | function-definition | |
6179 | template-declaration | |
6180 | explicit-instantiation | |
6181 | explicit-specialization | |
6182 | linkage-specification | |
1092805d MM |
6183 | namespace-definition |
6184 | ||
6185 | GNU extension: | |
6186 | ||
6187 | declaration: | |
6188 | __extension__ declaration */ | |
a723baf1 MM |
6189 | |
6190 | static void | |
94edc4ab | 6191 | cp_parser_declaration (cp_parser* parser) |
a723baf1 MM |
6192 | { |
6193 | cp_token token1; | |
6194 | cp_token token2; | |
1092805d MM |
6195 | int saved_pedantic; |
6196 | ||
6197 | /* Check for the `__extension__' keyword. */ | |
6198 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
6199 | { | |
6200 | /* Parse the qualified declaration. */ | |
6201 | cp_parser_declaration (parser); | |
6202 | /* Restore the PEDANTIC flag. */ | |
6203 | pedantic = saved_pedantic; | |
6204 | ||
6205 | return; | |
6206 | } | |
a723baf1 MM |
6207 | |
6208 | /* Try to figure out what kind of declaration is present. */ | |
6209 | token1 = *cp_lexer_peek_token (parser->lexer); | |
6210 | if (token1.type != CPP_EOF) | |
6211 | token2 = *cp_lexer_peek_nth_token (parser->lexer, 2); | |
6212 | ||
6213 | /* If the next token is `extern' and the following token is a string | |
6214 | literal, then we have a linkage specification. */ | |
6215 | if (token1.keyword == RID_EXTERN | |
6216 | && cp_parser_is_string_literal (&token2)) | |
6217 | cp_parser_linkage_specification (parser); | |
6218 | /* If the next token is `template', then we have either a template | |
6219 | declaration, an explicit instantiation, or an explicit | |
6220 | specialization. */ | |
6221 | else if (token1.keyword == RID_TEMPLATE) | |
6222 | { | |
6223 | /* `template <>' indicates a template specialization. */ | |
6224 | if (token2.type == CPP_LESS | |
6225 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER) | |
6226 | cp_parser_explicit_specialization (parser); | |
6227 | /* `template <' indicates a template declaration. */ | |
6228 | else if (token2.type == CPP_LESS) | |
6229 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
6230 | /* Anything else must be an explicit instantiation. */ | |
6231 | else | |
6232 | cp_parser_explicit_instantiation (parser); | |
6233 | } | |
6234 | /* If the next token is `export', then we have a template | |
6235 | declaration. */ | |
6236 | else if (token1.keyword == RID_EXPORT) | |
6237 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
6238 | /* If the next token is `extern', 'static' or 'inline' and the one | |
6239 | after that is `template', we have a GNU extended explicit | |
6240 | instantiation directive. */ | |
6241 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
6242 | && (token1.keyword == RID_EXTERN | |
6243 | || token1.keyword == RID_STATIC | |
6244 | || token1.keyword == RID_INLINE) | |
6245 | && token2.keyword == RID_TEMPLATE) | |
6246 | cp_parser_explicit_instantiation (parser); | |
6247 | /* If the next token is `namespace', check for a named or unnamed | |
6248 | namespace definition. */ | |
6249 | else if (token1.keyword == RID_NAMESPACE | |
6250 | && (/* A named namespace definition. */ | |
6251 | (token2.type == CPP_NAME | |
6252 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type | |
6253 | == CPP_OPEN_BRACE)) | |
6254 | /* An unnamed namespace definition. */ | |
6255 | || token2.type == CPP_OPEN_BRACE)) | |
6256 | cp_parser_namespace_definition (parser); | |
6257 | /* We must have either a block declaration or a function | |
6258 | definition. */ | |
6259 | else | |
6260 | /* Try to parse a block-declaration, or a function-definition. */ | |
6261 | cp_parser_block_declaration (parser, /*statement_p=*/false); | |
6262 | } | |
6263 | ||
6264 | /* Parse a block-declaration. | |
6265 | ||
6266 | block-declaration: | |
6267 | simple-declaration | |
6268 | asm-definition | |
6269 | namespace-alias-definition | |
6270 | using-declaration | |
6271 | using-directive | |
6272 | ||
6273 | GNU Extension: | |
6274 | ||
6275 | block-declaration: | |
6276 | __extension__ block-declaration | |
6277 | label-declaration | |
6278 | ||
34cd5ae7 | 6279 | If STATEMENT_P is TRUE, then this block-declaration is occurring as |
a723baf1 MM |
6280 | part of a declaration-statement. */ |
6281 | ||
6282 | static void | |
6283 | cp_parser_block_declaration (cp_parser *parser, | |
6284 | bool statement_p) | |
6285 | { | |
6286 | cp_token *token1; | |
6287 | int saved_pedantic; | |
6288 | ||
6289 | /* Check for the `__extension__' keyword. */ | |
6290 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
6291 | { | |
6292 | /* Parse the qualified declaration. */ | |
6293 | cp_parser_block_declaration (parser, statement_p); | |
6294 | /* Restore the PEDANTIC flag. */ | |
6295 | pedantic = saved_pedantic; | |
6296 | ||
6297 | return; | |
6298 | } | |
6299 | ||
6300 | /* Peek at the next token to figure out which kind of declaration is | |
6301 | present. */ | |
6302 | token1 = cp_lexer_peek_token (parser->lexer); | |
6303 | ||
6304 | /* If the next keyword is `asm', we have an asm-definition. */ | |
6305 | if (token1->keyword == RID_ASM) | |
6306 | { | |
6307 | if (statement_p) | |
6308 | cp_parser_commit_to_tentative_parse (parser); | |
6309 | cp_parser_asm_definition (parser); | |
6310 | } | |
6311 | /* If the next keyword is `namespace', we have a | |
6312 | namespace-alias-definition. */ | |
6313 | else if (token1->keyword == RID_NAMESPACE) | |
6314 | cp_parser_namespace_alias_definition (parser); | |
6315 | /* If the next keyword is `using', we have either a | |
6316 | using-declaration or a using-directive. */ | |
6317 | else if (token1->keyword == RID_USING) | |
6318 | { | |
6319 | cp_token *token2; | |
6320 | ||
6321 | if (statement_p) | |
6322 | cp_parser_commit_to_tentative_parse (parser); | |
6323 | /* If the token after `using' is `namespace', then we have a | |
6324 | using-directive. */ | |
6325 | token2 = cp_lexer_peek_nth_token (parser->lexer, 2); | |
6326 | if (token2->keyword == RID_NAMESPACE) | |
6327 | cp_parser_using_directive (parser); | |
6328 | /* Otherwise, it's a using-declaration. */ | |
6329 | else | |
6330 | cp_parser_using_declaration (parser); | |
6331 | } | |
6332 | /* If the next keyword is `__label__' we have a label declaration. */ | |
6333 | else if (token1->keyword == RID_LABEL) | |
6334 | { | |
6335 | if (statement_p) | |
6336 | cp_parser_commit_to_tentative_parse (parser); | |
6337 | cp_parser_label_declaration (parser); | |
6338 | } | |
6339 | /* Anything else must be a simple-declaration. */ | |
6340 | else | |
6341 | cp_parser_simple_declaration (parser, !statement_p); | |
6342 | } | |
6343 | ||
6344 | /* Parse a simple-declaration. | |
6345 | ||
6346 | simple-declaration: | |
6347 | decl-specifier-seq [opt] init-declarator-list [opt] ; | |
6348 | ||
6349 | init-declarator-list: | |
6350 | init-declarator | |
6351 | init-declarator-list , init-declarator | |
6352 | ||
34cd5ae7 | 6353 | If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a |
9bcb9aae | 6354 | function-definition as a simple-declaration. */ |
a723baf1 MM |
6355 | |
6356 | static void | |
94edc4ab NN |
6357 | cp_parser_simple_declaration (cp_parser* parser, |
6358 | bool function_definition_allowed_p) | |
a723baf1 MM |
6359 | { |
6360 | tree decl_specifiers; | |
6361 | tree attributes; | |
560ad596 | 6362 | int declares_class_or_enum; |
a723baf1 MM |
6363 | bool saw_declarator; |
6364 | ||
6365 | /* Defer access checks until we know what is being declared; the | |
6366 | checks for names appearing in the decl-specifier-seq should be | |
6367 | done as if we were in the scope of the thing being declared. */ | |
8d241e0b | 6368 | push_deferring_access_checks (dk_deferred); |
cf22909c | 6369 | |
a723baf1 MM |
6370 | /* Parse the decl-specifier-seq. We have to keep track of whether |
6371 | or not the decl-specifier-seq declares a named class or | |
6372 | enumeration type, since that is the only case in which the | |
6373 | init-declarator-list is allowed to be empty. | |
6374 | ||
6375 | [dcl.dcl] | |
6376 | ||
6377 | In a simple-declaration, the optional init-declarator-list can be | |
6378 | omitted only when declaring a class or enumeration, that is when | |
6379 | the decl-specifier-seq contains either a class-specifier, an | |
6380 | elaborated-type-specifier, or an enum-specifier. */ | |
6381 | decl_specifiers | |
6382 | = cp_parser_decl_specifier_seq (parser, | |
6383 | CP_PARSER_FLAGS_OPTIONAL, | |
6384 | &attributes, | |
6385 | &declares_class_or_enum); | |
6386 | /* We no longer need to defer access checks. */ | |
cf22909c | 6387 | stop_deferring_access_checks (); |
24c0ef37 | 6388 | |
39703eb9 MM |
6389 | /* In a block scope, a valid declaration must always have a |
6390 | decl-specifier-seq. By not trying to parse declarators, we can | |
6391 | resolve the declaration/expression ambiguity more quickly. */ | |
6392 | if (!function_definition_allowed_p && !decl_specifiers) | |
6393 | { | |
6394 | cp_parser_error (parser, "expected declaration"); | |
6395 | goto done; | |
6396 | } | |
6397 | ||
8fbc5ae7 MM |
6398 | /* If the next two tokens are both identifiers, the code is |
6399 | erroneous. The usual cause of this situation is code like: | |
6400 | ||
6401 | T t; | |
6402 | ||
6403 | where "T" should name a type -- but does not. */ | |
6404 | if (cp_parser_diagnose_invalid_type_name (parser)) | |
6405 | { | |
8d241e0b | 6406 | /* If parsing tentatively, we should commit; we really are |
8fbc5ae7 MM |
6407 | looking at a declaration. */ |
6408 | cp_parser_commit_to_tentative_parse (parser); | |
6409 | /* Give up. */ | |
39703eb9 | 6410 | goto done; |
8fbc5ae7 MM |
6411 | } |
6412 | ||
a723baf1 MM |
6413 | /* Keep going until we hit the `;' at the end of the simple |
6414 | declaration. */ | |
6415 | saw_declarator = false; | |
6416 | while (cp_lexer_next_token_is_not (parser->lexer, | |
6417 | CPP_SEMICOLON)) | |
6418 | { | |
6419 | cp_token *token; | |
6420 | bool function_definition_p; | |
560ad596 | 6421 | tree decl; |
a723baf1 MM |
6422 | |
6423 | saw_declarator = true; | |
6424 | /* Parse the init-declarator. */ | |
560ad596 MM |
6425 | decl = cp_parser_init_declarator (parser, decl_specifiers, attributes, |
6426 | function_definition_allowed_p, | |
6427 | /*member_p=*/false, | |
6428 | declares_class_or_enum, | |
6429 | &function_definition_p); | |
1fb3244a MM |
6430 | /* If an error occurred while parsing tentatively, exit quickly. |
6431 | (That usually happens when in the body of a function; each | |
6432 | statement is treated as a declaration-statement until proven | |
6433 | otherwise.) */ | |
6434 | if (cp_parser_error_occurred (parser)) | |
39703eb9 | 6435 | goto done; |
a723baf1 MM |
6436 | /* Handle function definitions specially. */ |
6437 | if (function_definition_p) | |
6438 | { | |
6439 | /* If the next token is a `,', then we are probably | |
6440 | processing something like: | |
6441 | ||
6442 | void f() {}, *p; | |
6443 | ||
6444 | which is erroneous. */ | |
6445 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
6446 | error ("mixing declarations and function-definitions is forbidden"); | |
6447 | /* Otherwise, we're done with the list of declarators. */ | |
6448 | else | |
24c0ef37 | 6449 | { |
cf22909c | 6450 | pop_deferring_access_checks (); |
24c0ef37 GS |
6451 | return; |
6452 | } | |
a723baf1 MM |
6453 | } |
6454 | /* The next token should be either a `,' or a `;'. */ | |
6455 | token = cp_lexer_peek_token (parser->lexer); | |
6456 | /* If it's a `,', there are more declarators to come. */ | |
6457 | if (token->type == CPP_COMMA) | |
6458 | cp_lexer_consume_token (parser->lexer); | |
6459 | /* If it's a `;', we are done. */ | |
6460 | else if (token->type == CPP_SEMICOLON) | |
6461 | break; | |
6462 | /* Anything else is an error. */ | |
6463 | else | |
6464 | { | |
6465 | cp_parser_error (parser, "expected `,' or `;'"); | |
6466 | /* Skip tokens until we reach the end of the statement. */ | |
6467 | cp_parser_skip_to_end_of_statement (parser); | |
39703eb9 | 6468 | goto done; |
a723baf1 MM |
6469 | } |
6470 | /* After the first time around, a function-definition is not | |
6471 | allowed -- even if it was OK at first. For example: | |
6472 | ||
6473 | int i, f() {} | |
6474 | ||
6475 | is not valid. */ | |
6476 | function_definition_allowed_p = false; | |
6477 | } | |
6478 | ||
6479 | /* Issue an error message if no declarators are present, and the | |
6480 | decl-specifier-seq does not itself declare a class or | |
6481 | enumeration. */ | |
6482 | if (!saw_declarator) | |
6483 | { | |
6484 | if (cp_parser_declares_only_class_p (parser)) | |
6485 | shadow_tag (decl_specifiers); | |
6486 | /* Perform any deferred access checks. */ | |
cf22909c | 6487 | perform_deferred_access_checks (); |
a723baf1 MM |
6488 | } |
6489 | ||
6490 | /* Consume the `;'. */ | |
6491 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6492 | ||
39703eb9 MM |
6493 | done: |
6494 | pop_deferring_access_checks (); | |
a723baf1 MM |
6495 | } |
6496 | ||
6497 | /* Parse a decl-specifier-seq. | |
6498 | ||
6499 | decl-specifier-seq: | |
6500 | decl-specifier-seq [opt] decl-specifier | |
6501 | ||
6502 | decl-specifier: | |
6503 | storage-class-specifier | |
6504 | type-specifier | |
6505 | function-specifier | |
6506 | friend | |
6507 | typedef | |
6508 | ||
6509 | GNU Extension: | |
6510 | ||
6511 | decl-specifier-seq: | |
6512 | decl-specifier-seq [opt] attributes | |
6513 | ||
6514 | Returns a TREE_LIST, giving the decl-specifiers in the order they | |
6515 | appear in the source code. The TREE_VALUE of each node is the | |
6516 | decl-specifier. For a keyword (such as `auto' or `friend'), the | |
34cd5ae7 | 6517 | TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the |
a723baf1 MM |
6518 | representation of a type-specifier, see cp_parser_type_specifier. |
6519 | ||
6520 | If there are attributes, they will be stored in *ATTRIBUTES, | |
6521 | represented as described above cp_parser_attributes. | |
6522 | ||
6523 | If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier | |
6524 | appears, and the entity that will be a friend is not going to be a | |
6525 | class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that | |
6526 | even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which | |
560ad596 MM |
6527 | friendship is granted might not be a class. |
6528 | ||
6529 | *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following | |
6530 | *flags: | |
6531 | ||
6532 | 1: one of the decl-specifiers is an elaborated-type-specifier | |
6533 | 2: one of the decl-specifiers is an enum-specifier or a | |
6534 | class-specifier | |
6535 | ||
6536 | */ | |
a723baf1 MM |
6537 | |
6538 | static tree | |
94edc4ab NN |
6539 | cp_parser_decl_specifier_seq (cp_parser* parser, |
6540 | cp_parser_flags flags, | |
6541 | tree* attributes, | |
560ad596 | 6542 | int* declares_class_or_enum) |
a723baf1 MM |
6543 | { |
6544 | tree decl_specs = NULL_TREE; | |
6545 | bool friend_p = false; | |
f2ce60b8 NS |
6546 | bool constructor_possible_p = !parser->in_declarator_p; |
6547 | ||
a723baf1 | 6548 | /* Assume no class or enumeration type is declared. */ |
560ad596 | 6549 | *declares_class_or_enum = 0; |
a723baf1 MM |
6550 | |
6551 | /* Assume there are no attributes. */ | |
6552 | *attributes = NULL_TREE; | |
6553 | ||
6554 | /* Keep reading specifiers until there are no more to read. */ | |
6555 | while (true) | |
6556 | { | |
6557 | tree decl_spec = NULL_TREE; | |
6558 | bool constructor_p; | |
6559 | cp_token *token; | |
6560 | ||
6561 | /* Peek at the next token. */ | |
6562 | token = cp_lexer_peek_token (parser->lexer); | |
6563 | /* Handle attributes. */ | |
6564 | if (token->keyword == RID_ATTRIBUTE) | |
6565 | { | |
6566 | /* Parse the attributes. */ | |
6567 | decl_spec = cp_parser_attributes_opt (parser); | |
6568 | /* Add them to the list. */ | |
6569 | *attributes = chainon (*attributes, decl_spec); | |
6570 | continue; | |
6571 | } | |
6572 | /* If the next token is an appropriate keyword, we can simply | |
6573 | add it to the list. */ | |
6574 | switch (token->keyword) | |
6575 | { | |
6576 | case RID_FRIEND: | |
6577 | /* decl-specifier: | |
6578 | friend */ | |
1918facf SB |
6579 | if (friend_p) |
6580 | error ("duplicate `friend'"); | |
6581 | else | |
6582 | friend_p = true; | |
a723baf1 MM |
6583 | /* The representation of the specifier is simply the |
6584 | appropriate TREE_IDENTIFIER node. */ | |
6585 | decl_spec = token->value; | |
6586 | /* Consume the token. */ | |
6587 | cp_lexer_consume_token (parser->lexer); | |
6588 | break; | |
6589 | ||
6590 | /* function-specifier: | |
6591 | inline | |
6592 | virtual | |
6593 | explicit */ | |
6594 | case RID_INLINE: | |
6595 | case RID_VIRTUAL: | |
6596 | case RID_EXPLICIT: | |
6597 | decl_spec = cp_parser_function_specifier_opt (parser); | |
6598 | break; | |
6599 | ||
6600 | /* decl-specifier: | |
6601 | typedef */ | |
6602 | case RID_TYPEDEF: | |
6603 | /* The representation of the specifier is simply the | |
6604 | appropriate TREE_IDENTIFIER node. */ | |
6605 | decl_spec = token->value; | |
6606 | /* Consume the token. */ | |
6607 | cp_lexer_consume_token (parser->lexer); | |
2050a1bb MM |
6608 | /* A constructor declarator cannot appear in a typedef. */ |
6609 | constructor_possible_p = false; | |
c006d942 MM |
6610 | /* The "typedef" keyword can only occur in a declaration; we |
6611 | may as well commit at this point. */ | |
6612 | cp_parser_commit_to_tentative_parse (parser); | |
a723baf1 MM |
6613 | break; |
6614 | ||
6615 | /* storage-class-specifier: | |
6616 | auto | |
6617 | register | |
6618 | static | |
6619 | extern | |
6620 | mutable | |
6621 | ||
6622 | GNU Extension: | |
6623 | thread */ | |
6624 | case RID_AUTO: | |
6625 | case RID_REGISTER: | |
6626 | case RID_STATIC: | |
6627 | case RID_EXTERN: | |
6628 | case RID_MUTABLE: | |
6629 | case RID_THREAD: | |
6630 | decl_spec = cp_parser_storage_class_specifier_opt (parser); | |
6631 | break; | |
6632 | ||
6633 | default: | |
6634 | break; | |
6635 | } | |
6636 | ||
6637 | /* Constructors are a special case. The `S' in `S()' is not a | |
6638 | decl-specifier; it is the beginning of the declarator. */ | |
6639 | constructor_p = (!decl_spec | |
2050a1bb | 6640 | && constructor_possible_p |
a723baf1 MM |
6641 | && cp_parser_constructor_declarator_p (parser, |
6642 | friend_p)); | |
6643 | ||
6644 | /* If we don't have a DECL_SPEC yet, then we must be looking at | |
6645 | a type-specifier. */ | |
6646 | if (!decl_spec && !constructor_p) | |
6647 | { | |
560ad596 | 6648 | int decl_spec_declares_class_or_enum; |
a723baf1 MM |
6649 | bool is_cv_qualifier; |
6650 | ||
6651 | decl_spec | |
6652 | = cp_parser_type_specifier (parser, flags, | |
6653 | friend_p, | |
6654 | /*is_declaration=*/true, | |
6655 | &decl_spec_declares_class_or_enum, | |
6656 | &is_cv_qualifier); | |
6657 | ||
6658 | *declares_class_or_enum |= decl_spec_declares_class_or_enum; | |
6659 | ||
6660 | /* If this type-specifier referenced a user-defined type | |
6661 | (a typedef, class-name, etc.), then we can't allow any | |
6662 | more such type-specifiers henceforth. | |
6663 | ||
6664 | [dcl.spec] | |
6665 | ||
6666 | The longest sequence of decl-specifiers that could | |
6667 | possibly be a type name is taken as the | |
6668 | decl-specifier-seq of a declaration. The sequence shall | |
6669 | be self-consistent as described below. | |
6670 | ||
6671 | [dcl.type] | |
6672 | ||
6673 | As a general rule, at most one type-specifier is allowed | |
6674 | in the complete decl-specifier-seq of a declaration. The | |
6675 | only exceptions are the following: | |
6676 | ||
6677 | -- const or volatile can be combined with any other | |
6678 | type-specifier. | |
6679 | ||
6680 | -- signed or unsigned can be combined with char, long, | |
6681 | short, or int. | |
6682 | ||
6683 | -- .. | |
6684 | ||
6685 | Example: | |
6686 | ||
6687 | typedef char* Pc; | |
6688 | void g (const int Pc); | |
6689 | ||
6690 | Here, Pc is *not* part of the decl-specifier seq; it's | |
6691 | the declarator. Therefore, once we see a type-specifier | |
6692 | (other than a cv-qualifier), we forbid any additional | |
6693 | user-defined types. We *do* still allow things like `int | |
6694 | int' to be considered a decl-specifier-seq, and issue the | |
6695 | error message later. */ | |
6696 | if (decl_spec && !is_cv_qualifier) | |
6697 | flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; | |
2050a1bb MM |
6698 | /* A constructor declarator cannot follow a type-specifier. */ |
6699 | if (decl_spec) | |
6700 | constructor_possible_p = false; | |
a723baf1 MM |
6701 | } |
6702 | ||
6703 | /* If we still do not have a DECL_SPEC, then there are no more | |
6704 | decl-specifiers. */ | |
6705 | if (!decl_spec) | |
6706 | { | |
6707 | /* Issue an error message, unless the entire construct was | |
6708 | optional. */ | |
6709 | if (!(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
6710 | { | |
6711 | cp_parser_error (parser, "expected decl specifier"); | |
6712 | return error_mark_node; | |
6713 | } | |
6714 | ||
6715 | break; | |
6716 | } | |
6717 | ||
6718 | /* Add the DECL_SPEC to the list of specifiers. */ | |
6719 | decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs); | |
6720 | ||
6721 | /* After we see one decl-specifier, further decl-specifiers are | |
6722 | always optional. */ | |
6723 | flags |= CP_PARSER_FLAGS_OPTIONAL; | |
6724 | } | |
6725 | ||
6726 | /* We have built up the DECL_SPECS in reverse order. Return them in | |
6727 | the correct order. */ | |
6728 | return nreverse (decl_specs); | |
6729 | } | |
6730 | ||
6731 | /* Parse an (optional) storage-class-specifier. | |
6732 | ||
6733 | storage-class-specifier: | |
6734 | auto | |
6735 | register | |
6736 | static | |
6737 | extern | |
6738 | mutable | |
6739 | ||
6740 | GNU Extension: | |
6741 | ||
6742 | storage-class-specifier: | |
6743 | thread | |
6744 | ||
6745 | Returns an IDENTIFIER_NODE corresponding to the keyword used. */ | |
6746 | ||
6747 | static tree | |
94edc4ab | 6748 | cp_parser_storage_class_specifier_opt (cp_parser* parser) |
a723baf1 MM |
6749 | { |
6750 | switch (cp_lexer_peek_token (parser->lexer)->keyword) | |
6751 | { | |
6752 | case RID_AUTO: | |
6753 | case RID_REGISTER: | |
6754 | case RID_STATIC: | |
6755 | case RID_EXTERN: | |
6756 | case RID_MUTABLE: | |
6757 | case RID_THREAD: | |
6758 | /* Consume the token. */ | |
6759 | return cp_lexer_consume_token (parser->lexer)->value; | |
6760 | ||
6761 | default: | |
6762 | return NULL_TREE; | |
6763 | } | |
6764 | } | |
6765 | ||
6766 | /* Parse an (optional) function-specifier. | |
6767 | ||
6768 | function-specifier: | |
6769 | inline | |
6770 | virtual | |
6771 | explicit | |
6772 | ||
6773 | Returns an IDENTIFIER_NODE corresponding to the keyword used. */ | |
6774 | ||
6775 | static tree | |
94edc4ab | 6776 | cp_parser_function_specifier_opt (cp_parser* parser) |
a723baf1 MM |
6777 | { |
6778 | switch (cp_lexer_peek_token (parser->lexer)->keyword) | |
6779 | { | |
6780 | case RID_INLINE: | |
6781 | case RID_VIRTUAL: | |
6782 | case RID_EXPLICIT: | |
6783 | /* Consume the token. */ | |
6784 | return cp_lexer_consume_token (parser->lexer)->value; | |
6785 | ||
6786 | default: | |
6787 | return NULL_TREE; | |
6788 | } | |
6789 | } | |
6790 | ||
6791 | /* Parse a linkage-specification. | |
6792 | ||
6793 | linkage-specification: | |
6794 | extern string-literal { declaration-seq [opt] } | |
6795 | extern string-literal declaration */ | |
6796 | ||
6797 | static void | |
94edc4ab | 6798 | cp_parser_linkage_specification (cp_parser* parser) |
a723baf1 MM |
6799 | { |
6800 | cp_token *token; | |
6801 | tree linkage; | |
6802 | ||
6803 | /* Look for the `extern' keyword. */ | |
6804 | cp_parser_require_keyword (parser, RID_EXTERN, "`extern'"); | |
6805 | ||
6806 | /* Peek at the next token. */ | |
6807 | token = cp_lexer_peek_token (parser->lexer); | |
6808 | /* If it's not a string-literal, then there's a problem. */ | |
6809 | if (!cp_parser_is_string_literal (token)) | |
6810 | { | |
6811 | cp_parser_error (parser, "expected language-name"); | |
6812 | return; | |
6813 | } | |
6814 | /* Consume the token. */ | |
6815 | cp_lexer_consume_token (parser->lexer); | |
6816 | ||
6817 | /* Transform the literal into an identifier. If the literal is a | |
6818 | wide-character string, or contains embedded NULs, then we can't | |
6819 | handle it as the user wants. */ | |
6820 | if (token->type == CPP_WSTRING | |
6821 | || (strlen (TREE_STRING_POINTER (token->value)) | |
6822 | != (size_t) (TREE_STRING_LENGTH (token->value) - 1))) | |
6823 | { | |
6824 | cp_parser_error (parser, "invalid linkage-specification"); | |
6825 | /* Assume C++ linkage. */ | |
6826 | linkage = get_identifier ("c++"); | |
6827 | } | |
6828 | /* If it's a simple string constant, things are easier. */ | |
6829 | else | |
6830 | linkage = get_identifier (TREE_STRING_POINTER (token->value)); | |
6831 | ||
6832 | /* We're now using the new linkage. */ | |
6833 | push_lang_context (linkage); | |
6834 | ||
6835 | /* If the next token is a `{', then we're using the first | |
6836 | production. */ | |
6837 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
6838 | { | |
6839 | /* Consume the `{' token. */ | |
6840 | cp_lexer_consume_token (parser->lexer); | |
6841 | /* Parse the declarations. */ | |
6842 | cp_parser_declaration_seq_opt (parser); | |
6843 | /* Look for the closing `}'. */ | |
6844 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
6845 | } | |
6846 | /* Otherwise, there's just one declaration. */ | |
6847 | else | |
6848 | { | |
6849 | bool saved_in_unbraced_linkage_specification_p; | |
6850 | ||
6851 | saved_in_unbraced_linkage_specification_p | |
6852 | = parser->in_unbraced_linkage_specification_p; | |
6853 | parser->in_unbraced_linkage_specification_p = true; | |
6854 | have_extern_spec = true; | |
6855 | cp_parser_declaration (parser); | |
6856 | have_extern_spec = false; | |
6857 | parser->in_unbraced_linkage_specification_p | |
6858 | = saved_in_unbraced_linkage_specification_p; | |
6859 | } | |
6860 | ||
6861 | /* We're done with the linkage-specification. */ | |
6862 | pop_lang_context (); | |
6863 | } | |
6864 | ||
6865 | /* Special member functions [gram.special] */ | |
6866 | ||
6867 | /* Parse a conversion-function-id. | |
6868 | ||
6869 | conversion-function-id: | |
6870 | operator conversion-type-id | |
6871 | ||
6872 | Returns an IDENTIFIER_NODE representing the operator. */ | |
6873 | ||
6874 | static tree | |
94edc4ab | 6875 | cp_parser_conversion_function_id (cp_parser* parser) |
a723baf1 MM |
6876 | { |
6877 | tree type; | |
6878 | tree saved_scope; | |
6879 | tree saved_qualifying_scope; | |
6880 | tree saved_object_scope; | |
6881 | ||
6882 | /* Look for the `operator' token. */ | |
6883 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'")) | |
6884 | return error_mark_node; | |
6885 | /* When we parse the conversion-type-id, the current scope will be | |
6886 | reset. However, we need that information in able to look up the | |
6887 | conversion function later, so we save it here. */ | |
6888 | saved_scope = parser->scope; | |
6889 | saved_qualifying_scope = parser->qualifying_scope; | |
6890 | saved_object_scope = parser->object_scope; | |
6891 | /* We must enter the scope of the class so that the names of | |
6892 | entities declared within the class are available in the | |
6893 | conversion-type-id. For example, consider: | |
6894 | ||
6895 | struct S { | |
6896 | typedef int I; | |
6897 | operator I(); | |
6898 | }; | |
6899 | ||
6900 | S::operator I() { ... } | |
6901 | ||
6902 | In order to see that `I' is a type-name in the definition, we | |
6903 | must be in the scope of `S'. */ | |
6904 | if (saved_scope) | |
6905 | push_scope (saved_scope); | |
6906 | /* Parse the conversion-type-id. */ | |
6907 | type = cp_parser_conversion_type_id (parser); | |
6908 | /* Leave the scope of the class, if any. */ | |
6909 | if (saved_scope) | |
6910 | pop_scope (saved_scope); | |
6911 | /* Restore the saved scope. */ | |
6912 | parser->scope = saved_scope; | |
6913 | parser->qualifying_scope = saved_qualifying_scope; | |
6914 | parser->object_scope = saved_object_scope; | |
6915 | /* If the TYPE is invalid, indicate failure. */ | |
6916 | if (type == error_mark_node) | |
6917 | return error_mark_node; | |
6918 | return mangle_conv_op_name_for_type (type); | |
6919 | } | |
6920 | ||
6921 | /* Parse a conversion-type-id: | |
6922 | ||
6923 | conversion-type-id: | |
6924 | type-specifier-seq conversion-declarator [opt] | |
6925 | ||
6926 | Returns the TYPE specified. */ | |
6927 | ||
6928 | static tree | |
94edc4ab | 6929 | cp_parser_conversion_type_id (cp_parser* parser) |
a723baf1 MM |
6930 | { |
6931 | tree attributes; | |
6932 | tree type_specifiers; | |
6933 | tree declarator; | |
6934 | ||
6935 | /* Parse the attributes. */ | |
6936 | attributes = cp_parser_attributes_opt (parser); | |
6937 | /* Parse the type-specifiers. */ | |
6938 | type_specifiers = cp_parser_type_specifier_seq (parser); | |
6939 | /* If that didn't work, stop. */ | |
6940 | if (type_specifiers == error_mark_node) | |
6941 | return error_mark_node; | |
6942 | /* Parse the conversion-declarator. */ | |
6943 | declarator = cp_parser_conversion_declarator_opt (parser); | |
6944 | ||
6945 | return grokdeclarator (declarator, type_specifiers, TYPENAME, | |
6946 | /*initialized=*/0, &attributes); | |
6947 | } | |
6948 | ||
6949 | /* Parse an (optional) conversion-declarator. | |
6950 | ||
6951 | conversion-declarator: | |
6952 | ptr-operator conversion-declarator [opt] | |
6953 | ||
6954 | Returns a representation of the declarator. See | |
6955 | cp_parser_declarator for details. */ | |
6956 | ||
6957 | static tree | |
94edc4ab | 6958 | cp_parser_conversion_declarator_opt (cp_parser* parser) |
a723baf1 MM |
6959 | { |
6960 | enum tree_code code; | |
6961 | tree class_type; | |
6962 | tree cv_qualifier_seq; | |
6963 | ||
6964 | /* We don't know if there's a ptr-operator next, or not. */ | |
6965 | cp_parser_parse_tentatively (parser); | |
6966 | /* Try the ptr-operator. */ | |
6967 | code = cp_parser_ptr_operator (parser, &class_type, | |
6968 | &cv_qualifier_seq); | |
6969 | /* If it worked, look for more conversion-declarators. */ | |
6970 | if (cp_parser_parse_definitely (parser)) | |
6971 | { | |
6972 | tree declarator; | |
6973 | ||
6974 | /* Parse another optional declarator. */ | |
6975 | declarator = cp_parser_conversion_declarator_opt (parser); | |
6976 | ||
6977 | /* Create the representation of the declarator. */ | |
6978 | if (code == INDIRECT_REF) | |
6979 | declarator = make_pointer_declarator (cv_qualifier_seq, | |
6980 | declarator); | |
6981 | else | |
6982 | declarator = make_reference_declarator (cv_qualifier_seq, | |
6983 | declarator); | |
6984 | ||
6985 | /* Handle the pointer-to-member case. */ | |
6986 | if (class_type) | |
6987 | declarator = build_nt (SCOPE_REF, class_type, declarator); | |
6988 | ||
6989 | return declarator; | |
6990 | } | |
6991 | ||
6992 | return NULL_TREE; | |
6993 | } | |
6994 | ||
6995 | /* Parse an (optional) ctor-initializer. | |
6996 | ||
6997 | ctor-initializer: | |
6998 | : mem-initializer-list | |
6999 | ||
7000 | Returns TRUE iff the ctor-initializer was actually present. */ | |
7001 | ||
7002 | static bool | |
94edc4ab | 7003 | cp_parser_ctor_initializer_opt (cp_parser* parser) |
a723baf1 MM |
7004 | { |
7005 | /* If the next token is not a `:', then there is no | |
7006 | ctor-initializer. */ | |
7007 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) | |
7008 | { | |
7009 | /* Do default initialization of any bases and members. */ | |
7010 | if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
7011 | finish_mem_initializers (NULL_TREE); | |
7012 | ||
7013 | return false; | |
7014 | } | |
7015 | ||
7016 | /* Consume the `:' token. */ | |
7017 | cp_lexer_consume_token (parser->lexer); | |
7018 | /* And the mem-initializer-list. */ | |
7019 | cp_parser_mem_initializer_list (parser); | |
7020 | ||
7021 | return true; | |
7022 | } | |
7023 | ||
7024 | /* Parse a mem-initializer-list. | |
7025 | ||
7026 | mem-initializer-list: | |
7027 | mem-initializer | |
7028 | mem-initializer , mem-initializer-list */ | |
7029 | ||
7030 | static void | |
94edc4ab | 7031 | cp_parser_mem_initializer_list (cp_parser* parser) |
a723baf1 MM |
7032 | { |
7033 | tree mem_initializer_list = NULL_TREE; | |
7034 | ||
7035 | /* Let the semantic analysis code know that we are starting the | |
7036 | mem-initializer-list. */ | |
0e136342 MM |
7037 | if (!DECL_CONSTRUCTOR_P (current_function_decl)) |
7038 | error ("only constructors take base initializers"); | |
a723baf1 MM |
7039 | |
7040 | /* Loop through the list. */ | |
7041 | while (true) | |
7042 | { | |
7043 | tree mem_initializer; | |
7044 | ||
7045 | /* Parse the mem-initializer. */ | |
7046 | mem_initializer = cp_parser_mem_initializer (parser); | |
7047 | /* Add it to the list, unless it was erroneous. */ | |
7048 | if (mem_initializer) | |
7049 | { | |
7050 | TREE_CHAIN (mem_initializer) = mem_initializer_list; | |
7051 | mem_initializer_list = mem_initializer; | |
7052 | } | |
7053 | /* If the next token is not a `,', we're done. */ | |
7054 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
7055 | break; | |
7056 | /* Consume the `,' token. */ | |
7057 | cp_lexer_consume_token (parser->lexer); | |
7058 | } | |
7059 | ||
7060 | /* Perform semantic analysis. */ | |
0e136342 MM |
7061 | if (DECL_CONSTRUCTOR_P (current_function_decl)) |
7062 | finish_mem_initializers (mem_initializer_list); | |
a723baf1 MM |
7063 | } |
7064 | ||
7065 | /* Parse a mem-initializer. | |
7066 | ||
7067 | mem-initializer: | |
7068 | mem-initializer-id ( expression-list [opt] ) | |
7069 | ||
7070 | GNU extension: | |
7071 | ||
7072 | mem-initializer: | |
34cd5ae7 | 7073 | ( expression-list [opt] ) |
a723baf1 MM |
7074 | |
7075 | Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base | |
7076 | class) or FIELD_DECL (for a non-static data member) to initialize; | |
7077 | the TREE_VALUE is the expression-list. */ | |
7078 | ||
7079 | static tree | |
94edc4ab | 7080 | cp_parser_mem_initializer (cp_parser* parser) |
a723baf1 MM |
7081 | { |
7082 | tree mem_initializer_id; | |
7083 | tree expression_list; | |
1f5a253a NS |
7084 | tree member; |
7085 | ||
a723baf1 MM |
7086 | /* Find out what is being initialized. */ |
7087 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
7088 | { | |
7089 | pedwarn ("anachronistic old-style base class initializer"); | |
7090 | mem_initializer_id = NULL_TREE; | |
7091 | } | |
7092 | else | |
7093 | mem_initializer_id = cp_parser_mem_initializer_id (parser); | |
1f5a253a NS |
7094 | member = expand_member_init (mem_initializer_id); |
7095 | if (member && !DECL_P (member)) | |
7096 | in_base_initializer = 1; | |
7efa3e22 | 7097 | |
39703eb9 MM |
7098 | expression_list |
7099 | = cp_parser_parenthesized_expression_list (parser, false, | |
7100 | /*non_constant_p=*/NULL); | |
7efa3e22 | 7101 | if (!expression_list) |
a723baf1 | 7102 | expression_list = void_type_node; |
a723baf1 | 7103 | |
1f5a253a NS |
7104 | in_base_initializer = 0; |
7105 | ||
7106 | return member ? build_tree_list (member, expression_list) : NULL_TREE; | |
a723baf1 MM |
7107 | } |
7108 | ||
7109 | /* Parse a mem-initializer-id. | |
7110 | ||
7111 | mem-initializer-id: | |
7112 | :: [opt] nested-name-specifier [opt] class-name | |
7113 | identifier | |
7114 | ||
7115 | Returns a TYPE indicating the class to be initializer for the first | |
7116 | production. Returns an IDENTIFIER_NODE indicating the data member | |
7117 | to be initialized for the second production. */ | |
7118 | ||
7119 | static tree | |
94edc4ab | 7120 | cp_parser_mem_initializer_id (cp_parser* parser) |
a723baf1 MM |
7121 | { |
7122 | bool global_scope_p; | |
7123 | bool nested_name_specifier_p; | |
7124 | tree id; | |
7125 | ||
7126 | /* Look for the optional `::' operator. */ | |
7127 | global_scope_p | |
7128 | = (cp_parser_global_scope_opt (parser, | |
7129 | /*current_scope_valid_p=*/false) | |
7130 | != NULL_TREE); | |
7131 | /* Look for the optional nested-name-specifier. The simplest way to | |
7132 | implement: | |
7133 | ||
7134 | [temp.res] | |
7135 | ||
7136 | The keyword `typename' is not permitted in a base-specifier or | |
7137 | mem-initializer; in these contexts a qualified name that | |
7138 | depends on a template-parameter is implicitly assumed to be a | |
7139 | type name. | |
7140 | ||
7141 | is to assume that we have seen the `typename' keyword at this | |
7142 | point. */ | |
7143 | nested_name_specifier_p | |
7144 | = (cp_parser_nested_name_specifier_opt (parser, | |
7145 | /*typename_keyword_p=*/true, | |
7146 | /*check_dependency_p=*/true, | |
a668c6ad MM |
7147 | /*type_p=*/true, |
7148 | /*is_declaration=*/true) | |
a723baf1 MM |
7149 | != NULL_TREE); |
7150 | /* If there is a `::' operator or a nested-name-specifier, then we | |
7151 | are definitely looking for a class-name. */ | |
7152 | if (global_scope_p || nested_name_specifier_p) | |
7153 | return cp_parser_class_name (parser, | |
7154 | /*typename_keyword_p=*/true, | |
7155 | /*template_keyword_p=*/false, | |
7156 | /*type_p=*/false, | |
a723baf1 | 7157 | /*check_dependency_p=*/true, |
a668c6ad MM |
7158 | /*class_head_p=*/false, |
7159 | /*is_declaration=*/true); | |
a723baf1 MM |
7160 | /* Otherwise, we could also be looking for an ordinary identifier. */ |
7161 | cp_parser_parse_tentatively (parser); | |
7162 | /* Try a class-name. */ | |
7163 | id = cp_parser_class_name (parser, | |
7164 | /*typename_keyword_p=*/true, | |
7165 | /*template_keyword_p=*/false, | |
7166 | /*type_p=*/false, | |
a723baf1 | 7167 | /*check_dependency_p=*/true, |
a668c6ad MM |
7168 | /*class_head_p=*/false, |
7169 | /*is_declaration=*/true); | |
a723baf1 MM |
7170 | /* If we found one, we're done. */ |
7171 | if (cp_parser_parse_definitely (parser)) | |
7172 | return id; | |
7173 | /* Otherwise, look for an ordinary identifier. */ | |
7174 | return cp_parser_identifier (parser); | |
7175 | } | |
7176 | ||
7177 | /* Overloading [gram.over] */ | |
7178 | ||
7179 | /* Parse an operator-function-id. | |
7180 | ||
7181 | operator-function-id: | |
7182 | operator operator | |
7183 | ||
7184 | Returns an IDENTIFIER_NODE for the operator which is a | |
7185 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
7186 | ||
7187 | static tree | |
94edc4ab | 7188 | cp_parser_operator_function_id (cp_parser* parser) |
a723baf1 MM |
7189 | { |
7190 | /* Look for the `operator' keyword. */ | |
7191 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'")) | |
7192 | return error_mark_node; | |
7193 | /* And then the name of the operator itself. */ | |
7194 | return cp_parser_operator (parser); | |
7195 | } | |
7196 | ||
7197 | /* Parse an operator. | |
7198 | ||
7199 | operator: | |
7200 | new delete new[] delete[] + - * / % ^ & | ~ ! = < > | |
7201 | += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && | |
7202 | || ++ -- , ->* -> () [] | |
7203 | ||
7204 | GNU Extensions: | |
7205 | ||
7206 | operator: | |
7207 | <? >? <?= >?= | |
7208 | ||
7209 | Returns an IDENTIFIER_NODE for the operator which is a | |
7210 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
7211 | ||
7212 | static tree | |
94edc4ab | 7213 | cp_parser_operator (cp_parser* parser) |
a723baf1 MM |
7214 | { |
7215 | tree id = NULL_TREE; | |
7216 | cp_token *token; | |
7217 | ||
7218 | /* Peek at the next token. */ | |
7219 | token = cp_lexer_peek_token (parser->lexer); | |
7220 | /* Figure out which operator we have. */ | |
7221 | switch (token->type) | |
7222 | { | |
7223 | case CPP_KEYWORD: | |
7224 | { | |
7225 | enum tree_code op; | |
7226 | ||
7227 | /* The keyword should be either `new' or `delete'. */ | |
7228 | if (token->keyword == RID_NEW) | |
7229 | op = NEW_EXPR; | |
7230 | else if (token->keyword == RID_DELETE) | |
7231 | op = DELETE_EXPR; | |
7232 | else | |
7233 | break; | |
7234 | ||
7235 | /* Consume the `new' or `delete' token. */ | |
7236 | cp_lexer_consume_token (parser->lexer); | |
7237 | ||
7238 | /* Peek at the next token. */ | |
7239 | token = cp_lexer_peek_token (parser->lexer); | |
7240 | /* If it's a `[' token then this is the array variant of the | |
7241 | operator. */ | |
7242 | if (token->type == CPP_OPEN_SQUARE) | |
7243 | { | |
7244 | /* Consume the `[' token. */ | |
7245 | cp_lexer_consume_token (parser->lexer); | |
7246 | /* Look for the `]' token. */ | |
7247 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
7248 | id = ansi_opname (op == NEW_EXPR | |
7249 | ? VEC_NEW_EXPR : VEC_DELETE_EXPR); | |
7250 | } | |
7251 | /* Otherwise, we have the non-array variant. */ | |
7252 | else | |
7253 | id = ansi_opname (op); | |
7254 | ||
7255 | return id; | |
7256 | } | |
7257 | ||
7258 | case CPP_PLUS: | |
7259 | id = ansi_opname (PLUS_EXPR); | |
7260 | break; | |
7261 | ||
7262 | case CPP_MINUS: | |
7263 | id = ansi_opname (MINUS_EXPR); | |
7264 | break; | |
7265 | ||
7266 | case CPP_MULT: | |
7267 | id = ansi_opname (MULT_EXPR); | |
7268 | break; | |
7269 | ||
7270 | case CPP_DIV: | |
7271 | id = ansi_opname (TRUNC_DIV_EXPR); | |
7272 | break; | |
7273 | ||
7274 | case CPP_MOD: | |
7275 | id = ansi_opname (TRUNC_MOD_EXPR); | |
7276 | break; | |
7277 | ||
7278 | case CPP_XOR: | |
7279 | id = ansi_opname (BIT_XOR_EXPR); | |
7280 | break; | |
7281 | ||
7282 | case CPP_AND: | |
7283 | id = ansi_opname (BIT_AND_EXPR); | |
7284 | break; | |
7285 | ||
7286 | case CPP_OR: | |
7287 | id = ansi_opname (BIT_IOR_EXPR); | |
7288 | break; | |
7289 | ||
7290 | case CPP_COMPL: | |
7291 | id = ansi_opname (BIT_NOT_EXPR); | |
7292 | break; | |
7293 | ||
7294 | case CPP_NOT: | |
7295 | id = ansi_opname (TRUTH_NOT_EXPR); | |
7296 | break; | |
7297 | ||
7298 | case CPP_EQ: | |
7299 | id = ansi_assopname (NOP_EXPR); | |
7300 | break; | |
7301 | ||
7302 | case CPP_LESS: | |
7303 | id = ansi_opname (LT_EXPR); | |
7304 | break; | |
7305 | ||
7306 | case CPP_GREATER: | |
7307 | id = ansi_opname (GT_EXPR); | |
7308 | break; | |
7309 | ||
7310 | case CPP_PLUS_EQ: | |
7311 | id = ansi_assopname (PLUS_EXPR); | |
7312 | break; | |
7313 | ||
7314 | case CPP_MINUS_EQ: | |
7315 | id = ansi_assopname (MINUS_EXPR); | |
7316 | break; | |
7317 | ||
7318 | case CPP_MULT_EQ: | |
7319 | id = ansi_assopname (MULT_EXPR); | |
7320 | break; | |
7321 | ||
7322 | case CPP_DIV_EQ: | |
7323 | id = ansi_assopname (TRUNC_DIV_EXPR); | |
7324 | break; | |
7325 | ||
7326 | case CPP_MOD_EQ: | |
7327 | id = ansi_assopname (TRUNC_MOD_EXPR); | |
7328 | break; | |
7329 | ||
7330 | case CPP_XOR_EQ: | |
7331 | id = ansi_assopname (BIT_XOR_EXPR); | |
7332 | break; | |
7333 | ||
7334 | case CPP_AND_EQ: | |
7335 | id = ansi_assopname (BIT_AND_EXPR); | |
7336 | break; | |
7337 | ||
7338 | case CPP_OR_EQ: | |
7339 | id = ansi_assopname (BIT_IOR_EXPR); | |
7340 | break; | |
7341 | ||
7342 | case CPP_LSHIFT: | |
7343 | id = ansi_opname (LSHIFT_EXPR); | |
7344 | break; | |
7345 | ||
7346 | case CPP_RSHIFT: | |
7347 | id = ansi_opname (RSHIFT_EXPR); | |
7348 | break; | |
7349 | ||
7350 | case CPP_LSHIFT_EQ: | |
7351 | id = ansi_assopname (LSHIFT_EXPR); | |
7352 | break; | |
7353 | ||
7354 | case CPP_RSHIFT_EQ: | |
7355 | id = ansi_assopname (RSHIFT_EXPR); | |
7356 | break; | |
7357 | ||
7358 | case CPP_EQ_EQ: | |
7359 | id = ansi_opname (EQ_EXPR); | |
7360 | break; | |
7361 | ||
7362 | case CPP_NOT_EQ: | |
7363 | id = ansi_opname (NE_EXPR); | |
7364 | break; | |
7365 | ||
7366 | case CPP_LESS_EQ: | |
7367 | id = ansi_opname (LE_EXPR); | |
7368 | break; | |
7369 | ||
7370 | case CPP_GREATER_EQ: | |
7371 | id = ansi_opname (GE_EXPR); | |
7372 | break; | |
7373 | ||
7374 | case CPP_AND_AND: | |
7375 | id = ansi_opname (TRUTH_ANDIF_EXPR); | |
7376 | break; | |
7377 | ||
7378 | case CPP_OR_OR: | |
7379 | id = ansi_opname (TRUTH_ORIF_EXPR); | |
7380 | break; | |
7381 | ||
7382 | case CPP_PLUS_PLUS: | |
7383 | id = ansi_opname (POSTINCREMENT_EXPR); | |
7384 | break; | |
7385 | ||
7386 | case CPP_MINUS_MINUS: | |
7387 | id = ansi_opname (PREDECREMENT_EXPR); | |
7388 | break; | |
7389 | ||
7390 | case CPP_COMMA: | |
7391 | id = ansi_opname (COMPOUND_EXPR); | |
7392 | break; | |
7393 | ||
7394 | case CPP_DEREF_STAR: | |
7395 | id = ansi_opname (MEMBER_REF); | |
7396 | break; | |
7397 | ||
7398 | case CPP_DEREF: | |
7399 | id = ansi_opname (COMPONENT_REF); | |
7400 | break; | |
7401 | ||
7402 | case CPP_OPEN_PAREN: | |
7403 | /* Consume the `('. */ | |
7404 | cp_lexer_consume_token (parser->lexer); | |
7405 | /* Look for the matching `)'. */ | |
7406 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
7407 | return ansi_opname (CALL_EXPR); | |
7408 | ||
7409 | case CPP_OPEN_SQUARE: | |
7410 | /* Consume the `['. */ | |
7411 | cp_lexer_consume_token (parser->lexer); | |
7412 | /* Look for the matching `]'. */ | |
7413 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
7414 | return ansi_opname (ARRAY_REF); | |
7415 | ||
7416 | /* Extensions. */ | |
7417 | case CPP_MIN: | |
7418 | id = ansi_opname (MIN_EXPR); | |
7419 | break; | |
7420 | ||
7421 | case CPP_MAX: | |
7422 | id = ansi_opname (MAX_EXPR); | |
7423 | break; | |
7424 | ||
7425 | case CPP_MIN_EQ: | |
7426 | id = ansi_assopname (MIN_EXPR); | |
7427 | break; | |
7428 | ||
7429 | case CPP_MAX_EQ: | |
7430 | id = ansi_assopname (MAX_EXPR); | |
7431 | break; | |
7432 | ||
7433 | default: | |
7434 | /* Anything else is an error. */ | |
7435 | break; | |
7436 | } | |
7437 | ||
7438 | /* If we have selected an identifier, we need to consume the | |
7439 | operator token. */ | |
7440 | if (id) | |
7441 | cp_lexer_consume_token (parser->lexer); | |
7442 | /* Otherwise, no valid operator name was present. */ | |
7443 | else | |
7444 | { | |
7445 | cp_parser_error (parser, "expected operator"); | |
7446 | id = error_mark_node; | |
7447 | } | |
7448 | ||
7449 | return id; | |
7450 | } | |
7451 | ||
7452 | /* Parse a template-declaration. | |
7453 | ||
7454 | template-declaration: | |
7455 | export [opt] template < template-parameter-list > declaration | |
7456 | ||
7457 | If MEMBER_P is TRUE, this template-declaration occurs within a | |
7458 | class-specifier. | |
7459 | ||
7460 | The grammar rule given by the standard isn't correct. What | |
7461 | is really meant is: | |
7462 | ||
7463 | template-declaration: | |
7464 | export [opt] template-parameter-list-seq | |
7465 | decl-specifier-seq [opt] init-declarator [opt] ; | |
7466 | export [opt] template-parameter-list-seq | |
7467 | function-definition | |
7468 | ||
7469 | template-parameter-list-seq: | |
7470 | template-parameter-list-seq [opt] | |
7471 | template < template-parameter-list > */ | |
7472 | ||
7473 | static void | |
94edc4ab | 7474 | cp_parser_template_declaration (cp_parser* parser, bool member_p) |
a723baf1 MM |
7475 | { |
7476 | /* Check for `export'. */ | |
7477 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT)) | |
7478 | { | |
7479 | /* Consume the `export' token. */ | |
7480 | cp_lexer_consume_token (parser->lexer); | |
7481 | /* Warn that we do not support `export'. */ | |
7482 | warning ("keyword `export' not implemented, and will be ignored"); | |
7483 | } | |
7484 | ||
7485 | cp_parser_template_declaration_after_export (parser, member_p); | |
7486 | } | |
7487 | ||
7488 | /* Parse a template-parameter-list. | |
7489 | ||
7490 | template-parameter-list: | |
7491 | template-parameter | |
7492 | template-parameter-list , template-parameter | |
7493 | ||
7494 | Returns a TREE_LIST. Each node represents a template parameter. | |
7495 | The nodes are connected via their TREE_CHAINs. */ | |
7496 | ||
7497 | static tree | |
94edc4ab | 7498 | cp_parser_template_parameter_list (cp_parser* parser) |
a723baf1 MM |
7499 | { |
7500 | tree parameter_list = NULL_TREE; | |
7501 | ||
7502 | while (true) | |
7503 | { | |
7504 | tree parameter; | |
7505 | cp_token *token; | |
7506 | ||
7507 | /* Parse the template-parameter. */ | |
7508 | parameter = cp_parser_template_parameter (parser); | |
7509 | /* Add it to the list. */ | |
7510 | parameter_list = process_template_parm (parameter_list, | |
7511 | parameter); | |
7512 | ||
7513 | /* Peek at the next token. */ | |
7514 | token = cp_lexer_peek_token (parser->lexer); | |
7515 | /* If it's not a `,', we're done. */ | |
7516 | if (token->type != CPP_COMMA) | |
7517 | break; | |
7518 | /* Otherwise, consume the `,' token. */ | |
7519 | cp_lexer_consume_token (parser->lexer); | |
7520 | } | |
7521 | ||
7522 | return parameter_list; | |
7523 | } | |
7524 | ||
7525 | /* Parse a template-parameter. | |
7526 | ||
7527 | template-parameter: | |
7528 | type-parameter | |
7529 | parameter-declaration | |
7530 | ||
7531 | Returns a TREE_LIST. The TREE_VALUE represents the parameter. The | |
7532 | TREE_PURPOSE is the default value, if any. */ | |
7533 | ||
7534 | static tree | |
94edc4ab | 7535 | cp_parser_template_parameter (cp_parser* parser) |
a723baf1 MM |
7536 | { |
7537 | cp_token *token; | |
7538 | ||
7539 | /* Peek at the next token. */ | |
7540 | token = cp_lexer_peek_token (parser->lexer); | |
7541 | /* If it is `class' or `template', we have a type-parameter. */ | |
7542 | if (token->keyword == RID_TEMPLATE) | |
7543 | return cp_parser_type_parameter (parser); | |
7544 | /* If it is `class' or `typename' we do not know yet whether it is a | |
7545 | type parameter or a non-type parameter. Consider: | |
7546 | ||
7547 | template <typename T, typename T::X X> ... | |
7548 | ||
7549 | or: | |
7550 | ||
7551 | template <class C, class D*> ... | |
7552 | ||
7553 | Here, the first parameter is a type parameter, and the second is | |
7554 | a non-type parameter. We can tell by looking at the token after | |
7555 | the identifier -- if it is a `,', `=', or `>' then we have a type | |
7556 | parameter. */ | |
7557 | if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS) | |
7558 | { | |
7559 | /* Peek at the token after `class' or `typename'. */ | |
7560 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
7561 | /* If it's an identifier, skip it. */ | |
7562 | if (token->type == CPP_NAME) | |
7563 | token = cp_lexer_peek_nth_token (parser->lexer, 3); | |
7564 | /* Now, see if the token looks like the end of a template | |
7565 | parameter. */ | |
7566 | if (token->type == CPP_COMMA | |
7567 | || token->type == CPP_EQ | |
7568 | || token->type == CPP_GREATER) | |
7569 | return cp_parser_type_parameter (parser); | |
7570 | } | |
7571 | ||
7572 | /* Otherwise, it is a non-type parameter. | |
7573 | ||
7574 | [temp.param] | |
7575 | ||
7576 | When parsing a default template-argument for a non-type | |
7577 | template-parameter, the first non-nested `>' is taken as the end | |
7578 | of the template parameter-list rather than a greater-than | |
7579 | operator. */ | |
7580 | return | |
4bb8ca28 MM |
7581 | cp_parser_parameter_declaration (parser, /*template_parm_p=*/true, |
7582 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
7583 | } |
7584 | ||
7585 | /* Parse a type-parameter. | |
7586 | ||
7587 | type-parameter: | |
7588 | class identifier [opt] | |
7589 | class identifier [opt] = type-id | |
7590 | typename identifier [opt] | |
7591 | typename identifier [opt] = type-id | |
7592 | template < template-parameter-list > class identifier [opt] | |
7593 | template < template-parameter-list > class identifier [opt] | |
7594 | = id-expression | |
7595 | ||
7596 | Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The | |
7597 | TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is | |
7598 | the declaration of the parameter. */ | |
7599 | ||
7600 | static tree | |
94edc4ab | 7601 | cp_parser_type_parameter (cp_parser* parser) |
a723baf1 MM |
7602 | { |
7603 | cp_token *token; | |
7604 | tree parameter; | |
7605 | ||
7606 | /* Look for a keyword to tell us what kind of parameter this is. */ | |
7607 | token = cp_parser_require (parser, CPP_KEYWORD, | |
8a6393df | 7608 | "`class', `typename', or `template'"); |
a723baf1 MM |
7609 | if (!token) |
7610 | return error_mark_node; | |
7611 | ||
7612 | switch (token->keyword) | |
7613 | { | |
7614 | case RID_CLASS: | |
7615 | case RID_TYPENAME: | |
7616 | { | |
7617 | tree identifier; | |
7618 | tree default_argument; | |
7619 | ||
7620 | /* If the next token is an identifier, then it names the | |
7621 | parameter. */ | |
7622 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
7623 | identifier = cp_parser_identifier (parser); | |
7624 | else | |
7625 | identifier = NULL_TREE; | |
7626 | ||
7627 | /* Create the parameter. */ | |
7628 | parameter = finish_template_type_parm (class_type_node, identifier); | |
7629 | ||
7630 | /* If the next token is an `=', we have a default argument. */ | |
7631 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
7632 | { | |
7633 | /* Consume the `=' token. */ | |
7634 | cp_lexer_consume_token (parser->lexer); | |
34cd5ae7 | 7635 | /* Parse the default-argument. */ |
a723baf1 MM |
7636 | default_argument = cp_parser_type_id (parser); |
7637 | } | |
7638 | else | |
7639 | default_argument = NULL_TREE; | |
7640 | ||
7641 | /* Create the combined representation of the parameter and the | |
7642 | default argument. */ | |
c67d36d0 | 7643 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
7644 | } |
7645 | break; | |
7646 | ||
7647 | case RID_TEMPLATE: | |
7648 | { | |
7649 | tree parameter_list; | |
7650 | tree identifier; | |
7651 | tree default_argument; | |
7652 | ||
7653 | /* Look for the `<'. */ | |
7654 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
7655 | /* Parse the template-parameter-list. */ | |
7656 | begin_template_parm_list (); | |
7657 | parameter_list | |
7658 | = cp_parser_template_parameter_list (parser); | |
7659 | parameter_list = end_template_parm_list (parameter_list); | |
7660 | /* Look for the `>'. */ | |
7661 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
7662 | /* Look for the `class' keyword. */ | |
7663 | cp_parser_require_keyword (parser, RID_CLASS, "`class'"); | |
7664 | /* If the next token is an `=', then there is a | |
7665 | default-argument. If the next token is a `>', we are at | |
7666 | the end of the parameter-list. If the next token is a `,', | |
7667 | then we are at the end of this parameter. */ | |
7668 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ) | |
7669 | && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER) | |
7670 | && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
7671 | identifier = cp_parser_identifier (parser); | |
7672 | else | |
7673 | identifier = NULL_TREE; | |
7674 | /* Create the template parameter. */ | |
7675 | parameter = finish_template_template_parm (class_type_node, | |
7676 | identifier); | |
7677 | ||
7678 | /* If the next token is an `=', then there is a | |
7679 | default-argument. */ | |
7680 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
7681 | { | |
b0bc6e8e KL |
7682 | bool is_template; |
7683 | ||
a723baf1 MM |
7684 | /* Consume the `='. */ |
7685 | cp_lexer_consume_token (parser->lexer); | |
7686 | /* Parse the id-expression. */ | |
7687 | default_argument | |
7688 | = cp_parser_id_expression (parser, | |
7689 | /*template_keyword_p=*/false, | |
7690 | /*check_dependency_p=*/true, | |
b0bc6e8e | 7691 | /*template_p=*/&is_template, |
f3c2dfc6 | 7692 | /*declarator_p=*/false); |
a723baf1 MM |
7693 | /* Look up the name. */ |
7694 | default_argument | |
b0bc6e8e KL |
7695 | = cp_parser_lookup_name (parser, default_argument, |
7696 | /*is_type=*/false, | |
7697 | /*is_template=*/is_template, | |
7698 | /*is_namespace=*/false, | |
7699 | /*check_dependency=*/true); | |
a723baf1 MM |
7700 | /* See if the default argument is valid. */ |
7701 | default_argument | |
7702 | = check_template_template_default_arg (default_argument); | |
7703 | } | |
7704 | else | |
7705 | default_argument = NULL_TREE; | |
7706 | ||
7707 | /* Create the combined representation of the parameter and the | |
7708 | default argument. */ | |
c67d36d0 | 7709 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
7710 | } |
7711 | break; | |
7712 | ||
7713 | default: | |
7714 | /* Anything else is an error. */ | |
7715 | cp_parser_error (parser, | |
7716 | "expected `class', `typename', or `template'"); | |
7717 | parameter = error_mark_node; | |
7718 | } | |
7719 | ||
7720 | return parameter; | |
7721 | } | |
7722 | ||
7723 | /* Parse a template-id. | |
7724 | ||
7725 | template-id: | |
7726 | template-name < template-argument-list [opt] > | |
7727 | ||
7728 | If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the | |
7729 | `template' keyword. In this case, a TEMPLATE_ID_EXPR will be | |
7730 | returned. Otherwise, if the template-name names a function, or set | |
7731 | of functions, returns a TEMPLATE_ID_EXPR. If the template-name | |
7732 | names a class, returns a TYPE_DECL for the specialization. | |
7733 | ||
7734 | If CHECK_DEPENDENCY_P is FALSE, names are looked up in | |
7735 | uninstantiated templates. */ | |
7736 | ||
7737 | static tree | |
7738 | cp_parser_template_id (cp_parser *parser, | |
7739 | bool template_keyword_p, | |
a668c6ad MM |
7740 | bool check_dependency_p, |
7741 | bool is_declaration) | |
a723baf1 MM |
7742 | { |
7743 | tree template; | |
7744 | tree arguments; | |
a723baf1 | 7745 | tree template_id; |
a723baf1 MM |
7746 | ptrdiff_t start_of_id; |
7747 | tree access_check = NULL_TREE; | |
2050a1bb | 7748 | cp_token *next_token; |
a668c6ad | 7749 | bool is_identifier; |
a723baf1 MM |
7750 | |
7751 | /* If the next token corresponds to a template-id, there is no need | |
7752 | to reparse it. */ | |
2050a1bb MM |
7753 | next_token = cp_lexer_peek_token (parser->lexer); |
7754 | if (next_token->type == CPP_TEMPLATE_ID) | |
a723baf1 MM |
7755 | { |
7756 | tree value; | |
7757 | tree check; | |
7758 | ||
7759 | /* Get the stored value. */ | |
7760 | value = cp_lexer_consume_token (parser->lexer)->value; | |
7761 | /* Perform any access checks that were deferred. */ | |
7762 | for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check)) | |
cf22909c KL |
7763 | perform_or_defer_access_check (TREE_PURPOSE (check), |
7764 | TREE_VALUE (check)); | |
a723baf1 MM |
7765 | /* Return the stored value. */ |
7766 | return TREE_VALUE (value); | |
7767 | } | |
7768 | ||
2050a1bb MM |
7769 | /* Avoid performing name lookup if there is no possibility of |
7770 | finding a template-id. */ | |
7771 | if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR) | |
7772 | || (next_token->type == CPP_NAME | |
7773 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)) | |
7774 | { | |
7775 | cp_parser_error (parser, "expected template-id"); | |
7776 | return error_mark_node; | |
7777 | } | |
7778 | ||
a723baf1 MM |
7779 | /* Remember where the template-id starts. */ |
7780 | if (cp_parser_parsing_tentatively (parser) | |
7781 | && !cp_parser_committed_to_tentative_parse (parser)) | |
7782 | { | |
2050a1bb | 7783 | next_token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
7784 | start_of_id = cp_lexer_token_difference (parser->lexer, |
7785 | parser->lexer->first_token, | |
7786 | next_token); | |
a723baf1 MM |
7787 | } |
7788 | else | |
7789 | start_of_id = -1; | |
7790 | ||
8d241e0b | 7791 | push_deferring_access_checks (dk_deferred); |
cf22909c | 7792 | |
a723baf1 | 7793 | /* Parse the template-name. */ |
a668c6ad | 7794 | is_identifier = false; |
a723baf1 | 7795 | template = cp_parser_template_name (parser, template_keyword_p, |
a668c6ad MM |
7796 | check_dependency_p, |
7797 | is_declaration, | |
7798 | &is_identifier); | |
7799 | if (template == error_mark_node || is_identifier) | |
cf22909c KL |
7800 | { |
7801 | pop_deferring_access_checks (); | |
a668c6ad | 7802 | return template; |
cf22909c | 7803 | } |
a723baf1 MM |
7804 | |
7805 | /* Look for the `<' that starts the template-argument-list. */ | |
7806 | if (!cp_parser_require (parser, CPP_LESS, "`<'")) | |
cf22909c KL |
7807 | { |
7808 | pop_deferring_access_checks (); | |
7809 | return error_mark_node; | |
7810 | } | |
a723baf1 | 7811 | |
ec75414f MM |
7812 | /* Parse the arguments. */ |
7813 | arguments = cp_parser_enclosed_template_argument_list (parser); | |
a723baf1 MM |
7814 | |
7815 | /* Build a representation of the specialization. */ | |
7816 | if (TREE_CODE (template) == IDENTIFIER_NODE) | |
7817 | template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments); | |
7818 | else if (DECL_CLASS_TEMPLATE_P (template) | |
7819 | || DECL_TEMPLATE_TEMPLATE_PARM_P (template)) | |
7820 | template_id | |
7821 | = finish_template_type (template, arguments, | |
7822 | cp_lexer_next_token_is (parser->lexer, | |
7823 | CPP_SCOPE)); | |
7824 | else | |
7825 | { | |
7826 | /* If it's not a class-template or a template-template, it should be | |
7827 | a function-template. */ | |
7828 | my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template) | |
7829 | || TREE_CODE (template) == OVERLOAD | |
7830 | || BASELINK_P (template)), | |
7831 | 20010716); | |
7832 | ||
7833 | template_id = lookup_template_function (template, arguments); | |
7834 | } | |
7835 | ||
cf22909c KL |
7836 | /* Retrieve any deferred checks. Do not pop this access checks yet |
7837 | so the memory will not be reclaimed during token replacing below. */ | |
7838 | access_check = get_deferred_access_checks (); | |
7839 | ||
a723baf1 MM |
7840 | /* If parsing tentatively, replace the sequence of tokens that makes |
7841 | up the template-id with a CPP_TEMPLATE_ID token. That way, | |
7842 | should we re-parse the token stream, we will not have to repeat | |
7843 | the effort required to do the parse, nor will we issue duplicate | |
7844 | error messages about problems during instantiation of the | |
7845 | template. */ | |
7846 | if (start_of_id >= 0) | |
7847 | { | |
7848 | cp_token *token; | |
a723baf1 MM |
7849 | |
7850 | /* Find the token that corresponds to the start of the | |
7851 | template-id. */ | |
7852 | token = cp_lexer_advance_token (parser->lexer, | |
7853 | parser->lexer->first_token, | |
7854 | start_of_id); | |
7855 | ||
a723baf1 MM |
7856 | /* Reset the contents of the START_OF_ID token. */ |
7857 | token->type = CPP_TEMPLATE_ID; | |
7858 | token->value = build_tree_list (access_check, template_id); | |
7859 | token->keyword = RID_MAX; | |
7860 | /* Purge all subsequent tokens. */ | |
7861 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
7862 | } | |
7863 | ||
cf22909c | 7864 | pop_deferring_access_checks (); |
a723baf1 MM |
7865 | return template_id; |
7866 | } | |
7867 | ||
7868 | /* Parse a template-name. | |
7869 | ||
7870 | template-name: | |
7871 | identifier | |
7872 | ||
7873 | The standard should actually say: | |
7874 | ||
7875 | template-name: | |
7876 | identifier | |
7877 | operator-function-id | |
7878 | conversion-function-id | |
7879 | ||
7880 | A defect report has been filed about this issue. | |
7881 | ||
7882 | If TEMPLATE_KEYWORD_P is true, then we have just seen the | |
7883 | `template' keyword, in a construction like: | |
7884 | ||
7885 | T::template f<3>() | |
7886 | ||
7887 | In that case `f' is taken to be a template-name, even though there | |
7888 | is no way of knowing for sure. | |
7889 | ||
7890 | Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the | |
7891 | name refers to a set of overloaded functions, at least one of which | |
7892 | is a template, or an IDENTIFIER_NODE with the name of the template, | |
7893 | if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE, | |
7894 | names are looked up inside uninstantiated templates. */ | |
7895 | ||
7896 | static tree | |
94edc4ab NN |
7897 | cp_parser_template_name (cp_parser* parser, |
7898 | bool template_keyword_p, | |
a668c6ad MM |
7899 | bool check_dependency_p, |
7900 | bool is_declaration, | |
7901 | bool *is_identifier) | |
a723baf1 MM |
7902 | { |
7903 | tree identifier; | |
7904 | tree decl; | |
7905 | tree fns; | |
7906 | ||
7907 | /* If the next token is `operator', then we have either an | |
7908 | operator-function-id or a conversion-function-id. */ | |
7909 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR)) | |
7910 | { | |
7911 | /* We don't know whether we're looking at an | |
7912 | operator-function-id or a conversion-function-id. */ | |
7913 | cp_parser_parse_tentatively (parser); | |
7914 | /* Try an operator-function-id. */ | |
7915 | identifier = cp_parser_operator_function_id (parser); | |
7916 | /* If that didn't work, try a conversion-function-id. */ | |
7917 | if (!cp_parser_parse_definitely (parser)) | |
7918 | identifier = cp_parser_conversion_function_id (parser); | |
7919 | } | |
7920 | /* Look for the identifier. */ | |
7921 | else | |
7922 | identifier = cp_parser_identifier (parser); | |
7923 | ||
7924 | /* If we didn't find an identifier, we don't have a template-id. */ | |
7925 | if (identifier == error_mark_node) | |
7926 | return error_mark_node; | |
7927 | ||
7928 | /* If the name immediately followed the `template' keyword, then it | |
7929 | is a template-name. However, if the next token is not `<', then | |
7930 | we do not treat it as a template-name, since it is not being used | |
7931 | as part of a template-id. This enables us to handle constructs | |
7932 | like: | |
7933 | ||
7934 | template <typename T> struct S { S(); }; | |
7935 | template <typename T> S<T>::S(); | |
7936 | ||
7937 | correctly. We would treat `S' as a template -- if it were `S<T>' | |
7938 | -- but we do not if there is no `<'. */ | |
a668c6ad MM |
7939 | |
7940 | if (processing_template_decl | |
a723baf1 | 7941 | && cp_lexer_next_token_is (parser->lexer, CPP_LESS)) |
a668c6ad MM |
7942 | { |
7943 | /* In a declaration, in a dependent context, we pretend that the | |
7944 | "template" keyword was present in order to improve error | |
7945 | recovery. For example, given: | |
7946 | ||
7947 | template <typename T> void f(T::X<int>); | |
7948 | ||
7949 | we want to treat "X<int>" as a template-id. */ | |
7950 | if (is_declaration | |
7951 | && !template_keyword_p | |
7952 | && parser->scope && TYPE_P (parser->scope) | |
7953 | && dependent_type_p (parser->scope)) | |
7954 | { | |
7955 | ptrdiff_t start; | |
7956 | cp_token* token; | |
7957 | /* Explain what went wrong. */ | |
7958 | error ("non-template `%D' used as template", identifier); | |
7959 | error ("(use `%T::template %D' to indicate that it is a template)", | |
7960 | parser->scope, identifier); | |
7961 | /* If parsing tentatively, find the location of the "<" | |
7962 | token. */ | |
7963 | if (cp_parser_parsing_tentatively (parser) | |
7964 | && !cp_parser_committed_to_tentative_parse (parser)) | |
7965 | { | |
7966 | cp_parser_simulate_error (parser); | |
7967 | token = cp_lexer_peek_token (parser->lexer); | |
7968 | token = cp_lexer_prev_token (parser->lexer, token); | |
7969 | start = cp_lexer_token_difference (parser->lexer, | |
7970 | parser->lexer->first_token, | |
7971 | token); | |
7972 | } | |
7973 | else | |
7974 | start = -1; | |
7975 | /* Parse the template arguments so that we can issue error | |
7976 | messages about them. */ | |
7977 | cp_lexer_consume_token (parser->lexer); | |
7978 | cp_parser_enclosed_template_argument_list (parser); | |
7979 | /* Skip tokens until we find a good place from which to | |
7980 | continue parsing. */ | |
7981 | cp_parser_skip_to_closing_parenthesis (parser, | |
7982 | /*recovering=*/true, | |
7983 | /*or_comma=*/true, | |
7984 | /*consume_paren=*/false); | |
7985 | /* If parsing tentatively, permanently remove the | |
7986 | template argument list. That will prevent duplicate | |
7987 | error messages from being issued about the missing | |
7988 | "template" keyword. */ | |
7989 | if (start >= 0) | |
7990 | { | |
7991 | token = cp_lexer_advance_token (parser->lexer, | |
7992 | parser->lexer->first_token, | |
7993 | start); | |
7994 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
7995 | } | |
7996 | if (is_identifier) | |
7997 | *is_identifier = true; | |
7998 | return identifier; | |
7999 | } | |
8000 | if (template_keyword_p) | |
8001 | return identifier; | |
8002 | } | |
a723baf1 MM |
8003 | |
8004 | /* Look up the name. */ | |
8005 | decl = cp_parser_lookup_name (parser, identifier, | |
a723baf1 | 8006 | /*is_type=*/false, |
b0bc6e8e | 8007 | /*is_template=*/false, |
eea9800f | 8008 | /*is_namespace=*/false, |
a723baf1 MM |
8009 | check_dependency_p); |
8010 | decl = maybe_get_template_decl_from_type_decl (decl); | |
8011 | ||
8012 | /* If DECL is a template, then the name was a template-name. */ | |
8013 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
8014 | ; | |
8015 | else | |
8016 | { | |
8017 | /* The standard does not explicitly indicate whether a name that | |
8018 | names a set of overloaded declarations, some of which are | |
8019 | templates, is a template-name. However, such a name should | |
8020 | be a template-name; otherwise, there is no way to form a | |
8021 | template-id for the overloaded templates. */ | |
8022 | fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl; | |
8023 | if (TREE_CODE (fns) == OVERLOAD) | |
8024 | { | |
8025 | tree fn; | |
8026 | ||
8027 | for (fn = fns; fn; fn = OVL_NEXT (fn)) | |
8028 | if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL) | |
8029 | break; | |
8030 | } | |
8031 | else | |
8032 | { | |
8033 | /* Otherwise, the name does not name a template. */ | |
8034 | cp_parser_error (parser, "expected template-name"); | |
8035 | return error_mark_node; | |
8036 | } | |
8037 | } | |
8038 | ||
8039 | /* If DECL is dependent, and refers to a function, then just return | |
8040 | its name; we will look it up again during template instantiation. */ | |
8041 | if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl)) | |
8042 | { | |
8043 | tree scope = CP_DECL_CONTEXT (get_first_fn (decl)); | |
1fb3244a | 8044 | if (TYPE_P (scope) && dependent_type_p (scope)) |
a723baf1 MM |
8045 | return identifier; |
8046 | } | |
8047 | ||
8048 | return decl; | |
8049 | } | |
8050 | ||
8051 | /* Parse a template-argument-list. | |
8052 | ||
8053 | template-argument-list: | |
8054 | template-argument | |
8055 | template-argument-list , template-argument | |
8056 | ||
04c06002 | 8057 | Returns a TREE_VEC containing the arguments. */ |
a723baf1 MM |
8058 | |
8059 | static tree | |
94edc4ab | 8060 | cp_parser_template_argument_list (cp_parser* parser) |
a723baf1 | 8061 | { |
bf12d54d NS |
8062 | tree fixed_args[10]; |
8063 | unsigned n_args = 0; | |
8064 | unsigned alloced = 10; | |
8065 | tree *arg_ary = fixed_args; | |
8066 | tree vec; | |
4bb8ca28 | 8067 | bool saved_in_template_argument_list_p; |
a723baf1 | 8068 | |
4bb8ca28 MM |
8069 | saved_in_template_argument_list_p = parser->in_template_argument_list_p; |
8070 | parser->in_template_argument_list_p = true; | |
bf12d54d | 8071 | do |
a723baf1 MM |
8072 | { |
8073 | tree argument; | |
8074 | ||
bf12d54d | 8075 | if (n_args) |
04c06002 | 8076 | /* Consume the comma. */ |
bf12d54d NS |
8077 | cp_lexer_consume_token (parser->lexer); |
8078 | ||
a723baf1 MM |
8079 | /* Parse the template-argument. */ |
8080 | argument = cp_parser_template_argument (parser); | |
bf12d54d NS |
8081 | if (n_args == alloced) |
8082 | { | |
8083 | alloced *= 2; | |
8084 | ||
8085 | if (arg_ary == fixed_args) | |
8086 | { | |
8087 | arg_ary = xmalloc (sizeof (tree) * alloced); | |
8088 | memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); | |
8089 | } | |
8090 | else | |
8091 | arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced); | |
8092 | } | |
8093 | arg_ary[n_args++] = argument; | |
a723baf1 | 8094 | } |
bf12d54d NS |
8095 | while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); |
8096 | ||
8097 | vec = make_tree_vec (n_args); | |
a723baf1 | 8098 | |
bf12d54d NS |
8099 | while (n_args--) |
8100 | TREE_VEC_ELT (vec, n_args) = arg_ary[n_args]; | |
8101 | ||
8102 | if (arg_ary != fixed_args) | |
8103 | free (arg_ary); | |
4bb8ca28 | 8104 | parser->in_template_argument_list_p = saved_in_template_argument_list_p; |
bf12d54d | 8105 | return vec; |
a723baf1 MM |
8106 | } |
8107 | ||
8108 | /* Parse a template-argument. | |
8109 | ||
8110 | template-argument: | |
8111 | assignment-expression | |
8112 | type-id | |
8113 | id-expression | |
8114 | ||
8115 | The representation is that of an assignment-expression, type-id, or | |
8116 | id-expression -- except that the qualified id-expression is | |
8117 | evaluated, so that the value returned is either a DECL or an | |
d17811fd MM |
8118 | OVERLOAD. |
8119 | ||
8120 | Although the standard says "assignment-expression", it forbids | |
8121 | throw-expressions or assignments in the template argument. | |
8122 | Therefore, we use "conditional-expression" instead. */ | |
a723baf1 MM |
8123 | |
8124 | static tree | |
94edc4ab | 8125 | cp_parser_template_argument (cp_parser* parser) |
a723baf1 MM |
8126 | { |
8127 | tree argument; | |
8128 | bool template_p; | |
d17811fd | 8129 | bool address_p; |
4d5297fa | 8130 | bool maybe_type_id = false; |
d17811fd | 8131 | cp_token *token; |
b3445994 | 8132 | cp_id_kind idk; |
d17811fd | 8133 | tree qualifying_class; |
a723baf1 MM |
8134 | |
8135 | /* There's really no way to know what we're looking at, so we just | |
8136 | try each alternative in order. | |
8137 | ||
8138 | [temp.arg] | |
8139 | ||
8140 | In a template-argument, an ambiguity between a type-id and an | |
8141 | expression is resolved to a type-id, regardless of the form of | |
8142 | the corresponding template-parameter. | |
8143 | ||
8144 | Therefore, we try a type-id first. */ | |
8145 | cp_parser_parse_tentatively (parser); | |
a723baf1 | 8146 | argument = cp_parser_type_id (parser); |
4d5297fa GB |
8147 | /* If there was no error parsing the type-id but the next token is a '>>', |
8148 | we probably found a typo for '> >'. But there are type-id which are | |
8149 | also valid expressions. For instance: | |
8150 | ||
8151 | struct X { int operator >> (int); }; | |
8152 | template <int V> struct Foo {}; | |
8153 | Foo<X () >> 5> r; | |
8154 | ||
8155 | Here 'X()' is a valid type-id of a function type, but the user just | |
8156 | wanted to write the expression "X() >> 5". Thus, we remember that we | |
8157 | found a valid type-id, but we still try to parse the argument as an | |
8158 | expression to see what happens. */ | |
8159 | if (!cp_parser_error_occurred (parser) | |
8160 | && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) | |
8161 | { | |
8162 | maybe_type_id = true; | |
8163 | cp_parser_abort_tentative_parse (parser); | |
8164 | } | |
8165 | else | |
8166 | { | |
8167 | /* If the next token isn't a `,' or a `>', then this argument wasn't | |
8168 | really finished. This means that the argument is not a valid | |
8169 | type-id. */ | |
8170 | if (!cp_parser_next_token_ends_template_argument_p (parser)) | |
8171 | cp_parser_error (parser, "expected template-argument"); | |
8172 | /* If that worked, we're done. */ | |
8173 | if (cp_parser_parse_definitely (parser)) | |
8174 | return argument; | |
8175 | } | |
a723baf1 MM |
8176 | /* We're still not sure what the argument will be. */ |
8177 | cp_parser_parse_tentatively (parser); | |
8178 | /* Try a template. */ | |
8179 | argument = cp_parser_id_expression (parser, | |
8180 | /*template_keyword_p=*/false, | |
8181 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
8182 | &template_p, |
8183 | /*declarator_p=*/false); | |
a723baf1 MM |
8184 | /* If the next token isn't a `,' or a `>', then this argument wasn't |
8185 | really finished. */ | |
d17811fd | 8186 | if (!cp_parser_next_token_ends_template_argument_p (parser)) |
a723baf1 MM |
8187 | cp_parser_error (parser, "expected template-argument"); |
8188 | if (!cp_parser_error_occurred (parser)) | |
8189 | { | |
8190 | /* Figure out what is being referred to. */ | |
5b4acce1 KL |
8191 | argument = cp_parser_lookup_name (parser, argument, |
8192 | /*is_type=*/false, | |
8193 | /*is_template=*/template_p, | |
8194 | /*is_namespace=*/false, | |
8195 | /*check_dependency=*/true); | |
8196 | if (TREE_CODE (argument) != TEMPLATE_DECL | |
8197 | && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE) | |
a723baf1 MM |
8198 | cp_parser_error (parser, "expected template-name"); |
8199 | } | |
8200 | if (cp_parser_parse_definitely (parser)) | |
8201 | return argument; | |
d17811fd MM |
8202 | /* It must be a non-type argument. There permitted cases are given |
8203 | in [temp.arg.nontype]: | |
8204 | ||
8205 | -- an integral constant-expression of integral or enumeration | |
8206 | type; or | |
8207 | ||
8208 | -- the name of a non-type template-parameter; or | |
8209 | ||
8210 | -- the name of an object or function with external linkage... | |
8211 | ||
8212 | -- the address of an object or function with external linkage... | |
8213 | ||
04c06002 | 8214 | -- a pointer to member... */ |
d17811fd MM |
8215 | /* Look for a non-type template parameter. */ |
8216 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
8217 | { | |
8218 | cp_parser_parse_tentatively (parser); | |
8219 | argument = cp_parser_primary_expression (parser, | |
8220 | &idk, | |
8221 | &qualifying_class); | |
8222 | if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX | |
8223 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
8224 | cp_parser_simulate_error (parser); | |
8225 | if (cp_parser_parse_definitely (parser)) | |
8226 | return argument; | |
8227 | } | |
8228 | /* If the next token is "&", the argument must be the address of an | |
8229 | object or function with external linkage. */ | |
8230 | address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND); | |
8231 | if (address_p) | |
8232 | cp_lexer_consume_token (parser->lexer); | |
8233 | /* See if we might have an id-expression. */ | |
8234 | token = cp_lexer_peek_token (parser->lexer); | |
8235 | if (token->type == CPP_NAME | |
8236 | || token->keyword == RID_OPERATOR | |
8237 | || token->type == CPP_SCOPE | |
8238 | || token->type == CPP_TEMPLATE_ID | |
8239 | || token->type == CPP_NESTED_NAME_SPECIFIER) | |
8240 | { | |
8241 | cp_parser_parse_tentatively (parser); | |
8242 | argument = cp_parser_primary_expression (parser, | |
8243 | &idk, | |
8244 | &qualifying_class); | |
8245 | if (cp_parser_error_occurred (parser) | |
8246 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
8247 | cp_parser_abort_tentative_parse (parser); | |
8248 | else | |
8249 | { | |
8250 | if (qualifying_class) | |
8251 | argument = finish_qualified_id_expr (qualifying_class, | |
8252 | argument, | |
8253 | /*done=*/true, | |
8254 | address_p); | |
8255 | if (TREE_CODE (argument) == VAR_DECL) | |
8256 | { | |
8257 | /* A variable without external linkage might still be a | |
8258 | valid constant-expression, so no error is issued here | |
8259 | if the external-linkage check fails. */ | |
8260 | if (!DECL_EXTERNAL_LINKAGE_P (argument)) | |
8261 | cp_parser_simulate_error (parser); | |
8262 | } | |
8263 | else if (is_overloaded_fn (argument)) | |
8264 | /* All overloaded functions are allowed; if the external | |
8265 | linkage test does not pass, an error will be issued | |
8266 | later. */ | |
8267 | ; | |
8268 | else if (address_p | |
8269 | && (TREE_CODE (argument) == OFFSET_REF | |
8270 | || TREE_CODE (argument) == SCOPE_REF)) | |
8271 | /* A pointer-to-member. */ | |
8272 | ; | |
8273 | else | |
8274 | cp_parser_simulate_error (parser); | |
8275 | ||
8276 | if (cp_parser_parse_definitely (parser)) | |
8277 | { | |
8278 | if (address_p) | |
8279 | argument = build_x_unary_op (ADDR_EXPR, argument); | |
8280 | return argument; | |
8281 | } | |
8282 | } | |
8283 | } | |
8284 | /* If the argument started with "&", there are no other valid | |
8285 | alternatives at this point. */ | |
8286 | if (address_p) | |
8287 | { | |
8288 | cp_parser_error (parser, "invalid non-type template argument"); | |
8289 | return error_mark_node; | |
8290 | } | |
4d5297fa GB |
8291 | /* If the argument wasn't successfully parsed as a type-id followed |
8292 | by '>>', the argument can only be a constant expression now. | |
8293 | Otherwise, we try parsing the constant-expression tentatively, | |
8294 | because the argument could really be a type-id. */ | |
8295 | if (maybe_type_id) | |
8296 | cp_parser_parse_tentatively (parser); | |
d17811fd MM |
8297 | argument = cp_parser_constant_expression (parser, |
8298 | /*allow_non_constant_p=*/false, | |
8299 | /*non_constant_p=*/NULL); | |
4d5297fa GB |
8300 | argument = cp_parser_fold_non_dependent_expr (argument); |
8301 | if (!maybe_type_id) | |
8302 | return argument; | |
8303 | if (!cp_parser_next_token_ends_template_argument_p (parser)) | |
8304 | cp_parser_error (parser, "expected template-argument"); | |
8305 | if (cp_parser_parse_definitely (parser)) | |
8306 | return argument; | |
8307 | /* We did our best to parse the argument as a non type-id, but that | |
8308 | was the only alternative that matched (albeit with a '>' after | |
8309 | it). We can assume it's just a typo from the user, and a | |
8310 | diagnostic will then be issued. */ | |
8311 | return cp_parser_type_id (parser); | |
a723baf1 MM |
8312 | } |
8313 | ||
8314 | /* Parse an explicit-instantiation. | |
8315 | ||
8316 | explicit-instantiation: | |
8317 | template declaration | |
8318 | ||
8319 | Although the standard says `declaration', what it really means is: | |
8320 | ||
8321 | explicit-instantiation: | |
8322 | template decl-specifier-seq [opt] declarator [opt] ; | |
8323 | ||
8324 | Things like `template int S<int>::i = 5, int S<double>::j;' are not | |
8325 | supposed to be allowed. A defect report has been filed about this | |
8326 | issue. | |
8327 | ||
8328 | GNU Extension: | |
8329 | ||
8330 | explicit-instantiation: | |
8331 | storage-class-specifier template | |
8332 | decl-specifier-seq [opt] declarator [opt] ; | |
8333 | function-specifier template | |
8334 | decl-specifier-seq [opt] declarator [opt] ; */ | |
8335 | ||
8336 | static void | |
94edc4ab | 8337 | cp_parser_explicit_instantiation (cp_parser* parser) |
a723baf1 | 8338 | { |
560ad596 | 8339 | int declares_class_or_enum; |
a723baf1 MM |
8340 | tree decl_specifiers; |
8341 | tree attributes; | |
8342 | tree extension_specifier = NULL_TREE; | |
8343 | ||
8344 | /* Look for an (optional) storage-class-specifier or | |
8345 | function-specifier. */ | |
8346 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
8347 | { | |
8348 | extension_specifier | |
8349 | = cp_parser_storage_class_specifier_opt (parser); | |
8350 | if (!extension_specifier) | |
8351 | extension_specifier = cp_parser_function_specifier_opt (parser); | |
8352 | } | |
8353 | ||
8354 | /* Look for the `template' keyword. */ | |
8355 | cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"); | |
8356 | /* Let the front end know that we are processing an explicit | |
8357 | instantiation. */ | |
8358 | begin_explicit_instantiation (); | |
8359 | /* [temp.explicit] says that we are supposed to ignore access | |
8360 | control while processing explicit instantiation directives. */ | |
78757caa | 8361 | push_deferring_access_checks (dk_no_check); |
a723baf1 MM |
8362 | /* Parse a decl-specifier-seq. */ |
8363 | decl_specifiers | |
8364 | = cp_parser_decl_specifier_seq (parser, | |
8365 | CP_PARSER_FLAGS_OPTIONAL, | |
8366 | &attributes, | |
8367 | &declares_class_or_enum); | |
8368 | /* If there was exactly one decl-specifier, and it declared a class, | |
8369 | and there's no declarator, then we have an explicit type | |
8370 | instantiation. */ | |
8371 | if (declares_class_or_enum && cp_parser_declares_only_class_p (parser)) | |
8372 | { | |
8373 | tree type; | |
8374 | ||
8375 | type = check_tag_decl (decl_specifiers); | |
b7fc8b57 KL |
8376 | /* Turn access control back on for names used during |
8377 | template instantiation. */ | |
8378 | pop_deferring_access_checks (); | |
a723baf1 MM |
8379 | if (type) |
8380 | do_type_instantiation (type, extension_specifier, /*complain=*/1); | |
8381 | } | |
8382 | else | |
8383 | { | |
8384 | tree declarator; | |
8385 | tree decl; | |
8386 | ||
8387 | /* Parse the declarator. */ | |
8388 | declarator | |
62b8a44e | 8389 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
8390 | /*ctor_dtor_or_conv_p=*/NULL, |
8391 | /*parenthesized_p=*/NULL); | |
560ad596 MM |
8392 | cp_parser_check_for_definition_in_return_type (declarator, |
8393 | declares_class_or_enum); | |
a723baf1 MM |
8394 | decl = grokdeclarator (declarator, decl_specifiers, |
8395 | NORMAL, 0, NULL); | |
b7fc8b57 KL |
8396 | /* Turn access control back on for names used during |
8397 | template instantiation. */ | |
8398 | pop_deferring_access_checks (); | |
a723baf1 MM |
8399 | /* Do the explicit instantiation. */ |
8400 | do_decl_instantiation (decl, extension_specifier); | |
8401 | } | |
8402 | /* We're done with the instantiation. */ | |
8403 | end_explicit_instantiation (); | |
a723baf1 | 8404 | |
e0860732 | 8405 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 MM |
8406 | } |
8407 | ||
8408 | /* Parse an explicit-specialization. | |
8409 | ||
8410 | explicit-specialization: | |
8411 | template < > declaration | |
8412 | ||
8413 | Although the standard says `declaration', what it really means is: | |
8414 | ||
8415 | explicit-specialization: | |
8416 | template <> decl-specifier [opt] init-declarator [opt] ; | |
8417 | template <> function-definition | |
8418 | template <> explicit-specialization | |
8419 | template <> template-declaration */ | |
8420 | ||
8421 | static void | |
94edc4ab | 8422 | cp_parser_explicit_specialization (cp_parser* parser) |
a723baf1 MM |
8423 | { |
8424 | /* Look for the `template' keyword. */ | |
8425 | cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"); | |
8426 | /* Look for the `<'. */ | |
8427 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
8428 | /* Look for the `>'. */ | |
8429 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
8430 | /* We have processed another parameter list. */ | |
8431 | ++parser->num_template_parameter_lists; | |
8432 | /* Let the front end know that we are beginning a specialization. */ | |
8433 | begin_specialization (); | |
8434 | ||
8435 | /* If the next keyword is `template', we need to figure out whether | |
8436 | or not we're looking a template-declaration. */ | |
8437 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
8438 | { | |
8439 | if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS | |
8440 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER) | |
8441 | cp_parser_template_declaration_after_export (parser, | |
8442 | /*member_p=*/false); | |
8443 | else | |
8444 | cp_parser_explicit_specialization (parser); | |
8445 | } | |
8446 | else | |
8447 | /* Parse the dependent declaration. */ | |
8448 | cp_parser_single_declaration (parser, | |
8449 | /*member_p=*/false, | |
8450 | /*friend_p=*/NULL); | |
8451 | ||
8452 | /* We're done with the specialization. */ | |
8453 | end_specialization (); | |
8454 | /* We're done with this parameter list. */ | |
8455 | --parser->num_template_parameter_lists; | |
8456 | } | |
8457 | ||
8458 | /* Parse a type-specifier. | |
8459 | ||
8460 | type-specifier: | |
8461 | simple-type-specifier | |
8462 | class-specifier | |
8463 | enum-specifier | |
8464 | elaborated-type-specifier | |
8465 | cv-qualifier | |
8466 | ||
8467 | GNU Extension: | |
8468 | ||
8469 | type-specifier: | |
8470 | __complex__ | |
8471 | ||
8472 | Returns a representation of the type-specifier. If the | |
8473 | type-specifier is a keyword (like `int' or `const', or | |
34cd5ae7 | 8474 | `__complex__') then the corresponding IDENTIFIER_NODE is returned. |
a723baf1 MM |
8475 | For a class-specifier, enum-specifier, or elaborated-type-specifier |
8476 | a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned. | |
8477 | ||
8478 | If IS_FRIEND is TRUE then this type-specifier is being declared a | |
8479 | `friend'. If IS_DECLARATION is TRUE, then this type-specifier is | |
8480 | appearing in a decl-specifier-seq. | |
8481 | ||
8482 | If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a | |
8483 | class-specifier, enum-specifier, or elaborated-type-specifier, then | |
83a00410 | 8484 | *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1 |
560ad596 MM |
8485 | if a type is declared; 2 if it is defined. Otherwise, it is set to |
8486 | zero. | |
a723baf1 MM |
8487 | |
8488 | If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a | |
8489 | cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it | |
8490 | is set to FALSE. */ | |
8491 | ||
8492 | static tree | |
94edc4ab NN |
8493 | cp_parser_type_specifier (cp_parser* parser, |
8494 | cp_parser_flags flags, | |
8495 | bool is_friend, | |
8496 | bool is_declaration, | |
560ad596 | 8497 | int* declares_class_or_enum, |
94edc4ab | 8498 | bool* is_cv_qualifier) |
a723baf1 MM |
8499 | { |
8500 | tree type_spec = NULL_TREE; | |
8501 | cp_token *token; | |
8502 | enum rid keyword; | |
8503 | ||
8504 | /* Assume this type-specifier does not declare a new type. */ | |
8505 | if (declares_class_or_enum) | |
8506 | *declares_class_or_enum = false; | |
8507 | /* And that it does not specify a cv-qualifier. */ | |
8508 | if (is_cv_qualifier) | |
8509 | *is_cv_qualifier = false; | |
8510 | /* Peek at the next token. */ | |
8511 | token = cp_lexer_peek_token (parser->lexer); | |
8512 | ||
8513 | /* If we're looking at a keyword, we can use that to guide the | |
8514 | production we choose. */ | |
8515 | keyword = token->keyword; | |
8516 | switch (keyword) | |
8517 | { | |
8518 | /* Any of these indicate either a class-specifier, or an | |
8519 | elaborated-type-specifier. */ | |
8520 | case RID_CLASS: | |
8521 | case RID_STRUCT: | |
8522 | case RID_UNION: | |
8523 | case RID_ENUM: | |
8524 | /* Parse tentatively so that we can back up if we don't find a | |
8525 | class-specifier or enum-specifier. */ | |
8526 | cp_parser_parse_tentatively (parser); | |
8527 | /* Look for the class-specifier or enum-specifier. */ | |
8528 | if (keyword == RID_ENUM) | |
8529 | type_spec = cp_parser_enum_specifier (parser); | |
8530 | else | |
8531 | type_spec = cp_parser_class_specifier (parser); | |
8532 | ||
8533 | /* If that worked, we're done. */ | |
8534 | if (cp_parser_parse_definitely (parser)) | |
8535 | { | |
8536 | if (declares_class_or_enum) | |
560ad596 | 8537 | *declares_class_or_enum = 2; |
a723baf1 MM |
8538 | return type_spec; |
8539 | } | |
8540 | ||
8541 | /* Fall through. */ | |
8542 | ||
8543 | case RID_TYPENAME: | |
8544 | /* Look for an elaborated-type-specifier. */ | |
8545 | type_spec = cp_parser_elaborated_type_specifier (parser, | |
8546 | is_friend, | |
8547 | is_declaration); | |
8548 | /* We're declaring a class or enum -- unless we're using | |
8549 | `typename'. */ | |
8550 | if (declares_class_or_enum && keyword != RID_TYPENAME) | |
560ad596 | 8551 | *declares_class_or_enum = 1; |
a723baf1 MM |
8552 | return type_spec; |
8553 | ||
8554 | case RID_CONST: | |
8555 | case RID_VOLATILE: | |
8556 | case RID_RESTRICT: | |
8557 | type_spec = cp_parser_cv_qualifier_opt (parser); | |
8558 | /* Even though we call a routine that looks for an optional | |
8559 | qualifier, we know that there should be one. */ | |
8560 | my_friendly_assert (type_spec != NULL, 20000328); | |
8561 | /* This type-specifier was a cv-qualified. */ | |
8562 | if (is_cv_qualifier) | |
8563 | *is_cv_qualifier = true; | |
8564 | ||
8565 | return type_spec; | |
8566 | ||
8567 | case RID_COMPLEX: | |
8568 | /* The `__complex__' keyword is a GNU extension. */ | |
8569 | return cp_lexer_consume_token (parser->lexer)->value; | |
8570 | ||
8571 | default: | |
8572 | break; | |
8573 | } | |
8574 | ||
8575 | /* If we do not already have a type-specifier, assume we are looking | |
8576 | at a simple-type-specifier. */ | |
4b0d3cbe MM |
8577 | type_spec = cp_parser_simple_type_specifier (parser, flags, |
8578 | /*identifier_p=*/true); | |
a723baf1 MM |
8579 | |
8580 | /* If we didn't find a type-specifier, and a type-specifier was not | |
8581 | optional in this context, issue an error message. */ | |
8582 | if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
8583 | { | |
8584 | cp_parser_error (parser, "expected type specifier"); | |
8585 | return error_mark_node; | |
8586 | } | |
8587 | ||
8588 | return type_spec; | |
8589 | } | |
8590 | ||
8591 | /* Parse a simple-type-specifier. | |
8592 | ||
8593 | simple-type-specifier: | |
8594 | :: [opt] nested-name-specifier [opt] type-name | |
8595 | :: [opt] nested-name-specifier template template-id | |
8596 | char | |
8597 | wchar_t | |
8598 | bool | |
8599 | short | |
8600 | int | |
8601 | long | |
8602 | signed | |
8603 | unsigned | |
8604 | float | |
8605 | double | |
8606 | void | |
8607 | ||
8608 | GNU Extension: | |
8609 | ||
8610 | simple-type-specifier: | |
8611 | __typeof__ unary-expression | |
8612 | __typeof__ ( type-id ) | |
8613 | ||
8614 | For the various keywords, the value returned is simply the | |
4b0d3cbe MM |
8615 | TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true. |
8616 | For the first two productions, and if IDENTIFIER_P is false, the | |
8617 | value returned is the indicated TYPE_DECL. */ | |
a723baf1 MM |
8618 | |
8619 | static tree | |
4b0d3cbe MM |
8620 | cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags, |
8621 | bool identifier_p) | |
a723baf1 MM |
8622 | { |
8623 | tree type = NULL_TREE; | |
8624 | cp_token *token; | |
8625 | ||
8626 | /* Peek at the next token. */ | |
8627 | token = cp_lexer_peek_token (parser->lexer); | |
8628 | ||
8629 | /* If we're looking at a keyword, things are easy. */ | |
8630 | switch (token->keyword) | |
8631 | { | |
8632 | case RID_CHAR: | |
4b0d3cbe MM |
8633 | type = char_type_node; |
8634 | break; | |
a723baf1 | 8635 | case RID_WCHAR: |
4b0d3cbe MM |
8636 | type = wchar_type_node; |
8637 | break; | |
a723baf1 | 8638 | case RID_BOOL: |
4b0d3cbe MM |
8639 | type = boolean_type_node; |
8640 | break; | |
a723baf1 | 8641 | case RID_SHORT: |
4b0d3cbe MM |
8642 | type = short_integer_type_node; |
8643 | break; | |
a723baf1 | 8644 | case RID_INT: |
4b0d3cbe MM |
8645 | type = integer_type_node; |
8646 | break; | |
a723baf1 | 8647 | case RID_LONG: |
4b0d3cbe MM |
8648 | type = long_integer_type_node; |
8649 | break; | |
a723baf1 | 8650 | case RID_SIGNED: |
4b0d3cbe MM |
8651 | type = integer_type_node; |
8652 | break; | |
a723baf1 | 8653 | case RID_UNSIGNED: |
4b0d3cbe MM |
8654 | type = unsigned_type_node; |
8655 | break; | |
a723baf1 | 8656 | case RID_FLOAT: |
4b0d3cbe MM |
8657 | type = float_type_node; |
8658 | break; | |
a723baf1 | 8659 | case RID_DOUBLE: |
4b0d3cbe MM |
8660 | type = double_type_node; |
8661 | break; | |
a723baf1 | 8662 | case RID_VOID: |
4b0d3cbe MM |
8663 | type = void_type_node; |
8664 | break; | |
a723baf1 MM |
8665 | |
8666 | case RID_TYPEOF: | |
8667 | { | |
8668 | tree operand; | |
8669 | ||
8670 | /* Consume the `typeof' token. */ | |
8671 | cp_lexer_consume_token (parser->lexer); | |
04c06002 | 8672 | /* Parse the operand to `typeof'. */ |
a723baf1 MM |
8673 | operand = cp_parser_sizeof_operand (parser, RID_TYPEOF); |
8674 | /* If it is not already a TYPE, take its type. */ | |
8675 | if (!TYPE_P (operand)) | |
8676 | operand = finish_typeof (operand); | |
8677 | ||
8678 | return operand; | |
8679 | } | |
8680 | ||
8681 | default: | |
8682 | break; | |
8683 | } | |
8684 | ||
4b0d3cbe MM |
8685 | /* If the type-specifier was for a built-in type, we're done. */ |
8686 | if (type) | |
8687 | { | |
8688 | tree id; | |
8689 | ||
8690 | /* Consume the token. */ | |
8691 | id = cp_lexer_consume_token (parser->lexer)->value; | |
8692 | return identifier_p ? id : TYPE_NAME (type); | |
8693 | } | |
8694 | ||
a723baf1 MM |
8695 | /* The type-specifier must be a user-defined type. */ |
8696 | if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) | |
8697 | { | |
8698 | /* Don't gobble tokens or issue error messages if this is an | |
8699 | optional type-specifier. */ | |
8700 | if (flags & CP_PARSER_FLAGS_OPTIONAL) | |
8701 | cp_parser_parse_tentatively (parser); | |
8702 | ||
8703 | /* Look for the optional `::' operator. */ | |
8704 | cp_parser_global_scope_opt (parser, | |
8705 | /*current_scope_valid_p=*/false); | |
8706 | /* Look for the nested-name specifier. */ | |
8707 | cp_parser_nested_name_specifier_opt (parser, | |
8708 | /*typename_keyword_p=*/false, | |
8709 | /*check_dependency_p=*/true, | |
a668c6ad MM |
8710 | /*type_p=*/false, |
8711 | /*is_declaration=*/false); | |
a723baf1 MM |
8712 | /* If we have seen a nested-name-specifier, and the next token |
8713 | is `template', then we are using the template-id production. */ | |
8714 | if (parser->scope | |
8715 | && cp_parser_optional_template_keyword (parser)) | |
8716 | { | |
8717 | /* Look for the template-id. */ | |
8718 | type = cp_parser_template_id (parser, | |
8719 | /*template_keyword_p=*/true, | |
a668c6ad MM |
8720 | /*check_dependency_p=*/true, |
8721 | /*is_declaration=*/false); | |
a723baf1 MM |
8722 | /* If the template-id did not name a type, we are out of |
8723 | luck. */ | |
8724 | if (TREE_CODE (type) != TYPE_DECL) | |
8725 | { | |
8726 | cp_parser_error (parser, "expected template-id for type"); | |
8727 | type = NULL_TREE; | |
8728 | } | |
8729 | } | |
8730 | /* Otherwise, look for a type-name. */ | |
8731 | else | |
4bb8ca28 | 8732 | type = cp_parser_type_name (parser); |
a723baf1 MM |
8733 | /* If it didn't work out, we don't have a TYPE. */ |
8734 | if ((flags & CP_PARSER_FLAGS_OPTIONAL) | |
8735 | && !cp_parser_parse_definitely (parser)) | |
8736 | type = NULL_TREE; | |
8737 | } | |
8738 | ||
8739 | /* If we didn't get a type-name, issue an error message. */ | |
8740 | if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
8741 | { | |
8742 | cp_parser_error (parser, "expected type-name"); | |
8743 | return error_mark_node; | |
8744 | } | |
8745 | ||
a668c6ad MM |
8746 | /* There is no valid C++ program where a non-template type is |
8747 | followed by a "<". That usually indicates that the user thought | |
8748 | that the type was a template. */ | |
4bb8ca28 | 8749 | if (type && type != error_mark_node) |
ee43dab5 | 8750 | cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type)); |
ec75414f | 8751 | |
a723baf1 MM |
8752 | return type; |
8753 | } | |
8754 | ||
8755 | /* Parse a type-name. | |
8756 | ||
8757 | type-name: | |
8758 | class-name | |
8759 | enum-name | |
8760 | typedef-name | |
8761 | ||
8762 | enum-name: | |
8763 | identifier | |
8764 | ||
8765 | typedef-name: | |
8766 | identifier | |
8767 | ||
8768 | Returns a TYPE_DECL for the the type. */ | |
8769 | ||
8770 | static tree | |
94edc4ab | 8771 | cp_parser_type_name (cp_parser* parser) |
a723baf1 MM |
8772 | { |
8773 | tree type_decl; | |
8774 | tree identifier; | |
8775 | ||
8776 | /* We can't know yet whether it is a class-name or not. */ | |
8777 | cp_parser_parse_tentatively (parser); | |
8778 | /* Try a class-name. */ | |
8779 | type_decl = cp_parser_class_name (parser, | |
8780 | /*typename_keyword_p=*/false, | |
8781 | /*template_keyword_p=*/false, | |
8782 | /*type_p=*/false, | |
a723baf1 | 8783 | /*check_dependency_p=*/true, |
a668c6ad MM |
8784 | /*class_head_p=*/false, |
8785 | /*is_declaration=*/false); | |
a723baf1 MM |
8786 | /* If it's not a class-name, keep looking. */ |
8787 | if (!cp_parser_parse_definitely (parser)) | |
8788 | { | |
8789 | /* It must be a typedef-name or an enum-name. */ | |
8790 | identifier = cp_parser_identifier (parser); | |
8791 | if (identifier == error_mark_node) | |
8792 | return error_mark_node; | |
8793 | ||
8794 | /* Look up the type-name. */ | |
8795 | type_decl = cp_parser_lookup_name_simple (parser, identifier); | |
8796 | /* Issue an error if we did not find a type-name. */ | |
8797 | if (TREE_CODE (type_decl) != TYPE_DECL) | |
8798 | { | |
4bb8ca28 MM |
8799 | if (!cp_parser_simulate_error (parser)) |
8800 | cp_parser_name_lookup_error (parser, identifier, type_decl, | |
8801 | "is not a type"); | |
a723baf1 MM |
8802 | type_decl = error_mark_node; |
8803 | } | |
8804 | /* Remember that the name was used in the definition of the | |
8805 | current class so that we can check later to see if the | |
8806 | meaning would have been different after the class was | |
8807 | entirely defined. */ | |
8808 | else if (type_decl != error_mark_node | |
8809 | && !parser->scope) | |
8810 | maybe_note_name_used_in_class (identifier, type_decl); | |
8811 | } | |
8812 | ||
8813 | return type_decl; | |
8814 | } | |
8815 | ||
8816 | ||
8817 | /* Parse an elaborated-type-specifier. Note that the grammar given | |
8818 | here incorporates the resolution to DR68. | |
8819 | ||
8820 | elaborated-type-specifier: | |
8821 | class-key :: [opt] nested-name-specifier [opt] identifier | |
8822 | class-key :: [opt] nested-name-specifier [opt] template [opt] template-id | |
8823 | enum :: [opt] nested-name-specifier [opt] identifier | |
8824 | typename :: [opt] nested-name-specifier identifier | |
8825 | typename :: [opt] nested-name-specifier template [opt] | |
8826 | template-id | |
8827 | ||
360d1b99 MM |
8828 | GNU extension: |
8829 | ||
8830 | elaborated-type-specifier: | |
8831 | class-key attributes :: [opt] nested-name-specifier [opt] identifier | |
8832 | class-key attributes :: [opt] nested-name-specifier [opt] | |
8833 | template [opt] template-id | |
8834 | enum attributes :: [opt] nested-name-specifier [opt] identifier | |
8835 | ||
a723baf1 MM |
8836 | If IS_FRIEND is TRUE, then this elaborated-type-specifier is being |
8837 | declared `friend'. If IS_DECLARATION is TRUE, then this | |
8838 | elaborated-type-specifier appears in a decl-specifiers-seq, i.e., | |
8839 | something is being declared. | |
8840 | ||
8841 | Returns the TYPE specified. */ | |
8842 | ||
8843 | static tree | |
94edc4ab NN |
8844 | cp_parser_elaborated_type_specifier (cp_parser* parser, |
8845 | bool is_friend, | |
8846 | bool is_declaration) | |
a723baf1 MM |
8847 | { |
8848 | enum tag_types tag_type; | |
8849 | tree identifier; | |
8850 | tree type = NULL_TREE; | |
360d1b99 | 8851 | tree attributes = NULL_TREE; |
a723baf1 MM |
8852 | |
8853 | /* See if we're looking at the `enum' keyword. */ | |
8854 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM)) | |
8855 | { | |
8856 | /* Consume the `enum' token. */ | |
8857 | cp_lexer_consume_token (parser->lexer); | |
8858 | /* Remember that it's an enumeration type. */ | |
8859 | tag_type = enum_type; | |
360d1b99 MM |
8860 | /* Parse the attributes. */ |
8861 | attributes = cp_parser_attributes_opt (parser); | |
a723baf1 MM |
8862 | } |
8863 | /* Or, it might be `typename'. */ | |
8864 | else if (cp_lexer_next_token_is_keyword (parser->lexer, | |
8865 | RID_TYPENAME)) | |
8866 | { | |
8867 | /* Consume the `typename' token. */ | |
8868 | cp_lexer_consume_token (parser->lexer); | |
8869 | /* Remember that it's a `typename' type. */ | |
8870 | tag_type = typename_type; | |
8871 | /* The `typename' keyword is only allowed in templates. */ | |
8872 | if (!processing_template_decl) | |
8873 | pedwarn ("using `typename' outside of template"); | |
8874 | } | |
8875 | /* Otherwise it must be a class-key. */ | |
8876 | else | |
8877 | { | |
8878 | tag_type = cp_parser_class_key (parser); | |
8879 | if (tag_type == none_type) | |
8880 | return error_mark_node; | |
360d1b99 MM |
8881 | /* Parse the attributes. */ |
8882 | attributes = cp_parser_attributes_opt (parser); | |
a723baf1 MM |
8883 | } |
8884 | ||
8885 | /* Look for the `::' operator. */ | |
8886 | cp_parser_global_scope_opt (parser, | |
8887 | /*current_scope_valid_p=*/false); | |
8888 | /* Look for the nested-name-specifier. */ | |
8889 | if (tag_type == typename_type) | |
8fa1ad0e MM |
8890 | { |
8891 | if (cp_parser_nested_name_specifier (parser, | |
8892 | /*typename_keyword_p=*/true, | |
8893 | /*check_dependency_p=*/true, | |
a668c6ad MM |
8894 | /*type_p=*/true, |
8895 | is_declaration) | |
8fa1ad0e MM |
8896 | == error_mark_node) |
8897 | return error_mark_node; | |
8898 | } | |
a723baf1 MM |
8899 | else |
8900 | /* Even though `typename' is not present, the proposed resolution | |
8901 | to Core Issue 180 says that in `class A<T>::B', `B' should be | |
8902 | considered a type-name, even if `A<T>' is dependent. */ | |
8903 | cp_parser_nested_name_specifier_opt (parser, | |
8904 | /*typename_keyword_p=*/true, | |
8905 | /*check_dependency_p=*/true, | |
a668c6ad MM |
8906 | /*type_p=*/true, |
8907 | is_declaration); | |
a723baf1 MM |
8908 | /* For everything but enumeration types, consider a template-id. */ |
8909 | if (tag_type != enum_type) | |
8910 | { | |
8911 | bool template_p = false; | |
8912 | tree decl; | |
8913 | ||
8914 | /* Allow the `template' keyword. */ | |
8915 | template_p = cp_parser_optional_template_keyword (parser); | |
8916 | /* If we didn't see `template', we don't know if there's a | |
8917 | template-id or not. */ | |
8918 | if (!template_p) | |
8919 | cp_parser_parse_tentatively (parser); | |
8920 | /* Parse the template-id. */ | |
8921 | decl = cp_parser_template_id (parser, template_p, | |
a668c6ad MM |
8922 | /*check_dependency_p=*/true, |
8923 | is_declaration); | |
a723baf1 MM |
8924 | /* If we didn't find a template-id, look for an ordinary |
8925 | identifier. */ | |
8926 | if (!template_p && !cp_parser_parse_definitely (parser)) | |
8927 | ; | |
8928 | /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is | |
8929 | in effect, then we must assume that, upon instantiation, the | |
8930 | template will correspond to a class. */ | |
8931 | else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR | |
8932 | && tag_type == typename_type) | |
8933 | type = make_typename_type (parser->scope, decl, | |
8934 | /*complain=*/1); | |
8935 | else | |
8936 | type = TREE_TYPE (decl); | |
8937 | } | |
8938 | ||
8939 | /* For an enumeration type, consider only a plain identifier. */ | |
8940 | if (!type) | |
8941 | { | |
8942 | identifier = cp_parser_identifier (parser); | |
8943 | ||
8944 | if (identifier == error_mark_node) | |
eb5abb39 NS |
8945 | { |
8946 | parser->scope = NULL_TREE; | |
8947 | return error_mark_node; | |
8948 | } | |
a723baf1 MM |
8949 | |
8950 | /* For a `typename', we needn't call xref_tag. */ | |
8951 | if (tag_type == typename_type) | |
8952 | return make_typename_type (parser->scope, identifier, | |
8953 | /*complain=*/1); | |
8954 | /* Look up a qualified name in the usual way. */ | |
8955 | if (parser->scope) | |
8956 | { | |
8957 | tree decl; | |
8958 | ||
8959 | /* In an elaborated-type-specifier, names are assumed to name | |
8960 | types, so we set IS_TYPE to TRUE when calling | |
8961 | cp_parser_lookup_name. */ | |
8962 | decl = cp_parser_lookup_name (parser, identifier, | |
a723baf1 | 8963 | /*is_type=*/true, |
b0bc6e8e | 8964 | /*is_template=*/false, |
eea9800f | 8965 | /*is_namespace=*/false, |
a723baf1 | 8966 | /*check_dependency=*/true); |
710b73e6 KL |
8967 | |
8968 | /* If we are parsing friend declaration, DECL may be a | |
8969 | TEMPLATE_DECL tree node here. However, we need to check | |
8970 | whether this TEMPLATE_DECL results in valid code. Consider | |
8971 | the following example: | |
8972 | ||
8973 | namespace N { | |
8974 | template <class T> class C {}; | |
8975 | } | |
8976 | class X { | |
8977 | template <class T> friend class N::C; // #1, valid code | |
8978 | }; | |
8979 | template <class T> class Y { | |
8980 | friend class N::C; // #2, invalid code | |
8981 | }; | |
8982 | ||
8983 | For both case #1 and #2, we arrive at a TEMPLATE_DECL after | |
8984 | name lookup of `N::C'. We see that friend declaration must | |
8985 | be template for the code to be valid. Note that | |
8986 | processing_template_decl does not work here since it is | |
8987 | always 1 for the above two cases. */ | |
8988 | ||
a723baf1 | 8989 | decl = (cp_parser_maybe_treat_template_as_class |
710b73e6 KL |
8990 | (decl, /*tag_name_p=*/is_friend |
8991 | && parser->num_template_parameter_lists)); | |
a723baf1 MM |
8992 | |
8993 | if (TREE_CODE (decl) != TYPE_DECL) | |
8994 | { | |
8995 | error ("expected type-name"); | |
8996 | return error_mark_node; | |
8997 | } | |
560ad596 MM |
8998 | |
8999 | if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE) | |
9000 | check_elaborated_type_specifier | |
4b0d3cbe | 9001 | (tag_type, decl, |
560ad596 MM |
9002 | (parser->num_template_parameter_lists |
9003 | || DECL_SELF_REFERENCE_P (decl))); | |
a723baf1 MM |
9004 | |
9005 | type = TREE_TYPE (decl); | |
9006 | } | |
9007 | else | |
9008 | { | |
9009 | /* An elaborated-type-specifier sometimes introduces a new type and | |
9010 | sometimes names an existing type. Normally, the rule is that it | |
9011 | introduces a new type only if there is not an existing type of | |
9012 | the same name already in scope. For example, given: | |
9013 | ||
9014 | struct S {}; | |
9015 | void f() { struct S s; } | |
9016 | ||
9017 | the `struct S' in the body of `f' is the same `struct S' as in | |
9018 | the global scope; the existing definition is used. However, if | |
9019 | there were no global declaration, this would introduce a new | |
9020 | local class named `S'. | |
9021 | ||
9022 | An exception to this rule applies to the following code: | |
9023 | ||
9024 | namespace N { struct S; } | |
9025 | ||
9026 | Here, the elaborated-type-specifier names a new type | |
9027 | unconditionally; even if there is already an `S' in the | |
9028 | containing scope this declaration names a new type. | |
9029 | This exception only applies if the elaborated-type-specifier | |
9030 | forms the complete declaration: | |
9031 | ||
9032 | [class.name] | |
9033 | ||
9034 | A declaration consisting solely of `class-key identifier ;' is | |
9035 | either a redeclaration of the name in the current scope or a | |
9036 | forward declaration of the identifier as a class name. It | |
9037 | introduces the name into the current scope. | |
9038 | ||
9039 | We are in this situation precisely when the next token is a `;'. | |
9040 | ||
9041 | An exception to the exception is that a `friend' declaration does | |
9042 | *not* name a new type; i.e., given: | |
9043 | ||
9044 | struct S { friend struct T; }; | |
9045 | ||
9046 | `T' is not a new type in the scope of `S'. | |
9047 | ||
9048 | Also, `new struct S' or `sizeof (struct S)' never results in the | |
9049 | definition of a new type; a new type can only be declared in a | |
9bcb9aae | 9050 | declaration context. */ |
a723baf1 MM |
9051 | |
9052 | type = xref_tag (tag_type, identifier, | |
360d1b99 | 9053 | attributes, |
a723baf1 MM |
9054 | (is_friend |
9055 | || !is_declaration | |
9056 | || cp_lexer_next_token_is_not (parser->lexer, | |
cbd63935 KL |
9057 | CPP_SEMICOLON)), |
9058 | parser->num_template_parameter_lists); | |
a723baf1 MM |
9059 | } |
9060 | } | |
9061 | if (tag_type != enum_type) | |
9062 | cp_parser_check_class_key (tag_type, type); | |
ee43dab5 MM |
9063 | |
9064 | /* A "<" cannot follow an elaborated type specifier. If that | |
9065 | happens, the user was probably trying to form a template-id. */ | |
9066 | cp_parser_check_for_invalid_template_id (parser, type); | |
9067 | ||
a723baf1 MM |
9068 | return type; |
9069 | } | |
9070 | ||
9071 | /* Parse an enum-specifier. | |
9072 | ||
9073 | enum-specifier: | |
9074 | enum identifier [opt] { enumerator-list [opt] } | |
9075 | ||
9076 | Returns an ENUM_TYPE representing the enumeration. */ | |
9077 | ||
9078 | static tree | |
94edc4ab | 9079 | cp_parser_enum_specifier (cp_parser* parser) |
a723baf1 MM |
9080 | { |
9081 | cp_token *token; | |
9082 | tree identifier = NULL_TREE; | |
9083 | tree type; | |
9084 | ||
9085 | /* Look for the `enum' keyword. */ | |
9086 | if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'")) | |
9087 | return error_mark_node; | |
9088 | /* Peek at the next token. */ | |
9089 | token = cp_lexer_peek_token (parser->lexer); | |
9090 | ||
9091 | /* See if it is an identifier. */ | |
9092 | if (token->type == CPP_NAME) | |
9093 | identifier = cp_parser_identifier (parser); | |
9094 | ||
9095 | /* Look for the `{'. */ | |
9096 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
9097 | return error_mark_node; | |
9098 | ||
9099 | /* At this point, we're going ahead with the enum-specifier, even | |
9100 | if some other problem occurs. */ | |
9101 | cp_parser_commit_to_tentative_parse (parser); | |
9102 | ||
9103 | /* Issue an error message if type-definitions are forbidden here. */ | |
9104 | cp_parser_check_type_definition (parser); | |
9105 | ||
9106 | /* Create the new type. */ | |
9107 | type = start_enum (identifier ? identifier : make_anon_name ()); | |
9108 | ||
9109 | /* Peek at the next token. */ | |
9110 | token = cp_lexer_peek_token (parser->lexer); | |
9111 | /* If it's not a `}', then there are some enumerators. */ | |
9112 | if (token->type != CPP_CLOSE_BRACE) | |
9113 | cp_parser_enumerator_list (parser, type); | |
9114 | /* Look for the `}'. */ | |
9115 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
9116 | ||
9117 | /* Finish up the enumeration. */ | |
9118 | finish_enum (type); | |
9119 | ||
9120 | return type; | |
9121 | } | |
9122 | ||
9123 | /* Parse an enumerator-list. The enumerators all have the indicated | |
9124 | TYPE. | |
9125 | ||
9126 | enumerator-list: | |
9127 | enumerator-definition | |
9128 | enumerator-list , enumerator-definition */ | |
9129 | ||
9130 | static void | |
94edc4ab | 9131 | cp_parser_enumerator_list (cp_parser* parser, tree type) |
a723baf1 MM |
9132 | { |
9133 | while (true) | |
9134 | { | |
9135 | cp_token *token; | |
9136 | ||
9137 | /* Parse an enumerator-definition. */ | |
9138 | cp_parser_enumerator_definition (parser, type); | |
9139 | /* Peek at the next token. */ | |
9140 | token = cp_lexer_peek_token (parser->lexer); | |
9141 | /* If it's not a `,', then we've reached the end of the | |
9142 | list. */ | |
9143 | if (token->type != CPP_COMMA) | |
9144 | break; | |
9145 | /* Otherwise, consume the `,' and keep going. */ | |
9146 | cp_lexer_consume_token (parser->lexer); | |
9147 | /* If the next token is a `}', there is a trailing comma. */ | |
9148 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
9149 | { | |
9150 | if (pedantic && !in_system_header) | |
9151 | pedwarn ("comma at end of enumerator list"); | |
9152 | break; | |
9153 | } | |
9154 | } | |
9155 | } | |
9156 | ||
9157 | /* Parse an enumerator-definition. The enumerator has the indicated | |
9158 | TYPE. | |
9159 | ||
9160 | enumerator-definition: | |
9161 | enumerator | |
9162 | enumerator = constant-expression | |
9163 | ||
9164 | enumerator: | |
9165 | identifier */ | |
9166 | ||
9167 | static void | |
94edc4ab | 9168 | cp_parser_enumerator_definition (cp_parser* parser, tree type) |
a723baf1 MM |
9169 | { |
9170 | cp_token *token; | |
9171 | tree identifier; | |
9172 | tree value; | |
9173 | ||
9174 | /* Look for the identifier. */ | |
9175 | identifier = cp_parser_identifier (parser); | |
9176 | if (identifier == error_mark_node) | |
9177 | return; | |
9178 | ||
9179 | /* Peek at the next token. */ | |
9180 | token = cp_lexer_peek_token (parser->lexer); | |
9181 | /* If it's an `=', then there's an explicit value. */ | |
9182 | if (token->type == CPP_EQ) | |
9183 | { | |
9184 | /* Consume the `=' token. */ | |
9185 | cp_lexer_consume_token (parser->lexer); | |
9186 | /* Parse the value. */ | |
14d22dd6 | 9187 | value = cp_parser_constant_expression (parser, |
d17811fd | 9188 | /*allow_non_constant_p=*/false, |
14d22dd6 | 9189 | NULL); |
a723baf1 MM |
9190 | } |
9191 | else | |
9192 | value = NULL_TREE; | |
9193 | ||
9194 | /* Create the enumerator. */ | |
9195 | build_enumerator (identifier, value, type); | |
9196 | } | |
9197 | ||
9198 | /* Parse a namespace-name. | |
9199 | ||
9200 | namespace-name: | |
9201 | original-namespace-name | |
9202 | namespace-alias | |
9203 | ||
9204 | Returns the NAMESPACE_DECL for the namespace. */ | |
9205 | ||
9206 | static tree | |
94edc4ab | 9207 | cp_parser_namespace_name (cp_parser* parser) |
a723baf1 MM |
9208 | { |
9209 | tree identifier; | |
9210 | tree namespace_decl; | |
9211 | ||
9212 | /* Get the name of the namespace. */ | |
9213 | identifier = cp_parser_identifier (parser); | |
9214 | if (identifier == error_mark_node) | |
9215 | return error_mark_node; | |
9216 | ||
eea9800f MM |
9217 | /* Look up the identifier in the currently active scope. Look only |
9218 | for namespaces, due to: | |
9219 | ||
9220 | [basic.lookup.udir] | |
9221 | ||
9222 | When looking up a namespace-name in a using-directive or alias | |
9223 | definition, only namespace names are considered. | |
9224 | ||
9225 | And: | |
9226 | ||
9227 | [basic.lookup.qual] | |
9228 | ||
9229 | During the lookup of a name preceding the :: scope resolution | |
9230 | operator, object, function, and enumerator names are ignored. | |
9231 | ||
9232 | (Note that cp_parser_class_or_namespace_name only calls this | |
9233 | function if the token after the name is the scope resolution | |
9234 | operator.) */ | |
9235 | namespace_decl = cp_parser_lookup_name (parser, identifier, | |
eea9800f | 9236 | /*is_type=*/false, |
b0bc6e8e | 9237 | /*is_template=*/false, |
eea9800f MM |
9238 | /*is_namespace=*/true, |
9239 | /*check_dependency=*/true); | |
a723baf1 MM |
9240 | /* If it's not a namespace, issue an error. */ |
9241 | if (namespace_decl == error_mark_node | |
9242 | || TREE_CODE (namespace_decl) != NAMESPACE_DECL) | |
9243 | { | |
9244 | cp_parser_error (parser, "expected namespace-name"); | |
9245 | namespace_decl = error_mark_node; | |
9246 | } | |
9247 | ||
9248 | return namespace_decl; | |
9249 | } | |
9250 | ||
9251 | /* Parse a namespace-definition. | |
9252 | ||
9253 | namespace-definition: | |
9254 | named-namespace-definition | |
9255 | unnamed-namespace-definition | |
9256 | ||
9257 | named-namespace-definition: | |
9258 | original-namespace-definition | |
9259 | extension-namespace-definition | |
9260 | ||
9261 | original-namespace-definition: | |
9262 | namespace identifier { namespace-body } | |
9263 | ||
9264 | extension-namespace-definition: | |
9265 | namespace original-namespace-name { namespace-body } | |
9266 | ||
9267 | unnamed-namespace-definition: | |
9268 | namespace { namespace-body } */ | |
9269 | ||
9270 | static void | |
94edc4ab | 9271 | cp_parser_namespace_definition (cp_parser* parser) |
a723baf1 MM |
9272 | { |
9273 | tree identifier; | |
9274 | ||
9275 | /* Look for the `namespace' keyword. */ | |
9276 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
9277 | ||
9278 | /* Get the name of the namespace. We do not attempt to distinguish | |
9279 | between an original-namespace-definition and an | |
9280 | extension-namespace-definition at this point. The semantic | |
9281 | analysis routines are responsible for that. */ | |
9282 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
9283 | identifier = cp_parser_identifier (parser); | |
9284 | else | |
9285 | identifier = NULL_TREE; | |
9286 | ||
9287 | /* Look for the `{' to start the namespace. */ | |
9288 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
9289 | /* Start the namespace. */ | |
9290 | push_namespace (identifier); | |
9291 | /* Parse the body of the namespace. */ | |
9292 | cp_parser_namespace_body (parser); | |
9293 | /* Finish the namespace. */ | |
9294 | pop_namespace (); | |
9295 | /* Look for the final `}'. */ | |
9296 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
9297 | } | |
9298 | ||
9299 | /* Parse a namespace-body. | |
9300 | ||
9301 | namespace-body: | |
9302 | declaration-seq [opt] */ | |
9303 | ||
9304 | static void | |
94edc4ab | 9305 | cp_parser_namespace_body (cp_parser* parser) |
a723baf1 MM |
9306 | { |
9307 | cp_parser_declaration_seq_opt (parser); | |
9308 | } | |
9309 | ||
9310 | /* Parse a namespace-alias-definition. | |
9311 | ||
9312 | namespace-alias-definition: | |
9313 | namespace identifier = qualified-namespace-specifier ; */ | |
9314 | ||
9315 | static void | |
94edc4ab | 9316 | cp_parser_namespace_alias_definition (cp_parser* parser) |
a723baf1 MM |
9317 | { |
9318 | tree identifier; | |
9319 | tree namespace_specifier; | |
9320 | ||
9321 | /* Look for the `namespace' keyword. */ | |
9322 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
9323 | /* Look for the identifier. */ | |
9324 | identifier = cp_parser_identifier (parser); | |
9325 | if (identifier == error_mark_node) | |
9326 | return; | |
9327 | /* Look for the `=' token. */ | |
9328 | cp_parser_require (parser, CPP_EQ, "`='"); | |
9329 | /* Look for the qualified-namespace-specifier. */ | |
9330 | namespace_specifier | |
9331 | = cp_parser_qualified_namespace_specifier (parser); | |
9332 | /* Look for the `;' token. */ | |
9333 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
9334 | ||
9335 | /* Register the alias in the symbol table. */ | |
9336 | do_namespace_alias (identifier, namespace_specifier); | |
9337 | } | |
9338 | ||
9339 | /* Parse a qualified-namespace-specifier. | |
9340 | ||
9341 | qualified-namespace-specifier: | |
9342 | :: [opt] nested-name-specifier [opt] namespace-name | |
9343 | ||
9344 | Returns a NAMESPACE_DECL corresponding to the specified | |
9345 | namespace. */ | |
9346 | ||
9347 | static tree | |
94edc4ab | 9348 | cp_parser_qualified_namespace_specifier (cp_parser* parser) |
a723baf1 MM |
9349 | { |
9350 | /* Look for the optional `::'. */ | |
9351 | cp_parser_global_scope_opt (parser, | |
9352 | /*current_scope_valid_p=*/false); | |
9353 | ||
9354 | /* Look for the optional nested-name-specifier. */ | |
9355 | cp_parser_nested_name_specifier_opt (parser, | |
9356 | /*typename_keyword_p=*/false, | |
9357 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9358 | /*type_p=*/false, |
9359 | /*is_declaration=*/true); | |
a723baf1 MM |
9360 | |
9361 | return cp_parser_namespace_name (parser); | |
9362 | } | |
9363 | ||
9364 | /* Parse a using-declaration. | |
9365 | ||
9366 | using-declaration: | |
9367 | using typename [opt] :: [opt] nested-name-specifier unqualified-id ; | |
9368 | using :: unqualified-id ; */ | |
9369 | ||
9370 | static void | |
94edc4ab | 9371 | cp_parser_using_declaration (cp_parser* parser) |
a723baf1 MM |
9372 | { |
9373 | cp_token *token; | |
9374 | bool typename_p = false; | |
9375 | bool global_scope_p; | |
9376 | tree decl; | |
9377 | tree identifier; | |
9378 | tree scope; | |
9379 | ||
9380 | /* Look for the `using' keyword. */ | |
9381 | cp_parser_require_keyword (parser, RID_USING, "`using'"); | |
9382 | ||
9383 | /* Peek at the next token. */ | |
9384 | token = cp_lexer_peek_token (parser->lexer); | |
9385 | /* See if it's `typename'. */ | |
9386 | if (token->keyword == RID_TYPENAME) | |
9387 | { | |
9388 | /* Remember that we've seen it. */ | |
9389 | typename_p = true; | |
9390 | /* Consume the `typename' token. */ | |
9391 | cp_lexer_consume_token (parser->lexer); | |
9392 | } | |
9393 | ||
9394 | /* Look for the optional global scope qualification. */ | |
9395 | global_scope_p | |
9396 | = (cp_parser_global_scope_opt (parser, | |
9397 | /*current_scope_valid_p=*/false) | |
9398 | != NULL_TREE); | |
9399 | ||
9400 | /* If we saw `typename', or didn't see `::', then there must be a | |
9401 | nested-name-specifier present. */ | |
9402 | if (typename_p || !global_scope_p) | |
9403 | cp_parser_nested_name_specifier (parser, typename_p, | |
9404 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9405 | /*type_p=*/false, |
9406 | /*is_declaration=*/true); | |
a723baf1 MM |
9407 | /* Otherwise, we could be in either of the two productions. In that |
9408 | case, treat the nested-name-specifier as optional. */ | |
9409 | else | |
9410 | cp_parser_nested_name_specifier_opt (parser, | |
9411 | /*typename_keyword_p=*/false, | |
9412 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9413 | /*type_p=*/false, |
9414 | /*is_declaration=*/true); | |
a723baf1 MM |
9415 | |
9416 | /* Parse the unqualified-id. */ | |
9417 | identifier = cp_parser_unqualified_id (parser, | |
9418 | /*template_keyword_p=*/false, | |
f3c2dfc6 MM |
9419 | /*check_dependency_p=*/true, |
9420 | /*declarator_p=*/true); | |
a723baf1 MM |
9421 | |
9422 | /* The function we call to handle a using-declaration is different | |
9423 | depending on what scope we are in. */ | |
f3c2dfc6 MM |
9424 | if (identifier == error_mark_node) |
9425 | ; | |
9426 | else if (TREE_CODE (identifier) != IDENTIFIER_NODE | |
9427 | && TREE_CODE (identifier) != BIT_NOT_EXPR) | |
9428 | /* [namespace.udecl] | |
9429 | ||
9430 | A using declaration shall not name a template-id. */ | |
9431 | error ("a template-id may not appear in a using-declaration"); | |
a723baf1 MM |
9432 | else |
9433 | { | |
f3c2dfc6 MM |
9434 | scope = current_scope (); |
9435 | if (scope && TYPE_P (scope)) | |
4eb6d609 | 9436 | { |
f3c2dfc6 MM |
9437 | /* Create the USING_DECL. */ |
9438 | decl = do_class_using_decl (build_nt (SCOPE_REF, | |
9439 | parser->scope, | |
9440 | identifier)); | |
9441 | /* Add it to the list of members in this class. */ | |
9442 | finish_member_declaration (decl); | |
4eb6d609 | 9443 | } |
a723baf1 | 9444 | else |
f3c2dfc6 MM |
9445 | { |
9446 | decl = cp_parser_lookup_name_simple (parser, identifier); | |
9447 | if (decl == error_mark_node) | |
4bb8ca28 | 9448 | cp_parser_name_lookup_error (parser, identifier, decl, NULL); |
f3c2dfc6 MM |
9449 | else if (scope) |
9450 | do_local_using_decl (decl); | |
9451 | else | |
9452 | do_toplevel_using_decl (decl); | |
9453 | } | |
a723baf1 MM |
9454 | } |
9455 | ||
9456 | /* Look for the final `;'. */ | |
9457 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
9458 | } | |
9459 | ||
9460 | /* Parse a using-directive. | |
9461 | ||
9462 | using-directive: | |
9463 | using namespace :: [opt] nested-name-specifier [opt] | |
9464 | namespace-name ; */ | |
9465 | ||
9466 | static void | |
94edc4ab | 9467 | cp_parser_using_directive (cp_parser* parser) |
a723baf1 MM |
9468 | { |
9469 | tree namespace_decl; | |
86098eb8 | 9470 | tree attribs; |
a723baf1 MM |
9471 | |
9472 | /* Look for the `using' keyword. */ | |
9473 | cp_parser_require_keyword (parser, RID_USING, "`using'"); | |
9474 | /* And the `namespace' keyword. */ | |
9475 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
9476 | /* Look for the optional `::' operator. */ | |
9477 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); | |
34cd5ae7 | 9478 | /* And the optional nested-name-specifier. */ |
a723baf1 MM |
9479 | cp_parser_nested_name_specifier_opt (parser, |
9480 | /*typename_keyword_p=*/false, | |
9481 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9482 | /*type_p=*/false, |
9483 | /*is_declaration=*/true); | |
a723baf1 MM |
9484 | /* Get the namespace being used. */ |
9485 | namespace_decl = cp_parser_namespace_name (parser); | |
86098eb8 JM |
9486 | /* And any specified attributes. */ |
9487 | attribs = cp_parser_attributes_opt (parser); | |
a723baf1 | 9488 | /* Update the symbol table. */ |
86098eb8 | 9489 | parse_using_directive (namespace_decl, attribs); |
a723baf1 MM |
9490 | /* Look for the final `;'. */ |
9491 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
9492 | } | |
9493 | ||
9494 | /* Parse an asm-definition. | |
9495 | ||
9496 | asm-definition: | |
9497 | asm ( string-literal ) ; | |
9498 | ||
9499 | GNU Extension: | |
9500 | ||
9501 | asm-definition: | |
9502 | asm volatile [opt] ( string-literal ) ; | |
9503 | asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ; | |
9504 | asm volatile [opt] ( string-literal : asm-operand-list [opt] | |
9505 | : asm-operand-list [opt] ) ; | |
9506 | asm volatile [opt] ( string-literal : asm-operand-list [opt] | |
9507 | : asm-operand-list [opt] | |
9508 | : asm-operand-list [opt] ) ; */ | |
9509 | ||
9510 | static void | |
94edc4ab | 9511 | cp_parser_asm_definition (cp_parser* parser) |
a723baf1 MM |
9512 | { |
9513 | cp_token *token; | |
9514 | tree string; | |
9515 | tree outputs = NULL_TREE; | |
9516 | tree inputs = NULL_TREE; | |
9517 | tree clobbers = NULL_TREE; | |
9518 | tree asm_stmt; | |
9519 | bool volatile_p = false; | |
9520 | bool extended_p = false; | |
9521 | ||
9522 | /* Look for the `asm' keyword. */ | |
9523 | cp_parser_require_keyword (parser, RID_ASM, "`asm'"); | |
9524 | /* See if the next token is `volatile'. */ | |
9525 | if (cp_parser_allow_gnu_extensions_p (parser) | |
9526 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE)) | |
9527 | { | |
9528 | /* Remember that we saw the `volatile' keyword. */ | |
9529 | volatile_p = true; | |
9530 | /* Consume the token. */ | |
9531 | cp_lexer_consume_token (parser->lexer); | |
9532 | } | |
9533 | /* Look for the opening `('. */ | |
9534 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
9535 | /* Look for the string. */ | |
9536 | token = cp_parser_require (parser, CPP_STRING, "asm body"); | |
9537 | if (!token) | |
9538 | return; | |
9539 | string = token->value; | |
9540 | /* If we're allowing GNU extensions, check for the extended assembly | |
9541 | syntax. Unfortunately, the `:' tokens need not be separated by | |
9542 | a space in C, and so, for compatibility, we tolerate that here | |
9543 | too. Doing that means that we have to treat the `::' operator as | |
9544 | two `:' tokens. */ | |
9545 | if (cp_parser_allow_gnu_extensions_p (parser) | |
9546 | && at_function_scope_p () | |
9547 | && (cp_lexer_next_token_is (parser->lexer, CPP_COLON) | |
9548 | || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))) | |
9549 | { | |
9550 | bool inputs_p = false; | |
9551 | bool clobbers_p = false; | |
9552 | ||
9553 | /* The extended syntax was used. */ | |
9554 | extended_p = true; | |
9555 | ||
9556 | /* Look for outputs. */ | |
9557 | if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
9558 | { | |
9559 | /* Consume the `:'. */ | |
9560 | cp_lexer_consume_token (parser->lexer); | |
9561 | /* Parse the output-operands. */ | |
9562 | if (cp_lexer_next_token_is_not (parser->lexer, | |
9563 | CPP_COLON) | |
9564 | && cp_lexer_next_token_is_not (parser->lexer, | |
8caf4c38 MM |
9565 | CPP_SCOPE) |
9566 | && cp_lexer_next_token_is_not (parser->lexer, | |
9567 | CPP_CLOSE_PAREN)) | |
a723baf1 MM |
9568 | outputs = cp_parser_asm_operand_list (parser); |
9569 | } | |
9570 | /* If the next token is `::', there are no outputs, and the | |
9571 | next token is the beginning of the inputs. */ | |
9572 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
9573 | { | |
9574 | /* Consume the `::' token. */ | |
9575 | cp_lexer_consume_token (parser->lexer); | |
9576 | /* The inputs are coming next. */ | |
9577 | inputs_p = true; | |
9578 | } | |
9579 | ||
9580 | /* Look for inputs. */ | |
9581 | if (inputs_p | |
9582 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
9583 | { | |
9584 | if (!inputs_p) | |
9585 | /* Consume the `:'. */ | |
9586 | cp_lexer_consume_token (parser->lexer); | |
9587 | /* Parse the output-operands. */ | |
9588 | if (cp_lexer_next_token_is_not (parser->lexer, | |
9589 | CPP_COLON) | |
9590 | && cp_lexer_next_token_is_not (parser->lexer, | |
8caf4c38 MM |
9591 | CPP_SCOPE) |
9592 | && cp_lexer_next_token_is_not (parser->lexer, | |
9593 | CPP_CLOSE_PAREN)) | |
a723baf1 MM |
9594 | inputs = cp_parser_asm_operand_list (parser); |
9595 | } | |
9596 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
9597 | /* The clobbers are coming next. */ | |
9598 | clobbers_p = true; | |
9599 | ||
9600 | /* Look for clobbers. */ | |
9601 | if (clobbers_p | |
9602 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
9603 | { | |
9604 | if (!clobbers_p) | |
9605 | /* Consume the `:'. */ | |
9606 | cp_lexer_consume_token (parser->lexer); | |
9607 | /* Parse the clobbers. */ | |
8caf4c38 MM |
9608 | if (cp_lexer_next_token_is_not (parser->lexer, |
9609 | CPP_CLOSE_PAREN)) | |
9610 | clobbers = cp_parser_asm_clobber_list (parser); | |
a723baf1 MM |
9611 | } |
9612 | } | |
9613 | /* Look for the closing `)'. */ | |
9614 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
a668c6ad MM |
9615 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
9616 | /*consume_paren=*/true); | |
a723baf1 MM |
9617 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
9618 | ||
9619 | /* Create the ASM_STMT. */ | |
9620 | if (at_function_scope_p ()) | |
9621 | { | |
9622 | asm_stmt = | |
9623 | finish_asm_stmt (volatile_p | |
9624 | ? ridpointers[(int) RID_VOLATILE] : NULL_TREE, | |
9625 | string, outputs, inputs, clobbers); | |
9626 | /* If the extended syntax was not used, mark the ASM_STMT. */ | |
9627 | if (!extended_p) | |
9628 | ASM_INPUT_P (asm_stmt) = 1; | |
9629 | } | |
9630 | else | |
9631 | assemble_asm (string); | |
9632 | } | |
9633 | ||
9634 | /* Declarators [gram.dcl.decl] */ | |
9635 | ||
9636 | /* Parse an init-declarator. | |
9637 | ||
9638 | init-declarator: | |
9639 | declarator initializer [opt] | |
9640 | ||
9641 | GNU Extension: | |
9642 | ||
9643 | init-declarator: | |
9644 | declarator asm-specification [opt] attributes [opt] initializer [opt] | |
9645 | ||
4bb8ca28 MM |
9646 | function-definition: |
9647 | decl-specifier-seq [opt] declarator ctor-initializer [opt] | |
9648 | function-body | |
9649 | decl-specifier-seq [opt] declarator function-try-block | |
9650 | ||
9651 | GNU Extension: | |
9652 | ||
9653 | function-definition: | |
9654 | __extension__ function-definition | |
9655 | ||
a723baf1 | 9656 | The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator. |
c8e4f0e9 | 9657 | Returns a representation of the entity declared. If MEMBER_P is TRUE, |
cf22909c KL |
9658 | then this declarator appears in a class scope. The new DECL created |
9659 | by this declarator is returned. | |
a723baf1 MM |
9660 | |
9661 | If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and | |
9662 | for a function-definition here as well. If the declarator is a | |
9663 | declarator for a function-definition, *FUNCTION_DEFINITION_P will | |
9664 | be TRUE upon return. By that point, the function-definition will | |
9665 | have been completely parsed. | |
9666 | ||
9667 | FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P | |
9668 | is FALSE. */ | |
9669 | ||
9670 | static tree | |
94edc4ab NN |
9671 | cp_parser_init_declarator (cp_parser* parser, |
9672 | tree decl_specifiers, | |
9673 | tree prefix_attributes, | |
9674 | bool function_definition_allowed_p, | |
9675 | bool member_p, | |
560ad596 | 9676 | int declares_class_or_enum, |
94edc4ab | 9677 | bool* function_definition_p) |
a723baf1 MM |
9678 | { |
9679 | cp_token *token; | |
9680 | tree declarator; | |
9681 | tree attributes; | |
9682 | tree asm_specification; | |
9683 | tree initializer; | |
9684 | tree decl = NULL_TREE; | |
9685 | tree scope; | |
a723baf1 MM |
9686 | bool is_initialized; |
9687 | bool is_parenthesized_init; | |
39703eb9 | 9688 | bool is_non_constant_init; |
7efa3e22 | 9689 | int ctor_dtor_or_conv_p; |
a723baf1 MM |
9690 | bool friend_p; |
9691 | ||
9692 | /* Assume that this is not the declarator for a function | |
9693 | definition. */ | |
9694 | if (function_definition_p) | |
9695 | *function_definition_p = false; | |
9696 | ||
9697 | /* Defer access checks while parsing the declarator; we cannot know | |
9698 | what names are accessible until we know what is being | |
9699 | declared. */ | |
cf22909c KL |
9700 | resume_deferring_access_checks (); |
9701 | ||
a723baf1 MM |
9702 | /* Parse the declarator. */ |
9703 | declarator | |
62b8a44e | 9704 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
9705 | &ctor_dtor_or_conv_p, |
9706 | /*parenthesized_p=*/NULL); | |
a723baf1 | 9707 | /* Gather up the deferred checks. */ |
cf22909c | 9708 | stop_deferring_access_checks (); |
24c0ef37 | 9709 | |
a723baf1 MM |
9710 | /* If the DECLARATOR was erroneous, there's no need to go |
9711 | further. */ | |
9712 | if (declarator == error_mark_node) | |
cf22909c | 9713 | return error_mark_node; |
a723baf1 | 9714 | |
560ad596 MM |
9715 | cp_parser_check_for_definition_in_return_type (declarator, |
9716 | declares_class_or_enum); | |
9717 | ||
a723baf1 MM |
9718 | /* Figure out what scope the entity declared by the DECLARATOR is |
9719 | located in. `grokdeclarator' sometimes changes the scope, so | |
9720 | we compute it now. */ | |
9721 | scope = get_scope_of_declarator (declarator); | |
9722 | ||
9723 | /* If we're allowing GNU extensions, look for an asm-specification | |
9724 | and attributes. */ | |
9725 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
9726 | { | |
9727 | /* Look for an asm-specification. */ | |
9728 | asm_specification = cp_parser_asm_specification_opt (parser); | |
9729 | /* And attributes. */ | |
9730 | attributes = cp_parser_attributes_opt (parser); | |
9731 | } | |
9732 | else | |
9733 | { | |
9734 | asm_specification = NULL_TREE; | |
9735 | attributes = NULL_TREE; | |
9736 | } | |
9737 | ||
9738 | /* Peek at the next token. */ | |
9739 | token = cp_lexer_peek_token (parser->lexer); | |
9740 | /* Check to see if the token indicates the start of a | |
9741 | function-definition. */ | |
9742 | if (cp_parser_token_starts_function_definition_p (token)) | |
9743 | { | |
9744 | if (!function_definition_allowed_p) | |
9745 | { | |
9746 | /* If a function-definition should not appear here, issue an | |
9747 | error message. */ | |
9748 | cp_parser_error (parser, | |
9749 | "a function-definition is not allowed here"); | |
9750 | return error_mark_node; | |
9751 | } | |
9752 | else | |
9753 | { | |
a723baf1 MM |
9754 | /* Neither attributes nor an asm-specification are allowed |
9755 | on a function-definition. */ | |
9756 | if (asm_specification) | |
9757 | error ("an asm-specification is not allowed on a function-definition"); | |
9758 | if (attributes) | |
9759 | error ("attributes are not allowed on a function-definition"); | |
9760 | /* This is a function-definition. */ | |
9761 | *function_definition_p = true; | |
9762 | ||
a723baf1 | 9763 | /* Parse the function definition. */ |
4bb8ca28 MM |
9764 | if (member_p) |
9765 | decl = cp_parser_save_member_function_body (parser, | |
9766 | decl_specifiers, | |
9767 | declarator, | |
9768 | prefix_attributes); | |
9769 | else | |
9770 | decl | |
9771 | = (cp_parser_function_definition_from_specifiers_and_declarator | |
9772 | (parser, decl_specifiers, prefix_attributes, declarator)); | |
24c0ef37 | 9773 | |
a723baf1 MM |
9774 | return decl; |
9775 | } | |
9776 | } | |
9777 | ||
9778 | /* [dcl.dcl] | |
9779 | ||
9780 | Only in function declarations for constructors, destructors, and | |
9781 | type conversions can the decl-specifier-seq be omitted. | |
9782 | ||
9783 | We explicitly postpone this check past the point where we handle | |
9784 | function-definitions because we tolerate function-definitions | |
9785 | that are missing their return types in some modes. */ | |
7efa3e22 | 9786 | if (!decl_specifiers && ctor_dtor_or_conv_p <= 0) |
a723baf1 MM |
9787 | { |
9788 | cp_parser_error (parser, | |
9789 | "expected constructor, destructor, or type conversion"); | |
9790 | return error_mark_node; | |
9791 | } | |
9792 | ||
9793 | /* An `=' or an `(' indicates an initializer. */ | |
9794 | is_initialized = (token->type == CPP_EQ | |
9795 | || token->type == CPP_OPEN_PAREN); | |
9796 | /* If the init-declarator isn't initialized and isn't followed by a | |
9797 | `,' or `;', it's not a valid init-declarator. */ | |
9798 | if (!is_initialized | |
9799 | && token->type != CPP_COMMA | |
9800 | && token->type != CPP_SEMICOLON) | |
9801 | { | |
9802 | cp_parser_error (parser, "expected init-declarator"); | |
9803 | return error_mark_node; | |
9804 | } | |
9805 | ||
9806 | /* Because start_decl has side-effects, we should only call it if we | |
9807 | know we're going ahead. By this point, we know that we cannot | |
9808 | possibly be looking at any other construct. */ | |
9809 | cp_parser_commit_to_tentative_parse (parser); | |
9810 | ||
9811 | /* Check to see whether or not this declaration is a friend. */ | |
9812 | friend_p = cp_parser_friend_p (decl_specifiers); | |
9813 | ||
9814 | /* Check that the number of template-parameter-lists is OK. */ | |
ee3071ef | 9815 | if (!cp_parser_check_declarator_template_parameters (parser, declarator)) |
cf22909c | 9816 | return error_mark_node; |
a723baf1 MM |
9817 | |
9818 | /* Enter the newly declared entry in the symbol table. If we're | |
9819 | processing a declaration in a class-specifier, we wait until | |
9820 | after processing the initializer. */ | |
9821 | if (!member_p) | |
9822 | { | |
9823 | if (parser->in_unbraced_linkage_specification_p) | |
9824 | { | |
9825 | decl_specifiers = tree_cons (error_mark_node, | |
9826 | get_identifier ("extern"), | |
9827 | decl_specifiers); | |
9828 | have_extern_spec = false; | |
9829 | } | |
ee3071ef NS |
9830 | decl = start_decl (declarator, decl_specifiers, |
9831 | is_initialized, attributes, prefix_attributes); | |
a723baf1 MM |
9832 | } |
9833 | ||
9834 | /* Enter the SCOPE. That way unqualified names appearing in the | |
9835 | initializer will be looked up in SCOPE. */ | |
9836 | if (scope) | |
9837 | push_scope (scope); | |
9838 | ||
9839 | /* Perform deferred access control checks, now that we know in which | |
9840 | SCOPE the declared entity resides. */ | |
9841 | if (!member_p && decl) | |
9842 | { | |
9843 | tree saved_current_function_decl = NULL_TREE; | |
9844 | ||
9845 | /* If the entity being declared is a function, pretend that we | |
9846 | are in its scope. If it is a `friend', it may have access to | |
9bcb9aae | 9847 | things that would not otherwise be accessible. */ |
a723baf1 MM |
9848 | if (TREE_CODE (decl) == FUNCTION_DECL) |
9849 | { | |
9850 | saved_current_function_decl = current_function_decl; | |
9851 | current_function_decl = decl; | |
9852 | } | |
9853 | ||
cf22909c KL |
9854 | /* Perform the access control checks for the declarator and the |
9855 | the decl-specifiers. */ | |
9856 | perform_deferred_access_checks (); | |
a723baf1 MM |
9857 | |
9858 | /* Restore the saved value. */ | |
9859 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
9860 | current_function_decl = saved_current_function_decl; | |
9861 | } | |
9862 | ||
9863 | /* Parse the initializer. */ | |
9864 | if (is_initialized) | |
39703eb9 MM |
9865 | initializer = cp_parser_initializer (parser, |
9866 | &is_parenthesized_init, | |
9867 | &is_non_constant_init); | |
a723baf1 MM |
9868 | else |
9869 | { | |
9870 | initializer = NULL_TREE; | |
9871 | is_parenthesized_init = false; | |
39703eb9 | 9872 | is_non_constant_init = true; |
a723baf1 MM |
9873 | } |
9874 | ||
9875 | /* The old parser allows attributes to appear after a parenthesized | |
9876 | initializer. Mark Mitchell proposed removing this functionality | |
9877 | on the GCC mailing lists on 2002-08-13. This parser accepts the | |
9878 | attributes -- but ignores them. */ | |
9879 | if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init) | |
9880 | if (cp_parser_attributes_opt (parser)) | |
9881 | warning ("attributes after parenthesized initializer ignored"); | |
9882 | ||
9883 | /* Leave the SCOPE, now that we have processed the initializer. It | |
9884 | is important to do this before calling cp_finish_decl because it | |
9885 | makes decisions about whether to create DECL_STMTs or not based | |
9886 | on the current scope. */ | |
9887 | if (scope) | |
9888 | pop_scope (scope); | |
9889 | ||
9890 | /* For an in-class declaration, use `grokfield' to create the | |
9891 | declaration. */ | |
9892 | if (member_p) | |
8db1028e NS |
9893 | { |
9894 | decl = grokfield (declarator, decl_specifiers, | |
9895 | initializer, /*asmspec=*/NULL_TREE, | |
a723baf1 | 9896 | /*attributes=*/NULL_TREE); |
8db1028e NS |
9897 | if (decl && TREE_CODE (decl) == FUNCTION_DECL) |
9898 | cp_parser_save_default_args (parser, decl); | |
9899 | } | |
9900 | ||
a723baf1 MM |
9901 | /* Finish processing the declaration. But, skip friend |
9902 | declarations. */ | |
9903 | if (!friend_p && decl) | |
9904 | cp_finish_decl (decl, | |
9905 | initializer, | |
9906 | asm_specification, | |
9907 | /* If the initializer is in parentheses, then this is | |
9908 | a direct-initialization, which means that an | |
9909 | `explicit' constructor is OK. Otherwise, an | |
9910 | `explicit' constructor cannot be used. */ | |
9911 | ((is_parenthesized_init || !is_initialized) | |
9912 | ? 0 : LOOKUP_ONLYCONVERTING)); | |
9913 | ||
39703eb9 MM |
9914 | /* Remember whether or not variables were initialized by |
9915 | constant-expressions. */ | |
9916 | if (decl && TREE_CODE (decl) == VAR_DECL | |
9917 | && is_initialized && !is_non_constant_init) | |
9918 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; | |
9919 | ||
a723baf1 MM |
9920 | return decl; |
9921 | } | |
9922 | ||
9923 | /* Parse a declarator. | |
9924 | ||
9925 | declarator: | |
9926 | direct-declarator | |
9927 | ptr-operator declarator | |
9928 | ||
9929 | abstract-declarator: | |
9930 | ptr-operator abstract-declarator [opt] | |
9931 | direct-abstract-declarator | |
9932 | ||
9933 | GNU Extensions: | |
9934 | ||
9935 | declarator: | |
9936 | attributes [opt] direct-declarator | |
9937 | attributes [opt] ptr-operator declarator | |
9938 | ||
9939 | abstract-declarator: | |
9940 | attributes [opt] ptr-operator abstract-declarator [opt] | |
9941 | attributes [opt] direct-abstract-declarator | |
9942 | ||
9943 | Returns a representation of the declarator. If the declarator has | |
9944 | the form `* declarator', then an INDIRECT_REF is returned, whose | |
34cd5ae7 | 9945 | only operand is the sub-declarator. Analogously, `& declarator' is |
a723baf1 MM |
9946 | represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is |
9947 | used. The first operand is the TYPE for `X'. The second operand | |
9948 | is an INDIRECT_REF whose operand is the sub-declarator. | |
9949 | ||
34cd5ae7 | 9950 | Otherwise, the representation is as for a direct-declarator. |
a723baf1 MM |
9951 | |
9952 | (It would be better to define a structure type to represent | |
9953 | declarators, rather than abusing `tree' nodes to represent | |
9954 | declarators. That would be much clearer and save some memory. | |
9955 | There is no reason for declarators to be garbage-collected, for | |
9956 | example; they are created during parser and no longer needed after | |
9957 | `grokdeclarator' has been called.) | |
9958 | ||
9959 | For a ptr-operator that has the optional cv-qualifier-seq, | |
9960 | cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF | |
9961 | node. | |
9962 | ||
7efa3e22 NS |
9963 | If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to |
9964 | detect constructor, destructor or conversion operators. It is set | |
9965 | to -1 if the declarator is a name, and +1 if it is a | |
9966 | function. Otherwise it is set to zero. Usually you just want to | |
9967 | test for >0, but internally the negative value is used. | |
9968 | ||
a723baf1 MM |
9969 | (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have |
9970 | a decl-specifier-seq unless it declares a constructor, destructor, | |
9971 | or conversion. It might seem that we could check this condition in | |
9972 | semantic analysis, rather than parsing, but that makes it difficult | |
9973 | to handle something like `f()'. We want to notice that there are | |
9974 | no decl-specifiers, and therefore realize that this is an | |
4bb8ca28 MM |
9975 | expression, not a declaration.) |
9976 | ||
9977 | If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff | |
9978 | the declarator is a direct-declarator of the form "(...)". */ | |
a723baf1 MM |
9979 | |
9980 | static tree | |
94edc4ab NN |
9981 | cp_parser_declarator (cp_parser* parser, |
9982 | cp_parser_declarator_kind dcl_kind, | |
4bb8ca28 MM |
9983 | int* ctor_dtor_or_conv_p, |
9984 | bool* parenthesized_p) | |
a723baf1 MM |
9985 | { |
9986 | cp_token *token; | |
9987 | tree declarator; | |
9988 | enum tree_code code; | |
9989 | tree cv_qualifier_seq; | |
9990 | tree class_type; | |
9991 | tree attributes = NULL_TREE; | |
9992 | ||
9993 | /* Assume this is not a constructor, destructor, or type-conversion | |
9994 | operator. */ | |
9995 | if (ctor_dtor_or_conv_p) | |
7efa3e22 | 9996 | *ctor_dtor_or_conv_p = 0; |
a723baf1 MM |
9997 | |
9998 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
9999 | attributes = cp_parser_attributes_opt (parser); | |
10000 | ||
10001 | /* Peek at the next token. */ | |
10002 | token = cp_lexer_peek_token (parser->lexer); | |
10003 | ||
10004 | /* Check for the ptr-operator production. */ | |
10005 | cp_parser_parse_tentatively (parser); | |
10006 | /* Parse the ptr-operator. */ | |
10007 | code = cp_parser_ptr_operator (parser, | |
10008 | &class_type, | |
10009 | &cv_qualifier_seq); | |
10010 | /* If that worked, then we have a ptr-operator. */ | |
10011 | if (cp_parser_parse_definitely (parser)) | |
10012 | { | |
4bb8ca28 MM |
10013 | /* If a ptr-operator was found, then this declarator was not |
10014 | parenthesized. */ | |
10015 | if (parenthesized_p) | |
10016 | *parenthesized_p = true; | |
a723baf1 MM |
10017 | /* The dependent declarator is optional if we are parsing an |
10018 | abstract-declarator. */ | |
62b8a44e | 10019 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
a723baf1 MM |
10020 | cp_parser_parse_tentatively (parser); |
10021 | ||
10022 | /* Parse the dependent declarator. */ | |
62b8a44e | 10023 | declarator = cp_parser_declarator (parser, dcl_kind, |
4bb8ca28 MM |
10024 | /*ctor_dtor_or_conv_p=*/NULL, |
10025 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
10026 | |
10027 | /* If we are parsing an abstract-declarator, we must handle the | |
10028 | case where the dependent declarator is absent. */ | |
62b8a44e NS |
10029 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED |
10030 | && !cp_parser_parse_definitely (parser)) | |
a723baf1 MM |
10031 | declarator = NULL_TREE; |
10032 | ||
10033 | /* Build the representation of the ptr-operator. */ | |
10034 | if (code == INDIRECT_REF) | |
10035 | declarator = make_pointer_declarator (cv_qualifier_seq, | |
10036 | declarator); | |
10037 | else | |
10038 | declarator = make_reference_declarator (cv_qualifier_seq, | |
10039 | declarator); | |
10040 | /* Handle the pointer-to-member case. */ | |
10041 | if (class_type) | |
10042 | declarator = build_nt (SCOPE_REF, class_type, declarator); | |
10043 | } | |
10044 | /* Everything else is a direct-declarator. */ | |
10045 | else | |
4bb8ca28 MM |
10046 | { |
10047 | if (parenthesized_p) | |
10048 | *parenthesized_p = cp_lexer_next_token_is (parser->lexer, | |
10049 | CPP_OPEN_PAREN); | |
10050 | declarator = cp_parser_direct_declarator (parser, dcl_kind, | |
10051 | ctor_dtor_or_conv_p); | |
10052 | } | |
a723baf1 MM |
10053 | |
10054 | if (attributes && declarator != error_mark_node) | |
10055 | declarator = tree_cons (attributes, declarator, NULL_TREE); | |
10056 | ||
10057 | return declarator; | |
10058 | } | |
10059 | ||
10060 | /* Parse a direct-declarator or direct-abstract-declarator. | |
10061 | ||
10062 | direct-declarator: | |
10063 | declarator-id | |
10064 | direct-declarator ( parameter-declaration-clause ) | |
10065 | cv-qualifier-seq [opt] | |
10066 | exception-specification [opt] | |
10067 | direct-declarator [ constant-expression [opt] ] | |
10068 | ( declarator ) | |
10069 | ||
10070 | direct-abstract-declarator: | |
10071 | direct-abstract-declarator [opt] | |
10072 | ( parameter-declaration-clause ) | |
10073 | cv-qualifier-seq [opt] | |
10074 | exception-specification [opt] | |
10075 | direct-abstract-declarator [opt] [ constant-expression [opt] ] | |
10076 | ( abstract-declarator ) | |
10077 | ||
62b8a44e NS |
10078 | Returns a representation of the declarator. DCL_KIND is |
10079 | CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a | |
10080 | direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if | |
10081 | we are parsing a direct-declarator. It is | |
10082 | CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case | |
10083 | of ambiguity we prefer an abstract declarator, as per | |
10084 | [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for | |
a723baf1 MM |
10085 | cp_parser_declarator. |
10086 | ||
10087 | For the declarator-id production, the representation is as for an | |
10088 | id-expression, except that a qualified name is represented as a | |
10089 | SCOPE_REF. A function-declarator is represented as a CALL_EXPR; | |
10090 | see the documentation of the FUNCTION_DECLARATOR_* macros for | |
10091 | information about how to find the various declarator components. | |
10092 | An array-declarator is represented as an ARRAY_REF. The | |
10093 | direct-declarator is the first operand; the constant-expression | |
10094 | indicating the size of the array is the second operand. */ | |
10095 | ||
10096 | static tree | |
94edc4ab NN |
10097 | cp_parser_direct_declarator (cp_parser* parser, |
10098 | cp_parser_declarator_kind dcl_kind, | |
7efa3e22 | 10099 | int* ctor_dtor_or_conv_p) |
a723baf1 MM |
10100 | { |
10101 | cp_token *token; | |
62b8a44e | 10102 | tree declarator = NULL_TREE; |
a723baf1 MM |
10103 | tree scope = NULL_TREE; |
10104 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
10105 | bool saved_in_declarator_p = parser->in_declarator_p; | |
62b8a44e NS |
10106 | bool first = true; |
10107 | ||
10108 | while (true) | |
a723baf1 | 10109 | { |
62b8a44e NS |
10110 | /* Peek at the next token. */ |
10111 | token = cp_lexer_peek_token (parser->lexer); | |
10112 | if (token->type == CPP_OPEN_PAREN) | |
a723baf1 | 10113 | { |
62b8a44e NS |
10114 | /* This is either a parameter-declaration-clause, or a |
10115 | parenthesized declarator. When we know we are parsing a | |
34cd5ae7 | 10116 | named declarator, it must be a parenthesized declarator |
62b8a44e NS |
10117 | if FIRST is true. For instance, `(int)' is a |
10118 | parameter-declaration-clause, with an omitted | |
10119 | direct-abstract-declarator. But `((*))', is a | |
10120 | parenthesized abstract declarator. Finally, when T is a | |
10121 | template parameter `(T)' is a | |
34cd5ae7 | 10122 | parameter-declaration-clause, and not a parenthesized |
62b8a44e | 10123 | named declarator. |
a723baf1 | 10124 | |
62b8a44e NS |
10125 | We first try and parse a parameter-declaration-clause, |
10126 | and then try a nested declarator (if FIRST is true). | |
a723baf1 | 10127 | |
62b8a44e NS |
10128 | It is not an error for it not to be a |
10129 | parameter-declaration-clause, even when FIRST is | |
10130 | false. Consider, | |
10131 | ||
10132 | int i (int); | |
10133 | int i (3); | |
10134 | ||
10135 | The first is the declaration of a function while the | |
10136 | second is a the definition of a variable, including its | |
10137 | initializer. | |
10138 | ||
10139 | Having seen only the parenthesis, we cannot know which of | |
10140 | these two alternatives should be selected. Even more | |
10141 | complex are examples like: | |
10142 | ||
10143 | int i (int (a)); | |
10144 | int i (int (3)); | |
10145 | ||
10146 | The former is a function-declaration; the latter is a | |
10147 | variable initialization. | |
10148 | ||
34cd5ae7 | 10149 | Thus again, we try a parameter-declaration-clause, and if |
62b8a44e NS |
10150 | that fails, we back out and return. */ |
10151 | ||
10152 | if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) | |
a723baf1 | 10153 | { |
62b8a44e | 10154 | tree params; |
4047b164 | 10155 | unsigned saved_num_template_parameter_lists; |
62b8a44e NS |
10156 | |
10157 | cp_parser_parse_tentatively (parser); | |
a723baf1 | 10158 | |
62b8a44e NS |
10159 | /* Consume the `('. */ |
10160 | cp_lexer_consume_token (parser->lexer); | |
10161 | if (first) | |
10162 | { | |
10163 | /* If this is going to be an abstract declarator, we're | |
10164 | in a declarator and we can't have default args. */ | |
10165 | parser->default_arg_ok_p = false; | |
10166 | parser->in_declarator_p = true; | |
10167 | } | |
10168 | ||
4047b164 KL |
10169 | /* Inside the function parameter list, surrounding |
10170 | template-parameter-lists do not apply. */ | |
10171 | saved_num_template_parameter_lists | |
10172 | = parser->num_template_parameter_lists; | |
10173 | parser->num_template_parameter_lists = 0; | |
10174 | ||
62b8a44e NS |
10175 | /* Parse the parameter-declaration-clause. */ |
10176 | params = cp_parser_parameter_declaration_clause (parser); | |
10177 | ||
4047b164 KL |
10178 | parser->num_template_parameter_lists |
10179 | = saved_num_template_parameter_lists; | |
10180 | ||
62b8a44e | 10181 | /* If all went well, parse the cv-qualifier-seq and the |
34cd5ae7 | 10182 | exception-specification. */ |
62b8a44e NS |
10183 | if (cp_parser_parse_definitely (parser)) |
10184 | { | |
10185 | tree cv_qualifiers; | |
10186 | tree exception_specification; | |
7efa3e22 NS |
10187 | |
10188 | if (ctor_dtor_or_conv_p) | |
10189 | *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0; | |
62b8a44e NS |
10190 | first = false; |
10191 | /* Consume the `)'. */ | |
10192 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
10193 | ||
10194 | /* Parse the cv-qualifier-seq. */ | |
10195 | cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser); | |
10196 | /* And the exception-specification. */ | |
10197 | exception_specification | |
10198 | = cp_parser_exception_specification_opt (parser); | |
10199 | ||
10200 | /* Create the function-declarator. */ | |
10201 | declarator = make_call_declarator (declarator, | |
10202 | params, | |
10203 | cv_qualifiers, | |
10204 | exception_specification); | |
10205 | /* Any subsequent parameter lists are to do with | |
10206 | return type, so are not those of the declared | |
10207 | function. */ | |
10208 | parser->default_arg_ok_p = false; | |
10209 | ||
10210 | /* Repeat the main loop. */ | |
10211 | continue; | |
10212 | } | |
10213 | } | |
10214 | ||
10215 | /* If this is the first, we can try a parenthesized | |
10216 | declarator. */ | |
10217 | if (first) | |
a723baf1 | 10218 | { |
a723baf1 | 10219 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
62b8a44e NS |
10220 | parser->in_declarator_p = saved_in_declarator_p; |
10221 | ||
10222 | /* Consume the `('. */ | |
10223 | cp_lexer_consume_token (parser->lexer); | |
10224 | /* Parse the nested declarator. */ | |
10225 | declarator | |
4bb8ca28 MM |
10226 | = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p, |
10227 | /*parenthesized_p=*/NULL); | |
62b8a44e NS |
10228 | first = false; |
10229 | /* Expect a `)'. */ | |
10230 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
10231 | declarator = error_mark_node; | |
10232 | if (declarator == error_mark_node) | |
10233 | break; | |
10234 | ||
10235 | goto handle_declarator; | |
a723baf1 | 10236 | } |
9bcb9aae | 10237 | /* Otherwise, we must be done. */ |
62b8a44e NS |
10238 | else |
10239 | break; | |
a723baf1 | 10240 | } |
62b8a44e NS |
10241 | else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
10242 | && token->type == CPP_OPEN_SQUARE) | |
a723baf1 | 10243 | { |
62b8a44e | 10244 | /* Parse an array-declarator. */ |
a723baf1 MM |
10245 | tree bounds; |
10246 | ||
7efa3e22 NS |
10247 | if (ctor_dtor_or_conv_p) |
10248 | *ctor_dtor_or_conv_p = 0; | |
10249 | ||
62b8a44e NS |
10250 | first = false; |
10251 | parser->default_arg_ok_p = false; | |
10252 | parser->in_declarator_p = true; | |
a723baf1 MM |
10253 | /* Consume the `['. */ |
10254 | cp_lexer_consume_token (parser->lexer); | |
10255 | /* Peek at the next token. */ | |
10256 | token = cp_lexer_peek_token (parser->lexer); | |
10257 | /* If the next token is `]', then there is no | |
10258 | constant-expression. */ | |
10259 | if (token->type != CPP_CLOSE_SQUARE) | |
14d22dd6 MM |
10260 | { |
10261 | bool non_constant_p; | |
10262 | ||
10263 | bounds | |
10264 | = cp_parser_constant_expression (parser, | |
10265 | /*allow_non_constant=*/true, | |
10266 | &non_constant_p); | |
d17811fd MM |
10267 | if (!non_constant_p) |
10268 | bounds = cp_parser_fold_non_dependent_expr (bounds); | |
14d22dd6 | 10269 | } |
a723baf1 MM |
10270 | else |
10271 | bounds = NULL_TREE; | |
10272 | /* Look for the closing `]'. */ | |
62b8a44e NS |
10273 | if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'")) |
10274 | { | |
10275 | declarator = error_mark_node; | |
10276 | break; | |
10277 | } | |
a723baf1 MM |
10278 | |
10279 | declarator = build_nt (ARRAY_REF, declarator, bounds); | |
10280 | } | |
62b8a44e | 10281 | else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT) |
a723baf1 | 10282 | { |
a668c6ad | 10283 | /* Parse a declarator-id */ |
62b8a44e NS |
10284 | if (dcl_kind == CP_PARSER_DECLARATOR_EITHER) |
10285 | cp_parser_parse_tentatively (parser); | |
10286 | declarator = cp_parser_declarator_id (parser); | |
712becab NS |
10287 | if (dcl_kind == CP_PARSER_DECLARATOR_EITHER) |
10288 | { | |
10289 | if (!cp_parser_parse_definitely (parser)) | |
10290 | declarator = error_mark_node; | |
10291 | else if (TREE_CODE (declarator) != IDENTIFIER_NODE) | |
10292 | { | |
10293 | cp_parser_error (parser, "expected unqualified-id"); | |
10294 | declarator = error_mark_node; | |
10295 | } | |
10296 | } | |
10297 | ||
62b8a44e NS |
10298 | if (declarator == error_mark_node) |
10299 | break; | |
a723baf1 | 10300 | |
d9a50301 KL |
10301 | if (TREE_CODE (declarator) == SCOPE_REF |
10302 | && !current_scope ()) | |
62b8a44e NS |
10303 | { |
10304 | tree scope = TREE_OPERAND (declarator, 0); | |
712becab | 10305 | |
62b8a44e NS |
10306 | /* In the declaration of a member of a template class |
10307 | outside of the class itself, the SCOPE will sometimes | |
10308 | be a TYPENAME_TYPE. For example, given: | |
10309 | ||
10310 | template <typename T> | |
10311 | int S<T>::R::i = 3; | |
10312 | ||
10313 | the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In | |
10314 | this context, we must resolve S<T>::R to an ordinary | |
10315 | type, rather than a typename type. | |
10316 | ||
10317 | The reason we normally avoid resolving TYPENAME_TYPEs | |
10318 | is that a specialization of `S' might render | |
10319 | `S<T>::R' not a type. However, if `S' is | |
10320 | specialized, then this `i' will not be used, so there | |
10321 | is no harm in resolving the types here. */ | |
10322 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
10323 | { | |
14d22dd6 MM |
10324 | tree type; |
10325 | ||
62b8a44e | 10326 | /* Resolve the TYPENAME_TYPE. */ |
14d22dd6 MM |
10327 | type = resolve_typename_type (scope, |
10328 | /*only_current_p=*/false); | |
62b8a44e | 10329 | /* If that failed, the declarator is invalid. */ |
14d22dd6 MM |
10330 | if (type != error_mark_node) |
10331 | scope = type; | |
62b8a44e NS |
10332 | /* Build a new DECLARATOR. */ |
10333 | declarator = build_nt (SCOPE_REF, | |
10334 | scope, | |
10335 | TREE_OPERAND (declarator, 1)); | |
10336 | } | |
10337 | } | |
10338 | ||
10339 | /* Check to see whether the declarator-id names a constructor, | |
10340 | destructor, or conversion. */ | |
10341 | if (declarator && ctor_dtor_or_conv_p | |
10342 | && ((TREE_CODE (declarator) == SCOPE_REF | |
10343 | && CLASS_TYPE_P (TREE_OPERAND (declarator, 0))) | |
10344 | || (TREE_CODE (declarator) != SCOPE_REF | |
10345 | && at_class_scope_p ()))) | |
a723baf1 | 10346 | { |
62b8a44e NS |
10347 | tree unqualified_name; |
10348 | tree class_type; | |
10349 | ||
10350 | /* Get the unqualified part of the name. */ | |
10351 | if (TREE_CODE (declarator) == SCOPE_REF) | |
10352 | { | |
10353 | class_type = TREE_OPERAND (declarator, 0); | |
10354 | unqualified_name = TREE_OPERAND (declarator, 1); | |
10355 | } | |
10356 | else | |
10357 | { | |
10358 | class_type = current_class_type; | |
10359 | unqualified_name = declarator; | |
10360 | } | |
10361 | ||
10362 | /* See if it names ctor, dtor or conv. */ | |
10363 | if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR | |
10364 | || IDENTIFIER_TYPENAME_P (unqualified_name) | |
10365 | || constructor_name_p (unqualified_name, class_type)) | |
7efa3e22 | 10366 | *ctor_dtor_or_conv_p = -1; |
a723baf1 | 10367 | } |
62b8a44e NS |
10368 | |
10369 | handle_declarator:; | |
10370 | scope = get_scope_of_declarator (declarator); | |
10371 | if (scope) | |
10372 | /* Any names that appear after the declarator-id for a member | |
10373 | are looked up in the containing scope. */ | |
10374 | push_scope (scope); | |
10375 | parser->in_declarator_p = true; | |
10376 | if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p) | |
10377 | || (declarator | |
10378 | && (TREE_CODE (declarator) == SCOPE_REF | |
10379 | || TREE_CODE (declarator) == IDENTIFIER_NODE))) | |
10380 | /* Default args are only allowed on function | |
10381 | declarations. */ | |
10382 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
a723baf1 | 10383 | else |
62b8a44e NS |
10384 | parser->default_arg_ok_p = false; |
10385 | ||
10386 | first = false; | |
a723baf1 | 10387 | } |
62b8a44e | 10388 | /* We're done. */ |
a723baf1 MM |
10389 | else |
10390 | break; | |
a723baf1 MM |
10391 | } |
10392 | ||
10393 | /* For an abstract declarator, we might wind up with nothing at this | |
10394 | point. That's an error; the declarator is not optional. */ | |
10395 | if (!declarator) | |
10396 | cp_parser_error (parser, "expected declarator"); | |
10397 | ||
10398 | /* If we entered a scope, we must exit it now. */ | |
10399 | if (scope) | |
10400 | pop_scope (scope); | |
10401 | ||
10402 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
10403 | parser->in_declarator_p = saved_in_declarator_p; | |
10404 | ||
10405 | return declarator; | |
10406 | } | |
10407 | ||
10408 | /* Parse a ptr-operator. | |
10409 | ||
10410 | ptr-operator: | |
10411 | * cv-qualifier-seq [opt] | |
10412 | & | |
10413 | :: [opt] nested-name-specifier * cv-qualifier-seq [opt] | |
10414 | ||
10415 | GNU Extension: | |
10416 | ||
10417 | ptr-operator: | |
10418 | & cv-qualifier-seq [opt] | |
10419 | ||
10420 | Returns INDIRECT_REF if a pointer, or pointer-to-member, was | |
10421 | used. Returns ADDR_EXPR if a reference was used. In the | |
10422 | case of a pointer-to-member, *TYPE is filled in with the | |
10423 | TYPE containing the member. *CV_QUALIFIER_SEQ is filled in | |
10424 | with the cv-qualifier-seq, or NULL_TREE, if there are no | |
10425 | cv-qualifiers. Returns ERROR_MARK if an error occurred. */ | |
10426 | ||
10427 | static enum tree_code | |
94edc4ab NN |
10428 | cp_parser_ptr_operator (cp_parser* parser, |
10429 | tree* type, | |
10430 | tree* cv_qualifier_seq) | |
a723baf1 MM |
10431 | { |
10432 | enum tree_code code = ERROR_MARK; | |
10433 | cp_token *token; | |
10434 | ||
10435 | /* Assume that it's not a pointer-to-member. */ | |
10436 | *type = NULL_TREE; | |
10437 | /* And that there are no cv-qualifiers. */ | |
10438 | *cv_qualifier_seq = NULL_TREE; | |
10439 | ||
10440 | /* Peek at the next token. */ | |
10441 | token = cp_lexer_peek_token (parser->lexer); | |
10442 | /* If it's a `*' or `&' we have a pointer or reference. */ | |
10443 | if (token->type == CPP_MULT || token->type == CPP_AND) | |
10444 | { | |
10445 | /* Remember which ptr-operator we were processing. */ | |
10446 | code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF); | |
10447 | ||
10448 | /* Consume the `*' or `&'. */ | |
10449 | cp_lexer_consume_token (parser->lexer); | |
10450 | ||
10451 | /* A `*' can be followed by a cv-qualifier-seq, and so can a | |
10452 | `&', if we are allowing GNU extensions. (The only qualifier | |
10453 | that can legally appear after `&' is `restrict', but that is | |
10454 | enforced during semantic analysis. */ | |
10455 | if (code == INDIRECT_REF | |
10456 | || cp_parser_allow_gnu_extensions_p (parser)) | |
10457 | *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser); | |
10458 | } | |
10459 | else | |
10460 | { | |
10461 | /* Try the pointer-to-member case. */ | |
10462 | cp_parser_parse_tentatively (parser); | |
10463 | /* Look for the optional `::' operator. */ | |
10464 | cp_parser_global_scope_opt (parser, | |
10465 | /*current_scope_valid_p=*/false); | |
10466 | /* Look for the nested-name specifier. */ | |
10467 | cp_parser_nested_name_specifier (parser, | |
10468 | /*typename_keyword_p=*/false, | |
10469 | /*check_dependency_p=*/true, | |
a668c6ad MM |
10470 | /*type_p=*/false, |
10471 | /*is_declaration=*/false); | |
a723baf1 MM |
10472 | /* If we found it, and the next token is a `*', then we are |
10473 | indeed looking at a pointer-to-member operator. */ | |
10474 | if (!cp_parser_error_occurred (parser) | |
10475 | && cp_parser_require (parser, CPP_MULT, "`*'")) | |
10476 | { | |
10477 | /* The type of which the member is a member is given by the | |
10478 | current SCOPE. */ | |
10479 | *type = parser->scope; | |
10480 | /* The next name will not be qualified. */ | |
10481 | parser->scope = NULL_TREE; | |
10482 | parser->qualifying_scope = NULL_TREE; | |
10483 | parser->object_scope = NULL_TREE; | |
10484 | /* Indicate that the `*' operator was used. */ | |
10485 | code = INDIRECT_REF; | |
10486 | /* Look for the optional cv-qualifier-seq. */ | |
10487 | *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser); | |
10488 | } | |
10489 | /* If that didn't work we don't have a ptr-operator. */ | |
10490 | if (!cp_parser_parse_definitely (parser)) | |
10491 | cp_parser_error (parser, "expected ptr-operator"); | |
10492 | } | |
10493 | ||
10494 | return code; | |
10495 | } | |
10496 | ||
10497 | /* Parse an (optional) cv-qualifier-seq. | |
10498 | ||
10499 | cv-qualifier-seq: | |
10500 | cv-qualifier cv-qualifier-seq [opt] | |
10501 | ||
10502 | Returns a TREE_LIST. The TREE_VALUE of each node is the | |
10503 | representation of a cv-qualifier. */ | |
10504 | ||
10505 | static tree | |
94edc4ab | 10506 | cp_parser_cv_qualifier_seq_opt (cp_parser* parser) |
a723baf1 MM |
10507 | { |
10508 | tree cv_qualifiers = NULL_TREE; | |
10509 | ||
10510 | while (true) | |
10511 | { | |
10512 | tree cv_qualifier; | |
10513 | ||
10514 | /* Look for the next cv-qualifier. */ | |
10515 | cv_qualifier = cp_parser_cv_qualifier_opt (parser); | |
10516 | /* If we didn't find one, we're done. */ | |
10517 | if (!cv_qualifier) | |
10518 | break; | |
10519 | ||
10520 | /* Add this cv-qualifier to the list. */ | |
10521 | cv_qualifiers | |
10522 | = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers); | |
10523 | } | |
10524 | ||
10525 | /* We built up the list in reverse order. */ | |
10526 | return nreverse (cv_qualifiers); | |
10527 | } | |
10528 | ||
10529 | /* Parse an (optional) cv-qualifier. | |
10530 | ||
10531 | cv-qualifier: | |
10532 | const | |
10533 | volatile | |
10534 | ||
10535 | GNU Extension: | |
10536 | ||
10537 | cv-qualifier: | |
10538 | __restrict__ */ | |
10539 | ||
10540 | static tree | |
94edc4ab | 10541 | cp_parser_cv_qualifier_opt (cp_parser* parser) |
a723baf1 MM |
10542 | { |
10543 | cp_token *token; | |
10544 | tree cv_qualifier = NULL_TREE; | |
10545 | ||
10546 | /* Peek at the next token. */ | |
10547 | token = cp_lexer_peek_token (parser->lexer); | |
10548 | /* See if it's a cv-qualifier. */ | |
10549 | switch (token->keyword) | |
10550 | { | |
10551 | case RID_CONST: | |
10552 | case RID_VOLATILE: | |
10553 | case RID_RESTRICT: | |
10554 | /* Save the value of the token. */ | |
10555 | cv_qualifier = token->value; | |
10556 | /* Consume the token. */ | |
10557 | cp_lexer_consume_token (parser->lexer); | |
10558 | break; | |
10559 | ||
10560 | default: | |
10561 | break; | |
10562 | } | |
10563 | ||
10564 | return cv_qualifier; | |
10565 | } | |
10566 | ||
10567 | /* Parse a declarator-id. | |
10568 | ||
10569 | declarator-id: | |
10570 | id-expression | |
10571 | :: [opt] nested-name-specifier [opt] type-name | |
10572 | ||
10573 | In the `id-expression' case, the value returned is as for | |
10574 | cp_parser_id_expression if the id-expression was an unqualified-id. | |
10575 | If the id-expression was a qualified-id, then a SCOPE_REF is | |
10576 | returned. The first operand is the scope (either a NAMESPACE_DECL | |
10577 | or TREE_TYPE), but the second is still just a representation of an | |
10578 | unqualified-id. */ | |
10579 | ||
10580 | static tree | |
94edc4ab | 10581 | cp_parser_declarator_id (cp_parser* parser) |
a723baf1 MM |
10582 | { |
10583 | tree id_expression; | |
10584 | ||
10585 | /* The expression must be an id-expression. Assume that qualified | |
10586 | names are the names of types so that: | |
10587 | ||
10588 | template <class T> | |
10589 | int S<T>::R::i = 3; | |
10590 | ||
10591 | will work; we must treat `S<T>::R' as the name of a type. | |
10592 | Similarly, assume that qualified names are templates, where | |
10593 | required, so that: | |
10594 | ||
10595 | template <class T> | |
10596 | int S<T>::R<T>::i = 3; | |
10597 | ||
10598 | will work, too. */ | |
10599 | id_expression = cp_parser_id_expression (parser, | |
10600 | /*template_keyword_p=*/false, | |
10601 | /*check_dependency_p=*/false, | |
f3c2dfc6 MM |
10602 | /*template_p=*/NULL, |
10603 | /*declarator_p=*/true); | |
a723baf1 MM |
10604 | /* If the name was qualified, create a SCOPE_REF to represent |
10605 | that. */ | |
10606 | if (parser->scope) | |
ec20aa6c MM |
10607 | { |
10608 | id_expression = build_nt (SCOPE_REF, parser->scope, id_expression); | |
10609 | parser->scope = NULL_TREE; | |
10610 | } | |
a723baf1 MM |
10611 | |
10612 | return id_expression; | |
10613 | } | |
10614 | ||
10615 | /* Parse a type-id. | |
10616 | ||
10617 | type-id: | |
10618 | type-specifier-seq abstract-declarator [opt] | |
10619 | ||
10620 | Returns the TYPE specified. */ | |
10621 | ||
10622 | static tree | |
94edc4ab | 10623 | cp_parser_type_id (cp_parser* parser) |
a723baf1 MM |
10624 | { |
10625 | tree type_specifier_seq; | |
10626 | tree abstract_declarator; | |
10627 | ||
10628 | /* Parse the type-specifier-seq. */ | |
10629 | type_specifier_seq | |
10630 | = cp_parser_type_specifier_seq (parser); | |
10631 | if (type_specifier_seq == error_mark_node) | |
10632 | return error_mark_node; | |
10633 | ||
10634 | /* There might or might not be an abstract declarator. */ | |
10635 | cp_parser_parse_tentatively (parser); | |
10636 | /* Look for the declarator. */ | |
10637 | abstract_declarator | |
4bb8ca28 MM |
10638 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL, |
10639 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
10640 | /* Check to see if there really was a declarator. */ |
10641 | if (!cp_parser_parse_definitely (parser)) | |
10642 | abstract_declarator = NULL_TREE; | |
10643 | ||
10644 | return groktypename (build_tree_list (type_specifier_seq, | |
10645 | abstract_declarator)); | |
10646 | } | |
10647 | ||
10648 | /* Parse a type-specifier-seq. | |
10649 | ||
10650 | type-specifier-seq: | |
10651 | type-specifier type-specifier-seq [opt] | |
10652 | ||
10653 | GNU extension: | |
10654 | ||
10655 | type-specifier-seq: | |
10656 | attributes type-specifier-seq [opt] | |
10657 | ||
10658 | Returns a TREE_LIST. Either the TREE_VALUE of each node is a | |
10659 | type-specifier, or the TREE_PURPOSE is a list of attributes. */ | |
10660 | ||
10661 | static tree | |
94edc4ab | 10662 | cp_parser_type_specifier_seq (cp_parser* parser) |
a723baf1 MM |
10663 | { |
10664 | bool seen_type_specifier = false; | |
10665 | tree type_specifier_seq = NULL_TREE; | |
10666 | ||
10667 | /* Parse the type-specifiers and attributes. */ | |
10668 | while (true) | |
10669 | { | |
10670 | tree type_specifier; | |
10671 | ||
10672 | /* Check for attributes first. */ | |
10673 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) | |
10674 | { | |
10675 | type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser), | |
10676 | NULL_TREE, | |
10677 | type_specifier_seq); | |
10678 | continue; | |
10679 | } | |
10680 | ||
10681 | /* After the first type-specifier, others are optional. */ | |
10682 | if (seen_type_specifier) | |
10683 | cp_parser_parse_tentatively (parser); | |
10684 | /* Look for the type-specifier. */ | |
10685 | type_specifier = cp_parser_type_specifier (parser, | |
10686 | CP_PARSER_FLAGS_NONE, | |
10687 | /*is_friend=*/false, | |
10688 | /*is_declaration=*/false, | |
10689 | NULL, | |
10690 | NULL); | |
10691 | /* If the first type-specifier could not be found, this is not a | |
10692 | type-specifier-seq at all. */ | |
10693 | if (!seen_type_specifier && type_specifier == error_mark_node) | |
10694 | return error_mark_node; | |
10695 | /* If subsequent type-specifiers could not be found, the | |
10696 | type-specifier-seq is complete. */ | |
10697 | else if (seen_type_specifier && !cp_parser_parse_definitely (parser)) | |
10698 | break; | |
10699 | ||
10700 | /* Add the new type-specifier to the list. */ | |
10701 | type_specifier_seq | |
10702 | = tree_cons (NULL_TREE, type_specifier, type_specifier_seq); | |
10703 | seen_type_specifier = true; | |
10704 | } | |
10705 | ||
10706 | /* We built up the list in reverse order. */ | |
10707 | return nreverse (type_specifier_seq); | |
10708 | } | |
10709 | ||
10710 | /* Parse a parameter-declaration-clause. | |
10711 | ||
10712 | parameter-declaration-clause: | |
10713 | parameter-declaration-list [opt] ... [opt] | |
10714 | parameter-declaration-list , ... | |
10715 | ||
10716 | Returns a representation for the parameter declarations. Each node | |
10717 | is a TREE_LIST. (See cp_parser_parameter_declaration for the exact | |
10718 | representation.) If the parameter-declaration-clause ends with an | |
10719 | ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the | |
10720 | list. A return value of NULL_TREE indicates a | |
10721 | parameter-declaration-clause consisting only of an ellipsis. */ | |
10722 | ||
10723 | static tree | |
94edc4ab | 10724 | cp_parser_parameter_declaration_clause (cp_parser* parser) |
a723baf1 MM |
10725 | { |
10726 | tree parameters; | |
10727 | cp_token *token; | |
10728 | bool ellipsis_p; | |
10729 | ||
10730 | /* Peek at the next token. */ | |
10731 | token = cp_lexer_peek_token (parser->lexer); | |
10732 | /* Check for trivial parameter-declaration-clauses. */ | |
10733 | if (token->type == CPP_ELLIPSIS) | |
10734 | { | |
10735 | /* Consume the `...' token. */ | |
10736 | cp_lexer_consume_token (parser->lexer); | |
10737 | return NULL_TREE; | |
10738 | } | |
10739 | else if (token->type == CPP_CLOSE_PAREN) | |
10740 | /* There are no parameters. */ | |
c73aecdf DE |
10741 | { |
10742 | #ifndef NO_IMPLICIT_EXTERN_C | |
10743 | if (in_system_header && current_class_type == NULL | |
10744 | && current_lang_name == lang_name_c) | |
10745 | return NULL_TREE; | |
10746 | else | |
10747 | #endif | |
10748 | return void_list_node; | |
10749 | } | |
a723baf1 MM |
10750 | /* Check for `(void)', too, which is a special case. */ |
10751 | else if (token->keyword == RID_VOID | |
10752 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
10753 | == CPP_CLOSE_PAREN)) | |
10754 | { | |
10755 | /* Consume the `void' token. */ | |
10756 | cp_lexer_consume_token (parser->lexer); | |
10757 | /* There are no parameters. */ | |
10758 | return void_list_node; | |
10759 | } | |
10760 | ||
10761 | /* Parse the parameter-declaration-list. */ | |
10762 | parameters = cp_parser_parameter_declaration_list (parser); | |
10763 | /* If a parse error occurred while parsing the | |
10764 | parameter-declaration-list, then the entire | |
10765 | parameter-declaration-clause is erroneous. */ | |
10766 | if (parameters == error_mark_node) | |
10767 | return error_mark_node; | |
10768 | ||
10769 | /* Peek at the next token. */ | |
10770 | token = cp_lexer_peek_token (parser->lexer); | |
10771 | /* If it's a `,', the clause should terminate with an ellipsis. */ | |
10772 | if (token->type == CPP_COMMA) | |
10773 | { | |
10774 | /* Consume the `,'. */ | |
10775 | cp_lexer_consume_token (parser->lexer); | |
10776 | /* Expect an ellipsis. */ | |
10777 | ellipsis_p | |
10778 | = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL); | |
10779 | } | |
10780 | /* It might also be `...' if the optional trailing `,' was | |
10781 | omitted. */ | |
10782 | else if (token->type == CPP_ELLIPSIS) | |
10783 | { | |
10784 | /* Consume the `...' token. */ | |
10785 | cp_lexer_consume_token (parser->lexer); | |
10786 | /* And remember that we saw it. */ | |
10787 | ellipsis_p = true; | |
10788 | } | |
10789 | else | |
10790 | ellipsis_p = false; | |
10791 | ||
10792 | /* Finish the parameter list. */ | |
10793 | return finish_parmlist (parameters, ellipsis_p); | |
10794 | } | |
10795 | ||
10796 | /* Parse a parameter-declaration-list. | |
10797 | ||
10798 | parameter-declaration-list: | |
10799 | parameter-declaration | |
10800 | parameter-declaration-list , parameter-declaration | |
10801 | ||
10802 | Returns a representation of the parameter-declaration-list, as for | |
10803 | cp_parser_parameter_declaration_clause. However, the | |
10804 | `void_list_node' is never appended to the list. */ | |
10805 | ||
10806 | static tree | |
94edc4ab | 10807 | cp_parser_parameter_declaration_list (cp_parser* parser) |
a723baf1 MM |
10808 | { |
10809 | tree parameters = NULL_TREE; | |
10810 | ||
10811 | /* Look for more parameters. */ | |
10812 | while (true) | |
10813 | { | |
10814 | tree parameter; | |
4bb8ca28 | 10815 | bool parenthesized_p; |
a723baf1 MM |
10816 | /* Parse the parameter. */ |
10817 | parameter | |
4bb8ca28 MM |
10818 | = cp_parser_parameter_declaration (parser, |
10819 | /*template_parm_p=*/false, | |
10820 | &parenthesized_p); | |
ec194454 | 10821 | |
34cd5ae7 | 10822 | /* If a parse error occurred parsing the parameter declaration, |
a723baf1 MM |
10823 | then the entire parameter-declaration-list is erroneous. */ |
10824 | if (parameter == error_mark_node) | |
10825 | { | |
10826 | parameters = error_mark_node; | |
10827 | break; | |
10828 | } | |
10829 | /* Add the new parameter to the list. */ | |
10830 | TREE_CHAIN (parameter) = parameters; | |
10831 | parameters = parameter; | |
10832 | ||
10833 | /* Peek at the next token. */ | |
10834 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN) | |
10835 | || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
10836 | /* The parameter-declaration-list is complete. */ | |
10837 | break; | |
10838 | else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
10839 | { | |
10840 | cp_token *token; | |
10841 | ||
10842 | /* Peek at the next token. */ | |
10843 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
10844 | /* If it's an ellipsis, then the list is complete. */ | |
10845 | if (token->type == CPP_ELLIPSIS) | |
10846 | break; | |
10847 | /* Otherwise, there must be more parameters. Consume the | |
10848 | `,'. */ | |
10849 | cp_lexer_consume_token (parser->lexer); | |
4bb8ca28 MM |
10850 | /* When parsing something like: |
10851 | ||
10852 | int i(float f, double d) | |
10853 | ||
10854 | we can tell after seeing the declaration for "f" that we | |
10855 | are not looking at an initialization of a variable "i", | |
10856 | but rather at the declaration of a function "i". | |
10857 | ||
10858 | Due to the fact that the parsing of template arguments | |
10859 | (as specified to a template-id) requires backtracking we | |
10860 | cannot use this technique when inside a template argument | |
10861 | list. */ | |
10862 | if (!parser->in_template_argument_list_p | |
10863 | && cp_parser_parsing_tentatively (parser) | |
10864 | && !cp_parser_committed_to_tentative_parse (parser) | |
10865 | /* However, a parameter-declaration of the form | |
10866 | "foat(f)" (which is a valid declaration of a | |
10867 | parameter "f") can also be interpreted as an | |
10868 | expression (the conversion of "f" to "float"). */ | |
10869 | && !parenthesized_p) | |
10870 | cp_parser_commit_to_tentative_parse (parser); | |
a723baf1 MM |
10871 | } |
10872 | else | |
10873 | { | |
10874 | cp_parser_error (parser, "expected `,' or `...'"); | |
4bb8ca28 MM |
10875 | if (!cp_parser_parsing_tentatively (parser) |
10876 | || cp_parser_committed_to_tentative_parse (parser)) | |
10877 | cp_parser_skip_to_closing_parenthesis (parser, | |
10878 | /*recovering=*/true, | |
5c832178 | 10879 | /*or_comma=*/false, |
4bb8ca28 | 10880 | /*consume_paren=*/false); |
a723baf1 MM |
10881 | break; |
10882 | } | |
10883 | } | |
10884 | ||
10885 | /* We built up the list in reverse order; straighten it out now. */ | |
10886 | return nreverse (parameters); | |
10887 | } | |
10888 | ||
10889 | /* Parse a parameter declaration. | |
10890 | ||
10891 | parameter-declaration: | |
10892 | decl-specifier-seq declarator | |
10893 | decl-specifier-seq declarator = assignment-expression | |
10894 | decl-specifier-seq abstract-declarator [opt] | |
10895 | decl-specifier-seq abstract-declarator [opt] = assignment-expression | |
10896 | ||
ec194454 MM |
10897 | If TEMPLATE_PARM_P is TRUE, then this parameter-declaration |
10898 | declares a template parameter. (In that case, a non-nested `>' | |
10899 | token encountered during the parsing of the assignment-expression | |
10900 | is not interpreted as a greater-than operator.) | |
a723baf1 MM |
10901 | |
10902 | Returns a TREE_LIST representing the parameter-declaration. The | |
4bb8ca28 MM |
10903 | TREE_PURPOSE is the default argument expression, or NULL_TREE if |
10904 | there is no default argument. The TREE_VALUE is a representation | |
10905 | of the decl-specifier-seq and declarator. In particular, the | |
10906 | TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the | |
10907 | decl-specifier-seq and whose TREE_VALUE represents the declarator. | |
10908 | If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff | |
10909 | the declarator is of the form "(p)". */ | |
a723baf1 MM |
10910 | |
10911 | static tree | |
ec194454 | 10912 | cp_parser_parameter_declaration (cp_parser *parser, |
4bb8ca28 MM |
10913 | bool template_parm_p, |
10914 | bool *parenthesized_p) | |
a723baf1 | 10915 | { |
560ad596 | 10916 | int declares_class_or_enum; |
ec194454 | 10917 | bool greater_than_is_operator_p; |
a723baf1 MM |
10918 | tree decl_specifiers; |
10919 | tree attributes; | |
10920 | tree declarator; | |
10921 | tree default_argument; | |
10922 | tree parameter; | |
10923 | cp_token *token; | |
10924 | const char *saved_message; | |
10925 | ||
ec194454 MM |
10926 | /* In a template parameter, `>' is not an operator. |
10927 | ||
10928 | [temp.param] | |
10929 | ||
10930 | When parsing a default template-argument for a non-type | |
10931 | template-parameter, the first non-nested `>' is taken as the end | |
10932 | of the template parameter-list rather than a greater-than | |
10933 | operator. */ | |
10934 | greater_than_is_operator_p = !template_parm_p; | |
10935 | ||
a723baf1 MM |
10936 | /* Type definitions may not appear in parameter types. */ |
10937 | saved_message = parser->type_definition_forbidden_message; | |
10938 | parser->type_definition_forbidden_message | |
10939 | = "types may not be defined in parameter types"; | |
10940 | ||
10941 | /* Parse the declaration-specifiers. */ | |
10942 | decl_specifiers | |
10943 | = cp_parser_decl_specifier_seq (parser, | |
10944 | CP_PARSER_FLAGS_NONE, | |
10945 | &attributes, | |
10946 | &declares_class_or_enum); | |
10947 | /* If an error occurred, there's no reason to attempt to parse the | |
10948 | rest of the declaration. */ | |
10949 | if (cp_parser_error_occurred (parser)) | |
10950 | { | |
10951 | parser->type_definition_forbidden_message = saved_message; | |
10952 | return error_mark_node; | |
10953 | } | |
10954 | ||
10955 | /* Peek at the next token. */ | |
10956 | token = cp_lexer_peek_token (parser->lexer); | |
10957 | /* If the next token is a `)', `,', `=', `>', or `...', then there | |
10958 | is no declarator. */ | |
10959 | if (token->type == CPP_CLOSE_PAREN | |
10960 | || token->type == CPP_COMMA | |
10961 | || token->type == CPP_EQ | |
10962 | || token->type == CPP_ELLIPSIS | |
10963 | || token->type == CPP_GREATER) | |
4bb8ca28 MM |
10964 | { |
10965 | declarator = NULL_TREE; | |
10966 | if (parenthesized_p) | |
10967 | *parenthesized_p = false; | |
10968 | } | |
a723baf1 MM |
10969 | /* Otherwise, there should be a declarator. */ |
10970 | else | |
10971 | { | |
10972 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
10973 | parser->default_arg_ok_p = false; | |
10974 | ||
5c832178 MM |
10975 | /* After seeing a decl-specifier-seq, if the next token is not a |
10976 | "(", there is no possibility that the code is a valid | |
4f8163b1 MM |
10977 | expression. Therefore, if parsing tentatively, we commit at |
10978 | this point. */ | |
5c832178 | 10979 | if (!parser->in_template_argument_list_p |
4f8163b1 MM |
10980 | /* Having seen: |
10981 | ||
10982 | (int((char *)... | |
10983 | ||
10984 | we cannot be sure whether we are looking at a | |
10985 | function-type (taking a */ | |
10986 | && !parser->in_type_id_in_expr_p | |
5c832178 MM |
10987 | && cp_parser_parsing_tentatively (parser) |
10988 | && !cp_parser_committed_to_tentative_parse (parser) | |
10989 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) | |
10990 | cp_parser_commit_to_tentative_parse (parser); | |
10991 | /* Parse the declarator. */ | |
a723baf1 | 10992 | declarator = cp_parser_declarator (parser, |
62b8a44e | 10993 | CP_PARSER_DECLARATOR_EITHER, |
4bb8ca28 MM |
10994 | /*ctor_dtor_or_conv_p=*/NULL, |
10995 | parenthesized_p); | |
a723baf1 | 10996 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
4971227d MM |
10997 | /* After the declarator, allow more attributes. */ |
10998 | attributes = chainon (attributes, cp_parser_attributes_opt (parser)); | |
a723baf1 MM |
10999 | } |
11000 | ||
62b8a44e | 11001 | /* The restriction on defining new types applies only to the type |
a723baf1 MM |
11002 | of the parameter, not to the default argument. */ |
11003 | parser->type_definition_forbidden_message = saved_message; | |
11004 | ||
11005 | /* If the next token is `=', then process a default argument. */ | |
11006 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
11007 | { | |
11008 | bool saved_greater_than_is_operator_p; | |
11009 | /* Consume the `='. */ | |
11010 | cp_lexer_consume_token (parser->lexer); | |
11011 | ||
11012 | /* If we are defining a class, then the tokens that make up the | |
11013 | default argument must be saved and processed later. */ | |
ec194454 MM |
11014 | if (!template_parm_p && at_class_scope_p () |
11015 | && TYPE_BEING_DEFINED (current_class_type)) | |
a723baf1 MM |
11016 | { |
11017 | unsigned depth = 0; | |
11018 | ||
11019 | /* Create a DEFAULT_ARG to represented the unparsed default | |
11020 | argument. */ | |
11021 | default_argument = make_node (DEFAULT_ARG); | |
11022 | DEFARG_TOKENS (default_argument) = cp_token_cache_new (); | |
11023 | ||
11024 | /* Add tokens until we have processed the entire default | |
11025 | argument. */ | |
11026 | while (true) | |
11027 | { | |
11028 | bool done = false; | |
11029 | cp_token *token; | |
11030 | ||
11031 | /* Peek at the next token. */ | |
11032 | token = cp_lexer_peek_token (parser->lexer); | |
11033 | /* What we do depends on what token we have. */ | |
11034 | switch (token->type) | |
11035 | { | |
11036 | /* In valid code, a default argument must be | |
11037 | immediately followed by a `,' `)', or `...'. */ | |
11038 | case CPP_COMMA: | |
11039 | case CPP_CLOSE_PAREN: | |
11040 | case CPP_ELLIPSIS: | |
11041 | /* If we run into a non-nested `;', `}', or `]', | |
11042 | then the code is invalid -- but the default | |
11043 | argument is certainly over. */ | |
11044 | case CPP_SEMICOLON: | |
11045 | case CPP_CLOSE_BRACE: | |
11046 | case CPP_CLOSE_SQUARE: | |
11047 | if (depth == 0) | |
11048 | done = true; | |
11049 | /* Update DEPTH, if necessary. */ | |
11050 | else if (token->type == CPP_CLOSE_PAREN | |
11051 | || token->type == CPP_CLOSE_BRACE | |
11052 | || token->type == CPP_CLOSE_SQUARE) | |
11053 | --depth; | |
11054 | break; | |
11055 | ||
11056 | case CPP_OPEN_PAREN: | |
11057 | case CPP_OPEN_SQUARE: | |
11058 | case CPP_OPEN_BRACE: | |
11059 | ++depth; | |
11060 | break; | |
11061 | ||
11062 | case CPP_GREATER: | |
11063 | /* If we see a non-nested `>', and `>' is not an | |
11064 | operator, then it marks the end of the default | |
11065 | argument. */ | |
11066 | if (!depth && !greater_than_is_operator_p) | |
11067 | done = true; | |
11068 | break; | |
11069 | ||
11070 | /* If we run out of tokens, issue an error message. */ | |
11071 | case CPP_EOF: | |
11072 | error ("file ends in default argument"); | |
11073 | done = true; | |
11074 | break; | |
11075 | ||
11076 | case CPP_NAME: | |
11077 | case CPP_SCOPE: | |
11078 | /* In these cases, we should look for template-ids. | |
11079 | For example, if the default argument is | |
11080 | `X<int, double>()', we need to do name lookup to | |
11081 | figure out whether or not `X' is a template; if | |
34cd5ae7 | 11082 | so, the `,' does not end the default argument. |
a723baf1 MM |
11083 | |
11084 | That is not yet done. */ | |
11085 | break; | |
11086 | ||
11087 | default: | |
11088 | break; | |
11089 | } | |
11090 | ||
11091 | /* If we've reached the end, stop. */ | |
11092 | if (done) | |
11093 | break; | |
11094 | ||
11095 | /* Add the token to the token block. */ | |
11096 | token = cp_lexer_consume_token (parser->lexer); | |
11097 | cp_token_cache_push_token (DEFARG_TOKENS (default_argument), | |
11098 | token); | |
11099 | } | |
11100 | } | |
11101 | /* Outside of a class definition, we can just parse the | |
11102 | assignment-expression. */ | |
11103 | else | |
11104 | { | |
11105 | bool saved_local_variables_forbidden_p; | |
11106 | ||
11107 | /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is | |
11108 | set correctly. */ | |
11109 | saved_greater_than_is_operator_p | |
11110 | = parser->greater_than_is_operator_p; | |
11111 | parser->greater_than_is_operator_p = greater_than_is_operator_p; | |
11112 | /* Local variable names (and the `this' keyword) may not | |
11113 | appear in a default argument. */ | |
11114 | saved_local_variables_forbidden_p | |
11115 | = parser->local_variables_forbidden_p; | |
11116 | parser->local_variables_forbidden_p = true; | |
11117 | /* Parse the assignment-expression. */ | |
11118 | default_argument = cp_parser_assignment_expression (parser); | |
11119 | /* Restore saved state. */ | |
11120 | parser->greater_than_is_operator_p | |
11121 | = saved_greater_than_is_operator_p; | |
11122 | parser->local_variables_forbidden_p | |
11123 | = saved_local_variables_forbidden_p; | |
11124 | } | |
11125 | if (!parser->default_arg_ok_p) | |
11126 | { | |
c67d36d0 NS |
11127 | if (!flag_pedantic_errors) |
11128 | warning ("deprecated use of default argument for parameter of non-function"); | |
11129 | else | |
11130 | { | |
11131 | error ("default arguments are only permitted for function parameters"); | |
11132 | default_argument = NULL_TREE; | |
11133 | } | |
a723baf1 MM |
11134 | } |
11135 | } | |
11136 | else | |
11137 | default_argument = NULL_TREE; | |
11138 | ||
11139 | /* Create the representation of the parameter. */ | |
11140 | if (attributes) | |
11141 | decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers); | |
11142 | parameter = build_tree_list (default_argument, | |
11143 | build_tree_list (decl_specifiers, | |
11144 | declarator)); | |
11145 | ||
11146 | return parameter; | |
11147 | } | |
11148 | ||
a723baf1 MM |
11149 | /* Parse a function-body. |
11150 | ||
11151 | function-body: | |
11152 | compound_statement */ | |
11153 | ||
11154 | static void | |
11155 | cp_parser_function_body (cp_parser *parser) | |
11156 | { | |
a5bcc582 | 11157 | cp_parser_compound_statement (parser, false); |
a723baf1 MM |
11158 | } |
11159 | ||
11160 | /* Parse a ctor-initializer-opt followed by a function-body. Return | |
11161 | true if a ctor-initializer was present. */ | |
11162 | ||
11163 | static bool | |
11164 | cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser) | |
11165 | { | |
11166 | tree body; | |
11167 | bool ctor_initializer_p; | |
11168 | ||
11169 | /* Begin the function body. */ | |
11170 | body = begin_function_body (); | |
11171 | /* Parse the optional ctor-initializer. */ | |
11172 | ctor_initializer_p = cp_parser_ctor_initializer_opt (parser); | |
11173 | /* Parse the function-body. */ | |
11174 | cp_parser_function_body (parser); | |
11175 | /* Finish the function body. */ | |
11176 | finish_function_body (body); | |
11177 | ||
11178 | return ctor_initializer_p; | |
11179 | } | |
11180 | ||
11181 | /* Parse an initializer. | |
11182 | ||
11183 | initializer: | |
11184 | = initializer-clause | |
11185 | ( expression-list ) | |
11186 | ||
11187 | Returns a expression representing the initializer. If no | |
11188 | initializer is present, NULL_TREE is returned. | |
11189 | ||
11190 | *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )' | |
11191 | production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is | |
39703eb9 MM |
11192 | set to FALSE if there is no initializer present. If there is an |
11193 | initializer, and it is not a constant-expression, *NON_CONSTANT_P | |
11194 | is set to true; otherwise it is set to false. */ | |
a723baf1 MM |
11195 | |
11196 | static tree | |
39703eb9 MM |
11197 | cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init, |
11198 | bool* non_constant_p) | |
a723baf1 MM |
11199 | { |
11200 | cp_token *token; | |
11201 | tree init; | |
11202 | ||
11203 | /* Peek at the next token. */ | |
11204 | token = cp_lexer_peek_token (parser->lexer); | |
11205 | ||
11206 | /* Let our caller know whether or not this initializer was | |
11207 | parenthesized. */ | |
11208 | *is_parenthesized_init = (token->type == CPP_OPEN_PAREN); | |
39703eb9 MM |
11209 | /* Assume that the initializer is constant. */ |
11210 | *non_constant_p = false; | |
a723baf1 MM |
11211 | |
11212 | if (token->type == CPP_EQ) | |
11213 | { | |
11214 | /* Consume the `='. */ | |
11215 | cp_lexer_consume_token (parser->lexer); | |
11216 | /* Parse the initializer-clause. */ | |
39703eb9 | 11217 | init = cp_parser_initializer_clause (parser, non_constant_p); |
a723baf1 MM |
11218 | } |
11219 | else if (token->type == CPP_OPEN_PAREN) | |
39703eb9 MM |
11220 | init = cp_parser_parenthesized_expression_list (parser, false, |
11221 | non_constant_p); | |
a723baf1 MM |
11222 | else |
11223 | { | |
11224 | /* Anything else is an error. */ | |
11225 | cp_parser_error (parser, "expected initializer"); | |
11226 | init = error_mark_node; | |
11227 | } | |
11228 | ||
11229 | return init; | |
11230 | } | |
11231 | ||
11232 | /* Parse an initializer-clause. | |
11233 | ||
11234 | initializer-clause: | |
11235 | assignment-expression | |
11236 | { initializer-list , [opt] } | |
11237 | { } | |
11238 | ||
11239 | Returns an expression representing the initializer. | |
11240 | ||
11241 | If the `assignment-expression' production is used the value | |
34cd5ae7 | 11242 | returned is simply a representation for the expression. |
a723baf1 MM |
11243 | |
11244 | Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be | |
11245 | the elements of the initializer-list (or NULL_TREE, if the last | |
11246 | production is used). The TREE_TYPE for the CONSTRUCTOR will be | |
11247 | NULL_TREE. There is no way to detect whether or not the optional | |
39703eb9 MM |
11248 | trailing `,' was provided. NON_CONSTANT_P is as for |
11249 | cp_parser_initializer. */ | |
a723baf1 MM |
11250 | |
11251 | static tree | |
39703eb9 | 11252 | cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p) |
a723baf1 MM |
11253 | { |
11254 | tree initializer; | |
11255 | ||
11256 | /* If it is not a `{', then we are looking at an | |
11257 | assignment-expression. */ | |
11258 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
39703eb9 MM |
11259 | initializer |
11260 | = cp_parser_constant_expression (parser, | |
11261 | /*allow_non_constant_p=*/true, | |
11262 | non_constant_p); | |
a723baf1 MM |
11263 | else |
11264 | { | |
11265 | /* Consume the `{' token. */ | |
11266 | cp_lexer_consume_token (parser->lexer); | |
11267 | /* Create a CONSTRUCTOR to represent the braced-initializer. */ | |
11268 | initializer = make_node (CONSTRUCTOR); | |
11269 | /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be | |
11270 | necessary, but check_initializer depends upon it, for | |
11271 | now. */ | |
11272 | TREE_HAS_CONSTRUCTOR (initializer) = 1; | |
11273 | /* If it's not a `}', then there is a non-trivial initializer. */ | |
11274 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)) | |
11275 | { | |
11276 | /* Parse the initializer list. */ | |
11277 | CONSTRUCTOR_ELTS (initializer) | |
39703eb9 | 11278 | = cp_parser_initializer_list (parser, non_constant_p); |
a723baf1 MM |
11279 | /* A trailing `,' token is allowed. */ |
11280 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
11281 | cp_lexer_consume_token (parser->lexer); | |
11282 | } | |
a723baf1 MM |
11283 | /* Now, there should be a trailing `}'. */ |
11284 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
11285 | } | |
11286 | ||
11287 | return initializer; | |
11288 | } | |
11289 | ||
11290 | /* Parse an initializer-list. | |
11291 | ||
11292 | initializer-list: | |
11293 | initializer-clause | |
11294 | initializer-list , initializer-clause | |
11295 | ||
11296 | GNU Extension: | |
11297 | ||
11298 | initializer-list: | |
11299 | identifier : initializer-clause | |
11300 | initializer-list, identifier : initializer-clause | |
11301 | ||
11302 | Returns a TREE_LIST. The TREE_VALUE of each node is an expression | |
11303 | for the initializer. If the TREE_PURPOSE is non-NULL, it is the | |
39703eb9 MM |
11304 | IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is |
11305 | as for cp_parser_initializer. */ | |
a723baf1 MM |
11306 | |
11307 | static tree | |
39703eb9 | 11308 | cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p) |
a723baf1 MM |
11309 | { |
11310 | tree initializers = NULL_TREE; | |
11311 | ||
39703eb9 MM |
11312 | /* Assume all of the expressions are constant. */ |
11313 | *non_constant_p = false; | |
11314 | ||
a723baf1 MM |
11315 | /* Parse the rest of the list. */ |
11316 | while (true) | |
11317 | { | |
11318 | cp_token *token; | |
11319 | tree identifier; | |
11320 | tree initializer; | |
39703eb9 MM |
11321 | bool clause_non_constant_p; |
11322 | ||
a723baf1 MM |
11323 | /* If the next token is an identifier and the following one is a |
11324 | colon, we are looking at the GNU designated-initializer | |
11325 | syntax. */ | |
11326 | if (cp_parser_allow_gnu_extensions_p (parser) | |
11327 | && cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
11328 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON) | |
11329 | { | |
11330 | /* Consume the identifier. */ | |
11331 | identifier = cp_lexer_consume_token (parser->lexer)->value; | |
11332 | /* Consume the `:'. */ | |
11333 | cp_lexer_consume_token (parser->lexer); | |
11334 | } | |
11335 | else | |
11336 | identifier = NULL_TREE; | |
11337 | ||
11338 | /* Parse the initializer. */ | |
39703eb9 MM |
11339 | initializer = cp_parser_initializer_clause (parser, |
11340 | &clause_non_constant_p); | |
11341 | /* If any clause is non-constant, so is the entire initializer. */ | |
11342 | if (clause_non_constant_p) | |
11343 | *non_constant_p = true; | |
a723baf1 MM |
11344 | /* Add it to the list. */ |
11345 | initializers = tree_cons (identifier, initializer, initializers); | |
11346 | ||
11347 | /* If the next token is not a comma, we have reached the end of | |
11348 | the list. */ | |
11349 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
11350 | break; | |
11351 | ||
11352 | /* Peek at the next token. */ | |
11353 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
11354 | /* If the next token is a `}', then we're still done. An | |
11355 | initializer-clause can have a trailing `,' after the | |
11356 | initializer-list and before the closing `}'. */ | |
11357 | if (token->type == CPP_CLOSE_BRACE) | |
11358 | break; | |
11359 | ||
11360 | /* Consume the `,' token. */ | |
11361 | cp_lexer_consume_token (parser->lexer); | |
11362 | } | |
11363 | ||
11364 | /* The initializers were built up in reverse order, so we need to | |
11365 | reverse them now. */ | |
11366 | return nreverse (initializers); | |
11367 | } | |
11368 | ||
11369 | /* Classes [gram.class] */ | |
11370 | ||
11371 | /* Parse a class-name. | |
11372 | ||
11373 | class-name: | |
11374 | identifier | |
11375 | template-id | |
11376 | ||
11377 | TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used | |
11378 | to indicate that names looked up in dependent types should be | |
11379 | assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template' | |
11380 | keyword has been used to indicate that the name that appears next | |
11381 | is a template. TYPE_P is true iff the next name should be treated | |
11382 | as class-name, even if it is declared to be some other kind of name | |
8d241e0b KL |
11383 | as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in |
11384 | dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class | |
11385 | being defined in a class-head. | |
a723baf1 MM |
11386 | |
11387 | Returns the TYPE_DECL representing the class. */ | |
11388 | ||
11389 | static tree | |
11390 | cp_parser_class_name (cp_parser *parser, | |
11391 | bool typename_keyword_p, | |
11392 | bool template_keyword_p, | |
11393 | bool type_p, | |
a723baf1 | 11394 | bool check_dependency_p, |
a668c6ad MM |
11395 | bool class_head_p, |
11396 | bool is_declaration) | |
a723baf1 MM |
11397 | { |
11398 | tree decl; | |
11399 | tree scope; | |
11400 | bool typename_p; | |
e5976695 MM |
11401 | cp_token *token; |
11402 | ||
11403 | /* All class-names start with an identifier. */ | |
11404 | token = cp_lexer_peek_token (parser->lexer); | |
11405 | if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID) | |
11406 | { | |
11407 | cp_parser_error (parser, "expected class-name"); | |
11408 | return error_mark_node; | |
11409 | } | |
11410 | ||
a723baf1 MM |
11411 | /* PARSER->SCOPE can be cleared when parsing the template-arguments |
11412 | to a template-id, so we save it here. */ | |
11413 | scope = parser->scope; | |
3adee96c KL |
11414 | if (scope == error_mark_node) |
11415 | return error_mark_node; | |
11416 | ||
a723baf1 MM |
11417 | /* Any name names a type if we're following the `typename' keyword |
11418 | in a qualified name where the enclosing scope is type-dependent. */ | |
11419 | typename_p = (typename_keyword_p && scope && TYPE_P (scope) | |
1fb3244a | 11420 | && dependent_type_p (scope)); |
e5976695 MM |
11421 | /* Handle the common case (an identifier, but not a template-id) |
11422 | efficiently. */ | |
11423 | if (token->type == CPP_NAME | |
11424 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS) | |
a723baf1 | 11425 | { |
a723baf1 MM |
11426 | tree identifier; |
11427 | ||
11428 | /* Look for the identifier. */ | |
11429 | identifier = cp_parser_identifier (parser); | |
11430 | /* If the next token isn't an identifier, we are certainly not | |
11431 | looking at a class-name. */ | |
11432 | if (identifier == error_mark_node) | |
11433 | decl = error_mark_node; | |
11434 | /* If we know this is a type-name, there's no need to look it | |
11435 | up. */ | |
11436 | else if (typename_p) | |
11437 | decl = identifier; | |
11438 | else | |
11439 | { | |
11440 | /* If the next token is a `::', then the name must be a type | |
11441 | name. | |
11442 | ||
11443 | [basic.lookup.qual] | |
11444 | ||
11445 | During the lookup for a name preceding the :: scope | |
11446 | resolution operator, object, function, and enumerator | |
11447 | names are ignored. */ | |
11448 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
11449 | type_p = true; | |
11450 | /* Look up the name. */ | |
11451 | decl = cp_parser_lookup_name (parser, identifier, | |
a723baf1 | 11452 | type_p, |
b0bc6e8e | 11453 | /*is_template=*/false, |
eea9800f | 11454 | /*is_namespace=*/false, |
a723baf1 MM |
11455 | check_dependency_p); |
11456 | } | |
11457 | } | |
e5976695 MM |
11458 | else |
11459 | { | |
11460 | /* Try a template-id. */ | |
11461 | decl = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
11462 | check_dependency_p, |
11463 | is_declaration); | |
e5976695 MM |
11464 | if (decl == error_mark_node) |
11465 | return error_mark_node; | |
11466 | } | |
a723baf1 MM |
11467 | |
11468 | decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p); | |
11469 | ||
11470 | /* If this is a typename, create a TYPENAME_TYPE. */ | |
11471 | if (typename_p && decl != error_mark_node) | |
11472 | decl = TYPE_NAME (make_typename_type (scope, decl, | |
11473 | /*complain=*/1)); | |
11474 | ||
11475 | /* Check to see that it is really the name of a class. */ | |
11476 | if (TREE_CODE (decl) == TEMPLATE_ID_EXPR | |
11477 | && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE | |
11478 | && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
11479 | /* Situations like this: | |
11480 | ||
11481 | template <typename T> struct A { | |
11482 | typename T::template X<int>::I i; | |
11483 | }; | |
11484 | ||
11485 | are problematic. Is `T::template X<int>' a class-name? The | |
11486 | standard does not seem to be definitive, but there is no other | |
11487 | valid interpretation of the following `::'. Therefore, those | |
11488 | names are considered class-names. */ | |
78757caa | 11489 | decl = TYPE_NAME (make_typename_type (scope, decl, tf_error)); |
a723baf1 MM |
11490 | else if (decl == error_mark_node |
11491 | || TREE_CODE (decl) != TYPE_DECL | |
11492 | || !IS_AGGR_TYPE (TREE_TYPE (decl))) | |
11493 | { | |
11494 | cp_parser_error (parser, "expected class-name"); | |
11495 | return error_mark_node; | |
11496 | } | |
11497 | ||
11498 | return decl; | |
11499 | } | |
11500 | ||
11501 | /* Parse a class-specifier. | |
11502 | ||
11503 | class-specifier: | |
11504 | class-head { member-specification [opt] } | |
11505 | ||
11506 | Returns the TREE_TYPE representing the class. */ | |
11507 | ||
11508 | static tree | |
94edc4ab | 11509 | cp_parser_class_specifier (cp_parser* parser) |
a723baf1 MM |
11510 | { |
11511 | cp_token *token; | |
11512 | tree type; | |
11513 | tree attributes = NULL_TREE; | |
11514 | int has_trailing_semicolon; | |
11515 | bool nested_name_specifier_p; | |
a723baf1 MM |
11516 | unsigned saved_num_template_parameter_lists; |
11517 | ||
8d241e0b | 11518 | push_deferring_access_checks (dk_no_deferred); |
cf22909c | 11519 | |
a723baf1 MM |
11520 | /* Parse the class-head. */ |
11521 | type = cp_parser_class_head (parser, | |
cf22909c | 11522 | &nested_name_specifier_p); |
a723baf1 MM |
11523 | /* If the class-head was a semantic disaster, skip the entire body |
11524 | of the class. */ | |
11525 | if (!type) | |
11526 | { | |
11527 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
cf22909c | 11528 | pop_deferring_access_checks (); |
a723baf1 MM |
11529 | return error_mark_node; |
11530 | } | |
cf22909c | 11531 | |
a723baf1 MM |
11532 | /* Look for the `{'. */ |
11533 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
cf22909c KL |
11534 | { |
11535 | pop_deferring_access_checks (); | |
11536 | return error_mark_node; | |
11537 | } | |
11538 | ||
a723baf1 MM |
11539 | /* Issue an error message if type-definitions are forbidden here. */ |
11540 | cp_parser_check_type_definition (parser); | |
11541 | /* Remember that we are defining one more class. */ | |
11542 | ++parser->num_classes_being_defined; | |
11543 | /* Inside the class, surrounding template-parameter-lists do not | |
11544 | apply. */ | |
11545 | saved_num_template_parameter_lists | |
11546 | = parser->num_template_parameter_lists; | |
11547 | parser->num_template_parameter_lists = 0; | |
78757caa | 11548 | |
a723baf1 | 11549 | /* Start the class. */ |
eeb23c11 MM |
11550 | if (nested_name_specifier_p) |
11551 | push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type))); | |
a723baf1 MM |
11552 | type = begin_class_definition (type); |
11553 | if (type == error_mark_node) | |
9bcb9aae | 11554 | /* If the type is erroneous, skip the entire body of the class. */ |
a723baf1 MM |
11555 | cp_parser_skip_to_closing_brace (parser); |
11556 | else | |
11557 | /* Parse the member-specification. */ | |
11558 | cp_parser_member_specification_opt (parser); | |
11559 | /* Look for the trailing `}'. */ | |
11560 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
11561 | /* We get better error messages by noticing a common problem: a | |
11562 | missing trailing `;'. */ | |
11563 | token = cp_lexer_peek_token (parser->lexer); | |
11564 | has_trailing_semicolon = (token->type == CPP_SEMICOLON); | |
11565 | /* Look for attributes to apply to this class. */ | |
11566 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
11567 | attributes = cp_parser_attributes_opt (parser); | |
560ad596 MM |
11568 | /* If we got any attributes in class_head, xref_tag will stick them in |
11569 | TREE_TYPE of the type. Grab them now. */ | |
11570 | if (type != error_mark_node) | |
11571 | { | |
11572 | attributes = chainon (TYPE_ATTRIBUTES (type), attributes); | |
11573 | TYPE_ATTRIBUTES (type) = NULL_TREE; | |
11574 | type = finish_struct (type, attributes); | |
11575 | } | |
11576 | if (nested_name_specifier_p) | |
11577 | pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type))); | |
a723baf1 MM |
11578 | /* If this class is not itself within the scope of another class, |
11579 | then we need to parse the bodies of all of the queued function | |
11580 | definitions. Note that the queued functions defined in a class | |
11581 | are not always processed immediately following the | |
11582 | class-specifier for that class. Consider: | |
11583 | ||
11584 | struct A { | |
11585 | struct B { void f() { sizeof (A); } }; | |
11586 | }; | |
11587 | ||
11588 | If `f' were processed before the processing of `A' were | |
11589 | completed, there would be no way to compute the size of `A'. | |
11590 | Note that the nesting we are interested in here is lexical -- | |
11591 | not the semantic nesting given by TYPE_CONTEXT. In particular, | |
11592 | for: | |
11593 | ||
11594 | struct A { struct B; }; | |
11595 | struct A::B { void f() { } }; | |
11596 | ||
11597 | there is no need to delay the parsing of `A::B::f'. */ | |
11598 | if (--parser->num_classes_being_defined == 0) | |
11599 | { | |
8218bd34 MM |
11600 | tree queue_entry; |
11601 | tree fn; | |
a723baf1 | 11602 | |
8218bd34 MM |
11603 | /* In a first pass, parse default arguments to the functions. |
11604 | Then, in a second pass, parse the bodies of the functions. | |
11605 | This two-phased approach handles cases like: | |
11606 | ||
11607 | struct S { | |
11608 | void f() { g(); } | |
11609 | void g(int i = 3); | |
11610 | }; | |
11611 | ||
11612 | */ | |
8db1028e NS |
11613 | for (TREE_PURPOSE (parser->unparsed_functions_queues) |
11614 | = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues)); | |
11615 | (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues)); | |
11616 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
11617 | = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues))) | |
8218bd34 MM |
11618 | { |
11619 | fn = TREE_VALUE (queue_entry); | |
8218bd34 MM |
11620 | /* Make sure that any template parameters are in scope. */ |
11621 | maybe_begin_member_template_processing (fn); | |
11622 | /* If there are default arguments that have not yet been processed, | |
11623 | take care of them now. */ | |
11624 | cp_parser_late_parsing_default_args (parser, fn); | |
11625 | /* Remove any template parameters from the symbol table. */ | |
11626 | maybe_end_member_template_processing (); | |
11627 | } | |
11628 | /* Now parse the body of the functions. */ | |
8db1028e NS |
11629 | for (TREE_VALUE (parser->unparsed_functions_queues) |
11630 | = nreverse (TREE_VALUE (parser->unparsed_functions_queues)); | |
11631 | (queue_entry = TREE_VALUE (parser->unparsed_functions_queues)); | |
11632 | TREE_VALUE (parser->unparsed_functions_queues) | |
11633 | = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues))) | |
a723baf1 | 11634 | { |
a723baf1 | 11635 | /* Figure out which function we need to process. */ |
a723baf1 MM |
11636 | fn = TREE_VALUE (queue_entry); |
11637 | ||
11638 | /* Parse the function. */ | |
11639 | cp_parser_late_parsing_for_member (parser, fn); | |
a723baf1 MM |
11640 | } |
11641 | ||
a723baf1 MM |
11642 | } |
11643 | ||
11644 | /* Put back any saved access checks. */ | |
cf22909c | 11645 | pop_deferring_access_checks (); |
a723baf1 MM |
11646 | |
11647 | /* Restore the count of active template-parameter-lists. */ | |
11648 | parser->num_template_parameter_lists | |
11649 | = saved_num_template_parameter_lists; | |
11650 | ||
11651 | return type; | |
11652 | } | |
11653 | ||
11654 | /* Parse a class-head. | |
11655 | ||
11656 | class-head: | |
11657 | class-key identifier [opt] base-clause [opt] | |
11658 | class-key nested-name-specifier identifier base-clause [opt] | |
11659 | class-key nested-name-specifier [opt] template-id | |
11660 | base-clause [opt] | |
11661 | ||
11662 | GNU Extensions: | |
11663 | class-key attributes identifier [opt] base-clause [opt] | |
11664 | class-key attributes nested-name-specifier identifier base-clause [opt] | |
11665 | class-key attributes nested-name-specifier [opt] template-id | |
11666 | base-clause [opt] | |
11667 | ||
11668 | Returns the TYPE of the indicated class. Sets | |
11669 | *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions | |
11670 | involving a nested-name-specifier was used, and FALSE otherwise. | |
a723baf1 MM |
11671 | |
11672 | Returns NULL_TREE if the class-head is syntactically valid, but | |
11673 | semantically invalid in a way that means we should skip the entire | |
11674 | body of the class. */ | |
11675 | ||
11676 | static tree | |
94edc4ab NN |
11677 | cp_parser_class_head (cp_parser* parser, |
11678 | bool* nested_name_specifier_p) | |
a723baf1 MM |
11679 | { |
11680 | cp_token *token; | |
11681 | tree nested_name_specifier; | |
11682 | enum tag_types class_key; | |
11683 | tree id = NULL_TREE; | |
11684 | tree type = NULL_TREE; | |
11685 | tree attributes; | |
11686 | bool template_id_p = false; | |
11687 | bool qualified_p = false; | |
11688 | bool invalid_nested_name_p = false; | |
afb0918a | 11689 | bool invalid_explicit_specialization_p = false; |
a723baf1 MM |
11690 | unsigned num_templates; |
11691 | ||
11692 | /* Assume no nested-name-specifier will be present. */ | |
11693 | *nested_name_specifier_p = false; | |
11694 | /* Assume no template parameter lists will be used in defining the | |
11695 | type. */ | |
11696 | num_templates = 0; | |
11697 | ||
11698 | /* Look for the class-key. */ | |
11699 | class_key = cp_parser_class_key (parser); | |
11700 | if (class_key == none_type) | |
11701 | return error_mark_node; | |
11702 | ||
11703 | /* Parse the attributes. */ | |
11704 | attributes = cp_parser_attributes_opt (parser); | |
11705 | ||
11706 | /* If the next token is `::', that is invalid -- but sometimes | |
11707 | people do try to write: | |
11708 | ||
11709 | struct ::S {}; | |
11710 | ||
11711 | Handle this gracefully by accepting the extra qualifier, and then | |
11712 | issuing an error about it later if this really is a | |
2050a1bb | 11713 | class-head. If it turns out just to be an elaborated type |
a723baf1 MM |
11714 | specifier, remain silent. */ |
11715 | if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)) | |
11716 | qualified_p = true; | |
11717 | ||
8d241e0b KL |
11718 | push_deferring_access_checks (dk_no_check); |
11719 | ||
a723baf1 MM |
11720 | /* Determine the name of the class. Begin by looking for an |
11721 | optional nested-name-specifier. */ | |
11722 | nested_name_specifier | |
11723 | = cp_parser_nested_name_specifier_opt (parser, | |
11724 | /*typename_keyword_p=*/false, | |
66d418e6 | 11725 | /*check_dependency_p=*/false, |
a668c6ad MM |
11726 | /*type_p=*/false, |
11727 | /*is_declaration=*/false); | |
a723baf1 MM |
11728 | /* If there was a nested-name-specifier, then there *must* be an |
11729 | identifier. */ | |
11730 | if (nested_name_specifier) | |
11731 | { | |
11732 | /* Although the grammar says `identifier', it really means | |
11733 | `class-name' or `template-name'. You are only allowed to | |
11734 | define a class that has already been declared with this | |
11735 | syntax. | |
11736 | ||
11737 | The proposed resolution for Core Issue 180 says that whever | |
11738 | you see `class T::X' you should treat `X' as a type-name. | |
11739 | ||
11740 | It is OK to define an inaccessible class; for example: | |
11741 | ||
11742 | class A { class B; }; | |
11743 | class A::B {}; | |
11744 | ||
a723baf1 MM |
11745 | We do not know if we will see a class-name, or a |
11746 | template-name. We look for a class-name first, in case the | |
11747 | class-name is a template-id; if we looked for the | |
11748 | template-name first we would stop after the template-name. */ | |
11749 | cp_parser_parse_tentatively (parser); | |
11750 | type = cp_parser_class_name (parser, | |
11751 | /*typename_keyword_p=*/false, | |
11752 | /*template_keyword_p=*/false, | |
11753 | /*type_p=*/true, | |
a723baf1 | 11754 | /*check_dependency_p=*/false, |
a668c6ad MM |
11755 | /*class_head_p=*/true, |
11756 | /*is_declaration=*/false); | |
a723baf1 MM |
11757 | /* If that didn't work, ignore the nested-name-specifier. */ |
11758 | if (!cp_parser_parse_definitely (parser)) | |
11759 | { | |
11760 | invalid_nested_name_p = true; | |
11761 | id = cp_parser_identifier (parser); | |
11762 | if (id == error_mark_node) | |
11763 | id = NULL_TREE; | |
11764 | } | |
11765 | /* If we could not find a corresponding TYPE, treat this | |
11766 | declaration like an unqualified declaration. */ | |
11767 | if (type == error_mark_node) | |
11768 | nested_name_specifier = NULL_TREE; | |
11769 | /* Otherwise, count the number of templates used in TYPE and its | |
11770 | containing scopes. */ | |
11771 | else | |
11772 | { | |
11773 | tree scope; | |
11774 | ||
11775 | for (scope = TREE_TYPE (type); | |
11776 | scope && TREE_CODE (scope) != NAMESPACE_DECL; | |
11777 | scope = (TYPE_P (scope) | |
11778 | ? TYPE_CONTEXT (scope) | |
11779 | : DECL_CONTEXT (scope))) | |
11780 | if (TYPE_P (scope) | |
11781 | && CLASS_TYPE_P (scope) | |
11782 | && CLASSTYPE_TEMPLATE_INFO (scope) | |
2050a1bb MM |
11783 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)) |
11784 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)) | |
a723baf1 MM |
11785 | ++num_templates; |
11786 | } | |
11787 | } | |
11788 | /* Otherwise, the identifier is optional. */ | |
11789 | else | |
11790 | { | |
11791 | /* We don't know whether what comes next is a template-id, | |
11792 | an identifier, or nothing at all. */ | |
11793 | cp_parser_parse_tentatively (parser); | |
11794 | /* Check for a template-id. */ | |
11795 | id = cp_parser_template_id (parser, | |
11796 | /*template_keyword_p=*/false, | |
a668c6ad MM |
11797 | /*check_dependency_p=*/true, |
11798 | /*is_declaration=*/true); | |
a723baf1 MM |
11799 | /* If that didn't work, it could still be an identifier. */ |
11800 | if (!cp_parser_parse_definitely (parser)) | |
11801 | { | |
11802 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
11803 | id = cp_parser_identifier (parser); | |
11804 | else | |
11805 | id = NULL_TREE; | |
11806 | } | |
11807 | else | |
11808 | { | |
11809 | template_id_p = true; | |
11810 | ++num_templates; | |
11811 | } | |
11812 | } | |
11813 | ||
8d241e0b KL |
11814 | pop_deferring_access_checks (); |
11815 | ||
ee43dab5 MM |
11816 | cp_parser_check_for_invalid_template_id (parser, id); |
11817 | ||
a723baf1 MM |
11818 | /* If it's not a `:' or a `{' then we can't really be looking at a |
11819 | class-head, since a class-head only appears as part of a | |
11820 | class-specifier. We have to detect this situation before calling | |
11821 | xref_tag, since that has irreversible side-effects. */ | |
11822 | if (!cp_parser_next_token_starts_class_definition_p (parser)) | |
11823 | { | |
11824 | cp_parser_error (parser, "expected `{' or `:'"); | |
11825 | return error_mark_node; | |
11826 | } | |
11827 | ||
11828 | /* At this point, we're going ahead with the class-specifier, even | |
11829 | if some other problem occurs. */ | |
11830 | cp_parser_commit_to_tentative_parse (parser); | |
11831 | /* Issue the error about the overly-qualified name now. */ | |
11832 | if (qualified_p) | |
11833 | cp_parser_error (parser, | |
11834 | "global qualification of class name is invalid"); | |
11835 | else if (invalid_nested_name_p) | |
11836 | cp_parser_error (parser, | |
11837 | "qualified name does not name a class"); | |
afb0918a MM |
11838 | /* An explicit-specialization must be preceded by "template <>". If |
11839 | it is not, try to recover gracefully. */ | |
11840 | if (at_namespace_scope_p () | |
11841 | && parser->num_template_parameter_lists == 0 | |
eeb23c11 | 11842 | && template_id_p) |
afb0918a MM |
11843 | { |
11844 | error ("an explicit specialization must be preceded by 'template <>'"); | |
11845 | invalid_explicit_specialization_p = true; | |
11846 | /* Take the same action that would have been taken by | |
11847 | cp_parser_explicit_specialization. */ | |
11848 | ++parser->num_template_parameter_lists; | |
11849 | begin_specialization (); | |
11850 | } | |
11851 | /* There must be no "return" statements between this point and the | |
11852 | end of this function; set "type "to the correct return value and | |
11853 | use "goto done;" to return. */ | |
a723baf1 MM |
11854 | /* Make sure that the right number of template parameters were |
11855 | present. */ | |
11856 | if (!cp_parser_check_template_parameters (parser, num_templates)) | |
afb0918a MM |
11857 | { |
11858 | /* If something went wrong, there is no point in even trying to | |
11859 | process the class-definition. */ | |
11860 | type = NULL_TREE; | |
11861 | goto done; | |
11862 | } | |
a723baf1 | 11863 | |
a723baf1 MM |
11864 | /* Look up the type. */ |
11865 | if (template_id_p) | |
11866 | { | |
11867 | type = TREE_TYPE (id); | |
11868 | maybe_process_partial_specialization (type); | |
11869 | } | |
11870 | else if (!nested_name_specifier) | |
11871 | { | |
11872 | /* If the class was unnamed, create a dummy name. */ | |
11873 | if (!id) | |
11874 | id = make_anon_name (); | |
cbd63935 KL |
11875 | type = xref_tag (class_key, id, attributes, /*globalize=*/false, |
11876 | parser->num_template_parameter_lists); | |
a723baf1 MM |
11877 | } |
11878 | else | |
11879 | { | |
a723baf1 | 11880 | tree class_type; |
089d6ea7 | 11881 | tree scope; |
a723baf1 MM |
11882 | |
11883 | /* Given: | |
11884 | ||
11885 | template <typename T> struct S { struct T }; | |
14d22dd6 | 11886 | template <typename T> struct S<T>::T { }; |
a723baf1 MM |
11887 | |
11888 | we will get a TYPENAME_TYPE when processing the definition of | |
11889 | `S::T'. We need to resolve it to the actual type before we | |
11890 | try to define it. */ | |
11891 | if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE) | |
11892 | { | |
14d22dd6 MM |
11893 | class_type = resolve_typename_type (TREE_TYPE (type), |
11894 | /*only_current_p=*/false); | |
11895 | if (class_type != error_mark_node) | |
11896 | type = TYPE_NAME (class_type); | |
11897 | else | |
11898 | { | |
11899 | cp_parser_error (parser, "could not resolve typename type"); | |
11900 | type = error_mark_node; | |
11901 | } | |
a723baf1 MM |
11902 | } |
11903 | ||
089d6ea7 MM |
11904 | /* Figure out in what scope the declaration is being placed. */ |
11905 | scope = current_scope (); | |
11906 | if (!scope) | |
11907 | scope = current_namespace; | |
11908 | /* If that scope does not contain the scope in which the | |
11909 | class was originally declared, the program is invalid. */ | |
11910 | if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type))) | |
11911 | { | |
0e136342 | 11912 | error ("declaration of `%D' in `%D' which does not " |
089d6ea7 | 11913 | "enclose `%D'", type, scope, nested_name_specifier); |
afb0918a MM |
11914 | type = NULL_TREE; |
11915 | goto done; | |
089d6ea7 | 11916 | } |
560ad596 | 11917 | /* [dcl.meaning] |
089d6ea7 | 11918 | |
560ad596 MM |
11919 | A declarator-id shall not be qualified exception of the |
11920 | definition of a ... nested class outside of its class | |
11921 | ... [or] a the definition or explicit instantiation of a | |
11922 | class member of a namespace outside of its namespace. */ | |
11923 | if (scope == CP_DECL_CONTEXT (type)) | |
a723baf1 | 11924 | { |
560ad596 MM |
11925 | pedwarn ("extra qualification ignored"); |
11926 | nested_name_specifier = NULL_TREE; | |
a723baf1 | 11927 | } |
560ad596 MM |
11928 | |
11929 | maybe_process_partial_specialization (TREE_TYPE (type)); | |
11930 | class_type = current_class_type; | |
11931 | /* Enter the scope indicated by the nested-name-specifier. */ | |
11932 | if (nested_name_specifier) | |
11933 | push_scope (nested_name_specifier); | |
11934 | /* Get the canonical version of this type. */ | |
11935 | type = TYPE_MAIN_DECL (TREE_TYPE (type)); | |
11936 | if (PROCESSING_REAL_TEMPLATE_DECL_P () | |
11937 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type))) | |
11938 | type = push_template_decl (type); | |
11939 | type = TREE_TYPE (type); | |
11940 | if (nested_name_specifier) | |
eeb23c11 MM |
11941 | { |
11942 | *nested_name_specifier_p = true; | |
11943 | pop_scope (nested_name_specifier); | |
11944 | } | |
a723baf1 MM |
11945 | } |
11946 | /* Indicate whether this class was declared as a `class' or as a | |
11947 | `struct'. */ | |
11948 | if (TREE_CODE (type) == RECORD_TYPE) | |
11949 | CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type); | |
11950 | cp_parser_check_class_key (class_key, type); | |
11951 | ||
11952 | /* Enter the scope containing the class; the names of base classes | |
11953 | should be looked up in that context. For example, given: | |
11954 | ||
11955 | struct A { struct B {}; struct C; }; | |
11956 | struct A::C : B {}; | |
11957 | ||
11958 | is valid. */ | |
11959 | if (nested_name_specifier) | |
11960 | push_scope (nested_name_specifier); | |
11961 | /* Now, look for the base-clause. */ | |
11962 | token = cp_lexer_peek_token (parser->lexer); | |
11963 | if (token->type == CPP_COLON) | |
11964 | { | |
11965 | tree bases; | |
11966 | ||
11967 | /* Get the list of base-classes. */ | |
11968 | bases = cp_parser_base_clause (parser); | |
11969 | /* Process them. */ | |
11970 | xref_basetypes (type, bases); | |
11971 | } | |
11972 | /* Leave the scope given by the nested-name-specifier. We will | |
11973 | enter the class scope itself while processing the members. */ | |
11974 | if (nested_name_specifier) | |
11975 | pop_scope (nested_name_specifier); | |
11976 | ||
afb0918a MM |
11977 | done: |
11978 | if (invalid_explicit_specialization_p) | |
11979 | { | |
11980 | end_specialization (); | |
11981 | --parser->num_template_parameter_lists; | |
11982 | } | |
a723baf1 MM |
11983 | return type; |
11984 | } | |
11985 | ||
11986 | /* Parse a class-key. | |
11987 | ||
11988 | class-key: | |
11989 | class | |
11990 | struct | |
11991 | union | |
11992 | ||
11993 | Returns the kind of class-key specified, or none_type to indicate | |
11994 | error. */ | |
11995 | ||
11996 | static enum tag_types | |
94edc4ab | 11997 | cp_parser_class_key (cp_parser* parser) |
a723baf1 MM |
11998 | { |
11999 | cp_token *token; | |
12000 | enum tag_types tag_type; | |
12001 | ||
12002 | /* Look for the class-key. */ | |
12003 | token = cp_parser_require (parser, CPP_KEYWORD, "class-key"); | |
12004 | if (!token) | |
12005 | return none_type; | |
12006 | ||
12007 | /* Check to see if the TOKEN is a class-key. */ | |
12008 | tag_type = cp_parser_token_is_class_key (token); | |
12009 | if (!tag_type) | |
12010 | cp_parser_error (parser, "expected class-key"); | |
12011 | return tag_type; | |
12012 | } | |
12013 | ||
12014 | /* Parse an (optional) member-specification. | |
12015 | ||
12016 | member-specification: | |
12017 | member-declaration member-specification [opt] | |
12018 | access-specifier : member-specification [opt] */ | |
12019 | ||
12020 | static void | |
94edc4ab | 12021 | cp_parser_member_specification_opt (cp_parser* parser) |
a723baf1 MM |
12022 | { |
12023 | while (true) | |
12024 | { | |
12025 | cp_token *token; | |
12026 | enum rid keyword; | |
12027 | ||
12028 | /* Peek at the next token. */ | |
12029 | token = cp_lexer_peek_token (parser->lexer); | |
12030 | /* If it's a `}', or EOF then we've seen all the members. */ | |
12031 | if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF) | |
12032 | break; | |
12033 | ||
12034 | /* See if this token is a keyword. */ | |
12035 | keyword = token->keyword; | |
12036 | switch (keyword) | |
12037 | { | |
12038 | case RID_PUBLIC: | |
12039 | case RID_PROTECTED: | |
12040 | case RID_PRIVATE: | |
12041 | /* Consume the access-specifier. */ | |
12042 | cp_lexer_consume_token (parser->lexer); | |
12043 | /* Remember which access-specifier is active. */ | |
12044 | current_access_specifier = token->value; | |
12045 | /* Look for the `:'. */ | |
12046 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
12047 | break; | |
12048 | ||
12049 | default: | |
12050 | /* Otherwise, the next construction must be a | |
12051 | member-declaration. */ | |
12052 | cp_parser_member_declaration (parser); | |
a723baf1 MM |
12053 | } |
12054 | } | |
12055 | } | |
12056 | ||
12057 | /* Parse a member-declaration. | |
12058 | ||
12059 | member-declaration: | |
12060 | decl-specifier-seq [opt] member-declarator-list [opt] ; | |
12061 | function-definition ; [opt] | |
12062 | :: [opt] nested-name-specifier template [opt] unqualified-id ; | |
12063 | using-declaration | |
12064 | template-declaration | |
12065 | ||
12066 | member-declarator-list: | |
12067 | member-declarator | |
12068 | member-declarator-list , member-declarator | |
12069 | ||
12070 | member-declarator: | |
12071 | declarator pure-specifier [opt] | |
12072 | declarator constant-initializer [opt] | |
12073 | identifier [opt] : constant-expression | |
12074 | ||
12075 | GNU Extensions: | |
12076 | ||
12077 | member-declaration: | |
12078 | __extension__ member-declaration | |
12079 | ||
12080 | member-declarator: | |
12081 | declarator attributes [opt] pure-specifier [opt] | |
12082 | declarator attributes [opt] constant-initializer [opt] | |
12083 | identifier [opt] attributes [opt] : constant-expression */ | |
12084 | ||
12085 | static void | |
94edc4ab | 12086 | cp_parser_member_declaration (cp_parser* parser) |
a723baf1 MM |
12087 | { |
12088 | tree decl_specifiers; | |
12089 | tree prefix_attributes; | |
12090 | tree decl; | |
560ad596 | 12091 | int declares_class_or_enum; |
a723baf1 MM |
12092 | bool friend_p; |
12093 | cp_token *token; | |
12094 | int saved_pedantic; | |
12095 | ||
12096 | /* Check for the `__extension__' keyword. */ | |
12097 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
12098 | { | |
12099 | /* Recurse. */ | |
12100 | cp_parser_member_declaration (parser); | |
12101 | /* Restore the old value of the PEDANTIC flag. */ | |
12102 | pedantic = saved_pedantic; | |
12103 | ||
12104 | return; | |
12105 | } | |
12106 | ||
12107 | /* Check for a template-declaration. */ | |
12108 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
12109 | { | |
12110 | /* Parse the template-declaration. */ | |
12111 | cp_parser_template_declaration (parser, /*member_p=*/true); | |
12112 | ||
12113 | return; | |
12114 | } | |
12115 | ||
12116 | /* Check for a using-declaration. */ | |
12117 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING)) | |
12118 | { | |
12119 | /* Parse the using-declaration. */ | |
12120 | cp_parser_using_declaration (parser); | |
12121 | ||
12122 | return; | |
12123 | } | |
12124 | ||
a723baf1 MM |
12125 | /* Parse the decl-specifier-seq. */ |
12126 | decl_specifiers | |
12127 | = cp_parser_decl_specifier_seq (parser, | |
12128 | CP_PARSER_FLAGS_OPTIONAL, | |
12129 | &prefix_attributes, | |
12130 | &declares_class_or_enum); | |
8fbc5ae7 MM |
12131 | /* Check for an invalid type-name. */ |
12132 | if (cp_parser_diagnose_invalid_type_name (parser)) | |
12133 | return; | |
a723baf1 MM |
12134 | /* If there is no declarator, then the decl-specifier-seq should |
12135 | specify a type. */ | |
12136 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
12137 | { | |
12138 | /* If there was no decl-specifier-seq, and the next token is a | |
12139 | `;', then we have something like: | |
12140 | ||
12141 | struct S { ; }; | |
12142 | ||
12143 | [class.mem] | |
12144 | ||
12145 | Each member-declaration shall declare at least one member | |
12146 | name of the class. */ | |
12147 | if (!decl_specifiers) | |
12148 | { | |
12149 | if (pedantic) | |
12150 | pedwarn ("extra semicolon"); | |
12151 | } | |
12152 | else | |
12153 | { | |
12154 | tree type; | |
12155 | ||
12156 | /* See if this declaration is a friend. */ | |
12157 | friend_p = cp_parser_friend_p (decl_specifiers); | |
12158 | /* If there were decl-specifiers, check to see if there was | |
12159 | a class-declaration. */ | |
12160 | type = check_tag_decl (decl_specifiers); | |
12161 | /* Nested classes have already been added to the class, but | |
12162 | a `friend' needs to be explicitly registered. */ | |
12163 | if (friend_p) | |
12164 | { | |
12165 | /* If the `friend' keyword was present, the friend must | |
12166 | be introduced with a class-key. */ | |
12167 | if (!declares_class_or_enum) | |
12168 | error ("a class-key must be used when declaring a friend"); | |
12169 | /* In this case: | |
12170 | ||
12171 | template <typename T> struct A { | |
12172 | friend struct A<T>::B; | |
12173 | }; | |
12174 | ||
12175 | A<T>::B will be represented by a TYPENAME_TYPE, and | |
12176 | therefore not recognized by check_tag_decl. */ | |
12177 | if (!type) | |
12178 | { | |
12179 | tree specifier; | |
12180 | ||
12181 | for (specifier = decl_specifiers; | |
12182 | specifier; | |
12183 | specifier = TREE_CHAIN (specifier)) | |
12184 | { | |
12185 | tree s = TREE_VALUE (specifier); | |
12186 | ||
c003e212 GDR |
12187 | if (TREE_CODE (s) == IDENTIFIER_NODE) |
12188 | get_global_value_if_present (s, &type); | |
a723baf1 MM |
12189 | if (TREE_CODE (s) == TYPE_DECL) |
12190 | s = TREE_TYPE (s); | |
12191 | if (TYPE_P (s)) | |
12192 | { | |
12193 | type = s; | |
12194 | break; | |
12195 | } | |
12196 | } | |
12197 | } | |
12198 | if (!type) | |
12199 | error ("friend declaration does not name a class or " | |
12200 | "function"); | |
12201 | else | |
19db77ce KL |
12202 | make_friend_class (current_class_type, type, |
12203 | /*complain=*/true); | |
a723baf1 MM |
12204 | } |
12205 | /* If there is no TYPE, an error message will already have | |
12206 | been issued. */ | |
12207 | else if (!type) | |
12208 | ; | |
12209 | /* An anonymous aggregate has to be handled specially; such | |
12210 | a declaration really declares a data member (with a | |
12211 | particular type), as opposed to a nested class. */ | |
12212 | else if (ANON_AGGR_TYPE_P (type)) | |
12213 | { | |
12214 | /* Remove constructors and such from TYPE, now that we | |
34cd5ae7 | 12215 | know it is an anonymous aggregate. */ |
a723baf1 MM |
12216 | fixup_anonymous_aggr (type); |
12217 | /* And make the corresponding data member. */ | |
12218 | decl = build_decl (FIELD_DECL, NULL_TREE, type); | |
12219 | /* Add it to the class. */ | |
12220 | finish_member_declaration (decl); | |
12221 | } | |
37d407a1 KL |
12222 | else |
12223 | cp_parser_check_access_in_redeclaration (TYPE_NAME (type)); | |
a723baf1 MM |
12224 | } |
12225 | } | |
12226 | else | |
12227 | { | |
12228 | /* See if these declarations will be friends. */ | |
12229 | friend_p = cp_parser_friend_p (decl_specifiers); | |
12230 | ||
12231 | /* Keep going until we hit the `;' at the end of the | |
12232 | declaration. */ | |
12233 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
12234 | { | |
12235 | tree attributes = NULL_TREE; | |
12236 | tree first_attribute; | |
12237 | ||
12238 | /* Peek at the next token. */ | |
12239 | token = cp_lexer_peek_token (parser->lexer); | |
12240 | ||
12241 | /* Check for a bitfield declaration. */ | |
12242 | if (token->type == CPP_COLON | |
12243 | || (token->type == CPP_NAME | |
12244 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
12245 | == CPP_COLON)) | |
12246 | { | |
12247 | tree identifier; | |
12248 | tree width; | |
12249 | ||
12250 | /* Get the name of the bitfield. Note that we cannot just | |
12251 | check TOKEN here because it may have been invalidated by | |
12252 | the call to cp_lexer_peek_nth_token above. */ | |
12253 | if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON) | |
12254 | identifier = cp_parser_identifier (parser); | |
12255 | else | |
12256 | identifier = NULL_TREE; | |
12257 | ||
12258 | /* Consume the `:' token. */ | |
12259 | cp_lexer_consume_token (parser->lexer); | |
12260 | /* Get the width of the bitfield. */ | |
14d22dd6 MM |
12261 | width |
12262 | = cp_parser_constant_expression (parser, | |
12263 | /*allow_non_constant=*/false, | |
12264 | NULL); | |
a723baf1 MM |
12265 | |
12266 | /* Look for attributes that apply to the bitfield. */ | |
12267 | attributes = cp_parser_attributes_opt (parser); | |
12268 | /* Remember which attributes are prefix attributes and | |
12269 | which are not. */ | |
12270 | first_attribute = attributes; | |
12271 | /* Combine the attributes. */ | |
12272 | attributes = chainon (prefix_attributes, attributes); | |
12273 | ||
12274 | /* Create the bitfield declaration. */ | |
12275 | decl = grokbitfield (identifier, | |
12276 | decl_specifiers, | |
12277 | width); | |
12278 | /* Apply the attributes. */ | |
12279 | cplus_decl_attributes (&decl, attributes, /*flags=*/0); | |
12280 | } | |
12281 | else | |
12282 | { | |
12283 | tree declarator; | |
12284 | tree initializer; | |
12285 | tree asm_specification; | |
7efa3e22 | 12286 | int ctor_dtor_or_conv_p; |
a723baf1 MM |
12287 | |
12288 | /* Parse the declarator. */ | |
12289 | declarator | |
62b8a44e | 12290 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
12291 | &ctor_dtor_or_conv_p, |
12292 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
12293 | |
12294 | /* If something went wrong parsing the declarator, make sure | |
12295 | that we at least consume some tokens. */ | |
12296 | if (declarator == error_mark_node) | |
12297 | { | |
12298 | /* Skip to the end of the statement. */ | |
12299 | cp_parser_skip_to_end_of_statement (parser); | |
4bb8ca28 MM |
12300 | /* If the next token is not a semicolon, that is |
12301 | probably because we just skipped over the body of | |
12302 | a function. So, we consume a semicolon if | |
12303 | present, but do not issue an error message if it | |
12304 | is not present. */ | |
12305 | if (cp_lexer_next_token_is (parser->lexer, | |
12306 | CPP_SEMICOLON)) | |
12307 | cp_lexer_consume_token (parser->lexer); | |
12308 | return; | |
a723baf1 MM |
12309 | } |
12310 | ||
560ad596 MM |
12311 | cp_parser_check_for_definition_in_return_type |
12312 | (declarator, declares_class_or_enum); | |
12313 | ||
a723baf1 MM |
12314 | /* Look for an asm-specification. */ |
12315 | asm_specification = cp_parser_asm_specification_opt (parser); | |
12316 | /* Look for attributes that apply to the declaration. */ | |
12317 | attributes = cp_parser_attributes_opt (parser); | |
12318 | /* Remember which attributes are prefix attributes and | |
12319 | which are not. */ | |
12320 | first_attribute = attributes; | |
12321 | /* Combine the attributes. */ | |
12322 | attributes = chainon (prefix_attributes, attributes); | |
12323 | ||
12324 | /* If it's an `=', then we have a constant-initializer or a | |
12325 | pure-specifier. It is not correct to parse the | |
12326 | initializer before registering the member declaration | |
12327 | since the member declaration should be in scope while | |
12328 | its initializer is processed. However, the rest of the | |
12329 | front end does not yet provide an interface that allows | |
12330 | us to handle this correctly. */ | |
12331 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
12332 | { | |
12333 | /* In [class.mem]: | |
12334 | ||
12335 | A pure-specifier shall be used only in the declaration of | |
12336 | a virtual function. | |
12337 | ||
12338 | A member-declarator can contain a constant-initializer | |
12339 | only if it declares a static member of integral or | |
12340 | enumeration type. | |
12341 | ||
12342 | Therefore, if the DECLARATOR is for a function, we look | |
12343 | for a pure-specifier; otherwise, we look for a | |
12344 | constant-initializer. When we call `grokfield', it will | |
12345 | perform more stringent semantics checks. */ | |
12346 | if (TREE_CODE (declarator) == CALL_EXPR) | |
12347 | initializer = cp_parser_pure_specifier (parser); | |
12348 | else | |
4bb8ca28 MM |
12349 | /* Parse the initializer. */ |
12350 | initializer = cp_parser_constant_initializer (parser); | |
a723baf1 MM |
12351 | } |
12352 | /* Otherwise, there is no initializer. */ | |
12353 | else | |
12354 | initializer = NULL_TREE; | |
12355 | ||
12356 | /* See if we are probably looking at a function | |
12357 | definition. We are certainly not looking at at a | |
12358 | member-declarator. Calling `grokfield' has | |
12359 | side-effects, so we must not do it unless we are sure | |
12360 | that we are looking at a member-declarator. */ | |
12361 | if (cp_parser_token_starts_function_definition_p | |
12362 | (cp_lexer_peek_token (parser->lexer))) | |
4bb8ca28 MM |
12363 | { |
12364 | /* The grammar does not allow a pure-specifier to be | |
12365 | used when a member function is defined. (It is | |
12366 | possible that this fact is an oversight in the | |
12367 | standard, since a pure function may be defined | |
12368 | outside of the class-specifier. */ | |
12369 | if (initializer) | |
12370 | error ("pure-specifier on function-definition"); | |
12371 | decl = cp_parser_save_member_function_body (parser, | |
12372 | decl_specifiers, | |
12373 | declarator, | |
12374 | attributes); | |
12375 | /* If the member was not a friend, declare it here. */ | |
12376 | if (!friend_p) | |
12377 | finish_member_declaration (decl); | |
12378 | /* Peek at the next token. */ | |
12379 | token = cp_lexer_peek_token (parser->lexer); | |
12380 | /* If the next token is a semicolon, consume it. */ | |
12381 | if (token->type == CPP_SEMICOLON) | |
12382 | cp_lexer_consume_token (parser->lexer); | |
12383 | return; | |
12384 | } | |
a723baf1 | 12385 | else |
39703eb9 MM |
12386 | { |
12387 | /* Create the declaration. */ | |
ee3071ef NS |
12388 | decl = grokfield (declarator, decl_specifiers, |
12389 | initializer, asm_specification, | |
39703eb9 MM |
12390 | attributes); |
12391 | /* Any initialization must have been from a | |
12392 | constant-expression. */ | |
12393 | if (decl && TREE_CODE (decl) == VAR_DECL && initializer) | |
12394 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; | |
12395 | } | |
a723baf1 MM |
12396 | } |
12397 | ||
12398 | /* Reset PREFIX_ATTRIBUTES. */ | |
12399 | while (attributes && TREE_CHAIN (attributes) != first_attribute) | |
12400 | attributes = TREE_CHAIN (attributes); | |
12401 | if (attributes) | |
12402 | TREE_CHAIN (attributes) = NULL_TREE; | |
12403 | ||
12404 | /* If there is any qualification still in effect, clear it | |
12405 | now; we will be starting fresh with the next declarator. */ | |
12406 | parser->scope = NULL_TREE; | |
12407 | parser->qualifying_scope = NULL_TREE; | |
12408 | parser->object_scope = NULL_TREE; | |
12409 | /* If it's a `,', then there are more declarators. */ | |
12410 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
12411 | cp_lexer_consume_token (parser->lexer); | |
12412 | /* If the next token isn't a `;', then we have a parse error. */ | |
12413 | else if (cp_lexer_next_token_is_not (parser->lexer, | |
12414 | CPP_SEMICOLON)) | |
12415 | { | |
12416 | cp_parser_error (parser, "expected `;'"); | |
04c06002 | 12417 | /* Skip tokens until we find a `;'. */ |
a723baf1 MM |
12418 | cp_parser_skip_to_end_of_statement (parser); |
12419 | ||
12420 | break; | |
12421 | } | |
12422 | ||
12423 | if (decl) | |
12424 | { | |
12425 | /* Add DECL to the list of members. */ | |
12426 | if (!friend_p) | |
12427 | finish_member_declaration (decl); | |
12428 | ||
a723baf1 | 12429 | if (TREE_CODE (decl) == FUNCTION_DECL) |
8db1028e | 12430 | cp_parser_save_default_args (parser, decl); |
a723baf1 MM |
12431 | } |
12432 | } | |
12433 | } | |
12434 | ||
4bb8ca28 | 12435 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
a723baf1 MM |
12436 | } |
12437 | ||
12438 | /* Parse a pure-specifier. | |
12439 | ||
12440 | pure-specifier: | |
12441 | = 0 | |
12442 | ||
12443 | Returns INTEGER_ZERO_NODE if a pure specifier is found. | |
cd0be382 | 12444 | Otherwise, ERROR_MARK_NODE is returned. */ |
a723baf1 MM |
12445 | |
12446 | static tree | |
94edc4ab | 12447 | cp_parser_pure_specifier (cp_parser* parser) |
a723baf1 MM |
12448 | { |
12449 | cp_token *token; | |
12450 | ||
12451 | /* Look for the `=' token. */ | |
12452 | if (!cp_parser_require (parser, CPP_EQ, "`='")) | |
12453 | return error_mark_node; | |
12454 | /* Look for the `0' token. */ | |
12455 | token = cp_parser_require (parser, CPP_NUMBER, "`0'"); | |
12456 | /* Unfortunately, this will accept `0L' and `0x00' as well. We need | |
12457 | to get information from the lexer about how the number was | |
12458 | spelled in order to fix this problem. */ | |
12459 | if (!token || !integer_zerop (token->value)) | |
12460 | return error_mark_node; | |
12461 | ||
12462 | return integer_zero_node; | |
12463 | } | |
12464 | ||
12465 | /* Parse a constant-initializer. | |
12466 | ||
12467 | constant-initializer: | |
12468 | = constant-expression | |
12469 | ||
12470 | Returns a representation of the constant-expression. */ | |
12471 | ||
12472 | static tree | |
94edc4ab | 12473 | cp_parser_constant_initializer (cp_parser* parser) |
a723baf1 MM |
12474 | { |
12475 | /* Look for the `=' token. */ | |
12476 | if (!cp_parser_require (parser, CPP_EQ, "`='")) | |
12477 | return error_mark_node; | |
12478 | ||
12479 | /* It is invalid to write: | |
12480 | ||
12481 | struct S { static const int i = { 7 }; }; | |
12482 | ||
12483 | */ | |
12484 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
12485 | { | |
12486 | cp_parser_error (parser, | |
12487 | "a brace-enclosed initializer is not allowed here"); | |
12488 | /* Consume the opening brace. */ | |
12489 | cp_lexer_consume_token (parser->lexer); | |
12490 | /* Skip the initializer. */ | |
12491 | cp_parser_skip_to_closing_brace (parser); | |
12492 | /* Look for the trailing `}'. */ | |
12493 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
12494 | ||
12495 | return error_mark_node; | |
12496 | } | |
12497 | ||
14d22dd6 MM |
12498 | return cp_parser_constant_expression (parser, |
12499 | /*allow_non_constant=*/false, | |
12500 | NULL); | |
a723baf1 MM |
12501 | } |
12502 | ||
12503 | /* Derived classes [gram.class.derived] */ | |
12504 | ||
12505 | /* Parse a base-clause. | |
12506 | ||
12507 | base-clause: | |
12508 | : base-specifier-list | |
12509 | ||
12510 | base-specifier-list: | |
12511 | base-specifier | |
12512 | base-specifier-list , base-specifier | |
12513 | ||
12514 | Returns a TREE_LIST representing the base-classes, in the order in | |
12515 | which they were declared. The representation of each node is as | |
12516 | described by cp_parser_base_specifier. | |
12517 | ||
12518 | In the case that no bases are specified, this function will return | |
12519 | NULL_TREE, not ERROR_MARK_NODE. */ | |
12520 | ||
12521 | static tree | |
94edc4ab | 12522 | cp_parser_base_clause (cp_parser* parser) |
a723baf1 MM |
12523 | { |
12524 | tree bases = NULL_TREE; | |
12525 | ||
12526 | /* Look for the `:' that begins the list. */ | |
12527 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
12528 | ||
12529 | /* Scan the base-specifier-list. */ | |
12530 | while (true) | |
12531 | { | |
12532 | cp_token *token; | |
12533 | tree base; | |
12534 | ||
12535 | /* Look for the base-specifier. */ | |
12536 | base = cp_parser_base_specifier (parser); | |
12537 | /* Add BASE to the front of the list. */ | |
12538 | if (base != error_mark_node) | |
12539 | { | |
12540 | TREE_CHAIN (base) = bases; | |
12541 | bases = base; | |
12542 | } | |
12543 | /* Peek at the next token. */ | |
12544 | token = cp_lexer_peek_token (parser->lexer); | |
12545 | /* If it's not a comma, then the list is complete. */ | |
12546 | if (token->type != CPP_COMMA) | |
12547 | break; | |
12548 | /* Consume the `,'. */ | |
12549 | cp_lexer_consume_token (parser->lexer); | |
12550 | } | |
12551 | ||
12552 | /* PARSER->SCOPE may still be non-NULL at this point, if the last | |
12553 | base class had a qualified name. However, the next name that | |
12554 | appears is certainly not qualified. */ | |
12555 | parser->scope = NULL_TREE; | |
12556 | parser->qualifying_scope = NULL_TREE; | |
12557 | parser->object_scope = NULL_TREE; | |
12558 | ||
12559 | return nreverse (bases); | |
12560 | } | |
12561 | ||
12562 | /* Parse a base-specifier. | |
12563 | ||
12564 | base-specifier: | |
12565 | :: [opt] nested-name-specifier [opt] class-name | |
12566 | virtual access-specifier [opt] :: [opt] nested-name-specifier | |
12567 | [opt] class-name | |
12568 | access-specifier virtual [opt] :: [opt] nested-name-specifier | |
12569 | [opt] class-name | |
12570 | ||
12571 | Returns a TREE_LIST. The TREE_PURPOSE will be one of | |
12572 | ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to | |
12573 | indicate the specifiers provided. The TREE_VALUE will be a TYPE | |
12574 | (or the ERROR_MARK_NODE) indicating the type that was specified. */ | |
12575 | ||
12576 | static tree | |
94edc4ab | 12577 | cp_parser_base_specifier (cp_parser* parser) |
a723baf1 MM |
12578 | { |
12579 | cp_token *token; | |
12580 | bool done = false; | |
12581 | bool virtual_p = false; | |
12582 | bool duplicate_virtual_error_issued_p = false; | |
12583 | bool duplicate_access_error_issued_p = false; | |
bbaab916 | 12584 | bool class_scope_p, template_p; |
dbbf88d1 | 12585 | tree access = access_default_node; |
a723baf1 MM |
12586 | tree type; |
12587 | ||
12588 | /* Process the optional `virtual' and `access-specifier'. */ | |
12589 | while (!done) | |
12590 | { | |
12591 | /* Peek at the next token. */ | |
12592 | token = cp_lexer_peek_token (parser->lexer); | |
12593 | /* Process `virtual'. */ | |
12594 | switch (token->keyword) | |
12595 | { | |
12596 | case RID_VIRTUAL: | |
12597 | /* If `virtual' appears more than once, issue an error. */ | |
12598 | if (virtual_p && !duplicate_virtual_error_issued_p) | |
12599 | { | |
12600 | cp_parser_error (parser, | |
12601 | "`virtual' specified more than once in base-specified"); | |
12602 | duplicate_virtual_error_issued_p = true; | |
12603 | } | |
12604 | ||
12605 | virtual_p = true; | |
12606 | ||
12607 | /* Consume the `virtual' token. */ | |
12608 | cp_lexer_consume_token (parser->lexer); | |
12609 | ||
12610 | break; | |
12611 | ||
12612 | case RID_PUBLIC: | |
12613 | case RID_PROTECTED: | |
12614 | case RID_PRIVATE: | |
12615 | /* If more than one access specifier appears, issue an | |
12616 | error. */ | |
dbbf88d1 NS |
12617 | if (access != access_default_node |
12618 | && !duplicate_access_error_issued_p) | |
a723baf1 MM |
12619 | { |
12620 | cp_parser_error (parser, | |
12621 | "more than one access specifier in base-specified"); | |
12622 | duplicate_access_error_issued_p = true; | |
12623 | } | |
12624 | ||
dbbf88d1 | 12625 | access = ridpointers[(int) token->keyword]; |
a723baf1 MM |
12626 | |
12627 | /* Consume the access-specifier. */ | |
12628 | cp_lexer_consume_token (parser->lexer); | |
12629 | ||
12630 | break; | |
12631 | ||
12632 | default: | |
12633 | done = true; | |
12634 | break; | |
12635 | } | |
12636 | } | |
12637 | ||
a723baf1 MM |
12638 | /* Look for the optional `::' operator. */ |
12639 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); | |
12640 | /* Look for the nested-name-specifier. The simplest way to | |
12641 | implement: | |
12642 | ||
12643 | [temp.res] | |
12644 | ||
12645 | The keyword `typename' is not permitted in a base-specifier or | |
12646 | mem-initializer; in these contexts a qualified name that | |
12647 | depends on a template-parameter is implicitly assumed to be a | |
12648 | type name. | |
12649 | ||
12650 | is to pretend that we have seen the `typename' keyword at this | |
12651 | point. */ | |
12652 | cp_parser_nested_name_specifier_opt (parser, | |
12653 | /*typename_keyword_p=*/true, | |
12654 | /*check_dependency_p=*/true, | |
a668c6ad MM |
12655 | /*type_p=*/true, |
12656 | /*is_declaration=*/true); | |
a723baf1 MM |
12657 | /* If the base class is given by a qualified name, assume that names |
12658 | we see are type names or templates, as appropriate. */ | |
12659 | class_scope_p = (parser->scope && TYPE_P (parser->scope)); | |
bbaab916 NS |
12660 | template_p = class_scope_p && cp_parser_optional_template_keyword (parser); |
12661 | ||
a723baf1 MM |
12662 | /* Finally, look for the class-name. */ |
12663 | type = cp_parser_class_name (parser, | |
12664 | class_scope_p, | |
bbaab916 | 12665 | template_p, |
a723baf1 | 12666 | /*type_p=*/true, |
a723baf1 | 12667 | /*check_dependency_p=*/true, |
a668c6ad MM |
12668 | /*class_head_p=*/false, |
12669 | /*is_declaration=*/true); | |
a723baf1 MM |
12670 | |
12671 | if (type == error_mark_node) | |
12672 | return error_mark_node; | |
12673 | ||
dbbf88d1 | 12674 | return finish_base_specifier (TREE_TYPE (type), access, virtual_p); |
a723baf1 MM |
12675 | } |
12676 | ||
12677 | /* Exception handling [gram.exception] */ | |
12678 | ||
12679 | /* Parse an (optional) exception-specification. | |
12680 | ||
12681 | exception-specification: | |
12682 | throw ( type-id-list [opt] ) | |
12683 | ||
12684 | Returns a TREE_LIST representing the exception-specification. The | |
12685 | TREE_VALUE of each node is a type. */ | |
12686 | ||
12687 | static tree | |
94edc4ab | 12688 | cp_parser_exception_specification_opt (cp_parser* parser) |
a723baf1 MM |
12689 | { |
12690 | cp_token *token; | |
12691 | tree type_id_list; | |
12692 | ||
12693 | /* Peek at the next token. */ | |
12694 | token = cp_lexer_peek_token (parser->lexer); | |
12695 | /* If it's not `throw', then there's no exception-specification. */ | |
12696 | if (!cp_parser_is_keyword (token, RID_THROW)) | |
12697 | return NULL_TREE; | |
12698 | ||
12699 | /* Consume the `throw'. */ | |
12700 | cp_lexer_consume_token (parser->lexer); | |
12701 | ||
12702 | /* Look for the `('. */ | |
12703 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
12704 | ||
12705 | /* Peek at the next token. */ | |
12706 | token = cp_lexer_peek_token (parser->lexer); | |
12707 | /* If it's not a `)', then there is a type-id-list. */ | |
12708 | if (token->type != CPP_CLOSE_PAREN) | |
12709 | { | |
12710 | const char *saved_message; | |
12711 | ||
12712 | /* Types may not be defined in an exception-specification. */ | |
12713 | saved_message = parser->type_definition_forbidden_message; | |
12714 | parser->type_definition_forbidden_message | |
12715 | = "types may not be defined in an exception-specification"; | |
12716 | /* Parse the type-id-list. */ | |
12717 | type_id_list = cp_parser_type_id_list (parser); | |
12718 | /* Restore the saved message. */ | |
12719 | parser->type_definition_forbidden_message = saved_message; | |
12720 | } | |
12721 | else | |
12722 | type_id_list = empty_except_spec; | |
12723 | ||
12724 | /* Look for the `)'. */ | |
12725 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
12726 | ||
12727 | return type_id_list; | |
12728 | } | |
12729 | ||
12730 | /* Parse an (optional) type-id-list. | |
12731 | ||
12732 | type-id-list: | |
12733 | type-id | |
12734 | type-id-list , type-id | |
12735 | ||
12736 | Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE, | |
12737 | in the order that the types were presented. */ | |
12738 | ||
12739 | static tree | |
94edc4ab | 12740 | cp_parser_type_id_list (cp_parser* parser) |
a723baf1 MM |
12741 | { |
12742 | tree types = NULL_TREE; | |
12743 | ||
12744 | while (true) | |
12745 | { | |
12746 | cp_token *token; | |
12747 | tree type; | |
12748 | ||
12749 | /* Get the next type-id. */ | |
12750 | type = cp_parser_type_id (parser); | |
12751 | /* Add it to the list. */ | |
12752 | types = add_exception_specifier (types, type, /*complain=*/1); | |
12753 | /* Peek at the next token. */ | |
12754 | token = cp_lexer_peek_token (parser->lexer); | |
12755 | /* If it is not a `,', we are done. */ | |
12756 | if (token->type != CPP_COMMA) | |
12757 | break; | |
12758 | /* Consume the `,'. */ | |
12759 | cp_lexer_consume_token (parser->lexer); | |
12760 | } | |
12761 | ||
12762 | return nreverse (types); | |
12763 | } | |
12764 | ||
12765 | /* Parse a try-block. | |
12766 | ||
12767 | try-block: | |
12768 | try compound-statement handler-seq */ | |
12769 | ||
12770 | static tree | |
94edc4ab | 12771 | cp_parser_try_block (cp_parser* parser) |
a723baf1 MM |
12772 | { |
12773 | tree try_block; | |
12774 | ||
12775 | cp_parser_require_keyword (parser, RID_TRY, "`try'"); | |
12776 | try_block = begin_try_block (); | |
a5bcc582 | 12777 | cp_parser_compound_statement (parser, false); |
a723baf1 MM |
12778 | finish_try_block (try_block); |
12779 | cp_parser_handler_seq (parser); | |
12780 | finish_handler_sequence (try_block); | |
12781 | ||
12782 | return try_block; | |
12783 | } | |
12784 | ||
12785 | /* Parse a function-try-block. | |
12786 | ||
12787 | function-try-block: | |
12788 | try ctor-initializer [opt] function-body handler-seq */ | |
12789 | ||
12790 | static bool | |
94edc4ab | 12791 | cp_parser_function_try_block (cp_parser* parser) |
a723baf1 MM |
12792 | { |
12793 | tree try_block; | |
12794 | bool ctor_initializer_p; | |
12795 | ||
12796 | /* Look for the `try' keyword. */ | |
12797 | if (!cp_parser_require_keyword (parser, RID_TRY, "`try'")) | |
12798 | return false; | |
12799 | /* Let the rest of the front-end know where we are. */ | |
12800 | try_block = begin_function_try_block (); | |
12801 | /* Parse the function-body. */ | |
12802 | ctor_initializer_p | |
12803 | = cp_parser_ctor_initializer_opt_and_function_body (parser); | |
12804 | /* We're done with the `try' part. */ | |
12805 | finish_function_try_block (try_block); | |
12806 | /* Parse the handlers. */ | |
12807 | cp_parser_handler_seq (parser); | |
12808 | /* We're done with the handlers. */ | |
12809 | finish_function_handler_sequence (try_block); | |
12810 | ||
12811 | return ctor_initializer_p; | |
12812 | } | |
12813 | ||
12814 | /* Parse a handler-seq. | |
12815 | ||
12816 | handler-seq: | |
12817 | handler handler-seq [opt] */ | |
12818 | ||
12819 | static void | |
94edc4ab | 12820 | cp_parser_handler_seq (cp_parser* parser) |
a723baf1 MM |
12821 | { |
12822 | while (true) | |
12823 | { | |
12824 | cp_token *token; | |
12825 | ||
12826 | /* Parse the handler. */ | |
12827 | cp_parser_handler (parser); | |
12828 | /* Peek at the next token. */ | |
12829 | token = cp_lexer_peek_token (parser->lexer); | |
12830 | /* If it's not `catch' then there are no more handlers. */ | |
12831 | if (!cp_parser_is_keyword (token, RID_CATCH)) | |
12832 | break; | |
12833 | } | |
12834 | } | |
12835 | ||
12836 | /* Parse a handler. | |
12837 | ||
12838 | handler: | |
12839 | catch ( exception-declaration ) compound-statement */ | |
12840 | ||
12841 | static void | |
94edc4ab | 12842 | cp_parser_handler (cp_parser* parser) |
a723baf1 MM |
12843 | { |
12844 | tree handler; | |
12845 | tree declaration; | |
12846 | ||
12847 | cp_parser_require_keyword (parser, RID_CATCH, "`catch'"); | |
12848 | handler = begin_handler (); | |
12849 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
12850 | declaration = cp_parser_exception_declaration (parser); | |
12851 | finish_handler_parms (declaration, handler); | |
12852 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
a5bcc582 | 12853 | cp_parser_compound_statement (parser, false); |
a723baf1 MM |
12854 | finish_handler (handler); |
12855 | } | |
12856 | ||
12857 | /* Parse an exception-declaration. | |
12858 | ||
12859 | exception-declaration: | |
12860 | type-specifier-seq declarator | |
12861 | type-specifier-seq abstract-declarator | |
12862 | type-specifier-seq | |
12863 | ... | |
12864 | ||
12865 | Returns a VAR_DECL for the declaration, or NULL_TREE if the | |
12866 | ellipsis variant is used. */ | |
12867 | ||
12868 | static tree | |
94edc4ab | 12869 | cp_parser_exception_declaration (cp_parser* parser) |
a723baf1 MM |
12870 | { |
12871 | tree type_specifiers; | |
12872 | tree declarator; | |
12873 | const char *saved_message; | |
12874 | ||
12875 | /* If it's an ellipsis, it's easy to handle. */ | |
12876 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
12877 | { | |
12878 | /* Consume the `...' token. */ | |
12879 | cp_lexer_consume_token (parser->lexer); | |
12880 | return NULL_TREE; | |
12881 | } | |
12882 | ||
12883 | /* Types may not be defined in exception-declarations. */ | |
12884 | saved_message = parser->type_definition_forbidden_message; | |
12885 | parser->type_definition_forbidden_message | |
12886 | = "types may not be defined in exception-declarations"; | |
12887 | ||
12888 | /* Parse the type-specifier-seq. */ | |
12889 | type_specifiers = cp_parser_type_specifier_seq (parser); | |
12890 | /* If it's a `)', then there is no declarator. */ | |
12891 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)) | |
12892 | declarator = NULL_TREE; | |
12893 | else | |
62b8a44e | 12894 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER, |
4bb8ca28 MM |
12895 | /*ctor_dtor_or_conv_p=*/NULL, |
12896 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
12897 | |
12898 | /* Restore the saved message. */ | |
12899 | parser->type_definition_forbidden_message = saved_message; | |
12900 | ||
12901 | return start_handler_parms (type_specifiers, declarator); | |
12902 | } | |
12903 | ||
12904 | /* Parse a throw-expression. | |
12905 | ||
12906 | throw-expression: | |
34cd5ae7 | 12907 | throw assignment-expression [opt] |
a723baf1 MM |
12908 | |
12909 | Returns a THROW_EXPR representing the throw-expression. */ | |
12910 | ||
12911 | static tree | |
94edc4ab | 12912 | cp_parser_throw_expression (cp_parser* parser) |
a723baf1 MM |
12913 | { |
12914 | tree expression; | |
89f1a6ec | 12915 | cp_token* token; |
a723baf1 MM |
12916 | |
12917 | cp_parser_require_keyword (parser, RID_THROW, "`throw'"); | |
89f1a6ec MM |
12918 | token = cp_lexer_peek_token (parser->lexer); |
12919 | /* Figure out whether or not there is an assignment-expression | |
12920 | following the "throw" keyword. */ | |
12921 | if (token->type == CPP_COMMA | |
12922 | || token->type == CPP_SEMICOLON | |
12923 | || token->type == CPP_CLOSE_PAREN | |
12924 | || token->type == CPP_CLOSE_SQUARE | |
12925 | || token->type == CPP_CLOSE_BRACE | |
12926 | || token->type == CPP_COLON) | |
a723baf1 | 12927 | expression = NULL_TREE; |
89f1a6ec MM |
12928 | else |
12929 | expression = cp_parser_assignment_expression (parser); | |
a723baf1 MM |
12930 | |
12931 | return build_throw (expression); | |
12932 | } | |
12933 | ||
12934 | /* GNU Extensions */ | |
12935 | ||
12936 | /* Parse an (optional) asm-specification. | |
12937 | ||
12938 | asm-specification: | |
12939 | asm ( string-literal ) | |
12940 | ||
12941 | If the asm-specification is present, returns a STRING_CST | |
12942 | corresponding to the string-literal. Otherwise, returns | |
12943 | NULL_TREE. */ | |
12944 | ||
12945 | static tree | |
94edc4ab | 12946 | cp_parser_asm_specification_opt (cp_parser* parser) |
a723baf1 MM |
12947 | { |
12948 | cp_token *token; | |
12949 | tree asm_specification; | |
12950 | ||
12951 | /* Peek at the next token. */ | |
12952 | token = cp_lexer_peek_token (parser->lexer); | |
12953 | /* If the next token isn't the `asm' keyword, then there's no | |
12954 | asm-specification. */ | |
12955 | if (!cp_parser_is_keyword (token, RID_ASM)) | |
12956 | return NULL_TREE; | |
12957 | ||
12958 | /* Consume the `asm' token. */ | |
12959 | cp_lexer_consume_token (parser->lexer); | |
12960 | /* Look for the `('. */ | |
12961 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
12962 | ||
12963 | /* Look for the string-literal. */ | |
12964 | token = cp_parser_require (parser, CPP_STRING, "string-literal"); | |
12965 | if (token) | |
12966 | asm_specification = token->value; | |
12967 | else | |
12968 | asm_specification = NULL_TREE; | |
12969 | ||
12970 | /* Look for the `)'. */ | |
12971 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`('"); | |
12972 | ||
12973 | return asm_specification; | |
12974 | } | |
12975 | ||
12976 | /* Parse an asm-operand-list. | |
12977 | ||
12978 | asm-operand-list: | |
12979 | asm-operand | |
12980 | asm-operand-list , asm-operand | |
12981 | ||
12982 | asm-operand: | |
12983 | string-literal ( expression ) | |
12984 | [ string-literal ] string-literal ( expression ) | |
12985 | ||
12986 | Returns a TREE_LIST representing the operands. The TREE_VALUE of | |
12987 | each node is the expression. The TREE_PURPOSE is itself a | |
12988 | TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed | |
12989 | string-literal (or NULL_TREE if not present) and whose TREE_VALUE | |
12990 | is a STRING_CST for the string literal before the parenthesis. */ | |
12991 | ||
12992 | static tree | |
94edc4ab | 12993 | cp_parser_asm_operand_list (cp_parser* parser) |
a723baf1 MM |
12994 | { |
12995 | tree asm_operands = NULL_TREE; | |
12996 | ||
12997 | while (true) | |
12998 | { | |
12999 | tree string_literal; | |
13000 | tree expression; | |
13001 | tree name; | |
13002 | cp_token *token; | |
13003 | ||
13004 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
13005 | { | |
13006 | /* Consume the `[' token. */ | |
13007 | cp_lexer_consume_token (parser->lexer); | |
13008 | /* Read the operand name. */ | |
13009 | name = cp_parser_identifier (parser); | |
13010 | if (name != error_mark_node) | |
13011 | name = build_string (IDENTIFIER_LENGTH (name), | |
13012 | IDENTIFIER_POINTER (name)); | |
13013 | /* Look for the closing `]'. */ | |
13014 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
13015 | } | |
13016 | else | |
13017 | name = NULL_TREE; | |
13018 | /* Look for the string-literal. */ | |
13019 | token = cp_parser_require (parser, CPP_STRING, "string-literal"); | |
13020 | string_literal = token ? token->value : error_mark_node; | |
13021 | /* Look for the `('. */ | |
13022 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13023 | /* Parse the expression. */ | |
13024 | expression = cp_parser_expression (parser); | |
13025 | /* Look for the `)'. */ | |
13026 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
13027 | /* Add this operand to the list. */ | |
13028 | asm_operands = tree_cons (build_tree_list (name, string_literal), | |
13029 | expression, | |
13030 | asm_operands); | |
13031 | /* If the next token is not a `,', there are no more | |
13032 | operands. */ | |
13033 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
13034 | break; | |
13035 | /* Consume the `,'. */ | |
13036 | cp_lexer_consume_token (parser->lexer); | |
13037 | } | |
13038 | ||
13039 | return nreverse (asm_operands); | |
13040 | } | |
13041 | ||
13042 | /* Parse an asm-clobber-list. | |
13043 | ||
13044 | asm-clobber-list: | |
13045 | string-literal | |
13046 | asm-clobber-list , string-literal | |
13047 | ||
13048 | Returns a TREE_LIST, indicating the clobbers in the order that they | |
13049 | appeared. The TREE_VALUE of each node is a STRING_CST. */ | |
13050 | ||
13051 | static tree | |
94edc4ab | 13052 | cp_parser_asm_clobber_list (cp_parser* parser) |
a723baf1 MM |
13053 | { |
13054 | tree clobbers = NULL_TREE; | |
13055 | ||
13056 | while (true) | |
13057 | { | |
13058 | cp_token *token; | |
13059 | tree string_literal; | |
13060 | ||
13061 | /* Look for the string literal. */ | |
13062 | token = cp_parser_require (parser, CPP_STRING, "string-literal"); | |
13063 | string_literal = token ? token->value : error_mark_node; | |
13064 | /* Add it to the list. */ | |
13065 | clobbers = tree_cons (NULL_TREE, string_literal, clobbers); | |
13066 | /* If the next token is not a `,', then the list is | |
13067 | complete. */ | |
13068 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
13069 | break; | |
13070 | /* Consume the `,' token. */ | |
13071 | cp_lexer_consume_token (parser->lexer); | |
13072 | } | |
13073 | ||
13074 | return clobbers; | |
13075 | } | |
13076 | ||
13077 | /* Parse an (optional) series of attributes. | |
13078 | ||
13079 | attributes: | |
13080 | attributes attribute | |
13081 | ||
13082 | attribute: | |
13083 | __attribute__ (( attribute-list [opt] )) | |
13084 | ||
13085 | The return value is as for cp_parser_attribute_list. */ | |
13086 | ||
13087 | static tree | |
94edc4ab | 13088 | cp_parser_attributes_opt (cp_parser* parser) |
a723baf1 MM |
13089 | { |
13090 | tree attributes = NULL_TREE; | |
13091 | ||
13092 | while (true) | |
13093 | { | |
13094 | cp_token *token; | |
13095 | tree attribute_list; | |
13096 | ||
13097 | /* Peek at the next token. */ | |
13098 | token = cp_lexer_peek_token (parser->lexer); | |
13099 | /* If it's not `__attribute__', then we're done. */ | |
13100 | if (token->keyword != RID_ATTRIBUTE) | |
13101 | break; | |
13102 | ||
13103 | /* Consume the `__attribute__' keyword. */ | |
13104 | cp_lexer_consume_token (parser->lexer); | |
13105 | /* Look for the two `(' tokens. */ | |
13106 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13107 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13108 | ||
13109 | /* Peek at the next token. */ | |
13110 | token = cp_lexer_peek_token (parser->lexer); | |
13111 | if (token->type != CPP_CLOSE_PAREN) | |
13112 | /* Parse the attribute-list. */ | |
13113 | attribute_list = cp_parser_attribute_list (parser); | |
13114 | else | |
13115 | /* If the next token is a `)', then there is no attribute | |
13116 | list. */ | |
13117 | attribute_list = NULL; | |
13118 | ||
13119 | /* Look for the two `)' tokens. */ | |
13120 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
13121 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
13122 | ||
13123 | /* Add these new attributes to the list. */ | |
13124 | attributes = chainon (attributes, attribute_list); | |
13125 | } | |
13126 | ||
13127 | return attributes; | |
13128 | } | |
13129 | ||
13130 | /* Parse an attribute-list. | |
13131 | ||
13132 | attribute-list: | |
13133 | attribute | |
13134 | attribute-list , attribute | |
13135 | ||
13136 | attribute: | |
13137 | identifier | |
13138 | identifier ( identifier ) | |
13139 | identifier ( identifier , expression-list ) | |
13140 | identifier ( expression-list ) | |
13141 | ||
13142 | Returns a TREE_LIST. Each node corresponds to an attribute. THe | |
13143 | TREE_PURPOSE of each node is the identifier indicating which | |
13144 | attribute is in use. The TREE_VALUE represents the arguments, if | |
13145 | any. */ | |
13146 | ||
13147 | static tree | |
94edc4ab | 13148 | cp_parser_attribute_list (cp_parser* parser) |
a723baf1 MM |
13149 | { |
13150 | tree attribute_list = NULL_TREE; | |
13151 | ||
13152 | while (true) | |
13153 | { | |
13154 | cp_token *token; | |
13155 | tree identifier; | |
13156 | tree attribute; | |
13157 | ||
13158 | /* Look for the identifier. We also allow keywords here; for | |
13159 | example `__attribute__ ((const))' is legal. */ | |
13160 | token = cp_lexer_peek_token (parser->lexer); | |
13161 | if (token->type != CPP_NAME | |
13162 | && token->type != CPP_KEYWORD) | |
13163 | return error_mark_node; | |
13164 | /* Consume the token. */ | |
13165 | token = cp_lexer_consume_token (parser->lexer); | |
13166 | ||
13167 | /* Save away the identifier that indicates which attribute this is. */ | |
13168 | identifier = token->value; | |
13169 | attribute = build_tree_list (identifier, NULL_TREE); | |
13170 | ||
13171 | /* Peek at the next token. */ | |
13172 | token = cp_lexer_peek_token (parser->lexer); | |
13173 | /* If it's an `(', then parse the attribute arguments. */ | |
13174 | if (token->type == CPP_OPEN_PAREN) | |
13175 | { | |
13176 | tree arguments; | |
a723baf1 | 13177 | |
39703eb9 MM |
13178 | arguments = (cp_parser_parenthesized_expression_list |
13179 | (parser, true, /*non_constant_p=*/NULL)); | |
a723baf1 MM |
13180 | /* Save the identifier and arguments away. */ |
13181 | TREE_VALUE (attribute) = arguments; | |
a723baf1 MM |
13182 | } |
13183 | ||
13184 | /* Add this attribute to the list. */ | |
13185 | TREE_CHAIN (attribute) = attribute_list; | |
13186 | attribute_list = attribute; | |
13187 | ||
13188 | /* Now, look for more attributes. */ | |
13189 | token = cp_lexer_peek_token (parser->lexer); | |
13190 | /* If the next token isn't a `,', we're done. */ | |
13191 | if (token->type != CPP_COMMA) | |
13192 | break; | |
13193 | ||
cd0be382 | 13194 | /* Consume the comma and keep going. */ |
a723baf1 MM |
13195 | cp_lexer_consume_token (parser->lexer); |
13196 | } | |
13197 | ||
13198 | /* We built up the list in reverse order. */ | |
13199 | return nreverse (attribute_list); | |
13200 | } | |
13201 | ||
13202 | /* Parse an optional `__extension__' keyword. Returns TRUE if it is | |
13203 | present, and FALSE otherwise. *SAVED_PEDANTIC is set to the | |
13204 | current value of the PEDANTIC flag, regardless of whether or not | |
13205 | the `__extension__' keyword is present. The caller is responsible | |
13206 | for restoring the value of the PEDANTIC flag. */ | |
13207 | ||
13208 | static bool | |
94edc4ab | 13209 | cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic) |
a723baf1 MM |
13210 | { |
13211 | /* Save the old value of the PEDANTIC flag. */ | |
13212 | *saved_pedantic = pedantic; | |
13213 | ||
13214 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION)) | |
13215 | { | |
13216 | /* Consume the `__extension__' token. */ | |
13217 | cp_lexer_consume_token (parser->lexer); | |
13218 | /* We're not being pedantic while the `__extension__' keyword is | |
13219 | in effect. */ | |
13220 | pedantic = 0; | |
13221 | ||
13222 | return true; | |
13223 | } | |
13224 | ||
13225 | return false; | |
13226 | } | |
13227 | ||
13228 | /* Parse a label declaration. | |
13229 | ||
13230 | label-declaration: | |
13231 | __label__ label-declarator-seq ; | |
13232 | ||
13233 | label-declarator-seq: | |
13234 | identifier , label-declarator-seq | |
13235 | identifier */ | |
13236 | ||
13237 | static void | |
94edc4ab | 13238 | cp_parser_label_declaration (cp_parser* parser) |
a723baf1 MM |
13239 | { |
13240 | /* Look for the `__label__' keyword. */ | |
13241 | cp_parser_require_keyword (parser, RID_LABEL, "`__label__'"); | |
13242 | ||
13243 | while (true) | |
13244 | { | |
13245 | tree identifier; | |
13246 | ||
13247 | /* Look for an identifier. */ | |
13248 | identifier = cp_parser_identifier (parser); | |
13249 | /* Declare it as a lobel. */ | |
13250 | finish_label_decl (identifier); | |
13251 | /* If the next token is a `;', stop. */ | |
13252 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
13253 | break; | |
13254 | /* Look for the `,' separating the label declarations. */ | |
13255 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
13256 | } | |
13257 | ||
13258 | /* Look for the final `;'. */ | |
13259 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
13260 | } | |
13261 | ||
13262 | /* Support Functions */ | |
13263 | ||
13264 | /* Looks up NAME in the current scope, as given by PARSER->SCOPE. | |
13265 | NAME should have one of the representations used for an | |
13266 | id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE | |
13267 | is returned. If PARSER->SCOPE is a dependent type, then a | |
13268 | SCOPE_REF is returned. | |
13269 | ||
13270 | If NAME is a TEMPLATE_ID_EXPR, then it will be immediately | |
13271 | returned; the name was already resolved when the TEMPLATE_ID_EXPR | |
13272 | was formed. Abstractly, such entities should not be passed to this | |
13273 | function, because they do not need to be looked up, but it is | |
13274 | simpler to check for this special case here, rather than at the | |
13275 | call-sites. | |
13276 | ||
13277 | In cases not explicitly covered above, this function returns a | |
13278 | DECL, OVERLOAD, or baselink representing the result of the lookup. | |
13279 | If there was no entity with the indicated NAME, the ERROR_MARK_NODE | |
13280 | is returned. | |
13281 | ||
a723baf1 MM |
13282 | If IS_TYPE is TRUE, bindings that do not refer to types are |
13283 | ignored. | |
13284 | ||
b0bc6e8e KL |
13285 | If IS_TEMPLATE is TRUE, bindings that do not refer to templates are |
13286 | ignored. | |
13287 | ||
eea9800f MM |
13288 | If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces |
13289 | are ignored. | |
13290 | ||
a723baf1 MM |
13291 | If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent |
13292 | types. */ | |
13293 | ||
13294 | static tree | |
8d241e0b | 13295 | cp_parser_lookup_name (cp_parser *parser, tree name, |
b0bc6e8e KL |
13296 | bool is_type, bool is_template, bool is_namespace, |
13297 | bool check_dependency) | |
a723baf1 MM |
13298 | { |
13299 | tree decl; | |
13300 | tree object_type = parser->context->object_type; | |
13301 | ||
13302 | /* Now that we have looked up the name, the OBJECT_TYPE (if any) is | |
13303 | no longer valid. Note that if we are parsing tentatively, and | |
13304 | the parse fails, OBJECT_TYPE will be automatically restored. */ | |
13305 | parser->context->object_type = NULL_TREE; | |
13306 | ||
13307 | if (name == error_mark_node) | |
13308 | return error_mark_node; | |
13309 | ||
13310 | /* A template-id has already been resolved; there is no lookup to | |
13311 | do. */ | |
13312 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
13313 | return name; | |
13314 | if (BASELINK_P (name)) | |
13315 | { | |
13316 | my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name)) | |
13317 | == TEMPLATE_ID_EXPR), | |
13318 | 20020909); | |
13319 | return name; | |
13320 | } | |
13321 | ||
13322 | /* A BIT_NOT_EXPR is used to represent a destructor. By this point, | |
13323 | it should already have been checked to make sure that the name | |
13324 | used matches the type being destroyed. */ | |
13325 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
13326 | { | |
13327 | tree type; | |
13328 | ||
13329 | /* Figure out to which type this destructor applies. */ | |
13330 | if (parser->scope) | |
13331 | type = parser->scope; | |
13332 | else if (object_type) | |
13333 | type = object_type; | |
13334 | else | |
13335 | type = current_class_type; | |
13336 | /* If that's not a class type, there is no destructor. */ | |
13337 | if (!type || !CLASS_TYPE_P (type)) | |
13338 | return error_mark_node; | |
13339 | /* If it was a class type, return the destructor. */ | |
13340 | return CLASSTYPE_DESTRUCTORS (type); | |
13341 | } | |
13342 | ||
13343 | /* By this point, the NAME should be an ordinary identifier. If | |
13344 | the id-expression was a qualified name, the qualifying scope is | |
13345 | stored in PARSER->SCOPE at this point. */ | |
13346 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, | |
13347 | 20000619); | |
13348 | ||
13349 | /* Perform the lookup. */ | |
13350 | if (parser->scope) | |
13351 | { | |
1fb3244a | 13352 | bool dependent_p; |
a723baf1 MM |
13353 | |
13354 | if (parser->scope == error_mark_node) | |
13355 | return error_mark_node; | |
13356 | ||
13357 | /* If the SCOPE is dependent, the lookup must be deferred until | |
13358 | the template is instantiated -- unless we are explicitly | |
13359 | looking up names in uninstantiated templates. Even then, we | |
13360 | cannot look up the name if the scope is not a class type; it | |
13361 | might, for example, be a template type parameter. */ | |
1fb3244a MM |
13362 | dependent_p = (TYPE_P (parser->scope) |
13363 | && !(parser->in_declarator_p | |
13364 | && currently_open_class (parser->scope)) | |
13365 | && dependent_type_p (parser->scope)); | |
a723baf1 | 13366 | if ((check_dependency || !CLASS_TYPE_P (parser->scope)) |
1fb3244a | 13367 | && dependent_p) |
a723baf1 | 13368 | { |
b0bc6e8e | 13369 | if (is_type) |
a723baf1 MM |
13370 | /* The resolution to Core Issue 180 says that `struct A::B' |
13371 | should be considered a type-name, even if `A' is | |
13372 | dependent. */ | |
13373 | decl = TYPE_NAME (make_typename_type (parser->scope, | |
13374 | name, | |
13375 | /*complain=*/1)); | |
b0bc6e8e | 13376 | else if (is_template) |
5b4acce1 KL |
13377 | decl = make_unbound_class_template (parser->scope, |
13378 | name, | |
13379 | /*complain=*/1); | |
b0bc6e8e KL |
13380 | else |
13381 | decl = build_nt (SCOPE_REF, parser->scope, name); | |
a723baf1 MM |
13382 | } |
13383 | else | |
13384 | { | |
13385 | /* If PARSER->SCOPE is a dependent type, then it must be a | |
13386 | class type, and we must not be checking dependencies; | |
13387 | otherwise, we would have processed this lookup above. So | |
13388 | that PARSER->SCOPE is not considered a dependent base by | |
13389 | lookup_member, we must enter the scope here. */ | |
1fb3244a | 13390 | if (dependent_p) |
a723baf1 MM |
13391 | push_scope (parser->scope); |
13392 | /* If the PARSER->SCOPE is a a template specialization, it | |
13393 | may be instantiated during name lookup. In that case, | |
13394 | errors may be issued. Even if we rollback the current | |
13395 | tentative parse, those errors are valid. */ | |
5e08432e MM |
13396 | decl = lookup_qualified_name (parser->scope, name, is_type, |
13397 | /*complain=*/true); | |
1fb3244a | 13398 | if (dependent_p) |
a723baf1 MM |
13399 | pop_scope (parser->scope); |
13400 | } | |
13401 | parser->qualifying_scope = parser->scope; | |
13402 | parser->object_scope = NULL_TREE; | |
13403 | } | |
13404 | else if (object_type) | |
13405 | { | |
13406 | tree object_decl = NULL_TREE; | |
13407 | /* Look up the name in the scope of the OBJECT_TYPE, unless the | |
13408 | OBJECT_TYPE is not a class. */ | |
13409 | if (CLASS_TYPE_P (object_type)) | |
13410 | /* If the OBJECT_TYPE is a template specialization, it may | |
13411 | be instantiated during name lookup. In that case, errors | |
13412 | may be issued. Even if we rollback the current tentative | |
13413 | parse, those errors are valid. */ | |
13414 | object_decl = lookup_member (object_type, | |
13415 | name, | |
13416 | /*protect=*/0, is_type); | |
13417 | /* Look it up in the enclosing context, too. */ | |
13418 | decl = lookup_name_real (name, is_type, /*nonclass=*/0, | |
eea9800f | 13419 | is_namespace, |
a723baf1 MM |
13420 | /*flags=*/0); |
13421 | parser->object_scope = object_type; | |
13422 | parser->qualifying_scope = NULL_TREE; | |
13423 | if (object_decl) | |
13424 | decl = object_decl; | |
13425 | } | |
13426 | else | |
13427 | { | |
13428 | decl = lookup_name_real (name, is_type, /*nonclass=*/0, | |
eea9800f | 13429 | is_namespace, |
a723baf1 MM |
13430 | /*flags=*/0); |
13431 | parser->qualifying_scope = NULL_TREE; | |
13432 | parser->object_scope = NULL_TREE; | |
13433 | } | |
13434 | ||
13435 | /* If the lookup failed, let our caller know. */ | |
13436 | if (!decl | |
13437 | || decl == error_mark_node | |
13438 | || (TREE_CODE (decl) == FUNCTION_DECL | |
13439 | && DECL_ANTICIPATED (decl))) | |
13440 | return error_mark_node; | |
13441 | ||
13442 | /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ | |
13443 | if (TREE_CODE (decl) == TREE_LIST) | |
13444 | { | |
13445 | /* The error message we have to print is too complicated for | |
13446 | cp_parser_error, so we incorporate its actions directly. */ | |
e5976695 | 13447 | if (!cp_parser_simulate_error (parser)) |
a723baf1 MM |
13448 | { |
13449 | error ("reference to `%D' is ambiguous", name); | |
13450 | print_candidates (decl); | |
13451 | } | |
13452 | return error_mark_node; | |
13453 | } | |
13454 | ||
13455 | my_friendly_assert (DECL_P (decl) | |
13456 | || TREE_CODE (decl) == OVERLOAD | |
13457 | || TREE_CODE (decl) == SCOPE_REF | |
5b4acce1 | 13458 | || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE |
a723baf1 MM |
13459 | || BASELINK_P (decl), |
13460 | 20000619); | |
13461 | ||
13462 | /* If we have resolved the name of a member declaration, check to | |
13463 | see if the declaration is accessible. When the name resolves to | |
34cd5ae7 | 13464 | set of overloaded functions, accessibility is checked when |
a723baf1 MM |
13465 | overload resolution is done. |
13466 | ||
13467 | During an explicit instantiation, access is not checked at all, | |
13468 | as per [temp.explicit]. */ | |
8d241e0b | 13469 | if (DECL_P (decl)) |
ee76b931 | 13470 | check_accessibility_of_qualified_id (decl, object_type, parser->scope); |
a723baf1 MM |
13471 | |
13472 | return decl; | |
13473 | } | |
13474 | ||
13475 | /* Like cp_parser_lookup_name, but for use in the typical case where | |
b0bc6e8e KL |
13476 | CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE, |
13477 | IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */ | |
a723baf1 MM |
13478 | |
13479 | static tree | |
94edc4ab | 13480 | cp_parser_lookup_name_simple (cp_parser* parser, tree name) |
a723baf1 MM |
13481 | { |
13482 | return cp_parser_lookup_name (parser, name, | |
eea9800f | 13483 | /*is_type=*/false, |
b0bc6e8e | 13484 | /*is_template=*/false, |
eea9800f | 13485 | /*is_namespace=*/false, |
a723baf1 MM |
13486 | /*check_dependency=*/true); |
13487 | } | |
13488 | ||
a723baf1 MM |
13489 | /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in |
13490 | the current context, return the TYPE_DECL. If TAG_NAME_P is | |
13491 | true, the DECL indicates the class being defined in a class-head, | |
13492 | or declared in an elaborated-type-specifier. | |
13493 | ||
13494 | Otherwise, return DECL. */ | |
13495 | ||
13496 | static tree | |
13497 | cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p) | |
13498 | { | |
710b73e6 KL |
13499 | /* If the TEMPLATE_DECL is being declared as part of a class-head, |
13500 | the translation from TEMPLATE_DECL to TYPE_DECL occurs: | |
a723baf1 MM |
13501 | |
13502 | struct A { | |
13503 | template <typename T> struct B; | |
13504 | }; | |
13505 | ||
13506 | template <typename T> struct A::B {}; | |
13507 | ||
13508 | Similarly, in a elaborated-type-specifier: | |
13509 | ||
13510 | namespace N { struct X{}; } | |
13511 | ||
13512 | struct A { | |
13513 | template <typename T> friend struct N::X; | |
13514 | }; | |
13515 | ||
710b73e6 KL |
13516 | However, if the DECL refers to a class type, and we are in |
13517 | the scope of the class, then the name lookup automatically | |
13518 | finds the TYPE_DECL created by build_self_reference rather | |
13519 | than a TEMPLATE_DECL. For example, in: | |
13520 | ||
13521 | template <class T> struct S { | |
13522 | S s; | |
13523 | }; | |
13524 | ||
13525 | there is no need to handle such case. */ | |
13526 | ||
13527 | if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p) | |
a723baf1 MM |
13528 | return DECL_TEMPLATE_RESULT (decl); |
13529 | ||
13530 | return decl; | |
13531 | } | |
13532 | ||
13533 | /* If too many, or too few, template-parameter lists apply to the | |
13534 | declarator, issue an error message. Returns TRUE if all went well, | |
13535 | and FALSE otherwise. */ | |
13536 | ||
13537 | static bool | |
94edc4ab NN |
13538 | cp_parser_check_declarator_template_parameters (cp_parser* parser, |
13539 | tree declarator) | |
a723baf1 MM |
13540 | { |
13541 | unsigned num_templates; | |
13542 | ||
13543 | /* We haven't seen any classes that involve template parameters yet. */ | |
13544 | num_templates = 0; | |
13545 | ||
13546 | switch (TREE_CODE (declarator)) | |
13547 | { | |
13548 | case CALL_EXPR: | |
13549 | case ARRAY_REF: | |
13550 | case INDIRECT_REF: | |
13551 | case ADDR_EXPR: | |
13552 | { | |
13553 | tree main_declarator = TREE_OPERAND (declarator, 0); | |
13554 | return | |
13555 | cp_parser_check_declarator_template_parameters (parser, | |
13556 | main_declarator); | |
13557 | } | |
13558 | ||
13559 | case SCOPE_REF: | |
13560 | { | |
13561 | tree scope; | |
13562 | tree member; | |
13563 | ||
13564 | scope = TREE_OPERAND (declarator, 0); | |
13565 | member = TREE_OPERAND (declarator, 1); | |
13566 | ||
13567 | /* If this is a pointer-to-member, then we are not interested | |
13568 | in the SCOPE, because it does not qualify the thing that is | |
13569 | being declared. */ | |
13570 | if (TREE_CODE (member) == INDIRECT_REF) | |
13571 | return (cp_parser_check_declarator_template_parameters | |
13572 | (parser, member)); | |
13573 | ||
13574 | while (scope && CLASS_TYPE_P (scope)) | |
13575 | { | |
13576 | /* You're supposed to have one `template <...>' | |
13577 | for every template class, but you don't need one | |
13578 | for a full specialization. For example: | |
13579 | ||
13580 | template <class T> struct S{}; | |
13581 | template <> struct S<int> { void f(); }; | |
13582 | void S<int>::f () {} | |
13583 | ||
13584 | is correct; there shouldn't be a `template <>' for | |
13585 | the definition of `S<int>::f'. */ | |
13586 | if (CLASSTYPE_TEMPLATE_INFO (scope) | |
13587 | && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope) | |
13588 | || uses_template_parms (CLASSTYPE_TI_ARGS (scope))) | |
13589 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))) | |
13590 | ++num_templates; | |
13591 | ||
13592 | scope = TYPE_CONTEXT (scope); | |
13593 | } | |
13594 | } | |
13595 | ||
13596 | /* Fall through. */ | |
13597 | ||
13598 | default: | |
13599 | /* If the DECLARATOR has the form `X<y>' then it uses one | |
13600 | additional level of template parameters. */ | |
13601 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
13602 | ++num_templates; | |
13603 | ||
13604 | return cp_parser_check_template_parameters (parser, | |
13605 | num_templates); | |
13606 | } | |
13607 | } | |
13608 | ||
13609 | /* NUM_TEMPLATES were used in the current declaration. If that is | |
13610 | invalid, return FALSE and issue an error messages. Otherwise, | |
13611 | return TRUE. */ | |
13612 | ||
13613 | static bool | |
94edc4ab NN |
13614 | cp_parser_check_template_parameters (cp_parser* parser, |
13615 | unsigned num_templates) | |
a723baf1 MM |
13616 | { |
13617 | /* If there are more template classes than parameter lists, we have | |
13618 | something like: | |
13619 | ||
13620 | template <class T> void S<T>::R<T>::f (); */ | |
13621 | if (parser->num_template_parameter_lists < num_templates) | |
13622 | { | |
13623 | error ("too few template-parameter-lists"); | |
13624 | return false; | |
13625 | } | |
13626 | /* If there are the same number of template classes and parameter | |
13627 | lists, that's OK. */ | |
13628 | if (parser->num_template_parameter_lists == num_templates) | |
13629 | return true; | |
13630 | /* If there are more, but only one more, then we are referring to a | |
13631 | member template. That's OK too. */ | |
13632 | if (parser->num_template_parameter_lists == num_templates + 1) | |
13633 | return true; | |
13634 | /* Otherwise, there are too many template parameter lists. We have | |
13635 | something like: | |
13636 | ||
13637 | template <class T> template <class U> void S::f(); */ | |
13638 | error ("too many template-parameter-lists"); | |
13639 | return false; | |
13640 | } | |
13641 | ||
13642 | /* Parse a binary-expression of the general form: | |
13643 | ||
13644 | binary-expression: | |
13645 | <expr> | |
13646 | binary-expression <token> <expr> | |
13647 | ||
13648 | The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used | |
13649 | to parser the <expr>s. If the first production is used, then the | |
13650 | value returned by FN is returned directly. Otherwise, a node with | |
13651 | the indicated EXPR_TYPE is returned, with operands corresponding to | |
13652 | the two sub-expressions. */ | |
13653 | ||
13654 | static tree | |
94edc4ab NN |
13655 | cp_parser_binary_expression (cp_parser* parser, |
13656 | const cp_parser_token_tree_map token_tree_map, | |
13657 | cp_parser_expression_fn fn) | |
a723baf1 MM |
13658 | { |
13659 | tree lhs; | |
13660 | ||
13661 | /* Parse the first expression. */ | |
13662 | lhs = (*fn) (parser); | |
13663 | /* Now, look for more expressions. */ | |
13664 | while (true) | |
13665 | { | |
13666 | cp_token *token; | |
39b1af70 | 13667 | const cp_parser_token_tree_map_node *map_node; |
a723baf1 MM |
13668 | tree rhs; |
13669 | ||
13670 | /* Peek at the next token. */ | |
13671 | token = cp_lexer_peek_token (parser->lexer); | |
13672 | /* If the token is `>', and that's not an operator at the | |
13673 | moment, then we're done. */ | |
13674 | if (token->type == CPP_GREATER | |
13675 | && !parser->greater_than_is_operator_p) | |
13676 | break; | |
34cd5ae7 | 13677 | /* If we find one of the tokens we want, build the corresponding |
a723baf1 MM |
13678 | tree representation. */ |
13679 | for (map_node = token_tree_map; | |
13680 | map_node->token_type != CPP_EOF; | |
13681 | ++map_node) | |
13682 | if (map_node->token_type == token->type) | |
13683 | { | |
13684 | /* Consume the operator token. */ | |
13685 | cp_lexer_consume_token (parser->lexer); | |
13686 | /* Parse the right-hand side of the expression. */ | |
13687 | rhs = (*fn) (parser); | |
13688 | /* Build the binary tree node. */ | |
13689 | lhs = build_x_binary_op (map_node->tree_type, lhs, rhs); | |
13690 | break; | |
13691 | } | |
13692 | ||
13693 | /* If the token wasn't one of the ones we want, we're done. */ | |
13694 | if (map_node->token_type == CPP_EOF) | |
13695 | break; | |
13696 | } | |
13697 | ||
13698 | return lhs; | |
13699 | } | |
13700 | ||
13701 | /* Parse an optional `::' token indicating that the following name is | |
13702 | from the global namespace. If so, PARSER->SCOPE is set to the | |
13703 | GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE, | |
13704 | unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone. | |
13705 | Returns the new value of PARSER->SCOPE, if the `::' token is | |
13706 | present, and NULL_TREE otherwise. */ | |
13707 | ||
13708 | static tree | |
94edc4ab | 13709 | cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p) |
a723baf1 MM |
13710 | { |
13711 | cp_token *token; | |
13712 | ||
13713 | /* Peek at the next token. */ | |
13714 | token = cp_lexer_peek_token (parser->lexer); | |
13715 | /* If we're looking at a `::' token then we're starting from the | |
13716 | global namespace, not our current location. */ | |
13717 | if (token->type == CPP_SCOPE) | |
13718 | { | |
13719 | /* Consume the `::' token. */ | |
13720 | cp_lexer_consume_token (parser->lexer); | |
13721 | /* Set the SCOPE so that we know where to start the lookup. */ | |
13722 | parser->scope = global_namespace; | |
13723 | parser->qualifying_scope = global_namespace; | |
13724 | parser->object_scope = NULL_TREE; | |
13725 | ||
13726 | return parser->scope; | |
13727 | } | |
13728 | else if (!current_scope_valid_p) | |
13729 | { | |
13730 | parser->scope = NULL_TREE; | |
13731 | parser->qualifying_scope = NULL_TREE; | |
13732 | parser->object_scope = NULL_TREE; | |
13733 | } | |
13734 | ||
13735 | return NULL_TREE; | |
13736 | } | |
13737 | ||
13738 | /* Returns TRUE if the upcoming token sequence is the start of a | |
13739 | constructor declarator. If FRIEND_P is true, the declarator is | |
13740 | preceded by the `friend' specifier. */ | |
13741 | ||
13742 | static bool | |
13743 | cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p) | |
13744 | { | |
13745 | bool constructor_p; | |
13746 | tree type_decl = NULL_TREE; | |
13747 | bool nested_name_p; | |
2050a1bb MM |
13748 | cp_token *next_token; |
13749 | ||
13750 | /* The common case is that this is not a constructor declarator, so | |
8fbc5ae7 MM |
13751 | try to avoid doing lots of work if at all possible. It's not |
13752 | valid declare a constructor at function scope. */ | |
13753 | if (at_function_scope_p ()) | |
13754 | return false; | |
13755 | /* And only certain tokens can begin a constructor declarator. */ | |
2050a1bb MM |
13756 | next_token = cp_lexer_peek_token (parser->lexer); |
13757 | if (next_token->type != CPP_NAME | |
13758 | && next_token->type != CPP_SCOPE | |
13759 | && next_token->type != CPP_NESTED_NAME_SPECIFIER | |
13760 | && next_token->type != CPP_TEMPLATE_ID) | |
13761 | return false; | |
a723baf1 MM |
13762 | |
13763 | /* Parse tentatively; we are going to roll back all of the tokens | |
13764 | consumed here. */ | |
13765 | cp_parser_parse_tentatively (parser); | |
13766 | /* Assume that we are looking at a constructor declarator. */ | |
13767 | constructor_p = true; | |
8d241e0b | 13768 | |
a723baf1 MM |
13769 | /* Look for the optional `::' operator. */ |
13770 | cp_parser_global_scope_opt (parser, | |
13771 | /*current_scope_valid_p=*/false); | |
13772 | /* Look for the nested-name-specifier. */ | |
13773 | nested_name_p | |
13774 | = (cp_parser_nested_name_specifier_opt (parser, | |
13775 | /*typename_keyword_p=*/false, | |
13776 | /*check_dependency_p=*/false, | |
a668c6ad MM |
13777 | /*type_p=*/false, |
13778 | /*is_declaration=*/false) | |
a723baf1 MM |
13779 | != NULL_TREE); |
13780 | /* Outside of a class-specifier, there must be a | |
13781 | nested-name-specifier. */ | |
13782 | if (!nested_name_p && | |
13783 | (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type) | |
13784 | || friend_p)) | |
13785 | constructor_p = false; | |
13786 | /* If we still think that this might be a constructor-declarator, | |
13787 | look for a class-name. */ | |
13788 | if (constructor_p) | |
13789 | { | |
13790 | /* If we have: | |
13791 | ||
8fbc5ae7 | 13792 | template <typename T> struct S { S(); }; |
a723baf1 MM |
13793 | template <typename T> S<T>::S (); |
13794 | ||
13795 | we must recognize that the nested `S' names a class. | |
13796 | Similarly, for: | |
13797 | ||
13798 | template <typename T> S<T>::S<T> (); | |
13799 | ||
13800 | we must recognize that the nested `S' names a template. */ | |
13801 | type_decl = cp_parser_class_name (parser, | |
13802 | /*typename_keyword_p=*/false, | |
13803 | /*template_keyword_p=*/false, | |
13804 | /*type_p=*/false, | |
a723baf1 | 13805 | /*check_dependency_p=*/false, |
a668c6ad MM |
13806 | /*class_head_p=*/false, |
13807 | /*is_declaration=*/false); | |
a723baf1 MM |
13808 | /* If there was no class-name, then this is not a constructor. */ |
13809 | constructor_p = !cp_parser_error_occurred (parser); | |
13810 | } | |
8d241e0b | 13811 | |
a723baf1 MM |
13812 | /* If we're still considering a constructor, we have to see a `(', |
13813 | to begin the parameter-declaration-clause, followed by either a | |
13814 | `)', an `...', or a decl-specifier. We need to check for a | |
13815 | type-specifier to avoid being fooled into thinking that: | |
13816 | ||
13817 | S::S (f) (int); | |
13818 | ||
13819 | is a constructor. (It is actually a function named `f' that | |
13820 | takes one parameter (of type `int') and returns a value of type | |
13821 | `S::S'. */ | |
13822 | if (constructor_p | |
13823 | && cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) | |
13824 | { | |
13825 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN) | |
13826 | && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS) | |
13827 | && !cp_parser_storage_class_specifier_opt (parser)) | |
13828 | { | |
5dae1114 | 13829 | tree type; |
4047b164 | 13830 | unsigned saved_num_template_parameter_lists; |
5dae1114 MM |
13831 | |
13832 | /* Names appearing in the type-specifier should be looked up | |
13833 | in the scope of the class. */ | |
13834 | if (current_class_type) | |
13835 | type = NULL_TREE; | |
a723baf1 MM |
13836 | else |
13837 | { | |
5dae1114 MM |
13838 | type = TREE_TYPE (type_decl); |
13839 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
14d22dd6 MM |
13840 | { |
13841 | type = resolve_typename_type (type, | |
13842 | /*only_current_p=*/false); | |
13843 | if (type == error_mark_node) | |
13844 | { | |
13845 | cp_parser_abort_tentative_parse (parser); | |
13846 | return false; | |
13847 | } | |
13848 | } | |
5dae1114 | 13849 | push_scope (type); |
a723baf1 | 13850 | } |
4047b164 KL |
13851 | |
13852 | /* Inside the constructor parameter list, surrounding | |
13853 | template-parameter-lists do not apply. */ | |
13854 | saved_num_template_parameter_lists | |
13855 | = parser->num_template_parameter_lists; | |
13856 | parser->num_template_parameter_lists = 0; | |
13857 | ||
5dae1114 MM |
13858 | /* Look for the type-specifier. */ |
13859 | cp_parser_type_specifier (parser, | |
13860 | CP_PARSER_FLAGS_NONE, | |
13861 | /*is_friend=*/false, | |
13862 | /*is_declarator=*/true, | |
13863 | /*declares_class_or_enum=*/NULL, | |
13864 | /*is_cv_qualifier=*/NULL); | |
4047b164 KL |
13865 | |
13866 | parser->num_template_parameter_lists | |
13867 | = saved_num_template_parameter_lists; | |
13868 | ||
5dae1114 MM |
13869 | /* Leave the scope of the class. */ |
13870 | if (type) | |
13871 | pop_scope (type); | |
13872 | ||
13873 | constructor_p = !cp_parser_error_occurred (parser); | |
a723baf1 MM |
13874 | } |
13875 | } | |
13876 | else | |
13877 | constructor_p = false; | |
13878 | /* We did not really want to consume any tokens. */ | |
13879 | cp_parser_abort_tentative_parse (parser); | |
13880 | ||
13881 | return constructor_p; | |
13882 | } | |
13883 | ||
13884 | /* Parse the definition of the function given by the DECL_SPECIFIERS, | |
cf22909c | 13885 | ATTRIBUTES, and DECLARATOR. The access checks have been deferred; |
a723baf1 MM |
13886 | they must be performed once we are in the scope of the function. |
13887 | ||
13888 | Returns the function defined. */ | |
13889 | ||
13890 | static tree | |
13891 | cp_parser_function_definition_from_specifiers_and_declarator | |
94edc4ab NN |
13892 | (cp_parser* parser, |
13893 | tree decl_specifiers, | |
13894 | tree attributes, | |
13895 | tree declarator) | |
a723baf1 MM |
13896 | { |
13897 | tree fn; | |
13898 | bool success_p; | |
13899 | ||
13900 | /* Begin the function-definition. */ | |
13901 | success_p = begin_function_definition (decl_specifiers, | |
13902 | attributes, | |
13903 | declarator); | |
13904 | ||
13905 | /* If there were names looked up in the decl-specifier-seq that we | |
13906 | did not check, check them now. We must wait until we are in the | |
13907 | scope of the function to perform the checks, since the function | |
13908 | might be a friend. */ | |
cf22909c | 13909 | perform_deferred_access_checks (); |
a723baf1 MM |
13910 | |
13911 | if (!success_p) | |
13912 | { | |
13913 | /* If begin_function_definition didn't like the definition, skip | |
13914 | the entire function. */ | |
13915 | error ("invalid function declaration"); | |
13916 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
13917 | fn = error_mark_node; | |
13918 | } | |
13919 | else | |
13920 | fn = cp_parser_function_definition_after_declarator (parser, | |
13921 | /*inline_p=*/false); | |
13922 | ||
13923 | return fn; | |
13924 | } | |
13925 | ||
13926 | /* Parse the part of a function-definition that follows the | |
13927 | declarator. INLINE_P is TRUE iff this function is an inline | |
13928 | function defined with a class-specifier. | |
13929 | ||
13930 | Returns the function defined. */ | |
13931 | ||
13932 | static tree | |
94edc4ab NN |
13933 | cp_parser_function_definition_after_declarator (cp_parser* parser, |
13934 | bool inline_p) | |
a723baf1 MM |
13935 | { |
13936 | tree fn; | |
13937 | bool ctor_initializer_p = false; | |
13938 | bool saved_in_unbraced_linkage_specification_p; | |
13939 | unsigned saved_num_template_parameter_lists; | |
13940 | ||
13941 | /* If the next token is `return', then the code may be trying to | |
13942 | make use of the "named return value" extension that G++ used to | |
13943 | support. */ | |
13944 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN)) | |
13945 | { | |
13946 | /* Consume the `return' keyword. */ | |
13947 | cp_lexer_consume_token (parser->lexer); | |
13948 | /* Look for the identifier that indicates what value is to be | |
13949 | returned. */ | |
13950 | cp_parser_identifier (parser); | |
13951 | /* Issue an error message. */ | |
13952 | error ("named return values are no longer supported"); | |
13953 | /* Skip tokens until we reach the start of the function body. */ | |
21eb631b MM |
13954 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE) |
13955 | && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF)) | |
a723baf1 MM |
13956 | cp_lexer_consume_token (parser->lexer); |
13957 | } | |
13958 | /* The `extern' in `extern "C" void f () { ... }' does not apply to | |
13959 | anything declared inside `f'. */ | |
13960 | saved_in_unbraced_linkage_specification_p | |
13961 | = parser->in_unbraced_linkage_specification_p; | |
13962 | parser->in_unbraced_linkage_specification_p = false; | |
13963 | /* Inside the function, surrounding template-parameter-lists do not | |
13964 | apply. */ | |
13965 | saved_num_template_parameter_lists | |
13966 | = parser->num_template_parameter_lists; | |
13967 | parser->num_template_parameter_lists = 0; | |
13968 | /* If the next token is `try', then we are looking at a | |
13969 | function-try-block. */ | |
13970 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY)) | |
13971 | ctor_initializer_p = cp_parser_function_try_block (parser); | |
13972 | /* A function-try-block includes the function-body, so we only do | |
13973 | this next part if we're not processing a function-try-block. */ | |
13974 | else | |
13975 | ctor_initializer_p | |
13976 | = cp_parser_ctor_initializer_opt_and_function_body (parser); | |
13977 | ||
13978 | /* Finish the function. */ | |
13979 | fn = finish_function ((ctor_initializer_p ? 1 : 0) | | |
13980 | (inline_p ? 2 : 0)); | |
13981 | /* Generate code for it, if necessary. */ | |
8cd2462c | 13982 | expand_or_defer_fn (fn); |
a723baf1 MM |
13983 | /* Restore the saved values. */ |
13984 | parser->in_unbraced_linkage_specification_p | |
13985 | = saved_in_unbraced_linkage_specification_p; | |
13986 | parser->num_template_parameter_lists | |
13987 | = saved_num_template_parameter_lists; | |
13988 | ||
13989 | return fn; | |
13990 | } | |
13991 | ||
13992 | /* Parse a template-declaration, assuming that the `export' (and | |
13993 | `extern') keywords, if present, has already been scanned. MEMBER_P | |
13994 | is as for cp_parser_template_declaration. */ | |
13995 | ||
13996 | static void | |
94edc4ab | 13997 | cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p) |
a723baf1 MM |
13998 | { |
13999 | tree decl = NULL_TREE; | |
14000 | tree parameter_list; | |
14001 | bool friend_p = false; | |
14002 | ||
14003 | /* Look for the `template' keyword. */ | |
14004 | if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'")) | |
14005 | return; | |
14006 | ||
14007 | /* And the `<'. */ | |
14008 | if (!cp_parser_require (parser, CPP_LESS, "`<'")) | |
14009 | return; | |
14010 | ||
a723baf1 MM |
14011 | /* If the next token is `>', then we have an invalid |
14012 | specialization. Rather than complain about an invalid template | |
14013 | parameter, issue an error message here. */ | |
14014 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) | |
14015 | { | |
14016 | cp_parser_error (parser, "invalid explicit specialization"); | |
2f9afd51 | 14017 | begin_specialization (); |
a723baf1 MM |
14018 | parameter_list = NULL_TREE; |
14019 | } | |
14020 | else | |
2f9afd51 KL |
14021 | { |
14022 | /* Parse the template parameters. */ | |
14023 | begin_template_parm_list (); | |
14024 | parameter_list = cp_parser_template_parameter_list (parser); | |
14025 | parameter_list = end_template_parm_list (parameter_list); | |
14026 | } | |
14027 | ||
a723baf1 MM |
14028 | /* Look for the `>'. */ |
14029 | cp_parser_skip_until_found (parser, CPP_GREATER, "`>'"); | |
14030 | /* We just processed one more parameter list. */ | |
14031 | ++parser->num_template_parameter_lists; | |
14032 | /* If the next token is `template', there are more template | |
14033 | parameters. */ | |
14034 | if (cp_lexer_next_token_is_keyword (parser->lexer, | |
14035 | RID_TEMPLATE)) | |
14036 | cp_parser_template_declaration_after_export (parser, member_p); | |
14037 | else | |
14038 | { | |
14039 | decl = cp_parser_single_declaration (parser, | |
14040 | member_p, | |
14041 | &friend_p); | |
14042 | ||
14043 | /* If this is a member template declaration, let the front | |
14044 | end know. */ | |
14045 | if (member_p && !friend_p && decl) | |
37d407a1 KL |
14046 | { |
14047 | if (TREE_CODE (decl) == TYPE_DECL) | |
14048 | cp_parser_check_access_in_redeclaration (decl); | |
14049 | ||
14050 | decl = finish_member_template_decl (decl); | |
14051 | } | |
a723baf1 | 14052 | else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL) |
19db77ce KL |
14053 | make_friend_class (current_class_type, TREE_TYPE (decl), |
14054 | /*complain=*/true); | |
a723baf1 MM |
14055 | } |
14056 | /* We are done with the current parameter list. */ | |
14057 | --parser->num_template_parameter_lists; | |
14058 | ||
14059 | /* Finish up. */ | |
14060 | finish_template_decl (parameter_list); | |
14061 | ||
14062 | /* Register member declarations. */ | |
14063 | if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl)) | |
14064 | finish_member_declaration (decl); | |
14065 | ||
14066 | /* If DECL is a function template, we must return to parse it later. | |
14067 | (Even though there is no definition, there might be default | |
14068 | arguments that need handling.) */ | |
14069 | if (member_p && decl | |
14070 | && (TREE_CODE (decl) == FUNCTION_DECL | |
14071 | || DECL_FUNCTION_TEMPLATE_P (decl))) | |
14072 | TREE_VALUE (parser->unparsed_functions_queues) | |
8218bd34 | 14073 | = tree_cons (NULL_TREE, decl, |
a723baf1 MM |
14074 | TREE_VALUE (parser->unparsed_functions_queues)); |
14075 | } | |
14076 | ||
14077 | /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or | |
14078 | `function-definition' sequence. MEMBER_P is true, this declaration | |
14079 | appears in a class scope. | |
14080 | ||
14081 | Returns the DECL for the declared entity. If FRIEND_P is non-NULL, | |
14082 | *FRIEND_P is set to TRUE iff the declaration is a friend. */ | |
14083 | ||
14084 | static tree | |
94edc4ab NN |
14085 | cp_parser_single_declaration (cp_parser* parser, |
14086 | bool member_p, | |
14087 | bool* friend_p) | |
a723baf1 | 14088 | { |
560ad596 | 14089 | int declares_class_or_enum; |
a723baf1 MM |
14090 | tree decl = NULL_TREE; |
14091 | tree decl_specifiers; | |
14092 | tree attributes; | |
4bb8ca28 | 14093 | bool function_definition_p = false; |
a723baf1 | 14094 | |
a723baf1 | 14095 | /* Defer access checks until we know what is being declared. */ |
8d241e0b | 14096 | push_deferring_access_checks (dk_deferred); |
cf22909c | 14097 | |
a723baf1 MM |
14098 | /* Try the `decl-specifier-seq [opt] init-declarator [opt]' |
14099 | alternative. */ | |
14100 | decl_specifiers | |
14101 | = cp_parser_decl_specifier_seq (parser, | |
14102 | CP_PARSER_FLAGS_OPTIONAL, | |
14103 | &attributes, | |
14104 | &declares_class_or_enum); | |
4bb8ca28 MM |
14105 | if (friend_p) |
14106 | *friend_p = cp_parser_friend_p (decl_specifiers); | |
a723baf1 MM |
14107 | /* Gather up the access checks that occurred the |
14108 | decl-specifier-seq. */ | |
cf22909c KL |
14109 | stop_deferring_access_checks (); |
14110 | ||
a723baf1 MM |
14111 | /* Check for the declaration of a template class. */ |
14112 | if (declares_class_or_enum) | |
14113 | { | |
14114 | if (cp_parser_declares_only_class_p (parser)) | |
14115 | { | |
14116 | decl = shadow_tag (decl_specifiers); | |
14117 | if (decl) | |
14118 | decl = TYPE_NAME (decl); | |
14119 | else | |
14120 | decl = error_mark_node; | |
14121 | } | |
14122 | } | |
14123 | else | |
14124 | decl = NULL_TREE; | |
14125 | /* If it's not a template class, try for a template function. If | |
14126 | the next token is a `;', then this declaration does not declare | |
14127 | anything. But, if there were errors in the decl-specifiers, then | |
14128 | the error might well have come from an attempted class-specifier. | |
14129 | In that case, there's no need to warn about a missing declarator. */ | |
14130 | if (!decl | |
14131 | && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON) | |
14132 | || !value_member (error_mark_node, decl_specifiers))) | |
14133 | decl = cp_parser_init_declarator (parser, | |
14134 | decl_specifiers, | |
14135 | attributes, | |
4bb8ca28 | 14136 | /*function_definition_allowed_p=*/true, |
a723baf1 | 14137 | member_p, |
560ad596 | 14138 | declares_class_or_enum, |
4bb8ca28 | 14139 | &function_definition_p); |
cf22909c KL |
14140 | |
14141 | pop_deferring_access_checks (); | |
14142 | ||
a723baf1 MM |
14143 | /* Clear any current qualification; whatever comes next is the start |
14144 | of something new. */ | |
14145 | parser->scope = NULL_TREE; | |
14146 | parser->qualifying_scope = NULL_TREE; | |
14147 | parser->object_scope = NULL_TREE; | |
14148 | /* Look for a trailing `;' after the declaration. */ | |
4bb8ca28 MM |
14149 | if (!function_definition_p |
14150 | && !cp_parser_require (parser, CPP_SEMICOLON, "`;'")) | |
a723baf1 | 14151 | cp_parser_skip_to_end_of_block_or_statement (parser); |
a723baf1 MM |
14152 | |
14153 | return decl; | |
14154 | } | |
14155 | ||
d6b4ea85 MM |
14156 | /* Parse a cast-expression that is not the operand of a unary "&". */ |
14157 | ||
14158 | static tree | |
14159 | cp_parser_simple_cast_expression (cp_parser *parser) | |
14160 | { | |
14161 | return cp_parser_cast_expression (parser, /*address_p=*/false); | |
14162 | } | |
14163 | ||
a723baf1 MM |
14164 | /* Parse a functional cast to TYPE. Returns an expression |
14165 | representing the cast. */ | |
14166 | ||
14167 | static tree | |
94edc4ab | 14168 | cp_parser_functional_cast (cp_parser* parser, tree type) |
a723baf1 MM |
14169 | { |
14170 | tree expression_list; | |
14171 | ||
39703eb9 MM |
14172 | expression_list |
14173 | = cp_parser_parenthesized_expression_list (parser, false, | |
14174 | /*non_constant_p=*/NULL); | |
a723baf1 MM |
14175 | |
14176 | return build_functional_cast (type, expression_list); | |
14177 | } | |
14178 | ||
4bb8ca28 MM |
14179 | /* Save the tokens that make up the body of a member function defined |
14180 | in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have | |
14181 | already been parsed. The ATTRIBUTES are any GNU "__attribute__" | |
14182 | specifiers applied to the declaration. Returns the FUNCTION_DECL | |
14183 | for the member function. */ | |
14184 | ||
14185 | tree | |
14186 | cp_parser_save_member_function_body (cp_parser* parser, | |
14187 | tree decl_specifiers, | |
14188 | tree declarator, | |
14189 | tree attributes) | |
14190 | { | |
14191 | cp_token_cache *cache; | |
14192 | tree fn; | |
14193 | ||
14194 | /* Create the function-declaration. */ | |
14195 | fn = start_method (decl_specifiers, declarator, attributes); | |
14196 | /* If something went badly wrong, bail out now. */ | |
14197 | if (fn == error_mark_node) | |
14198 | { | |
14199 | /* If there's a function-body, skip it. */ | |
14200 | if (cp_parser_token_starts_function_definition_p | |
14201 | (cp_lexer_peek_token (parser->lexer))) | |
14202 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
14203 | return error_mark_node; | |
14204 | } | |
14205 | ||
14206 | /* Remember it, if there default args to post process. */ | |
14207 | cp_parser_save_default_args (parser, fn); | |
14208 | ||
14209 | /* Create a token cache. */ | |
14210 | cache = cp_token_cache_new (); | |
14211 | /* Save away the tokens that make up the body of the | |
14212 | function. */ | |
14213 | cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0); | |
14214 | /* Handle function try blocks. */ | |
14215 | while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH)) | |
14216 | cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0); | |
14217 | ||
14218 | /* Save away the inline definition; we will process it when the | |
14219 | class is complete. */ | |
14220 | DECL_PENDING_INLINE_INFO (fn) = cache; | |
14221 | DECL_PENDING_INLINE_P (fn) = 1; | |
14222 | ||
14223 | /* We need to know that this was defined in the class, so that | |
14224 | friend templates are handled correctly. */ | |
14225 | DECL_INITIALIZED_IN_CLASS_P (fn) = 1; | |
14226 | ||
14227 | /* We're done with the inline definition. */ | |
14228 | finish_method (fn); | |
14229 | ||
14230 | /* Add FN to the queue of functions to be parsed later. */ | |
14231 | TREE_VALUE (parser->unparsed_functions_queues) | |
14232 | = tree_cons (NULL_TREE, fn, | |
14233 | TREE_VALUE (parser->unparsed_functions_queues)); | |
14234 | ||
14235 | return fn; | |
14236 | } | |
14237 | ||
ec75414f MM |
14238 | /* Parse a template-argument-list, as well as the trailing ">" (but |
14239 | not the opening ">"). See cp_parser_template_argument_list for the | |
14240 | return value. */ | |
14241 | ||
14242 | static tree | |
14243 | cp_parser_enclosed_template_argument_list (cp_parser* parser) | |
14244 | { | |
14245 | tree arguments; | |
14246 | tree saved_scope; | |
14247 | tree saved_qualifying_scope; | |
14248 | tree saved_object_scope; | |
14249 | bool saved_greater_than_is_operator_p; | |
14250 | ||
14251 | /* [temp.names] | |
14252 | ||
14253 | When parsing a template-id, the first non-nested `>' is taken as | |
14254 | the end of the template-argument-list rather than a greater-than | |
14255 | operator. */ | |
14256 | saved_greater_than_is_operator_p | |
14257 | = parser->greater_than_is_operator_p; | |
14258 | parser->greater_than_is_operator_p = false; | |
14259 | /* Parsing the argument list may modify SCOPE, so we save it | |
14260 | here. */ | |
14261 | saved_scope = parser->scope; | |
14262 | saved_qualifying_scope = parser->qualifying_scope; | |
14263 | saved_object_scope = parser->object_scope; | |
14264 | /* Parse the template-argument-list itself. */ | |
14265 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) | |
14266 | arguments = NULL_TREE; | |
14267 | else | |
14268 | arguments = cp_parser_template_argument_list (parser); | |
4d5297fa GB |
14269 | /* Look for the `>' that ends the template-argument-list. If we find |
14270 | a '>>' instead, it's probably just a typo. */ | |
14271 | if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) | |
14272 | { | |
14273 | if (!saved_greater_than_is_operator_p) | |
14274 | { | |
14275 | /* If we're in a nested template argument list, the '>>' has to be | |
14276 | a typo for '> >'. We emit the error message, but we continue | |
14277 | parsing and we push a '>' as next token, so that the argument | |
14278 | list will be parsed correctly.. */ | |
14279 | cp_token* token; | |
14280 | error ("`>>' should be `> >' within a nested template argument list"); | |
14281 | token = cp_lexer_peek_token (parser->lexer); | |
14282 | token->type = CPP_GREATER; | |
14283 | } | |
14284 | else | |
14285 | { | |
14286 | /* If this is not a nested template argument list, the '>>' is | |
14287 | a typo for '>'. Emit an error message and continue. */ | |
14288 | error ("spurious `>>', use `>' to terminate a template argument list"); | |
14289 | cp_lexer_consume_token (parser->lexer); | |
14290 | } | |
14291 | } | |
14292 | else | |
14293 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
ec75414f MM |
14294 | /* The `>' token might be a greater-than operator again now. */ |
14295 | parser->greater_than_is_operator_p | |
14296 | = saved_greater_than_is_operator_p; | |
14297 | /* Restore the SAVED_SCOPE. */ | |
14298 | parser->scope = saved_scope; | |
14299 | parser->qualifying_scope = saved_qualifying_scope; | |
14300 | parser->object_scope = saved_object_scope; | |
14301 | ||
14302 | return arguments; | |
14303 | } | |
14304 | ||
a723baf1 MM |
14305 | /* MEMBER_FUNCTION is a member function, or a friend. If default |
14306 | arguments, or the body of the function have not yet been parsed, | |
14307 | parse them now. */ | |
14308 | ||
14309 | static void | |
94edc4ab | 14310 | cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function) |
a723baf1 MM |
14311 | { |
14312 | cp_lexer *saved_lexer; | |
14313 | ||
14314 | /* If this member is a template, get the underlying | |
14315 | FUNCTION_DECL. */ | |
14316 | if (DECL_FUNCTION_TEMPLATE_P (member_function)) | |
14317 | member_function = DECL_TEMPLATE_RESULT (member_function); | |
14318 | ||
14319 | /* There should not be any class definitions in progress at this | |
14320 | point; the bodies of members are only parsed outside of all class | |
14321 | definitions. */ | |
14322 | my_friendly_assert (parser->num_classes_being_defined == 0, 20010816); | |
14323 | /* While we're parsing the member functions we might encounter more | |
14324 | classes. We want to handle them right away, but we don't want | |
14325 | them getting mixed up with functions that are currently in the | |
14326 | queue. */ | |
14327 | parser->unparsed_functions_queues | |
14328 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
14329 | ||
14330 | /* Make sure that any template parameters are in scope. */ | |
14331 | maybe_begin_member_template_processing (member_function); | |
14332 | ||
a723baf1 MM |
14333 | /* If the body of the function has not yet been parsed, parse it |
14334 | now. */ | |
14335 | if (DECL_PENDING_INLINE_P (member_function)) | |
14336 | { | |
14337 | tree function_scope; | |
14338 | cp_token_cache *tokens; | |
14339 | ||
14340 | /* The function is no longer pending; we are processing it. */ | |
14341 | tokens = DECL_PENDING_INLINE_INFO (member_function); | |
14342 | DECL_PENDING_INLINE_INFO (member_function) = NULL; | |
14343 | DECL_PENDING_INLINE_P (member_function) = 0; | |
14344 | /* If this was an inline function in a local class, enter the scope | |
14345 | of the containing function. */ | |
14346 | function_scope = decl_function_context (member_function); | |
14347 | if (function_scope) | |
14348 | push_function_context_to (function_scope); | |
14349 | ||
14350 | /* Save away the current lexer. */ | |
14351 | saved_lexer = parser->lexer; | |
14352 | /* Make a new lexer to feed us the tokens saved for this function. */ | |
14353 | parser->lexer = cp_lexer_new_from_tokens (tokens); | |
14354 | parser->lexer->next = saved_lexer; | |
14355 | ||
14356 | /* Set the current source position to be the location of the first | |
14357 | token in the saved inline body. */ | |
3466b292 | 14358 | cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
14359 | |
14360 | /* Let the front end know that we going to be defining this | |
14361 | function. */ | |
14362 | start_function (NULL_TREE, member_function, NULL_TREE, | |
14363 | SF_PRE_PARSED | SF_INCLASS_INLINE); | |
14364 | ||
14365 | /* Now, parse the body of the function. */ | |
14366 | cp_parser_function_definition_after_declarator (parser, | |
14367 | /*inline_p=*/true); | |
14368 | ||
14369 | /* Leave the scope of the containing function. */ | |
14370 | if (function_scope) | |
14371 | pop_function_context_from (function_scope); | |
14372 | /* Restore the lexer. */ | |
14373 | parser->lexer = saved_lexer; | |
14374 | } | |
14375 | ||
14376 | /* Remove any template parameters from the symbol table. */ | |
14377 | maybe_end_member_template_processing (); | |
14378 | ||
14379 | /* Restore the queue. */ | |
14380 | parser->unparsed_functions_queues | |
14381 | = TREE_CHAIN (parser->unparsed_functions_queues); | |
14382 | } | |
14383 | ||
cd0be382 | 14384 | /* If DECL contains any default args, remember it on the unparsed |
8db1028e NS |
14385 | functions queue. */ |
14386 | ||
14387 | static void | |
14388 | cp_parser_save_default_args (cp_parser* parser, tree decl) | |
14389 | { | |
14390 | tree probe; | |
14391 | ||
14392 | for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
14393 | probe; | |
14394 | probe = TREE_CHAIN (probe)) | |
14395 | if (TREE_PURPOSE (probe)) | |
14396 | { | |
14397 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
14398 | = tree_cons (NULL_TREE, decl, | |
14399 | TREE_PURPOSE (parser->unparsed_functions_queues)); | |
14400 | break; | |
14401 | } | |
14402 | return; | |
14403 | } | |
14404 | ||
8218bd34 MM |
14405 | /* FN is a FUNCTION_DECL which may contains a parameter with an |
14406 | unparsed DEFAULT_ARG. Parse the default args now. */ | |
a723baf1 MM |
14407 | |
14408 | static void | |
8218bd34 | 14409 | cp_parser_late_parsing_default_args (cp_parser *parser, tree fn) |
a723baf1 MM |
14410 | { |
14411 | cp_lexer *saved_lexer; | |
14412 | cp_token_cache *tokens; | |
14413 | bool saved_local_variables_forbidden_p; | |
14414 | tree parameters; | |
8218bd34 | 14415 | |
b92bc2a0 NS |
14416 | /* While we're parsing the default args, we might (due to the |
14417 | statement expression extension) encounter more classes. We want | |
14418 | to handle them right away, but we don't want them getting mixed | |
14419 | up with default args that are currently in the queue. */ | |
14420 | parser->unparsed_functions_queues | |
14421 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
14422 | ||
8218bd34 | 14423 | for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
a723baf1 MM |
14424 | parameters; |
14425 | parameters = TREE_CHAIN (parameters)) | |
14426 | { | |
14427 | if (!TREE_PURPOSE (parameters) | |
14428 | || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG) | |
14429 | continue; | |
14430 | ||
14431 | /* Save away the current lexer. */ | |
14432 | saved_lexer = parser->lexer; | |
14433 | /* Create a new one, using the tokens we have saved. */ | |
14434 | tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters)); | |
14435 | parser->lexer = cp_lexer_new_from_tokens (tokens); | |
14436 | ||
14437 | /* Set the current source position to be the location of the | |
14438 | first token in the default argument. */ | |
3466b292 | 14439 | cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
14440 | |
14441 | /* Local variable names (and the `this' keyword) may not appear | |
14442 | in a default argument. */ | |
14443 | saved_local_variables_forbidden_p = parser->local_variables_forbidden_p; | |
14444 | parser->local_variables_forbidden_p = true; | |
14445 | /* Parse the assignment-expression. */ | |
f128e1f3 | 14446 | if (DECL_CLASS_SCOPE_P (fn)) |
14d22dd6 | 14447 | push_nested_class (DECL_CONTEXT (fn)); |
a723baf1 | 14448 | TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser); |
f128e1f3 | 14449 | if (DECL_CLASS_SCOPE_P (fn)) |
e5976695 | 14450 | pop_nested_class (); |
a723baf1 MM |
14451 | |
14452 | /* Restore saved state. */ | |
14453 | parser->lexer = saved_lexer; | |
14454 | parser->local_variables_forbidden_p = saved_local_variables_forbidden_p; | |
14455 | } | |
b92bc2a0 NS |
14456 | |
14457 | /* Restore the queue. */ | |
14458 | parser->unparsed_functions_queues | |
14459 | = TREE_CHAIN (parser->unparsed_functions_queues); | |
a723baf1 MM |
14460 | } |
14461 | ||
14462 | /* Parse the operand of `sizeof' (or a similar operator). Returns | |
14463 | either a TYPE or an expression, depending on the form of the | |
14464 | input. The KEYWORD indicates which kind of expression we have | |
14465 | encountered. */ | |
14466 | ||
14467 | static tree | |
94edc4ab | 14468 | cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword) |
a723baf1 MM |
14469 | { |
14470 | static const char *format; | |
14471 | tree expr = NULL_TREE; | |
14472 | const char *saved_message; | |
67c03833 | 14473 | bool saved_integral_constant_expression_p; |
a723baf1 MM |
14474 | |
14475 | /* Initialize FORMAT the first time we get here. */ | |
14476 | if (!format) | |
14477 | format = "types may not be defined in `%s' expressions"; | |
14478 | ||
14479 | /* Types cannot be defined in a `sizeof' expression. Save away the | |
14480 | old message. */ | |
14481 | saved_message = parser->type_definition_forbidden_message; | |
14482 | /* And create the new one. */ | |
14483 | parser->type_definition_forbidden_message | |
c68b0a84 KG |
14484 | = xmalloc (strlen (format) |
14485 | + strlen (IDENTIFIER_POINTER (ridpointers[keyword])) | |
14486 | + 1 /* `\0' */); | |
a723baf1 MM |
14487 | sprintf ((char *) parser->type_definition_forbidden_message, |
14488 | format, IDENTIFIER_POINTER (ridpointers[keyword])); | |
14489 | ||
14490 | /* The restrictions on constant-expressions do not apply inside | |
14491 | sizeof expressions. */ | |
67c03833 JM |
14492 | saved_integral_constant_expression_p = parser->integral_constant_expression_p; |
14493 | parser->integral_constant_expression_p = false; | |
a723baf1 | 14494 | |
3beb3abf MM |
14495 | /* Do not actually evaluate the expression. */ |
14496 | ++skip_evaluation; | |
a723baf1 MM |
14497 | /* If it's a `(', then we might be looking at the type-id |
14498 | construction. */ | |
14499 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
14500 | { | |
14501 | tree type; | |
4f8163b1 | 14502 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
14503 | |
14504 | /* We can't be sure yet whether we're looking at a type-id or an | |
14505 | expression. */ | |
14506 | cp_parser_parse_tentatively (parser); | |
14507 | /* Consume the `('. */ | |
14508 | cp_lexer_consume_token (parser->lexer); | |
14509 | /* Parse the type-id. */ | |
4f8163b1 MM |
14510 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
14511 | parser->in_type_id_in_expr_p = true; | |
a723baf1 | 14512 | type = cp_parser_type_id (parser); |
4f8163b1 | 14513 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 MM |
14514 | /* Now, look for the trailing `)'. */ |
14515 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
14516 | /* If all went well, then we're done. */ | |
14517 | if (cp_parser_parse_definitely (parser)) | |
14518 | { | |
14519 | /* Build a list of decl-specifiers; right now, we have only | |
14520 | a single type-specifier. */ | |
14521 | type = build_tree_list (NULL_TREE, | |
14522 | type); | |
14523 | ||
14524 | /* Call grokdeclarator to figure out what type this is. */ | |
14525 | expr = grokdeclarator (NULL_TREE, | |
14526 | type, | |
14527 | TYPENAME, | |
14528 | /*initialized=*/0, | |
14529 | /*attrlist=*/NULL); | |
14530 | } | |
14531 | } | |
14532 | ||
14533 | /* If the type-id production did not work out, then we must be | |
14534 | looking at the unary-expression production. */ | |
14535 | if (!expr) | |
14536 | expr = cp_parser_unary_expression (parser, /*address_p=*/false); | |
3beb3abf MM |
14537 | /* Go back to evaluating expressions. */ |
14538 | --skip_evaluation; | |
a723baf1 MM |
14539 | |
14540 | /* Free the message we created. */ | |
14541 | free ((char *) parser->type_definition_forbidden_message); | |
14542 | /* And restore the old one. */ | |
14543 | parser->type_definition_forbidden_message = saved_message; | |
67c03833 | 14544 | parser->integral_constant_expression_p = saved_integral_constant_expression_p; |
a723baf1 MM |
14545 | |
14546 | return expr; | |
14547 | } | |
14548 | ||
14549 | /* If the current declaration has no declarator, return true. */ | |
14550 | ||
14551 | static bool | |
14552 | cp_parser_declares_only_class_p (cp_parser *parser) | |
14553 | { | |
14554 | /* If the next token is a `;' or a `,' then there is no | |
14555 | declarator. */ | |
14556 | return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) | |
14557 | || cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); | |
14558 | } | |
14559 | ||
d17811fd MM |
14560 | /* Simplify EXPR if it is a non-dependent expression. Returns the |
14561 | (possibly simplified) expression. */ | |
14562 | ||
14563 | static tree | |
14564 | cp_parser_fold_non_dependent_expr (tree expr) | |
14565 | { | |
14566 | /* If we're in a template, but EXPR isn't value dependent, simplify | |
14567 | it. We're supposed to treat: | |
14568 | ||
14569 | template <typename T> void f(T[1 + 1]); | |
14570 | template <typename T> void f(T[2]); | |
14571 | ||
14572 | as two declarations of the same function, for example. */ | |
14573 | if (processing_template_decl | |
14574 | && !type_dependent_expression_p (expr) | |
14575 | && !value_dependent_expression_p (expr)) | |
14576 | { | |
14577 | HOST_WIDE_INT saved_processing_template_decl; | |
14578 | ||
14579 | saved_processing_template_decl = processing_template_decl; | |
14580 | processing_template_decl = 0; | |
14581 | expr = tsubst_copy_and_build (expr, | |
14582 | /*args=*/NULL_TREE, | |
14583 | tf_error, | |
b3445994 MM |
14584 | /*in_decl=*/NULL_TREE, |
14585 | /*function_p=*/false); | |
d17811fd MM |
14586 | processing_template_decl = saved_processing_template_decl; |
14587 | } | |
14588 | return expr; | |
14589 | } | |
14590 | ||
a723baf1 MM |
14591 | /* DECL_SPECIFIERS is the representation of a decl-specifier-seq. |
14592 | Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */ | |
14593 | ||
14594 | static bool | |
94edc4ab | 14595 | cp_parser_friend_p (tree decl_specifiers) |
a723baf1 MM |
14596 | { |
14597 | while (decl_specifiers) | |
14598 | { | |
14599 | /* See if this decl-specifier is `friend'. */ | |
14600 | if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE | |
14601 | && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND) | |
14602 | return true; | |
14603 | ||
14604 | /* Go on to the next decl-specifier. */ | |
14605 | decl_specifiers = TREE_CHAIN (decl_specifiers); | |
14606 | } | |
14607 | ||
14608 | return false; | |
14609 | } | |
14610 | ||
14611 | /* If the next token is of the indicated TYPE, consume it. Otherwise, | |
14612 | issue an error message indicating that TOKEN_DESC was expected. | |
14613 | ||
14614 | Returns the token consumed, if the token had the appropriate type. | |
14615 | Otherwise, returns NULL. */ | |
14616 | ||
14617 | static cp_token * | |
94edc4ab NN |
14618 | cp_parser_require (cp_parser* parser, |
14619 | enum cpp_ttype type, | |
14620 | const char* token_desc) | |
a723baf1 MM |
14621 | { |
14622 | if (cp_lexer_next_token_is (parser->lexer, type)) | |
14623 | return cp_lexer_consume_token (parser->lexer); | |
14624 | else | |
14625 | { | |
e5976695 MM |
14626 | /* Output the MESSAGE -- unless we're parsing tentatively. */ |
14627 | if (!cp_parser_simulate_error (parser)) | |
14628 | error ("expected %s", token_desc); | |
a723baf1 MM |
14629 | return NULL; |
14630 | } | |
14631 | } | |
14632 | ||
14633 | /* Like cp_parser_require, except that tokens will be skipped until | |
14634 | the desired token is found. An error message is still produced if | |
14635 | the next token is not as expected. */ | |
14636 | ||
14637 | static void | |
94edc4ab NN |
14638 | cp_parser_skip_until_found (cp_parser* parser, |
14639 | enum cpp_ttype type, | |
14640 | const char* token_desc) | |
a723baf1 MM |
14641 | { |
14642 | cp_token *token; | |
14643 | unsigned nesting_depth = 0; | |
14644 | ||
14645 | if (cp_parser_require (parser, type, token_desc)) | |
14646 | return; | |
14647 | ||
14648 | /* Skip tokens until the desired token is found. */ | |
14649 | while (true) | |
14650 | { | |
14651 | /* Peek at the next token. */ | |
14652 | token = cp_lexer_peek_token (parser->lexer); | |
14653 | /* If we've reached the token we want, consume it and | |
14654 | stop. */ | |
14655 | if (token->type == type && !nesting_depth) | |
14656 | { | |
14657 | cp_lexer_consume_token (parser->lexer); | |
14658 | return; | |
14659 | } | |
14660 | /* If we've run out of tokens, stop. */ | |
14661 | if (token->type == CPP_EOF) | |
14662 | return; | |
14663 | if (token->type == CPP_OPEN_BRACE | |
14664 | || token->type == CPP_OPEN_PAREN | |
14665 | || token->type == CPP_OPEN_SQUARE) | |
14666 | ++nesting_depth; | |
14667 | else if (token->type == CPP_CLOSE_BRACE | |
14668 | || token->type == CPP_CLOSE_PAREN | |
14669 | || token->type == CPP_CLOSE_SQUARE) | |
14670 | { | |
14671 | if (nesting_depth-- == 0) | |
14672 | return; | |
14673 | } | |
14674 | /* Consume this token. */ | |
14675 | cp_lexer_consume_token (parser->lexer); | |
14676 | } | |
14677 | } | |
14678 | ||
14679 | /* If the next token is the indicated keyword, consume it. Otherwise, | |
14680 | issue an error message indicating that TOKEN_DESC was expected. | |
14681 | ||
14682 | Returns the token consumed, if the token had the appropriate type. | |
14683 | Otherwise, returns NULL. */ | |
14684 | ||
14685 | static cp_token * | |
94edc4ab NN |
14686 | cp_parser_require_keyword (cp_parser* parser, |
14687 | enum rid keyword, | |
14688 | const char* token_desc) | |
a723baf1 MM |
14689 | { |
14690 | cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc); | |
14691 | ||
14692 | if (token && token->keyword != keyword) | |
14693 | { | |
14694 | dyn_string_t error_msg; | |
14695 | ||
14696 | /* Format the error message. */ | |
14697 | error_msg = dyn_string_new (0); | |
14698 | dyn_string_append_cstr (error_msg, "expected "); | |
14699 | dyn_string_append_cstr (error_msg, token_desc); | |
14700 | cp_parser_error (parser, error_msg->s); | |
14701 | dyn_string_delete (error_msg); | |
14702 | return NULL; | |
14703 | } | |
14704 | ||
14705 | return token; | |
14706 | } | |
14707 | ||
14708 | /* Returns TRUE iff TOKEN is a token that can begin the body of a | |
14709 | function-definition. */ | |
14710 | ||
14711 | static bool | |
94edc4ab | 14712 | cp_parser_token_starts_function_definition_p (cp_token* token) |
a723baf1 MM |
14713 | { |
14714 | return (/* An ordinary function-body begins with an `{'. */ | |
14715 | token->type == CPP_OPEN_BRACE | |
14716 | /* A ctor-initializer begins with a `:'. */ | |
14717 | || token->type == CPP_COLON | |
14718 | /* A function-try-block begins with `try'. */ | |
14719 | || token->keyword == RID_TRY | |
14720 | /* The named return value extension begins with `return'. */ | |
14721 | || token->keyword == RID_RETURN); | |
14722 | } | |
14723 | ||
14724 | /* Returns TRUE iff the next token is the ":" or "{" beginning a class | |
14725 | definition. */ | |
14726 | ||
14727 | static bool | |
14728 | cp_parser_next_token_starts_class_definition_p (cp_parser *parser) | |
14729 | { | |
14730 | cp_token *token; | |
14731 | ||
14732 | token = cp_lexer_peek_token (parser->lexer); | |
14733 | return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON); | |
14734 | } | |
14735 | ||
d17811fd | 14736 | /* Returns TRUE iff the next token is the "," or ">" ending a |
4d5297fa GB |
14737 | template-argument. ">>" is also accepted (after the full |
14738 | argument was parsed) because it's probably a typo for "> >", | |
14739 | and there is a specific diagnostic for this. */ | |
d17811fd MM |
14740 | |
14741 | static bool | |
14742 | cp_parser_next_token_ends_template_argument_p (cp_parser *parser) | |
14743 | { | |
14744 | cp_token *token; | |
14745 | ||
14746 | token = cp_lexer_peek_token (parser->lexer); | |
4d5297fa GB |
14747 | return (token->type == CPP_COMMA || token->type == CPP_GREATER |
14748 | || token->type == CPP_RSHIFT); | |
d17811fd MM |
14749 | } |
14750 | ||
a723baf1 MM |
14751 | /* Returns the kind of tag indicated by TOKEN, if it is a class-key, |
14752 | or none_type otherwise. */ | |
14753 | ||
14754 | static enum tag_types | |
94edc4ab | 14755 | cp_parser_token_is_class_key (cp_token* token) |
a723baf1 MM |
14756 | { |
14757 | switch (token->keyword) | |
14758 | { | |
14759 | case RID_CLASS: | |
14760 | return class_type; | |
14761 | case RID_STRUCT: | |
14762 | return record_type; | |
14763 | case RID_UNION: | |
14764 | return union_type; | |
14765 | ||
14766 | default: | |
14767 | return none_type; | |
14768 | } | |
14769 | } | |
14770 | ||
14771 | /* Issue an error message if the CLASS_KEY does not match the TYPE. */ | |
14772 | ||
14773 | static void | |
14774 | cp_parser_check_class_key (enum tag_types class_key, tree type) | |
14775 | { | |
14776 | if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type)) | |
14777 | pedwarn ("`%s' tag used in naming `%#T'", | |
14778 | class_key == union_type ? "union" | |
14779 | : class_key == record_type ? "struct" : "class", | |
14780 | type); | |
14781 | } | |
14782 | ||
cd0be382 | 14783 | /* Issue an error message if DECL is redeclared with different |
37d407a1 KL |
14784 | access than its original declaration [class.access.spec/3]. |
14785 | This applies to nested classes and nested class templates. | |
14786 | [class.mem/1]. */ | |
14787 | ||
14788 | static void cp_parser_check_access_in_redeclaration (tree decl) | |
14789 | { | |
14790 | if (!CLASS_TYPE_P (TREE_TYPE (decl))) | |
14791 | return; | |
14792 | ||
14793 | if ((TREE_PRIVATE (decl) | |
14794 | != (current_access_specifier == access_private_node)) | |
14795 | || (TREE_PROTECTED (decl) | |
14796 | != (current_access_specifier == access_protected_node))) | |
14797 | error ("%D redeclared with different access", decl); | |
14798 | } | |
14799 | ||
a723baf1 MM |
14800 | /* Look for the `template' keyword, as a syntactic disambiguator. |
14801 | Return TRUE iff it is present, in which case it will be | |
14802 | consumed. */ | |
14803 | ||
14804 | static bool | |
14805 | cp_parser_optional_template_keyword (cp_parser *parser) | |
14806 | { | |
14807 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
14808 | { | |
14809 | /* The `template' keyword can only be used within templates; | |
14810 | outside templates the parser can always figure out what is a | |
14811 | template and what is not. */ | |
14812 | if (!processing_template_decl) | |
14813 | { | |
14814 | error ("`template' (as a disambiguator) is only allowed " | |
14815 | "within templates"); | |
14816 | /* If this part of the token stream is rescanned, the same | |
14817 | error message would be generated. So, we purge the token | |
14818 | from the stream. */ | |
14819 | cp_lexer_purge_token (parser->lexer); | |
14820 | return false; | |
14821 | } | |
14822 | else | |
14823 | { | |
14824 | /* Consume the `template' keyword. */ | |
14825 | cp_lexer_consume_token (parser->lexer); | |
14826 | return true; | |
14827 | } | |
14828 | } | |
14829 | ||
14830 | return false; | |
14831 | } | |
14832 | ||
2050a1bb MM |
14833 | /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token, |
14834 | set PARSER->SCOPE, and perform other related actions. */ | |
14835 | ||
14836 | static void | |
14837 | cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser) | |
14838 | { | |
14839 | tree value; | |
14840 | tree check; | |
14841 | ||
14842 | /* Get the stored value. */ | |
14843 | value = cp_lexer_consume_token (parser->lexer)->value; | |
14844 | /* Perform any access checks that were deferred. */ | |
14845 | for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check)) | |
cf22909c | 14846 | perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check)); |
2050a1bb MM |
14847 | /* Set the scope from the stored value. */ |
14848 | parser->scope = TREE_VALUE (value); | |
14849 | parser->qualifying_scope = TREE_TYPE (value); | |
14850 | parser->object_scope = NULL_TREE; | |
14851 | } | |
14852 | ||
a723baf1 MM |
14853 | /* Add tokens to CACHE until an non-nested END token appears. */ |
14854 | ||
14855 | static void | |
14856 | cp_parser_cache_group (cp_parser *parser, | |
14857 | cp_token_cache *cache, | |
14858 | enum cpp_ttype end, | |
14859 | unsigned depth) | |
14860 | { | |
14861 | while (true) | |
14862 | { | |
14863 | cp_token *token; | |
14864 | ||
14865 | /* Abort a parenthesized expression if we encounter a brace. */ | |
14866 | if ((end == CPP_CLOSE_PAREN || depth == 0) | |
14867 | && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
14868 | return; | |
14869 | /* Consume the next token. */ | |
14870 | token = cp_lexer_consume_token (parser->lexer); | |
14871 | /* If we've reached the end of the file, stop. */ | |
14872 | if (token->type == CPP_EOF) | |
14873 | return; | |
14874 | /* Add this token to the tokens we are saving. */ | |
14875 | cp_token_cache_push_token (cache, token); | |
14876 | /* See if it starts a new group. */ | |
14877 | if (token->type == CPP_OPEN_BRACE) | |
14878 | { | |
14879 | cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1); | |
14880 | if (depth == 0) | |
14881 | return; | |
14882 | } | |
14883 | else if (token->type == CPP_OPEN_PAREN) | |
14884 | cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1); | |
14885 | else if (token->type == end) | |
14886 | return; | |
14887 | } | |
14888 | } | |
14889 | ||
14890 | /* Begin parsing tentatively. We always save tokens while parsing | |
14891 | tentatively so that if the tentative parsing fails we can restore the | |
14892 | tokens. */ | |
14893 | ||
14894 | static void | |
94edc4ab | 14895 | cp_parser_parse_tentatively (cp_parser* parser) |
a723baf1 MM |
14896 | { |
14897 | /* Enter a new parsing context. */ | |
14898 | parser->context = cp_parser_context_new (parser->context); | |
14899 | /* Begin saving tokens. */ | |
14900 | cp_lexer_save_tokens (parser->lexer); | |
14901 | /* In order to avoid repetitive access control error messages, | |
14902 | access checks are queued up until we are no longer parsing | |
14903 | tentatively. */ | |
8d241e0b | 14904 | push_deferring_access_checks (dk_deferred); |
a723baf1 MM |
14905 | } |
14906 | ||
14907 | /* Commit to the currently active tentative parse. */ | |
14908 | ||
14909 | static void | |
94edc4ab | 14910 | cp_parser_commit_to_tentative_parse (cp_parser* parser) |
a723baf1 MM |
14911 | { |
14912 | cp_parser_context *context; | |
14913 | cp_lexer *lexer; | |
14914 | ||
14915 | /* Mark all of the levels as committed. */ | |
14916 | lexer = parser->lexer; | |
14917 | for (context = parser->context; context->next; context = context->next) | |
14918 | { | |
14919 | if (context->status == CP_PARSER_STATUS_KIND_COMMITTED) | |
14920 | break; | |
14921 | context->status = CP_PARSER_STATUS_KIND_COMMITTED; | |
14922 | while (!cp_lexer_saving_tokens (lexer)) | |
14923 | lexer = lexer->next; | |
14924 | cp_lexer_commit_tokens (lexer); | |
14925 | } | |
14926 | } | |
14927 | ||
14928 | /* Abort the currently active tentative parse. All consumed tokens | |
14929 | will be rolled back, and no diagnostics will be issued. */ | |
14930 | ||
14931 | static void | |
94edc4ab | 14932 | cp_parser_abort_tentative_parse (cp_parser* parser) |
a723baf1 MM |
14933 | { |
14934 | cp_parser_simulate_error (parser); | |
14935 | /* Now, pretend that we want to see if the construct was | |
14936 | successfully parsed. */ | |
14937 | cp_parser_parse_definitely (parser); | |
14938 | } | |
14939 | ||
34cd5ae7 | 14940 | /* Stop parsing tentatively. If a parse error has occurred, restore the |
a723baf1 MM |
14941 | token stream. Otherwise, commit to the tokens we have consumed. |
14942 | Returns true if no error occurred; false otherwise. */ | |
14943 | ||
14944 | static bool | |
94edc4ab | 14945 | cp_parser_parse_definitely (cp_parser* parser) |
a723baf1 MM |
14946 | { |
14947 | bool error_occurred; | |
14948 | cp_parser_context *context; | |
14949 | ||
34cd5ae7 | 14950 | /* Remember whether or not an error occurred, since we are about to |
a723baf1 MM |
14951 | destroy that information. */ |
14952 | error_occurred = cp_parser_error_occurred (parser); | |
14953 | /* Remove the topmost context from the stack. */ | |
14954 | context = parser->context; | |
14955 | parser->context = context->next; | |
14956 | /* If no parse errors occurred, commit to the tentative parse. */ | |
14957 | if (!error_occurred) | |
14958 | { | |
14959 | /* Commit to the tokens read tentatively, unless that was | |
14960 | already done. */ | |
14961 | if (context->status != CP_PARSER_STATUS_KIND_COMMITTED) | |
14962 | cp_lexer_commit_tokens (parser->lexer); | |
cf22909c KL |
14963 | |
14964 | pop_to_parent_deferring_access_checks (); | |
a723baf1 MM |
14965 | } |
14966 | /* Otherwise, if errors occurred, roll back our state so that things | |
14967 | are just as they were before we began the tentative parse. */ | |
14968 | else | |
cf22909c KL |
14969 | { |
14970 | cp_lexer_rollback_tokens (parser->lexer); | |
14971 | pop_deferring_access_checks (); | |
14972 | } | |
e5976695 MM |
14973 | /* Add the context to the front of the free list. */ |
14974 | context->next = cp_parser_context_free_list; | |
14975 | cp_parser_context_free_list = context; | |
14976 | ||
14977 | return !error_occurred; | |
a723baf1 MM |
14978 | } |
14979 | ||
a723baf1 MM |
14980 | /* Returns true if we are parsing tentatively -- but have decided that |
14981 | we will stick with this tentative parse, even if errors occur. */ | |
14982 | ||
14983 | static bool | |
94edc4ab | 14984 | cp_parser_committed_to_tentative_parse (cp_parser* parser) |
a723baf1 MM |
14985 | { |
14986 | return (cp_parser_parsing_tentatively (parser) | |
14987 | && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED); | |
14988 | } | |
14989 | ||
4de8668e | 14990 | /* Returns nonzero iff an error has occurred during the most recent |
a723baf1 MM |
14991 | tentative parse. */ |
14992 | ||
14993 | static bool | |
94edc4ab | 14994 | cp_parser_error_occurred (cp_parser* parser) |
a723baf1 MM |
14995 | { |
14996 | return (cp_parser_parsing_tentatively (parser) | |
14997 | && parser->context->status == CP_PARSER_STATUS_KIND_ERROR); | |
14998 | } | |
14999 | ||
4de8668e | 15000 | /* Returns nonzero if GNU extensions are allowed. */ |
a723baf1 MM |
15001 | |
15002 | static bool | |
94edc4ab | 15003 | cp_parser_allow_gnu_extensions_p (cp_parser* parser) |
a723baf1 MM |
15004 | { |
15005 | return parser->allow_gnu_extensions_p; | |
15006 | } | |
15007 | ||
15008 | \f | |
15009 | ||
15010 | /* The parser. */ | |
15011 | ||
15012 | static GTY (()) cp_parser *the_parser; | |
15013 | ||
15014 | /* External interface. */ | |
15015 | ||
d1bd0ded | 15016 | /* Parse one entire translation unit. */ |
a723baf1 | 15017 | |
d1bd0ded GK |
15018 | void |
15019 | c_parse_file (void) | |
a723baf1 MM |
15020 | { |
15021 | bool error_occurred; | |
15022 | ||
15023 | the_parser = cp_parser_new (); | |
78757caa KL |
15024 | push_deferring_access_checks (flag_access_control |
15025 | ? dk_no_deferred : dk_no_check); | |
a723baf1 MM |
15026 | error_occurred = cp_parser_translation_unit (the_parser); |
15027 | the_parser = NULL; | |
a723baf1 MM |
15028 | } |
15029 | ||
15030 | /* Clean up after parsing the entire translation unit. */ | |
15031 | ||
15032 | void | |
94edc4ab | 15033 | free_parser_stacks (void) |
a723baf1 MM |
15034 | { |
15035 | /* Nothing to do. */ | |
15036 | } | |
15037 | ||
15038 | /* This variable must be provided by every front end. */ | |
15039 | ||
15040 | int yydebug; | |
15041 | ||
15042 | #include "gt-cp-parser.h" |