]>
Commit | Line | Data |
---|---|---|
a723baf1 | 1 | /* C++ Parser. |
4514aa8c NS |
2 | Copyright (C) 2000, 2001, 2002, 2003, 2004, |
3 | 2005 Free Software Foundation, Inc. | |
a723baf1 MM |
4 | Written by Mark Mitchell <mark@codesourcery.com>. |
5 | ||
f5adbb8d | 6 | This file is part of GCC. |
a723baf1 | 7 | |
f5adbb8d | 8 | GCC is free software; you can redistribute it and/or modify it |
a723baf1 MM |
9 | under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
f5adbb8d | 13 | GCC is distributed in the hope that it will be useful, but |
a723baf1 MM |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
f5adbb8d | 19 | along with GCC; see the file COPYING. If not, write to the Free |
a723baf1 MM |
20 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
21 | 02111-1307, USA. */ | |
22 | ||
23 | #include "config.h" | |
24 | #include "system.h" | |
25 | #include "coretypes.h" | |
26 | #include "tm.h" | |
27 | #include "dyn-string.h" | |
28 | #include "varray.h" | |
29 | #include "cpplib.h" | |
30 | #include "tree.h" | |
31 | #include "cp-tree.h" | |
32 | #include "c-pragma.h" | |
33 | #include "decl.h" | |
34 | #include "flags.h" | |
35 | #include "diagnostic.h" | |
a723baf1 MM |
36 | #include "toplev.h" |
37 | #include "output.h" | |
62d1db17 | 38 | #include "target.h" |
e58a9aa1 | 39 | #include "c-common.h" |
a723baf1 MM |
40 | |
41 | \f | |
42 | /* The lexer. */ | |
43 | ||
c162c75e MA |
44 | /* The cp_lexer_* routines mediate between the lexer proper (in libcpp |
45 | and c-lex.c) and the C++ parser. */ | |
a723baf1 MM |
46 | |
47 | /* A C++ token. */ | |
48 | ||
49 | typedef struct cp_token GTY (()) | |
50 | { | |
51 | /* The kind of token. */ | |
df2b750f | 52 | ENUM_BITFIELD (cpp_ttype) type : 8; |
a723baf1 MM |
53 | /* If this token is a keyword, this value indicates which keyword. |
54 | Otherwise, this value is RID_MAX. */ | |
df2b750f | 55 | ENUM_BITFIELD (rid) keyword : 8; |
f4abade9 GB |
56 | /* Token flags. */ |
57 | unsigned char flags; | |
03fd3f84 | 58 | /* True if this token is from a system header. */ |
c162c75e | 59 | BOOL_BITFIELD in_system_header : 1; |
7d381002 MA |
60 | /* True if this token is from a context where it is implicitly extern "C" */ |
61 | BOOL_BITFIELD implicit_extern_c : 1; | |
522df488 DN |
62 | /* The value associated with this token, if any. */ |
63 | tree value; | |
82a98427 NS |
64 | /* The location at which this token was found. */ |
65 | location_t location; | |
a723baf1 MM |
66 | } cp_token; |
67 | ||
0c5e4866 NS |
68 | /* We use a stack of token pointer for saving token sets. */ |
69 | typedef struct cp_token *cp_token_position; | |
d4e6fecb NS |
70 | DEF_VEC_P (cp_token_position); |
71 | DEF_VEC_ALLOC_P (cp_token_position,heap); | |
0c5e4866 | 72 | |
76aebc9f NS |
73 | static const cp_token eof_token = |
74 | { | |
75 | CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE, | |
76 | #if USE_MAPPED_LOCATION | |
77 | 0 | |
78 | #else | |
79 | {0, 0} | |
80 | #endif | |
81 | }; | |
0c5e4866 | 82 | |
a723baf1 MM |
83 | /* The cp_lexer structure represents the C++ lexer. It is responsible |
84 | for managing the token stream from the preprocessor and supplying | |
c162c75e | 85 | it to the parser. Tokens are never added to the cp_lexer after |
03fd3f84 | 86 | it is created. */ |
a723baf1 MM |
87 | |
88 | typedef struct cp_lexer GTY (()) | |
89 | { | |
0c5e4866 NS |
90 | /* The memory allocated for the buffer. NULL if this lexer does not |
91 | own the token buffer. */ | |
76aebc9f NS |
92 | cp_token * GTY ((length ("%h.buffer_length"))) buffer; |
93 | /* If the lexer owns the buffer, this is the number of tokens in the | |
94 | buffer. */ | |
95 | size_t buffer_length; | |
0c5e4866 | 96 | |
c162c75e | 97 | /* A pointer just past the last available token. The tokens |
03fd3f84 | 98 | in this lexer are [buffer, last_token). */ |
0c5e4866 | 99 | cp_token_position GTY ((skip)) last_token; |
c162c75e | 100 | |
0c5e4866 | 101 | /* The next available token. If NEXT_TOKEN is &eof_token, then there are |
a723baf1 | 102 | no more available tokens. */ |
0c5e4866 | 103 | cp_token_position GTY ((skip)) next_token; |
a723baf1 MM |
104 | |
105 | /* A stack indicating positions at which cp_lexer_save_tokens was | |
106 | called. The top entry is the most recent position at which we | |
0c5e4866 NS |
107 | began saving tokens. If the stack is non-empty, we are saving |
108 | tokens. */ | |
d4e6fecb | 109 | VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens; |
a723baf1 | 110 | |
a723baf1 MM |
111 | /* True if we should output debugging information. */ |
112 | bool debugging_p; | |
113 | ||
114 | /* The next lexer in a linked list of lexers. */ | |
115 | struct cp_lexer *next; | |
116 | } cp_lexer; | |
117 | ||
c162c75e MA |
118 | /* cp_token_cache is a range of tokens. There is no need to represent |
119 | allocate heap memory for it, since tokens are never removed from the | |
120 | lexer's array. There is also no need for the GC to walk through | |
121 | a cp_token_cache, since everything in here is referenced through | |
03fd3f84 | 122 | a lexer. */ |
c162c75e MA |
123 | |
124 | typedef struct cp_token_cache GTY(()) | |
125 | { | |
03fd3f84 | 126 | /* The beginning of the token range. */ |
c162c75e MA |
127 | cp_token * GTY((skip)) first; |
128 | ||
03fd3f84 | 129 | /* Points immediately after the last token in the range. */ |
c162c75e MA |
130 | cp_token * GTY ((skip)) last; |
131 | } cp_token_cache; | |
132 | ||
a723baf1 MM |
133 | /* Prototypes. */ |
134 | ||
17211ab5 | 135 | static cp_lexer *cp_lexer_new_main |
94edc4ab | 136 | (void); |
a723baf1 | 137 | static cp_lexer *cp_lexer_new_from_tokens |
c162c75e MA |
138 | (cp_token_cache *tokens); |
139 | static void cp_lexer_destroy | |
140 | (cp_lexer *); | |
a723baf1 | 141 | static int cp_lexer_saving_tokens |
94edc4ab | 142 | (const cp_lexer *); |
0c5e4866 NS |
143 | static cp_token_position cp_lexer_token_position |
144 | (cp_lexer *, bool); | |
145 | static cp_token *cp_lexer_token_at | |
146 | (cp_lexer *, cp_token_position); | |
a723baf1 | 147 | static void cp_lexer_get_preprocessor_token |
94edc4ab | 148 | (cp_lexer *, cp_token *); |
c162c75e MA |
149 | static inline cp_token *cp_lexer_peek_token |
150 | (cp_lexer *); | |
a723baf1 | 151 | static cp_token *cp_lexer_peek_nth_token |
94edc4ab | 152 | (cp_lexer *, size_t); |
f7b5ecd9 | 153 | static inline bool cp_lexer_next_token_is |
94edc4ab | 154 | (cp_lexer *, enum cpp_ttype); |
a723baf1 | 155 | static bool cp_lexer_next_token_is_not |
94edc4ab | 156 | (cp_lexer *, enum cpp_ttype); |
a723baf1 | 157 | static bool cp_lexer_next_token_is_keyword |
94edc4ab | 158 | (cp_lexer *, enum rid); |
21526606 | 159 | static cp_token *cp_lexer_consume_token |
94edc4ab | 160 | (cp_lexer *); |
a723baf1 MM |
161 | static void cp_lexer_purge_token |
162 | (cp_lexer *); | |
163 | static void cp_lexer_purge_tokens_after | |
0c5e4866 | 164 | (cp_lexer *, cp_token_position); |
c162c75e MA |
165 | static void cp_lexer_handle_pragma |
166 | (cp_lexer *); | |
a723baf1 | 167 | static void cp_lexer_save_tokens |
94edc4ab | 168 | (cp_lexer *); |
a723baf1 | 169 | static void cp_lexer_commit_tokens |
94edc4ab | 170 | (cp_lexer *); |
a723baf1 | 171 | static void cp_lexer_rollback_tokens |
94edc4ab | 172 | (cp_lexer *); |
6983ea08 | 173 | #ifdef ENABLE_CHECKING |
a723baf1 | 174 | static void cp_lexer_print_token |
94edc4ab | 175 | (FILE *, cp_token *); |
21526606 | 176 | static inline bool cp_lexer_debugging_p |
94edc4ab | 177 | (cp_lexer *); |
a723baf1 | 178 | static void cp_lexer_start_debugging |
94edc4ab | 179 | (cp_lexer *) ATTRIBUTE_UNUSED; |
a723baf1 | 180 | static void cp_lexer_stop_debugging |
94edc4ab | 181 | (cp_lexer *) ATTRIBUTE_UNUSED; |
6983ea08 | 182 | #else |
2cfe82fe ZW |
183 | /* If we define cp_lexer_debug_stream to NULL it will provoke warnings |
184 | about passing NULL to functions that require non-NULL arguments | |
185 | (fputs, fprintf). It will never be used, so all we need is a value | |
186 | of the right type that's guaranteed not to be NULL. */ | |
187 | #define cp_lexer_debug_stream stdout | |
188 | #define cp_lexer_print_token(str, tok) (void) 0 | |
6983ea08 MA |
189 | #define cp_lexer_debugging_p(lexer) 0 |
190 | #endif /* ENABLE_CHECKING */ | |
a723baf1 | 191 | |
c162c75e MA |
192 | static cp_token_cache *cp_token_cache_new |
193 | (cp_token *, cp_token *); | |
194 | ||
a723baf1 | 195 | /* Manifest constants. */ |
c162c75e | 196 | #define CP_LEXER_BUFFER_SIZE 10000 |
0c5e4866 | 197 | #define CP_SAVED_TOKEN_STACK 5 |
a723baf1 MM |
198 | |
199 | /* A token type for keywords, as opposed to ordinary identifiers. */ | |
200 | #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1)) | |
201 | ||
202 | /* A token type for template-ids. If a template-id is processed while | |
203 | parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token; | |
204 | the value of the CPP_TEMPLATE_ID is whatever was returned by | |
205 | cp_parser_template_id. */ | |
206 | #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1)) | |
207 | ||
208 | /* A token type for nested-name-specifiers. If a | |
209 | nested-name-specifier is processed while parsing tentatively, it is | |
210 | replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the | |
211 | CPP_NESTED_NAME_SPECIFIER is whatever was returned by | |
212 | cp_parser_nested_name_specifier_opt. */ | |
213 | #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1)) | |
214 | ||
215 | /* A token type for tokens that are not tokens at all; these are used | |
c162c75e | 216 | to represent slots in the array where there used to be a token |
03fd3f84 | 217 | that has now been deleted. */ |
b8b94c5b PB |
218 | #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1)) |
219 | ||
220 | /* The number of token types, including C++-specific ones. */ | |
221 | #define N_CP_TTYPES ((int) (CPP_PURGED + 1)) | |
a723baf1 MM |
222 | |
223 | /* Variables. */ | |
224 | ||
6983ea08 | 225 | #ifdef ENABLE_CHECKING |
a723baf1 MM |
226 | /* The stream to which debugging output should be written. */ |
227 | static FILE *cp_lexer_debug_stream; | |
6983ea08 | 228 | #endif /* ENABLE_CHECKING */ |
a723baf1 | 229 | |
17211ab5 GK |
230 | /* Create a new main C++ lexer, the lexer that gets tokens from the |
231 | preprocessor. */ | |
a723baf1 MM |
232 | |
233 | static cp_lexer * | |
17211ab5 | 234 | cp_lexer_new_main (void) |
a723baf1 | 235 | { |
17211ab5 | 236 | cp_token first_token; |
76aebc9f NS |
237 | cp_lexer *lexer; |
238 | cp_token *pos; | |
239 | size_t alloc; | |
240 | size_t space; | |
241 | cp_token *buffer; | |
17211ab5 | 242 | |
3d8a8aad MS |
243 | /* It's possible that lexing the first token will load a PCH file, |
244 | which is a GC collection point. So we have to grab the first | |
245 | token before allocating any memory. Pragmas must not be deferred | |
246 | as -fpch-preprocess can generate a pragma to load the PCH file in | |
247 | the preprocessed output used by -save-temps. */ | |
248 | cp_lexer_get_preprocessor_token (NULL, &first_token); | |
249 | ||
03fd3f84 | 250 | /* Tell cpplib we want CPP_PRAGMA tokens. */ |
c162c75e MA |
251 | cpp_get_options (parse_in)->defer_pragmas = true; |
252 | ||
253 | /* Tell c_lex not to merge string constants. */ | |
254 | c_lex_return_raw_strings = true; | |
255 | ||
18c81520 | 256 | c_common_no_more_pch (); |
a723baf1 MM |
257 | |
258 | /* Allocate the memory. */ | |
99dd239f | 259 | lexer = GGC_CNEW (cp_lexer); |
a723baf1 | 260 | |
6983ea08 | 261 | #ifdef ENABLE_CHECKING |
c162c75e | 262 | /* Initially we are not debugging. */ |
a723baf1 | 263 | lexer->debugging_p = false; |
6983ea08 | 264 | #endif /* ENABLE_CHECKING */ |
d4e6fecb NS |
265 | lexer->saved_tokens = VEC_alloc (cp_token_position, heap, |
266 | CP_SAVED_TOKEN_STACK); | |
76aebc9f NS |
267 | |
268 | /* Create the buffer. */ | |
269 | alloc = CP_LEXER_BUFFER_SIZE; | |
270 | buffer = ggc_alloc (alloc * sizeof (cp_token)); | |
a723baf1 | 271 | |
76aebc9f NS |
272 | /* Put the first token in the buffer. */ |
273 | space = alloc; | |
274 | pos = buffer; | |
275 | *pos = first_token; | |
276 | ||
03fd3f84 | 277 | /* Get the remaining tokens from the preprocessor. */ |
76aebc9f | 278 | while (pos->type != CPP_EOF) |
c162c75e | 279 | { |
76aebc9f NS |
280 | pos++; |
281 | if (!--space) | |
282 | { | |
283 | space = alloc; | |
284 | alloc *= 2; | |
285 | buffer = ggc_realloc (buffer, alloc * sizeof (cp_token)); | |
286 | pos = buffer + space; | |
287 | } | |
288 | cp_lexer_get_preprocessor_token (lexer, pos); | |
c162c75e | 289 | } |
76aebc9f NS |
290 | lexer->buffer = buffer; |
291 | lexer->buffer_length = alloc - space; | |
292 | lexer->last_token = pos; | |
293 | lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token; | |
c162c75e MA |
294 | |
295 | /* Pragma processing (via cpp_handle_deferred_pragma) may result in | |
296 | direct calls to c_lex. Those callers all expect c_lex to do | |
297 | string constant concatenation. */ | |
298 | c_lex_return_raw_strings = false; | |
299 | ||
2cfe82fe | 300 | gcc_assert (lexer->next_token->type != CPP_PURGED); |
a723baf1 MM |
301 | return lexer; |
302 | } | |
303 | ||
c162c75e | 304 | /* Create a new lexer whose token stream is primed with the tokens in |
2cfe82fe | 305 | CACHE. When these tokens are exhausted, no new tokens will be read. */ |
a723baf1 MM |
306 | |
307 | static cp_lexer * | |
2cfe82fe | 308 | cp_lexer_new_from_tokens (cp_token_cache *cache) |
a723baf1 | 309 | { |
2cfe82fe ZW |
310 | cp_token *first = cache->first; |
311 | cp_token *last = cache->last; | |
c162c75e | 312 | cp_lexer *lexer = GGC_CNEW (cp_lexer); |
17211ab5 | 313 | |
0c5e4866 | 314 | /* We do not own the buffer. */ |
76aebc9f NS |
315 | lexer->buffer = NULL; |
316 | lexer->buffer_length = 0; | |
317 | lexer->next_token = first == last ? (cp_token *)&eof_token : first; | |
0c5e4866 NS |
318 | lexer->last_token = last; |
319 | ||
d4e6fecb NS |
320 | lexer->saved_tokens = VEC_alloc (cp_token_position, heap, |
321 | CP_SAVED_TOKEN_STACK); | |
21526606 | 322 | |
6983ea08 | 323 | #ifdef ENABLE_CHECKING |
c162c75e | 324 | /* Initially we are not debugging. */ |
17211ab5 | 325 | lexer->debugging_p = false; |
c162c75e | 326 | #endif |
a723baf1 | 327 | |
2cfe82fe ZW |
328 | gcc_assert (lexer->next_token->type != CPP_PURGED); |
329 | return lexer; | |
c162c75e MA |
330 | } |
331 | ||
03fd3f84 | 332 | /* Frees all resources associated with LEXER. */ |
c162c75e MA |
333 | |
334 | static void | |
335 | cp_lexer_destroy (cp_lexer *lexer) | |
336 | { | |
0c5e4866 NS |
337 | if (lexer->buffer) |
338 | ggc_free (lexer->buffer); | |
d4e6fecb | 339 | VEC_free (cp_token_position, heap, lexer->saved_tokens); |
c162c75e MA |
340 | ggc_free (lexer); |
341 | } | |
342 | ||
4de8668e | 343 | /* Returns nonzero if debugging information should be output. */ |
a723baf1 | 344 | |
6983ea08 MA |
345 | #ifdef ENABLE_CHECKING |
346 | ||
f7b5ecd9 MM |
347 | static inline bool |
348 | cp_lexer_debugging_p (cp_lexer *lexer) | |
a723baf1 | 349 | { |
f7b5ecd9 MM |
350 | return lexer->debugging_p; |
351 | } | |
352 | ||
6983ea08 MA |
353 | #endif /* ENABLE_CHECKING */ |
354 | ||
0c5e4866 NS |
355 | static inline cp_token_position |
356 | cp_lexer_token_position (cp_lexer *lexer, bool previous_p) | |
a723baf1 | 357 | { |
0c5e4866 NS |
358 | gcc_assert (!previous_p || lexer->next_token != &eof_token); |
359 | ||
360 | return lexer->next_token - previous_p; | |
a723baf1 MM |
361 | } |
362 | ||
a668c6ad | 363 | static inline cp_token * |
0c5e4866 | 364 | cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos) |
a668c6ad | 365 | { |
0c5e4866 | 366 | return pos; |
a668c6ad MM |
367 | } |
368 | ||
4de8668e | 369 | /* nonzero if we are presently saving tokens. */ |
f7b5ecd9 | 370 | |
0c5e4866 | 371 | static inline int |
94edc4ab | 372 | cp_lexer_saving_tokens (const cp_lexer* lexer) |
f7b5ecd9 | 373 | { |
0c5e4866 | 374 | return VEC_length (cp_token_position, lexer->saved_tokens) != 0; |
a723baf1 MM |
375 | } |
376 | ||
76aebc9f NS |
377 | /* Store the next token from the preprocessor in *TOKEN. Return true |
378 | if we reach EOF. */ | |
a723baf1 | 379 | |
21526606 | 380 | static void |
94edc4ab NN |
381 | cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED , |
382 | cp_token *token) | |
a723baf1 | 383 | { |
7d381002 | 384 | static int is_extern_c = 0; |
a723baf1 | 385 | |
51e63e60 | 386 | /* Get a new token from the preprocessor. */ |
b68b6828 PB |
387 | token->type |
388 | = c_lex_with_flags (&token->value, &token->location, &token->flags); | |
c162c75e | 389 | token->in_system_header = in_system_header; |
a723baf1 | 390 | |
7d381002 MA |
391 | /* On some systems, some header files are surrounded by an |
392 | implicit extern "C" block. Set a flag in the token if it | |
03fd3f84 | 393 | comes from such a header. */ |
7d381002 MA |
394 | is_extern_c += pending_lang_change; |
395 | pending_lang_change = 0; | |
396 | token->implicit_extern_c = is_extern_c > 0; | |
397 | ||
a723baf1 | 398 | /* Check to see if this token is a keyword. */ |
21526606 | 399 | if (token->type == CPP_NAME |
a723baf1 MM |
400 | && C_IS_RESERVED_WORD (token->value)) |
401 | { | |
402 | /* Mark this token as a keyword. */ | |
403 | token->type = CPP_KEYWORD; | |
404 | /* Record which keyword. */ | |
405 | token->keyword = C_RID_CODE (token->value); | |
406 | /* Update the value. Some keywords are mapped to particular | |
407 | entities, rather than simply having the value of the | |
408 | corresponding IDENTIFIER_NODE. For example, `__const' is | |
409 | mapped to `const'. */ | |
410 | token->value = ridpointers[token->keyword]; | |
411 | } | |
e58a9aa1 ZL |
412 | /* Handle Objective-C++ keywords. */ |
413 | else if (token->type == CPP_AT_NAME) | |
414 | { | |
415 | token->type = CPP_KEYWORD; | |
416 | switch (C_RID_CODE (token->value)) | |
417 | { | |
418 | /* Map 'class' to '@class', 'private' to '@private', etc. */ | |
419 | case RID_CLASS: token->keyword = RID_AT_CLASS; break; | |
420 | case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break; | |
421 | case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break; | |
422 | case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break; | |
423 | case RID_THROW: token->keyword = RID_AT_THROW; break; | |
424 | case RID_TRY: token->keyword = RID_AT_TRY; break; | |
425 | case RID_CATCH: token->keyword = RID_AT_CATCH; break; | |
426 | default: token->keyword = C_RID_CODE (token->value); | |
427 | } | |
428 | } | |
a723baf1 MM |
429 | else |
430 | token->keyword = RID_MAX; | |
431 | } | |
432 | ||
03fd3f84 | 433 | /* Update the globals input_location and in_system_header from TOKEN. */ |
2cfe82fe ZW |
434 | static inline void |
435 | cp_lexer_set_source_position_from_token (cp_token *token) | |
436 | { | |
437 | if (token->type != CPP_EOF) | |
438 | { | |
439 | input_location = token->location; | |
440 | in_system_header = token->in_system_header; | |
441 | } | |
442 | } | |
443 | ||
a723baf1 MM |
444 | /* Return a pointer to the next token in the token stream, but do not |
445 | consume it. */ | |
446 | ||
c162c75e MA |
447 | static inline cp_token * |
448 | cp_lexer_peek_token (cp_lexer *lexer) | |
a723baf1 | 449 | { |
a723baf1 | 450 | if (cp_lexer_debugging_p (lexer)) |
0c5e4866 NS |
451 | { |
452 | fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream); | |
453 | cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token); | |
454 | putc ('\n', cp_lexer_debug_stream); | |
455 | } | |
2cfe82fe | 456 | return lexer->next_token; |
a723baf1 MM |
457 | } |
458 | ||
459 | /* Return true if the next token has the indicated TYPE. */ | |
460 | ||
2cfe82fe | 461 | static inline bool |
94edc4ab | 462 | cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type) |
a723baf1 | 463 | { |
2cfe82fe | 464 | return cp_lexer_peek_token (lexer)->type == type; |
a723baf1 MM |
465 | } |
466 | ||
467 | /* Return true if the next token does not have the indicated TYPE. */ | |
468 | ||
2cfe82fe | 469 | static inline bool |
94edc4ab | 470 | cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type) |
a723baf1 MM |
471 | { |
472 | return !cp_lexer_next_token_is (lexer, type); | |
473 | } | |
474 | ||
475 | /* Return true if the next token is the indicated KEYWORD. */ | |
476 | ||
2cfe82fe | 477 | static inline bool |
94edc4ab | 478 | cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword) |
a723baf1 MM |
479 | { |
480 | cp_token *token; | |
481 | ||
482 | /* Peek at the next token. */ | |
483 | token = cp_lexer_peek_token (lexer); | |
484 | /* Check to see if it is the indicated keyword. */ | |
485 | return token->keyword == keyword; | |
486 | } | |
487 | ||
488 | /* Return a pointer to the Nth token in the token stream. If N is 1, | |
2cfe82fe ZW |
489 | then this is precisely equivalent to cp_lexer_peek_token (except |
490 | that it is not inline). One would like to disallow that case, but | |
491 | there is one case (cp_parser_nth_token_starts_template_id) where | |
492 | the caller passes a variable for N and it might be 1. */ | |
a723baf1 MM |
493 | |
494 | static cp_token * | |
94edc4ab | 495 | cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n) |
a723baf1 MM |
496 | { |
497 | cp_token *token; | |
498 | ||
499 | /* N is 1-based, not zero-based. */ | |
0c5e4866 | 500 | gcc_assert (n > 0 && lexer->next_token != &eof_token); |
a723baf1 | 501 | |
2cfe82fe ZW |
502 | if (cp_lexer_debugging_p (lexer)) |
503 | fprintf (cp_lexer_debug_stream, | |
504 | "cp_lexer: peeking ahead %ld at token: ", (long)n); | |
505 | ||
c162c75e | 506 | --n; |
a723baf1 | 507 | token = lexer->next_token; |
c162c75e | 508 | while (n != 0) |
a723baf1 | 509 | { |
c162c75e | 510 | ++token; |
0c5e4866 NS |
511 | if (token == lexer->last_token) |
512 | { | |
76aebc9f | 513 | token = (cp_token *)&eof_token; |
0c5e4866 NS |
514 | break; |
515 | } | |
516 | ||
c162c75e MA |
517 | if (token->type != CPP_PURGED) |
518 | --n; | |
a723baf1 MM |
519 | } |
520 | ||
2cfe82fe ZW |
521 | if (cp_lexer_debugging_p (lexer)) |
522 | { | |
523 | cp_lexer_print_token (cp_lexer_debug_stream, token); | |
524 | putc ('\n', cp_lexer_debug_stream); | |
525 | } | |
526 | ||
a723baf1 MM |
527 | return token; |
528 | } | |
529 | ||
2cfe82fe ZW |
530 | /* Return the next token, and advance the lexer's next_token pointer |
531 | to point to the next non-purged token. */ | |
a723baf1 MM |
532 | |
533 | static cp_token * | |
94edc4ab | 534 | cp_lexer_consume_token (cp_lexer* lexer) |
a723baf1 | 535 | { |
2cfe82fe | 536 | cp_token *token = lexer->next_token; |
a723baf1 | 537 | |
0c5e4866 NS |
538 | gcc_assert (token != &eof_token); |
539 | ||
2cfe82fe | 540 | do |
0c5e4866 NS |
541 | { |
542 | lexer->next_token++; | |
543 | if (lexer->next_token == lexer->last_token) | |
544 | { | |
76aebc9f | 545 | lexer->next_token = (cp_token *)&eof_token; |
0c5e4866 NS |
546 | break; |
547 | } | |
548 | ||
549 | } | |
2cfe82fe | 550 | while (lexer->next_token->type == CPP_PURGED); |
0c5e4866 | 551 | |
2cfe82fe | 552 | cp_lexer_set_source_position_from_token (token); |
0c5e4866 | 553 | |
a723baf1 MM |
554 | /* Provide debugging output. */ |
555 | if (cp_lexer_debugging_p (lexer)) | |
556 | { | |
2cfe82fe | 557 | fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream); |
a723baf1 | 558 | cp_lexer_print_token (cp_lexer_debug_stream, token); |
2cfe82fe | 559 | putc ('\n', cp_lexer_debug_stream); |
a723baf1 | 560 | } |
0c5e4866 | 561 | |
a723baf1 MM |
562 | return token; |
563 | } | |
564 | ||
2cfe82fe ZW |
565 | /* Permanently remove the next token from the token stream, and |
566 | advance the next_token pointer to refer to the next non-purged | |
567 | token. */ | |
a723baf1 MM |
568 | |
569 | static void | |
570 | cp_lexer_purge_token (cp_lexer *lexer) | |
571 | { | |
c162c75e | 572 | cp_token *tok = lexer->next_token; |
0c5e4866 NS |
573 | |
574 | gcc_assert (tok != &eof_token); | |
c162c75e MA |
575 | tok->type = CPP_PURGED; |
576 | tok->location = UNKNOWN_LOCATION; | |
577 | tok->value = NULL_TREE; | |
578 | tok->keyword = RID_MAX; | |
2cfe82fe ZW |
579 | |
580 | do | |
0c5e4866 NS |
581 | { |
582 | tok++; | |
583 | if (tok == lexer->last_token) | |
584 | { | |
76aebc9f | 585 | tok = (cp_token *)&eof_token; |
0c5e4866 NS |
586 | break; |
587 | } | |
588 | } | |
589 | while (tok->type == CPP_PURGED); | |
590 | lexer->next_token = tok; | |
a723baf1 MM |
591 | } |
592 | ||
c162c75e | 593 | /* Permanently remove all tokens after TOK, up to, but not |
a723baf1 MM |
594 | including, the token that will be returned next by |
595 | cp_lexer_peek_token. */ | |
596 | ||
597 | static void | |
c162c75e | 598 | cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok) |
a723baf1 | 599 | { |
0c5e4866 | 600 | cp_token *peek = lexer->next_token; |
a723baf1 | 601 | |
0c5e4866 NS |
602 | if (peek == &eof_token) |
603 | peek = lexer->last_token; | |
604 | ||
c162c75e MA |
605 | gcc_assert (tok < peek); |
606 | ||
607 | for ( tok += 1; tok != peek; tok += 1) | |
a723baf1 | 608 | { |
c162c75e MA |
609 | tok->type = CPP_PURGED; |
610 | tok->location = UNKNOWN_LOCATION; | |
611 | tok->value = NULL_TREE; | |
612 | tok->keyword = RID_MAX; | |
a723baf1 | 613 | } |
c162c75e MA |
614 | } |
615 | ||
03fd3f84 | 616 | /* Consume and handle a pragma token. */ |
c162c75e MA |
617 | static void |
618 | cp_lexer_handle_pragma (cp_lexer *lexer) | |
619 | { | |
36952dea ZW |
620 | cpp_string s; |
621 | cp_token *token = cp_lexer_consume_token (lexer); | |
622 | gcc_assert (token->type == CPP_PRAGMA); | |
623 | gcc_assert (token->value); | |
c162c75e | 624 | |
36952dea ZW |
625 | s.len = TREE_STRING_LENGTH (token->value); |
626 | s.text = (const unsigned char *) TREE_STRING_POINTER (token->value); | |
c162c75e | 627 | |
36952dea | 628 | cpp_handle_deferred_pragma (parse_in, &s); |
c162c75e | 629 | |
36952dea ZW |
630 | /* Clearing token->value here means that we will get an ICE if we |
631 | try to process this #pragma again (which should be impossible). */ | |
632 | token->value = NULL; | |
a723baf1 MM |
633 | } |
634 | ||
635 | /* Begin saving tokens. All tokens consumed after this point will be | |
636 | preserved. */ | |
637 | ||
638 | static void | |
94edc4ab | 639 | cp_lexer_save_tokens (cp_lexer* lexer) |
a723baf1 MM |
640 | { |
641 | /* Provide debugging output. */ | |
642 | if (cp_lexer_debugging_p (lexer)) | |
643 | fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n"); | |
644 | ||
d4e6fecb NS |
645 | VEC_safe_push (cp_token_position, heap, |
646 | lexer->saved_tokens, lexer->next_token); | |
a723baf1 MM |
647 | } |
648 | ||
649 | /* Commit to the portion of the token stream most recently saved. */ | |
650 | ||
651 | static void | |
94edc4ab | 652 | cp_lexer_commit_tokens (cp_lexer* lexer) |
a723baf1 MM |
653 | { |
654 | /* Provide debugging output. */ | |
655 | if (cp_lexer_debugging_p (lexer)) | |
656 | fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n"); | |
657 | ||
0c5e4866 | 658 | VEC_pop (cp_token_position, lexer->saved_tokens); |
a723baf1 MM |
659 | } |
660 | ||
661 | /* Return all tokens saved since the last call to cp_lexer_save_tokens | |
662 | to the token stream. Stop saving tokens. */ | |
663 | ||
664 | static void | |
94edc4ab | 665 | cp_lexer_rollback_tokens (cp_lexer* lexer) |
a723baf1 | 666 | { |
a723baf1 MM |
667 | /* Provide debugging output. */ |
668 | if (cp_lexer_debugging_p (lexer)) | |
669 | fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n"); | |
670 | ||
0c5e4866 | 671 | lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens); |
a723baf1 MM |
672 | } |
673 | ||
a723baf1 MM |
674 | /* Print a representation of the TOKEN on the STREAM. */ |
675 | ||
6983ea08 MA |
676 | #ifdef ENABLE_CHECKING |
677 | ||
a723baf1 | 678 | static void |
c162c75e MA |
679 | cp_lexer_print_token (FILE * stream, cp_token *token) |
680 | { | |
681 | /* We don't use cpp_type2name here because the parser defines | |
682 | a few tokens of its own. */ | |
683 | static const char *const token_names[] = { | |
684 | /* cpplib-defined token types */ | |
685 | #define OP(e, s) #e, | |
686 | #define TK(e, s) #e, | |
687 | TTYPE_TABLE | |
688 | #undef OP | |
689 | #undef TK | |
690 | /* C++ parser token types - see "Manifest constants", above. */ | |
691 | "KEYWORD", | |
692 | "TEMPLATE_ID", | |
693 | "NESTED_NAME_SPECIFIER", | |
694 | "PURGED" | |
695 | }; | |
696 | ||
697 | /* If we have a name for the token, print it out. Otherwise, we | |
698 | simply give the numeric code. */ | |
699 | gcc_assert (token->type < ARRAY_SIZE(token_names)); | |
700 | fputs (token_names[token->type], stream); | |
a723baf1 | 701 | |
c162c75e | 702 | /* For some tokens, print the associated data. */ |
a723baf1 MM |
703 | switch (token->type) |
704 | { | |
c162c75e MA |
705 | case CPP_KEYWORD: |
706 | /* Some keywords have a value that is not an IDENTIFIER_NODE. | |
707 | For example, `struct' is mapped to an INTEGER_CST. */ | |
708 | if (TREE_CODE (token->value) != IDENTIFIER_NODE) | |
709 | break; | |
710 | /* else fall through */ | |
a723baf1 | 711 | case CPP_NAME: |
c162c75e | 712 | fputs (IDENTIFIER_POINTER (token->value), stream); |
a723baf1 MM |
713 | break; |
714 | ||
c162c75e MA |
715 | case CPP_STRING: |
716 | case CPP_WSTRING: | |
717 | case CPP_PRAGMA: | |
718 | fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value)); | |
a723baf1 MM |
719 | break; |
720 | ||
a723baf1 MM |
721 | default: |
722 | break; | |
723 | } | |
a723baf1 MM |
724 | } |
725 | ||
a723baf1 MM |
726 | /* Start emitting debugging information. */ |
727 | ||
728 | static void | |
94edc4ab | 729 | cp_lexer_start_debugging (cp_lexer* lexer) |
a723baf1 | 730 | { |
81122c44 | 731 | lexer->debugging_p = true; |
a723baf1 | 732 | } |
21526606 | 733 | |
a723baf1 MM |
734 | /* Stop emitting debugging information. */ |
735 | ||
736 | static void | |
94edc4ab | 737 | cp_lexer_stop_debugging (cp_lexer* lexer) |
a723baf1 | 738 | { |
81122c44 | 739 | lexer->debugging_p = false; |
a723baf1 MM |
740 | } |
741 | ||
6983ea08 MA |
742 | #endif /* ENABLE_CHECKING */ |
743 | ||
03fd3f84 | 744 | /* Create a new cp_token_cache, representing a range of tokens. */ |
c162c75e MA |
745 | |
746 | static cp_token_cache * | |
747 | cp_token_cache_new (cp_token *first, cp_token *last) | |
748 | { | |
749 | cp_token_cache *cache = GGC_NEW (cp_token_cache); | |
750 | cache->first = first; | |
751 | cache->last = last; | |
752 | return cache; | |
753 | } | |
754 | ||
a723baf1 | 755 | \f |
62d1db17 MM |
756 | /* Decl-specifiers. */ |
757 | ||
758 | static void clear_decl_specs | |
759 | (cp_decl_specifier_seq *); | |
760 | ||
761 | /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */ | |
762 | ||
763 | static void | |
764 | clear_decl_specs (cp_decl_specifier_seq *decl_specs) | |
765 | { | |
766 | memset (decl_specs, 0, sizeof (cp_decl_specifier_seq)); | |
767 | } | |
768 | ||
058b15c1 MM |
769 | /* Declarators. */ |
770 | ||
771 | /* Nothing other than the parser should be creating declarators; | |
772 | declarators are a semi-syntactic representation of C++ entities. | |
773 | Other parts of the front end that need to create entities (like | |
774 | VAR_DECLs or FUNCTION_DECLs) should do that directly. */ | |
775 | ||
98ca843c | 776 | static cp_declarator *make_call_declarator |
3c01e5df | 777 | (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree); |
98ca843c | 778 | static cp_declarator *make_array_declarator |
058b15c1 | 779 | (cp_declarator *, tree); |
98ca843c | 780 | static cp_declarator *make_pointer_declarator |
3c01e5df | 781 | (cp_cv_quals, cp_declarator *); |
98ca843c | 782 | static cp_declarator *make_reference_declarator |
3c01e5df | 783 | (cp_cv_quals, cp_declarator *); |
98ca843c | 784 | static cp_parameter_declarator *make_parameter_declarator |
62d1db17 | 785 | (cp_decl_specifier_seq *, cp_declarator *, tree); |
98ca843c | 786 | static cp_declarator *make_ptrmem_declarator |
3c01e5df | 787 | (cp_cv_quals, tree, cp_declarator *); |
058b15c1 MM |
788 | |
789 | cp_declarator *cp_error_declarator; | |
790 | ||
791 | /* The obstack on which declarators and related data structures are | |
792 | allocated. */ | |
793 | static struct obstack declarator_obstack; | |
794 | ||
795 | /* Alloc BYTES from the declarator memory pool. */ | |
796 | ||
797 | static inline void * | |
798 | alloc_declarator (size_t bytes) | |
799 | { | |
800 | return obstack_alloc (&declarator_obstack, bytes); | |
801 | } | |
802 | ||
803 | /* Allocate a declarator of the indicated KIND. Clear fields that are | |
804 | common to all declarators. */ | |
805 | ||
806 | static cp_declarator * | |
807 | make_declarator (cp_declarator_kind kind) | |
808 | { | |
809 | cp_declarator *declarator; | |
810 | ||
811 | declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator)); | |
812 | declarator->kind = kind; | |
813 | declarator->attributes = NULL_TREE; | |
814 | declarator->declarator = NULL; | |
815 | ||
816 | return declarator; | |
817 | } | |
818 | ||
1d786913 MM |
819 | /* Make a declarator for a generalized identifier. If non-NULL, the |
820 | identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is | |
821 | just UNQUALIFIED_NAME. */ | |
058b15c1 | 822 | |
1d786913 MM |
823 | static cp_declarator * |
824 | make_id_declarator (tree qualifying_scope, tree unqualified_name) | |
058b15c1 MM |
825 | { |
826 | cp_declarator *declarator; | |
98ca843c | 827 | |
1d786913 MM |
828 | /* It is valid to write: |
829 | ||
830 | class C { void f(); }; | |
831 | typedef C D; | |
832 | void D::f(); | |
833 | ||
834 | The standard is not clear about whether `typedef const C D' is | |
835 | legal; as of 2002-09-15 the committee is considering that | |
836 | question. EDG 3.0 allows that syntax. Therefore, we do as | |
837 | well. */ | |
838 | if (qualifying_scope && TYPE_P (qualifying_scope)) | |
839 | qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope); | |
840 | ||
058b15c1 | 841 | declarator = make_declarator (cdk_id); |
1d786913 MM |
842 | declarator->u.id.qualifying_scope = qualifying_scope; |
843 | declarator->u.id.unqualified_name = unqualified_name; | |
058b15c1 MM |
844 | declarator->u.id.sfk = sfk_none; |
845 | ||
846 | return declarator; | |
847 | } | |
848 | ||
849 | /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list | |
850 | of modifiers such as const or volatile to apply to the pointer | |
851 | type, represented as identifiers. */ | |
852 | ||
853 | cp_declarator * | |
3c01e5df | 854 | make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target) |
058b15c1 MM |
855 | { |
856 | cp_declarator *declarator; | |
857 | ||
858 | declarator = make_declarator (cdk_pointer); | |
859 | declarator->declarator = target; | |
860 | declarator->u.pointer.qualifiers = cv_qualifiers; | |
861 | declarator->u.pointer.class_type = NULL_TREE; | |
862 | ||
863 | return declarator; | |
864 | } | |
865 | ||
866 | /* Like make_pointer_declarator -- but for references. */ | |
867 | ||
868 | cp_declarator * | |
3c01e5df | 869 | make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target) |
058b15c1 MM |
870 | { |
871 | cp_declarator *declarator; | |
872 | ||
873 | declarator = make_declarator (cdk_reference); | |
874 | declarator->declarator = target; | |
875 | declarator->u.pointer.qualifiers = cv_qualifiers; | |
876 | declarator->u.pointer.class_type = NULL_TREE; | |
877 | ||
878 | return declarator; | |
879 | } | |
880 | ||
881 | /* Like make_pointer_declarator -- but for a pointer to a non-static | |
882 | member of CLASS_TYPE. */ | |
883 | ||
884 | cp_declarator * | |
3c01e5df | 885 | make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type, |
058b15c1 MM |
886 | cp_declarator *pointee) |
887 | { | |
888 | cp_declarator *declarator; | |
889 | ||
890 | declarator = make_declarator (cdk_ptrmem); | |
891 | declarator->declarator = pointee; | |
892 | declarator->u.pointer.qualifiers = cv_qualifiers; | |
893 | declarator->u.pointer.class_type = class_type; | |
894 | ||
895 | return declarator; | |
896 | } | |
897 | ||
898 | /* Make a declarator for the function given by TARGET, with the | |
899 | indicated PARMS. The CV_QUALIFIERS aply to the function, as in | |
900 | "const"-qualified member function. The EXCEPTION_SPECIFICATION | |
901 | indicates what exceptions can be thrown. */ | |
902 | ||
903 | cp_declarator * | |
98ca843c | 904 | make_call_declarator (cp_declarator *target, |
058b15c1 | 905 | cp_parameter_declarator *parms, |
3c01e5df | 906 | cp_cv_quals cv_qualifiers, |
058b15c1 MM |
907 | tree exception_specification) |
908 | { | |
909 | cp_declarator *declarator; | |
910 | ||
911 | declarator = make_declarator (cdk_function); | |
912 | declarator->declarator = target; | |
913 | declarator->u.function.parameters = parms; | |
914 | declarator->u.function.qualifiers = cv_qualifiers; | |
915 | declarator->u.function.exception_specification = exception_specification; | |
916 | ||
917 | return declarator; | |
918 | } | |
919 | ||
920 | /* Make a declarator for an array of BOUNDS elements, each of which is | |
921 | defined by ELEMENT. */ | |
922 | ||
923 | cp_declarator * | |
924 | make_array_declarator (cp_declarator *element, tree bounds) | |
925 | { | |
926 | cp_declarator *declarator; | |
927 | ||
928 | declarator = make_declarator (cdk_array); | |
929 | declarator->declarator = element; | |
930 | declarator->u.array.bounds = bounds; | |
931 | ||
932 | return declarator; | |
933 | } | |
934 | ||
935 | cp_parameter_declarator *no_parameters; | |
936 | ||
937 | /* Create a parameter declarator with the indicated DECL_SPECIFIERS, | |
938 | DECLARATOR and DEFAULT_ARGUMENT. */ | |
939 | ||
940 | cp_parameter_declarator * | |
98ca843c | 941 | make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers, |
058b15c1 MM |
942 | cp_declarator *declarator, |
943 | tree default_argument) | |
944 | { | |
945 | cp_parameter_declarator *parameter; | |
946 | ||
98ca843c | 947 | parameter = ((cp_parameter_declarator *) |
058b15c1 MM |
948 | alloc_declarator (sizeof (cp_parameter_declarator))); |
949 | parameter->next = NULL; | |
62d1db17 MM |
950 | if (decl_specifiers) |
951 | parameter->decl_specifiers = *decl_specifiers; | |
952 | else | |
953 | clear_decl_specs (¶meter->decl_specifiers); | |
058b15c1 MM |
954 | parameter->declarator = declarator; |
955 | parameter->default_argument = default_argument; | |
956 | parameter->ellipsis_p = false; | |
957 | ||
958 | return parameter; | |
959 | } | |
960 | ||
a723baf1 MM |
961 | /* The parser. */ |
962 | ||
963 | /* Overview | |
964 | -------- | |
965 | ||
966 | A cp_parser parses the token stream as specified by the C++ | |
967 | grammar. Its job is purely parsing, not semantic analysis. For | |
968 | example, the parser breaks the token stream into declarators, | |
969 | expressions, statements, and other similar syntactic constructs. | |
970 | It does not check that the types of the expressions on either side | |
971 | of an assignment-statement are compatible, or that a function is | |
972 | not declared with a parameter of type `void'. | |
973 | ||
974 | The parser invokes routines elsewhere in the compiler to perform | |
975 | semantic analysis and to build up the abstract syntax tree for the | |
21526606 | 976 | code processed. |
a723baf1 MM |
977 | |
978 | The parser (and the template instantiation code, which is, in a | |
979 | way, a close relative of parsing) are the only parts of the | |
980 | compiler that should be calling push_scope and pop_scope, or | |
981 | related functions. The parser (and template instantiation code) | |
982 | keeps track of what scope is presently active; everything else | |
983 | should simply honor that. (The code that generates static | |
984 | initializers may also need to set the scope, in order to check | |
985 | access control correctly when emitting the initializers.) | |
986 | ||
987 | Methodology | |
988 | ----------- | |
21526606 | 989 | |
a723baf1 MM |
990 | The parser is of the standard recursive-descent variety. Upcoming |
991 | tokens in the token stream are examined in order to determine which | |
992 | production to use when parsing a non-terminal. Some C++ constructs | |
993 | require arbitrary look ahead to disambiguate. For example, it is | |
994 | impossible, in the general case, to tell whether a statement is an | |
995 | expression or declaration without scanning the entire statement. | |
996 | Therefore, the parser is capable of "parsing tentatively." When the | |
997 | parser is not sure what construct comes next, it enters this mode. | |
998 | Then, while we attempt to parse the construct, the parser queues up | |
999 | error messages, rather than issuing them immediately, and saves the | |
1000 | tokens it consumes. If the construct is parsed successfully, the | |
1001 | parser "commits", i.e., it issues any queued error messages and | |
1002 | the tokens that were being preserved are permanently discarded. | |
1003 | If, however, the construct is not parsed successfully, the parser | |
1004 | rolls back its state completely so that it can resume parsing using | |
1005 | a different alternative. | |
1006 | ||
1007 | Future Improvements | |
1008 | ------------------- | |
21526606 | 1009 | |
b8b94c5b PB |
1010 | The performance of the parser could probably be improved substantially. |
1011 | We could often eliminate the need to parse tentatively by looking ahead | |
1012 | a little bit. In some places, this approach might not entirely eliminate | |
1013 | the need to parse tentatively, but it might still speed up the average | |
1014 | case. */ | |
a723baf1 MM |
1015 | |
1016 | /* Flags that are passed to some parsing functions. These values can | |
1017 | be bitwise-ored together. */ | |
1018 | ||
1019 | typedef enum cp_parser_flags | |
1020 | { | |
1021 | /* No flags. */ | |
1022 | CP_PARSER_FLAGS_NONE = 0x0, | |
1023 | /* The construct is optional. If it is not present, then no error | |
1024 | should be issued. */ | |
1025 | CP_PARSER_FLAGS_OPTIONAL = 0x1, | |
1026 | /* When parsing a type-specifier, do not allow user-defined types. */ | |
1027 | CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2 | |
1028 | } cp_parser_flags; | |
1029 | ||
62b8a44e NS |
1030 | /* The different kinds of declarators we want to parse. */ |
1031 | ||
1032 | typedef enum cp_parser_declarator_kind | |
1033 | { | |
852dcbdd | 1034 | /* We want an abstract declarator. */ |
62b8a44e NS |
1035 | CP_PARSER_DECLARATOR_ABSTRACT, |
1036 | /* We want a named declarator. */ | |
1037 | CP_PARSER_DECLARATOR_NAMED, | |
04c06002 | 1038 | /* We don't mind, but the name must be an unqualified-id. */ |
62b8a44e NS |
1039 | CP_PARSER_DECLARATOR_EITHER |
1040 | } cp_parser_declarator_kind; | |
1041 | ||
b8b94c5b PB |
1042 | /* The precedence values used to parse binary expressions. The minimum value |
1043 | of PREC must be 1, because zero is reserved to quickly discriminate | |
1044 | binary operators from other tokens. */ | |
a723baf1 | 1045 | |
b8b94c5b | 1046 | enum cp_parser_prec |
a723baf1 | 1047 | { |
b8b94c5b PB |
1048 | PREC_NOT_OPERATOR, |
1049 | PREC_LOGICAL_OR_EXPRESSION, | |
1050 | PREC_LOGICAL_AND_EXPRESSION, | |
1051 | PREC_INCLUSIVE_OR_EXPRESSION, | |
1052 | PREC_EXCLUSIVE_OR_EXPRESSION, | |
1053 | PREC_AND_EXPRESSION, | |
b8b94c5b | 1054 | PREC_EQUALITY_EXPRESSION, |
69475123 | 1055 | PREC_RELATIONAL_EXPRESSION, |
b8b94c5b PB |
1056 | PREC_SHIFT_EXPRESSION, |
1057 | PREC_ADDITIVE_EXPRESSION, | |
1058 | PREC_MULTIPLICATIVE_EXPRESSION, | |
1059 | PREC_PM_EXPRESSION, | |
1060 | NUM_PREC_VALUES = PREC_PM_EXPRESSION | |
1061 | }; | |
a723baf1 | 1062 | |
b8b94c5b PB |
1063 | /* A mapping from a token type to a corresponding tree node type, with a |
1064 | precedence value. */ | |
a723baf1 | 1065 | |
b8b94c5b PB |
1066 | typedef struct cp_parser_binary_operations_map_node |
1067 | { | |
1068 | /* The token type. */ | |
1069 | enum cpp_ttype token_type; | |
1070 | /* The corresponding tree code. */ | |
1071 | enum tree_code tree_type; | |
1072 | /* The precedence of this operator. */ | |
1073 | enum cp_parser_prec prec; | |
1074 | } cp_parser_binary_operations_map_node; | |
a723baf1 MM |
1075 | |
1076 | /* The status of a tentative parse. */ | |
1077 | ||
1078 | typedef enum cp_parser_status_kind | |
1079 | { | |
1080 | /* No errors have occurred. */ | |
1081 | CP_PARSER_STATUS_KIND_NO_ERROR, | |
1082 | /* An error has occurred. */ | |
1083 | CP_PARSER_STATUS_KIND_ERROR, | |
1084 | /* We are committed to this tentative parse, whether or not an error | |
1085 | has occurred. */ | |
1086 | CP_PARSER_STATUS_KIND_COMMITTED | |
1087 | } cp_parser_status_kind; | |
1088 | ||
b8b94c5b PB |
1089 | typedef struct cp_parser_expression_stack_entry |
1090 | { | |
1091 | tree lhs; | |
1092 | enum tree_code tree_type; | |
1093 | int prec; | |
1094 | } cp_parser_expression_stack_entry; | |
1095 | ||
43c2a69a PB |
1096 | /* The stack for storing partial expressions. We only need NUM_PREC_VALUES |
1097 | entries because precedence levels on the stack are monotonically | |
1098 | increasing. */ | |
b8b94c5b PB |
1099 | typedef struct cp_parser_expression_stack_entry |
1100 | cp_parser_expression_stack[NUM_PREC_VALUES]; | |
a723baf1 | 1101 | |
b8b94c5b | 1102 | /* Context that is saved and restored when parsing tentatively. */ |
a723baf1 MM |
1103 | typedef struct cp_parser_context GTY (()) |
1104 | { | |
1105 | /* If this is a tentative parsing context, the status of the | |
1106 | tentative parse. */ | |
1107 | enum cp_parser_status_kind status; | |
1108 | /* If non-NULL, we have just seen a `x->' or `x.' expression. Names | |
1109 | that are looked up in this context must be looked up both in the | |
1110 | scope given by OBJECT_TYPE (the type of `x' or `*x') and also in | |
1111 | the context of the containing expression. */ | |
1112 | tree object_type; | |
b8b94c5b | 1113 | |
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 | ||
1431042e | 1127 | static GTY((deletable)) cp_parser_context* cp_parser_context_free_list; |
e5976695 | 1128 | |
b8b94c5b PB |
1129 | /* The operator-precedence table used by cp_parser_binary_expression. |
1130 | Transformed into an associative array (binops_by_token) by | |
1131 | cp_parser_new. */ | |
1132 | ||
1133 | static const cp_parser_binary_operations_map_node binops[] = { | |
1134 | { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION }, | |
1135 | { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION }, | |
1136 | ||
1137 | { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION }, | |
1138 | { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION }, | |
1139 | { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION }, | |
1140 | ||
1141 | { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION }, | |
1142 | { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION }, | |
1143 | ||
1144 | { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION }, | |
1145 | { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION }, | |
1146 | ||
1147 | { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
1148 | { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
1149 | { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
1150 | { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
1151 | { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
1152 | { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
1153 | ||
1154 | { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION }, | |
1155 | { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION }, | |
1156 | ||
1157 | { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION }, | |
1158 | ||
1159 | { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION }, | |
1160 | ||
1161 | { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION }, | |
1162 | ||
1163 | { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION }, | |
1164 | ||
1165 | { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION } | |
1166 | }; | |
1167 | ||
1168 | /* The same as binops, but initialized by cp_parser_new so that | |
1169 | binops_by_token[N].token_type == N. Used in cp_parser_binary_expression | |
1170 | for speed. */ | |
1171 | static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES]; | |
1172 | ||
a723baf1 MM |
1173 | /* Constructors and destructors. */ |
1174 | ||
1175 | /* Construct a new context. The context below this one on the stack | |
1176 | is given by NEXT. */ | |
1177 | ||
1178 | static cp_parser_context * | |
94edc4ab | 1179 | cp_parser_context_new (cp_parser_context* next) |
a723baf1 MM |
1180 | { |
1181 | cp_parser_context *context; | |
1182 | ||
1183 | /* Allocate the storage. */ | |
e5976695 MM |
1184 | if (cp_parser_context_free_list != NULL) |
1185 | { | |
1186 | /* Pull the first entry from the free list. */ | |
1187 | context = cp_parser_context_free_list; | |
1188 | cp_parser_context_free_list = context->next; | |
c68b0a84 | 1189 | memset (context, 0, sizeof (*context)); |
e5976695 MM |
1190 | } |
1191 | else | |
99dd239f | 1192 | context = GGC_CNEW (cp_parser_context); |
b8b94c5b | 1193 | |
a723baf1 MM |
1194 | /* No errors have occurred yet in this context. */ |
1195 | context->status = CP_PARSER_STATUS_KIND_NO_ERROR; | |
1196 | /* If this is not the bottomost context, copy information that we | |
1197 | need from the previous context. */ | |
1198 | if (next) | |
1199 | { | |
1200 | /* If, in the NEXT context, we are parsing an `x->' or `x.' | |
1201 | expression, then we are parsing one in this context, too. */ | |
1202 | context->object_type = next->object_type; | |
a723baf1 MM |
1203 | /* Thread the stack. */ |
1204 | context->next = next; | |
1205 | } | |
1206 | ||
1207 | return context; | |
1208 | } | |
1209 | ||
1210 | /* The cp_parser structure represents the C++ parser. */ | |
1211 | ||
1212 | typedef struct cp_parser GTY(()) | |
1213 | { | |
1214 | /* The lexer from which we are obtaining tokens. */ | |
1215 | cp_lexer *lexer; | |
1216 | ||
1217 | /* The scope in which names should be looked up. If NULL_TREE, then | |
1218 | we look up names in the scope that is currently open in the | |
1219 | source program. If non-NULL, this is either a TYPE or | |
21526606 | 1220 | NAMESPACE_DECL for the scope in which we should look. |
a723baf1 MM |
1221 | |
1222 | This value is not cleared automatically after a name is looked | |
1223 | up, so we must be careful to clear it before starting a new look | |
1224 | up sequence. (If it is not cleared, then `X::Y' followed by `Z' | |
1225 | will look up `Z' in the scope of `X', rather than the current | |
1226 | scope.) Unfortunately, it is difficult to tell when name lookup | |
1227 | is complete, because we sometimes peek at a token, look it up, | |
1228 | and then decide not to consume it. */ | |
1229 | tree scope; | |
1230 | ||
1231 | /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the | |
1232 | last lookup took place. OBJECT_SCOPE is used if an expression | |
1233 | like "x->y" or "x.y" was used; it gives the type of "*x" or "x", | |
21526606 | 1234 | respectively. QUALIFYING_SCOPE is used for an expression of the |
a723baf1 MM |
1235 | form "X::Y"; it refers to X. */ |
1236 | tree object_scope; | |
1237 | tree qualifying_scope; | |
1238 | ||
1239 | /* A stack of parsing contexts. All but the bottom entry on the | |
1240 | stack will be tentative contexts. | |
1241 | ||
1242 | We parse tentatively in order to determine which construct is in | |
1243 | use in some situations. For example, in order to determine | |
1244 | whether a statement is an expression-statement or a | |
1245 | declaration-statement we parse it tentatively as a | |
1246 | declaration-statement. If that fails, we then reparse the same | |
1247 | token stream as an expression-statement. */ | |
1248 | cp_parser_context *context; | |
1249 | ||
1250 | /* True if we are parsing GNU C++. If this flag is not set, then | |
1251 | GNU extensions are not recognized. */ | |
1252 | bool allow_gnu_extensions_p; | |
1253 | ||
1254 | /* TRUE if the `>' token should be interpreted as the greater-than | |
1255 | operator. FALSE if it is the end of a template-id or | |
1256 | template-parameter-list. */ | |
1257 | bool greater_than_is_operator_p; | |
1258 | ||
1259 | /* TRUE if default arguments are allowed within a parameter list | |
1260 | that starts at this point. FALSE if only a gnu extension makes | |
cd0be382 | 1261 | them permissible. */ |
a723baf1 | 1262 | bool default_arg_ok_p; |
21526606 | 1263 | |
a723baf1 MM |
1264 | /* TRUE if we are parsing an integral constant-expression. See |
1265 | [expr.const] for a precise definition. */ | |
67c03833 | 1266 | bool integral_constant_expression_p; |
a723baf1 | 1267 | |
14d22dd6 MM |
1268 | /* TRUE if we are parsing an integral constant-expression -- but a |
1269 | non-constant expression should be permitted as well. This flag | |
1270 | is used when parsing an array bound so that GNU variable-length | |
1271 | arrays are tolerated. */ | |
67c03833 | 1272 | bool allow_non_integral_constant_expression_p; |
14d22dd6 MM |
1273 | |
1274 | /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has | |
1275 | been seen that makes the expression non-constant. */ | |
67c03833 | 1276 | bool non_integral_constant_expression_p; |
14d22dd6 | 1277 | |
a723baf1 MM |
1278 | /* TRUE if local variable names and `this' are forbidden in the |
1279 | current context. */ | |
1280 | bool local_variables_forbidden_p; | |
1281 | ||
1282 | /* TRUE if the declaration we are parsing is part of a | |
1283 | linkage-specification of the form `extern string-literal | |
1284 | declaration'. */ | |
1285 | bool in_unbraced_linkage_specification_p; | |
1286 | ||
1287 | /* TRUE if we are presently parsing a declarator, after the | |
1288 | direct-declarator. */ | |
1289 | bool in_declarator_p; | |
1290 | ||
4bb8ca28 MM |
1291 | /* TRUE if we are presently parsing a template-argument-list. */ |
1292 | bool in_template_argument_list_p; | |
1293 | ||
0e59b3fb MM |
1294 | /* TRUE if we are presently parsing the body of an |
1295 | iteration-statement. */ | |
1296 | bool in_iteration_statement_p; | |
1297 | ||
1298 | /* TRUE if we are presently parsing the body of a switch | |
1299 | statement. */ | |
1300 | bool in_switch_statement_p; | |
1301 | ||
4f8163b1 MM |
1302 | /* TRUE if we are parsing a type-id in an expression context. In |
1303 | such a situation, both "type (expr)" and "type (type)" are valid | |
1304 | alternatives. */ | |
1305 | bool in_type_id_in_expr_p; | |
1306 | ||
7d381002 | 1307 | /* TRUE if we are currently in a header file where declarations are |
03fd3f84 | 1308 | implicitly extern "C". */ |
7d381002 MA |
1309 | bool implicit_extern_c; |
1310 | ||
c162c75e MA |
1311 | /* TRUE if strings in expressions should be translated to the execution |
1312 | character set. */ | |
1313 | bool translate_strings_p; | |
1314 | ||
a723baf1 MM |
1315 | /* If non-NULL, then we are parsing a construct where new type |
1316 | definitions are not permitted. The string stored here will be | |
1317 | issued as an error message if a type is defined. */ | |
1318 | const char *type_definition_forbidden_message; | |
1319 | ||
8db1028e NS |
1320 | /* A list of lists. The outer list is a stack, used for member |
1321 | functions of local classes. At each level there are two sub-list, | |
1322 | one on TREE_VALUE and one on TREE_PURPOSE. Each of those | |
1323 | sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their | |
1324 | TREE_VALUE's. The functions are chained in reverse declaration | |
1325 | order. | |
1326 | ||
1327 | The TREE_PURPOSE sublist contains those functions with default | |
1328 | arguments that need post processing, and the TREE_VALUE sublist | |
1329 | contains those functions with definitions that need post | |
1330 | processing. | |
1331 | ||
1332 | These lists can only be processed once the outermost class being | |
9bcb9aae | 1333 | defined is complete. */ |
a723baf1 MM |
1334 | tree unparsed_functions_queues; |
1335 | ||
1336 | /* The number of classes whose definitions are currently in | |
1337 | progress. */ | |
1338 | unsigned num_classes_being_defined; | |
1339 | ||
1340 | /* The number of template parameter lists that apply directly to the | |
1341 | current declaration. */ | |
1342 | unsigned num_template_parameter_lists; | |
1343 | } cp_parser; | |
1344 | ||
04c06002 | 1345 | /* The type of a function that parses some kind of expression. */ |
94edc4ab | 1346 | typedef tree (*cp_parser_expression_fn) (cp_parser *); |
a723baf1 MM |
1347 | |
1348 | /* Prototypes. */ | |
1349 | ||
1350 | /* Constructors and destructors. */ | |
1351 | ||
1352 | static cp_parser *cp_parser_new | |
94edc4ab | 1353 | (void); |
a723baf1 | 1354 | |
21526606 | 1355 | /* Routines to parse various constructs. |
a723baf1 MM |
1356 | |
1357 | Those that return `tree' will return the error_mark_node (rather | |
1358 | than NULL_TREE) if a parse error occurs, unless otherwise noted. | |
1359 | Sometimes, they will return an ordinary node if error-recovery was | |
34cd5ae7 | 1360 | attempted, even though a parse error occurred. So, to check |
a723baf1 MM |
1361 | whether or not a parse error occurred, you should always use |
1362 | cp_parser_error_occurred. If the construct is optional (indicated | |
1363 | either by an `_opt' in the name of the function that does the | |
1364 | parsing or via a FLAGS parameter), then NULL_TREE is returned if | |
1365 | the construct is not present. */ | |
1366 | ||
1367 | /* Lexical conventions [gram.lex] */ | |
1368 | ||
1369 | static tree cp_parser_identifier | |
94edc4ab | 1370 | (cp_parser *); |
c162c75e MA |
1371 | static tree cp_parser_string_literal |
1372 | (cp_parser *, bool, bool); | |
a723baf1 MM |
1373 | |
1374 | /* Basic concepts [gram.basic] */ | |
1375 | ||
1376 | static bool cp_parser_translation_unit | |
94edc4ab | 1377 | (cp_parser *); |
a723baf1 MM |
1378 | |
1379 | /* Expressions [gram.expr] */ | |
1380 | ||
1381 | static tree cp_parser_primary_expression | |
93678513 | 1382 | (cp_parser *, bool, cp_id_kind *, tree *); |
a723baf1 | 1383 | static tree cp_parser_id_expression |
f3c2dfc6 | 1384 | (cp_parser *, bool, bool, bool *, bool); |
a723baf1 | 1385 | static tree cp_parser_unqualified_id |
f3c2dfc6 | 1386 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1387 | static tree cp_parser_nested_name_specifier_opt |
a668c6ad | 1388 | (cp_parser *, bool, bool, bool, bool); |
a723baf1 | 1389 | static tree cp_parser_nested_name_specifier |
a723baf1 | 1390 | (cp_parser *, bool, bool, bool, bool); |
a668c6ad MM |
1391 | static tree cp_parser_class_or_namespace_name |
1392 | (cp_parser *, bool, bool, bool, bool, bool); | |
a723baf1 | 1393 | static tree cp_parser_postfix_expression |
93678513 | 1394 | (cp_parser *, bool, bool); |
7a3ea201 RH |
1395 | static tree cp_parser_postfix_open_square_expression |
1396 | (cp_parser *, tree, bool); | |
1397 | static tree cp_parser_postfix_dot_deref_expression | |
1398 | (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *); | |
7efa3e22 | 1399 | static tree cp_parser_parenthesized_expression_list |
93678513 | 1400 | (cp_parser *, bool, bool, bool *); |
a723baf1 | 1401 | static void cp_parser_pseudo_destructor_name |
94edc4ab | 1402 | (cp_parser *, tree *, tree *); |
a723baf1 | 1403 | static tree cp_parser_unary_expression |
93678513 | 1404 | (cp_parser *, bool, bool); |
a723baf1 | 1405 | static enum tree_code cp_parser_unary_operator |
94edc4ab | 1406 | (cp_token *); |
a723baf1 | 1407 | static tree cp_parser_new_expression |
94edc4ab | 1408 | (cp_parser *); |
a723baf1 | 1409 | static tree cp_parser_new_placement |
94edc4ab | 1410 | (cp_parser *); |
a723baf1 | 1411 | static tree cp_parser_new_type_id |
058b15c1 MM |
1412 | (cp_parser *, tree *); |
1413 | static cp_declarator *cp_parser_new_declarator_opt | |
94edc4ab | 1414 | (cp_parser *); |
058b15c1 | 1415 | static cp_declarator *cp_parser_direct_new_declarator |
94edc4ab | 1416 | (cp_parser *); |
a723baf1 | 1417 | static tree cp_parser_new_initializer |
94edc4ab | 1418 | (cp_parser *); |
a723baf1 | 1419 | static tree cp_parser_delete_expression |
94edc4ab | 1420 | (cp_parser *); |
21526606 | 1421 | static tree cp_parser_cast_expression |
93678513 | 1422 | (cp_parser *, bool, bool); |
b8b94c5b | 1423 | static tree cp_parser_binary_expression |
93678513 | 1424 | (cp_parser *, bool); |
a723baf1 | 1425 | static tree cp_parser_question_colon_clause |
94edc4ab | 1426 | (cp_parser *, tree); |
a723baf1 | 1427 | static tree cp_parser_assignment_expression |
93678513 | 1428 | (cp_parser *, bool); |
a723baf1 | 1429 | static enum tree_code cp_parser_assignment_operator_opt |
94edc4ab | 1430 | (cp_parser *); |
a723baf1 | 1431 | static tree cp_parser_expression |
93678513 | 1432 | (cp_parser *, bool); |
a723baf1 | 1433 | static tree cp_parser_constant_expression |
14d22dd6 | 1434 | (cp_parser *, bool, bool *); |
7a3ea201 RH |
1435 | static tree cp_parser_builtin_offsetof |
1436 | (cp_parser *); | |
a723baf1 MM |
1437 | |
1438 | /* Statements [gram.stmt.stmt] */ | |
1439 | ||
1440 | static void cp_parser_statement | |
325c3691 | 1441 | (cp_parser *, tree); |
a723baf1 | 1442 | static tree cp_parser_labeled_statement |
325c3691 | 1443 | (cp_parser *, tree); |
a723baf1 | 1444 | static tree cp_parser_expression_statement |
325c3691 | 1445 | (cp_parser *, tree); |
a723baf1 | 1446 | static tree cp_parser_compound_statement |
325c3691 | 1447 | (cp_parser *, tree, bool); |
a723baf1 | 1448 | static void cp_parser_statement_seq_opt |
325c3691 | 1449 | (cp_parser *, tree); |
a723baf1 | 1450 | static tree cp_parser_selection_statement |
94edc4ab | 1451 | (cp_parser *); |
a723baf1 | 1452 | static tree cp_parser_condition |
94edc4ab | 1453 | (cp_parser *); |
a723baf1 | 1454 | static tree cp_parser_iteration_statement |
94edc4ab | 1455 | (cp_parser *); |
a723baf1 | 1456 | static void cp_parser_for_init_statement |
94edc4ab | 1457 | (cp_parser *); |
a723baf1 | 1458 | static tree cp_parser_jump_statement |
94edc4ab | 1459 | (cp_parser *); |
a723baf1 | 1460 | static void cp_parser_declaration_statement |
94edc4ab | 1461 | (cp_parser *); |
a723baf1 MM |
1462 | |
1463 | static tree cp_parser_implicitly_scoped_statement | |
94edc4ab | 1464 | (cp_parser *); |
a723baf1 | 1465 | static void cp_parser_already_scoped_statement |
94edc4ab | 1466 | (cp_parser *); |
a723baf1 MM |
1467 | |
1468 | /* Declarations [gram.dcl.dcl] */ | |
1469 | ||
1470 | static void cp_parser_declaration_seq_opt | |
94edc4ab | 1471 | (cp_parser *); |
a723baf1 | 1472 | static void cp_parser_declaration |
94edc4ab | 1473 | (cp_parser *); |
a723baf1 | 1474 | static void cp_parser_block_declaration |
94edc4ab | 1475 | (cp_parser *, bool); |
a723baf1 | 1476 | static void cp_parser_simple_declaration |
94edc4ab | 1477 | (cp_parser *, bool); |
62d1db17 MM |
1478 | static void cp_parser_decl_specifier_seq |
1479 | (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *); | |
a723baf1 | 1480 | static tree cp_parser_storage_class_specifier_opt |
94edc4ab | 1481 | (cp_parser *); |
a723baf1 | 1482 | static tree cp_parser_function_specifier_opt |
62d1db17 | 1483 | (cp_parser *, cp_decl_specifier_seq *); |
a723baf1 | 1484 | static tree cp_parser_type_specifier |
98ca843c | 1485 | (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool, |
62d1db17 | 1486 | int *, bool *); |
a723baf1 | 1487 | static tree cp_parser_simple_type_specifier |
62d1db17 | 1488 | (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags); |
a723baf1 | 1489 | static tree cp_parser_type_name |
94edc4ab | 1490 | (cp_parser *); |
a723baf1 | 1491 | static tree cp_parser_elaborated_type_specifier |
94edc4ab | 1492 | (cp_parser *, bool, bool); |
a723baf1 | 1493 | static tree cp_parser_enum_specifier |
94edc4ab | 1494 | (cp_parser *); |
a723baf1 | 1495 | static void cp_parser_enumerator_list |
94edc4ab | 1496 | (cp_parser *, tree); |
21526606 | 1497 | static void cp_parser_enumerator_definition |
94edc4ab | 1498 | (cp_parser *, tree); |
a723baf1 | 1499 | static tree cp_parser_namespace_name |
94edc4ab | 1500 | (cp_parser *); |
a723baf1 | 1501 | static void cp_parser_namespace_definition |
94edc4ab | 1502 | (cp_parser *); |
a723baf1 | 1503 | static void cp_parser_namespace_body |
94edc4ab | 1504 | (cp_parser *); |
a723baf1 | 1505 | static tree cp_parser_qualified_namespace_specifier |
94edc4ab | 1506 | (cp_parser *); |
a723baf1 | 1507 | static void cp_parser_namespace_alias_definition |
94edc4ab | 1508 | (cp_parser *); |
a723baf1 | 1509 | static void cp_parser_using_declaration |
94edc4ab | 1510 | (cp_parser *); |
a723baf1 | 1511 | static void cp_parser_using_directive |
94edc4ab | 1512 | (cp_parser *); |
a723baf1 | 1513 | static void cp_parser_asm_definition |
94edc4ab | 1514 | (cp_parser *); |
a723baf1 | 1515 | static void cp_parser_linkage_specification |
94edc4ab | 1516 | (cp_parser *); |
a723baf1 MM |
1517 | |
1518 | /* Declarators [gram.dcl.decl] */ | |
1519 | ||
1520 | static tree cp_parser_init_declarator | |
62d1db17 | 1521 | (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *); |
058b15c1 | 1522 | static cp_declarator *cp_parser_declarator |
db86dd14 | 1523 | (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool); |
058b15c1 | 1524 | static cp_declarator *cp_parser_direct_declarator |
db86dd14 | 1525 | (cp_parser *, cp_parser_declarator_kind, int *, bool); |
a723baf1 | 1526 | static enum tree_code cp_parser_ptr_operator |
3c01e5df MM |
1527 | (cp_parser *, tree *, cp_cv_quals *); |
1528 | static cp_cv_quals cp_parser_cv_qualifier_seq_opt | |
94edc4ab | 1529 | (cp_parser *); |
a723baf1 | 1530 | static tree cp_parser_declarator_id |
94edc4ab | 1531 | (cp_parser *); |
a723baf1 | 1532 | static tree cp_parser_type_id |
94edc4ab | 1533 | (cp_parser *); |
62d1db17 | 1534 | static void cp_parser_type_specifier_seq |
d4113656 | 1535 | (cp_parser *, bool, cp_decl_specifier_seq *); |
058b15c1 | 1536 | static cp_parameter_declarator *cp_parser_parameter_declaration_clause |
94edc4ab | 1537 | (cp_parser *); |
058b15c1 MM |
1538 | static cp_parameter_declarator *cp_parser_parameter_declaration_list |
1539 | (cp_parser *, bool *); | |
1540 | static cp_parameter_declarator *cp_parser_parameter_declaration | |
4bb8ca28 | 1541 | (cp_parser *, bool, bool *); |
a723baf1 MM |
1542 | static void cp_parser_function_body |
1543 | (cp_parser *); | |
1544 | static tree cp_parser_initializer | |
39703eb9 | 1545 | (cp_parser *, bool *, bool *); |
a723baf1 | 1546 | static tree cp_parser_initializer_clause |
39703eb9 | 1547 | (cp_parser *, bool *); |
a723baf1 | 1548 | static tree cp_parser_initializer_list |
39703eb9 | 1549 | (cp_parser *, bool *); |
a723baf1 MM |
1550 | |
1551 | static bool cp_parser_ctor_initializer_opt_and_function_body | |
1552 | (cp_parser *); | |
1553 | ||
1554 | /* Classes [gram.class] */ | |
1555 | ||
1556 | static tree cp_parser_class_name | |
fc6a28d7 | 1557 | (cp_parser *, bool, bool, enum tag_types, bool, bool, bool); |
a723baf1 | 1558 | static tree cp_parser_class_specifier |
94edc4ab | 1559 | (cp_parser *); |
a723baf1 | 1560 | static tree cp_parser_class_head |
38b305d0 | 1561 | (cp_parser *, bool *, tree *); |
a723baf1 | 1562 | static enum tag_types cp_parser_class_key |
94edc4ab | 1563 | (cp_parser *); |
a723baf1 | 1564 | static void cp_parser_member_specification_opt |
94edc4ab | 1565 | (cp_parser *); |
a723baf1 | 1566 | static void cp_parser_member_declaration |
94edc4ab | 1567 | (cp_parser *); |
a723baf1 | 1568 | static tree cp_parser_pure_specifier |
94edc4ab | 1569 | (cp_parser *); |
a723baf1 | 1570 | static tree cp_parser_constant_initializer |
94edc4ab | 1571 | (cp_parser *); |
a723baf1 MM |
1572 | |
1573 | /* Derived classes [gram.class.derived] */ | |
1574 | ||
1575 | static tree cp_parser_base_clause | |
94edc4ab | 1576 | (cp_parser *); |
a723baf1 | 1577 | static tree cp_parser_base_specifier |
94edc4ab | 1578 | (cp_parser *); |
a723baf1 MM |
1579 | |
1580 | /* Special member functions [gram.special] */ | |
1581 | ||
1582 | static tree cp_parser_conversion_function_id | |
94edc4ab | 1583 | (cp_parser *); |
a723baf1 | 1584 | static tree cp_parser_conversion_type_id |
94edc4ab | 1585 | (cp_parser *); |
058b15c1 | 1586 | static cp_declarator *cp_parser_conversion_declarator_opt |
94edc4ab | 1587 | (cp_parser *); |
a723baf1 | 1588 | static bool cp_parser_ctor_initializer_opt |
94edc4ab | 1589 | (cp_parser *); |
a723baf1 | 1590 | static void cp_parser_mem_initializer_list |
94edc4ab | 1591 | (cp_parser *); |
a723baf1 | 1592 | static tree cp_parser_mem_initializer |
94edc4ab | 1593 | (cp_parser *); |
a723baf1 | 1594 | static tree cp_parser_mem_initializer_id |
94edc4ab | 1595 | (cp_parser *); |
a723baf1 MM |
1596 | |
1597 | /* Overloading [gram.over] */ | |
1598 | ||
1599 | static tree cp_parser_operator_function_id | |
94edc4ab | 1600 | (cp_parser *); |
a723baf1 | 1601 | static tree cp_parser_operator |
94edc4ab | 1602 | (cp_parser *); |
a723baf1 MM |
1603 | |
1604 | /* Templates [gram.temp] */ | |
1605 | ||
1606 | static void cp_parser_template_declaration | |
94edc4ab | 1607 | (cp_parser *, bool); |
a723baf1 | 1608 | static tree cp_parser_template_parameter_list |
94edc4ab | 1609 | (cp_parser *); |
a723baf1 | 1610 | static tree cp_parser_template_parameter |
058b15c1 | 1611 | (cp_parser *, bool *); |
a723baf1 | 1612 | static tree cp_parser_type_parameter |
94edc4ab | 1613 | (cp_parser *); |
a723baf1 | 1614 | static tree cp_parser_template_id |
a668c6ad | 1615 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1616 | static tree cp_parser_template_name |
a668c6ad | 1617 | (cp_parser *, bool, bool, bool, bool *); |
a723baf1 | 1618 | static tree cp_parser_template_argument_list |
94edc4ab | 1619 | (cp_parser *); |
a723baf1 | 1620 | static tree cp_parser_template_argument |
94edc4ab | 1621 | (cp_parser *); |
a723baf1 | 1622 | static void cp_parser_explicit_instantiation |
94edc4ab | 1623 | (cp_parser *); |
a723baf1 | 1624 | static void cp_parser_explicit_specialization |
94edc4ab | 1625 | (cp_parser *); |
a723baf1 MM |
1626 | |
1627 | /* Exception handling [gram.exception] */ | |
1628 | ||
21526606 | 1629 | static tree cp_parser_try_block |
94edc4ab | 1630 | (cp_parser *); |
a723baf1 | 1631 | static bool cp_parser_function_try_block |
94edc4ab | 1632 | (cp_parser *); |
a723baf1 | 1633 | static void cp_parser_handler_seq |
94edc4ab | 1634 | (cp_parser *); |
a723baf1 | 1635 | static void cp_parser_handler |
94edc4ab | 1636 | (cp_parser *); |
a723baf1 | 1637 | static tree cp_parser_exception_declaration |
94edc4ab | 1638 | (cp_parser *); |
a723baf1 | 1639 | static tree cp_parser_throw_expression |
94edc4ab | 1640 | (cp_parser *); |
a723baf1 | 1641 | static tree cp_parser_exception_specification_opt |
94edc4ab | 1642 | (cp_parser *); |
a723baf1 | 1643 | static tree cp_parser_type_id_list |
94edc4ab | 1644 | (cp_parser *); |
a723baf1 MM |
1645 | |
1646 | /* GNU Extensions */ | |
1647 | ||
1648 | static tree cp_parser_asm_specification_opt | |
94edc4ab | 1649 | (cp_parser *); |
a723baf1 | 1650 | static tree cp_parser_asm_operand_list |
94edc4ab | 1651 | (cp_parser *); |
a723baf1 | 1652 | static tree cp_parser_asm_clobber_list |
94edc4ab | 1653 | (cp_parser *); |
a723baf1 | 1654 | static tree cp_parser_attributes_opt |
94edc4ab | 1655 | (cp_parser *); |
a723baf1 | 1656 | static tree cp_parser_attribute_list |
94edc4ab | 1657 | (cp_parser *); |
a723baf1 | 1658 | static bool cp_parser_extension_opt |
94edc4ab | 1659 | (cp_parser *, int *); |
a723baf1 | 1660 | static void cp_parser_label_declaration |
94edc4ab | 1661 | (cp_parser *); |
a723baf1 | 1662 | |
e58a9aa1 ZL |
1663 | /* Objective-C++ Productions */ |
1664 | ||
1665 | static tree cp_parser_objc_message_receiver | |
1666 | (cp_parser *); | |
1667 | static tree cp_parser_objc_message_args | |
1668 | (cp_parser *); | |
1669 | static tree cp_parser_objc_message_expression | |
1670 | (cp_parser *); | |
1671 | static tree cp_parser_objc_encode_expression | |
1672 | (cp_parser *); | |
1673 | static tree cp_parser_objc_defs_expression | |
1674 | (cp_parser *); | |
1675 | static tree cp_parser_objc_protocol_expression | |
1676 | (cp_parser *); | |
1677 | static tree cp_parser_objc_selector_expression | |
1678 | (cp_parser *); | |
1679 | static tree cp_parser_objc_expression | |
1680 | (cp_parser *); | |
1681 | static bool cp_parser_objc_selector_p | |
1682 | (enum cpp_ttype); | |
1683 | static tree cp_parser_objc_selector | |
1684 | (cp_parser *); | |
1685 | static tree cp_parser_objc_protocol_refs_opt | |
1686 | (cp_parser *); | |
1687 | static void cp_parser_objc_declaration | |
1688 | (cp_parser *); | |
1689 | static tree cp_parser_objc_statement | |
1690 | (cp_parser *); | |
1691 | ||
a723baf1 MM |
1692 | /* Utility Routines */ |
1693 | ||
1694 | static tree cp_parser_lookup_name | |
fc6a28d7 | 1695 | (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *); |
a723baf1 | 1696 | static tree cp_parser_lookup_name_simple |
94edc4ab | 1697 | (cp_parser *, tree); |
a723baf1 MM |
1698 | static tree cp_parser_maybe_treat_template_as_class |
1699 | (tree, bool); | |
1700 | static bool cp_parser_check_declarator_template_parameters | |
058b15c1 | 1701 | (cp_parser *, cp_declarator *); |
a723baf1 | 1702 | static bool cp_parser_check_template_parameters |
94edc4ab | 1703 | (cp_parser *, unsigned); |
d6b4ea85 MM |
1704 | static tree cp_parser_simple_cast_expression |
1705 | (cp_parser *); | |
a723baf1 | 1706 | static tree cp_parser_global_scope_opt |
94edc4ab | 1707 | (cp_parser *, bool); |
a723baf1 MM |
1708 | static bool cp_parser_constructor_declarator_p |
1709 | (cp_parser *, bool); | |
1710 | static tree cp_parser_function_definition_from_specifiers_and_declarator | |
62d1db17 | 1711 | (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *); |
a723baf1 | 1712 | static tree cp_parser_function_definition_after_declarator |
94edc4ab | 1713 | (cp_parser *, bool); |
a723baf1 | 1714 | static void cp_parser_template_declaration_after_export |
94edc4ab | 1715 | (cp_parser *, bool); |
a723baf1 | 1716 | static tree cp_parser_single_declaration |
94edc4ab | 1717 | (cp_parser *, bool, bool *); |
a723baf1 | 1718 | static tree cp_parser_functional_cast |
94edc4ab | 1719 | (cp_parser *, tree); |
4bb8ca28 | 1720 | static tree cp_parser_save_member_function_body |
62d1db17 | 1721 | (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree); |
ec75414f MM |
1722 | static tree cp_parser_enclosed_template_argument_list |
1723 | (cp_parser *); | |
8db1028e NS |
1724 | static void cp_parser_save_default_args |
1725 | (cp_parser *, tree); | |
a723baf1 | 1726 | static void cp_parser_late_parsing_for_member |
94edc4ab | 1727 | (cp_parser *, tree); |
a723baf1 | 1728 | static void cp_parser_late_parsing_default_args |
8218bd34 | 1729 | (cp_parser *, tree); |
a723baf1 | 1730 | static tree cp_parser_sizeof_operand |
94edc4ab | 1731 | (cp_parser *, enum rid); |
a723baf1 | 1732 | static bool cp_parser_declares_only_class_p |
94edc4ab | 1733 | (cp_parser *); |
62d1db17 MM |
1734 | static void cp_parser_set_storage_class |
1735 | (cp_decl_specifier_seq *, cp_storage_class); | |
98ca843c | 1736 | static void cp_parser_set_decl_spec_type |
62d1db17 | 1737 | (cp_decl_specifier_seq *, tree, bool); |
a723baf1 | 1738 | static bool cp_parser_friend_p |
62d1db17 | 1739 | (const cp_decl_specifier_seq *); |
a723baf1 | 1740 | static cp_token *cp_parser_require |
94edc4ab | 1741 | (cp_parser *, enum cpp_ttype, const char *); |
a723baf1 | 1742 | static cp_token *cp_parser_require_keyword |
94edc4ab | 1743 | (cp_parser *, enum rid, const char *); |
21526606 | 1744 | static bool cp_parser_token_starts_function_definition_p |
94edc4ab | 1745 | (cp_token *); |
a723baf1 MM |
1746 | static bool cp_parser_next_token_starts_class_definition_p |
1747 | (cp_parser *); | |
d17811fd MM |
1748 | static bool cp_parser_next_token_ends_template_argument_p |
1749 | (cp_parser *); | |
f4abade9 GB |
1750 | static bool cp_parser_nth_token_starts_template_argument_list_p |
1751 | (cp_parser *, size_t); | |
a723baf1 | 1752 | static enum tag_types cp_parser_token_is_class_key |
94edc4ab | 1753 | (cp_token *); |
a723baf1 MM |
1754 | static void cp_parser_check_class_key |
1755 | (enum tag_types, tree type); | |
37d407a1 KL |
1756 | static void cp_parser_check_access_in_redeclaration |
1757 | (tree type); | |
a723baf1 MM |
1758 | static bool cp_parser_optional_template_keyword |
1759 | (cp_parser *); | |
21526606 | 1760 | static void cp_parser_pre_parsed_nested_name_specifier |
2050a1bb | 1761 | (cp_parser *); |
a723baf1 | 1762 | static void cp_parser_cache_group |
c162c75e | 1763 | (cp_parser *, enum cpp_ttype, unsigned); |
21526606 | 1764 | static void cp_parser_parse_tentatively |
94edc4ab | 1765 | (cp_parser *); |
a723baf1 | 1766 | static void cp_parser_commit_to_tentative_parse |
94edc4ab | 1767 | (cp_parser *); |
a723baf1 | 1768 | static void cp_parser_abort_tentative_parse |
94edc4ab | 1769 | (cp_parser *); |
a723baf1 | 1770 | static bool cp_parser_parse_definitely |
94edc4ab | 1771 | (cp_parser *); |
f7b5ecd9 | 1772 | static inline bool cp_parser_parsing_tentatively |
94edc4ab | 1773 | (cp_parser *); |
0b16f8f4 | 1774 | static bool cp_parser_uncommitted_to_tentative_parse_p |
94edc4ab | 1775 | (cp_parser *); |
a723baf1 | 1776 | static void cp_parser_error |
94edc4ab | 1777 | (cp_parser *, const char *); |
4bb8ca28 MM |
1778 | static void cp_parser_name_lookup_error |
1779 | (cp_parser *, tree, tree, const char *); | |
e5976695 | 1780 | static bool cp_parser_simulate_error |
94edc4ab | 1781 | (cp_parser *); |
a723baf1 | 1782 | static void cp_parser_check_type_definition |
94edc4ab | 1783 | (cp_parser *); |
560ad596 | 1784 | static void cp_parser_check_for_definition_in_return_type |
fc6a28d7 | 1785 | (cp_declarator *, tree); |
ee43dab5 MM |
1786 | static void cp_parser_check_for_invalid_template_id |
1787 | (cp_parser *, tree); | |
625cbf93 MM |
1788 | static bool cp_parser_non_integral_constant_expression |
1789 | (cp_parser *, const char *); | |
2097b5f2 GB |
1790 | static void cp_parser_diagnose_invalid_type_name |
1791 | (cp_parser *, tree, tree); | |
1792 | static bool cp_parser_parse_and_diagnose_invalid_type_name | |
8fbc5ae7 | 1793 | (cp_parser *); |
7efa3e22 | 1794 | static int cp_parser_skip_to_closing_parenthesis |
a668c6ad | 1795 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1796 | static void cp_parser_skip_to_end_of_statement |
94edc4ab | 1797 | (cp_parser *); |
e0860732 MM |
1798 | static void cp_parser_consume_semicolon_at_end_of_statement |
1799 | (cp_parser *); | |
a723baf1 | 1800 | static void cp_parser_skip_to_end_of_block_or_statement |
94edc4ab | 1801 | (cp_parser *); |
a723baf1 MM |
1802 | static void cp_parser_skip_to_closing_brace |
1803 | (cp_parser *); | |
1804 | static void cp_parser_skip_until_found | |
94edc4ab | 1805 | (cp_parser *, enum cpp_ttype, const char *); |
a723baf1 | 1806 | static bool cp_parser_error_occurred |
94edc4ab | 1807 | (cp_parser *); |
a723baf1 | 1808 | static bool cp_parser_allow_gnu_extensions_p |
94edc4ab | 1809 | (cp_parser *); |
a723baf1 | 1810 | static bool cp_parser_is_string_literal |
94edc4ab | 1811 | (cp_token *); |
21526606 | 1812 | static bool cp_parser_is_keyword |
94edc4ab | 1813 | (cp_token *, enum rid); |
2097b5f2 GB |
1814 | static tree cp_parser_make_typename_type |
1815 | (cp_parser *, tree, tree); | |
a723baf1 | 1816 | |
4de8668e | 1817 | /* Returns nonzero if we are parsing tentatively. */ |
f7b5ecd9 MM |
1818 | |
1819 | static inline bool | |
94edc4ab | 1820 | cp_parser_parsing_tentatively (cp_parser* parser) |
f7b5ecd9 MM |
1821 | { |
1822 | return parser->context->next != NULL; | |
1823 | } | |
1824 | ||
4de8668e | 1825 | /* Returns nonzero if TOKEN is a string literal. */ |
a723baf1 MM |
1826 | |
1827 | static bool | |
94edc4ab | 1828 | cp_parser_is_string_literal (cp_token* token) |
a723baf1 MM |
1829 | { |
1830 | return (token->type == CPP_STRING || token->type == CPP_WSTRING); | |
1831 | } | |
1832 | ||
4de8668e | 1833 | /* Returns nonzero if TOKEN is the indicated KEYWORD. */ |
a723baf1 MM |
1834 | |
1835 | static bool | |
94edc4ab | 1836 | cp_parser_is_keyword (cp_token* token, enum rid keyword) |
a723baf1 MM |
1837 | { |
1838 | return token->keyword == keyword; | |
1839 | } | |
1840 | ||
8ff24a79 MM |
1841 | /* A minimum or maximum operator has been seen. As these are |
1842 | deprecated, issue a warning. */ | |
1843 | ||
1844 | static inline void | |
1845 | cp_parser_warn_min_max (void) | |
1846 | { | |
1847 | if (warn_deprecated && !in_system_header) | |
d4ee4d25 | 1848 | warning (0, "minimum/maximum operators are deprecated"); |
8ff24a79 MM |
1849 | } |
1850 | ||
2cfe82fe ZW |
1851 | /* If not parsing tentatively, issue a diagnostic of the form |
1852 | FILE:LINE: MESSAGE before TOKEN | |
1853 | where TOKEN is the next token in the input stream. MESSAGE | |
1854 | (specified by the caller) is usually of the form "expected | |
1855 | OTHER-TOKEN". */ | |
a723baf1 MM |
1856 | |
1857 | static void | |
94edc4ab | 1858 | cp_parser_error (cp_parser* parser, const char* message) |
a723baf1 | 1859 | { |
e5976695 | 1860 | if (!cp_parser_simulate_error (parser)) |
4bb8ca28 | 1861 | { |
2cfe82fe ZW |
1862 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
1863 | /* This diagnostic makes more sense if it is tagged to the line | |
1864 | of the token we just peeked at. */ | |
1865 | cp_lexer_set_source_position_from_token (token); | |
0d63048c MM |
1866 | if (token->type == CPP_PRAGMA) |
1867 | { | |
1868 | error ("%<#pragma%> is not allowed here"); | |
1869 | cp_lexer_purge_token (parser->lexer); | |
1870 | return; | |
1871 | } | |
21526606 | 1872 | c_parse_error (message, |
5c832178 MM |
1873 | /* Because c_parser_error does not understand |
1874 | CPP_KEYWORD, keywords are treated like | |
1875 | identifiers. */ | |
21526606 | 1876 | (token->type == CPP_KEYWORD ? CPP_NAME : token->type), |
5c832178 | 1877 | token->value); |
4bb8ca28 MM |
1878 | } |
1879 | } | |
1880 | ||
1881 | /* Issue an error about name-lookup failing. NAME is the | |
1882 | IDENTIFIER_NODE DECL is the result of | |
1883 | the lookup (as returned from cp_parser_lookup_name). DESIRED is | |
1884 | the thing that we hoped to find. */ | |
1885 | ||
1886 | static void | |
1887 | cp_parser_name_lookup_error (cp_parser* parser, | |
1888 | tree name, | |
1889 | tree decl, | |
1890 | const char* desired) | |
1891 | { | |
1892 | /* If name lookup completely failed, tell the user that NAME was not | |
1893 | declared. */ | |
1894 | if (decl == error_mark_node) | |
1895 | { | |
1896 | if (parser->scope && parser->scope != global_namespace) | |
2a13a625 | 1897 | error ("%<%D::%D%> has not been declared", |
4bb8ca28 MM |
1898 | parser->scope, name); |
1899 | else if (parser->scope == global_namespace) | |
2a13a625 | 1900 | error ("%<::%D%> has not been declared", name); |
b14454ba MM |
1901 | else if (parser->object_scope |
1902 | && !CLASS_TYPE_P (parser->object_scope)) | |
2a13a625 | 1903 | error ("request for member %qD in non-class type %qT", |
b14454ba MM |
1904 | name, parser->object_scope); |
1905 | else if (parser->object_scope) | |
2a13a625 | 1906 | error ("%<%T::%D%> has not been declared", |
b14454ba | 1907 | parser->object_scope, name); |
4bb8ca28 | 1908 | else |
9e637a26 | 1909 | error ("%qD has not been declared", name); |
4bb8ca28 MM |
1910 | } |
1911 | else if (parser->scope && parser->scope != global_namespace) | |
2a13a625 | 1912 | error ("%<%D::%D%> %s", parser->scope, name, desired); |
4bb8ca28 | 1913 | else if (parser->scope == global_namespace) |
2a13a625 | 1914 | error ("%<::%D%> %s", name, desired); |
4bb8ca28 | 1915 | else |
2a13a625 | 1916 | error ("%qD %s", name, desired); |
a723baf1 MM |
1917 | } |
1918 | ||
1919 | /* If we are parsing tentatively, remember that an error has occurred | |
e5976695 | 1920 | during this tentative parse. Returns true if the error was |
77077b39 | 1921 | simulated; false if a message should be issued by the caller. */ |
a723baf1 | 1922 | |
e5976695 | 1923 | static bool |
94edc4ab | 1924 | cp_parser_simulate_error (cp_parser* parser) |
a723baf1 | 1925 | { |
0b16f8f4 | 1926 | if (cp_parser_uncommitted_to_tentative_parse_p (parser)) |
e5976695 MM |
1927 | { |
1928 | parser->context->status = CP_PARSER_STATUS_KIND_ERROR; | |
1929 | return true; | |
1930 | } | |
1931 | return false; | |
a723baf1 MM |
1932 | } |
1933 | ||
1934 | /* This function is called when a type is defined. If type | |
1935 | definitions are forbidden at this point, an error message is | |
1936 | issued. */ | |
1937 | ||
1938 | static void | |
94edc4ab | 1939 | cp_parser_check_type_definition (cp_parser* parser) |
a723baf1 MM |
1940 | { |
1941 | /* If types are forbidden here, issue a message. */ | |
1942 | if (parser->type_definition_forbidden_message) | |
1943 | /* Use `%s' to print the string in case there are any escape | |
1944 | characters in the message. */ | |
1945 | error ("%s", parser->type_definition_forbidden_message); | |
1946 | } | |
1947 | ||
fc6a28d7 | 1948 | /* This function is called when the DECLARATOR is processed. The TYPE |
472c29c3 | 1949 | was a type defined in the decl-specifiers. If it is invalid to |
fc6a28d7 MM |
1950 | define a type in the decl-specifiers for DECLARATOR, an error is |
1951 | issued. */ | |
560ad596 MM |
1952 | |
1953 | static void | |
058b15c1 | 1954 | cp_parser_check_for_definition_in_return_type (cp_declarator *declarator, |
fc6a28d7 | 1955 | tree type) |
560ad596 MM |
1956 | { |
1957 | /* [dcl.fct] forbids type definitions in return types. | |
1958 | Unfortunately, it's not easy to know whether or not we are | |
1959 | processing a return type until after the fact. */ | |
1960 | while (declarator | |
058b15c1 MM |
1961 | && (declarator->kind == cdk_pointer |
1962 | || declarator->kind == cdk_reference | |
1963 | || declarator->kind == cdk_ptrmem)) | |
1964 | declarator = declarator->declarator; | |
560ad596 | 1965 | if (declarator |
fc6a28d7 MM |
1966 | && declarator->kind == cdk_function) |
1967 | { | |
1968 | error ("new types may not be defined in a return type"); | |
1969 | inform ("(perhaps a semicolon is missing after the definition of %qT)", | |
1970 | type); | |
1971 | } | |
560ad596 MM |
1972 | } |
1973 | ||
ee43dab5 MM |
1974 | /* A type-specifier (TYPE) has been parsed which cannot be followed by |
1975 | "<" in any valid C++ program. If the next token is indeed "<", | |
1976 | issue a message warning the user about what appears to be an | |
1977 | invalid attempt to form a template-id. */ | |
1978 | ||
1979 | static void | |
21526606 | 1980 | cp_parser_check_for_invalid_template_id (cp_parser* parser, |
ee43dab5 MM |
1981 | tree type) |
1982 | { | |
0c5e4866 | 1983 | cp_token_position start = 0; |
ee43dab5 MM |
1984 | |
1985 | if (cp_lexer_next_token_is (parser->lexer, CPP_LESS)) | |
1986 | { | |
1987 | if (TYPE_P (type)) | |
2a13a625 | 1988 | error ("%qT is not a template", type); |
ee43dab5 | 1989 | else if (TREE_CODE (type) == IDENTIFIER_NODE) |
2a13a625 | 1990 | error ("%qE is not a template", type); |
ee43dab5 MM |
1991 | else |
1992 | error ("invalid template-id"); | |
1993 | /* Remember the location of the invalid "<". */ | |
0b16f8f4 | 1994 | if (cp_parser_uncommitted_to_tentative_parse_p (parser)) |
0c5e4866 | 1995 | start = cp_lexer_token_position (parser->lexer, true); |
ee43dab5 MM |
1996 | /* Consume the "<". */ |
1997 | cp_lexer_consume_token (parser->lexer); | |
1998 | /* Parse the template arguments. */ | |
1999 | cp_parser_enclosed_template_argument_list (parser); | |
da1d7781 | 2000 | /* Permanently remove the invalid template arguments so that |
ee43dab5 | 2001 | this error message is not issued again. */ |
0c5e4866 NS |
2002 | if (start) |
2003 | cp_lexer_purge_tokens_after (parser->lexer, start); | |
ee43dab5 MM |
2004 | } |
2005 | } | |
2006 | ||
625cbf93 MM |
2007 | /* If parsing an integral constant-expression, issue an error message |
2008 | about the fact that THING appeared and return true. Otherwise, | |
93678513 MM |
2009 | return false. In either case, set |
2010 | PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */ | |
14d22dd6 | 2011 | |
625cbf93 MM |
2012 | static bool |
2013 | cp_parser_non_integral_constant_expression (cp_parser *parser, | |
2014 | const char *thing) | |
14d22dd6 | 2015 | { |
93678513 | 2016 | parser->non_integral_constant_expression_p = true; |
625cbf93 MM |
2017 | if (parser->integral_constant_expression_p) |
2018 | { | |
2019 | if (!parser->allow_non_integral_constant_expression_p) | |
2020 | { | |
2021 | error ("%s cannot appear in a constant-expression", thing); | |
2022 | return true; | |
2023 | } | |
625cbf93 MM |
2024 | } |
2025 | return false; | |
14d22dd6 MM |
2026 | } |
2027 | ||
0c88d886 | 2028 | /* Emit a diagnostic for an invalid type name. SCOPE is the |
6ca2d67f MM |
2029 | qualifying scope (or NULL, if none) for ID. This function commits |
2030 | to the current active tentative parse, if any. (Otherwise, the | |
2031 | problematic construct might be encountered again later, resulting | |
2032 | in duplicate error messages.) */ | |
8fbc5ae7 | 2033 | |
2097b5f2 GB |
2034 | static void |
2035 | cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id) | |
6c0cc713 GB |
2036 | { |
2037 | tree decl, old_scope; | |
2097b5f2 GB |
2038 | /* Try to lookup the identifier. */ |
2039 | old_scope = parser->scope; | |
2040 | parser->scope = scope; | |
2041 | decl = cp_parser_lookup_name_simple (parser, id); | |
2042 | parser->scope = old_scope; | |
2043 | /* If the lookup found a template-name, it means that the user forgot | |
2044 | to specify an argument list. Emit an useful error message. */ | |
2045 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
2a13a625 | 2046 | error ("invalid use of template-name %qE without an argument list", |
6c0cc713 | 2047 | decl); |
2097b5f2 | 2048 | else if (!parser->scope) |
8fbc5ae7 | 2049 | { |
8fbc5ae7 | 2050 | /* Issue an error message. */ |
2a13a625 | 2051 | error ("%qE does not name a type", id); |
8fbc5ae7 MM |
2052 | /* If we're in a template class, it's possible that the user was |
2053 | referring to a type from a base class. For example: | |
2054 | ||
2055 | template <typename T> struct A { typedef T X; }; | |
2056 | template <typename T> struct B : public A<T> { X x; }; | |
2057 | ||
2058 | The user should have said "typename A<T>::X". */ | |
32db39c0 PC |
2059 | if (processing_template_decl && current_class_type |
2060 | && TYPE_BINFO (current_class_type)) | |
8fbc5ae7 MM |
2061 | { |
2062 | tree b; | |
2063 | ||
2064 | for (b = TREE_CHAIN (TYPE_BINFO (current_class_type)); | |
2065 | b; | |
2066 | b = TREE_CHAIN (b)) | |
2067 | { | |
2068 | tree base_type = BINFO_TYPE (b); | |
21526606 | 2069 | if (CLASS_TYPE_P (base_type) |
1fb3244a | 2070 | && dependent_type_p (base_type)) |
8fbc5ae7 MM |
2071 | { |
2072 | tree field; | |
2073 | /* Go from a particular instantiation of the | |
2074 | template (which will have an empty TYPE_FIELDs), | |
2075 | to the main version. */ | |
353b4fc0 | 2076 | base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type); |
8fbc5ae7 MM |
2077 | for (field = TYPE_FIELDS (base_type); |
2078 | field; | |
2079 | field = TREE_CHAIN (field)) | |
2080 | if (TREE_CODE (field) == TYPE_DECL | |
2097b5f2 | 2081 | && DECL_NAME (field) == id) |
8fbc5ae7 | 2082 | { |
c4f73174 | 2083 | inform ("(perhaps %<typename %T::%E%> was intended)", |
2097b5f2 | 2084 | BINFO_TYPE (b), id); |
8fbc5ae7 MM |
2085 | break; |
2086 | } | |
2087 | if (field) | |
2088 | break; | |
2089 | } | |
2090 | } | |
2091 | } | |
8fbc5ae7 | 2092 | } |
2097b5f2 GB |
2093 | /* Here we diagnose qualified-ids where the scope is actually correct, |
2094 | but the identifier does not resolve to a valid type name. */ | |
21526606 | 2095 | else |
2097b5f2 GB |
2096 | { |
2097 | if (TREE_CODE (parser->scope) == NAMESPACE_DECL) | |
2a13a625 | 2098 | error ("%qE in namespace %qE does not name a type", |
2097b5f2 GB |
2099 | id, parser->scope); |
2100 | else if (TYPE_P (parser->scope)) | |
2fbe4889 | 2101 | error ("%qE in class %qT does not name a type", id, parser->scope); |
2097b5f2 | 2102 | else |
315fb5db | 2103 | gcc_unreachable (); |
2097b5f2 | 2104 | } |
6ca2d67f | 2105 | cp_parser_commit_to_tentative_parse (parser); |
2097b5f2 | 2106 | } |
8fbc5ae7 | 2107 | |
2097b5f2 GB |
2108 | /* Check for a common situation where a type-name should be present, |
2109 | but is not, and issue a sensible error message. Returns true if an | |
2110 | invalid type-name was detected. | |
21526606 | 2111 | |
2097b5f2 | 2112 | The situation handled by this function are variable declarations of the |
21526606 EC |
2113 | form `ID a', where `ID' is an id-expression and `a' is a plain identifier. |
2114 | Usually, `ID' should name a type, but if we got here it means that it | |
2097b5f2 GB |
2115 | does not. We try to emit the best possible error message depending on |
2116 | how exactly the id-expression looks like. | |
2117 | */ | |
2118 | ||
2119 | static bool | |
2120 | cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser) | |
2121 | { | |
2122 | tree id; | |
2123 | ||
2124 | cp_parser_parse_tentatively (parser); | |
21526606 | 2125 | id = cp_parser_id_expression (parser, |
2097b5f2 GB |
2126 | /*template_keyword_p=*/false, |
2127 | /*check_dependency_p=*/true, | |
2128 | /*template_p=*/NULL, | |
2129 | /*declarator_p=*/true); | |
2130 | /* After the id-expression, there should be a plain identifier, | |
2131 | otherwise this is not a simple variable declaration. Also, if | |
2132 | the scope is dependent, we cannot do much. */ | |
2133 | if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
21526606 | 2134 | || (parser->scope && TYPE_P (parser->scope) |
2097b5f2 GB |
2135 | && dependent_type_p (parser->scope))) |
2136 | { | |
2137 | cp_parser_abort_tentative_parse (parser); | |
2138 | return false; | |
2139 | } | |
3590f0a6 MM |
2140 | if (!cp_parser_parse_definitely (parser) |
2141 | || TREE_CODE (id) != IDENTIFIER_NODE) | |
2097b5f2 GB |
2142 | return false; |
2143 | ||
2097b5f2 GB |
2144 | /* Emit a diagnostic for the invalid type. */ |
2145 | cp_parser_diagnose_invalid_type_name (parser, parser->scope, id); | |
2146 | /* Skip to the end of the declaration; there's no point in | |
2147 | trying to process it. */ | |
2148 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
2149 | return true; | |
8fbc5ae7 MM |
2150 | } |
2151 | ||
21526606 | 2152 | /* Consume tokens up to, and including, the next non-nested closing `)'. |
7efa3e22 NS |
2153 | Returns 1 iff we found a closing `)'. RECOVERING is true, if we |
2154 | are doing error recovery. Returns -1 if OR_COMMA is true and we | |
2155 | found an unnested comma. */ | |
a723baf1 | 2156 | |
7efa3e22 NS |
2157 | static int |
2158 | cp_parser_skip_to_closing_parenthesis (cp_parser *parser, | |
21526606 | 2159 | bool recovering, |
a668c6ad MM |
2160 | bool or_comma, |
2161 | bool consume_paren) | |
a723baf1 | 2162 | { |
7efa3e22 NS |
2163 | unsigned paren_depth = 0; |
2164 | unsigned brace_depth = 0; | |
0173bb6f | 2165 | int result; |
a723baf1 | 2166 | |
0b16f8f4 VR |
2167 | if (recovering && !or_comma |
2168 | && cp_parser_uncommitted_to_tentative_parse_p (parser)) | |
7efa3e22 | 2169 | return 0; |
21526606 | 2170 | |
a723baf1 MM |
2171 | while (true) |
2172 | { | |
2173 | cp_token *token; | |
21526606 | 2174 | |
a723baf1 MM |
2175 | /* If we've run out of tokens, then there is no closing `)'. */ |
2176 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
0173bb6f AO |
2177 | { |
2178 | result = 0; | |
2179 | break; | |
2180 | } | |
a723baf1 | 2181 | |
a668c6ad | 2182 | token = cp_lexer_peek_token (parser->lexer); |
21526606 | 2183 | |
f4f206f4 | 2184 | /* This matches the processing in skip_to_end_of_statement. */ |
a668c6ad | 2185 | if (token->type == CPP_SEMICOLON && !brace_depth) |
0173bb6f AO |
2186 | { |
2187 | result = 0; | |
2188 | break; | |
2189 | } | |
a668c6ad MM |
2190 | if (token->type == CPP_OPEN_BRACE) |
2191 | ++brace_depth; | |
2192 | if (token->type == CPP_CLOSE_BRACE) | |
7efa3e22 | 2193 | { |
a668c6ad | 2194 | if (!brace_depth--) |
0173bb6f AO |
2195 | { |
2196 | result = 0; | |
2197 | break; | |
2198 | } | |
7efa3e22 | 2199 | } |
a668c6ad MM |
2200 | if (recovering && or_comma && token->type == CPP_COMMA |
2201 | && !brace_depth && !paren_depth) | |
0173bb6f AO |
2202 | { |
2203 | result = -1; | |
2204 | break; | |
2205 | } | |
21526606 | 2206 | |
7efa3e22 NS |
2207 | if (!brace_depth) |
2208 | { | |
2209 | /* If it is an `(', we have entered another level of nesting. */ | |
2210 | if (token->type == CPP_OPEN_PAREN) | |
2211 | ++paren_depth; | |
2212 | /* If it is a `)', then we might be done. */ | |
2213 | else if (token->type == CPP_CLOSE_PAREN && !paren_depth--) | |
a668c6ad MM |
2214 | { |
2215 | if (consume_paren) | |
2216 | cp_lexer_consume_token (parser->lexer); | |
0173bb6f AO |
2217 | { |
2218 | result = 1; | |
2219 | break; | |
2220 | } | |
a668c6ad | 2221 | } |
7efa3e22 | 2222 | } |
21526606 | 2223 | |
a668c6ad MM |
2224 | /* Consume the token. */ |
2225 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 2226 | } |
0173bb6f | 2227 | |
0173bb6f | 2228 | return result; |
a723baf1 MM |
2229 | } |
2230 | ||
2231 | /* Consume tokens until we reach the end of the current statement. | |
2232 | Normally, that will be just before consuming a `;'. However, if a | |
2233 | non-nested `}' comes first, then we stop before consuming that. */ | |
2234 | ||
2235 | static void | |
94edc4ab | 2236 | cp_parser_skip_to_end_of_statement (cp_parser* parser) |
a723baf1 MM |
2237 | { |
2238 | unsigned nesting_depth = 0; | |
2239 | ||
2240 | while (true) | |
2241 | { | |
2242 | cp_token *token; | |
2243 | ||
2244 | /* Peek at the next token. */ | |
2245 | token = cp_lexer_peek_token (parser->lexer); | |
2246 | /* If we've run out of tokens, stop. */ | |
2247 | if (token->type == CPP_EOF) | |
2248 | break; | |
2249 | /* If the next token is a `;', we have reached the end of the | |
2250 | statement. */ | |
2251 | if (token->type == CPP_SEMICOLON && !nesting_depth) | |
2252 | break; | |
2253 | /* If the next token is a non-nested `}', then we have reached | |
2254 | the end of the current block. */ | |
2255 | if (token->type == CPP_CLOSE_BRACE) | |
2256 | { | |
2257 | /* If this is a non-nested `}', stop before consuming it. | |
2258 | That way, when confronted with something like: | |
2259 | ||
21526606 | 2260 | { 3 + } |
a723baf1 MM |
2261 | |
2262 | we stop before consuming the closing `}', even though we | |
2263 | have not yet reached a `;'. */ | |
2264 | if (nesting_depth == 0) | |
2265 | break; | |
2266 | /* If it is the closing `}' for a block that we have | |
2267 | scanned, stop -- but only after consuming the token. | |
2268 | That way given: | |
2269 | ||
2270 | void f g () { ... } | |
2271 | typedef int I; | |
2272 | ||
2273 | we will stop after the body of the erroneously declared | |
2274 | function, but before consuming the following `typedef' | |
2275 | declaration. */ | |
2276 | if (--nesting_depth == 0) | |
2277 | { | |
2278 | cp_lexer_consume_token (parser->lexer); | |
2279 | break; | |
2280 | } | |
2281 | } | |
2282 | /* If it the next token is a `{', then we are entering a new | |
2283 | block. Consume the entire block. */ | |
2284 | else if (token->type == CPP_OPEN_BRACE) | |
2285 | ++nesting_depth; | |
2286 | /* Consume the token. */ | |
2287 | cp_lexer_consume_token (parser->lexer); | |
2288 | } | |
2289 | } | |
2290 | ||
e0860732 MM |
2291 | /* This function is called at the end of a statement or declaration. |
2292 | If the next token is a semicolon, it is consumed; otherwise, error | |
2293 | recovery is attempted. */ | |
2294 | ||
2295 | static void | |
2296 | cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser) | |
2297 | { | |
2298 | /* Look for the trailing `;'. */ | |
2299 | if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")) | |
2300 | { | |
2301 | /* If there is additional (erroneous) input, skip to the end of | |
2302 | the statement. */ | |
2303 | cp_parser_skip_to_end_of_statement (parser); | |
2304 | /* If the next token is now a `;', consume it. */ | |
2305 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
2306 | cp_lexer_consume_token (parser->lexer); | |
2307 | } | |
2308 | } | |
2309 | ||
a723baf1 MM |
2310 | /* Skip tokens until we have consumed an entire block, or until we |
2311 | have consumed a non-nested `;'. */ | |
2312 | ||
2313 | static void | |
94edc4ab | 2314 | cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser) |
a723baf1 MM |
2315 | { |
2316 | unsigned nesting_depth = 0; | |
2317 | ||
2318 | while (true) | |
2319 | { | |
2320 | cp_token *token; | |
2321 | ||
2322 | /* Peek at the next token. */ | |
2323 | token = cp_lexer_peek_token (parser->lexer); | |
2324 | /* If we've run out of tokens, stop. */ | |
2325 | if (token->type == CPP_EOF) | |
2326 | break; | |
2327 | /* If the next token is a `;', we have reached the end of the | |
2328 | statement. */ | |
2329 | if (token->type == CPP_SEMICOLON && !nesting_depth) | |
2330 | { | |
2331 | /* Consume the `;'. */ | |
2332 | cp_lexer_consume_token (parser->lexer); | |
2333 | break; | |
2334 | } | |
2335 | /* Consume the token. */ | |
2336 | token = cp_lexer_consume_token (parser->lexer); | |
2337 | /* If the next token is a non-nested `}', then we have reached | |
2338 | the end of the current block. */ | |
21526606 | 2339 | if (token->type == CPP_CLOSE_BRACE |
a723baf1 MM |
2340 | && (nesting_depth == 0 || --nesting_depth == 0)) |
2341 | break; | |
2342 | /* If it the next token is a `{', then we are entering a new | |
2343 | block. Consume the entire block. */ | |
2344 | if (token->type == CPP_OPEN_BRACE) | |
2345 | ++nesting_depth; | |
2346 | } | |
2347 | } | |
2348 | ||
2349 | /* Skip tokens until a non-nested closing curly brace is the next | |
2350 | token. */ | |
2351 | ||
2352 | static void | |
2353 | cp_parser_skip_to_closing_brace (cp_parser *parser) | |
2354 | { | |
2355 | unsigned nesting_depth = 0; | |
2356 | ||
2357 | while (true) | |
2358 | { | |
2359 | cp_token *token; | |
2360 | ||
2361 | /* Peek at the next token. */ | |
2362 | token = cp_lexer_peek_token (parser->lexer); | |
2363 | /* If we've run out of tokens, stop. */ | |
2364 | if (token->type == CPP_EOF) | |
2365 | break; | |
2366 | /* If the next token is a non-nested `}', then we have reached | |
2367 | the end of the current block. */ | |
2368 | if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0) | |
2369 | break; | |
2370 | /* If it the next token is a `{', then we are entering a new | |
2371 | block. Consume the entire block. */ | |
2372 | else if (token->type == CPP_OPEN_BRACE) | |
2373 | ++nesting_depth; | |
2374 | /* Consume the token. */ | |
2375 | cp_lexer_consume_token (parser->lexer); | |
2376 | } | |
2377 | } | |
2378 | ||
2097b5f2 GB |
2379 | /* This is a simple wrapper around make_typename_type. When the id is |
2380 | an unresolved identifier node, we can provide a superior diagnostic | |
2381 | using cp_parser_diagnose_invalid_type_name. */ | |
2382 | ||
2383 | static tree | |
2384 | cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id) | |
6c0cc713 GB |
2385 | { |
2386 | tree result; | |
2387 | if (TREE_CODE (id) == IDENTIFIER_NODE) | |
2388 | { | |
fc6a28d7 MM |
2389 | result = make_typename_type (scope, id, typename_type, |
2390 | /*complain=*/0); | |
6c0cc713 GB |
2391 | if (result == error_mark_node) |
2392 | cp_parser_diagnose_invalid_type_name (parser, scope, id); | |
2393 | return result; | |
2394 | } | |
fc6a28d7 | 2395 | return make_typename_type (scope, id, typename_type, tf_error); |
2097b5f2 GB |
2396 | } |
2397 | ||
2398 | ||
a723baf1 MM |
2399 | /* Create a new C++ parser. */ |
2400 | ||
2401 | static cp_parser * | |
94edc4ab | 2402 | cp_parser_new (void) |
a723baf1 MM |
2403 | { |
2404 | cp_parser *parser; | |
17211ab5 | 2405 | cp_lexer *lexer; |
b8b94c5b | 2406 | unsigned i; |
17211ab5 GK |
2407 | |
2408 | /* cp_lexer_new_main is called before calling ggc_alloc because | |
2409 | cp_lexer_new_main might load a PCH file. */ | |
2410 | lexer = cp_lexer_new_main (); | |
a723baf1 | 2411 | |
b8b94c5b PB |
2412 | /* Initialize the binops_by_token so that we can get the tree |
2413 | directly from the token. */ | |
2414 | for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++) | |
2415 | binops_by_token[binops[i].token_type] = binops[i]; | |
2416 | ||
99dd239f | 2417 | parser = GGC_CNEW (cp_parser); |
17211ab5 | 2418 | parser->lexer = lexer; |
a723baf1 MM |
2419 | parser->context = cp_parser_context_new (NULL); |
2420 | ||
2421 | /* For now, we always accept GNU extensions. */ | |
2422 | parser->allow_gnu_extensions_p = 1; | |
2423 | ||
2424 | /* The `>' token is a greater-than operator, not the end of a | |
2425 | template-id. */ | |
2426 | parser->greater_than_is_operator_p = true; | |
2427 | ||
2428 | parser->default_arg_ok_p = true; | |
21526606 | 2429 | |
a723baf1 | 2430 | /* We are not parsing a constant-expression. */ |
67c03833 JM |
2431 | parser->integral_constant_expression_p = false; |
2432 | parser->allow_non_integral_constant_expression_p = false; | |
2433 | parser->non_integral_constant_expression_p = false; | |
a723baf1 MM |
2434 | |
2435 | /* Local variable names are not forbidden. */ | |
2436 | parser->local_variables_forbidden_p = false; | |
2437 | ||
34cd5ae7 | 2438 | /* We are not processing an `extern "C"' declaration. */ |
a723baf1 MM |
2439 | parser->in_unbraced_linkage_specification_p = false; |
2440 | ||
2441 | /* We are not processing a declarator. */ | |
2442 | parser->in_declarator_p = false; | |
2443 | ||
4bb8ca28 MM |
2444 | /* We are not processing a template-argument-list. */ |
2445 | parser->in_template_argument_list_p = false; | |
2446 | ||
0e59b3fb MM |
2447 | /* We are not in an iteration statement. */ |
2448 | parser->in_iteration_statement_p = false; | |
2449 | ||
2450 | /* We are not in a switch statement. */ | |
2451 | parser->in_switch_statement_p = false; | |
2452 | ||
4f8163b1 MM |
2453 | /* We are not parsing a type-id inside an expression. */ |
2454 | parser->in_type_id_in_expr_p = false; | |
2455 | ||
03fd3f84 | 2456 | /* Declarations aren't implicitly extern "C". */ |
7d381002 MA |
2457 | parser->implicit_extern_c = false; |
2458 | ||
c162c75e MA |
2459 | /* String literals should be translated to the execution character set. */ |
2460 | parser->translate_strings_p = true; | |
2461 | ||
a723baf1 MM |
2462 | /* The unparsed function queue is empty. */ |
2463 | parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE); | |
2464 | ||
2465 | /* There are no classes being defined. */ | |
2466 | parser->num_classes_being_defined = 0; | |
2467 | ||
2468 | /* No template parameters apply. */ | |
2469 | parser->num_template_parameter_lists = 0; | |
2470 | ||
2471 | return parser; | |
2472 | } | |
2473 | ||
2cfe82fe ZW |
2474 | /* Create a cp_lexer structure which will emit the tokens in CACHE |
2475 | and push it onto the parser's lexer stack. This is used for delayed | |
2476 | parsing of in-class method bodies and default arguments, and should | |
2477 | not be confused with tentative parsing. */ | |
2478 | static void | |
2479 | cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache) | |
2480 | { | |
2481 | cp_lexer *lexer = cp_lexer_new_from_tokens (cache); | |
2482 | lexer->next = parser->lexer; | |
2483 | parser->lexer = lexer; | |
2484 | ||
2485 | /* Move the current source position to that of the first token in the | |
2486 | new lexer. */ | |
2487 | cp_lexer_set_source_position_from_token (lexer->next_token); | |
2488 | } | |
2489 | ||
2490 | /* Pop the top lexer off the parser stack. This is never used for the | |
2491 | "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */ | |
2492 | static void | |
2493 | cp_parser_pop_lexer (cp_parser *parser) | |
2494 | { | |
2495 | cp_lexer *lexer = parser->lexer; | |
2496 | parser->lexer = lexer->next; | |
2497 | cp_lexer_destroy (lexer); | |
2498 | ||
2499 | /* Put the current source position back where it was before this | |
2500 | lexer was pushed. */ | |
2501 | cp_lexer_set_source_position_from_token (parser->lexer->next_token); | |
2502 | } | |
2503 | ||
a723baf1 MM |
2504 | /* Lexical conventions [gram.lex] */ |
2505 | ||
2506 | /* Parse an identifier. Returns an IDENTIFIER_NODE representing the | |
2507 | identifier. */ | |
2508 | ||
21526606 | 2509 | static tree |
94edc4ab | 2510 | cp_parser_identifier (cp_parser* parser) |
a723baf1 MM |
2511 | { |
2512 | cp_token *token; | |
2513 | ||
2514 | /* Look for the identifier. */ | |
2515 | token = cp_parser_require (parser, CPP_NAME, "identifier"); | |
2516 | /* Return the value. */ | |
2517 | return token ? token->value : error_mark_node; | |
2518 | } | |
2519 | ||
c162c75e MA |
2520 | /* Parse a sequence of adjacent string constants. Returns a |
2521 | TREE_STRING representing the combined, nul-terminated string | |
2522 | constant. If TRANSLATE is true, translate the string to the | |
2523 | execution character set. If WIDE_OK is true, a wide string is | |
2524 | invalid here. | |
2525 | ||
2526 | C++98 [lex.string] says that if a narrow string literal token is | |
2527 | adjacent to a wide string literal token, the behavior is undefined. | |
2528 | However, C99 6.4.5p4 says that this results in a wide string literal. | |
2529 | We follow C99 here, for consistency with the C front end. | |
2530 | ||
2531 | This code is largely lifted from lex_string() in c-lex.c. | |
2532 | ||
2533 | FUTURE: ObjC++ will need to handle @-strings here. */ | |
2534 | static tree | |
2535 | cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok) | |
2536 | { | |
2537 | tree value; | |
2538 | bool wide = false; | |
2539 | size_t count; | |
2540 | struct obstack str_ob; | |
2541 | cpp_string str, istr, *strs; | |
2542 | cp_token *tok; | |
2543 | ||
2544 | tok = cp_lexer_peek_token (parser->lexer); | |
2545 | if (!cp_parser_is_string_literal (tok)) | |
2546 | { | |
2547 | cp_parser_error (parser, "expected string-literal"); | |
2548 | return error_mark_node; | |
2549 | } | |
2550 | ||
9688c3b8 | 2551 | /* Try to avoid the overhead of creating and destroying an obstack |
c162c75e | 2552 | for the common case of just one string. */ |
2cfe82fe ZW |
2553 | if (!cp_parser_is_string_literal |
2554 | (cp_lexer_peek_nth_token (parser->lexer, 2))) | |
c162c75e | 2555 | { |
2cfe82fe ZW |
2556 | cp_lexer_consume_token (parser->lexer); |
2557 | ||
c162c75e MA |
2558 | str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value); |
2559 | str.len = TREE_STRING_LENGTH (tok->value); | |
2560 | count = 1; | |
2561 | if (tok->type == CPP_WSTRING) | |
2562 | wide = true; | |
c162c75e MA |
2563 | |
2564 | strs = &str; | |
2565 | } | |
2566 | else | |
2567 | { | |
2568 | gcc_obstack_init (&str_ob); | |
2569 | count = 0; | |
2570 | ||
2571 | do | |
2572 | { | |
2cfe82fe | 2573 | cp_lexer_consume_token (parser->lexer); |
c162c75e MA |
2574 | count++; |
2575 | str.text = (unsigned char *)TREE_STRING_POINTER (tok->value); | |
2576 | str.len = TREE_STRING_LENGTH (tok->value); | |
2577 | if (tok->type == CPP_WSTRING) | |
2578 | wide = true; | |
2579 | ||
2580 | obstack_grow (&str_ob, &str, sizeof (cpp_string)); | |
2581 | ||
2cfe82fe | 2582 | tok = cp_lexer_peek_token (parser->lexer); |
c162c75e MA |
2583 | } |
2584 | while (cp_parser_is_string_literal (tok)); | |
2585 | ||
2586 | strs = (cpp_string *) obstack_finish (&str_ob); | |
2587 | } | |
2588 | ||
2589 | if (wide && !wide_ok) | |
2590 | { | |
2591 | cp_parser_error (parser, "a wide string is invalid in this context"); | |
2592 | wide = false; | |
2593 | } | |
2594 | ||
2595 | if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate) | |
2596 | (parse_in, strs, count, &istr, wide)) | |
2597 | { | |
2598 | value = build_string (istr.len, (char *)istr.text); | |
2599 | free ((void *)istr.text); | |
2600 | ||
2601 | TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node; | |
2602 | value = fix_string_type (value); | |
2603 | } | |
2604 | else | |
2605 | /* cpp_interpret_string has issued an error. */ | |
2606 | value = error_mark_node; | |
2607 | ||
2608 | if (count > 1) | |
2609 | obstack_free (&str_ob, 0); | |
2610 | ||
2611 | return value; | |
2612 | } | |
2613 | ||
2614 | ||
a723baf1 MM |
2615 | /* Basic concepts [gram.basic] */ |
2616 | ||
2617 | /* Parse a translation-unit. | |
2618 | ||
2619 | translation-unit: | |
21526606 | 2620 | declaration-seq [opt] |
a723baf1 MM |
2621 | |
2622 | Returns TRUE if all went well. */ | |
2623 | ||
2624 | static bool | |
94edc4ab | 2625 | cp_parser_translation_unit (cp_parser* parser) |
a723baf1 | 2626 | { |
058b15c1 MM |
2627 | /* The address of the first non-permanent object on the declarator |
2628 | obstack. */ | |
2629 | static void *declarator_obstack_base; | |
2630 | ||
2631 | bool success; | |
98ca843c | 2632 | |
058b15c1 MM |
2633 | /* Create the declarator obstack, if necessary. */ |
2634 | if (!cp_error_declarator) | |
2635 | { | |
2636 | gcc_obstack_init (&declarator_obstack); | |
2637 | /* Create the error declarator. */ | |
2638 | cp_error_declarator = make_declarator (cdk_error); | |
2639 | /* Create the empty parameter list. */ | |
62d1db17 | 2640 | no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE); |
058b15c1 MM |
2641 | /* Remember where the base of the declarator obstack lies. */ |
2642 | declarator_obstack_base = obstack_next_free (&declarator_obstack); | |
2643 | } | |
2644 | ||
a723baf1 MM |
2645 | while (true) |
2646 | { | |
2647 | cp_parser_declaration_seq_opt (parser); | |
2648 | ||
2649 | /* If there are no tokens left then all went well. */ | |
2650 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
058b15c1 | 2651 | { |
03fd3f84 | 2652 | /* Get rid of the token array; we don't need it any more. */ |
c162c75e MA |
2653 | cp_lexer_destroy (parser->lexer); |
2654 | parser->lexer = NULL; | |
2655 | ||
7d381002 MA |
2656 | /* This file might have been a context that's implicitly extern |
2657 | "C". If so, pop the lang context. (Only relevant for PCH.) */ | |
2658 | if (parser->implicit_extern_c) | |
2659 | { | |
2660 | pop_lang_context (); | |
2661 | parser->implicit_extern_c = false; | |
2662 | } | |
2663 | ||
058b15c1 MM |
2664 | /* Finish up. */ |
2665 | finish_translation_unit (); | |
21526606 | 2666 | |
058b15c1 MM |
2667 | success = true; |
2668 | break; | |
2669 | } | |
2670 | else | |
2671 | { | |
2672 | cp_parser_error (parser, "expected declaration"); | |
2673 | success = false; | |
2674 | break; | |
2675 | } | |
a723baf1 MM |
2676 | } |
2677 | ||
058b15c1 | 2678 | /* Make sure the declarator obstack was fully cleaned up. */ |
50bc768d NS |
2679 | gcc_assert (obstack_next_free (&declarator_obstack) |
2680 | == declarator_obstack_base); | |
a723baf1 MM |
2681 | |
2682 | /* All went well. */ | |
058b15c1 | 2683 | return success; |
a723baf1 MM |
2684 | } |
2685 | ||
2686 | /* Expressions [gram.expr] */ | |
2687 | ||
2688 | /* Parse a primary-expression. | |
2689 | ||
2690 | primary-expression: | |
2691 | literal | |
2692 | this | |
2693 | ( expression ) | |
2694 | id-expression | |
2695 | ||
2696 | GNU Extensions: | |
2697 | ||
2698 | primary-expression: | |
2699 | ( compound-statement ) | |
2700 | __builtin_va_arg ( assignment-expression , type-id ) | |
2701 | ||
e58a9aa1 ZL |
2702 | Objective-C++ Extension: |
2703 | ||
2704 | primary-expression: | |
2705 | objc-expression | |
2706 | ||
a723baf1 MM |
2707 | literal: |
2708 | __null | |
2709 | ||
93678513 MM |
2710 | CAST_P is true if this primary expression is the target of a cast. |
2711 | ||
21526606 | 2712 | Returns a representation of the expression. |
a723baf1 | 2713 | |
21526606 | 2714 | *IDK indicates what kind of id-expression (if any) was present. |
a723baf1 MM |
2715 | |
2716 | *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be | |
2717 | used as the operand of a pointer-to-member. In that case, | |
2718 | *QUALIFYING_CLASS gives the class that is used as the qualifying | |
2719 | class in the pointer-to-member. */ | |
2720 | ||
2721 | static tree | |
21526606 | 2722 | cp_parser_primary_expression (cp_parser *parser, |
93678513 | 2723 | bool cast_p, |
b3445994 | 2724 | cp_id_kind *idk, |
a723baf1 MM |
2725 | tree *qualifying_class) |
2726 | { | |
2727 | cp_token *token; | |
2728 | ||
2729 | /* Assume the primary expression is not an id-expression. */ | |
b3445994 | 2730 | *idk = CP_ID_KIND_NONE; |
a723baf1 MM |
2731 | /* And that it cannot be used as pointer-to-member. */ |
2732 | *qualifying_class = NULL_TREE; | |
2733 | ||
2734 | /* Peek at the next token. */ | |
2735 | token = cp_lexer_peek_token (parser->lexer); | |
2736 | switch (token->type) | |
2737 | { | |
2738 | /* literal: | |
2739 | integer-literal | |
2740 | character-literal | |
2741 | floating-literal | |
2742 | string-literal | |
2743 | boolean-literal */ | |
2744 | case CPP_CHAR: | |
2745 | case CPP_WCHAR: | |
a723baf1 MM |
2746 | case CPP_NUMBER: |
2747 | token = cp_lexer_consume_token (parser->lexer); | |
93678513 MM |
2748 | /* Floating-point literals are only allowed in an integral |
2749 | constant expression if they are cast to an integral or | |
2750 | enumeration type. */ | |
2751 | if (TREE_CODE (token->value) == REAL_CST | |
8c94c75a MM |
2752 | && parser->integral_constant_expression_p |
2753 | && pedantic) | |
93678513 MM |
2754 | { |
2755 | /* CAST_P will be set even in invalid code like "int(2.7 + | |
2756 | ...)". Therefore, we have to check that the next token | |
2757 | is sure to end the cast. */ | |
2758 | if (cast_p) | |
2759 | { | |
2760 | cp_token *next_token; | |
2761 | ||
2762 | next_token = cp_lexer_peek_token (parser->lexer); | |
2763 | if (/* The comma at the end of an | |
2764 | enumerator-definition. */ | |
2765 | next_token->type != CPP_COMMA | |
2766 | /* The curly brace at the end of an enum-specifier. */ | |
2767 | && next_token->type != CPP_CLOSE_BRACE | |
2768 | /* The end of a statement. */ | |
2769 | && next_token->type != CPP_SEMICOLON | |
2770 | /* The end of the cast-expression. */ | |
2771 | && next_token->type != CPP_CLOSE_PAREN | |
2772 | /* The end of an array bound. */ | |
2773 | && next_token->type != CPP_CLOSE_SQUARE) | |
2774 | cast_p = false; | |
2775 | } | |
2776 | ||
2777 | /* If we are within a cast, then the constraint that the | |
2778 | cast is to an integral or enumeration type will be | |
2779 | checked at that point. If we are not within a cast, then | |
2780 | this code is invalid. */ | |
2781 | if (!cast_p) | |
2782 | cp_parser_non_integral_constant_expression | |
2783 | (parser, "floating-point literal"); | |
2784 | } | |
a723baf1 MM |
2785 | return token->value; |
2786 | ||
0173bb6f AO |
2787 | case CPP_STRING: |
2788 | case CPP_WSTRING: | |
c162c75e MA |
2789 | /* ??? Should wide strings be allowed when parser->translate_strings_p |
2790 | is false (i.e. in attributes)? If not, we can kill the third | |
2791 | argument to cp_parser_string_literal. */ | |
2792 | return cp_parser_string_literal (parser, | |
2793 | parser->translate_strings_p, | |
2794 | true); | |
0173bb6f | 2795 | |
a723baf1 MM |
2796 | case CPP_OPEN_PAREN: |
2797 | { | |
2798 | tree expr; | |
2799 | bool saved_greater_than_is_operator_p; | |
2800 | ||
2801 | /* Consume the `('. */ | |
2802 | cp_lexer_consume_token (parser->lexer); | |
2803 | /* Within a parenthesized expression, a `>' token is always | |
2804 | the greater-than operator. */ | |
21526606 | 2805 | saved_greater_than_is_operator_p |
a723baf1 MM |
2806 | = parser->greater_than_is_operator_p; |
2807 | parser->greater_than_is_operator_p = true; | |
2808 | /* If we see `( { ' then we are looking at the beginning of | |
2809 | a GNU statement-expression. */ | |
2810 | if (cp_parser_allow_gnu_extensions_p (parser) | |
2811 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
2812 | { | |
2813 | /* Statement-expressions are not allowed by the standard. */ | |
2814 | if (pedantic) | |
21526606 EC |
2815 | pedwarn ("ISO C++ forbids braced-groups within expressions"); |
2816 | ||
a723baf1 MM |
2817 | /* And they're not allowed outside of a function-body; you |
2818 | cannot, for example, write: | |
21526606 | 2819 | |
a723baf1 | 2820 | int i = ({ int j = 3; j + 1; }); |
21526606 | 2821 | |
a723baf1 MM |
2822 | at class or namespace scope. */ |
2823 | if (!at_function_scope_p ()) | |
2824 | error ("statement-expressions are allowed only inside functions"); | |
2825 | /* Start the statement-expression. */ | |
2826 | expr = begin_stmt_expr (); | |
2827 | /* Parse the compound-statement. */ | |
325c3691 | 2828 | cp_parser_compound_statement (parser, expr, false); |
a723baf1 | 2829 | /* Finish up. */ |
303b7406 | 2830 | expr = finish_stmt_expr (expr, false); |
a723baf1 MM |
2831 | } |
2832 | else | |
2833 | { | |
2834 | /* Parse the parenthesized expression. */ | |
93678513 | 2835 | expr = cp_parser_expression (parser, cast_p); |
a723baf1 MM |
2836 | /* Let the front end know that this expression was |
2837 | enclosed in parentheses. This matters in case, for | |
2838 | example, the expression is of the form `A::B', since | |
2839 | `&A::B' might be a pointer-to-member, but `&(A::B)' is | |
2840 | not. */ | |
2841 | finish_parenthesized_expr (expr); | |
2842 | } | |
2843 | /* The `>' token might be the end of a template-id or | |
2844 | template-parameter-list now. */ | |
21526606 | 2845 | parser->greater_than_is_operator_p |
a723baf1 MM |
2846 | = saved_greater_than_is_operator_p; |
2847 | /* Consume the `)'. */ | |
2848 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
2849 | cp_parser_skip_to_end_of_statement (parser); | |
2850 | ||
2851 | return expr; | |
2852 | } | |
2853 | ||
2854 | case CPP_KEYWORD: | |
2855 | switch (token->keyword) | |
2856 | { | |
2857 | /* These two are the boolean literals. */ | |
2858 | case RID_TRUE: | |
2859 | cp_lexer_consume_token (parser->lexer); | |
2860 | return boolean_true_node; | |
2861 | case RID_FALSE: | |
2862 | cp_lexer_consume_token (parser->lexer); | |
2863 | return boolean_false_node; | |
21526606 | 2864 | |
a723baf1 MM |
2865 | /* The `__null' literal. */ |
2866 | case RID_NULL: | |
2867 | cp_lexer_consume_token (parser->lexer); | |
2868 | return null_node; | |
2869 | ||
2870 | /* Recognize the `this' keyword. */ | |
2871 | case RID_THIS: | |
2872 | cp_lexer_consume_token (parser->lexer); | |
2873 | if (parser->local_variables_forbidden_p) | |
2874 | { | |
2a13a625 | 2875 | error ("%<this%> may not be used in this context"); |
a723baf1 MM |
2876 | return error_mark_node; |
2877 | } | |
14d22dd6 | 2878 | /* Pointers cannot appear in constant-expressions. */ |
625cbf93 MM |
2879 | if (cp_parser_non_integral_constant_expression (parser, |
2880 | "`this'")) | |
2881 | return error_mark_node; | |
a723baf1 MM |
2882 | return finish_this_expr (); |
2883 | ||
2884 | /* The `operator' keyword can be the beginning of an | |
2885 | id-expression. */ | |
2886 | case RID_OPERATOR: | |
2887 | goto id_expression; | |
2888 | ||
2889 | case RID_FUNCTION_NAME: | |
2890 | case RID_PRETTY_FUNCTION_NAME: | |
2891 | case RID_C99_FUNCTION_NAME: | |
2892 | /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and | |
2893 | __func__ are the names of variables -- but they are | |
2894 | treated specially. Therefore, they are handled here, | |
2895 | rather than relying on the generic id-expression logic | |
21526606 | 2896 | below. Grammatically, these names are id-expressions. |
a723baf1 MM |
2897 | |
2898 | Consume the token. */ | |
2899 | token = cp_lexer_consume_token (parser->lexer); | |
2900 | /* Look up the name. */ | |
2901 | return finish_fname (token->value); | |
2902 | ||
2903 | case RID_VA_ARG: | |
2904 | { | |
2905 | tree expression; | |
2906 | tree type; | |
2907 | ||
2908 | /* The `__builtin_va_arg' construct is used to handle | |
2909 | `va_arg'. Consume the `__builtin_va_arg' token. */ | |
2910 | cp_lexer_consume_token (parser->lexer); | |
2911 | /* Look for the opening `('. */ | |
2912 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
2913 | /* Now, parse the assignment-expression. */ | |
93678513 MM |
2914 | expression = cp_parser_assignment_expression (parser, |
2915 | /*cast_p=*/false); | |
a723baf1 MM |
2916 | /* Look for the `,'. */ |
2917 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
2918 | /* Parse the type-id. */ | |
2919 | type = cp_parser_type_id (parser); | |
2920 | /* Look for the closing `)'. */ | |
2921 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
14d22dd6 MM |
2922 | /* Using `va_arg' in a constant-expression is not |
2923 | allowed. */ | |
625cbf93 MM |
2924 | if (cp_parser_non_integral_constant_expression (parser, |
2925 | "`va_arg'")) | |
2926 | return error_mark_node; | |
a723baf1 MM |
2927 | return build_x_va_arg (expression, type); |
2928 | } | |
2929 | ||
263ee052 | 2930 | case RID_OFFSETOF: |
7a3ea201 | 2931 | return cp_parser_builtin_offsetof (parser); |
263ee052 | 2932 | |
e58a9aa1 ZL |
2933 | /* Objective-C++ expressions. */ |
2934 | case RID_AT_ENCODE: | |
2935 | case RID_AT_PROTOCOL: | |
2936 | case RID_AT_SELECTOR: | |
2937 | return cp_parser_objc_expression (parser); | |
2938 | ||
a723baf1 MM |
2939 | default: |
2940 | cp_parser_error (parser, "expected primary-expression"); | |
2941 | return error_mark_node; | |
2942 | } | |
a723baf1 MM |
2943 | |
2944 | /* An id-expression can start with either an identifier, a | |
2945 | `::' as the beginning of a qualified-id, or the "operator" | |
2946 | keyword. */ | |
2947 | case CPP_NAME: | |
2948 | case CPP_SCOPE: | |
2949 | case CPP_TEMPLATE_ID: | |
2950 | case CPP_NESTED_NAME_SPECIFIER: | |
2951 | { | |
2952 | tree id_expression; | |
2953 | tree decl; | |
b3445994 | 2954 | const char *error_msg; |
a723baf1 MM |
2955 | |
2956 | id_expression: | |
2957 | /* Parse the id-expression. */ | |
21526606 EC |
2958 | id_expression |
2959 | = cp_parser_id_expression (parser, | |
a723baf1 MM |
2960 | /*template_keyword_p=*/false, |
2961 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
2962 | /*template_p=*/NULL, |
2963 | /*declarator_p=*/false); | |
a723baf1 MM |
2964 | if (id_expression == error_mark_node) |
2965 | return error_mark_node; | |
2966 | /* If we have a template-id, then no further lookup is | |
2967 | required. If the template-id was for a template-class, we | |
2968 | will sometimes have a TYPE_DECL at this point. */ | |
2969 | else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR | |
2970 | || TREE_CODE (id_expression) == TYPE_DECL) | |
2971 | decl = id_expression; | |
2972 | /* Look up the name. */ | |
21526606 | 2973 | else |
a723baf1 | 2974 | { |
8f78f01f MM |
2975 | bool ambiguous_p; |
2976 | ||
2977 | decl = cp_parser_lookup_name (parser, id_expression, | |
fc6a28d7 | 2978 | none_type, |
8f78f01f MM |
2979 | /*is_template=*/false, |
2980 | /*is_namespace=*/false, | |
2981 | /*check_dependency=*/true, | |
2982 | &ambiguous_p); | |
2983 | /* If the lookup was ambiguous, an error will already have | |
2984 | been issued. */ | |
2985 | if (ambiguous_p) | |
2986 | return error_mark_node; | |
e58a9aa1 ZL |
2987 | |
2988 | /* In Objective-C++, an instance variable (ivar) may be preferred | |
2989 | to whatever cp_parser_lookup_name() found. */ | |
2990 | decl = objc_lookup_ivar (decl, id_expression); | |
2991 | ||
a723baf1 MM |
2992 | /* If name lookup gives us a SCOPE_REF, then the |
2993 | qualifying scope was dependent. Just propagate the | |
2994 | name. */ | |
2995 | if (TREE_CODE (decl) == SCOPE_REF) | |
2996 | { | |
2997 | if (TYPE_P (TREE_OPERAND (decl, 0))) | |
2998 | *qualifying_class = TREE_OPERAND (decl, 0); | |
2999 | return decl; | |
3000 | } | |
3001 | /* Check to see if DECL is a local variable in a context | |
3002 | where that is forbidden. */ | |
3003 | if (parser->local_variables_forbidden_p | |
3004 | && local_variable_p (decl)) | |
3005 | { | |
3006 | /* It might be that we only found DECL because we are | |
3007 | trying to be generous with pre-ISO scoping rules. | |
3008 | For example, consider: | |
3009 | ||
3010 | int i; | |
3011 | void g() { | |
3012 | for (int i = 0; i < 10; ++i) {} | |
3013 | extern void f(int j = i); | |
3014 | } | |
3015 | ||
21526606 | 3016 | Here, name look up will originally find the out |
a723baf1 MM |
3017 | of scope `i'. We need to issue a warning message, |
3018 | but then use the global `i'. */ | |
3019 | decl = check_for_out_of_scope_variable (decl); | |
3020 | if (local_variable_p (decl)) | |
3021 | { | |
2a13a625 | 3022 | error ("local variable %qD may not appear in this context", |
a723baf1 MM |
3023 | decl); |
3024 | return error_mark_node; | |
3025 | } | |
3026 | } | |
c006d942 | 3027 | } |
21526606 EC |
3028 | |
3029 | decl = finish_id_expression (id_expression, decl, parser->scope, | |
b3445994 | 3030 | idk, qualifying_class, |
67c03833 JM |
3031 | parser->integral_constant_expression_p, |
3032 | parser->allow_non_integral_constant_expression_p, | |
3033 | &parser->non_integral_constant_expression_p, | |
b3445994 MM |
3034 | &error_msg); |
3035 | if (error_msg) | |
3036 | cp_parser_error (parser, error_msg); | |
a723baf1 MM |
3037 | return decl; |
3038 | } | |
3039 | ||
3040 | /* Anything else is an error. */ | |
3041 | default: | |
e58a9aa1 ZL |
3042 | /* ...unless we have an Objective-C++ message or string literal, that is. */ |
3043 | if (c_dialect_objc () | |
3044 | && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING)) | |
3045 | return cp_parser_objc_expression (parser); | |
3046 | ||
a723baf1 MM |
3047 | cp_parser_error (parser, "expected primary-expression"); |
3048 | return error_mark_node; | |
3049 | } | |
3050 | } | |
3051 | ||
3052 | /* Parse an id-expression. | |
3053 | ||
3054 | id-expression: | |
3055 | unqualified-id | |
3056 | qualified-id | |
3057 | ||
3058 | qualified-id: | |
3059 | :: [opt] nested-name-specifier template [opt] unqualified-id | |
3060 | :: identifier | |
3061 | :: operator-function-id | |
3062 | :: template-id | |
3063 | ||
3064 | Return a representation of the unqualified portion of the | |
3065 | identifier. Sets PARSER->SCOPE to the qualifying scope if there is | |
3066 | a `::' or nested-name-specifier. | |
3067 | ||
3068 | Often, if the id-expression was a qualified-id, the caller will | |
3069 | want to make a SCOPE_REF to represent the qualified-id. This | |
3070 | function does not do this in order to avoid wastefully creating | |
3071 | SCOPE_REFs when they are not required. | |
3072 | ||
a723baf1 MM |
3073 | If TEMPLATE_KEYWORD_P is true, then we have just seen the |
3074 | `template' keyword. | |
3075 | ||
3076 | If CHECK_DEPENDENCY_P is false, then names are looked up inside | |
21526606 | 3077 | uninstantiated templates. |
a723baf1 | 3078 | |
15d2cb19 | 3079 | If *TEMPLATE_P is non-NULL, it is set to true iff the |
a723baf1 | 3080 | `template' keyword is used to explicitly indicate that the entity |
21526606 | 3081 | named is a template. |
f3c2dfc6 MM |
3082 | |
3083 | If DECLARATOR_P is true, the id-expression is appearing as part of | |
cd0be382 | 3084 | a declarator, rather than as part of an expression. */ |
a723baf1 MM |
3085 | |
3086 | static tree | |
3087 | cp_parser_id_expression (cp_parser *parser, | |
3088 | bool template_keyword_p, | |
3089 | bool check_dependency_p, | |
f3c2dfc6 MM |
3090 | bool *template_p, |
3091 | bool declarator_p) | |
a723baf1 MM |
3092 | { |
3093 | bool global_scope_p; | |
3094 | bool nested_name_specifier_p; | |
3095 | ||
3096 | /* Assume the `template' keyword was not used. */ | |
3097 | if (template_p) | |
3098 | *template_p = false; | |
3099 | ||
3100 | /* Look for the optional `::' operator. */ | |
21526606 EC |
3101 | global_scope_p |
3102 | = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) | |
a723baf1 MM |
3103 | != NULL_TREE); |
3104 | /* Look for the optional nested-name-specifier. */ | |
21526606 | 3105 | nested_name_specifier_p |
a723baf1 MM |
3106 | = (cp_parser_nested_name_specifier_opt (parser, |
3107 | /*typename_keyword_p=*/false, | |
3108 | check_dependency_p, | |
a668c6ad | 3109 | /*type_p=*/false, |
a52eb3bc | 3110 | declarator_p) |
a723baf1 MM |
3111 | != NULL_TREE); |
3112 | /* If there is a nested-name-specifier, then we are looking at | |
3113 | the first qualified-id production. */ | |
3114 | if (nested_name_specifier_p) | |
3115 | { | |
3116 | tree saved_scope; | |
3117 | tree saved_object_scope; | |
3118 | tree saved_qualifying_scope; | |
3119 | tree unqualified_id; | |
3120 | bool is_template; | |
3121 | ||
3122 | /* See if the next token is the `template' keyword. */ | |
3123 | if (!template_p) | |
3124 | template_p = &is_template; | |
3125 | *template_p = cp_parser_optional_template_keyword (parser); | |
3126 | /* Name lookup we do during the processing of the | |
3127 | unqualified-id might obliterate SCOPE. */ | |
3128 | saved_scope = parser->scope; | |
3129 | saved_object_scope = parser->object_scope; | |
3130 | saved_qualifying_scope = parser->qualifying_scope; | |
3131 | /* Process the final unqualified-id. */ | |
3132 | unqualified_id = cp_parser_unqualified_id (parser, *template_p, | |
f3c2dfc6 MM |
3133 | check_dependency_p, |
3134 | declarator_p); | |
a723baf1 MM |
3135 | /* Restore the SAVED_SCOPE for our caller. */ |
3136 | parser->scope = saved_scope; | |
3137 | parser->object_scope = saved_object_scope; | |
3138 | parser->qualifying_scope = saved_qualifying_scope; | |
3139 | ||
3140 | return unqualified_id; | |
3141 | } | |
3142 | /* Otherwise, if we are in global scope, then we are looking at one | |
3143 | of the other qualified-id productions. */ | |
3144 | else if (global_scope_p) | |
3145 | { | |
3146 | cp_token *token; | |
3147 | tree id; | |
3148 | ||
e5976695 MM |
3149 | /* Peek at the next token. */ |
3150 | token = cp_lexer_peek_token (parser->lexer); | |
3151 | ||
3152 | /* If it's an identifier, and the next token is not a "<", then | |
3153 | we can avoid the template-id case. This is an optimization | |
3154 | for this common case. */ | |
21526606 EC |
3155 | if (token->type == CPP_NAME |
3156 | && !cp_parser_nth_token_starts_template_argument_list_p | |
f4abade9 | 3157 | (parser, 2)) |
e5976695 MM |
3158 | return cp_parser_identifier (parser); |
3159 | ||
a723baf1 MM |
3160 | cp_parser_parse_tentatively (parser); |
3161 | /* Try a template-id. */ | |
21526606 | 3162 | id = cp_parser_template_id (parser, |
a723baf1 | 3163 | /*template_keyword_p=*/false, |
a668c6ad MM |
3164 | /*check_dependency_p=*/true, |
3165 | declarator_p); | |
a723baf1 MM |
3166 | /* If that worked, we're done. */ |
3167 | if (cp_parser_parse_definitely (parser)) | |
3168 | return id; | |
3169 | ||
e5976695 MM |
3170 | /* Peek at the next token. (Changes in the token buffer may |
3171 | have invalidated the pointer obtained above.) */ | |
a723baf1 MM |
3172 | token = cp_lexer_peek_token (parser->lexer); |
3173 | ||
3174 | switch (token->type) | |
3175 | { | |
3176 | case CPP_NAME: | |
3177 | return cp_parser_identifier (parser); | |
3178 | ||
3179 | case CPP_KEYWORD: | |
3180 | if (token->keyword == RID_OPERATOR) | |
3181 | return cp_parser_operator_function_id (parser); | |
3182 | /* Fall through. */ | |
21526606 | 3183 | |
a723baf1 MM |
3184 | default: |
3185 | cp_parser_error (parser, "expected id-expression"); | |
3186 | return error_mark_node; | |
3187 | } | |
3188 | } | |
3189 | else | |
3190 | return cp_parser_unqualified_id (parser, template_keyword_p, | |
f3c2dfc6 MM |
3191 | /*check_dependency_p=*/true, |
3192 | declarator_p); | |
a723baf1 MM |
3193 | } |
3194 | ||
3195 | /* Parse an unqualified-id. | |
3196 | ||
3197 | unqualified-id: | |
3198 | identifier | |
3199 | operator-function-id | |
3200 | conversion-function-id | |
3201 | ~ class-name | |
3202 | template-id | |
3203 | ||
3204 | If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template' | |
3205 | keyword, in a construct like `A::template ...'. | |
3206 | ||
3207 | Returns a representation of unqualified-id. For the `identifier' | |
3208 | production, an IDENTIFIER_NODE is returned. For the `~ class-name' | |
3209 | production a BIT_NOT_EXPR is returned; the operand of the | |
3210 | BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the | |
3211 | other productions, see the documentation accompanying the | |
3212 | corresponding parsing functions. If CHECK_DEPENDENCY_P is false, | |
f3c2dfc6 MM |
3213 | names are looked up in uninstantiated templates. If DECLARATOR_P |
3214 | is true, the unqualified-id is appearing as part of a declarator, | |
3215 | rather than as part of an expression. */ | |
a723baf1 MM |
3216 | |
3217 | static tree | |
21526606 | 3218 | cp_parser_unqualified_id (cp_parser* parser, |
94edc4ab | 3219 | bool template_keyword_p, |
f3c2dfc6 MM |
3220 | bool check_dependency_p, |
3221 | bool declarator_p) | |
a723baf1 MM |
3222 | { |
3223 | cp_token *token; | |
3224 | ||
3225 | /* Peek at the next token. */ | |
3226 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 3227 | |
a723baf1 MM |
3228 | switch (token->type) |
3229 | { | |
3230 | case CPP_NAME: | |
3231 | { | |
3232 | tree id; | |
3233 | ||
3234 | /* We don't know yet whether or not this will be a | |
3235 | template-id. */ | |
3236 | cp_parser_parse_tentatively (parser); | |
3237 | /* Try a template-id. */ | |
3238 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
3239 | check_dependency_p, |
3240 | declarator_p); | |
a723baf1 MM |
3241 | /* If it worked, we're done. */ |
3242 | if (cp_parser_parse_definitely (parser)) | |
3243 | return id; | |
3244 | /* Otherwise, it's an ordinary identifier. */ | |
3245 | return cp_parser_identifier (parser); | |
3246 | } | |
3247 | ||
3248 | case CPP_TEMPLATE_ID: | |
3249 | return cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
3250 | check_dependency_p, |
3251 | declarator_p); | |
a723baf1 MM |
3252 | |
3253 | case CPP_COMPL: | |
3254 | { | |
3255 | tree type_decl; | |
3256 | tree qualifying_scope; | |
3257 | tree object_scope; | |
3258 | tree scope; | |
88e95ee3 | 3259 | bool done; |
a723baf1 MM |
3260 | |
3261 | /* Consume the `~' token. */ | |
3262 | cp_lexer_consume_token (parser->lexer); | |
3263 | /* Parse the class-name. The standard, as written, seems to | |
3264 | say that: | |
3265 | ||
3266 | template <typename T> struct S { ~S (); }; | |
3267 | template <typename T> S<T>::~S() {} | |
3268 | ||
3269 | is invalid, since `~' must be followed by a class-name, but | |
3270 | `S<T>' is dependent, and so not known to be a class. | |
3271 | That's not right; we need to look in uninstantiated | |
3272 | templates. A further complication arises from: | |
3273 | ||
3274 | template <typename T> void f(T t) { | |
3275 | t.T::~T(); | |
21526606 | 3276 | } |
a723baf1 MM |
3277 | |
3278 | Here, it is not possible to look up `T' in the scope of `T' | |
3279 | itself. We must look in both the current scope, and the | |
21526606 | 3280 | scope of the containing complete expression. |
a723baf1 MM |
3281 | |
3282 | Yet another issue is: | |
3283 | ||
3284 | struct S { | |
3285 | int S; | |
3286 | ~S(); | |
3287 | }; | |
3288 | ||
3289 | S::~S() {} | |
3290 | ||
3291 | The standard does not seem to say that the `S' in `~S' | |
3292 | should refer to the type `S' and not the data member | |
3293 | `S::S'. */ | |
3294 | ||
3295 | /* DR 244 says that we look up the name after the "~" in the | |
3296 | same scope as we looked up the qualifying name. That idea | |
3297 | isn't fully worked out; it's more complicated than that. */ | |
3298 | scope = parser->scope; | |
3299 | object_scope = parser->object_scope; | |
3300 | qualifying_scope = parser->qualifying_scope; | |
3301 | ||
3302 | /* If the name is of the form "X::~X" it's OK. */ | |
3303 | if (scope && TYPE_P (scope) | |
3304 | && cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
21526606 | 3305 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 | 3306 | == CPP_OPEN_PAREN) |
21526606 | 3307 | && (cp_lexer_peek_token (parser->lexer)->value |
a723baf1 MM |
3308 | == TYPE_IDENTIFIER (scope))) |
3309 | { | |
3310 | cp_lexer_consume_token (parser->lexer); | |
3311 | return build_nt (BIT_NOT_EXPR, scope); | |
3312 | } | |
3313 | ||
3314 | /* If there was an explicit qualification (S::~T), first look | |
3315 | in the scope given by the qualification (i.e., S). */ | |
88e95ee3 | 3316 | done = false; |
e3754f9c | 3317 | type_decl = NULL_TREE; |
a723baf1 MM |
3318 | if (scope) |
3319 | { | |
3320 | cp_parser_parse_tentatively (parser); | |
21526606 | 3321 | type_decl = cp_parser_class_name (parser, |
a723baf1 MM |
3322 | /*typename_keyword_p=*/false, |
3323 | /*template_keyword_p=*/false, | |
fc6a28d7 | 3324 | none_type, |
a723baf1 | 3325 | /*check_dependency=*/false, |
a668c6ad MM |
3326 | /*class_head_p=*/false, |
3327 | declarator_p); | |
a723baf1 | 3328 | if (cp_parser_parse_definitely (parser)) |
88e95ee3 | 3329 | done = true; |
a723baf1 MM |
3330 | } |
3331 | /* In "N::S::~S", look in "N" as well. */ | |
88e95ee3 | 3332 | if (!done && scope && qualifying_scope) |
a723baf1 MM |
3333 | { |
3334 | cp_parser_parse_tentatively (parser); | |
3335 | parser->scope = qualifying_scope; | |
3336 | parser->object_scope = NULL_TREE; | |
3337 | parser->qualifying_scope = NULL_TREE; | |
21526606 EC |
3338 | type_decl |
3339 | = cp_parser_class_name (parser, | |
a723baf1 MM |
3340 | /*typename_keyword_p=*/false, |
3341 | /*template_keyword_p=*/false, | |
fc6a28d7 | 3342 | none_type, |
a723baf1 | 3343 | /*check_dependency=*/false, |
a668c6ad MM |
3344 | /*class_head_p=*/false, |
3345 | declarator_p); | |
a723baf1 | 3346 | if (cp_parser_parse_definitely (parser)) |
88e95ee3 | 3347 | done = true; |
a723baf1 MM |
3348 | } |
3349 | /* In "p->S::~T", look in the scope given by "*p" as well. */ | |
88e95ee3 | 3350 | else if (!done && object_scope) |
a723baf1 MM |
3351 | { |
3352 | cp_parser_parse_tentatively (parser); | |
3353 | parser->scope = object_scope; | |
3354 | parser->object_scope = NULL_TREE; | |
3355 | parser->qualifying_scope = NULL_TREE; | |
21526606 EC |
3356 | type_decl |
3357 | = cp_parser_class_name (parser, | |
a723baf1 MM |
3358 | /*typename_keyword_p=*/false, |
3359 | /*template_keyword_p=*/false, | |
fc6a28d7 | 3360 | none_type, |
a723baf1 | 3361 | /*check_dependency=*/false, |
a668c6ad MM |
3362 | /*class_head_p=*/false, |
3363 | declarator_p); | |
a723baf1 | 3364 | if (cp_parser_parse_definitely (parser)) |
88e95ee3 | 3365 | done = true; |
a723baf1 MM |
3366 | } |
3367 | /* Look in the surrounding context. */ | |
88e95ee3 MM |
3368 | if (!done) |
3369 | { | |
3370 | parser->scope = NULL_TREE; | |
3371 | parser->object_scope = NULL_TREE; | |
3372 | parser->qualifying_scope = NULL_TREE; | |
3373 | type_decl | |
3374 | = cp_parser_class_name (parser, | |
3375 | /*typename_keyword_p=*/false, | |
3376 | /*template_keyword_p=*/false, | |
3377 | none_type, | |
3378 | /*check_dependency=*/false, | |
3379 | /*class_head_p=*/false, | |
3380 | declarator_p); | |
3381 | } | |
a723baf1 MM |
3382 | /* If an error occurred, assume that the name of the |
3383 | destructor is the same as the name of the qualifying | |
3384 | class. That allows us to keep parsing after running | |
3385 | into ill-formed destructor names. */ | |
3386 | if (type_decl == error_mark_node && scope && TYPE_P (scope)) | |
3387 | return build_nt (BIT_NOT_EXPR, scope); | |
3388 | else if (type_decl == error_mark_node) | |
3389 | return error_mark_node; | |
3390 | ||
f3c2dfc6 MM |
3391 | /* [class.dtor] |
3392 | ||
3393 | A typedef-name that names a class shall not be used as the | |
3394 | identifier in the declarator for a destructor declaration. */ | |
21526606 | 3395 | if (declarator_p |
f3c2dfc6 | 3396 | && !DECL_IMPLICIT_TYPEDEF_P (type_decl) |
33a69702 VR |
3397 | && !DECL_SELF_REFERENCE_P (type_decl) |
3398 | && !cp_parser_uncommitted_to_tentative_parse_p (parser)) | |
2a13a625 | 3399 | error ("typedef-name %qD used as destructor declarator", |
f3c2dfc6 MM |
3400 | type_decl); |
3401 | ||
a723baf1 MM |
3402 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); |
3403 | } | |
3404 | ||
3405 | case CPP_KEYWORD: | |
3406 | if (token->keyword == RID_OPERATOR) | |
3407 | { | |
3408 | tree id; | |
3409 | ||
3410 | /* This could be a template-id, so we try that first. */ | |
3411 | cp_parser_parse_tentatively (parser); | |
3412 | /* Try a template-id. */ | |
3413 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
3414 | /*check_dependency_p=*/true, |
3415 | declarator_p); | |
a723baf1 MM |
3416 | /* If that worked, we're done. */ |
3417 | if (cp_parser_parse_definitely (parser)) | |
3418 | return id; | |
3419 | /* We still don't know whether we're looking at an | |
3420 | operator-function-id or a conversion-function-id. */ | |
3421 | cp_parser_parse_tentatively (parser); | |
3422 | /* Try an operator-function-id. */ | |
3423 | id = cp_parser_operator_function_id (parser); | |
3424 | /* If that didn't work, try a conversion-function-id. */ | |
3425 | if (!cp_parser_parse_definitely (parser)) | |
3426 | id = cp_parser_conversion_function_id (parser); | |
3427 | ||
3428 | return id; | |
3429 | } | |
3430 | /* Fall through. */ | |
3431 | ||
3432 | default: | |
3433 | cp_parser_error (parser, "expected unqualified-id"); | |
3434 | return error_mark_node; | |
3435 | } | |
3436 | } | |
3437 | ||
3438 | /* Parse an (optional) nested-name-specifier. | |
3439 | ||
3440 | nested-name-specifier: | |
3441 | class-or-namespace-name :: nested-name-specifier [opt] | |
3442 | class-or-namespace-name :: template nested-name-specifier [opt] | |
3443 | ||
3444 | PARSER->SCOPE should be set appropriately before this function is | |
3445 | called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in | |
3446 | effect. TYPE_P is TRUE if we non-type bindings should be ignored | |
3447 | in name lookups. | |
3448 | ||
3449 | Sets PARSER->SCOPE to the class (TYPE) or namespace | |
3450 | (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves | |
3451 | it unchanged if there is no nested-name-specifier. Returns the new | |
21526606 | 3452 | scope iff there is a nested-name-specifier, or NULL_TREE otherwise. |
a668c6ad MM |
3453 | |
3454 | If IS_DECLARATION is TRUE, the nested-name-specifier is known to be | |
3455 | part of a declaration and/or decl-specifier. */ | |
a723baf1 MM |
3456 | |
3457 | static tree | |
21526606 EC |
3458 | cp_parser_nested_name_specifier_opt (cp_parser *parser, |
3459 | bool typename_keyword_p, | |
a723baf1 | 3460 | bool check_dependency_p, |
a668c6ad MM |
3461 | bool type_p, |
3462 | bool is_declaration) | |
a723baf1 MM |
3463 | { |
3464 | bool success = false; | |
3465 | tree access_check = NULL_TREE; | |
0c5e4866 NS |
3466 | cp_token_position start = 0; |
3467 | cp_token *token; | |
a723baf1 MM |
3468 | |
3469 | /* If the next token corresponds to a nested name specifier, there | |
2050a1bb | 3470 | is no need to reparse it. However, if CHECK_DEPENDENCY_P is |
21526606 | 3471 | false, it may have been true before, in which case something |
2050a1bb MM |
3472 | like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier |
3473 | of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when | |
3474 | CHECK_DEPENDENCY_P is false, we have to fall through into the | |
3475 | main loop. */ | |
3476 | if (check_dependency_p | |
3477 | && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER)) | |
3478 | { | |
3479 | cp_parser_pre_parsed_nested_name_specifier (parser); | |
a723baf1 MM |
3480 | return parser->scope; |
3481 | } | |
3482 | ||
3483 | /* Remember where the nested-name-specifier starts. */ | |
0b16f8f4 | 3484 | if (cp_parser_uncommitted_to_tentative_parse_p (parser)) |
0c5e4866 | 3485 | start = cp_lexer_token_position (parser->lexer, false); |
a723baf1 | 3486 | |
8d241e0b | 3487 | push_deferring_access_checks (dk_deferred); |
cf22909c | 3488 | |
a723baf1 MM |
3489 | while (true) |
3490 | { | |
3491 | tree new_scope; | |
3492 | tree old_scope; | |
3493 | tree saved_qualifying_scope; | |
a723baf1 MM |
3494 | bool template_keyword_p; |
3495 | ||
2050a1bb MM |
3496 | /* Spot cases that cannot be the beginning of a |
3497 | nested-name-specifier. */ | |
3498 | token = cp_lexer_peek_token (parser->lexer); | |
3499 | ||
3500 | /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process | |
3501 | the already parsed nested-name-specifier. */ | |
3502 | if (token->type == CPP_NESTED_NAME_SPECIFIER) | |
3503 | { | |
3504 | /* Grab the nested-name-specifier and continue the loop. */ | |
3505 | cp_parser_pre_parsed_nested_name_specifier (parser); | |
3506 | success = true; | |
3507 | continue; | |
3508 | } | |
3509 | ||
a723baf1 MM |
3510 | /* Spot cases that cannot be the beginning of a |
3511 | nested-name-specifier. On the second and subsequent times | |
3512 | through the loop, we look for the `template' keyword. */ | |
f7b5ecd9 | 3513 | if (success && token->keyword == RID_TEMPLATE) |
a723baf1 MM |
3514 | ; |
3515 | /* A template-id can start a nested-name-specifier. */ | |
f7b5ecd9 | 3516 | else if (token->type == CPP_TEMPLATE_ID) |
a723baf1 MM |
3517 | ; |
3518 | else | |
3519 | { | |
3520 | /* If the next token is not an identifier, then it is | |
3521 | definitely not a class-or-namespace-name. */ | |
f7b5ecd9 | 3522 | if (token->type != CPP_NAME) |
a723baf1 MM |
3523 | break; |
3524 | /* If the following token is neither a `<' (to begin a | |
3525 | template-id), nor a `::', then we are not looking at a | |
3526 | nested-name-specifier. */ | |
3527 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
f4abade9 GB |
3528 | if (token->type != CPP_SCOPE |
3529 | && !cp_parser_nth_token_starts_template_argument_list_p | |
3530 | (parser, 2)) | |
a723baf1 MM |
3531 | break; |
3532 | } | |
3533 | ||
3534 | /* The nested-name-specifier is optional, so we parse | |
3535 | tentatively. */ | |
3536 | cp_parser_parse_tentatively (parser); | |
3537 | ||
3538 | /* Look for the optional `template' keyword, if this isn't the | |
3539 | first time through the loop. */ | |
3540 | if (success) | |
3541 | template_keyword_p = cp_parser_optional_template_keyword (parser); | |
3542 | else | |
3543 | template_keyword_p = false; | |
3544 | ||
3545 | /* Save the old scope since the name lookup we are about to do | |
3546 | might destroy it. */ | |
3547 | old_scope = parser->scope; | |
3548 | saved_qualifying_scope = parser->qualifying_scope; | |
a52eb3bc MM |
3549 | /* In a declarator-id like "X<T>::I::Y<T>" we must be able to |
3550 | look up names in "X<T>::I" in order to determine that "Y" is | |
3551 | a template. So, if we have a typename at this point, we make | |
3552 | an effort to look through it. */ | |
67bcc252 MM |
3553 | if (is_declaration |
3554 | && !typename_keyword_p | |
3555 | && parser->scope | |
a52eb3bc MM |
3556 | && TREE_CODE (parser->scope) == TYPENAME_TYPE) |
3557 | parser->scope = resolve_typename_type (parser->scope, | |
3558 | /*only_current_p=*/false); | |
a723baf1 | 3559 | /* Parse the qualifying entity. */ |
21526606 | 3560 | new_scope |
a723baf1 MM |
3561 | = cp_parser_class_or_namespace_name (parser, |
3562 | typename_keyword_p, | |
3563 | template_keyword_p, | |
3564 | check_dependency_p, | |
a668c6ad MM |
3565 | type_p, |
3566 | is_declaration); | |
a723baf1 MM |
3567 | /* Look for the `::' token. */ |
3568 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
3569 | ||
3570 | /* If we found what we wanted, we keep going; otherwise, we're | |
3571 | done. */ | |
3572 | if (!cp_parser_parse_definitely (parser)) | |
3573 | { | |
3574 | bool error_p = false; | |
3575 | ||
3576 | /* Restore the OLD_SCOPE since it was valid before the | |
3577 | failed attempt at finding the last | |
3578 | class-or-namespace-name. */ | |
3579 | parser->scope = old_scope; | |
3580 | parser->qualifying_scope = saved_qualifying_scope; | |
3581 | /* If the next token is an identifier, and the one after | |
3582 | that is a `::', then any valid interpretation would have | |
3583 | found a class-or-namespace-name. */ | |
3584 | while (cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
21526606 | 3585 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 | 3586 | == CPP_SCOPE) |
21526606 | 3587 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type |
a723baf1 MM |
3588 | != CPP_COMPL)) |
3589 | { | |
3590 | token = cp_lexer_consume_token (parser->lexer); | |
21526606 | 3591 | if (!error_p) |
a723baf1 MM |
3592 | { |
3593 | tree decl; | |
3594 | ||
3595 | decl = cp_parser_lookup_name_simple (parser, token->value); | |
3596 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
2a13a625 | 3597 | error ("%qD used without template parameters", decl); |
a723baf1 | 3598 | else |
21526606 EC |
3599 | cp_parser_name_lookup_error |
3600 | (parser, token->value, decl, | |
4bb8ca28 | 3601 | "is not a class or namespace"); |
a723baf1 MM |
3602 | parser->scope = NULL_TREE; |
3603 | error_p = true; | |
eea9800f MM |
3604 | /* Treat this as a successful nested-name-specifier |
3605 | due to: | |
3606 | ||
3607 | [basic.lookup.qual] | |
3608 | ||
3609 | If the name found is not a class-name (clause | |
3610 | _class_) or namespace-name (_namespace.def_), the | |
3611 | program is ill-formed. */ | |
3612 | success = true; | |
a723baf1 MM |
3613 | } |
3614 | cp_lexer_consume_token (parser->lexer); | |
3615 | } | |
3616 | break; | |
3617 | } | |
3618 | ||
3619 | /* We've found one valid nested-name-specifier. */ | |
3620 | success = true; | |
3621 | /* Make sure we look in the right scope the next time through | |
3622 | the loop. */ | |
21526606 | 3623 | parser->scope = (TREE_CODE (new_scope) == TYPE_DECL |
a723baf1 MM |
3624 | ? TREE_TYPE (new_scope) |
3625 | : new_scope); | |
3626 | /* If it is a class scope, try to complete it; we are about to | |
3627 | be looking up names inside the class. */ | |
8fbc5ae7 MM |
3628 | if (TYPE_P (parser->scope) |
3629 | /* Since checking types for dependency can be expensive, | |
3630 | avoid doing it if the type is already complete. */ | |
3631 | && !COMPLETE_TYPE_P (parser->scope) | |
3632 | /* Do not try to complete dependent types. */ | |
1fb3244a | 3633 | && !dependent_type_p (parser->scope)) |
a723baf1 MM |
3634 | complete_type (parser->scope); |
3635 | } | |
3636 | ||
cf22909c KL |
3637 | /* Retrieve any deferred checks. Do not pop this access checks yet |
3638 | so the memory will not be reclaimed during token replacing below. */ | |
3639 | access_check = get_deferred_access_checks (); | |
3640 | ||
a723baf1 MM |
3641 | /* If parsing tentatively, replace the sequence of tokens that makes |
3642 | up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER | |
3643 | token. That way, should we re-parse the token stream, we will | |
3644 | not have to repeat the effort required to do the parse, nor will | |
3645 | we issue duplicate error messages. */ | |
0c5e4866 | 3646 | if (success && start) |
a723baf1 | 3647 | { |
0c5e4866 NS |
3648 | cp_token *token = cp_lexer_token_at (parser->lexer, start); |
3649 | ||
a723baf1 MM |
3650 | /* Reset the contents of the START token. */ |
3651 | token->type = CPP_NESTED_NAME_SPECIFIER; | |
3652 | token->value = build_tree_list (access_check, parser->scope); | |
3653 | TREE_TYPE (token->value) = parser->qualifying_scope; | |
3654 | token->keyword = RID_MAX; | |
0c5e4866 | 3655 | |
a723baf1 | 3656 | /* Purge all subsequent tokens. */ |
0c5e4866 | 3657 | cp_lexer_purge_tokens_after (parser->lexer, start); |
a723baf1 MM |
3658 | } |
3659 | ||
cf22909c | 3660 | pop_deferring_access_checks (); |
a723baf1 MM |
3661 | return success ? parser->scope : NULL_TREE; |
3662 | } | |
3663 | ||
3664 | /* Parse a nested-name-specifier. See | |
3665 | cp_parser_nested_name_specifier_opt for details. This function | |
3666 | behaves identically, except that it will an issue an error if no | |
3667 | nested-name-specifier is present, and it will return | |
3668 | ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier | |
3669 | is present. */ | |
3670 | ||
3671 | static tree | |
21526606 EC |
3672 | cp_parser_nested_name_specifier (cp_parser *parser, |
3673 | bool typename_keyword_p, | |
a723baf1 | 3674 | bool check_dependency_p, |
a668c6ad MM |
3675 | bool type_p, |
3676 | bool is_declaration) | |
a723baf1 MM |
3677 | { |
3678 | tree scope; | |
3679 | ||
3680 | /* Look for the nested-name-specifier. */ | |
3681 | scope = cp_parser_nested_name_specifier_opt (parser, | |
3682 | typename_keyword_p, | |
3683 | check_dependency_p, | |
a668c6ad MM |
3684 | type_p, |
3685 | is_declaration); | |
a723baf1 MM |
3686 | /* If it was not present, issue an error message. */ |
3687 | if (!scope) | |
3688 | { | |
3689 | cp_parser_error (parser, "expected nested-name-specifier"); | |
eb5abb39 | 3690 | parser->scope = NULL_TREE; |
a723baf1 MM |
3691 | return error_mark_node; |
3692 | } | |
3693 | ||
3694 | return scope; | |
3695 | } | |
3696 | ||
3697 | /* Parse a class-or-namespace-name. | |
3698 | ||
3699 | class-or-namespace-name: | |
3700 | class-name | |
3701 | namespace-name | |
3702 | ||
3703 | TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect. | |
3704 | TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect. | |
3705 | CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up. | |
3706 | TYPE_P is TRUE iff the next name should be taken as a class-name, | |
3707 | even the same name is declared to be another entity in the same | |
3708 | scope. | |
3709 | ||
3710 | Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL) | |
eea9800f MM |
3711 | specified by the class-or-namespace-name. If neither is found the |
3712 | ERROR_MARK_NODE is returned. */ | |
a723baf1 MM |
3713 | |
3714 | static tree | |
21526606 | 3715 | cp_parser_class_or_namespace_name (cp_parser *parser, |
a723baf1 MM |
3716 | bool typename_keyword_p, |
3717 | bool template_keyword_p, | |
3718 | bool check_dependency_p, | |
a668c6ad MM |
3719 | bool type_p, |
3720 | bool is_declaration) | |
a723baf1 MM |
3721 | { |
3722 | tree saved_scope; | |
3723 | tree saved_qualifying_scope; | |
3724 | tree saved_object_scope; | |
3725 | tree scope; | |
eea9800f | 3726 | bool only_class_p; |
a723baf1 | 3727 | |
a723baf1 MM |
3728 | /* Before we try to parse the class-name, we must save away the |
3729 | current PARSER->SCOPE since cp_parser_class_name will destroy | |
3730 | it. */ | |
3731 | saved_scope = parser->scope; | |
3732 | saved_qualifying_scope = parser->qualifying_scope; | |
3733 | saved_object_scope = parser->object_scope; | |
eea9800f MM |
3734 | /* Try for a class-name first. If the SAVED_SCOPE is a type, then |
3735 | there is no need to look for a namespace-name. */ | |
bbaab916 | 3736 | only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope)); |
eea9800f MM |
3737 | if (!only_class_p) |
3738 | cp_parser_parse_tentatively (parser); | |
21526606 | 3739 | scope = cp_parser_class_name (parser, |
a723baf1 MM |
3740 | typename_keyword_p, |
3741 | template_keyword_p, | |
fc6a28d7 | 3742 | type_p ? class_type : none_type, |
a723baf1 | 3743 | check_dependency_p, |
a668c6ad MM |
3744 | /*class_head_p=*/false, |
3745 | is_declaration); | |
a723baf1 | 3746 | /* If that didn't work, try for a namespace-name. */ |
eea9800f | 3747 | if (!only_class_p && !cp_parser_parse_definitely (parser)) |
a723baf1 MM |
3748 | { |
3749 | /* Restore the saved scope. */ | |
3750 | parser->scope = saved_scope; | |
3751 | parser->qualifying_scope = saved_qualifying_scope; | |
3752 | parser->object_scope = saved_object_scope; | |
eea9800f MM |
3753 | /* If we are not looking at an identifier followed by the scope |
3754 | resolution operator, then this is not part of a | |
3755 | nested-name-specifier. (Note that this function is only used | |
3756 | to parse the components of a nested-name-specifier.) */ | |
3757 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME) | |
3758 | || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE) | |
3759 | return error_mark_node; | |
a723baf1 MM |
3760 | scope = cp_parser_namespace_name (parser); |
3761 | } | |
3762 | ||
3763 | return scope; | |
3764 | } | |
3765 | ||
3766 | /* Parse a postfix-expression. | |
3767 | ||
3768 | postfix-expression: | |
3769 | primary-expression | |
3770 | postfix-expression [ expression ] | |
3771 | postfix-expression ( expression-list [opt] ) | |
3772 | simple-type-specifier ( expression-list [opt] ) | |
21526606 | 3773 | typename :: [opt] nested-name-specifier identifier |
a723baf1 MM |
3774 | ( expression-list [opt] ) |
3775 | typename :: [opt] nested-name-specifier template [opt] template-id | |
3776 | ( expression-list [opt] ) | |
3777 | postfix-expression . template [opt] id-expression | |
3778 | postfix-expression -> template [opt] id-expression | |
3779 | postfix-expression . pseudo-destructor-name | |
3780 | postfix-expression -> pseudo-destructor-name | |
3781 | postfix-expression ++ | |
3782 | postfix-expression -- | |
3783 | dynamic_cast < type-id > ( expression ) | |
3784 | static_cast < type-id > ( expression ) | |
3785 | reinterpret_cast < type-id > ( expression ) | |
3786 | const_cast < type-id > ( expression ) | |
3787 | typeid ( expression ) | |
3788 | typeid ( type-id ) | |
3789 | ||
3790 | GNU Extension: | |
21526606 | 3791 | |
a723baf1 MM |
3792 | postfix-expression: |
3793 | ( type-id ) { initializer-list , [opt] } | |
3794 | ||
3795 | This extension is a GNU version of the C99 compound-literal | |
3796 | construct. (The C99 grammar uses `type-name' instead of `type-id', | |
3797 | but they are essentially the same concept.) | |
3798 | ||
3799 | If ADDRESS_P is true, the postfix expression is the operand of the | |
93678513 MM |
3800 | `&' operator. CAST_P is true if this expression is the target of a |
3801 | cast. | |
a723baf1 MM |
3802 | |
3803 | Returns a representation of the expression. */ | |
3804 | ||
3805 | static tree | |
93678513 | 3806 | cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p) |
a723baf1 MM |
3807 | { |
3808 | cp_token *token; | |
3809 | enum rid keyword; | |
b3445994 | 3810 | cp_id_kind idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3811 | tree postfix_expression = NULL_TREE; |
3812 | /* Non-NULL only if the current postfix-expression can be used to | |
3813 | form a pointer-to-member. In that case, QUALIFYING_CLASS is the | |
3814 | class used to qualify the member. */ | |
3815 | tree qualifying_class = NULL_TREE; | |
a723baf1 MM |
3816 | |
3817 | /* Peek at the next token. */ | |
3818 | token = cp_lexer_peek_token (parser->lexer); | |
3819 | /* Some of the productions are determined by keywords. */ | |
3820 | keyword = token->keyword; | |
3821 | switch (keyword) | |
3822 | { | |
3823 | case RID_DYNCAST: | |
3824 | case RID_STATCAST: | |
3825 | case RID_REINTCAST: | |
3826 | case RID_CONSTCAST: | |
3827 | { | |
3828 | tree type; | |
3829 | tree expression; | |
3830 | const char *saved_message; | |
3831 | ||
3832 | /* All of these can be handled in the same way from the point | |
3833 | of view of parsing. Begin by consuming the token | |
3834 | identifying the cast. */ | |
3835 | cp_lexer_consume_token (parser->lexer); | |
21526606 | 3836 | |
a723baf1 MM |
3837 | /* New types cannot be defined in the cast. */ |
3838 | saved_message = parser->type_definition_forbidden_message; | |
3839 | parser->type_definition_forbidden_message | |
3840 | = "types may not be defined in casts"; | |
3841 | ||
3842 | /* Look for the opening `<'. */ | |
3843 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
3844 | /* Parse the type to which we are casting. */ | |
3845 | type = cp_parser_type_id (parser); | |
3846 | /* Look for the closing `>'. */ | |
3847 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
3848 | /* Restore the old message. */ | |
3849 | parser->type_definition_forbidden_message = saved_message; | |
3850 | ||
3851 | /* And the expression which is being cast. */ | |
3852 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
93678513 | 3853 | expression = cp_parser_expression (parser, /*cast_p=*/true); |
a723baf1 MM |
3854 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); |
3855 | ||
14d22dd6 MM |
3856 | /* Only type conversions to integral or enumeration types |
3857 | can be used in constant-expressions. */ | |
67c03833 | 3858 | if (parser->integral_constant_expression_p |
14d22dd6 | 3859 | && !dependent_type_p (type) |
263ee052 | 3860 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
98ca843c | 3861 | && (cp_parser_non_integral_constant_expression |
625cbf93 MM |
3862 | (parser, |
3863 | "a cast to a type other than an integral or " | |
3864 | "enumeration type"))) | |
3865 | return error_mark_node; | |
14d22dd6 | 3866 | |
a723baf1 MM |
3867 | switch (keyword) |
3868 | { | |
3869 | case RID_DYNCAST: | |
3870 | postfix_expression | |
3871 | = build_dynamic_cast (type, expression); | |
3872 | break; | |
3873 | case RID_STATCAST: | |
3874 | postfix_expression | |
3875 | = build_static_cast (type, expression); | |
3876 | break; | |
3877 | case RID_REINTCAST: | |
3878 | postfix_expression | |
3879 | = build_reinterpret_cast (type, expression); | |
3880 | break; | |
3881 | case RID_CONSTCAST: | |
3882 | postfix_expression | |
3883 | = build_const_cast (type, expression); | |
3884 | break; | |
3885 | default: | |
315fb5db | 3886 | gcc_unreachable (); |
a723baf1 MM |
3887 | } |
3888 | } | |
3889 | break; | |
3890 | ||
3891 | case RID_TYPEID: | |
3892 | { | |
3893 | tree type; | |
3894 | const char *saved_message; | |
4f8163b1 | 3895 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
3896 | |
3897 | /* Consume the `typeid' token. */ | |
3898 | cp_lexer_consume_token (parser->lexer); | |
3899 | /* Look for the `(' token. */ | |
3900 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
3901 | /* Types cannot be defined in a `typeid' expression. */ | |
3902 | saved_message = parser->type_definition_forbidden_message; | |
3903 | parser->type_definition_forbidden_message | |
3904 | = "types may not be defined in a `typeid\' expression"; | |
3905 | /* We can't be sure yet whether we're looking at a type-id or an | |
3906 | expression. */ | |
3907 | cp_parser_parse_tentatively (parser); | |
3908 | /* Try a type-id first. */ | |
4f8163b1 MM |
3909 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
3910 | parser->in_type_id_in_expr_p = true; | |
a723baf1 | 3911 | type = cp_parser_type_id (parser); |
4f8163b1 | 3912 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 MM |
3913 | /* Look for the `)' token. Otherwise, we can't be sure that |
3914 | we're not looking at an expression: consider `typeid (int | |
3915 | (3))', for example. */ | |
3916 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3917 | /* If all went well, simply lookup the type-id. */ | |
3918 | if (cp_parser_parse_definitely (parser)) | |
3919 | postfix_expression = get_typeid (type); | |
3920 | /* Otherwise, fall back to the expression variant. */ | |
3921 | else | |
3922 | { | |
3923 | tree expression; | |
3924 | ||
3925 | /* Look for an expression. */ | |
93678513 | 3926 | expression = cp_parser_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
3927 | /* Compute its typeid. */ |
3928 | postfix_expression = build_typeid (expression); | |
3929 | /* Look for the `)' token. */ | |
3930 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3931 | } | |
4424e0da | 3932 | /* `typeid' may not appear in an integral constant expression. */ |
98ca843c | 3933 | if (cp_parser_non_integral_constant_expression(parser, |
4424e0da GB |
3934 | "`typeid' operator")) |
3935 | return error_mark_node; | |
a723baf1 MM |
3936 | /* Restore the saved message. */ |
3937 | parser->type_definition_forbidden_message = saved_message; | |
3938 | } | |
3939 | break; | |
21526606 | 3940 | |
a723baf1 MM |
3941 | case RID_TYPENAME: |
3942 | { | |
3943 | bool template_p = false; | |
3944 | tree id; | |
3945 | tree type; | |
26c895e7 | 3946 | tree scope; |
a723baf1 MM |
3947 | |
3948 | /* Consume the `typename' token. */ | |
3949 | cp_lexer_consume_token (parser->lexer); | |
3950 | /* Look for the optional `::' operator. */ | |
21526606 | 3951 | cp_parser_global_scope_opt (parser, |
a723baf1 | 3952 | /*current_scope_valid_p=*/false); |
26c895e7 KL |
3953 | /* Look for the nested-name-specifier. In case of error here, |
3954 | consume the trailing id to avoid subsequent error messages | |
3955 | for usual cases. */ | |
3956 | scope = cp_parser_nested_name_specifier (parser, | |
3957 | /*typename_keyword_p=*/true, | |
3958 | /*check_dependency_p=*/true, | |
3959 | /*type_p=*/true, | |
3960 | /*is_declaration=*/true); | |
3961 | ||
a723baf1 MM |
3962 | /* Look for the optional `template' keyword. */ |
3963 | template_p = cp_parser_optional_template_keyword (parser); | |
3964 | /* We don't know whether we're looking at a template-id or an | |
3965 | identifier. */ | |
3966 | cp_parser_parse_tentatively (parser); | |
3967 | /* Try a template-id. */ | |
3968 | id = cp_parser_template_id (parser, template_p, | |
a668c6ad MM |
3969 | /*check_dependency_p=*/true, |
3970 | /*is_declaration=*/true); | |
a723baf1 MM |
3971 | /* If that didn't work, try an identifier. */ |
3972 | if (!cp_parser_parse_definitely (parser)) | |
3973 | id = cp_parser_identifier (parser); | |
26c895e7 KL |
3974 | |
3975 | /* Don't process id if nested name specifier is invalid. */ | |
3976 | if (scope == error_mark_node) | |
3977 | return error_mark_node; | |
26bcf8fc MM |
3978 | /* If we look up a template-id in a non-dependent qualifying |
3979 | scope, there's no need to create a dependent type. */ | |
26c895e7 | 3980 | else if (TREE_CODE (id) == TYPE_DECL |
26bcf8fc MM |
3981 | && !dependent_type_p (parser->scope)) |
3982 | type = TREE_TYPE (id); | |
a723baf1 MM |
3983 | /* Create a TYPENAME_TYPE to represent the type to which the |
3984 | functional cast is being performed. */ | |
26bcf8fc | 3985 | else |
98ca843c | 3986 | type = make_typename_type (parser->scope, id, |
fc6a28d7 | 3987 | typename_type, |
26bcf8fc | 3988 | /*complain=*/1); |
a723baf1 MM |
3989 | |
3990 | postfix_expression = cp_parser_functional_cast (parser, type); | |
3991 | } | |
3992 | break; | |
3993 | ||
3994 | default: | |
3995 | { | |
3996 | tree type; | |
3997 | ||
3998 | /* If the next thing is a simple-type-specifier, we may be | |
3999 | looking at a functional cast. We could also be looking at | |
4000 | an id-expression. So, we try the functional cast, and if | |
4001 | that doesn't work we fall back to the primary-expression. */ | |
4002 | cp_parser_parse_tentatively (parser); | |
4003 | /* Look for the simple-type-specifier. */ | |
21526606 | 4004 | type = cp_parser_simple_type_specifier (parser, |
62d1db17 MM |
4005 | /*decl_specs=*/NULL, |
4006 | CP_PARSER_FLAGS_NONE); | |
a723baf1 MM |
4007 | /* Parse the cast itself. */ |
4008 | if (!cp_parser_error_occurred (parser)) | |
21526606 | 4009 | postfix_expression |
a723baf1 MM |
4010 | = cp_parser_functional_cast (parser, type); |
4011 | /* If that worked, we're done. */ | |
4012 | if (cp_parser_parse_definitely (parser)) | |
4013 | break; | |
4014 | ||
4015 | /* If the functional-cast didn't work out, try a | |
4016 | compound-literal. */ | |
14d22dd6 MM |
4017 | if (cp_parser_allow_gnu_extensions_p (parser) |
4018 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
a723baf1 MM |
4019 | { |
4020 | tree initializer_list = NULL_TREE; | |
4f8163b1 | 4021 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
4022 | |
4023 | cp_parser_parse_tentatively (parser); | |
14d22dd6 MM |
4024 | /* Consume the `('. */ |
4025 | cp_lexer_consume_token (parser->lexer); | |
4026 | /* Parse the type. */ | |
4f8163b1 MM |
4027 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
4028 | parser->in_type_id_in_expr_p = true; | |
14d22dd6 | 4029 | type = cp_parser_type_id (parser); |
4f8163b1 | 4030 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
14d22dd6 MM |
4031 | /* Look for the `)'. */ |
4032 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
4033 | /* Look for the `{'. */ | |
4034 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
4035 | /* If things aren't going well, there's no need to | |
4036 | keep going. */ | |
4037 | if (!cp_parser_error_occurred (parser)) | |
a723baf1 | 4038 | { |
39703eb9 | 4039 | bool non_constant_p; |
14d22dd6 | 4040 | /* Parse the initializer-list. */ |
21526606 | 4041 | initializer_list |
39703eb9 | 4042 | = cp_parser_initializer_list (parser, &non_constant_p); |
14d22dd6 MM |
4043 | /* Allow a trailing `,'. */ |
4044 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
4045 | cp_lexer_consume_token (parser->lexer); | |
4046 | /* Look for the final `}'. */ | |
4047 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
a723baf1 MM |
4048 | } |
4049 | /* If that worked, we're definitely looking at a | |
4050 | compound-literal expression. */ | |
4051 | if (cp_parser_parse_definitely (parser)) | |
4052 | { | |
4053 | /* Warn the user that a compound literal is not | |
4054 | allowed in standard C++. */ | |
4055 | if (pedantic) | |
4056 | pedwarn ("ISO C++ forbids compound-literals"); | |
4057 | /* Form the representation of the compound-literal. */ | |
21526606 | 4058 | postfix_expression |
a723baf1 MM |
4059 | = finish_compound_literal (type, initializer_list); |
4060 | break; | |
4061 | } | |
4062 | } | |
4063 | ||
4064 | /* It must be a primary-expression. */ | |
21526606 | 4065 | postfix_expression = cp_parser_primary_expression (parser, |
93678513 | 4066 | cast_p, |
a723baf1 MM |
4067 | &idk, |
4068 | &qualifying_class); | |
4069 | } | |
4070 | break; | |
4071 | } | |
4072 | ||
ee76b931 MM |
4073 | /* If we were avoiding committing to the processing of a |
4074 | qualified-id until we knew whether or not we had a | |
4075 | pointer-to-member, we now know. */ | |
089d6ea7 | 4076 | if (qualifying_class) |
a723baf1 | 4077 | { |
ee76b931 | 4078 | bool done; |
a723baf1 | 4079 | |
ee76b931 MM |
4080 | /* Peek at the next token. */ |
4081 | token = cp_lexer_peek_token (parser->lexer); | |
4082 | done = (token->type != CPP_OPEN_SQUARE | |
4083 | && token->type != CPP_OPEN_PAREN | |
4084 | && token->type != CPP_DOT | |
4085 | && token->type != CPP_DEREF | |
4086 | && token->type != CPP_PLUS_PLUS | |
4087 | && token->type != CPP_MINUS_MINUS); | |
4088 | ||
4089 | postfix_expression = finish_qualified_id_expr (qualifying_class, | |
4090 | postfix_expression, | |
4091 | done, | |
4092 | address_p); | |
4093 | if (done) | |
4094 | return postfix_expression; | |
a723baf1 MM |
4095 | } |
4096 | ||
a723baf1 MM |
4097 | /* Keep looping until the postfix-expression is complete. */ |
4098 | while (true) | |
4099 | { | |
10b1d5e7 MM |
4100 | if (idk == CP_ID_KIND_UNQUALIFIED |
4101 | && TREE_CODE (postfix_expression) == IDENTIFIER_NODE | |
a723baf1 | 4102 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) |
b3445994 | 4103 | /* It is not a Koenig lookup function call. */ |
21526606 | 4104 | postfix_expression |
b3445994 | 4105 | = unqualified_name_lookup_error (postfix_expression); |
21526606 | 4106 | |
a723baf1 MM |
4107 | /* Peek at the next token. */ |
4108 | token = cp_lexer_peek_token (parser->lexer); | |
4109 | ||
4110 | switch (token->type) | |
4111 | { | |
4112 | case CPP_OPEN_SQUARE: | |
7a3ea201 RH |
4113 | postfix_expression |
4114 | = cp_parser_postfix_open_square_expression (parser, | |
4115 | postfix_expression, | |
4116 | false); | |
4117 | idk = CP_ID_KIND_NONE; | |
a723baf1 MM |
4118 | break; |
4119 | ||
4120 | case CPP_OPEN_PAREN: | |
4121 | /* postfix-expression ( expression-list [opt] ) */ | |
4122 | { | |
6d80c4b9 | 4123 | bool koenig_p; |
88a7beb7 MM |
4124 | bool is_builtin_constant_p; |
4125 | bool saved_integral_constant_expression_p = false; | |
4126 | bool saved_non_integral_constant_expression_p = false; | |
4127 | tree args; | |
4128 | ||
4129 | is_builtin_constant_p | |
4130 | = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression); | |
4131 | if (is_builtin_constant_p) | |
4132 | { | |
4133 | /* The whole point of __builtin_constant_p is to allow | |
4134 | non-constant expressions to appear as arguments. */ | |
4135 | saved_integral_constant_expression_p | |
4136 | = parser->integral_constant_expression_p; | |
4137 | saved_non_integral_constant_expression_p | |
4138 | = parser->non_integral_constant_expression_p; | |
4139 | parser->integral_constant_expression_p = false; | |
4140 | } | |
4141 | args = (cp_parser_parenthesized_expression_list | |
4142 | (parser, /*is_attribute_list=*/false, | |
4143 | /*cast_p=*/false, | |
4144 | /*non_constant_p=*/NULL)); | |
4145 | if (is_builtin_constant_p) | |
4146 | { | |
4147 | parser->integral_constant_expression_p | |
4148 | = saved_integral_constant_expression_p; | |
4149 | parser->non_integral_constant_expression_p | |
4150 | = saved_non_integral_constant_expression_p; | |
4151 | } | |
a723baf1 | 4152 | |
7efa3e22 NS |
4153 | if (args == error_mark_node) |
4154 | { | |
4155 | postfix_expression = error_mark_node; | |
4156 | break; | |
4157 | } | |
21526606 | 4158 | |
14d22dd6 MM |
4159 | /* Function calls are not permitted in |
4160 | constant-expressions. */ | |
100d337a MA |
4161 | if (! builtin_valid_in_constant_expr_p (postfix_expression) |
4162 | && cp_parser_non_integral_constant_expression (parser, | |
4163 | "a function call")) | |
14d22dd6 | 4164 | { |
625cbf93 MM |
4165 | postfix_expression = error_mark_node; |
4166 | break; | |
14d22dd6 | 4167 | } |
a723baf1 | 4168 | |
6d80c4b9 | 4169 | koenig_p = false; |
399dedb9 NS |
4170 | if (idk == CP_ID_KIND_UNQUALIFIED) |
4171 | { | |
89d594a2 NS |
4172 | if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE) |
4173 | { | |
4174 | if (args) | |
4175 | { | |
4176 | koenig_p = true; | |
4177 | postfix_expression | |
4178 | = perform_koenig_lookup (postfix_expression, args); | |
4179 | } | |
4180 | else | |
4181 | postfix_expression | |
4182 | = unqualified_fn_lookup_error (postfix_expression); | |
4183 | } | |
676e33ca MM |
4184 | /* We do not perform argument-dependent lookup if |
4185 | normal lookup finds a non-function, in accordance | |
4186 | with the expected resolution of DR 218. */ | |
89d594a2 | 4187 | else if (args && is_overloaded_fn (postfix_expression)) |
6d80c4b9 | 4188 | { |
89d594a2 NS |
4189 | tree fn = get_first_fn (postfix_expression); |
4190 | ||
4191 | if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) | |
4192 | fn = OVL_CURRENT (TREE_OPERAND (fn, 0)); | |
4193 | ||
4194 | /* Only do argument dependent lookup if regular | |
4195 | lookup does not find a set of member functions. | |
4196 | [basic.lookup.koenig]/2a */ | |
4197 | if (!DECL_FUNCTION_MEMBER_P (fn)) | |
4198 | { | |
4199 | koenig_p = true; | |
4200 | postfix_expression | |
4201 | = perform_koenig_lookup (postfix_expression, args); | |
4202 | } | |
6d80c4b9 | 4203 | } |
399dedb9 | 4204 | } |
21526606 | 4205 | |
d17811fd | 4206 | if (TREE_CODE (postfix_expression) == COMPONENT_REF) |
a723baf1 | 4207 | { |
d17811fd MM |
4208 | tree instance = TREE_OPERAND (postfix_expression, 0); |
4209 | tree fn = TREE_OPERAND (postfix_expression, 1); | |
4210 | ||
4211 | if (processing_template_decl | |
4212 | && (type_dependent_expression_p (instance) | |
4213 | || (!BASELINK_P (fn) | |
4214 | && TREE_CODE (fn) != FIELD_DECL) | |
584672ee | 4215 | || type_dependent_expression_p (fn) |
d17811fd MM |
4216 | || any_type_dependent_arguments_p (args))) |
4217 | { | |
4218 | postfix_expression | |
6de9cd9a DN |
4219 | = build_min_nt (CALL_EXPR, postfix_expression, |
4220 | args, NULL_TREE); | |
d17811fd MM |
4221 | break; |
4222 | } | |
9f880ef9 MM |
4223 | |
4224 | if (BASELINK_P (fn)) | |
4225 | postfix_expression | |
21526606 EC |
4226 | = (build_new_method_call |
4227 | (instance, fn, args, NULL_TREE, | |
4228 | (idk == CP_ID_KIND_QUALIFIED | |
9f880ef9 MM |
4229 | ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL))); |
4230 | else | |
4231 | postfix_expression | |
4232 | = finish_call_expr (postfix_expression, args, | |
4233 | /*disallow_virtual=*/false, | |
4234 | /*koenig_p=*/false); | |
a723baf1 | 4235 | } |
d17811fd MM |
4236 | else if (TREE_CODE (postfix_expression) == OFFSET_REF |
4237 | || TREE_CODE (postfix_expression) == MEMBER_REF | |
4238 | || TREE_CODE (postfix_expression) == DOTSTAR_EXPR) | |
a723baf1 MM |
4239 | postfix_expression = (build_offset_ref_call_from_tree |
4240 | (postfix_expression, args)); | |
b3445994 | 4241 | else if (idk == CP_ID_KIND_QUALIFIED) |
2050a1bb MM |
4242 | /* A call to a static class member, or a namespace-scope |
4243 | function. */ | |
4244 | postfix_expression | |
4245 | = finish_call_expr (postfix_expression, args, | |
6d80c4b9 MM |
4246 | /*disallow_virtual=*/true, |
4247 | koenig_p); | |
a723baf1 | 4248 | else |
2050a1bb | 4249 | /* All other function calls. */ |
21526606 EC |
4250 | postfix_expression |
4251 | = finish_call_expr (postfix_expression, args, | |
6d80c4b9 MM |
4252 | /*disallow_virtual=*/false, |
4253 | koenig_p); | |
a723baf1 MM |
4254 | |
4255 | /* The POSTFIX_EXPRESSION is certainly no longer an id. */ | |
b3445994 | 4256 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
4257 | } |
4258 | break; | |
21526606 | 4259 | |
a723baf1 MM |
4260 | case CPP_DOT: |
4261 | case CPP_DEREF: | |
21526606 EC |
4262 | /* postfix-expression . template [opt] id-expression |
4263 | postfix-expression . pseudo-destructor-name | |
a723baf1 MM |
4264 | postfix-expression -> template [opt] id-expression |
4265 | postfix-expression -> pseudo-destructor-name */ | |
98ca843c | 4266 | |
7a3ea201 RH |
4267 | /* Consume the `.' or `->' operator. */ |
4268 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 4269 | |
7a3ea201 RH |
4270 | postfix_expression |
4271 | = cp_parser_postfix_dot_deref_expression (parser, token->type, | |
4272 | postfix_expression, | |
4273 | false, &idk); | |
a723baf1 MM |
4274 | break; |
4275 | ||
4276 | case CPP_PLUS_PLUS: | |
4277 | /* postfix-expression ++ */ | |
4278 | /* Consume the `++' token. */ | |
4279 | cp_lexer_consume_token (parser->lexer); | |
a5ac3982 | 4280 | /* Generate a representation for the complete expression. */ |
21526606 EC |
4281 | postfix_expression |
4282 | = finish_increment_expr (postfix_expression, | |
a5ac3982 | 4283 | POSTINCREMENT_EXPR); |
14d22dd6 | 4284 | /* Increments may not appear in constant-expressions. */ |
625cbf93 MM |
4285 | if (cp_parser_non_integral_constant_expression (parser, |
4286 | "an increment")) | |
4287 | postfix_expression = error_mark_node; | |
b3445994 | 4288 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
4289 | break; |
4290 | ||
4291 | case CPP_MINUS_MINUS: | |
4292 | /* postfix-expression -- */ | |
4293 | /* Consume the `--' token. */ | |
4294 | cp_lexer_consume_token (parser->lexer); | |
a5ac3982 | 4295 | /* Generate a representation for the complete expression. */ |
21526606 EC |
4296 | postfix_expression |
4297 | = finish_increment_expr (postfix_expression, | |
a5ac3982 | 4298 | POSTDECREMENT_EXPR); |
14d22dd6 | 4299 | /* Decrements may not appear in constant-expressions. */ |
625cbf93 MM |
4300 | if (cp_parser_non_integral_constant_expression (parser, |
4301 | "a decrement")) | |
4302 | postfix_expression = error_mark_node; | |
b3445994 | 4303 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
4304 | break; |
4305 | ||
4306 | default: | |
4307 | return postfix_expression; | |
4308 | } | |
4309 | } | |
4310 | ||
4311 | /* We should never get here. */ | |
315fb5db | 4312 | gcc_unreachable (); |
a723baf1 MM |
4313 | return error_mark_node; |
4314 | } | |
4315 | ||
7a3ea201 RH |
4316 | /* A subroutine of cp_parser_postfix_expression that also gets hijacked |
4317 | by cp_parser_builtin_offsetof. We're looking for | |
4318 | ||
4319 | postfix-expression [ expression ] | |
4320 | ||
4321 | FOR_OFFSETOF is set if we're being called in that context, which | |
4322 | changes how we deal with integer constant expressions. */ | |
4323 | ||
4324 | static tree | |
4325 | cp_parser_postfix_open_square_expression (cp_parser *parser, | |
4326 | tree postfix_expression, | |
4327 | bool for_offsetof) | |
4328 | { | |
4329 | tree index; | |
4330 | ||
4331 | /* Consume the `[' token. */ | |
4332 | cp_lexer_consume_token (parser->lexer); | |
4333 | ||
4334 | /* Parse the index expression. */ | |
4335 | /* ??? For offsetof, there is a question of what to allow here. If | |
4336 | offsetof is not being used in an integral constant expression context, | |
4337 | then we *could* get the right answer by computing the value at runtime. | |
4338 | If we are in an integral constant expression context, then we might | |
4339 | could accept any constant expression; hard to say without analysis. | |
4340 | Rather than open the barn door too wide right away, allow only integer | |
77880ae4 | 4341 | constant expressions here. */ |
7a3ea201 RH |
4342 | if (for_offsetof) |
4343 | index = cp_parser_constant_expression (parser, false, NULL); | |
4344 | else | |
93678513 | 4345 | index = cp_parser_expression (parser, /*cast_p=*/false); |
7a3ea201 RH |
4346 | |
4347 | /* Look for the closing `]'. */ | |
4348 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
4349 | ||
4350 | /* Build the ARRAY_REF. */ | |
4351 | postfix_expression = grok_array_decl (postfix_expression, index); | |
4352 | ||
4353 | /* When not doing offsetof, array references are not permitted in | |
4354 | constant-expressions. */ | |
4355 | if (!for_offsetof | |
4356 | && (cp_parser_non_integral_constant_expression | |
4357 | (parser, "an array reference"))) | |
4358 | postfix_expression = error_mark_node; | |
4359 | ||
4360 | return postfix_expression; | |
4361 | } | |
4362 | ||
4363 | /* A subroutine of cp_parser_postfix_expression that also gets hijacked | |
4364 | by cp_parser_builtin_offsetof. We're looking for | |
4365 | ||
4366 | postfix-expression . template [opt] id-expression | |
4367 | postfix-expression . pseudo-destructor-name | |
4368 | postfix-expression -> template [opt] id-expression | |
4369 | postfix-expression -> pseudo-destructor-name | |
4370 | ||
4371 | FOR_OFFSETOF is set if we're being called in that context. That sorta | |
4372 | limits what of the above we'll actually accept, but nevermind. | |
4373 | TOKEN_TYPE is the "." or "->" token, which will already have been | |
4374 | removed from the stream. */ | |
4375 | ||
4376 | static tree | |
4377 | cp_parser_postfix_dot_deref_expression (cp_parser *parser, | |
4378 | enum cpp_ttype token_type, | |
4379 | tree postfix_expression, | |
4380 | bool for_offsetof, cp_id_kind *idk) | |
4381 | { | |
4382 | tree name; | |
4383 | bool dependent_p; | |
4384 | bool template_p; | |
17a27b4f | 4385 | bool pseudo_destructor_p; |
7a3ea201 RH |
4386 | tree scope = NULL_TREE; |
4387 | ||
4388 | /* If this is a `->' operator, dereference the pointer. */ | |
4389 | if (token_type == CPP_DEREF) | |
4390 | postfix_expression = build_x_arrow (postfix_expression); | |
4391 | /* Check to see whether or not the expression is type-dependent. */ | |
4392 | dependent_p = type_dependent_expression_p (postfix_expression); | |
4393 | /* The identifier following the `->' or `.' is not qualified. */ | |
4394 | parser->scope = NULL_TREE; | |
4395 | parser->qualifying_scope = NULL_TREE; | |
4396 | parser->object_scope = NULL_TREE; | |
4397 | *idk = CP_ID_KIND_NONE; | |
4398 | /* Enter the scope corresponding to the type of the object | |
4399 | given by the POSTFIX_EXPRESSION. */ | |
4400 | if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE) | |
4401 | { | |
4402 | scope = TREE_TYPE (postfix_expression); | |
4403 | /* According to the standard, no expression should ever have | |
4404 | reference type. Unfortunately, we do not currently match | |
4405 | the standard in this respect in that our internal representation | |
4406 | of an expression may have reference type even when the standard | |
4407 | says it does not. Therefore, we have to manually obtain the | |
4408 | underlying type here. */ | |
4409 | scope = non_reference (scope); | |
4410 | /* The type of the POSTFIX_EXPRESSION must be complete. */ | |
4411 | scope = complete_type_or_else (scope, NULL_TREE); | |
4412 | /* Let the name lookup machinery know that we are processing a | |
4413 | class member access expression. */ | |
4414 | parser->context->object_type = scope; | |
4415 | /* If something went wrong, we want to be able to discern that case, | |
4416 | as opposed to the case where there was no SCOPE due to the type | |
4417 | of expression being dependent. */ | |
4418 | if (!scope) | |
4419 | scope = error_mark_node; | |
4420 | /* If the SCOPE was erroneous, make the various semantic analysis | |
4421 | functions exit quickly -- and without issuing additional error | |
4422 | messages. */ | |
4423 | if (scope == error_mark_node) | |
4424 | postfix_expression = error_mark_node; | |
4425 | } | |
4426 | ||
17a27b4f MM |
4427 | /* Assume this expression is not a pseudo-destructor access. */ |
4428 | pseudo_destructor_p = false; | |
4429 | ||
4430 | /* If the SCOPE is a scalar type, then, if this is a valid program, | |
4431 | we must be looking at a pseudo-destructor-name. */ | |
4432 | if (scope && SCALAR_TYPE_P (scope)) | |
7a3ea201 | 4433 | { |
17a27b4f MM |
4434 | tree s; |
4435 | tree type; | |
4436 | ||
4437 | cp_parser_parse_tentatively (parser); | |
4438 | /* Parse the pseudo-destructor-name. */ | |
4439 | s = NULL_TREE; | |
4440 | cp_parser_pseudo_destructor_name (parser, &s, &type); | |
4441 | if (cp_parser_parse_definitely (parser)) | |
4442 | { | |
4443 | pseudo_destructor_p = true; | |
4444 | postfix_expression | |
4445 | = finish_pseudo_destructor_expr (postfix_expression, | |
4446 | s, TREE_TYPE (type)); | |
4447 | } | |
4448 | } | |
4449 | ||
4450 | if (!pseudo_destructor_p) | |
4451 | { | |
4452 | /* If the SCOPE is not a scalar type, we are looking at an | |
4453 | ordinary class member access expression, rather than a | |
4454 | pseudo-destructor-name. */ | |
7a3ea201 RH |
4455 | template_p = cp_parser_optional_template_keyword (parser); |
4456 | /* Parse the id-expression. */ | |
4457 | name = cp_parser_id_expression (parser, template_p, | |
4458 | /*check_dependency_p=*/true, | |
4459 | /*template_p=*/NULL, | |
4460 | /*declarator_p=*/false); | |
4461 | /* In general, build a SCOPE_REF if the member name is qualified. | |
4462 | However, if the name was not dependent and has already been | |
4463 | resolved; there is no need to build the SCOPE_REF. For example; | |
4464 | ||
4465 | struct X { void f(); }; | |
4466 | template <typename T> void f(T* t) { t->X::f(); } | |
4467 | ||
4468 | Even though "t" is dependent, "X::f" is not and has been resolved | |
4469 | to a BASELINK; there is no need to include scope information. */ | |
4470 | ||
4471 | /* But we do need to remember that there was an explicit scope for | |
4472 | virtual function calls. */ | |
4473 | if (parser->scope) | |
4474 | *idk = CP_ID_KIND_QUALIFIED; | |
4475 | ||
fc6a28d7 MM |
4476 | /* If the name is a template-id that names a type, we will get a |
4477 | TYPE_DECL here. That is invalid code. */ | |
4478 | if (TREE_CODE (name) == TYPE_DECL) | |
7a3ea201 | 4479 | { |
fc6a28d7 MM |
4480 | error ("invalid use of %qD", name); |
4481 | postfix_expression = error_mark_node; | |
4482 | } | |
4483 | else | |
4484 | { | |
4485 | if (name != error_mark_node && !BASELINK_P (name) && parser->scope) | |
4486 | { | |
4487 | name = build_nt (SCOPE_REF, parser->scope, name); | |
4488 | parser->scope = NULL_TREE; | |
4489 | parser->qualifying_scope = NULL_TREE; | |
4490 | parser->object_scope = NULL_TREE; | |
4491 | } | |
4492 | if (scope && name && BASELINK_P (name)) | |
4493 | adjust_result_of_qualified_name_lookup | |
4494 | (name, BINFO_TYPE (BASELINK_BINFO (name)), scope); | |
4495 | postfix_expression | |
4496 | = finish_class_member_access_expr (postfix_expression, name); | |
7a3ea201 | 4497 | } |
7a3ea201 | 4498 | } |
7a3ea201 RH |
4499 | |
4500 | /* We no longer need to look up names in the scope of the object on | |
4501 | the left-hand side of the `.' or `->' operator. */ | |
4502 | parser->context->object_type = NULL_TREE; | |
4503 | ||
4504 | /* Outside of offsetof, these operators may not appear in | |
4505 | constant-expressions. */ | |
4506 | if (!for_offsetof | |
98ca843c | 4507 | && (cp_parser_non_integral_constant_expression |
7a3ea201 RH |
4508 | (parser, token_type == CPP_DEREF ? "'->'" : "`.'"))) |
4509 | postfix_expression = error_mark_node; | |
4510 | ||
4511 | return postfix_expression; | |
4512 | } | |
4513 | ||
7efa3e22 | 4514 | /* Parse a parenthesized expression-list. |
a723baf1 MM |
4515 | |
4516 | expression-list: | |
4517 | assignment-expression | |
4518 | expression-list, assignment-expression | |
4519 | ||
7efa3e22 NS |
4520 | attribute-list: |
4521 | expression-list | |
4522 | identifier | |
4523 | identifier, expression-list | |
4524 | ||
93678513 MM |
4525 | CAST_P is true if this expression is the target of a cast. |
4526 | ||
a723baf1 MM |
4527 | Returns a TREE_LIST. The TREE_VALUE of each node is a |
4528 | representation of an assignment-expression. Note that a TREE_LIST | |
7efa3e22 NS |
4529 | is returned even if there is only a single expression in the list. |
4530 | error_mark_node is returned if the ( and or ) are | |
4531 | missing. NULL_TREE is returned on no expressions. The parentheses | |
4532 | are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute | |
39703eb9 MM |
4533 | list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P |
4534 | indicates whether or not all of the expressions in the list were | |
4535 | constant. */ | |
a723baf1 MM |
4536 | |
4537 | static tree | |
21526606 | 4538 | cp_parser_parenthesized_expression_list (cp_parser* parser, |
39703eb9 | 4539 | bool is_attribute_list, |
93678513 | 4540 | bool cast_p, |
39703eb9 | 4541 | bool *non_constant_p) |
a723baf1 MM |
4542 | { |
4543 | tree expression_list = NULL_TREE; | |
1ed3dfd5 | 4544 | bool fold_expr_p = is_attribute_list; |
7efa3e22 | 4545 | tree identifier = NULL_TREE; |
39703eb9 MM |
4546 | |
4547 | /* Assume all the expressions will be constant. */ | |
4548 | if (non_constant_p) | |
4549 | *non_constant_p = false; | |
4550 | ||
7efa3e22 NS |
4551 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) |
4552 | return error_mark_node; | |
21526606 | 4553 | |
a723baf1 | 4554 | /* Consume expressions until there are no more. */ |
7efa3e22 NS |
4555 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) |
4556 | while (true) | |
4557 | { | |
4558 | tree expr; | |
21526606 | 4559 | |
7efa3e22 NS |
4560 | /* At the beginning of attribute lists, check to see if the |
4561 | next token is an identifier. */ | |
4562 | if (is_attribute_list | |
4563 | && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME) | |
4564 | { | |
4565 | cp_token *token; | |
21526606 | 4566 | |
7efa3e22 NS |
4567 | /* Consume the identifier. */ |
4568 | token = cp_lexer_consume_token (parser->lexer); | |
4569 | /* Save the identifier. */ | |
4570 | identifier = token->value; | |
4571 | } | |
4572 | else | |
4573 | { | |
4574 | /* Parse the next assignment-expression. */ | |
39703eb9 MM |
4575 | if (non_constant_p) |
4576 | { | |
4577 | bool expr_non_constant_p; | |
21526606 | 4578 | expr = (cp_parser_constant_expression |
39703eb9 MM |
4579 | (parser, /*allow_non_constant_p=*/true, |
4580 | &expr_non_constant_p)); | |
4581 | if (expr_non_constant_p) | |
4582 | *non_constant_p = true; | |
4583 | } | |
4584 | else | |
93678513 | 4585 | expr = cp_parser_assignment_expression (parser, cast_p); |
a723baf1 | 4586 | |
1ed3dfd5 GB |
4587 | if (fold_expr_p) |
4588 | expr = fold_non_dependent_expr (expr); | |
4589 | ||
7efa3e22 NS |
4590 | /* Add it to the list. We add error_mark_node |
4591 | expressions to the list, so that we can still tell if | |
4592 | the correct form for a parenthesized expression-list | |
4593 | is found. That gives better errors. */ | |
4594 | expression_list = tree_cons (NULL_TREE, expr, expression_list); | |
a723baf1 | 4595 | |
7efa3e22 NS |
4596 | if (expr == error_mark_node) |
4597 | goto skip_comma; | |
4598 | } | |
a723baf1 | 4599 | |
7efa3e22 NS |
4600 | /* After the first item, attribute lists look the same as |
4601 | expression lists. */ | |
4602 | is_attribute_list = false; | |
21526606 | 4603 | |
7efa3e22 NS |
4604 | get_comma:; |
4605 | /* If the next token isn't a `,', then we are done. */ | |
4606 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
4607 | break; | |
4608 | ||
4609 | /* Otherwise, consume the `,' and keep going. */ | |
4610 | cp_lexer_consume_token (parser->lexer); | |
4611 | } | |
21526606 | 4612 | |
7efa3e22 NS |
4613 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) |
4614 | { | |
4615 | int ending; | |
21526606 | 4616 | |
7efa3e22 NS |
4617 | skip_comma:; |
4618 | /* We try and resync to an unnested comma, as that will give the | |
4619 | user better diagnostics. */ | |
21526606 EC |
4620 | ending = cp_parser_skip_to_closing_parenthesis (parser, |
4621 | /*recovering=*/true, | |
4bb8ca28 | 4622 | /*or_comma=*/true, |
a668c6ad | 4623 | /*consume_paren=*/true); |
7efa3e22 NS |
4624 | if (ending < 0) |
4625 | goto get_comma; | |
4626 | if (!ending) | |
4627 | return error_mark_node; | |
a723baf1 MM |
4628 | } |
4629 | ||
4630 | /* We built up the list in reverse order so we must reverse it now. */ | |
7efa3e22 NS |
4631 | expression_list = nreverse (expression_list); |
4632 | if (identifier) | |
4633 | expression_list = tree_cons (NULL_TREE, identifier, expression_list); | |
21526606 | 4634 | |
7efa3e22 | 4635 | return expression_list; |
a723baf1 MM |
4636 | } |
4637 | ||
4638 | /* Parse a pseudo-destructor-name. | |
4639 | ||
4640 | pseudo-destructor-name: | |
4641 | :: [opt] nested-name-specifier [opt] type-name :: ~ type-name | |
4642 | :: [opt] nested-name-specifier template template-id :: ~ type-name | |
4643 | :: [opt] nested-name-specifier [opt] ~ type-name | |
4644 | ||
4645 | If either of the first two productions is used, sets *SCOPE to the | |
4646 | TYPE specified before the final `::'. Otherwise, *SCOPE is set to | |
4647 | NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name, | |
d6e57462 | 4648 | or ERROR_MARK_NODE if the parse fails. */ |
a723baf1 MM |
4649 | |
4650 | static void | |
21526606 EC |
4651 | cp_parser_pseudo_destructor_name (cp_parser* parser, |
4652 | tree* scope, | |
94edc4ab | 4653 | tree* type) |
a723baf1 MM |
4654 | { |
4655 | bool nested_name_specifier_p; | |
4656 | ||
b14454ba MM |
4657 | /* Assume that things will not work out. */ |
4658 | *type = error_mark_node; | |
4659 | ||
a723baf1 MM |
4660 | /* Look for the optional `::' operator. */ |
4661 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true); | |
4662 | /* Look for the optional nested-name-specifier. */ | |
21526606 | 4663 | nested_name_specifier_p |
a723baf1 MM |
4664 | = (cp_parser_nested_name_specifier_opt (parser, |
4665 | /*typename_keyword_p=*/false, | |
4666 | /*check_dependency_p=*/true, | |
a668c6ad | 4667 | /*type_p=*/false, |
21526606 | 4668 | /*is_declaration=*/true) |
a723baf1 MM |
4669 | != NULL_TREE); |
4670 | /* Now, if we saw a nested-name-specifier, we might be doing the | |
4671 | second production. */ | |
21526606 | 4672 | if (nested_name_specifier_p |
a723baf1 MM |
4673 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) |
4674 | { | |
4675 | /* Consume the `template' keyword. */ | |
4676 | cp_lexer_consume_token (parser->lexer); | |
4677 | /* Parse the template-id. */ | |
21526606 | 4678 | cp_parser_template_id (parser, |
a723baf1 | 4679 | /*template_keyword_p=*/true, |
a668c6ad MM |
4680 | /*check_dependency_p=*/false, |
4681 | /*is_declaration=*/true); | |
a723baf1 MM |
4682 | /* Look for the `::' token. */ |
4683 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
4684 | } | |
4685 | /* If the next token is not a `~', then there might be some | |
9bcb9aae | 4686 | additional qualification. */ |
a723baf1 MM |
4687 | else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL)) |
4688 | { | |
4689 | /* Look for the type-name. */ | |
4690 | *scope = TREE_TYPE (cp_parser_type_name (parser)); | |
d6e57462 | 4691 | |
b14454ba MM |
4692 | if (*scope == error_mark_node) |
4693 | return; | |
4694 | ||
4695 | /* If we don't have ::~, then something has gone wrong. Since | |
4696 | the only caller of this function is looking for something | |
4697 | after `.' or `->' after a scalar type, most likely the | |
4698 | program is trying to get a member of a non-aggregate | |
4699 | type. */ | |
4700 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE) | |
d6e57462 ILT |
4701 | || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL) |
4702 | { | |
4703 | cp_parser_error (parser, "request for member of non-aggregate type"); | |
d6e57462 ILT |
4704 | return; |
4705 | } | |
4706 | ||
a723baf1 MM |
4707 | /* Look for the `::' token. */ |
4708 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
4709 | } | |
4710 | else | |
4711 | *scope = NULL_TREE; | |
4712 | ||
4713 | /* Look for the `~'. */ | |
4714 | cp_parser_require (parser, CPP_COMPL, "`~'"); | |
4715 | /* Look for the type-name again. We are not responsible for | |
4716 | checking that it matches the first type-name. */ | |
4717 | *type = cp_parser_type_name (parser); | |
4718 | } | |
4719 | ||
4720 | /* Parse a unary-expression. | |
4721 | ||
4722 | unary-expression: | |
4723 | postfix-expression | |
4724 | ++ cast-expression | |
4725 | -- cast-expression | |
4726 | unary-operator cast-expression | |
4727 | sizeof unary-expression | |
4728 | sizeof ( type-id ) | |
4729 | new-expression | |
4730 | delete-expression | |
4731 | ||
4732 | GNU Extensions: | |
4733 | ||
4734 | unary-expression: | |
4735 | __extension__ cast-expression | |
4736 | __alignof__ unary-expression | |
4737 | __alignof__ ( type-id ) | |
4738 | __real__ cast-expression | |
4739 | __imag__ cast-expression | |
4740 | && identifier | |
4741 | ||
4742 | ADDRESS_P is true iff the unary-expression is appearing as the | |
93678513 MM |
4743 | operand of the `&' operator. CAST_P is true if this expression is |
4744 | the target of a cast. | |
a723baf1 | 4745 | |
34cd5ae7 | 4746 | Returns a representation of the expression. */ |
a723baf1 MM |
4747 | |
4748 | static tree | |
93678513 | 4749 | cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p) |
a723baf1 MM |
4750 | { |
4751 | cp_token *token; | |
4752 | enum tree_code unary_operator; | |
4753 | ||
4754 | /* Peek at the next token. */ | |
4755 | token = cp_lexer_peek_token (parser->lexer); | |
4756 | /* Some keywords give away the kind of expression. */ | |
4757 | if (token->type == CPP_KEYWORD) | |
4758 | { | |
4759 | enum rid keyword = token->keyword; | |
4760 | ||
4761 | switch (keyword) | |
4762 | { | |
4763 | case RID_ALIGNOF: | |
a723baf1 MM |
4764 | case RID_SIZEOF: |
4765 | { | |
4766 | tree operand; | |
7a18b933 | 4767 | enum tree_code op; |
21526606 | 4768 | |
7a18b933 NS |
4769 | op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR; |
4770 | /* Consume the token. */ | |
a723baf1 MM |
4771 | cp_lexer_consume_token (parser->lexer); |
4772 | /* Parse the operand. */ | |
4773 | operand = cp_parser_sizeof_operand (parser, keyword); | |
4774 | ||
7a18b933 NS |
4775 | if (TYPE_P (operand)) |
4776 | return cxx_sizeof_or_alignof_type (operand, op, true); | |
a723baf1 | 4777 | else |
7a18b933 | 4778 | return cxx_sizeof_or_alignof_expr (operand, op); |
a723baf1 MM |
4779 | } |
4780 | ||
4781 | case RID_NEW: | |
4782 | return cp_parser_new_expression (parser); | |
4783 | ||
4784 | case RID_DELETE: | |
4785 | return cp_parser_delete_expression (parser); | |
21526606 | 4786 | |
a723baf1 MM |
4787 | case RID_EXTENSION: |
4788 | { | |
4789 | /* The saved value of the PEDANTIC flag. */ | |
4790 | int saved_pedantic; | |
4791 | tree expr; | |
4792 | ||
4793 | /* Save away the PEDANTIC flag. */ | |
4794 | cp_parser_extension_opt (parser, &saved_pedantic); | |
4795 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4796 | expr = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4797 | /* Restore the PEDANTIC flag. */ |
4798 | pedantic = saved_pedantic; | |
4799 | ||
4800 | return expr; | |
4801 | } | |
4802 | ||
4803 | case RID_REALPART: | |
4804 | case RID_IMAGPART: | |
4805 | { | |
4806 | tree expression; | |
4807 | ||
4808 | /* Consume the `__real__' or `__imag__' token. */ | |
4809 | cp_lexer_consume_token (parser->lexer); | |
4810 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4811 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4812 | /* Create the complete representation. */ |
4813 | return build_x_unary_op ((keyword == RID_REALPART | |
4814 | ? REALPART_EXPR : IMAGPART_EXPR), | |
4815 | expression); | |
4816 | } | |
4817 | break; | |
4818 | ||
4819 | default: | |
4820 | break; | |
4821 | } | |
4822 | } | |
4823 | ||
4824 | /* Look for the `:: new' and `:: delete', which also signal the | |
4825 | beginning of a new-expression, or delete-expression, | |
4826 | respectively. If the next token is `::', then it might be one of | |
4827 | these. */ | |
4828 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
4829 | { | |
4830 | enum rid keyword; | |
4831 | ||
4832 | /* See if the token after the `::' is one of the keywords in | |
4833 | which we're interested. */ | |
4834 | keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword; | |
4835 | /* If it's `new', we have a new-expression. */ | |
4836 | if (keyword == RID_NEW) | |
4837 | return cp_parser_new_expression (parser); | |
4838 | /* Similarly, for `delete'. */ | |
4839 | else if (keyword == RID_DELETE) | |
4840 | return cp_parser_delete_expression (parser); | |
4841 | } | |
4842 | ||
4843 | /* Look for a unary operator. */ | |
4844 | unary_operator = cp_parser_unary_operator (token); | |
4845 | /* The `++' and `--' operators can be handled similarly, even though | |
4846 | they are not technically unary-operators in the grammar. */ | |
4847 | if (unary_operator == ERROR_MARK) | |
4848 | { | |
4849 | if (token->type == CPP_PLUS_PLUS) | |
4850 | unary_operator = PREINCREMENT_EXPR; | |
4851 | else if (token->type == CPP_MINUS_MINUS) | |
4852 | unary_operator = PREDECREMENT_EXPR; | |
4853 | /* Handle the GNU address-of-label extension. */ | |
4854 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
4855 | && token->type == CPP_AND_AND) | |
4856 | { | |
4857 | tree identifier; | |
4858 | ||
4859 | /* Consume the '&&' token. */ | |
4860 | cp_lexer_consume_token (parser->lexer); | |
4861 | /* Look for the identifier. */ | |
4862 | identifier = cp_parser_identifier (parser); | |
4863 | /* Create an expression representing the address. */ | |
4864 | return finish_label_address_expr (identifier); | |
4865 | } | |
4866 | } | |
4867 | if (unary_operator != ERROR_MARK) | |
4868 | { | |
4869 | tree cast_expression; | |
a5ac3982 MM |
4870 | tree expression = error_mark_node; |
4871 | const char *non_constant_p = NULL; | |
a723baf1 MM |
4872 | |
4873 | /* Consume the operator token. */ | |
4874 | token = cp_lexer_consume_token (parser->lexer); | |
4875 | /* Parse the cast-expression. */ | |
21526606 | 4876 | cast_expression |
93678513 MM |
4877 | = cp_parser_cast_expression (parser, |
4878 | unary_operator == ADDR_EXPR, | |
4879 | /*cast_p=*/false); | |
a723baf1 MM |
4880 | /* Now, build an appropriate representation. */ |
4881 | switch (unary_operator) | |
4882 | { | |
4883 | case INDIRECT_REF: | |
a5ac3982 MM |
4884 | non_constant_p = "`*'"; |
4885 | expression = build_x_indirect_ref (cast_expression, "unary *"); | |
4886 | break; | |
4887 | ||
a723baf1 | 4888 | case ADDR_EXPR: |
7a3ea201 | 4889 | non_constant_p = "`&'"; |
a5ac3982 | 4890 | /* Fall through. */ |
d17811fd | 4891 | case BIT_NOT_EXPR: |
a5ac3982 MM |
4892 | expression = build_x_unary_op (unary_operator, cast_expression); |
4893 | break; | |
4894 | ||
14d22dd6 MM |
4895 | case PREINCREMENT_EXPR: |
4896 | case PREDECREMENT_EXPR: | |
a5ac3982 MM |
4897 | non_constant_p = (unary_operator == PREINCREMENT_EXPR |
4898 | ? "`++'" : "`--'"); | |
14d22dd6 | 4899 | /* Fall through. */ |
392e3d51 | 4900 | case UNARY_PLUS_EXPR: |
a723baf1 MM |
4901 | case NEGATE_EXPR: |
4902 | case TRUTH_NOT_EXPR: | |
a5ac3982 MM |
4903 | expression = finish_unary_op_expr (unary_operator, cast_expression); |
4904 | break; | |
a723baf1 | 4905 | |
a723baf1 | 4906 | default: |
315fb5db | 4907 | gcc_unreachable (); |
a723baf1 | 4908 | } |
a5ac3982 | 4909 | |
98ca843c | 4910 | if (non_constant_p |
625cbf93 MM |
4911 | && cp_parser_non_integral_constant_expression (parser, |
4912 | non_constant_p)) | |
4913 | expression = error_mark_node; | |
a5ac3982 MM |
4914 | |
4915 | return expression; | |
a723baf1 MM |
4916 | } |
4917 | ||
93678513 | 4918 | return cp_parser_postfix_expression (parser, address_p, cast_p); |
a723baf1 MM |
4919 | } |
4920 | ||
4921 | /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a | |
4922 | unary-operator, the corresponding tree code is returned. */ | |
4923 | ||
4924 | static enum tree_code | |
94edc4ab | 4925 | cp_parser_unary_operator (cp_token* token) |
a723baf1 MM |
4926 | { |
4927 | switch (token->type) | |
4928 | { | |
4929 | case CPP_MULT: | |
4930 | return INDIRECT_REF; | |
4931 | ||
4932 | case CPP_AND: | |
4933 | return ADDR_EXPR; | |
4934 | ||
4935 | case CPP_PLUS: | |
392e3d51 | 4936 | return UNARY_PLUS_EXPR; |
a723baf1 MM |
4937 | |
4938 | case CPP_MINUS: | |
4939 | return NEGATE_EXPR; | |
4940 | ||
4941 | case CPP_NOT: | |
4942 | return TRUTH_NOT_EXPR; | |
21526606 | 4943 | |
a723baf1 MM |
4944 | case CPP_COMPL: |
4945 | return BIT_NOT_EXPR; | |
4946 | ||
4947 | default: | |
4948 | return ERROR_MARK; | |
4949 | } | |
4950 | } | |
4951 | ||
4952 | /* Parse a new-expression. | |
4953 | ||
ca099ac8 | 4954 | new-expression: |
a723baf1 MM |
4955 | :: [opt] new new-placement [opt] new-type-id new-initializer [opt] |
4956 | :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt] | |
4957 | ||
4958 | Returns a representation of the expression. */ | |
4959 | ||
4960 | static tree | |
94edc4ab | 4961 | cp_parser_new_expression (cp_parser* parser) |
a723baf1 MM |
4962 | { |
4963 | bool global_scope_p; | |
4964 | tree placement; | |
4965 | tree type; | |
4966 | tree initializer; | |
058b15c1 | 4967 | tree nelts; |
a723baf1 MM |
4968 | |
4969 | /* Look for the optional `::' operator. */ | |
21526606 | 4970 | global_scope_p |
a723baf1 MM |
4971 | = (cp_parser_global_scope_opt (parser, |
4972 | /*current_scope_valid_p=*/false) | |
4973 | != NULL_TREE); | |
4974 | /* Look for the `new' operator. */ | |
4975 | cp_parser_require_keyword (parser, RID_NEW, "`new'"); | |
4976 | /* There's no easy way to tell a new-placement from the | |
4977 | `( type-id )' construct. */ | |
4978 | cp_parser_parse_tentatively (parser); | |
4979 | /* Look for a new-placement. */ | |
4980 | placement = cp_parser_new_placement (parser); | |
4981 | /* If that didn't work out, there's no new-placement. */ | |
4982 | if (!cp_parser_parse_definitely (parser)) | |
4983 | placement = NULL_TREE; | |
4984 | ||
4985 | /* If the next token is a `(', then we have a parenthesized | |
4986 | type-id. */ | |
4987 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4988 | { | |
4989 | /* Consume the `('. */ | |
4990 | cp_lexer_consume_token (parser->lexer); | |
4991 | /* Parse the type-id. */ | |
4992 | type = cp_parser_type_id (parser); | |
4993 | /* Look for the closing `)'. */ | |
4994 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
98ca843c | 4995 | /* There should not be a direct-new-declarator in this production, |
063e900f GB |
4996 | but GCC used to allowed this, so we check and emit a sensible error |
4997 | message for this case. */ | |
4998 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
0da99d4e GB |
4999 | { |
5000 | error ("array bound forbidden after parenthesized type-id"); | |
5001 | inform ("try removing the parentheses around the type-id"); | |
063e900f GB |
5002 | cp_parser_direct_new_declarator (parser); |
5003 | } | |
17a27b4f | 5004 | nelts = NULL_TREE; |
a723baf1 MM |
5005 | } |
5006 | /* Otherwise, there must be a new-type-id. */ | |
5007 | else | |
058b15c1 | 5008 | type = cp_parser_new_type_id (parser, &nelts); |
a723baf1 MM |
5009 | |
5010 | /* If the next token is a `(', then we have a new-initializer. */ | |
5011 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
5012 | initializer = cp_parser_new_initializer (parser); | |
5013 | else | |
5014 | initializer = NULL_TREE; | |
5015 | ||
625cbf93 MM |
5016 | /* A new-expression may not appear in an integral constant |
5017 | expression. */ | |
5018 | if (cp_parser_non_integral_constant_expression (parser, "`new'")) | |
5019 | return error_mark_node; | |
5020 | ||
a723baf1 | 5021 | /* Create a representation of the new-expression. */ |
058b15c1 | 5022 | return build_new (placement, type, nelts, initializer, global_scope_p); |
a723baf1 MM |
5023 | } |
5024 | ||
5025 | /* Parse a new-placement. | |
5026 | ||
5027 | new-placement: | |
5028 | ( expression-list ) | |
5029 | ||
5030 | Returns the same representation as for an expression-list. */ | |
5031 | ||
5032 | static tree | |
94edc4ab | 5033 | cp_parser_new_placement (cp_parser* parser) |
a723baf1 MM |
5034 | { |
5035 | tree expression_list; | |
5036 | ||
a723baf1 | 5037 | /* Parse the expression-list. */ |
21526606 | 5038 | expression_list = (cp_parser_parenthesized_expression_list |
93678513 MM |
5039 | (parser, false, /*cast_p=*/false, |
5040 | /*non_constant_p=*/NULL)); | |
a723baf1 MM |
5041 | |
5042 | return expression_list; | |
5043 | } | |
5044 | ||
5045 | /* Parse a new-type-id. | |
5046 | ||
5047 | new-type-id: | |
5048 | type-specifier-seq new-declarator [opt] | |
5049 | ||
058b15c1 MM |
5050 | Returns the TYPE allocated. If the new-type-id indicates an array |
5051 | type, *NELTS is set to the number of elements in the last array | |
5052 | bound; the TYPE will not include the last array bound. */ | |
a723baf1 MM |
5053 | |
5054 | static tree | |
058b15c1 | 5055 | cp_parser_new_type_id (cp_parser* parser, tree *nelts) |
a723baf1 | 5056 | { |
62d1db17 | 5057 | cp_decl_specifier_seq type_specifier_seq; |
058b15c1 MM |
5058 | cp_declarator *new_declarator; |
5059 | cp_declarator *declarator; | |
5060 | cp_declarator *outer_declarator; | |
a723baf1 | 5061 | const char *saved_message; |
058b15c1 | 5062 | tree type; |
a723baf1 MM |
5063 | |
5064 | /* The type-specifier sequence must not contain type definitions. | |
5065 | (It cannot contain declarations of new types either, but if they | |
5066 | are not definitions we will catch that because they are not | |
5067 | complete.) */ | |
5068 | saved_message = parser->type_definition_forbidden_message; | |
5069 | parser->type_definition_forbidden_message | |
5070 | = "types may not be defined in a new-type-id"; | |
5071 | /* Parse the type-specifier-seq. */ | |
d4113656 MM |
5072 | cp_parser_type_specifier_seq (parser, /*is_condition=*/false, |
5073 | &type_specifier_seq); | |
a723baf1 MM |
5074 | /* Restore the old message. */ |
5075 | parser->type_definition_forbidden_message = saved_message; | |
5076 | /* Parse the new-declarator. */ | |
058b15c1 MM |
5077 | new_declarator = cp_parser_new_declarator_opt (parser); |
5078 | ||
5079 | /* Determine the number of elements in the last array dimension, if | |
5080 | any. */ | |
5081 | *nelts = NULL_TREE; | |
5082 | /* Skip down to the last array dimension. */ | |
5083 | declarator = new_declarator; | |
5084 | outer_declarator = NULL; | |
5085 | while (declarator && (declarator->kind == cdk_pointer | |
5086 | || declarator->kind == cdk_ptrmem)) | |
5087 | { | |
5088 | outer_declarator = declarator; | |
5089 | declarator = declarator->declarator; | |
5090 | } | |
98ca843c | 5091 | while (declarator |
058b15c1 MM |
5092 | && declarator->kind == cdk_array |
5093 | && declarator->declarator | |
5094 | && declarator->declarator->kind == cdk_array) | |
5095 | { | |
5096 | outer_declarator = declarator; | |
5097 | declarator = declarator->declarator; | |
5098 | } | |
98ca843c | 5099 | |
058b15c1 MM |
5100 | if (declarator && declarator->kind == cdk_array) |
5101 | { | |
5102 | *nelts = declarator->u.array.bounds; | |
5103 | if (*nelts == error_mark_node) | |
5104 | *nelts = integer_one_node; | |
ad1063d5 | 5105 | |
058b15c1 MM |
5106 | if (outer_declarator) |
5107 | outer_declarator->declarator = declarator->declarator; | |
5108 | else | |
5109 | new_declarator = NULL; | |
5110 | } | |
a723baf1 | 5111 | |
62d1db17 | 5112 | type = groktypename (&type_specifier_seq, new_declarator); |
058b15c1 MM |
5113 | if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE) |
5114 | { | |
5115 | *nelts = array_type_nelts_top (type); | |
5116 | type = TREE_TYPE (type); | |
5117 | } | |
5118 | return type; | |
a723baf1 MM |
5119 | } |
5120 | ||
5121 | /* Parse an (optional) new-declarator. | |
5122 | ||
5123 | new-declarator: | |
5124 | ptr-operator new-declarator [opt] | |
5125 | direct-new-declarator | |
5126 | ||
058b15c1 | 5127 | Returns the declarator. */ |
a723baf1 | 5128 | |
058b15c1 | 5129 | static cp_declarator * |
94edc4ab | 5130 | cp_parser_new_declarator_opt (cp_parser* parser) |
a723baf1 MM |
5131 | { |
5132 | enum tree_code code; | |
5133 | tree type; | |
3c01e5df | 5134 | cp_cv_quals cv_quals; |
a723baf1 MM |
5135 | |
5136 | /* We don't know if there's a ptr-operator next, or not. */ | |
5137 | cp_parser_parse_tentatively (parser); | |
5138 | /* Look for a ptr-operator. */ | |
3c01e5df | 5139 | code = cp_parser_ptr_operator (parser, &type, &cv_quals); |
a723baf1 MM |
5140 | /* If that worked, look for more new-declarators. */ |
5141 | if (cp_parser_parse_definitely (parser)) | |
5142 | { | |
058b15c1 | 5143 | cp_declarator *declarator; |
a723baf1 MM |
5144 | |
5145 | /* Parse another optional declarator. */ | |
5146 | declarator = cp_parser_new_declarator_opt (parser); | |
5147 | ||
5148 | /* Create the representation of the declarator. */ | |
058b15c1 | 5149 | if (type) |
3c01e5df | 5150 | declarator = make_ptrmem_declarator (cv_quals, type, declarator); |
058b15c1 | 5151 | else if (code == INDIRECT_REF) |
3c01e5df | 5152 | declarator = make_pointer_declarator (cv_quals, declarator); |
a723baf1 | 5153 | else |
3c01e5df | 5154 | declarator = make_reference_declarator (cv_quals, declarator); |
a723baf1 | 5155 | |
a723baf1 MM |
5156 | return declarator; |
5157 | } | |
5158 | ||
5159 | /* If the next token is a `[', there is a direct-new-declarator. */ | |
5160 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
5161 | return cp_parser_direct_new_declarator (parser); | |
5162 | ||
058b15c1 | 5163 | return NULL; |
a723baf1 MM |
5164 | } |
5165 | ||
5166 | /* Parse a direct-new-declarator. | |
5167 | ||
5168 | direct-new-declarator: | |
5169 | [ expression ] | |
21526606 | 5170 | direct-new-declarator [constant-expression] |
a723baf1 | 5171 | |
058b15c1 | 5172 | */ |
a723baf1 | 5173 | |
058b15c1 | 5174 | static cp_declarator * |
94edc4ab | 5175 | cp_parser_direct_new_declarator (cp_parser* parser) |
a723baf1 | 5176 | { |
058b15c1 | 5177 | cp_declarator *declarator = NULL; |
a723baf1 MM |
5178 | |
5179 | while (true) | |
5180 | { | |
5181 | tree expression; | |
5182 | ||
5183 | /* Look for the opening `['. */ | |
5184 | cp_parser_require (parser, CPP_OPEN_SQUARE, "`['"); | |
5185 | /* The first expression is not required to be constant. */ | |
5186 | if (!declarator) | |
5187 | { | |
93678513 | 5188 | expression = cp_parser_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
5189 | /* The standard requires that the expression have integral |
5190 | type. DR 74 adds enumeration types. We believe that the | |
5191 | real intent is that these expressions be handled like the | |
5192 | expression in a `switch' condition, which also allows | |
5193 | classes with a single conversion to integral or | |
5194 | enumeration type. */ | |
5195 | if (!processing_template_decl) | |
5196 | { | |
21526606 | 5197 | expression |
a723baf1 MM |
5198 | = build_expr_type_conversion (WANT_INT | WANT_ENUM, |
5199 | expression, | |
b746c5dc | 5200 | /*complain=*/true); |
a723baf1 MM |
5201 | if (!expression) |
5202 | { | |
2a13a625 GDR |
5203 | error ("expression in new-declarator must have integral " |
5204 | "or enumeration type"); | |
a723baf1 MM |
5205 | expression = error_mark_node; |
5206 | } | |
5207 | } | |
5208 | } | |
5209 | /* But all the other expressions must be. */ | |
5210 | else | |
21526606 EC |
5211 | expression |
5212 | = cp_parser_constant_expression (parser, | |
14d22dd6 MM |
5213 | /*allow_non_constant=*/false, |
5214 | NULL); | |
a723baf1 MM |
5215 | /* Look for the closing `]'. */ |
5216 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
5217 | ||
5218 | /* Add this bound to the declarator. */ | |
058b15c1 | 5219 | declarator = make_array_declarator (declarator, expression); |
a723baf1 MM |
5220 | |
5221 | /* If the next token is not a `[', then there are no more | |
5222 | bounds. */ | |
5223 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) | |
5224 | break; | |
5225 | } | |
5226 | ||
5227 | return declarator; | |
5228 | } | |
5229 | ||
5230 | /* Parse a new-initializer. | |
5231 | ||
5232 | new-initializer: | |
5233 | ( expression-list [opt] ) | |
5234 | ||
34cd5ae7 | 5235 | Returns a representation of the expression-list. If there is no |
a723baf1 MM |
5236 | expression-list, VOID_ZERO_NODE is returned. */ |
5237 | ||
5238 | static tree | |
94edc4ab | 5239 | cp_parser_new_initializer (cp_parser* parser) |
a723baf1 MM |
5240 | { |
5241 | tree expression_list; | |
5242 | ||
21526606 | 5243 | expression_list = (cp_parser_parenthesized_expression_list |
93678513 MM |
5244 | (parser, false, /*cast_p=*/false, |
5245 | /*non_constant_p=*/NULL)); | |
7efa3e22 | 5246 | if (!expression_list) |
a723baf1 | 5247 | expression_list = void_zero_node; |
a723baf1 MM |
5248 | |
5249 | return expression_list; | |
5250 | } | |
5251 | ||
5252 | /* Parse a delete-expression. | |
5253 | ||
5254 | delete-expression: | |
5255 | :: [opt] delete cast-expression | |
5256 | :: [opt] delete [ ] cast-expression | |
5257 | ||
5258 | Returns a representation of the expression. */ | |
5259 | ||
5260 | static tree | |
94edc4ab | 5261 | cp_parser_delete_expression (cp_parser* parser) |
a723baf1 MM |
5262 | { |
5263 | bool global_scope_p; | |
5264 | bool array_p; | |
5265 | tree expression; | |
5266 | ||
5267 | /* Look for the optional `::' operator. */ | |
5268 | global_scope_p | |
5269 | = (cp_parser_global_scope_opt (parser, | |
5270 | /*current_scope_valid_p=*/false) | |
5271 | != NULL_TREE); | |
5272 | /* Look for the `delete' keyword. */ | |
5273 | cp_parser_require_keyword (parser, RID_DELETE, "`delete'"); | |
5274 | /* See if the array syntax is in use. */ | |
5275 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
5276 | { | |
5277 | /* Consume the `[' token. */ | |
5278 | cp_lexer_consume_token (parser->lexer); | |
5279 | /* Look for the `]' token. */ | |
5280 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
5281 | /* Remember that this is the `[]' construct. */ | |
5282 | array_p = true; | |
5283 | } | |
5284 | else | |
5285 | array_p = false; | |
5286 | ||
5287 | /* Parse the cast-expression. */ | |
d6b4ea85 | 5288 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 | 5289 | |
625cbf93 MM |
5290 | /* A delete-expression may not appear in an integral constant |
5291 | expression. */ | |
5292 | if (cp_parser_non_integral_constant_expression (parser, "`delete'")) | |
5293 | return error_mark_node; | |
5294 | ||
a723baf1 MM |
5295 | return delete_sanity (expression, NULL_TREE, array_p, global_scope_p); |
5296 | } | |
5297 | ||
5298 | /* Parse a cast-expression. | |
5299 | ||
5300 | cast-expression: | |
5301 | unary-expression | |
5302 | ( type-id ) cast-expression | |
5303 | ||
93678513 MM |
5304 | ADDRESS_P is true iff the unary-expression is appearing as the |
5305 | operand of the `&' operator. CAST_P is true if this expression is | |
5306 | the target of a cast. | |
5307 | ||
a723baf1 MM |
5308 | Returns a representation of the expression. */ |
5309 | ||
5310 | static tree | |
93678513 | 5311 | cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p) |
a723baf1 MM |
5312 | { |
5313 | /* If it's a `(', then we might be looking at a cast. */ | |
5314 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
5315 | { | |
5316 | tree type = NULL_TREE; | |
5317 | tree expr = NULL_TREE; | |
5318 | bool compound_literal_p; | |
5319 | const char *saved_message; | |
5320 | ||
5321 | /* There's no way to know yet whether or not this is a cast. | |
5322 | For example, `(int (3))' is a unary-expression, while `(int) | |
5323 | 3' is a cast. So, we resort to parsing tentatively. */ | |
5324 | cp_parser_parse_tentatively (parser); | |
5325 | /* Types may not be defined in a cast. */ | |
5326 | saved_message = parser->type_definition_forbidden_message; | |
5327 | parser->type_definition_forbidden_message | |
5328 | = "types may not be defined in casts"; | |
5329 | /* Consume the `('. */ | |
5330 | cp_lexer_consume_token (parser->lexer); | |
5331 | /* A very tricky bit is that `(struct S) { 3 }' is a | |
5332 | compound-literal (which we permit in C++ as an extension). | |
5333 | But, that construct is not a cast-expression -- it is a | |
5334 | postfix-expression. (The reason is that `(struct S) { 3 }.i' | |
5335 | is legal; if the compound-literal were a cast-expression, | |
5336 | you'd need an extra set of parentheses.) But, if we parse | |
5337 | the type-id, and it happens to be a class-specifier, then we | |
5338 | will commit to the parse at that point, because we cannot | |
5339 | undo the action that is done when creating a new class. So, | |
21526606 | 5340 | then we cannot back up and do a postfix-expression. |
a723baf1 MM |
5341 | |
5342 | Therefore, we scan ahead to the closing `)', and check to see | |
5343 | if the token after the `)' is a `{'. If so, we are not | |
21526606 | 5344 | looking at a cast-expression. |
a723baf1 MM |
5345 | |
5346 | Save tokens so that we can put them back. */ | |
5347 | cp_lexer_save_tokens (parser->lexer); | |
5348 | /* Skip tokens until the next token is a closing parenthesis. | |
5349 | If we find the closing `)', and the next token is a `{', then | |
5350 | we are looking at a compound-literal. */ | |
21526606 | 5351 | compound_literal_p |
a668c6ad MM |
5352 | = (cp_parser_skip_to_closing_parenthesis (parser, false, false, |
5353 | /*consume_paren=*/true) | |
a723baf1 MM |
5354 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)); |
5355 | /* Roll back the tokens we skipped. */ | |
5356 | cp_lexer_rollback_tokens (parser->lexer); | |
5357 | /* If we were looking at a compound-literal, simulate an error | |
5358 | so that the call to cp_parser_parse_definitely below will | |
5359 | fail. */ | |
5360 | if (compound_literal_p) | |
5361 | cp_parser_simulate_error (parser); | |
5362 | else | |
5363 | { | |
4f8163b1 MM |
5364 | bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
5365 | parser->in_type_id_in_expr_p = true; | |
a723baf1 MM |
5366 | /* Look for the type-id. */ |
5367 | type = cp_parser_type_id (parser); | |
5368 | /* Look for the closing `)'. */ | |
5369 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
4f8163b1 | 5370 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 MM |
5371 | } |
5372 | ||
5373 | /* Restore the saved message. */ | |
5374 | parser->type_definition_forbidden_message = saved_message; | |
5375 | ||
bbaab916 NS |
5376 | /* If ok so far, parse the dependent expression. We cannot be |
5377 | sure it is a cast. Consider `(T ())'. It is a parenthesized | |
5378 | ctor of T, but looks like a cast to function returning T | |
5379 | without a dependent expression. */ | |
5380 | if (!cp_parser_error_occurred (parser)) | |
93678513 MM |
5381 | expr = cp_parser_cast_expression (parser, |
5382 | /*address_p=*/false, | |
5383 | /*cast_p=*/true); | |
bbaab916 | 5384 | |
a723baf1 MM |
5385 | if (cp_parser_parse_definitely (parser)) |
5386 | { | |
a723baf1 | 5387 | /* Warn about old-style casts, if so requested. */ |
21526606 EC |
5388 | if (warn_old_style_cast |
5389 | && !in_system_header | |
5390 | && !VOID_TYPE_P (type) | |
a723baf1 | 5391 | && current_lang_name != lang_name_c) |
d4ee4d25 | 5392 | warning (0, "use of old-style cast"); |
14d22dd6 MM |
5393 | |
5394 | /* Only type conversions to integral or enumeration types | |
5395 | can be used in constant-expressions. */ | |
67c03833 | 5396 | if (parser->integral_constant_expression_p |
14d22dd6 | 5397 | && !dependent_type_p (type) |
625cbf93 | 5398 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
98ca843c | 5399 | && (cp_parser_non_integral_constant_expression |
625cbf93 MM |
5400 | (parser, |
5401 | "a cast to a type other than an integral or " | |
5402 | "enumeration type"))) | |
5403 | return error_mark_node; | |
5404 | ||
a723baf1 MM |
5405 | /* Perform the cast. */ |
5406 | expr = build_c_cast (type, expr); | |
bbaab916 | 5407 | return expr; |
a723baf1 | 5408 | } |
a723baf1 MM |
5409 | } |
5410 | ||
5411 | /* If we get here, then it's not a cast, so it must be a | |
5412 | unary-expression. */ | |
93678513 | 5413 | return cp_parser_unary_expression (parser, address_p, cast_p); |
a723baf1 MM |
5414 | } |
5415 | ||
b8b94c5b | 5416 | /* Parse a binary expression of the general form: |
a723baf1 MM |
5417 | |
5418 | pm-expression: | |
5419 | cast-expression | |
5420 | pm-expression .* cast-expression | |
5421 | pm-expression ->* cast-expression | |
5422 | ||
77077b39 | 5423 | multiplicative-expression: |
a723baf1 MM |
5424 | pm-expression |
5425 | multiplicative-expression * pm-expression | |
5426 | multiplicative-expression / pm-expression | |
5427 | multiplicative-expression % pm-expression | |
5428 | ||
a723baf1 MM |
5429 | additive-expression: |
5430 | multiplicative-expression | |
5431 | additive-expression + multiplicative-expression | |
5432 | additive-expression - multiplicative-expression | |
5433 | ||
a723baf1 MM |
5434 | shift-expression: |
5435 | additive-expression | |
5436 | shift-expression << additive-expression | |
5437 | shift-expression >> additive-expression | |
5438 | ||
a723baf1 MM |
5439 | relational-expression: |
5440 | shift-expression | |
5441 | relational-expression < shift-expression | |
5442 | relational-expression > shift-expression | |
5443 | relational-expression <= shift-expression | |
5444 | relational-expression >= shift-expression | |
5445 | ||
b8b94c5b PB |
5446 | GNU Extension: |
5447 | ||
a723baf1 MM |
5448 | relational-expression: |
5449 | relational-expression <? shift-expression | |
5450 | relational-expression >? shift-expression | |
5451 | ||
a723baf1 MM |
5452 | equality-expression: |
5453 | relational-expression | |
5454 | equality-expression == relational-expression | |
5455 | equality-expression != relational-expression | |
5456 | ||
a723baf1 MM |
5457 | and-expression: |
5458 | equality-expression | |
5459 | and-expression & equality-expression | |
5460 | ||
a723baf1 MM |
5461 | exclusive-or-expression: |
5462 | and-expression | |
5463 | exclusive-or-expression ^ and-expression | |
5464 | ||
b8b94c5b PB |
5465 | inclusive-or-expression: |
5466 | exclusive-or-expression | |
5467 | inclusive-or-expression | exclusive-or-expression | |
a723baf1 | 5468 | |
b8b94c5b PB |
5469 | logical-and-expression: |
5470 | inclusive-or-expression | |
5471 | logical-and-expression && inclusive-or-expression | |
a723baf1 | 5472 | |
b8b94c5b PB |
5473 | logical-or-expression: |
5474 | logical-and-expression | |
5475 | logical-or-expression || logical-and-expression | |
a723baf1 | 5476 | |
b8b94c5b | 5477 | All these are implemented with a single function like: |
a723baf1 | 5478 | |
b8b94c5b PB |
5479 | binary-expression: |
5480 | simple-cast-expression | |
5481 | binary-expression <token> binary-expression | |
a723baf1 | 5482 | |
93678513 MM |
5483 | CAST_P is true if this expression is the target of a cast. |
5484 | ||
b8b94c5b PB |
5485 | The binops_by_token map is used to get the tree codes for each <token> type. |
5486 | binary-expressions are associated according to a precedence table. */ | |
a723baf1 | 5487 | |
b8b94c5b PB |
5488 | #define TOKEN_PRECEDENCE(token) \ |
5489 | ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \ | |
5490 | ? PREC_NOT_OPERATOR \ | |
5491 | : binops_by_token[token->type].prec) | |
a723baf1 MM |
5492 | |
5493 | static tree | |
93678513 | 5494 | cp_parser_binary_expression (cp_parser* parser, bool cast_p) |
a723baf1 | 5495 | { |
b8b94c5b PB |
5496 | cp_parser_expression_stack stack; |
5497 | cp_parser_expression_stack_entry *sp = &stack[0]; | |
5498 | tree lhs, rhs; | |
5499 | cp_token *token; | |
5500 | enum tree_code tree_type; | |
5501 | enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec; | |
5502 | bool overloaded_p; | |
a723baf1 | 5503 | |
b8b94c5b | 5504 | /* Parse the first expression. */ |
93678513 | 5505 | lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p); |
a723baf1 | 5506 | |
b8b94c5b PB |
5507 | for (;;) |
5508 | { | |
5509 | /* Get an operator token. */ | |
5510 | token = cp_lexer_peek_token (parser->lexer); | |
8ff24a79 MM |
5511 | if (token->type == CPP_MIN || token->type == CPP_MAX) |
5512 | cp_parser_warn_min_max (); | |
5513 | ||
b8b94c5b PB |
5514 | new_prec = TOKEN_PRECEDENCE (token); |
5515 | ||
5516 | /* Popping an entry off the stack means we completed a subexpression: | |
5517 | - either we found a token which is not an operator (`>' where it is not | |
5518 | an operator, or prec == PREC_NOT_OPERATOR), in which case popping | |
5519 | will happen repeatedly; | |
5520 | - or, we found an operator which has lower priority. This is the case | |
5521 | where the recursive descent *ascends*, as in `3 * 4 + 5' after | |
03fd3f84 | 5522 | parsing `3 * 4'. */ |
b8b94c5b PB |
5523 | if (new_prec <= prec) |
5524 | { | |
5525 | if (sp == stack) | |
5526 | break; | |
5527 | else | |
5528 | goto pop; | |
5529 | } | |
a723baf1 | 5530 | |
b8b94c5b PB |
5531 | get_rhs: |
5532 | tree_type = binops_by_token[token->type].tree_type; | |
a723baf1 | 5533 | |
03fd3f84 | 5534 | /* We used the operator token. */ |
b8b94c5b | 5535 | cp_lexer_consume_token (parser->lexer); |
a723baf1 | 5536 | |
b8b94c5b PB |
5537 | /* Extract another operand. It may be the RHS of this expression |
5538 | or the LHS of a new, higher priority expression. */ | |
5539 | rhs = cp_parser_simple_cast_expression (parser); | |
a723baf1 | 5540 | |
b8b94c5b PB |
5541 | /* Get another operator token. Look up its precedence to avoid |
5542 | building a useless (immediately popped) stack entry for common | |
03fd3f84 | 5543 | cases such as 3 + 4 + 5 or 3 * 4 + 5. */ |
b8b94c5b PB |
5544 | token = cp_lexer_peek_token (parser->lexer); |
5545 | lookahead_prec = TOKEN_PRECEDENCE (token); | |
5546 | if (lookahead_prec > new_prec) | |
5547 | { | |
5548 | /* ... and prepare to parse the RHS of the new, higher priority | |
43c2a69a PB |
5549 | expression. Since precedence levels on the stack are |
5550 | monotonically increasing, we do not have to care about | |
5551 | stack overflows. */ | |
b8b94c5b PB |
5552 | sp->prec = prec; |
5553 | sp->tree_type = tree_type; | |
5554 | sp->lhs = lhs; | |
5555 | sp++; | |
5556 | lhs = rhs; | |
5557 | prec = new_prec; | |
5558 | new_prec = lookahead_prec; | |
5559 | goto get_rhs; | |
5560 | ||
5561 | pop: | |
5562 | /* If the stack is not empty, we have parsed into LHS the right side | |
5563 | (`4' in the example above) of an expression we had suspended. | |
5564 | We can use the information on the stack to recover the LHS (`3') | |
5565 | from the stack together with the tree code (`MULT_EXPR'), and | |
5566 | the precedence of the higher level subexpression | |
5567 | (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token, | |
5568 | which will be used to actually build the additive expression. */ | |
5569 | --sp; | |
5570 | prec = sp->prec; | |
5571 | tree_type = sp->tree_type; | |
5572 | rhs = lhs; | |
5573 | lhs = sp->lhs; | |
5574 | } | |
a723baf1 | 5575 | |
b8b94c5b PB |
5576 | overloaded_p = false; |
5577 | lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p); | |
a723baf1 | 5578 | |
b8b94c5b PB |
5579 | /* If the binary operator required the use of an overloaded operator, |
5580 | then this expression cannot be an integral constant-expression. | |
5581 | An overloaded operator can be used even if both operands are | |
5582 | otherwise permissible in an integral constant-expression if at | |
5583 | least one of the operands is of enumeration type. */ | |
a723baf1 | 5584 | |
b8b94c5b PB |
5585 | if (overloaded_p |
5586 | && (cp_parser_non_integral_constant_expression | |
5587 | (parser, "calls to overloaded operators"))) | |
5588 | return error_mark_node; | |
5589 | } | |
a723baf1 | 5590 | |
b8b94c5b | 5591 | return lhs; |
a723baf1 MM |
5592 | } |
5593 | ||
b8b94c5b | 5594 | |
a723baf1 MM |
5595 | /* Parse the `? expression : assignment-expression' part of a |
5596 | conditional-expression. The LOGICAL_OR_EXPR is the | |
5597 | logical-or-expression that started the conditional-expression. | |
5598 | Returns a representation of the entire conditional-expression. | |
5599 | ||
39703eb9 | 5600 | This routine is used by cp_parser_assignment_expression. |
a723baf1 MM |
5601 | |
5602 | ? expression : assignment-expression | |
21526606 | 5603 | |
a723baf1 | 5604 | GNU Extensions: |
21526606 | 5605 | |
a723baf1 MM |
5606 | ? : assignment-expression */ |
5607 | ||
5608 | static tree | |
94edc4ab | 5609 | cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr) |
a723baf1 MM |
5610 | { |
5611 | tree expr; | |
5612 | tree assignment_expr; | |
5613 | ||
5614 | /* Consume the `?' token. */ | |
5615 | cp_lexer_consume_token (parser->lexer); | |
5616 | if (cp_parser_allow_gnu_extensions_p (parser) | |
5617 | && cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
5618 | /* Implicit true clause. */ | |
5619 | expr = NULL_TREE; | |
5620 | else | |
5621 | /* Parse the expression. */ | |
93678513 | 5622 | expr = cp_parser_expression (parser, /*cast_p=*/false); |
21526606 | 5623 | |
a723baf1 MM |
5624 | /* The next token should be a `:'. */ |
5625 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
5626 | /* Parse the assignment-expression. */ | |
93678513 | 5627 | assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
5628 | |
5629 | /* Build the conditional-expression. */ | |
5630 | return build_x_conditional_expr (logical_or_expr, | |
5631 | expr, | |
5632 | assignment_expr); | |
5633 | } | |
5634 | ||
5635 | /* Parse an assignment-expression. | |
5636 | ||
5637 | assignment-expression: | |
5638 | conditional-expression | |
5639 | logical-or-expression assignment-operator assignment_expression | |
5640 | throw-expression | |
5641 | ||
93678513 MM |
5642 | CAST_P is true if this expression is the target of a cast. |
5643 | ||
a723baf1 MM |
5644 | Returns a representation for the expression. */ |
5645 | ||
5646 | static tree | |
93678513 | 5647 | cp_parser_assignment_expression (cp_parser* parser, bool cast_p) |
a723baf1 MM |
5648 | { |
5649 | tree expr; | |
5650 | ||
5651 | /* If the next token is the `throw' keyword, then we're looking at | |
5652 | a throw-expression. */ | |
5653 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW)) | |
5654 | expr = cp_parser_throw_expression (parser); | |
5655 | /* Otherwise, it must be that we are looking at a | |
5656 | logical-or-expression. */ | |
5657 | else | |
5658 | { | |
b8b94c5b | 5659 | /* Parse the binary expressions (logical-or-expression). */ |
93678513 | 5660 | expr = cp_parser_binary_expression (parser, cast_p); |
a723baf1 MM |
5661 | /* If the next token is a `?' then we're actually looking at a |
5662 | conditional-expression. */ | |
5663 | if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY)) | |
5664 | return cp_parser_question_colon_clause (parser, expr); | |
21526606 | 5665 | else |
a723baf1 MM |
5666 | { |
5667 | enum tree_code assignment_operator; | |
5668 | ||
5669 | /* If it's an assignment-operator, we're using the second | |
5670 | production. */ | |
21526606 | 5671 | assignment_operator |
a723baf1 MM |
5672 | = cp_parser_assignment_operator_opt (parser); |
5673 | if (assignment_operator != ERROR_MARK) | |
5674 | { | |
5675 | tree rhs; | |
5676 | ||
5677 | /* Parse the right-hand side of the assignment. */ | |
93678513 | 5678 | rhs = cp_parser_assignment_expression (parser, cast_p); |
14d22dd6 MM |
5679 | /* An assignment may not appear in a |
5680 | constant-expression. */ | |
625cbf93 MM |
5681 | if (cp_parser_non_integral_constant_expression (parser, |
5682 | "an assignment")) | |
5683 | return error_mark_node; | |
34cd5ae7 | 5684 | /* Build the assignment expression. */ |
21526606 EC |
5685 | expr = build_x_modify_expr (expr, |
5686 | assignment_operator, | |
a723baf1 MM |
5687 | rhs); |
5688 | } | |
5689 | } | |
5690 | } | |
5691 | ||
5692 | return expr; | |
5693 | } | |
5694 | ||
5695 | /* Parse an (optional) assignment-operator. | |
5696 | ||
21526606 EC |
5697 | assignment-operator: one of |
5698 | = *= /= %= += -= >>= <<= &= ^= |= | |
a723baf1 MM |
5699 | |
5700 | GNU Extension: | |
21526606 | 5701 | |
a723baf1 MM |
5702 | assignment-operator: one of |
5703 | <?= >?= | |
5704 | ||
5705 | If the next token is an assignment operator, the corresponding tree | |
5706 | code is returned, and the token is consumed. For example, for | |
5707 | `+=', PLUS_EXPR is returned. For `=' itself, the code returned is | |
5708 | NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%', | |
5709 | TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment | |
5710 | operator, ERROR_MARK is returned. */ | |
5711 | ||
5712 | static enum tree_code | |
94edc4ab | 5713 | cp_parser_assignment_operator_opt (cp_parser* parser) |
a723baf1 MM |
5714 | { |
5715 | enum tree_code op; | |
5716 | cp_token *token; | |
5717 | ||
5718 | /* Peek at the next toen. */ | |
5719 | token = cp_lexer_peek_token (parser->lexer); | |
5720 | ||
5721 | switch (token->type) | |
5722 | { | |
5723 | case CPP_EQ: | |
5724 | op = NOP_EXPR; | |
5725 | break; | |
5726 | ||
5727 | case CPP_MULT_EQ: | |
5728 | op = MULT_EXPR; | |
5729 | break; | |
5730 | ||
5731 | case CPP_DIV_EQ: | |
5732 | op = TRUNC_DIV_EXPR; | |
5733 | break; | |
5734 | ||
5735 | case CPP_MOD_EQ: | |
5736 | op = TRUNC_MOD_EXPR; | |
5737 | break; | |
5738 | ||
5739 | case CPP_PLUS_EQ: | |
5740 | op = PLUS_EXPR; | |
5741 | break; | |
5742 | ||
5743 | case CPP_MINUS_EQ: | |
5744 | op = MINUS_EXPR; | |
5745 | break; | |
5746 | ||
5747 | case CPP_RSHIFT_EQ: | |
5748 | op = RSHIFT_EXPR; | |
5749 | break; | |
5750 | ||
5751 | case CPP_LSHIFT_EQ: | |
5752 | op = LSHIFT_EXPR; | |
5753 | break; | |
5754 | ||
5755 | case CPP_AND_EQ: | |
5756 | op = BIT_AND_EXPR; | |
5757 | break; | |
5758 | ||
5759 | case CPP_XOR_EQ: | |
5760 | op = BIT_XOR_EXPR; | |
5761 | break; | |
5762 | ||
5763 | case CPP_OR_EQ: | |
5764 | op = BIT_IOR_EXPR; | |
5765 | break; | |
5766 | ||
5767 | case CPP_MIN_EQ: | |
5768 | op = MIN_EXPR; | |
8ff24a79 | 5769 | cp_parser_warn_min_max (); |
a723baf1 MM |
5770 | break; |
5771 | ||
5772 | case CPP_MAX_EQ: | |
5773 | op = MAX_EXPR; | |
8ff24a79 | 5774 | cp_parser_warn_min_max (); |
a723baf1 MM |
5775 | break; |
5776 | ||
21526606 | 5777 | default: |
a723baf1 MM |
5778 | /* Nothing else is an assignment operator. */ |
5779 | op = ERROR_MARK; | |
5780 | } | |
5781 | ||
5782 | /* If it was an assignment operator, consume it. */ | |
5783 | if (op != ERROR_MARK) | |
5784 | cp_lexer_consume_token (parser->lexer); | |
5785 | ||
5786 | return op; | |
5787 | } | |
5788 | ||
5789 | /* Parse an expression. | |
5790 | ||
5791 | expression: | |
5792 | assignment-expression | |
5793 | expression , assignment-expression | |
5794 | ||
93678513 MM |
5795 | CAST_P is true if this expression is the target of a cast. |
5796 | ||
a723baf1 MM |
5797 | Returns a representation of the expression. */ |
5798 | ||
5799 | static tree | |
93678513 | 5800 | cp_parser_expression (cp_parser* parser, bool cast_p) |
a723baf1 MM |
5801 | { |
5802 | tree expression = NULL_TREE; | |
a723baf1 MM |
5803 | |
5804 | while (true) | |
5805 | { | |
5806 | tree assignment_expression; | |
5807 | ||
5808 | /* Parse the next assignment-expression. */ | |
21526606 | 5809 | assignment_expression |
93678513 | 5810 | = cp_parser_assignment_expression (parser, cast_p); |
a723baf1 MM |
5811 | /* If this is the first assignment-expression, we can just |
5812 | save it away. */ | |
5813 | if (!expression) | |
5814 | expression = assignment_expression; | |
a723baf1 | 5815 | else |
d17811fd MM |
5816 | expression = build_x_compound_expr (expression, |
5817 | assignment_expression); | |
a723baf1 MM |
5818 | /* If the next token is not a comma, then we are done with the |
5819 | expression. */ | |
5820 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
5821 | break; | |
5822 | /* Consume the `,'. */ | |
5823 | cp_lexer_consume_token (parser->lexer); | |
14d22dd6 | 5824 | /* A comma operator cannot appear in a constant-expression. */ |
625cbf93 MM |
5825 | if (cp_parser_non_integral_constant_expression (parser, |
5826 | "a comma operator")) | |
5827 | expression = error_mark_node; | |
14d22dd6 | 5828 | } |
a723baf1 MM |
5829 | |
5830 | return expression; | |
5831 | } | |
5832 | ||
21526606 | 5833 | /* Parse a constant-expression. |
a723baf1 MM |
5834 | |
5835 | constant-expression: | |
21526606 | 5836 | conditional-expression |
14d22dd6 MM |
5837 | |
5838 | If ALLOW_NON_CONSTANT_P a non-constant expression is silently | |
d17811fd MM |
5839 | accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not |
5840 | constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P | |
5841 | is false, NON_CONSTANT_P should be NULL. */ | |
a723baf1 MM |
5842 | |
5843 | static tree | |
21526606 | 5844 | cp_parser_constant_expression (cp_parser* parser, |
14d22dd6 MM |
5845 | bool allow_non_constant_p, |
5846 | bool *non_constant_p) | |
a723baf1 | 5847 | { |
67c03833 JM |
5848 | bool saved_integral_constant_expression_p; |
5849 | bool saved_allow_non_integral_constant_expression_p; | |
5850 | bool saved_non_integral_constant_expression_p; | |
a723baf1 MM |
5851 | tree expression; |
5852 | ||
5853 | /* It might seem that we could simply parse the | |
5854 | conditional-expression, and then check to see if it were | |
5855 | TREE_CONSTANT. However, an expression that is TREE_CONSTANT is | |
5856 | one that the compiler can figure out is constant, possibly after | |
5857 | doing some simplifications or optimizations. The standard has a | |
5858 | precise definition of constant-expression, and we must honor | |
5859 | that, even though it is somewhat more restrictive. | |
5860 | ||
5861 | For example: | |
5862 | ||
5863 | int i[(2, 3)]; | |
5864 | ||
5865 | is not a legal declaration, because `(2, 3)' is not a | |
5866 | constant-expression. The `,' operator is forbidden in a | |
5867 | constant-expression. However, GCC's constant-folding machinery | |
5868 | will fold this operation to an INTEGER_CST for `3'. */ | |
5869 | ||
14d22dd6 | 5870 | /* Save the old settings. */ |
67c03833 | 5871 | saved_integral_constant_expression_p = parser->integral_constant_expression_p; |
21526606 | 5872 | saved_allow_non_integral_constant_expression_p |
67c03833 JM |
5873 | = parser->allow_non_integral_constant_expression_p; |
5874 | saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p; | |
a723baf1 | 5875 | /* We are now parsing a constant-expression. */ |
67c03833 JM |
5876 | parser->integral_constant_expression_p = true; |
5877 | parser->allow_non_integral_constant_expression_p = allow_non_constant_p; | |
5878 | parser->non_integral_constant_expression_p = false; | |
39703eb9 MM |
5879 | /* Although the grammar says "conditional-expression", we parse an |
5880 | "assignment-expression", which also permits "throw-expression" | |
5881 | and the use of assignment operators. In the case that | |
5882 | ALLOW_NON_CONSTANT_P is false, we get better errors than we would | |
5883 | otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is | |
5884 | actually essential that we look for an assignment-expression. | |
5885 | For example, cp_parser_initializer_clauses uses this function to | |
5886 | determine whether a particular assignment-expression is in fact | |
5887 | constant. */ | |
93678513 | 5888 | expression = cp_parser_assignment_expression (parser, /*cast_p=*/false); |
14d22dd6 | 5889 | /* Restore the old settings. */ |
93678513 MM |
5890 | parser->integral_constant_expression_p |
5891 | = saved_integral_constant_expression_p; | |
21526606 | 5892 | parser->allow_non_integral_constant_expression_p |
67c03833 | 5893 | = saved_allow_non_integral_constant_expression_p; |
14d22dd6 | 5894 | if (allow_non_constant_p) |
67c03833 | 5895 | *non_constant_p = parser->non_integral_constant_expression_p; |
93678513 MM |
5896 | else if (parser->non_integral_constant_expression_p) |
5897 | expression = error_mark_node; | |
5898 | parser->non_integral_constant_expression_p | |
5899 | = saved_non_integral_constant_expression_p; | |
a723baf1 MM |
5900 | |
5901 | return expression; | |
5902 | } | |
5903 | ||
7a3ea201 RH |
5904 | /* Parse __builtin_offsetof. |
5905 | ||
5906 | offsetof-expression: | |
5907 | "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")" | |
5908 | ||
5909 | offsetof-member-designator: | |
5910 | id-expression | |
5911 | | offsetof-member-designator "." id-expression | |
5912 | | offsetof-member-designator "[" expression "]" | |
5913 | */ | |
5914 | ||
5915 | static tree | |
5916 | cp_parser_builtin_offsetof (cp_parser *parser) | |
5917 | { | |
5918 | int save_ice_p, save_non_ice_p; | |
5919 | tree type, expr; | |
5920 | cp_id_kind dummy; | |
5921 | ||
5922 | /* We're about to accept non-integral-constant things, but will | |
5923 | definitely yield an integral constant expression. Save and | |
5924 | restore these values around our local parsing. */ | |
5925 | save_ice_p = parser->integral_constant_expression_p; | |
5926 | save_non_ice_p = parser->non_integral_constant_expression_p; | |
5927 | ||
5928 | /* Consume the "__builtin_offsetof" token. */ | |
5929 | cp_lexer_consume_token (parser->lexer); | |
5930 | /* Consume the opening `('. */ | |
5931 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
5932 | /* Parse the type-id. */ | |
5933 | type = cp_parser_type_id (parser); | |
5934 | /* Look for the `,'. */ | |
5935 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
5936 | ||
5937 | /* Build the (type *)null that begins the traditional offsetof macro. */ | |
5938 | expr = build_static_cast (build_pointer_type (type), null_pointer_node); | |
5939 | ||
5940 | /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */ | |
5941 | expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr, | |
5942 | true, &dummy); | |
5943 | while (true) | |
5944 | { | |
5945 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
5946 | switch (token->type) | |
5947 | { | |
5948 | case CPP_OPEN_SQUARE: | |
5949 | /* offsetof-member-designator "[" expression "]" */ | |
5950 | expr = cp_parser_postfix_open_square_expression (parser, expr, true); | |
5951 | break; | |
5952 | ||
5953 | case CPP_DOT: | |
5954 | /* offsetof-member-designator "." identifier */ | |
5955 | cp_lexer_consume_token (parser->lexer); | |
5956 | expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr, | |
5957 | true, &dummy); | |
5958 | break; | |
5959 | ||
5960 | case CPP_CLOSE_PAREN: | |
5961 | /* Consume the ")" token. */ | |
5962 | cp_lexer_consume_token (parser->lexer); | |
5963 | goto success; | |
5964 | ||
5965 | default: | |
5966 | /* Error. We know the following require will fail, but | |
5967 | that gives the proper error message. */ | |
5968 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
5969 | cp_parser_skip_to_closing_parenthesis (parser, true, false, true); | |
5970 | expr = error_mark_node; | |
5971 | goto failure; | |
5972 | } | |
5973 | } | |
5974 | ||
5975 | success: | |
42c244d8 RH |
5976 | /* If we're processing a template, we can't finish the semantics yet. |
5977 | Otherwise we can fold the entire expression now. */ | |
5978 | if (processing_template_decl) | |
5979 | expr = build1 (OFFSETOF_EXPR, size_type_node, expr); | |
5980 | else | |
5981 | expr = fold_offsetof (expr); | |
7a3ea201 RH |
5982 | |
5983 | failure: | |
5984 | parser->integral_constant_expression_p = save_ice_p; | |
5985 | parser->non_integral_constant_expression_p = save_non_ice_p; | |
5986 | ||
5987 | return expr; | |
5988 | } | |
5989 | ||
a723baf1 MM |
5990 | /* Statements [gram.stmt.stmt] */ |
5991 | ||
21526606 | 5992 | /* Parse a statement. |
a723baf1 MM |
5993 | |
5994 | statement: | |
5995 | labeled-statement | |
5996 | expression-statement | |
5997 | compound-statement | |
5998 | selection-statement | |
5999 | iteration-statement | |
6000 | jump-statement | |
6001 | declaration-statement | |
6002 | try-block */ | |
6003 | ||
6004 | static void | |
325c3691 | 6005 | cp_parser_statement (cp_parser* parser, tree in_statement_expr) |
a723baf1 MM |
6006 | { |
6007 | tree statement; | |
6008 | cp_token *token; | |
93409b8c | 6009 | location_t statement_location; |
a723baf1 MM |
6010 | |
6011 | /* There is no statement yet. */ | |
6012 | statement = NULL_TREE; | |
6013 | /* Peek at the next token. */ | |
6014 | token = cp_lexer_peek_token (parser->lexer); | |
6de9cd9a | 6015 | /* Remember the location of the first token in the statement. */ |
93409b8c | 6016 | statement_location = token->location; |
a723baf1 MM |
6017 | /* If this is a keyword, then that will often determine what kind of |
6018 | statement we have. */ | |
6019 | if (token->type == CPP_KEYWORD) | |
6020 | { | |
6021 | enum rid keyword = token->keyword; | |
6022 | ||
6023 | switch (keyword) | |
6024 | { | |
6025 | case RID_CASE: | |
6026 | case RID_DEFAULT: | |
a5bcc582 | 6027 | statement = cp_parser_labeled_statement (parser, |
325c3691 | 6028 | in_statement_expr); |
a723baf1 MM |
6029 | break; |
6030 | ||
6031 | case RID_IF: | |
6032 | case RID_SWITCH: | |
6033 | statement = cp_parser_selection_statement (parser); | |
6034 | break; | |
6035 | ||
6036 | case RID_WHILE: | |
6037 | case RID_DO: | |
6038 | case RID_FOR: | |
6039 | statement = cp_parser_iteration_statement (parser); | |
6040 | break; | |
6041 | ||
6042 | case RID_BREAK: | |
6043 | case RID_CONTINUE: | |
6044 | case RID_RETURN: | |
6045 | case RID_GOTO: | |
6046 | statement = cp_parser_jump_statement (parser); | |
6047 | break; | |
6048 | ||
e58a9aa1 ZL |
6049 | /* Objective-C++ exception-handling constructs. */ |
6050 | case RID_AT_TRY: | |
6051 | case RID_AT_CATCH: | |
6052 | case RID_AT_FINALLY: | |
6053 | case RID_AT_SYNCHRONIZED: | |
6054 | case RID_AT_THROW: | |
6055 | statement = cp_parser_objc_statement (parser); | |
6056 | break; | |
6057 | ||
a723baf1 MM |
6058 | case RID_TRY: |
6059 | statement = cp_parser_try_block (parser); | |
6060 | break; | |
6061 | ||
6062 | default: | |
6063 | /* It might be a keyword like `int' that can start a | |
6064 | declaration-statement. */ | |
6065 | break; | |
6066 | } | |
6067 | } | |
6068 | else if (token->type == CPP_NAME) | |
6069 | { | |
6070 | /* If the next token is a `:', then we are looking at a | |
6071 | labeled-statement. */ | |
6072 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
6073 | if (token->type == CPP_COLON) | |
325c3691 | 6074 | statement = cp_parser_labeled_statement (parser, in_statement_expr); |
a723baf1 MM |
6075 | } |
6076 | /* Anything that starts with a `{' must be a compound-statement. */ | |
6077 | else if (token->type == CPP_OPEN_BRACE) | |
325c3691 | 6078 | statement = cp_parser_compound_statement (parser, NULL, false); |
36952dea ZW |
6079 | /* CPP_PRAGMA is a #pragma inside a function body, which constitutes |
6080 | a statement all its own. */ | |
6081 | else if (token->type == CPP_PRAGMA) | |
6082 | { | |
6083 | cp_lexer_handle_pragma (parser->lexer); | |
6084 | return; | |
6085 | } | |
a723baf1 MM |
6086 | |
6087 | /* Everything else must be a declaration-statement or an | |
21526606 | 6088 | expression-statement. Try for the declaration-statement |
a723baf1 MM |
6089 | first, unless we are looking at a `;', in which case we know that |
6090 | we have an expression-statement. */ | |
6091 | if (!statement) | |
6092 | { | |
6093 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
6094 | { | |
6095 | cp_parser_parse_tentatively (parser); | |
6096 | /* Try to parse the declaration-statement. */ | |
6097 | cp_parser_declaration_statement (parser); | |
6098 | /* If that worked, we're done. */ | |
6099 | if (cp_parser_parse_definitely (parser)) | |
6100 | return; | |
6101 | } | |
6102 | /* Look for an expression-statement instead. */ | |
325c3691 | 6103 | statement = cp_parser_expression_statement (parser, in_statement_expr); |
a723baf1 MM |
6104 | } |
6105 | ||
6106 | /* Set the line number for the statement. */ | |
009ed910 | 6107 | if (statement && STATEMENT_CODE_P (TREE_CODE (statement))) |
93409b8c | 6108 | SET_EXPR_LOCATION (statement, statement_location); |
a723baf1 MM |
6109 | } |
6110 | ||
6111 | /* Parse a labeled-statement. | |
6112 | ||
6113 | labeled-statement: | |
6114 | identifier : statement | |
6115 | case constant-expression : statement | |
98ce043b MM |
6116 | default : statement |
6117 | ||
6118 | GNU Extension: | |
21526606 | 6119 | |
98ce043b MM |
6120 | labeled-statement: |
6121 | case constant-expression ... constant-expression : statement | |
a723baf1 | 6122 | |
8c161995 RH |
6123 | Returns the new CASE_LABEL_EXPR, for a `case' or `default' label. |
6124 | For an ordinary label, returns a LABEL_EXPR. */ | |
a723baf1 MM |
6125 | |
6126 | static tree | |
325c3691 | 6127 | cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr) |
a723baf1 MM |
6128 | { |
6129 | cp_token *token; | |
0e59b3fb | 6130 | tree statement = error_mark_node; |
a723baf1 MM |
6131 | |
6132 | /* The next token should be an identifier. */ | |
6133 | token = cp_lexer_peek_token (parser->lexer); | |
6134 | if (token->type != CPP_NAME | |
6135 | && token->type != CPP_KEYWORD) | |
6136 | { | |
6137 | cp_parser_error (parser, "expected labeled-statement"); | |
6138 | return error_mark_node; | |
6139 | } | |
6140 | ||
6141 | switch (token->keyword) | |
6142 | { | |
6143 | case RID_CASE: | |
6144 | { | |
98ce043b MM |
6145 | tree expr, expr_hi; |
6146 | cp_token *ellipsis; | |
a723baf1 MM |
6147 | |
6148 | /* Consume the `case' token. */ | |
6149 | cp_lexer_consume_token (parser->lexer); | |
6150 | /* Parse the constant-expression. */ | |
21526606 | 6151 | expr = cp_parser_constant_expression (parser, |
d17811fd | 6152 | /*allow_non_constant_p=*/false, |
14d22dd6 | 6153 | NULL); |
98ce043b MM |
6154 | |
6155 | ellipsis = cp_lexer_peek_token (parser->lexer); | |
6156 | if (ellipsis->type == CPP_ELLIPSIS) | |
6157 | { | |
6158 | /* Consume the `...' token. */ | |
6159 | cp_lexer_consume_token (parser->lexer); | |
6160 | expr_hi = | |
6161 | cp_parser_constant_expression (parser, | |
6162 | /*allow_non_constant_p=*/false, | |
6163 | NULL); | |
6164 | /* We don't need to emit warnings here, as the common code | |
6165 | will do this for us. */ | |
6166 | } | |
6167 | else | |
6168 | expr_hi = NULL_TREE; | |
6169 | ||
0e59b3fb | 6170 | if (!parser->in_switch_statement_p) |
2a13a625 | 6171 | error ("case label %qE not within a switch statement", expr); |
0e59b3fb | 6172 | else |
98ce043b | 6173 | statement = finish_case_label (expr, expr_hi); |
a723baf1 MM |
6174 | } |
6175 | break; | |
6176 | ||
6177 | case RID_DEFAULT: | |
6178 | /* Consume the `default' token. */ | |
6179 | cp_lexer_consume_token (parser->lexer); | |
0e59b3fb MM |
6180 | if (!parser->in_switch_statement_p) |
6181 | error ("case label not within a switch statement"); | |
6182 | else | |
6183 | statement = finish_case_label (NULL_TREE, NULL_TREE); | |
a723baf1 MM |
6184 | break; |
6185 | ||
6186 | default: | |
6187 | /* Anything else must be an ordinary label. */ | |
6188 | statement = finish_label_stmt (cp_parser_identifier (parser)); | |
6189 | break; | |
6190 | } | |
6191 | ||
6192 | /* Require the `:' token. */ | |
6193 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
6194 | /* Parse the labeled statement. */ | |
325c3691 | 6195 | cp_parser_statement (parser, in_statement_expr); |
a723baf1 MM |
6196 | |
6197 | /* Return the label, in the case of a `case' or `default' label. */ | |
6198 | return statement; | |
6199 | } | |
6200 | ||
6201 | /* Parse an expression-statement. | |
6202 | ||
6203 | expression-statement: | |
6204 | expression [opt] ; | |
6205 | ||
6206 | Returns the new EXPR_STMT -- or NULL_TREE if the expression | |
a5bcc582 NS |
6207 | statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P |
6208 | indicates whether this expression-statement is part of an | |
6209 | expression statement. */ | |
a723baf1 MM |
6210 | |
6211 | static tree | |
325c3691 | 6212 | cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr) |
a723baf1 | 6213 | { |
a5bcc582 | 6214 | tree statement = NULL_TREE; |
a723baf1 | 6215 | |
a5bcc582 | 6216 | /* If the next token is a ';', then there is no expression |
04c06002 | 6217 | statement. */ |
a723baf1 | 6218 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) |
93678513 | 6219 | statement = cp_parser_expression (parser, /*cast_p=*/false); |
21526606 | 6220 | |
a723baf1 | 6221 | /* Consume the final `;'. */ |
e0860732 | 6222 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 | 6223 | |
325c3691 | 6224 | if (in_statement_expr |
a5bcc582 | 6225 | && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) |
93678513 MM |
6226 | /* This is the final expression statement of a statement |
6227 | expression. */ | |
6228 | statement = finish_stmt_expr_expr (statement, in_statement_expr); | |
a5bcc582 NS |
6229 | else if (statement) |
6230 | statement = finish_expr_stmt (statement); | |
6231 | else | |
6232 | finish_stmt (); | |
21526606 | 6233 | |
a723baf1 MM |
6234 | return statement; |
6235 | } | |
6236 | ||
6237 | /* Parse a compound-statement. | |
6238 | ||
6239 | compound-statement: | |
6240 | { statement-seq [opt] } | |
21526606 | 6241 | |
5882f0f3 | 6242 | Returns a tree representing the statement. */ |
a723baf1 MM |
6243 | |
6244 | static tree | |
325c3691 RH |
6245 | cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr, |
6246 | bool in_try) | |
a723baf1 MM |
6247 | { |
6248 | tree compound_stmt; | |
6249 | ||
6250 | /* Consume the `{'. */ | |
6251 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
6252 | return error_mark_node; | |
6253 | /* Begin the compound-statement. */ | |
325c3691 | 6254 | compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0); |
a723baf1 | 6255 | /* Parse an (optional) statement-seq. */ |
325c3691 | 6256 | cp_parser_statement_seq_opt (parser, in_statement_expr); |
a723baf1 | 6257 | /* Finish the compound-statement. */ |
7a3397c7 | 6258 | finish_compound_stmt (compound_stmt); |
a723baf1 MM |
6259 | /* Consume the `}'. */ |
6260 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
6261 | ||
6262 | return compound_stmt; | |
6263 | } | |
6264 | ||
6265 | /* Parse an (optional) statement-seq. | |
6266 | ||
6267 | statement-seq: | |
6268 | statement | |
6269 | statement-seq [opt] statement */ | |
6270 | ||
6271 | static void | |
325c3691 | 6272 | cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr) |
a723baf1 MM |
6273 | { |
6274 | /* Scan statements until there aren't any more. */ | |
6275 | while (true) | |
6276 | { | |
6277 | /* If we're looking at a `}', then we've run out of statements. */ | |
6278 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE) | |
6279 | || cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
6280 | break; | |
6281 | ||
6282 | /* Parse the statement. */ | |
325c3691 | 6283 | cp_parser_statement (parser, in_statement_expr); |
a723baf1 MM |
6284 | } |
6285 | } | |
6286 | ||
6287 | /* Parse a selection-statement. | |
6288 | ||
6289 | selection-statement: | |
6290 | if ( condition ) statement | |
6291 | if ( condition ) statement else statement | |
21526606 | 6292 | switch ( condition ) statement |
a723baf1 MM |
6293 | |
6294 | Returns the new IF_STMT or SWITCH_STMT. */ | |
6295 | ||
6296 | static tree | |
94edc4ab | 6297 | cp_parser_selection_statement (cp_parser* parser) |
a723baf1 MM |
6298 | { |
6299 | cp_token *token; | |
6300 | enum rid keyword; | |
6301 | ||
6302 | /* Peek at the next token. */ | |
6303 | token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement"); | |
6304 | ||
6305 | /* See what kind of keyword it is. */ | |
6306 | keyword = token->keyword; | |
6307 | switch (keyword) | |
6308 | { | |
6309 | case RID_IF: | |
6310 | case RID_SWITCH: | |
6311 | { | |
6312 | tree statement; | |
6313 | tree condition; | |
6314 | ||
6315 | /* Look for the `('. */ | |
6316 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) | |
6317 | { | |
6318 | cp_parser_skip_to_end_of_statement (parser); | |
6319 | return error_mark_node; | |
6320 | } | |
6321 | ||
6322 | /* Begin the selection-statement. */ | |
6323 | if (keyword == RID_IF) | |
6324 | statement = begin_if_stmt (); | |
6325 | else | |
6326 | statement = begin_switch_stmt (); | |
6327 | ||
6328 | /* Parse the condition. */ | |
6329 | condition = cp_parser_condition (parser); | |
6330 | /* Look for the `)'. */ | |
6331 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
a668c6ad MM |
6332 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
6333 | /*consume_paren=*/true); | |
a723baf1 MM |
6334 | |
6335 | if (keyword == RID_IF) | |
6336 | { | |
a723baf1 MM |
6337 | /* Add the condition. */ |
6338 | finish_if_stmt_cond (condition, statement); | |
6339 | ||
6340 | /* Parse the then-clause. */ | |
325c3691 | 6341 | cp_parser_implicitly_scoped_statement (parser); |
a723baf1 MM |
6342 | finish_then_clause (statement); |
6343 | ||
6344 | /* If the next token is `else', parse the else-clause. */ | |
6345 | if (cp_lexer_next_token_is_keyword (parser->lexer, | |
6346 | RID_ELSE)) | |
6347 | { | |
a723baf1 MM |
6348 | /* Consume the `else' keyword. */ |
6349 | cp_lexer_consume_token (parser->lexer); | |
325c3691 | 6350 | begin_else_clause (statement); |
a723baf1 | 6351 | /* Parse the else-clause. */ |
325c3691 | 6352 | cp_parser_implicitly_scoped_statement (parser); |
a723baf1 MM |
6353 | finish_else_clause (statement); |
6354 | } | |
6355 | ||
6356 | /* Now we're all done with the if-statement. */ | |
325c3691 | 6357 | finish_if_stmt (statement); |
a723baf1 MM |
6358 | } |
6359 | else | |
6360 | { | |
0e59b3fb | 6361 | bool in_switch_statement_p; |
a723baf1 MM |
6362 | |
6363 | /* Add the condition. */ | |
6364 | finish_switch_cond (condition, statement); | |
6365 | ||
6366 | /* Parse the body of the switch-statement. */ | |
0e59b3fb MM |
6367 | in_switch_statement_p = parser->in_switch_statement_p; |
6368 | parser->in_switch_statement_p = true; | |
325c3691 | 6369 | cp_parser_implicitly_scoped_statement (parser); |
0e59b3fb | 6370 | parser->in_switch_statement_p = in_switch_statement_p; |
a723baf1 MM |
6371 | |
6372 | /* Now we're all done with the switch-statement. */ | |
6373 | finish_switch_stmt (statement); | |
6374 | } | |
6375 | ||
6376 | return statement; | |
6377 | } | |
6378 | break; | |
6379 | ||
6380 | default: | |
6381 | cp_parser_error (parser, "expected selection-statement"); | |
6382 | return error_mark_node; | |
6383 | } | |
6384 | } | |
6385 | ||
21526606 | 6386 | /* Parse a condition. |
a723baf1 MM |
6387 | |
6388 | condition: | |
6389 | expression | |
21526606 | 6390 | type-specifier-seq declarator = assignment-expression |
a723baf1 MM |
6391 | |
6392 | GNU Extension: | |
21526606 | 6393 | |
a723baf1 | 6394 | condition: |
21526606 | 6395 | type-specifier-seq declarator asm-specification [opt] |
a723baf1 | 6396 | attributes [opt] = assignment-expression |
21526606 | 6397 | |
a723baf1 MM |
6398 | Returns the expression that should be tested. */ |
6399 | ||
6400 | static tree | |
94edc4ab | 6401 | cp_parser_condition (cp_parser* parser) |
a723baf1 | 6402 | { |
62d1db17 | 6403 | cp_decl_specifier_seq type_specifiers; |
a723baf1 MM |
6404 | const char *saved_message; |
6405 | ||
6406 | /* Try the declaration first. */ | |
6407 | cp_parser_parse_tentatively (parser); | |
6408 | /* New types are not allowed in the type-specifier-seq for a | |
6409 | condition. */ | |
6410 | saved_message = parser->type_definition_forbidden_message; | |
6411 | parser->type_definition_forbidden_message | |
6412 | = "types may not be defined in conditions"; | |
6413 | /* Parse the type-specifier-seq. */ | |
d4113656 MM |
6414 | cp_parser_type_specifier_seq (parser, /*is_condition==*/true, |
6415 | &type_specifiers); | |
a723baf1 MM |
6416 | /* Restore the saved message. */ |
6417 | parser->type_definition_forbidden_message = saved_message; | |
6418 | /* If all is well, we might be looking at a declaration. */ | |
6419 | if (!cp_parser_error_occurred (parser)) | |
6420 | { | |
6421 | tree decl; | |
6422 | tree asm_specification; | |
6423 | tree attributes; | |
058b15c1 | 6424 | cp_declarator *declarator; |
a723baf1 | 6425 | tree initializer = NULL_TREE; |
21526606 | 6426 | |
a723baf1 | 6427 | /* Parse the declarator. */ |
62b8a44e | 6428 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 | 6429 | /*ctor_dtor_or_conv_p=*/NULL, |
db86dd14 MM |
6430 | /*parenthesized_p=*/NULL, |
6431 | /*member_p=*/false); | |
a723baf1 MM |
6432 | /* Parse the attributes. */ |
6433 | attributes = cp_parser_attributes_opt (parser); | |
6434 | /* Parse the asm-specification. */ | |
6435 | asm_specification = cp_parser_asm_specification_opt (parser); | |
6436 | /* If the next token is not an `=', then we might still be | |
6437 | looking at an expression. For example: | |
21526606 | 6438 | |
a723baf1 | 6439 | if (A(a).x) |
21526606 | 6440 | |
a723baf1 MM |
6441 | looks like a decl-specifier-seq and a declarator -- but then |
6442 | there is no `=', so this is an expression. */ | |
6443 | cp_parser_require (parser, CPP_EQ, "`='"); | |
6444 | /* If we did see an `=', then we are looking at a declaration | |
6445 | for sure. */ | |
6446 | if (cp_parser_parse_definitely (parser)) | |
6447 | { | |
4514aa8c | 6448 | tree pushed_scope; |
73a8adb6 | 6449 | |
a723baf1 | 6450 | /* Create the declaration. */ |
62d1db17 | 6451 | decl = start_decl (declarator, &type_specifiers, |
a723baf1 | 6452 | /*initialized_p=*/true, |
73a8adb6 | 6453 | attributes, /*prefix_attributes=*/NULL_TREE, |
4514aa8c | 6454 | &pushed_scope); |
a723baf1 | 6455 | /* Parse the assignment-expression. */ |
93678513 MM |
6456 | initializer = cp_parser_assignment_expression (parser, |
6457 | /*cast_p=*/false); | |
21526606 | 6458 | |
a723baf1 | 6459 | /* Process the initializer. */ |
21526606 EC |
6460 | cp_finish_decl (decl, |
6461 | initializer, | |
6462 | asm_specification, | |
a723baf1 | 6463 | LOOKUP_ONLYCONVERTING); |
c162c75e | 6464 | |
4514aa8c NS |
6465 | if (pushed_scope) |
6466 | pop_scope (pushed_scope); | |
21526606 | 6467 | |
a723baf1 MM |
6468 | return convert_from_reference (decl); |
6469 | } | |
6470 | } | |
6471 | /* If we didn't even get past the declarator successfully, we are | |
6472 | definitely not looking at a declaration. */ | |
6473 | else | |
6474 | cp_parser_abort_tentative_parse (parser); | |
6475 | ||
6476 | /* Otherwise, we are looking at an expression. */ | |
93678513 | 6477 | return cp_parser_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
6478 | } |
6479 | ||
6480 | /* Parse an iteration-statement. | |
6481 | ||
6482 | iteration-statement: | |
6483 | while ( condition ) statement | |
6484 | do statement while ( expression ) ; | |
6485 | for ( for-init-statement condition [opt] ; expression [opt] ) | |
6486 | statement | |
6487 | ||
6488 | Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */ | |
6489 | ||
6490 | static tree | |
94edc4ab | 6491 | cp_parser_iteration_statement (cp_parser* parser) |
a723baf1 MM |
6492 | { |
6493 | cp_token *token; | |
6494 | enum rid keyword; | |
6495 | tree statement; | |
0e59b3fb MM |
6496 | bool in_iteration_statement_p; |
6497 | ||
a723baf1 MM |
6498 | |
6499 | /* Peek at the next token. */ | |
6500 | token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement"); | |
6501 | if (!token) | |
6502 | return error_mark_node; | |
6503 | ||
0e59b3fb | 6504 | /* Remember whether or not we are already within an iteration |
21526606 | 6505 | statement. */ |
0e59b3fb MM |
6506 | in_iteration_statement_p = parser->in_iteration_statement_p; |
6507 | ||
a723baf1 MM |
6508 | /* See what kind of keyword it is. */ |
6509 | keyword = token->keyword; | |
6510 | switch (keyword) | |
6511 | { | |
6512 | case RID_WHILE: | |
6513 | { | |
6514 | tree condition; | |
6515 | ||
6516 | /* Begin the while-statement. */ | |
6517 | statement = begin_while_stmt (); | |
6518 | /* Look for the `('. */ | |
6519 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
6520 | /* Parse the condition. */ | |
6521 | condition = cp_parser_condition (parser); | |
6522 | finish_while_stmt_cond (condition, statement); | |
6523 | /* Look for the `)'. */ | |
6524 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
6525 | /* Parse the dependent statement. */ | |
0e59b3fb | 6526 | parser->in_iteration_statement_p = true; |
a723baf1 | 6527 | cp_parser_already_scoped_statement (parser); |
0e59b3fb | 6528 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
6529 | /* We're done with the while-statement. */ |
6530 | finish_while_stmt (statement); | |
6531 | } | |
6532 | break; | |
6533 | ||
6534 | case RID_DO: | |
6535 | { | |
6536 | tree expression; | |
6537 | ||
6538 | /* Begin the do-statement. */ | |
6539 | statement = begin_do_stmt (); | |
6540 | /* Parse the body of the do-statement. */ | |
0e59b3fb | 6541 | parser->in_iteration_statement_p = true; |
a723baf1 | 6542 | cp_parser_implicitly_scoped_statement (parser); |
0e59b3fb | 6543 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
6544 | finish_do_body (statement); |
6545 | /* Look for the `while' keyword. */ | |
6546 | cp_parser_require_keyword (parser, RID_WHILE, "`while'"); | |
6547 | /* Look for the `('. */ | |
6548 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
6549 | /* Parse the expression. */ | |
93678513 | 6550 | expression = cp_parser_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
6551 | /* We're done with the do-statement. */ |
6552 | finish_do_stmt (expression, statement); | |
6553 | /* Look for the `)'. */ | |
6554 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
6555 | /* Look for the `;'. */ | |
6556 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6557 | } | |
6558 | break; | |
6559 | ||
6560 | case RID_FOR: | |
6561 | { | |
6562 | tree condition = NULL_TREE; | |
6563 | tree expression = NULL_TREE; | |
6564 | ||
6565 | /* Begin the for-statement. */ | |
6566 | statement = begin_for_stmt (); | |
6567 | /* Look for the `('. */ | |
6568 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
6569 | /* Parse the initialization. */ | |
6570 | cp_parser_for_init_statement (parser); | |
6571 | finish_for_init_stmt (statement); | |
6572 | ||
6573 | /* If there's a condition, process it. */ | |
6574 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
6575 | condition = cp_parser_condition (parser); | |
6576 | finish_for_cond (condition, statement); | |
6577 | /* Look for the `;'. */ | |
6578 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6579 | ||
6580 | /* If there's an expression, process it. */ | |
6581 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) | |
93678513 | 6582 | expression = cp_parser_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
6583 | finish_for_expr (expression, statement); |
6584 | /* Look for the `)'. */ | |
d5a10cf0 | 6585 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); |
98ca843c | 6586 | |
a723baf1 | 6587 | /* Parse the body of the for-statement. */ |
0e59b3fb | 6588 | parser->in_iteration_statement_p = true; |
a723baf1 | 6589 | cp_parser_already_scoped_statement (parser); |
0e59b3fb | 6590 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
6591 | |
6592 | /* We're done with the for-statement. */ | |
6593 | finish_for_stmt (statement); | |
6594 | } | |
6595 | break; | |
6596 | ||
6597 | default: | |
6598 | cp_parser_error (parser, "expected iteration-statement"); | |
6599 | statement = error_mark_node; | |
6600 | break; | |
6601 | } | |
6602 | ||
6603 | return statement; | |
6604 | } | |
6605 | ||
6606 | /* Parse a for-init-statement. | |
6607 | ||
6608 | for-init-statement: | |
6609 | expression-statement | |
6610 | simple-declaration */ | |
6611 | ||
6612 | static void | |
94edc4ab | 6613 | cp_parser_for_init_statement (cp_parser* parser) |
a723baf1 MM |
6614 | { |
6615 | /* If the next token is a `;', then we have an empty | |
34cd5ae7 | 6616 | expression-statement. Grammatically, this is also a |
a723baf1 MM |
6617 | simple-declaration, but an invalid one, because it does not |
6618 | declare anything. Therefore, if we did not handle this case | |
6619 | specially, we would issue an error message about an invalid | |
6620 | declaration. */ | |
6621 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
6622 | { | |
6623 | /* We're going to speculatively look for a declaration, falling back | |
6624 | to an expression, if necessary. */ | |
6625 | cp_parser_parse_tentatively (parser); | |
6626 | /* Parse the declaration. */ | |
6627 | cp_parser_simple_declaration (parser, | |
6628 | /*function_definition_allowed_p=*/false); | |
6629 | /* If the tentative parse failed, then we shall need to look for an | |
6630 | expression-statement. */ | |
6631 | if (cp_parser_parse_definitely (parser)) | |
6632 | return; | |
6633 | } | |
6634 | ||
a5bcc582 | 6635 | cp_parser_expression_statement (parser, false); |
a723baf1 MM |
6636 | } |
6637 | ||
6638 | /* Parse a jump-statement. | |
6639 | ||
6640 | jump-statement: | |
6641 | break ; | |
6642 | continue ; | |
6643 | return expression [opt] ; | |
21526606 | 6644 | goto identifier ; |
a723baf1 MM |
6645 | |
6646 | GNU extension: | |
6647 | ||
6648 | jump-statement: | |
6649 | goto * expression ; | |
6650 | ||
5088b058 | 6651 | Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */ |
a723baf1 MM |
6652 | |
6653 | static tree | |
94edc4ab | 6654 | cp_parser_jump_statement (cp_parser* parser) |
a723baf1 MM |
6655 | { |
6656 | tree statement = error_mark_node; | |
6657 | cp_token *token; | |
6658 | enum rid keyword; | |
6659 | ||
6660 | /* Peek at the next token. */ | |
6661 | token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement"); | |
6662 | if (!token) | |
6663 | return error_mark_node; | |
6664 | ||
6665 | /* See what kind of keyword it is. */ | |
6666 | keyword = token->keyword; | |
6667 | switch (keyword) | |
6668 | { | |
6669 | case RID_BREAK: | |
0e59b3fb MM |
6670 | if (!parser->in_switch_statement_p |
6671 | && !parser->in_iteration_statement_p) | |
6672 | { | |
6673 | error ("break statement not within loop or switch"); | |
6674 | statement = error_mark_node; | |
6675 | } | |
6676 | else | |
6677 | statement = finish_break_stmt (); | |
2a13a625 | 6678 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
6679 | break; |
6680 | ||
6681 | case RID_CONTINUE: | |
0e59b3fb MM |
6682 | if (!parser->in_iteration_statement_p) |
6683 | { | |
6684 | error ("continue statement not within a loop"); | |
6685 | statement = error_mark_node; | |
6686 | } | |
6687 | else | |
6688 | statement = finish_continue_stmt (); | |
2a13a625 | 6689 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
6690 | break; |
6691 | ||
6692 | case RID_RETURN: | |
6693 | { | |
6694 | tree expr; | |
6695 | ||
21526606 | 6696 | /* If the next token is a `;', then there is no |
a723baf1 MM |
6697 | expression. */ |
6698 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
93678513 | 6699 | expr = cp_parser_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
6700 | else |
6701 | expr = NULL_TREE; | |
6702 | /* Build the return-statement. */ | |
6703 | statement = finish_return_stmt (expr); | |
6704 | /* Look for the final `;'. */ | |
2a13a625 | 6705 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
6706 | } |
6707 | break; | |
6708 | ||
6709 | case RID_GOTO: | |
6710 | /* Create the goto-statement. */ | |
6711 | if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)) | |
6712 | { | |
6713 | /* Issue a warning about this use of a GNU extension. */ | |
6714 | if (pedantic) | |
6715 | pedwarn ("ISO C++ forbids computed gotos"); | |
6716 | /* Consume the '*' token. */ | |
6717 | cp_lexer_consume_token (parser->lexer); | |
6718 | /* Parse the dependent expression. */ | |
93678513 | 6719 | finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false)); |
a723baf1 MM |
6720 | } |
6721 | else | |
6722 | finish_goto_stmt (cp_parser_identifier (parser)); | |
6723 | /* Look for the final `;'. */ | |
2a13a625 | 6724 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
6725 | break; |
6726 | ||
6727 | default: | |
6728 | cp_parser_error (parser, "expected jump-statement"); | |
6729 | break; | |
6730 | } | |
6731 | ||
6732 | return statement; | |
6733 | } | |
6734 | ||
6735 | /* Parse a declaration-statement. | |
6736 | ||
6737 | declaration-statement: | |
6738 | block-declaration */ | |
6739 | ||
6740 | static void | |
94edc4ab | 6741 | cp_parser_declaration_statement (cp_parser* parser) |
a723baf1 | 6742 | { |
058b15c1 MM |
6743 | void *p; |
6744 | ||
6745 | /* Get the high-water mark for the DECLARATOR_OBSTACK. */ | |
6746 | p = obstack_alloc (&declarator_obstack, 0); | |
6747 | ||
6748 | /* Parse the block-declaration. */ | |
a723baf1 MM |
6749 | cp_parser_block_declaration (parser, /*statement_p=*/true); |
6750 | ||
058b15c1 MM |
6751 | /* Free any declarators allocated. */ |
6752 | obstack_free (&declarator_obstack, p); | |
6753 | ||
a723baf1 MM |
6754 | /* Finish off the statement. */ |
6755 | finish_stmt (); | |
6756 | } | |
6757 | ||
6758 | /* Some dependent statements (like `if (cond) statement'), are | |
6759 | implicitly in their own scope. In other words, if the statement is | |
6760 | a single statement (as opposed to a compound-statement), it is | |
6761 | none-the-less treated as if it were enclosed in braces. Any | |
6762 | declarations appearing in the dependent statement are out of scope | |
6763 | after control passes that point. This function parses a statement, | |
6764 | but ensures that is in its own scope, even if it is not a | |
21526606 | 6765 | compound-statement. |
a723baf1 MM |
6766 | |
6767 | Returns the new statement. */ | |
6768 | ||
6769 | static tree | |
94edc4ab | 6770 | cp_parser_implicitly_scoped_statement (cp_parser* parser) |
a723baf1 MM |
6771 | { |
6772 | tree statement; | |
6773 | ||
6774 | /* If the token is not a `{', then we must take special action. */ | |
6775 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
6776 | { | |
6777 | /* Create a compound-statement. */ | |
325c3691 | 6778 | statement = begin_compound_stmt (0); |
a723baf1 | 6779 | /* Parse the dependent-statement. */ |
a5bcc582 | 6780 | cp_parser_statement (parser, false); |
a723baf1 | 6781 | /* Finish the dummy compound-statement. */ |
7a3397c7 | 6782 | finish_compound_stmt (statement); |
a723baf1 MM |
6783 | } |
6784 | /* Otherwise, we simply parse the statement directly. */ | |
6785 | else | |
325c3691 | 6786 | statement = cp_parser_compound_statement (parser, NULL, false); |
a723baf1 MM |
6787 | |
6788 | /* Return the statement. */ | |
6789 | return statement; | |
6790 | } | |
6791 | ||
6792 | /* For some dependent statements (like `while (cond) statement'), we | |
6793 | have already created a scope. Therefore, even if the dependent | |
6794 | statement is a compound-statement, we do not want to create another | |
6795 | scope. */ | |
6796 | ||
6797 | static void | |
94edc4ab | 6798 | cp_parser_already_scoped_statement (cp_parser* parser) |
a723baf1 | 6799 | { |
325c3691 RH |
6800 | /* If the token is a `{', then we must take special action. */ |
6801 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
6802 | cp_parser_statement (parser, false); | |
6803 | else | |
a723baf1 | 6804 | { |
325c3691 RH |
6805 | /* Avoid calling cp_parser_compound_statement, so that we |
6806 | don't create a new scope. Do everything else by hand. */ | |
6807 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
6808 | cp_parser_statement_seq_opt (parser, false); | |
6809 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
a723baf1 | 6810 | } |
a723baf1 MM |
6811 | } |
6812 | ||
6813 | /* Declarations [gram.dcl.dcl] */ | |
6814 | ||
6815 | /* Parse an optional declaration-sequence. | |
6816 | ||
6817 | declaration-seq: | |
6818 | declaration | |
6819 | declaration-seq declaration */ | |
6820 | ||
6821 | static void | |
94edc4ab | 6822 | cp_parser_declaration_seq_opt (cp_parser* parser) |
a723baf1 MM |
6823 | { |
6824 | while (true) | |
6825 | { | |
6826 | cp_token *token; | |
6827 | ||
6828 | token = cp_lexer_peek_token (parser->lexer); | |
6829 | ||
6830 | if (token->type == CPP_CLOSE_BRACE | |
6831 | || token->type == CPP_EOF) | |
6832 | break; | |
6833 | ||
21526606 | 6834 | if (token->type == CPP_SEMICOLON) |
a723baf1 MM |
6835 | { |
6836 | /* A declaration consisting of a single semicolon is | |
6837 | invalid. Allow it unless we're being pedantic. */ | |
a723baf1 | 6838 | cp_lexer_consume_token (parser->lexer); |
2cfe82fe ZW |
6839 | if (pedantic && !in_system_header) |
6840 | pedwarn ("extra %<;%>"); | |
a723baf1 MM |
6841 | continue; |
6842 | } | |
6843 | ||
7d381002 | 6844 | /* If we're entering or exiting a region that's implicitly |
03fd3f84 | 6845 | extern "C", modify the lang context appropriately. */ |
7d381002 MA |
6846 | if (!parser->implicit_extern_c && token->implicit_extern_c) |
6847 | { | |
6848 | push_lang_context (lang_name_c); | |
6849 | parser->implicit_extern_c = true; | |
6850 | } | |
6851 | else if (parser->implicit_extern_c && !token->implicit_extern_c) | |
6852 | { | |
6853 | pop_lang_context (); | |
6854 | parser->implicit_extern_c = false; | |
6855 | } | |
6856 | ||
36952dea ZW |
6857 | if (token->type == CPP_PRAGMA) |
6858 | { | |
6859 | /* A top-level declaration can consist solely of a #pragma. | |
6860 | A nested declaration cannot, so this is done here and not | |
6861 | in cp_parser_declaration. (A #pragma at block scope is | |
6862 | handled in cp_parser_statement.) */ | |
6863 | cp_lexer_handle_pragma (parser->lexer); | |
6864 | continue; | |
6865 | } | |
6866 | ||
c838d82f | 6867 | /* Parse the declaration itself. */ |
a723baf1 MM |
6868 | cp_parser_declaration (parser); |
6869 | } | |
6870 | } | |
6871 | ||
6872 | /* Parse a declaration. | |
6873 | ||
6874 | declaration: | |
6875 | block-declaration | |
6876 | function-definition | |
6877 | template-declaration | |
6878 | explicit-instantiation | |
6879 | explicit-specialization | |
6880 | linkage-specification | |
21526606 | 6881 | namespace-definition |
1092805d MM |
6882 | |
6883 | GNU extension: | |
6884 | ||
6885 | declaration: | |
6886 | __extension__ declaration */ | |
a723baf1 MM |
6887 | |
6888 | static void | |
94edc4ab | 6889 | cp_parser_declaration (cp_parser* parser) |
a723baf1 MM |
6890 | { |
6891 | cp_token token1; | |
6892 | cp_token token2; | |
1092805d | 6893 | int saved_pedantic; |
058b15c1 | 6894 | void *p; |
1092805d MM |
6895 | |
6896 | /* Check for the `__extension__' keyword. */ | |
6897 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
6898 | { | |
6899 | /* Parse the qualified declaration. */ | |
6900 | cp_parser_declaration (parser); | |
6901 | /* Restore the PEDANTIC flag. */ | |
6902 | pedantic = saved_pedantic; | |
6903 | ||
6904 | return; | |
6905 | } | |
a723baf1 MM |
6906 | |
6907 | /* Try to figure out what kind of declaration is present. */ | |
6908 | token1 = *cp_lexer_peek_token (parser->lexer); | |
21526606 | 6909 | |
a723baf1 MM |
6910 | if (token1.type != CPP_EOF) |
6911 | token2 = *cp_lexer_peek_nth_token (parser->lexer, 2); | |
6912 | ||
058b15c1 MM |
6913 | /* Get the high-water mark for the DECLARATOR_OBSTACK. */ |
6914 | p = obstack_alloc (&declarator_obstack, 0); | |
6915 | ||
a723baf1 MM |
6916 | /* If the next token is `extern' and the following token is a string |
6917 | literal, then we have a linkage specification. */ | |
6918 | if (token1.keyword == RID_EXTERN | |
6919 | && cp_parser_is_string_literal (&token2)) | |
6920 | cp_parser_linkage_specification (parser); | |
6921 | /* If the next token is `template', then we have either a template | |
6922 | declaration, an explicit instantiation, or an explicit | |
6923 | specialization. */ | |
6924 | else if (token1.keyword == RID_TEMPLATE) | |
6925 | { | |
6926 | /* `template <>' indicates a template specialization. */ | |
6927 | if (token2.type == CPP_LESS | |
6928 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER) | |
6929 | cp_parser_explicit_specialization (parser); | |
6930 | /* `template <' indicates a template declaration. */ | |
6931 | else if (token2.type == CPP_LESS) | |
6932 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
6933 | /* Anything else must be an explicit instantiation. */ | |
6934 | else | |
6935 | cp_parser_explicit_instantiation (parser); | |
6936 | } | |
6937 | /* If the next token is `export', then we have a template | |
6938 | declaration. */ | |
6939 | else if (token1.keyword == RID_EXPORT) | |
6940 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
6941 | /* If the next token is `extern', 'static' or 'inline' and the one | |
6942 | after that is `template', we have a GNU extended explicit | |
6943 | instantiation directive. */ | |
6944 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
6945 | && (token1.keyword == RID_EXTERN | |
6946 | || token1.keyword == RID_STATIC | |
6947 | || token1.keyword == RID_INLINE) | |
6948 | && token2.keyword == RID_TEMPLATE) | |
6949 | cp_parser_explicit_instantiation (parser); | |
6950 | /* If the next token is `namespace', check for a named or unnamed | |
6951 | namespace definition. */ | |
6952 | else if (token1.keyword == RID_NAMESPACE | |
6953 | && (/* A named namespace definition. */ | |
6954 | (token2.type == CPP_NAME | |
21526606 | 6955 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type |
a723baf1 MM |
6956 | == CPP_OPEN_BRACE)) |
6957 | /* An unnamed namespace definition. */ | |
6958 | || token2.type == CPP_OPEN_BRACE)) | |
6959 | cp_parser_namespace_definition (parser); | |
e58a9aa1 ZL |
6960 | /* Objective-C++ declaration/definition. */ |
6961 | else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword)) | |
6962 | cp_parser_objc_declaration (parser); | |
a723baf1 MM |
6963 | /* We must have either a block declaration or a function |
6964 | definition. */ | |
6965 | else | |
6966 | /* Try to parse a block-declaration, or a function-definition. */ | |
6967 | cp_parser_block_declaration (parser, /*statement_p=*/false); | |
058b15c1 MM |
6968 | |
6969 | /* Free any declarators allocated. */ | |
6970 | obstack_free (&declarator_obstack, p); | |
a723baf1 MM |
6971 | } |
6972 | ||
21526606 | 6973 | /* Parse a block-declaration. |
a723baf1 MM |
6974 | |
6975 | block-declaration: | |
6976 | simple-declaration | |
6977 | asm-definition | |
6978 | namespace-alias-definition | |
6979 | using-declaration | |
21526606 | 6980 | using-directive |
a723baf1 MM |
6981 | |
6982 | GNU Extension: | |
6983 | ||
6984 | block-declaration: | |
21526606 | 6985 | __extension__ block-declaration |
a723baf1 MM |
6986 | label-declaration |
6987 | ||
34cd5ae7 | 6988 | If STATEMENT_P is TRUE, then this block-declaration is occurring as |
a723baf1 MM |
6989 | part of a declaration-statement. */ |
6990 | ||
6991 | static void | |
21526606 | 6992 | cp_parser_block_declaration (cp_parser *parser, |
a723baf1 MM |
6993 | bool statement_p) |
6994 | { | |
6995 | cp_token *token1; | |
6996 | int saved_pedantic; | |
6997 | ||
6998 | /* Check for the `__extension__' keyword. */ | |
6999 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
7000 | { | |
7001 | /* Parse the qualified declaration. */ | |
7002 | cp_parser_block_declaration (parser, statement_p); | |
7003 | /* Restore the PEDANTIC flag. */ | |
7004 | pedantic = saved_pedantic; | |
7005 | ||
7006 | return; | |
7007 | } | |
7008 | ||
7009 | /* Peek at the next token to figure out which kind of declaration is | |
7010 | present. */ | |
7011 | token1 = cp_lexer_peek_token (parser->lexer); | |
7012 | ||
7013 | /* If the next keyword is `asm', we have an asm-definition. */ | |
7014 | if (token1->keyword == RID_ASM) | |
7015 | { | |
7016 | if (statement_p) | |
7017 | cp_parser_commit_to_tentative_parse (parser); | |
7018 | cp_parser_asm_definition (parser); | |
7019 | } | |
7020 | /* If the next keyword is `namespace', we have a | |
7021 | namespace-alias-definition. */ | |
7022 | else if (token1->keyword == RID_NAMESPACE) | |
7023 | cp_parser_namespace_alias_definition (parser); | |
7024 | /* If the next keyword is `using', we have either a | |
7025 | using-declaration or a using-directive. */ | |
7026 | else if (token1->keyword == RID_USING) | |
7027 | { | |
7028 | cp_token *token2; | |
7029 | ||
7030 | if (statement_p) | |
7031 | cp_parser_commit_to_tentative_parse (parser); | |
7032 | /* If the token after `using' is `namespace', then we have a | |
7033 | using-directive. */ | |
7034 | token2 = cp_lexer_peek_nth_token (parser->lexer, 2); | |
7035 | if (token2->keyword == RID_NAMESPACE) | |
7036 | cp_parser_using_directive (parser); | |
7037 | /* Otherwise, it's a using-declaration. */ | |
7038 | else | |
7039 | cp_parser_using_declaration (parser); | |
7040 | } | |
7041 | /* If the next keyword is `__label__' we have a label declaration. */ | |
7042 | else if (token1->keyword == RID_LABEL) | |
7043 | { | |
7044 | if (statement_p) | |
7045 | cp_parser_commit_to_tentative_parse (parser); | |
7046 | cp_parser_label_declaration (parser); | |
7047 | } | |
7048 | /* Anything else must be a simple-declaration. */ | |
7049 | else | |
7050 | cp_parser_simple_declaration (parser, !statement_p); | |
7051 | } | |
7052 | ||
7053 | /* Parse a simple-declaration. | |
7054 | ||
7055 | simple-declaration: | |
21526606 | 7056 | decl-specifier-seq [opt] init-declarator-list [opt] ; |
a723baf1 MM |
7057 | |
7058 | init-declarator-list: | |
7059 | init-declarator | |
21526606 | 7060 | init-declarator-list , init-declarator |
a723baf1 | 7061 | |
34cd5ae7 | 7062 | If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a |
9bcb9aae | 7063 | function-definition as a simple-declaration. */ |
a723baf1 MM |
7064 | |
7065 | static void | |
21526606 | 7066 | cp_parser_simple_declaration (cp_parser* parser, |
94edc4ab | 7067 | bool function_definition_allowed_p) |
a723baf1 | 7068 | { |
62d1db17 | 7069 | cp_decl_specifier_seq decl_specifiers; |
560ad596 | 7070 | int declares_class_or_enum; |
a723baf1 MM |
7071 | bool saw_declarator; |
7072 | ||
7073 | /* Defer access checks until we know what is being declared; the | |
7074 | checks for names appearing in the decl-specifier-seq should be | |
7075 | done as if we were in the scope of the thing being declared. */ | |
8d241e0b | 7076 | push_deferring_access_checks (dk_deferred); |
cf22909c | 7077 | |
a723baf1 MM |
7078 | /* Parse the decl-specifier-seq. We have to keep track of whether |
7079 | or not the decl-specifier-seq declares a named class or | |
7080 | enumeration type, since that is the only case in which the | |
21526606 | 7081 | init-declarator-list is allowed to be empty. |
a723baf1 MM |
7082 | |
7083 | [dcl.dcl] | |
7084 | ||
7085 | In a simple-declaration, the optional init-declarator-list can be | |
7086 | omitted only when declaring a class or enumeration, that is when | |
7087 | the decl-specifier-seq contains either a class-specifier, an | |
7088 | elaborated-type-specifier, or an enum-specifier. */ | |
62d1db17 MM |
7089 | cp_parser_decl_specifier_seq (parser, |
7090 | CP_PARSER_FLAGS_OPTIONAL, | |
7091 | &decl_specifiers, | |
7092 | &declares_class_or_enum); | |
a723baf1 | 7093 | /* We no longer need to defer access checks. */ |
cf22909c | 7094 | stop_deferring_access_checks (); |
24c0ef37 | 7095 | |
39703eb9 MM |
7096 | /* In a block scope, a valid declaration must always have a |
7097 | decl-specifier-seq. By not trying to parse declarators, we can | |
7098 | resolve the declaration/expression ambiguity more quickly. */ | |
98ca843c | 7099 | if (!function_definition_allowed_p |
62d1db17 | 7100 | && !decl_specifiers.any_specifiers_p) |
39703eb9 MM |
7101 | { |
7102 | cp_parser_error (parser, "expected declaration"); | |
7103 | goto done; | |
7104 | } | |
7105 | ||
8fbc5ae7 MM |
7106 | /* If the next two tokens are both identifiers, the code is |
7107 | erroneous. The usual cause of this situation is code like: | |
7108 | ||
7109 | T t; | |
7110 | ||
7111 | where "T" should name a type -- but does not. */ | |
de3fe73c MM |
7112 | if (!decl_specifiers.type |
7113 | && cp_parser_parse_and_diagnose_invalid_type_name (parser)) | |
8fbc5ae7 | 7114 | { |
8d241e0b | 7115 | /* If parsing tentatively, we should commit; we really are |
8fbc5ae7 MM |
7116 | looking at a declaration. */ |
7117 | cp_parser_commit_to_tentative_parse (parser); | |
7118 | /* Give up. */ | |
39703eb9 | 7119 | goto done; |
8fbc5ae7 | 7120 | } |
996c2b52 MM |
7121 | |
7122 | /* If we have seen at least one decl-specifier, and the next token | |
7123 | is not a parenthesis, then we must be looking at a declaration. | |
7124 | (After "int (" we might be looking at a functional cast.) */ | |
7125 | if (decl_specifiers.any_specifiers_p | |
7126 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) | |
7127 | cp_parser_commit_to_tentative_parse (parser); | |
8fbc5ae7 | 7128 | |
a723baf1 MM |
7129 | /* Keep going until we hit the `;' at the end of the simple |
7130 | declaration. */ | |
7131 | saw_declarator = false; | |
21526606 | 7132 | while (cp_lexer_next_token_is_not (parser->lexer, |
a723baf1 MM |
7133 | CPP_SEMICOLON)) |
7134 | { | |
7135 | cp_token *token; | |
7136 | bool function_definition_p; | |
560ad596 | 7137 | tree decl; |
a723baf1 MM |
7138 | |
7139 | saw_declarator = true; | |
7140 | /* Parse the init-declarator. */ | |
62d1db17 | 7141 | decl = cp_parser_init_declarator (parser, &decl_specifiers, |
560ad596 MM |
7142 | function_definition_allowed_p, |
7143 | /*member_p=*/false, | |
7144 | declares_class_or_enum, | |
7145 | &function_definition_p); | |
1fb3244a MM |
7146 | /* If an error occurred while parsing tentatively, exit quickly. |
7147 | (That usually happens when in the body of a function; each | |
7148 | statement is treated as a declaration-statement until proven | |
7149 | otherwise.) */ | |
7150 | if (cp_parser_error_occurred (parser)) | |
39703eb9 | 7151 | goto done; |
a723baf1 MM |
7152 | /* Handle function definitions specially. */ |
7153 | if (function_definition_p) | |
7154 | { | |
7155 | /* If the next token is a `,', then we are probably | |
7156 | processing something like: | |
7157 | ||
7158 | void f() {}, *p; | |
7159 | ||
7160 | which is erroneous. */ | |
7161 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
7162 | error ("mixing declarations and function-definitions is forbidden"); | |
7163 | /* Otherwise, we're done with the list of declarators. */ | |
7164 | else | |
24c0ef37 | 7165 | { |
cf22909c | 7166 | pop_deferring_access_checks (); |
24c0ef37 GS |
7167 | return; |
7168 | } | |
a723baf1 MM |
7169 | } |
7170 | /* The next token should be either a `,' or a `;'. */ | |
7171 | token = cp_lexer_peek_token (parser->lexer); | |
7172 | /* If it's a `,', there are more declarators to come. */ | |
7173 | if (token->type == CPP_COMMA) | |
7174 | cp_lexer_consume_token (parser->lexer); | |
7175 | /* If it's a `;', we are done. */ | |
7176 | else if (token->type == CPP_SEMICOLON) | |
7177 | break; | |
7178 | /* Anything else is an error. */ | |
7179 | else | |
7180 | { | |
996c2b52 MM |
7181 | /* If we have already issued an error message we don't need |
7182 | to issue another one. */ | |
7183 | if (decl != error_mark_node | |
0b16f8f4 | 7184 | || cp_parser_uncommitted_to_tentative_parse_p (parser)) |
2a13a625 | 7185 | cp_parser_error (parser, "expected %<,%> or %<;%>"); |
a723baf1 MM |
7186 | /* Skip tokens until we reach the end of the statement. */ |
7187 | cp_parser_skip_to_end_of_statement (parser); | |
5a98fa7b MM |
7188 | /* If the next token is now a `;', consume it. */ |
7189 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
7190 | cp_lexer_consume_token (parser->lexer); | |
39703eb9 | 7191 | goto done; |
a723baf1 MM |
7192 | } |
7193 | /* After the first time around, a function-definition is not | |
7194 | allowed -- even if it was OK at first. For example: | |
7195 | ||
7196 | int i, f() {} | |
7197 | ||
7198 | is not valid. */ | |
7199 | function_definition_allowed_p = false; | |
7200 | } | |
7201 | ||
7202 | /* Issue an error message if no declarators are present, and the | |
7203 | decl-specifier-seq does not itself declare a class or | |
7204 | enumeration. */ | |
7205 | if (!saw_declarator) | |
7206 | { | |
7207 | if (cp_parser_declares_only_class_p (parser)) | |
62d1db17 | 7208 | shadow_tag (&decl_specifiers); |
a723baf1 | 7209 | /* Perform any deferred access checks. */ |
cf22909c | 7210 | perform_deferred_access_checks (); |
a723baf1 MM |
7211 | } |
7212 | ||
7213 | /* Consume the `;'. */ | |
7214 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
7215 | ||
39703eb9 MM |
7216 | done: |
7217 | pop_deferring_access_checks (); | |
a723baf1 MM |
7218 | } |
7219 | ||
7220 | /* Parse a decl-specifier-seq. | |
7221 | ||
7222 | decl-specifier-seq: | |
7223 | decl-specifier-seq [opt] decl-specifier | |
7224 | ||
7225 | decl-specifier: | |
7226 | storage-class-specifier | |
7227 | type-specifier | |
7228 | function-specifier | |
7229 | friend | |
21526606 | 7230 | typedef |
a723baf1 MM |
7231 | |
7232 | GNU Extension: | |
7233 | ||
15077df5 MM |
7234 | decl-specifier: |
7235 | attributes | |
a723baf1 | 7236 | |
62d1db17 | 7237 | Set *DECL_SPECS to a representation of the decl-specifier-seq. |
a723baf1 | 7238 | |
eb1aef53 | 7239 | The parser flags FLAGS is used to control type-specifier parsing. |
560ad596 MM |
7240 | |
7241 | *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following | |
543ca912 | 7242 | flags: |
560ad596 MM |
7243 | |
7244 | 1: one of the decl-specifiers is an elaborated-type-specifier | |
543ca912 | 7245 | (i.e., a type declaration) |
560ad596 | 7246 | 2: one of the decl-specifiers is an enum-specifier or a |
543ca912 | 7247 | class-specifier (i.e., a type definition) |
98ca843c | 7248 | |
560ad596 | 7249 | */ |
a723baf1 | 7250 | |
62d1db17 | 7251 | static void |
21526606 | 7252 | cp_parser_decl_specifier_seq (cp_parser* parser, |
62d1db17 MM |
7253 | cp_parser_flags flags, |
7254 | cp_decl_specifier_seq *decl_specs, | |
560ad596 | 7255 | int* declares_class_or_enum) |
a723baf1 | 7256 | { |
f2ce60b8 | 7257 | bool constructor_possible_p = !parser->in_declarator_p; |
21526606 | 7258 | |
62d1db17 MM |
7259 | /* Clear DECL_SPECS. */ |
7260 | clear_decl_specs (decl_specs); | |
7261 | ||
a723baf1 | 7262 | /* Assume no class or enumeration type is declared. */ |
560ad596 | 7263 | *declares_class_or_enum = 0; |
a723baf1 | 7264 | |
a723baf1 MM |
7265 | /* Keep reading specifiers until there are no more to read. */ |
7266 | while (true) | |
7267 | { | |
a723baf1 | 7268 | bool constructor_p; |
62d1db17 | 7269 | bool found_decl_spec; |
a723baf1 MM |
7270 | cp_token *token; |
7271 | ||
7272 | /* Peek at the next token. */ | |
7273 | token = cp_lexer_peek_token (parser->lexer); | |
7274 | /* Handle attributes. */ | |
7275 | if (token->keyword == RID_ATTRIBUTE) | |
7276 | { | |
7277 | /* Parse the attributes. */ | |
98ca843c | 7278 | decl_specs->attributes |
62d1db17 MM |
7279 | = chainon (decl_specs->attributes, |
7280 | cp_parser_attributes_opt (parser)); | |
a723baf1 MM |
7281 | continue; |
7282 | } | |
62d1db17 MM |
7283 | /* Assume we will find a decl-specifier keyword. */ |
7284 | found_decl_spec = true; | |
a723baf1 MM |
7285 | /* If the next token is an appropriate keyword, we can simply |
7286 | add it to the list. */ | |
7287 | switch (token->keyword) | |
7288 | { | |
a723baf1 MM |
7289 | /* decl-specifier: |
7290 | friend */ | |
62d1db17 MM |
7291 | case RID_FRIEND: |
7292 | if (decl_specs->specs[(int) ds_friend]++) | |
2a13a625 | 7293 | error ("duplicate %<friend%>"); |
a723baf1 MM |
7294 | /* Consume the token. */ |
7295 | cp_lexer_consume_token (parser->lexer); | |
7296 | break; | |
7297 | ||
7298 | /* function-specifier: | |
7299 | inline | |
7300 | virtual | |
7301 | explicit */ | |
7302 | case RID_INLINE: | |
7303 | case RID_VIRTUAL: | |
7304 | case RID_EXPLICIT: | |
62d1db17 | 7305 | cp_parser_function_specifier_opt (parser, decl_specs); |
a723baf1 | 7306 | break; |
21526606 | 7307 | |
a723baf1 MM |
7308 | /* decl-specifier: |
7309 | typedef */ | |
7310 | case RID_TYPEDEF: | |
62d1db17 | 7311 | ++decl_specs->specs[(int) ds_typedef]; |
a723baf1 MM |
7312 | /* Consume the token. */ |
7313 | cp_lexer_consume_token (parser->lexer); | |
2050a1bb MM |
7314 | /* A constructor declarator cannot appear in a typedef. */ |
7315 | constructor_possible_p = false; | |
c006d942 MM |
7316 | /* The "typedef" keyword can only occur in a declaration; we |
7317 | may as well commit at this point. */ | |
7318 | cp_parser_commit_to_tentative_parse (parser); | |
a723baf1 MM |
7319 | break; |
7320 | ||
7321 | /* storage-class-specifier: | |
7322 | auto | |
7323 | register | |
7324 | static | |
7325 | extern | |
21526606 | 7326 | mutable |
a723baf1 MM |
7327 | |
7328 | GNU Extension: | |
7329 | thread */ | |
7330 | case RID_AUTO: | |
62d1db17 MM |
7331 | /* Consume the token. */ |
7332 | cp_lexer_consume_token (parser->lexer); | |
7333 | cp_parser_set_storage_class (decl_specs, sc_auto); | |
7334 | break; | |
a723baf1 | 7335 | case RID_REGISTER: |
62d1db17 MM |
7336 | /* Consume the token. */ |
7337 | cp_lexer_consume_token (parser->lexer); | |
7338 | cp_parser_set_storage_class (decl_specs, sc_register); | |
7339 | break; | |
a723baf1 | 7340 | case RID_STATIC: |
62d1db17 MM |
7341 | /* Consume the token. */ |
7342 | cp_lexer_consume_token (parser->lexer); | |
7343 | if (decl_specs->specs[(int) ds_thread]) | |
f1b90a04 | 7344 | { |
2d01edd7 | 7345 | error ("%<__thread%> before %<static%>"); |
f1b90a04 MM |
7346 | decl_specs->specs[(int) ds_thread] = 0; |
7347 | } | |
7348 | cp_parser_set_storage_class (decl_specs, sc_static); | |
62d1db17 | 7349 | break; |
a723baf1 | 7350 | case RID_EXTERN: |
62d1db17 MM |
7351 | /* Consume the token. */ |
7352 | cp_lexer_consume_token (parser->lexer); | |
7353 | if (decl_specs->specs[(int) ds_thread]) | |
f1b90a04 | 7354 | { |
2d01edd7 | 7355 | error ("%<__thread%> before %<extern%>"); |
f1b90a04 MM |
7356 | decl_specs->specs[(int) ds_thread] = 0; |
7357 | } | |
7358 | cp_parser_set_storage_class (decl_specs, sc_extern); | |
62d1db17 | 7359 | break; |
a723baf1 | 7360 | case RID_MUTABLE: |
62d1db17 MM |
7361 | /* Consume the token. */ |
7362 | cp_lexer_consume_token (parser->lexer); | |
7363 | cp_parser_set_storage_class (decl_specs, sc_mutable); | |
7364 | break; | |
a723baf1 | 7365 | case RID_THREAD: |
62d1db17 MM |
7366 | /* Consume the token. */ |
7367 | cp_lexer_consume_token (parser->lexer); | |
7368 | ++decl_specs->specs[(int) ds_thread]; | |
a723baf1 | 7369 | break; |
21526606 | 7370 | |
a723baf1 | 7371 | default: |
62d1db17 MM |
7372 | /* We did not yet find a decl-specifier yet. */ |
7373 | found_decl_spec = false; | |
a723baf1 MM |
7374 | break; |
7375 | } | |
7376 | ||
7377 | /* Constructors are a special case. The `S' in `S()' is not a | |
7378 | decl-specifier; it is the beginning of the declarator. */ | |
98ca843c | 7379 | constructor_p |
62d1db17 MM |
7380 | = (!found_decl_spec |
7381 | && constructor_possible_p | |
98ca843c | 7382 | && (cp_parser_constructor_declarator_p |
62d1db17 | 7383 | (parser, decl_specs->specs[(int) ds_friend] != 0))); |
a723baf1 MM |
7384 | |
7385 | /* If we don't have a DECL_SPEC yet, then we must be looking at | |
7386 | a type-specifier. */ | |
62d1db17 | 7387 | if (!found_decl_spec && !constructor_p) |
a723baf1 | 7388 | { |
560ad596 | 7389 | int decl_spec_declares_class_or_enum; |
a723baf1 | 7390 | bool is_cv_qualifier; |
62d1db17 | 7391 | tree type_spec; |
a723baf1 | 7392 | |
62d1db17 | 7393 | type_spec |
a723baf1 | 7394 | = cp_parser_type_specifier (parser, flags, |
62d1db17 | 7395 | decl_specs, |
a723baf1 MM |
7396 | /*is_declaration=*/true, |
7397 | &decl_spec_declares_class_or_enum, | |
7398 | &is_cv_qualifier); | |
7399 | ||
7400 | *declares_class_or_enum |= decl_spec_declares_class_or_enum; | |
7401 | ||
7402 | /* If this type-specifier referenced a user-defined type | |
7403 | (a typedef, class-name, etc.), then we can't allow any | |
7404 | more such type-specifiers henceforth. | |
7405 | ||
7406 | [dcl.spec] | |
7407 | ||
7408 | The longest sequence of decl-specifiers that could | |
7409 | possibly be a type name is taken as the | |
7410 | decl-specifier-seq of a declaration. The sequence shall | |
7411 | be self-consistent as described below. | |
7412 | ||
7413 | [dcl.type] | |
7414 | ||
7415 | As a general rule, at most one type-specifier is allowed | |
7416 | in the complete decl-specifier-seq of a declaration. The | |
7417 | only exceptions are the following: | |
7418 | ||
7419 | -- const or volatile can be combined with any other | |
21526606 | 7420 | type-specifier. |
a723baf1 MM |
7421 | |
7422 | -- signed or unsigned can be combined with char, long, | |
7423 | short, or int. | |
7424 | ||
7425 | -- .. | |
7426 | ||
7427 | Example: | |
7428 | ||
7429 | typedef char* Pc; | |
7430 | void g (const int Pc); | |
7431 | ||
7432 | Here, Pc is *not* part of the decl-specifier seq; it's | |
7433 | the declarator. Therefore, once we see a type-specifier | |
7434 | (other than a cv-qualifier), we forbid any additional | |
7435 | user-defined types. We *do* still allow things like `int | |
7436 | int' to be considered a decl-specifier-seq, and issue the | |
7437 | error message later. */ | |
62d1db17 | 7438 | if (type_spec && !is_cv_qualifier) |
a723baf1 | 7439 | flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; |
2050a1bb | 7440 | /* A constructor declarator cannot follow a type-specifier. */ |
62d1db17 | 7441 | if (type_spec) |
a723baf1 | 7442 | { |
62d1db17 MM |
7443 | constructor_possible_p = false; |
7444 | found_decl_spec = true; | |
a723baf1 | 7445 | } |
a723baf1 MM |
7446 | } |
7447 | ||
62d1db17 MM |
7448 | /* If we still do not have a DECL_SPEC, then there are no more |
7449 | decl-specifiers. */ | |
7450 | if (!found_decl_spec) | |
7451 | break; | |
a723baf1 | 7452 | |
62d1db17 | 7453 | decl_specs->any_specifiers_p = true; |
a723baf1 MM |
7454 | /* After we see one decl-specifier, further decl-specifiers are |
7455 | always optional. */ | |
7456 | flags |= CP_PARSER_FLAGS_OPTIONAL; | |
7457 | } | |
7458 | ||
0426c4ca | 7459 | /* Don't allow a friend specifier with a class definition. */ |
62d1db17 MM |
7460 | if (decl_specs->specs[(int) ds_friend] != 0 |
7461 | && (*declares_class_or_enum & 2)) | |
0426c4ca | 7462 | error ("class definition may not be declared a friend"); |
a723baf1 MM |
7463 | } |
7464 | ||
21526606 | 7465 | /* Parse an (optional) storage-class-specifier. |
a723baf1 MM |
7466 | |
7467 | storage-class-specifier: | |
7468 | auto | |
7469 | register | |
7470 | static | |
7471 | extern | |
21526606 | 7472 | mutable |
a723baf1 MM |
7473 | |
7474 | GNU Extension: | |
7475 | ||
7476 | storage-class-specifier: | |
7477 | thread | |
7478 | ||
7479 | Returns an IDENTIFIER_NODE corresponding to the keyword used. */ | |
21526606 | 7480 | |
a723baf1 | 7481 | static tree |
94edc4ab | 7482 | cp_parser_storage_class_specifier_opt (cp_parser* parser) |
a723baf1 MM |
7483 | { |
7484 | switch (cp_lexer_peek_token (parser->lexer)->keyword) | |
7485 | { | |
7486 | case RID_AUTO: | |
7487 | case RID_REGISTER: | |
7488 | case RID_STATIC: | |
7489 | case RID_EXTERN: | |
7490 | case RID_MUTABLE: | |
7491 | case RID_THREAD: | |
7492 | /* Consume the token. */ | |
7493 | return cp_lexer_consume_token (parser->lexer)->value; | |
7494 | ||
7495 | default: | |
7496 | return NULL_TREE; | |
7497 | } | |
7498 | } | |
7499 | ||
21526606 | 7500 | /* Parse an (optional) function-specifier. |
a723baf1 MM |
7501 | |
7502 | function-specifier: | |
7503 | inline | |
7504 | virtual | |
7505 | explicit | |
7506 | ||
62d1db17 MM |
7507 | Returns an IDENTIFIER_NODE corresponding to the keyword used. |
7508 | Updates DECL_SPECS, if it is non-NULL. */ | |
21526606 | 7509 | |
a723baf1 | 7510 | static tree |
62d1db17 MM |
7511 | cp_parser_function_specifier_opt (cp_parser* parser, |
7512 | cp_decl_specifier_seq *decl_specs) | |
a723baf1 MM |
7513 | { |
7514 | switch (cp_lexer_peek_token (parser->lexer)->keyword) | |
7515 | { | |
7516 | case RID_INLINE: | |
62d1db17 MM |
7517 | if (decl_specs) |
7518 | ++decl_specs->specs[(int) ds_inline]; | |
7519 | break; | |
7520 | ||
a723baf1 | 7521 | case RID_VIRTUAL: |
62d1db17 MM |
7522 | if (decl_specs) |
7523 | ++decl_specs->specs[(int) ds_virtual]; | |
7524 | break; | |
7525 | ||
a723baf1 | 7526 | case RID_EXPLICIT: |
62d1db17 MM |
7527 | if (decl_specs) |
7528 | ++decl_specs->specs[(int) ds_explicit]; | |
7529 | break; | |
a723baf1 MM |
7530 | |
7531 | default: | |
7532 | return NULL_TREE; | |
7533 | } | |
62d1db17 MM |
7534 | |
7535 | /* Consume the token. */ | |
7536 | return cp_lexer_consume_token (parser->lexer)->value; | |
a723baf1 MM |
7537 | } |
7538 | ||
7539 | /* Parse a linkage-specification. | |
7540 | ||
7541 | linkage-specification: | |
7542 | extern string-literal { declaration-seq [opt] } | |
7543 | extern string-literal declaration */ | |
7544 | ||
7545 | static void | |
94edc4ab | 7546 | cp_parser_linkage_specification (cp_parser* parser) |
a723baf1 | 7547 | { |
a723baf1 MM |
7548 | tree linkage; |
7549 | ||
7550 | /* Look for the `extern' keyword. */ | |
7551 | cp_parser_require_keyword (parser, RID_EXTERN, "`extern'"); | |
7552 | ||
c162c75e MA |
7553 | /* Look for the string-literal. */ |
7554 | linkage = cp_parser_string_literal (parser, false, false); | |
a723baf1 MM |
7555 | |
7556 | /* Transform the literal into an identifier. If the literal is a | |
7557 | wide-character string, or contains embedded NULs, then we can't | |
7558 | handle it as the user wants. */ | |
c162c75e MA |
7559 | if (strlen (TREE_STRING_POINTER (linkage)) |
7560 | != (size_t) (TREE_STRING_LENGTH (linkage) - 1)) | |
a723baf1 MM |
7561 | { |
7562 | cp_parser_error (parser, "invalid linkage-specification"); | |
7563 | /* Assume C++ linkage. */ | |
c162c75e | 7564 | linkage = lang_name_cplusplus; |
a723baf1 | 7565 | } |
a723baf1 | 7566 | else |
c162c75e | 7567 | linkage = get_identifier (TREE_STRING_POINTER (linkage)); |
a723baf1 MM |
7568 | |
7569 | /* We're now using the new linkage. */ | |
7570 | push_lang_context (linkage); | |
7571 | ||
7572 | /* If the next token is a `{', then we're using the first | |
7573 | production. */ | |
7574 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
7575 | { | |
7576 | /* Consume the `{' token. */ | |
7577 | cp_lexer_consume_token (parser->lexer); | |
7578 | /* Parse the declarations. */ | |
7579 | cp_parser_declaration_seq_opt (parser); | |
7580 | /* Look for the closing `}'. */ | |
7581 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
7582 | } | |
7583 | /* Otherwise, there's just one declaration. */ | |
7584 | else | |
7585 | { | |
7586 | bool saved_in_unbraced_linkage_specification_p; | |
7587 | ||
21526606 | 7588 | saved_in_unbraced_linkage_specification_p |
a723baf1 MM |
7589 | = parser->in_unbraced_linkage_specification_p; |
7590 | parser->in_unbraced_linkage_specification_p = true; | |
7591 | have_extern_spec = true; | |
7592 | cp_parser_declaration (parser); | |
7593 | have_extern_spec = false; | |
21526606 | 7594 | parser->in_unbraced_linkage_specification_p |
a723baf1 MM |
7595 | = saved_in_unbraced_linkage_specification_p; |
7596 | } | |
7597 | ||
7598 | /* We're done with the linkage-specification. */ | |
7599 | pop_lang_context (); | |
7600 | } | |
7601 | ||
7602 | /* Special member functions [gram.special] */ | |
7603 | ||
7604 | /* Parse a conversion-function-id. | |
7605 | ||
7606 | conversion-function-id: | |
21526606 | 7607 | operator conversion-type-id |
a723baf1 MM |
7608 | |
7609 | Returns an IDENTIFIER_NODE representing the operator. */ | |
7610 | ||
21526606 | 7611 | static tree |
94edc4ab | 7612 | cp_parser_conversion_function_id (cp_parser* parser) |
a723baf1 MM |
7613 | { |
7614 | tree type; | |
7615 | tree saved_scope; | |
7616 | tree saved_qualifying_scope; | |
7617 | tree saved_object_scope; | |
4514aa8c | 7618 | tree pushed_scope = NULL_TREE; |
a723baf1 MM |
7619 | |
7620 | /* Look for the `operator' token. */ | |
7621 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'")) | |
7622 | return error_mark_node; | |
7623 | /* When we parse the conversion-type-id, the current scope will be | |
7624 | reset. However, we need that information in able to look up the | |
7625 | conversion function later, so we save it here. */ | |
7626 | saved_scope = parser->scope; | |
7627 | saved_qualifying_scope = parser->qualifying_scope; | |
7628 | saved_object_scope = parser->object_scope; | |
7629 | /* We must enter the scope of the class so that the names of | |
7630 | entities declared within the class are available in the | |
7631 | conversion-type-id. For example, consider: | |
7632 | ||
21526606 | 7633 | struct S { |
a723baf1 MM |
7634 | typedef int I; |
7635 | operator I(); | |
7636 | }; | |
7637 | ||
7638 | S::operator I() { ... } | |
7639 | ||
7640 | In order to see that `I' is a type-name in the definition, we | |
7641 | must be in the scope of `S'. */ | |
7642 | if (saved_scope) | |
4514aa8c | 7643 | pushed_scope = push_scope (saved_scope); |
a723baf1 MM |
7644 | /* Parse the conversion-type-id. */ |
7645 | type = cp_parser_conversion_type_id (parser); | |
7646 | /* Leave the scope of the class, if any. */ | |
4514aa8c NS |
7647 | if (pushed_scope) |
7648 | pop_scope (pushed_scope); | |
a723baf1 MM |
7649 | /* Restore the saved scope. */ |
7650 | parser->scope = saved_scope; | |
7651 | parser->qualifying_scope = saved_qualifying_scope; | |
7652 | parser->object_scope = saved_object_scope; | |
7653 | /* If the TYPE is invalid, indicate failure. */ | |
7654 | if (type == error_mark_node) | |
7655 | return error_mark_node; | |
7656 | return mangle_conv_op_name_for_type (type); | |
7657 | } | |
7658 | ||
7659 | /* Parse a conversion-type-id: | |
7660 | ||
7661 | conversion-type-id: | |
7662 | type-specifier-seq conversion-declarator [opt] | |
7663 | ||
7664 | Returns the TYPE specified. */ | |
7665 | ||
7666 | static tree | |
94edc4ab | 7667 | cp_parser_conversion_type_id (cp_parser* parser) |
a723baf1 MM |
7668 | { |
7669 | tree attributes; | |
62d1db17 | 7670 | cp_decl_specifier_seq type_specifiers; |
058b15c1 | 7671 | cp_declarator *declarator; |
037cc9c5 | 7672 | tree type_specified; |
a723baf1 MM |
7673 | |
7674 | /* Parse the attributes. */ | |
7675 | attributes = cp_parser_attributes_opt (parser); | |
7676 | /* Parse the type-specifiers. */ | |
d4113656 MM |
7677 | cp_parser_type_specifier_seq (parser, /*is_condition=*/false, |
7678 | &type_specifiers); | |
a723baf1 | 7679 | /* If that didn't work, stop. */ |
62d1db17 | 7680 | if (type_specifiers.type == error_mark_node) |
a723baf1 MM |
7681 | return error_mark_node; |
7682 | /* Parse the conversion-declarator. */ | |
7683 | declarator = cp_parser_conversion_declarator_opt (parser); | |
7684 | ||
037cc9c5 FJ |
7685 | type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME, |
7686 | /*initialized=*/0, &attributes); | |
7687 | if (attributes) | |
7688 | cplus_decl_attributes (&type_specified, attributes, /*flags=*/0); | |
7689 | return type_specified; | |
a723baf1 MM |
7690 | } |
7691 | ||
7692 | /* Parse an (optional) conversion-declarator. | |
7693 | ||
7694 | conversion-declarator: | |
21526606 | 7695 | ptr-operator conversion-declarator [opt] |
a723baf1 | 7696 | |
058b15c1 | 7697 | */ |
a723baf1 | 7698 | |
058b15c1 | 7699 | static cp_declarator * |
94edc4ab | 7700 | cp_parser_conversion_declarator_opt (cp_parser* parser) |
a723baf1 MM |
7701 | { |
7702 | enum tree_code code; | |
7703 | tree class_type; | |
3c01e5df | 7704 | cp_cv_quals cv_quals; |
a723baf1 MM |
7705 | |
7706 | /* We don't know if there's a ptr-operator next, or not. */ | |
7707 | cp_parser_parse_tentatively (parser); | |
7708 | /* Try the ptr-operator. */ | |
3c01e5df | 7709 | code = cp_parser_ptr_operator (parser, &class_type, &cv_quals); |
a723baf1 MM |
7710 | /* If it worked, look for more conversion-declarators. */ |
7711 | if (cp_parser_parse_definitely (parser)) | |
7712 | { | |
058b15c1 | 7713 | cp_declarator *declarator; |
98ca843c | 7714 | |
058b15c1 MM |
7715 | /* Parse another optional declarator. */ |
7716 | declarator = cp_parser_conversion_declarator_opt (parser); | |
98ca843c | 7717 | |
058b15c1 MM |
7718 | /* Create the representation of the declarator. */ |
7719 | if (class_type) | |
3c01e5df | 7720 | declarator = make_ptrmem_declarator (cv_quals, class_type, |
a723baf1 | 7721 | declarator); |
058b15c1 | 7722 | else if (code == INDIRECT_REF) |
3c01e5df | 7723 | declarator = make_pointer_declarator (cv_quals, declarator); |
058b15c1 | 7724 | else |
3c01e5df | 7725 | declarator = make_reference_declarator (cv_quals, declarator); |
98ca843c | 7726 | |
058b15c1 | 7727 | return declarator; |
a723baf1 MM |
7728 | } |
7729 | ||
058b15c1 | 7730 | return NULL; |
a723baf1 MM |
7731 | } |
7732 | ||
7733 | /* Parse an (optional) ctor-initializer. | |
7734 | ||
7735 | ctor-initializer: | |
21526606 | 7736 | : mem-initializer-list |
a723baf1 MM |
7737 | |
7738 | Returns TRUE iff the ctor-initializer was actually present. */ | |
7739 | ||
7740 | static bool | |
94edc4ab | 7741 | cp_parser_ctor_initializer_opt (cp_parser* parser) |
a723baf1 MM |
7742 | { |
7743 | /* If the next token is not a `:', then there is no | |
7744 | ctor-initializer. */ | |
7745 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) | |
7746 | { | |
7747 | /* Do default initialization of any bases and members. */ | |
7748 | if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
7749 | finish_mem_initializers (NULL_TREE); | |
7750 | ||
7751 | return false; | |
7752 | } | |
7753 | ||
7754 | /* Consume the `:' token. */ | |
7755 | cp_lexer_consume_token (parser->lexer); | |
7756 | /* And the mem-initializer-list. */ | |
7757 | cp_parser_mem_initializer_list (parser); | |
7758 | ||
7759 | return true; | |
7760 | } | |
7761 | ||
7762 | /* Parse a mem-initializer-list. | |
7763 | ||
7764 | mem-initializer-list: | |
7765 | mem-initializer | |
7766 | mem-initializer , mem-initializer-list */ | |
7767 | ||
7768 | static void | |
94edc4ab | 7769 | cp_parser_mem_initializer_list (cp_parser* parser) |
a723baf1 MM |
7770 | { |
7771 | tree mem_initializer_list = NULL_TREE; | |
7772 | ||
7773 | /* Let the semantic analysis code know that we are starting the | |
7774 | mem-initializer-list. */ | |
0e136342 MM |
7775 | if (!DECL_CONSTRUCTOR_P (current_function_decl)) |
7776 | error ("only constructors take base initializers"); | |
a723baf1 MM |
7777 | |
7778 | /* Loop through the list. */ | |
7779 | while (true) | |
7780 | { | |
7781 | tree mem_initializer; | |
7782 | ||
7783 | /* Parse the mem-initializer. */ | |
7784 | mem_initializer = cp_parser_mem_initializer (parser); | |
7785 | /* Add it to the list, unless it was erroneous. */ | |
7786 | if (mem_initializer) | |
7787 | { | |
7788 | TREE_CHAIN (mem_initializer) = mem_initializer_list; | |
7789 | mem_initializer_list = mem_initializer; | |
7790 | } | |
7791 | /* If the next token is not a `,', we're done. */ | |
7792 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
7793 | break; | |
7794 | /* Consume the `,' token. */ | |
7795 | cp_lexer_consume_token (parser->lexer); | |
7796 | } | |
7797 | ||
7798 | /* Perform semantic analysis. */ | |
0e136342 MM |
7799 | if (DECL_CONSTRUCTOR_P (current_function_decl)) |
7800 | finish_mem_initializers (mem_initializer_list); | |
a723baf1 MM |
7801 | } |
7802 | ||
7803 | /* Parse a mem-initializer. | |
7804 | ||
7805 | mem-initializer: | |
21526606 | 7806 | mem-initializer-id ( expression-list [opt] ) |
a723baf1 MM |
7807 | |
7808 | GNU extension: | |
21526606 | 7809 | |
a723baf1 | 7810 | mem-initializer: |
34cd5ae7 | 7811 | ( expression-list [opt] ) |
a723baf1 MM |
7812 | |
7813 | Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base | |
7814 | class) or FIELD_DECL (for a non-static data member) to initialize; | |
7815 | the TREE_VALUE is the expression-list. */ | |
7816 | ||
7817 | static tree | |
94edc4ab | 7818 | cp_parser_mem_initializer (cp_parser* parser) |
a723baf1 MM |
7819 | { |
7820 | tree mem_initializer_id; | |
7821 | tree expression_list; | |
1f5a253a | 7822 | tree member; |
21526606 | 7823 | |
a723baf1 MM |
7824 | /* Find out what is being initialized. */ |
7825 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
7826 | { | |
7827 | pedwarn ("anachronistic old-style base class initializer"); | |
7828 | mem_initializer_id = NULL_TREE; | |
7829 | } | |
7830 | else | |
7831 | mem_initializer_id = cp_parser_mem_initializer_id (parser); | |
1f5a253a NS |
7832 | member = expand_member_init (mem_initializer_id); |
7833 | if (member && !DECL_P (member)) | |
7834 | in_base_initializer = 1; | |
7efa3e22 | 7835 | |
21526606 | 7836 | expression_list |
39703eb9 | 7837 | = cp_parser_parenthesized_expression_list (parser, false, |
93678513 | 7838 | /*cast_p=*/false, |
39703eb9 | 7839 | /*non_constant_p=*/NULL); |
7efa3e22 | 7840 | if (!expression_list) |
a723baf1 | 7841 | expression_list = void_type_node; |
a723baf1 | 7842 | |
1f5a253a | 7843 | in_base_initializer = 0; |
21526606 | 7844 | |
1f5a253a | 7845 | return member ? build_tree_list (member, expression_list) : NULL_TREE; |
a723baf1 MM |
7846 | } |
7847 | ||
7848 | /* Parse a mem-initializer-id. | |
7849 | ||
7850 | mem-initializer-id: | |
7851 | :: [opt] nested-name-specifier [opt] class-name | |
21526606 | 7852 | identifier |
a723baf1 MM |
7853 | |
7854 | Returns a TYPE indicating the class to be initializer for the first | |
7855 | production. Returns an IDENTIFIER_NODE indicating the data member | |
7856 | to be initialized for the second production. */ | |
7857 | ||
7858 | static tree | |
94edc4ab | 7859 | cp_parser_mem_initializer_id (cp_parser* parser) |
a723baf1 MM |
7860 | { |
7861 | bool global_scope_p; | |
7862 | bool nested_name_specifier_p; | |
8a83a693 | 7863 | bool template_p = false; |
a723baf1 MM |
7864 | tree id; |
7865 | ||
8a83a693 GB |
7866 | /* `typename' is not allowed in this context ([temp.res]). */ |
7867 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME)) | |
7868 | { | |
2a13a625 | 7869 | error ("keyword %<typename%> not allowed in this context (a qualified " |
8a83a693 GB |
7870 | "member initializer is implicitly a type)"); |
7871 | cp_lexer_consume_token (parser->lexer); | |
7872 | } | |
a723baf1 | 7873 | /* Look for the optional `::' operator. */ |
21526606 EC |
7874 | global_scope_p |
7875 | = (cp_parser_global_scope_opt (parser, | |
7876 | /*current_scope_valid_p=*/false) | |
a723baf1 MM |
7877 | != NULL_TREE); |
7878 | /* Look for the optional nested-name-specifier. The simplest way to | |
7879 | implement: | |
7880 | ||
7881 | [temp.res] | |
7882 | ||
7883 | The keyword `typename' is not permitted in a base-specifier or | |
7884 | mem-initializer; in these contexts a qualified name that | |
7885 | depends on a template-parameter is implicitly assumed to be a | |
7886 | type name. | |
7887 | ||
7888 | is to assume that we have seen the `typename' keyword at this | |
7889 | point. */ | |
21526606 | 7890 | nested_name_specifier_p |
a723baf1 MM |
7891 | = (cp_parser_nested_name_specifier_opt (parser, |
7892 | /*typename_keyword_p=*/true, | |
7893 | /*check_dependency_p=*/true, | |
a668c6ad MM |
7894 | /*type_p=*/true, |
7895 | /*is_declaration=*/true) | |
a723baf1 | 7896 | != NULL_TREE); |
8a83a693 GB |
7897 | if (nested_name_specifier_p) |
7898 | template_p = cp_parser_optional_template_keyword (parser); | |
a723baf1 MM |
7899 | /* If there is a `::' operator or a nested-name-specifier, then we |
7900 | are definitely looking for a class-name. */ | |
7901 | if (global_scope_p || nested_name_specifier_p) | |
7902 | return cp_parser_class_name (parser, | |
7903 | /*typename_keyword_p=*/true, | |
8a83a693 | 7904 | /*template_keyword_p=*/template_p, |
fc6a28d7 | 7905 | none_type, |
a723baf1 | 7906 | /*check_dependency_p=*/true, |
a668c6ad MM |
7907 | /*class_head_p=*/false, |
7908 | /*is_declaration=*/true); | |
a723baf1 MM |
7909 | /* Otherwise, we could also be looking for an ordinary identifier. */ |
7910 | cp_parser_parse_tentatively (parser); | |
7911 | /* Try a class-name. */ | |
21526606 | 7912 | id = cp_parser_class_name (parser, |
a723baf1 MM |
7913 | /*typename_keyword_p=*/true, |
7914 | /*template_keyword_p=*/false, | |
fc6a28d7 | 7915 | none_type, |
a723baf1 | 7916 | /*check_dependency_p=*/true, |
a668c6ad MM |
7917 | /*class_head_p=*/false, |
7918 | /*is_declaration=*/true); | |
a723baf1 MM |
7919 | /* If we found one, we're done. */ |
7920 | if (cp_parser_parse_definitely (parser)) | |
7921 | return id; | |
7922 | /* Otherwise, look for an ordinary identifier. */ | |
7923 | return cp_parser_identifier (parser); | |
7924 | } | |
7925 | ||
7926 | /* Overloading [gram.over] */ | |
7927 | ||
7928 | /* Parse an operator-function-id. | |
7929 | ||
7930 | operator-function-id: | |
21526606 | 7931 | operator operator |
a723baf1 MM |
7932 | |
7933 | Returns an IDENTIFIER_NODE for the operator which is a | |
7934 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
7935 | ||
21526606 | 7936 | static tree |
94edc4ab | 7937 | cp_parser_operator_function_id (cp_parser* parser) |
a723baf1 MM |
7938 | { |
7939 | /* Look for the `operator' keyword. */ | |
7940 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'")) | |
7941 | return error_mark_node; | |
7942 | /* And then the name of the operator itself. */ | |
7943 | return cp_parser_operator (parser); | |
7944 | } | |
7945 | ||
7946 | /* Parse an operator. | |
7947 | ||
7948 | operator: | |
7949 | new delete new[] delete[] + - * / % ^ & | ~ ! = < > | |
7950 | += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && | |
7951 | || ++ -- , ->* -> () [] | |
7952 | ||
7953 | GNU Extensions: | |
21526606 | 7954 | |
a723baf1 MM |
7955 | operator: |
7956 | <? >? <?= >?= | |
7957 | ||
7958 | Returns an IDENTIFIER_NODE for the operator which is a | |
7959 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
21526606 | 7960 | |
a723baf1 | 7961 | static tree |
94edc4ab | 7962 | cp_parser_operator (cp_parser* parser) |
a723baf1 MM |
7963 | { |
7964 | tree id = NULL_TREE; | |
7965 | cp_token *token; | |
7966 | ||
7967 | /* Peek at the next token. */ | |
7968 | token = cp_lexer_peek_token (parser->lexer); | |
7969 | /* Figure out which operator we have. */ | |
7970 | switch (token->type) | |
7971 | { | |
7972 | case CPP_KEYWORD: | |
7973 | { | |
7974 | enum tree_code op; | |
7975 | ||
7976 | /* The keyword should be either `new' or `delete'. */ | |
7977 | if (token->keyword == RID_NEW) | |
7978 | op = NEW_EXPR; | |
7979 | else if (token->keyword == RID_DELETE) | |
7980 | op = DELETE_EXPR; | |
7981 | else | |
7982 | break; | |
7983 | ||
7984 | /* Consume the `new' or `delete' token. */ | |
7985 | cp_lexer_consume_token (parser->lexer); | |
7986 | ||
7987 | /* Peek at the next token. */ | |
7988 | token = cp_lexer_peek_token (parser->lexer); | |
7989 | /* If it's a `[' token then this is the array variant of the | |
7990 | operator. */ | |
7991 | if (token->type == CPP_OPEN_SQUARE) | |
7992 | { | |
7993 | /* Consume the `[' token. */ | |
7994 | cp_lexer_consume_token (parser->lexer); | |
7995 | /* Look for the `]' token. */ | |
7996 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
21526606 | 7997 | id = ansi_opname (op == NEW_EXPR |
a723baf1 MM |
7998 | ? VEC_NEW_EXPR : VEC_DELETE_EXPR); |
7999 | } | |
8000 | /* Otherwise, we have the non-array variant. */ | |
8001 | else | |
8002 | id = ansi_opname (op); | |
8003 | ||
8004 | return id; | |
8005 | } | |
8006 | ||
8007 | case CPP_PLUS: | |
8008 | id = ansi_opname (PLUS_EXPR); | |
8009 | break; | |
8010 | ||
8011 | case CPP_MINUS: | |
8012 | id = ansi_opname (MINUS_EXPR); | |
8013 | break; | |
8014 | ||
8015 | case CPP_MULT: | |
8016 | id = ansi_opname (MULT_EXPR); | |
8017 | break; | |
8018 | ||
8019 | case CPP_DIV: | |
8020 | id = ansi_opname (TRUNC_DIV_EXPR); | |
8021 | break; | |
8022 | ||
8023 | case CPP_MOD: | |
8024 | id = ansi_opname (TRUNC_MOD_EXPR); | |
8025 | break; | |
8026 | ||
8027 | case CPP_XOR: | |
8028 | id = ansi_opname (BIT_XOR_EXPR); | |
8029 | break; | |
8030 | ||
8031 | case CPP_AND: | |
8032 | id = ansi_opname (BIT_AND_EXPR); | |
8033 | break; | |
8034 | ||
8035 | case CPP_OR: | |
8036 | id = ansi_opname (BIT_IOR_EXPR); | |
8037 | break; | |
8038 | ||
8039 | case CPP_COMPL: | |
8040 | id = ansi_opname (BIT_NOT_EXPR); | |
8041 | break; | |
21526606 | 8042 | |
a723baf1 MM |
8043 | case CPP_NOT: |
8044 | id = ansi_opname (TRUTH_NOT_EXPR); | |
8045 | break; | |
8046 | ||
8047 | case CPP_EQ: | |
8048 | id = ansi_assopname (NOP_EXPR); | |
8049 | break; | |
8050 | ||
8051 | case CPP_LESS: | |
8052 | id = ansi_opname (LT_EXPR); | |
8053 | break; | |
8054 | ||
8055 | case CPP_GREATER: | |
8056 | id = ansi_opname (GT_EXPR); | |
8057 | break; | |
8058 | ||
8059 | case CPP_PLUS_EQ: | |
8060 | id = ansi_assopname (PLUS_EXPR); | |
8061 | break; | |
8062 | ||
8063 | case CPP_MINUS_EQ: | |
8064 | id = ansi_assopname (MINUS_EXPR); | |
8065 | break; | |
8066 | ||
8067 | case CPP_MULT_EQ: | |
8068 | id = ansi_assopname (MULT_EXPR); | |
8069 | break; | |
8070 | ||
8071 | case CPP_DIV_EQ: | |
8072 | id = ansi_assopname (TRUNC_DIV_EXPR); | |
8073 | break; | |
8074 | ||
8075 | case CPP_MOD_EQ: | |
8076 | id = ansi_assopname (TRUNC_MOD_EXPR); | |
8077 | break; | |
8078 | ||
8079 | case CPP_XOR_EQ: | |
8080 | id = ansi_assopname (BIT_XOR_EXPR); | |
8081 | break; | |
8082 | ||
8083 | case CPP_AND_EQ: | |
8084 | id = ansi_assopname (BIT_AND_EXPR); | |
8085 | break; | |
8086 | ||
8087 | case CPP_OR_EQ: | |
8088 | id = ansi_assopname (BIT_IOR_EXPR); | |
8089 | break; | |
8090 | ||
8091 | case CPP_LSHIFT: | |
8092 | id = ansi_opname (LSHIFT_EXPR); | |
8093 | break; | |
8094 | ||
8095 | case CPP_RSHIFT: | |
8096 | id = ansi_opname (RSHIFT_EXPR); | |
8097 | break; | |
8098 | ||
8099 | case CPP_LSHIFT_EQ: | |
8100 | id = ansi_assopname (LSHIFT_EXPR); | |
8101 | break; | |
8102 | ||
8103 | case CPP_RSHIFT_EQ: | |
8104 | id = ansi_assopname (RSHIFT_EXPR); | |
8105 | break; | |
8106 | ||
8107 | case CPP_EQ_EQ: | |
8108 | id = ansi_opname (EQ_EXPR); | |
8109 | break; | |
8110 | ||
8111 | case CPP_NOT_EQ: | |
8112 | id = ansi_opname (NE_EXPR); | |
8113 | break; | |
8114 | ||
8115 | case CPP_LESS_EQ: | |
8116 | id = ansi_opname (LE_EXPR); | |
8117 | break; | |
8118 | ||
8119 | case CPP_GREATER_EQ: | |
8120 | id = ansi_opname (GE_EXPR); | |
8121 | break; | |
8122 | ||
8123 | case CPP_AND_AND: | |
8124 | id = ansi_opname (TRUTH_ANDIF_EXPR); | |
8125 | break; | |
8126 | ||
8127 | case CPP_OR_OR: | |
8128 | id = ansi_opname (TRUTH_ORIF_EXPR); | |
8129 | break; | |
21526606 | 8130 | |
a723baf1 MM |
8131 | case CPP_PLUS_PLUS: |
8132 | id = ansi_opname (POSTINCREMENT_EXPR); | |
8133 | break; | |
8134 | ||
8135 | case CPP_MINUS_MINUS: | |
8136 | id = ansi_opname (PREDECREMENT_EXPR); | |
8137 | break; | |
8138 | ||
8139 | case CPP_COMMA: | |
8140 | id = ansi_opname (COMPOUND_EXPR); | |
8141 | break; | |
8142 | ||
8143 | case CPP_DEREF_STAR: | |
8144 | id = ansi_opname (MEMBER_REF); | |
8145 | break; | |
8146 | ||
8147 | case CPP_DEREF: | |
8148 | id = ansi_opname (COMPONENT_REF); | |
8149 | break; | |
8150 | ||
8151 | case CPP_OPEN_PAREN: | |
8152 | /* Consume the `('. */ | |
8153 | cp_lexer_consume_token (parser->lexer); | |
8154 | /* Look for the matching `)'. */ | |
8155 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
8156 | return ansi_opname (CALL_EXPR); | |
8157 | ||
8158 | case CPP_OPEN_SQUARE: | |
8159 | /* Consume the `['. */ | |
8160 | cp_lexer_consume_token (parser->lexer); | |
8161 | /* Look for the matching `]'. */ | |
8162 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
8163 | return ansi_opname (ARRAY_REF); | |
8164 | ||
8165 | /* Extensions. */ | |
8166 | case CPP_MIN: | |
8167 | id = ansi_opname (MIN_EXPR); | |
8ff24a79 | 8168 | cp_parser_warn_min_max (); |
a723baf1 MM |
8169 | break; |
8170 | ||
8171 | case CPP_MAX: | |
8172 | id = ansi_opname (MAX_EXPR); | |
8ff24a79 | 8173 | cp_parser_warn_min_max (); |
a723baf1 MM |
8174 | break; |
8175 | ||
8176 | case CPP_MIN_EQ: | |
8177 | id = ansi_assopname (MIN_EXPR); | |
8ff24a79 | 8178 | cp_parser_warn_min_max (); |
a723baf1 MM |
8179 | break; |
8180 | ||
8181 | case CPP_MAX_EQ: | |
8182 | id = ansi_assopname (MAX_EXPR); | |
8ff24a79 | 8183 | cp_parser_warn_min_max (); |
a723baf1 MM |
8184 | break; |
8185 | ||
8186 | default: | |
8187 | /* Anything else is an error. */ | |
8188 | break; | |
8189 | } | |
8190 | ||
8191 | /* If we have selected an identifier, we need to consume the | |
8192 | operator token. */ | |
8193 | if (id) | |
8194 | cp_lexer_consume_token (parser->lexer); | |
8195 | /* Otherwise, no valid operator name was present. */ | |
8196 | else | |
8197 | { | |
8198 | cp_parser_error (parser, "expected operator"); | |
8199 | id = error_mark_node; | |
8200 | } | |
8201 | ||
8202 | return id; | |
8203 | } | |
8204 | ||
8205 | /* Parse a template-declaration. | |
8206 | ||
8207 | template-declaration: | |
21526606 | 8208 | export [opt] template < template-parameter-list > declaration |
a723baf1 MM |
8209 | |
8210 | If MEMBER_P is TRUE, this template-declaration occurs within a | |
21526606 | 8211 | class-specifier. |
a723baf1 MM |
8212 | |
8213 | The grammar rule given by the standard isn't correct. What | |
8214 | is really meant is: | |
8215 | ||
8216 | template-declaration: | |
21526606 | 8217 | export [opt] template-parameter-list-seq |
a723baf1 | 8218 | decl-specifier-seq [opt] init-declarator [opt] ; |
21526606 | 8219 | export [opt] template-parameter-list-seq |
a723baf1 MM |
8220 | function-definition |
8221 | ||
8222 | template-parameter-list-seq: | |
8223 | template-parameter-list-seq [opt] | |
8224 | template < template-parameter-list > */ | |
8225 | ||
8226 | static void | |
94edc4ab | 8227 | cp_parser_template_declaration (cp_parser* parser, bool member_p) |
a723baf1 MM |
8228 | { |
8229 | /* Check for `export'. */ | |
8230 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT)) | |
8231 | { | |
8232 | /* Consume the `export' token. */ | |
8233 | cp_lexer_consume_token (parser->lexer); | |
8234 | /* Warn that we do not support `export'. */ | |
d4ee4d25 | 8235 | warning (0, "keyword %<export%> not implemented, and will be ignored"); |
a723baf1 MM |
8236 | } |
8237 | ||
8238 | cp_parser_template_declaration_after_export (parser, member_p); | |
8239 | } | |
8240 | ||
8241 | /* Parse a template-parameter-list. | |
8242 | ||
8243 | template-parameter-list: | |
8244 | template-parameter | |
8245 | template-parameter-list , template-parameter | |
8246 | ||
8247 | Returns a TREE_LIST. Each node represents a template parameter. | |
8248 | The nodes are connected via their TREE_CHAINs. */ | |
8249 | ||
8250 | static tree | |
94edc4ab | 8251 | cp_parser_template_parameter_list (cp_parser* parser) |
a723baf1 MM |
8252 | { |
8253 | tree parameter_list = NULL_TREE; | |
8254 | ||
8255 | while (true) | |
8256 | { | |
8257 | tree parameter; | |
8258 | cp_token *token; | |
058b15c1 | 8259 | bool is_non_type; |
a723baf1 MM |
8260 | |
8261 | /* Parse the template-parameter. */ | |
058b15c1 | 8262 | parameter = cp_parser_template_parameter (parser, &is_non_type); |
a723baf1 | 8263 | /* Add it to the list. */ |
943e3ede MM |
8264 | if (parameter != error_mark_node) |
8265 | parameter_list = process_template_parm (parameter_list, | |
8266 | parameter, | |
8267 | is_non_type); | |
a723baf1 MM |
8268 | /* Peek at the next token. */ |
8269 | token = cp_lexer_peek_token (parser->lexer); | |
8270 | /* If it's not a `,', we're done. */ | |
8271 | if (token->type != CPP_COMMA) | |
8272 | break; | |
8273 | /* Otherwise, consume the `,' token. */ | |
8274 | cp_lexer_consume_token (parser->lexer); | |
8275 | } | |
8276 | ||
8277 | return parameter_list; | |
8278 | } | |
8279 | ||
8280 | /* Parse a template-parameter. | |
8281 | ||
8282 | template-parameter: | |
8283 | type-parameter | |
8284 | parameter-declaration | |
8285 | ||
943e3ede MM |
8286 | If all goes well, returns a TREE_LIST. The TREE_VALUE represents |
8287 | the parameter. The TREE_PURPOSE is the default value, if any. | |
8288 | Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true | |
8289 | iff this parameter is a non-type parameter. */ | |
a723baf1 MM |
8290 | |
8291 | static tree | |
058b15c1 | 8292 | cp_parser_template_parameter (cp_parser* parser, bool *is_non_type) |
a723baf1 MM |
8293 | { |
8294 | cp_token *token; | |
62d1db17 | 8295 | cp_parameter_declarator *parameter_declarator; |
943e3ede | 8296 | tree parm; |
a723baf1 | 8297 | |
058b15c1 MM |
8298 | /* Assume it is a type parameter or a template parameter. */ |
8299 | *is_non_type = false; | |
a723baf1 MM |
8300 | /* Peek at the next token. */ |
8301 | token = cp_lexer_peek_token (parser->lexer); | |
8302 | /* If it is `class' or `template', we have a type-parameter. */ | |
8303 | if (token->keyword == RID_TEMPLATE) | |
8304 | return cp_parser_type_parameter (parser); | |
8305 | /* If it is `class' or `typename' we do not know yet whether it is a | |
8306 | type parameter or a non-type parameter. Consider: | |
8307 | ||
8308 | template <typename T, typename T::X X> ... | |
8309 | ||
8310 | or: | |
21526606 | 8311 | |
a723baf1 MM |
8312 | template <class C, class D*> ... |
8313 | ||
8314 | Here, the first parameter is a type parameter, and the second is | |
8315 | a non-type parameter. We can tell by looking at the token after | |
8316 | the identifier -- if it is a `,', `=', or `>' then we have a type | |
8317 | parameter. */ | |
8318 | if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS) | |
8319 | { | |
8320 | /* Peek at the token after `class' or `typename'. */ | |
8321 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
8322 | /* If it's an identifier, skip it. */ | |
8323 | if (token->type == CPP_NAME) | |
8324 | token = cp_lexer_peek_nth_token (parser->lexer, 3); | |
8325 | /* Now, see if the token looks like the end of a template | |
8326 | parameter. */ | |
21526606 | 8327 | if (token->type == CPP_COMMA |
a723baf1 MM |
8328 | || token->type == CPP_EQ |
8329 | || token->type == CPP_GREATER) | |
8330 | return cp_parser_type_parameter (parser); | |
8331 | } | |
8332 | ||
21526606 | 8333 | /* Otherwise, it is a non-type parameter. |
a723baf1 MM |
8334 | |
8335 | [temp.param] | |
8336 | ||
8337 | When parsing a default template-argument for a non-type | |
8338 | template-parameter, the first non-nested `>' is taken as the end | |
8339 | of the template parameter-list rather than a greater-than | |
8340 | operator. */ | |
058b15c1 MM |
8341 | *is_non_type = true; |
8342 | parameter_declarator | |
8343 | = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true, | |
8344 | /*parenthesized_p=*/NULL); | |
943e3ede MM |
8345 | parm = grokdeclarator (parameter_declarator->declarator, |
8346 | ¶meter_declarator->decl_specifiers, | |
8347 | PARM, /*initialized=*/0, | |
8348 | /*attrlist=*/NULL); | |
8349 | if (parm == error_mark_node) | |
8350 | return error_mark_node; | |
8351 | return build_tree_list (parameter_declarator->default_argument, parm); | |
a723baf1 MM |
8352 | } |
8353 | ||
8354 | /* Parse a type-parameter. | |
8355 | ||
8356 | type-parameter: | |
8357 | class identifier [opt] | |
8358 | class identifier [opt] = type-id | |
8359 | typename identifier [opt] | |
8360 | typename identifier [opt] = type-id | |
8361 | template < template-parameter-list > class identifier [opt] | |
21526606 EC |
8362 | template < template-parameter-list > class identifier [opt] |
8363 | = id-expression | |
a723baf1 MM |
8364 | |
8365 | Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The | |
8366 | TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is | |
8367 | the declaration of the parameter. */ | |
8368 | ||
8369 | static tree | |
94edc4ab | 8370 | cp_parser_type_parameter (cp_parser* parser) |
a723baf1 MM |
8371 | { |
8372 | cp_token *token; | |
8373 | tree parameter; | |
8374 | ||
8375 | /* Look for a keyword to tell us what kind of parameter this is. */ | |
21526606 | 8376 | token = cp_parser_require (parser, CPP_KEYWORD, |
8a6393df | 8377 | "`class', `typename', or `template'"); |
a723baf1 MM |
8378 | if (!token) |
8379 | return error_mark_node; | |
8380 | ||
8381 | switch (token->keyword) | |
8382 | { | |
8383 | case RID_CLASS: | |
8384 | case RID_TYPENAME: | |
8385 | { | |
8386 | tree identifier; | |
8387 | tree default_argument; | |
8388 | ||
8389 | /* If the next token is an identifier, then it names the | |
8390 | parameter. */ | |
8391 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
8392 | identifier = cp_parser_identifier (parser); | |
8393 | else | |
8394 | identifier = NULL_TREE; | |
8395 | ||
8396 | /* Create the parameter. */ | |
8397 | parameter = finish_template_type_parm (class_type_node, identifier); | |
8398 | ||
8399 | /* If the next token is an `=', we have a default argument. */ | |
8400 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
8401 | { | |
8402 | /* Consume the `=' token. */ | |
8403 | cp_lexer_consume_token (parser->lexer); | |
34cd5ae7 | 8404 | /* Parse the default-argument. */ |
a723baf1 MM |
8405 | default_argument = cp_parser_type_id (parser); |
8406 | } | |
8407 | else | |
8408 | default_argument = NULL_TREE; | |
8409 | ||
8410 | /* Create the combined representation of the parameter and the | |
8411 | default argument. */ | |
c67d36d0 | 8412 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
8413 | } |
8414 | break; | |
8415 | ||
8416 | case RID_TEMPLATE: | |
8417 | { | |
8418 | tree parameter_list; | |
8419 | tree identifier; | |
8420 | tree default_argument; | |
8421 | ||
8422 | /* Look for the `<'. */ | |
8423 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
8424 | /* Parse the template-parameter-list. */ | |
8425 | begin_template_parm_list (); | |
21526606 | 8426 | parameter_list |
a723baf1 MM |
8427 | = cp_parser_template_parameter_list (parser); |
8428 | parameter_list = end_template_parm_list (parameter_list); | |
8429 | /* Look for the `>'. */ | |
8430 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
8431 | /* Look for the `class' keyword. */ | |
8432 | cp_parser_require_keyword (parser, RID_CLASS, "`class'"); | |
8433 | /* If the next token is an `=', then there is a | |
8434 | default-argument. If the next token is a `>', we are at | |
8435 | the end of the parameter-list. If the next token is a `,', | |
8436 | then we are at the end of this parameter. */ | |
8437 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ) | |
8438 | && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER) | |
8439 | && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
71bd7186 MM |
8440 | { |
8441 | identifier = cp_parser_identifier (parser); | |
03fd3f84 | 8442 | /* Treat invalid names as if the parameter were nameless. */ |
71bd7186 MM |
8443 | if (identifier == error_mark_node) |
8444 | identifier = NULL_TREE; | |
8445 | } | |
a723baf1 MM |
8446 | else |
8447 | identifier = NULL_TREE; | |
71bd7186 | 8448 | |
a723baf1 MM |
8449 | /* Create the template parameter. */ |
8450 | parameter = finish_template_template_parm (class_type_node, | |
8451 | identifier); | |
21526606 | 8452 | |
a723baf1 MM |
8453 | /* If the next token is an `=', then there is a |
8454 | default-argument. */ | |
8455 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
8456 | { | |
b0bc6e8e KL |
8457 | bool is_template; |
8458 | ||
a723baf1 MM |
8459 | /* Consume the `='. */ |
8460 | cp_lexer_consume_token (parser->lexer); | |
8461 | /* Parse the id-expression. */ | |
21526606 | 8462 | default_argument |
a723baf1 MM |
8463 | = cp_parser_id_expression (parser, |
8464 | /*template_keyword_p=*/false, | |
8465 | /*check_dependency_p=*/true, | |
b0bc6e8e | 8466 | /*template_p=*/&is_template, |
f3c2dfc6 | 8467 | /*declarator_p=*/false); |
a3a503a5 GB |
8468 | if (TREE_CODE (default_argument) == TYPE_DECL) |
8469 | /* If the id-expression was a template-id that refers to | |
8470 | a template-class, we already have the declaration here, | |
8471 | so no further lookup is needed. */ | |
8472 | ; | |
8473 | else | |
8474 | /* Look up the name. */ | |
21526606 | 8475 | default_argument |
a3a503a5 | 8476 | = cp_parser_lookup_name (parser, default_argument, |
fc6a28d7 MM |
8477 | none_type, |
8478 | /*is_template=*/is_template, | |
8479 | /*is_namespace=*/false, | |
8480 | /*check_dependency=*/true, | |
8481 | /*ambiguous_p=*/NULL); | |
a723baf1 MM |
8482 | /* See if the default argument is valid. */ |
8483 | default_argument | |
8484 | = check_template_template_default_arg (default_argument); | |
8485 | } | |
8486 | else | |
8487 | default_argument = NULL_TREE; | |
8488 | ||
8489 | /* Create the combined representation of the parameter and the | |
8490 | default argument. */ | |
71bd7186 | 8491 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
8492 | } |
8493 | break; | |
8494 | ||
8495 | default: | |
71bd7186 MM |
8496 | gcc_unreachable (); |
8497 | break; | |
a723baf1 | 8498 | } |
21526606 | 8499 | |
a723baf1 MM |
8500 | return parameter; |
8501 | } | |
8502 | ||
8503 | /* Parse a template-id. | |
8504 | ||
8505 | template-id: | |
8506 | template-name < template-argument-list [opt] > | |
8507 | ||
8508 | If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the | |
8509 | `template' keyword. In this case, a TEMPLATE_ID_EXPR will be | |
8510 | returned. Otherwise, if the template-name names a function, or set | |
8511 | of functions, returns a TEMPLATE_ID_EXPR. If the template-name | |
21526606 | 8512 | names a class, returns a TYPE_DECL for the specialization. |
a723baf1 MM |
8513 | |
8514 | If CHECK_DEPENDENCY_P is FALSE, names are looked up in | |
8515 | uninstantiated templates. */ | |
8516 | ||
8517 | static tree | |
21526606 EC |
8518 | cp_parser_template_id (cp_parser *parser, |
8519 | bool template_keyword_p, | |
a668c6ad MM |
8520 | bool check_dependency_p, |
8521 | bool is_declaration) | |
a723baf1 MM |
8522 | { |
8523 | tree template; | |
8524 | tree arguments; | |
a723baf1 | 8525 | tree template_id; |
0c5e4866 | 8526 | cp_token_position start_of_id = 0; |
a723baf1 | 8527 | tree access_check = NULL_TREE; |
f4abade9 | 8528 | cp_token *next_token, *next_token_2; |
a668c6ad | 8529 | bool is_identifier; |
a723baf1 MM |
8530 | |
8531 | /* If the next token corresponds to a template-id, there is no need | |
8532 | to reparse it. */ | |
2050a1bb MM |
8533 | next_token = cp_lexer_peek_token (parser->lexer); |
8534 | if (next_token->type == CPP_TEMPLATE_ID) | |
a723baf1 MM |
8535 | { |
8536 | tree value; | |
8537 | tree check; | |
8538 | ||
8539 | /* Get the stored value. */ | |
8540 | value = cp_lexer_consume_token (parser->lexer)->value; | |
8541 | /* Perform any access checks that were deferred. */ | |
8542 | for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check)) | |
cf22909c KL |
8543 | perform_or_defer_access_check (TREE_PURPOSE (check), |
8544 | TREE_VALUE (check)); | |
a723baf1 MM |
8545 | /* Return the stored value. */ |
8546 | return TREE_VALUE (value); | |
8547 | } | |
8548 | ||
2050a1bb MM |
8549 | /* Avoid performing name lookup if there is no possibility of |
8550 | finding a template-id. */ | |
8551 | if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR) | |
8552 | || (next_token->type == CPP_NAME | |
21526606 | 8553 | && !cp_parser_nth_token_starts_template_argument_list_p |
f4abade9 | 8554 | (parser, 2))) |
2050a1bb MM |
8555 | { |
8556 | cp_parser_error (parser, "expected template-id"); | |
8557 | return error_mark_node; | |
8558 | } | |
8559 | ||
a723baf1 | 8560 | /* Remember where the template-id starts. */ |
0b16f8f4 | 8561 | if (cp_parser_uncommitted_to_tentative_parse_p (parser)) |
0c5e4866 | 8562 | start_of_id = cp_lexer_token_position (parser->lexer, false); |
a723baf1 | 8563 | |
8d241e0b | 8564 | push_deferring_access_checks (dk_deferred); |
cf22909c | 8565 | |
a723baf1 | 8566 | /* Parse the template-name. */ |
a668c6ad | 8567 | is_identifier = false; |
a723baf1 | 8568 | template = cp_parser_template_name (parser, template_keyword_p, |
a668c6ad MM |
8569 | check_dependency_p, |
8570 | is_declaration, | |
8571 | &is_identifier); | |
8572 | if (template == error_mark_node || is_identifier) | |
cf22909c KL |
8573 | { |
8574 | pop_deferring_access_checks (); | |
a668c6ad | 8575 | return template; |
cf22909c | 8576 | } |
a723baf1 | 8577 | |
21526606 | 8578 | /* If we find the sequence `[:' after a template-name, it's probably |
f4abade9 GB |
8579 | a digraph-typo for `< ::'. Substitute the tokens and check if we can |
8580 | parse correctly the argument list. */ | |
2cfe82fe | 8581 | next_token = cp_lexer_peek_token (parser->lexer); |
f4abade9 | 8582 | next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2); |
21526606 | 8583 | if (next_token->type == CPP_OPEN_SQUARE |
f4abade9 | 8584 | && next_token->flags & DIGRAPH |
21526606 | 8585 | && next_token_2->type == CPP_COLON |
f4abade9 | 8586 | && !(next_token_2->flags & PREV_WHITE)) |
cf22909c | 8587 | { |
f4abade9 GB |
8588 | cp_parser_parse_tentatively (parser); |
8589 | /* Change `:' into `::'. */ | |
8590 | next_token_2->type = CPP_SCOPE; | |
8591 | /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is | |
8592 | CPP_LESS. */ | |
8593 | cp_lexer_consume_token (parser->lexer); | |
8594 | /* Parse the arguments. */ | |
8595 | arguments = cp_parser_enclosed_template_argument_list (parser); | |
8596 | if (!cp_parser_parse_definitely (parser)) | |
8597 | { | |
8598 | /* If we couldn't parse an argument list, then we revert our changes | |
8599 | and return simply an error. Maybe this is not a template-id | |
8600 | after all. */ | |
8601 | next_token_2->type = CPP_COLON; | |
2a13a625 | 8602 | cp_parser_error (parser, "expected %<<%>"); |
f4abade9 GB |
8603 | pop_deferring_access_checks (); |
8604 | return error_mark_node; | |
8605 | } | |
8606 | /* Otherwise, emit an error about the invalid digraph, but continue | |
8607 | parsing because we got our argument list. */ | |
2a13a625 GDR |
8608 | pedwarn ("%<<::%> cannot begin a template-argument list"); |
8609 | inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace " | |
8610 | "between %<<%> and %<::%>"); | |
f4abade9 GB |
8611 | if (!flag_permissive) |
8612 | { | |
8613 | static bool hint; | |
8614 | if (!hint) | |
8615 | { | |
2a13a625 | 8616 | inform ("(if you use -fpermissive G++ will accept your code)"); |
f4abade9 GB |
8617 | hint = true; |
8618 | } | |
8619 | } | |
8620 | } | |
8621 | else | |
8622 | { | |
8623 | /* Look for the `<' that starts the template-argument-list. */ | |
8624 | if (!cp_parser_require (parser, CPP_LESS, "`<'")) | |
8625 | { | |
8626 | pop_deferring_access_checks (); | |
8627 | return error_mark_node; | |
8628 | } | |
8629 | /* Parse the arguments. */ | |
8630 | arguments = cp_parser_enclosed_template_argument_list (parser); | |
cf22909c | 8631 | } |
a723baf1 MM |
8632 | |
8633 | /* Build a representation of the specialization. */ | |
8634 | if (TREE_CODE (template) == IDENTIFIER_NODE) | |
8635 | template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments); | |
8636 | else if (DECL_CLASS_TEMPLATE_P (template) | |
8637 | || DECL_TEMPLATE_TEMPLATE_PARM_P (template)) | |
21526606 EC |
8638 | template_id |
8639 | = finish_template_type (template, arguments, | |
8640 | cp_lexer_next_token_is (parser->lexer, | |
a723baf1 MM |
8641 | CPP_SCOPE)); |
8642 | else | |
8643 | { | |
8644 | /* If it's not a class-template or a template-template, it should be | |
8645 | a function-template. */ | |
50bc768d NS |
8646 | gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template) |
8647 | || TREE_CODE (template) == OVERLOAD | |
8648 | || BASELINK_P (template))); | |
21526606 | 8649 | |
a723baf1 MM |
8650 | template_id = lookup_template_function (template, arguments); |
8651 | } | |
21526606 | 8652 | |
cf22909c KL |
8653 | /* Retrieve any deferred checks. Do not pop this access checks yet |
8654 | so the memory will not be reclaimed during token replacing below. */ | |
8655 | access_check = get_deferred_access_checks (); | |
8656 | ||
a723baf1 MM |
8657 | /* If parsing tentatively, replace the sequence of tokens that makes |
8658 | up the template-id with a CPP_TEMPLATE_ID token. That way, | |
8659 | should we re-parse the token stream, we will not have to repeat | |
8660 | the effort required to do the parse, nor will we issue duplicate | |
8661 | error messages about problems during instantiation of the | |
e894ab29 | 8662 | template. */ |
c8a7ed43 | 8663 | if (start_of_id) |
a723baf1 | 8664 | { |
0c5e4866 NS |
8665 | cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id); |
8666 | ||
a723baf1 MM |
8667 | /* Reset the contents of the START_OF_ID token. */ |
8668 | token->type = CPP_TEMPLATE_ID; | |
8669 | token->value = build_tree_list (access_check, template_id); | |
8670 | token->keyword = RID_MAX; | |
0c5e4866 | 8671 | |
a723baf1 | 8672 | /* Purge all subsequent tokens. */ |
0c5e4866 | 8673 | cp_lexer_purge_tokens_after (parser->lexer, start_of_id); |
c8a7ed43 AO |
8674 | |
8675 | /* ??? Can we actually assume that, if template_id == | |
8676 | error_mark_node, we will have issued a diagnostic to the | |
8677 | user, as opposed to simply marking the tentative parse as | |
8678 | failed? */ | |
8679 | if (cp_parser_error_occurred (parser) && template_id != error_mark_node) | |
8680 | error ("parse error in template argument list"); | |
a723baf1 MM |
8681 | } |
8682 | ||
cf22909c | 8683 | pop_deferring_access_checks (); |
a723baf1 MM |
8684 | return template_id; |
8685 | } | |
8686 | ||
8687 | /* Parse a template-name. | |
8688 | ||
8689 | template-name: | |
8690 | identifier | |
21526606 | 8691 | |
a723baf1 MM |
8692 | The standard should actually say: |
8693 | ||
8694 | template-name: | |
8695 | identifier | |
8696 | operator-function-id | |
a723baf1 MM |
8697 | |
8698 | A defect report has been filed about this issue. | |
8699 | ||
0d956474 GB |
8700 | A conversion-function-id cannot be a template name because they cannot |
8701 | be part of a template-id. In fact, looking at this code: | |
8702 | ||
8703 | a.operator K<int>() | |
8704 | ||
8705 | the conversion-function-id is "operator K<int>", and K<int> is a type-id. | |
21526606 | 8706 | It is impossible to call a templated conversion-function-id with an |
0d956474 GB |
8707 | explicit argument list, since the only allowed template parameter is |
8708 | the type to which it is converting. | |
8709 | ||
a723baf1 MM |
8710 | If TEMPLATE_KEYWORD_P is true, then we have just seen the |
8711 | `template' keyword, in a construction like: | |
8712 | ||
8713 | T::template f<3>() | |
8714 | ||
8715 | In that case `f' is taken to be a template-name, even though there | |
8716 | is no way of knowing for sure. | |
8717 | ||
8718 | Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the | |
8719 | name refers to a set of overloaded functions, at least one of which | |
8720 | is a template, or an IDENTIFIER_NODE with the name of the template, | |
8721 | if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE, | |
8722 | names are looked up inside uninstantiated templates. */ | |
8723 | ||
8724 | static tree | |
21526606 EC |
8725 | cp_parser_template_name (cp_parser* parser, |
8726 | bool template_keyword_p, | |
a668c6ad MM |
8727 | bool check_dependency_p, |
8728 | bool is_declaration, | |
8729 | bool *is_identifier) | |
a723baf1 MM |
8730 | { |
8731 | tree identifier; | |
8732 | tree decl; | |
8733 | tree fns; | |
8734 | ||
8735 | /* If the next token is `operator', then we have either an | |
8736 | operator-function-id or a conversion-function-id. */ | |
8737 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR)) | |
8738 | { | |
8739 | /* We don't know whether we're looking at an | |
8740 | operator-function-id or a conversion-function-id. */ | |
8741 | cp_parser_parse_tentatively (parser); | |
8742 | /* Try an operator-function-id. */ | |
8743 | identifier = cp_parser_operator_function_id (parser); | |
8744 | /* If that didn't work, try a conversion-function-id. */ | |
8745 | if (!cp_parser_parse_definitely (parser)) | |
0d956474 GB |
8746 | { |
8747 | cp_parser_error (parser, "expected template-name"); | |
8748 | return error_mark_node; | |
8749 | } | |
a723baf1 MM |
8750 | } |
8751 | /* Look for the identifier. */ | |
8752 | else | |
8753 | identifier = cp_parser_identifier (parser); | |
21526606 | 8754 | |
a723baf1 MM |
8755 | /* If we didn't find an identifier, we don't have a template-id. */ |
8756 | if (identifier == error_mark_node) | |
8757 | return error_mark_node; | |
8758 | ||
8759 | /* If the name immediately followed the `template' keyword, then it | |
8760 | is a template-name. However, if the next token is not `<', then | |
8761 | we do not treat it as a template-name, since it is not being used | |
8762 | as part of a template-id. This enables us to handle constructs | |
8763 | like: | |
8764 | ||
8765 | template <typename T> struct S { S(); }; | |
8766 | template <typename T> S<T>::S(); | |
8767 | ||
8768 | correctly. We would treat `S' as a template -- if it were `S<T>' | |
8769 | -- but we do not if there is no `<'. */ | |
a668c6ad MM |
8770 | |
8771 | if (processing_template_decl | |
f4abade9 | 8772 | && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)) |
a668c6ad MM |
8773 | { |
8774 | /* In a declaration, in a dependent context, we pretend that the | |
8775 | "template" keyword was present in order to improve error | |
8776 | recovery. For example, given: | |
21526606 | 8777 | |
a668c6ad | 8778 | template <typename T> void f(T::X<int>); |
21526606 | 8779 | |
a668c6ad | 8780 | we want to treat "X<int>" as a template-id. */ |
21526606 EC |
8781 | if (is_declaration |
8782 | && !template_keyword_p | |
a668c6ad | 8783 | && parser->scope && TYPE_P (parser->scope) |
a52eb3bc | 8784 | && check_dependency_p |
4e0f4df5 GB |
8785 | && dependent_type_p (parser->scope) |
8786 | /* Do not do this for dtors (or ctors), since they never | |
8787 | need the template keyword before their name. */ | |
8788 | && !constructor_name_p (identifier, parser->scope)) | |
a668c6ad | 8789 | { |
0c5e4866 NS |
8790 | cp_token_position start = 0; |
8791 | ||
a668c6ad | 8792 | /* Explain what went wrong. */ |
2a13a625 GDR |
8793 | error ("non-template %qD used as template", identifier); |
8794 | inform ("use %<%T::template %D%> to indicate that it is a template", | |
4e0f4df5 | 8795 | parser->scope, identifier); |
0b16f8f4 VR |
8796 | /* If parsing tentatively, find the location of the "<" token. */ |
8797 | if (cp_parser_simulate_error (parser)) | |
8798 | start = cp_lexer_token_position (parser->lexer, true); | |
a668c6ad MM |
8799 | /* Parse the template arguments so that we can issue error |
8800 | messages about them. */ | |
8801 | cp_lexer_consume_token (parser->lexer); | |
8802 | cp_parser_enclosed_template_argument_list (parser); | |
8803 | /* Skip tokens until we find a good place from which to | |
8804 | continue parsing. */ | |
8805 | cp_parser_skip_to_closing_parenthesis (parser, | |
8806 | /*recovering=*/true, | |
8807 | /*or_comma=*/true, | |
8808 | /*consume_paren=*/false); | |
8809 | /* If parsing tentatively, permanently remove the | |
8810 | template argument list. That will prevent duplicate | |
8811 | error messages from being issued about the missing | |
8812 | "template" keyword. */ | |
0c5e4866 NS |
8813 | if (start) |
8814 | cp_lexer_purge_tokens_after (parser->lexer, start); | |
a668c6ad MM |
8815 | if (is_identifier) |
8816 | *is_identifier = true; | |
8817 | return identifier; | |
8818 | } | |
9d363a56 MM |
8819 | |
8820 | /* If the "template" keyword is present, then there is generally | |
8821 | no point in doing name-lookup, so we just return IDENTIFIER. | |
8822 | But, if the qualifying scope is non-dependent then we can | |
8823 | (and must) do name-lookup normally. */ | |
8824 | if (template_keyword_p | |
8825 | && (!parser->scope | |
98ca843c | 8826 | || (TYPE_P (parser->scope) |
9d363a56 | 8827 | && dependent_type_p (parser->scope)))) |
a668c6ad MM |
8828 | return identifier; |
8829 | } | |
a723baf1 MM |
8830 | |
8831 | /* Look up the name. */ | |
8832 | decl = cp_parser_lookup_name (parser, identifier, | |
fc6a28d7 | 8833 | none_type, |
b0bc6e8e | 8834 | /*is_template=*/false, |
eea9800f | 8835 | /*is_namespace=*/false, |
8f78f01f MM |
8836 | check_dependency_p, |
8837 | /*ambiguous_p=*/NULL); | |
a723baf1 MM |
8838 | decl = maybe_get_template_decl_from_type_decl (decl); |
8839 | ||
8840 | /* If DECL is a template, then the name was a template-name. */ | |
8841 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
8842 | ; | |
21526606 | 8843 | else |
a723baf1 | 8844 | { |
d58a2b83 MM |
8845 | tree fn = NULL_TREE; |
8846 | ||
a723baf1 MM |
8847 | /* The standard does not explicitly indicate whether a name that |
8848 | names a set of overloaded declarations, some of which are | |
8849 | templates, is a template-name. However, such a name should | |
8850 | be a template-name; otherwise, there is no way to form a | |
8851 | template-id for the overloaded templates. */ | |
8852 | fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl; | |
8853 | if (TREE_CODE (fns) == OVERLOAD) | |
d58a2b83 MM |
8854 | for (fn = fns; fn; fn = OVL_NEXT (fn)) |
8855 | if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL) | |
8856 | break; | |
21526606 | 8857 | |
d58a2b83 | 8858 | if (!fn) |
a723baf1 | 8859 | { |
d58a2b83 | 8860 | /* The name does not name a template. */ |
a723baf1 MM |
8861 | cp_parser_error (parser, "expected template-name"); |
8862 | return error_mark_node; | |
8863 | } | |
8864 | } | |
8865 | ||
8866 | /* If DECL is dependent, and refers to a function, then just return | |
8867 | its name; we will look it up again during template instantiation. */ | |
8868 | if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl)) | |
8869 | { | |
8870 | tree scope = CP_DECL_CONTEXT (get_first_fn (decl)); | |
1fb3244a | 8871 | if (TYPE_P (scope) && dependent_type_p (scope)) |
a723baf1 MM |
8872 | return identifier; |
8873 | } | |
8874 | ||
8875 | return decl; | |
8876 | } | |
8877 | ||
8878 | /* Parse a template-argument-list. | |
8879 | ||
8880 | template-argument-list: | |
8881 | template-argument | |
8882 | template-argument-list , template-argument | |
8883 | ||
04c06002 | 8884 | Returns a TREE_VEC containing the arguments. */ |
a723baf1 MM |
8885 | |
8886 | static tree | |
94edc4ab | 8887 | cp_parser_template_argument_list (cp_parser* parser) |
a723baf1 | 8888 | { |
bf12d54d NS |
8889 | tree fixed_args[10]; |
8890 | unsigned n_args = 0; | |
8891 | unsigned alloced = 10; | |
8892 | tree *arg_ary = fixed_args; | |
8893 | tree vec; | |
4bb8ca28 | 8894 | bool saved_in_template_argument_list_p; |
a723baf1 | 8895 | |
4bb8ca28 MM |
8896 | saved_in_template_argument_list_p = parser->in_template_argument_list_p; |
8897 | parser->in_template_argument_list_p = true; | |
bf12d54d | 8898 | do |
a723baf1 MM |
8899 | { |
8900 | tree argument; | |
8901 | ||
bf12d54d | 8902 | if (n_args) |
04c06002 | 8903 | /* Consume the comma. */ |
bf12d54d | 8904 | cp_lexer_consume_token (parser->lexer); |
21526606 | 8905 | |
a723baf1 MM |
8906 | /* Parse the template-argument. */ |
8907 | argument = cp_parser_template_argument (parser); | |
bf12d54d NS |
8908 | if (n_args == alloced) |
8909 | { | |
8910 | alloced *= 2; | |
21526606 | 8911 | |
bf12d54d NS |
8912 | if (arg_ary == fixed_args) |
8913 | { | |
8914 | arg_ary = xmalloc (sizeof (tree) * alloced); | |
8915 | memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); | |
8916 | } | |
8917 | else | |
8918 | arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced); | |
8919 | } | |
8920 | arg_ary[n_args++] = argument; | |
a723baf1 | 8921 | } |
bf12d54d NS |
8922 | while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); |
8923 | ||
8924 | vec = make_tree_vec (n_args); | |
a723baf1 | 8925 | |
bf12d54d NS |
8926 | while (n_args--) |
8927 | TREE_VEC_ELT (vec, n_args) = arg_ary[n_args]; | |
21526606 | 8928 | |
bf12d54d NS |
8929 | if (arg_ary != fixed_args) |
8930 | free (arg_ary); | |
4bb8ca28 | 8931 | parser->in_template_argument_list_p = saved_in_template_argument_list_p; |
bf12d54d | 8932 | return vec; |
a723baf1 MM |
8933 | } |
8934 | ||
8935 | /* Parse a template-argument. | |
8936 | ||
8937 | template-argument: | |
8938 | assignment-expression | |
8939 | type-id | |
8940 | id-expression | |
8941 | ||
8942 | The representation is that of an assignment-expression, type-id, or | |
8943 | id-expression -- except that the qualified id-expression is | |
8944 | evaluated, so that the value returned is either a DECL or an | |
21526606 | 8945 | OVERLOAD. |
d17811fd MM |
8946 | |
8947 | Although the standard says "assignment-expression", it forbids | |
8948 | throw-expressions or assignments in the template argument. | |
8949 | Therefore, we use "conditional-expression" instead. */ | |
a723baf1 MM |
8950 | |
8951 | static tree | |
94edc4ab | 8952 | cp_parser_template_argument (cp_parser* parser) |
a723baf1 MM |
8953 | { |
8954 | tree argument; | |
8955 | bool template_p; | |
d17811fd | 8956 | bool address_p; |
4d5297fa | 8957 | bool maybe_type_id = false; |
d17811fd | 8958 | cp_token *token; |
b3445994 | 8959 | cp_id_kind idk; |
d17811fd | 8960 | tree qualifying_class; |
a723baf1 MM |
8961 | |
8962 | /* There's really no way to know what we're looking at, so we just | |
21526606 | 8963 | try each alternative in order. |
a723baf1 MM |
8964 | |
8965 | [temp.arg] | |
8966 | ||
8967 | In a template-argument, an ambiguity between a type-id and an | |
8968 | expression is resolved to a type-id, regardless of the form of | |
21526606 | 8969 | the corresponding template-parameter. |
a723baf1 MM |
8970 | |
8971 | Therefore, we try a type-id first. */ | |
8972 | cp_parser_parse_tentatively (parser); | |
a723baf1 | 8973 | argument = cp_parser_type_id (parser); |
4d5297fa | 8974 | /* If there was no error parsing the type-id but the next token is a '>>', |
21526606 | 8975 | we probably found a typo for '> >'. But there are type-id which are |
4d5297fa GB |
8976 | also valid expressions. For instance: |
8977 | ||
8978 | struct X { int operator >> (int); }; | |
8979 | template <int V> struct Foo {}; | |
8980 | Foo<X () >> 5> r; | |
8981 | ||
8982 | Here 'X()' is a valid type-id of a function type, but the user just | |
8983 | wanted to write the expression "X() >> 5". Thus, we remember that we | |
8984 | found a valid type-id, but we still try to parse the argument as an | |
8985 | expression to see what happens. */ | |
8986 | if (!cp_parser_error_occurred (parser) | |
8987 | && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) | |
8988 | { | |
8989 | maybe_type_id = true; | |
8990 | cp_parser_abort_tentative_parse (parser); | |
8991 | } | |
8992 | else | |
8993 | { | |
8994 | /* If the next token isn't a `,' or a `>', then this argument wasn't | |
8995 | really finished. This means that the argument is not a valid | |
8996 | type-id. */ | |
8997 | if (!cp_parser_next_token_ends_template_argument_p (parser)) | |
8998 | cp_parser_error (parser, "expected template-argument"); | |
8999 | /* If that worked, we're done. */ | |
9000 | if (cp_parser_parse_definitely (parser)) | |
9001 | return argument; | |
9002 | } | |
a723baf1 MM |
9003 | /* We're still not sure what the argument will be. */ |
9004 | cp_parser_parse_tentatively (parser); | |
9005 | /* Try a template. */ | |
21526606 | 9006 | argument = cp_parser_id_expression (parser, |
a723baf1 MM |
9007 | /*template_keyword_p=*/false, |
9008 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
9009 | &template_p, |
9010 | /*declarator_p=*/false); | |
a723baf1 MM |
9011 | /* If the next token isn't a `,' or a `>', then this argument wasn't |
9012 | really finished. */ | |
d17811fd | 9013 | if (!cp_parser_next_token_ends_template_argument_p (parser)) |
a723baf1 MM |
9014 | cp_parser_error (parser, "expected template-argument"); |
9015 | if (!cp_parser_error_occurred (parser)) | |
9016 | { | |
f746161e MM |
9017 | /* Figure out what is being referred to. If the id-expression |
9018 | was for a class template specialization, then we will have a | |
9019 | TYPE_DECL at this point. There is no need to do name lookup | |
9020 | at this point in that case. */ | |
9021 | if (TREE_CODE (argument) != TYPE_DECL) | |
9022 | argument = cp_parser_lookup_name (parser, argument, | |
fc6a28d7 | 9023 | none_type, |
f746161e MM |
9024 | /*is_template=*/template_p, |
9025 | /*is_namespace=*/false, | |
8f78f01f MM |
9026 | /*check_dependency=*/true, |
9027 | /*ambiguous_p=*/NULL); | |
5b4acce1 KL |
9028 | if (TREE_CODE (argument) != TEMPLATE_DECL |
9029 | && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE) | |
a723baf1 MM |
9030 | cp_parser_error (parser, "expected template-name"); |
9031 | } | |
9032 | if (cp_parser_parse_definitely (parser)) | |
9033 | return argument; | |
d17811fd MM |
9034 | /* It must be a non-type argument. There permitted cases are given |
9035 | in [temp.arg.nontype]: | |
9036 | ||
9037 | -- an integral constant-expression of integral or enumeration | |
9038 | type; or | |
9039 | ||
9040 | -- the name of a non-type template-parameter; or | |
9041 | ||
9042 | -- the name of an object or function with external linkage... | |
9043 | ||
9044 | -- the address of an object or function with external linkage... | |
9045 | ||
04c06002 | 9046 | -- a pointer to member... */ |
d17811fd MM |
9047 | /* Look for a non-type template parameter. */ |
9048 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
9049 | { | |
9050 | cp_parser_parse_tentatively (parser); | |
9051 | argument = cp_parser_primary_expression (parser, | |
93678513 | 9052 | /*cast_p=*/false, |
d17811fd MM |
9053 | &idk, |
9054 | &qualifying_class); | |
9055 | if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX | |
9056 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
9057 | cp_parser_simulate_error (parser); | |
9058 | if (cp_parser_parse_definitely (parser)) | |
9059 | return argument; | |
9060 | } | |
db24eb1f | 9061 | |
d17811fd MM |
9062 | /* If the next token is "&", the argument must be the address of an |
9063 | object or function with external linkage. */ | |
9064 | address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND); | |
9065 | if (address_p) | |
9066 | cp_lexer_consume_token (parser->lexer); | |
9067 | /* See if we might have an id-expression. */ | |
9068 | token = cp_lexer_peek_token (parser->lexer); | |
9069 | if (token->type == CPP_NAME | |
9070 | || token->keyword == RID_OPERATOR | |
9071 | || token->type == CPP_SCOPE | |
9072 | || token->type == CPP_TEMPLATE_ID | |
9073 | || token->type == CPP_NESTED_NAME_SPECIFIER) | |
9074 | { | |
9075 | cp_parser_parse_tentatively (parser); | |
9076 | argument = cp_parser_primary_expression (parser, | |
93678513 | 9077 | /*cast_p=*/false, |
d17811fd MM |
9078 | &idk, |
9079 | &qualifying_class); | |
9080 | if (cp_parser_error_occurred (parser) | |
9081 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
9082 | cp_parser_abort_tentative_parse (parser); | |
9083 | else | |
9084 | { | |
db24eb1f NS |
9085 | if (TREE_CODE (argument) == INDIRECT_REF) |
9086 | { | |
9087 | gcc_assert (REFERENCE_REF_P (argument)); | |
9088 | argument = TREE_OPERAND (argument, 0); | |
9089 | } | |
9090 | ||
d17811fd MM |
9091 | if (qualifying_class) |
9092 | argument = finish_qualified_id_expr (qualifying_class, | |
9093 | argument, | |
9094 | /*done=*/true, | |
9095 | address_p); | |
9096 | if (TREE_CODE (argument) == VAR_DECL) | |
9097 | { | |
9098 | /* A variable without external linkage might still be a | |
9099 | valid constant-expression, so no error is issued here | |
9100 | if the external-linkage check fails. */ | |
9101 | if (!DECL_EXTERNAL_LINKAGE_P (argument)) | |
9102 | cp_parser_simulate_error (parser); | |
9103 | } | |
9104 | else if (is_overloaded_fn (argument)) | |
9105 | /* All overloaded functions are allowed; if the external | |
9106 | linkage test does not pass, an error will be issued | |
9107 | later. */ | |
9108 | ; | |
9109 | else if (address_p | |
21526606 | 9110 | && (TREE_CODE (argument) == OFFSET_REF |
d17811fd MM |
9111 | || TREE_CODE (argument) == SCOPE_REF)) |
9112 | /* A pointer-to-member. */ | |
9113 | ; | |
db24eb1f NS |
9114 | else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX) |
9115 | ; | |
d17811fd MM |
9116 | else |
9117 | cp_parser_simulate_error (parser); | |
9118 | ||
9119 | if (cp_parser_parse_definitely (parser)) | |
9120 | { | |
9121 | if (address_p) | |
9122 | argument = build_x_unary_op (ADDR_EXPR, argument); | |
9123 | return argument; | |
9124 | } | |
9125 | } | |
9126 | } | |
9127 | /* If the argument started with "&", there are no other valid | |
9128 | alternatives at this point. */ | |
9129 | if (address_p) | |
9130 | { | |
9131 | cp_parser_error (parser, "invalid non-type template argument"); | |
9132 | return error_mark_node; | |
9133 | } | |
db24eb1f | 9134 | |
4d5297fa | 9135 | /* If the argument wasn't successfully parsed as a type-id followed |
21526606 | 9136 | by '>>', the argument can only be a constant expression now. |
4d5297fa GB |
9137 | Otherwise, we try parsing the constant-expression tentatively, |
9138 | because the argument could really be a type-id. */ | |
9139 | if (maybe_type_id) | |
9140 | cp_parser_parse_tentatively (parser); | |
21526606 | 9141 | argument = cp_parser_constant_expression (parser, |
d17811fd MM |
9142 | /*allow_non_constant_p=*/false, |
9143 | /*non_constant_p=*/NULL); | |
9baa27a9 | 9144 | argument = fold_non_dependent_expr (argument); |
4d5297fa GB |
9145 | if (!maybe_type_id) |
9146 | return argument; | |
9147 | if (!cp_parser_next_token_ends_template_argument_p (parser)) | |
9148 | cp_parser_error (parser, "expected template-argument"); | |
9149 | if (cp_parser_parse_definitely (parser)) | |
9150 | return argument; | |
9151 | /* We did our best to parse the argument as a non type-id, but that | |
9152 | was the only alternative that matched (albeit with a '>' after | |
21526606 | 9153 | it). We can assume it's just a typo from the user, and a |
4d5297fa GB |
9154 | diagnostic will then be issued. */ |
9155 | return cp_parser_type_id (parser); | |
a723baf1 MM |
9156 | } |
9157 | ||
9158 | /* Parse an explicit-instantiation. | |
9159 | ||
9160 | explicit-instantiation: | |
21526606 | 9161 | template declaration |
a723baf1 MM |
9162 | |
9163 | Although the standard says `declaration', what it really means is: | |
9164 | ||
9165 | explicit-instantiation: | |
21526606 | 9166 | template decl-specifier-seq [opt] declarator [opt] ; |
a723baf1 MM |
9167 | |
9168 | Things like `template int S<int>::i = 5, int S<double>::j;' are not | |
9169 | supposed to be allowed. A defect report has been filed about this | |
21526606 | 9170 | issue. |
a723baf1 MM |
9171 | |
9172 | GNU Extension: | |
21526606 | 9173 | |
a723baf1 | 9174 | explicit-instantiation: |
21526606 | 9175 | storage-class-specifier template |
a723baf1 | 9176 | decl-specifier-seq [opt] declarator [opt] ; |
21526606 | 9177 | function-specifier template |
a723baf1 MM |
9178 | decl-specifier-seq [opt] declarator [opt] ; */ |
9179 | ||
9180 | static void | |
94edc4ab | 9181 | cp_parser_explicit_instantiation (cp_parser* parser) |
a723baf1 | 9182 | { |
560ad596 | 9183 | int declares_class_or_enum; |
62d1db17 | 9184 | cp_decl_specifier_seq decl_specifiers; |
a723baf1 MM |
9185 | tree extension_specifier = NULL_TREE; |
9186 | ||
9187 | /* Look for an (optional) storage-class-specifier or | |
9188 | function-specifier. */ | |
9189 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
9190 | { | |
21526606 | 9191 | extension_specifier |
a723baf1 MM |
9192 | = cp_parser_storage_class_specifier_opt (parser); |
9193 | if (!extension_specifier) | |
98ca843c | 9194 | extension_specifier |
62d1db17 MM |
9195 | = cp_parser_function_specifier_opt (parser, |
9196 | /*decl_specs=*/NULL); | |
a723baf1 MM |
9197 | } |
9198 | ||
9199 | /* Look for the `template' keyword. */ | |
9200 | cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"); | |
9201 | /* Let the front end know that we are processing an explicit | |
9202 | instantiation. */ | |
9203 | begin_explicit_instantiation (); | |
9204 | /* [temp.explicit] says that we are supposed to ignore access | |
9205 | control while processing explicit instantiation directives. */ | |
78757caa | 9206 | push_deferring_access_checks (dk_no_check); |
a723baf1 | 9207 | /* Parse a decl-specifier-seq. */ |
62d1db17 MM |
9208 | cp_parser_decl_specifier_seq (parser, |
9209 | CP_PARSER_FLAGS_OPTIONAL, | |
9210 | &decl_specifiers, | |
9211 | &declares_class_or_enum); | |
a723baf1 MM |
9212 | /* If there was exactly one decl-specifier, and it declared a class, |
9213 | and there's no declarator, then we have an explicit type | |
9214 | instantiation. */ | |
9215 | if (declares_class_or_enum && cp_parser_declares_only_class_p (parser)) | |
9216 | { | |
9217 | tree type; | |
9218 | ||
62d1db17 | 9219 | type = check_tag_decl (&decl_specifiers); |
b7fc8b57 KL |
9220 | /* Turn access control back on for names used during |
9221 | template instantiation. */ | |
9222 | pop_deferring_access_checks (); | |
a723baf1 MM |
9223 | if (type) |
9224 | do_type_instantiation (type, extension_specifier, /*complain=*/1); | |
9225 | } | |
9226 | else | |
9227 | { | |
058b15c1 | 9228 | cp_declarator *declarator; |
a723baf1 MM |
9229 | tree decl; |
9230 | ||
9231 | /* Parse the declarator. */ | |
21526606 | 9232 | declarator |
62b8a44e | 9233 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 | 9234 | /*ctor_dtor_or_conv_p=*/NULL, |
db86dd14 MM |
9235 | /*parenthesized_p=*/NULL, |
9236 | /*member_p=*/false); | |
fc6a28d7 MM |
9237 | if (declares_class_or_enum & 2) |
9238 | cp_parser_check_for_definition_in_return_type (declarator, | |
9239 | decl_specifiers.type); | |
058b15c1 | 9240 | if (declarator != cp_error_declarator) |
216bb6e1 | 9241 | { |
62d1db17 | 9242 | decl = grokdeclarator (declarator, &decl_specifiers, |
216bb6e1 MM |
9243 | NORMAL, 0, NULL); |
9244 | /* Turn access control back on for names used during | |
9245 | template instantiation. */ | |
9246 | pop_deferring_access_checks (); | |
9247 | /* Do the explicit instantiation. */ | |
9248 | do_decl_instantiation (decl, extension_specifier); | |
9249 | } | |
9250 | else | |
9251 | { | |
9252 | pop_deferring_access_checks (); | |
9253 | /* Skip the body of the explicit instantiation. */ | |
9254 | cp_parser_skip_to_end_of_statement (parser); | |
9255 | } | |
a723baf1 MM |
9256 | } |
9257 | /* We're done with the instantiation. */ | |
9258 | end_explicit_instantiation (); | |
a723baf1 | 9259 | |
e0860732 | 9260 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 MM |
9261 | } |
9262 | ||
9263 | /* Parse an explicit-specialization. | |
9264 | ||
9265 | explicit-specialization: | |
21526606 | 9266 | template < > declaration |
a723baf1 MM |
9267 | |
9268 | Although the standard says `declaration', what it really means is: | |
9269 | ||
9270 | explicit-specialization: | |
9271 | template <> decl-specifier [opt] init-declarator [opt] ; | |
21526606 | 9272 | template <> function-definition |
a723baf1 MM |
9273 | template <> explicit-specialization |
9274 | template <> template-declaration */ | |
9275 | ||
9276 | static void | |
94edc4ab | 9277 | cp_parser_explicit_specialization (cp_parser* parser) |
a723baf1 MM |
9278 | { |
9279 | /* Look for the `template' keyword. */ | |
9280 | cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"); | |
9281 | /* Look for the `<'. */ | |
9282 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
9283 | /* Look for the `>'. */ | |
9284 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
9285 | /* We have processed another parameter list. */ | |
9286 | ++parser->num_template_parameter_lists; | |
9287 | /* Let the front end know that we are beginning a specialization. */ | |
9288 | begin_specialization (); | |
9289 | ||
9290 | /* If the next keyword is `template', we need to figure out whether | |
9291 | or not we're looking a template-declaration. */ | |
9292 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
9293 | { | |
9294 | if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS | |
9295 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER) | |
9296 | cp_parser_template_declaration_after_export (parser, | |
9297 | /*member_p=*/false); | |
9298 | else | |
9299 | cp_parser_explicit_specialization (parser); | |
9300 | } | |
9301 | else | |
9302 | /* Parse the dependent declaration. */ | |
21526606 | 9303 | cp_parser_single_declaration (parser, |
a723baf1 MM |
9304 | /*member_p=*/false, |
9305 | /*friend_p=*/NULL); | |
9306 | ||
9307 | /* We're done with the specialization. */ | |
9308 | end_specialization (); | |
9309 | /* We're done with this parameter list. */ | |
9310 | --parser->num_template_parameter_lists; | |
9311 | } | |
9312 | ||
9313 | /* Parse a type-specifier. | |
9314 | ||
9315 | type-specifier: | |
9316 | simple-type-specifier | |
9317 | class-specifier | |
9318 | enum-specifier | |
9319 | elaborated-type-specifier | |
9320 | cv-qualifier | |
9321 | ||
9322 | GNU Extension: | |
9323 | ||
9324 | type-specifier: | |
9325 | __complex__ | |
9326 | ||
62d1db17 MM |
9327 | Returns a representation of the type-specifier. For a |
9328 | class-specifier, enum-specifier, or elaborated-type-specifier, a | |
9329 | TREE_TYPE is returned; otherwise, a TYPE_DECL is returned. | |
a723baf1 | 9330 | |
eb1aef53 KL |
9331 | The parser flags FLAGS is used to control type-specifier parsing. |
9332 | ||
9333 | If IS_DECLARATION is TRUE, then this type-specifier is appearing | |
9334 | in a decl-specifier-seq. | |
a723baf1 MM |
9335 | |
9336 | If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a | |
9337 | class-specifier, enum-specifier, or elaborated-type-specifier, then | |
83a00410 | 9338 | *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1 |
560ad596 MM |
9339 | if a type is declared; 2 if it is defined. Otherwise, it is set to |
9340 | zero. | |
a723baf1 MM |
9341 | |
9342 | If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a | |
9343 | cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it | |
9344 | is set to FALSE. */ | |
9345 | ||
9346 | static tree | |
21526606 EC |
9347 | cp_parser_type_specifier (cp_parser* parser, |
9348 | cp_parser_flags flags, | |
62d1db17 | 9349 | cp_decl_specifier_seq *decl_specs, |
94edc4ab | 9350 | bool is_declaration, |
560ad596 | 9351 | int* declares_class_or_enum, |
94edc4ab | 9352 | bool* is_cv_qualifier) |
a723baf1 MM |
9353 | { |
9354 | tree type_spec = NULL_TREE; | |
9355 | cp_token *token; | |
9356 | enum rid keyword; | |
62d1db17 | 9357 | cp_decl_spec ds = ds_last; |
a723baf1 MM |
9358 | |
9359 | /* Assume this type-specifier does not declare a new type. */ | |
9360 | if (declares_class_or_enum) | |
543ca912 | 9361 | *declares_class_or_enum = 0; |
a723baf1 MM |
9362 | /* And that it does not specify a cv-qualifier. */ |
9363 | if (is_cv_qualifier) | |
9364 | *is_cv_qualifier = false; | |
9365 | /* Peek at the next token. */ | |
9366 | token = cp_lexer_peek_token (parser->lexer); | |
9367 | ||
9368 | /* If we're looking at a keyword, we can use that to guide the | |
9369 | production we choose. */ | |
9370 | keyword = token->keyword; | |
9371 | switch (keyword) | |
9372 | { | |
ff4eb0b5 ZW |
9373 | case RID_ENUM: |
9374 | /* 'enum' [identifier] '{' introduces an enum-specifier; | |
9375 | 'enum' <anything else> introduces an elaborated-type-specifier. */ | |
9376 | if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE | |
9377 | || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME | |
9378 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type | |
9379 | == CPP_OPEN_BRACE)) | |
9380 | { | |
a5e51518 KL |
9381 | if (parser->num_template_parameter_lists) |
9382 | { | |
9383 | error ("template declaration of %qs", "enum"); | |
9384 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
9385 | type_spec = error_mark_node; | |
9386 | } | |
9387 | else | |
9388 | type_spec = cp_parser_enum_specifier (parser); | |
9389 | ||
ff4eb0b5 ZW |
9390 | if (declares_class_or_enum) |
9391 | *declares_class_or_enum = 2; | |
9392 | if (decl_specs) | |
9393 | cp_parser_set_decl_spec_type (decl_specs, | |
9394 | type_spec, | |
9395 | /*user_defined_p=*/true); | |
9396 | return type_spec; | |
9397 | } | |
9398 | else | |
9399 | goto elaborated_type_specifier; | |
9400 | ||
a723baf1 MM |
9401 | /* Any of these indicate either a class-specifier, or an |
9402 | elaborated-type-specifier. */ | |
9403 | case RID_CLASS: | |
9404 | case RID_STRUCT: | |
9405 | case RID_UNION: | |
a723baf1 | 9406 | /* Parse tentatively so that we can back up if we don't find a |
ff4eb0b5 | 9407 | class-specifier. */ |
a723baf1 | 9408 | cp_parser_parse_tentatively (parser); |
ff4eb0b5 ZW |
9409 | /* Look for the class-specifier. */ |
9410 | type_spec = cp_parser_class_specifier (parser); | |
a723baf1 MM |
9411 | /* If that worked, we're done. */ |
9412 | if (cp_parser_parse_definitely (parser)) | |
9413 | { | |
9414 | if (declares_class_or_enum) | |
560ad596 | 9415 | *declares_class_or_enum = 2; |
62d1db17 MM |
9416 | if (decl_specs) |
9417 | cp_parser_set_decl_spec_type (decl_specs, | |
9418 | type_spec, | |
9419 | /*user_defined_p=*/true); | |
a723baf1 MM |
9420 | return type_spec; |
9421 | } | |
9422 | ||
9423 | /* Fall through. */ | |
ff4eb0b5 ZW |
9424 | elaborated_type_specifier: |
9425 | /* We're declaring (not defining) a class or enum. */ | |
9426 | if (declares_class_or_enum) | |
9427 | *declares_class_or_enum = 1; | |
a723baf1 | 9428 | |
ff4eb0b5 | 9429 | /* Fall through. */ |
a723baf1 MM |
9430 | case RID_TYPENAME: |
9431 | /* Look for an elaborated-type-specifier. */ | |
98ca843c EC |
9432 | type_spec |
9433 | = (cp_parser_elaborated_type_specifier | |
62d1db17 MM |
9434 | (parser, |
9435 | decl_specs && decl_specs->specs[(int) ds_friend], | |
9436 | is_declaration)); | |
62d1db17 MM |
9437 | if (decl_specs) |
9438 | cp_parser_set_decl_spec_type (decl_specs, | |
9439 | type_spec, | |
9440 | /*user_defined_p=*/true); | |
a723baf1 MM |
9441 | return type_spec; |
9442 | ||
9443 | case RID_CONST: | |
62d1db17 MM |
9444 | ds = ds_const; |
9445 | if (is_cv_qualifier) | |
9446 | *is_cv_qualifier = true; | |
9447 | break; | |
98ca843c | 9448 | |
a723baf1 | 9449 | case RID_VOLATILE: |
62d1db17 | 9450 | ds = ds_volatile; |
a723baf1 MM |
9451 | if (is_cv_qualifier) |
9452 | *is_cv_qualifier = true; | |
62d1db17 | 9453 | break; |
a723baf1 | 9454 | |
62d1db17 MM |
9455 | case RID_RESTRICT: |
9456 | ds = ds_restrict; | |
9457 | if (is_cv_qualifier) | |
9458 | *is_cv_qualifier = true; | |
9459 | break; | |
a723baf1 MM |
9460 | |
9461 | case RID_COMPLEX: | |
9462 | /* The `__complex__' keyword is a GNU extension. */ | |
62d1db17 MM |
9463 | ds = ds_complex; |
9464 | break; | |
a723baf1 MM |
9465 | |
9466 | default: | |
9467 | break; | |
9468 | } | |
9469 | ||
62d1db17 MM |
9470 | /* Handle simple keywords. */ |
9471 | if (ds != ds_last) | |
9472 | { | |
9473 | if (decl_specs) | |
9474 | { | |
9475 | ++decl_specs->specs[(int)ds]; | |
9476 | decl_specs->any_specifiers_p = true; | |
9477 | } | |
9478 | return cp_lexer_consume_token (parser->lexer)->value; | |
9479 | } | |
9480 | ||
a723baf1 MM |
9481 | /* If we do not already have a type-specifier, assume we are looking |
9482 | at a simple-type-specifier. */ | |
98ca843c | 9483 | type_spec = cp_parser_simple_type_specifier (parser, |
62d1db17 MM |
9484 | decl_specs, |
9485 | flags); | |
a723baf1 MM |
9486 | |
9487 | /* If we didn't find a type-specifier, and a type-specifier was not | |
9488 | optional in this context, issue an error message. */ | |
9489 | if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
9490 | { | |
9491 | cp_parser_error (parser, "expected type specifier"); | |
9492 | return error_mark_node; | |
9493 | } | |
9494 | ||
9495 | return type_spec; | |
9496 | } | |
9497 | ||
9498 | /* Parse a simple-type-specifier. | |
9499 | ||
9500 | simple-type-specifier: | |
9501 | :: [opt] nested-name-specifier [opt] type-name | |
9502 | :: [opt] nested-name-specifier template template-id | |
9503 | char | |
9504 | wchar_t | |
9505 | bool | |
9506 | short | |
9507 | int | |
9508 | long | |
9509 | signed | |
9510 | unsigned | |
9511 | float | |
9512 | double | |
21526606 | 9513 | void |
a723baf1 MM |
9514 | |
9515 | GNU Extension: | |
9516 | ||
9517 | simple-type-specifier: | |
9518 | __typeof__ unary-expression | |
9519 | __typeof__ ( type-id ) | |
9520 | ||
62d1db17 MM |
9521 | Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is |
9522 | appropriately updated. */ | |
a723baf1 MM |
9523 | |
9524 | static tree | |
98ca843c | 9525 | cp_parser_simple_type_specifier (cp_parser* parser, |
62d1db17 MM |
9526 | cp_decl_specifier_seq *decl_specs, |
9527 | cp_parser_flags flags) | |
a723baf1 MM |
9528 | { |
9529 | tree type = NULL_TREE; | |
9530 | cp_token *token; | |
9531 | ||
9532 | /* Peek at the next token. */ | |
9533 | token = cp_lexer_peek_token (parser->lexer); | |
9534 | ||
9535 | /* If we're looking at a keyword, things are easy. */ | |
9536 | switch (token->keyword) | |
9537 | { | |
9538 | case RID_CHAR: | |
62d1db17 MM |
9539 | if (decl_specs) |
9540 | decl_specs->explicit_char_p = true; | |
4b0d3cbe MM |
9541 | type = char_type_node; |
9542 | break; | |
a723baf1 | 9543 | case RID_WCHAR: |
4b0d3cbe MM |
9544 | type = wchar_type_node; |
9545 | break; | |
a723baf1 | 9546 | case RID_BOOL: |
4b0d3cbe MM |
9547 | type = boolean_type_node; |
9548 | break; | |
a723baf1 | 9549 | case RID_SHORT: |
62d1db17 MM |
9550 | if (decl_specs) |
9551 | ++decl_specs->specs[(int) ds_short]; | |
4b0d3cbe MM |
9552 | type = short_integer_type_node; |
9553 | break; | |
a723baf1 | 9554 | case RID_INT: |
62d1db17 MM |
9555 | if (decl_specs) |
9556 | decl_specs->explicit_int_p = true; | |
4b0d3cbe MM |
9557 | type = integer_type_node; |
9558 | break; | |
a723baf1 | 9559 | case RID_LONG: |
62d1db17 MM |
9560 | if (decl_specs) |
9561 | ++decl_specs->specs[(int) ds_long]; | |
4b0d3cbe MM |
9562 | type = long_integer_type_node; |
9563 | break; | |
a723baf1 | 9564 | case RID_SIGNED: |
62d1db17 MM |
9565 | if (decl_specs) |
9566 | ++decl_specs->specs[(int) ds_signed]; | |
4b0d3cbe MM |
9567 | type = integer_type_node; |
9568 | break; | |
a723baf1 | 9569 | case RID_UNSIGNED: |
62d1db17 MM |
9570 | if (decl_specs) |
9571 | ++decl_specs->specs[(int) ds_unsigned]; | |
4b0d3cbe MM |
9572 | type = unsigned_type_node; |
9573 | break; | |
a723baf1 | 9574 | case RID_FLOAT: |
4b0d3cbe MM |
9575 | type = float_type_node; |
9576 | break; | |
a723baf1 | 9577 | case RID_DOUBLE: |
4b0d3cbe MM |
9578 | type = double_type_node; |
9579 | break; | |
a723baf1 | 9580 | case RID_VOID: |
4b0d3cbe MM |
9581 | type = void_type_node; |
9582 | break; | |
a723baf1 MM |
9583 | |
9584 | case RID_TYPEOF: | |
62d1db17 MM |
9585 | /* Consume the `typeof' token. */ |
9586 | cp_lexer_consume_token (parser->lexer); | |
9587 | /* Parse the operand to `typeof'. */ | |
9588 | type = cp_parser_sizeof_operand (parser, RID_TYPEOF); | |
9589 | /* If it is not already a TYPE, take its type. */ | |
9590 | if (!TYPE_P (type)) | |
9591 | type = finish_typeof (type); | |
9592 | ||
9593 | if (decl_specs) | |
9594 | cp_parser_set_decl_spec_type (decl_specs, type, | |
9595 | /*user_defined_p=*/true); | |
98ca843c | 9596 | |
62d1db17 | 9597 | return type; |
a723baf1 MM |
9598 | |
9599 | default: | |
9600 | break; | |
9601 | } | |
9602 | ||
4b0d3cbe MM |
9603 | /* If the type-specifier was for a built-in type, we're done. */ |
9604 | if (type) | |
9605 | { | |
9606 | tree id; | |
9607 | ||
62d1db17 MM |
9608 | /* Record the type. */ |
9609 | if (decl_specs | |
9610 | && (token->keyword != RID_SIGNED | |
9611 | && token->keyword != RID_UNSIGNED | |
9612 | && token->keyword != RID_SHORT | |
9613 | && token->keyword != RID_LONG)) | |
98ca843c | 9614 | cp_parser_set_decl_spec_type (decl_specs, |
62d1db17 MM |
9615 | type, |
9616 | /*user_defined=*/false); | |
9617 | if (decl_specs) | |
9618 | decl_specs->any_specifiers_p = true; | |
9619 | ||
4b0d3cbe MM |
9620 | /* Consume the token. */ |
9621 | id = cp_lexer_consume_token (parser->lexer)->value; | |
0d956474 GB |
9622 | |
9623 | /* There is no valid C++ program where a non-template type is | |
9624 | followed by a "<". That usually indicates that the user thought | |
9625 | that the type was a template. */ | |
9626 | cp_parser_check_for_invalid_template_id (parser, type); | |
9627 | ||
62d1db17 | 9628 | return TYPE_NAME (type); |
4b0d3cbe MM |
9629 | } |
9630 | ||
a723baf1 | 9631 | /* The type-specifier must be a user-defined type. */ |
21526606 | 9632 | if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) |
a723baf1 | 9633 | { |
0c1a1ecd | 9634 | bool qualified_p; |
f68e4dc8 | 9635 | bool global_p; |
0c1a1ecd | 9636 | |
a723baf1 MM |
9637 | /* Don't gobble tokens or issue error messages if this is an |
9638 | optional type-specifier. */ | |
9639 | if (flags & CP_PARSER_FLAGS_OPTIONAL) | |
9640 | cp_parser_parse_tentatively (parser); | |
9641 | ||
9642 | /* Look for the optional `::' operator. */ | |
f68e4dc8 | 9643 | global_p |
da740453 MM |
9644 | = (cp_parser_global_scope_opt (parser, |
9645 | /*current_scope_valid_p=*/false) | |
9646 | != NULL_TREE); | |
a723baf1 | 9647 | /* Look for the nested-name specifier. */ |
0c1a1ecd MM |
9648 | qualified_p |
9649 | = (cp_parser_nested_name_specifier_opt (parser, | |
9650 | /*typename_keyword_p=*/false, | |
9651 | /*check_dependency_p=*/true, | |
9652 | /*type_p=*/false, | |
6661a85f EB |
9653 | /*is_declaration=*/false) |
9654 | != NULL_TREE); | |
a723baf1 MM |
9655 | /* If we have seen a nested-name-specifier, and the next token |
9656 | is `template', then we are using the template-id production. */ | |
21526606 | 9657 | if (parser->scope |
a723baf1 MM |
9658 | && cp_parser_optional_template_keyword (parser)) |
9659 | { | |
9660 | /* Look for the template-id. */ | |
21526606 | 9661 | type = cp_parser_template_id (parser, |
a723baf1 | 9662 | /*template_keyword_p=*/true, |
a668c6ad MM |
9663 | /*check_dependency_p=*/true, |
9664 | /*is_declaration=*/false); | |
a723baf1 MM |
9665 | /* If the template-id did not name a type, we are out of |
9666 | luck. */ | |
9667 | if (TREE_CODE (type) != TYPE_DECL) | |
9668 | { | |
9669 | cp_parser_error (parser, "expected template-id for type"); | |
9670 | type = NULL_TREE; | |
9671 | } | |
9672 | } | |
9673 | /* Otherwise, look for a type-name. */ | |
9674 | else | |
4bb8ca28 | 9675 | type = cp_parser_type_name (parser); |
0c1a1ecd | 9676 | /* Keep track of all name-lookups performed in class scopes. */ |
98ca843c | 9677 | if (type |
f68e4dc8 | 9678 | && !global_p |
0c1a1ecd | 9679 | && !qualified_p |
98ca843c | 9680 | && TREE_CODE (type) == TYPE_DECL |
0c1a1ecd MM |
9681 | && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE) |
9682 | maybe_note_name_used_in_class (DECL_NAME (type), type); | |
a723baf1 | 9683 | /* If it didn't work out, we don't have a TYPE. */ |
21526606 | 9684 | if ((flags & CP_PARSER_FLAGS_OPTIONAL) |
a723baf1 MM |
9685 | && !cp_parser_parse_definitely (parser)) |
9686 | type = NULL_TREE; | |
62d1db17 MM |
9687 | if (type && decl_specs) |
9688 | cp_parser_set_decl_spec_type (decl_specs, type, | |
9689 | /*user_defined=*/true); | |
a723baf1 MM |
9690 | } |
9691 | ||
9692 | /* If we didn't get a type-name, issue an error message. */ | |
9693 | if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
9694 | { | |
9695 | cp_parser_error (parser, "expected type-name"); | |
9696 | return error_mark_node; | |
9697 | } | |
9698 | ||
a668c6ad MM |
9699 | /* There is no valid C++ program where a non-template type is |
9700 | followed by a "<". That usually indicates that the user thought | |
9701 | that the type was a template. */ | |
4bb8ca28 | 9702 | if (type && type != error_mark_node) |
e58a9aa1 ZL |
9703 | { |
9704 | /* As a last-ditch effort, see if TYPE is an Objective-C type. | |
9705 | If it is, then the '<'...'>' enclose protocol names rather than | |
9706 | template arguments, and so everything is fine. */ | |
9707 | if (c_dialect_objc () | |
9708 | && (objc_is_id (type) || objc_is_class_name (type))) | |
9709 | { | |
9710 | tree protos = cp_parser_objc_protocol_refs_opt (parser); | |
9711 | tree qual_type = objc_get_protocol_qualified_type (type, protos); | |
9712 | ||
9713 | /* Clobber the "unqualified" type previously entered into | |
128a79fb | 9714 | DECL_SPECS with the new, improved protocol-qualified version. */ |
e58a9aa1 ZL |
9715 | if (decl_specs) |
9716 | decl_specs->type = qual_type; | |
9717 | ||
9718 | return qual_type; | |
9719 | } | |
9720 | ||
9721 | cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type)); | |
9722 | } | |
ec75414f | 9723 | |
a723baf1 MM |
9724 | return type; |
9725 | } | |
9726 | ||
9727 | /* Parse a type-name. | |
9728 | ||
9729 | type-name: | |
9730 | class-name | |
9731 | enum-name | |
21526606 | 9732 | typedef-name |
a723baf1 MM |
9733 | |
9734 | enum-name: | |
9735 | identifier | |
9736 | ||
9737 | typedef-name: | |
21526606 | 9738 | identifier |
a723baf1 | 9739 | |
78dcd41a | 9740 | Returns a TYPE_DECL for the type. */ |
a723baf1 MM |
9741 | |
9742 | static tree | |
94edc4ab | 9743 | cp_parser_type_name (cp_parser* parser) |
a723baf1 MM |
9744 | { |
9745 | tree type_decl; | |
9746 | tree identifier; | |
9747 | ||
9748 | /* We can't know yet whether it is a class-name or not. */ | |
9749 | cp_parser_parse_tentatively (parser); | |
9750 | /* Try a class-name. */ | |
21526606 | 9751 | type_decl = cp_parser_class_name (parser, |
a723baf1 MM |
9752 | /*typename_keyword_p=*/false, |
9753 | /*template_keyword_p=*/false, | |
fc6a28d7 | 9754 | none_type, |
a723baf1 | 9755 | /*check_dependency_p=*/true, |
a668c6ad MM |
9756 | /*class_head_p=*/false, |
9757 | /*is_declaration=*/false); | |
a723baf1 MM |
9758 | /* If it's not a class-name, keep looking. */ |
9759 | if (!cp_parser_parse_definitely (parser)) | |
9760 | { | |
9761 | /* It must be a typedef-name or an enum-name. */ | |
9762 | identifier = cp_parser_identifier (parser); | |
9763 | if (identifier == error_mark_node) | |
9764 | return error_mark_node; | |
21526606 | 9765 | |
a723baf1 MM |
9766 | /* Look up the type-name. */ |
9767 | type_decl = cp_parser_lookup_name_simple (parser, identifier); | |
e58a9aa1 ZL |
9768 | |
9769 | if (TREE_CODE (type_decl) != TYPE_DECL | |
9770 | && (objc_is_id (identifier) || objc_is_class_name (identifier))) | |
9771 | { | |
9772 | /* See if this is an Objective-C type. */ | |
9773 | tree protos = cp_parser_objc_protocol_refs_opt (parser); | |
9774 | tree type = objc_get_protocol_qualified_type (identifier, protos); | |
9775 | if (type) | |
9776 | type_decl = TYPE_NAME (type); | |
9777 | } | |
9778 | ||
a723baf1 MM |
9779 | /* Issue an error if we did not find a type-name. */ |
9780 | if (TREE_CODE (type_decl) != TYPE_DECL) | |
9781 | { | |
4bb8ca28 | 9782 | if (!cp_parser_simulate_error (parser)) |
21526606 | 9783 | cp_parser_name_lookup_error (parser, identifier, type_decl, |
4bb8ca28 | 9784 | "is not a type"); |
a723baf1 MM |
9785 | type_decl = error_mark_node; |
9786 | } | |
9787 | /* Remember that the name was used in the definition of the | |
9788 | current class so that we can check later to see if the | |
9789 | meaning would have been different after the class was | |
9790 | entirely defined. */ | |
9791 | else if (type_decl != error_mark_node | |
9792 | && !parser->scope) | |
9793 | maybe_note_name_used_in_class (identifier, type_decl); | |
9794 | } | |
21526606 | 9795 | |
a723baf1 MM |
9796 | return type_decl; |
9797 | } | |
9798 | ||
9799 | ||
9800 | /* Parse an elaborated-type-specifier. Note that the grammar given | |
9801 | here incorporates the resolution to DR68. | |
9802 | ||
9803 | elaborated-type-specifier: | |
9804 | class-key :: [opt] nested-name-specifier [opt] identifier | |
9805 | class-key :: [opt] nested-name-specifier [opt] template [opt] template-id | |
9806 | enum :: [opt] nested-name-specifier [opt] identifier | |
9807 | typename :: [opt] nested-name-specifier identifier | |
21526606 EC |
9808 | typename :: [opt] nested-name-specifier template [opt] |
9809 | template-id | |
a723baf1 | 9810 | |
360d1b99 MM |
9811 | GNU extension: |
9812 | ||
9813 | elaborated-type-specifier: | |
9814 | class-key attributes :: [opt] nested-name-specifier [opt] identifier | |
21526606 | 9815 | class-key attributes :: [opt] nested-name-specifier [opt] |
360d1b99 MM |
9816 | template [opt] template-id |
9817 | enum attributes :: [opt] nested-name-specifier [opt] identifier | |
9818 | ||
a723baf1 MM |
9819 | If IS_FRIEND is TRUE, then this elaborated-type-specifier is being |
9820 | declared `friend'. If IS_DECLARATION is TRUE, then this | |
9821 | elaborated-type-specifier appears in a decl-specifiers-seq, i.e., | |
9822 | something is being declared. | |
9823 | ||
9824 | Returns the TYPE specified. */ | |
9825 | ||
9826 | static tree | |
21526606 EC |
9827 | cp_parser_elaborated_type_specifier (cp_parser* parser, |
9828 | bool is_friend, | |
94edc4ab | 9829 | bool is_declaration) |
a723baf1 MM |
9830 | { |
9831 | enum tag_types tag_type; | |
9832 | tree identifier; | |
9833 | tree type = NULL_TREE; | |
360d1b99 | 9834 | tree attributes = NULL_TREE; |
a723baf1 MM |
9835 | |
9836 | /* See if we're looking at the `enum' keyword. */ | |
9837 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM)) | |
9838 | { | |
9839 | /* Consume the `enum' token. */ | |
9840 | cp_lexer_consume_token (parser->lexer); | |
9841 | /* Remember that it's an enumeration type. */ | |
9842 | tag_type = enum_type; | |
360d1b99 MM |
9843 | /* Parse the attributes. */ |
9844 | attributes = cp_parser_attributes_opt (parser); | |
a723baf1 MM |
9845 | } |
9846 | /* Or, it might be `typename'. */ | |
9847 | else if (cp_lexer_next_token_is_keyword (parser->lexer, | |
9848 | RID_TYPENAME)) | |
9849 | { | |
9850 | /* Consume the `typename' token. */ | |
9851 | cp_lexer_consume_token (parser->lexer); | |
9852 | /* Remember that it's a `typename' type. */ | |
9853 | tag_type = typename_type; | |
9854 | /* The `typename' keyword is only allowed in templates. */ | |
9855 | if (!processing_template_decl) | |
2a13a625 | 9856 | pedwarn ("using %<typename%> outside of template"); |
a723baf1 MM |
9857 | } |
9858 | /* Otherwise it must be a class-key. */ | |
9859 | else | |
9860 | { | |
9861 | tag_type = cp_parser_class_key (parser); | |
9862 | if (tag_type == none_type) | |
9863 | return error_mark_node; | |
360d1b99 MM |
9864 | /* Parse the attributes. */ |
9865 | attributes = cp_parser_attributes_opt (parser); | |
a723baf1 MM |
9866 | } |
9867 | ||
9868 | /* Look for the `::' operator. */ | |
21526606 | 9869 | cp_parser_global_scope_opt (parser, |
a723baf1 MM |
9870 | /*current_scope_valid_p=*/false); |
9871 | /* Look for the nested-name-specifier. */ | |
9872 | if (tag_type == typename_type) | |
8fa1ad0e MM |
9873 | { |
9874 | if (cp_parser_nested_name_specifier (parser, | |
9875 | /*typename_keyword_p=*/true, | |
9876 | /*check_dependency_p=*/true, | |
a668c6ad | 9877 | /*type_p=*/true, |
21526606 | 9878 | is_declaration) |
8fa1ad0e MM |
9879 | == error_mark_node) |
9880 | return error_mark_node; | |
9881 | } | |
a723baf1 MM |
9882 | else |
9883 | /* Even though `typename' is not present, the proposed resolution | |
9884 | to Core Issue 180 says that in `class A<T>::B', `B' should be | |
9885 | considered a type-name, even if `A<T>' is dependent. */ | |
9886 | cp_parser_nested_name_specifier_opt (parser, | |
9887 | /*typename_keyword_p=*/true, | |
9888 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9889 | /*type_p=*/true, |
9890 | is_declaration); | |
a723baf1 MM |
9891 | /* For everything but enumeration types, consider a template-id. */ |
9892 | if (tag_type != enum_type) | |
9893 | { | |
9894 | bool template_p = false; | |
9895 | tree decl; | |
9896 | ||
9897 | /* Allow the `template' keyword. */ | |
9898 | template_p = cp_parser_optional_template_keyword (parser); | |
9899 | /* If we didn't see `template', we don't know if there's a | |
9900 | template-id or not. */ | |
9901 | if (!template_p) | |
9902 | cp_parser_parse_tentatively (parser); | |
9903 | /* Parse the template-id. */ | |
9904 | decl = cp_parser_template_id (parser, template_p, | |
a668c6ad MM |
9905 | /*check_dependency_p=*/true, |
9906 | is_declaration); | |
a723baf1 MM |
9907 | /* If we didn't find a template-id, look for an ordinary |
9908 | identifier. */ | |
9909 | if (!template_p && !cp_parser_parse_definitely (parser)) | |
9910 | ; | |
9911 | /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is | |
9912 | in effect, then we must assume that, upon instantiation, the | |
9913 | template will correspond to a class. */ | |
9914 | else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR | |
9915 | && tag_type == typename_type) | |
9916 | type = make_typename_type (parser->scope, decl, | |
fc6a28d7 | 9917 | typename_type, |
a723baf1 | 9918 | /*complain=*/1); |
21526606 | 9919 | else |
a723baf1 MM |
9920 | type = TREE_TYPE (decl); |
9921 | } | |
9922 | ||
9923 | /* For an enumeration type, consider only a plain identifier. */ | |
9924 | if (!type) | |
9925 | { | |
9926 | identifier = cp_parser_identifier (parser); | |
9927 | ||
9928 | if (identifier == error_mark_node) | |
eb5abb39 NS |
9929 | { |
9930 | parser->scope = NULL_TREE; | |
9931 | return error_mark_node; | |
9932 | } | |
a723baf1 MM |
9933 | |
9934 | /* For a `typename', we needn't call xref_tag. */ | |
0c88d886 MM |
9935 | if (tag_type == typename_type |
9936 | && TREE_CODE (parser->scope) != NAMESPACE_DECL) | |
21526606 | 9937 | return cp_parser_make_typename_type (parser, parser->scope, |
2097b5f2 | 9938 | identifier); |
a723baf1 MM |
9939 | /* Look up a qualified name in the usual way. */ |
9940 | if (parser->scope) | |
9941 | { | |
9942 | tree decl; | |
9943 | ||
21526606 | 9944 | decl = cp_parser_lookup_name (parser, identifier, |
fc6a28d7 | 9945 | tag_type, |
b0bc6e8e | 9946 | /*is_template=*/false, |
eea9800f | 9947 | /*is_namespace=*/false, |
8f78f01f MM |
9948 | /*check_dependency=*/true, |
9949 | /*ambiguous_p=*/NULL); | |
710b73e6 KL |
9950 | |
9951 | /* If we are parsing friend declaration, DECL may be a | |
9952 | TEMPLATE_DECL tree node here. However, we need to check | |
9953 | whether this TEMPLATE_DECL results in valid code. Consider | |
9954 | the following example: | |
9955 | ||
9956 | namespace N { | |
9957 | template <class T> class C {}; | |
9958 | } | |
9959 | class X { | |
9960 | template <class T> friend class N::C; // #1, valid code | |
9961 | }; | |
9962 | template <class T> class Y { | |
9963 | friend class N::C; // #2, invalid code | |
9964 | }; | |
9965 | ||
9966 | For both case #1 and #2, we arrive at a TEMPLATE_DECL after | |
9967 | name lookup of `N::C'. We see that friend declaration must | |
9968 | be template for the code to be valid. Note that | |
9969 | processing_template_decl does not work here since it is | |
9970 | always 1 for the above two cases. */ | |
9971 | ||
21526606 | 9972 | decl = (cp_parser_maybe_treat_template_as_class |
710b73e6 KL |
9973 | (decl, /*tag_name_p=*/is_friend |
9974 | && parser->num_template_parameter_lists)); | |
a723baf1 MM |
9975 | |
9976 | if (TREE_CODE (decl) != TYPE_DECL) | |
9977 | { | |
0c88d886 MM |
9978 | cp_parser_diagnose_invalid_type_name (parser, |
9979 | parser->scope, | |
9980 | identifier); | |
a723baf1 MM |
9981 | return error_mark_node; |
9982 | } | |
560ad596 MM |
9983 | |
9984 | if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE) | |
21526606 | 9985 | check_elaborated_type_specifier |
4b0d3cbe | 9986 | (tag_type, decl, |
560ad596 MM |
9987 | (parser->num_template_parameter_lists |
9988 | || DECL_SELF_REFERENCE_P (decl))); | |
a723baf1 MM |
9989 | |
9990 | type = TREE_TYPE (decl); | |
9991 | } | |
21526606 | 9992 | else |
a723baf1 MM |
9993 | { |
9994 | /* An elaborated-type-specifier sometimes introduces a new type and | |
9995 | sometimes names an existing type. Normally, the rule is that it | |
9996 | introduces a new type only if there is not an existing type of | |
9997 | the same name already in scope. For example, given: | |
9998 | ||
9999 | struct S {}; | |
10000 | void f() { struct S s; } | |
10001 | ||
10002 | the `struct S' in the body of `f' is the same `struct S' as in | |
10003 | the global scope; the existing definition is used. However, if | |
21526606 | 10004 | there were no global declaration, this would introduce a new |
a723baf1 MM |
10005 | local class named `S'. |
10006 | ||
10007 | An exception to this rule applies to the following code: | |
10008 | ||
10009 | namespace N { struct S; } | |
10010 | ||
10011 | Here, the elaborated-type-specifier names a new type | |
10012 | unconditionally; even if there is already an `S' in the | |
10013 | containing scope this declaration names a new type. | |
10014 | This exception only applies if the elaborated-type-specifier | |
10015 | forms the complete declaration: | |
10016 | ||
21526606 | 10017 | [class.name] |
a723baf1 MM |
10018 | |
10019 | A declaration consisting solely of `class-key identifier ;' is | |
10020 | either a redeclaration of the name in the current scope or a | |
10021 | forward declaration of the identifier as a class name. It | |
10022 | introduces the name into the current scope. | |
10023 | ||
10024 | We are in this situation precisely when the next token is a `;'. | |
10025 | ||
10026 | An exception to the exception is that a `friend' declaration does | |
10027 | *not* name a new type; i.e., given: | |
10028 | ||
10029 | struct S { friend struct T; }; | |
10030 | ||
21526606 | 10031 | `T' is not a new type in the scope of `S'. |
a723baf1 MM |
10032 | |
10033 | Also, `new struct S' or `sizeof (struct S)' never results in the | |
10034 | definition of a new type; a new type can only be declared in a | |
9bcb9aae | 10035 | declaration context. */ |
a723baf1 | 10036 | |
29ef83de KL |
10037 | tag_scope ts; |
10038 | if (is_friend) | |
10039 | /* Friends have special name lookup rules. */ | |
10040 | ts = ts_within_enclosing_non_class; | |
10041 | else if (is_declaration | |
10042 | && cp_lexer_next_token_is (parser->lexer, | |
10043 | CPP_SEMICOLON)) | |
10044 | /* This is a `class-key identifier ;' */ | |
10045 | ts = ts_current; | |
10046 | else | |
10047 | ts = ts_global; | |
10048 | ||
e0fed25b DS |
10049 | /* Warn about attributes. They are ignored. */ |
10050 | if (attributes) | |
5c498b10 DD |
10051 | warning (OPT_Wattributes, |
10052 | "type attributes are honored only at type definition"); | |
e0fed25b | 10053 | |
29ef83de | 10054 | type = xref_tag (tag_type, identifier, ts, |
cbd63935 | 10055 | parser->num_template_parameter_lists); |
a723baf1 MM |
10056 | } |
10057 | } | |
10058 | if (tag_type != enum_type) | |
10059 | cp_parser_check_class_key (tag_type, type); | |
ee43dab5 MM |
10060 | |
10061 | /* A "<" cannot follow an elaborated type specifier. If that | |
10062 | happens, the user was probably trying to form a template-id. */ | |
10063 | cp_parser_check_for_invalid_template_id (parser, type); | |
10064 | ||
a723baf1 MM |
10065 | return type; |
10066 | } | |
10067 | ||
10068 | /* Parse an enum-specifier. | |
10069 | ||
10070 | enum-specifier: | |
10071 | enum identifier [opt] { enumerator-list [opt] } | |
10072 | ||
f6af9a15 MA |
10073 | GNU Extensions: |
10074 | enum identifier [opt] { enumerator-list [opt] } attributes | |
10075 | ||
a723baf1 MM |
10076 | Returns an ENUM_TYPE representing the enumeration. */ |
10077 | ||
10078 | static tree | |
94edc4ab | 10079 | cp_parser_enum_specifier (cp_parser* parser) |
a723baf1 | 10080 | { |
ff4eb0b5 | 10081 | tree identifier; |
a723baf1 MM |
10082 | tree type; |
10083 | ||
ff4eb0b5 ZW |
10084 | /* Caller guarantees that the current token is 'enum', an identifier |
10085 | possibly follows, and the token after that is an opening brace. | |
10086 | If we don't have an identifier, fabricate an anonymous name for | |
10087 | the enumeration being defined. */ | |
10088 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 10089 | |
ff4eb0b5 | 10090 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) |
a723baf1 | 10091 | identifier = cp_parser_identifier (parser); |
ff4eb0b5 ZW |
10092 | else |
10093 | identifier = make_anon_name (); | |
a723baf1 | 10094 | |
a723baf1 MM |
10095 | /* Issue an error message if type-definitions are forbidden here. */ |
10096 | cp_parser_check_type_definition (parser); | |
10097 | ||
2cfe82fe ZW |
10098 | /* Create the new type. We do this before consuming the opening brace |
10099 | so the enum will be recorded as being on the line of its tag (or the | |
10100 | 'enum' keyword, if there is no tag). */ | |
ff4eb0b5 | 10101 | type = start_enum (identifier); |
a723baf1 | 10102 | |
2cfe82fe ZW |
10103 | /* Consume the opening brace. */ |
10104 | cp_lexer_consume_token (parser->lexer); | |
10105 | ||
ff4eb0b5 ZW |
10106 | /* If the next token is not '}', then there are some enumerators. */ |
10107 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)) | |
a723baf1 | 10108 | cp_parser_enumerator_list (parser, type); |
ff4eb0b5 ZW |
10109 | |
10110 | /* Consume the final '}'. */ | |
a723baf1 MM |
10111 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); |
10112 | ||
f6af9a15 | 10113 | /* Look for trailing attributes to apply to this enumeration, and |
03fd3f84 | 10114 | apply them if appropriate. */ |
f6af9a15 MA |
10115 | if (cp_parser_allow_gnu_extensions_p (parser)) |
10116 | { | |
10117 | tree trailing_attr = cp_parser_attributes_opt (parser); | |
10118 | cplus_decl_attributes (&type, | |
10119 | trailing_attr, | |
10120 | (int) ATTR_FLAG_TYPE_IN_PLACE); | |
10121 | } | |
10122 | ||
a723baf1 MM |
10123 | /* Finish up the enumeration. */ |
10124 | finish_enum (type); | |
10125 | ||
10126 | return type; | |
10127 | } | |
10128 | ||
10129 | /* Parse an enumerator-list. The enumerators all have the indicated | |
21526606 | 10130 | TYPE. |
a723baf1 MM |
10131 | |
10132 | enumerator-list: | |
10133 | enumerator-definition | |
10134 | enumerator-list , enumerator-definition */ | |
10135 | ||
10136 | static void | |
94edc4ab | 10137 | cp_parser_enumerator_list (cp_parser* parser, tree type) |
a723baf1 MM |
10138 | { |
10139 | while (true) | |
10140 | { | |
a723baf1 MM |
10141 | /* Parse an enumerator-definition. */ |
10142 | cp_parser_enumerator_definition (parser, type); | |
ff4eb0b5 ZW |
10143 | |
10144 | /* If the next token is not a ',', we've reached the end of | |
10145 | the list. */ | |
10146 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
a723baf1 MM |
10147 | break; |
10148 | /* Otherwise, consume the `,' and keep going. */ | |
10149 | cp_lexer_consume_token (parser->lexer); | |
10150 | /* If the next token is a `}', there is a trailing comma. */ | |
10151 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
10152 | { | |
10153 | if (pedantic && !in_system_header) | |
10154 | pedwarn ("comma at end of enumerator list"); | |
10155 | break; | |
10156 | } | |
10157 | } | |
10158 | } | |
10159 | ||
10160 | /* Parse an enumerator-definition. The enumerator has the indicated | |
10161 | TYPE. | |
10162 | ||
10163 | enumerator-definition: | |
10164 | enumerator | |
10165 | enumerator = constant-expression | |
21526606 | 10166 | |
a723baf1 MM |
10167 | enumerator: |
10168 | identifier */ | |
10169 | ||
10170 | static void | |
94edc4ab | 10171 | cp_parser_enumerator_definition (cp_parser* parser, tree type) |
a723baf1 | 10172 | { |
a723baf1 MM |
10173 | tree identifier; |
10174 | tree value; | |
10175 | ||
10176 | /* Look for the identifier. */ | |
10177 | identifier = cp_parser_identifier (parser); | |
10178 | if (identifier == error_mark_node) | |
10179 | return; | |
21526606 | 10180 | |
ff4eb0b5 ZW |
10181 | /* If the next token is an '=', then there is an explicit value. */ |
10182 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
a723baf1 MM |
10183 | { |
10184 | /* Consume the `=' token. */ | |
10185 | cp_lexer_consume_token (parser->lexer); | |
10186 | /* Parse the value. */ | |
21526606 | 10187 | value = cp_parser_constant_expression (parser, |
d17811fd | 10188 | /*allow_non_constant_p=*/false, |
14d22dd6 | 10189 | NULL); |
a723baf1 MM |
10190 | } |
10191 | else | |
10192 | value = NULL_TREE; | |
10193 | ||
10194 | /* Create the enumerator. */ | |
10195 | build_enumerator (identifier, value, type); | |
10196 | } | |
10197 | ||
10198 | /* Parse a namespace-name. | |
10199 | ||
10200 | namespace-name: | |
10201 | original-namespace-name | |
10202 | namespace-alias | |
10203 | ||
10204 | Returns the NAMESPACE_DECL for the namespace. */ | |
10205 | ||
10206 | static tree | |
94edc4ab | 10207 | cp_parser_namespace_name (cp_parser* parser) |
a723baf1 MM |
10208 | { |
10209 | tree identifier; | |
10210 | tree namespace_decl; | |
10211 | ||
10212 | /* Get the name of the namespace. */ | |
10213 | identifier = cp_parser_identifier (parser); | |
10214 | if (identifier == error_mark_node) | |
10215 | return error_mark_node; | |
10216 | ||
eea9800f MM |
10217 | /* Look up the identifier in the currently active scope. Look only |
10218 | for namespaces, due to: | |
10219 | ||
10220 | [basic.lookup.udir] | |
10221 | ||
10222 | When looking up a namespace-name in a using-directive or alias | |
21526606 | 10223 | definition, only namespace names are considered. |
eea9800f MM |
10224 | |
10225 | And: | |
10226 | ||
10227 | [basic.lookup.qual] | |
10228 | ||
10229 | During the lookup of a name preceding the :: scope resolution | |
21526606 | 10230 | operator, object, function, and enumerator names are ignored. |
eea9800f MM |
10231 | |
10232 | (Note that cp_parser_class_or_namespace_name only calls this | |
10233 | function if the token after the name is the scope resolution | |
10234 | operator.) */ | |
10235 | namespace_decl = cp_parser_lookup_name (parser, identifier, | |
fc6a28d7 | 10236 | none_type, |
b0bc6e8e | 10237 | /*is_template=*/false, |
eea9800f | 10238 | /*is_namespace=*/true, |
8f78f01f MM |
10239 | /*check_dependency=*/true, |
10240 | /*ambiguous_p=*/NULL); | |
a723baf1 MM |
10241 | /* If it's not a namespace, issue an error. */ |
10242 | if (namespace_decl == error_mark_node | |
10243 | || TREE_CODE (namespace_decl) != NAMESPACE_DECL) | |
10244 | { | |
10245 | cp_parser_error (parser, "expected namespace-name"); | |
10246 | namespace_decl = error_mark_node; | |
10247 | } | |
21526606 | 10248 | |
a723baf1 MM |
10249 | return namespace_decl; |
10250 | } | |
10251 | ||
10252 | /* Parse a namespace-definition. | |
10253 | ||
10254 | namespace-definition: | |
10255 | named-namespace-definition | |
21526606 | 10256 | unnamed-namespace-definition |
a723baf1 MM |
10257 | |
10258 | named-namespace-definition: | |
10259 | original-namespace-definition | |
10260 | extension-namespace-definition | |
10261 | ||
10262 | original-namespace-definition: | |
10263 | namespace identifier { namespace-body } | |
21526606 | 10264 | |
a723baf1 MM |
10265 | extension-namespace-definition: |
10266 | namespace original-namespace-name { namespace-body } | |
21526606 | 10267 | |
a723baf1 MM |
10268 | unnamed-namespace-definition: |
10269 | namespace { namespace-body } */ | |
10270 | ||
10271 | static void | |
94edc4ab | 10272 | cp_parser_namespace_definition (cp_parser* parser) |
a723baf1 MM |
10273 | { |
10274 | tree identifier; | |
10275 | ||
10276 | /* Look for the `namespace' keyword. */ | |
10277 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
10278 | ||
10279 | /* Get the name of the namespace. We do not attempt to distinguish | |
10280 | between an original-namespace-definition and an | |
10281 | extension-namespace-definition at this point. The semantic | |
10282 | analysis routines are responsible for that. */ | |
10283 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
10284 | identifier = cp_parser_identifier (parser); | |
10285 | else | |
10286 | identifier = NULL_TREE; | |
10287 | ||
10288 | /* Look for the `{' to start the namespace. */ | |
10289 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
10290 | /* Start the namespace. */ | |
10291 | push_namespace (identifier); | |
10292 | /* Parse the body of the namespace. */ | |
10293 | cp_parser_namespace_body (parser); | |
10294 | /* Finish the namespace. */ | |
10295 | pop_namespace (); | |
10296 | /* Look for the final `}'. */ | |
10297 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
10298 | } | |
10299 | ||
10300 | /* Parse a namespace-body. | |
10301 | ||
10302 | namespace-body: | |
10303 | declaration-seq [opt] */ | |
10304 | ||
10305 | static void | |
94edc4ab | 10306 | cp_parser_namespace_body (cp_parser* parser) |
a723baf1 MM |
10307 | { |
10308 | cp_parser_declaration_seq_opt (parser); | |
10309 | } | |
10310 | ||
10311 | /* Parse a namespace-alias-definition. | |
10312 | ||
10313 | namespace-alias-definition: | |
10314 | namespace identifier = qualified-namespace-specifier ; */ | |
10315 | ||
10316 | static void | |
94edc4ab | 10317 | cp_parser_namespace_alias_definition (cp_parser* parser) |
a723baf1 MM |
10318 | { |
10319 | tree identifier; | |
10320 | tree namespace_specifier; | |
10321 | ||
10322 | /* Look for the `namespace' keyword. */ | |
10323 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
10324 | /* Look for the identifier. */ | |
10325 | identifier = cp_parser_identifier (parser); | |
10326 | if (identifier == error_mark_node) | |
10327 | return; | |
10328 | /* Look for the `=' token. */ | |
10329 | cp_parser_require (parser, CPP_EQ, "`='"); | |
10330 | /* Look for the qualified-namespace-specifier. */ | |
21526606 | 10331 | namespace_specifier |
a723baf1 MM |
10332 | = cp_parser_qualified_namespace_specifier (parser); |
10333 | /* Look for the `;' token. */ | |
10334 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
10335 | ||
10336 | /* Register the alias in the symbol table. */ | |
10337 | do_namespace_alias (identifier, namespace_specifier); | |
10338 | } | |
10339 | ||
10340 | /* Parse a qualified-namespace-specifier. | |
10341 | ||
10342 | qualified-namespace-specifier: | |
10343 | :: [opt] nested-name-specifier [opt] namespace-name | |
10344 | ||
10345 | Returns a NAMESPACE_DECL corresponding to the specified | |
10346 | namespace. */ | |
10347 | ||
10348 | static tree | |
94edc4ab | 10349 | cp_parser_qualified_namespace_specifier (cp_parser* parser) |
a723baf1 MM |
10350 | { |
10351 | /* Look for the optional `::'. */ | |
21526606 | 10352 | cp_parser_global_scope_opt (parser, |
a723baf1 MM |
10353 | /*current_scope_valid_p=*/false); |
10354 | ||
10355 | /* Look for the optional nested-name-specifier. */ | |
10356 | cp_parser_nested_name_specifier_opt (parser, | |
10357 | /*typename_keyword_p=*/false, | |
10358 | /*check_dependency_p=*/true, | |
a668c6ad MM |
10359 | /*type_p=*/false, |
10360 | /*is_declaration=*/true); | |
a723baf1 MM |
10361 | |
10362 | return cp_parser_namespace_name (parser); | |
10363 | } | |
10364 | ||
10365 | /* Parse a using-declaration. | |
10366 | ||
10367 | using-declaration: | |
10368 | using typename [opt] :: [opt] nested-name-specifier unqualified-id ; | |
10369 | using :: unqualified-id ; */ | |
10370 | ||
10371 | static void | |
94edc4ab | 10372 | cp_parser_using_declaration (cp_parser* parser) |
a723baf1 MM |
10373 | { |
10374 | cp_token *token; | |
10375 | bool typename_p = false; | |
10376 | bool global_scope_p; | |
10377 | tree decl; | |
10378 | tree identifier; | |
ed5f054f | 10379 | tree qscope; |
a723baf1 MM |
10380 | |
10381 | /* Look for the `using' keyword. */ | |
10382 | cp_parser_require_keyword (parser, RID_USING, "`using'"); | |
21526606 | 10383 | |
a723baf1 MM |
10384 | /* Peek at the next token. */ |
10385 | token = cp_lexer_peek_token (parser->lexer); | |
10386 | /* See if it's `typename'. */ | |
10387 | if (token->keyword == RID_TYPENAME) | |
10388 | { | |
10389 | /* Remember that we've seen it. */ | |
10390 | typename_p = true; | |
10391 | /* Consume the `typename' token. */ | |
10392 | cp_lexer_consume_token (parser->lexer); | |
10393 | } | |
10394 | ||
10395 | /* Look for the optional global scope qualification. */ | |
21526606 | 10396 | global_scope_p |
a723baf1 | 10397 | = (cp_parser_global_scope_opt (parser, |
21526606 | 10398 | /*current_scope_valid_p=*/false) |
a723baf1 MM |
10399 | != NULL_TREE); |
10400 | ||
10401 | /* If we saw `typename', or didn't see `::', then there must be a | |
10402 | nested-name-specifier present. */ | |
10403 | if (typename_p || !global_scope_p) | |
21526606 | 10404 | qscope = cp_parser_nested_name_specifier (parser, typename_p, |
ed5f054f AO |
10405 | /*check_dependency_p=*/true, |
10406 | /*type_p=*/false, | |
10407 | /*is_declaration=*/true); | |
a723baf1 MM |
10408 | /* Otherwise, we could be in either of the two productions. In that |
10409 | case, treat the nested-name-specifier as optional. */ | |
10410 | else | |
ed5f054f AO |
10411 | qscope = cp_parser_nested_name_specifier_opt (parser, |
10412 | /*typename_keyword_p=*/false, | |
10413 | /*check_dependency_p=*/true, | |
10414 | /*type_p=*/false, | |
10415 | /*is_declaration=*/true); | |
10416 | if (!qscope) | |
10417 | qscope = global_namespace; | |
a723baf1 MM |
10418 | |
10419 | /* Parse the unqualified-id. */ | |
21526606 | 10420 | identifier = cp_parser_unqualified_id (parser, |
a723baf1 | 10421 | /*template_keyword_p=*/false, |
f3c2dfc6 MM |
10422 | /*check_dependency_p=*/true, |
10423 | /*declarator_p=*/true); | |
a723baf1 MM |
10424 | |
10425 | /* The function we call to handle a using-declaration is different | |
10426 | depending on what scope we are in. */ | |
f3c2dfc6 MM |
10427 | if (identifier == error_mark_node) |
10428 | ; | |
10429 | else if (TREE_CODE (identifier) != IDENTIFIER_NODE | |
10430 | && TREE_CODE (identifier) != BIT_NOT_EXPR) | |
10431 | /* [namespace.udecl] | |
10432 | ||
10433 | A using declaration shall not name a template-id. */ | |
10434 | error ("a template-id may not appear in a using-declaration"); | |
a723baf1 MM |
10435 | else |
10436 | { | |
a5201a91 | 10437 | if (at_class_scope_p ()) |
4eb6d609 | 10438 | { |
f3c2dfc6 | 10439 | /* Create the USING_DECL. */ |
1d786913 | 10440 | decl = do_class_using_decl (parser->scope, identifier); |
f3c2dfc6 MM |
10441 | /* Add it to the list of members in this class. */ |
10442 | finish_member_declaration (decl); | |
4eb6d609 | 10443 | } |
a723baf1 | 10444 | else |
f3c2dfc6 MM |
10445 | { |
10446 | decl = cp_parser_lookup_name_simple (parser, identifier); | |
10447 | if (decl == error_mark_node) | |
4bb8ca28 | 10448 | cp_parser_name_lookup_error (parser, identifier, decl, NULL); |
a5201a91 | 10449 | else if (!at_namespace_scope_p ()) |
ed5f054f | 10450 | do_local_using_decl (decl, qscope, identifier); |
f3c2dfc6 | 10451 | else |
ed5f054f | 10452 | do_toplevel_using_decl (decl, qscope, identifier); |
f3c2dfc6 | 10453 | } |
a723baf1 MM |
10454 | } |
10455 | ||
10456 | /* Look for the final `;'. */ | |
10457 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
10458 | } | |
10459 | ||
21526606 EC |
10460 | /* Parse a using-directive. |
10461 | ||
a723baf1 MM |
10462 | using-directive: |
10463 | using namespace :: [opt] nested-name-specifier [opt] | |
10464 | namespace-name ; */ | |
10465 | ||
10466 | static void | |
94edc4ab | 10467 | cp_parser_using_directive (cp_parser* parser) |
a723baf1 MM |
10468 | { |
10469 | tree namespace_decl; | |
86098eb8 | 10470 | tree attribs; |
a723baf1 MM |
10471 | |
10472 | /* Look for the `using' keyword. */ | |
10473 | cp_parser_require_keyword (parser, RID_USING, "`using'"); | |
10474 | /* And the `namespace' keyword. */ | |
10475 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
10476 | /* Look for the optional `::' operator. */ | |
10477 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); | |
34cd5ae7 | 10478 | /* And the optional nested-name-specifier. */ |
a723baf1 MM |
10479 | cp_parser_nested_name_specifier_opt (parser, |
10480 | /*typename_keyword_p=*/false, | |
10481 | /*check_dependency_p=*/true, | |
a668c6ad MM |
10482 | /*type_p=*/false, |
10483 | /*is_declaration=*/true); | |
a723baf1 MM |
10484 | /* Get the namespace being used. */ |
10485 | namespace_decl = cp_parser_namespace_name (parser); | |
86098eb8 JM |
10486 | /* And any specified attributes. */ |
10487 | attribs = cp_parser_attributes_opt (parser); | |
a723baf1 | 10488 | /* Update the symbol table. */ |
86098eb8 | 10489 | parse_using_directive (namespace_decl, attribs); |
a723baf1 MM |
10490 | /* Look for the final `;'. */ |
10491 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
10492 | } | |
10493 | ||
10494 | /* Parse an asm-definition. | |
10495 | ||
10496 | asm-definition: | |
21526606 | 10497 | asm ( string-literal ) ; |
a723baf1 MM |
10498 | |
10499 | GNU Extension: | |
10500 | ||
10501 | asm-definition: | |
10502 | asm volatile [opt] ( string-literal ) ; | |
10503 | asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ; | |
10504 | asm volatile [opt] ( string-literal : asm-operand-list [opt] | |
10505 | : asm-operand-list [opt] ) ; | |
21526606 EC |
10506 | asm volatile [opt] ( string-literal : asm-operand-list [opt] |
10507 | : asm-operand-list [opt] | |
a723baf1 MM |
10508 | : asm-operand-list [opt] ) ; */ |
10509 | ||
10510 | static void | |
94edc4ab | 10511 | cp_parser_asm_definition (cp_parser* parser) |
a723baf1 | 10512 | { |
a723baf1 MM |
10513 | tree string; |
10514 | tree outputs = NULL_TREE; | |
10515 | tree inputs = NULL_TREE; | |
10516 | tree clobbers = NULL_TREE; | |
10517 | tree asm_stmt; | |
10518 | bool volatile_p = false; | |
10519 | bool extended_p = false; | |
10520 | ||
10521 | /* Look for the `asm' keyword. */ | |
10522 | cp_parser_require_keyword (parser, RID_ASM, "`asm'"); | |
10523 | /* See if the next token is `volatile'. */ | |
10524 | if (cp_parser_allow_gnu_extensions_p (parser) | |
10525 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE)) | |
10526 | { | |
10527 | /* Remember that we saw the `volatile' keyword. */ | |
10528 | volatile_p = true; | |
10529 | /* Consume the token. */ | |
10530 | cp_lexer_consume_token (parser->lexer); | |
10531 | } | |
10532 | /* Look for the opening `('. */ | |
c162c75e MA |
10533 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) |
10534 | return; | |
a723baf1 | 10535 | /* Look for the string. */ |
c162c75e MA |
10536 | string = cp_parser_string_literal (parser, false, false); |
10537 | if (string == error_mark_node) | |
10538 | { | |
10539 | cp_parser_skip_to_closing_parenthesis (parser, true, false, | |
10540 | /*consume_paren=*/true); | |
10541 | return; | |
10542 | } | |
10543 | ||
a723baf1 | 10544 | /* If we're allowing GNU extensions, check for the extended assembly |
21526606 | 10545 | syntax. Unfortunately, the `:' tokens need not be separated by |
a723baf1 MM |
10546 | a space in C, and so, for compatibility, we tolerate that here |
10547 | too. Doing that means that we have to treat the `::' operator as | |
10548 | two `:' tokens. */ | |
10549 | if (cp_parser_allow_gnu_extensions_p (parser) | |
10550 | && at_function_scope_p () | |
10551 | && (cp_lexer_next_token_is (parser->lexer, CPP_COLON) | |
10552 | || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))) | |
10553 | { | |
10554 | bool inputs_p = false; | |
10555 | bool clobbers_p = false; | |
10556 | ||
10557 | /* The extended syntax was used. */ | |
10558 | extended_p = true; | |
10559 | ||
10560 | /* Look for outputs. */ | |
10561 | if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
10562 | { | |
10563 | /* Consume the `:'. */ | |
10564 | cp_lexer_consume_token (parser->lexer); | |
10565 | /* Parse the output-operands. */ | |
21526606 | 10566 | if (cp_lexer_next_token_is_not (parser->lexer, |
a723baf1 MM |
10567 | CPP_COLON) |
10568 | && cp_lexer_next_token_is_not (parser->lexer, | |
8caf4c38 MM |
10569 | CPP_SCOPE) |
10570 | && cp_lexer_next_token_is_not (parser->lexer, | |
10571 | CPP_CLOSE_PAREN)) | |
a723baf1 MM |
10572 | outputs = cp_parser_asm_operand_list (parser); |
10573 | } | |
10574 | /* If the next token is `::', there are no outputs, and the | |
10575 | next token is the beginning of the inputs. */ | |
10576 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
bf5930d4 JB |
10577 | /* The inputs are coming next. */ |
10578 | inputs_p = true; | |
a723baf1 MM |
10579 | |
10580 | /* Look for inputs. */ | |
10581 | if (inputs_p | |
10582 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
10583 | { | |
bf5930d4 JB |
10584 | /* Consume the `:' or `::'. */ |
10585 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 10586 | /* Parse the output-operands. */ |
21526606 | 10587 | if (cp_lexer_next_token_is_not (parser->lexer, |
a723baf1 | 10588 | CPP_COLON) |
8caf4c38 MM |
10589 | && cp_lexer_next_token_is_not (parser->lexer, |
10590 | CPP_CLOSE_PAREN)) | |
a723baf1 MM |
10591 | inputs = cp_parser_asm_operand_list (parser); |
10592 | } | |
10593 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
10594 | /* The clobbers are coming next. */ | |
10595 | clobbers_p = true; | |
10596 | ||
10597 | /* Look for clobbers. */ | |
21526606 | 10598 | if (clobbers_p |
a723baf1 MM |
10599 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) |
10600 | { | |
bf5930d4 JB |
10601 | /* Consume the `:' or `::'. */ |
10602 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 10603 | /* Parse the clobbers. */ |
8caf4c38 MM |
10604 | if (cp_lexer_next_token_is_not (parser->lexer, |
10605 | CPP_CLOSE_PAREN)) | |
10606 | clobbers = cp_parser_asm_clobber_list (parser); | |
a723baf1 MM |
10607 | } |
10608 | } | |
10609 | /* Look for the closing `)'. */ | |
10610 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
a668c6ad MM |
10611 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
10612 | /*consume_paren=*/true); | |
a723baf1 MM |
10613 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
10614 | ||
e130a54b | 10615 | /* Create the ASM_EXPR. */ |
a723baf1 MM |
10616 | if (at_function_scope_p ()) |
10617 | { | |
6de9cd9a DN |
10618 | asm_stmt = finish_asm_stmt (volatile_p, string, outputs, |
10619 | inputs, clobbers); | |
e130a54b | 10620 | /* If the extended syntax was not used, mark the ASM_EXPR. */ |
a723baf1 | 10621 | if (!extended_p) |
ca059043 AP |
10622 | { |
10623 | tree temp = asm_stmt; | |
10624 | if (TREE_CODE (temp) == CLEANUP_POINT_EXPR) | |
10625 | temp = TREE_OPERAND (temp, 0); | |
10626 | ||
10627 | ASM_INPUT_P (temp) = 1; | |
10628 | } | |
a723baf1 MM |
10629 | } |
10630 | else | |
10631 | assemble_asm (string); | |
10632 | } | |
10633 | ||
10634 | /* Declarators [gram.dcl.decl] */ | |
10635 | ||
10636 | /* Parse an init-declarator. | |
10637 | ||
10638 | init-declarator: | |
10639 | declarator initializer [opt] | |
10640 | ||
10641 | GNU Extension: | |
10642 | ||
10643 | init-declarator: | |
10644 | declarator asm-specification [opt] attributes [opt] initializer [opt] | |
10645 | ||
4bb8ca28 MM |
10646 | function-definition: |
10647 | decl-specifier-seq [opt] declarator ctor-initializer [opt] | |
21526606 EC |
10648 | function-body |
10649 | decl-specifier-seq [opt] declarator function-try-block | |
4bb8ca28 MM |
10650 | |
10651 | GNU Extension: | |
10652 | ||
10653 | function-definition: | |
21526606 | 10654 | __extension__ function-definition |
4bb8ca28 | 10655 | |
a723baf1 | 10656 | The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator. |
c8e4f0e9 | 10657 | Returns a representation of the entity declared. If MEMBER_P is TRUE, |
cf22909c KL |
10658 | then this declarator appears in a class scope. The new DECL created |
10659 | by this declarator is returned. | |
a723baf1 MM |
10660 | |
10661 | If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and | |
10662 | for a function-definition here as well. If the declarator is a | |
10663 | declarator for a function-definition, *FUNCTION_DEFINITION_P will | |
10664 | be TRUE upon return. By that point, the function-definition will | |
10665 | have been completely parsed. | |
10666 | ||
10667 | FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P | |
10668 | is FALSE. */ | |
10669 | ||
10670 | static tree | |
21526606 | 10671 | cp_parser_init_declarator (cp_parser* parser, |
62d1db17 | 10672 | cp_decl_specifier_seq *decl_specifiers, |
94edc4ab NN |
10673 | bool function_definition_allowed_p, |
10674 | bool member_p, | |
560ad596 | 10675 | int declares_class_or_enum, |
94edc4ab | 10676 | bool* function_definition_p) |
a723baf1 MM |
10677 | { |
10678 | cp_token *token; | |
058b15c1 | 10679 | cp_declarator *declarator; |
62d1db17 | 10680 | tree prefix_attributes; |
a723baf1 MM |
10681 | tree attributes; |
10682 | tree asm_specification; | |
10683 | tree initializer; | |
10684 | tree decl = NULL_TREE; | |
10685 | tree scope; | |
a723baf1 MM |
10686 | bool is_initialized; |
10687 | bool is_parenthesized_init; | |
39703eb9 | 10688 | bool is_non_constant_init; |
7efa3e22 | 10689 | int ctor_dtor_or_conv_p; |
a723baf1 | 10690 | bool friend_p; |
4514aa8c | 10691 | tree pushed_scope = NULL; |
a723baf1 | 10692 | |
62d1db17 MM |
10693 | /* Gather the attributes that were provided with the |
10694 | decl-specifiers. */ | |
10695 | prefix_attributes = decl_specifiers->attributes; | |
62d1db17 | 10696 | |
a723baf1 MM |
10697 | /* Assume that this is not the declarator for a function |
10698 | definition. */ | |
10699 | if (function_definition_p) | |
10700 | *function_definition_p = false; | |
10701 | ||
10702 | /* Defer access checks while parsing the declarator; we cannot know | |
21526606 | 10703 | what names are accessible until we know what is being |
a723baf1 | 10704 | declared. */ |
cf22909c KL |
10705 | resume_deferring_access_checks (); |
10706 | ||
a723baf1 | 10707 | /* Parse the declarator. */ |
21526606 | 10708 | declarator |
62b8a44e | 10709 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 | 10710 | &ctor_dtor_or_conv_p, |
db86dd14 MM |
10711 | /*parenthesized_p=*/NULL, |
10712 | /*member_p=*/false); | |
a723baf1 | 10713 | /* Gather up the deferred checks. */ |
cf22909c | 10714 | stop_deferring_access_checks (); |
24c0ef37 | 10715 | |
a723baf1 MM |
10716 | /* If the DECLARATOR was erroneous, there's no need to go |
10717 | further. */ | |
058b15c1 | 10718 | if (declarator == cp_error_declarator) |
cf22909c | 10719 | return error_mark_node; |
a723baf1 | 10720 | |
fc6a28d7 MM |
10721 | if (declares_class_or_enum & 2) |
10722 | cp_parser_check_for_definition_in_return_type (declarator, | |
10723 | decl_specifiers->type); | |
560ad596 | 10724 | |
a723baf1 MM |
10725 | /* Figure out what scope the entity declared by the DECLARATOR is |
10726 | located in. `grokdeclarator' sometimes changes the scope, so | |
10727 | we compute it now. */ | |
10728 | scope = get_scope_of_declarator (declarator); | |
10729 | ||
10730 | /* If we're allowing GNU extensions, look for an asm-specification | |
10731 | and attributes. */ | |
10732 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
10733 | { | |
10734 | /* Look for an asm-specification. */ | |
10735 | asm_specification = cp_parser_asm_specification_opt (parser); | |
10736 | /* And attributes. */ | |
10737 | attributes = cp_parser_attributes_opt (parser); | |
10738 | } | |
10739 | else | |
10740 | { | |
10741 | asm_specification = NULL_TREE; | |
10742 | attributes = NULL_TREE; | |
10743 | } | |
10744 | ||
10745 | /* Peek at the next token. */ | |
10746 | token = cp_lexer_peek_token (parser->lexer); | |
10747 | /* Check to see if the token indicates the start of a | |
10748 | function-definition. */ | |
10749 | if (cp_parser_token_starts_function_definition_p (token)) | |
10750 | { | |
10751 | if (!function_definition_allowed_p) | |
10752 | { | |
10753 | /* If a function-definition should not appear here, issue an | |
10754 | error message. */ | |
10755 | cp_parser_error (parser, | |
10756 | "a function-definition is not allowed here"); | |
10757 | return error_mark_node; | |
10758 | } | |
10759 | else | |
10760 | { | |
a723baf1 MM |
10761 | /* Neither attributes nor an asm-specification are allowed |
10762 | on a function-definition. */ | |
10763 | if (asm_specification) | |
10764 | error ("an asm-specification is not allowed on a function-definition"); | |
10765 | if (attributes) | |
10766 | error ("attributes are not allowed on a function-definition"); | |
10767 | /* This is a function-definition. */ | |
10768 | *function_definition_p = true; | |
10769 | ||
a723baf1 | 10770 | /* Parse the function definition. */ |
4bb8ca28 MM |
10771 | if (member_p) |
10772 | decl = cp_parser_save_member_function_body (parser, | |
10773 | decl_specifiers, | |
10774 | declarator, | |
10775 | prefix_attributes); | |
10776 | else | |
21526606 | 10777 | decl |
4bb8ca28 MM |
10778 | = (cp_parser_function_definition_from_specifiers_and_declarator |
10779 | (parser, decl_specifiers, prefix_attributes, declarator)); | |
24c0ef37 | 10780 | |
a723baf1 MM |
10781 | return decl; |
10782 | } | |
10783 | } | |
10784 | ||
10785 | /* [dcl.dcl] | |
10786 | ||
10787 | Only in function declarations for constructors, destructors, and | |
21526606 | 10788 | type conversions can the decl-specifier-seq be omitted. |
a723baf1 MM |
10789 | |
10790 | We explicitly postpone this check past the point where we handle | |
10791 | function-definitions because we tolerate function-definitions | |
10792 | that are missing their return types in some modes. */ | |
62d1db17 | 10793 | if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0) |
a723baf1 | 10794 | { |
21526606 | 10795 | cp_parser_error (parser, |
a723baf1 MM |
10796 | "expected constructor, destructor, or type conversion"); |
10797 | return error_mark_node; | |
10798 | } | |
10799 | ||
10800 | /* An `=' or an `(' indicates an initializer. */ | |
21526606 | 10801 | is_initialized = (token->type == CPP_EQ |
a723baf1 MM |
10802 | || token->type == CPP_OPEN_PAREN); |
10803 | /* If the init-declarator isn't initialized and isn't followed by a | |
10804 | `,' or `;', it's not a valid init-declarator. */ | |
21526606 | 10805 | if (!is_initialized |
a723baf1 MM |
10806 | && token->type != CPP_COMMA |
10807 | && token->type != CPP_SEMICOLON) | |
10808 | { | |
996c2b52 | 10809 | cp_parser_error (parser, "expected initializer"); |
a723baf1 MM |
10810 | return error_mark_node; |
10811 | } | |
10812 | ||
10813 | /* Because start_decl has side-effects, we should only call it if we | |
10814 | know we're going ahead. By this point, we know that we cannot | |
10815 | possibly be looking at any other construct. */ | |
10816 | cp_parser_commit_to_tentative_parse (parser); | |
10817 | ||
e90c7b84 ILT |
10818 | /* If the decl specifiers were bad, issue an error now that we're |
10819 | sure this was intended to be a declarator. Then continue | |
10820 | declaring the variable(s), as int, to try to cut down on further | |
10821 | errors. */ | |
62d1db17 MM |
10822 | if (decl_specifiers->any_specifiers_p |
10823 | && decl_specifiers->type == error_mark_node) | |
e90c7b84 ILT |
10824 | { |
10825 | cp_parser_error (parser, "invalid type in declaration"); | |
62d1db17 | 10826 | decl_specifiers->type = integer_type_node; |
e90c7b84 ILT |
10827 | } |
10828 | ||
a723baf1 MM |
10829 | /* Check to see whether or not this declaration is a friend. */ |
10830 | friend_p = cp_parser_friend_p (decl_specifiers); | |
10831 | ||
10832 | /* Check that the number of template-parameter-lists is OK. */ | |
ee3071ef | 10833 | if (!cp_parser_check_declarator_template_parameters (parser, declarator)) |
cf22909c | 10834 | return error_mark_node; |
a723baf1 MM |
10835 | |
10836 | /* Enter the newly declared entry in the symbol table. If we're | |
10837 | processing a declaration in a class-specifier, we wait until | |
10838 | after processing the initializer. */ | |
10839 | if (!member_p) | |
10840 | { | |
10841 | if (parser->in_unbraced_linkage_specification_p) | |
10842 | { | |
62d1db17 | 10843 | decl_specifiers->storage_class = sc_extern; |
a723baf1 MM |
10844 | have_extern_spec = false; |
10845 | } | |
ee3071ef | 10846 | decl = start_decl (declarator, decl_specifiers, |
73a8adb6 | 10847 | is_initialized, attributes, prefix_attributes, |
4514aa8c | 10848 | &pushed_scope); |
a723baf1 | 10849 | } |
73a8adb6 MM |
10850 | else if (scope) |
10851 | /* Enter the SCOPE. That way unqualified names appearing in the | |
10852 | initializer will be looked up in SCOPE. */ | |
4514aa8c | 10853 | pushed_scope = push_scope (scope); |
a723baf1 MM |
10854 | |
10855 | /* Perform deferred access control checks, now that we know in which | |
10856 | SCOPE the declared entity resides. */ | |
21526606 | 10857 | if (!member_p && decl) |
a723baf1 MM |
10858 | { |
10859 | tree saved_current_function_decl = NULL_TREE; | |
10860 | ||
10861 | /* If the entity being declared is a function, pretend that we | |
10862 | are in its scope. If it is a `friend', it may have access to | |
9bcb9aae | 10863 | things that would not otherwise be accessible. */ |
a723baf1 MM |
10864 | if (TREE_CODE (decl) == FUNCTION_DECL) |
10865 | { | |
10866 | saved_current_function_decl = current_function_decl; | |
10867 | current_function_decl = decl; | |
10868 | } | |
21526606 | 10869 | |
cf22909c KL |
10870 | /* Perform the access control checks for the declarator and the |
10871 | the decl-specifiers. */ | |
10872 | perform_deferred_access_checks (); | |
a723baf1 MM |
10873 | |
10874 | /* Restore the saved value. */ | |
10875 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
10876 | current_function_decl = saved_current_function_decl; | |
10877 | } | |
10878 | ||
10879 | /* Parse the initializer. */ | |
10880 | if (is_initialized) | |
21526606 | 10881 | initializer = cp_parser_initializer (parser, |
39703eb9 MM |
10882 | &is_parenthesized_init, |
10883 | &is_non_constant_init); | |
a723baf1 MM |
10884 | else |
10885 | { | |
10886 | initializer = NULL_TREE; | |
10887 | is_parenthesized_init = false; | |
39703eb9 | 10888 | is_non_constant_init = true; |
a723baf1 MM |
10889 | } |
10890 | ||
10891 | /* The old parser allows attributes to appear after a parenthesized | |
10892 | initializer. Mark Mitchell proposed removing this functionality | |
10893 | on the GCC mailing lists on 2002-08-13. This parser accepts the | |
10894 | attributes -- but ignores them. */ | |
10895 | if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init) | |
10896 | if (cp_parser_attributes_opt (parser)) | |
5c498b10 DD |
10897 | warning (OPT_Wattributes, |
10898 | "attributes after parenthesized initializer ignored"); | |
a723baf1 | 10899 | |
a723baf1 MM |
10900 | /* For an in-class declaration, use `grokfield' to create the |
10901 | declaration. */ | |
10902 | if (member_p) | |
8db1028e | 10903 | { |
4514aa8c | 10904 | if (pushed_scope) |
62a4d942 | 10905 | { |
4514aa8c NS |
10906 | pop_scope (pushed_scope); |
10907 | pushed_scope = false; | |
62a4d942 | 10908 | } |
8db1028e NS |
10909 | decl = grokfield (declarator, decl_specifiers, |
10910 | initializer, /*asmspec=*/NULL_TREE, | |
a723baf1 | 10911 | /*attributes=*/NULL_TREE); |
8db1028e NS |
10912 | if (decl && TREE_CODE (decl) == FUNCTION_DECL) |
10913 | cp_parser_save_default_args (parser, decl); | |
10914 | } | |
21526606 | 10915 | |
a723baf1 MM |
10916 | /* Finish processing the declaration. But, skip friend |
10917 | declarations. */ | |
550205c3 | 10918 | if (!friend_p && decl && decl != error_mark_node) |
73a8adb6 MM |
10919 | { |
10920 | cp_finish_decl (decl, | |
10921 | initializer, | |
10922 | asm_specification, | |
10923 | /* If the initializer is in parentheses, then this is | |
10924 | a direct-initialization, which means that an | |
10925 | `explicit' constructor is OK. Otherwise, an | |
10926 | `explicit' constructor cannot be used. */ | |
10927 | ((is_parenthesized_init || !is_initialized) | |
a723baf1 | 10928 | ? 0 : LOOKUP_ONLYCONVERTING)); |
73a8adb6 | 10929 | } |
4514aa8c NS |
10930 | if (!friend_p && pushed_scope) |
10931 | pop_scope (pushed_scope); | |
a723baf1 | 10932 | |
39703eb9 MM |
10933 | /* Remember whether or not variables were initialized by |
10934 | constant-expressions. */ | |
21526606 | 10935 | if (decl && TREE_CODE (decl) == VAR_DECL |
39703eb9 MM |
10936 | && is_initialized && !is_non_constant_init) |
10937 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; | |
10938 | ||
a723baf1 MM |
10939 | return decl; |
10940 | } | |
10941 | ||
10942 | /* Parse a declarator. | |
21526606 | 10943 | |
a723baf1 MM |
10944 | declarator: |
10945 | direct-declarator | |
21526606 | 10946 | ptr-operator declarator |
a723baf1 MM |
10947 | |
10948 | abstract-declarator: | |
10949 | ptr-operator abstract-declarator [opt] | |
10950 | direct-abstract-declarator | |
10951 | ||
10952 | GNU Extensions: | |
10953 | ||
10954 | declarator: | |
10955 | attributes [opt] direct-declarator | |
21526606 | 10956 | attributes [opt] ptr-operator declarator |
a723baf1 MM |
10957 | |
10958 | abstract-declarator: | |
10959 | attributes [opt] ptr-operator abstract-declarator [opt] | |
10960 | attributes [opt] direct-abstract-declarator | |
21526606 | 10961 | |
7efa3e22 NS |
10962 | If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to |
10963 | detect constructor, destructor or conversion operators. It is set | |
10964 | to -1 if the declarator is a name, and +1 if it is a | |
10965 | function. Otherwise it is set to zero. Usually you just want to | |
10966 | test for >0, but internally the negative value is used. | |
21526606 | 10967 | |
a723baf1 MM |
10968 | (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have |
10969 | a decl-specifier-seq unless it declares a constructor, destructor, | |
10970 | or conversion. It might seem that we could check this condition in | |
10971 | semantic analysis, rather than parsing, but that makes it difficult | |
10972 | to handle something like `f()'. We want to notice that there are | |
10973 | no decl-specifiers, and therefore realize that this is an | |
21526606 EC |
10974 | expression, not a declaration.) |
10975 | ||
4bb8ca28 | 10976 | If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff |
db86dd14 MM |
10977 | the declarator is a direct-declarator of the form "(...)". |
10978 | ||
10979 | MEMBER_P is true iff this declarator is a member-declarator. */ | |
a723baf1 | 10980 | |
058b15c1 | 10981 | static cp_declarator * |
21526606 EC |
10982 | cp_parser_declarator (cp_parser* parser, |
10983 | cp_parser_declarator_kind dcl_kind, | |
4bb8ca28 | 10984 | int* ctor_dtor_or_conv_p, |
db86dd14 MM |
10985 | bool* parenthesized_p, |
10986 | bool member_p) | |
a723baf1 MM |
10987 | { |
10988 | cp_token *token; | |
058b15c1 | 10989 | cp_declarator *declarator; |
a723baf1 | 10990 | enum tree_code code; |
3c01e5df | 10991 | cp_cv_quals cv_quals; |
a723baf1 MM |
10992 | tree class_type; |
10993 | tree attributes = NULL_TREE; | |
10994 | ||
10995 | /* Assume this is not a constructor, destructor, or type-conversion | |
10996 | operator. */ | |
10997 | if (ctor_dtor_or_conv_p) | |
7efa3e22 | 10998 | *ctor_dtor_or_conv_p = 0; |
a723baf1 MM |
10999 | |
11000 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
11001 | attributes = cp_parser_attributes_opt (parser); | |
21526606 | 11002 | |
a723baf1 MM |
11003 | /* Peek at the next token. */ |
11004 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 11005 | |
a723baf1 MM |
11006 | /* Check for the ptr-operator production. */ |
11007 | cp_parser_parse_tentatively (parser); | |
11008 | /* Parse the ptr-operator. */ | |
21526606 EC |
11009 | code = cp_parser_ptr_operator (parser, |
11010 | &class_type, | |
3c01e5df | 11011 | &cv_quals); |
a723baf1 MM |
11012 | /* If that worked, then we have a ptr-operator. */ |
11013 | if (cp_parser_parse_definitely (parser)) | |
11014 | { | |
4bb8ca28 MM |
11015 | /* If a ptr-operator was found, then this declarator was not |
11016 | parenthesized. */ | |
11017 | if (parenthesized_p) | |
11018 | *parenthesized_p = true; | |
a723baf1 MM |
11019 | /* The dependent declarator is optional if we are parsing an |
11020 | abstract-declarator. */ | |
62b8a44e | 11021 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
a723baf1 MM |
11022 | cp_parser_parse_tentatively (parser); |
11023 | ||
11024 | /* Parse the dependent declarator. */ | |
62b8a44e | 11025 | declarator = cp_parser_declarator (parser, dcl_kind, |
4bb8ca28 | 11026 | /*ctor_dtor_or_conv_p=*/NULL, |
db86dd14 MM |
11027 | /*parenthesized_p=*/NULL, |
11028 | /*member_p=*/false); | |
a723baf1 MM |
11029 | |
11030 | /* If we are parsing an abstract-declarator, we must handle the | |
11031 | case where the dependent declarator is absent. */ | |
62b8a44e NS |
11032 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED |
11033 | && !cp_parser_parse_definitely (parser)) | |
058b15c1 | 11034 | declarator = NULL; |
21526606 | 11035 | |
a723baf1 | 11036 | /* Build the representation of the ptr-operator. */ |
058b15c1 | 11037 | if (class_type) |
3c01e5df | 11038 | declarator = make_ptrmem_declarator (cv_quals, |
058b15c1 MM |
11039 | class_type, |
11040 | declarator); | |
11041 | else if (code == INDIRECT_REF) | |
3c01e5df | 11042 | declarator = make_pointer_declarator (cv_quals, declarator); |
a723baf1 | 11043 | else |
3c01e5df | 11044 | declarator = make_reference_declarator (cv_quals, declarator); |
a723baf1 MM |
11045 | } |
11046 | /* Everything else is a direct-declarator. */ | |
11047 | else | |
4bb8ca28 MM |
11048 | { |
11049 | if (parenthesized_p) | |
11050 | *parenthesized_p = cp_lexer_next_token_is (parser->lexer, | |
11051 | CPP_OPEN_PAREN); | |
11052 | declarator = cp_parser_direct_declarator (parser, dcl_kind, | |
db86dd14 MM |
11053 | ctor_dtor_or_conv_p, |
11054 | member_p); | |
4bb8ca28 | 11055 | } |
a723baf1 | 11056 | |
058b15c1 MM |
11057 | if (attributes && declarator != cp_error_declarator) |
11058 | declarator->attributes = attributes; | |
21526606 | 11059 | |
a723baf1 MM |
11060 | return declarator; |
11061 | } | |
11062 | ||
11063 | /* Parse a direct-declarator or direct-abstract-declarator. | |
11064 | ||
11065 | direct-declarator: | |
11066 | declarator-id | |
11067 | direct-declarator ( parameter-declaration-clause ) | |
21526606 | 11068 | cv-qualifier-seq [opt] |
a723baf1 MM |
11069 | exception-specification [opt] |
11070 | direct-declarator [ constant-expression [opt] ] | |
21526606 | 11071 | ( declarator ) |
a723baf1 MM |
11072 | |
11073 | direct-abstract-declarator: | |
11074 | direct-abstract-declarator [opt] | |
21526606 | 11075 | ( parameter-declaration-clause ) |
a723baf1 MM |
11076 | cv-qualifier-seq [opt] |
11077 | exception-specification [opt] | |
11078 | direct-abstract-declarator [opt] [ constant-expression [opt] ] | |
11079 | ( abstract-declarator ) | |
11080 | ||
62b8a44e NS |
11081 | Returns a representation of the declarator. DCL_KIND is |
11082 | CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a | |
11083 | direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if | |
11084 | we are parsing a direct-declarator. It is | |
11085 | CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case | |
11086 | of ambiguity we prefer an abstract declarator, as per | |
db86dd14 | 11087 | [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for |
058b15c1 | 11088 | cp_parser_declarator. */ |
a723baf1 | 11089 | |
058b15c1 | 11090 | static cp_declarator * |
94edc4ab NN |
11091 | cp_parser_direct_declarator (cp_parser* parser, |
11092 | cp_parser_declarator_kind dcl_kind, | |
db86dd14 MM |
11093 | int* ctor_dtor_or_conv_p, |
11094 | bool member_p) | |
a723baf1 MM |
11095 | { |
11096 | cp_token *token; | |
058b15c1 | 11097 | cp_declarator *declarator = NULL; |
a723baf1 MM |
11098 | tree scope = NULL_TREE; |
11099 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
11100 | bool saved_in_declarator_p = parser->in_declarator_p; | |
62b8a44e | 11101 | bool first = true; |
4514aa8c | 11102 | tree pushed_scope = NULL_TREE; |
21526606 | 11103 | |
62b8a44e | 11104 | while (true) |
a723baf1 | 11105 | { |
62b8a44e NS |
11106 | /* Peek at the next token. */ |
11107 | token = cp_lexer_peek_token (parser->lexer); | |
11108 | if (token->type == CPP_OPEN_PAREN) | |
a723baf1 | 11109 | { |
62b8a44e NS |
11110 | /* This is either a parameter-declaration-clause, or a |
11111 | parenthesized declarator. When we know we are parsing a | |
34cd5ae7 | 11112 | named declarator, it must be a parenthesized declarator |
62b8a44e NS |
11113 | if FIRST is true. For instance, `(int)' is a |
11114 | parameter-declaration-clause, with an omitted | |
11115 | direct-abstract-declarator. But `((*))', is a | |
11116 | parenthesized abstract declarator. Finally, when T is a | |
11117 | template parameter `(T)' is a | |
34cd5ae7 | 11118 | parameter-declaration-clause, and not a parenthesized |
62b8a44e | 11119 | named declarator. |
21526606 | 11120 | |
62b8a44e NS |
11121 | We first try and parse a parameter-declaration-clause, |
11122 | and then try a nested declarator (if FIRST is true). | |
a723baf1 | 11123 | |
62b8a44e NS |
11124 | It is not an error for it not to be a |
11125 | parameter-declaration-clause, even when FIRST is | |
11126 | false. Consider, | |
11127 | ||
11128 | int i (int); | |
11129 | int i (3); | |
11130 | ||
11131 | The first is the declaration of a function while the | |
11132 | second is a the definition of a variable, including its | |
11133 | initializer. | |
11134 | ||
11135 | Having seen only the parenthesis, we cannot know which of | |
11136 | these two alternatives should be selected. Even more | |
11137 | complex are examples like: | |
11138 | ||
11139 | int i (int (a)); | |
11140 | int i (int (3)); | |
11141 | ||
11142 | The former is a function-declaration; the latter is a | |
21526606 | 11143 | variable initialization. |
62b8a44e | 11144 | |
34cd5ae7 | 11145 | Thus again, we try a parameter-declaration-clause, and if |
62b8a44e NS |
11146 | that fails, we back out and return. */ |
11147 | ||
11148 | if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) | |
a723baf1 | 11149 | { |
058b15c1 | 11150 | cp_parameter_declarator *params; |
4047b164 | 11151 | unsigned saved_num_template_parameter_lists; |
21526606 | 11152 | |
db86dd14 MM |
11153 | /* In a member-declarator, the only valid interpretation |
11154 | of a parenthesis is the start of a | |
11155 | parameter-declaration-clause. (It is invalid to | |
11156 | initialize a static data member with a parenthesized | |
11157 | initializer; only the "=" form of initialization is | |
11158 | permitted.) */ | |
11159 | if (!member_p) | |
11160 | cp_parser_parse_tentatively (parser); | |
a723baf1 | 11161 | |
62b8a44e NS |
11162 | /* Consume the `('. */ |
11163 | cp_lexer_consume_token (parser->lexer); | |
11164 | if (first) | |
11165 | { | |
11166 | /* If this is going to be an abstract declarator, we're | |
11167 | in a declarator and we can't have default args. */ | |
11168 | parser->default_arg_ok_p = false; | |
11169 | parser->in_declarator_p = true; | |
11170 | } | |
21526606 | 11171 | |
4047b164 KL |
11172 | /* Inside the function parameter list, surrounding |
11173 | template-parameter-lists do not apply. */ | |
11174 | saved_num_template_parameter_lists | |
11175 | = parser->num_template_parameter_lists; | |
11176 | parser->num_template_parameter_lists = 0; | |
11177 | ||
62b8a44e NS |
11178 | /* Parse the parameter-declaration-clause. */ |
11179 | params = cp_parser_parameter_declaration_clause (parser); | |
11180 | ||
4047b164 KL |
11181 | parser->num_template_parameter_lists |
11182 | = saved_num_template_parameter_lists; | |
11183 | ||
62b8a44e | 11184 | /* If all went well, parse the cv-qualifier-seq and the |
34cd5ae7 | 11185 | exception-specification. */ |
db86dd14 | 11186 | if (member_p || cp_parser_parse_definitely (parser)) |
62b8a44e | 11187 | { |
3c01e5df | 11188 | cp_cv_quals cv_quals; |
62b8a44e | 11189 | tree exception_specification; |
7efa3e22 NS |
11190 | |
11191 | if (ctor_dtor_or_conv_p) | |
11192 | *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0; | |
62b8a44e NS |
11193 | first = false; |
11194 | /* Consume the `)'. */ | |
11195 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
11196 | ||
11197 | /* Parse the cv-qualifier-seq. */ | |
3c01e5df | 11198 | cv_quals = cp_parser_cv_qualifier_seq_opt (parser); |
62b8a44e | 11199 | /* And the exception-specification. */ |
21526606 | 11200 | exception_specification |
62b8a44e NS |
11201 | = cp_parser_exception_specification_opt (parser); |
11202 | ||
11203 | /* Create the function-declarator. */ | |
11204 | declarator = make_call_declarator (declarator, | |
11205 | params, | |
3c01e5df | 11206 | cv_quals, |
62b8a44e NS |
11207 | exception_specification); |
11208 | /* Any subsequent parameter lists are to do with | |
11209 | return type, so are not those of the declared | |
11210 | function. */ | |
11211 | parser->default_arg_ok_p = false; | |
21526606 | 11212 | |
62b8a44e NS |
11213 | /* Repeat the main loop. */ |
11214 | continue; | |
11215 | } | |
11216 | } | |
21526606 | 11217 | |
62b8a44e NS |
11218 | /* If this is the first, we can try a parenthesized |
11219 | declarator. */ | |
11220 | if (first) | |
a723baf1 | 11221 | { |
a7324e75 MM |
11222 | bool saved_in_type_id_in_expr_p; |
11223 | ||
a723baf1 | 11224 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
62b8a44e | 11225 | parser->in_declarator_p = saved_in_declarator_p; |
21526606 | 11226 | |
62b8a44e NS |
11227 | /* Consume the `('. */ |
11228 | cp_lexer_consume_token (parser->lexer); | |
11229 | /* Parse the nested declarator. */ | |
a7324e75 MM |
11230 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
11231 | parser->in_type_id_in_expr_p = true; | |
21526606 | 11232 | declarator |
4bb8ca28 | 11233 | = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p, |
db86dd14 MM |
11234 | /*parenthesized_p=*/NULL, |
11235 | member_p); | |
a7324e75 | 11236 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
62b8a44e NS |
11237 | first = false; |
11238 | /* Expect a `)'. */ | |
11239 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
058b15c1 MM |
11240 | declarator = cp_error_declarator; |
11241 | if (declarator == cp_error_declarator) | |
62b8a44e | 11242 | break; |
21526606 | 11243 | |
62b8a44e | 11244 | goto handle_declarator; |
a723baf1 | 11245 | } |
9bcb9aae | 11246 | /* Otherwise, we must be done. */ |
62b8a44e NS |
11247 | else |
11248 | break; | |
a723baf1 | 11249 | } |
62b8a44e NS |
11250 | else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
11251 | && token->type == CPP_OPEN_SQUARE) | |
a723baf1 | 11252 | { |
62b8a44e | 11253 | /* Parse an array-declarator. */ |
a723baf1 MM |
11254 | tree bounds; |
11255 | ||
7efa3e22 NS |
11256 | if (ctor_dtor_or_conv_p) |
11257 | *ctor_dtor_or_conv_p = 0; | |
21526606 | 11258 | |
62b8a44e NS |
11259 | first = false; |
11260 | parser->default_arg_ok_p = false; | |
11261 | parser->in_declarator_p = true; | |
a723baf1 MM |
11262 | /* Consume the `['. */ |
11263 | cp_lexer_consume_token (parser->lexer); | |
11264 | /* Peek at the next token. */ | |
11265 | token = cp_lexer_peek_token (parser->lexer); | |
11266 | /* If the next token is `]', then there is no | |
11267 | constant-expression. */ | |
11268 | if (token->type != CPP_CLOSE_SQUARE) | |
14d22dd6 MM |
11269 | { |
11270 | bool non_constant_p; | |
11271 | ||
21526606 | 11272 | bounds |
14d22dd6 MM |
11273 | = cp_parser_constant_expression (parser, |
11274 | /*allow_non_constant=*/true, | |
11275 | &non_constant_p); | |
d17811fd | 11276 | if (!non_constant_p) |
9baa27a9 | 11277 | bounds = fold_non_dependent_expr (bounds); |
88e95ee3 MM |
11278 | /* Normally, the array bound must be an integral constant |
11279 | expression. However, as an extension, we allow VLAs | |
b671e5a4 MM |
11280 | in function scopes. */ |
11281 | else if (!at_function_scope_p ()) | |
44370687 MM |
11282 | { |
11283 | error ("array bound is not an integer constant"); | |
11284 | bounds = error_mark_node; | |
11285 | } | |
14d22dd6 | 11286 | } |
a723baf1 MM |
11287 | else |
11288 | bounds = NULL_TREE; | |
11289 | /* Look for the closing `]'. */ | |
62b8a44e NS |
11290 | if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'")) |
11291 | { | |
058b15c1 | 11292 | declarator = cp_error_declarator; |
62b8a44e NS |
11293 | break; |
11294 | } | |
a723baf1 | 11295 | |
058b15c1 | 11296 | declarator = make_array_declarator (declarator, bounds); |
a723baf1 | 11297 | } |
62b8a44e | 11298 | else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT) |
a723baf1 | 11299 | { |
1d786913 MM |
11300 | tree qualifying_scope; |
11301 | tree unqualified_name; | |
058b15c1 | 11302 | |
a668c6ad | 11303 | /* Parse a declarator-id */ |
62b8a44e NS |
11304 | if (dcl_kind == CP_PARSER_DECLARATOR_EITHER) |
11305 | cp_parser_parse_tentatively (parser); | |
1d786913 MM |
11306 | unqualified_name = cp_parser_declarator_id (parser); |
11307 | qualifying_scope = parser->scope; | |
712becab NS |
11308 | if (dcl_kind == CP_PARSER_DECLARATOR_EITHER) |
11309 | { | |
11310 | if (!cp_parser_parse_definitely (parser)) | |
1d786913 MM |
11311 | unqualified_name = error_mark_node; |
11312 | else if (qualifying_scope | |
11313 | || (TREE_CODE (unqualified_name) | |
11314 | != IDENTIFIER_NODE)) | |
712becab NS |
11315 | { |
11316 | cp_parser_error (parser, "expected unqualified-id"); | |
1d786913 | 11317 | unqualified_name = error_mark_node; |
712becab NS |
11318 | } |
11319 | } | |
21526606 | 11320 | |
1d786913 | 11321 | if (unqualified_name == error_mark_node) |
058b15c1 MM |
11322 | { |
11323 | declarator = cp_error_declarator; | |
11324 | break; | |
11325 | } | |
21526606 | 11326 | |
1d786913 MM |
11327 | if (qualifying_scope && at_namespace_scope_p () |
11328 | && TREE_CODE (qualifying_scope) == TYPENAME_TYPE) | |
62b8a44e | 11329 | { |
62b8a44e NS |
11330 | /* In the declaration of a member of a template class |
11331 | outside of the class itself, the SCOPE will sometimes | |
11332 | be a TYPENAME_TYPE. For example, given: | |
21526606 | 11333 | |
62b8a44e NS |
11334 | template <typename T> |
11335 | int S<T>::R::i = 3; | |
21526606 | 11336 | |
62b8a44e NS |
11337 | the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In |
11338 | this context, we must resolve S<T>::R to an ordinary | |
11339 | type, rather than a typename type. | |
21526606 | 11340 | |
62b8a44e NS |
11341 | The reason we normally avoid resolving TYPENAME_TYPEs |
11342 | is that a specialization of `S' might render | |
11343 | `S<T>::R' not a type. However, if `S' is | |
11344 | specialized, then this `i' will not be used, so there | |
11345 | is no harm in resolving the types here. */ | |
1d786913 MM |
11346 | tree type; |
11347 | ||
11348 | /* Resolve the TYPENAME_TYPE. */ | |
11349 | type = resolve_typename_type (qualifying_scope, | |
11350 | /*only_current_p=*/false); | |
11351 | /* If that failed, the declarator is invalid. */ | |
11352 | if (type == error_mark_node) | |
11353 | error ("%<%T::%D%> is not a type", | |
11354 | TYPE_CONTEXT (qualifying_scope), | |
11355 | TYPE_IDENTIFIER (qualifying_scope)); | |
11356 | qualifying_scope = type; | |
62b8a44e | 11357 | } |
21526606 | 11358 | |
1d786913 MM |
11359 | declarator = make_id_declarator (qualifying_scope, |
11360 | unqualified_name); | |
b68b6828 | 11361 | declarator->id_loc = token->location; |
1d786913 | 11362 | if (unqualified_name) |
a723baf1 | 11363 | { |
62b8a44e NS |
11364 | tree class_type; |
11365 | ||
1d786913 MM |
11366 | if (qualifying_scope |
11367 | && CLASS_TYPE_P (qualifying_scope)) | |
11368 | class_type = qualifying_scope; | |
62b8a44e | 11369 | else |
1d786913 | 11370 | class_type = current_class_type; |
62b8a44e | 11371 | |
058b15c1 MM |
11372 | if (class_type) |
11373 | { | |
11374 | if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR) | |
11375 | declarator->u.id.sfk = sfk_destructor; | |
11376 | else if (IDENTIFIER_TYPENAME_P (unqualified_name)) | |
11377 | declarator->u.id.sfk = sfk_conversion; | |
27d6592c MM |
11378 | else if (/* There's no way to declare a constructor |
11379 | for an anonymous type, even if the type | |
11380 | got a name for linkage purposes. */ | |
11381 | !TYPE_WAS_ANONYMOUS (class_type) | |
11382 | && (constructor_name_p (unqualified_name, | |
11383 | class_type) | |
11384 | || (TREE_CODE (unqualified_name) == TYPE_DECL | |
11385 | && (same_type_p | |
11386 | (TREE_TYPE (unqualified_name), | |
11387 | class_type))))) | |
058b15c1 MM |
11388 | declarator->u.id.sfk = sfk_constructor; |
11389 | ||
11390 | if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none) | |
11391 | *ctor_dtor_or_conv_p = -1; | |
1d786913 | 11392 | if (qualifying_scope |
98ca843c | 11393 | && TREE_CODE (unqualified_name) == TYPE_DECL |
058b15c1 MM |
11394 | && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name))) |
11395 | { | |
11396 | error ("invalid use of constructor as a template"); | |
2a13a625 GDR |
11397 | inform ("use %<%T::%D%> instead of %<%T::%T%> to name " |
11398 | "the constructor in a qualified name", | |
11399 | class_type, | |
058b15c1 MM |
11400 | DECL_NAME (TYPE_TI_TEMPLATE (class_type)), |
11401 | class_type, class_type); | |
11402 | } | |
11403 | } | |
a723baf1 | 11404 | } |
62b8a44e NS |
11405 | |
11406 | handle_declarator:; | |
11407 | scope = get_scope_of_declarator (declarator); | |
11408 | if (scope) | |
91b004e5 MM |
11409 | /* Any names that appear after the declarator-id for a |
11410 | member are looked up in the containing scope. */ | |
4514aa8c | 11411 | pushed_scope = push_scope (scope); |
62b8a44e NS |
11412 | parser->in_declarator_p = true; |
11413 | if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p) | |
058b15c1 | 11414 | || (declarator && declarator->kind == cdk_id)) |
62b8a44e NS |
11415 | /* Default args are only allowed on function |
11416 | declarations. */ | |
11417 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
a723baf1 | 11418 | else |
62b8a44e NS |
11419 | parser->default_arg_ok_p = false; |
11420 | ||
11421 | first = false; | |
a723baf1 | 11422 | } |
62b8a44e | 11423 | /* We're done. */ |
a723baf1 MM |
11424 | else |
11425 | break; | |
a723baf1 MM |
11426 | } |
11427 | ||
11428 | /* For an abstract declarator, we might wind up with nothing at this | |
11429 | point. That's an error; the declarator is not optional. */ | |
11430 | if (!declarator) | |
11431 | cp_parser_error (parser, "expected declarator"); | |
11432 | ||
11433 | /* If we entered a scope, we must exit it now. */ | |
4514aa8c NS |
11434 | if (pushed_scope) |
11435 | pop_scope (pushed_scope); | |
a723baf1 MM |
11436 | |
11437 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
11438 | parser->in_declarator_p = saved_in_declarator_p; | |
21526606 | 11439 | |
a723baf1 MM |
11440 | return declarator; |
11441 | } | |
11442 | ||
21526606 | 11443 | /* Parse a ptr-operator. |
a723baf1 MM |
11444 | |
11445 | ptr-operator: | |
11446 | * cv-qualifier-seq [opt] | |
11447 | & | |
11448 | :: [opt] nested-name-specifier * cv-qualifier-seq [opt] | |
11449 | ||
11450 | GNU Extension: | |
11451 | ||
11452 | ptr-operator: | |
11453 | & cv-qualifier-seq [opt] | |
11454 | ||
3c01e5df MM |
11455 | Returns INDIRECT_REF if a pointer, or pointer-to-member, was used. |
11456 | Returns ADDR_EXPR if a reference was used. In the case of a | |
11457 | pointer-to-member, *TYPE is filled in with the TYPE containing the | |
11458 | member. *CV_QUALS is filled in with the cv-qualifier-seq, or | |
11459 | TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns | |
11460 | ERROR_MARK if an error occurred. */ | |
21526606 | 11461 | |
a723baf1 | 11462 | static enum tree_code |
21526606 EC |
11463 | cp_parser_ptr_operator (cp_parser* parser, |
11464 | tree* type, | |
3c01e5df | 11465 | cp_cv_quals *cv_quals) |
a723baf1 MM |
11466 | { |
11467 | enum tree_code code = ERROR_MARK; | |
11468 | cp_token *token; | |
11469 | ||
11470 | /* Assume that it's not a pointer-to-member. */ | |
11471 | *type = NULL_TREE; | |
11472 | /* And that there are no cv-qualifiers. */ | |
3c01e5df | 11473 | *cv_quals = TYPE_UNQUALIFIED; |
a723baf1 MM |
11474 | |
11475 | /* Peek at the next token. */ | |
11476 | token = cp_lexer_peek_token (parser->lexer); | |
11477 | /* If it's a `*' or `&' we have a pointer or reference. */ | |
11478 | if (token->type == CPP_MULT || token->type == CPP_AND) | |
11479 | { | |
11480 | /* Remember which ptr-operator we were processing. */ | |
11481 | code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF); | |
11482 | ||
11483 | /* Consume the `*' or `&'. */ | |
11484 | cp_lexer_consume_token (parser->lexer); | |
11485 | ||
11486 | /* A `*' can be followed by a cv-qualifier-seq, and so can a | |
11487 | `&', if we are allowing GNU extensions. (The only qualifier | |
11488 | that can legally appear after `&' is `restrict', but that is | |
11489 | enforced during semantic analysis. */ | |
21526606 | 11490 | if (code == INDIRECT_REF |
a723baf1 | 11491 | || cp_parser_allow_gnu_extensions_p (parser)) |
3c01e5df | 11492 | *cv_quals = cp_parser_cv_qualifier_seq_opt (parser); |
a723baf1 MM |
11493 | } |
11494 | else | |
11495 | { | |
11496 | /* Try the pointer-to-member case. */ | |
11497 | cp_parser_parse_tentatively (parser); | |
11498 | /* Look for the optional `::' operator. */ | |
11499 | cp_parser_global_scope_opt (parser, | |
11500 | /*current_scope_valid_p=*/false); | |
11501 | /* Look for the nested-name specifier. */ | |
11502 | cp_parser_nested_name_specifier (parser, | |
11503 | /*typename_keyword_p=*/false, | |
11504 | /*check_dependency_p=*/true, | |
a668c6ad MM |
11505 | /*type_p=*/false, |
11506 | /*is_declaration=*/false); | |
a723baf1 MM |
11507 | /* If we found it, and the next token is a `*', then we are |
11508 | indeed looking at a pointer-to-member operator. */ | |
11509 | if (!cp_parser_error_occurred (parser) | |
11510 | && cp_parser_require (parser, CPP_MULT, "`*'")) | |
11511 | { | |
11512 | /* The type of which the member is a member is given by the | |
11513 | current SCOPE. */ | |
11514 | *type = parser->scope; | |
11515 | /* The next name will not be qualified. */ | |
11516 | parser->scope = NULL_TREE; | |
11517 | parser->qualifying_scope = NULL_TREE; | |
11518 | parser->object_scope = NULL_TREE; | |
11519 | /* Indicate that the `*' operator was used. */ | |
11520 | code = INDIRECT_REF; | |
11521 | /* Look for the optional cv-qualifier-seq. */ | |
3c01e5df | 11522 | *cv_quals = cp_parser_cv_qualifier_seq_opt (parser); |
a723baf1 MM |
11523 | } |
11524 | /* If that didn't work we don't have a ptr-operator. */ | |
11525 | if (!cp_parser_parse_definitely (parser)) | |
11526 | cp_parser_error (parser, "expected ptr-operator"); | |
11527 | } | |
11528 | ||
11529 | return code; | |
11530 | } | |
11531 | ||
11532 | /* Parse an (optional) cv-qualifier-seq. | |
11533 | ||
11534 | cv-qualifier-seq: | |
21526606 | 11535 | cv-qualifier cv-qualifier-seq [opt] |
a723baf1 | 11536 | |
a723baf1 MM |
11537 | cv-qualifier: |
11538 | const | |
21526606 | 11539 | volatile |
a723baf1 MM |
11540 | |
11541 | GNU Extension: | |
11542 | ||
11543 | cv-qualifier: | |
98ca843c | 11544 | __restrict__ |
a723baf1 | 11545 | |
3c01e5df MM |
11546 | Returns a bitmask representing the cv-qualifiers. */ |
11547 | ||
11548 | static cp_cv_quals | |
11549 | cp_parser_cv_qualifier_seq_opt (cp_parser* parser) | |
a723baf1 | 11550 | { |
3c01e5df | 11551 | cp_cv_quals cv_quals = TYPE_UNQUALIFIED; |
a723baf1 | 11552 | |
3c01e5df | 11553 | while (true) |
a723baf1 | 11554 | { |
3c01e5df MM |
11555 | cp_token *token; |
11556 | cp_cv_quals cv_qualifier; | |
98ca843c | 11557 | |
3c01e5df MM |
11558 | /* Peek at the next token. */ |
11559 | token = cp_lexer_peek_token (parser->lexer); | |
11560 | /* See if it's a cv-qualifier. */ | |
11561 | switch (token->keyword) | |
11562 | { | |
11563 | case RID_CONST: | |
11564 | cv_qualifier = TYPE_QUAL_CONST; | |
11565 | break; | |
98ca843c | 11566 | |
3c01e5df MM |
11567 | case RID_VOLATILE: |
11568 | cv_qualifier = TYPE_QUAL_VOLATILE; | |
11569 | break; | |
98ca843c | 11570 | |
3c01e5df MM |
11571 | case RID_RESTRICT: |
11572 | cv_qualifier = TYPE_QUAL_RESTRICT; | |
11573 | break; | |
98ca843c | 11574 | |
3c01e5df MM |
11575 | default: |
11576 | cv_qualifier = TYPE_UNQUALIFIED; | |
11577 | break; | |
11578 | } | |
98ca843c | 11579 | |
3c01e5df MM |
11580 | if (!cv_qualifier) |
11581 | break; | |
a723baf1 | 11582 | |
3c01e5df MM |
11583 | if (cv_quals & cv_qualifier) |
11584 | { | |
11585 | error ("duplicate cv-qualifier"); | |
11586 | cp_lexer_purge_token (parser->lexer); | |
11587 | } | |
11588 | else | |
11589 | { | |
11590 | cp_lexer_consume_token (parser->lexer); | |
11591 | cv_quals |= cv_qualifier; | |
11592 | } | |
a723baf1 MM |
11593 | } |
11594 | ||
3c01e5df | 11595 | return cv_quals; |
a723baf1 MM |
11596 | } |
11597 | ||
11598 | /* Parse a declarator-id. | |
11599 | ||
11600 | declarator-id: | |
11601 | id-expression | |
21526606 | 11602 | :: [opt] nested-name-specifier [opt] type-name |
a723baf1 MM |
11603 | |
11604 | In the `id-expression' case, the value returned is as for | |
11605 | cp_parser_id_expression if the id-expression was an unqualified-id. | |
11606 | If the id-expression was a qualified-id, then a SCOPE_REF is | |
11607 | returned. The first operand is the scope (either a NAMESPACE_DECL | |
11608 | or TREE_TYPE), but the second is still just a representation of an | |
11609 | unqualified-id. */ | |
11610 | ||
11611 | static tree | |
94edc4ab | 11612 | cp_parser_declarator_id (cp_parser* parser) |
a723baf1 | 11613 | { |
a723baf1 MM |
11614 | /* The expression must be an id-expression. Assume that qualified |
11615 | names are the names of types so that: | |
11616 | ||
11617 | template <class T> | |
11618 | int S<T>::R::i = 3; | |
11619 | ||
11620 | will work; we must treat `S<T>::R' as the name of a type. | |
11621 | Similarly, assume that qualified names are templates, where | |
11622 | required, so that: | |
11623 | ||
11624 | template <class T> | |
11625 | int S<T>::R<T>::i = 3; | |
11626 | ||
11627 | will work, too. */ | |
1d786913 MM |
11628 | return cp_parser_id_expression (parser, |
11629 | /*template_keyword_p=*/false, | |
11630 | /*check_dependency_p=*/false, | |
11631 | /*template_p=*/NULL, | |
11632 | /*declarator_p=*/true); | |
a723baf1 MM |
11633 | } |
11634 | ||
11635 | /* Parse a type-id. | |
11636 | ||
11637 | type-id: | |
11638 | type-specifier-seq abstract-declarator [opt] | |
11639 | ||
11640 | Returns the TYPE specified. */ | |
11641 | ||
11642 | static tree | |
94edc4ab | 11643 | cp_parser_type_id (cp_parser* parser) |
a723baf1 | 11644 | { |
62d1db17 | 11645 | cp_decl_specifier_seq type_specifier_seq; |
058b15c1 | 11646 | cp_declarator *abstract_declarator; |
a723baf1 MM |
11647 | |
11648 | /* Parse the type-specifier-seq. */ | |
d4113656 MM |
11649 | cp_parser_type_specifier_seq (parser, /*is_condition=*/false, |
11650 | &type_specifier_seq); | |
62d1db17 | 11651 | if (type_specifier_seq.type == error_mark_node) |
a723baf1 MM |
11652 | return error_mark_node; |
11653 | ||
11654 | /* There might or might not be an abstract declarator. */ | |
11655 | cp_parser_parse_tentatively (parser); | |
11656 | /* Look for the declarator. */ | |
21526606 | 11657 | abstract_declarator |
4bb8ca28 | 11658 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL, |
db86dd14 MM |
11659 | /*parenthesized_p=*/NULL, |
11660 | /*member_p=*/false); | |
a723baf1 MM |
11661 | /* Check to see if there really was a declarator. */ |
11662 | if (!cp_parser_parse_definitely (parser)) | |
058b15c1 | 11663 | abstract_declarator = NULL; |
a723baf1 | 11664 | |
62d1db17 | 11665 | return groktypename (&type_specifier_seq, abstract_declarator); |
a723baf1 MM |
11666 | } |
11667 | ||
11668 | /* Parse a type-specifier-seq. | |
11669 | ||
11670 | type-specifier-seq: | |
11671 | type-specifier type-specifier-seq [opt] | |
11672 | ||
11673 | GNU extension: | |
11674 | ||
11675 | type-specifier-seq: | |
11676 | attributes type-specifier-seq [opt] | |
11677 | ||
d4113656 MM |
11678 | If IS_CONDITION is true, we are at the start of a "condition", |
11679 | e.g., we've just seen "if (". | |
11680 | ||
62d1db17 | 11681 | Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */ |
a723baf1 | 11682 | |
62d1db17 MM |
11683 | static void |
11684 | cp_parser_type_specifier_seq (cp_parser* parser, | |
d4113656 | 11685 | bool is_condition, |
62d1db17 | 11686 | cp_decl_specifier_seq *type_specifier_seq) |
a723baf1 MM |
11687 | { |
11688 | bool seen_type_specifier = false; | |
d4113656 | 11689 | cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL; |
62d1db17 MM |
11690 | |
11691 | /* Clear the TYPE_SPECIFIER_SEQ. */ | |
11692 | clear_decl_specs (type_specifier_seq); | |
a723baf1 MM |
11693 | |
11694 | /* Parse the type-specifiers and attributes. */ | |
11695 | while (true) | |
11696 | { | |
11697 | tree type_specifier; | |
d4113656 | 11698 | bool is_cv_qualifier; |
a723baf1 MM |
11699 | |
11700 | /* Check for attributes first. */ | |
11701 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) | |
11702 | { | |
98ca843c | 11703 | type_specifier_seq->attributes = |
62d1db17 MM |
11704 | chainon (type_specifier_seq->attributes, |
11705 | cp_parser_attributes_opt (parser)); | |
a723baf1 MM |
11706 | continue; |
11707 | } | |
11708 | ||
a723baf1 | 11709 | /* Look for the type-specifier. */ |
21526606 | 11710 | type_specifier = cp_parser_type_specifier (parser, |
d4113656 | 11711 | flags, |
62d1db17 | 11712 | type_specifier_seq, |
a723baf1 MM |
11713 | /*is_declaration=*/false, |
11714 | NULL, | |
d4113656 MM |
11715 | &is_cv_qualifier); |
11716 | if (!type_specifier) | |
62d1db17 | 11717 | { |
d4113656 MM |
11718 | /* If the first type-specifier could not be found, this is not a |
11719 | type-specifier-seq at all. */ | |
11720 | if (!seen_type_specifier) | |
11721 | { | |
11722 | cp_parser_error (parser, "expected type-specifier"); | |
11723 | type_specifier_seq->type = error_mark_node; | |
11724 | return; | |
11725 | } | |
11726 | /* If subsequent type-specifiers could not be found, the | |
11727 | type-specifier-seq is complete. */ | |
11728 | break; | |
62d1db17 | 11729 | } |
a723baf1 | 11730 | |
a723baf1 | 11731 | seen_type_specifier = true; |
d4113656 MM |
11732 | /* The standard says that a condition can be: |
11733 | ||
11734 | type-specifier-seq declarator = assignment-expression | |
11735 | ||
11736 | However, given: | |
11737 | ||
11738 | struct S {}; | |
11739 | if (int S = ...) | |
11740 | ||
11741 | we should treat the "S" as a declarator, not as a | |
11742 | type-specifier. The standard doesn't say that explicitly for | |
11743 | type-specifier-seq, but it does say that for | |
11744 | decl-specifier-seq in an ordinary declaration. Perhaps it | |
11745 | would be clearer just to allow a decl-specifier-seq here, and | |
11746 | then add a semantic restriction that if any decl-specifiers | |
11747 | that are not type-specifiers appear, the program is invalid. */ | |
11748 | if (is_condition && !is_cv_qualifier) | |
11749 | flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; | |
a723baf1 MM |
11750 | } |
11751 | ||
62d1db17 | 11752 | return; |
a723baf1 MM |
11753 | } |
11754 | ||
11755 | /* Parse a parameter-declaration-clause. | |
11756 | ||
11757 | parameter-declaration-clause: | |
11758 | parameter-declaration-list [opt] ... [opt] | |
11759 | parameter-declaration-list , ... | |
11760 | ||
058b15c1 MM |
11761 | Returns a representation for the parameter declarations. A return |
11762 | value of NULL indicates a parameter-declaration-clause consisting | |
11763 | only of an ellipsis. */ | |
a723baf1 | 11764 | |
058b15c1 | 11765 | static cp_parameter_declarator * |
94edc4ab | 11766 | cp_parser_parameter_declaration_clause (cp_parser* parser) |
a723baf1 | 11767 | { |
058b15c1 | 11768 | cp_parameter_declarator *parameters; |
a723baf1 MM |
11769 | cp_token *token; |
11770 | bool ellipsis_p; | |
058b15c1 | 11771 | bool is_error; |
a723baf1 MM |
11772 | |
11773 | /* Peek at the next token. */ | |
11774 | token = cp_lexer_peek_token (parser->lexer); | |
11775 | /* Check for trivial parameter-declaration-clauses. */ | |
11776 | if (token->type == CPP_ELLIPSIS) | |
11777 | { | |
11778 | /* Consume the `...' token. */ | |
11779 | cp_lexer_consume_token (parser->lexer); | |
058b15c1 | 11780 | return NULL; |
a723baf1 MM |
11781 | } |
11782 | else if (token->type == CPP_CLOSE_PAREN) | |
11783 | /* There are no parameters. */ | |
c73aecdf DE |
11784 | { |
11785 | #ifndef NO_IMPLICIT_EXTERN_C | |
11786 | if (in_system_header && current_class_type == NULL | |
11787 | && current_lang_name == lang_name_c) | |
058b15c1 | 11788 | return NULL; |
c73aecdf DE |
11789 | else |
11790 | #endif | |
058b15c1 | 11791 | return no_parameters; |
c73aecdf | 11792 | } |
a723baf1 MM |
11793 | /* Check for `(void)', too, which is a special case. */ |
11794 | else if (token->keyword == RID_VOID | |
21526606 | 11795 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 MM |
11796 | == CPP_CLOSE_PAREN)) |
11797 | { | |
11798 | /* Consume the `void' token. */ | |
11799 | cp_lexer_consume_token (parser->lexer); | |
11800 | /* There are no parameters. */ | |
058b15c1 | 11801 | return no_parameters; |
a723baf1 | 11802 | } |
21526606 | 11803 | |
a723baf1 | 11804 | /* Parse the parameter-declaration-list. */ |
058b15c1 | 11805 | parameters = cp_parser_parameter_declaration_list (parser, &is_error); |
a723baf1 MM |
11806 | /* If a parse error occurred while parsing the |
11807 | parameter-declaration-list, then the entire | |
11808 | parameter-declaration-clause is erroneous. */ | |
058b15c1 MM |
11809 | if (is_error) |
11810 | return NULL; | |
a723baf1 MM |
11811 | |
11812 | /* Peek at the next token. */ | |
11813 | token = cp_lexer_peek_token (parser->lexer); | |
11814 | /* If it's a `,', the clause should terminate with an ellipsis. */ | |
11815 | if (token->type == CPP_COMMA) | |
11816 | { | |
11817 | /* Consume the `,'. */ | |
11818 | cp_lexer_consume_token (parser->lexer); | |
11819 | /* Expect an ellipsis. */ | |
21526606 | 11820 | ellipsis_p |
a723baf1 MM |
11821 | = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL); |
11822 | } | |
21526606 | 11823 | /* It might also be `...' if the optional trailing `,' was |
a723baf1 MM |
11824 | omitted. */ |
11825 | else if (token->type == CPP_ELLIPSIS) | |
11826 | { | |
11827 | /* Consume the `...' token. */ | |
11828 | cp_lexer_consume_token (parser->lexer); | |
11829 | /* And remember that we saw it. */ | |
11830 | ellipsis_p = true; | |
11831 | } | |
11832 | else | |
11833 | ellipsis_p = false; | |
11834 | ||
11835 | /* Finish the parameter list. */ | |
058b15c1 MM |
11836 | if (parameters && ellipsis_p) |
11837 | parameters->ellipsis_p = true; | |
98ca843c | 11838 | |
058b15c1 | 11839 | return parameters; |
a723baf1 MM |
11840 | } |
11841 | ||
11842 | /* Parse a parameter-declaration-list. | |
11843 | ||
11844 | parameter-declaration-list: | |
11845 | parameter-declaration | |
11846 | parameter-declaration-list , parameter-declaration | |
11847 | ||
11848 | Returns a representation of the parameter-declaration-list, as for | |
11849 | cp_parser_parameter_declaration_clause. However, the | |
058b15c1 MM |
11850 | `void_list_node' is never appended to the list. Upon return, |
11851 | *IS_ERROR will be true iff an error occurred. */ | |
a723baf1 | 11852 | |
058b15c1 MM |
11853 | static cp_parameter_declarator * |
11854 | cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error) | |
a723baf1 | 11855 | { |
058b15c1 MM |
11856 | cp_parameter_declarator *parameters = NULL; |
11857 | cp_parameter_declarator **tail = ¶meters; | |
11858 | ||
11859 | /* Assume all will go well. */ | |
11860 | *is_error = false; | |
a723baf1 MM |
11861 | |
11862 | /* Look for more parameters. */ | |
11863 | while (true) | |
11864 | { | |
058b15c1 | 11865 | cp_parameter_declarator *parameter; |
4bb8ca28 | 11866 | bool parenthesized_p; |
a723baf1 | 11867 | /* Parse the parameter. */ |
21526606 EC |
11868 | parameter |
11869 | = cp_parser_parameter_declaration (parser, | |
4bb8ca28 MM |
11870 | /*template_parm_p=*/false, |
11871 | &parenthesized_p); | |
ec194454 | 11872 | |
34cd5ae7 | 11873 | /* If a parse error occurred parsing the parameter declaration, |
a723baf1 | 11874 | then the entire parameter-declaration-list is erroneous. */ |
058b15c1 | 11875 | if (!parameter) |
a723baf1 | 11876 | { |
058b15c1 MM |
11877 | *is_error = true; |
11878 | parameters = NULL; | |
a723baf1 MM |
11879 | break; |
11880 | } | |
11881 | /* Add the new parameter to the list. */ | |
058b15c1 MM |
11882 | *tail = parameter; |
11883 | tail = ¶meter->next; | |
a723baf1 MM |
11884 | |
11885 | /* Peek at the next token. */ | |
11886 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN) | |
e58a9aa1 ZL |
11887 | || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS) |
11888 | /* These are for Objective-C++ */ | |
11889 | || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) | |
11890 | || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
a723baf1 MM |
11891 | /* The parameter-declaration-list is complete. */ |
11892 | break; | |
11893 | else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
11894 | { | |
11895 | cp_token *token; | |
11896 | ||
11897 | /* Peek at the next token. */ | |
11898 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
11899 | /* If it's an ellipsis, then the list is complete. */ | |
11900 | if (token->type == CPP_ELLIPSIS) | |
11901 | break; | |
11902 | /* Otherwise, there must be more parameters. Consume the | |
11903 | `,'. */ | |
11904 | cp_lexer_consume_token (parser->lexer); | |
4bb8ca28 MM |
11905 | /* When parsing something like: |
11906 | ||
11907 | int i(float f, double d) | |
21526606 | 11908 | |
4bb8ca28 MM |
11909 | we can tell after seeing the declaration for "f" that we |
11910 | are not looking at an initialization of a variable "i", | |
21526606 | 11911 | but rather at the declaration of a function "i". |
4bb8ca28 MM |
11912 | |
11913 | Due to the fact that the parsing of template arguments | |
11914 | (as specified to a template-id) requires backtracking we | |
11915 | cannot use this technique when inside a template argument | |
11916 | list. */ | |
11917 | if (!parser->in_template_argument_list_p | |
4d5fe289 | 11918 | && !parser->in_type_id_in_expr_p |
0b16f8f4 | 11919 | && cp_parser_uncommitted_to_tentative_parse_p (parser) |
4bb8ca28 MM |
11920 | /* However, a parameter-declaration of the form |
11921 | "foat(f)" (which is a valid declaration of a | |
11922 | parameter "f") can also be interpreted as an | |
11923 | expression (the conversion of "f" to "float"). */ | |
11924 | && !parenthesized_p) | |
11925 | cp_parser_commit_to_tentative_parse (parser); | |
a723baf1 MM |
11926 | } |
11927 | else | |
11928 | { | |
2a13a625 | 11929 | cp_parser_error (parser, "expected %<,%> or %<...%>"); |
0b16f8f4 | 11930 | if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) |
21526606 | 11931 | cp_parser_skip_to_closing_parenthesis (parser, |
4bb8ca28 | 11932 | /*recovering=*/true, |
5c832178 | 11933 | /*or_comma=*/false, |
4bb8ca28 | 11934 | /*consume_paren=*/false); |
a723baf1 MM |
11935 | break; |
11936 | } | |
11937 | } | |
11938 | ||
058b15c1 | 11939 | return parameters; |
a723baf1 MM |
11940 | } |
11941 | ||
11942 | /* Parse a parameter declaration. | |
11943 | ||
11944 | parameter-declaration: | |
11945 | decl-specifier-seq declarator | |
11946 | decl-specifier-seq declarator = assignment-expression | |
11947 | decl-specifier-seq abstract-declarator [opt] | |
11948 | decl-specifier-seq abstract-declarator [opt] = assignment-expression | |
11949 | ||
ec194454 MM |
11950 | If TEMPLATE_PARM_P is TRUE, then this parameter-declaration |
11951 | declares a template parameter. (In that case, a non-nested `>' | |
11952 | token encountered during the parsing of the assignment-expression | |
11953 | is not interpreted as a greater-than operator.) | |
a723baf1 | 11954 | |
058b15c1 MM |
11955 | Returns a representation of the parameter, or NULL if an error |
11956 | occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to | |
11957 | true iff the declarator is of the form "(p)". */ | |
a723baf1 | 11958 | |
058b15c1 | 11959 | static cp_parameter_declarator * |
21526606 | 11960 | cp_parser_parameter_declaration (cp_parser *parser, |
4bb8ca28 MM |
11961 | bool template_parm_p, |
11962 | bool *parenthesized_p) | |
a723baf1 | 11963 | { |
560ad596 | 11964 | int declares_class_or_enum; |
ec194454 | 11965 | bool greater_than_is_operator_p; |
62d1db17 | 11966 | cp_decl_specifier_seq decl_specifiers; |
058b15c1 | 11967 | cp_declarator *declarator; |
a723baf1 | 11968 | tree default_argument; |
a723baf1 MM |
11969 | cp_token *token; |
11970 | const char *saved_message; | |
11971 | ||
ec194454 MM |
11972 | /* In a template parameter, `>' is not an operator. |
11973 | ||
11974 | [temp.param] | |
11975 | ||
11976 | When parsing a default template-argument for a non-type | |
11977 | template-parameter, the first non-nested `>' is taken as the end | |
11978 | of the template parameter-list rather than a greater-than | |
11979 | operator. */ | |
11980 | greater_than_is_operator_p = !template_parm_p; | |
11981 | ||
a723baf1 MM |
11982 | /* Type definitions may not appear in parameter types. */ |
11983 | saved_message = parser->type_definition_forbidden_message; | |
21526606 | 11984 | parser->type_definition_forbidden_message |
a723baf1 MM |
11985 | = "types may not be defined in parameter types"; |
11986 | ||
11987 | /* Parse the declaration-specifiers. */ | |
62d1db17 MM |
11988 | cp_parser_decl_specifier_seq (parser, |
11989 | CP_PARSER_FLAGS_NONE, | |
11990 | &decl_specifiers, | |
11991 | &declares_class_or_enum); | |
a723baf1 MM |
11992 | /* If an error occurred, there's no reason to attempt to parse the |
11993 | rest of the declaration. */ | |
11994 | if (cp_parser_error_occurred (parser)) | |
11995 | { | |
11996 | parser->type_definition_forbidden_message = saved_message; | |
058b15c1 | 11997 | return NULL; |
a723baf1 MM |
11998 | } |
11999 | ||
12000 | /* Peek at the next token. */ | |
12001 | token = cp_lexer_peek_token (parser->lexer); | |
12002 | /* If the next token is a `)', `,', `=', `>', or `...', then there | |
12003 | is no declarator. */ | |
21526606 | 12004 | if (token->type == CPP_CLOSE_PAREN |
a723baf1 MM |
12005 | || token->type == CPP_COMMA |
12006 | || token->type == CPP_EQ | |
12007 | || token->type == CPP_ELLIPSIS | |
12008 | || token->type == CPP_GREATER) | |
4bb8ca28 | 12009 | { |
058b15c1 | 12010 | declarator = NULL; |
4bb8ca28 MM |
12011 | if (parenthesized_p) |
12012 | *parenthesized_p = false; | |
12013 | } | |
a723baf1 MM |
12014 | /* Otherwise, there should be a declarator. */ |
12015 | else | |
12016 | { | |
12017 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
12018 | parser->default_arg_ok_p = false; | |
21526606 | 12019 | |
5c832178 MM |
12020 | /* After seeing a decl-specifier-seq, if the next token is not a |
12021 | "(", there is no possibility that the code is a valid | |
4f8163b1 MM |
12022 | expression. Therefore, if parsing tentatively, we commit at |
12023 | this point. */ | |
5c832178 | 12024 | if (!parser->in_template_argument_list_p |
643aee72 | 12025 | /* In an expression context, having seen: |
4f8163b1 | 12026 | |
a7324e75 | 12027 | (int((char ... |
4f8163b1 MM |
12028 | |
12029 | we cannot be sure whether we are looking at a | |
a7324e75 MM |
12030 | function-type (taking a "char" as a parameter) or a cast |
12031 | of some object of type "char" to "int". */ | |
4f8163b1 | 12032 | && !parser->in_type_id_in_expr_p |
0b16f8f4 | 12033 | && cp_parser_uncommitted_to_tentative_parse_p (parser) |
5c832178 MM |
12034 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) |
12035 | cp_parser_commit_to_tentative_parse (parser); | |
12036 | /* Parse the declarator. */ | |
a723baf1 | 12037 | declarator = cp_parser_declarator (parser, |
62b8a44e | 12038 | CP_PARSER_DECLARATOR_EITHER, |
4bb8ca28 | 12039 | /*ctor_dtor_or_conv_p=*/NULL, |
db86dd14 MM |
12040 | parenthesized_p, |
12041 | /*member_p=*/false); | |
a723baf1 | 12042 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
4971227d | 12043 | /* After the declarator, allow more attributes. */ |
62d1db17 | 12044 | decl_specifiers.attributes |
98ca843c | 12045 | = chainon (decl_specifiers.attributes, |
62d1db17 | 12046 | cp_parser_attributes_opt (parser)); |
a723baf1 MM |
12047 | } |
12048 | ||
62b8a44e | 12049 | /* The restriction on defining new types applies only to the type |
a723baf1 MM |
12050 | of the parameter, not to the default argument. */ |
12051 | parser->type_definition_forbidden_message = saved_message; | |
12052 | ||
12053 | /* If the next token is `=', then process a default argument. */ | |
12054 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
12055 | { | |
12056 | bool saved_greater_than_is_operator_p; | |
12057 | /* Consume the `='. */ | |
12058 | cp_lexer_consume_token (parser->lexer); | |
12059 | ||
12060 | /* If we are defining a class, then the tokens that make up the | |
12061 | default argument must be saved and processed later. */ | |
21526606 | 12062 | if (!template_parm_p && at_class_scope_p () |
ec194454 | 12063 | && TYPE_BEING_DEFINED (current_class_type)) |
a723baf1 MM |
12064 | { |
12065 | unsigned depth = 0; | |
c162c75e MA |
12066 | cp_token *first_token; |
12067 | cp_token *token; | |
a723baf1 MM |
12068 | |
12069 | /* Add tokens until we have processed the entire default | |
03fd3f84 | 12070 | argument. We add the range [first_token, token). */ |
c162c75e | 12071 | first_token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
12072 | while (true) |
12073 | { | |
12074 | bool done = false; | |
a723baf1 MM |
12075 | |
12076 | /* Peek at the next token. */ | |
12077 | token = cp_lexer_peek_token (parser->lexer); | |
12078 | /* What we do depends on what token we have. */ | |
12079 | switch (token->type) | |
12080 | { | |
12081 | /* In valid code, a default argument must be | |
12082 | immediately followed by a `,' `)', or `...'. */ | |
12083 | case CPP_COMMA: | |
12084 | case CPP_CLOSE_PAREN: | |
12085 | case CPP_ELLIPSIS: | |
12086 | /* If we run into a non-nested `;', `}', or `]', | |
12087 | then the code is invalid -- but the default | |
12088 | argument is certainly over. */ | |
12089 | case CPP_SEMICOLON: | |
12090 | case CPP_CLOSE_BRACE: | |
12091 | case CPP_CLOSE_SQUARE: | |
12092 | if (depth == 0) | |
12093 | done = true; | |
12094 | /* Update DEPTH, if necessary. */ | |
12095 | else if (token->type == CPP_CLOSE_PAREN | |
12096 | || token->type == CPP_CLOSE_BRACE | |
12097 | || token->type == CPP_CLOSE_SQUARE) | |
12098 | --depth; | |
12099 | break; | |
12100 | ||
12101 | case CPP_OPEN_PAREN: | |
12102 | case CPP_OPEN_SQUARE: | |
12103 | case CPP_OPEN_BRACE: | |
12104 | ++depth; | |
12105 | break; | |
12106 | ||
12107 | case CPP_GREATER: | |
12108 | /* If we see a non-nested `>', and `>' is not an | |
12109 | operator, then it marks the end of the default | |
12110 | argument. */ | |
12111 | if (!depth && !greater_than_is_operator_p) | |
12112 | done = true; | |
12113 | break; | |
12114 | ||
12115 | /* If we run out of tokens, issue an error message. */ | |
12116 | case CPP_EOF: | |
12117 | error ("file ends in default argument"); | |
12118 | done = true; | |
12119 | break; | |
12120 | ||
12121 | case CPP_NAME: | |
12122 | case CPP_SCOPE: | |
12123 | /* In these cases, we should look for template-ids. | |
21526606 | 12124 | For example, if the default argument is |
a723baf1 MM |
12125 | `X<int, double>()', we need to do name lookup to |
12126 | figure out whether or not `X' is a template; if | |
34cd5ae7 | 12127 | so, the `,' does not end the default argument. |
a723baf1 MM |
12128 | |
12129 | That is not yet done. */ | |
12130 | break; | |
12131 | ||
12132 | default: | |
12133 | break; | |
12134 | } | |
12135 | ||
12136 | /* If we've reached the end, stop. */ | |
12137 | if (done) | |
12138 | break; | |
21526606 | 12139 | |
a723baf1 MM |
12140 | /* Add the token to the token block. */ |
12141 | token = cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 12142 | } |
c162c75e MA |
12143 | |
12144 | /* Create a DEFAULT_ARG to represented the unparsed default | |
12145 | argument. */ | |
12146 | default_argument = make_node (DEFAULT_ARG); | |
12147 | DEFARG_TOKENS (default_argument) | |
01ea1ea8 NS |
12148 | = cp_token_cache_new (first_token, token); |
12149 | DEFARG_INSTANTIATIONS (default_argument) = NULL; | |
a723baf1 MM |
12150 | } |
12151 | /* Outside of a class definition, we can just parse the | |
12152 | assignment-expression. */ | |
12153 | else | |
12154 | { | |
12155 | bool saved_local_variables_forbidden_p; | |
12156 | ||
12157 | /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is | |
12158 | set correctly. */ | |
21526606 | 12159 | saved_greater_than_is_operator_p |
a723baf1 MM |
12160 | = parser->greater_than_is_operator_p; |
12161 | parser->greater_than_is_operator_p = greater_than_is_operator_p; | |
12162 | /* Local variable names (and the `this' keyword) may not | |
12163 | appear in a default argument. */ | |
21526606 | 12164 | saved_local_variables_forbidden_p |
a723baf1 MM |
12165 | = parser->local_variables_forbidden_p; |
12166 | parser->local_variables_forbidden_p = true; | |
12167 | /* Parse the assignment-expression. */ | |
93678513 MM |
12168 | default_argument |
12169 | = cp_parser_assignment_expression (parser, /*cast_p=*/false); | |
a723baf1 | 12170 | /* Restore saved state. */ |
21526606 | 12171 | parser->greater_than_is_operator_p |
a723baf1 | 12172 | = saved_greater_than_is_operator_p; |
21526606 EC |
12173 | parser->local_variables_forbidden_p |
12174 | = saved_local_variables_forbidden_p; | |
a723baf1 MM |
12175 | } |
12176 | if (!parser->default_arg_ok_p) | |
12177 | { | |
c67d36d0 | 12178 | if (!flag_pedantic_errors) |
d4ee4d25 | 12179 | warning (0, "deprecated use of default argument for parameter of non-function"); |
c67d36d0 NS |
12180 | else |
12181 | { | |
12182 | error ("default arguments are only permitted for function parameters"); | |
12183 | default_argument = NULL_TREE; | |
12184 | } | |
a723baf1 MM |
12185 | } |
12186 | } | |
12187 | else | |
12188 | default_argument = NULL_TREE; | |
21526606 | 12189 | |
62d1db17 | 12190 | return make_parameter_declarator (&decl_specifiers, |
058b15c1 MM |
12191 | declarator, |
12192 | default_argument); | |
a723baf1 MM |
12193 | } |
12194 | ||
a723baf1 MM |
12195 | /* Parse a function-body. |
12196 | ||
12197 | function-body: | |
12198 | compound_statement */ | |
12199 | ||
12200 | static void | |
12201 | cp_parser_function_body (cp_parser *parser) | |
12202 | { | |
325c3691 | 12203 | cp_parser_compound_statement (parser, NULL, false); |
a723baf1 MM |
12204 | } |
12205 | ||
12206 | /* Parse a ctor-initializer-opt followed by a function-body. Return | |
12207 | true if a ctor-initializer was present. */ | |
12208 | ||
12209 | static bool | |
12210 | cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser) | |
12211 | { | |
12212 | tree body; | |
12213 | bool ctor_initializer_p; | |
12214 | ||
12215 | /* Begin the function body. */ | |
12216 | body = begin_function_body (); | |
12217 | /* Parse the optional ctor-initializer. */ | |
12218 | ctor_initializer_p = cp_parser_ctor_initializer_opt (parser); | |
12219 | /* Parse the function-body. */ | |
12220 | cp_parser_function_body (parser); | |
12221 | /* Finish the function body. */ | |
12222 | finish_function_body (body); | |
12223 | ||
12224 | return ctor_initializer_p; | |
12225 | } | |
12226 | ||
12227 | /* Parse an initializer. | |
12228 | ||
12229 | initializer: | |
12230 | = initializer-clause | |
21526606 | 12231 | ( expression-list ) |
a723baf1 MM |
12232 | |
12233 | Returns a expression representing the initializer. If no | |
21526606 | 12234 | initializer is present, NULL_TREE is returned. |
a723baf1 MM |
12235 | |
12236 | *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )' | |
12237 | production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is | |
39703eb9 MM |
12238 | set to FALSE if there is no initializer present. If there is an |
12239 | initializer, and it is not a constant-expression, *NON_CONSTANT_P | |
12240 | is set to true; otherwise it is set to false. */ | |
a723baf1 MM |
12241 | |
12242 | static tree | |
39703eb9 MM |
12243 | cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init, |
12244 | bool* non_constant_p) | |
a723baf1 MM |
12245 | { |
12246 | cp_token *token; | |
12247 | tree init; | |
12248 | ||
12249 | /* Peek at the next token. */ | |
12250 | token = cp_lexer_peek_token (parser->lexer); | |
12251 | ||
12252 | /* Let our caller know whether or not this initializer was | |
12253 | parenthesized. */ | |
12254 | *is_parenthesized_init = (token->type == CPP_OPEN_PAREN); | |
39703eb9 MM |
12255 | /* Assume that the initializer is constant. */ |
12256 | *non_constant_p = false; | |
a723baf1 MM |
12257 | |
12258 | if (token->type == CPP_EQ) | |
12259 | { | |
12260 | /* Consume the `='. */ | |
12261 | cp_lexer_consume_token (parser->lexer); | |
12262 | /* Parse the initializer-clause. */ | |
39703eb9 | 12263 | init = cp_parser_initializer_clause (parser, non_constant_p); |
a723baf1 MM |
12264 | } |
12265 | else if (token->type == CPP_OPEN_PAREN) | |
39703eb9 | 12266 | init = cp_parser_parenthesized_expression_list (parser, false, |
93678513 | 12267 | /*cast_p=*/false, |
39703eb9 | 12268 | non_constant_p); |
a723baf1 MM |
12269 | else |
12270 | { | |
12271 | /* Anything else is an error. */ | |
12272 | cp_parser_error (parser, "expected initializer"); | |
12273 | init = error_mark_node; | |
12274 | } | |
12275 | ||
12276 | return init; | |
12277 | } | |
12278 | ||
21526606 | 12279 | /* Parse an initializer-clause. |
a723baf1 MM |
12280 | |
12281 | initializer-clause: | |
12282 | assignment-expression | |
12283 | { initializer-list , [opt] } | |
12284 | { } | |
12285 | ||
21526606 | 12286 | Returns an expression representing the initializer. |
a723baf1 MM |
12287 | |
12288 | If the `assignment-expression' production is used the value | |
21526606 | 12289 | returned is simply a representation for the expression. |
a723baf1 MM |
12290 | |
12291 | Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be | |
12292 | the elements of the initializer-list (or NULL_TREE, if the last | |
12293 | production is used). The TREE_TYPE for the CONSTRUCTOR will be | |
12294 | NULL_TREE. There is no way to detect whether or not the optional | |
39703eb9 MM |
12295 | trailing `,' was provided. NON_CONSTANT_P is as for |
12296 | cp_parser_initializer. */ | |
a723baf1 MM |
12297 | |
12298 | static tree | |
39703eb9 | 12299 | cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p) |
a723baf1 MM |
12300 | { |
12301 | tree initializer; | |
12302 | ||
b2802a4b R |
12303 | /* Assume the expression is constant. */ |
12304 | *non_constant_p = false; | |
12305 | ||
a723baf1 MM |
12306 | /* If it is not a `{', then we are looking at an |
12307 | assignment-expression. */ | |
12308 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
0da99d4e | 12309 | { |
98ca843c | 12310 | initializer |
0da99d4e GB |
12311 | = cp_parser_constant_expression (parser, |
12312 | /*allow_non_constant_p=*/true, | |
12313 | non_constant_p); | |
12314 | if (!*non_constant_p) | |
12315 | initializer = fold_non_dependent_expr (initializer); | |
12316 | } | |
a723baf1 MM |
12317 | else |
12318 | { | |
12319 | /* Consume the `{' token. */ | |
12320 | cp_lexer_consume_token (parser->lexer); | |
12321 | /* Create a CONSTRUCTOR to represent the braced-initializer. */ | |
12322 | initializer = make_node (CONSTRUCTOR); | |
a723baf1 MM |
12323 | /* If it's not a `}', then there is a non-trivial initializer. */ |
12324 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)) | |
12325 | { | |
12326 | /* Parse the initializer list. */ | |
12327 | CONSTRUCTOR_ELTS (initializer) | |
39703eb9 | 12328 | = cp_parser_initializer_list (parser, non_constant_p); |
a723baf1 MM |
12329 | /* A trailing `,' token is allowed. */ |
12330 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
12331 | cp_lexer_consume_token (parser->lexer); | |
12332 | } | |
a723baf1 MM |
12333 | /* Now, there should be a trailing `}'. */ |
12334 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
12335 | } | |
12336 | ||
12337 | return initializer; | |
12338 | } | |
12339 | ||
12340 | /* Parse an initializer-list. | |
12341 | ||
12342 | initializer-list: | |
12343 | initializer-clause | |
12344 | initializer-list , initializer-clause | |
12345 | ||
12346 | GNU Extension: | |
21526606 | 12347 | |
a723baf1 MM |
12348 | initializer-list: |
12349 | identifier : initializer-clause | |
12350 | initializer-list, identifier : initializer-clause | |
12351 | ||
12352 | Returns a TREE_LIST. The TREE_VALUE of each node is an expression | |
12353 | for the initializer. If the TREE_PURPOSE is non-NULL, it is the | |
39703eb9 MM |
12354 | IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is |
12355 | as for cp_parser_initializer. */ | |
a723baf1 MM |
12356 | |
12357 | static tree | |
39703eb9 | 12358 | cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p) |
a723baf1 MM |
12359 | { |
12360 | tree initializers = NULL_TREE; | |
12361 | ||
39703eb9 MM |
12362 | /* Assume all of the expressions are constant. */ |
12363 | *non_constant_p = false; | |
12364 | ||
a723baf1 MM |
12365 | /* Parse the rest of the list. */ |
12366 | while (true) | |
12367 | { | |
12368 | cp_token *token; | |
12369 | tree identifier; | |
12370 | tree initializer; | |
39703eb9 MM |
12371 | bool clause_non_constant_p; |
12372 | ||
a723baf1 MM |
12373 | /* If the next token is an identifier and the following one is a |
12374 | colon, we are looking at the GNU designated-initializer | |
12375 | syntax. */ | |
12376 | if (cp_parser_allow_gnu_extensions_p (parser) | |
12377 | && cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
12378 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON) | |
12379 | { | |
12380 | /* Consume the identifier. */ | |
12381 | identifier = cp_lexer_consume_token (parser->lexer)->value; | |
12382 | /* Consume the `:'. */ | |
12383 | cp_lexer_consume_token (parser->lexer); | |
12384 | } | |
12385 | else | |
12386 | identifier = NULL_TREE; | |
12387 | ||
12388 | /* Parse the initializer. */ | |
21526606 | 12389 | initializer = cp_parser_initializer_clause (parser, |
39703eb9 MM |
12390 | &clause_non_constant_p); |
12391 | /* If any clause is non-constant, so is the entire initializer. */ | |
12392 | if (clause_non_constant_p) | |
12393 | *non_constant_p = true; | |
a723baf1 MM |
12394 | /* Add it to the list. */ |
12395 | initializers = tree_cons (identifier, initializer, initializers); | |
12396 | ||
12397 | /* If the next token is not a comma, we have reached the end of | |
12398 | the list. */ | |
12399 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
12400 | break; | |
12401 | ||
12402 | /* Peek at the next token. */ | |
12403 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
12404 | /* If the next token is a `}', then we're still done. An | |
12405 | initializer-clause can have a trailing `,' after the | |
12406 | initializer-list and before the closing `}'. */ | |
12407 | if (token->type == CPP_CLOSE_BRACE) | |
12408 | break; | |
12409 | ||
12410 | /* Consume the `,' token. */ | |
12411 | cp_lexer_consume_token (parser->lexer); | |
12412 | } | |
12413 | ||
12414 | /* The initializers were built up in reverse order, so we need to | |
12415 | reverse them now. */ | |
12416 | return nreverse (initializers); | |
12417 | } | |
12418 | ||
12419 | /* Classes [gram.class] */ | |
12420 | ||
12421 | /* Parse a class-name. | |
12422 | ||
12423 | class-name: | |
12424 | identifier | |
12425 | template-id | |
12426 | ||
12427 | TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used | |
12428 | to indicate that names looked up in dependent types should be | |
12429 | assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template' | |
12430 | keyword has been used to indicate that the name that appears next | |
fc6a28d7 MM |
12431 | is a template. TAG_TYPE indicates the explicit tag given before |
12432 | the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are | |
12433 | looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class | |
12434 | is the class being defined in a class-head. | |
a723baf1 MM |
12435 | |
12436 | Returns the TYPE_DECL representing the class. */ | |
12437 | ||
12438 | static tree | |
21526606 EC |
12439 | cp_parser_class_name (cp_parser *parser, |
12440 | bool typename_keyword_p, | |
12441 | bool template_keyword_p, | |
fc6a28d7 | 12442 | enum tag_types tag_type, |
a723baf1 | 12443 | bool check_dependency_p, |
a668c6ad MM |
12444 | bool class_head_p, |
12445 | bool is_declaration) | |
a723baf1 MM |
12446 | { |
12447 | tree decl; | |
12448 | tree scope; | |
12449 | bool typename_p; | |
e5976695 MM |
12450 | cp_token *token; |
12451 | ||
12452 | /* All class-names start with an identifier. */ | |
12453 | token = cp_lexer_peek_token (parser->lexer); | |
12454 | if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID) | |
12455 | { | |
12456 | cp_parser_error (parser, "expected class-name"); | |
12457 | return error_mark_node; | |
12458 | } | |
21526606 | 12459 | |
a723baf1 MM |
12460 | /* PARSER->SCOPE can be cleared when parsing the template-arguments |
12461 | to a template-id, so we save it here. */ | |
12462 | scope = parser->scope; | |
3adee96c KL |
12463 | if (scope == error_mark_node) |
12464 | return error_mark_node; | |
21526606 | 12465 | |
a723baf1 MM |
12466 | /* Any name names a type if we're following the `typename' keyword |
12467 | in a qualified name where the enclosing scope is type-dependent. */ | |
12468 | typename_p = (typename_keyword_p && scope && TYPE_P (scope) | |
1fb3244a | 12469 | && dependent_type_p (scope)); |
e5976695 MM |
12470 | /* Handle the common case (an identifier, but not a template-id) |
12471 | efficiently. */ | |
21526606 | 12472 | if (token->type == CPP_NAME |
f4abade9 | 12473 | && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2)) |
a723baf1 | 12474 | { |
a723baf1 MM |
12475 | tree identifier; |
12476 | ||
12477 | /* Look for the identifier. */ | |
12478 | identifier = cp_parser_identifier (parser); | |
12479 | /* If the next token isn't an identifier, we are certainly not | |
12480 | looking at a class-name. */ | |
12481 | if (identifier == error_mark_node) | |
12482 | decl = error_mark_node; | |
12483 | /* If we know this is a type-name, there's no need to look it | |
12484 | up. */ | |
12485 | else if (typename_p) | |
12486 | decl = identifier; | |
12487 | else | |
12488 | { | |
12489 | /* If the next token is a `::', then the name must be a type | |
12490 | name. | |
12491 | ||
12492 | [basic.lookup.qual] | |
12493 | ||
12494 | During the lookup for a name preceding the :: scope | |
12495 | resolution operator, object, function, and enumerator | |
12496 | names are ignored. */ | |
12497 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
fc6a28d7 | 12498 | tag_type = typename_type; |
a723baf1 | 12499 | /* Look up the name. */ |
21526606 | 12500 | decl = cp_parser_lookup_name (parser, identifier, |
fc6a28d7 | 12501 | tag_type, |
b0bc6e8e | 12502 | /*is_template=*/false, |
eea9800f | 12503 | /*is_namespace=*/false, |
8f78f01f MM |
12504 | check_dependency_p, |
12505 | /*ambiguous_p=*/NULL); | |
a723baf1 MM |
12506 | } |
12507 | } | |
e5976695 MM |
12508 | else |
12509 | { | |
12510 | /* Try a template-id. */ | |
12511 | decl = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
12512 | check_dependency_p, |
12513 | is_declaration); | |
e5976695 MM |
12514 | if (decl == error_mark_node) |
12515 | return error_mark_node; | |
12516 | } | |
a723baf1 MM |
12517 | |
12518 | decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p); | |
12519 | ||
12520 | /* If this is a typename, create a TYPENAME_TYPE. */ | |
12521 | if (typename_p && decl != error_mark_node) | |
4bfb8bba | 12522 | { |
fc6a28d7 | 12523 | decl = make_typename_type (scope, decl, typename_type, /*complain=*/1); |
4bfb8bba MM |
12524 | if (decl != error_mark_node) |
12525 | decl = TYPE_NAME (decl); | |
12526 | } | |
a723baf1 MM |
12527 | |
12528 | /* Check to see that it is really the name of a class. */ | |
21526606 | 12529 | if (TREE_CODE (decl) == TEMPLATE_ID_EXPR |
a723baf1 MM |
12530 | && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE |
12531 | && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
12532 | /* Situations like this: | |
12533 | ||
12534 | template <typename T> struct A { | |
21526606 | 12535 | typename T::template X<int>::I i; |
a723baf1 MM |
12536 | }; |
12537 | ||
12538 | are problematic. Is `T::template X<int>' a class-name? The | |
12539 | standard does not seem to be definitive, but there is no other | |
12540 | valid interpretation of the following `::'. Therefore, those | |
12541 | names are considered class-names. */ | |
fc6a28d7 | 12542 | decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error)); |
a723baf1 MM |
12543 | else if (decl == error_mark_node |
12544 | || TREE_CODE (decl) != TYPE_DECL | |
07c65e00 | 12545 | || TREE_TYPE (decl) == error_mark_node |
a723baf1 MM |
12546 | || !IS_AGGR_TYPE (TREE_TYPE (decl))) |
12547 | { | |
12548 | cp_parser_error (parser, "expected class-name"); | |
12549 | return error_mark_node; | |
12550 | } | |
12551 | ||
12552 | return decl; | |
12553 | } | |
12554 | ||
12555 | /* Parse a class-specifier. | |
12556 | ||
12557 | class-specifier: | |
12558 | class-head { member-specification [opt] } | |
12559 | ||
12560 | Returns the TREE_TYPE representing the class. */ | |
12561 | ||
12562 | static tree | |
94edc4ab | 12563 | cp_parser_class_specifier (cp_parser* parser) |
a723baf1 MM |
12564 | { |
12565 | cp_token *token; | |
12566 | tree type; | |
6de9cd9a | 12567 | tree attributes = NULL_TREE; |
a723baf1 MM |
12568 | int has_trailing_semicolon; |
12569 | bool nested_name_specifier_p; | |
a723baf1 | 12570 | unsigned saved_num_template_parameter_lists; |
87c465f5 | 12571 | tree old_scope = NULL_TREE; |
2436b51f | 12572 | tree scope = NULL_TREE; |
a723baf1 | 12573 | |
8d241e0b | 12574 | push_deferring_access_checks (dk_no_deferred); |
cf22909c | 12575 | |
a723baf1 MM |
12576 | /* Parse the class-head. */ |
12577 | type = cp_parser_class_head (parser, | |
38b305d0 JM |
12578 | &nested_name_specifier_p, |
12579 | &attributes); | |
a723baf1 MM |
12580 | /* If the class-head was a semantic disaster, skip the entire body |
12581 | of the class. */ | |
12582 | if (!type) | |
12583 | { | |
12584 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
cf22909c | 12585 | pop_deferring_access_checks (); |
a723baf1 MM |
12586 | return error_mark_node; |
12587 | } | |
cf22909c | 12588 | |
a723baf1 MM |
12589 | /* Look for the `{'. */ |
12590 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
cf22909c KL |
12591 | { |
12592 | pop_deferring_access_checks (); | |
12593 | return error_mark_node; | |
12594 | } | |
12595 | ||
a723baf1 MM |
12596 | /* Issue an error message if type-definitions are forbidden here. */ |
12597 | cp_parser_check_type_definition (parser); | |
12598 | /* Remember that we are defining one more class. */ | |
12599 | ++parser->num_classes_being_defined; | |
12600 | /* Inside the class, surrounding template-parameter-lists do not | |
12601 | apply. */ | |
21526606 EC |
12602 | saved_num_template_parameter_lists |
12603 | = parser->num_template_parameter_lists; | |
a723baf1 | 12604 | parser->num_template_parameter_lists = 0; |
78757caa | 12605 | |
a723baf1 | 12606 | /* Start the class. */ |
eeb23c11 | 12607 | if (nested_name_specifier_p) |
2436b51f MM |
12608 | { |
12609 | scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)); | |
87c465f5 | 12610 | old_scope = push_inner_scope (scope); |
2436b51f | 12611 | } |
a723baf1 | 12612 | type = begin_class_definition (type); |
98ca843c | 12613 | |
a723baf1 | 12614 | if (type == error_mark_node) |
9bcb9aae | 12615 | /* If the type is erroneous, skip the entire body of the class. */ |
a723baf1 MM |
12616 | cp_parser_skip_to_closing_brace (parser); |
12617 | else | |
12618 | /* Parse the member-specification. */ | |
12619 | cp_parser_member_specification_opt (parser); | |
98ca843c | 12620 | |
a723baf1 MM |
12621 | /* Look for the trailing `}'. */ |
12622 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
12623 | /* We get better error messages by noticing a common problem: a | |
12624 | missing trailing `;'. */ | |
12625 | token = cp_lexer_peek_token (parser->lexer); | |
12626 | has_trailing_semicolon = (token->type == CPP_SEMICOLON); | |
38b305d0 | 12627 | /* Look for trailing attributes to apply to this class. */ |
a723baf1 | 12628 | if (cp_parser_allow_gnu_extensions_p (parser)) |
560ad596 | 12629 | { |
38b305d0 JM |
12630 | tree sub_attr = cp_parser_attributes_opt (parser); |
12631 | attributes = chainon (attributes, sub_attr); | |
560ad596 | 12632 | } |
38b305d0 JM |
12633 | if (type != error_mark_node) |
12634 | type = finish_struct (type, attributes); | |
87c465f5 KL |
12635 | if (nested_name_specifier_p) |
12636 | pop_inner_scope (old_scope, scope); | |
a723baf1 MM |
12637 | /* If this class is not itself within the scope of another class, |
12638 | then we need to parse the bodies of all of the queued function | |
12639 | definitions. Note that the queued functions defined in a class | |
12640 | are not always processed immediately following the | |
12641 | class-specifier for that class. Consider: | |
12642 | ||
12643 | struct A { | |
12644 | struct B { void f() { sizeof (A); } }; | |
12645 | }; | |
12646 | ||
12647 | If `f' were processed before the processing of `A' were | |
12648 | completed, there would be no way to compute the size of `A'. | |
12649 | Note that the nesting we are interested in here is lexical -- | |
12650 | not the semantic nesting given by TYPE_CONTEXT. In particular, | |
12651 | for: | |
12652 | ||
12653 | struct A { struct B; }; | |
12654 | struct A::B { void f() { } }; | |
12655 | ||
12656 | there is no need to delay the parsing of `A::B::f'. */ | |
21526606 | 12657 | if (--parser->num_classes_being_defined == 0) |
a723baf1 | 12658 | { |
8218bd34 MM |
12659 | tree queue_entry; |
12660 | tree fn; | |
4514aa8c NS |
12661 | tree class_type = NULL_TREE; |
12662 | tree pushed_scope = NULL_TREE; | |
a723baf1 | 12663 | |
8218bd34 MM |
12664 | /* In a first pass, parse default arguments to the functions. |
12665 | Then, in a second pass, parse the bodies of the functions. | |
12666 | This two-phased approach handles cases like: | |
21526606 EC |
12667 | |
12668 | struct S { | |
12669 | void f() { g(); } | |
8218bd34 MM |
12670 | void g(int i = 3); |
12671 | }; | |
12672 | ||
12673 | */ | |
8db1028e NS |
12674 | for (TREE_PURPOSE (parser->unparsed_functions_queues) |
12675 | = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues)); | |
12676 | (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues)); | |
12677 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
12678 | = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues))) | |
8218bd34 MM |
12679 | { |
12680 | fn = TREE_VALUE (queue_entry); | |
8218bd34 MM |
12681 | /* If there are default arguments that have not yet been processed, |
12682 | take care of them now. */ | |
f44b0c8e MM |
12683 | if (class_type != TREE_PURPOSE (queue_entry)) |
12684 | { | |
4514aa8c NS |
12685 | if (pushed_scope) |
12686 | pop_scope (pushed_scope); | |
f44b0c8e | 12687 | class_type = TREE_PURPOSE (queue_entry); |
4514aa8c | 12688 | pushed_scope = push_scope (class_type); |
f44b0c8e MM |
12689 | } |
12690 | /* Make sure that any template parameters are in scope. */ | |
12691 | maybe_begin_member_template_processing (fn); | |
12692 | /* Parse the default argument expressions. */ | |
8218bd34 MM |
12693 | cp_parser_late_parsing_default_args (parser, fn); |
12694 | /* Remove any template parameters from the symbol table. */ | |
12695 | maybe_end_member_template_processing (); | |
12696 | } | |
4514aa8c NS |
12697 | if (pushed_scope) |
12698 | pop_scope (pushed_scope); | |
8218bd34 | 12699 | /* Now parse the body of the functions. */ |
8db1028e NS |
12700 | for (TREE_VALUE (parser->unparsed_functions_queues) |
12701 | = nreverse (TREE_VALUE (parser->unparsed_functions_queues)); | |
12702 | (queue_entry = TREE_VALUE (parser->unparsed_functions_queues)); | |
12703 | TREE_VALUE (parser->unparsed_functions_queues) | |
12704 | = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues))) | |
a723baf1 | 12705 | { |
a723baf1 | 12706 | /* Figure out which function we need to process. */ |
a723baf1 MM |
12707 | fn = TREE_VALUE (queue_entry); |
12708 | ||
4543ee47 ZD |
12709 | /* A hack to prevent garbage collection. */ |
12710 | function_depth++; | |
12711 | ||
a723baf1 MM |
12712 | /* Parse the function. */ |
12713 | cp_parser_late_parsing_for_member (parser, fn); | |
4543ee47 | 12714 | function_depth--; |
a723baf1 | 12715 | } |
a723baf1 MM |
12716 | } |
12717 | ||
12718 | /* Put back any saved access checks. */ | |
cf22909c | 12719 | pop_deferring_access_checks (); |
a723baf1 MM |
12720 | |
12721 | /* Restore the count of active template-parameter-lists. */ | |
12722 | parser->num_template_parameter_lists | |
12723 | = saved_num_template_parameter_lists; | |
12724 | ||
12725 | return type; | |
12726 | } | |
12727 | ||
12728 | /* Parse a class-head. | |
12729 | ||
12730 | class-head: | |
12731 | class-key identifier [opt] base-clause [opt] | |
12732 | class-key nested-name-specifier identifier base-clause [opt] | |
21526606 EC |
12733 | class-key nested-name-specifier [opt] template-id |
12734 | base-clause [opt] | |
a723baf1 MM |
12735 | |
12736 | GNU Extensions: | |
12737 | class-key attributes identifier [opt] base-clause [opt] | |
12738 | class-key attributes nested-name-specifier identifier base-clause [opt] | |
21526606 EC |
12739 | class-key attributes nested-name-specifier [opt] template-id |
12740 | base-clause [opt] | |
a723baf1 MM |
12741 | |
12742 | Returns the TYPE of the indicated class. Sets | |
12743 | *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions | |
12744 | involving a nested-name-specifier was used, and FALSE otherwise. | |
a723baf1 | 12745 | |
55dcbc12 NS |
12746 | Returns error_mark_node if this is not a class-head. |
12747 | ||
a723baf1 MM |
12748 | Returns NULL_TREE if the class-head is syntactically valid, but |
12749 | semantically invalid in a way that means we should skip the entire | |
12750 | body of the class. */ | |
12751 | ||
12752 | static tree | |
21526606 | 12753 | cp_parser_class_head (cp_parser* parser, |
38b305d0 JM |
12754 | bool* nested_name_specifier_p, |
12755 | tree *attributes_p) | |
a723baf1 | 12756 | { |
a723baf1 MM |
12757 | tree nested_name_specifier; |
12758 | enum tag_types class_key; | |
12759 | tree id = NULL_TREE; | |
12760 | tree type = NULL_TREE; | |
12761 | tree attributes; | |
12762 | bool template_id_p = false; | |
12763 | bool qualified_p = false; | |
12764 | bool invalid_nested_name_p = false; | |
afb0918a | 12765 | bool invalid_explicit_specialization_p = false; |
4514aa8c | 12766 | tree pushed_scope = NULL_TREE; |
a723baf1 | 12767 | unsigned num_templates; |
cad7e87b | 12768 | tree bases; |
a723baf1 MM |
12769 | |
12770 | /* Assume no nested-name-specifier will be present. */ | |
12771 | *nested_name_specifier_p = false; | |
12772 | /* Assume no template parameter lists will be used in defining the | |
12773 | type. */ | |
12774 | num_templates = 0; | |
12775 | ||
12776 | /* Look for the class-key. */ | |
12777 | class_key = cp_parser_class_key (parser); | |
12778 | if (class_key == none_type) | |
12779 | return error_mark_node; | |
12780 | ||
12781 | /* Parse the attributes. */ | |
12782 | attributes = cp_parser_attributes_opt (parser); | |
12783 | ||
12784 | /* If the next token is `::', that is invalid -- but sometimes | |
12785 | people do try to write: | |
12786 | ||
21526606 | 12787 | struct ::S {}; |
a723baf1 MM |
12788 | |
12789 | Handle this gracefully by accepting the extra qualifier, and then | |
12790 | issuing an error about it later if this really is a | |
2050a1bb | 12791 | class-head. If it turns out just to be an elaborated type |
a723baf1 MM |
12792 | specifier, remain silent. */ |
12793 | if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)) | |
12794 | qualified_p = true; | |
12795 | ||
8d241e0b KL |
12796 | push_deferring_access_checks (dk_no_check); |
12797 | ||
a723baf1 MM |
12798 | /* Determine the name of the class. Begin by looking for an |
12799 | optional nested-name-specifier. */ | |
21526606 | 12800 | nested_name_specifier |
a723baf1 MM |
12801 | = cp_parser_nested_name_specifier_opt (parser, |
12802 | /*typename_keyword_p=*/false, | |
66d418e6 | 12803 | /*check_dependency_p=*/false, |
a668c6ad MM |
12804 | /*type_p=*/false, |
12805 | /*is_declaration=*/false); | |
a723baf1 MM |
12806 | /* If there was a nested-name-specifier, then there *must* be an |
12807 | identifier. */ | |
12808 | if (nested_name_specifier) | |
12809 | { | |
12810 | /* Although the grammar says `identifier', it really means | |
12811 | `class-name' or `template-name'. You are only allowed to | |
12812 | define a class that has already been declared with this | |
21526606 | 12813 | syntax. |
a723baf1 MM |
12814 | |
12815 | The proposed resolution for Core Issue 180 says that whever | |
12816 | you see `class T::X' you should treat `X' as a type-name. | |
21526606 | 12817 | |
a723baf1 | 12818 | It is OK to define an inaccessible class; for example: |
21526606 | 12819 | |
a723baf1 MM |
12820 | class A { class B; }; |
12821 | class A::B {}; | |
21526606 | 12822 | |
a723baf1 MM |
12823 | We do not know if we will see a class-name, or a |
12824 | template-name. We look for a class-name first, in case the | |
12825 | class-name is a template-id; if we looked for the | |
12826 | template-name first we would stop after the template-name. */ | |
12827 | cp_parser_parse_tentatively (parser); | |
12828 | type = cp_parser_class_name (parser, | |
12829 | /*typename_keyword_p=*/false, | |
12830 | /*template_keyword_p=*/false, | |
fc6a28d7 | 12831 | class_type, |
a723baf1 | 12832 | /*check_dependency_p=*/false, |
a668c6ad MM |
12833 | /*class_head_p=*/true, |
12834 | /*is_declaration=*/false); | |
a723baf1 MM |
12835 | /* If that didn't work, ignore the nested-name-specifier. */ |
12836 | if (!cp_parser_parse_definitely (parser)) | |
12837 | { | |
12838 | invalid_nested_name_p = true; | |
12839 | id = cp_parser_identifier (parser); | |
12840 | if (id == error_mark_node) | |
12841 | id = NULL_TREE; | |
12842 | } | |
12843 | /* If we could not find a corresponding TYPE, treat this | |
12844 | declaration like an unqualified declaration. */ | |
12845 | if (type == error_mark_node) | |
12846 | nested_name_specifier = NULL_TREE; | |
12847 | /* Otherwise, count the number of templates used in TYPE and its | |
12848 | containing scopes. */ | |
21526606 | 12849 | else |
a723baf1 MM |
12850 | { |
12851 | tree scope; | |
12852 | ||
21526606 | 12853 | for (scope = TREE_TYPE (type); |
a723baf1 | 12854 | scope && TREE_CODE (scope) != NAMESPACE_DECL; |
21526606 | 12855 | scope = (TYPE_P (scope) |
a723baf1 | 12856 | ? TYPE_CONTEXT (scope) |
21526606 EC |
12857 | : DECL_CONTEXT (scope))) |
12858 | if (TYPE_P (scope) | |
a723baf1 MM |
12859 | && CLASS_TYPE_P (scope) |
12860 | && CLASSTYPE_TEMPLATE_INFO (scope) | |
2050a1bb MM |
12861 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)) |
12862 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)) | |
a723baf1 MM |
12863 | ++num_templates; |
12864 | } | |
12865 | } | |
12866 | /* Otherwise, the identifier is optional. */ | |
12867 | else | |
12868 | { | |
12869 | /* We don't know whether what comes next is a template-id, | |
12870 | an identifier, or nothing at all. */ | |
12871 | cp_parser_parse_tentatively (parser); | |
12872 | /* Check for a template-id. */ | |
21526606 | 12873 | id = cp_parser_template_id (parser, |
a723baf1 | 12874 | /*template_keyword_p=*/false, |
a668c6ad MM |
12875 | /*check_dependency_p=*/true, |
12876 | /*is_declaration=*/true); | |
a723baf1 MM |
12877 | /* If that didn't work, it could still be an identifier. */ |
12878 | if (!cp_parser_parse_definitely (parser)) | |
12879 | { | |
12880 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
12881 | id = cp_parser_identifier (parser); | |
12882 | else | |
12883 | id = NULL_TREE; | |
12884 | } | |
12885 | else | |
12886 | { | |
12887 | template_id_p = true; | |
12888 | ++num_templates; | |
12889 | } | |
12890 | } | |
12891 | ||
8d241e0b KL |
12892 | pop_deferring_access_checks (); |
12893 | ||
15077df5 MM |
12894 | if (id) |
12895 | cp_parser_check_for_invalid_template_id (parser, id); | |
ee43dab5 | 12896 | |
a723baf1 MM |
12897 | /* If it's not a `:' or a `{' then we can't really be looking at a |
12898 | class-head, since a class-head only appears as part of a | |
12899 | class-specifier. We have to detect this situation before calling | |
12900 | xref_tag, since that has irreversible side-effects. */ | |
12901 | if (!cp_parser_next_token_starts_class_definition_p (parser)) | |
12902 | { | |
2a13a625 | 12903 | cp_parser_error (parser, "expected %<{%> or %<:%>"); |
a723baf1 MM |
12904 | return error_mark_node; |
12905 | } | |
12906 | ||
12907 | /* At this point, we're going ahead with the class-specifier, even | |
12908 | if some other problem occurs. */ | |
12909 | cp_parser_commit_to_tentative_parse (parser); | |
12910 | /* Issue the error about the overly-qualified name now. */ | |
12911 | if (qualified_p) | |
12912 | cp_parser_error (parser, | |
12913 | "global qualification of class name is invalid"); | |
12914 | else if (invalid_nested_name_p) | |
12915 | cp_parser_error (parser, | |
12916 | "qualified name does not name a class"); | |
88081599 MM |
12917 | else if (nested_name_specifier) |
12918 | { | |
12919 | tree scope; | |
9bf0e588 VR |
12920 | |
12921 | /* Reject typedef-names in class heads. */ | |
12922 | if (!DECL_IMPLICIT_TYPEDEF_P (type)) | |
12923 | { | |
12924 | error ("invalid class name in declaration of %qD", type); | |
12925 | type = NULL_TREE; | |
12926 | goto done; | |
12927 | } | |
12928 | ||
88081599 MM |
12929 | /* Figure out in what scope the declaration is being placed. */ |
12930 | scope = current_scope (); | |
88081599 MM |
12931 | /* If that scope does not contain the scope in which the |
12932 | class was originally declared, the program is invalid. */ | |
12933 | if (scope && !is_ancestor (scope, nested_name_specifier)) | |
12934 | { | |
2a13a625 GDR |
12935 | error ("declaration of %qD in %qD which does not enclose %qD", |
12936 | type, scope, nested_name_specifier); | |
88081599 MM |
12937 | type = NULL_TREE; |
12938 | goto done; | |
12939 | } | |
12940 | /* [dcl.meaning] | |
12941 | ||
12942 | A declarator-id shall not be qualified exception of the | |
12943 | definition of a ... nested class outside of its class | |
12944 | ... [or] a the definition or explicit instantiation of a | |
12945 | class member of a namespace outside of its namespace. */ | |
12946 | if (scope == nested_name_specifier) | |
12947 | { | |
12948 | pedwarn ("extra qualification ignored"); | |
12949 | nested_name_specifier = NULL_TREE; | |
12950 | num_templates = 0; | |
12951 | } | |
12952 | } | |
afb0918a MM |
12953 | /* An explicit-specialization must be preceded by "template <>". If |
12954 | it is not, try to recover gracefully. */ | |
21526606 | 12955 | if (at_namespace_scope_p () |
afb0918a | 12956 | && parser->num_template_parameter_lists == 0 |
eeb23c11 | 12957 | && template_id_p) |
afb0918a | 12958 | { |
2a13a625 | 12959 | error ("an explicit specialization must be preceded by %<template <>%>"); |
afb0918a MM |
12960 | invalid_explicit_specialization_p = true; |
12961 | /* Take the same action that would have been taken by | |
12962 | cp_parser_explicit_specialization. */ | |
12963 | ++parser->num_template_parameter_lists; | |
12964 | begin_specialization (); | |
12965 | } | |
12966 | /* There must be no "return" statements between this point and the | |
12967 | end of this function; set "type "to the correct return value and | |
12968 | use "goto done;" to return. */ | |
a723baf1 MM |
12969 | /* Make sure that the right number of template parameters were |
12970 | present. */ | |
12971 | if (!cp_parser_check_template_parameters (parser, num_templates)) | |
afb0918a MM |
12972 | { |
12973 | /* If something went wrong, there is no point in even trying to | |
12974 | process the class-definition. */ | |
12975 | type = NULL_TREE; | |
12976 | goto done; | |
12977 | } | |
a723baf1 | 12978 | |
a723baf1 MM |
12979 | /* Look up the type. */ |
12980 | if (template_id_p) | |
12981 | { | |
12982 | type = TREE_TYPE (id); | |
12983 | maybe_process_partial_specialization (type); | |
4514aa8c NS |
12984 | if (nested_name_specifier) |
12985 | pushed_scope = push_scope (nested_name_specifier); | |
a723baf1 | 12986 | } |
4514aa8c | 12987 | else if (nested_name_specifier) |
a723baf1 | 12988 | { |
a723baf1 MM |
12989 | tree class_type; |
12990 | ||
12991 | /* Given: | |
12992 | ||
12993 | template <typename T> struct S { struct T }; | |
14d22dd6 | 12994 | template <typename T> struct S<T>::T { }; |
a723baf1 MM |
12995 | |
12996 | we will get a TYPENAME_TYPE when processing the definition of | |
12997 | `S::T'. We need to resolve it to the actual type before we | |
12998 | try to define it. */ | |
12999 | if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE) | |
13000 | { | |
14d22dd6 MM |
13001 | class_type = resolve_typename_type (TREE_TYPE (type), |
13002 | /*only_current_p=*/false); | |
13003 | if (class_type != error_mark_node) | |
13004 | type = TYPE_NAME (class_type); | |
13005 | else | |
13006 | { | |
13007 | cp_parser_error (parser, "could not resolve typename type"); | |
13008 | type = error_mark_node; | |
13009 | } | |
a723baf1 MM |
13010 | } |
13011 | ||
560ad596 MM |
13012 | maybe_process_partial_specialization (TREE_TYPE (type)); |
13013 | class_type = current_class_type; | |
13014 | /* Enter the scope indicated by the nested-name-specifier. */ | |
4514aa8c | 13015 | pushed_scope = push_scope (nested_name_specifier); |
560ad596 MM |
13016 | /* Get the canonical version of this type. */ |
13017 | type = TYPE_MAIN_DECL (TREE_TYPE (type)); | |
13018 | if (PROCESSING_REAL_TEMPLATE_DECL_P () | |
13019 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type))) | |
55dcbc12 NS |
13020 | { |
13021 | type = push_template_decl (type); | |
13022 | if (type == error_mark_node) | |
13023 | { | |
13024 | type = NULL_TREE; | |
13025 | goto done; | |
13026 | } | |
13027 | } | |
13028 | ||
560ad596 | 13029 | type = TREE_TYPE (type); |
4514aa8c | 13030 | *nested_name_specifier_p = true; |
a723baf1 | 13031 | } |
4514aa8c NS |
13032 | else /* The name is not a nested name. */ |
13033 | { | |
13034 | /* If the class was unnamed, create a dummy name. */ | |
13035 | if (!id) | |
13036 | id = make_anon_name (); | |
13037 | type = xref_tag (class_key, id, /*tag_scope=*/ts_current, | |
13038 | parser->num_template_parameter_lists); | |
13039 | } | |
13040 | ||
a723baf1 MM |
13041 | /* Indicate whether this class was declared as a `class' or as a |
13042 | `struct'. */ | |
13043 | if (TREE_CODE (type) == RECORD_TYPE) | |
13044 | CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type); | |
13045 | cp_parser_check_class_key (class_key, type); | |
13046 | ||
744b12b6 MM |
13047 | /* If this type was already complete, and we see another definition, |
13048 | that's an error. */ | |
13049 | if (type != error_mark_node && COMPLETE_TYPE_P (type)) | |
13050 | { | |
13051 | error ("redefinition of %q#T", type); | |
13052 | cp_error_at ("previous definition of %q#T", type); | |
0f3744f8 VR |
13053 | type = NULL_TREE; |
13054 | goto done; | |
744b12b6 MM |
13055 | } |
13056 | ||
4514aa8c | 13057 | /* We will have entered the scope containing the class; the names of |
744b12b6 | 13058 | base classes should be looked up in that context. For example: |
a723baf1 MM |
13059 | |
13060 | struct A { struct B {}; struct C; }; | |
13061 | struct A::C : B {}; | |
13062 | ||
13063 | is valid. */ | |
cad7e87b | 13064 | bases = NULL_TREE; |
98ca843c | 13065 | |
cad7e87b NS |
13066 | /* Get the list of base-classes, if there is one. */ |
13067 | if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
13068 | bases = cp_parser_base_clause (parser); | |
98ca843c | 13069 | |
cad7e87b NS |
13070 | /* Process the base classes. */ |
13071 | xref_basetypes (type, bases); | |
a723baf1 | 13072 | |
4514aa8c | 13073 | done: |
a723baf1 MM |
13074 | /* Leave the scope given by the nested-name-specifier. We will |
13075 | enter the class scope itself while processing the members. */ | |
4514aa8c NS |
13076 | if (pushed_scope) |
13077 | pop_scope (pushed_scope); | |
a723baf1 | 13078 | |
afb0918a MM |
13079 | if (invalid_explicit_specialization_p) |
13080 | { | |
13081 | end_specialization (); | |
13082 | --parser->num_template_parameter_lists; | |
13083 | } | |
38b305d0 | 13084 | *attributes_p = attributes; |
a723baf1 MM |
13085 | return type; |
13086 | } | |
13087 | ||
13088 | /* Parse a class-key. | |
13089 | ||
13090 | class-key: | |
13091 | class | |
13092 | struct | |
13093 | union | |
13094 | ||
13095 | Returns the kind of class-key specified, or none_type to indicate | |
13096 | error. */ | |
13097 | ||
13098 | static enum tag_types | |
94edc4ab | 13099 | cp_parser_class_key (cp_parser* parser) |
a723baf1 MM |
13100 | { |
13101 | cp_token *token; | |
13102 | enum tag_types tag_type; | |
13103 | ||
13104 | /* Look for the class-key. */ | |
13105 | token = cp_parser_require (parser, CPP_KEYWORD, "class-key"); | |
13106 | if (!token) | |
13107 | return none_type; | |
13108 | ||
13109 | /* Check to see if the TOKEN is a class-key. */ | |
13110 | tag_type = cp_parser_token_is_class_key (token); | |
13111 | if (!tag_type) | |
13112 | cp_parser_error (parser, "expected class-key"); | |
13113 | return tag_type; | |
13114 | } | |
13115 | ||
13116 | /* Parse an (optional) member-specification. | |
13117 | ||
13118 | member-specification: | |
13119 | member-declaration member-specification [opt] | |
13120 | access-specifier : member-specification [opt] */ | |
13121 | ||
13122 | static void | |
94edc4ab | 13123 | cp_parser_member_specification_opt (cp_parser* parser) |
a723baf1 MM |
13124 | { |
13125 | while (true) | |
13126 | { | |
13127 | cp_token *token; | |
13128 | enum rid keyword; | |
13129 | ||
13130 | /* Peek at the next token. */ | |
13131 | token = cp_lexer_peek_token (parser->lexer); | |
13132 | /* If it's a `}', or EOF then we've seen all the members. */ | |
13133 | if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF) | |
13134 | break; | |
13135 | ||
13136 | /* See if this token is a keyword. */ | |
13137 | keyword = token->keyword; | |
13138 | switch (keyword) | |
13139 | { | |
13140 | case RID_PUBLIC: | |
13141 | case RID_PROTECTED: | |
13142 | case RID_PRIVATE: | |
13143 | /* Consume the access-specifier. */ | |
13144 | cp_lexer_consume_token (parser->lexer); | |
13145 | /* Remember which access-specifier is active. */ | |
13146 | current_access_specifier = token->value; | |
13147 | /* Look for the `:'. */ | |
13148 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
13149 | break; | |
13150 | ||
13151 | default: | |
de3fe73c MM |
13152 | /* Accept #pragmas at class scope. */ |
13153 | if (token->type == CPP_PRAGMA) | |
13154 | { | |
13155 | cp_lexer_handle_pragma (parser->lexer); | |
13156 | break; | |
13157 | } | |
13158 | ||
a723baf1 MM |
13159 | /* Otherwise, the next construction must be a |
13160 | member-declaration. */ | |
13161 | cp_parser_member_declaration (parser); | |
a723baf1 MM |
13162 | } |
13163 | } | |
13164 | } | |
13165 | ||
21526606 | 13166 | /* Parse a member-declaration. |
a723baf1 MM |
13167 | |
13168 | member-declaration: | |
13169 | decl-specifier-seq [opt] member-declarator-list [opt] ; | |
13170 | function-definition ; [opt] | |
13171 | :: [opt] nested-name-specifier template [opt] unqualified-id ; | |
13172 | using-declaration | |
21526606 | 13173 | template-declaration |
a723baf1 MM |
13174 | |
13175 | member-declarator-list: | |
13176 | member-declarator | |
13177 | member-declarator-list , member-declarator | |
13178 | ||
13179 | member-declarator: | |
21526606 | 13180 | declarator pure-specifier [opt] |
a723baf1 | 13181 | declarator constant-initializer [opt] |
21526606 | 13182 | identifier [opt] : constant-expression |
a723baf1 MM |
13183 | |
13184 | GNU Extensions: | |
13185 | ||
13186 | member-declaration: | |
13187 | __extension__ member-declaration | |
13188 | ||
13189 | member-declarator: | |
13190 | declarator attributes [opt] pure-specifier [opt] | |
13191 | declarator attributes [opt] constant-initializer [opt] | |
13192 | identifier [opt] attributes [opt] : constant-expression */ | |
13193 | ||
13194 | static void | |
94edc4ab | 13195 | cp_parser_member_declaration (cp_parser* parser) |
a723baf1 | 13196 | { |
62d1db17 | 13197 | cp_decl_specifier_seq decl_specifiers; |
a723baf1 MM |
13198 | tree prefix_attributes; |
13199 | tree decl; | |
560ad596 | 13200 | int declares_class_or_enum; |
a723baf1 MM |
13201 | bool friend_p; |
13202 | cp_token *token; | |
13203 | int saved_pedantic; | |
13204 | ||
13205 | /* Check for the `__extension__' keyword. */ | |
13206 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
13207 | { | |
13208 | /* Recurse. */ | |
13209 | cp_parser_member_declaration (parser); | |
13210 | /* Restore the old value of the PEDANTIC flag. */ | |
13211 | pedantic = saved_pedantic; | |
13212 | ||
13213 | return; | |
13214 | } | |
13215 | ||
13216 | /* Check for a template-declaration. */ | |
13217 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
13218 | { | |
13219 | /* Parse the template-declaration. */ | |
13220 | cp_parser_template_declaration (parser, /*member_p=*/true); | |
13221 | ||
13222 | return; | |
13223 | } | |
13224 | ||
13225 | /* Check for a using-declaration. */ | |
13226 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING)) | |
13227 | { | |
13228 | /* Parse the using-declaration. */ | |
13229 | cp_parser_using_declaration (parser); | |
13230 | ||
13231 | return; | |
13232 | } | |
21526606 | 13233 | |
e58a9aa1 ZL |
13234 | /* Check for @defs. */ |
13235 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS)) | |
13236 | { | |
13237 | tree ivar, member; | |
13238 | tree ivar_chains = cp_parser_objc_defs_expression (parser); | |
13239 | ivar = ivar_chains; | |
13240 | while (ivar) | |
13241 | { | |
13242 | member = ivar; | |
13243 | ivar = TREE_CHAIN (member); | |
13244 | TREE_CHAIN (member) = NULL_TREE; | |
13245 | finish_member_declaration (member); | |
13246 | } | |
13247 | return; | |
13248 | } | |
13249 | ||
a723baf1 | 13250 | /* Parse the decl-specifier-seq. */ |
62d1db17 MM |
13251 | cp_parser_decl_specifier_seq (parser, |
13252 | CP_PARSER_FLAGS_OPTIONAL, | |
13253 | &decl_specifiers, | |
13254 | &declares_class_or_enum); | |
13255 | prefix_attributes = decl_specifiers.attributes; | |
13256 | decl_specifiers.attributes = NULL_TREE; | |
8fbc5ae7 | 13257 | /* Check for an invalid type-name. */ |
de3fe73c MM |
13258 | if (!decl_specifiers.type |
13259 | && cp_parser_parse_and_diagnose_invalid_type_name (parser)) | |
8fbc5ae7 | 13260 | return; |
a723baf1 MM |
13261 | /* If there is no declarator, then the decl-specifier-seq should |
13262 | specify a type. */ | |
13263 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
13264 | { | |
13265 | /* If there was no decl-specifier-seq, and the next token is a | |
13266 | `;', then we have something like: | |
13267 | ||
13268 | struct S { ; }; | |
13269 | ||
13270 | [class.mem] | |
13271 | ||
13272 | Each member-declaration shall declare at least one member | |
13273 | name of the class. */ | |
62d1db17 | 13274 | if (!decl_specifiers.any_specifiers_p) |
a723baf1 | 13275 | { |
2cfe82fe ZW |
13276 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
13277 | if (pedantic && !token->in_system_header) | |
13278 | pedwarn ("%Hextra %<;%>", &token->location); | |
a723baf1 | 13279 | } |
21526606 | 13280 | else |
a723baf1 MM |
13281 | { |
13282 | tree type; | |
21526606 | 13283 | |
a723baf1 | 13284 | /* See if this declaration is a friend. */ |
62d1db17 | 13285 | friend_p = cp_parser_friend_p (&decl_specifiers); |
a723baf1 MM |
13286 | /* If there were decl-specifiers, check to see if there was |
13287 | a class-declaration. */ | |
62d1db17 | 13288 | type = check_tag_decl (&decl_specifiers); |
a723baf1 MM |
13289 | /* Nested classes have already been added to the class, but |
13290 | a `friend' needs to be explicitly registered. */ | |
13291 | if (friend_p) | |
13292 | { | |
13293 | /* If the `friend' keyword was present, the friend must | |
13294 | be introduced with a class-key. */ | |
13295 | if (!declares_class_or_enum) | |
13296 | error ("a class-key must be used when declaring a friend"); | |
13297 | /* In this case: | |
13298 | ||
21526606 EC |
13299 | template <typename T> struct A { |
13300 | friend struct A<T>::B; | |
a723baf1 | 13301 | }; |
21526606 | 13302 | |
a723baf1 MM |
13303 | A<T>::B will be represented by a TYPENAME_TYPE, and |
13304 | therefore not recognized by check_tag_decl. */ | |
98ca843c | 13305 | if (!type |
62d1db17 MM |
13306 | && decl_specifiers.type |
13307 | && TYPE_P (decl_specifiers.type)) | |
13308 | type = decl_specifiers.type; | |
fdd09134 | 13309 | if (!type || !TYPE_P (type)) |
a723baf1 MM |
13310 | error ("friend declaration does not name a class or " |
13311 | "function"); | |
13312 | else | |
19db77ce KL |
13313 | make_friend_class (current_class_type, type, |
13314 | /*complain=*/true); | |
a723baf1 MM |
13315 | } |
13316 | /* If there is no TYPE, an error message will already have | |
13317 | been issued. */ | |
62d1db17 | 13318 | else if (!type || type == error_mark_node) |
a723baf1 MM |
13319 | ; |
13320 | /* An anonymous aggregate has to be handled specially; such | |
13321 | a declaration really declares a data member (with a | |
13322 | particular type), as opposed to a nested class. */ | |
13323 | else if (ANON_AGGR_TYPE_P (type)) | |
13324 | { | |
13325 | /* Remove constructors and such from TYPE, now that we | |
34cd5ae7 | 13326 | know it is an anonymous aggregate. */ |
a723baf1 MM |
13327 | fixup_anonymous_aggr (type); |
13328 | /* And make the corresponding data member. */ | |
13329 | decl = build_decl (FIELD_DECL, NULL_TREE, type); | |
13330 | /* Add it to the class. */ | |
13331 | finish_member_declaration (decl); | |
13332 | } | |
37d407a1 KL |
13333 | else |
13334 | cp_parser_check_access_in_redeclaration (TYPE_NAME (type)); | |
a723baf1 MM |
13335 | } |
13336 | } | |
13337 | else | |
13338 | { | |
13339 | /* See if these declarations will be friends. */ | |
62d1db17 | 13340 | friend_p = cp_parser_friend_p (&decl_specifiers); |
a723baf1 | 13341 | |
21526606 | 13342 | /* Keep going until we hit the `;' at the end of the |
a723baf1 MM |
13343 | declaration. */ |
13344 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
13345 | { | |
13346 | tree attributes = NULL_TREE; | |
13347 | tree first_attribute; | |
13348 | ||
13349 | /* Peek at the next token. */ | |
13350 | token = cp_lexer_peek_token (parser->lexer); | |
13351 | ||
13352 | /* Check for a bitfield declaration. */ | |
13353 | if (token->type == CPP_COLON | |
13354 | || (token->type == CPP_NAME | |
21526606 | 13355 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 MM |
13356 | == CPP_COLON)) |
13357 | { | |
13358 | tree identifier; | |
13359 | tree width; | |
13360 | ||
13361 | /* Get the name of the bitfield. Note that we cannot just | |
13362 | check TOKEN here because it may have been invalidated by | |
13363 | the call to cp_lexer_peek_nth_token above. */ | |
13364 | if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON) | |
13365 | identifier = cp_parser_identifier (parser); | |
13366 | else | |
13367 | identifier = NULL_TREE; | |
13368 | ||
13369 | /* Consume the `:' token. */ | |
13370 | cp_lexer_consume_token (parser->lexer); | |
13371 | /* Get the width of the bitfield. */ | |
21526606 | 13372 | width |
14d22dd6 MM |
13373 | = cp_parser_constant_expression (parser, |
13374 | /*allow_non_constant=*/false, | |
13375 | NULL); | |
a723baf1 MM |
13376 | |
13377 | /* Look for attributes that apply to the bitfield. */ | |
13378 | attributes = cp_parser_attributes_opt (parser); | |
13379 | /* Remember which attributes are prefix attributes and | |
13380 | which are not. */ | |
13381 | first_attribute = attributes; | |
13382 | /* Combine the attributes. */ | |
13383 | attributes = chainon (prefix_attributes, attributes); | |
13384 | ||
13385 | /* Create the bitfield declaration. */ | |
98ca843c | 13386 | decl = grokbitfield (identifier |
1d786913 MM |
13387 | ? make_id_declarator (NULL_TREE, |
13388 | identifier) | |
058b15c1 | 13389 | : NULL, |
62d1db17 | 13390 | &decl_specifiers, |
a723baf1 MM |
13391 | width); |
13392 | /* Apply the attributes. */ | |
13393 | cplus_decl_attributes (&decl, attributes, /*flags=*/0); | |
13394 | } | |
13395 | else | |
13396 | { | |
058b15c1 | 13397 | cp_declarator *declarator; |
a723baf1 MM |
13398 | tree initializer; |
13399 | tree asm_specification; | |
7efa3e22 | 13400 | int ctor_dtor_or_conv_p; |
a723baf1 MM |
13401 | |
13402 | /* Parse the declarator. */ | |
21526606 | 13403 | declarator |
62b8a44e | 13404 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 | 13405 | &ctor_dtor_or_conv_p, |
db86dd14 MM |
13406 | /*parenthesized_p=*/NULL, |
13407 | /*member_p=*/true); | |
a723baf1 MM |
13408 | |
13409 | /* If something went wrong parsing the declarator, make sure | |
13410 | that we at least consume some tokens. */ | |
058b15c1 | 13411 | if (declarator == cp_error_declarator) |
a723baf1 MM |
13412 | { |
13413 | /* Skip to the end of the statement. */ | |
13414 | cp_parser_skip_to_end_of_statement (parser); | |
4bb8ca28 MM |
13415 | /* If the next token is not a semicolon, that is |
13416 | probably because we just skipped over the body of | |
13417 | a function. So, we consume a semicolon if | |
13418 | present, but do not issue an error message if it | |
13419 | is not present. */ | |
13420 | if (cp_lexer_next_token_is (parser->lexer, | |
13421 | CPP_SEMICOLON)) | |
13422 | cp_lexer_consume_token (parser->lexer); | |
13423 | return; | |
a723baf1 MM |
13424 | } |
13425 | ||
fc6a28d7 MM |
13426 | if (declares_class_or_enum & 2) |
13427 | cp_parser_check_for_definition_in_return_type | |
13428 | (declarator, decl_specifiers.type); | |
560ad596 | 13429 | |
a723baf1 MM |
13430 | /* Look for an asm-specification. */ |
13431 | asm_specification = cp_parser_asm_specification_opt (parser); | |
13432 | /* Look for attributes that apply to the declaration. */ | |
13433 | attributes = cp_parser_attributes_opt (parser); | |
13434 | /* Remember which attributes are prefix attributes and | |
13435 | which are not. */ | |
13436 | first_attribute = attributes; | |
13437 | /* Combine the attributes. */ | |
13438 | attributes = chainon (prefix_attributes, attributes); | |
13439 | ||
13440 | /* If it's an `=', then we have a constant-initializer or a | |
13441 | pure-specifier. It is not correct to parse the | |
13442 | initializer before registering the member declaration | |
13443 | since the member declaration should be in scope while | |
13444 | its initializer is processed. However, the rest of the | |
13445 | front end does not yet provide an interface that allows | |
13446 | us to handle this correctly. */ | |
13447 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
13448 | { | |
13449 | /* In [class.mem]: | |
13450 | ||
13451 | A pure-specifier shall be used only in the declaration of | |
21526606 | 13452 | a virtual function. |
a723baf1 MM |
13453 | |
13454 | A member-declarator can contain a constant-initializer | |
13455 | only if it declares a static member of integral or | |
21526606 | 13456 | enumeration type. |
a723baf1 MM |
13457 | |
13458 | Therefore, if the DECLARATOR is for a function, we look | |
13459 | for a pure-specifier; otherwise, we look for a | |
13460 | constant-initializer. When we call `grokfield', it will | |
13461 | perform more stringent semantics checks. */ | |
058b15c1 | 13462 | if (declarator->kind == cdk_function) |
a723baf1 MM |
13463 | initializer = cp_parser_pure_specifier (parser); |
13464 | else | |
4bb8ca28 MM |
13465 | /* Parse the initializer. */ |
13466 | initializer = cp_parser_constant_initializer (parser); | |
a723baf1 MM |
13467 | } |
13468 | /* Otherwise, there is no initializer. */ | |
13469 | else | |
13470 | initializer = NULL_TREE; | |
13471 | ||
13472 | /* See if we are probably looking at a function | |
5a19910e | 13473 | definition. We are certainly not looking at a |
a723baf1 MM |
13474 | member-declarator. Calling `grokfield' has |
13475 | side-effects, so we must not do it unless we are sure | |
13476 | that we are looking at a member-declarator. */ | |
21526606 | 13477 | if (cp_parser_token_starts_function_definition_p |
a723baf1 | 13478 | (cp_lexer_peek_token (parser->lexer))) |
4bb8ca28 MM |
13479 | { |
13480 | /* The grammar does not allow a pure-specifier to be | |
13481 | used when a member function is defined. (It is | |
13482 | possible that this fact is an oversight in the | |
13483 | standard, since a pure function may be defined | |
13484 | outside of the class-specifier. */ | |
13485 | if (initializer) | |
13486 | error ("pure-specifier on function-definition"); | |
13487 | decl = cp_parser_save_member_function_body (parser, | |
62d1db17 | 13488 | &decl_specifiers, |
4bb8ca28 MM |
13489 | declarator, |
13490 | attributes); | |
13491 | /* If the member was not a friend, declare it here. */ | |
13492 | if (!friend_p) | |
13493 | finish_member_declaration (decl); | |
13494 | /* Peek at the next token. */ | |
13495 | token = cp_lexer_peek_token (parser->lexer); | |
13496 | /* If the next token is a semicolon, consume it. */ | |
13497 | if (token->type == CPP_SEMICOLON) | |
13498 | cp_lexer_consume_token (parser->lexer); | |
13499 | return; | |
13500 | } | |
a723baf1 | 13501 | else |
39703eb9 MM |
13502 | { |
13503 | /* Create the declaration. */ | |
62d1db17 | 13504 | decl = grokfield (declarator, &decl_specifiers, |
ee3071ef | 13505 | initializer, asm_specification, |
39703eb9 MM |
13506 | attributes); |
13507 | /* Any initialization must have been from a | |
13508 | constant-expression. */ | |
13509 | if (decl && TREE_CODE (decl) == VAR_DECL && initializer) | |
13510 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; | |
13511 | } | |
a723baf1 MM |
13512 | } |
13513 | ||
13514 | /* Reset PREFIX_ATTRIBUTES. */ | |
13515 | while (attributes && TREE_CHAIN (attributes) != first_attribute) | |
13516 | attributes = TREE_CHAIN (attributes); | |
13517 | if (attributes) | |
13518 | TREE_CHAIN (attributes) = NULL_TREE; | |
13519 | ||
13520 | /* If there is any qualification still in effect, clear it | |
13521 | now; we will be starting fresh with the next declarator. */ | |
13522 | parser->scope = NULL_TREE; | |
13523 | parser->qualifying_scope = NULL_TREE; | |
13524 | parser->object_scope = NULL_TREE; | |
13525 | /* If it's a `,', then there are more declarators. */ | |
13526 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
13527 | cp_lexer_consume_token (parser->lexer); | |
13528 | /* If the next token isn't a `;', then we have a parse error. */ | |
13529 | else if (cp_lexer_next_token_is_not (parser->lexer, | |
13530 | CPP_SEMICOLON)) | |
13531 | { | |
2a13a625 | 13532 | cp_parser_error (parser, "expected %<;%>"); |
04c06002 | 13533 | /* Skip tokens until we find a `;'. */ |
a723baf1 MM |
13534 | cp_parser_skip_to_end_of_statement (parser); |
13535 | ||
13536 | break; | |
13537 | } | |
13538 | ||
13539 | if (decl) | |
13540 | { | |
13541 | /* Add DECL to the list of members. */ | |
13542 | if (!friend_p) | |
13543 | finish_member_declaration (decl); | |
13544 | ||
a723baf1 | 13545 | if (TREE_CODE (decl) == FUNCTION_DECL) |
8db1028e | 13546 | cp_parser_save_default_args (parser, decl); |
a723baf1 MM |
13547 | } |
13548 | } | |
13549 | } | |
13550 | ||
4bb8ca28 | 13551 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
a723baf1 MM |
13552 | } |
13553 | ||
13554 | /* Parse a pure-specifier. | |
13555 | ||
13556 | pure-specifier: | |
13557 | = 0 | |
13558 | ||
13559 | Returns INTEGER_ZERO_NODE if a pure specifier is found. | |
cd0be382 | 13560 | Otherwise, ERROR_MARK_NODE is returned. */ |
a723baf1 MM |
13561 | |
13562 | static tree | |
94edc4ab | 13563 | cp_parser_pure_specifier (cp_parser* parser) |
a723baf1 MM |
13564 | { |
13565 | cp_token *token; | |
13566 | ||
13567 | /* Look for the `=' token. */ | |
13568 | if (!cp_parser_require (parser, CPP_EQ, "`='")) | |
13569 | return error_mark_node; | |
13570 | /* Look for the `0' token. */ | |
515e6a84 GB |
13571 | token = cp_lexer_consume_token (parser->lexer); |
13572 | if (token->type != CPP_NUMBER || !integer_zerop (token->value)) | |
13573 | { | |
13574 | cp_parser_error (parser, | |
13575 | "invalid pure specifier (only `= 0' is allowed)"); | |
13576 | cp_parser_skip_to_end_of_statement (parser); | |
13577 | return error_mark_node; | |
13578 | } | |
a723baf1 | 13579 | |
515e6a84 GB |
13580 | /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well. |
13581 | We need to get information from the lexer about how the number | |
13582 | was spelled in order to fix this problem. */ | |
a723baf1 MM |
13583 | return integer_zero_node; |
13584 | } | |
13585 | ||
13586 | /* Parse a constant-initializer. | |
13587 | ||
13588 | constant-initializer: | |
13589 | = constant-expression | |
13590 | ||
13591 | Returns a representation of the constant-expression. */ | |
13592 | ||
13593 | static tree | |
94edc4ab | 13594 | cp_parser_constant_initializer (cp_parser* parser) |
a723baf1 MM |
13595 | { |
13596 | /* Look for the `=' token. */ | |
13597 | if (!cp_parser_require (parser, CPP_EQ, "`='")) | |
13598 | return error_mark_node; | |
13599 | ||
13600 | /* It is invalid to write: | |
13601 | ||
13602 | struct S { static const int i = { 7 }; }; | |
13603 | ||
13604 | */ | |
13605 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
13606 | { | |
13607 | cp_parser_error (parser, | |
13608 | "a brace-enclosed initializer is not allowed here"); | |
13609 | /* Consume the opening brace. */ | |
13610 | cp_lexer_consume_token (parser->lexer); | |
13611 | /* Skip the initializer. */ | |
13612 | cp_parser_skip_to_closing_brace (parser); | |
13613 | /* Look for the trailing `}'. */ | |
13614 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
21526606 | 13615 | |
a723baf1 MM |
13616 | return error_mark_node; |
13617 | } | |
13618 | ||
21526606 | 13619 | return cp_parser_constant_expression (parser, |
14d22dd6 MM |
13620 | /*allow_non_constant=*/false, |
13621 | NULL); | |
a723baf1 MM |
13622 | } |
13623 | ||
13624 | /* Derived classes [gram.class.derived] */ | |
13625 | ||
13626 | /* Parse a base-clause. | |
13627 | ||
13628 | base-clause: | |
21526606 | 13629 | : base-specifier-list |
a723baf1 MM |
13630 | |
13631 | base-specifier-list: | |
13632 | base-specifier | |
13633 | base-specifier-list , base-specifier | |
13634 | ||
13635 | Returns a TREE_LIST representing the base-classes, in the order in | |
13636 | which they were declared. The representation of each node is as | |
21526606 | 13637 | described by cp_parser_base_specifier. |
a723baf1 MM |
13638 | |
13639 | In the case that no bases are specified, this function will return | |
13640 | NULL_TREE, not ERROR_MARK_NODE. */ | |
13641 | ||
13642 | static tree | |
94edc4ab | 13643 | cp_parser_base_clause (cp_parser* parser) |
a723baf1 MM |
13644 | { |
13645 | tree bases = NULL_TREE; | |
13646 | ||
13647 | /* Look for the `:' that begins the list. */ | |
13648 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
13649 | ||
13650 | /* Scan the base-specifier-list. */ | |
13651 | while (true) | |
13652 | { | |
13653 | cp_token *token; | |
13654 | tree base; | |
13655 | ||
13656 | /* Look for the base-specifier. */ | |
13657 | base = cp_parser_base_specifier (parser); | |
13658 | /* Add BASE to the front of the list. */ | |
13659 | if (base != error_mark_node) | |
13660 | { | |
13661 | TREE_CHAIN (base) = bases; | |
13662 | bases = base; | |
13663 | } | |
13664 | /* Peek at the next token. */ | |
13665 | token = cp_lexer_peek_token (parser->lexer); | |
13666 | /* If it's not a comma, then the list is complete. */ | |
13667 | if (token->type != CPP_COMMA) | |
13668 | break; | |
13669 | /* Consume the `,'. */ | |
13670 | cp_lexer_consume_token (parser->lexer); | |
13671 | } | |
13672 | ||
13673 | /* PARSER->SCOPE may still be non-NULL at this point, if the last | |
13674 | base class had a qualified name. However, the next name that | |
13675 | appears is certainly not qualified. */ | |
13676 | parser->scope = NULL_TREE; | |
13677 | parser->qualifying_scope = NULL_TREE; | |
13678 | parser->object_scope = NULL_TREE; | |
13679 | ||
13680 | return nreverse (bases); | |
13681 | } | |
13682 | ||
13683 | /* Parse a base-specifier. | |
13684 | ||
13685 | base-specifier: | |
13686 | :: [opt] nested-name-specifier [opt] class-name | |
13687 | virtual access-specifier [opt] :: [opt] nested-name-specifier | |
13688 | [opt] class-name | |
13689 | access-specifier virtual [opt] :: [opt] nested-name-specifier | |
13690 | [opt] class-name | |
13691 | ||
13692 | Returns a TREE_LIST. The TREE_PURPOSE will be one of | |
13693 | ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to | |
13694 | indicate the specifiers provided. The TREE_VALUE will be a TYPE | |
13695 | (or the ERROR_MARK_NODE) indicating the type that was specified. */ | |
21526606 | 13696 | |
a723baf1 | 13697 | static tree |
94edc4ab | 13698 | cp_parser_base_specifier (cp_parser* parser) |
a723baf1 MM |
13699 | { |
13700 | cp_token *token; | |
13701 | bool done = false; | |
13702 | bool virtual_p = false; | |
13703 | bool duplicate_virtual_error_issued_p = false; | |
13704 | bool duplicate_access_error_issued_p = false; | |
bbaab916 | 13705 | bool class_scope_p, template_p; |
dbbf88d1 | 13706 | tree access = access_default_node; |
a723baf1 MM |
13707 | tree type; |
13708 | ||
13709 | /* Process the optional `virtual' and `access-specifier'. */ | |
13710 | while (!done) | |
13711 | { | |
13712 | /* Peek at the next token. */ | |
13713 | token = cp_lexer_peek_token (parser->lexer); | |
13714 | /* Process `virtual'. */ | |
13715 | switch (token->keyword) | |
13716 | { | |
13717 | case RID_VIRTUAL: | |
13718 | /* If `virtual' appears more than once, issue an error. */ | |
13719 | if (virtual_p && !duplicate_virtual_error_issued_p) | |
13720 | { | |
13721 | cp_parser_error (parser, | |
2a13a625 | 13722 | "%<virtual%> specified more than once in base-specified"); |
a723baf1 MM |
13723 | duplicate_virtual_error_issued_p = true; |
13724 | } | |
13725 | ||
13726 | virtual_p = true; | |
13727 | ||
13728 | /* Consume the `virtual' token. */ | |
13729 | cp_lexer_consume_token (parser->lexer); | |
13730 | ||
13731 | break; | |
13732 | ||
13733 | case RID_PUBLIC: | |
13734 | case RID_PROTECTED: | |
13735 | case RID_PRIVATE: | |
13736 | /* If more than one access specifier appears, issue an | |
13737 | error. */ | |
dbbf88d1 NS |
13738 | if (access != access_default_node |
13739 | && !duplicate_access_error_issued_p) | |
a723baf1 MM |
13740 | { |
13741 | cp_parser_error (parser, | |
13742 | "more than one access specifier in base-specified"); | |
13743 | duplicate_access_error_issued_p = true; | |
13744 | } | |
13745 | ||
dbbf88d1 | 13746 | access = ridpointers[(int) token->keyword]; |
a723baf1 MM |
13747 | |
13748 | /* Consume the access-specifier. */ | |
13749 | cp_lexer_consume_token (parser->lexer); | |
13750 | ||
13751 | break; | |
13752 | ||
13753 | default: | |
13754 | done = true; | |
13755 | break; | |
13756 | } | |
13757 | } | |
852dcbdd | 13758 | /* It is not uncommon to see programs mechanically, erroneously, use |
a3a503a5 | 13759 | the 'typename' keyword to denote (dependent) qualified types |
1ed53ef3 GB |
13760 | as base classes. */ |
13761 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME)) | |
13762 | { | |
13763 | if (!processing_template_decl) | |
2a13a625 | 13764 | error ("keyword %<typename%> not allowed outside of templates"); |
1ed53ef3 | 13765 | else |
2a13a625 | 13766 | error ("keyword %<typename%> not allowed in this context " |
1ed53ef3 GB |
13767 | "(the base class is implicitly a type)"); |
13768 | cp_lexer_consume_token (parser->lexer); | |
13769 | } | |
a723baf1 | 13770 | |
a723baf1 MM |
13771 | /* Look for the optional `::' operator. */ |
13772 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); | |
13773 | /* Look for the nested-name-specifier. The simplest way to | |
13774 | implement: | |
13775 | ||
13776 | [temp.res] | |
13777 | ||
13778 | The keyword `typename' is not permitted in a base-specifier or | |
13779 | mem-initializer; in these contexts a qualified name that | |
13780 | depends on a template-parameter is implicitly assumed to be a | |
13781 | type name. | |
13782 | ||
13783 | is to pretend that we have seen the `typename' keyword at this | |
21526606 | 13784 | point. */ |
a723baf1 MM |
13785 | cp_parser_nested_name_specifier_opt (parser, |
13786 | /*typename_keyword_p=*/true, | |
13787 | /*check_dependency_p=*/true, | |
fc6a28d7 | 13788 | typename_type, |
a668c6ad | 13789 | /*is_declaration=*/true); |
a723baf1 MM |
13790 | /* If the base class is given by a qualified name, assume that names |
13791 | we see are type names or templates, as appropriate. */ | |
13792 | class_scope_p = (parser->scope && TYPE_P (parser->scope)); | |
bbaab916 | 13793 | template_p = class_scope_p && cp_parser_optional_template_keyword (parser); |
21526606 | 13794 | |
a723baf1 | 13795 | /* Finally, look for the class-name. */ |
21526606 | 13796 | type = cp_parser_class_name (parser, |
a723baf1 | 13797 | class_scope_p, |
bbaab916 | 13798 | template_p, |
fc6a28d7 | 13799 | typename_type, |
a723baf1 | 13800 | /*check_dependency_p=*/true, |
a668c6ad MM |
13801 | /*class_head_p=*/false, |
13802 | /*is_declaration=*/true); | |
a723baf1 MM |
13803 | |
13804 | if (type == error_mark_node) | |
13805 | return error_mark_node; | |
13806 | ||
dbbf88d1 | 13807 | return finish_base_specifier (TREE_TYPE (type), access, virtual_p); |
a723baf1 MM |
13808 | } |
13809 | ||
13810 | /* Exception handling [gram.exception] */ | |
13811 | ||
13812 | /* Parse an (optional) exception-specification. | |
13813 | ||
13814 | exception-specification: | |
13815 | throw ( type-id-list [opt] ) | |
13816 | ||
13817 | Returns a TREE_LIST representing the exception-specification. The | |
13818 | TREE_VALUE of each node is a type. */ | |
13819 | ||
13820 | static tree | |
94edc4ab | 13821 | cp_parser_exception_specification_opt (cp_parser* parser) |
a723baf1 MM |
13822 | { |
13823 | cp_token *token; | |
13824 | tree type_id_list; | |
13825 | ||
13826 | /* Peek at the next token. */ | |
13827 | token = cp_lexer_peek_token (parser->lexer); | |
13828 | /* If it's not `throw', then there's no exception-specification. */ | |
13829 | if (!cp_parser_is_keyword (token, RID_THROW)) | |
13830 | return NULL_TREE; | |
13831 | ||
13832 | /* Consume the `throw'. */ | |
13833 | cp_lexer_consume_token (parser->lexer); | |
13834 | ||
13835 | /* Look for the `('. */ | |
13836 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13837 | ||
13838 | /* Peek at the next token. */ | |
13839 | token = cp_lexer_peek_token (parser->lexer); | |
13840 | /* If it's not a `)', then there is a type-id-list. */ | |
13841 | if (token->type != CPP_CLOSE_PAREN) | |
13842 | { | |
13843 | const char *saved_message; | |
13844 | ||
13845 | /* Types may not be defined in an exception-specification. */ | |
13846 | saved_message = parser->type_definition_forbidden_message; | |
13847 | parser->type_definition_forbidden_message | |
13848 | = "types may not be defined in an exception-specification"; | |
13849 | /* Parse the type-id-list. */ | |
13850 | type_id_list = cp_parser_type_id_list (parser); | |
13851 | /* Restore the saved message. */ | |
13852 | parser->type_definition_forbidden_message = saved_message; | |
13853 | } | |
13854 | else | |
13855 | type_id_list = empty_except_spec; | |
13856 | ||
13857 | /* Look for the `)'. */ | |
13858 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
13859 | ||
13860 | return type_id_list; | |
13861 | } | |
13862 | ||
13863 | /* Parse an (optional) type-id-list. | |
13864 | ||
13865 | type-id-list: | |
13866 | type-id | |
13867 | type-id-list , type-id | |
13868 | ||
13869 | Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE, | |
13870 | in the order that the types were presented. */ | |
13871 | ||
13872 | static tree | |
94edc4ab | 13873 | cp_parser_type_id_list (cp_parser* parser) |
a723baf1 MM |
13874 | { |
13875 | tree types = NULL_TREE; | |
13876 | ||
13877 | while (true) | |
13878 | { | |
13879 | cp_token *token; | |
13880 | tree type; | |
13881 | ||
13882 | /* Get the next type-id. */ | |
13883 | type = cp_parser_type_id (parser); | |
13884 | /* Add it to the list. */ | |
13885 | types = add_exception_specifier (types, type, /*complain=*/1); | |
13886 | /* Peek at the next token. */ | |
13887 | token = cp_lexer_peek_token (parser->lexer); | |
13888 | /* If it is not a `,', we are done. */ | |
13889 | if (token->type != CPP_COMMA) | |
13890 | break; | |
13891 | /* Consume the `,'. */ | |
13892 | cp_lexer_consume_token (parser->lexer); | |
13893 | } | |
13894 | ||
13895 | return nreverse (types); | |
13896 | } | |
13897 | ||
13898 | /* Parse a try-block. | |
13899 | ||
13900 | try-block: | |
13901 | try compound-statement handler-seq */ | |
13902 | ||
13903 | static tree | |
94edc4ab | 13904 | cp_parser_try_block (cp_parser* parser) |
a723baf1 MM |
13905 | { |
13906 | tree try_block; | |
13907 | ||
13908 | cp_parser_require_keyword (parser, RID_TRY, "`try'"); | |
13909 | try_block = begin_try_block (); | |
325c3691 | 13910 | cp_parser_compound_statement (parser, NULL, true); |
a723baf1 MM |
13911 | finish_try_block (try_block); |
13912 | cp_parser_handler_seq (parser); | |
13913 | finish_handler_sequence (try_block); | |
13914 | ||
13915 | return try_block; | |
13916 | } | |
13917 | ||
13918 | /* Parse a function-try-block. | |
13919 | ||
13920 | function-try-block: | |
13921 | try ctor-initializer [opt] function-body handler-seq */ | |
13922 | ||
13923 | static bool | |
94edc4ab | 13924 | cp_parser_function_try_block (cp_parser* parser) |
a723baf1 MM |
13925 | { |
13926 | tree try_block; | |
13927 | bool ctor_initializer_p; | |
13928 | ||
13929 | /* Look for the `try' keyword. */ | |
13930 | if (!cp_parser_require_keyword (parser, RID_TRY, "`try'")) | |
13931 | return false; | |
13932 | /* Let the rest of the front-end know where we are. */ | |
13933 | try_block = begin_function_try_block (); | |
13934 | /* Parse the function-body. */ | |
21526606 | 13935 | ctor_initializer_p |
a723baf1 MM |
13936 | = cp_parser_ctor_initializer_opt_and_function_body (parser); |
13937 | /* We're done with the `try' part. */ | |
13938 | finish_function_try_block (try_block); | |
13939 | /* Parse the handlers. */ | |
13940 | cp_parser_handler_seq (parser); | |
13941 | /* We're done with the handlers. */ | |
13942 | finish_function_handler_sequence (try_block); | |
13943 | ||
13944 | return ctor_initializer_p; | |
13945 | } | |
13946 | ||
13947 | /* Parse a handler-seq. | |
13948 | ||
13949 | handler-seq: | |
13950 | handler handler-seq [opt] */ | |
13951 | ||
13952 | static void | |
94edc4ab | 13953 | cp_parser_handler_seq (cp_parser* parser) |
a723baf1 MM |
13954 | { |
13955 | while (true) | |
13956 | { | |
13957 | cp_token *token; | |
13958 | ||
13959 | /* Parse the handler. */ | |
13960 | cp_parser_handler (parser); | |
13961 | /* Peek at the next token. */ | |
13962 | token = cp_lexer_peek_token (parser->lexer); | |
13963 | /* If it's not `catch' then there are no more handlers. */ | |
13964 | if (!cp_parser_is_keyword (token, RID_CATCH)) | |
13965 | break; | |
13966 | } | |
13967 | } | |
13968 | ||
13969 | /* Parse a handler. | |
13970 | ||
13971 | handler: | |
13972 | catch ( exception-declaration ) compound-statement */ | |
13973 | ||
13974 | static void | |
94edc4ab | 13975 | cp_parser_handler (cp_parser* parser) |
a723baf1 MM |
13976 | { |
13977 | tree handler; | |
13978 | tree declaration; | |
13979 | ||
13980 | cp_parser_require_keyword (parser, RID_CATCH, "`catch'"); | |
13981 | handler = begin_handler (); | |
13982 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13983 | declaration = cp_parser_exception_declaration (parser); | |
13984 | finish_handler_parms (declaration, handler); | |
13985 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
325c3691 | 13986 | cp_parser_compound_statement (parser, NULL, false); |
a723baf1 MM |
13987 | finish_handler (handler); |
13988 | } | |
13989 | ||
13990 | /* Parse an exception-declaration. | |
13991 | ||
13992 | exception-declaration: | |
13993 | type-specifier-seq declarator | |
13994 | type-specifier-seq abstract-declarator | |
13995 | type-specifier-seq | |
21526606 | 13996 | ... |
a723baf1 MM |
13997 | |
13998 | Returns a VAR_DECL for the declaration, or NULL_TREE if the | |
13999 | ellipsis variant is used. */ | |
14000 | ||
14001 | static tree | |
94edc4ab | 14002 | cp_parser_exception_declaration (cp_parser* parser) |
a723baf1 | 14003 | { |
058b15c1 | 14004 | tree decl; |
62d1db17 | 14005 | cp_decl_specifier_seq type_specifiers; |
058b15c1 | 14006 | cp_declarator *declarator; |
a723baf1 MM |
14007 | const char *saved_message; |
14008 | ||
14009 | /* If it's an ellipsis, it's easy to handle. */ | |
14010 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
14011 | { | |
14012 | /* Consume the `...' token. */ | |
14013 | cp_lexer_consume_token (parser->lexer); | |
14014 | return NULL_TREE; | |
14015 | } | |
14016 | ||
14017 | /* Types may not be defined in exception-declarations. */ | |
14018 | saved_message = parser->type_definition_forbidden_message; | |
14019 | parser->type_definition_forbidden_message | |
14020 | = "types may not be defined in exception-declarations"; | |
14021 | ||
14022 | /* Parse the type-specifier-seq. */ | |
d4113656 MM |
14023 | cp_parser_type_specifier_seq (parser, /*is_condition=*/false, |
14024 | &type_specifiers); | |
a723baf1 MM |
14025 | /* If it's a `)', then there is no declarator. */ |
14026 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)) | |
058b15c1 | 14027 | declarator = NULL; |
a723baf1 | 14028 | else |
62b8a44e | 14029 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER, |
4bb8ca28 | 14030 | /*ctor_dtor_or_conv_p=*/NULL, |
db86dd14 MM |
14031 | /*parenthesized_p=*/NULL, |
14032 | /*member_p=*/false); | |
a723baf1 MM |
14033 | |
14034 | /* Restore the saved message. */ | |
14035 | parser->type_definition_forbidden_message = saved_message; | |
14036 | ||
62d1db17 | 14037 | if (type_specifiers.any_specifiers_p) |
058b15c1 | 14038 | { |
62d1db17 | 14039 | decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL); |
058b15c1 MM |
14040 | if (decl == NULL_TREE) |
14041 | error ("invalid catch parameter"); | |
14042 | } | |
14043 | else | |
14044 | decl = NULL_TREE; | |
14045 | ||
14046 | return decl; | |
a723baf1 MM |
14047 | } |
14048 | ||
21526606 | 14049 | /* Parse a throw-expression. |
a723baf1 MM |
14050 | |
14051 | throw-expression: | |
34cd5ae7 | 14052 | throw assignment-expression [opt] |
a723baf1 MM |
14053 | |
14054 | Returns a THROW_EXPR representing the throw-expression. */ | |
14055 | ||
14056 | static tree | |
94edc4ab | 14057 | cp_parser_throw_expression (cp_parser* parser) |
a723baf1 MM |
14058 | { |
14059 | tree expression; | |
89f1a6ec | 14060 | cp_token* token; |
a723baf1 MM |
14061 | |
14062 | cp_parser_require_keyword (parser, RID_THROW, "`throw'"); | |
89f1a6ec MM |
14063 | token = cp_lexer_peek_token (parser->lexer); |
14064 | /* Figure out whether or not there is an assignment-expression | |
14065 | following the "throw" keyword. */ | |
14066 | if (token->type == CPP_COMMA | |
14067 | || token->type == CPP_SEMICOLON | |
14068 | || token->type == CPP_CLOSE_PAREN | |
14069 | || token->type == CPP_CLOSE_SQUARE | |
14070 | || token->type == CPP_CLOSE_BRACE | |
14071 | || token->type == CPP_COLON) | |
a723baf1 | 14072 | expression = NULL_TREE; |
89f1a6ec | 14073 | else |
93678513 MM |
14074 | expression = cp_parser_assignment_expression (parser, |
14075 | /*cast_p=*/false); | |
a723baf1 MM |
14076 | |
14077 | return build_throw (expression); | |
14078 | } | |
14079 | ||
14080 | /* GNU Extensions */ | |
14081 | ||
14082 | /* Parse an (optional) asm-specification. | |
14083 | ||
14084 | asm-specification: | |
14085 | asm ( string-literal ) | |
14086 | ||
14087 | If the asm-specification is present, returns a STRING_CST | |
14088 | corresponding to the string-literal. Otherwise, returns | |
14089 | NULL_TREE. */ | |
14090 | ||
14091 | static tree | |
94edc4ab | 14092 | cp_parser_asm_specification_opt (cp_parser* parser) |
a723baf1 MM |
14093 | { |
14094 | cp_token *token; | |
14095 | tree asm_specification; | |
14096 | ||
14097 | /* Peek at the next token. */ | |
14098 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 14099 | /* If the next token isn't the `asm' keyword, then there's no |
a723baf1 MM |
14100 | asm-specification. */ |
14101 | if (!cp_parser_is_keyword (token, RID_ASM)) | |
14102 | return NULL_TREE; | |
14103 | ||
14104 | /* Consume the `asm' token. */ | |
14105 | cp_lexer_consume_token (parser->lexer); | |
14106 | /* Look for the `('. */ | |
14107 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
14108 | ||
14109 | /* Look for the string-literal. */ | |
c162c75e | 14110 | asm_specification = cp_parser_string_literal (parser, false, false); |
a723baf1 MM |
14111 | |
14112 | /* Look for the `)'. */ | |
14113 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`('"); | |
14114 | ||
14115 | return asm_specification; | |
14116 | } | |
14117 | ||
21526606 | 14118 | /* Parse an asm-operand-list. |
a723baf1 MM |
14119 | |
14120 | asm-operand-list: | |
14121 | asm-operand | |
14122 | asm-operand-list , asm-operand | |
21526606 | 14123 | |
a723baf1 | 14124 | asm-operand: |
21526606 | 14125 | string-literal ( expression ) |
a723baf1 MM |
14126 | [ string-literal ] string-literal ( expression ) |
14127 | ||
14128 | Returns a TREE_LIST representing the operands. The TREE_VALUE of | |
14129 | each node is the expression. The TREE_PURPOSE is itself a | |
14130 | TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed | |
14131 | string-literal (or NULL_TREE if not present) and whose TREE_VALUE | |
14132 | is a STRING_CST for the string literal before the parenthesis. */ | |
14133 | ||
14134 | static tree | |
94edc4ab | 14135 | cp_parser_asm_operand_list (cp_parser* parser) |
a723baf1 MM |
14136 | { |
14137 | tree asm_operands = NULL_TREE; | |
14138 | ||
14139 | while (true) | |
14140 | { | |
14141 | tree string_literal; | |
14142 | tree expression; | |
14143 | tree name; | |
21526606 | 14144 | |
21526606 | 14145 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) |
a723baf1 MM |
14146 | { |
14147 | /* Consume the `[' token. */ | |
14148 | cp_lexer_consume_token (parser->lexer); | |
14149 | /* Read the operand name. */ | |
14150 | name = cp_parser_identifier (parser); | |
21526606 | 14151 | if (name != error_mark_node) |
a723baf1 MM |
14152 | name = build_string (IDENTIFIER_LENGTH (name), |
14153 | IDENTIFIER_POINTER (name)); | |
14154 | /* Look for the closing `]'. */ | |
14155 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
14156 | } | |
14157 | else | |
14158 | name = NULL_TREE; | |
14159 | /* Look for the string-literal. */ | |
c162c75e MA |
14160 | string_literal = cp_parser_string_literal (parser, false, false); |
14161 | ||
a723baf1 MM |
14162 | /* Look for the `('. */ |
14163 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
14164 | /* Parse the expression. */ | |
93678513 | 14165 | expression = cp_parser_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
14166 | /* Look for the `)'. */ |
14167 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
c162c75e | 14168 | |
a723baf1 MM |
14169 | /* Add this operand to the list. */ |
14170 | asm_operands = tree_cons (build_tree_list (name, string_literal), | |
21526606 | 14171 | expression, |
a723baf1 | 14172 | asm_operands); |
21526606 | 14173 | /* If the next token is not a `,', there are no more |
a723baf1 MM |
14174 | operands. */ |
14175 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
14176 | break; | |
14177 | /* Consume the `,'. */ | |
14178 | cp_lexer_consume_token (parser->lexer); | |
14179 | } | |
14180 | ||
14181 | return nreverse (asm_operands); | |
14182 | } | |
14183 | ||
21526606 | 14184 | /* Parse an asm-clobber-list. |
a723baf1 MM |
14185 | |
14186 | asm-clobber-list: | |
14187 | string-literal | |
21526606 | 14188 | asm-clobber-list , string-literal |
a723baf1 MM |
14189 | |
14190 | Returns a TREE_LIST, indicating the clobbers in the order that they | |
14191 | appeared. The TREE_VALUE of each node is a STRING_CST. */ | |
14192 | ||
14193 | static tree | |
94edc4ab | 14194 | cp_parser_asm_clobber_list (cp_parser* parser) |
a723baf1 MM |
14195 | { |
14196 | tree clobbers = NULL_TREE; | |
14197 | ||
14198 | while (true) | |
14199 | { | |
a723baf1 MM |
14200 | tree string_literal; |
14201 | ||
14202 | /* Look for the string literal. */ | |
c162c75e | 14203 | string_literal = cp_parser_string_literal (parser, false, false); |
a723baf1 MM |
14204 | /* Add it to the list. */ |
14205 | clobbers = tree_cons (NULL_TREE, string_literal, clobbers); | |
21526606 | 14206 | /* If the next token is not a `,', then the list is |
a723baf1 MM |
14207 | complete. */ |
14208 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
14209 | break; | |
14210 | /* Consume the `,' token. */ | |
14211 | cp_lexer_consume_token (parser->lexer); | |
14212 | } | |
14213 | ||
14214 | return clobbers; | |
14215 | } | |
14216 | ||
14217 | /* Parse an (optional) series of attributes. | |
14218 | ||
14219 | attributes: | |
14220 | attributes attribute | |
14221 | ||
14222 | attribute: | |
21526606 | 14223 | __attribute__ (( attribute-list [opt] )) |
a723baf1 MM |
14224 | |
14225 | The return value is as for cp_parser_attribute_list. */ | |
21526606 | 14226 | |
a723baf1 | 14227 | static tree |
94edc4ab | 14228 | cp_parser_attributes_opt (cp_parser* parser) |
a723baf1 MM |
14229 | { |
14230 | tree attributes = NULL_TREE; | |
14231 | ||
14232 | while (true) | |
14233 | { | |
14234 | cp_token *token; | |
14235 | tree attribute_list; | |
14236 | ||
14237 | /* Peek at the next token. */ | |
14238 | token = cp_lexer_peek_token (parser->lexer); | |
14239 | /* If it's not `__attribute__', then we're done. */ | |
14240 | if (token->keyword != RID_ATTRIBUTE) | |
14241 | break; | |
14242 | ||
14243 | /* Consume the `__attribute__' keyword. */ | |
14244 | cp_lexer_consume_token (parser->lexer); | |
14245 | /* Look for the two `(' tokens. */ | |
14246 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
14247 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
14248 | ||
14249 | /* Peek at the next token. */ | |
14250 | token = cp_lexer_peek_token (parser->lexer); | |
14251 | if (token->type != CPP_CLOSE_PAREN) | |
14252 | /* Parse the attribute-list. */ | |
14253 | attribute_list = cp_parser_attribute_list (parser); | |
14254 | else | |
14255 | /* If the next token is a `)', then there is no attribute | |
14256 | list. */ | |
14257 | attribute_list = NULL; | |
14258 | ||
14259 | /* Look for the two `)' tokens. */ | |
14260 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
14261 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
14262 | ||
14263 | /* Add these new attributes to the list. */ | |
14264 | attributes = chainon (attributes, attribute_list); | |
14265 | } | |
14266 | ||
14267 | return attributes; | |
14268 | } | |
14269 | ||
21526606 | 14270 | /* Parse an attribute-list. |
a723baf1 | 14271 | |
21526606 EC |
14272 | attribute-list: |
14273 | attribute | |
a723baf1 MM |
14274 | attribute-list , attribute |
14275 | ||
14276 | attribute: | |
21526606 | 14277 | identifier |
a723baf1 MM |
14278 | identifier ( identifier ) |
14279 | identifier ( identifier , expression-list ) | |
21526606 | 14280 | identifier ( expression-list ) |
a723baf1 | 14281 | |
88e95ee3 MM |
14282 | Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds |
14283 | to an attribute. The TREE_PURPOSE of each node is the identifier | |
14284 | indicating which attribute is in use. The TREE_VALUE represents | |
14285 | the arguments, if any. */ | |
a723baf1 MM |
14286 | |
14287 | static tree | |
94edc4ab | 14288 | cp_parser_attribute_list (cp_parser* parser) |
a723baf1 MM |
14289 | { |
14290 | tree attribute_list = NULL_TREE; | |
c162c75e | 14291 | bool save_translate_strings_p = parser->translate_strings_p; |
a723baf1 | 14292 | |
c162c75e | 14293 | parser->translate_strings_p = false; |
a723baf1 MM |
14294 | while (true) |
14295 | { | |
14296 | cp_token *token; | |
14297 | tree identifier; | |
14298 | tree attribute; | |
14299 | ||
14300 | /* Look for the identifier. We also allow keywords here; for | |
14301 | example `__attribute__ ((const))' is legal. */ | |
14302 | token = cp_lexer_peek_token (parser->lexer); | |
88e95ee3 MM |
14303 | if (token->type == CPP_NAME |
14304 | || token->type == CPP_KEYWORD) | |
14305 | { | |
14306 | /* Consume the token. */ | |
14307 | token = cp_lexer_consume_token (parser->lexer); | |
21526606 | 14308 | |
88e95ee3 MM |
14309 | /* Save away the identifier that indicates which attribute |
14310 | this is. */ | |
14311 | identifier = token->value; | |
14312 | attribute = build_tree_list (identifier, NULL_TREE); | |
a723baf1 | 14313 | |
88e95ee3 MM |
14314 | /* Peek at the next token. */ |
14315 | token = cp_lexer_peek_token (parser->lexer); | |
14316 | /* If it's an `(', then parse the attribute arguments. */ | |
14317 | if (token->type == CPP_OPEN_PAREN) | |
14318 | { | |
14319 | tree arguments; | |
a723baf1 | 14320 | |
88e95ee3 MM |
14321 | arguments = (cp_parser_parenthesized_expression_list |
14322 | (parser, true, /*cast_p=*/false, | |
14323 | /*non_constant_p=*/NULL)); | |
14324 | /* Save the identifier and arguments away. */ | |
14325 | TREE_VALUE (attribute) = arguments; | |
14326 | } | |
a723baf1 | 14327 | |
88e95ee3 MM |
14328 | /* Add this attribute to the list. */ |
14329 | TREE_CHAIN (attribute) = attribute_list; | |
14330 | attribute_list = attribute; | |
a723baf1 | 14331 | |
88e95ee3 MM |
14332 | token = cp_lexer_peek_token (parser->lexer); |
14333 | } | |
14334 | /* Now, look for more attributes. If the next token isn't a | |
14335 | `,', we're done. */ | |
a723baf1 MM |
14336 | if (token->type != CPP_COMMA) |
14337 | break; | |
14338 | ||
cd0be382 | 14339 | /* Consume the comma and keep going. */ |
a723baf1 MM |
14340 | cp_lexer_consume_token (parser->lexer); |
14341 | } | |
c162c75e | 14342 | parser->translate_strings_p = save_translate_strings_p; |
a723baf1 MM |
14343 | |
14344 | /* We built up the list in reverse order. */ | |
14345 | return nreverse (attribute_list); | |
14346 | } | |
14347 | ||
14348 | /* Parse an optional `__extension__' keyword. Returns TRUE if it is | |
14349 | present, and FALSE otherwise. *SAVED_PEDANTIC is set to the | |
14350 | current value of the PEDANTIC flag, regardless of whether or not | |
14351 | the `__extension__' keyword is present. The caller is responsible | |
14352 | for restoring the value of the PEDANTIC flag. */ | |
14353 | ||
14354 | static bool | |
94edc4ab | 14355 | cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic) |
a723baf1 MM |
14356 | { |
14357 | /* Save the old value of the PEDANTIC flag. */ | |
14358 | *saved_pedantic = pedantic; | |
14359 | ||
14360 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION)) | |
14361 | { | |
14362 | /* Consume the `__extension__' token. */ | |
14363 | cp_lexer_consume_token (parser->lexer); | |
14364 | /* We're not being pedantic while the `__extension__' keyword is | |
14365 | in effect. */ | |
14366 | pedantic = 0; | |
14367 | ||
14368 | return true; | |
14369 | } | |
14370 | ||
14371 | return false; | |
14372 | } | |
14373 | ||
14374 | /* Parse a label declaration. | |
14375 | ||
14376 | label-declaration: | |
14377 | __label__ label-declarator-seq ; | |
14378 | ||
14379 | label-declarator-seq: | |
14380 | identifier , label-declarator-seq | |
14381 | identifier */ | |
14382 | ||
14383 | static void | |
94edc4ab | 14384 | cp_parser_label_declaration (cp_parser* parser) |
a723baf1 MM |
14385 | { |
14386 | /* Look for the `__label__' keyword. */ | |
14387 | cp_parser_require_keyword (parser, RID_LABEL, "`__label__'"); | |
14388 | ||
14389 | while (true) | |
14390 | { | |
14391 | tree identifier; | |
14392 | ||
14393 | /* Look for an identifier. */ | |
14394 | identifier = cp_parser_identifier (parser); | |
cb6d4a9f VR |
14395 | /* If we failed, stop. */ |
14396 | if (identifier == error_mark_node) | |
14397 | break; | |
14398 | /* Declare it as a label. */ | |
a723baf1 MM |
14399 | finish_label_decl (identifier); |
14400 | /* If the next token is a `;', stop. */ | |
14401 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
14402 | break; | |
14403 | /* Look for the `,' separating the label declarations. */ | |
14404 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
14405 | } | |
14406 | ||
14407 | /* Look for the final `;'. */ | |
14408 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
14409 | } | |
14410 | ||
14411 | /* Support Functions */ | |
14412 | ||
14413 | /* Looks up NAME in the current scope, as given by PARSER->SCOPE. | |
14414 | NAME should have one of the representations used for an | |
14415 | id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE | |
14416 | is returned. If PARSER->SCOPE is a dependent type, then a | |
14417 | SCOPE_REF is returned. | |
14418 | ||
14419 | If NAME is a TEMPLATE_ID_EXPR, then it will be immediately | |
14420 | returned; the name was already resolved when the TEMPLATE_ID_EXPR | |
14421 | was formed. Abstractly, such entities should not be passed to this | |
14422 | function, because they do not need to be looked up, but it is | |
14423 | simpler to check for this special case here, rather than at the | |
14424 | call-sites. | |
14425 | ||
14426 | In cases not explicitly covered above, this function returns a | |
14427 | DECL, OVERLOAD, or baselink representing the result of the lookup. | |
14428 | If there was no entity with the indicated NAME, the ERROR_MARK_NODE | |
14429 | is returned. | |
14430 | ||
472c29c3 | 14431 | If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword |
fc6a28d7 MM |
14432 | (e.g., "struct") that was used. In that case bindings that do not |
14433 | refer to types are ignored. | |
a723baf1 | 14434 | |
b0bc6e8e KL |
14435 | If IS_TEMPLATE is TRUE, bindings that do not refer to templates are |
14436 | ignored. | |
14437 | ||
eea9800f MM |
14438 | If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces |
14439 | are ignored. | |
14440 | ||
a723baf1 | 14441 | If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent |
8f78f01f MM |
14442 | types. |
14443 | ||
14444 | If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup | |
14445 | results in an ambiguity, and false otherwise. */ | |
a723baf1 MM |
14446 | |
14447 | static tree | |
21526606 | 14448 | cp_parser_lookup_name (cp_parser *parser, tree name, |
fc6a28d7 MM |
14449 | enum tag_types tag_type, |
14450 | bool is_template, bool is_namespace, | |
8f78f01f MM |
14451 | bool check_dependency, |
14452 | bool *ambiguous_p) | |
a723baf1 MM |
14453 | { |
14454 | tree decl; | |
14455 | tree object_type = parser->context->object_type; | |
14456 | ||
8f78f01f MM |
14457 | /* Assume that the lookup will be unambiguous. */ |
14458 | if (ambiguous_p) | |
14459 | *ambiguous_p = false; | |
14460 | ||
a723baf1 MM |
14461 | /* Now that we have looked up the name, the OBJECT_TYPE (if any) is |
14462 | no longer valid. Note that if we are parsing tentatively, and | |
14463 | the parse fails, OBJECT_TYPE will be automatically restored. */ | |
14464 | parser->context->object_type = NULL_TREE; | |
14465 | ||
14466 | if (name == error_mark_node) | |
14467 | return error_mark_node; | |
14468 | ||
14469 | /* A template-id has already been resolved; there is no lookup to | |
14470 | do. */ | |
14471 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
14472 | return name; | |
14473 | if (BASELINK_P (name)) | |
14474 | { | |
50bc768d NS |
14475 | gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name)) |
14476 | == TEMPLATE_ID_EXPR); | |
a723baf1 MM |
14477 | return name; |
14478 | } | |
14479 | ||
14480 | /* A BIT_NOT_EXPR is used to represent a destructor. By this point, | |
14481 | it should already have been checked to make sure that the name | |
14482 | used matches the type being destroyed. */ | |
14483 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
14484 | { | |
14485 | tree type; | |
14486 | ||
14487 | /* Figure out to which type this destructor applies. */ | |
14488 | if (parser->scope) | |
14489 | type = parser->scope; | |
14490 | else if (object_type) | |
14491 | type = object_type; | |
14492 | else | |
14493 | type = current_class_type; | |
14494 | /* If that's not a class type, there is no destructor. */ | |
14495 | if (!type || !CLASS_TYPE_P (type)) | |
14496 | return error_mark_node; | |
9f4faeae MM |
14497 | if (CLASSTYPE_LAZY_DESTRUCTOR (type)) |
14498 | lazily_declare_fn (sfk_destructor, type); | |
fd6e3cce GB |
14499 | if (!CLASSTYPE_DESTRUCTORS (type)) |
14500 | return error_mark_node; | |
a723baf1 MM |
14501 | /* If it was a class type, return the destructor. */ |
14502 | return CLASSTYPE_DESTRUCTORS (type); | |
14503 | } | |
14504 | ||
14505 | /* By this point, the NAME should be an ordinary identifier. If | |
14506 | the id-expression was a qualified name, the qualifying scope is | |
14507 | stored in PARSER->SCOPE at this point. */ | |
50bc768d | 14508 | gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); |
21526606 | 14509 | |
a723baf1 MM |
14510 | /* Perform the lookup. */ |
14511 | if (parser->scope) | |
21526606 | 14512 | { |
1fb3244a | 14513 | bool dependent_p; |
a723baf1 MM |
14514 | |
14515 | if (parser->scope == error_mark_node) | |
14516 | return error_mark_node; | |
14517 | ||
14518 | /* If the SCOPE is dependent, the lookup must be deferred until | |
14519 | the template is instantiated -- unless we are explicitly | |
14520 | looking up names in uninstantiated templates. Even then, we | |
14521 | cannot look up the name if the scope is not a class type; it | |
14522 | might, for example, be a template type parameter. */ | |
1fb3244a MM |
14523 | dependent_p = (TYPE_P (parser->scope) |
14524 | && !(parser->in_declarator_p | |
14525 | && currently_open_class (parser->scope)) | |
14526 | && dependent_type_p (parser->scope)); | |
a723baf1 | 14527 | if ((check_dependency || !CLASS_TYPE_P (parser->scope)) |
1fb3244a | 14528 | && dependent_p) |
a723baf1 | 14529 | { |
fc6a28d7 MM |
14530 | if (tag_type) |
14531 | { | |
14532 | tree type; | |
14533 | ||
14534 | /* The resolution to Core Issue 180 says that `struct | |
14535 | A::B' should be considered a type-name, even if `A' | |
14536 | is dependent. */ | |
14537 | type = make_typename_type (parser->scope, name, tag_type, | |
14538 | /*complain=*/1); | |
fc6a28d7 MM |
14539 | decl = TYPE_NAME (type); |
14540 | } | |
b0bc6e8e | 14541 | else if (is_template) |
5b4acce1 | 14542 | decl = make_unbound_class_template (parser->scope, |
b939a023 | 14543 | name, NULL_TREE, |
5b4acce1 | 14544 | /*complain=*/1); |
b0bc6e8e KL |
14545 | else |
14546 | decl = build_nt (SCOPE_REF, parser->scope, name); | |
a723baf1 MM |
14547 | } |
14548 | else | |
14549 | { | |
4514aa8c | 14550 | tree pushed_scope = NULL_TREE; |
91b004e5 | 14551 | |
a723baf1 MM |
14552 | /* If PARSER->SCOPE is a dependent type, then it must be a |
14553 | class type, and we must not be checking dependencies; | |
14554 | otherwise, we would have processed this lookup above. So | |
14555 | that PARSER->SCOPE is not considered a dependent base by | |
14556 | lookup_member, we must enter the scope here. */ | |
1fb3244a | 14557 | if (dependent_p) |
4514aa8c | 14558 | pushed_scope = push_scope (parser->scope); |
78dcd41a | 14559 | /* If the PARSER->SCOPE is a template specialization, it |
a723baf1 MM |
14560 | may be instantiated during name lookup. In that case, |
14561 | errors may be issued. Even if we rollback the current | |
14562 | tentative parse, those errors are valid. */ | |
fc6a28d7 MM |
14563 | decl = lookup_qualified_name (parser->scope, name, |
14564 | tag_type != none_type, | |
5e08432e | 14565 | /*complain=*/true); |
4514aa8c NS |
14566 | if (pushed_scope) |
14567 | pop_scope (pushed_scope); | |
a723baf1 MM |
14568 | } |
14569 | parser->qualifying_scope = parser->scope; | |
14570 | parser->object_scope = NULL_TREE; | |
14571 | } | |
14572 | else if (object_type) | |
14573 | { | |
14574 | tree object_decl = NULL_TREE; | |
14575 | /* Look up the name in the scope of the OBJECT_TYPE, unless the | |
14576 | OBJECT_TYPE is not a class. */ | |
14577 | if (CLASS_TYPE_P (object_type)) | |
14578 | /* If the OBJECT_TYPE is a template specialization, it may | |
14579 | be instantiated during name lookup. In that case, errors | |
14580 | may be issued. Even if we rollback the current tentative | |
14581 | parse, those errors are valid. */ | |
14582 | object_decl = lookup_member (object_type, | |
14583 | name, | |
fc6a28d7 MM |
14584 | /*protect=*/0, |
14585 | tag_type != none_type); | |
a723baf1 | 14586 | /* Look it up in the enclosing context, too. */ |
fc6a28d7 MM |
14587 | decl = lookup_name_real (name, tag_type != none_type, |
14588 | /*nonclass=*/0, | |
12cf89fa | 14589 | /*block_p=*/true, is_namespace, |
a723baf1 MM |
14590 | /*flags=*/0); |
14591 | parser->object_scope = object_type; | |
14592 | parser->qualifying_scope = NULL_TREE; | |
14593 | if (object_decl) | |
14594 | decl = object_decl; | |
14595 | } | |
14596 | else | |
14597 | { | |
fc6a28d7 MM |
14598 | decl = lookup_name_real (name, tag_type != none_type, |
14599 | /*nonclass=*/0, | |
12cf89fa | 14600 | /*block_p=*/true, is_namespace, |
a723baf1 MM |
14601 | /*flags=*/0); |
14602 | parser->qualifying_scope = NULL_TREE; | |
14603 | parser->object_scope = NULL_TREE; | |
14604 | } | |
14605 | ||
14606 | /* If the lookup failed, let our caller know. */ | |
bd3d082e | 14607 | if (!decl || decl == error_mark_node) |
a723baf1 MM |
14608 | return error_mark_node; |
14609 | ||
14610 | /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ | |
14611 | if (TREE_CODE (decl) == TREE_LIST) | |
14612 | { | |
8f78f01f MM |
14613 | if (ambiguous_p) |
14614 | *ambiguous_p = true; | |
a723baf1 MM |
14615 | /* The error message we have to print is too complicated for |
14616 | cp_parser_error, so we incorporate its actions directly. */ | |
e5976695 | 14617 | if (!cp_parser_simulate_error (parser)) |
a723baf1 | 14618 | { |
2a13a625 | 14619 | error ("reference to %qD is ambiguous", name); |
a723baf1 MM |
14620 | print_candidates (decl); |
14621 | } | |
14622 | return error_mark_node; | |
14623 | } | |
14624 | ||
50bc768d NS |
14625 | gcc_assert (DECL_P (decl) |
14626 | || TREE_CODE (decl) == OVERLOAD | |
14627 | || TREE_CODE (decl) == SCOPE_REF | |
14628 | || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE | |
14629 | || BASELINK_P (decl)); | |
a723baf1 MM |
14630 | |
14631 | /* If we have resolved the name of a member declaration, check to | |
14632 | see if the declaration is accessible. When the name resolves to | |
34cd5ae7 | 14633 | set of overloaded functions, accessibility is checked when |
21526606 | 14634 | overload resolution is done. |
a723baf1 MM |
14635 | |
14636 | During an explicit instantiation, access is not checked at all, | |
14637 | as per [temp.explicit]. */ | |
8d241e0b | 14638 | if (DECL_P (decl)) |
ee76b931 | 14639 | check_accessibility_of_qualified_id (decl, object_type, parser->scope); |
a723baf1 MM |
14640 | |
14641 | return decl; | |
14642 | } | |
14643 | ||
14644 | /* Like cp_parser_lookup_name, but for use in the typical case where | |
b0bc6e8e KL |
14645 | CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE, |
14646 | IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */ | |
a723baf1 MM |
14647 | |
14648 | static tree | |
94edc4ab | 14649 | cp_parser_lookup_name_simple (cp_parser* parser, tree name) |
a723baf1 | 14650 | { |
21526606 | 14651 | return cp_parser_lookup_name (parser, name, |
fc6a28d7 | 14652 | none_type, |
b0bc6e8e | 14653 | /*is_template=*/false, |
eea9800f | 14654 | /*is_namespace=*/false, |
8f78f01f MM |
14655 | /*check_dependency=*/true, |
14656 | /*ambiguous_p=*/NULL); | |
a723baf1 MM |
14657 | } |
14658 | ||
a723baf1 MM |
14659 | /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in |
14660 | the current context, return the TYPE_DECL. If TAG_NAME_P is | |
14661 | true, the DECL indicates the class being defined in a class-head, | |
14662 | or declared in an elaborated-type-specifier. | |
14663 | ||
14664 | Otherwise, return DECL. */ | |
14665 | ||
14666 | static tree | |
14667 | cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p) | |
14668 | { | |
710b73e6 KL |
14669 | /* If the TEMPLATE_DECL is being declared as part of a class-head, |
14670 | the translation from TEMPLATE_DECL to TYPE_DECL occurs: | |
a723baf1 | 14671 | |
21526606 | 14672 | struct A { |
a723baf1 MM |
14673 | template <typename T> struct B; |
14674 | }; | |
14675 | ||
21526606 EC |
14676 | template <typename T> struct A::B {}; |
14677 | ||
a723baf1 MM |
14678 | Similarly, in a elaborated-type-specifier: |
14679 | ||
14680 | namespace N { struct X{}; } | |
14681 | ||
14682 | struct A { | |
14683 | template <typename T> friend struct N::X; | |
14684 | }; | |
14685 | ||
710b73e6 KL |
14686 | However, if the DECL refers to a class type, and we are in |
14687 | the scope of the class, then the name lookup automatically | |
14688 | finds the TYPE_DECL created by build_self_reference rather | |
14689 | than a TEMPLATE_DECL. For example, in: | |
14690 | ||
14691 | template <class T> struct S { | |
14692 | S s; | |
14693 | }; | |
14694 | ||
14695 | there is no need to handle such case. */ | |
14696 | ||
14697 | if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p) | |
a723baf1 MM |
14698 | return DECL_TEMPLATE_RESULT (decl); |
14699 | ||
14700 | return decl; | |
14701 | } | |
14702 | ||
14703 | /* If too many, or too few, template-parameter lists apply to the | |
14704 | declarator, issue an error message. Returns TRUE if all went well, | |
14705 | and FALSE otherwise. */ | |
14706 | ||
14707 | static bool | |
21526606 | 14708 | cp_parser_check_declarator_template_parameters (cp_parser* parser, |
058b15c1 | 14709 | cp_declarator *declarator) |
a723baf1 MM |
14710 | { |
14711 | unsigned num_templates; | |
14712 | ||
14713 | /* We haven't seen any classes that involve template parameters yet. */ | |
14714 | num_templates = 0; | |
14715 | ||
058b15c1 | 14716 | switch (declarator->kind) |
a723baf1 | 14717 | { |
058b15c1 | 14718 | case cdk_id: |
1d786913 | 14719 | if (declarator->u.id.qualifying_scope) |
058b15c1 MM |
14720 | { |
14721 | tree scope; | |
14722 | tree member; | |
a723baf1 | 14723 | |
1d786913 MM |
14724 | scope = declarator->u.id.qualifying_scope; |
14725 | member = declarator->u.id.unqualified_name; | |
a723baf1 | 14726 | |
058b15c1 MM |
14727 | while (scope && CLASS_TYPE_P (scope)) |
14728 | { | |
14729 | /* You're supposed to have one `template <...>' | |
14730 | for every template class, but you don't need one | |
14731 | for a full specialization. For example: | |
14732 | ||
14733 | template <class T> struct S{}; | |
14734 | template <> struct S<int> { void f(); }; | |
14735 | void S<int>::f () {} | |
14736 | ||
14737 | is correct; there shouldn't be a `template <>' for | |
14738 | the definition of `S<int>::f'. */ | |
14739 | if (CLASSTYPE_TEMPLATE_INFO (scope) | |
14740 | && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope) | |
14741 | || uses_template_parms (CLASSTYPE_TI_ARGS (scope))) | |
14742 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))) | |
14743 | ++num_templates; | |
14744 | ||
14745 | scope = TYPE_CONTEXT (scope); | |
14746 | } | |
14747 | } | |
1d786913 MM |
14748 | else if (TREE_CODE (declarator->u.id.unqualified_name) |
14749 | == TEMPLATE_ID_EXPR) | |
14750 | /* If the DECLARATOR has the form `X<y>' then it uses one | |
14751 | additional level of template parameters. */ | |
a723baf1 MM |
14752 | ++num_templates; |
14753 | ||
21526606 | 14754 | return cp_parser_check_template_parameters (parser, |
a723baf1 | 14755 | num_templates); |
058b15c1 MM |
14756 | |
14757 | case cdk_function: | |
14758 | case cdk_array: | |
14759 | case cdk_pointer: | |
14760 | case cdk_reference: | |
14761 | case cdk_ptrmem: | |
98ca843c | 14762 | return (cp_parser_check_declarator_template_parameters |
058b15c1 MM |
14763 | (parser, declarator->declarator)); |
14764 | ||
14765 | case cdk_error: | |
14766 | return true; | |
14767 | ||
14768 | default: | |
315fb5db | 14769 | gcc_unreachable (); |
a723baf1 | 14770 | } |
315fb5db | 14771 | return false; |
a723baf1 MM |
14772 | } |
14773 | ||
14774 | /* NUM_TEMPLATES were used in the current declaration. If that is | |
14775 | invalid, return FALSE and issue an error messages. Otherwise, | |
14776 | return TRUE. */ | |
14777 | ||
14778 | static bool | |
94edc4ab NN |
14779 | cp_parser_check_template_parameters (cp_parser* parser, |
14780 | unsigned num_templates) | |
a723baf1 MM |
14781 | { |
14782 | /* If there are more template classes than parameter lists, we have | |
14783 | something like: | |
21526606 | 14784 | |
a723baf1 MM |
14785 | template <class T> void S<T>::R<T>::f (); */ |
14786 | if (parser->num_template_parameter_lists < num_templates) | |
14787 | { | |
14788 | error ("too few template-parameter-lists"); | |
14789 | return false; | |
14790 | } | |
14791 | /* If there are the same number of template classes and parameter | |
14792 | lists, that's OK. */ | |
14793 | if (parser->num_template_parameter_lists == num_templates) | |
14794 | return true; | |
14795 | /* If there are more, but only one more, then we are referring to a | |
14796 | member template. That's OK too. */ | |
14797 | if (parser->num_template_parameter_lists == num_templates + 1) | |
14798 | return true; | |
14799 | /* Otherwise, there are too many template parameter lists. We have | |
14800 | something like: | |
14801 | ||
14802 | template <class T> template <class U> void S::f(); */ | |
14803 | error ("too many template-parameter-lists"); | |
14804 | return false; | |
14805 | } | |
14806 | ||
a723baf1 MM |
14807 | /* Parse an optional `::' token indicating that the following name is |
14808 | from the global namespace. If so, PARSER->SCOPE is set to the | |
14809 | GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE, | |
14810 | unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone. | |
14811 | Returns the new value of PARSER->SCOPE, if the `::' token is | |
14812 | present, and NULL_TREE otherwise. */ | |
14813 | ||
14814 | static tree | |
94edc4ab | 14815 | cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p) |
a723baf1 MM |
14816 | { |
14817 | cp_token *token; | |
14818 | ||
14819 | /* Peek at the next token. */ | |
14820 | token = cp_lexer_peek_token (parser->lexer); | |
14821 | /* If we're looking at a `::' token then we're starting from the | |
14822 | global namespace, not our current location. */ | |
14823 | if (token->type == CPP_SCOPE) | |
14824 | { | |
14825 | /* Consume the `::' token. */ | |
14826 | cp_lexer_consume_token (parser->lexer); | |
14827 | /* Set the SCOPE so that we know where to start the lookup. */ | |
14828 | parser->scope = global_namespace; | |
14829 | parser->qualifying_scope = global_namespace; | |
14830 | parser->object_scope = NULL_TREE; | |
14831 | ||
14832 | return parser->scope; | |
14833 | } | |
14834 | else if (!current_scope_valid_p) | |
14835 | { | |
14836 | parser->scope = NULL_TREE; | |
14837 | parser->qualifying_scope = NULL_TREE; | |
14838 | parser->object_scope = NULL_TREE; | |
14839 | } | |
14840 | ||
14841 | return NULL_TREE; | |
14842 | } | |
14843 | ||
14844 | /* Returns TRUE if the upcoming token sequence is the start of a | |
14845 | constructor declarator. If FRIEND_P is true, the declarator is | |
14846 | preceded by the `friend' specifier. */ | |
14847 | ||
14848 | static bool | |
14849 | cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p) | |
14850 | { | |
14851 | bool constructor_p; | |
14852 | tree type_decl = NULL_TREE; | |
14853 | bool nested_name_p; | |
2050a1bb MM |
14854 | cp_token *next_token; |
14855 | ||
14856 | /* The common case is that this is not a constructor declarator, so | |
8fbc5ae7 MM |
14857 | try to avoid doing lots of work if at all possible. It's not |
14858 | valid declare a constructor at function scope. */ | |
14859 | if (at_function_scope_p ()) | |
14860 | return false; | |
14861 | /* And only certain tokens can begin a constructor declarator. */ | |
2050a1bb MM |
14862 | next_token = cp_lexer_peek_token (parser->lexer); |
14863 | if (next_token->type != CPP_NAME | |
14864 | && next_token->type != CPP_SCOPE | |
14865 | && next_token->type != CPP_NESTED_NAME_SPECIFIER | |
14866 | && next_token->type != CPP_TEMPLATE_ID) | |
14867 | return false; | |
a723baf1 MM |
14868 | |
14869 | /* Parse tentatively; we are going to roll back all of the tokens | |
14870 | consumed here. */ | |
14871 | cp_parser_parse_tentatively (parser); | |
14872 | /* Assume that we are looking at a constructor declarator. */ | |
14873 | constructor_p = true; | |
8d241e0b | 14874 | |
a723baf1 MM |
14875 | /* Look for the optional `::' operator. */ |
14876 | cp_parser_global_scope_opt (parser, | |
14877 | /*current_scope_valid_p=*/false); | |
14878 | /* Look for the nested-name-specifier. */ | |
21526606 | 14879 | nested_name_p |
a723baf1 MM |
14880 | = (cp_parser_nested_name_specifier_opt (parser, |
14881 | /*typename_keyword_p=*/false, | |
14882 | /*check_dependency_p=*/false, | |
a668c6ad MM |
14883 | /*type_p=*/false, |
14884 | /*is_declaration=*/false) | |
a723baf1 MM |
14885 | != NULL_TREE); |
14886 | /* Outside of a class-specifier, there must be a | |
14887 | nested-name-specifier. */ | |
21526606 | 14888 | if (!nested_name_p && |
a723baf1 MM |
14889 | (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type) |
14890 | || friend_p)) | |
14891 | constructor_p = false; | |
14892 | /* If we still think that this might be a constructor-declarator, | |
14893 | look for a class-name. */ | |
14894 | if (constructor_p) | |
14895 | { | |
14896 | /* If we have: | |
14897 | ||
8fbc5ae7 | 14898 | template <typename T> struct S { S(); }; |
a723baf1 MM |
14899 | template <typename T> S<T>::S (); |
14900 | ||
14901 | we must recognize that the nested `S' names a class. | |
14902 | Similarly, for: | |
14903 | ||
14904 | template <typename T> S<T>::S<T> (); | |
14905 | ||
14906 | we must recognize that the nested `S' names a template. */ | |
14907 | type_decl = cp_parser_class_name (parser, | |
14908 | /*typename_keyword_p=*/false, | |
14909 | /*template_keyword_p=*/false, | |
fc6a28d7 | 14910 | none_type, |
a723baf1 | 14911 | /*check_dependency_p=*/false, |
a668c6ad MM |
14912 | /*class_head_p=*/false, |
14913 | /*is_declaration=*/false); | |
a723baf1 MM |
14914 | /* If there was no class-name, then this is not a constructor. */ |
14915 | constructor_p = !cp_parser_error_occurred (parser); | |
14916 | } | |
8d241e0b | 14917 | |
a723baf1 MM |
14918 | /* If we're still considering a constructor, we have to see a `(', |
14919 | to begin the parameter-declaration-clause, followed by either a | |
14920 | `)', an `...', or a decl-specifier. We need to check for a | |
14921 | type-specifier to avoid being fooled into thinking that: | |
14922 | ||
14923 | S::S (f) (int); | |
14924 | ||
14925 | is a constructor. (It is actually a function named `f' that | |
14926 | takes one parameter (of type `int') and returns a value of type | |
14927 | `S::S'. */ | |
21526606 | 14928 | if (constructor_p |
a723baf1 MM |
14929 | && cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) |
14930 | { | |
14931 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN) | |
14932 | && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS) | |
15077df5 MM |
14933 | /* A parameter declaration begins with a decl-specifier, |
14934 | which is either the "attribute" keyword, a storage class | |
14935 | specifier, or (usually) a type-specifier. */ | |
14936 | && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE) | |
a723baf1 MM |
14937 | && !cp_parser_storage_class_specifier_opt (parser)) |
14938 | { | |
5dae1114 | 14939 | tree type; |
4514aa8c | 14940 | tree pushed_scope = NULL_TREE; |
4047b164 | 14941 | unsigned saved_num_template_parameter_lists; |
5dae1114 MM |
14942 | |
14943 | /* Names appearing in the type-specifier should be looked up | |
14944 | in the scope of the class. */ | |
14945 | if (current_class_type) | |
14946 | type = NULL_TREE; | |
a723baf1 MM |
14947 | else |
14948 | { | |
5dae1114 MM |
14949 | type = TREE_TYPE (type_decl); |
14950 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
14d22dd6 | 14951 | { |
21526606 | 14952 | type = resolve_typename_type (type, |
14d22dd6 MM |
14953 | /*only_current_p=*/false); |
14954 | if (type == error_mark_node) | |
14955 | { | |
14956 | cp_parser_abort_tentative_parse (parser); | |
14957 | return false; | |
14958 | } | |
14959 | } | |
4514aa8c | 14960 | pushed_scope = push_scope (type); |
a723baf1 | 14961 | } |
4047b164 KL |
14962 | |
14963 | /* Inside the constructor parameter list, surrounding | |
14964 | template-parameter-lists do not apply. */ | |
14965 | saved_num_template_parameter_lists | |
14966 | = parser->num_template_parameter_lists; | |
14967 | parser->num_template_parameter_lists = 0; | |
14968 | ||
5dae1114 MM |
14969 | /* Look for the type-specifier. */ |
14970 | cp_parser_type_specifier (parser, | |
14971 | CP_PARSER_FLAGS_NONE, | |
62d1db17 | 14972 | /*decl_specs=*/NULL, |
5dae1114 MM |
14973 | /*is_declarator=*/true, |
14974 | /*declares_class_or_enum=*/NULL, | |
14975 | /*is_cv_qualifier=*/NULL); | |
4047b164 KL |
14976 | |
14977 | parser->num_template_parameter_lists | |
14978 | = saved_num_template_parameter_lists; | |
14979 | ||
5dae1114 | 14980 | /* Leave the scope of the class. */ |
4514aa8c NS |
14981 | if (pushed_scope) |
14982 | pop_scope (pushed_scope); | |
5dae1114 MM |
14983 | |
14984 | constructor_p = !cp_parser_error_occurred (parser); | |
a723baf1 MM |
14985 | } |
14986 | } | |
14987 | else | |
14988 | constructor_p = false; | |
14989 | /* We did not really want to consume any tokens. */ | |
14990 | cp_parser_abort_tentative_parse (parser); | |
14991 | ||
14992 | return constructor_p; | |
14993 | } | |
14994 | ||
14995 | /* Parse the definition of the function given by the DECL_SPECIFIERS, | |
cf22909c | 14996 | ATTRIBUTES, and DECLARATOR. The access checks have been deferred; |
a723baf1 MM |
14997 | they must be performed once we are in the scope of the function. |
14998 | ||
14999 | Returns the function defined. */ | |
15000 | ||
15001 | static tree | |
15002 | cp_parser_function_definition_from_specifiers_and_declarator | |
94edc4ab | 15003 | (cp_parser* parser, |
62d1db17 | 15004 | cp_decl_specifier_seq *decl_specifiers, |
94edc4ab | 15005 | tree attributes, |
058b15c1 | 15006 | const cp_declarator *declarator) |
a723baf1 MM |
15007 | { |
15008 | tree fn; | |
15009 | bool success_p; | |
15010 | ||
15011 | /* Begin the function-definition. */ | |
058b15c1 MM |
15012 | success_p = start_function (decl_specifiers, declarator, attributes); |
15013 | ||
15014 | /* The things we're about to see are not directly qualified by any | |
15015 | template headers we've seen thus far. */ | |
15016 | reset_specialization (); | |
a723baf1 MM |
15017 | |
15018 | /* If there were names looked up in the decl-specifier-seq that we | |
15019 | did not check, check them now. We must wait until we are in the | |
15020 | scope of the function to perform the checks, since the function | |
15021 | might be a friend. */ | |
cf22909c | 15022 | perform_deferred_access_checks (); |
a723baf1 MM |
15023 | |
15024 | if (!success_p) | |
15025 | { | |
058b15c1 | 15026 | /* Skip the entire function. */ |
a723baf1 MM |
15027 | error ("invalid function declaration"); |
15028 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
15029 | fn = error_mark_node; | |
15030 | } | |
15031 | else | |
15032 | fn = cp_parser_function_definition_after_declarator (parser, | |
15033 | /*inline_p=*/false); | |
15034 | ||
15035 | return fn; | |
15036 | } | |
15037 | ||
15038 | /* Parse the part of a function-definition that follows the | |
15039 | declarator. INLINE_P is TRUE iff this function is an inline | |
15040 | function defined with a class-specifier. | |
15041 | ||
15042 | Returns the function defined. */ | |
15043 | ||
21526606 EC |
15044 | static tree |
15045 | cp_parser_function_definition_after_declarator (cp_parser* parser, | |
94edc4ab | 15046 | bool inline_p) |
a723baf1 MM |
15047 | { |
15048 | tree fn; | |
15049 | bool ctor_initializer_p = false; | |
15050 | bool saved_in_unbraced_linkage_specification_p; | |
15051 | unsigned saved_num_template_parameter_lists; | |
15052 | ||
15053 | /* If the next token is `return', then the code may be trying to | |
15054 | make use of the "named return value" extension that G++ used to | |
15055 | support. */ | |
15056 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN)) | |
15057 | { | |
15058 | /* Consume the `return' keyword. */ | |
15059 | cp_lexer_consume_token (parser->lexer); | |
15060 | /* Look for the identifier that indicates what value is to be | |
15061 | returned. */ | |
15062 | cp_parser_identifier (parser); | |
15063 | /* Issue an error message. */ | |
15064 | error ("named return values are no longer supported"); | |
15065 | /* Skip tokens until we reach the start of the function body. */ | |
21eb631b MM |
15066 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE) |
15067 | && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF)) | |
a723baf1 MM |
15068 | cp_lexer_consume_token (parser->lexer); |
15069 | } | |
15070 | /* The `extern' in `extern "C" void f () { ... }' does not apply to | |
15071 | anything declared inside `f'. */ | |
21526606 | 15072 | saved_in_unbraced_linkage_specification_p |
a723baf1 MM |
15073 | = parser->in_unbraced_linkage_specification_p; |
15074 | parser->in_unbraced_linkage_specification_p = false; | |
15075 | /* Inside the function, surrounding template-parameter-lists do not | |
15076 | apply. */ | |
21526606 EC |
15077 | saved_num_template_parameter_lists |
15078 | = parser->num_template_parameter_lists; | |
a723baf1 MM |
15079 | parser->num_template_parameter_lists = 0; |
15080 | /* If the next token is `try', then we are looking at a | |
15081 | function-try-block. */ | |
15082 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY)) | |
15083 | ctor_initializer_p = cp_parser_function_try_block (parser); | |
15084 | /* A function-try-block includes the function-body, so we only do | |
15085 | this next part if we're not processing a function-try-block. */ | |
15086 | else | |
21526606 | 15087 | ctor_initializer_p |
a723baf1 MM |
15088 | = cp_parser_ctor_initializer_opt_and_function_body (parser); |
15089 | ||
15090 | /* Finish the function. */ | |
21526606 | 15091 | fn = finish_function ((ctor_initializer_p ? 1 : 0) | |
a723baf1 MM |
15092 | (inline_p ? 2 : 0)); |
15093 | /* Generate code for it, if necessary. */ | |
8cd2462c | 15094 | expand_or_defer_fn (fn); |
a723baf1 | 15095 | /* Restore the saved values. */ |
21526606 | 15096 | parser->in_unbraced_linkage_specification_p |
a723baf1 | 15097 | = saved_in_unbraced_linkage_specification_p; |
21526606 | 15098 | parser->num_template_parameter_lists |
a723baf1 MM |
15099 | = saved_num_template_parameter_lists; |
15100 | ||
15101 | return fn; | |
15102 | } | |
15103 | ||
15104 | /* Parse a template-declaration, assuming that the `export' (and | |
15105 | `extern') keywords, if present, has already been scanned. MEMBER_P | |
15106 | is as for cp_parser_template_declaration. */ | |
15107 | ||
15108 | static void | |
94edc4ab | 15109 | cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p) |
a723baf1 MM |
15110 | { |
15111 | tree decl = NULL_TREE; | |
15112 | tree parameter_list; | |
15113 | bool friend_p = false; | |
15114 | ||
15115 | /* Look for the `template' keyword. */ | |
15116 | if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'")) | |
15117 | return; | |
21526606 | 15118 | |
a723baf1 MM |
15119 | /* And the `<'. */ |
15120 | if (!cp_parser_require (parser, CPP_LESS, "`<'")) | |
15121 | return; | |
21526606 | 15122 | |
a723baf1 MM |
15123 | /* If the next token is `>', then we have an invalid |
15124 | specialization. Rather than complain about an invalid template | |
15125 | parameter, issue an error message here. */ | |
15126 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) | |
15127 | { | |
15128 | cp_parser_error (parser, "invalid explicit specialization"); | |
2f9afd51 | 15129 | begin_specialization (); |
a723baf1 MM |
15130 | parameter_list = NULL_TREE; |
15131 | } | |
15132 | else | |
2f9afd51 KL |
15133 | { |
15134 | /* Parse the template parameters. */ | |
15135 | begin_template_parm_list (); | |
15136 | parameter_list = cp_parser_template_parameter_list (parser); | |
15137 | parameter_list = end_template_parm_list (parameter_list); | |
15138 | } | |
15139 | ||
a723baf1 MM |
15140 | /* Look for the `>'. */ |
15141 | cp_parser_skip_until_found (parser, CPP_GREATER, "`>'"); | |
15142 | /* We just processed one more parameter list. */ | |
15143 | ++parser->num_template_parameter_lists; | |
15144 | /* If the next token is `template', there are more template | |
15145 | parameters. */ | |
21526606 | 15146 | if (cp_lexer_next_token_is_keyword (parser->lexer, |
a723baf1 MM |
15147 | RID_TEMPLATE)) |
15148 | cp_parser_template_declaration_after_export (parser, member_p); | |
15149 | else | |
15150 | { | |
fe88415f NS |
15151 | /* There are no access checks when parsing a template, as we do not |
15152 | know if a specialization will be a friend. */ | |
15153 | push_deferring_access_checks (dk_no_check); | |
98ca843c | 15154 | |
a723baf1 MM |
15155 | decl = cp_parser_single_declaration (parser, |
15156 | member_p, | |
15157 | &friend_p); | |
15158 | ||
fe88415f | 15159 | pop_deferring_access_checks (); |
98ca843c | 15160 | |
a723baf1 MM |
15161 | /* If this is a member template declaration, let the front |
15162 | end know. */ | |
15163 | if (member_p && !friend_p && decl) | |
37d407a1 KL |
15164 | { |
15165 | if (TREE_CODE (decl) == TYPE_DECL) | |
15166 | cp_parser_check_access_in_redeclaration (decl); | |
15167 | ||
15168 | decl = finish_member_template_decl (decl); | |
15169 | } | |
a723baf1 | 15170 | else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL) |
19db77ce KL |
15171 | make_friend_class (current_class_type, TREE_TYPE (decl), |
15172 | /*complain=*/true); | |
a723baf1 MM |
15173 | } |
15174 | /* We are done with the current parameter list. */ | |
15175 | --parser->num_template_parameter_lists; | |
15176 | ||
15177 | /* Finish up. */ | |
15178 | finish_template_decl (parameter_list); | |
15179 | ||
15180 | /* Register member declarations. */ | |
15181 | if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl)) | |
15182 | finish_member_declaration (decl); | |
15183 | ||
15184 | /* If DECL is a function template, we must return to parse it later. | |
15185 | (Even though there is no definition, there might be default | |
15186 | arguments that need handling.) */ | |
21526606 | 15187 | if (member_p && decl |
a723baf1 MM |
15188 | && (TREE_CODE (decl) == FUNCTION_DECL |
15189 | || DECL_FUNCTION_TEMPLATE_P (decl))) | |
15190 | TREE_VALUE (parser->unparsed_functions_queues) | |
21526606 | 15191 | = tree_cons (NULL_TREE, decl, |
a723baf1 MM |
15192 | TREE_VALUE (parser->unparsed_functions_queues)); |
15193 | } | |
15194 | ||
15195 | /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or | |
15196 | `function-definition' sequence. MEMBER_P is true, this declaration | |
15197 | appears in a class scope. | |
15198 | ||
15199 | Returns the DECL for the declared entity. If FRIEND_P is non-NULL, | |
15200 | *FRIEND_P is set to TRUE iff the declaration is a friend. */ | |
15201 | ||
15202 | static tree | |
21526606 | 15203 | cp_parser_single_declaration (cp_parser* parser, |
94edc4ab NN |
15204 | bool member_p, |
15205 | bool* friend_p) | |
a723baf1 | 15206 | { |
560ad596 | 15207 | int declares_class_or_enum; |
a723baf1 | 15208 | tree decl = NULL_TREE; |
62d1db17 | 15209 | cp_decl_specifier_seq decl_specifiers; |
4bb8ca28 | 15210 | bool function_definition_p = false; |
a723baf1 | 15211 | |
71bd7186 MM |
15212 | /* This function is only used when processing a template |
15213 | declaration. */ | |
15214 | gcc_assert (innermost_scope_kind () == sk_template_parms | |
15215 | || innermost_scope_kind () == sk_template_spec); | |
15216 | ||
a723baf1 | 15217 | /* Defer access checks until we know what is being declared. */ |
8d241e0b | 15218 | push_deferring_access_checks (dk_deferred); |
cf22909c | 15219 | |
a723baf1 MM |
15220 | /* Try the `decl-specifier-seq [opt] init-declarator [opt]' |
15221 | alternative. */ | |
62d1db17 MM |
15222 | cp_parser_decl_specifier_seq (parser, |
15223 | CP_PARSER_FLAGS_OPTIONAL, | |
15224 | &decl_specifiers, | |
15225 | &declares_class_or_enum); | |
4bb8ca28 | 15226 | if (friend_p) |
62d1db17 | 15227 | *friend_p = cp_parser_friend_p (&decl_specifiers); |
71bd7186 MM |
15228 | |
15229 | /* There are no template typedefs. */ | |
15230 | if (decl_specifiers.specs[(int) ds_typedef]) | |
15231 | { | |
15232 | error ("template declaration of %qs", "typedef"); | |
15233 | decl = error_mark_node; | |
15234 | } | |
15235 | ||
a723baf1 MM |
15236 | /* Gather up the access checks that occurred the |
15237 | decl-specifier-seq. */ | |
cf22909c KL |
15238 | stop_deferring_access_checks (); |
15239 | ||
a723baf1 MM |
15240 | /* Check for the declaration of a template class. */ |
15241 | if (declares_class_or_enum) | |
15242 | { | |
15243 | if (cp_parser_declares_only_class_p (parser)) | |
15244 | { | |
62d1db17 | 15245 | decl = shadow_tag (&decl_specifiers); |
b939a023 KL |
15246 | |
15247 | /* In this case: | |
15248 | ||
15249 | struct C { | |
15250 | friend template <typename T> struct A<T>::B; | |
15251 | }; | |
15252 | ||
15253 | A<T>::B will be represented by a TYPENAME_TYPE, and | |
15254 | therefore not recognized by shadow_tag. */ | |
15255 | if (friend_p && *friend_p | |
15256 | && !decl | |
15257 | && decl_specifiers.type | |
15258 | && TYPE_P (decl_specifiers.type)) | |
15259 | decl = decl_specifiers.type; | |
15260 | ||
62d1db17 | 15261 | if (decl && decl != error_mark_node) |
a723baf1 MM |
15262 | decl = TYPE_NAME (decl); |
15263 | else | |
15264 | decl = error_mark_node; | |
15265 | } | |
15266 | } | |
a723baf1 MM |
15267 | /* If it's not a template class, try for a template function. If |
15268 | the next token is a `;', then this declaration does not declare | |
15269 | anything. But, if there were errors in the decl-specifiers, then | |
15270 | the error might well have come from an attempted class-specifier. | |
15271 | In that case, there's no need to warn about a missing declarator. */ | |
15272 | if (!decl | |
15273 | && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON) | |
62d1db17 | 15274 | || decl_specifiers.type != error_mark_node)) |
21526606 | 15275 | decl = cp_parser_init_declarator (parser, |
62d1db17 | 15276 | &decl_specifiers, |
4bb8ca28 | 15277 | /*function_definition_allowed_p=*/true, |
a723baf1 | 15278 | member_p, |
560ad596 | 15279 | declares_class_or_enum, |
4bb8ca28 | 15280 | &function_definition_p); |
cf22909c KL |
15281 | |
15282 | pop_deferring_access_checks (); | |
15283 | ||
a723baf1 MM |
15284 | /* Clear any current qualification; whatever comes next is the start |
15285 | of something new. */ | |
15286 | parser->scope = NULL_TREE; | |
15287 | parser->qualifying_scope = NULL_TREE; | |
15288 | parser->object_scope = NULL_TREE; | |
15289 | /* Look for a trailing `;' after the declaration. */ | |
4bb8ca28 | 15290 | if (!function_definition_p |
71bd7186 MM |
15291 | && (decl == error_mark_node |
15292 | || !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))) | |
a723baf1 | 15293 | cp_parser_skip_to_end_of_block_or_statement (parser); |
a723baf1 MM |
15294 | |
15295 | return decl; | |
15296 | } | |
15297 | ||
d6b4ea85 MM |
15298 | /* Parse a cast-expression that is not the operand of a unary "&". */ |
15299 | ||
15300 | static tree | |
15301 | cp_parser_simple_cast_expression (cp_parser *parser) | |
15302 | { | |
93678513 MM |
15303 | return cp_parser_cast_expression (parser, /*address_p=*/false, |
15304 | /*cast_p=*/false); | |
d6b4ea85 MM |
15305 | } |
15306 | ||
a723baf1 MM |
15307 | /* Parse a functional cast to TYPE. Returns an expression |
15308 | representing the cast. */ | |
15309 | ||
15310 | static tree | |
94edc4ab | 15311 | cp_parser_functional_cast (cp_parser* parser, tree type) |
a723baf1 MM |
15312 | { |
15313 | tree expression_list; | |
d36d5600 | 15314 | tree cast; |
a723baf1 | 15315 | |
21526606 | 15316 | expression_list |
39703eb9 | 15317 | = cp_parser_parenthesized_expression_list (parser, false, |
93678513 | 15318 | /*cast_p=*/true, |
39703eb9 | 15319 | /*non_constant_p=*/NULL); |
a723baf1 | 15320 | |
d36d5600 GB |
15321 | cast = build_functional_cast (type, expression_list); |
15322 | /* [expr.const]/1: In an integral constant expression "only type | |
15323 | conversions to integral or enumeration type can be used". */ | |
98ca843c | 15324 | if (cast != error_mark_node && !type_dependent_expression_p (type) |
d36d5600 GB |
15325 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type))) |
15326 | { | |
98ca843c | 15327 | if (cp_parser_non_integral_constant_expression |
d36d5600 GB |
15328 | (parser, "a call to a constructor")) |
15329 | return error_mark_node; | |
15330 | } | |
15331 | return cast; | |
a723baf1 MM |
15332 | } |
15333 | ||
4bb8ca28 MM |
15334 | /* Save the tokens that make up the body of a member function defined |
15335 | in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have | |
15336 | already been parsed. The ATTRIBUTES are any GNU "__attribute__" | |
15337 | specifiers applied to the declaration. Returns the FUNCTION_DECL | |
15338 | for the member function. */ | |
15339 | ||
7ce27103 | 15340 | static tree |
4bb8ca28 | 15341 | cp_parser_save_member_function_body (cp_parser* parser, |
62d1db17 | 15342 | cp_decl_specifier_seq *decl_specifiers, |
058b15c1 | 15343 | cp_declarator *declarator, |
4bb8ca28 MM |
15344 | tree attributes) |
15345 | { | |
c162c75e MA |
15346 | cp_token *first; |
15347 | cp_token *last; | |
4bb8ca28 MM |
15348 | tree fn; |
15349 | ||
15350 | /* Create the function-declaration. */ | |
15351 | fn = start_method (decl_specifiers, declarator, attributes); | |
15352 | /* If something went badly wrong, bail out now. */ | |
15353 | if (fn == error_mark_node) | |
15354 | { | |
15355 | /* If there's a function-body, skip it. */ | |
21526606 | 15356 | if (cp_parser_token_starts_function_definition_p |
4bb8ca28 MM |
15357 | (cp_lexer_peek_token (parser->lexer))) |
15358 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
15359 | return error_mark_node; | |
15360 | } | |
15361 | ||
15362 | /* Remember it, if there default args to post process. */ | |
15363 | cp_parser_save_default_args (parser, fn); | |
15364 | ||
21526606 | 15365 | /* Save away the tokens that make up the body of the |
4bb8ca28 | 15366 | function. */ |
c162c75e MA |
15367 | first = parser->lexer->next_token; |
15368 | cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0); | |
4bb8ca28 MM |
15369 | /* Handle function try blocks. */ |
15370 | while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH)) | |
c162c75e MA |
15371 | cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0); |
15372 | last = parser->lexer->next_token; | |
4bb8ca28 MM |
15373 | |
15374 | /* Save away the inline definition; we will process it when the | |
15375 | class is complete. */ | |
c162c75e | 15376 | DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last); |
4bb8ca28 MM |
15377 | DECL_PENDING_INLINE_P (fn) = 1; |
15378 | ||
15379 | /* We need to know that this was defined in the class, so that | |
15380 | friend templates are handled correctly. */ | |
15381 | DECL_INITIALIZED_IN_CLASS_P (fn) = 1; | |
15382 | ||
15383 | /* We're done with the inline definition. */ | |
15384 | finish_method (fn); | |
15385 | ||
15386 | /* Add FN to the queue of functions to be parsed later. */ | |
15387 | TREE_VALUE (parser->unparsed_functions_queues) | |
21526606 | 15388 | = tree_cons (NULL_TREE, fn, |
4bb8ca28 MM |
15389 | TREE_VALUE (parser->unparsed_functions_queues)); |
15390 | ||
15391 | return fn; | |
15392 | } | |
15393 | ||
ec75414f MM |
15394 | /* Parse a template-argument-list, as well as the trailing ">" (but |
15395 | not the opening ">"). See cp_parser_template_argument_list for the | |
15396 | return value. */ | |
15397 | ||
15398 | static tree | |
15399 | cp_parser_enclosed_template_argument_list (cp_parser* parser) | |
15400 | { | |
15401 | tree arguments; | |
15402 | tree saved_scope; | |
15403 | tree saved_qualifying_scope; | |
15404 | tree saved_object_scope; | |
15405 | bool saved_greater_than_is_operator_p; | |
15406 | ||
15407 | /* [temp.names] | |
15408 | ||
15409 | When parsing a template-id, the first non-nested `>' is taken as | |
15410 | the end of the template-argument-list rather than a greater-than | |
15411 | operator. */ | |
21526606 | 15412 | saved_greater_than_is_operator_p |
ec75414f MM |
15413 | = parser->greater_than_is_operator_p; |
15414 | parser->greater_than_is_operator_p = false; | |
15415 | /* Parsing the argument list may modify SCOPE, so we save it | |
15416 | here. */ | |
15417 | saved_scope = parser->scope; | |
15418 | saved_qualifying_scope = parser->qualifying_scope; | |
15419 | saved_object_scope = parser->object_scope; | |
15420 | /* Parse the template-argument-list itself. */ | |
15421 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) | |
15422 | arguments = NULL_TREE; | |
15423 | else | |
15424 | arguments = cp_parser_template_argument_list (parser); | |
4d5297fa GB |
15425 | /* Look for the `>' that ends the template-argument-list. If we find |
15426 | a '>>' instead, it's probably just a typo. */ | |
15427 | if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) | |
15428 | { | |
15429 | if (!saved_greater_than_is_operator_p) | |
15430 | { | |
2cfe82fe ZW |
15431 | /* If we're in a nested template argument list, the '>>' has |
15432 | to be a typo for '> >'. We emit the error message, but we | |
15433 | continue parsing and we push a '>' as next token, so that | |
15434 | the argument list will be parsed correctly. Note that the | |
15435 | global source location is still on the token before the | |
15436 | '>>', so we need to say explicitly where we want it. */ | |
15437 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
15438 | error ("%H%<>>%> should be %<> >%> " | |
15439 | "within a nested template argument list", | |
15440 | &token->location); | |
15441 | ||
15442 | /* ??? Proper recovery should terminate two levels of | |
15443 | template argument list here. */ | |
4d5297fa GB |
15444 | token->type = CPP_GREATER; |
15445 | } | |
15446 | else | |
15447 | { | |
2cfe82fe ZW |
15448 | /* If this is not a nested template argument list, the '>>' |
15449 | is a typo for '>'. Emit an error message and continue. | |
15450 | Same deal about the token location, but here we can get it | |
15451 | right by consuming the '>>' before issuing the diagnostic. */ | |
4d5297fa | 15452 | cp_lexer_consume_token (parser->lexer); |
2cfe82fe ZW |
15453 | error ("spurious %<>>%>, use %<>%> to terminate " |
15454 | "a template argument list"); | |
4d5297fa GB |
15455 | } |
15456 | } | |
2cfe82fe ZW |
15457 | else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) |
15458 | error ("missing %<>%> to terminate the template argument list"); | |
15459 | else | |
15460 | /* It's what we want, a '>'; consume it. */ | |
15461 | cp_lexer_consume_token (parser->lexer); | |
ec75414f | 15462 | /* The `>' token might be a greater-than operator again now. */ |
21526606 | 15463 | parser->greater_than_is_operator_p |
ec75414f MM |
15464 | = saved_greater_than_is_operator_p; |
15465 | /* Restore the SAVED_SCOPE. */ | |
15466 | parser->scope = saved_scope; | |
15467 | parser->qualifying_scope = saved_qualifying_scope; | |
15468 | parser->object_scope = saved_object_scope; | |
15469 | ||
15470 | return arguments; | |
15471 | } | |
15472 | ||
a723baf1 MM |
15473 | /* MEMBER_FUNCTION is a member function, or a friend. If default |
15474 | arguments, or the body of the function have not yet been parsed, | |
15475 | parse them now. */ | |
15476 | ||
15477 | static void | |
94edc4ab | 15478 | cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function) |
a723baf1 | 15479 | { |
a723baf1 MM |
15480 | /* If this member is a template, get the underlying |
15481 | FUNCTION_DECL. */ | |
15482 | if (DECL_FUNCTION_TEMPLATE_P (member_function)) | |
15483 | member_function = DECL_TEMPLATE_RESULT (member_function); | |
15484 | ||
15485 | /* There should not be any class definitions in progress at this | |
15486 | point; the bodies of members are only parsed outside of all class | |
15487 | definitions. */ | |
50bc768d | 15488 | gcc_assert (parser->num_classes_being_defined == 0); |
a723baf1 MM |
15489 | /* While we're parsing the member functions we might encounter more |
15490 | classes. We want to handle them right away, but we don't want | |
15491 | them getting mixed up with functions that are currently in the | |
15492 | queue. */ | |
15493 | parser->unparsed_functions_queues | |
15494 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
15495 | ||
15496 | /* Make sure that any template parameters are in scope. */ | |
15497 | maybe_begin_member_template_processing (member_function); | |
15498 | ||
a723baf1 MM |
15499 | /* If the body of the function has not yet been parsed, parse it |
15500 | now. */ | |
15501 | if (DECL_PENDING_INLINE_P (member_function)) | |
15502 | { | |
15503 | tree function_scope; | |
15504 | cp_token_cache *tokens; | |
15505 | ||
15506 | /* The function is no longer pending; we are processing it. */ | |
15507 | tokens = DECL_PENDING_INLINE_INFO (member_function); | |
15508 | DECL_PENDING_INLINE_INFO (member_function) = NULL; | |
15509 | DECL_PENDING_INLINE_P (member_function) = 0; | |
f769035f NS |
15510 | |
15511 | /* If this is a local class, enter the scope of the containing | |
15512 | function. */ | |
15513 | function_scope = current_function_decl; | |
a723baf1 MM |
15514 | if (function_scope) |
15515 | push_function_context_to (function_scope); | |
21526606 | 15516 | |
2d637547 | 15517 | |
2cfe82fe ZW |
15518 | /* Push the body of the function onto the lexer stack. */ |
15519 | cp_parser_push_lexer_for_tokens (parser, tokens); | |
21526606 | 15520 | |
a723baf1 MM |
15521 | /* Let the front end know that we going to be defining this |
15522 | function. */ | |
058b15c1 MM |
15523 | start_preparsed_function (member_function, NULL_TREE, |
15524 | SF_PRE_PARSED | SF_INCLASS_INLINE); | |
21526606 | 15525 | |
2d637547 NS |
15526 | /* Don't do access checking if it is a templated function. */ |
15527 | if (processing_template_decl) | |
15528 | push_deferring_access_checks (dk_no_check); | |
15529 | ||
a723baf1 MM |
15530 | /* Now, parse the body of the function. */ |
15531 | cp_parser_function_definition_after_declarator (parser, | |
15532 | /*inline_p=*/true); | |
21526606 | 15533 | |
2d637547 NS |
15534 | if (processing_template_decl) |
15535 | pop_deferring_access_checks (); | |
15536 | ||
a723baf1 MM |
15537 | /* Leave the scope of the containing function. */ |
15538 | if (function_scope) | |
15539 | pop_function_context_from (function_scope); | |
2cfe82fe | 15540 | cp_parser_pop_lexer (parser); |
a723baf1 MM |
15541 | } |
15542 | ||
15543 | /* Remove any template parameters from the symbol table. */ | |
15544 | maybe_end_member_template_processing (); | |
15545 | ||
15546 | /* Restore the queue. */ | |
21526606 | 15547 | parser->unparsed_functions_queues |
a723baf1 MM |
15548 | = TREE_CHAIN (parser->unparsed_functions_queues); |
15549 | } | |
15550 | ||
cd0be382 | 15551 | /* If DECL contains any default args, remember it on the unparsed |
8db1028e NS |
15552 | functions queue. */ |
15553 | ||
15554 | static void | |
15555 | cp_parser_save_default_args (cp_parser* parser, tree decl) | |
15556 | { | |
15557 | tree probe; | |
15558 | ||
15559 | for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
15560 | probe; | |
15561 | probe = TREE_CHAIN (probe)) | |
15562 | if (TREE_PURPOSE (probe)) | |
15563 | { | |
15564 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
f44b0c8e | 15565 | = tree_cons (current_class_type, decl, |
8db1028e NS |
15566 | TREE_PURPOSE (parser->unparsed_functions_queues)); |
15567 | break; | |
15568 | } | |
15569 | return; | |
15570 | } | |
15571 | ||
8218bd34 | 15572 | /* FN is a FUNCTION_DECL which may contains a parameter with an |
f44b0c8e MM |
15573 | unparsed DEFAULT_ARG. Parse the default args now. This function |
15574 | assumes that the current scope is the scope in which the default | |
15575 | argument should be processed. */ | |
a723baf1 MM |
15576 | |
15577 | static void | |
8218bd34 | 15578 | cp_parser_late_parsing_default_args (cp_parser *parser, tree fn) |
a723baf1 | 15579 | { |
a723baf1 | 15580 | bool saved_local_variables_forbidden_p; |
2cfe82fe | 15581 | tree parm; |
8218bd34 | 15582 | |
b92bc2a0 NS |
15583 | /* While we're parsing the default args, we might (due to the |
15584 | statement expression extension) encounter more classes. We want | |
15585 | to handle them right away, but we don't want them getting mixed | |
15586 | up with default args that are currently in the queue. */ | |
15587 | parser->unparsed_functions_queues | |
15588 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
15589 | ||
2cfe82fe ZW |
15590 | /* Local variable names (and the `this' keyword) may not appear |
15591 | in a default argument. */ | |
15592 | saved_local_variables_forbidden_p = parser->local_variables_forbidden_p; | |
15593 | parser->local_variables_forbidden_p = true; | |
15594 | ||
15595 | for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
15596 | parm; | |
15597 | parm = TREE_CHAIN (parm)) | |
a723baf1 | 15598 | { |
2cfe82fe | 15599 | cp_token_cache *tokens; |
5e97d404 NS |
15600 | tree default_arg = TREE_PURPOSE (parm); |
15601 | tree parsed_arg; | |
01ea1ea8 NS |
15602 | VEC(tree,gc) *insts; |
15603 | tree copy; | |
15604 | unsigned ix; | |
5e97d404 NS |
15605 | |
15606 | if (!default_arg) | |
2cfe82fe | 15607 | continue; |
a723baf1 | 15608 | |
efb169b0 NS |
15609 | if (TREE_CODE (default_arg) != DEFAULT_ARG) |
15610 | /* This can happen for a friend declaration for a function | |
15611 | already declared with default arguments. */ | |
15612 | continue; | |
5e97d404 | 15613 | |
2cfe82fe ZW |
15614 | /* Push the saved tokens for the default argument onto the parser's |
15615 | lexer stack. */ | |
5e97d404 | 15616 | tokens = DEFARG_TOKENS (default_arg); |
2cfe82fe | 15617 | cp_parser_push_lexer_for_tokens (parser, tokens); |
a723baf1 | 15618 | |
2cfe82fe | 15619 | /* Parse the assignment-expression. */ |
5e97d404 NS |
15620 | parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false); |
15621 | ||
15622 | TREE_PURPOSE (parm) = parsed_arg; | |
15623 | ||
15624 | /* Update any instantiations we've already created. */ | |
01ea1ea8 NS |
15625 | for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0; |
15626 | VEC_iterate (tree, insts, ix, copy); ix++) | |
15627 | TREE_PURPOSE (copy) = parsed_arg; | |
a723baf1 | 15628 | |
676e33ca MM |
15629 | /* If the token stream has not been completely used up, then |
15630 | there was extra junk after the end of the default | |
15631 | argument. */ | |
15632 | if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
2a13a625 | 15633 | cp_parser_error (parser, "expected %<,%>"); |
676e33ca | 15634 | |
2cfe82fe ZW |
15635 | /* Revert to the main lexer. */ |
15636 | cp_parser_pop_lexer (parser); | |
a723baf1 | 15637 | } |
b92bc2a0 | 15638 | |
2cfe82fe ZW |
15639 | /* Restore the state of local_variables_forbidden_p. */ |
15640 | parser->local_variables_forbidden_p = saved_local_variables_forbidden_p; | |
15641 | ||
b92bc2a0 | 15642 | /* Restore the queue. */ |
21526606 | 15643 | parser->unparsed_functions_queues |
b92bc2a0 | 15644 | = TREE_CHAIN (parser->unparsed_functions_queues); |
a723baf1 MM |
15645 | } |
15646 | ||
15647 | /* Parse the operand of `sizeof' (or a similar operator). Returns | |
15648 | either a TYPE or an expression, depending on the form of the | |
15649 | input. The KEYWORD indicates which kind of expression we have | |
15650 | encountered. */ | |
15651 | ||
15652 | static tree | |
94edc4ab | 15653 | cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword) |
a723baf1 MM |
15654 | { |
15655 | static const char *format; | |
15656 | tree expr = NULL_TREE; | |
15657 | const char *saved_message; | |
67c03833 | 15658 | bool saved_integral_constant_expression_p; |
93678513 | 15659 | bool saved_non_integral_constant_expression_p; |
a723baf1 MM |
15660 | |
15661 | /* Initialize FORMAT the first time we get here. */ | |
15662 | if (!format) | |
9e637a26 | 15663 | format = "types may not be defined in '%s' expressions"; |
a723baf1 MM |
15664 | |
15665 | /* Types cannot be defined in a `sizeof' expression. Save away the | |
15666 | old message. */ | |
15667 | saved_message = parser->type_definition_forbidden_message; | |
15668 | /* And create the new one. */ | |
21526606 EC |
15669 | parser->type_definition_forbidden_message |
15670 | = xmalloc (strlen (format) | |
c68b0a84 KG |
15671 | + strlen (IDENTIFIER_POINTER (ridpointers[keyword])) |
15672 | + 1 /* `\0' */); | |
a723baf1 MM |
15673 | sprintf ((char *) parser->type_definition_forbidden_message, |
15674 | format, IDENTIFIER_POINTER (ridpointers[keyword])); | |
15675 | ||
15676 | /* The restrictions on constant-expressions do not apply inside | |
15677 | sizeof expressions. */ | |
93678513 MM |
15678 | saved_integral_constant_expression_p |
15679 | = parser->integral_constant_expression_p; | |
15680 | saved_non_integral_constant_expression_p | |
15681 | = parser->non_integral_constant_expression_p; | |
67c03833 | 15682 | parser->integral_constant_expression_p = false; |
a723baf1 | 15683 | |
3beb3abf MM |
15684 | /* Do not actually evaluate the expression. */ |
15685 | ++skip_evaluation; | |
a723baf1 MM |
15686 | /* If it's a `(', then we might be looking at the type-id |
15687 | construction. */ | |
15688 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
15689 | { | |
15690 | tree type; | |
4f8163b1 | 15691 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
15692 | |
15693 | /* We can't be sure yet whether we're looking at a type-id or an | |
15694 | expression. */ | |
15695 | cp_parser_parse_tentatively (parser); | |
15696 | /* Consume the `('. */ | |
15697 | cp_lexer_consume_token (parser->lexer); | |
15698 | /* Parse the type-id. */ | |
4f8163b1 MM |
15699 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
15700 | parser->in_type_id_in_expr_p = true; | |
a723baf1 | 15701 | type = cp_parser_type_id (parser); |
4f8163b1 | 15702 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 | 15703 | /* Now, look for the trailing `)'. */ |
9e637a26 | 15704 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
a723baf1 MM |
15705 | /* If all went well, then we're done. */ |
15706 | if (cp_parser_parse_definitely (parser)) | |
15707 | { | |
62d1db17 MM |
15708 | cp_decl_specifier_seq decl_specs; |
15709 | ||
15710 | /* Build a trivial decl-specifier-seq. */ | |
15711 | clear_decl_specs (&decl_specs); | |
15712 | decl_specs.type = type; | |
a723baf1 MM |
15713 | |
15714 | /* Call grokdeclarator to figure out what type this is. */ | |
058b15c1 | 15715 | expr = grokdeclarator (NULL, |
62d1db17 | 15716 | &decl_specs, |
a723baf1 MM |
15717 | TYPENAME, |
15718 | /*initialized=*/0, | |
15719 | /*attrlist=*/NULL); | |
15720 | } | |
15721 | } | |
15722 | ||
15723 | /* If the type-id production did not work out, then we must be | |
15724 | looking at the unary-expression production. */ | |
15725 | if (!expr) | |
93678513 MM |
15726 | expr = cp_parser_unary_expression (parser, /*address_p=*/false, |
15727 | /*cast_p=*/false); | |
3beb3abf MM |
15728 | /* Go back to evaluating expressions. */ |
15729 | --skip_evaluation; | |
a723baf1 MM |
15730 | |
15731 | /* Free the message we created. */ | |
15732 | free ((char *) parser->type_definition_forbidden_message); | |
15733 | /* And restore the old one. */ | |
15734 | parser->type_definition_forbidden_message = saved_message; | |
93678513 MM |
15735 | parser->integral_constant_expression_p |
15736 | = saved_integral_constant_expression_p; | |
15737 | parser->non_integral_constant_expression_p | |
15738 | = saved_non_integral_constant_expression_p; | |
a723baf1 MM |
15739 | |
15740 | return expr; | |
15741 | } | |
15742 | ||
15743 | /* If the current declaration has no declarator, return true. */ | |
15744 | ||
15745 | static bool | |
15746 | cp_parser_declares_only_class_p (cp_parser *parser) | |
15747 | { | |
21526606 | 15748 | /* If the next token is a `;' or a `,' then there is no |
a723baf1 MM |
15749 | declarator. */ |
15750 | return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) | |
15751 | || cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); | |
15752 | } | |
15753 | ||
62d1db17 | 15754 | /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */ |
a723baf1 | 15755 | |
62d1db17 MM |
15756 | static void |
15757 | cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs, | |
15758 | cp_storage_class storage_class) | |
a723baf1 | 15759 | { |
62d1db17 MM |
15760 | if (decl_specs->storage_class != sc_none) |
15761 | decl_specs->multiple_storage_classes_p = true; | |
15762 | else | |
15763 | decl_specs->storage_class = storage_class; | |
15764 | } | |
15765 | ||
15766 | /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P | |
15767 | is true, the type is a user-defined type; otherwise it is a | |
15768 | built-in type specified by a keyword. */ | |
a723baf1 | 15769 | |
62d1db17 MM |
15770 | static void |
15771 | cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs, | |
15772 | tree type_spec, | |
15773 | bool user_defined_p) | |
15774 | { | |
15775 | decl_specs->any_specifiers_p = true; | |
98ca843c | 15776 | |
9306cccb MM |
15777 | /* If the user tries to redeclare bool or wchar_t (with, for |
15778 | example, in "typedef int wchar_t;") we remember that this is what | |
f84b6c96 MM |
15779 | happened. In system headers, we ignore these declarations so |
15780 | that G++ can work with system headers that are not C++-safe. */ | |
98ca843c | 15781 | if (decl_specs->specs[(int) ds_typedef] |
f84b6c96 | 15782 | && !user_defined_p |
9306cccb MM |
15783 | && (type_spec == boolean_type_node |
15784 | || type_spec == wchar_type_node) | |
f84b6c96 MM |
15785 | && (decl_specs->type |
15786 | || decl_specs->specs[(int) ds_long] | |
15787 | || decl_specs->specs[(int) ds_short] | |
15788 | || decl_specs->specs[(int) ds_unsigned] | |
15789 | || decl_specs->specs[(int) ds_signed])) | |
0a73e37f MM |
15790 | { |
15791 | decl_specs->redefined_builtin_type = type_spec; | |
15792 | if (!decl_specs->type) | |
15793 | { | |
15794 | decl_specs->type = type_spec; | |
15795 | decl_specs->user_defined_type_p = false; | |
15796 | } | |
15797 | } | |
f84b6c96 MM |
15798 | else if (decl_specs->type) |
15799 | decl_specs->multiple_types_p = true; | |
62d1db17 MM |
15800 | else |
15801 | { | |
15802 | decl_specs->type = type_spec; | |
15803 | decl_specs->user_defined_type_p = user_defined_p; | |
0a73e37f | 15804 | decl_specs->redefined_builtin_type = NULL_TREE; |
a723baf1 | 15805 | } |
62d1db17 | 15806 | } |
a723baf1 | 15807 | |
62d1db17 MM |
15808 | /* DECL_SPECIFIERS is the representation of a decl-specifier-seq. |
15809 | Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */ | |
15810 | ||
15811 | static bool | |
15812 | cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers) | |
15813 | { | |
15814 | return decl_specifiers->specs[(int) ds_friend] != 0; | |
a723baf1 MM |
15815 | } |
15816 | ||
15817 | /* If the next token is of the indicated TYPE, consume it. Otherwise, | |
15818 | issue an error message indicating that TOKEN_DESC was expected. | |
21526606 | 15819 | |
a723baf1 MM |
15820 | Returns the token consumed, if the token had the appropriate type. |
15821 | Otherwise, returns NULL. */ | |
15822 | ||
15823 | static cp_token * | |
94edc4ab NN |
15824 | cp_parser_require (cp_parser* parser, |
15825 | enum cpp_ttype type, | |
15826 | const char* token_desc) | |
a723baf1 MM |
15827 | { |
15828 | if (cp_lexer_next_token_is (parser->lexer, type)) | |
15829 | return cp_lexer_consume_token (parser->lexer); | |
15830 | else | |
15831 | { | |
e5976695 MM |
15832 | /* Output the MESSAGE -- unless we're parsing tentatively. */ |
15833 | if (!cp_parser_simulate_error (parser)) | |
216bb6e1 MM |
15834 | { |
15835 | char *message = concat ("expected ", token_desc, NULL); | |
15836 | cp_parser_error (parser, message); | |
15837 | free (message); | |
15838 | } | |
a723baf1 MM |
15839 | return NULL; |
15840 | } | |
15841 | } | |
15842 | ||
15843 | /* Like cp_parser_require, except that tokens will be skipped until | |
15844 | the desired token is found. An error message is still produced if | |
15845 | the next token is not as expected. */ | |
15846 | ||
15847 | static void | |
21526606 EC |
15848 | cp_parser_skip_until_found (cp_parser* parser, |
15849 | enum cpp_ttype type, | |
94edc4ab | 15850 | const char* token_desc) |
a723baf1 MM |
15851 | { |
15852 | cp_token *token; | |
15853 | unsigned nesting_depth = 0; | |
15854 | ||
15855 | if (cp_parser_require (parser, type, token_desc)) | |
15856 | return; | |
15857 | ||
15858 | /* Skip tokens until the desired token is found. */ | |
15859 | while (true) | |
15860 | { | |
15861 | /* Peek at the next token. */ | |
15862 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 15863 | /* If we've reached the token we want, consume it and |
a723baf1 MM |
15864 | stop. */ |
15865 | if (token->type == type && !nesting_depth) | |
15866 | { | |
15867 | cp_lexer_consume_token (parser->lexer); | |
15868 | return; | |
15869 | } | |
15870 | /* If we've run out of tokens, stop. */ | |
15871 | if (token->type == CPP_EOF) | |
15872 | return; | |
21526606 | 15873 | if (token->type == CPP_OPEN_BRACE |
a723baf1 MM |
15874 | || token->type == CPP_OPEN_PAREN |
15875 | || token->type == CPP_OPEN_SQUARE) | |
15876 | ++nesting_depth; | |
21526606 | 15877 | else if (token->type == CPP_CLOSE_BRACE |
a723baf1 MM |
15878 | || token->type == CPP_CLOSE_PAREN |
15879 | || token->type == CPP_CLOSE_SQUARE) | |
15880 | { | |
15881 | if (nesting_depth-- == 0) | |
15882 | return; | |
15883 | } | |
15884 | /* Consume this token. */ | |
15885 | cp_lexer_consume_token (parser->lexer); | |
15886 | } | |
15887 | } | |
15888 | ||
15889 | /* If the next token is the indicated keyword, consume it. Otherwise, | |
15890 | issue an error message indicating that TOKEN_DESC was expected. | |
21526606 | 15891 | |
a723baf1 MM |
15892 | Returns the token consumed, if the token had the appropriate type. |
15893 | Otherwise, returns NULL. */ | |
15894 | ||
15895 | static cp_token * | |
94edc4ab NN |
15896 | cp_parser_require_keyword (cp_parser* parser, |
15897 | enum rid keyword, | |
15898 | const char* token_desc) | |
a723baf1 MM |
15899 | { |
15900 | cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc); | |
15901 | ||
15902 | if (token && token->keyword != keyword) | |
15903 | { | |
15904 | dyn_string_t error_msg; | |
15905 | ||
15906 | /* Format the error message. */ | |
15907 | error_msg = dyn_string_new (0); | |
15908 | dyn_string_append_cstr (error_msg, "expected "); | |
15909 | dyn_string_append_cstr (error_msg, token_desc); | |
15910 | cp_parser_error (parser, error_msg->s); | |
15911 | dyn_string_delete (error_msg); | |
15912 | return NULL; | |
15913 | } | |
15914 | ||
15915 | return token; | |
15916 | } | |
15917 | ||
15918 | /* Returns TRUE iff TOKEN is a token that can begin the body of a | |
15919 | function-definition. */ | |
15920 | ||
21526606 | 15921 | static bool |
94edc4ab | 15922 | cp_parser_token_starts_function_definition_p (cp_token* token) |
a723baf1 MM |
15923 | { |
15924 | return (/* An ordinary function-body begins with an `{'. */ | |
15925 | token->type == CPP_OPEN_BRACE | |
15926 | /* A ctor-initializer begins with a `:'. */ | |
15927 | || token->type == CPP_COLON | |
15928 | /* A function-try-block begins with `try'. */ | |
15929 | || token->keyword == RID_TRY | |
15930 | /* The named return value extension begins with `return'. */ | |
15931 | || token->keyword == RID_RETURN); | |
15932 | } | |
15933 | ||
15934 | /* Returns TRUE iff the next token is the ":" or "{" beginning a class | |
15935 | definition. */ | |
15936 | ||
15937 | static bool | |
15938 | cp_parser_next_token_starts_class_definition_p (cp_parser *parser) | |
15939 | { | |
15940 | cp_token *token; | |
15941 | ||
15942 | token = cp_lexer_peek_token (parser->lexer); | |
15943 | return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON); | |
15944 | } | |
15945 | ||
d17811fd | 15946 | /* Returns TRUE iff the next token is the "," or ">" ending a |
03fd3f84 | 15947 | template-argument. */ |
d17811fd MM |
15948 | |
15949 | static bool | |
15950 | cp_parser_next_token_ends_template_argument_p (cp_parser *parser) | |
15951 | { | |
15952 | cp_token *token; | |
15953 | ||
15954 | token = cp_lexer_peek_token (parser->lexer); | |
391c4bc5 | 15955 | return (token->type == CPP_COMMA || token->type == CPP_GREATER); |
d17811fd | 15956 | } |
f4abade9 GB |
15957 | |
15958 | /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the | |
15959 | (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */ | |
15960 | ||
15961 | static bool | |
21526606 | 15962 | cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser, |
f4abade9 GB |
15963 | size_t n) |
15964 | { | |
15965 | cp_token *token; | |
15966 | ||
15967 | token = cp_lexer_peek_nth_token (parser->lexer, n); | |
15968 | if (token->type == CPP_LESS) | |
15969 | return true; | |
15970 | /* Check for the sequence `<::' in the original code. It would be lexed as | |
15971 | `[:', where `[' is a digraph, and there is no whitespace before | |
15972 | `:'. */ | |
15973 | if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH) | |
15974 | { | |
15975 | cp_token *token2; | |
15976 | token2 = cp_lexer_peek_nth_token (parser->lexer, n+1); | |
15977 | if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE)) | |
15978 | return true; | |
15979 | } | |
15980 | return false; | |
15981 | } | |
21526606 | 15982 | |
a723baf1 MM |
15983 | /* Returns the kind of tag indicated by TOKEN, if it is a class-key, |
15984 | or none_type otherwise. */ | |
15985 | ||
15986 | static enum tag_types | |
94edc4ab | 15987 | cp_parser_token_is_class_key (cp_token* token) |
a723baf1 MM |
15988 | { |
15989 | switch (token->keyword) | |
15990 | { | |
15991 | case RID_CLASS: | |
15992 | return class_type; | |
15993 | case RID_STRUCT: | |
15994 | return record_type; | |
15995 | case RID_UNION: | |
15996 | return union_type; | |
21526606 | 15997 | |
a723baf1 MM |
15998 | default: |
15999 | return none_type; | |
16000 | } | |
16001 | } | |
16002 | ||
16003 | /* Issue an error message if the CLASS_KEY does not match the TYPE. */ | |
16004 | ||
16005 | static void | |
16006 | cp_parser_check_class_key (enum tag_types class_key, tree type) | |
16007 | { | |
16008 | if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type)) | |
2a13a625 | 16009 | pedwarn ("%qs tag used in naming %q#T", |
a723baf1 | 16010 | class_key == union_type ? "union" |
21526606 | 16011 | : class_key == record_type ? "struct" : "class", |
a723baf1 MM |
16012 | type); |
16013 | } | |
21526606 | 16014 | |
cd0be382 | 16015 | /* Issue an error message if DECL is redeclared with different |
37d407a1 KL |
16016 | access than its original declaration [class.access.spec/3]. |
16017 | This applies to nested classes and nested class templates. | |
16018 | [class.mem/1]. */ | |
16019 | ||
2a13a625 GDR |
16020 | static void |
16021 | cp_parser_check_access_in_redeclaration (tree decl) | |
37d407a1 KL |
16022 | { |
16023 | if (!CLASS_TYPE_P (TREE_TYPE (decl))) | |
16024 | return; | |
16025 | ||
16026 | if ((TREE_PRIVATE (decl) | |
16027 | != (current_access_specifier == access_private_node)) | |
16028 | || (TREE_PROTECTED (decl) | |
16029 | != (current_access_specifier == access_protected_node))) | |
2a13a625 | 16030 | error ("%qD redeclared with different access", decl); |
37d407a1 KL |
16031 | } |
16032 | ||
a723baf1 | 16033 | /* Look for the `template' keyword, as a syntactic disambiguator. |
21526606 | 16034 | Return TRUE iff it is present, in which case it will be |
a723baf1 MM |
16035 | consumed. */ |
16036 | ||
16037 | static bool | |
16038 | cp_parser_optional_template_keyword (cp_parser *parser) | |
16039 | { | |
16040 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
16041 | { | |
16042 | /* The `template' keyword can only be used within templates; | |
16043 | outside templates the parser can always figure out what is a | |
16044 | template and what is not. */ | |
16045 | if (!processing_template_decl) | |
16046 | { | |
2a13a625 | 16047 | error ("%<template%> (as a disambiguator) is only allowed " |
a723baf1 MM |
16048 | "within templates"); |
16049 | /* If this part of the token stream is rescanned, the same | |
16050 | error message would be generated. So, we purge the token | |
16051 | from the stream. */ | |
16052 | cp_lexer_purge_token (parser->lexer); | |
16053 | return false; | |
16054 | } | |
16055 | else | |
16056 | { | |
16057 | /* Consume the `template' keyword. */ | |
16058 | cp_lexer_consume_token (parser->lexer); | |
16059 | return true; | |
16060 | } | |
16061 | } | |
16062 | ||
16063 | return false; | |
16064 | } | |
16065 | ||
2050a1bb MM |
16066 | /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token, |
16067 | set PARSER->SCOPE, and perform other related actions. */ | |
16068 | ||
16069 | static void | |
16070 | cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser) | |
16071 | { | |
16072 | tree value; | |
16073 | tree check; | |
16074 | ||
16075 | /* Get the stored value. */ | |
16076 | value = cp_lexer_consume_token (parser->lexer)->value; | |
16077 | /* Perform any access checks that were deferred. */ | |
16078 | for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check)) | |
cf22909c | 16079 | perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check)); |
2050a1bb MM |
16080 | /* Set the scope from the stored value. */ |
16081 | parser->scope = TREE_VALUE (value); | |
16082 | parser->qualifying_scope = TREE_TYPE (value); | |
16083 | parser->object_scope = NULL_TREE; | |
16084 | } | |
16085 | ||
03fd3f84 | 16086 | /* Consume tokens up through a non-nested END token. */ |
a723baf1 MM |
16087 | |
16088 | static void | |
c162c75e MA |
16089 | cp_parser_cache_group (cp_parser *parser, |
16090 | enum cpp_ttype end, | |
16091 | unsigned depth) | |
a723baf1 MM |
16092 | { |
16093 | while (true) | |
16094 | { | |
16095 | cp_token *token; | |
16096 | ||
16097 | /* Abort a parenthesized expression if we encounter a brace. */ | |
16098 | if ((end == CPP_CLOSE_PAREN || depth == 0) | |
16099 | && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
16100 | return; | |
a723baf1 | 16101 | /* If we've reached the end of the file, stop. */ |
4bfb8bba | 16102 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) |
a723baf1 | 16103 | return; |
4bfb8bba MM |
16104 | /* Consume the next token. */ |
16105 | token = cp_lexer_consume_token (parser->lexer); | |
a723baf1 MM |
16106 | /* See if it starts a new group. */ |
16107 | if (token->type == CPP_OPEN_BRACE) | |
16108 | { | |
c162c75e | 16109 | cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1); |
a723baf1 MM |
16110 | if (depth == 0) |
16111 | return; | |
16112 | } | |
16113 | else if (token->type == CPP_OPEN_PAREN) | |
c162c75e | 16114 | cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1); |
a723baf1 MM |
16115 | else if (token->type == end) |
16116 | return; | |
16117 | } | |
16118 | } | |
16119 | ||
16120 | /* Begin parsing tentatively. We always save tokens while parsing | |
16121 | tentatively so that if the tentative parsing fails we can restore the | |
16122 | tokens. */ | |
16123 | ||
16124 | static void | |
94edc4ab | 16125 | cp_parser_parse_tentatively (cp_parser* parser) |
a723baf1 MM |
16126 | { |
16127 | /* Enter a new parsing context. */ | |
16128 | parser->context = cp_parser_context_new (parser->context); | |
16129 | /* Begin saving tokens. */ | |
16130 | cp_lexer_save_tokens (parser->lexer); | |
16131 | /* In order to avoid repetitive access control error messages, | |
16132 | access checks are queued up until we are no longer parsing | |
16133 | tentatively. */ | |
8d241e0b | 16134 | push_deferring_access_checks (dk_deferred); |
a723baf1 MM |
16135 | } |
16136 | ||
16137 | /* Commit to the currently active tentative parse. */ | |
16138 | ||
16139 | static void | |
94edc4ab | 16140 | cp_parser_commit_to_tentative_parse (cp_parser* parser) |
a723baf1 MM |
16141 | { |
16142 | cp_parser_context *context; | |
16143 | cp_lexer *lexer; | |
16144 | ||
16145 | /* Mark all of the levels as committed. */ | |
16146 | lexer = parser->lexer; | |
16147 | for (context = parser->context; context->next; context = context->next) | |
16148 | { | |
16149 | if (context->status == CP_PARSER_STATUS_KIND_COMMITTED) | |
16150 | break; | |
16151 | context->status = CP_PARSER_STATUS_KIND_COMMITTED; | |
16152 | while (!cp_lexer_saving_tokens (lexer)) | |
16153 | lexer = lexer->next; | |
16154 | cp_lexer_commit_tokens (lexer); | |
16155 | } | |
16156 | } | |
16157 | ||
16158 | /* Abort the currently active tentative parse. All consumed tokens | |
16159 | will be rolled back, and no diagnostics will be issued. */ | |
16160 | ||
16161 | static void | |
94edc4ab | 16162 | cp_parser_abort_tentative_parse (cp_parser* parser) |
a723baf1 MM |
16163 | { |
16164 | cp_parser_simulate_error (parser); | |
16165 | /* Now, pretend that we want to see if the construct was | |
16166 | successfully parsed. */ | |
16167 | cp_parser_parse_definitely (parser); | |
16168 | } | |
16169 | ||
34cd5ae7 | 16170 | /* Stop parsing tentatively. If a parse error has occurred, restore the |
a723baf1 MM |
16171 | token stream. Otherwise, commit to the tokens we have consumed. |
16172 | Returns true if no error occurred; false otherwise. */ | |
16173 | ||
16174 | static bool | |
94edc4ab | 16175 | cp_parser_parse_definitely (cp_parser* parser) |
a723baf1 MM |
16176 | { |
16177 | bool error_occurred; | |
16178 | cp_parser_context *context; | |
16179 | ||
34cd5ae7 | 16180 | /* Remember whether or not an error occurred, since we are about to |
a723baf1 MM |
16181 | destroy that information. */ |
16182 | error_occurred = cp_parser_error_occurred (parser); | |
16183 | /* Remove the topmost context from the stack. */ | |
16184 | context = parser->context; | |
16185 | parser->context = context->next; | |
16186 | /* If no parse errors occurred, commit to the tentative parse. */ | |
16187 | if (!error_occurred) | |
16188 | { | |
16189 | /* Commit to the tokens read tentatively, unless that was | |
16190 | already done. */ | |
16191 | if (context->status != CP_PARSER_STATUS_KIND_COMMITTED) | |
16192 | cp_lexer_commit_tokens (parser->lexer); | |
cf22909c KL |
16193 | |
16194 | pop_to_parent_deferring_access_checks (); | |
a723baf1 MM |
16195 | } |
16196 | /* Otherwise, if errors occurred, roll back our state so that things | |
16197 | are just as they were before we began the tentative parse. */ | |
16198 | else | |
cf22909c KL |
16199 | { |
16200 | cp_lexer_rollback_tokens (parser->lexer); | |
16201 | pop_deferring_access_checks (); | |
16202 | } | |
e5976695 MM |
16203 | /* Add the context to the front of the free list. */ |
16204 | context->next = cp_parser_context_free_list; | |
16205 | cp_parser_context_free_list = context; | |
16206 | ||
16207 | return !error_occurred; | |
a723baf1 MM |
16208 | } |
16209 | ||
0b16f8f4 VR |
16210 | /* Returns true if we are parsing tentatively and are not committed to |
16211 | this tentative parse. */ | |
a723baf1 MM |
16212 | |
16213 | static bool | |
0b16f8f4 | 16214 | cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser) |
a723baf1 MM |
16215 | { |
16216 | return (cp_parser_parsing_tentatively (parser) | |
0b16f8f4 | 16217 | && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED); |
a723baf1 MM |
16218 | } |
16219 | ||
4de8668e | 16220 | /* Returns nonzero iff an error has occurred during the most recent |
a723baf1 | 16221 | tentative parse. */ |
21526606 | 16222 | |
a723baf1 | 16223 | static bool |
94edc4ab | 16224 | cp_parser_error_occurred (cp_parser* parser) |
a723baf1 MM |
16225 | { |
16226 | return (cp_parser_parsing_tentatively (parser) | |
16227 | && parser->context->status == CP_PARSER_STATUS_KIND_ERROR); | |
16228 | } | |
16229 | ||
4de8668e | 16230 | /* Returns nonzero if GNU extensions are allowed. */ |
a723baf1 MM |
16231 | |
16232 | static bool | |
94edc4ab | 16233 | cp_parser_allow_gnu_extensions_p (cp_parser* parser) |
a723baf1 MM |
16234 | { |
16235 | return parser->allow_gnu_extensions_p; | |
16236 | } | |
e58a9aa1 ZL |
16237 | \f |
16238 | /* Objective-C++ Productions */ | |
16239 | ||
16240 | ||
16241 | /* Parse an Objective-C expression, which feeds into a primary-expression | |
16242 | above. | |
16243 | ||
16244 | objc-expression: | |
16245 | objc-message-expression | |
16246 | objc-string-literal | |
16247 | objc-encode-expression | |
16248 | objc-protocol-expression | |
16249 | objc-selector-expression | |
16250 | ||
16251 | Returns a tree representation of the expression. */ | |
16252 | ||
16253 | static tree | |
16254 | cp_parser_objc_expression (cp_parser* parser) | |
16255 | { | |
16256 | /* Try to figure out what kind of declaration is present. */ | |
16257 | cp_token *kwd = cp_lexer_peek_token (parser->lexer); | |
16258 | ||
16259 | switch (kwd->type) | |
16260 | { | |
16261 | case CPP_OPEN_SQUARE: | |
16262 | return cp_parser_objc_message_expression (parser); | |
16263 | ||
16264 | case CPP_OBJC_STRING: | |
16265 | kwd = cp_lexer_consume_token (parser->lexer); | |
16266 | return objc_build_string_object (kwd->value); | |
16267 | ||
16268 | case CPP_KEYWORD: | |
16269 | switch (kwd->keyword) | |
16270 | { | |
16271 | case RID_AT_ENCODE: | |
16272 | return cp_parser_objc_encode_expression (parser); | |
16273 | ||
16274 | case RID_AT_PROTOCOL: | |
16275 | return cp_parser_objc_protocol_expression (parser); | |
16276 | ||
16277 | case RID_AT_SELECTOR: | |
16278 | return cp_parser_objc_selector_expression (parser); | |
16279 | ||
16280 | default: | |
16281 | break; | |
16282 | } | |
16283 | default: | |
16284 | error ("misplaced `@%D' Objective-C++ construct", kwd->value); | |
16285 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
16286 | } | |
16287 | ||
16288 | return error_mark_node; | |
16289 | } | |
16290 | ||
16291 | /* Parse an Objective-C message expression. | |
16292 | ||
16293 | objc-message-expression: | |
16294 | [ objc-message-receiver objc-message-args ] | |
16295 | ||
16296 | Returns a representation of an Objective-C message. */ | |
16297 | ||
16298 | static tree | |
16299 | cp_parser_objc_message_expression (cp_parser* parser) | |
16300 | { | |
16301 | tree receiver, messageargs; | |
16302 | ||
16303 | cp_lexer_consume_token (parser->lexer); /* Eat '['. */ | |
16304 | receiver = cp_parser_objc_message_receiver (parser); | |
16305 | messageargs = cp_parser_objc_message_args (parser); | |
16306 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
16307 | ||
16308 | return objc_build_message_expr (build_tree_list (receiver, messageargs)); | |
16309 | } | |
16310 | ||
16311 | /* Parse an objc-message-receiver. | |
16312 | ||
16313 | objc-message-receiver: | |
e58a9aa1 | 16314 | expression |
660845bf | 16315 | simple-type-specifier |
e58a9aa1 ZL |
16316 | |
16317 | Returns a representation of the type or expression. */ | |
16318 | ||
16319 | static tree | |
16320 | cp_parser_objc_message_receiver (cp_parser* parser) | |
16321 | { | |
16322 | tree rcv; | |
e58a9aa1 ZL |
16323 | |
16324 | /* An Objective-C message receiver may be either (1) a type | |
16325 | or (2) an expression. */ | |
16326 | cp_parser_parse_tentatively (parser); | |
16327 | rcv = cp_parser_expression (parser, false); | |
16328 | ||
16329 | if (cp_parser_parse_definitely (parser)) | |
16330 | return rcv; | |
16331 | ||
660845bf ZL |
16332 | rcv = cp_parser_simple_type_specifier (parser, |
16333 | /*decl_specs=*/NULL, | |
16334 | CP_PARSER_FLAGS_NONE); | |
e58a9aa1 ZL |
16335 | |
16336 | return objc_get_class_reference (rcv); | |
16337 | } | |
16338 | ||
16339 | /* Parse the arguments and selectors comprising an Objective-C message. | |
16340 | ||
16341 | objc-message-args: | |
16342 | objc-selector | |
16343 | objc-selector-args | |
16344 | objc-selector-args , objc-comma-args | |
16345 | ||
16346 | objc-selector-args: | |
16347 | objc-selector [opt] : assignment-expression | |
16348 | objc-selector-args objc-selector [opt] : assignment-expression | |
16349 | ||
16350 | objc-comma-args: | |
16351 | assignment-expression | |
16352 | objc-comma-args , assignment-expression | |
16353 | ||
16354 | Returns a TREE_LIST, with TREE_PURPOSE containing a list of | |
16355 | selector arguments and TREE_VALUE containing a list of comma | |
16356 | arguments. */ | |
16357 | ||
16358 | static tree | |
16359 | cp_parser_objc_message_args (cp_parser* parser) | |
16360 | { | |
16361 | tree sel_args = NULL_TREE, addl_args = NULL_TREE; | |
16362 | bool maybe_unary_selector_p = true; | |
16363 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
16364 | ||
16365 | while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON) | |
16366 | { | |
16367 | tree selector = NULL_TREE, arg; | |
16368 | ||
16369 | if (token->type != CPP_COLON) | |
16370 | selector = cp_parser_objc_selector (parser); | |
16371 | ||
16372 | /* Detect if we have a unary selector. */ | |
16373 | if (maybe_unary_selector_p | |
16374 | && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) | |
16375 | return build_tree_list (selector, NULL_TREE); | |
16376 | ||
16377 | maybe_unary_selector_p = false; | |
16378 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
16379 | arg = cp_parser_assignment_expression (parser, false); | |
16380 | ||
16381 | sel_args | |
16382 | = chainon (sel_args, | |
16383 | build_tree_list (selector, arg)); | |
16384 | ||
16385 | token = cp_lexer_peek_token (parser->lexer); | |
16386 | } | |
16387 | ||
16388 | /* Handle non-selector arguments, if any. */ | |
16389 | while (token->type == CPP_COMMA) | |
16390 | { | |
16391 | tree arg; | |
16392 | ||
16393 | cp_lexer_consume_token (parser->lexer); | |
16394 | arg = cp_parser_assignment_expression (parser, false); | |
16395 | ||
16396 | addl_args | |
16397 | = chainon (addl_args, | |
16398 | build_tree_list (NULL_TREE, arg)); | |
16399 | ||
16400 | token = cp_lexer_peek_token (parser->lexer); | |
16401 | } | |
16402 | ||
16403 | return build_tree_list (sel_args, addl_args); | |
16404 | } | |
16405 | ||
16406 | /* Parse an Objective-C encode expression. | |
16407 | ||
16408 | objc-encode-expression: | |
16409 | @encode objc-typename | |
16410 | ||
16411 | Returns an encoded representation of the type argument. */ | |
16412 | ||
16413 | static tree | |
16414 | cp_parser_objc_encode_expression (cp_parser* parser) | |
16415 | { | |
16416 | tree type; | |
16417 | ||
16418 | cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */ | |
16419 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
16420 | type = complete_type (cp_parser_type_id (parser)); | |
16421 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
16422 | ||
16423 | if (!type) | |
16424 | { | |
16425 | error ("`@encode' must specify a type as an argument"); | |
16426 | return error_mark_node; | |
16427 | } | |
16428 | ||
16429 | return objc_build_encode_expr (type); | |
16430 | } | |
16431 | ||
16432 | /* Parse an Objective-C @defs expression. */ | |
16433 | ||
16434 | static tree | |
16435 | cp_parser_objc_defs_expression (cp_parser *parser) | |
16436 | { | |
16437 | tree name; | |
16438 | ||
16439 | cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */ | |
16440 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
16441 | name = cp_parser_identifier (parser); | |
16442 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
16443 | ||
16444 | return objc_get_class_ivars (name); | |
16445 | } | |
16446 | ||
16447 | /* Parse an Objective-C protocol expression. | |
16448 | ||
16449 | objc-protocol-expression: | |
16450 | @protocol ( identifier ) | |
16451 | ||
16452 | Returns a representation of the protocol expression. */ | |
16453 | ||
16454 | static tree | |
16455 | cp_parser_objc_protocol_expression (cp_parser* parser) | |
16456 | { | |
16457 | tree proto; | |
16458 | ||
16459 | cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */ | |
16460 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
16461 | proto = cp_parser_identifier (parser); | |
16462 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
16463 | ||
16464 | return objc_build_protocol_expr (proto); | |
16465 | } | |
16466 | ||
16467 | /* Parse an Objective-C selector expression. | |
16468 | ||
16469 | objc-selector-expression: | |
16470 | @selector ( objc-method-signature ) | |
16471 | ||
16472 | objc-method-signature: | |
16473 | objc-selector | |
16474 | objc-selector-seq | |
16475 | ||
16476 | objc-selector-seq: | |
16477 | objc-selector : | |
16478 | objc-selector-seq objc-selector : | |
16479 | ||
16480 | Returns a representation of the method selector. */ | |
16481 | ||
16482 | static tree | |
16483 | cp_parser_objc_selector_expression (cp_parser* parser) | |
16484 | { | |
16485 | tree sel_seq = NULL_TREE; | |
16486 | bool maybe_unary_selector_p = true; | |
16487 | cp_token *token; | |
16488 | ||
16489 | cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */ | |
16490 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
16491 | token = cp_lexer_peek_token (parser->lexer); | |
16492 | ||
16493 | while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON) | |
16494 | { | |
16495 | tree selector = NULL_TREE; | |
16496 | ||
16497 | if (token->type != CPP_COLON) | |
16498 | selector = cp_parser_objc_selector (parser); | |
16499 | ||
16500 | /* Detect if we have a unary selector. */ | |
16501 | if (maybe_unary_selector_p | |
16502 | && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) | |
16503 | { | |
16504 | sel_seq = selector; | |
16505 | goto finish_selector; | |
16506 | } | |
16507 | ||
16508 | maybe_unary_selector_p = false; | |
16509 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
16510 | ||
16511 | sel_seq | |
16512 | = chainon (sel_seq, | |
16513 | build_tree_list (selector, NULL_TREE)); | |
16514 | ||
16515 | token = cp_lexer_peek_token (parser->lexer); | |
16516 | } | |
16517 | ||
16518 | finish_selector: | |
16519 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
16520 | ||
16521 | return objc_build_selector_expr (sel_seq); | |
16522 | } | |
16523 | ||
16524 | /* Parse a list of identifiers. | |
16525 | ||
16526 | objc-identifier-list: | |
16527 | identifier | |
16528 | objc-identifier-list , identifier | |
16529 | ||
16530 | Returns a TREE_LIST of identifier nodes. */ | |
16531 | ||
16532 | static tree | |
16533 | cp_parser_objc_identifier_list (cp_parser* parser) | |
16534 | { | |
16535 | tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser)); | |
16536 | cp_token *sep = cp_lexer_peek_token (parser->lexer); | |
16537 | ||
16538 | while (sep->type == CPP_COMMA) | |
16539 | { | |
16540 | cp_lexer_consume_token (parser->lexer); /* Eat ','. */ | |
16541 | list = chainon (list, | |
16542 | build_tree_list (NULL_TREE, | |
16543 | cp_parser_identifier (parser))); | |
16544 | sep = cp_lexer_peek_token (parser->lexer); | |
16545 | } | |
16546 | ||
16547 | return list; | |
16548 | } | |
16549 | ||
16550 | /* Parse an Objective-C alias declaration. | |
16551 | ||
16552 | objc-alias-declaration: | |
16553 | @compatibility_alias identifier identifier ; | |
16554 | ||
16555 | This function registers the alias mapping with the Objective-C front-end. | |
16556 | It returns nothing. */ | |
16557 | ||
16558 | static void | |
16559 | cp_parser_objc_alias_declaration (cp_parser* parser) | |
16560 | { | |
16561 | tree alias, orig; | |
16562 | ||
16563 | cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */ | |
16564 | alias = cp_parser_identifier (parser); | |
16565 | orig = cp_parser_identifier (parser); | |
16566 | objc_declare_alias (alias, orig); | |
16567 | cp_parser_consume_semicolon_at_end_of_statement (parser); | |
16568 | } | |
16569 | ||
16570 | /* Parse an Objective-C class forward-declaration. | |
16571 | ||
16572 | objc-class-declaration: | |
16573 | @class objc-identifier-list ; | |
16574 | ||
16575 | The function registers the forward declarations with the Objective-C | |
16576 | front-end. It returns nothing. */ | |
16577 | ||
16578 | static void | |
16579 | cp_parser_objc_class_declaration (cp_parser* parser) | |
16580 | { | |
16581 | cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */ | |
16582 | objc_declare_class (cp_parser_objc_identifier_list (parser)); | |
16583 | cp_parser_consume_semicolon_at_end_of_statement (parser); | |
16584 | } | |
16585 | ||
16586 | /* Parse a list of Objective-C protocol references. | |
16587 | ||
16588 | objc-protocol-refs-opt: | |
16589 | objc-protocol-refs [opt] | |
16590 | ||
16591 | objc-protocol-refs: | |
16592 | < objc-identifier-list > | |
16593 | ||
16594 | Returns a TREE_LIST of identifiers, if any. */ | |
16595 | ||
16596 | static tree | |
16597 | cp_parser_objc_protocol_refs_opt (cp_parser* parser) | |
16598 | { | |
16599 | tree protorefs = NULL_TREE; | |
16600 | ||
16601 | if(cp_lexer_next_token_is (parser->lexer, CPP_LESS)) | |
16602 | { | |
16603 | cp_lexer_consume_token (parser->lexer); /* Eat '<'. */ | |
16604 | protorefs = cp_parser_objc_identifier_list (parser); | |
16605 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
16606 | } | |
16607 | ||
16608 | return protorefs; | |
16609 | } | |
16610 | ||
16611 | /* Parse a Objective-C visibility specification. */ | |
16612 | ||
16613 | static void | |
16614 | cp_parser_objc_visibility_spec (cp_parser* parser) | |
16615 | { | |
16616 | cp_token *vis = cp_lexer_peek_token (parser->lexer); | |
16617 | ||
16618 | switch (vis->keyword) | |
16619 | { | |
16620 | case RID_AT_PRIVATE: | |
16621 | objc_set_visibility (2); | |
16622 | break; | |
16623 | case RID_AT_PROTECTED: | |
16624 | objc_set_visibility (0); | |
16625 | break; | |
16626 | case RID_AT_PUBLIC: | |
16627 | objc_set_visibility (1); | |
16628 | break; | |
16629 | default: | |
16630 | return; | |
16631 | } | |
a723baf1 | 16632 | |
e58a9aa1 ZL |
16633 | /* Eat '@private'/'@protected'/'@public'. */ |
16634 | cp_lexer_consume_token (parser->lexer); | |
16635 | } | |
16636 | ||
16637 | /* Parse an Objective-C method type. */ | |
16638 | ||
16639 | static void | |
16640 | cp_parser_objc_method_type (cp_parser* parser) | |
16641 | { | |
16642 | objc_set_method_type | |
16643 | (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS | |
16644 | ? PLUS_EXPR | |
16645 | : MINUS_EXPR); | |
16646 | } | |
16647 | ||
16648 | /* Parse an Objective-C protocol qualifier. */ | |
16649 | ||
16650 | static tree | |
16651 | cp_parser_objc_protocol_qualifiers (cp_parser* parser) | |
16652 | { | |
16653 | tree quals = NULL_TREE, node; | |
16654 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
16655 | ||
16656 | node = token->value; | |
16657 | ||
16658 | while (node && TREE_CODE (node) == IDENTIFIER_NODE | |
16659 | && (node == ridpointers [(int) RID_IN] | |
16660 | || node == ridpointers [(int) RID_OUT] | |
16661 | || node == ridpointers [(int) RID_INOUT] | |
16662 | || node == ridpointers [(int) RID_BYCOPY] | |
16663 | || node == ridpointers [(int) RID_BYREF] | |
16664 | || node == ridpointers [(int) RID_ONEWAY])) | |
16665 | { | |
16666 | quals = tree_cons (NULL_TREE, node, quals); | |
16667 | cp_lexer_consume_token (parser->lexer); | |
16668 | token = cp_lexer_peek_token (parser->lexer); | |
16669 | node = token->value; | |
16670 | } | |
16671 | ||
16672 | return quals; | |
16673 | } | |
16674 | ||
16675 | /* Parse an Objective-C typename. */ | |
16676 | ||
16677 | static tree | |
16678 | cp_parser_objc_typename (cp_parser* parser) | |
16679 | { | |
16680 | tree typename = NULL_TREE; | |
16681 | ||
16682 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
16683 | { | |
16684 | tree proto_quals, cp_type = NULL_TREE; | |
16685 | ||
16686 | cp_lexer_consume_token (parser->lexer); /* Eat '('. */ | |
16687 | proto_quals = cp_parser_objc_protocol_qualifiers (parser); | |
16688 | ||
16689 | /* An ObjC type name may consist of just protocol qualifiers, in which | |
16690 | case the type shall default to 'id'. */ | |
16691 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) | |
16692 | cp_type = cp_parser_type_id (parser); | |
16693 | ||
16694 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
16695 | typename = build_tree_list (proto_quals, cp_type); | |
16696 | } | |
16697 | ||
16698 | return typename; | |
16699 | } | |
16700 | ||
16701 | /* Check to see if TYPE refers to an Objective-C selector name. */ | |
16702 | ||
16703 | static bool | |
16704 | cp_parser_objc_selector_p (enum cpp_ttype type) | |
16705 | { | |
16706 | return (type == CPP_NAME || type == CPP_KEYWORD | |
16707 | || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND | |
16708 | || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT | |
16709 | || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ | |
16710 | || type == CPP_XOR || type == CPP_XOR_EQ); | |
16711 | } | |
16712 | ||
16713 | /* Parse an Objective-C selector. */ | |
16714 | ||
16715 | static tree | |
16716 | cp_parser_objc_selector (cp_parser* parser) | |
16717 | { | |
16718 | cp_token *token = cp_lexer_consume_token (parser->lexer); | |
16719 | ||
16720 | if (!cp_parser_objc_selector_p (token->type)) | |
16721 | { | |
16722 | error ("invalid Objective-C++ selector name"); | |
16723 | return error_mark_node; | |
16724 | } | |
16725 | ||
16726 | /* C++ operator names are allowed to appear in ObjC selectors. */ | |
16727 | switch (token->type) | |
16728 | { | |
16729 | case CPP_AND_AND: return get_identifier ("and"); | |
16730 | case CPP_AND_EQ: return get_identifier ("and_eq"); | |
16731 | case CPP_AND: return get_identifier ("bitand"); | |
16732 | case CPP_OR: return get_identifier ("bitor"); | |
16733 | case CPP_COMPL: return get_identifier ("compl"); | |
16734 | case CPP_NOT: return get_identifier ("not"); | |
16735 | case CPP_NOT_EQ: return get_identifier ("not_eq"); | |
16736 | case CPP_OR_OR: return get_identifier ("or"); | |
16737 | case CPP_OR_EQ: return get_identifier ("or_eq"); | |
16738 | case CPP_XOR: return get_identifier ("xor"); | |
16739 | case CPP_XOR_EQ: return get_identifier ("xor_eq"); | |
16740 | default: return token->value; | |
16741 | } | |
16742 | } | |
16743 | ||
16744 | /* Parse an Objective-C params list. */ | |
16745 | ||
16746 | static tree | |
16747 | cp_parser_objc_method_keyword_params (cp_parser* parser) | |
16748 | { | |
16749 | tree params = NULL_TREE; | |
16750 | bool maybe_unary_selector_p = true; | |
16751 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
16752 | ||
16753 | while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON) | |
16754 | { | |
16755 | tree selector = NULL_TREE, typename, identifier; | |
16756 | ||
16757 | if (token->type != CPP_COLON) | |
16758 | selector = cp_parser_objc_selector (parser); | |
16759 | ||
16760 | /* Detect if we have a unary selector. */ | |
16761 | if (maybe_unary_selector_p | |
16762 | && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) | |
16763 | return selector; | |
16764 | ||
16765 | maybe_unary_selector_p = false; | |
16766 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
16767 | typename = cp_parser_objc_typename (parser); | |
16768 | identifier = cp_parser_identifier (parser); | |
16769 | ||
16770 | params | |
16771 | = chainon (params, | |
16772 | objc_build_keyword_decl (selector, | |
16773 | typename, | |
16774 | identifier)); | |
16775 | ||
16776 | token = cp_lexer_peek_token (parser->lexer); | |
16777 | } | |
16778 | ||
16779 | return params; | |
16780 | } | |
16781 | ||
16782 | /* Parse the non-keyword Objective-C params. */ | |
16783 | ||
16784 | static tree | |
16785 | cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp) | |
16786 | { | |
16787 | tree params = make_node (TREE_LIST); | |
16788 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
16789 | *ellipsisp = false; /* Initially, assume no ellipsis. */ | |
16790 | ||
16791 | while (token->type == CPP_COMMA) | |
16792 | { | |
16793 | cp_parameter_declarator *parmdecl; | |
16794 | tree parm; | |
16795 | ||
16796 | cp_lexer_consume_token (parser->lexer); /* Eat ','. */ | |
16797 | token = cp_lexer_peek_token (parser->lexer); | |
16798 | ||
16799 | if (token->type == CPP_ELLIPSIS) | |
16800 | { | |
16801 | cp_lexer_consume_token (parser->lexer); /* Eat '...'. */ | |
16802 | *ellipsisp = true; | |
16803 | break; | |
16804 | } | |
16805 | ||
16806 | parmdecl = cp_parser_parameter_declaration (parser, false, NULL); | |
16807 | parm = grokdeclarator (parmdecl->declarator, | |
16808 | &parmdecl->decl_specifiers, | |
16809 | PARM, /*initialized=*/0, | |
16810 | /*attrlist=*/NULL); | |
16811 | ||
16812 | chainon (params, build_tree_list (NULL_TREE, parm)); | |
16813 | token = cp_lexer_peek_token (parser->lexer); | |
16814 | } | |
16815 | ||
16816 | return params; | |
16817 | } | |
16818 | ||
16819 | /* Parse a linkage specification, a pragma, an extra semicolon or a block. */ | |
16820 | ||
16821 | static void | |
16822 | cp_parser_objc_interstitial_code (cp_parser* parser) | |
16823 | { | |
16824 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
16825 | ||
16826 | /* If the next token is `extern' and the following token is a string | |
16827 | literal, then we have a linkage specification. */ | |
16828 | if (token->keyword == RID_EXTERN | |
16829 | && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2))) | |
16830 | cp_parser_linkage_specification (parser); | |
16831 | /* Handle #pragma, if any. */ | |
16832 | else if (token->type == CPP_PRAGMA) | |
16833 | cp_lexer_handle_pragma (parser->lexer); | |
16834 | /* Allow stray semicolons. */ | |
16835 | else if (token->type == CPP_SEMICOLON) | |
16836 | cp_lexer_consume_token (parser->lexer); | |
16837 | /* Finally, try to parse a block-declaration, or a function-definition. */ | |
16838 | else | |
16839 | cp_parser_block_declaration (parser, /*statement_p=*/false); | |
16840 | } | |
16841 | ||
16842 | /* Parse a method signature. */ | |
16843 | ||
16844 | static tree | |
16845 | cp_parser_objc_method_signature (cp_parser* parser) | |
16846 | { | |
16847 | tree rettype, kwdparms, optparms; | |
16848 | bool ellipsis = false; | |
16849 | ||
16850 | cp_parser_objc_method_type (parser); | |
16851 | rettype = cp_parser_objc_typename (parser); | |
16852 | kwdparms = cp_parser_objc_method_keyword_params (parser); | |
16853 | optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis); | |
16854 | ||
16855 | return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis); | |
16856 | } | |
16857 | ||
16858 | /* Pars an Objective-C method prototype list. */ | |
16859 | ||
16860 | static void | |
16861 | cp_parser_objc_method_prototype_list (cp_parser* parser) | |
16862 | { | |
16863 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
16864 | ||
16865 | while (token->keyword != RID_AT_END) | |
16866 | { | |
16867 | if (token->type == CPP_PLUS || token->type == CPP_MINUS) | |
16868 | { | |
16869 | objc_add_method_declaration | |
16870 | (cp_parser_objc_method_signature (parser)); | |
16871 | cp_parser_consume_semicolon_at_end_of_statement (parser); | |
16872 | } | |
16873 | else | |
16874 | /* Allow for interspersed non-ObjC++ code. */ | |
16875 | cp_parser_objc_interstitial_code (parser); | |
16876 | ||
16877 | token = cp_lexer_peek_token (parser->lexer); | |
16878 | } | |
16879 | ||
16880 | cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */ | |
16881 | objc_finish_interface (); | |
16882 | } | |
16883 | ||
16884 | /* Parse an Objective-C method definition list. */ | |
16885 | ||
16886 | static void | |
16887 | cp_parser_objc_method_definition_list (cp_parser* parser) | |
16888 | { | |
16889 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
16890 | ||
16891 | while (token->keyword != RID_AT_END) | |
16892 | { | |
16893 | tree meth; | |
16894 | ||
16895 | if (token->type == CPP_PLUS || token->type == CPP_MINUS) | |
16896 | { | |
16897 | push_deferring_access_checks (dk_deferred); | |
16898 | objc_start_method_definition | |
16899 | (cp_parser_objc_method_signature (parser)); | |
16900 | ||
16901 | /* For historical reasons, we accept an optional semicolon. */ | |
16902 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
16903 | cp_lexer_consume_token (parser->lexer); | |
16904 | ||
16905 | perform_deferred_access_checks (); | |
16906 | stop_deferring_access_checks (); | |
16907 | meth = cp_parser_function_definition_after_declarator (parser, | |
16908 | false); | |
16909 | pop_deferring_access_checks (); | |
16910 | objc_finish_method_definition (meth); | |
16911 | } | |
16912 | else | |
16913 | /* Allow for interspersed non-ObjC++ code. */ | |
16914 | cp_parser_objc_interstitial_code (parser); | |
16915 | ||
16916 | token = cp_lexer_peek_token (parser->lexer); | |
16917 | } | |
16918 | ||
16919 | cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */ | |
16920 | objc_finish_implementation (); | |
16921 | } | |
16922 | ||
16923 | /* Parse Objective-C ivars. */ | |
16924 | ||
16925 | static void | |
16926 | cp_parser_objc_class_ivars (cp_parser* parser) | |
16927 | { | |
16928 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
16929 | ||
16930 | if (token->type != CPP_OPEN_BRACE) | |
16931 | return; /* No ivars specified. */ | |
16932 | ||
16933 | cp_lexer_consume_token (parser->lexer); /* Eat '{'. */ | |
16934 | token = cp_lexer_peek_token (parser->lexer); | |
16935 | ||
16936 | while (token->type != CPP_CLOSE_BRACE) | |
16937 | { | |
16938 | cp_decl_specifier_seq declspecs; | |
16939 | int decl_class_or_enum_p; | |
16940 | tree prefix_attributes; | |
16941 | ||
16942 | cp_parser_objc_visibility_spec (parser); | |
16943 | ||
16944 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
16945 | break; | |
16946 | ||
16947 | cp_parser_decl_specifier_seq (parser, | |
16948 | CP_PARSER_FLAGS_OPTIONAL, | |
16949 | &declspecs, | |
16950 | &decl_class_or_enum_p); | |
16951 | prefix_attributes = declspecs.attributes; | |
16952 | declspecs.attributes = NULL_TREE; | |
16953 | ||
16954 | /* Keep going until we hit the `;' at the end of the | |
16955 | declaration. */ | |
16956 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
16957 | { | |
16958 | tree width = NULL_TREE, attributes, first_attribute, decl; | |
16959 | cp_declarator *declarator = NULL; | |
16960 | int ctor_dtor_or_conv_p; | |
16961 | ||
16962 | /* Check for a (possibly unnamed) bitfield declaration. */ | |
16963 | token = cp_lexer_peek_token (parser->lexer); | |
16964 | if (token->type == CPP_COLON) | |
16965 | goto eat_colon; | |
16966 | ||
16967 | if (token->type == CPP_NAME | |
16968 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type | |
16969 | == CPP_COLON)) | |
16970 | { | |
16971 | /* Get the name of the bitfield. */ | |
16972 | declarator = make_id_declarator (NULL_TREE, | |
16973 | cp_parser_identifier (parser)); | |
16974 | ||
16975 | eat_colon: | |
16976 | cp_lexer_consume_token (parser->lexer); /* Eat ':'. */ | |
16977 | /* Get the width of the bitfield. */ | |
16978 | width | |
16979 | = cp_parser_constant_expression (parser, | |
16980 | /*allow_non_constant=*/false, | |
16981 | NULL); | |
16982 | } | |
16983 | else | |
16984 | { | |
16985 | /* Parse the declarator. */ | |
16986 | declarator | |
16987 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, | |
16988 | &ctor_dtor_or_conv_p, | |
16989 | /*parenthesized_p=*/NULL, | |
16990 | /*member_p=*/false); | |
16991 | } | |
16992 | ||
16993 | /* Look for attributes that apply to the ivar. */ | |
16994 | attributes = cp_parser_attributes_opt (parser); | |
16995 | /* Remember which attributes are prefix attributes and | |
16996 | which are not. */ | |
16997 | first_attribute = attributes; | |
16998 | /* Combine the attributes. */ | |
16999 | attributes = chainon (prefix_attributes, attributes); | |
17000 | ||
17001 | if (width) | |
17002 | { | |
17003 | /* Create the bitfield declaration. */ | |
17004 | decl = grokbitfield (declarator, &declspecs, width); | |
17005 | cplus_decl_attributes (&decl, attributes, /*flags=*/0); | |
17006 | } | |
17007 | else | |
17008 | decl = grokfield (declarator, &declspecs, NULL_TREE, | |
17009 | NULL_TREE, attributes); | |
17010 | ||
17011 | /* Add the instance variable. */ | |
17012 | objc_add_instance_variable (decl); | |
17013 | ||
17014 | /* Reset PREFIX_ATTRIBUTES. */ | |
17015 | while (attributes && TREE_CHAIN (attributes) != first_attribute) | |
17016 | attributes = TREE_CHAIN (attributes); | |
17017 | if (attributes) | |
17018 | TREE_CHAIN (attributes) = NULL_TREE; | |
17019 | ||
17020 | token = cp_lexer_peek_token (parser->lexer); | |
17021 | ||
17022 | if (token->type == CPP_COMMA) | |
17023 | { | |
17024 | cp_lexer_consume_token (parser->lexer); /* Eat ','. */ | |
17025 | continue; | |
17026 | } | |
17027 | break; | |
17028 | } | |
17029 | ||
17030 | cp_parser_consume_semicolon_at_end_of_statement (parser); | |
17031 | token = cp_lexer_peek_token (parser->lexer); | |
17032 | } | |
17033 | ||
17034 | cp_lexer_consume_token (parser->lexer); /* Eat '}'. */ | |
17035 | /* For historical reasons, we accept an optional semicolon. */ | |
17036 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
17037 | cp_lexer_consume_token (parser->lexer); | |
17038 | } | |
17039 | ||
17040 | /* Parse an Objective-C protocol declaration. */ | |
17041 | ||
17042 | static void | |
17043 | cp_parser_objc_protocol_declaration (cp_parser* parser) | |
17044 | { | |
17045 | tree proto, protorefs; | |
17046 | cp_token *tok; | |
17047 | ||
17048 | cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */ | |
17049 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)) | |
17050 | { | |
17051 | error ("identifier expected after `@protocol'"); | |
17052 | goto finish; | |
17053 | } | |
17054 | ||
128a79fb | 17055 | /* See if we have a forward declaration or a definition. */ |
e58a9aa1 ZL |
17056 | tok = cp_lexer_peek_nth_token (parser->lexer, 2); |
17057 | ||
17058 | /* Try a forward declaration first. */ | |
17059 | if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON) | |
17060 | { | |
17061 | objc_declare_protocols (cp_parser_objc_identifier_list (parser)); | |
17062 | finish: | |
17063 | cp_parser_consume_semicolon_at_end_of_statement (parser); | |
17064 | } | |
17065 | ||
17066 | /* Ok, we got a full-fledged definition (or at least should). */ | |
17067 | else | |
17068 | { | |
17069 | proto = cp_parser_identifier (parser); | |
17070 | protorefs = cp_parser_objc_protocol_refs_opt (parser); | |
17071 | objc_start_protocol (proto, protorefs); | |
17072 | cp_parser_objc_method_prototype_list (parser); | |
17073 | } | |
17074 | } | |
17075 | ||
17076 | /* Parse an Objective-C superclass or category. */ | |
17077 | ||
17078 | static void | |
17079 | cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super, | |
17080 | tree *categ) | |
17081 | { | |
17082 | cp_token *next = cp_lexer_peek_token (parser->lexer); | |
17083 | ||
17084 | *super = *categ = NULL_TREE; | |
17085 | if (next->type == CPP_COLON) | |
17086 | { | |
17087 | cp_lexer_consume_token (parser->lexer); /* Eat ':'. */ | |
17088 | *super = cp_parser_identifier (parser); | |
17089 | } | |
17090 | else if (next->type == CPP_OPEN_PAREN) | |
17091 | { | |
17092 | cp_lexer_consume_token (parser->lexer); /* Eat '('. */ | |
17093 | *categ = cp_parser_identifier (parser); | |
17094 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
17095 | } | |
17096 | } | |
17097 | ||
17098 | /* Parse an Objective-C class interface. */ | |
17099 | ||
17100 | static void | |
17101 | cp_parser_objc_class_interface (cp_parser* parser) | |
17102 | { | |
17103 | tree name, super, categ, protos; | |
17104 | ||
17105 | cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */ | |
17106 | name = cp_parser_identifier (parser); | |
17107 | cp_parser_objc_superclass_or_category (parser, &super, &categ); | |
17108 | protos = cp_parser_objc_protocol_refs_opt (parser); | |
17109 | ||
17110 | /* We have either a class or a category on our hands. */ | |
17111 | if (categ) | |
17112 | objc_start_category_interface (name, categ, protos); | |
17113 | else | |
17114 | { | |
17115 | objc_start_class_interface (name, super, protos); | |
17116 | /* Handle instance variable declarations, if any. */ | |
17117 | cp_parser_objc_class_ivars (parser); | |
17118 | objc_continue_interface (); | |
17119 | } | |
17120 | ||
17121 | cp_parser_objc_method_prototype_list (parser); | |
17122 | } | |
17123 | ||
17124 | /* Parse an Objective-C class implementation. */ | |
17125 | ||
17126 | static void | |
17127 | cp_parser_objc_class_implementation (cp_parser* parser) | |
17128 | { | |
17129 | tree name, super, categ; | |
17130 | ||
17131 | cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */ | |
17132 | name = cp_parser_identifier (parser); | |
17133 | cp_parser_objc_superclass_or_category (parser, &super, &categ); | |
17134 | ||
17135 | /* We have either a class or a category on our hands. */ | |
17136 | if (categ) | |
17137 | objc_start_category_implementation (name, categ); | |
17138 | else | |
17139 | { | |
17140 | objc_start_class_implementation (name, super); | |
17141 | /* Handle instance variable declarations, if any. */ | |
17142 | cp_parser_objc_class_ivars (parser); | |
17143 | objc_continue_implementation (); | |
17144 | } | |
17145 | ||
17146 | cp_parser_objc_method_definition_list (parser); | |
17147 | } | |
17148 | ||
17149 | /* Consume the @end token and finish off the implementation. */ | |
17150 | ||
17151 | static void | |
17152 | cp_parser_objc_end_implementation (cp_parser* parser) | |
17153 | { | |
17154 | cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */ | |
17155 | objc_finish_implementation (); | |
17156 | } | |
17157 | ||
17158 | /* Parse an Objective-C declaration. */ | |
17159 | ||
17160 | static void | |
17161 | cp_parser_objc_declaration (cp_parser* parser) | |
17162 | { | |
17163 | /* Try to figure out what kind of declaration is present. */ | |
17164 | cp_token *kwd = cp_lexer_peek_token (parser->lexer); | |
17165 | ||
17166 | switch (kwd->keyword) | |
17167 | { | |
17168 | case RID_AT_ALIAS: | |
17169 | cp_parser_objc_alias_declaration (parser); | |
17170 | break; | |
17171 | case RID_AT_CLASS: | |
17172 | cp_parser_objc_class_declaration (parser); | |
17173 | break; | |
17174 | case RID_AT_PROTOCOL: | |
17175 | cp_parser_objc_protocol_declaration (parser); | |
17176 | break; | |
17177 | case RID_AT_INTERFACE: | |
17178 | cp_parser_objc_class_interface (parser); | |
17179 | break; | |
17180 | case RID_AT_IMPLEMENTATION: | |
17181 | cp_parser_objc_class_implementation (parser); | |
17182 | break; | |
17183 | case RID_AT_END: | |
17184 | cp_parser_objc_end_implementation (parser); | |
17185 | break; | |
17186 | default: | |
17187 | error ("misplaced `@%D' Objective-C++ construct", kwd->value); | |
17188 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
17189 | } | |
17190 | } | |
17191 | ||
17192 | /* Parse an Objective-C try-catch-finally statement. | |
17193 | ||
17194 | objc-try-catch-finally-stmt: | |
17195 | @try compound-statement objc-catch-clause-seq [opt] | |
17196 | objc-finally-clause [opt] | |
17197 | ||
17198 | objc-catch-clause-seq: | |
17199 | objc-catch-clause objc-catch-clause-seq [opt] | |
17200 | ||
17201 | objc-catch-clause: | |
17202 | @catch ( exception-declaration ) compound-statement | |
17203 | ||
17204 | objc-finally-clause | |
17205 | @finally compound-statement | |
17206 | ||
17207 | Returns NULL_TREE. */ | |
17208 | ||
17209 | static tree | |
17210 | cp_parser_objc_try_catch_finally_statement (cp_parser *parser) { | |
17211 | location_t location; | |
17212 | tree stmt; | |
17213 | ||
17214 | cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'"); | |
17215 | location = cp_lexer_peek_token (parser->lexer)->location; | |
17216 | /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST | |
17217 | node, lest it get absorbed into the surrounding block. */ | |
17218 | stmt = push_stmt_list (); | |
17219 | cp_parser_compound_statement (parser, NULL, false); | |
17220 | objc_begin_try_stmt (location, pop_stmt_list (stmt)); | |
17221 | ||
17222 | while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH)) | |
17223 | { | |
17224 | cp_parameter_declarator *parmdecl; | |
17225 | tree parm; | |
17226 | ||
17227 | cp_lexer_consume_token (parser->lexer); | |
17228 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
17229 | parmdecl = cp_parser_parameter_declaration (parser, false, NULL); | |
17230 | parm = grokdeclarator (parmdecl->declarator, | |
17231 | &parmdecl->decl_specifiers, | |
17232 | PARM, /*initialized=*/0, | |
17233 | /*attrlist=*/NULL); | |
17234 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
17235 | objc_begin_catch_clause (parm); | |
17236 | cp_parser_compound_statement (parser, NULL, false); | |
17237 | objc_finish_catch_clause (); | |
17238 | } | |
17239 | ||
17240 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY)) | |
17241 | { | |
17242 | cp_lexer_consume_token (parser->lexer); | |
17243 | location = cp_lexer_peek_token (parser->lexer)->location; | |
17244 | /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST | |
17245 | node, lest it get absorbed into the surrounding block. */ | |
17246 | stmt = push_stmt_list (); | |
17247 | cp_parser_compound_statement (parser, NULL, false); | |
17248 | objc_build_finally_clause (location, pop_stmt_list (stmt)); | |
17249 | } | |
17250 | ||
17251 | return objc_finish_try_stmt (); | |
17252 | } | |
17253 | ||
17254 | /* Parse an Objective-C synchronized statement. | |
17255 | ||
17256 | objc-synchronized-stmt: | |
17257 | @synchronized ( expression ) compound-statement | |
17258 | ||
17259 | Returns NULL_TREE. */ | |
17260 | ||
17261 | static tree | |
17262 | cp_parser_objc_synchronized_statement (cp_parser *parser) { | |
17263 | location_t location; | |
17264 | tree lock, stmt; | |
17265 | ||
17266 | cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'"); | |
17267 | ||
17268 | location = cp_lexer_peek_token (parser->lexer)->location; | |
17269 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
17270 | lock = cp_parser_expression (parser, false); | |
17271 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
17272 | ||
17273 | /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST | |
17274 | node, lest it get absorbed into the surrounding block. */ | |
17275 | stmt = push_stmt_list (); | |
17276 | cp_parser_compound_statement (parser, NULL, false); | |
17277 | ||
17278 | return objc_build_synchronized (location, lock, pop_stmt_list (stmt)); | |
17279 | } | |
17280 | ||
17281 | /* Parse an Objective-C throw statement. | |
17282 | ||
17283 | objc-throw-stmt: | |
17284 | @throw assignment-expression [opt] ; | |
17285 | ||
17286 | Returns a constructed '@throw' statement. */ | |
17287 | ||
17288 | static tree | |
17289 | cp_parser_objc_throw_statement (cp_parser *parser) { | |
17290 | tree expr = NULL_TREE; | |
17291 | ||
17292 | cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'"); | |
17293 | ||
17294 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
17295 | expr = cp_parser_assignment_expression (parser, false); | |
17296 | ||
17297 | cp_parser_consume_semicolon_at_end_of_statement (parser); | |
17298 | ||
17299 | return objc_build_throw_stmt (expr); | |
17300 | } | |
17301 | ||
17302 | /* Parse an Objective-C statement. */ | |
17303 | ||
17304 | static tree | |
17305 | cp_parser_objc_statement (cp_parser * parser) { | |
17306 | /* Try to figure out what kind of declaration is present. */ | |
17307 | cp_token *kwd = cp_lexer_peek_token (parser->lexer); | |
17308 | ||
17309 | switch (kwd->keyword) | |
17310 | { | |
17311 | case RID_AT_TRY: | |
17312 | return cp_parser_objc_try_catch_finally_statement (parser); | |
17313 | case RID_AT_SYNCHRONIZED: | |
17314 | return cp_parser_objc_synchronized_statement (parser); | |
17315 | case RID_AT_THROW: | |
17316 | return cp_parser_objc_throw_statement (parser); | |
17317 | default: | |
17318 | error ("misplaced `@%D' Objective-C++ construct", kwd->value); | |
17319 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
17320 | } | |
17321 | ||
17322 | return error_mark_node; | |
17323 | } | |
a723baf1 | 17324 | \f |
a723baf1 MM |
17325 | /* The parser. */ |
17326 | ||
17327 | static GTY (()) cp_parser *the_parser; | |
17328 | ||
17329 | /* External interface. */ | |
17330 | ||
d1bd0ded | 17331 | /* Parse one entire translation unit. */ |
a723baf1 | 17332 | |
d1bd0ded GK |
17333 | void |
17334 | c_parse_file (void) | |
a723baf1 MM |
17335 | { |
17336 | bool error_occurred; | |
f75fbaf7 ZW |
17337 | static bool already_called = false; |
17338 | ||
17339 | if (already_called) | |
17340 | { | |
17341 | sorry ("inter-module optimizations not implemented for C++"); | |
17342 | return; | |
17343 | } | |
17344 | already_called = true; | |
a723baf1 MM |
17345 | |
17346 | the_parser = cp_parser_new (); | |
78757caa KL |
17347 | push_deferring_access_checks (flag_access_control |
17348 | ? dk_no_deferred : dk_no_check); | |
a723baf1 MM |
17349 | error_occurred = cp_parser_translation_unit (the_parser); |
17350 | the_parser = NULL; | |
a723baf1 MM |
17351 | } |
17352 | ||
a723baf1 MM |
17353 | /* This variable must be provided by every front end. */ |
17354 | ||
17355 | int yydebug; | |
17356 | ||
17357 | #include "gt-cp-parser.h" |