]>
Commit | Line | Data |
---|---|---|
a723baf1 | 1 | /* C++ Parser. |
3beb3abf | 2 | Copyright (C) 2000, 2001, 2002, 2003 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. */ | |
a723baf1 MM |
1221 | bool constant_expression_p; |
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. */ | |
1227 | bool allow_non_constant_expression_p; | |
1228 | ||
1229 | /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has | |
1230 | been seen that makes the expression non-constant. */ | |
1231 | bool non_constant_expression_p; | |
1232 | ||
a723baf1 MM |
1233 | /* TRUE if local variable names and `this' are forbidden in the |
1234 | current context. */ | |
1235 | bool local_variables_forbidden_p; | |
1236 | ||
1237 | /* TRUE if the declaration we are parsing is part of a | |
1238 | linkage-specification of the form `extern string-literal | |
1239 | declaration'. */ | |
1240 | bool in_unbraced_linkage_specification_p; | |
1241 | ||
1242 | /* TRUE if we are presently parsing a declarator, after the | |
1243 | direct-declarator. */ | |
1244 | bool in_declarator_p; | |
1245 | ||
0e59b3fb MM |
1246 | /* TRUE if we are presently parsing the body of an |
1247 | iteration-statement. */ | |
1248 | bool in_iteration_statement_p; | |
1249 | ||
1250 | /* TRUE if we are presently parsing the body of a switch | |
1251 | statement. */ | |
1252 | bool in_switch_statement_p; | |
1253 | ||
a723baf1 MM |
1254 | /* If non-NULL, then we are parsing a construct where new type |
1255 | definitions are not permitted. The string stored here will be | |
1256 | issued as an error message if a type is defined. */ | |
1257 | const char *type_definition_forbidden_message; | |
1258 | ||
8db1028e NS |
1259 | /* A list of lists. The outer list is a stack, used for member |
1260 | functions of local classes. At each level there are two sub-list, | |
1261 | one on TREE_VALUE and one on TREE_PURPOSE. Each of those | |
1262 | sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their | |
1263 | TREE_VALUE's. The functions are chained in reverse declaration | |
1264 | order. | |
1265 | ||
1266 | The TREE_PURPOSE sublist contains those functions with default | |
1267 | arguments that need post processing, and the TREE_VALUE sublist | |
1268 | contains those functions with definitions that need post | |
1269 | processing. | |
1270 | ||
1271 | These lists can only be processed once the outermost class being | |
9bcb9aae | 1272 | defined is complete. */ |
a723baf1 MM |
1273 | tree unparsed_functions_queues; |
1274 | ||
1275 | /* The number of classes whose definitions are currently in | |
1276 | progress. */ | |
1277 | unsigned num_classes_being_defined; | |
1278 | ||
1279 | /* The number of template parameter lists that apply directly to the | |
1280 | current declaration. */ | |
1281 | unsigned num_template_parameter_lists; | |
1282 | } cp_parser; | |
1283 | ||
04c06002 | 1284 | /* The type of a function that parses some kind of expression. */ |
94edc4ab | 1285 | typedef tree (*cp_parser_expression_fn) (cp_parser *); |
a723baf1 MM |
1286 | |
1287 | /* Prototypes. */ | |
1288 | ||
1289 | /* Constructors and destructors. */ | |
1290 | ||
1291 | static cp_parser *cp_parser_new | |
94edc4ab | 1292 | (void); |
a723baf1 MM |
1293 | |
1294 | /* Routines to parse various constructs. | |
1295 | ||
1296 | Those that return `tree' will return the error_mark_node (rather | |
1297 | than NULL_TREE) if a parse error occurs, unless otherwise noted. | |
1298 | Sometimes, they will return an ordinary node if error-recovery was | |
34cd5ae7 | 1299 | attempted, even though a parse error occurred. So, to check |
a723baf1 MM |
1300 | whether or not a parse error occurred, you should always use |
1301 | cp_parser_error_occurred. If the construct is optional (indicated | |
1302 | either by an `_opt' in the name of the function that does the | |
1303 | parsing or via a FLAGS parameter), then NULL_TREE is returned if | |
1304 | the construct is not present. */ | |
1305 | ||
1306 | /* Lexical conventions [gram.lex] */ | |
1307 | ||
1308 | static tree cp_parser_identifier | |
94edc4ab | 1309 | (cp_parser *); |
a723baf1 MM |
1310 | |
1311 | /* Basic concepts [gram.basic] */ | |
1312 | ||
1313 | static bool cp_parser_translation_unit | |
94edc4ab | 1314 | (cp_parser *); |
a723baf1 MM |
1315 | |
1316 | /* Expressions [gram.expr] */ | |
1317 | ||
1318 | static tree cp_parser_primary_expression | |
b3445994 | 1319 | (cp_parser *, cp_id_kind *, tree *); |
a723baf1 | 1320 | static tree cp_parser_id_expression |
f3c2dfc6 | 1321 | (cp_parser *, bool, bool, bool *, bool); |
a723baf1 | 1322 | static tree cp_parser_unqualified_id |
f3c2dfc6 | 1323 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1324 | static tree cp_parser_nested_name_specifier_opt |
a668c6ad | 1325 | (cp_parser *, bool, bool, bool, bool); |
a723baf1 | 1326 | static tree cp_parser_nested_name_specifier |
a723baf1 | 1327 | (cp_parser *, bool, bool, bool, bool); |
a668c6ad MM |
1328 | static tree cp_parser_class_or_namespace_name |
1329 | (cp_parser *, bool, bool, bool, bool, bool); | |
a723baf1 MM |
1330 | static tree cp_parser_postfix_expression |
1331 | (cp_parser *, bool); | |
7efa3e22 | 1332 | static tree cp_parser_parenthesized_expression_list |
39703eb9 | 1333 | (cp_parser *, bool, bool *); |
a723baf1 | 1334 | static void cp_parser_pseudo_destructor_name |
94edc4ab | 1335 | (cp_parser *, tree *, tree *); |
a723baf1 MM |
1336 | static tree cp_parser_unary_expression |
1337 | (cp_parser *, bool); | |
1338 | static enum tree_code cp_parser_unary_operator | |
94edc4ab | 1339 | (cp_token *); |
a723baf1 | 1340 | static tree cp_parser_new_expression |
94edc4ab | 1341 | (cp_parser *); |
a723baf1 | 1342 | static tree cp_parser_new_placement |
94edc4ab | 1343 | (cp_parser *); |
a723baf1 | 1344 | static tree cp_parser_new_type_id |
94edc4ab | 1345 | (cp_parser *); |
a723baf1 | 1346 | static tree cp_parser_new_declarator_opt |
94edc4ab | 1347 | (cp_parser *); |
a723baf1 | 1348 | static tree cp_parser_direct_new_declarator |
94edc4ab | 1349 | (cp_parser *); |
a723baf1 | 1350 | static tree cp_parser_new_initializer |
94edc4ab | 1351 | (cp_parser *); |
a723baf1 | 1352 | static tree cp_parser_delete_expression |
94edc4ab | 1353 | (cp_parser *); |
a723baf1 MM |
1354 | static tree cp_parser_cast_expression |
1355 | (cp_parser *, bool); | |
1356 | static tree cp_parser_pm_expression | |
94edc4ab | 1357 | (cp_parser *); |
a723baf1 | 1358 | static tree cp_parser_multiplicative_expression |
94edc4ab | 1359 | (cp_parser *); |
a723baf1 | 1360 | static tree cp_parser_additive_expression |
94edc4ab | 1361 | (cp_parser *); |
a723baf1 | 1362 | static tree cp_parser_shift_expression |
94edc4ab | 1363 | (cp_parser *); |
a723baf1 | 1364 | static tree cp_parser_relational_expression |
94edc4ab | 1365 | (cp_parser *); |
a723baf1 | 1366 | static tree cp_parser_equality_expression |
94edc4ab | 1367 | (cp_parser *); |
a723baf1 | 1368 | static tree cp_parser_and_expression |
94edc4ab | 1369 | (cp_parser *); |
a723baf1 | 1370 | static tree cp_parser_exclusive_or_expression |
94edc4ab | 1371 | (cp_parser *); |
a723baf1 | 1372 | static tree cp_parser_inclusive_or_expression |
94edc4ab | 1373 | (cp_parser *); |
a723baf1 | 1374 | static tree cp_parser_logical_and_expression |
94edc4ab | 1375 | (cp_parser *); |
a723baf1 | 1376 | static tree cp_parser_logical_or_expression |
94edc4ab | 1377 | (cp_parser *); |
a723baf1 | 1378 | static tree cp_parser_question_colon_clause |
94edc4ab | 1379 | (cp_parser *, tree); |
a723baf1 | 1380 | static tree cp_parser_assignment_expression |
94edc4ab | 1381 | (cp_parser *); |
a723baf1 | 1382 | static enum tree_code cp_parser_assignment_operator_opt |
94edc4ab | 1383 | (cp_parser *); |
a723baf1 | 1384 | static tree cp_parser_expression |
94edc4ab | 1385 | (cp_parser *); |
a723baf1 | 1386 | static tree cp_parser_constant_expression |
14d22dd6 | 1387 | (cp_parser *, bool, bool *); |
a723baf1 MM |
1388 | |
1389 | /* Statements [gram.stmt.stmt] */ | |
1390 | ||
1391 | static void cp_parser_statement | |
a5bcc582 | 1392 | (cp_parser *, bool); |
a723baf1 | 1393 | static tree cp_parser_labeled_statement |
a5bcc582 | 1394 | (cp_parser *, bool); |
a723baf1 | 1395 | static tree cp_parser_expression_statement |
a5bcc582 | 1396 | (cp_parser *, bool); |
a723baf1 | 1397 | static tree cp_parser_compound_statement |
a5bcc582 | 1398 | (cp_parser *, bool); |
a723baf1 | 1399 | static void cp_parser_statement_seq_opt |
a5bcc582 | 1400 | (cp_parser *, bool); |
a723baf1 | 1401 | static tree cp_parser_selection_statement |
94edc4ab | 1402 | (cp_parser *); |
a723baf1 | 1403 | static tree cp_parser_condition |
94edc4ab | 1404 | (cp_parser *); |
a723baf1 | 1405 | static tree cp_parser_iteration_statement |
94edc4ab | 1406 | (cp_parser *); |
a723baf1 | 1407 | static void cp_parser_for_init_statement |
94edc4ab | 1408 | (cp_parser *); |
a723baf1 | 1409 | static tree cp_parser_jump_statement |
94edc4ab | 1410 | (cp_parser *); |
a723baf1 | 1411 | static void cp_parser_declaration_statement |
94edc4ab | 1412 | (cp_parser *); |
a723baf1 MM |
1413 | |
1414 | static tree cp_parser_implicitly_scoped_statement | |
94edc4ab | 1415 | (cp_parser *); |
a723baf1 | 1416 | static void cp_parser_already_scoped_statement |
94edc4ab | 1417 | (cp_parser *); |
a723baf1 MM |
1418 | |
1419 | /* Declarations [gram.dcl.dcl] */ | |
1420 | ||
1421 | static void cp_parser_declaration_seq_opt | |
94edc4ab | 1422 | (cp_parser *); |
a723baf1 | 1423 | static void cp_parser_declaration |
94edc4ab | 1424 | (cp_parser *); |
a723baf1 | 1425 | static void cp_parser_block_declaration |
94edc4ab | 1426 | (cp_parser *, bool); |
a723baf1 | 1427 | static void cp_parser_simple_declaration |
94edc4ab | 1428 | (cp_parser *, bool); |
a723baf1 | 1429 | static tree cp_parser_decl_specifier_seq |
560ad596 | 1430 | (cp_parser *, cp_parser_flags, tree *, int *); |
a723baf1 | 1431 | static tree cp_parser_storage_class_specifier_opt |
94edc4ab | 1432 | (cp_parser *); |
a723baf1 | 1433 | static tree cp_parser_function_specifier_opt |
94edc4ab | 1434 | (cp_parser *); |
a723baf1 | 1435 | static tree cp_parser_type_specifier |
560ad596 | 1436 | (cp_parser *, cp_parser_flags, bool, bool, int *, bool *); |
a723baf1 | 1437 | static tree cp_parser_simple_type_specifier |
4b0d3cbe | 1438 | (cp_parser *, cp_parser_flags, bool); |
a723baf1 | 1439 | static tree cp_parser_type_name |
94edc4ab | 1440 | (cp_parser *); |
a723baf1 | 1441 | static tree cp_parser_elaborated_type_specifier |
94edc4ab | 1442 | (cp_parser *, bool, bool); |
a723baf1 | 1443 | static tree cp_parser_enum_specifier |
94edc4ab | 1444 | (cp_parser *); |
a723baf1 | 1445 | static void cp_parser_enumerator_list |
94edc4ab | 1446 | (cp_parser *, tree); |
a723baf1 | 1447 | static void cp_parser_enumerator_definition |
94edc4ab | 1448 | (cp_parser *, tree); |
a723baf1 | 1449 | static tree cp_parser_namespace_name |
94edc4ab | 1450 | (cp_parser *); |
a723baf1 | 1451 | static void cp_parser_namespace_definition |
94edc4ab | 1452 | (cp_parser *); |
a723baf1 | 1453 | static void cp_parser_namespace_body |
94edc4ab | 1454 | (cp_parser *); |
a723baf1 | 1455 | static tree cp_parser_qualified_namespace_specifier |
94edc4ab | 1456 | (cp_parser *); |
a723baf1 | 1457 | static void cp_parser_namespace_alias_definition |
94edc4ab | 1458 | (cp_parser *); |
a723baf1 | 1459 | static void cp_parser_using_declaration |
94edc4ab | 1460 | (cp_parser *); |
a723baf1 | 1461 | static void cp_parser_using_directive |
94edc4ab | 1462 | (cp_parser *); |
a723baf1 | 1463 | static void cp_parser_asm_definition |
94edc4ab | 1464 | (cp_parser *); |
a723baf1 | 1465 | static void cp_parser_linkage_specification |
94edc4ab | 1466 | (cp_parser *); |
a723baf1 MM |
1467 | |
1468 | /* Declarators [gram.dcl.decl] */ | |
1469 | ||
1470 | static tree cp_parser_init_declarator | |
560ad596 | 1471 | (cp_parser *, tree, tree, bool, bool, int, bool *); |
a723baf1 | 1472 | static tree cp_parser_declarator |
7efa3e22 | 1473 | (cp_parser *, cp_parser_declarator_kind, int *); |
a723baf1 | 1474 | static tree cp_parser_direct_declarator |
7efa3e22 | 1475 | (cp_parser *, cp_parser_declarator_kind, int *); |
a723baf1 | 1476 | static enum tree_code cp_parser_ptr_operator |
94edc4ab | 1477 | (cp_parser *, tree *, tree *); |
a723baf1 | 1478 | static tree cp_parser_cv_qualifier_seq_opt |
94edc4ab | 1479 | (cp_parser *); |
a723baf1 | 1480 | static tree cp_parser_cv_qualifier_opt |
94edc4ab | 1481 | (cp_parser *); |
a723baf1 | 1482 | static tree cp_parser_declarator_id |
94edc4ab | 1483 | (cp_parser *); |
a723baf1 | 1484 | static tree cp_parser_type_id |
94edc4ab | 1485 | (cp_parser *); |
a723baf1 | 1486 | static tree cp_parser_type_specifier_seq |
94edc4ab | 1487 | (cp_parser *); |
a723baf1 | 1488 | static tree cp_parser_parameter_declaration_clause |
94edc4ab | 1489 | (cp_parser *); |
a723baf1 | 1490 | static tree cp_parser_parameter_declaration_list |
94edc4ab | 1491 | (cp_parser *); |
a723baf1 | 1492 | static tree cp_parser_parameter_declaration |
94edc4ab | 1493 | (cp_parser *, bool); |
a723baf1 | 1494 | static tree cp_parser_function_definition |
94edc4ab | 1495 | (cp_parser *, bool *); |
a723baf1 MM |
1496 | static void cp_parser_function_body |
1497 | (cp_parser *); | |
1498 | static tree cp_parser_initializer | |
39703eb9 | 1499 | (cp_parser *, bool *, bool *); |
a723baf1 | 1500 | static tree cp_parser_initializer_clause |
39703eb9 | 1501 | (cp_parser *, bool *); |
a723baf1 | 1502 | static tree cp_parser_initializer_list |
39703eb9 | 1503 | (cp_parser *, bool *); |
a723baf1 MM |
1504 | |
1505 | static bool cp_parser_ctor_initializer_opt_and_function_body | |
1506 | (cp_parser *); | |
1507 | ||
1508 | /* Classes [gram.class] */ | |
1509 | ||
1510 | static tree cp_parser_class_name | |
a668c6ad | 1511 | (cp_parser *, bool, bool, bool, bool, bool, bool); |
a723baf1 | 1512 | static tree cp_parser_class_specifier |
94edc4ab | 1513 | (cp_parser *); |
a723baf1 | 1514 | static tree cp_parser_class_head |
94edc4ab | 1515 | (cp_parser *, bool *); |
a723baf1 | 1516 | static enum tag_types cp_parser_class_key |
94edc4ab | 1517 | (cp_parser *); |
a723baf1 | 1518 | static void cp_parser_member_specification_opt |
94edc4ab | 1519 | (cp_parser *); |
a723baf1 | 1520 | static void cp_parser_member_declaration |
94edc4ab | 1521 | (cp_parser *); |
a723baf1 | 1522 | static tree cp_parser_pure_specifier |
94edc4ab | 1523 | (cp_parser *); |
a723baf1 | 1524 | static tree cp_parser_constant_initializer |
94edc4ab | 1525 | (cp_parser *); |
a723baf1 MM |
1526 | |
1527 | /* Derived classes [gram.class.derived] */ | |
1528 | ||
1529 | static tree cp_parser_base_clause | |
94edc4ab | 1530 | (cp_parser *); |
a723baf1 | 1531 | static tree cp_parser_base_specifier |
94edc4ab | 1532 | (cp_parser *); |
a723baf1 MM |
1533 | |
1534 | /* Special member functions [gram.special] */ | |
1535 | ||
1536 | static tree cp_parser_conversion_function_id | |
94edc4ab | 1537 | (cp_parser *); |
a723baf1 | 1538 | static tree cp_parser_conversion_type_id |
94edc4ab | 1539 | (cp_parser *); |
a723baf1 | 1540 | static tree cp_parser_conversion_declarator_opt |
94edc4ab | 1541 | (cp_parser *); |
a723baf1 | 1542 | static bool cp_parser_ctor_initializer_opt |
94edc4ab | 1543 | (cp_parser *); |
a723baf1 | 1544 | static void cp_parser_mem_initializer_list |
94edc4ab | 1545 | (cp_parser *); |
a723baf1 | 1546 | static tree cp_parser_mem_initializer |
94edc4ab | 1547 | (cp_parser *); |
a723baf1 | 1548 | static tree cp_parser_mem_initializer_id |
94edc4ab | 1549 | (cp_parser *); |
a723baf1 MM |
1550 | |
1551 | /* Overloading [gram.over] */ | |
1552 | ||
1553 | static tree cp_parser_operator_function_id | |
94edc4ab | 1554 | (cp_parser *); |
a723baf1 | 1555 | static tree cp_parser_operator |
94edc4ab | 1556 | (cp_parser *); |
a723baf1 MM |
1557 | |
1558 | /* Templates [gram.temp] */ | |
1559 | ||
1560 | static void cp_parser_template_declaration | |
94edc4ab | 1561 | (cp_parser *, bool); |
a723baf1 | 1562 | static tree cp_parser_template_parameter_list |
94edc4ab | 1563 | (cp_parser *); |
a723baf1 | 1564 | static tree cp_parser_template_parameter |
94edc4ab | 1565 | (cp_parser *); |
a723baf1 | 1566 | static tree cp_parser_type_parameter |
94edc4ab | 1567 | (cp_parser *); |
a723baf1 | 1568 | static tree cp_parser_template_id |
a668c6ad | 1569 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1570 | static tree cp_parser_template_name |
a668c6ad | 1571 | (cp_parser *, bool, bool, bool, bool *); |
a723baf1 | 1572 | static tree cp_parser_template_argument_list |
94edc4ab | 1573 | (cp_parser *); |
a723baf1 | 1574 | static tree cp_parser_template_argument |
94edc4ab | 1575 | (cp_parser *); |
a723baf1 | 1576 | static void cp_parser_explicit_instantiation |
94edc4ab | 1577 | (cp_parser *); |
a723baf1 | 1578 | static void cp_parser_explicit_specialization |
94edc4ab | 1579 | (cp_parser *); |
a723baf1 MM |
1580 | |
1581 | /* Exception handling [gram.exception] */ | |
1582 | ||
1583 | static tree cp_parser_try_block | |
94edc4ab | 1584 | (cp_parser *); |
a723baf1 | 1585 | static bool cp_parser_function_try_block |
94edc4ab | 1586 | (cp_parser *); |
a723baf1 | 1587 | static void cp_parser_handler_seq |
94edc4ab | 1588 | (cp_parser *); |
a723baf1 | 1589 | static void cp_parser_handler |
94edc4ab | 1590 | (cp_parser *); |
a723baf1 | 1591 | static tree cp_parser_exception_declaration |
94edc4ab | 1592 | (cp_parser *); |
a723baf1 | 1593 | static tree cp_parser_throw_expression |
94edc4ab | 1594 | (cp_parser *); |
a723baf1 | 1595 | static tree cp_parser_exception_specification_opt |
94edc4ab | 1596 | (cp_parser *); |
a723baf1 | 1597 | static tree cp_parser_type_id_list |
94edc4ab | 1598 | (cp_parser *); |
a723baf1 MM |
1599 | |
1600 | /* GNU Extensions */ | |
1601 | ||
1602 | static tree cp_parser_asm_specification_opt | |
94edc4ab | 1603 | (cp_parser *); |
a723baf1 | 1604 | static tree cp_parser_asm_operand_list |
94edc4ab | 1605 | (cp_parser *); |
a723baf1 | 1606 | static tree cp_parser_asm_clobber_list |
94edc4ab | 1607 | (cp_parser *); |
a723baf1 | 1608 | static tree cp_parser_attributes_opt |
94edc4ab | 1609 | (cp_parser *); |
a723baf1 | 1610 | static tree cp_parser_attribute_list |
94edc4ab | 1611 | (cp_parser *); |
a723baf1 | 1612 | static bool cp_parser_extension_opt |
94edc4ab | 1613 | (cp_parser *, int *); |
a723baf1 | 1614 | static void cp_parser_label_declaration |
94edc4ab | 1615 | (cp_parser *); |
a723baf1 MM |
1616 | |
1617 | /* Utility Routines */ | |
1618 | ||
1619 | static tree cp_parser_lookup_name | |
8d241e0b | 1620 | (cp_parser *, tree, bool, bool, bool); |
a723baf1 | 1621 | static tree cp_parser_lookup_name_simple |
94edc4ab | 1622 | (cp_parser *, tree); |
a723baf1 MM |
1623 | static tree cp_parser_maybe_treat_template_as_class |
1624 | (tree, bool); | |
1625 | static bool cp_parser_check_declarator_template_parameters | |
94edc4ab | 1626 | (cp_parser *, tree); |
a723baf1 | 1627 | static bool cp_parser_check_template_parameters |
94edc4ab | 1628 | (cp_parser *, unsigned); |
d6b4ea85 MM |
1629 | static tree cp_parser_simple_cast_expression |
1630 | (cp_parser *); | |
a723baf1 | 1631 | static tree cp_parser_binary_expression |
94edc4ab | 1632 | (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn); |
a723baf1 | 1633 | static tree cp_parser_global_scope_opt |
94edc4ab | 1634 | (cp_parser *, bool); |
a723baf1 MM |
1635 | static bool cp_parser_constructor_declarator_p |
1636 | (cp_parser *, bool); | |
1637 | static tree cp_parser_function_definition_from_specifiers_and_declarator | |
94edc4ab | 1638 | (cp_parser *, tree, tree, tree); |
a723baf1 | 1639 | static tree cp_parser_function_definition_after_declarator |
94edc4ab | 1640 | (cp_parser *, bool); |
a723baf1 | 1641 | static void cp_parser_template_declaration_after_export |
94edc4ab | 1642 | (cp_parser *, bool); |
a723baf1 | 1643 | static tree cp_parser_single_declaration |
94edc4ab | 1644 | (cp_parser *, bool, bool *); |
a723baf1 | 1645 | static tree cp_parser_functional_cast |
94edc4ab | 1646 | (cp_parser *, tree); |
ec75414f MM |
1647 | static tree cp_parser_enclosed_template_argument_list |
1648 | (cp_parser *); | |
8db1028e NS |
1649 | static void cp_parser_save_default_args |
1650 | (cp_parser *, tree); | |
a723baf1 | 1651 | static void cp_parser_late_parsing_for_member |
94edc4ab | 1652 | (cp_parser *, tree); |
a723baf1 | 1653 | static void cp_parser_late_parsing_default_args |
8218bd34 | 1654 | (cp_parser *, tree); |
a723baf1 | 1655 | static tree cp_parser_sizeof_operand |
94edc4ab | 1656 | (cp_parser *, enum rid); |
a723baf1 | 1657 | static bool cp_parser_declares_only_class_p |
94edc4ab | 1658 | (cp_parser *); |
d17811fd MM |
1659 | static tree cp_parser_fold_non_dependent_expr |
1660 | (tree); | |
a723baf1 | 1661 | static bool cp_parser_friend_p |
94edc4ab | 1662 | (tree); |
a723baf1 | 1663 | static cp_token *cp_parser_require |
94edc4ab | 1664 | (cp_parser *, enum cpp_ttype, const char *); |
a723baf1 | 1665 | static cp_token *cp_parser_require_keyword |
94edc4ab | 1666 | (cp_parser *, enum rid, const char *); |
a723baf1 | 1667 | static bool cp_parser_token_starts_function_definition_p |
94edc4ab | 1668 | (cp_token *); |
a723baf1 MM |
1669 | static bool cp_parser_next_token_starts_class_definition_p |
1670 | (cp_parser *); | |
d17811fd MM |
1671 | static bool cp_parser_next_token_ends_template_argument_p |
1672 | (cp_parser *); | |
a723baf1 | 1673 | static enum tag_types cp_parser_token_is_class_key |
94edc4ab | 1674 | (cp_token *); |
a723baf1 MM |
1675 | static void cp_parser_check_class_key |
1676 | (enum tag_types, tree type); | |
37d407a1 KL |
1677 | static void cp_parser_check_access_in_redeclaration |
1678 | (tree type); | |
a723baf1 MM |
1679 | static bool cp_parser_optional_template_keyword |
1680 | (cp_parser *); | |
2050a1bb MM |
1681 | static void cp_parser_pre_parsed_nested_name_specifier |
1682 | (cp_parser *); | |
a723baf1 MM |
1683 | static void cp_parser_cache_group |
1684 | (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned); | |
1685 | static void cp_parser_parse_tentatively | |
94edc4ab | 1686 | (cp_parser *); |
a723baf1 | 1687 | static void cp_parser_commit_to_tentative_parse |
94edc4ab | 1688 | (cp_parser *); |
a723baf1 | 1689 | static void cp_parser_abort_tentative_parse |
94edc4ab | 1690 | (cp_parser *); |
a723baf1 | 1691 | static bool cp_parser_parse_definitely |
94edc4ab | 1692 | (cp_parser *); |
f7b5ecd9 | 1693 | static inline bool cp_parser_parsing_tentatively |
94edc4ab | 1694 | (cp_parser *); |
a723baf1 | 1695 | static bool cp_parser_committed_to_tentative_parse |
94edc4ab | 1696 | (cp_parser *); |
a723baf1 | 1697 | static void cp_parser_error |
94edc4ab | 1698 | (cp_parser *, const char *); |
e5976695 | 1699 | static bool cp_parser_simulate_error |
94edc4ab | 1700 | (cp_parser *); |
a723baf1 | 1701 | static void cp_parser_check_type_definition |
94edc4ab | 1702 | (cp_parser *); |
560ad596 MM |
1703 | static void cp_parser_check_for_definition_in_return_type |
1704 | (tree, int); | |
14d22dd6 MM |
1705 | static tree cp_parser_non_constant_expression |
1706 | (const char *); | |
8fbc5ae7 MM |
1707 | static bool cp_parser_diagnose_invalid_type_name |
1708 | (cp_parser *); | |
7efa3e22 | 1709 | static int cp_parser_skip_to_closing_parenthesis |
a668c6ad | 1710 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1711 | static void cp_parser_skip_to_end_of_statement |
94edc4ab | 1712 | (cp_parser *); |
e0860732 MM |
1713 | static void cp_parser_consume_semicolon_at_end_of_statement |
1714 | (cp_parser *); | |
a723baf1 | 1715 | static void cp_parser_skip_to_end_of_block_or_statement |
94edc4ab | 1716 | (cp_parser *); |
a723baf1 MM |
1717 | static void cp_parser_skip_to_closing_brace |
1718 | (cp_parser *); | |
1719 | static void cp_parser_skip_until_found | |
94edc4ab | 1720 | (cp_parser *, enum cpp_ttype, const char *); |
a723baf1 | 1721 | static bool cp_parser_error_occurred |
94edc4ab | 1722 | (cp_parser *); |
a723baf1 | 1723 | static bool cp_parser_allow_gnu_extensions_p |
94edc4ab | 1724 | (cp_parser *); |
a723baf1 | 1725 | static bool cp_parser_is_string_literal |
94edc4ab | 1726 | (cp_token *); |
a723baf1 | 1727 | static bool cp_parser_is_keyword |
94edc4ab | 1728 | (cp_token *, enum rid); |
a723baf1 | 1729 | |
4de8668e | 1730 | /* Returns nonzero if we are parsing tentatively. */ |
f7b5ecd9 MM |
1731 | |
1732 | static inline bool | |
94edc4ab | 1733 | cp_parser_parsing_tentatively (cp_parser* parser) |
f7b5ecd9 MM |
1734 | { |
1735 | return parser->context->next != NULL; | |
1736 | } | |
1737 | ||
4de8668e | 1738 | /* Returns nonzero if TOKEN is a string literal. */ |
a723baf1 MM |
1739 | |
1740 | static bool | |
94edc4ab | 1741 | cp_parser_is_string_literal (cp_token* token) |
a723baf1 MM |
1742 | { |
1743 | return (token->type == CPP_STRING || token->type == CPP_WSTRING); | |
1744 | } | |
1745 | ||
4de8668e | 1746 | /* Returns nonzero if TOKEN is the indicated KEYWORD. */ |
a723baf1 MM |
1747 | |
1748 | static bool | |
94edc4ab | 1749 | cp_parser_is_keyword (cp_token* token, enum rid keyword) |
a723baf1 MM |
1750 | { |
1751 | return token->keyword == keyword; | |
1752 | } | |
1753 | ||
a723baf1 MM |
1754 | /* Issue the indicated error MESSAGE. */ |
1755 | ||
1756 | static void | |
94edc4ab | 1757 | cp_parser_error (cp_parser* parser, const char* message) |
a723baf1 | 1758 | { |
a723baf1 | 1759 | /* Output the MESSAGE -- unless we're parsing tentatively. */ |
e5976695 | 1760 | if (!cp_parser_simulate_error (parser)) |
a723baf1 MM |
1761 | error (message); |
1762 | } | |
1763 | ||
1764 | /* If we are parsing tentatively, remember that an error has occurred | |
e5976695 MM |
1765 | during this tentative parse. Returns true if the error was |
1766 | simulated; false if a messgae should be issued by the caller. */ | |
a723baf1 | 1767 | |
e5976695 | 1768 | static bool |
94edc4ab | 1769 | cp_parser_simulate_error (cp_parser* parser) |
a723baf1 MM |
1770 | { |
1771 | if (cp_parser_parsing_tentatively (parser) | |
1772 | && !cp_parser_committed_to_tentative_parse (parser)) | |
e5976695 MM |
1773 | { |
1774 | parser->context->status = CP_PARSER_STATUS_KIND_ERROR; | |
1775 | return true; | |
1776 | } | |
1777 | return false; | |
a723baf1 MM |
1778 | } |
1779 | ||
1780 | /* This function is called when a type is defined. If type | |
1781 | definitions are forbidden at this point, an error message is | |
1782 | issued. */ | |
1783 | ||
1784 | static void | |
94edc4ab | 1785 | cp_parser_check_type_definition (cp_parser* parser) |
a723baf1 MM |
1786 | { |
1787 | /* If types are forbidden here, issue a message. */ | |
1788 | if (parser->type_definition_forbidden_message) | |
1789 | /* Use `%s' to print the string in case there are any escape | |
1790 | characters in the message. */ | |
1791 | error ("%s", parser->type_definition_forbidden_message); | |
1792 | } | |
1793 | ||
560ad596 MM |
1794 | /* This function is called when a declaration is parsed. If |
1795 | DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM | |
1796 | indicates that a type was defined in the decl-specifiers for DECL, | |
1797 | then an error is issued. */ | |
1798 | ||
1799 | static void | |
1800 | cp_parser_check_for_definition_in_return_type (tree declarator, | |
1801 | int declares_class_or_enum) | |
1802 | { | |
1803 | /* [dcl.fct] forbids type definitions in return types. | |
1804 | Unfortunately, it's not easy to know whether or not we are | |
1805 | processing a return type until after the fact. */ | |
1806 | while (declarator | |
1807 | && (TREE_CODE (declarator) == INDIRECT_REF | |
1808 | || TREE_CODE (declarator) == ADDR_EXPR)) | |
1809 | declarator = TREE_OPERAND (declarator, 0); | |
1810 | if (declarator | |
1811 | && TREE_CODE (declarator) == CALL_EXPR | |
1812 | && declares_class_or_enum & 2) | |
1813 | error ("new types may not be defined in a return type"); | |
1814 | } | |
1815 | ||
cd0be382 | 1816 | /* Issue an error message about the fact that THING appeared in a |
14d22dd6 MM |
1817 | constant-expression. Returns ERROR_MARK_NODE. */ |
1818 | ||
1819 | static tree | |
1820 | cp_parser_non_constant_expression (const char *thing) | |
1821 | { | |
1822 | error ("%s cannot appear in a constant-expression", thing); | |
1823 | return error_mark_node; | |
1824 | } | |
1825 | ||
8fbc5ae7 MM |
1826 | /* Check for a common situation where a type-name should be present, |
1827 | but is not, and issue a sensible error message. Returns true if an | |
1828 | invalid type-name was detected. */ | |
1829 | ||
1830 | static bool | |
1831 | cp_parser_diagnose_invalid_type_name (cp_parser *parser) | |
1832 | { | |
1833 | /* If the next two tokens are both identifiers, the code is | |
1834 | erroneous. The usual cause of this situation is code like: | |
1835 | ||
1836 | T t; | |
1837 | ||
1838 | where "T" should name a type -- but does not. */ | |
1839 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
1840 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME) | |
1841 | { | |
1842 | tree name; | |
1843 | ||
8d241e0b | 1844 | /* If parsing tentatively, we should commit; we really are |
8fbc5ae7 MM |
1845 | looking at a declaration. */ |
1846 | /* Consume the first identifier. */ | |
1847 | name = cp_lexer_consume_token (parser->lexer)->value; | |
1848 | /* Issue an error message. */ | |
1849 | error ("`%s' does not name a type", IDENTIFIER_POINTER (name)); | |
1850 | /* If we're in a template class, it's possible that the user was | |
1851 | referring to a type from a base class. For example: | |
1852 | ||
1853 | template <typename T> struct A { typedef T X; }; | |
1854 | template <typename T> struct B : public A<T> { X x; }; | |
1855 | ||
1856 | The user should have said "typename A<T>::X". */ | |
1857 | if (processing_template_decl && current_class_type) | |
1858 | { | |
1859 | tree b; | |
1860 | ||
1861 | for (b = TREE_CHAIN (TYPE_BINFO (current_class_type)); | |
1862 | b; | |
1863 | b = TREE_CHAIN (b)) | |
1864 | { | |
1865 | tree base_type = BINFO_TYPE (b); | |
1866 | if (CLASS_TYPE_P (base_type) | |
1fb3244a | 1867 | && dependent_type_p (base_type)) |
8fbc5ae7 MM |
1868 | { |
1869 | tree field; | |
1870 | /* Go from a particular instantiation of the | |
1871 | template (which will have an empty TYPE_FIELDs), | |
1872 | to the main version. */ | |
353b4fc0 | 1873 | base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type); |
8fbc5ae7 MM |
1874 | for (field = TYPE_FIELDS (base_type); |
1875 | field; | |
1876 | field = TREE_CHAIN (field)) | |
1877 | if (TREE_CODE (field) == TYPE_DECL | |
1878 | && DECL_NAME (field) == name) | |
1879 | { | |
1880 | error ("(perhaps `typename %T::%s' was intended)", | |
1881 | BINFO_TYPE (b), IDENTIFIER_POINTER (name)); | |
1882 | break; | |
1883 | } | |
1884 | if (field) | |
1885 | break; | |
1886 | } | |
1887 | } | |
1888 | } | |
1889 | /* Skip to the end of the declaration; there's no point in | |
1890 | trying to process it. */ | |
1891 | cp_parser_skip_to_end_of_statement (parser); | |
1892 | ||
1893 | return true; | |
1894 | } | |
1895 | ||
1896 | return false; | |
1897 | } | |
1898 | ||
a723baf1 | 1899 | /* Consume tokens up to, and including, the next non-nested closing `)'. |
7efa3e22 NS |
1900 | Returns 1 iff we found a closing `)'. RECOVERING is true, if we |
1901 | are doing error recovery. Returns -1 if OR_COMMA is true and we | |
1902 | found an unnested comma. */ | |
a723baf1 | 1903 | |
7efa3e22 NS |
1904 | static int |
1905 | cp_parser_skip_to_closing_parenthesis (cp_parser *parser, | |
a668c6ad MM |
1906 | bool recovering, |
1907 | bool or_comma, | |
1908 | bool consume_paren) | |
a723baf1 | 1909 | { |
7efa3e22 NS |
1910 | unsigned paren_depth = 0; |
1911 | unsigned brace_depth = 0; | |
a723baf1 | 1912 | |
7efa3e22 NS |
1913 | if (recovering && !or_comma && cp_parser_parsing_tentatively (parser) |
1914 | && !cp_parser_committed_to_tentative_parse (parser)) | |
1915 | return 0; | |
1916 | ||
a723baf1 MM |
1917 | while (true) |
1918 | { | |
1919 | cp_token *token; | |
7efa3e22 | 1920 | |
a723baf1 MM |
1921 | /* If we've run out of tokens, then there is no closing `)'. */ |
1922 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
7efa3e22 | 1923 | return 0; |
a723baf1 | 1924 | |
a668c6ad MM |
1925 | token = cp_lexer_peek_token (parser->lexer); |
1926 | ||
1927 | /* This matches the processing in skip_to_end_of_statement */ | |
1928 | if (token->type == CPP_SEMICOLON && !brace_depth) | |
1929 | return 0; | |
1930 | if (token->type == CPP_OPEN_BRACE) | |
1931 | ++brace_depth; | |
1932 | if (token->type == CPP_CLOSE_BRACE) | |
7efa3e22 | 1933 | { |
a668c6ad | 1934 | if (!brace_depth--) |
7efa3e22 | 1935 | return 0; |
7efa3e22 | 1936 | } |
a668c6ad MM |
1937 | if (recovering && or_comma && token->type == CPP_COMMA |
1938 | && !brace_depth && !paren_depth) | |
1939 | return -1; | |
7efa3e22 | 1940 | |
7efa3e22 NS |
1941 | if (!brace_depth) |
1942 | { | |
1943 | /* If it is an `(', we have entered another level of nesting. */ | |
1944 | if (token->type == CPP_OPEN_PAREN) | |
1945 | ++paren_depth; | |
1946 | /* If it is a `)', then we might be done. */ | |
1947 | else if (token->type == CPP_CLOSE_PAREN && !paren_depth--) | |
a668c6ad MM |
1948 | { |
1949 | if (consume_paren) | |
1950 | cp_lexer_consume_token (parser->lexer); | |
1951 | return 1; | |
1952 | } | |
7efa3e22 | 1953 | } |
a668c6ad MM |
1954 | |
1955 | /* Consume the token. */ | |
1956 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 MM |
1957 | } |
1958 | } | |
1959 | ||
1960 | /* Consume tokens until we reach the end of the current statement. | |
1961 | Normally, that will be just before consuming a `;'. However, if a | |
1962 | non-nested `}' comes first, then we stop before consuming that. */ | |
1963 | ||
1964 | static void | |
94edc4ab | 1965 | cp_parser_skip_to_end_of_statement (cp_parser* parser) |
a723baf1 MM |
1966 | { |
1967 | unsigned nesting_depth = 0; | |
1968 | ||
1969 | while (true) | |
1970 | { | |
1971 | cp_token *token; | |
1972 | ||
1973 | /* Peek at the next token. */ | |
1974 | token = cp_lexer_peek_token (parser->lexer); | |
1975 | /* If we've run out of tokens, stop. */ | |
1976 | if (token->type == CPP_EOF) | |
1977 | break; | |
1978 | /* If the next token is a `;', we have reached the end of the | |
1979 | statement. */ | |
1980 | if (token->type == CPP_SEMICOLON && !nesting_depth) | |
1981 | break; | |
1982 | /* If the next token is a non-nested `}', then we have reached | |
1983 | the end of the current block. */ | |
1984 | if (token->type == CPP_CLOSE_BRACE) | |
1985 | { | |
1986 | /* If this is a non-nested `}', stop before consuming it. | |
1987 | That way, when confronted with something like: | |
1988 | ||
1989 | { 3 + } | |
1990 | ||
1991 | we stop before consuming the closing `}', even though we | |
1992 | have not yet reached a `;'. */ | |
1993 | if (nesting_depth == 0) | |
1994 | break; | |
1995 | /* If it is the closing `}' for a block that we have | |
1996 | scanned, stop -- but only after consuming the token. | |
1997 | That way given: | |
1998 | ||
1999 | void f g () { ... } | |
2000 | typedef int I; | |
2001 | ||
2002 | we will stop after the body of the erroneously declared | |
2003 | function, but before consuming the following `typedef' | |
2004 | declaration. */ | |
2005 | if (--nesting_depth == 0) | |
2006 | { | |
2007 | cp_lexer_consume_token (parser->lexer); | |
2008 | break; | |
2009 | } | |
2010 | } | |
2011 | /* If it the next token is a `{', then we are entering a new | |
2012 | block. Consume the entire block. */ | |
2013 | else if (token->type == CPP_OPEN_BRACE) | |
2014 | ++nesting_depth; | |
2015 | /* Consume the token. */ | |
2016 | cp_lexer_consume_token (parser->lexer); | |
2017 | } | |
2018 | } | |
2019 | ||
e0860732 MM |
2020 | /* This function is called at the end of a statement or declaration. |
2021 | If the next token is a semicolon, it is consumed; otherwise, error | |
2022 | recovery is attempted. */ | |
2023 | ||
2024 | static void | |
2025 | cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser) | |
2026 | { | |
2027 | /* Look for the trailing `;'. */ | |
2028 | if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")) | |
2029 | { | |
2030 | /* If there is additional (erroneous) input, skip to the end of | |
2031 | the statement. */ | |
2032 | cp_parser_skip_to_end_of_statement (parser); | |
2033 | /* If the next token is now a `;', consume it. */ | |
2034 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
2035 | cp_lexer_consume_token (parser->lexer); | |
2036 | } | |
2037 | } | |
2038 | ||
a723baf1 MM |
2039 | /* Skip tokens until we have consumed an entire block, or until we |
2040 | have consumed a non-nested `;'. */ | |
2041 | ||
2042 | static void | |
94edc4ab | 2043 | cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser) |
a723baf1 MM |
2044 | { |
2045 | unsigned nesting_depth = 0; | |
2046 | ||
2047 | while (true) | |
2048 | { | |
2049 | cp_token *token; | |
2050 | ||
2051 | /* Peek at the next token. */ | |
2052 | token = cp_lexer_peek_token (parser->lexer); | |
2053 | /* If we've run out of tokens, stop. */ | |
2054 | if (token->type == CPP_EOF) | |
2055 | break; | |
2056 | /* If the next token is a `;', we have reached the end of the | |
2057 | statement. */ | |
2058 | if (token->type == CPP_SEMICOLON && !nesting_depth) | |
2059 | { | |
2060 | /* Consume the `;'. */ | |
2061 | cp_lexer_consume_token (parser->lexer); | |
2062 | break; | |
2063 | } | |
2064 | /* Consume the token. */ | |
2065 | token = cp_lexer_consume_token (parser->lexer); | |
2066 | /* If the next token is a non-nested `}', then we have reached | |
2067 | the end of the current block. */ | |
2068 | if (token->type == CPP_CLOSE_BRACE | |
2069 | && (nesting_depth == 0 || --nesting_depth == 0)) | |
2070 | break; | |
2071 | /* If it the next token is a `{', then we are entering a new | |
2072 | block. Consume the entire block. */ | |
2073 | if (token->type == CPP_OPEN_BRACE) | |
2074 | ++nesting_depth; | |
2075 | } | |
2076 | } | |
2077 | ||
2078 | /* Skip tokens until a non-nested closing curly brace is the next | |
2079 | token. */ | |
2080 | ||
2081 | static void | |
2082 | cp_parser_skip_to_closing_brace (cp_parser *parser) | |
2083 | { | |
2084 | unsigned nesting_depth = 0; | |
2085 | ||
2086 | while (true) | |
2087 | { | |
2088 | cp_token *token; | |
2089 | ||
2090 | /* Peek at the next token. */ | |
2091 | token = cp_lexer_peek_token (parser->lexer); | |
2092 | /* If we've run out of tokens, stop. */ | |
2093 | if (token->type == CPP_EOF) | |
2094 | break; | |
2095 | /* If the next token is a non-nested `}', then we have reached | |
2096 | the end of the current block. */ | |
2097 | if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0) | |
2098 | break; | |
2099 | /* If it the next token is a `{', then we are entering a new | |
2100 | block. Consume the entire block. */ | |
2101 | else if (token->type == CPP_OPEN_BRACE) | |
2102 | ++nesting_depth; | |
2103 | /* Consume the token. */ | |
2104 | cp_lexer_consume_token (parser->lexer); | |
2105 | } | |
2106 | } | |
2107 | ||
2108 | /* Create a new C++ parser. */ | |
2109 | ||
2110 | static cp_parser * | |
94edc4ab | 2111 | cp_parser_new (void) |
a723baf1 MM |
2112 | { |
2113 | cp_parser *parser; | |
17211ab5 GK |
2114 | cp_lexer *lexer; |
2115 | ||
2116 | /* cp_lexer_new_main is called before calling ggc_alloc because | |
2117 | cp_lexer_new_main might load a PCH file. */ | |
2118 | lexer = cp_lexer_new_main (); | |
a723baf1 | 2119 | |
c68b0a84 | 2120 | parser = ggc_alloc_cleared (sizeof (cp_parser)); |
17211ab5 | 2121 | parser->lexer = lexer; |
a723baf1 MM |
2122 | parser->context = cp_parser_context_new (NULL); |
2123 | ||
2124 | /* For now, we always accept GNU extensions. */ | |
2125 | parser->allow_gnu_extensions_p = 1; | |
2126 | ||
2127 | /* The `>' token is a greater-than operator, not the end of a | |
2128 | template-id. */ | |
2129 | parser->greater_than_is_operator_p = true; | |
2130 | ||
2131 | parser->default_arg_ok_p = true; | |
2132 | ||
2133 | /* We are not parsing a constant-expression. */ | |
2134 | parser->constant_expression_p = false; | |
14d22dd6 MM |
2135 | parser->allow_non_constant_expression_p = false; |
2136 | parser->non_constant_expression_p = false; | |
a723baf1 MM |
2137 | |
2138 | /* Local variable names are not forbidden. */ | |
2139 | parser->local_variables_forbidden_p = false; | |
2140 | ||
34cd5ae7 | 2141 | /* We are not processing an `extern "C"' declaration. */ |
a723baf1 MM |
2142 | parser->in_unbraced_linkage_specification_p = false; |
2143 | ||
2144 | /* We are not processing a declarator. */ | |
2145 | parser->in_declarator_p = false; | |
2146 | ||
0e59b3fb MM |
2147 | /* We are not in an iteration statement. */ |
2148 | parser->in_iteration_statement_p = false; | |
2149 | ||
2150 | /* We are not in a switch statement. */ | |
2151 | parser->in_switch_statement_p = false; | |
2152 | ||
a723baf1 MM |
2153 | /* The unparsed function queue is empty. */ |
2154 | parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE); | |
2155 | ||
2156 | /* There are no classes being defined. */ | |
2157 | parser->num_classes_being_defined = 0; | |
2158 | ||
2159 | /* No template parameters apply. */ | |
2160 | parser->num_template_parameter_lists = 0; | |
2161 | ||
2162 | return parser; | |
2163 | } | |
2164 | ||
2165 | /* Lexical conventions [gram.lex] */ | |
2166 | ||
2167 | /* Parse an identifier. Returns an IDENTIFIER_NODE representing the | |
2168 | identifier. */ | |
2169 | ||
2170 | static tree | |
94edc4ab | 2171 | cp_parser_identifier (cp_parser* parser) |
a723baf1 MM |
2172 | { |
2173 | cp_token *token; | |
2174 | ||
2175 | /* Look for the identifier. */ | |
2176 | token = cp_parser_require (parser, CPP_NAME, "identifier"); | |
2177 | /* Return the value. */ | |
2178 | return token ? token->value : error_mark_node; | |
2179 | } | |
2180 | ||
2181 | /* Basic concepts [gram.basic] */ | |
2182 | ||
2183 | /* Parse a translation-unit. | |
2184 | ||
2185 | translation-unit: | |
2186 | declaration-seq [opt] | |
2187 | ||
2188 | Returns TRUE if all went well. */ | |
2189 | ||
2190 | static bool | |
94edc4ab | 2191 | cp_parser_translation_unit (cp_parser* parser) |
a723baf1 MM |
2192 | { |
2193 | while (true) | |
2194 | { | |
2195 | cp_parser_declaration_seq_opt (parser); | |
2196 | ||
2197 | /* If there are no tokens left then all went well. */ | |
2198 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
2199 | break; | |
2200 | ||
2201 | /* Otherwise, issue an error message. */ | |
2202 | cp_parser_error (parser, "expected declaration"); | |
2203 | return false; | |
2204 | } | |
2205 | ||
2206 | /* Consume the EOF token. */ | |
2207 | cp_parser_require (parser, CPP_EOF, "end-of-file"); | |
2208 | ||
2209 | /* Finish up. */ | |
2210 | finish_translation_unit (); | |
2211 | ||
2212 | /* All went well. */ | |
2213 | return true; | |
2214 | } | |
2215 | ||
2216 | /* Expressions [gram.expr] */ | |
2217 | ||
2218 | /* Parse a primary-expression. | |
2219 | ||
2220 | primary-expression: | |
2221 | literal | |
2222 | this | |
2223 | ( expression ) | |
2224 | id-expression | |
2225 | ||
2226 | GNU Extensions: | |
2227 | ||
2228 | primary-expression: | |
2229 | ( compound-statement ) | |
2230 | __builtin_va_arg ( assignment-expression , type-id ) | |
2231 | ||
2232 | literal: | |
2233 | __null | |
2234 | ||
2235 | Returns a representation of the expression. | |
2236 | ||
2237 | *IDK indicates what kind of id-expression (if any) was present. | |
2238 | ||
2239 | *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be | |
2240 | used as the operand of a pointer-to-member. In that case, | |
2241 | *QUALIFYING_CLASS gives the class that is used as the qualifying | |
2242 | class in the pointer-to-member. */ | |
2243 | ||
2244 | static tree | |
2245 | cp_parser_primary_expression (cp_parser *parser, | |
b3445994 | 2246 | cp_id_kind *idk, |
a723baf1 MM |
2247 | tree *qualifying_class) |
2248 | { | |
2249 | cp_token *token; | |
2250 | ||
2251 | /* Assume the primary expression is not an id-expression. */ | |
b3445994 | 2252 | *idk = CP_ID_KIND_NONE; |
a723baf1 MM |
2253 | /* And that it cannot be used as pointer-to-member. */ |
2254 | *qualifying_class = NULL_TREE; | |
2255 | ||
2256 | /* Peek at the next token. */ | |
2257 | token = cp_lexer_peek_token (parser->lexer); | |
2258 | switch (token->type) | |
2259 | { | |
2260 | /* literal: | |
2261 | integer-literal | |
2262 | character-literal | |
2263 | floating-literal | |
2264 | string-literal | |
2265 | boolean-literal */ | |
2266 | case CPP_CHAR: | |
2267 | case CPP_WCHAR: | |
2268 | case CPP_STRING: | |
2269 | case CPP_WSTRING: | |
2270 | case CPP_NUMBER: | |
2271 | token = cp_lexer_consume_token (parser->lexer); | |
2272 | return token->value; | |
2273 | ||
2274 | case CPP_OPEN_PAREN: | |
2275 | { | |
2276 | tree expr; | |
2277 | bool saved_greater_than_is_operator_p; | |
2278 | ||
2279 | /* Consume the `('. */ | |
2280 | cp_lexer_consume_token (parser->lexer); | |
2281 | /* Within a parenthesized expression, a `>' token is always | |
2282 | the greater-than operator. */ | |
2283 | saved_greater_than_is_operator_p | |
2284 | = parser->greater_than_is_operator_p; | |
2285 | parser->greater_than_is_operator_p = true; | |
2286 | /* If we see `( { ' then we are looking at the beginning of | |
2287 | a GNU statement-expression. */ | |
2288 | if (cp_parser_allow_gnu_extensions_p (parser) | |
2289 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
2290 | { | |
2291 | /* Statement-expressions are not allowed by the standard. */ | |
2292 | if (pedantic) | |
2293 | pedwarn ("ISO C++ forbids braced-groups within expressions"); | |
2294 | ||
2295 | /* And they're not allowed outside of a function-body; you | |
2296 | cannot, for example, write: | |
2297 | ||
2298 | int i = ({ int j = 3; j + 1; }); | |
2299 | ||
2300 | at class or namespace scope. */ | |
2301 | if (!at_function_scope_p ()) | |
2302 | error ("statement-expressions are allowed only inside functions"); | |
2303 | /* Start the statement-expression. */ | |
2304 | expr = begin_stmt_expr (); | |
2305 | /* Parse the compound-statement. */ | |
a5bcc582 | 2306 | cp_parser_compound_statement (parser, true); |
a723baf1 | 2307 | /* Finish up. */ |
303b7406 | 2308 | expr = finish_stmt_expr (expr, false); |
a723baf1 MM |
2309 | } |
2310 | else | |
2311 | { | |
2312 | /* Parse the parenthesized expression. */ | |
2313 | expr = cp_parser_expression (parser); | |
2314 | /* Let the front end know that this expression was | |
2315 | enclosed in parentheses. This matters in case, for | |
2316 | example, the expression is of the form `A::B', since | |
2317 | `&A::B' might be a pointer-to-member, but `&(A::B)' is | |
2318 | not. */ | |
2319 | finish_parenthesized_expr (expr); | |
2320 | } | |
2321 | /* The `>' token might be the end of a template-id or | |
2322 | template-parameter-list now. */ | |
2323 | parser->greater_than_is_operator_p | |
2324 | = saved_greater_than_is_operator_p; | |
2325 | /* Consume the `)'. */ | |
2326 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
2327 | cp_parser_skip_to_end_of_statement (parser); | |
2328 | ||
2329 | return expr; | |
2330 | } | |
2331 | ||
2332 | case CPP_KEYWORD: | |
2333 | switch (token->keyword) | |
2334 | { | |
2335 | /* These two are the boolean literals. */ | |
2336 | case RID_TRUE: | |
2337 | cp_lexer_consume_token (parser->lexer); | |
2338 | return boolean_true_node; | |
2339 | case RID_FALSE: | |
2340 | cp_lexer_consume_token (parser->lexer); | |
2341 | return boolean_false_node; | |
2342 | ||
2343 | /* The `__null' literal. */ | |
2344 | case RID_NULL: | |
2345 | cp_lexer_consume_token (parser->lexer); | |
2346 | return null_node; | |
2347 | ||
2348 | /* Recognize the `this' keyword. */ | |
2349 | case RID_THIS: | |
2350 | cp_lexer_consume_token (parser->lexer); | |
2351 | if (parser->local_variables_forbidden_p) | |
2352 | { | |
2353 | error ("`this' may not be used in this context"); | |
2354 | return error_mark_node; | |
2355 | } | |
14d22dd6 MM |
2356 | /* Pointers cannot appear in constant-expressions. */ |
2357 | if (parser->constant_expression_p) | |
2358 | { | |
2359 | if (!parser->allow_non_constant_expression_p) | |
2360 | return cp_parser_non_constant_expression ("`this'"); | |
2361 | parser->non_constant_expression_p = true; | |
2362 | } | |
a723baf1 MM |
2363 | return finish_this_expr (); |
2364 | ||
2365 | /* The `operator' keyword can be the beginning of an | |
2366 | id-expression. */ | |
2367 | case RID_OPERATOR: | |
2368 | goto id_expression; | |
2369 | ||
2370 | case RID_FUNCTION_NAME: | |
2371 | case RID_PRETTY_FUNCTION_NAME: | |
2372 | case RID_C99_FUNCTION_NAME: | |
2373 | /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and | |
2374 | __func__ are the names of variables -- but they are | |
2375 | treated specially. Therefore, they are handled here, | |
2376 | rather than relying on the generic id-expression logic | |
34cd5ae7 | 2377 | below. Grammatically, these names are id-expressions. |
a723baf1 MM |
2378 | |
2379 | Consume the token. */ | |
2380 | token = cp_lexer_consume_token (parser->lexer); | |
2381 | /* Look up the name. */ | |
2382 | return finish_fname (token->value); | |
2383 | ||
2384 | case RID_VA_ARG: | |
2385 | { | |
2386 | tree expression; | |
2387 | tree type; | |
2388 | ||
2389 | /* The `__builtin_va_arg' construct is used to handle | |
2390 | `va_arg'. Consume the `__builtin_va_arg' token. */ | |
2391 | cp_lexer_consume_token (parser->lexer); | |
2392 | /* Look for the opening `('. */ | |
2393 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
2394 | /* Now, parse the assignment-expression. */ | |
2395 | expression = cp_parser_assignment_expression (parser); | |
2396 | /* Look for the `,'. */ | |
2397 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
2398 | /* Parse the type-id. */ | |
2399 | type = cp_parser_type_id (parser); | |
2400 | /* Look for the closing `)'. */ | |
2401 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
14d22dd6 MM |
2402 | /* Using `va_arg' in a constant-expression is not |
2403 | allowed. */ | |
2404 | if (parser->constant_expression_p) | |
2405 | { | |
2406 | if (!parser->allow_non_constant_expression_p) | |
2407 | return cp_parser_non_constant_expression ("`va_arg'"); | |
2408 | parser->non_constant_expression_p = true; | |
2409 | } | |
a723baf1 MM |
2410 | return build_x_va_arg (expression, type); |
2411 | } | |
2412 | ||
2413 | default: | |
2414 | cp_parser_error (parser, "expected primary-expression"); | |
2415 | return error_mark_node; | |
2416 | } | |
a723baf1 MM |
2417 | |
2418 | /* An id-expression can start with either an identifier, a | |
2419 | `::' as the beginning of a qualified-id, or the "operator" | |
2420 | keyword. */ | |
2421 | case CPP_NAME: | |
2422 | case CPP_SCOPE: | |
2423 | case CPP_TEMPLATE_ID: | |
2424 | case CPP_NESTED_NAME_SPECIFIER: | |
2425 | { | |
2426 | tree id_expression; | |
2427 | tree decl; | |
b3445994 | 2428 | const char *error_msg; |
a723baf1 MM |
2429 | |
2430 | id_expression: | |
2431 | /* Parse the id-expression. */ | |
2432 | id_expression | |
2433 | = cp_parser_id_expression (parser, | |
2434 | /*template_keyword_p=*/false, | |
2435 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
2436 | /*template_p=*/NULL, |
2437 | /*declarator_p=*/false); | |
a723baf1 MM |
2438 | if (id_expression == error_mark_node) |
2439 | return error_mark_node; | |
2440 | /* If we have a template-id, then no further lookup is | |
2441 | required. If the template-id was for a template-class, we | |
2442 | will sometimes have a TYPE_DECL at this point. */ | |
2443 | else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR | |
2444 | || TREE_CODE (id_expression) == TYPE_DECL) | |
2445 | decl = id_expression; | |
2446 | /* Look up the name. */ | |
2447 | else | |
2448 | { | |
2449 | decl = cp_parser_lookup_name_simple (parser, id_expression); | |
2450 | /* If name lookup gives us a SCOPE_REF, then the | |
2451 | qualifying scope was dependent. Just propagate the | |
2452 | name. */ | |
2453 | if (TREE_CODE (decl) == SCOPE_REF) | |
2454 | { | |
2455 | if (TYPE_P (TREE_OPERAND (decl, 0))) | |
2456 | *qualifying_class = TREE_OPERAND (decl, 0); | |
2457 | return decl; | |
2458 | } | |
2459 | /* Check to see if DECL is a local variable in a context | |
2460 | where that is forbidden. */ | |
2461 | if (parser->local_variables_forbidden_p | |
2462 | && local_variable_p (decl)) | |
2463 | { | |
2464 | /* It might be that we only found DECL because we are | |
2465 | trying to be generous with pre-ISO scoping rules. | |
2466 | For example, consider: | |
2467 | ||
2468 | int i; | |
2469 | void g() { | |
2470 | for (int i = 0; i < 10; ++i) {} | |
2471 | extern void f(int j = i); | |
2472 | } | |
2473 | ||
2474 | Here, name look up will originally find the out | |
2475 | of scope `i'. We need to issue a warning message, | |
2476 | but then use the global `i'. */ | |
2477 | decl = check_for_out_of_scope_variable (decl); | |
2478 | if (local_variable_p (decl)) | |
2479 | { | |
2480 | error ("local variable `%D' may not appear in this context", | |
2481 | decl); | |
2482 | return error_mark_node; | |
2483 | } | |
2484 | } | |
c006d942 | 2485 | } |
b3445994 MM |
2486 | |
2487 | decl = finish_id_expression (id_expression, decl, parser->scope, | |
2488 | idk, qualifying_class, | |
2489 | parser->constant_expression_p, | |
2490 | parser->allow_non_constant_expression_p, | |
2491 | &parser->non_constant_expression_p, | |
2492 | &error_msg); | |
2493 | if (error_msg) | |
2494 | cp_parser_error (parser, error_msg); | |
a723baf1 MM |
2495 | return decl; |
2496 | } | |
2497 | ||
2498 | /* Anything else is an error. */ | |
2499 | default: | |
2500 | cp_parser_error (parser, "expected primary-expression"); | |
2501 | return error_mark_node; | |
2502 | } | |
2503 | } | |
2504 | ||
2505 | /* Parse an id-expression. | |
2506 | ||
2507 | id-expression: | |
2508 | unqualified-id | |
2509 | qualified-id | |
2510 | ||
2511 | qualified-id: | |
2512 | :: [opt] nested-name-specifier template [opt] unqualified-id | |
2513 | :: identifier | |
2514 | :: operator-function-id | |
2515 | :: template-id | |
2516 | ||
2517 | Return a representation of the unqualified portion of the | |
2518 | identifier. Sets PARSER->SCOPE to the qualifying scope if there is | |
2519 | a `::' or nested-name-specifier. | |
2520 | ||
2521 | Often, if the id-expression was a qualified-id, the caller will | |
2522 | want to make a SCOPE_REF to represent the qualified-id. This | |
2523 | function does not do this in order to avoid wastefully creating | |
2524 | SCOPE_REFs when they are not required. | |
2525 | ||
a723baf1 MM |
2526 | If TEMPLATE_KEYWORD_P is true, then we have just seen the |
2527 | `template' keyword. | |
2528 | ||
2529 | If CHECK_DEPENDENCY_P is false, then names are looked up inside | |
2530 | uninstantiated templates. | |
2531 | ||
15d2cb19 | 2532 | If *TEMPLATE_P is non-NULL, it is set to true iff the |
a723baf1 | 2533 | `template' keyword is used to explicitly indicate that the entity |
f3c2dfc6 MM |
2534 | named is a template. |
2535 | ||
2536 | If DECLARATOR_P is true, the id-expression is appearing as part of | |
cd0be382 | 2537 | a declarator, rather than as part of an expression. */ |
a723baf1 MM |
2538 | |
2539 | static tree | |
2540 | cp_parser_id_expression (cp_parser *parser, | |
2541 | bool template_keyword_p, | |
2542 | bool check_dependency_p, | |
f3c2dfc6 MM |
2543 | bool *template_p, |
2544 | bool declarator_p) | |
a723baf1 MM |
2545 | { |
2546 | bool global_scope_p; | |
2547 | bool nested_name_specifier_p; | |
2548 | ||
2549 | /* Assume the `template' keyword was not used. */ | |
2550 | if (template_p) | |
2551 | *template_p = false; | |
2552 | ||
2553 | /* Look for the optional `::' operator. */ | |
2554 | global_scope_p | |
2555 | = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) | |
2556 | != NULL_TREE); | |
2557 | /* Look for the optional nested-name-specifier. */ | |
2558 | nested_name_specifier_p | |
2559 | = (cp_parser_nested_name_specifier_opt (parser, | |
2560 | /*typename_keyword_p=*/false, | |
2561 | check_dependency_p, | |
a668c6ad MM |
2562 | /*type_p=*/false, |
2563 | /*is_declarator=*/false) | |
a723baf1 MM |
2564 | != NULL_TREE); |
2565 | /* If there is a nested-name-specifier, then we are looking at | |
2566 | the first qualified-id production. */ | |
2567 | if (nested_name_specifier_p) | |
2568 | { | |
2569 | tree saved_scope; | |
2570 | tree saved_object_scope; | |
2571 | tree saved_qualifying_scope; | |
2572 | tree unqualified_id; | |
2573 | bool is_template; | |
2574 | ||
2575 | /* See if the next token is the `template' keyword. */ | |
2576 | if (!template_p) | |
2577 | template_p = &is_template; | |
2578 | *template_p = cp_parser_optional_template_keyword (parser); | |
2579 | /* Name lookup we do during the processing of the | |
2580 | unqualified-id might obliterate SCOPE. */ | |
2581 | saved_scope = parser->scope; | |
2582 | saved_object_scope = parser->object_scope; | |
2583 | saved_qualifying_scope = parser->qualifying_scope; | |
2584 | /* Process the final unqualified-id. */ | |
2585 | unqualified_id = cp_parser_unqualified_id (parser, *template_p, | |
f3c2dfc6 MM |
2586 | check_dependency_p, |
2587 | declarator_p); | |
a723baf1 MM |
2588 | /* Restore the SAVED_SCOPE for our caller. */ |
2589 | parser->scope = saved_scope; | |
2590 | parser->object_scope = saved_object_scope; | |
2591 | parser->qualifying_scope = saved_qualifying_scope; | |
2592 | ||
2593 | return unqualified_id; | |
2594 | } | |
2595 | /* Otherwise, if we are in global scope, then we are looking at one | |
2596 | of the other qualified-id productions. */ | |
2597 | else if (global_scope_p) | |
2598 | { | |
2599 | cp_token *token; | |
2600 | tree id; | |
2601 | ||
e5976695 MM |
2602 | /* Peek at the next token. */ |
2603 | token = cp_lexer_peek_token (parser->lexer); | |
2604 | ||
2605 | /* If it's an identifier, and the next token is not a "<", then | |
2606 | we can avoid the template-id case. This is an optimization | |
2607 | for this common case. */ | |
2608 | if (token->type == CPP_NAME | |
2609 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS) | |
2610 | return cp_parser_identifier (parser); | |
2611 | ||
a723baf1 MM |
2612 | cp_parser_parse_tentatively (parser); |
2613 | /* Try a template-id. */ | |
2614 | id = cp_parser_template_id (parser, | |
2615 | /*template_keyword_p=*/false, | |
a668c6ad MM |
2616 | /*check_dependency_p=*/true, |
2617 | declarator_p); | |
a723baf1 MM |
2618 | /* If that worked, we're done. */ |
2619 | if (cp_parser_parse_definitely (parser)) | |
2620 | return id; | |
2621 | ||
e5976695 MM |
2622 | /* Peek at the next token. (Changes in the token buffer may |
2623 | have invalidated the pointer obtained above.) */ | |
a723baf1 MM |
2624 | token = cp_lexer_peek_token (parser->lexer); |
2625 | ||
2626 | switch (token->type) | |
2627 | { | |
2628 | case CPP_NAME: | |
2629 | return cp_parser_identifier (parser); | |
2630 | ||
2631 | case CPP_KEYWORD: | |
2632 | if (token->keyword == RID_OPERATOR) | |
2633 | return cp_parser_operator_function_id (parser); | |
2634 | /* Fall through. */ | |
2635 | ||
2636 | default: | |
2637 | cp_parser_error (parser, "expected id-expression"); | |
2638 | return error_mark_node; | |
2639 | } | |
2640 | } | |
2641 | else | |
2642 | return cp_parser_unqualified_id (parser, template_keyword_p, | |
f3c2dfc6 MM |
2643 | /*check_dependency_p=*/true, |
2644 | declarator_p); | |
a723baf1 MM |
2645 | } |
2646 | ||
2647 | /* Parse an unqualified-id. | |
2648 | ||
2649 | unqualified-id: | |
2650 | identifier | |
2651 | operator-function-id | |
2652 | conversion-function-id | |
2653 | ~ class-name | |
2654 | template-id | |
2655 | ||
2656 | If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template' | |
2657 | keyword, in a construct like `A::template ...'. | |
2658 | ||
2659 | Returns a representation of unqualified-id. For the `identifier' | |
2660 | production, an IDENTIFIER_NODE is returned. For the `~ class-name' | |
2661 | production a BIT_NOT_EXPR is returned; the operand of the | |
2662 | BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the | |
2663 | other productions, see the documentation accompanying the | |
2664 | corresponding parsing functions. If CHECK_DEPENDENCY_P is false, | |
f3c2dfc6 MM |
2665 | names are looked up in uninstantiated templates. If DECLARATOR_P |
2666 | is true, the unqualified-id is appearing as part of a declarator, | |
2667 | rather than as part of an expression. */ | |
a723baf1 MM |
2668 | |
2669 | static tree | |
94edc4ab NN |
2670 | cp_parser_unqualified_id (cp_parser* parser, |
2671 | bool template_keyword_p, | |
f3c2dfc6 MM |
2672 | bool check_dependency_p, |
2673 | bool declarator_p) | |
a723baf1 MM |
2674 | { |
2675 | cp_token *token; | |
2676 | ||
2677 | /* Peek at the next token. */ | |
2678 | token = cp_lexer_peek_token (parser->lexer); | |
2679 | ||
2680 | switch (token->type) | |
2681 | { | |
2682 | case CPP_NAME: | |
2683 | { | |
2684 | tree id; | |
2685 | ||
2686 | /* We don't know yet whether or not this will be a | |
2687 | template-id. */ | |
2688 | cp_parser_parse_tentatively (parser); | |
2689 | /* Try a template-id. */ | |
2690 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
2691 | check_dependency_p, |
2692 | declarator_p); | |
a723baf1 MM |
2693 | /* If it worked, we're done. */ |
2694 | if (cp_parser_parse_definitely (parser)) | |
2695 | return id; | |
2696 | /* Otherwise, it's an ordinary identifier. */ | |
2697 | return cp_parser_identifier (parser); | |
2698 | } | |
2699 | ||
2700 | case CPP_TEMPLATE_ID: | |
2701 | return cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
2702 | check_dependency_p, |
2703 | declarator_p); | |
a723baf1 MM |
2704 | |
2705 | case CPP_COMPL: | |
2706 | { | |
2707 | tree type_decl; | |
2708 | tree qualifying_scope; | |
2709 | tree object_scope; | |
2710 | tree scope; | |
2711 | ||
2712 | /* Consume the `~' token. */ | |
2713 | cp_lexer_consume_token (parser->lexer); | |
2714 | /* Parse the class-name. The standard, as written, seems to | |
2715 | say that: | |
2716 | ||
2717 | template <typename T> struct S { ~S (); }; | |
2718 | template <typename T> S<T>::~S() {} | |
2719 | ||
2720 | is invalid, since `~' must be followed by a class-name, but | |
2721 | `S<T>' is dependent, and so not known to be a class. | |
2722 | That's not right; we need to look in uninstantiated | |
2723 | templates. A further complication arises from: | |
2724 | ||
2725 | template <typename T> void f(T t) { | |
2726 | t.T::~T(); | |
2727 | } | |
2728 | ||
2729 | Here, it is not possible to look up `T' in the scope of `T' | |
2730 | itself. We must look in both the current scope, and the | |
2731 | scope of the containing complete expression. | |
2732 | ||
2733 | Yet another issue is: | |
2734 | ||
2735 | struct S { | |
2736 | int S; | |
2737 | ~S(); | |
2738 | }; | |
2739 | ||
2740 | S::~S() {} | |
2741 | ||
2742 | The standard does not seem to say that the `S' in `~S' | |
2743 | should refer to the type `S' and not the data member | |
2744 | `S::S'. */ | |
2745 | ||
2746 | /* DR 244 says that we look up the name after the "~" in the | |
2747 | same scope as we looked up the qualifying name. That idea | |
2748 | isn't fully worked out; it's more complicated than that. */ | |
2749 | scope = parser->scope; | |
2750 | object_scope = parser->object_scope; | |
2751 | qualifying_scope = parser->qualifying_scope; | |
2752 | ||
2753 | /* If the name is of the form "X::~X" it's OK. */ | |
2754 | if (scope && TYPE_P (scope) | |
2755 | && cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
2756 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
2757 | == CPP_OPEN_PAREN) | |
2758 | && (cp_lexer_peek_token (parser->lexer)->value | |
2759 | == TYPE_IDENTIFIER (scope))) | |
2760 | { | |
2761 | cp_lexer_consume_token (parser->lexer); | |
2762 | return build_nt (BIT_NOT_EXPR, scope); | |
2763 | } | |
2764 | ||
2765 | /* If there was an explicit qualification (S::~T), first look | |
2766 | in the scope given by the qualification (i.e., S). */ | |
2767 | if (scope) | |
2768 | { | |
2769 | cp_parser_parse_tentatively (parser); | |
2770 | type_decl = cp_parser_class_name (parser, | |
2771 | /*typename_keyword_p=*/false, | |
2772 | /*template_keyword_p=*/false, | |
2773 | /*type_p=*/false, | |
a723baf1 | 2774 | /*check_dependency=*/false, |
a668c6ad MM |
2775 | /*class_head_p=*/false, |
2776 | declarator_p); | |
a723baf1 MM |
2777 | if (cp_parser_parse_definitely (parser)) |
2778 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); | |
2779 | } | |
2780 | /* In "N::S::~S", look in "N" as well. */ | |
2781 | if (scope && qualifying_scope) | |
2782 | { | |
2783 | cp_parser_parse_tentatively (parser); | |
2784 | parser->scope = qualifying_scope; | |
2785 | parser->object_scope = NULL_TREE; | |
2786 | parser->qualifying_scope = NULL_TREE; | |
2787 | type_decl | |
2788 | = cp_parser_class_name (parser, | |
2789 | /*typename_keyword_p=*/false, | |
2790 | /*template_keyword_p=*/false, | |
2791 | /*type_p=*/false, | |
a723baf1 | 2792 | /*check_dependency=*/false, |
a668c6ad MM |
2793 | /*class_head_p=*/false, |
2794 | declarator_p); | |
a723baf1 MM |
2795 | if (cp_parser_parse_definitely (parser)) |
2796 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); | |
2797 | } | |
2798 | /* In "p->S::~T", look in the scope given by "*p" as well. */ | |
2799 | else if (object_scope) | |
2800 | { | |
2801 | cp_parser_parse_tentatively (parser); | |
2802 | parser->scope = object_scope; | |
2803 | parser->object_scope = NULL_TREE; | |
2804 | parser->qualifying_scope = NULL_TREE; | |
2805 | type_decl | |
2806 | = cp_parser_class_name (parser, | |
2807 | /*typename_keyword_p=*/false, | |
2808 | /*template_keyword_p=*/false, | |
2809 | /*type_p=*/false, | |
a723baf1 | 2810 | /*check_dependency=*/false, |
a668c6ad MM |
2811 | /*class_head_p=*/false, |
2812 | declarator_p); | |
a723baf1 MM |
2813 | if (cp_parser_parse_definitely (parser)) |
2814 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); | |
2815 | } | |
2816 | /* Look in the surrounding context. */ | |
2817 | parser->scope = NULL_TREE; | |
2818 | parser->object_scope = NULL_TREE; | |
2819 | parser->qualifying_scope = NULL_TREE; | |
2820 | type_decl | |
2821 | = cp_parser_class_name (parser, | |
2822 | /*typename_keyword_p=*/false, | |
2823 | /*template_keyword_p=*/false, | |
2824 | /*type_p=*/false, | |
a723baf1 | 2825 | /*check_dependency=*/false, |
a668c6ad MM |
2826 | /*class_head_p=*/false, |
2827 | declarator_p); | |
a723baf1 MM |
2828 | /* If an error occurred, assume that the name of the |
2829 | destructor is the same as the name of the qualifying | |
2830 | class. That allows us to keep parsing after running | |
2831 | into ill-formed destructor names. */ | |
2832 | if (type_decl == error_mark_node && scope && TYPE_P (scope)) | |
2833 | return build_nt (BIT_NOT_EXPR, scope); | |
2834 | else if (type_decl == error_mark_node) | |
2835 | return error_mark_node; | |
2836 | ||
f3c2dfc6 MM |
2837 | /* [class.dtor] |
2838 | ||
2839 | A typedef-name that names a class shall not be used as the | |
2840 | identifier in the declarator for a destructor declaration. */ | |
2841 | if (declarator_p | |
2842 | && !DECL_IMPLICIT_TYPEDEF_P (type_decl) | |
2843 | && !DECL_SELF_REFERENCE_P (type_decl)) | |
2844 | error ("typedef-name `%D' used as destructor declarator", | |
2845 | type_decl); | |
2846 | ||
a723baf1 MM |
2847 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); |
2848 | } | |
2849 | ||
2850 | case CPP_KEYWORD: | |
2851 | if (token->keyword == RID_OPERATOR) | |
2852 | { | |
2853 | tree id; | |
2854 | ||
2855 | /* This could be a template-id, so we try that first. */ | |
2856 | cp_parser_parse_tentatively (parser); | |
2857 | /* Try a template-id. */ | |
2858 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
2859 | /*check_dependency_p=*/true, |
2860 | declarator_p); | |
a723baf1 MM |
2861 | /* If that worked, we're done. */ |
2862 | if (cp_parser_parse_definitely (parser)) | |
2863 | return id; | |
2864 | /* We still don't know whether we're looking at an | |
2865 | operator-function-id or a conversion-function-id. */ | |
2866 | cp_parser_parse_tentatively (parser); | |
2867 | /* Try an operator-function-id. */ | |
2868 | id = cp_parser_operator_function_id (parser); | |
2869 | /* If that didn't work, try a conversion-function-id. */ | |
2870 | if (!cp_parser_parse_definitely (parser)) | |
2871 | id = cp_parser_conversion_function_id (parser); | |
2872 | ||
2873 | return id; | |
2874 | } | |
2875 | /* Fall through. */ | |
2876 | ||
2877 | default: | |
2878 | cp_parser_error (parser, "expected unqualified-id"); | |
2879 | return error_mark_node; | |
2880 | } | |
2881 | } | |
2882 | ||
2883 | /* Parse an (optional) nested-name-specifier. | |
2884 | ||
2885 | nested-name-specifier: | |
2886 | class-or-namespace-name :: nested-name-specifier [opt] | |
2887 | class-or-namespace-name :: template nested-name-specifier [opt] | |
2888 | ||
2889 | PARSER->SCOPE should be set appropriately before this function is | |
2890 | called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in | |
2891 | effect. TYPE_P is TRUE if we non-type bindings should be ignored | |
2892 | in name lookups. | |
2893 | ||
2894 | Sets PARSER->SCOPE to the class (TYPE) or namespace | |
2895 | (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves | |
2896 | it unchanged if there is no nested-name-specifier. Returns the new | |
a668c6ad MM |
2897 | scope iff there is a nested-name-specifier, or NULL_TREE otherwise. |
2898 | ||
2899 | If IS_DECLARATION is TRUE, the nested-name-specifier is known to be | |
2900 | part of a declaration and/or decl-specifier. */ | |
a723baf1 MM |
2901 | |
2902 | static tree | |
2903 | cp_parser_nested_name_specifier_opt (cp_parser *parser, | |
2904 | bool typename_keyword_p, | |
2905 | bool check_dependency_p, | |
a668c6ad MM |
2906 | bool type_p, |
2907 | bool is_declaration) | |
a723baf1 MM |
2908 | { |
2909 | bool success = false; | |
2910 | tree access_check = NULL_TREE; | |
2911 | ptrdiff_t start; | |
2050a1bb | 2912 | cp_token* token; |
a723baf1 MM |
2913 | |
2914 | /* If the next token corresponds to a nested name specifier, there | |
2050a1bb MM |
2915 | is no need to reparse it. However, if CHECK_DEPENDENCY_P is |
2916 | false, it may have been true before, in which case something | |
2917 | like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier | |
2918 | of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when | |
2919 | CHECK_DEPENDENCY_P is false, we have to fall through into the | |
2920 | main loop. */ | |
2921 | if (check_dependency_p | |
2922 | && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER)) | |
2923 | { | |
2924 | cp_parser_pre_parsed_nested_name_specifier (parser); | |
a723baf1 MM |
2925 | return parser->scope; |
2926 | } | |
2927 | ||
2928 | /* Remember where the nested-name-specifier starts. */ | |
2929 | if (cp_parser_parsing_tentatively (parser) | |
2930 | && !cp_parser_committed_to_tentative_parse (parser)) | |
2931 | { | |
2050a1bb | 2932 | token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
2933 | start = cp_lexer_token_difference (parser->lexer, |
2934 | parser->lexer->first_token, | |
2050a1bb | 2935 | token); |
a723baf1 MM |
2936 | } |
2937 | else | |
2938 | start = -1; | |
2939 | ||
8d241e0b | 2940 | push_deferring_access_checks (dk_deferred); |
cf22909c | 2941 | |
a723baf1 MM |
2942 | while (true) |
2943 | { | |
2944 | tree new_scope; | |
2945 | tree old_scope; | |
2946 | tree saved_qualifying_scope; | |
a723baf1 MM |
2947 | bool template_keyword_p; |
2948 | ||
2050a1bb MM |
2949 | /* Spot cases that cannot be the beginning of a |
2950 | nested-name-specifier. */ | |
2951 | token = cp_lexer_peek_token (parser->lexer); | |
2952 | ||
2953 | /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process | |
2954 | the already parsed nested-name-specifier. */ | |
2955 | if (token->type == CPP_NESTED_NAME_SPECIFIER) | |
2956 | { | |
2957 | /* Grab the nested-name-specifier and continue the loop. */ | |
2958 | cp_parser_pre_parsed_nested_name_specifier (parser); | |
2959 | success = true; | |
2960 | continue; | |
2961 | } | |
2962 | ||
a723baf1 MM |
2963 | /* Spot cases that cannot be the beginning of a |
2964 | nested-name-specifier. On the second and subsequent times | |
2965 | through the loop, we look for the `template' keyword. */ | |
f7b5ecd9 | 2966 | if (success && token->keyword == RID_TEMPLATE) |
a723baf1 MM |
2967 | ; |
2968 | /* A template-id can start a nested-name-specifier. */ | |
f7b5ecd9 | 2969 | else if (token->type == CPP_TEMPLATE_ID) |
a723baf1 MM |
2970 | ; |
2971 | else | |
2972 | { | |
2973 | /* If the next token is not an identifier, then it is | |
2974 | definitely not a class-or-namespace-name. */ | |
f7b5ecd9 | 2975 | if (token->type != CPP_NAME) |
a723baf1 MM |
2976 | break; |
2977 | /* If the following token is neither a `<' (to begin a | |
2978 | template-id), nor a `::', then we are not looking at a | |
2979 | nested-name-specifier. */ | |
2980 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
2981 | if (token->type != CPP_LESS && token->type != CPP_SCOPE) | |
2982 | break; | |
2983 | } | |
2984 | ||
2985 | /* The nested-name-specifier is optional, so we parse | |
2986 | tentatively. */ | |
2987 | cp_parser_parse_tentatively (parser); | |
2988 | ||
2989 | /* Look for the optional `template' keyword, if this isn't the | |
2990 | first time through the loop. */ | |
2991 | if (success) | |
2992 | template_keyword_p = cp_parser_optional_template_keyword (parser); | |
2993 | else | |
2994 | template_keyword_p = false; | |
2995 | ||
2996 | /* Save the old scope since the name lookup we are about to do | |
2997 | might destroy it. */ | |
2998 | old_scope = parser->scope; | |
2999 | saved_qualifying_scope = parser->qualifying_scope; | |
3000 | /* Parse the qualifying entity. */ | |
3001 | new_scope | |
3002 | = cp_parser_class_or_namespace_name (parser, | |
3003 | typename_keyword_p, | |
3004 | template_keyword_p, | |
3005 | check_dependency_p, | |
a668c6ad MM |
3006 | type_p, |
3007 | is_declaration); | |
a723baf1 MM |
3008 | /* Look for the `::' token. */ |
3009 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
3010 | ||
3011 | /* If we found what we wanted, we keep going; otherwise, we're | |
3012 | done. */ | |
3013 | if (!cp_parser_parse_definitely (parser)) | |
3014 | { | |
3015 | bool error_p = false; | |
3016 | ||
3017 | /* Restore the OLD_SCOPE since it was valid before the | |
3018 | failed attempt at finding the last | |
3019 | class-or-namespace-name. */ | |
3020 | parser->scope = old_scope; | |
3021 | parser->qualifying_scope = saved_qualifying_scope; | |
3022 | /* If the next token is an identifier, and the one after | |
3023 | that is a `::', then any valid interpretation would have | |
3024 | found a class-or-namespace-name. */ | |
3025 | while (cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
3026 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
3027 | == CPP_SCOPE) | |
3028 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type | |
3029 | != CPP_COMPL)) | |
3030 | { | |
3031 | token = cp_lexer_consume_token (parser->lexer); | |
3032 | if (!error_p) | |
3033 | { | |
3034 | tree decl; | |
3035 | ||
3036 | decl = cp_parser_lookup_name_simple (parser, token->value); | |
3037 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
3038 | error ("`%D' used without template parameters", | |
3039 | decl); | |
3040 | else if (parser->scope) | |
3041 | { | |
3042 | if (TYPE_P (parser->scope)) | |
3043 | error ("`%T::%D' is not a class-name or " | |
3044 | "namespace-name", | |
3045 | parser->scope, token->value); | |
9075a305 KL |
3046 | else if (parser->scope == global_namespace) |
3047 | error ("`::%D' is not a class-name or " | |
3048 | "namespace-name", | |
3049 | token->value); | |
a723baf1 MM |
3050 | else |
3051 | error ("`%D::%D' is not a class-name or " | |
3052 | "namespace-name", | |
3053 | parser->scope, token->value); | |
3054 | } | |
3055 | else | |
3056 | error ("`%D' is not a class-name or namespace-name", | |
3057 | token->value); | |
3058 | parser->scope = NULL_TREE; | |
3059 | error_p = true; | |
eea9800f MM |
3060 | /* Treat this as a successful nested-name-specifier |
3061 | due to: | |
3062 | ||
3063 | [basic.lookup.qual] | |
3064 | ||
3065 | If the name found is not a class-name (clause | |
3066 | _class_) or namespace-name (_namespace.def_), the | |
3067 | program is ill-formed. */ | |
3068 | success = true; | |
a723baf1 MM |
3069 | } |
3070 | cp_lexer_consume_token (parser->lexer); | |
3071 | } | |
3072 | break; | |
3073 | } | |
3074 | ||
3075 | /* We've found one valid nested-name-specifier. */ | |
3076 | success = true; | |
3077 | /* Make sure we look in the right scope the next time through | |
3078 | the loop. */ | |
3079 | parser->scope = (TREE_CODE (new_scope) == TYPE_DECL | |
3080 | ? TREE_TYPE (new_scope) | |
3081 | : new_scope); | |
3082 | /* If it is a class scope, try to complete it; we are about to | |
3083 | be looking up names inside the class. */ | |
8fbc5ae7 MM |
3084 | if (TYPE_P (parser->scope) |
3085 | /* Since checking types for dependency can be expensive, | |
3086 | avoid doing it if the type is already complete. */ | |
3087 | && !COMPLETE_TYPE_P (parser->scope) | |
3088 | /* Do not try to complete dependent types. */ | |
1fb3244a | 3089 | && !dependent_type_p (parser->scope)) |
a723baf1 MM |
3090 | complete_type (parser->scope); |
3091 | } | |
3092 | ||
cf22909c KL |
3093 | /* Retrieve any deferred checks. Do not pop this access checks yet |
3094 | so the memory will not be reclaimed during token replacing below. */ | |
3095 | access_check = get_deferred_access_checks (); | |
3096 | ||
a723baf1 MM |
3097 | /* If parsing tentatively, replace the sequence of tokens that makes |
3098 | up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER | |
3099 | token. That way, should we re-parse the token stream, we will | |
3100 | not have to repeat the effort required to do the parse, nor will | |
3101 | we issue duplicate error messages. */ | |
3102 | if (success && start >= 0) | |
3103 | { | |
a723baf1 MM |
3104 | /* Find the token that corresponds to the start of the |
3105 | template-id. */ | |
3106 | token = cp_lexer_advance_token (parser->lexer, | |
3107 | parser->lexer->first_token, | |
3108 | start); | |
3109 | ||
a723baf1 MM |
3110 | /* Reset the contents of the START token. */ |
3111 | token->type = CPP_NESTED_NAME_SPECIFIER; | |
3112 | token->value = build_tree_list (access_check, parser->scope); | |
3113 | TREE_TYPE (token->value) = parser->qualifying_scope; | |
3114 | token->keyword = RID_MAX; | |
3115 | /* Purge all subsequent tokens. */ | |
3116 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
3117 | } | |
3118 | ||
cf22909c | 3119 | pop_deferring_access_checks (); |
a723baf1 MM |
3120 | return success ? parser->scope : NULL_TREE; |
3121 | } | |
3122 | ||
3123 | /* Parse a nested-name-specifier. See | |
3124 | cp_parser_nested_name_specifier_opt for details. This function | |
3125 | behaves identically, except that it will an issue an error if no | |
3126 | nested-name-specifier is present, and it will return | |
3127 | ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier | |
3128 | is present. */ | |
3129 | ||
3130 | static tree | |
3131 | cp_parser_nested_name_specifier (cp_parser *parser, | |
3132 | bool typename_keyword_p, | |
3133 | bool check_dependency_p, | |
a668c6ad MM |
3134 | bool type_p, |
3135 | bool is_declaration) | |
a723baf1 MM |
3136 | { |
3137 | tree scope; | |
3138 | ||
3139 | /* Look for the nested-name-specifier. */ | |
3140 | scope = cp_parser_nested_name_specifier_opt (parser, | |
3141 | typename_keyword_p, | |
3142 | check_dependency_p, | |
a668c6ad MM |
3143 | type_p, |
3144 | is_declaration); | |
a723baf1 MM |
3145 | /* If it was not present, issue an error message. */ |
3146 | if (!scope) | |
3147 | { | |
3148 | cp_parser_error (parser, "expected nested-name-specifier"); | |
eb5abb39 | 3149 | parser->scope = NULL_TREE; |
a723baf1 MM |
3150 | return error_mark_node; |
3151 | } | |
3152 | ||
3153 | return scope; | |
3154 | } | |
3155 | ||
3156 | /* Parse a class-or-namespace-name. | |
3157 | ||
3158 | class-or-namespace-name: | |
3159 | class-name | |
3160 | namespace-name | |
3161 | ||
3162 | TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect. | |
3163 | TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect. | |
3164 | CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up. | |
3165 | TYPE_P is TRUE iff the next name should be taken as a class-name, | |
3166 | even the same name is declared to be another entity in the same | |
3167 | scope. | |
3168 | ||
3169 | Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL) | |
eea9800f MM |
3170 | specified by the class-or-namespace-name. If neither is found the |
3171 | ERROR_MARK_NODE is returned. */ | |
a723baf1 MM |
3172 | |
3173 | static tree | |
3174 | cp_parser_class_or_namespace_name (cp_parser *parser, | |
3175 | bool typename_keyword_p, | |
3176 | bool template_keyword_p, | |
3177 | bool check_dependency_p, | |
a668c6ad MM |
3178 | bool type_p, |
3179 | bool is_declaration) | |
a723baf1 MM |
3180 | { |
3181 | tree saved_scope; | |
3182 | tree saved_qualifying_scope; | |
3183 | tree saved_object_scope; | |
3184 | tree scope; | |
eea9800f | 3185 | bool only_class_p; |
a723baf1 | 3186 | |
a723baf1 MM |
3187 | /* Before we try to parse the class-name, we must save away the |
3188 | current PARSER->SCOPE since cp_parser_class_name will destroy | |
3189 | it. */ | |
3190 | saved_scope = parser->scope; | |
3191 | saved_qualifying_scope = parser->qualifying_scope; | |
3192 | saved_object_scope = parser->object_scope; | |
eea9800f MM |
3193 | /* Try for a class-name first. If the SAVED_SCOPE is a type, then |
3194 | there is no need to look for a namespace-name. */ | |
bbaab916 | 3195 | only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope)); |
eea9800f MM |
3196 | if (!only_class_p) |
3197 | cp_parser_parse_tentatively (parser); | |
a723baf1 MM |
3198 | scope = cp_parser_class_name (parser, |
3199 | typename_keyword_p, | |
3200 | template_keyword_p, | |
3201 | type_p, | |
a723baf1 | 3202 | check_dependency_p, |
a668c6ad MM |
3203 | /*class_head_p=*/false, |
3204 | is_declaration); | |
a723baf1 | 3205 | /* If that didn't work, try for a namespace-name. */ |
eea9800f | 3206 | if (!only_class_p && !cp_parser_parse_definitely (parser)) |
a723baf1 MM |
3207 | { |
3208 | /* Restore the saved scope. */ | |
3209 | parser->scope = saved_scope; | |
3210 | parser->qualifying_scope = saved_qualifying_scope; | |
3211 | parser->object_scope = saved_object_scope; | |
eea9800f MM |
3212 | /* If we are not looking at an identifier followed by the scope |
3213 | resolution operator, then this is not part of a | |
3214 | nested-name-specifier. (Note that this function is only used | |
3215 | to parse the components of a nested-name-specifier.) */ | |
3216 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME) | |
3217 | || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE) | |
3218 | return error_mark_node; | |
a723baf1 MM |
3219 | scope = cp_parser_namespace_name (parser); |
3220 | } | |
3221 | ||
3222 | return scope; | |
3223 | } | |
3224 | ||
3225 | /* Parse a postfix-expression. | |
3226 | ||
3227 | postfix-expression: | |
3228 | primary-expression | |
3229 | postfix-expression [ expression ] | |
3230 | postfix-expression ( expression-list [opt] ) | |
3231 | simple-type-specifier ( expression-list [opt] ) | |
3232 | typename :: [opt] nested-name-specifier identifier | |
3233 | ( expression-list [opt] ) | |
3234 | typename :: [opt] nested-name-specifier template [opt] template-id | |
3235 | ( expression-list [opt] ) | |
3236 | postfix-expression . template [opt] id-expression | |
3237 | postfix-expression -> template [opt] id-expression | |
3238 | postfix-expression . pseudo-destructor-name | |
3239 | postfix-expression -> pseudo-destructor-name | |
3240 | postfix-expression ++ | |
3241 | postfix-expression -- | |
3242 | dynamic_cast < type-id > ( expression ) | |
3243 | static_cast < type-id > ( expression ) | |
3244 | reinterpret_cast < type-id > ( expression ) | |
3245 | const_cast < type-id > ( expression ) | |
3246 | typeid ( expression ) | |
3247 | typeid ( type-id ) | |
3248 | ||
3249 | GNU Extension: | |
3250 | ||
3251 | postfix-expression: | |
3252 | ( type-id ) { initializer-list , [opt] } | |
3253 | ||
3254 | This extension is a GNU version of the C99 compound-literal | |
3255 | construct. (The C99 grammar uses `type-name' instead of `type-id', | |
3256 | but they are essentially the same concept.) | |
3257 | ||
3258 | If ADDRESS_P is true, the postfix expression is the operand of the | |
3259 | `&' operator. | |
3260 | ||
3261 | Returns a representation of the expression. */ | |
3262 | ||
3263 | static tree | |
3264 | cp_parser_postfix_expression (cp_parser *parser, bool address_p) | |
3265 | { | |
3266 | cp_token *token; | |
3267 | enum rid keyword; | |
b3445994 | 3268 | cp_id_kind idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3269 | tree postfix_expression = NULL_TREE; |
3270 | /* Non-NULL only if the current postfix-expression can be used to | |
3271 | form a pointer-to-member. In that case, QUALIFYING_CLASS is the | |
3272 | class used to qualify the member. */ | |
3273 | tree qualifying_class = NULL_TREE; | |
a723baf1 MM |
3274 | |
3275 | /* Peek at the next token. */ | |
3276 | token = cp_lexer_peek_token (parser->lexer); | |
3277 | /* Some of the productions are determined by keywords. */ | |
3278 | keyword = token->keyword; | |
3279 | switch (keyword) | |
3280 | { | |
3281 | case RID_DYNCAST: | |
3282 | case RID_STATCAST: | |
3283 | case RID_REINTCAST: | |
3284 | case RID_CONSTCAST: | |
3285 | { | |
3286 | tree type; | |
3287 | tree expression; | |
3288 | const char *saved_message; | |
3289 | ||
3290 | /* All of these can be handled in the same way from the point | |
3291 | of view of parsing. Begin by consuming the token | |
3292 | identifying the cast. */ | |
3293 | cp_lexer_consume_token (parser->lexer); | |
3294 | ||
3295 | /* New types cannot be defined in the cast. */ | |
3296 | saved_message = parser->type_definition_forbidden_message; | |
3297 | parser->type_definition_forbidden_message | |
3298 | = "types may not be defined in casts"; | |
3299 | ||
3300 | /* Look for the opening `<'. */ | |
3301 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
3302 | /* Parse the type to which we are casting. */ | |
3303 | type = cp_parser_type_id (parser); | |
3304 | /* Look for the closing `>'. */ | |
3305 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
3306 | /* Restore the old message. */ | |
3307 | parser->type_definition_forbidden_message = saved_message; | |
3308 | ||
3309 | /* And the expression which is being cast. */ | |
3310 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
3311 | expression = cp_parser_expression (parser); | |
3312 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3313 | ||
14d22dd6 MM |
3314 | /* Only type conversions to integral or enumeration types |
3315 | can be used in constant-expressions. */ | |
3316 | if (parser->constant_expression_p | |
3317 | && !dependent_type_p (type) | |
3318 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)) | |
3319 | { | |
3320 | if (!parser->allow_non_constant_expression_p) | |
3321 | return (cp_parser_non_constant_expression | |
3322 | ("a cast to a type other than an integral or " | |
3323 | "enumeration type")); | |
3324 | parser->non_constant_expression_p = true; | |
3325 | } | |
3326 | ||
a723baf1 MM |
3327 | switch (keyword) |
3328 | { | |
3329 | case RID_DYNCAST: | |
3330 | postfix_expression | |
3331 | = build_dynamic_cast (type, expression); | |
3332 | break; | |
3333 | case RID_STATCAST: | |
3334 | postfix_expression | |
3335 | = build_static_cast (type, expression); | |
3336 | break; | |
3337 | case RID_REINTCAST: | |
3338 | postfix_expression | |
3339 | = build_reinterpret_cast (type, expression); | |
3340 | break; | |
3341 | case RID_CONSTCAST: | |
3342 | postfix_expression | |
3343 | = build_const_cast (type, expression); | |
3344 | break; | |
3345 | default: | |
3346 | abort (); | |
3347 | } | |
3348 | } | |
3349 | break; | |
3350 | ||
3351 | case RID_TYPEID: | |
3352 | { | |
3353 | tree type; | |
3354 | const char *saved_message; | |
3355 | ||
3356 | /* Consume the `typeid' token. */ | |
3357 | cp_lexer_consume_token (parser->lexer); | |
3358 | /* Look for the `(' token. */ | |
3359 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
3360 | /* Types cannot be defined in a `typeid' expression. */ | |
3361 | saved_message = parser->type_definition_forbidden_message; | |
3362 | parser->type_definition_forbidden_message | |
3363 | = "types may not be defined in a `typeid\' expression"; | |
3364 | /* We can't be sure yet whether we're looking at a type-id or an | |
3365 | expression. */ | |
3366 | cp_parser_parse_tentatively (parser); | |
3367 | /* Try a type-id first. */ | |
3368 | type = cp_parser_type_id (parser); | |
3369 | /* Look for the `)' token. Otherwise, we can't be sure that | |
3370 | we're not looking at an expression: consider `typeid (int | |
3371 | (3))', for example. */ | |
3372 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3373 | /* If all went well, simply lookup the type-id. */ | |
3374 | if (cp_parser_parse_definitely (parser)) | |
3375 | postfix_expression = get_typeid (type); | |
3376 | /* Otherwise, fall back to the expression variant. */ | |
3377 | else | |
3378 | { | |
3379 | tree expression; | |
3380 | ||
3381 | /* Look for an expression. */ | |
3382 | expression = cp_parser_expression (parser); | |
3383 | /* Compute its typeid. */ | |
3384 | postfix_expression = build_typeid (expression); | |
3385 | /* Look for the `)' token. */ | |
3386 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3387 | } | |
3388 | ||
3389 | /* Restore the saved message. */ | |
3390 | parser->type_definition_forbidden_message = saved_message; | |
3391 | } | |
3392 | break; | |
3393 | ||
3394 | case RID_TYPENAME: | |
3395 | { | |
3396 | bool template_p = false; | |
3397 | tree id; | |
3398 | tree type; | |
3399 | ||
3400 | /* Consume the `typename' token. */ | |
3401 | cp_lexer_consume_token (parser->lexer); | |
3402 | /* Look for the optional `::' operator. */ | |
3403 | cp_parser_global_scope_opt (parser, | |
3404 | /*current_scope_valid_p=*/false); | |
3405 | /* Look for the nested-name-specifier. */ | |
3406 | cp_parser_nested_name_specifier (parser, | |
3407 | /*typename_keyword_p=*/true, | |
3408 | /*check_dependency_p=*/true, | |
a668c6ad MM |
3409 | /*type_p=*/true, |
3410 | /*is_declaration=*/true); | |
a723baf1 MM |
3411 | /* Look for the optional `template' keyword. */ |
3412 | template_p = cp_parser_optional_template_keyword (parser); | |
3413 | /* We don't know whether we're looking at a template-id or an | |
3414 | identifier. */ | |
3415 | cp_parser_parse_tentatively (parser); | |
3416 | /* Try a template-id. */ | |
3417 | id = cp_parser_template_id (parser, template_p, | |
a668c6ad MM |
3418 | /*check_dependency_p=*/true, |
3419 | /*is_declaration=*/true); | |
a723baf1 MM |
3420 | /* If that didn't work, try an identifier. */ |
3421 | if (!cp_parser_parse_definitely (parser)) | |
3422 | id = cp_parser_identifier (parser); | |
3423 | /* Create a TYPENAME_TYPE to represent the type to which the | |
3424 | functional cast is being performed. */ | |
3425 | type = make_typename_type (parser->scope, id, | |
3426 | /*complain=*/1); | |
3427 | ||
3428 | postfix_expression = cp_parser_functional_cast (parser, type); | |
3429 | } | |
3430 | break; | |
3431 | ||
3432 | default: | |
3433 | { | |
3434 | tree type; | |
3435 | ||
3436 | /* If the next thing is a simple-type-specifier, we may be | |
3437 | looking at a functional cast. We could also be looking at | |
3438 | an id-expression. So, we try the functional cast, and if | |
3439 | that doesn't work we fall back to the primary-expression. */ | |
3440 | cp_parser_parse_tentatively (parser); | |
3441 | /* Look for the simple-type-specifier. */ | |
3442 | type = cp_parser_simple_type_specifier (parser, | |
4b0d3cbe MM |
3443 | CP_PARSER_FLAGS_NONE, |
3444 | /*identifier_p=*/false); | |
a723baf1 MM |
3445 | /* Parse the cast itself. */ |
3446 | if (!cp_parser_error_occurred (parser)) | |
3447 | postfix_expression | |
3448 | = cp_parser_functional_cast (parser, type); | |
3449 | /* If that worked, we're done. */ | |
3450 | if (cp_parser_parse_definitely (parser)) | |
3451 | break; | |
3452 | ||
3453 | /* If the functional-cast didn't work out, try a | |
3454 | compound-literal. */ | |
14d22dd6 MM |
3455 | if (cp_parser_allow_gnu_extensions_p (parser) |
3456 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
a723baf1 MM |
3457 | { |
3458 | tree initializer_list = NULL_TREE; | |
3459 | ||
3460 | cp_parser_parse_tentatively (parser); | |
14d22dd6 MM |
3461 | /* Consume the `('. */ |
3462 | cp_lexer_consume_token (parser->lexer); | |
3463 | /* Parse the type. */ | |
3464 | type = cp_parser_type_id (parser); | |
3465 | /* Look for the `)'. */ | |
3466 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3467 | /* Look for the `{'. */ | |
3468 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
3469 | /* If things aren't going well, there's no need to | |
3470 | keep going. */ | |
3471 | if (!cp_parser_error_occurred (parser)) | |
a723baf1 | 3472 | { |
39703eb9 | 3473 | bool non_constant_p; |
14d22dd6 MM |
3474 | /* Parse the initializer-list. */ |
3475 | initializer_list | |
39703eb9 | 3476 | = cp_parser_initializer_list (parser, &non_constant_p); |
14d22dd6 MM |
3477 | /* Allow a trailing `,'. */ |
3478 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
3479 | cp_lexer_consume_token (parser->lexer); | |
3480 | /* Look for the final `}'. */ | |
3481 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
a723baf1 MM |
3482 | } |
3483 | /* If that worked, we're definitely looking at a | |
3484 | compound-literal expression. */ | |
3485 | if (cp_parser_parse_definitely (parser)) | |
3486 | { | |
3487 | /* Warn the user that a compound literal is not | |
3488 | allowed in standard C++. */ | |
3489 | if (pedantic) | |
3490 | pedwarn ("ISO C++ forbids compound-literals"); | |
3491 | /* Form the representation of the compound-literal. */ | |
3492 | postfix_expression | |
3493 | = finish_compound_literal (type, initializer_list); | |
3494 | break; | |
3495 | } | |
3496 | } | |
3497 | ||
3498 | /* It must be a primary-expression. */ | |
3499 | postfix_expression = cp_parser_primary_expression (parser, | |
3500 | &idk, | |
3501 | &qualifying_class); | |
3502 | } | |
3503 | break; | |
3504 | } | |
3505 | ||
ee76b931 MM |
3506 | /* If we were avoiding committing to the processing of a |
3507 | qualified-id until we knew whether or not we had a | |
3508 | pointer-to-member, we now know. */ | |
089d6ea7 | 3509 | if (qualifying_class) |
a723baf1 | 3510 | { |
ee76b931 | 3511 | bool done; |
a723baf1 | 3512 | |
ee76b931 MM |
3513 | /* Peek at the next token. */ |
3514 | token = cp_lexer_peek_token (parser->lexer); | |
3515 | done = (token->type != CPP_OPEN_SQUARE | |
3516 | && token->type != CPP_OPEN_PAREN | |
3517 | && token->type != CPP_DOT | |
3518 | && token->type != CPP_DEREF | |
3519 | && token->type != CPP_PLUS_PLUS | |
3520 | && token->type != CPP_MINUS_MINUS); | |
3521 | ||
3522 | postfix_expression = finish_qualified_id_expr (qualifying_class, | |
3523 | postfix_expression, | |
3524 | done, | |
3525 | address_p); | |
3526 | if (done) | |
3527 | return postfix_expression; | |
a723baf1 MM |
3528 | } |
3529 | ||
a723baf1 MM |
3530 | /* Keep looping until the postfix-expression is complete. */ |
3531 | while (true) | |
3532 | { | |
10b1d5e7 MM |
3533 | if (idk == CP_ID_KIND_UNQUALIFIED |
3534 | && TREE_CODE (postfix_expression) == IDENTIFIER_NODE | |
a723baf1 | 3535 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) |
b3445994 MM |
3536 | /* It is not a Koenig lookup function call. */ |
3537 | postfix_expression | |
3538 | = unqualified_name_lookup_error (postfix_expression); | |
a723baf1 MM |
3539 | |
3540 | /* Peek at the next token. */ | |
3541 | token = cp_lexer_peek_token (parser->lexer); | |
3542 | ||
3543 | switch (token->type) | |
3544 | { | |
3545 | case CPP_OPEN_SQUARE: | |
3546 | /* postfix-expression [ expression ] */ | |
3547 | { | |
3548 | tree index; | |
3549 | ||
3550 | /* Consume the `[' token. */ | |
3551 | cp_lexer_consume_token (parser->lexer); | |
3552 | /* Parse the index expression. */ | |
3553 | index = cp_parser_expression (parser); | |
3554 | /* Look for the closing `]'. */ | |
3555 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
3556 | ||
3557 | /* Build the ARRAY_REF. */ | |
3558 | postfix_expression | |
3559 | = grok_array_decl (postfix_expression, index); | |
b3445994 | 3560 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3561 | } |
3562 | break; | |
3563 | ||
3564 | case CPP_OPEN_PAREN: | |
3565 | /* postfix-expression ( expression-list [opt] ) */ | |
3566 | { | |
6d80c4b9 | 3567 | bool koenig_p; |
39703eb9 MM |
3568 | tree args = (cp_parser_parenthesized_expression_list |
3569 | (parser, false, /*non_constant_p=*/NULL)); | |
a723baf1 | 3570 | |
7efa3e22 NS |
3571 | if (args == error_mark_node) |
3572 | { | |
3573 | postfix_expression = error_mark_node; | |
3574 | break; | |
3575 | } | |
3576 | ||
14d22dd6 MM |
3577 | /* Function calls are not permitted in |
3578 | constant-expressions. */ | |
3579 | if (parser->constant_expression_p) | |
3580 | { | |
3581 | if (!parser->allow_non_constant_expression_p) | |
3582 | return cp_parser_non_constant_expression ("a function call"); | |
3583 | parser->non_constant_expression_p = true; | |
3584 | } | |
a723baf1 | 3585 | |
6d80c4b9 | 3586 | koenig_p = false; |
399dedb9 NS |
3587 | if (idk == CP_ID_KIND_UNQUALIFIED) |
3588 | { | |
3589 | if (args | |
3590 | && (is_overloaded_fn (postfix_expression) | |
3591 | || DECL_P (postfix_expression) | |
3592 | || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)) | |
6d80c4b9 MM |
3593 | { |
3594 | koenig_p = true; | |
3595 | postfix_expression | |
3596 | = perform_koenig_lookup (postfix_expression, args); | |
3597 | } | |
399dedb9 NS |
3598 | else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE) |
3599 | postfix_expression | |
3600 | = unqualified_fn_lookup_error (postfix_expression); | |
3601 | } | |
3602 | ||
d17811fd | 3603 | if (TREE_CODE (postfix_expression) == COMPONENT_REF) |
a723baf1 | 3604 | { |
d17811fd MM |
3605 | tree instance = TREE_OPERAND (postfix_expression, 0); |
3606 | tree fn = TREE_OPERAND (postfix_expression, 1); | |
3607 | ||
3608 | if (processing_template_decl | |
3609 | && (type_dependent_expression_p (instance) | |
3610 | || (!BASELINK_P (fn) | |
3611 | && TREE_CODE (fn) != FIELD_DECL) | |
584672ee | 3612 | || type_dependent_expression_p (fn) |
d17811fd MM |
3613 | || any_type_dependent_arguments_p (args))) |
3614 | { | |
3615 | postfix_expression | |
3616 | = build_min_nt (CALL_EXPR, postfix_expression, args); | |
3617 | break; | |
3618 | } | |
3619 | ||
3620 | postfix_expression | |
3621 | = (build_new_method_call | |
3622 | (instance, fn, args, NULL_TREE, | |
b3445994 | 3623 | (idk == CP_ID_KIND_QUALIFIED |
d17811fd | 3624 | ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL))); |
a723baf1 | 3625 | } |
d17811fd MM |
3626 | else if (TREE_CODE (postfix_expression) == OFFSET_REF |
3627 | || TREE_CODE (postfix_expression) == MEMBER_REF | |
3628 | || TREE_CODE (postfix_expression) == DOTSTAR_EXPR) | |
a723baf1 MM |
3629 | postfix_expression = (build_offset_ref_call_from_tree |
3630 | (postfix_expression, args)); | |
b3445994 | 3631 | else if (idk == CP_ID_KIND_QUALIFIED) |
2050a1bb MM |
3632 | /* A call to a static class member, or a namespace-scope |
3633 | function. */ | |
3634 | postfix_expression | |
3635 | = finish_call_expr (postfix_expression, args, | |
6d80c4b9 MM |
3636 | /*disallow_virtual=*/true, |
3637 | koenig_p); | |
a723baf1 | 3638 | else |
2050a1bb MM |
3639 | /* All other function calls. */ |
3640 | postfix_expression | |
3641 | = finish_call_expr (postfix_expression, args, | |
6d80c4b9 MM |
3642 | /*disallow_virtual=*/false, |
3643 | koenig_p); | |
a723baf1 MM |
3644 | |
3645 | /* The POSTFIX_EXPRESSION is certainly no longer an id. */ | |
b3445994 | 3646 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3647 | } |
3648 | break; | |
3649 | ||
3650 | case CPP_DOT: | |
3651 | case CPP_DEREF: | |
3652 | /* postfix-expression . template [opt] id-expression | |
3653 | postfix-expression . pseudo-destructor-name | |
3654 | postfix-expression -> template [opt] id-expression | |
3655 | postfix-expression -> pseudo-destructor-name */ | |
3656 | { | |
3657 | tree name; | |
3658 | bool dependent_p; | |
3659 | bool template_p; | |
3660 | tree scope = NULL_TREE; | |
3661 | ||
3662 | /* If this is a `->' operator, dereference the pointer. */ | |
3663 | if (token->type == CPP_DEREF) | |
3664 | postfix_expression = build_x_arrow (postfix_expression); | |
3665 | /* Check to see whether or not the expression is | |
3666 | type-dependent. */ | |
bbaab916 | 3667 | dependent_p = type_dependent_expression_p (postfix_expression); |
a723baf1 MM |
3668 | /* The identifier following the `->' or `.' is not |
3669 | qualified. */ | |
3670 | parser->scope = NULL_TREE; | |
3671 | parser->qualifying_scope = NULL_TREE; | |
3672 | parser->object_scope = NULL_TREE; | |
b3445994 | 3673 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3674 | /* Enter the scope corresponding to the type of the object |
3675 | given by the POSTFIX_EXPRESSION. */ | |
3676 | if (!dependent_p | |
3677 | && TREE_TYPE (postfix_expression) != NULL_TREE) | |
3678 | { | |
3679 | scope = TREE_TYPE (postfix_expression); | |
3680 | /* According to the standard, no expression should | |
3681 | ever have reference type. Unfortunately, we do not | |
3682 | currently match the standard in this respect in | |
3683 | that our internal representation of an expression | |
3684 | may have reference type even when the standard says | |
3685 | it does not. Therefore, we have to manually obtain | |
3686 | the underlying type here. */ | |
ee76b931 | 3687 | scope = non_reference (scope); |
a723baf1 MM |
3688 | /* The type of the POSTFIX_EXPRESSION must be |
3689 | complete. */ | |
3690 | scope = complete_type_or_else (scope, NULL_TREE); | |
3691 | /* Let the name lookup machinery know that we are | |
3692 | processing a class member access expression. */ | |
3693 | parser->context->object_type = scope; | |
3694 | /* If something went wrong, we want to be able to | |
3695 | discern that case, as opposed to the case where | |
3696 | there was no SCOPE due to the type of expression | |
3697 | being dependent. */ | |
3698 | if (!scope) | |
3699 | scope = error_mark_node; | |
3700 | } | |
3701 | ||
3702 | /* Consume the `.' or `->' operator. */ | |
3703 | cp_lexer_consume_token (parser->lexer); | |
3704 | /* If the SCOPE is not a scalar type, we are looking at an | |
3705 | ordinary class member access expression, rather than a | |
3706 | pseudo-destructor-name. */ | |
3707 | if (!scope || !SCALAR_TYPE_P (scope)) | |
3708 | { | |
3709 | template_p = cp_parser_optional_template_keyword (parser); | |
3710 | /* Parse the id-expression. */ | |
3711 | name = cp_parser_id_expression (parser, | |
3712 | template_p, | |
3713 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
3714 | /*template_p=*/NULL, |
3715 | /*declarator_p=*/false); | |
a723baf1 MM |
3716 | /* In general, build a SCOPE_REF if the member name is |
3717 | qualified. However, if the name was not dependent | |
3718 | and has already been resolved; there is no need to | |
3719 | build the SCOPE_REF. For example; | |
3720 | ||
3721 | struct X { void f(); }; | |
3722 | template <typename T> void f(T* t) { t->X::f(); } | |
3723 | ||
d17811fd MM |
3724 | Even though "t" is dependent, "X::f" is not and has |
3725 | been resolved to a BASELINK; there is no need to | |
a723baf1 | 3726 | include scope information. */ |
a6bd211d JM |
3727 | |
3728 | /* But we do need to remember that there was an explicit | |
3729 | scope for virtual function calls. */ | |
3730 | if (parser->scope) | |
b3445994 | 3731 | idk = CP_ID_KIND_QUALIFIED; |
a6bd211d | 3732 | |
a723baf1 MM |
3733 | if (name != error_mark_node |
3734 | && !BASELINK_P (name) | |
3735 | && parser->scope) | |
3736 | { | |
3737 | name = build_nt (SCOPE_REF, parser->scope, name); | |
3738 | parser->scope = NULL_TREE; | |
3739 | parser->qualifying_scope = NULL_TREE; | |
3740 | parser->object_scope = NULL_TREE; | |
3741 | } | |
3742 | postfix_expression | |
3743 | = finish_class_member_access_expr (postfix_expression, name); | |
3744 | } | |
3745 | /* Otherwise, try the pseudo-destructor-name production. */ | |
3746 | else | |
3747 | { | |
90808894 | 3748 | tree s = NULL_TREE; |
a723baf1 MM |
3749 | tree type; |
3750 | ||
3751 | /* Parse the pseudo-destructor-name. */ | |
3752 | cp_parser_pseudo_destructor_name (parser, &s, &type); | |
3753 | /* Form the call. */ | |
3754 | postfix_expression | |
3755 | = finish_pseudo_destructor_expr (postfix_expression, | |
3756 | s, TREE_TYPE (type)); | |
3757 | } | |
3758 | ||
3759 | /* We no longer need to look up names in the scope of the | |
3760 | object on the left-hand side of the `.' or `->' | |
3761 | operator. */ | |
3762 | parser->context->object_type = NULL_TREE; | |
a723baf1 MM |
3763 | } |
3764 | break; | |
3765 | ||
3766 | case CPP_PLUS_PLUS: | |
3767 | /* postfix-expression ++ */ | |
3768 | /* Consume the `++' token. */ | |
3769 | cp_lexer_consume_token (parser->lexer); | |
14d22dd6 MM |
3770 | /* Increments may not appear in constant-expressions. */ |
3771 | if (parser->constant_expression_p) | |
3772 | { | |
3773 | if (!parser->allow_non_constant_expression_p) | |
3774 | return cp_parser_non_constant_expression ("an increment"); | |
3775 | parser->non_constant_expression_p = true; | |
3776 | } | |
34cd5ae7 | 3777 | /* Generate a representation for the complete expression. */ |
a723baf1 MM |
3778 | postfix_expression |
3779 | = finish_increment_expr (postfix_expression, | |
3780 | POSTINCREMENT_EXPR); | |
b3445994 | 3781 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3782 | break; |
3783 | ||
3784 | case CPP_MINUS_MINUS: | |
3785 | /* postfix-expression -- */ | |
3786 | /* Consume the `--' token. */ | |
3787 | cp_lexer_consume_token (parser->lexer); | |
14d22dd6 MM |
3788 | /* Decrements may not appear in constant-expressions. */ |
3789 | if (parser->constant_expression_p) | |
3790 | { | |
3791 | if (!parser->allow_non_constant_expression_p) | |
3792 | return cp_parser_non_constant_expression ("a decrement"); | |
3793 | parser->non_constant_expression_p = true; | |
3794 | } | |
34cd5ae7 | 3795 | /* Generate a representation for the complete expression. */ |
a723baf1 MM |
3796 | postfix_expression |
3797 | = finish_increment_expr (postfix_expression, | |
3798 | POSTDECREMENT_EXPR); | |
b3445994 | 3799 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3800 | break; |
3801 | ||
3802 | default: | |
3803 | return postfix_expression; | |
3804 | } | |
3805 | } | |
3806 | ||
3807 | /* We should never get here. */ | |
3808 | abort (); | |
3809 | return error_mark_node; | |
3810 | } | |
3811 | ||
7efa3e22 | 3812 | /* Parse a parenthesized expression-list. |
a723baf1 MM |
3813 | |
3814 | expression-list: | |
3815 | assignment-expression | |
3816 | expression-list, assignment-expression | |
3817 | ||
7efa3e22 NS |
3818 | attribute-list: |
3819 | expression-list | |
3820 | identifier | |
3821 | identifier, expression-list | |
3822 | ||
a723baf1 MM |
3823 | Returns a TREE_LIST. The TREE_VALUE of each node is a |
3824 | representation of an assignment-expression. Note that a TREE_LIST | |
7efa3e22 NS |
3825 | is returned even if there is only a single expression in the list. |
3826 | error_mark_node is returned if the ( and or ) are | |
3827 | missing. NULL_TREE is returned on no expressions. The parentheses | |
3828 | are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute | |
39703eb9 MM |
3829 | list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P |
3830 | indicates whether or not all of the expressions in the list were | |
3831 | constant. */ | |
a723baf1 MM |
3832 | |
3833 | static tree | |
39703eb9 MM |
3834 | cp_parser_parenthesized_expression_list (cp_parser* parser, |
3835 | bool is_attribute_list, | |
3836 | bool *non_constant_p) | |
a723baf1 MM |
3837 | { |
3838 | tree expression_list = NULL_TREE; | |
7efa3e22 | 3839 | tree identifier = NULL_TREE; |
39703eb9 MM |
3840 | |
3841 | /* Assume all the expressions will be constant. */ | |
3842 | if (non_constant_p) | |
3843 | *non_constant_p = false; | |
3844 | ||
7efa3e22 NS |
3845 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) |
3846 | return error_mark_node; | |
3847 | ||
a723baf1 | 3848 | /* Consume expressions until there are no more. */ |
7efa3e22 NS |
3849 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) |
3850 | while (true) | |
3851 | { | |
3852 | tree expr; | |
3853 | ||
3854 | /* At the beginning of attribute lists, check to see if the | |
3855 | next token is an identifier. */ | |
3856 | if (is_attribute_list | |
3857 | && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME) | |
3858 | { | |
3859 | cp_token *token; | |
3860 | ||
3861 | /* Consume the identifier. */ | |
3862 | token = cp_lexer_consume_token (parser->lexer); | |
3863 | /* Save the identifier. */ | |
3864 | identifier = token->value; | |
3865 | } | |
3866 | else | |
3867 | { | |
3868 | /* Parse the next assignment-expression. */ | |
39703eb9 MM |
3869 | if (non_constant_p) |
3870 | { | |
3871 | bool expr_non_constant_p; | |
3872 | expr = (cp_parser_constant_expression | |
3873 | (parser, /*allow_non_constant_p=*/true, | |
3874 | &expr_non_constant_p)); | |
3875 | if (expr_non_constant_p) | |
3876 | *non_constant_p = true; | |
3877 | } | |
3878 | else | |
3879 | expr = cp_parser_assignment_expression (parser); | |
a723baf1 | 3880 | |
7efa3e22 NS |
3881 | /* Add it to the list. We add error_mark_node |
3882 | expressions to the list, so that we can still tell if | |
3883 | the correct form for a parenthesized expression-list | |
3884 | is found. That gives better errors. */ | |
3885 | expression_list = tree_cons (NULL_TREE, expr, expression_list); | |
a723baf1 | 3886 | |
7efa3e22 NS |
3887 | if (expr == error_mark_node) |
3888 | goto skip_comma; | |
3889 | } | |
a723baf1 | 3890 | |
7efa3e22 NS |
3891 | /* After the first item, attribute lists look the same as |
3892 | expression lists. */ | |
3893 | is_attribute_list = false; | |
3894 | ||
3895 | get_comma:; | |
3896 | /* If the next token isn't a `,', then we are done. */ | |
3897 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
3898 | break; | |
3899 | ||
3900 | /* Otherwise, consume the `,' and keep going. */ | |
3901 | cp_lexer_consume_token (parser->lexer); | |
3902 | } | |
3903 | ||
3904 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
3905 | { | |
3906 | int ending; | |
3907 | ||
3908 | skip_comma:; | |
3909 | /* We try and resync to an unnested comma, as that will give the | |
3910 | user better diagnostics. */ | |
a668c6ad MM |
3911 | ending = cp_parser_skip_to_closing_parenthesis (parser, true, true, |
3912 | /*consume_paren=*/true); | |
7efa3e22 NS |
3913 | if (ending < 0) |
3914 | goto get_comma; | |
3915 | if (!ending) | |
3916 | return error_mark_node; | |
a723baf1 MM |
3917 | } |
3918 | ||
3919 | /* We built up the list in reverse order so we must reverse it now. */ | |
7efa3e22 NS |
3920 | expression_list = nreverse (expression_list); |
3921 | if (identifier) | |
3922 | expression_list = tree_cons (NULL_TREE, identifier, expression_list); | |
3923 | ||
3924 | return expression_list; | |
a723baf1 MM |
3925 | } |
3926 | ||
3927 | /* Parse a pseudo-destructor-name. | |
3928 | ||
3929 | pseudo-destructor-name: | |
3930 | :: [opt] nested-name-specifier [opt] type-name :: ~ type-name | |
3931 | :: [opt] nested-name-specifier template template-id :: ~ type-name | |
3932 | :: [opt] nested-name-specifier [opt] ~ type-name | |
3933 | ||
3934 | If either of the first two productions is used, sets *SCOPE to the | |
3935 | TYPE specified before the final `::'. Otherwise, *SCOPE is set to | |
3936 | NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name, | |
3937 | or ERROR_MARK_NODE if no type-name is present. */ | |
3938 | ||
3939 | static void | |
94edc4ab NN |
3940 | cp_parser_pseudo_destructor_name (cp_parser* parser, |
3941 | tree* scope, | |
3942 | tree* type) | |
a723baf1 MM |
3943 | { |
3944 | bool nested_name_specifier_p; | |
3945 | ||
3946 | /* Look for the optional `::' operator. */ | |
3947 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true); | |
3948 | /* Look for the optional nested-name-specifier. */ | |
3949 | nested_name_specifier_p | |
3950 | = (cp_parser_nested_name_specifier_opt (parser, | |
3951 | /*typename_keyword_p=*/false, | |
3952 | /*check_dependency_p=*/true, | |
a668c6ad MM |
3953 | /*type_p=*/false, |
3954 | /*is_declaration=*/true) | |
a723baf1 MM |
3955 | != NULL_TREE); |
3956 | /* Now, if we saw a nested-name-specifier, we might be doing the | |
3957 | second production. */ | |
3958 | if (nested_name_specifier_p | |
3959 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
3960 | { | |
3961 | /* Consume the `template' keyword. */ | |
3962 | cp_lexer_consume_token (parser->lexer); | |
3963 | /* Parse the template-id. */ | |
3964 | cp_parser_template_id (parser, | |
3965 | /*template_keyword_p=*/true, | |
a668c6ad MM |
3966 | /*check_dependency_p=*/false, |
3967 | /*is_declaration=*/true); | |
a723baf1 MM |
3968 | /* Look for the `::' token. */ |
3969 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
3970 | } | |
3971 | /* If the next token is not a `~', then there might be some | |
9bcb9aae | 3972 | additional qualification. */ |
a723baf1 MM |
3973 | else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL)) |
3974 | { | |
3975 | /* Look for the type-name. */ | |
3976 | *scope = TREE_TYPE (cp_parser_type_name (parser)); | |
3977 | /* Look for the `::' token. */ | |
3978 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
3979 | } | |
3980 | else | |
3981 | *scope = NULL_TREE; | |
3982 | ||
3983 | /* Look for the `~'. */ | |
3984 | cp_parser_require (parser, CPP_COMPL, "`~'"); | |
3985 | /* Look for the type-name again. We are not responsible for | |
3986 | checking that it matches the first type-name. */ | |
3987 | *type = cp_parser_type_name (parser); | |
3988 | } | |
3989 | ||
3990 | /* Parse a unary-expression. | |
3991 | ||
3992 | unary-expression: | |
3993 | postfix-expression | |
3994 | ++ cast-expression | |
3995 | -- cast-expression | |
3996 | unary-operator cast-expression | |
3997 | sizeof unary-expression | |
3998 | sizeof ( type-id ) | |
3999 | new-expression | |
4000 | delete-expression | |
4001 | ||
4002 | GNU Extensions: | |
4003 | ||
4004 | unary-expression: | |
4005 | __extension__ cast-expression | |
4006 | __alignof__ unary-expression | |
4007 | __alignof__ ( type-id ) | |
4008 | __real__ cast-expression | |
4009 | __imag__ cast-expression | |
4010 | && identifier | |
4011 | ||
4012 | ADDRESS_P is true iff the unary-expression is appearing as the | |
4013 | operand of the `&' operator. | |
4014 | ||
34cd5ae7 | 4015 | Returns a representation of the expression. */ |
a723baf1 MM |
4016 | |
4017 | static tree | |
4018 | cp_parser_unary_expression (cp_parser *parser, bool address_p) | |
4019 | { | |
4020 | cp_token *token; | |
4021 | enum tree_code unary_operator; | |
4022 | ||
4023 | /* Peek at the next token. */ | |
4024 | token = cp_lexer_peek_token (parser->lexer); | |
4025 | /* Some keywords give away the kind of expression. */ | |
4026 | if (token->type == CPP_KEYWORD) | |
4027 | { | |
4028 | enum rid keyword = token->keyword; | |
4029 | ||
4030 | switch (keyword) | |
4031 | { | |
4032 | case RID_ALIGNOF: | |
a723baf1 MM |
4033 | case RID_SIZEOF: |
4034 | { | |
4035 | tree operand; | |
7a18b933 | 4036 | enum tree_code op; |
a723baf1 | 4037 | |
7a18b933 NS |
4038 | op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR; |
4039 | /* Consume the token. */ | |
a723baf1 MM |
4040 | cp_lexer_consume_token (parser->lexer); |
4041 | /* Parse the operand. */ | |
4042 | operand = cp_parser_sizeof_operand (parser, keyword); | |
4043 | ||
7a18b933 NS |
4044 | if (TYPE_P (operand)) |
4045 | return cxx_sizeof_or_alignof_type (operand, op, true); | |
a723baf1 | 4046 | else |
7a18b933 | 4047 | return cxx_sizeof_or_alignof_expr (operand, op); |
a723baf1 MM |
4048 | } |
4049 | ||
4050 | case RID_NEW: | |
4051 | return cp_parser_new_expression (parser); | |
4052 | ||
4053 | case RID_DELETE: | |
4054 | return cp_parser_delete_expression (parser); | |
4055 | ||
4056 | case RID_EXTENSION: | |
4057 | { | |
4058 | /* The saved value of the PEDANTIC flag. */ | |
4059 | int saved_pedantic; | |
4060 | tree expr; | |
4061 | ||
4062 | /* Save away the PEDANTIC flag. */ | |
4063 | cp_parser_extension_opt (parser, &saved_pedantic); | |
4064 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4065 | expr = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4066 | /* Restore the PEDANTIC flag. */ |
4067 | pedantic = saved_pedantic; | |
4068 | ||
4069 | return expr; | |
4070 | } | |
4071 | ||
4072 | case RID_REALPART: | |
4073 | case RID_IMAGPART: | |
4074 | { | |
4075 | tree expression; | |
4076 | ||
4077 | /* Consume the `__real__' or `__imag__' token. */ | |
4078 | cp_lexer_consume_token (parser->lexer); | |
4079 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4080 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4081 | /* Create the complete representation. */ |
4082 | return build_x_unary_op ((keyword == RID_REALPART | |
4083 | ? REALPART_EXPR : IMAGPART_EXPR), | |
4084 | expression); | |
4085 | } | |
4086 | break; | |
4087 | ||
4088 | default: | |
4089 | break; | |
4090 | } | |
4091 | } | |
4092 | ||
4093 | /* Look for the `:: new' and `:: delete', which also signal the | |
4094 | beginning of a new-expression, or delete-expression, | |
4095 | respectively. If the next token is `::', then it might be one of | |
4096 | these. */ | |
4097 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
4098 | { | |
4099 | enum rid keyword; | |
4100 | ||
4101 | /* See if the token after the `::' is one of the keywords in | |
4102 | which we're interested. */ | |
4103 | keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword; | |
4104 | /* If it's `new', we have a new-expression. */ | |
4105 | if (keyword == RID_NEW) | |
4106 | return cp_parser_new_expression (parser); | |
4107 | /* Similarly, for `delete'. */ | |
4108 | else if (keyword == RID_DELETE) | |
4109 | return cp_parser_delete_expression (parser); | |
4110 | } | |
4111 | ||
4112 | /* Look for a unary operator. */ | |
4113 | unary_operator = cp_parser_unary_operator (token); | |
4114 | /* The `++' and `--' operators can be handled similarly, even though | |
4115 | they are not technically unary-operators in the grammar. */ | |
4116 | if (unary_operator == ERROR_MARK) | |
4117 | { | |
4118 | if (token->type == CPP_PLUS_PLUS) | |
4119 | unary_operator = PREINCREMENT_EXPR; | |
4120 | else if (token->type == CPP_MINUS_MINUS) | |
4121 | unary_operator = PREDECREMENT_EXPR; | |
4122 | /* Handle the GNU address-of-label extension. */ | |
4123 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
4124 | && token->type == CPP_AND_AND) | |
4125 | { | |
4126 | tree identifier; | |
4127 | ||
4128 | /* Consume the '&&' token. */ | |
4129 | cp_lexer_consume_token (parser->lexer); | |
4130 | /* Look for the identifier. */ | |
4131 | identifier = cp_parser_identifier (parser); | |
4132 | /* Create an expression representing the address. */ | |
4133 | return finish_label_address_expr (identifier); | |
4134 | } | |
4135 | } | |
4136 | if (unary_operator != ERROR_MARK) | |
4137 | { | |
4138 | tree cast_expression; | |
4139 | ||
4140 | /* Consume the operator token. */ | |
4141 | token = cp_lexer_consume_token (parser->lexer); | |
4142 | /* Parse the cast-expression. */ | |
4143 | cast_expression | |
4144 | = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR); | |
4145 | /* Now, build an appropriate representation. */ | |
4146 | switch (unary_operator) | |
4147 | { | |
4148 | case INDIRECT_REF: | |
4149 | return build_x_indirect_ref (cast_expression, "unary *"); | |
4150 | ||
4151 | case ADDR_EXPR: | |
d17811fd MM |
4152 | case BIT_NOT_EXPR: |
4153 | return build_x_unary_op (unary_operator, cast_expression); | |
a723baf1 | 4154 | |
14d22dd6 MM |
4155 | case PREINCREMENT_EXPR: |
4156 | case PREDECREMENT_EXPR: | |
4157 | if (parser->constant_expression_p) | |
4158 | { | |
4159 | if (!parser->allow_non_constant_expression_p) | |
4160 | return cp_parser_non_constant_expression (PREINCREMENT_EXPR | |
4161 | ? "an increment" | |
4162 | : "a decrement"); | |
4163 | parser->non_constant_expression_p = true; | |
4164 | } | |
4165 | /* Fall through. */ | |
a723baf1 MM |
4166 | case CONVERT_EXPR: |
4167 | case NEGATE_EXPR: | |
4168 | case TRUTH_NOT_EXPR: | |
a723baf1 MM |
4169 | return finish_unary_op_expr (unary_operator, cast_expression); |
4170 | ||
a723baf1 MM |
4171 | default: |
4172 | abort (); | |
4173 | return error_mark_node; | |
4174 | } | |
4175 | } | |
4176 | ||
4177 | return cp_parser_postfix_expression (parser, address_p); | |
4178 | } | |
4179 | ||
4180 | /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a | |
4181 | unary-operator, the corresponding tree code is returned. */ | |
4182 | ||
4183 | static enum tree_code | |
94edc4ab | 4184 | cp_parser_unary_operator (cp_token* token) |
a723baf1 MM |
4185 | { |
4186 | switch (token->type) | |
4187 | { | |
4188 | case CPP_MULT: | |
4189 | return INDIRECT_REF; | |
4190 | ||
4191 | case CPP_AND: | |
4192 | return ADDR_EXPR; | |
4193 | ||
4194 | case CPP_PLUS: | |
4195 | return CONVERT_EXPR; | |
4196 | ||
4197 | case CPP_MINUS: | |
4198 | return NEGATE_EXPR; | |
4199 | ||
4200 | case CPP_NOT: | |
4201 | return TRUTH_NOT_EXPR; | |
4202 | ||
4203 | case CPP_COMPL: | |
4204 | return BIT_NOT_EXPR; | |
4205 | ||
4206 | default: | |
4207 | return ERROR_MARK; | |
4208 | } | |
4209 | } | |
4210 | ||
4211 | /* Parse a new-expression. | |
4212 | ||
ca099ac8 | 4213 | new-expression: |
a723baf1 MM |
4214 | :: [opt] new new-placement [opt] new-type-id new-initializer [opt] |
4215 | :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt] | |
4216 | ||
4217 | Returns a representation of the expression. */ | |
4218 | ||
4219 | static tree | |
94edc4ab | 4220 | cp_parser_new_expression (cp_parser* parser) |
a723baf1 MM |
4221 | { |
4222 | bool global_scope_p; | |
4223 | tree placement; | |
4224 | tree type; | |
4225 | tree initializer; | |
4226 | ||
4227 | /* Look for the optional `::' operator. */ | |
4228 | global_scope_p | |
4229 | = (cp_parser_global_scope_opt (parser, | |
4230 | /*current_scope_valid_p=*/false) | |
4231 | != NULL_TREE); | |
4232 | /* Look for the `new' operator. */ | |
4233 | cp_parser_require_keyword (parser, RID_NEW, "`new'"); | |
4234 | /* There's no easy way to tell a new-placement from the | |
4235 | `( type-id )' construct. */ | |
4236 | cp_parser_parse_tentatively (parser); | |
4237 | /* Look for a new-placement. */ | |
4238 | placement = cp_parser_new_placement (parser); | |
4239 | /* If that didn't work out, there's no new-placement. */ | |
4240 | if (!cp_parser_parse_definitely (parser)) | |
4241 | placement = NULL_TREE; | |
4242 | ||
4243 | /* If the next token is a `(', then we have a parenthesized | |
4244 | type-id. */ | |
4245 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4246 | { | |
4247 | /* Consume the `('. */ | |
4248 | cp_lexer_consume_token (parser->lexer); | |
4249 | /* Parse the type-id. */ | |
4250 | type = cp_parser_type_id (parser); | |
4251 | /* Look for the closing `)'. */ | |
4252 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
4253 | } | |
4254 | /* Otherwise, there must be a new-type-id. */ | |
4255 | else | |
4256 | type = cp_parser_new_type_id (parser); | |
4257 | ||
4258 | /* If the next token is a `(', then we have a new-initializer. */ | |
4259 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4260 | initializer = cp_parser_new_initializer (parser); | |
4261 | else | |
4262 | initializer = NULL_TREE; | |
4263 | ||
4264 | /* Create a representation of the new-expression. */ | |
4265 | return build_new (placement, type, initializer, global_scope_p); | |
4266 | } | |
4267 | ||
4268 | /* Parse a new-placement. | |
4269 | ||
4270 | new-placement: | |
4271 | ( expression-list ) | |
4272 | ||
4273 | Returns the same representation as for an expression-list. */ | |
4274 | ||
4275 | static tree | |
94edc4ab | 4276 | cp_parser_new_placement (cp_parser* parser) |
a723baf1 MM |
4277 | { |
4278 | tree expression_list; | |
4279 | ||
a723baf1 | 4280 | /* Parse the expression-list. */ |
39703eb9 MM |
4281 | expression_list = (cp_parser_parenthesized_expression_list |
4282 | (parser, false, /*non_constant_p=*/NULL)); | |
a723baf1 MM |
4283 | |
4284 | return expression_list; | |
4285 | } | |
4286 | ||
4287 | /* Parse a new-type-id. | |
4288 | ||
4289 | new-type-id: | |
4290 | type-specifier-seq new-declarator [opt] | |
4291 | ||
4292 | Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq, | |
4293 | and whose TREE_VALUE is the new-declarator. */ | |
4294 | ||
4295 | static tree | |
94edc4ab | 4296 | cp_parser_new_type_id (cp_parser* parser) |
a723baf1 MM |
4297 | { |
4298 | tree type_specifier_seq; | |
4299 | tree declarator; | |
4300 | const char *saved_message; | |
4301 | ||
4302 | /* The type-specifier sequence must not contain type definitions. | |
4303 | (It cannot contain declarations of new types either, but if they | |
4304 | are not definitions we will catch that because they are not | |
4305 | complete.) */ | |
4306 | saved_message = parser->type_definition_forbidden_message; | |
4307 | parser->type_definition_forbidden_message | |
4308 | = "types may not be defined in a new-type-id"; | |
4309 | /* Parse the type-specifier-seq. */ | |
4310 | type_specifier_seq = cp_parser_type_specifier_seq (parser); | |
4311 | /* Restore the old message. */ | |
4312 | parser->type_definition_forbidden_message = saved_message; | |
4313 | /* Parse the new-declarator. */ | |
4314 | declarator = cp_parser_new_declarator_opt (parser); | |
4315 | ||
4316 | return build_tree_list (type_specifier_seq, declarator); | |
4317 | } | |
4318 | ||
4319 | /* Parse an (optional) new-declarator. | |
4320 | ||
4321 | new-declarator: | |
4322 | ptr-operator new-declarator [opt] | |
4323 | direct-new-declarator | |
4324 | ||
4325 | Returns a representation of the declarator. See | |
4326 | cp_parser_declarator for the representations used. */ | |
4327 | ||
4328 | static tree | |
94edc4ab | 4329 | cp_parser_new_declarator_opt (cp_parser* parser) |
a723baf1 MM |
4330 | { |
4331 | enum tree_code code; | |
4332 | tree type; | |
4333 | tree cv_qualifier_seq; | |
4334 | ||
4335 | /* We don't know if there's a ptr-operator next, or not. */ | |
4336 | cp_parser_parse_tentatively (parser); | |
4337 | /* Look for a ptr-operator. */ | |
4338 | code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq); | |
4339 | /* If that worked, look for more new-declarators. */ | |
4340 | if (cp_parser_parse_definitely (parser)) | |
4341 | { | |
4342 | tree declarator; | |
4343 | ||
4344 | /* Parse another optional declarator. */ | |
4345 | declarator = cp_parser_new_declarator_opt (parser); | |
4346 | ||
4347 | /* Create the representation of the declarator. */ | |
4348 | if (code == INDIRECT_REF) | |
4349 | declarator = make_pointer_declarator (cv_qualifier_seq, | |
4350 | declarator); | |
4351 | else | |
4352 | declarator = make_reference_declarator (cv_qualifier_seq, | |
4353 | declarator); | |
4354 | ||
4355 | /* Handle the pointer-to-member case. */ | |
4356 | if (type) | |
4357 | declarator = build_nt (SCOPE_REF, type, declarator); | |
4358 | ||
4359 | return declarator; | |
4360 | } | |
4361 | ||
4362 | /* If the next token is a `[', there is a direct-new-declarator. */ | |
4363 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
4364 | return cp_parser_direct_new_declarator (parser); | |
4365 | ||
4366 | return NULL_TREE; | |
4367 | } | |
4368 | ||
4369 | /* Parse a direct-new-declarator. | |
4370 | ||
4371 | direct-new-declarator: | |
4372 | [ expression ] | |
4373 | direct-new-declarator [constant-expression] | |
4374 | ||
4375 | Returns an ARRAY_REF, following the same conventions as are | |
4376 | documented for cp_parser_direct_declarator. */ | |
4377 | ||
4378 | static tree | |
94edc4ab | 4379 | cp_parser_direct_new_declarator (cp_parser* parser) |
a723baf1 MM |
4380 | { |
4381 | tree declarator = NULL_TREE; | |
4382 | ||
4383 | while (true) | |
4384 | { | |
4385 | tree expression; | |
4386 | ||
4387 | /* Look for the opening `['. */ | |
4388 | cp_parser_require (parser, CPP_OPEN_SQUARE, "`['"); | |
4389 | /* The first expression is not required to be constant. */ | |
4390 | if (!declarator) | |
4391 | { | |
4392 | expression = cp_parser_expression (parser); | |
4393 | /* The standard requires that the expression have integral | |
4394 | type. DR 74 adds enumeration types. We believe that the | |
4395 | real intent is that these expressions be handled like the | |
4396 | expression in a `switch' condition, which also allows | |
4397 | classes with a single conversion to integral or | |
4398 | enumeration type. */ | |
4399 | if (!processing_template_decl) | |
4400 | { | |
4401 | expression | |
4402 | = build_expr_type_conversion (WANT_INT | WANT_ENUM, | |
4403 | expression, | |
b746c5dc | 4404 | /*complain=*/true); |
a723baf1 MM |
4405 | if (!expression) |
4406 | { | |
4407 | error ("expression in new-declarator must have integral or enumeration type"); | |
4408 | expression = error_mark_node; | |
4409 | } | |
4410 | } | |
4411 | } | |
4412 | /* But all the other expressions must be. */ | |
4413 | else | |
14d22dd6 MM |
4414 | expression |
4415 | = cp_parser_constant_expression (parser, | |
4416 | /*allow_non_constant=*/false, | |
4417 | NULL); | |
a723baf1 MM |
4418 | /* Look for the closing `]'. */ |
4419 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
4420 | ||
4421 | /* Add this bound to the declarator. */ | |
4422 | declarator = build_nt (ARRAY_REF, declarator, expression); | |
4423 | ||
4424 | /* If the next token is not a `[', then there are no more | |
4425 | bounds. */ | |
4426 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) | |
4427 | break; | |
4428 | } | |
4429 | ||
4430 | return declarator; | |
4431 | } | |
4432 | ||
4433 | /* Parse a new-initializer. | |
4434 | ||
4435 | new-initializer: | |
4436 | ( expression-list [opt] ) | |
4437 | ||
34cd5ae7 | 4438 | Returns a representation of the expression-list. If there is no |
a723baf1 MM |
4439 | expression-list, VOID_ZERO_NODE is returned. */ |
4440 | ||
4441 | static tree | |
94edc4ab | 4442 | cp_parser_new_initializer (cp_parser* parser) |
a723baf1 MM |
4443 | { |
4444 | tree expression_list; | |
4445 | ||
39703eb9 MM |
4446 | expression_list = (cp_parser_parenthesized_expression_list |
4447 | (parser, false, /*non_constant_p=*/NULL)); | |
7efa3e22 | 4448 | if (!expression_list) |
a723baf1 | 4449 | expression_list = void_zero_node; |
a723baf1 MM |
4450 | |
4451 | return expression_list; | |
4452 | } | |
4453 | ||
4454 | /* Parse a delete-expression. | |
4455 | ||
4456 | delete-expression: | |
4457 | :: [opt] delete cast-expression | |
4458 | :: [opt] delete [ ] cast-expression | |
4459 | ||
4460 | Returns a representation of the expression. */ | |
4461 | ||
4462 | static tree | |
94edc4ab | 4463 | cp_parser_delete_expression (cp_parser* parser) |
a723baf1 MM |
4464 | { |
4465 | bool global_scope_p; | |
4466 | bool array_p; | |
4467 | tree expression; | |
4468 | ||
4469 | /* Look for the optional `::' operator. */ | |
4470 | global_scope_p | |
4471 | = (cp_parser_global_scope_opt (parser, | |
4472 | /*current_scope_valid_p=*/false) | |
4473 | != NULL_TREE); | |
4474 | /* Look for the `delete' keyword. */ | |
4475 | cp_parser_require_keyword (parser, RID_DELETE, "`delete'"); | |
4476 | /* See if the array syntax is in use. */ | |
4477 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
4478 | { | |
4479 | /* Consume the `[' token. */ | |
4480 | cp_lexer_consume_token (parser->lexer); | |
4481 | /* Look for the `]' token. */ | |
4482 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
4483 | /* Remember that this is the `[]' construct. */ | |
4484 | array_p = true; | |
4485 | } | |
4486 | else | |
4487 | array_p = false; | |
4488 | ||
4489 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4490 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4491 | |
4492 | return delete_sanity (expression, NULL_TREE, array_p, global_scope_p); | |
4493 | } | |
4494 | ||
4495 | /* Parse a cast-expression. | |
4496 | ||
4497 | cast-expression: | |
4498 | unary-expression | |
4499 | ( type-id ) cast-expression | |
4500 | ||
4501 | Returns a representation of the expression. */ | |
4502 | ||
4503 | static tree | |
4504 | cp_parser_cast_expression (cp_parser *parser, bool address_p) | |
4505 | { | |
4506 | /* If it's a `(', then we might be looking at a cast. */ | |
4507 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4508 | { | |
4509 | tree type = NULL_TREE; | |
4510 | tree expr = NULL_TREE; | |
4511 | bool compound_literal_p; | |
4512 | const char *saved_message; | |
4513 | ||
4514 | /* There's no way to know yet whether or not this is a cast. | |
4515 | For example, `(int (3))' is a unary-expression, while `(int) | |
4516 | 3' is a cast. So, we resort to parsing tentatively. */ | |
4517 | cp_parser_parse_tentatively (parser); | |
4518 | /* Types may not be defined in a cast. */ | |
4519 | saved_message = parser->type_definition_forbidden_message; | |
4520 | parser->type_definition_forbidden_message | |
4521 | = "types may not be defined in casts"; | |
4522 | /* Consume the `('. */ | |
4523 | cp_lexer_consume_token (parser->lexer); | |
4524 | /* A very tricky bit is that `(struct S) { 3 }' is a | |
4525 | compound-literal (which we permit in C++ as an extension). | |
4526 | But, that construct is not a cast-expression -- it is a | |
4527 | postfix-expression. (The reason is that `(struct S) { 3 }.i' | |
4528 | is legal; if the compound-literal were a cast-expression, | |
4529 | you'd need an extra set of parentheses.) But, if we parse | |
4530 | the type-id, and it happens to be a class-specifier, then we | |
4531 | will commit to the parse at that point, because we cannot | |
4532 | undo the action that is done when creating a new class. So, | |
4533 | then we cannot back up and do a postfix-expression. | |
4534 | ||
4535 | Therefore, we scan ahead to the closing `)', and check to see | |
4536 | if the token after the `)' is a `{'. If so, we are not | |
4537 | looking at a cast-expression. | |
4538 | ||
4539 | Save tokens so that we can put them back. */ | |
4540 | cp_lexer_save_tokens (parser->lexer); | |
4541 | /* Skip tokens until the next token is a closing parenthesis. | |
4542 | If we find the closing `)', and the next token is a `{', then | |
4543 | we are looking at a compound-literal. */ | |
4544 | compound_literal_p | |
a668c6ad MM |
4545 | = (cp_parser_skip_to_closing_parenthesis (parser, false, false, |
4546 | /*consume_paren=*/true) | |
a723baf1 MM |
4547 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)); |
4548 | /* Roll back the tokens we skipped. */ | |
4549 | cp_lexer_rollback_tokens (parser->lexer); | |
4550 | /* If we were looking at a compound-literal, simulate an error | |
4551 | so that the call to cp_parser_parse_definitely below will | |
4552 | fail. */ | |
4553 | if (compound_literal_p) | |
4554 | cp_parser_simulate_error (parser); | |
4555 | else | |
4556 | { | |
4557 | /* Look for the type-id. */ | |
4558 | type = cp_parser_type_id (parser); | |
4559 | /* Look for the closing `)'. */ | |
4560 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
4561 | } | |
4562 | ||
4563 | /* Restore the saved message. */ | |
4564 | parser->type_definition_forbidden_message = saved_message; | |
4565 | ||
bbaab916 NS |
4566 | /* If ok so far, parse the dependent expression. We cannot be |
4567 | sure it is a cast. Consider `(T ())'. It is a parenthesized | |
4568 | ctor of T, but looks like a cast to function returning T | |
4569 | without a dependent expression. */ | |
4570 | if (!cp_parser_error_occurred (parser)) | |
d6b4ea85 | 4571 | expr = cp_parser_simple_cast_expression (parser); |
bbaab916 | 4572 | |
a723baf1 MM |
4573 | if (cp_parser_parse_definitely (parser)) |
4574 | { | |
a723baf1 MM |
4575 | /* Warn about old-style casts, if so requested. */ |
4576 | if (warn_old_style_cast | |
4577 | && !in_system_header | |
4578 | && !VOID_TYPE_P (type) | |
4579 | && current_lang_name != lang_name_c) | |
4580 | warning ("use of old-style cast"); | |
14d22dd6 MM |
4581 | |
4582 | /* Only type conversions to integral or enumeration types | |
4583 | can be used in constant-expressions. */ | |
4584 | if (parser->constant_expression_p | |
4585 | && !dependent_type_p (type) | |
4586 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)) | |
4587 | { | |
4588 | if (!parser->allow_non_constant_expression_p) | |
4589 | return (cp_parser_non_constant_expression | |
4590 | ("a casts to a type other than an integral or " | |
4591 | "enumeration type")); | |
4592 | parser->non_constant_expression_p = true; | |
4593 | } | |
a723baf1 MM |
4594 | /* Perform the cast. */ |
4595 | expr = build_c_cast (type, expr); | |
bbaab916 | 4596 | return expr; |
a723baf1 | 4597 | } |
a723baf1 MM |
4598 | } |
4599 | ||
4600 | /* If we get here, then it's not a cast, so it must be a | |
4601 | unary-expression. */ | |
4602 | return cp_parser_unary_expression (parser, address_p); | |
4603 | } | |
4604 | ||
4605 | /* Parse a pm-expression. | |
4606 | ||
4607 | pm-expression: | |
4608 | cast-expression | |
4609 | pm-expression .* cast-expression | |
4610 | pm-expression ->* cast-expression | |
4611 | ||
4612 | Returns a representation of the expression. */ | |
4613 | ||
4614 | static tree | |
94edc4ab | 4615 | cp_parser_pm_expression (cp_parser* parser) |
a723baf1 | 4616 | { |
d6b4ea85 MM |
4617 | static const cp_parser_token_tree_map map = { |
4618 | { CPP_DEREF_STAR, MEMBER_REF }, | |
4619 | { CPP_DOT_STAR, DOTSTAR_EXPR }, | |
4620 | { CPP_EOF, ERROR_MARK } | |
4621 | }; | |
a723baf1 | 4622 | |
d6b4ea85 MM |
4623 | return cp_parser_binary_expression (parser, map, |
4624 | cp_parser_simple_cast_expression); | |
a723baf1 MM |
4625 | } |
4626 | ||
4627 | /* Parse a multiplicative-expression. | |
4628 | ||
4629 | mulitplicative-expression: | |
4630 | pm-expression | |
4631 | multiplicative-expression * pm-expression | |
4632 | multiplicative-expression / pm-expression | |
4633 | multiplicative-expression % pm-expression | |
4634 | ||
4635 | Returns a representation of the expression. */ | |
4636 | ||
4637 | static tree | |
94edc4ab | 4638 | cp_parser_multiplicative_expression (cp_parser* parser) |
a723baf1 | 4639 | { |
39b1af70 | 4640 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4641 | { CPP_MULT, MULT_EXPR }, |
4642 | { CPP_DIV, TRUNC_DIV_EXPR }, | |
4643 | { CPP_MOD, TRUNC_MOD_EXPR }, | |
4644 | { CPP_EOF, ERROR_MARK } | |
4645 | }; | |
4646 | ||
4647 | return cp_parser_binary_expression (parser, | |
4648 | map, | |
4649 | cp_parser_pm_expression); | |
4650 | } | |
4651 | ||
4652 | /* Parse an additive-expression. | |
4653 | ||
4654 | additive-expression: | |
4655 | multiplicative-expression | |
4656 | additive-expression + multiplicative-expression | |
4657 | additive-expression - multiplicative-expression | |
4658 | ||
4659 | Returns a representation of the expression. */ | |
4660 | ||
4661 | static tree | |
94edc4ab | 4662 | cp_parser_additive_expression (cp_parser* parser) |
a723baf1 | 4663 | { |
39b1af70 | 4664 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4665 | { CPP_PLUS, PLUS_EXPR }, |
4666 | { CPP_MINUS, MINUS_EXPR }, | |
4667 | { CPP_EOF, ERROR_MARK } | |
4668 | }; | |
4669 | ||
4670 | return cp_parser_binary_expression (parser, | |
4671 | map, | |
4672 | cp_parser_multiplicative_expression); | |
4673 | } | |
4674 | ||
4675 | /* Parse a shift-expression. | |
4676 | ||
4677 | shift-expression: | |
4678 | additive-expression | |
4679 | shift-expression << additive-expression | |
4680 | shift-expression >> additive-expression | |
4681 | ||
4682 | Returns a representation of the expression. */ | |
4683 | ||
4684 | static tree | |
94edc4ab | 4685 | cp_parser_shift_expression (cp_parser* parser) |
a723baf1 | 4686 | { |
39b1af70 | 4687 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4688 | { CPP_LSHIFT, LSHIFT_EXPR }, |
4689 | { CPP_RSHIFT, RSHIFT_EXPR }, | |
4690 | { CPP_EOF, ERROR_MARK } | |
4691 | }; | |
4692 | ||
4693 | return cp_parser_binary_expression (parser, | |
4694 | map, | |
4695 | cp_parser_additive_expression); | |
4696 | } | |
4697 | ||
4698 | /* Parse a relational-expression. | |
4699 | ||
4700 | relational-expression: | |
4701 | shift-expression | |
4702 | relational-expression < shift-expression | |
4703 | relational-expression > shift-expression | |
4704 | relational-expression <= shift-expression | |
4705 | relational-expression >= shift-expression | |
4706 | ||
4707 | GNU Extension: | |
4708 | ||
4709 | relational-expression: | |
4710 | relational-expression <? shift-expression | |
4711 | relational-expression >? shift-expression | |
4712 | ||
4713 | Returns a representation of the expression. */ | |
4714 | ||
4715 | static tree | |
94edc4ab | 4716 | cp_parser_relational_expression (cp_parser* parser) |
a723baf1 | 4717 | { |
39b1af70 | 4718 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4719 | { CPP_LESS, LT_EXPR }, |
4720 | { CPP_GREATER, GT_EXPR }, | |
4721 | { CPP_LESS_EQ, LE_EXPR }, | |
4722 | { CPP_GREATER_EQ, GE_EXPR }, | |
4723 | { CPP_MIN, MIN_EXPR }, | |
4724 | { CPP_MAX, MAX_EXPR }, | |
4725 | { CPP_EOF, ERROR_MARK } | |
4726 | }; | |
4727 | ||
4728 | return cp_parser_binary_expression (parser, | |
4729 | map, | |
4730 | cp_parser_shift_expression); | |
4731 | } | |
4732 | ||
4733 | /* Parse an equality-expression. | |
4734 | ||
4735 | equality-expression: | |
4736 | relational-expression | |
4737 | equality-expression == relational-expression | |
4738 | equality-expression != relational-expression | |
4739 | ||
4740 | Returns a representation of the expression. */ | |
4741 | ||
4742 | static tree | |
94edc4ab | 4743 | cp_parser_equality_expression (cp_parser* parser) |
a723baf1 | 4744 | { |
39b1af70 | 4745 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4746 | { CPP_EQ_EQ, EQ_EXPR }, |
4747 | { CPP_NOT_EQ, NE_EXPR }, | |
4748 | { CPP_EOF, ERROR_MARK } | |
4749 | }; | |
4750 | ||
4751 | return cp_parser_binary_expression (parser, | |
4752 | map, | |
4753 | cp_parser_relational_expression); | |
4754 | } | |
4755 | ||
4756 | /* Parse an and-expression. | |
4757 | ||
4758 | and-expression: | |
4759 | equality-expression | |
4760 | and-expression & equality-expression | |
4761 | ||
4762 | Returns a representation of the expression. */ | |
4763 | ||
4764 | static tree | |
94edc4ab | 4765 | cp_parser_and_expression (cp_parser* parser) |
a723baf1 | 4766 | { |
39b1af70 | 4767 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4768 | { CPP_AND, BIT_AND_EXPR }, |
4769 | { CPP_EOF, ERROR_MARK } | |
4770 | }; | |
4771 | ||
4772 | return cp_parser_binary_expression (parser, | |
4773 | map, | |
4774 | cp_parser_equality_expression); | |
4775 | } | |
4776 | ||
4777 | /* Parse an exclusive-or-expression. | |
4778 | ||
4779 | exclusive-or-expression: | |
4780 | and-expression | |
4781 | exclusive-or-expression ^ and-expression | |
4782 | ||
4783 | Returns a representation of the expression. */ | |
4784 | ||
4785 | static tree | |
94edc4ab | 4786 | cp_parser_exclusive_or_expression (cp_parser* parser) |
a723baf1 | 4787 | { |
39b1af70 | 4788 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4789 | { CPP_XOR, BIT_XOR_EXPR }, |
4790 | { CPP_EOF, ERROR_MARK } | |
4791 | }; | |
4792 | ||
4793 | return cp_parser_binary_expression (parser, | |
4794 | map, | |
4795 | cp_parser_and_expression); | |
4796 | } | |
4797 | ||
4798 | ||
4799 | /* Parse an inclusive-or-expression. | |
4800 | ||
4801 | inclusive-or-expression: | |
4802 | exclusive-or-expression | |
4803 | inclusive-or-expression | exclusive-or-expression | |
4804 | ||
4805 | Returns a representation of the expression. */ | |
4806 | ||
4807 | static tree | |
94edc4ab | 4808 | cp_parser_inclusive_or_expression (cp_parser* parser) |
a723baf1 | 4809 | { |
39b1af70 | 4810 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4811 | { CPP_OR, BIT_IOR_EXPR }, |
4812 | { CPP_EOF, ERROR_MARK } | |
4813 | }; | |
4814 | ||
4815 | return cp_parser_binary_expression (parser, | |
4816 | map, | |
4817 | cp_parser_exclusive_or_expression); | |
4818 | } | |
4819 | ||
4820 | /* Parse a logical-and-expression. | |
4821 | ||
4822 | logical-and-expression: | |
4823 | inclusive-or-expression | |
4824 | logical-and-expression && inclusive-or-expression | |
4825 | ||
4826 | Returns a representation of the expression. */ | |
4827 | ||
4828 | static tree | |
94edc4ab | 4829 | cp_parser_logical_and_expression (cp_parser* parser) |
a723baf1 | 4830 | { |
39b1af70 | 4831 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4832 | { CPP_AND_AND, TRUTH_ANDIF_EXPR }, |
4833 | { CPP_EOF, ERROR_MARK } | |
4834 | }; | |
4835 | ||
4836 | return cp_parser_binary_expression (parser, | |
4837 | map, | |
4838 | cp_parser_inclusive_or_expression); | |
4839 | } | |
4840 | ||
4841 | /* Parse a logical-or-expression. | |
4842 | ||
4843 | logical-or-expression: | |
34cd5ae7 | 4844 | logical-and-expression |
a723baf1 MM |
4845 | logical-or-expression || logical-and-expression |
4846 | ||
4847 | Returns a representation of the expression. */ | |
4848 | ||
4849 | static tree | |
94edc4ab | 4850 | cp_parser_logical_or_expression (cp_parser* parser) |
a723baf1 | 4851 | { |
39b1af70 | 4852 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4853 | { CPP_OR_OR, TRUTH_ORIF_EXPR }, |
4854 | { CPP_EOF, ERROR_MARK } | |
4855 | }; | |
4856 | ||
4857 | return cp_parser_binary_expression (parser, | |
4858 | map, | |
4859 | cp_parser_logical_and_expression); | |
4860 | } | |
4861 | ||
a723baf1 MM |
4862 | /* Parse the `? expression : assignment-expression' part of a |
4863 | conditional-expression. The LOGICAL_OR_EXPR is the | |
4864 | logical-or-expression that started the conditional-expression. | |
4865 | Returns a representation of the entire conditional-expression. | |
4866 | ||
39703eb9 | 4867 | This routine is used by cp_parser_assignment_expression. |
a723baf1 MM |
4868 | |
4869 | ? expression : assignment-expression | |
4870 | ||
4871 | GNU Extensions: | |
4872 | ||
4873 | ? : assignment-expression */ | |
4874 | ||
4875 | static tree | |
94edc4ab | 4876 | cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr) |
a723baf1 MM |
4877 | { |
4878 | tree expr; | |
4879 | tree assignment_expr; | |
4880 | ||
4881 | /* Consume the `?' token. */ | |
4882 | cp_lexer_consume_token (parser->lexer); | |
4883 | if (cp_parser_allow_gnu_extensions_p (parser) | |
4884 | && cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
4885 | /* Implicit true clause. */ | |
4886 | expr = NULL_TREE; | |
4887 | else | |
4888 | /* Parse the expression. */ | |
4889 | expr = cp_parser_expression (parser); | |
4890 | ||
4891 | /* The next token should be a `:'. */ | |
4892 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
4893 | /* Parse the assignment-expression. */ | |
4894 | assignment_expr = cp_parser_assignment_expression (parser); | |
4895 | ||
4896 | /* Build the conditional-expression. */ | |
4897 | return build_x_conditional_expr (logical_or_expr, | |
4898 | expr, | |
4899 | assignment_expr); | |
4900 | } | |
4901 | ||
4902 | /* Parse an assignment-expression. | |
4903 | ||
4904 | assignment-expression: | |
4905 | conditional-expression | |
4906 | logical-or-expression assignment-operator assignment_expression | |
4907 | throw-expression | |
4908 | ||
4909 | Returns a representation for the expression. */ | |
4910 | ||
4911 | static tree | |
94edc4ab | 4912 | cp_parser_assignment_expression (cp_parser* parser) |
a723baf1 MM |
4913 | { |
4914 | tree expr; | |
4915 | ||
4916 | /* If the next token is the `throw' keyword, then we're looking at | |
4917 | a throw-expression. */ | |
4918 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW)) | |
4919 | expr = cp_parser_throw_expression (parser); | |
4920 | /* Otherwise, it must be that we are looking at a | |
4921 | logical-or-expression. */ | |
4922 | else | |
4923 | { | |
4924 | /* Parse the logical-or-expression. */ | |
4925 | expr = cp_parser_logical_or_expression (parser); | |
4926 | /* If the next token is a `?' then we're actually looking at a | |
4927 | conditional-expression. */ | |
4928 | if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY)) | |
4929 | return cp_parser_question_colon_clause (parser, expr); | |
4930 | else | |
4931 | { | |
4932 | enum tree_code assignment_operator; | |
4933 | ||
4934 | /* If it's an assignment-operator, we're using the second | |
4935 | production. */ | |
4936 | assignment_operator | |
4937 | = cp_parser_assignment_operator_opt (parser); | |
4938 | if (assignment_operator != ERROR_MARK) | |
4939 | { | |
4940 | tree rhs; | |
4941 | ||
4942 | /* Parse the right-hand side of the assignment. */ | |
4943 | rhs = cp_parser_assignment_expression (parser); | |
14d22dd6 MM |
4944 | /* An assignment may not appear in a |
4945 | constant-expression. */ | |
4946 | if (parser->constant_expression_p) | |
4947 | { | |
4948 | if (!parser->allow_non_constant_expression_p) | |
4949 | return cp_parser_non_constant_expression ("an assignment"); | |
4950 | parser->non_constant_expression_p = true; | |
4951 | } | |
34cd5ae7 | 4952 | /* Build the assignment expression. */ |
a723baf1 MM |
4953 | expr = build_x_modify_expr (expr, |
4954 | assignment_operator, | |
4955 | rhs); | |
4956 | } | |
4957 | } | |
4958 | } | |
4959 | ||
4960 | return expr; | |
4961 | } | |
4962 | ||
4963 | /* Parse an (optional) assignment-operator. | |
4964 | ||
4965 | assignment-operator: one of | |
4966 | = *= /= %= += -= >>= <<= &= ^= |= | |
4967 | ||
4968 | GNU Extension: | |
4969 | ||
4970 | assignment-operator: one of | |
4971 | <?= >?= | |
4972 | ||
4973 | If the next token is an assignment operator, the corresponding tree | |
4974 | code is returned, and the token is consumed. For example, for | |
4975 | `+=', PLUS_EXPR is returned. For `=' itself, the code returned is | |
4976 | NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%', | |
4977 | TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment | |
4978 | operator, ERROR_MARK is returned. */ | |
4979 | ||
4980 | static enum tree_code | |
94edc4ab | 4981 | cp_parser_assignment_operator_opt (cp_parser* parser) |
a723baf1 MM |
4982 | { |
4983 | enum tree_code op; | |
4984 | cp_token *token; | |
4985 | ||
4986 | /* Peek at the next toen. */ | |
4987 | token = cp_lexer_peek_token (parser->lexer); | |
4988 | ||
4989 | switch (token->type) | |
4990 | { | |
4991 | case CPP_EQ: | |
4992 | op = NOP_EXPR; | |
4993 | break; | |
4994 | ||
4995 | case CPP_MULT_EQ: | |
4996 | op = MULT_EXPR; | |
4997 | break; | |
4998 | ||
4999 | case CPP_DIV_EQ: | |
5000 | op = TRUNC_DIV_EXPR; | |
5001 | break; | |
5002 | ||
5003 | case CPP_MOD_EQ: | |
5004 | op = TRUNC_MOD_EXPR; | |
5005 | break; | |
5006 | ||
5007 | case CPP_PLUS_EQ: | |
5008 | op = PLUS_EXPR; | |
5009 | break; | |
5010 | ||
5011 | case CPP_MINUS_EQ: | |
5012 | op = MINUS_EXPR; | |
5013 | break; | |
5014 | ||
5015 | case CPP_RSHIFT_EQ: | |
5016 | op = RSHIFT_EXPR; | |
5017 | break; | |
5018 | ||
5019 | case CPP_LSHIFT_EQ: | |
5020 | op = LSHIFT_EXPR; | |
5021 | break; | |
5022 | ||
5023 | case CPP_AND_EQ: | |
5024 | op = BIT_AND_EXPR; | |
5025 | break; | |
5026 | ||
5027 | case CPP_XOR_EQ: | |
5028 | op = BIT_XOR_EXPR; | |
5029 | break; | |
5030 | ||
5031 | case CPP_OR_EQ: | |
5032 | op = BIT_IOR_EXPR; | |
5033 | break; | |
5034 | ||
5035 | case CPP_MIN_EQ: | |
5036 | op = MIN_EXPR; | |
5037 | break; | |
5038 | ||
5039 | case CPP_MAX_EQ: | |
5040 | op = MAX_EXPR; | |
5041 | break; | |
5042 | ||
5043 | default: | |
5044 | /* Nothing else is an assignment operator. */ | |
5045 | op = ERROR_MARK; | |
5046 | } | |
5047 | ||
5048 | /* If it was an assignment operator, consume it. */ | |
5049 | if (op != ERROR_MARK) | |
5050 | cp_lexer_consume_token (parser->lexer); | |
5051 | ||
5052 | return op; | |
5053 | } | |
5054 | ||
5055 | /* Parse an expression. | |
5056 | ||
5057 | expression: | |
5058 | assignment-expression | |
5059 | expression , assignment-expression | |
5060 | ||
5061 | Returns a representation of the expression. */ | |
5062 | ||
5063 | static tree | |
94edc4ab | 5064 | cp_parser_expression (cp_parser* parser) |
a723baf1 MM |
5065 | { |
5066 | tree expression = NULL_TREE; | |
a723baf1 MM |
5067 | |
5068 | while (true) | |
5069 | { | |
5070 | tree assignment_expression; | |
5071 | ||
5072 | /* Parse the next assignment-expression. */ | |
5073 | assignment_expression | |
5074 | = cp_parser_assignment_expression (parser); | |
5075 | /* If this is the first assignment-expression, we can just | |
5076 | save it away. */ | |
5077 | if (!expression) | |
5078 | expression = assignment_expression; | |
a723baf1 | 5079 | else |
d17811fd MM |
5080 | expression = build_x_compound_expr (expression, |
5081 | assignment_expression); | |
a723baf1 MM |
5082 | /* If the next token is not a comma, then we are done with the |
5083 | expression. */ | |
5084 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
5085 | break; | |
5086 | /* Consume the `,'. */ | |
5087 | cp_lexer_consume_token (parser->lexer); | |
14d22dd6 MM |
5088 | /* A comma operator cannot appear in a constant-expression. */ |
5089 | if (parser->constant_expression_p) | |
5090 | { | |
5091 | if (!parser->allow_non_constant_expression_p) | |
d17811fd MM |
5092 | expression |
5093 | = cp_parser_non_constant_expression ("a comma operator"); | |
14d22dd6 MM |
5094 | parser->non_constant_expression_p = true; |
5095 | } | |
14d22dd6 | 5096 | } |
a723baf1 MM |
5097 | |
5098 | return expression; | |
5099 | } | |
5100 | ||
5101 | /* Parse a constant-expression. | |
5102 | ||
5103 | constant-expression: | |
14d22dd6 MM |
5104 | conditional-expression |
5105 | ||
5106 | If ALLOW_NON_CONSTANT_P a non-constant expression is silently | |
d17811fd MM |
5107 | accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not |
5108 | constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P | |
5109 | is false, NON_CONSTANT_P should be NULL. */ | |
a723baf1 MM |
5110 | |
5111 | static tree | |
14d22dd6 MM |
5112 | cp_parser_constant_expression (cp_parser* parser, |
5113 | bool allow_non_constant_p, | |
5114 | bool *non_constant_p) | |
a723baf1 MM |
5115 | { |
5116 | bool saved_constant_expression_p; | |
14d22dd6 MM |
5117 | bool saved_allow_non_constant_expression_p; |
5118 | bool saved_non_constant_expression_p; | |
a723baf1 MM |
5119 | tree expression; |
5120 | ||
5121 | /* It might seem that we could simply parse the | |
5122 | conditional-expression, and then check to see if it were | |
5123 | TREE_CONSTANT. However, an expression that is TREE_CONSTANT is | |
5124 | one that the compiler can figure out is constant, possibly after | |
5125 | doing some simplifications or optimizations. The standard has a | |
5126 | precise definition of constant-expression, and we must honor | |
5127 | that, even though it is somewhat more restrictive. | |
5128 | ||
5129 | For example: | |
5130 | ||
5131 | int i[(2, 3)]; | |
5132 | ||
5133 | is not a legal declaration, because `(2, 3)' is not a | |
5134 | constant-expression. The `,' operator is forbidden in a | |
5135 | constant-expression. However, GCC's constant-folding machinery | |
5136 | will fold this operation to an INTEGER_CST for `3'. */ | |
5137 | ||
14d22dd6 | 5138 | /* Save the old settings. */ |
a723baf1 | 5139 | saved_constant_expression_p = parser->constant_expression_p; |
14d22dd6 MM |
5140 | saved_allow_non_constant_expression_p |
5141 | = parser->allow_non_constant_expression_p; | |
5142 | saved_non_constant_expression_p = parser->non_constant_expression_p; | |
a723baf1 MM |
5143 | /* We are now parsing a constant-expression. */ |
5144 | parser->constant_expression_p = true; | |
14d22dd6 MM |
5145 | parser->allow_non_constant_expression_p = allow_non_constant_p; |
5146 | parser->non_constant_expression_p = false; | |
39703eb9 MM |
5147 | /* Although the grammar says "conditional-expression", we parse an |
5148 | "assignment-expression", which also permits "throw-expression" | |
5149 | and the use of assignment operators. In the case that | |
5150 | ALLOW_NON_CONSTANT_P is false, we get better errors than we would | |
5151 | otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is | |
5152 | actually essential that we look for an assignment-expression. | |
5153 | For example, cp_parser_initializer_clauses uses this function to | |
5154 | determine whether a particular assignment-expression is in fact | |
5155 | constant. */ | |
5156 | expression = cp_parser_assignment_expression (parser); | |
14d22dd6 | 5157 | /* Restore the old settings. */ |
a723baf1 | 5158 | parser->constant_expression_p = saved_constant_expression_p; |
14d22dd6 MM |
5159 | parser->allow_non_constant_expression_p |
5160 | = saved_allow_non_constant_expression_p; | |
5161 | if (allow_non_constant_p) | |
5162 | *non_constant_p = parser->non_constant_expression_p; | |
5163 | parser->non_constant_expression_p = saved_non_constant_expression_p; | |
a723baf1 MM |
5164 | |
5165 | return expression; | |
5166 | } | |
5167 | ||
5168 | /* Statements [gram.stmt.stmt] */ | |
5169 | ||
5170 | /* Parse a statement. | |
5171 | ||
5172 | statement: | |
5173 | labeled-statement | |
5174 | expression-statement | |
5175 | compound-statement | |
5176 | selection-statement | |
5177 | iteration-statement | |
5178 | jump-statement | |
5179 | declaration-statement | |
5180 | try-block */ | |
5181 | ||
5182 | static void | |
a5bcc582 | 5183 | cp_parser_statement (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 MM |
5184 | { |
5185 | tree statement; | |
5186 | cp_token *token; | |
5187 | int statement_line_number; | |
5188 | ||
5189 | /* There is no statement yet. */ | |
5190 | statement = NULL_TREE; | |
5191 | /* Peek at the next token. */ | |
5192 | token = cp_lexer_peek_token (parser->lexer); | |
5193 | /* Remember the line number of the first token in the statement. */ | |
82a98427 | 5194 | statement_line_number = token->location.line; |
a723baf1 MM |
5195 | /* If this is a keyword, then that will often determine what kind of |
5196 | statement we have. */ | |
5197 | if (token->type == CPP_KEYWORD) | |
5198 | { | |
5199 | enum rid keyword = token->keyword; | |
5200 | ||
5201 | switch (keyword) | |
5202 | { | |
5203 | case RID_CASE: | |
5204 | case RID_DEFAULT: | |
a5bcc582 NS |
5205 | statement = cp_parser_labeled_statement (parser, |
5206 | in_statement_expr_p); | |
a723baf1 MM |
5207 | break; |
5208 | ||
5209 | case RID_IF: | |
5210 | case RID_SWITCH: | |
5211 | statement = cp_parser_selection_statement (parser); | |
5212 | break; | |
5213 | ||
5214 | case RID_WHILE: | |
5215 | case RID_DO: | |
5216 | case RID_FOR: | |
5217 | statement = cp_parser_iteration_statement (parser); | |
5218 | break; | |
5219 | ||
5220 | case RID_BREAK: | |
5221 | case RID_CONTINUE: | |
5222 | case RID_RETURN: | |
5223 | case RID_GOTO: | |
5224 | statement = cp_parser_jump_statement (parser); | |
5225 | break; | |
5226 | ||
5227 | case RID_TRY: | |
5228 | statement = cp_parser_try_block (parser); | |
5229 | break; | |
5230 | ||
5231 | default: | |
5232 | /* It might be a keyword like `int' that can start a | |
5233 | declaration-statement. */ | |
5234 | break; | |
5235 | } | |
5236 | } | |
5237 | else if (token->type == CPP_NAME) | |
5238 | { | |
5239 | /* If the next token is a `:', then we are looking at a | |
5240 | labeled-statement. */ | |
5241 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
5242 | if (token->type == CPP_COLON) | |
a5bcc582 | 5243 | statement = cp_parser_labeled_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5244 | } |
5245 | /* Anything that starts with a `{' must be a compound-statement. */ | |
5246 | else if (token->type == CPP_OPEN_BRACE) | |
a5bcc582 | 5247 | statement = cp_parser_compound_statement (parser, false); |
a723baf1 MM |
5248 | |
5249 | /* Everything else must be a declaration-statement or an | |
5250 | expression-statement. Try for the declaration-statement | |
5251 | first, unless we are looking at a `;', in which case we know that | |
5252 | we have an expression-statement. */ | |
5253 | if (!statement) | |
5254 | { | |
5255 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5256 | { | |
5257 | cp_parser_parse_tentatively (parser); | |
5258 | /* Try to parse the declaration-statement. */ | |
5259 | cp_parser_declaration_statement (parser); | |
5260 | /* If that worked, we're done. */ | |
5261 | if (cp_parser_parse_definitely (parser)) | |
5262 | return; | |
5263 | } | |
5264 | /* Look for an expression-statement instead. */ | |
a5bcc582 | 5265 | statement = cp_parser_expression_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5266 | } |
5267 | ||
5268 | /* Set the line number for the statement. */ | |
009ed910 | 5269 | if (statement && STATEMENT_CODE_P (TREE_CODE (statement))) |
a723baf1 MM |
5270 | STMT_LINENO (statement) = statement_line_number; |
5271 | } | |
5272 | ||
5273 | /* Parse a labeled-statement. | |
5274 | ||
5275 | labeled-statement: | |
5276 | identifier : statement | |
5277 | case constant-expression : statement | |
5278 | default : statement | |
5279 | ||
5280 | Returns the new CASE_LABEL, for a `case' or `default' label. For | |
5281 | an ordinary label, returns a LABEL_STMT. */ | |
5282 | ||
5283 | static tree | |
a5bcc582 | 5284 | cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 MM |
5285 | { |
5286 | cp_token *token; | |
0e59b3fb | 5287 | tree statement = error_mark_node; |
a723baf1 MM |
5288 | |
5289 | /* The next token should be an identifier. */ | |
5290 | token = cp_lexer_peek_token (parser->lexer); | |
5291 | if (token->type != CPP_NAME | |
5292 | && token->type != CPP_KEYWORD) | |
5293 | { | |
5294 | cp_parser_error (parser, "expected labeled-statement"); | |
5295 | return error_mark_node; | |
5296 | } | |
5297 | ||
5298 | switch (token->keyword) | |
5299 | { | |
5300 | case RID_CASE: | |
5301 | { | |
5302 | tree expr; | |
5303 | ||
5304 | /* Consume the `case' token. */ | |
5305 | cp_lexer_consume_token (parser->lexer); | |
5306 | /* Parse the constant-expression. */ | |
14d22dd6 | 5307 | expr = cp_parser_constant_expression (parser, |
d17811fd | 5308 | /*allow_non_constant_p=*/false, |
14d22dd6 | 5309 | NULL); |
0e59b3fb MM |
5310 | if (!parser->in_switch_statement_p) |
5311 | error ("case label `%E' not within a switch statement", expr); | |
5312 | else | |
5313 | statement = finish_case_label (expr, NULL_TREE); | |
a723baf1 MM |
5314 | } |
5315 | break; | |
5316 | ||
5317 | case RID_DEFAULT: | |
5318 | /* Consume the `default' token. */ | |
5319 | cp_lexer_consume_token (parser->lexer); | |
0e59b3fb MM |
5320 | if (!parser->in_switch_statement_p) |
5321 | error ("case label not within a switch statement"); | |
5322 | else | |
5323 | statement = finish_case_label (NULL_TREE, NULL_TREE); | |
a723baf1 MM |
5324 | break; |
5325 | ||
5326 | default: | |
5327 | /* Anything else must be an ordinary label. */ | |
5328 | statement = finish_label_stmt (cp_parser_identifier (parser)); | |
5329 | break; | |
5330 | } | |
5331 | ||
5332 | /* Require the `:' token. */ | |
5333 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
5334 | /* Parse the labeled statement. */ | |
a5bcc582 | 5335 | cp_parser_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5336 | |
5337 | /* Return the label, in the case of a `case' or `default' label. */ | |
5338 | return statement; | |
5339 | } | |
5340 | ||
5341 | /* Parse an expression-statement. | |
5342 | ||
5343 | expression-statement: | |
5344 | expression [opt] ; | |
5345 | ||
5346 | Returns the new EXPR_STMT -- or NULL_TREE if the expression | |
a5bcc582 NS |
5347 | statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P |
5348 | indicates whether this expression-statement is part of an | |
5349 | expression statement. */ | |
a723baf1 MM |
5350 | |
5351 | static tree | |
a5bcc582 | 5352 | cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 | 5353 | { |
a5bcc582 | 5354 | tree statement = NULL_TREE; |
a723baf1 | 5355 | |
a5bcc582 | 5356 | /* If the next token is a ';', then there is no expression |
04c06002 | 5357 | statement. */ |
a723baf1 | 5358 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) |
a5bcc582 NS |
5359 | statement = cp_parser_expression (parser); |
5360 | ||
a723baf1 | 5361 | /* Consume the final `;'. */ |
e0860732 | 5362 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 | 5363 | |
a5bcc582 NS |
5364 | if (in_statement_expr_p |
5365 | && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
5366 | { | |
5367 | /* This is the final expression statement of a statement | |
5368 | expression. */ | |
5369 | statement = finish_stmt_expr_expr (statement); | |
5370 | } | |
5371 | else if (statement) | |
5372 | statement = finish_expr_stmt (statement); | |
5373 | else | |
5374 | finish_stmt (); | |
5375 | ||
a723baf1 MM |
5376 | return statement; |
5377 | } | |
5378 | ||
5379 | /* Parse a compound-statement. | |
5380 | ||
5381 | compound-statement: | |
5382 | { statement-seq [opt] } | |
5383 | ||
5384 | Returns a COMPOUND_STMT representing the statement. */ | |
5385 | ||
5386 | static tree | |
a5bcc582 | 5387 | cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p) |
a723baf1 MM |
5388 | { |
5389 | tree compound_stmt; | |
5390 | ||
5391 | /* Consume the `{'. */ | |
5392 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
5393 | return error_mark_node; | |
5394 | /* Begin the compound-statement. */ | |
7a3397c7 | 5395 | compound_stmt = begin_compound_stmt (/*has_no_scope=*/false); |
a723baf1 | 5396 | /* Parse an (optional) statement-seq. */ |
a5bcc582 | 5397 | cp_parser_statement_seq_opt (parser, in_statement_expr_p); |
a723baf1 | 5398 | /* Finish the compound-statement. */ |
7a3397c7 | 5399 | finish_compound_stmt (compound_stmt); |
a723baf1 MM |
5400 | /* Consume the `}'. */ |
5401 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
5402 | ||
5403 | return compound_stmt; | |
5404 | } | |
5405 | ||
5406 | /* Parse an (optional) statement-seq. | |
5407 | ||
5408 | statement-seq: | |
5409 | statement | |
5410 | statement-seq [opt] statement */ | |
5411 | ||
5412 | static void | |
a5bcc582 | 5413 | cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 MM |
5414 | { |
5415 | /* Scan statements until there aren't any more. */ | |
5416 | while (true) | |
5417 | { | |
5418 | /* If we're looking at a `}', then we've run out of statements. */ | |
5419 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE) | |
5420 | || cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
5421 | break; | |
5422 | ||
5423 | /* Parse the statement. */ | |
a5bcc582 | 5424 | cp_parser_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5425 | } |
5426 | } | |
5427 | ||
5428 | /* Parse a selection-statement. | |
5429 | ||
5430 | selection-statement: | |
5431 | if ( condition ) statement | |
5432 | if ( condition ) statement else statement | |
5433 | switch ( condition ) statement | |
5434 | ||
5435 | Returns the new IF_STMT or SWITCH_STMT. */ | |
5436 | ||
5437 | static tree | |
94edc4ab | 5438 | cp_parser_selection_statement (cp_parser* parser) |
a723baf1 MM |
5439 | { |
5440 | cp_token *token; | |
5441 | enum rid keyword; | |
5442 | ||
5443 | /* Peek at the next token. */ | |
5444 | token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement"); | |
5445 | ||
5446 | /* See what kind of keyword it is. */ | |
5447 | keyword = token->keyword; | |
5448 | switch (keyword) | |
5449 | { | |
5450 | case RID_IF: | |
5451 | case RID_SWITCH: | |
5452 | { | |
5453 | tree statement; | |
5454 | tree condition; | |
5455 | ||
5456 | /* Look for the `('. */ | |
5457 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) | |
5458 | { | |
5459 | cp_parser_skip_to_end_of_statement (parser); | |
5460 | return error_mark_node; | |
5461 | } | |
5462 | ||
5463 | /* Begin the selection-statement. */ | |
5464 | if (keyword == RID_IF) | |
5465 | statement = begin_if_stmt (); | |
5466 | else | |
5467 | statement = begin_switch_stmt (); | |
5468 | ||
5469 | /* Parse the condition. */ | |
5470 | condition = cp_parser_condition (parser); | |
5471 | /* Look for the `)'. */ | |
5472 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
a668c6ad MM |
5473 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
5474 | /*consume_paren=*/true); | |
a723baf1 MM |
5475 | |
5476 | if (keyword == RID_IF) | |
5477 | { | |
5478 | tree then_stmt; | |
5479 | ||
5480 | /* Add the condition. */ | |
5481 | finish_if_stmt_cond (condition, statement); | |
5482 | ||
5483 | /* Parse the then-clause. */ | |
5484 | then_stmt = cp_parser_implicitly_scoped_statement (parser); | |
5485 | finish_then_clause (statement); | |
5486 | ||
5487 | /* If the next token is `else', parse the else-clause. */ | |
5488 | if (cp_lexer_next_token_is_keyword (parser->lexer, | |
5489 | RID_ELSE)) | |
5490 | { | |
5491 | tree else_stmt; | |
5492 | ||
5493 | /* Consume the `else' keyword. */ | |
5494 | cp_lexer_consume_token (parser->lexer); | |
5495 | /* Parse the else-clause. */ | |
5496 | else_stmt | |
5497 | = cp_parser_implicitly_scoped_statement (parser); | |
5498 | finish_else_clause (statement); | |
5499 | } | |
5500 | ||
5501 | /* Now we're all done with the if-statement. */ | |
5502 | finish_if_stmt (); | |
5503 | } | |
5504 | else | |
5505 | { | |
5506 | tree body; | |
0e59b3fb | 5507 | bool in_switch_statement_p; |
a723baf1 MM |
5508 | |
5509 | /* Add the condition. */ | |
5510 | finish_switch_cond (condition, statement); | |
5511 | ||
5512 | /* Parse the body of the switch-statement. */ | |
0e59b3fb MM |
5513 | in_switch_statement_p = parser->in_switch_statement_p; |
5514 | parser->in_switch_statement_p = true; | |
a723baf1 | 5515 | body = cp_parser_implicitly_scoped_statement (parser); |
0e59b3fb | 5516 | parser->in_switch_statement_p = in_switch_statement_p; |
a723baf1 MM |
5517 | |
5518 | /* Now we're all done with the switch-statement. */ | |
5519 | finish_switch_stmt (statement); | |
5520 | } | |
5521 | ||
5522 | return statement; | |
5523 | } | |
5524 | break; | |
5525 | ||
5526 | default: | |
5527 | cp_parser_error (parser, "expected selection-statement"); | |
5528 | return error_mark_node; | |
5529 | } | |
5530 | } | |
5531 | ||
5532 | /* Parse a condition. | |
5533 | ||
5534 | condition: | |
5535 | expression | |
5536 | type-specifier-seq declarator = assignment-expression | |
5537 | ||
5538 | GNU Extension: | |
5539 | ||
5540 | condition: | |
5541 | type-specifier-seq declarator asm-specification [opt] | |
5542 | attributes [opt] = assignment-expression | |
5543 | ||
5544 | Returns the expression that should be tested. */ | |
5545 | ||
5546 | static tree | |
94edc4ab | 5547 | cp_parser_condition (cp_parser* parser) |
a723baf1 MM |
5548 | { |
5549 | tree type_specifiers; | |
5550 | const char *saved_message; | |
5551 | ||
5552 | /* Try the declaration first. */ | |
5553 | cp_parser_parse_tentatively (parser); | |
5554 | /* New types are not allowed in the type-specifier-seq for a | |
5555 | condition. */ | |
5556 | saved_message = parser->type_definition_forbidden_message; | |
5557 | parser->type_definition_forbidden_message | |
5558 | = "types may not be defined in conditions"; | |
5559 | /* Parse the type-specifier-seq. */ | |
5560 | type_specifiers = cp_parser_type_specifier_seq (parser); | |
5561 | /* Restore the saved message. */ | |
5562 | parser->type_definition_forbidden_message = saved_message; | |
5563 | /* If all is well, we might be looking at a declaration. */ | |
5564 | if (!cp_parser_error_occurred (parser)) | |
5565 | { | |
5566 | tree decl; | |
5567 | tree asm_specification; | |
5568 | tree attributes; | |
5569 | tree declarator; | |
5570 | tree initializer = NULL_TREE; | |
5571 | ||
5572 | /* Parse the declarator. */ | |
62b8a44e | 5573 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
a723baf1 MM |
5574 | /*ctor_dtor_or_conv_p=*/NULL); |
5575 | /* Parse the attributes. */ | |
5576 | attributes = cp_parser_attributes_opt (parser); | |
5577 | /* Parse the asm-specification. */ | |
5578 | asm_specification = cp_parser_asm_specification_opt (parser); | |
5579 | /* If the next token is not an `=', then we might still be | |
5580 | looking at an expression. For example: | |
5581 | ||
5582 | if (A(a).x) | |
5583 | ||
5584 | looks like a decl-specifier-seq and a declarator -- but then | |
5585 | there is no `=', so this is an expression. */ | |
5586 | cp_parser_require (parser, CPP_EQ, "`='"); | |
5587 | /* If we did see an `=', then we are looking at a declaration | |
5588 | for sure. */ | |
5589 | if (cp_parser_parse_definitely (parser)) | |
5590 | { | |
5591 | /* Create the declaration. */ | |
5592 | decl = start_decl (declarator, type_specifiers, | |
5593 | /*initialized_p=*/true, | |
5594 | attributes, /*prefix_attributes=*/NULL_TREE); | |
5595 | /* Parse the assignment-expression. */ | |
5596 | initializer = cp_parser_assignment_expression (parser); | |
5597 | ||
5598 | /* Process the initializer. */ | |
5599 | cp_finish_decl (decl, | |
5600 | initializer, | |
5601 | asm_specification, | |
5602 | LOOKUP_ONLYCONVERTING); | |
5603 | ||
5604 | return convert_from_reference (decl); | |
5605 | } | |
5606 | } | |
5607 | /* If we didn't even get past the declarator successfully, we are | |
5608 | definitely not looking at a declaration. */ | |
5609 | else | |
5610 | cp_parser_abort_tentative_parse (parser); | |
5611 | ||
5612 | /* Otherwise, we are looking at an expression. */ | |
5613 | return cp_parser_expression (parser); | |
5614 | } | |
5615 | ||
5616 | /* Parse an iteration-statement. | |
5617 | ||
5618 | iteration-statement: | |
5619 | while ( condition ) statement | |
5620 | do statement while ( expression ) ; | |
5621 | for ( for-init-statement condition [opt] ; expression [opt] ) | |
5622 | statement | |
5623 | ||
5624 | Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */ | |
5625 | ||
5626 | static tree | |
94edc4ab | 5627 | cp_parser_iteration_statement (cp_parser* parser) |
a723baf1 MM |
5628 | { |
5629 | cp_token *token; | |
5630 | enum rid keyword; | |
5631 | tree statement; | |
0e59b3fb MM |
5632 | bool in_iteration_statement_p; |
5633 | ||
a723baf1 MM |
5634 | |
5635 | /* Peek at the next token. */ | |
5636 | token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement"); | |
5637 | if (!token) | |
5638 | return error_mark_node; | |
5639 | ||
0e59b3fb MM |
5640 | /* Remember whether or not we are already within an iteration |
5641 | statement. */ | |
5642 | in_iteration_statement_p = parser->in_iteration_statement_p; | |
5643 | ||
a723baf1 MM |
5644 | /* See what kind of keyword it is. */ |
5645 | keyword = token->keyword; | |
5646 | switch (keyword) | |
5647 | { | |
5648 | case RID_WHILE: | |
5649 | { | |
5650 | tree condition; | |
5651 | ||
5652 | /* Begin the while-statement. */ | |
5653 | statement = begin_while_stmt (); | |
5654 | /* Look for the `('. */ | |
5655 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
5656 | /* Parse the condition. */ | |
5657 | condition = cp_parser_condition (parser); | |
5658 | finish_while_stmt_cond (condition, statement); | |
5659 | /* Look for the `)'. */ | |
5660 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
5661 | /* Parse the dependent statement. */ | |
0e59b3fb | 5662 | parser->in_iteration_statement_p = true; |
a723baf1 | 5663 | cp_parser_already_scoped_statement (parser); |
0e59b3fb | 5664 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
5665 | /* We're done with the while-statement. */ |
5666 | finish_while_stmt (statement); | |
5667 | } | |
5668 | break; | |
5669 | ||
5670 | case RID_DO: | |
5671 | { | |
5672 | tree expression; | |
5673 | ||
5674 | /* Begin the do-statement. */ | |
5675 | statement = begin_do_stmt (); | |
5676 | /* Parse the body of the do-statement. */ | |
0e59b3fb | 5677 | parser->in_iteration_statement_p = true; |
a723baf1 | 5678 | cp_parser_implicitly_scoped_statement (parser); |
0e59b3fb | 5679 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
5680 | finish_do_body (statement); |
5681 | /* Look for the `while' keyword. */ | |
5682 | cp_parser_require_keyword (parser, RID_WHILE, "`while'"); | |
5683 | /* Look for the `('. */ | |
5684 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
5685 | /* Parse the expression. */ | |
5686 | expression = cp_parser_expression (parser); | |
5687 | /* We're done with the do-statement. */ | |
5688 | finish_do_stmt (expression, statement); | |
5689 | /* Look for the `)'. */ | |
5690 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
5691 | /* Look for the `;'. */ | |
5692 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
5693 | } | |
5694 | break; | |
5695 | ||
5696 | case RID_FOR: | |
5697 | { | |
5698 | tree condition = NULL_TREE; | |
5699 | tree expression = NULL_TREE; | |
5700 | ||
5701 | /* Begin the for-statement. */ | |
5702 | statement = begin_for_stmt (); | |
5703 | /* Look for the `('. */ | |
5704 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
5705 | /* Parse the initialization. */ | |
5706 | cp_parser_for_init_statement (parser); | |
5707 | finish_for_init_stmt (statement); | |
5708 | ||
5709 | /* If there's a condition, process it. */ | |
5710 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5711 | condition = cp_parser_condition (parser); | |
5712 | finish_for_cond (condition, statement); | |
5713 | /* Look for the `;'. */ | |
5714 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
5715 | ||
5716 | /* If there's an expression, process it. */ | |
5717 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) | |
5718 | expression = cp_parser_expression (parser); | |
5719 | finish_for_expr (expression, statement); | |
5720 | /* Look for the `)'. */ | |
5721 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'"); | |
5722 | ||
5723 | /* Parse the body of the for-statement. */ | |
0e59b3fb | 5724 | parser->in_iteration_statement_p = true; |
a723baf1 | 5725 | cp_parser_already_scoped_statement (parser); |
0e59b3fb | 5726 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
5727 | |
5728 | /* We're done with the for-statement. */ | |
5729 | finish_for_stmt (statement); | |
5730 | } | |
5731 | break; | |
5732 | ||
5733 | default: | |
5734 | cp_parser_error (parser, "expected iteration-statement"); | |
5735 | statement = error_mark_node; | |
5736 | break; | |
5737 | } | |
5738 | ||
5739 | return statement; | |
5740 | } | |
5741 | ||
5742 | /* Parse a for-init-statement. | |
5743 | ||
5744 | for-init-statement: | |
5745 | expression-statement | |
5746 | simple-declaration */ | |
5747 | ||
5748 | static void | |
94edc4ab | 5749 | cp_parser_for_init_statement (cp_parser* parser) |
a723baf1 MM |
5750 | { |
5751 | /* If the next token is a `;', then we have an empty | |
34cd5ae7 | 5752 | expression-statement. Grammatically, this is also a |
a723baf1 MM |
5753 | simple-declaration, but an invalid one, because it does not |
5754 | declare anything. Therefore, if we did not handle this case | |
5755 | specially, we would issue an error message about an invalid | |
5756 | declaration. */ | |
5757 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5758 | { | |
5759 | /* We're going to speculatively look for a declaration, falling back | |
5760 | to an expression, if necessary. */ | |
5761 | cp_parser_parse_tentatively (parser); | |
5762 | /* Parse the declaration. */ | |
5763 | cp_parser_simple_declaration (parser, | |
5764 | /*function_definition_allowed_p=*/false); | |
5765 | /* If the tentative parse failed, then we shall need to look for an | |
5766 | expression-statement. */ | |
5767 | if (cp_parser_parse_definitely (parser)) | |
5768 | return; | |
5769 | } | |
5770 | ||
a5bcc582 | 5771 | cp_parser_expression_statement (parser, false); |
a723baf1 MM |
5772 | } |
5773 | ||
5774 | /* Parse a jump-statement. | |
5775 | ||
5776 | jump-statement: | |
5777 | break ; | |
5778 | continue ; | |
5779 | return expression [opt] ; | |
5780 | goto identifier ; | |
5781 | ||
5782 | GNU extension: | |
5783 | ||
5784 | jump-statement: | |
5785 | goto * expression ; | |
5786 | ||
5787 | Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or | |
5788 | GOTO_STMT. */ | |
5789 | ||
5790 | static tree | |
94edc4ab | 5791 | cp_parser_jump_statement (cp_parser* parser) |
a723baf1 MM |
5792 | { |
5793 | tree statement = error_mark_node; | |
5794 | cp_token *token; | |
5795 | enum rid keyword; | |
5796 | ||
5797 | /* Peek at the next token. */ | |
5798 | token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement"); | |
5799 | if (!token) | |
5800 | return error_mark_node; | |
5801 | ||
5802 | /* See what kind of keyword it is. */ | |
5803 | keyword = token->keyword; | |
5804 | switch (keyword) | |
5805 | { | |
5806 | case RID_BREAK: | |
0e59b3fb MM |
5807 | if (!parser->in_switch_statement_p |
5808 | && !parser->in_iteration_statement_p) | |
5809 | { | |
5810 | error ("break statement not within loop or switch"); | |
5811 | statement = error_mark_node; | |
5812 | } | |
5813 | else | |
5814 | statement = finish_break_stmt (); | |
a723baf1 MM |
5815 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
5816 | break; | |
5817 | ||
5818 | case RID_CONTINUE: | |
0e59b3fb MM |
5819 | if (!parser->in_iteration_statement_p) |
5820 | { | |
5821 | error ("continue statement not within a loop"); | |
5822 | statement = error_mark_node; | |
5823 | } | |
5824 | else | |
5825 | statement = finish_continue_stmt (); | |
a723baf1 MM |
5826 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
5827 | break; | |
5828 | ||
5829 | case RID_RETURN: | |
5830 | { | |
5831 | tree expr; | |
5832 | ||
5833 | /* If the next token is a `;', then there is no | |
5834 | expression. */ | |
5835 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5836 | expr = cp_parser_expression (parser); | |
5837 | else | |
5838 | expr = NULL_TREE; | |
5839 | /* Build the return-statement. */ | |
5840 | statement = finish_return_stmt (expr); | |
5841 | /* Look for the final `;'. */ | |
5842 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
5843 | } | |
5844 | break; | |
5845 | ||
5846 | case RID_GOTO: | |
5847 | /* Create the goto-statement. */ | |
5848 | if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)) | |
5849 | { | |
5850 | /* Issue a warning about this use of a GNU extension. */ | |
5851 | if (pedantic) | |
5852 | pedwarn ("ISO C++ forbids computed gotos"); | |
5853 | /* Consume the '*' token. */ | |
5854 | cp_lexer_consume_token (parser->lexer); | |
5855 | /* Parse the dependent expression. */ | |
5856 | finish_goto_stmt (cp_parser_expression (parser)); | |
5857 | } | |
5858 | else | |
5859 | finish_goto_stmt (cp_parser_identifier (parser)); | |
5860 | /* Look for the final `;'. */ | |
5861 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
5862 | break; | |
5863 | ||
5864 | default: | |
5865 | cp_parser_error (parser, "expected jump-statement"); | |
5866 | break; | |
5867 | } | |
5868 | ||
5869 | return statement; | |
5870 | } | |
5871 | ||
5872 | /* Parse a declaration-statement. | |
5873 | ||
5874 | declaration-statement: | |
5875 | block-declaration */ | |
5876 | ||
5877 | static void | |
94edc4ab | 5878 | cp_parser_declaration_statement (cp_parser* parser) |
a723baf1 MM |
5879 | { |
5880 | /* Parse the block-declaration. */ | |
5881 | cp_parser_block_declaration (parser, /*statement_p=*/true); | |
5882 | ||
5883 | /* Finish off the statement. */ | |
5884 | finish_stmt (); | |
5885 | } | |
5886 | ||
5887 | /* Some dependent statements (like `if (cond) statement'), are | |
5888 | implicitly in their own scope. In other words, if the statement is | |
5889 | a single statement (as opposed to a compound-statement), it is | |
5890 | none-the-less treated as if it were enclosed in braces. Any | |
5891 | declarations appearing in the dependent statement are out of scope | |
5892 | after control passes that point. This function parses a statement, | |
5893 | but ensures that is in its own scope, even if it is not a | |
5894 | compound-statement. | |
5895 | ||
5896 | Returns the new statement. */ | |
5897 | ||
5898 | static tree | |
94edc4ab | 5899 | cp_parser_implicitly_scoped_statement (cp_parser* parser) |
a723baf1 MM |
5900 | { |
5901 | tree statement; | |
5902 | ||
5903 | /* If the token is not a `{', then we must take special action. */ | |
5904 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
5905 | { | |
5906 | /* Create a compound-statement. */ | |
7a3397c7 | 5907 | statement = begin_compound_stmt (/*has_no_scope=*/false); |
a723baf1 | 5908 | /* Parse the dependent-statement. */ |
a5bcc582 | 5909 | cp_parser_statement (parser, false); |
a723baf1 | 5910 | /* Finish the dummy compound-statement. */ |
7a3397c7 | 5911 | finish_compound_stmt (statement); |
a723baf1 MM |
5912 | } |
5913 | /* Otherwise, we simply parse the statement directly. */ | |
5914 | else | |
a5bcc582 | 5915 | statement = cp_parser_compound_statement (parser, false); |
a723baf1 MM |
5916 | |
5917 | /* Return the statement. */ | |
5918 | return statement; | |
5919 | } | |
5920 | ||
5921 | /* For some dependent statements (like `while (cond) statement'), we | |
5922 | have already created a scope. Therefore, even if the dependent | |
5923 | statement is a compound-statement, we do not want to create another | |
5924 | scope. */ | |
5925 | ||
5926 | static void | |
94edc4ab | 5927 | cp_parser_already_scoped_statement (cp_parser* parser) |
a723baf1 MM |
5928 | { |
5929 | /* If the token is not a `{', then we must take special action. */ | |
5930 | if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE)) | |
5931 | { | |
5932 | tree statement; | |
5933 | ||
5934 | /* Create a compound-statement. */ | |
7a3397c7 | 5935 | statement = begin_compound_stmt (/*has_no_scope=*/true); |
a723baf1 | 5936 | /* Parse the dependent-statement. */ |
a5bcc582 | 5937 | cp_parser_statement (parser, false); |
a723baf1 | 5938 | /* Finish the dummy compound-statement. */ |
7a3397c7 | 5939 | finish_compound_stmt (statement); |
a723baf1 MM |
5940 | } |
5941 | /* Otherwise, we simply parse the statement directly. */ | |
5942 | else | |
a5bcc582 | 5943 | cp_parser_statement (parser, false); |
a723baf1 MM |
5944 | } |
5945 | ||
5946 | /* Declarations [gram.dcl.dcl] */ | |
5947 | ||
5948 | /* Parse an optional declaration-sequence. | |
5949 | ||
5950 | declaration-seq: | |
5951 | declaration | |
5952 | declaration-seq declaration */ | |
5953 | ||
5954 | static void | |
94edc4ab | 5955 | cp_parser_declaration_seq_opt (cp_parser* parser) |
a723baf1 MM |
5956 | { |
5957 | while (true) | |
5958 | { | |
5959 | cp_token *token; | |
5960 | ||
5961 | token = cp_lexer_peek_token (parser->lexer); | |
5962 | ||
5963 | if (token->type == CPP_CLOSE_BRACE | |
5964 | || token->type == CPP_EOF) | |
5965 | break; | |
5966 | ||
5967 | if (token->type == CPP_SEMICOLON) | |
5968 | { | |
5969 | /* A declaration consisting of a single semicolon is | |
5970 | invalid. Allow it unless we're being pedantic. */ | |
5971 | if (pedantic) | |
5972 | pedwarn ("extra `;'"); | |
5973 | cp_lexer_consume_token (parser->lexer); | |
5974 | continue; | |
5975 | } | |
5976 | ||
c838d82f | 5977 | /* The C lexer modifies PENDING_LANG_CHANGE when it wants the |
34cd5ae7 | 5978 | parser to enter or exit implicit `extern "C"' blocks. */ |
c838d82f MM |
5979 | while (pending_lang_change > 0) |
5980 | { | |
5981 | push_lang_context (lang_name_c); | |
5982 | --pending_lang_change; | |
5983 | } | |
5984 | while (pending_lang_change < 0) | |
5985 | { | |
5986 | pop_lang_context (); | |
5987 | ++pending_lang_change; | |
5988 | } | |
5989 | ||
5990 | /* Parse the declaration itself. */ | |
a723baf1 MM |
5991 | cp_parser_declaration (parser); |
5992 | } | |
5993 | } | |
5994 | ||
5995 | /* Parse a declaration. | |
5996 | ||
5997 | declaration: | |
5998 | block-declaration | |
5999 | function-definition | |
6000 | template-declaration | |
6001 | explicit-instantiation | |
6002 | explicit-specialization | |
6003 | linkage-specification | |
1092805d MM |
6004 | namespace-definition |
6005 | ||
6006 | GNU extension: | |
6007 | ||
6008 | declaration: | |
6009 | __extension__ declaration */ | |
a723baf1 MM |
6010 | |
6011 | static void | |
94edc4ab | 6012 | cp_parser_declaration (cp_parser* parser) |
a723baf1 MM |
6013 | { |
6014 | cp_token token1; | |
6015 | cp_token token2; | |
1092805d MM |
6016 | int saved_pedantic; |
6017 | ||
6018 | /* Check for the `__extension__' keyword. */ | |
6019 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
6020 | { | |
6021 | /* Parse the qualified declaration. */ | |
6022 | cp_parser_declaration (parser); | |
6023 | /* Restore the PEDANTIC flag. */ | |
6024 | pedantic = saved_pedantic; | |
6025 | ||
6026 | return; | |
6027 | } | |
a723baf1 MM |
6028 | |
6029 | /* Try to figure out what kind of declaration is present. */ | |
6030 | token1 = *cp_lexer_peek_token (parser->lexer); | |
6031 | if (token1.type != CPP_EOF) | |
6032 | token2 = *cp_lexer_peek_nth_token (parser->lexer, 2); | |
6033 | ||
6034 | /* If the next token is `extern' and the following token is a string | |
6035 | literal, then we have a linkage specification. */ | |
6036 | if (token1.keyword == RID_EXTERN | |
6037 | && cp_parser_is_string_literal (&token2)) | |
6038 | cp_parser_linkage_specification (parser); | |
6039 | /* If the next token is `template', then we have either a template | |
6040 | declaration, an explicit instantiation, or an explicit | |
6041 | specialization. */ | |
6042 | else if (token1.keyword == RID_TEMPLATE) | |
6043 | { | |
6044 | /* `template <>' indicates a template specialization. */ | |
6045 | if (token2.type == CPP_LESS | |
6046 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER) | |
6047 | cp_parser_explicit_specialization (parser); | |
6048 | /* `template <' indicates a template declaration. */ | |
6049 | else if (token2.type == CPP_LESS) | |
6050 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
6051 | /* Anything else must be an explicit instantiation. */ | |
6052 | else | |
6053 | cp_parser_explicit_instantiation (parser); | |
6054 | } | |
6055 | /* If the next token is `export', then we have a template | |
6056 | declaration. */ | |
6057 | else if (token1.keyword == RID_EXPORT) | |
6058 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
6059 | /* If the next token is `extern', 'static' or 'inline' and the one | |
6060 | after that is `template', we have a GNU extended explicit | |
6061 | instantiation directive. */ | |
6062 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
6063 | && (token1.keyword == RID_EXTERN | |
6064 | || token1.keyword == RID_STATIC | |
6065 | || token1.keyword == RID_INLINE) | |
6066 | && token2.keyword == RID_TEMPLATE) | |
6067 | cp_parser_explicit_instantiation (parser); | |
6068 | /* If the next token is `namespace', check for a named or unnamed | |
6069 | namespace definition. */ | |
6070 | else if (token1.keyword == RID_NAMESPACE | |
6071 | && (/* A named namespace definition. */ | |
6072 | (token2.type == CPP_NAME | |
6073 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type | |
6074 | == CPP_OPEN_BRACE)) | |
6075 | /* An unnamed namespace definition. */ | |
6076 | || token2.type == CPP_OPEN_BRACE)) | |
6077 | cp_parser_namespace_definition (parser); | |
6078 | /* We must have either a block declaration or a function | |
6079 | definition. */ | |
6080 | else | |
6081 | /* Try to parse a block-declaration, or a function-definition. */ | |
6082 | cp_parser_block_declaration (parser, /*statement_p=*/false); | |
6083 | } | |
6084 | ||
6085 | /* Parse a block-declaration. | |
6086 | ||
6087 | block-declaration: | |
6088 | simple-declaration | |
6089 | asm-definition | |
6090 | namespace-alias-definition | |
6091 | using-declaration | |
6092 | using-directive | |
6093 | ||
6094 | GNU Extension: | |
6095 | ||
6096 | block-declaration: | |
6097 | __extension__ block-declaration | |
6098 | label-declaration | |
6099 | ||
34cd5ae7 | 6100 | If STATEMENT_P is TRUE, then this block-declaration is occurring as |
a723baf1 MM |
6101 | part of a declaration-statement. */ |
6102 | ||
6103 | static void | |
6104 | cp_parser_block_declaration (cp_parser *parser, | |
6105 | bool statement_p) | |
6106 | { | |
6107 | cp_token *token1; | |
6108 | int saved_pedantic; | |
6109 | ||
6110 | /* Check for the `__extension__' keyword. */ | |
6111 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
6112 | { | |
6113 | /* Parse the qualified declaration. */ | |
6114 | cp_parser_block_declaration (parser, statement_p); | |
6115 | /* Restore the PEDANTIC flag. */ | |
6116 | pedantic = saved_pedantic; | |
6117 | ||
6118 | return; | |
6119 | } | |
6120 | ||
6121 | /* Peek at the next token to figure out which kind of declaration is | |
6122 | present. */ | |
6123 | token1 = cp_lexer_peek_token (parser->lexer); | |
6124 | ||
6125 | /* If the next keyword is `asm', we have an asm-definition. */ | |
6126 | if (token1->keyword == RID_ASM) | |
6127 | { | |
6128 | if (statement_p) | |
6129 | cp_parser_commit_to_tentative_parse (parser); | |
6130 | cp_parser_asm_definition (parser); | |
6131 | } | |
6132 | /* If the next keyword is `namespace', we have a | |
6133 | namespace-alias-definition. */ | |
6134 | else if (token1->keyword == RID_NAMESPACE) | |
6135 | cp_parser_namespace_alias_definition (parser); | |
6136 | /* If the next keyword is `using', we have either a | |
6137 | using-declaration or a using-directive. */ | |
6138 | else if (token1->keyword == RID_USING) | |
6139 | { | |
6140 | cp_token *token2; | |
6141 | ||
6142 | if (statement_p) | |
6143 | cp_parser_commit_to_tentative_parse (parser); | |
6144 | /* If the token after `using' is `namespace', then we have a | |
6145 | using-directive. */ | |
6146 | token2 = cp_lexer_peek_nth_token (parser->lexer, 2); | |
6147 | if (token2->keyword == RID_NAMESPACE) | |
6148 | cp_parser_using_directive (parser); | |
6149 | /* Otherwise, it's a using-declaration. */ | |
6150 | else | |
6151 | cp_parser_using_declaration (parser); | |
6152 | } | |
6153 | /* If the next keyword is `__label__' we have a label declaration. */ | |
6154 | else if (token1->keyword == RID_LABEL) | |
6155 | { | |
6156 | if (statement_p) | |
6157 | cp_parser_commit_to_tentative_parse (parser); | |
6158 | cp_parser_label_declaration (parser); | |
6159 | } | |
6160 | /* Anything else must be a simple-declaration. */ | |
6161 | else | |
6162 | cp_parser_simple_declaration (parser, !statement_p); | |
6163 | } | |
6164 | ||
6165 | /* Parse a simple-declaration. | |
6166 | ||
6167 | simple-declaration: | |
6168 | decl-specifier-seq [opt] init-declarator-list [opt] ; | |
6169 | ||
6170 | init-declarator-list: | |
6171 | init-declarator | |
6172 | init-declarator-list , init-declarator | |
6173 | ||
34cd5ae7 | 6174 | If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a |
9bcb9aae | 6175 | function-definition as a simple-declaration. */ |
a723baf1 MM |
6176 | |
6177 | static void | |
94edc4ab NN |
6178 | cp_parser_simple_declaration (cp_parser* parser, |
6179 | bool function_definition_allowed_p) | |
a723baf1 MM |
6180 | { |
6181 | tree decl_specifiers; | |
6182 | tree attributes; | |
560ad596 | 6183 | int declares_class_or_enum; |
a723baf1 MM |
6184 | bool saw_declarator; |
6185 | ||
6186 | /* Defer access checks until we know what is being declared; the | |
6187 | checks for names appearing in the decl-specifier-seq should be | |
6188 | done as if we were in the scope of the thing being declared. */ | |
8d241e0b | 6189 | push_deferring_access_checks (dk_deferred); |
cf22909c | 6190 | |
a723baf1 MM |
6191 | /* Parse the decl-specifier-seq. We have to keep track of whether |
6192 | or not the decl-specifier-seq declares a named class or | |
6193 | enumeration type, since that is the only case in which the | |
6194 | init-declarator-list is allowed to be empty. | |
6195 | ||
6196 | [dcl.dcl] | |
6197 | ||
6198 | In a simple-declaration, the optional init-declarator-list can be | |
6199 | omitted only when declaring a class or enumeration, that is when | |
6200 | the decl-specifier-seq contains either a class-specifier, an | |
6201 | elaborated-type-specifier, or an enum-specifier. */ | |
6202 | decl_specifiers | |
6203 | = cp_parser_decl_specifier_seq (parser, | |
6204 | CP_PARSER_FLAGS_OPTIONAL, | |
6205 | &attributes, | |
6206 | &declares_class_or_enum); | |
6207 | /* We no longer need to defer access checks. */ | |
cf22909c | 6208 | stop_deferring_access_checks (); |
24c0ef37 | 6209 | |
39703eb9 MM |
6210 | /* In a block scope, a valid declaration must always have a |
6211 | decl-specifier-seq. By not trying to parse declarators, we can | |
6212 | resolve the declaration/expression ambiguity more quickly. */ | |
6213 | if (!function_definition_allowed_p && !decl_specifiers) | |
6214 | { | |
6215 | cp_parser_error (parser, "expected declaration"); | |
6216 | goto done; | |
6217 | } | |
6218 | ||
8fbc5ae7 MM |
6219 | /* If the next two tokens are both identifiers, the code is |
6220 | erroneous. The usual cause of this situation is code like: | |
6221 | ||
6222 | T t; | |
6223 | ||
6224 | where "T" should name a type -- but does not. */ | |
6225 | if (cp_parser_diagnose_invalid_type_name (parser)) | |
6226 | { | |
8d241e0b | 6227 | /* If parsing tentatively, we should commit; we really are |
8fbc5ae7 MM |
6228 | looking at a declaration. */ |
6229 | cp_parser_commit_to_tentative_parse (parser); | |
6230 | /* Give up. */ | |
39703eb9 | 6231 | goto done; |
8fbc5ae7 MM |
6232 | } |
6233 | ||
a723baf1 MM |
6234 | /* Keep going until we hit the `;' at the end of the simple |
6235 | declaration. */ | |
6236 | saw_declarator = false; | |
6237 | while (cp_lexer_next_token_is_not (parser->lexer, | |
6238 | CPP_SEMICOLON)) | |
6239 | { | |
6240 | cp_token *token; | |
6241 | bool function_definition_p; | |
560ad596 | 6242 | tree decl; |
a723baf1 MM |
6243 | |
6244 | saw_declarator = true; | |
6245 | /* Parse the init-declarator. */ | |
560ad596 MM |
6246 | decl = cp_parser_init_declarator (parser, decl_specifiers, attributes, |
6247 | function_definition_allowed_p, | |
6248 | /*member_p=*/false, | |
6249 | declares_class_or_enum, | |
6250 | &function_definition_p); | |
1fb3244a MM |
6251 | /* If an error occurred while parsing tentatively, exit quickly. |
6252 | (That usually happens when in the body of a function; each | |
6253 | statement is treated as a declaration-statement until proven | |
6254 | otherwise.) */ | |
6255 | if (cp_parser_error_occurred (parser)) | |
39703eb9 | 6256 | goto done; |
a723baf1 MM |
6257 | /* Handle function definitions specially. */ |
6258 | if (function_definition_p) | |
6259 | { | |
6260 | /* If the next token is a `,', then we are probably | |
6261 | processing something like: | |
6262 | ||
6263 | void f() {}, *p; | |
6264 | ||
6265 | which is erroneous. */ | |
6266 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
6267 | error ("mixing declarations and function-definitions is forbidden"); | |
6268 | /* Otherwise, we're done with the list of declarators. */ | |
6269 | else | |
24c0ef37 | 6270 | { |
cf22909c | 6271 | pop_deferring_access_checks (); |
24c0ef37 GS |
6272 | return; |
6273 | } | |
a723baf1 MM |
6274 | } |
6275 | /* The next token should be either a `,' or a `;'. */ | |
6276 | token = cp_lexer_peek_token (parser->lexer); | |
6277 | /* If it's a `,', there are more declarators to come. */ | |
6278 | if (token->type == CPP_COMMA) | |
6279 | cp_lexer_consume_token (parser->lexer); | |
6280 | /* If it's a `;', we are done. */ | |
6281 | else if (token->type == CPP_SEMICOLON) | |
6282 | break; | |
6283 | /* Anything else is an error. */ | |
6284 | else | |
6285 | { | |
6286 | cp_parser_error (parser, "expected `,' or `;'"); | |
6287 | /* Skip tokens until we reach the end of the statement. */ | |
6288 | cp_parser_skip_to_end_of_statement (parser); | |
39703eb9 | 6289 | goto done; |
a723baf1 MM |
6290 | } |
6291 | /* After the first time around, a function-definition is not | |
6292 | allowed -- even if it was OK at first. For example: | |
6293 | ||
6294 | int i, f() {} | |
6295 | ||
6296 | is not valid. */ | |
6297 | function_definition_allowed_p = false; | |
6298 | } | |
6299 | ||
6300 | /* Issue an error message if no declarators are present, and the | |
6301 | decl-specifier-seq does not itself declare a class or | |
6302 | enumeration. */ | |
6303 | if (!saw_declarator) | |
6304 | { | |
6305 | if (cp_parser_declares_only_class_p (parser)) | |
6306 | shadow_tag (decl_specifiers); | |
6307 | /* Perform any deferred access checks. */ | |
cf22909c | 6308 | perform_deferred_access_checks (); |
a723baf1 MM |
6309 | } |
6310 | ||
6311 | /* Consume the `;'. */ | |
6312 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6313 | ||
39703eb9 MM |
6314 | done: |
6315 | pop_deferring_access_checks (); | |
a723baf1 MM |
6316 | } |
6317 | ||
6318 | /* Parse a decl-specifier-seq. | |
6319 | ||
6320 | decl-specifier-seq: | |
6321 | decl-specifier-seq [opt] decl-specifier | |
6322 | ||
6323 | decl-specifier: | |
6324 | storage-class-specifier | |
6325 | type-specifier | |
6326 | function-specifier | |
6327 | friend | |
6328 | typedef | |
6329 | ||
6330 | GNU Extension: | |
6331 | ||
6332 | decl-specifier-seq: | |
6333 | decl-specifier-seq [opt] attributes | |
6334 | ||
6335 | Returns a TREE_LIST, giving the decl-specifiers in the order they | |
6336 | appear in the source code. The TREE_VALUE of each node is the | |
6337 | decl-specifier. For a keyword (such as `auto' or `friend'), the | |
34cd5ae7 | 6338 | TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the |
a723baf1 MM |
6339 | representation of a type-specifier, see cp_parser_type_specifier. |
6340 | ||
6341 | If there are attributes, they will be stored in *ATTRIBUTES, | |
6342 | represented as described above cp_parser_attributes. | |
6343 | ||
6344 | If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier | |
6345 | appears, and the entity that will be a friend is not going to be a | |
6346 | class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that | |
6347 | even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which | |
560ad596 MM |
6348 | friendship is granted might not be a class. |
6349 | ||
6350 | *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following | |
6351 | *flags: | |
6352 | ||
6353 | 1: one of the decl-specifiers is an elaborated-type-specifier | |
6354 | 2: one of the decl-specifiers is an enum-specifier or a | |
6355 | class-specifier | |
6356 | ||
6357 | */ | |
a723baf1 MM |
6358 | |
6359 | static tree | |
94edc4ab NN |
6360 | cp_parser_decl_specifier_seq (cp_parser* parser, |
6361 | cp_parser_flags flags, | |
6362 | tree* attributes, | |
560ad596 | 6363 | int* declares_class_or_enum) |
a723baf1 MM |
6364 | { |
6365 | tree decl_specs = NULL_TREE; | |
6366 | bool friend_p = false; | |
f2ce60b8 NS |
6367 | bool constructor_possible_p = !parser->in_declarator_p; |
6368 | ||
a723baf1 | 6369 | /* Assume no class or enumeration type is declared. */ |
560ad596 | 6370 | *declares_class_or_enum = 0; |
a723baf1 MM |
6371 | |
6372 | /* Assume there are no attributes. */ | |
6373 | *attributes = NULL_TREE; | |
6374 | ||
6375 | /* Keep reading specifiers until there are no more to read. */ | |
6376 | while (true) | |
6377 | { | |
6378 | tree decl_spec = NULL_TREE; | |
6379 | bool constructor_p; | |
6380 | cp_token *token; | |
6381 | ||
6382 | /* Peek at the next token. */ | |
6383 | token = cp_lexer_peek_token (parser->lexer); | |
6384 | /* Handle attributes. */ | |
6385 | if (token->keyword == RID_ATTRIBUTE) | |
6386 | { | |
6387 | /* Parse the attributes. */ | |
6388 | decl_spec = cp_parser_attributes_opt (parser); | |
6389 | /* Add them to the list. */ | |
6390 | *attributes = chainon (*attributes, decl_spec); | |
6391 | continue; | |
6392 | } | |
6393 | /* If the next token is an appropriate keyword, we can simply | |
6394 | add it to the list. */ | |
6395 | switch (token->keyword) | |
6396 | { | |
6397 | case RID_FRIEND: | |
6398 | /* decl-specifier: | |
6399 | friend */ | |
1918facf SB |
6400 | if (friend_p) |
6401 | error ("duplicate `friend'"); | |
6402 | else | |
6403 | friend_p = true; | |
a723baf1 MM |
6404 | /* The representation of the specifier is simply the |
6405 | appropriate TREE_IDENTIFIER node. */ | |
6406 | decl_spec = token->value; | |
6407 | /* Consume the token. */ | |
6408 | cp_lexer_consume_token (parser->lexer); | |
6409 | break; | |
6410 | ||
6411 | /* function-specifier: | |
6412 | inline | |
6413 | virtual | |
6414 | explicit */ | |
6415 | case RID_INLINE: | |
6416 | case RID_VIRTUAL: | |
6417 | case RID_EXPLICIT: | |
6418 | decl_spec = cp_parser_function_specifier_opt (parser); | |
6419 | break; | |
6420 | ||
6421 | /* decl-specifier: | |
6422 | typedef */ | |
6423 | case RID_TYPEDEF: | |
6424 | /* The representation of the specifier is simply the | |
6425 | appropriate TREE_IDENTIFIER node. */ | |
6426 | decl_spec = token->value; | |
6427 | /* Consume the token. */ | |
6428 | cp_lexer_consume_token (parser->lexer); | |
2050a1bb MM |
6429 | /* A constructor declarator cannot appear in a typedef. */ |
6430 | constructor_possible_p = false; | |
c006d942 MM |
6431 | /* The "typedef" keyword can only occur in a declaration; we |
6432 | may as well commit at this point. */ | |
6433 | cp_parser_commit_to_tentative_parse (parser); | |
a723baf1 MM |
6434 | break; |
6435 | ||
6436 | /* storage-class-specifier: | |
6437 | auto | |
6438 | register | |
6439 | static | |
6440 | extern | |
6441 | mutable | |
6442 | ||
6443 | GNU Extension: | |
6444 | thread */ | |
6445 | case RID_AUTO: | |
6446 | case RID_REGISTER: | |
6447 | case RID_STATIC: | |
6448 | case RID_EXTERN: | |
6449 | case RID_MUTABLE: | |
6450 | case RID_THREAD: | |
6451 | decl_spec = cp_parser_storage_class_specifier_opt (parser); | |
6452 | break; | |
6453 | ||
6454 | default: | |
6455 | break; | |
6456 | } | |
6457 | ||
6458 | /* Constructors are a special case. The `S' in `S()' is not a | |
6459 | decl-specifier; it is the beginning of the declarator. */ | |
6460 | constructor_p = (!decl_spec | |
2050a1bb | 6461 | && constructor_possible_p |
a723baf1 MM |
6462 | && cp_parser_constructor_declarator_p (parser, |
6463 | friend_p)); | |
6464 | ||
6465 | /* If we don't have a DECL_SPEC yet, then we must be looking at | |
6466 | a type-specifier. */ | |
6467 | if (!decl_spec && !constructor_p) | |
6468 | { | |
560ad596 | 6469 | int decl_spec_declares_class_or_enum; |
a723baf1 MM |
6470 | bool is_cv_qualifier; |
6471 | ||
6472 | decl_spec | |
6473 | = cp_parser_type_specifier (parser, flags, | |
6474 | friend_p, | |
6475 | /*is_declaration=*/true, | |
6476 | &decl_spec_declares_class_or_enum, | |
6477 | &is_cv_qualifier); | |
6478 | ||
6479 | *declares_class_or_enum |= decl_spec_declares_class_or_enum; | |
6480 | ||
6481 | /* If this type-specifier referenced a user-defined type | |
6482 | (a typedef, class-name, etc.), then we can't allow any | |
6483 | more such type-specifiers henceforth. | |
6484 | ||
6485 | [dcl.spec] | |
6486 | ||
6487 | The longest sequence of decl-specifiers that could | |
6488 | possibly be a type name is taken as the | |
6489 | decl-specifier-seq of a declaration. The sequence shall | |
6490 | be self-consistent as described below. | |
6491 | ||
6492 | [dcl.type] | |
6493 | ||
6494 | As a general rule, at most one type-specifier is allowed | |
6495 | in the complete decl-specifier-seq of a declaration. The | |
6496 | only exceptions are the following: | |
6497 | ||
6498 | -- const or volatile can be combined with any other | |
6499 | type-specifier. | |
6500 | ||
6501 | -- signed or unsigned can be combined with char, long, | |
6502 | short, or int. | |
6503 | ||
6504 | -- .. | |
6505 | ||
6506 | Example: | |
6507 | ||
6508 | typedef char* Pc; | |
6509 | void g (const int Pc); | |
6510 | ||
6511 | Here, Pc is *not* part of the decl-specifier seq; it's | |
6512 | the declarator. Therefore, once we see a type-specifier | |
6513 | (other than a cv-qualifier), we forbid any additional | |
6514 | user-defined types. We *do* still allow things like `int | |
6515 | int' to be considered a decl-specifier-seq, and issue the | |
6516 | error message later. */ | |
6517 | if (decl_spec && !is_cv_qualifier) | |
6518 | flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; | |
2050a1bb MM |
6519 | /* A constructor declarator cannot follow a type-specifier. */ |
6520 | if (decl_spec) | |
6521 | constructor_possible_p = false; | |
a723baf1 MM |
6522 | } |
6523 | ||
6524 | /* If we still do not have a DECL_SPEC, then there are no more | |
6525 | decl-specifiers. */ | |
6526 | if (!decl_spec) | |
6527 | { | |
6528 | /* Issue an error message, unless the entire construct was | |
6529 | optional. */ | |
6530 | if (!(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
6531 | { | |
6532 | cp_parser_error (parser, "expected decl specifier"); | |
6533 | return error_mark_node; | |
6534 | } | |
6535 | ||
6536 | break; | |
6537 | } | |
6538 | ||
6539 | /* Add the DECL_SPEC to the list of specifiers. */ | |
6540 | decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs); | |
6541 | ||
6542 | /* After we see one decl-specifier, further decl-specifiers are | |
6543 | always optional. */ | |
6544 | flags |= CP_PARSER_FLAGS_OPTIONAL; | |
6545 | } | |
6546 | ||
6547 | /* We have built up the DECL_SPECS in reverse order. Return them in | |
6548 | the correct order. */ | |
6549 | return nreverse (decl_specs); | |
6550 | } | |
6551 | ||
6552 | /* Parse an (optional) storage-class-specifier. | |
6553 | ||
6554 | storage-class-specifier: | |
6555 | auto | |
6556 | register | |
6557 | static | |
6558 | extern | |
6559 | mutable | |
6560 | ||
6561 | GNU Extension: | |
6562 | ||
6563 | storage-class-specifier: | |
6564 | thread | |
6565 | ||
6566 | Returns an IDENTIFIER_NODE corresponding to the keyword used. */ | |
6567 | ||
6568 | static tree | |
94edc4ab | 6569 | cp_parser_storage_class_specifier_opt (cp_parser* parser) |
a723baf1 MM |
6570 | { |
6571 | switch (cp_lexer_peek_token (parser->lexer)->keyword) | |
6572 | { | |
6573 | case RID_AUTO: | |
6574 | case RID_REGISTER: | |
6575 | case RID_STATIC: | |
6576 | case RID_EXTERN: | |
6577 | case RID_MUTABLE: | |
6578 | case RID_THREAD: | |
6579 | /* Consume the token. */ | |
6580 | return cp_lexer_consume_token (parser->lexer)->value; | |
6581 | ||
6582 | default: | |
6583 | return NULL_TREE; | |
6584 | } | |
6585 | } | |
6586 | ||
6587 | /* Parse an (optional) function-specifier. | |
6588 | ||
6589 | function-specifier: | |
6590 | inline | |
6591 | virtual | |
6592 | explicit | |
6593 | ||
6594 | Returns an IDENTIFIER_NODE corresponding to the keyword used. */ | |
6595 | ||
6596 | static tree | |
94edc4ab | 6597 | cp_parser_function_specifier_opt (cp_parser* parser) |
a723baf1 MM |
6598 | { |
6599 | switch (cp_lexer_peek_token (parser->lexer)->keyword) | |
6600 | { | |
6601 | case RID_INLINE: | |
6602 | case RID_VIRTUAL: | |
6603 | case RID_EXPLICIT: | |
6604 | /* Consume the token. */ | |
6605 | return cp_lexer_consume_token (parser->lexer)->value; | |
6606 | ||
6607 | default: | |
6608 | return NULL_TREE; | |
6609 | } | |
6610 | } | |
6611 | ||
6612 | /* Parse a linkage-specification. | |
6613 | ||
6614 | linkage-specification: | |
6615 | extern string-literal { declaration-seq [opt] } | |
6616 | extern string-literal declaration */ | |
6617 | ||
6618 | static void | |
94edc4ab | 6619 | cp_parser_linkage_specification (cp_parser* parser) |
a723baf1 MM |
6620 | { |
6621 | cp_token *token; | |
6622 | tree linkage; | |
6623 | ||
6624 | /* Look for the `extern' keyword. */ | |
6625 | cp_parser_require_keyword (parser, RID_EXTERN, "`extern'"); | |
6626 | ||
6627 | /* Peek at the next token. */ | |
6628 | token = cp_lexer_peek_token (parser->lexer); | |
6629 | /* If it's not a string-literal, then there's a problem. */ | |
6630 | if (!cp_parser_is_string_literal (token)) | |
6631 | { | |
6632 | cp_parser_error (parser, "expected language-name"); | |
6633 | return; | |
6634 | } | |
6635 | /* Consume the token. */ | |
6636 | cp_lexer_consume_token (parser->lexer); | |
6637 | ||
6638 | /* Transform the literal into an identifier. If the literal is a | |
6639 | wide-character string, or contains embedded NULs, then we can't | |
6640 | handle it as the user wants. */ | |
6641 | if (token->type == CPP_WSTRING | |
6642 | || (strlen (TREE_STRING_POINTER (token->value)) | |
6643 | != (size_t) (TREE_STRING_LENGTH (token->value) - 1))) | |
6644 | { | |
6645 | cp_parser_error (parser, "invalid linkage-specification"); | |
6646 | /* Assume C++ linkage. */ | |
6647 | linkage = get_identifier ("c++"); | |
6648 | } | |
6649 | /* If it's a simple string constant, things are easier. */ | |
6650 | else | |
6651 | linkage = get_identifier (TREE_STRING_POINTER (token->value)); | |
6652 | ||
6653 | /* We're now using the new linkage. */ | |
6654 | push_lang_context (linkage); | |
6655 | ||
6656 | /* If the next token is a `{', then we're using the first | |
6657 | production. */ | |
6658 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
6659 | { | |
6660 | /* Consume the `{' token. */ | |
6661 | cp_lexer_consume_token (parser->lexer); | |
6662 | /* Parse the declarations. */ | |
6663 | cp_parser_declaration_seq_opt (parser); | |
6664 | /* Look for the closing `}'. */ | |
6665 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
6666 | } | |
6667 | /* Otherwise, there's just one declaration. */ | |
6668 | else | |
6669 | { | |
6670 | bool saved_in_unbraced_linkage_specification_p; | |
6671 | ||
6672 | saved_in_unbraced_linkage_specification_p | |
6673 | = parser->in_unbraced_linkage_specification_p; | |
6674 | parser->in_unbraced_linkage_specification_p = true; | |
6675 | have_extern_spec = true; | |
6676 | cp_parser_declaration (parser); | |
6677 | have_extern_spec = false; | |
6678 | parser->in_unbraced_linkage_specification_p | |
6679 | = saved_in_unbraced_linkage_specification_p; | |
6680 | } | |
6681 | ||
6682 | /* We're done with the linkage-specification. */ | |
6683 | pop_lang_context (); | |
6684 | } | |
6685 | ||
6686 | /* Special member functions [gram.special] */ | |
6687 | ||
6688 | /* Parse a conversion-function-id. | |
6689 | ||
6690 | conversion-function-id: | |
6691 | operator conversion-type-id | |
6692 | ||
6693 | Returns an IDENTIFIER_NODE representing the operator. */ | |
6694 | ||
6695 | static tree | |
94edc4ab | 6696 | cp_parser_conversion_function_id (cp_parser* parser) |
a723baf1 MM |
6697 | { |
6698 | tree type; | |
6699 | tree saved_scope; | |
6700 | tree saved_qualifying_scope; | |
6701 | tree saved_object_scope; | |
6702 | ||
6703 | /* Look for the `operator' token. */ | |
6704 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'")) | |
6705 | return error_mark_node; | |
6706 | /* When we parse the conversion-type-id, the current scope will be | |
6707 | reset. However, we need that information in able to look up the | |
6708 | conversion function later, so we save it here. */ | |
6709 | saved_scope = parser->scope; | |
6710 | saved_qualifying_scope = parser->qualifying_scope; | |
6711 | saved_object_scope = parser->object_scope; | |
6712 | /* We must enter the scope of the class so that the names of | |
6713 | entities declared within the class are available in the | |
6714 | conversion-type-id. For example, consider: | |
6715 | ||
6716 | struct S { | |
6717 | typedef int I; | |
6718 | operator I(); | |
6719 | }; | |
6720 | ||
6721 | S::operator I() { ... } | |
6722 | ||
6723 | In order to see that `I' is a type-name in the definition, we | |
6724 | must be in the scope of `S'. */ | |
6725 | if (saved_scope) | |
6726 | push_scope (saved_scope); | |
6727 | /* Parse the conversion-type-id. */ | |
6728 | type = cp_parser_conversion_type_id (parser); | |
6729 | /* Leave the scope of the class, if any. */ | |
6730 | if (saved_scope) | |
6731 | pop_scope (saved_scope); | |
6732 | /* Restore the saved scope. */ | |
6733 | parser->scope = saved_scope; | |
6734 | parser->qualifying_scope = saved_qualifying_scope; | |
6735 | parser->object_scope = saved_object_scope; | |
6736 | /* If the TYPE is invalid, indicate failure. */ | |
6737 | if (type == error_mark_node) | |
6738 | return error_mark_node; | |
6739 | return mangle_conv_op_name_for_type (type); | |
6740 | } | |
6741 | ||
6742 | /* Parse a conversion-type-id: | |
6743 | ||
6744 | conversion-type-id: | |
6745 | type-specifier-seq conversion-declarator [opt] | |
6746 | ||
6747 | Returns the TYPE specified. */ | |
6748 | ||
6749 | static tree | |
94edc4ab | 6750 | cp_parser_conversion_type_id (cp_parser* parser) |
a723baf1 MM |
6751 | { |
6752 | tree attributes; | |
6753 | tree type_specifiers; | |
6754 | tree declarator; | |
6755 | ||
6756 | /* Parse the attributes. */ | |
6757 | attributes = cp_parser_attributes_opt (parser); | |
6758 | /* Parse the type-specifiers. */ | |
6759 | type_specifiers = cp_parser_type_specifier_seq (parser); | |
6760 | /* If that didn't work, stop. */ | |
6761 | if (type_specifiers == error_mark_node) | |
6762 | return error_mark_node; | |
6763 | /* Parse the conversion-declarator. */ | |
6764 | declarator = cp_parser_conversion_declarator_opt (parser); | |
6765 | ||
6766 | return grokdeclarator (declarator, type_specifiers, TYPENAME, | |
6767 | /*initialized=*/0, &attributes); | |
6768 | } | |
6769 | ||
6770 | /* Parse an (optional) conversion-declarator. | |
6771 | ||
6772 | conversion-declarator: | |
6773 | ptr-operator conversion-declarator [opt] | |
6774 | ||
6775 | Returns a representation of the declarator. See | |
6776 | cp_parser_declarator for details. */ | |
6777 | ||
6778 | static tree | |
94edc4ab | 6779 | cp_parser_conversion_declarator_opt (cp_parser* parser) |
a723baf1 MM |
6780 | { |
6781 | enum tree_code code; | |
6782 | tree class_type; | |
6783 | tree cv_qualifier_seq; | |
6784 | ||
6785 | /* We don't know if there's a ptr-operator next, or not. */ | |
6786 | cp_parser_parse_tentatively (parser); | |
6787 | /* Try the ptr-operator. */ | |
6788 | code = cp_parser_ptr_operator (parser, &class_type, | |
6789 | &cv_qualifier_seq); | |
6790 | /* If it worked, look for more conversion-declarators. */ | |
6791 | if (cp_parser_parse_definitely (parser)) | |
6792 | { | |
6793 | tree declarator; | |
6794 | ||
6795 | /* Parse another optional declarator. */ | |
6796 | declarator = cp_parser_conversion_declarator_opt (parser); | |
6797 | ||
6798 | /* Create the representation of the declarator. */ | |
6799 | if (code == INDIRECT_REF) | |
6800 | declarator = make_pointer_declarator (cv_qualifier_seq, | |
6801 | declarator); | |
6802 | else | |
6803 | declarator = make_reference_declarator (cv_qualifier_seq, | |
6804 | declarator); | |
6805 | ||
6806 | /* Handle the pointer-to-member case. */ | |
6807 | if (class_type) | |
6808 | declarator = build_nt (SCOPE_REF, class_type, declarator); | |
6809 | ||
6810 | return declarator; | |
6811 | } | |
6812 | ||
6813 | return NULL_TREE; | |
6814 | } | |
6815 | ||
6816 | /* Parse an (optional) ctor-initializer. | |
6817 | ||
6818 | ctor-initializer: | |
6819 | : mem-initializer-list | |
6820 | ||
6821 | Returns TRUE iff the ctor-initializer was actually present. */ | |
6822 | ||
6823 | static bool | |
94edc4ab | 6824 | cp_parser_ctor_initializer_opt (cp_parser* parser) |
a723baf1 MM |
6825 | { |
6826 | /* If the next token is not a `:', then there is no | |
6827 | ctor-initializer. */ | |
6828 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) | |
6829 | { | |
6830 | /* Do default initialization of any bases and members. */ | |
6831 | if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
6832 | finish_mem_initializers (NULL_TREE); | |
6833 | ||
6834 | return false; | |
6835 | } | |
6836 | ||
6837 | /* Consume the `:' token. */ | |
6838 | cp_lexer_consume_token (parser->lexer); | |
6839 | /* And the mem-initializer-list. */ | |
6840 | cp_parser_mem_initializer_list (parser); | |
6841 | ||
6842 | return true; | |
6843 | } | |
6844 | ||
6845 | /* Parse a mem-initializer-list. | |
6846 | ||
6847 | mem-initializer-list: | |
6848 | mem-initializer | |
6849 | mem-initializer , mem-initializer-list */ | |
6850 | ||
6851 | static void | |
94edc4ab | 6852 | cp_parser_mem_initializer_list (cp_parser* parser) |
a723baf1 MM |
6853 | { |
6854 | tree mem_initializer_list = NULL_TREE; | |
6855 | ||
6856 | /* Let the semantic analysis code know that we are starting the | |
6857 | mem-initializer-list. */ | |
0e136342 MM |
6858 | if (!DECL_CONSTRUCTOR_P (current_function_decl)) |
6859 | error ("only constructors take base initializers"); | |
a723baf1 MM |
6860 | |
6861 | /* Loop through the list. */ | |
6862 | while (true) | |
6863 | { | |
6864 | tree mem_initializer; | |
6865 | ||
6866 | /* Parse the mem-initializer. */ | |
6867 | mem_initializer = cp_parser_mem_initializer (parser); | |
6868 | /* Add it to the list, unless it was erroneous. */ | |
6869 | if (mem_initializer) | |
6870 | { | |
6871 | TREE_CHAIN (mem_initializer) = mem_initializer_list; | |
6872 | mem_initializer_list = mem_initializer; | |
6873 | } | |
6874 | /* If the next token is not a `,', we're done. */ | |
6875 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
6876 | break; | |
6877 | /* Consume the `,' token. */ | |
6878 | cp_lexer_consume_token (parser->lexer); | |
6879 | } | |
6880 | ||
6881 | /* Perform semantic analysis. */ | |
0e136342 MM |
6882 | if (DECL_CONSTRUCTOR_P (current_function_decl)) |
6883 | finish_mem_initializers (mem_initializer_list); | |
a723baf1 MM |
6884 | } |
6885 | ||
6886 | /* Parse a mem-initializer. | |
6887 | ||
6888 | mem-initializer: | |
6889 | mem-initializer-id ( expression-list [opt] ) | |
6890 | ||
6891 | GNU extension: | |
6892 | ||
6893 | mem-initializer: | |
34cd5ae7 | 6894 | ( expression-list [opt] ) |
a723baf1 MM |
6895 | |
6896 | Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base | |
6897 | class) or FIELD_DECL (for a non-static data member) to initialize; | |
6898 | the TREE_VALUE is the expression-list. */ | |
6899 | ||
6900 | static tree | |
94edc4ab | 6901 | cp_parser_mem_initializer (cp_parser* parser) |
a723baf1 MM |
6902 | { |
6903 | tree mem_initializer_id; | |
6904 | tree expression_list; | |
1f5a253a NS |
6905 | tree member; |
6906 | ||
a723baf1 MM |
6907 | /* Find out what is being initialized. */ |
6908 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
6909 | { | |
6910 | pedwarn ("anachronistic old-style base class initializer"); | |
6911 | mem_initializer_id = NULL_TREE; | |
6912 | } | |
6913 | else | |
6914 | mem_initializer_id = cp_parser_mem_initializer_id (parser); | |
1f5a253a NS |
6915 | member = expand_member_init (mem_initializer_id); |
6916 | if (member && !DECL_P (member)) | |
6917 | in_base_initializer = 1; | |
7efa3e22 | 6918 | |
39703eb9 MM |
6919 | expression_list |
6920 | = cp_parser_parenthesized_expression_list (parser, false, | |
6921 | /*non_constant_p=*/NULL); | |
7efa3e22 | 6922 | if (!expression_list) |
a723baf1 | 6923 | expression_list = void_type_node; |
a723baf1 | 6924 | |
1f5a253a NS |
6925 | in_base_initializer = 0; |
6926 | ||
6927 | return member ? build_tree_list (member, expression_list) : NULL_TREE; | |
a723baf1 MM |
6928 | } |
6929 | ||
6930 | /* Parse a mem-initializer-id. | |
6931 | ||
6932 | mem-initializer-id: | |
6933 | :: [opt] nested-name-specifier [opt] class-name | |
6934 | identifier | |
6935 | ||
6936 | Returns a TYPE indicating the class to be initializer for the first | |
6937 | production. Returns an IDENTIFIER_NODE indicating the data member | |
6938 | to be initialized for the second production. */ | |
6939 | ||
6940 | static tree | |
94edc4ab | 6941 | cp_parser_mem_initializer_id (cp_parser* parser) |
a723baf1 MM |
6942 | { |
6943 | bool global_scope_p; | |
6944 | bool nested_name_specifier_p; | |
6945 | tree id; | |
6946 | ||
6947 | /* Look for the optional `::' operator. */ | |
6948 | global_scope_p | |
6949 | = (cp_parser_global_scope_opt (parser, | |
6950 | /*current_scope_valid_p=*/false) | |
6951 | != NULL_TREE); | |
6952 | /* Look for the optional nested-name-specifier. The simplest way to | |
6953 | implement: | |
6954 | ||
6955 | [temp.res] | |
6956 | ||
6957 | The keyword `typename' is not permitted in a base-specifier or | |
6958 | mem-initializer; in these contexts a qualified name that | |
6959 | depends on a template-parameter is implicitly assumed to be a | |
6960 | type name. | |
6961 | ||
6962 | is to assume that we have seen the `typename' keyword at this | |
6963 | point. */ | |
6964 | nested_name_specifier_p | |
6965 | = (cp_parser_nested_name_specifier_opt (parser, | |
6966 | /*typename_keyword_p=*/true, | |
6967 | /*check_dependency_p=*/true, | |
a668c6ad MM |
6968 | /*type_p=*/true, |
6969 | /*is_declaration=*/true) | |
a723baf1 MM |
6970 | != NULL_TREE); |
6971 | /* If there is a `::' operator or a nested-name-specifier, then we | |
6972 | are definitely looking for a class-name. */ | |
6973 | if (global_scope_p || nested_name_specifier_p) | |
6974 | return cp_parser_class_name (parser, | |
6975 | /*typename_keyword_p=*/true, | |
6976 | /*template_keyword_p=*/false, | |
6977 | /*type_p=*/false, | |
a723baf1 | 6978 | /*check_dependency_p=*/true, |
a668c6ad MM |
6979 | /*class_head_p=*/false, |
6980 | /*is_declaration=*/true); | |
a723baf1 MM |
6981 | /* Otherwise, we could also be looking for an ordinary identifier. */ |
6982 | cp_parser_parse_tentatively (parser); | |
6983 | /* Try a class-name. */ | |
6984 | id = cp_parser_class_name (parser, | |
6985 | /*typename_keyword_p=*/true, | |
6986 | /*template_keyword_p=*/false, | |
6987 | /*type_p=*/false, | |
a723baf1 | 6988 | /*check_dependency_p=*/true, |
a668c6ad MM |
6989 | /*class_head_p=*/false, |
6990 | /*is_declaration=*/true); | |
a723baf1 MM |
6991 | /* If we found one, we're done. */ |
6992 | if (cp_parser_parse_definitely (parser)) | |
6993 | return id; | |
6994 | /* Otherwise, look for an ordinary identifier. */ | |
6995 | return cp_parser_identifier (parser); | |
6996 | } | |
6997 | ||
6998 | /* Overloading [gram.over] */ | |
6999 | ||
7000 | /* Parse an operator-function-id. | |
7001 | ||
7002 | operator-function-id: | |
7003 | operator operator | |
7004 | ||
7005 | Returns an IDENTIFIER_NODE for the operator which is a | |
7006 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
7007 | ||
7008 | static tree | |
94edc4ab | 7009 | cp_parser_operator_function_id (cp_parser* parser) |
a723baf1 MM |
7010 | { |
7011 | /* Look for the `operator' keyword. */ | |
7012 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'")) | |
7013 | return error_mark_node; | |
7014 | /* And then the name of the operator itself. */ | |
7015 | return cp_parser_operator (parser); | |
7016 | } | |
7017 | ||
7018 | /* Parse an operator. | |
7019 | ||
7020 | operator: | |
7021 | new delete new[] delete[] + - * / % ^ & | ~ ! = < > | |
7022 | += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && | |
7023 | || ++ -- , ->* -> () [] | |
7024 | ||
7025 | GNU Extensions: | |
7026 | ||
7027 | operator: | |
7028 | <? >? <?= >?= | |
7029 | ||
7030 | Returns an IDENTIFIER_NODE for the operator which is a | |
7031 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
7032 | ||
7033 | static tree | |
94edc4ab | 7034 | cp_parser_operator (cp_parser* parser) |
a723baf1 MM |
7035 | { |
7036 | tree id = NULL_TREE; | |
7037 | cp_token *token; | |
7038 | ||
7039 | /* Peek at the next token. */ | |
7040 | token = cp_lexer_peek_token (parser->lexer); | |
7041 | /* Figure out which operator we have. */ | |
7042 | switch (token->type) | |
7043 | { | |
7044 | case CPP_KEYWORD: | |
7045 | { | |
7046 | enum tree_code op; | |
7047 | ||
7048 | /* The keyword should be either `new' or `delete'. */ | |
7049 | if (token->keyword == RID_NEW) | |
7050 | op = NEW_EXPR; | |
7051 | else if (token->keyword == RID_DELETE) | |
7052 | op = DELETE_EXPR; | |
7053 | else | |
7054 | break; | |
7055 | ||
7056 | /* Consume the `new' or `delete' token. */ | |
7057 | cp_lexer_consume_token (parser->lexer); | |
7058 | ||
7059 | /* Peek at the next token. */ | |
7060 | token = cp_lexer_peek_token (parser->lexer); | |
7061 | /* If it's a `[' token then this is the array variant of the | |
7062 | operator. */ | |
7063 | if (token->type == CPP_OPEN_SQUARE) | |
7064 | { | |
7065 | /* Consume the `[' token. */ | |
7066 | cp_lexer_consume_token (parser->lexer); | |
7067 | /* Look for the `]' token. */ | |
7068 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
7069 | id = ansi_opname (op == NEW_EXPR | |
7070 | ? VEC_NEW_EXPR : VEC_DELETE_EXPR); | |
7071 | } | |
7072 | /* Otherwise, we have the non-array variant. */ | |
7073 | else | |
7074 | id = ansi_opname (op); | |
7075 | ||
7076 | return id; | |
7077 | } | |
7078 | ||
7079 | case CPP_PLUS: | |
7080 | id = ansi_opname (PLUS_EXPR); | |
7081 | break; | |
7082 | ||
7083 | case CPP_MINUS: | |
7084 | id = ansi_opname (MINUS_EXPR); | |
7085 | break; | |
7086 | ||
7087 | case CPP_MULT: | |
7088 | id = ansi_opname (MULT_EXPR); | |
7089 | break; | |
7090 | ||
7091 | case CPP_DIV: | |
7092 | id = ansi_opname (TRUNC_DIV_EXPR); | |
7093 | break; | |
7094 | ||
7095 | case CPP_MOD: | |
7096 | id = ansi_opname (TRUNC_MOD_EXPR); | |
7097 | break; | |
7098 | ||
7099 | case CPP_XOR: | |
7100 | id = ansi_opname (BIT_XOR_EXPR); | |
7101 | break; | |
7102 | ||
7103 | case CPP_AND: | |
7104 | id = ansi_opname (BIT_AND_EXPR); | |
7105 | break; | |
7106 | ||
7107 | case CPP_OR: | |
7108 | id = ansi_opname (BIT_IOR_EXPR); | |
7109 | break; | |
7110 | ||
7111 | case CPP_COMPL: | |
7112 | id = ansi_opname (BIT_NOT_EXPR); | |
7113 | break; | |
7114 | ||
7115 | case CPP_NOT: | |
7116 | id = ansi_opname (TRUTH_NOT_EXPR); | |
7117 | break; | |
7118 | ||
7119 | case CPP_EQ: | |
7120 | id = ansi_assopname (NOP_EXPR); | |
7121 | break; | |
7122 | ||
7123 | case CPP_LESS: | |
7124 | id = ansi_opname (LT_EXPR); | |
7125 | break; | |
7126 | ||
7127 | case CPP_GREATER: | |
7128 | id = ansi_opname (GT_EXPR); | |
7129 | break; | |
7130 | ||
7131 | case CPP_PLUS_EQ: | |
7132 | id = ansi_assopname (PLUS_EXPR); | |
7133 | break; | |
7134 | ||
7135 | case CPP_MINUS_EQ: | |
7136 | id = ansi_assopname (MINUS_EXPR); | |
7137 | break; | |
7138 | ||
7139 | case CPP_MULT_EQ: | |
7140 | id = ansi_assopname (MULT_EXPR); | |
7141 | break; | |
7142 | ||
7143 | case CPP_DIV_EQ: | |
7144 | id = ansi_assopname (TRUNC_DIV_EXPR); | |
7145 | break; | |
7146 | ||
7147 | case CPP_MOD_EQ: | |
7148 | id = ansi_assopname (TRUNC_MOD_EXPR); | |
7149 | break; | |
7150 | ||
7151 | case CPP_XOR_EQ: | |
7152 | id = ansi_assopname (BIT_XOR_EXPR); | |
7153 | break; | |
7154 | ||
7155 | case CPP_AND_EQ: | |
7156 | id = ansi_assopname (BIT_AND_EXPR); | |
7157 | break; | |
7158 | ||
7159 | case CPP_OR_EQ: | |
7160 | id = ansi_assopname (BIT_IOR_EXPR); | |
7161 | break; | |
7162 | ||
7163 | case CPP_LSHIFT: | |
7164 | id = ansi_opname (LSHIFT_EXPR); | |
7165 | break; | |
7166 | ||
7167 | case CPP_RSHIFT: | |
7168 | id = ansi_opname (RSHIFT_EXPR); | |
7169 | break; | |
7170 | ||
7171 | case CPP_LSHIFT_EQ: | |
7172 | id = ansi_assopname (LSHIFT_EXPR); | |
7173 | break; | |
7174 | ||
7175 | case CPP_RSHIFT_EQ: | |
7176 | id = ansi_assopname (RSHIFT_EXPR); | |
7177 | break; | |
7178 | ||
7179 | case CPP_EQ_EQ: | |
7180 | id = ansi_opname (EQ_EXPR); | |
7181 | break; | |
7182 | ||
7183 | case CPP_NOT_EQ: | |
7184 | id = ansi_opname (NE_EXPR); | |
7185 | break; | |
7186 | ||
7187 | case CPP_LESS_EQ: | |
7188 | id = ansi_opname (LE_EXPR); | |
7189 | break; | |
7190 | ||
7191 | case CPP_GREATER_EQ: | |
7192 | id = ansi_opname (GE_EXPR); | |
7193 | break; | |
7194 | ||
7195 | case CPP_AND_AND: | |
7196 | id = ansi_opname (TRUTH_ANDIF_EXPR); | |
7197 | break; | |
7198 | ||
7199 | case CPP_OR_OR: | |
7200 | id = ansi_opname (TRUTH_ORIF_EXPR); | |
7201 | break; | |
7202 | ||
7203 | case CPP_PLUS_PLUS: | |
7204 | id = ansi_opname (POSTINCREMENT_EXPR); | |
7205 | break; | |
7206 | ||
7207 | case CPP_MINUS_MINUS: | |
7208 | id = ansi_opname (PREDECREMENT_EXPR); | |
7209 | break; | |
7210 | ||
7211 | case CPP_COMMA: | |
7212 | id = ansi_opname (COMPOUND_EXPR); | |
7213 | break; | |
7214 | ||
7215 | case CPP_DEREF_STAR: | |
7216 | id = ansi_opname (MEMBER_REF); | |
7217 | break; | |
7218 | ||
7219 | case CPP_DEREF: | |
7220 | id = ansi_opname (COMPONENT_REF); | |
7221 | break; | |
7222 | ||
7223 | case CPP_OPEN_PAREN: | |
7224 | /* Consume the `('. */ | |
7225 | cp_lexer_consume_token (parser->lexer); | |
7226 | /* Look for the matching `)'. */ | |
7227 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
7228 | return ansi_opname (CALL_EXPR); | |
7229 | ||
7230 | case CPP_OPEN_SQUARE: | |
7231 | /* Consume the `['. */ | |
7232 | cp_lexer_consume_token (parser->lexer); | |
7233 | /* Look for the matching `]'. */ | |
7234 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
7235 | return ansi_opname (ARRAY_REF); | |
7236 | ||
7237 | /* Extensions. */ | |
7238 | case CPP_MIN: | |
7239 | id = ansi_opname (MIN_EXPR); | |
7240 | break; | |
7241 | ||
7242 | case CPP_MAX: | |
7243 | id = ansi_opname (MAX_EXPR); | |
7244 | break; | |
7245 | ||
7246 | case CPP_MIN_EQ: | |
7247 | id = ansi_assopname (MIN_EXPR); | |
7248 | break; | |
7249 | ||
7250 | case CPP_MAX_EQ: | |
7251 | id = ansi_assopname (MAX_EXPR); | |
7252 | break; | |
7253 | ||
7254 | default: | |
7255 | /* Anything else is an error. */ | |
7256 | break; | |
7257 | } | |
7258 | ||
7259 | /* If we have selected an identifier, we need to consume the | |
7260 | operator token. */ | |
7261 | if (id) | |
7262 | cp_lexer_consume_token (parser->lexer); | |
7263 | /* Otherwise, no valid operator name was present. */ | |
7264 | else | |
7265 | { | |
7266 | cp_parser_error (parser, "expected operator"); | |
7267 | id = error_mark_node; | |
7268 | } | |
7269 | ||
7270 | return id; | |
7271 | } | |
7272 | ||
7273 | /* Parse a template-declaration. | |
7274 | ||
7275 | template-declaration: | |
7276 | export [opt] template < template-parameter-list > declaration | |
7277 | ||
7278 | If MEMBER_P is TRUE, this template-declaration occurs within a | |
7279 | class-specifier. | |
7280 | ||
7281 | The grammar rule given by the standard isn't correct. What | |
7282 | is really meant is: | |
7283 | ||
7284 | template-declaration: | |
7285 | export [opt] template-parameter-list-seq | |
7286 | decl-specifier-seq [opt] init-declarator [opt] ; | |
7287 | export [opt] template-parameter-list-seq | |
7288 | function-definition | |
7289 | ||
7290 | template-parameter-list-seq: | |
7291 | template-parameter-list-seq [opt] | |
7292 | template < template-parameter-list > */ | |
7293 | ||
7294 | static void | |
94edc4ab | 7295 | cp_parser_template_declaration (cp_parser* parser, bool member_p) |
a723baf1 MM |
7296 | { |
7297 | /* Check for `export'. */ | |
7298 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT)) | |
7299 | { | |
7300 | /* Consume the `export' token. */ | |
7301 | cp_lexer_consume_token (parser->lexer); | |
7302 | /* Warn that we do not support `export'. */ | |
7303 | warning ("keyword `export' not implemented, and will be ignored"); | |
7304 | } | |
7305 | ||
7306 | cp_parser_template_declaration_after_export (parser, member_p); | |
7307 | } | |
7308 | ||
7309 | /* Parse a template-parameter-list. | |
7310 | ||
7311 | template-parameter-list: | |
7312 | template-parameter | |
7313 | template-parameter-list , template-parameter | |
7314 | ||
7315 | Returns a TREE_LIST. Each node represents a template parameter. | |
7316 | The nodes are connected via their TREE_CHAINs. */ | |
7317 | ||
7318 | static tree | |
94edc4ab | 7319 | cp_parser_template_parameter_list (cp_parser* parser) |
a723baf1 MM |
7320 | { |
7321 | tree parameter_list = NULL_TREE; | |
7322 | ||
7323 | while (true) | |
7324 | { | |
7325 | tree parameter; | |
7326 | cp_token *token; | |
7327 | ||
7328 | /* Parse the template-parameter. */ | |
7329 | parameter = cp_parser_template_parameter (parser); | |
7330 | /* Add it to the list. */ | |
7331 | parameter_list = process_template_parm (parameter_list, | |
7332 | parameter); | |
7333 | ||
7334 | /* Peek at the next token. */ | |
7335 | token = cp_lexer_peek_token (parser->lexer); | |
7336 | /* If it's not a `,', we're done. */ | |
7337 | if (token->type != CPP_COMMA) | |
7338 | break; | |
7339 | /* Otherwise, consume the `,' token. */ | |
7340 | cp_lexer_consume_token (parser->lexer); | |
7341 | } | |
7342 | ||
7343 | return parameter_list; | |
7344 | } | |
7345 | ||
7346 | /* Parse a template-parameter. | |
7347 | ||
7348 | template-parameter: | |
7349 | type-parameter | |
7350 | parameter-declaration | |
7351 | ||
7352 | Returns a TREE_LIST. The TREE_VALUE represents the parameter. The | |
7353 | TREE_PURPOSE is the default value, if any. */ | |
7354 | ||
7355 | static tree | |
94edc4ab | 7356 | cp_parser_template_parameter (cp_parser* parser) |
a723baf1 MM |
7357 | { |
7358 | cp_token *token; | |
7359 | ||
7360 | /* Peek at the next token. */ | |
7361 | token = cp_lexer_peek_token (parser->lexer); | |
7362 | /* If it is `class' or `template', we have a type-parameter. */ | |
7363 | if (token->keyword == RID_TEMPLATE) | |
7364 | return cp_parser_type_parameter (parser); | |
7365 | /* If it is `class' or `typename' we do not know yet whether it is a | |
7366 | type parameter or a non-type parameter. Consider: | |
7367 | ||
7368 | template <typename T, typename T::X X> ... | |
7369 | ||
7370 | or: | |
7371 | ||
7372 | template <class C, class D*> ... | |
7373 | ||
7374 | Here, the first parameter is a type parameter, and the second is | |
7375 | a non-type parameter. We can tell by looking at the token after | |
7376 | the identifier -- if it is a `,', `=', or `>' then we have a type | |
7377 | parameter. */ | |
7378 | if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS) | |
7379 | { | |
7380 | /* Peek at the token after `class' or `typename'. */ | |
7381 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
7382 | /* If it's an identifier, skip it. */ | |
7383 | if (token->type == CPP_NAME) | |
7384 | token = cp_lexer_peek_nth_token (parser->lexer, 3); | |
7385 | /* Now, see if the token looks like the end of a template | |
7386 | parameter. */ | |
7387 | if (token->type == CPP_COMMA | |
7388 | || token->type == CPP_EQ | |
7389 | || token->type == CPP_GREATER) | |
7390 | return cp_parser_type_parameter (parser); | |
7391 | } | |
7392 | ||
7393 | /* Otherwise, it is a non-type parameter. | |
7394 | ||
7395 | [temp.param] | |
7396 | ||
7397 | When parsing a default template-argument for a non-type | |
7398 | template-parameter, the first non-nested `>' is taken as the end | |
7399 | of the template parameter-list rather than a greater-than | |
7400 | operator. */ | |
7401 | return | |
ec194454 | 7402 | cp_parser_parameter_declaration (parser, /*template_parm_p=*/true); |
a723baf1 MM |
7403 | } |
7404 | ||
7405 | /* Parse a type-parameter. | |
7406 | ||
7407 | type-parameter: | |
7408 | class identifier [opt] | |
7409 | class identifier [opt] = type-id | |
7410 | typename identifier [opt] | |
7411 | typename identifier [opt] = type-id | |
7412 | template < template-parameter-list > class identifier [opt] | |
7413 | template < template-parameter-list > class identifier [opt] | |
7414 | = id-expression | |
7415 | ||
7416 | Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The | |
7417 | TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is | |
7418 | the declaration of the parameter. */ | |
7419 | ||
7420 | static tree | |
94edc4ab | 7421 | cp_parser_type_parameter (cp_parser* parser) |
a723baf1 MM |
7422 | { |
7423 | cp_token *token; | |
7424 | tree parameter; | |
7425 | ||
7426 | /* Look for a keyword to tell us what kind of parameter this is. */ | |
7427 | token = cp_parser_require (parser, CPP_KEYWORD, | |
8a6393df | 7428 | "`class', `typename', or `template'"); |
a723baf1 MM |
7429 | if (!token) |
7430 | return error_mark_node; | |
7431 | ||
7432 | switch (token->keyword) | |
7433 | { | |
7434 | case RID_CLASS: | |
7435 | case RID_TYPENAME: | |
7436 | { | |
7437 | tree identifier; | |
7438 | tree default_argument; | |
7439 | ||
7440 | /* If the next token is an identifier, then it names the | |
7441 | parameter. */ | |
7442 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
7443 | identifier = cp_parser_identifier (parser); | |
7444 | else | |
7445 | identifier = NULL_TREE; | |
7446 | ||
7447 | /* Create the parameter. */ | |
7448 | parameter = finish_template_type_parm (class_type_node, identifier); | |
7449 | ||
7450 | /* If the next token is an `=', we have a default argument. */ | |
7451 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
7452 | { | |
7453 | /* Consume the `=' token. */ | |
7454 | cp_lexer_consume_token (parser->lexer); | |
34cd5ae7 | 7455 | /* Parse the default-argument. */ |
a723baf1 MM |
7456 | default_argument = cp_parser_type_id (parser); |
7457 | } | |
7458 | else | |
7459 | default_argument = NULL_TREE; | |
7460 | ||
7461 | /* Create the combined representation of the parameter and the | |
7462 | default argument. */ | |
c67d36d0 | 7463 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
7464 | } |
7465 | break; | |
7466 | ||
7467 | case RID_TEMPLATE: | |
7468 | { | |
7469 | tree parameter_list; | |
7470 | tree identifier; | |
7471 | tree default_argument; | |
7472 | ||
7473 | /* Look for the `<'. */ | |
7474 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
7475 | /* Parse the template-parameter-list. */ | |
7476 | begin_template_parm_list (); | |
7477 | parameter_list | |
7478 | = cp_parser_template_parameter_list (parser); | |
7479 | parameter_list = end_template_parm_list (parameter_list); | |
7480 | /* Look for the `>'. */ | |
7481 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
7482 | /* Look for the `class' keyword. */ | |
7483 | cp_parser_require_keyword (parser, RID_CLASS, "`class'"); | |
7484 | /* If the next token is an `=', then there is a | |
7485 | default-argument. If the next token is a `>', we are at | |
7486 | the end of the parameter-list. If the next token is a `,', | |
7487 | then we are at the end of this parameter. */ | |
7488 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ) | |
7489 | && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER) | |
7490 | && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
7491 | identifier = cp_parser_identifier (parser); | |
7492 | else | |
7493 | identifier = NULL_TREE; | |
7494 | /* Create the template parameter. */ | |
7495 | parameter = finish_template_template_parm (class_type_node, | |
7496 | identifier); | |
7497 | ||
7498 | /* If the next token is an `=', then there is a | |
7499 | default-argument. */ | |
7500 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
7501 | { | |
7502 | /* Consume the `='. */ | |
7503 | cp_lexer_consume_token (parser->lexer); | |
7504 | /* Parse the id-expression. */ | |
7505 | default_argument | |
7506 | = cp_parser_id_expression (parser, | |
7507 | /*template_keyword_p=*/false, | |
7508 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
7509 | /*template_p=*/NULL, |
7510 | /*declarator_p=*/false); | |
a723baf1 MM |
7511 | /* Look up the name. */ |
7512 | default_argument | |
7513 | = cp_parser_lookup_name_simple (parser, default_argument); | |
7514 | /* See if the default argument is valid. */ | |
7515 | default_argument | |
7516 | = check_template_template_default_arg (default_argument); | |
7517 | } | |
7518 | else | |
7519 | default_argument = NULL_TREE; | |
7520 | ||
7521 | /* Create the combined representation of the parameter and the | |
7522 | default argument. */ | |
c67d36d0 | 7523 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
7524 | } |
7525 | break; | |
7526 | ||
7527 | default: | |
7528 | /* Anything else is an error. */ | |
7529 | cp_parser_error (parser, | |
7530 | "expected `class', `typename', or `template'"); | |
7531 | parameter = error_mark_node; | |
7532 | } | |
7533 | ||
7534 | return parameter; | |
7535 | } | |
7536 | ||
7537 | /* Parse a template-id. | |
7538 | ||
7539 | template-id: | |
7540 | template-name < template-argument-list [opt] > | |
7541 | ||
7542 | If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the | |
7543 | `template' keyword. In this case, a TEMPLATE_ID_EXPR will be | |
7544 | returned. Otherwise, if the template-name names a function, or set | |
7545 | of functions, returns a TEMPLATE_ID_EXPR. If the template-name | |
7546 | names a class, returns a TYPE_DECL for the specialization. | |
7547 | ||
7548 | If CHECK_DEPENDENCY_P is FALSE, names are looked up in | |
7549 | uninstantiated templates. */ | |
7550 | ||
7551 | static tree | |
7552 | cp_parser_template_id (cp_parser *parser, | |
7553 | bool template_keyword_p, | |
a668c6ad MM |
7554 | bool check_dependency_p, |
7555 | bool is_declaration) | |
a723baf1 MM |
7556 | { |
7557 | tree template; | |
7558 | tree arguments; | |
a723baf1 | 7559 | tree template_id; |
a723baf1 MM |
7560 | ptrdiff_t start_of_id; |
7561 | tree access_check = NULL_TREE; | |
2050a1bb | 7562 | cp_token *next_token; |
a668c6ad | 7563 | bool is_identifier; |
a723baf1 MM |
7564 | |
7565 | /* If the next token corresponds to a template-id, there is no need | |
7566 | to reparse it. */ | |
2050a1bb MM |
7567 | next_token = cp_lexer_peek_token (parser->lexer); |
7568 | if (next_token->type == CPP_TEMPLATE_ID) | |
a723baf1 MM |
7569 | { |
7570 | tree value; | |
7571 | tree check; | |
7572 | ||
7573 | /* Get the stored value. */ | |
7574 | value = cp_lexer_consume_token (parser->lexer)->value; | |
7575 | /* Perform any access checks that were deferred. */ | |
7576 | for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check)) | |
cf22909c KL |
7577 | perform_or_defer_access_check (TREE_PURPOSE (check), |
7578 | TREE_VALUE (check)); | |
a723baf1 MM |
7579 | /* Return the stored value. */ |
7580 | return TREE_VALUE (value); | |
7581 | } | |
7582 | ||
2050a1bb MM |
7583 | /* Avoid performing name lookup if there is no possibility of |
7584 | finding a template-id. */ | |
7585 | if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR) | |
7586 | || (next_token->type == CPP_NAME | |
7587 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)) | |
7588 | { | |
7589 | cp_parser_error (parser, "expected template-id"); | |
7590 | return error_mark_node; | |
7591 | } | |
7592 | ||
a723baf1 MM |
7593 | /* Remember where the template-id starts. */ |
7594 | if (cp_parser_parsing_tentatively (parser) | |
7595 | && !cp_parser_committed_to_tentative_parse (parser)) | |
7596 | { | |
2050a1bb | 7597 | next_token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
7598 | start_of_id = cp_lexer_token_difference (parser->lexer, |
7599 | parser->lexer->first_token, | |
7600 | next_token); | |
a723baf1 MM |
7601 | } |
7602 | else | |
7603 | start_of_id = -1; | |
7604 | ||
8d241e0b | 7605 | push_deferring_access_checks (dk_deferred); |
cf22909c | 7606 | |
a723baf1 | 7607 | /* Parse the template-name. */ |
a668c6ad | 7608 | is_identifier = false; |
a723baf1 | 7609 | template = cp_parser_template_name (parser, template_keyword_p, |
a668c6ad MM |
7610 | check_dependency_p, |
7611 | is_declaration, | |
7612 | &is_identifier); | |
7613 | if (template == error_mark_node || is_identifier) | |
cf22909c KL |
7614 | { |
7615 | pop_deferring_access_checks (); | |
a668c6ad | 7616 | return template; |
cf22909c | 7617 | } |
a723baf1 MM |
7618 | |
7619 | /* Look for the `<' that starts the template-argument-list. */ | |
7620 | if (!cp_parser_require (parser, CPP_LESS, "`<'")) | |
cf22909c KL |
7621 | { |
7622 | pop_deferring_access_checks (); | |
7623 | return error_mark_node; | |
7624 | } | |
a723baf1 | 7625 | |
ec75414f MM |
7626 | /* Parse the arguments. */ |
7627 | arguments = cp_parser_enclosed_template_argument_list (parser); | |
a723baf1 MM |
7628 | |
7629 | /* Build a representation of the specialization. */ | |
7630 | if (TREE_CODE (template) == IDENTIFIER_NODE) | |
7631 | template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments); | |
7632 | else if (DECL_CLASS_TEMPLATE_P (template) | |
7633 | || DECL_TEMPLATE_TEMPLATE_PARM_P (template)) | |
7634 | template_id | |
7635 | = finish_template_type (template, arguments, | |
7636 | cp_lexer_next_token_is (parser->lexer, | |
7637 | CPP_SCOPE)); | |
7638 | else | |
7639 | { | |
7640 | /* If it's not a class-template or a template-template, it should be | |
7641 | a function-template. */ | |
7642 | my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template) | |
7643 | || TREE_CODE (template) == OVERLOAD | |
7644 | || BASELINK_P (template)), | |
7645 | 20010716); | |
7646 | ||
7647 | template_id = lookup_template_function (template, arguments); | |
7648 | } | |
7649 | ||
cf22909c KL |
7650 | /* Retrieve any deferred checks. Do not pop this access checks yet |
7651 | so the memory will not be reclaimed during token replacing below. */ | |
7652 | access_check = get_deferred_access_checks (); | |
7653 | ||
a723baf1 MM |
7654 | /* If parsing tentatively, replace the sequence of tokens that makes |
7655 | up the template-id with a CPP_TEMPLATE_ID token. That way, | |
7656 | should we re-parse the token stream, we will not have to repeat | |
7657 | the effort required to do the parse, nor will we issue duplicate | |
7658 | error messages about problems during instantiation of the | |
7659 | template. */ | |
7660 | if (start_of_id >= 0) | |
7661 | { | |
7662 | cp_token *token; | |
a723baf1 MM |
7663 | |
7664 | /* Find the token that corresponds to the start of the | |
7665 | template-id. */ | |
7666 | token = cp_lexer_advance_token (parser->lexer, | |
7667 | parser->lexer->first_token, | |
7668 | start_of_id); | |
7669 | ||
a723baf1 MM |
7670 | /* Reset the contents of the START_OF_ID token. */ |
7671 | token->type = CPP_TEMPLATE_ID; | |
7672 | token->value = build_tree_list (access_check, template_id); | |
7673 | token->keyword = RID_MAX; | |
7674 | /* Purge all subsequent tokens. */ | |
7675 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
7676 | } | |
7677 | ||
cf22909c | 7678 | pop_deferring_access_checks (); |
a723baf1 MM |
7679 | return template_id; |
7680 | } | |
7681 | ||
7682 | /* Parse a template-name. | |
7683 | ||
7684 | template-name: | |
7685 | identifier | |
7686 | ||
7687 | The standard should actually say: | |
7688 | ||
7689 | template-name: | |
7690 | identifier | |
7691 | operator-function-id | |
7692 | conversion-function-id | |
7693 | ||
7694 | A defect report has been filed about this issue. | |
7695 | ||
7696 | If TEMPLATE_KEYWORD_P is true, then we have just seen the | |
7697 | `template' keyword, in a construction like: | |
7698 | ||
7699 | T::template f<3>() | |
7700 | ||
7701 | In that case `f' is taken to be a template-name, even though there | |
7702 | is no way of knowing for sure. | |
7703 | ||
7704 | Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the | |
7705 | name refers to a set of overloaded functions, at least one of which | |
7706 | is a template, or an IDENTIFIER_NODE with the name of the template, | |
7707 | if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE, | |
7708 | names are looked up inside uninstantiated templates. */ | |
7709 | ||
7710 | static tree | |
94edc4ab NN |
7711 | cp_parser_template_name (cp_parser* parser, |
7712 | bool template_keyword_p, | |
a668c6ad MM |
7713 | bool check_dependency_p, |
7714 | bool is_declaration, | |
7715 | bool *is_identifier) | |
a723baf1 MM |
7716 | { |
7717 | tree identifier; | |
7718 | tree decl; | |
7719 | tree fns; | |
7720 | ||
7721 | /* If the next token is `operator', then we have either an | |
7722 | operator-function-id or a conversion-function-id. */ | |
7723 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR)) | |
7724 | { | |
7725 | /* We don't know whether we're looking at an | |
7726 | operator-function-id or a conversion-function-id. */ | |
7727 | cp_parser_parse_tentatively (parser); | |
7728 | /* Try an operator-function-id. */ | |
7729 | identifier = cp_parser_operator_function_id (parser); | |
7730 | /* If that didn't work, try a conversion-function-id. */ | |
7731 | if (!cp_parser_parse_definitely (parser)) | |
7732 | identifier = cp_parser_conversion_function_id (parser); | |
7733 | } | |
7734 | /* Look for the identifier. */ | |
7735 | else | |
7736 | identifier = cp_parser_identifier (parser); | |
7737 | ||
7738 | /* If we didn't find an identifier, we don't have a template-id. */ | |
7739 | if (identifier == error_mark_node) | |
7740 | return error_mark_node; | |
7741 | ||
7742 | /* If the name immediately followed the `template' keyword, then it | |
7743 | is a template-name. However, if the next token is not `<', then | |
7744 | we do not treat it as a template-name, since it is not being used | |
7745 | as part of a template-id. This enables us to handle constructs | |
7746 | like: | |
7747 | ||
7748 | template <typename T> struct S { S(); }; | |
7749 | template <typename T> S<T>::S(); | |
7750 | ||
7751 | correctly. We would treat `S' as a template -- if it were `S<T>' | |
7752 | -- but we do not if there is no `<'. */ | |
a668c6ad MM |
7753 | |
7754 | if (processing_template_decl | |
a723baf1 | 7755 | && cp_lexer_next_token_is (parser->lexer, CPP_LESS)) |
a668c6ad MM |
7756 | { |
7757 | /* In a declaration, in a dependent context, we pretend that the | |
7758 | "template" keyword was present in order to improve error | |
7759 | recovery. For example, given: | |
7760 | ||
7761 | template <typename T> void f(T::X<int>); | |
7762 | ||
7763 | we want to treat "X<int>" as a template-id. */ | |
7764 | if (is_declaration | |
7765 | && !template_keyword_p | |
7766 | && parser->scope && TYPE_P (parser->scope) | |
7767 | && dependent_type_p (parser->scope)) | |
7768 | { | |
7769 | ptrdiff_t start; | |
7770 | cp_token* token; | |
7771 | /* Explain what went wrong. */ | |
7772 | error ("non-template `%D' used as template", identifier); | |
7773 | error ("(use `%T::template %D' to indicate that it is a template)", | |
7774 | parser->scope, identifier); | |
7775 | /* If parsing tentatively, find the location of the "<" | |
7776 | token. */ | |
7777 | if (cp_parser_parsing_tentatively (parser) | |
7778 | && !cp_parser_committed_to_tentative_parse (parser)) | |
7779 | { | |
7780 | cp_parser_simulate_error (parser); | |
7781 | token = cp_lexer_peek_token (parser->lexer); | |
7782 | token = cp_lexer_prev_token (parser->lexer, token); | |
7783 | start = cp_lexer_token_difference (parser->lexer, | |
7784 | parser->lexer->first_token, | |
7785 | token); | |
7786 | } | |
7787 | else | |
7788 | start = -1; | |
7789 | /* Parse the template arguments so that we can issue error | |
7790 | messages about them. */ | |
7791 | cp_lexer_consume_token (parser->lexer); | |
7792 | cp_parser_enclosed_template_argument_list (parser); | |
7793 | /* Skip tokens until we find a good place from which to | |
7794 | continue parsing. */ | |
7795 | cp_parser_skip_to_closing_parenthesis (parser, | |
7796 | /*recovering=*/true, | |
7797 | /*or_comma=*/true, | |
7798 | /*consume_paren=*/false); | |
7799 | /* If parsing tentatively, permanently remove the | |
7800 | template argument list. That will prevent duplicate | |
7801 | error messages from being issued about the missing | |
7802 | "template" keyword. */ | |
7803 | if (start >= 0) | |
7804 | { | |
7805 | token = cp_lexer_advance_token (parser->lexer, | |
7806 | parser->lexer->first_token, | |
7807 | start); | |
7808 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
7809 | } | |
7810 | if (is_identifier) | |
7811 | *is_identifier = true; | |
7812 | return identifier; | |
7813 | } | |
7814 | if (template_keyword_p) | |
7815 | return identifier; | |
7816 | } | |
a723baf1 MM |
7817 | |
7818 | /* Look up the name. */ | |
7819 | decl = cp_parser_lookup_name (parser, identifier, | |
a723baf1 | 7820 | /*is_type=*/false, |
eea9800f | 7821 | /*is_namespace=*/false, |
a723baf1 MM |
7822 | check_dependency_p); |
7823 | decl = maybe_get_template_decl_from_type_decl (decl); | |
7824 | ||
7825 | /* If DECL is a template, then the name was a template-name. */ | |
7826 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
7827 | ; | |
7828 | else | |
7829 | { | |
7830 | /* The standard does not explicitly indicate whether a name that | |
7831 | names a set of overloaded declarations, some of which are | |
7832 | templates, is a template-name. However, such a name should | |
7833 | be a template-name; otherwise, there is no way to form a | |
7834 | template-id for the overloaded templates. */ | |
7835 | fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl; | |
7836 | if (TREE_CODE (fns) == OVERLOAD) | |
7837 | { | |
7838 | tree fn; | |
7839 | ||
7840 | for (fn = fns; fn; fn = OVL_NEXT (fn)) | |
7841 | if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL) | |
7842 | break; | |
7843 | } | |
7844 | else | |
7845 | { | |
7846 | /* Otherwise, the name does not name a template. */ | |
7847 | cp_parser_error (parser, "expected template-name"); | |
7848 | return error_mark_node; | |
7849 | } | |
7850 | } | |
7851 | ||
7852 | /* If DECL is dependent, and refers to a function, then just return | |
7853 | its name; we will look it up again during template instantiation. */ | |
7854 | if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl)) | |
7855 | { | |
7856 | tree scope = CP_DECL_CONTEXT (get_first_fn (decl)); | |
1fb3244a | 7857 | if (TYPE_P (scope) && dependent_type_p (scope)) |
a723baf1 MM |
7858 | return identifier; |
7859 | } | |
7860 | ||
7861 | return decl; | |
7862 | } | |
7863 | ||
7864 | /* Parse a template-argument-list. | |
7865 | ||
7866 | template-argument-list: | |
7867 | template-argument | |
7868 | template-argument-list , template-argument | |
7869 | ||
04c06002 | 7870 | Returns a TREE_VEC containing the arguments. */ |
a723baf1 MM |
7871 | |
7872 | static tree | |
94edc4ab | 7873 | cp_parser_template_argument_list (cp_parser* parser) |
a723baf1 | 7874 | { |
bf12d54d NS |
7875 | tree fixed_args[10]; |
7876 | unsigned n_args = 0; | |
7877 | unsigned alloced = 10; | |
7878 | tree *arg_ary = fixed_args; | |
7879 | tree vec; | |
a723baf1 | 7880 | |
bf12d54d | 7881 | do |
a723baf1 MM |
7882 | { |
7883 | tree argument; | |
7884 | ||
bf12d54d | 7885 | if (n_args) |
04c06002 | 7886 | /* Consume the comma. */ |
bf12d54d NS |
7887 | cp_lexer_consume_token (parser->lexer); |
7888 | ||
a723baf1 MM |
7889 | /* Parse the template-argument. */ |
7890 | argument = cp_parser_template_argument (parser); | |
bf12d54d NS |
7891 | if (n_args == alloced) |
7892 | { | |
7893 | alloced *= 2; | |
7894 | ||
7895 | if (arg_ary == fixed_args) | |
7896 | { | |
7897 | arg_ary = xmalloc (sizeof (tree) * alloced); | |
7898 | memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); | |
7899 | } | |
7900 | else | |
7901 | arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced); | |
7902 | } | |
7903 | arg_ary[n_args++] = argument; | |
a723baf1 | 7904 | } |
bf12d54d NS |
7905 | while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); |
7906 | ||
7907 | vec = make_tree_vec (n_args); | |
a723baf1 | 7908 | |
bf12d54d NS |
7909 | while (n_args--) |
7910 | TREE_VEC_ELT (vec, n_args) = arg_ary[n_args]; | |
7911 | ||
7912 | if (arg_ary != fixed_args) | |
7913 | free (arg_ary); | |
7914 | return vec; | |
a723baf1 MM |
7915 | } |
7916 | ||
7917 | /* Parse a template-argument. | |
7918 | ||
7919 | template-argument: | |
7920 | assignment-expression | |
7921 | type-id | |
7922 | id-expression | |
7923 | ||
7924 | The representation is that of an assignment-expression, type-id, or | |
7925 | id-expression -- except that the qualified id-expression is | |
7926 | evaluated, so that the value returned is either a DECL or an | |
d17811fd MM |
7927 | OVERLOAD. |
7928 | ||
7929 | Although the standard says "assignment-expression", it forbids | |
7930 | throw-expressions or assignments in the template argument. | |
7931 | Therefore, we use "conditional-expression" instead. */ | |
a723baf1 MM |
7932 | |
7933 | static tree | |
94edc4ab | 7934 | cp_parser_template_argument (cp_parser* parser) |
a723baf1 MM |
7935 | { |
7936 | tree argument; | |
7937 | bool template_p; | |
d17811fd MM |
7938 | bool address_p; |
7939 | cp_token *token; | |
b3445994 | 7940 | cp_id_kind idk; |
d17811fd | 7941 | tree qualifying_class; |
a723baf1 MM |
7942 | |
7943 | /* There's really no way to know what we're looking at, so we just | |
7944 | try each alternative in order. | |
7945 | ||
7946 | [temp.arg] | |
7947 | ||
7948 | In a template-argument, an ambiguity between a type-id and an | |
7949 | expression is resolved to a type-id, regardless of the form of | |
7950 | the corresponding template-parameter. | |
7951 | ||
7952 | Therefore, we try a type-id first. */ | |
7953 | cp_parser_parse_tentatively (parser); | |
a723baf1 MM |
7954 | argument = cp_parser_type_id (parser); |
7955 | /* If the next token isn't a `,' or a `>', then this argument wasn't | |
7956 | really finished. */ | |
d17811fd | 7957 | if (!cp_parser_next_token_ends_template_argument_p (parser)) |
a723baf1 MM |
7958 | cp_parser_error (parser, "expected template-argument"); |
7959 | /* If that worked, we're done. */ | |
7960 | if (cp_parser_parse_definitely (parser)) | |
7961 | return argument; | |
7962 | /* We're still not sure what the argument will be. */ | |
7963 | cp_parser_parse_tentatively (parser); | |
7964 | /* Try a template. */ | |
7965 | argument = cp_parser_id_expression (parser, | |
7966 | /*template_keyword_p=*/false, | |
7967 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
7968 | &template_p, |
7969 | /*declarator_p=*/false); | |
a723baf1 MM |
7970 | /* If the next token isn't a `,' or a `>', then this argument wasn't |
7971 | really finished. */ | |
d17811fd | 7972 | if (!cp_parser_next_token_ends_template_argument_p (parser)) |
a723baf1 MM |
7973 | cp_parser_error (parser, "expected template-argument"); |
7974 | if (!cp_parser_error_occurred (parser)) | |
7975 | { | |
7976 | /* Figure out what is being referred to. */ | |
7977 | argument = cp_parser_lookup_name_simple (parser, argument); | |
7978 | if (template_p) | |
7979 | argument = make_unbound_class_template (TREE_OPERAND (argument, 0), | |
7980 | TREE_OPERAND (argument, 1), | |
78757caa | 7981 | tf_error); |
a723baf1 MM |
7982 | else if (TREE_CODE (argument) != TEMPLATE_DECL) |
7983 | cp_parser_error (parser, "expected template-name"); | |
7984 | } | |
7985 | if (cp_parser_parse_definitely (parser)) | |
7986 | return argument; | |
d17811fd MM |
7987 | /* It must be a non-type argument. There permitted cases are given |
7988 | in [temp.arg.nontype]: | |
7989 | ||
7990 | -- an integral constant-expression of integral or enumeration | |
7991 | type; or | |
7992 | ||
7993 | -- the name of a non-type template-parameter; or | |
7994 | ||
7995 | -- the name of an object or function with external linkage... | |
7996 | ||
7997 | -- the address of an object or function with external linkage... | |
7998 | ||
04c06002 | 7999 | -- a pointer to member... */ |
d17811fd MM |
8000 | /* Look for a non-type template parameter. */ |
8001 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
8002 | { | |
8003 | cp_parser_parse_tentatively (parser); | |
8004 | argument = cp_parser_primary_expression (parser, | |
8005 | &idk, | |
8006 | &qualifying_class); | |
8007 | if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX | |
8008 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
8009 | cp_parser_simulate_error (parser); | |
8010 | if (cp_parser_parse_definitely (parser)) | |
8011 | return argument; | |
8012 | } | |
8013 | /* If the next token is "&", the argument must be the address of an | |
8014 | object or function with external linkage. */ | |
8015 | address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND); | |
8016 | if (address_p) | |
8017 | cp_lexer_consume_token (parser->lexer); | |
8018 | /* See if we might have an id-expression. */ | |
8019 | token = cp_lexer_peek_token (parser->lexer); | |
8020 | if (token->type == CPP_NAME | |
8021 | || token->keyword == RID_OPERATOR | |
8022 | || token->type == CPP_SCOPE | |
8023 | || token->type == CPP_TEMPLATE_ID | |
8024 | || token->type == CPP_NESTED_NAME_SPECIFIER) | |
8025 | { | |
8026 | cp_parser_parse_tentatively (parser); | |
8027 | argument = cp_parser_primary_expression (parser, | |
8028 | &idk, | |
8029 | &qualifying_class); | |
8030 | if (cp_parser_error_occurred (parser) | |
8031 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
8032 | cp_parser_abort_tentative_parse (parser); | |
8033 | else | |
8034 | { | |
8035 | if (qualifying_class) | |
8036 | argument = finish_qualified_id_expr (qualifying_class, | |
8037 | argument, | |
8038 | /*done=*/true, | |
8039 | address_p); | |
8040 | if (TREE_CODE (argument) == VAR_DECL) | |
8041 | { | |
8042 | /* A variable without external linkage might still be a | |
8043 | valid constant-expression, so no error is issued here | |
8044 | if the external-linkage check fails. */ | |
8045 | if (!DECL_EXTERNAL_LINKAGE_P (argument)) | |
8046 | cp_parser_simulate_error (parser); | |
8047 | } | |
8048 | else if (is_overloaded_fn (argument)) | |
8049 | /* All overloaded functions are allowed; if the external | |
8050 | linkage test does not pass, an error will be issued | |
8051 | later. */ | |
8052 | ; | |
8053 | else if (address_p | |
8054 | && (TREE_CODE (argument) == OFFSET_REF | |
8055 | || TREE_CODE (argument) == SCOPE_REF)) | |
8056 | /* A pointer-to-member. */ | |
8057 | ; | |
8058 | else | |
8059 | cp_parser_simulate_error (parser); | |
8060 | ||
8061 | if (cp_parser_parse_definitely (parser)) | |
8062 | { | |
8063 | if (address_p) | |
8064 | argument = build_x_unary_op (ADDR_EXPR, argument); | |
8065 | return argument; | |
8066 | } | |
8067 | } | |
8068 | } | |
8069 | /* If the argument started with "&", there are no other valid | |
8070 | alternatives at this point. */ | |
8071 | if (address_p) | |
8072 | { | |
8073 | cp_parser_error (parser, "invalid non-type template argument"); | |
8074 | return error_mark_node; | |
8075 | } | |
04c06002 | 8076 | /* The argument must be a constant-expression. */ |
d17811fd MM |
8077 | argument = cp_parser_constant_expression (parser, |
8078 | /*allow_non_constant_p=*/false, | |
8079 | /*non_constant_p=*/NULL); | |
8080 | /* If it's non-dependent, simplify it. */ | |
8081 | return cp_parser_fold_non_dependent_expr (argument); | |
a723baf1 MM |
8082 | } |
8083 | ||
8084 | /* Parse an explicit-instantiation. | |
8085 | ||
8086 | explicit-instantiation: | |
8087 | template declaration | |
8088 | ||
8089 | Although the standard says `declaration', what it really means is: | |
8090 | ||
8091 | explicit-instantiation: | |
8092 | template decl-specifier-seq [opt] declarator [opt] ; | |
8093 | ||
8094 | Things like `template int S<int>::i = 5, int S<double>::j;' are not | |
8095 | supposed to be allowed. A defect report has been filed about this | |
8096 | issue. | |
8097 | ||
8098 | GNU Extension: | |
8099 | ||
8100 | explicit-instantiation: | |
8101 | storage-class-specifier template | |
8102 | decl-specifier-seq [opt] declarator [opt] ; | |
8103 | function-specifier template | |
8104 | decl-specifier-seq [opt] declarator [opt] ; */ | |
8105 | ||
8106 | static void | |
94edc4ab | 8107 | cp_parser_explicit_instantiation (cp_parser* parser) |
a723baf1 | 8108 | { |
560ad596 | 8109 | int declares_class_or_enum; |
a723baf1 MM |
8110 | tree decl_specifiers; |
8111 | tree attributes; | |
8112 | tree extension_specifier = NULL_TREE; | |
8113 | ||
8114 | /* Look for an (optional) storage-class-specifier or | |
8115 | function-specifier. */ | |
8116 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
8117 | { | |
8118 | extension_specifier | |
8119 | = cp_parser_storage_class_specifier_opt (parser); | |
8120 | if (!extension_specifier) | |
8121 | extension_specifier = cp_parser_function_specifier_opt (parser); | |
8122 | } | |
8123 | ||
8124 | /* Look for the `template' keyword. */ | |
8125 | cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"); | |
8126 | /* Let the front end know that we are processing an explicit | |
8127 | instantiation. */ | |
8128 | begin_explicit_instantiation (); | |
8129 | /* [temp.explicit] says that we are supposed to ignore access | |
8130 | control while processing explicit instantiation directives. */ | |
78757caa | 8131 | push_deferring_access_checks (dk_no_check); |
a723baf1 MM |
8132 | /* Parse a decl-specifier-seq. */ |
8133 | decl_specifiers | |
8134 | = cp_parser_decl_specifier_seq (parser, | |
8135 | CP_PARSER_FLAGS_OPTIONAL, | |
8136 | &attributes, | |
8137 | &declares_class_or_enum); | |
8138 | /* If there was exactly one decl-specifier, and it declared a class, | |
8139 | and there's no declarator, then we have an explicit type | |
8140 | instantiation. */ | |
8141 | if (declares_class_or_enum && cp_parser_declares_only_class_p (parser)) | |
8142 | { | |
8143 | tree type; | |
8144 | ||
8145 | type = check_tag_decl (decl_specifiers); | |
b7fc8b57 KL |
8146 | /* Turn access control back on for names used during |
8147 | template instantiation. */ | |
8148 | pop_deferring_access_checks (); | |
a723baf1 MM |
8149 | if (type) |
8150 | do_type_instantiation (type, extension_specifier, /*complain=*/1); | |
8151 | } | |
8152 | else | |
8153 | { | |
8154 | tree declarator; | |
8155 | tree decl; | |
8156 | ||
8157 | /* Parse the declarator. */ | |
8158 | declarator | |
62b8a44e | 8159 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
a723baf1 | 8160 | /*ctor_dtor_or_conv_p=*/NULL); |
560ad596 MM |
8161 | cp_parser_check_for_definition_in_return_type (declarator, |
8162 | declares_class_or_enum); | |
a723baf1 MM |
8163 | decl = grokdeclarator (declarator, decl_specifiers, |
8164 | NORMAL, 0, NULL); | |
b7fc8b57 KL |
8165 | /* Turn access control back on for names used during |
8166 | template instantiation. */ | |
8167 | pop_deferring_access_checks (); | |
a723baf1 MM |
8168 | /* Do the explicit instantiation. */ |
8169 | do_decl_instantiation (decl, extension_specifier); | |
8170 | } | |
8171 | /* We're done with the instantiation. */ | |
8172 | end_explicit_instantiation (); | |
a723baf1 | 8173 | |
e0860732 | 8174 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 MM |
8175 | } |
8176 | ||
8177 | /* Parse an explicit-specialization. | |
8178 | ||
8179 | explicit-specialization: | |
8180 | template < > declaration | |
8181 | ||
8182 | Although the standard says `declaration', what it really means is: | |
8183 | ||
8184 | explicit-specialization: | |
8185 | template <> decl-specifier [opt] init-declarator [opt] ; | |
8186 | template <> function-definition | |
8187 | template <> explicit-specialization | |
8188 | template <> template-declaration */ | |
8189 | ||
8190 | static void | |
94edc4ab | 8191 | cp_parser_explicit_specialization (cp_parser* parser) |
a723baf1 MM |
8192 | { |
8193 | /* Look for the `template' keyword. */ | |
8194 | cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"); | |
8195 | /* Look for the `<'. */ | |
8196 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
8197 | /* Look for the `>'. */ | |
8198 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
8199 | /* We have processed another parameter list. */ | |
8200 | ++parser->num_template_parameter_lists; | |
8201 | /* Let the front end know that we are beginning a specialization. */ | |
8202 | begin_specialization (); | |
8203 | ||
8204 | /* If the next keyword is `template', we need to figure out whether | |
8205 | or not we're looking a template-declaration. */ | |
8206 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
8207 | { | |
8208 | if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS | |
8209 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER) | |
8210 | cp_parser_template_declaration_after_export (parser, | |
8211 | /*member_p=*/false); | |
8212 | else | |
8213 | cp_parser_explicit_specialization (parser); | |
8214 | } | |
8215 | else | |
8216 | /* Parse the dependent declaration. */ | |
8217 | cp_parser_single_declaration (parser, | |
8218 | /*member_p=*/false, | |
8219 | /*friend_p=*/NULL); | |
8220 | ||
8221 | /* We're done with the specialization. */ | |
8222 | end_specialization (); | |
8223 | /* We're done with this parameter list. */ | |
8224 | --parser->num_template_parameter_lists; | |
8225 | } | |
8226 | ||
8227 | /* Parse a type-specifier. | |
8228 | ||
8229 | type-specifier: | |
8230 | simple-type-specifier | |
8231 | class-specifier | |
8232 | enum-specifier | |
8233 | elaborated-type-specifier | |
8234 | cv-qualifier | |
8235 | ||
8236 | GNU Extension: | |
8237 | ||
8238 | type-specifier: | |
8239 | __complex__ | |
8240 | ||
8241 | Returns a representation of the type-specifier. If the | |
8242 | type-specifier is a keyword (like `int' or `const', or | |
34cd5ae7 | 8243 | `__complex__') then the corresponding IDENTIFIER_NODE is returned. |
a723baf1 MM |
8244 | For a class-specifier, enum-specifier, or elaborated-type-specifier |
8245 | a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned. | |
8246 | ||
8247 | If IS_FRIEND is TRUE then this type-specifier is being declared a | |
8248 | `friend'. If IS_DECLARATION is TRUE, then this type-specifier is | |
8249 | appearing in a decl-specifier-seq. | |
8250 | ||
8251 | If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a | |
8252 | class-specifier, enum-specifier, or elaborated-type-specifier, then | |
83a00410 | 8253 | *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1 |
560ad596 MM |
8254 | if a type is declared; 2 if it is defined. Otherwise, it is set to |
8255 | zero. | |
a723baf1 MM |
8256 | |
8257 | If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a | |
8258 | cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it | |
8259 | is set to FALSE. */ | |
8260 | ||
8261 | static tree | |
94edc4ab NN |
8262 | cp_parser_type_specifier (cp_parser* parser, |
8263 | cp_parser_flags flags, | |
8264 | bool is_friend, | |
8265 | bool is_declaration, | |
560ad596 | 8266 | int* declares_class_or_enum, |
94edc4ab | 8267 | bool* is_cv_qualifier) |
a723baf1 MM |
8268 | { |
8269 | tree type_spec = NULL_TREE; | |
8270 | cp_token *token; | |
8271 | enum rid keyword; | |
8272 | ||
8273 | /* Assume this type-specifier does not declare a new type. */ | |
8274 | if (declares_class_or_enum) | |
8275 | *declares_class_or_enum = false; | |
8276 | /* And that it does not specify a cv-qualifier. */ | |
8277 | if (is_cv_qualifier) | |
8278 | *is_cv_qualifier = false; | |
8279 | /* Peek at the next token. */ | |
8280 | token = cp_lexer_peek_token (parser->lexer); | |
8281 | ||
8282 | /* If we're looking at a keyword, we can use that to guide the | |
8283 | production we choose. */ | |
8284 | keyword = token->keyword; | |
8285 | switch (keyword) | |
8286 | { | |
8287 | /* Any of these indicate either a class-specifier, or an | |
8288 | elaborated-type-specifier. */ | |
8289 | case RID_CLASS: | |
8290 | case RID_STRUCT: | |
8291 | case RID_UNION: | |
8292 | case RID_ENUM: | |
8293 | /* Parse tentatively so that we can back up if we don't find a | |
8294 | class-specifier or enum-specifier. */ | |
8295 | cp_parser_parse_tentatively (parser); | |
8296 | /* Look for the class-specifier or enum-specifier. */ | |
8297 | if (keyword == RID_ENUM) | |
8298 | type_spec = cp_parser_enum_specifier (parser); | |
8299 | else | |
8300 | type_spec = cp_parser_class_specifier (parser); | |
8301 | ||
8302 | /* If that worked, we're done. */ | |
8303 | if (cp_parser_parse_definitely (parser)) | |
8304 | { | |
8305 | if (declares_class_or_enum) | |
560ad596 | 8306 | *declares_class_or_enum = 2; |
a723baf1 MM |
8307 | return type_spec; |
8308 | } | |
8309 | ||
8310 | /* Fall through. */ | |
8311 | ||
8312 | case RID_TYPENAME: | |
8313 | /* Look for an elaborated-type-specifier. */ | |
8314 | type_spec = cp_parser_elaborated_type_specifier (parser, | |
8315 | is_friend, | |
8316 | is_declaration); | |
8317 | /* We're declaring a class or enum -- unless we're using | |
8318 | `typename'. */ | |
8319 | if (declares_class_or_enum && keyword != RID_TYPENAME) | |
560ad596 | 8320 | *declares_class_or_enum = 1; |
a723baf1 MM |
8321 | return type_spec; |
8322 | ||
8323 | case RID_CONST: | |
8324 | case RID_VOLATILE: | |
8325 | case RID_RESTRICT: | |
8326 | type_spec = cp_parser_cv_qualifier_opt (parser); | |
8327 | /* Even though we call a routine that looks for an optional | |
8328 | qualifier, we know that there should be one. */ | |
8329 | my_friendly_assert (type_spec != NULL, 20000328); | |
8330 | /* This type-specifier was a cv-qualified. */ | |
8331 | if (is_cv_qualifier) | |
8332 | *is_cv_qualifier = true; | |
8333 | ||
8334 | return type_spec; | |
8335 | ||
8336 | case RID_COMPLEX: | |
8337 | /* The `__complex__' keyword is a GNU extension. */ | |
8338 | return cp_lexer_consume_token (parser->lexer)->value; | |
8339 | ||
8340 | default: | |
8341 | break; | |
8342 | } | |
8343 | ||
8344 | /* If we do not already have a type-specifier, assume we are looking | |
8345 | at a simple-type-specifier. */ | |
4b0d3cbe MM |
8346 | type_spec = cp_parser_simple_type_specifier (parser, flags, |
8347 | /*identifier_p=*/true); | |
a723baf1 MM |
8348 | |
8349 | /* If we didn't find a type-specifier, and a type-specifier was not | |
8350 | optional in this context, issue an error message. */ | |
8351 | if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
8352 | { | |
8353 | cp_parser_error (parser, "expected type specifier"); | |
8354 | return error_mark_node; | |
8355 | } | |
8356 | ||
8357 | return type_spec; | |
8358 | } | |
8359 | ||
8360 | /* Parse a simple-type-specifier. | |
8361 | ||
8362 | simple-type-specifier: | |
8363 | :: [opt] nested-name-specifier [opt] type-name | |
8364 | :: [opt] nested-name-specifier template template-id | |
8365 | char | |
8366 | wchar_t | |
8367 | bool | |
8368 | short | |
8369 | int | |
8370 | long | |
8371 | signed | |
8372 | unsigned | |
8373 | float | |
8374 | double | |
8375 | void | |
8376 | ||
8377 | GNU Extension: | |
8378 | ||
8379 | simple-type-specifier: | |
8380 | __typeof__ unary-expression | |
8381 | __typeof__ ( type-id ) | |
8382 | ||
8383 | For the various keywords, the value returned is simply the | |
4b0d3cbe MM |
8384 | TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true. |
8385 | For the first two productions, and if IDENTIFIER_P is false, the | |
8386 | value returned is the indicated TYPE_DECL. */ | |
a723baf1 MM |
8387 | |
8388 | static tree | |
4b0d3cbe MM |
8389 | cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags, |
8390 | bool identifier_p) | |
a723baf1 MM |
8391 | { |
8392 | tree type = NULL_TREE; | |
8393 | cp_token *token; | |
8394 | ||
8395 | /* Peek at the next token. */ | |
8396 | token = cp_lexer_peek_token (parser->lexer); | |
8397 | ||
8398 | /* If we're looking at a keyword, things are easy. */ | |
8399 | switch (token->keyword) | |
8400 | { | |
8401 | case RID_CHAR: | |
4b0d3cbe MM |
8402 | type = char_type_node; |
8403 | break; | |
a723baf1 | 8404 | case RID_WCHAR: |
4b0d3cbe MM |
8405 | type = wchar_type_node; |
8406 | break; | |
a723baf1 | 8407 | case RID_BOOL: |
4b0d3cbe MM |
8408 | type = boolean_type_node; |
8409 | break; | |
a723baf1 | 8410 | case RID_SHORT: |
4b0d3cbe MM |
8411 | type = short_integer_type_node; |
8412 | break; | |
a723baf1 | 8413 | case RID_INT: |
4b0d3cbe MM |
8414 | type = integer_type_node; |
8415 | break; | |
a723baf1 | 8416 | case RID_LONG: |
4b0d3cbe MM |
8417 | type = long_integer_type_node; |
8418 | break; | |
a723baf1 | 8419 | case RID_SIGNED: |
4b0d3cbe MM |
8420 | type = integer_type_node; |
8421 | break; | |
a723baf1 | 8422 | case RID_UNSIGNED: |
4b0d3cbe MM |
8423 | type = unsigned_type_node; |
8424 | break; | |
a723baf1 | 8425 | case RID_FLOAT: |
4b0d3cbe MM |
8426 | type = float_type_node; |
8427 | break; | |
a723baf1 | 8428 | case RID_DOUBLE: |
4b0d3cbe MM |
8429 | type = double_type_node; |
8430 | break; | |
a723baf1 | 8431 | case RID_VOID: |
4b0d3cbe MM |
8432 | type = void_type_node; |
8433 | break; | |
a723baf1 MM |
8434 | |
8435 | case RID_TYPEOF: | |
8436 | { | |
8437 | tree operand; | |
8438 | ||
8439 | /* Consume the `typeof' token. */ | |
8440 | cp_lexer_consume_token (parser->lexer); | |
04c06002 | 8441 | /* Parse the operand to `typeof'. */ |
a723baf1 MM |
8442 | operand = cp_parser_sizeof_operand (parser, RID_TYPEOF); |
8443 | /* If it is not already a TYPE, take its type. */ | |
8444 | if (!TYPE_P (operand)) | |
8445 | operand = finish_typeof (operand); | |
8446 | ||
8447 | return operand; | |
8448 | } | |
8449 | ||
8450 | default: | |
8451 | break; | |
8452 | } | |
8453 | ||
4b0d3cbe MM |
8454 | /* If the type-specifier was for a built-in type, we're done. */ |
8455 | if (type) | |
8456 | { | |
8457 | tree id; | |
8458 | ||
8459 | /* Consume the token. */ | |
8460 | id = cp_lexer_consume_token (parser->lexer)->value; | |
8461 | return identifier_p ? id : TYPE_NAME (type); | |
8462 | } | |
8463 | ||
a723baf1 MM |
8464 | /* The type-specifier must be a user-defined type. */ |
8465 | if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) | |
8466 | { | |
8467 | /* Don't gobble tokens or issue error messages if this is an | |
8468 | optional type-specifier. */ | |
8469 | if (flags & CP_PARSER_FLAGS_OPTIONAL) | |
8470 | cp_parser_parse_tentatively (parser); | |
8471 | ||
8472 | /* Look for the optional `::' operator. */ | |
8473 | cp_parser_global_scope_opt (parser, | |
8474 | /*current_scope_valid_p=*/false); | |
8475 | /* Look for the nested-name specifier. */ | |
8476 | cp_parser_nested_name_specifier_opt (parser, | |
8477 | /*typename_keyword_p=*/false, | |
8478 | /*check_dependency_p=*/true, | |
a668c6ad MM |
8479 | /*type_p=*/false, |
8480 | /*is_declaration=*/false); | |
a723baf1 MM |
8481 | /* If we have seen a nested-name-specifier, and the next token |
8482 | is `template', then we are using the template-id production. */ | |
8483 | if (parser->scope | |
8484 | && cp_parser_optional_template_keyword (parser)) | |
8485 | { | |
8486 | /* Look for the template-id. */ | |
8487 | type = cp_parser_template_id (parser, | |
8488 | /*template_keyword_p=*/true, | |
a668c6ad MM |
8489 | /*check_dependency_p=*/true, |
8490 | /*is_declaration=*/false); | |
a723baf1 MM |
8491 | /* If the template-id did not name a type, we are out of |
8492 | luck. */ | |
8493 | if (TREE_CODE (type) != TYPE_DECL) | |
8494 | { | |
8495 | cp_parser_error (parser, "expected template-id for type"); | |
8496 | type = NULL_TREE; | |
8497 | } | |
8498 | } | |
8499 | /* Otherwise, look for a type-name. */ | |
8500 | else | |
8501 | { | |
8502 | type = cp_parser_type_name (parser); | |
8503 | if (type == error_mark_node) | |
8504 | type = NULL_TREE; | |
8505 | } | |
8506 | ||
8507 | /* If it didn't work out, we don't have a TYPE. */ | |
8508 | if ((flags & CP_PARSER_FLAGS_OPTIONAL) | |
8509 | && !cp_parser_parse_definitely (parser)) | |
8510 | type = NULL_TREE; | |
8511 | } | |
8512 | ||
8513 | /* If we didn't get a type-name, issue an error message. */ | |
8514 | if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
8515 | { | |
8516 | cp_parser_error (parser, "expected type-name"); | |
8517 | return error_mark_node; | |
8518 | } | |
8519 | ||
a668c6ad MM |
8520 | /* There is no valid C++ program where a non-template type is |
8521 | followed by a "<". That usually indicates that the user thought | |
8522 | that the type was a template. */ | |
ec75414f MM |
8523 | if (type && cp_lexer_next_token_is (parser->lexer, CPP_LESS)) |
8524 | { | |
8525 | error ("`%T' is not a template", TREE_TYPE (type)); | |
8526 | /* Consume the "<". */ | |
8527 | cp_lexer_consume_token (parser->lexer); | |
8528 | /* Parse the template arguments. */ | |
8529 | cp_parser_enclosed_template_argument_list (parser); | |
8530 | /* Attempt to recover by using the basic type, ignoring the | |
8531 | template arguments. */ | |
8532 | return type; | |
8533 | } | |
8534 | ||
a723baf1 MM |
8535 | return type; |
8536 | } | |
8537 | ||
8538 | /* Parse a type-name. | |
8539 | ||
8540 | type-name: | |
8541 | class-name | |
8542 | enum-name | |
8543 | typedef-name | |
8544 | ||
8545 | enum-name: | |
8546 | identifier | |
8547 | ||
8548 | typedef-name: | |
8549 | identifier | |
8550 | ||
8551 | Returns a TYPE_DECL for the the type. */ | |
8552 | ||
8553 | static tree | |
94edc4ab | 8554 | cp_parser_type_name (cp_parser* parser) |
a723baf1 MM |
8555 | { |
8556 | tree type_decl; | |
8557 | tree identifier; | |
8558 | ||
8559 | /* We can't know yet whether it is a class-name or not. */ | |
8560 | cp_parser_parse_tentatively (parser); | |
8561 | /* Try a class-name. */ | |
8562 | type_decl = cp_parser_class_name (parser, | |
8563 | /*typename_keyword_p=*/false, | |
8564 | /*template_keyword_p=*/false, | |
8565 | /*type_p=*/false, | |
a723baf1 | 8566 | /*check_dependency_p=*/true, |
a668c6ad MM |
8567 | /*class_head_p=*/false, |
8568 | /*is_declaration=*/false); | |
a723baf1 MM |
8569 | /* If it's not a class-name, keep looking. */ |
8570 | if (!cp_parser_parse_definitely (parser)) | |
8571 | { | |
8572 | /* It must be a typedef-name or an enum-name. */ | |
8573 | identifier = cp_parser_identifier (parser); | |
8574 | if (identifier == error_mark_node) | |
8575 | return error_mark_node; | |
8576 | ||
8577 | /* Look up the type-name. */ | |
8578 | type_decl = cp_parser_lookup_name_simple (parser, identifier); | |
8579 | /* Issue an error if we did not find a type-name. */ | |
8580 | if (TREE_CODE (type_decl) != TYPE_DECL) | |
8581 | { | |
8582 | cp_parser_error (parser, "expected type-name"); | |
8583 | type_decl = error_mark_node; | |
8584 | } | |
8585 | /* Remember that the name was used in the definition of the | |
8586 | current class so that we can check later to see if the | |
8587 | meaning would have been different after the class was | |
8588 | entirely defined. */ | |
8589 | else if (type_decl != error_mark_node | |
8590 | && !parser->scope) | |
8591 | maybe_note_name_used_in_class (identifier, type_decl); | |
8592 | } | |
8593 | ||
8594 | return type_decl; | |
8595 | } | |
8596 | ||
8597 | ||
8598 | /* Parse an elaborated-type-specifier. Note that the grammar given | |
8599 | here incorporates the resolution to DR68. | |
8600 | ||
8601 | elaborated-type-specifier: | |
8602 | class-key :: [opt] nested-name-specifier [opt] identifier | |
8603 | class-key :: [opt] nested-name-specifier [opt] template [opt] template-id | |
8604 | enum :: [opt] nested-name-specifier [opt] identifier | |
8605 | typename :: [opt] nested-name-specifier identifier | |
8606 | typename :: [opt] nested-name-specifier template [opt] | |
8607 | template-id | |
8608 | ||
8609 | If IS_FRIEND is TRUE, then this elaborated-type-specifier is being | |
8610 | declared `friend'. If IS_DECLARATION is TRUE, then this | |
8611 | elaborated-type-specifier appears in a decl-specifiers-seq, i.e., | |
8612 | something is being declared. | |
8613 | ||
8614 | Returns the TYPE specified. */ | |
8615 | ||
8616 | static tree | |
94edc4ab NN |
8617 | cp_parser_elaborated_type_specifier (cp_parser* parser, |
8618 | bool is_friend, | |
8619 | bool is_declaration) | |
a723baf1 MM |
8620 | { |
8621 | enum tag_types tag_type; | |
8622 | tree identifier; | |
8623 | tree type = NULL_TREE; | |
8624 | ||
8625 | /* See if we're looking at the `enum' keyword. */ | |
8626 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM)) | |
8627 | { | |
8628 | /* Consume the `enum' token. */ | |
8629 | cp_lexer_consume_token (parser->lexer); | |
8630 | /* Remember that it's an enumeration type. */ | |
8631 | tag_type = enum_type; | |
8632 | } | |
8633 | /* Or, it might be `typename'. */ | |
8634 | else if (cp_lexer_next_token_is_keyword (parser->lexer, | |
8635 | RID_TYPENAME)) | |
8636 | { | |
8637 | /* Consume the `typename' token. */ | |
8638 | cp_lexer_consume_token (parser->lexer); | |
8639 | /* Remember that it's a `typename' type. */ | |
8640 | tag_type = typename_type; | |
8641 | /* The `typename' keyword is only allowed in templates. */ | |
8642 | if (!processing_template_decl) | |
8643 | pedwarn ("using `typename' outside of template"); | |
8644 | } | |
8645 | /* Otherwise it must be a class-key. */ | |
8646 | else | |
8647 | { | |
8648 | tag_type = cp_parser_class_key (parser); | |
8649 | if (tag_type == none_type) | |
8650 | return error_mark_node; | |
8651 | } | |
8652 | ||
8653 | /* Look for the `::' operator. */ | |
8654 | cp_parser_global_scope_opt (parser, | |
8655 | /*current_scope_valid_p=*/false); | |
8656 | /* Look for the nested-name-specifier. */ | |
8657 | if (tag_type == typename_type) | |
8fa1ad0e MM |
8658 | { |
8659 | if (cp_parser_nested_name_specifier (parser, | |
8660 | /*typename_keyword_p=*/true, | |
8661 | /*check_dependency_p=*/true, | |
a668c6ad MM |
8662 | /*type_p=*/true, |
8663 | is_declaration) | |
8fa1ad0e MM |
8664 | == error_mark_node) |
8665 | return error_mark_node; | |
8666 | } | |
a723baf1 MM |
8667 | else |
8668 | /* Even though `typename' is not present, the proposed resolution | |
8669 | to Core Issue 180 says that in `class A<T>::B', `B' should be | |
8670 | considered a type-name, even if `A<T>' is dependent. */ | |
8671 | cp_parser_nested_name_specifier_opt (parser, | |
8672 | /*typename_keyword_p=*/true, | |
8673 | /*check_dependency_p=*/true, | |
a668c6ad MM |
8674 | /*type_p=*/true, |
8675 | is_declaration); | |
a723baf1 MM |
8676 | /* For everything but enumeration types, consider a template-id. */ |
8677 | if (tag_type != enum_type) | |
8678 | { | |
8679 | bool template_p = false; | |
8680 | tree decl; | |
8681 | ||
8682 | /* Allow the `template' keyword. */ | |
8683 | template_p = cp_parser_optional_template_keyword (parser); | |
8684 | /* If we didn't see `template', we don't know if there's a | |
8685 | template-id or not. */ | |
8686 | if (!template_p) | |
8687 | cp_parser_parse_tentatively (parser); | |
8688 | /* Parse the template-id. */ | |
8689 | decl = cp_parser_template_id (parser, template_p, | |
a668c6ad MM |
8690 | /*check_dependency_p=*/true, |
8691 | is_declaration); | |
a723baf1 MM |
8692 | /* If we didn't find a template-id, look for an ordinary |
8693 | identifier. */ | |
8694 | if (!template_p && !cp_parser_parse_definitely (parser)) | |
8695 | ; | |
8696 | /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is | |
8697 | in effect, then we must assume that, upon instantiation, the | |
8698 | template will correspond to a class. */ | |
8699 | else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR | |
8700 | && tag_type == typename_type) | |
8701 | type = make_typename_type (parser->scope, decl, | |
8702 | /*complain=*/1); | |
8703 | else | |
8704 | type = TREE_TYPE (decl); | |
8705 | } | |
8706 | ||
8707 | /* For an enumeration type, consider only a plain identifier. */ | |
8708 | if (!type) | |
8709 | { | |
8710 | identifier = cp_parser_identifier (parser); | |
8711 | ||
8712 | if (identifier == error_mark_node) | |
eb5abb39 NS |
8713 | { |
8714 | parser->scope = NULL_TREE; | |
8715 | return error_mark_node; | |
8716 | } | |
a723baf1 MM |
8717 | |
8718 | /* For a `typename', we needn't call xref_tag. */ | |
8719 | if (tag_type == typename_type) | |
8720 | return make_typename_type (parser->scope, identifier, | |
8721 | /*complain=*/1); | |
8722 | /* Look up a qualified name in the usual way. */ | |
8723 | if (parser->scope) | |
8724 | { | |
8725 | tree decl; | |
8726 | ||
8727 | /* In an elaborated-type-specifier, names are assumed to name | |
8728 | types, so we set IS_TYPE to TRUE when calling | |
8729 | cp_parser_lookup_name. */ | |
8730 | decl = cp_parser_lookup_name (parser, identifier, | |
a723baf1 | 8731 | /*is_type=*/true, |
eea9800f | 8732 | /*is_namespace=*/false, |
a723baf1 | 8733 | /*check_dependency=*/true); |
710b73e6 KL |
8734 | |
8735 | /* If we are parsing friend declaration, DECL may be a | |
8736 | TEMPLATE_DECL tree node here. However, we need to check | |
8737 | whether this TEMPLATE_DECL results in valid code. Consider | |
8738 | the following example: | |
8739 | ||
8740 | namespace N { | |
8741 | template <class T> class C {}; | |
8742 | } | |
8743 | class X { | |
8744 | template <class T> friend class N::C; // #1, valid code | |
8745 | }; | |
8746 | template <class T> class Y { | |
8747 | friend class N::C; // #2, invalid code | |
8748 | }; | |
8749 | ||
8750 | For both case #1 and #2, we arrive at a TEMPLATE_DECL after | |
8751 | name lookup of `N::C'. We see that friend declaration must | |
8752 | be template for the code to be valid. Note that | |
8753 | processing_template_decl does not work here since it is | |
8754 | always 1 for the above two cases. */ | |
8755 | ||
a723baf1 | 8756 | decl = (cp_parser_maybe_treat_template_as_class |
710b73e6 KL |
8757 | (decl, /*tag_name_p=*/is_friend |
8758 | && parser->num_template_parameter_lists)); | |
a723baf1 MM |
8759 | |
8760 | if (TREE_CODE (decl) != TYPE_DECL) | |
8761 | { | |
8762 | error ("expected type-name"); | |
8763 | return error_mark_node; | |
8764 | } | |
560ad596 MM |
8765 | |
8766 | if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE) | |
8767 | check_elaborated_type_specifier | |
4b0d3cbe | 8768 | (tag_type, decl, |
560ad596 MM |
8769 | (parser->num_template_parameter_lists |
8770 | || DECL_SELF_REFERENCE_P (decl))); | |
a723baf1 MM |
8771 | |
8772 | type = TREE_TYPE (decl); | |
8773 | } | |
8774 | else | |
8775 | { | |
8776 | /* An elaborated-type-specifier sometimes introduces a new type and | |
8777 | sometimes names an existing type. Normally, the rule is that it | |
8778 | introduces a new type only if there is not an existing type of | |
8779 | the same name already in scope. For example, given: | |
8780 | ||
8781 | struct S {}; | |
8782 | void f() { struct S s; } | |
8783 | ||
8784 | the `struct S' in the body of `f' is the same `struct S' as in | |
8785 | the global scope; the existing definition is used. However, if | |
8786 | there were no global declaration, this would introduce a new | |
8787 | local class named `S'. | |
8788 | ||
8789 | An exception to this rule applies to the following code: | |
8790 | ||
8791 | namespace N { struct S; } | |
8792 | ||
8793 | Here, the elaborated-type-specifier names a new type | |
8794 | unconditionally; even if there is already an `S' in the | |
8795 | containing scope this declaration names a new type. | |
8796 | This exception only applies if the elaborated-type-specifier | |
8797 | forms the complete declaration: | |
8798 | ||
8799 | [class.name] | |
8800 | ||
8801 | A declaration consisting solely of `class-key identifier ;' is | |
8802 | either a redeclaration of the name in the current scope or a | |
8803 | forward declaration of the identifier as a class name. It | |
8804 | introduces the name into the current scope. | |
8805 | ||
8806 | We are in this situation precisely when the next token is a `;'. | |
8807 | ||
8808 | An exception to the exception is that a `friend' declaration does | |
8809 | *not* name a new type; i.e., given: | |
8810 | ||
8811 | struct S { friend struct T; }; | |
8812 | ||
8813 | `T' is not a new type in the scope of `S'. | |
8814 | ||
8815 | Also, `new struct S' or `sizeof (struct S)' never results in the | |
8816 | definition of a new type; a new type can only be declared in a | |
9bcb9aae | 8817 | declaration context. */ |
a723baf1 MM |
8818 | |
8819 | type = xref_tag (tag_type, identifier, | |
8820 | /*attributes=*/NULL_TREE, | |
8821 | (is_friend | |
8822 | || !is_declaration | |
8823 | || cp_lexer_next_token_is_not (parser->lexer, | |
cbd63935 KL |
8824 | CPP_SEMICOLON)), |
8825 | parser->num_template_parameter_lists); | |
a723baf1 MM |
8826 | } |
8827 | } | |
8828 | if (tag_type != enum_type) | |
8829 | cp_parser_check_class_key (tag_type, type); | |
8830 | return type; | |
8831 | } | |
8832 | ||
8833 | /* Parse an enum-specifier. | |
8834 | ||
8835 | enum-specifier: | |
8836 | enum identifier [opt] { enumerator-list [opt] } | |
8837 | ||
8838 | Returns an ENUM_TYPE representing the enumeration. */ | |
8839 | ||
8840 | static tree | |
94edc4ab | 8841 | cp_parser_enum_specifier (cp_parser* parser) |
a723baf1 MM |
8842 | { |
8843 | cp_token *token; | |
8844 | tree identifier = NULL_TREE; | |
8845 | tree type; | |
8846 | ||
8847 | /* Look for the `enum' keyword. */ | |
8848 | if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'")) | |
8849 | return error_mark_node; | |
8850 | /* Peek at the next token. */ | |
8851 | token = cp_lexer_peek_token (parser->lexer); | |
8852 | ||
8853 | /* See if it is an identifier. */ | |
8854 | if (token->type == CPP_NAME) | |
8855 | identifier = cp_parser_identifier (parser); | |
8856 | ||
8857 | /* Look for the `{'. */ | |
8858 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
8859 | return error_mark_node; | |
8860 | ||
8861 | /* At this point, we're going ahead with the enum-specifier, even | |
8862 | if some other problem occurs. */ | |
8863 | cp_parser_commit_to_tentative_parse (parser); | |
8864 | ||
8865 | /* Issue an error message if type-definitions are forbidden here. */ | |
8866 | cp_parser_check_type_definition (parser); | |
8867 | ||
8868 | /* Create the new type. */ | |
8869 | type = start_enum (identifier ? identifier : make_anon_name ()); | |
8870 | ||
8871 | /* Peek at the next token. */ | |
8872 | token = cp_lexer_peek_token (parser->lexer); | |
8873 | /* If it's not a `}', then there are some enumerators. */ | |
8874 | if (token->type != CPP_CLOSE_BRACE) | |
8875 | cp_parser_enumerator_list (parser, type); | |
8876 | /* Look for the `}'. */ | |
8877 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
8878 | ||
8879 | /* Finish up the enumeration. */ | |
8880 | finish_enum (type); | |
8881 | ||
8882 | return type; | |
8883 | } | |
8884 | ||
8885 | /* Parse an enumerator-list. The enumerators all have the indicated | |
8886 | TYPE. | |
8887 | ||
8888 | enumerator-list: | |
8889 | enumerator-definition | |
8890 | enumerator-list , enumerator-definition */ | |
8891 | ||
8892 | static void | |
94edc4ab | 8893 | cp_parser_enumerator_list (cp_parser* parser, tree type) |
a723baf1 MM |
8894 | { |
8895 | while (true) | |
8896 | { | |
8897 | cp_token *token; | |
8898 | ||
8899 | /* Parse an enumerator-definition. */ | |
8900 | cp_parser_enumerator_definition (parser, type); | |
8901 | /* Peek at the next token. */ | |
8902 | token = cp_lexer_peek_token (parser->lexer); | |
8903 | /* If it's not a `,', then we've reached the end of the | |
8904 | list. */ | |
8905 | if (token->type != CPP_COMMA) | |
8906 | break; | |
8907 | /* Otherwise, consume the `,' and keep going. */ | |
8908 | cp_lexer_consume_token (parser->lexer); | |
8909 | /* If the next token is a `}', there is a trailing comma. */ | |
8910 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
8911 | { | |
8912 | if (pedantic && !in_system_header) | |
8913 | pedwarn ("comma at end of enumerator list"); | |
8914 | break; | |
8915 | } | |
8916 | } | |
8917 | } | |
8918 | ||
8919 | /* Parse an enumerator-definition. The enumerator has the indicated | |
8920 | TYPE. | |
8921 | ||
8922 | enumerator-definition: | |
8923 | enumerator | |
8924 | enumerator = constant-expression | |
8925 | ||
8926 | enumerator: | |
8927 | identifier */ | |
8928 | ||
8929 | static void | |
94edc4ab | 8930 | cp_parser_enumerator_definition (cp_parser* parser, tree type) |
a723baf1 MM |
8931 | { |
8932 | cp_token *token; | |
8933 | tree identifier; | |
8934 | tree value; | |
8935 | ||
8936 | /* Look for the identifier. */ | |
8937 | identifier = cp_parser_identifier (parser); | |
8938 | if (identifier == error_mark_node) | |
8939 | return; | |
8940 | ||
8941 | /* Peek at the next token. */ | |
8942 | token = cp_lexer_peek_token (parser->lexer); | |
8943 | /* If it's an `=', then there's an explicit value. */ | |
8944 | if (token->type == CPP_EQ) | |
8945 | { | |
8946 | /* Consume the `=' token. */ | |
8947 | cp_lexer_consume_token (parser->lexer); | |
8948 | /* Parse the value. */ | |
14d22dd6 | 8949 | value = cp_parser_constant_expression (parser, |
d17811fd | 8950 | /*allow_non_constant_p=*/false, |
14d22dd6 | 8951 | NULL); |
a723baf1 MM |
8952 | } |
8953 | else | |
8954 | value = NULL_TREE; | |
8955 | ||
8956 | /* Create the enumerator. */ | |
8957 | build_enumerator (identifier, value, type); | |
8958 | } | |
8959 | ||
8960 | /* Parse a namespace-name. | |
8961 | ||
8962 | namespace-name: | |
8963 | original-namespace-name | |
8964 | namespace-alias | |
8965 | ||
8966 | Returns the NAMESPACE_DECL for the namespace. */ | |
8967 | ||
8968 | static tree | |
94edc4ab | 8969 | cp_parser_namespace_name (cp_parser* parser) |
a723baf1 MM |
8970 | { |
8971 | tree identifier; | |
8972 | tree namespace_decl; | |
8973 | ||
8974 | /* Get the name of the namespace. */ | |
8975 | identifier = cp_parser_identifier (parser); | |
8976 | if (identifier == error_mark_node) | |
8977 | return error_mark_node; | |
8978 | ||
eea9800f MM |
8979 | /* Look up the identifier in the currently active scope. Look only |
8980 | for namespaces, due to: | |
8981 | ||
8982 | [basic.lookup.udir] | |
8983 | ||
8984 | When looking up a namespace-name in a using-directive or alias | |
8985 | definition, only namespace names are considered. | |
8986 | ||
8987 | And: | |
8988 | ||
8989 | [basic.lookup.qual] | |
8990 | ||
8991 | During the lookup of a name preceding the :: scope resolution | |
8992 | operator, object, function, and enumerator names are ignored. | |
8993 | ||
8994 | (Note that cp_parser_class_or_namespace_name only calls this | |
8995 | function if the token after the name is the scope resolution | |
8996 | operator.) */ | |
8997 | namespace_decl = cp_parser_lookup_name (parser, identifier, | |
eea9800f MM |
8998 | /*is_type=*/false, |
8999 | /*is_namespace=*/true, | |
9000 | /*check_dependency=*/true); | |
a723baf1 MM |
9001 | /* If it's not a namespace, issue an error. */ |
9002 | if (namespace_decl == error_mark_node | |
9003 | || TREE_CODE (namespace_decl) != NAMESPACE_DECL) | |
9004 | { | |
9005 | cp_parser_error (parser, "expected namespace-name"); | |
9006 | namespace_decl = error_mark_node; | |
9007 | } | |
9008 | ||
9009 | return namespace_decl; | |
9010 | } | |
9011 | ||
9012 | /* Parse a namespace-definition. | |
9013 | ||
9014 | namespace-definition: | |
9015 | named-namespace-definition | |
9016 | unnamed-namespace-definition | |
9017 | ||
9018 | named-namespace-definition: | |
9019 | original-namespace-definition | |
9020 | extension-namespace-definition | |
9021 | ||
9022 | original-namespace-definition: | |
9023 | namespace identifier { namespace-body } | |
9024 | ||
9025 | extension-namespace-definition: | |
9026 | namespace original-namespace-name { namespace-body } | |
9027 | ||
9028 | unnamed-namespace-definition: | |
9029 | namespace { namespace-body } */ | |
9030 | ||
9031 | static void | |
94edc4ab | 9032 | cp_parser_namespace_definition (cp_parser* parser) |
a723baf1 MM |
9033 | { |
9034 | tree identifier; | |
9035 | ||
9036 | /* Look for the `namespace' keyword. */ | |
9037 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
9038 | ||
9039 | /* Get the name of the namespace. We do not attempt to distinguish | |
9040 | between an original-namespace-definition and an | |
9041 | extension-namespace-definition at this point. The semantic | |
9042 | analysis routines are responsible for that. */ | |
9043 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
9044 | identifier = cp_parser_identifier (parser); | |
9045 | else | |
9046 | identifier = NULL_TREE; | |
9047 | ||
9048 | /* Look for the `{' to start the namespace. */ | |
9049 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
9050 | /* Start the namespace. */ | |
9051 | push_namespace (identifier); | |
9052 | /* Parse the body of the namespace. */ | |
9053 | cp_parser_namespace_body (parser); | |
9054 | /* Finish the namespace. */ | |
9055 | pop_namespace (); | |
9056 | /* Look for the final `}'. */ | |
9057 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
9058 | } | |
9059 | ||
9060 | /* Parse a namespace-body. | |
9061 | ||
9062 | namespace-body: | |
9063 | declaration-seq [opt] */ | |
9064 | ||
9065 | static void | |
94edc4ab | 9066 | cp_parser_namespace_body (cp_parser* parser) |
a723baf1 MM |
9067 | { |
9068 | cp_parser_declaration_seq_opt (parser); | |
9069 | } | |
9070 | ||
9071 | /* Parse a namespace-alias-definition. | |
9072 | ||
9073 | namespace-alias-definition: | |
9074 | namespace identifier = qualified-namespace-specifier ; */ | |
9075 | ||
9076 | static void | |
94edc4ab | 9077 | cp_parser_namespace_alias_definition (cp_parser* parser) |
a723baf1 MM |
9078 | { |
9079 | tree identifier; | |
9080 | tree namespace_specifier; | |
9081 | ||
9082 | /* Look for the `namespace' keyword. */ | |
9083 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
9084 | /* Look for the identifier. */ | |
9085 | identifier = cp_parser_identifier (parser); | |
9086 | if (identifier == error_mark_node) | |
9087 | return; | |
9088 | /* Look for the `=' token. */ | |
9089 | cp_parser_require (parser, CPP_EQ, "`='"); | |
9090 | /* Look for the qualified-namespace-specifier. */ | |
9091 | namespace_specifier | |
9092 | = cp_parser_qualified_namespace_specifier (parser); | |
9093 | /* Look for the `;' token. */ | |
9094 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
9095 | ||
9096 | /* Register the alias in the symbol table. */ | |
9097 | do_namespace_alias (identifier, namespace_specifier); | |
9098 | } | |
9099 | ||
9100 | /* Parse a qualified-namespace-specifier. | |
9101 | ||
9102 | qualified-namespace-specifier: | |
9103 | :: [opt] nested-name-specifier [opt] namespace-name | |
9104 | ||
9105 | Returns a NAMESPACE_DECL corresponding to the specified | |
9106 | namespace. */ | |
9107 | ||
9108 | static tree | |
94edc4ab | 9109 | cp_parser_qualified_namespace_specifier (cp_parser* parser) |
a723baf1 MM |
9110 | { |
9111 | /* Look for the optional `::'. */ | |
9112 | cp_parser_global_scope_opt (parser, | |
9113 | /*current_scope_valid_p=*/false); | |
9114 | ||
9115 | /* Look for the optional nested-name-specifier. */ | |
9116 | cp_parser_nested_name_specifier_opt (parser, | |
9117 | /*typename_keyword_p=*/false, | |
9118 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9119 | /*type_p=*/false, |
9120 | /*is_declaration=*/true); | |
a723baf1 MM |
9121 | |
9122 | return cp_parser_namespace_name (parser); | |
9123 | } | |
9124 | ||
9125 | /* Parse a using-declaration. | |
9126 | ||
9127 | using-declaration: | |
9128 | using typename [opt] :: [opt] nested-name-specifier unqualified-id ; | |
9129 | using :: unqualified-id ; */ | |
9130 | ||
9131 | static void | |
94edc4ab | 9132 | cp_parser_using_declaration (cp_parser* parser) |
a723baf1 MM |
9133 | { |
9134 | cp_token *token; | |
9135 | bool typename_p = false; | |
9136 | bool global_scope_p; | |
9137 | tree decl; | |
9138 | tree identifier; | |
9139 | tree scope; | |
9140 | ||
9141 | /* Look for the `using' keyword. */ | |
9142 | cp_parser_require_keyword (parser, RID_USING, "`using'"); | |
9143 | ||
9144 | /* Peek at the next token. */ | |
9145 | token = cp_lexer_peek_token (parser->lexer); | |
9146 | /* See if it's `typename'. */ | |
9147 | if (token->keyword == RID_TYPENAME) | |
9148 | { | |
9149 | /* Remember that we've seen it. */ | |
9150 | typename_p = true; | |
9151 | /* Consume the `typename' token. */ | |
9152 | cp_lexer_consume_token (parser->lexer); | |
9153 | } | |
9154 | ||
9155 | /* Look for the optional global scope qualification. */ | |
9156 | global_scope_p | |
9157 | = (cp_parser_global_scope_opt (parser, | |
9158 | /*current_scope_valid_p=*/false) | |
9159 | != NULL_TREE); | |
9160 | ||
9161 | /* If we saw `typename', or didn't see `::', then there must be a | |
9162 | nested-name-specifier present. */ | |
9163 | if (typename_p || !global_scope_p) | |
9164 | cp_parser_nested_name_specifier (parser, typename_p, | |
9165 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9166 | /*type_p=*/false, |
9167 | /*is_declaration=*/true); | |
a723baf1 MM |
9168 | /* Otherwise, we could be in either of the two productions. In that |
9169 | case, treat the nested-name-specifier as optional. */ | |
9170 | else | |
9171 | cp_parser_nested_name_specifier_opt (parser, | |
9172 | /*typename_keyword_p=*/false, | |
9173 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9174 | /*type_p=*/false, |
9175 | /*is_declaration=*/true); | |
a723baf1 MM |
9176 | |
9177 | /* Parse the unqualified-id. */ | |
9178 | identifier = cp_parser_unqualified_id (parser, | |
9179 | /*template_keyword_p=*/false, | |
f3c2dfc6 MM |
9180 | /*check_dependency_p=*/true, |
9181 | /*declarator_p=*/true); | |
a723baf1 MM |
9182 | |
9183 | /* The function we call to handle a using-declaration is different | |
9184 | depending on what scope we are in. */ | |
f3c2dfc6 MM |
9185 | if (identifier == error_mark_node) |
9186 | ; | |
9187 | else if (TREE_CODE (identifier) != IDENTIFIER_NODE | |
9188 | && TREE_CODE (identifier) != BIT_NOT_EXPR) | |
9189 | /* [namespace.udecl] | |
9190 | ||
9191 | A using declaration shall not name a template-id. */ | |
9192 | error ("a template-id may not appear in a using-declaration"); | |
a723baf1 MM |
9193 | else |
9194 | { | |
f3c2dfc6 MM |
9195 | scope = current_scope (); |
9196 | if (scope && TYPE_P (scope)) | |
4eb6d609 | 9197 | { |
f3c2dfc6 MM |
9198 | /* Create the USING_DECL. */ |
9199 | decl = do_class_using_decl (build_nt (SCOPE_REF, | |
9200 | parser->scope, | |
9201 | identifier)); | |
9202 | /* Add it to the list of members in this class. */ | |
9203 | finish_member_declaration (decl); | |
4eb6d609 | 9204 | } |
a723baf1 | 9205 | else |
f3c2dfc6 MM |
9206 | { |
9207 | decl = cp_parser_lookup_name_simple (parser, identifier); | |
9208 | if (decl == error_mark_node) | |
9209 | { | |
9210 | if (parser->scope && parser->scope != global_namespace) | |
9211 | error ("`%D::%D' has not been declared", | |
9212 | parser->scope, identifier); | |
9213 | else | |
9214 | error ("`::%D' has not been declared", identifier); | |
9215 | } | |
9216 | else if (scope) | |
9217 | do_local_using_decl (decl); | |
9218 | else | |
9219 | do_toplevel_using_decl (decl); | |
9220 | } | |
a723baf1 MM |
9221 | } |
9222 | ||
9223 | /* Look for the final `;'. */ | |
9224 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
9225 | } | |
9226 | ||
9227 | /* Parse a using-directive. | |
9228 | ||
9229 | using-directive: | |
9230 | using namespace :: [opt] nested-name-specifier [opt] | |
9231 | namespace-name ; */ | |
9232 | ||
9233 | static void | |
94edc4ab | 9234 | cp_parser_using_directive (cp_parser* parser) |
a723baf1 MM |
9235 | { |
9236 | tree namespace_decl; | |
86098eb8 | 9237 | tree attribs; |
a723baf1 MM |
9238 | |
9239 | /* Look for the `using' keyword. */ | |
9240 | cp_parser_require_keyword (parser, RID_USING, "`using'"); | |
9241 | /* And the `namespace' keyword. */ | |
9242 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
9243 | /* Look for the optional `::' operator. */ | |
9244 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); | |
34cd5ae7 | 9245 | /* And the optional nested-name-specifier. */ |
a723baf1 MM |
9246 | cp_parser_nested_name_specifier_opt (parser, |
9247 | /*typename_keyword_p=*/false, | |
9248 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9249 | /*type_p=*/false, |
9250 | /*is_declaration=*/true); | |
a723baf1 MM |
9251 | /* Get the namespace being used. */ |
9252 | namespace_decl = cp_parser_namespace_name (parser); | |
86098eb8 JM |
9253 | /* And any specified attributes. */ |
9254 | attribs = cp_parser_attributes_opt (parser); | |
a723baf1 | 9255 | /* Update the symbol table. */ |
86098eb8 | 9256 | parse_using_directive (namespace_decl, attribs); |
a723baf1 MM |
9257 | /* Look for the final `;'. */ |
9258 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
9259 | } | |
9260 | ||
9261 | /* Parse an asm-definition. | |
9262 | ||
9263 | asm-definition: | |
9264 | asm ( string-literal ) ; | |
9265 | ||
9266 | GNU Extension: | |
9267 | ||
9268 | asm-definition: | |
9269 | asm volatile [opt] ( string-literal ) ; | |
9270 | asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ; | |
9271 | asm volatile [opt] ( string-literal : asm-operand-list [opt] | |
9272 | : asm-operand-list [opt] ) ; | |
9273 | asm volatile [opt] ( string-literal : asm-operand-list [opt] | |
9274 | : asm-operand-list [opt] | |
9275 | : asm-operand-list [opt] ) ; */ | |
9276 | ||
9277 | static void | |
94edc4ab | 9278 | cp_parser_asm_definition (cp_parser* parser) |
a723baf1 MM |
9279 | { |
9280 | cp_token *token; | |
9281 | tree string; | |
9282 | tree outputs = NULL_TREE; | |
9283 | tree inputs = NULL_TREE; | |
9284 | tree clobbers = NULL_TREE; | |
9285 | tree asm_stmt; | |
9286 | bool volatile_p = false; | |
9287 | bool extended_p = false; | |
9288 | ||
9289 | /* Look for the `asm' keyword. */ | |
9290 | cp_parser_require_keyword (parser, RID_ASM, "`asm'"); | |
9291 | /* See if the next token is `volatile'. */ | |
9292 | if (cp_parser_allow_gnu_extensions_p (parser) | |
9293 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE)) | |
9294 | { | |
9295 | /* Remember that we saw the `volatile' keyword. */ | |
9296 | volatile_p = true; | |
9297 | /* Consume the token. */ | |
9298 | cp_lexer_consume_token (parser->lexer); | |
9299 | } | |
9300 | /* Look for the opening `('. */ | |
9301 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
9302 | /* Look for the string. */ | |
9303 | token = cp_parser_require (parser, CPP_STRING, "asm body"); | |
9304 | if (!token) | |
9305 | return; | |
9306 | string = token->value; | |
9307 | /* If we're allowing GNU extensions, check for the extended assembly | |
9308 | syntax. Unfortunately, the `:' tokens need not be separated by | |
9309 | a space in C, and so, for compatibility, we tolerate that here | |
9310 | too. Doing that means that we have to treat the `::' operator as | |
9311 | two `:' tokens. */ | |
9312 | if (cp_parser_allow_gnu_extensions_p (parser) | |
9313 | && at_function_scope_p () | |
9314 | && (cp_lexer_next_token_is (parser->lexer, CPP_COLON) | |
9315 | || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))) | |
9316 | { | |
9317 | bool inputs_p = false; | |
9318 | bool clobbers_p = false; | |
9319 | ||
9320 | /* The extended syntax was used. */ | |
9321 | extended_p = true; | |
9322 | ||
9323 | /* Look for outputs. */ | |
9324 | if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
9325 | { | |
9326 | /* Consume the `:'. */ | |
9327 | cp_lexer_consume_token (parser->lexer); | |
9328 | /* Parse the output-operands. */ | |
9329 | if (cp_lexer_next_token_is_not (parser->lexer, | |
9330 | CPP_COLON) | |
9331 | && cp_lexer_next_token_is_not (parser->lexer, | |
8caf4c38 MM |
9332 | CPP_SCOPE) |
9333 | && cp_lexer_next_token_is_not (parser->lexer, | |
9334 | CPP_CLOSE_PAREN)) | |
a723baf1 MM |
9335 | outputs = cp_parser_asm_operand_list (parser); |
9336 | } | |
9337 | /* If the next token is `::', there are no outputs, and the | |
9338 | next token is the beginning of the inputs. */ | |
9339 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
9340 | { | |
9341 | /* Consume the `::' token. */ | |
9342 | cp_lexer_consume_token (parser->lexer); | |
9343 | /* The inputs are coming next. */ | |
9344 | inputs_p = true; | |
9345 | } | |
9346 | ||
9347 | /* Look for inputs. */ | |
9348 | if (inputs_p | |
9349 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
9350 | { | |
9351 | if (!inputs_p) | |
9352 | /* Consume the `:'. */ | |
9353 | cp_lexer_consume_token (parser->lexer); | |
9354 | /* Parse the output-operands. */ | |
9355 | if (cp_lexer_next_token_is_not (parser->lexer, | |
9356 | CPP_COLON) | |
9357 | && cp_lexer_next_token_is_not (parser->lexer, | |
8caf4c38 MM |
9358 | CPP_SCOPE) |
9359 | && cp_lexer_next_token_is_not (parser->lexer, | |
9360 | CPP_CLOSE_PAREN)) | |
a723baf1 MM |
9361 | inputs = cp_parser_asm_operand_list (parser); |
9362 | } | |
9363 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
9364 | /* The clobbers are coming next. */ | |
9365 | clobbers_p = true; | |
9366 | ||
9367 | /* Look for clobbers. */ | |
9368 | if (clobbers_p | |
9369 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
9370 | { | |
9371 | if (!clobbers_p) | |
9372 | /* Consume the `:'. */ | |
9373 | cp_lexer_consume_token (parser->lexer); | |
9374 | /* Parse the clobbers. */ | |
8caf4c38 MM |
9375 | if (cp_lexer_next_token_is_not (parser->lexer, |
9376 | CPP_CLOSE_PAREN)) | |
9377 | clobbers = cp_parser_asm_clobber_list (parser); | |
a723baf1 MM |
9378 | } |
9379 | } | |
9380 | /* Look for the closing `)'. */ | |
9381 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
a668c6ad MM |
9382 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
9383 | /*consume_paren=*/true); | |
a723baf1 MM |
9384 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
9385 | ||
9386 | /* Create the ASM_STMT. */ | |
9387 | if (at_function_scope_p ()) | |
9388 | { | |
9389 | asm_stmt = | |
9390 | finish_asm_stmt (volatile_p | |
9391 | ? ridpointers[(int) RID_VOLATILE] : NULL_TREE, | |
9392 | string, outputs, inputs, clobbers); | |
9393 | /* If the extended syntax was not used, mark the ASM_STMT. */ | |
9394 | if (!extended_p) | |
9395 | ASM_INPUT_P (asm_stmt) = 1; | |
9396 | } | |
9397 | else | |
9398 | assemble_asm (string); | |
9399 | } | |
9400 | ||
9401 | /* Declarators [gram.dcl.decl] */ | |
9402 | ||
9403 | /* Parse an init-declarator. | |
9404 | ||
9405 | init-declarator: | |
9406 | declarator initializer [opt] | |
9407 | ||
9408 | GNU Extension: | |
9409 | ||
9410 | init-declarator: | |
9411 | declarator asm-specification [opt] attributes [opt] initializer [opt] | |
9412 | ||
9413 | The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator. | |
c8e4f0e9 | 9414 | Returns a representation of the entity declared. If MEMBER_P is TRUE, |
cf22909c KL |
9415 | then this declarator appears in a class scope. The new DECL created |
9416 | by this declarator is returned. | |
a723baf1 MM |
9417 | |
9418 | If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and | |
9419 | for a function-definition here as well. If the declarator is a | |
9420 | declarator for a function-definition, *FUNCTION_DEFINITION_P will | |
9421 | be TRUE upon return. By that point, the function-definition will | |
9422 | have been completely parsed. | |
9423 | ||
9424 | FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P | |
9425 | is FALSE. */ | |
9426 | ||
9427 | static tree | |
94edc4ab NN |
9428 | cp_parser_init_declarator (cp_parser* parser, |
9429 | tree decl_specifiers, | |
9430 | tree prefix_attributes, | |
9431 | bool function_definition_allowed_p, | |
9432 | bool member_p, | |
560ad596 | 9433 | int declares_class_or_enum, |
94edc4ab | 9434 | bool* function_definition_p) |
a723baf1 MM |
9435 | { |
9436 | cp_token *token; | |
9437 | tree declarator; | |
9438 | tree attributes; | |
9439 | tree asm_specification; | |
9440 | tree initializer; | |
9441 | tree decl = NULL_TREE; | |
9442 | tree scope; | |
a723baf1 MM |
9443 | bool is_initialized; |
9444 | bool is_parenthesized_init; | |
39703eb9 | 9445 | bool is_non_constant_init; |
7efa3e22 | 9446 | int ctor_dtor_or_conv_p; |
a723baf1 MM |
9447 | bool friend_p; |
9448 | ||
9449 | /* Assume that this is not the declarator for a function | |
9450 | definition. */ | |
9451 | if (function_definition_p) | |
9452 | *function_definition_p = false; | |
9453 | ||
9454 | /* Defer access checks while parsing the declarator; we cannot know | |
9455 | what names are accessible until we know what is being | |
9456 | declared. */ | |
cf22909c KL |
9457 | resume_deferring_access_checks (); |
9458 | ||
a723baf1 MM |
9459 | /* Parse the declarator. */ |
9460 | declarator | |
62b8a44e | 9461 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
a723baf1 MM |
9462 | &ctor_dtor_or_conv_p); |
9463 | /* Gather up the deferred checks. */ | |
cf22909c | 9464 | stop_deferring_access_checks (); |
24c0ef37 | 9465 | |
a723baf1 MM |
9466 | /* If the DECLARATOR was erroneous, there's no need to go |
9467 | further. */ | |
9468 | if (declarator == error_mark_node) | |
cf22909c | 9469 | return error_mark_node; |
a723baf1 | 9470 | |
560ad596 MM |
9471 | cp_parser_check_for_definition_in_return_type (declarator, |
9472 | declares_class_or_enum); | |
9473 | ||
a723baf1 MM |
9474 | /* Figure out what scope the entity declared by the DECLARATOR is |
9475 | located in. `grokdeclarator' sometimes changes the scope, so | |
9476 | we compute it now. */ | |
9477 | scope = get_scope_of_declarator (declarator); | |
9478 | ||
9479 | /* If we're allowing GNU extensions, look for an asm-specification | |
9480 | and attributes. */ | |
9481 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
9482 | { | |
9483 | /* Look for an asm-specification. */ | |
9484 | asm_specification = cp_parser_asm_specification_opt (parser); | |
9485 | /* And attributes. */ | |
9486 | attributes = cp_parser_attributes_opt (parser); | |
9487 | } | |
9488 | else | |
9489 | { | |
9490 | asm_specification = NULL_TREE; | |
9491 | attributes = NULL_TREE; | |
9492 | } | |
9493 | ||
9494 | /* Peek at the next token. */ | |
9495 | token = cp_lexer_peek_token (parser->lexer); | |
9496 | /* Check to see if the token indicates the start of a | |
9497 | function-definition. */ | |
9498 | if (cp_parser_token_starts_function_definition_p (token)) | |
9499 | { | |
9500 | if (!function_definition_allowed_p) | |
9501 | { | |
9502 | /* If a function-definition should not appear here, issue an | |
9503 | error message. */ | |
9504 | cp_parser_error (parser, | |
9505 | "a function-definition is not allowed here"); | |
9506 | return error_mark_node; | |
9507 | } | |
9508 | else | |
9509 | { | |
a723baf1 MM |
9510 | /* Neither attributes nor an asm-specification are allowed |
9511 | on a function-definition. */ | |
9512 | if (asm_specification) | |
9513 | error ("an asm-specification is not allowed on a function-definition"); | |
9514 | if (attributes) | |
9515 | error ("attributes are not allowed on a function-definition"); | |
9516 | /* This is a function-definition. */ | |
9517 | *function_definition_p = true; | |
9518 | ||
a723baf1 MM |
9519 | /* Parse the function definition. */ |
9520 | decl = (cp_parser_function_definition_from_specifiers_and_declarator | |
cf22909c | 9521 | (parser, decl_specifiers, prefix_attributes, declarator)); |
24c0ef37 | 9522 | |
a723baf1 MM |
9523 | return decl; |
9524 | } | |
9525 | } | |
9526 | ||
9527 | /* [dcl.dcl] | |
9528 | ||
9529 | Only in function declarations for constructors, destructors, and | |
9530 | type conversions can the decl-specifier-seq be omitted. | |
9531 | ||
9532 | We explicitly postpone this check past the point where we handle | |
9533 | function-definitions because we tolerate function-definitions | |
9534 | that are missing their return types in some modes. */ | |
7efa3e22 | 9535 | if (!decl_specifiers && ctor_dtor_or_conv_p <= 0) |
a723baf1 MM |
9536 | { |
9537 | cp_parser_error (parser, | |
9538 | "expected constructor, destructor, or type conversion"); | |
9539 | return error_mark_node; | |
9540 | } | |
9541 | ||
9542 | /* An `=' or an `(' indicates an initializer. */ | |
9543 | is_initialized = (token->type == CPP_EQ | |
9544 | || token->type == CPP_OPEN_PAREN); | |
9545 | /* If the init-declarator isn't initialized and isn't followed by a | |
9546 | `,' or `;', it's not a valid init-declarator. */ | |
9547 | if (!is_initialized | |
9548 | && token->type != CPP_COMMA | |
9549 | && token->type != CPP_SEMICOLON) | |
9550 | { | |
9551 | cp_parser_error (parser, "expected init-declarator"); | |
9552 | return error_mark_node; | |
9553 | } | |
9554 | ||
9555 | /* Because start_decl has side-effects, we should only call it if we | |
9556 | know we're going ahead. By this point, we know that we cannot | |
9557 | possibly be looking at any other construct. */ | |
9558 | cp_parser_commit_to_tentative_parse (parser); | |
9559 | ||
9560 | /* Check to see whether or not this declaration is a friend. */ | |
9561 | friend_p = cp_parser_friend_p (decl_specifiers); | |
9562 | ||
9563 | /* Check that the number of template-parameter-lists is OK. */ | |
ee3071ef | 9564 | if (!cp_parser_check_declarator_template_parameters (parser, declarator)) |
cf22909c | 9565 | return error_mark_node; |
a723baf1 MM |
9566 | |
9567 | /* Enter the newly declared entry in the symbol table. If we're | |
9568 | processing a declaration in a class-specifier, we wait until | |
9569 | after processing the initializer. */ | |
9570 | if (!member_p) | |
9571 | { | |
9572 | if (parser->in_unbraced_linkage_specification_p) | |
9573 | { | |
9574 | decl_specifiers = tree_cons (error_mark_node, | |
9575 | get_identifier ("extern"), | |
9576 | decl_specifiers); | |
9577 | have_extern_spec = false; | |
9578 | } | |
ee3071ef NS |
9579 | decl = start_decl (declarator, decl_specifiers, |
9580 | is_initialized, attributes, prefix_attributes); | |
a723baf1 MM |
9581 | } |
9582 | ||
9583 | /* Enter the SCOPE. That way unqualified names appearing in the | |
9584 | initializer will be looked up in SCOPE. */ | |
9585 | if (scope) | |
9586 | push_scope (scope); | |
9587 | ||
9588 | /* Perform deferred access control checks, now that we know in which | |
9589 | SCOPE the declared entity resides. */ | |
9590 | if (!member_p && decl) | |
9591 | { | |
9592 | tree saved_current_function_decl = NULL_TREE; | |
9593 | ||
9594 | /* If the entity being declared is a function, pretend that we | |
9595 | are in its scope. If it is a `friend', it may have access to | |
9bcb9aae | 9596 | things that would not otherwise be accessible. */ |
a723baf1 MM |
9597 | if (TREE_CODE (decl) == FUNCTION_DECL) |
9598 | { | |
9599 | saved_current_function_decl = current_function_decl; | |
9600 | current_function_decl = decl; | |
9601 | } | |
9602 | ||
cf22909c KL |
9603 | /* Perform the access control checks for the declarator and the |
9604 | the decl-specifiers. */ | |
9605 | perform_deferred_access_checks (); | |
a723baf1 MM |
9606 | |
9607 | /* Restore the saved value. */ | |
9608 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
9609 | current_function_decl = saved_current_function_decl; | |
9610 | } | |
9611 | ||
9612 | /* Parse the initializer. */ | |
9613 | if (is_initialized) | |
39703eb9 MM |
9614 | initializer = cp_parser_initializer (parser, |
9615 | &is_parenthesized_init, | |
9616 | &is_non_constant_init); | |
a723baf1 MM |
9617 | else |
9618 | { | |
9619 | initializer = NULL_TREE; | |
9620 | is_parenthesized_init = false; | |
39703eb9 | 9621 | is_non_constant_init = true; |
a723baf1 MM |
9622 | } |
9623 | ||
9624 | /* The old parser allows attributes to appear after a parenthesized | |
9625 | initializer. Mark Mitchell proposed removing this functionality | |
9626 | on the GCC mailing lists on 2002-08-13. This parser accepts the | |
9627 | attributes -- but ignores them. */ | |
9628 | if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init) | |
9629 | if (cp_parser_attributes_opt (parser)) | |
9630 | warning ("attributes after parenthesized initializer ignored"); | |
9631 | ||
9632 | /* Leave the SCOPE, now that we have processed the initializer. It | |
9633 | is important to do this before calling cp_finish_decl because it | |
9634 | makes decisions about whether to create DECL_STMTs or not based | |
9635 | on the current scope. */ | |
9636 | if (scope) | |
9637 | pop_scope (scope); | |
9638 | ||
9639 | /* For an in-class declaration, use `grokfield' to create the | |
9640 | declaration. */ | |
9641 | if (member_p) | |
8db1028e NS |
9642 | { |
9643 | decl = grokfield (declarator, decl_specifiers, | |
9644 | initializer, /*asmspec=*/NULL_TREE, | |
a723baf1 | 9645 | /*attributes=*/NULL_TREE); |
8db1028e NS |
9646 | if (decl && TREE_CODE (decl) == FUNCTION_DECL) |
9647 | cp_parser_save_default_args (parser, decl); | |
9648 | } | |
9649 | ||
a723baf1 MM |
9650 | /* Finish processing the declaration. But, skip friend |
9651 | declarations. */ | |
9652 | if (!friend_p && decl) | |
9653 | cp_finish_decl (decl, | |
9654 | initializer, | |
9655 | asm_specification, | |
9656 | /* If the initializer is in parentheses, then this is | |
9657 | a direct-initialization, which means that an | |
9658 | `explicit' constructor is OK. Otherwise, an | |
9659 | `explicit' constructor cannot be used. */ | |
9660 | ((is_parenthesized_init || !is_initialized) | |
9661 | ? 0 : LOOKUP_ONLYCONVERTING)); | |
9662 | ||
39703eb9 MM |
9663 | /* Remember whether or not variables were initialized by |
9664 | constant-expressions. */ | |
9665 | if (decl && TREE_CODE (decl) == VAR_DECL | |
9666 | && is_initialized && !is_non_constant_init) | |
9667 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; | |
9668 | ||
a723baf1 MM |
9669 | return decl; |
9670 | } | |
9671 | ||
9672 | /* Parse a declarator. | |
9673 | ||
9674 | declarator: | |
9675 | direct-declarator | |
9676 | ptr-operator declarator | |
9677 | ||
9678 | abstract-declarator: | |
9679 | ptr-operator abstract-declarator [opt] | |
9680 | direct-abstract-declarator | |
9681 | ||
9682 | GNU Extensions: | |
9683 | ||
9684 | declarator: | |
9685 | attributes [opt] direct-declarator | |
9686 | attributes [opt] ptr-operator declarator | |
9687 | ||
9688 | abstract-declarator: | |
9689 | attributes [opt] ptr-operator abstract-declarator [opt] | |
9690 | attributes [opt] direct-abstract-declarator | |
9691 | ||
9692 | Returns a representation of the declarator. If the declarator has | |
9693 | the form `* declarator', then an INDIRECT_REF is returned, whose | |
34cd5ae7 | 9694 | only operand is the sub-declarator. Analogously, `& declarator' is |
a723baf1 MM |
9695 | represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is |
9696 | used. The first operand is the TYPE for `X'. The second operand | |
9697 | is an INDIRECT_REF whose operand is the sub-declarator. | |
9698 | ||
34cd5ae7 | 9699 | Otherwise, the representation is as for a direct-declarator. |
a723baf1 MM |
9700 | |
9701 | (It would be better to define a structure type to represent | |
9702 | declarators, rather than abusing `tree' nodes to represent | |
9703 | declarators. That would be much clearer and save some memory. | |
9704 | There is no reason for declarators to be garbage-collected, for | |
9705 | example; they are created during parser and no longer needed after | |
9706 | `grokdeclarator' has been called.) | |
9707 | ||
9708 | For a ptr-operator that has the optional cv-qualifier-seq, | |
9709 | cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF | |
9710 | node. | |
9711 | ||
7efa3e22 NS |
9712 | If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to |
9713 | detect constructor, destructor or conversion operators. It is set | |
9714 | to -1 if the declarator is a name, and +1 if it is a | |
9715 | function. Otherwise it is set to zero. Usually you just want to | |
9716 | test for >0, but internally the negative value is used. | |
9717 | ||
a723baf1 MM |
9718 | (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have |
9719 | a decl-specifier-seq unless it declares a constructor, destructor, | |
9720 | or conversion. It might seem that we could check this condition in | |
9721 | semantic analysis, rather than parsing, but that makes it difficult | |
9722 | to handle something like `f()'. We want to notice that there are | |
9723 | no decl-specifiers, and therefore realize that this is an | |
9724 | expression, not a declaration.) */ | |
9725 | ||
9726 | static tree | |
94edc4ab NN |
9727 | cp_parser_declarator (cp_parser* parser, |
9728 | cp_parser_declarator_kind dcl_kind, | |
7efa3e22 | 9729 | int* ctor_dtor_or_conv_p) |
a723baf1 MM |
9730 | { |
9731 | cp_token *token; | |
9732 | tree declarator; | |
9733 | enum tree_code code; | |
9734 | tree cv_qualifier_seq; | |
9735 | tree class_type; | |
9736 | tree attributes = NULL_TREE; | |
9737 | ||
9738 | /* Assume this is not a constructor, destructor, or type-conversion | |
9739 | operator. */ | |
9740 | if (ctor_dtor_or_conv_p) | |
7efa3e22 | 9741 | *ctor_dtor_or_conv_p = 0; |
a723baf1 MM |
9742 | |
9743 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
9744 | attributes = cp_parser_attributes_opt (parser); | |
9745 | ||
9746 | /* Peek at the next token. */ | |
9747 | token = cp_lexer_peek_token (parser->lexer); | |
9748 | ||
9749 | /* Check for the ptr-operator production. */ | |
9750 | cp_parser_parse_tentatively (parser); | |
9751 | /* Parse the ptr-operator. */ | |
9752 | code = cp_parser_ptr_operator (parser, | |
9753 | &class_type, | |
9754 | &cv_qualifier_seq); | |
9755 | /* If that worked, then we have a ptr-operator. */ | |
9756 | if (cp_parser_parse_definitely (parser)) | |
9757 | { | |
9758 | /* The dependent declarator is optional if we are parsing an | |
9759 | abstract-declarator. */ | |
62b8a44e | 9760 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
a723baf1 MM |
9761 | cp_parser_parse_tentatively (parser); |
9762 | ||
9763 | /* Parse the dependent declarator. */ | |
62b8a44e | 9764 | declarator = cp_parser_declarator (parser, dcl_kind, |
a723baf1 MM |
9765 | /*ctor_dtor_or_conv_p=*/NULL); |
9766 | ||
9767 | /* If we are parsing an abstract-declarator, we must handle the | |
9768 | case where the dependent declarator is absent. */ | |
62b8a44e NS |
9769 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED |
9770 | && !cp_parser_parse_definitely (parser)) | |
a723baf1 MM |
9771 | declarator = NULL_TREE; |
9772 | ||
9773 | /* Build the representation of the ptr-operator. */ | |
9774 | if (code == INDIRECT_REF) | |
9775 | declarator = make_pointer_declarator (cv_qualifier_seq, | |
9776 | declarator); | |
9777 | else | |
9778 | declarator = make_reference_declarator (cv_qualifier_seq, | |
9779 | declarator); | |
9780 | /* Handle the pointer-to-member case. */ | |
9781 | if (class_type) | |
9782 | declarator = build_nt (SCOPE_REF, class_type, declarator); | |
9783 | } | |
9784 | /* Everything else is a direct-declarator. */ | |
9785 | else | |
7efa3e22 | 9786 | declarator = cp_parser_direct_declarator (parser, dcl_kind, |
a723baf1 MM |
9787 | ctor_dtor_or_conv_p); |
9788 | ||
9789 | if (attributes && declarator != error_mark_node) | |
9790 | declarator = tree_cons (attributes, declarator, NULL_TREE); | |
9791 | ||
9792 | return declarator; | |
9793 | } | |
9794 | ||
9795 | /* Parse a direct-declarator or direct-abstract-declarator. | |
9796 | ||
9797 | direct-declarator: | |
9798 | declarator-id | |
9799 | direct-declarator ( parameter-declaration-clause ) | |
9800 | cv-qualifier-seq [opt] | |
9801 | exception-specification [opt] | |
9802 | direct-declarator [ constant-expression [opt] ] | |
9803 | ( declarator ) | |
9804 | ||
9805 | direct-abstract-declarator: | |
9806 | direct-abstract-declarator [opt] | |
9807 | ( parameter-declaration-clause ) | |
9808 | cv-qualifier-seq [opt] | |
9809 | exception-specification [opt] | |
9810 | direct-abstract-declarator [opt] [ constant-expression [opt] ] | |
9811 | ( abstract-declarator ) | |
9812 | ||
62b8a44e NS |
9813 | Returns a representation of the declarator. DCL_KIND is |
9814 | CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a | |
9815 | direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if | |
9816 | we are parsing a direct-declarator. It is | |
9817 | CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case | |
9818 | of ambiguity we prefer an abstract declarator, as per | |
9819 | [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for | |
a723baf1 MM |
9820 | cp_parser_declarator. |
9821 | ||
9822 | For the declarator-id production, the representation is as for an | |
9823 | id-expression, except that a qualified name is represented as a | |
9824 | SCOPE_REF. A function-declarator is represented as a CALL_EXPR; | |
9825 | see the documentation of the FUNCTION_DECLARATOR_* macros for | |
9826 | information about how to find the various declarator components. | |
9827 | An array-declarator is represented as an ARRAY_REF. The | |
9828 | direct-declarator is the first operand; the constant-expression | |
9829 | indicating the size of the array is the second operand. */ | |
9830 | ||
9831 | static tree | |
94edc4ab NN |
9832 | cp_parser_direct_declarator (cp_parser* parser, |
9833 | cp_parser_declarator_kind dcl_kind, | |
7efa3e22 | 9834 | int* ctor_dtor_or_conv_p) |
a723baf1 MM |
9835 | { |
9836 | cp_token *token; | |
62b8a44e | 9837 | tree declarator = NULL_TREE; |
a723baf1 MM |
9838 | tree scope = NULL_TREE; |
9839 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
9840 | bool saved_in_declarator_p = parser->in_declarator_p; | |
62b8a44e NS |
9841 | bool first = true; |
9842 | ||
9843 | while (true) | |
a723baf1 | 9844 | { |
62b8a44e NS |
9845 | /* Peek at the next token. */ |
9846 | token = cp_lexer_peek_token (parser->lexer); | |
9847 | if (token->type == CPP_OPEN_PAREN) | |
a723baf1 | 9848 | { |
62b8a44e NS |
9849 | /* This is either a parameter-declaration-clause, or a |
9850 | parenthesized declarator. When we know we are parsing a | |
34cd5ae7 | 9851 | named declarator, it must be a parenthesized declarator |
62b8a44e NS |
9852 | if FIRST is true. For instance, `(int)' is a |
9853 | parameter-declaration-clause, with an omitted | |
9854 | direct-abstract-declarator. But `((*))', is a | |
9855 | parenthesized abstract declarator. Finally, when T is a | |
9856 | template parameter `(T)' is a | |
34cd5ae7 | 9857 | parameter-declaration-clause, and not a parenthesized |
62b8a44e | 9858 | named declarator. |
a723baf1 | 9859 | |
62b8a44e NS |
9860 | We first try and parse a parameter-declaration-clause, |
9861 | and then try a nested declarator (if FIRST is true). | |
a723baf1 | 9862 | |
62b8a44e NS |
9863 | It is not an error for it not to be a |
9864 | parameter-declaration-clause, even when FIRST is | |
9865 | false. Consider, | |
9866 | ||
9867 | int i (int); | |
9868 | int i (3); | |
9869 | ||
9870 | The first is the declaration of a function while the | |
9871 | second is a the definition of a variable, including its | |
9872 | initializer. | |
9873 | ||
9874 | Having seen only the parenthesis, we cannot know which of | |
9875 | these two alternatives should be selected. Even more | |
9876 | complex are examples like: | |
9877 | ||
9878 | int i (int (a)); | |
9879 | int i (int (3)); | |
9880 | ||
9881 | The former is a function-declaration; the latter is a | |
9882 | variable initialization. | |
9883 | ||
34cd5ae7 | 9884 | Thus again, we try a parameter-declaration-clause, and if |
62b8a44e NS |
9885 | that fails, we back out and return. */ |
9886 | ||
9887 | if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) | |
a723baf1 | 9888 | { |
62b8a44e | 9889 | tree params; |
4047b164 | 9890 | unsigned saved_num_template_parameter_lists; |
62b8a44e NS |
9891 | |
9892 | cp_parser_parse_tentatively (parser); | |
a723baf1 | 9893 | |
62b8a44e NS |
9894 | /* Consume the `('. */ |
9895 | cp_lexer_consume_token (parser->lexer); | |
9896 | if (first) | |
9897 | { | |
9898 | /* If this is going to be an abstract declarator, we're | |
9899 | in a declarator and we can't have default args. */ | |
9900 | parser->default_arg_ok_p = false; | |
9901 | parser->in_declarator_p = true; | |
9902 | } | |
9903 | ||
4047b164 KL |
9904 | /* Inside the function parameter list, surrounding |
9905 | template-parameter-lists do not apply. */ | |
9906 | saved_num_template_parameter_lists | |
9907 | = parser->num_template_parameter_lists; | |
9908 | parser->num_template_parameter_lists = 0; | |
9909 | ||
62b8a44e NS |
9910 | /* Parse the parameter-declaration-clause. */ |
9911 | params = cp_parser_parameter_declaration_clause (parser); | |
9912 | ||
4047b164 KL |
9913 | parser->num_template_parameter_lists |
9914 | = saved_num_template_parameter_lists; | |
9915 | ||
62b8a44e | 9916 | /* If all went well, parse the cv-qualifier-seq and the |
34cd5ae7 | 9917 | exception-specification. */ |
62b8a44e NS |
9918 | if (cp_parser_parse_definitely (parser)) |
9919 | { | |
9920 | tree cv_qualifiers; | |
9921 | tree exception_specification; | |
7efa3e22 NS |
9922 | |
9923 | if (ctor_dtor_or_conv_p) | |
9924 | *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0; | |
62b8a44e NS |
9925 | first = false; |
9926 | /* Consume the `)'. */ | |
9927 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
9928 | ||
9929 | /* Parse the cv-qualifier-seq. */ | |
9930 | cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser); | |
9931 | /* And the exception-specification. */ | |
9932 | exception_specification | |
9933 | = cp_parser_exception_specification_opt (parser); | |
9934 | ||
9935 | /* Create the function-declarator. */ | |
9936 | declarator = make_call_declarator (declarator, | |
9937 | params, | |
9938 | cv_qualifiers, | |
9939 | exception_specification); | |
9940 | /* Any subsequent parameter lists are to do with | |
9941 | return type, so are not those of the declared | |
9942 | function. */ | |
9943 | parser->default_arg_ok_p = false; | |
9944 | ||
9945 | /* Repeat the main loop. */ | |
9946 | continue; | |
9947 | } | |
9948 | } | |
9949 | ||
9950 | /* If this is the first, we can try a parenthesized | |
9951 | declarator. */ | |
9952 | if (first) | |
a723baf1 | 9953 | { |
a723baf1 | 9954 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
62b8a44e NS |
9955 | parser->in_declarator_p = saved_in_declarator_p; |
9956 | ||
9957 | /* Consume the `('. */ | |
9958 | cp_lexer_consume_token (parser->lexer); | |
9959 | /* Parse the nested declarator. */ | |
9960 | declarator | |
9961 | = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p); | |
9962 | first = false; | |
9963 | /* Expect a `)'. */ | |
9964 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
9965 | declarator = error_mark_node; | |
9966 | if (declarator == error_mark_node) | |
9967 | break; | |
9968 | ||
9969 | goto handle_declarator; | |
a723baf1 | 9970 | } |
9bcb9aae | 9971 | /* Otherwise, we must be done. */ |
62b8a44e NS |
9972 | else |
9973 | break; | |
a723baf1 | 9974 | } |
62b8a44e NS |
9975 | else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
9976 | && token->type == CPP_OPEN_SQUARE) | |
a723baf1 | 9977 | { |
62b8a44e | 9978 | /* Parse an array-declarator. */ |
a723baf1 MM |
9979 | tree bounds; |
9980 | ||
7efa3e22 NS |
9981 | if (ctor_dtor_or_conv_p) |
9982 | *ctor_dtor_or_conv_p = 0; | |
9983 | ||
62b8a44e NS |
9984 | first = false; |
9985 | parser->default_arg_ok_p = false; | |
9986 | parser->in_declarator_p = true; | |
a723baf1 MM |
9987 | /* Consume the `['. */ |
9988 | cp_lexer_consume_token (parser->lexer); | |
9989 | /* Peek at the next token. */ | |
9990 | token = cp_lexer_peek_token (parser->lexer); | |
9991 | /* If the next token is `]', then there is no | |
9992 | constant-expression. */ | |
9993 | if (token->type != CPP_CLOSE_SQUARE) | |
14d22dd6 MM |
9994 | { |
9995 | bool non_constant_p; | |
9996 | ||
9997 | bounds | |
9998 | = cp_parser_constant_expression (parser, | |
9999 | /*allow_non_constant=*/true, | |
10000 | &non_constant_p); | |
d17811fd MM |
10001 | if (!non_constant_p) |
10002 | bounds = cp_parser_fold_non_dependent_expr (bounds); | |
14d22dd6 | 10003 | } |
a723baf1 MM |
10004 | else |
10005 | bounds = NULL_TREE; | |
10006 | /* Look for the closing `]'. */ | |
62b8a44e NS |
10007 | if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'")) |
10008 | { | |
10009 | declarator = error_mark_node; | |
10010 | break; | |
10011 | } | |
a723baf1 MM |
10012 | |
10013 | declarator = build_nt (ARRAY_REF, declarator, bounds); | |
10014 | } | |
62b8a44e | 10015 | else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT) |
a723baf1 | 10016 | { |
a668c6ad | 10017 | /* Parse a declarator-id */ |
62b8a44e NS |
10018 | if (dcl_kind == CP_PARSER_DECLARATOR_EITHER) |
10019 | cp_parser_parse_tentatively (parser); | |
10020 | declarator = cp_parser_declarator_id (parser); | |
712becab NS |
10021 | if (dcl_kind == CP_PARSER_DECLARATOR_EITHER) |
10022 | { | |
10023 | if (!cp_parser_parse_definitely (parser)) | |
10024 | declarator = error_mark_node; | |
10025 | else if (TREE_CODE (declarator) != IDENTIFIER_NODE) | |
10026 | { | |
10027 | cp_parser_error (parser, "expected unqualified-id"); | |
10028 | declarator = error_mark_node; | |
10029 | } | |
10030 | } | |
10031 | ||
62b8a44e NS |
10032 | if (declarator == error_mark_node) |
10033 | break; | |
a723baf1 | 10034 | |
d9a50301 KL |
10035 | if (TREE_CODE (declarator) == SCOPE_REF |
10036 | && !current_scope ()) | |
62b8a44e NS |
10037 | { |
10038 | tree scope = TREE_OPERAND (declarator, 0); | |
712becab | 10039 | |
62b8a44e NS |
10040 | /* In the declaration of a member of a template class |
10041 | outside of the class itself, the SCOPE will sometimes | |
10042 | be a TYPENAME_TYPE. For example, given: | |
10043 | ||
10044 | template <typename T> | |
10045 | int S<T>::R::i = 3; | |
10046 | ||
10047 | the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In | |
10048 | this context, we must resolve S<T>::R to an ordinary | |
10049 | type, rather than a typename type. | |
10050 | ||
10051 | The reason we normally avoid resolving TYPENAME_TYPEs | |
10052 | is that a specialization of `S' might render | |
10053 | `S<T>::R' not a type. However, if `S' is | |
10054 | specialized, then this `i' will not be used, so there | |
10055 | is no harm in resolving the types here. */ | |
10056 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
10057 | { | |
14d22dd6 MM |
10058 | tree type; |
10059 | ||
62b8a44e | 10060 | /* Resolve the TYPENAME_TYPE. */ |
14d22dd6 MM |
10061 | type = resolve_typename_type (scope, |
10062 | /*only_current_p=*/false); | |
62b8a44e | 10063 | /* If that failed, the declarator is invalid. */ |
14d22dd6 MM |
10064 | if (type != error_mark_node) |
10065 | scope = type; | |
62b8a44e NS |
10066 | /* Build a new DECLARATOR. */ |
10067 | declarator = build_nt (SCOPE_REF, | |
10068 | scope, | |
10069 | TREE_OPERAND (declarator, 1)); | |
10070 | } | |
10071 | } | |
10072 | ||
10073 | /* Check to see whether the declarator-id names a constructor, | |
10074 | destructor, or conversion. */ | |
10075 | if (declarator && ctor_dtor_or_conv_p | |
10076 | && ((TREE_CODE (declarator) == SCOPE_REF | |
10077 | && CLASS_TYPE_P (TREE_OPERAND (declarator, 0))) | |
10078 | || (TREE_CODE (declarator) != SCOPE_REF | |
10079 | && at_class_scope_p ()))) | |
a723baf1 | 10080 | { |
62b8a44e NS |
10081 | tree unqualified_name; |
10082 | tree class_type; | |
10083 | ||
10084 | /* Get the unqualified part of the name. */ | |
10085 | if (TREE_CODE (declarator) == SCOPE_REF) | |
10086 | { | |
10087 | class_type = TREE_OPERAND (declarator, 0); | |
10088 | unqualified_name = TREE_OPERAND (declarator, 1); | |
10089 | } | |
10090 | else | |
10091 | { | |
10092 | class_type = current_class_type; | |
10093 | unqualified_name = declarator; | |
10094 | } | |
10095 | ||
10096 | /* See if it names ctor, dtor or conv. */ | |
10097 | if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR | |
10098 | || IDENTIFIER_TYPENAME_P (unqualified_name) | |
10099 | || constructor_name_p (unqualified_name, class_type)) | |
7efa3e22 | 10100 | *ctor_dtor_or_conv_p = -1; |
a723baf1 | 10101 | } |
62b8a44e NS |
10102 | |
10103 | handle_declarator:; | |
10104 | scope = get_scope_of_declarator (declarator); | |
10105 | if (scope) | |
10106 | /* Any names that appear after the declarator-id for a member | |
10107 | are looked up in the containing scope. */ | |
10108 | push_scope (scope); | |
10109 | parser->in_declarator_p = true; | |
10110 | if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p) | |
10111 | || (declarator | |
10112 | && (TREE_CODE (declarator) == SCOPE_REF | |
10113 | || TREE_CODE (declarator) == IDENTIFIER_NODE))) | |
10114 | /* Default args are only allowed on function | |
10115 | declarations. */ | |
10116 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
a723baf1 | 10117 | else |
62b8a44e NS |
10118 | parser->default_arg_ok_p = false; |
10119 | ||
10120 | first = false; | |
a723baf1 | 10121 | } |
62b8a44e | 10122 | /* We're done. */ |
a723baf1 MM |
10123 | else |
10124 | break; | |
a723baf1 MM |
10125 | } |
10126 | ||
10127 | /* For an abstract declarator, we might wind up with nothing at this | |
10128 | point. That's an error; the declarator is not optional. */ | |
10129 | if (!declarator) | |
10130 | cp_parser_error (parser, "expected declarator"); | |
10131 | ||
10132 | /* If we entered a scope, we must exit it now. */ | |
10133 | if (scope) | |
10134 | pop_scope (scope); | |
10135 | ||
10136 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
10137 | parser->in_declarator_p = saved_in_declarator_p; | |
10138 | ||
10139 | return declarator; | |
10140 | } | |
10141 | ||
10142 | /* Parse a ptr-operator. | |
10143 | ||
10144 | ptr-operator: | |
10145 | * cv-qualifier-seq [opt] | |
10146 | & | |
10147 | :: [opt] nested-name-specifier * cv-qualifier-seq [opt] | |
10148 | ||
10149 | GNU Extension: | |
10150 | ||
10151 | ptr-operator: | |
10152 | & cv-qualifier-seq [opt] | |
10153 | ||
10154 | Returns INDIRECT_REF if a pointer, or pointer-to-member, was | |
10155 | used. Returns ADDR_EXPR if a reference was used. In the | |
10156 | case of a pointer-to-member, *TYPE is filled in with the | |
10157 | TYPE containing the member. *CV_QUALIFIER_SEQ is filled in | |
10158 | with the cv-qualifier-seq, or NULL_TREE, if there are no | |
10159 | cv-qualifiers. Returns ERROR_MARK if an error occurred. */ | |
10160 | ||
10161 | static enum tree_code | |
94edc4ab NN |
10162 | cp_parser_ptr_operator (cp_parser* parser, |
10163 | tree* type, | |
10164 | tree* cv_qualifier_seq) | |
a723baf1 MM |
10165 | { |
10166 | enum tree_code code = ERROR_MARK; | |
10167 | cp_token *token; | |
10168 | ||
10169 | /* Assume that it's not a pointer-to-member. */ | |
10170 | *type = NULL_TREE; | |
10171 | /* And that there are no cv-qualifiers. */ | |
10172 | *cv_qualifier_seq = NULL_TREE; | |
10173 | ||
10174 | /* Peek at the next token. */ | |
10175 | token = cp_lexer_peek_token (parser->lexer); | |
10176 | /* If it's a `*' or `&' we have a pointer or reference. */ | |
10177 | if (token->type == CPP_MULT || token->type == CPP_AND) | |
10178 | { | |
10179 | /* Remember which ptr-operator we were processing. */ | |
10180 | code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF); | |
10181 | ||
10182 | /* Consume the `*' or `&'. */ | |
10183 | cp_lexer_consume_token (parser->lexer); | |
10184 | ||
10185 | /* A `*' can be followed by a cv-qualifier-seq, and so can a | |
10186 | `&', if we are allowing GNU extensions. (The only qualifier | |
10187 | that can legally appear after `&' is `restrict', but that is | |
10188 | enforced during semantic analysis. */ | |
10189 | if (code == INDIRECT_REF | |
10190 | || cp_parser_allow_gnu_extensions_p (parser)) | |
10191 | *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser); | |
10192 | } | |
10193 | else | |
10194 | { | |
10195 | /* Try the pointer-to-member case. */ | |
10196 | cp_parser_parse_tentatively (parser); | |
10197 | /* Look for the optional `::' operator. */ | |
10198 | cp_parser_global_scope_opt (parser, | |
10199 | /*current_scope_valid_p=*/false); | |
10200 | /* Look for the nested-name specifier. */ | |
10201 | cp_parser_nested_name_specifier (parser, | |
10202 | /*typename_keyword_p=*/false, | |
10203 | /*check_dependency_p=*/true, | |
a668c6ad MM |
10204 | /*type_p=*/false, |
10205 | /*is_declaration=*/false); | |
a723baf1 MM |
10206 | /* If we found it, and the next token is a `*', then we are |
10207 | indeed looking at a pointer-to-member operator. */ | |
10208 | if (!cp_parser_error_occurred (parser) | |
10209 | && cp_parser_require (parser, CPP_MULT, "`*'")) | |
10210 | { | |
10211 | /* The type of which the member is a member is given by the | |
10212 | current SCOPE. */ | |
10213 | *type = parser->scope; | |
10214 | /* The next name will not be qualified. */ | |
10215 | parser->scope = NULL_TREE; | |
10216 | parser->qualifying_scope = NULL_TREE; | |
10217 | parser->object_scope = NULL_TREE; | |
10218 | /* Indicate that the `*' operator was used. */ | |
10219 | code = INDIRECT_REF; | |
10220 | /* Look for the optional cv-qualifier-seq. */ | |
10221 | *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser); | |
10222 | } | |
10223 | /* If that didn't work we don't have a ptr-operator. */ | |
10224 | if (!cp_parser_parse_definitely (parser)) | |
10225 | cp_parser_error (parser, "expected ptr-operator"); | |
10226 | } | |
10227 | ||
10228 | return code; | |
10229 | } | |
10230 | ||
10231 | /* Parse an (optional) cv-qualifier-seq. | |
10232 | ||
10233 | cv-qualifier-seq: | |
10234 | cv-qualifier cv-qualifier-seq [opt] | |
10235 | ||
10236 | Returns a TREE_LIST. The TREE_VALUE of each node is the | |
10237 | representation of a cv-qualifier. */ | |
10238 | ||
10239 | static tree | |
94edc4ab | 10240 | cp_parser_cv_qualifier_seq_opt (cp_parser* parser) |
a723baf1 MM |
10241 | { |
10242 | tree cv_qualifiers = NULL_TREE; | |
10243 | ||
10244 | while (true) | |
10245 | { | |
10246 | tree cv_qualifier; | |
10247 | ||
10248 | /* Look for the next cv-qualifier. */ | |
10249 | cv_qualifier = cp_parser_cv_qualifier_opt (parser); | |
10250 | /* If we didn't find one, we're done. */ | |
10251 | if (!cv_qualifier) | |
10252 | break; | |
10253 | ||
10254 | /* Add this cv-qualifier to the list. */ | |
10255 | cv_qualifiers | |
10256 | = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers); | |
10257 | } | |
10258 | ||
10259 | /* We built up the list in reverse order. */ | |
10260 | return nreverse (cv_qualifiers); | |
10261 | } | |
10262 | ||
10263 | /* Parse an (optional) cv-qualifier. | |
10264 | ||
10265 | cv-qualifier: | |
10266 | const | |
10267 | volatile | |
10268 | ||
10269 | GNU Extension: | |
10270 | ||
10271 | cv-qualifier: | |
10272 | __restrict__ */ | |
10273 | ||
10274 | static tree | |
94edc4ab | 10275 | cp_parser_cv_qualifier_opt (cp_parser* parser) |
a723baf1 MM |
10276 | { |
10277 | cp_token *token; | |
10278 | tree cv_qualifier = NULL_TREE; | |
10279 | ||
10280 | /* Peek at the next token. */ | |
10281 | token = cp_lexer_peek_token (parser->lexer); | |
10282 | /* See if it's a cv-qualifier. */ | |
10283 | switch (token->keyword) | |
10284 | { | |
10285 | case RID_CONST: | |
10286 | case RID_VOLATILE: | |
10287 | case RID_RESTRICT: | |
10288 | /* Save the value of the token. */ | |
10289 | cv_qualifier = token->value; | |
10290 | /* Consume the token. */ | |
10291 | cp_lexer_consume_token (parser->lexer); | |
10292 | break; | |
10293 | ||
10294 | default: | |
10295 | break; | |
10296 | } | |
10297 | ||
10298 | return cv_qualifier; | |
10299 | } | |
10300 | ||
10301 | /* Parse a declarator-id. | |
10302 | ||
10303 | declarator-id: | |
10304 | id-expression | |
10305 | :: [opt] nested-name-specifier [opt] type-name | |
10306 | ||
10307 | In the `id-expression' case, the value returned is as for | |
10308 | cp_parser_id_expression if the id-expression was an unqualified-id. | |
10309 | If the id-expression was a qualified-id, then a SCOPE_REF is | |
10310 | returned. The first operand is the scope (either a NAMESPACE_DECL | |
10311 | or TREE_TYPE), but the second is still just a representation of an | |
10312 | unqualified-id. */ | |
10313 | ||
10314 | static tree | |
94edc4ab | 10315 | cp_parser_declarator_id (cp_parser* parser) |
a723baf1 MM |
10316 | { |
10317 | tree id_expression; | |
10318 | ||
10319 | /* The expression must be an id-expression. Assume that qualified | |
10320 | names are the names of types so that: | |
10321 | ||
10322 | template <class T> | |
10323 | int S<T>::R::i = 3; | |
10324 | ||
10325 | will work; we must treat `S<T>::R' as the name of a type. | |
10326 | Similarly, assume that qualified names are templates, where | |
10327 | required, so that: | |
10328 | ||
10329 | template <class T> | |
10330 | int S<T>::R<T>::i = 3; | |
10331 | ||
10332 | will work, too. */ | |
10333 | id_expression = cp_parser_id_expression (parser, | |
10334 | /*template_keyword_p=*/false, | |
10335 | /*check_dependency_p=*/false, | |
f3c2dfc6 MM |
10336 | /*template_p=*/NULL, |
10337 | /*declarator_p=*/true); | |
a723baf1 MM |
10338 | /* If the name was qualified, create a SCOPE_REF to represent |
10339 | that. */ | |
10340 | if (parser->scope) | |
ec20aa6c MM |
10341 | { |
10342 | id_expression = build_nt (SCOPE_REF, parser->scope, id_expression); | |
10343 | parser->scope = NULL_TREE; | |
10344 | } | |
a723baf1 MM |
10345 | |
10346 | return id_expression; | |
10347 | } | |
10348 | ||
10349 | /* Parse a type-id. | |
10350 | ||
10351 | type-id: | |
10352 | type-specifier-seq abstract-declarator [opt] | |
10353 | ||
10354 | Returns the TYPE specified. */ | |
10355 | ||
10356 | static tree | |
94edc4ab | 10357 | cp_parser_type_id (cp_parser* parser) |
a723baf1 MM |
10358 | { |
10359 | tree type_specifier_seq; | |
10360 | tree abstract_declarator; | |
10361 | ||
10362 | /* Parse the type-specifier-seq. */ | |
10363 | type_specifier_seq | |
10364 | = cp_parser_type_specifier_seq (parser); | |
10365 | if (type_specifier_seq == error_mark_node) | |
10366 | return error_mark_node; | |
10367 | ||
10368 | /* There might or might not be an abstract declarator. */ | |
10369 | cp_parser_parse_tentatively (parser); | |
10370 | /* Look for the declarator. */ | |
10371 | abstract_declarator | |
62b8a44e | 10372 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL); |
a723baf1 MM |
10373 | /* Check to see if there really was a declarator. */ |
10374 | if (!cp_parser_parse_definitely (parser)) | |
10375 | abstract_declarator = NULL_TREE; | |
10376 | ||
10377 | return groktypename (build_tree_list (type_specifier_seq, | |
10378 | abstract_declarator)); | |
10379 | } | |
10380 | ||
10381 | /* Parse a type-specifier-seq. | |
10382 | ||
10383 | type-specifier-seq: | |
10384 | type-specifier type-specifier-seq [opt] | |
10385 | ||
10386 | GNU extension: | |
10387 | ||
10388 | type-specifier-seq: | |
10389 | attributes type-specifier-seq [opt] | |
10390 | ||
10391 | Returns a TREE_LIST. Either the TREE_VALUE of each node is a | |
10392 | type-specifier, or the TREE_PURPOSE is a list of attributes. */ | |
10393 | ||
10394 | static tree | |
94edc4ab | 10395 | cp_parser_type_specifier_seq (cp_parser* parser) |
a723baf1 MM |
10396 | { |
10397 | bool seen_type_specifier = false; | |
10398 | tree type_specifier_seq = NULL_TREE; | |
10399 | ||
10400 | /* Parse the type-specifiers and attributes. */ | |
10401 | while (true) | |
10402 | { | |
10403 | tree type_specifier; | |
10404 | ||
10405 | /* Check for attributes first. */ | |
10406 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) | |
10407 | { | |
10408 | type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser), | |
10409 | NULL_TREE, | |
10410 | type_specifier_seq); | |
10411 | continue; | |
10412 | } | |
10413 | ||
10414 | /* After the first type-specifier, others are optional. */ | |
10415 | if (seen_type_specifier) | |
10416 | cp_parser_parse_tentatively (parser); | |
10417 | /* Look for the type-specifier. */ | |
10418 | type_specifier = cp_parser_type_specifier (parser, | |
10419 | CP_PARSER_FLAGS_NONE, | |
10420 | /*is_friend=*/false, | |
10421 | /*is_declaration=*/false, | |
10422 | NULL, | |
10423 | NULL); | |
10424 | /* If the first type-specifier could not be found, this is not a | |
10425 | type-specifier-seq at all. */ | |
10426 | if (!seen_type_specifier && type_specifier == error_mark_node) | |
10427 | return error_mark_node; | |
10428 | /* If subsequent type-specifiers could not be found, the | |
10429 | type-specifier-seq is complete. */ | |
10430 | else if (seen_type_specifier && !cp_parser_parse_definitely (parser)) | |
10431 | break; | |
10432 | ||
10433 | /* Add the new type-specifier to the list. */ | |
10434 | type_specifier_seq | |
10435 | = tree_cons (NULL_TREE, type_specifier, type_specifier_seq); | |
10436 | seen_type_specifier = true; | |
10437 | } | |
10438 | ||
10439 | /* We built up the list in reverse order. */ | |
10440 | return nreverse (type_specifier_seq); | |
10441 | } | |
10442 | ||
10443 | /* Parse a parameter-declaration-clause. | |
10444 | ||
10445 | parameter-declaration-clause: | |
10446 | parameter-declaration-list [opt] ... [opt] | |
10447 | parameter-declaration-list , ... | |
10448 | ||
10449 | Returns a representation for the parameter declarations. Each node | |
10450 | is a TREE_LIST. (See cp_parser_parameter_declaration for the exact | |
10451 | representation.) If the parameter-declaration-clause ends with an | |
10452 | ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the | |
10453 | list. A return value of NULL_TREE indicates a | |
10454 | parameter-declaration-clause consisting only of an ellipsis. */ | |
10455 | ||
10456 | static tree | |
94edc4ab | 10457 | cp_parser_parameter_declaration_clause (cp_parser* parser) |
a723baf1 MM |
10458 | { |
10459 | tree parameters; | |
10460 | cp_token *token; | |
10461 | bool ellipsis_p; | |
10462 | ||
10463 | /* Peek at the next token. */ | |
10464 | token = cp_lexer_peek_token (parser->lexer); | |
10465 | /* Check for trivial parameter-declaration-clauses. */ | |
10466 | if (token->type == CPP_ELLIPSIS) | |
10467 | { | |
10468 | /* Consume the `...' token. */ | |
10469 | cp_lexer_consume_token (parser->lexer); | |
10470 | return NULL_TREE; | |
10471 | } | |
10472 | else if (token->type == CPP_CLOSE_PAREN) | |
10473 | /* There are no parameters. */ | |
c73aecdf DE |
10474 | { |
10475 | #ifndef NO_IMPLICIT_EXTERN_C | |
10476 | if (in_system_header && current_class_type == NULL | |
10477 | && current_lang_name == lang_name_c) | |
10478 | return NULL_TREE; | |
10479 | else | |
10480 | #endif | |
10481 | return void_list_node; | |
10482 | } | |
a723baf1 MM |
10483 | /* Check for `(void)', too, which is a special case. */ |
10484 | else if (token->keyword == RID_VOID | |
10485 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
10486 | == CPP_CLOSE_PAREN)) | |
10487 | { | |
10488 | /* Consume the `void' token. */ | |
10489 | cp_lexer_consume_token (parser->lexer); | |
10490 | /* There are no parameters. */ | |
10491 | return void_list_node; | |
10492 | } | |
10493 | ||
10494 | /* Parse the parameter-declaration-list. */ | |
10495 | parameters = cp_parser_parameter_declaration_list (parser); | |
10496 | /* If a parse error occurred while parsing the | |
10497 | parameter-declaration-list, then the entire | |
10498 | parameter-declaration-clause is erroneous. */ | |
10499 | if (parameters == error_mark_node) | |
10500 | return error_mark_node; | |
10501 | ||
10502 | /* Peek at the next token. */ | |
10503 | token = cp_lexer_peek_token (parser->lexer); | |
10504 | /* If it's a `,', the clause should terminate with an ellipsis. */ | |
10505 | if (token->type == CPP_COMMA) | |
10506 | { | |
10507 | /* Consume the `,'. */ | |
10508 | cp_lexer_consume_token (parser->lexer); | |
10509 | /* Expect an ellipsis. */ | |
10510 | ellipsis_p | |
10511 | = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL); | |
10512 | } | |
10513 | /* It might also be `...' if the optional trailing `,' was | |
10514 | omitted. */ | |
10515 | else if (token->type == CPP_ELLIPSIS) | |
10516 | { | |
10517 | /* Consume the `...' token. */ | |
10518 | cp_lexer_consume_token (parser->lexer); | |
10519 | /* And remember that we saw it. */ | |
10520 | ellipsis_p = true; | |
10521 | } | |
10522 | else | |
10523 | ellipsis_p = false; | |
10524 | ||
10525 | /* Finish the parameter list. */ | |
10526 | return finish_parmlist (parameters, ellipsis_p); | |
10527 | } | |
10528 | ||
10529 | /* Parse a parameter-declaration-list. | |
10530 | ||
10531 | parameter-declaration-list: | |
10532 | parameter-declaration | |
10533 | parameter-declaration-list , parameter-declaration | |
10534 | ||
10535 | Returns a representation of the parameter-declaration-list, as for | |
10536 | cp_parser_parameter_declaration_clause. However, the | |
10537 | `void_list_node' is never appended to the list. */ | |
10538 | ||
10539 | static tree | |
94edc4ab | 10540 | cp_parser_parameter_declaration_list (cp_parser* parser) |
a723baf1 MM |
10541 | { |
10542 | tree parameters = NULL_TREE; | |
10543 | ||
10544 | /* Look for more parameters. */ | |
10545 | while (true) | |
10546 | { | |
10547 | tree parameter; | |
10548 | /* Parse the parameter. */ | |
10549 | parameter | |
ec194454 MM |
10550 | = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false); |
10551 | ||
34cd5ae7 | 10552 | /* If a parse error occurred parsing the parameter declaration, |
a723baf1 MM |
10553 | then the entire parameter-declaration-list is erroneous. */ |
10554 | if (parameter == error_mark_node) | |
10555 | { | |
10556 | parameters = error_mark_node; | |
10557 | break; | |
10558 | } | |
10559 | /* Add the new parameter to the list. */ | |
10560 | TREE_CHAIN (parameter) = parameters; | |
10561 | parameters = parameter; | |
10562 | ||
10563 | /* Peek at the next token. */ | |
10564 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN) | |
10565 | || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
10566 | /* The parameter-declaration-list is complete. */ | |
10567 | break; | |
10568 | else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
10569 | { | |
10570 | cp_token *token; | |
10571 | ||
10572 | /* Peek at the next token. */ | |
10573 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
10574 | /* If it's an ellipsis, then the list is complete. */ | |
10575 | if (token->type == CPP_ELLIPSIS) | |
10576 | break; | |
10577 | /* Otherwise, there must be more parameters. Consume the | |
10578 | `,'. */ | |
10579 | cp_lexer_consume_token (parser->lexer); | |
10580 | } | |
10581 | else | |
10582 | { | |
10583 | cp_parser_error (parser, "expected `,' or `...'"); | |
10584 | break; | |
10585 | } | |
10586 | } | |
10587 | ||
10588 | /* We built up the list in reverse order; straighten it out now. */ | |
10589 | return nreverse (parameters); | |
10590 | } | |
10591 | ||
10592 | /* Parse a parameter declaration. | |
10593 | ||
10594 | parameter-declaration: | |
10595 | decl-specifier-seq declarator | |
10596 | decl-specifier-seq declarator = assignment-expression | |
10597 | decl-specifier-seq abstract-declarator [opt] | |
10598 | decl-specifier-seq abstract-declarator [opt] = assignment-expression | |
10599 | ||
ec194454 MM |
10600 | If TEMPLATE_PARM_P is TRUE, then this parameter-declaration |
10601 | declares a template parameter. (In that case, a non-nested `>' | |
10602 | token encountered during the parsing of the assignment-expression | |
10603 | is not interpreted as a greater-than operator.) | |
a723baf1 MM |
10604 | |
10605 | Returns a TREE_LIST representing the parameter-declaration. The | |
10606 | TREE_VALUE is a representation of the decl-specifier-seq and | |
10607 | declarator. In particular, the TREE_VALUE will be a TREE_LIST | |
10608 | whose TREE_PURPOSE represents the decl-specifier-seq and whose | |
10609 | TREE_VALUE represents the declarator. */ | |
10610 | ||
10611 | static tree | |
ec194454 MM |
10612 | cp_parser_parameter_declaration (cp_parser *parser, |
10613 | bool template_parm_p) | |
a723baf1 | 10614 | { |
560ad596 | 10615 | int declares_class_or_enum; |
ec194454 | 10616 | bool greater_than_is_operator_p; |
a723baf1 MM |
10617 | tree decl_specifiers; |
10618 | tree attributes; | |
10619 | tree declarator; | |
10620 | tree default_argument; | |
10621 | tree parameter; | |
10622 | cp_token *token; | |
10623 | const char *saved_message; | |
10624 | ||
ec194454 MM |
10625 | /* In a template parameter, `>' is not an operator. |
10626 | ||
10627 | [temp.param] | |
10628 | ||
10629 | When parsing a default template-argument for a non-type | |
10630 | template-parameter, the first non-nested `>' is taken as the end | |
10631 | of the template parameter-list rather than a greater-than | |
10632 | operator. */ | |
10633 | greater_than_is_operator_p = !template_parm_p; | |
10634 | ||
a723baf1 MM |
10635 | /* Type definitions may not appear in parameter types. */ |
10636 | saved_message = parser->type_definition_forbidden_message; | |
10637 | parser->type_definition_forbidden_message | |
10638 | = "types may not be defined in parameter types"; | |
10639 | ||
10640 | /* Parse the declaration-specifiers. */ | |
10641 | decl_specifiers | |
10642 | = cp_parser_decl_specifier_seq (parser, | |
10643 | CP_PARSER_FLAGS_NONE, | |
10644 | &attributes, | |
10645 | &declares_class_or_enum); | |
10646 | /* If an error occurred, there's no reason to attempt to parse the | |
10647 | rest of the declaration. */ | |
10648 | if (cp_parser_error_occurred (parser)) | |
10649 | { | |
10650 | parser->type_definition_forbidden_message = saved_message; | |
10651 | return error_mark_node; | |
10652 | } | |
10653 | ||
10654 | /* Peek at the next token. */ | |
10655 | token = cp_lexer_peek_token (parser->lexer); | |
10656 | /* If the next token is a `)', `,', `=', `>', or `...', then there | |
10657 | is no declarator. */ | |
10658 | if (token->type == CPP_CLOSE_PAREN | |
10659 | || token->type == CPP_COMMA | |
10660 | || token->type == CPP_EQ | |
10661 | || token->type == CPP_ELLIPSIS | |
10662 | || token->type == CPP_GREATER) | |
10663 | declarator = NULL_TREE; | |
10664 | /* Otherwise, there should be a declarator. */ | |
10665 | else | |
10666 | { | |
10667 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
10668 | parser->default_arg_ok_p = false; | |
10669 | ||
a723baf1 | 10670 | declarator = cp_parser_declarator (parser, |
62b8a44e | 10671 | CP_PARSER_DECLARATOR_EITHER, |
a723baf1 | 10672 | /*ctor_dtor_or_conv_p=*/NULL); |
a723baf1 | 10673 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
4971227d MM |
10674 | /* After the declarator, allow more attributes. */ |
10675 | attributes = chainon (attributes, cp_parser_attributes_opt (parser)); | |
a723baf1 MM |
10676 | } |
10677 | ||
62b8a44e | 10678 | /* The restriction on defining new types applies only to the type |
a723baf1 MM |
10679 | of the parameter, not to the default argument. */ |
10680 | parser->type_definition_forbidden_message = saved_message; | |
10681 | ||
10682 | /* If the next token is `=', then process a default argument. */ | |
10683 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
10684 | { | |
10685 | bool saved_greater_than_is_operator_p; | |
10686 | /* Consume the `='. */ | |
10687 | cp_lexer_consume_token (parser->lexer); | |
10688 | ||
10689 | /* If we are defining a class, then the tokens that make up the | |
10690 | default argument must be saved and processed later. */ | |
ec194454 MM |
10691 | if (!template_parm_p && at_class_scope_p () |
10692 | && TYPE_BEING_DEFINED (current_class_type)) | |
a723baf1 MM |
10693 | { |
10694 | unsigned depth = 0; | |
10695 | ||
10696 | /* Create a DEFAULT_ARG to represented the unparsed default | |
10697 | argument. */ | |
10698 | default_argument = make_node (DEFAULT_ARG); | |
10699 | DEFARG_TOKENS (default_argument) = cp_token_cache_new (); | |
10700 | ||
10701 | /* Add tokens until we have processed the entire default | |
10702 | argument. */ | |
10703 | while (true) | |
10704 | { | |
10705 | bool done = false; | |
10706 | cp_token *token; | |
10707 | ||
10708 | /* Peek at the next token. */ | |
10709 | token = cp_lexer_peek_token (parser->lexer); | |
10710 | /* What we do depends on what token we have. */ | |
10711 | switch (token->type) | |
10712 | { | |
10713 | /* In valid code, a default argument must be | |
10714 | immediately followed by a `,' `)', or `...'. */ | |
10715 | case CPP_COMMA: | |
10716 | case CPP_CLOSE_PAREN: | |
10717 | case CPP_ELLIPSIS: | |
10718 | /* If we run into a non-nested `;', `}', or `]', | |
10719 | then the code is invalid -- but the default | |
10720 | argument is certainly over. */ | |
10721 | case CPP_SEMICOLON: | |
10722 | case CPP_CLOSE_BRACE: | |
10723 | case CPP_CLOSE_SQUARE: | |
10724 | if (depth == 0) | |
10725 | done = true; | |
10726 | /* Update DEPTH, if necessary. */ | |
10727 | else if (token->type == CPP_CLOSE_PAREN | |
10728 | || token->type == CPP_CLOSE_BRACE | |
10729 | || token->type == CPP_CLOSE_SQUARE) | |
10730 | --depth; | |
10731 | break; | |
10732 | ||
10733 | case CPP_OPEN_PAREN: | |
10734 | case CPP_OPEN_SQUARE: | |
10735 | case CPP_OPEN_BRACE: | |
10736 | ++depth; | |
10737 | break; | |
10738 | ||
10739 | case CPP_GREATER: | |
10740 | /* If we see a non-nested `>', and `>' is not an | |
10741 | operator, then it marks the end of the default | |
10742 | argument. */ | |
10743 | if (!depth && !greater_than_is_operator_p) | |
10744 | done = true; | |
10745 | break; | |
10746 | ||
10747 | /* If we run out of tokens, issue an error message. */ | |
10748 | case CPP_EOF: | |
10749 | error ("file ends in default argument"); | |
10750 | done = true; | |
10751 | break; | |
10752 | ||
10753 | case CPP_NAME: | |
10754 | case CPP_SCOPE: | |
10755 | /* In these cases, we should look for template-ids. | |
10756 | For example, if the default argument is | |
10757 | `X<int, double>()', we need to do name lookup to | |
10758 | figure out whether or not `X' is a template; if | |
34cd5ae7 | 10759 | so, the `,' does not end the default argument. |
a723baf1 MM |
10760 | |
10761 | That is not yet done. */ | |
10762 | break; | |
10763 | ||
10764 | default: | |
10765 | break; | |
10766 | } | |
10767 | ||
10768 | /* If we've reached the end, stop. */ | |
10769 | if (done) | |
10770 | break; | |
10771 | ||
10772 | /* Add the token to the token block. */ | |
10773 | token = cp_lexer_consume_token (parser->lexer); | |
10774 | cp_token_cache_push_token (DEFARG_TOKENS (default_argument), | |
10775 | token); | |
10776 | } | |
10777 | } | |
10778 | /* Outside of a class definition, we can just parse the | |
10779 | assignment-expression. */ | |
10780 | else | |
10781 | { | |
10782 | bool saved_local_variables_forbidden_p; | |
10783 | ||
10784 | /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is | |
10785 | set correctly. */ | |
10786 | saved_greater_than_is_operator_p | |
10787 | = parser->greater_than_is_operator_p; | |
10788 | parser->greater_than_is_operator_p = greater_than_is_operator_p; | |
10789 | /* Local variable names (and the `this' keyword) may not | |
10790 | appear in a default argument. */ | |
10791 | saved_local_variables_forbidden_p | |
10792 | = parser->local_variables_forbidden_p; | |
10793 | parser->local_variables_forbidden_p = true; | |
10794 | /* Parse the assignment-expression. */ | |
10795 | default_argument = cp_parser_assignment_expression (parser); | |
10796 | /* Restore saved state. */ | |
10797 | parser->greater_than_is_operator_p | |
10798 | = saved_greater_than_is_operator_p; | |
10799 | parser->local_variables_forbidden_p | |
10800 | = saved_local_variables_forbidden_p; | |
10801 | } | |
10802 | if (!parser->default_arg_ok_p) | |
10803 | { | |
c67d36d0 NS |
10804 | if (!flag_pedantic_errors) |
10805 | warning ("deprecated use of default argument for parameter of non-function"); | |
10806 | else | |
10807 | { | |
10808 | error ("default arguments are only permitted for function parameters"); | |
10809 | default_argument = NULL_TREE; | |
10810 | } | |
a723baf1 MM |
10811 | } |
10812 | } | |
10813 | else | |
10814 | default_argument = NULL_TREE; | |
10815 | ||
10816 | /* Create the representation of the parameter. */ | |
10817 | if (attributes) | |
10818 | decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers); | |
10819 | parameter = build_tree_list (default_argument, | |
10820 | build_tree_list (decl_specifiers, | |
10821 | declarator)); | |
10822 | ||
10823 | return parameter; | |
10824 | } | |
10825 | ||
10826 | /* Parse a function-definition. | |
10827 | ||
10828 | function-definition: | |
10829 | decl-specifier-seq [opt] declarator ctor-initializer [opt] | |
10830 | function-body | |
10831 | decl-specifier-seq [opt] declarator function-try-block | |
10832 | ||
10833 | GNU Extension: | |
10834 | ||
10835 | function-definition: | |
10836 | __extension__ function-definition | |
10837 | ||
10838 | Returns the FUNCTION_DECL for the function. If FRIEND_P is | |
10839 | non-NULL, *FRIEND_P is set to TRUE iff the function was declared to | |
10840 | be a `friend'. */ | |
10841 | ||
10842 | static tree | |
94edc4ab | 10843 | cp_parser_function_definition (cp_parser* parser, bool* friend_p) |
a723baf1 MM |
10844 | { |
10845 | tree decl_specifiers; | |
10846 | tree attributes; | |
10847 | tree declarator; | |
10848 | tree fn; | |
a723baf1 | 10849 | cp_token *token; |
560ad596 | 10850 | int declares_class_or_enum; |
a723baf1 MM |
10851 | bool member_p; |
10852 | /* The saved value of the PEDANTIC flag. */ | |
10853 | int saved_pedantic; | |
10854 | ||
10855 | /* Any pending qualification must be cleared by our caller. It is | |
10856 | more robust to force the callers to clear PARSER->SCOPE than to | |
10857 | do it here since if the qualification is in effect here, it might | |
10858 | also end up in effect elsewhere that it is not intended. */ | |
10859 | my_friendly_assert (!parser->scope, 20010821); | |
10860 | ||
10861 | /* Handle `__extension__'. */ | |
10862 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
10863 | { | |
10864 | /* Parse the function-definition. */ | |
10865 | fn = cp_parser_function_definition (parser, friend_p); | |
10866 | /* Restore the PEDANTIC flag. */ | |
10867 | pedantic = saved_pedantic; | |
10868 | ||
10869 | return fn; | |
10870 | } | |
10871 | ||
10872 | /* Check to see if this definition appears in a class-specifier. */ | |
10873 | member_p = (at_class_scope_p () | |
10874 | && TYPE_BEING_DEFINED (current_class_type)); | |
10875 | /* Defer access checks in the decl-specifier-seq until we know what | |
10876 | function is being defined. There is no need to do this for the | |
10877 | definition of member functions; we cannot be defining a member | |
10878 | from another class. */ | |
8d241e0b | 10879 | push_deferring_access_checks (member_p ? dk_no_check: dk_deferred); |
cf22909c | 10880 | |
a723baf1 MM |
10881 | /* Parse the decl-specifier-seq. */ |
10882 | decl_specifiers | |
10883 | = cp_parser_decl_specifier_seq (parser, | |
10884 | CP_PARSER_FLAGS_OPTIONAL, | |
10885 | &attributes, | |
10886 | &declares_class_or_enum); | |
10887 | /* Figure out whether this declaration is a `friend'. */ | |
10888 | if (friend_p) | |
10889 | *friend_p = cp_parser_friend_p (decl_specifiers); | |
10890 | ||
10891 | /* Parse the declarator. */ | |
62b8a44e | 10892 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
a723baf1 MM |
10893 | /*ctor_dtor_or_conv_p=*/NULL); |
10894 | ||
10895 | /* Gather up any access checks that occurred. */ | |
cf22909c | 10896 | stop_deferring_access_checks (); |
a723baf1 MM |
10897 | |
10898 | /* If something has already gone wrong, we may as well stop now. */ | |
10899 | if (declarator == error_mark_node) | |
10900 | { | |
10901 | /* Skip to the end of the function, or if this wasn't anything | |
10902 | like a function-definition, to a `;' in the hopes of finding | |
10903 | a sensible place from which to continue parsing. */ | |
10904 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
cf22909c | 10905 | pop_deferring_access_checks (); |
a723baf1 MM |
10906 | return error_mark_node; |
10907 | } | |
10908 | ||
10909 | /* The next character should be a `{' (for a simple function | |
10910 | definition), a `:' (for a ctor-initializer), or `try' (for a | |
10911 | function-try block). */ | |
10912 | token = cp_lexer_peek_token (parser->lexer); | |
10913 | if (!cp_parser_token_starts_function_definition_p (token)) | |
10914 | { | |
10915 | /* Issue the error-message. */ | |
10916 | cp_parser_error (parser, "expected function-definition"); | |
10917 | /* Skip to the next `;'. */ | |
10918 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
10919 | ||
cf22909c | 10920 | pop_deferring_access_checks (); |
a723baf1 MM |
10921 | return error_mark_node; |
10922 | } | |
10923 | ||
560ad596 MM |
10924 | cp_parser_check_for_definition_in_return_type (declarator, |
10925 | declares_class_or_enum); | |
10926 | ||
a723baf1 MM |
10927 | /* If we are in a class scope, then we must handle |
10928 | function-definitions specially. In particular, we save away the | |
10929 | tokens that make up the function body, and parse them again | |
10930 | later, in order to handle code like: | |
10931 | ||
10932 | struct S { | |
10933 | int f () { return i; } | |
10934 | int i; | |
10935 | }; | |
10936 | ||
10937 | Here, we cannot parse the body of `f' until after we have seen | |
10938 | the declaration of `i'. */ | |
10939 | if (member_p) | |
10940 | { | |
10941 | cp_token_cache *cache; | |
10942 | ||
10943 | /* Create the function-declaration. */ | |
10944 | fn = start_method (decl_specifiers, declarator, attributes); | |
10945 | /* If something went badly wrong, bail out now. */ | |
10946 | if (fn == error_mark_node) | |
10947 | { | |
10948 | /* If there's a function-body, skip it. */ | |
10949 | if (cp_parser_token_starts_function_definition_p | |
10950 | (cp_lexer_peek_token (parser->lexer))) | |
10951 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
cf22909c | 10952 | pop_deferring_access_checks (); |
a723baf1 MM |
10953 | return error_mark_node; |
10954 | } | |
10955 | ||
8db1028e NS |
10956 | /* Remember it, if there default args to post process. */ |
10957 | cp_parser_save_default_args (parser, fn); | |
10958 | ||
a723baf1 MM |
10959 | /* Create a token cache. */ |
10960 | cache = cp_token_cache_new (); | |
10961 | /* Save away the tokens that make up the body of the | |
10962 | function. */ | |
10963 | cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0); | |
10964 | /* Handle function try blocks. */ | |
10965 | while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH)) | |
10966 | cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0); | |
10967 | ||
10968 | /* Save away the inline definition; we will process it when the | |
10969 | class is complete. */ | |
10970 | DECL_PENDING_INLINE_INFO (fn) = cache; | |
10971 | DECL_PENDING_INLINE_P (fn) = 1; | |
10972 | ||
649fc72d NS |
10973 | /* We need to know that this was defined in the class, so that |
10974 | friend templates are handled correctly. */ | |
10975 | DECL_INITIALIZED_IN_CLASS_P (fn) = 1; | |
10976 | ||
a723baf1 MM |
10977 | /* We're done with the inline definition. */ |
10978 | finish_method (fn); | |
10979 | ||
10980 | /* Add FN to the queue of functions to be parsed later. */ | |
10981 | TREE_VALUE (parser->unparsed_functions_queues) | |
8218bd34 | 10982 | = tree_cons (NULL_TREE, fn, |
a723baf1 MM |
10983 | TREE_VALUE (parser->unparsed_functions_queues)); |
10984 | ||
cf22909c | 10985 | pop_deferring_access_checks (); |
a723baf1 MM |
10986 | return fn; |
10987 | } | |
10988 | ||
10989 | /* Check that the number of template-parameter-lists is OK. */ | |
10990 | if (!cp_parser_check_declarator_template_parameters (parser, | |
10991 | declarator)) | |
10992 | { | |
10993 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
cf22909c | 10994 | pop_deferring_access_checks (); |
a723baf1 MM |
10995 | return error_mark_node; |
10996 | } | |
10997 | ||
cf22909c KL |
10998 | fn = cp_parser_function_definition_from_specifiers_and_declarator |
10999 | (parser, decl_specifiers, attributes, declarator); | |
11000 | pop_deferring_access_checks (); | |
11001 | return fn; | |
a723baf1 MM |
11002 | } |
11003 | ||
11004 | /* Parse a function-body. | |
11005 | ||
11006 | function-body: | |
11007 | compound_statement */ | |
11008 | ||
11009 | static void | |
11010 | cp_parser_function_body (cp_parser *parser) | |
11011 | { | |
a5bcc582 | 11012 | cp_parser_compound_statement (parser, false); |
a723baf1 MM |
11013 | } |
11014 | ||
11015 | /* Parse a ctor-initializer-opt followed by a function-body. Return | |
11016 | true if a ctor-initializer was present. */ | |
11017 | ||
11018 | static bool | |
11019 | cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser) | |
11020 | { | |
11021 | tree body; | |
11022 | bool ctor_initializer_p; | |
11023 | ||
11024 | /* Begin the function body. */ | |
11025 | body = begin_function_body (); | |
11026 | /* Parse the optional ctor-initializer. */ | |
11027 | ctor_initializer_p = cp_parser_ctor_initializer_opt (parser); | |
11028 | /* Parse the function-body. */ | |
11029 | cp_parser_function_body (parser); | |
11030 | /* Finish the function body. */ | |
11031 | finish_function_body (body); | |
11032 | ||
11033 | return ctor_initializer_p; | |
11034 | } | |
11035 | ||
11036 | /* Parse an initializer. | |
11037 | ||
11038 | initializer: | |
11039 | = initializer-clause | |
11040 | ( expression-list ) | |
11041 | ||
11042 | Returns a expression representing the initializer. If no | |
11043 | initializer is present, NULL_TREE is returned. | |
11044 | ||
11045 | *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )' | |
11046 | production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is | |
39703eb9 MM |
11047 | set to FALSE if there is no initializer present. If there is an |
11048 | initializer, and it is not a constant-expression, *NON_CONSTANT_P | |
11049 | is set to true; otherwise it is set to false. */ | |
a723baf1 MM |
11050 | |
11051 | static tree | |
39703eb9 MM |
11052 | cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init, |
11053 | bool* non_constant_p) | |
a723baf1 MM |
11054 | { |
11055 | cp_token *token; | |
11056 | tree init; | |
11057 | ||
11058 | /* Peek at the next token. */ | |
11059 | token = cp_lexer_peek_token (parser->lexer); | |
11060 | ||
11061 | /* Let our caller know whether or not this initializer was | |
11062 | parenthesized. */ | |
11063 | *is_parenthesized_init = (token->type == CPP_OPEN_PAREN); | |
39703eb9 MM |
11064 | /* Assume that the initializer is constant. */ |
11065 | *non_constant_p = false; | |
a723baf1 MM |
11066 | |
11067 | if (token->type == CPP_EQ) | |
11068 | { | |
11069 | /* Consume the `='. */ | |
11070 | cp_lexer_consume_token (parser->lexer); | |
11071 | /* Parse the initializer-clause. */ | |
39703eb9 | 11072 | init = cp_parser_initializer_clause (parser, non_constant_p); |
a723baf1 MM |
11073 | } |
11074 | else if (token->type == CPP_OPEN_PAREN) | |
39703eb9 MM |
11075 | init = cp_parser_parenthesized_expression_list (parser, false, |
11076 | non_constant_p); | |
a723baf1 MM |
11077 | else |
11078 | { | |
11079 | /* Anything else is an error. */ | |
11080 | cp_parser_error (parser, "expected initializer"); | |
11081 | init = error_mark_node; | |
11082 | } | |
11083 | ||
11084 | return init; | |
11085 | } | |
11086 | ||
11087 | /* Parse an initializer-clause. | |
11088 | ||
11089 | initializer-clause: | |
11090 | assignment-expression | |
11091 | { initializer-list , [opt] } | |
11092 | { } | |
11093 | ||
11094 | Returns an expression representing the initializer. | |
11095 | ||
11096 | If the `assignment-expression' production is used the value | |
34cd5ae7 | 11097 | returned is simply a representation for the expression. |
a723baf1 MM |
11098 | |
11099 | Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be | |
11100 | the elements of the initializer-list (or NULL_TREE, if the last | |
11101 | production is used). The TREE_TYPE for the CONSTRUCTOR will be | |
11102 | NULL_TREE. There is no way to detect whether or not the optional | |
39703eb9 MM |
11103 | trailing `,' was provided. NON_CONSTANT_P is as for |
11104 | cp_parser_initializer. */ | |
a723baf1 MM |
11105 | |
11106 | static tree | |
39703eb9 | 11107 | cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p) |
a723baf1 MM |
11108 | { |
11109 | tree initializer; | |
11110 | ||
11111 | /* If it is not a `{', then we are looking at an | |
11112 | assignment-expression. */ | |
11113 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
39703eb9 MM |
11114 | initializer |
11115 | = cp_parser_constant_expression (parser, | |
11116 | /*allow_non_constant_p=*/true, | |
11117 | non_constant_p); | |
a723baf1 MM |
11118 | else |
11119 | { | |
11120 | /* Consume the `{' token. */ | |
11121 | cp_lexer_consume_token (parser->lexer); | |
11122 | /* Create a CONSTRUCTOR to represent the braced-initializer. */ | |
11123 | initializer = make_node (CONSTRUCTOR); | |
11124 | /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be | |
11125 | necessary, but check_initializer depends upon it, for | |
11126 | now. */ | |
11127 | TREE_HAS_CONSTRUCTOR (initializer) = 1; | |
11128 | /* If it's not a `}', then there is a non-trivial initializer. */ | |
11129 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)) | |
11130 | { | |
11131 | /* Parse the initializer list. */ | |
11132 | CONSTRUCTOR_ELTS (initializer) | |
39703eb9 | 11133 | = cp_parser_initializer_list (parser, non_constant_p); |
a723baf1 MM |
11134 | /* A trailing `,' token is allowed. */ |
11135 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
11136 | cp_lexer_consume_token (parser->lexer); | |
11137 | } | |
a723baf1 MM |
11138 | /* Now, there should be a trailing `}'. */ |
11139 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
11140 | } | |
11141 | ||
11142 | return initializer; | |
11143 | } | |
11144 | ||
11145 | /* Parse an initializer-list. | |
11146 | ||
11147 | initializer-list: | |
11148 | initializer-clause | |
11149 | initializer-list , initializer-clause | |
11150 | ||
11151 | GNU Extension: | |
11152 | ||
11153 | initializer-list: | |
11154 | identifier : initializer-clause | |
11155 | initializer-list, identifier : initializer-clause | |
11156 | ||
11157 | Returns a TREE_LIST. The TREE_VALUE of each node is an expression | |
11158 | for the initializer. If the TREE_PURPOSE is non-NULL, it is the | |
39703eb9 MM |
11159 | IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is |
11160 | as for cp_parser_initializer. */ | |
a723baf1 MM |
11161 | |
11162 | static tree | |
39703eb9 | 11163 | cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p) |
a723baf1 MM |
11164 | { |
11165 | tree initializers = NULL_TREE; | |
11166 | ||
39703eb9 MM |
11167 | /* Assume all of the expressions are constant. */ |
11168 | *non_constant_p = false; | |
11169 | ||
a723baf1 MM |
11170 | /* Parse the rest of the list. */ |
11171 | while (true) | |
11172 | { | |
11173 | cp_token *token; | |
11174 | tree identifier; | |
11175 | tree initializer; | |
39703eb9 MM |
11176 | bool clause_non_constant_p; |
11177 | ||
a723baf1 MM |
11178 | /* If the next token is an identifier and the following one is a |
11179 | colon, we are looking at the GNU designated-initializer | |
11180 | syntax. */ | |
11181 | if (cp_parser_allow_gnu_extensions_p (parser) | |
11182 | && cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
11183 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON) | |
11184 | { | |
11185 | /* Consume the identifier. */ | |
11186 | identifier = cp_lexer_consume_token (parser->lexer)->value; | |
11187 | /* Consume the `:'. */ | |
11188 | cp_lexer_consume_token (parser->lexer); | |
11189 | } | |
11190 | else | |
11191 | identifier = NULL_TREE; | |
11192 | ||
11193 | /* Parse the initializer. */ | |
39703eb9 MM |
11194 | initializer = cp_parser_initializer_clause (parser, |
11195 | &clause_non_constant_p); | |
11196 | /* If any clause is non-constant, so is the entire initializer. */ | |
11197 | if (clause_non_constant_p) | |
11198 | *non_constant_p = true; | |
a723baf1 MM |
11199 | /* Add it to the list. */ |
11200 | initializers = tree_cons (identifier, initializer, initializers); | |
11201 | ||
11202 | /* If the next token is not a comma, we have reached the end of | |
11203 | the list. */ | |
11204 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
11205 | break; | |
11206 | ||
11207 | /* Peek at the next token. */ | |
11208 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
11209 | /* If the next token is a `}', then we're still done. An | |
11210 | initializer-clause can have a trailing `,' after the | |
11211 | initializer-list and before the closing `}'. */ | |
11212 | if (token->type == CPP_CLOSE_BRACE) | |
11213 | break; | |
11214 | ||
11215 | /* Consume the `,' token. */ | |
11216 | cp_lexer_consume_token (parser->lexer); | |
11217 | } | |
11218 | ||
11219 | /* The initializers were built up in reverse order, so we need to | |
11220 | reverse them now. */ | |
11221 | return nreverse (initializers); | |
11222 | } | |
11223 | ||
11224 | /* Classes [gram.class] */ | |
11225 | ||
11226 | /* Parse a class-name. | |
11227 | ||
11228 | class-name: | |
11229 | identifier | |
11230 | template-id | |
11231 | ||
11232 | TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used | |
11233 | to indicate that names looked up in dependent types should be | |
11234 | assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template' | |
11235 | keyword has been used to indicate that the name that appears next | |
11236 | is a template. TYPE_P is true iff the next name should be treated | |
11237 | as class-name, even if it is declared to be some other kind of name | |
8d241e0b KL |
11238 | as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in |
11239 | dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class | |
11240 | being defined in a class-head. | |
a723baf1 MM |
11241 | |
11242 | Returns the TYPE_DECL representing the class. */ | |
11243 | ||
11244 | static tree | |
11245 | cp_parser_class_name (cp_parser *parser, | |
11246 | bool typename_keyword_p, | |
11247 | bool template_keyword_p, | |
11248 | bool type_p, | |
a723baf1 | 11249 | bool check_dependency_p, |
a668c6ad MM |
11250 | bool class_head_p, |
11251 | bool is_declaration) | |
a723baf1 MM |
11252 | { |
11253 | tree decl; | |
11254 | tree scope; | |
11255 | bool typename_p; | |
e5976695 MM |
11256 | cp_token *token; |
11257 | ||
11258 | /* All class-names start with an identifier. */ | |
11259 | token = cp_lexer_peek_token (parser->lexer); | |
11260 | if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID) | |
11261 | { | |
11262 | cp_parser_error (parser, "expected class-name"); | |
11263 | return error_mark_node; | |
11264 | } | |
11265 | ||
a723baf1 MM |
11266 | /* PARSER->SCOPE can be cleared when parsing the template-arguments |
11267 | to a template-id, so we save it here. */ | |
11268 | scope = parser->scope; | |
3adee96c KL |
11269 | if (scope == error_mark_node) |
11270 | return error_mark_node; | |
11271 | ||
a723baf1 MM |
11272 | /* Any name names a type if we're following the `typename' keyword |
11273 | in a qualified name where the enclosing scope is type-dependent. */ | |
11274 | typename_p = (typename_keyword_p && scope && TYPE_P (scope) | |
1fb3244a | 11275 | && dependent_type_p (scope)); |
e5976695 MM |
11276 | /* Handle the common case (an identifier, but not a template-id) |
11277 | efficiently. */ | |
11278 | if (token->type == CPP_NAME | |
11279 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS) | |
a723baf1 | 11280 | { |
a723baf1 MM |
11281 | tree identifier; |
11282 | ||
11283 | /* Look for the identifier. */ | |
11284 | identifier = cp_parser_identifier (parser); | |
11285 | /* If the next token isn't an identifier, we are certainly not | |
11286 | looking at a class-name. */ | |
11287 | if (identifier == error_mark_node) | |
11288 | decl = error_mark_node; | |
11289 | /* If we know this is a type-name, there's no need to look it | |
11290 | up. */ | |
11291 | else if (typename_p) | |
11292 | decl = identifier; | |
11293 | else | |
11294 | { | |
11295 | /* If the next token is a `::', then the name must be a type | |
11296 | name. | |
11297 | ||
11298 | [basic.lookup.qual] | |
11299 | ||
11300 | During the lookup for a name preceding the :: scope | |
11301 | resolution operator, object, function, and enumerator | |
11302 | names are ignored. */ | |
11303 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
11304 | type_p = true; | |
11305 | /* Look up the name. */ | |
11306 | decl = cp_parser_lookup_name (parser, identifier, | |
a723baf1 | 11307 | type_p, |
eea9800f | 11308 | /*is_namespace=*/false, |
a723baf1 MM |
11309 | check_dependency_p); |
11310 | } | |
11311 | } | |
e5976695 MM |
11312 | else |
11313 | { | |
11314 | /* Try a template-id. */ | |
11315 | decl = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
11316 | check_dependency_p, |
11317 | is_declaration); | |
e5976695 MM |
11318 | if (decl == error_mark_node) |
11319 | return error_mark_node; | |
11320 | } | |
a723baf1 MM |
11321 | |
11322 | decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p); | |
11323 | ||
11324 | /* If this is a typename, create a TYPENAME_TYPE. */ | |
11325 | if (typename_p && decl != error_mark_node) | |
11326 | decl = TYPE_NAME (make_typename_type (scope, decl, | |
11327 | /*complain=*/1)); | |
11328 | ||
11329 | /* Check to see that it is really the name of a class. */ | |
11330 | if (TREE_CODE (decl) == TEMPLATE_ID_EXPR | |
11331 | && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE | |
11332 | && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
11333 | /* Situations like this: | |
11334 | ||
11335 | template <typename T> struct A { | |
11336 | typename T::template X<int>::I i; | |
11337 | }; | |
11338 | ||
11339 | are problematic. Is `T::template X<int>' a class-name? The | |
11340 | standard does not seem to be definitive, but there is no other | |
11341 | valid interpretation of the following `::'. Therefore, those | |
11342 | names are considered class-names. */ | |
78757caa | 11343 | decl = TYPE_NAME (make_typename_type (scope, decl, tf_error)); |
a723baf1 MM |
11344 | else if (decl == error_mark_node |
11345 | || TREE_CODE (decl) != TYPE_DECL | |
11346 | || !IS_AGGR_TYPE (TREE_TYPE (decl))) | |
11347 | { | |
11348 | cp_parser_error (parser, "expected class-name"); | |
11349 | return error_mark_node; | |
11350 | } | |
11351 | ||
11352 | return decl; | |
11353 | } | |
11354 | ||
11355 | /* Parse a class-specifier. | |
11356 | ||
11357 | class-specifier: | |
11358 | class-head { member-specification [opt] } | |
11359 | ||
11360 | Returns the TREE_TYPE representing the class. */ | |
11361 | ||
11362 | static tree | |
94edc4ab | 11363 | cp_parser_class_specifier (cp_parser* parser) |
a723baf1 MM |
11364 | { |
11365 | cp_token *token; | |
11366 | tree type; | |
11367 | tree attributes = NULL_TREE; | |
11368 | int has_trailing_semicolon; | |
11369 | bool nested_name_specifier_p; | |
a723baf1 MM |
11370 | unsigned saved_num_template_parameter_lists; |
11371 | ||
8d241e0b | 11372 | push_deferring_access_checks (dk_no_deferred); |
cf22909c | 11373 | |
a723baf1 MM |
11374 | /* Parse the class-head. */ |
11375 | type = cp_parser_class_head (parser, | |
cf22909c | 11376 | &nested_name_specifier_p); |
a723baf1 MM |
11377 | /* If the class-head was a semantic disaster, skip the entire body |
11378 | of the class. */ | |
11379 | if (!type) | |
11380 | { | |
11381 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
cf22909c | 11382 | pop_deferring_access_checks (); |
a723baf1 MM |
11383 | return error_mark_node; |
11384 | } | |
cf22909c | 11385 | |
a723baf1 MM |
11386 | /* Look for the `{'. */ |
11387 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
cf22909c KL |
11388 | { |
11389 | pop_deferring_access_checks (); | |
11390 | return error_mark_node; | |
11391 | } | |
11392 | ||
a723baf1 MM |
11393 | /* Issue an error message if type-definitions are forbidden here. */ |
11394 | cp_parser_check_type_definition (parser); | |
11395 | /* Remember that we are defining one more class. */ | |
11396 | ++parser->num_classes_being_defined; | |
11397 | /* Inside the class, surrounding template-parameter-lists do not | |
11398 | apply. */ | |
11399 | saved_num_template_parameter_lists | |
11400 | = parser->num_template_parameter_lists; | |
11401 | parser->num_template_parameter_lists = 0; | |
78757caa | 11402 | |
a723baf1 MM |
11403 | /* Start the class. */ |
11404 | type = begin_class_definition (type); | |
11405 | if (type == error_mark_node) | |
9bcb9aae | 11406 | /* If the type is erroneous, skip the entire body of the class. */ |
a723baf1 MM |
11407 | cp_parser_skip_to_closing_brace (parser); |
11408 | else | |
11409 | /* Parse the member-specification. */ | |
11410 | cp_parser_member_specification_opt (parser); | |
11411 | /* Look for the trailing `}'. */ | |
11412 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
11413 | /* We get better error messages by noticing a common problem: a | |
11414 | missing trailing `;'. */ | |
11415 | token = cp_lexer_peek_token (parser->lexer); | |
11416 | has_trailing_semicolon = (token->type == CPP_SEMICOLON); | |
11417 | /* Look for attributes to apply to this class. */ | |
11418 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
11419 | attributes = cp_parser_attributes_opt (parser); | |
560ad596 MM |
11420 | /* If we got any attributes in class_head, xref_tag will stick them in |
11421 | TREE_TYPE of the type. Grab them now. */ | |
11422 | if (type != error_mark_node) | |
11423 | { | |
11424 | attributes = chainon (TYPE_ATTRIBUTES (type), attributes); | |
11425 | TYPE_ATTRIBUTES (type) = NULL_TREE; | |
11426 | type = finish_struct (type, attributes); | |
11427 | } | |
11428 | if (nested_name_specifier_p) | |
11429 | pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type))); | |
a723baf1 MM |
11430 | /* If this class is not itself within the scope of another class, |
11431 | then we need to parse the bodies of all of the queued function | |
11432 | definitions. Note that the queued functions defined in a class | |
11433 | are not always processed immediately following the | |
11434 | class-specifier for that class. Consider: | |
11435 | ||
11436 | struct A { | |
11437 | struct B { void f() { sizeof (A); } }; | |
11438 | }; | |
11439 | ||
11440 | If `f' were processed before the processing of `A' were | |
11441 | completed, there would be no way to compute the size of `A'. | |
11442 | Note that the nesting we are interested in here is lexical -- | |
11443 | not the semantic nesting given by TYPE_CONTEXT. In particular, | |
11444 | for: | |
11445 | ||
11446 | struct A { struct B; }; | |
11447 | struct A::B { void f() { } }; | |
11448 | ||
11449 | there is no need to delay the parsing of `A::B::f'. */ | |
11450 | if (--parser->num_classes_being_defined == 0) | |
11451 | { | |
8218bd34 MM |
11452 | tree queue_entry; |
11453 | tree fn; | |
a723baf1 | 11454 | |
8218bd34 MM |
11455 | /* In a first pass, parse default arguments to the functions. |
11456 | Then, in a second pass, parse the bodies of the functions. | |
11457 | This two-phased approach handles cases like: | |
11458 | ||
11459 | struct S { | |
11460 | void f() { g(); } | |
11461 | void g(int i = 3); | |
11462 | }; | |
11463 | ||
11464 | */ | |
8db1028e NS |
11465 | for (TREE_PURPOSE (parser->unparsed_functions_queues) |
11466 | = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues)); | |
11467 | (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues)); | |
11468 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
11469 | = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues))) | |
8218bd34 MM |
11470 | { |
11471 | fn = TREE_VALUE (queue_entry); | |
8218bd34 MM |
11472 | /* Make sure that any template parameters are in scope. */ |
11473 | maybe_begin_member_template_processing (fn); | |
11474 | /* If there are default arguments that have not yet been processed, | |
11475 | take care of them now. */ | |
11476 | cp_parser_late_parsing_default_args (parser, fn); | |
11477 | /* Remove any template parameters from the symbol table. */ | |
11478 | maybe_end_member_template_processing (); | |
11479 | } | |
11480 | /* Now parse the body of the functions. */ | |
8db1028e NS |
11481 | for (TREE_VALUE (parser->unparsed_functions_queues) |
11482 | = nreverse (TREE_VALUE (parser->unparsed_functions_queues)); | |
11483 | (queue_entry = TREE_VALUE (parser->unparsed_functions_queues)); | |
11484 | TREE_VALUE (parser->unparsed_functions_queues) | |
11485 | = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues))) | |
a723baf1 | 11486 | { |
a723baf1 | 11487 | /* Figure out which function we need to process. */ |
a723baf1 MM |
11488 | fn = TREE_VALUE (queue_entry); |
11489 | ||
11490 | /* Parse the function. */ | |
11491 | cp_parser_late_parsing_for_member (parser, fn); | |
a723baf1 MM |
11492 | } |
11493 | ||
a723baf1 MM |
11494 | } |
11495 | ||
11496 | /* Put back any saved access checks. */ | |
cf22909c | 11497 | pop_deferring_access_checks (); |
a723baf1 MM |
11498 | |
11499 | /* Restore the count of active template-parameter-lists. */ | |
11500 | parser->num_template_parameter_lists | |
11501 | = saved_num_template_parameter_lists; | |
11502 | ||
11503 | return type; | |
11504 | } | |
11505 | ||
11506 | /* Parse a class-head. | |
11507 | ||
11508 | class-head: | |
11509 | class-key identifier [opt] base-clause [opt] | |
11510 | class-key nested-name-specifier identifier base-clause [opt] | |
11511 | class-key nested-name-specifier [opt] template-id | |
11512 | base-clause [opt] | |
11513 | ||
11514 | GNU Extensions: | |
11515 | class-key attributes identifier [opt] base-clause [opt] | |
11516 | class-key attributes nested-name-specifier identifier base-clause [opt] | |
11517 | class-key attributes nested-name-specifier [opt] template-id | |
11518 | base-clause [opt] | |
11519 | ||
11520 | Returns the TYPE of the indicated class. Sets | |
11521 | *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions | |
11522 | involving a nested-name-specifier was used, and FALSE otherwise. | |
a723baf1 MM |
11523 | |
11524 | Returns NULL_TREE if the class-head is syntactically valid, but | |
11525 | semantically invalid in a way that means we should skip the entire | |
11526 | body of the class. */ | |
11527 | ||
11528 | static tree | |
94edc4ab NN |
11529 | cp_parser_class_head (cp_parser* parser, |
11530 | bool* nested_name_specifier_p) | |
a723baf1 MM |
11531 | { |
11532 | cp_token *token; | |
11533 | tree nested_name_specifier; | |
11534 | enum tag_types class_key; | |
11535 | tree id = NULL_TREE; | |
11536 | tree type = NULL_TREE; | |
11537 | tree attributes; | |
11538 | bool template_id_p = false; | |
11539 | bool qualified_p = false; | |
11540 | bool invalid_nested_name_p = false; | |
11541 | unsigned num_templates; | |
11542 | ||
11543 | /* Assume no nested-name-specifier will be present. */ | |
11544 | *nested_name_specifier_p = false; | |
11545 | /* Assume no template parameter lists will be used in defining the | |
11546 | type. */ | |
11547 | num_templates = 0; | |
11548 | ||
11549 | /* Look for the class-key. */ | |
11550 | class_key = cp_parser_class_key (parser); | |
11551 | if (class_key == none_type) | |
11552 | return error_mark_node; | |
11553 | ||
11554 | /* Parse the attributes. */ | |
11555 | attributes = cp_parser_attributes_opt (parser); | |
11556 | ||
11557 | /* If the next token is `::', that is invalid -- but sometimes | |
11558 | people do try to write: | |
11559 | ||
11560 | struct ::S {}; | |
11561 | ||
11562 | Handle this gracefully by accepting the extra qualifier, and then | |
11563 | issuing an error about it later if this really is a | |
2050a1bb | 11564 | class-head. If it turns out just to be an elaborated type |
a723baf1 MM |
11565 | specifier, remain silent. */ |
11566 | if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)) | |
11567 | qualified_p = true; | |
11568 | ||
8d241e0b KL |
11569 | push_deferring_access_checks (dk_no_check); |
11570 | ||
a723baf1 MM |
11571 | /* Determine the name of the class. Begin by looking for an |
11572 | optional nested-name-specifier. */ | |
11573 | nested_name_specifier | |
11574 | = cp_parser_nested_name_specifier_opt (parser, | |
11575 | /*typename_keyword_p=*/false, | |
66d418e6 | 11576 | /*check_dependency_p=*/false, |
a668c6ad MM |
11577 | /*type_p=*/false, |
11578 | /*is_declaration=*/false); | |
a723baf1 MM |
11579 | /* If there was a nested-name-specifier, then there *must* be an |
11580 | identifier. */ | |
11581 | if (nested_name_specifier) | |
11582 | { | |
11583 | /* Although the grammar says `identifier', it really means | |
11584 | `class-name' or `template-name'. You are only allowed to | |
11585 | define a class that has already been declared with this | |
11586 | syntax. | |
11587 | ||
11588 | The proposed resolution for Core Issue 180 says that whever | |
11589 | you see `class T::X' you should treat `X' as a type-name. | |
11590 | ||
11591 | It is OK to define an inaccessible class; for example: | |
11592 | ||
11593 | class A { class B; }; | |
11594 | class A::B {}; | |
11595 | ||
a723baf1 MM |
11596 | We do not know if we will see a class-name, or a |
11597 | template-name. We look for a class-name first, in case the | |
11598 | class-name is a template-id; if we looked for the | |
11599 | template-name first we would stop after the template-name. */ | |
11600 | cp_parser_parse_tentatively (parser); | |
11601 | type = cp_parser_class_name (parser, | |
11602 | /*typename_keyword_p=*/false, | |
11603 | /*template_keyword_p=*/false, | |
11604 | /*type_p=*/true, | |
a723baf1 | 11605 | /*check_dependency_p=*/false, |
a668c6ad MM |
11606 | /*class_head_p=*/true, |
11607 | /*is_declaration=*/false); | |
a723baf1 MM |
11608 | /* If that didn't work, ignore the nested-name-specifier. */ |
11609 | if (!cp_parser_parse_definitely (parser)) | |
11610 | { | |
11611 | invalid_nested_name_p = true; | |
11612 | id = cp_parser_identifier (parser); | |
11613 | if (id == error_mark_node) | |
11614 | id = NULL_TREE; | |
11615 | } | |
11616 | /* If we could not find a corresponding TYPE, treat this | |
11617 | declaration like an unqualified declaration. */ | |
11618 | if (type == error_mark_node) | |
11619 | nested_name_specifier = NULL_TREE; | |
11620 | /* Otherwise, count the number of templates used in TYPE and its | |
11621 | containing scopes. */ | |
11622 | else | |
11623 | { | |
11624 | tree scope; | |
11625 | ||
11626 | for (scope = TREE_TYPE (type); | |
11627 | scope && TREE_CODE (scope) != NAMESPACE_DECL; | |
11628 | scope = (TYPE_P (scope) | |
11629 | ? TYPE_CONTEXT (scope) | |
11630 | : DECL_CONTEXT (scope))) | |
11631 | if (TYPE_P (scope) | |
11632 | && CLASS_TYPE_P (scope) | |
11633 | && CLASSTYPE_TEMPLATE_INFO (scope) | |
2050a1bb MM |
11634 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)) |
11635 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)) | |
a723baf1 MM |
11636 | ++num_templates; |
11637 | } | |
11638 | } | |
11639 | /* Otherwise, the identifier is optional. */ | |
11640 | else | |
11641 | { | |
11642 | /* We don't know whether what comes next is a template-id, | |
11643 | an identifier, or nothing at all. */ | |
11644 | cp_parser_parse_tentatively (parser); | |
11645 | /* Check for a template-id. */ | |
11646 | id = cp_parser_template_id (parser, | |
11647 | /*template_keyword_p=*/false, | |
a668c6ad MM |
11648 | /*check_dependency_p=*/true, |
11649 | /*is_declaration=*/true); | |
a723baf1 MM |
11650 | /* If that didn't work, it could still be an identifier. */ |
11651 | if (!cp_parser_parse_definitely (parser)) | |
11652 | { | |
11653 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
11654 | id = cp_parser_identifier (parser); | |
11655 | else | |
11656 | id = NULL_TREE; | |
11657 | } | |
11658 | else | |
11659 | { | |
11660 | template_id_p = true; | |
11661 | ++num_templates; | |
11662 | } | |
11663 | } | |
11664 | ||
8d241e0b KL |
11665 | pop_deferring_access_checks (); |
11666 | ||
a723baf1 MM |
11667 | /* If it's not a `:' or a `{' then we can't really be looking at a |
11668 | class-head, since a class-head only appears as part of a | |
11669 | class-specifier. We have to detect this situation before calling | |
11670 | xref_tag, since that has irreversible side-effects. */ | |
11671 | if (!cp_parser_next_token_starts_class_definition_p (parser)) | |
11672 | { | |
11673 | cp_parser_error (parser, "expected `{' or `:'"); | |
11674 | return error_mark_node; | |
11675 | } | |
11676 | ||
11677 | /* At this point, we're going ahead with the class-specifier, even | |
11678 | if some other problem occurs. */ | |
11679 | cp_parser_commit_to_tentative_parse (parser); | |
11680 | /* Issue the error about the overly-qualified name now. */ | |
11681 | if (qualified_p) | |
11682 | cp_parser_error (parser, | |
11683 | "global qualification of class name is invalid"); | |
11684 | else if (invalid_nested_name_p) | |
11685 | cp_parser_error (parser, | |
11686 | "qualified name does not name a class"); | |
11687 | /* Make sure that the right number of template parameters were | |
11688 | present. */ | |
11689 | if (!cp_parser_check_template_parameters (parser, num_templates)) | |
11690 | /* If something went wrong, there is no point in even trying to | |
11691 | process the class-definition. */ | |
11692 | return NULL_TREE; | |
11693 | ||
a723baf1 MM |
11694 | /* Look up the type. */ |
11695 | if (template_id_p) | |
11696 | { | |
11697 | type = TREE_TYPE (id); | |
11698 | maybe_process_partial_specialization (type); | |
11699 | } | |
11700 | else if (!nested_name_specifier) | |
11701 | { | |
11702 | /* If the class was unnamed, create a dummy name. */ | |
11703 | if (!id) | |
11704 | id = make_anon_name (); | |
cbd63935 KL |
11705 | type = xref_tag (class_key, id, attributes, /*globalize=*/false, |
11706 | parser->num_template_parameter_lists); | |
a723baf1 MM |
11707 | } |
11708 | else | |
11709 | { | |
a723baf1 | 11710 | tree class_type; |
089d6ea7 | 11711 | tree scope; |
a723baf1 MM |
11712 | |
11713 | /* Given: | |
11714 | ||
11715 | template <typename T> struct S { struct T }; | |
14d22dd6 | 11716 | template <typename T> struct S<T>::T { }; |
a723baf1 MM |
11717 | |
11718 | we will get a TYPENAME_TYPE when processing the definition of | |
11719 | `S::T'. We need to resolve it to the actual type before we | |
11720 | try to define it. */ | |
11721 | if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE) | |
11722 | { | |
14d22dd6 MM |
11723 | class_type = resolve_typename_type (TREE_TYPE (type), |
11724 | /*only_current_p=*/false); | |
11725 | if (class_type != error_mark_node) | |
11726 | type = TYPE_NAME (class_type); | |
11727 | else | |
11728 | { | |
11729 | cp_parser_error (parser, "could not resolve typename type"); | |
11730 | type = error_mark_node; | |
11731 | } | |
a723baf1 MM |
11732 | } |
11733 | ||
089d6ea7 MM |
11734 | /* Figure out in what scope the declaration is being placed. */ |
11735 | scope = current_scope (); | |
11736 | if (!scope) | |
11737 | scope = current_namespace; | |
11738 | /* If that scope does not contain the scope in which the | |
11739 | class was originally declared, the program is invalid. */ | |
11740 | if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type))) | |
11741 | { | |
0e136342 | 11742 | error ("declaration of `%D' in `%D' which does not " |
089d6ea7 MM |
11743 | "enclose `%D'", type, scope, nested_name_specifier); |
11744 | return NULL_TREE; | |
11745 | } | |
560ad596 | 11746 | /* [dcl.meaning] |
089d6ea7 | 11747 | |
560ad596 MM |
11748 | A declarator-id shall not be qualified exception of the |
11749 | definition of a ... nested class outside of its class | |
11750 | ... [or] a the definition or explicit instantiation of a | |
11751 | class member of a namespace outside of its namespace. */ | |
11752 | if (scope == CP_DECL_CONTEXT (type)) | |
a723baf1 | 11753 | { |
560ad596 MM |
11754 | pedwarn ("extra qualification ignored"); |
11755 | nested_name_specifier = NULL_TREE; | |
a723baf1 | 11756 | } |
560ad596 MM |
11757 | |
11758 | maybe_process_partial_specialization (TREE_TYPE (type)); | |
11759 | class_type = current_class_type; | |
11760 | /* Enter the scope indicated by the nested-name-specifier. */ | |
11761 | if (nested_name_specifier) | |
11762 | push_scope (nested_name_specifier); | |
11763 | /* Get the canonical version of this type. */ | |
11764 | type = TYPE_MAIN_DECL (TREE_TYPE (type)); | |
11765 | if (PROCESSING_REAL_TEMPLATE_DECL_P () | |
11766 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type))) | |
11767 | type = push_template_decl (type); | |
11768 | type = TREE_TYPE (type); | |
11769 | if (nested_name_specifier) | |
11770 | *nested_name_specifier_p = true; | |
a723baf1 MM |
11771 | } |
11772 | /* Indicate whether this class was declared as a `class' or as a | |
11773 | `struct'. */ | |
11774 | if (TREE_CODE (type) == RECORD_TYPE) | |
11775 | CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type); | |
11776 | cp_parser_check_class_key (class_key, type); | |
11777 | ||
11778 | /* Enter the scope containing the class; the names of base classes | |
11779 | should be looked up in that context. For example, given: | |
11780 | ||
11781 | struct A { struct B {}; struct C; }; | |
11782 | struct A::C : B {}; | |
11783 | ||
11784 | is valid. */ | |
11785 | if (nested_name_specifier) | |
11786 | push_scope (nested_name_specifier); | |
11787 | /* Now, look for the base-clause. */ | |
11788 | token = cp_lexer_peek_token (parser->lexer); | |
11789 | if (token->type == CPP_COLON) | |
11790 | { | |
11791 | tree bases; | |
11792 | ||
11793 | /* Get the list of base-classes. */ | |
11794 | bases = cp_parser_base_clause (parser); | |
11795 | /* Process them. */ | |
11796 | xref_basetypes (type, bases); | |
11797 | } | |
11798 | /* Leave the scope given by the nested-name-specifier. We will | |
11799 | enter the class scope itself while processing the members. */ | |
11800 | if (nested_name_specifier) | |
11801 | pop_scope (nested_name_specifier); | |
11802 | ||
11803 | return type; | |
11804 | } | |
11805 | ||
11806 | /* Parse a class-key. | |
11807 | ||
11808 | class-key: | |
11809 | class | |
11810 | struct | |
11811 | union | |
11812 | ||
11813 | Returns the kind of class-key specified, or none_type to indicate | |
11814 | error. */ | |
11815 | ||
11816 | static enum tag_types | |
94edc4ab | 11817 | cp_parser_class_key (cp_parser* parser) |
a723baf1 MM |
11818 | { |
11819 | cp_token *token; | |
11820 | enum tag_types tag_type; | |
11821 | ||
11822 | /* Look for the class-key. */ | |
11823 | token = cp_parser_require (parser, CPP_KEYWORD, "class-key"); | |
11824 | if (!token) | |
11825 | return none_type; | |
11826 | ||
11827 | /* Check to see if the TOKEN is a class-key. */ | |
11828 | tag_type = cp_parser_token_is_class_key (token); | |
11829 | if (!tag_type) | |
11830 | cp_parser_error (parser, "expected class-key"); | |
11831 | return tag_type; | |
11832 | } | |
11833 | ||
11834 | /* Parse an (optional) member-specification. | |
11835 | ||
11836 | member-specification: | |
11837 | member-declaration member-specification [opt] | |
11838 | access-specifier : member-specification [opt] */ | |
11839 | ||
11840 | static void | |
94edc4ab | 11841 | cp_parser_member_specification_opt (cp_parser* parser) |
a723baf1 MM |
11842 | { |
11843 | while (true) | |
11844 | { | |
11845 | cp_token *token; | |
11846 | enum rid keyword; | |
11847 | ||
11848 | /* Peek at the next token. */ | |
11849 | token = cp_lexer_peek_token (parser->lexer); | |
11850 | /* If it's a `}', or EOF then we've seen all the members. */ | |
11851 | if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF) | |
11852 | break; | |
11853 | ||
11854 | /* See if this token is a keyword. */ | |
11855 | keyword = token->keyword; | |
11856 | switch (keyword) | |
11857 | { | |
11858 | case RID_PUBLIC: | |
11859 | case RID_PROTECTED: | |
11860 | case RID_PRIVATE: | |
11861 | /* Consume the access-specifier. */ | |
11862 | cp_lexer_consume_token (parser->lexer); | |
11863 | /* Remember which access-specifier is active. */ | |
11864 | current_access_specifier = token->value; | |
11865 | /* Look for the `:'. */ | |
11866 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
11867 | break; | |
11868 | ||
11869 | default: | |
11870 | /* Otherwise, the next construction must be a | |
11871 | member-declaration. */ | |
11872 | cp_parser_member_declaration (parser); | |
a723baf1 MM |
11873 | } |
11874 | } | |
11875 | } | |
11876 | ||
11877 | /* Parse a member-declaration. | |
11878 | ||
11879 | member-declaration: | |
11880 | decl-specifier-seq [opt] member-declarator-list [opt] ; | |
11881 | function-definition ; [opt] | |
11882 | :: [opt] nested-name-specifier template [opt] unqualified-id ; | |
11883 | using-declaration | |
11884 | template-declaration | |
11885 | ||
11886 | member-declarator-list: | |
11887 | member-declarator | |
11888 | member-declarator-list , member-declarator | |
11889 | ||
11890 | member-declarator: | |
11891 | declarator pure-specifier [opt] | |
11892 | declarator constant-initializer [opt] | |
11893 | identifier [opt] : constant-expression | |
11894 | ||
11895 | GNU Extensions: | |
11896 | ||
11897 | member-declaration: | |
11898 | __extension__ member-declaration | |
11899 | ||
11900 | member-declarator: | |
11901 | declarator attributes [opt] pure-specifier [opt] | |
11902 | declarator attributes [opt] constant-initializer [opt] | |
11903 | identifier [opt] attributes [opt] : constant-expression */ | |
11904 | ||
11905 | static void | |
94edc4ab | 11906 | cp_parser_member_declaration (cp_parser* parser) |
a723baf1 MM |
11907 | { |
11908 | tree decl_specifiers; | |
11909 | tree prefix_attributes; | |
11910 | tree decl; | |
560ad596 | 11911 | int declares_class_or_enum; |
a723baf1 MM |
11912 | bool friend_p; |
11913 | cp_token *token; | |
11914 | int saved_pedantic; | |
11915 | ||
11916 | /* Check for the `__extension__' keyword. */ | |
11917 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
11918 | { | |
11919 | /* Recurse. */ | |
11920 | cp_parser_member_declaration (parser); | |
11921 | /* Restore the old value of the PEDANTIC flag. */ | |
11922 | pedantic = saved_pedantic; | |
11923 | ||
11924 | return; | |
11925 | } | |
11926 | ||
11927 | /* Check for a template-declaration. */ | |
11928 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
11929 | { | |
11930 | /* Parse the template-declaration. */ | |
11931 | cp_parser_template_declaration (parser, /*member_p=*/true); | |
11932 | ||
11933 | return; | |
11934 | } | |
11935 | ||
11936 | /* Check for a using-declaration. */ | |
11937 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING)) | |
11938 | { | |
11939 | /* Parse the using-declaration. */ | |
11940 | cp_parser_using_declaration (parser); | |
11941 | ||
11942 | return; | |
11943 | } | |
11944 | ||
11945 | /* We can't tell whether we're looking at a declaration or a | |
11946 | function-definition. */ | |
11947 | cp_parser_parse_tentatively (parser); | |
11948 | ||
11949 | /* Parse the decl-specifier-seq. */ | |
11950 | decl_specifiers | |
11951 | = cp_parser_decl_specifier_seq (parser, | |
11952 | CP_PARSER_FLAGS_OPTIONAL, | |
11953 | &prefix_attributes, | |
11954 | &declares_class_or_enum); | |
8fbc5ae7 MM |
11955 | /* Check for an invalid type-name. */ |
11956 | if (cp_parser_diagnose_invalid_type_name (parser)) | |
11957 | return; | |
a723baf1 MM |
11958 | /* If there is no declarator, then the decl-specifier-seq should |
11959 | specify a type. */ | |
11960 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
11961 | { | |
11962 | /* If there was no decl-specifier-seq, and the next token is a | |
11963 | `;', then we have something like: | |
11964 | ||
11965 | struct S { ; }; | |
11966 | ||
11967 | [class.mem] | |
11968 | ||
11969 | Each member-declaration shall declare at least one member | |
11970 | name of the class. */ | |
11971 | if (!decl_specifiers) | |
11972 | { | |
11973 | if (pedantic) | |
11974 | pedwarn ("extra semicolon"); | |
11975 | } | |
11976 | else | |
11977 | { | |
11978 | tree type; | |
11979 | ||
11980 | /* See if this declaration is a friend. */ | |
11981 | friend_p = cp_parser_friend_p (decl_specifiers); | |
11982 | /* If there were decl-specifiers, check to see if there was | |
11983 | a class-declaration. */ | |
11984 | type = check_tag_decl (decl_specifiers); | |
11985 | /* Nested classes have already been added to the class, but | |
11986 | a `friend' needs to be explicitly registered. */ | |
11987 | if (friend_p) | |
11988 | { | |
11989 | /* If the `friend' keyword was present, the friend must | |
11990 | be introduced with a class-key. */ | |
11991 | if (!declares_class_or_enum) | |
11992 | error ("a class-key must be used when declaring a friend"); | |
11993 | /* In this case: | |
11994 | ||
11995 | template <typename T> struct A { | |
11996 | friend struct A<T>::B; | |
11997 | }; | |
11998 | ||
11999 | A<T>::B will be represented by a TYPENAME_TYPE, and | |
12000 | therefore not recognized by check_tag_decl. */ | |
12001 | if (!type) | |
12002 | { | |
12003 | tree specifier; | |
12004 | ||
12005 | for (specifier = decl_specifiers; | |
12006 | specifier; | |
12007 | specifier = TREE_CHAIN (specifier)) | |
12008 | { | |
12009 | tree s = TREE_VALUE (specifier); | |
12010 | ||
c003e212 GDR |
12011 | if (TREE_CODE (s) == IDENTIFIER_NODE) |
12012 | get_global_value_if_present (s, &type); | |
a723baf1 MM |
12013 | if (TREE_CODE (s) == TYPE_DECL) |
12014 | s = TREE_TYPE (s); | |
12015 | if (TYPE_P (s)) | |
12016 | { | |
12017 | type = s; | |
12018 | break; | |
12019 | } | |
12020 | } | |
12021 | } | |
12022 | if (!type) | |
12023 | error ("friend declaration does not name a class or " | |
12024 | "function"); | |
12025 | else | |
19db77ce KL |
12026 | make_friend_class (current_class_type, type, |
12027 | /*complain=*/true); | |
a723baf1 MM |
12028 | } |
12029 | /* If there is no TYPE, an error message will already have | |
12030 | been issued. */ | |
12031 | else if (!type) | |
12032 | ; | |
12033 | /* An anonymous aggregate has to be handled specially; such | |
12034 | a declaration really declares a data member (with a | |
12035 | particular type), as opposed to a nested class. */ | |
12036 | else if (ANON_AGGR_TYPE_P (type)) | |
12037 | { | |
12038 | /* Remove constructors and such from TYPE, now that we | |
34cd5ae7 | 12039 | know it is an anonymous aggregate. */ |
a723baf1 MM |
12040 | fixup_anonymous_aggr (type); |
12041 | /* And make the corresponding data member. */ | |
12042 | decl = build_decl (FIELD_DECL, NULL_TREE, type); | |
12043 | /* Add it to the class. */ | |
12044 | finish_member_declaration (decl); | |
12045 | } | |
37d407a1 KL |
12046 | else |
12047 | cp_parser_check_access_in_redeclaration (TYPE_NAME (type)); | |
a723baf1 MM |
12048 | } |
12049 | } | |
12050 | else | |
12051 | { | |
12052 | /* See if these declarations will be friends. */ | |
12053 | friend_p = cp_parser_friend_p (decl_specifiers); | |
12054 | ||
12055 | /* Keep going until we hit the `;' at the end of the | |
12056 | declaration. */ | |
12057 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
12058 | { | |
12059 | tree attributes = NULL_TREE; | |
12060 | tree first_attribute; | |
12061 | ||
12062 | /* Peek at the next token. */ | |
12063 | token = cp_lexer_peek_token (parser->lexer); | |
12064 | ||
12065 | /* Check for a bitfield declaration. */ | |
12066 | if (token->type == CPP_COLON | |
12067 | || (token->type == CPP_NAME | |
12068 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
12069 | == CPP_COLON)) | |
12070 | { | |
12071 | tree identifier; | |
12072 | tree width; | |
12073 | ||
12074 | /* Get the name of the bitfield. Note that we cannot just | |
12075 | check TOKEN here because it may have been invalidated by | |
12076 | the call to cp_lexer_peek_nth_token above. */ | |
12077 | if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON) | |
12078 | identifier = cp_parser_identifier (parser); | |
12079 | else | |
12080 | identifier = NULL_TREE; | |
12081 | ||
12082 | /* Consume the `:' token. */ | |
12083 | cp_lexer_consume_token (parser->lexer); | |
12084 | /* Get the width of the bitfield. */ | |
14d22dd6 MM |
12085 | width |
12086 | = cp_parser_constant_expression (parser, | |
12087 | /*allow_non_constant=*/false, | |
12088 | NULL); | |
a723baf1 MM |
12089 | |
12090 | /* Look for attributes that apply to the bitfield. */ | |
12091 | attributes = cp_parser_attributes_opt (parser); | |
12092 | /* Remember which attributes are prefix attributes and | |
12093 | which are not. */ | |
12094 | first_attribute = attributes; | |
12095 | /* Combine the attributes. */ | |
12096 | attributes = chainon (prefix_attributes, attributes); | |
12097 | ||
12098 | /* Create the bitfield declaration. */ | |
12099 | decl = grokbitfield (identifier, | |
12100 | decl_specifiers, | |
12101 | width); | |
12102 | /* Apply the attributes. */ | |
12103 | cplus_decl_attributes (&decl, attributes, /*flags=*/0); | |
12104 | } | |
12105 | else | |
12106 | { | |
12107 | tree declarator; | |
12108 | tree initializer; | |
12109 | tree asm_specification; | |
7efa3e22 | 12110 | int ctor_dtor_or_conv_p; |
a723baf1 MM |
12111 | |
12112 | /* Parse the declarator. */ | |
12113 | declarator | |
62b8a44e | 12114 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
a723baf1 MM |
12115 | &ctor_dtor_or_conv_p); |
12116 | ||
12117 | /* If something went wrong parsing the declarator, make sure | |
12118 | that we at least consume some tokens. */ | |
12119 | if (declarator == error_mark_node) | |
12120 | { | |
12121 | /* Skip to the end of the statement. */ | |
12122 | cp_parser_skip_to_end_of_statement (parser); | |
12123 | break; | |
12124 | } | |
12125 | ||
560ad596 MM |
12126 | cp_parser_check_for_definition_in_return_type |
12127 | (declarator, declares_class_or_enum); | |
12128 | ||
a723baf1 MM |
12129 | /* Look for an asm-specification. */ |
12130 | asm_specification = cp_parser_asm_specification_opt (parser); | |
12131 | /* Look for attributes that apply to the declaration. */ | |
12132 | attributes = cp_parser_attributes_opt (parser); | |
12133 | /* Remember which attributes are prefix attributes and | |
12134 | which are not. */ | |
12135 | first_attribute = attributes; | |
12136 | /* Combine the attributes. */ | |
12137 | attributes = chainon (prefix_attributes, attributes); | |
12138 | ||
12139 | /* If it's an `=', then we have a constant-initializer or a | |
12140 | pure-specifier. It is not correct to parse the | |
12141 | initializer before registering the member declaration | |
12142 | since the member declaration should be in scope while | |
12143 | its initializer is processed. However, the rest of the | |
12144 | front end does not yet provide an interface that allows | |
12145 | us to handle this correctly. */ | |
12146 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
12147 | { | |
12148 | /* In [class.mem]: | |
12149 | ||
12150 | A pure-specifier shall be used only in the declaration of | |
12151 | a virtual function. | |
12152 | ||
12153 | A member-declarator can contain a constant-initializer | |
12154 | only if it declares a static member of integral or | |
12155 | enumeration type. | |
12156 | ||
12157 | Therefore, if the DECLARATOR is for a function, we look | |
12158 | for a pure-specifier; otherwise, we look for a | |
12159 | constant-initializer. When we call `grokfield', it will | |
12160 | perform more stringent semantics checks. */ | |
12161 | if (TREE_CODE (declarator) == CALL_EXPR) | |
12162 | initializer = cp_parser_pure_specifier (parser); | |
12163 | else | |
12164 | { | |
12165 | /* This declaration cannot be a function | |
12166 | definition. */ | |
12167 | cp_parser_commit_to_tentative_parse (parser); | |
12168 | /* Parse the initializer. */ | |
12169 | initializer = cp_parser_constant_initializer (parser); | |
12170 | } | |
12171 | } | |
12172 | /* Otherwise, there is no initializer. */ | |
12173 | else | |
12174 | initializer = NULL_TREE; | |
12175 | ||
12176 | /* See if we are probably looking at a function | |
12177 | definition. We are certainly not looking at at a | |
12178 | member-declarator. Calling `grokfield' has | |
12179 | side-effects, so we must not do it unless we are sure | |
12180 | that we are looking at a member-declarator. */ | |
12181 | if (cp_parser_token_starts_function_definition_p | |
12182 | (cp_lexer_peek_token (parser->lexer))) | |
12183 | decl = error_mark_node; | |
12184 | else | |
39703eb9 MM |
12185 | { |
12186 | /* Create the declaration. */ | |
ee3071ef NS |
12187 | decl = grokfield (declarator, decl_specifiers, |
12188 | initializer, asm_specification, | |
39703eb9 MM |
12189 | attributes); |
12190 | /* Any initialization must have been from a | |
12191 | constant-expression. */ | |
12192 | if (decl && TREE_CODE (decl) == VAR_DECL && initializer) | |
12193 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; | |
12194 | } | |
a723baf1 MM |
12195 | } |
12196 | ||
12197 | /* Reset PREFIX_ATTRIBUTES. */ | |
12198 | while (attributes && TREE_CHAIN (attributes) != first_attribute) | |
12199 | attributes = TREE_CHAIN (attributes); | |
12200 | if (attributes) | |
12201 | TREE_CHAIN (attributes) = NULL_TREE; | |
12202 | ||
12203 | /* If there is any qualification still in effect, clear it | |
12204 | now; we will be starting fresh with the next declarator. */ | |
12205 | parser->scope = NULL_TREE; | |
12206 | parser->qualifying_scope = NULL_TREE; | |
12207 | parser->object_scope = NULL_TREE; | |
12208 | /* If it's a `,', then there are more declarators. */ | |
12209 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
12210 | cp_lexer_consume_token (parser->lexer); | |
12211 | /* If the next token isn't a `;', then we have a parse error. */ | |
12212 | else if (cp_lexer_next_token_is_not (parser->lexer, | |
12213 | CPP_SEMICOLON)) | |
12214 | { | |
12215 | cp_parser_error (parser, "expected `;'"); | |
04c06002 | 12216 | /* Skip tokens until we find a `;'. */ |
a723baf1 MM |
12217 | cp_parser_skip_to_end_of_statement (parser); |
12218 | ||
12219 | break; | |
12220 | } | |
12221 | ||
12222 | if (decl) | |
12223 | { | |
12224 | /* Add DECL to the list of members. */ | |
12225 | if (!friend_p) | |
12226 | finish_member_declaration (decl); | |
12227 | ||
a723baf1 | 12228 | if (TREE_CODE (decl) == FUNCTION_DECL) |
8db1028e | 12229 | cp_parser_save_default_args (parser, decl); |
a723baf1 MM |
12230 | } |
12231 | } | |
12232 | } | |
12233 | ||
12234 | /* If everything went well, look for the `;'. */ | |
12235 | if (cp_parser_parse_definitely (parser)) | |
12236 | { | |
12237 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
12238 | return; | |
12239 | } | |
12240 | ||
12241 | /* Parse the function-definition. */ | |
12242 | decl = cp_parser_function_definition (parser, &friend_p); | |
12243 | /* If the member was not a friend, declare it here. */ | |
12244 | if (!friend_p) | |
12245 | finish_member_declaration (decl); | |
12246 | /* Peek at the next token. */ | |
12247 | token = cp_lexer_peek_token (parser->lexer); | |
12248 | /* If the next token is a semicolon, consume it. */ | |
12249 | if (token->type == CPP_SEMICOLON) | |
12250 | cp_lexer_consume_token (parser->lexer); | |
12251 | } | |
12252 | ||
12253 | /* Parse a pure-specifier. | |
12254 | ||
12255 | pure-specifier: | |
12256 | = 0 | |
12257 | ||
12258 | Returns INTEGER_ZERO_NODE if a pure specifier is found. | |
cd0be382 | 12259 | Otherwise, ERROR_MARK_NODE is returned. */ |
a723baf1 MM |
12260 | |
12261 | static tree | |
94edc4ab | 12262 | cp_parser_pure_specifier (cp_parser* parser) |
a723baf1 MM |
12263 | { |
12264 | cp_token *token; | |
12265 | ||
12266 | /* Look for the `=' token. */ | |
12267 | if (!cp_parser_require (parser, CPP_EQ, "`='")) | |
12268 | return error_mark_node; | |
12269 | /* Look for the `0' token. */ | |
12270 | token = cp_parser_require (parser, CPP_NUMBER, "`0'"); | |
12271 | /* Unfortunately, this will accept `0L' and `0x00' as well. We need | |
12272 | to get information from the lexer about how the number was | |
12273 | spelled in order to fix this problem. */ | |
12274 | if (!token || !integer_zerop (token->value)) | |
12275 | return error_mark_node; | |
12276 | ||
12277 | return integer_zero_node; | |
12278 | } | |
12279 | ||
12280 | /* Parse a constant-initializer. | |
12281 | ||
12282 | constant-initializer: | |
12283 | = constant-expression | |
12284 | ||
12285 | Returns a representation of the constant-expression. */ | |
12286 | ||
12287 | static tree | |
94edc4ab | 12288 | cp_parser_constant_initializer (cp_parser* parser) |
a723baf1 MM |
12289 | { |
12290 | /* Look for the `=' token. */ | |
12291 | if (!cp_parser_require (parser, CPP_EQ, "`='")) | |
12292 | return error_mark_node; | |
12293 | ||
12294 | /* It is invalid to write: | |
12295 | ||
12296 | struct S { static const int i = { 7 }; }; | |
12297 | ||
12298 | */ | |
12299 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
12300 | { | |
12301 | cp_parser_error (parser, | |
12302 | "a brace-enclosed initializer is not allowed here"); | |
12303 | /* Consume the opening brace. */ | |
12304 | cp_lexer_consume_token (parser->lexer); | |
12305 | /* Skip the initializer. */ | |
12306 | cp_parser_skip_to_closing_brace (parser); | |
12307 | /* Look for the trailing `}'. */ | |
12308 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
12309 | ||
12310 | return error_mark_node; | |
12311 | } | |
12312 | ||
14d22dd6 MM |
12313 | return cp_parser_constant_expression (parser, |
12314 | /*allow_non_constant=*/false, | |
12315 | NULL); | |
a723baf1 MM |
12316 | } |
12317 | ||
12318 | /* Derived classes [gram.class.derived] */ | |
12319 | ||
12320 | /* Parse a base-clause. | |
12321 | ||
12322 | base-clause: | |
12323 | : base-specifier-list | |
12324 | ||
12325 | base-specifier-list: | |
12326 | base-specifier | |
12327 | base-specifier-list , base-specifier | |
12328 | ||
12329 | Returns a TREE_LIST representing the base-classes, in the order in | |
12330 | which they were declared. The representation of each node is as | |
12331 | described by cp_parser_base_specifier. | |
12332 | ||
12333 | In the case that no bases are specified, this function will return | |
12334 | NULL_TREE, not ERROR_MARK_NODE. */ | |
12335 | ||
12336 | static tree | |
94edc4ab | 12337 | cp_parser_base_clause (cp_parser* parser) |
a723baf1 MM |
12338 | { |
12339 | tree bases = NULL_TREE; | |
12340 | ||
12341 | /* Look for the `:' that begins the list. */ | |
12342 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
12343 | ||
12344 | /* Scan the base-specifier-list. */ | |
12345 | while (true) | |
12346 | { | |
12347 | cp_token *token; | |
12348 | tree base; | |
12349 | ||
12350 | /* Look for the base-specifier. */ | |
12351 | base = cp_parser_base_specifier (parser); | |
12352 | /* Add BASE to the front of the list. */ | |
12353 | if (base != error_mark_node) | |
12354 | { | |
12355 | TREE_CHAIN (base) = bases; | |
12356 | bases = base; | |
12357 | } | |
12358 | /* Peek at the next token. */ | |
12359 | token = cp_lexer_peek_token (parser->lexer); | |
12360 | /* If it's not a comma, then the list is complete. */ | |
12361 | if (token->type != CPP_COMMA) | |
12362 | break; | |
12363 | /* Consume the `,'. */ | |
12364 | cp_lexer_consume_token (parser->lexer); | |
12365 | } | |
12366 | ||
12367 | /* PARSER->SCOPE may still be non-NULL at this point, if the last | |
12368 | base class had a qualified name. However, the next name that | |
12369 | appears is certainly not qualified. */ | |
12370 | parser->scope = NULL_TREE; | |
12371 | parser->qualifying_scope = NULL_TREE; | |
12372 | parser->object_scope = NULL_TREE; | |
12373 | ||
12374 | return nreverse (bases); | |
12375 | } | |
12376 | ||
12377 | /* Parse a base-specifier. | |
12378 | ||
12379 | base-specifier: | |
12380 | :: [opt] nested-name-specifier [opt] class-name | |
12381 | virtual access-specifier [opt] :: [opt] nested-name-specifier | |
12382 | [opt] class-name | |
12383 | access-specifier virtual [opt] :: [opt] nested-name-specifier | |
12384 | [opt] class-name | |
12385 | ||
12386 | Returns a TREE_LIST. The TREE_PURPOSE will be one of | |
12387 | ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to | |
12388 | indicate the specifiers provided. The TREE_VALUE will be a TYPE | |
12389 | (or the ERROR_MARK_NODE) indicating the type that was specified. */ | |
12390 | ||
12391 | static tree | |
94edc4ab | 12392 | cp_parser_base_specifier (cp_parser* parser) |
a723baf1 MM |
12393 | { |
12394 | cp_token *token; | |
12395 | bool done = false; | |
12396 | bool virtual_p = false; | |
12397 | bool duplicate_virtual_error_issued_p = false; | |
12398 | bool duplicate_access_error_issued_p = false; | |
bbaab916 | 12399 | bool class_scope_p, template_p; |
dbbf88d1 | 12400 | tree access = access_default_node; |
a723baf1 MM |
12401 | tree type; |
12402 | ||
12403 | /* Process the optional `virtual' and `access-specifier'. */ | |
12404 | while (!done) | |
12405 | { | |
12406 | /* Peek at the next token. */ | |
12407 | token = cp_lexer_peek_token (parser->lexer); | |
12408 | /* Process `virtual'. */ | |
12409 | switch (token->keyword) | |
12410 | { | |
12411 | case RID_VIRTUAL: | |
12412 | /* If `virtual' appears more than once, issue an error. */ | |
12413 | if (virtual_p && !duplicate_virtual_error_issued_p) | |
12414 | { | |
12415 | cp_parser_error (parser, | |
12416 | "`virtual' specified more than once in base-specified"); | |
12417 | duplicate_virtual_error_issued_p = true; | |
12418 | } | |
12419 | ||
12420 | virtual_p = true; | |
12421 | ||
12422 | /* Consume the `virtual' token. */ | |
12423 | cp_lexer_consume_token (parser->lexer); | |
12424 | ||
12425 | break; | |
12426 | ||
12427 | case RID_PUBLIC: | |
12428 | case RID_PROTECTED: | |
12429 | case RID_PRIVATE: | |
12430 | /* If more than one access specifier appears, issue an | |
12431 | error. */ | |
dbbf88d1 NS |
12432 | if (access != access_default_node |
12433 | && !duplicate_access_error_issued_p) | |
a723baf1 MM |
12434 | { |
12435 | cp_parser_error (parser, | |
12436 | "more than one access specifier in base-specified"); | |
12437 | duplicate_access_error_issued_p = true; | |
12438 | } | |
12439 | ||
dbbf88d1 | 12440 | access = ridpointers[(int) token->keyword]; |
a723baf1 MM |
12441 | |
12442 | /* Consume the access-specifier. */ | |
12443 | cp_lexer_consume_token (parser->lexer); | |
12444 | ||
12445 | break; | |
12446 | ||
12447 | default: | |
12448 | done = true; | |
12449 | break; | |
12450 | } | |
12451 | } | |
12452 | ||
a723baf1 MM |
12453 | /* Look for the optional `::' operator. */ |
12454 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); | |
12455 | /* Look for the nested-name-specifier. The simplest way to | |
12456 | implement: | |
12457 | ||
12458 | [temp.res] | |
12459 | ||
12460 | The keyword `typename' is not permitted in a base-specifier or | |
12461 | mem-initializer; in these contexts a qualified name that | |
12462 | depends on a template-parameter is implicitly assumed to be a | |
12463 | type name. | |
12464 | ||
12465 | is to pretend that we have seen the `typename' keyword at this | |
12466 | point. */ | |
12467 | cp_parser_nested_name_specifier_opt (parser, | |
12468 | /*typename_keyword_p=*/true, | |
12469 | /*check_dependency_p=*/true, | |
a668c6ad MM |
12470 | /*type_p=*/true, |
12471 | /*is_declaration=*/true); | |
a723baf1 MM |
12472 | /* If the base class is given by a qualified name, assume that names |
12473 | we see are type names or templates, as appropriate. */ | |
12474 | class_scope_p = (parser->scope && TYPE_P (parser->scope)); | |
bbaab916 NS |
12475 | template_p = class_scope_p && cp_parser_optional_template_keyword (parser); |
12476 | ||
a723baf1 MM |
12477 | /* Finally, look for the class-name. */ |
12478 | type = cp_parser_class_name (parser, | |
12479 | class_scope_p, | |
bbaab916 | 12480 | template_p, |
a723baf1 | 12481 | /*type_p=*/true, |
a723baf1 | 12482 | /*check_dependency_p=*/true, |
a668c6ad MM |
12483 | /*class_head_p=*/false, |
12484 | /*is_declaration=*/true); | |
a723baf1 MM |
12485 | |
12486 | if (type == error_mark_node) | |
12487 | return error_mark_node; | |
12488 | ||
dbbf88d1 | 12489 | return finish_base_specifier (TREE_TYPE (type), access, virtual_p); |
a723baf1 MM |
12490 | } |
12491 | ||
12492 | /* Exception handling [gram.exception] */ | |
12493 | ||
12494 | /* Parse an (optional) exception-specification. | |
12495 | ||
12496 | exception-specification: | |
12497 | throw ( type-id-list [opt] ) | |
12498 | ||
12499 | Returns a TREE_LIST representing the exception-specification. The | |
12500 | TREE_VALUE of each node is a type. */ | |
12501 | ||
12502 | static tree | |
94edc4ab | 12503 | cp_parser_exception_specification_opt (cp_parser* parser) |
a723baf1 MM |
12504 | { |
12505 | cp_token *token; | |
12506 | tree type_id_list; | |
12507 | ||
12508 | /* Peek at the next token. */ | |
12509 | token = cp_lexer_peek_token (parser->lexer); | |
12510 | /* If it's not `throw', then there's no exception-specification. */ | |
12511 | if (!cp_parser_is_keyword (token, RID_THROW)) | |
12512 | return NULL_TREE; | |
12513 | ||
12514 | /* Consume the `throw'. */ | |
12515 | cp_lexer_consume_token (parser->lexer); | |
12516 | ||
12517 | /* Look for the `('. */ | |
12518 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
12519 | ||
12520 | /* Peek at the next token. */ | |
12521 | token = cp_lexer_peek_token (parser->lexer); | |
12522 | /* If it's not a `)', then there is a type-id-list. */ | |
12523 | if (token->type != CPP_CLOSE_PAREN) | |
12524 | { | |
12525 | const char *saved_message; | |
12526 | ||
12527 | /* Types may not be defined in an exception-specification. */ | |
12528 | saved_message = parser->type_definition_forbidden_message; | |
12529 | parser->type_definition_forbidden_message | |
12530 | = "types may not be defined in an exception-specification"; | |
12531 | /* Parse the type-id-list. */ | |
12532 | type_id_list = cp_parser_type_id_list (parser); | |
12533 | /* Restore the saved message. */ | |
12534 | parser->type_definition_forbidden_message = saved_message; | |
12535 | } | |
12536 | else | |
12537 | type_id_list = empty_except_spec; | |
12538 | ||
12539 | /* Look for the `)'. */ | |
12540 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
12541 | ||
12542 | return type_id_list; | |
12543 | } | |
12544 | ||
12545 | /* Parse an (optional) type-id-list. | |
12546 | ||
12547 | type-id-list: | |
12548 | type-id | |
12549 | type-id-list , type-id | |
12550 | ||
12551 | Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE, | |
12552 | in the order that the types were presented. */ | |
12553 | ||
12554 | static tree | |
94edc4ab | 12555 | cp_parser_type_id_list (cp_parser* parser) |
a723baf1 MM |
12556 | { |
12557 | tree types = NULL_TREE; | |
12558 | ||
12559 | while (true) | |
12560 | { | |
12561 | cp_token *token; | |
12562 | tree type; | |
12563 | ||
12564 | /* Get the next type-id. */ | |
12565 | type = cp_parser_type_id (parser); | |
12566 | /* Add it to the list. */ | |
12567 | types = add_exception_specifier (types, type, /*complain=*/1); | |
12568 | /* Peek at the next token. */ | |
12569 | token = cp_lexer_peek_token (parser->lexer); | |
12570 | /* If it is not a `,', we are done. */ | |
12571 | if (token->type != CPP_COMMA) | |
12572 | break; | |
12573 | /* Consume the `,'. */ | |
12574 | cp_lexer_consume_token (parser->lexer); | |
12575 | } | |
12576 | ||
12577 | return nreverse (types); | |
12578 | } | |
12579 | ||
12580 | /* Parse a try-block. | |
12581 | ||
12582 | try-block: | |
12583 | try compound-statement handler-seq */ | |
12584 | ||
12585 | static tree | |
94edc4ab | 12586 | cp_parser_try_block (cp_parser* parser) |
a723baf1 MM |
12587 | { |
12588 | tree try_block; | |
12589 | ||
12590 | cp_parser_require_keyword (parser, RID_TRY, "`try'"); | |
12591 | try_block = begin_try_block (); | |
a5bcc582 | 12592 | cp_parser_compound_statement (parser, false); |
a723baf1 MM |
12593 | finish_try_block (try_block); |
12594 | cp_parser_handler_seq (parser); | |
12595 | finish_handler_sequence (try_block); | |
12596 | ||
12597 | return try_block; | |
12598 | } | |
12599 | ||
12600 | /* Parse a function-try-block. | |
12601 | ||
12602 | function-try-block: | |
12603 | try ctor-initializer [opt] function-body handler-seq */ | |
12604 | ||
12605 | static bool | |
94edc4ab | 12606 | cp_parser_function_try_block (cp_parser* parser) |
a723baf1 MM |
12607 | { |
12608 | tree try_block; | |
12609 | bool ctor_initializer_p; | |
12610 | ||
12611 | /* Look for the `try' keyword. */ | |
12612 | if (!cp_parser_require_keyword (parser, RID_TRY, "`try'")) | |
12613 | return false; | |
12614 | /* Let the rest of the front-end know where we are. */ | |
12615 | try_block = begin_function_try_block (); | |
12616 | /* Parse the function-body. */ | |
12617 | ctor_initializer_p | |
12618 | = cp_parser_ctor_initializer_opt_and_function_body (parser); | |
12619 | /* We're done with the `try' part. */ | |
12620 | finish_function_try_block (try_block); | |
12621 | /* Parse the handlers. */ | |
12622 | cp_parser_handler_seq (parser); | |
12623 | /* We're done with the handlers. */ | |
12624 | finish_function_handler_sequence (try_block); | |
12625 | ||
12626 | return ctor_initializer_p; | |
12627 | } | |
12628 | ||
12629 | /* Parse a handler-seq. | |
12630 | ||
12631 | handler-seq: | |
12632 | handler handler-seq [opt] */ | |
12633 | ||
12634 | static void | |
94edc4ab | 12635 | cp_parser_handler_seq (cp_parser* parser) |
a723baf1 MM |
12636 | { |
12637 | while (true) | |
12638 | { | |
12639 | cp_token *token; | |
12640 | ||
12641 | /* Parse the handler. */ | |
12642 | cp_parser_handler (parser); | |
12643 | /* Peek at the next token. */ | |
12644 | token = cp_lexer_peek_token (parser->lexer); | |
12645 | /* If it's not `catch' then there are no more handlers. */ | |
12646 | if (!cp_parser_is_keyword (token, RID_CATCH)) | |
12647 | break; | |
12648 | } | |
12649 | } | |
12650 | ||
12651 | /* Parse a handler. | |
12652 | ||
12653 | handler: | |
12654 | catch ( exception-declaration ) compound-statement */ | |
12655 | ||
12656 | static void | |
94edc4ab | 12657 | cp_parser_handler (cp_parser* parser) |
a723baf1 MM |
12658 | { |
12659 | tree handler; | |
12660 | tree declaration; | |
12661 | ||
12662 | cp_parser_require_keyword (parser, RID_CATCH, "`catch'"); | |
12663 | handler = begin_handler (); | |
12664 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
12665 | declaration = cp_parser_exception_declaration (parser); | |
12666 | finish_handler_parms (declaration, handler); | |
12667 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
a5bcc582 | 12668 | cp_parser_compound_statement (parser, false); |
a723baf1 MM |
12669 | finish_handler (handler); |
12670 | } | |
12671 | ||
12672 | /* Parse an exception-declaration. | |
12673 | ||
12674 | exception-declaration: | |
12675 | type-specifier-seq declarator | |
12676 | type-specifier-seq abstract-declarator | |
12677 | type-specifier-seq | |
12678 | ... | |
12679 | ||
12680 | Returns a VAR_DECL for the declaration, or NULL_TREE if the | |
12681 | ellipsis variant is used. */ | |
12682 | ||
12683 | static tree | |
94edc4ab | 12684 | cp_parser_exception_declaration (cp_parser* parser) |
a723baf1 MM |
12685 | { |
12686 | tree type_specifiers; | |
12687 | tree declarator; | |
12688 | const char *saved_message; | |
12689 | ||
12690 | /* If it's an ellipsis, it's easy to handle. */ | |
12691 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
12692 | { | |
12693 | /* Consume the `...' token. */ | |
12694 | cp_lexer_consume_token (parser->lexer); | |
12695 | return NULL_TREE; | |
12696 | } | |
12697 | ||
12698 | /* Types may not be defined in exception-declarations. */ | |
12699 | saved_message = parser->type_definition_forbidden_message; | |
12700 | parser->type_definition_forbidden_message | |
12701 | = "types may not be defined in exception-declarations"; | |
12702 | ||
12703 | /* Parse the type-specifier-seq. */ | |
12704 | type_specifiers = cp_parser_type_specifier_seq (parser); | |
12705 | /* If it's a `)', then there is no declarator. */ | |
12706 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)) | |
12707 | declarator = NULL_TREE; | |
12708 | else | |
62b8a44e NS |
12709 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER, |
12710 | /*ctor_dtor_or_conv_p=*/NULL); | |
a723baf1 MM |
12711 | |
12712 | /* Restore the saved message. */ | |
12713 | parser->type_definition_forbidden_message = saved_message; | |
12714 | ||
12715 | return start_handler_parms (type_specifiers, declarator); | |
12716 | } | |
12717 | ||
12718 | /* Parse a throw-expression. | |
12719 | ||
12720 | throw-expression: | |
34cd5ae7 | 12721 | throw assignment-expression [opt] |
a723baf1 MM |
12722 | |
12723 | Returns a THROW_EXPR representing the throw-expression. */ | |
12724 | ||
12725 | static tree | |
94edc4ab | 12726 | cp_parser_throw_expression (cp_parser* parser) |
a723baf1 MM |
12727 | { |
12728 | tree expression; | |
12729 | ||
12730 | cp_parser_require_keyword (parser, RID_THROW, "`throw'"); | |
12731 | /* We can't be sure if there is an assignment-expression or not. */ | |
12732 | cp_parser_parse_tentatively (parser); | |
12733 | /* Try it. */ | |
12734 | expression = cp_parser_assignment_expression (parser); | |
12735 | /* If it didn't work, this is just a rethrow. */ | |
12736 | if (!cp_parser_parse_definitely (parser)) | |
12737 | expression = NULL_TREE; | |
12738 | ||
12739 | return build_throw (expression); | |
12740 | } | |
12741 | ||
12742 | /* GNU Extensions */ | |
12743 | ||
12744 | /* Parse an (optional) asm-specification. | |
12745 | ||
12746 | asm-specification: | |
12747 | asm ( string-literal ) | |
12748 | ||
12749 | If the asm-specification is present, returns a STRING_CST | |
12750 | corresponding to the string-literal. Otherwise, returns | |
12751 | NULL_TREE. */ | |
12752 | ||
12753 | static tree | |
94edc4ab | 12754 | cp_parser_asm_specification_opt (cp_parser* parser) |
a723baf1 MM |
12755 | { |
12756 | cp_token *token; | |
12757 | tree asm_specification; | |
12758 | ||
12759 | /* Peek at the next token. */ | |
12760 | token = cp_lexer_peek_token (parser->lexer); | |
12761 | /* If the next token isn't the `asm' keyword, then there's no | |
12762 | asm-specification. */ | |
12763 | if (!cp_parser_is_keyword (token, RID_ASM)) | |
12764 | return NULL_TREE; | |
12765 | ||
12766 | /* Consume the `asm' token. */ | |
12767 | cp_lexer_consume_token (parser->lexer); | |
12768 | /* Look for the `('. */ | |
12769 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
12770 | ||
12771 | /* Look for the string-literal. */ | |
12772 | token = cp_parser_require (parser, CPP_STRING, "string-literal"); | |
12773 | if (token) | |
12774 | asm_specification = token->value; | |
12775 | else | |
12776 | asm_specification = NULL_TREE; | |
12777 | ||
12778 | /* Look for the `)'. */ | |
12779 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`('"); | |
12780 | ||
12781 | return asm_specification; | |
12782 | } | |
12783 | ||
12784 | /* Parse an asm-operand-list. | |
12785 | ||
12786 | asm-operand-list: | |
12787 | asm-operand | |
12788 | asm-operand-list , asm-operand | |
12789 | ||
12790 | asm-operand: | |
12791 | string-literal ( expression ) | |
12792 | [ string-literal ] string-literal ( expression ) | |
12793 | ||
12794 | Returns a TREE_LIST representing the operands. The TREE_VALUE of | |
12795 | each node is the expression. The TREE_PURPOSE is itself a | |
12796 | TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed | |
12797 | string-literal (or NULL_TREE if not present) and whose TREE_VALUE | |
12798 | is a STRING_CST for the string literal before the parenthesis. */ | |
12799 | ||
12800 | static tree | |
94edc4ab | 12801 | cp_parser_asm_operand_list (cp_parser* parser) |
a723baf1 MM |
12802 | { |
12803 | tree asm_operands = NULL_TREE; | |
12804 | ||
12805 | while (true) | |
12806 | { | |
12807 | tree string_literal; | |
12808 | tree expression; | |
12809 | tree name; | |
12810 | cp_token *token; | |
12811 | ||
12812 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
12813 | { | |
12814 | /* Consume the `[' token. */ | |
12815 | cp_lexer_consume_token (parser->lexer); | |
12816 | /* Read the operand name. */ | |
12817 | name = cp_parser_identifier (parser); | |
12818 | if (name != error_mark_node) | |
12819 | name = build_string (IDENTIFIER_LENGTH (name), | |
12820 | IDENTIFIER_POINTER (name)); | |
12821 | /* Look for the closing `]'. */ | |
12822 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
12823 | } | |
12824 | else | |
12825 | name = NULL_TREE; | |
12826 | /* Look for the string-literal. */ | |
12827 | token = cp_parser_require (parser, CPP_STRING, "string-literal"); | |
12828 | string_literal = token ? token->value : error_mark_node; | |
12829 | /* Look for the `('. */ | |
12830 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
12831 | /* Parse the expression. */ | |
12832 | expression = cp_parser_expression (parser); | |
12833 | /* Look for the `)'. */ | |
12834 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
12835 | /* Add this operand to the list. */ | |
12836 | asm_operands = tree_cons (build_tree_list (name, string_literal), | |
12837 | expression, | |
12838 | asm_operands); | |
12839 | /* If the next token is not a `,', there are no more | |
12840 | operands. */ | |
12841 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
12842 | break; | |
12843 | /* Consume the `,'. */ | |
12844 | cp_lexer_consume_token (parser->lexer); | |
12845 | } | |
12846 | ||
12847 | return nreverse (asm_operands); | |
12848 | } | |
12849 | ||
12850 | /* Parse an asm-clobber-list. | |
12851 | ||
12852 | asm-clobber-list: | |
12853 | string-literal | |
12854 | asm-clobber-list , string-literal | |
12855 | ||
12856 | Returns a TREE_LIST, indicating the clobbers in the order that they | |
12857 | appeared. The TREE_VALUE of each node is a STRING_CST. */ | |
12858 | ||
12859 | static tree | |
94edc4ab | 12860 | cp_parser_asm_clobber_list (cp_parser* parser) |
a723baf1 MM |
12861 | { |
12862 | tree clobbers = NULL_TREE; | |
12863 | ||
12864 | while (true) | |
12865 | { | |
12866 | cp_token *token; | |
12867 | tree string_literal; | |
12868 | ||
12869 | /* Look for the string literal. */ | |
12870 | token = cp_parser_require (parser, CPP_STRING, "string-literal"); | |
12871 | string_literal = token ? token->value : error_mark_node; | |
12872 | /* Add it to the list. */ | |
12873 | clobbers = tree_cons (NULL_TREE, string_literal, clobbers); | |
12874 | /* If the next token is not a `,', then the list is | |
12875 | complete. */ | |
12876 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
12877 | break; | |
12878 | /* Consume the `,' token. */ | |
12879 | cp_lexer_consume_token (parser->lexer); | |
12880 | } | |
12881 | ||
12882 | return clobbers; | |
12883 | } | |
12884 | ||
12885 | /* Parse an (optional) series of attributes. | |
12886 | ||
12887 | attributes: | |
12888 | attributes attribute | |
12889 | ||
12890 | attribute: | |
12891 | __attribute__ (( attribute-list [opt] )) | |
12892 | ||
12893 | The return value is as for cp_parser_attribute_list. */ | |
12894 | ||
12895 | static tree | |
94edc4ab | 12896 | cp_parser_attributes_opt (cp_parser* parser) |
a723baf1 MM |
12897 | { |
12898 | tree attributes = NULL_TREE; | |
12899 | ||
12900 | while (true) | |
12901 | { | |
12902 | cp_token *token; | |
12903 | tree attribute_list; | |
12904 | ||
12905 | /* Peek at the next token. */ | |
12906 | token = cp_lexer_peek_token (parser->lexer); | |
12907 | /* If it's not `__attribute__', then we're done. */ | |
12908 | if (token->keyword != RID_ATTRIBUTE) | |
12909 | break; | |
12910 | ||
12911 | /* Consume the `__attribute__' keyword. */ | |
12912 | cp_lexer_consume_token (parser->lexer); | |
12913 | /* Look for the two `(' tokens. */ | |
12914 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
12915 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
12916 | ||
12917 | /* Peek at the next token. */ | |
12918 | token = cp_lexer_peek_token (parser->lexer); | |
12919 | if (token->type != CPP_CLOSE_PAREN) | |
12920 | /* Parse the attribute-list. */ | |
12921 | attribute_list = cp_parser_attribute_list (parser); | |
12922 | else | |
12923 | /* If the next token is a `)', then there is no attribute | |
12924 | list. */ | |
12925 | attribute_list = NULL; | |
12926 | ||
12927 | /* Look for the two `)' tokens. */ | |
12928 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
12929 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
12930 | ||
12931 | /* Add these new attributes to the list. */ | |
12932 | attributes = chainon (attributes, attribute_list); | |
12933 | } | |
12934 | ||
12935 | return attributes; | |
12936 | } | |
12937 | ||
12938 | /* Parse an attribute-list. | |
12939 | ||
12940 | attribute-list: | |
12941 | attribute | |
12942 | attribute-list , attribute | |
12943 | ||
12944 | attribute: | |
12945 | identifier | |
12946 | identifier ( identifier ) | |
12947 | identifier ( identifier , expression-list ) | |
12948 | identifier ( expression-list ) | |
12949 | ||
12950 | Returns a TREE_LIST. Each node corresponds to an attribute. THe | |
12951 | TREE_PURPOSE of each node is the identifier indicating which | |
12952 | attribute is in use. The TREE_VALUE represents the arguments, if | |
12953 | any. */ | |
12954 | ||
12955 | static tree | |
94edc4ab | 12956 | cp_parser_attribute_list (cp_parser* parser) |
a723baf1 MM |
12957 | { |
12958 | tree attribute_list = NULL_TREE; | |
12959 | ||
12960 | while (true) | |
12961 | { | |
12962 | cp_token *token; | |
12963 | tree identifier; | |
12964 | tree attribute; | |
12965 | ||
12966 | /* Look for the identifier. We also allow keywords here; for | |
12967 | example `__attribute__ ((const))' is legal. */ | |
12968 | token = cp_lexer_peek_token (parser->lexer); | |
12969 | if (token->type != CPP_NAME | |
12970 | && token->type != CPP_KEYWORD) | |
12971 | return error_mark_node; | |
12972 | /* Consume the token. */ | |
12973 | token = cp_lexer_consume_token (parser->lexer); | |
12974 | ||
12975 | /* Save away the identifier that indicates which attribute this is. */ | |
12976 | identifier = token->value; | |
12977 | attribute = build_tree_list (identifier, NULL_TREE); | |
12978 | ||
12979 | /* Peek at the next token. */ | |
12980 | token = cp_lexer_peek_token (parser->lexer); | |
12981 | /* If it's an `(', then parse the attribute arguments. */ | |
12982 | if (token->type == CPP_OPEN_PAREN) | |
12983 | { | |
12984 | tree arguments; | |
a723baf1 | 12985 | |
39703eb9 MM |
12986 | arguments = (cp_parser_parenthesized_expression_list |
12987 | (parser, true, /*non_constant_p=*/NULL)); | |
a723baf1 MM |
12988 | /* Save the identifier and arguments away. */ |
12989 | TREE_VALUE (attribute) = arguments; | |
a723baf1 MM |
12990 | } |
12991 | ||
12992 | /* Add this attribute to the list. */ | |
12993 | TREE_CHAIN (attribute) = attribute_list; | |
12994 | attribute_list = attribute; | |
12995 | ||
12996 | /* Now, look for more attributes. */ | |
12997 | token = cp_lexer_peek_token (parser->lexer); | |
12998 | /* If the next token isn't a `,', we're done. */ | |
12999 | if (token->type != CPP_COMMA) | |
13000 | break; | |
13001 | ||
cd0be382 | 13002 | /* Consume the comma and keep going. */ |
a723baf1 MM |
13003 | cp_lexer_consume_token (parser->lexer); |
13004 | } | |
13005 | ||
13006 | /* We built up the list in reverse order. */ | |
13007 | return nreverse (attribute_list); | |
13008 | } | |
13009 | ||
13010 | /* Parse an optional `__extension__' keyword. Returns TRUE if it is | |
13011 | present, and FALSE otherwise. *SAVED_PEDANTIC is set to the | |
13012 | current value of the PEDANTIC flag, regardless of whether or not | |
13013 | the `__extension__' keyword is present. The caller is responsible | |
13014 | for restoring the value of the PEDANTIC flag. */ | |
13015 | ||
13016 | static bool | |
94edc4ab | 13017 | cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic) |
a723baf1 MM |
13018 | { |
13019 | /* Save the old value of the PEDANTIC flag. */ | |
13020 | *saved_pedantic = pedantic; | |
13021 | ||
13022 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION)) | |
13023 | { | |
13024 | /* Consume the `__extension__' token. */ | |
13025 | cp_lexer_consume_token (parser->lexer); | |
13026 | /* We're not being pedantic while the `__extension__' keyword is | |
13027 | in effect. */ | |
13028 | pedantic = 0; | |
13029 | ||
13030 | return true; | |
13031 | } | |
13032 | ||
13033 | return false; | |
13034 | } | |
13035 | ||
13036 | /* Parse a label declaration. | |
13037 | ||
13038 | label-declaration: | |
13039 | __label__ label-declarator-seq ; | |
13040 | ||
13041 | label-declarator-seq: | |
13042 | identifier , label-declarator-seq | |
13043 | identifier */ | |
13044 | ||
13045 | static void | |
94edc4ab | 13046 | cp_parser_label_declaration (cp_parser* parser) |
a723baf1 MM |
13047 | { |
13048 | /* Look for the `__label__' keyword. */ | |
13049 | cp_parser_require_keyword (parser, RID_LABEL, "`__label__'"); | |
13050 | ||
13051 | while (true) | |
13052 | { | |
13053 | tree identifier; | |
13054 | ||
13055 | /* Look for an identifier. */ | |
13056 | identifier = cp_parser_identifier (parser); | |
13057 | /* Declare it as a lobel. */ | |
13058 | finish_label_decl (identifier); | |
13059 | /* If the next token is a `;', stop. */ | |
13060 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
13061 | break; | |
13062 | /* Look for the `,' separating the label declarations. */ | |
13063 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
13064 | } | |
13065 | ||
13066 | /* Look for the final `;'. */ | |
13067 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
13068 | } | |
13069 | ||
13070 | /* Support Functions */ | |
13071 | ||
13072 | /* Looks up NAME in the current scope, as given by PARSER->SCOPE. | |
13073 | NAME should have one of the representations used for an | |
13074 | id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE | |
13075 | is returned. If PARSER->SCOPE is a dependent type, then a | |
13076 | SCOPE_REF is returned. | |
13077 | ||
13078 | If NAME is a TEMPLATE_ID_EXPR, then it will be immediately | |
13079 | returned; the name was already resolved when the TEMPLATE_ID_EXPR | |
13080 | was formed. Abstractly, such entities should not be passed to this | |
13081 | function, because they do not need to be looked up, but it is | |
13082 | simpler to check for this special case here, rather than at the | |
13083 | call-sites. | |
13084 | ||
13085 | In cases not explicitly covered above, this function returns a | |
13086 | DECL, OVERLOAD, or baselink representing the result of the lookup. | |
13087 | If there was no entity with the indicated NAME, the ERROR_MARK_NODE | |
13088 | is returned. | |
13089 | ||
a723baf1 MM |
13090 | If IS_TYPE is TRUE, bindings that do not refer to types are |
13091 | ignored. | |
13092 | ||
eea9800f MM |
13093 | If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces |
13094 | are ignored. | |
13095 | ||
a723baf1 MM |
13096 | If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent |
13097 | types. */ | |
13098 | ||
13099 | static tree | |
8d241e0b | 13100 | cp_parser_lookup_name (cp_parser *parser, tree name, |
eea9800f | 13101 | bool is_type, bool is_namespace, bool check_dependency) |
a723baf1 MM |
13102 | { |
13103 | tree decl; | |
13104 | tree object_type = parser->context->object_type; | |
13105 | ||
13106 | /* Now that we have looked up the name, the OBJECT_TYPE (if any) is | |
13107 | no longer valid. Note that if we are parsing tentatively, and | |
13108 | the parse fails, OBJECT_TYPE will be automatically restored. */ | |
13109 | parser->context->object_type = NULL_TREE; | |
13110 | ||
13111 | if (name == error_mark_node) | |
13112 | return error_mark_node; | |
13113 | ||
13114 | /* A template-id has already been resolved; there is no lookup to | |
13115 | do. */ | |
13116 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
13117 | return name; | |
13118 | if (BASELINK_P (name)) | |
13119 | { | |
13120 | my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name)) | |
13121 | == TEMPLATE_ID_EXPR), | |
13122 | 20020909); | |
13123 | return name; | |
13124 | } | |
13125 | ||
13126 | /* A BIT_NOT_EXPR is used to represent a destructor. By this point, | |
13127 | it should already have been checked to make sure that the name | |
13128 | used matches the type being destroyed. */ | |
13129 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
13130 | { | |
13131 | tree type; | |
13132 | ||
13133 | /* Figure out to which type this destructor applies. */ | |
13134 | if (parser->scope) | |
13135 | type = parser->scope; | |
13136 | else if (object_type) | |
13137 | type = object_type; | |
13138 | else | |
13139 | type = current_class_type; | |
13140 | /* If that's not a class type, there is no destructor. */ | |
13141 | if (!type || !CLASS_TYPE_P (type)) | |
13142 | return error_mark_node; | |
13143 | /* If it was a class type, return the destructor. */ | |
13144 | return CLASSTYPE_DESTRUCTORS (type); | |
13145 | } | |
13146 | ||
13147 | /* By this point, the NAME should be an ordinary identifier. If | |
13148 | the id-expression was a qualified name, the qualifying scope is | |
13149 | stored in PARSER->SCOPE at this point. */ | |
13150 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, | |
13151 | 20000619); | |
13152 | ||
13153 | /* Perform the lookup. */ | |
13154 | if (parser->scope) | |
13155 | { | |
1fb3244a | 13156 | bool dependent_p; |
a723baf1 MM |
13157 | |
13158 | if (parser->scope == error_mark_node) | |
13159 | return error_mark_node; | |
13160 | ||
13161 | /* If the SCOPE is dependent, the lookup must be deferred until | |
13162 | the template is instantiated -- unless we are explicitly | |
13163 | looking up names in uninstantiated templates. Even then, we | |
13164 | cannot look up the name if the scope is not a class type; it | |
13165 | might, for example, be a template type parameter. */ | |
1fb3244a MM |
13166 | dependent_p = (TYPE_P (parser->scope) |
13167 | && !(parser->in_declarator_p | |
13168 | && currently_open_class (parser->scope)) | |
13169 | && dependent_type_p (parser->scope)); | |
a723baf1 | 13170 | if ((check_dependency || !CLASS_TYPE_P (parser->scope)) |
1fb3244a | 13171 | && dependent_p) |
a723baf1 MM |
13172 | { |
13173 | if (!is_type) | |
13174 | decl = build_nt (SCOPE_REF, parser->scope, name); | |
13175 | else | |
13176 | /* The resolution to Core Issue 180 says that `struct A::B' | |
13177 | should be considered a type-name, even if `A' is | |
13178 | dependent. */ | |
13179 | decl = TYPE_NAME (make_typename_type (parser->scope, | |
13180 | name, | |
13181 | /*complain=*/1)); | |
13182 | } | |
13183 | else | |
13184 | { | |
13185 | /* If PARSER->SCOPE is a dependent type, then it must be a | |
13186 | class type, and we must not be checking dependencies; | |
13187 | otherwise, we would have processed this lookup above. So | |
13188 | that PARSER->SCOPE is not considered a dependent base by | |
13189 | lookup_member, we must enter the scope here. */ | |
1fb3244a | 13190 | if (dependent_p) |
a723baf1 MM |
13191 | push_scope (parser->scope); |
13192 | /* If the PARSER->SCOPE is a a template specialization, it | |
13193 | may be instantiated during name lookup. In that case, | |
13194 | errors may be issued. Even if we rollback the current | |
13195 | tentative parse, those errors are valid. */ | |
5e08432e MM |
13196 | decl = lookup_qualified_name (parser->scope, name, is_type, |
13197 | /*complain=*/true); | |
1fb3244a | 13198 | if (dependent_p) |
a723baf1 MM |
13199 | pop_scope (parser->scope); |
13200 | } | |
13201 | parser->qualifying_scope = parser->scope; | |
13202 | parser->object_scope = NULL_TREE; | |
13203 | } | |
13204 | else if (object_type) | |
13205 | { | |
13206 | tree object_decl = NULL_TREE; | |
13207 | /* Look up the name in the scope of the OBJECT_TYPE, unless the | |
13208 | OBJECT_TYPE is not a class. */ | |
13209 | if (CLASS_TYPE_P (object_type)) | |
13210 | /* If the OBJECT_TYPE is a template specialization, it may | |
13211 | be instantiated during name lookup. In that case, errors | |
13212 | may be issued. Even if we rollback the current tentative | |
13213 | parse, those errors are valid. */ | |
13214 | object_decl = lookup_member (object_type, | |
13215 | name, | |
13216 | /*protect=*/0, is_type); | |
13217 | /* Look it up in the enclosing context, too. */ | |
13218 | decl = lookup_name_real (name, is_type, /*nonclass=*/0, | |
eea9800f | 13219 | is_namespace, |
a723baf1 MM |
13220 | /*flags=*/0); |
13221 | parser->object_scope = object_type; | |
13222 | parser->qualifying_scope = NULL_TREE; | |
13223 | if (object_decl) | |
13224 | decl = object_decl; | |
13225 | } | |
13226 | else | |
13227 | { | |
13228 | decl = lookup_name_real (name, is_type, /*nonclass=*/0, | |
eea9800f | 13229 | is_namespace, |
a723baf1 MM |
13230 | /*flags=*/0); |
13231 | parser->qualifying_scope = NULL_TREE; | |
13232 | parser->object_scope = NULL_TREE; | |
13233 | } | |
13234 | ||
13235 | /* If the lookup failed, let our caller know. */ | |
13236 | if (!decl | |
13237 | || decl == error_mark_node | |
13238 | || (TREE_CODE (decl) == FUNCTION_DECL | |
13239 | && DECL_ANTICIPATED (decl))) | |
13240 | return error_mark_node; | |
13241 | ||
13242 | /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ | |
13243 | if (TREE_CODE (decl) == TREE_LIST) | |
13244 | { | |
13245 | /* The error message we have to print is too complicated for | |
13246 | cp_parser_error, so we incorporate its actions directly. */ | |
e5976695 | 13247 | if (!cp_parser_simulate_error (parser)) |
a723baf1 MM |
13248 | { |
13249 | error ("reference to `%D' is ambiguous", name); | |
13250 | print_candidates (decl); | |
13251 | } | |
13252 | return error_mark_node; | |
13253 | } | |
13254 | ||
13255 | my_friendly_assert (DECL_P (decl) | |
13256 | || TREE_CODE (decl) == OVERLOAD | |
13257 | || TREE_CODE (decl) == SCOPE_REF | |
13258 | || BASELINK_P (decl), | |
13259 | 20000619); | |
13260 | ||
13261 | /* If we have resolved the name of a member declaration, check to | |
13262 | see if the declaration is accessible. When the name resolves to | |
34cd5ae7 | 13263 | set of overloaded functions, accessibility is checked when |
a723baf1 MM |
13264 | overload resolution is done. |
13265 | ||
13266 | During an explicit instantiation, access is not checked at all, | |
13267 | as per [temp.explicit]. */ | |
8d241e0b | 13268 | if (DECL_P (decl)) |
ee76b931 | 13269 | check_accessibility_of_qualified_id (decl, object_type, parser->scope); |
a723baf1 MM |
13270 | |
13271 | return decl; | |
13272 | } | |
13273 | ||
13274 | /* Like cp_parser_lookup_name, but for use in the typical case where | |
13275 | CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is | |
13276 | TRUE. */ | |
13277 | ||
13278 | static tree | |
94edc4ab | 13279 | cp_parser_lookup_name_simple (cp_parser* parser, tree name) |
a723baf1 MM |
13280 | { |
13281 | return cp_parser_lookup_name (parser, name, | |
eea9800f MM |
13282 | /*is_type=*/false, |
13283 | /*is_namespace=*/false, | |
a723baf1 MM |
13284 | /*check_dependency=*/true); |
13285 | } | |
13286 | ||
a723baf1 MM |
13287 | /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in |
13288 | the current context, return the TYPE_DECL. If TAG_NAME_P is | |
13289 | true, the DECL indicates the class being defined in a class-head, | |
13290 | or declared in an elaborated-type-specifier. | |
13291 | ||
13292 | Otherwise, return DECL. */ | |
13293 | ||
13294 | static tree | |
13295 | cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p) | |
13296 | { | |
710b73e6 KL |
13297 | /* If the TEMPLATE_DECL is being declared as part of a class-head, |
13298 | the translation from TEMPLATE_DECL to TYPE_DECL occurs: | |
a723baf1 MM |
13299 | |
13300 | struct A { | |
13301 | template <typename T> struct B; | |
13302 | }; | |
13303 | ||
13304 | template <typename T> struct A::B {}; | |
13305 | ||
13306 | Similarly, in a elaborated-type-specifier: | |
13307 | ||
13308 | namespace N { struct X{}; } | |
13309 | ||
13310 | struct A { | |
13311 | template <typename T> friend struct N::X; | |
13312 | }; | |
13313 | ||
710b73e6 KL |
13314 | However, if the DECL refers to a class type, and we are in |
13315 | the scope of the class, then the name lookup automatically | |
13316 | finds the TYPE_DECL created by build_self_reference rather | |
13317 | than a TEMPLATE_DECL. For example, in: | |
13318 | ||
13319 | template <class T> struct S { | |
13320 | S s; | |
13321 | }; | |
13322 | ||
13323 | there is no need to handle such case. */ | |
13324 | ||
13325 | if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p) | |
a723baf1 MM |
13326 | return DECL_TEMPLATE_RESULT (decl); |
13327 | ||
13328 | return decl; | |
13329 | } | |
13330 | ||
13331 | /* If too many, or too few, template-parameter lists apply to the | |
13332 | declarator, issue an error message. Returns TRUE if all went well, | |
13333 | and FALSE otherwise. */ | |
13334 | ||
13335 | static bool | |
94edc4ab NN |
13336 | cp_parser_check_declarator_template_parameters (cp_parser* parser, |
13337 | tree declarator) | |
a723baf1 MM |
13338 | { |
13339 | unsigned num_templates; | |
13340 | ||
13341 | /* We haven't seen any classes that involve template parameters yet. */ | |
13342 | num_templates = 0; | |
13343 | ||
13344 | switch (TREE_CODE (declarator)) | |
13345 | { | |
13346 | case CALL_EXPR: | |
13347 | case ARRAY_REF: | |
13348 | case INDIRECT_REF: | |
13349 | case ADDR_EXPR: | |
13350 | { | |
13351 | tree main_declarator = TREE_OPERAND (declarator, 0); | |
13352 | return | |
13353 | cp_parser_check_declarator_template_parameters (parser, | |
13354 | main_declarator); | |
13355 | } | |
13356 | ||
13357 | case SCOPE_REF: | |
13358 | { | |
13359 | tree scope; | |
13360 | tree member; | |
13361 | ||
13362 | scope = TREE_OPERAND (declarator, 0); | |
13363 | member = TREE_OPERAND (declarator, 1); | |
13364 | ||
13365 | /* If this is a pointer-to-member, then we are not interested | |
13366 | in the SCOPE, because it does not qualify the thing that is | |
13367 | being declared. */ | |
13368 | if (TREE_CODE (member) == INDIRECT_REF) | |
13369 | return (cp_parser_check_declarator_template_parameters | |
13370 | (parser, member)); | |
13371 | ||
13372 | while (scope && CLASS_TYPE_P (scope)) | |
13373 | { | |
13374 | /* You're supposed to have one `template <...>' | |
13375 | for every template class, but you don't need one | |
13376 | for a full specialization. For example: | |
13377 | ||
13378 | template <class T> struct S{}; | |
13379 | template <> struct S<int> { void f(); }; | |
13380 | void S<int>::f () {} | |
13381 | ||
13382 | is correct; there shouldn't be a `template <>' for | |
13383 | the definition of `S<int>::f'. */ | |
13384 | if (CLASSTYPE_TEMPLATE_INFO (scope) | |
13385 | && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope) | |
13386 | || uses_template_parms (CLASSTYPE_TI_ARGS (scope))) | |
13387 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))) | |
13388 | ++num_templates; | |
13389 | ||
13390 | scope = TYPE_CONTEXT (scope); | |
13391 | } | |
13392 | } | |
13393 | ||
13394 | /* Fall through. */ | |
13395 | ||
13396 | default: | |
13397 | /* If the DECLARATOR has the form `X<y>' then it uses one | |
13398 | additional level of template parameters. */ | |
13399 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
13400 | ++num_templates; | |
13401 | ||
13402 | return cp_parser_check_template_parameters (parser, | |
13403 | num_templates); | |
13404 | } | |
13405 | } | |
13406 | ||
13407 | /* NUM_TEMPLATES were used in the current declaration. If that is | |
13408 | invalid, return FALSE and issue an error messages. Otherwise, | |
13409 | return TRUE. */ | |
13410 | ||
13411 | static bool | |
94edc4ab NN |
13412 | cp_parser_check_template_parameters (cp_parser* parser, |
13413 | unsigned num_templates) | |
a723baf1 MM |
13414 | { |
13415 | /* If there are more template classes than parameter lists, we have | |
13416 | something like: | |
13417 | ||
13418 | template <class T> void S<T>::R<T>::f (); */ | |
13419 | if (parser->num_template_parameter_lists < num_templates) | |
13420 | { | |
13421 | error ("too few template-parameter-lists"); | |
13422 | return false; | |
13423 | } | |
13424 | /* If there are the same number of template classes and parameter | |
13425 | lists, that's OK. */ | |
13426 | if (parser->num_template_parameter_lists == num_templates) | |
13427 | return true; | |
13428 | /* If there are more, but only one more, then we are referring to a | |
13429 | member template. That's OK too. */ | |
13430 | if (parser->num_template_parameter_lists == num_templates + 1) | |
13431 | return true; | |
13432 | /* Otherwise, there are too many template parameter lists. We have | |
13433 | something like: | |
13434 | ||
13435 | template <class T> template <class U> void S::f(); */ | |
13436 | error ("too many template-parameter-lists"); | |
13437 | return false; | |
13438 | } | |
13439 | ||
13440 | /* Parse a binary-expression of the general form: | |
13441 | ||
13442 | binary-expression: | |
13443 | <expr> | |
13444 | binary-expression <token> <expr> | |
13445 | ||
13446 | The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used | |
13447 | to parser the <expr>s. If the first production is used, then the | |
13448 | value returned by FN is returned directly. Otherwise, a node with | |
13449 | the indicated EXPR_TYPE is returned, with operands corresponding to | |
13450 | the two sub-expressions. */ | |
13451 | ||
13452 | static tree | |
94edc4ab NN |
13453 | cp_parser_binary_expression (cp_parser* parser, |
13454 | const cp_parser_token_tree_map token_tree_map, | |
13455 | cp_parser_expression_fn fn) | |
a723baf1 MM |
13456 | { |
13457 | tree lhs; | |
13458 | ||
13459 | /* Parse the first expression. */ | |
13460 | lhs = (*fn) (parser); | |
13461 | /* Now, look for more expressions. */ | |
13462 | while (true) | |
13463 | { | |
13464 | cp_token *token; | |
39b1af70 | 13465 | const cp_parser_token_tree_map_node *map_node; |
a723baf1 MM |
13466 | tree rhs; |
13467 | ||
13468 | /* Peek at the next token. */ | |
13469 | token = cp_lexer_peek_token (parser->lexer); | |
13470 | /* If the token is `>', and that's not an operator at the | |
13471 | moment, then we're done. */ | |
13472 | if (token->type == CPP_GREATER | |
13473 | && !parser->greater_than_is_operator_p) | |
13474 | break; | |
34cd5ae7 | 13475 | /* If we find one of the tokens we want, build the corresponding |
a723baf1 MM |
13476 | tree representation. */ |
13477 | for (map_node = token_tree_map; | |
13478 | map_node->token_type != CPP_EOF; | |
13479 | ++map_node) | |
13480 | if (map_node->token_type == token->type) | |
13481 | { | |
13482 | /* Consume the operator token. */ | |
13483 | cp_lexer_consume_token (parser->lexer); | |
13484 | /* Parse the right-hand side of the expression. */ | |
13485 | rhs = (*fn) (parser); | |
13486 | /* Build the binary tree node. */ | |
13487 | lhs = build_x_binary_op (map_node->tree_type, lhs, rhs); | |
13488 | break; | |
13489 | } | |
13490 | ||
13491 | /* If the token wasn't one of the ones we want, we're done. */ | |
13492 | if (map_node->token_type == CPP_EOF) | |
13493 | break; | |
13494 | } | |
13495 | ||
13496 | return lhs; | |
13497 | } | |
13498 | ||
13499 | /* Parse an optional `::' token indicating that the following name is | |
13500 | from the global namespace. If so, PARSER->SCOPE is set to the | |
13501 | GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE, | |
13502 | unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone. | |
13503 | Returns the new value of PARSER->SCOPE, if the `::' token is | |
13504 | present, and NULL_TREE otherwise. */ | |
13505 | ||
13506 | static tree | |
94edc4ab | 13507 | cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p) |
a723baf1 MM |
13508 | { |
13509 | cp_token *token; | |
13510 | ||
13511 | /* Peek at the next token. */ | |
13512 | token = cp_lexer_peek_token (parser->lexer); | |
13513 | /* If we're looking at a `::' token then we're starting from the | |
13514 | global namespace, not our current location. */ | |
13515 | if (token->type == CPP_SCOPE) | |
13516 | { | |
13517 | /* Consume the `::' token. */ | |
13518 | cp_lexer_consume_token (parser->lexer); | |
13519 | /* Set the SCOPE so that we know where to start the lookup. */ | |
13520 | parser->scope = global_namespace; | |
13521 | parser->qualifying_scope = global_namespace; | |
13522 | parser->object_scope = NULL_TREE; | |
13523 | ||
13524 | return parser->scope; | |
13525 | } | |
13526 | else if (!current_scope_valid_p) | |
13527 | { | |
13528 | parser->scope = NULL_TREE; | |
13529 | parser->qualifying_scope = NULL_TREE; | |
13530 | parser->object_scope = NULL_TREE; | |
13531 | } | |
13532 | ||
13533 | return NULL_TREE; | |
13534 | } | |
13535 | ||
13536 | /* Returns TRUE if the upcoming token sequence is the start of a | |
13537 | constructor declarator. If FRIEND_P is true, the declarator is | |
13538 | preceded by the `friend' specifier. */ | |
13539 | ||
13540 | static bool | |
13541 | cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p) | |
13542 | { | |
13543 | bool constructor_p; | |
13544 | tree type_decl = NULL_TREE; | |
13545 | bool nested_name_p; | |
2050a1bb MM |
13546 | cp_token *next_token; |
13547 | ||
13548 | /* The common case is that this is not a constructor declarator, so | |
8fbc5ae7 MM |
13549 | try to avoid doing lots of work if at all possible. It's not |
13550 | valid declare a constructor at function scope. */ | |
13551 | if (at_function_scope_p ()) | |
13552 | return false; | |
13553 | /* And only certain tokens can begin a constructor declarator. */ | |
2050a1bb MM |
13554 | next_token = cp_lexer_peek_token (parser->lexer); |
13555 | if (next_token->type != CPP_NAME | |
13556 | && next_token->type != CPP_SCOPE | |
13557 | && next_token->type != CPP_NESTED_NAME_SPECIFIER | |
13558 | && next_token->type != CPP_TEMPLATE_ID) | |
13559 | return false; | |
a723baf1 MM |
13560 | |
13561 | /* Parse tentatively; we are going to roll back all of the tokens | |
13562 | consumed here. */ | |
13563 | cp_parser_parse_tentatively (parser); | |
13564 | /* Assume that we are looking at a constructor declarator. */ | |
13565 | constructor_p = true; | |
8d241e0b | 13566 | |
a723baf1 MM |
13567 | /* Look for the optional `::' operator. */ |
13568 | cp_parser_global_scope_opt (parser, | |
13569 | /*current_scope_valid_p=*/false); | |
13570 | /* Look for the nested-name-specifier. */ | |
13571 | nested_name_p | |
13572 | = (cp_parser_nested_name_specifier_opt (parser, | |
13573 | /*typename_keyword_p=*/false, | |
13574 | /*check_dependency_p=*/false, | |
a668c6ad MM |
13575 | /*type_p=*/false, |
13576 | /*is_declaration=*/false) | |
a723baf1 MM |
13577 | != NULL_TREE); |
13578 | /* Outside of a class-specifier, there must be a | |
13579 | nested-name-specifier. */ | |
13580 | if (!nested_name_p && | |
13581 | (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type) | |
13582 | || friend_p)) | |
13583 | constructor_p = false; | |
13584 | /* If we still think that this might be a constructor-declarator, | |
13585 | look for a class-name. */ | |
13586 | if (constructor_p) | |
13587 | { | |
13588 | /* If we have: | |
13589 | ||
8fbc5ae7 | 13590 | template <typename T> struct S { S(); }; |
a723baf1 MM |
13591 | template <typename T> S<T>::S (); |
13592 | ||
13593 | we must recognize that the nested `S' names a class. | |
13594 | Similarly, for: | |
13595 | ||
13596 | template <typename T> S<T>::S<T> (); | |
13597 | ||
13598 | we must recognize that the nested `S' names a template. */ | |
13599 | type_decl = cp_parser_class_name (parser, | |
13600 | /*typename_keyword_p=*/false, | |
13601 | /*template_keyword_p=*/false, | |
13602 | /*type_p=*/false, | |
a723baf1 | 13603 | /*check_dependency_p=*/false, |
a668c6ad MM |
13604 | /*class_head_p=*/false, |
13605 | /*is_declaration=*/false); | |
a723baf1 MM |
13606 | /* If there was no class-name, then this is not a constructor. */ |
13607 | constructor_p = !cp_parser_error_occurred (parser); | |
13608 | } | |
8d241e0b | 13609 | |
a723baf1 MM |
13610 | /* If we're still considering a constructor, we have to see a `(', |
13611 | to begin the parameter-declaration-clause, followed by either a | |
13612 | `)', an `...', or a decl-specifier. We need to check for a | |
13613 | type-specifier to avoid being fooled into thinking that: | |
13614 | ||
13615 | S::S (f) (int); | |
13616 | ||
13617 | is a constructor. (It is actually a function named `f' that | |
13618 | takes one parameter (of type `int') and returns a value of type | |
13619 | `S::S'. */ | |
13620 | if (constructor_p | |
13621 | && cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) | |
13622 | { | |
13623 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN) | |
13624 | && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS) | |
13625 | && !cp_parser_storage_class_specifier_opt (parser)) | |
13626 | { | |
5dae1114 | 13627 | tree type; |
4047b164 | 13628 | unsigned saved_num_template_parameter_lists; |
5dae1114 MM |
13629 | |
13630 | /* Names appearing in the type-specifier should be looked up | |
13631 | in the scope of the class. */ | |
13632 | if (current_class_type) | |
13633 | type = NULL_TREE; | |
a723baf1 MM |
13634 | else |
13635 | { | |
5dae1114 MM |
13636 | type = TREE_TYPE (type_decl); |
13637 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
14d22dd6 MM |
13638 | { |
13639 | type = resolve_typename_type (type, | |
13640 | /*only_current_p=*/false); | |
13641 | if (type == error_mark_node) | |
13642 | { | |
13643 | cp_parser_abort_tentative_parse (parser); | |
13644 | return false; | |
13645 | } | |
13646 | } | |
5dae1114 | 13647 | push_scope (type); |
a723baf1 | 13648 | } |
4047b164 KL |
13649 | |
13650 | /* Inside the constructor parameter list, surrounding | |
13651 | template-parameter-lists do not apply. */ | |
13652 | saved_num_template_parameter_lists | |
13653 | = parser->num_template_parameter_lists; | |
13654 | parser->num_template_parameter_lists = 0; | |
13655 | ||
5dae1114 MM |
13656 | /* Look for the type-specifier. */ |
13657 | cp_parser_type_specifier (parser, | |
13658 | CP_PARSER_FLAGS_NONE, | |
13659 | /*is_friend=*/false, | |
13660 | /*is_declarator=*/true, | |
13661 | /*declares_class_or_enum=*/NULL, | |
13662 | /*is_cv_qualifier=*/NULL); | |
4047b164 KL |
13663 | |
13664 | parser->num_template_parameter_lists | |
13665 | = saved_num_template_parameter_lists; | |
13666 | ||
5dae1114 MM |
13667 | /* Leave the scope of the class. */ |
13668 | if (type) | |
13669 | pop_scope (type); | |
13670 | ||
13671 | constructor_p = !cp_parser_error_occurred (parser); | |
a723baf1 MM |
13672 | } |
13673 | } | |
13674 | else | |
13675 | constructor_p = false; | |
13676 | /* We did not really want to consume any tokens. */ | |
13677 | cp_parser_abort_tentative_parse (parser); | |
13678 | ||
13679 | return constructor_p; | |
13680 | } | |
13681 | ||
13682 | /* Parse the definition of the function given by the DECL_SPECIFIERS, | |
cf22909c | 13683 | ATTRIBUTES, and DECLARATOR. The access checks have been deferred; |
a723baf1 MM |
13684 | they must be performed once we are in the scope of the function. |
13685 | ||
13686 | Returns the function defined. */ | |
13687 | ||
13688 | static tree | |
13689 | cp_parser_function_definition_from_specifiers_and_declarator | |
94edc4ab NN |
13690 | (cp_parser* parser, |
13691 | tree decl_specifiers, | |
13692 | tree attributes, | |
13693 | tree declarator) | |
a723baf1 MM |
13694 | { |
13695 | tree fn; | |
13696 | bool success_p; | |
13697 | ||
13698 | /* Begin the function-definition. */ | |
13699 | success_p = begin_function_definition (decl_specifiers, | |
13700 | attributes, | |
13701 | declarator); | |
13702 | ||
13703 | /* If there were names looked up in the decl-specifier-seq that we | |
13704 | did not check, check them now. We must wait until we are in the | |
13705 | scope of the function to perform the checks, since the function | |
13706 | might be a friend. */ | |
cf22909c | 13707 | perform_deferred_access_checks (); |
a723baf1 MM |
13708 | |
13709 | if (!success_p) | |
13710 | { | |
13711 | /* If begin_function_definition didn't like the definition, skip | |
13712 | the entire function. */ | |
13713 | error ("invalid function declaration"); | |
13714 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
13715 | fn = error_mark_node; | |
13716 | } | |
13717 | else | |
13718 | fn = cp_parser_function_definition_after_declarator (parser, | |
13719 | /*inline_p=*/false); | |
13720 | ||
13721 | return fn; | |
13722 | } | |
13723 | ||
13724 | /* Parse the part of a function-definition that follows the | |
13725 | declarator. INLINE_P is TRUE iff this function is an inline | |
13726 | function defined with a class-specifier. | |
13727 | ||
13728 | Returns the function defined. */ | |
13729 | ||
13730 | static tree | |
94edc4ab NN |
13731 | cp_parser_function_definition_after_declarator (cp_parser* parser, |
13732 | bool inline_p) | |
a723baf1 MM |
13733 | { |
13734 | tree fn; | |
13735 | bool ctor_initializer_p = false; | |
13736 | bool saved_in_unbraced_linkage_specification_p; | |
13737 | unsigned saved_num_template_parameter_lists; | |
13738 | ||
13739 | /* If the next token is `return', then the code may be trying to | |
13740 | make use of the "named return value" extension that G++ used to | |
13741 | support. */ | |
13742 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN)) | |
13743 | { | |
13744 | /* Consume the `return' keyword. */ | |
13745 | cp_lexer_consume_token (parser->lexer); | |
13746 | /* Look for the identifier that indicates what value is to be | |
13747 | returned. */ | |
13748 | cp_parser_identifier (parser); | |
13749 | /* Issue an error message. */ | |
13750 | error ("named return values are no longer supported"); | |
13751 | /* Skip tokens until we reach the start of the function body. */ | |
13752 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
13753 | cp_lexer_consume_token (parser->lexer); | |
13754 | } | |
13755 | /* The `extern' in `extern "C" void f () { ... }' does not apply to | |
13756 | anything declared inside `f'. */ | |
13757 | saved_in_unbraced_linkage_specification_p | |
13758 | = parser->in_unbraced_linkage_specification_p; | |
13759 | parser->in_unbraced_linkage_specification_p = false; | |
13760 | /* Inside the function, surrounding template-parameter-lists do not | |
13761 | apply. */ | |
13762 | saved_num_template_parameter_lists | |
13763 | = parser->num_template_parameter_lists; | |
13764 | parser->num_template_parameter_lists = 0; | |
13765 | /* If the next token is `try', then we are looking at a | |
13766 | function-try-block. */ | |
13767 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY)) | |
13768 | ctor_initializer_p = cp_parser_function_try_block (parser); | |
13769 | /* A function-try-block includes the function-body, so we only do | |
13770 | this next part if we're not processing a function-try-block. */ | |
13771 | else | |
13772 | ctor_initializer_p | |
13773 | = cp_parser_ctor_initializer_opt_and_function_body (parser); | |
13774 | ||
13775 | /* Finish the function. */ | |
13776 | fn = finish_function ((ctor_initializer_p ? 1 : 0) | | |
13777 | (inline_p ? 2 : 0)); | |
13778 | /* Generate code for it, if necessary. */ | |
8cd2462c | 13779 | expand_or_defer_fn (fn); |
a723baf1 MM |
13780 | /* Restore the saved values. */ |
13781 | parser->in_unbraced_linkage_specification_p | |
13782 | = saved_in_unbraced_linkage_specification_p; | |
13783 | parser->num_template_parameter_lists | |
13784 | = saved_num_template_parameter_lists; | |
13785 | ||
13786 | return fn; | |
13787 | } | |
13788 | ||
13789 | /* Parse a template-declaration, assuming that the `export' (and | |
13790 | `extern') keywords, if present, has already been scanned. MEMBER_P | |
13791 | is as for cp_parser_template_declaration. */ | |
13792 | ||
13793 | static void | |
94edc4ab | 13794 | cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p) |
a723baf1 MM |
13795 | { |
13796 | tree decl = NULL_TREE; | |
13797 | tree parameter_list; | |
13798 | bool friend_p = false; | |
13799 | ||
13800 | /* Look for the `template' keyword. */ | |
13801 | if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'")) | |
13802 | return; | |
13803 | ||
13804 | /* And the `<'. */ | |
13805 | if (!cp_parser_require (parser, CPP_LESS, "`<'")) | |
13806 | return; | |
13807 | ||
13808 | /* Parse the template parameters. */ | |
13809 | begin_template_parm_list (); | |
13810 | /* If the next token is `>', then we have an invalid | |
13811 | specialization. Rather than complain about an invalid template | |
13812 | parameter, issue an error message here. */ | |
13813 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) | |
13814 | { | |
13815 | cp_parser_error (parser, "invalid explicit specialization"); | |
13816 | parameter_list = NULL_TREE; | |
13817 | } | |
13818 | else | |
13819 | parameter_list = cp_parser_template_parameter_list (parser); | |
13820 | parameter_list = end_template_parm_list (parameter_list); | |
13821 | /* Look for the `>'. */ | |
13822 | cp_parser_skip_until_found (parser, CPP_GREATER, "`>'"); | |
13823 | /* We just processed one more parameter list. */ | |
13824 | ++parser->num_template_parameter_lists; | |
13825 | /* If the next token is `template', there are more template | |
13826 | parameters. */ | |
13827 | if (cp_lexer_next_token_is_keyword (parser->lexer, | |
13828 | RID_TEMPLATE)) | |
13829 | cp_parser_template_declaration_after_export (parser, member_p); | |
13830 | else | |
13831 | { | |
13832 | decl = cp_parser_single_declaration (parser, | |
13833 | member_p, | |
13834 | &friend_p); | |
13835 | ||
13836 | /* If this is a member template declaration, let the front | |
13837 | end know. */ | |
13838 | if (member_p && !friend_p && decl) | |
37d407a1 KL |
13839 | { |
13840 | if (TREE_CODE (decl) == TYPE_DECL) | |
13841 | cp_parser_check_access_in_redeclaration (decl); | |
13842 | ||
13843 | decl = finish_member_template_decl (decl); | |
13844 | } | |
a723baf1 | 13845 | else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL) |
19db77ce KL |
13846 | make_friend_class (current_class_type, TREE_TYPE (decl), |
13847 | /*complain=*/true); | |
a723baf1 MM |
13848 | } |
13849 | /* We are done with the current parameter list. */ | |
13850 | --parser->num_template_parameter_lists; | |
13851 | ||
13852 | /* Finish up. */ | |
13853 | finish_template_decl (parameter_list); | |
13854 | ||
13855 | /* Register member declarations. */ | |
13856 | if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl)) | |
13857 | finish_member_declaration (decl); | |
13858 | ||
13859 | /* If DECL is a function template, we must return to parse it later. | |
13860 | (Even though there is no definition, there might be default | |
13861 | arguments that need handling.) */ | |
13862 | if (member_p && decl | |
13863 | && (TREE_CODE (decl) == FUNCTION_DECL | |
13864 | || DECL_FUNCTION_TEMPLATE_P (decl))) | |
13865 | TREE_VALUE (parser->unparsed_functions_queues) | |
8218bd34 | 13866 | = tree_cons (NULL_TREE, decl, |
a723baf1 MM |
13867 | TREE_VALUE (parser->unparsed_functions_queues)); |
13868 | } | |
13869 | ||
13870 | /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or | |
13871 | `function-definition' sequence. MEMBER_P is true, this declaration | |
13872 | appears in a class scope. | |
13873 | ||
13874 | Returns the DECL for the declared entity. If FRIEND_P is non-NULL, | |
13875 | *FRIEND_P is set to TRUE iff the declaration is a friend. */ | |
13876 | ||
13877 | static tree | |
94edc4ab NN |
13878 | cp_parser_single_declaration (cp_parser* parser, |
13879 | bool member_p, | |
13880 | bool* friend_p) | |
a723baf1 | 13881 | { |
560ad596 | 13882 | int declares_class_or_enum; |
a723baf1 MM |
13883 | tree decl = NULL_TREE; |
13884 | tree decl_specifiers; | |
13885 | tree attributes; | |
a723baf1 MM |
13886 | |
13887 | /* Parse the dependent declaration. We don't know yet | |
13888 | whether it will be a function-definition. */ | |
13889 | cp_parser_parse_tentatively (parser); | |
13890 | /* Defer access checks until we know what is being declared. */ | |
8d241e0b | 13891 | push_deferring_access_checks (dk_deferred); |
cf22909c | 13892 | |
a723baf1 MM |
13893 | /* Try the `decl-specifier-seq [opt] init-declarator [opt]' |
13894 | alternative. */ | |
13895 | decl_specifiers | |
13896 | = cp_parser_decl_specifier_seq (parser, | |
13897 | CP_PARSER_FLAGS_OPTIONAL, | |
13898 | &attributes, | |
13899 | &declares_class_or_enum); | |
13900 | /* Gather up the access checks that occurred the | |
13901 | decl-specifier-seq. */ | |
cf22909c KL |
13902 | stop_deferring_access_checks (); |
13903 | ||
a723baf1 MM |
13904 | /* Check for the declaration of a template class. */ |
13905 | if (declares_class_or_enum) | |
13906 | { | |
13907 | if (cp_parser_declares_only_class_p (parser)) | |
13908 | { | |
13909 | decl = shadow_tag (decl_specifiers); | |
13910 | if (decl) | |
13911 | decl = TYPE_NAME (decl); | |
13912 | else | |
13913 | decl = error_mark_node; | |
13914 | } | |
13915 | } | |
13916 | else | |
13917 | decl = NULL_TREE; | |
13918 | /* If it's not a template class, try for a template function. If | |
13919 | the next token is a `;', then this declaration does not declare | |
13920 | anything. But, if there were errors in the decl-specifiers, then | |
13921 | the error might well have come from an attempted class-specifier. | |
13922 | In that case, there's no need to warn about a missing declarator. */ | |
13923 | if (!decl | |
13924 | && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON) | |
13925 | || !value_member (error_mark_node, decl_specifiers))) | |
13926 | decl = cp_parser_init_declarator (parser, | |
13927 | decl_specifiers, | |
13928 | attributes, | |
a723baf1 MM |
13929 | /*function_definition_allowed_p=*/false, |
13930 | member_p, | |
560ad596 | 13931 | declares_class_or_enum, |
a723baf1 | 13932 | /*function_definition_p=*/NULL); |
cf22909c KL |
13933 | |
13934 | pop_deferring_access_checks (); | |
13935 | ||
a723baf1 MM |
13936 | /* Clear any current qualification; whatever comes next is the start |
13937 | of something new. */ | |
13938 | parser->scope = NULL_TREE; | |
13939 | parser->qualifying_scope = NULL_TREE; | |
13940 | parser->object_scope = NULL_TREE; | |
13941 | /* Look for a trailing `;' after the declaration. */ | |
8a6393df | 13942 | if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'") |
a723baf1 MM |
13943 | && cp_parser_committed_to_tentative_parse (parser)) |
13944 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
13945 | /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */ | |
13946 | if (cp_parser_parse_definitely (parser)) | |
13947 | { | |
13948 | if (friend_p) | |
13949 | *friend_p = cp_parser_friend_p (decl_specifiers); | |
13950 | } | |
13951 | /* Otherwise, try a function-definition. */ | |
13952 | else | |
13953 | decl = cp_parser_function_definition (parser, friend_p); | |
13954 | ||
13955 | return decl; | |
13956 | } | |
13957 | ||
d6b4ea85 MM |
13958 | /* Parse a cast-expression that is not the operand of a unary "&". */ |
13959 | ||
13960 | static tree | |
13961 | cp_parser_simple_cast_expression (cp_parser *parser) | |
13962 | { | |
13963 | return cp_parser_cast_expression (parser, /*address_p=*/false); | |
13964 | } | |
13965 | ||
a723baf1 MM |
13966 | /* Parse a functional cast to TYPE. Returns an expression |
13967 | representing the cast. */ | |
13968 | ||
13969 | static tree | |
94edc4ab | 13970 | cp_parser_functional_cast (cp_parser* parser, tree type) |
a723baf1 MM |
13971 | { |
13972 | tree expression_list; | |
13973 | ||
39703eb9 MM |
13974 | expression_list |
13975 | = cp_parser_parenthesized_expression_list (parser, false, | |
13976 | /*non_constant_p=*/NULL); | |
a723baf1 MM |
13977 | |
13978 | return build_functional_cast (type, expression_list); | |
13979 | } | |
13980 | ||
ec75414f MM |
13981 | /* Parse a template-argument-list, as well as the trailing ">" (but |
13982 | not the opening ">"). See cp_parser_template_argument_list for the | |
13983 | return value. */ | |
13984 | ||
13985 | static tree | |
13986 | cp_parser_enclosed_template_argument_list (cp_parser* parser) | |
13987 | { | |
13988 | tree arguments; | |
13989 | tree saved_scope; | |
13990 | tree saved_qualifying_scope; | |
13991 | tree saved_object_scope; | |
13992 | bool saved_greater_than_is_operator_p; | |
13993 | ||
13994 | /* [temp.names] | |
13995 | ||
13996 | When parsing a template-id, the first non-nested `>' is taken as | |
13997 | the end of the template-argument-list rather than a greater-than | |
13998 | operator. */ | |
13999 | saved_greater_than_is_operator_p | |
14000 | = parser->greater_than_is_operator_p; | |
14001 | parser->greater_than_is_operator_p = false; | |
14002 | /* Parsing the argument list may modify SCOPE, so we save it | |
14003 | here. */ | |
14004 | saved_scope = parser->scope; | |
14005 | saved_qualifying_scope = parser->qualifying_scope; | |
14006 | saved_object_scope = parser->object_scope; | |
14007 | /* Parse the template-argument-list itself. */ | |
14008 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) | |
14009 | arguments = NULL_TREE; | |
14010 | else | |
14011 | arguments = cp_parser_template_argument_list (parser); | |
14012 | /* Look for the `>' that ends the template-argument-list. */ | |
14013 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
14014 | /* The `>' token might be a greater-than operator again now. */ | |
14015 | parser->greater_than_is_operator_p | |
14016 | = saved_greater_than_is_operator_p; | |
14017 | /* Restore the SAVED_SCOPE. */ | |
14018 | parser->scope = saved_scope; | |
14019 | parser->qualifying_scope = saved_qualifying_scope; | |
14020 | parser->object_scope = saved_object_scope; | |
14021 | ||
14022 | return arguments; | |
14023 | } | |
14024 | ||
14025 | ||
a723baf1 MM |
14026 | /* MEMBER_FUNCTION is a member function, or a friend. If default |
14027 | arguments, or the body of the function have not yet been parsed, | |
14028 | parse them now. */ | |
14029 | ||
14030 | static void | |
94edc4ab | 14031 | cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function) |
a723baf1 MM |
14032 | { |
14033 | cp_lexer *saved_lexer; | |
14034 | ||
14035 | /* If this member is a template, get the underlying | |
14036 | FUNCTION_DECL. */ | |
14037 | if (DECL_FUNCTION_TEMPLATE_P (member_function)) | |
14038 | member_function = DECL_TEMPLATE_RESULT (member_function); | |
14039 | ||
14040 | /* There should not be any class definitions in progress at this | |
14041 | point; the bodies of members are only parsed outside of all class | |
14042 | definitions. */ | |
14043 | my_friendly_assert (parser->num_classes_being_defined == 0, 20010816); | |
14044 | /* While we're parsing the member functions we might encounter more | |
14045 | classes. We want to handle them right away, but we don't want | |
14046 | them getting mixed up with functions that are currently in the | |
14047 | queue. */ | |
14048 | parser->unparsed_functions_queues | |
14049 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
14050 | ||
14051 | /* Make sure that any template parameters are in scope. */ | |
14052 | maybe_begin_member_template_processing (member_function); | |
14053 | ||
a723baf1 MM |
14054 | /* If the body of the function has not yet been parsed, parse it |
14055 | now. */ | |
14056 | if (DECL_PENDING_INLINE_P (member_function)) | |
14057 | { | |
14058 | tree function_scope; | |
14059 | cp_token_cache *tokens; | |
14060 | ||
14061 | /* The function is no longer pending; we are processing it. */ | |
14062 | tokens = DECL_PENDING_INLINE_INFO (member_function); | |
14063 | DECL_PENDING_INLINE_INFO (member_function) = NULL; | |
14064 | DECL_PENDING_INLINE_P (member_function) = 0; | |
14065 | /* If this was an inline function in a local class, enter the scope | |
14066 | of the containing function. */ | |
14067 | function_scope = decl_function_context (member_function); | |
14068 | if (function_scope) | |
14069 | push_function_context_to (function_scope); | |
14070 | ||
14071 | /* Save away the current lexer. */ | |
14072 | saved_lexer = parser->lexer; | |
14073 | /* Make a new lexer to feed us the tokens saved for this function. */ | |
14074 | parser->lexer = cp_lexer_new_from_tokens (tokens); | |
14075 | parser->lexer->next = saved_lexer; | |
14076 | ||
14077 | /* Set the current source position to be the location of the first | |
14078 | token in the saved inline body. */ | |
3466b292 | 14079 | cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
14080 | |
14081 | /* Let the front end know that we going to be defining this | |
14082 | function. */ | |
14083 | start_function (NULL_TREE, member_function, NULL_TREE, | |
14084 | SF_PRE_PARSED | SF_INCLASS_INLINE); | |
14085 | ||
14086 | /* Now, parse the body of the function. */ | |
14087 | cp_parser_function_definition_after_declarator (parser, | |
14088 | /*inline_p=*/true); | |
14089 | ||
14090 | /* Leave the scope of the containing function. */ | |
14091 | if (function_scope) | |
14092 | pop_function_context_from (function_scope); | |
14093 | /* Restore the lexer. */ | |
14094 | parser->lexer = saved_lexer; | |
14095 | } | |
14096 | ||
14097 | /* Remove any template parameters from the symbol table. */ | |
14098 | maybe_end_member_template_processing (); | |
14099 | ||
14100 | /* Restore the queue. */ | |
14101 | parser->unparsed_functions_queues | |
14102 | = TREE_CHAIN (parser->unparsed_functions_queues); | |
14103 | } | |
14104 | ||
cd0be382 | 14105 | /* If DECL contains any default args, remember it on the unparsed |
8db1028e NS |
14106 | functions queue. */ |
14107 | ||
14108 | static void | |
14109 | cp_parser_save_default_args (cp_parser* parser, tree decl) | |
14110 | { | |
14111 | tree probe; | |
14112 | ||
14113 | for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
14114 | probe; | |
14115 | probe = TREE_CHAIN (probe)) | |
14116 | if (TREE_PURPOSE (probe)) | |
14117 | { | |
14118 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
14119 | = tree_cons (NULL_TREE, decl, | |
14120 | TREE_PURPOSE (parser->unparsed_functions_queues)); | |
14121 | break; | |
14122 | } | |
14123 | return; | |
14124 | } | |
14125 | ||
8218bd34 MM |
14126 | /* FN is a FUNCTION_DECL which may contains a parameter with an |
14127 | unparsed DEFAULT_ARG. Parse the default args now. */ | |
a723baf1 MM |
14128 | |
14129 | static void | |
8218bd34 | 14130 | cp_parser_late_parsing_default_args (cp_parser *parser, tree fn) |
a723baf1 MM |
14131 | { |
14132 | cp_lexer *saved_lexer; | |
14133 | cp_token_cache *tokens; | |
14134 | bool saved_local_variables_forbidden_p; | |
14135 | tree parameters; | |
8218bd34 | 14136 | |
b92bc2a0 NS |
14137 | /* While we're parsing the default args, we might (due to the |
14138 | statement expression extension) encounter more classes. We want | |
14139 | to handle them right away, but we don't want them getting mixed | |
14140 | up with default args that are currently in the queue. */ | |
14141 | parser->unparsed_functions_queues | |
14142 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
14143 | ||
8218bd34 | 14144 | for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
a723baf1 MM |
14145 | parameters; |
14146 | parameters = TREE_CHAIN (parameters)) | |
14147 | { | |
14148 | if (!TREE_PURPOSE (parameters) | |
14149 | || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG) | |
14150 | continue; | |
14151 | ||
14152 | /* Save away the current lexer. */ | |
14153 | saved_lexer = parser->lexer; | |
14154 | /* Create a new one, using the tokens we have saved. */ | |
14155 | tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters)); | |
14156 | parser->lexer = cp_lexer_new_from_tokens (tokens); | |
14157 | ||
14158 | /* Set the current source position to be the location of the | |
14159 | first token in the default argument. */ | |
3466b292 | 14160 | cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
14161 | |
14162 | /* Local variable names (and the `this' keyword) may not appear | |
14163 | in a default argument. */ | |
14164 | saved_local_variables_forbidden_p = parser->local_variables_forbidden_p; | |
14165 | parser->local_variables_forbidden_p = true; | |
14166 | /* Parse the assignment-expression. */ | |
8218bd34 | 14167 | if (DECL_CONTEXT (fn)) |
14d22dd6 | 14168 | push_nested_class (DECL_CONTEXT (fn)); |
a723baf1 | 14169 | TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser); |
8218bd34 | 14170 | if (DECL_CONTEXT (fn)) |
e5976695 | 14171 | pop_nested_class (); |
a723baf1 MM |
14172 | |
14173 | /* Restore saved state. */ | |
14174 | parser->lexer = saved_lexer; | |
14175 | parser->local_variables_forbidden_p = saved_local_variables_forbidden_p; | |
14176 | } | |
b92bc2a0 NS |
14177 | |
14178 | /* Restore the queue. */ | |
14179 | parser->unparsed_functions_queues | |
14180 | = TREE_CHAIN (parser->unparsed_functions_queues); | |
a723baf1 MM |
14181 | } |
14182 | ||
14183 | /* Parse the operand of `sizeof' (or a similar operator). Returns | |
14184 | either a TYPE or an expression, depending on the form of the | |
14185 | input. The KEYWORD indicates which kind of expression we have | |
14186 | encountered. */ | |
14187 | ||
14188 | static tree | |
94edc4ab | 14189 | cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword) |
a723baf1 MM |
14190 | { |
14191 | static const char *format; | |
14192 | tree expr = NULL_TREE; | |
14193 | const char *saved_message; | |
14194 | bool saved_constant_expression_p; | |
14195 | ||
14196 | /* Initialize FORMAT the first time we get here. */ | |
14197 | if (!format) | |
14198 | format = "types may not be defined in `%s' expressions"; | |
14199 | ||
14200 | /* Types cannot be defined in a `sizeof' expression. Save away the | |
14201 | old message. */ | |
14202 | saved_message = parser->type_definition_forbidden_message; | |
14203 | /* And create the new one. */ | |
14204 | parser->type_definition_forbidden_message | |
c68b0a84 KG |
14205 | = xmalloc (strlen (format) |
14206 | + strlen (IDENTIFIER_POINTER (ridpointers[keyword])) | |
14207 | + 1 /* `\0' */); | |
a723baf1 MM |
14208 | sprintf ((char *) parser->type_definition_forbidden_message, |
14209 | format, IDENTIFIER_POINTER (ridpointers[keyword])); | |
14210 | ||
14211 | /* The restrictions on constant-expressions do not apply inside | |
14212 | sizeof expressions. */ | |
14213 | saved_constant_expression_p = parser->constant_expression_p; | |
14214 | parser->constant_expression_p = false; | |
14215 | ||
3beb3abf MM |
14216 | /* Do not actually evaluate the expression. */ |
14217 | ++skip_evaluation; | |
a723baf1 MM |
14218 | /* If it's a `(', then we might be looking at the type-id |
14219 | construction. */ | |
14220 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
14221 | { | |
14222 | tree type; | |
14223 | ||
14224 | /* We can't be sure yet whether we're looking at a type-id or an | |
14225 | expression. */ | |
14226 | cp_parser_parse_tentatively (parser); | |
14227 | /* Consume the `('. */ | |
14228 | cp_lexer_consume_token (parser->lexer); | |
14229 | /* Parse the type-id. */ | |
14230 | type = cp_parser_type_id (parser); | |
14231 | /* Now, look for the trailing `)'. */ | |
14232 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
14233 | /* If all went well, then we're done. */ | |
14234 | if (cp_parser_parse_definitely (parser)) | |
14235 | { | |
14236 | /* Build a list of decl-specifiers; right now, we have only | |
14237 | a single type-specifier. */ | |
14238 | type = build_tree_list (NULL_TREE, | |
14239 | type); | |
14240 | ||
14241 | /* Call grokdeclarator to figure out what type this is. */ | |
14242 | expr = grokdeclarator (NULL_TREE, | |
14243 | type, | |
14244 | TYPENAME, | |
14245 | /*initialized=*/0, | |
14246 | /*attrlist=*/NULL); | |
14247 | } | |
14248 | } | |
14249 | ||
14250 | /* If the type-id production did not work out, then we must be | |
14251 | looking at the unary-expression production. */ | |
14252 | if (!expr) | |
14253 | expr = cp_parser_unary_expression (parser, /*address_p=*/false); | |
3beb3abf MM |
14254 | /* Go back to evaluating expressions. */ |
14255 | --skip_evaluation; | |
a723baf1 MM |
14256 | |
14257 | /* Free the message we created. */ | |
14258 | free ((char *) parser->type_definition_forbidden_message); | |
14259 | /* And restore the old one. */ | |
14260 | parser->type_definition_forbidden_message = saved_message; | |
14261 | parser->constant_expression_p = saved_constant_expression_p; | |
14262 | ||
14263 | return expr; | |
14264 | } | |
14265 | ||
14266 | /* If the current declaration has no declarator, return true. */ | |
14267 | ||
14268 | static bool | |
14269 | cp_parser_declares_only_class_p (cp_parser *parser) | |
14270 | { | |
14271 | /* If the next token is a `;' or a `,' then there is no | |
14272 | declarator. */ | |
14273 | return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) | |
14274 | || cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); | |
14275 | } | |
14276 | ||
d17811fd MM |
14277 | /* Simplify EXPR if it is a non-dependent expression. Returns the |
14278 | (possibly simplified) expression. */ | |
14279 | ||
14280 | static tree | |
14281 | cp_parser_fold_non_dependent_expr (tree expr) | |
14282 | { | |
14283 | /* If we're in a template, but EXPR isn't value dependent, simplify | |
14284 | it. We're supposed to treat: | |
14285 | ||
14286 | template <typename T> void f(T[1 + 1]); | |
14287 | template <typename T> void f(T[2]); | |
14288 | ||
14289 | as two declarations of the same function, for example. */ | |
14290 | if (processing_template_decl | |
14291 | && !type_dependent_expression_p (expr) | |
14292 | && !value_dependent_expression_p (expr)) | |
14293 | { | |
14294 | HOST_WIDE_INT saved_processing_template_decl; | |
14295 | ||
14296 | saved_processing_template_decl = processing_template_decl; | |
14297 | processing_template_decl = 0; | |
14298 | expr = tsubst_copy_and_build (expr, | |
14299 | /*args=*/NULL_TREE, | |
14300 | tf_error, | |
b3445994 MM |
14301 | /*in_decl=*/NULL_TREE, |
14302 | /*function_p=*/false); | |
d17811fd MM |
14303 | processing_template_decl = saved_processing_template_decl; |
14304 | } | |
14305 | return expr; | |
14306 | } | |
14307 | ||
a723baf1 MM |
14308 | /* DECL_SPECIFIERS is the representation of a decl-specifier-seq. |
14309 | Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */ | |
14310 | ||
14311 | static bool | |
94edc4ab | 14312 | cp_parser_friend_p (tree decl_specifiers) |
a723baf1 MM |
14313 | { |
14314 | while (decl_specifiers) | |
14315 | { | |
14316 | /* See if this decl-specifier is `friend'. */ | |
14317 | if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE | |
14318 | && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND) | |
14319 | return true; | |
14320 | ||
14321 | /* Go on to the next decl-specifier. */ | |
14322 | decl_specifiers = TREE_CHAIN (decl_specifiers); | |
14323 | } | |
14324 | ||
14325 | return false; | |
14326 | } | |
14327 | ||
14328 | /* If the next token is of the indicated TYPE, consume it. Otherwise, | |
14329 | issue an error message indicating that TOKEN_DESC was expected. | |
14330 | ||
14331 | Returns the token consumed, if the token had the appropriate type. | |
14332 | Otherwise, returns NULL. */ | |
14333 | ||
14334 | static cp_token * | |
94edc4ab NN |
14335 | cp_parser_require (cp_parser* parser, |
14336 | enum cpp_ttype type, | |
14337 | const char* token_desc) | |
a723baf1 MM |
14338 | { |
14339 | if (cp_lexer_next_token_is (parser->lexer, type)) | |
14340 | return cp_lexer_consume_token (parser->lexer); | |
14341 | else | |
14342 | { | |
e5976695 MM |
14343 | /* Output the MESSAGE -- unless we're parsing tentatively. */ |
14344 | if (!cp_parser_simulate_error (parser)) | |
14345 | error ("expected %s", token_desc); | |
a723baf1 MM |
14346 | return NULL; |
14347 | } | |
14348 | } | |
14349 | ||
14350 | /* Like cp_parser_require, except that tokens will be skipped until | |
14351 | the desired token is found. An error message is still produced if | |
14352 | the next token is not as expected. */ | |
14353 | ||
14354 | static void | |
94edc4ab NN |
14355 | cp_parser_skip_until_found (cp_parser* parser, |
14356 | enum cpp_ttype type, | |
14357 | const char* token_desc) | |
a723baf1 MM |
14358 | { |
14359 | cp_token *token; | |
14360 | unsigned nesting_depth = 0; | |
14361 | ||
14362 | if (cp_parser_require (parser, type, token_desc)) | |
14363 | return; | |
14364 | ||
14365 | /* Skip tokens until the desired token is found. */ | |
14366 | while (true) | |
14367 | { | |
14368 | /* Peek at the next token. */ | |
14369 | token = cp_lexer_peek_token (parser->lexer); | |
14370 | /* If we've reached the token we want, consume it and | |
14371 | stop. */ | |
14372 | if (token->type == type && !nesting_depth) | |
14373 | { | |
14374 | cp_lexer_consume_token (parser->lexer); | |
14375 | return; | |
14376 | } | |
14377 | /* If we've run out of tokens, stop. */ | |
14378 | if (token->type == CPP_EOF) | |
14379 | return; | |
14380 | if (token->type == CPP_OPEN_BRACE | |
14381 | || token->type == CPP_OPEN_PAREN | |
14382 | || token->type == CPP_OPEN_SQUARE) | |
14383 | ++nesting_depth; | |
14384 | else if (token->type == CPP_CLOSE_BRACE | |
14385 | || token->type == CPP_CLOSE_PAREN | |
14386 | || token->type == CPP_CLOSE_SQUARE) | |
14387 | { | |
14388 | if (nesting_depth-- == 0) | |
14389 | return; | |
14390 | } | |
14391 | /* Consume this token. */ | |
14392 | cp_lexer_consume_token (parser->lexer); | |
14393 | } | |
14394 | } | |
14395 | ||
14396 | /* If the next token is the indicated keyword, consume it. Otherwise, | |
14397 | issue an error message indicating that TOKEN_DESC was expected. | |
14398 | ||
14399 | Returns the token consumed, if the token had the appropriate type. | |
14400 | Otherwise, returns NULL. */ | |
14401 | ||
14402 | static cp_token * | |
94edc4ab NN |
14403 | cp_parser_require_keyword (cp_parser* parser, |
14404 | enum rid keyword, | |
14405 | const char* token_desc) | |
a723baf1 MM |
14406 | { |
14407 | cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc); | |
14408 | ||
14409 | if (token && token->keyword != keyword) | |
14410 | { | |
14411 | dyn_string_t error_msg; | |
14412 | ||
14413 | /* Format the error message. */ | |
14414 | error_msg = dyn_string_new (0); | |
14415 | dyn_string_append_cstr (error_msg, "expected "); | |
14416 | dyn_string_append_cstr (error_msg, token_desc); | |
14417 | cp_parser_error (parser, error_msg->s); | |
14418 | dyn_string_delete (error_msg); | |
14419 | return NULL; | |
14420 | } | |
14421 | ||
14422 | return token; | |
14423 | } | |
14424 | ||
14425 | /* Returns TRUE iff TOKEN is a token that can begin the body of a | |
14426 | function-definition. */ | |
14427 | ||
14428 | static bool | |
94edc4ab | 14429 | cp_parser_token_starts_function_definition_p (cp_token* token) |
a723baf1 MM |
14430 | { |
14431 | return (/* An ordinary function-body begins with an `{'. */ | |
14432 | token->type == CPP_OPEN_BRACE | |
14433 | /* A ctor-initializer begins with a `:'. */ | |
14434 | || token->type == CPP_COLON | |
14435 | /* A function-try-block begins with `try'. */ | |
14436 | || token->keyword == RID_TRY | |
14437 | /* The named return value extension begins with `return'. */ | |
14438 | || token->keyword == RID_RETURN); | |
14439 | } | |
14440 | ||
14441 | /* Returns TRUE iff the next token is the ":" or "{" beginning a class | |
14442 | definition. */ | |
14443 | ||
14444 | static bool | |
14445 | cp_parser_next_token_starts_class_definition_p (cp_parser *parser) | |
14446 | { | |
14447 | cp_token *token; | |
14448 | ||
14449 | token = cp_lexer_peek_token (parser->lexer); | |
14450 | return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON); | |
14451 | } | |
14452 | ||
d17811fd MM |
14453 | /* Returns TRUE iff the next token is the "," or ">" ending a |
14454 | template-argument. */ | |
14455 | ||
14456 | static bool | |
14457 | cp_parser_next_token_ends_template_argument_p (cp_parser *parser) | |
14458 | { | |
14459 | cp_token *token; | |
14460 | ||
14461 | token = cp_lexer_peek_token (parser->lexer); | |
14462 | return (token->type == CPP_COMMA || token->type == CPP_GREATER); | |
14463 | } | |
14464 | ||
a723baf1 MM |
14465 | /* Returns the kind of tag indicated by TOKEN, if it is a class-key, |
14466 | or none_type otherwise. */ | |
14467 | ||
14468 | static enum tag_types | |
94edc4ab | 14469 | cp_parser_token_is_class_key (cp_token* token) |
a723baf1 MM |
14470 | { |
14471 | switch (token->keyword) | |
14472 | { | |
14473 | case RID_CLASS: | |
14474 | return class_type; | |
14475 | case RID_STRUCT: | |
14476 | return record_type; | |
14477 | case RID_UNION: | |
14478 | return union_type; | |
14479 | ||
14480 | default: | |
14481 | return none_type; | |
14482 | } | |
14483 | } | |
14484 | ||
14485 | /* Issue an error message if the CLASS_KEY does not match the TYPE. */ | |
14486 | ||
14487 | static void | |
14488 | cp_parser_check_class_key (enum tag_types class_key, tree type) | |
14489 | { | |
14490 | if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type)) | |
14491 | pedwarn ("`%s' tag used in naming `%#T'", | |
14492 | class_key == union_type ? "union" | |
14493 | : class_key == record_type ? "struct" : "class", | |
14494 | type); | |
14495 | } | |
14496 | ||
cd0be382 | 14497 | /* Issue an error message if DECL is redeclared with different |
37d407a1 KL |
14498 | access than its original declaration [class.access.spec/3]. |
14499 | This applies to nested classes and nested class templates. | |
14500 | [class.mem/1]. */ | |
14501 | ||
14502 | static void cp_parser_check_access_in_redeclaration (tree decl) | |
14503 | { | |
14504 | if (!CLASS_TYPE_P (TREE_TYPE (decl))) | |
14505 | return; | |
14506 | ||
14507 | if ((TREE_PRIVATE (decl) | |
14508 | != (current_access_specifier == access_private_node)) | |
14509 | || (TREE_PROTECTED (decl) | |
14510 | != (current_access_specifier == access_protected_node))) | |
14511 | error ("%D redeclared with different access", decl); | |
14512 | } | |
14513 | ||
a723baf1 MM |
14514 | /* Look for the `template' keyword, as a syntactic disambiguator. |
14515 | Return TRUE iff it is present, in which case it will be | |
14516 | consumed. */ | |
14517 | ||
14518 | static bool | |
14519 | cp_parser_optional_template_keyword (cp_parser *parser) | |
14520 | { | |
14521 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
14522 | { | |
14523 | /* The `template' keyword can only be used within templates; | |
14524 | outside templates the parser can always figure out what is a | |
14525 | template and what is not. */ | |
14526 | if (!processing_template_decl) | |
14527 | { | |
14528 | error ("`template' (as a disambiguator) is only allowed " | |
14529 | "within templates"); | |
14530 | /* If this part of the token stream is rescanned, the same | |
14531 | error message would be generated. So, we purge the token | |
14532 | from the stream. */ | |
14533 | cp_lexer_purge_token (parser->lexer); | |
14534 | return false; | |
14535 | } | |
14536 | else | |
14537 | { | |
14538 | /* Consume the `template' keyword. */ | |
14539 | cp_lexer_consume_token (parser->lexer); | |
14540 | return true; | |
14541 | } | |
14542 | } | |
14543 | ||
14544 | return false; | |
14545 | } | |
14546 | ||
2050a1bb MM |
14547 | /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token, |
14548 | set PARSER->SCOPE, and perform other related actions. */ | |
14549 | ||
14550 | static void | |
14551 | cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser) | |
14552 | { | |
14553 | tree value; | |
14554 | tree check; | |
14555 | ||
14556 | /* Get the stored value. */ | |
14557 | value = cp_lexer_consume_token (parser->lexer)->value; | |
14558 | /* Perform any access checks that were deferred. */ | |
14559 | for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check)) | |
cf22909c | 14560 | perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check)); |
2050a1bb MM |
14561 | /* Set the scope from the stored value. */ |
14562 | parser->scope = TREE_VALUE (value); | |
14563 | parser->qualifying_scope = TREE_TYPE (value); | |
14564 | parser->object_scope = NULL_TREE; | |
14565 | } | |
14566 | ||
a723baf1 MM |
14567 | /* Add tokens to CACHE until an non-nested END token appears. */ |
14568 | ||
14569 | static void | |
14570 | cp_parser_cache_group (cp_parser *parser, | |
14571 | cp_token_cache *cache, | |
14572 | enum cpp_ttype end, | |
14573 | unsigned depth) | |
14574 | { | |
14575 | while (true) | |
14576 | { | |
14577 | cp_token *token; | |
14578 | ||
14579 | /* Abort a parenthesized expression if we encounter a brace. */ | |
14580 | if ((end == CPP_CLOSE_PAREN || depth == 0) | |
14581 | && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
14582 | return; | |
14583 | /* Consume the next token. */ | |
14584 | token = cp_lexer_consume_token (parser->lexer); | |
14585 | /* If we've reached the end of the file, stop. */ | |
14586 | if (token->type == CPP_EOF) | |
14587 | return; | |
14588 | /* Add this token to the tokens we are saving. */ | |
14589 | cp_token_cache_push_token (cache, token); | |
14590 | /* See if it starts a new group. */ | |
14591 | if (token->type == CPP_OPEN_BRACE) | |
14592 | { | |
14593 | cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1); | |
14594 | if (depth == 0) | |
14595 | return; | |
14596 | } | |
14597 | else if (token->type == CPP_OPEN_PAREN) | |
14598 | cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1); | |
14599 | else if (token->type == end) | |
14600 | return; | |
14601 | } | |
14602 | } | |
14603 | ||
14604 | /* Begin parsing tentatively. We always save tokens while parsing | |
14605 | tentatively so that if the tentative parsing fails we can restore the | |
14606 | tokens. */ | |
14607 | ||
14608 | static void | |
94edc4ab | 14609 | cp_parser_parse_tentatively (cp_parser* parser) |
a723baf1 MM |
14610 | { |
14611 | /* Enter a new parsing context. */ | |
14612 | parser->context = cp_parser_context_new (parser->context); | |
14613 | /* Begin saving tokens. */ | |
14614 | cp_lexer_save_tokens (parser->lexer); | |
14615 | /* In order to avoid repetitive access control error messages, | |
14616 | access checks are queued up until we are no longer parsing | |
14617 | tentatively. */ | |
8d241e0b | 14618 | push_deferring_access_checks (dk_deferred); |
a723baf1 MM |
14619 | } |
14620 | ||
14621 | /* Commit to the currently active tentative parse. */ | |
14622 | ||
14623 | static void | |
94edc4ab | 14624 | cp_parser_commit_to_tentative_parse (cp_parser* parser) |
a723baf1 MM |
14625 | { |
14626 | cp_parser_context *context; | |
14627 | cp_lexer *lexer; | |
14628 | ||
14629 | /* Mark all of the levels as committed. */ | |
14630 | lexer = parser->lexer; | |
14631 | for (context = parser->context; context->next; context = context->next) | |
14632 | { | |
14633 | if (context->status == CP_PARSER_STATUS_KIND_COMMITTED) | |
14634 | break; | |
14635 | context->status = CP_PARSER_STATUS_KIND_COMMITTED; | |
14636 | while (!cp_lexer_saving_tokens (lexer)) | |
14637 | lexer = lexer->next; | |
14638 | cp_lexer_commit_tokens (lexer); | |
14639 | } | |
14640 | } | |
14641 | ||
14642 | /* Abort the currently active tentative parse. All consumed tokens | |
14643 | will be rolled back, and no diagnostics will be issued. */ | |
14644 | ||
14645 | static void | |
94edc4ab | 14646 | cp_parser_abort_tentative_parse (cp_parser* parser) |
a723baf1 MM |
14647 | { |
14648 | cp_parser_simulate_error (parser); | |
14649 | /* Now, pretend that we want to see if the construct was | |
14650 | successfully parsed. */ | |
14651 | cp_parser_parse_definitely (parser); | |
14652 | } | |
14653 | ||
34cd5ae7 | 14654 | /* Stop parsing tentatively. If a parse error has occurred, restore the |
a723baf1 MM |
14655 | token stream. Otherwise, commit to the tokens we have consumed. |
14656 | Returns true if no error occurred; false otherwise. */ | |
14657 | ||
14658 | static bool | |
94edc4ab | 14659 | cp_parser_parse_definitely (cp_parser* parser) |
a723baf1 MM |
14660 | { |
14661 | bool error_occurred; | |
14662 | cp_parser_context *context; | |
14663 | ||
34cd5ae7 | 14664 | /* Remember whether or not an error occurred, since we are about to |
a723baf1 MM |
14665 | destroy that information. */ |
14666 | error_occurred = cp_parser_error_occurred (parser); | |
14667 | /* Remove the topmost context from the stack. */ | |
14668 | context = parser->context; | |
14669 | parser->context = context->next; | |
14670 | /* If no parse errors occurred, commit to the tentative parse. */ | |
14671 | if (!error_occurred) | |
14672 | { | |
14673 | /* Commit to the tokens read tentatively, unless that was | |
14674 | already done. */ | |
14675 | if (context->status != CP_PARSER_STATUS_KIND_COMMITTED) | |
14676 | cp_lexer_commit_tokens (parser->lexer); | |
cf22909c KL |
14677 | |
14678 | pop_to_parent_deferring_access_checks (); | |
a723baf1 MM |
14679 | } |
14680 | /* Otherwise, if errors occurred, roll back our state so that things | |
14681 | are just as they were before we began the tentative parse. */ | |
14682 | else | |
cf22909c KL |
14683 | { |
14684 | cp_lexer_rollback_tokens (parser->lexer); | |
14685 | pop_deferring_access_checks (); | |
14686 | } | |
e5976695 MM |
14687 | /* Add the context to the front of the free list. */ |
14688 | context->next = cp_parser_context_free_list; | |
14689 | cp_parser_context_free_list = context; | |
14690 | ||
14691 | return !error_occurred; | |
a723baf1 MM |
14692 | } |
14693 | ||
a723baf1 MM |
14694 | /* Returns true if we are parsing tentatively -- but have decided that |
14695 | we will stick with this tentative parse, even if errors occur. */ | |
14696 | ||
14697 | static bool | |
94edc4ab | 14698 | cp_parser_committed_to_tentative_parse (cp_parser* parser) |
a723baf1 MM |
14699 | { |
14700 | return (cp_parser_parsing_tentatively (parser) | |
14701 | && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED); | |
14702 | } | |
14703 | ||
4de8668e | 14704 | /* Returns nonzero iff an error has occurred during the most recent |
a723baf1 MM |
14705 | tentative parse. */ |
14706 | ||
14707 | static bool | |
94edc4ab | 14708 | cp_parser_error_occurred (cp_parser* parser) |
a723baf1 MM |
14709 | { |
14710 | return (cp_parser_parsing_tentatively (parser) | |
14711 | && parser->context->status == CP_PARSER_STATUS_KIND_ERROR); | |
14712 | } | |
14713 | ||
4de8668e | 14714 | /* Returns nonzero if GNU extensions are allowed. */ |
a723baf1 MM |
14715 | |
14716 | static bool | |
94edc4ab | 14717 | cp_parser_allow_gnu_extensions_p (cp_parser* parser) |
a723baf1 MM |
14718 | { |
14719 | return parser->allow_gnu_extensions_p; | |
14720 | } | |
14721 | ||
14722 | \f | |
14723 | ||
14724 | /* The parser. */ | |
14725 | ||
14726 | static GTY (()) cp_parser *the_parser; | |
14727 | ||
14728 | /* External interface. */ | |
14729 | ||
d1bd0ded | 14730 | /* Parse one entire translation unit. */ |
a723baf1 | 14731 | |
d1bd0ded GK |
14732 | void |
14733 | c_parse_file (void) | |
a723baf1 MM |
14734 | { |
14735 | bool error_occurred; | |
14736 | ||
14737 | the_parser = cp_parser_new (); | |
78757caa KL |
14738 | push_deferring_access_checks (flag_access_control |
14739 | ? dk_no_deferred : dk_no_check); | |
a723baf1 MM |
14740 | error_occurred = cp_parser_translation_unit (the_parser); |
14741 | the_parser = NULL; | |
a723baf1 MM |
14742 | } |
14743 | ||
14744 | /* Clean up after parsing the entire translation unit. */ | |
14745 | ||
14746 | void | |
94edc4ab | 14747 | free_parser_stacks (void) |
a723baf1 MM |
14748 | { |
14749 | /* Nothing to do. */ | |
14750 | } | |
14751 | ||
14752 | /* This variable must be provided by every front end. */ | |
14753 | ||
14754 | int yydebug; | |
14755 | ||
14756 | #include "gt-cp-parser.h" |